7 #include "connections.h"
11 #include "stat_cache.h"
15 #include <sys/types.h>
28 #if defined(HAVE_LIBXML_H) && defined(HAVE_SQLITE3_H)
30 #include <libxml/tree.h>
31 #include <libxml/parser.h>
36 #if defined(HAVE_LIBXML_H) && defined(HAVE_SQLITE3_H) && defined(HAVE_UUID_UUID_H)
38 #include <uuid/uuid.h>
42 * this is a webdav for a lighttpd plugin
44 * at least a very basic one.
45 * - for now it is read-only and we only support PROPFIND
49 #define WEBDAV_FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
50 #define WEBDAV_DIR_MODE S_IRWXU | S_IRWXG | S_IRWXO
52 /* plugin config for all request/connections */
55 unsigned short enabled
;
56 unsigned short is_readonly
;
57 unsigned short log_xml
;
59 buffer
*sqlite_db_name
;
62 sqlite3_stmt
*stmt_update_prop
;
63 sqlite3_stmt
*stmt_delete_prop
;
64 sqlite3_stmt
*stmt_select_prop
;
65 sqlite3_stmt
*stmt_select_propnames
;
67 sqlite3_stmt
*stmt_delete_uri
;
68 sqlite3_stmt
*stmt_move_uri
;
69 sqlite3_stmt
*stmt_copy_uri
;
71 sqlite3_stmt
*stmt_remove_lock
;
72 sqlite3_stmt
*stmt_create_lock
;
73 sqlite3_stmt
*stmt_read_lock
;
74 sqlite3_stmt
*stmt_read_lock_by_uri
;
75 sqlite3_stmt
*stmt_refresh_lock
;
86 plugin_config
**config_storage
;
95 /* init the plugin data */
96 INIT_FUNC(mod_webdav_init
) {
99 p
= calloc(1, sizeof(*p
));
101 p
->tmp_buf
= buffer_init();
103 p
->uri
.scheme
= buffer_init();
104 p
->uri
.path_raw
= buffer_init();
105 p
->uri
.path
= buffer_init();
106 p
->uri
.authority
= buffer_init();
108 p
->physical
.path
= buffer_init();
109 p
->physical
.rel_path
= buffer_init();
110 p
->physical
.doc_root
= buffer_init();
111 p
->physical
.basedir
= buffer_init();
116 /* detroy the plugin data */
117 FREE_FUNC(mod_webdav_free
) {
118 plugin_data
*p
= p_d
;
122 if (!p
) return HANDLER_GO_ON
;
124 if (p
->config_storage
) {
126 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
127 plugin_config
*s
= p
->config_storage
[i
];
129 if (NULL
== s
) continue;
131 buffer_free(s
->sqlite_db_name
);
134 sqlite3_finalize(s
->stmt_delete_prop
);
135 sqlite3_finalize(s
->stmt_delete_uri
);
136 sqlite3_finalize(s
->stmt_copy_uri
);
137 sqlite3_finalize(s
->stmt_move_uri
);
138 sqlite3_finalize(s
->stmt_update_prop
);
139 sqlite3_finalize(s
->stmt_select_prop
);
140 sqlite3_finalize(s
->stmt_select_propnames
);
142 sqlite3_finalize(s
->stmt_read_lock
);
143 sqlite3_finalize(s
->stmt_read_lock_by_uri
);
144 sqlite3_finalize(s
->stmt_create_lock
);
145 sqlite3_finalize(s
->stmt_remove_lock
);
146 sqlite3_finalize(s
->stmt_refresh_lock
);
147 sqlite3_close(s
->sql
);
152 free(p
->config_storage
);
155 buffer_free(p
->uri
.scheme
);
156 buffer_free(p
->uri
.path_raw
);
157 buffer_free(p
->uri
.path
);
158 buffer_free(p
->uri
.authority
);
160 buffer_free(p
->physical
.path
);
161 buffer_free(p
->physical
.rel_path
);
162 buffer_free(p
->physical
.doc_root
);
163 buffer_free(p
->physical
.basedir
);
165 buffer_free(p
->tmp_buf
);
169 return HANDLER_GO_ON
;
172 /* handle plugin config and check values */
174 SETDEFAULTS_FUNC(mod_webdav_set_defaults
) {
175 plugin_data
*p
= p_d
;
178 config_values_t cv
[] = {
179 { "webdav.activate", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
180 { "webdav.is-readonly", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
181 { "webdav.sqlite-db-name", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
182 { "webdav.log-xml", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
183 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
186 if (!p
) return HANDLER_ERROR
;
188 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
190 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
191 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
194 s
= calloc(1, sizeof(plugin_config
));
195 s
->sqlite_db_name
= buffer_init();
197 cv
[0].destination
= &(s
->enabled
);
198 cv
[1].destination
= &(s
->is_readonly
);
199 cv
[2].destination
= s
->sqlite_db_name
;
200 cv
[3].destination
= &(s
->log_xml
);
202 p
->config_storage
[i
] = s
;
204 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
205 return HANDLER_ERROR
;
208 if (!buffer_string_is_empty(s
->sqlite_db_name
)) {
210 const char *next_stmt
;
213 if (SQLITE_OK
!= sqlite3_open(s
->sqlite_db_name
->ptr
, &(s
->sql
))) {
214 log_error_write(srv
, __FILE__
, __LINE__
, "sbs", "sqlite3_open failed for",
216 sqlite3_errmsg(s
->sql
));
217 return HANDLER_ERROR
;
220 if (SQLITE_OK
!= sqlite3_exec(s
->sql
,
221 "CREATE TABLE IF NOT EXISTS properties ("
222 " resource TEXT NOT NULL,"
223 " prop TEXT NOT NULL,"
225 " value TEXT NOT NULL,"
226 " PRIMARY KEY(resource, prop, ns))",
229 if (0 != strcmp(err
, "table properties already exists")) {
230 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "can't open transaction:", err
);
233 return HANDLER_ERROR
;
238 if (SQLITE_OK
!= sqlite3_exec(s
->sql
,
239 "CREATE TABLE IF NOT EXISTS locks ("
240 " locktoken TEXT NOT NULL,"
241 " resource TEXT NOT NULL,"
242 " lockscope TEXT NOT NULL,"
243 " locktype TEXT NOT NULL,"
244 " owner TEXT NOT NULL,"
245 " depth INT NOT NULL,"
246 " timeout TIMESTAMP NOT NULL,"
247 " PRIMARY KEY(locktoken))",
250 if (0 != strcmp(err
, "table locks already exists")) {
251 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "can't open transaction:", err
);
254 return HANDLER_ERROR
;
259 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
260 CONST_STR_LEN("SELECT value FROM properties WHERE resource = ? AND prop = ? AND ns = ?"),
261 &(s
->stmt_select_prop
), &next_stmt
)) {
264 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed:", sqlite3_errmsg(s
->sql
));
265 return HANDLER_ERROR
;
268 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
269 CONST_STR_LEN("SELECT ns, prop FROM properties WHERE resource = ?"),
270 &(s
->stmt_select_propnames
), &next_stmt
)) {
273 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed:", sqlite3_errmsg(s
->sql
));
274 return HANDLER_ERROR
;
278 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
279 CONST_STR_LEN("REPLACE INTO properties (resource, prop, ns, value) VALUES (?, ?, ?, ?)"),
280 &(s
->stmt_update_prop
), &next_stmt
)) {
283 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed:", sqlite3_errmsg(s
->sql
));
284 return HANDLER_ERROR
;
287 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
288 CONST_STR_LEN("DELETE FROM properties WHERE resource = ? AND prop = ? AND ns = ?"),
289 &(s
->stmt_delete_prop
), &next_stmt
)) {
291 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
293 return HANDLER_ERROR
;
296 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
297 CONST_STR_LEN("DELETE FROM properties WHERE resource = ?"),
298 &(s
->stmt_delete_uri
), &next_stmt
)) {
300 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
302 return HANDLER_ERROR
;
305 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
306 CONST_STR_LEN("INSERT INTO properties SELECT ?, prop, ns, value FROM properties WHERE resource = ?"),
307 &(s
->stmt_copy_uri
), &next_stmt
)) {
309 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
311 return HANDLER_ERROR
;
314 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
315 CONST_STR_LEN("UPDATE OR REPLACE properties SET resource = ? WHERE resource = ?"),
316 &(s
->stmt_move_uri
), &next_stmt
)) {
318 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
320 return HANDLER_ERROR
;
325 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
326 CONST_STR_LEN("INSERT INTO locks (locktoken, resource, lockscope, locktype, owner, depth, timeout) VALUES (?,?,?,?,?,?, CURRENT_TIME + 600)"),
327 &(s
->stmt_create_lock
), &next_stmt
)) {
329 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
331 return HANDLER_ERROR
;
334 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
335 CONST_STR_LEN("DELETE FROM locks WHERE locktoken = ?"),
336 &(s
->stmt_remove_lock
), &next_stmt
)) {
338 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
340 return HANDLER_ERROR
;
343 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
344 CONST_STR_LEN("SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout-CURRENT_TIME FROM locks WHERE locktoken = ?"),
345 &(s
->stmt_read_lock
), &next_stmt
)) {
347 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
349 return HANDLER_ERROR
;
352 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
353 CONST_STR_LEN("SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout-CURRENT_TIME FROM locks WHERE resource = ?"),
354 &(s
->stmt_read_lock_by_uri
), &next_stmt
)) {
356 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
358 return HANDLER_ERROR
;
361 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
362 CONST_STR_LEN("UPDATE locks SET timeout = CURRENT_TIME + 600 WHERE locktoken = ?"),
363 &(s
->stmt_refresh_lock
), &next_stmt
)) {
365 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
367 return HANDLER_ERROR
;
372 log_error_write(srv
, __FILE__
, __LINE__
, "s", "Sorry, no sqlite3 and libxml2 support include, compile with --with-webdav-props");
373 return HANDLER_ERROR
;
378 return HANDLER_GO_ON
;
381 #define PATCH_OPTION(x) \
383 static int mod_webdav_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
385 plugin_config
*s
= p
->config_storage
[0];
387 PATCH_OPTION(enabled
);
388 PATCH_OPTION(is_readonly
);
389 PATCH_OPTION(log_xml
);
393 PATCH_OPTION(stmt_update_prop
);
394 PATCH_OPTION(stmt_delete_prop
);
395 PATCH_OPTION(stmt_select_prop
);
396 PATCH_OPTION(stmt_select_propnames
);
398 PATCH_OPTION(stmt_delete_uri
);
399 PATCH_OPTION(stmt_move_uri
);
400 PATCH_OPTION(stmt_copy_uri
);
402 PATCH_OPTION(stmt_remove_lock
);
403 PATCH_OPTION(stmt_refresh_lock
);
404 PATCH_OPTION(stmt_create_lock
);
405 PATCH_OPTION(stmt_read_lock
);
406 PATCH_OPTION(stmt_read_lock_by_uri
);
408 /* skip the first, the global context */
409 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
410 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
411 s
= p
->config_storage
[i
];
413 /* condition didn't match */
414 if (!config_check_cond(srv
, con
, dc
)) continue;
417 for (j
= 0; j
< dc
->value
->used
; j
++) {
418 data_unset
*du
= dc
->value
->data
[j
];
420 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("webdav.activate"))) {
421 PATCH_OPTION(enabled
);
422 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("webdav.is-readonly"))) {
423 PATCH_OPTION(is_readonly
);
424 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("webdav.log-xml"))) {
425 PATCH_OPTION(log_xml
);
426 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("webdav.sqlite-db-name"))) {
429 PATCH_OPTION(stmt_update_prop
);
430 PATCH_OPTION(stmt_delete_prop
);
431 PATCH_OPTION(stmt_select_prop
);
432 PATCH_OPTION(stmt_select_propnames
);
434 PATCH_OPTION(stmt_delete_uri
);
435 PATCH_OPTION(stmt_move_uri
);
436 PATCH_OPTION(stmt_copy_uri
);
438 PATCH_OPTION(stmt_remove_lock
);
439 PATCH_OPTION(stmt_refresh_lock
);
440 PATCH_OPTION(stmt_create_lock
);
441 PATCH_OPTION(stmt_read_lock
);
442 PATCH_OPTION(stmt_read_lock_by_uri
);
451 URIHANDLER_FUNC(mod_webdav_uri_handler
) {
452 plugin_data
*p
= p_d
;
456 if (buffer_is_empty(con
->uri
.path
)) return HANDLER_GO_ON
;
458 mod_webdav_patch_connection(srv
, con
, p
);
460 if (!p
->conf
.enabled
) return HANDLER_GO_ON
;
462 switch (con
->request
.http_method
) {
463 case HTTP_METHOD_OPTIONS
:
464 /* we fake a little bit but it makes MS W2k happy and it let's us mount the volume */
465 response_header_overwrite(srv
, con
, CONST_STR_LEN("DAV"), CONST_STR_LEN("1,2"));
466 response_header_overwrite(srv
, con
, CONST_STR_LEN("MS-Author-Via"), CONST_STR_LEN("DAV"));
468 if (p
->conf
.is_readonly
) {
469 response_header_insert(srv
, con
, CONST_STR_LEN("Allow"), CONST_STR_LEN("PROPFIND"));
471 response_header_insert(srv
, con
, CONST_STR_LEN("Allow"), CONST_STR_LEN("PROPFIND, DELETE, MKCOL, PUT, MOVE, COPY, PROPPATCH, LOCK, UNLOCK"));
479 return HANDLER_GO_ON
;
481 static int webdav_gen_prop_tag(server
*srv
, connection
*con
,
491 buffer_append_string_len(b
,CONST_STR_LEN("<"));
492 buffer_append_string(b
, prop_name
);
493 buffer_append_string_len(b
, CONST_STR_LEN(" xmlns=\""));
494 buffer_append_string(b
, prop_ns
);
495 buffer_append_string_len(b
, CONST_STR_LEN("\">"));
497 buffer_append_string(b
, value
);
499 buffer_append_string_len(b
,CONST_STR_LEN("</"));
500 buffer_append_string(b
, prop_name
);
501 buffer_append_string_len(b
, CONST_STR_LEN(">"));
503 buffer_append_string_len(b
,CONST_STR_LEN("<"));
504 buffer_append_string(b
, prop_name
);
505 buffer_append_string_len(b
, CONST_STR_LEN(" xmlns=\""));
506 buffer_append_string(b
, prop_ns
);
507 buffer_append_string_len(b
, CONST_STR_LEN("\"/>"));
514 static int webdav_gen_response_status_tag(server
*srv
, connection
*con
, physical
*dst
, int status
, buffer
*b
) {
517 buffer_append_string_len(b
,CONST_STR_LEN("<D:response xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\">\n"));
519 buffer_append_string_len(b
,CONST_STR_LEN("<D:href>\n"));
520 buffer_append_string_buffer(b
, dst
->rel_path
);
521 buffer_append_string_len(b
,CONST_STR_LEN("</D:href>\n"));
522 buffer_append_string_len(b
,CONST_STR_LEN("<D:status>\n"));
524 if (con
->request
.http_version
== HTTP_VERSION_1_1
) {
525 buffer_copy_string_len(b
, CONST_STR_LEN("HTTP/1.1 "));
527 buffer_copy_string_len(b
, CONST_STR_LEN("HTTP/1.0 "));
529 buffer_append_int(b
, status
);
530 buffer_append_string_len(b
, CONST_STR_LEN(" "));
531 buffer_append_string(b
, get_http_status_name(status
));
533 buffer_append_string_len(b
,CONST_STR_LEN("</D:status>\n"));
534 buffer_append_string_len(b
,CONST_STR_LEN("</D:response>\n"));
539 static int webdav_delete_file(server
*srv
, connection
*con
, handler_ctx
*hctx
, physical
*dst
, buffer
*b
) {
542 /* try to unlink it */
543 if (-1 == unlink(dst
->path
->ptr
)) {
554 webdav_gen_response_status_tag(srv
, con
, dst
, status
, b
);
557 sqlite3_stmt
*stmt
= hctx
->conf
.stmt_delete_uri
;
561 webdav_gen_response_status_tag(srv
, con
, dst
, status
, b
);
565 /* bind the values to the insert */
567 sqlite3_bind_text(stmt
, 1,
568 CONST_BUF_LEN(dst
->rel_path
),
571 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
580 return (status
!= 0);
583 static int webdav_delete_dir(server
*srv
, connection
*con
, handler_ctx
*hctx
, physical
*dst
, buffer
*b
) {
585 int have_multi_status
= 0;
588 d
.path
= buffer_init();
589 d
.rel_path
= buffer_init();
591 if (NULL
!= (dir
= opendir(dst
->path
->ptr
))) {
594 while(NULL
!= (de
= readdir(dir
))) {
597 if ((de
->d_name
[0] == '.' && de
->d_name
[1] == '\0') ||
598 (de
->d_name
[0] == '.' && de
->d_name
[1] == '.' && de
->d_name
[2] == '\0')) {
600 /* ignore the parent dir */
603 buffer_copy_buffer(d
.path
, dst
->path
);
604 buffer_append_slash(d
.path
);
605 buffer_append_string(d
.path
, de
->d_name
);
607 buffer_copy_buffer(d
.rel_path
, dst
->rel_path
);
608 buffer_append_slash(d
.rel_path
);
609 buffer_append_string(d
.rel_path
, de
->d_name
);
611 /* stat and unlink afterwards */
612 if (-1 == stat(d
.path
->ptr
, &st
)) {
613 /* don't about it yet, rmdir will fail too */
614 } else if (S_ISDIR(st
.st_mode
)) {
615 have_multi_status
= webdav_delete_dir(srv
, con
, hctx
, &d
, b
);
617 /* try to unlink it */
618 if (-1 == rmdir(d
.path
->ptr
)) {
630 have_multi_status
= 1;
632 webdav_gen_response_status_tag(srv
, con
, &d
, status
, b
);
635 sqlite3_stmt
*stmt
= hctx
->conf
.stmt_delete_uri
;
640 /* bind the values to the insert */
642 sqlite3_bind_text(stmt
, 1,
643 CONST_BUF_LEN(d
.rel_path
),
646 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
653 have_multi_status
= webdav_delete_file(srv
, con
, hctx
, &d
, b
);
659 buffer_free(d
.rel_path
);
662 return have_multi_status
;
665 /* don't want to block when open()ing a fifo */
666 #if defined(O_NONBLOCK)
667 # define FIFO_NONBLOCK O_NONBLOCK
669 # define FIFO_NONBLOCK 0
676 static int webdav_copy_file(server
*srv
, connection
*con
, handler_ctx
*hctx
, physical
*src
, physical
*dst
, int overwrite
) {
678 ssize_t rd
, wr
, offset
;
679 int status
= 0, ifd
, ofd
;
683 if (-1 == (ifd
= open(src
->path
->ptr
, O_RDONLY
| O_BINARY
| FIFO_NONBLOCK
))) {
687 if (-1 == (ofd
= open(dst
->path
->ptr
, O_WRONLY
|O_TRUNC
|O_CREAT
|(overwrite
? 0 : O_EXCL
), WEBDAV_FILE_MODE
))) {
688 /* opening the destination failed for some reason */
697 /* at least one part in the middle wasn't existing */
708 data
= malloc(131072);
711 while (0 < (rd
= read(ifd
, data
, 131072))) {
714 wr
= write(ofd
, data
+offset
, (size_t)(rd
-offset
));
715 } while (wr
>= 0 ? (offset
+= wr
) != rd
: (errno
== EINTR
));
717 status
= (errno
== ENOSPC
) ? 507 : 403;
722 if (0 != rd
&& 0 == status
) status
= 403;
726 if (0 != close(ofd
)) {
727 if (0 == status
) status
= (errno
== ENOSPC
) ? 507 : 403;
728 log_error_write(srv
, __FILE__
, __LINE__
, "sbss",
729 "close ", dst
->path
, "failed: ", strerror(errno
));
734 /* copy worked fine, copy connected properties */
735 sqlite3_stmt
*stmt
= hctx
->conf
.stmt_copy_uri
;
740 /* bind the values to the insert */
741 sqlite3_bind_text(stmt
, 1,
742 CONST_BUF_LEN(dst
->rel_path
),
745 sqlite3_bind_text(stmt
, 2,
746 CONST_BUF_LEN(src
->rel_path
),
749 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
760 static int webdav_copy_dir(server
*srv
, connection
*con
, handler_ctx
*hctx
, physical
*src
, physical
*dst
, int overwrite
) {
764 if (NULL
!= (srcdir
= opendir(src
->path
->ptr
))) {
768 s
.path
= buffer_init();
769 s
.rel_path
= buffer_init();
771 d
.path
= buffer_init();
772 d
.rel_path
= buffer_init();
774 while (NULL
!= (de
= readdir(srcdir
))) {
777 if ((de
->d_name
[0] == '.' && de
->d_name
[1] == '\0')
778 || (de
->d_name
[0] == '.' && de
->d_name
[1] == '.' && de
->d_name
[2] == '\0')) {
782 buffer_copy_buffer(s
.path
, src
->path
);
783 buffer_append_slash(s
.path
);
784 buffer_append_string(s
.path
, de
->d_name
);
786 buffer_copy_buffer(d
.path
, dst
->path
);
787 buffer_append_slash(d
.path
);
788 buffer_append_string(d
.path
, de
->d_name
);
790 buffer_copy_buffer(s
.rel_path
, src
->rel_path
);
791 buffer_append_slash(s
.rel_path
);
792 buffer_append_string(s
.rel_path
, de
->d_name
);
794 buffer_copy_buffer(d
.rel_path
, dst
->rel_path
);
795 buffer_append_slash(d
.rel_path
);
796 buffer_append_string(d
.rel_path
, de
->d_name
);
798 if (-1 == stat(s
.path
->ptr
, &st
)) {
800 } else if (S_ISDIR(st
.st_mode
)) {
802 if (-1 == mkdir(d
.path
->ptr
, WEBDAV_DIR_MODE
) &&
807 sqlite3_stmt
*stmt
= hctx
->conf
.stmt_copy_uri
;
809 if (0 != (status
= webdav_copy_dir(srv
, con
, hctx
, &s
, &d
, overwrite
))) {
812 /* directory is copied, copy the properties too */
817 /* bind the values to the insert */
818 sqlite3_bind_text(stmt
, 1,
819 CONST_BUF_LEN(dst
->rel_path
),
822 sqlite3_bind_text(stmt
, 2,
823 CONST_BUF_LEN(src
->rel_path
),
826 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
832 } else if (S_ISREG(st
.st_mode
)) {
834 if (0 != (status
= webdav_copy_file(srv
, con
, hctx
, &s
, &d
, overwrite
))) {
841 buffer_free(s
.rel_path
);
843 buffer_free(d
.rel_path
);
852 static void webdav_activelock(buffer
*b
,
853 const buffer
*locktoken
, const char *lockscope
, const char *locktype
, int depth
, int timeout
) {
854 buffer_append_string_len(b
, CONST_STR_LEN("<D:activelock>\n"));
856 buffer_append_string_len(b
, CONST_STR_LEN("<D:lockscope>"));
857 buffer_append_string_len(b
, CONST_STR_LEN("<D:"));
858 buffer_append_string(b
, lockscope
);
859 buffer_append_string_len(b
, CONST_STR_LEN("/>"));
860 buffer_append_string_len(b
, CONST_STR_LEN("</D:lockscope>\n"));
862 buffer_append_string_len(b
, CONST_STR_LEN("<D:locktype>"));
863 buffer_append_string_len(b
, CONST_STR_LEN("<D:"));
864 buffer_append_string(b
, locktype
);
865 buffer_append_string_len(b
, CONST_STR_LEN("/>"));
866 buffer_append_string_len(b
, CONST_STR_LEN("</D:locktype>\n"));
868 buffer_append_string_len(b
, CONST_STR_LEN("<D:depth>"));
869 buffer_append_string(b
, depth
== 0 ? "0" : "infinity");
870 buffer_append_string_len(b
, CONST_STR_LEN("</D:depth>\n"));
872 buffer_append_string_len(b
, CONST_STR_LEN("<D:timeout>"));
873 buffer_append_string_len(b
, CONST_STR_LEN("Second-"));
874 buffer_append_int(b
, timeout
);
875 buffer_append_string_len(b
, CONST_STR_LEN("</D:timeout>\n"));
877 buffer_append_string_len(b
, CONST_STR_LEN("<D:owner>"));
878 buffer_append_string_len(b
, CONST_STR_LEN("</D:owner>\n"));
880 buffer_append_string_len(b
, CONST_STR_LEN("<D:locktoken>"));
881 buffer_append_string_len(b
, CONST_STR_LEN("<D:href>"));
882 buffer_append_string_buffer(b
, locktoken
);
883 buffer_append_string_len(b
, CONST_STR_LEN("</D:href>"));
884 buffer_append_string_len(b
, CONST_STR_LEN("</D:locktoken>\n"));
886 buffer_append_string_len(b
, CONST_STR_LEN("</D:activelock>\n"));
889 static void webdav_get_live_property_lockdiscovery(server
*srv
, connection
*con
, handler_ctx
*hctx
, physical
*dst
, buffer
*b
) {
891 sqlite3_stmt
*stmt
= hctx
->conf
.stmt_read_lock_by_uri
;
892 if (!stmt
) { /*(should not happen)*/
893 buffer_append_string_len(b
, CONST_STR_LEN("<D:lockdiscovery>\n</D:lockdiscovery>\n"));
899 /* SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout
901 * WHERE resource = ? */
905 sqlite3_bind_text(stmt
, 1,
906 CONST_BUF_LEN(dst
->rel_path
),
909 buffer_append_string_len(b
, CONST_STR_LEN("<D:lockdiscovery>\n"));
910 while (SQLITE_ROW
== sqlite3_step(stmt
)) {
911 const char *lockscope
= (const char *)sqlite3_column_text(stmt
, 2);
912 const char *locktype
= (const char *)sqlite3_column_text(stmt
, 3);
913 const int depth
= sqlite3_column_int(stmt
, 5);
914 const int timeout
= sqlite3_column_int(stmt
, 6);
915 buffer locktoken
= { NULL
, 0, 0 };
916 locktoken
.ptr
= (char *)sqlite3_column_text(stmt
, 0);
917 locktoken
.used
= sqlite3_column_bytes(stmt
, 0);
918 if (locktoken
.used
) ++locktoken
.used
;
919 locktoken
.size
= locktoken
.used
;
922 webdav_activelock(b
, &locktoken
, lockscope
, locktype
, depth
, timeout
);
925 buffer_append_string_len(b
, CONST_STR_LEN("</D:lockdiscovery>\n"));
929 static int webdav_get_live_property(server
*srv
, connection
*con
, handler_ctx
*hctx
, physical
*dst
, char *prop_name
, buffer
*b
) {
930 stat_cache_entry
*sce
= NULL
;
935 if (HANDLER_ERROR
!= (stat_cache_get_entry(srv
, con
, dst
->path
, &sce
))) {
936 char ctime_buf
[] = "2005-08-18T07:27:16Z";
937 char mtime_buf
[] = "Thu, 18 Aug 2005 07:27:16 GMT";
940 if (0 == strcmp(prop_name
, "resourcetype")) {
941 if (S_ISDIR(sce
->st
.st_mode
)) {
942 buffer_append_string_len(b
, CONST_STR_LEN("<D:resourcetype><D:collection/></D:resourcetype>"));
944 buffer_append_string_len(b
, CONST_STR_LEN("<D:resourcetype/>"));
947 } else if (0 == strcmp(prop_name
, "getcontenttype")) {
948 if (S_ISDIR(sce
->st
.st_mode
)) {
949 buffer_append_string_len(b
, CONST_STR_LEN("<D:getcontenttype>httpd/unix-directory</D:getcontenttype>"));
951 } else if(S_ISREG(sce
->st
.st_mode
)) {
952 for (k
= 0; k
< con
->conf
.mimetypes
->used
; k
++) {
953 data_string
*ds
= (data_string
*)con
->conf
.mimetypes
->data
[k
];
955 if (buffer_is_empty(ds
->key
)) continue;
957 if (buffer_is_equal_right_len(dst
->path
, ds
->key
, buffer_string_length(ds
->key
))) {
958 buffer_append_string_len(b
,CONST_STR_LEN("<D:getcontenttype>"));
959 buffer_append_string_buffer(b
, ds
->value
);
960 buffer_append_string_len(b
, CONST_STR_LEN("</D:getcontenttype>"));
967 } else if (0 == strcmp(prop_name
, "creationdate")) {
968 buffer_append_string_len(b
, CONST_STR_LEN("<D:creationdate ns0:dt=\"dateTime.tz\">"));
969 strftime(ctime_buf
, sizeof(ctime_buf
), "%Y-%m-%dT%H:%M:%SZ", gmtime(&(sce
->st
.st_ctime
)));
970 buffer_append_string(b
, ctime_buf
);
971 buffer_append_string_len(b
, CONST_STR_LEN("</D:creationdate>"));
973 } else if (0 == strcmp(prop_name
, "getlastmodified")) {
974 buffer_append_string_len(b
,CONST_STR_LEN("<D:getlastmodified ns0:dt=\"dateTime.rfc1123\">"));
975 strftime(mtime_buf
, sizeof(mtime_buf
), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&(sce
->st
.st_mtime
)));
976 buffer_append_string(b
, mtime_buf
);
977 buffer_append_string_len(b
, CONST_STR_LEN("</D:getlastmodified>"));
979 } else if (0 == strcmp(prop_name
, "getcontentlength")) {
980 buffer_append_string_len(b
,CONST_STR_LEN("<D:getcontentlength>"));
981 buffer_append_int(b
, sce
->st
.st_size
);
982 buffer_append_string_len(b
, CONST_STR_LEN("</D:getcontentlength>"));
984 } else if (0 == strcmp(prop_name
, "getcontentlanguage")) {
985 buffer_append_string_len(b
,CONST_STR_LEN("<D:getcontentlanguage>"));
986 buffer_append_string_len(b
, CONST_STR_LEN("en"));
987 buffer_append_string_len(b
, CONST_STR_LEN("</D:getcontentlanguage>"));
989 } else if (0 == strcmp(prop_name
, "getetag")) {
990 etag_create(con
->physical
.etag
, &sce
->st
, con
->etag_flags
);
991 buffer_append_string_len(b
, CONST_STR_LEN("<D:getetag>"));
992 buffer_append_string_buffer(b
, con
->physical
.etag
);
993 buffer_append_string_len(b
, CONST_STR_LEN("</D:getetag>"));
994 buffer_reset(con
->physical
.etag
);
997 } else if (0 == strcmp(prop_name
, "lockdiscovery")) {
998 webdav_get_live_property_lockdiscovery(srv
, con
, hctx
, dst
, b
);
1000 } else if (0 == strcmp(prop_name
, "supportedlock")) {
1001 buffer_append_string_len(b
,CONST_STR_LEN("<D:supportedlock>"));
1002 buffer_append_string_len(b
,CONST_STR_LEN("<D:lockentry>"));
1003 buffer_append_string_len(b
,CONST_STR_LEN("<D:lockscope><D:exclusive/></D:lockscope>"));
1004 buffer_append_string_len(b
,CONST_STR_LEN("<D:locktype><D:write/></D:locktype>"));
1005 buffer_append_string_len(b
,CONST_STR_LEN("</D:lockentry>"));
1006 buffer_append_string_len(b
, CONST_STR_LEN("</D:supportedlock>"));
1012 return found
? 0 : -1;
1015 static int webdav_get_property(server
*srv
, connection
*con
, handler_ctx
*hctx
, physical
*dst
, char *prop_name
, char *prop_ns
, buffer
*b
) {
1016 if (0 == strcmp(prop_ns
, "DAV:")) {
1017 /* a local 'live' property */
1018 return webdav_get_live_property(srv
, con
, hctx
, dst
, prop_name
, b
);
1021 #ifdef USE_PROPPATCH
1022 sqlite3_stmt
*stmt
= hctx
->conf
.stmt_select_prop
;
1025 /* perhaps it is in sqlite3 */
1026 sqlite3_reset(stmt
);
1028 /* bind the values to the insert */
1030 sqlite3_bind_text(stmt
, 1,
1031 CONST_BUF_LEN(dst
->rel_path
),
1033 sqlite3_bind_text(stmt
, 2,
1037 sqlite3_bind_text(stmt
, 3,
1043 while (SQLITE_ROW
== sqlite3_step(stmt
)) {
1044 /* there is a row for us, we only expect a single col 'value' */
1045 webdav_gen_prop_tag(srv
, con
, prop_name
, prop_ns
, (char *)sqlite3_column_text(stmt
, 0), b
);
1050 return found
? 0 : -1;
1062 static webdav_property live_properties
[] = {
1063 { "DAV:", "creationdate" },
1064 /*{ "DAV:", "displayname" },*//*(not implemented)*/
1065 { "DAV:", "getcontentlanguage" },
1066 { "DAV:", "getcontentlength" },
1067 { "DAV:", "getcontenttype" },
1068 { "DAV:", "getetag" },
1069 { "DAV:", "getlastmodified" },
1070 { "DAV:", "resourcetype" },
1071 /*{ "DAV:", "source" },*//*(not implemented)*/
1073 { "DAV:", "lockdiscovery" },
1074 { "DAV:", "supportedlock" },
1081 webdav_property
**ptr
;
1085 } webdav_properties
;
1087 static int webdav_get_props(server
*srv
, connection
*con
, handler_ctx
*hctx
, physical
*dst
, webdav_properties
*props
, buffer
*b_200
, buffer
*b_404
) {
1090 if (props
&& props
->used
) {
1091 for (i
= 0; i
< props
->used
; i
++) {
1092 webdav_property
*prop
;
1094 prop
= props
->ptr
[i
];
1096 if (0 != webdav_get_property(srv
, con
, hctx
,
1097 dst
, prop
->prop
, prop
->ns
, b_200
)) {
1098 webdav_gen_prop_tag(srv
, con
, prop
->prop
, prop
->ns
, NULL
, b_404
);
1102 for (i
= 0; live_properties
[i
].prop
; i
++) {
1103 /* a local 'live' property */
1104 webdav_get_live_property(srv
, con
, hctx
, dst
, live_properties
[i
].prop
, b_200
);
1111 #ifdef USE_PROPPATCH
1112 static int webdav_parse_chunkqueue(server
*srv
, connection
*con
, handler_ctx
*hctx
, chunkqueue
*cq
, xmlDoc
**ret_xml
) {
1113 xmlParserCtxtPtr ctxt
;
1122 /* read the chunks in to the XML document */
1123 ctxt
= xmlCreatePushParserCtxt(NULL
, NULL
, NULL
, 0, NULL
);
1125 for (c
= cq
->first
; cq
->bytes_out
!= cq
->bytes_in
; c
= cq
->first
) {
1126 size_t weWant
= cq
->bytes_out
- cq
->bytes_in
;
1133 weHave
= c
->file
.length
- c
->offset
;
1135 if (weHave
> weWant
) weHave
= weWant
;
1137 /* xml chunks are always memory, mmap() is our friend */
1138 mapped
= (c
->file
.mmap
.start
!= MAP_FAILED
);
1140 data
= c
->file
.mmap
.start
+ c
->offset
;
1142 if (-1 == c
->file
.fd
&& /* open the file if not already open */
1143 -1 == (c
->file
.fd
= open(c
->file
.name
->ptr
, O_RDONLY
))) {
1144 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "open failed: ", strerror(errno
));
1149 if (MAP_FAILED
!= (c
->file
.mmap
.start
= mmap(0, c
->file
.length
, PROT_READ
, MAP_PRIVATE
, c
->file
.fd
, 0))) {
1150 /* chunk_reset() or chunk_free() will cleanup for us */
1151 c
->file
.mmap
.length
= c
->file
.length
;
1152 data
= c
->file
.mmap
.start
+ c
->offset
;
1156 if (weHave
> 65536) weHave
= 65536;
1157 data
= malloc(weHave
);
1159 if (-1 == lseek(c
->file
.fd
, c
->file
.start
+ c
->offset
, SEEK_SET
)
1160 || 0 > (rd
= read(c
->file
.fd
, data
, weHave
))) {
1161 log_error_write(srv
, __FILE__
, __LINE__
, "ssbd", "lseek/read failed: ",
1162 strerror(errno
), c
->file
.name
, c
->file
.fd
);
1166 weHave
= (size_t)rd
;
1170 if (XML_ERR_OK
!= (err
= xmlParseChunk(ctxt
, data
, weHave
, 0))) {
1171 log_error_write(srv
, __FILE__
, __LINE__
, "sodd", "xmlParseChunk failed at:", cq
->bytes_out
, weHave
, err
);
1174 chunkqueue_mark_written(cq
, weHave
);
1176 if (!mapped
) free(data
);
1179 /* append to the buffer */
1180 weHave
= buffer_string_length(c
->mem
) - c
->offset
;
1182 if (weHave
> weWant
) weHave
= weWant
;
1184 if (hctx
->conf
.log_xml
) {
1185 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "XML-request-body:", c
->mem
->ptr
+ c
->offset
);
1188 if (XML_ERR_OK
!= (err
= xmlParseChunk(ctxt
, c
->mem
->ptr
+ c
->offset
, weHave
, 0))) {
1189 log_error_write(srv
, __FILE__
, __LINE__
, "sodd", "xmlParseChunk failed at:", cq
->bytes_out
, weHave
, err
);
1192 chunkqueue_mark_written(cq
, weHave
);
1198 switch ((err
= xmlParseChunk(ctxt
, 0, 0, 1))) {
1199 case XML_ERR_DOCUMENT_END
:
1203 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "xmlParseChunk failed at final packet:", err
);
1208 res
= ctxt
->wellFormed
;
1209 xmlFreeParserCtxt(ctxt
);
1222 static int webdav_lockdiscovery(server
*srv
, connection
*con
,
1223 buffer
*locktoken
, const char *lockscope
, const char *locktype
, int depth
) {
1225 buffer
*b
= buffer_init();
1227 response_header_overwrite(srv
, con
, CONST_STR_LEN("Lock-Token"), CONST_BUF_LEN(locktoken
));
1229 response_header_overwrite(srv
, con
,
1230 CONST_STR_LEN("Content-Type"),
1231 CONST_STR_LEN("text/xml; charset=\"utf-8\""));
1233 buffer_copy_string_len(b
, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"));
1235 buffer_append_string_len(b
,CONST_STR_LEN("<D:prop xmlns:D=\"DAV:\" xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\">\n"));
1236 buffer_append_string_len(b
,CONST_STR_LEN("<D:lockdiscovery>\n"));
1237 webdav_activelock(b
, locktoken
, lockscope
, locktype
, depth
, 600);
1238 buffer_append_string_len(b
,CONST_STR_LEN("</D:lockdiscovery>\n"));
1239 buffer_append_string_len(b
,CONST_STR_LEN("</D:prop>\n"));
1241 chunkqueue_append_buffer(con
->write_queue
, b
);
1249 * check if resource is having the right locks to access to resource
1254 static int webdav_has_lock(server
*srv
, connection
*con
, handler_ctx
*hctx
, buffer
*uri
) {
1262 * This implementation is more fake than real
1263 * we need a parser for the If: header to really handle the full scope
1265 * X-Litmus: locks: 11 (owner_modify)
1266 * If: <http://127.0.0.1:1025/dav/litmus/lockme> (<opaquelocktoken:2165478d-0611-49c4-be92-e790d68a38f1>)
1268 * if http://127.0.0.1:1025/dav/litmus/lockme is locked with
1269 * opaquelocktoken:2165478d-0611-49c4-be92-e790d68a38f1, go on
1271 * X-Litmus: locks: 16 (fail_cond_put)
1272 * If: (<DAV:no-lock> ["-1622396671"])
1274 * go on if the resource has the etag [...] and the lock
1276 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "If"))) {
1277 /* Ooh, ooh. A if tag, now the fun begins.
1279 * this can only work with a real parser
1282 /* we didn't provided a lock-token -> */
1283 /* if the resource is locked -> 423 */
1285 sqlite3_stmt
*stmt
= hctx
->conf
.stmt_read_lock_by_uri
;
1287 sqlite3_reset(stmt
);
1289 sqlite3_bind_text(stmt
, 1,
1293 while (SQLITE_ROW
== sqlite3_step(stmt
)) {
1308 SUBREQUEST_FUNC(mod_webdav_subrequest_handler_huge
) {
1309 plugin_data
*p
= p_d
;
1310 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
1314 int depth
= -1; /* (Depth: infinity) */
1318 webdav_properties
*req_props
;
1319 stat_cache_entry
*sce
= NULL
;
1323 if (NULL
== hctx
) return HANDLER_GO_ON
;
1324 if (!hctx
->conf
.enabled
) return HANDLER_GO_ON
;
1325 /* physical path is setup */
1326 if (buffer_is_empty(con
->physical
.path
)) return HANDLER_GO_ON
;
1328 /* PROPFIND need them */
1329 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "Depth")) && 1 == buffer_string_length(ds
->value
)) {
1330 if ('0' == *ds
->value
->ptr
) {
1332 } else if ('1' == *ds
->value
->ptr
) {
1335 } /* else treat as Depth: infinity */
1337 switch (con
->request
.http_method
) {
1338 case HTTP_METHOD_PROPFIND
:
1339 /* they want to know the properties of the directory */
1342 /* is there a content-body ? */
1344 switch (stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) {
1346 if (errno
== ENOENT
) {
1347 con
->http_status
= 404;
1348 return HANDLER_FINISHED
;
1355 if (S_ISDIR(sce
->st
.st_mode
) && con
->physical
.path
->ptr
[buffer_string_length(con
->physical
.path
)-1] != '/') {
1356 http_response_redirect_to_directory(srv
, con
);
1357 return HANDLER_FINISHED
;
1360 #ifdef USE_PROPPATCH
1361 /* any special requests or just allprop ? */
1362 if (con
->request
.content_length
) {
1365 if (con
->state
== CON_STATE_READ_POST
) {
1366 handler_t r
= connection_handle_read_post_state(srv
, con
);
1367 if (r
!= HANDLER_GO_ON
) return r
;
1370 if (1 == webdav_parse_chunkqueue(srv
, con
, hctx
, con
->request_content_queue
, &xml
)) {
1371 xmlNode
*rootnode
= xmlDocGetRootElement(xml
);
1373 force_assert(rootnode
);
1375 if (0 == xmlStrcmp(rootnode
->name
, BAD_CAST
"propfind")) {
1378 req_props
= calloc(1, sizeof(*req_props
));
1380 for (cmd
= rootnode
->children
; cmd
; cmd
= cmd
->next
) {
1382 if (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"prop")) {
1383 /* get prop by name */
1386 for (prop
= cmd
->children
; prop
; prop
= prop
->next
) {
1387 if (prop
->type
== XML_TEXT_NODE
) continue; /* ignore WS */
1390 (0 == xmlStrcmp(prop
->ns
->href
, BAD_CAST
"")) &&
1391 (0 != xmlStrcmp(prop
->ns
->prefix
, BAD_CAST
""))) {
1393 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
1394 "no name space for:",
1399 for (i
= 0; i
< req_props
->used
; i
++) {
1400 free(req_props
->ptr
[i
]->ns
);
1401 free(req_props
->ptr
[i
]->prop
);
1402 free(req_props
->ptr
[i
]);
1404 free(req_props
->ptr
);
1407 con
->http_status
= 400;
1408 return HANDLER_FINISHED
;
1411 /* add property to requested list */
1412 if (req_props
->size
== 0) {
1413 req_props
->size
= 16;
1414 req_props
->ptr
= malloc(sizeof(*(req_props
->ptr
)) * req_props
->size
);
1415 } else if (req_props
->used
== req_props
->size
) {
1416 req_props
->size
+= 16;
1417 req_props
->ptr
= realloc(req_props
->ptr
, sizeof(*(req_props
->ptr
)) * req_props
->size
);
1420 req_props
->ptr
[req_props
->used
] = malloc(sizeof(webdav_property
));
1421 req_props
->ptr
[req_props
->used
]->ns
= (char *)xmlStrdup(prop
->ns
? prop
->ns
->href
: (xmlChar
*)"");
1422 req_props
->ptr
[req_props
->used
]->prop
= (char *)xmlStrdup(prop
->name
);
1425 } else if (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"propname")) {
1426 sqlite3_stmt
*stmt
= p
->conf
.stmt_select_propnames
;
1429 /* get all property names (EMPTY) */
1430 sqlite3_reset(stmt
);
1431 /* bind the values to the insert */
1433 sqlite3_bind_text(stmt
, 1,
1434 CONST_BUF_LEN(con
->uri
.path
),
1437 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
1440 } else if (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"allprop")) {
1441 /* get all properties (EMPTY) */
1448 con
->http_status
= 400;
1449 return HANDLER_FINISHED
;
1453 con
->http_status
= 207;
1455 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/xml; charset=\"utf-8\""));
1459 buffer_copy_string_len(b
, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"));
1461 buffer_append_string_len(b
,CONST_STR_LEN("<D:multistatus xmlns:D=\"DAV:\" xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\">\n"));
1465 prop_200
= buffer_init();
1466 prop_404
= buffer_init();
1469 /* Depth: 0 or Depth: 1 */
1470 webdav_get_props(srv
, con
, hctx
, &(con
->physical
), req_props
, prop_200
, prop_404
);
1472 buffer_append_string_len(b
,CONST_STR_LEN("<D:response>\n"));
1473 buffer_append_string_len(b
,CONST_STR_LEN("<D:href>"));
1474 buffer_append_string_buffer(b
, con
->uri
.scheme
);
1475 buffer_append_string_len(b
,CONST_STR_LEN("://"));
1476 buffer_append_string_buffer(b
, con
->uri
.authority
);
1477 buffer_append_string_encoded(b
, CONST_BUF_LEN(con
->uri
.path
), ENCODING_REL_URI
);
1478 buffer_append_string_len(b
,CONST_STR_LEN("</D:href>\n"));
1480 if (!buffer_string_is_empty(prop_200
)) {
1481 buffer_append_string_len(b
,CONST_STR_LEN("<D:propstat>\n"));
1482 buffer_append_string_len(b
,CONST_STR_LEN("<D:prop>\n"));
1484 buffer_append_string_buffer(b
, prop_200
);
1486 buffer_append_string_len(b
,CONST_STR_LEN("</D:prop>\n"));
1488 buffer_append_string_len(b
,CONST_STR_LEN("<D:status>HTTP/1.1 200 OK</D:status>\n"));
1490 buffer_append_string_len(b
,CONST_STR_LEN("</D:propstat>\n"));
1492 if (!buffer_string_is_empty(prop_404
)) {
1493 buffer_append_string_len(b
,CONST_STR_LEN("<D:propstat>\n"));
1494 buffer_append_string_len(b
,CONST_STR_LEN("<D:prop>\n"));
1496 buffer_append_string_buffer(b
, prop_404
);
1498 buffer_append_string_len(b
,CONST_STR_LEN("</D:prop>\n"));
1500 buffer_append_string_len(b
,CONST_STR_LEN("<D:status>HTTP/1.1 404 Not Found</D:status>\n"));
1502 buffer_append_string_len(b
,CONST_STR_LEN("</D:propstat>\n"));
1505 buffer_append_string_len(b
,CONST_STR_LEN("</D:response>\n"));
1510 if (NULL
!= (dir
= opendir(con
->physical
.path
->ptr
))) {
1513 physical
*dst
= &(con
->physical
);
1515 d
.path
= buffer_init();
1516 d
.rel_path
= buffer_init();
1518 while(NULL
!= (de
= readdir(dir
))) {
1519 if (de
->d_name
[0] == '.' && (de
->d_name
[1] == '\0' || (de
->d_name
[1] == '.' && de
->d_name
[2] == '\0'))) {
1521 /* ignore the parent and target dir */
1524 buffer_copy_buffer(d
.path
, dst
->path
);
1525 buffer_append_slash(d
.path
);
1527 buffer_copy_buffer(d
.rel_path
, dst
->rel_path
);
1528 buffer_append_slash(d
.rel_path
);
1530 buffer_append_string(d
.path
, de
->d_name
);
1531 buffer_append_string(d
.rel_path
, de
->d_name
);
1533 buffer_reset(prop_200
);
1534 buffer_reset(prop_404
);
1536 webdav_get_props(srv
, con
, hctx
, &d
, req_props
, prop_200
, prop_404
);
1538 buffer_append_string_len(b
,CONST_STR_LEN("<D:response>\n"));
1539 buffer_append_string_len(b
,CONST_STR_LEN("<D:href>"));
1540 buffer_append_string_buffer(b
, con
->uri
.scheme
);
1541 buffer_append_string_len(b
,CONST_STR_LEN("://"));
1542 buffer_append_string_buffer(b
, con
->uri
.authority
);
1543 buffer_append_string_encoded(b
, CONST_BUF_LEN(d
.rel_path
), ENCODING_REL_URI
);
1544 if (0 == stat(d
.path
->ptr
, &st
) && S_ISDIR(st
.st_mode
)) {
1545 /* Append a '/' on subdirectories */
1546 buffer_append_string_len(b
,CONST_STR_LEN("/"));
1548 buffer_append_string_len(b
,CONST_STR_LEN("</D:href>\n"));
1550 if (!buffer_string_is_empty(prop_200
)) {
1551 buffer_append_string_len(b
,CONST_STR_LEN("<D:propstat>\n"));
1552 buffer_append_string_len(b
,CONST_STR_LEN("<D:prop>\n"));
1554 buffer_append_string_buffer(b
, prop_200
);
1556 buffer_append_string_len(b
,CONST_STR_LEN("</D:prop>\n"));
1558 buffer_append_string_len(b
,CONST_STR_LEN("<D:status>HTTP/1.1 200 OK</D:status>\n"));
1560 buffer_append_string_len(b
,CONST_STR_LEN("</D:propstat>\n"));
1562 if (!buffer_string_is_empty(prop_404
)) {
1563 buffer_append_string_len(b
,CONST_STR_LEN("<D:propstat>\n"));
1564 buffer_append_string_len(b
,CONST_STR_LEN("<D:prop>\n"));
1566 buffer_append_string_buffer(b
, prop_404
);
1568 buffer_append_string_len(b
,CONST_STR_LEN("</D:prop>\n"));
1570 buffer_append_string_len(b
,CONST_STR_LEN("<D:status>HTTP/1.1 404 Not Found</D:status>\n"));
1572 buffer_append_string_len(b
,CONST_STR_LEN("</D:propstat>\n"));
1575 buffer_append_string_len(b
,CONST_STR_LEN("</D:response>\n"));
1578 buffer_free(d
.path
);
1579 buffer_free(d
.rel_path
);
1586 for (i
= 0; i
< req_props
->used
; i
++) {
1587 free(req_props
->ptr
[i
]->ns
);
1588 free(req_props
->ptr
[i
]->prop
);
1589 free(req_props
->ptr
[i
]);
1591 free(req_props
->ptr
);
1595 buffer_free(prop_200
);
1596 buffer_free(prop_404
);
1598 buffer_append_string_len(b
,CONST_STR_LEN("</D:multistatus>\n"));
1600 if (p
->conf
.log_xml
) {
1601 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "XML-response-body:", b
);
1604 chunkqueue_append_buffer(con
->write_queue
, b
);
1607 con
->file_finished
= 1;
1609 return HANDLER_FINISHED
;
1610 case HTTP_METHOD_MKCOL
:
1611 if (p
->conf
.is_readonly
) {
1612 con
->http_status
= 403;
1613 return HANDLER_FINISHED
;
1616 if (con
->request
.content_length
!= 0) {
1617 /* we don't support MKCOL with a body */
1618 con
->http_status
= 415;
1620 return HANDLER_FINISHED
;
1623 /* let's create the directory */
1625 if (-1 == mkdir(con
->physical
.path
->ptr
, WEBDAV_DIR_MODE
)) {
1628 con
->http_status
= 403;
1632 con
->http_status
= 409;
1636 con
->http_status
= 405; /* not allowed */
1640 con
->http_status
= 201;
1641 con
->file_finished
= 1;
1644 return HANDLER_FINISHED
;
1645 case HTTP_METHOD_DELETE
:
1646 if (p
->conf
.is_readonly
) {
1647 con
->http_status
= 403;
1648 return HANDLER_FINISHED
;
1651 /* does the client have a lock for this connection ? */
1652 if (!webdav_has_lock(srv
, con
, hctx
, con
->uri
.path
)) {
1653 con
->http_status
= 423;
1654 return HANDLER_FINISHED
;
1657 /* stat and unlink afterwards */
1658 if (-1 == stat(con
->physical
.path
->ptr
, &st
)) {
1659 /* don't about it yet, unlink will fail too */
1662 con
->http_status
= 404;
1665 con
->http_status
= 403;
1668 } else if (S_ISDIR(st
.st_mode
)) {
1669 buffer
*multi_status_resp
;
1671 if (con
->physical
.path
->ptr
[buffer_string_length(con
->physical
.path
)-1] != '/') {
1672 http_response_redirect_to_directory(srv
, con
);
1673 return HANDLER_FINISHED
;
1676 multi_status_resp
= buffer_init();
1678 if (webdav_delete_dir(srv
, con
, hctx
, &(con
->physical
), multi_status_resp
)) {
1679 /* we got an error somewhere in between, build a 207 */
1680 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/xml; charset=\"utf-8\""));
1684 buffer_copy_string_len(b
, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"));
1686 buffer_append_string_len(b
,CONST_STR_LEN("<D:multistatus xmlns:D=\"DAV:\">\n"));
1688 buffer_append_string_buffer(b
, multi_status_resp
);
1690 buffer_append_string_len(b
,CONST_STR_LEN("</D:multistatus>\n"));
1692 if (p
->conf
.log_xml
) {
1693 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "XML-response-body:", b
);
1696 chunkqueue_append_buffer(con
->write_queue
, b
);
1699 con
->http_status
= 207;
1700 con
->file_finished
= 1;
1702 /* everything went fine, remove the directory */
1704 if (-1 == rmdir(con
->physical
.path
->ptr
)) {
1707 con
->http_status
= 403;
1710 con
->http_status
= 404;
1713 con
->http_status
= 501;
1717 con
->http_status
= 204;
1721 buffer_free(multi_status_resp
);
1722 } else if (-1 == unlink(con
->physical
.path
->ptr
)) {
1725 con
->http_status
= 403;
1728 con
->http_status
= 404;
1731 con
->http_status
= 501;
1735 con
->http_status
= 204;
1737 return HANDLER_FINISHED
;
1738 case HTTP_METHOD_PUT
: {
1740 chunkqueue
*cq
= con
->request_content_queue
;
1742 data_string
*ds_range
;
1744 if (p
->conf
.is_readonly
) {
1745 con
->http_status
= 403;
1746 return HANDLER_FINISHED
;
1749 /* is a exclusive lock set on the source */
1750 /* (check for lock once before potentially reading large input) */
1751 if (0 == cq
->bytes_in
&& !webdav_has_lock(srv
, con
, hctx
, con
->uri
.path
)) {
1752 con
->http_status
= 423;
1753 return HANDLER_FINISHED
;
1756 if (con
->state
== CON_STATE_READ_POST
) {
1757 handler_t r
= connection_handle_read_post_state(srv
, con
);
1758 if (r
!= HANDLER_GO_ON
) return r
;
1761 /* RFC2616 Section 9.6 PUT requires us to send 501 on all Content-* we don't support
1762 * - most important Content-Range
1765 * Example: Content-Range: bytes 100-1037/1038 */
1767 if (NULL
!= (ds_range
= (data_string
*)array_get_element(con
->request
.headers
, "Content-Range"))) {
1768 const char *num
= ds_range
->value
->ptr
;
1772 if (0 != strncmp(num
, "bytes ", 6)) {
1773 con
->http_status
= 501; /* not implemented */
1775 return HANDLER_FINISHED
;
1778 /* we only support <num>- ... */
1783 while (*num
== ' ' || *num
== '\t') num
++;
1786 con
->http_status
= 501; /* not implemented */
1788 return HANDLER_FINISHED
;
1791 offset
= strtoll(num
, &err
, 10);
1793 if (*err
!= '-' || offset
< 0) {
1794 con
->http_status
= 501; /* not implemented */
1796 return HANDLER_FINISHED
;
1799 if (-1 == (fd
= open(con
->physical
.path
->ptr
, O_WRONLY
, WEBDAV_FILE_MODE
))) {
1802 con
->http_status
= 404; /* not found */
1805 con
->http_status
= 403; /* not found */
1808 return HANDLER_FINISHED
;
1811 if (-1 == lseek(fd
, offset
, SEEK_SET
)) {
1812 con
->http_status
= 501; /* not implemented */
1816 return HANDLER_FINISHED
;
1818 con
->http_status
= 200; /* modified */
1820 /* take what we have in the request-body and write it to a file */
1822 /* if the file doesn't exist, create it */
1823 if (-1 == (fd
= open(con
->physical
.path
->ptr
, O_WRONLY
|O_TRUNC
, WEBDAV_FILE_MODE
))) {
1824 if (errno
!= ENOENT
||
1825 -1 == (fd
= open(con
->physical
.path
->ptr
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
, WEBDAV_FILE_MODE
))) {
1826 /* we can't open the file */
1827 con
->http_status
= 403;
1829 return HANDLER_FINISHED
;
1831 con
->http_status
= 201; /* created */
1834 con
->http_status
= 200; /* modified */
1838 con
->file_finished
= 1;
1840 for (c
= cq
->first
; c
; c
= cq
->first
) {
1846 /* copy all chunks */
1850 mapped
= (c
->file
.mmap
.start
!= MAP_FAILED
);
1851 dlen
= c
->file
.length
- c
->offset
;
1853 data
= c
->file
.mmap
.start
+ c
->offset
;
1855 if (-1 == c
->file
.fd
&& /* open the file if not already open */
1856 -1 == (c
->file
.fd
= open(c
->file
.name
->ptr
, O_RDONLY
))) {
1857 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "open failed: ", strerror(errno
));
1859 return HANDLER_ERROR
;
1862 if (MAP_FAILED
!= (c
->file
.mmap
.start
= mmap(NULL
, c
->file
.length
, PROT_READ
, MAP_PRIVATE
, c
->file
.fd
, 0))) {
1863 /* chunk_reset() or chunk_free() will cleanup for us */
1864 c
->file
.mmap
.length
= c
->file
.length
;
1865 data
= c
->file
.mmap
.start
+ c
->offset
;
1869 if (dlen
> 65536) dlen
= 65536;
1870 data
= malloc(dlen
);
1872 if (-1 == lseek(c
->file
.fd
, c
->file
.start
+ c
->offset
, SEEK_SET
)
1873 || 0 > (rd
= read(c
->file
.fd
, data
, dlen
))) {
1874 log_error_write(srv
, __FILE__
, __LINE__
, "ssbd", "lseek/read failed: ",
1875 strerror(errno
), c
->file
.name
, c
->file
.fd
);
1878 return HANDLER_ERROR
;
1885 if ((r
= write(fd
, data
, dlen
)) < 0) {
1888 con
->http_status
= 507;
1892 con
->http_status
= 403;
1897 if (!mapped
) free(data
);
1900 if ((r
= write(fd
, c
->mem
->ptr
+ c
->offset
, buffer_string_length(c
->mem
) - c
->offset
)) < 0) {
1903 con
->http_status
= 507;
1907 con
->http_status
= 403;
1915 chunkqueue_mark_written(cq
, r
);
1920 if (0 != close(fd
)) {
1921 log_error_write(srv
, __FILE__
, __LINE__
, "sbss",
1922 "close ", con
->physical
.path
, "failed: ", strerror(errno
));
1923 return HANDLER_ERROR
;
1926 return HANDLER_FINISHED
;
1928 case HTTP_METHOD_MOVE
:
1929 case HTTP_METHOD_COPY
: {
1930 buffer
*destination
= NULL
;
1931 char *sep
, *sep2
, *start
;
1934 if (p
->conf
.is_readonly
) {
1935 con
->http_status
= 403;
1936 return HANDLER_FINISHED
;
1939 /* is a exclusive lock set on the source */
1940 if (con
->request
.http_method
== HTTP_METHOD_MOVE
) {
1941 if (!webdav_has_lock(srv
, con
, hctx
, con
->uri
.path
)) {
1942 con
->http_status
= 423;
1943 return HANDLER_FINISHED
;
1947 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "Destination"))) {
1948 destination
= ds
->value
;
1950 con
->http_status
= 400;
1951 return HANDLER_FINISHED
;
1954 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "Overwrite"))) {
1955 if (buffer_string_length(ds
->value
) != 1 ||
1956 (ds
->value
->ptr
[0] != 'F' &&
1957 ds
->value
->ptr
[0] != 'T') ) {
1958 con
->http_status
= 400;
1959 return HANDLER_FINISHED
;
1961 overwrite
= (ds
->value
->ptr
[0] == 'F' ? 0 : 1);
1963 /* let's parse the Destination
1965 * http://127.0.0.1:1025/dav/litmus/copydest
1967 * - host has to be the same as the Host: header we got
1968 * - we have to stay inside the document root
1969 * - the query string is thrown away
1972 buffer_reset(p
->uri
.scheme
);
1973 buffer_reset(p
->uri
.path_raw
);
1974 buffer_reset(p
->uri
.authority
);
1976 start
= destination
->ptr
;
1978 if (NULL
== (sep
= strstr(start
, "://"))) {
1979 con
->http_status
= 400;
1980 return HANDLER_FINISHED
;
1982 buffer_copy_string_len(p
->uri
.scheme
, start
, sep
- start
);
1986 if (NULL
== (sep
= strchr(start
, '/'))) {
1987 con
->http_status
= 400;
1988 return HANDLER_FINISHED
;
1990 if (NULL
!= (sep2
= memchr(start
, '@', sep
- start
))) {
1991 /* skip login information */
1994 buffer_copy_string_len(p
->uri
.authority
, start
, sep
- start
);
1998 if (NULL
== (sep
= strchr(start
, '?'))) {
1999 /* no query string, good */
2000 buffer_copy_string(p
->uri
.path_raw
, start
);
2002 buffer_copy_string_len(p
->uri
.path_raw
, start
, sep
- start
);
2005 if (!buffer_is_equal(p
->uri
.authority
, con
->uri
.authority
)) {
2006 /* not the same host */
2007 con
->http_status
= 502;
2008 return HANDLER_FINISHED
;
2011 buffer_copy_buffer(p
->tmp_buf
, p
->uri
.path_raw
);
2012 buffer_urldecode_path(p
->tmp_buf
);
2013 buffer_path_simplify(p
->uri
.path
, p
->tmp_buf
);
2015 /* we now have a URI which is clean. transform it into a physical path */
2016 buffer_copy_buffer(p
->physical
.doc_root
, con
->physical
.doc_root
);
2017 buffer_copy_buffer(p
->physical
.rel_path
, p
->uri
.path
);
2019 if (con
->conf
.force_lowercase_filenames
) {
2020 buffer_to_lower(p
->physical
.rel_path
);
2023 /* Destination physical path
2024 * src con->physical.path might have been remapped with mod_alias.
2025 * (but mod_alias does not modify con->physical.rel_path)
2026 * Find matching prefix to support use of mod_alias to remap webdav root.
2027 * Aliasing of paths underneath the webdav root might not work.
2028 * Likewise, mod_rewrite URL rewriting might thwart this comparison.
2029 * Use mod_redirect instead of mod_alias to remap paths *under* webdav root.
2030 * Use mod_redirect instead of mod_rewrite on *any* parts of path to webdav.
2031 * (Related, use mod_auth to protect webdav root, but avoid attempting to
2032 * use mod_auth on paths underneath webdav root, as Destination is not
2033 * validated with mod_auth)
2035 * tl;dr: webdav paths and webdav properties are managed by mod_webdav,
2036 * so do not modify paths externally or else undefined behavior
2037 * or corruption may occur
2040 /* find matching URI prefix
2041 * check if remaining con->physical.rel_path matches suffix
2042 * of con->physical.basedir so that we can use it to
2043 * remap Destination physical path */
2045 sep
= con
->uri
.path
->ptr
;
2046 sep2
= p
->uri
.path
->ptr
;
2047 for (i
= 0; sep
[i
] && sep
[i
] == sep2
[i
]; ++i
) ;
2048 if (sep
[i
] == '\0' && (sep2
[i
] == '\0' || sep2
[i
] == '/' || (i
> 0 && sep
[i
-1] == '/'))) {
2049 /* src and dst URI match or dst is nested inside src; invalid COPY or MOVE */
2050 con
->http_status
= 403;
2051 return HANDLER_FINISHED
;
2053 while (i
!= 0 && sep
[--i
] != '/') ; /* find matching directory path */
2054 remain
= buffer_string_length(con
->uri
.path
) - i
;
2055 if (!con
->conf
.force_lowercase_filenames
2056 ? buffer_is_equal_right_len(con
->physical
.path
, con
->physical
.rel_path
, remain
)
2057 :(buffer_string_length(con
->physical
.path
) >= remain
2058 && 0 == strncasecmp(con
->physical
.path
->ptr
+buffer_string_length(con
->physical
.path
)-remain
, con
->physical
.rel_path
->ptr
+i
, remain
))) {
2059 /* (at this point, p->physical.rel_path is identical to (or lowercased version of) p->uri.path) */
2060 buffer_copy_string_len(p
->physical
.path
, con
->physical
.path
->ptr
, buffer_string_length(con
->physical
.path
)-remain
);
2061 buffer_append_string_len(p
->physical
.path
, p
->physical
.rel_path
->ptr
+i
, buffer_string_length(p
->physical
.rel_path
)-i
);
2063 buffer_copy_buffer(p
->physical
.basedir
, con
->physical
.basedir
);
2064 buffer_append_slash(p
->physical
.basedir
);
2066 /* unable to perform physical path remap here;
2067 * assume doc_root/rel_path and no remapping */
2068 buffer_copy_buffer(p
->physical
.path
, p
->physical
.doc_root
);
2069 buffer_append_slash(p
->physical
.path
);
2070 buffer_copy_buffer(p
->physical
.basedir
, p
->physical
.path
);
2072 /* don't add a second / */
2073 if (p
->physical
.rel_path
->ptr
[0] == '/') {
2074 buffer_append_string_len(p
->physical
.path
, p
->physical
.rel_path
->ptr
+ 1, buffer_string_length(p
->physical
.rel_path
) - 1);
2076 buffer_append_string_buffer(p
->physical
.path
, p
->physical
.rel_path
);
2081 /* let's see if the source is a directory
2082 * if yes, we fail with 501 */
2084 if (-1 == stat(con
->physical
.path
->ptr
, &st
)) {
2085 /* don't about it yet, unlink will fail too */
2088 con
->http_status
= 404;
2091 con
->http_status
= 403;
2094 } else if (S_ISDIR(st
.st_mode
)) {
2097 /* src is a directory */
2099 if (con
->physical
.path
->ptr
[buffer_string_length(con
->physical
.path
)-1] != '/') {
2100 http_response_redirect_to_directory(srv
, con
);
2101 return HANDLER_FINISHED
;
2104 if (-1 == stat(p
->physical
.path
->ptr
, &st
)) {
2105 if (-1 == mkdir(p
->physical
.path
->ptr
, WEBDAV_DIR_MODE
)) {
2106 con
->http_status
= 403;
2107 return HANDLER_FINISHED
;
2110 } else if (!S_ISDIR(st
.st_mode
)) {
2111 if (overwrite
== 0) {
2112 /* copying into a non-dir ? */
2113 con
->http_status
= 409;
2114 return HANDLER_FINISHED
;
2116 unlink(p
->physical
.path
->ptr
);
2117 if (-1 == mkdir(p
->physical
.path
->ptr
, WEBDAV_DIR_MODE
)) {
2118 con
->http_status
= 403;
2119 return HANDLER_FINISHED
;
2125 /* copy the content of src to dest */
2126 if (0 != (r
= webdav_copy_dir(srv
, con
, hctx
, &(con
->physical
), &(p
->physical
), overwrite
))) {
2127 con
->http_status
= r
;
2128 return HANDLER_FINISHED
;
2130 if (con
->request
.http_method
== HTTP_METHOD_MOVE
) {
2132 webdav_delete_dir(srv
, con
, hctx
, &(con
->physical
), b
); /* content */
2135 rmdir(con
->physical
.path
->ptr
);
2137 con
->http_status
= created
? 201 : 204;
2138 con
->file_finished
= 1;
2140 /* it is just a file, good */
2144 /* does the client have a lock for this connection ? */
2145 if (!webdav_has_lock(srv
, con
, hctx
, p
->uri
.path
)) {
2146 con
->http_status
= 423;
2147 return HANDLER_FINISHED
;
2150 /* destination exists */
2151 if (0 == (r
= stat(p
->physical
.path
->ptr
, &st
))) {
2152 if (S_ISDIR(st
.st_mode
)) {
2154 * append basename to physical path */
2157 if (NULL
!= (sep
= strrchr(con
->physical
.path
->ptr
, '/'))) {
2158 buffer_append_string(p
->physical
.path
, sep
);
2159 r
= stat(p
->physical
.path
->ptr
, &st
);
2165 con
->http_status
= destdir
? 204 : 201; /* we will create a new one */
2166 con
->file_finished
= 1;
2170 con
->http_status
= 409;
2171 return HANDLER_FINISHED
;
2173 } else if (overwrite
== 0) {
2174 /* destination exists, but overwrite is not set */
2175 con
->http_status
= 412;
2176 return HANDLER_FINISHED
;
2178 con
->http_status
= 204; /* resource already existed */
2181 if (con
->request
.http_method
== HTTP_METHOD_MOVE
) {
2184 if (0 == rename(con
->physical
.path
->ptr
, p
->physical
.path
->ptr
)) {
2185 #ifdef USE_PROPPATCH
2188 stmt
= p
->conf
.stmt_move_uri
;
2191 sqlite3_reset(stmt
);
2193 /* bind the values to the insert */
2194 sqlite3_bind_text(stmt
, 1,
2195 CONST_BUF_LEN(p
->uri
.path
),
2198 sqlite3_bind_text(stmt
, 2,
2199 CONST_BUF_LEN(con
->uri
.path
),
2202 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
2203 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sql-move failed:", sqlite3_errmsg(p
->conf
.sql
));
2207 return HANDLER_FINISHED
;
2210 /* rename failed, fall back to COPY + DELETE */
2213 if (0 != (r
= webdav_copy_file(srv
, con
, hctx
, &(con
->physical
), &(p
->physical
), overwrite
))) {
2214 con
->http_status
= r
;
2216 return HANDLER_FINISHED
;
2219 if (con
->request
.http_method
== HTTP_METHOD_MOVE
) {
2221 webdav_delete_file(srv
, con
, hctx
, &(con
->physical
), b
);
2226 return HANDLER_FINISHED
;
2228 case HTTP_METHOD_PROPPATCH
:
2229 if (p
->conf
.is_readonly
) {
2230 con
->http_status
= 403;
2231 return HANDLER_FINISHED
;
2234 if (!webdav_has_lock(srv
, con
, hctx
, con
->uri
.path
)) {
2235 con
->http_status
= 423;
2236 return HANDLER_FINISHED
;
2239 /* check if destination exists */
2240 if (-1 == stat(con
->physical
.path
->ptr
, &st
)) {
2243 con
->http_status
= 404;
2248 if (S_ISDIR(st
.st_mode
) && con
->physical
.path
->ptr
[buffer_string_length(con
->physical
.path
)-1] != '/') {
2249 http_response_redirect_to_directory(srv
, con
);
2250 return HANDLER_FINISHED
;
2253 #ifdef USE_PROPPATCH
2254 if (con
->request
.content_length
) {
2257 if (con
->state
== CON_STATE_READ_POST
) {
2258 handler_t r
= connection_handle_read_post_state(srv
, con
);
2259 if (r
!= HANDLER_GO_ON
) return r
;
2262 if (1 == webdav_parse_chunkqueue(srv
, con
, hctx
, con
->request_content_queue
, &xml
)) {
2263 xmlNode
*rootnode
= xmlDocGetRootElement(xml
);
2265 if (0 == xmlStrcmp(rootnode
->name
, BAD_CAST
"propertyupdate")) {
2268 int empty_ns
= 0; /* send 400 on a empty namespace attribute */
2270 /* start response */
2272 if (SQLITE_OK
!= sqlite3_exec(p
->conf
.sql
, "BEGIN TRANSACTION", NULL
, NULL
, &err
)) {
2273 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "can't open transaction:", err
);
2276 goto propmatch_cleanup
;
2279 /* a UPDATE request, we know 'set' and 'remove' */
2280 for (cmd
= rootnode
->children
; cmd
; cmd
= cmd
->next
) {
2282 /* either set or remove */
2284 if ((0 == xmlStrcmp(cmd
->name
, BAD_CAST
"set")) ||
2285 (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"remove"))) {
2289 stmt
= (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"remove")) ?
2290 p
->conf
.stmt_delete_prop
: p
->conf
.stmt_update_prop
;
2292 for (props
= cmd
->children
; props
; props
= props
->next
) {
2293 if (0 == xmlStrcmp(props
->name
, BAD_CAST
"prop")) {
2295 char *propval
= NULL
;
2298 prop
= props
->children
;
2301 (0 == xmlStrcmp(prop
->ns
->href
, BAD_CAST
"")) &&
2302 (0 != xmlStrcmp(prop
->ns
->prefix
, BAD_CAST
""))) {
2303 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2304 "no name space for:",
2311 sqlite3_reset(stmt
);
2313 /* bind the values to the insert */
2315 sqlite3_bind_text(stmt
, 1,
2316 CONST_BUF_LEN(con
->uri
.path
),
2318 sqlite3_bind_text(stmt
, 2,
2320 strlen((char *)prop
->name
),
2323 sqlite3_bind_text(stmt
, 3,
2324 (char *)prop
->ns
->href
,
2325 strlen((char *)prop
->ns
->href
),
2328 sqlite3_bind_text(stmt
, 3,
2333 if (stmt
== p
->conf
.stmt_update_prop
) {
2334 propval
= prop
->children
2335 ? (char *)xmlNodeListGetString(xml
, prop
->children
, 0)
2338 sqlite3_bind_text(stmt
, 4,
2339 propval
? propval
: "",
2340 propval
? strlen(propval
) : 0,
2344 if (SQLITE_DONE
!= (r
= sqlite3_step(stmt
))) {
2345 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2346 "sql-set failed:", sqlite3_errmsg(p
->conf
.sql
));
2349 if (propval
) xmlFree(propval
);
2352 if (empty_ns
) break;
2357 if (SQLITE_OK
!= sqlite3_exec(p
->conf
.sql
, "ROLLBACK", NULL
, NULL
, &err
)) {
2358 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "can't rollback transaction:", err
);
2361 goto propmatch_cleanup
;
2364 con
->http_status
= 400;
2366 if (SQLITE_OK
!= sqlite3_exec(p
->conf
.sql
, "COMMIT", NULL
, NULL
, &err
)) {
2367 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "can't commit transaction:", err
);
2370 goto propmatch_cleanup
;
2372 con
->http_status
= 200;
2374 con
->file_finished
= 1;
2377 return HANDLER_FINISHED
;
2384 con
->http_status
= 400;
2385 return HANDLER_FINISHED
;
2389 con
->http_status
= 501;
2390 return HANDLER_FINISHED
;
2391 case HTTP_METHOD_LOCK
:
2393 * a mac wants to write
2395 * LOCK /dav/expire.txt HTTP/1.1\r\n
2396 * User-Agent: WebDAVFS/1.3 (01308000) Darwin/8.1.0 (Power Macintosh)\r\n
2399 * Timeout: Second-600\r\n
2400 * Content-Type: text/xml; charset=\"utf-8\"\r\n
2401 * Content-Length: 229\r\n
2402 * Connection: keep-alive\r\n
2403 * Host: 192.168.178.23:1025\r\n
2405 * <?xml version=\"1.0\" encoding=\"utf-8\"?>\n
2406 * <D:lockinfo xmlns:D=\"DAV:\">\n
2407 * <D:lockscope><D:exclusive/></D:lockscope>\n
2408 * <D:locktype><D:write/></D:locktype>\n
2410 * <D:href>http://www.apple.com/webdav_fs/</D:href>\n
2415 if (depth
!= 0 && depth
!= -1) {
2416 con
->http_status
= 400;
2418 return HANDLER_FINISHED
;
2422 if (con
->request
.content_length
) {
2424 buffer
*hdr_if
= NULL
;
2427 if (con
->state
== CON_STATE_READ_POST
) {
2428 handler_t r
= connection_handle_read_post_state(srv
, con
);
2429 if (r
!= HANDLER_GO_ON
) return r
;
2432 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "If"))) {
2436 if (0 != stat(con
->physical
.path
->ptr
, &st
)) {
2437 if (errno
== ENOENT
) {
2438 int fd
= open(con
->physical
.path
->ptr
, O_WRONLY
|O_CREAT
|O_APPEND
|O_BINARY
|FIFO_NONBLOCK
, WEBDAV_FILE_MODE
);
2443 log_error_write(srv
, __FILE__
, __LINE__
, "sBss",
2444 "create file", con
->physical
.path
, ":", strerror(errno
));
2445 con
->http_status
= 403; /* Forbidden */
2447 return HANDLER_FINISHED
;
2450 } else if (hdr_if
== NULL
&& depth
== -1) {
2451 /* we don't support Depth: Infinity on directories */
2452 if (S_ISDIR(st
.st_mode
)) {
2453 con
->http_status
= 409; /* Conflict */
2455 return HANDLER_FINISHED
;
2459 if (1 == webdav_parse_chunkqueue(srv
, con
, hctx
, con
->request_content_queue
, &xml
)) {
2460 xmlNode
*rootnode
= xmlDocGetRootElement(xml
);
2462 force_assert(rootnode
);
2464 if (0 == xmlStrcmp(rootnode
->name
, BAD_CAST
"lockinfo")) {
2466 const xmlChar
*lockscope
= NULL
, *locktype
= NULL
; /* TODO: compiler says unused: *owner = NULL; */
2468 for (lockinfo
= rootnode
->children
; lockinfo
; lockinfo
= lockinfo
->next
) {
2469 if (0 == xmlStrcmp(lockinfo
->name
, BAD_CAST
"lockscope")) {
2471 for (value
= lockinfo
->children
; value
; value
= value
->next
) {
2472 if ((0 == xmlStrcmp(value
->name
, BAD_CAST
"exclusive")) ||
2473 (0 == xmlStrcmp(value
->name
, BAD_CAST
"shared"))) {
2474 lockscope
= value
->name
;
2476 con
->http_status
= 400;
2479 return HANDLER_FINISHED
;
2482 } else if (0 == xmlStrcmp(lockinfo
->name
, BAD_CAST
"locktype")) {
2484 for (value
= lockinfo
->children
; value
; value
= value
->next
) {
2485 if ((0 == xmlStrcmp(value
->name
, BAD_CAST
"write"))) {
2486 locktype
= value
->name
;
2488 con
->http_status
= 400;
2491 return HANDLER_FINISHED
;
2495 } else if (0 == xmlStrcmp(lockinfo
->name
, BAD_CAST
"owner")) {
2499 if (lockscope
&& locktype
) {
2500 sqlite3_stmt
*stmt
= p
->conf
.stmt_read_lock_by_uri
;
2502 /* is this resourse already locked ? */
2504 /* SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout
2506 * WHERE resource = ? */
2510 sqlite3_reset(stmt
);
2512 sqlite3_bind_text(stmt
, 1,
2513 CONST_BUF_LEN(p
->uri
.path
),
2517 while (SQLITE_ROW
== sqlite3_step(stmt
)) {
2519 * 1. is it compatible ?
2521 char *sql_lockscope
= (char *)sqlite3_column_text(stmt
, 2);
2523 if (strcmp(sql_lockscope
, "exclusive")) {
2524 con
->http_status
= 423;
2525 } else if (0 == xmlStrcmp(lockscope
, BAD_CAST
"exclusive")) {
2526 /* resourse is locked with a shared lock
2527 * client wants exclusive */
2528 con
->http_status
= 423;
2531 if (con
->http_status
== 423) {
2533 return HANDLER_FINISHED
;
2537 stmt
= p
->conf
.stmt_create_lock
;
2539 /* create a lock-token */
2541 char uuid
[37] /* 36 + \0 */;
2544 uuid_unparse(id
, uuid
);
2546 buffer_copy_string_len(p
->tmp_buf
, CONST_STR_LEN("opaquelocktoken:"));
2547 buffer_append_string(p
->tmp_buf
, uuid
);
2549 /* "CREATE TABLE locks ("
2550 * " locktoken TEXT NOT NULL,"
2551 * " resource TEXT NOT NULL,"
2552 * " lockscope TEXT NOT NULL,"
2553 * " locktype TEXT NOT NULL,"
2554 * " owner TEXT NOT NULL,"
2555 * " depth INT NOT NULL,"
2558 sqlite3_reset(stmt
);
2560 sqlite3_bind_text(stmt
, 1,
2561 CONST_BUF_LEN(p
->tmp_buf
),
2564 sqlite3_bind_text(stmt
, 2,
2565 CONST_BUF_LEN(con
->uri
.path
),
2568 sqlite3_bind_text(stmt
, 3,
2569 (const char *)lockscope
,
2570 xmlStrlen(lockscope
),
2573 sqlite3_bind_text(stmt
, 4,
2574 (const char *)locktype
,
2575 xmlStrlen(locktype
),
2579 sqlite3_bind_text(stmt
, 5,
2585 sqlite3_bind_int(stmt
, 6,
2589 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
2590 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2591 "create lock:", sqlite3_errmsg(p
->conf
.sql
));
2594 /* looks like we survived */
2595 webdav_lockdiscovery(srv
, con
, p
->tmp_buf
, (const char *)lockscope
, (const char *)locktype
, depth
);
2597 con
->http_status
= created
? 201 : 200;
2598 con
->file_finished
= 1;
2604 return HANDLER_FINISHED
;
2606 con
->http_status
= 400;
2607 return HANDLER_FINISHED
;
2611 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "If"))) {
2612 buffer
*locktoken
= ds
->value
;
2613 sqlite3_stmt
*stmt
= p
->conf
.stmt_refresh_lock
;
2615 /* remove the < > around the token */
2616 if (buffer_string_length(locktoken
) < 5) {
2617 con
->http_status
= 400;
2619 return HANDLER_FINISHED
;
2622 buffer_copy_string_len(p
->tmp_buf
, locktoken
->ptr
+ 2, buffer_string_length(locktoken
) - 4);
2624 sqlite3_reset(stmt
);
2626 sqlite3_bind_text(stmt
, 1,
2627 CONST_BUF_LEN(p
->tmp_buf
),
2630 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
2631 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2632 "refresh lock:", sqlite3_errmsg(p
->conf
.sql
));
2635 webdav_lockdiscovery(srv
, con
, p
->tmp_buf
, "exclusive", "write", 0);
2637 con
->http_status
= 200;
2638 con
->file_finished
= 1;
2639 return HANDLER_FINISHED
;
2641 /* we need a lock-token to refresh */
2642 con
->http_status
= 400;
2644 return HANDLER_FINISHED
;
2649 con
->http_status
= 501;
2650 return HANDLER_FINISHED
;
2652 case HTTP_METHOD_UNLOCK
:
2654 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "Lock-Token"))) {
2655 buffer
*locktoken
= ds
->value
;
2656 sqlite3_stmt
*stmt
= p
->conf
.stmt_remove_lock
;
2658 /* remove the < > around the token */
2659 if (buffer_string_length(locktoken
) < 3) {
2660 con
->http_status
= 400;
2662 return HANDLER_FINISHED
;
2668 * if the resourse is locked:
2670 * - by someone else: 401
2671 * if the resource is not locked:
2675 buffer_copy_string_len(p
->tmp_buf
, locktoken
->ptr
+ 1, buffer_string_length(locktoken
) - 2);
2677 sqlite3_reset(stmt
);
2679 sqlite3_bind_text(stmt
, 1,
2680 CONST_BUF_LEN(p
->tmp_buf
),
2683 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
2684 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2685 "remove lock:", sqlite3_errmsg(p
->conf
.sql
));
2688 if (0 == sqlite3_changes(p
->conf
.sql
)) {
2689 con
->http_status
= 401;
2691 con
->http_status
= 204;
2693 return HANDLER_FINISHED
;
2695 /* we need a lock-token to unlock */
2696 con
->http_status
= 400;
2698 return HANDLER_FINISHED
;
2702 con
->http_status
= 501;
2703 return HANDLER_FINISHED
;
2710 return HANDLER_GO_ON
;
2714 SUBREQUEST_FUNC(mod_webdav_subrequest_handler
) {
2716 plugin_data
*p
= p_d
;
2717 if (con
->mode
!= p
->id
) return HANDLER_GO_ON
;
2719 r
= mod_webdav_subrequest_handler_huge(srv
, con
, p_d
);
2720 if (con
->http_status
>= 400) con
->mode
= DIRECT
;
2725 PHYSICALPATH_FUNC(mod_webdav_physical_handler
) {
2726 plugin_data
*p
= p_d
;
2727 if (!p
->conf
.enabled
) return HANDLER_GO_ON
;
2729 /* physical path is setup */
2730 if (buffer_is_empty(con
->physical
.path
)) return HANDLER_GO_ON
;
2734 switch (con
->request
.http_method
) {
2735 case HTTP_METHOD_PROPFIND
:
2736 case HTTP_METHOD_PROPPATCH
:
2737 case HTTP_METHOD_PUT
:
2738 case HTTP_METHOD_COPY
:
2739 case HTTP_METHOD_MOVE
:
2740 case HTTP_METHOD_MKCOL
:
2741 case HTTP_METHOD_DELETE
:
2742 case HTTP_METHOD_LOCK
:
2743 case HTTP_METHOD_UNLOCK
: {
2744 handler_ctx
*hctx
= calloc(1, sizeof(*hctx
));
2745 memcpy(&hctx
->conf
, &p
->conf
, sizeof(plugin_config
));
2746 con
->plugin_ctx
[p
->id
] = hctx
;
2747 con
->conf
.stream_request_body
= 0;
2755 return HANDLER_GO_ON
;
2758 static handler_t
mod_webdav_connection_reset(server
*srv
, connection
*con
, void *p_d
) {
2759 plugin_data
*p
= p_d
;
2760 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
2763 con
->plugin_ctx
[p
->id
] = NULL
;
2767 return HANDLER_GO_ON
;
2771 /* this function is called at dlopen() time and inits the callbacks */
2773 int mod_webdav_plugin_init(plugin
*p
);
2774 int mod_webdav_plugin_init(plugin
*p
) {
2775 p
->version
= LIGHTTPD_VERSION_ID
;
2776 p
->name
= buffer_init_string("webdav");
2778 p
->init
= mod_webdav_init
;
2779 p
->handle_uri_clean
= mod_webdav_uri_handler
;
2780 p
->handle_physical
= mod_webdav_physical_handler
;
2781 p
->handle_subrequest
= mod_webdav_subrequest_handler
;
2782 p
->connection_reset
= mod_webdav_connection_reset
;
2783 p
->set_defaults
= mod_webdav_set_defaults
;
2784 p
->cleanup
= mod_webdav_free
;