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
;
142 struct ldb_dn
*parent_dn
= NULL
;
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
, req
);
162 return ldb_next_request(module
, req
);
165 if (schema
->base_dn
== NULL
) {
166 ldb_debug_set(ldb
, LDB_DEBUG_FATAL
,
167 "schema_data_add: base_dn NULL\n");
168 return LDB_ERR_OPERATIONS_ERROR
;
171 ret
= samdb_rodc(ldb
, &rodc
);
172 if (ret
!= LDB_SUCCESS
) {
173 DEBUG(4, (__location__
": unable to tell if we are an RODC \n"));
176 if (!schema
->fsmo
.we_are_master
&& !rodc
) {
177 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
178 "schema_data_add: we are not master: reject request\n");
179 return LDB_ERR_UNWILLING_TO_PERFORM
;
182 if (!schema
->fsmo
.update_allowed
&& !rodc
) {
183 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
184 "schema_data_add: updates are not allowed: reject request\n");
185 return LDB_ERR_UNWILLING_TO_PERFORM
;
188 if (ldb_request_get_control(req
, LDB_CONTROL_RELAX_OID
)) {
190 * the provision code needs to create
191 * the schema root object.
193 cmp
= ldb_dn_compare(req
->op
.add
.message
->dn
, schema
->base_dn
);
195 return ldb_next_request(module
, req
);
199 parent_dn
= ldb_dn_get_parent(req
, req
->op
.add
.message
->dn
);
204 cmp
= ldb_dn_compare(parent_dn
, schema
->base_dn
);
206 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
207 "schema_data_add: no direct child :%s\n",
208 ldb_dn_get_linearized(req
->op
.add
.message
->dn
));
209 return LDB_ERR_UNWILLING_TO_PERFORM
;
212 attributeID
= ldb_msg_find_ldb_val(req
->op
.add
.message
, "attributeID");
213 governsID
= ldb_msg_find_ldb_val(req
->op
.add
.message
, "governsID");
216 oid_attr
= "attributeID";
217 oid
= talloc_strndup(req
, (const char *)attributeID
->data
, attributeID
->length
);
218 } else if (governsID
) {
219 oid_attr
= "governsID";
220 oid
= talloc_strndup(req
, (const char *)governsID
->data
, governsID
->length
);
222 return ldb_next_request(module
, req
);
229 status
= dsdb_schema_pfm_find_oid(schema
->prefixmap
, oid
, NULL
);
230 if (!W_ERROR_IS_OK(status
)) {
231 /* check for internal errors */
232 if (!W_ERROR_EQUAL(status
, WERR_NOT_FOUND
)) {
233 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
234 "schema_data_add: failed to map %s[%s]: %s\n",
235 oid_attr
, oid
, win_errstr(status
));
236 return LDB_ERR_UNWILLING_TO_PERFORM
;
239 /* Update prefixMap and save it */
240 status
= dsdb_create_prefix_mapping(ldb
, schema
, oid
);
241 if (!W_ERROR_IS_OK(status
)) {
242 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
243 "schema_data_add: failed to create prefix mapping for %s[%s]: %s\n",
244 oid_attr
, oid
, win_errstr(status
));
245 return LDB_ERR_UNWILLING_TO_PERFORM
;
249 return ldb_next_request(module
, req
);
252 static int schema_data_modify(struct ldb_module
*module
, struct ldb_request
*req
)
254 struct ldb_context
*ldb
;
255 struct dsdb_schema
*schema
;
259 struct ldb_control
*sd_propagation_control
;
261 ldb
= ldb_module_get_ctx(module
);
263 /* special objects should always go through */
264 if (ldb_dn_is_special(req
->op
.mod
.message
->dn
)) {
265 return ldb_next_request(module
, req
);
268 /* replicated update should always go through */
269 if (ldb_request_get_control(req
, DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
270 return ldb_next_request(module
, req
);
273 /* dbcheck should be able to fix things */
274 if (ldb_request_get_control(req
, DSDB_CONTROL_DBCHECK
)) {
275 return ldb_next_request(module
, req
);
278 sd_propagation_control
= ldb_request_get_control(req
,
279 DSDB_CONTROL_SEC_DESC_PROPAGATION_OID
);
280 if (sd_propagation_control
!= NULL
) {
281 if (req
->op
.mod
.message
->num_elements
!= 1) {
282 return ldb_module_operr(module
);
284 ret
= strcmp(req
->op
.mod
.message
->elements
[0].name
,
285 "nTSecurityDescriptor");
287 return ldb_module_operr(module
);
290 return ldb_next_request(module
, req
);
293 schema
= dsdb_get_schema(ldb
, req
);
295 return ldb_next_request(module
, req
);
298 cmp
= ldb_dn_compare(req
->op
.mod
.message
->dn
, schema
->base_dn
);
300 static const char * const constrained_attrs
[] = {
303 "msDs-Schema-Extensions",
308 struct ldb_message_element
*el
;
310 if (ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
)) {
311 return ldb_next_request(module
, req
);
314 for (i
=0; constrained_attrs
[i
]; i
++) {
315 el
= ldb_msg_find_element(req
->op
.mod
.message
,
316 constrained_attrs
[i
]);
321 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
322 "schema_data_modify: reject update "
323 "of attribute[%s]\n",
324 constrained_attrs
[i
]);
325 return LDB_ERR_CONSTRAINT_VIOLATION
;
328 return ldb_next_request(module
, req
);
331 ret
= samdb_rodc(ldb
, &rodc
);
332 if (ret
!= LDB_SUCCESS
) {
333 DEBUG(4, (__location__
": unable to tell if we are an RODC \n"));
336 if (!schema
->fsmo
.we_are_master
&& !rodc
) {
337 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
338 "schema_data_modify: we are not master: reject request\n");
339 return LDB_ERR_UNWILLING_TO_PERFORM
;
342 if (!schema
->fsmo
.update_allowed
&& !rodc
) {
343 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
344 "schema_data_modify: updates are not allowed: reject request\n");
345 return LDB_ERR_UNWILLING_TO_PERFORM
;
348 return ldb_next_request(module
, req
);
351 static int schema_data_del(struct ldb_module
*module
, struct ldb_request
*req
)
353 struct ldb_context
*ldb
;
354 struct dsdb_schema
*schema
;
358 ldb
= ldb_module_get_ctx(module
);
360 /* special objects should always go through */
361 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
362 return ldb_next_request(module
, req
);
365 /* replicated update should always go through */
366 if (ldb_request_get_control(req
, DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
367 return ldb_next_request(module
, req
);
370 /* dbcheck should be able to fix things */
371 if (ldb_request_get_control(req
, DSDB_CONTROL_DBCHECK
)) {
372 return ldb_next_request(module
, req
);
375 schema
= dsdb_get_schema(ldb
, req
);
377 return ldb_next_request(module
, req
);
380 ret
= samdb_rodc(ldb
, &rodc
);
381 if (ret
!= LDB_SUCCESS
) {
382 DEBUG(4, (__location__
": unable to tell if we are an RODC \n"));
385 if (!schema
->fsmo
.we_are_master
&& !rodc
) {
386 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
387 "schema_data_modify: we are not master: reject request\n");
388 return LDB_ERR_UNWILLING_TO_PERFORM
;
392 * normaly the DACL will prevent delete
393 * with LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
396 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
397 "schema_data_del: delete is not allowed in the schema\n");
398 return LDB_ERR_UNWILLING_TO_PERFORM
;
401 static int generate_objectClasses(struct ldb_context
*ldb
, struct ldb_message
*msg
,
402 const struct dsdb_schema
*schema
)
404 const struct dsdb_class
*sclass
;
407 for (sclass
= schema
->classes
; sclass
; sclass
= sclass
->next
) {
408 char *v
= schema_class_to_description(msg
, sclass
);
412 ret
= ldb_msg_add_steal_string(msg
, "objectClasses", v
);
413 if (ret
!= LDB_SUCCESS
) {
419 static int generate_attributeTypes(struct ldb_context
*ldb
, struct ldb_message
*msg
,
420 const struct dsdb_schema
*schema
)
422 const struct dsdb_attribute
*attribute
;
425 for (attribute
= schema
->attributes
; attribute
; attribute
= attribute
->next
) {
426 char *v
= schema_attribute_to_description(msg
, attribute
);
430 ret
= ldb_msg_add_steal_string(msg
, "attributeTypes", v
);
431 if (ret
!= LDB_SUCCESS
) {
438 static int generate_dITContentRules(struct ldb_context
*ldb
, struct ldb_message
*msg
,
439 const struct dsdb_schema
*schema
)
441 const struct dsdb_class
*sclass
;
444 for (sclass
= schema
->classes
; sclass
; sclass
= sclass
->next
) {
445 if (sclass
->auxiliaryClass
|| sclass
->systemAuxiliaryClass
) {
446 char *ditcontentrule
= schema_class_to_dITContentRule(msg
, sclass
, schema
);
447 if (!ditcontentrule
) {
450 ret
= ldb_msg_add_steal_string(msg
, "dITContentRules", ditcontentrule
);
451 if (ret
!= LDB_SUCCESS
) {
459 static int generate_extendedAttributeInfo(struct ldb_context
*ldb
,
460 struct ldb_message
*msg
,
461 const struct dsdb_schema
*schema
)
463 const struct dsdb_attribute
*attribute
;
466 for (attribute
= schema
->attributes
; attribute
; attribute
= attribute
->next
) {
467 char *val
= schema_attribute_to_extendedInfo(msg
, attribute
);
472 ret
= ldb_msg_add_steal_string(msg
, "extendedAttributeInfo", val
);
473 if (ret
!= LDB_SUCCESS
) {
481 static int generate_extendedClassInfo(struct ldb_context
*ldb
,
482 struct ldb_message
*msg
,
483 const struct dsdb_schema
*schema
)
485 const struct dsdb_class
*sclass
;
488 for (sclass
= schema
->classes
; sclass
; sclass
= sclass
->next
) {
489 char *val
= schema_class_to_extendedInfo(msg
, sclass
);
494 ret
= ldb_msg_add_steal_string(msg
, "extendedClassInfo", val
);
495 if (ret
!= LDB_SUCCESS
) {
504 static int generate_possibleInferiors(struct ldb_context
*ldb
, struct ldb_message
*msg
,
505 const struct dsdb_schema
*schema
)
507 struct ldb_dn
*dn
= msg
->dn
;
510 const char *first_component_name
= ldb_dn_get_component_name(dn
, 0);
511 const struct ldb_val
*first_component_val
;
512 const struct dsdb_class
*schema_class
;
513 const char **possibleInferiors
;
515 if (strcasecmp(first_component_name
, "cn") != 0) {
519 first_component_val
= ldb_dn_get_component_val(dn
, 0);
521 schema_class
= dsdb_class_by_cn_ldb_val(schema
, first_component_val
);
522 if (schema_class
== NULL
) {
526 possibleInferiors
= schema_class
->possibleInferiors
;
527 if (possibleInferiors
== NULL
) {
531 for (i
=0;possibleInferiors
[i
];i
++) {
532 char *v
= talloc_strdup(msg
, possibleInferiors
[i
]);
536 ret
= ldb_msg_add_steal_string(msg
, "possibleInferiors", v
);
537 if (ret
!= LDB_SUCCESS
) {
546 /* Add objectClasses, attributeTypes and dITContentRules from the
547 schema object (they are not stored in the database)
549 static int schema_data_search_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
551 struct ldb_context
*ldb
;
552 struct schema_data_search_data
*ac
;
553 struct schema_data_private_data
*mc
;
557 ac
= talloc_get_type(req
->context
, struct schema_data_search_data
);
558 mc
= talloc_get_type(ldb_module_get_private(ac
->module
), struct schema_data_private_data
);
559 ldb
= ldb_module_get_ctx(ac
->module
);
562 return ldb_module_done(ac
->req
, NULL
, NULL
,
563 LDB_ERR_OPERATIONS_ERROR
);
565 if (ares
->error
!= LDB_SUCCESS
) {
566 return ldb_module_done(ac
->req
, ares
->controls
,
567 ares
->response
, ares
->error
);
569 /* Only entries are interesting, and we handle the case of the parent seperatly */
571 switch (ares
->type
) {
572 case LDB_REPLY_ENTRY
:
574 if (ldb_dn_compare(ares
->message
->dn
, mc
->aggregate_dn
) == 0) {
575 for (i
=0; i
< ARRAY_SIZE(generated_attrs
); i
++) {
576 if (generated_attrs
[i
].aggregate
&&
577 ldb_attr_in_list(ac
->req
->op
.search
.attrs
, generated_attrs
[i
].attr
)) {
578 ret
= generated_attrs
[i
].fn(ldb
, ares
->message
, ac
->schema
);
579 if (ret
!= LDB_SUCCESS
) {
584 } else if ((ldb_dn_compare_base(mc
->schema_dn
, ares
->message
->dn
) == 0)
585 && (ldb_dn_compare(mc
->schema_dn
, ares
->message
->dn
) != 0)) {
586 for (i
=0; i
< ARRAY_SIZE(generated_attrs
); i
++) {
587 if (!generated_attrs
[i
].aggregate
&&
588 ldb_attr_in_list(ac
->req
->op
.search
.attrs
, generated_attrs
[i
].attr
)) {
589 ret
= generated_attrs
[i
].fn(ldb
, ares
->message
, ac
->schema
);
590 if (ret
!= LDB_SUCCESS
) {
598 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
600 case LDB_REPLY_REFERRAL
:
602 return ldb_module_send_referral(ac
->req
, ares
->referral
);
606 return ldb_module_done(ac
->req
, ares
->controls
,
607 ares
->response
, ares
->error
);
614 static int schema_data_search(struct ldb_module
*module
, struct ldb_request
*req
)
616 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
619 struct schema_data_search_data
*search_context
;
620 struct ldb_request
*down_req
;
621 const struct dsdb_schema
*schema
;
622 if (!ldb_module_get_private(module
)) {
623 /* If there is no module data, there is little we can do */
624 return ldb_next_request(module
, req
);
627 /* The schema manipulation does not apply to special DNs */
628 if (ldb_dn_is_special(req
->op
.search
.base
)) {
629 return ldb_next_request(module
, req
);
632 for (i
=0; i
< ARRAY_SIZE(generated_attrs
); i
++) {
633 if (ldb_attr_in_list(req
->op
.search
.attrs
, generated_attrs
[i
].attr
)) {
637 if (i
== ARRAY_SIZE(generated_attrs
)) {
638 /* No request for a generated attr found, nothing to
639 * see here, move along... */
640 return ldb_next_request(module
, req
);
643 schema
= dsdb_get_schema(ldb
, NULL
);
644 if (!schema
|| !ldb_module_get_private(module
)) {
645 /* If there is no schema, there is little we can do */
646 return ldb_next_request(module
, req
);
649 search_context
= talloc(req
, struct schema_data_search_data
);
650 if (!search_context
) {
654 search_context
->module
= module
;
655 search_context
->req
= req
;
656 search_context
->schema
= talloc_reference(search_context
, schema
);
657 if (!search_context
->schema
) {
661 ret
= ldb_build_search_req_ex(&down_req
, ldb
, search_context
,
663 req
->op
.search
.scope
,
665 req
->op
.search
.attrs
,
667 search_context
, schema_data_search_callback
,
669 LDB_REQ_SET_LOCATION(down_req
);
670 if (ret
!= LDB_SUCCESS
) {
671 return ldb_operr(ldb
);
674 return ldb_next_request(module
, down_req
);
678 static const struct ldb_module_ops ldb_schema_data_module_ops
= {
679 .name
= "schema_data",
680 .init_context
= schema_data_init
,
681 .add
= schema_data_add
,
682 .modify
= schema_data_modify
,
683 .del
= schema_data_del
,
684 .search
= schema_data_search
687 int ldb_schema_data_module_init(const char *version
)
689 LDB_MODULE_CHECK_VERSION(version
);
690 return ldb_register_module(&ldb_schema_data_module_ops
);