~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to source4/dsdb/samdb/ldb_modules/kludge_acl.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
   ldb database library
 
3
 
 
4
   Copyright (C) Andrew Bartlett 2005
 
5
   Copyright (C) Simo Sorce 2006-2008
 
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_module.h"
 
35
#include "auth/auth.h"
 
36
#include "libcli/security/security.h"
 
37
#include "dsdb/samdb/samdb.h"
 
38
 
 
39
/* Kludge ACL rules:
 
40
 *
 
41
 * - System can read passwords
 
42
 * - Administrators can write anything
 
43
 * - Users can read anything that is not a password
 
44
 *
 
45
 */
 
46
 
 
47
struct kludge_private_data {
 
48
        const char **password_attrs;
 
49
};
 
50
 
 
51
static enum security_user_level what_is_user(struct ldb_module *module) 
 
52
{
 
53
        struct ldb_context *ldb = ldb_module_get_ctx(module);
 
54
        struct auth_session_info *session_info
 
55
                = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
 
56
        return security_session_user_level(session_info);
 
57
}
 
58
 
 
59
static const char *user_name(TALLOC_CTX *mem_ctx, struct ldb_module *module) 
 
60
{
 
61
        struct ldb_context *ldb = ldb_module_get_ctx(module);
 
62
        struct auth_session_info *session_info
 
63
                = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
 
64
        if (!session_info) {
 
65
                return "UNKNOWN (NULL)";
 
66
        }
 
67
        
 
68
        return talloc_asprintf(mem_ctx, "%s\\%s",
 
69
                               session_info->server_info->domain_name,
 
70
                               session_info->server_info->account_name);
 
71
}
 
72
 
 
73
/* search */
 
74
struct kludge_acl_context {
 
75
 
 
76
        struct ldb_module *module;
 
77
        struct ldb_request *req;
 
78
 
 
79
        enum security_user_level user_type;
 
80
        bool allowedAttributes;
 
81
        bool allowedAttributesEffective;
 
82
        bool allowedChildClasses;
 
83
        bool allowedChildClassesEffective;
 
84
        const char * const *attrs;
 
85
};
 
86
 
 
87
/* read all objectClasses */
 
88
 
 
89
static int kludge_acl_allowedAttributes(struct ldb_context *ldb, struct ldb_message *msg,
 
90
                                        const char *attrName) 
 
91
{
 
92
        struct ldb_message_element *oc_el;
 
93
        struct ldb_message_element *allowedAttributes;
 
94
        const struct dsdb_schema *schema = dsdb_get_schema(ldb);
 
95
        TALLOC_CTX *mem_ctx;
 
96
        const char **objectclass_list, **attr_list;
 
97
        int i, ret;
 
98
 
 
99
        /* If we don't have a schema yet, we can't do anything... */
 
100
        if (schema == NULL) {
 
101
                return LDB_SUCCESS;
 
102
        }
 
103
 
 
104
        /* Must remove any existing attribute, or else confusion reins */
 
105
        ldb_msg_remove_attr(msg, attrName);
 
106
        ret = ldb_msg_add_empty(msg, attrName, 0, &allowedAttributes);
 
107
        if (ret != LDB_SUCCESS) {
 
108
                return ret;
 
109
        }
 
110
        
 
111
        mem_ctx = talloc_new(msg);
 
112
        if (!mem_ctx) {
 
113
                ldb_oom(ldb);
 
114
                return LDB_ERR_OPERATIONS_ERROR;
 
115
        }
 
116
 
 
117
        /* To ensure that oc_el is valid, we must look for it after 
 
118
           we alter the element array in ldb_msg_add_empty() */
 
119
        oc_el = ldb_msg_find_element(msg, "objectClass");
 
120
        
 
121
        objectclass_list = talloc_array(mem_ctx, const char *, oc_el->num_values + 1);
 
122
        if (!objectclass_list) {
 
123
                ldb_oom(ldb);
 
124
                talloc_free(mem_ctx);
 
125
                return LDB_ERR_OPERATIONS_ERROR;
 
126
        }
 
127
 
 
128
        for (i=0; oc_el && i < oc_el->num_values; i++) {
 
129
                objectclass_list[i] = (const char *)oc_el->values[i].data;
 
130
        }
 
131
        objectclass_list[i] = NULL;
 
132
 
 
133
        attr_list = dsdb_full_attribute_list(mem_ctx, schema, objectclass_list, DSDB_SCHEMA_ALL);
 
134
        if (!attr_list) {
 
135
                ldb_asprintf_errstring(ldb, "kludge_acl: Failed to get list of attributes create %s attribute", attrName);
 
136
                talloc_free(mem_ctx);
 
137
                return LDB_ERR_OPERATIONS_ERROR;
 
138
        }
 
139
 
 
140
        for (i=0; attr_list && attr_list[i]; i++) {
 
141
                ldb_msg_add_string(msg, attrName, attr_list[i]);
 
142
        }
 
143
        talloc_free(mem_ctx);
 
144
        return LDB_SUCCESS;
 
145
 
 
146
}
 
