s3-pylibsmb: Factor out py_tevent_cond_wait
[Samba/gebeck_regimport.git] / source4 / dsdb / samdb / ldb_modules / schema_data.c
blob3e0bb9c9c47585701b4e20a11870de21754e3bed
1 /*
2 Unix SMB/CIFS mplementation.
4 The module that handles the Schema checkings and dynamic attributes
6 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "ldb_module.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 #include "param/param.h"
31 #include "dsdb/samdb/ldb_modules/util.h"
33 static int generate_objectClasses(struct ldb_context *ldb, struct ldb_message *msg,
34 const struct dsdb_schema *schema);
35 static int generate_attributeTypes(struct ldb_context *ldb, struct ldb_message *msg,
36 const struct dsdb_schema *schema);
37 static int generate_dITContentRules(struct ldb_context *ldb, struct ldb_message *msg,
38 const struct dsdb_schema *schema);
39 static int generate_extendedAttributeInfo(struct ldb_context *ldb, struct ldb_message *msg,
40 const struct dsdb_schema *schema);
41 static int generate_extendedClassInfo(struct ldb_context *ldb, struct ldb_message *msg,
42 const struct dsdb_schema *schema);
43 static int generate_possibleInferiors(struct ldb_context *ldb, struct ldb_message *msg,
44 const struct dsdb_schema *schema);
46 static const struct {
47 const char *attr;
48 int (*fn)(struct ldb_context *, struct ldb_message *, const struct dsdb_schema *);
49 bool aggregate;
50 } generated_attrs[] = {
52 .attr = "objectClasses",
53 .fn = generate_objectClasses,
54 .aggregate = true,
57 .attr = "attributeTypes",
58 .fn = generate_attributeTypes,
59 .aggregate = true,
62 .attr = "dITContentRules",
63 .fn = generate_dITContentRules,
64 .aggregate = true,
67 .attr = "extendedAttributeInfo",
68 .fn = generate_extendedAttributeInfo,
69 .aggregate = true,
72 .attr = "extendedClassInfo",
73 .fn = generate_extendedClassInfo,
74 .aggregate = true,
77 .attr = "possibleInferiors",
78 .fn = generate_possibleInferiors,
79 .aggregate = false,
83 struct schema_data_private_data {
84 struct ldb_dn *aggregate_dn;
85 struct ldb_dn *schema_dn;
88 struct schema_data_search_data {
89 struct ldb_module *module;
90 struct ldb_request *req;
92 const struct dsdb_schema *schema;
95 static int schema_data_init(struct ldb_module *module)
97 struct ldb_context *ldb;
98 struct ldb_dn *schema_dn;
99 int ret;
100 struct schema_data_private_data *data;
102 ret = ldb_next_init(module);
103 if (ret != LDB_SUCCESS) {
104 return ret;
107 ldb = ldb_module_get_ctx(module);
108 schema_dn = ldb_get_schema_basedn(ldb);
109 if (!schema_dn) {
110 ldb_reset_err_string(ldb);
111 ldb_debug(ldb, LDB_DEBUG_WARNING,
112 "schema_data_init: no schema dn present: (skip schema loading)\n");
113 return LDB_SUCCESS;
116 data = talloc(module, struct schema_data_private_data);
117 if (data == NULL) {
118 return ldb_oom(ldb);
121 data->schema_dn = schema_dn;
123 /* Used to check to see if this is a result on the CN=Aggregate schema */
124 data->aggregate_dn = samdb_aggregate_schema_dn(ldb, data);
125 if (!data->aggregate_dn) {
126 ldb_asprintf_errstring(ldb, "schema_data_init: Could not build aggregate schema DN for schema in %s", ldb_dn_get_linearized(schema_dn));
127 return LDB_ERR_OPERATIONS_ERROR;
130 ldb_module_set_private(module, data);
131 return LDB_SUCCESS;
134 static int schema_data_add(struct ldb_module *module, struct ldb_request *req)
136 struct ldb_context *ldb;
137 struct dsdb_schema *schema;
138 const struct ldb_val *attributeID = NULL;
139 const struct ldb_val *governsID = NULL;
140 const char *oid_attr = NULL;
141 const char *oid = NULL;
142 struct ldb_dn *parent_dn = NULL;
143 int cmp;
144 WERROR status;
145 bool rodc = false;
146 int ret;
148 ldb = ldb_module_get_ctx(module);
150 /* special objects should always go through */
151 if (ldb_dn_is_special(req->op.add.message->dn)) {
152 return ldb_next_request(module, req);
155 /* replicated update should always go through */
156 if (ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
157 return ldb_next_request(module, req);
160 schema = dsdb_get_schema(ldb, req);
161 if (!schema) {
162 return ldb_next_request(module, req);
165 if (schema->base_dn == NULL) {
166 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
167 "schema_data_add: base_dn NULL\n");
168 return LDB_ERR_OPERATIONS_ERROR;
171 ret = samdb_rodc(ldb, &rodc);
172 if (ret != LDB_SUCCESS) {
173 DEBUG(4, (__location__ ": unable to tell if we are an RODC \n"));
176 if (!schema->fsmo.we_are_master && !rodc) {
177 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
178 "schema_data_add: we are not master: reject request\n");
179 return LDB_ERR_UNWILLING_TO_PERFORM;
182 if (!schema->fsmo.update_allowed && !rodc) {
183 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
184 "schema_data_add: updates are not allowed: reject request\n");
185 return LDB_ERR_UNWILLING_TO_PERFORM;
188 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID)) {
190 * the provision code needs to create
191 * the schema root object.
193 cmp = ldb_dn_compare(req->op.add.message->dn, schema->base_dn);
194 if (cmp == 0) {
195 return ldb_next_request(module, req);
199 parent_dn = ldb_dn_get_parent(req, req->op.add.message->dn);
200 if (!parent_dn) {
201 return ldb_oom(ldb);
204 cmp = ldb_dn_compare(parent_dn, schema->base_dn);
205 if (cmp != 0) {
206 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
207 "schema_data_add: no direct child :%s\n",
208 ldb_dn_get_linearized(req->op.add.message->dn));
209 return LDB_ERR_UNWILLING_TO_PERFORM;
212 attributeID = ldb_msg_find_ldb_val(req->op.add.message, "attributeID");
213 governsID = ldb_msg_find_ldb_val(req->op.add.message, "governsID");
215 if (attributeID) {
216 oid_attr = "attributeID";
217 oid = talloc_strndup(req, (const char *)attributeID->data, attributeID->length);
218 } else if (governsID) {
219 oid_attr = "governsID";
220 oid = talloc_strndup(req, (const char *)governsID->data, governsID->length);
221 } else {
222 return ldb_next_request(module, req);
225 if (!oid) {
226 return ldb_oom(ldb);
229 status = dsdb_schema_pfm_find_oid(schema->prefixmap, oid, NULL);
230 if (!W_ERROR_IS_OK(status)) {
231 /* check for internal errors */
232 if (!W_ERROR_EQUAL(status, WERR_NOT_FOUND)) {
233 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
234 "schema_data_add: failed to map %s[%s]: %s\n",
235 oid_attr, oid, win_errstr(status));
236 return LDB_ERR_UNWILLING_TO_PERFORM;
239 /* Update prefixMap and save it */
240 status = dsdb_create_prefix_mapping(ldb, schema, oid);
241 if (!W_ERROR_IS_OK(status)) {
242 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
243 "schema_data_add: failed to create prefix mapping for %s[%s]: %s\n",
244 oid_attr, oid, win_errstr(status));
245 return LDB_ERR_UNWILLING_TO_PERFORM;
249 return ldb_next_request(module, req);
252 static int schema_data_modify(struct ldb_module *module, struct ldb_request *req)
254 struct ldb_context *ldb;
255 struct dsdb_schema *schema;
256 int cmp;
257 bool rodc = false;
258 int ret;
260 ldb = ldb_module_get_ctx(module);
262 /* special objects should always go through */
263 if (ldb_dn_is_special(req->op.mod.message->dn)) {
264 return ldb_next_request(module, req);
267 /* replicated update should always go through */
268 if (ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
269 return ldb_next_request(module, req);
272 /* dbcheck should be able to fix things */
273 if (ldb_request_get_control(req, DSDB_CONTROL_DBCHECK)) {
274 return ldb_next_request(module, req);
277 schema = dsdb_get_schema(ldb, req);
278 if (!schema) {
279 return ldb_next_request(module, req);
282 cmp = ldb_dn_compare(req->op.mod.message->dn, schema->base_dn);
283 if (cmp == 0) {
284 static const char * const constrained_attrs[] = {
285 "schemaInfo",
286 "prefixMap",
287 "msDs-Schema-Extensions",
288 "msDS-IntId",
289 NULL
291 size_t i;
292 struct ldb_message_element *el;
294 if (ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID)) {
295 return ldb_next_request(module, req);
298 for (i=0; constrained_attrs[i]; i++) {
299 el = ldb_msg_find_element(req->op.mod.message,
300 constrained_attrs[i]);
301 if (el == NULL) {
302 continue;
305 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
306 "schema_data_modify: reject update "
307 "of attribute[%s]\n",
308 constrained_attrs[i]);
309 return LDB_ERR_CONSTRAINT_VIOLATION;
312 return ldb_next_request(module, req);
315 ret = samdb_rodc(ldb, &rodc);
316 if (ret != LDB_SUCCESS) {
317 DEBUG(4, (__location__ ": unable to tell if we are an RODC \n"));
320 if (!schema->fsmo.we_are_master && !rodc) {
321 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
322 "schema_data_modify: we are not master: reject request\n");
323 return LDB_ERR_UNWILLING_TO_PERFORM;
326 if (!schema->fsmo.update_allowed && !rodc) {
327 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
328 "schema_data_add: updates are not allowed: reject request\n");
329 return LDB_ERR_UNWILLING_TO_PERFORM;
332 return ldb_next_request(module, req);
335 static int schema_data_del(struct ldb_module *module, struct ldb_request *req)
337 struct ldb_context *ldb;
338 struct dsdb_schema *schema;
339 bool rodc = false;
340 int ret;
342 ldb = ldb_module_get_ctx(module);
344 /* special objects should always go through */
345 if (ldb_dn_is_special(req->op.del.dn)) {
346 return ldb_next_request(module, req);
349 /* replicated update should always go through */
350 if (ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
351 return ldb_next_request(module, req);
354 /* dbcheck should be able to fix things */
355 if (ldb_request_get_control(req, DSDB_CONTROL_DBCHECK)) {
356 return ldb_next_request(module, req);
359 schema = dsdb_get_schema(ldb, req);
360 if (!schema) {
361 return ldb_next_request(module, req);
364 ret = samdb_rodc(ldb, &rodc);
365 if (ret != LDB_SUCCESS) {
366 DEBUG(4, (__location__ ": unable to tell if we are an RODC \n"));
369 if (!schema->fsmo.we_are_master && !rodc) {
370 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
371 "schema_data_modify: we are not master: reject request\n");
372 return LDB_ERR_UNWILLING_TO_PERFORM;
376 * normaly the DACL will prevent delete
377 * with LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
378 * above us.
380 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
381 "schema_data_del: delete is not allowed in the schema\n");
382 return LDB_ERR_UNWILLING_TO_PERFORM;
385 static int generate_objectClasses(struct ldb_context *ldb, struct ldb_message *msg,
386 const struct dsdb_schema *schema)
388 const struct dsdb_class *sclass;
389 int ret;
391 for (sclass = schema->classes; sclass; sclass = sclass->next) {
392 ret = ldb_msg_add_string(msg, "objectClasses", schema_class_to_description(msg, sclass));
393 if (ret != LDB_SUCCESS) {
394 return ret;
397 return LDB_SUCCESS;
399 static int generate_attributeTypes(struct ldb_context *ldb, struct ldb_message *msg,
400 const struct dsdb_schema *schema)
402 const struct dsdb_attribute *attribute;
403 int ret;
405 for (attribute = schema->attributes; attribute; attribute = attribute->next) {
406 ret = ldb_msg_add_string(msg, "attributeTypes", schema_attribute_to_description(msg, attribute));
407 if (ret != LDB_SUCCESS) {
408 return ret;
411 return LDB_SUCCESS;
414 static int generate_dITContentRules(struct ldb_context *ldb, struct ldb_message *msg,
415 const struct dsdb_schema *schema)
417 const struct dsdb_class *sclass;
418 int ret;
420 for (sclass = schema->classes; sclass; sclass = sclass->next) {
421 if (sclass->auxiliaryClass || sclass->systemAuxiliaryClass) {
422 char *ditcontentrule = schema_class_to_dITContentRule(msg, sclass, schema);
423 if (!ditcontentrule) {
424 return ldb_oom(ldb);
426 ret = ldb_msg_add_steal_string(msg, "dITContentRules", ditcontentrule);
427 if (ret != LDB_SUCCESS) {
428 return ret;
432 return LDB_SUCCESS;
435 static int generate_extendedAttributeInfo(struct ldb_context *ldb,
436 struct ldb_message *msg,
437 const struct dsdb_schema *schema)
439 const struct dsdb_attribute *attribute;
440 int ret;
442 for (attribute = schema->attributes; attribute; attribute = attribute->next) {
443 char *val = schema_attribute_to_extendedInfo(msg, attribute);
444 if (!val) {
445 return ldb_oom(ldb);
448 ret = ldb_msg_add_string(msg, "extendedAttributeInfo", val);
449 if (ret != LDB_SUCCESS) {
450 return ret;
454 return LDB_SUCCESS;
457 static int generate_extendedClassInfo(struct ldb_context *ldb,
458 struct ldb_message *msg,
459 const struct dsdb_schema *schema)
461 const struct dsdb_class *sclass;
462 int ret;
464 for (sclass = schema->classes; sclass; sclass = sclass->next) {
465 char *val = schema_class_to_extendedInfo(msg, sclass);
466 if (!val) {
467 return ldb_oom(ldb);
470 ret = ldb_msg_add_string(msg, "extendedClassInfo", val);
471 if (ret != LDB_SUCCESS) {
472 return ret;
476 return LDB_SUCCESS;
480 static int generate_possibleInferiors(struct ldb_context *ldb, struct ldb_message *msg,
481 const struct dsdb_schema *schema)
483 struct ldb_dn *dn = msg->dn;
484 unsigned int i;
485 int ret;
486 const char *first_component_name = ldb_dn_get_component_name(dn, 0);
487 const struct ldb_val *first_component_val;
488 const struct dsdb_class *schema_class;
489 const char **possibleInferiors;
491 if (strcasecmp(first_component_name, "cn") != 0) {
492 return LDB_SUCCESS;
495 first_component_val = ldb_dn_get_component_val(dn, 0);
497 schema_class = dsdb_class_by_cn_ldb_val(schema, first_component_val);
498 if (schema_class == NULL) {
499 return LDB_SUCCESS;
502 possibleInferiors = schema_class->possibleInferiors;
503 if (possibleInferiors == NULL) {
504 return LDB_SUCCESS;
507 for (i=0;possibleInferiors[i];i++) {
508 ret = ldb_msg_add_string(msg, "possibleInferiors", possibleInferiors[i]);
509 if (ret != LDB_SUCCESS) {
510 return ret;
514 return LDB_SUCCESS;
518 /* Add objectClasses, attributeTypes and dITContentRules from the
519 schema object (they are not stored in the database)
521 static int schema_data_search_callback(struct ldb_request *req, struct ldb_reply *ares)
523 struct ldb_context *ldb;
524 struct schema_data_search_data *ac;
525 struct schema_data_private_data *mc;
526 unsigned int i;
527 int ret;
529 ac = talloc_get_type(req->context, struct schema_data_search_data);
530 mc = talloc_get_type(ldb_module_get_private(ac->module), struct schema_data_private_data);
531 ldb = ldb_module_get_ctx(ac->module);
533 if (!ares) {
534 return ldb_module_done(ac->req, NULL, NULL,
535 LDB_ERR_OPERATIONS_ERROR);
537 if (ares->error != LDB_SUCCESS) {
538 return ldb_module_done(ac->req, ares->controls,
539 ares->response, ares->error);
541 /* Only entries are interesting, and we handle the case of the parent seperatly */
543 switch (ares->type) {
544 case LDB_REPLY_ENTRY:
546 if (ldb_dn_compare(ares->message->dn, mc->aggregate_dn) == 0) {
547 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
548 if (generated_attrs[i].aggregate &&
549 ldb_attr_in_list(ac->req->op.search.attrs, generated_attrs[i].attr)) {
550 ret = generated_attrs[i].fn(ldb, ares->message, ac->schema);
551 if (ret != LDB_SUCCESS) {
552 return ret;
556 } else if ((ldb_dn_compare_base(mc->schema_dn, ares->message->dn) == 0)
557 && (ldb_dn_compare(mc->schema_dn, ares->message->dn) != 0)) {
558 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
559 if (!generated_attrs[i].aggregate &&
560 ldb_attr_in_list(ac->req->op.search.attrs, generated_attrs[i].attr)) {
561 ret = generated_attrs[i].fn(ldb, ares->message, ac->schema);
562 if (ret != LDB_SUCCESS) {
563 return ret;
570 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
572 case LDB_REPLY_REFERRAL:
574 return ldb_module_send_referral(ac->req, ares->referral);
576 case LDB_REPLY_DONE:
578 return ldb_module_done(ac->req, ares->controls,
579 ares->response, ares->error);
582 return LDB_SUCCESS;
585 /* search */
586 static int schema_data_search(struct ldb_module *module, struct ldb_request *req)
588 struct ldb_context *ldb = ldb_module_get_ctx(module);
589 unsigned int i;
590 int ret;
591 struct schema_data_search_data *search_context;
592 struct ldb_request *down_req;
593 const struct dsdb_schema *schema;
594 if (!ldb_module_get_private(module)) {
595 /* If there is no module data, there is little we can do */
596 return ldb_next_request(module, req);
599 /* The schema manipulation does not apply to special DNs */
600 if (ldb_dn_is_special(req->op.search.base)) {
601 return ldb_next_request(module, req);
604 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
605 if (ldb_attr_in_list(req->op.search.attrs, generated_attrs[i].attr)) {
606 break;
609 if (i == ARRAY_SIZE(generated_attrs)) {
610 /* No request for a generated attr found, nothing to
611 * see here, move along... */
612 return ldb_next_request(module, req);
615 schema = dsdb_get_schema(ldb, NULL);
616 if (!schema || !ldb_module_get_private(module)) {
617 /* If there is no schema, there is little we can do */
618 return ldb_next_request(module, req);
621 search_context = talloc(req, struct schema_data_search_data);
622 if (!search_context) {
623 return ldb_oom(ldb);
626 search_context->module = module;
627 search_context->req = req;
628 search_context->schema = talloc_reference(search_context, schema);
629 if (!search_context->schema) {
630 return ldb_oom(ldb);
633 ret = ldb_build_search_req_ex(&down_req, ldb, search_context,
634 req->op.search.base,
635 req->op.search.scope,
636 req->op.search.tree,
637 req->op.search.attrs,
638 req->controls,
639 search_context, schema_data_search_callback,
640 req);
641 LDB_REQ_SET_LOCATION(down_req);
642 if (ret != LDB_SUCCESS) {
643 return ldb_operr(ldb);
646 return ldb_next_request(module, down_req);
650 static const struct ldb_module_ops ldb_schema_data_module_ops = {
651 .name = "schema_data",
652 .init_context = schema_data_init,
653 .add = schema_data_add,
654 .modify = schema_data_modify,
655 .del = schema_data_del,
656 .search = schema_data_search
659 int ldb_schema_data_module_init(const char *version)
661 LDB_MODULE_CHECK_VERSION(version);
662 return ldb_register_module(&ldb_schema_data_module_ops);