-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathproppatch.c
230 lines (201 loc) · 5.87 KB
/
proppatch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (c) Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <kcgi.h>
#include <kcgixml.h>
#include "libkcaldav.h"
#include "db.h"
#include "server.h"
/*
* This converts the request into a CalDav object.
* We know that the request is a well-formed CalDav object because it
* appears in the fieldmap and we parsed it during HTTP unpacking.
* So we really only check its media type.
*/
static struct caldav *
req2caldav(struct kreq *r, enum kmime *mime)
{
struct state *st = r->arg;
if (r->fieldmap[VALID_BODY] == NULL) {
kutil_warnx(r, st->prncpl->name,
"failed CalDAV parse");
http_error(r, KHTTP_400);
return NULL;
}
if (r->fieldmap[VALID_BODY]->ctypepos != KMIME_TEXT_XML &&
r->fieldmap[VALID_BODY]->ctypepos != KMIME_APP_XML) {
kutil_warnx(r, st->prncpl->name,
"bad CalDAV MIME type");
http_error(r, KHTTP_415);
return NULL;
}
*mime = r->fieldmap[VALID_BODY]->ctypepos;
/* This shouldn't fail now. */
return caldav_parse
(r->fieldmap[VALID_BODY]->val,
r->fieldmap[VALID_BODY]->valsz, NULL);
}
/*
* Handle the PROPPATCH method, which is in its most general way
* described in RFC 4918, section 9.2, accepting the "propertyupdate"
* CalDAV XML (ibid., 14.9).
*/
void
method_proppatch(struct kreq *r)
{
struct caldav *dav;
struct state *st = r->arg;
struct kxmlreq xml;
size_t nf, df, bf, i;
int accepted[CALPROP__MAX + 1];
struct coln cfg;
enum kmime mime;
if (st->cfg == NULL) {
kutil_warnx(r, st->prncpl->name,
"PROPPATCH of non-calendar collection");
http_error(r, KHTTP_403);
return;
} else if ((dav = req2caldav(r, &mime)) == NULL)
return;
cfg = *st->cfg;
memset(accepted, 0, sizeof(accepted));
accepted[CALPROP_CALENDAR_COLOR] = 1;
accepted[CALPROP_CALENDAR_DESCRIPTION] = 1;
accepted[CALPROP_DISPLAYNAME] = 1;
if (CALREQTYPE_PROPERTYUPDATE != dav->type) {
kutil_warnx(r, st->prncpl->name,
"unknown PROPPATCH request type");
http_error(r, KHTTP_415);
caldav_free(dav);
return;
}
khttp_head(r, kresps[KRESP_STATUS],
"%s", khttps[KHTTP_207]);
khttp_head(r, kresps[KRESP_CONTENT_TYPE],
"%s", kmimetypes[mime]);
khttp_head(r, "DAV", "1, access-control, "
"calendar-access, calendar-proxy");
khttp_body(r);
kxml_open(&xml, r, xmls, XML__MAX);
kxml_prologue(&xml);
kxml_pushattrs(&xml, XML_DAV_MULTISTATUS,
"xmlns:B", "http://calendarserver.org/ns/",
"xmlns:C", "urn:ietf:params:xml:ns:caldav",
"xmlns:D", "DAV:", NULL);
/*
* Begin by looking over all of our properties and setting.
* If we don't understand a property, skip it.
* If we understand it but the value wasn't valid, skip it.
* Otherwise, set into a temporary "struct config".
*/
kxml_push(&xml, XML_DAV_PROPSTAT);
kxml_push(&xml, XML_DAV_PROP);
for (nf = df = bf = i = 0; i < dav->propsz; i++) {
if (!accepted[dav->props[i].key]) {
nf++;
bf += dav->props[i].valid < 0;
continue;
} else if (dav->props[i].valid < 0) {
bf++;
continue;
}
df++;
switch (dav->props[i].key) {
case CALPROP_DISPLAYNAME:
cfg.displayname = dav->props[i].val;
kutil_dbg(r, st->prncpl->name,
"display name modified");
break;
case CALPROP_CALENDAR_COLOR:
cfg.colour = dav->props[i].val;
kutil_dbg(r, st->prncpl->name,
"calendar colour modified");
break;
case CALPROP_CALENDAR_DESCRIPTION:
cfg.description = dav->props[i].val;
kutil_dbg(r, st->prncpl->name,
"calendar description modified");
break;
default:
abort();
}
}
kxml_pop(&xml);
kxml_push(&xml, XML_DAV_STATUS);
kxml_puts(&xml, "HTTP/1.1 ");
kxml_puts(&xml, khttps[KHTTP_200]);
kxml_pop(&xml);
kxml_pop(&xml);
/*
* In this event, we didn't understand one or more properties.
* Serialise these as error 404 according to the spec.
*/
if (nf > 0) {
kxml_push(&xml, XML_DAV_PROPSTAT);
kxml_push(&xml, XML_DAV_PROP);
for (i = 0; i < dav->propsz; i++) {
if (accepted[dav->props[i].key])
continue;
khttp_puts(r, "<X:");
khttp_puts(r, dav->props[i].name);
khttp_puts(r, " xmlns:X=\"");
khttp_puts(r, dav->props[i].xmlns);
khttp_puts(r, "\" />");
}
kxml_pop(&xml);
kxml_push(&xml, XML_DAV_STATUS);
kxml_puts(&xml, "HTTP/1.1 ");
kxml_puts(&xml, khttps[KHTTP_404]);
kxml_pop(&xml);
kxml_pop(&xml);
}
/*
* Bad things: we're asked to set invalid data.
* Use code 409 as specified by RFC 4918, 9.2.1.
*/
if (bf > 0) {
kxml_push(&xml, XML_DAV_PROPSTAT);
kxml_push(&xml, XML_DAV_PROP);
for (i = 0; i < dav->propsz; i++) {
if (dav->props[i].valid >= 0)
continue;
khttp_puts(r, "<X:");
khttp_puts(r, dav->props[i].name);
khttp_puts(r, " xmlns:X=\"");
khttp_puts(r, dav->props[i].xmlns);
khttp_puts(r, "\" />");
}
kxml_pop(&xml);
kxml_push(&xml, XML_DAV_STATUS);
kxml_puts(&xml, "HTTP/1.1 ");
kxml_puts(&xml, khttps[KHTTP_409]);
kxml_pop(&xml);
kxml_pop(&xml);
}
kxml_popall(&xml);
kxml_close(&xml);
/* FIXME: do this first to catch any HTTP 505 errors. */
if (df != 0 && !db_collection_update(&cfg, st->rprncpl))
kutil_errx_noexit(r, st->prncpl->name,
"cannot update collection");
caldav_free(dav);
}