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