Merge ldb_search() and ldb_search_exp_fmt() into a simgle function.
[Samba.git] / source4 / dsdb / schema / schema_init.c
blobd983e3c2720fe68762839b146fec2ddcb359fb6f
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"
32 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
34 struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
35 if (!schema) {
36 return NULL;
39 schema->iconv_convenience = iconv_convenience;
40 return schema;
44 WERROR dsdb_load_oid_mappings_drsuapi(struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
46 uint32_t i,j;
48 schema->prefixes = talloc_array(schema, struct dsdb_schema_oid_prefix, ctr->num_mappings);
49 W_ERROR_HAVE_NO_MEMORY(schema->prefixes);
51 for (i=0, j=0; i < ctr->num_mappings; i++) {
52 if (ctr->mappings[i].oid.oid == NULL) {
53 return WERR_INVALID_PARAM;
56 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
57 if (ctr->mappings[i].id_prefix != 0) {
58 return WERR_INVALID_PARAM;
61 /* the magic value should be in the last array member */
62 if (i != (ctr->num_mappings - 1)) {
63 return WERR_INVALID_PARAM;
66 if (ctr->mappings[i].oid.__ndr_size != 21) {
67 return WERR_INVALID_PARAM;
70 schema->schema_info = talloc_strdup(schema, ctr->mappings[i].oid.oid);
71 W_ERROR_HAVE_NO_MEMORY(schema->schema_info);
72 } else {
73 /* the last array member should contain the magic value not a oid */
74 if (i == (ctr->num_mappings - 1)) {
75 return WERR_INVALID_PARAM;
78 schema->prefixes[j].id = ctr->mappings[i].id_prefix<<16;
79 schema->prefixes[j].oid = talloc_asprintf(schema->prefixes, "%s.",
80 ctr->mappings[i].oid.oid);
81 W_ERROR_HAVE_NO_MEMORY(schema->prefixes[j].oid);
82 schema->prefixes[j].oid_len = strlen(schema->prefixes[j].oid);
83 j++;
87 schema->num_prefixes = j;
88 return WERR_OK;
91 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
92 const struct ldb_val *prefixMap,
93 const struct ldb_val *schemaInfo)
95 WERROR status;
96 enum ndr_err_code ndr_err;
97 struct prefixMapBlob pfm;
98 char *schema_info;
100 TALLOC_CTX *mem_ctx = talloc_new(schema);
101 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
103 ndr_err = ndr_pull_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
104 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
105 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
106 talloc_free(mem_ctx);
107 return ntstatus_to_werror(nt_status);
110 if (pfm.version != PREFIX_MAP_VERSION_DSDB) {
111 talloc_free(mem_ctx);
112 return WERR_FOOBAR;
115 if (schemaInfo->length != 21 && schemaInfo->data[0] == 0xFF) {
116 talloc_free(mem_ctx);
117 return WERR_FOOBAR;
120 /* append the schema info as last element */
121 pfm.ctr.dsdb.num_mappings++;
122 pfm.ctr.dsdb.mappings = talloc_realloc(mem_ctx, pfm.ctr.dsdb.mappings,
123 struct drsuapi_DsReplicaOIDMapping,
124 pfm.ctr.dsdb.num_mappings);
125 W_ERROR_HAVE_NO_MEMORY(pfm.ctr.dsdb.mappings);
127 schema_info = data_blob_hex_string(pfm.ctr.dsdb.mappings, schemaInfo);
128 W_ERROR_HAVE_NO_MEMORY(schema_info);
130 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].id_prefix = 0;
131 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.__ndr_size = schemaInfo->length;
132 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.oid = schema_info;
134 /* call the drsuapi version */
135 status = dsdb_load_oid_mappings_drsuapi(schema, &pfm.ctr.dsdb);
136 talloc_free(mem_ctx);
138 W_ERROR_NOT_OK_RETURN(status);
140 return WERR_OK;
143 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
144 bool include_schema_info,
145 TALLOC_CTX *mem_ctx,
146 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
148 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
149 uint32_t i;
151 ctr = talloc(mem_ctx, struct drsuapi_DsReplicaOIDMapping_Ctr);
152 W_ERROR_HAVE_NO_MEMORY(ctr);
154 ctr->num_mappings = schema->num_prefixes;
155 if (include_schema_info) ctr->num_mappings++;
156 ctr->mappings = talloc_array(schema, struct drsuapi_DsReplicaOIDMapping, ctr->num_mappings);
157 W_ERROR_HAVE_NO_MEMORY(ctr->mappings);
159 for (i=0; i < schema->num_prefixes; i++) {
160 ctr->mappings[i].id_prefix = schema->prefixes[i].id>>16;
161 ctr->mappings[i].oid.oid = talloc_strndup(ctr->mappings,
162 schema->prefixes[i].oid,
163 schema->prefixes[i].oid_len - 1);
164 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
167 if (include_schema_info) {
168 ctr->mappings[i].id_prefix = 0;
169 ctr->mappings[i].oid.oid = talloc_strdup(ctr->mappings,
170 schema->schema_info);
171 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
174 *_ctr = ctr;
175 return WERR_OK;
178 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
179 TALLOC_CTX *mem_ctx,
180 struct ldb_val *prefixMap,
181 struct ldb_val *schemaInfo)
183 WERROR status;
184 enum ndr_err_code ndr_err;
185 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
186 struct prefixMapBlob pfm;
188 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
189 W_ERROR_NOT_OK_RETURN(status);
191 pfm.version = PREFIX_MAP_VERSION_DSDB;
192 pfm.reserved = 0;
193 pfm.ctr.dsdb = *ctr;
195 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
196 talloc_free(ctr);
197 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
198 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
199 return ntstatus_to_werror(nt_status);
202 *schemaInfo = strhex_to_data_blob(schema->schema_info);
203 W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
204 talloc_steal(mem_ctx, 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(mem_ctx);
353 return status;
356 WERROR dsdb_prefix_map_update(TALLOC_CTX *mem_ctx, uint32_t *num_prefixes, struct dsdb_schema_oid_prefix **prefixes, const char *oid)
358 uint32_t new_num_prefixes, index_new_prefix, new_entry_id;
359 const char* lastDotOffset;
360 size_t size;
362 new_num_prefixes = *num_prefixes + 1;
363 index_new_prefix = *num_prefixes;
366 * this is the algorithm we use to create new mappings for now
368 * TODO: find what algorithm windows use
370 new_entry_id = (*num_prefixes)<<16;
372 /* Extract the prefix from the oid*/
373 lastDotOffset = strrchr(oid, '.');
374 if (lastDotOffset == NULL) {
375 DEBUG(0,("dsdb_prefix_map_update: failed to find the last dot\n"));
376 return WERR_NOT_FOUND;
379 /* Calculate the size of the remainig string that should be the prefix of it */
380 size = strlen(oid) - strlen(lastDotOffset);
381 if (size <= 0) {
382 DEBUG(0,("dsdb_prefix_map_update: size of the remaining string invalid\n"));
383 return WERR_FOOBAR;
385 /* Add one because we need to copy the dot */
386 size += 1;
388 /* Create a spot in the prefixMap for one more prefix*/
389 (*prefixes) = talloc_realloc(mem_ctx, *prefixes, struct dsdb_schema_oid_prefix, new_num_prefixes);
390 W_ERROR_HAVE_NO_MEMORY(*prefixes);
392 /* Add the new prefix entry*/
393 (*prefixes)[index_new_prefix].id = new_entry_id;
394 (*prefixes)[index_new_prefix].oid = talloc_strndup(mem_ctx, oid, size);
395 (*prefixes)[index_new_prefix].oid_len = strlen((*prefixes)[index_new_prefix].oid);
397 /* Increase num_prefixes because new prefix has been added */
398 ++(*num_prefixes);
400 return WERR_OK;
403 WERROR dsdb_find_prefix_for_oid(uint32_t num_prefixes, const struct dsdb_schema_oid_prefix *prefixes, const char *in, uint32_t *out)
405 uint32_t i;
407 for (i=0; i < num_prefixes; i++) {
408 const char *val_str;
409 char *end_str;
410 unsigned val;
412 if (strncmp(prefixes[i].oid, in, prefixes[i].oid_len) != 0) {
413 continue;
416 val_str = in + prefixes[i].oid_len;
417 end_str = NULL;
418 errno = 0;
420 if (val_str[0] == '\0') {
421 return WERR_INVALID_PARAM;
424 /* two '.' chars are invalid */
425 if (val_str[0] == '.') {
426 return WERR_INVALID_PARAM;
429 val = strtoul(val_str, &end_str, 10);
430 if (end_str[0] == '.' && end_str[1] != '\0') {
432 * if it's a '.' and not the last char
433 * then maybe an other mapping apply
435 continue;
436 } else if (end_str[0] != '\0') {
437 return WERR_INVALID_PARAM;
438 } else if (val > 0xFFFF) {
439 return WERR_INVALID_PARAM;
442 *out = prefixes[i].id | val;
443 return WERR_OK;
446 return WERR_DS_NO_MSDS_INTID;
449 WERROR dsdb_write_prefixes_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
450 uint32_t num_prefixes,
451 const struct dsdb_schema_oid_prefix *prefixes)
453 struct ldb_message msg;
454 struct ldb_dn *schema_dn;
455 struct ldb_message_element el;
456 struct prefixMapBlob pm;
457 struct ldb_val ndr_blob;
458 enum ndr_err_code ndr_err;
459 uint32_t i;
460 int ret;
462 schema_dn = samdb_schema_dn(ldb);
463 if (!schema_dn) {
464 DEBUG(0,("dsdb_write_prefixes_to_ldb: no schema dn present\n"));
465 return WERR_FOOBAR;
468 pm.version = PREFIX_MAP_VERSION_DSDB;
469 pm.ctr.dsdb.num_mappings = num_prefixes;
470 pm.ctr.dsdb.mappings = talloc_array(mem_ctx,
471 struct drsuapi_DsReplicaOIDMapping,
472 pm.ctr.dsdb.num_mappings);
473 if (!pm.ctr.dsdb.mappings) {
474 return WERR_NOMEM;
477 for (i=0; i < num_prefixes; i++) {
478 pm.ctr.dsdb.mappings[i].id_prefix = prefixes[i].id>>16;
479 pm.ctr.dsdb.mappings[i].oid.oid = talloc_strdup(pm.ctr.dsdb.mappings, prefixes[i].oid);
482 ndr_err = ndr_push_struct_blob(&ndr_blob, ldb,
483 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
484 &pm,
485 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
486 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
487 return WERR_FOOBAR;
490 el.num_values = 1;
491 el.values = &ndr_blob;
492 el.flags = LDB_FLAG_MOD_REPLACE;
493 el.name = talloc_strdup(mem_ctx, "prefixMap");
495 msg.dn = ldb_dn_copy(mem_ctx, schema_dn);
496 msg.num_elements = 1;
497 msg.elements = &el;
499 ret = ldb_modify( ldb, &msg );
500 if (ret != 0) {
501 DEBUG(0,("dsdb_write_prefixes_to_ldb: ldb_modify failed\n"));
502 return WERR_FOOBAR;
505 return WERR_OK;
508 WERROR dsdb_read_prefixes_from_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, uint32_t* num_prefixes, struct dsdb_schema_oid_prefix **prefixes)
510 struct prefixMapBlob *blob;
511 enum ndr_err_code ndr_err;
512 uint32_t i;
513 const struct ldb_val *prefix_val;
514 struct ldb_dn *schema_dn;
515 struct ldb_result *schema_res;
516 int ret;
517 static const char *schema_attrs[] = {
518 "prefixMap",
519 NULL
522 schema_dn = samdb_schema_dn(ldb);
523 if (!schema_dn) {
524 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
525 return WERR_FOOBAR;
528 ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
529 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
530 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
531 talloc_free(schema_res);
532 return WERR_FOOBAR;
533 } else if (ret != LDB_SUCCESS) {
534 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
535 talloc_free(schema_res);
536 return WERR_FOOBAR;
539 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
540 if (!prefix_val) {
541 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
542 talloc_free(schema_res);
543 return WERR_FOOBAR;
546 blob = talloc(mem_ctx, struct prefixMapBlob);
547 W_ERROR_HAVE_NO_MEMORY(blob);
549 ndr_err = ndr_pull_struct_blob(prefix_val, blob,
550 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
551 blob,
552 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
553 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
554 DEBUG(0,("dsdb_read_prefixes_from_ldb: ndr_pull_struct_blob failed\n"));
555 talloc_free(blob);
556 talloc_free(schema_res);
557 return WERR_FOOBAR;
560 talloc_free(schema_res);
562 if (blob->version != PREFIX_MAP_VERSION_DSDB) {
563 DEBUG(0,("dsdb_read_prefixes_from_ldb: blob->version incorect\n"));
564 talloc_free(blob);
565 return WERR_FOOBAR;
568 *num_prefixes = blob->ctr.dsdb.num_mappings;
569 *prefixes = talloc_array(mem_ctx, struct dsdb_schema_oid_prefix, *num_prefixes);
570 if(!(*prefixes)) {
571 talloc_free(blob);
572 return WERR_NOMEM;
574 for (i=0; i < blob->ctr.dsdb.num_mappings; i++) {
575 char *oid;
576 (*prefixes)[i].id = blob->ctr.dsdb.mappings[i].id_prefix<<16;
577 oid = talloc_strdup(mem_ctx, blob->ctr.dsdb.mappings[i].oid.oid);
578 (*prefixes)[i].oid = talloc_asprintf_append(oid, ".");
579 (*prefixes)[i].oid_len = strlen(blob->ctr.dsdb.mappings[i].oid.oid);
582 talloc_free(blob);
583 return WERR_OK;
586 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
587 (p)->elem = samdb_result_string(msg, attr, NULL);\
588 if (strict && (p)->elem == NULL) { \
589 d_printf("%s: %s == NULL\n", __location__, attr); \
590 return WERR_INVALID_PARAM; \
592 talloc_steal(mem_ctx, (p)->elem); \
593 } while (0)
595 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
596 int get_string_list_counter; \
597 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
598 if (get_string_list_el == NULL) { \
599 if (strict) { \
600 d_printf("%s: %s == NULL\n", __location__, attr); \
601 return WERR_INVALID_PARAM; \
602 } else { \
603 (p)->elem = NULL; \
604 break; \
607 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
608 for (get_string_list_counter=0; \
609 get_string_list_counter < get_string_list_el->num_values; \
610 get_string_list_counter++) { \
611 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
612 (const char *)get_string_list_el->values[get_string_list_counter].data, \
613 get_string_list_el->values[get_string_list_counter].length); \
614 if (!(p)->elem[get_string_list_counter]) { \
615 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
616 return WERR_NOMEM; \
618 (p)->elem[get_string_list_counter+1] = NULL; \
620 talloc_steal(mem_ctx, (p)->elem); \
621 } while (0)
623 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
624 const char *str; \
625 str = samdb_result_string(msg, attr, NULL);\
626 if (str == NULL) { \
627 if (strict) { \
628 d_printf("%s: %s == NULL\n", __location__, attr); \
629 return WERR_INVALID_PARAM; \
630 } else { \
631 (p)->elem = false; \
633 } else if (strcasecmp("TRUE", str) == 0) { \
634 (p)->elem = true; \
635 } else if (strcasecmp("FALSE", str) == 0) { \
636 (p)->elem = false; \
637 } else { \
638 d_printf("%s: %s == %s\n", __location__, attr, str); \
639 return WERR_INVALID_PARAM; \
641 } while (0)
643 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
644 (p)->elem = samdb_result_uint(msg, attr, 0);\
645 } while (0)
647 #define GET_GUID_LDB(msg, attr, p, elem) do { \
648 (p)->elem = samdb_result_guid(msg, attr);\
649 } while (0)
651 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
652 const struct ldb_val *_val;\
653 _val = ldb_msg_find_ldb_val(msg, attr);\
654 if (_val) {\
655 (p)->elem = *_val;\
656 talloc_steal(mem_ctx, (p)->elem.data);\
657 } else {\
658 ZERO_STRUCT((p)->elem);\
660 } while (0)
662 WERROR dsdb_attribute_from_ldb(const struct dsdb_schema *schema,
663 struct ldb_message *msg,
664 TALLOC_CTX *mem_ctx,
665 struct dsdb_attribute *attr)
667 WERROR status;
669 GET_STRING_LDB(msg, "cn", mem_ctx, attr, cn, false);
670 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
671 GET_STRING_LDB(msg, "attributeID", mem_ctx, attr, attributeID_oid, true);
672 if (schema->num_prefixes == 0) {
673 /* set an invalid value */
674 attr->attributeID_id = 0xFFFFFFFF;
675 } else {
676 status = dsdb_map_oid2int(schema, attr->attributeID_oid, &attr->attributeID_id);
677 if (!W_ERROR_IS_OK(status)) {
678 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
679 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
680 win_errstr(status)));
681 return status;
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_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
690 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
691 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
692 GET_UINT32_LDB(msg, "linkID", attr, linkID);
694 GET_STRING_LDB(msg, "attributeSyntax", mem_ctx, attr, attributeSyntax_oid, true);
695 if (schema->num_prefixes == 0) {
696 /* set an invalid value */
697 attr->attributeSyntax_id = 0xFFFFFFFF;
698 } else {
699 status = dsdb_map_oid2int(schema, attr->attributeSyntax_oid, &attr->attributeSyntax_id);
700 if (!W_ERROR_IS_OK(status)) {
701 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
702 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
703 win_errstr(status)));
704 return status;
707 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
708 GET_BLOB_LDB(msg, "oMObjectClass", mem_ctx, attr, oMObjectClass);
710 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
711 GET_UINT32_LDB(msg, "rangeLower", attr, rangeLower);
712 GET_UINT32_LDB(msg, "rangeUpper", attr, rangeUpper);
713 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
715 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
716 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
718 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
719 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
720 GET_STRING_LDB(msg, "adminDescription", mem_ctx, attr, adminDescription, false);
721 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, attr, classDisplayName, false);
722 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
723 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
724 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
726 attr->syntax = dsdb_syntax_for_attribute(attr);
727 if (!attr->syntax) {
728 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
731 return WERR_OK;
734 WERROR dsdb_class_from_ldb(const struct dsdb_schema *schema,
735 struct ldb_message *msg,
736 TALLOC_CTX *mem_ctx,
737 struct dsdb_class *obj)
739 WERROR status;
741 GET_STRING_LDB(msg, "cn", mem_ctx, obj, cn, false);
742 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
743 GET_STRING_LDB(msg, "governsID", mem_ctx, obj, governsID_oid, true);
744 if (schema->num_prefixes == 0) {
745 /* set an invalid value */
746 obj->governsID_id = 0xFFFFFFFF;
747 } else {
748 status = dsdb_map_oid2int(schema, obj->governsID_oid, &obj->governsID_id);
749 if (!W_ERROR_IS_OK(status)) {
750 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
751 __location__, obj->lDAPDisplayName, obj->governsID_oid,
752 win_errstr(status)));
753 return status;
756 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
758 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
759 GET_STRING_LDB(msg, "rDNAttID", mem_ctx, obj, rDNAttID, false);
760 GET_STRING_LDB(msg, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
762 GET_STRING_LDB(msg, "subClassOf", mem_ctx, obj, subClassOf, true);
764 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass, false);
765 GET_STRING_LIST_LDB(msg, "auxiliaryClass", mem_ctx, obj, auxiliaryClass, false);
767 GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, false);
768 GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, false);
769 GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, false);
770 GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, false);
772 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
773 GET_STRING_LIST_LDB(msg, "possSuperiors", mem_ctx, obj, possSuperiors, false);
774 GET_STRING_LIST_LDB(msg, "possibleInferiors", mem_ctx, obj, possibleInferiors, false);
776 GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
778 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
779 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
781 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
782 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
783 GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, false);
784 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, false);
785 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
786 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
787 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
789 return WERR_OK;
792 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
794 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
795 struct smb_iconv_convenience *iconv_convenience,
796 struct ldb_result *schema_res,
797 struct ldb_result *attrs_res, struct ldb_result *objectclass_res,
798 struct dsdb_schema **schema_out,
799 char **error_string)
801 WERROR status;
802 uint32_t i;
803 const struct ldb_val *prefix_val;
804 const struct ldb_val *info_val;
805 struct ldb_val info_val_default;
806 struct dsdb_schema *schema;
808 schema = dsdb_new_schema(mem_ctx, iconv_convenience);
809 if (!schema) {
810 dsdb_oom(error_string, mem_ctx);
811 return LDB_ERR_OPERATIONS_ERROR;
814 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
815 if (!prefix_val) {
816 *error_string = talloc_asprintf(mem_ctx,
817 "schema_fsmo_init: no prefixMap attribute found");
818 return LDB_ERR_CONSTRAINT_VIOLATION;
820 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
821 if (!info_val) {
822 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
823 if (!info_val_default.data) {
824 dsdb_oom(error_string, mem_ctx);
825 return LDB_ERR_OPERATIONS_ERROR;
827 talloc_steal(mem_ctx, info_val_default.data);
828 info_val = &info_val_default;
831 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
832 if (!W_ERROR_IS_OK(status)) {
833 *error_string = talloc_asprintf(mem_ctx,
834 "schema_fsmo_init: failed to load oid mappings: %s",
835 win_errstr(status));
836 return LDB_ERR_CONSTRAINT_VIOLATION;
839 for (i=0; i < attrs_res->count; i++) {
840 struct dsdb_attribute *sa;
842 sa = talloc_zero(schema, struct dsdb_attribute);
843 if (!sa) {
844 dsdb_oom(error_string, mem_ctx);
845 return LDB_ERR_OPERATIONS_ERROR;
848 status = dsdb_attribute_from_ldb(schema, attrs_res->msgs[i], sa, sa);
849 if (!W_ERROR_IS_OK(status)) {
850 *error_string = talloc_asprintf(mem_ctx,
851 "schema_fsmo_init: failed to load attribute definition: %s:%s",
852 ldb_dn_get_linearized(attrs_res->msgs[i]->dn),
853 win_errstr(status));
854 return LDB_ERR_CONSTRAINT_VIOLATION;
857 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
860 for (i=0; i < objectclass_res->count; i++) {
861 struct dsdb_class *sc;
863 sc = talloc_zero(schema, struct dsdb_class);
864 if (!sc) {
865 dsdb_oom(error_string, mem_ctx);
866 return LDB_ERR_OPERATIONS_ERROR;
869 status = dsdb_class_from_ldb(schema, objectclass_res->msgs[i], sc, sc);
870 if (!W_ERROR_IS_OK(status)) {
871 *error_string = talloc_asprintf(mem_ctx,
872 "schema_fsmo_init: failed to load class definition: %s:%s",
873 ldb_dn_get_linearized(objectclass_res->msgs[i]->dn),
874 win_errstr(status));
875 return LDB_ERR_CONSTRAINT_VIOLATION;
878 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
881 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
882 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
883 schema->fsmo.we_are_master = true;
884 } else {
885 schema->fsmo.we_are_master = false;
888 DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
889 (schema->fsmo.we_are_master?"yes":"no")));
891 *schema_out = schema;
892 return LDB_SUCCESS;
895 /* This recursive load of the objectClasses presumes that they
896 * everything is in a strict subClassOf hirarchy.
898 * We load this in order so we produce certain outputs (such as the
899 * exported schema for openldap, and sorted objectClass attribute) 'in
900 * order' */
902 static int fetch_oc_recursive(struct ldb_context *ldb, struct ldb_dn *schemadn,
903 TALLOC_CTX *mem_ctx,
904 struct ldb_result *search_from,
905 struct ldb_result *res_list)
907 int i;
908 int ret = 0;
909 for (i=0; i < search_from->count; i++) {
910 struct ldb_result *res;
911 const char *name = ldb_msg_find_attr_as_string(search_from->msgs[i],
912 "lDAPDisplayname", NULL);
914 ret = ldb_search(ldb, mem_ctx, &res,
915 schemadn, LDB_SCOPE_SUBTREE, NULL,
916 "(&(&(objectClass=classSchema)(subClassOf=%s))(!(lDAPDisplayName=%s)))",
917 name, name);
918 if (ret != LDB_SUCCESS) {
919 return ret;
922 res_list->msgs = talloc_realloc(res_list, res_list->msgs,
923 struct ldb_message *, res_list->count + 2);
924 if (!res_list->msgs) {
925 return LDB_ERR_OPERATIONS_ERROR;
927 res_list->msgs[res_list->count] = talloc_move(res_list,
928 &search_from->msgs[i]);
929 res_list->count++;
930 res_list->msgs[res_list->count] = NULL;
932 if (res->count > 0) {
933 ret = fetch_oc_recursive(ldb, schemadn, mem_ctx, res, res_list);
935 if (ret != LDB_SUCCESS) {
936 return ret;
939 return ret;
942 static int fetch_objectclass_schema(struct ldb_context *ldb, struct ldb_dn *schemadn,
943 TALLOC_CTX *mem_ctx,
944 struct ldb_result **objectclasses_res,
945 char **error_string)
947 TALLOC_CTX *local_ctx = talloc_new(mem_ctx);
948 struct ldb_result *top_res, *ret_res;
949 int ret;
950 if (!local_ctx) {
951 return LDB_ERR_OPERATIONS_ERROR;
954 /* Download 'top' */
955 ret = ldb_search(ldb, local_ctx, &top_res,
956 schemadn, LDB_SCOPE_SUBTREE, NULL,
957 "(&(objectClass=classSchema)(lDAPDisplayName=top))");
958 if (ret != LDB_SUCCESS) {
959 *error_string = talloc_asprintf(mem_ctx,
960 "dsdb_schema: failed to search for top classSchema object: %s",
961 ldb_errstring(ldb));
962 return ret;
965 if (top_res->count != 1) {
966 *error_string = talloc_asprintf(mem_ctx,
967 "dsdb_schema: failed to find top classSchema object");
968 return LDB_ERR_NO_SUCH_OBJECT;
971 ret_res = talloc_zero(local_ctx, struct ldb_result);
972 if (!ret_res) {
973 return LDB_ERR_OPERATIONS_ERROR;
976 ret = fetch_oc_recursive(ldb, schemadn, local_ctx, top_res, ret_res);
978 if (ret != LDB_SUCCESS) {
979 return ret;
982 *objectclasses_res = talloc_move(mem_ctx, &ret_res);
983 return ret;
986 int dsdb_schema_from_schema_dn(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
987 struct smb_iconv_convenience *iconv_convenience,
988 struct ldb_dn *schema_dn,
989 struct dsdb_schema **schema,
990 char **error_string_out)
992 TALLOC_CTX *tmp_ctx;
993 char *error_string;
994 int ret;
996 struct ldb_result *schema_res;
997 struct ldb_result *a_res;
998 struct ldb_result *c_res;
999 static const char *schema_attrs[] = {
1000 "prefixMap",
1001 "schemaInfo",
1002 "fSMORoleOwner",
1003 NULL
1006 tmp_ctx = talloc_new(mem_ctx);
1007 if (!tmp_ctx) {
1008 dsdb_oom(error_string_out, mem_ctx);
1009 return LDB_ERR_OPERATIONS_ERROR;
1013 * setup the prefix mappings and schema info
1015 ret = ldb_search(ldb, tmp_ctx, &schema_res,
1016 schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
1017 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1018 talloc_free(tmp_ctx);
1019 return ret;
1020 } else if (ret != LDB_SUCCESS) {
1021 *error_string_out = talloc_asprintf(mem_ctx,
1022 "dsdb_schema: failed to search the schema head: %s",
1023 ldb_errstring(ldb));
1024 talloc_free(tmp_ctx);
1025 return ret;
1027 if (schema_res->count != 1) {
1028 *error_string_out = talloc_asprintf(mem_ctx,
1029 "dsdb_schema: [%u] schema heads found on a base search",
1030 schema_res->count);
1031 talloc_free(tmp_ctx);
1032 return LDB_ERR_CONSTRAINT_VIOLATION;
1036 * load the attribute definitions
1038 ret = ldb_search(ldb, tmp_ctx, &a_res,
1039 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
1040 "(objectClass=attributeSchema)");
1041 if (ret != LDB_SUCCESS) {
1042 *error_string_out = talloc_asprintf(mem_ctx,
1043 "dsdb_schema: failed to search attributeSchema objects: %s",
1044 ldb_errstring(ldb));
1045 talloc_free(tmp_ctx);
1046 return ret;
1050 * load the objectClass definitions
1052 ret = fetch_objectclass_schema(ldb, schema_dn, tmp_ctx, &c_res, &error_string);
1053 if (ret != LDB_SUCCESS) {
1054 *error_string_out = talloc_asprintf(mem_ctx,
1055 "Failed to fetch objectClass schema elements: %s", error_string);
1056 talloc_free(tmp_ctx);
1057 return ret;
1060 ret = dsdb_schema_from_ldb_results(tmp_ctx, ldb,
1061 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
1062 schema_res, a_res, c_res, schema, &error_string);
1063 if (ret != LDB_SUCCESS) {
1064 *error_string_out = talloc_asprintf(mem_ctx,
1065 "dsdb_schema load failed: %s",
1066 error_string);
1067 talloc_free(tmp_ctx);
1068 return ret;
1070 talloc_steal(mem_ctx, *schema);
1071 talloc_free(tmp_ctx);
1073 return LDB_SUCCESS;
1077 static const struct {
1078 const char *name;
1079 const char *oid;
1080 } name_mappings[] = {
1081 { "cn", "2.5.4.3" },
1082 { "name", "1.2.840.113556.1.4.1" },
1083 { "lDAPDisplayName", "1.2.840.113556.1.2.460" },
1084 { "attributeID", "1.2.840.113556.1.2.30" },
1085 { "schemaIDGUID", "1.2.840.113556.1.4.148" },
1086 { "mAPIID", "1.2.840.113556.1.2.49" },
1087 { "attributeSecurityGUID", "1.2.840.113556.1.4.149" },
1088 { "searchFlags", "1.2.840.113556.1.2.334" },
1089 { "systemFlags", "1.2.840.113556.1.4.375" },
1090 { "isMemberOfPartialAttributeSet", "1.2.840.113556.1.4.639" },
1091 { "linkID", "1.2.840.113556.1.2.50" },
1092 { "attributeSyntax", "1.2.840.113556.1.2.32" },
1093 { "oMSyntax", "1.2.840.113556.1.2.231" },
1094 { "oMObjectClass", "1.2.840.113556.1.2.218" },
1095 { "isSingleValued", "1.2.840.113556.1.2.33" },
1096 { "rangeLower", "1.2.840.113556.1.2.34" },
1097 { "rangeUpper", "1.2.840.113556.1.2.35" },
1098 { "extendedCharsAllowed", "1.2.840.113556.1.2.380" },
1099 { "schemaFlagsEx", "1.2.840.113556.1.4.120" },
1100 { "msDs-Schema-Extensions", "1.2.840.113556.1.4.1440" },
1101 { "showInAdvancedViewOnly", "1.2.840.113556.1.2.169" },
1102 { "adminDisplayName", "1.2.840.113556.1.2.194" },
1103 { "adminDescription", "1.2.840.113556.1.2.226" },
1104 { "classDisplayName", "1.2.840.113556.1.4.610" },
1105 { "isEphemeral", "1.2.840.113556.1.4.1212" },
1106 { "isDefunct", "1.2.840.113556.1.4.661" },
1107 { "systemOnly", "1.2.840.113556.1.4.170" },
1108 { "governsID", "1.2.840.113556.1.2.22" },
1109 { "objectClassCategory", "1.2.840.113556.1.2.370" },
1110 { "rDNAttID", "1.2.840.113556.1.2.26" },
1111 { "defaultObjectCategory", "1.2.840.113556.1.4.783" },
1112 { "subClassOf", "1.2.840.113556.1.2.21" },
1113 { "systemAuxiliaryClass", "1.2.840.113556.1.4.198" },
1114 { "systemPossSuperiors", "1.2.840.113556.1.4.195" },
1115 { "systemMustContain", "1.2.840.113556.1.4.197" },
1116 { "systemMayContain", "1.2.840.113556.1.4.196" },
1117 { "auxiliaryClass", "1.2.840.113556.1.2.351" },
1118 { "possSuperiors", "1.2.840.113556.1.2.8" },
1119 { "mustContain", "1.2.840.113556.1.2.24" },
1120 { "mayContain", "1.2.840.113556.1.2.25" },
1121 { "defaultSecurityDescriptor", "1.2.840.113556.1.4.224" },
1122 { "defaultHidingValue", "1.2.840.113556.1.4.518" },
1125 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
1126 struct drsuapi_DsReplicaObject *obj,
1127 const char *name,
1128 uint32_t *idx)
1130 WERROR status;
1131 uint32_t i, id;
1132 const char *oid = NULL;
1134 for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
1135 if (strcmp(name_mappings[i].name, name) != 0) continue;
1137 oid = name_mappings[i].oid;
1138 break;
1141 if (!oid) {
1142 return NULL;
1145 status = dsdb_map_oid2int(schema, oid, &id);
1146 if (!W_ERROR_IS_OK(status)) {
1147 return NULL;
1150 for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
1151 if (obj->attribute_ctr.attributes[i].attid != id) continue;
1153 if (idx) *idx = i;
1154 return &obj->attribute_ctr.attributes[i];
1157 return NULL;
1160 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1161 struct drsuapi_DsReplicaAttribute *_a; \
1162 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1163 if (strict && !_a) { \
1164 d_printf("%s: %s == NULL\n", __location__, attr); \
1165 return WERR_INVALID_PARAM; \
1167 if (strict && _a->value_ctr.num_values != 1) { \
1168 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1169 _a->value_ctr.num_values); \
1170 return WERR_INVALID_PARAM; \
1172 if (_a && _a->value_ctr.num_values >= 1) { \
1173 ssize_t _ret; \
1174 _ret = convert_string_talloc(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
1175 _a->value_ctr.values[0].blob->data, \
1176 _a->value_ctr.values[0].blob->length, \
1177 (void **)discard_const(&(p)->elem)); \
1178 if (_ret == -1) { \
1179 DEBUG(0,("%s: invalid data!\n", attr)); \
1180 dump_data(0, \
1181 _a->value_ctr.values[0].blob->data, \
1182 _a->value_ctr.values[0].blob->length); \
1183 return WERR_FOOBAR; \
1185 } else { \
1186 (p)->elem = NULL; \
1188 } while (0)
1190 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1191 struct drsuapi_DsReplicaAttribute *_a; \
1192 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1193 if (strict && !_a) { \
1194 d_printf("%s: %s == NULL\n", __location__, attr); \
1195 return WERR_INVALID_PARAM; \
1197 if (strict && _a->value_ctr.num_values != 1) { \
1198 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1199 _a->value_ctr.num_values); \
1200 return WERR_INVALID_PARAM; \
1202 if (strict && !_a->value_ctr.values[0].blob) { \
1203 d_printf("%s: %s data == NULL\n", __location__, attr); \
1204 return WERR_INVALID_PARAM; \
1206 if (_a && _a->value_ctr.num_values >= 1 \
1207 && _a->value_ctr.values[0].blob) { \
1208 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
1209 enum ndr_err_code _ndr_err; \
1210 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1211 mem_ctx, s->iconv_convenience, &_id3,\
1212 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
1213 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1214 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1215 return ntstatus_to_werror(_nt_status); \
1217 (p)->elem = _id3.dn; \
1218 } else { \
1219 (p)->elem = NULL; \
1221 } while (0)
1223 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
1224 struct drsuapi_DsReplicaAttribute *_a; \
1225 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1226 if (strict && !_a) { \
1227 d_printf("%s: %s == NULL\n", __location__, attr); \
1228 return WERR_INVALID_PARAM; \
1230 if (strict && _a->value_ctr.num_values != 1) { \
1231 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1232 (unsigned int)_a->value_ctr.num_values); \
1233 return WERR_INVALID_PARAM; \
1235 if (strict && !_a->value_ctr.values[0].blob) { \
1236 d_printf("%s: %s data == NULL\n", __location__, attr); \
1237 return WERR_INVALID_PARAM; \
1239 if (strict && _a->value_ctr.values[0].blob->length != 4) { \
1240 d_printf("%s: %s length == %u\n", __location__, attr, \
1241 (unsigned int)_a->value_ctr.values[0].blob->length); \
1242 return WERR_INVALID_PARAM; \
1244 if (_a && _a->value_ctr.num_values >= 1 \
1245 && _a->value_ctr.values[0].blob \
1246 && _a->value_ctr.values[0].blob->length == 4) { \
1247 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?true:false);\
1248 } else { \
1249 (p)->elem = false; \
1251 } while (0)
1253 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
1254 struct drsuapi_DsReplicaAttribute *_a; \
1255 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1256 if (_a && _a->value_ctr.num_values >= 1 \
1257 && _a->value_ctr.values[0].blob \
1258 && _a->value_ctr.values[0].blob->length == 4) { \
1259 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1260 } else { \
1261 (p)->elem = 0; \
1263 } while (0)
1265 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
1266 struct drsuapi_DsReplicaAttribute *_a; \
1267 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1268 if (_a && _a->value_ctr.num_values >= 1 \
1269 && _a->value_ctr.values[0].blob \
1270 && _a->value_ctr.values[0].blob->length == 16) { \
1271 enum ndr_err_code _ndr_err; \
1272 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1273 mem_ctx, s->iconv_convenience, &(p)->elem, \
1274 (ndr_pull_flags_fn_t)ndr_pull_GUID); \
1275 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1276 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1277 return ntstatus_to_werror(_nt_status); \
1279 } else { \
1280 ZERO_STRUCT((p)->elem);\
1282 } while (0)
1284 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
1285 struct drsuapi_DsReplicaAttribute *_a; \
1286 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1287 if (_a && _a->value_ctr.num_values >= 1 \
1288 && _a->value_ctr.values[0].blob) { \
1289 (p)->elem = *_a->value_ctr.values[0].blob;\
1290 talloc_steal(mem_ctx, (p)->elem.data); \
1291 } else { \
1292 ZERO_STRUCT((p)->elem);\
1294 } while (0)
1296 WERROR dsdb_attribute_from_drsuapi(struct dsdb_schema *schema,
1297 struct drsuapi_DsReplicaObject *r,
1298 TALLOC_CTX *mem_ctx,
1299 struct dsdb_attribute *attr)
1301 WERROR status;
1303 GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, true);
1304 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
1305 GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
1306 status = dsdb_map_int2oid(schema, attr->attributeID_id, mem_ctx, &attr->attributeID_oid);
1307 if (!W_ERROR_IS_OK(status)) {
1308 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
1309 __location__, attr->lDAPDisplayName, attr->attributeID_id,
1310 win_errstr(status)));
1311 return status;
1313 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
1314 GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
1316 GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
1318 GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
1319 GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
1320 GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
1321 GET_UINT32_DS(schema, r, "linkID", attr, linkID);
1323 GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
1324 status = dsdb_map_int2oid(schema, attr->attributeSyntax_id, mem_ctx, &attr->attributeSyntax_oid);
1325 if (!W_ERROR_IS_OK(status)) {
1326 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
1327 __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
1328 win_errstr(status)));
1329 return status;
1331 GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
1332 GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
1334 GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, true);
1335 GET_UINT32_DS(schema, r, "rangeLower", attr, rangeLower);
1336 GET_UINT32_DS(schema, r, "rangeUpper", attr, rangeUpper);
1337 GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
1339 GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
1340 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
1342 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
1343 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
1344 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, false);
1345 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, false);
1346 GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, false);
1347 GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, false);
1348 GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, false);
1350 attr->syntax = dsdb_syntax_for_attribute(attr);
1351 if (!attr->syntax) {
1352 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1355 return WERR_OK;
1358 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
1359 struct drsuapi_DsReplicaObject *r,
1360 TALLOC_CTX *mem_ctx,
1361 struct dsdb_class *obj)
1363 WERROR status;
1365 GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, true);
1366 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
1367 GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
1368 status = dsdb_map_int2oid(schema, obj->governsID_id, mem_ctx, &obj->governsID_oid);
1369 if (!W_ERROR_IS_OK(status)) {
1370 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
1371 __location__, obj->lDAPDisplayName, obj->governsID_id,
1372 win_errstr(status)));
1373 return status;
1375 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
1377 GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
1378 GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
1379 GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
1381 GET_STRING_DS(schema, r, "subClassOf", mem_ctx, obj, subClassOf, true);
1383 obj->systemAuxiliaryClass = NULL;
1384 obj->systemPossSuperiors = NULL;
1385 obj->systemMustContain = NULL;
1386 obj->systemMayContain = NULL;
1388 obj->auxiliaryClass = NULL;
1389 obj->possSuperiors = NULL;
1390 obj->mustContain = NULL;
1391 obj->mayContain = NULL;
1393 obj->possibleInferiors = NULL;
1395 GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
1397 GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
1398 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
1400 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
1401 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
1402 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, false);
1403 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, false);
1404 GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, false);
1405 GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, false);
1406 GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, false);
1408 return WERR_OK;