s4-dsdb: Do not use a possibly-old loadparm context in schema reload
[Samba.git] / source4 / dsdb / schema / schema_init.c
blob752d4f57dd1c7dde8bb218d47c59c42e087955ea
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;
42 schema->refresh_interval = 120;
44 return schema;
47 struct dsdb_schema *dsdb_schema_copy_shallow(TALLOC_CTX *mem_ctx,
48 struct ldb_context *ldb,
49 const struct dsdb_schema *schema)
51 int ret;
52 struct dsdb_class *cls;
53 struct dsdb_attribute *attr;
54 struct dsdb_schema *schema_copy;
56 schema_copy = dsdb_new_schema(mem_ctx);
57 if (!schema_copy) {
58 return NULL;
61 /* schema base_dn */
62 schema_copy->base_dn = ldb_dn_copy(schema_copy, schema->base_dn);
63 if (!schema_copy->base_dn) {
64 goto failed;
67 /* copy prexiMap & schemaInfo */
68 schema_copy->prefixmap = dsdb_schema_pfm_copy_shallow(schema_copy,
69 schema->prefixmap);
70 if (!schema_copy->prefixmap) {
71 goto failed;
74 schema_copy->schema_info = talloc_strdup(schema_copy, schema->schema_info);
76 /* copy classes and attributes*/
77 for (cls = schema->classes; cls; cls = cls->next) {
78 struct dsdb_class *class_copy = talloc_memdup(schema_copy,
79 cls, sizeof(*cls));
80 if (!class_copy) {
81 goto failed;
83 DLIST_ADD(schema_copy->classes, class_copy);
85 schema_copy->num_classes = schema->num_classes;
87 for (attr = schema->attributes; attr; attr = attr->next) {
88 struct dsdb_attribute *a_copy = talloc_memdup(schema_copy,
89 attr, sizeof(*attr));
90 if (!a_copy) {
91 goto failed;
93 DLIST_ADD(schema_copy->attributes, a_copy);
95 schema_copy->num_attributes = schema->num_attributes;
97 schema_copy->refresh_interval = schema->refresh_interval;
99 /* rebuild indexes */
100 ret = dsdb_setup_sorted_accessors(ldb, schema_copy);
101 if (ret != LDB_SUCCESS) {
102 goto failed;
105 /* leave reload_seq_number = 0 so it will be refresh ASAP */
106 schema_copy->refresh_fn = schema->refresh_fn;
107 schema_copy->loaded_from_module = schema->loaded_from_module;
109 return schema_copy;
111 failed:
112 talloc_free(schema_copy);
113 return NULL;
117 WERROR dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema *schema,
118 const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
120 WERROR werr;
121 const char *schema_info;
122 struct dsdb_schema_prefixmap *pfm;
124 werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, schema, &pfm, &schema_info);
125 W_ERROR_NOT_OK_RETURN(werr);
127 /* set loaded prefixMap */
128 talloc_free(schema->prefixmap);
129 schema->prefixmap = pfm;
131 talloc_free(discard_const(schema->schema_info));
132 schema->schema_info = schema_info;
134 return WERR_OK;
137 static WERROR _dsdb_prefixmap_from_ldb_val(const struct ldb_val *pfm_ldb_val,
138 TALLOC_CTX *mem_ctx,
139 struct dsdb_schema_prefixmap **_pfm)
141 WERROR werr;
142 enum ndr_err_code ndr_err;
143 struct prefixMapBlob pfm_blob;
145 TALLOC_CTX *temp_ctx = talloc_new(mem_ctx);
146 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
148 ndr_err = ndr_pull_struct_blob(pfm_ldb_val, temp_ctx,
149 &pfm_blob,
150 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
151 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
152 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
153 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: Failed to parse prefixmap of length %u: %s\n",
154 (unsigned int)pfm_ldb_val->length, ndr_map_error2string(ndr_err)));
155 talloc_free(temp_ctx);
156 return ntstatus_to_werror(nt_status);
159 if (pfm_blob.version != PREFIX_MAP_VERSION_DSDB) {
160 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version %u incorrect\n", (unsigned int)pfm_blob.version));
161 talloc_free(temp_ctx);
162 return WERR_VERSION_PARSE_ERROR;
165 /* call the drsuapi version */
166 werr = dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob.ctr.dsdb, false, mem_ctx, _pfm, NULL);
167 if (!W_ERROR_IS_OK(werr)) {
168 DEBUG(0, (__location__ " dsdb_schema_pfm_from_drsuapi_pfm failed: %s\n", win_errstr(werr)));
169 talloc_free(temp_ctx);
170 return werr;
173 talloc_free(temp_ctx);
175 return werr;
178 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
179 const struct ldb_val *prefixMap,
180 const struct ldb_val *schemaInfo)
182 WERROR werr;
183 const char *schema_info;
184 struct dsdb_schema_prefixmap *pfm;
185 TALLOC_CTX *mem_ctx;
187 /* verify schemaInfo blob is valid one */
188 if (!dsdb_schema_info_blob_is_valid(schemaInfo)) {
189 DEBUG(0,(__location__": dsdb_schema_info_blob_is_valid() failed.\n"));
190 return WERR_INVALID_PARAMETER;
193 mem_ctx = talloc_new(schema);
194 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
196 /* fetch prefixMap */
197 werr = _dsdb_prefixmap_from_ldb_val(prefixMap,
198 mem_ctx, &pfm);
199 if (!W_ERROR_IS_OK(werr)) {
200 DEBUG(0, (__location__ " _dsdb_prefixmap_from_ldb_val failed: %s\n", win_errstr(werr)));
201 talloc_free(mem_ctx);
202 return werr;
205 /* decode schema_info */
206 schema_info = hex_encode_talloc(mem_ctx,
207 schemaInfo->data,
208 schemaInfo->length);
209 if (!schema_info) {
210 talloc_free(mem_ctx);
211 return WERR_NOMEM;
214 /* store prefixMap and schema_info into cached Schema */
215 talloc_free(schema->prefixmap);
216 schema->prefixmap = talloc_steal(schema, pfm);
218 talloc_free(discard_const(schema->schema_info));
219 schema->schema_info = talloc_steal(schema, schema_info);
221 /* clean up locally allocated mem */
222 talloc_free(mem_ctx);
224 return WERR_OK;
227 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
228 bool include_schema_info,
229 TALLOC_CTX *mem_ctx,
230 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
232 return dsdb_drsuapi_pfm_from_schema_pfm(schema->prefixmap,
233 include_schema_info ? schema->schema_info : NULL,
234 mem_ctx, _ctr);
237 WERROR dsdb_get_drsuapi_prefixmap_as_blob(const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr,
238 TALLOC_CTX *mem_ctx,
239 struct ldb_val *prefixMap)
241 struct prefixMapBlob pfm;
242 enum ndr_err_code ndr_err;
243 pfm.version = PREFIX_MAP_VERSION_DSDB;
244 pfm.reserved = 0;
245 pfm.ctr.dsdb = *ctr;
247 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, &pfm,
248 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
249 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
250 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
251 return ntstatus_to_werror(nt_status);
253 return WERR_OK;
256 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
257 TALLOC_CTX *mem_ctx,
258 struct ldb_val *prefixMap,
259 struct ldb_val *schemaInfo)
261 WERROR status;
262 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
264 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
265 W_ERROR_NOT_OK_RETURN(status);
267 status = dsdb_get_drsuapi_prefixmap_as_blob(ctr, mem_ctx, prefixMap);
268 talloc_free(ctr);
269 W_ERROR_NOT_OK_RETURN(status);
271 *schemaInfo = strhex_to_data_blob(mem_ctx, schema->schema_info);
272 W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
274 return WERR_OK;
279 * this function is called from within a ldb transaction from the schema_fsmo module
281 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
283 WERROR status;
284 uint32_t attid;
285 TALLOC_CTX *mem_ctx;
286 struct dsdb_schema_prefixmap *pfm;
288 mem_ctx = talloc_new(ldb);
289 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
291 /* Read prefixes from disk*/
292 status = dsdb_read_prefixes_from_ldb(ldb, mem_ctx, &pfm);
293 if (!W_ERROR_IS_OK(status)) {
294 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
295 win_errstr(status)));
296 talloc_free(mem_ctx);
297 return status;
300 /* Check if there is a prefix for the oid in the prefixes array*/
301 status = dsdb_schema_pfm_find_oid(pfm, full_oid, NULL);
302 if (W_ERROR_IS_OK(status)) {
303 /* prefix found*/
304 talloc_free(mem_ctx);
305 return status;
306 } else if (!W_ERROR_EQUAL(status, WERR_NOT_FOUND)) {
307 /* error */
308 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
309 win_errstr(status)));
310 talloc_free(mem_ctx);
311 return status;
314 /* Create the new mapping for the prefix of full_oid */
315 status = dsdb_schema_pfm_make_attid(pfm, full_oid, &attid);
316 if (!W_ERROR_IS_OK(status)) {
317 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_schema_pfm_make_attid: %s\n",
318 win_errstr(status)));
319 talloc_free(mem_ctx);
320 return status;
323 talloc_unlink(schema, schema->prefixmap);
324 schema->prefixmap = talloc_steal(schema, pfm);
326 /* Update prefixMap in ldb*/
327 status = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, ldb, schema);
328 if (!W_ERROR_IS_OK(status)) {
329 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
330 win_errstr(status)));
331 talloc_free(mem_ctx);
332 return status;
335 DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
336 full_oid, schema->prefixmap->length));
338 talloc_free(mem_ctx);
339 return status;
343 WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
344 const struct dsdb_schema *schema)
346 WERROR status;
347 int ldb_ret;
348 struct ldb_message *msg;
349 struct ldb_dn *schema_dn;
350 struct prefixMapBlob pfm_blob;
351 struct ldb_val ndr_blob;
352 enum ndr_err_code ndr_err;
353 TALLOC_CTX *temp_ctx;
354 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
356 schema_dn = ldb_get_schema_basedn(ldb);
357 if (!schema_dn) {
358 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));
359 return WERR_FOOBAR;
362 temp_ctx = talloc_new(mem_ctx);
363 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
365 /* convert schema_prefixMap to prefixMap blob */
366 status = dsdb_get_oid_mappings_drsuapi(schema, false, temp_ctx, &ctr);
367 if (!W_ERROR_IS_OK(status)) {
368 talloc_free(temp_ctx);
369 return status;
372 pfm_blob.version = PREFIX_MAP_VERSION_DSDB;
373 pfm_blob.ctr.dsdb = *ctr;
375 ndr_err = ndr_push_struct_blob(&ndr_blob, temp_ctx,
376 &pfm_blob,
377 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
378 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
379 talloc_free(temp_ctx);
380 return WERR_FOOBAR;
383 /* write serialized prefixMap into LDB */
384 msg = ldb_msg_new(temp_ctx);
385 if (!msg) {
386 talloc_free(temp_ctx);
387 return WERR_NOMEM;
390 msg->dn = schema_dn;
391 ldb_ret = ldb_msg_add_value(msg, "prefixMap", &ndr_blob, NULL);
392 if (ldb_ret != 0) {
393 talloc_free(temp_ctx);
394 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));
395 return WERR_NOMEM;
398 ldb_ret = dsdb_replace(ldb, msg, DSDB_FLAG_AS_SYSTEM);
400 talloc_free(temp_ctx);
402 if (ldb_ret != 0) {
403 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: dsdb_replace failed\n"));
404 return WERR_FOOBAR;
407 return WERR_OK;
410 WERROR dsdb_read_prefixes_from_ldb(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct dsdb_schema_prefixmap **_pfm)
412 WERROR werr;
413 int ldb_ret;
414 const struct ldb_val *prefix_val;
415 struct ldb_dn *schema_dn;
416 struct ldb_result *schema_res = NULL;
417 static const char *schema_attrs[] = {
418 "prefixMap",
419 NULL
422 schema_dn = ldb_get_schema_basedn(ldb);
423 if (!schema_dn) {
424 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
425 return WERR_FOOBAR;
428 ldb_ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
429 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
430 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
431 talloc_free(schema_res);
432 return WERR_FOOBAR;
433 } else if (ldb_ret != LDB_SUCCESS) {
434 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
435 talloc_free(schema_res);
436 return WERR_FOOBAR;
439 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
440 if (!prefix_val) {
441 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
442 talloc_free(schema_res);
443 return WERR_FOOBAR;
446 werr = _dsdb_prefixmap_from_ldb_val(prefix_val,
447 mem_ctx,
448 _pfm);
449 talloc_free(schema_res);
450 W_ERROR_NOT_OK_RETURN(werr);
452 return WERR_OK;
456 this will be replaced with something that looks at the right part of
457 the schema once we know where unique indexing information is hidden
459 static bool dsdb_schema_unique_attribute(const char *attr)
461 const char *attrs[] = { "objectGUID", "objectSid" , NULL };
462 unsigned int i;
463 for (i=0;attrs[i];i++) {
464 if (ldb_attr_cmp(attr, attrs[i]) == 0) {
465 return true;
468 return false;
473 setup the ldb_schema_attribute field for a dsdb_attribute
475 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb,
476 struct dsdb_attribute *attr)
478 const char *syntax = attr->syntax->ldb_syntax;
479 const struct ldb_schema_syntax *s;
480 struct ldb_schema_attribute *a;
482 if (!syntax) {
483 syntax = attr->syntax->ldap_oid;
486 s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
487 if (s == NULL) {
488 s = ldb_samba_syntax_by_name(ldb, syntax);
490 if (s == NULL) {
491 s = ldb_standard_syntax_by_name(ldb, syntax);
494 if (s == NULL) {
495 return ldb_operr(ldb);
498 attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
499 if (attr->ldb_schema_attribute == NULL) {
500 return ldb_oom(ldb);
503 a->name = attr->lDAPDisplayName;
504 a->flags = 0;
505 a->syntax = s;
507 if (dsdb_schema_unique_attribute(a->name)) {
508 a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
510 if (attr->isSingleValued) {
511 a->flags |= LDB_ATTR_FLAG_SINGLE_VALUE;
515 return LDB_SUCCESS;
519 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
520 const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
521 if (get_string_val == NULL) { \
522 if (strict) { \
523 d_printf("%s: %s == NULL in %s\n", __location__, attr, ldb_dn_get_linearized(msg->dn)); \
524 return WERR_INVALID_PARAM; \
525 } else { \
526 (p)->elem = NULL; \
528 } else { \
529 (p)->elem = talloc_strndup(mem_ctx, \
530 (const char *)get_string_val->data, \
531 get_string_val->length); \
532 if (!(p)->elem) { \
533 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
534 return WERR_NOMEM; \
537 } while (0)
539 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem) do { \
540 int get_string_list_counter; \
541 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
542 /* We may get empty attributes over the replication channel */ \
543 if (get_string_list_el == NULL || get_string_list_el->num_values == 0) { \
544 (p)->elem = NULL; \
545 break; \
547 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
548 for (get_string_list_counter=0; \
549 get_string_list_counter < get_string_list_el->num_values; \
550 get_string_list_counter++) { \
551 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
552 (const char *)get_string_list_el->values[get_string_list_counter].data, \
553 get_string_list_el->values[get_string_list_counter].length); \
554 if (!(p)->elem[get_string_list_counter]) { \
555 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
556 return WERR_NOMEM; \
558 (p)->elem[get_string_list_counter+1] = NULL; \
560 talloc_steal(mem_ctx, (p)->elem); \
561 } while (0)
563 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
564 const char *str; \
565 str = ldb_msg_find_attr_as_string(msg, attr, NULL);\
566 if (str == NULL) { \
567 if (strict) { \
568 d_printf("%s: %s == NULL\n", __location__, attr); \
569 return WERR_INVALID_PARAM; \
570 } else { \
571 (p)->elem = false; \
573 } else if (strcasecmp("TRUE", str) == 0) { \
574 (p)->elem = true; \
575 } else if (strcasecmp("FALSE", str) == 0) { \
576 (p)->elem = false; \
577 } else { \
578 d_printf("%s: %s == %s\n", __location__, attr, str); \
579 return WERR_INVALID_PARAM; \
581 } while (0)
583 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
584 (p)->elem = ldb_msg_find_attr_as_uint(msg, attr, 0);\
585 } while (0)
587 #define GET_UINT32_PTR_LDB(msg, attr, mem_ctx, p, elem) do { \
588 uint64_t _v = ldb_msg_find_attr_as_uint64(msg, attr, UINT64_MAX);\
589 if (_v == UINT64_MAX) { \
590 (p)->elem = NULL; \
591 } else if (_v > UINT32_MAX) { \
592 d_printf("%s: %s == 0x%llX\n", __location__, \
593 attr, (unsigned long long)_v); \
594 return WERR_INVALID_PARAM; \
595 } else { \
596 (p)->elem = talloc(mem_ctx, uint32_t); \
597 if (!(p)->elem) { \
598 d_printf("%s: talloc failed for %s\n", __location__, attr); \
599 return WERR_NOMEM; \
601 *(p)->elem = (uint32_t)_v; \
603 } while (0)
605 #define GET_GUID_LDB(msg, attr, p, elem) do { \
606 (p)->elem = samdb_result_guid(msg, attr);\
607 } while (0)
609 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
610 const struct ldb_val *_val;\
611 _val = ldb_msg_find_ldb_val(msg, attr);\
612 if (_val) {\
613 (p)->elem = *_val;\
614 talloc_steal(mem_ctx, (p)->elem.data);\
615 } else {\
616 ZERO_STRUCT((p)->elem);\
618 } while (0)
620 /** Create an dsdb_attribute out of ldb message, attr must be already talloced
623 WERROR dsdb_attribute_from_ldb(const struct dsdb_schema *schema,
624 struct ldb_message *msg,
625 struct dsdb_attribute *attr)
627 WERROR status;
628 if (attr == NULL) {
629 DEBUG(0, ("%s: attr is null, it's expected not to be so\n", __location__));
630 return WERR_INVALID_PARAM;
633 GET_STRING_LDB(msg, "cn", attr, attr, cn, false);
634 GET_STRING_LDB(msg, "lDAPDisplayName", attr, attr, lDAPDisplayName, true);
635 GET_STRING_LDB(msg, "attributeID", attr, attr, attributeID_oid, true);
636 if (!schema->prefixmap || schema->prefixmap->length == 0) {
637 /* set an invalid value */
638 attr->attributeID_id = DRSUAPI_ATTID_INVALID;
639 } else {
640 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
641 attr->attributeID_oid,
642 &attr->attributeID_id);
643 if (!W_ERROR_IS_OK(status)) {
644 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
645 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
646 win_errstr(status)));
647 return status;
650 /* fetch msDS-IntId to be used in resolving ATTRTYP values */
651 GET_UINT32_LDB(msg, "msDS-IntId", attr, msDS_IntId);
653 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
654 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
656 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
658 GET_GUID_LDB(msg, "objectGUID", attr, objectGUID);
660 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
661 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
662 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
663 GET_UINT32_LDB(msg, "linkID", attr, linkID);
665 GET_STRING_LDB(msg, "attributeSyntax", attr, attr, attributeSyntax_oid, true);
666 if (!schema->prefixmap || schema->prefixmap->length == 0) {
667 /* set an invalid value */
668 attr->attributeSyntax_id = DRSUAPI_ATTID_INVALID;
669 } else {
670 status = dsdb_schema_pfm_attid_from_oid(schema->prefixmap,
671 attr->attributeSyntax_oid,
672 &attr->attributeSyntax_id);
673 if (!W_ERROR_IS_OK(status)) {
674 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
675 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
676 win_errstr(status)));
677 return status;
680 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
681 GET_BLOB_LDB(msg, "oMObjectClass", attr, attr, oMObjectClass);
683 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
684 GET_UINT32_PTR_LDB(msg, "rangeLower", attr, attr, rangeLower);
685 GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, attr, rangeUpper);
686 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
688 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
689 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", attr, attr, msDs_Schema_Extensions);
691 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
692 GET_STRING_LDB(msg, "adminDisplayName", attr, attr, adminDisplayName, false);
693 GET_STRING_LDB(msg, "adminDescription", attr, attr, adminDescription, false);
694 GET_STRING_LDB(msg, "classDisplayName", attr, attr, classDisplayName, false);
695 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
696 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
697 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
699 return WERR_OK;
702 WERROR dsdb_set_attribute_from_ldb(struct ldb_context *ldb,
703 struct dsdb_schema *schema,
704 struct ldb_message *msg)
706 WERROR status;
707 struct dsdb_attribute *attr = talloc_zero(schema, struct dsdb_attribute);
708 if (!attr) {
709 return WERR_NOMEM;
712 status = dsdb_attribute_from_ldb(schema, msg, attr);
713 if (!W_ERROR_IS_OK(status)) {
714 return status;
717 attr->syntax = dsdb_syntax_for_attribute(attr);
718 if (!attr->syntax) {
719 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
720 attr->lDAPDisplayName));
721 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
724 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
725 DEBUG(0,(__location__ ": Unknown schema syntax for %s - ldb_syntax: %s, ldap_oid: %s\n",
726 attr->lDAPDisplayName,
727 attr->syntax->ldb_syntax,
728 attr->syntax->ldap_oid));
729 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
732 DLIST_ADD(schema->attributes, attr);
733 return WERR_OK;
736 WERROR dsdb_set_class_from_ldb(struct dsdb_schema *schema,
737 struct ldb_message *msg)
739 WERROR status;
740 struct dsdb_class *obj = talloc_zero(schema, struct dsdb_class);
741 if (!obj) {
742 return WERR_NOMEM;
744 GET_STRING_LDB(msg, "cn", obj, obj, cn, false);
745 GET_STRING_LDB(msg, "lDAPDisplayName", obj, obj, lDAPDisplayName, true);
746 GET_STRING_LDB(msg, "governsID", obj, obj, governsID_oid, true);
747 if (!schema->prefixmap || schema->prefixmap->length == 0) {
748 /* set an invalid value */
749 obj->governsID_id = DRSUAPI_ATTID_INVALID;
750 } else {
751 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
752 obj->governsID_oid,
753 &obj->governsID_id);
754 if (!W_ERROR_IS_OK(status)) {
755 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
756 __location__, obj->lDAPDisplayName, obj->governsID_oid,
757 win_errstr(status)));
758 return status;
761 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
762 GET_GUID_LDB(msg, "objectGUID", obj, objectGUID);
764 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
765 GET_STRING_LDB(msg, "rDNAttID", obj, obj, rDNAttID, false);
766 GET_STRING_LDB(msg, "defaultObjectCategory", obj, obj, defaultObjectCategory, true);
768 GET_STRING_LDB(msg, "subClassOf", obj, obj, subClassOf, true);
770 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", obj, obj, systemAuxiliaryClass);
771 GET_STRING_LIST_LDB(msg, "auxiliaryClass", obj, obj, auxiliaryClass);
773 GET_STRING_LIST_LDB(msg, "systemMustContain", obj, obj, systemMustContain);
774 GET_STRING_LIST_LDB(msg, "systemMayContain", obj, obj, systemMayContain);
775 GET_STRING_LIST_LDB(msg, "mustContain", obj, obj, mustContain);
776 GET_STRING_LIST_LDB(msg, "mayContain", obj, obj, mayContain);
778 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", obj, obj, systemPossSuperiors);
779 GET_STRING_LIST_LDB(msg, "possSuperiors", obj, obj, possSuperiors);
781 GET_STRING_LDB(msg, "defaultSecurityDescriptor", obj, obj, defaultSecurityDescriptor, false);
783 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
784 GET_UINT32_LDB(msg, "systemFlags", obj, systemFlags);
785 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", obj, obj, msDs_Schema_Extensions);
787 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
788 GET_STRING_LDB(msg, "adminDisplayName", obj, obj, adminDisplayName, false);
789 GET_STRING_LDB(msg, "adminDescription", obj, obj, adminDescription, false);
790 GET_STRING_LDB(msg, "classDisplayName", obj, obj, classDisplayName, false);
791 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
792 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
793 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
795 DLIST_ADD(schema->classes, obj);
796 return WERR_OK;
799 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
802 Fill a DSDB schema from the ldb results provided. This is called
803 directly when a schema must be created with a pre-initialised prefixMap
806 int dsdb_load_ldb_results_into_schema(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
807 struct dsdb_schema *schema,
808 struct ldb_result *attrs_class_res,
809 char **error_string)
811 unsigned int i;
813 schema->ts_last_change = 0;
814 for (i=0; i < attrs_class_res->count; i++) {
815 WERROR status = dsdb_schema_set_el_from_ldb_msg(ldb, schema, attrs_class_res->msgs[i]);
816 if (!W_ERROR_IS_OK(status)) {
817 *error_string = talloc_asprintf(mem_ctx,
818 "dsdb_load_ldb_results_into_schema: failed to load attribute or class definition: %s:%s",
819 ldb_dn_get_linearized(attrs_class_res->msgs[i]->dn),
820 win_errstr(status));
821 DEBUG(0,(__location__ ": %s\n", *error_string));
822 return LDB_ERR_CONSTRAINT_VIOLATION;
826 return LDB_SUCCESS;
830 Create a DSDB schema from the ldb results provided. This is called
831 directly when the schema is provisioned from an on-disk LDIF file, or
832 from dsdb_schema_from_schema_dn in schema_fsmo
835 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
836 struct ldb_result *schema_res,
837 struct ldb_result *attrs_class_res,
838 struct dsdb_schema **schema_out,
839 char **error_string)
841 WERROR status;
842 const struct ldb_val *prefix_val;
843 const struct ldb_val *info_val;
844 struct ldb_val info_val_default;
845 struct dsdb_schema *schema;
846 void *lp_opaque = ldb_get_opaque(ldb, "loadparm");
847 int ret;
849 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
850 if (!tmp_ctx) {
851 dsdb_oom(error_string, mem_ctx);
852 return ldb_operr(ldb);
855 schema = dsdb_new_schema(tmp_ctx);
856 if (!schema) {
857 dsdb_oom(error_string, mem_ctx);
858 talloc_free(tmp_ctx);
859 return ldb_operr(ldb);
862 if (lp_opaque) {
863 struct loadparm_context *lp_ctx = talloc_get_type_abort(lp_opaque, struct loadparm_context);
864 schema->refresh_interval = lpcfg_parm_int(lp_ctx, NULL, "dsdb", "schema_reload_interval", schema->refresh_interval);
865 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
866 struct loadparm_context);
867 schema->fsmo.update_allowed = lpcfg_parm_bool(lp_ctx, NULL,
868 "dsdb", "schema update allowed",
869 false);
872 schema->base_dn = talloc_steal(schema, schema_res->msgs[0]->dn);
874 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
875 if (!prefix_val) {
876 *error_string = talloc_asprintf(mem_ctx,
877 "schema_fsmo_init: no prefixMap attribute found");
878 DEBUG(0,(__location__ ": %s\n", *error_string));
879 talloc_free(tmp_ctx);
880 return LDB_ERR_CONSTRAINT_VIOLATION;
882 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
883 if (!info_val) {
884 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
885 if (!W_ERROR_IS_OK(status)) {
886 *error_string = talloc_asprintf(mem_ctx,
887 "schema_fsmo_init: dsdb_schema_info_blob_new() failed - %s",
888 win_errstr(status));
889 DEBUG(0,(__location__ ": %s\n", *error_string));
890 talloc_free(tmp_ctx);
891 return ldb_operr(ldb);
893 info_val = &info_val_default;
896 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
897 if (!W_ERROR_IS_OK(status)) {
898 *error_string = talloc_asprintf(mem_ctx,
899 "schema_fsmo_init: failed to load oid mappings: %s",
900 win_errstr(status));
901 DEBUG(0,(__location__ ": %s\n", *error_string));
902 talloc_free(tmp_ctx);
903 return LDB_ERR_CONSTRAINT_VIOLATION;
906 ret = dsdb_load_ldb_results_into_schema(mem_ctx, ldb, schema, attrs_class_res, error_string);
907 if (ret != LDB_SUCCESS) {
908 talloc_free(tmp_ctx);
909 return ret;
912 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
913 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb, tmp_ctx), schema->fsmo.master_dn) == 0) {
914 schema->fsmo.we_are_master = true;
915 } else {
916 schema->fsmo.we_are_master = false;
919 DEBUG(5, ("schema_fsmo_init: we are master[%s] updates allowed[%s]\n",
920 (schema->fsmo.we_are_master?"yes":"no"),
921 (schema->fsmo.update_allowed?"yes":"no")));
923 *schema_out = talloc_steal(mem_ctx, schema);
924 talloc_free(tmp_ctx);
925 return LDB_SUCCESS;