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