[mod_proxy] move data_fastcgi into mod_proxy.c
[lighttpd.git] / src / mod_webdav.c
blob804f0da2343aff6edcf29829947ec6b7c26d6a31
1 #include "first.h"
3 #include "base.h"
4 #include "log.h"
5 #include "buffer.h"
6 #include "response.h"
7 #include "connections.h"
9 #include "plugin.h"
11 #include "stat_cache.h"
13 #include "sys-mmap.h"
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <fcntl.h>
23 #include <unistd.h>
24 #include <dirent.h>
26 #if defined(HAVE_LIBXML_H) && defined(HAVE_SQLITE3_H)
27 #define USE_PROPPATCH
28 #include <libxml/tree.h>
29 #include <libxml/parser.h>
31 #include <sqlite3.h>
32 #endif
34 #if defined(HAVE_LIBXML_H) && defined(HAVE_SQLITE3_H) && defined(HAVE_UUID_UUID_H)
35 #define USE_LOCKS
36 #include <uuid/uuid.h>
37 #endif
39 /**
40 * this is a webdav for a lighttpd plugin
42 * at least a very basic one.
43 * - for now it is read-only and we only support PROPFIND
47 #define WEBDAV_FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
48 #define WEBDAV_DIR_MODE S_IRWXU | S_IRWXG | S_IRWXO
50 /* plugin config for all request/connections */
52 typedef struct {
53 unsigned short enabled;
54 unsigned short is_readonly;
55 unsigned short log_xml;
57 buffer *sqlite_db_name;
58 #ifdef USE_PROPPATCH
59 sqlite3 *sql;
60 sqlite3_stmt *stmt_update_prop;
61 sqlite3_stmt *stmt_delete_prop;
62 sqlite3_stmt *stmt_select_prop;
63 sqlite3_stmt *stmt_select_propnames;
65 sqlite3_stmt *stmt_delete_uri;
66 sqlite3_stmt *stmt_move_uri;
67 sqlite3_stmt *stmt_copy_uri;
69 sqlite3_stmt *stmt_remove_lock;
70 sqlite3_stmt *stmt_create_lock;
71 sqlite3_stmt *stmt_read_lock;
72 sqlite3_stmt *stmt_read_lock_by_uri;
73 sqlite3_stmt *stmt_refresh_lock;
74 #endif
75 } plugin_config;
77 typedef struct {
78 PLUGIN_DATA;
80 buffer *tmp_buf;
81 request_uri uri;
82 physical physical;
84 plugin_config **config_storage;
86 plugin_config conf;
87 } plugin_data;
89 typedef struct {
90 plugin_config conf;
91 } handler_ctx;
93 /* init the plugin data */
94 INIT_FUNC(mod_webdav_init) {
95 plugin_data *p;
97 p = calloc(1, sizeof(*p));
99 p->tmp_buf = buffer_init();
101 p->uri.scheme = buffer_init();
102 p->uri.path_raw = buffer_init();
103 p->uri.path = buffer_init();
104 p->uri.authority = buffer_init();
106 p->physical.path = buffer_init();
107 p->physical.rel_path = buffer_init();
108 p->physical.doc_root = buffer_init();
109 p->physical.basedir = buffer_init();
111 return p;
114 /* detroy the plugin data */
115 FREE_FUNC(mod_webdav_free) {
116 plugin_data *p = p_d;
118 UNUSED(srv);
120 if (!p) return HANDLER_GO_ON;
122 if (p->config_storage) {
123 size_t i;
124 for (i = 0; i < srv->config_context->used; i++) {
125 plugin_config *s = p->config_storage[i];
127 if (NULL == s) continue;
129 buffer_free(s->sqlite_db_name);
130 #ifdef USE_PROPPATCH
131 if (s->sql) {
132 sqlite3_finalize(s->stmt_delete_prop);
133 sqlite3_finalize(s->stmt_delete_uri);
134 sqlite3_finalize(s->stmt_copy_uri);
135 sqlite3_finalize(s->stmt_move_uri);
136 sqlite3_finalize(s->stmt_update_prop);
137 sqlite3_finalize(s->stmt_select_prop);
138 sqlite3_finalize(s->stmt_select_propnames);
140 sqlite3_finalize(s->stmt_read_lock);
141 sqlite3_finalize(s->stmt_read_lock_by_uri);
142 sqlite3_finalize(s->stmt_create_lock);
143 sqlite3_finalize(s->stmt_remove_lock);
144 sqlite3_finalize(s->stmt_refresh_lock);
145 sqlite3_close(s->sql);
147 #endif
148 free(s);
150 free(p->config_storage);
153 buffer_free(p->uri.scheme);
154 buffer_free(p->uri.path_raw);
155 buffer_free(p->uri.path);
156 buffer_free(p->uri.authority);
158 buffer_free(p->physical.path);
159 buffer_free(p->physical.rel_path);
160 buffer_free(p->physical.doc_root);
161 buffer_free(p->physical.basedir);
163 buffer_free(p->tmp_buf);
165 free(p);
167 return HANDLER_GO_ON;
170 /* handle plugin config and check values */
172 SETDEFAULTS_FUNC(mod_webdav_set_defaults) {
173 plugin_data *p = p_d;
174 size_t i = 0;
176 config_values_t cv[] = {
177 { "webdav.activate", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
178 { "webdav.is-readonly", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
179 { "webdav.sqlite-db-name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
180 { "webdav.log-xml", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
181 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
184 if (!p) return HANDLER_ERROR;
186 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
188 for (i = 0; i < srv->config_context->used; i++) {
189 data_config const* config = (data_config const*)srv->config_context->data[i];
190 plugin_config *s;
192 s = calloc(1, sizeof(plugin_config));
193 s->sqlite_db_name = buffer_init();
195 cv[0].destination = &(s->enabled);
196 cv[1].destination = &(s->is_readonly);
197 cv[2].destination = s->sqlite_db_name;
198 cv[3].destination = &(s->log_xml);
200 p->config_storage[i] = s;
202 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
203 return HANDLER_ERROR;
206 if (!buffer_string_is_empty(s->sqlite_db_name)) {
207 #ifdef USE_PROPPATCH
208 const char *next_stmt;
209 char *err;
211 if (SQLITE_OK != sqlite3_open(s->sqlite_db_name->ptr, &(s->sql))) {
212 log_error_write(srv, __FILE__, __LINE__, "sbs", "sqlite3_open failed for",
213 s->sqlite_db_name,
214 sqlite3_errmsg(s->sql));
215 return HANDLER_ERROR;
218 if (SQLITE_OK != sqlite3_exec(s->sql,
219 "CREATE TABLE IF NOT EXISTS properties ("
220 " resource TEXT NOT NULL,"
221 " prop TEXT NOT NULL,"
222 " ns TEXT NOT NULL,"
223 " value TEXT NOT NULL,"
224 " PRIMARY KEY(resource, prop, ns))",
225 NULL, NULL, &err)) {
227 if (0 != strcmp(err, "table properties already exists")) {
228 log_error_write(srv, __FILE__, __LINE__, "ss", "can't open transaction:", err);
229 sqlite3_free(err);
231 return HANDLER_ERROR;
233 sqlite3_free(err);
236 if (SQLITE_OK != sqlite3_exec(s->sql,
237 "CREATE TABLE IF NOT EXISTS locks ("
238 " locktoken TEXT NOT NULL,"
239 " resource TEXT NOT NULL,"
240 " lockscope TEXT NOT NULL,"
241 " locktype TEXT NOT NULL,"
242 " owner TEXT NOT NULL,"
243 " depth INT NOT NULL,"
244 " timeout TIMESTAMP NOT NULL,"
245 " PRIMARY KEY(locktoken))",
246 NULL, NULL, &err)) {
248 if (0 != strcmp(err, "table locks already exists")) {
249 log_error_write(srv, __FILE__, __LINE__, "ss", "can't open transaction:", err);
250 sqlite3_free(err);
252 return HANDLER_ERROR;
254 sqlite3_free(err);
257 if (SQLITE_OK != sqlite3_prepare(s->sql,
258 CONST_STR_LEN("SELECT value FROM properties WHERE resource = ? AND prop = ? AND ns = ?"),
259 &(s->stmt_select_prop), &next_stmt)) {
260 /* prepare failed */
262 log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed:", sqlite3_errmsg(s->sql));
263 return HANDLER_ERROR;
266 if (SQLITE_OK != sqlite3_prepare(s->sql,
267 CONST_STR_LEN("SELECT ns, prop FROM properties WHERE resource = ?"),
268 &(s->stmt_select_propnames), &next_stmt)) {
269 /* prepare failed */
271 log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed:", sqlite3_errmsg(s->sql));
272 return HANDLER_ERROR;
276 if (SQLITE_OK != sqlite3_prepare(s->sql,
277 CONST_STR_LEN("REPLACE INTO properties (resource, prop, ns, value) VALUES (?, ?, ?, ?)"),
278 &(s->stmt_update_prop), &next_stmt)) {
279 /* prepare failed */
281 log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed:", sqlite3_errmsg(s->sql));
282 return HANDLER_ERROR;
285 if (SQLITE_OK != sqlite3_prepare(s->sql,
286 CONST_STR_LEN("DELETE FROM properties WHERE resource = ? AND prop = ? AND ns = ?"),
287 &(s->stmt_delete_prop), &next_stmt)) {
288 /* prepare failed */
289 log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
291 return HANDLER_ERROR;
294 if (SQLITE_OK != sqlite3_prepare(s->sql,
295 CONST_STR_LEN("DELETE FROM properties WHERE resource = ?"),
296 &(s->stmt_delete_uri), &next_stmt)) {
297 /* prepare failed */
298 log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
300 return HANDLER_ERROR;
303 if (SQLITE_OK != sqlite3_prepare(s->sql,
304 CONST_STR_LEN("INSERT INTO properties SELECT ?, prop, ns, value FROM properties WHERE resource = ?"),
305 &(s->stmt_copy_uri), &next_stmt)) {
306 /* prepare failed */
307 log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
309 return HANDLER_ERROR;
312 if (SQLITE_OK != sqlite3_prepare(s->sql,
313 CONST_STR_LEN("UPDATE OR REPLACE properties SET resource = ? WHERE resource = ?"),
314 &(s->stmt_move_uri), &next_stmt)) {
315 /* prepare failed */
316 log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
318 return HANDLER_ERROR;
321 /* LOCKS */
323 if (SQLITE_OK != sqlite3_prepare(s->sql,
324 CONST_STR_LEN("INSERT INTO locks (locktoken, resource, lockscope, locktype, owner, depth, timeout) VALUES (?,?,?,?,?,?, CURRENT_TIME + 600)"),
325 &(s->stmt_create_lock), &next_stmt)) {
326 /* prepare failed */
327 log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
329 return HANDLER_ERROR;
332 if (SQLITE_OK != sqlite3_prepare(s->sql,
333 CONST_STR_LEN("DELETE FROM locks WHERE locktoken = ?"),
334 &(s->stmt_remove_lock), &next_stmt)) {
335 /* prepare failed */
336 log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
338 return HANDLER_ERROR;
341 if (SQLITE_OK != sqlite3_prepare(s->sql,
342 CONST_STR_LEN("SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout-CURRENT_TIME FROM locks WHERE locktoken = ?"),
343 &(s->stmt_read_lock), &next_stmt)) {
344 /* prepare failed */
345 log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
347 return HANDLER_ERROR;
350 if (SQLITE_OK != sqlite3_prepare(s->sql,
351 CONST_STR_LEN("SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout-CURRENT_TIME FROM locks WHERE resource = ?"),
352 &(s->stmt_read_lock_by_uri), &next_stmt)) {
353 /* prepare failed */
354 log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
356 return HANDLER_ERROR;
359 if (SQLITE_OK != sqlite3_prepare(s->sql,
360 CONST_STR_LEN("UPDATE locks SET timeout = CURRENT_TIME + 600 WHERE locktoken = ?"),
361 &(s->stmt_refresh_lock), &next_stmt)) {
362 /* prepare failed */
363 log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
365 return HANDLER_ERROR;
369 #else
370 log_error_write(srv, __FILE__, __LINE__, "s", "Sorry, no sqlite3 and libxml2 support include, compile with --with-webdav-props");
371 return HANDLER_ERROR;
372 #endif
376 return HANDLER_GO_ON;
379 #define PATCH_OPTION(x) \
380 p->conf.x = s->x;
381 static int mod_webdav_patch_connection(server *srv, connection *con, plugin_data *p) {
382 size_t i, j;
383 plugin_config *s = p->config_storage[0];
385 PATCH_OPTION(enabled);
386 PATCH_OPTION(is_readonly);
387 PATCH_OPTION(log_xml);
389 #ifdef USE_PROPPATCH
390 PATCH_OPTION(sql);
391 PATCH_OPTION(stmt_update_prop);
392 PATCH_OPTION(stmt_delete_prop);
393 PATCH_OPTION(stmt_select_prop);
394 PATCH_OPTION(stmt_select_propnames);
396 PATCH_OPTION(stmt_delete_uri);
397 PATCH_OPTION(stmt_move_uri);
398 PATCH_OPTION(stmt_copy_uri);
400 PATCH_OPTION(stmt_remove_lock);
401 PATCH_OPTION(stmt_refresh_lock);
402 PATCH_OPTION(stmt_create_lock);
403 PATCH_OPTION(stmt_read_lock);
404 PATCH_OPTION(stmt_read_lock_by_uri);
405 #endif
406 /* skip the first, the global context */
407 for (i = 1; i < srv->config_context->used; i++) {
408 data_config *dc = (data_config *)srv->config_context->data[i];
409 s = p->config_storage[i];
411 /* condition didn't match */
412 if (!config_check_cond(srv, con, dc)) continue;
414 /* merge config */
415 for (j = 0; j < dc->value->used; j++) {
416 data_unset *du = dc->value->data[j];
418 if (buffer_is_equal_string(du->key, CONST_STR_LEN("webdav.activate"))) {
419 PATCH_OPTION(enabled);
420 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("webdav.is-readonly"))) {
421 PATCH_OPTION(is_readonly);
422 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("webdav.log-xml"))) {
423 PATCH_OPTION(log_xml);
424 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("webdav.sqlite-db-name"))) {
425 #ifdef USE_PROPPATCH
426 PATCH_OPTION(sql);
427 PATCH_OPTION(stmt_update_prop);
428 PATCH_OPTION(stmt_delete_prop);
429 PATCH_OPTION(stmt_select_prop);
430 PATCH_OPTION(stmt_select_propnames);
432 PATCH_OPTION(stmt_delete_uri);
433 PATCH_OPTION(stmt_move_uri);
434 PATCH_OPTION(stmt_copy_uri);
436 PATCH_OPTION(stmt_remove_lock);
437 PATCH_OPTION(stmt_refresh_lock);
438 PATCH_OPTION(stmt_create_lock);
439 PATCH_OPTION(stmt_read_lock);
440 PATCH_OPTION(stmt_read_lock_by_uri);
441 #endif
446 return 0;
449 URIHANDLER_FUNC(mod_webdav_uri_handler) {
450 plugin_data *p = p_d;
452 UNUSED(srv);
454 if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON;
456 mod_webdav_patch_connection(srv, con, p);
458 if (!p->conf.enabled) return HANDLER_GO_ON;
460 switch (con->request.http_method) {
461 case HTTP_METHOD_OPTIONS:
462 /* we fake a little bit but it makes MS W2k happy and it let's us mount the volume */
463 response_header_overwrite(srv, con, CONST_STR_LEN("DAV"), CONST_STR_LEN("1,2"));
464 response_header_overwrite(srv, con, CONST_STR_LEN("MS-Author-Via"), CONST_STR_LEN("DAV"));
466 if (p->conf.is_readonly) {
467 response_header_insert(srv, con, CONST_STR_LEN("Allow"), CONST_STR_LEN("PROPFIND"));
468 } else {
469 response_header_insert(srv, con, CONST_STR_LEN("Allow"), CONST_STR_LEN("PROPFIND, DELETE, MKCOL, PUT, MOVE, COPY, PROPPATCH, LOCK, UNLOCK"));
471 break;
472 default:
473 break;
476 /* not found */
477 return HANDLER_GO_ON;
479 static int webdav_gen_prop_tag(server *srv, connection *con,
480 char *prop_name,
481 char *prop_ns,
482 char *value,
483 buffer *b) {
485 UNUSED(srv);
486 UNUSED(con);
488 if (value) {
489 buffer_append_string_len(b,CONST_STR_LEN("<"));
490 buffer_append_string(b, prop_name);
491 buffer_append_string_len(b, CONST_STR_LEN(" xmlns=\""));
492 buffer_append_string(b, prop_ns);
493 buffer_append_string_len(b, CONST_STR_LEN("\">"));
495 buffer_append_string(b, value);
497 buffer_append_string_len(b,CONST_STR_LEN("</"));
498 buffer_append_string(b, prop_name);
499 buffer_append_string_len(b, CONST_STR_LEN(">"));
500 } else {
501 buffer_append_string_len(b,CONST_STR_LEN("<"));
502 buffer_append_string(b, prop_name);
503 buffer_append_string_len(b, CONST_STR_LEN(" xmlns=\""));
504 buffer_append_string(b, prop_ns);
505 buffer_append_string_len(b, CONST_STR_LEN("\"/>"));
508 return 0;
512 static int webdav_gen_response_status_tag(server *srv, connection *con, physical *dst, int status, buffer *b) {
513 UNUSED(srv);
515 buffer_append_string_len(b,CONST_STR_LEN("<D:response xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\">\n"));
517 buffer_append_string_len(b,CONST_STR_LEN("<D:href>\n"));
518 buffer_append_string_buffer(b, dst->rel_path);
519 buffer_append_string_len(b,CONST_STR_LEN("</D:href>\n"));
520 buffer_append_string_len(b,CONST_STR_LEN("<D:status>\n"));
522 if (con->request.http_version == HTTP_VERSION_1_1) {
523 buffer_copy_string_len(b, CONST_STR_LEN("HTTP/1.1 "));
524 } else {
525 buffer_copy_string_len(b, CONST_STR_LEN("HTTP/1.0 "));
527 buffer_append_int(b, status);
528 buffer_append_string_len(b, CONST_STR_LEN(" "));
529 buffer_append_string(b, get_http_status_name(status));
531 buffer_append_string_len(b,CONST_STR_LEN("</D:status>\n"));
532 buffer_append_string_len(b,CONST_STR_LEN("</D:response>\n"));
534 return 0;
537 static int webdav_delete_file(server *srv, connection *con, handler_ctx *hctx, physical *dst, buffer *b) {
538 int status = 0;
540 /* try to unlink it */
541 if (-1 == unlink(dst->path->ptr)) {
542 switch(errno) {
543 case EACCES:
544 case EPERM:
545 /* 403 */
546 status = 403;
547 break;
548 default:
549 status = 501;
550 break;
552 webdav_gen_response_status_tag(srv, con, dst, status, b);
553 } else {
554 #ifdef USE_PROPPATCH
555 sqlite3_stmt *stmt = hctx->conf.stmt_delete_uri;
557 if (!stmt) {
558 status = 403;
559 webdav_gen_response_status_tag(srv, con, dst, status, b);
560 } else {
561 sqlite3_reset(stmt);
563 /* bind the values to the insert */
565 sqlite3_bind_text(stmt, 1,
566 CONST_BUF_LEN(dst->rel_path),
567 SQLITE_TRANSIENT);
569 if (SQLITE_DONE != sqlite3_step(stmt)) {
570 /* */
573 #else
574 UNUSED(hctx);
575 #endif
578 return (status != 0);
581 static int webdav_delete_dir(server *srv, connection *con, handler_ctx *hctx, physical *dst, buffer *b) {
582 DIR *dir;
583 int have_multi_status = 0;
584 physical d;
586 d.path = buffer_init();
587 d.rel_path = buffer_init();
589 if (NULL != (dir = opendir(dst->path->ptr))) {
590 struct dirent *de;
592 while(NULL != (de = readdir(dir))) {
593 struct stat st;
595 if ((de->d_name[0] == '.' && de->d_name[1] == '\0') ||
596 (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')) {
597 continue;
598 /* ignore the parent dir */
601 buffer_copy_buffer(d.path, dst->path);
602 buffer_append_slash(d.path);
603 buffer_append_string(d.path, de->d_name);
605 buffer_copy_buffer(d.rel_path, dst->rel_path);
606 buffer_append_slash(d.rel_path);
607 buffer_append_string(d.rel_path, de->d_name);
609 /* stat and unlink afterwards */
610 if (-1 == stat(d.path->ptr, &st)) {
611 /* don't about it yet, rmdir will fail too */
612 } else if (S_ISDIR(st.st_mode)) {
613 have_multi_status = webdav_delete_dir(srv, con, hctx, &d, b);
615 /* try to unlink it */
616 if (-1 == rmdir(d.path->ptr)) {
617 int status;
618 switch(errno) {
619 case EACCES:
620 case EPERM:
621 /* 403 */
622 status = 403;
623 break;
624 default:
625 status = 501;
626 break;
628 have_multi_status = 1;
630 webdav_gen_response_status_tag(srv, con, &d, status, b);
631 } else {
632 #ifdef USE_PROPPATCH
633 sqlite3_stmt *stmt = hctx->conf.stmt_delete_uri;
635 if (stmt) {
636 sqlite3_reset(stmt);
638 /* bind the values to the insert */
640 sqlite3_bind_text(stmt, 1,
641 CONST_BUF_LEN(d.rel_path),
642 SQLITE_TRANSIENT);
644 if (SQLITE_DONE != sqlite3_step(stmt)) {
645 /* */
648 #endif
650 } else {
651 have_multi_status = webdav_delete_file(srv, con, hctx, &d, b);
654 closedir(dir);
656 buffer_free(d.path);
657 buffer_free(d.rel_path);
660 return have_multi_status;
663 /* don't want to block when open()ing a fifo */
664 #if defined(O_NONBLOCK)
665 # define FIFO_NONBLOCK O_NONBLOCK
666 #else
667 # define FIFO_NONBLOCK 0
668 #endif
670 #ifndef O_BINARY
671 #define O_BINARY 0
672 #endif
674 static int webdav_copy_file(server *srv, connection *con, handler_ctx *hctx, physical *src, physical *dst, int overwrite) {
675 char *data;
676 ssize_t rd, wr, offset;
677 int status = 0, ifd, ofd;
678 UNUSED(srv);
679 UNUSED(con);
681 if (-1 == (ifd = open(src->path->ptr, O_RDONLY | O_BINARY | FIFO_NONBLOCK))) {
682 return 403;
685 if (-1 == (ofd = open(dst->path->ptr, O_WRONLY|O_TRUNC|O_CREAT|(overwrite ? 0 : O_EXCL), WEBDAV_FILE_MODE))) {
686 /* opening the destination failed for some reason */
687 switch(errno) {
688 case EEXIST:
689 status = 412;
690 break;
691 case EISDIR:
692 status = 409;
693 break;
694 case ENOENT:
695 /* at least one part in the middle wasn't existing */
696 status = 409;
697 break;
698 default:
699 status = 403;
700 break;
702 close(ifd);
703 return status;
706 data = malloc(131072);
707 force_assert(data);
709 while (0 < (rd = read(ifd, data, 131072))) {
710 offset = 0;
711 do {
712 wr = write(ofd, data+offset, (size_t)(rd-offset));
713 } while (wr >= 0 ? (offset += wr) != rd : (errno == EINTR));
714 if (-1 == wr) {
715 status = (errno == ENOSPC) ? 507 : 403;
716 break;
720 if (0 != rd && 0 == status) status = 403;
722 free(data);
723 close(ifd);
724 if (0 != close(ofd)) {
725 if (0 == status) status = (errno == ENOSPC) ? 507 : 403;
726 log_error_write(srv, __FILE__, __LINE__, "sbss",
727 "close ", dst->path, "failed: ", strerror(errno));
730 #ifdef USE_PROPPATCH
731 if (0 == status) {
732 /* copy worked fine, copy connected properties */
733 sqlite3_stmt *stmt = hctx->conf.stmt_copy_uri;
735 if (stmt) {
736 sqlite3_reset(stmt);
738 /* bind the values to the insert */
739 sqlite3_bind_text(stmt, 1,
740 CONST_BUF_LEN(dst->rel_path),
741 SQLITE_TRANSIENT);
743 sqlite3_bind_text(stmt, 2,
744 CONST_BUF_LEN(src->rel_path),
745 SQLITE_TRANSIENT);
747 if (SQLITE_DONE != sqlite3_step(stmt)) {
748 /* */
752 #else
753 UNUSED(hctx);
754 #endif
755 return status;
758 static int webdav_copy_dir(server *srv, connection *con, handler_ctx *hctx, physical *src, physical *dst, int overwrite) {
759 DIR *srcdir;
760 int status = 0;
762 if (NULL != (srcdir = opendir(src->path->ptr))) {
763 struct dirent *de;
764 physical s, d;
766 s.path = buffer_init();
767 s.rel_path = buffer_init();
769 d.path = buffer_init();
770 d.rel_path = buffer_init();
772 while (NULL != (de = readdir(srcdir))) {
773 struct stat st;
775 if ((de->d_name[0] == '.' && de->d_name[1] == '\0')
776 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')) {
777 continue;
780 buffer_copy_buffer(s.path, src->path);
781 buffer_append_slash(s.path);
782 buffer_append_string(s.path, de->d_name);
784 buffer_copy_buffer(d.path, dst->path);
785 buffer_append_slash(d.path);
786 buffer_append_string(d.path, de->d_name);
788 buffer_copy_buffer(s.rel_path, src->rel_path);
789 buffer_append_slash(s.rel_path);
790 buffer_append_string(s.rel_path, de->d_name);
792 buffer_copy_buffer(d.rel_path, dst->rel_path);
793 buffer_append_slash(d.rel_path);
794 buffer_append_string(d.rel_path, de->d_name);
796 if (-1 == stat(s.path->ptr, &st)) {
797 /* why ? */
798 } else if (S_ISDIR(st.st_mode)) {
799 /* a directory */
800 if (-1 == mkdir(d.path->ptr, WEBDAV_DIR_MODE) &&
801 errno != EEXIST) {
802 /* WTH ? */
803 } else {
804 #ifdef USE_PROPPATCH
805 sqlite3_stmt *stmt = hctx->conf.stmt_copy_uri;
807 if (0 != (status = webdav_copy_dir(srv, con, hctx, &s, &d, overwrite))) {
808 break;
810 /* directory is copied, copy the properties too */
812 if (stmt) {
813 sqlite3_reset(stmt);
815 /* bind the values to the insert */
816 sqlite3_bind_text(stmt, 1,
817 CONST_BUF_LEN(dst->rel_path),
818 SQLITE_TRANSIENT);
820 sqlite3_bind_text(stmt, 2,
821 CONST_BUF_LEN(src->rel_path),
822 SQLITE_TRANSIENT);
824 if (SQLITE_DONE != sqlite3_step(stmt)) {
825 /* */
828 #endif
830 } else if (S_ISREG(st.st_mode)) {
831 /* a plain file */
832 if (0 != (status = webdav_copy_file(srv, con, hctx, &s, &d, overwrite))) {
833 break;
838 buffer_free(s.path);
839 buffer_free(s.rel_path);
840 buffer_free(d.path);
841 buffer_free(d.rel_path);
843 closedir(srcdir);
846 return status;
849 #ifdef USE_LOCKS
850 static void webdav_activelock(buffer *b,
851 const buffer *locktoken, const char *lockscope, const char *locktype, int depth, int timeout) {
852 buffer_append_string_len(b, CONST_STR_LEN("<D:activelock>\n"));
854 buffer_append_string_len(b, CONST_STR_LEN("<D:lockscope>"));
855 buffer_append_string_len(b, CONST_STR_LEN("<D:"));
856 buffer_append_string(b, lockscope);
857 buffer_append_string_len(b, CONST_STR_LEN("/>"));
858 buffer_append_string_len(b, CONST_STR_LEN("</D:lockscope>\n"));
860 buffer_append_string_len(b, CONST_STR_LEN("<D:locktype>"));
861 buffer_append_string_len(b, CONST_STR_LEN("<D:"));
862 buffer_append_string(b, locktype);
863 buffer_append_string_len(b, CONST_STR_LEN("/>"));
864 buffer_append_string_len(b, CONST_STR_LEN("</D:locktype>\n"));
866 buffer_append_string_len(b, CONST_STR_LEN("<D:depth>"));
867 buffer_append_string(b, depth == 0 ? "0" : "infinity");
868 buffer_append_string_len(b, CONST_STR_LEN("</D:depth>\n"));
870 buffer_append_string_len(b, CONST_STR_LEN("<D:timeout>"));
871 buffer_append_string_len(b, CONST_STR_LEN("Second-"));
872 buffer_append_int(b, timeout);
873 buffer_append_string_len(b, CONST_STR_LEN("</D:timeout>\n"));
875 buffer_append_string_len(b, CONST_STR_LEN("<D:owner>"));
876 buffer_append_string_len(b, CONST_STR_LEN("</D:owner>\n"));
878 buffer_append_string_len(b, CONST_STR_LEN("<D:locktoken>"));
879 buffer_append_string_len(b, CONST_STR_LEN("<D:href>"));
880 buffer_append_string_buffer(b, locktoken);
881 buffer_append_string_len(b, CONST_STR_LEN("</D:href>"));
882 buffer_append_string_len(b, CONST_STR_LEN("</D:locktoken>\n"));
884 buffer_append_string_len(b, CONST_STR_LEN("</D:activelock>\n"));
887 static void webdav_get_live_property_lockdiscovery(server *srv, connection *con, handler_ctx *hctx, physical *dst, buffer *b) {
889 sqlite3_stmt *stmt = hctx->conf.stmt_read_lock_by_uri;
890 if (!stmt) { /*(should not happen)*/
891 buffer_append_string_len(b, CONST_STR_LEN("<D:lockdiscovery>\n</D:lockdiscovery>\n"));
892 return;
894 UNUSED(srv);
895 UNUSED(con);
897 /* SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout
898 * FROM locks
899 * WHERE resource = ? */
901 sqlite3_reset(stmt);
903 sqlite3_bind_text(stmt, 1,
904 CONST_BUF_LEN(dst->rel_path),
905 SQLITE_TRANSIENT);
907 buffer_append_string_len(b, CONST_STR_LEN("<D:lockdiscovery>\n"));
908 while (SQLITE_ROW == sqlite3_step(stmt)) {
909 const char *lockscope = (const char *)sqlite3_column_text(stmt, 2);
910 const char *locktype = (const char *)sqlite3_column_text(stmt, 3);
911 const int depth = sqlite3_column_int(stmt, 5);
912 const int timeout = sqlite3_column_int(stmt, 6);
913 buffer locktoken = { NULL, 0, 0 };
914 locktoken.ptr = (char *)sqlite3_column_text(stmt, 0);
915 locktoken.used = sqlite3_column_bytes(stmt, 0);
916 if (locktoken.used) ++locktoken.used;
917 locktoken.size = locktoken.used;
919 if (timeout > 0) {
920 webdav_activelock(b, &locktoken, lockscope, locktype, depth, timeout);
923 buffer_append_string_len(b, CONST_STR_LEN("</D:lockdiscovery>\n"));
925 #endif
927 static int webdav_get_live_property(server *srv, connection *con, handler_ctx *hctx, physical *dst, char *prop_name, buffer *b) {
928 stat_cache_entry *sce = NULL;
929 int found = 0;
931 UNUSED(hctx);
933 if (HANDLER_ERROR != (stat_cache_get_entry(srv, con, dst->path, &sce))) {
934 char ctime_buf[] = "2005-08-18T07:27:16Z";
935 char mtime_buf[] = "Thu, 18 Aug 2005 07:27:16 GMT";
937 if (0 == strcmp(prop_name, "resourcetype")) {
938 if (S_ISDIR(sce->st.st_mode)) {
939 buffer_append_string_len(b, CONST_STR_LEN("<D:resourcetype><D:collection/></D:resourcetype>"));
940 } else {
941 buffer_append_string_len(b, CONST_STR_LEN("<D:resourcetype/>"));
943 found = 1;
944 } else if (0 == strcmp(prop_name, "getcontenttype")) {
945 if (S_ISDIR(sce->st.st_mode)) {
946 buffer_append_string_len(b, CONST_STR_LEN("<D:getcontenttype>httpd/unix-directory</D:getcontenttype>"));
947 found = 1;
948 } else if(S_ISREG(sce->st.st_mode)) {
949 const buffer *type = stat_cache_mimetype_by_ext(con, CONST_BUF_LEN(dst->path));
950 if (NULL != type) {
951 buffer_append_string_len(b, CONST_STR_LEN("<D:getcontenttype>"));
952 buffer_append_string_buffer(b, type);
953 buffer_append_string_len(b, CONST_STR_LEN("</D:getcontenttype>"));
954 found = 1;
957 } else if (0 == strcmp(prop_name, "creationdate")) {
958 buffer_append_string_len(b, CONST_STR_LEN("<D:creationdate ns0:dt=\"dateTime.tz\">"));
959 strftime(ctime_buf, sizeof(ctime_buf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&(sce->st.st_ctime)));
960 buffer_append_string(b, ctime_buf);
961 buffer_append_string_len(b, CONST_STR_LEN("</D:creationdate>"));
962 found = 1;
963 } else if (0 == strcmp(prop_name, "getlastmodified")) {
964 buffer_append_string_len(b,CONST_STR_LEN("<D:getlastmodified ns0:dt=\"dateTime.rfc1123\">"));
965 strftime(mtime_buf, sizeof(mtime_buf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&(sce->st.st_mtime)));
966 buffer_append_string(b, mtime_buf);
967 buffer_append_string_len(b, CONST_STR_LEN("</D:getlastmodified>"));
968 found = 1;
969 } else if (0 == strcmp(prop_name, "getcontentlength")) {
970 buffer_append_string_len(b,CONST_STR_LEN("<D:getcontentlength>"));
971 buffer_append_int(b, sce->st.st_size);
972 buffer_append_string_len(b, CONST_STR_LEN("</D:getcontentlength>"));
973 found = 1;
974 } else if (0 == strcmp(prop_name, "getcontentlanguage")) {
975 buffer_append_string_len(b,CONST_STR_LEN("<D:getcontentlanguage>"));
976 buffer_append_string_len(b, CONST_STR_LEN("en"));
977 buffer_append_string_len(b, CONST_STR_LEN("</D:getcontentlanguage>"));
978 found = 1;
979 } else if (0 == strcmp(prop_name, "getetag")) {
980 etag_create(con->physical.etag, &sce->st, con->etag_flags);
981 buffer_append_string_len(b, CONST_STR_LEN("<D:getetag>"));
982 buffer_append_string_buffer(b, con->physical.etag);
983 buffer_append_string_len(b, CONST_STR_LEN("</D:getetag>"));
984 buffer_reset(con->physical.etag);
985 found = 1;
986 #ifdef USE_LOCKS
987 } else if (0 == strcmp(prop_name, "lockdiscovery")) {
988 webdav_get_live_property_lockdiscovery(srv, con, hctx, dst, b);
989 found = 1;
990 } else if (0 == strcmp(prop_name, "supportedlock")) {
991 buffer_append_string_len(b,CONST_STR_LEN("<D:supportedlock>"));
992 buffer_append_string_len(b,CONST_STR_LEN("<D:lockentry>"));
993 buffer_append_string_len(b,CONST_STR_LEN("<D:lockscope><D:exclusive/></D:lockscope>"));
994 buffer_append_string_len(b,CONST_STR_LEN("<D:locktype><D:write/></D:locktype>"));
995 buffer_append_string_len(b,CONST_STR_LEN("</D:lockentry>"));
996 buffer_append_string_len(b, CONST_STR_LEN("</D:supportedlock>"));
997 found = 1;
998 #endif
1002 return found ? 0 : -1;
1005 static int webdav_get_property(server *srv, connection *con, handler_ctx *hctx, physical *dst, char *prop_name, char *prop_ns, buffer *b) {
1006 if (0 == strcmp(prop_ns, "DAV:")) {
1007 /* a local 'live' property */
1008 return webdav_get_live_property(srv, con, hctx, dst, prop_name, b);
1009 } else {
1010 int found = 0;
1011 #ifdef USE_PROPPATCH
1012 sqlite3_stmt *stmt = hctx->conf.stmt_select_prop;
1014 if (stmt) {
1015 /* perhaps it is in sqlite3 */
1016 sqlite3_reset(stmt);
1018 /* bind the values to the insert */
1020 sqlite3_bind_text(stmt, 1,
1021 CONST_BUF_LEN(dst->rel_path),
1022 SQLITE_TRANSIENT);
1023 sqlite3_bind_text(stmt, 2,
1024 prop_name,
1025 strlen(prop_name),
1026 SQLITE_TRANSIENT);
1027 sqlite3_bind_text(stmt, 3,
1028 prop_ns,
1029 strlen(prop_ns),
1030 SQLITE_TRANSIENT);
1032 /* it is the PK */
1033 while (SQLITE_ROW == sqlite3_step(stmt)) {
1034 /* there is a row for us, we only expect a single col 'value' */
1035 webdav_gen_prop_tag(srv, con, prop_name, prop_ns, (char *)sqlite3_column_text(stmt, 0), b);
1036 found = 1;
1039 #endif
1040 return found ? 0 : -1;
1043 /* not found */
1044 return -1;
1047 typedef struct {
1048 char *ns;
1049 char *prop;
1050 } webdav_property;
1052 static webdav_property live_properties[] = {
1053 { "DAV:", "creationdate" },
1054 /*{ "DAV:", "displayname" },*//*(not implemented)*/
1055 { "DAV:", "getcontentlanguage" },
1056 { "DAV:", "getcontentlength" },
1057 { "DAV:", "getcontenttype" },
1058 { "DAV:", "getetag" },
1059 { "DAV:", "getlastmodified" },
1060 { "DAV:", "resourcetype" },
1061 /*{ "DAV:", "source" },*//*(not implemented)*/
1062 #ifdef USE_LOCKS
1063 { "DAV:", "lockdiscovery" },
1064 { "DAV:", "supportedlock" },
1065 #endif
1067 { NULL, NULL }
1070 typedef struct {
1071 webdav_property **ptr;
1073 size_t used;
1074 size_t size;
1075 } webdav_properties;
1077 static int webdav_get_props(server *srv, connection *con, handler_ctx *hctx, physical *dst, webdav_properties *props, buffer *b_200, buffer *b_404) {
1078 size_t i;
1080 if (props && props->used) {
1081 for (i = 0; i < props->used; i++) {
1082 webdav_property *prop;
1084 prop = props->ptr[i];
1086 if (0 != webdav_get_property(srv, con, hctx,
1087 dst, prop->prop, prop->ns, b_200)) {
1088 webdav_gen_prop_tag(srv, con, prop->prop, prop->ns, NULL, b_404);
1091 } else {
1092 for (i = 0; live_properties[i].prop; i++) {
1093 /* a local 'live' property */
1094 webdav_get_live_property(srv, con, hctx, dst, live_properties[i].prop, b_200);
1098 return 0;
1101 #ifdef USE_PROPPATCH
1102 static int webdav_parse_chunkqueue(server *srv, connection *con, handler_ctx *hctx, chunkqueue *cq, xmlDoc **ret_xml) {
1103 xmlParserCtxtPtr ctxt;
1104 xmlDoc *xml;
1105 int res;
1106 int err;
1108 chunk *c;
1110 UNUSED(con);
1112 /* read the chunks in to the XML document */
1113 ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
1115 for (c = cq->first; cq->bytes_out != cq->bytes_in; c = cq->first) {
1116 size_t weWant = cq->bytes_out - cq->bytes_in;
1117 size_t weHave;
1118 int mapped;
1119 void *data;
1121 switch(c->type) {
1122 case FILE_CHUNK:
1123 weHave = c->file.length - c->offset;
1125 if (weHave > weWant) weHave = weWant;
1127 /* xml chunks are always memory, mmap() is our friend */
1128 mapped = (c->file.mmap.start != MAP_FAILED);
1129 if (mapped) {
1130 data = c->file.mmap.start + c->offset;
1131 } else {
1132 if (-1 == c->file.fd && /* open the file if not already open */
1133 -1 == (c->file.fd = open(c->file.name->ptr, O_RDONLY))) {
1134 log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno));
1136 return -1;
1139 if (MAP_FAILED != (c->file.mmap.start = mmap(0, c->file.length, PROT_READ, MAP_PRIVATE, c->file.fd, 0))) {
1140 /* chunk_reset() or chunk_free() will cleanup for us */
1141 c->file.mmap.length = c->file.length;
1142 data = c->file.mmap.start + c->offset;
1143 mapped = 1;
1144 } else {
1145 ssize_t rd;
1146 if (weHave > 65536) weHave = 65536;
1147 data = malloc(weHave);
1148 force_assert(data);
1149 if (-1 == lseek(c->file.fd, c->file.start + c->offset, SEEK_SET)
1150 || 0 > (rd = read(c->file.fd, data, weHave))) {
1151 log_error_write(srv, __FILE__, __LINE__, "ssbd", "lseek/read failed: ",
1152 strerror(errno), c->file.name, c->file.fd);
1153 free(data);
1154 return -1;
1156 weHave = (size_t)rd;
1160 if (XML_ERR_OK != (err = xmlParseChunk(ctxt, data, weHave, 0))) {
1161 log_error_write(srv, __FILE__, __LINE__, "sodd", "xmlParseChunk failed at:", cq->bytes_out, weHave, err);
1164 chunkqueue_mark_written(cq, weHave);
1166 if (!mapped) free(data);
1167 break;
1168 case MEM_CHUNK:
1169 /* append to the buffer */
1170 weHave = buffer_string_length(c->mem) - c->offset;
1172 if (weHave > weWant) weHave = weWant;
1174 if (hctx->conf.log_xml) {
1175 log_error_write(srv, __FILE__, __LINE__, "ss", "XML-request-body:", c->mem->ptr + c->offset);
1178 if (XML_ERR_OK != (err = xmlParseChunk(ctxt, c->mem->ptr + c->offset, weHave, 0))) {
1179 log_error_write(srv, __FILE__, __LINE__, "sodd", "xmlParseChunk failed at:", cq->bytes_out, weHave, err);
1182 chunkqueue_mark_written(cq, weHave);
1184 break;
1188 switch ((err = xmlParseChunk(ctxt, 0, 0, 1))) {
1189 case XML_ERR_DOCUMENT_END:
1190 case XML_ERR_OK:
1191 break;
1192 default:
1193 log_error_write(srv, __FILE__, __LINE__, "sd", "xmlParseChunk failed at final packet:", err);
1194 break;
1197 xml = ctxt->myDoc;
1198 res = ctxt->wellFormed;
1199 xmlFreeParserCtxt(ctxt);
1201 if (res == 0) {
1202 xmlFreeDoc(xml);
1203 } else {
1204 *ret_xml = xml;
1207 return res;
1209 #endif
1211 #ifdef USE_LOCKS
1212 static int webdav_lockdiscovery(server *srv, connection *con,
1213 buffer *locktoken, const char *lockscope, const char *locktype, int depth) {
1215 buffer *b = buffer_init();
1217 response_header_overwrite(srv, con, CONST_STR_LEN("Lock-Token"), CONST_BUF_LEN(locktoken));
1219 response_header_overwrite(srv, con,
1220 CONST_STR_LEN("Content-Type"),
1221 CONST_STR_LEN("text/xml; charset=\"utf-8\""));
1223 buffer_copy_string_len(b, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"));
1225 buffer_append_string_len(b,CONST_STR_LEN("<D:prop xmlns:D=\"DAV:\" xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\">\n"));
1226 buffer_append_string_len(b,CONST_STR_LEN("<D:lockdiscovery>\n"));
1227 webdav_activelock(b, locktoken, lockscope, locktype, depth, 600);
1228 buffer_append_string_len(b,CONST_STR_LEN("</D:lockdiscovery>\n"));
1229 buffer_append_string_len(b,CONST_STR_LEN("</D:prop>\n"));
1231 chunkqueue_append_buffer(con->write_queue, b);
1232 buffer_free(b);
1234 return 0;
1236 #endif
1239 * check if resource is having the right locks to access to resource
1244 static int webdav_has_lock(server *srv, connection *con, handler_ctx *hctx, buffer *uri) {
1245 int has_lock = 1;
1247 #ifdef USE_LOCKS
1248 data_string *ds;
1249 UNUSED(srv);
1252 * This implementation is more fake than real
1253 * we need a parser for the If: header to really handle the full scope
1255 * X-Litmus: locks: 11 (owner_modify)
1256 * If: <http://127.0.0.1:1025/dav/litmus/lockme> (<opaquelocktoken:2165478d-0611-49c4-be92-e790d68a38f1>)
1257 * - a tagged check:
1258 * if http://127.0.0.1:1025/dav/litmus/lockme is locked with
1259 * opaquelocktoken:2165478d-0611-49c4-be92-e790d68a38f1, go on
1261 * X-Litmus: locks: 16 (fail_cond_put)
1262 * If: (<DAV:no-lock> ["-1622396671"])
1263 * - untagged:
1264 * go on if the resource has the etag [...] and the lock
1266 if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "If"))) {
1267 /* Ooh, ooh. A if tag, now the fun begins.
1269 * this can only work with a real parser
1271 } else {
1272 /* we didn't provided a lock-token -> */
1273 /* if the resource is locked -> 423 */
1275 sqlite3_stmt *stmt = hctx->conf.stmt_read_lock_by_uri;
1277 sqlite3_reset(stmt);
1279 sqlite3_bind_text(stmt, 1,
1280 CONST_BUF_LEN(uri),
1281 SQLITE_TRANSIENT);
1283 while (SQLITE_ROW == sqlite3_step(stmt)) {
1284 has_lock = 0;
1287 #else
1288 UNUSED(srv);
1289 UNUSED(con);
1290 UNUSED(hctx);
1291 UNUSED(uri);
1292 #endif
1294 return has_lock;
1298 SUBREQUEST_FUNC(mod_webdav_subrequest_handler_huge) {
1299 plugin_data *p = p_d;
1300 handler_ctx *hctx = con->plugin_ctx[p->id];
1301 buffer *b;
1302 DIR *dir;
1303 data_string *ds;
1304 int depth = -1; /* (Depth: infinity) */
1305 struct stat st;
1306 buffer *prop_200;
1307 buffer *prop_404;
1308 webdav_properties *req_props;
1309 stat_cache_entry *sce = NULL;
1311 UNUSED(srv);
1313 if (NULL == hctx) return HANDLER_GO_ON;
1314 if (!hctx->conf.enabled) return HANDLER_GO_ON;
1315 /* physical path is setup */
1316 if (buffer_is_empty(con->physical.path)) return HANDLER_GO_ON;
1318 /* PROPFIND need them */
1319 if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "Depth")) && 1 == buffer_string_length(ds->value)) {
1320 if ('0' == *ds->value->ptr) {
1321 depth = 0;
1322 } else if ('1' == *ds->value->ptr) {
1323 depth = 1;
1325 } /* else treat as Depth: infinity */
1327 switch (con->request.http_method) {
1328 case HTTP_METHOD_PROPFIND:
1329 /* they want to know the properties of the directory */
1330 req_props = NULL;
1332 /* is there a content-body ? */
1334 switch (stat_cache_get_entry(srv, con, con->physical.path, &sce)) {
1335 case HANDLER_ERROR:
1336 if (errno == ENOENT) {
1337 con->http_status = 404;
1338 return HANDLER_FINISHED;
1340 break;
1341 default:
1342 break;
1345 if (S_ISDIR(sce->st.st_mode) && con->physical.path->ptr[buffer_string_length(con->physical.path)-1] != '/') {
1346 http_response_redirect_to_directory(srv, con);
1347 return HANDLER_FINISHED;
1350 #ifdef USE_PROPPATCH
1351 /* any special requests or just allprop ? */
1352 if (con->request.content_length) {
1353 xmlDocPtr xml;
1355 if (con->state == CON_STATE_READ_POST) {
1356 handler_t r = connection_handle_read_post_state(srv, con);
1357 if (r != HANDLER_GO_ON) return r;
1360 if (1 == webdav_parse_chunkqueue(srv, con, hctx, con->request_content_queue, &xml)) {
1361 xmlNode *rootnode = xmlDocGetRootElement(xml);
1363 force_assert(rootnode);
1365 if (0 == xmlStrcmp(rootnode->name, BAD_CAST "propfind")) {
1366 xmlNode *cmd;
1368 req_props = calloc(1, sizeof(*req_props));
1370 for (cmd = rootnode->children; cmd; cmd = cmd->next) {
1372 if (0 == xmlStrcmp(cmd->name, BAD_CAST "prop")) {
1373 /* get prop by name */
1374 xmlNode *prop;
1376 for (prop = cmd->children; prop; prop = prop->next) {
1377 if (prop->type == XML_TEXT_NODE) continue; /* ignore WS */
1379 if (prop->ns &&
1380 (0 == xmlStrcmp(prop->ns->href, BAD_CAST "")) &&
1381 (0 != xmlStrcmp(prop->ns->prefix, BAD_CAST ""))) {
1382 size_t i;
1383 log_error_write(srv, __FILE__, __LINE__, "ss",
1384 "no name space for:",
1385 prop->name);
1387 xmlFreeDoc(xml);
1389 for (i = 0; i < req_props->used; i++) {
1390 free(req_props->ptr[i]->ns);
1391 free(req_props->ptr[i]->prop);
1392 free(req_props->ptr[i]);
1394 free(req_props->ptr);
1395 free(req_props);
1397 con->http_status = 400;
1398 return HANDLER_FINISHED;
1401 /* add property to requested list */
1402 if (req_props->size == 0) {
1403 req_props->size = 16;
1404 req_props->ptr = malloc(sizeof(*(req_props->ptr)) * req_props->size);
1405 } else if (req_props->used == req_props->size) {
1406 req_props->size += 16;
1407 req_props->ptr = realloc(req_props->ptr, sizeof(*(req_props->ptr)) * req_props->size);
1410 req_props->ptr[req_props->used] = malloc(sizeof(webdav_property));
1411 req_props->ptr[req_props->used]->ns = (char *)xmlStrdup(prop->ns ? prop->ns->href : (xmlChar *)"");
1412 req_props->ptr[req_props->used]->prop = (char *)xmlStrdup(prop->name);
1413 req_props->used++;
1415 } else if (0 == xmlStrcmp(cmd->name, BAD_CAST "propname")) {
1416 sqlite3_stmt *stmt = p->conf.stmt_select_propnames;
1418 if (stmt) {
1419 /* get all property names (EMPTY) */
1420 sqlite3_reset(stmt);
1421 /* bind the values to the insert */
1423 sqlite3_bind_text(stmt, 1,
1424 CONST_BUF_LEN(con->uri.path),
1425 SQLITE_TRANSIENT);
1427 if (SQLITE_DONE != sqlite3_step(stmt)) {
1430 } else if (0 == xmlStrcmp(cmd->name, BAD_CAST "allprop")) {
1431 /* get all properties (EMPTY) */
1436 xmlFreeDoc(xml);
1437 } else {
1438 con->http_status = 400;
1439 return HANDLER_FINISHED;
1442 #endif
1443 con->http_status = 207;
1445 response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/xml; charset=\"utf-8\""));
1447 b = buffer_init();
1449 buffer_copy_string_len(b, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"));
1451 buffer_append_string_len(b,CONST_STR_LEN("<D:multistatus xmlns:D=\"DAV:\" xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\">\n"));
1453 /* allprop */
1455 prop_200 = buffer_init();
1456 prop_404 = buffer_init();
1459 /* Depth: 0 or Depth: 1 */
1460 webdav_get_props(srv, con, hctx, &(con->physical), req_props, prop_200, prop_404);
1462 buffer_append_string_len(b,CONST_STR_LEN("<D:response>\n"));
1463 buffer_append_string_len(b,CONST_STR_LEN("<D:href>"));
1464 buffer_append_string_buffer(b, con->uri.scheme);
1465 buffer_append_string_len(b,CONST_STR_LEN("://"));
1466 buffer_append_string_buffer(b, con->uri.authority);
1467 buffer_append_string_encoded(b, CONST_BUF_LEN(con->uri.path), ENCODING_REL_URI);
1468 buffer_append_string_len(b,CONST_STR_LEN("</D:href>\n"));
1470 if (!buffer_string_is_empty(prop_200)) {
1471 buffer_append_string_len(b,CONST_STR_LEN("<D:propstat>\n"));
1472 buffer_append_string_len(b,CONST_STR_LEN("<D:prop>\n"));
1474 buffer_append_string_buffer(b, prop_200);
1476 buffer_append_string_len(b,CONST_STR_LEN("</D:prop>\n"));
1478 buffer_append_string_len(b,CONST_STR_LEN("<D:status>HTTP/1.1 200 OK</D:status>\n"));
1480 buffer_append_string_len(b,CONST_STR_LEN("</D:propstat>\n"));
1482 if (!buffer_string_is_empty(prop_404)) {
1483 buffer_append_string_len(b,CONST_STR_LEN("<D:propstat>\n"));
1484 buffer_append_string_len(b,CONST_STR_LEN("<D:prop>\n"));
1486 buffer_append_string_buffer(b, prop_404);
1488 buffer_append_string_len(b,CONST_STR_LEN("</D:prop>\n"));
1490 buffer_append_string_len(b,CONST_STR_LEN("<D:status>HTTP/1.1 404 Not Found</D:status>\n"));
1492 buffer_append_string_len(b,CONST_STR_LEN("</D:propstat>\n"));
1495 buffer_append_string_len(b,CONST_STR_LEN("</D:response>\n"));
1498 if (depth == 1) {
1500 if (NULL != (dir = opendir(con->physical.path->ptr))) {
1501 struct dirent *de;
1502 physical d;
1503 physical *dst = &(con->physical);
1505 d.path = buffer_init();
1506 d.rel_path = buffer_init();
1508 while(NULL != (de = readdir(dir))) {
1509 if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || (de->d_name[1] == '.' && de->d_name[2] == '\0'))) {
1510 continue;
1511 /* ignore the parent and target dir */
1514 buffer_copy_buffer(d.path, dst->path);
1515 buffer_append_slash(d.path);
1517 buffer_copy_buffer(d.rel_path, dst->rel_path);
1518 buffer_append_slash(d.rel_path);
1520 buffer_append_string(d.path, de->d_name);
1521 buffer_append_string(d.rel_path, de->d_name);
1523 buffer_reset(prop_200);
1524 buffer_reset(prop_404);
1526 webdav_get_props(srv, con, hctx, &d, req_props, prop_200, prop_404);
1528 buffer_append_string_len(b,CONST_STR_LEN("<D:response>\n"));
1529 buffer_append_string_len(b,CONST_STR_LEN("<D:href>"));
1530 buffer_append_string_buffer(b, con->uri.scheme);
1531 buffer_append_string_len(b,CONST_STR_LEN("://"));
1532 buffer_append_string_buffer(b, con->uri.authority);
1533 buffer_append_string_encoded(b, CONST_BUF_LEN(d.rel_path), ENCODING_REL_URI);
1534 if (0 == stat(d.path->ptr, &st) && S_ISDIR(st.st_mode)) {
1535 /* Append a '/' on subdirectories */
1536 buffer_append_string_len(b,CONST_STR_LEN("/"));
1538 buffer_append_string_len(b,CONST_STR_LEN("</D:href>\n"));
1540 if (!buffer_string_is_empty(prop_200)) {
1541 buffer_append_string_len(b,CONST_STR_LEN("<D:propstat>\n"));
1542 buffer_append_string_len(b,CONST_STR_LEN("<D:prop>\n"));
1544 buffer_append_string_buffer(b, prop_200);
1546 buffer_append_string_len(b,CONST_STR_LEN("</D:prop>\n"));
1548 buffer_append_string_len(b,CONST_STR_LEN("<D:status>HTTP/1.1 200 OK</D:status>\n"));
1550 buffer_append_string_len(b,CONST_STR_LEN("</D:propstat>\n"));
1552 if (!buffer_string_is_empty(prop_404)) {
1553 buffer_append_string_len(b,CONST_STR_LEN("<D:propstat>\n"));
1554 buffer_append_string_len(b,CONST_STR_LEN("<D:prop>\n"));
1556 buffer_append_string_buffer(b, prop_404);
1558 buffer_append_string_len(b,CONST_STR_LEN("</D:prop>\n"));
1560 buffer_append_string_len(b,CONST_STR_LEN("<D:status>HTTP/1.1 404 Not Found</D:status>\n"));
1562 buffer_append_string_len(b,CONST_STR_LEN("</D:propstat>\n"));
1565 buffer_append_string_len(b,CONST_STR_LEN("</D:response>\n"));
1567 closedir(dir);
1568 buffer_free(d.path);
1569 buffer_free(d.rel_path);
1574 if (req_props) {
1575 size_t i;
1576 for (i = 0; i < req_props->used; i++) {
1577 free(req_props->ptr[i]->ns);
1578 free(req_props->ptr[i]->prop);
1579 free(req_props->ptr[i]);
1581 free(req_props->ptr);
1582 free(req_props);
1585 buffer_free(prop_200);
1586 buffer_free(prop_404);
1588 buffer_append_string_len(b,CONST_STR_LEN("</D:multistatus>\n"));
1590 if (p->conf.log_xml) {
1591 log_error_write(srv, __FILE__, __LINE__, "sb", "XML-response-body:", b);
1594 chunkqueue_append_buffer(con->write_queue, b);
1595 buffer_free(b);
1597 con->file_finished = 1;
1599 return HANDLER_FINISHED;
1600 case HTTP_METHOD_MKCOL:
1601 if (p->conf.is_readonly) {
1602 con->http_status = 403;
1603 return HANDLER_FINISHED;
1606 if (con->request.content_length != 0) {
1607 /* we don't support MKCOL with a body */
1608 con->http_status = 415;
1610 return HANDLER_FINISHED;
1613 /* let's create the directory */
1615 if (-1 == mkdir(con->physical.path->ptr, WEBDAV_DIR_MODE)) {
1616 switch(errno) {
1617 case EPERM:
1618 con->http_status = 403;
1619 break;
1620 case ENOENT:
1621 case ENOTDIR:
1622 con->http_status = 409;
1623 break;
1624 case EEXIST:
1625 default:
1626 con->http_status = 405; /* not allowed */
1627 break;
1629 } else {
1630 con->http_status = 201;
1631 con->file_finished = 1;
1634 return HANDLER_FINISHED;
1635 case HTTP_METHOD_DELETE:
1636 if (p->conf.is_readonly) {
1637 con->http_status = 403;
1638 return HANDLER_FINISHED;
1641 /* does the client have a lock for this connection ? */
1642 if (!webdav_has_lock(srv, con, hctx, con->uri.path)) {
1643 con->http_status = 423;
1644 return HANDLER_FINISHED;
1647 /* stat and unlink afterwards */
1648 if (-1 == stat(con->physical.path->ptr, &st)) {
1649 /* don't about it yet, unlink will fail too */
1650 switch(errno) {
1651 case ENOENT:
1652 con->http_status = 404;
1653 break;
1654 default:
1655 con->http_status = 403;
1656 break;
1658 } else if (S_ISDIR(st.st_mode)) {
1659 buffer *multi_status_resp;
1661 if (con->physical.path->ptr[buffer_string_length(con->physical.path)-1] != '/') {
1662 http_response_redirect_to_directory(srv, con);
1663 return HANDLER_FINISHED;
1666 multi_status_resp = buffer_init();
1668 if (webdav_delete_dir(srv, con, hctx, &(con->physical), multi_status_resp)) {
1669 /* we got an error somewhere in between, build a 207 */
1670 response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/xml; charset=\"utf-8\""));
1672 b = buffer_init();
1674 buffer_copy_string_len(b, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"));
1676 buffer_append_string_len(b,CONST_STR_LEN("<D:multistatus xmlns:D=\"DAV:\">\n"));
1678 buffer_append_string_buffer(b, multi_status_resp);
1680 buffer_append_string_len(b,CONST_STR_LEN("</D:multistatus>\n"));
1682 if (p->conf.log_xml) {
1683 log_error_write(srv, __FILE__, __LINE__, "sb", "XML-response-body:", b);
1686 chunkqueue_append_buffer(con->write_queue, b);
1687 buffer_free(b);
1689 con->http_status = 207;
1690 con->file_finished = 1;
1691 } else {
1692 /* everything went fine, remove the directory */
1694 if (-1 == rmdir(con->physical.path->ptr)) {
1695 switch(errno) {
1696 case EPERM:
1697 con->http_status = 403;
1698 break;
1699 case ENOENT:
1700 con->http_status = 404;
1701 break;
1702 default:
1703 con->http_status = 501;
1704 break;
1706 } else {
1707 con->http_status = 204;
1711 buffer_free(multi_status_resp);
1712 } else if (-1 == unlink(con->physical.path->ptr)) {
1713 switch(errno) {
1714 case EPERM:
1715 con->http_status = 403;
1716 break;
1717 case ENOENT:
1718 con->http_status = 404;
1719 break;
1720 default:
1721 con->http_status = 501;
1722 break;
1724 } else {
1725 con->http_status = 204;
1727 return HANDLER_FINISHED;
1728 case HTTP_METHOD_PUT: {
1729 int fd;
1730 chunkqueue *cq = con->request_content_queue;
1731 chunk *c;
1732 data_string *ds_range;
1734 if (p->conf.is_readonly) {
1735 con->http_status = 403;
1736 return HANDLER_FINISHED;
1739 /* is a exclusive lock set on the source */
1740 /* (check for lock once before potentially reading large input) */
1741 if (0 == cq->bytes_in && !webdav_has_lock(srv, con, hctx, con->uri.path)) {
1742 con->http_status = 423;
1743 return HANDLER_FINISHED;
1746 if (con->state == CON_STATE_READ_POST) {
1747 handler_t r = connection_handle_read_post_state(srv, con);
1748 if (r != HANDLER_GO_ON) return r;
1751 /* RFC2616 Section 9.6 PUT requires us to send 501 on all Content-* we don't support
1752 * - most important Content-Range
1755 * Example: Content-Range: bytes 100-1037/1038 */
1757 if (NULL != (ds_range = (data_string *)array_get_element(con->request.headers, "Content-Range"))) {
1758 const char *num = ds_range->value->ptr;
1759 off_t offset;
1760 char *err = NULL;
1762 if (0 != strncmp(num, "bytes ", 6)) {
1763 con->http_status = 501; /* not implemented */
1765 return HANDLER_FINISHED;
1768 /* we only support <num>- ... */
1770 num += 6;
1772 /* skip WS */
1773 while (*num == ' ' || *num == '\t') num++;
1775 if (*num == '\0') {
1776 con->http_status = 501; /* not implemented */
1778 return HANDLER_FINISHED;
1781 offset = strtoll(num, &err, 10);
1783 if (*err != '-' || offset < 0) {
1784 con->http_status = 501; /* not implemented */
1786 return HANDLER_FINISHED;
1789 if (-1 == (fd = open(con->physical.path->ptr, O_WRONLY, WEBDAV_FILE_MODE))) {
1790 switch (errno) {
1791 case ENOENT:
1792 con->http_status = 404; /* not found */
1793 break;
1794 default:
1795 con->http_status = 403; /* not found */
1796 break;
1798 return HANDLER_FINISHED;
1801 if (-1 == lseek(fd, offset, SEEK_SET)) {
1802 con->http_status = 501; /* not implemented */
1804 close(fd);
1806 return HANDLER_FINISHED;
1808 con->http_status = 200; /* modified */
1809 } else {
1810 /* take what we have in the request-body and write it to a file */
1812 /* if the file doesn't exist, create it */
1813 if (-1 == (fd = open(con->physical.path->ptr, O_WRONLY|O_TRUNC, WEBDAV_FILE_MODE))) {
1814 if (errno != ENOENT ||
1815 -1 == (fd = open(con->physical.path->ptr, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, WEBDAV_FILE_MODE))) {
1816 /* we can't open the file */
1817 con->http_status = 403;
1819 return HANDLER_FINISHED;
1820 } else {
1821 con->http_status = 201; /* created */
1823 } else {
1824 con->http_status = 200; /* modified */
1828 con->file_finished = 1;
1830 for (c = cq->first; c; c = cq->first) {
1831 int r = 0;
1832 int mapped;
1833 void *data;
1834 size_t dlen;
1836 /* copy all chunks */
1837 switch(c->type) {
1838 case FILE_CHUNK:
1840 mapped = (c->file.mmap.start != MAP_FAILED);
1841 dlen = c->file.length - c->offset;
1842 if (mapped) {
1843 data = c->file.mmap.start + c->offset;
1844 } else {
1845 if (-1 == c->file.fd && /* open the file if not already open */
1846 -1 == (c->file.fd = open(c->file.name->ptr, O_RDONLY))) {
1847 log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno));
1848 close(fd);
1849 return HANDLER_ERROR;
1852 if (MAP_FAILED != (c->file.mmap.start = mmap(NULL, c->file.length, PROT_READ, MAP_PRIVATE, c->file.fd, 0))) {
1853 /* chunk_reset() or chunk_free() will cleanup for us */
1854 c->file.mmap.length = c->file.length;
1855 data = c->file.mmap.start + c->offset;
1856 mapped = 1;
1857 } else {
1858 ssize_t rd;
1859 if (dlen > 65536) dlen = 65536;
1860 data = malloc(dlen);
1861 force_assert(data);
1862 if (-1 == lseek(c->file.fd, c->file.start + c->offset, SEEK_SET)
1863 || 0 > (rd = read(c->file.fd, data, dlen))) {
1864 log_error_write(srv, __FILE__, __LINE__, "ssbd", "lseek/read failed: ",
1865 strerror(errno), c->file.name, c->file.fd);
1866 free(data);
1867 close(fd);
1868 return HANDLER_ERROR;
1870 dlen = (size_t)rd;
1875 if ((r = write(fd, data, dlen)) < 0) {
1876 switch(errno) {
1877 case ENOSPC:
1878 con->http_status = 507;
1880 break;
1881 default:
1882 con->http_status = 403;
1883 break;
1887 if (!mapped) free(data);
1888 break;
1889 case MEM_CHUNK:
1890 if ((r = write(fd, c->mem->ptr + c->offset, buffer_string_length(c->mem) - c->offset)) < 0) {
1891 switch(errno) {
1892 case ENOSPC:
1893 con->http_status = 507;
1895 break;
1896 default:
1897 con->http_status = 403;
1898 break;
1901 break;
1904 if (r > 0) {
1905 chunkqueue_mark_written(cq, r);
1906 } else {
1907 break;
1910 if (0 != close(fd)) {
1911 log_error_write(srv, __FILE__, __LINE__, "sbss",
1912 "close ", con->physical.path, "failed: ", strerror(errno));
1913 return HANDLER_ERROR;
1916 return HANDLER_FINISHED;
1918 case HTTP_METHOD_MOVE:
1919 case HTTP_METHOD_COPY: {
1920 buffer *destination = NULL;
1921 char *sep, *sep2, *start;
1922 int overwrite = 1;
1924 if (p->conf.is_readonly) {
1925 con->http_status = 403;
1926 return HANDLER_FINISHED;
1929 /* is a exclusive lock set on the source */
1930 if (con->request.http_method == HTTP_METHOD_MOVE) {
1931 if (!webdav_has_lock(srv, con, hctx, con->uri.path)) {
1932 con->http_status = 423;
1933 return HANDLER_FINISHED;
1937 if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "Destination"))) {
1938 destination = ds->value;
1939 } else {
1940 con->http_status = 400;
1941 return HANDLER_FINISHED;
1944 if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "Overwrite"))) {
1945 if (buffer_string_length(ds->value) != 1 ||
1946 (ds->value->ptr[0] != 'F' &&
1947 ds->value->ptr[0] != 'T') ) {
1948 con->http_status = 400;
1949 return HANDLER_FINISHED;
1951 overwrite = (ds->value->ptr[0] == 'F' ? 0 : 1);
1953 /* let's parse the Destination
1955 * http://127.0.0.1:1025/dav/litmus/copydest
1957 * - host has to be the same as the Host: header we got
1958 * - we have to stay inside the document root
1959 * - the query string is thrown away
1960 * */
1962 buffer_reset(p->uri.scheme);
1963 buffer_reset(p->uri.path_raw);
1964 buffer_reset(p->uri.authority);
1966 start = destination->ptr;
1968 if (NULL == (sep = strstr(start, "://"))) {
1969 con->http_status = 400;
1970 return HANDLER_FINISHED;
1972 buffer_copy_string_len(p->uri.scheme, start, sep - start);
1974 start = sep + 3;
1976 if (NULL == (sep = strchr(start, '/'))) {
1977 con->http_status = 400;
1978 return HANDLER_FINISHED;
1980 if (NULL != (sep2 = memchr(start, '@', sep - start))) {
1981 /* skip login information */
1982 start = sep2 + 1;
1984 buffer_copy_string_len(p->uri.authority, start, sep - start);
1986 start = sep + 1;
1988 if (NULL == (sep = strchr(start, '?'))) {
1989 /* no query string, good */
1990 buffer_copy_string(p->uri.path_raw, start);
1991 } else {
1992 buffer_copy_string_len(p->uri.path_raw, start, sep - start);
1995 if (!buffer_is_equal(p->uri.authority, con->uri.authority)) {
1996 /* not the same host */
1997 con->http_status = 502;
1998 return HANDLER_FINISHED;
2001 buffer_copy_buffer(p->tmp_buf, p->uri.path_raw);
2002 buffer_urldecode_path(p->tmp_buf);
2003 buffer_path_simplify(p->uri.path, p->tmp_buf);
2005 /* we now have a URI which is clean. transform it into a physical path */
2006 buffer_copy_buffer(p->physical.doc_root, con->physical.doc_root);
2007 buffer_copy_buffer(p->physical.rel_path, p->uri.path);
2009 if (con->conf.force_lowercase_filenames) {
2010 buffer_to_lower(p->physical.rel_path);
2013 /* Destination physical path
2014 * src con->physical.path might have been remapped with mod_alias.
2015 * (but mod_alias does not modify con->physical.rel_path)
2016 * Find matching prefix to support use of mod_alias to remap webdav root.
2017 * Aliasing of paths underneath the webdav root might not work.
2018 * Likewise, mod_rewrite URL rewriting might thwart this comparison.
2019 * Use mod_redirect instead of mod_alias to remap paths *under* webdav root.
2020 * Use mod_redirect instead of mod_rewrite on *any* parts of path to webdav.
2021 * (Related, use mod_auth to protect webdav root, but avoid attempting to
2022 * use mod_auth on paths underneath webdav root, as Destination is not
2023 * validated with mod_auth)
2025 * tl;dr: webdav paths and webdav properties are managed by mod_webdav,
2026 * so do not modify paths externally or else undefined behavior
2027 * or corruption may occur
2030 /* find matching URI prefix
2031 * check if remaining con->physical.rel_path matches suffix
2032 * of con->physical.basedir so that we can use it to
2033 * remap Destination physical path */
2034 size_t i, remain;
2035 sep = con->uri.path->ptr;
2036 sep2 = p->uri.path->ptr;
2037 for (i = 0; sep[i] && sep[i] == sep2[i]; ++i) ;
2038 if (sep[i] == '\0' && (sep2[i] == '\0' || sep2[i] == '/' || (i > 0 && sep[i-1] == '/'))) {
2039 /* src and dst URI match or dst is nested inside src; invalid COPY or MOVE */
2040 con->http_status = 403;
2041 return HANDLER_FINISHED;
2043 while (i != 0 && sep[--i] != '/') ; /* find matching directory path */
2044 remain = buffer_string_length(con->uri.path) - i;
2045 if (!con->conf.force_lowercase_filenames
2046 ? buffer_is_equal_right_len(con->physical.path, con->physical.rel_path, remain)
2047 :(buffer_string_length(con->physical.path) >= remain
2048 && 0 == strncasecmp(con->physical.path->ptr+buffer_string_length(con->physical.path)-remain, con->physical.rel_path->ptr+i, remain))) {
2049 /* (at this point, p->physical.rel_path is identical to (or lowercased version of) p->uri.path) */
2050 buffer_copy_string_len(p->physical.path, con->physical.path->ptr, buffer_string_length(con->physical.path)-remain);
2051 buffer_append_string_len(p->physical.path, p->physical.rel_path->ptr+i, buffer_string_length(p->physical.rel_path)-i);
2053 buffer_copy_buffer(p->physical.basedir, con->physical.basedir);
2054 buffer_append_slash(p->physical.basedir);
2055 } else {
2056 /* unable to perform physical path remap here;
2057 * assume doc_root/rel_path and no remapping */
2058 buffer_copy_buffer(p->physical.path, p->physical.doc_root);
2059 buffer_append_slash(p->physical.path);
2060 buffer_copy_buffer(p->physical.basedir, p->physical.path);
2062 /* don't add a second / */
2063 if (p->physical.rel_path->ptr[0] == '/') {
2064 buffer_append_string_len(p->physical.path, p->physical.rel_path->ptr + 1, buffer_string_length(p->physical.rel_path) - 1);
2065 } else {
2066 buffer_append_string_buffer(p->physical.path, p->physical.rel_path);
2071 /* let's see if the source is a directory
2072 * if yes, we fail with 501 */
2074 if (-1 == stat(con->physical.path->ptr, &st)) {
2075 /* don't about it yet, unlink will fail too */
2076 switch(errno) {
2077 case ENOENT:
2078 con->http_status = 404;
2079 break;
2080 default:
2081 con->http_status = 403;
2082 break;
2084 } else if (S_ISDIR(st.st_mode)) {
2085 int r;
2086 int created = 0;
2087 /* src is a directory */
2089 if (con->physical.path->ptr[buffer_string_length(con->physical.path)-1] != '/') {
2090 http_response_redirect_to_directory(srv, con);
2091 return HANDLER_FINISHED;
2094 if (-1 == stat(p->physical.path->ptr, &st)) {
2095 if (-1 == mkdir(p->physical.path->ptr, WEBDAV_DIR_MODE)) {
2096 con->http_status = 403;
2097 return HANDLER_FINISHED;
2099 created = 1;
2100 } else if (!S_ISDIR(st.st_mode)) {
2101 if (overwrite == 0) {
2102 /* copying into a non-dir ? */
2103 con->http_status = 409;
2104 return HANDLER_FINISHED;
2105 } else {
2106 unlink(p->physical.path->ptr);
2107 if (-1 == mkdir(p->physical.path->ptr, WEBDAV_DIR_MODE)) {
2108 con->http_status = 403;
2109 return HANDLER_FINISHED;
2111 created = 1;
2115 /* copy the content of src to dest */
2116 if (0 != (r = webdav_copy_dir(srv, con, hctx, &(con->physical), &(p->physical), overwrite))) {
2117 con->http_status = r;
2118 return HANDLER_FINISHED;
2120 if (con->request.http_method == HTTP_METHOD_MOVE) {
2121 b = buffer_init();
2122 webdav_delete_dir(srv, con, hctx, &(con->physical), b); /* content */
2123 buffer_free(b);
2125 rmdir(con->physical.path->ptr);
2127 con->http_status = created ? 201 : 204;
2128 con->file_finished = 1;
2129 } else {
2130 /* it is just a file, good */
2131 int r;
2132 int destdir = 0;
2134 /* does the client have a lock for this connection ? */
2135 if (!webdav_has_lock(srv, con, hctx, p->uri.path)) {
2136 con->http_status = 423;
2137 return HANDLER_FINISHED;
2140 /* destination exists */
2141 if (0 == (r = stat(p->physical.path->ptr, &st))) {
2142 if (S_ISDIR(st.st_mode)) {
2143 /* file to dir/
2144 * append basename to physical path */
2145 destdir = 1;
2147 if (NULL != (sep = strrchr(con->physical.path->ptr, '/'))) {
2148 buffer_append_string(p->physical.path, sep);
2149 r = stat(p->physical.path->ptr, &st);
2154 if (-1 == r) {
2155 con->http_status = destdir ? 204 : 201; /* we will create a new one */
2156 con->file_finished = 1;
2158 switch(errno) {
2159 case ENOTDIR:
2160 con->http_status = 409;
2161 return HANDLER_FINISHED;
2163 } else if (overwrite == 0) {
2164 /* destination exists, but overwrite is not set */
2165 con->http_status = 412;
2166 return HANDLER_FINISHED;
2167 } else {
2168 con->http_status = 204; /* resource already existed */
2171 if (con->request.http_method == HTTP_METHOD_MOVE) {
2172 /* try a rename */
2174 if (0 == rename(con->physical.path->ptr, p->physical.path->ptr)) {
2175 #ifdef USE_PROPPATCH
2176 sqlite3_stmt *stmt;
2178 stmt = p->conf.stmt_move_uri;
2179 if (stmt) {
2181 sqlite3_reset(stmt);
2183 /* bind the values to the insert */
2184 sqlite3_bind_text(stmt, 1,
2185 CONST_BUF_LEN(p->uri.path),
2186 SQLITE_TRANSIENT);
2188 sqlite3_bind_text(stmt, 2,
2189 CONST_BUF_LEN(con->uri.path),
2190 SQLITE_TRANSIENT);
2192 if (SQLITE_DONE != sqlite3_step(stmt)) {
2193 log_error_write(srv, __FILE__, __LINE__, "ss", "sql-move failed:", sqlite3_errmsg(p->conf.sql));
2196 #endif
2197 return HANDLER_FINISHED;
2200 /* rename failed, fall back to COPY + DELETE */
2203 if (0 != (r = webdav_copy_file(srv, con, hctx, &(con->physical), &(p->physical), overwrite))) {
2204 con->http_status = r;
2206 return HANDLER_FINISHED;
2209 if (con->request.http_method == HTTP_METHOD_MOVE) {
2210 b = buffer_init();
2211 webdav_delete_file(srv, con, hctx, &(con->physical), b);
2212 buffer_free(b);
2216 return HANDLER_FINISHED;
2218 case HTTP_METHOD_PROPPATCH:
2219 if (p->conf.is_readonly) {
2220 con->http_status = 403;
2221 return HANDLER_FINISHED;
2224 if (!webdav_has_lock(srv, con, hctx, con->uri.path)) {
2225 con->http_status = 423;
2226 return HANDLER_FINISHED;
2229 /* check if destination exists */
2230 if (-1 == stat(con->physical.path->ptr, &st)) {
2231 switch(errno) {
2232 case ENOENT:
2233 con->http_status = 404;
2234 break;
2238 if (S_ISDIR(st.st_mode) && con->physical.path->ptr[buffer_string_length(con->physical.path)-1] != '/') {
2239 http_response_redirect_to_directory(srv, con);
2240 return HANDLER_FINISHED;
2243 #ifdef USE_PROPPATCH
2244 if (con->request.content_length) {
2245 xmlDocPtr xml;
2247 if (con->state == CON_STATE_READ_POST) {
2248 handler_t r = connection_handle_read_post_state(srv, con);
2249 if (r != HANDLER_GO_ON) return r;
2252 if (1 == webdav_parse_chunkqueue(srv, con, hctx, con->request_content_queue, &xml)) {
2253 xmlNode *rootnode = xmlDocGetRootElement(xml);
2255 if (0 == xmlStrcmp(rootnode->name, BAD_CAST "propertyupdate")) {
2256 xmlNode *cmd;
2257 char *err = NULL;
2258 int empty_ns = 0; /* send 400 on a empty namespace attribute */
2260 /* start response */
2262 if (SQLITE_OK != sqlite3_exec(p->conf.sql, "BEGIN TRANSACTION", NULL, NULL, &err)) {
2263 log_error_write(srv, __FILE__, __LINE__, "ss", "can't open transaction:", err);
2264 sqlite3_free(err);
2266 goto propmatch_cleanup;
2269 /* a UPDATE request, we know 'set' and 'remove' */
2270 for (cmd = rootnode->children; cmd; cmd = cmd->next) {
2271 xmlNode *props;
2272 /* either set or remove */
2274 if ((0 == xmlStrcmp(cmd->name, BAD_CAST "set")) ||
2275 (0 == xmlStrcmp(cmd->name, BAD_CAST "remove"))) {
2277 sqlite3_stmt *stmt;
2279 stmt = (0 == xmlStrcmp(cmd->name, BAD_CAST "remove")) ?
2280 p->conf.stmt_delete_prop : p->conf.stmt_update_prop;
2282 for (props = cmd->children; props; props = props->next) {
2283 if (0 == xmlStrcmp(props->name, BAD_CAST "prop")) {
2284 xmlNode *prop;
2285 char *propval = NULL;
2286 int r;
2288 prop = props->children;
2290 if (prop->ns &&
2291 (0 == xmlStrcmp(prop->ns->href, BAD_CAST "")) &&
2292 (0 != xmlStrcmp(prop->ns->prefix, BAD_CAST ""))) {
2293 log_error_write(srv, __FILE__, __LINE__, "ss",
2294 "no name space for:",
2295 prop->name);
2297 empty_ns = 1;
2298 break;
2301 sqlite3_reset(stmt);
2303 /* bind the values to the insert */
2305 sqlite3_bind_text(stmt, 1,
2306 CONST_BUF_LEN(con->uri.path),
2307 SQLITE_TRANSIENT);
2308 sqlite3_bind_text(stmt, 2,
2309 (char *)prop->name,
2310 strlen((char *)prop->name),
2311 SQLITE_TRANSIENT);
2312 if (prop->ns) {
2313 sqlite3_bind_text(stmt, 3,
2314 (char *)prop->ns->href,
2315 strlen((char *)prop->ns->href),
2316 SQLITE_TRANSIENT);
2317 } else {
2318 sqlite3_bind_text(stmt, 3,
2321 SQLITE_TRANSIENT);
2323 if (stmt == p->conf.stmt_update_prop) {
2324 propval = prop->children
2325 ? (char *)xmlNodeListGetString(xml, prop->children, 0)
2326 : NULL;
2328 sqlite3_bind_text(stmt, 4,
2329 propval ? propval : "",
2330 propval ? strlen(propval) : 0,
2331 SQLITE_TRANSIENT);
2334 if (SQLITE_DONE != (r = sqlite3_step(stmt))) {
2335 log_error_write(srv, __FILE__, __LINE__, "ss",
2336 "sql-set failed:", sqlite3_errmsg(p->conf.sql));
2339 if (propval) xmlFree(propval);
2342 if (empty_ns) break;
2346 if (empty_ns) {
2347 if (SQLITE_OK != sqlite3_exec(p->conf.sql, "ROLLBACK", NULL, NULL, &err)) {
2348 log_error_write(srv, __FILE__, __LINE__, "ss", "can't rollback transaction:", err);
2349 sqlite3_free(err);
2351 goto propmatch_cleanup;
2354 con->http_status = 400;
2355 } else {
2356 if (SQLITE_OK != sqlite3_exec(p->conf.sql, "COMMIT", NULL, NULL, &err)) {
2357 log_error_write(srv, __FILE__, __LINE__, "ss", "can't commit transaction:", err);
2358 sqlite3_free(err);
2360 goto propmatch_cleanup;
2362 con->http_status = 200;
2364 con->file_finished = 1;
2366 xmlFreeDoc(xml);
2367 return HANDLER_FINISHED;
2370 propmatch_cleanup:
2372 xmlFreeDoc(xml);
2373 } else {
2374 con->http_status = 400;
2375 return HANDLER_FINISHED;
2378 #endif
2379 con->http_status = 501;
2380 return HANDLER_FINISHED;
2381 case HTTP_METHOD_LOCK:
2383 * a mac wants to write
2385 * LOCK /dav/expire.txt HTTP/1.1\r\n
2386 * User-Agent: WebDAVFS/1.3 (01308000) Darwin/8.1.0 (Power Macintosh)\r\n
2387 * Accept: * / *\r\n
2388 * Depth: 0\r\n
2389 * Timeout: Second-600\r\n
2390 * Content-Type: text/xml; charset=\"utf-8\"\r\n
2391 * Content-Length: 229\r\n
2392 * Connection: keep-alive\r\n
2393 * Host: 192.168.178.23:1025\r\n
2394 * \r\n
2395 * <?xml version=\"1.0\" encoding=\"utf-8\"?>\n
2396 * <D:lockinfo xmlns:D=\"DAV:\">\n
2397 * <D:lockscope><D:exclusive/></D:lockscope>\n
2398 * <D:locktype><D:write/></D:locktype>\n
2399 * <D:owner>\n
2400 * <D:href>http://www.apple.com/webdav_fs/</D:href>\n
2401 * </D:owner>\n
2402 * </D:lockinfo>\n
2405 if (depth != 0 && depth != -1) {
2406 con->http_status = 400;
2408 return HANDLER_FINISHED;
2411 #ifdef USE_LOCKS
2412 if (con->request.content_length) {
2413 xmlDocPtr xml;
2414 buffer *hdr_if = NULL;
2415 int created = 0;
2417 if (con->state == CON_STATE_READ_POST) {
2418 handler_t r = connection_handle_read_post_state(srv, con);
2419 if (r != HANDLER_GO_ON) return r;
2422 if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "If"))) {
2423 hdr_if = ds->value;
2426 if (0 != stat(con->physical.path->ptr, &st)) {
2427 if (errno == ENOENT) {
2428 int fd = open(con->physical.path->ptr, O_WRONLY|O_CREAT|O_APPEND|O_BINARY|FIFO_NONBLOCK, WEBDAV_FILE_MODE);
2429 if (fd >= 0) {
2430 close(fd);
2431 created = 1;
2432 } else {
2433 log_error_write(srv, __FILE__, __LINE__, "sBss",
2434 "create file", con->physical.path, ":", strerror(errno));
2435 con->http_status = 403; /* Forbidden */
2437 return HANDLER_FINISHED;
2440 } else if (hdr_if == NULL && depth == -1) {
2441 /* we don't support Depth: Infinity on directories */
2442 if (S_ISDIR(st.st_mode)) {
2443 con->http_status = 409; /* Conflict */
2445 return HANDLER_FINISHED;
2449 if (1 == webdav_parse_chunkqueue(srv, con, hctx, con->request_content_queue, &xml)) {
2450 xmlNode *rootnode = xmlDocGetRootElement(xml);
2452 force_assert(rootnode);
2454 if (0 == xmlStrcmp(rootnode->name, BAD_CAST "lockinfo")) {
2455 xmlNode *lockinfo;
2456 const xmlChar *lockscope = NULL, *locktype = NULL; /* TODO: compiler says unused: *owner = NULL; */
2458 for (lockinfo = rootnode->children; lockinfo; lockinfo = lockinfo->next) {
2459 if (0 == xmlStrcmp(lockinfo->name, BAD_CAST "lockscope")) {
2460 xmlNode *value;
2461 for (value = lockinfo->children; value; value = value->next) {
2462 if ((0 == xmlStrcmp(value->name, BAD_CAST "exclusive")) ||
2463 (0 == xmlStrcmp(value->name, BAD_CAST "shared"))) {
2464 lockscope = value->name;
2465 } else {
2466 con->http_status = 400;
2468 xmlFreeDoc(xml);
2469 return HANDLER_FINISHED;
2472 } else if (0 == xmlStrcmp(lockinfo->name, BAD_CAST "locktype")) {
2473 xmlNode *value;
2474 for (value = lockinfo->children; value; value = value->next) {
2475 if ((0 == xmlStrcmp(value->name, BAD_CAST "write"))) {
2476 locktype = value->name;
2477 } else {
2478 con->http_status = 400;
2480 xmlFreeDoc(xml);
2481 return HANDLER_FINISHED;
2485 } else if (0 == xmlStrcmp(lockinfo->name, BAD_CAST "owner")) {
2489 if (lockscope && locktype) {
2490 sqlite3_stmt *stmt = p->conf.stmt_read_lock_by_uri;
2492 /* is this resourse already locked ? */
2494 /* SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout
2495 * FROM locks
2496 * WHERE resource = ? */
2498 if (stmt) {
2500 sqlite3_reset(stmt);
2502 sqlite3_bind_text(stmt, 1,
2503 CONST_BUF_LEN(p->uri.path),
2504 SQLITE_TRANSIENT);
2506 /* it is the PK */
2507 while (SQLITE_ROW == sqlite3_step(stmt)) {
2508 /* we found a lock
2509 * 1. is it compatible ?
2510 * 2. is it ours */
2511 char *sql_lockscope = (char *)sqlite3_column_text(stmt, 2);
2513 if (strcmp(sql_lockscope, "exclusive")) {
2514 con->http_status = 423;
2515 } else if (0 == xmlStrcmp(lockscope, BAD_CAST "exclusive")) {
2516 /* resourse is locked with a shared lock
2517 * client wants exclusive */
2518 con->http_status = 423;
2521 if (con->http_status == 423) {
2522 xmlFreeDoc(xml);
2523 return HANDLER_FINISHED;
2527 stmt = p->conf.stmt_create_lock;
2528 if (stmt) {
2529 /* create a lock-token */
2530 uuid_t id;
2531 char uuid[37] /* 36 + \0 */;
2533 uuid_generate(id);
2534 uuid_unparse(id, uuid);
2536 buffer_copy_string_len(p->tmp_buf, CONST_STR_LEN("opaquelocktoken:"));
2537 buffer_append_string(p->tmp_buf, uuid);
2539 /* "CREATE TABLE locks ("
2540 * " locktoken TEXT NOT NULL,"
2541 * " resource TEXT NOT NULL,"
2542 * " lockscope TEXT NOT NULL,"
2543 * " locktype TEXT NOT NULL,"
2544 * " owner TEXT NOT NULL,"
2545 * " depth INT NOT NULL,"
2548 sqlite3_reset(stmt);
2550 sqlite3_bind_text(stmt, 1,
2551 CONST_BUF_LEN(p->tmp_buf),
2552 SQLITE_TRANSIENT);
2554 sqlite3_bind_text(stmt, 2,
2555 CONST_BUF_LEN(con->uri.path),
2556 SQLITE_TRANSIENT);
2558 sqlite3_bind_text(stmt, 3,
2559 (const char *)lockscope,
2560 xmlStrlen(lockscope),
2561 SQLITE_TRANSIENT);
2563 sqlite3_bind_text(stmt, 4,
2564 (const char *)locktype,
2565 xmlStrlen(locktype),
2566 SQLITE_TRANSIENT);
2568 /* owner */
2569 sqlite3_bind_text(stmt, 5,
2572 SQLITE_TRANSIENT);
2574 /* depth */
2575 sqlite3_bind_int(stmt, 6,
2576 depth);
2579 if (SQLITE_DONE != sqlite3_step(stmt)) {
2580 log_error_write(srv, __FILE__, __LINE__, "ss",
2581 "create lock:", sqlite3_errmsg(p->conf.sql));
2584 /* looks like we survived */
2585 webdav_lockdiscovery(srv, con, p->tmp_buf, (const char *)lockscope, (const char *)locktype, depth);
2587 con->http_status = created ? 201 : 200;
2588 con->file_finished = 1;
2593 xmlFreeDoc(xml);
2594 return HANDLER_FINISHED;
2595 } else {
2596 con->http_status = 400;
2597 return HANDLER_FINISHED;
2599 } else {
2601 if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "If"))) {
2602 buffer *locktoken = ds->value;
2603 sqlite3_stmt *stmt = p->conf.stmt_refresh_lock;
2605 /* remove the < > around the token */
2606 if (buffer_string_length(locktoken) < 5) {
2607 con->http_status = 400;
2609 return HANDLER_FINISHED;
2612 buffer_copy_string_len(p->tmp_buf, locktoken->ptr + 2, buffer_string_length(locktoken) - 4);
2614 sqlite3_reset(stmt);
2616 sqlite3_bind_text(stmt, 1,
2617 CONST_BUF_LEN(p->tmp_buf),
2618 SQLITE_TRANSIENT);
2620 if (SQLITE_DONE != sqlite3_step(stmt)) {
2621 log_error_write(srv, __FILE__, __LINE__, "ss",
2622 "refresh lock:", sqlite3_errmsg(p->conf.sql));
2625 webdav_lockdiscovery(srv, con, p->tmp_buf, "exclusive", "write", 0);
2627 con->http_status = 200;
2628 con->file_finished = 1;
2629 return HANDLER_FINISHED;
2630 } else {
2631 /* we need a lock-token to refresh */
2632 con->http_status = 400;
2634 return HANDLER_FINISHED;
2637 break;
2638 #else
2639 con->http_status = 501;
2640 return HANDLER_FINISHED;
2641 #endif
2642 case HTTP_METHOD_UNLOCK:
2643 #ifdef USE_LOCKS
2644 if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "Lock-Token"))) {
2645 buffer *locktoken = ds->value;
2646 sqlite3_stmt *stmt = p->conf.stmt_remove_lock;
2648 /* remove the < > around the token */
2649 if (buffer_string_length(locktoken) < 3) {
2650 con->http_status = 400;
2652 return HANDLER_FINISHED;
2656 * FIXME:
2658 * if the resourse is locked:
2659 * - by us: unlock
2660 * - by someone else: 401
2661 * if the resource is not locked:
2662 * - 412
2663 * */
2665 buffer_copy_string_len(p->tmp_buf, locktoken->ptr + 1, buffer_string_length(locktoken) - 2);
2667 sqlite3_reset(stmt);
2669 sqlite3_bind_text(stmt, 1,
2670 CONST_BUF_LEN(p->tmp_buf),
2671 SQLITE_TRANSIENT);
2673 if (SQLITE_DONE != sqlite3_step(stmt)) {
2674 log_error_write(srv, __FILE__, __LINE__, "ss",
2675 "remove lock:", sqlite3_errmsg(p->conf.sql));
2678 if (0 == sqlite3_changes(p->conf.sql)) {
2679 con->http_status = 401;
2680 } else {
2681 con->http_status = 204;
2683 return HANDLER_FINISHED;
2684 } else {
2685 /* we need a lock-token to unlock */
2686 con->http_status = 400;
2688 return HANDLER_FINISHED;
2690 break;
2691 #else
2692 con->http_status = 501;
2693 return HANDLER_FINISHED;
2694 #endif
2695 default:
2696 break;
2699 /* not found */
2700 return HANDLER_GO_ON;
2704 SUBREQUEST_FUNC(mod_webdav_subrequest_handler) {
2705 handler_t r;
2706 plugin_data *p = p_d;
2707 if (con->mode != p->id) return HANDLER_GO_ON;
2709 r = mod_webdav_subrequest_handler_huge(srv, con, p_d);
2710 if (con->http_status >= 400) con->mode = DIRECT;
2711 return r;
2715 PHYSICALPATH_FUNC(mod_webdav_physical_handler) {
2716 plugin_data *p = p_d;
2717 if (!p->conf.enabled) return HANDLER_GO_ON;
2719 /* physical path is setup */
2720 if (buffer_is_empty(con->physical.path)) return HANDLER_GO_ON;
2722 UNUSED(srv);
2724 switch (con->request.http_method) {
2725 case HTTP_METHOD_PROPFIND:
2726 case HTTP_METHOD_PROPPATCH:
2727 case HTTP_METHOD_PUT:
2728 case HTTP_METHOD_COPY:
2729 case HTTP_METHOD_MOVE:
2730 case HTTP_METHOD_MKCOL:
2731 case HTTP_METHOD_DELETE:
2732 case HTTP_METHOD_LOCK:
2733 case HTTP_METHOD_UNLOCK: {
2734 handler_ctx *hctx = calloc(1, sizeof(*hctx));
2735 memcpy(&hctx->conf, &p->conf, sizeof(plugin_config));
2736 con->plugin_ctx[p->id] = hctx;
2737 con->conf.stream_request_body = 0;
2738 con->mode = p->id;
2739 break;
2741 default:
2742 break;
2745 return HANDLER_GO_ON;
2748 static handler_t mod_webdav_connection_reset(server *srv, connection *con, void *p_d) {
2749 plugin_data *p = p_d;
2750 handler_ctx *hctx = con->plugin_ctx[p->id];
2751 if (hctx) {
2752 free(hctx);
2753 con->plugin_ctx[p->id] = NULL;
2756 UNUSED(srv);
2757 return HANDLER_GO_ON;
2761 /* this function is called at dlopen() time and inits the callbacks */
2763 int mod_webdav_plugin_init(plugin *p);
2764 int mod_webdav_plugin_init(plugin *p) {
2765 p->version = LIGHTTPD_VERSION_ID;
2766 p->name = buffer_init_string("webdav");
2768 p->init = mod_webdav_init;
2769 p->handle_uri_clean = mod_webdav_uri_handler;
2770 p->handle_physical = mod_webdav_physical_handler;
2771 p->handle_subrequest = mod_webdav_subrequest_handler;
2772 p->connection_reset = mod_webdav_connection_reset;
2773 p->set_defaults = mod_webdav_set_defaults;
2774 p->cleanup = mod_webdav_free;
2776 p->data = NULL;
2778 return 0;