dsdb: Do not refresh the schema using the wrong event context
[Samba/wip.git] / source4 / dsdb / schema / schema_init.c
blobd03880792417f42a56c0b6f5827a1912444a684d
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 /* copy prexiMap & schemaInfo */
62 schema_copy->prefixmap = dsdb_schema_pfm_copy_shallow(schema_copy,
63 schema->prefixmap);
64 if (!schema_copy->prefixmap) {
65 goto failed;
68 schema_copy->schema_info = talloc_strdup(schema_copy, schema->schema_info);
70 /* copy classes and attributes*/
71 for (cls = schema->classes; cls; cls = cls->next) {
72 struct dsdb_class *class_copy = talloc_memdup(schema_copy,
73 cls, sizeof(*cls));
74 if (!class_copy) {
75 goto failed;
77 DLIST_ADD(schema_copy->classes, class_copy);
79 schema_copy->num_classes = schema->num_classes;
81 for (attr = schema->attributes; attr; attr = attr->next) {
82 struct dsdb_attribute *a_copy = talloc_memdup(schema_copy,
83 attr, sizeof(*attr));
84 if (!a_copy) {
85 goto failed;
87 DLIST_ADD(schema_copy->attributes, a_copy);
89 schema_copy->num_attributes = schema->num_attributes;
91 schema_copy->refresh_interval = schema->refresh_interval;
93 /* rebuild indexes */
94 ret = dsdb_setup_sorted_accessors(ldb, schema_copy);
95 if (ret != LDB_SUCCESS) {
96 goto failed;
99 /* leave reload_seq_number = 0 so it will be refresh ASAP */
101 return schema_copy;
103 failed:
104 talloc_free(schema_copy);
105 return NULL;
109 WERROR dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema *schema,
110 const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
112 WERROR werr;
113 const char *schema_info;
114 struct dsdb_schema_prefixmap *pfm;
116 werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, schema, &pfm, &schema_info);
117 W_ERROR_NOT_OK_RETURN(werr);
119 /* set loaded prefixMap */
120 talloc_free(schema->prefixmap);
121 schema->prefixmap = pfm;
123 talloc_free(discard_const(schema->schema_info));
124 schema->schema_info = schema_info;
126 return WERR_OK;
129 static WERROR _dsdb_prefixmap_from_ldb_val(const struct ldb_val *pfm_ldb_val,
130 TALLOC_CTX *mem_ctx,
131 struct dsdb_schema_prefixmap **_pfm)
133 WERROR werr;
134 enum ndr_err_code ndr_err;
135 struct prefixMapBlob pfm_blob;
137 TALLOC_CTX *temp_ctx = talloc_new(mem_ctx);
138 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
140 ndr_err = ndr_pull_struct_blob(pfm_ldb_val, temp_ctx,
141 &pfm_blob,
142 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
143 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
144 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
145 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: Failed to parse prefixmap of length %u: %s\n",
146 (unsigned int)pfm_ldb_val->length, ndr_map_error2string(ndr_err)));
147 talloc_free(temp_ctx);
148 return ntstatus_to_werror(nt_status);
151 if (pfm_blob.version != PREFIX_MAP_VERSION_DSDB) {
152 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version %u incorrect\n", (unsigned int)pfm_blob.version));
153 talloc_free(temp_ctx);
154 return WERR_VERSION_PARSE_ERROR;
157 /* call the drsuapi version */
158 werr = dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob.ctr.dsdb, false, mem_ctx, _pfm, NULL);
159 if (!W_ERROR_IS_OK(werr)) {
160 DEBUG(0, (__location__ " dsdb_schema_pfm_from_drsuapi_pfm failed: %s\n", win_errstr(werr)));
161 talloc_free(temp_ctx);
162 return werr;
165 talloc_free(temp_ctx);
167 return werr;
170 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
171 const struct ldb_val *prefixMap,
172 const struct ldb_val *schemaInfo)
174 WERROR werr;
175 const char *schema_info;
176 struct dsdb_schema_prefixmap *pfm;
177 TALLOC_CTX *mem_ctx;
179 /* verify schemaInfo blob is valid one */
180 if (!dsdb_schema_info_blob_is_valid(schemaInfo)) {
181 DEBUG(0,(__location__": dsdb_schema_info_blob_is_valid() failed.\n"));
182 return WERR_INVALID_PARAMETER;
185 mem_ctx = talloc_new(schema);
186 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
188 /* fetch prefixMap */
189 werr = _dsdb_prefixmap_from_ldb_val(prefixMap,
190 mem_ctx, &pfm);
191 if (!W_ERROR_IS_OK(werr)) {
192 DEBUG(0, (__location__ " _dsdb_prefixmap_from_ldb_val failed: %s\n", win_errstr(werr)));
193 talloc_free(mem_ctx);
194 return werr;
197 /* decode schema_info */
198 schema_info = hex_encode_talloc(mem_ctx,
199 schemaInfo->data,
200 schemaInfo->length);
201 if (!schema_info) {
202 talloc_free(mem_ctx);
203 return WERR_NOMEM;
206 /* store prefixMap and schema_info into cached Schema */
207 talloc_free(schema->prefixmap);
208 schema->prefixmap = talloc_steal(schema, pfm);
210 talloc_free(discard_const(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 *schemaInfo = strhex_to_data_blob(mem_ctx, schema->schema_info);
264 W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
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);
626 GET_STRING_LDB(msg, "lDAPDisplayName", attr, attr, lDAPDisplayName, true);
627 GET_STRING_LDB(msg, "attributeID", attr, attr, attributeID_oid, true);
628 if (!schema->prefixmap || schema->prefixmap->length == 0) {
629 /* set an invalid value */
630 attr->attributeID_id = DRSUAPI_ATTID_INVALID;
631 } else {
632 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
633 attr->attributeID_oid,
634 &attr->attributeID_id);
635 if (!W_ERROR_IS_OK(status)) {
636 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
637 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
638 win_errstr(status)));
639 return status;
642 /* fetch msDS-IntId to be used in resolving ATTRTYP values */
643 GET_UINT32_LDB(msg, "msDS-IntId", attr, msDS_IntId);
645 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
646 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
648 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
650 GET_GUID_LDB(msg, "objectGUID", attr, objectGUID);
652 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
653 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
654 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
655 GET_UINT32_LDB(msg, "linkID", attr, linkID);
657 GET_STRING_LDB(msg, "attributeSyntax", attr, attr, attributeSyntax_oid, true);
658 if (!schema->prefixmap || schema->prefixmap->length == 0) {
659 /* set an invalid value */
660 attr->attributeSyntax_id = DRSUAPI_ATTID_INVALID;
661 } else {
662 status = dsdb_schema_pfm_attid_from_oid(schema->prefixmap,
663 attr->attributeSyntax_oid,
664 &attr->attributeSyntax_id);
665 if (!W_ERROR_IS_OK(status)) {
666 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
667 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
668 win_errstr(status)));
669 return status;
672 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
673 GET_BLOB_LDB(msg, "oMObjectClass", attr, attr, oMObjectClass);
675 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
676 GET_UINT32_PTR_LDB(msg, "rangeLower", attr, attr, rangeLower);
677 GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, attr, rangeUpper);
678 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
680 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
681 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", attr, attr, msDs_Schema_Extensions);
683 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
684 GET_STRING_LDB(msg, "adminDisplayName", attr, attr, adminDisplayName, false);
685 GET_STRING_LDB(msg, "adminDescription", attr, attr, adminDescription, false);
686 GET_STRING_LDB(msg, "classDisplayName", attr, attr, classDisplayName, false);
687 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
688 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
689 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
691 return WERR_OK;
694 WERROR dsdb_set_attribute_from_ldb_dups(struct ldb_context *ldb,
695 struct dsdb_schema *schema,
696 struct ldb_message *msg,
697 bool checkdups)
699 WERROR status;
700 struct dsdb_attribute *attr = talloc_zero(schema, struct dsdb_attribute);
701 if (!attr) {
702 return WERR_NOMEM;
705 status = dsdb_attribute_from_ldb(schema, msg, attr);
706 if (!W_ERROR_IS_OK(status)) {
707 return status;
710 attr->syntax = dsdb_syntax_for_attribute(attr);
711 if (!attr->syntax) {
712 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
713 attr->lDAPDisplayName));
714 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
717 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
718 DEBUG(0,(__location__ ": Unknown schema syntax for %s - ldb_syntax: %s, ldap_oid: %s\n",
719 attr->lDAPDisplayName,
720 attr->syntax->ldb_syntax,
721 attr->syntax->ldap_oid));
722 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
725 if (checkdups) {
726 const struct dsdb_attribute *a2;
727 struct dsdb_attribute **a;
728 uint32_t i;
730 a2 = dsdb_attribute_by_attributeID_id(schema,
731 attr->attributeID_id);
732 if (a2 == NULL) {
733 goto done;
736 i = schema->attributes_to_remove_size;
737 a = talloc_realloc(schema, schema->attributes_to_remove,
738 struct dsdb_attribute *, i + 1);
739 if (a == NULL) {
740 return WERR_NOMEM;
742 /* Mark the old attribute as to be removed */
743 a[i] = discard_const_p(struct dsdb_attribute, a2);
744 schema->attributes_to_remove = a;
745 schema->attributes_to_remove_size++;
748 done:
749 DLIST_ADD(schema->attributes, attr);
750 return WERR_OK;
753 WERROR dsdb_set_attribute_from_ldb(struct ldb_context *ldb,
754 struct dsdb_schema *schema,
755 struct ldb_message *msg)
757 return dsdb_set_attribute_from_ldb_dups(ldb, schema, msg, false);
760 WERROR dsdb_set_class_from_ldb_dups(struct dsdb_schema *schema,
761 struct ldb_message *msg,
762 bool checkdups)
764 WERROR status;
765 struct dsdb_class *obj = talloc_zero(schema, struct dsdb_class);
766 if (!obj) {
767 return WERR_NOMEM;
769 GET_STRING_LDB(msg, "cn", obj, obj, cn, false);
770 GET_STRING_LDB(msg, "lDAPDisplayName", obj, obj, lDAPDisplayName, true);
771 GET_STRING_LDB(msg, "governsID", obj, obj, governsID_oid, true);
772 if (!schema->prefixmap || schema->prefixmap->length == 0) {
773 /* set an invalid value */
774 obj->governsID_id = DRSUAPI_ATTID_INVALID;
775 } else {
776 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
777 obj->governsID_oid,
778 &obj->governsID_id);
779 if (!W_ERROR_IS_OK(status)) {
780 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
781 __location__, obj->lDAPDisplayName, obj->governsID_oid,
782 win_errstr(status)));
783 return status;
786 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
787 GET_GUID_LDB(msg, "objectGUID", obj, objectGUID);
789 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
790 GET_STRING_LDB(msg, "rDNAttID", obj, obj, rDNAttID, false);
791 GET_STRING_LDB(msg, "defaultObjectCategory", obj, obj, defaultObjectCategory, true);
793 GET_STRING_LDB(msg, "subClassOf", obj, obj, subClassOf, true);
795 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", obj, obj, systemAuxiliaryClass);
796 GET_STRING_LIST_LDB(msg, "auxiliaryClass", obj, obj, auxiliaryClass);
798 GET_STRING_LIST_LDB(msg, "systemMustContain", obj, obj, systemMustContain);
799 GET_STRING_LIST_LDB(msg, "systemMayContain", obj, obj, systemMayContain);
800 GET_STRING_LIST_LDB(msg, "mustContain", obj, obj, mustContain);
801 GET_STRING_LIST_LDB(msg, "mayContain", obj, obj, mayContain);
803 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", obj, obj, systemPossSuperiors);
804 GET_STRING_LIST_LDB(msg, "possSuperiors", obj, obj, possSuperiors);
806 GET_STRING_LDB(msg, "defaultSecurityDescriptor", obj, obj, defaultSecurityDescriptor, false);
808 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
809 GET_UINT32_LDB(msg, "systemFlags", obj, systemFlags);
810 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", obj, obj, msDs_Schema_Extensions);
812 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
813 GET_STRING_LDB(msg, "adminDisplayName", obj, obj, adminDisplayName, false);
814 GET_STRING_LDB(msg, "adminDescription", obj, obj, adminDescription, false);
815 GET_STRING_LDB(msg, "classDisplayName", obj, obj, classDisplayName, false);
816 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
817 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
818 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
820 if (checkdups) {
821 const struct dsdb_class *c2;
822 struct dsdb_class **c;
823 uint32_t i;
825 c2 = dsdb_class_by_governsID_id(schema, obj->governsID_id);
826 if (c2 == NULL) {
827 goto done;
830 i = schema->classes_to_remove_size;
831 c = talloc_realloc(schema, schema->classes_to_remove,
832 struct dsdb_class *, i + 1);
833 if (c == NULL) {
834 return WERR_NOMEM;
836 /* Mark the old class to be removed */
837 c[i] = discard_const_p(struct dsdb_class, c2);
838 schema->classes_to_remove = c;
839 schema->classes_to_remove_size++;
842 done:
843 DLIST_ADD(schema->classes, obj);
844 return WERR_OK;
847 WERROR dsdb_set_class_from_ldb(struct dsdb_schema *schema,
848 struct ldb_message *msg)
850 return dsdb_set_class_from_ldb_dups(schema, msg, false);
853 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
856 Fill a DSDB schema from the ldb results provided. This is called
857 directly when a schema must be created with a pre-initialised prefixMap
860 int dsdb_load_ldb_results_into_schema(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
861 struct dsdb_schema *schema,
862 struct ldb_result *attrs_class_res,
863 char **error_string)
865 unsigned int i;
867 schema->ts_last_change = 0;
868 for (i=0; i < attrs_class_res->count; i++) {
869 WERROR status = dsdb_schema_set_el_from_ldb_msg(ldb, schema, attrs_class_res->msgs[i]);
870 if (!W_ERROR_IS_OK(status)) {
871 *error_string = talloc_asprintf(mem_ctx,
872 "dsdb_load_ldb_results_into_schema: failed to load attribute or class definition: %s:%s",
873 ldb_dn_get_linearized(attrs_class_res->msgs[i]->dn),
874 win_errstr(status));
875 DEBUG(0,(__location__ ": %s\n", *error_string));
876 return LDB_ERR_CONSTRAINT_VIOLATION;
880 return LDB_SUCCESS;
884 Create a DSDB schema from the ldb results provided. This is called
885 directly when the schema is provisioned from an on-disk LDIF file, or
886 from dsdb_schema_from_schema_dn in schema_fsmo
889 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
890 struct ldb_result *schema_res,
891 struct ldb_result *attrs_class_res,
892 struct dsdb_schema **schema_out,
893 char **error_string)
895 WERROR status;
896 const struct ldb_val *prefix_val;
897 const struct ldb_val *info_val;
898 struct ldb_val info_val_default;
899 struct dsdb_schema *schema;
900 void *lp_opaque = ldb_get_opaque(ldb, "loadparm");
901 int ret;
903 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
904 if (!tmp_ctx) {
905 dsdb_oom(error_string, mem_ctx);
906 return ldb_operr(ldb);
909 schema = dsdb_new_schema(tmp_ctx);
910 if (!schema) {
911 dsdb_oom(error_string, mem_ctx);
912 talloc_free(tmp_ctx);
913 return ldb_operr(ldb);
916 if (lp_opaque) {
917 struct loadparm_context *lp_ctx = talloc_get_type_abort(lp_opaque, struct loadparm_context);
918 schema->refresh_interval = lpcfg_parm_int(lp_ctx, NULL, "dsdb", "schema_reload_interval", schema->refresh_interval);
919 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
920 struct loadparm_context);
921 schema->fsmo.update_allowed = lpcfg_parm_bool(lp_ctx, NULL,
922 "dsdb", "schema update allowed",
923 false);
926 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
927 if (!prefix_val) {
928 *error_string = talloc_asprintf(mem_ctx,
929 "schema_fsmo_init: no prefixMap attribute found");
930 DEBUG(0,(__location__ ": %s\n", *error_string));
931 talloc_free(tmp_ctx);
932 return LDB_ERR_CONSTRAINT_VIOLATION;
934 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
935 if (!info_val) {
936 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
937 if (!W_ERROR_IS_OK(status)) {
938 *error_string = talloc_asprintf(mem_ctx,
939 "schema_fsmo_init: dsdb_schema_info_blob_new() failed - %s",
940 win_errstr(status));
941 DEBUG(0,(__location__ ": %s\n", *error_string));
942 talloc_free(tmp_ctx);
943 return ldb_operr(ldb);
945 info_val = &info_val_default;
948 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
949 if (!W_ERROR_IS_OK(status)) {
950 *error_string = talloc_asprintf(mem_ctx,
951 "schema_fsmo_init: failed to load oid mappings: %s",
952 win_errstr(status));
953 DEBUG(0,(__location__ ": %s\n", *error_string));
954 talloc_free(tmp_ctx);
955 return LDB_ERR_CONSTRAINT_VIOLATION;
958 ret = dsdb_load_ldb_results_into_schema(mem_ctx, ldb, schema, attrs_class_res, error_string);
959 if (ret != LDB_SUCCESS) {
960 talloc_free(tmp_ctx);
961 return ret;
964 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
965 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb, tmp_ctx), schema->fsmo.master_dn) == 0) {
966 schema->fsmo.we_are_master = true;
967 } else {
968 schema->fsmo.we_are_master = false;
971 DEBUG(5, ("schema_fsmo_init: we are master[%s] updates allowed[%s]\n",
972 (schema->fsmo.we_are_master?"yes":"no"),
973 (schema->fsmo.update_allowed?"yes":"no")));
975 *schema_out = talloc_steal(mem_ctx, schema);
976 talloc_free(tmp_ctx);
977 return LDB_SUCCESS;