7 #include "connections.h"
11 #include "stat_cache.h"
15 #include <sys/types.h>
26 #if defined(HAVE_LIBXML_H) && defined(HAVE_SQLITE3_H)
28 #include <libxml/tree.h>
29 #include <libxml/parser.h>
34 #if defined(HAVE_LIBXML_H) && defined(HAVE_SQLITE3_H) && defined(HAVE_UUID_UUID_H)
36 #include <uuid/uuid.h>
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 */
53 unsigned short enabled
;
54 unsigned short is_readonly
;
55 unsigned short log_xml
;
57 buffer
*sqlite_db_name
;
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
;
84 plugin_config
**config_storage
;
93 /* init the plugin data */
94 INIT_FUNC(mod_webdav_init
) {
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();
114 /* detroy the plugin data */
115 FREE_FUNC(mod_webdav_free
) {
116 plugin_data
*p
= p_d
;
120 if (!p
) return HANDLER_GO_ON
;
122 if (p
->config_storage
) {
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
);
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
);
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
);
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
;
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
];
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
)) {
208 const char *next_stmt
;
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",
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,"
223 " value TEXT NOT NULL,"
224 " PRIMARY KEY(resource, prop, ns))",
227 if (0 != strcmp(err
, "table properties already exists")) {
228 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "can't open transaction:", err
);
231 return HANDLER_ERROR
;
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))",
248 if (0 != strcmp(err
, "table locks already exists")) {
249 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "can't open transaction:", err
);
252 return HANDLER_ERROR
;
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
)) {
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
)) {
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
)) {
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
)) {
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
)) {
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
)) {
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
)) {
316 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
318 return HANDLER_ERROR
;
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
)) {
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
)) {
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
)) {
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
)) {
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
)) {
363 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
365 return HANDLER_ERROR
;
370 log_error_write(srv
, __FILE__
, __LINE__
, "s", "Sorry, no sqlite3 and libxml2 support include, compile with --with-webdav-props");
371 return HANDLER_ERROR
;
376 return HANDLER_GO_ON
;
379 #define PATCH_OPTION(x) \
381 static int mod_webdav_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
383 plugin_config
*s
= p
->config_storage
[0];
385 PATCH_OPTION(enabled
);
386 PATCH_OPTION(is_readonly
);
387 PATCH_OPTION(log_xml
);
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
);
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;
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"))) {
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
);
449 URIHANDLER_FUNC(mod_webdav_uri_handler
) {
450 plugin_data
*p
= p_d
;
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"));
469 response_header_insert(srv
, con
, CONST_STR_LEN("Allow"), CONST_STR_LEN("PROPFIND, DELETE, MKCOL, PUT, MOVE, COPY, PROPPATCH, LOCK, UNLOCK"));
477 return HANDLER_GO_ON
;
479 static int webdav_gen_prop_tag(server
*srv
, connection
*con
,
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(">"));
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("\"/>"));
512 static int webdav_gen_response_status_tag(server
*srv
, connection
*con
, physical
*dst
, int status
, buffer
*b
) {
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 "));
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"));
537 static int webdav_delete_file(server
*srv
, connection
*con
, handler_ctx
*hctx
, physical
*dst
, buffer
*b
) {
540 /* try to unlink it */
541 if (-1 == unlink(dst
->path
->ptr
)) {
552 webdav_gen_response_status_tag(srv
, con
, dst
, status
, b
);
555 sqlite3_stmt
*stmt
= hctx
->conf
.stmt_delete_uri
;
559 webdav_gen_response_status_tag(srv
, con
, dst
, status
, b
);
563 /* bind the values to the insert */
565 sqlite3_bind_text(stmt
, 1,
566 CONST_BUF_LEN(dst
->rel_path
),
569 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
578 return (status
!= 0);
581 static int webdav_delete_dir(server
*srv
, connection
*con
, handler_ctx
*hctx
, physical
*dst
, buffer
*b
) {
583 int have_multi_status
= 0;
586 d
.path
= buffer_init();
587 d
.rel_path
= buffer_init();
589 if (NULL
!= (dir
= opendir(dst
->path
->ptr
))) {
592 while(NULL
!= (de
= readdir(dir
))) {
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')) {
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
)) {
628 have_multi_status
= 1;
630 webdav_gen_response_status_tag(srv
, con
, &d
, status
, b
);
633 sqlite3_stmt
*stmt
= hctx
->conf
.stmt_delete_uri
;
638 /* bind the values to the insert */
640 sqlite3_bind_text(stmt
, 1,
641 CONST_BUF_LEN(d
.rel_path
),
644 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
651 have_multi_status
= webdav_delete_file(srv
, con
, hctx
, &d
, b
);
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
667 # define FIFO_NONBLOCK 0
674 static int webdav_copy_file(server
*srv
, connection
*con
, handler_ctx
*hctx
, physical
*src
, physical
*dst
, int overwrite
) {
676 ssize_t rd
, wr
, offset
;
677 int status
= 0, ifd
, ofd
;
681 if (-1 == (ifd
= open(src
->path
->ptr
, O_RDONLY
| O_BINARY
| FIFO_NONBLOCK
))) {
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 */
695 /* at least one part in the middle wasn't existing */
706 data
= malloc(131072);
709 while (0 < (rd
= read(ifd
, data
, 131072))) {
712 wr
= write(ofd
, data
+offset
, (size_t)(rd
-offset
));
713 } while (wr
>= 0 ? (offset
+= wr
) != rd
: (errno
== EINTR
));
715 status
= (errno
== ENOSPC
) ? 507 : 403;
720 if (0 != rd
&& 0 == status
) status
= 403;
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
));
732 /* copy worked fine, copy connected properties */
733 sqlite3_stmt
*stmt
= hctx
->conf
.stmt_copy_uri
;
738 /* bind the values to the insert */
739 sqlite3_bind_text(stmt
, 1,
740 CONST_BUF_LEN(dst
->rel_path
),
743 sqlite3_bind_text(stmt
, 2,
744 CONST_BUF_LEN(src
->rel_path
),
747 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
758 static int webdav_copy_dir(server
*srv
, connection
*con
, handler_ctx
*hctx
, physical
*src
, physical
*dst
, int overwrite
) {
762 if (NULL
!= (srcdir
= opendir(src
->path
->ptr
))) {
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
))) {
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')) {
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
)) {
798 } else if (S_ISDIR(st
.st_mode
)) {
800 if (-1 == mkdir(d
.path
->ptr
, WEBDAV_DIR_MODE
) &&
805 sqlite3_stmt
*stmt
= hctx
->conf
.stmt_copy_uri
;
807 if (0 != (status
= webdav_copy_dir(srv
, con
, hctx
, &s
, &d
, overwrite
))) {
810 /* directory is copied, copy the properties too */
815 /* bind the values to the insert */
816 sqlite3_bind_text(stmt
, 1,
817 CONST_BUF_LEN(dst
->rel_path
),
820 sqlite3_bind_text(stmt
, 2,
821 CONST_BUF_LEN(src
->rel_path
),
824 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
830 } else if (S_ISREG(st
.st_mode
)) {
832 if (0 != (status
= webdav_copy_file(srv
, con
, hctx
, &s
, &d
, overwrite
))) {
839 buffer_free(s
.rel_path
);
841 buffer_free(d
.rel_path
);
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"));
897 /* SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout
899 * WHERE resource = ? */
903 sqlite3_bind_text(stmt
, 1,
904 CONST_BUF_LEN(dst
->rel_path
),
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
;
920 webdav_activelock(b
, &locktoken
, lockscope
, locktype
, depth
, timeout
);
923 buffer_append_string_len(b
, CONST_STR_LEN("</D:lockdiscovery>\n"));
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
;
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>"));
941 buffer_append_string_len(b
, CONST_STR_LEN("<D:resourcetype/>"));
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>"));
948 } else if(S_ISREG(sce
->st
.st_mode
)) {
949 const buffer
*type
= stat_cache_mimetype_by_ext(con
, CONST_BUF_LEN(dst
->path
));
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>"));
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>"));
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>"));
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>"));
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>"));
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
);
987 } else if (0 == strcmp(prop_name
, "lockdiscovery")) {
988 webdav_get_live_property_lockdiscovery(srv
, con
, hctx
, dst
, b
);
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>"));
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
);
1011 #ifdef USE_PROPPATCH
1012 sqlite3_stmt
*stmt
= hctx
->conf
.stmt_select_prop
;
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
),
1023 sqlite3_bind_text(stmt
, 2,
1027 sqlite3_bind_text(stmt
, 3,
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
);
1040 return found
? 0 : -1;
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)*/
1063 { "DAV:", "lockdiscovery" },
1064 { "DAV:", "supportedlock" },
1071 webdav_property
**ptr
;
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
) {
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
);
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
);
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
;
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
;
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
);
1130 data
= c
->file
.mmap
.start
+ c
->offset
;
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
));
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
;
1146 if (weHave
> 65536) weHave
= 65536;
1147 data
= malloc(weHave
);
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
);
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
);
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
);
1188 switch ((err
= xmlParseChunk(ctxt
, 0, 0, 1))) {
1189 case XML_ERR_DOCUMENT_END
:
1193 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "xmlParseChunk failed at final packet:", err
);
1198 res
= ctxt
->wellFormed
;
1199 xmlFreeParserCtxt(ctxt
);
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
);
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
) {
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>)
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"])
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
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,
1283 while (SQLITE_ROW
== sqlite3_step(stmt
)) {
1298 SUBREQUEST_FUNC(mod_webdav_subrequest_handler_huge
) {
1299 plugin_data
*p
= p_d
;
1300 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
1304 int depth
= -1; /* (Depth: infinity) */
1308 webdav_properties
*req_props
;
1309 stat_cache_entry
*sce
= NULL
;
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
) {
1322 } else if ('1' == *ds
->value
->ptr
) {
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 */
1332 /* is there a content-body ? */
1334 switch (stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) {
1336 if (errno
== ENOENT
) {
1337 con
->http_status
= 404;
1338 return HANDLER_FINISHED
;
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
) {
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")) {
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 */
1376 for (prop
= cmd
->children
; prop
; prop
= prop
->next
) {
1377 if (prop
->type
== XML_TEXT_NODE
) continue; /* ignore WS */
1380 (0 == xmlStrcmp(prop
->ns
->href
, BAD_CAST
"")) &&
1381 (0 != xmlStrcmp(prop
->ns
->prefix
, BAD_CAST
""))) {
1383 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
1384 "no name space for:",
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
);
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
);
1415 } else if (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"propname")) {
1416 sqlite3_stmt
*stmt
= p
->conf
.stmt_select_propnames
;
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
),
1427 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
1430 } else if (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"allprop")) {
1431 /* get all properties (EMPTY) */
1438 con
->http_status
= 400;
1439 return HANDLER_FINISHED
;
1443 con
->http_status
= 207;
1445 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/xml; charset=\"utf-8\""));
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"));
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"));
1500 if (NULL
!= (dir
= opendir(con
->physical
.path
->ptr
))) {
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'))) {
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"));
1568 buffer_free(d
.path
);
1569 buffer_free(d
.rel_path
);
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
);
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
);
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
)) {
1618 con
->http_status
= 403;
1622 con
->http_status
= 409;
1626 con
->http_status
= 405; /* not allowed */
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 */
1652 con
->http_status
= 404;
1655 con
->http_status
= 403;
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\""));
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
);
1689 con
->http_status
= 207;
1690 con
->file_finished
= 1;
1692 /* everything went fine, remove the directory */
1694 if (-1 == rmdir(con
->physical
.path
->ptr
)) {
1697 con
->http_status
= 403;
1700 con
->http_status
= 404;
1703 con
->http_status
= 501;
1707 con
->http_status
= 204;
1711 buffer_free(multi_status_resp
);
1712 } else if (-1 == unlink(con
->physical
.path
->ptr
)) {
1715 con
->http_status
= 403;
1718 con
->http_status
= 404;
1721 con
->http_status
= 501;
1725 con
->http_status
= 204;
1727 return HANDLER_FINISHED
;
1728 case HTTP_METHOD_PUT
: {
1730 chunkqueue
*cq
= con
->request_content_queue
;
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
;
1762 if (0 != strncmp(num
, "bytes ", 6)) {
1763 con
->http_status
= 501; /* not implemented */
1765 return HANDLER_FINISHED
;
1768 /* we only support <num>- ... */
1773 while (*num
== ' ' || *num
== '\t') num
++;
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
))) {
1792 con
->http_status
= 404; /* not found */
1795 con
->http_status
= 403; /* not found */
1798 return HANDLER_FINISHED
;
1801 if (-1 == lseek(fd
, offset
, SEEK_SET
)) {
1802 con
->http_status
= 501; /* not implemented */
1806 return HANDLER_FINISHED
;
1808 con
->http_status
= 200; /* modified */
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
;
1821 con
->http_status
= 201; /* created */
1824 con
->http_status
= 200; /* modified */
1828 con
->file_finished
= 1;
1830 for (c
= cq
->first
; c
; c
= cq
->first
) {
1836 /* copy all chunks */
1840 mapped
= (c
->file
.mmap
.start
!= MAP_FAILED
);
1841 dlen
= c
->file
.length
- c
->offset
;
1843 data
= c
->file
.mmap
.start
+ c
->offset
;
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
));
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
;
1859 if (dlen
> 65536) dlen
= 65536;
1860 data
= malloc(dlen
);
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
);
1868 return HANDLER_ERROR
;
1875 if ((r
= write(fd
, data
, dlen
)) < 0) {
1878 con
->http_status
= 507;
1882 con
->http_status
= 403;
1887 if (!mapped
) free(data
);
1890 if ((r
= write(fd
, c
->mem
->ptr
+ c
->offset
, buffer_string_length(c
->mem
) - c
->offset
)) < 0) {
1893 con
->http_status
= 507;
1897 con
->http_status
= 403;
1905 chunkqueue_mark_written(cq
, r
);
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
;
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
;
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
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
);
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 */
1984 buffer_copy_string_len(p
->uri
.authority
, start
, sep
- start
);
1988 if (NULL
== (sep
= strchr(start
, '?'))) {
1989 /* no query string, good */
1990 buffer_copy_string(p
->uri
.path_raw
, start
);
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 */
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
);
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);
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 */
2078 con
->http_status
= 404;
2081 con
->http_status
= 403;
2084 } else if (S_ISDIR(st
.st_mode
)) {
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
;
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
;
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
;
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
) {
2122 webdav_delete_dir(srv
, con
, hctx
, &(con
->physical
), b
); /* content */
2125 rmdir(con
->physical
.path
->ptr
);
2127 con
->http_status
= created
? 201 : 204;
2128 con
->file_finished
= 1;
2130 /* it is just a file, good */
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
)) {
2144 * append basename to physical path */
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
);
2155 con
->http_status
= destdir
? 204 : 201; /* we will create a new one */
2156 con
->file_finished
= 1;
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
;
2168 con
->http_status
= 204; /* resource already existed */
2171 if (con
->request
.http_method
== HTTP_METHOD_MOVE
) {
2174 if (0 == rename(con
->physical
.path
->ptr
, p
->physical
.path
->ptr
)) {
2175 #ifdef USE_PROPPATCH
2178 stmt
= p
->conf
.stmt_move_uri
;
2181 sqlite3_reset(stmt
);
2183 /* bind the values to the insert */
2184 sqlite3_bind_text(stmt
, 1,
2185 CONST_BUF_LEN(p
->uri
.path
),
2188 sqlite3_bind_text(stmt
, 2,
2189 CONST_BUF_LEN(con
->uri
.path
),
2192 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
2193 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sql-move failed:", sqlite3_errmsg(p
->conf
.sql
));
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
) {
2211 webdav_delete_file(srv
, con
, hctx
, &(con
->physical
), 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
)) {
2233 con
->http_status
= 404;
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
) {
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")) {
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
);
2266 goto propmatch_cleanup
;
2269 /* a UPDATE request, we know 'set' and 'remove' */
2270 for (cmd
= rootnode
->children
; cmd
; cmd
= cmd
->next
) {
2272 /* either set or remove */
2274 if ((0 == xmlStrcmp(cmd
->name
, BAD_CAST
"set")) ||
2275 (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"remove"))) {
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")) {
2285 char *propval
= NULL
;
2288 prop
= props
->children
;
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:",
2301 sqlite3_reset(stmt
);
2303 /* bind the values to the insert */
2305 sqlite3_bind_text(stmt
, 1,
2306 CONST_BUF_LEN(con
->uri
.path
),
2308 sqlite3_bind_text(stmt
, 2,
2310 strlen((char *)prop
->name
),
2313 sqlite3_bind_text(stmt
, 3,
2314 (char *)prop
->ns
->href
,
2315 strlen((char *)prop
->ns
->href
),
2318 sqlite3_bind_text(stmt
, 3,
2323 if (stmt
== p
->conf
.stmt_update_prop
) {
2324 propval
= prop
->children
2325 ? (char *)xmlNodeListGetString(xml
, prop
->children
, 0)
2328 sqlite3_bind_text(stmt
, 4,
2329 propval
? propval
: "",
2330 propval
? strlen(propval
) : 0,
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;
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
);
2351 goto propmatch_cleanup
;
2354 con
->http_status
= 400;
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
);
2360 goto propmatch_cleanup
;
2362 con
->http_status
= 200;
2364 con
->file_finished
= 1;
2367 return HANDLER_FINISHED
;
2374 con
->http_status
= 400;
2375 return HANDLER_FINISHED
;
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
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
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
2400 * <D:href>http://www.apple.com/webdav_fs/</D:href>\n
2405 if (depth
!= 0 && depth
!= -1) {
2406 con
->http_status
= 400;
2408 return HANDLER_FINISHED
;
2412 if (con
->request
.content_length
) {
2414 buffer
*hdr_if
= NULL
;
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"))) {
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
);
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")) {
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")) {
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
;
2466 con
->http_status
= 400;
2469 return HANDLER_FINISHED
;
2472 } else if (0 == xmlStrcmp(lockinfo
->name
, BAD_CAST
"locktype")) {
2474 for (value
= lockinfo
->children
; value
; value
= value
->next
) {
2475 if ((0 == xmlStrcmp(value
->name
, BAD_CAST
"write"))) {
2476 locktype
= value
->name
;
2478 con
->http_status
= 400;
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
2496 * WHERE resource = ? */
2500 sqlite3_reset(stmt
);
2502 sqlite3_bind_text(stmt
, 1,
2503 CONST_BUF_LEN(p
->uri
.path
),
2507 while (SQLITE_ROW
== sqlite3_step(stmt
)) {
2509 * 1. is it compatible ?
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) {
2523 return HANDLER_FINISHED
;
2527 stmt
= p
->conf
.stmt_create_lock
;
2529 /* create a lock-token */
2531 char uuid
[37] /* 36 + \0 */;
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
),
2554 sqlite3_bind_text(stmt
, 2,
2555 CONST_BUF_LEN(con
->uri
.path
),
2558 sqlite3_bind_text(stmt
, 3,
2559 (const char *)lockscope
,
2560 xmlStrlen(lockscope
),
2563 sqlite3_bind_text(stmt
, 4,
2564 (const char *)locktype
,
2565 xmlStrlen(locktype
),
2569 sqlite3_bind_text(stmt
, 5,
2575 sqlite3_bind_int(stmt
, 6,
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;
2594 return HANDLER_FINISHED
;
2596 con
->http_status
= 400;
2597 return HANDLER_FINISHED
;
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
),
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
;
2631 /* we need a lock-token to refresh */
2632 con
->http_status
= 400;
2634 return HANDLER_FINISHED
;
2639 con
->http_status
= 501;
2640 return HANDLER_FINISHED
;
2642 case HTTP_METHOD_UNLOCK
:
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
;
2658 * if the resourse is locked:
2660 * - by someone else: 401
2661 * if the resource is not locked:
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
),
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;
2681 con
->http_status
= 204;
2683 return HANDLER_FINISHED
;
2685 /* we need a lock-token to unlock */
2686 con
->http_status
= 400;
2688 return HANDLER_FINISHED
;
2692 con
->http_status
= 501;
2693 return HANDLER_FINISHED
;
2700 return HANDLER_GO_ON
;
2704 SUBREQUEST_FUNC(mod_webdav_subrequest_handler
) {
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
;
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
;
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;
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
];
2753 con
->plugin_ctx
[p
->id
] = NULL
;
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
;