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