props.c revision 4f01ee597f569d80e2e81f912e425a572002328c
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
** DAV extension module for Apache 2.0.*
** - Property database handling (repository-independent)
**
** NOTES:
**
** PROPERTY DATABASE
**
** This version assumes that there is a per-resource database provider
** to record properties. The database provider decides how and where to
** store these databases.
**
** The DBM keys for the properties have the following form:
**
** namespace ":" propname
**
** For example: 5:author
**
** The namespace provides an integer index into the namespace table
** (see below). propname is simply the property name, without a namespace
** prefix.
**
** A special case exists for properties that had a prefix starting with
** "xml". The XML Specification reserves these for future use. mod_dav
** stores and retrieves them unchanged. The keys for these properties
** have the form:
**
** ":" propname
**
** The propname will contain the prefix and the property name. For
** example, a key might be ":xmlfoo:name"
**
** The ":name" style will also be used for properties that do not
** exist within a namespace.
**
** The DBM values consist of two null-terminated strings, appended
** together (the null-terms are retained and stored in the database).
** The first string is the xml:lang value for the property. An empty
** string signifies that a lang value was not in context for the value.
** The second string is the property value itself.
**
**
** NAMESPACE TABLE
**
** The namespace table is an array that lists each of the namespaces
** that are in use by the properties in the given propdb. Each entry
** in the array is a simple URI.
**
** For example: http://www.foo.bar/standards/props/
**
** The prefix used for the property is stripped and the URI for it
** is entered into the namespace table. Also, any namespaces used
** within the property value will be entered into the table (and
** stripped from the child elements).
**
** The namespaces are stored in the DBM database under the "METADATA" key.
**
**
** STRIPPING NAMESPACES
**
** Within the property values, the namespace declarations (xmlns...)
** are stripped. Each element and attribute will have its prefix removed
** and a new prefix inserted.
**
** This must be done so that we can return multiple properties in a
** PROPFIND which may have (originally) used conflicting prefixes. For
** that case, we must bind all property value elements to new namespace
** values.
**
** This implies that clients must NOT be sensitive to the namespace
** prefix used for their properties. It WILL change when the properties
** are returned (we return them as "ns<index>", e.g. "ns5"). Also, the
** property value can contain ONLY XML elements and CDATA. PI and comment
** elements will be stripped. CDATA whitespace will be preserved, but
** whitespace within element tags will be altered. Attribute ordering
** may be altered. Element and CDATA ordering will be preserved.
**
**
** ATTRIBUTES ON PROPERTY NAME ELEMENTS
**
**
** <prop>
** <propname1>value</propname1>
** <propname2>value</propname1>
** </prop>
**
** This implementation (mod_dav) DOES NOT save any attributes that are
** associated with the <propname1> element. The property value is deemed
** to be only the contents ("value" in the above example).
**
** We do store the xml:lang value (if any) that applies to the context
** of the <propname1> element. Whether the xml:lang attribute is on
** <propname1> itself, or from a higher level element, we will store it
** with the property value.
**
**
** VERSIONING
**
** The DBM db contains a key named "METADATA" that holds database-level
** information, such as the namespace table. The record also contains the
** db's version number as the very first 16-bit value. This first number
** is actually stored as two single bytes: the first byte is a "major"
** version number. The second byte is a "minor" number.
**
** If the major number is not what mod_dav expects, then the db is closed
** immediately and an error is returned. A minor number change is
** acceptable -- it is presumed that old/new dav_props.c can deal with
** the database format. For example, a newer dav_props might update the
** minor value and append information to the end of the metadata record
** (which would be ignored by previous versions).
**
**
** ISSUES:
**
** At the moment, for the dav_get_allprops() and dav_get_props() functions,
** we must return a set of xmlns: declarations for ALL known namespaces
** in the file. There isn't a way to filter this because we don't know
** which are going to be used or not. Examining property names is not
** sufficient because the property values could use entirely different
** namespaces.
**
** ==> we must devise a scheme where we can "garbage collect" the namespace
** entries from the property database.
*/
#include "apr.h"
#include "apr_strings.h"
#define APR_WANT_STDIO
#define APR_WANT_BYTEFUNC
#include "apr_want.h"
#include "mod_dav.h"
#include "http_log.h"
#include "http_request.h"
/*
** There is some rough support for writable DAV:getcontenttype and
** DAV:getcontentlanguage properties. If this #define is (1), then
** this support is disabled.
**
** We are disabling it because of a lack of support in GET and PUT
** operations. For GET, it would be "expensive" to look for a propdb,
** open it, and attempt to extract the Content-Type and Content-Language
** values for the response.
** (Handling the PUT would not be difficult, though)
*/
#define DAV_DISABLE_WRITABLE_PROPS 1
struct dav_propdb {
apr_pool_t *p; /* the pool we should use */
request_rec *r; /* the request record */
int deferred; /* open of db has been deferred */
/* if we ever run a GET subreq, it will be stored here */
/* hooks we should use for processing (based on the target resource) */
const dav_hooks_db *db_hooks;
};
/* NOTE: dav_core_props[] and the following enum must stay in sync. */
/* ### move these into a "core" liveprop provider? */
static const char * const dav_core_props[] =
{
"getcontenttype",
"getcontentlanguage",
"lockdiscovery",
"supportedlock",
NULL /* sentinel */
};
enum {
};
/*
** This structure is used to track information needed for a rollback.
*/
typedef struct dav_rollback_item {
/* select one of the two rollback context structures based on the
value of dav_prop_ctx.is_liveprop */
const char *ns_uri,
const char *propname,
const dav_hooks_liveprop **provider)
{
int propid;
/* policy: liveprop providers cannot define no-namespace properties */
return DAV_PROPID_CORE_UNKNOWN;
}
/* check liveprop providers first, so they can define core properties */
provider);
if (propid != 0) {
return propid;
}
/* check for core property */
const char * const *p = dav_core_props;
return propid;
}
}
/* no provider for this property */
return DAV_PROPID_CORE_UNKNOWN;
}
{
const char *ns_uri;
const dav_hooks_liveprop *hooks;
ns_uri = "DAV:";
else
&hooks);
/* ### this test seems redundant... */
}
}
{
/*
** Check the liveprop provider (if this is a provider-defined prop)
*/
}
/* these are defined as read-only */
#endif
) {
return 0;
}
|| propid == DAV_PROPID_CORE_UNKNOWN) {
return 1;
}
/*
** We don't recognize the property, so it must be dead (and writable)
*/
return 1;
}
/* do a sub-request to fetch properties for the target resource's URI. */
{
/* need to escape the uri that's in the resource struct because during
* the property walker it's not encoded. */
/* perform a "GET" on the resource's URI (note that the resource
may not correspond to the current request!). */
}
{
/* fast-path the common case */
if (propid == DAV_PROPID_CORE_UNKNOWN)
return NULL;
switch (propid) {
"DAV:lockdiscovery could not be "
"determined due to a problem fetching "
"the locks for this resource.",
err);
}
/* fast-path the no-locks case */
value = "";
}
else {
/*
** This may modify the buffer. value may point to
** wb_lock.pbuf or a string constant.
*/
/* make a copy to isolate it from changes to wb_lock */
}
}
break;
}
break;
}
}
break;
{
const char *lang;
}
"Content-Language")) != NULL) {
}
break;
}
default:
/* fall through to interpret as a dead property */
break;
}
/* if something was supplied, then insert it */
const char *s;
if (what == DAV_PROP_INSERT_SUPPORTED) {
/* use D: prefix to refer to the DAV: namespace URI,
* and let the namespace attribute default to "DAV:"
*/
s = apr_psprintf(propdb->p,
"<D:supported-live-property D:name=\"%s\"/>" DEBUG_CR,
name);
}
/* use D: prefix to refer to the DAV: namespace URI */
}
else {
/* use D: prefix to refer to the DAV: namespace URI */
}
}
return NULL;
}
const apr_xml_elem *elem,
{
/* this is a "core" property that we define */
}
/* ask the provider (that defined this prop) to insert the prop */
return NULL;
}
const dav_prop_name *name,
{
const char *s;
else {
}
}
{
const char *s;
apr_text_append(p, phdr, s);
}
{
/* we're trying to open the db; turn off the 'deferred' flag */
/* ask the DB provider to open the thing */
"Could not open the property database.",
err);
}
/*
** NOTE: propdb->db could be NULL if we attempted to open a readonly
** access, then a database was created and opened.
*/
return NULL;
}
const dav_resource *resource,
int ro,
{
#if DAV_DEBUG
"INTERNAL DESIGN ERROR: resource must define "
"its URI.");
}
#endif
propdb->r = r;
/* always defer actual open, to avoid expense of accessing db
* when only live properties are involved
*/
/* ### what to do about closing the propdb on server failure? */
return NULL;
}
{
}
/* Currently, mod_dav's pool usage doesn't allow clearing this pool. */
#if 0
apr_pool_destroy(propdb->p);
#endif
return;
}
{
apr_text_header hdr = { 0 };
apr_text_header hdr_ns = { 0 };
dav_get_props_result result = { 0 };
int found_contenttype = 0;
int found_contentlang = 0;
/* if not just getting supported live properties,
* scan all properties in the dead prop database
*/
if (what != DAV_PROP_INSERT_SUPPORTED) {
/* ### what to do with db open error? */
}
/* initialize the result with some start tags... */
"<D:propstat>" DEBUG_CR
"<D:prop>" DEBUG_CR);
/* if there ARE properties, then scan them */
/* define (up front) any namespaces the db might need */
/* get the first property name, beginning the scan */
/*
** We also look for <DAV:getcontenttype> and
** <DAV:getcontentlanguage>. If they are not stored as dead
** properties, then we need to perform a subrequest to get
** their values (if any).
*/
found_contenttype = 1;
}
found_contentlang = 1;
}
}
if (what == DAV_PROP_INSERT_VALUE) {
int found;
/* ### anything better to do? */
/* ### probably should enter a 500 error */
goto next_key;
}
/* assert: found == 1 */
}
else {
/* the value was not requested, so just add an empty
tag specifying the property name. */
}
}
/* all namespaces have been entered into xi. generate them into
the output now. */
} /* propdb->db != NULL */
/* add namespaces for all the liveprop providers */
}
/* ask the liveprop providers to insert their properties */
/* insert the standard properties */
/* ### should be handling the return errors here */
(void)dav_insert_coreprop(propdb,
DAV_PROPID_CORE_supportedlock, "supportedlock",
(void)dav_insert_coreprop(propdb,
DAV_PROPID_CORE_lockdiscovery, "lockdiscovery",
/* if we didn't find these, then do the whole subreq thing. */
if (!found_contenttype) {
/* ### should be handling the return error here */
(void)dav_insert_coreprop(propdb,
"getcontenttype",
}
if (!found_contentlang) {
/* ### should be handling the return error here */
(void)dav_insert_coreprop(propdb,
"getcontentlanguage",
}
/* if not just reporting on supported live props,
* terminate the result */
if (what != DAV_PROP_INSERT_SUPPORTED) {
"</D:prop>" DEBUG_CR
"<D:status>HTTP/1.1 200 OK</D:status>" DEBUG_CR
"</D:propstat>" DEBUG_CR);
}
return result;
}
{
apr_text_header hdr_good = { 0 };
apr_text_header hdr_bad = { 0 };
apr_text_header hdr_ns = { 0 };
int have_good = 0;
dav_get_props_result result = { 0 };
char *marks_liveprop;
int xi_filled = 0;
/* ### NOTE: we should pass in TWO buffers -- one for keys, one for
the marks */
/* we will ALWAYS provide a "good" result, even if it is EMPTY */
"<D:propstat>" DEBUG_CR
"<D:prop>" DEBUG_CR);
/* ### the marks should be in a buffer! */
/* allocate zeroed-memory for the marks. These marks indicate which
liveprop namespaces we've generated into the output xmlns buffer */
/* same for the liveprops */
/*
** First try live property providers; if they don't handle
** the property, then try looking it up in the propdb.
*/
}
/* cache the propid; dav_get_props() could be called many times */
/* insert the property. returns 1 if an insertion was done. */
/* ### need to propagate the error to the caller... */
/* ### skip it for now, as if nothing was inserted */
}
if (inserted == DAV_PROP_INSERT_VALUE) {
have_good = 1;
/*
** Add the liveprop's namespace URIs. Note that provider==NULL
** for core properties.
*/
const char * const * scan_ns_uri;
*scan_ns_uri != NULL;
++scan_ns_uri) {
long ns;
if (marks_liveprop[ns])
continue;
&hdr_ns);
}
}
/* property added. move on to the next property. */
continue;
}
else if (inserted == DAV_PROP_INSERT_NOTDEF) {
/* nothing to do. fall thru to allow property to be handled
as a dead property */
}
#if DAV_DEBUG
else {
#if 0
/* ### need to change signature to return an error */
0,
"INTERNAL DESIGN ERROR: insert_liveprop "
"did not insert what was asked for.");
#endif
}
#endif
}
/* The property wasn't a live property, so look in the dead property
database. */
/* make sure propdb is really open */
/* ### what to do with db open error? */
}
else
/* only bother to look if a database exists */
int found;
/* ### what to do? continue doesn't seem right... */
continue;
}
if (found) {
have_good = 1;
/* if we haven't added the db's namespaces, then do so... */
if (!xi_filled) {
xi_filled = 1;
}
continue;
}
}
/* not found as a live OR dead property. add a record to the "bad"
propstats */
/* make sure we've started our "bad" propstat */
"<D:propstat>" DEBUG_CR
"<D:prop>" DEBUG_CR);
}
/* output this property's name (into the bad propstats) */
}
"</D:prop>" DEBUG_CR
"<D:status>HTTP/1.1 200 OK</D:status>" DEBUG_CR
"</D:propstat>" DEBUG_CR);
/* default to start with the good */
/* we may not have any "bad" results */
/* "close" the bad propstat */
"</D:prop>" DEBUG_CR
"<D:status>HTTP/1.1 404 Not Found</D:status>" DEBUG_CR
"</D:propstat>" DEBUG_CR);
/* if there are no good props, then just return the bad */
if (!have_good) {
}
else {
/* hook the bad propstat to the end of the good one */
}
}
/* add in all the various namespaces, and return them */
return result;
}
const char *ns_uri,
const char *propname,
{
int propid;
const dav_hooks_liveprop *hooks;
if (propid != DAV_PROPID_CORE_UNKNOWN) {
/* this is a "core" property that we define */
}
else {
}
}
}
{
/*
** Check to see if this is a live property, and fill the fields
** in the XML elem, as appropriate.
**
** be SET or DELETEd.
*/
/* it's a liveprop if a provider was found */
/* ### actually the "core" props should really be liveprops, but
### there is no "provider" for those and the r/w props are
### treated as dead props anyhow */
}
"Property is read-only.");
return;
}
if (ctx->is_liveprop) {
int defer_to_dead = 0;
&ctx->liveprop_ctx,
return;
/* clear is_liveprop -- act as a dead prop now */
ctx->is_liveprop = 0;
}
/*
** The property is supposed to be stored into the dead-property
** database. Make sure the thing is truly open (and writable).
*/
return;
}
/*
** There should be an open, writable database in here!
**
** Note: the database would be NULL if it was opened readonly and it
** did not exist.
*/
"property database.");
return;
}
/*
** Prep the element => propdb namespace index mapping, inserting
** namespace URIs into the propdb that don't exist.
*/
}
/*
** There are no checks to perform here. If a property exists, then
** we will delete it. If it does not exist, then it does not matter
** (see S12.13.1).
**
** Note that if a property does not exist, that does not rule out
** that a SET will occur during this PROPPATCH (thusly creating it).
*/
}
}
{
if (ctx->is_liveprop) {
}
else {
else
/* save the old value so that we can do a rollback. */
goto error;
/* Note: propdb->mapping was set in dav_prop_validate() */
/*
** If an error occurred, then assume that we didn't change the
** value. Remove the rollback item so that we don't try to set
** its value during the rollback.
*/
/* ### euh... where is the removal? */
}
/*
** Delete the property. Ignore errors -- the property is there, or
** we are deleting it for a second time.
**
** http://tools.ietf.org/html/rfc4918#section-14.23 says
** "Specifying the removal of a property that does not exist is
** not an error"
*/
/* ### but what about other errors? */
}
}
/* push a more specific error here */
/*
** Use HTTP_INTERNAL_SERVER_ERROR because we shouldn't have seen
** any errors at this point.
*/
"Could not execute PROPPATCH.", err);
}
}
{
/*
** Note that a commit implies ctx->err is NULL. The caller should assume
** a status of HTTP_OK for this case.
*/
if (ctx->is_liveprop) {
}
}
{
/* do nothing if there is no rollback information. */
return;
/*
** ### if we have an error, and a rollback occurs, then the namespace
** ### mods should not happen at all. Basically, the namespace management
** ### is simply a bitch.
*/
if (ctx->is_liveprop) {
}
else {
}
else {
/* hook previous errors at the end of the rollback error */
}
}
}