tdb: add overflow detection to tdb_expand_adjust()
[Samba/id10ts.git] / source4 / dsdb / schema / schema_init.c
blobefbd38a4ba39540c994f52152d75b4a58d6e6c3f
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_dups(struct ldb_context *ldb,
703 struct dsdb_schema *schema,
704 struct ldb_message *msg,
705 bool checkdups)
707 WERROR status;
708 struct dsdb_attribute *attr = talloc_zero(schema, struct dsdb_attribute);
709 if (!attr) {
710 return WERR_NOMEM;
713 status = dsdb_attribute_from_ldb(schema, msg, attr);
714 if (!W_ERROR_IS_OK(status)) {
715 return status;
718 attr->syntax = dsdb_syntax_for_attribute(attr);
719 if (!attr->syntax) {
720 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
721 attr->lDAPDisplayName));
722 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
725 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
726 DEBUG(0,(__location__ ": Unknown schema syntax for %s - ldb_syntax: %s, ldap_oid: %s\n",
727 attr->lDAPDisplayName,
728 attr->syntax->ldb_syntax,
729 attr->syntax->ldap_oid));
730 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
733 if (checkdups) {
734 const struct dsdb_attribute *a2;
735 struct dsdb_attribute **a;
736 uint32_t i;
738 a2 = dsdb_attribute_by_attributeID_id(schema,
739 attr->attributeID_id);
740 if (a2 == NULL) {
741 goto done;
744 i = schema->attributes_to_remove_size;
745 a = talloc_realloc(schema, schema->attributes_to_remove,
746 struct dsdb_attribute *, i + 1);
747 if (a == NULL) {
748 return WERR_NOMEM;
750 /* Mark the old attribute as to be removed */
751 a[i] = discard_const_p(struct dsdb_attribute, a2);
752 schema->attributes_to_remove = a;
753 schema->attributes_to_remove_size++;
756 done:
757 DLIST_ADD(schema->attributes, attr);
758 return WERR_OK;
761 WERROR dsdb_set_attribute_from_ldb(struct ldb_context *ldb,
762 struct dsdb_schema *schema,
763 struct ldb_message *msg)
765 return dsdb_set_attribute_from_ldb_dups(ldb, schema, msg, false);
768 WERROR dsdb_set_class_from_ldb_dups(struct dsdb_schema *schema,
769 struct ldb_message *msg,
770 bool checkdups)
772 WERROR status;
773 struct dsdb_class *obj = talloc_zero(schema, struct dsdb_class);
774 if (!obj) {
775 return WERR_NOMEM;
777 GET_STRING_LDB(msg, "cn", obj, obj, cn, false);
778 GET_STRING_LDB(msg, "lDAPDisplayName", obj, obj, lDAPDisplayName, true);
779 GET_STRING_LDB(msg, "governsID", obj, obj, governsID_oid, true);
780 if (!schema->prefixmap || schema->prefixmap->length == 0) {
781 /* set an invalid value */
782 obj->governsID_id = DRSUAPI_ATTID_INVALID;
783 } else {
784 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
785 obj->governsID_oid,
786 &obj->governsID_id);
787 if (!W_ERROR_IS_OK(status)) {
788 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
789 __location__, obj->lDAPDisplayName, obj->governsID_oid,
790 win_errstr(status)));
791 return status;
794 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
795 GET_GUID_LDB(msg, "objectGUID", obj, objectGUID);
797 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
798 GET_STRING_LDB(msg, "rDNAttID", obj, obj, rDNAttID, false);
799 GET_STRING_LDB(msg, "defaultObjectCategory", obj, obj, defaultObjectCategory, true);
801 GET_STRING_LDB(msg, "subClassOf", obj, obj, subClassOf, true);
803 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", obj, obj, systemAuxiliaryClass);
804 GET_STRING_LIST_LDB(msg, "auxiliaryClass", obj, obj, auxiliaryClass);
806 GET_STRING_LIST_LDB(msg, "systemMustContain", obj, obj, systemMustContain);
807 GET_STRING_LIST_LDB(msg, "systemMayContain", obj, obj, systemMayContain);
808 GET_STRING_LIST_LDB(msg, "mustContain", obj, obj, mustContain);
809 GET_STRING_LIST_LDB(msg, "mayContain", obj, obj, mayContain);
811 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", obj, obj, systemPossSuperiors);
812 GET_STRING_LIST_LDB(msg, "possSuperiors", obj, obj, possSuperiors);
814 GET_STRING_LDB(msg, "defaultSecurityDescriptor", obj, obj, defaultSecurityDescriptor, false);
816 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
817 GET_UINT32_LDB(msg, "systemFlags", obj, systemFlags);
818 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", obj, obj, msDs_Schema_Extensions);
820 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
821 GET_STRING_LDB(msg, "adminDisplayName", obj, obj, adminDisplayName, false);
822 GET_STRING_LDB(msg, "adminDescription", obj, obj, adminDescription, false);
823 GET_STRING_LDB(msg, "classDisplayName", obj, obj, classDisplayName, false);
824 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
825 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
826 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
828 if (checkdups) {
829 const struct dsdb_class *c2;
830 struct dsdb_class **c;
831 uint32_t i;
833 c2 = dsdb_class_by_governsID_id(schema, obj->governsID_id);
834 if (c2 == NULL) {
835 goto done;
838 i = schema->classes_to_remove_size;
839 c = talloc_realloc(schema, schema->classes_to_remove,
840 struct dsdb_class *, i + 1);
841 if (c == NULL) {
842 return WERR_NOMEM;
844 /* Mark the old class to be removed */
845 c[i] = discard_const_p(struct dsdb_class, c2);
846 schema->classes_to_remove = c;
847 schema->classes_to_remove_size++;
850 done:
851 DLIST_ADD(schema->classes, obj);
852 return WERR_OK;
855 WERROR dsdb_set_class_from_ldb(struct dsdb_schema *schema,
856 struct ldb_message *msg)
858 return dsdb_set_class_from_ldb_dups(schema, msg, false);
861 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
864 Fill a DSDB schema from the ldb results provided. This is called
865 directly when a schema must be created with a pre-initialised prefixMap
868 int dsdb_load_ldb_results_into_schema(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
869 struct dsdb_schema *schema,
870 struct ldb_result *attrs_class_res,
871 char **error_string)
873 unsigned int i;
875 schema->ts_last_change = 0;
876 for (i=0; i < attrs_class_res->count; i++) {
877 WERROR status = dsdb_schema_set_el_from_ldb_msg(ldb, schema, attrs_class_res->msgs[i]);
878 if (!W_ERROR_IS_OK(status)) {
879 *error_string = talloc_asprintf(mem_ctx,
880 "dsdb_load_ldb_results_into_schema: failed to load attribute or class definition: %s:%s",
881 ldb_dn_get_linearized(attrs_class_res->msgs[i]->dn),
882 win_errstr(status));
883 DEBUG(0,(__location__ ": %s\n", *error_string));
884 return LDB_ERR_CONSTRAINT_VIOLATION;
888 return LDB_SUCCESS;
892 Create a DSDB schema from the ldb results provided. This is called
893 directly when the schema is provisioned from an on-disk LDIF file, or
894 from dsdb_schema_from_schema_dn in schema_fsmo
897 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
898 struct ldb_result *schema_res,
899 struct ldb_result *attrs_class_res,
900 struct dsdb_schema **schema_out,
901 char **error_string)
903 WERROR status;
904 const struct ldb_val *prefix_val;
905 const struct ldb_val *info_val;
906 struct ldb_val info_val_default;
907 struct dsdb_schema *schema;
908 void *lp_opaque = ldb_get_opaque(ldb, "loadparm");
909 int ret;
911 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
912 if (!tmp_ctx) {
913 dsdb_oom(error_string, mem_ctx);
914 return ldb_operr(ldb);
917 schema = dsdb_new_schema(tmp_ctx);
918 if (!schema) {
919 dsdb_oom(error_string, mem_ctx);
920 talloc_free(tmp_ctx);
921 return ldb_operr(ldb);
924 if (lp_opaque) {
925 struct loadparm_context *lp_ctx = talloc_get_type_abort(lp_opaque, struct loadparm_context);
926 schema->refresh_interval = lpcfg_parm_int(lp_ctx, NULL, "dsdb", "schema_reload_interval", schema->refresh_interval);
927 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
928 struct loadparm_context);
929 schema->fsmo.update_allowed = lpcfg_parm_bool(lp_ctx, NULL,
930 "dsdb", "schema update allowed",
931 false);
934 schema->base_dn = talloc_steal(schema, schema_res->msgs[0]->dn);
936 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
937 if (!prefix_val) {
938 *error_string = talloc_asprintf(mem_ctx,
939 "schema_fsmo_init: no prefixMap attribute found");
940 DEBUG(0,(__location__ ": %s\n", *error_string));
941 talloc_free(tmp_ctx);
942 return LDB_ERR_CONSTRAINT_VIOLATION;
944 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
945 if (!info_val) {
946 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
947 if (!W_ERROR_IS_OK(status)) {
948 *error_string = talloc_asprintf(mem_ctx,
949 "schema_fsmo_init: dsdb_schema_info_blob_new() failed - %s",
950 win_errstr(status));
951 DEBUG(0,(__location__ ": %s\n", *error_string));
952 talloc_free(tmp_ctx);
953 return ldb_operr(ldb);
955 info_val = &info_val_default;
958 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
959 if (!W_ERROR_IS_OK(status)) {
960 *error_string = talloc_asprintf(mem_ctx,
961 "schema_fsmo_init: failed to load oid mappings: %s",
962 win_errstr(status));
963 DEBUG(0,(__location__ ": %s\n", *error_string));
964 talloc_free(tmp_ctx);
965 return LDB_ERR_CONSTRAINT_VIOLATION;
968 ret = dsdb_load_ldb_results_into_schema(mem_ctx, ldb, schema, attrs_class_res, error_string);
969 if (ret != LDB_SUCCESS) {
970 talloc_free(tmp_ctx);
971 return ret;
974 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
975 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb, tmp_ctx), schema->fsmo.master_dn) == 0) {
976 schema->fsmo.we_are_master = true;
977 } else {
978 schema->fsmo.we_are_master = false;
981 DEBUG(5, ("schema_fsmo_init: we are master[%s] updates allowed[%s]\n",
982 (schema->fsmo.we_are_master?"yes":"no"),
983 (schema->fsmo.update_allowed?"yes":"no")));
985 *schema_out = talloc_steal(mem_ctx, schema);
986 talloc_free(tmp_ctx);
987 return LDB_SUCCESS;