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 /* context to be used during async operations */
96 struct schema_data_context
{
97 struct ldb_module
*module
;
98 struct ldb_request
*req
;
100 const struct dsdb_schema
*schema
;
103 /* Create new context using
104 * ldb_request as memory context */
105 static int _schema_data_context_new(struct ldb_module
*module
,
106 struct ldb_request
*req
,
107 struct schema_data_context
**pac
)
109 struct schema_data_context
*ac
;
110 struct ldb_context
*ldb
;
112 ldb
= ldb_module_get_ctx(module
);
114 *pac
= ac
= talloc_zero(req
, struct schema_data_context
);
117 return LDB_ERR_OPERATIONS_ERROR
;
121 ac
->schema
= dsdb_get_schema(ldb
);
126 static int schema_data_init(struct ldb_module
*module
)
128 struct ldb_context
*ldb
;
129 struct ldb_dn
*schema_dn
;
131 struct schema_data_private_data
*data
;
133 ret
= ldb_next_init(module
);
134 if (ret
!= LDB_SUCCESS
) {
138 ldb
= ldb_module_get_ctx(module
);
139 schema_dn
= samdb_schema_dn(ldb
);
141 ldb_reset_err_string(ldb
);
142 ldb_debug(ldb
, LDB_DEBUG_WARNING
,
143 "schema_data_init: no schema dn present: (skip schema loading)\n");
147 data
= talloc(module
, struct schema_data_private_data
);
150 return LDB_ERR_OPERATIONS_ERROR
;
153 data
->schema_dn
= schema_dn
;
155 /* Used to check to see if this is a result on the CN=Aggregate schema */
156 data
->aggregate_dn
= samdb_aggregate_schema_dn(ldb
, data
);
157 if (!data
->aggregate_dn
) {
158 ldb_set_errstring(ldb
, "Could not build aggregate schema DN");
159 return LDB_ERR_OPERATIONS_ERROR
;
162 ldb_module_set_private(module
, data
);
167 /* Generate new value for msDs-IntId
168 * Value should be in 0x80000000..0xBFFFFFFF range
169 * Generated value is added ldb_msg */
170 static int _schema_data_gen_msds_intid(struct schema_data_context
*ac
,
171 struct ldb_message
*ldb_msg
)
175 /* generate random num in 0x80000000..0xBFFFFFFF */
176 id
= generate_random() % 0X3FFFFFFF;
179 /* make sure id is unique and adjust if not */
180 while (dsdb_attribute_by_attributeID_id(ac
->schema
, id
)) {
182 if (id
> 0xBFFFFFFF) {
187 /* add generated msDS-IntId value to ldb_msg */
188 return ldb_msg_add_fmt(ldb_msg
, "msDS-IntId", "%d", id
);
191 static int _schema_data_add_callback(struct ldb_request
*req
,
192 struct ldb_reply
*ares
)
194 struct schema_data_context
*ac
;
196 ac
= talloc_get_type(req
->context
, struct schema_data_context
);
199 return ldb_module_done(ac
->req
, NULL
, NULL
,
200 LDB_ERR_OPERATIONS_ERROR
);
202 if (ares
->error
!= LDB_SUCCESS
) {
203 return ldb_module_done(ac
->req
, ares
->controls
,
204 ares
->response
, ares
->error
);
207 if (ares
->type
!= LDB_REPLY_DONE
) {
209 return ldb_module_done(ac
->req
, NULL
, NULL
,
210 LDB_ERR_OPERATIONS_ERROR
);
213 return ldb_module_done(ac
->req
, ares
->controls
,
214 ares
->response
, ares
->error
);
217 static int schema_data_add(struct ldb_module
*module
, struct ldb_request
*req
)
219 struct ldb_context
*ldb
;
220 struct dsdb_schema
*schema
;
221 const struct ldb_val
*attributeID
= NULL
;
222 const struct ldb_val
*governsID
= NULL
;
223 const char *oid_attr
= NULL
;
224 const char *oid
= NULL
;
227 ldb
= ldb_module_get_ctx(module
);
229 /* special objects should always go through */
230 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
231 return ldb_next_request(module
, req
);
234 /* replicated update should always go through */
235 if (ldb_request_get_control(req
, DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
236 return ldb_next_request(module
, req
);
239 schema
= dsdb_get_schema(ldb
);
241 return ldb_next_request(module
, req
);
244 if (!schema
->fsmo
.we_are_master
) {
245 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
246 "schema_data_add: we are not master: reject request\n");
247 return LDB_ERR_UNWILLING_TO_PERFORM
;
250 attributeID
= ldb_msg_find_ldb_val(req
->op
.add
.message
, "attributeID");
251 governsID
= ldb_msg_find_ldb_val(req
->op
.add
.message
, "governsID");
254 /* Sanity check for not allowed attributes */
255 if (ldb_msg_find_ldb_val(req
->op
.add
.message
, "msDS-IntId")) {
256 return LDB_ERR_UNWILLING_TO_PERFORM
;
259 oid_attr
= "attributeID";
260 oid
= talloc_strndup(req
, (const char *)attributeID
->data
, attributeID
->length
);
261 } else if (governsID
) {
262 oid_attr
= "governsID";
263 oid
= talloc_strndup(req
, (const char *)governsID
->data
, governsID
->length
);
265 return ldb_next_request(module
, req
);
270 return LDB_ERR_OPERATIONS_ERROR
;
273 status
= dsdb_schema_pfm_find_oid(schema
->prefixmap
, oid
, NULL
);
274 if (!W_ERROR_IS_OK(status
)) {
275 /* check for internal errors */
276 if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID
, status
)) {
277 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
278 "schema_data_add: failed to map %s[%s]: %s\n",
279 oid_attr
, oid
, win_errstr(status
));
280 return LDB_ERR_UNWILLING_TO_PERFORM
;
283 /* Update prefixMap and save it */
284 status
= dsdb_create_prefix_mapping(ldb
, schema
, oid
);
285 if (!W_ERROR_IS_OK(status
)) {
286 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
287 "schema_data_add: failed to create prefix mapping for %s[%s]: %s\n",
288 oid_attr
, oid
, win_errstr(status
));
289 return LDB_ERR_UNWILLING_TO_PERFORM
;
293 /* bypass further processing if CONTROL_RELAX is set */
294 if (ldb_request_get_control(req
, LDB_CONTROL_RELAX_OID
)) {
295 return ldb_next_request(module
, req
);
298 /* generate and add msDS-IntId attr value */
300 && (dsdb_functional_level(ldb
) >= DS_DOMAIN_FUNCTION_2003
)
301 && !(ldb_msg_find_attr_as_uint(req
->op
.add
.message
, "systemFlags", 0) & SYSTEM_FLAG_SCHEMA_BASE_OBJECT
)) {
302 struct ldb_message
*msg
;
303 struct schema_data_context
*ac
;
304 struct ldb_request
*add_req
;
306 if (_schema_data_context_new(module
, req
, &ac
) != LDB_SUCCESS
) {
307 return LDB_ERR_OPERATIONS_ERROR
;
310 /* we have to copy the message as the caller might have it as a const */
311 msg
= ldb_msg_copy_shallow(ac
, req
->op
.add
.message
);
314 return LDB_ERR_OPERATIONS_ERROR
;
317 /* generate unique value for msDS-IntId attr value */
318 if (_schema_data_gen_msds_intid(ac
, msg
) != LDB_SUCCESS
) {
319 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
320 "_schema_data_gen_msds_intid() failed to generate msDS-IntId value\n");
321 return LDB_ERR_OPERATIONS_ERROR
;
324 ldb_build_add_req(&add_req
, ldb
, ac
,
327 ac
, _schema_data_add_callback
,
330 return ldb_next_request(module
, add_req
);
333 return ldb_next_request(module
, req
);
336 static int schema_data_modify(struct ldb_module
*module
, struct ldb_request
*req
)
338 /* special objects should always go through */
339 if (ldb_dn_is_special(req
->op
.mod
.message
->dn
)) {
340 return ldb_next_request(module
, req
);
343 /* replicated update should always go through */
344 if (ldb_request_get_control(req
, DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
345 return ldb_next_request(module
, req
);
348 /* msDS-IntId is not allowed to be modified */
349 if (ldb_msg_find_ldb_val(req
->op
.mod
.message
, "msDS-IntId")) {
350 return LDB_ERR_CONSTRAINT_VIOLATION
;
353 /* go on with the call chain */
354 return ldb_next_request(module
, req
);
357 static int generate_objectClasses(struct ldb_context
*ldb
, struct ldb_message
*msg
,
358 const struct dsdb_schema
*schema
)
360 const struct dsdb_class
*sclass
;
363 for (sclass
= schema
->classes
; sclass
; sclass
= sclass
->next
) {
364 ret
= ldb_msg_add_string(msg
, "objectClasses", schema_class_to_description(msg
, sclass
));
365 if (ret
!= LDB_SUCCESS
) {
371 static int generate_attributeTypes(struct ldb_context
*ldb
, struct ldb_message
*msg
,
372 const struct dsdb_schema
*schema
)
374 const struct dsdb_attribute
*attribute
;
377 for (attribute
= schema
->attributes
; attribute
; attribute
= attribute
->next
) {
378 ret
= ldb_msg_add_string(msg
, "attributeTypes", schema_attribute_to_description(msg
, attribute
));
379 if (ret
!= LDB_SUCCESS
) {
386 static int generate_dITContentRules(struct ldb_context
*ldb
, struct ldb_message
*msg
,
387 const struct dsdb_schema
*schema
)
389 const struct dsdb_class
*sclass
;
392 for (sclass
= schema
->classes
; sclass
; sclass
= sclass
->next
) {
393 if (sclass
->auxiliaryClass
|| sclass
->systemAuxiliaryClass
) {
394 char *ditcontentrule
= schema_class_to_dITContentRule(msg
, sclass
, schema
);
395 if (!ditcontentrule
) {
397 return LDB_ERR_OPERATIONS_ERROR
;
399 ret
= ldb_msg_add_steal_string(msg
, "dITContentRules", ditcontentrule
);
400 if (ret
!= LDB_SUCCESS
) {
408 static int generate_extendedAttributeInfo(struct ldb_context
*ldb
,
409 struct ldb_message
*msg
,
410 const struct dsdb_schema
*schema
)
412 const struct dsdb_attribute
*attribute
;
415 for (attribute
= schema
->attributes
; attribute
; attribute
= attribute
->next
) {
416 char *val
= schema_attribute_to_extendedInfo(msg
, attribute
);
419 return LDB_ERR_OPERATIONS_ERROR
;
422 ret
= ldb_msg_add_string(msg
, "extendedAttributeInfo", val
);
423 if (ret
!= LDB_SUCCESS
) {
431 static int generate_extendedClassInfo(struct ldb_context
*ldb
,
432 struct ldb_message
*msg
,
433 const struct dsdb_schema
*schema
)
435 const struct dsdb_class
*sclass
;
438 for (sclass
= schema
->classes
; sclass
; sclass
= sclass
->next
) {
439 char *val
= schema_class_to_extendedInfo(msg
, sclass
);
442 return LDB_ERR_OPERATIONS_ERROR
;
445 ret
= ldb_msg_add_string(msg
, "extendedClassInfo", val
);
446 if (ret
!= LDB_SUCCESS
) {
455 static int generate_possibleInferiors(struct ldb_context
*ldb
, struct ldb_message
*msg
,
456 const struct dsdb_schema
*schema
)
458 struct ldb_dn
*dn
= msg
->dn
;
460 const char *first_component_name
= ldb_dn_get_component_name(dn
, 0);
461 const struct ldb_val
*first_component_val
;
462 const struct dsdb_class
*schema_class
;
463 const char **possibleInferiors
;
465 if (strcasecmp(first_component_name
, "cn") != 0) {
469 first_component_val
= ldb_dn_get_component_val(dn
, 0);
471 schema_class
= dsdb_class_by_cn_ldb_val(schema
, first_component_val
);
472 if (schema_class
== NULL
) {
476 possibleInferiors
= schema_class
->possibleInferiors
;
477 if (possibleInferiors
== NULL
) {
481 for (i
=0;possibleInferiors
[i
];i
++) {
482 ret
= ldb_msg_add_string(msg
, "possibleInferiors", possibleInferiors
[i
]);
483 if (ret
!= LDB_SUCCESS
) {
492 /* Add objectClasses, attributeTypes and dITContentRules from the
493 schema object (they are not stored in the database)
495 static int schema_data_search_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
497 struct ldb_context
*ldb
;
498 struct schema_data_search_data
*ac
;
499 struct schema_data_private_data
*mc
;
502 ac
= talloc_get_type(req
->context
, struct schema_data_search_data
);
503 mc
= talloc_get_type(ldb_module_get_private(ac
->module
), struct schema_data_private_data
);
504 ldb
= ldb_module_get_ctx(ac
->module
);
507 return ldb_module_done(ac
->req
, NULL
, NULL
,
508 LDB_ERR_OPERATIONS_ERROR
);
510 if (ares
->error
!= LDB_SUCCESS
) {
511 return ldb_module_done(ac
->req
, ares
->controls
,
512 ares
->response
, ares
->error
);
514 /* Only entries are interesting, and we handle the case of the parent seperatly */
516 switch (ares
->type
) {
517 case LDB_REPLY_ENTRY
:
519 if (ldb_dn_compare(ares
->message
->dn
, mc
->aggregate_dn
) == 0) {
520 for (i
=0; i
< ARRAY_SIZE(generated_attrs
); i
++) {
521 if (generated_attrs
[i
].aggregate
&&
522 ldb_attr_in_list(ac
->req
->op
.search
.attrs
, generated_attrs
[i
].attr
)) {
523 ret
= generated_attrs
[i
].fn(ldb
, ares
->message
, ac
->schema
);
524 if (ret
!= LDB_SUCCESS
) {
529 } else if ((ldb_dn_compare_base(mc
->schema_dn
, ares
->message
->dn
) == 0)
530 && (ldb_dn_compare(mc
->schema_dn
, ares
->message
->dn
) != 0)) {
531 for (i
=0; i
< ARRAY_SIZE(generated_attrs
); i
++) {
532 if (!generated_attrs
[i
].aggregate
&&
533 ldb_attr_in_list(ac
->req
->op
.search
.attrs
, generated_attrs
[i
].attr
)) {
534 ret
= generated_attrs
[i
].fn(ldb
, ares
->message
, ac
->schema
);
535 if (ret
!= LDB_SUCCESS
) {
543 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
545 case LDB_REPLY_REFERRAL
:
547 return ldb_module_send_referral(ac
->req
, ares
->referral
);
551 return ldb_module_done(ac
->req
, ares
->controls
,
552 ares
->response
, ares
->error
);
559 static int schema_data_search(struct ldb_module
*module
, struct ldb_request
*req
)
561 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
563 struct schema_data_search_data
*search_context
;
564 struct ldb_request
*down_req
;
565 struct dsdb_schema
*schema
= dsdb_get_schema(ldb
);
567 if (!schema
|| !ldb_module_get_private(module
)) {
568 /* If there is no schema, there is little we can do */
569 return ldb_next_request(module
, req
);
571 for (i
=0; i
< ARRAY_SIZE(generated_attrs
); i
++) {
572 if (ldb_attr_in_list(req
->op
.search
.attrs
, generated_attrs
[i
].attr
)) {
576 if (i
== ARRAY_SIZE(generated_attrs
)) {
577 /* No request for a generated attr found, nothing to
578 * see here, move along... */
579 return ldb_next_request(module
, req
);
582 search_context
= talloc(req
, struct schema_data_search_data
);
583 if (!search_context
) {
585 return LDB_ERR_OPERATIONS_ERROR
;
588 search_context
->module
= module
;
589 search_context
->req
= req
;
590 search_context
->schema
= schema
;
592 ret
= ldb_build_search_req_ex(&down_req
, ldb
, search_context
,
594 req
->op
.search
.scope
,
596 req
->op
.search
.attrs
,
598 search_context
, schema_data_search_callback
,
600 if (ret
!= LDB_SUCCESS
) {
601 return LDB_ERR_OPERATIONS_ERROR
;
604 return ldb_next_request(module
, down_req
);
608 _PUBLIC_
const struct ldb_module_ops ldb_schema_data_module_ops
= {
609 .name
= "schema_data",
610 .init_context
= schema_data_init
,
611 .add
= schema_data_add
,
612 .modify
= schema_data_modify
,
613 .search
= schema_data_search