147
/* read all objectClasses */
 
148
 
 
149
static int kludge_acl_childClasses(struct ldb_context *ldb, struct ldb_message *msg,
 
150
                                   const char *attrName) 
 
151
{
 
152
        struct ldb_message_element *oc_el;
 
153
        struct ldb_message_element *allowedClasses;
 
154
        const struct dsdb_schema *schema = dsdb_get_schema(ldb);
 
155
        const struct dsdb_class *sclass;
 
156
        int i, j, ret;
 
157
 
 
158
        /* If we don't have a schema yet, we can't do anything... */
 
159
        if (schema == NULL) {
 
160
                return LDB_SUCCESS;
 
161
        }
 
162
 
 
163
        /* Must remove any existing attribute, or else confusion reins */
 
164
        ldb_msg_remove_attr(msg, attrName);
 
165
        ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
 
166
        if (ret != LDB_SUCCESS) {
 
167
                return ret;
 
168
        }
 
169
        
 
170
        /* To ensure that oc_el is valid, we must look for it after 
 
171
           we alter the element array in ldb_msg_add_empty() */
 
172
        oc_el = ldb_msg_find_element(msg, "objectClass");
 
173
 
 
174
        for (i=0; oc_el && i < oc_el->num_values; i++) {
 
175
                sclass = dsdb_class_by_lDAPDisplayName(schema, (const char *)oc_el->values[i].data);
 
176
                if (!sclass) {
 
177
                        /* We don't know this class?  what is going on? */
 
178
                        continue;
 
179
                }
 
180
 
 
181
                for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
 
182
                        ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
 
183
                }
 
184
        }
 
185
                
 
186
        if (allowedClasses->num_values > 1) {
 
187
                qsort(allowedClasses->values, 
 
188
                      allowedClasses->num_values, 
 
189
                      sizeof(*allowedClasses->values),
 
190
                      (comparison_fn_t)data_blob_cmp);
 
191
        
 
192
                for (i=1 ; i < allowedClasses->num_values; i++) {
 
193
 
 
194
                        struct ldb_val *val1 = &allowedClasses->values[i-1];
 
195
                        struct ldb_val *val2 = &allowedClasses->values[i];
 
196
                        if (data_blob_cmp(val1, val2) == 0) {
 
197
                                memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val)); 
 
198
                                allowedClasses->num_values--;
 
199
                                i--;
 
200
                        }
 
201
                }
 
202
        }
 
203
 
 
204
        return LDB_SUCCESS;
 
205
 
 
206
}
 
207
 
 
208
/* find all attributes allowed by all these objectClasses */
 
209
 
 
210
static int kludge_acl_callback(struct ldb_request *req, struct ldb_reply *ares)
 
