8f16c0b24d4954786be1e62ea2827d41924f575d
[citadel.git] / webcit / dav_main.c
1 /*
2  * Entry point for GroupDAV functions
3  *
4  * Copyright (c) 2005-2012 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 3.
8  * 
9  * 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * 
17  * 
18  * 
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23 #include "dav.h"
24
25 extern HashList *HandlerHash;
26
27 HashList *DavNamespaces = NULL;
28
29 /*
30  * Output HTTP headers which are common to all requests.
31  *
32  * Please observe that we don't use the usual output_headers()
33  * and wDumpContent() functions in the GroupDAV subsystem, so we
34  * do our own header stuff here.
35  *
36  */
37 void dav_common_headers(void) {
38         hprintf(
39                 "Server: %s / %s\r\n"
40                 "Connection: close\r\n",
41                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
42         );
43 }
44
45
46
47 /*
48  * string conversion function
49  */
50 void euid_escapize(char *target, const char *source) {
51         int i, len;
52         int target_length = 0;
53
54         strcpy(target, "");
55         len = strlen(source);
56         for (i=0; i<len; ++i) {
57                 if ( (isalnum(source[i])) || (source[i]=='-') || (source[i]=='_') ) {
58                         target[target_length] = source[i];
59                         target[++target_length] = 0;
60                 }
61                 else {
62                         sprintf(&target[target_length], "=%02X", (0xFF & source[i]));
63                         target_length += 3;
64                 }
65         }
66 }
67
68 /*
69  * string conversion function
70  */
71 void euid_unescapize(char *target, const char *source) {
72         int a, b, len;
73         char hex[3];
74         int target_length = 0;
75
76         strcpy(target, "");
77
78         len = strlen(source);
79         for (a = 0; a < len; ++a) {
80                 if (source[a] == '=') {
81                         hex[0] = source[a + 1];
82                         hex[1] = source[a + 2];
83                         hex[2] = 0;
84                         b = 0;
85                         b = decode_hex(hex);
86                         target[target_length] = b;
87                         target[++target_length] = 0;
88                         a += 2;
89                 }
90                 else {
91                         target[target_length] = source[a];
92                         target[++target_length] = 0;
93                 }
94         }
95 }
96
97
98
99
100 /*
101  * Main entry point for GroupDAV requests
102  */
103 void dav_main(void)
104 {
105         wcsession *WCC = WC;
106         int i, len;
107
108         syslog(LOG_DEBUG, "dav_main() called, logged_in=%d", WCC->logged_in );
109
110         StrBufUnescape(WCC->Hdr->HR.ReqLine, 0);
111         StrBufStripSlashes(WCC->Hdr->HR.ReqLine, 0);
112
113         /*
114          * If there's an If-Match: header, strip out the quotes if present, and
115          * then if all that's left is an asterisk, make it go away entirely.
116          */
117         len = StrLength(WCC->Hdr->HR.dav_ifmatch);
118         if (len > 0) {
119                 StrBufTrim(WCC->Hdr->HR.dav_ifmatch);
120                 if (ChrPtr(WCC->Hdr->HR.dav_ifmatch)[0] == '\"') {
121                         StrBufCutLeft(WCC->Hdr->HR.dav_ifmatch, 1);
122                         len --;
123                         for (i=0; i<len; ++i) {
124                                 if (ChrPtr(WCC->Hdr->HR.dav_ifmatch)[i] == '\"') {
125                                         StrBufCutAt(WCC->Hdr->HR.dav_ifmatch, i, NULL);
126                                         len = StrLength(WCC->Hdr->HR.dav_ifmatch);
127                                 }
128                         }
129                 }
130                 if (!strcmp(ChrPtr(WCC->Hdr->HR.dav_ifmatch), "*")) {
131                         FlushStrBuf(WCC->Hdr->HR.dav_ifmatch);
132                 }
133         }
134
135         switch (WCC->Hdr->HR.eReqType)
136         {
137         /*
138          * The OPTIONS method is not required by GroupDAV but it will be
139          * needed for future implementations of other DAV-based protocols.
140          */
141         case eOPTIONS:
142                 dav_options();
143                 break;
144
145         /*
146          * The PROPFIND method is basically used to list all objects in a
147          * room, or to list all relevant rooms on the server.
148          */
149         case ePROPFIND:
150                 dav_propfind();
151                 break;
152
153         /*
154          * The GET method is used for fetching individual items.
155          */
156         case eGET:
157                 dav_get();
158                 break;
159         
160         /*
161          * The PUT method is used to add or modify items.
162          */
163         case ePUT:
164                 dav_put();
165                 break;
166         
167         /*
168          * The DELETE method kills, maims, and destroys.
169          */
170         case eDELETE:
171                 dav_delete();
172                 break;
173
174         /*
175          * The REPORT method tells us that Mike Shaver is a self-righteous asshole.
176          */
177         case eREPORT:
178                 dav_report();
179                 break;
180
181         default:
182         /*
183          * Couldn't find what we were looking for.  Die in a car fire.
184          */
185                 hprintf("HTTP/1.1 501 Method not implemented\r\n");
186                 dav_common_headers();
187                 hprintf("Content-Type: text/plain\r\n");
188                 wc_printf("GroupDAV method \"%s\" is not implemented.\r\n",
189                         ReqStrs[WCC->Hdr->HR.eReqType]);
190                 end_burst();
191         }
192 }
193
194
195 /*
196  * Output our host prefix for globally absolute URL's.
197  */  
198 void dav_identify_host(void) {
199         wc_printf("%s", ChrPtr(site_prefix));
200 }
201
202
203 void tmplput_dav_HOSTNAME(StrBuf *Target, WCTemplputParams *TP) 
204 {
205         StrBufAppendPrintf(Target, "%s", ChrPtr(site_prefix));
206 }
207
208 /*
209  * Output our host prefix for globally absolute URL's.
210  */  
211 void dav_identify_hosthdr(void) {
212         hprintf("%s", ChrPtr(site_prefix));
213 }
214
215
216 void Header_HandleIfMatch(StrBuf *Line, ParsedHttpHdrs *hdr)
217 {
218         hdr->HR.dav_ifmatch = Line;
219 }
220         
221
222 void Header_HandleDepth(StrBuf *Line, ParsedHttpHdrs *hdr)
223 {
224         if (!strcasecmp(ChrPtr(Line), "infinity")) {
225                 hdr->HR.dav_depth = 32767;
226         }
227         else if (strcmp(ChrPtr(Line), "0") == 0) {
228                 hdr->HR.dav_depth = 0;
229         }
230         else if (strcmp(ChrPtr(Line), "1") == 0) {
231                 hdr->HR.dav_depth = 1;
232         }
233 }
234
235
236 int Conditional_DAV_DEPTH(StrBuf *Target, WCTemplputParams *TP)
237 {
238         return WC->Hdr->HR.dav_depth == GetTemplateTokenNumber(Target, TP, 2, 0);
239 }
240
241
242 void RegisterDAVNamespace(const char * UrlString, 
243                           long UrlSLen, 
244                           const char *DisplayName, 
245                           long dslen, 
246                           WebcitHandlerFunc F, 
247                           WebcitRESTDispatchID RID,
248                           long Flags)
249 {
250         void *vHandler;
251
252         /* first put it in... */
253         WebcitAddUrlHandler(UrlString, UrlSLen, DisplayName, dslen, F, Flags|PARSE_REST_URL);
254         /* get it out again... */
255         GetHash(HandlerHash, UrlString, UrlSLen, &vHandler);
256         ((WebcitHandler*)vHandler)->RID = RID;
257         /* and keep a copy of it, so we can compare it later */
258         Put(DavNamespaces, UrlString, UrlSLen, vHandler, reference_free_handler);
259 }
260
261
262 int Conditional_DAV_NS(StrBuf *Target, WCTemplputParams *TP)
263 {
264         wcsession *WCC = WC;
265         void *vHandler;
266         const char *NS;
267         long NSLen;
268
269         GetTemplateTokenString(NULL, TP, 2, &NS, &NSLen);
270         GetHash(HandlerHash, NS, NSLen, &vHandler);
271         return WCC->Hdr->HR.Handler == vHandler;
272 }
273
274
275 int Conditional_DAV_NSCURRENT(StrBuf *Target, WCTemplputParams *TP)
276 {
277         wcsession *WCC = WC;
278         void *vHandler;
279
280         vHandler = CTX;
281         return WCC->Hdr->HR.Handler == vHandler;
282 }
283
284
285 void tmplput_DAV_NAMESPACE(StrBuf *Target, WCTemplputParams *TP)
286 {
287         wcsession *WCC = WC;
288
289         if (TP->Filter.ContextType == CTX_DAVNS) {
290                 WebcitHandler *H;
291                 H = (WebcitHandler*) CTX;
292                 StrBufAppendTemplate(Target, TP, H->Name, 0);
293         }
294         else if (WCC->Hdr->HR.Handler != NULL) {
295                 StrBufAppendTemplate(Target, TP, WCC->Hdr->HR.Handler->Name, 0);
296         }
297 }
298
299
300 int GroupdavDispatchREST(RESTDispatchID WhichAction, int IgnoreFloor)
301 {
302         wcsession *WCC = WC;
303         void *vDir;
304         
305         switch(WhichAction){
306         case ExistsID:
307                 GetHash(WCC->Directory, IKEY(WCC->ThisRoom->nRoomNameParts + 1), &vDir);
308                 return locate_message_by_uid(ChrPtr((StrBuf*)vDir)) != -1;
309                 /* TODO: remember euid */
310         case PutID:
311         case DeleteID:
312                 break;
313
314
315         }
316         return 0;
317 }
318
319
320 void
321 ServerStartModule_DAV
322 (void)
323 {
324
325         DavNamespaces = NewHash(1, NULL);
326 }
327
328
329 void 
330 ServerShutdownModule_DAV
331 (void)
332 {
333         DeleteHash(&DavNamespaces);
334 }
335
336
337 void 
338 InitModule_GROUPDAV
339 (void)
340 {
341         RegisterDAVNamespace(HKEY("groupdav"), HKEY("GroupDAV"), 
342                              dav_main, GroupdavDispatchREST, 
343                              XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE
344         );
345
346         RegisterNamespace("DAV:HOSTNAME", 0, 0, tmplput_dav_HOSTNAME, NULL, CTX_NONE);
347
348         RegisterConditional(HKEY("COND:DAV:NS"), 0, Conditional_DAV_NS,  CTX_NONE);
349
350         RegisterIterator("DAV:NS", 0, DavNamespaces, NULL, 
351                          NULL, NULL, CTX_DAVNS, CTX_NONE, IT_NOFLAG
352         );
353
354         RegisterConditional(HKEY("COND:DAV:NSCURRENT"), 0, Conditional_DAV_NSCURRENT,  CTX_DAVNS);
355         RegisterNamespace("DAV:NAMESPACE", 0, 1, tmplput_DAV_NAMESPACE, NULL, CTX_NONE);
356
357         RegisterHeaderHandler(HKEY("IF-MATCH"), Header_HandleIfMatch);
358         RegisterHeaderHandler(HKEY("DEPTH"), Header_HandleDepth);
359         RegisterConditional(HKEY("COND:DAV:DEPTH"), 1, Conditional_DAV_DEPTH,  CTX_NONE);
360 }