s4/drs: dsdb_read_prefixes_from_ldb() refactored
[ira/wip.git] / source4 / dsdb / schema / schema_init.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    DSDB schema header
4    
5    Copyright (C) Stefan Metzmacher <metze@samba.org> 2006-2007
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2008
7
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.
12    
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.
17    
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/>.
20    
21 */
22
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"
32 #include "../lib/util/asn1.h"
33
34
35 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
36 {
37         struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
38         if (!schema) {
39                 return NULL;
40         }
41
42         schema->iconv_convenience = iconv_convenience;
43         return schema;
44 }
45
46
47 WERROR dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema *schema,
48                                         const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
49 {
50         WERROR werr;
51         const char *schema_info;
52         struct dsdb_schema_prefixmap *pfm;
53
54         werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, schema, &pfm, &schema_info);
55         W_ERROR_NOT_OK_RETURN(werr);
56
57         /* set loaded prefixMap */
58         talloc_free(schema->prefixmap);
59         schema->prefixmap = pfm;
60
61         talloc_free(discard_const(schema->schema_info));
62         schema->schema_info = schema_info;
63
64         return WERR_OK;
65 }
66
67 static WERROR _dsdb_prefixmap_from_ldb_val(const struct ldb_val *pfm_ldb_val,
68                                            struct smb_iconv_convenience *iconv_convenience,
69                                            TALLOC_CTX *mem_ctx,
70                                            struct dsdb_schema_prefixmap **_pfm)
71 {
72         WERROR werr;
73         enum ndr_err_code ndr_err;
74         struct prefixMapBlob pfm_blob;
75
76         TALLOC_CTX *temp_ctx = talloc_new(mem_ctx);
77         W_ERROR_HAVE_NO_MEMORY(temp_ctx);
78
79         ndr_err = ndr_pull_struct_blob(pfm_ldb_val, temp_ctx,
80                                 iconv_convenience, &pfm_blob,
81                                 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
82         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
83                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
84                 talloc_free(temp_ctx);
85                 return ntstatus_to_werror(nt_status);
86         }
87
88         if (pfm_blob.version != PREFIX_MAP_VERSION_DSDB) {
89                 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version incorrect\n"));
90                 talloc_free(temp_ctx);
91                 return WERR_VERSION_PARSE_ERROR;
92         }
93
94         /* call the drsuapi version */
95         werr = dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob.ctr.dsdb, false, mem_ctx, _pfm, NULL);
96
97         talloc_free(temp_ctx);
98
99         return werr;
100 }
101
102 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
103                                   const struct ldb_val *prefixMap,
104                                   const struct ldb_val *schemaInfo)
105 {
106         WERROR status;
107         const char *schema_info;
108         struct dsdb_schema_prefixmap *pfm;
109         TALLOC_CTX *mem_ctx;
110
111         /* verify input params */
112         if (schemaInfo->length != 21) {
113                 return WERR_INVALID_PARAMETER;
114         }
115         if (schemaInfo->data[0] != 0xFF) {
116                 return WERR_INVALID_PARAMETER;
117         }
118
119         mem_ctx = talloc_new(schema);
120         W_ERROR_HAVE_NO_MEMORY(mem_ctx);
121
122         /* fetch prefixMap */
123         status = _dsdb_prefixmap_from_ldb_val(prefixMap,
124                                               schema->iconv_convenience,
125                                               mem_ctx, &pfm);
126         W_ERROR_NOT_OK_RETURN(status);
127
128         /* decode schema_info */
129         schema_info = hex_encode_talloc(mem_ctx,
130                                         schemaInfo->data,
131                                         schemaInfo->length);
132         if (!schema_info) {
133                 talloc_free(mem_ctx);
134                 return WERR_NOMEM;
135         }
136
137         /* store prefixMap and schema_info into cached Schema */
138         talloc_free(schema->prefixmap);
139         schema->prefixmap = talloc_steal(schema, pfm);
140
141         talloc_free(discard_const(schema->schema_info));
142         schema->schema_info = talloc_steal(schema, schema_info);
143
144         /* clean up locally allocated mem */
145         talloc_free(mem_ctx);
146
147         return WERR_OK;
148 }
149
150 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
151                                      bool include_schema_info,
152                                      TALLOC_CTX *mem_ctx,
153                                      struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
154 {
155         return dsdb_drsuapi_pfm_from_schema_pfm(schema->prefixmap,
156                                                 include_schema_info ? schema->schema_info : NULL,
157                                                 mem_ctx, _ctr);
158 }
159
160 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
161                                  TALLOC_CTX *mem_ctx,
162                                  struct ldb_val *prefixMap,
163                                  struct ldb_val *schemaInfo)
164 {
165         WERROR status;
166         enum ndr_err_code ndr_err;
167         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
168         struct prefixMapBlob pfm;
169
170         status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
171         W_ERROR_NOT_OK_RETURN(status);
172
173         pfm.version     = PREFIX_MAP_VERSION_DSDB;
174         pfm.reserved    = 0;
175         pfm.ctr.dsdb    = *ctr;
176
177         ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
178         talloc_free(ctr);
179         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
180                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
181                 return ntstatus_to_werror(nt_status);
182         }
183
184         *schemaInfo = strhex_to_data_blob(mem_ctx, schema->schema_info);
185         W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
186
187         return WERR_OK;
188 }
189
190
191 /*
192  * this function is called from within a ldb transaction from the schema_fsmo module
193  */
194 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
195 {
196         WERROR status;
197         uint32_t num_prefixes;
198         struct dsdb_schema_oid_prefix *prefixes;
199         TALLOC_CTX *mem_ctx;
200         uint32_t out;
201
202         mem_ctx = talloc_new(ldb);
203         W_ERROR_HAVE_NO_MEMORY(mem_ctx);
204
205         /* Read prefixes from disk*/
206         status = dsdb_read_prefixes_from_ldb( mem_ctx, ldb, &num_prefixes, &prefixes ); 
207         if (!W_ERROR_IS_OK(status)) {
208                 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
209                         win_errstr(status)));
210                 talloc_free(mem_ctx);
211                 return status;
212         }
213
214         /* Check if there is a prefix for the oid in the prefixes array*/
215         status = dsdb_find_prefix_for_oid( num_prefixes, prefixes, full_oid, &out ); 
216         if (W_ERROR_IS_OK(status)) {
217                 /* prefix found*/
218                 talloc_free(mem_ctx);
219                 return status;
220         } else if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
221                 /* error */
222                 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
223                         win_errstr(status)));
224                 talloc_free(mem_ctx);
225                 return status;
226         }
227
228         /* Create the new mapping for the prefix of full_oid */
229         status = dsdb_prefix_map_update(mem_ctx, &num_prefixes, &prefixes, full_oid);
230         if (!W_ERROR_IS_OK(status)) {
231                 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_prefix_map_update: %s\n",
232                         win_errstr(status)));
233                 talloc_free(mem_ctx);
234                 return status;
235         }
236
237         talloc_free(schema->prefixes);
238         schema->prefixes = talloc_steal(schema, prefixes);
239         schema->num_prefixes = num_prefixes;
240
241         /* Update prefixMap in ldb*/
242         status = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, ldb, schema);
243         if (!W_ERROR_IS_OK(status)) {
244                 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
245                         win_errstr(status)));
246                 talloc_free(mem_ctx);
247                 return status;
248         }
249
250         DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
251                  full_oid, num_prefixes));
252
253         talloc_free(mem_ctx);
254         return status;
255 }
256
257 WERROR dsdb_prefix_map_update(TALLOC_CTX *mem_ctx, uint32_t *num_prefixes, struct dsdb_schema_oid_prefix **prefixes, const char *oid)
258 {
259         uint32_t new_num_prefixes, index_new_prefix, new_entry_id;
260         const char* lastDotOffset;
261         size_t size;
262         
263         new_num_prefixes = *num_prefixes + 1;
264         index_new_prefix = *num_prefixes;
265
266         /*
267          * this is the algorithm we use to create new mappings for now
268          *
269          * TODO: find what algorithm windows use
270          */
271         new_entry_id = (*num_prefixes)<<16;
272
273         /* Extract the prefix from the oid*/
274         lastDotOffset = strrchr(oid, '.');
275         if (lastDotOffset == NULL) {
276                 DEBUG(0,("dsdb_prefix_map_update: failed to find the last dot\n"));
277                 return WERR_NOT_FOUND;
278         }
279
280         /* Calculate the size of the remainig string that should be the prefix of it */
281         size = strlen(oid) - strlen(lastDotOffset);
282         if (size <= 0) {
283                 DEBUG(0,("dsdb_prefix_map_update: size of the remaining string invalid\n"));
284                 return WERR_FOOBAR;
285         }
286
287         /* Create a spot in the prefixMap for one more prefix*/
288         (*prefixes) = talloc_realloc(mem_ctx, *prefixes, struct dsdb_schema_oid_prefix, new_num_prefixes);
289         W_ERROR_HAVE_NO_MEMORY(*prefixes);
290
291         /* Add the new prefix entry*/
292         (*prefixes)[index_new_prefix].id = new_entry_id;
293         (*prefixes)[index_new_prefix].oid = talloc_strndup(mem_ctx, oid, size);
294         (*prefixes)[index_new_prefix].oid_len = strlen((*prefixes)[index_new_prefix].oid);
295
296         /* Increase num_prefixes because new prefix has been added */
297         ++(*num_prefixes);
298
299         return WERR_OK;
300 }
301
302 WERROR dsdb_find_prefix_for_oid(uint32_t num_prefixes, const struct dsdb_schema_oid_prefix *prefixes, const char *in, uint32_t *out)
303 {
304         uint32_t i;
305         char *oid_prefix;
306         char *pstr;
307         char *end_str;
308         unsigned val;
309
310         /* make oid prefix, i.e. oid w/o last subidentifier */
311         pstr = strrchr(in, '.');
312         if (!pstr)      return WERR_INVALID_PARAM;
313         if (pstr < in)  return WERR_INVALID_PARAM;
314         if ((pstr - in) < 4) return WERR_INVALID_PARAM;
315
316         oid_prefix = talloc_strndup(0, in, pstr - in);
317
318         for (i=0; i < num_prefixes; i++) {
319                 if (strcmp(prefixes[i].oid, oid_prefix) == 0) {
320                         break;
321                 }
322         }
323
324         talloc_free(oid_prefix);
325
326         if (i < num_prefixes) {
327                 /* move next to '.' char */
328                 pstr++;
329
330                 val = strtoul(pstr, &end_str, 10);
331                 if (end_str[0] != '\0') {
332                         return WERR_INVALID_PARAM;
333                 } else if (val > 0xFFFF) {
334                         return WERR_INVALID_PARAM;
335                 }
336
337                 *out = prefixes[i].id | val;
338                 return WERR_OK;
339         }
340
341         DEBUG(5,(__location__ " Failed to find oid %s - have %u prefixes\n", in, num_prefixes));
342
343         return WERR_DS_NO_MSDS_INTID;
344 }
345
346 WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
347                                                      const struct dsdb_schema *schema)
348 {
349         struct ldb_message *msg = ldb_msg_new(mem_ctx);
350         struct ldb_dn *schema_dn;
351         struct prefixMapBlob pm;
352         struct ldb_val ndr_blob;
353         enum ndr_err_code ndr_err;
354         uint32_t i;
355         int ret;
356
357         if (!msg) {
358                 return WERR_NOMEM;
359         }
360         
361         schema_dn = samdb_schema_dn(ldb);
362         if (!schema_dn) {
363                 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));    
364                 return WERR_FOOBAR;
365         }
366
367         pm.version                      = PREFIX_MAP_VERSION_DSDB;
368         pm.ctr.dsdb.num_mappings        = schema->num_prefixes;
369         pm.ctr.dsdb.mappings            = talloc_array(msg,
370                                                 struct drsuapi_DsReplicaOIDMapping,
371                                                 pm.ctr.dsdb.num_mappings);
372         if (!pm.ctr.dsdb.mappings) {
373                 talloc_free(msg);
374                 return WERR_NOMEM;
375         }
376
377         for (i=0; i < schema->num_prefixes; i++) {
378                 DATA_BLOB oid_blob;
379
380                 if (!ber_write_partial_OID_String(pm.ctr.dsdb.mappings, &oid_blob, schema->prefixes[i].oid)) {
381                         DEBUG(0, ("write_partial_OID failed for %s", schema->prefixes[i].oid));
382                         return WERR_INTERNAL_ERROR;
383                 }
384
385                 pm.ctr.dsdb.mappings[i].id_prefix       = schema->prefixes[i].id>>16;
386                 pm.ctr.dsdb.mappings[i].oid.length      = oid_blob.length;
387                 pm.ctr.dsdb.mappings[i].oid.binary_oid  = oid_blob.data;
388         }
389
390         ndr_err = ndr_push_struct_blob(&ndr_blob, msg,
391                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
392                                        &pm,
393                                        (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
394         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
395                 talloc_free(msg);
396                 return WERR_FOOBAR;
397         }
398  
399         msg->dn = schema_dn;
400         ret = ldb_msg_add_value(msg, "prefixMap", &ndr_blob, NULL);
401         if (ret != 0) {
402                 talloc_free(msg);
403                 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));        
404                 return WERR_NOMEM;
405         }
406  
407         ret = samdb_replace( ldb, msg, msg );
408         talloc_free(msg);
409
410         if (ret != 0) {
411                 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: samdb_replace failed\n"));    
412                 return WERR_FOOBAR;
413         }
414  
415         return WERR_OK;
416 }
417
418 WERROR dsdb_read_prefixes_from_ldb(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct dsdb_schema_prefixmap **_pfm)
419 {
420         WERROR werr;
421         int ldb_ret;
422         const struct ldb_val *prefix_val;
423         struct smb_iconv_convenience *iconv_convenience;
424         struct ldb_dn *schema_dn;
425         struct ldb_result *schema_res = NULL;
426         static const char *schema_attrs[] = {
427                 "prefixMap",
428                 NULL
429         };
430
431         schema_dn = samdb_schema_dn(ldb);
432         if (!schema_dn) {
433                 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
434                 return WERR_FOOBAR;
435         }
436
437         ldb_ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
438         if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
439                 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
440                 talloc_free(schema_res);
441                 return WERR_FOOBAR;
442         } else if (ldb_ret != LDB_SUCCESS) {
443                 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
444                 talloc_free(schema_res);
445                 return WERR_FOOBAR;
446         }
447
448         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
449         if (!prefix_val) {
450                 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
451                 talloc_free(schema_res);
452                 return WERR_FOOBAR;
453         }
454
455         iconv_convenience = lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm"));
456
457         werr = _dsdb_prefixmap_from_ldb_val(prefix_val,
458                                             iconv_convenience,
459                                             mem_ctx,
460                                             _pfm);
461         talloc_free(schema_res);
462         W_ERROR_NOT_OK_RETURN(werr);
463
464         return WERR_OK;
465 }
466
467 /*
468   this will be replaced with something that looks at the right part of
469   the schema once we know where unique indexing information is hidden
470  */
471 static bool dsdb_schema_unique_attribute(const char *attr)
472 {
473         const char *attrs[] = { "objectGUID", "objectSID" , NULL };
474         int i;
475         for (i=0;attrs[i];i++) {
476                 if (strcasecmp(attr, attrs[i]) == 0) {
477                         return true;
478                 }
479         }
480         return false;
481 }
482
483
484 /*
485   setup the ldb_schema_attribute field for a dsdb_attribute
486  */
487 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb, 
488                                                   struct dsdb_attribute *attr)
489 {
490         const char *syntax = attr->syntax->ldb_syntax;
491         const struct ldb_schema_syntax *s;
492         struct ldb_schema_attribute *a;
493
494         if (!syntax) {
495                 syntax = attr->syntax->ldap_oid;
496         }
497
498         s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
499         if (s == NULL) {
500                 s = ldb_samba_syntax_by_name(ldb, syntax);
501         }
502         if (s == NULL) {
503                 s = ldb_standard_syntax_by_name(ldb, syntax);
504         }
505
506         if (s == NULL) {
507                 return LDB_ERR_OPERATIONS_ERROR;                
508         }
509
510         attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
511         if (attr->ldb_schema_attribute == NULL) {
512                 ldb_oom(ldb);
513                 return LDB_ERR_OPERATIONS_ERROR;
514         }
515
516         a->name = attr->lDAPDisplayName;
517         a->flags = 0;
518         a->syntax = s;
519
520         if (dsdb_schema_unique_attribute(a->name)) {
521                 a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
522         }
523         if (attr->isSingleValued) {
524                 a->flags |= LDB_ATTR_FLAG_SINGLE_VALUE;
525         }
526         
527         
528         return LDB_SUCCESS;
529 }
530
531
532 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
533         const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
534         if (get_string_val == NULL) { \
535                 if (strict) {                                     \
536                         d_printf("%s: %s == NULL\n", __location__, attr); \
537                         return WERR_INVALID_PARAM;                      \
538                 } else {                                                \
539                         (p)->elem = NULL;                               \
540                 }                                                       \
541         } else {                                                        \
542                 (p)->elem = talloc_strndup(mem_ctx,                     \
543                                            (const char *)get_string_val->data, \
544                                            get_string_val->length); \
545                 if (!(p)->elem) {                                       \
546                         d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
547                         return WERR_NOMEM;                              \
548                 }                                                       \
549         }                                                               \
550 } while (0)
551
552 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do {   \
553         int get_string_list_counter;                                    \
554         struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
555         if (get_string_list_el == NULL) {                               \
556                 if (strict) {                                           \
557                         d_printf("%s: %s == NULL\n", __location__, attr); \
558                         return WERR_INVALID_PARAM;                      \
559                 } else {                                                \
560                         (p)->elem = NULL;                               \
561                         break;                                          \
562                 }                                                       \
563         }                                                               \
564         (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
565         for (get_string_list_counter=0;                                 \
566              get_string_list_counter < get_string_list_el->num_values;  \
567              get_string_list_counter++) {                               \
568                 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
569                                                                     (const char *)get_string_list_el->values[get_string_list_counter].data, \
570                                                                     get_string_list_el->values[get_string_list_counter].length); \
571                 if (!(p)->elem[get_string_list_counter]) {              \
572                         d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
573                         return WERR_NOMEM;                              \
574                 }                                                       \
575                 (p)->elem[get_string_list_counter+1] = NULL;            \
576         }                                                               \
577         talloc_steal(mem_ctx, (p)->elem);                               \
578 } while (0)
579
580 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
581         const char *str; \
582         str = samdb_result_string(msg, attr, NULL);\
583         if (str == NULL) { \
584                 if (strict) { \
585                         d_printf("%s: %s == NULL\n", __location__, attr); \
586                         return WERR_INVALID_PARAM; \
587                 } else { \
588                         (p)->elem = false; \
589                 } \
590         } else if (strcasecmp("TRUE", str) == 0) { \
591                 (p)->elem = true; \
592         } else if (strcasecmp("FALSE", str) == 0) { \
593                 (p)->elem = false; \
594         } else { \
595                 d_printf("%s: %s == %s\n", __location__, attr, str); \
596                 return WERR_INVALID_PARAM; \
597         } \
598 } while (0)
599
600 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
601         (p)->elem = samdb_result_uint(msg, attr, 0);\
602 } while (0)
603
604 #define GET_UINT32_PTR_LDB(msg, attr, p, elem) do { \
605         uint64_t _v = samdb_result_uint64(msg, attr, UINT64_MAX);\
606         if (_v == UINT64_MAX) { \
607                 (p)->elem = NULL; \
608         } else if (_v > UINT32_MAX) { \
609                 d_printf("%s: %s == 0x%llX\n", __location__, \
610                          attr, (unsigned long long)_v); \
611                 return WERR_INVALID_PARAM; \
612         } else { \
613                 (p)->elem = talloc(mem_ctx, uint32_t); \
614                 if (!(p)->elem) { \
615                         d_printf("%s: talloc failed for %s\n", __location__, attr); \
616                         return WERR_NOMEM; \
617                 } \
618                 *(p)->elem = (uint32_t)_v; \
619         } \
620 } while (0)
621
622 #define GET_GUID_LDB(msg, attr, p, elem) do { \
623         (p)->elem = samdb_result_guid(msg, attr);\
624 } while (0)
625
626 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
627         const struct ldb_val *_val;\
628         _val = ldb_msg_find_ldb_val(msg, attr);\
629         if (_val) {\
630                 (p)->elem = *_val;\
631                 talloc_steal(mem_ctx, (p)->elem.data);\
632         } else {\
633                 ZERO_STRUCT((p)->elem);\
634         }\
635 } while (0)
636
637 WERROR dsdb_attribute_from_ldb(struct ldb_context *ldb,
638                                const struct dsdb_schema *schema,
639                                struct ldb_message *msg,
640                                TALLOC_CTX *mem_ctx,
641                                struct dsdb_attribute *attr)
642 {
643         WERROR status;
644
645         GET_STRING_LDB(msg, "cn", mem_ctx, attr, cn, false);
646         GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
647         GET_STRING_LDB(msg, "attributeID", mem_ctx, attr, attributeID_oid, true);
648         if (schema->num_prefixes == 0) {
649                 /* set an invalid value */
650                 attr->attributeID_id = 0xFFFFFFFF;
651         } else {
652                 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
653                                                     attr->attributeID_oid,
654                                                     &attr->attributeID_id);
655                 if (!W_ERROR_IS_OK(status)) {
656                         DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
657                                 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
658                                 win_errstr(status)));
659                         return status;
660                 }
661         }
662         GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
663         GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
664
665         GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
666
667         GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
668         GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
669         GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
670         GET_UINT32_LDB(msg, "linkID", attr, linkID);
671
672         GET_STRING_LDB(msg, "attributeSyntax", mem_ctx, attr, attributeSyntax_oid, true);
673         if (schema->num_prefixes == 0) {
674                 /* set an invalid value */
675                 attr->attributeSyntax_id = 0xFFFFFFFF;
676         } else {
677                 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
678                                                     attr->attributeSyntax_oid,
679                                                     &attr->attributeSyntax_id);
680                 if (!W_ERROR_IS_OK(status)) {
681                         DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
682                                 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
683                                 win_errstr(status)));
684                         return status;
685                 }
686         }
687         GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
688         GET_BLOB_LDB(msg, "oMObjectClass", mem_ctx, attr, oMObjectClass);
689
690         GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
691         GET_UINT32_PTR_LDB(msg, "rangeLower", attr, rangeLower);
692         GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, rangeUpper);
693         GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
694
695         GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
696         GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
697
698         GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
699         GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
700         GET_STRING_LDB(msg, "adminDescription", mem_ctx, attr, adminDescription, false);
701         GET_STRING_LDB(msg, "classDisplayName", mem_ctx, attr, classDisplayName, false);
702         GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
703         GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
704         GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
705
706         attr->syntax = dsdb_syntax_for_attribute(attr);
707         if (!attr->syntax) {
708                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
709         }
710
711         if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
712                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
713         }
714
715         return WERR_OK;
716 }
717
718 WERROR dsdb_class_from_ldb(const struct dsdb_schema *schema,
719                            struct ldb_message *msg,
720                            TALLOC_CTX *mem_ctx,
721                            struct dsdb_class *obj)
722 {
723         WERROR status;
724
725         GET_STRING_LDB(msg, "cn", mem_ctx, obj, cn, false);
726         GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
727         GET_STRING_LDB(msg, "governsID", mem_ctx, obj, governsID_oid, true);
728         if (schema->num_prefixes == 0) {
729                 /* set an invalid value */
730                 obj->governsID_id = 0xFFFFFFFF;
731         } else {
732                 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
733                                                     obj->governsID_oid,
734                                                     &obj->governsID_id);
735                 if (!W_ERROR_IS_OK(status)) {
736                         DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
737                                 __location__, obj->lDAPDisplayName, obj->governsID_oid,
738                                 win_errstr(status)));
739                         return status;
740                 }
741         }
742         GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
743
744         GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
745         GET_STRING_LDB(msg, "rDNAttID", mem_ctx, obj, rDNAttID, false);
746         GET_STRING_LDB(msg, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
747  
748         GET_STRING_LDB(msg, "subClassOf", mem_ctx, obj, subClassOf, true);
749
750         GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass, false);
751         GET_STRING_LIST_LDB(msg, "auxiliaryClass", mem_ctx, obj, auxiliaryClass, false);
752
753         GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, false);
754         GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, false);
755         GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, false);
756         GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, false);
757
758         GET_STRING_LIST_LDB(msg, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
759         GET_STRING_LIST_LDB(msg, "possSuperiors", mem_ctx, obj, possSuperiors, false);
760
761         GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
762
763         GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
764         GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
765
766         GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
767         GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
768         GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, false);
769         GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, false);
770         GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
771         GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
772         GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
773
774         return WERR_OK;
775 }
776
777 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
778
779 /* 
780  Create a DSDB schema from the ldb results provided.  This is called
781  directly when the schema is provisioned from an on-disk LDIF file, or
782  from dsdb_schema_from_schema_dn in schema_fsmo
783 */
784
785 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
786                                  struct smb_iconv_convenience *iconv_convenience, 
787                                  struct ldb_result *schema_res,
788                                  struct ldb_result *attrs_res, struct ldb_result *objectclass_res, 
789                                  struct dsdb_schema **schema_out,
790                                  char **error_string)
791 {
792         WERROR status;
793         uint32_t i;
794         const struct ldb_val *prefix_val;
795         const struct ldb_val *info_val;
796         struct ldb_val info_val_default;
797         struct dsdb_schema *schema;
798
799         schema = dsdb_new_schema(mem_ctx, iconv_convenience);
800         if (!schema) {
801                 dsdb_oom(error_string, mem_ctx);
802                 return LDB_ERR_OPERATIONS_ERROR;
803         }
804
805         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
806         if (!prefix_val) {
807                 *error_string = talloc_asprintf(mem_ctx, 
808                                                 "schema_fsmo_init: no prefixMap attribute found");
809                 DEBUG(0,(__location__ ": %s\n", *error_string));
810                 return LDB_ERR_CONSTRAINT_VIOLATION;
811         }
812         info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
813         if (!info_val) {
814                 info_val_default = strhex_to_data_blob(mem_ctx, "FF0000000000000000000000000000000000000000");
815                 if (!info_val_default.data) {
816                         dsdb_oom(error_string, mem_ctx);
817                         return LDB_ERR_OPERATIONS_ERROR;
818                 }
819                 info_val = &info_val_default;
820         }
821
822         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
823         if (!W_ERROR_IS_OK(status)) {
824                 *error_string = talloc_asprintf(mem_ctx, 
825                               "schema_fsmo_init: failed to load oid mappings: %s",
826                               win_errstr(status));
827                 DEBUG(0,(__location__ ": %s\n", *error_string));
828                 return LDB_ERR_CONSTRAINT_VIOLATION;
829         }
830
831         for (i=0; i < attrs_res->count; i++) {
832                 struct dsdb_attribute *sa;
833
834                 sa = talloc_zero(schema, struct dsdb_attribute);
835                 if (!sa) {
836                         dsdb_oom(error_string, mem_ctx);
837                         return LDB_ERR_OPERATIONS_ERROR;
838                 }
839
840                 status = dsdb_attribute_from_ldb(ldb, schema, attrs_res->msgs[i], sa, sa);
841                 if (!W_ERROR_IS_OK(status)) {
842                         *error_string = talloc_asprintf(mem_ctx, 
843                                       "schema_fsmo_init: failed to load attribute definition: %s:%s",
844                                       ldb_dn_get_linearized(attrs_res->msgs[i]->dn),
845                                       win_errstr(status));
846                         DEBUG(0,(__location__ ": %s\n", *error_string));
847                         return LDB_ERR_CONSTRAINT_VIOLATION;
848                 }
849
850                 DLIST_ADD(schema->attributes, sa);
851         }
852
853         for (i=0; i < objectclass_res->count; i++) {
854                 struct dsdb_class *sc;
855
856                 sc = talloc_zero(schema, struct dsdb_class);
857                 if (!sc) {
858                         dsdb_oom(error_string, mem_ctx);
859                         return LDB_ERR_OPERATIONS_ERROR;
860                 }
861
862                 status = dsdb_class_from_ldb(schema, objectclass_res->msgs[i], sc, sc);
863                 if (!W_ERROR_IS_OK(status)) {
864                         *error_string = talloc_asprintf(mem_ctx, 
865                                       "schema_fsmo_init: failed to load class definition: %s:%s",
866                                       ldb_dn_get_linearized(objectclass_res->msgs[i]->dn),
867                                       win_errstr(status));
868                         DEBUG(0,(__location__ ": %s\n", *error_string));
869                         return LDB_ERR_CONSTRAINT_VIOLATION;
870                 }
871
872                 DLIST_ADD(schema->classes, sc);
873         }
874
875         schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
876         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
877                 schema->fsmo.we_are_master = true;
878         } else {
879                 schema->fsmo.we_are_master = false;
880         }
881
882         DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
883                   (schema->fsmo.we_are_master?"yes":"no")));
884
885         *schema_out = schema;
886         return LDB_SUCCESS;
887 }
888
889
890 static const struct {
891         const char *name;
892         const char *oid;
893 } name_mappings[] = {
894         { "cn",                                 "2.5.4.3" },
895         { "name",                               "1.2.840.113556.1.4.1" },
896         { "lDAPDisplayName",                    "1.2.840.113556.1.2.460" },
897         { "attributeID",                        "1.2.840.113556.1.2.30" },
898         { "schemaIDGUID",                       "1.2.840.113556.1.4.148" },
899         { "mAPIID",                             "1.2.840.113556.1.2.49" },
900         { "attributeSecurityGUID",              "1.2.840.113556.1.4.149" },
901         { "searchFlags",                        "1.2.840.113556.1.2.334" },
902         { "systemFlags",                        "1.2.840.113556.1.4.375" },
903         { "isMemberOfPartialAttributeSet",      "1.2.840.113556.1.4.639" },
904         { "linkID",                             "1.2.840.113556.1.2.50" },
905         { "attributeSyntax",                    "1.2.840.113556.1.2.32" },
906         { "oMSyntax",                           "1.2.840.113556.1.2.231" },
907         { "oMObjectClass",                      "1.2.840.113556.1.2.218" },
908         { "isSingleValued",                     "1.2.840.113556.1.2.33" },
909         { "rangeLower",                         "1.2.840.113556.1.2.34" },
910         { "rangeUpper",                         "1.2.840.113556.1.2.35" },
911         { "extendedCharsAllowed",               "1.2.840.113556.1.2.380" },
912         { "schemaFlagsEx",                      "1.2.840.113556.1.4.120" },
913         { "msDs-Schema-Extensions",             "1.2.840.113556.1.4.1440" },
914         { "showInAdvancedViewOnly",             "1.2.840.113556.1.2.169" },
915         { "adminDisplayName",                   "1.2.840.113556.1.2.194" },
916         { "adminDescription",                   "1.2.840.113556.1.2.226" },
917         { "classDisplayName",                   "1.2.840.113556.1.4.610" },
918         { "isEphemeral",                        "1.2.840.113556.1.4.1212" },
919         { "isDefunct",                          "1.2.840.113556.1.4.661" },
920         { "systemOnly",                         "1.2.840.113556.1.4.170" },
921         { "governsID",                          "1.2.840.113556.1.2.22" },
922         { "objectClassCategory",                "1.2.840.113556.1.2.370" },
923         { "rDNAttID",                           "1.2.840.113556.1.2.26" },
924         { "defaultObjectCategory",              "1.2.840.113556.1.4.783" },
925         { "subClassOf",                         "1.2.840.113556.1.2.21" },
926         { "systemAuxiliaryClass",               "1.2.840.113556.1.4.198" },
927         { "systemPossSuperiors",                "1.2.840.113556.1.4.195" },
928         { "systemMustContain",                  "1.2.840.113556.1.4.197" },
929         { "systemMayContain",                   "1.2.840.113556.1.4.196" },
930         { "auxiliaryClass",                     "1.2.840.113556.1.2.351" },
931         { "possSuperiors",                      "1.2.840.113556.1.2.8" },
932         { "mustContain",                        "1.2.840.113556.1.2.24" },
933         { "mayContain",                         "1.2.840.113556.1.2.25" },
934         { "defaultSecurityDescriptor",          "1.2.840.113556.1.4.224" },
935         { "defaultHidingValue",                 "1.2.840.113556.1.4.518" },
936 };
937
938 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
939                                                                      struct drsuapi_DsReplicaObject *obj,
940                                                                      const char *name,
941                                                                      uint32_t *idx)
942 {
943         WERROR status;
944         uint32_t i, attid;
945         const char *oid = NULL;
946
947         for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
948                 if (strcmp(name_mappings[i].name, name) != 0) continue;
949
950                 oid = name_mappings[i].oid;
951                 break;
952         }
953
954         if (!oid) {
955                 return NULL;
956         }
957
958         status = dsdb_schema_pfm_make_attid(schema->prefixmap, oid, &attid);
959         if (!W_ERROR_IS_OK(status)) {
960                 return NULL;
961         }
962
963         for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
964                 if (obj->attribute_ctr.attributes[i].attid != attid) continue;
965
966                 if (idx) *idx = i;
967                 return &obj->attribute_ctr.attributes[i];
968         }
969
970         return NULL;
971 }
972
973 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
974         struct drsuapi_DsReplicaAttribute *_a; \
975         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
976         if (strict && !_a) { \
977                 d_printf("%s: %s == NULL\n", __location__, attr); \
978                 return WERR_INVALID_PARAM; \
979         } \
980         if (strict && _a->value_ctr.num_values != 1) { \
981                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
982                         _a->value_ctr.num_values); \
983                 return WERR_INVALID_PARAM; \
984         } \
985         if (_a && _a->value_ctr.num_values >= 1) { \
986                 size_t _ret; \
987                 if (!convert_string_talloc_convenience(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
988                                              _a->value_ctr.values[0].blob->data, \
989                                              _a->value_ctr.values[0].blob->length, \
990                                              (void **)discard_const(&(p)->elem), &_ret, false)) { \
991                         DEBUG(0,("%s: invalid data!\n", attr)); \
992                         dump_data(0, \
993                                      _a->value_ctr.values[0].blob->data, \
994                                      _a->value_ctr.values[0].blob->length); \
995                         return WERR_FOOBAR; \
996                 } \
997         } else { \
998                 (p)->elem = NULL; \
999         } \
1000 } while (0)
1001
1002 #define GET_UINT32_LIST_DS(s, r, attr, mem_ctx, p, elem) do { \
1003         int list_counter;                                       \
1004         struct drsuapi_DsReplicaAttribute *_a; \
1005         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1006         (p)->elem = _a ? talloc_array(mem_ctx, uint32_t, _a->value_ctr.num_values + 1) : NULL; \
1007         for (list_counter=0;                                    \
1008              _a && list_counter < _a->value_ctr.num_values;     \
1009              list_counter++) {                          \
1010                 if (_a->value_ctr.values[list_counter].blob->length != 4) { \
1011                         return WERR_INVALID_PARAM;                      \
1012                 }                                                       \
1013                 (p)->elem[list_counter] = IVAL(_a->value_ctr.values[list_counter].blob->data, 0); \
1014         }                                                               \
1015         if (_a) (p)->elem[list_counter] = 0;                            \
1016 } while (0)
1017
1018 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1019         struct drsuapi_DsReplicaAttribute *_a; \
1020         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1021         if (strict && !_a) { \
1022                 d_printf("%s: %s == NULL\n", __location__, attr); \
1023                 return WERR_INVALID_PARAM; \
1024         } \
1025         if (strict && _a->value_ctr.num_values != 1) { \
1026                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1027                         _a->value_ctr.num_values); \
1028                 return WERR_INVALID_PARAM; \
1029         } \
1030         if (strict && !_a->value_ctr.values[0].blob) { \
1031                 d_printf("%s: %s data == NULL\n", __location__, attr); \
1032                 return WERR_INVALID_PARAM; \
1033         } \
1034         if (_a && _a->value_ctr.num_values >= 1 \
1035             && _a->value_ctr.values[0].blob) { \
1036                 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
1037                 enum ndr_err_code _ndr_err; \
1038                 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1039                                                       mem_ctx, s->iconv_convenience, &_id3,\
1040                                                       (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
1041                 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1042                         NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1043                         return ntstatus_to_werror(_nt_status); \
1044                 } \
1045                 (p)->elem = _id3.dn; \
1046         } else { \
1047                 (p)->elem = NULL; \
1048         } \
1049 } while (0)
1050
1051 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
1052         struct drsuapi_DsReplicaAttribute *_a; \
1053         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1054         if (strict && !_a) { \
1055                 d_printf("%s: %s == NULL\n", __location__, attr); \
1056                 return WERR_INVALID_PARAM; \
1057         } \
1058         if (strict && _a->value_ctr.num_values != 1) { \
1059                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1060                          (unsigned int)_a->value_ctr.num_values);       \
1061                 return WERR_INVALID_PARAM; \
1062         } \
1063         if (strict && !_a->value_ctr.values[0].blob) { \
1064                 d_printf("%s: %s data == NULL\n", __location__, attr); \
1065                 return WERR_INVALID_PARAM; \
1066         } \
1067         if (strict && _a->value_ctr.values[0].blob->length != 4) { \
1068                 d_printf("%s: %s length == %u\n", __location__, attr, \
1069                          (unsigned int)_a->value_ctr.values[0].blob->length); \
1070                 return WERR_INVALID_PARAM; \
1071         } \
1072         if (_a && _a->value_ctr.num_values >= 1 \
1073             && _a->value_ctr.values[0].blob \
1074             && _a->value_ctr.values[0].blob->length == 4) { \
1075                 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?true:false);\
1076         } else { \
1077                 (p)->elem = false; \
1078         } \
1079 } while (0)
1080
1081 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
1082         struct drsuapi_DsReplicaAttribute *_a; \
1083         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1084         if (_a && _a->value_ctr.num_values >= 1 \
1085             && _a->value_ctr.values[0].blob \
1086             && _a->value_ctr.values[0].blob->length == 4) { \
1087                 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1088         } else { \
1089                 (p)->elem = 0; \
1090         } \
1091 } while (0)
1092
1093 #define GET_UINT32_PTR_DS(s, r, attr, p, elem) do { \
1094         struct drsuapi_DsReplicaAttribute *_a; \
1095         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1096         if (_a && _a->value_ctr.num_values >= 1 \
1097             && _a->value_ctr.values[0].blob \
1098             && _a->value_ctr.values[0].blob->length == 4) { \
1099                 (p)->elem = talloc(mem_ctx, uint32_t); \
1100                 if (!(p)->elem) { \
1101                         d_printf("%s: talloc failed for %s\n", __location__, attr); \
1102                         return WERR_NOMEM; \
1103                 } \
1104                 *(p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1105         } else { \
1106                 (p)->elem = NULL; \
1107         } \
1108 } while (0)
1109
1110 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
1111         struct drsuapi_DsReplicaAttribute *_a; \
1112         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1113         if (_a && _a->value_ctr.num_values >= 1 \
1114             && _a->value_ctr.values[0].blob \
1115             && _a->value_ctr.values[0].blob->length == 16) { \
1116                 enum ndr_err_code _ndr_err; \
1117                 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1118                                                       mem_ctx, s->iconv_convenience, &(p)->elem, \
1119                                                       (ndr_pull_flags_fn_t)ndr_pull_GUID); \
1120                 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1121                         NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1122                         return ntstatus_to_werror(_nt_status); \
1123                 } \
1124         } else { \
1125                 ZERO_STRUCT((p)->elem);\
1126         } \
1127 } while (0)
1128
1129 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
1130         struct drsuapi_DsReplicaAttribute *_a; \
1131         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1132         if (_a && _a->value_ctr.num_values >= 1 \
1133             && _a->value_ctr.values[0].blob) { \
1134                 (p)->elem = *_a->value_ctr.values[0].blob;\
1135                 talloc_steal(mem_ctx, (p)->elem.data); \
1136         } else { \
1137                 ZERO_STRUCT((p)->elem);\
1138         }\
1139 } while (0)
1140
1141 WERROR dsdb_attribute_from_drsuapi(struct ldb_context *ldb,
1142                                    struct dsdb_schema *schema,
1143                                    struct drsuapi_DsReplicaObject *r,
1144                                    TALLOC_CTX *mem_ctx,
1145                                    struct dsdb_attribute *attr)
1146 {
1147         WERROR status;
1148
1149         GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, true);
1150         GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
1151         GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
1152         status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, attr->attributeID_id,
1153                                                 mem_ctx, &attr->attributeID_oid);
1154         if (!W_ERROR_IS_OK(status)) {
1155                 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
1156                         __location__, attr->lDAPDisplayName, attr->attributeID_id,
1157                         win_errstr(status)));
1158                 return status;
1159         }
1160         GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
1161         GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
1162
1163         GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
1164
1165         GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
1166         GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
1167         GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
1168         GET_UINT32_DS(schema, r, "linkID", attr, linkID);
1169
1170         GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
1171         status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, attr->attributeSyntax_id,
1172                                                 mem_ctx, &attr->attributeSyntax_oid);
1173         if (!W_ERROR_IS_OK(status)) {
1174                 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
1175                         __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
1176                         win_errstr(status)));
1177                 return status;
1178         }
1179         GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
1180         GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
1181
1182         GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, true);
1183         GET_UINT32_PTR_DS(schema, r, "rangeLower", attr, rangeLower);
1184         GET_UINT32_PTR_DS(schema, r, "rangeUpper", attr, rangeUpper);
1185         GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
1186
1187         GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
1188         GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
1189
1190         GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
1191         GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
1192         GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, false);
1193         GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, false);
1194         GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, false);
1195         GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, false);
1196         GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, false);
1197
1198         attr->syntax = dsdb_syntax_for_attribute(attr);
1199         if (!attr->syntax) {
1200                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1201         }
1202
1203         if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
1204                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1205         }
1206
1207         return WERR_OK;
1208 }
1209
1210 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
1211                                struct drsuapi_DsReplicaObject *r,
1212                                TALLOC_CTX *mem_ctx,
1213                                struct dsdb_class *obj)
1214 {
1215         WERROR status;
1216
1217         GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, true);
1218         GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
1219         GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
1220         status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, obj->governsID_id,
1221                                                 mem_ctx, &obj->governsID_oid);
1222         if (!W_ERROR_IS_OK(status)) {
1223                 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
1224                         __location__, obj->lDAPDisplayName, obj->governsID_id,
1225                         win_errstr(status)));
1226                 return status;
1227         }
1228         GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
1229
1230         GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
1231         GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
1232         GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
1233
1234         GET_UINT32_DS(schema, r, "subClassOf", obj, subClassOf_id);
1235
1236         GET_UINT32_LIST_DS(schema, r, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass_ids);
1237         GET_UINT32_LIST_DS(schema, r, "auxiliaryClass", mem_ctx, obj, auxiliaryClass_ids);
1238
1239         GET_UINT32_LIST_DS(schema, r, "systemMustContain", mem_ctx, obj, systemMustContain_ids);
1240         GET_UINT32_LIST_DS(schema, r, "systemMayContain", mem_ctx, obj, systemMayContain_ids);
1241         GET_UINT32_LIST_DS(schema, r, "mustContain", mem_ctx, obj, mustContain_ids);
1242         GET_UINT32_LIST_DS(schema, r, "mayContain", mem_ctx, obj, mayContain_ids);
1243
1244         GET_UINT32_LIST_DS(schema, r, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors_ids);
1245         GET_UINT32_LIST_DS(schema, r, "possSuperiors", mem_ctx, obj, possSuperiors_ids);
1246
1247         GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
1248
1249         GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
1250         GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
1251
1252         GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
1253         GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
1254         GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, false);
1255         GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, false);
1256         GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, false);
1257         GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, false);
1258         GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, false);
1259
1260         return WERR_OK;
1261 }
1262