211
{
 
212
        struct ldb_context *ldb;
 
213
        struct kludge_acl_context *ac;
 
214
        struct kludge_private_data *data;
 
215
        int i, ret;
 
216
 
 
217
        ac = talloc_get_type(req->context, struct kludge_acl_context);
 
218
        data = talloc_get_type(ldb_module_get_private(ac->module), struct kludge_private_data);
 
219
        ldb = ldb_module_get_ctx(ac->module);
 
220
 
 
221
        if (!ares) {
 
222
                return ldb_module_done(ac->req, NULL, NULL,
 
223
                                        LDB_ERR_OPERATIONS_ERROR);
 
224
        }
 
225
        if (ares->error != LDB_SUCCESS) {
 
226
                return ldb_module_done(ac->req, ares->controls,
 
227
                                        ares->response, ares->error);
 
228
        }
 
229
 
 
230
        switch (ares->type) {
 
231
        case LDB_REPLY_ENTRY:
 
232
                if (ac->allowedAttributes) {
 
233
                        ret = kludge_acl_allowedAttributes(ldb,
 
234
                                                   ares->message,
 
235
                                                   "allowedAttributes");
 
236
                        if (ret != LDB_SUCCESS) {
 
237
                                return ldb_module_done(ac->req, NULL, NULL, ret);
 
238
                        }
 
239
                }
 
240
                if (ac->allowedChildClasses) {
 
241
                        ret = kludge_acl_childClasses(ldb,
 
242
                                                ares->message,
 
243
                                                "allowedChildClasses");
 
244
                        if (ret != LDB_SUCCESS) {
 
245
                                return ldb_module_done(ac->req, NULL, NULL, ret);
 
246
                        }
 
247
                }
 
248
 
 
249
                if (data && data->password_attrs) /* if we are not initialized just get through */
 
250
                {
 
251
                        switch (ac->user_type) {
 
252
                        case SECURITY_SYSTEM:
 
253
                                if (ac->allowedAttributesEffective) {
 
254
                                        ret = kludge_acl_allowedAttributes(ldb, ares->message,
 
255
                                                                        "allowedAttributesEffective");
 
256
                                        if (ret != LDB_SUCCESS) {
 
257
                                                return ldb_module_done(ac->req, NULL, NULL, ret);
 
258
                                        }
 
259
                                }
 
260
                                if (ac->allowedChildClassesEffective) {
 
261
                                        ret = kludge_acl_childClasses(ldb, ares->message,
 
262
                                                                        "allowedChildClassesEffective");
 
263
                                        if (ret != LDB_SUCCESS) {
 
264
                                                return ldb_module_done(ac->req, NULL, NULL, ret);
 
265
                                        }
 
266
                                }
 
267
                                break;
 
268
 
 
269
                        case SECURITY_ADMINISTRATOR:
 
270
                                if (ac->allowedAttributesEffective) {
 
271
                                        ret = kludge_acl_allowedAttributes(ldb, ares->message,
 
272
                                                                        "allowedAttributesEffective");
 
273
                                        if (ret != LDB_SUCCESS) {
 
274
                                                return ldb_module_done(ac->req, NULL, NULL, ret);
 
275
                                        }
 
276
                                }
 
277
                                if (ac->allowedChildClassesEffective) {
 
278
                                        ret = kludge_acl_childClasses(ldb, ares->message,
 
279
                                                                        "allowedChildClassesEffective");
 
280
                                        if (ret != LDB_SUCCESS) {
 
281
                                                return ldb_module_done(ac->req, NULL, NULL, ret);
 
282
                                        }
 
283
                                }
 
284
                                /* fall through */
 
285
                        default:
 
286
                                /* remove password attributes */
 
287
                                for (i = 0; data->password_attrs[i]; i++) {
 
288
                                        ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
 
289
                                }
 
290
                        }
 
291
                }
 
292
 
 
293
                if (ac->allowedAttributes ||
 
294
                    ac->allowedAttributesEffective ||
 
295
                    ac->allowedChildClasses ||
 
296
                    ac->allowedChildClassesEffective) {
 
297
 
 
298
                        if (!ldb_attr_in_list(ac->attrs, "objectClass") &&
 
299
                            !ldb_attr_in_list(ac->attrs, "*")) {
 
300
 
 
301
                                ldb_msg_remove_attr(ares->message,
 
302
                                                    "objectClass");
 
303
                        }
 
304
                }
 
305
 
 
306
                return ldb_module_send_entry(ac->req, ares->message, ares->controls);
 
307
 
 
308
        case LDB_REPLY_REFERRAL:
 
309
                return ldb_module_send_referral(ac->req, ares->referral);
 
310
 
 
311
        case LDB_REPLY_DONE:
 
312
                return ldb_module_done(ac->req, ares->controls,
 
313
                                        ares->response, LDB_SUCCESS);
 
314
 
 
315
        }
 
316
        return LDB_SUCCESS;
 
317
}
 
318
 
 
319
static int kludge_acl_search(struct ldb_module *module, struct ldb_request *req)
 
