1/*
2   Access control
3   Copyright (C) 2001-2006, 2008, Joe Orton <joe@manyfish.co.uk>
4   Copyright (C) 2001, Arun Garg <arung@pspl.co.in>
5   Copyright (C) 2007 Henrik Holst <henrik.holst2@gmail.com>
6
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Library General Public
9   License as published by the Free Software Foundation; either
10   version 2 of the License, or (at your option) any later version.
11
12   This library is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   Library General Public License for more details.
16
17   You should have received a copy of the GNU Library General Public
18   License along with this library; if not, write to the Free
19   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20   MA 02111-1307, USA
21
22*/
23
24/* Contributed by Arun Garg <arung@pspl.co.in> */
25
26#include "config.h"
27
28#include <sys/types.h>
29
30#ifdef HAVE_STRING_H
31#include <string.h>
32#endif
33#ifdef HAVE_UNISTD_H
34#include <unistd.h>
35#endif
36#ifdef HAVE_STDLIB_H
37#include <stdlib.h>
38#endif
39
40#include "ne_request.h"
41#include "ne_locks.h"
42#include "ne_alloc.h"
43#include "ne_string.h"
44#include "ne_acl3744.h"
45#include "ne_uri.h"
46#include "ne_xml.h" /* for NE_XML_MEDIA_TYPE */
47
48#define EOL "\r\n"
49
50static ne_buffer *acl_body(const ne_acl_entry *right, int count)
51{
52    ne_buffer *body = ne_buffer_create();
53    int m;
54
55    ne_buffer_zappend(body,
56		      "<?xml version=\"1.0\" encoding=\"utf-8\"?>" EOL
57		      "<acl xmlns='DAV:'>" EOL);
58
59    for (m = 0; m < count; m++) {
60	const char *type;
61
62	type = (right[m].type == ne_acl_grant ? "grant" : "deny");
63
64	ne_buffer_concat(body, "<ace>" EOL "<principal>", NULL);
65
66	switch (right[m].target) {
67	case ne_acl_all:
68	    ne_buffer_czappend(body, "<all/>" EOL);
69	    break;
70	case ne_acl_authenticated:
71	    ne_buffer_czappend(body, "<authenticated/>" EOL);
72	    break;
73	case ne_acl_unauthenticated:
74	    ne_buffer_czappend(body, "<unauthenticated/>" EOL);
75	    break;
76	case ne_acl_self:
77	    ne_buffer_czappend(body, "<self/>" EOL);
78	    break;
79	case ne_acl_property:
80	    ne_buffer_concat(body, "<property><", right[m].tname,
81			     "/></property>" EOL, NULL);
82	    break;
83	case ne_acl_href:
84	    ne_buffer_concat(body, "<href>", right[m].tname,
85			     "</href>" EOL, NULL);
86	    break;
87	}
88
89	ne_buffer_concat(body, "</principal>" EOL
90                         "<", type, ">" EOL, NULL);
91
92	if ((right[m].privileges & NE_ACL_READ) == NE_ACL_READ)
93	    ne_buffer_concat(body,
94			     "<privilege>" "<read/>" "</privilege>" EOL,
95			     NULL);
96
97	if ((right[m].privileges & NE_ACL_WRITE) == NE_ACL_WRITE)
98	    ne_buffer_concat(body,
99			     "<privilege>" "<write/>" "</privilege>" EOL,
100			     NULL);
101
102	if ((right[m].privileges & NE_ACL_WRITE_PROPERTIES) == NE_ACL_WRITE_PROPERTIES)
103	    ne_buffer_concat(body,
104			     "<privilege>" "<write-properties/>" "</privilege>" EOL,
105			     NULL);
106
107	if ((right[m].privileges & NE_ACL_WRITE_CONTENT) == NE_ACL_WRITE_CONTENT)
108	    ne_buffer_concat(body,
109			     "<privilege>" "<write-content/>" "</privilege>" EOL,
110			     NULL);
111
112	if ((right[m].privileges & NE_ACL_UNLOCK) == NE_ACL_UNLOCK)
113	    ne_buffer_concat(body,
114			     "<privilege>" "<unlock/>" "</privilege>" EOL,
115			     NULL);
116
117	if ((right[m].privileges & NE_ACL_READ_ACL) == NE_ACL_READ_ACL)
118	    ne_buffer_concat(body,
119			     "<privilege>" "<read-acl/>" "</privilege>" EOL,
120			     NULL);
121
122	if ((right[m].privileges & NE_ACL_READ_CUPRIVSET) == NE_ACL_READ_CUPRIVSET)
123	    ne_buffer_concat(body,
124			     "<privilege>" "<read-current-user-privileges-set/>" "</privilege>" EOL,
125			     NULL);
126
127	if ((right[m].privileges & NE_ACL_WRITE_ACL) == NE_ACL_WRITE_ACL)
128	    ne_buffer_concat(body,
129			     "<privilege>" "<write-acl/>" "</privilege>" EOL,
130			     NULL);
131
132	if ((right[m].privileges & NE_ACL_BIND) == NE_ACL_BIND)
133	    ne_buffer_concat(body,
134			     "<privilege>" "<bind/>" "</privilege>" EOL,
135			     NULL);
136
137	if ((right[m].privileges & NE_ACL_UNBIND) == NE_ACL_UNBIND)
138	    ne_buffer_concat(body,
139			     "<privilege>" "<unbind/>" "</privilege>" EOL,
140			     NULL);
141
142	if ((right[m].privileges & NE_ACL_ALL) == NE_ACL_ALL)
143	    ne_buffer_concat(body,
144			     "<privilege>" "<all/>" "</privilege>" EOL,
145			     NULL);
146
147	ne_buffer_concat(body, "</", type, ">" EOL, NULL);
148	ne_buffer_czappend(body, "</ace>" EOL);
149    }
150
151    ne_buffer_czappend(body, "</acl>" EOL);
152
153    return body;
154}
155
156int ne_acl3744_set(ne_session *sess, const char *uri,
157                   const ne_acl_entry *entries, int numentries)
158{
159    int ret;
160    ne_request *req = ne_request_create(sess, "ACL", uri);
161    ne_buffer *body = acl_body(entries, numentries);
162
163#ifdef NE_HAVE_DAV
164    ne_lock_using_resource(req, uri, 0);
165#endif
166
167    ne_set_request_body_buffer(req, body->data, ne_buffer_size(body));
168    ne_add_request_header(req, "Content-Type", NE_XML_MEDIA_TYPE);
169    ret = ne_request_dispatch(req);
170
171    ne_buffer_destroy(body);
172
173    if (ret == NE_OK && ne_get_status(req)->code == 207) {
174	ret = NE_ERROR;
175    }
176
177    ne_request_destroy(req);
178    return ret;
179}
180