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