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