r24010: Fix warning for the function paramter to qsort().
[ira/wip.git] / source4 / dsdb / samdb / ldb_modules / kludge_acl.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Bartlett 2005
5    Copyright (C) Simo Sorce 2006
6
7     This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22  *  Name: ldb
23  *
24  *  Component: ldb kludge ACL module
25  *
26  *  Description: Simple module to enforce a simple form of access
27  *               control, sufficient for securing a default Samba4 
28  *               installation.
29  *
30  *  Author: Andrew Bartlett
31  */
32
33 #include "includes.h"
34 #include "ldb/include/ldb.h"
35 #include "ldb/include/ldb_errors.h"
36 #include "ldb/include/ldb_private.h"
37 #include "auth/auth.h"
38 #include "libcli/security/security.h"
39 #include "dsdb/samdb/samdb.h"
40
41 /* Kludge ACL rules:
42  *
43  * - System can read passwords
44  * - Administrators can write anything
45  * - Users can read anything that is not a password
46  *
47  */
48
49 enum user_is {
50         ANONYMOUS,
51         USER,
52         ADMINISTRATOR,
53         SYSTEM
54 };
55
56 struct kludge_private_data {
57         const char **password_attrs;
58 };
59
60 static enum user_is what_is_user(struct ldb_module *module) 
61 {
62         struct auth_session_info *session_info
63                 = ldb_get_opaque(module->ldb, "sessionInfo");
64         if (!session_info) {
65                 return ANONYMOUS;
66         }
67         
68         if (security_token_is_system(session_info->security_token)) {
69                 return SYSTEM;
70         }
71
72         if (security_token_is_anonymous(session_info->security_token)) {
73                 return ANONYMOUS;
74         }
75
76         if (security_token_has_builtin_administrators(session_info->security_token)) {
77                 return ADMINISTRATOR;
78         }
79
80         if (security_token_has_nt_authenticated_users(session_info->security_token)) {
81                 return USER;
82         }
83
84         return ANONYMOUS;
85 }
86
87 static const char *user_name(TALLOC_CTX *mem_ctx, struct ldb_module *module) 
88 {
89         struct auth_session_info *session_info
90                 = ldb_get_opaque(module->ldb, "sessionInfo");
91         if (!session_info) {
92                 return "UNKNOWN (NULL)";
93         }
94         
95         return talloc_asprintf(mem_ctx, "%s\\%s",
96                                session_info->server_info->domain_name,
97                                session_info->server_info->account_name);
98 }
99
100 /* search */
101 struct kludge_acl_context {
102
103         struct ldb_module *module;
104         void *up_context;
105         int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
106
107         enum user_is user_type;
108         bool allowedAttributes;
109         bool allowedAttributesEffective;
110         const char **attrs;
111 };
112
113 /* read all objectClasses */
114
115 static int kludge_acl_allowedAttributes(struct ldb_context *ldb, struct ldb_message *msg,
116                                                          const char *attrName) 
117 {
118         struct ldb_message_element *oc_el;
119         struct ldb_message_element *allowedAttributes;
120         const struct dsdb_schema *schema = dsdb_get_schema(ldb);
121         const struct dsdb_class *class;
122         int i, j, ret;
123         ret = ldb_msg_add_empty(msg, attrName, 0, &allowedAttributes);
124         if (ret != LDB_SUCCESS) {
125                 return ret;
126         }
127         
128         /* To ensure that oc_el is valid, we must look for it after 
129            we alter the element array in ldb_msg_add_empty() */
130         oc_el = ldb_msg_find_element(msg, "objectClass");
131
132         for (i=0; i < oc_el->num_values; i++) {
133                 class = dsdb_class_by_lDAPDisplayName(schema, (const char *)oc_el->values[i].data);
134                 if (!class) {
135                         /* We don't know this class?  what is going on? */
136                         continue;
137                 }
138                 for (j=0; class->mayContain && class->mayContain[j]; j++) {
139                         ldb_msg_add_string(msg, attrName, class->mayContain[j]);
140                 }
141                 for (j=0; class->mustContain && class->mustContain[j]; j++) {
142                         ldb_msg_add_string(msg, attrName, class->mustContain[j]);
143                 }
144                 for (j=0; class->systemMayContain && class->systemMayContain[j]; j++) {
145                         ldb_msg_add_string(msg, attrName, class->systemMayContain[j]);
146                 }
147                 for (j=0; class->systemMustContain && class->systemMustContain[j]; j++) {
148                         ldb_msg_add_string(msg, attrName, class->systemMustContain[j]);
149                 }
150         }
151                 
152         if (allowedAttributes->num_values > 1) {
153                 qsort(allowedAttributes->values, 
154                       allowedAttributes->num_values, 
155                       sizeof(*allowedAttributes->values),
156                       (comparison_fn_t)data_blob_cmp);
157         
158                 for (i=1 ; i < allowedAttributes->num_values; i++) {
159                         struct ldb_val *val1 = &allowedAttributes->values[i-1];
160                         struct ldb_val *val2 = &allowedAttributes->values[i];
161                         if (data_blob_cmp(val1, val2) == 0) {
162                                 memmove(val1, val2, (allowedAttributes->num_values - i) * sizeof( struct ldb_val)); 
163                                 allowedAttributes->num_values--;
164                                 i--;
165                         }
166                 }
167         }
168
169         return 0;
170
171 }
172
173 /* find all attributes allowed by all these objectClasses */
174
175 static int kludge_acl_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
176 {
177         struct kludge_acl_context *ac;
178         struct kludge_private_data *data;
179         int i, ret;
180
181         if (!context || !ares) {
182                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
183                 goto error;
184         }
185
186         ac = talloc_get_type(context, struct kludge_acl_context);
187         data = talloc_get_type(ac->module->private_data, struct kludge_private_data);
188
189         if (ares->type != LDB_REPLY_ENTRY) {
190                 return ac->up_callback(ldb, ac->up_context, ares);
191         }
192
193         if (ac->allowedAttributes) {
194                 ret = kludge_acl_allowedAttributes(ldb, ares->message, "allowedAttributes");
195                 if (ret != LDB_SUCCESS) {
196                         return ret;
197                 }
198         }
199
200         if (data && data->password_attrs) /* if we are not initialized just get through */
201         {
202                 switch (ac->user_type) {
203                 case SYSTEM:
204                 case ADMINISTRATOR:
205                         if (ac->allowedAttributesEffective) {
206                                 ret = kludge_acl_allowedAttributes(ldb, ares->message, "allowedAttributesEffective");
207                                 if (ret != LDB_SUCCESS) {
208                                         return ret;
209                                 }
210                         }
211                         break;
212                 default:
213                         /* remove password attributes */
214                         for (i = 0; data->password_attrs[i]; i++) {
215                                 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
216                         }
217                 }
218         }
219
220         if ((ac->allowedAttributes || ac->allowedAttributesEffective) && 
221             (!ldb_attr_in_list(ac->attrs, "objectClass") && 
222              !ldb_attr_in_list(ac->attrs, "*"))) {
223                 ldb_msg_remove_attr(ares->message, "objectClass");
224         }
225
226         return ac->up_callback(ldb, ac->up_context, ares);
227
228 error:
229         talloc_free(ares);
230         return LDB_ERR_OPERATIONS_ERROR;
231 }
232
233 static int kludge_acl_search(struct ldb_module *module, struct ldb_request *req)
234 {
235         struct kludge_acl_context *ac;
236         struct ldb_request *down_req;
237         struct kludge_private_data *data;
238         int ret, i;
239
240         req->handle = NULL;
241
242         ac = talloc(req, struct kludge_acl_context);
243         if (ac == NULL) {
244                 return LDB_ERR_OPERATIONS_ERROR;
245         }
246
247         data = talloc_get_type(module->private_data, struct kludge_private_data);
248
249         ac->module = module;
250         ac->up_context = req->context;
251         ac->up_callback = req->callback;
252         ac->user_type = what_is_user(module);
253         ac->attrs = req->op.search.attrs;
254
255         down_req = talloc_zero(req, struct ldb_request);
256         if (down_req == NULL) {
257                 return LDB_ERR_OPERATIONS_ERROR;
258         }
259
260         down_req->operation = req->operation;
261         down_req->op.search.base = req->op.search.base;
262         down_req->op.search.scope = req->op.search.scope;
263         down_req->op.search.tree = req->op.search.tree;
264         down_req->op.search.attrs = req->op.search.attrs;
265
266         ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
267
268         ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
269
270         if (ac->allowedAttributes || ac->allowedAttributesEffective) {
271                 down_req->op.search.attrs
272                         = ldb_attr_list_copy_add(down_req, down_req->op.search.attrs, "objectClass");
273         }
274
275         /*  FIXME: I hink we should copy the tree and keep the original
276          *  unmodified. SSS */
277         /* replace any attributes in the parse tree that are private,
278            so we don't allow a search for 'sambaPassword=penguin',
279            just as we would not allow that attribute to be returned */
280         switch (ac->user_type) {
281         case SYSTEM:
282                 break;
283         default:
284                 /* remove password attributes */
285                 for (i = 0; data && data->password_attrs && data->password_attrs[i]; i++) {
286                         ldb_parse_tree_attr_replace(down_req->op.search.tree, 
287                                                     data->password_attrs[i],
288                                                     "kludgeACLredactedattribute");
289                 }
290         }
291
292         down_req->controls = req->controls;
293
294         down_req->context = ac;
295         down_req->callback = kludge_acl_callback;
296         ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
297
298         /* perform the search */
299         ret = ldb_next_request(module, down_req);
300
301         /* do not free down_req as the call results may be linked to it,
302          * it will be freed when the upper level request get freed */
303         if (ret == LDB_SUCCESS) {
304                 req->handle = down_req->handle;
305         }
306
307         return ret;
308 }
309
310 /* ANY change type */
311 static int kludge_acl_change(struct ldb_module *module, struct ldb_request *req)
312 {
313         enum user_is user_type = what_is_user(module);
314         switch (user_type) {
315         case SYSTEM:
316         case ADMINISTRATOR:
317                 return ldb_next_request(module, req);
318         default:
319                 ldb_asprintf_errstring(module->ldb,
320                                        "kludge_acl_change: "
321                                        "attempted database modify not permitted. "
322                                        "User %s is not SYSTEM or an administrator",
323                                        user_name(req, module));
324                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
325         }
326 }
327
328 static int kludge_acl_init(struct ldb_module *module)
329 {
330         int ret, i;
331         TALLOC_CTX *mem_ctx = talloc_new(module);
332         static const char *attrs[] = { "passwordAttribute", NULL };
333         struct ldb_result *res;
334         struct ldb_message *msg;
335         struct ldb_message_element *password_attributes;
336
337         struct kludge_private_data *data;
338
339         data = talloc(module, struct kludge_private_data);
340         if (data == NULL) {
341                 return LDB_ERR_OPERATIONS_ERROR;
342         }
343
344         data->password_attrs = NULL;
345         module->private_data = data;
346
347         if (!mem_ctx) {
348                 return LDB_ERR_OPERATIONS_ERROR;
349         }
350
351         ret = ldb_search(module->ldb, ldb_dn_new(mem_ctx, module->ldb, "@KLUDGEACL"),
352                          LDB_SCOPE_BASE,
353                          NULL, attrs,
354                          &res);
355         if (ret != LDB_SUCCESS) {
356                 goto done;
357         }
358         talloc_steal(mem_ctx, res);
359         if (res->count == 0) {
360                 goto done;
361         }
362
363         if (res->count > 1) {
364                 talloc_free(mem_ctx);
365                 return LDB_ERR_CONSTRAINT_VIOLATION;
366         }
367
368         msg = res->msgs[0];
369
370         password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
371         if (!password_attributes) {
372                 goto done;
373         }
374         data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
375         if (!data->password_attrs) {
376                 talloc_free(mem_ctx);
377                 return LDB_ERR_OPERATIONS_ERROR;
378         }
379         for (i=0; i < password_attributes->num_values; i++) {
380                 data->password_attrs[i] = (const char *)password_attributes->values[i].data;    
381                 talloc_steal(data->password_attrs, password_attributes->values[i].data);
382         }
383         data->password_attrs[i] = NULL;
384
385 done:
386         talloc_free(mem_ctx);
387         return ldb_next_init(module);
388 }
389
390 static const struct ldb_module_ops kludge_acl_ops = {
391         .name              = "kludge_acl",
392         .search            = kludge_acl_search,
393         .add               = kludge_acl_change,
394         .modify            = kludge_acl_change,
395         .del               = kludge_acl_change,
396         .rename            = kludge_acl_change,
397         .extended          = kludge_acl_change,
398         .init_context      = kludge_acl_init
399 };
400
401 int ldb_kludge_acl_init(void)
402 {
403         return ldb_register_module(&kludge_acl_ops);
404 }