s4:dsdb/schema: avoid an implicit prefix map creation in lookup functions
[Samba.git] / source4 / dsdb / schema / schema_init.c
blobc1cf466493ae982253a3d24842da27245fd400b1
1 /*
2 Unix SMB/CIFS mplementation.
3 DSDB schema header
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2006-2007
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "dsdb/common/util.h"
26 #include <ldb_errors.h>
27 #include "../lib/util/dlinklist.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "param/param.h"
32 #include <ldb_module.h>
33 #include "../lib/util/asn1.h"
36 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx)
38 struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
39 if (!schema) {
40 return NULL;
43 return schema;
46 struct dsdb_schema *dsdb_schema_copy_shallow(TALLOC_CTX *mem_ctx,
47 struct ldb_context *ldb,
48 const struct dsdb_schema *schema)
50 int ret;
51 struct dsdb_class *cls;
52 struct dsdb_attribute *attr;
53 struct dsdb_schema *schema_copy;
55 schema_copy = dsdb_new_schema(mem_ctx);
56 if (!schema_copy) {
57 return NULL;
60 /* copy prexiMap & schemaInfo */
61 schema_copy->prefixmap = dsdb_schema_pfm_copy_shallow(schema_copy,
62 schema->prefixmap);
63 if (!schema_copy->prefixmap) {
64 goto failed;
67 schema_copy->schema_info = talloc(schema_copy, struct dsdb_schema_info);
68 if (!schema_copy->schema_info) {
69 goto failed;
71 *schema_copy->schema_info = *schema->schema_info;
73 /* copy classes and attributes*/
74 for (cls = schema->classes; cls; cls = cls->next) {
75 struct dsdb_class *class_copy = talloc_memdup(schema_copy,
76 cls, sizeof(*cls));
77 if (!class_copy) {
78 goto failed;
80 DLIST_ADD(schema_copy->classes, class_copy);
82 schema_copy->num_classes = schema->num_classes;
84 for (attr = schema->attributes; attr; attr = attr->next) {
85 struct dsdb_attribute *a_copy = talloc_memdup(schema_copy,
86 attr, sizeof(*attr));
87 if (!a_copy) {
88 goto failed;
90 DLIST_ADD(schema_copy->attributes, a_copy);
92 schema_copy->num_attributes = schema->num_attributes;
94 /* rebuild indexes */
95 ret = dsdb_setup_sorted_accessors(ldb, schema_copy);
96 if (ret != LDB_SUCCESS) {
97 goto failed;
100 /* leave reload_seq_number = 0 so it will be refresh ASAP */
102 return schema_copy;
104 failed:
105 talloc_free(schema_copy);
106 return NULL;
110 WERROR dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema *schema,
111 const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
113 WERROR werr;
114 struct dsdb_schema_info *schema_info = NULL;
115 struct dsdb_schema_prefixmap *pfm = NULL;
117 werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, schema, &pfm, &schema_info);
118 W_ERROR_NOT_OK_RETURN(werr);
120 /* set loaded prefixMap */
121 talloc_free(schema->prefixmap);
122 schema->prefixmap = pfm;
124 talloc_free(schema->schema_info);
125 schema->schema_info = schema_info;
127 return WERR_OK;
130 static WERROR _dsdb_prefixmap_from_ldb_val(const struct ldb_val *pfm_ldb_val,
131 TALLOC_CTX *mem_ctx,
132 struct dsdb_schema_prefixmap **_pfm)
134 WERROR werr;
135 enum ndr_err_code ndr_err;
136 struct prefixMapBlob pfm_blob;
138 TALLOC_CTX *temp_ctx = talloc_new(mem_ctx);
139 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
141 ndr_err = ndr_pull_struct_blob(pfm_ldb_val, temp_ctx,
142 &pfm_blob,
143 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
144 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
145 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
146 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: Failed to parse prefixmap of length %u: %s\n",
147 (unsigned int)pfm_ldb_val->length, ndr_map_error2string(ndr_err)));
148 talloc_free(temp_ctx);
149 return ntstatus_to_werror(nt_status);
152 if (pfm_blob.version != PREFIX_MAP_VERSION_DSDB) {
153 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version %u incorrect\n", (unsigned int)pfm_blob.version));
154 talloc_free(temp_ctx);
155 return WERR_VERSION_PARSE_ERROR;
158 /* call the drsuapi version */
159 werr = dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob.ctr.dsdb, false, mem_ctx, _pfm, NULL);
160 if (!W_ERROR_IS_OK(werr)) {
161 DEBUG(0, (__location__ " dsdb_schema_pfm_from_drsuapi_pfm failed: %s\n", win_errstr(werr)));
162 talloc_free(temp_ctx);
163 return werr;
166 talloc_free(temp_ctx);
168 return werr;
171 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
172 const struct ldb_val *prefixMap,
173 const struct ldb_val *schemaInfo)
175 WERROR werr;
176 struct dsdb_schema_info *schema_info = NULL;
177 struct dsdb_schema_prefixmap *pfm = NULL;
178 TALLOC_CTX *mem_ctx;
180 /* verify schemaInfo blob is valid one */
181 if (!dsdb_schema_info_blob_is_valid(schemaInfo)) {
182 DEBUG(0,(__location__": dsdb_schema_info_blob_is_valid() failed.\n"));
183 return WERR_INVALID_PARAMETER;
186 mem_ctx = talloc_new(schema);
187 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
189 /* fetch prefixMap */
190 werr = _dsdb_prefixmap_from_ldb_val(prefixMap,
191 mem_ctx, &pfm);
192 if (!W_ERROR_IS_OK(werr)) {
193 DEBUG(0, (__location__ " _dsdb_prefixmap_from_ldb_val failed: %s\n", win_errstr(werr)));
194 talloc_free(mem_ctx);
195 return werr;
198 /* decode schema_info */
199 werr = dsdb_schema_info_from_blob(schemaInfo, mem_ctx, &schema_info);
200 if (!W_ERROR_IS_OK(werr)) {
201 DEBUG(0, (__location__ " dsdb_schema_info_from_blob failed: %s\n", win_errstr(werr)));
202 talloc_free(mem_ctx);
203 return werr;
206 /* store prefixMap and schema_info into cached Schema */
207 talloc_free(schema->prefixmap);
208 schema->prefixmap = talloc_steal(schema, pfm);
210 talloc_free(schema->schema_info);
211 schema->schema_info = talloc_steal(schema, schema_info);
213 /* clean up locally allocated mem */
214 talloc_free(mem_ctx);
216 return WERR_OK;
219 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
220 bool include_schema_info,
221 TALLOC_CTX *mem_ctx,
222 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
224 return dsdb_drsuapi_pfm_from_schema_pfm(schema->prefixmap,
225 include_schema_info ? schema->schema_info : NULL,
226 mem_ctx, _ctr);
229 WERROR dsdb_get_drsuapi_prefixmap_as_blob(const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr,
230 TALLOC_CTX *mem_ctx,
231 struct ldb_val *prefixMap)
233 struct prefixMapBlob pfm;
234 enum ndr_err_code ndr_err;
235 pfm.version = PREFIX_MAP_VERSION_DSDB;
236 pfm.reserved = 0;
237 pfm.ctr.dsdb = *ctr;
239 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, &pfm,
240 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
241 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
242 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
243 return ntstatus_to_werror(nt_status);
245 return WERR_OK;
248 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
249 TALLOC_CTX *mem_ctx,
250 struct ldb_val *prefixMap,
251 struct ldb_val *schemaInfo)
253 WERROR status;
254 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
256 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
257 W_ERROR_NOT_OK_RETURN(status);
259 status = dsdb_get_drsuapi_prefixmap_as_blob(ctr, mem_ctx, prefixMap);
260 talloc_free(ctr);
261 W_ERROR_NOT_OK_RETURN(status);
263 status = dsdb_blob_from_schema_info(schema->schema_info, mem_ctx, schemaInfo);
264 W_ERROR_NOT_OK_RETURN(status);
266 return WERR_OK;
271 * this function is called from within a ldb transaction from the schema_fsmo module
273 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
275 WERROR status;
276 uint32_t attid;
277 TALLOC_CTX *mem_ctx;
278 struct dsdb_schema_prefixmap *pfm;
280 mem_ctx = talloc_new(ldb);
281 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
283 /* Read prefixes from disk*/
284 status = dsdb_read_prefixes_from_ldb(ldb, mem_ctx, &pfm);
285 if (!W_ERROR_IS_OK(status)) {
286 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
287 win_errstr(status)));
288 talloc_free(mem_ctx);
289 return status;
292 /* Check if there is a prefix for the oid in the prefixes array*/
293 status = dsdb_schema_pfm_find_oid(pfm, full_oid, NULL);
294 if (W_ERROR_IS_OK(status)) {
295 /* prefix found*/
296 talloc_free(mem_ctx);
297 return status;
298 } else if (!W_ERROR_EQUAL(status, WERR_NOT_FOUND)) {
299 /* error */
300 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
301 win_errstr(status)));
302 talloc_free(mem_ctx);
303 return status;
306 /* Create the new mapping for the prefix of full_oid */
307 status = dsdb_schema_pfm_make_attid(pfm, full_oid, &attid);
308 if (!W_ERROR_IS_OK(status)) {
309 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_schema_pfm_make_attid: %s\n",
310 win_errstr(status)));
311 talloc_free(mem_ctx);
312 return status;
315 talloc_unlink(schema, schema->prefixmap);
316 schema->prefixmap = talloc_steal(schema, pfm);
318 /* Update prefixMap in ldb*/
319 status = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, ldb, schema);
320 if (!W_ERROR_IS_OK(status)) {
321 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
322 win_errstr(status)));
323 talloc_free(mem_ctx);
324 return status;
327 DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
328 full_oid, schema->prefixmap->length));
330 talloc_free(mem_ctx);
331 return status;
335 WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
336 const struct dsdb_schema *schema)
338 WERROR status;
339 int ldb_ret;
340 struct ldb_message *msg;
341 struct ldb_dn *schema_dn;
342 struct prefixMapBlob pfm_blob;
343 struct ldb_val ndr_blob;
344 enum ndr_err_code ndr_err;
345 TALLOC_CTX *temp_ctx;
346 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
348 schema_dn = ldb_get_schema_basedn(ldb);
349 if (!schema_dn) {
350 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));
351 return WERR_FOOBAR;
354 temp_ctx = talloc_new(mem_ctx);
355 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
357 /* convert schema_prefixMap to prefixMap blob */
358 status = dsdb_get_oid_mappings_drsuapi(schema, false, temp_ctx, &ctr);
359 if (!W_ERROR_IS_OK(status)) {
360 talloc_free(temp_ctx);
361 return status;
364 pfm_blob.version = PREFIX_MAP_VERSION_DSDB;
365 pfm_blob.ctr.dsdb = *ctr;
367 ndr_err = ndr_push_struct_blob(&ndr_blob, temp_ctx,
368 &pfm_blob,
369 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
370 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
371 talloc_free(temp_ctx);
372 return WERR_FOOBAR;
375 /* write serialized prefixMap into LDB */
376 msg = ldb_msg_new(temp_ctx);
377 if (!msg) {
378 talloc_free(temp_ctx);
379 return WERR_NOMEM;
382 msg->dn = schema_dn;
383 ldb_ret = ldb_msg_add_value(msg, "prefixMap", &ndr_blob, NULL);
384 if (ldb_ret != 0) {
385 talloc_free(temp_ctx);
386 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));
387 return WERR_NOMEM;
390 ldb_ret = dsdb_replace(ldb, msg, DSDB_FLAG_AS_SYSTEM);
392 talloc_free(temp_ctx);
394 if (ldb_ret != 0) {
395 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: dsdb_replace failed\n"));
396 return WERR_FOOBAR;
399 return WERR_OK;
402 WERROR dsdb_read_prefixes_from_ldb(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct dsdb_schema_prefixmap **_pfm)
404 WERROR werr;
405 int ldb_ret;
406 const struct ldb_val *prefix_val;
407 struct ldb_dn *schema_dn;
408 struct ldb_result *schema_res = NULL;
409 static const char *schema_attrs[] = {
410 "prefixMap",
411 NULL
414 schema_dn = ldb_get_schema_basedn(ldb);
415 if (!schema_dn) {
416 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
417 return WERR_FOOBAR;
420 ldb_ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
421 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
422 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
423 talloc_free(schema_res);
424 return WERR_FOOBAR;
425 } else if (ldb_ret != LDB_SUCCESS) {
426 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
427 talloc_free(schema_res);
428 return WERR_FOOBAR;
431 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
432 if (!prefix_val) {
433 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
434 talloc_free(schema_res);
435 return WERR_FOOBAR;
438 werr = _dsdb_prefixmap_from_ldb_val(prefix_val,
439 mem_ctx,
440 _pfm);
441 talloc_free(schema_res);
442 W_ERROR_NOT_OK_RETURN(werr);
444 return WERR_OK;
448 this will be replaced with something that looks at the right part of
449 the schema once we know where unique indexing information is hidden
451 static bool dsdb_schema_unique_attribute(const char *attr)
453 const char *attrs[] = { "objectGUID", "objectSid" , NULL };
454 unsigned int i;
455 for (i=0;attrs[i];i++) {
456 if (ldb_attr_cmp(attr, attrs[i]) == 0) {
457 return true;
460 return false;
465 setup the ldb_schema_attribute field for a dsdb_attribute
467 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb,
468 struct dsdb_attribute *attr)
470 const char *syntax = attr->syntax->ldb_syntax;
471 const struct ldb_schema_syntax *s;
472 struct ldb_schema_attribute *a;
474 if (!syntax) {
475 syntax = attr->syntax->ldap_oid;
478 s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
479 if (s == NULL) {
480 s = ldb_samba_syntax_by_name(ldb, syntax);
482 if (s == NULL) {
483 s = ldb_standard_syntax_by_name(ldb, syntax);
486 if (s == NULL) {
487 return ldb_operr(ldb);
490 attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
491 if (attr->ldb_schema_attribute == NULL) {
492 return ldb_oom(ldb);
495 a->name = attr->lDAPDisplayName;
496 a->flags = 0;
497 a->syntax = s;
499 if (dsdb_schema_unique_attribute(a->name)) {
500 a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
502 if (attr->isSingleValued) {
503 a->flags |= LDB_ATTR_FLAG_SINGLE_VALUE;
507 return LDB_SUCCESS;
511 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
512 const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
513 if (get_string_val == NULL) { \
514 if (strict) { \
515 d_printf("%s: %s == NULL in %s\n", __location__, attr, ldb_dn_get_linearized(msg->dn)); \
516 return WERR_INVALID_PARAM; \
517 } else { \
518 (p)->elem = NULL; \
520 } else { \
521 (p)->elem = talloc_strndup(mem_ctx, \
522 (const char *)get_string_val->data, \
523 get_string_val->length); \
524 if (!(p)->elem) { \
525 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
526 return WERR_NOMEM; \
529 } while (0)
531 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem) do { \
532 int get_string_list_counter; \
533 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
534 /* We may get empty attributes over the replication channel */ \
535 if (get_string_list_el == NULL || get_string_list_el->num_values == 0) { \
536 (p)->elem = NULL; \
537 break; \
539 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
540 for (get_string_list_counter=0; \
541 get_string_list_counter < get_string_list_el->num_values; \
542 get_string_list_counter++) { \
543 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
544 (const char *)get_string_list_el->values[get_string_list_counter].data, \
545 get_string_list_el->values[get_string_list_counter].length); \
546 if (!(p)->elem[get_string_list_counter]) { \
547 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
548 return WERR_NOMEM; \
550 (p)->elem[get_string_list_counter+1] = NULL; \
552 talloc_steal(mem_ctx, (p)->elem); \
553 } while (0)
555 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
556 const char *str; \
557 str = ldb_msg_find_attr_as_string(msg, attr, NULL);\
558 if (str == NULL) { \
559 if (strict) { \
560 d_printf("%s: %s == NULL\n", __location__, attr); \
561 return WERR_INVALID_PARAM; \
562 } else { \
563 (p)->elem = false; \
565 } else if (strcasecmp("TRUE", str) == 0) { \
566 (p)->elem = true; \
567 } else if (strcasecmp("FALSE", str) == 0) { \
568 (p)->elem = false; \
569 } else { \
570 d_printf("%s: %s == %s\n", __location__, attr, str); \
571 return WERR_INVALID_PARAM; \
573 } while (0)
575 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
576 (p)->elem = ldb_msg_find_attr_as_uint(msg, attr, 0);\
577 } while (0)
579 #define GET_UINT32_PTR_LDB(msg, attr, mem_ctx, p, elem) do { \
580 uint64_t _v = ldb_msg_find_attr_as_uint64(msg, attr, UINT64_MAX);\
581 if (_v == UINT64_MAX) { \
582 (p)->elem = NULL; \
583 } else if (_v > UINT32_MAX) { \
584 d_printf("%s: %s == 0x%llX\n", __location__, \
585 attr, (unsigned long long)_v); \
586 return WERR_INVALID_PARAM; \
587 } else { \
588 (p)->elem = talloc(mem_ctx, uint32_t); \
589 if (!(p)->elem) { \
590 d_printf("%s: talloc failed for %s\n", __location__, attr); \
591 return WERR_NOMEM; \
593 *(p)->elem = (uint32_t)_v; \
595 } while (0)
597 #define GET_GUID_LDB(msg, attr, p, elem) do { \
598 (p)->elem = samdb_result_guid(msg, attr);\
599 } while (0)
601 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
602 const struct ldb_val *_val;\
603 _val = ldb_msg_find_ldb_val(msg, attr);\
604 if (_val) {\
605 (p)->elem = *_val;\
606 talloc_steal(mem_ctx, (p)->elem.data);\
607 } else {\
608 ZERO_STRUCT((p)->elem);\
610 } while (0)
612 /** Create an dsdb_attribute out of ldb message, attr must be already talloced
615 WERROR dsdb_attribute_from_ldb(const struct dsdb_schema *schema,
616 struct ldb_message *msg,
617 struct dsdb_attribute *attr)
619 WERROR status;
620 if (attr == NULL) {
621 DEBUG(0, ("%s: attr is null, it's expected not to be so\n", __location__));
622 return WERR_INVALID_PARAM;
625 GET_STRING_LDB(msg, "cn", attr, attr, cn, false);
628 * This allows for the fact that the CN attribute is not
629 * replicated over DRS, it is only replicated under the alias
630 * 'name'.
632 if (attr->cn == NULL) {
633 GET_STRING_LDB(msg, "name", attr, attr, cn, true);
636 GET_STRING_LDB(msg, "lDAPDisplayName", attr, attr, lDAPDisplayName, true);
637 GET_STRING_LDB(msg, "attributeID", attr, attr, attributeID_oid, true);
638 if (!schema->prefixmap || schema->prefixmap->length == 0) {
639 /* set an invalid value */
640 attr->attributeID_id = DRSUAPI_ATTID_INVALID;
641 } else {
642 status = dsdb_schema_pfm_attid_from_oid(schema->prefixmap,
643 attr->attributeID_oid,
644 &attr->attributeID_id);
645 if (!W_ERROR_IS_OK(status)) {
646 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
647 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
648 win_errstr(status)));
649 return status;
652 /* fetch msDS-IntId to be used in resolving ATTRTYP values */
653 GET_UINT32_LDB(msg, "msDS-IntId", attr, msDS_IntId);
655 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
656 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
658 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
660 GET_GUID_LDB(msg, "objectGUID", attr, objectGUID);
662 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
663 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
664 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
665 GET_UINT32_LDB(msg, "linkID", attr, linkID);
667 GET_STRING_LDB(msg, "attributeSyntax", attr, attr, attributeSyntax_oid, true);
668 if (!schema->prefixmap || schema->prefixmap->length == 0) {
669 /* set an invalid value */
670 attr->attributeSyntax_id = DRSUAPI_ATTID_INVALID;
671 } else {
672 status = dsdb_schema_pfm_attid_from_oid(schema->prefixmap,
673 attr->attributeSyntax_oid,
674 &attr->attributeSyntax_id);
675 if (!W_ERROR_IS_OK(status)) {
676 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
677 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
678 win_errstr(status)));
679 return status;
682 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
683 GET_BLOB_LDB(msg, "oMObjectClass", attr, attr, oMObjectClass);
685 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
686 GET_UINT32_PTR_LDB(msg, "rangeLower", attr, attr, rangeLower);
687 GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, attr, rangeUpper);
688 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
690 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
691 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", attr, attr, msDs_Schema_Extensions);
693 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
694 GET_STRING_LDB(msg, "adminDisplayName", attr, attr, adminDisplayName, false);
695 GET_STRING_LDB(msg, "adminDescription", attr, attr, adminDescription, false);
696 GET_STRING_LDB(msg, "classDisplayName", attr, attr, classDisplayName, false);
697 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
698 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
699 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
701 return WERR_OK;
704 WERROR dsdb_set_attribute_from_ldb_dups(struct ldb_context *ldb,
705 struct dsdb_schema *schema,
706 struct ldb_message *msg,
707 bool checkdups)
709 WERROR status;
710 struct dsdb_attribute *attr = talloc_zero(schema, struct dsdb_attribute);
711 if (!attr) {
712 return WERR_NOMEM;
715 status = dsdb_attribute_from_ldb(schema, msg, attr);
716 if (!W_ERROR_IS_OK(status)) {
717 return status;
720 attr->syntax = dsdb_syntax_for_attribute(attr);
721 if (!attr->syntax) {
722 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
723 attr->lDAPDisplayName));
724 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
727 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
728 DEBUG(0,(__location__ ": Unknown schema syntax for %s - ldb_syntax: %s, ldap_oid: %s\n",
729 attr->lDAPDisplayName,
730 attr->syntax->ldb_syntax,
731 attr->syntax->ldap_oid));
732 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
735 if (checkdups) {
736 const struct dsdb_attribute *a2;
737 struct dsdb_attribute **a;
738 uint32_t i;
740 a2 = dsdb_attribute_by_attributeID_id(schema,
741 attr->attributeID_id);
742 if (a2 == NULL) {
743 goto done;
746 i = schema->attributes_to_remove_size;
747 a = talloc_realloc(schema, schema->attributes_to_remove,
748 struct dsdb_attribute *, i + 1);
749 if (a == NULL) {
750 return WERR_NOMEM;
752 /* Mark the old attribute as to be removed */
753 a[i] = discard_const_p(struct dsdb_attribute, a2);
754 schema->attributes_to_remove = a;
755 schema->attributes_to_remove_size++;
758 done:
759 DLIST_ADD(schema->attributes, attr);
760 return WERR_OK;
763 WERROR dsdb_set_attribute_from_ldb(struct ldb_context *ldb,
764 struct dsdb_schema *schema,
765 struct ldb_message *msg)
767 return dsdb_set_attribute_from_ldb_dups(ldb, schema, msg, false);
770 WERROR dsdb_set_class_from_ldb_dups(struct dsdb_schema *schema,
771 struct ldb_message *msg,
772 bool checkdups)
774 WERROR status;
775 struct dsdb_class *obj = talloc_zero(schema, struct dsdb_class);
776 if (!obj) {
777 return WERR_NOMEM;
779 GET_STRING_LDB(msg, "cn", obj, obj, cn, false);
782 * This allows for the fact that the CN attribute is not
783 * replicated over DRS, it is only replicated under the alias
784 * 'name'.
786 if (obj->cn == NULL) {
787 GET_STRING_LDB(msg, "name", obj, obj, cn, true);
790 GET_STRING_LDB(msg, "lDAPDisplayName", obj, obj, lDAPDisplayName, true);
791 GET_STRING_LDB(msg, "governsID", obj, obj, governsID_oid, true);
792 if (!schema->prefixmap || schema->prefixmap->length == 0) {
793 /* set an invalid value */
794 obj->governsID_id = DRSUAPI_ATTID_INVALID;
795 } else {
796 status = dsdb_schema_pfm_attid_from_oid(schema->prefixmap,
797 obj->governsID_oid,
798 &obj->governsID_id);
799 if (!W_ERROR_IS_OK(status)) {
800 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
801 __location__, obj->lDAPDisplayName, obj->governsID_oid,
802 win_errstr(status)));
803 return status;
806 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
807 GET_GUID_LDB(msg, "objectGUID", obj, objectGUID);
809 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
810 GET_STRING_LDB(msg, "rDNAttID", obj, obj, rDNAttID, false);
811 GET_STRING_LDB(msg, "defaultObjectCategory", obj, obj, defaultObjectCategory, true);
813 GET_STRING_LDB(msg, "subClassOf", obj, obj, subClassOf, true);
815 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", obj, obj, systemAuxiliaryClass);
816 GET_STRING_LIST_LDB(msg, "auxiliaryClass", obj, obj, auxiliaryClass);
818 GET_STRING_LIST_LDB(msg, "systemMustContain", obj, obj, systemMustContain);
819 GET_STRING_LIST_LDB(msg, "systemMayContain", obj, obj, systemMayContain);
820 GET_STRING_LIST_LDB(msg, "mustContain", obj, obj, mustContain);
821 GET_STRING_LIST_LDB(msg, "mayContain", obj, obj, mayContain);
823 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", obj, obj, systemPossSuperiors);
824 GET_STRING_LIST_LDB(msg, "possSuperiors", obj, obj, possSuperiors);
826 GET_STRING_LDB(msg, "defaultSecurityDescriptor", obj, obj, defaultSecurityDescriptor, false);
828 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
829 GET_UINT32_LDB(msg, "systemFlags", obj, systemFlags);
830 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", obj, obj, msDs_Schema_Extensions);
832 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
833 GET_STRING_LDB(msg, "adminDisplayName", obj, obj, adminDisplayName, false);
834 GET_STRING_LDB(msg, "adminDescription", obj, obj, adminDescription, false);
835 GET_STRING_LDB(msg, "classDisplayName", obj, obj, classDisplayName, false);
836 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
837 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
838 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
840 if (checkdups) {
841 const struct dsdb_class *c2;
842 struct dsdb_class **c;
843 uint32_t i;
845 c2 = dsdb_class_by_governsID_id(schema, obj->governsID_id);
846 if (c2 == NULL) {
847 goto done;
850 i = schema->classes_to_remove_size;
851 c = talloc_realloc(schema, schema->classes_to_remove,
852 struct dsdb_class *, i + 1);
853 if (c == NULL) {
854 return WERR_NOMEM;
856 /* Mark the old class to be removed */
857 c[i] = discard_const_p(struct dsdb_class, c2);
858 schema->classes_to_remove = c;
859 schema->classes_to_remove_size++;
862 done:
863 DLIST_ADD(schema->classes, obj);
864 return WERR_OK;
867 WERROR dsdb_set_class_from_ldb(struct dsdb_schema *schema,
868 struct ldb_message *msg)
870 return dsdb_set_class_from_ldb_dups(schema, msg, false);
873 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
876 Fill a DSDB schema from the ldb results provided. This is called
877 directly when a schema must be created with a pre-initialised prefixMap
880 int dsdb_load_ldb_results_into_schema(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
881 struct dsdb_schema *schema,
882 struct ldb_result *attrs_class_res,
883 char **error_string)
885 unsigned int i;
887 schema->ts_last_change = 0;
888 for (i=0; i < attrs_class_res->count; i++) {
889 WERROR status = dsdb_schema_set_el_from_ldb_msg(ldb, schema, attrs_class_res->msgs[i]);
890 if (!W_ERROR_IS_OK(status)) {
891 *error_string = talloc_asprintf(mem_ctx,
892 "dsdb_load_ldb_results_into_schema: failed to load attribute or class definition: %s:%s",
893 ldb_dn_get_linearized(attrs_class_res->msgs[i]->dn),
894 win_errstr(status));
895 DEBUG(0,(__location__ ": %s\n", *error_string));
896 return LDB_ERR_CONSTRAINT_VIOLATION;
900 return LDB_SUCCESS;
904 Create a DSDB schema from the ldb results provided. This is called
905 directly when the schema is provisioned from an on-disk LDIF file, or
906 from dsdb_schema_from_schema_dn in schema_fsmo
909 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
910 struct ldb_result *schema_res,
911 struct ldb_result *attrs_class_res,
912 struct dsdb_schema **schema_out,
913 char **error_string)
915 WERROR status;
916 const struct ldb_val *prefix_val;
917 const struct ldb_val *info_val;
918 struct ldb_val info_val_default;
919 struct dsdb_schema *schema;
920 void *lp_opaque = ldb_get_opaque(ldb, "loadparm");
921 int ret;
923 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
924 if (!tmp_ctx) {
925 dsdb_oom(error_string, mem_ctx);
926 return ldb_operr(ldb);
929 schema = dsdb_new_schema(tmp_ctx);
930 if (!schema) {
931 dsdb_oom(error_string, mem_ctx);
932 talloc_free(tmp_ctx);
933 return ldb_operr(ldb);
936 if (lp_opaque) {
937 struct loadparm_context *lp_ctx = talloc_get_type_abort(lp_opaque, struct loadparm_context);
938 schema->fsmo.update_allowed = lpcfg_parm_bool(lp_ctx, NULL,
939 "dsdb", "schema update allowed",
940 false);
943 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
944 if (!prefix_val) {
945 *error_string = talloc_asprintf(mem_ctx,
946 "schema_fsmo_init: no prefixMap attribute found");
947 DEBUG(0,(__location__ ": %s\n", *error_string));
948 talloc_free(tmp_ctx);
949 return LDB_ERR_CONSTRAINT_VIOLATION;
951 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
952 if (!info_val) {
953 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
954 if (!W_ERROR_IS_OK(status)) {
955 *error_string = talloc_asprintf(mem_ctx,
956 "schema_fsmo_init: dsdb_schema_info_blob_new() failed - %s",
957 win_errstr(status));
958 DEBUG(0,(__location__ ": %s\n", *error_string));
959 talloc_free(tmp_ctx);
960 return ldb_operr(ldb);
962 info_val = &info_val_default;
965 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
966 if (!W_ERROR_IS_OK(status)) {
967 *error_string = talloc_asprintf(mem_ctx,
968 "schema_fsmo_init: failed to load oid mappings: %s",
969 win_errstr(status));
970 DEBUG(0,(__location__ ": %s\n", *error_string));
971 talloc_free(tmp_ctx);
972 return LDB_ERR_CONSTRAINT_VIOLATION;
975 ret = dsdb_load_ldb_results_into_schema(mem_ctx, ldb, schema, attrs_class_res, error_string);
976 if (ret != LDB_SUCCESS) {
977 talloc_free(tmp_ctx);
978 return ret;
981 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
982 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb, tmp_ctx), schema->fsmo.master_dn) == 0) {
983 schema->fsmo.we_are_master = true;
984 } else {
985 schema->fsmo.we_are_master = false;
988 DEBUG(5, ("schema_fsmo_init: we are master[%s] updates allowed[%s]\n",
989 (schema->fsmo.we_are_master?"yes":"no"),
990 (schema->fsmo.update_allowed?"yes":"no")));
992 *schema_out = talloc_steal(mem_ctx, schema);
993 talloc_free(tmp_ctx);
994 return LDB_SUCCESS;