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