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 "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "lib/ldb/include/ldb_private.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "librpc/gen_ndr/ndr_misc.h"
30 #include "librpc/gen_ndr/ndr_drsuapi.h"
31 #include "librpc/gen_ndr/ndr_drsblobs.h"
32 #include "lib/util/dlinklist.h"
33 #include "param/param.h"
35 static int generate_objectClasses(struct ldb_context *ldb, struct ldb_message *msg,
36 const struct dsdb_schema *schema);
37 static int generate_attributeTypes(struct ldb_context *ldb, struct ldb_message *msg,
38 const struct dsdb_schema *schema);
39 static int generate_dITContentRules(struct ldb_context *ldb, struct ldb_message *msg,
40 const struct dsdb_schema *schema);
44 int (*fn)(struct ldb_context *, struct ldb_message *, const struct dsdb_schema *);
45 } generated_attrs[] = {
47 .attr = "objectClasses",
48 .fn = generate_objectClasses
51 .attr = "attributeTypes",
52 .fn = generate_attributeTypes
55 .attr = "dITContentRules",
56 .fn = generate_dITContentRules
60 struct schema_fsmo_private_data {
61 struct ldb_dn *aggregate_dn;
64 struct schema_fsmo_search_data {
65 struct schema_fsmo_private_data *module_context;
66 struct ldb_request *orig_req;
69 static int schema_fsmo_init(struct ldb_module *module)
72 struct ldb_dn *schema_dn;
73 struct dsdb_schema *schema;
74 char *error_string = NULL;
76 struct schema_fsmo_private_data *data;
78 schema_dn = samdb_schema_dn(module->ldb);
80 ldb_reset_err_string(module->ldb);
81 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
82 "schema_fsmo_init: no schema dn present: (skip schema loading)\n");
83 return ldb_next_init(module);
86 data = talloc(module, struct schema_fsmo_private_data);
89 return LDB_ERR_OPERATIONS_ERROR;
92 /* Check to see if this is a result on the CN=Aggregate schema */
93 data->aggregate_dn = ldb_dn_copy(data, schema_dn);
94 if (!ldb_dn_add_child_fmt(data->aggregate_dn, "CN=Aggregate")) {
96 return LDB_ERR_OPERATIONS_ERROR;
99 module->private_data = data;
101 if (dsdb_get_schema(module->ldb)) {
102 return ldb_next_init(module);
105 mem_ctx = talloc_new(module);
107 ldb_oom(module->ldb);
108 return LDB_ERR_OPERATIONS_ERROR;
111 ret = dsdb_schema_from_schema_dn(mem_ctx, module->ldb,
112 lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")),
113 schema_dn, &schema, &error_string);
115 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
116 ldb_reset_err_string(module->ldb);
117 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
118 "schema_fsmo_init: no schema head present: (skip schema loading)\n");
119 talloc_free(mem_ctx);
120 return ldb_next_init(module);
123 if (ret != LDB_SUCCESS) {
124 ldb_asprintf_errstring(module->ldb,
125 "schema_fsmo_init: dsdb_schema load failed: %s",
127 talloc_free(mem_ctx);
131 /* dsdb_set_schema() steal schema into the ldb_context */
132 ret = dsdb_set_schema(module->ldb, schema);
133 if (ret != LDB_SUCCESS) {
134 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
135 "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
136 ret, ldb_strerror(ret));
137 talloc_free(mem_ctx);
141 talloc_free(mem_ctx);
142 return ldb_next_init(module);
145 static int schema_fsmo_add(struct ldb_module *module, struct ldb_request *req)
147 struct dsdb_schema *schema;
148 const char *attributeID = NULL;
149 const char *governsID = NULL;
150 const char *oid_attr = NULL;
151 const char *oid = NULL;
155 schema = dsdb_get_schema(module->ldb);
157 return ldb_next_request(module, req);
160 if (!schema->fsmo.we_are_master) {
161 ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
162 "schema_fsmo_add: we are not master: reject request\n");
163 return LDB_ERR_UNWILLING_TO_PERFORM;
166 attributeID = samdb_result_string(req->op.add.message, "attributeID", NULL);
167 governsID = samdb_result_string(req->op.add.message, "governsID", NULL);
170 oid_attr = "attributeID";
172 } else if (governsID) {
173 oid_attr = "governsID";
178 return ldb_next_request(module, req);
181 status = dsdb_map_oid2int(schema, oid, &id32);
182 if (W_ERROR_IS_OK(status)) {
183 return ldb_next_request(module, req);
184 } else if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
185 ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
186 "schema_fsmo_add: failed to map %s[%s]: %s\n",
187 oid_attr, oid, win_errstr(status));
188 return LDB_ERR_UNWILLING_TO_PERFORM;
191 status = dsdb_create_prefix_mapping(module->ldb, schema, oid);
192 if (!W_ERROR_IS_OK(status)) {
193 ldb_debug_set(module->ldb, LDB_DEBUG_ERROR,
194 "schema_fsmo_add: failed to create prefix mapping for %s[%s]: %s\n",
195 oid_attr, oid, win_errstr(status));
196 return LDB_ERR_UNWILLING_TO_PERFORM;
199 return ldb_next_request(module, req);
202 static int schema_fsmo_extended(struct ldb_module *module, struct ldb_request *req)
204 struct ldb_dn *schema_dn;
205 struct dsdb_schema *schema;
206 char *error_string = NULL;
210 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID) != 0) {
211 return ldb_next_request(module, req);
214 schema_dn = samdb_schema_dn(module->ldb);
216 ldb_reset_err_string(module->ldb);
217 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
218 "schema_fsmo_extended: no schema dn present: (skip schema loading)\n");
219 return ldb_next_request(module, req);
222 mem_ctx = talloc_new(module);
224 ldb_oom(module->ldb);
225 return LDB_ERR_OPERATIONS_ERROR;
228 ret = dsdb_schema_from_schema_dn(mem_ctx, module->ldb,
229 lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")),
230 schema_dn, &schema, &error_string);
232 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
233 ldb_reset_err_string(module->ldb);
234 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
235 "schema_fsmo_extended: no schema head present: (skip schema loading)\n");
236 talloc_free(mem_ctx);
237 return ldb_next_request(module, req);
240 if (ret != LDB_SUCCESS) {
241 ldb_asprintf_errstring(module->ldb,
242 "schema_fsmo_extended: dsdb_schema load failed: %s",
244 talloc_free(mem_ctx);
245 return ldb_next_request(module, req);
248 /* Replace the old schema*/
249 ret = dsdb_set_schema(module->ldb, schema);
250 if (ret != LDB_SUCCESS) {
251 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
252 "schema_fsmo_extended: dsdb_set_schema() failed: %d:%s",
253 ret, ldb_strerror(ret));
254 talloc_free(mem_ctx);
258 talloc_free(mem_ctx);
262 static int generate_objectClasses(struct ldb_context *ldb, struct ldb_message *msg,
263 const struct dsdb_schema *schema)
265 const struct dsdb_class *class;
268 for (class = schema->classes; class; class = class->next) {
269 ret = ldb_msg_add_string(msg, "objectClasses", schema_class_to_description(msg, class));
270 if (ret != LDB_SUCCESS) {
276 static int generate_attributeTypes(struct ldb_context *ldb, struct ldb_message *msg,
277 const struct dsdb_schema *schema)
279 const struct dsdb_attribute *attribute;
282 for (attribute = schema->attributes; attribute; attribute = attribute->next) {
283 ret = ldb_msg_add_string(msg, "attributeTypes", schema_attribute_to_description(msg, attribute));
284 if (ret != LDB_SUCCESS) {
291 static int generate_dITContentRules(struct ldb_context *ldb, struct ldb_message *msg,
292 const struct dsdb_schema *schema)
294 const struct dsdb_class *class;
297 for (class = schema->classes; class; class = class->next) {
298 if (class->auxiliaryClass || class->systemAuxiliaryClass) {
299 char *ditcontentrule = schema_class_to_dITContentRule(msg, class, schema);
300 if (!ditcontentrule) {
302 return LDB_ERR_OPERATIONS_ERROR;
304 ret = ldb_msg_add_steal_string(msg, "dITContentRules", ditcontentrule);
305 if (ret != LDB_SUCCESS) {
315 /* Add objectClasses, attributeTypes and dITContentRules from the
316 schema object (they are not stored in the database)
318 static int schema_fsmo_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
320 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
321 struct schema_fsmo_search_data *search_data = talloc_get_type(context, struct schema_fsmo_search_data);
322 struct ldb_request *orig_req = search_data->orig_req;
326 /* Only entries are interesting, and we handle the case of the parent seperatly */
327 if (ares->type != LDB_REPLY_ENTRY) {
328 return orig_req->callback(ldb, orig_req->context, ares);
331 if (ldb_dn_compare(ares->message->dn, search_data->module_context->aggregate_dn) != 0) {
332 talloc_free(mem_ctx);
333 return orig_req->callback(ldb, orig_req->context, ares);
336 mem_ctx = talloc_new(ares);
339 return LDB_ERR_OPERATIONS_ERROR;
342 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
343 if (ldb_attr_in_list(orig_req->op.search.attrs, generated_attrs[i].attr)) {
344 ret = generated_attrs[i].fn(ldb, ares->message, schema);
345 if (ret != LDB_SUCCESS) {
351 talloc_free(mem_ctx);
352 return orig_req->callback(ldb, orig_req->context, ares);
356 static int schema_fsmo_search(struct ldb_module *module, struct ldb_request *req)
359 struct schema_fsmo_search_data *search_context;
360 struct ldb_request *down_req;
361 struct dsdb_schema *schema = dsdb_get_schema(module->ldb);
363 if (!schema || !module->private_data) {
364 /* If there is no schema, there is little we can do */
365 return ldb_next_request(module, req);
367 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
368 if (ldb_attr_in_list(req->op.search.attrs, generated_attrs[i].attr)) {
372 if (i == ARRAY_SIZE(generated_attrs)) {
373 /* No request for a generated attr found, nothing to
374 * see here, move along... */
375 return ldb_next_request(module, req);
378 search_context = talloc(req, struct schema_fsmo_search_data);
379 if (!search_context) {
380 ldb_oom(module->ldb);
381 return LDB_ERR_OPERATIONS_ERROR;
383 down_req = talloc(req, struct ldb_request);
385 ldb_oom(module->ldb);
386 return LDB_ERR_OPERATIONS_ERROR;
390 search_context->orig_req = req;
391 search_context->module_context = talloc_get_type(module->private_data, struct schema_fsmo_private_data);
392 down_req->context = search_context;
394 down_req->callback = schema_fsmo_search_callback;
396 ret = ldb_next_request(module, down_req);
398 /* do not free down_req as the call results may be linked to it,
399 * it will be freed when the upper level request get freed */
400 if (ret == LDB_SUCCESS) {
401 req->handle = down_req->handle;
407 _PUBLIC_ const struct ldb_module_ops ldb_schema_fsmo_module_ops = {
408 .name = "schema_fsmo",
409 .init_context = schema_fsmo_init,
410 .add = schema_fsmo_add,
411 .extended = schema_fsmo_extended,
412 .search = schema_fsmo_search