Tue, 15 May 2018 18:48:53 +0200
adds test suite to build system
51 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
91 | 31 | #include <string.h> |
51 | 32 | |
33 | #include "acl.h" | |
34 | ||
35 | ACLFile* load_acl_file(char *file) { | |
36 | FILE *in = fopen(file, "r"); | |
37 | if(in == NULL) { | |
38 | return NULL; | |
39 | } | |
40 | ||
41 | ACLFile *conf = malloc(sizeof(ACLFile)); | |
42 | conf->parser.parse = acl_parse; | |
43 | conf->namedACLs = NULL; | |
44 | conf->uriACLs = NULL; | |
45 | conf->pathACLs = NULL; | |
46 | ||
47 | int r = cfg_parse_basic_file((ConfigParser*)conf, in); | |
48 | if(r != 0) { | |
49 | free_acl_file(conf); | |
50 | return NULL; | |
51 | } | |
79
f48cea237ec3
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
52 | |
62
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
53 | fclose(in); |
51 | 54 | |
55 | return conf; | |
56 | } | |
57 | ||
79
f48cea237ec3
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
58 | void free_acl_file(ACLFile *conf) { |
91 | 59 | ucx_mempool_destroy(conf->parser.mp->pool); |
79
f48cea237ec3
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
60 | free(conf); |
51 | 61 | } |
62 | ||
63 | int acl_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) { | |
64 | ACLFile *aclf = p; | |
91 | 65 | UcxAllocator *mp = aclf->parser.mp; |
51 | 66 | |
97
09fbefc0e6a9
added ldap group support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
91
diff
changeset
|
67 | if(sstrprefix(line, sstr("ACL "))) { |
51 | 68 | sstr_t param = sstrsubs(line, 4); |
69 | UcxList *plist = cfg_param_list(param, mp); | |
70 | ACLConfig *acl = OBJ_NEW(mp, ACLConfig); | |
71 | acl->type.ptr = NULL; | |
72 | acl->authparam = NULL; | |
73 | acl->entries = NULL; | |
74 | aclf->cur = acl; | |
75 | ||
76 | sstr_t type = cfg_param_get(plist, sstr("type")); | |
77 | sstr_t name = cfg_param_get(plist, sstr("name")); | |
78 | sstr_t path = cfg_param_get(plist, sstr("path")); | |
79 | sstr_t uri = cfg_param_get(plist, sstr("uri")); | |
80 | ||
81 | if(name.ptr) { | |
82 | acl->id = name; | |
91 | 83 | aclf->namedACLs = ucx_list_append_a(mp, aclf->namedACLs, acl); |
51 | 84 | } else if(path.ptr) { |
85 | acl->id = path; | |
91 | 86 | aclf->pathACLs = ucx_list_append_a(mp, aclf->pathACLs, acl); |
51 | 87 | } else if(uri.ptr) { |
88 | acl->id = uri; | |
91 | 89 | aclf->uriACLs = ucx_list_append_a(mp, aclf->uriACLs, acl); |
51 | 90 | } |
91 | ||
92 | if(type.ptr) { | |
93 | acl->type = type; | |
94 | } | |
97
09fbefc0e6a9
added ldap group support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
91
diff
changeset
|
95 | } else if(sstrprefix(line, sstr("Authenticate "))) { |
51 | 96 | sstr_t param = sstrsubs(line, 13); |
97 | UcxList *plist = cfg_param_list(param, mp); | |
98 | aclf->cur->authparam = plist; | |
99 | } else { | |
100 | if(parse_ace(aclf, line)) { | |
101 | // TODO: error | |
102 | return 1; | |
103 | } | |
104 | } | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
109 | int parse_ace(ACLFile *f, sstr_t line) { | |
110 | ACLConfig *cur = f->cur; | |
91 | 111 | UcxAllocator *mp = f->parser.mp; |
51 | 112 | |
101
7fbcdbad0baa
added support for absolute URIs and improved keep alive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
97
diff
changeset
|
113 | ssize_t tkn = 0; |
51 | 114 | sstr_t *tk = sstrsplit(line, sstr(":"), &tkn); |
115 | if(!tk || tkn < 3) { | |
115
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
116 | log_ereport(LOG_FAILURE, "parse_ace: to few tokens"); |
51 | 117 | return 1; |
118 | } | |
119 | ||
120 | ACEConfig *ace = OBJ_NEW(mp, ACEConfig); | |
121 | memset(ace, 0, sizeof(ACEConfig)); | |
122 | ||
123 | /* | |
124 | * first step: determine who is affected by this ace | |
125 | */ | |
126 | int n = 0; | |
127 | sstr_t s = tk[0]; | |
128 | ||
129 | if(!sstrcmp(s, sstr("user"))) { | |
130 | // next token is the user name | |
131 | s = tk[1]; | |
132 | n++; | |
91 | 133 | ace->who = sstrdup_a(mp, s); |
51 | 134 | } else if(!sstrcmp(s, sstr("group"))) { |
135 | // next token is the group name | |
136 | s = tk[1]; | |
137 | n++; | |
91 | 138 | ace->who = sstrdup_a(mp, s); |
51 | 139 | ace->flags = ACLCFG_IDENTIFIER_GROUP; |
140 | } else if(!sstrcmp(s, sstr("owner@"))) { | |
141 | ace->flags = ACLCFG_OWNER; | |
142 | } else if(!sstrcmp(s, sstr("group@"))) { | |
143 | ace->flags = ACLCFG_GROUP; | |
144 | } else if(!sstrcmp(s, sstr("everyone@"))) { | |
145 | ace->flags = ACLCFG_EVERYONE; | |
146 | } else { | |
147 | // you can specify only the user name in the ace | |
91 | 148 | ace->who = sstrdup_a(mp, s); |
51 | 149 | } |
150 | ||
151 | n++; //next token | |
152 | ||
153 | /* | |
154 | * get the access mask | |
155 | */ | |
156 | ||
157 | if(n >= tkn) { | |
158 | // to few tokens | |
115
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
159 | log_ereport(LOG_FAILURE, "parse_ace: ace incomplete"); |
51 | 160 | return 1; |
161 | } | |
162 | s = tk[n]; | |
163 | ||
101
7fbcdbad0baa
added support for absolute URIs and improved keep alive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
97
diff
changeset
|
164 | ssize_t maskn = 0; |
51 | 165 | sstr_t *accessmask = sstrsplit(s, sstr(","), &maskn); |
166 | for(int i=0;i<maskn;i++) { | |
167 | sstr_t access = accessmask[i]; | |
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
168 | ace->access_mask = ace->access_mask | accstr2int(access); |
51 | 169 | } |
170 | free(accessmask); | |
171 | n++; // next token | |
172 | ||
173 | /* | |
174 | * get flags (optional) and ace type | |
175 | */ | |
176 | ||
177 | int complete = 0; | |
178 | while(n < tkn) { | |
179 | s = tk[n]; | |
180 | if(!sstrcmp(s, sstr("allow"))) { | |
181 | ace->type = ACLCFG_TYPE_ALLOWED; | |
182 | complete = 1; | |
183 | break; | |
184 | } else if(!sstrcmp(s, sstr("deny"))) { | |
185 | ace->type = ACLCFG_TYPE_DENIED; | |
186 | complete = 1; | |
187 | break; | |
188 | } else if(!sstrcmp(s, sstr("audit"))) { | |
189 | ace->type = ACLCFG_TYPE_AUDIT; | |
190 | complete = 1; | |
191 | break; | |
192 | } else if(!sstrcmp(s, sstr("alarm"))) { | |
193 | ace->type = ACLCFG_TYPE_ALARM; | |
194 | complete = 1; | |
195 | break; | |
196 | } else { | |
197 | // set flags | |
101
7fbcdbad0baa
added support for absolute URIs and improved keep alive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
97
diff
changeset
|
198 | ssize_t fln = 0; |
51 | 199 | sstr_t *flags = sstrsplit(s, sstr(","), &fln); |
200 | for(int i=0;i<fln;i++) { | |
201 | sstr_t flag = flags[i]; | |
202 | if(!sstrcmp(flag, sstr("successful_access_flag"))) { | |
203 | ace->flags = ace->flags | ACLCFG_SUCCESSFUL_ACCESS_FLAG; | |
204 | } else if(!sstrcmp(flag, sstr("failed_access_flag"))) { | |
205 | ace->flags = ace->flags | ACLCFG_FAILED_ACCESS_ACE_FLAG; | |
206 | } | |
207 | // TODO: other flags | |
208 | } | |
209 | free(flags); | |
210 | } | |
211 | n++; | |
212 | } | |
213 | ||
214 | if(!complete) { | |
115
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
215 | log_ereport(LOG_FAILURE, "parse_ace: ace incomplete"); |
51 | 216 | return 1; |
217 | } | |
218 | ||
91 | 219 | cur->entries = ucx_list_append_a(mp, cur->entries, ace); |
51 | 220 | |
221 | return 0; | |
222 | } | |
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
223 | |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
224 | uint32_t accstr2int(sstr_t access) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
225 | uint32_t val = 0; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
226 | if(!sstrcmp(access, sstr("read"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
227 | val = ACLCFG_READ; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
228 | } else if(!sstrcmp(access, sstr("write"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
229 | val = ACLCFG_WRITE; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
230 | } else if(!sstrcmp(access, sstr("read_data"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
231 | val = ACLCFG_READ_DATA; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
232 | } else if(!sstrcmp(access, sstr("write_data"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
233 | val = ACLCFG_WRITE_DATA; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
234 | } else if(!sstrcmp(access, sstr("append"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
235 | val = ACLCFG_APPEND; |
56
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
52
diff
changeset
|
236 | } else if(!sstrcmp(access, sstr("add"))) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
52
diff
changeset
|
237 | val = ACLCFG_ADD_FILE; |
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
238 | } else if(!sstrcmp(access, sstr("add_file"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
239 | val = ACLCFG_ADD_FILE; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
240 | } else if(!sstrcmp(access, sstr("add_subdirectory"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
241 | val = ACLCFG_ADD_SUBDIRECTORY; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
242 | } else if(!sstrcmp(access, sstr("read_xattr"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
243 | val = ACLCFG_READ_XATTR; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
244 | } else if(!sstrcmp(access, sstr("write_xattr"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
245 | val = ACLCFG_WRITE_XATTR; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
246 | } else if(!sstrcmp(access, sstr("execute"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
247 | val = ACLCFG_EXECUTE; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
248 | } else if(!sstrcmp(access, sstr("delete_child"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
249 | val = ACLCFG_DELETE_CHILD; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
250 | } else if(!sstrcmp(access, sstr("delete"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
251 | val = ACLCFG_DELETE; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
252 | } else if(!sstrcmp(access, sstr("read_attributes"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
253 | val = ACLCFG_READ_ATTRIBUTES; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
254 | } else if(!sstrcmp(access, sstr("write_attributes"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
255 | val = ACLCFG_WRITE_ATTRIBUTES; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
256 | } else if(!sstrcmp(access, sstr("list"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
257 | val = ACLCFG_LIST; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
258 | } else if(!sstrcmp(access, sstr("read_acl"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
259 | val = ACLCFG_READ_ACL; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
260 | } else if(!sstrcmp(access, sstr("write_acl"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
261 | val = ACLCFG_WRITE_ACL; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
262 | } else if(!sstrcmp(access, sstr("write_owner"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
263 | val = ACLCFG_WRITE_OWNER; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
264 | } else if(!sstrcmp(access, sstr("synchronize"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
265 | val = ACLCFG_SYNCHRONIZE; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
266 | } |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
267 | return val; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
268 | } |