make sure we update the current schema->prefixes when we add a new prefix
[Samba.git] / source4 / dsdb / schema / schema_init.c
blob170d5a12e970bddaf2be81907bee862c74829973
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 "lib/ldb/include/ldb_errors.h"
26 #include "../lib/util/dlinklist.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 #include "param/param.h"
31 #include "lib/ldb/include/ldb_module.h"
33 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
35 struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
36 if (!schema) {
37 return NULL;
40 schema->iconv_convenience = iconv_convenience;
41 return schema;
45 WERROR dsdb_load_oid_mappings_drsuapi(struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
47 uint32_t i,j;
49 schema->prefixes = talloc_array(schema, struct dsdb_schema_oid_prefix, ctr->num_mappings);
50 W_ERROR_HAVE_NO_MEMORY(schema->prefixes);
52 for (i=0, j=0; i < ctr->num_mappings; i++) {
53 if (ctr->mappings[i].oid.oid == NULL) {
54 return WERR_INVALID_PARAM;
57 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
58 if (ctr->mappings[i].id_prefix != 0) {
59 return WERR_INVALID_PARAM;
62 /* the magic value should be in the last array member */
63 if (i != (ctr->num_mappings - 1)) {
64 return WERR_INVALID_PARAM;
67 if (ctr->mappings[i].oid.__ndr_size != 21) {
68 return WERR_INVALID_PARAM;
71 schema->schema_info = talloc_strdup(schema, ctr->mappings[i].oid.oid);
72 W_ERROR_HAVE_NO_MEMORY(schema->schema_info);
73 } else {
74 /* the last array member should contain the magic value not a oid */
75 if (i == (ctr->num_mappings - 1)) {
76 return WERR_INVALID_PARAM;
79 schema->prefixes[j].id = ctr->mappings[i].id_prefix<<16;
80 schema->prefixes[j].oid = talloc_asprintf(schema->prefixes, "%s.",
81 ctr->mappings[i].oid.oid);
82 W_ERROR_HAVE_NO_MEMORY(schema->prefixes[j].oid);
83 schema->prefixes[j].oid_len = strlen(schema->prefixes[j].oid);
84 j++;
88 schema->num_prefixes = j;
89 return WERR_OK;
92 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
93 const struct ldb_val *prefixMap,
94 const struct ldb_val *schemaInfo)
96 WERROR status;
97 enum ndr_err_code ndr_err;
98 struct prefixMapBlob pfm;
99 char *schema_info;
101 TALLOC_CTX *mem_ctx = talloc_new(schema);
102 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
104 ndr_err = ndr_pull_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
105 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
106 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
107 talloc_free(mem_ctx);
108 return ntstatus_to_werror(nt_status);
111 if (pfm.version != PREFIX_MAP_VERSION_DSDB) {
112 talloc_free(mem_ctx);
113 return WERR_FOOBAR;
116 if (schemaInfo->length != 21 && schemaInfo->data[0] == 0xFF) {
117 talloc_free(mem_ctx);
118 return WERR_FOOBAR;
121 /* append the schema info as last element */
122 pfm.ctr.dsdb.num_mappings++;
123 pfm.ctr.dsdb.mappings = talloc_realloc(mem_ctx, pfm.ctr.dsdb.mappings,
124 struct drsuapi_DsReplicaOIDMapping,
125 pfm.ctr.dsdb.num_mappings);
126 W_ERROR_HAVE_NO_MEMORY(pfm.ctr.dsdb.mappings);
128 schema_info = data_blob_hex_string(pfm.ctr.dsdb.mappings, schemaInfo);
129 W_ERROR_HAVE_NO_MEMORY(schema_info);
131 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].id_prefix = 0;
132 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.__ndr_size = schemaInfo->length;
133 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.oid = schema_info;
135 /* call the drsuapi version */
136 status = dsdb_load_oid_mappings_drsuapi(schema, &pfm.ctr.dsdb);
137 talloc_free(mem_ctx);
139 W_ERROR_NOT_OK_RETURN(status);
141 return WERR_OK;
144 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
145 bool include_schema_info,
146 TALLOC_CTX *mem_ctx,
147 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
149 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
150 uint32_t i;
152 ctr = talloc(mem_ctx, struct drsuapi_DsReplicaOIDMapping_Ctr);
153 W_ERROR_HAVE_NO_MEMORY(ctr);
155 ctr->num_mappings = schema->num_prefixes;
156 if (include_schema_info) ctr->num_mappings++;
157 ctr->mappings = talloc_array(schema, struct drsuapi_DsReplicaOIDMapping, ctr->num_mappings);
158 W_ERROR_HAVE_NO_MEMORY(ctr->mappings);
160 for (i=0; i < schema->num_prefixes; i++) {
161 ctr->mappings[i].id_prefix = schema->prefixes[i].id>>16;
162 ctr->mappings[i].oid.oid = talloc_strndup(ctr->mappings,
163 schema->prefixes[i].oid,
164 schema->prefixes[i].oid_len - 1);
165 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
168 if (include_schema_info) {
169 ctr->mappings[i].id_prefix = 0;
170 ctr->mappings[i].oid.oid = talloc_strdup(ctr->mappings,
171 schema->schema_info);
172 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
175 *_ctr = ctr;
176 return WERR_OK;
179 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
180 TALLOC_CTX *mem_ctx,
181 struct ldb_val *prefixMap,
182 struct ldb_val *schemaInfo)
184 WERROR status;
185 enum ndr_err_code ndr_err;
186 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
187 struct prefixMapBlob pfm;
189 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
190 W_ERROR_NOT_OK_RETURN(status);
192 pfm.version = PREFIX_MAP_VERSION_DSDB;
193 pfm.reserved = 0;
194 pfm.ctr.dsdb = *ctr;
196 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
197 talloc_free(ctr);
198 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
199 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
200 return ntstatus_to_werror(nt_status);
203 *schemaInfo = strhex_to_data_blob(mem_ctx, schema->schema_info);
204 W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
206 return WERR_OK;
209 WERROR dsdb_verify_oid_mappings_drsuapi(const struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
211 uint32_t i,j;
213 for (i=0; i < ctr->num_mappings; i++) {
214 if (ctr->mappings[i].oid.oid == NULL) {
215 return WERR_INVALID_PARAM;
218 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
219 if (ctr->mappings[i].id_prefix != 0) {
220 return WERR_INVALID_PARAM;
223 /* the magic value should be in the last array member */
224 if (i != (ctr->num_mappings - 1)) {
225 return WERR_INVALID_PARAM;
228 if (ctr->mappings[i].oid.__ndr_size != 21) {
229 return WERR_INVALID_PARAM;
232 if (strcasecmp(schema->schema_info, ctr->mappings[i].oid.oid) != 0) {
233 return WERR_DS_DRA_SCHEMA_MISMATCH;
235 } else {
236 /* the last array member should contain the magic value not a oid */
237 if (i == (ctr->num_mappings - 1)) {
238 return WERR_INVALID_PARAM;
241 for (j=0; j < schema->num_prefixes; j++) {
242 size_t oid_len;
243 if (schema->prefixes[j].id != (ctr->mappings[i].id_prefix<<16)) {
244 continue;
247 oid_len = strlen(ctr->mappings[i].oid.oid);
249 if (oid_len != (schema->prefixes[j].oid_len - 1)) {
250 return WERR_DS_DRA_SCHEMA_MISMATCH;
253 if (strncmp(ctr->mappings[i].oid.oid, schema->prefixes[j].oid, oid_len) != 0) {
254 return WERR_DS_DRA_SCHEMA_MISMATCH;
257 break;
260 if (j == schema->num_prefixes) {
261 return WERR_DS_DRA_SCHEMA_MISMATCH;
266 return WERR_OK;
269 WERROR dsdb_map_oid2int(const struct dsdb_schema *schema, const char *in, uint32_t *out)
271 return dsdb_find_prefix_for_oid(schema->num_prefixes, schema->prefixes, in, out);
275 WERROR dsdb_map_int2oid(const struct dsdb_schema *schema, uint32_t in, TALLOC_CTX *mem_ctx, const char **out)
277 uint32_t i;
279 for (i=0; i < schema->num_prefixes; i++) {
280 const char *val;
281 if (schema->prefixes[i].id != (in & 0xFFFF0000)) {
282 continue;
285 val = talloc_asprintf(mem_ctx, "%s%u",
286 schema->prefixes[i].oid,
287 in & 0xFFFF);
288 W_ERROR_HAVE_NO_MEMORY(val);
290 *out = val;
291 return WERR_OK;
294 return WERR_DS_NO_MSDS_INTID;
298 * this function is called from within a ldb transaction from the schema_fsmo module
300 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
302 WERROR status;
303 uint32_t num_prefixes;
304 struct dsdb_schema_oid_prefix *prefixes;
305 TALLOC_CTX *mem_ctx;
306 uint32_t out;
308 mem_ctx = talloc_new(ldb);
309 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
311 /* Read prefixes from disk*/
312 status = dsdb_read_prefixes_from_ldb( mem_ctx, ldb, &num_prefixes, &prefixes );
313 if (!W_ERROR_IS_OK(status)) {
314 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
315 win_errstr(status)));
316 talloc_free(mem_ctx);
317 return status;
320 /* Check if there is a prefix for the oid in the prefixes array*/
321 status = dsdb_find_prefix_for_oid( num_prefixes, prefixes, full_oid, &out );
322 if (W_ERROR_IS_OK(status)) {
323 /* prefix found*/
324 talloc_free(mem_ctx);
325 return status;
326 } else if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
327 /* error */
328 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
329 win_errstr(status)));
330 talloc_free(mem_ctx);
331 return status;
334 /* Create the new mapping for the prefix of full_oid */
335 status = dsdb_prefix_map_update(mem_ctx, &num_prefixes, &prefixes, full_oid);
336 if (!W_ERROR_IS_OK(status)) {
337 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_prefix_map_update: %s\n",
338 win_errstr(status)));
339 talloc_free(mem_ctx);
340 return status;
343 /* Update prefixMap in ldb*/
344 status = dsdb_write_prefixes_to_ldb(mem_ctx, ldb, num_prefixes, prefixes);
345 if (!W_ERROR_IS_OK(status)) {
346 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
347 win_errstr(status)));
348 talloc_free(mem_ctx);
349 return status;
352 talloc_free(schema->prefixes);
353 schema->prefixes = talloc_steal(schema, prefixes);
354 schema->num_prefixes = num_prefixes;
356 DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
357 full_oid, num_prefixes));
359 talloc_free(mem_ctx);
360 return status;
363 WERROR dsdb_prefix_map_update(TALLOC_CTX *mem_ctx, uint32_t *num_prefixes, struct dsdb_schema_oid_prefix **prefixes, const char *oid)
365 uint32_t new_num_prefixes, index_new_prefix, new_entry_id;
366 const char* lastDotOffset;
367 size_t size;
369 new_num_prefixes = *num_prefixes + 1;
370 index_new_prefix = *num_prefixes;
373 * this is the algorithm we use to create new mappings for now
375 * TODO: find what algorithm windows use
377 new_entry_id = (*num_prefixes)<<16;
379 /* Extract the prefix from the oid*/
380 lastDotOffset = strrchr(oid, '.');
381 if (lastDotOffset == NULL) {
382 DEBUG(0,("dsdb_prefix_map_update: failed to find the last dot\n"));
383 return WERR_NOT_FOUND;
386 /* Calculate the size of the remainig string that should be the prefix of it */
387 size = strlen(oid) - strlen(lastDotOffset);
388 if (size <= 0) {
389 DEBUG(0,("dsdb_prefix_map_update: size of the remaining string invalid\n"));
390 return WERR_FOOBAR;
392 /* Add one because we need to copy the dot */
393 size += 1;
395 /* Create a spot in the prefixMap for one more prefix*/
396 (*prefixes) = talloc_realloc(mem_ctx, *prefixes, struct dsdb_schema_oid_prefix, new_num_prefixes);
397 W_ERROR_HAVE_NO_MEMORY(*prefixes);
399 /* Add the new prefix entry*/
400 (*prefixes)[index_new_prefix].id = new_entry_id;
401 (*prefixes)[index_new_prefix].oid = talloc_strndup(mem_ctx, oid, size);
402 (*prefixes)[index_new_prefix].oid_len = strlen((*prefixes)[index_new_prefix].oid);
404 /* Increase num_prefixes because new prefix has been added */
405 ++(*num_prefixes);
407 return WERR_OK;
410 WERROR dsdb_find_prefix_for_oid(uint32_t num_prefixes, const struct dsdb_schema_oid_prefix *prefixes, const char *in, uint32_t *out)
412 uint32_t i;
414 for (i=0; i < num_prefixes; i++) {
415 const char *val_str;
416 char *end_str;
417 unsigned val;
419 if (strncmp(prefixes[i].oid, in, prefixes[i].oid_len) != 0) {
420 continue;
423 val_str = in + prefixes[i].oid_len;
424 end_str = NULL;
425 errno = 0;
427 if (val_str[0] == '\0') {
428 return WERR_INVALID_PARAM;
431 /* two '.' chars are invalid */
432 if (val_str[0] == '.') {
433 return WERR_INVALID_PARAM;
436 val = strtoul(val_str, &end_str, 10);
437 if (end_str[0] == '.' && end_str[1] != '\0') {
439 * if it's a '.' and not the last char
440 * then maybe an other mapping apply
442 continue;
443 } else if (end_str[0] != '\0') {
444 return WERR_INVALID_PARAM;
445 } else if (val > 0xFFFF) {
446 return WERR_INVALID_PARAM;
449 *out = prefixes[i].id | val;
450 return WERR_OK;
453 DEBUG(5,(__location__ " Failed to find oid %s - have %u prefixes\n", in, num_prefixes));
455 return WERR_DS_NO_MSDS_INTID;
458 WERROR dsdb_write_prefixes_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
459 uint32_t num_prefixes,
460 const struct dsdb_schema_oid_prefix *prefixes)
462 struct ldb_message msg;
463 struct ldb_dn *schema_dn;
464 struct ldb_message_element el;
465 struct prefixMapBlob pm;
466 struct ldb_val ndr_blob;
467 enum ndr_err_code ndr_err;
468 uint32_t i;
469 int ret;
471 schema_dn = samdb_schema_dn(ldb);
472 if (!schema_dn) {
473 DEBUG(0,("dsdb_write_prefixes_to_ldb: no schema dn present\n"));
474 return WERR_FOOBAR;
477 pm.version = PREFIX_MAP_VERSION_DSDB;
478 pm.ctr.dsdb.num_mappings = num_prefixes;
479 pm.ctr.dsdb.mappings = talloc_array(mem_ctx,
480 struct drsuapi_DsReplicaOIDMapping,
481 pm.ctr.dsdb.num_mappings);
482 if (!pm.ctr.dsdb.mappings) {
483 return WERR_NOMEM;
486 for (i=0; i < num_prefixes; i++) {
487 pm.ctr.dsdb.mappings[i].id_prefix = prefixes[i].id>>16;
488 pm.ctr.dsdb.mappings[i].oid.oid = talloc_strdup(pm.ctr.dsdb.mappings, prefixes[i].oid);
491 ndr_err = ndr_push_struct_blob(&ndr_blob, ldb,
492 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
493 &pm,
494 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
495 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
496 return WERR_FOOBAR;
499 el.num_values = 1;
500 el.values = &ndr_blob;
501 el.flags = LDB_FLAG_MOD_REPLACE;
502 el.name = talloc_strdup(mem_ctx, "prefixMap");
504 msg.dn = ldb_dn_copy(mem_ctx, schema_dn);
505 msg.num_elements = 1;
506 msg.elements = &el;
508 ret = ldb_modify( ldb, &msg );
509 if (ret != 0) {
510 DEBUG(0,("dsdb_write_prefixes_to_ldb: ldb_modify failed\n"));
511 return WERR_FOOBAR;
514 return WERR_OK;
517 WERROR dsdb_read_prefixes_from_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, uint32_t* num_prefixes, struct dsdb_schema_oid_prefix **prefixes)
519 struct prefixMapBlob *blob;
520 enum ndr_err_code ndr_err;
521 uint32_t i;
522 const struct ldb_val *prefix_val;
523 struct ldb_dn *schema_dn;
524 struct ldb_result *schema_res;
525 int ret;
526 static const char *schema_attrs[] = {
527 "prefixMap",
528 NULL
531 schema_dn = samdb_schema_dn(ldb);
532 if (!schema_dn) {
533 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
534 return WERR_FOOBAR;
537 ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
538 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
539 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
540 talloc_free(schema_res);
541 return WERR_FOOBAR;
542 } else if (ret != LDB_SUCCESS) {
543 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
544 talloc_free(schema_res);
545 return WERR_FOOBAR;
548 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
549 if (!prefix_val) {
550 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
551 talloc_free(schema_res);
552 return WERR_FOOBAR;
555 blob = talloc(mem_ctx, struct prefixMapBlob);
556 W_ERROR_HAVE_NO_MEMORY(blob);
558 ndr_err = ndr_pull_struct_blob(prefix_val, blob,
559 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
560 blob,
561 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
562 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
563 DEBUG(0,("dsdb_read_prefixes_from_ldb: ndr_pull_struct_blob failed\n"));
564 talloc_free(blob);
565 talloc_free(schema_res);
566 return WERR_FOOBAR;
569 talloc_free(schema_res);
571 if (blob->version != PREFIX_MAP_VERSION_DSDB) {
572 DEBUG(0,("dsdb_read_prefixes_from_ldb: blob->version incorect\n"));
573 talloc_free(blob);
574 return WERR_FOOBAR;
577 *num_prefixes = blob->ctr.dsdb.num_mappings;
578 *prefixes = talloc_array(mem_ctx, struct dsdb_schema_oid_prefix, *num_prefixes);
579 if(!(*prefixes)) {
580 talloc_free(blob);
581 return WERR_NOMEM;
583 for (i=0; i < blob->ctr.dsdb.num_mappings; i++) {
584 char *oid;
585 (*prefixes)[i].id = blob->ctr.dsdb.mappings[i].id_prefix<<16;
586 oid = talloc_strdup(mem_ctx, blob->ctr.dsdb.mappings[i].oid.oid);
587 (*prefixes)[i].oid = talloc_asprintf_append(oid, ".");
588 (*prefixes)[i].oid_len = strlen((*prefixes)[i].oid);
591 talloc_free(blob);
592 return WERR_OK;
596 this will be replaced with something that looks at the right part of
597 the schema once we know where unique indexing information is hidden
599 static bool dsdb_schema_unique_attribute(const char *attr)
601 const char *attrs[] = { "objectGUID", "objectSID" , NULL };
602 int i;
603 for (i=0;attrs[i];i++) {
604 if (strcasecmp(attr, attrs[i]) == 0) {
605 return true;
608 return false;
613 setup the ldb_schema_attribute field for a dsdb_attribute
615 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb,
616 struct dsdb_attribute *attr)
618 const char *syntax = attr->syntax->ldb_syntax;
619 const struct ldb_schema_syntax *s;
620 struct ldb_schema_attribute *a;
622 if (!syntax) {
623 syntax = attr->syntax->ldap_oid;
626 s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
627 if (s == NULL) {
628 s = ldb_samba_syntax_by_name(ldb, syntax);
630 if (s == NULL) {
631 s = ldb_standard_syntax_by_name(ldb, syntax);
634 if (s == NULL) {
635 return LDB_ERR_OPERATIONS_ERROR;
638 attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
639 if (attr->ldb_schema_attribute == NULL) {
640 ldb_oom(ldb);
641 return LDB_ERR_OPERATIONS_ERROR;
644 a->name = attr->lDAPDisplayName;
645 a->flags = 0;
646 a->syntax = s;
648 if (dsdb_schema_unique_attribute(a->name)) {
649 a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
652 return LDB_SUCCESS;
657 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
658 (p)->elem = samdb_result_string(msg, attr, NULL);\
659 if (strict && (p)->elem == NULL) { \
660 d_printf("%s: %s == NULL\n", __location__, attr); \
661 return WERR_INVALID_PARAM; \
663 talloc_steal(mem_ctx, (p)->elem); \
664 } while (0)
666 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
667 int get_string_list_counter; \
668 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
669 if (get_string_list_el == NULL) { \
670 if (strict) { \
671 d_printf("%s: %s == NULL\n", __location__, attr); \
672 return WERR_INVALID_PARAM; \
673 } else { \
674 (p)->elem = NULL; \
675 break; \
678 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
679 for (get_string_list_counter=0; \
680 get_string_list_counter < get_string_list_el->num_values; \
681 get_string_list_counter++) { \
682 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
683 (const char *)get_string_list_el->values[get_string_list_counter].data, \
684 get_string_list_el->values[get_string_list_counter].length); \
685 if (!(p)->elem[get_string_list_counter]) { \
686 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
687 return WERR_NOMEM; \
689 (p)->elem[get_string_list_counter+1] = NULL; \
691 talloc_steal(mem_ctx, (p)->elem); \
692 } while (0)
694 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
695 const char *str; \
696 str = samdb_result_string(msg, attr, NULL);\
697 if (str == NULL) { \
698 if (strict) { \
699 d_printf("%s: %s == NULL\n", __location__, attr); \
700 return WERR_INVALID_PARAM; \
701 } else { \
702 (p)->elem = false; \
704 } else if (strcasecmp("TRUE", str) == 0) { \
705 (p)->elem = true; \
706 } else if (strcasecmp("FALSE", str) == 0) { \
707 (p)->elem = false; \
708 } else { \
709 d_printf("%s: %s == %s\n", __location__, attr, str); \
710 return WERR_INVALID_PARAM; \
712 } while (0)
714 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
715 (p)->elem = samdb_result_uint(msg, attr, 0);\
716 } while (0)
718 #define GET_UINT32_PTR_LDB(msg, attr, p, elem) do { \
719 uint64_t _v = samdb_result_uint64(msg, attr, UINT64_MAX);\
720 if (_v == UINT64_MAX) { \
721 (p)->elem = NULL; \
722 } else if (_v > UINT32_MAX) { \
723 d_printf("%s: %s == 0x%llX\n", __location__, \
724 attr, (unsigned long long)_v); \
725 return WERR_INVALID_PARAM; \
726 } else { \
727 (p)->elem = talloc(mem_ctx, uint32_t); \
728 if (!(p)->elem) { \
729 d_printf("%s: talloc failed for %s\n", __location__, attr); \
730 return WERR_NOMEM; \
732 *(p)->elem = (uint32_t)_v; \
734 } while (0)
736 #define GET_GUID_LDB(msg, attr, p, elem) do { \
737 (p)->elem = samdb_result_guid(msg, attr);\
738 } while (0)
740 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
741 const struct ldb_val *_val;\
742 _val = ldb_msg_find_ldb_val(msg, attr);\
743 if (_val) {\
744 (p)->elem = *_val;\
745 talloc_steal(mem_ctx, (p)->elem.data);\
746 } else {\
747 ZERO_STRUCT((p)->elem);\
749 } while (0)
751 WERROR dsdb_attribute_from_ldb(struct ldb_context *ldb,
752 const struct dsdb_schema *schema,
753 struct ldb_message *msg,
754 TALLOC_CTX *mem_ctx,
755 struct dsdb_attribute *attr)
757 WERROR status;
759 GET_STRING_LDB(msg, "cn", mem_ctx, attr, cn, false);
760 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
761 GET_STRING_LDB(msg, "attributeID", mem_ctx, attr, attributeID_oid, true);
762 if (schema->num_prefixes == 0) {
763 /* set an invalid value */
764 attr->attributeID_id = 0xFFFFFFFF;
765 } else {
766 status = dsdb_map_oid2int(schema, attr->attributeID_oid, &attr->attributeID_id);
767 if (!W_ERROR_IS_OK(status)) {
768 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
769 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
770 win_errstr(status)));
771 return status;
774 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
775 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
777 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
779 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
780 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
781 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
782 GET_UINT32_LDB(msg, "linkID", attr, linkID);
784 GET_STRING_LDB(msg, "attributeSyntax", mem_ctx, attr, attributeSyntax_oid, true);
785 if (schema->num_prefixes == 0) {
786 /* set an invalid value */
787 attr->attributeSyntax_id = 0xFFFFFFFF;
788 } else {
789 status = dsdb_map_oid2int(schema, attr->attributeSyntax_oid, &attr->attributeSyntax_id);
790 if (!W_ERROR_IS_OK(status)) {
791 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
792 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
793 win_errstr(status)));
794 return status;
797 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
798 GET_BLOB_LDB(msg, "oMObjectClass", mem_ctx, attr, oMObjectClass);
800 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
801 GET_UINT32_PTR_LDB(msg, "rangeLower", attr, rangeLower);
802 GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, rangeUpper);
803 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
805 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
806 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
808 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
809 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
810 GET_STRING_LDB(msg, "adminDescription", mem_ctx, attr, adminDescription, false);
811 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, attr, classDisplayName, false);
812 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
813 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
814 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
816 attr->syntax = dsdb_syntax_for_attribute(attr);
817 if (!attr->syntax) {
818 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
821 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
822 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
825 return WERR_OK;
828 WERROR dsdb_class_from_ldb(const struct dsdb_schema *schema,
829 struct ldb_message *msg,
830 TALLOC_CTX *mem_ctx,
831 struct dsdb_class *obj)
833 WERROR status;
835 GET_STRING_LDB(msg, "cn", mem_ctx, obj, cn, false);
836 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
837 GET_STRING_LDB(msg, "governsID", mem_ctx, obj, governsID_oid, true);
838 if (schema->num_prefixes == 0) {
839 /* set an invalid value */
840 obj->governsID_id = 0xFFFFFFFF;
841 } else {
842 status = dsdb_map_oid2int(schema, obj->governsID_oid, &obj->governsID_id);
843 if (!W_ERROR_IS_OK(status)) {
844 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
845 __location__, obj->lDAPDisplayName, obj->governsID_oid,
846 win_errstr(status)));
847 return status;
850 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
852 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
853 GET_STRING_LDB(msg, "rDNAttID", mem_ctx, obj, rDNAttID, false);
854 GET_STRING_LDB(msg, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
856 GET_STRING_LDB(msg, "subClassOf", mem_ctx, obj, subClassOf, true);
858 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass, false);
859 GET_STRING_LIST_LDB(msg, "auxiliaryClass", mem_ctx, obj, auxiliaryClass, false);
861 GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, false);
862 GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, false);
863 GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, false);
864 GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, false);
866 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
867 GET_STRING_LIST_LDB(msg, "possSuperiors", mem_ctx, obj, possSuperiors, false);
869 GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
871 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
872 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
874 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
875 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
876 GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, false);
877 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, false);
878 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
879 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
880 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
882 return WERR_OK;
885 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
888 Create a DSDB schema from the ldb results provided. This is called
889 directly when the schema is provisioned from an on-disk LDIF file, or
890 from dsdb_schema_from_schema_dn below
893 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
894 struct smb_iconv_convenience *iconv_convenience,
895 struct ldb_result *schema_res,
896 struct ldb_result *attrs_res, struct ldb_result *objectclass_res,
897 struct dsdb_schema **schema_out,
898 char **error_string)
900 WERROR status;
901 uint32_t i;
902 const struct ldb_val *prefix_val;
903 const struct ldb_val *info_val;
904 struct ldb_val info_val_default;
905 struct dsdb_schema *schema;
907 schema = dsdb_new_schema(mem_ctx, iconv_convenience);
908 if (!schema) {
909 dsdb_oom(error_string, mem_ctx);
910 return LDB_ERR_OPERATIONS_ERROR;
913 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
914 if (!prefix_val) {
915 *error_string = talloc_asprintf(mem_ctx,
916 "schema_fsmo_init: no prefixMap attribute found");
917 return LDB_ERR_CONSTRAINT_VIOLATION;
919 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
920 if (!info_val) {
921 info_val_default = strhex_to_data_blob(mem_ctx, "FF0000000000000000000000000000000000000000");
922 if (!info_val_default.data) {
923 dsdb_oom(error_string, mem_ctx);
924 return LDB_ERR_OPERATIONS_ERROR;
926 info_val = &info_val_default;
929 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
930 if (!W_ERROR_IS_OK(status)) {
931 *error_string = talloc_asprintf(mem_ctx,
932 "schema_fsmo_init: failed to load oid mappings: %s",
933 win_errstr(status));
934 return LDB_ERR_CONSTRAINT_VIOLATION;
937 for (i=0; i < attrs_res->count; i++) {
938 struct dsdb_attribute *sa;
940 sa = talloc_zero(schema, struct dsdb_attribute);
941 if (!sa) {
942 dsdb_oom(error_string, mem_ctx);
943 return LDB_ERR_OPERATIONS_ERROR;
946 status = dsdb_attribute_from_ldb(ldb, schema, attrs_res->msgs[i], sa, sa);
947 if (!W_ERROR_IS_OK(status)) {
948 *error_string = talloc_asprintf(mem_ctx,
949 "schema_fsmo_init: failed to load attribute definition: %s:%s",
950 ldb_dn_get_linearized(attrs_res->msgs[i]->dn),
951 win_errstr(status));
952 return LDB_ERR_CONSTRAINT_VIOLATION;
955 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
958 for (i=0; i < objectclass_res->count; i++) {
959 struct dsdb_class *sc;
961 sc = talloc_zero(schema, struct dsdb_class);
962 if (!sc) {
963 dsdb_oom(error_string, mem_ctx);
964 return LDB_ERR_OPERATIONS_ERROR;
967 status = dsdb_class_from_ldb(schema, objectclass_res->msgs[i], sc, sc);
968 if (!W_ERROR_IS_OK(status)) {
969 *error_string = talloc_asprintf(mem_ctx,
970 "schema_fsmo_init: failed to load class definition: %s:%s",
971 ldb_dn_get_linearized(objectclass_res->msgs[i]->dn),
972 win_errstr(status));
973 return LDB_ERR_CONSTRAINT_VIOLATION;
976 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
979 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
980 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
981 schema->fsmo.we_are_master = true;
982 } else {
983 schema->fsmo.we_are_master = false;
986 DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
987 (schema->fsmo.we_are_master?"yes":"no")));
989 *schema_out = schema;
990 return LDB_SUCCESS;
994 Given an LDB, and the DN, return a populated schema
997 int dsdb_schema_from_schema_dn(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
998 struct smb_iconv_convenience *iconv_convenience,
999 struct ldb_dn *schema_dn,
1000 struct dsdb_schema **schema,
1001 char **error_string_out)
1003 TALLOC_CTX *tmp_ctx;
1004 char *error_string;
1005 int ret;
1007 struct ldb_result *schema_res;
1008 struct ldb_result *a_res;
1009 struct ldb_result *c_res;
1010 static const char *schema_attrs[] = {
1011 "prefixMap",
1012 "schemaInfo",
1013 "fSMORoleOwner",
1014 NULL
1017 tmp_ctx = talloc_new(mem_ctx);
1018 if (!tmp_ctx) {
1019 dsdb_oom(error_string_out, mem_ctx);
1020 return LDB_ERR_OPERATIONS_ERROR;
1024 * setup the prefix mappings and schema info
1026 ret = ldb_search(ldb, tmp_ctx, &schema_res,
1027 schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
1028 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1029 talloc_free(tmp_ctx);
1030 return ret;
1031 } else if (ret != LDB_SUCCESS) {
1032 *error_string_out = talloc_asprintf(mem_ctx,
1033 "dsdb_schema: failed to search the schema head: %s",
1034 ldb_errstring(ldb));
1035 talloc_free(tmp_ctx);
1036 return ret;
1038 if (schema_res->count != 1) {
1039 *error_string_out = talloc_asprintf(mem_ctx,
1040 "dsdb_schema: [%u] schema heads found on a base search",
1041 schema_res->count);
1042 talloc_free(tmp_ctx);
1043 return LDB_ERR_CONSTRAINT_VIOLATION;
1047 * load the attribute definitions
1049 ret = ldb_search(ldb, tmp_ctx, &a_res,
1050 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
1051 "(objectClass=attributeSchema)");
1052 if (ret != LDB_SUCCESS) {
1053 *error_string_out = talloc_asprintf(mem_ctx,
1054 "dsdb_schema: failed to search attributeSchema objects: %s",
1055 ldb_errstring(ldb));
1056 talloc_free(tmp_ctx);
1057 return ret;
1061 * load the objectClass definitions
1063 ret = ldb_search(ldb, tmp_ctx, &c_res,
1064 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
1065 "(objectClass=classSchema)");
1066 if (ret != LDB_SUCCESS) {
1067 *error_string_out = talloc_asprintf(mem_ctx,
1068 "dsdb_schema: failed to search attributeSchema objects: %s",
1069 ldb_errstring(ldb));
1070 talloc_free(tmp_ctx);
1071 return ret;
1074 ret = dsdb_schema_from_ldb_results(tmp_ctx, ldb,
1075 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
1076 schema_res, a_res, c_res, schema, &error_string);
1077 if (ret != LDB_SUCCESS) {
1078 *error_string_out = talloc_asprintf(mem_ctx,
1079 "dsdb_schema load failed: %s",
1080 error_string);
1081 talloc_free(tmp_ctx);
1082 return ret;
1084 talloc_steal(mem_ctx, *schema);
1085 talloc_free(tmp_ctx);
1087 return LDB_SUCCESS;
1091 static const struct {
1092 const char *name;
1093 const char *oid;
1094 } name_mappings[] = {
1095 { "cn", "2.5.4.3" },
1096 { "name", "1.2.840.113556.1.4.1" },
1097 { "lDAPDisplayName", "1.2.840.113556.1.2.460" },
1098 { "attributeID", "1.2.840.113556.1.2.30" },
1099 { "schemaIDGUID", "1.2.840.113556.1.4.148" },
1100 { "mAPIID", "1.2.840.113556.1.2.49" },
1101 { "attributeSecurityGUID", "1.2.840.113556.1.4.149" },
1102 { "searchFlags", "1.2.840.113556.1.2.334" },
1103 { "systemFlags", "1.2.840.113556.1.4.375" },
1104 { "isMemberOfPartialAttributeSet", "1.2.840.113556.1.4.639" },
1105 { "linkID", "1.2.840.113556.1.2.50" },
1106 { "attributeSyntax", "1.2.840.113556.1.2.32" },
1107 { "oMSyntax", "1.2.840.113556.1.2.231" },
1108 { "oMObjectClass", "1.2.840.113556.1.2.218" },
1109 { "isSingleValued", "1.2.840.113556.1.2.33" },
1110 { "rangeLower", "1.2.840.113556.1.2.34" },
1111 { "rangeUpper", "1.2.840.113556.1.2.35" },
1112 { "extendedCharsAllowed", "1.2.840.113556.1.2.380" },
1113 { "schemaFlagsEx", "1.2.840.113556.1.4.120" },
1114 { "msDs-Schema-Extensions", "1.2.840.113556.1.4.1440" },
1115 { "showInAdvancedViewOnly", "1.2.840.113556.1.2.169" },
1116 { "adminDisplayName", "1.2.840.113556.1.2.194" },
1117 { "adminDescription", "1.2.840.113556.1.2.226" },
1118 { "classDisplayName", "1.2.840.113556.1.4.610" },
1119 { "isEphemeral", "1.2.840.113556.1.4.1212" },
1120 { "isDefunct", "1.2.840.113556.1.4.661" },
1121 { "systemOnly", "1.2.840.113556.1.4.170" },
1122 { "governsID", "1.2.840.113556.1.2.22" },
1123 { "objectClassCategory", "1.2.840.113556.1.2.370" },
1124 { "rDNAttID", "1.2.840.113556.1.2.26" },
1125 { "defaultObjectCategory", "1.2.840.113556.1.4.783" },
1126 { "subClassOf", "1.2.840.113556.1.2.21" },
1127 { "systemAuxiliaryClass", "1.2.840.113556.1.4.198" },
1128 { "systemPossSuperiors", "1.2.840.113556.1.4.195" },
1129 { "systemMustContain", "1.2.840.113556.1.4.197" },
1130 { "systemMayContain", "1.2.840.113556.1.4.196" },
1131 { "auxiliaryClass", "1.2.840.113556.1.2.351" },
1132 { "possSuperiors", "1.2.840.113556.1.2.8" },
1133 { "mustContain", "1.2.840.113556.1.2.24" },
1134 { "mayContain", "1.2.840.113556.1.2.25" },
1135 { "defaultSecurityDescriptor", "1.2.840.113556.1.4.224" },
1136 { "defaultHidingValue", "1.2.840.113556.1.4.518" },
1139 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
1140 struct drsuapi_DsReplicaObject *obj,
1141 const char *name,
1142 uint32_t *idx)
1144 WERROR status;
1145 uint32_t i, id;
1146 const char *oid = NULL;
1148 for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
1149 if (strcmp(name_mappings[i].name, name) != 0) continue;
1151 oid = name_mappings[i].oid;
1152 break;
1155 if (!oid) {
1156 return NULL;
1159 status = dsdb_map_oid2int(schema, oid, &id);
1160 if (!W_ERROR_IS_OK(status)) {
1161 return NULL;
1164 for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
1165 if (obj->attribute_ctr.attributes[i].attid != id) continue;
1167 if (idx) *idx = i;
1168 return &obj->attribute_ctr.attributes[i];
1171 return NULL;
1174 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1175 struct drsuapi_DsReplicaAttribute *_a; \
1176 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1177 if (strict && !_a) { \
1178 d_printf("%s: %s == NULL\n", __location__, attr); \
1179 return WERR_INVALID_PARAM; \
1181 if (strict && _a->value_ctr.num_values != 1) { \
1182 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1183 _a->value_ctr.num_values); \
1184 return WERR_INVALID_PARAM; \
1186 if (_a && _a->value_ctr.num_values >= 1) { \
1187 size_t _ret; \
1188 if (!convert_string_talloc_convenience(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
1189 _a->value_ctr.values[0].blob->data, \
1190 _a->value_ctr.values[0].blob->length, \
1191 (void **)discard_const(&(p)->elem), &_ret, false)) { \
1192 DEBUG(0,("%s: invalid data!\n", attr)); \
1193 dump_data(0, \
1194 _a->value_ctr.values[0].blob->data, \
1195 _a->value_ctr.values[0].blob->length); \
1196 return WERR_FOOBAR; \
1198 } else { \
1199 (p)->elem = NULL; \
1201 } while (0)
1203 #define GET_UINT32_LIST_DS(s, r, attr, mem_ctx, p, elem) do { \
1204 int list_counter; \
1205 struct drsuapi_DsReplicaAttribute *_a; \
1206 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1207 (p)->elem = _a ? talloc_array(mem_ctx, uint32_t, _a->value_ctr.num_values + 1) : NULL; \
1208 for (list_counter=0; \
1209 _a && list_counter < _a->value_ctr.num_values; \
1210 list_counter++) { \
1211 if (_a->value_ctr.values[list_counter].blob->length != 4) { \
1212 return WERR_INVALID_PARAM; \
1214 (p)->elem[list_counter] = IVAL(_a->value_ctr.values[list_counter].blob->data, 0); \
1216 if (_a) (p)->elem[list_counter] = 0; \
1217 } while (0)
1219 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1220 struct drsuapi_DsReplicaAttribute *_a; \
1221 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1222 if (strict && !_a) { \
1223 d_printf("%s: %s == NULL\n", __location__, attr); \
1224 return WERR_INVALID_PARAM; \
1226 if (strict && _a->value_ctr.num_values != 1) { \
1227 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1228 _a->value_ctr.num_values); \
1229 return WERR_INVALID_PARAM; \
1231 if (strict && !_a->value_ctr.values[0].blob) { \
1232 d_printf("%s: %s data == NULL\n", __location__, attr); \
1233 return WERR_INVALID_PARAM; \
1235 if (_a && _a->value_ctr.num_values >= 1 \
1236 && _a->value_ctr.values[0].blob) { \
1237 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
1238 enum ndr_err_code _ndr_err; \
1239 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1240 mem_ctx, s->iconv_convenience, &_id3,\
1241 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
1242 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1243 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1244 return ntstatus_to_werror(_nt_status); \
1246 (p)->elem = _id3.dn; \
1247 } else { \
1248 (p)->elem = NULL; \
1250 } while (0)
1252 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
1253 struct drsuapi_DsReplicaAttribute *_a; \
1254 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1255 if (strict && !_a) { \
1256 d_printf("%s: %s == NULL\n", __location__, attr); \
1257 return WERR_INVALID_PARAM; \
1259 if (strict && _a->value_ctr.num_values != 1) { \
1260 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1261 (unsigned int)_a->value_ctr.num_values); \
1262 return WERR_INVALID_PARAM; \
1264 if (strict && !_a->value_ctr.values[0].blob) { \
1265 d_printf("%s: %s data == NULL\n", __location__, attr); \
1266 return WERR_INVALID_PARAM; \
1268 if (strict && _a->value_ctr.values[0].blob->length != 4) { \
1269 d_printf("%s: %s length == %u\n", __location__, attr, \
1270 (unsigned int)_a->value_ctr.values[0].blob->length); \
1271 return WERR_INVALID_PARAM; \
1273 if (_a && _a->value_ctr.num_values >= 1 \
1274 && _a->value_ctr.values[0].blob \
1275 && _a->value_ctr.values[0].blob->length == 4) { \
1276 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?true:false);\
1277 } else { \
1278 (p)->elem = false; \
1280 } while (0)
1282 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
1283 struct drsuapi_DsReplicaAttribute *_a; \
1284 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1285 if (_a && _a->value_ctr.num_values >= 1 \
1286 && _a->value_ctr.values[0].blob \
1287 && _a->value_ctr.values[0].blob->length == 4) { \
1288 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1289 } else { \
1290 (p)->elem = 0; \
1292 } while (0)
1294 #define GET_UINT32_PTR_DS(s, r, attr, p, elem) do { \
1295 struct drsuapi_DsReplicaAttribute *_a; \
1296 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1297 if (_a && _a->value_ctr.num_values >= 1 \
1298 && _a->value_ctr.values[0].blob \
1299 && _a->value_ctr.values[0].blob->length == 4) { \
1300 (p)->elem = talloc(mem_ctx, uint32_t); \
1301 if (!(p)->elem) { \
1302 d_printf("%s: talloc failed for %s\n", __location__, attr); \
1303 return WERR_NOMEM; \
1305 *(p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1306 } else { \
1307 (p)->elem = NULL; \
1309 } while (0)
1311 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
1312 struct drsuapi_DsReplicaAttribute *_a; \
1313 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1314 if (_a && _a->value_ctr.num_values >= 1 \
1315 && _a->value_ctr.values[0].blob \
1316 && _a->value_ctr.values[0].blob->length == 16) { \
1317 enum ndr_err_code _ndr_err; \
1318 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1319 mem_ctx, s->iconv_convenience, &(p)->elem, \
1320 (ndr_pull_flags_fn_t)ndr_pull_GUID); \
1321 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1322 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1323 return ntstatus_to_werror(_nt_status); \
1325 } else { \
1326 ZERO_STRUCT((p)->elem);\
1328 } while (0)
1330 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
1331 struct drsuapi_DsReplicaAttribute *_a; \
1332 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1333 if (_a && _a->value_ctr.num_values >= 1 \
1334 && _a->value_ctr.values[0].blob) { \
1335 (p)->elem = *_a->value_ctr.values[0].blob;\
1336 talloc_steal(mem_ctx, (p)->elem.data); \
1337 } else { \
1338 ZERO_STRUCT((p)->elem);\
1340 } while (0)
1342 WERROR dsdb_attribute_from_drsuapi(struct ldb_context *ldb,
1343 struct dsdb_schema *schema,
1344 struct drsuapi_DsReplicaObject *r,
1345 TALLOC_CTX *mem_ctx,
1346 struct dsdb_attribute *attr)
1348 WERROR status;
1350 GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, true);
1351 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
1352 GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
1353 status = dsdb_map_int2oid(schema, attr->attributeID_id, mem_ctx, &attr->attributeID_oid);
1354 if (!W_ERROR_IS_OK(status)) {
1355 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
1356 __location__, attr->lDAPDisplayName, attr->attributeID_id,
1357 win_errstr(status)));
1358 return status;
1360 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
1361 GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
1363 GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
1365 GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
1366 GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
1367 GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
1368 GET_UINT32_DS(schema, r, "linkID", attr, linkID);
1370 GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
1371 status = dsdb_map_int2oid(schema, attr->attributeSyntax_id, mem_ctx, &attr->attributeSyntax_oid);
1372 if (!W_ERROR_IS_OK(status)) {
1373 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
1374 __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
1375 win_errstr(status)));
1376 return status;
1378 GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
1379 GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
1381 GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, true);
1382 GET_UINT32_PTR_DS(schema, r, "rangeLower", attr, rangeLower);
1383 GET_UINT32_PTR_DS(schema, r, "rangeUpper", attr, rangeUpper);
1384 GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
1386 GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
1387 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
1389 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
1390 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
1391 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, false);
1392 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, false);
1393 GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, false);
1394 GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, false);
1395 GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, false);
1397 attr->syntax = dsdb_syntax_for_attribute(attr);
1398 if (!attr->syntax) {
1399 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1402 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
1403 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1406 return WERR_OK;
1409 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
1410 struct drsuapi_DsReplicaObject *r,
1411 TALLOC_CTX *mem_ctx,
1412 struct dsdb_class *obj)
1414 WERROR status;
1416 GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, true);
1417 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
1418 GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
1419 status = dsdb_map_int2oid(schema, obj->governsID_id, mem_ctx, &obj->governsID_oid);
1420 if (!W_ERROR_IS_OK(status)) {
1421 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
1422 __location__, obj->lDAPDisplayName, obj->governsID_id,
1423 win_errstr(status)));
1424 return status;
1426 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
1428 GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
1429 GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
1430 GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
1432 GET_UINT32_DS(schema, r, "subClassOf", obj, subClassOf_id);
1434 GET_UINT32_LIST_DS(schema, r, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass_ids);
1435 GET_UINT32_LIST_DS(schema, r, "auxiliaryClass", mem_ctx, obj, auxiliaryClass_ids);
1437 GET_UINT32_LIST_DS(schema, r, "systemMustContain", mem_ctx, obj, systemMustContain_ids);
1438 GET_UINT32_LIST_DS(schema, r, "systemMayContain", mem_ctx, obj, systemMayContain_ids);
1439 GET_UINT32_LIST_DS(schema, r, "mustContain", mem_ctx, obj, mustContain_ids);
1440 GET_UINT32_LIST_DS(schema, r, "mayContain", mem_ctx, obj, mayContain_ids);
1442 GET_UINT32_LIST_DS(schema, r, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors_ids);
1443 GET_UINT32_LIST_DS(schema, r, "possSuperiors", mem_ctx, obj, possSuperiors_ids);
1445 GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
1447 GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
1448 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
1450 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
1451 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
1452 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, false);
1453 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, false);
1454 GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, false);
1455 GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, false);
1456 GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, false);
1458 return WERR_OK;