Back to index

webcit  8.12-dfsg
dav_main.c
Go to the documentation of this file.
00001 /*
00002  * Entry point for GroupDAV functions
00003  *
00004  * Copyright (c) 2005-2012 by the citadel.org team
00005  *
00006  * This program is open source software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License, version 3.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "webcit.h"
00016 #include "webserver.h"
00017 #include "dav.h"
00018 
00019 extern HashList *HandlerHash;
00020 
00021 HashList *DavNamespaces = NULL;
00022 
00023 /*
00024  * Output HTTP headers which are common to all requests.
00025  *
00026  * Please observe that we don't use the usual output_headers()
00027  * and wDumpContent() functions in the GroupDAV subsystem, so we
00028  * do our own header stuff here.
00029  *
00030  */
00031 void dav_common_headers(void) {
00032        hprintf(
00033               "Server: %s / %s\r\n"
00034               "Connection: close\r\n",
00035               PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
00036        );
00037 }
00038 
00039 
00040 
00041 /*
00042  * string conversion function
00043  */
00044 void euid_escapize(char *target, const char *source) {
00045        int i, len;
00046        int target_length = 0;
00047 
00048        strcpy(target, "");
00049        len = strlen(source);
00050        for (i=0; i<len; ++i) {
00051               if ( (isalnum(source[i])) || (source[i]=='-') || (source[i]=='_') ) {
00052                      target[target_length] = source[i];
00053                      target[++target_length] = 0;
00054               }
00055               else {
00056                      sprintf(&target[target_length], "=%02X", (0xFF & source[i]));
00057                      target_length += 3;
00058               }
00059        }
00060 }
00061 
00062 /*
00063  * string conversion function
00064  */
00065 void euid_unescapize(char *target, const char *source) {
00066        int a, b, len;
00067        char hex[3];
00068        int target_length = 0;
00069 
00070        strcpy(target, "");
00071 
00072        len = strlen(source);
00073        for (a = 0; a < len; ++a) {
00074               if (source[a] == '=') {
00075                      hex[0] = source[a + 1];
00076                      hex[1] = source[a + 2];
00077                      hex[2] = 0;
00078                      b = 0;
00079                      b = decode_hex(hex);
00080                      target[target_length] = b;
00081                      target[++target_length] = 0;
00082                      a += 2;
00083               }
00084               else {
00085                      target[target_length] = source[a];
00086                      target[++target_length] = 0;
00087               }
00088        }
00089 }
00090 
00091 
00092 
00093 
00094 /*
00095  * Main entry point for GroupDAV requests
00096  */
00097 void dav_main(void)
00098 {
00099        wcsession *WCC = WC;
00100        int i, len;
00101 
00102        syslog(LOG_DEBUG, "dav_main() called, logged_in=%d", WCC->logged_in );
00103 
00104        StrBufUnescape(WCC->Hdr->HR.ReqLine, 0);
00105        StrBufStripSlashes(WCC->Hdr->HR.ReqLine, 0);
00106 
00107        /*
00108         * If there's an If-Match: header, strip out the quotes if present, and
00109         * then if all that's left is an asterisk, make it go away entirely.
00110         */
00111        len = StrLength(WCC->Hdr->HR.dav_ifmatch);
00112        if (len > 0) {
00113               StrBufTrim(WCC->Hdr->HR.dav_ifmatch);
00114               if (ChrPtr(WCC->Hdr->HR.dav_ifmatch)[0] == '\"') {
00115                      StrBufCutLeft(WCC->Hdr->HR.dav_ifmatch, 1);
00116                      len --;
00117                      for (i=0; i<len; ++i) {
00118                             if (ChrPtr(WCC->Hdr->HR.dav_ifmatch)[i] == '\"') {
00119                                    StrBufCutAt(WCC->Hdr->HR.dav_ifmatch, i, NULL);
00120                                    len = StrLength(WCC->Hdr->HR.dav_ifmatch);
00121                             }
00122                      }
00123               }
00124               if (!strcmp(ChrPtr(WCC->Hdr->HR.dav_ifmatch), "*")) {
00125                      FlushStrBuf(WCC->Hdr->HR.dav_ifmatch);
00126               }
00127        }
00128 
00129        switch (WCC->Hdr->HR.eReqType)
00130        {
00131        /*
00132         * The OPTIONS method is not required by GroupDAV but it will be
00133         * needed for future implementations of other DAV-based protocols.
00134         */
00135        case eOPTIONS:
00136               dav_options();
00137               break;
00138 
00139        /*
00140         * The PROPFIND method is basically used to list all objects in a
00141         * room, or to list all relevant rooms on the server.
00142         */
00143        case ePROPFIND:
00144               dav_propfind();
00145               break;
00146 
00147        /*
00148         * The GET method is used for fetching individual items.
00149         */
00150        case eGET:
00151               dav_get();
00152               break;
00153        
00154        /*
00155         * The PUT method is used to add or modify items.
00156         */
00157        case ePUT:
00158               dav_put();
00159               break;
00160        
00161        /*
00162         * The DELETE method kills, maims, and destroys.
00163         */
00164        case eDELETE:
00165               dav_delete();
00166               break;
00167 
00168        /*
00169         * The REPORT method tells us that Mike Shaver is a self-righteous asshole.
00170         */
00171        case eREPORT:
00172               dav_report();
00173               break;
00174 
00175        default:
00176        /*
00177         * Couldn't find what we were looking for.  Die in a car fire.
00178         */
00179               hprintf("HTTP/1.1 501 Method not implemented\r\n");
00180               dav_common_headers();
00181               hprintf("Content-Type: text/plain\r\n");
00182               wc_printf("GroupDAV method \"%s\" is not implemented.\r\n",
00183                      ReqStrs[WCC->Hdr->HR.eReqType]);
00184               end_burst();
00185        }
00186 }
00187 
00188 
00189 /*
00190  * Output our host prefix for globally absolute URL's.
00191  */  
00192 void dav_identify_host(void) {
00193        wc_printf("%s", ChrPtr(site_prefix));
00194 }
00195 
00196 
00197 void tmplput_dav_HOSTNAME(StrBuf *Target, WCTemplputParams *TP) 
00198 {
00199        StrBufAppendPrintf(Target, "%s", ChrPtr(site_prefix));
00200 }
00201 
00202 /*
00203  * Output our host prefix for globally absolute URL's.
00204  */  
00205 void dav_identify_hosthdr(void) {
00206        hprintf("%s", ChrPtr(site_prefix));
00207 }
00208 
00209 
00210 void Header_HandleIfMatch(StrBuf *Line, ParsedHttpHdrs *hdr)
00211 {
00212        hdr->HR.dav_ifmatch = Line;
00213 }
00214        
00215 
00216 void Header_HandleDepth(StrBuf *Line, ParsedHttpHdrs *hdr)
00217 {
00218        if (!strcasecmp(ChrPtr(Line), "infinity")) {
00219               hdr->HR.dav_depth = 32767;
00220        }
00221        else if (strcmp(ChrPtr(Line), "0") == 0) {
00222               hdr->HR.dav_depth = 0;
00223        }
00224        else if (strcmp(ChrPtr(Line), "1") == 0) {
00225               hdr->HR.dav_depth = 1;
00226        }
00227 }
00228 
00229 
00230 int Conditional_DAV_DEPTH(StrBuf *Target, WCTemplputParams *TP)
00231 {
00232        return WC->Hdr->HR.dav_depth == GetTemplateTokenNumber(Target, TP, 2, 0);
00233 }
00234 
00235 
00236 void RegisterDAVNamespace(const char * UrlString, 
00237                        long UrlSLen, 
00238                        const char *DisplayName, 
00239                        long dslen, 
00240                        WebcitHandlerFunc F, 
00241                        WebcitRESTDispatchID RID,
00242                        long Flags)
00243 {
00244        void *vHandler;
00245 
00246        /* first put it in... */
00247        WebcitAddUrlHandler(UrlString, UrlSLen, DisplayName, dslen, F, Flags|PARSE_REST_URL);
00248        /* get it out again... */
00249        GetHash(HandlerHash, UrlString, UrlSLen, &vHandler);
00250        ((WebcitHandler*)vHandler)->RID = RID;
00251        /* and keep a copy of it, so we can compare it later */
00252        Put(DavNamespaces, UrlString, UrlSLen, vHandler, reference_free_handler);
00253 }
00254 
00255 
00256 int Conditional_DAV_NS(StrBuf *Target, WCTemplputParams *TP)
00257 {
00258        wcsession *WCC = WC;
00259        void *vHandler;
00260        const char *NS;
00261        long NSLen;
00262 
00263        GetTemplateTokenString(NULL, TP, 2, &NS, &NSLen);
00264        GetHash(HandlerHash, NS, NSLen, &vHandler);
00265        return WCC->Hdr->HR.Handler == vHandler;
00266 }
00267 
00268 
00269 int Conditional_DAV_NSCURRENT(StrBuf *Target, WCTemplputParams *TP)
00270 {
00271        wcsession *WCC = WC;
00272        void *vHandler;
00273 
00274        vHandler = CTX;
00275        return WCC->Hdr->HR.Handler == vHandler;
00276 }
00277 
00278 
00279 void tmplput_DAV_NAMESPACE(StrBuf *Target, WCTemplputParams *TP)
00280 {
00281        wcsession *WCC = WC;
00282 
00283        if (TP->Filter.ContextType == CTX_DAVNS) {
00284               WebcitHandler *H;
00285               H = (WebcitHandler*) CTX;
00286               StrBufAppendTemplate(Target, TP, H->Name, 0);
00287        }
00288        else if (WCC->Hdr->HR.Handler != NULL) {
00289               StrBufAppendTemplate(Target, TP, WCC->Hdr->HR.Handler->Name, 0);
00290        }
00291 }
00292 
00293 
00294 int GroupdavDispatchREST(RESTDispatchID WhichAction, int IgnoreFloor)
00295 {
00296        wcsession *WCC = WC;
00297        void *vDir;
00298        
00299        switch(WhichAction){
00300        case ExistsID:
00301               GetHash(WCC->Directory, IKEY(WCC->ThisRoom->nRoomNameParts + 1), &vDir);
00302               return locate_message_by_uid(ChrPtr((StrBuf*)vDir)) != -1;
00303               /* TODO: remember euid */
00304        case PutID:
00305        case DeleteID:
00306               break;
00307 
00308 
00309        }
00310        return 0;
00311 }
00312 
00313 
00314 void
00315 ServerStartModule_DAV
00316 (void)
00317 {
00318 
00319        DavNamespaces = NewHash(1, NULL);
00320 }
00321 
00322 
00323 void 
00324 ServerShutdownModule_DAV
00325 (void)
00326 {
00327        DeleteHash(&DavNamespaces);
00328 }
00329 
00330 
00331 void 
00332 InitModule_GROUPDAV
00333 (void)
00334 {
00335        RegisterDAVNamespace(HKEY("groupdav"), HKEY("GroupDAV"), 
00336                           dav_main, GroupdavDispatchREST, 
00337                           XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE
00338        );
00339 
00340        RegisterNamespace("DAV:HOSTNAME", 0, 0, tmplput_dav_HOSTNAME, NULL, CTX_NONE);
00341 
00342        RegisterConditional(HKEY("COND:DAV:NS"), 0, Conditional_DAV_NS,  CTX_NONE);
00343 
00344        RegisterIterator("DAV:NS", 0, DavNamespaces, NULL, 
00345                       NULL, NULL, CTX_DAVNS, CTX_NONE, IT_NOFLAG
00346        );
00347 
00348        RegisterConditional(HKEY("COND:DAV:NSCURRENT"), 0, Conditional_DAV_NSCURRENT,  CTX_DAVNS);
00349        RegisterNamespace("DAV:NAMESPACE", 0, 1, tmplput_DAV_NAMESPACE, NULL, CTX_NONE);
00350 
00351        RegisterHeaderHandler(HKEY("IF-MATCH"), Header_HandleIfMatch);
00352        RegisterHeaderHandler(HKEY("DEPTH"), Header_HandleDepth);
00353        RegisterConditional(HKEY("COND:DAV:DEPTH"), 1, Conditional_DAV_DEPTH,  CTX_NONE);
00354 }