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