Fri, 01 Mar 2013 21:15:52 +0100
new pathcheck saf and code cleanup
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 "../util/pool.h" | |
33 | #include "../safs/auth.h" | |
34 | #include "acl.h" | |
35 | ||
36 | void acllist_createhandle(Session *sn, Request *rq) { | |
37 | ACLListHandle *handle = pool_malloc(sn->pool, sizeof(ACLListHandle)); | |
38 | handle->defaultauthdb = NULL; | |
39 | handle->listhead = NULL; | |
40 | handle->listtail = NULL; | |
41 | rq->acllist = handle; | |
42 | } | |
43 | ||
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
44 | /* |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
45 | * append or prepend an ACL |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
46 | */ |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
47 | void acllist_add(Session *sn, Request *rq, ACLList *acl, int append) { |
51 | 48 | if(!rq->acllist) { |
49 | acllist_createhandle(sn, rq); | |
50 | } | |
51 | ACLListHandle *list = rq->acllist; | |
52 | ||
53 | if(!list->defaultauthdb && acl->authdb) { | |
54 | list->defaultauthdb = acl->authdb; | |
55 | } | |
56 | ||
57 | ACLListElm *elm = pool_malloc(sn->pool, sizeof(ACLListElm)); | |
58 | elm->acl = acl; | |
59 | elm->next = NULL; | |
60 | if(list->listhead == NULL) { | |
61 | list->listhead = elm; | |
62 | list->listtail = elm; | |
63 | } else { | |
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
64 | if(append) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
65 | list->listtail->next = elm; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
66 | list->listtail = elm; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
67 | } else { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
68 | elm->next = list->listhead; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
69 | list->listhead = elm; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
70 | } |
51 | 71 | } |
72 | } | |
73 | ||
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
74 | void acllist_append(Session *sn, Request *rq, ACLList *acl) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
75 | acllist_add(sn, rq, acl, 1); |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
76 | } |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
77 | |
51 | 78 | void acllist_prepend(Session *sn, Request *rq, ACLList *acl) { |
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
79 | acllist_add(sn, rq, acl, 0); |
51 | 80 | } |
81 | ||
82 | ||
83 | int acl_evaluate(Session *sn, Request *rq, int access_mask) { | |
84 | ACLListHandle *list = rq->acllist; | |
85 | if(!list) { | |
86 | return REQ_PROCEED; | |
87 | } | |
88 | ||
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
89 | // we combine access_mask with the required access rights |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
90 | access_mask = access_mask | rq->aclreqaccess; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
91 | |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
92 | |
51 | 93 | // get user |
94 | User *user = NULL; | |
95 | if(list->defaultauthdb) { | |
96 | char *usr; | |
97 | char *pw; | |
98 | if(!basicauth_getuser(sn, rq, &usr, &pw)) { | |
99 | user = list->defaultauthdb->get_user(list->defaultauthdb, usr); | |
100 | if(!user) { | |
101 | // wrong user name | |
102 | return REQ_ABORTED; | |
103 | } | |
104 | if(!user->verify_password(user, pw)) { | |
105 | // wrong password | |
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
106 | user->free(user); |
51 | 107 | return REQ_ABORTED; |
108 | } | |
109 | // ok - user is authenticated | |
110 | } | |
111 | } else { | |
112 | // TODO | |
113 | return REQ_ABORTED; | |
114 | } | |
115 | ||
116 | // evaluate each acl until one denies access | |
117 | ACLListElm *elm = list->listhead; | |
118 | while(elm) { | |
119 | ACLList *acl = elm->acl; | |
120 | if(!wsacl_check(acl, user, access_mask)) { | |
121 | // the acl denies access | |
122 | ||
123 | if(!user) { | |
124 | pblock_nvinsert( | |
125 | "www-authenticate", | |
126 | "Basic realm=\"Webserver\"", | |
127 | rq->srvhdrs); | |
128 | protocol_status(sn, rq, PROTOCOL_UNAUTHORIZED, NULL); | |
129 | } | |
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
130 | user->free(user); |
51 | 131 | return REQ_ABORTED; |
132 | } | |
133 | elm = elm->next; | |
134 | } | |
135 | ||
136 | // ok - all acls allowed access | |
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
137 | user->free(user); |
51 | 138 | return REQ_PROCEED; |
139 | } | |
140 | ||
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
141 | int wsacl_affects_user(ACLEntry *ace, User *user) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
142 | int check_access = 0; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
143 | |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
144 | /* |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
145 | * an ace can affect |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
146 | * a named user or group (ace->who is set) |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
147 | * the owner of the resource (ACL_OWNER is set) |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
148 | * the owning group of the resource (ACL_GROUP is set) |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
149 | * everyone (ACL_EVERYONE is set) |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
150 | * |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
151 | * Only one of this conditions should be true. The behavior on |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
152 | * illegal flag combination is undefined. We assume that the acls |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
153 | * are created correctly by the configuration loader. |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
154 | */ |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
155 | |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
156 | if(ace->who && user) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
157 | // this ace is defined for a named user or group |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
158 | if((ace->flags & ACL_IDENTIFIER_GROUP) == ACL_IDENTIFIER_GROUP) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
159 | if(user->check_group(user, ace->who)) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
160 | // the user is in the group |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
161 | check_access = 1; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
162 | } |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
163 | } else { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
164 | if(!strcmp(user->name, ace->who)) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
165 | check_access = 1; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
166 | } |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
167 | } |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
168 | } else if((ace->flags & ACL_OWNER) == ACL_OWNER) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
169 | // TODO |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
170 | } else if((ace->flags & ACL_GROUP) == ACL_GROUP) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
171 | // TODO |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
172 | } else if((ace->flags & ACL_EVERYONE) == ACL_EVERYONE) { |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
173 | check_access = 1; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
174 | } |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
175 | |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
176 | return check_access; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
177 | } |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
178 | |
51 | 179 | int wsacl_check(ACLList *acl, User *user, int access_mask) { |
180 | int allow = 0; | |
181 | uint32_t allowed_access = 0; | |
182 | // check each access control entry | |
183 | for(int i=0;i<acl->acenum;i++) { | |
52
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
184 | ACLEntry *ace = acl->ace[i]; |
aced2245fb1c
new pathcheck saf and code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
185 | if(wsacl_affects_user(ace, user)) { |
51 | 186 | if(ace->type == ACL_TYPE_ALLOWED) { |
187 | // add all new access rights | |
188 | allowed_access = allowed_access | | |
189 | (access_mask & ace->access_mask); | |
190 | // check if we have all requested rights | |
191 | if((allowed_access & access_mask) == access_mask) { | |
192 | allow = 1; | |
193 | break; | |
194 | } | |
195 | } else { | |
196 | // ACL_TYPE_DENIED | |
197 | ||
198 | if((ace->access_mask & access_mask) != 0) { | |
199 | // access denied | |
200 | break; | |
201 | } | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
206 | // TODO: events | |
207 | ||
208 | return allow; | |
209 | } |