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/>.
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);
48 int (*fn)(struct ldb_context *, struct ldb_message *, const struct dsdb_schema *);
50 } generated_attrs[] = {
52 .attr = "objectClasses",
53 .fn = generate_objectClasses,
57 .attr = "attributeTypes",
58 .fn = generate_attributeTypes,
62 .attr = "dITContentRules",
63 .fn = generate_dITContentRules,
67 .attr = "extendedAttributeInfo",
68 .fn = generate_extendedAttributeInfo,
72 .attr = "extendedClassInfo",
73 .fn = generate_extendedClassInfo,
77 .attr = "possibleInferiors",
78 .fn = generate_possibleInferiors,
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;
100 struct schema_data_private_data *data;
102 ret = ldb_next_init(module);
103 if (ret != LDB_SUCCESS) {
107 ldb = ldb_module_get_ctx(module);
108 schema_dn = ldb_get_schema_basedn(ldb);
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");
116 data = talloc(module, struct schema_data_private_data);
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);
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;
146 ldb = ldb_module_get_ctx(module);
148 /* special objects should always go through */
149 if (ldb_dn_is_special(req->op.add.message->dn)) {
150 return ldb_next_request(module, req);
153 /* replicated update should always go through */
154 if (ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
155 return ldb_next_request(module, req);
158 schema = dsdb_get_schema(ldb, req);
160 return ldb_next_request(module, req);
163 ret = samdb_rodc(ldb, &rodc);
164 if (ret != LDB_SUCCESS) {
165 DEBUG(4, (__location__ ": unable to tell if we are an RODC \n"));
168 if (!schema->fsmo.we_are_master && !rodc) {
169 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
170 "schema_data_add: we are not master: reject request\n");
171 return LDB_ERR_UNWILLING_TO_PERFORM;
174 attributeID = ldb_msg_find_ldb_val(req->op.add.message, "attributeID");
175 governsID = ldb_msg_find_ldb_val(req->op.add.message, "governsID");
178 oid_attr = "attributeID";
179 oid = talloc_strndup(req, (const char *)attributeID->data, attributeID->length);
180 } else if (governsID) {
181 oid_attr = "governsID";
182 oid = talloc_strndup(req, (const char *)governsID->data, governsID->length);
184 return ldb_next_request(module, req);
191 status = dsdb_schema_pfm_find_oid(schema->prefixmap, oid, NULL);
192 if (!W_ERROR_IS_OK(status)) {
193 /* check for internal errors */
194 if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
195 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
196 "schema_data_add: failed to map %s[%s]: %s\n",
197 oid_attr, oid, win_errstr(status));
198 return LDB_ERR_UNWILLING_TO_PERFORM;
201 /* Update prefixMap and save it */
202 status = dsdb_create_prefix_mapping(ldb, schema, oid);
203 if (!W_ERROR_IS_OK(status)) {
204 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
205 "schema_data_add: failed to create prefix mapping for %s[%s]: %s\n",
206 oid_attr, oid, win_errstr(status));
207 return LDB_ERR_UNWILLING_TO_PERFORM;
211 return ldb_next_request(module, req);
214 static int generate_objectClasses(struct ldb_context *ldb, struct ldb_message *msg,
215 const struct dsdb_schema *schema)
217 const struct dsdb_class *sclass;
220 for (sclass = schema->classes; sclass; sclass = sclass->next) {
221 ret = ldb_msg_add_string(msg, "objectClasses", schema_class_to_description(msg, sclass));
222 if (ret != LDB_SUCCESS) {
228 static int generate_attributeTypes(struct ldb_context *ldb, struct ldb_message *msg,
229 const struct dsdb_schema *schema)
231 const struct dsdb_attribute *attribute;
234 for (attribute = schema->attributes; attribute; attribute = attribute->next) {
235 ret = ldb_msg_add_string(msg, "attributeTypes", schema_attribute_to_description(msg, attribute));
236 if (ret != LDB_SUCCESS) {
243 static int generate_dITContentRules(struct ldb_context *ldb, struct ldb_message *msg,
244 const struct dsdb_schema *schema)
246 const struct dsdb_class *sclass;
249 for (sclass = schema->classes; sclass; sclass = sclass->next) {
250 if (sclass->auxiliaryClass || sclass->systemAuxiliaryClass) {
251 char *ditcontentrule = schema_class_to_dITContentRule(msg, sclass, schema);
252 if (!ditcontentrule) {
255 ret = ldb_msg_add_steal_string(msg, "dITContentRules", ditcontentrule);
256 if (ret != LDB_SUCCESS) {
264 static int generate_extendedAttributeInfo(struct ldb_context *ldb,
265 struct ldb_message *msg,
266 const struct dsdb_schema *schema)
268 const struct dsdb_attribute *attribute;
271 for (attribute = schema->attributes; attribute; attribute = attribute->next) {
272 char *val = schema_attribute_to_extendedInfo(msg, attribute);
277 ret = ldb_msg_add_string(msg, "extendedAttributeInfo", val);
278 if (ret != LDB_SUCCESS) {
286 static int generate_extendedClassInfo(struct ldb_context *ldb,
287 struct ldb_message *msg,
288 const struct dsdb_schema *schema)
290 const struct dsdb_class *sclass;
293 for (sclass = schema->classes; sclass; sclass = sclass->next) {
294 char *val = schema_class_to_extendedInfo(msg, sclass);
299 ret = ldb_msg_add_string(msg, "extendedClassInfo", val);
300 if (ret != LDB_SUCCESS) {
309 static int generate_possibleInferiors(struct ldb_context *ldb, struct ldb_message *msg,
310 const struct dsdb_schema *schema)
312 struct ldb_dn *dn = msg->dn;
315 const char *first_component_name = ldb_dn_get_component_name(dn, 0);
316 const struct ldb_val *first_component_val;
317 const struct dsdb_class *schema_class;
318 const char **possibleInferiors;
320 if (strcasecmp(first_component_name, "cn") != 0) {
324 first_component_val = ldb_dn_get_component_val(dn, 0);
326 schema_class = dsdb_class_by_cn_ldb_val(schema, first_component_val);
327 if (schema_class == NULL) {
331 possibleInferiors = schema_class->possibleInferiors;
332 if (possibleInferiors == NULL) {
336 for (i=0;possibleInferiors[i];i++) {
337 ret = ldb_msg_add_string(msg, "possibleInferiors", possibleInferiors[i]);
338 if (ret != LDB_SUCCESS) {
347 /* Add objectClasses, attributeTypes and dITContentRules from the
348 schema object (they are not stored in the database)
350 static int schema_data_search_callback(struct ldb_request *req, struct ldb_reply *ares)
352 struct ldb_context *ldb;
353 struct schema_data_search_data *ac;
354 struct schema_data_private_data *mc;
358 ac = talloc_get_type(req->context, struct schema_data_search_data);
359 mc = talloc_get_type(ldb_module_get_private(ac->module), struct schema_data_private_data);
360 ldb = ldb_module_get_ctx(ac->module);
363 return ldb_module_done(ac->req, NULL, NULL,
364 LDB_ERR_OPERATIONS_ERROR);
366 if (ares->error != LDB_SUCCESS) {
367 return ldb_module_done(ac->req, ares->controls,
368 ares->response, ares->error);
370 /* Only entries are interesting, and we handle the case of the parent seperatly */
372 switch (ares->type) {
373 case LDB_REPLY_ENTRY:
375 if (ldb_dn_compare(ares->message->dn, mc->aggregate_dn) == 0) {
376 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
377 if (generated_attrs[i].aggregate &&
378 ldb_attr_in_list(ac->req->op.search.attrs, generated_attrs[i].attr)) {
379 ret = generated_attrs[i].fn(ldb, ares->message, ac->schema);
380 if (ret != LDB_SUCCESS) {
385 } else if ((ldb_dn_compare_base(mc->schema_dn, ares->message->dn) == 0)
386 && (ldb_dn_compare(mc->schema_dn, ares->message->dn) != 0)) {
387 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
388 if (!generated_attrs[i].aggregate &&
389 ldb_attr_in_list(ac->req->op.search.attrs, generated_attrs[i].attr)) {
390 ret = generated_attrs[i].fn(ldb, ares->message, ac->schema);
391 if (ret != LDB_SUCCESS) {
399 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
401 case LDB_REPLY_REFERRAL:
403 return ldb_module_send_referral(ac->req, ares->referral);
407 return ldb_module_done(ac->req, ares->controls,
408 ares->response, ares->error);
415 static int schema_data_search(struct ldb_module *module, struct ldb_request *req)
417 struct ldb_context *ldb = ldb_module_get_ctx(module);
420 struct schema_data_search_data *search_context;
421 struct ldb_request *down_req;
422 const struct dsdb_schema *schema;
423 if (!ldb_module_get_private(module)) {
424 /* If there is no module data, there is little we can do */
425 return ldb_next_request(module, req);
428 /* The schema manipulation does not apply to special DNs */
429 if (ldb_dn_is_special(req->op.search.base)) {
430 return ldb_next_request(module, req);
433 for (i=0; i < ARRAY_SIZE(generated_attrs); i++) {
434 if (ldb_attr_in_list(req->op.search.attrs, generated_attrs[i].attr)) {
438 if (i == ARRAY_SIZE(generated_attrs)) {
439 /* No request for a generated attr found, nothing to
440 * see here, move along... */
441 return ldb_next_request(module, req);
444 schema = dsdb_get_schema(ldb, NULL);
445 if (!schema || !ldb_module_get_private(module)) {
446 /* If there is no schema, there is little we can do */
447 return ldb_next_request(module, req);
450 search_context = talloc(req, struct schema_data_search_data);
451 if (!search_context) {
455 search_context->module = module;
456 search_context->req = req;
457 search_context->schema = talloc_reference(search_context, schema);
458 if (!search_context->schema) {
462 ret = ldb_build_search_req_ex(&down_req, ldb, search_context,
464 req->op.search.scope,
466 req->op.search.attrs,
468 search_context, schema_data_search_callback,
470 if (ret != LDB_SUCCESS) {
471 return ldb_operr(ldb);
474 return ldb_next_request(module, down_req);
478 _PUBLIC_ const struct ldb_module_ops ldb_schema_data_module_ops = {
479 .name = "schema_data",
480 .init_context = schema_data_init,
481 .add = schema_data_add,
482 .search = schema_data_search