320
{
 
321
        struct ldb_context *ldb;
 
322
        struct kludge_acl_context *ac;
 
323
        struct ldb_request *down_req;
 
324
        struct kludge_private_data *data;
 
325
        const char * const *attrs;
 
326
        int ret, i;
 
327
        struct ldb_control *sd_control;
 
328
        struct ldb_control **sd_saved_controls;
 
329
 
 
330
        ldb = ldb_module_get_ctx(module);
 
331
 
 
332
        ac = talloc(req, struct kludge_acl_context);
 
333
        if (ac == NULL) {
 
334
                ldb_oom(ldb);
 
335
                return LDB_ERR_OPERATIONS_ERROR;
 
336
        }
 
337
 
 
338
        data = talloc_get_type(ldb_module_get_private(module), struct kludge_private_data);
 
339
 
 
340
        ac->module = module;
 
341
        ac->req = req;
 
342
        ac->user_type = what_is_user(module);
 
343
        ac->attrs = req->op.search.attrs;
 
344
 
 
345
        ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
 
346
 
 
347
        ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
 
348
 
 
349
        ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
 
350
 
 
351
        ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
 
352
 
 
353
        if (ac->allowedAttributes || ac->allowedAttributesEffective || ac->allowedChildClasses || ac->allowedChildClassesEffective) {
 
354
                attrs = ldb_attr_list_copy_add(ac, req->op.search.attrs, "objectClass");
 
355
        } else {
 
356
                attrs = req->op.search.attrs;
 
357
        }
 
358
 
 
359
        /* replace any attributes in the parse tree that are private,
 
360
           so we don't allow a search for 'userPassword=penguin',
 
361
           just as we would not allow that attribute to be returned */
 
362
        switch (ac->user_type) {
 
363
        case SECURITY_SYSTEM:
 
364
                break;
 
365
        default:
 
366
        /* FIXME: We should copy the tree and keep the original unmodified. */
 
367
                /* remove password attributes */
 
368
 
 
369
                if (!data || !data->password_attrs) {
 
370
                        break;
 
371
                }
 
372
                for (i = 0; data->password_attrs[i]; i++) {
 
373
                        ldb_parse_tree_attr_replace(req->op.search.tree,
 
374
                                                    data->password_attrs[i],
 
375
                                                    "kludgeACLredactedattribute");
 
376
                }
 
377
        }
 
378
 
 
379
        ret = ldb_build_search_req_ex(&down_req,
 
380
                                        ldb, ac,
 
381
                                        req->op.search.base,
 
382
                                        req->op.search.scope,
 
383
                                        req->op.search.tree,
 
384
                                        attrs,
 
385
                                        req->controls,
 
386
                                        ac, kludge_acl_callback,
 
387
                                        req);
 
388
        if (ret != LDB_SUCCESS) {
 
389
                return LDB_ERR_OPERATIONS_ERROR;
 
390
        }
 
391
 
 
392
        /* check if there's an SD_FLAGS control */
 
393
        sd_control = ldb_request_get_control(down_req, LDB_CONTROL_SD_FLAGS_OID);
 
394
        if (sd_control) {
 
395
                /* save it locally and remove it from the list */
 
396
                /* we do not need to replace them later as we
 
397
                 * are keeping the original req intact */
 
398
                if (!save_controls(sd_control, down_req, &sd_saved_controls)) {
 
399
                        return LDB_ERR_OPERATIONS_ERROR;
 
400
                }
 
401
        }
 
402
 
 
403
        /* perform the search */
 
404
        return ldb_next_request(module, down_req);
 
405
}
 
406
 
 
407
/* ANY change type */
 
408
static int kludge_acl_change(struct ldb_module *module, struct ldb_request *req)
 
409
{
 
410
        struct ldb_context *ldb = ldb_module_get_ctx(module);
 
411
        enum security_user_level user_type = what_is_user(module);
 
412
        switch (user_type) {
 
413
        case SECURITY_SYSTEM:
 
414
        case SECURITY_ADMINISTRATOR:
 
415
                return ldb_next_request(module, req);
 
416
        default:
 
417
                ldb_asprintf_errstring(ldb,
 
418
                                       "kludge_acl_change: "
 
419
                                       "attempted database modify not permitted. "
 
420
                                       "User %s is not SYSTEM or an administrator",
 
421
                                       user_name(req, module));
 
422
                return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
 
423
        }
 
424
}
 
425
 
 
426
static int kludge_acl_extended(struct ldb_module *module, struct ldb_request *req)
 
