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
= samdb_schema_dn(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
);
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
);
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
;
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
);
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");
172 /* Sanity check for not allowed attributes */
173 if (ldb_msg_find_ldb_val(req
->op
.add
.message
, "msDS-IntId")) {
174 return LDB_ERR_UNWILLING_TO_PERFORM
;
177 oid_attr
= "attributeID";
178 oid
= talloc_strndup(req
, (const char *)attributeID
->data
, attributeID
->length
);
179 } else if (governsID
) {
180 oid_attr
= "governsID";
181 oid
= talloc_strndup(req
, (const char *)governsID
->data
, governsID
->length
);
183 return ldb_next_request(module
, req
);
188 return LDB_ERR_OPERATIONS_ERROR
;
191 status
= dsdb_schema_pfm_find_oid(schema
->prefixmap
, oid
, NULL
);
192 if (W_ERROR_IS_OK(status
)) {
193 return ldb_next_request(module
, req
);
194 } else 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 status
= dsdb_create_prefix_mapping(ldb
, schema
, oid
);
202 if (!W_ERROR_IS_OK(status
)) {
203 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
204 "schema_data_add: failed to create prefix mapping for %s[%s]: %s\n",
205 oid_attr
, oid
, win_errstr(status
));
206 return LDB_ERR_UNWILLING_TO_PERFORM
;
209 return ldb_next_request(module
, req
);
212 static int schema_data_modify(struct ldb_module
*module
, struct ldb_request
*req
)
214 /* special objects should always go through */
215 if (ldb_dn_is_special(req
->op
.mod
.message
->dn
)) {
216 return ldb_next_request(module
, req
);
219 /* replicated update should always go through */
220 if (ldb_request_get_control(req
, DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
221 return ldb_next_request(module
, req
);
224 /* msDS-IntId is not allowed to be modified */
225 if (ldb_msg_find_ldb_val(req
->op
.mod
.message
, "msDS-IntId")) {
226 return LDB_ERR_CONSTRAINT_VIOLATION
;
229 /* go on with the call chain */
230 return ldb_next_request(module
, req
);
233 static int generate_objectClasses(struct ldb_context
*ldb
, struct ldb_message
*msg
,
234 const struct dsdb_schema
*schema
)
236 const struct dsdb_class
*sclass
;
239 for (sclass
= schema
->classes
; sclass
; sclass
= sclass
->next
) {
240 ret
= ldb_msg_add_string(msg
, "objectClasses", schema_class_to_description(msg
, sclass
));
241 if (ret
!= LDB_SUCCESS
) {
247 static int generate_attributeTypes(struct ldb_context
*ldb
, struct ldb_message
*msg
,
248 const struct dsdb_schema
*schema
)
250 const struct dsdb_attribute
*attribute
;
253 for (attribute
= schema
->attributes
; attribute
; attribute
= attribute
->next
) {
254 ret
= ldb_msg_add_string(msg
, "attributeTypes", schema_attribute_to_description(msg
, attribute
));
255 if (ret
!= LDB_SUCCESS
) {
262 static int generate_dITContentRules(struct ldb_context
*ldb
, struct ldb_message
*msg
,
263 const struct dsdb_schema
*schema
)
265 const struct dsdb_class
*sclass
;
268 for (sclass
= schema
->classes
; sclass
; sclass
= sclass
->next
) {
269 if (sclass
->auxiliaryClass
|| sclass
->systemAuxiliaryClass
) {
270 char *ditcontentrule
= schema_class_to_dITContentRule(msg
, sclass
, schema
);
271 if (!ditcontentrule
) {
273 return LDB_ERR_OPERATIONS_ERROR
;
275 ret
= ldb_msg_add_steal_string(msg
, "dITContentRules", ditcontentrule
);
276 if (ret
!= LDB_SUCCESS
) {
284 static int generate_extendedAttributeInfo(struct ldb_context
*ldb
,
285 struct ldb_message
*msg
,
286 const struct dsdb_schema
*schema
)
288 const struct dsdb_attribute
*attribute
;
291 for (attribute
= schema
->attributes
; attribute
; attribute
= attribute
->next
) {
292 char *val
= schema_attribute_to_extendedInfo(msg
, attribute
);
295 return LDB_ERR_OPERATIONS_ERROR
;
298 ret
= ldb_msg_add_string(msg
, "extendedAttributeInfo", val
);
299 if (ret
!= LDB_SUCCESS
) {
307 static int generate_extendedClassInfo(struct ldb_context
*ldb
,
308 struct ldb_message
*msg
,
309 const struct dsdb_schema
*schema
)
311 const struct dsdb_class
*sclass
;
314 for (sclass
= schema
->classes
; sclass
; sclass
= sclass
->next
) {
315 char *val
= schema_class_to_extendedInfo(msg
, sclass
);
318 return LDB_ERR_OPERATIONS_ERROR
;
321 ret
= ldb_msg_add_string(msg
, "extendedClassInfo", val
);
322 if (ret
!= LDB_SUCCESS
) {
331 static int generate_possibleInferiors(struct ldb_context
*ldb
, struct ldb_message
*msg
,
332 const struct dsdb_schema
*schema
)
334 struct ldb_dn
*dn
= msg
->dn
;
336 const char *first_component_name
= ldb_dn_get_component_name(dn
, 0);
337 const struct ldb_val
*first_component_val
;
338 const struct dsdb_class
*schema_class
;
339 const char **possibleInferiors
;
341 if (strcasecmp(first_component_name
, "cn") != 0) {
345 first_component_val
= ldb_dn_get_component_val(dn
, 0);
347 schema_class
= dsdb_class_by_cn_ldb_val(schema
, first_component_val
);
348 if (schema_class
== NULL
) {
352 possibleInferiors
= schema_class
->possibleInferiors
;
353 if (possibleInferiors
== NULL
) {
357 for (i
=0;possibleInferiors
[i
];i
++) {
358 ret
= ldb_msg_add_string(msg
, "possibleInferiors", possibleInferiors
[i
]);
359 if (ret
!= LDB_SUCCESS
) {
368 /* Add objectClasses, attributeTypes and dITContentRules from the
369 schema object (they are not stored in the database)
371 static int schema_data_search_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
373 struct ldb_context
*ldb
;
374 struct schema_data_search_data
*ac
;
375 struct schema_data_private_data
*mc
;
378 ac
= talloc_get_type(req
->context
, struct schema_data_search_data
);
379 mc
= talloc_get_type(ldb_module_get_private(ac
->module
), struct schema_data_private_data
);
380 ldb
= ldb_module_get_ctx(ac
->module
);
383 return ldb_module_done(ac
->req
, NULL
, NULL
,
384 LDB_ERR_OPERATIONS_ERROR
);
386 if (ares
->error
!= LDB_SUCCESS
) {
387 return ldb_module_done(ac
->req
, ares
->controls
,
388 ares
->response
, ares
->error
);
390 /* Only entries are interesting, and we handle the case of the parent seperatly */
392 switch (ares
->type
) {
393 case LDB_REPLY_ENTRY
:
395 if (ldb_dn_compare(ares
->message
->dn
, mc
->aggregate_dn
) == 0) {
396 for (i
=0; i
< ARRAY_SIZE(generated_attrs
); i
++) {
397 if (generated_attrs
[i
].aggregate
&&
398 ldb_attr_in_list(ac
->req
->op
.search
.attrs
, generated_attrs
[i
].attr
)) {
399 ret
= generated_attrs
[i
].fn(ldb
, ares
->message
, ac
->schema
);
400 if (ret
!= LDB_SUCCESS
) {
405 } else if ((ldb_dn_compare_base(mc
->schema_dn
, ares
->message
->dn
) == 0)
406 && (ldb_dn_compare(mc
->schema_dn
, ares
->message
->dn
) != 0)) {
407 for (i
=0; i
< ARRAY_SIZE(generated_attrs
); i
++) {
408 if (!generated_attrs
[i
].aggregate
&&
409 ldb_attr_in_list(ac
->req
->op
.search
.attrs
, generated_attrs
[i
].attr
)) {
410 ret
= generated_attrs
[i
].fn(ldb
, ares
->message
, ac
->schema
);
411 if (ret
!= LDB_SUCCESS
) {
419 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
421 case LDB_REPLY_REFERRAL
:
423 return ldb_module_send_referral(ac
->req
, ares
->referral
);
427 return ldb_module_done(ac
->req
, ares
->controls
,
428 ares
->response
, ares
->error
);
435 static int schema_data_search(struct ldb_module
*module
, struct ldb_request
*req
)
437 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
439 struct schema_data_search_data
*search_context
;
440 struct ldb_request
*down_req
;
441 struct dsdb_schema
*schema
= dsdb_get_schema(ldb
);
443 if (!schema
|| !ldb_module_get_private(module
)) {
444 /* If there is no schema, there is little we can do */
445 return ldb_next_request(module
, req
);
447 for (i
=0; i
< ARRAY_SIZE(generated_attrs
); i
++) {
448 if (ldb_attr_in_list(req
->op
.search
.attrs
, generated_attrs
[i
].attr
)) {
452 if (i
== ARRAY_SIZE(generated_attrs
)) {
453 /* No request for a generated attr found, nothing to
454 * see here, move along... */
455 return ldb_next_request(module
, req
);
458 search_context
= talloc(req
, struct schema_data_search_data
);
459 if (!search_context
) {
461 return LDB_ERR_OPERATIONS_ERROR
;
464 search_context
->module
= module
;
465 search_context
->req
= req
;
466 search_context
->schema
= schema
;
468 ret
= ldb_build_search_req_ex(&down_req
, ldb
, search_context
,
470 req
->op
.search
.scope
,
472 req
->op
.search
.attrs
,
474 search_context
, schema_data_search_callback
,
476 if (ret
!= LDB_SUCCESS
) {
477 return LDB_ERR_OPERATIONS_ERROR
;
480 return ldb_next_request(module
, down_req
);
484 _PUBLIC_
const struct ldb_module_ops ldb_schema_data_module_ops
= {
485 .name
= "schema_data",
486 .init_context
= schema_data_init
,
487 .add
= schema_data_add
,
488 .modify
= schema_data_modify
,
489 .search
= schema_data_search