-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutil.c
293 lines (255 loc) · 6.47 KB
/
util.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* 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 <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.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"
const char *const xmls[XML__MAX] = {
"C:calendar", /* XML_CALDAV_CALENDAR */
"C:calendar-data", /* XML_CALDAV_CALENDAR_DATA */
"C:comp", /* XML_CALDAV_COMP */
"C:opaque", /* XML_CALDAV_OPAQUE */
"B:calendar-proxy-read", /* XML_CALDAVSERV_PROXY_READ */
"B:calendar-proxy-read-for", /* XML_CALDAVSERV_PROXY_READ_FOR */
"B:calendar-proxy-write", /* XML_CALDAVSERV_PROXY_WRITE */
"B:calendar-proxy-write-for", /* XML_CALDAVSERV_PROXY_WRIT... */
"D:bind", /* XML_DAV_BIND */
"D:collection", /* XML_DAV_COLLECTION */
"D:href", /* XML_DAV_HREF */
"D:multistatus", /* XML_DAV_MULTISTATUS */
"D:principal", /* XML_DAV_PRINCIPAL */
"D:privilege", /* XML_DAV_PRIVILEGE */
"D:prop", /* XML_DAV_PROP */
"D:propstat", /* XML_DAV_PROPSTAT */
"D:read", /* XML_DAV_READ */
"D:read-current-user-privilege-set", /* XML_DAV_READ_CUR... */
"D:resourcetype", /* XML_DAV_RESOURCETYPE */
"D:response", /* XML_DAV_RESPONSE */
"D:status", /* XML_DAV_STATUS */
"D:unbind", /* XML_DAV_UNBIND */
"D:write", /* XML_DAV_WRITE */
};
/*
* Provided mainly for Linux that doesn't have arc4random.
* Returns a (non-cryptographic) random number between [0, sz).
*/
static uint32_t
get_random_uniform(size_t sz)
{
#if HAVE_ARC4RANDOM
return arc4random_uniform(sz);
#else
return random() % sz;
#endif
}
int
xml_ical_putc(int c, void *arg)
{
struct kxmlreq *r = arg;
return kxml_putc(r, c) == KCGI_OK;
}
static char
parsehex(char ch)
{
return(isdigit((unsigned char)ch) ? ch - '0' :
tolower((unsigned char)ch) - 'a' + 10);
}
static void
http_decode(const char *in, char **rp)
{
size_t i, j, sz;
sz = strlen(in);
*rp = kcalloc(sz + 1, 1);
for (i = j = 0; i < sz; i++, j++) {
if ('+' == in[i]) {
(*rp)[j] = ' ';
continue;
} else if ('%' != in[i]) {
(*rp)[j] = in[i];
continue;
}
if ('\0' == in[i + 1] ||
'\0' == in[i + 2] ||
! isalnum((unsigned char)in[i + 1]) ||
! isalnum((unsigned char)in[i + 2])) {
(*rp)[j] = in[i];
continue;
}
(*rp)[j] =
parsehex(in[i + 1]) << 4 |
parsehex(in[i + 2]);
i += 2;
}
}
/*
* Decompose a path into the principal, collection, and resource parts.
* We manage this as follows:
* /principal/collection/resource
* For now, the collection can't have directories of its own.
* Return zero on failure (no leading slash), non-zero on success.
* All pointers will be set.
*/
int
http_paths(const char *in, char **pp, char **cp, char **rp)
{
const char *p;
*pp = *cp = *rp = NULL;
/* Strip leading absolute path. */
if ('/' != in[0])
return 0;
in++;
if ((p = strchr(in, '/')) != NULL) {
*pp = kmalloc(p - in + 1);
memcpy(*pp, in, p - in);
(*pp)[p - in] = '\0';
in = p + 1;
if ((p = strrchr(in, '/')) != NULL) {
*cp = kmalloc(p - in + 1);
memcpy(*cp, in, p - in);
(*cp)[p - in] = '\0';
in = p + 1;
http_decode(in, rp);
} else {
*cp = kstrdup("");
http_decode(in, rp);
}
} else {
*pp = kstrdup(in);
*cp = kstrdup("");
*rp = kstrdup("");
}
return 1;
}
/*
* Check the safety of the string according to RFC 3986, section 3.3.
* Note that we don't allow percent-encodings, `&', and the apostrophe.
* This ensures that all words that will show up as path components
* (e.g., the principal name) are in fact already well-formed and
* needn't be re-encoded.
*/
int
http_safe_string(const char *cp)
{
if ('\0' == *cp)
return(0);
else if (0 == strcmp(cp, ".") || 0 == strcmp(cp, ".."))
return(0);
for (; '\0' != *cp; cp++)
switch (*cp) {
case ('.'):
case ('-'):
case ('_'):
case ('~'):
case ('!'):
case ('$'):
case ('('):
case (')'):
case ('*'):
case ('+'):
case (','):
case (';'):
case ('='):
case (':'):
case ('@'):
break;
default:
if (isalnum((unsigned char)*cp))
break;
return(0);
}
return(1);
}
int
http_ical_putc(int c, void *arg)
{
struct kreq *r = arg;
return khttp_putc(r, c) == KCGI_OK;
}
void
http_error(struct kreq *r, enum khttp c)
{
char nonce[17];
size_t i;
khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[c]);
switch (c) {
case (KHTTP_200):
case (KHTTP_201):
case (KHTTP_204):
case (KHTTP_207):
case (KHTTP_304):
khttp_head(r, "DAV", "1, access-control, calendar-access");
break;
case (KHTTP_401):
/*
* FIXME: we essentially throw away the nonce
* We should be keeping nonces in a database of client
* sessions, then using the increasing (not necessarily
* linearly, apparently) nonce-count value.
*/
for (i = 0; i < sizeof(nonce) - 1; i++)
snprintf(nonce + i, 2, "%01X",
get_random_uniform(128));
khttp_head(r, kresps[KRESP_WWW_AUTHENTICATE],
"Digest realm=\"%s\", "
"algorithm=\"MD5-sess\", "
"qop=\"auth,auth-int\", "
"nonce=\"%s\"", KREALM, nonce);
break;
default:
break;
}
khttp_body(r);
}
/*
* Parse an etag as described in HTTP 7232.
* This simply strips out quotes and returns non-empty strings.
* If the value is a literal, "buf" is set to non-NULL and must be
* freed.
* It works for both "If-Match" and "If-None-Match".
* Returns the parsed etag or NULL on failure.
*/
const char *
http_etag_if_match(const char *val, char **buf)
{
size_t sz;
assert(val != NULL);
*buf = NULL;
/* Zero-length is a no-no. */
if ((sz = strlen(val)) == 0)
return NULL;
/* Quoted-string needs to be \"[.+]\". */
if (sz > 1 && val[0] == '"' && val[sz - 1] == '"') {
val++;
sz--;
if (sz == 1)
return NULL;
*buf = kstrdup(val);
(*buf)[sz - 1] = '\0';
return (*buf);
}
/* Un-quoted (or badly-quoted) value. */
return val;
}