lighttpd 1.4.x https://www.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1576 lines
41 KiB

#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <sqlite3.h>
#include "base.h"
#include "log.h"
#include "buffer.h"
#include "response.h"
#include "plugin.h"
#include "config.h"
#include "stream.h"
#include "stat_cache.h"
/**
* this is a webdav for a lighttpd plugin
*
* at least a very basic one.
* - for now it is read-only and we only support PROPFIND
*
*/
/* plugin config for all request/connections */
typedef struct {
unsigned short enabled;
unsigned short is_readonly;
buffer *sqlite_db_name;
sqlite3 *sql;
sqlite3_stmt *stmt_update_prop;
sqlite3_stmt *stmt_delete_prop;
sqlite3_stmt *stmt_select_prop;
sqlite3_stmt *stmt_select_propnames;
sqlite3_stmt *stmt_delete_uri;
sqlite3_stmt *stmt_move_uri;
sqlite3_stmt *stmt_copy_uri;
} plugin_config;
typedef struct {
PLUGIN_DATA;
buffer *tmp_buf;
request_uri uri;
physical physical;
plugin_config **config_storage;
plugin_config conf;
} plugin_data;
/* init the plugin data */
INIT_FUNC(mod_webdav_init) {
plugin_data *p;
p = calloc(1, sizeof(*p));
p->tmp_buf = buffer_init();
p->uri.scheme = buffer_init();
p->uri.path_raw = buffer_init();
p->uri.path = buffer_init();
p->uri.authority = buffer_init();
p->physical.path = buffer_init();
p->physical.rel_path = buffer_init();
p->physical.doc_root = buffer_init();
p->physical.basedir = buffer_init();
return p;
}
/* detroy the plugin data */
FREE_FUNC(mod_webdav_free) {
plugin_data *p = p_d;
UNUSED(srv);
if (!p) return HANDLER_GO_ON;
if (p->config_storage) {
size_t i;
for (i = 0; i < srv->config_context->used; i++) {
plugin_config *s = p->config_storage[i];
if (!s) continue;
if (s->sql) {
sqlite3_finalize(s->stmt_delete_prop);
sqlite3_finalize(s->stmt_delete_uri);
sqlite3_finalize(s->stmt_update_prop);
sqlite3_finalize(s->stmt_select_prop);
sqlite3_finalize(s->stmt_select_propnames);
sqlite3_close(s->sql);
}
free(s);
}
free(p->config_storage);
}
buffer_free(p->tmp_buf);
free(p);
return HANDLER_GO_ON;
}
/* handle plugin config and check values */
SETDEFAULTS_FUNC(mod_webdav_set_defaults) {
plugin_data *p = p_d;
size_t i = 0;
config_values_t cv[] = {
{ "webdav.activate", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
{ "webdav.is-readonly", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
{ "webdav.sqlite-db-name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
};
if (!p) return HANDLER_ERROR;
p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
for (i = 0; i < srv->config_context->used; i++) {
plugin_config *s;
s = calloc(1, sizeof(plugin_config));
s->sqlite_db_name = buffer_init();
cv[0].destination = &(s->enabled);
cv[1].destination = &(s->is_readonly);
cv[2].destination = s->sqlite_db_name;
p->config_storage[i] = s;
if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) {
return HANDLER_ERROR;
}
if (!buffer_is_empty(s->sqlite_db_name)) {
const char *next_stmt;
char *err;
if (SQLITE_OK != sqlite3_open(s->sqlite_db_name->ptr, &(s->sql))) {
log_error_write(srv, __FILE__, __LINE__, "s", "sqlite3_open failed");
return HANDLER_ERROR;
}
if (SQLITE_OK != sqlite3_prepare(s->sql,
CONST_STR_LEN("SELECT value FROM properties WHERE resource = ? AND prop = ? AND ns = ?"),
&(s->stmt_select_prop), &next_stmt)) {
/* prepare failed */
log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed:", sqlite3_errmsg(s->sql));
return HANDLER_ERROR;
}
if (SQLITE_OK != sqlite3_prepare(s->sql,
CONST_STR_LEN("SELECT ns, prop FROM properties WHERE resource = ?"),
&(s->stmt_select_propnames), &next_stmt)) {
/* prepare failed */
log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed:", sqlite3_errmsg(s->sql));
return HANDLER_ERROR;
}
if (SQLITE_OK != sqlite3_exec(s->sql,
"CREATE TABLE properties ("
" resource TEXT NOT NULL,"
" prop TEXT NOT NULL,"
" ns TEXT NOT NULL,"
" value TEXT NOT NULL,"
" PRIMARY KEY(resource, prop, ns))",
NULL, NULL, &err)) {
if (0 != strcmp(err, "table properties already exists")) {
log_error_write(srv, __FILE__, __LINE__, "ss", "can't open transaction:", err);
sqlite3_free(err);
return HANDLER_ERROR;
}
}
if (SQLITE_OK != sqlite3_prepare(s->sql,
CONST_STR_LEN("REPLACE INTO properties (resource, prop, ns, value) VALUES (?, ?, ?, ?)"),
&(s->stmt_update_prop), &next_stmt)) {
/* prepare failed */
log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed:", sqlite3_errmsg(s->sql));
return HANDLER_ERROR;
}
if (SQLITE_OK != sqlite3_prepare(s->sql,
CONST_STR_LEN("DELETE FROM properties WHERE resource = ? AND prop = ? AND ns = ?"),
&(s->stmt_delete_prop), &next_stmt)) {
/* prepare failed */
log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
return HANDLER_ERROR;
}
if (SQLITE_OK != sqlite3_prepare(s->sql,
CONST_STR_LEN("DELETE FROM properties WHERE resource = ?"),
&(s->stmt_delete_uri), &next_stmt)) {
/* prepare failed */
log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
return HANDLER_ERROR;
}
if (SQLITE_OK != sqlite3_prepare(s->sql,
CONST_STR_LEN("INSERT INTO properties SELECT ?, prop, ns, value FROM properties WHERE resource = ?"),
&(s->stmt_copy_uri), &next_stmt)) {
/* prepare failed */
log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
return HANDLER_ERROR;
}
if (SQLITE_OK != sqlite3_prepare(s->sql,
CONST_STR_LEN("UPDATE properties SET resource = ? WHERE resource = ?"),
&(s->stmt_move_uri), &next_stmt)) {
/* prepare failed */
log_error_write(srv, __FILE__, __LINE__, "ss", "sqlite3_prepare failed", sqlite3_errmsg(s->sql));
return HANDLER_ERROR;
}
}
}
return HANDLER_GO_ON;
}
#define PATCH(x) \
p->conf.x = s->x;
static int mod_webdav_patch_connection(server *srv, connection *con, plugin_data *p) {
size_t i, j;
plugin_config *s = p->config_storage[0];
PATCH(enabled);
PATCH(is_readonly);
PATCH(sql);
PATCH(stmt_update_prop);
PATCH(stmt_delete_prop);
PATCH(stmt_select_prop);
PATCH(stmt_select_propnames);
PATCH(stmt_delete_uri);
PATCH(stmt_move_uri);
PATCH(stmt_copy_uri);
/* skip the first, the global context */
for (i = 1; i < srv->config_context->used; i++) {
data_config *dc = (data_config *)srv->config_context->data[i];
s = p->config_storage[i];
/* condition didn't match */
if (!config_check_cond(srv, con, dc)) continue;
/* merge config */
for (j = 0; j < dc->value->used; j++) {
data_unset *du = dc->value->data[j];
if (buffer_is_equal_string(du->key, CONST_STR_LEN("webdav.activate"))) {
PATCH(enabled);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("webdav.is-readonly"))) {
PATCH(is_readonly);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("webdav.sqlite-db-name"))) {
PATCH(sql);
PATCH(stmt_update_prop);
PATCH(stmt_delete_prop);
PATCH(stmt_select_prop);
PATCH(stmt_select_propnames);
PATCH(stmt_delete_uri);
PATCH(stmt_move_uri);
PATCH(stmt_copy_uri);
}
}
}
return 0;
}
#undef PATCH
URIHANDLER_FUNC(mod_webdav_uri_handler) {
plugin_data *p = p_d;
UNUSED(srv);
if (con->uri.path->used == 0) return HANDLER_GO_ON;
mod_webdav_patch_connection(srv, con, p);
if (!p->conf.enabled) return HANDLER_GO_ON;
switch (con->request.http_method) {
case HTTP_METHOD_OPTIONS:
/* we fake a little bit but it makes MS W2k happy and it let's us mount the volume */
response_header_overwrite(srv, con, CONST_STR_LEN("DAV"), CONST_STR_LEN("1,2"));
response_header_overwrite(srv, con, CONST_STR_LEN("MS-Author-Via"), CONST_STR_LEN("DAV"));
if (p->conf.is_readonly) {
response_header_insert(srv, con, CONST_STR_LEN("Allow"), CONST_STR_LEN("PROPFIND"));
} else {
response_header_insert(srv, con, CONST_STR_LEN("Allow"), CONST_STR_LEN("PROPFIND, DELETE, MKCOL, PUT, MOVE, COPY"));
}
break;
default:
break;
}
/* not found */
return HANDLER_GO_ON;
}
static int webdav_gen_prop_tag(server *srv, connection *con,
xmlChar *prop_name,
xmlChar *prop_ns,
const char *value,
buffer *b) {
if (value) {
buffer_append_string(b,"<");
buffer_append_string(b, prop_name);
buffer_append_string(b, " xmlns=\"");
buffer_append_string(b, prop_ns);
buffer_append_string(b, "\">");
buffer_append_string(b, value);
buffer_append_string(b,"</");
buffer_append_string(b, prop_name);
buffer_append_string(b, ">");
} else {
buffer_append_string(b,"<");
buffer_append_string(b, prop_name);
buffer_append_string(b, " xmlns=\"");
buffer_append_string(b, prop_ns);
buffer_append_string(b, "\"/>");
}
return 0;
}
static int webdav_gen_response_status_tag(server *srv, connection *con, physical *dst, int status, buffer *b) {
buffer_append_string(b,"<D:response xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\">\n");
buffer_append_string(b,"<D:href>\n");
buffer_append_string_buffer(b, dst->rel_path);
buffer_append_string(b,"</D:href>\n");
buffer_append_string(b,"<D:status>\n");
if (con->request.http_version == HTTP_VERSION_1_1) {
BUFFER_COPY_STRING_CONST(b, "HTTP/1.1 ");
} else {
BUFFER_COPY_STRING_CONST(b, "HTTP/1.0 ");
}
buffer_append_long(b, status);
BUFFER_APPEND_STRING_CONST(b, " ");
buffer_append_string(b, get_http_status_name(status));
buffer_append_string(b,"</D:status>\n");
buffer_append_string(b,"</D:response>\n");
return 0;
}
static int webdav_delete_file(server *srv, connection *con, plugin_data *p, physical *dst, buffer *b) {
int status = 0;
/* try to unlink it */
if (-1 == unlink(dst->path->ptr)) {
switch(errno) {
case EACCES:
case EPERM:
/* 403 */
status = 403;
break;
default:
status = 501;
break;
}
webdav_gen_response_status_tag(srv, con, dst, status, b);
} else {
sqlite3_stmt *stmt = p->conf.stmt_delete_uri;
sqlite3_reset(stmt);
/* bind the values to the insert */
sqlite3_bind_text(stmt, 1,
dst->rel_path->ptr,
dst->rel_path->used - 1,
SQLITE_TRANSIENT);
if (SQLITE_DONE != sqlite3_step(stmt)) {
/* */
WP();
}
}
return (status != 0);
}
static int webdav_delete_dir(server *srv, connection *con, plugin_data *p, physical *dst, buffer *b) {
DIR *dir;
int have_multi_status = 0;
physical d;
d.path = buffer_init();
d.rel_path = buffer_init();
if (NULL != (dir = opendir(dst->path->ptr))) {
struct dirent *de;
while(NULL != (de = readdir(dir))) {
struct stat st;
int status = 0;
if ((de->d_name[0] == '.' && de->d_name[1] == '\0') ||
(de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')) {
continue;
/* ignore the parent dir */
}
buffer_copy_string_buffer(d.path, dst->path);
BUFFER_APPEND_SLASH(d.path);
buffer_append_string(d.path, de->d_name);
buffer_copy_string_buffer(d.rel_path, dst->rel_path);
BUFFER_APPEND_SLASH(d.rel_path);
buffer_append_string(d.rel_path, de->d_name);
/* stat and unlink afterwards */
if (-1 == stat(d.path->ptr, &st)) {
/* don't about it yet, rmdir will fail too */
} else if (S_ISDIR(st.st_mode)) {
have_multi_status = webdav_delete_dir(srv, con, p, &d, b);
/* try to unlink it */
if (-1 == rmdir(d.path->ptr)) {
switch(errno) {
case EACCES:
case EPERM:
/* 403 */
status = 403;
break;
default:
status = 501;
break;
}
have_multi_status = 1;
webdav_gen_response_status_tag(srv, con, &d, status, b);
} else {
sqlite3_stmt *stmt = p->conf.stmt_delete_uri;
status = 0;
sqlite3_reset(stmt);
/* bind the values to the insert */
sqlite3_bind_text(stmt, 1,
d.rel_path->ptr,
d.rel_path->used - 1,
SQLITE_TRANSIENT);
if (SQLITE_DONE != sqlite3_step(stmt)) {
/* */
WP();
}
}
} else {
have_multi_status = webdav_delete_file(srv, con, p, &d, b);
}
}
closedir(dir);
buffer_free(d.path);
buffer_free(d.rel_path);
}
return have_multi_status;
}
static int webdav_copy_file(server *srv, connection *con, plugin_data *p, physical *src, physical *dst, int overwrite) {
stream s;
int status = 0, ofd;
if (stream_open(&s, src->path)) {
return 403;
}
if (-1 == (ofd = open(dst->path->ptr, O_WRONLY|O_TRUNC|O_CREAT|(overwrite ? 0 : O_EXCL), 0600))) {
/* opening the destination failed for some reason */
switch(errno) {
case EEXIST:
status = 412;
break;
case EISDIR:
status = 409;
break;
case ENOENT:
/* at least one part in the middle wasn't existing */
status = 409;
break;
default:
status = 403;
break;
}
stream_close(&s);
return status;
}
if (-1 == write(ofd, s.start, s.size)) {
switch(errno) {
case ENOSPC:
status = 507;
break;
default:
status = 403;
break;
}
}
stream_close(&s);
close(ofd);
if (0 == status) {
/* copy worked fine, copy connected properties */
sqlite3_stmt *stmt = p->conf.stmt_copy_uri;
sqlite3_reset(stmt);
/* bind the values to the insert */
sqlite3_bind_text(stmt, 1,
dst->rel_path->ptr,
dst->rel_path->used - 1,
SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2,
src->rel_path->ptr,
src->rel_path->used - 1,
SQLITE_TRANSIENT);
if (SQLITE_DONE != sqlite3_step(stmt)) {
/* */
WP();
}
}
return status;
}
static int webdav_copy_dir(server *srv, connection *con, plugin_data *p, physical *src, physical *dst, int overwrite) {
DIR *srcdir;
int status = 0;
if (NULL != (srcdir = opendir(src->path->ptr))) {
struct dirent *de;
physical s, d;
s.path = buffer_init();
s.rel_path = buffer_init();
d.path = buffer_init();
d.rel_path = buffer_init();
while (NULL != (de = readdir(srcdir))) {
struct stat st;
if ((de->d_name[0] == '.' && de->d_name[1] == '\0') ||
(de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')) {
continue;
}
buffer_copy_string_buffer(s.path, src->path);
BUFFER_APPEND_SLASH(s.path);
buffer_append_string(s.path, de->d_name);
buffer_copy_string_buffer(d.path, dst->path);
BUFFER_APPEND_SLASH(d.path);
buffer_append_string(d.path, de->d_name);
buffer_copy_string_buffer(s.rel_path, src->rel_path);
BUFFER_APPEND_SLASH(s.rel_path);
buffer_append_string(s.rel_path, de->d_name);
buffer_copy_string_buffer(d.rel_path, dst->rel_path);
BUFFER_APPEND_SLASH(d.rel_path);
buffer_append_string(d.rel_path, de->d_name);
if (-1 == stat(s.path->ptr, &st)) {
/* why ? */
} else if (S_ISDIR(st.st_mode)) {
/* a directory */
if (-1 == mkdir(d.path->ptr, 0700) &&
errno != EEXIST) {
/* WTH ? */
} else {
sqlite3_stmt *stmt = p->conf.stmt_copy_uri;
if (0 != (status = webdav_copy_dir(srv, con, p, &s, &d, overwrite))) {
break;
}
/* directory is copied, copy the properties too */
sqlite3_reset(stmt);
/* bind the values to the insert */
sqlite3_bind_text(stmt, 1,
dst->rel_path->ptr,
dst->rel_path->used - 1,
SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2,
src->rel_path->ptr,
src->rel_path->used - 1,
SQLITE_TRANSIENT);
if (SQLITE_DONE != sqlite3_step(stmt)) {
/* */
WP();
}
}
} else if (S_ISREG(st.st_mode)) {
/* a plain file */
if (0 != (status = webdav_copy_file(srv, con, p, &s, &d, overwrite))) {
break;
}
}
}
buffer_free(s.path);
buffer_free(s.rel_path);
buffer_free(d.path);
buffer_free(d.rel_path);
closedir(srcdir);
}
return status;
}
static int webdav_get_live_property(server *srv, connection *con, plugin_data *p, physical *dst, xmlChar *prop_name, buffer *b) {
stat_cache_entry *sce = NULL;
int found = 0;
if (HANDLER_ERROR != (stat_cache_get_entry(srv, con, dst->path, &sce))) {
char ctime_buf[] = "2005-08-18T07:27:16Z";
char mtime_buf[] = "Thu, 18 Aug 2005 07:27:16 GMT";
size_t k;
if (0 == xmlStrcmp(prop_name, BAD_CAST "resourcetype")) {
if (S_ISDIR(sce->st.st_mode)) {
buffer_append_string(b, "<D:resourcetype><D:collection/></D:resourcetype>");
found = 1;
}
} else if (0 == xmlStrcmp(prop_name, BAD_CAST "getcontenttype")) {
if (S_ISDIR(sce->st.st_mode)) {
buffer_append_string(b, "<D:getcontenttype>httpd/unix-directory</D:getcontenttype>");
found = 1;
} else if(S_ISREG(sce->st.st_mode)) {
for (k = 0; k < con->conf.mimetypes->used; k++) {
data_string *ds = (data_string *)con->conf.mimetypes->data[k];
if (ds->key->used == 0) continue;
if (buffer_is_equal_right_len(p->tmp_buf, ds->key, ds->key->used - 1)) {
buffer_append_string(b,"<D:getcontenttype>");
buffer_append_string_buffer(b, ds->value);
buffer_append_string(b, "</D:getcontenttype>");
found = 1;
break;
}
}
}
} else if (0 == xmlStrcmp(prop_name, BAD_CAST "creationdate")) {
buffer_append_string(b, "<D:creationdate ns0:dt=\"dateTime.tz\">");
strftime(ctime_buf, sizeof(ctime_buf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&(sce->st.st_ctime)));
buffer_append_string(b, ctime_buf);
buffer_append_string(b, "</D:creationdate>");
found = 1;
} else if (0 == xmlStrcmp(prop_name, BAD_CAST "getlastmodified")) {
buffer_append_string(b,"<D:getlastmodified ns0:dt=\"dateTime.rfc1123\">");
strftime(mtime_buf, sizeof(mtime_buf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&(sce->st.st_mtime)));
buffer_append_string(b, mtime_buf);
buffer_append_string(b, "</D:getlastmodified>");
found = 1;
} else if (0 == xmlStrcmp(prop_name, BAD_CAST "getcontentlength")) {
buffer_append_string(b,"<D:getcontentlength>");
buffer_append_off_t(b, sce->st.st_size);
buffer_append_string(b, "</D:getcontentlength>");
found = 1;
} else if (0 == xmlStrcmp(prop_name, BAD_CAST "getcontentlanguage")) {
buffer_append_string(b,"<D:getcontentlanguage>");
buffer_append_string(b, "en");
buffer_append_string(b, "</D:getcontentlanguage>");
found = 1;
}
}
return found ? 0 : -1;
}
static int webdav_get_property(server *srv, connection *con, plugin_data *p, physical *dst, xmlChar *prop_name, xmlChar *prop_ns, buffer *b) {
if (0 == xmlStrcmp(prop_ns, BAD_CAST "DAV:")) {
/* a local 'live' property */
return webdav_get_live_property(srv, con, p, dst, prop_name, b);
} else {
int found = 0;
/* perhaps it is in sqlite3 */
sqlite3_reset(p->conf.stmt_select_prop);
/* bind the values to the insert */
sqlite3_bind_text(p->conf.stmt_select_prop, 1,
dst->rel_path->ptr,
dst->rel_path->used - 1,
SQLITE_TRANSIENT);
sqlite3_bind_text(p->conf.stmt_select_prop, 2,
prop_name,
strlen(prop_name),
SQLITE_TRANSIENT);
sqlite3_bind_text(p->conf.stmt_select_prop, 3,
prop_ns,
strlen(prop_ns),
SQLITE_TRANSIENT);
/* it is the PK */
while (SQLITE_ROW == sqlite3_step(p->conf.stmt_select_prop)) {
/* there is a row for us, we only expect a single col 'value' */
webdav_gen_prop_tag(srv, con, prop_name, prop_ns, sqlite3_column_text(p->conf.stmt_select_prop, 0), b);
found = 1;
}
return found ? 0 : -1;
}
/* not found */
return -1;
}
typedef struct {
char *ns;
char *prop;
} webdav_property;
webdav_property live_properties[] = {
{ "DAV:", "creationdate" },
{ "DAV:", "displayname" },
{ "DAV:", "getcontentlanguage" },
{ "DAV:", "getcontentlength" },
{ "DAV:", "getcontenttype" },
{ "DAV:", "getetag" },
{ "DAV:", "getlastmodified" },
{ "DAV:", "resourcetype" },
{ "DAV:", "lockdiscovery" },
{ "DAV:", "source" },
{ "DAV:", "supportedlock" },
{ NULL, NULL }
};
typedef struct {
webdav_property **ptr;
size_t used;
size_t size;
} webdav_properties;
static int webdav_get_props(server *srv, connection *con, plugin_data *p, physical *dst, webdav_properties *props, buffer *b_200, buffer *b_404) {
size_t i;
if (props) {
for (i = 0; i < props->used; i++) {
webdav_property *prop;
prop = props->ptr[i];
if (0 != webdav_get_property(srv, con, p,
&(con->physical), prop->prop, prop->ns, b_200)) {
webdav_gen_prop_tag(srv, con, prop->prop, prop->ns, NULL, b_404);
}
}
} else {
for (i = 0; live_properties[i].prop; i++) {
/* a local 'live' property */
webdav_get_live_property(srv, con, p, dst, live_properties[i].prop, b_200);
}
}
return 0;
}
URIHANDLER_FUNC(mod_webdav_subrequest_handler) {
plugin_data *p = p_d;
buffer *b;
DIR *dir;
data_string *ds;
int depth = -1;
struct stat st;
buffer *prop_200;
buffer *prop_404;
webdav_properties *req_props;
UNUSED(srv);
if (!p->conf.enabled) return HANDLER_GO_ON;
/* physical path is setup */
if (con->physical.path->used == 0) return HANDLER_GO_ON;
/* PROPFIND need them */
if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "Depth"))) {
depth = strtol(ds->value->ptr, NULL, 10);
}
switch (con->request.http_method) {
case HTTP_METHOD_PROPFIND:
/* they want to know the properties of the directory */
req_props = NULL;
/* is there a content-body ? */
/* any special requests or just allprop ? */
if (con->request.content_length) {
xmlDocPtr xml;
buffer *xmldoc = con->request.content;
if (NULL != (xml = xmlReadMemory(xmldoc->ptr, xmldoc->used - 1, "DAV", NULL, 0))) {
xmlNode *rootnode = xmlDocGetRootElement(xml);
if (0 == xmlStrcmp(rootnode->name, BAD_CAST "propfind")) {
xmlNode *cmd;
req_props = calloc(1, sizeof(*req_props));
for (cmd = rootnode->children; cmd; cmd = cmd->next) {
if (0 == xmlStrcmp(cmd->name, BAD_CAST "prop")) {
/* get prop by name */
xmlNode *prop;
for (prop = cmd->children; prop; prop = prop->next) {
if (prop->type == XML_TEXT_NODE) continue; /* ignore WS */
if (prop->ns &&
(0 == xmlStrcmp(prop->ns->href, BAD_CAST "")) &&
(0 != xmlStrcmp(prop->ns->prefix, BAD_CAST ""))) {
size_t i;
log_error_write(srv, __FILE__, __LINE__, "ss",
"no name space for:",
prop->name);
xmlFreeDoc(xml);
for (i = 0; i < req_props->used; i++) {
free(req_props->ptr[i]->ns);
free(req_props->ptr[i]->prop);
free(req_props->ptr[i]);
}
free(req_props->ptr);
free(req_props);
con->http_status = 400;
return HANDLER_FINISHED;
}
/* add property to requested list */
if (req_props->size == 0) {
req_props->size = 16;
req_props->ptr = malloc(sizeof(*(req_props->ptr)) * req_props->size);
} else if (req_props->used == req_props->size) {
req_props->size += 16;
req_props->ptr = realloc(req_props->ptr, sizeof(*(req_props->ptr)) * req_props->size);
}
req_props->ptr[req_props->used] = malloc(sizeof(webdav_property));
req_props->ptr[req_props->used]->ns = xmlStrdup(prop->ns ? prop->ns->href : (xmlChar *)"");
req_props->ptr[req_props->used]->prop = xmlStrdup(prop->name);
req_props->used++;