r23792: convert Samba4 to GPLv3
[garming/samba-autobuild/.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         obj->systemPossSuperiors        = NULL;
496
497         obj->auxiliaryClass             = NULL;
498         obj->possSuperiors              = NULL;
499
500         GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, False);
501         GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, False);
502         GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, False);
503         GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, False);
504
505         GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, False);
506
507         GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
508         GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
509
510         GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, False);
511         GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, False);
512         GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, False);
513         GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, False);
514         GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, False);
515         GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, False);
516         GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, False);
517
518         return WERR_OK;
519 }
520
521 static const struct {
522         const char *name;
523         const char *oid;
524 } name_mappings[] = {
525         { "cn",                                 "2.5.4.3" },
526         { "name",                               "1.2.840.113556.1.4.1" },
527         { "lDAPDisplayName",                    "1.2.840.113556.1.2.460" },
528         { "attributeID",                        "1.2.840.113556.1.2.30" },
529         { "schemaIDGUID",                       "1.2.840.113556.1.4.148" },
530         { "mAPIID",                             "1.2.840.113556.1.2.49" },
531         { "attributeSecurityGUID",              "1.2.840.113556.1.4.149" },
532         { "searchFlags",                        "1.2.840.113556.1.2.334" },
533         { "systemFlags",                        "1.2.840.113556.1.4.375" },
534         { "isMemberOfPartialAttributeSet",      "1.2.840.113556.1.4.639" },
535         { "linkID",                             "1.2.840.113556.1.2.50" },
536         { "attributeSyntax",                    "1.2.840.113556.1.2.32" },
537         { "oMSyntax",                           "1.2.840.113556.1.2.231" },
538         { "oMObjectClass",                      "1.2.840.113556.1.2.218" },
539         { "isSingleValued",                     "1.2.840.113556.1.2.33" },
540         { "rangeLower",                         "1.2.840.113556.1.2.34" },
541         { "rangeUpper",                         "1.2.840.113556.1.2.35" },
542         { "extendedCharsAllowed",               "1.2.840.113556.1.2.380" },
543         { "schemaFlagsEx",                      "1.2.840.113556.1.4.120" },
544         { "msDs-Schema-Extensions",             "1.2.840.113556.1.4.1440" },
545         { "showInAdvancedViewOnly",             "1.2.840.113556.1.2.169" },
546         { "adminDisplayName",                   "1.2.840.113556.1.2.194" },
547         { "adminDescription",                   "1.2.840.113556.1.2.226" },
548         { "classDisplayName",                   "1.2.840.113556.1.4.610" },
549         { "isEphemeral",                        "1.2.840.113556.1.4.1212" },
550         { "isDefunct",                          "1.2.840.113556.1.4.661" },
551         { "systemOnly",                         "1.2.840.113556.1.4.170" },
552         { "governsID",                          "1.2.840.113556.1.2.22" },
553         { "objectClassCategory",                "1.2.840.113556.1.2.370" },
554         { "rDNAttID",                           "1.2.840.113556.1.2.26" },
555         { "defaultObjectCategory",              "1.2.840.113556.1.4.783" },
556         { "subClassOf",                         "1.2.840.113556.1.2.21" },
557         { "systemAuxiliaryClass",               "1.2.840.113556.1.4.198" },
558         { "systemPossSuperiors",                "1.2.840.113556.1.4.195" },
559         { "systemMustContain",                  "1.2.840.113556.1.4.197" },
560         { "systemMayContain",                   "1.2.840.113556.1.4.196" },
561         { "auxiliaryClass",                     "1.2.840.113556.1.2.351" },
562         { "possSuperiors",                      "1.2.840.113556.1.2.8" },
563         { "mustContain",                        "1.2.840.113556.1.2.24" },
564         { "mayContain",                         "1.2.840.113556.1.2.25" },
565         { "defaultSecurityDescriptor",          "1.2.840.113556.1.4.224" },
566         { "defaultHidingValue",                 "1.2.840.113556.1.4.518" },
567 };
568
569 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
570                                                                      struct drsuapi_DsReplicaObject *obj,
571                                                                      const char *name,
572                                                                      uint32_t *idx)
573 {
574         WERROR status;
575         uint32_t i, id;
576         const char *oid = NULL;
577
578         for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
579                 if (strcmp(name_mappings[i].name, name) != 0) continue;
580
581                 oid = name_mappings[i].oid;
582                 break;
583         }
584
585         if (!oid) {
586                 return NULL;
587         }
588
589         status = dsdb_map_oid2int(schema, oid, &id);
590         if (!W_ERROR_IS_OK(status)) {
591                 return NULL;
592         }
593
594         for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
595                 if (obj->attribute_ctr.attributes[i].attid != id) continue;
596
597                 if (idx) *idx = i;
598                 return &obj->attribute_ctr.attributes[i];
599         }
600
601         return NULL;
602 }
603
604 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
605         struct drsuapi_DsReplicaAttribute *_a; \
606         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
607         if (strict && !_a) { \
608                 d_printf("%s: %s == NULL\n", __location__, attr); \
609                 return WERR_INVALID_PARAM; \
610         } \
611         if (strict && _a->value_ctr.num_values != 1) { \
612                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
613                         _a->value_ctr.num_values); \
614                 return WERR_INVALID_PARAM; \
615         } \
616         if (_a && _a->value_ctr.num_values >= 1) { \
617                 ssize_t _ret; \
618                 _ret = convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX, \
619                                              _a->value_ctr.values[0].blob->data, \
620                                              _a->value_ctr.values[0].blob->length, \
621                                              (void **)discard_const(&(p)->elem)); \
622                 if (_ret == -1) { \
623                         DEBUG(0,("%s: invalid data!\n", attr)); \
624                         dump_data(0, \
625                                      _a->value_ctr.values[0].blob->data, \
626                                      _a->value_ctr.values[0].blob->length); \
627                         return WERR_FOOBAR; \
628                 } \
629         } else { \
630                 (p)->elem = NULL; \
631         } \
632 } while (0)
633
634 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
635         struct drsuapi_DsReplicaAttribute *_a; \
636         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
637         if (strict && !_a) { \
638                 d_printf("%s: %s == NULL\n", __location__, attr); \
639                 return WERR_INVALID_PARAM; \
640         } \
641         if (strict && _a->value_ctr.num_values != 1) { \
642                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
643                         _a->value_ctr.num_values); \
644                 return WERR_INVALID_PARAM; \
645         } \
646         if (strict && !_a->value_ctr.values[0].blob) { \
647                 d_printf("%s: %s data == NULL\n", __location__, attr); \
648                 return WERR_INVALID_PARAM; \
649         } \
650         if (_a && _a->value_ctr.num_values >= 1 \
651             && _a->value_ctr.values[0].blob) { \
652                 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
653                 NTSTATUS _nt_status; \
654                 _nt_status = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
655                                                       mem_ctx, &_id3,\
656                                                       (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
657                 if (!NT_STATUS_IS_OK(_nt_status)) { \
658                         return ntstatus_to_werror(_nt_status); \
659                 } \
660                 (p)->elem = _id3.dn; \
661         } else { \
662                 (p)->elem = NULL; \
663         } \
664 } while (0)
665
666 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
667         struct drsuapi_DsReplicaAttribute *_a; \
668         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
669         if (strict && !_a) { \
670                 d_printf("%s: %s == NULL\n", __location__, attr); \
671                 return WERR_INVALID_PARAM; \
672         } \
673         if (strict && _a->value_ctr.num_values != 1) { \
674                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
675                         _a->value_ctr.num_values); \
676                 return WERR_INVALID_PARAM; \
677         } \
678         if (strict && !_a->value_ctr.values[0].blob) { \
679                 d_printf("%s: %s data == NULL\n", __location__, attr); \
680                 return WERR_INVALID_PARAM; \
681         } \
682         if (strict && _a->value_ctr.values[0].blob->length != 4) { \
683                 d_printf("%s: %s length == %u\n", __location__, attr, \
684                         _a->value_ctr.values[0].blob->length); \
685                 return WERR_INVALID_PARAM; \
686         } \
687         if (_a && _a->value_ctr.num_values >= 1 \
688             && _a->value_ctr.values[0].blob \
689             && _a->value_ctr.values[0].blob->length == 4) { \
690                 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?True:False);\
691         } else { \
692                 (p)->elem = False; \
693         } \
694 } while (0)
695
696 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
697         struct drsuapi_DsReplicaAttribute *_a; \
698         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
699         if (_a && _a->value_ctr.num_values >= 1 \
700             && _a->value_ctr.values[0].blob \
701             && _a->value_ctr.values[0].blob->length == 4) { \
702                 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
703         } else { \
704                 (p)->elem = 0; \
705         } \
706 } while (0)
707
708 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
709         struct drsuapi_DsReplicaAttribute *_a; \
710         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
711         if (_a && _a->value_ctr.num_values >= 1 \
712             && _a->value_ctr.values[0].blob \
713             && _a->value_ctr.values[0].blob->length == 16) { \
714                 NTSTATUS _nt_status; \
715                 _nt_status = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
716                                                       mem_ctx, &(p)->elem, \
717                                                       (ndr_pull_flags_fn_t)ndr_pull_GUID); \
718                 if (!NT_STATUS_IS_OK(_nt_status)) { \
719                         return ntstatus_to_werror(_nt_status); \
720                 } \
721         } else { \
722                 ZERO_STRUCT((p)->elem);\
723         } \
724 } while (0)
725
726 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
727         struct drsuapi_DsReplicaAttribute *_a; \
728         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
729         if (_a && _a->value_ctr.num_values >= 1 \
730             && _a->value_ctr.values[0].blob) { \
731                 (p)->elem = *_a->value_ctr.values[0].blob;\
732                 talloc_steal(mem_ctx, (p)->elem.data); \
733         } else { \
734                 ZERO_STRUCT((p)->elem);\
735         }\
736 } while (0)
737
738 WERROR dsdb_attribute_from_drsuapi(struct dsdb_schema *schema,
739                                    struct drsuapi_DsReplicaObject *r,
740                                    TALLOC_CTX *mem_ctx,
741                                    struct dsdb_attribute *attr)
742 {
743         WERROR status;
744
745         GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, True);
746         GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, True);
747         GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
748         status = dsdb_map_int2oid(schema, attr->attributeID_id, mem_ctx, &attr->attributeID_oid);
749         if (!W_ERROR_IS_OK(status)) {
750                 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
751                         __location__, attr->lDAPDisplayName, attr->attributeID_id,
752                         win_errstr(status)));
753                 return status;
754         }
755         GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
756         GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
757
758         GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
759
760         GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
761         GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
762         GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, False);
763         GET_UINT32_DS(schema, r, "linkID", attr, linkID);
764
765         GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
766         status = dsdb_map_int2oid(schema, attr->attributeSyntax_id, mem_ctx, &attr->attributeSyntax_oid);
767         if (!W_ERROR_IS_OK(status)) {
768                 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
769                         __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
770                         win_errstr(status)));
771                 return status;
772         }
773         GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
774         GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
775
776         GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, True);
777         GET_UINT32_DS(schema, r, "rangeLower", attr, rangeLower);
778         GET_UINT32_DS(schema, r, "rangeUpper", attr, rangeUpper);
779         GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, False);
780
781         GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
782         GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
783
784         GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, False);
785         GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, False);
786         GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, False);
787         GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, False);
788         GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, False);
789         GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, False);
790         GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, False);
791
792         attr->syntax = dsdb_syntax_for_attribute(attr);
793         if (!attr->syntax) {
794                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
795         }
796
797         return WERR_OK;
798 }
799
800 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
801                                struct drsuapi_DsReplicaObject *r,
802                                TALLOC_CTX *mem_ctx,
803                                struct dsdb_class *obj)
804 {
805         WERROR status;
806
807         GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, True);
808         GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, True);
809         GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
810         status = dsdb_map_int2oid(schema, obj->governsID_id, mem_ctx, &obj->governsID_oid);
811         if (!W_ERROR_IS_OK(status)) {
812                 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
813                         __location__, obj->lDAPDisplayName, obj->governsID_id,
814                         win_errstr(status)));
815                 return status;
816         }
817         GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
818
819         GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
820         GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, False);
821         GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, True);
822
823         GET_STRING_DS(schema, r, "subClassOf", mem_ctx, obj, subClassOf, True);
824
825         obj->systemAuxiliaryClass       = NULL;
826         obj->systemPossSuperiors        = NULL;
827         obj->systemMustContain          = NULL;
828         obj->systemMayContain           = NULL;
829
830         obj->auxiliaryClass             = NULL;
831         obj->possSuperiors              = NULL;
832         obj->mustContain                = NULL;
833         obj->mayContain                 = NULL;
834
835         GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, False);
836
837         GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
838         GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
839
840         GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, False);
841         GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, False);
842         GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, False);
843         GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, False);
844         GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, False);
845         GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, False);
846         GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, False);
847
848         return WERR_OK;
849 }
850
851 const struct dsdb_attribute *dsdb_attribute_by_attributeID_id(const struct dsdb_schema *schema,
852                                                               uint32_t id)
853 {
854         struct dsdb_attribute *cur;
855
856         /*
857          * 0xFFFFFFFF is used as value when no mapping table is available,
858          * so don't try to match with it
859          */
860         if (id == 0xFFFFFFFF) return NULL;
861
862         /* TODO: add binary search */
863         for (cur = schema->attributes; cur; cur = cur->next) {
864                 if (cur->attributeID_id != id) continue;
865
866                 return cur;
867         }
868
869         return NULL;
870 }
871
872 const struct dsdb_attribute *dsdb_attribute_by_attributeID_oid(const struct dsdb_schema *schema,
873                                                                const char *oid)
874 {
875         struct dsdb_attribute *cur;
876
877         if (!oid) return NULL;
878
879         /* TODO: add binary search */
880         for (cur = schema->attributes; cur; cur = cur->next) {
881                 if (strcmp(cur->attributeID_oid, oid) != 0) continue;
882
883                 return cur;
884         }
885
886         return NULL;
887 }
888
889 const struct dsdb_attribute *dsdb_attribute_by_lDAPDisplayName(const struct dsdb_schema *schema,
890                                                                const char *name)
891 {
892         struct dsdb_attribute *cur;
893
894         if (!name) return NULL;
895
896         /* TODO: add binary search */
897         for (cur = schema->attributes; cur; cur = cur->next) {
898                 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
899
900                 return cur;
901         }
902
903         return NULL;
904 }
905
906 const struct dsdb_class *dsdb_class_by_governsID_id(const struct dsdb_schema *schema,
907                                                     uint32_t id)
908 {
909         struct dsdb_class *cur;
910
911         /*
912          * 0xFFFFFFFF is used as value when no mapping table is available,
913          * so don't try to match with it
914          */
915         if (id == 0xFFFFFFFF) return NULL;
916
917         /* TODO: add binary search */
918         for (cur = schema->classes; cur; cur = cur->next) {
919                 if (cur->governsID_id != id) continue;
920
921                 return cur;
922         }
923
924         return NULL;
925 }
926
927 const struct dsdb_class *dsdb_class_by_governsID_oid(const struct dsdb_schema *schema,
928                                                      const char *oid)
929 {
930         struct dsdb_class *cur;
931
932         if (!oid) return NULL;
933
934         /* TODO: add binary search */
935         for (cur = schema->classes; cur; cur = cur->next) {
936                 if (strcmp(cur->governsID_oid, oid) != 0) continue;
937
938                 return cur;
939         }
940
941         return NULL;
942 }
943
944 const struct dsdb_class *dsdb_class_by_lDAPDisplayName(const struct dsdb_schema *schema,
945                                                        const char *name)
946 {
947         struct dsdb_class *cur;
948
949         if (!name) return NULL;
950
951         /* TODO: add binary search */
952         for (cur = schema->classes; cur; cur = cur->next) {
953                 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
954
955                 return cur;
956         }
957
958         return NULL;
959 }
960
961 const struct dsdb_class *dsdb_class_by_cn(const struct dsdb_schema *schema,
962                                           const char *cn)
963 {
964         struct dsdb_class *cur;
965
966         if (!cn) return NULL;
967
968         /* TODO: add binary search */
969         for (cur = schema->classes; cur; cur = cur->next) {
970                 if (strcasecmp(cur->cn, cn) != 0) continue;
971
972                 return cur;
973         }
974
975         return NULL;
976 }
977
978 const char *dsdb_lDAPDisplayName_by_id(const struct dsdb_schema *schema,
979                                        uint32_t id)
980 {
981         const struct dsdb_attribute *a;
982         const struct dsdb_class *c;
983
984         /* TODO: add binary search */
985         a = dsdb_attribute_by_attributeID_id(schema, id);
986         if (a) {
987                 return a->lDAPDisplayName;
988         }
989
990         c = dsdb_class_by_governsID_id(schema, id);
991         if (c) {
992                 return c->lDAPDisplayName;
993         }
994
995         return NULL;
996 }
997
998 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
999 {
1000         int ret;
1001
1002         ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
1003         if (ret != LDB_SUCCESS) {
1004                 return ret;
1005         }
1006
1007         talloc_steal(ldb, schema);
1008
1009         return LDB_SUCCESS;
1010 }
1011
1012 static struct dsdb_schema *global_schema;
1013
1014 int dsdb_set_global_schema(struct ldb_context *ldb)
1015 {
1016         int ret;
1017         if (!global_schema) {
1018                 return LDB_SUCCESS;
1019         }
1020         ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
1021         if (ret != LDB_SUCCESS) {
1022                 return ret;
1023         }
1024
1025         return LDB_SUCCESS;
1026 }
1027
1028 const struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb)
1029 {
1030         const void *p;
1031         const struct dsdb_schema *schema;
1032
1033         /* see if we have a cached copy */
1034         p = ldb_get_opaque(ldb, "dsdb_schema");
1035         if (!p) {
1036                 return NULL;
1037         }
1038
1039         schema = talloc_get_type(p, struct dsdb_schema);
1040         if (!schema) {
1041                 return NULL;
1042         }
1043
1044         return schema;
1045 }
1046
1047 void dsdb_make_schema_global(struct ldb_context *ldb)
1048 {
1049         const void *p;
1050         const struct dsdb_schema *schema;
1051
1052         /* see if we have a cached copy */
1053         p = ldb_get_opaque(ldb, "dsdb_schema");
1054         if (!p) {
1055                 return;
1056         }
1057
1058         schema = talloc_get_type(p, struct dsdb_schema);
1059         if (!schema) {
1060                 return;
1061         }
1062
1063         talloc_steal(talloc_autofree_context(), schema);
1064         global_schema = schema;
1065
1066         dsdb_set_global_schema(ldb);
1067 }
1068
1069 WERROR dsdb_attach_schema_from_ldif_file(struct ldb_context *ldb, const char *pf, const char *df)
1070 {
1071         struct ldb_ldif *ldif;
1072         struct ldb_message *msg;
1073         TALLOC_CTX *mem_ctx;
1074         WERROR status;
1075         int ret;
1076         struct dsdb_schema *schema;
1077         const struct ldb_val *prefix_val;
1078         const struct ldb_val *info_val;
1079         struct ldb_val info_val_default;
1080
1081         mem_ctx = talloc_new(ldb);
1082         if (!mem_ctx) {
1083                 goto nomem;
1084         }
1085
1086         schema = talloc_zero(mem_ctx, struct dsdb_schema);
1087         if (!schema) {
1088                 goto nomem;
1089         }
1090
1091         /*
1092          * load the prefixMap attribute from pf
1093          */
1094         ldif = ldb_ldif_read_string(ldb, &pf);
1095         if (!ldif) {
1096                 status = WERR_INVALID_PARAM;
1097                 goto failed;
1098         }
1099         talloc_steal(mem_ctx, ldif);
1100
1101         msg = ldb_msg_canonicalize(ldb, ldif->msg);
1102         if (!msg) {
1103                 goto nomem;
1104         }
1105
1106         prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
1107         if (!prefix_val) {
1108                 status = WERR_INVALID_PARAM;
1109                 goto failed;
1110         }
1111
1112         info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
1113         if (!info_val) {
1114                 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
1115                 if (!info_val_default.data) {
1116                         goto nomem;
1117                 }
1118                 talloc_steal(mem_ctx, info_val_default.data);
1119                 info_val = &info_val_default;
1120         }
1121
1122         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
1123         if (!W_ERROR_IS_OK(status)) {
1124                 goto failed;
1125         }
1126
1127         /*
1128          * load the attribute and class definitions outof df
1129          */
1130         while ((ldif = ldb_ldif_read_string(ldb, &df))) {
1131                 bool is_sa;
1132                 bool is_sc;
1133
1134                 talloc_steal(mem_ctx, ldif);
1135
1136                 msg = ldb_msg_canonicalize(ldb, ldif->msg);
1137                 if (!msg) {
1138                         goto nomem;
1139                 }
1140
1141                 is_sa = ldb_msg_check_string_attribute(msg, "objectClass", "attributeSchema");
1142                 is_sc = ldb_msg_check_string_attribute(msg, "objectClass", "classSchema");
1143
1144                 if (is_sa) {
1145                         struct dsdb_attribute *sa;
1146
1147                         sa = talloc_zero(schema, struct dsdb_attribute);
1148                         if (!sa) {
1149                                 goto nomem;
1150                         }
1151
1152                         status = dsdb_attribute_from_ldb(schema, msg, sa, sa);
1153                         if (!W_ERROR_IS_OK(status)) {
1154                                 goto failed;
1155                         }
1156
1157                         DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
1158                 } else if (is_sc) {
1159                         struct dsdb_class *sc;
1160
1161                         sc = talloc_zero(schema, struct dsdb_class);
1162                         if (!sc) {
1163                                 goto nomem;
1164                         }
1165
1166                         status = dsdb_class_from_ldb(schema, msg, sc, sc);
1167                         if (!W_ERROR_IS_OK(status)) {
1168                                 goto failed;
1169                         }
1170
1171                         DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
1172                 }
1173         }
1174
1175         ret = dsdb_set_schema(ldb, schema);
1176         if (ret != LDB_SUCCESS) {
1177                 status = WERR_FOOBAR;
1178                 goto failed;
1179         }
1180
1181         goto done;
1182
1183 nomem:
1184         status = WERR_NOMEM;
1185 failed:
1186 done:
1187         talloc_free(mem_ctx);
1188         return status;
1189 }