torture4: Replace direct netlogon ping calls with netlogon_pings()
[samba4-gss.git] / source4 / dsdb / schema / schema_init.c
blobc8197b86306144bb47af4d260f8266b95eea1a50
1 /*
2 Unix SMB/CIFS Implementation.
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"
35 #undef strcasecmp
37 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx)
39 struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
40 if (!schema) {
41 return NULL;
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(schema_copy, struct dsdb_schema_info);
69 if (!schema_copy->schema_info) {
70 goto failed;
72 *schema_copy->schema_info = *schema->schema_info;
74 /* copy classes and attributes*/
75 for (cls = schema->classes; cls; cls = cls->next) {
76 struct dsdb_class *class_copy = talloc_memdup(schema_copy,
77 cls, sizeof(*cls));
78 if (!class_copy) {
79 goto failed;
81 DLIST_ADD(schema_copy->classes, class_copy);
83 schema_copy->num_classes = schema->num_classes;
85 for (attr = schema->attributes; attr; attr = attr->next) {
86 struct dsdb_attribute *a_copy = talloc_memdup(schema_copy,
87 attr, sizeof(*attr));
88 if (!a_copy) {
89 goto failed;
91 DLIST_ADD(schema_copy->attributes, a_copy);
93 schema_copy->num_attributes = schema->num_attributes;
95 /* rebuild indexes */
96 ret = dsdb_setup_sorted_accessors(ldb, schema_copy);
97 if (ret != LDB_SUCCESS) {
98 goto failed;
101 /* leave reload_seq_number = 0 so it will be refresh ASAP */
103 return schema_copy;
105 failed:
106 talloc_free(schema_copy);
107 return NULL;
111 WERROR dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema *schema,
112 const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
114 WERROR werr;
115 struct dsdb_schema_info *schema_info = NULL;
116 struct dsdb_schema_prefixmap *pfm = NULL;
118 werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, schema, &pfm, &schema_info);
119 W_ERROR_NOT_OK_RETURN(werr);
121 /* set loaded prefixMap */
122 talloc_free(schema->prefixmap);
123 schema->prefixmap = pfm;
125 talloc_free(schema->schema_info);
126 schema->schema_info = schema_info;
128 return WERR_OK;
131 static WERROR _dsdb_prefixmap_from_ldb_val(const struct ldb_val *pfm_ldb_val,
132 TALLOC_CTX *mem_ctx,
133 struct dsdb_schema_prefixmap **_pfm)
135 WERROR werr;
136 enum ndr_err_code ndr_err;
137 struct prefixMapBlob pfm_blob;
139 TALLOC_CTX *temp_ctx = talloc_new(mem_ctx);
140 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
142 ndr_err = ndr_pull_struct_blob(pfm_ldb_val, temp_ctx,
143 &pfm_blob,
144 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
145 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
146 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
147 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: Failed to parse prefixmap of length %u: %s\n",
148 (unsigned int)pfm_ldb_val->length, ndr_map_error2string(ndr_err)));
149 talloc_free(temp_ctx);
150 return ntstatus_to_werror(nt_status);
153 if (pfm_blob.version != PREFIX_MAP_VERSION_DSDB) {
154 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version %u incorrect\n", (unsigned int)pfm_blob.version));
155 talloc_free(temp_ctx);
156 return WERR_VERSION_PARSE_ERROR;
159 /* call the drsuapi version */
160 werr = dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob.ctr.dsdb, false, mem_ctx, _pfm, NULL);
161 if (!W_ERROR_IS_OK(werr)) {
162 DEBUG(0, (__location__ " dsdb_schema_pfm_from_drsuapi_pfm failed: %s\n", win_errstr(werr)));
163 talloc_free(temp_ctx);
164 return werr;
167 talloc_free(temp_ctx);
169 return werr;
172 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
173 const struct ldb_val *prefixMap,
174 const struct ldb_val *schemaInfo)
176 WERROR werr;
177 struct dsdb_schema_info *schema_info = NULL;
178 struct dsdb_schema_prefixmap *pfm = NULL;
179 TALLOC_CTX *mem_ctx;
181 /* verify schemaInfo blob is valid one */
182 if (!dsdb_schema_info_blob_is_valid(schemaInfo)) {
183 DEBUG(0,(__location__": dsdb_schema_info_blob_is_valid() failed.\n"));
184 return WERR_INVALID_PARAMETER;
187 mem_ctx = talloc_new(schema);
188 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
190 /* fetch prefixMap */
191 werr = _dsdb_prefixmap_from_ldb_val(prefixMap,
192 mem_ctx, &pfm);
193 if (!W_ERROR_IS_OK(werr)) {
194 DEBUG(0, (__location__ " _dsdb_prefixmap_from_ldb_val failed: %s\n", win_errstr(werr)));
195 talloc_free(mem_ctx);
196 return werr;
199 /* decode schema_info */
200 werr = dsdb_schema_info_from_blob(schemaInfo, mem_ctx, &schema_info);
201 if (!W_ERROR_IS_OK(werr)) {
202 DEBUG(0, (__location__ " dsdb_schema_info_from_blob failed: %s\n", win_errstr(werr)));
203 talloc_free(mem_ctx);
204 return werr;
207 /* store prefixMap and schema_info into cached Schema */
208 talloc_free(schema->prefixmap);
209 schema->prefixmap = talloc_steal(schema, pfm);
211 talloc_free(schema->schema_info);
212 schema->schema_info = talloc_steal(schema, schema_info);
214 /* clean up locally allocated mem */
215 talloc_free(mem_ctx);
217 return WERR_OK;
220 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
221 bool include_schema_info,
222 TALLOC_CTX *mem_ctx,
223 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
225 return dsdb_drsuapi_pfm_from_schema_pfm(schema->prefixmap,
226 include_schema_info ? schema->schema_info : NULL,
227 mem_ctx, _ctr);
230 WERROR dsdb_get_drsuapi_prefixmap_as_blob(const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr,
231 TALLOC_CTX *mem_ctx,
232 struct ldb_val *prefixMap)
234 struct prefixMapBlob pfm;
235 enum ndr_err_code ndr_err;
236 pfm.version = PREFIX_MAP_VERSION_DSDB;
237 pfm.reserved = 0;
238 pfm.ctr.dsdb = *ctr;
240 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, &pfm,
241 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
242 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
243 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
244 return ntstatus_to_werror(nt_status);
246 return WERR_OK;
249 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
250 TALLOC_CTX *mem_ctx,
251 struct ldb_val *prefixMap,
252 struct ldb_val *schemaInfo)
254 WERROR status;
255 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
257 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
258 W_ERROR_NOT_OK_RETURN(status);
260 status = dsdb_get_drsuapi_prefixmap_as_blob(ctr, mem_ctx, prefixMap);
261 talloc_free(ctr);
262 W_ERROR_NOT_OK_RETURN(status);
264 status = dsdb_blob_from_schema_info(schema->schema_info, mem_ctx, schemaInfo);
265 W_ERROR_NOT_OK_RETURN(status);
267 return WERR_OK;
272 * this function is called from within a ldb transaction from the schema_fsmo module
274 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
276 WERROR status;
277 uint32_t attid;
278 TALLOC_CTX *mem_ctx;
279 struct dsdb_schema_prefixmap *pfm;
280 struct dsdb_schema_prefixmap *orig_pfm = NULL;
282 mem_ctx = talloc_new(ldb);
283 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
285 /* Read prefixes from disk*/
286 status = dsdb_read_prefixes_from_ldb(ldb, mem_ctx, &pfm);
287 if (!W_ERROR_IS_OK(status)) {
288 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
289 win_errstr(status)));
290 talloc_free(mem_ctx);
291 return status;
294 /* Check if there is a prefix for the oid in the prefixes array*/
295 status = dsdb_schema_pfm_find_oid(pfm, full_oid, NULL);
296 if (W_ERROR_IS_OK(status)) {
297 /* prefix found*/
298 talloc_free(mem_ctx);
299 return status;
300 } else if (!W_ERROR_EQUAL(status, WERR_NOT_FOUND)) {
301 /* error */
302 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
303 win_errstr(status)));
304 talloc_free(mem_ctx);
305 return status;
308 /* Create the new mapping for the prefix of full_oid */
309 status = dsdb_schema_pfm_make_attid(pfm, full_oid, &attid);
310 if (!W_ERROR_IS_OK(status)) {
311 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_schema_pfm_make_attid: %s\n",
312 win_errstr(status)));
313 talloc_free(mem_ctx);
314 return status;
318 * We temporary replcate schema->prefixmap.
320 orig_pfm = schema->prefixmap;
321 schema->prefixmap = pfm;
323 /* Update prefixMap in ldb*/
324 status = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, ldb, schema);
325 if (!W_ERROR_IS_OK(status)) {
326 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
327 win_errstr(status)));
328 talloc_free(mem_ctx);
329 return status;
332 DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
333 full_oid, schema->prefixmap->length));
336 * We restore the original prefix map.
338 * The next schema reload should get an updated prefix map!
340 schema->prefixmap = orig_pfm;
342 talloc_free(mem_ctx);
343 return status;
347 WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
348 const struct dsdb_schema *schema)
350 WERROR status;
351 int ldb_ret;
352 struct ldb_message *msg;
353 struct ldb_dn *schema_dn;
354 struct prefixMapBlob pfm_blob;
355 struct ldb_val ndr_blob;
356 enum ndr_err_code ndr_err;
357 TALLOC_CTX *temp_ctx;
358 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
360 schema_dn = ldb_get_schema_basedn(ldb);
361 if (!schema_dn) {
362 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));
363 return WERR_FOOBAR;
366 temp_ctx = talloc_new(mem_ctx);
367 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
369 /* convert schema_prefixMap to prefixMap blob */
370 status = dsdb_get_oid_mappings_drsuapi(schema, false, temp_ctx, &ctr);
371 if (!W_ERROR_IS_OK(status)) {
372 talloc_free(temp_ctx);
373 return status;
376 pfm_blob.version = PREFIX_MAP_VERSION_DSDB;
377 pfm_blob.ctr.dsdb = *ctr;
379 ndr_err = ndr_push_struct_blob(&ndr_blob, temp_ctx,
380 &pfm_blob,
381 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
382 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
383 talloc_free(temp_ctx);
384 return WERR_FOOBAR;
387 /* write serialized prefixMap into LDB */
388 msg = ldb_msg_new(temp_ctx);
389 if (!msg) {
390 talloc_free(temp_ctx);
391 return WERR_NOT_ENOUGH_MEMORY;
394 msg->dn = schema_dn;
395 ldb_ret = ldb_msg_add_value(msg, "prefixMap", &ndr_blob, NULL);
396 if (ldb_ret != 0) {
397 talloc_free(temp_ctx);
398 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));
399 return WERR_NOT_ENOUGH_MEMORY;
402 ldb_ret = dsdb_replace(ldb, msg, DSDB_FLAG_AS_SYSTEM);
404 talloc_free(temp_ctx);
406 if (ldb_ret != 0) {
407 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: dsdb_replace failed\n"));
408 return WERR_FOOBAR;
411 return WERR_OK;
414 WERROR dsdb_read_prefixes_from_ldb(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct dsdb_schema_prefixmap **_pfm)
416 WERROR werr;
417 int ldb_ret;
418 const struct ldb_val *prefix_val;
419 struct ldb_dn *schema_dn;
420 struct ldb_result *schema_res = NULL;
421 static const char *schema_attrs[] = {
422 "prefixMap",
423 NULL
426 schema_dn = ldb_get_schema_basedn(ldb);
427 if (!schema_dn) {
428 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
429 return WERR_FOOBAR;
432 ldb_ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
433 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
434 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
435 talloc_free(schema_res);
436 return WERR_FOOBAR;
437 } else if (ldb_ret != LDB_SUCCESS) {
438 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
439 talloc_free(schema_res);
440 return WERR_FOOBAR;
443 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
444 if (!prefix_val) {
445 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
446 talloc_free(schema_res);
447 return WERR_FOOBAR;
450 werr = _dsdb_prefixmap_from_ldb_val(prefix_val,
451 mem_ctx,
452 _pfm);
453 talloc_free(schema_res);
454 W_ERROR_NOT_OK_RETURN(werr);
456 return WERR_OK;
460 this will be replaced with something that looks at the right part of
461 the schema once we know where unique indexing information is hidden
463 static bool dsdb_schema_unique_attribute(const char *attr)
465 const char *attrs[] = { "objectGUID", NULL };
466 unsigned int i;
467 for (i=0;attrs[i];i++) {
468 if (ldb_attr_cmp(attr, attrs[i]) == 0) {
469 return true;
472 return false;
477 setup the ldb_schema_attribute field for a dsdb_attribute
479 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb,
480 struct dsdb_attribute *attr)
482 const char *syntax = attr->syntax->ldb_syntax;
483 const struct ldb_schema_syntax *s;
484 struct ldb_schema_attribute *a;
486 if (!syntax) {
487 syntax = attr->syntax->ldap_oid;
490 s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
491 if (s == NULL) {
492 s = ldb_samba_syntax_by_name(ldb, syntax);
494 if (s == NULL) {
495 s = ldb_standard_syntax_by_name(ldb, syntax);
498 if (s == NULL) {
499 return ldb_operr(ldb);
502 attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
503 if (attr->ldb_schema_attribute == NULL) {
504 return ldb_oom(ldb);
507 a->name = attr->lDAPDisplayName;
508 a->flags = 0;
509 a->syntax = s;
511 if (dsdb_schema_unique_attribute(a->name)) {
512 a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
514 if (attr->isSingleValued) {
515 a->flags |= LDB_ATTR_FLAG_SINGLE_VALUE;
519 * Is the attribute indexed? By treating confidential attributes as
520 * unindexed, we force searches to go through the unindexed search path,
521 * avoiding observable timing differences.
523 if (attr->searchFlags & SEARCH_FLAG_ATTINDEX &&
524 !(attr->searchFlags & SEARCH_FLAG_CONFIDENTIAL))
526 a->flags |= LDB_ATTR_FLAG_INDEXED;
530 return LDB_SUCCESS;
534 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
535 const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
536 if (get_string_val == NULL) { \
537 if (strict) { \
538 d_printf("%s: %s == NULL in %s\n", __location__, attr, ldb_dn_get_linearized(msg->dn)); \
539 return WERR_INVALID_PARAMETER; \
540 } else { \
541 (p)->elem = NULL; \
543 } else { \
544 (p)->elem = talloc_strndup(mem_ctx, \
545 (const char *)get_string_val->data, \
546 get_string_val->length); \
547 if (!(p)->elem) { \
548 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
549 return WERR_NOT_ENOUGH_MEMORY; \
552 } while (0)
554 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem) do { \
555 int get_string_list_counter; \
556 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
557 /* We may get empty attributes over the replication channel */ \
558 if (get_string_list_el == NULL || get_string_list_el->num_values == 0) { \
559 (p)->elem = NULL; \
560 break; \
562 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
563 for (get_string_list_counter=0; \
564 get_string_list_counter < get_string_list_el->num_values; \
565 get_string_list_counter++) { \
566 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
567 (const char *)get_string_list_el->values[get_string_list_counter].data, \
568 get_string_list_el->values[get_string_list_counter].length); \
569 if (!(p)->elem[get_string_list_counter]) { \
570 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
571 return WERR_NOT_ENOUGH_MEMORY; \
573 (p)->elem[get_string_list_counter+1] = NULL; \
575 talloc_steal(mem_ctx, (p)->elem); \
576 } while (0)
578 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
579 const char *str; \
580 str = ldb_msg_find_attr_as_string(msg, attr, NULL);\
581 if (str == NULL) { \
582 if (strict) { \
583 d_printf("%s: %s == NULL\n", __location__, attr); \
584 return WERR_INVALID_PARAMETER; \
585 } else { \
586 (p)->elem = false; \
588 } else if (strcasecmp("TRUE", str) == 0) { \
589 (p)->elem = true; \
590 } else if (strcasecmp("FALSE", str) == 0) { \
591 (p)->elem = false; \
592 } else { \
593 d_printf("%s: %s == %s\n", __location__, attr, str); \
594 return WERR_INVALID_PARAMETER; \
596 } while (0)
598 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
599 (p)->elem = ldb_msg_find_attr_as_uint(msg, attr, 0);\
600 } while (0)
602 #define GET_UINT32_PTR_LDB(msg, attr, mem_ctx, p, elem) do { \
603 uint64_t _v = ldb_msg_find_attr_as_uint64(msg, attr, UINT64_MAX);\
604 if (_v == UINT64_MAX) { \
605 (p)->elem = NULL; \
606 } else if (_v > UINT32_MAX) { \
607 d_printf("%s: %s == 0x%llX\n", __location__, \
608 attr, (unsigned long long)_v); \
609 return WERR_INVALID_PARAMETER; \
610 } else { \
611 (p)->elem = talloc(mem_ctx, uint32_t); \
612 if (!(p)->elem) { \
613 d_printf("%s: talloc failed for %s\n", __location__, attr); \
614 return WERR_NOT_ENOUGH_MEMORY; \
616 *(p)->elem = (uint32_t)_v; \
618 } while (0)
620 #define GET_GUID_LDB(msg, attr, p, elem) do { \
621 (p)->elem = samdb_result_guid(msg, attr);\
622 } while (0)
624 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
625 const struct ldb_val *_val;\
626 _val = ldb_msg_find_ldb_val(msg, attr);\
627 if (_val) {\
628 (p)->elem = *_val;\
629 talloc_steal(mem_ctx, (p)->elem.data);\
630 } else {\
631 ZERO_STRUCT((p)->elem);\
633 } while (0)
635 /** Create an dsdb_attribute out of ldb message, attr must be already talloced
637 * If supplied the attribute will be checked against the prefixmap to
638 * ensure it can be mapped. However we can't have this attribute
639 * const as dsdb_schema_pfm_attid_from_oid calls
640 * dsdb_schema_pfm_make_attid_impl() which may modify prefixmap in
641 * other situations.
644 WERROR dsdb_attribute_from_ldb(struct dsdb_schema_prefixmap *prefixmap,
645 struct ldb_message *msg,
646 struct dsdb_attribute *attr)
648 WERROR status;
649 if (attr == NULL) {
650 DEBUG(0, ("%s: attr is null, it's expected not to be so\n", __location__));
651 return WERR_INVALID_PARAMETER;
654 GET_STRING_LDB(msg, "cn", attr, attr, cn, false);
657 * This allows for the fact that the CN attribute is not
658 * replicated over DRS, it is only replicated under the alias
659 * 'name'.
661 if (attr->cn == NULL) {
662 GET_STRING_LDB(msg, "name", attr, attr, cn, true);
665 GET_STRING_LDB(msg, "lDAPDisplayName", attr, attr, lDAPDisplayName, true);
666 GET_STRING_LDB(msg, "attributeID", attr, attr, attributeID_oid, true);
667 if (!prefixmap || prefixmap->length == 0) {
668 /* set an invalid value */
669 attr->attributeID_id = DRSUAPI_ATTID_INVALID;
670 } else {
671 status = dsdb_schema_pfm_attid_from_oid(prefixmap,
672 attr->attributeID_oid,
673 &attr->attributeID_id);
674 if (!W_ERROR_IS_OK(status)) {
675 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
676 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
677 win_errstr(status)));
678 return status;
681 /* fetch msDS-IntId to be used in resolving ATTRTYP values */
682 GET_UINT32_LDB(msg, "msDS-IntId", attr, msDS_IntId);
684 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
685 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
687 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
689 GET_GUID_LDB(msg, "objectGUID", attr, objectGUID);
691 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
692 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
693 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
694 GET_UINT32_LDB(msg, "linkID", attr, linkID);
696 GET_STRING_LDB(msg, "attributeSyntax", attr, attr, attributeSyntax_oid, true);
697 if (!prefixmap || prefixmap->length == 0) {
698 /* set an invalid value */
699 attr->attributeSyntax_id = DRSUAPI_ATTID_INVALID;
700 } else {
701 status = dsdb_schema_pfm_attid_from_oid(prefixmap,
702 attr->attributeSyntax_oid,
703 &attr->attributeSyntax_id);
704 if (!W_ERROR_IS_OK(status)) {
705 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
706 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
707 win_errstr(status)));
708 return status;
711 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
712 GET_BLOB_LDB(msg, "oMObjectClass", attr, attr, oMObjectClass);
714 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
715 GET_UINT32_PTR_LDB(msg, "rangeLower", attr, attr, rangeLower);
716 GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, attr, rangeUpper);
717 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
719 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
720 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", attr, attr, msDs_Schema_Extensions);
722 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
723 GET_STRING_LDB(msg, "adminDisplayName", attr, attr, adminDisplayName, false);
724 GET_STRING_LDB(msg, "adminDescription", attr, attr, adminDescription, false);
725 GET_STRING_LDB(msg, "classDisplayName", attr, attr, classDisplayName, false);
726 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
727 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
728 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
730 return WERR_OK;
733 WERROR dsdb_set_attribute_from_ldb_dups(struct ldb_context *ldb,
734 struct dsdb_schema *schema,
735 struct ldb_message *msg,
736 bool checkdups)
738 WERROR status;
739 struct dsdb_attribute *attr = talloc_zero(schema, struct dsdb_attribute);
740 if (!attr) {
741 return WERR_NOT_ENOUGH_MEMORY;
744 status = dsdb_attribute_from_ldb(schema->prefixmap, msg, attr);
745 if (!W_ERROR_IS_OK(status)) {
746 return status;
749 attr->syntax = dsdb_syntax_for_attribute(attr);
750 if (!attr->syntax) {
751 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
752 attr->lDAPDisplayName));
753 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
756 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
757 DEBUG(0,(__location__ ": Unknown schema syntax for %s - ldb_syntax: %s, ldap_oid: %s\n",
758 attr->lDAPDisplayName,
759 attr->syntax->ldb_syntax,
760 attr->syntax->ldap_oid));
761 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
764 if (checkdups) {
765 const struct dsdb_attribute *a2;
766 struct dsdb_attribute **a;
767 uint32_t i;
769 a2 = dsdb_attribute_by_attributeID_id(schema,
770 attr->attributeID_id);
771 if (a2 == NULL) {
772 goto done;
775 i = schema->attributes_to_remove_size;
776 a = talloc_realloc(schema, schema->attributes_to_remove,
777 struct dsdb_attribute *, i + 1);
778 if (a == NULL) {
779 return WERR_NOT_ENOUGH_MEMORY;
781 /* Mark the old attribute as to be removed */
782 a[i] = discard_const_p(struct dsdb_attribute, a2);
783 schema->attributes_to_remove = a;
784 schema->attributes_to_remove_size++;
787 done:
788 DLIST_ADD(schema->attributes, attr);
789 return WERR_OK;
792 WERROR dsdb_set_attribute_from_ldb(struct ldb_context *ldb,
793 struct dsdb_schema *schema,
794 struct ldb_message *msg)
796 return dsdb_set_attribute_from_ldb_dups(ldb, schema, msg, false);
799 WERROR dsdb_set_class_from_ldb_dups(struct dsdb_schema *schema,
800 struct ldb_message *msg,
801 bool checkdups)
803 WERROR status;
804 struct dsdb_class *obj = talloc_zero(schema, struct dsdb_class);
805 if (!obj) {
806 return WERR_NOT_ENOUGH_MEMORY;
808 GET_STRING_LDB(msg, "cn", obj, obj, cn, false);
811 * This allows for the fact that the CN attribute is not
812 * replicated over DRS, it is only replicated under the alias
813 * 'name'.
815 if (obj->cn == NULL) {
816 GET_STRING_LDB(msg, "name", obj, obj, cn, true);
819 GET_STRING_LDB(msg, "lDAPDisplayName", obj, obj, lDAPDisplayName, true);
820 GET_STRING_LDB(msg, "governsID", obj, obj, governsID_oid, true);
821 if (!schema->prefixmap || schema->prefixmap->length == 0) {
822 /* set an invalid value */
823 obj->governsID_id = DRSUAPI_ATTID_INVALID;
824 } else {
825 status = dsdb_schema_pfm_attid_from_oid(schema->prefixmap,
826 obj->governsID_oid,
827 &obj->governsID_id);
828 if (!W_ERROR_IS_OK(status)) {
829 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
830 __location__, obj->lDAPDisplayName, obj->governsID_oid,
831 win_errstr(status)));
832 return status;
835 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
836 GET_GUID_LDB(msg, "objectGUID", obj, objectGUID);
838 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
839 GET_STRING_LDB(msg, "rDNAttID", obj, obj, rDNAttID, false);
840 GET_STRING_LDB(msg, "defaultObjectCategory", obj, obj, defaultObjectCategory, true);
842 GET_STRING_LDB(msg, "subClassOf", obj, obj, subClassOf, true);
844 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", obj, obj, systemAuxiliaryClass);
845 GET_STRING_LIST_LDB(msg, "auxiliaryClass", obj, obj, auxiliaryClass);
847 GET_STRING_LIST_LDB(msg, "systemMustContain", obj, obj, systemMustContain);
848 GET_STRING_LIST_LDB(msg, "systemMayContain", obj, obj, systemMayContain);
849 GET_STRING_LIST_LDB(msg, "mustContain", obj, obj, mustContain);
850 GET_STRING_LIST_LDB(msg, "mayContain", obj, obj, mayContain);
852 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", obj, obj, systemPossSuperiors);
853 GET_STRING_LIST_LDB(msg, "possSuperiors", obj, obj, possSuperiors);
855 GET_STRING_LDB(msg, "defaultSecurityDescriptor", obj, obj, defaultSecurityDescriptor, false);
857 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
858 GET_UINT32_LDB(msg, "systemFlags", obj, systemFlags);
859 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", obj, obj, msDs_Schema_Extensions);
861 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
862 GET_STRING_LDB(msg, "adminDisplayName", obj, obj, adminDisplayName, false);
863 GET_STRING_LDB(msg, "adminDescription", obj, obj, adminDescription, false);
864 GET_STRING_LDB(msg, "classDisplayName", obj, obj, classDisplayName, false);
865 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
866 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
867 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
869 if (checkdups) {
870 const struct dsdb_class *c2;
871 struct dsdb_class **c;
872 uint32_t i;
874 c2 = dsdb_class_by_governsID_id(schema, obj->governsID_id);
875 if (c2 == NULL) {
876 goto done;
879 i = schema->classes_to_remove_size;
880 c = talloc_realloc(schema, schema->classes_to_remove,
881 struct dsdb_class *, i + 1);
882 if (c == NULL) {
883 return WERR_NOT_ENOUGH_MEMORY;
885 /* Mark the old class to be removed */
886 c[i] = discard_const_p(struct dsdb_class, c2);
887 schema->classes_to_remove = c;
888 schema->classes_to_remove_size++;
891 done:
892 DLIST_ADD(schema->classes, obj);
893 return WERR_OK;
896 WERROR dsdb_set_class_from_ldb(struct dsdb_schema *schema,
897 struct ldb_message *msg)
899 return dsdb_set_class_from_ldb_dups(schema, msg, false);
902 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
905 Fill a DSDB schema from the ldb results provided. This is called
906 directly when a schema must be created with a pre-initialised prefixMap
909 int dsdb_load_ldb_results_into_schema(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
910 struct dsdb_schema *schema,
911 struct ldb_result *attrs_class_res,
912 char **error_string)
914 unsigned int i;
915 WERROR status;
917 schema->ts_last_change = 0;
918 for (i=0; i < attrs_class_res->count; i++) {
919 const char *prefixMap = NULL;
921 * attrs_class_res also includes the schema object;
922 * we only want to process classes & attributes
924 prefixMap = ldb_msg_find_attr_as_string(
925 attrs_class_res->msgs[i],
926 "prefixMap", NULL);
927 if (prefixMap != NULL) {
928 continue;
931 status = dsdb_schema_set_el_from_ldb_msg(ldb, schema,
932 attrs_class_res->msgs[i]);
933 if (!W_ERROR_IS_OK(status)) {
934 *error_string = talloc_asprintf(mem_ctx,
935 "dsdb_load_ldb_results_into_schema: failed to load attribute or class definition: %s:%s",
936 ldb_dn_get_linearized(attrs_class_res->msgs[i]->dn),
937 win_errstr(status));
938 DEBUG(0,(__location__ ": %s\n", *error_string));
939 return LDB_ERR_CONSTRAINT_VIOLATION;
943 return LDB_SUCCESS;
947 Create a DSDB schema from the ldb results provided. This is called
948 directly when the schema is provisioned from an on-disk LDIF file, or
949 from dsdb_schema_from_schema_dn in schema_fsmo
952 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
953 struct ldb_message *schema_msg,
954 struct ldb_result *attrs_class_res,
955 struct dsdb_schema **schema_out,
956 char **error_string)
958 WERROR status;
959 const struct ldb_val *prefix_val;
960 const struct ldb_val *info_val;
961 struct ldb_val info_val_default;
962 struct dsdb_schema *schema;
963 void *lp_opaque = ldb_get_opaque(ldb, "loadparm");
964 int ret;
966 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
967 if (!tmp_ctx) {
968 dsdb_oom(error_string, mem_ctx);
969 return ldb_operr(ldb);
972 schema = dsdb_new_schema(tmp_ctx);
973 if (!schema) {
974 dsdb_oom(error_string, mem_ctx);
975 talloc_free(tmp_ctx);
976 return ldb_operr(ldb);
979 if (lp_opaque) {
980 struct loadparm_context *lp_ctx = talloc_get_type_abort(lp_opaque, struct loadparm_context);
981 schema->fsmo.update_allowed = lpcfg_parm_bool(lp_ctx, NULL,
982 "dsdb", "schema update allowed",
983 false);
986 prefix_val = ldb_msg_find_ldb_val(schema_msg, "prefixMap");
987 if (!prefix_val) {
988 *error_string = talloc_asprintf(mem_ctx,
989 "schema_fsmo_init: no prefixMap attribute found");
990 DEBUG(0,(__location__ ": %s\n", *error_string));
991 talloc_free(tmp_ctx);
992 return LDB_ERR_CONSTRAINT_VIOLATION;
994 info_val = ldb_msg_find_ldb_val(schema_msg, "schemaInfo");
995 if (!info_val) {
996 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
997 if (!W_ERROR_IS_OK(status)) {
998 *error_string = talloc_asprintf(mem_ctx,
999 "schema_fsmo_init: dsdb_schema_info_blob_new() failed - %s",
1000 win_errstr(status));
1001 DEBUG(0,(__location__ ": %s\n", *error_string));
1002 talloc_free(tmp_ctx);
1003 return ldb_operr(ldb);
1005 info_val = &info_val_default;
1008 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
1009 if (!W_ERROR_IS_OK(status)) {
1010 *error_string = talloc_asprintf(mem_ctx,
1011 "schema_fsmo_init: failed to load oid mappings: %s",
1012 win_errstr(status));
1013 DEBUG(0,(__location__ ": %s\n", *error_string));
1014 talloc_free(tmp_ctx);
1015 return LDB_ERR_CONSTRAINT_VIOLATION;
1018 ret = dsdb_load_ldb_results_into_schema(mem_ctx, ldb, schema, attrs_class_res, error_string);
1019 if (ret != LDB_SUCCESS) {
1020 talloc_free(tmp_ctx);
1021 return ret;
1024 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_msg, "fSMORoleOwner");
1025 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb, tmp_ctx), schema->fsmo.master_dn) == 0) {
1026 schema->fsmo.we_are_master = true;
1027 } else {
1028 schema->fsmo.we_are_master = false;
1031 DEBUG(5, ("schema_fsmo_init: we are master[%s] updates allowed[%s]\n",
1032 (schema->fsmo.we_are_master?"yes":"no"),
1033 (schema->fsmo.update_allowed?"yes":"no")));
1035 *schema_out = talloc_steal(mem_ctx, schema);
1036 talloc_free(tmp_ctx);
1037 return LDB_SUCCESS;