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