1 /*
2    Access control
3    Copyright (C) 2001, Joe Orton <joe@manyfish.co.uk>
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9 
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public
16    License along with this library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18    MA 02111-1307, USA
19 
20 */
21 
22 /* Contributed by Arun Garg <arung@pspl.co.in> */
23 
24 #include "config.h"
25 
26 #include <sys/types.h>
27 
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 
38 #include "ne_request.h"
39 #include "ne_locks.h"
40 #include "ne_alloc.h"
41 #include "ne_string.h"
42 #include "ne_acl.h"
43 #include "ne_uri.h"
44 #include "ne_xml.h" /* for NE_XML_MEDIA_TYPE */
45 
acl_body(ne_acl_entry * right,int count)46 static ne_buffer *acl_body(ne_acl_entry *right, int count)
47 {
48     ne_buffer *body = ne_buffer_create();
49     int m;
50 
51     ne_buffer_zappend(body,
52 		      "<?xml version=\"1.0\" encoding=\"utf-8\"?>" EOL
53 		      "<acl xmlns='DAV:'>" EOL);
54 
55     for (m = 0; m < count; m++) {
56 	const char *type;
57 
58 	type = (right[m].type == ne_acl_grant ? "grant" : "deny");
59 
60 	ne_buffer_concat(body, "<ace>" EOL "<principal>", NULL);
61 
62 	switch (right[m].apply) {
63 	case ne_acl_all:
64 	    ne_buffer_zappend(body, "<all/>" EOL);
65 	    break;
66 	case ne_acl_property:
67 	    ne_buffer_concat(body, "<property><", right[m].principal,
68 			     "/></property>" EOL, NULL);
69 	    break;
70 	case ne_acl_href:
71 	    ne_buffer_concat(body, "<href>", right[m].principal,
72 			     "</href>" EOL, NULL);
73 	    break;
74 	}
75 
76 	ne_buffer_concat(body, "</principal>" EOL "<", type, ">" EOL, NULL);
77 
78 	if (right[m].read == 0)
79 	    ne_buffer_concat(body,
80 			     "<privilege>" "<read/>" "</privilege>" EOL,
81 			     NULL);
82 	if (right[m].read_acl == 0)
83 	    ne_buffer_concat(body,
84 			     "<privilege>" "<read-acl/>" "</privilege>" EOL,
85 			     NULL);
86 	if (right[m].write == 0)
87 	    ne_buffer_concat(body,
88 			     "<privilege>" "<write/>" "</privilege>" EOL,
89 			     NULL);
90 	if (right[m].write_acl == 0)
91 	    ne_buffer_concat(body,
92 			     "<privilege>" "<write-acl/>" "</privilege>" EOL,
93 			     NULL);
94 	if (right[m].read_cuprivset == 0)
95 	    ne_buffer_concat(body,
96 			     "<privilege>"
97 			     "<read-current-user-privilege-set/>"
98 			     "</privilege>" EOL, NULL);
99 	ne_buffer_concat(body, "</", type, ">" EOL, NULL);
100 	ne_buffer_zappend(body, "</ace>" EOL);
101     }
102     ne_buffer_zappend(body, "</acl>" EOL);
103 
104     return body;
105 }
106 
ne_acl_set(ne_session * sess,const char * uri,ne_acl_entry * entries,int numentries)107 int ne_acl_set(ne_session *sess, const char *uri,
108 	       ne_acl_entry *entries, int numentries)
109 {
110     int ret;
111     ne_request *req = ne_request_create(sess, "ACL", uri);
112     ne_buffer *body = acl_body(entries, numentries);
113 
114 #ifdef USE_DAV_LOCKS
115     ne_lock_using_resource(req, uri, 0);
116 #endif
117 
118     ne_set_request_body_buffer(req, body->data, ne_buffer_size(body));
119     ne_add_request_header(req, "Content-Type", NE_XML_MEDIA_TYPE);
120     ret = ne_request_dispatch(req);
121 
122     ne_buffer_destroy(body);
123 
124     if (ret == NE_OK && ne_get_status(req)->code == 207) {
125 	ret = NE_ERROR;
126     }
127 
128     ne_request_destroy(req);
129     return ret;
130 }
131