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