2 * Handles GroupDAV and CalDAV PROPFIND requests.
4 * A few notes about our XML output:
6 * --> Yes, we are spewing tags directly instead of using an XML library.
7 * Whining about it will be summarily ignored.
9 * --> XML is deliberately output with no whitespace/newlines between tags.
10 * This makes it difficult to read, but we have discovered clients which
11 * crash when you try to pretty it up.
14 * http://www.ietf.org/rfc/rfc4791.txt
15 * http://blogs.nologin.es/rickyepoderi/index.php?/archives/14-Introducing-CalDAV-Part-I.html
16 * https://msdn.microsoft.com/en-us/library/aa142960(v=exchg.65).aspx
18 * Copyright (c) 2005-2017 by the citadel.org team
20 * This program is open source software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License version 3.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
30 #include "webserver.h"
34 * Given an encoded UID, translate that to an unencoded Citadel EUID and
35 * then search for it in the current room. Return a message number or -1
39 long locate_message_by_uid(const char *uid) {
41 char decoded_uid[1024];
45 euid_unescapize(decoded_uid, uid);
47 /* ask Citadel if we have this one */
48 serv_printf("EUID %s", decoded_uid);
49 serv_getln(buf, sizeof buf);
51 retval = atol(&buf[4]);
59 * IgnoreFloor: set to 0 or 1 _nothing else_
60 * Subfolders: direct child floors will be put here.
62 const folder *GetRESTFolder(int IgnoreFloor, HashList *Subfolders)
66 const folder *ThisFolder = NULL;
67 const folder *FoundFolder = NULL;
68 const folder *BestGuess = NULL;
75 int iRoom, jURL, urlp;
79 * Guess room: if the full URL matches a room, list thats it. We also need to remember direct sub rooms.
80 * if the URL is longer, we need to find the "best guess" so we can find the room we're in, and the rest
81 * of the URL will be uids and so on.
83 itfl = GetNewHashPos(WCC->Floors, 0);
84 urlp = GetCount(WCC->Directory);
86 while (GetNextHashPos(WCC->Floors, itfl, &len, &Key, &vFolder) &&
90 if (!IgnoreFloor && /* so we can handle legacy URLS... */
91 (ThisFolder->Floor != WCC->CurrentFloor))
94 if (ThisFolder->nRoomNameParts > 1)
96 /*TODO: is that number all right? */
97 // if (urlp - ThisFolder->nRoomNameParts != 2) {
98 // if (BestGuess != NULL)
101 // itd = GetNewHashPos(WCC->Directory, 0);
102 // GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
104 itd = GetNewHashPos(WCC->Directory, 0);
105 GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
107 for (iRoom = 0, /* Fast forward the floorname as we checked it above: */ jURL = IgnoreFloor;
109 (iRoom <= ThisFolder->nRoomNameParts) && (jURL <= urlp);
111 iRoom++, jURL++, GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir))
114 if (strcmp(ChrPtr(ThisFolder->RoomNameParts[iRoom]),
123 if ((iRoom == ThisFolder->nRoomNameParts) && (jURL == urlp))
125 FoundFolder = ThisFolder;
127 /* URL got more parts then this room, so we remember it for the best guess*/
128 else if ((jURL <= urlp) &&
129 (ThisFolder->nRoomNameParts <= nBestGuess))
131 BestGuess = ThisFolder;
132 nBestGuess = jURL - 1;
134 /* Room has more parts than the URL, it might be a sub-room? */
135 else if (iRoom <ThisFolder->nRoomNameParts)
136 {//// TODO: ThisFolder->nRoomNameParts == urlp - IgnoreFloor???
137 Put(Subfolders, SKEY(ThisFolder->name),
138 /* Cast away const, its a reference. */
139 (void*)ThisFolder, reference_free_handler);
143 delta = GetCount(WCC->Directory) - ThisFolder->nRoomNameParts;
144 if ((delta != 2) && (nBestGuess > 1))
147 itd = GetNewHashPos(WCC->Directory, 0);
149 if (!GetNextHashPos(WCC->Directory,
150 itd, &len, &Key, &vDir) ||
155 syslog(LOG_DEBUG, "5\n");
159 Dir = (StrBuf*) vDir;
160 if (strcmp(ChrPtr(ThisFolder->name),
166 syslog(LOG_DEBUG, "5\n");
169 DeleteHashPos(&itfl);
173 BestGuess = ThisFolder;
176 FoundFolder = ThisFolder;
180 /* TODO: Subfolders: remove patterns not matching the best guess or thisfolder */
181 DeleteHashPos(&itfl);
182 if (FoundFolder != NULL)
191 long GotoRestRoom(HashList *SubRooms)
193 int IgnoreFloor = 0; /* deprecated... */
197 const folder *ThisFolder;
199 State = REST_TOPLEVEL;
201 if (WCC->Hdr->HR.Handler != NULL)
202 State |= REST_IN_NAMESPACE;
204 Count = GetCount(WCC->Directory);
206 if (Count == 0) return State;
208 if (Count >= 1) State |=REST_IN_FLOOR;
209 if (Count == 1) return State;
212 * More than 3 params and no floor found?
213 * -> fall back to old non-floored notation
215 if ((Count >= 3) && (WCC->CurrentFloor == NULL))
220 State |= REST_IN_FLOOR;
222 ThisFolder = GetRESTFolder(IgnoreFloor, SubRooms);
223 if (ThisFolder != NULL)
225 if (WCC->ThisRoom != NULL)
226 if (CompareRooms(WCC->ThisRoom, ThisFolder) != 0)
227 gotoroom(ThisFolder->name);
228 State |= REST_IN_ROOM;
231 if (GetCount(SubRooms) > 0)
232 State |= REST_HAVE_SUB_ROOMS;
234 if ((WCC->ThisRoom != NULL) &&
235 (Count + IgnoreFloor > 3))
237 if (WCC->Hdr->HR.Handler->RID(ExistsID, IgnoreFloor))
239 State |= REST_GOT_LOCAL_PART;
242 /// WHOOPS, not there???
243 State |= REST_NONEXIST;
254 * List rooms (or "collections" in DAV terminology) which contain
255 * interesting groupware objects.
257 void dav_collection_list(void)
263 char datestring[256];
266 int is_groupware_collection = 0;
267 int starting_point = 1; /**< 0 for /, 1 for /groupdav/ */
269 if (WCC->Hdr->HR.Handler == NULL) {
272 else if (StrLength(WCC->Hdr->HR.ReqLine) == 0) {
280 http_datestring(datestring, sizeof datestring, now);
283 * Be rude. Completely ignore the XML request and simply send them
284 * everything we know about. Let the client sort it out.
286 hprintf("HTTP/1.0 207 Multi-Status\r\n");
287 dav_common_headers();
288 hprintf("Date: %s\r\n", datestring);
289 hprintf("Content-type: text/xml\r\n");
290 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))
291 hprintf("Content-encoding: identity\r\n");
295 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
296 "<multistatus xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
300 * If the client is requesting the root, show a root node.
302 if (starting_point == 0) {
303 wc_printf("<response>");
307 wc_printf("</href>");
308 wc_printf("<propstat>");
309 wc_printf("<status>HTTP/1.1 200 OK</status>");
311 wc_printf("<displayname>/</displayname>");
312 wc_printf("<resourcetype><collection/></resourcetype>");
313 wc_printf("<getlastmodified>");
315 wc_printf("</getlastmodified>");
316 wc_printf("</prop>");
317 wc_printf("</propstat>");
318 wc_printf("</response>");
322 * If the client is requesting "/groupdav", show a /groupdav subdirectory.
324 if ((starting_point + WCC->Hdr->HR.dav_depth) >= 1) {
325 wc_printf("<response>");
328 wc_printf("/groupdav");
329 wc_printf("</href>");
330 wc_printf("<propstat>");
331 wc_printf("<status>HTTP/1.1 200 OK</status>");
333 wc_printf("<displayname>GroupDAV</displayname>");
334 wc_printf("<resourcetype><collection/></resourcetype>");
335 wc_printf("<getlastmodified>");
337 wc_printf("</getlastmodified>");
338 wc_printf("</prop>");
339 wc_printf("</propstat>");
340 wc_printf("</response>");
344 * Now go through the list and make it look like a DAV collection
347 serv_getln(buf, sizeof buf);
348 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
350 extract_token(roomname, buf, 0, '|', sizeof roomname);
351 view = extract_int(buf, 7);
352 mtime = extract_long(buf, 8);
353 http_datestring(datestring, sizeof datestring, mtime);
356 * For now, only list rooms that we know a GroupDAV client
357 * might be interested in. In the future we may add
360 * We determine the type of objects which are stored in each
361 * room by looking at the *default* view for the room. This
362 * allows, for example, a Calendar room to appear as a
363 * GroupDAV calendar even if the user has switched it to a
364 * Calendar List view.
366 if ( (view == VIEW_CALENDAR) ||
367 (view == VIEW_TASKS) ||
368 (view == VIEW_ADDRESSBOOK) ||
369 (view == VIEW_NOTES) ||
370 (view == VIEW_JOURNAL) ||
371 (view == VIEW_WIKI) ||
372 (view == VIEW_WIKIMD)
374 is_groupware_collection = 1;
377 is_groupware_collection = 0;
380 if ( (is_groupware_collection) && ((starting_point + WCC->Hdr->HR.dav_depth) >= 2) ) {
381 wc_printf("<response>");
385 wc_printf("/groupdav/");
386 urlescputs(roomname);
387 wc_printf("/</href>");
389 wc_printf("<propstat>");
390 wc_printf("<status>HTTP/1.1 200 OK</status>");
392 wc_printf("<displayname>");
394 wc_printf("</displayname>");
395 wc_printf("<resourcetype><collection/>");
399 wc_printf("<G:vevent-collection />");
402 wc_printf("<G:vtodo-collection />");
404 case VIEW_ADDRESSBOOK:
405 wc_printf("<G:vcard-collection />");
408 wc_printf("<G:vnotes-collection />");
411 wc_printf("<G:vjournal-collection />");
415 wc_printf("<G:wiki-collection />");
419 wc_printf("</resourcetype>");
420 wc_printf("<getlastmodified>");
422 wc_printf("</getlastmodified>");
423 wc_printf("</prop>");
424 wc_printf("</propstat>");
425 wc_printf("</response>");
428 wc_printf("</multistatus>\n");
434 void propfind_xml_start(void *data, const char *supplied_el, const char **attr) {
435 // syslog(LOG_DEBUG, "<%s>", supplied_el);
438 void propfind_xml_end(void *data, const char *supplied_el) {
439 // syslog(LOG_DEBUG, "</%s>", supplied_el);
445 * The pathname is always going to be /groupdav/room_name/msg_num
447 void dav_propfind(void)
450 StrBuf *dav_roomname;
454 long dav_msgnum = (-1);
456 char encoded_uid[256];
460 char datestring[256];
464 http_datestring(datestring, sizeof datestring, now);
466 int parse_success = 0;
467 XML_Parser xp = XML_ParserCreateNS(NULL, '|');
469 // XML_SetUserData(xp, XXX);
470 XML_SetElementHandler(xp, propfind_xml_start, propfind_xml_end);
471 // XML_SetCharacterDataHandler(xp, xrds_xml_chardata);
473 const char *req = ChrPtr(WCC->upload);
475 req = strchr(req, '<'); /* hunt for the first tag */
478 req = "ERROR"; /* force it to barf */
481 i = XML_Parse(xp, req, strlen(req), 1);
483 syslog(LOG_DEBUG, "XML_Parse() failed: %s", XML_ErrorString(XML_GetErrorCode(xp)));
492 if (!parse_success) {
493 hprintf("HTTP/1.1 500 Internal Server Error\r\n");
494 dav_common_headers();
495 hprintf("Date: %s\r\n", datestring);
496 hprintf("Content-Type: text/plain\r\n");
497 wc_printf("An internal error has occurred at %s:%d.\r\n", __FILE__ , __LINE__ );
502 dav_roomname = NewStrBuf();
503 dav_uid = NewStrBuf();
504 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
505 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
507 syslog(LOG_DEBUG, "PROPFIND requested for '%s' at depth %d",
508 ChrPtr(dav_roomname), WCC->Hdr->HR.dav_depth
512 * If the room name is blank, the client is requesting a folder list.
514 if (StrLength(dav_roomname) == 0) {
515 dav_collection_list();
516 FreeStrBuf(&dav_roomname);
517 FreeStrBuf(&dav_uid);
521 /* Go to the correct room. */
522 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
523 gotoroom(dav_roomname);
525 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
526 hprintf("HTTP/1.1 404 not found\r\n");
527 dav_common_headers();
528 hprintf("Date: %s\r\n", datestring);
529 hprintf("Content-Type: text/plain\r\n");
530 wc_printf("There is no folder called \"%s\" on this server.\r\n", ChrPtr(dav_roomname));
532 FreeStrBuf(&dav_roomname);
533 FreeStrBuf(&dav_uid);
537 /* If dav_uid is non-empty, client is requesting a PROPFIND on
538 * a specific item in the room. This is not valid GroupDAV, but
539 * it is valid WebDAV (and probably CalDAV too).
541 if (StrLength(dav_uid) != 0) {
543 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
544 if (dav_msgnum < 0) {
545 hprintf("HTTP/1.1 404 not found\r\n");
546 dav_common_headers();
547 hprintf("Content-Type: text/plain\r\n");
548 wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
553 FreeStrBuf(&dav_roomname);
554 FreeStrBuf(&dav_uid);
558 /* Be rude. Completely ignore the XML request and simply send them
559 * everything we know about (which is going to simply be the ETag and
560 * nothing else). Let the client-side parser sort it out.
562 hprintf("HTTP/1.0 207 Multi-Status\r\n");
563 dav_common_headers();
564 hprintf("Date: %s\r\n", datestring);
565 hprintf("Content-type: text/xml\r\n");
566 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))
567 hprintf("Content-encoding: identity\r\n");
571 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
572 "<multistatus xmlns=\"DAV:\">"
575 wc_printf("<response>");
579 wc_printf("/groupdav/");
580 urlescputs(ChrPtr(WCC->CurRoom.name));
581 euid_escapize(encoded_uid, ChrPtr(dav_uid));
582 wc_printf("/%s", encoded_uid);
583 wc_printf("</href>");
584 wc_printf("<propstat>");
585 wc_printf("<status>HTTP/1.1 200 OK</status>");
587 wc_printf("<getetag>\"%ld\"</getetag>", dav_msgnum);
588 wc_printf("<getlastmodified>");
590 wc_printf("</getlastmodified>");
591 wc_printf("</prop>");
592 wc_printf("</propstat>");
594 wc_printf("</response>\n");
595 wc_printf("</multistatus>\n");
597 FreeStrBuf(&dav_roomname);
598 FreeStrBuf(&dav_uid);
601 FreeStrBuf(&dav_roomname);
602 FreeStrBuf(&dav_uid);
606 * If we get to this point the client is performing a PROPFIND on the room itself.
608 * We call it a room; DAV calls it a "collection." We have to give it some properties
609 * of the room itself and then offer a list of all items contained therein.
611 * Be rude. Completely ignore the XML request and simply send them
612 * everything we know about (which is going to simply be the ETag and
613 * nothing else). Let the client-side parser sort it out.
615 //syslog(LOG_DEBUG, "BE RUDE AND IGNORE: \033[31m%s\033[0m", ChrPtr(WC->upload) );
616 hprintf("HTTP/1.0 207 Multi-Status\r\n");
617 dav_common_headers();
618 hprintf("Date: %s\r\n", datestring);
619 hprintf("Content-type: text/xml\r\n");
620 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok)) {
621 hprintf("Content-encoding: identity\r\n");
625 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
628 "xmlns:G=\"http://groupdav.org/\" "
629 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
633 /* Transmit the collection resource */
634 wc_printf("<D:response>");
636 wc_printf("<D:href>");
638 wc_printf("/groupdav/");
639 urlescputs(ChrPtr(WCC->CurRoom.name));
640 wc_printf("</D:href>");
642 wc_printf("<D:propstat>");
643 wc_printf("<D:status>HTTP/1.1 200 OK</D:status>");
644 wc_printf("<D:prop>");
645 wc_printf("<D:displayname>");
646 escputs(ChrPtr(WCC->CurRoom.name));
647 wc_printf("</D:displayname>");
649 wc_printf("<D:owner/>"); /* empty owner ought to be legal; see rfc3744 section 5.1 */
651 wc_printf("<D:resourcetype><D:collection/>");
652 switch(WCC->CurRoom.defview) {
654 wc_printf("<G:vevent-collection />");
655 wc_printf("<C:calendar />");
658 wc_printf("<G:vtodo-collection />");
660 case VIEW_ADDRESSBOOK:
661 wc_printf("<G:vcard-collection />");
664 wc_printf("</D:resourcetype>");
666 /* FIXME get the mtime
667 wc_printf("<D:getlastmodified>");
669 wc_printf("</D:getlastmodified>");
671 wc_printf("</D:prop>");
672 wc_printf("</D:propstat>");
673 wc_printf("</D:response>");
675 /* If a depth greater than zero was specified, transmit the collection listing */
677 if (WCC->Hdr->HR.dav_depth > 0) {
678 MsgNum = NewStrBuf();
679 serv_puts("MSGS ALL");
681 StrBuf_ServGetln(MsgNum);
682 if (GetServerStatus(MsgNum, NULL) == 1)
683 while (BufLen = StrBuf_ServGetln(MsgNum),
685 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
687 msgs = realloc(msgs, ++num_msgs * sizeof(long));
688 msgs[num_msgs-1] = StrTol(MsgNum);
691 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
693 syslog(LOG_DEBUG, "PROPFIND enumerating message # %ld", msgs[i]);
696 serv_printf("MSG0 %ld|3", msgs[i]);
697 StrBuf_ServGetln(MsgNum);
698 if (GetServerStatus(MsgNum, NULL) == 1)
699 while (BufLen = StrBuf_ServGetln(MsgNum),
701 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
703 if (!strncasecmp(ChrPtr(MsgNum), "exti=", 5)) {
704 strcpy(uid, &ChrPtr(MsgNum)[5]);
706 else if (!strncasecmp(ChrPtr(MsgNum), "time=", 5)) {
707 now = atol(&ChrPtr(MsgNum)[5]);
711 if (!IsEmptyStr(uid)) {
712 wc_printf("<D:response>");
713 wc_printf("<D:href>");
715 wc_printf("/groupdav/");
716 urlescputs(ChrPtr(WCC->CurRoom.name));
717 euid_escapize(encoded_uid, uid);
718 wc_printf("/%s", encoded_uid);
719 wc_printf("</D:href>");
720 wc_printf("<D:propstat>");
721 wc_printf("<D:status>HTTP/1.1 200 OK</D:status>");
722 wc_printf("<D:prop>");
723 wc_printf("<D:getetag>\"%ld\"</D:getetag>", msgs[i]);
724 switch(WCC->CurRoom.defview) {
726 wc_printf("<D:getcontenttype>text/x-ical</D:getcontenttype>");
729 wc_printf("<D:getcontenttype>text/x-ical</D:getcontenttype>");
731 case VIEW_ADDRESSBOOK:
732 wc_printf("<D:getcontenttype>text/x-vcard</D:getcontenttype>");
736 http_datestring(datestring, sizeof datestring, now);
737 wc_printf("<D:getlastmodified>");
739 wc_printf("</D:getlastmodified>");
741 wc_printf("</D:prop>");
742 wc_printf("</D:propstat>");
743 wc_printf("</D:response>");
749 wc_printf("</D:multistatus>\n");
759 int ParseMessageListHeaders_EUID(StrBuf *Line,
761 message_summary *Msg,
762 StrBuf *ConversionBuffer,
765 Msg->euid = NewStrBuf();
766 StrBufExtract_NextToken(Msg->euid, Line, pos, '|');
767 Msg->date = StrBufExtractNext_long(Line, pos, '|');
769 return StrLength(Msg->euid) > 0;
772 int DavUIDL_GetParamsGetServerCall(SharedMessageStatus *Stat,
780 Stat->defaultsortorder = 0;
783 Stat->maxmsgs = 9999999;
785 snprintf(cmd, len, "MSGS ALL|||2");
789 int DavUIDL_RenderView_or_Tail(SharedMessageStatus *Stat,
794 DoTemplate(HKEY("msg_listview"),NULL,&NoCtx);
799 int DavUIDL_Cleanup(void **ViewSpecific)
801 /* Note: wDumpContent() will output one additional </div> tag. */
802 /* We ought to move this out into template */
815 RegisterReadLoopHandlerset(
817 DavUIDL_GetParamsGetServerCall,
820 ParseMessageListHeaders_EUID,
822 DavUIDL_RenderView_or_Tail,