s4:dsdb Split schema loading and schema data management
[Samba/cd1.git] / source4 / dsdb / samdb / ldb_modules / schema_data.c
blob675cae3dc71277362d8b4568865f4a1523ac0cf6
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 TALLOC_CTX *mem_ctx;
99 struct ldb_dn *schema_dn;
100 struct dsdb_schema *schema;
101 int ret;
102 struct schema_data_private_data *data;
104 ret = ldb_next_init(module);
105 if (ret != LDB_SUCCESS) {
106 return ret;
109 ldb = ldb_module_get_ctx(module);
110 schema_dn = samdb_schema_dn(ldb);
111 if (!schema_dn) {
112 ldb_reset_err_string(ldb);
113 ldb_debug(ldb, LDB_DEBUG_WARNING,
114 "schema_data_init: no schema dn present: (skip schema loading)\n");
115 return LDB_SUCCESS;
118 data = talloc(module, struct schema_data_private_data);
119 if (data == NULL) {
120 ldb_oom(ldb);
121 return LDB_ERR_OPERATIONS_ERROR;
124 /* Check to see if this is a result on the CN=Aggregate schema */
125 data->aggregate_dn = ldb_dn_copy(data, schema_dn);
126 if (!ldb_dn_add_child_fmt(data->aggregate_dn, "CN=Aggregate")) {
127 ldb_oom(ldb);
128 return LDB_ERR_OPERATIONS_ERROR;
131 data->schema_dn = schema_dn;
133 ldb_module_set_private(module, data);
134 return LDB_SUCCESS;
137 static int schema_data_add(struct ldb_module *module, struct ldb_request *req)
139 struct ldb_context *ldb;
140 struct dsdb_schema *schema;
141 const struct ldb_val *attributeID = NULL;
142 const struct ldb_val *governsID = NULL;
143 const char *oid_attr = NULL;
144 const char *oid = NULL;
145 uint32_t id32;
146 WERROR status;
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);
161 if (!schema) {
162 return ldb_next_request(module, req);
165 if (!schema->fsmo.we_are_master) {
166 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
167 "schema_data_add: we are not master: reject request\n");
168 return LDB_ERR_UNWILLING_TO_PERFORM;
171 attributeID = ldb_msg_find_ldb_val(req->op.add.message, "attributeID");
172 governsID = ldb_msg_find_ldb_val(req->op.add.message, "governsID");
174 if (attributeID) {
175 oid_attr = "attributeID";
176 oid = talloc_strndup(req, (const char *)attributeID->data, attributeID->length);
177 } else if (governsID) {
178 oid_attr = "governsID";
179 oid = talloc_strndup(req, (const char *)governsID->data, governsID->length);
180 } else {
181 return ldb_next_request(module, req);
184 if (!oid) {
185 ldb_oom(ldb);
186 return LDB_ERR_OPERATIONS_ERROR;
189 status = dsdb_map_oid2int(schema, oid, &id32);
190 if (W_ERROR_IS_OK(status)) {
191 return ldb_next_request(module, req);
192 } else if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
193 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
194 "schema_data_add: failed to map %s[%s]: %s\n",
195 oid_attr, oid, win_errstr(status));
196 return LDB_ERR_UNWILLING_TO_PERFORM;
199 status = dsdb_create_prefix_mapping(ldb, schema, oid);
200 if (!W_ERROR_IS_OK(status)) {
201 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
202 "schema_data_add: failed to create prefix mapping for %s[%s]: %s\n",
203 oid_attr, oid, win_errstr(status));
204 return LDB_ERR_UNWILLING_TO_PERFORM;
207 return ldb_next_request(module, req);
210 static int generate_objectClasses(struct ldb_context *ldb, struct ldb_message *msg,
211 const struct dsdb_schema *schema)
213 const struct dsdb_class *sclass;
214 int ret;
216 for (sclass = schema->classes; sclass; sclass = sclass->next) {
217 ret = ldb_msg_add_string(msg, "objectClasses", schema_class_to_description(msg, sclass));
218 if (ret != LDB_SUCCESS) {
219 return ret;
222 return LDB_SUCCESS;
224 static int generate_attributeTypes(struct ldb_context *ldb, struct ldb_message *msg,
225 const struct dsdb_schema *schema)
227 const struct dsdb_attribute *attribute;
228 int ret;
230 for (attribute = schema->attributes; attribute; attribute = attribute->next) {
231 ret = ldb_msg_add_string(msg, "attributeTypes", schema_attribute_to_description(msg, attribute));
232 if (ret != LDB_SUCCESS) {
233 return ret;
236 return LDB_SUCCESS;
239 static int generate_dITContentRules(struct ldb_context *ldb, struct ldb_message *msg,
240 const struct dsdb_schema *schema)
242 const struct dsdb_class *sclass;
243 int ret;
245 for (sclass = schema->classes; sclass; sclass = sclass->next) {
246 if (sclass->auxiliaryClass || sclass->systemAuxiliaryClass) {
247 char *ditcontentrule = schema_class_to_dITContentRule(msg, sclass, schema);
248 if (!ditcontentrule) {
249 ldb_oom(ldb);
250 return LDB_ERR_OPERATIONS_ERROR;
252 ret = ldb_msg_add_steal_string(msg, "dITContentRules", ditcontentrule);
253 if (ret != LDB_SUCCESS) {
254 return ret;
258 return LDB_SUCCESS;
261 static int generate_extendedAttributeInfo(struct ldb_context *ldb,
262 struct ldb_message *msg,
263 const struct dsdb_schema *schema)
265 const struct dsdb_attribute *attribute;
266 int ret;
268 for (attribute = schema->attributes; attribute; attribute = attribute->next) {
269 char *val = schema_attribute_to_extendedInfo(msg, attribute);
270 if (!val) {
271 ldb_oom(ldb);
272 return LDB_ERR_OPERATIONS_ERROR;
275 ret = ldb_msg_add_string(msg, "extendedAttributeInfo", val);
276 if (ret != LDB_SUCCESS) {
277 return ret;
281 return LDB_SUCCESS;
284 static int generate_extendedClassInfo(struct ldb_context *ldb,
285 struct ldb_message *msg,
286 const struct dsdb_schema *schema)
288 const struct dsdb_class *sclass;
289 int ret;
291 for (sclass = schema->classes; sclass; sclass = sclass->next) {
292 char *val = schema_class_to_extendedInfo(msg, sclass);
293 if (!val) {
294 ldb_oom(ldb);
295 return LDB_ERR_OPERATIONS_ERROR;
298 ret = ldb_msg_add_string(msg, "extendedClassInfo", val);
299 if (ret != LDB_SUCCESS) {
300 return ret;
304 return LDB_SUCCESS;
308 static int generate_possibleInferiors(struct ldb_context *ldb, struct ldb_message *msg,
309 const struct dsdb_schema *schema)
311 struct ldb_dn *dn = msg->dn;
312 int ret, i;
313 const char *first_component_name = ldb_dn_get_component_name(dn, 0);
314 const struct ldb_val *first_component_val;
315 const struct dsdb_class *schema_class;
316 const char **possibleInferiors;
318 if (strcasecmp(first_component_name, "cn") != 0) {
319 return LDB_SUCCESS;
322 first_component_val = ldb_dn_get_component_val(dn, 0);
324 schema_class = dsdb_class_by_cn_ldb_val(schema, first_component_val);
325 if (schema_class == NULL) {
326 return LDB_SUCCESS;
329 possibleInferiors = schema_class->possibleInferiors;
330 if (possibleInferiors == NULL) {
331 return LDB_SUCCESS;
334 for (i=0;possibleInferiors[i];i++) {
335 ret = ldb_msg_add_string(msg, "possibleInferiors", possibleInferiors[i]);
336 if (ret != LDB_SUCCESS) {
337 return ret;
341 return LDB_SUCCESS;
345 /* Add objectClasses, attributeTypes and dITContentRules from the
346 schema object (they are not stored in the database)
348 static int schema_data_search_callback(struct ldb_request *req, struct ldb_reply *ares)
350 struct ldb_context *ldb;
351 struct schema_data_search_data *ac;
352 struct schema_data_private_data *mc;
353 int i, ret;
355 ac = talloc_get_type(req->context, struct schema_data_search_data);
356 mc = talloc_get_type(ldb_module_get_private(ac->module), struct schema_data_private_data);
357 ldb = ldb_module_get_ctx(ac->module);
359 if (!ares) {
360 return ldb_module_done(ac->req, NULL, NULL,
361 LDB_ERR_OPERATIONS_ERROR);
363 if (ares->error != LDB_SUCCESS) {
364 return ldb_module_done(ac->req, ares->controls,
365 ares->response, ares->error);
367 /* Only entries are interesting, and we handle the case of the parent seperatly */
369 switch (ares->type) {
370 case LDB_REPLY_ENTRY:
372 if (ldb_dn_compare(ares->message->dn, mc->aggregate_dn) == 0) {
373 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
374 if (generated_attrs[i].aggregate &&
375 ldb_attr_in_list(ac->req->op.search.attrs, generated_attrs[i].attr)) {
376 ret = generated_attrs[i].fn(ldb, ares->message, ac->schema);
377 if (ret != LDB_SUCCESS) {
378 return ret;
382 } else if ((ldb_dn_compare_base(mc->schema_dn, ares->message->dn) == 0)
383 && (ldb_dn_compare(mc->schema_dn, ares->message->dn) != 0)) {
384 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
385 if (!generated_attrs[i].aggregate &&
386 ldb_attr_in_list(ac->req->op.search.attrs, generated_attrs[i].attr)) {
387 ret = generated_attrs[i].fn(ldb, ares->message, ac->schema);
388 if (ret != LDB_SUCCESS) {
389 return ret;
396 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
398 case LDB_REPLY_REFERRAL:
400 return ldb_module_send_referral(ac->req, ares->referral);
402 case LDB_REPLY_DONE:
404 return ldb_module_done(ac->req, ares->controls,
405 ares->response, ares->error);
408 return LDB_SUCCESS;
411 /* search */
412 static int schema_data_search(struct ldb_module *module, struct ldb_request *req)
414 struct ldb_context *ldb = ldb_module_get_ctx(module);
415 int i, ret;
416 struct schema_data_search_data *search_context;
417 struct ldb_request *down_req;
418 struct dsdb_schema *schema = dsdb_get_schema(ldb);
420 if (!schema || !ldb_module_get_private(module)) {
421 /* If there is no schema, there is little we can do */
422 return ldb_next_request(module, req);
424 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
425 if (ldb_attr_in_list(req->op.search.attrs, generated_attrs[i].attr)) {
426 break;
429 if (i == ARRAY_SIZE(generated_attrs)) {
430 /* No request for a generated attr found, nothing to
431 * see here, move along... */
432 return ldb_next_request(module, req);
435 search_context = talloc(req, struct schema_data_search_data);
436 if (!search_context) {
437 ldb_oom(ldb);
438 return LDB_ERR_OPERATIONS_ERROR;
441 search_context->module = module;
442 search_context->req = req;
443 search_context->schema = schema;
445 ret = ldb_build_search_req_ex(&down_req, ldb, search_context,
446 req->op.search.base,
447 req->op.search.scope,
448 req->op.search.tree,
449 req->op.search.attrs,
450 req->controls,
451 search_context, schema_data_search_callback,
452 req);
453 if (ret != LDB_SUCCESS) {
454 return LDB_ERR_OPERATIONS_ERROR;
457 return ldb_next_request(module, down_req);
461 _PUBLIC_ const struct ldb_module_ops ldb_schema_data_module_ops = {
462 .name = "schema_data",
463 .init_context = schema_data_init,
464 .add = schema_data_add,
465 .search = schema_data_search