s4:dsdb Change dsdb_get_schema() callers to use new talloc argument
[Samba/gebeck_regimport.git] / source4 / dsdb / samdb / ldb_modules / schema_data.c
blobc71a01d6306afef93d06c7a6a06c9c3e7456fd82
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 ldb_oom(ldb);
119 return LDB_ERR_OPERATIONS_ERROR;
122 data->schema_dn = schema_dn;
124 /* Used to check to see if this is a result on the CN=Aggregate schema */
125 data->aggregate_dn = samdb_aggregate_schema_dn(ldb, data);
126 if (!data->aggregate_dn) {
127 ldb_set_errstring(ldb, "Could not build aggregate schema DN");
128 return LDB_ERR_OPERATIONS_ERROR;
131 ldb_module_set_private(module, data);
132 return LDB_SUCCESS;
135 static int schema_data_add(struct ldb_module *module, struct ldb_request *req)
137 struct ldb_context *ldb;
138 struct dsdb_schema *schema;
139 const struct ldb_val *attributeID = NULL;
140 const struct ldb_val *governsID = NULL;
141 const char *oid_attr = NULL;
142 const char *oid = NULL;
143 WERROR status;
145 ldb = ldb_module_get_ctx(module);
147 /* special objects should always go through */
148 if (ldb_dn_is_special(req->op.add.message->dn)) {
149 return ldb_next_request(module, req);
152 /* replicated update should always go through */
153 if (ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
154 return ldb_next_request(module, req);
157 schema = dsdb_get_schema(ldb, req);
158 if (!schema) {
159 return ldb_next_request(module, req);
162 if (!schema->fsmo.we_are_master) {
163 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
164 "schema_data_add: we are not master: reject request\n");
165 return LDB_ERR_UNWILLING_TO_PERFORM;
168 attributeID = ldb_msg_find_ldb_val(req->op.add.message, "attributeID");
169 governsID = ldb_msg_find_ldb_val(req->op.add.message, "governsID");
171 if (attributeID) {
172 oid_attr = "attributeID";
173 oid = talloc_strndup(req, (const char *)attributeID->data, attributeID->length);
174 } else if (governsID) {
175 oid_attr = "governsID";
176 oid = talloc_strndup(req, (const char *)governsID->data, governsID->length);
177 } else {
178 return ldb_next_request(module, req);
181 if (!oid) {
182 ldb_oom(ldb);
183 return LDB_ERR_OPERATIONS_ERROR;
186 status = dsdb_schema_pfm_find_oid(schema->prefixmap, oid, NULL);
187 if (!W_ERROR_IS_OK(status)) {
188 /* check for internal errors */
189 if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
190 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
191 "schema_data_add: failed to map %s[%s]: %s\n",
192 oid_attr, oid, win_errstr(status));
193 return LDB_ERR_UNWILLING_TO_PERFORM;
196 /* Update prefixMap and save it */
197 status = dsdb_create_prefix_mapping(ldb, schema, oid);
198 if (!W_ERROR_IS_OK(status)) {
199 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
200 "schema_data_add: failed to create prefix mapping for %s[%s]: %s\n",
201 oid_attr, oid, win_errstr(status));
202 return LDB_ERR_UNWILLING_TO_PERFORM;
206 return ldb_next_request(module, req);
209 static int generate_objectClasses(struct ldb_context *ldb, struct ldb_message *msg,
210 const struct dsdb_schema *schema)
212 const struct dsdb_class *sclass;
213 int ret;
215 for (sclass = schema->classes; sclass; sclass = sclass->next) {
216 ret = ldb_msg_add_string(msg, "objectClasses", schema_class_to_description(msg, sclass));
217 if (ret != LDB_SUCCESS) {
218 return ret;
221 return LDB_SUCCESS;
223 static int generate_attributeTypes(struct ldb_context *ldb, struct ldb_message *msg,
224 const struct dsdb_schema *schema)
226 const struct dsdb_attribute *attribute;
227 int ret;
229 for (attribute = schema->attributes; attribute; attribute = attribute->next) {
230 ret = ldb_msg_add_string(msg, "attributeTypes", schema_attribute_to_description(msg, attribute));
231 if (ret != LDB_SUCCESS) {
232 return ret;
235 return LDB_SUCCESS;
238 static int generate_dITContentRules(struct ldb_context *ldb, struct ldb_message *msg,
239 const struct dsdb_schema *schema)
241 const struct dsdb_class *sclass;
242 int ret;
244 for (sclass = schema->classes; sclass; sclass = sclass->next) {
245 if (sclass->auxiliaryClass || sclass->systemAuxiliaryClass) {
246 char *ditcontentrule = schema_class_to_dITContentRule(msg, sclass, schema);
247 if (!ditcontentrule) {
248 ldb_oom(ldb);
249 return LDB_ERR_OPERATIONS_ERROR;
251 ret = ldb_msg_add_steal_string(msg, "dITContentRules", ditcontentrule);
252 if (ret != LDB_SUCCESS) {
253 return ret;
257 return LDB_SUCCESS;
260 static int generate_extendedAttributeInfo(struct ldb_context *ldb,
261 struct ldb_message *msg,
262 const struct dsdb_schema *schema)
264 const struct dsdb_attribute *attribute;
265 int ret;
267 for (attribute = schema->attributes; attribute; attribute = attribute->next) {
268 char *val = schema_attribute_to_extendedInfo(msg, attribute);
269 if (!val) {
270 ldb_oom(ldb);
271 return LDB_ERR_OPERATIONS_ERROR;
274 ret = ldb_msg_add_string(msg, "extendedAttributeInfo", val);
275 if (ret != LDB_SUCCESS) {
276 return ret;
280 return LDB_SUCCESS;
283 static int generate_extendedClassInfo(struct ldb_context *ldb,
284 struct ldb_message *msg,
285 const struct dsdb_schema *schema)
287 const struct dsdb_class *sclass;
288 int ret;
290 for (sclass = schema->classes; sclass; sclass = sclass->next) {
291 char *val = schema_class_to_extendedInfo(msg, sclass);
292 if (!val) {
293 ldb_oom(ldb);
294 return LDB_ERR_OPERATIONS_ERROR;
297 ret = ldb_msg_add_string(msg, "extendedClassInfo", val);
298 if (ret != LDB_SUCCESS) {
299 return ret;
303 return LDB_SUCCESS;
307 static int generate_possibleInferiors(struct ldb_context *ldb, struct ldb_message *msg,
308 const struct dsdb_schema *schema)
310 struct ldb_dn *dn = msg->dn;
311 unsigned int i;
312 int ret;
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 unsigned int i;
354 int ret;
356 ac = talloc_get_type(req->context, struct schema_data_search_data);
357 mc = talloc_get_type(ldb_module_get_private(ac->module), struct schema_data_private_data);
358 ldb = ldb_module_get_ctx(ac->module);
360 if (!ares) {
361 return ldb_module_done(ac->req, NULL, NULL,
362 LDB_ERR_OPERATIONS_ERROR);
364 if (ares->error != LDB_SUCCESS) {
365 return ldb_module_done(ac->req, ares->controls,
366 ares->response, ares->error);
368 /* Only entries are interesting, and we handle the case of the parent seperatly */
370 switch (ares->type) {
371 case LDB_REPLY_ENTRY:
373 if (ldb_dn_compare(ares->message->dn, mc->aggregate_dn) == 0) {
374 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
375 if (generated_attrs[i].aggregate &&
376 ldb_attr_in_list(ac->req->op.search.attrs, generated_attrs[i].attr)) {
377 ret = generated_attrs[i].fn(ldb, ares->message, ac->schema);
378 if (ret != LDB_SUCCESS) {
379 return ret;
383 } else if ((ldb_dn_compare_base(mc->schema_dn, ares->message->dn) == 0)
384 && (ldb_dn_compare(mc->schema_dn, ares->message->dn) != 0)) {
385 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
386 if (!generated_attrs[i].aggregate &&
387 ldb_attr_in_list(ac->req->op.search.attrs, generated_attrs[i].attr)) {
388 ret = generated_attrs[i].fn(ldb, ares->message, ac->schema);
389 if (ret != LDB_SUCCESS) {
390 return ret;
397 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
399 case LDB_REPLY_REFERRAL:
401 return ldb_module_send_referral(ac->req, ares->referral);
403 case LDB_REPLY_DONE:
405 return ldb_module_done(ac->req, ares->controls,
406 ares->response, ares->error);
409 return LDB_SUCCESS;
412 /* search */
413 static int schema_data_search(struct ldb_module *module, struct ldb_request *req)
415 struct ldb_context *ldb = ldb_module_get_ctx(module);
416 unsigned int i;
417 int ret;
418 struct schema_data_search_data *search_context;
419 struct ldb_request *down_req;
420 struct dsdb_schema *schema = dsdb_get_schema(ldb, NULL);
422 if (!schema || !ldb_module_get_private(module)) {
423 /* If there is no schema, there is little we can do */
424 return ldb_next_request(module, req);
426 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
427 if (ldb_attr_in_list(req->op.search.attrs, generated_attrs[i].attr)) {
428 break;
431 if (i == ARRAY_SIZE(generated_attrs)) {
432 /* No request for a generated attr found, nothing to
433 * see here, move along... */
434 return ldb_next_request(module, req);
437 search_context = talloc(req, struct schema_data_search_data);
438 if (!search_context) {
439 ldb_oom(ldb);
440 return LDB_ERR_OPERATIONS_ERROR;
443 search_context->module = module;
444 search_context->req = req;
445 search_context->schema = talloc_reference(search_context, schema);
446 if (!search_context->schema) {
447 ldb_oom(ldb);
448 return LDB_ERR_OPERATIONS_ERROR;
451 ret = ldb_build_search_req_ex(&down_req, ldb, search_context,
452 req->op.search.base,
453 req->op.search.scope,
454 req->op.search.tree,
455 req->op.search.attrs,
456 req->controls,
457 search_context, schema_data_search_callback,
458 req);
459 if (ret != LDB_SUCCESS) {
460 return LDB_ERR_OPERATIONS_ERROR;
463 return ldb_next_request(module, down_req);
467 _PUBLIC_ const struct ldb_module_ops ldb_schema_data_module_ops = {
468 .name = "schema_data",
469 .init_context = schema_data_init,
470 .add = schema_data_add,
471 .search = schema_data_search