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