2d6d8a2d80196c93434246965ead07d539290c2d
[jelmer/samba4-debian.git] / source / 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         bool allowedChildClasses;
111         bool allowedChildClassesEffective;
112         const char **attrs;
113 };
114
115 /* read all objectClasses */
116
117 static int kludge_acl_allowedAttributes(struct ldb_context *ldb, struct ldb_message *msg,
118                                         const char *attrName) 
119 {
120         struct ldb_message_element *oc_el;
121         struct ldb_message_element *allowedAttributes;
122         const struct dsdb_schema *schema = dsdb_get_schema(ldb);
123         const struct dsdb_class *class;
124         int i, j, ret;
125         /* Must remove any existing attribute, or else confusion reins */
126         ldb_msg_remove_attr(msg, attrName);
127         ret = ldb_msg_add_empty(msg, attrName, 0, &allowedAttributes);
128         if (ret != LDB_SUCCESS) {
129                 return ret;
130         }
131         
132         /* To ensure that oc_el is valid, we must look for it after 
133            we alter the element array in ldb_msg_add_empty() */
134         oc_el = ldb_msg_find_element(msg, "objectClass");
135
136         for (i=0; oc_el && i < oc_el->num_values; i++) {
137                 class = dsdb_class_by_lDAPDisplayName(schema, (const char *)oc_el->values[i].data);
138                 if (!class) {
139                         /* We don't know this class?  what is going on? */
140                         continue;
141                 }
142
143                 for (j=0; class->mayContain && class->mayContain[j]; j++) {
144                         ldb_msg_add_string(msg, attrName, class->mayContain[j]);
145                 }
146                 for (j=0; class->mustContain && class->mustContain[j]; j++) {
147                         ldb_msg_add_string(msg, attrName, class->mustContain[j]);
148                 }
149                 for (j=0; class->systemMayContain && class->systemMayContain[j]; j++) {
150                         ldb_msg_add_string(msg, attrName, class->systemMayContain[j]);
151                 }
152                 for (j=0; class->systemMustContain && class->systemMustContain[j]; j++) {
153                         ldb_msg_add_string(msg, attrName, class->systemMustContain[j]);
154                 }
155         }
156                 
157         if (allowedAttributes->num_values > 1) {
158                 qsort(allowedAttributes->values, 
159                       allowedAttributes->num_values, 
160                       sizeof(*allowedAttributes->values),
161                       (comparison_fn_t)data_blob_cmp);
162         
163                 for (i=1 ; i < allowedAttributes->num_values; i++) {
164                         struct ldb_val *val1 = &allowedAttributes->values[i-1];
165                         struct ldb_val *val2 = &allowedAttributes->values[i];
166                         if (data_blob_cmp(val1, val2) == 0) {
167                                 memmove(val1, val2, (allowedAttributes->num_values - i) * sizeof( struct ldb_val)); 
168                                 allowedAttributes->num_values--;
169                                 i--;
170                         }
171                 }
172         }
173
174         return 0;
175
176 }
177 /* read all objectClasses */
178
179 static int kludge_acl_childClasses(struct ldb_context *ldb, struct ldb_message *msg,
180                                    const char *attrName) 
181 {
182         struct ldb_message_element *oc_el;
183         struct ldb_message_element *allowedClasses;
184         const struct dsdb_schema *schema = dsdb_get_schema(ldb);
185         const struct dsdb_class *class;
186         int i, j, ret;
187         /* Must remove any existing attribute, or else confusion reins */
188         ldb_msg_remove_attr(msg, attrName);
189         ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
190         if (ret != LDB_SUCCESS) {
191                 return ret;
192         }
193         
194         /* To ensure that oc_el is valid, we must look for it after 
195            we alter the element array in ldb_msg_add_empty() */
196         oc_el = ldb_msg_find_element(msg, "objectClass");
197
198         for (i=0; oc_el && i < oc_el->num_values; i++) {
199                 class = dsdb_class_by_lDAPDisplayName(schema, (const char *)oc_el->values[i].data);
200                 if (!class) {
201                         /* We don't know this class?  what is going on? */
202                         continue;
203                 }
204
205                 for (j=0; class->possibleInferiors && class->possibleInferiors[j]; j++) {
206                         ldb_msg_add_string(msg, attrName, class->possibleInferiors[j]);
207                 }
208         }
209                 
210         if (allowedClasses->num_values > 1) {
211                 qsort(allowedClasses->values, 
212                       allowedClasses->num_values, 
213                       sizeof(*allowedClasses->values),
214                       (comparison_fn_t)data_blob_cmp);
215         
216                 for (i=1 ; i < allowedClasses->num_values; i++) {
217                         struct ldb_val *val1 = &allowedClasses->values[i-1];
218                         struct ldb_val *val2 = &allowedClasses->values[i];
219                         if (data_blob_cmp(val1, val2) == 0) {
220                                 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val)); 
221                                 allowedClasses->num_values--;
222                                 i--;
223                         }
224                 }
225         }
226
227         return 0;
228
229 }
230
231 /* find all attributes allowed by all these objectClasses */
232
233 static int kludge_acl_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
234 {
235         struct kludge_acl_context *ac;
236         struct kludge_private_data *data;
237         int i, ret;
238
239         ac = talloc_get_type(context, struct kludge_acl_context);
240         data = talloc_get_type(ac->module->private_data, struct kludge_private_data);
241
242         if (ares->type != LDB_REPLY_ENTRY) {
243                 return ac->up_callback(ldb, ac->up_context, ares);
244         }
245
246         if (ac->allowedAttributes) {
247                 ret = kludge_acl_allowedAttributes(ldb, ares->message, "allowedAttributes");
248                 if (ret != LDB_SUCCESS) {
249                         return ret;
250
251                 }
252         }
253         if (ac->allowedChildClasses) {
254                 ret = kludge_acl_childClasses(ldb, ares->message, "allowedChildClasses");
255                 if (ret != LDB_SUCCESS) {
256                         return ret;
257                 }
258         }
259
260         if (data && data->password_attrs) /* if we are not initialized just get through */
261         {
262                 switch (ac->user_type) {
263                 case SYSTEM:
264                 case ADMINISTRATOR:
265                         if (ac->allowedAttributesEffective) {
266                                 ret = kludge_acl_allowedAttributes(ldb, ares->message, "allowedAttributesEffective");
267                                 if (ret != LDB_SUCCESS) {
268                                         return ret;
269                                 }
270                         }
271                         if (ac->allowedChildClassesEffective) {
272                                 ret = kludge_acl_childClasses(ldb, ares->message, "allowedChildClassesEffective");
273                                 if (ret != LDB_SUCCESS) {
274                                         return ret;
275                                 }
276                         }
277                         break;
278                 default:
279                         /* remove password attributes */
280                         for (i = 0; data->password_attrs[i]; i++) {
281                                 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
282                         }
283                 }
284         }
285
286         if ((ac->allowedAttributes || ac->allowedAttributesEffective
287              || ac->allowedChildClasses || ac->allowedChildClassesEffective) && 
288             (!ldb_attr_in_list(ac->attrs, "objectClass") && 
289              !ldb_attr_in_list(ac->attrs, "*"))) {
290                 ldb_msg_remove_attr(ares->message, "objectClass");
291         }
292
293         return ac->up_callback(ldb, ac->up_context, ares);
294 }
295
296 static int kludge_acl_search(struct ldb_module *module, struct ldb_request *req)
297 {
298         struct kludge_acl_context *ac;
299         struct ldb_request *down_req;
300         struct kludge_private_data *data;
301         int ret, i;
302
303         req->handle = NULL;
304
305         ac = talloc(req, struct kludge_acl_context);
306         if (ac == NULL) {
307                 return LDB_ERR_OPERATIONS_ERROR;
308         }
309
310         data = talloc_get_type(module->private_data, struct kludge_private_data);
311
312         ac->module = module;
313         ac->up_context = req->context;
314         ac->up_callback = req->callback;
315         ac->user_type = what_is_user(module);
316         ac->attrs = req->op.search.attrs;
317
318         down_req = talloc_zero(req, struct ldb_request);
319         if (down_req == NULL) {
320                 return LDB_ERR_OPERATIONS_ERROR;
321         }
322
323         down_req->operation = req->operation;
324         down_req->op.search.base = req->op.search.base;
325         down_req->op.search.scope = req->op.search.scope;
326         down_req->op.search.tree = req->op.search.tree;
327         down_req->op.search.attrs = req->op.search.attrs;
328
329         ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
330
331         ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
332
333         ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
334
335         ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
336
337         if (ac->allowedAttributes || ac->allowedAttributesEffective || ac->allowedChildClasses || ac->allowedChildClassesEffective) {
338                 down_req->op.search.attrs
339                         = ldb_attr_list_copy_add(down_req, down_req->op.search.attrs, "objectClass");
340         }
341
342         /*  FIXME: I hink we should copy the tree and keep the original
343          *  unmodified. SSS */
344         /* replace any attributes in the parse tree that are private,
345            so we don't allow a search for 'sambaPassword=penguin',
346            just as we would not allow that attribute to be returned */
347         switch (ac->user_type) {
348         case SYSTEM:
349                 break;
350         default:
351                 /* remove password attributes */
352                 for (i = 0; data && data->password_attrs && data->password_attrs[i]; i++) {
353                         ldb_parse_tree_attr_replace(down_req->op.search.tree, 
354                                                     data->password_attrs[i],
355                                                     "kludgeACLredactedattribute");
356                 }
357         }
358
359         down_req->controls = req->controls;
360
361         down_req->context = ac;
362         down_req->callback = kludge_acl_callback;
363         ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
364
365         /* perform the search */
366         ret = ldb_next_request(module, down_req);
367
368         /* do not free down_req as the call results may be linked to it,
369          * it will be freed when the upper level request get freed */
370         if (ret == LDB_SUCCESS) {
371                 req->handle = down_req->handle;
372         }
373
374         return ret;
375 }
376
377 /* ANY change type */
378 static int kludge_acl_change(struct ldb_module *module, struct ldb_request *req)
379 {
380         enum user_is user_type = what_is_user(module);
381         switch (user_type) {
382         case SYSTEM:
383         case ADMINISTRATOR:
384                 return ldb_next_request(module, req);
385         default:
386                 ldb_asprintf_errstring(module->ldb,
387                                        "kludge_acl_change: "
388                                        "attempted database modify not permitted. "
389                                        "User %s is not SYSTEM or an administrator",
390                                        user_name(req, module));
391                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
392         }
393 }
394
395 static int kludge_acl_init(struct ldb_module *module)
396 {
397         int ret, i;
398         TALLOC_CTX *mem_ctx = talloc_new(module);
399         static const char *attrs[] = { "passwordAttribute", NULL };
400         struct ldb_result *res;
401         struct ldb_message *msg;
402         struct ldb_message_element *password_attributes;
403
404         struct kludge_private_data *data;
405
406         data = talloc(module, struct kludge_private_data);
407         if (data == NULL) {
408                 return LDB_ERR_OPERATIONS_ERROR;
409         }
410
411         data->password_attrs = NULL;
412         module->private_data = data;
413
414         if (!mem_ctx) {
415                 return LDB_ERR_OPERATIONS_ERROR;
416         }
417
418         ret = ldb_search(module->ldb, ldb_dn_new(mem_ctx, module->ldb, "@KLUDGEACL"),
419                          LDB_SCOPE_BASE,
420                          NULL, attrs,
421                          &res);
422         if (ret != LDB_SUCCESS) {
423                 goto done;
424         }
425         talloc_steal(mem_ctx, res);
426         if (res->count == 0) {
427                 goto done;
428         }
429
430         if (res->count > 1) {
431                 talloc_free(mem_ctx);
432                 return LDB_ERR_CONSTRAINT_VIOLATION;
433         }
434
435         msg = res->msgs[0];
436
437         password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
438         if (!password_attributes) {
439                 goto done;
440         }
441         data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
442         if (!data->password_attrs) {
443                 talloc_free(mem_ctx);
444                 return LDB_ERR_OPERATIONS_ERROR;
445         }
446         for (i=0; i < password_attributes->num_values; i++) {
447                 data->password_attrs[i] = (const char *)password_attributes->values[i].data;    
448                 talloc_steal(data->password_attrs, password_attributes->values[i].data);
449         }
450         data->password_attrs[i] = NULL;
451
452 done:
453         talloc_free(mem_ctx);
454         return ldb_next_init(module);
455 }
456
457 static const struct ldb_module_ops kludge_acl_ops = {
458         .name              = "kludge_acl",
459         .search            = kludge_acl_search,
460         .add               = kludge_acl_change,
461         .modify            = kludge_acl_change,
462         .del               = kludge_acl_change,
463         .rename            = kludge_acl_change,
464         .extended          = kludge_acl_change,
465         .init_context      = kludge_acl_init
466 };
467
468 int ldb_kludge_acl_init(void)
469 {
470         return ldb_register_module(&kludge_acl_ops);
471 }