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