summaryrefslogtreecommitdiff
path: root/src/ne_acl3744.c
blob: 3ad7e1df49b5df9439fc8c3072f7fee97a71fff5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
   Access control
   Copyright (C) 2001-2006, 2008, Joe Orton <joe@manyfish.co.uk>
   Copyright (C) 2001, Arun Garg <arung@pspl.co.in>
   Copyright (C) 2007 Henrik Holst <henrik.holst2@gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA

*/

/* Contributed by Arun Garg <arung@pspl.co.in> */

#include "config.h"

#include <sys/types.h>

#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#include "ne_request.h"
#include "ne_locks.h"
#include "ne_alloc.h"
#include "ne_string.h"
#include "ne_acl3744.h"
#include "ne_uri.h"
#include "ne_xml.h" /* for NE_XML_MEDIA_TYPE */

#define EOL "\r\n"

static ne_buffer *acl_body(const ne_acl_entry *right, int count)
{
    ne_buffer *body = ne_buffer_create();
    int m;

    ne_buffer_zappend(body,
		      "<?xml version=\"1.0\" encoding=\"utf-8\"?>" EOL
		      "<acl xmlns='DAV:'>" EOL);

    for (m = 0; m < count; m++) {
	const char *type;

	type = (right[m].type == ne_acl_grant ? "grant" : "deny");

	ne_buffer_concat(body, "<ace>" EOL "<principal>", NULL);

	switch (right[m].target) {
	case ne_acl_all:
	    ne_buffer_czappend(body, "<all/>" EOL);
	    break;
	case ne_acl_authenticated:
	    ne_buffer_czappend(body, "<authenticated/>" EOL);
	    break;
	case ne_acl_unauthenticated:
	    ne_buffer_czappend(body, "<unauthenticated/>" EOL);
	    break;
	case ne_acl_self:
	    ne_buffer_czappend(body, "<self/>" EOL);
	    break;
	case ne_acl_property:
	    ne_buffer_concat(body, "<property><", right[m].tname,
			     "/></property>" EOL, NULL);
	    break;
	case ne_acl_href:
	    ne_buffer_concat(body, "<href>", right[m].tname,
			     "</href>" EOL, NULL);
	    break;
	}

	ne_buffer_concat(body, "</principal>" EOL 
                         "<", type, ">" EOL, NULL);

	if ((right[m].privileges & NE_ACL_READ) == NE_ACL_READ)
	    ne_buffer_concat(body,
			     "<privilege>" "<read/>" "</privilege>" EOL,
			     NULL);

	if ((right[m].privileges & NE_ACL_WRITE) == NE_ACL_WRITE)
	    ne_buffer_concat(body,
			     "<privilege>" "<write/>" "</privilege>" EOL,
			     NULL);

	if ((right[m].privileges & NE_ACL_WRITE_PROPERTIES) == NE_ACL_WRITE_PROPERTIES)
	    ne_buffer_concat(body,
			     "<privilege>" "<write-properties/>" "</privilege>" EOL,
			     NULL);

	if ((right[m].privileges & NE_ACL_WRITE_CONTENT) == NE_ACL_WRITE_CONTENT)
	    ne_buffer_concat(body,
			     "<privilege>" "<write-content/>" "</privilege>" EOL,
			     NULL);

	if ((right[m].privileges & NE_ACL_UNLOCK) == NE_ACL_UNLOCK)
	    ne_buffer_concat(body,
			     "<privilege>" "<unlock/>" "</privilege>" EOL,
			     NULL);

	if ((right[m].privileges & NE_ACL_READ_ACL) == NE_ACL_READ_ACL)
	    ne_buffer_concat(body,
			     "<privilege>" "<read-acl/>" "</privilege>" EOL,
			     NULL);

	if ((right[m].privileges & NE_ACL_READ_CUPRIVSET) == NE_ACL_READ_CUPRIVSET)
	    ne_buffer_concat(body,
			     "<privilege>" "<read-current-user-privileges-set/>" "</privilege>" EOL,
			     NULL);

	if ((right[m].privileges & NE_ACL_WRITE_ACL) == NE_ACL_WRITE_ACL)
	    ne_buffer_concat(body,
			     "<privilege>" "<write-acl/>" "</privilege>" EOL,
			     NULL);

	if ((right[m].privileges & NE_ACL_BIND) == NE_ACL_BIND)
	    ne_buffer_concat(body,
			     "<privilege>" "<bind/>" "</privilege>" EOL,
			     NULL);

	if ((right[m].privileges & NE_ACL_UNBIND) == NE_ACL_UNBIND)
	    ne_buffer_concat(body,
			     "<privilege>" "<unbind/>" "</privilege>" EOL,
			     NULL);

	if ((right[m].privileges & NE_ACL_ALL) == NE_ACL_ALL)
	    ne_buffer_concat(body,
			     "<privilege>" "<all/>" "</privilege>" EOL,
			     NULL);

	ne_buffer_concat(body, "</", type, ">" EOL, NULL);
	ne_buffer_czappend(body, "</ace>" EOL);
    }

    ne_buffer_czappend(body, "</acl>" EOL);

    return body;
}

int ne_acl3744_set(ne_session *sess, const char *uri,
                   const ne_acl_entry *entries, int numentries)
{
    int ret;
    ne_request *req = ne_request_create(sess, "ACL", uri);
    ne_buffer *body = acl_body(entries, numentries);

#ifdef NE_HAVE_DAV
    ne_lock_using_resource(req, uri, 0);
#endif

    ne_set_request_body_buffer(req, body->data, ne_buffer_size(body));
    ne_add_request_header(req, "Content-Type", NE_XML_MEDIA_TYPE);
    ret = ne_request_dispatch(req);

    ne_buffer_destroy(body);

    if (ret == NE_OK && ne_get_status(req)->code == 207) {
	ret = NE_ERROR;
    }

    ne_request_destroy(req);
    return ret;
}