2 Unix SMB/CIFS mplementation.
4 The module that handles the Schema FSMO Role Owner
5 checkings, it also loads the dsdb_schema.
7 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
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/>.
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"
32 static int generate_objectClasses(struct ldb_context *ldb, struct ldb_message *msg,
33 const struct dsdb_schema *schema);
34 static int generate_attributeTypes(struct ldb_context *ldb, struct ldb_message *msg,
35 const struct dsdb_schema *schema);
36 static int generate_dITContentRules(struct ldb_context *ldb, struct ldb_message *msg,
37 const struct dsdb_schema *schema);
38 static int generate_extendedAttributeInfo(struct ldb_context *ldb, struct ldb_message *msg,
39 const struct dsdb_schema *schema);
40 static int generate_extendedClassInfo(struct ldb_context *ldb, struct ldb_message *msg,
41 const struct dsdb_schema *schema);
42 static int generate_possibleInferiors(struct ldb_context *ldb, struct ldb_message *msg,
43 const struct dsdb_schema *schema);
47 int (*fn)(struct ldb_context *, struct ldb_message *, const struct dsdb_schema *);
49 } generated_attrs[] = {
51 .attr = "objectClasses",
52 .fn = generate_objectClasses,
56 .attr = "attributeTypes",
57 .fn = generate_attributeTypes,
61 .attr = "dITContentRules",
62 .fn = generate_dITContentRules,
66 .attr = "extendedAttributeInfo",
67 .fn = generate_extendedAttributeInfo,
71 .attr = "extendedClassInfo",
72 .fn = generate_extendedClassInfo,
76 .attr = "possibleInferiors",
77 .fn = generate_possibleInferiors,
82 struct schema_fsmo_private_data {
83 struct ldb_dn *aggregate_dn;
86 struct schema_fsmo_search_data {
87 struct ldb_module *module;
88 struct ldb_request *req;
90 const struct dsdb_schema *schema;
93 static int schema_fsmo_init(struct ldb_module *module)
95 struct ldb_context *ldb;
97 struct ldb_dn *schema_dn;
98 struct dsdb_schema *schema;
99 char *error_string = NULL;
101 struct schema_fsmo_private_data *data;
103 ldb = ldb_module_get_ctx(module);
104 schema_dn = samdb_schema_dn(ldb);
106 ldb_reset_err_string(ldb);
107 ldb_debug(ldb, LDB_DEBUG_WARNING,
108 "schema_fsmo_init: no schema dn present: (skip schema loading)\n");
109 return ldb_next_init(module);
112 data = talloc(module, struct schema_fsmo_private_data);
115 return LDB_ERR_OPERATIONS_ERROR;
118 /* Check to see if this is a result on the CN=Aggregate schema */
119 data->aggregate_dn = ldb_dn_copy(data, schema_dn);
120 if (!ldb_dn_add_child_fmt(data->aggregate_dn, "CN=Aggregate")) {
122 return LDB_ERR_OPERATIONS_ERROR;
125 ldb_module_set_private(module, data);
127 if (dsdb_get_schema(ldb)) {
128 return ldb_next_init(module);
131 mem_ctx = talloc_new(module);
134 return LDB_ERR_OPERATIONS_ERROR;
137 ret = dsdb_schema_from_schema_dn(mem_ctx, ldb,
138 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
139 schema_dn, &schema, &error_string);
141 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
142 ldb_reset_err_string(ldb);
143 ldb_debug(ldb, LDB_DEBUG_WARNING,
144 "schema_fsmo_init: no schema head present: (skip schema loading)\n");
145 talloc_free(mem_ctx);
146 return ldb_next_init(module);
149 if (ret != LDB_SUCCESS) {
150 ldb_asprintf_errstring(ldb,
151 "schema_fsmo_init: dsdb_schema load failed: %s",
153 talloc_free(mem_ctx);
157 /* dsdb_set_schema() steal schema into the ldb_context */
158 ret = dsdb_set_schema(ldb, schema);
159 if (ret != LDB_SUCCESS) {
160 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
161 "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
162 ret, ldb_strerror(ret));
163 talloc_free(mem_ctx);
167 talloc_free(mem_ctx);
168 return ldb_next_init(module);
171 static int schema_fsmo_add(struct ldb_module *module, struct ldb_request *req)
173 struct ldb_context *ldb;
174 struct dsdb_schema *schema;
175 const char *attributeID = NULL;
176 const char *governsID = NULL;
177 const char *oid_attr = NULL;
178 const char *oid = NULL;
182 ldb = ldb_module_get_ctx(module);
184 /* special objects should always go through */
185 if (ldb_dn_is_special(req->op.add.message->dn)) {
186 return ldb_next_request(module, req);
189 /* replicated update should always go through */
190 if (ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
191 return ldb_next_request(module, req);
194 schema = dsdb_get_schema(ldb);
196 return ldb_next_request(module, req);
199 if (!schema->fsmo.we_are_master) {
200 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
201 "schema_fsmo_add: we are not master: reject request\n");
202 return LDB_ERR_UNWILLING_TO_PERFORM;
205 attributeID = samdb_result_string(req->op.add.message, "attributeID", NULL);
206 governsID = samdb_result_string(req->op.add.message, "governsID", NULL);
209 oid_attr = "attributeID";
211 } else if (governsID) {
212 oid_attr = "governsID";
217 return ldb_next_request(module, req);
220 status = dsdb_map_oid2int(schema, oid, &id32);
221 if (W_ERROR_IS_OK(status)) {
222 return ldb_next_request(module, req);
223 } else if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
224 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
225 "schema_fsmo_add: failed to map %s[%s]: %s\n",
226 oid_attr, oid, win_errstr(status));
227 return LDB_ERR_UNWILLING_TO_PERFORM;
230 status = dsdb_create_prefix_mapping(ldb, schema, oid);
231 if (!W_ERROR_IS_OK(status)) {
232 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
233 "schema_fsmo_add: failed to create prefix mapping for %s[%s]: %s\n",
234 oid_attr, oid, win_errstr(status));
235 return LDB_ERR_UNWILLING_TO_PERFORM;
238 return ldb_next_request(module, req);
241 static int schema_fsmo_extended(struct ldb_module *module, struct ldb_request *req)
243 struct ldb_context *ldb;
244 struct ldb_dn *schema_dn;
245 struct dsdb_schema *schema;
246 char *error_string = NULL;
250 ldb = ldb_module_get_ctx(module);
252 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID) != 0) {
253 return ldb_next_request(module, req);
256 schema_dn = samdb_schema_dn(ldb);
258 ldb_reset_err_string(ldb);
259 ldb_debug(ldb, LDB_DEBUG_WARNING,
260 "schema_fsmo_extended: no schema dn present: (skip schema loading)\n");
261 return ldb_next_request(module, req);
264 mem_ctx = talloc_new(module);
267 return LDB_ERR_OPERATIONS_ERROR;
270 ret = dsdb_schema_from_schema_dn(mem_ctx, ldb,
271 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
272 schema_dn, &schema, &error_string);
274 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
275 ldb_reset_err_string(ldb);
276 ldb_debug(ldb, LDB_DEBUG_WARNING,
277 "schema_fsmo_extended: no schema head present: (skip schema loading)\n");
278 talloc_free(mem_ctx);
279 return ldb_next_request(module, req);
282 if (ret != LDB_SUCCESS) {
283 ldb_asprintf_errstring(ldb,
284 "schema_fsmo_extended: dsdb_schema load failed: %s",
286 talloc_free(mem_ctx);
287 return ldb_next_request(module, req);
290 /* Replace the old schema*/
291 ret = dsdb_set_schema(ldb, schema);
292 if (ret != LDB_SUCCESS) {
293 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
294 "schema_fsmo_extended: dsdb_set_schema() failed: %d:%s",
295 ret, ldb_strerror(ret));
296 talloc_free(mem_ctx);
300 talloc_free(mem_ctx);
304 static int generate_objectClasses(struct ldb_context *ldb, struct ldb_message *msg,
305 const struct dsdb_schema *schema)
307 const struct dsdb_class *sclass;
310 for (sclass = schema->classes; sclass; sclass = sclass->next) {
311 ret = ldb_msg_add_string(msg, "objectClasses", schema_class_to_description(msg, sclass));
312 if (ret != LDB_SUCCESS) {
318 static int generate_attributeTypes(struct ldb_context *ldb, struct ldb_message *msg,
319 const struct dsdb_schema *schema)
321 const struct dsdb_attribute *attribute;
324 for (attribute = schema->attributes; attribute; attribute = attribute->next) {
325 ret = ldb_msg_add_string(msg, "attributeTypes", schema_attribute_to_description(msg, attribute));
326 if (ret != LDB_SUCCESS) {
333 static int generate_dITContentRules(struct ldb_context *ldb, struct ldb_message *msg,
334 const struct dsdb_schema *schema)
336 const struct dsdb_class *sclass;
339 for (sclass = schema->classes; sclass; sclass = sclass->next) {
340 if (sclass->auxiliaryClass || sclass->systemAuxiliaryClass) {
341 char *ditcontentrule = schema_class_to_dITContentRule(msg, sclass, schema);
342 if (!ditcontentrule) {
344 return LDB_ERR_OPERATIONS_ERROR;
346 ret = ldb_msg_add_steal_string(msg, "dITContentRules", ditcontentrule);
347 if (ret != LDB_SUCCESS) {
355 static int generate_extendedAttributeInfo(struct ldb_context *ldb,
356 struct ldb_message *msg,
357 const struct dsdb_schema *schema)
359 const struct dsdb_attribute *attribute;
362 for (attribute = schema->attributes; attribute; attribute = attribute->next) {
363 char *val = schema_attribute_to_extendedInfo(msg, attribute);
366 return LDB_ERR_OPERATIONS_ERROR;
369 ret = ldb_msg_add_string(msg, "extendedAttributeInfo", val);
370 if (ret != LDB_SUCCESS) {
378 static int generate_extendedClassInfo(struct ldb_context *ldb,
379 struct ldb_message *msg,
380 const struct dsdb_schema *schema)
382 const struct dsdb_class *sclass;
385 for (sclass = schema->classes; sclass; sclass = sclass->next) {
386 char *val = schema_class_to_extendedInfo(msg, sclass);
389 return LDB_ERR_OPERATIONS_ERROR;
392 ret = ldb_msg_add_string(msg, "extendedClassInfo", val);
393 if (ret != LDB_SUCCESS) {
402 static int generate_possibleInferiors(struct ldb_context *ldb, struct ldb_message *msg,
403 const struct dsdb_schema *schema)
405 struct ldb_dn *dn = msg->dn;
407 const char *first_component_name = ldb_dn_get_component_name(dn, 0);
408 const struct ldb_val *first_component_val;
409 const char *class_name;
410 const struct dsdb_class *schema_class;
411 const char **possibleInferiors;
413 if (strcasecmp(first_component_name, "cn") != 0) {
417 first_component_val = ldb_dn_get_component_val(dn, 0);
418 class_name = (const char *)first_component_val->data;
420 schema_class = dsdb_class_by_cn(schema, class_name);
421 if (schema_class == NULL) {
425 possibleInferiors = schema_class->possibleInferiors;
426 if (possibleInferiors == NULL) {
430 for (i=0;possibleInferiors[i];i++) {
431 ret = ldb_msg_add_string(msg, "possibleInferiors", possibleInferiors[i]);
432 if (ret != LDB_SUCCESS) {
441 /* Add objectClasses, attributeTypes and dITContentRules from the
442 schema object (they are not stored in the database)
444 static int schema_fsmo_search_callback(struct ldb_request *req, struct ldb_reply *ares)
446 struct ldb_context *ldb;
447 struct schema_fsmo_search_data *ac;
448 struct schema_fsmo_private_data *mc;
451 ac = talloc_get_type(req->context, struct schema_fsmo_search_data);
452 mc = talloc_get_type(ldb_module_get_private(ac->module), struct schema_fsmo_private_data);
453 ldb = ldb_module_get_ctx(ac->module);
456 return ldb_module_done(ac->req, NULL, NULL,
457 LDB_ERR_OPERATIONS_ERROR);
459 if (ares->error != LDB_SUCCESS) {
460 return ldb_module_done(ac->req, ares->controls,
461 ares->response, ares->error);
463 /* Only entries are interesting, and we handle the case of the parent seperatly */
465 switch (ares->type) {
466 case LDB_REPLY_ENTRY:
468 if (ldb_dn_compare(ares->message->dn, mc->aggregate_dn) == 0) {
469 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
470 if (generated_attrs[i].aggregate &&
471 ldb_attr_in_list(ac->req->op.search.attrs, generated_attrs[i].attr)) {
472 ret = generated_attrs[i].fn(ldb, ares->message, ac->schema);
473 if (ret != LDB_SUCCESS) {
479 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
480 if (!generated_attrs[i].aggregate &&
481 ldb_attr_in_list(ac->req->op.search.attrs, generated_attrs[i].attr)) {
482 ret = generated_attrs[i].fn(ldb, ares->message, ac->schema);
483 if (ret != LDB_SUCCESS) {
491 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
493 case LDB_REPLY_REFERRAL:
495 return ldb_module_send_referral(ac->req, ares->referral);
499 return ldb_module_done(ac->req, ares->controls,
500 ares->response, ares->error);
507 static int schema_fsmo_search(struct ldb_module *module, struct ldb_request *req)
509 struct ldb_context *ldb = ldb_module_get_ctx(module);
511 struct schema_fsmo_search_data *search_context;
512 struct ldb_request *down_req;
513 struct dsdb_schema *schema = dsdb_get_schema(ldb);
515 if (!schema || !ldb_module_get_private(module)) {
516 /* If there is no schema, there is little we can do */
517 return ldb_next_request(module, req);
519 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
520 if (ldb_attr_in_list(req->op.search.attrs, generated_attrs[i].attr)) {
524 if (i == ARRAY_SIZE(generated_attrs)) {
525 /* No request for a generated attr found, nothing to
526 * see here, move along... */
527 return ldb_next_request(module, req);
530 search_context = talloc(req, struct schema_fsmo_search_data);
531 if (!search_context) {
533 return LDB_ERR_OPERATIONS_ERROR;
536 search_context->module = module;
537 search_context->req = req;
538 search_context->schema = schema;
540 ret = ldb_build_search_req_ex(&down_req, ldb, search_context,
542 req->op.search.scope,
544 req->op.search.attrs,
546 search_context, schema_fsmo_search_callback,
548 if (ret != LDB_SUCCESS) {
549 return LDB_ERR_OPERATIONS_ERROR;
552 return ldb_next_request(module, down_req);
556 _PUBLIC_ const struct ldb_module_ops ldb_schema_fsmo_module_ops = {
557 .name = "schema_fsmo",
558 .init_context = schema_fsmo_init,
559 .add = schema_fsmo_add,
560 .extended = schema_fsmo_extended,
561 .search = schema_fsmo_search