Improved logging of XML parse
[citadel.git] / webcit-ng / server / caldav_reports.c
1 // This file contains functions which handle all of the CalDAV "REPORT" queries specified in RFC4791 section 7.
2 // Copyright (c) 2023-2024 by the citadel.org team
3 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
4
5 #include "webcit.h"
6
7
8 // A CalDAV REPORT can only be one type.  This is stored in the report_type member.
9 enum cr_type {
10         cr_calendar_query,
11         cr_calendar_multiget,
12         cr_freebusy_query
13 };
14
15
16 // Data type for CalDAV Report Parameters.
17 // As we slog our way through the XML we learn what the client is asking for
18 // and build up the contents of this data type.
19 struct cr_parms {
20         int tag_nesting_level;          // not needed, just kept for pretty-printing
21         enum cr_type report_type;       // which RFC4791 section 7 REPORT are we generating
22         StrBuf *Chardata;               // XML chardata in between tags is built up here
23         StrBuf *Hrefs;                  // list of items requested by a `calendar-multiget` REPORT
24 };
25
26
27 // XML parser callback
28 void caldav_xml_start(void *data, const char *el, const char **attr) {
29         struct cr_parms *crp = (struct cr_parms *) data;
30
31 #ifdef DEBUG_XML_PARSE
32         // logging
33         int i;
34         char indent[256];
35         indent[0] = 0;
36         for (i=0; i<crp->tag_nesting_level; ++i) {
37                 strcat(indent, "  ");
38         }
39         syslog(LOG_DEBUG, "%s<%s>", indent, el);
40         for (i = 0; attr[i] != NULL; i += 2) {
41                 syslog(LOG_DEBUG, "%sAttribute '%s' = '%s'", indent, attr[i], attr[i + 1]);
42         }
43         // end logging
44 #endif
45
46         // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
47         if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-query")) {
48                 crp->report_type = cr_calendar_query;
49         }
50
51         // RFC4791 7.9 "calendar-multiget" REPORT - Client will supply a list of specific hrefs.
52         else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-multiget")) {
53                 crp->report_type = cr_calendar_multiget;
54         }
55
56         // RFC4791 7.10 "free-busy-query" REPORT
57         else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:free-busy-query")) {
58                 crp->report_type = cr_freebusy_query;
59         }
60
61         ++crp->tag_nesting_level;
62 }
63
64
65 // XML parser callback
66 void caldav_xml_end(void *data, const char *el) {
67         struct cr_parms *crp = (struct cr_parms *) data;
68
69         --crp->tag_nesting_level;
70
71 #ifdef DEBUG_XML_PARSE
72         // logging
73         int i;
74         char indent[256];
75         indent[0] = 0;
76         for (i=0; i<crp->tag_nesting_level; ++i) {
77                 strcat(indent, "  ");
78         }
79         if (crp->Chardata != NULL) {
80                 syslog(LOG_DEBUG, "%sCHARDATA: '%s'", indent, ChrPtr(crp->Chardata));
81         }
82         syslog(LOG_DEBUG, "%s</%s>", indent, el);
83         // end logging
84 #endif
85
86         if ((!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href"))) {
87                 if (crp->Hrefs == NULL) {       // append crp->Chardata to crp->Hrefs
88                         crp->Hrefs = NewStrBuf();
89                 }
90                 else {
91                         StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
92                 }
93                 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
94         }
95
96         if (crp->Chardata != NULL) {            // Tag is closed; chardata is now out of scope.
97                 FreeStrBuf(&crp->Chardata);     // Free the buffer.
98                 crp->Chardata = NULL;
99         }
100 }
101
102
103 // XML parser callback
104 void caldav_xml_chardata(void *data, const XML_Char * s, int len) {
105         struct cr_parms *crp = (struct cr_parms *) data;
106
107         if (crp->Chardata == NULL) {
108                 crp->Chardata = NewStrBuf();
109         }
110
111         StrBufAppendBufPlain(crp->Chardata, s, len, 0);
112
113         return;
114 }
115
116
117 // Called by caldav_report_one_item() to fetch a message (by number) in the current room,
118 // and return only the icalendar data as a StrBuf.  Returns NULL if not found.
119 //
120 // NOTE: this function expects that "MSGP text/calendar" was issued at the beginning
121 // of a REPORT operation to set our preferred MIME type to calendar data.
122 StrBuf *fetch_ical(struct ctdlsession *c, long msgnum) {
123         char buf[1024];
124         StrBuf *Buf = NULL;
125
126         ctdl_printf(c, "MSG4 %ld", msgnum);
127         ctdl_readline(c, buf, sizeof(buf));
128         if (buf[0] != '1') {
129                 return NULL;
130         }
131
132         while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
133                 if (Buf != NULL) {              // already in body
134                         StrBufAppendPrintf(Buf, "%s\n", buf);
135                 }
136                 else if (IsEmptyStr(buf)) {     // beginning of body
137                         Buf = NewStrBuf();
138                 }
139         }
140
141         return Buf;
142 }
143
144
145 // Called by multiple REPORT types to actually perform the output in "multiget" format.
146 // We need to already know the source message number and the href, but also already have the output data.
147 void cal_multiget_out(long msgnum, StrBuf *ThisHref, StrBuf *Caldata, StrBuf *ReportOut) {
148
149         StrBufAppendPrintf(ReportOut, "<D:response>");
150         StrBufAppendPrintf(ReportOut, "<D:href>");
151         StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0);
152         StrBufAppendPrintf(ReportOut, "</D:href>");
153         StrBufAppendPrintf(ReportOut, "<D:propstat>");
154
155         if (Caldata != NULL) {
156                 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 200 OK", ChrPtr(ThisHref));
157                 StrBufAppendPrintf(ReportOut, "<D:status>");
158                 StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK");
159                 StrBufAppendPrintf(ReportOut, "</D:status>");
160                 StrBufAppendPrintf(ReportOut, "<D:prop>");
161                 StrBufAppendPrintf(ReportOut, "<D:getetag>");
162                 StrBufAppendPrintf(ReportOut, "%ld", msgnum);
163                 StrBufAppendPrintf(ReportOut, "</D:getetag>");
164                 StrBufAppendPrintf(ReportOut, "<C:calendar-data>");
165                 StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0);
166                 StrBufAppendPrintf(ReportOut, "</C:calendar-data>");
167                 StrBufAppendPrintf(ReportOut, "</D:prop>");
168         }
169         else {
170                 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 404 not found", ChrPtr(ThisHref));
171                 StrBufAppendPrintf(ReportOut, "<D:status>");
172                 StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found");
173                 StrBufAppendPrintf(ReportOut, "</D:status>");
174         }
175
176         StrBufAppendPrintf(ReportOut, "</D:propstat>");
177         StrBufAppendPrintf(ReportOut, "</D:response>");
178 }
179
180
181 // Called by caldav_report() to output a single item.
182 // Our policy is to throw away the list of properties the client asked for, and just send everything.
183 void caldav_report_one_item(struct http_transaction *h, struct ctdlsession *c, StrBuf *ReportOut, StrBuf *ThisHref) {
184         long msgnum;
185         StrBuf *Caldata = NULL;
186         char *euid;
187
188         euid = strrchr(ChrPtr(ThisHref), '/');
189         if (euid != NULL) {
190                 ++euid;
191         }
192         else {
193                 euid = (char *) ChrPtr(ThisHref);
194         }
195
196         char *unescaped_euid = strdup(euid);
197         if (!unescaped_euid) {
198                 return;
199         }
200         unescape_input(unescaped_euid);
201
202         msgnum = locate_message_by_uid(c, unescaped_euid);
203         free(unescaped_euid);
204         if (msgnum > 0) {
205                 Caldata = fetch_ical(c, msgnum);
206         }
207         else {
208                 Caldata = NULL;
209         }
210
211         cal_multiget_out(msgnum, ThisHref, Caldata, ReportOut);
212
213         if (Caldata != NULL) {
214                 FreeStrBuf(&Caldata);
215         }
216 }
217
218
219 // Called by report_the_room_itself() in room_functions.c when a CalDAV REPORT method
220 // is requested on a calendar room.  We fire up an XML Parser to decode the request and
221 // hopefully produce the correct output.
222 void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
223         struct cr_parms crp;
224         char buf[1024];
225
226         memset(&crp, 0, sizeof(struct cr_parms));
227
228         XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
229         if (xp == NULL) {
230                 syslog(LOG_INFO, "Cannot create XML parser!");
231                 do_404(h);
232                 return;
233         }
234
235         XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end);
236         XML_SetCharacterDataHandler(xp, caldav_xml_chardata);
237         XML_SetUserData(xp, &crp);
238         XML_SetDefaultHandler(xp, NULL);        // Disable internal entity expansion to prevent "billion laughs attack"
239         XML_Parse(xp, h->request_body, h->request_body_length, 1);
240         XML_ParserFree(xp);
241
242         if (crp.Chardata != NULL) {             // Discard any trailing chardata ... normally nothing here
243                 FreeStrBuf(&crp.Chardata);
244                 crp.Chardata = NULL;
245         }
246
247         // We're going to make a lot of MSG4 calls, and the preferred MIME type we want is "text/calendar".
248         // The iCalendar standard is mature now, and we are no longer interested in text/x-vcal or application/ics.
249         ctdl_printf(c, "MSGP text/calendar");
250         ctdl_readline(c, buf, sizeof buf);
251
252         // Now begin the REPORT.
253         syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type);
254         StrBuf *ReportOut = NewStrBuf();
255         StrBufAppendPrintf(ReportOut,
256                 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
257                 "<D:multistatus "
258                 "xmlns:D=\"DAV:\" "
259                 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
260                 ">"
261         );
262
263         // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
264         if (crp.report_type == cr_calendar_query) {
265                 int i = 0;
266                 Array *msglist = get_msglist(c, "ALL");
267                 if (msglist != NULL) {
268                         for (i = 0; i < array_len(msglist); ++i) {
269                                 long m;
270                                 memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
271
272                                 // load and parse one calendar item
273                                 StrBuf *one_item = fetch_ical(c, m);
274                                 icalcomponent *cal = icalcomponent_new_from_string(ChrPtr(one_item));
275
276                                 // qualify will be set to nonzero if this calendar item is a match for the QUERY.
277                                 int qualify = 0;
278
279                                 // this is a horrible temporary hack to output every item (for now)
280                                 qualify = 1;
281
282                                 // Did this calendar item match the query?  If so, output it.
283                                 if (qualify) {
284                                         // FIXME need to populate the Href instead of NULL
285                                         cal_multiget_out(m, NULL, one_item, ReportOut);
286                                 }
287
288                                 icalcomponent_free(cal);
289                                 FreeStrBuf(&one_item);
290
291                         }
292                         array_free(msglist);
293                 }
294         }
295
296         // RFC4791 7.9 "calendar-multiget" REPORT - go get the specific Hrefs the client asked for.
297         // Can we move this back into citserver too?
298         else if ( (crp.report_type == cr_calendar_multiget) && (crp.Hrefs != NULL) ) {
299
300                 StrBuf *ThisHref = NewStrBuf();
301                 const char *pvset = NULL;
302                 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
303                         StrBufTrim(ThisHref);                           // remove leading/trailing whitespace from the href
304                         caldav_report_one_item(h, c, ReportOut, ThisHref);
305                 }
306                 FreeStrBuf(&ThisHref);
307         }
308
309         // RFC4791 7.10 "free-busy-query" REPORT
310         else if (crp.report_type == cr_freebusy_query) {
311                 // FIXME build this REPORT.  At the moment we send an empty multistatus.
312         }
313
314         // Free any query parameters that might have been allocated during the xml parse
315         if (crp.Hrefs != NULL) {
316                 FreeStrBuf(&crp.Hrefs);
317                 crp.Hrefs = NULL;
318         }
319
320         StrBufAppendPrintf(ReportOut, "</D:multistatus>\n");            // End the REPORT.
321
322         add_response_header(h, strdup("Content-type"), strdup("text/xml"));
323         h->response_code = 207;
324         h->response_string = strdup("Multi-Status");
325         h->response_body_length = StrLength(ReportOut);
326         h->response_body = SmashStrBuf(&ReportOut);
327 }