7 #include "connections.h"
12 #include "stat_cache.h"
16 #include <sys/types.h>
29 #if defined(HAVE_LIBXML_H) && defined(HAVE_SQLITE3_H)
31 #include <libxml/tree.h>
32 #include <libxml/parser.h>
37 #if defined(HAVE_LIBXML_H) && defined(HAVE_SQLITE3_H) && defined(HAVE_UUID_UUID_H)
39 #include <uuid/uuid.h>
43 * this is a webdav for a lighttpd plugin
45 * at least a very basic one.
46 * - for now it is read-only and we only support PROPFIND
50 #define WEBDAV_FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
51 #define WEBDAV_DIR_MODE S_IRWXU | S_IRWXG | S_IRWXO
53 /* plugin config for all request/connections */
56 unsigned short enabled
;
57 unsigned short is_readonly
;
58 unsigned short log_xml
;
60 buffer
*sqlite_db_name
;
63 sqlite3_stmt
*stmt_update_prop
;
64 sqlite3_stmt
*stmt_delete_prop
;
65 sqlite3_stmt
*stmt_select_prop
;
66 sqlite3_stmt
*stmt_select_propnames
;
68 sqlite3_stmt
*stmt_delete_uri
;
69 sqlite3_stmt
*stmt_move_uri
;
70 sqlite3_stmt
*stmt_copy_uri
;
72 sqlite3_stmt
*stmt_remove_lock
;
73 sqlite3_stmt
*stmt_create_lock
;
74 sqlite3_stmt
*stmt_read_lock
;
75 sqlite3_stmt
*stmt_read_lock_by_uri
;
76 sqlite3_stmt
*stmt_refresh_lock
;
87 plugin_config
**config_storage
;
92 /* init the plugin data */
93 INIT_FUNC(mod_webdav_init
) {
96 p
= calloc(1, sizeof(*p
));
98 p
->tmp_buf
= buffer_init();
100 p
->uri
.scheme
= buffer_init();
101 p
->uri
.path_raw
= buffer_init();
102 p
->uri
.path
= buffer_init();
103 p
->uri
.authority
= buffer_init();
105 p
->physical
.path
= buffer_init();
106 p
->physical
.rel_path
= buffer_init();
107 p
->physical
.doc_root
= buffer_init();
108 p
->physical
.basedir
= buffer_init();
113 /* detroy the plugin data */
114 FREE_FUNC(mod_webdav_free
) {
115 plugin_data
*p
= p_d
;
119 if (!p
) return HANDLER_GO_ON
;
121 if (p
->config_storage
) {
123 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
124 plugin_config
*s
= p
->config_storage
[i
];
126 if (NULL
== s
) continue;
128 buffer_free(s
->sqlite_db_name
);
131 sqlite3_finalize(s
->stmt_delete_prop
);
132 sqlite3_finalize(s
->stmt_delete_uri
);
133 sqlite3_finalize(s
->stmt_copy_uri
);
134 sqlite3_finalize(s
->stmt_move_uri
);
135 sqlite3_finalize(s
->stmt_update_prop
);
136 sqlite3_finalize(s
->stmt_select_prop
);
137 sqlite3_finalize(s
->stmt_select_propnames
);
139 sqlite3_finalize(s
->stmt_read_lock
);
140 sqlite3_finalize(s
->stmt_read_lock_by_uri
);
141 sqlite3_finalize(s
->stmt_create_lock
);
142 sqlite3_finalize(s
->stmt_remove_lock
);
143 sqlite3_finalize(s
->stmt_refresh_lock
);
144 sqlite3_close(s
->sql
);
149 free(p
->config_storage
);
152 buffer_free(p
->uri
.scheme
);
153 buffer_free(p
->uri
.path_raw
);
154 buffer_free(p
->uri
.path
);
155 buffer_free(p
->uri
.authority
);
157 buffer_free(p
->physical
.path
);
158 buffer_free(p
->physical
.rel_path
);
159 buffer_free(p
->physical
.doc_root
);
160 buffer_free(p
->physical
.basedir
);
162 buffer_free(p
->tmp_buf
);
166 return HANDLER_GO_ON
;
169 /* handle plugin config and check values */
171 SETDEFAULTS_FUNC(mod_webdav_set_defaults
) {
172 plugin_data
*p
= p_d
;
175 config_values_t cv
[] = {
176 { "webdav.activate", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
177 { "webdav.is-readonly", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
178 { "webdav.sqlite-db-name", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
179 { "webdav.log-xml", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
180 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
183 if (!p
) return HANDLER_ERROR
;
185 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
187 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
188 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
191 s
= calloc(1, sizeof(plugin_config
));
192 s
->sqlite_db_name
= buffer_init();
194 cv
[0].destination
= &(s
->enabled
);
195 cv
[1].destination
= &(s
->is_readonly
);
196 cv
[2].destination
= s
->sqlite_db_name
;
197 cv
[3].destination
= &(s
->log_xml
);
199 p
->config_storage
[i
] = s
;
201 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
202 return HANDLER_ERROR
;
205 if (!buffer_string_is_empty(s
->sqlite_db_name
)) {
207 const char *next_stmt
;
210 if (SQLITE_OK
!= sqlite3_open(s
->sqlite_db_name
->ptr
, &(s
->sql
))) {
211 log_error_write(srv
, __FILE__
, __LINE__
, "sbs", "sqlite3_open failed for",
213 sqlite3_errmsg(s
->sql
));
214 return HANDLER_ERROR
;
217 if (SQLITE_OK
!= sqlite3_exec(s
->sql
,
218 "CREATE TABLE IF NOT EXISTS properties ("
219 " resource TEXT NOT NULL,"
220 " prop TEXT NOT NULL,"
222 " value TEXT NOT NULL,"
223 " PRIMARY KEY(resource, prop, ns))",
226 if (0 != strcmp(err
, "table properties already exists")) {
227 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "can't open transaction:", err
);
230 return HANDLER_ERROR
;
235 if (SQLITE_OK
!= sqlite3_exec(s
->sql
,
236 "CREATE TABLE IF NOT EXISTS locks ("
237 " locktoken TEXT NOT NULL,"
238 " resource TEXT NOT NULL,"
239 " lockscope TEXT NOT NULL,"
240 " locktype TEXT NOT NULL,"
241 " owner TEXT NOT NULL,"
242 " depth INT NOT NULL,"
243 " timeout TIMESTAMP NOT NULL,"
244 " PRIMARY KEY(locktoken))",
247 if (0 != strcmp(err
, "table locks already exists")) {
248 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "can't open transaction:", err
);
251 return HANDLER_ERROR
;
256 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
257 CONST_STR_LEN("SELECT value FROM properties WHERE resource = ? AND prop = ? AND ns = ?"),
258 &(s
->stmt_select_prop
), &next_stmt
)) {
261 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed:", sqlite3_errmsg(s
->sql
));
262 return HANDLER_ERROR
;
265 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
266 CONST_STR_LEN("SELECT ns, prop FROM properties WHERE resource = ?"),
267 &(s
->stmt_select_propnames
), &next_stmt
)) {
270 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed:", sqlite3_errmsg(s
->sql
));
271 return HANDLER_ERROR
;
275 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
276 CONST_STR_LEN("REPLACE INTO properties (resource, prop, ns, value) VALUES (?, ?, ?, ?)"),
277 &(s
->stmt_update_prop
), &next_stmt
)) {
280 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed:", sqlite3_errmsg(s
->sql
));
281 return HANDLER_ERROR
;
284 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
285 CONST_STR_LEN("DELETE FROM properties WHERE resource = ? AND prop = ? AND ns = ?"),
286 &(s
->stmt_delete_prop
), &next_stmt
)) {
288 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
290 return HANDLER_ERROR
;
293 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
294 CONST_STR_LEN("DELETE FROM properties WHERE resource = ?"),
295 &(s
->stmt_delete_uri
), &next_stmt
)) {
297 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
299 return HANDLER_ERROR
;
302 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
303 CONST_STR_LEN("INSERT INTO properties SELECT ?, prop, ns, value FROM properties WHERE resource = ?"),
304 &(s
->stmt_copy_uri
), &next_stmt
)) {
306 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
308 return HANDLER_ERROR
;
311 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
312 CONST_STR_LEN("UPDATE OR REPLACE properties SET resource = ? WHERE resource = ?"),
313 &(s
->stmt_move_uri
), &next_stmt
)) {
315 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
317 return HANDLER_ERROR
;
322 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
323 CONST_STR_LEN("INSERT INTO locks (locktoken, resource, lockscope, locktype, owner, depth, timeout) VALUES (?,?,?,?,?,?, CURRENT_TIME + 600)"),
324 &(s
->stmt_create_lock
), &next_stmt
)) {
326 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
328 return HANDLER_ERROR
;
331 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
332 CONST_STR_LEN("DELETE FROM locks WHERE locktoken = ?"),
333 &(s
->stmt_remove_lock
), &next_stmt
)) {
335 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
337 return HANDLER_ERROR
;
340 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
341 CONST_STR_LEN("SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout-CURRENT_TIME FROM locks WHERE locktoken = ?"),
342 &(s
->stmt_read_lock
), &next_stmt
)) {
344 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
346 return HANDLER_ERROR
;
349 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
350 CONST_STR_LEN("SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout-CURRENT_TIME FROM locks WHERE resource = ?"),
351 &(s
->stmt_read_lock_by_uri
), &next_stmt
)) {
353 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
355 return HANDLER_ERROR
;
358 if (SQLITE_OK
!= sqlite3_prepare(s
->sql
,
359 CONST_STR_LEN("UPDATE locks SET timeout = CURRENT_TIME + 600 WHERE locktoken = ?"),
360 &(s
->stmt_refresh_lock
), &next_stmt
)) {
362 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s
->sql
));
364 return HANDLER_ERROR
;
369 log_error_write(srv
, __FILE__
, __LINE__
, "s", "Sorry, no sqlite3 and libxml2 support include, compile with --with-webdav-props");
370 return HANDLER_ERROR
;
375 return HANDLER_GO_ON
;
378 #define PATCH_OPTION(x) \
380 static int mod_webdav_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
382 plugin_config
*s
= p
->config_storage
[0];
384 PATCH_OPTION(enabled
);
385 PATCH_OPTION(is_readonly
);
386 PATCH_OPTION(log_xml
);
390 PATCH_OPTION(stmt_update_prop
);
391 PATCH_OPTION(stmt_delete_prop
);
392 PATCH_OPTION(stmt_select_prop
);
393 PATCH_OPTION(stmt_select_propnames
);
395 PATCH_OPTION(stmt_delete_uri
);
396 PATCH_OPTION(stmt_move_uri
);
397 PATCH_OPTION(stmt_copy_uri
);
399 PATCH_OPTION(stmt_remove_lock
);
400 PATCH_OPTION(stmt_refresh_lock
);
401 PATCH_OPTION(stmt_create_lock
);
402 PATCH_OPTION(stmt_read_lock
);
403 PATCH_OPTION(stmt_read_lock_by_uri
);
405 /* skip the first, the global context */
406 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
407 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
408 s
= p
->config_storage
[i
];
410 /* condition didn't match */
411 if (!config_check_cond(srv
, con
, dc
)) continue;
414 for (j
= 0; j
< dc
->value
->used
; j
++) {
415 data_unset
*du
= dc
->value
->data
[j
];
417 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("webdav.activate"))) {
418 PATCH_OPTION(enabled
);
419 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("webdav.is-readonly"))) {
420 PATCH_OPTION(is_readonly
);
421 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("webdav.log-xml"))) {
422 PATCH_OPTION(log_xml
);
423 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("webdav.sqlite-db-name"))) {
426 PATCH_OPTION(stmt_update_prop
);
427 PATCH_OPTION(stmt_delete_prop
);
428 PATCH_OPTION(stmt_select_prop
);
429 PATCH_OPTION(stmt_select_propnames
);
431 PATCH_OPTION(stmt_delete_uri
);
432 PATCH_OPTION(stmt_move_uri
);
433 PATCH_OPTION(stmt_copy_uri
);
435 PATCH_OPTION(stmt_remove_lock
);
436 PATCH_OPTION(stmt_refresh_lock
);
437 PATCH_OPTION(stmt_create_lock
);
438 PATCH_OPTION(stmt_read_lock
);
439 PATCH_OPTION(stmt_read_lock_by_uri
);
448 URIHANDLER_FUNC(mod_webdav_uri_handler
) {
449 plugin_data
*p
= p_d
;
453 if (buffer_is_empty(con
->uri
.path
)) return HANDLER_GO_ON
;
455 mod_webdav_patch_connection(srv
, con
, p
);
457 if (!p
->conf
.enabled
) return HANDLER_GO_ON
;
459 switch (con
->request
.http_method
) {
460 case HTTP_METHOD_OPTIONS
:
461 /* we fake a little bit but it makes MS W2k happy and it let's us mount the volume */
462 response_header_overwrite(srv
, con
, CONST_STR_LEN("DAV"), CONST_STR_LEN("1,2"));
463 response_header_overwrite(srv
, con
, CONST_STR_LEN("MS-Author-Via"), CONST_STR_LEN("DAV"));
465 if (p
->conf
.is_readonly
) {
466 response_header_insert(srv
, con
, CONST_STR_LEN("Allow"), CONST_STR_LEN("PROPFIND"));
468 response_header_insert(srv
, con
, CONST_STR_LEN("Allow"), CONST_STR_LEN("PROPFIND, DELETE, MKCOL, PUT, MOVE, COPY, PROPPATCH, LOCK, UNLOCK"));
476 return HANDLER_GO_ON
;
478 static int webdav_gen_prop_tag(server
*srv
, connection
*con
,
488 buffer_append_string_len(b
,CONST_STR_LEN("<"));
489 buffer_append_string(b
, prop_name
);
490 buffer_append_string_len(b
, CONST_STR_LEN(" xmlns=\""));
491 buffer_append_string(b
, prop_ns
);
492 buffer_append_string_len(b
, CONST_STR_LEN("\">"));
494 buffer_append_string(b
, value
);
496 buffer_append_string_len(b
,CONST_STR_LEN("</"));
497 buffer_append_string(b
, prop_name
);
498 buffer_append_string_len(b
, CONST_STR_LEN(">"));
500 buffer_append_string_len(b
,CONST_STR_LEN("<"));
501 buffer_append_string(b
, prop_name
);
502 buffer_append_string_len(b
, CONST_STR_LEN(" xmlns=\""));
503 buffer_append_string(b
, prop_ns
);
504 buffer_append_string_len(b
, CONST_STR_LEN("\"/>"));
511 static int webdav_gen_response_status_tag(server
*srv
, connection
*con
, physical
*dst
, int status
, buffer
*b
) {
514 buffer_append_string_len(b
,CONST_STR_LEN("<D:response xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\">\n"));
516 buffer_append_string_len(b
,CONST_STR_LEN("<D:href>\n"));
517 buffer_append_string_buffer(b
, dst
->rel_path
);
518 buffer_append_string_len(b
,CONST_STR_LEN("</D:href>\n"));
519 buffer_append_string_len(b
,CONST_STR_LEN("<D:status>\n"));
521 if (con
->request
.http_version
== HTTP_VERSION_1_1
) {
522 buffer_copy_string_len(b
, CONST_STR_LEN("HTTP/1.1 "));
524 buffer_copy_string_len(b
, CONST_STR_LEN("HTTP/1.0 "));
526 buffer_append_int(b
, status
);
527 buffer_append_string_len(b
, CONST_STR_LEN(" "));
528 buffer_append_string(b
, get_http_status_name(status
));
530 buffer_append_string_len(b
,CONST_STR_LEN("</D:status>\n"));
531 buffer_append_string_len(b
,CONST_STR_LEN("</D:response>\n"));
536 static int webdav_delete_file(server
*srv
, connection
*con
, plugin_data
*p
, physical
*dst
, buffer
*b
) {
539 /* try to unlink it */
540 if (-1 == unlink(dst
->path
->ptr
)) {
551 webdav_gen_response_status_tag(srv
, con
, dst
, status
, b
);
554 sqlite3_stmt
*stmt
= p
->conf
.stmt_delete_uri
;
558 webdav_gen_response_status_tag(srv
, con
, dst
, status
, b
);
562 /* bind the values to the insert */
564 sqlite3_bind_text(stmt
, 1,
565 CONST_BUF_LEN(dst
->rel_path
),
568 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
577 return (status
!= 0);
580 static int webdav_delete_dir(server
*srv
, connection
*con
, plugin_data
*p
, physical
*dst
, buffer
*b
) {
582 int have_multi_status
= 0;
585 d
.path
= buffer_init();
586 d
.rel_path
= buffer_init();
588 if (NULL
!= (dir
= opendir(dst
->path
->ptr
))) {
591 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
, p
, &d
, b
);
615 /* try to unlink it */
616 if (-1 == rmdir(d
.path
->ptr
)) {
627 have_multi_status
= 1;
629 webdav_gen_response_status_tag(srv
, con
, &d
, status
, b
);
632 sqlite3_stmt
*stmt
= p
->conf
.stmt_delete_uri
;
639 /* bind the values to the insert */
641 sqlite3_bind_text(stmt
, 1,
642 CONST_BUF_LEN(d
.rel_path
),
645 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
652 have_multi_status
= webdav_delete_file(srv
, con
, p
, &d
, b
);
658 buffer_free(d
.rel_path
);
661 return have_multi_status
;
664 /* don't want to block when open()ing a fifo */
665 #if defined(O_NONBLOCK)
666 # define FIFO_NONBLOCK O_NONBLOCK
668 # define FIFO_NONBLOCK 0
675 static int webdav_copy_file(server
*srv
, connection
*con
, plugin_data
*p
, physical
*src
, physical
*dst
, int overwrite
) {
677 ssize_t rd
, wr
, offset
;
678 int status
= 0, ifd
, ofd
;
682 if (-1 == (ifd
= open(src
->path
->ptr
, O_RDONLY
| O_BINARY
| FIFO_NONBLOCK
))) {
686 if (-1 == (ofd
= open(dst
->path
->ptr
, O_WRONLY
|O_TRUNC
|O_CREAT
|(overwrite
? 0 : O_EXCL
), WEBDAV_FILE_MODE
))) {
687 /* opening the destination failed for some reason */
696 /* at least one part in the middle wasn't existing */
707 data
= malloc(131072);
710 while (0 < (rd
= read(ifd
, data
, 131072))) {
713 wr
= write(ofd
, data
+offset
, (size_t)(rd
-offset
));
714 } while (wr
>= 0 ? (offset
+= wr
) != rd
: (errno
== EINTR
));
716 status
= (errno
== ENOSPC
) ? 507 : 403;
721 if (0 != rd
&& 0 == status
) status
= 403;
725 if (0 != close(ofd
)) {
726 if (0 == status
) status
= (errno
== ENOSPC
) ? 507 : 403;
727 log_error_write(srv
, __FILE__
, __LINE__
, "sbss",
728 "close ", dst
->path
, "failed: ", strerror(errno
));
733 /* copy worked fine, copy connected properties */
734 sqlite3_stmt
*stmt
= p
->conf
.stmt_copy_uri
;
739 /* bind the values to the insert */
740 sqlite3_bind_text(stmt
, 1,
741 CONST_BUF_LEN(dst
->rel_path
),
744 sqlite3_bind_text(stmt
, 2,
745 CONST_BUF_LEN(src
->rel_path
),
748 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
759 static int webdav_copy_dir(server
*srv
, connection
*con
, plugin_data
*p
, physical
*src
, physical
*dst
, int overwrite
) {
763 if (NULL
!= (srcdir
= opendir(src
->path
->ptr
))) {
767 s
.path
= buffer_init();
768 s
.rel_path
= buffer_init();
770 d
.path
= buffer_init();
771 d
.rel_path
= buffer_init();
773 while (NULL
!= (de
= readdir(srcdir
))) {
776 if ((de
->d_name
[0] == '.' && de
->d_name
[1] == '\0')
777 || (de
->d_name
[0] == '.' && de
->d_name
[1] == '.' && de
->d_name
[2] == '\0')) {
781 buffer_copy_buffer(s
.path
, src
->path
);
782 buffer_append_slash(s
.path
);
783 buffer_append_string(s
.path
, de
->d_name
);
785 buffer_copy_buffer(d
.path
, dst
->path
);
786 buffer_append_slash(d
.path
);
787 buffer_append_string(d
.path
, de
->d_name
);
789 buffer_copy_buffer(s
.rel_path
, src
->rel_path
);
790 buffer_append_slash(s
.rel_path
);
791 buffer_append_string(s
.rel_path
, de
->d_name
);
793 buffer_copy_buffer(d
.rel_path
, dst
->rel_path
);
794 buffer_append_slash(d
.rel_path
);
795 buffer_append_string(d
.rel_path
, de
->d_name
);
797 if (-1 == stat(s
.path
->ptr
, &st
)) {
799 } else if (S_ISDIR(st
.st_mode
)) {
801 if (-1 == mkdir(d
.path
->ptr
, WEBDAV_DIR_MODE
) &&
806 sqlite3_stmt
*stmt
= p
->conf
.stmt_copy_uri
;
808 if (0 != (status
= webdav_copy_dir(srv
, con
, p
, &s
, &d
, overwrite
))) {
811 /* directory is copied, copy the properties too */
816 /* bind the values to the insert */
817 sqlite3_bind_text(stmt
, 1,
818 CONST_BUF_LEN(dst
->rel_path
),
821 sqlite3_bind_text(stmt
, 2,
822 CONST_BUF_LEN(src
->rel_path
),
825 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
831 } else if (S_ISREG(st
.st_mode
)) {
833 if (0 != (status
= webdav_copy_file(srv
, con
, p
, &s
, &d
, overwrite
))) {
840 buffer_free(s
.rel_path
);
842 buffer_free(d
.rel_path
);
851 static void webdav_activelock(buffer
*b
,
852 const buffer
*locktoken
, const char *lockscope
, const char *locktype
, int depth
, int timeout
) {
853 buffer_append_string_len(b
, CONST_STR_LEN("<D:activelock>\n"));
855 buffer_append_string_len(b
, CONST_STR_LEN("<D:lockscope>"));
856 buffer_append_string_len(b
, CONST_STR_LEN("<D:"));
857 buffer_append_string(b
, lockscope
);
858 buffer_append_string_len(b
, CONST_STR_LEN("/>"));
859 buffer_append_string_len(b
, CONST_STR_LEN("</D:lockscope>\n"));
861 buffer_append_string_len(b
, CONST_STR_LEN("<D:locktype>"));
862 buffer_append_string_len(b
, CONST_STR_LEN("<D:"));
863 buffer_append_string(b
, locktype
);
864 buffer_append_string_len(b
, CONST_STR_LEN("/>"));
865 buffer_append_string_len(b
, CONST_STR_LEN("</D:locktype>\n"));
867 buffer_append_string_len(b
, CONST_STR_LEN("<D:depth>"));
868 buffer_append_string(b
, depth
== 0 ? "0" : "infinity");
869 buffer_append_string_len(b
, CONST_STR_LEN("</D:depth>\n"));
871 buffer_append_string_len(b
, CONST_STR_LEN("<D:timeout>"));
872 buffer_append_string_len(b
, CONST_STR_LEN("Second-"));
873 buffer_append_int(b
, timeout
);
874 buffer_append_string_len(b
, CONST_STR_LEN("</D:timeout>\n"));
876 buffer_append_string_len(b
, CONST_STR_LEN("<D:owner>"));
877 buffer_append_string_len(b
, CONST_STR_LEN("</D:owner>\n"));
879 buffer_append_string_len(b
, CONST_STR_LEN("<D:locktoken>"));
880 buffer_append_string_len(b
, CONST_STR_LEN("<D:href>"));
881 buffer_append_string_buffer(b
, locktoken
);
882 buffer_append_string_len(b
, CONST_STR_LEN("</D:href>"));
883 buffer_append_string_len(b
, CONST_STR_LEN("</D:locktoken>\n"));
885 buffer_append_string_len(b
, CONST_STR_LEN("</D:activelock>\n"));
888 static void webdav_get_live_property_lockdiscovery(server
*srv
, connection
*con
, plugin_data
*p
, physical
*dst
, buffer
*b
) {
890 sqlite3_stmt
*stmt
= p
->conf
.stmt_read_lock_by_uri
;
891 if (!stmt
) { /*(should not happen)*/
892 buffer_append_string_len(b
, CONST_STR_LEN("<D:lockdiscovery>\n</D:lockdiscovery>\n"));
898 /* SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout
900 * WHERE resource = ? */
904 sqlite3_bind_text(stmt
, 1,
905 CONST_BUF_LEN(dst
->rel_path
),
908 buffer_append_string_len(b
, CONST_STR_LEN("<D:lockdiscovery>\n"));
909 while (SQLITE_ROW
== sqlite3_step(stmt
)) {
910 const char *lockscope
= (const char *)sqlite3_column_text(stmt
, 2);
911 const char *locktype
= (const char *)sqlite3_column_text(stmt
, 3);
912 const int depth
= sqlite3_column_int(stmt
, 5);
913 const int timeout
= sqlite3_column_int(stmt
, 6);
914 buffer locktoken
= { NULL
, 0, 0 };
915 locktoken
.ptr
= (char *)sqlite3_column_text(stmt
, 0);
916 locktoken
.used
= sqlite3_column_bytes(stmt
, 0);
917 if (locktoken
.used
) ++locktoken
.used
;
918 locktoken
.size
= locktoken
.used
;
921 webdav_activelock(b
, &locktoken
, lockscope
, locktype
, depth
, timeout
);
924 buffer_append_string_len(b
, CONST_STR_LEN("</D:lockdiscovery>\n"));
928 static int webdav_get_live_property(server
*srv
, connection
*con
, plugin_data
*p
, physical
*dst
, char *prop_name
, buffer
*b
) {
929 stat_cache_entry
*sce
= NULL
;
934 if (HANDLER_ERROR
!= (stat_cache_get_entry(srv
, con
, dst
->path
, &sce
))) {
935 char ctime_buf
[] = "2005-08-18T07:27:16Z";
936 char mtime_buf
[] = "Thu, 18 Aug 2005 07:27:16 GMT";
939 if (0 == strcmp(prop_name
, "resourcetype")) {
940 if (S_ISDIR(sce
->st
.st_mode
)) {
941 buffer_append_string_len(b
, CONST_STR_LEN("<D:resourcetype><D:collection/></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 for (k
= 0; k
< con
->conf
.mimetypes
->used
; k
++) {
950 data_string
*ds
= (data_string
*)con
->conf
.mimetypes
->data
[k
];
952 if (buffer_is_empty(ds
->key
)) continue;
954 if (buffer_is_equal_right_len(dst
->path
, ds
->key
, buffer_string_length(ds
->key
))) {
955 buffer_append_string_len(b
,CONST_STR_LEN("<D:getcontenttype>"));
956 buffer_append_string_buffer(b
, ds
->value
);
957 buffer_append_string_len(b
, CONST_STR_LEN("</D:getcontenttype>"));
964 } else if (0 == strcmp(prop_name
, "creationdate")) {
965 buffer_append_string_len(b
, CONST_STR_LEN("<D:creationdate ns0:dt=\"dateTime.tz\">"));
966 strftime(ctime_buf
, sizeof(ctime_buf
), "%Y-%m-%dT%H:%M:%SZ", gmtime(&(sce
->st
.st_ctime
)));
967 buffer_append_string(b
, ctime_buf
);
968 buffer_append_string_len(b
, CONST_STR_LEN("</D:creationdate>"));
970 } else if (0 == strcmp(prop_name
, "getlastmodified")) {
971 buffer_append_string_len(b
,CONST_STR_LEN("<D:getlastmodified ns0:dt=\"dateTime.rfc1123\">"));
972 strftime(mtime_buf
, sizeof(mtime_buf
), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&(sce
->st
.st_mtime
)));
973 buffer_append_string(b
, mtime_buf
);
974 buffer_append_string_len(b
, CONST_STR_LEN("</D:getlastmodified>"));
976 } else if (0 == strcmp(prop_name
, "getcontentlength")) {
977 buffer_append_string_len(b
,CONST_STR_LEN("<D:getcontentlength>"));
978 buffer_append_int(b
, sce
->st
.st_size
);
979 buffer_append_string_len(b
, CONST_STR_LEN("</D:getcontentlength>"));
981 } else if (0 == strcmp(prop_name
, "getcontentlanguage")) {
982 buffer_append_string_len(b
,CONST_STR_LEN("<D:getcontentlanguage>"));
983 buffer_append_string_len(b
, CONST_STR_LEN("en"));
984 buffer_append_string_len(b
, CONST_STR_LEN("</D:getcontentlanguage>"));
986 } else if (0 == strcmp(prop_name
, "getetag")) {
987 etag_create(con
->physical
.etag
, &sce
->st
, con
->etag_flags
);
988 buffer_append_string_len(b
, CONST_STR_LEN("<D:getetag>"));
989 buffer_append_string_buffer(b
, con
->physical
.etag
);
990 buffer_append_string_len(b
, CONST_STR_LEN("</D:getetag>"));
991 buffer_reset(con
->physical
.etag
);
994 } else if (0 == strcmp(prop_name
, "lockdiscovery")) {
995 webdav_get_live_property_lockdiscovery(srv
, con
, p
, dst
, b
);
997 } else if (0 == strcmp(prop_name
, "supportedlock")) {
998 buffer_append_string_len(b
,CONST_STR_LEN("<D:supportedlock>"));
999 buffer_append_string_len(b
,CONST_STR_LEN("<D:lockentry>"));
1000 buffer_append_string_len(b
,CONST_STR_LEN("<D:lockscope><D:exclusive/></D:lockscope>"));
1001 buffer_append_string_len(b
,CONST_STR_LEN("<D:locktype><D:write/></D:locktype>"));
1002 buffer_append_string_len(b
,CONST_STR_LEN("</D:lockentry>"));
1003 buffer_append_string_len(b
, CONST_STR_LEN("</D:supportedlock>"));
1009 return found
? 0 : -1;
1012 static int webdav_get_property(server
*srv
, connection
*con
, plugin_data
*p
, physical
*dst
, char *prop_name
, char *prop_ns
, buffer
*b
) {
1013 if (0 == strcmp(prop_ns
, "DAV:")) {
1014 /* a local 'live' property */
1015 return webdav_get_live_property(srv
, con
, p
, dst
, prop_name
, b
);
1018 #ifdef USE_PROPPATCH
1019 sqlite3_stmt
*stmt
= p
->conf
.stmt_select_prop
;
1022 /* perhaps it is in sqlite3 */
1023 sqlite3_reset(stmt
);
1025 /* bind the values to the insert */
1027 sqlite3_bind_text(stmt
, 1,
1028 CONST_BUF_LEN(dst
->rel_path
),
1030 sqlite3_bind_text(stmt
, 2,
1034 sqlite3_bind_text(stmt
, 3,
1040 while (SQLITE_ROW
== sqlite3_step(stmt
)) {
1041 /* there is a row for us, we only expect a single col 'value' */
1042 webdav_gen_prop_tag(srv
, con
, prop_name
, prop_ns
, (char *)sqlite3_column_text(stmt
, 0), b
);
1047 return found
? 0 : -1;
1059 static webdav_property live_properties
[] = {
1060 { "DAV:", "creationdate" },
1061 /*{ "DAV:", "displayname" },*//*(not implemented)*/
1062 { "DAV:", "getcontentlanguage" },
1063 { "DAV:", "getcontentlength" },
1064 { "DAV:", "getcontenttype" },
1065 { "DAV:", "getetag" },
1066 { "DAV:", "getlastmodified" },
1067 { "DAV:", "resourcetype" },
1068 /*{ "DAV:", "source" },*//*(not implemented)*/
1070 { "DAV:", "lockdiscovery" },
1071 { "DAV:", "supportedlock" },
1078 webdav_property
**ptr
;
1082 } webdav_properties
;
1084 static int webdav_get_props(server
*srv
, connection
*con
, plugin_data
*p
, physical
*dst
, webdav_properties
*props
, buffer
*b_200
, buffer
*b_404
) {
1087 if (props
&& props
->used
) {
1088 for (i
= 0; i
< props
->used
; i
++) {
1089 webdav_property
*prop
;
1091 prop
= props
->ptr
[i
];
1093 if (0 != webdav_get_property(srv
, con
, p
,
1094 dst
, prop
->prop
, prop
->ns
, b_200
)) {
1095 webdav_gen_prop_tag(srv
, con
, prop
->prop
, prop
->ns
, NULL
, b_404
);
1099 for (i
= 0; live_properties
[i
].prop
; i
++) {
1100 /* a local 'live' property */
1101 webdav_get_live_property(srv
, con
, p
, dst
, live_properties
[i
].prop
, b_200
);
1108 #ifdef USE_PROPPATCH
1109 static int webdav_parse_chunkqueue(server
*srv
, connection
*con
, plugin_data
*p
, chunkqueue
*cq
, xmlDoc
**ret_xml
) {
1110 xmlParserCtxtPtr ctxt
;
1119 /* read the chunks in to the XML document */
1120 ctxt
= xmlCreatePushParserCtxt(NULL
, NULL
, NULL
, 0, NULL
);
1122 for (c
= cq
->first
; cq
->bytes_out
!= cq
->bytes_in
; c
= cq
->first
) {
1123 size_t weWant
= cq
->bytes_out
- cq
->bytes_in
;
1130 weHave
= c
->file
.length
- c
->offset
;
1132 if (weHave
> weWant
) weHave
= weWant
;
1134 /* xml chunks are always memory, mmap() is our friend */
1135 mapped
= (c
->file
.mmap
.start
!= MAP_FAILED
);
1137 data
= c
->file
.mmap
.start
+ c
->offset
;
1139 if (-1 == c
->file
.fd
&& /* open the file if not already open */
1140 -1 == (c
->file
.fd
= open(c
->file
.name
->ptr
, O_RDONLY
))) {
1141 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "open failed: ", strerror(errno
));
1146 if (MAP_FAILED
!= (c
->file
.mmap
.start
= mmap(0, c
->file
.length
, PROT_READ
, MAP_PRIVATE
, c
->file
.fd
, 0))) {
1147 /* chunk_reset() or chunk_free() will cleanup for us */
1148 c
->file
.mmap
.length
= c
->file
.length
;
1149 data
= c
->file
.mmap
.start
+ c
->offset
;
1153 if (weHave
> 65536) weHave
= 65536;
1154 data
= malloc(weHave
);
1156 if (-1 == lseek(c
->file
.fd
, c
->file
.start
+ c
->offset
, SEEK_SET
)
1157 || 0 > (rd
= read(c
->file
.fd
, data
, weHave
))) {
1158 log_error_write(srv
, __FILE__
, __LINE__
, "ssbd", "lseek/read failed: ",
1159 strerror(errno
), c
->file
.name
, c
->file
.fd
);
1163 weHave
= (size_t)rd
;
1167 if (XML_ERR_OK
!= (err
= xmlParseChunk(ctxt
, data
, weHave
, 0))) {
1168 log_error_write(srv
, __FILE__
, __LINE__
, "sodd", "xmlParseChunk failed at:", cq
->bytes_out
, weHave
, err
);
1171 chunkqueue_mark_written(cq
, weHave
);
1173 if (!mapped
) free(data
);
1176 /* append to the buffer */
1177 weHave
= buffer_string_length(c
->mem
) - c
->offset
;
1179 if (weHave
> weWant
) weHave
= weWant
;
1181 if (p
->conf
.log_xml
) {
1182 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "XML-request-body:", c
->mem
->ptr
+ c
->offset
);
1185 if (XML_ERR_OK
!= (err
= xmlParseChunk(ctxt
, c
->mem
->ptr
+ c
->offset
, weHave
, 0))) {
1186 log_error_write(srv
, __FILE__
, __LINE__
, "sodd", "xmlParseChunk failed at:", cq
->bytes_out
, weHave
, err
);
1189 chunkqueue_mark_written(cq
, weHave
);
1195 switch ((err
= xmlParseChunk(ctxt
, 0, 0, 1))) {
1196 case XML_ERR_DOCUMENT_END
:
1200 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "xmlParseChunk failed at final packet:", err
);
1205 res
= ctxt
->wellFormed
;
1206 xmlFreeParserCtxt(ctxt
);
1219 static int webdav_lockdiscovery(server
*srv
, connection
*con
,
1220 buffer
*locktoken
, const char *lockscope
, const char *locktype
, int depth
) {
1222 buffer
*b
= buffer_init();
1224 response_header_overwrite(srv
, con
, CONST_STR_LEN("Lock-Token"), CONST_BUF_LEN(locktoken
));
1226 response_header_overwrite(srv
, con
,
1227 CONST_STR_LEN("Content-Type"),
1228 CONST_STR_LEN("text/xml; charset=\"utf-8\""));
1230 buffer_copy_string_len(b
, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"));
1232 buffer_append_string_len(b
,CONST_STR_LEN("<D:prop xmlns:D=\"DAV:\" xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\">\n"));
1233 buffer_append_string_len(b
,CONST_STR_LEN("<D:lockdiscovery>\n"));
1234 webdav_activelock(b
, locktoken
, lockscope
, locktype
, depth
, 600);
1235 buffer_append_string_len(b
,CONST_STR_LEN("</D:lockdiscovery>\n"));
1236 buffer_append_string_len(b
,CONST_STR_LEN("</D:prop>\n"));
1238 chunkqueue_append_buffer(con
->write_queue
, b
);
1246 * check if resource is having the right locks to access to resource
1251 static int webdav_has_lock(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*uri
) {
1259 * This implementation is more fake than real
1260 * we need a parser for the If: header to really handle the full scope
1262 * X-Litmus: locks: 11 (owner_modify)
1263 * If: <http://127.0.0.1:1025/dav/litmus/lockme> (<opaquelocktoken:2165478d-0611-49c4-be92-e790d68a38f1>)
1265 * if http://127.0.0.1:1025/dav/litmus/lockme is locked with
1266 * opaquelocktoken:2165478d-0611-49c4-be92-e790d68a38f1, go on
1268 * X-Litmus: locks: 16 (fail_cond_put)
1269 * If: (<DAV:no-lock> ["-1622396671"])
1271 * go on if the resource has the etag [...] and the lock
1273 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "If"))) {
1274 /* Ooh, ooh. A if tag, now the fun begins.
1276 * this can only work with a real parser
1279 /* we didn't provided a lock-token -> */
1280 /* if the resource is locked -> 423 */
1282 sqlite3_stmt
*stmt
= p
->conf
.stmt_read_lock_by_uri
;
1284 sqlite3_reset(stmt
);
1286 sqlite3_bind_text(stmt
, 1,
1290 while (SQLITE_ROW
== sqlite3_step(stmt
)) {
1305 SUBREQUEST_FUNC(mod_webdav_subrequest_handler_huge
) {
1306 plugin_data
*p
= p_d
;
1310 int depth
= -1; /* (Depth: infinity) */
1314 webdav_properties
*req_props
;
1315 stat_cache_entry
*sce
= NULL
;
1319 if (!p
->conf
.enabled
) return HANDLER_GO_ON
;
1320 /* physical path is setup */
1321 if (buffer_is_empty(con
->physical
.path
)) return HANDLER_GO_ON
;
1323 /* PROPFIND need them */
1324 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "Depth")) && 1 == buffer_string_length(ds
->value
)) {
1325 if ('0' == *ds
->value
->ptr
) {
1327 } else if ('1' == *ds
->value
->ptr
) {
1330 } /* else treat as Depth: infinity */
1332 switch (con
->request
.http_method
) {
1333 case HTTP_METHOD_PROPFIND
:
1334 /* they want to know the properties of the directory */
1337 /* is there a content-body ? */
1339 switch (stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) {
1341 if (errno
== ENOENT
) {
1342 con
->http_status
= 404;
1343 return HANDLER_FINISHED
;
1350 if (S_ISDIR(sce
->st
.st_mode
) && con
->physical
.path
->ptr
[buffer_string_length(con
->physical
.path
)-1] != '/') {
1351 http_response_redirect_to_directory(srv
, con
);
1352 return HANDLER_FINISHED
;
1355 #ifdef USE_PROPPATCH
1356 /* any special requests or just allprop ? */
1357 if (con
->request
.content_length
) {
1360 if (con
->state
== CON_STATE_READ_POST
) {
1361 handler_t r
= connection_handle_read_post_state(srv
, con
);
1362 if (r
!= HANDLER_GO_ON
) return r
;
1365 if (1 == webdav_parse_chunkqueue(srv
, con
, p
, con
->request_content_queue
, &xml
)) {
1366 xmlNode
*rootnode
= xmlDocGetRootElement(xml
);
1368 force_assert(rootnode
);
1370 if (0 == xmlStrcmp(rootnode
->name
, BAD_CAST
"propfind")) {
1373 req_props
= calloc(1, sizeof(*req_props
));
1375 for (cmd
= rootnode
->children
; cmd
; cmd
= cmd
->next
) {
1377 if (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"prop")) {
1378 /* get prop by name */
1381 for (prop
= cmd
->children
; prop
; prop
= prop
->next
) {
1382 if (prop
->type
== XML_TEXT_NODE
) continue; /* ignore WS */
1385 (0 == xmlStrcmp(prop
->ns
->href
, BAD_CAST
"")) &&
1386 (0 != xmlStrcmp(prop
->ns
->prefix
, BAD_CAST
""))) {
1388 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
1389 "no name space for:",
1394 for (i
= 0; i
< req_props
->used
; i
++) {
1395 free(req_props
->ptr
[i
]->ns
);
1396 free(req_props
->ptr
[i
]->prop
);
1397 free(req_props
->ptr
[i
]);
1399 free(req_props
->ptr
);
1402 con
->http_status
= 400;
1403 return HANDLER_FINISHED
;
1406 /* add property to requested list */
1407 if (req_props
->size
== 0) {
1408 req_props
->size
= 16;
1409 req_props
->ptr
= malloc(sizeof(*(req_props
->ptr
)) * req_props
->size
);
1410 } else if (req_props
->used
== req_props
->size
) {
1411 req_props
->size
+= 16;
1412 req_props
->ptr
= realloc(req_props
->ptr
, sizeof(*(req_props
->ptr
)) * req_props
->size
);
1415 req_props
->ptr
[req_props
->used
] = malloc(sizeof(webdav_property
));
1416 req_props
->ptr
[req_props
->used
]->ns
= (char *)xmlStrdup(prop
->ns
? prop
->ns
->href
: (xmlChar
*)"");
1417 req_props
->ptr
[req_props
->used
]->prop
= (char *)xmlStrdup(prop
->name
);
1420 } else if (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"propname")) {
1421 sqlite3_stmt
*stmt
= p
->conf
.stmt_select_propnames
;
1424 /* get all property names (EMPTY) */
1425 sqlite3_reset(stmt
);
1426 /* bind the values to the insert */
1428 sqlite3_bind_text(stmt
, 1,
1429 CONST_BUF_LEN(con
->uri
.path
),
1432 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
1435 } else if (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"allprop")) {
1436 /* get all properties (EMPTY) */
1443 con
->http_status
= 400;
1444 return HANDLER_FINISHED
;
1448 con
->http_status
= 207;
1450 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/xml; charset=\"utf-8\""));
1454 buffer_copy_string_len(b
, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"));
1456 buffer_append_string_len(b
,CONST_STR_LEN("<D:multistatus xmlns:D=\"DAV:\" xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\">\n"));
1460 prop_200
= buffer_init();
1461 prop_404
= buffer_init();
1464 /* Depth: 0 or Depth: 1 */
1465 webdav_get_props(srv
, con
, p
, &(con
->physical
), req_props
, prop_200
, prop_404
);
1467 buffer_append_string_len(b
,CONST_STR_LEN("<D:response>\n"));
1468 buffer_append_string_len(b
,CONST_STR_LEN("<D:href>"));
1469 buffer_append_string_buffer(b
, con
->uri
.scheme
);
1470 buffer_append_string_len(b
,CONST_STR_LEN("://"));
1471 buffer_append_string_buffer(b
, con
->uri
.authority
);
1472 buffer_append_string_encoded(b
, CONST_BUF_LEN(con
->uri
.path
), ENCODING_REL_URI
);
1473 buffer_append_string_len(b
,CONST_STR_LEN("</D:href>\n"));
1475 if (!buffer_string_is_empty(prop_200
)) {
1476 buffer_append_string_len(b
,CONST_STR_LEN("<D:propstat>\n"));
1477 buffer_append_string_len(b
,CONST_STR_LEN("<D:prop>\n"));
1479 buffer_append_string_buffer(b
, prop_200
);
1481 buffer_append_string_len(b
,CONST_STR_LEN("</D:prop>\n"));
1483 buffer_append_string_len(b
,CONST_STR_LEN("<D:status>HTTP/1.1 200 OK</D:status>\n"));
1485 buffer_append_string_len(b
,CONST_STR_LEN("</D:propstat>\n"));
1487 if (!buffer_string_is_empty(prop_404
)) {
1488 buffer_append_string_len(b
,CONST_STR_LEN("<D:propstat>\n"));
1489 buffer_append_string_len(b
,CONST_STR_LEN("<D:prop>\n"));
1491 buffer_append_string_buffer(b
, prop_404
);
1493 buffer_append_string_len(b
,CONST_STR_LEN("</D:prop>\n"));
1495 buffer_append_string_len(b
,CONST_STR_LEN("<D:status>HTTP/1.1 404 Not Found</D:status>\n"));
1497 buffer_append_string_len(b
,CONST_STR_LEN("</D:propstat>\n"));
1500 buffer_append_string_len(b
,CONST_STR_LEN("</D:response>\n"));
1505 if (NULL
!= (dir
= opendir(con
->physical
.path
->ptr
))) {
1508 physical
*dst
= &(con
->physical
);
1510 d
.path
= buffer_init();
1511 d
.rel_path
= buffer_init();
1513 while(NULL
!= (de
= readdir(dir
))) {
1514 if (de
->d_name
[0] == '.' && (de
->d_name
[1] == '\0' || (de
->d_name
[1] == '.' && de
->d_name
[2] == '\0'))) {
1516 /* ignore the parent and target dir */
1519 buffer_copy_buffer(d
.path
, dst
->path
);
1520 buffer_append_slash(d
.path
);
1522 buffer_copy_buffer(d
.rel_path
, dst
->rel_path
);
1523 buffer_append_slash(d
.rel_path
);
1525 buffer_append_string(d
.path
, de
->d_name
);
1526 buffer_append_string(d
.rel_path
, de
->d_name
);
1528 buffer_reset(prop_200
);
1529 buffer_reset(prop_404
);
1531 webdav_get_props(srv
, con
, p
, &d
, req_props
, prop_200
, prop_404
);
1533 buffer_append_string_len(b
,CONST_STR_LEN("<D:response>\n"));
1534 buffer_append_string_len(b
,CONST_STR_LEN("<D:href>"));
1535 buffer_append_string_buffer(b
, con
->uri
.scheme
);
1536 buffer_append_string_len(b
,CONST_STR_LEN("://"));
1537 buffer_append_string_buffer(b
, con
->uri
.authority
);
1538 buffer_append_string_encoded(b
, CONST_BUF_LEN(d
.rel_path
), ENCODING_REL_URI
);
1539 if (0 == stat(d
.path
->ptr
, &st
) && S_ISDIR(st
.st_mode
)) {
1540 /* Append a '/' on subdirectories */
1541 buffer_append_string_len(b
,CONST_STR_LEN("/"));
1543 buffer_append_string_len(b
,CONST_STR_LEN("</D:href>\n"));
1545 if (!buffer_string_is_empty(prop_200
)) {
1546 buffer_append_string_len(b
,CONST_STR_LEN("<D:propstat>\n"));
1547 buffer_append_string_len(b
,CONST_STR_LEN("<D:prop>\n"));
1549 buffer_append_string_buffer(b
, prop_200
);
1551 buffer_append_string_len(b
,CONST_STR_LEN("</D:prop>\n"));
1553 buffer_append_string_len(b
,CONST_STR_LEN("<D:status>HTTP/1.1 200 OK</D:status>\n"));
1555 buffer_append_string_len(b
,CONST_STR_LEN("</D:propstat>\n"));
1557 if (!buffer_string_is_empty(prop_404
)) {
1558 buffer_append_string_len(b
,CONST_STR_LEN("<D:propstat>\n"));
1559 buffer_append_string_len(b
,CONST_STR_LEN("<D:prop>\n"));
1561 buffer_append_string_buffer(b
, prop_404
);
1563 buffer_append_string_len(b
,CONST_STR_LEN("</D:prop>\n"));
1565 buffer_append_string_len(b
,CONST_STR_LEN("<D:status>HTTP/1.1 404 Not Found</D:status>\n"));
1567 buffer_append_string_len(b
,CONST_STR_LEN("</D:propstat>\n"));
1570 buffer_append_string_len(b
,CONST_STR_LEN("</D:response>\n"));
1573 buffer_free(d
.path
);
1574 buffer_free(d
.rel_path
);
1581 for (i
= 0; i
< req_props
->used
; i
++) {
1582 free(req_props
->ptr
[i
]->ns
);
1583 free(req_props
->ptr
[i
]->prop
);
1584 free(req_props
->ptr
[i
]);
1586 free(req_props
->ptr
);
1590 buffer_free(prop_200
);
1591 buffer_free(prop_404
);
1593 buffer_append_string_len(b
,CONST_STR_LEN("</D:multistatus>\n"));
1595 if (p
->conf
.log_xml
) {
1596 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "XML-response-body:", b
);
1599 chunkqueue_append_buffer(con
->write_queue
, b
);
1602 con
->file_finished
= 1;
1604 return HANDLER_FINISHED
;
1605 case HTTP_METHOD_MKCOL
:
1606 if (p
->conf
.is_readonly
) {
1607 con
->http_status
= 403;
1608 return HANDLER_FINISHED
;
1611 if (con
->request
.content_length
!= 0) {
1612 /* we don't support MKCOL with a body */
1613 con
->http_status
= 415;
1615 return HANDLER_FINISHED
;
1618 /* let's create the directory */
1620 if (-1 == mkdir(con
->physical
.path
->ptr
, WEBDAV_DIR_MODE
)) {
1623 con
->http_status
= 403;
1627 con
->http_status
= 409;
1631 con
->http_status
= 405; /* not allowed */
1635 con
->http_status
= 201;
1636 con
->file_finished
= 1;
1639 return HANDLER_FINISHED
;
1640 case HTTP_METHOD_DELETE
:
1641 if (p
->conf
.is_readonly
) {
1642 con
->http_status
= 403;
1643 return HANDLER_FINISHED
;
1646 /* does the client have a lock for this connection ? */
1647 if (!webdav_has_lock(srv
, con
, p
, con
->uri
.path
)) {
1648 con
->http_status
= 423;
1649 return HANDLER_FINISHED
;
1652 /* stat and unlink afterwards */
1653 if (-1 == stat(con
->physical
.path
->ptr
, &st
)) {
1654 /* don't about it yet, unlink will fail too */
1657 con
->http_status
= 404;
1660 con
->http_status
= 403;
1663 } else if (S_ISDIR(st
.st_mode
)) {
1664 buffer
*multi_status_resp
;
1666 if (con
->physical
.path
->ptr
[buffer_string_length(con
->physical
.path
)-1] != '/') {
1667 http_response_redirect_to_directory(srv
, con
);
1668 return HANDLER_FINISHED
;
1671 multi_status_resp
= buffer_init();
1673 if (webdav_delete_dir(srv
, con
, p
, &(con
->physical
), multi_status_resp
)) {
1674 /* we got an error somewhere in between, build a 207 */
1675 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/xml; charset=\"utf-8\""));
1679 buffer_copy_string_len(b
, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"));
1681 buffer_append_string_len(b
,CONST_STR_LEN("<D:multistatus xmlns:D=\"DAV:\">\n"));
1683 buffer_append_string_buffer(b
, multi_status_resp
);
1685 buffer_append_string_len(b
,CONST_STR_LEN("</D:multistatus>\n"));
1687 if (p
->conf
.log_xml
) {
1688 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "XML-response-body:", b
);
1691 chunkqueue_append_buffer(con
->write_queue
, b
);
1694 con
->http_status
= 207;
1695 con
->file_finished
= 1;
1697 /* everything went fine, remove the directory */
1699 if (-1 == rmdir(con
->physical
.path
->ptr
)) {
1702 con
->http_status
= 404;
1705 con
->http_status
= 501;
1709 con
->http_status
= 204;
1713 buffer_free(multi_status_resp
);
1714 } else if (-1 == unlink(con
->physical
.path
->ptr
)) {
1717 con
->http_status
= 403;
1720 con
->http_status
= 404;
1723 con
->http_status
= 501;
1727 con
->http_status
= 204;
1729 return HANDLER_FINISHED
;
1730 case HTTP_METHOD_PUT
: {
1732 chunkqueue
*cq
= con
->request_content_queue
;
1734 data_string
*ds_range
;
1736 if (p
->conf
.is_readonly
) {
1737 con
->http_status
= 403;
1738 return HANDLER_FINISHED
;
1741 /* is a exclusive lock set on the source */
1742 /* (check for lock once before potentially reading large input) */
1743 if (0 == cq
->bytes_in
&& !webdav_has_lock(srv
, con
, p
, con
->uri
.path
)) {
1744 con
->http_status
= 423;
1745 return HANDLER_FINISHED
;
1748 if (con
->state
== CON_STATE_READ_POST
) {
1749 handler_t r
= connection_handle_read_post_state(srv
, con
);
1750 if (r
!= HANDLER_GO_ON
) return r
;
1753 /* RFC2616 Section 9.6 PUT requires us to send 501 on all Content-* we don't support
1754 * - most important Content-Range
1757 * Example: Content-Range: bytes 100-1037/1038 */
1759 if (NULL
!= (ds_range
= (data_string
*)array_get_element(con
->request
.headers
, "Content-Range"))) {
1760 const char *num
= ds_range
->value
->ptr
;
1764 if (0 != strncmp(num
, "bytes ", 6)) {
1765 con
->http_status
= 501; /* not implemented */
1767 return HANDLER_FINISHED
;
1770 /* we only support <num>- ... */
1775 while (*num
== ' ' || *num
== '\t') num
++;
1778 con
->http_status
= 501; /* not implemented */
1780 return HANDLER_FINISHED
;
1783 offset
= strtoll(num
, &err
, 10);
1785 if (*err
!= '-' || offset
< 0) {
1786 con
->http_status
= 501; /* not implemented */
1788 return HANDLER_FINISHED
;
1791 if (-1 == (fd
= open(con
->physical
.path
->ptr
, O_WRONLY
, WEBDAV_FILE_MODE
))) {
1794 con
->http_status
= 404; /* not found */
1797 con
->http_status
= 403; /* not found */
1800 return HANDLER_FINISHED
;
1803 if (-1 == lseek(fd
, offset
, SEEK_SET
)) {
1804 con
->http_status
= 501; /* not implemented */
1808 return HANDLER_FINISHED
;
1810 con
->http_status
= 200; /* modified */
1812 /* take what we have in the request-body and write it to a file */
1814 /* if the file doesn't exist, create it */
1815 if (-1 == (fd
= open(con
->physical
.path
->ptr
, O_WRONLY
|O_TRUNC
, WEBDAV_FILE_MODE
))) {
1816 if (errno
!= ENOENT
||
1817 -1 == (fd
= open(con
->physical
.path
->ptr
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
, WEBDAV_FILE_MODE
))) {
1818 /* we can't open the file */
1819 con
->http_status
= 403;
1821 return HANDLER_FINISHED
;
1823 con
->http_status
= 201; /* created */
1826 con
->http_status
= 200; /* modified */
1830 con
->file_finished
= 1;
1832 for (c
= cq
->first
; c
; c
= cq
->first
) {
1838 /* copy all chunks */
1842 mapped
= (c
->file
.mmap
.start
!= MAP_FAILED
);
1843 dlen
= c
->file
.length
- c
->offset
;
1845 data
= c
->file
.mmap
.start
+ c
->offset
;
1847 if (-1 == c
->file
.fd
&& /* open the file if not already open */
1848 -1 == (c
->file
.fd
= open(c
->file
.name
->ptr
, O_RDONLY
))) {
1849 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "open failed: ", strerror(errno
));
1851 return HANDLER_ERROR
;
1854 if (MAP_FAILED
!= (c
->file
.mmap
.start
= mmap(NULL
, c
->file
.length
, PROT_READ
, MAP_PRIVATE
, c
->file
.fd
, 0))) {
1855 /* chunk_reset() or chunk_free() will cleanup for us */
1856 c
->file
.mmap
.length
= c
->file
.length
;
1857 data
= c
->file
.mmap
.start
+ c
->offset
;
1861 if (dlen
> 65536) dlen
= 65536;
1862 data
= malloc(dlen
);
1864 if (-1 == lseek(c
->file
.fd
, c
->file
.start
+ c
->offset
, SEEK_SET
)
1865 || 0 > (rd
= read(c
->file
.fd
, data
, dlen
))) {
1866 log_error_write(srv
, __FILE__
, __LINE__
, "ssbd", "lseek/read failed: ",
1867 strerror(errno
), c
->file
.name
, c
->file
.fd
);
1870 return HANDLER_ERROR
;
1877 if ((r
= write(fd
, data
, dlen
)) < 0) {
1880 con
->http_status
= 507;
1884 con
->http_status
= 403;
1889 if (!mapped
) free(data
);
1892 if ((r
= write(fd
, c
->mem
->ptr
+ c
->offset
, buffer_string_length(c
->mem
) - c
->offset
)) < 0) {
1895 con
->http_status
= 507;
1899 con
->http_status
= 403;
1907 chunkqueue_mark_written(cq
, r
);
1912 if (0 != close(fd
)) {
1913 log_error_write(srv
, __FILE__
, __LINE__
, "sbss",
1914 "close ", con
->physical
.path
, "failed: ", strerror(errno
));
1915 return HANDLER_ERROR
;
1918 return HANDLER_FINISHED
;
1920 case HTTP_METHOD_MOVE
:
1921 case HTTP_METHOD_COPY
: {
1922 buffer
*destination
= NULL
;
1923 char *sep
, *sep2
, *start
;
1926 if (p
->conf
.is_readonly
) {
1927 con
->http_status
= 403;
1928 return HANDLER_FINISHED
;
1931 /* is a exclusive lock set on the source */
1932 if (con
->request
.http_method
== HTTP_METHOD_MOVE
) {
1933 if (!webdav_has_lock(srv
, con
, p
, con
->uri
.path
)) {
1934 con
->http_status
= 423;
1935 return HANDLER_FINISHED
;
1939 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "Destination"))) {
1940 destination
= ds
->value
;
1942 con
->http_status
= 400;
1943 return HANDLER_FINISHED
;
1946 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "Overwrite"))) {
1947 if (buffer_string_length(ds
->value
) != 1 ||
1948 (ds
->value
->ptr
[0] != 'F' &&
1949 ds
->value
->ptr
[0] != 'T') ) {
1950 con
->http_status
= 400;
1951 return HANDLER_FINISHED
;
1953 overwrite
= (ds
->value
->ptr
[0] == 'F' ? 0 : 1);
1955 /* let's parse the Destination
1957 * http://127.0.0.1:1025/dav/litmus/copydest
1959 * - host has to be the same as the Host: header we got
1960 * - we have to stay inside the document root
1961 * - the query string is thrown away
1964 buffer_reset(p
->uri
.scheme
);
1965 buffer_reset(p
->uri
.path_raw
);
1966 buffer_reset(p
->uri
.authority
);
1968 start
= destination
->ptr
;
1970 if (NULL
== (sep
= strstr(start
, "://"))) {
1971 con
->http_status
= 400;
1972 return HANDLER_FINISHED
;
1974 buffer_copy_string_len(p
->uri
.scheme
, start
, sep
- start
);
1978 if (NULL
== (sep
= strchr(start
, '/'))) {
1979 con
->http_status
= 400;
1980 return HANDLER_FINISHED
;
1982 if (NULL
!= (sep2
= memchr(start
, '@', sep
- start
))) {
1983 /* skip login information */
1986 buffer_copy_string_len(p
->uri
.authority
, start
, sep
- start
);
1990 if (NULL
== (sep
= strchr(start
, '?'))) {
1991 /* no query string, good */
1992 buffer_copy_string(p
->uri
.path_raw
, start
);
1994 buffer_copy_string_len(p
->uri
.path_raw
, start
, sep
- start
);
1997 if (!buffer_is_equal(p
->uri
.authority
, con
->uri
.authority
)) {
1998 /* not the same host */
1999 con
->http_status
= 502;
2000 return HANDLER_FINISHED
;
2003 buffer_copy_buffer(p
->tmp_buf
, p
->uri
.path_raw
);
2004 buffer_urldecode_path(p
->tmp_buf
);
2005 buffer_path_simplify(p
->uri
.path
, p
->tmp_buf
);
2007 /* we now have a URI which is clean. transform it into a physical path */
2008 buffer_copy_buffer(p
->physical
.doc_root
, con
->physical
.doc_root
);
2009 buffer_copy_buffer(p
->physical
.rel_path
, p
->uri
.path
);
2011 if (con
->conf
.force_lowercase_filenames
) {
2012 buffer_to_lower(p
->physical
.rel_path
);
2015 /* Destination physical path
2016 * src con->physical.path might have been remapped with mod_alias.
2017 * (but mod_alias does not modify con->physical.rel_path)
2018 * Find matching prefix to support use of mod_alias to remap webdav root.
2019 * Aliasing of paths underneath the webdav root might not work.
2020 * Likewise, mod_rewrite URL rewriting might thwart this comparison.
2021 * Use mod_redirect instead of mod_alias to remap paths *under* webdav root.
2022 * Use mod_redirect instead of mod_rewrite on *any* parts of path to webdav.
2023 * (Related, use mod_auth to protect webdav root, but avoid attempting to
2024 * use mod_auth on paths underneath webdav root, as Destination is not
2025 * validated with mod_auth)
2027 * tl;dr: webdav paths and webdav properties are managed by mod_webdav,
2028 * so do not modify paths externally or else undefined behavior
2029 * or corruption may occur
2032 /* find matching URI prefix
2033 * check if remaining con->physical.rel_path matches suffix
2034 * of con->physical.basedir so that we can use it to
2035 * remap Destination physical path */
2037 sep
= con
->uri
.path
->ptr
;
2038 sep2
= p
->uri
.path
->ptr
;
2039 for (i
= 0; sep
[i
] && sep
[i
] == sep2
[i
]; ++i
) ;
2040 if (sep
[i
] == '\0' && (sep2
[i
] == '\0' || sep2
[i
] == '/' || (i
> 0 && sep
[i
-1] == '/'))) {
2041 /* src and dst URI match or dst is nested inside src; invalid COPY or MOVE */
2042 con
->http_status
= 403;
2043 return HANDLER_FINISHED
;
2045 while (i
!= 0 && sep
[--i
] != '/') ; /* find matching directory path */
2046 remain
= buffer_string_length(con
->uri
.path
) - i
;
2047 if (!con
->conf
.force_lowercase_filenames
2048 ? buffer_is_equal_right_len(con
->physical
.path
, con
->physical
.rel_path
, remain
)
2049 :(buffer_string_length(con
->physical
.path
) >= remain
2050 && 0 == strncasecmp(con
->physical
.path
->ptr
+buffer_string_length(con
->physical
.path
)-remain
, con
->physical
.rel_path
->ptr
+i
, remain
))) {
2051 /* (at this point, p->physical.rel_path is identical to (or lowercased version of) p->uri.path) */
2052 buffer_copy_string_len(p
->physical
.path
, con
->physical
.path
->ptr
, buffer_string_length(con
->physical
.path
)-remain
);
2053 buffer_append_string_len(p
->physical
.path
, p
->physical
.rel_path
->ptr
+i
, buffer_string_length(p
->physical
.rel_path
)-i
);
2055 buffer_copy_buffer(p
->physical
.basedir
, con
->physical
.basedir
);
2056 buffer_append_slash(p
->physical
.basedir
);
2058 /* unable to perform physical path remap here;
2059 * assume doc_root/rel_path and no remapping */
2060 buffer_copy_buffer(p
->physical
.path
, p
->physical
.doc_root
);
2061 buffer_append_slash(p
->physical
.path
);
2062 buffer_copy_buffer(p
->physical
.basedir
, p
->physical
.path
);
2064 /* don't add a second / */
2065 if (p
->physical
.rel_path
->ptr
[0] == '/') {
2066 buffer_append_string_len(p
->physical
.path
, p
->physical
.rel_path
->ptr
+ 1, buffer_string_length(p
->physical
.rel_path
) - 1);
2068 buffer_append_string_buffer(p
->physical
.path
, p
->physical
.rel_path
);
2073 /* let's see if the source is a directory
2074 * if yes, we fail with 501 */
2076 if (-1 == stat(con
->physical
.path
->ptr
, &st
)) {
2077 /* don't about it yet, unlink will fail too */
2080 con
->http_status
= 404;
2083 con
->http_status
= 403;
2086 } else if (S_ISDIR(st
.st_mode
)) {
2089 /* src is a directory */
2091 if (con
->physical
.path
->ptr
[buffer_string_length(con
->physical
.path
)-1] != '/') {
2092 http_response_redirect_to_directory(srv
, con
);
2093 return HANDLER_FINISHED
;
2096 if (-1 == stat(p
->physical
.path
->ptr
, &st
)) {
2097 if (-1 == mkdir(p
->physical
.path
->ptr
, WEBDAV_DIR_MODE
)) {
2098 con
->http_status
= 403;
2099 return HANDLER_FINISHED
;
2102 } else if (!S_ISDIR(st
.st_mode
)) {
2103 if (overwrite
== 0) {
2104 /* copying into a non-dir ? */
2105 con
->http_status
= 409;
2106 return HANDLER_FINISHED
;
2108 unlink(p
->physical
.path
->ptr
);
2109 if (-1 == mkdir(p
->physical
.path
->ptr
, WEBDAV_DIR_MODE
)) {
2110 con
->http_status
= 403;
2111 return HANDLER_FINISHED
;
2117 /* copy the content of src to dest */
2118 if (0 != (r
= webdav_copy_dir(srv
, con
, p
, &(con
->physical
), &(p
->physical
), overwrite
))) {
2119 con
->http_status
= r
;
2120 return HANDLER_FINISHED
;
2122 if (con
->request
.http_method
== HTTP_METHOD_MOVE
) {
2124 webdav_delete_dir(srv
, con
, p
, &(con
->physical
), b
); /* content */
2127 rmdir(con
->physical
.path
->ptr
);
2129 con
->http_status
= created
? 201 : 204;
2130 con
->file_finished
= 1;
2132 /* it is just a file, good */
2136 /* does the client have a lock for this connection ? */
2137 if (!webdav_has_lock(srv
, con
, p
, p
->uri
.path
)) {
2138 con
->http_status
= 423;
2139 return HANDLER_FINISHED
;
2142 /* destination exists */
2143 if (0 == (r
= stat(p
->physical
.path
->ptr
, &st
))) {
2144 if (S_ISDIR(st
.st_mode
)) {
2146 * append basename to physical path */
2149 if (NULL
!= (sep
= strrchr(con
->physical
.path
->ptr
, '/'))) {
2150 buffer_append_string(p
->physical
.path
, sep
);
2151 r
= stat(p
->physical
.path
->ptr
, &st
);
2157 con
->http_status
= destdir
? 204 : 201; /* we will create a new one */
2158 con
->file_finished
= 1;
2162 con
->http_status
= 409;
2163 return HANDLER_FINISHED
;
2165 } else if (overwrite
== 0) {
2166 /* destination exists, but overwrite is not set */
2167 con
->http_status
= 412;
2168 return HANDLER_FINISHED
;
2170 con
->http_status
= 204; /* resource already existed */
2173 if (con
->request
.http_method
== HTTP_METHOD_MOVE
) {
2176 if (0 == rename(con
->physical
.path
->ptr
, p
->physical
.path
->ptr
)) {
2177 #ifdef USE_PROPPATCH
2180 stmt
= p
->conf
.stmt_move_uri
;
2183 sqlite3_reset(stmt
);
2185 /* bind the values to the insert */
2186 sqlite3_bind_text(stmt
, 1,
2187 CONST_BUF_LEN(p
->uri
.path
),
2190 sqlite3_bind_text(stmt
, 2,
2191 CONST_BUF_LEN(con
->uri
.path
),
2194 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
2195 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "sql-move failed:", sqlite3_errmsg(p
->conf
.sql
));
2199 return HANDLER_FINISHED
;
2202 /* rename failed, fall back to COPY + DELETE */
2205 if (0 != (r
= webdav_copy_file(srv
, con
, p
, &(con
->physical
), &(p
->physical
), overwrite
))) {
2206 con
->http_status
= r
;
2208 return HANDLER_FINISHED
;
2211 if (con
->request
.http_method
== HTTP_METHOD_MOVE
) {
2213 webdav_delete_file(srv
, con
, p
, &(con
->physical
), b
);
2218 return HANDLER_FINISHED
;
2220 case HTTP_METHOD_PROPPATCH
:
2221 if (p
->conf
.is_readonly
) {
2222 con
->http_status
= 403;
2223 return HANDLER_FINISHED
;
2226 if (!webdav_has_lock(srv
, con
, p
, con
->uri
.path
)) {
2227 con
->http_status
= 423;
2228 return HANDLER_FINISHED
;
2231 /* check if destination exists */
2232 if (-1 == stat(con
->physical
.path
->ptr
, &st
)) {
2235 con
->http_status
= 404;
2240 if (S_ISDIR(st
.st_mode
) && con
->physical
.path
->ptr
[buffer_string_length(con
->physical
.path
)-1] != '/') {
2241 http_response_redirect_to_directory(srv
, con
);
2242 return HANDLER_FINISHED
;
2245 #ifdef USE_PROPPATCH
2246 if (con
->request
.content_length
) {
2249 if (con
->state
== CON_STATE_READ_POST
) {
2250 handler_t r
= connection_handle_read_post_state(srv
, con
);
2251 if (r
!= HANDLER_GO_ON
) return r
;
2254 if (1 == webdav_parse_chunkqueue(srv
, con
, p
, con
->request_content_queue
, &xml
)) {
2255 xmlNode
*rootnode
= xmlDocGetRootElement(xml
);
2257 if (0 == xmlStrcmp(rootnode
->name
, BAD_CAST
"propertyupdate")) {
2260 int empty_ns
= 0; /* send 400 on a empty namespace attribute */
2262 /* start response */
2264 if (SQLITE_OK
!= sqlite3_exec(p
->conf
.sql
, "BEGIN TRANSACTION", NULL
, NULL
, &err
)) {
2265 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "can't open transaction:", err
);
2268 goto propmatch_cleanup
;
2271 /* a UPDATE request, we know 'set' and 'remove' */
2272 for (cmd
= rootnode
->children
; cmd
; cmd
= cmd
->next
) {
2274 /* either set or remove */
2276 if ((0 == xmlStrcmp(cmd
->name
, BAD_CAST
"set")) ||
2277 (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"remove"))) {
2281 stmt
= (0 == xmlStrcmp(cmd
->name
, BAD_CAST
"remove")) ?
2282 p
->conf
.stmt_delete_prop
: p
->conf
.stmt_update_prop
;
2284 for (props
= cmd
->children
; props
; props
= props
->next
) {
2285 if (0 == xmlStrcmp(props
->name
, BAD_CAST
"prop")) {
2287 char *propval
= NULL
;
2290 prop
= props
->children
;
2293 (0 == xmlStrcmp(prop
->ns
->href
, BAD_CAST
"")) &&
2294 (0 != xmlStrcmp(prop
->ns
->prefix
, BAD_CAST
""))) {
2295 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2296 "no name space for:",
2303 sqlite3_reset(stmt
);
2305 /* bind the values to the insert */
2307 sqlite3_bind_text(stmt
, 1,
2308 CONST_BUF_LEN(con
->uri
.path
),
2310 sqlite3_bind_text(stmt
, 2,
2312 strlen((char *)prop
->name
),
2315 sqlite3_bind_text(stmt
, 3,
2316 (char *)prop
->ns
->href
,
2317 strlen((char *)prop
->ns
->href
),
2320 sqlite3_bind_text(stmt
, 3,
2325 if (stmt
== p
->conf
.stmt_update_prop
) {
2326 propval
= prop
->children
2327 ? (char *)xmlNodeListGetString(xml
, prop
->children
, 0)
2330 sqlite3_bind_text(stmt
, 4,
2331 propval
? propval
: "",
2332 propval
? strlen(propval
) : 0,
2336 if (SQLITE_DONE
!= (r
= sqlite3_step(stmt
))) {
2337 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2338 "sql-set failed:", sqlite3_errmsg(p
->conf
.sql
));
2341 if (propval
) xmlFree(propval
);
2344 if (empty_ns
) break;
2349 if (SQLITE_OK
!= sqlite3_exec(p
->conf
.sql
, "ROLLBACK", NULL
, NULL
, &err
)) {
2350 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "can't rollback transaction:", err
);
2353 goto propmatch_cleanup
;
2356 con
->http_status
= 400;
2358 if (SQLITE_OK
!= sqlite3_exec(p
->conf
.sql
, "COMMIT", NULL
, NULL
, &err
)) {
2359 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "can't commit transaction:", err
);
2362 goto propmatch_cleanup
;
2364 con
->http_status
= 200;
2366 con
->file_finished
= 1;
2368 return HANDLER_FINISHED
;
2375 con
->http_status
= 400;
2376 return HANDLER_FINISHED
;
2380 con
->http_status
= 501;
2381 return HANDLER_FINISHED
;
2382 case HTTP_METHOD_LOCK
:
2384 * a mac wants to write
2386 * LOCK /dav/expire.txt HTTP/1.1\r\n
2387 * User-Agent: WebDAVFS/1.3 (01308000) Darwin/8.1.0 (Power Macintosh)\r\n
2390 * Timeout: Second-600\r\n
2391 * Content-Type: text/xml; charset=\"utf-8\"\r\n
2392 * Content-Length: 229\r\n
2393 * Connection: keep-alive\r\n
2394 * Host: 192.168.178.23:1025\r\n
2396 * <?xml version=\"1.0\" encoding=\"utf-8\"?>\n
2397 * <D:lockinfo xmlns:D=\"DAV:\">\n
2398 * <D:lockscope><D:exclusive/></D:lockscope>\n
2399 * <D:locktype><D:write/></D:locktype>\n
2401 * <D:href>http://www.apple.com/webdav_fs/</D:href>\n
2406 if (depth
!= 0 && depth
!= -1) {
2407 con
->http_status
= 400;
2409 return HANDLER_FINISHED
;
2413 if (con
->request
.content_length
) {
2415 buffer
*hdr_if
= NULL
;
2418 if (con
->state
== CON_STATE_READ_POST
) {
2419 handler_t r
= connection_handle_read_post_state(srv
, con
);
2420 if (r
!= HANDLER_GO_ON
) return r
;
2423 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "If"))) {
2427 if (0 != stat(con
->physical
.path
->ptr
, &st
)) {
2428 if (errno
== ENOENT
) {
2429 int fd
= open(con
->physical
.path
->ptr
, O_WRONLY
|O_CREAT
|O_APPEND
|O_BINARY
|FIFO_NONBLOCK
, WEBDAV_FILE_MODE
);
2434 log_error_write(srv
, __FILE__
, __LINE__
, "sBss",
2435 "create file", con
->physical
.path
, ":", strerror(errno
));
2436 con
->http_status
= 403; /* Forbidden */
2438 return HANDLER_FINISHED
;
2441 } else if (hdr_if
== NULL
&& depth
== -1) {
2442 /* we don't support Depth: Infinity on directories */
2443 if (S_ISDIR(st
.st_mode
)) {
2444 con
->http_status
= 409; /* Conflict */
2446 return HANDLER_FINISHED
;
2450 if (1 == webdav_parse_chunkqueue(srv
, con
, p
, con
->request_content_queue
, &xml
)) {
2451 xmlNode
*rootnode
= xmlDocGetRootElement(xml
);
2453 force_assert(rootnode
);
2455 if (0 == xmlStrcmp(rootnode
->name
, BAD_CAST
"lockinfo")) {
2457 const xmlChar
*lockscope
= NULL
, *locktype
= NULL
; /* TODO: compiler says unused: *owner = NULL; */
2459 for (lockinfo
= rootnode
->children
; lockinfo
; lockinfo
= lockinfo
->next
) {
2460 if (0 == xmlStrcmp(lockinfo
->name
, BAD_CAST
"lockscope")) {
2462 for (value
= lockinfo
->children
; value
; value
= value
->next
) {
2463 if ((0 == xmlStrcmp(value
->name
, BAD_CAST
"exclusive")) ||
2464 (0 == xmlStrcmp(value
->name
, BAD_CAST
"shared"))) {
2465 lockscope
= value
->name
;
2467 con
->http_status
= 400;
2470 return HANDLER_FINISHED
;
2473 } else if (0 == xmlStrcmp(lockinfo
->name
, BAD_CAST
"locktype")) {
2475 for (value
= lockinfo
->children
; value
; value
= value
->next
) {
2476 if ((0 == xmlStrcmp(value
->name
, BAD_CAST
"write"))) {
2477 locktype
= value
->name
;
2479 con
->http_status
= 400;
2482 return HANDLER_FINISHED
;
2486 } else if (0 == xmlStrcmp(lockinfo
->name
, BAD_CAST
"owner")) {
2490 if (lockscope
&& locktype
) {
2491 sqlite3_stmt
*stmt
= p
->conf
.stmt_read_lock_by_uri
;
2493 /* is this resourse already locked ? */
2495 /* SELECT locktoken, resource, lockscope, locktype, owner, depth, timeout
2497 * WHERE resource = ? */
2501 sqlite3_reset(stmt
);
2503 sqlite3_bind_text(stmt
, 1,
2504 CONST_BUF_LEN(p
->uri
.path
),
2508 while (SQLITE_ROW
== sqlite3_step(stmt
)) {
2510 * 1. is it compatible ?
2512 char *sql_lockscope
= (char *)sqlite3_column_text(stmt
, 2);
2514 if (strcmp(sql_lockscope
, "exclusive")) {
2515 con
->http_status
= 423;
2516 } else if (0 == xmlStrcmp(lockscope
, BAD_CAST
"exclusive")) {
2517 /* resourse is locked with a shared lock
2518 * client wants exclusive */
2519 con
->http_status
= 423;
2522 if (con
->http_status
== 423) {
2524 return HANDLER_FINISHED
;
2528 stmt
= p
->conf
.stmt_create_lock
;
2530 /* create a lock-token */
2532 char uuid
[37] /* 36 + \0 */;
2535 uuid_unparse(id
, uuid
);
2537 buffer_copy_string_len(p
->tmp_buf
, CONST_STR_LEN("opaquelocktoken:"));
2538 buffer_append_string(p
->tmp_buf
, uuid
);
2540 /* "CREATE TABLE locks ("
2541 * " locktoken TEXT NOT NULL,"
2542 * " resource TEXT NOT NULL,"
2543 * " lockscope TEXT NOT NULL,"
2544 * " locktype TEXT NOT NULL,"
2545 * " owner TEXT NOT NULL,"
2546 * " depth INT NOT NULL,"
2549 sqlite3_reset(stmt
);
2551 sqlite3_bind_text(stmt
, 1,
2552 CONST_BUF_LEN(p
->tmp_buf
),
2555 sqlite3_bind_text(stmt
, 2,
2556 CONST_BUF_LEN(con
->uri
.path
),
2559 sqlite3_bind_text(stmt
, 3,
2560 (const char *)lockscope
,
2561 xmlStrlen(lockscope
),
2564 sqlite3_bind_text(stmt
, 4,
2565 (const char *)locktype
,
2566 xmlStrlen(locktype
),
2570 sqlite3_bind_text(stmt
, 5,
2576 sqlite3_bind_int(stmt
, 6,
2580 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
2581 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2582 "create lock:", sqlite3_errmsg(p
->conf
.sql
));
2585 /* looks like we survived */
2586 webdav_lockdiscovery(srv
, con
, p
->tmp_buf
, (const char *)lockscope
, (const char *)locktype
, depth
);
2588 con
->http_status
= created
? 201 : 200;
2589 con
->file_finished
= 1;
2595 return HANDLER_FINISHED
;
2597 con
->http_status
= 400;
2598 return HANDLER_FINISHED
;
2602 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "If"))) {
2603 buffer
*locktoken
= ds
->value
;
2604 sqlite3_stmt
*stmt
= p
->conf
.stmt_refresh_lock
;
2606 /* remove the < > around the token */
2607 if (buffer_string_length(locktoken
) < 5) {
2608 con
->http_status
= 400;
2610 return HANDLER_FINISHED
;
2613 buffer_copy_string_len(p
->tmp_buf
, locktoken
->ptr
+ 2, buffer_string_length(locktoken
) - 4);
2615 sqlite3_reset(stmt
);
2617 sqlite3_bind_text(stmt
, 1,
2618 CONST_BUF_LEN(p
->tmp_buf
),
2621 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
2622 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2623 "refresh lock:", sqlite3_errmsg(p
->conf
.sql
));
2626 webdav_lockdiscovery(srv
, con
, p
->tmp_buf
, "exclusive", "write", 0);
2628 con
->http_status
= 200;
2629 con
->file_finished
= 1;
2630 return HANDLER_FINISHED
;
2632 /* we need a lock-token to refresh */
2633 con
->http_status
= 400;
2635 return HANDLER_FINISHED
;
2640 con
->http_status
= 501;
2641 return HANDLER_FINISHED
;
2643 case HTTP_METHOD_UNLOCK
:
2645 if (NULL
!= (ds
= (data_string
*)array_get_element(con
->request
.headers
, "Lock-Token"))) {
2646 buffer
*locktoken
= ds
->value
;
2647 sqlite3_stmt
*stmt
= p
->conf
.stmt_remove_lock
;
2649 /* remove the < > around the token */
2650 if (buffer_string_length(locktoken
) < 3) {
2651 con
->http_status
= 400;
2653 return HANDLER_FINISHED
;
2659 * if the resourse is locked:
2661 * - by someone else: 401
2662 * if the resource is not locked:
2666 buffer_copy_string_len(p
->tmp_buf
, locktoken
->ptr
+ 1, buffer_string_length(locktoken
) - 2);
2668 sqlite3_reset(stmt
);
2670 sqlite3_bind_text(stmt
, 1,
2671 CONST_BUF_LEN(p
->tmp_buf
),
2674 sqlite3_bind_text(stmt
, 2,
2675 CONST_BUF_LEN(con
->uri
.path
),
2678 if (SQLITE_DONE
!= sqlite3_step(stmt
)) {
2679 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2680 "remove lock:", sqlite3_errmsg(p
->conf
.sql
));
2683 if (0 == sqlite3_changes(p
->conf
.sql
)) {
2684 con
->http_status
= 401;
2686 con
->http_status
= 204;
2688 return HANDLER_FINISHED
;
2690 /* we need a lock-token to unlock */
2691 con
->http_status
= 400;
2693 return HANDLER_FINISHED
;
2697 con
->http_status
= 501;
2698 return HANDLER_FINISHED
;
2705 return HANDLER_GO_ON
;
2709 SUBREQUEST_FUNC(mod_webdav_subrequest_handler
) {
2711 plugin_data
*p
= p_d
;
2712 if (con
->mode
!= p
->id
) return HANDLER_GO_ON
;
2714 r
= mod_webdav_subrequest_handler_huge(srv
, con
, p_d
);
2715 if (con
->http_status
>= 400) con
->mode
= DIRECT
;
2720 PHYSICALPATH_FUNC(mod_webdav_physical_handler
) {
2721 plugin_data
*p
= p_d
;
2722 if (!p
->conf
.enabled
) return HANDLER_GO_ON
;
2724 /* physical path is setup */
2725 if (buffer_is_empty(con
->physical
.path
)) return HANDLER_GO_ON
;
2729 switch (con
->request
.http_method
) {
2730 case HTTP_METHOD_PROPFIND
:
2731 case HTTP_METHOD_PROPPATCH
:
2732 case HTTP_METHOD_PUT
:
2733 case HTTP_METHOD_COPY
:
2734 case HTTP_METHOD_MOVE
:
2735 case HTTP_METHOD_MKCOL
:
2736 case HTTP_METHOD_DELETE
:
2737 case HTTP_METHOD_LOCK
:
2738 case HTTP_METHOD_UNLOCK
:
2739 con
->conf
.stream_request_body
= 0;
2746 return HANDLER_GO_ON
;
2750 /* this function is called at dlopen() time and inits the callbacks */
2752 int mod_webdav_plugin_init(plugin
*p
);
2753 int mod_webdav_plugin_init(plugin
*p
) {
2754 p
->version
= LIGHTTPD_VERSION_ID
;
2755 p
->name
= buffer_init_string("webdav");
2757 p
->init
= mod_webdav_init
;
2758 p
->handle_uri_clean
= mod_webdav_uri_handler
;
2759 p
->handle_physical
= mod_webdav_physical_handler
;
2760 p
->handle_subrequest
= mod_webdav_subrequest_handler
;
2761 p
->set_defaults
= mod_webdav_set_defaults
;
2762 p
->cleanup
= mod_webdav_free
;