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