tevent: expose tevent_context_init_ops
[Samba/gebeck_regimport.git] / source4 / dsdb / schema / schema_init.c
blob2db708d2df5e74011bc4dece315943fab55c24f4
1 /*
2 Unix SMB/CIFS mplementation.
3 DSDB schema header
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2006-2007
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "dsdb/common/util.h"
26 #include <ldb_errors.h>
27 #include "../lib/util/dlinklist.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "param/param.h"
32 #include <ldb_module.h>
33 #include "../lib/util/asn1.h"
36 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx)
38 struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
39 if (!schema) {
40 return NULL;
43 return schema;
46 struct dsdb_schema *dsdb_schema_copy_shallow(TALLOC_CTX *mem_ctx,
47 struct ldb_context *ldb,
48 const struct dsdb_schema *schema)
50 int ret;
51 struct dsdb_class *cls;
52 struct dsdb_attribute *attr;
53 struct dsdb_schema *schema_copy;
55 schema_copy = dsdb_new_schema(mem_ctx);
56 if (!schema_copy) {
57 return NULL;
60 /* schema base_dn */
61 schema_copy->base_dn = ldb_dn_copy(schema_copy, schema->base_dn);
62 if (!schema_copy->base_dn) {
63 goto failed;
66 /* copy prexiMap & schemaInfo */
67 schema_copy->prefixmap = dsdb_schema_pfm_copy_shallow(schema_copy,
68 schema->prefixmap);
69 if (!schema_copy->prefixmap) {
70 goto failed;
73 schema_copy->schema_info = talloc_strdup(schema_copy, schema->schema_info);
75 /* copy classes and attributes*/
76 for (cls = schema->classes; cls; cls = cls->next) {
77 struct dsdb_class *class_copy = talloc_memdup(schema_copy,
78 cls, sizeof(*cls));
79 if (!class_copy) {
80 goto failed;
82 DLIST_ADD(schema_copy->classes, class_copy);
84 schema_copy->num_classes = schema->num_classes;
86 for (attr = schema->attributes; attr; attr = attr->next) {
87 struct dsdb_attribute *a_copy = talloc_memdup(schema_copy,
88 attr, sizeof(*attr));
89 if (!a_copy) {
90 goto failed;
92 DLIST_ADD(schema_copy->attributes, a_copy);
94 schema_copy->num_attributes = schema->num_attributes;
96 /* rebuild indexes */
97 ret = dsdb_setup_sorted_accessors(ldb, schema_copy);
98 if (ret != LDB_SUCCESS) {
99 goto failed;
102 /* leave reload_seq_number = 0 so it will be refresh ASAP */
103 schema_copy->refresh_fn = schema->refresh_fn;
104 schema_copy->loaded_from_module = schema->loaded_from_module;
106 return schema_copy;
108 failed:
109 talloc_free(schema_copy);
110 return NULL;
114 WERROR dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema *schema,
115 const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
117 WERROR werr;
118 const char *schema_info;
119 struct dsdb_schema_prefixmap *pfm;
121 werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, schema, &pfm, &schema_info);
122 W_ERROR_NOT_OK_RETURN(werr);
124 /* set loaded prefixMap */
125 talloc_free(schema->prefixmap);
126 schema->prefixmap = pfm;
128 talloc_free(discard_const(schema->schema_info));
129 schema->schema_info = schema_info;
131 return WERR_OK;
134 static WERROR _dsdb_prefixmap_from_ldb_val(const struct ldb_val *pfm_ldb_val,
135 TALLOC_CTX *mem_ctx,
136 struct dsdb_schema_prefixmap **_pfm)
138 WERROR werr;
139 enum ndr_err_code ndr_err;
140 struct prefixMapBlob pfm_blob;
142 TALLOC_CTX *temp_ctx = talloc_new(mem_ctx);
143 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
145 ndr_err = ndr_pull_struct_blob(pfm_ldb_val, temp_ctx,
146 &pfm_blob,
147 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
148 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
149 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
150 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: Failed to parse prefixmap of length %u: %s\n",
151 (unsigned int)pfm_ldb_val->length, ndr_map_error2string(ndr_err)));
152 talloc_free(temp_ctx);
153 return ntstatus_to_werror(nt_status);
156 if (pfm_blob.version != PREFIX_MAP_VERSION_DSDB) {
157 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version %u incorrect\n", (unsigned int)pfm_blob.version));
158 talloc_free(temp_ctx);
159 return WERR_VERSION_PARSE_ERROR;
162 /* call the drsuapi version */
163 werr = dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob.ctr.dsdb, false, mem_ctx, _pfm, NULL);
164 if (!W_ERROR_IS_OK(werr)) {
165 DEBUG(0, (__location__ " dsdb_schema_pfm_from_drsuapi_pfm failed: %s\n", win_errstr(werr)));
166 talloc_free(temp_ctx);
167 return werr;
170 talloc_free(temp_ctx);
172 return werr;
175 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
176 const struct ldb_val *prefixMap,
177 const struct ldb_val *schemaInfo)
179 WERROR werr;
180 const char *schema_info;
181 struct dsdb_schema_prefixmap *pfm;
182 TALLOC_CTX *mem_ctx;
184 /* verify schemaInfo blob is valid one */
185 if (!dsdb_schema_info_blob_is_valid(schemaInfo)) {
186 DEBUG(0,(__location__": dsdb_schema_info_blob_is_valid() failed.\n"));
187 return WERR_INVALID_PARAMETER;
190 mem_ctx = talloc_new(schema);
191 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
193 /* fetch prefixMap */
194 werr = _dsdb_prefixmap_from_ldb_val(prefixMap,
195 mem_ctx, &pfm);
196 if (!W_ERROR_IS_OK(werr)) {
197 DEBUG(0, (__location__ " _dsdb_prefixmap_from_ldb_val failed: %s\n", win_errstr(werr)));
198 talloc_free(mem_ctx);
199 return werr;
202 /* decode schema_info */
203 schema_info = hex_encode_talloc(mem_ctx,
204 schemaInfo->data,
205 schemaInfo->length);
206 if (!schema_info) {
207 talloc_free(mem_ctx);
208 return WERR_NOMEM;
211 /* store prefixMap and schema_info into cached Schema */
212 talloc_free(schema->prefixmap);
213 schema->prefixmap = talloc_steal(schema, pfm);
215 talloc_free(discard_const(schema->schema_info));
216 schema->schema_info = talloc_steal(schema, schema_info);
218 /* clean up locally allocated mem */
219 talloc_free(mem_ctx);
221 return WERR_OK;
224 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
225 bool include_schema_info,
226 TALLOC_CTX *mem_ctx,
227 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
229 return dsdb_drsuapi_pfm_from_schema_pfm(schema->prefixmap,
230 include_schema_info ? schema->schema_info : NULL,
231 mem_ctx, _ctr);
234 WERROR dsdb_get_drsuapi_prefixmap_as_blob(const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr,
235 TALLOC_CTX *mem_ctx,
236 struct ldb_val *prefixMap)
238 struct prefixMapBlob pfm;
239 enum ndr_err_code ndr_err;
240 pfm.version = PREFIX_MAP_VERSION_DSDB;
241 pfm.reserved = 0;
242 pfm.ctr.dsdb = *ctr;
244 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, &pfm,
245 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
246 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
247 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
248 return ntstatus_to_werror(nt_status);
250 return WERR_OK;
253 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
254 TALLOC_CTX *mem_ctx,
255 struct ldb_val *prefixMap,
256 struct ldb_val *schemaInfo)
258 WERROR status;
259 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
261 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
262 W_ERROR_NOT_OK_RETURN(status);
264 status = dsdb_get_drsuapi_prefixmap_as_blob(ctr, mem_ctx, prefixMap);
265 talloc_free(ctr);
266 W_ERROR_NOT_OK_RETURN(status);
268 *schemaInfo = strhex_to_data_blob(mem_ctx, schema->schema_info);
269 W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
271 return WERR_OK;
276 * this function is called from within a ldb transaction from the schema_fsmo module
278 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
280 WERROR status;
281 uint32_t attid;
282 TALLOC_CTX *mem_ctx;
283 struct dsdb_schema_prefixmap *pfm;
285 mem_ctx = talloc_new(ldb);
286 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
288 /* Read prefixes from disk*/
289 status = dsdb_read_prefixes_from_ldb(ldb, mem_ctx, &pfm);
290 if (!W_ERROR_IS_OK(status)) {
291 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
292 win_errstr(status)));
293 talloc_free(mem_ctx);
294 return status;
297 /* Check if there is a prefix for the oid in the prefixes array*/
298 status = dsdb_schema_pfm_find_oid(pfm, full_oid, NULL);
299 if (W_ERROR_IS_OK(status)) {
300 /* prefix found*/
301 talloc_free(mem_ctx);
302 return status;
303 } else if (!W_ERROR_EQUAL(status, WERR_NOT_FOUND)) {
304 /* error */
305 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
306 win_errstr(status)));
307 talloc_free(mem_ctx);
308 return status;
311 /* Create the new mapping for the prefix of full_oid */
312 status = dsdb_schema_pfm_make_attid(pfm, full_oid, &attid);
313 if (!W_ERROR_IS_OK(status)) {
314 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_schema_pfm_make_attid: %s\n",
315 win_errstr(status)));
316 talloc_free(mem_ctx);
317 return status;
320 talloc_unlink(schema, schema->prefixmap);
321 schema->prefixmap = talloc_steal(schema, 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));
335 talloc_free(mem_ctx);
336 return status;
340 WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
341 const struct dsdb_schema *schema)
343 WERROR status;
344 int ldb_ret;
345 struct ldb_message *msg;
346 struct ldb_dn *schema_dn;
347 struct prefixMapBlob pfm_blob;
348 struct ldb_val ndr_blob;
349 enum ndr_err_code ndr_err;
350 TALLOC_CTX *temp_ctx;
351 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
353 schema_dn = ldb_get_schema_basedn(ldb);
354 if (!schema_dn) {
355 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));
356 return WERR_FOOBAR;
359 temp_ctx = talloc_new(mem_ctx);
360 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
362 /* convert schema_prefixMap to prefixMap blob */
363 status = dsdb_get_oid_mappings_drsuapi(schema, false, temp_ctx, &ctr);
364 if (!W_ERROR_IS_OK(status)) {
365 talloc_free(temp_ctx);
366 return status;
369 pfm_blob.version = PREFIX_MAP_VERSION_DSDB;
370 pfm_blob.ctr.dsdb = *ctr;
372 ndr_err = ndr_push_struct_blob(&ndr_blob, temp_ctx,
373 &pfm_blob,
374 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
375 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
376 talloc_free(temp_ctx);
377 return WERR_FOOBAR;
380 /* write serialized prefixMap into LDB */
381 msg = ldb_msg_new(temp_ctx);
382 if (!msg) {
383 talloc_free(temp_ctx);
384 return WERR_NOMEM;
387 msg->dn = schema_dn;
388 ldb_ret = ldb_msg_add_value(msg, "prefixMap", &ndr_blob, NULL);
389 if (ldb_ret != 0) {
390 talloc_free(temp_ctx);
391 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));
392 return WERR_NOMEM;
395 ldb_ret = dsdb_replace(ldb, msg, DSDB_FLAG_AS_SYSTEM);
397 talloc_free(temp_ctx);
399 if (ldb_ret != 0) {
400 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: dsdb_replace failed\n"));
401 return WERR_FOOBAR;
404 return WERR_OK;
407 WERROR dsdb_read_prefixes_from_ldb(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct dsdb_schema_prefixmap **_pfm)
409 WERROR werr;
410 int ldb_ret;
411 const struct ldb_val *prefix_val;
412 struct ldb_dn *schema_dn;
413 struct ldb_result *schema_res = NULL;
414 static const char *schema_attrs[] = {
415 "prefixMap",
416 NULL
419 schema_dn = ldb_get_schema_basedn(ldb);
420 if (!schema_dn) {
421 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
422 return WERR_FOOBAR;
425 ldb_ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
426 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
427 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
428 talloc_free(schema_res);
429 return WERR_FOOBAR;
430 } else if (ldb_ret != LDB_SUCCESS) {
431 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
432 talloc_free(schema_res);
433 return WERR_FOOBAR;
436 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
437 if (!prefix_val) {
438 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
439 talloc_free(schema_res);
440 return WERR_FOOBAR;
443 werr = _dsdb_prefixmap_from_ldb_val(prefix_val,
444 mem_ctx,
445 _pfm);
446 talloc_free(schema_res);
447 W_ERROR_NOT_OK_RETURN(werr);
449 return WERR_OK;
453 this will be replaced with something that looks at the right part of
454 the schema once we know where unique indexing information is hidden
456 static bool dsdb_schema_unique_attribute(const char *attr)
458 const char *attrs[] = { "objectGUID", "objectSid" , NULL };
459 unsigned int i;
460 for (i=0;attrs[i];i++) {
461 if (ldb_attr_cmp(attr, attrs[i]) == 0) {
462 return true;
465 return false;
470 setup the ldb_schema_attribute field for a dsdb_attribute
472 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb,
473 struct dsdb_attribute *attr)
475 const char *syntax = attr->syntax->ldb_syntax;
476 const struct ldb_schema_syntax *s;
477 struct ldb_schema_attribute *a;
479 if (!syntax) {
480 syntax = attr->syntax->ldap_oid;
483 s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
484 if (s == NULL) {
485 s = ldb_samba_syntax_by_name(ldb, syntax);
487 if (s == NULL) {
488 s = ldb_standard_syntax_by_name(ldb, syntax);
491 if (s == NULL) {
492 return ldb_operr(ldb);
495 attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
496 if (attr->ldb_schema_attribute == NULL) {
497 return ldb_oom(ldb);
500 a->name = attr->lDAPDisplayName;
501 a->flags = 0;
502 a->syntax = s;
504 if (dsdb_schema_unique_attribute(a->name)) {
505 a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
507 if (attr->isSingleValued) {
508 a->flags |= LDB_ATTR_FLAG_SINGLE_VALUE;
512 return LDB_SUCCESS;
516 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
517 const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
518 if (get_string_val == NULL) { \
519 if (strict) { \
520 d_printf("%s: %s == NULL in %s\n", __location__, attr, ldb_dn_get_linearized(msg->dn)); \
521 return WERR_INVALID_PARAM; \
522 } else { \
523 (p)->elem = NULL; \
525 } else { \
526 (p)->elem = talloc_strndup(mem_ctx, \
527 (const char *)get_string_val->data, \
528 get_string_val->length); \
529 if (!(p)->elem) { \
530 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
531 return WERR_NOMEM; \
534 } while (0)
536 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem) do { \
537 int get_string_list_counter; \
538 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
539 /* We may get empty attributes over the replication channel */ \
540 if (get_string_list_el == NULL || get_string_list_el->num_values == 0) { \
541 (p)->elem = NULL; \
542 break; \
544 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
545 for (get_string_list_counter=0; \
546 get_string_list_counter < get_string_list_el->num_values; \
547 get_string_list_counter++) { \
548 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
549 (const char *)get_string_list_el->values[get_string_list_counter].data, \
550 get_string_list_el->values[get_string_list_counter].length); \
551 if (!(p)->elem[get_string_list_counter]) { \
552 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
553 return WERR_NOMEM; \
555 (p)->elem[get_string_list_counter+1] = NULL; \
557 talloc_steal(mem_ctx, (p)->elem); \
558 } while (0)
560 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
561 const char *str; \
562 str = ldb_msg_find_attr_as_string(msg, attr, NULL);\
563 if (str == NULL) { \
564 if (strict) { \
565 d_printf("%s: %s == NULL\n", __location__, attr); \
566 return WERR_INVALID_PARAM; \
567 } else { \
568 (p)->elem = false; \
570 } else if (strcasecmp("TRUE", str) == 0) { \
571 (p)->elem = true; \
572 } else if (strcasecmp("FALSE", str) == 0) { \
573 (p)->elem = false; \
574 } else { \
575 d_printf("%s: %s == %s\n", __location__, attr, str); \
576 return WERR_INVALID_PARAM; \
578 } while (0)
580 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
581 (p)->elem = ldb_msg_find_attr_as_uint(msg, attr, 0);\
582 } while (0)
584 #define GET_UINT32_PTR_LDB(msg, attr, mem_ctx, p, elem) do { \
585 uint64_t _v = ldb_msg_find_attr_as_uint64(msg, attr, UINT64_MAX);\
586 if (_v == UINT64_MAX) { \
587 (p)->elem = NULL; \
588 } else if (_v > UINT32_MAX) { \
589 d_printf("%s: %s == 0x%llX\n", __location__, \
590 attr, (unsigned long long)_v); \
591 return WERR_INVALID_PARAM; \
592 } else { \
593 (p)->elem = talloc(mem_ctx, uint32_t); \
594 if (!(p)->elem) { \
595 d_printf("%s: talloc failed for %s\n", __location__, attr); \
596 return WERR_NOMEM; \
598 *(p)->elem = (uint32_t)_v; \
600 } while (0)
602 #define GET_GUID_LDB(msg, attr, p, elem) do { \
603 (p)->elem = samdb_result_guid(msg, attr);\
604 } while (0)
606 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
607 const struct ldb_val *_val;\
608 _val = ldb_msg_find_ldb_val(msg, attr);\
609 if (_val) {\
610 (p)->elem = *_val;\
611 talloc_steal(mem_ctx, (p)->elem.data);\
612 } else {\
613 ZERO_STRUCT((p)->elem);\
615 } while (0)
617 /** Create an dsdb_attribute out of ldb message, attr must be already talloced
620 WERROR dsdb_attribute_from_ldb(const struct dsdb_schema *schema,
621 struct ldb_message *msg,
622 struct dsdb_attribute *attr)
624 WERROR status;
625 if (attr == NULL) {
626 DEBUG(0, ("%s: attr is null, it's expected not to be so\n", __location__));
627 return WERR_INVALID_PARAM;
630 GET_STRING_LDB(msg, "cn", attr, attr, cn, false);
631 GET_STRING_LDB(msg, "lDAPDisplayName", attr, attr, lDAPDisplayName, true);
632 GET_STRING_LDB(msg, "attributeID", attr, attr, attributeID_oid, true);
633 if (!schema->prefixmap || schema->prefixmap->length == 0) {
634 /* set an invalid value */
635 attr->attributeID_id = DRSUAPI_ATTID_INVALID;
636 } else {
637 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
638 attr->attributeID_oid,
639 &attr->attributeID_id);
640 if (!W_ERROR_IS_OK(status)) {
641 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
642 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
643 win_errstr(status)));
644 return status;
647 /* fetch msDS-IntId to be used in resolving ATTRTYP values */
648 GET_UINT32_LDB(msg, "msDS-IntId", attr, msDS_IntId);
650 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
651 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
653 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
655 GET_GUID_LDB(msg, "objectGUID", attr, objectGUID);
657 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
658 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
659 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
660 GET_UINT32_LDB(msg, "linkID", attr, linkID);
662 GET_STRING_LDB(msg, "attributeSyntax", attr, attr, attributeSyntax_oid, true);
663 if (!schema->prefixmap || schema->prefixmap->length == 0) {
664 /* set an invalid value */
665 attr->attributeSyntax_id = DRSUAPI_ATTID_INVALID;
666 } else {
667 status = dsdb_schema_pfm_attid_from_oid(schema->prefixmap,
668 attr->attributeSyntax_oid,
669 &attr->attributeSyntax_id);
670 if (!W_ERROR_IS_OK(status)) {
671 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
672 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
673 win_errstr(status)));
674 return status;
677 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
678 GET_BLOB_LDB(msg, "oMObjectClass", attr, attr, oMObjectClass);
680 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
681 GET_UINT32_PTR_LDB(msg, "rangeLower", attr, attr, rangeLower);
682 GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, attr, rangeUpper);
683 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
685 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
686 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", attr, attr, msDs_Schema_Extensions);
688 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
689 GET_STRING_LDB(msg, "adminDisplayName", attr, attr, adminDisplayName, false);
690 GET_STRING_LDB(msg, "adminDescription", attr, attr, adminDescription, false);
691 GET_STRING_LDB(msg, "classDisplayName", attr, attr, classDisplayName, false);
692 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
693 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
694 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
696 return WERR_OK;
699 WERROR dsdb_set_attribute_from_ldb(struct ldb_context *ldb,
700 struct dsdb_schema *schema,
701 struct ldb_message *msg)
703 WERROR status;
704 struct dsdb_attribute *attr = talloc_zero(schema, struct dsdb_attribute);
705 if (!attr) {
706 return WERR_NOMEM;
709 status = dsdb_attribute_from_ldb(schema, msg, attr);
710 if (!W_ERROR_IS_OK(status)) {
711 return status;
714 attr->syntax = dsdb_syntax_for_attribute(attr);
715 if (!attr->syntax) {
716 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
717 attr->lDAPDisplayName));
718 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
721 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
722 DEBUG(0,(__location__ ": Unknown schema syntax for %s - ldb_syntax: %s, ldap_oid: %s\n",
723 attr->lDAPDisplayName,
724 attr->syntax->ldb_syntax,
725 attr->syntax->ldap_oid));
726 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
729 DLIST_ADD(schema->attributes, attr);
730 return WERR_OK;
733 WERROR dsdb_set_class_from_ldb(struct dsdb_schema *schema,
734 struct ldb_message *msg)
736 WERROR status;
737 struct dsdb_class *obj = talloc_zero(schema, struct dsdb_class);
738 if (!obj) {
739 return WERR_NOMEM;
741 GET_STRING_LDB(msg, "cn", obj, obj, cn, false);
742 GET_STRING_LDB(msg, "lDAPDisplayName", obj, obj, lDAPDisplayName, true);
743 GET_STRING_LDB(msg, "governsID", obj, obj, governsID_oid, true);
744 if (!schema->prefixmap || schema->prefixmap->length == 0) {
745 /* set an invalid value */
746 obj->governsID_id = DRSUAPI_ATTID_INVALID;
747 } else {
748 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
749 obj->governsID_oid,
750 &obj->governsID_id);
751 if (!W_ERROR_IS_OK(status)) {
752 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
753 __location__, obj->lDAPDisplayName, obj->governsID_oid,
754 win_errstr(status)));
755 return status;
758 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
759 GET_GUID_LDB(msg, "objectGUID", obj, objectGUID);
761 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
762 GET_STRING_LDB(msg, "rDNAttID", obj, obj, rDNAttID, false);
763 GET_STRING_LDB(msg, "defaultObjectCategory", obj, obj, defaultObjectCategory, true);
765 GET_STRING_LDB(msg, "subClassOf", obj, obj, subClassOf, true);
767 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", obj, obj, systemAuxiliaryClass);
768 GET_STRING_LIST_LDB(msg, "auxiliaryClass", obj, obj, auxiliaryClass);
770 GET_STRING_LIST_LDB(msg, "systemMustContain", obj, obj, systemMustContain);
771 GET_STRING_LIST_LDB(msg, "systemMayContain", obj, obj, systemMayContain);
772 GET_STRING_LIST_LDB(msg, "mustContain", obj, obj, mustContain);
773 GET_STRING_LIST_LDB(msg, "mayContain", obj, obj, mayContain);
775 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", obj, obj, systemPossSuperiors);
776 GET_STRING_LIST_LDB(msg, "possSuperiors", obj, obj, possSuperiors);
778 GET_STRING_LDB(msg, "defaultSecurityDescriptor", obj, obj, defaultSecurityDescriptor, false);
780 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
781 GET_UINT32_LDB(msg, "systemFlags", obj, systemFlags);
782 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", obj, obj, msDs_Schema_Extensions);
784 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
785 GET_STRING_LDB(msg, "adminDisplayName", obj, obj, adminDisplayName, false);
786 GET_STRING_LDB(msg, "adminDescription", obj, obj, adminDescription, false);
787 GET_STRING_LDB(msg, "classDisplayName", obj, obj, classDisplayName, false);
788 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
789 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
790 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
792 DLIST_ADD(schema->classes, obj);
793 return WERR_OK;
796 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
799 Fill a DSDB schema from the ldb results provided. This is called
800 directly when a schema must be created with a pre-initialised prefixMap
803 int dsdb_load_ldb_results_into_schema(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
804 struct dsdb_schema *schema,
805 struct ldb_result *attrs_class_res,
806 char **error_string)
808 unsigned int i;
810 for (i=0; i < attrs_class_res->count; i++) {
811 WERROR status = dsdb_schema_set_el_from_ldb_msg(ldb, schema, attrs_class_res->msgs[i]);
812 if (!W_ERROR_IS_OK(status)) {
813 *error_string = talloc_asprintf(mem_ctx,
814 "dsdb_load_ldb_results_into_schema: failed to load attribute or class definition: %s:%s",
815 ldb_dn_get_linearized(attrs_class_res->msgs[i]->dn),
816 win_errstr(status));
817 DEBUG(0,(__location__ ": %s\n", *error_string));
818 return LDB_ERR_CONSTRAINT_VIOLATION;
822 return LDB_SUCCESS;
826 Create a DSDB schema from the ldb results provided. This is called
827 directly when the schema is provisioned from an on-disk LDIF file, or
828 from dsdb_schema_from_schema_dn in schema_fsmo
831 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
832 struct ldb_result *schema_res,
833 struct ldb_result *attrs_class_res,
834 struct dsdb_schema **schema_out,
835 char **error_string)
837 WERROR status;
838 const struct ldb_val *prefix_val;
839 const struct ldb_val *info_val;
840 struct ldb_val info_val_default;
841 struct dsdb_schema *schema;
842 struct loadparm_context *lp_ctx = NULL;
843 int ret;
845 schema = dsdb_new_schema(mem_ctx);
846 if (!schema) {
847 dsdb_oom(error_string, mem_ctx);
848 return ldb_operr(ldb);
851 schema->base_dn = talloc_steal(schema, schema_res->msgs[0]->dn);
853 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
854 if (!prefix_val) {
855 *error_string = talloc_asprintf(mem_ctx,
856 "schema_fsmo_init: no prefixMap attribute found");
857 DEBUG(0,(__location__ ": %s\n", *error_string));
858 return LDB_ERR_CONSTRAINT_VIOLATION;
860 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
861 if (!info_val) {
862 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
863 if (!W_ERROR_IS_OK(status)) {
864 *error_string = talloc_asprintf(mem_ctx,
865 "schema_fsmo_init: dsdb_schema_info_blob_new() failed - %s",
866 win_errstr(status));
867 DEBUG(0,(__location__ ": %s\n", *error_string));
868 return ldb_operr(ldb);
870 info_val = &info_val_default;
873 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
874 if (!W_ERROR_IS_OK(status)) {
875 *error_string = talloc_asprintf(mem_ctx,
876 "schema_fsmo_init: failed to load oid mappings: %s",
877 win_errstr(status));
878 DEBUG(0,(__location__ ": %s\n", *error_string));
879 return LDB_ERR_CONSTRAINT_VIOLATION;
882 ret = dsdb_load_ldb_results_into_schema(mem_ctx, ldb, schema, attrs_class_res, error_string);
883 if (ret != LDB_SUCCESS) {
884 return ret;
887 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
888 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
889 schema->fsmo.we_are_master = true;
890 } else {
891 schema->fsmo.we_are_master = false;
894 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
895 struct loadparm_context);
896 if (lp_ctx) {
897 bool allowed = lpcfg_parm_bool(lp_ctx, NULL,
898 "dsdb", "schema update allowed",
899 false);
900 schema->fsmo.update_allowed = allowed;
901 } else {
902 schema->fsmo.update_allowed = false;
905 DEBUG(5, ("schema_fsmo_init: we are master[%s] updates allowed[%s]\n",
906 (schema->fsmo.we_are_master?"yes":"no"),
907 (schema->fsmo.update_allowed?"yes":"no")));
909 *schema_out = schema;
910 return LDB_SUCCESS;