Sat, 22 Jun 2013 13:08:36 +0200
some fixes
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> | |
31 | ||
32 | #include "acl.h" | |
33 | ||
34 | ACLFile* load_acl_file(char *file) { | |
35 | FILE *in = fopen(file, "r"); | |
36 | if(in == NULL) { | |
37 | return NULL; | |
38 | } | |
39 | ||
40 | ACLFile *conf = malloc(sizeof(ACLFile)); | |
41 | conf->parser.parse = acl_parse; | |
42 | conf->file = file; | |
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 | } | |
62
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
52 | |
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 | ||
58 | void free_acl_file(ACLFile *aclfile) { | |
59 | ||
60 | } | |
61 | ||
62 | int acl_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) { | |
63 | ACLFile *aclf = p; | |
64 | UcxMempool *mp = aclf->parser.mp; | |
65 | ||
66 | if(sstr_startswith(line, sstr("ACL "))) { | |
67 | sstr_t param = sstrsubs(line, 4); | |
68 | UcxList *plist = cfg_param_list(param, mp); | |
69 | ACLConfig *acl = OBJ_NEW(mp, ACLConfig); | |
70 | acl->type.ptr = NULL; | |
71 | acl->authparam = NULL; | |
72 | acl->entries = NULL; | |
73 | aclf->cur = acl; | |
74 | ||
75 | sstr_t type = cfg_param_get(plist, sstr("type")); | |
76 | sstr_t name = cfg_param_get(plist, sstr("name")); | |
77 | sstr_t path = cfg_param_get(plist, sstr("path")); | |
78 | sstr_t uri = cfg_param_get(plist, sstr("uri")); | |
79 | ||
80 | if(name.ptr) { | |
81 | acl->id = name; | |
82 | aclf->namedACLs = ucx_list_append(aclf->namedACLs, acl); | |
83 | } else if(path.ptr) { | |
84 | acl->id = path; | |
85 | aclf->pathACLs = ucx_list_append(aclf->pathACLs, acl); | |
86 | } else if(uri.ptr) { | |
87 | acl->id = uri; | |
88 | aclf->uriACLs = ucx_list_append(aclf->uriACLs, acl); | |
89 | } | |
90 | ||
91 | if(type.ptr) { | |
92 | acl->type = type; | |
93 | } | |
94 | } else if(sstr_startswith(line, sstr("Authenticate "))) { | |
95 | sstr_t param = sstrsubs(line, 13); | |
96 | UcxList *plist = cfg_param_list(param, mp); | |
97 | aclf->cur->authparam = plist; | |
98 | } else { | |
99 | if(parse_ace(aclf, line)) { | |
100 | // TODO: error | |
101 | return 1; | |
102 | } | |
103 | } | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
108 | int parse_ace(ACLFile *f, sstr_t line) { | |
109 | ACLConfig *cur = f->cur; | |
110 | UcxMempool *mp = f->parser.mp; | |
111 | ||
112 | size_t tkn = 0; | |
113 | sstr_t *tk = sstrsplit(line, sstr(":"), &tkn); | |
114 | if(!tk || tkn < 3) { | |
115 | fprintf(stderr, "parse_ace: to few tokens\n"); | |
116 | return 1; | |
117 | } | |
118 | ||
119 | ACEConfig *ace = OBJ_NEW(mp, ACEConfig); | |
120 | memset(ace, 0, sizeof(ACEConfig)); | |
121 | ||
122 | /* | |
123 | * first step: determine who is affected by this ace | |
124 | */ | |
125 | int n = 0; | |
126 | sstr_t s = tk[0]; | |
127 | ||
128 | if(!sstrcmp(s, sstr("user"))) { | |
129 | // next token is the user name | |
130 | s = tk[1]; | |
131 | n++; | |
132 | ace->who = sstrdup_mp(mp, s); | |
133 | } else if(!sstrcmp(s, sstr("group"))) { | |
134 | // next token is the group name | |
135 | s = tk[1]; | |
136 | n++; | |
137 | ace->who = sstrdup_mp(mp, s); | |
138 | ace->flags = ACLCFG_IDENTIFIER_GROUP; | |
139 | } else if(!sstrcmp(s, sstr("owner@"))) { | |
140 | ace->flags = ACLCFG_OWNER; | |
141 | } else if(!sstrcmp(s, sstr("group@"))) { | |
142 | ace->flags = ACLCFG_GROUP; | |
143 | } else if(!sstrcmp(s, sstr("everyone@"))) { | |
144 | ace->flags = ACLCFG_EVERYONE; | |
145 | } else { | |
146 | // you can specify only the user name in the ace | |
147 | ace->who = sstrdup_mp(mp, s); | |
148 | } | |
149 | ||
150 | n++; //next token | |
151 | ||
152 | /* | |
153 | * get the access mask | |
154 | */ | |
155 | ||
156 | if(n >= tkn) { | |
157 | // to few tokens | |
158 | fprintf(stderr, "parse_ace: ace incomplete\n"); | |
159 | return 1; | |
160 | } | |
161 | s = tk[n]; | |
162 | ||
163 | size_t maskn = 0; | |
164 | sstr_t *accessmask = sstrsplit(s, sstr(","), &maskn); | |
165 | for(int i=0;i<maskn;i++) { | |
166 | sstr_t access = accessmask[i]; | |
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
167 | ace->access_mask = ace->access_mask | accstr2int(access); |
51 | 168 | } |
169 | free(accessmask); | |
170 | n++; // next token | |
171 | ||
172 | /* | |
173 | * get flags (optional) and ace type | |
174 | */ | |
175 | ||
176 | int complete = 0; | |
177 | while(n < tkn) { | |
178 | s = tk[n]; | |
179 | if(!sstrcmp(s, sstr("allow"))) { | |
180 | ace->type = ACLCFG_TYPE_ALLOWED; | |
181 | complete = 1; | |
182 | break; | |
183 | } else if(!sstrcmp(s, sstr("deny"))) { | |
184 | ace->type = ACLCFG_TYPE_DENIED; | |
185 | complete = 1; | |
186 | break; | |
187 | } else if(!sstrcmp(s, sstr("audit"))) { | |
188 | ace->type = ACLCFG_TYPE_AUDIT; | |
189 | complete = 1; | |
190 | break; | |
191 | } else if(!sstrcmp(s, sstr("alarm"))) { | |
192 | ace->type = ACLCFG_TYPE_ALARM; | |
193 | complete = 1; | |
194 | break; | |
195 | } else { | |
196 | // set flags | |
197 | size_t fln = 0; | |
198 | sstr_t *flags = sstrsplit(s, sstr(","), &fln); | |
199 | for(int i=0;i<fln;i++) { | |
200 | sstr_t flag = flags[i]; | |
201 | if(!sstrcmp(flag, sstr("successful_access_flag"))) { | |
202 | ace->flags = ace->flags | ACLCFG_SUCCESSFUL_ACCESS_FLAG; | |
203 | } else if(!sstrcmp(flag, sstr("failed_access_flag"))) { | |
204 | ace->flags = ace->flags | ACLCFG_FAILED_ACCESS_ACE_FLAG; | |
205 | } | |
206 | // TODO: other flags | |
207 | } | |
208 | free(flags); | |
209 | } | |
210 | n++; | |
211 | } | |
212 | ||
213 | if(!complete) { | |
214 | fprintf(stderr, "parse_ace: ace incomplete\n"); | |
215 | return 1; | |
216 | } | |
217 | ||
218 | cur->entries = ucx_list_append(cur->entries, ace); | |
219 | ||
220 | return 0; | |
221 | } | |
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
222 | |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
223 | uint32_t accstr2int(sstr_t access) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
224 | uint32_t val = 0; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
225 | if(!sstrcmp(access, sstr("read"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
226 | val = ACLCFG_READ; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
227 | } else if(!sstrcmp(access, sstr("write"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
228 | val = ACLCFG_WRITE; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
229 | } else if(!sstrcmp(access, sstr("read_data"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
230 | val = ACLCFG_READ_DATA; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
231 | } else if(!sstrcmp(access, sstr("write_data"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
232 | val = ACLCFG_WRITE_DATA; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
233 | } else if(!sstrcmp(access, sstr("append"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
234 | val = ACLCFG_APPEND; |
56
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
52
diff
changeset
|
235 | } else if(!sstrcmp(access, sstr("add"))) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
52
diff
changeset
|
236 | val = ACLCFG_ADD_FILE; |
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
237 | } else if(!sstrcmp(access, sstr("add_file"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
238 | val = ACLCFG_ADD_FILE; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
239 | } else if(!sstrcmp(access, sstr("add_subdirectory"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
240 | val = ACLCFG_ADD_SUBDIRECTORY; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
241 | } else if(!sstrcmp(access, sstr("read_xattr"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
242 | val = ACLCFG_READ_XATTR; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
243 | } else if(!sstrcmp(access, sstr("write_xattr"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
244 | val = ACLCFG_WRITE_XATTR; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
245 | } else if(!sstrcmp(access, sstr("execute"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
246 | val = ACLCFG_EXECUTE; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
247 | } else if(!sstrcmp(access, sstr("delete_child"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
248 | val = ACLCFG_DELETE_CHILD; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
249 | } else if(!sstrcmp(access, sstr("delete"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
250 | val = ACLCFG_DELETE; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
251 | } else if(!sstrcmp(access, sstr("read_attributes"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
252 | val = ACLCFG_READ_ATTRIBUTES; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
253 | } else if(!sstrcmp(access, sstr("write_attributes"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
254 | val = ACLCFG_WRITE_ATTRIBUTES; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
255 | } else if(!sstrcmp(access, sstr("list"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
256 | val = ACLCFG_LIST; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
257 | } else if(!sstrcmp(access, sstr("read_acl"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
258 | val = ACLCFG_READ_ACL; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
259 | } else if(!sstrcmp(access, sstr("write_acl"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
260 | val = ACLCFG_WRITE_ACL; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
261 | } else if(!sstrcmp(access, sstr("write_owner"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
262 | val = ACLCFG_WRITE_OWNER; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
263 | } else if(!sstrcmp(access, sstr("synchronize"))) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
264 | val = ACLCFG_SYNCHRONIZE; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
265 | } |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
266 | return val; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
267 | } |