427
{
 
428
        struct ldb_context *ldb = ldb_module_get_ctx(module);
 
429
        enum security_user_level user_type;
 
430
 
 
431
        /* allow everybody to read the sequence number */
 
432
        if (strcmp(req->op.extended.oid,
 
433
                   LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
 
434
                return ldb_next_request(module, req);
 
435
        }
 
436
 
 
437
        user_type = what_is_user(module);
 
438
 
 
439
        switch (user_type) {
 
440
        case SECURITY_SYSTEM:
 
441
        case SECURITY_ADMINISTRATOR:
 
442
                return ldb_next_request(module, req);
 
443
        default:
 
444
                ldb_asprintf_errstring(ldb,
 
445
                                       "kludge_acl_change: "
 
446
                                       "attempted database modify not permitted. "
 
447
                                       "User %s is not SYSTEM or an administrator",
 
448
                                       user_name(req, module));
 
449
                return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
 
450
        }
 
451
}
 
452
 
 
453
static int kludge_acl_init(struct ldb_module *module)
 
454
{
 
455
        struct ldb_context *ldb;
 
456
        int ret, i;
 
457
        TALLOC_CTX *mem_ctx = talloc_new(module);
 
458
        static const char *attrs[] = { "passwordAttribute", NULL };
 
459
        struct ldb_result *res;
 
460
        struct ldb_message *msg;
 
461
        struct ldb_message_element *password_attributes;
 
462
 
 
463
        struct kludge_private_data *data;
 
464
 
 
465
        ldb = ldb_module_get_ctx(module);
 
466
 
 
467
        data = talloc(module, struct kludge_private_data);
 
468
        if (data == NULL) {
 
469
                ldb_oom(ldb);
 
470
                return LDB_ERR_OPERATIONS_ERROR;
 
471
        }
 
472
 
 
473
        data->password_attrs = NULL;
 
474
        ldb_module_set_private(module, data);
 
475
 
 
476
        if (!mem_ctx) {
 
477
                ldb_oom(ldb);
 
478
                return LDB_ERR_OPERATIONS_ERROR;
 
479
        }
 
480
 
 
481
        ret = ldb_search(ldb, mem_ctx, &res,
 
482
                         ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
 
483
                         LDB_SCOPE_BASE, attrs, NULL);
 
484
        if (ret != LDB_SUCCESS) {
 
485
                goto done;
 
486
        }
 
487
        if (res->count == 0) {
 
488
                goto done;
 
489
        }
 
490
 
 
491
        if (res->count > 1) {
 
492
                talloc_free(mem_ctx);
 
493
                return LDB_ERR_CONSTRAINT_VIOLATION;
 
494
        }
 
495
 
 
496
        msg = res->msgs[0];
 
497
 
 
498
        password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
 
499
        if (!password_attributes) {
 
500
                goto done;
 
501
        }
 
502
        data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
 
503
        if (!data->password_attrs) {
 
504
                talloc_free(mem_ctx);
 
505
                ldb_oom(ldb);
 
506
                return LDB_ERR_OPERATIONS_ERROR;
 
507
        }
 
508
        for (i=0; i < password_attributes->num_values; i++) {
 
509
                data->password_attrs[i] = (const char *)password_attributes->values[i].data;    
 
510
                talloc_steal(data->password_attrs, password_attributes->values[i].data);
 
511
        }
 
512
        data->password_attrs[i] = NULL;
 
513
 
 
514
        ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
 
515
        if (ret != LDB_SUCCESS) {
 
516
                ldb_debug(ldb, LDB_DEBUG_ERROR,
 
517
                        "partition: Unable to register control with rootdse!\n");
 
518
                return LDB_ERR_OPERATIONS_ERROR;
 
519
        }
 
520
 
 
521
done:
 
522
        talloc_free(mem_ctx);
 
523
        return ldb_next_init(module);
 
524
}
 
525
 
 
526
_PUBLIC_ const struct ldb_module_ops ldb_kludge_acl_module_ops = {
 
527
        .name              = "kludge_acl",
 
528
        .search            = kludge_acl_search,
 
529
        .add               = kludge_acl_change,
 
530
        .modify            = kludge_acl_change,
 
531
        .del               = kludge_acl_change,
 
532
        .rename            = kludge_acl_change,
 
533
        .extended          = kludge_acl_extended,
 
534
        .init_context      = kludge_acl_init
 
535
};