Use common code to fill in allowedAttributes in kludge_acl.
[kai/samba.git] / source4 / dsdb / schema / schema_init.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    DSDB schema header
4    
5    Copyright (C) Stefan Metzmacher <metze@samba.org> 2006-2007
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2008
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20    
21 */
22
23 #include "includes.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "lib/ldb/include/ldb_errors.h"
26 #include "lib/util/dlinklist.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 #include "param/param.h"
31
32 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
33 {
34         struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
35         if (!schema) {
36                 return NULL;
37         }
38
39         schema->iconv_convenience = iconv_convenience;
40         return schema;
41 }
42
43
44 WERROR dsdb_load_oid_mappings_drsuapi(struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
45 {
46         uint32_t i,j;
47
48         schema->prefixes = talloc_array(schema, struct dsdb_schema_oid_prefix, ctr->num_mappings);
49         W_ERROR_HAVE_NO_MEMORY(schema->prefixes);
50
51         for (i=0, j=0; i < ctr->num_mappings; i++) {
52                 if (ctr->mappings[i].oid.oid == NULL) {
53                         return WERR_INVALID_PARAM;
54                 }
55
56                 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
57                         if (ctr->mappings[i].id_prefix != 0) {
58                                 return WERR_INVALID_PARAM;
59                         }
60
61                         /* the magic value should be in the last array member */
62                         if (i != (ctr->num_mappings - 1)) {
63                                 return WERR_INVALID_PARAM;
64                         }
65
66                         if (ctr->mappings[i].oid.__ndr_size != 21) {
67                                 return WERR_INVALID_PARAM;
68                         }
69
70                         schema->schema_info = talloc_strdup(schema, ctr->mappings[i].oid.oid);
71                         W_ERROR_HAVE_NO_MEMORY(schema->schema_info);
72                 } else {
73                         /* the last array member should contain the magic value not a oid */
74                         if (i == (ctr->num_mappings - 1)) {
75                                 return WERR_INVALID_PARAM;
76                         }
77
78                         schema->prefixes[j].id  = ctr->mappings[i].id_prefix<<16;
79                         schema->prefixes[j].oid = talloc_asprintf(schema->prefixes, "%s.",
80                                                                   ctr->mappings[i].oid.oid);
81                         W_ERROR_HAVE_NO_MEMORY(schema->prefixes[j].oid);
82                         schema->prefixes[j].oid_len = strlen(schema->prefixes[j].oid);
83                         j++;
84                 }
85         }
86
87         schema->num_prefixes = j;
88         return WERR_OK;
89 }
90
91 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
92                                   const struct ldb_val *prefixMap,
93                                   const struct ldb_val *schemaInfo)
94 {
95         WERROR status;
96         enum ndr_err_code ndr_err;
97         struct prefixMapBlob pfm;
98         char *schema_info;
99
100         TALLOC_CTX *mem_ctx = talloc_new(schema);
101         W_ERROR_HAVE_NO_MEMORY(mem_ctx);
102         
103         ndr_err = ndr_pull_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
104         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
105                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
106                 talloc_free(mem_ctx);
107                 return ntstatus_to_werror(nt_status);
108         }
109
110         if (pfm.version != PREFIX_MAP_VERSION_DSDB) {
111                 talloc_free(mem_ctx);
112                 return WERR_FOOBAR;
113         }
114
115         if (schemaInfo->length != 21 && schemaInfo->data[0] == 0xFF) {
116                 talloc_free(mem_ctx);
117                 return WERR_FOOBAR;
118         }
119
120         /* append the schema info as last element */
121         pfm.ctr.dsdb.num_mappings++;
122         pfm.ctr.dsdb.mappings = talloc_realloc(mem_ctx, pfm.ctr.dsdb.mappings,
123                                                struct drsuapi_DsReplicaOIDMapping,
124                                                pfm.ctr.dsdb.num_mappings);
125         W_ERROR_HAVE_NO_MEMORY(pfm.ctr.dsdb.mappings);
126
127         schema_info = data_blob_hex_string(pfm.ctr.dsdb.mappings, schemaInfo);
128         W_ERROR_HAVE_NO_MEMORY(schema_info);
129
130         pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].id_prefix          = 0;    
131         pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.__ndr_size     = schemaInfo->length;
132         pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.oid            = schema_info;
133
134         /* call the drsuapi version */
135         status = dsdb_load_oid_mappings_drsuapi(schema, &pfm.ctr.dsdb);
136         talloc_free(mem_ctx);
137
138         W_ERROR_NOT_OK_RETURN(status);
139
140         return WERR_OK;
141 }
142
143 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
144                                      bool include_schema_info,
145                                      TALLOC_CTX *mem_ctx,
146                                      struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
147 {
148         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
149         uint32_t i;
150
151         ctr = talloc(mem_ctx, struct drsuapi_DsReplicaOIDMapping_Ctr);
152         W_ERROR_HAVE_NO_MEMORY(ctr);
153
154         ctr->num_mappings       = schema->num_prefixes;
155         if (include_schema_info) ctr->num_mappings++;
156         ctr->mappings = talloc_array(schema, struct drsuapi_DsReplicaOIDMapping, ctr->num_mappings);
157         W_ERROR_HAVE_NO_MEMORY(ctr->mappings);
158
159         for (i=0; i < schema->num_prefixes; i++) {
160                 ctr->mappings[i].id_prefix      = schema->prefixes[i].id>>16;
161                 ctr->mappings[i].oid.oid        = talloc_strndup(ctr->mappings,
162                                                                  schema->prefixes[i].oid,
163                                                                  schema->prefixes[i].oid_len - 1);
164                 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
165         }
166
167         if (include_schema_info) {
168                 ctr->mappings[i].id_prefix      = 0;
169                 ctr->mappings[i].oid.oid        = talloc_strdup(ctr->mappings,
170                                                                 schema->schema_info);
171                 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
172         }
173
174         *_ctr = ctr;
175         return WERR_OK;
176 }
177
178 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
179                                  TALLOC_CTX *mem_ctx,
180                                  struct ldb_val *prefixMap,
181                                  struct ldb_val *schemaInfo)
182 {
183         WERROR status;
184         enum ndr_err_code ndr_err;
185         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
186         struct prefixMapBlob pfm;
187
188         status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
189         W_ERROR_NOT_OK_RETURN(status);
190
191         pfm.version     = PREFIX_MAP_VERSION_DSDB;
192         pfm.reserved    = 0;
193         pfm.ctr.dsdb    = *ctr;
194
195         ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
196         talloc_free(ctr);
197         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
198                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
199                 return ntstatus_to_werror(nt_status);
200         }
201
202         *schemaInfo = strhex_to_data_blob(schema->schema_info);
203         W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
204         talloc_steal(mem_ctx, schemaInfo->data);
205
206         return WERR_OK;
207 }
208
209 WERROR dsdb_verify_oid_mappings_drsuapi(const struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
210 {
211         uint32_t i,j;
212
213         for (i=0; i < ctr->num_mappings; i++) {
214                 if (ctr->mappings[i].oid.oid == NULL) {
215                         return WERR_INVALID_PARAM;
216                 }
217
218                 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
219                         if (ctr->mappings[i].id_prefix != 0) {
220                                 return WERR_INVALID_PARAM;
221                         }
222
223                         /* the magic value should be in the last array member */
224                         if (i != (ctr->num_mappings - 1)) {
225                                 return WERR_INVALID_PARAM;
226                         }
227
228                         if (ctr->mappings[i].oid.__ndr_size != 21) {
229                                 return WERR_INVALID_PARAM;
230                         }
231
232                         if (strcasecmp(schema->schema_info, ctr->mappings[i].oid.oid) != 0) {
233                                 return WERR_DS_DRA_SCHEMA_MISMATCH;
234                         }
235                 } else {
236                         /* the last array member should contain the magic value not a oid */
237                         if (i == (ctr->num_mappings - 1)) {
238                                 return WERR_INVALID_PARAM;
239                         }
240
241                         for (j=0; j < schema->num_prefixes; j++) {
242                                 size_t oid_len;
243                                 if (schema->prefixes[j].id != (ctr->mappings[i].id_prefix<<16)) {
244                                         continue;
245                                 }
246
247                                 oid_len = strlen(ctr->mappings[i].oid.oid);
248
249                                 if (oid_len != (schema->prefixes[j].oid_len - 1)) {
250                                         return WERR_DS_DRA_SCHEMA_MISMATCH;
251                                 }
252
253                                 if (strncmp(ctr->mappings[i].oid.oid, schema->prefixes[j].oid, oid_len) != 0) {
254                                         return WERR_DS_DRA_SCHEMA_MISMATCH;                             
255                                 }
256
257                                 break;
258                         }
259
260                         if (j == schema->num_prefixes) {
261                                 return WERR_DS_DRA_SCHEMA_MISMATCH;                             
262                         }
263                 }
264         }
265
266         return WERR_OK;
267 }
268
269 WERROR dsdb_map_oid2int(const struct dsdb_schema *schema, const char *in, uint32_t *out)
270 {
271         uint32_t i;
272
273         for (i=0; i < schema->num_prefixes; i++) {
274                 const char *val_str;
275                 char *end_str;
276                 unsigned val;
277
278                 if (strncmp(schema->prefixes[i].oid, in, schema->prefixes[i].oid_len) != 0) {
279                         continue;
280                 }
281
282                 val_str = in + schema->prefixes[i].oid_len;
283                 end_str = NULL;
284                 errno = 0;
285
286                 if (val_str[0] == '\0') {
287                         return WERR_INVALID_PARAM;
288                 }
289
290                 /* two '.' chars are invalid */
291                 if (val_str[0] == '.') {
292                         return WERR_INVALID_PARAM;
293                 }
294
295                 val = strtoul(val_str, &end_str, 10);
296                 if (end_str[0] == '.' && end_str[1] != '\0') {
297                         /*
298                          * if it's a '.' and not the last char
299                          * then maybe an other mapping apply
300                          */
301                         continue;
302                 } else if (end_str[0] != '\0') {
303                         return WERR_INVALID_PARAM;
304                 } else if (val > 0xFFFF) {
305                         return WERR_INVALID_PARAM;
306                 }
307
308                 *out = schema->prefixes[i].id | val;
309                 return WERR_OK;
310         }
311
312         return WERR_DS_NO_MSDS_INTID;
313 }
314
315 WERROR dsdb_map_int2oid(const struct dsdb_schema *schema, uint32_t in, TALLOC_CTX *mem_ctx, const char **out)
316 {
317         uint32_t i;
318
319         for (i=0; i < schema->num_prefixes; i++) {
320                 const char *val;
321                 if (schema->prefixes[i].id != (in & 0xFFFF0000)) {
322                         continue;
323                 }
324
325                 val = talloc_asprintf(mem_ctx, "%s%u",
326                                       schema->prefixes[i].oid,
327                                       in & 0xFFFF);
328                 W_ERROR_HAVE_NO_MEMORY(val);
329
330                 *out = val;
331                 return WERR_OK;
332         }
333
334         return WERR_DS_NO_MSDS_INTID;
335 }
336
337 /*
338  * this function is called from within a ldb transaction from the schema_fsmo module
339  */
340 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
341 {
342         /*
343          * TODO:
344          *      - (maybe) read the old prefixMap attribute and parse it
345          *
346          *      - recheck the prefix doesn't exist (because the ldb
347          *        has maybe a more uptodate value than schem->prefixes
348          *
349          *      - calculate a new mapping for the oid prefix of full_oid
350          *      - store the new prefixMap attribute
351          *
352          *      - (maybe) update schema->prefixes
353          *      or
354          *      - better find a way to indicate a schema reload,
355          *        so that other processes also notice the schema change
356          */
357         return WERR_NOT_SUPPORTED;
358 }
359
360 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
361         (p)->elem = samdb_result_string(msg, attr, NULL);\
362         if (strict && (p)->elem == NULL) { \
363                 d_printf("%s: %s == NULL\n", __location__, attr); \
364                 return WERR_INVALID_PARAM; \
365         } \
366         talloc_steal(mem_ctx, (p)->elem); \
367 } while (0)
368
369 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do {   \
370         int get_string_list_counter;                                    \
371         struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
372         if (get_string_list_el == NULL) {                               \
373                 if (strict) {                                           \
374                         d_printf("%s: %s == NULL\n", __location__, attr); \
375                         return WERR_INVALID_PARAM;                      \
376                 } else {                                                \
377                         (p)->elem = NULL;                               \
378                         break;                                          \
379                 }                                                       \
380         }                                                               \
381         (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
382         for (get_string_list_counter=0;                                 \
383              get_string_list_counter < get_string_list_el->num_values;  \
384              get_string_list_counter++) {                               \
385                 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
386                                                                     (const char *)get_string_list_el->values[get_string_list_counter].data, \
387                                                                     get_string_list_el->values[get_string_list_counter].length); \
388                 if (!(p)->elem[get_string_list_counter]) {              \
389                         d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
390                         return WERR_NOMEM;                              \
391                 }                                                       \
392                 (p)->elem[get_string_list_counter+1] = NULL;            \
393         }                                                               \
394         talloc_steal(mem_ctx, (p)->elem);                               \
395 } while (0)
396
397 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
398         const char *str; \
399         str = samdb_result_string(msg, attr, NULL);\
400         if (str == NULL) { \
401                 if (strict) { \
402                         d_printf("%s: %s == NULL\n", __location__, attr); \
403                         return WERR_INVALID_PARAM; \
404                 } else { \
405                         (p)->elem = false; \
406                 } \
407         } else if (strcasecmp("TRUE", str) == 0) { \
408                 (p)->elem = true; \
409         } else if (strcasecmp("FALSE", str) == 0) { \
410                 (p)->elem = false; \
411         } else { \
412                 d_printf("%s: %s == %s\n", __location__, attr, str); \
413                 return WERR_INVALID_PARAM; \
414         } \
415 } while (0)
416
417 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
418         (p)->elem = samdb_result_uint(msg, attr, 0);\
419 } while (0)
420
421 #define GET_GUID_LDB(msg, attr, p, elem) do { \
422         (p)->elem = samdb_result_guid(msg, attr);\
423 } while (0)
424
425 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
426         const struct ldb_val *_val;\
427         _val = ldb_msg_find_ldb_val(msg, attr);\
428         if (_val) {\
429                 (p)->elem = *_val;\
430                 talloc_steal(mem_ctx, (p)->elem.data);\
431         } else {\
432                 ZERO_STRUCT((p)->elem);\
433         }\
434 } while (0)
435
436 WERROR dsdb_attribute_from_ldb(const struct dsdb_schema *schema,
437                                struct ldb_message *msg,
438                                TALLOC_CTX *mem_ctx,
439                                struct dsdb_attribute *attr)
440 {
441         WERROR status;
442
443         GET_STRING_LDB(msg, "cn", mem_ctx, attr, cn, false);
444         GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
445         GET_STRING_LDB(msg, "attributeID", mem_ctx, attr, attributeID_oid, true);
446         if (schema->num_prefixes == 0) {
447                 /* set an invalid value */
448                 attr->attributeID_id = 0xFFFFFFFF;
449         } else {
450                 status = dsdb_map_oid2int(schema, attr->attributeID_oid, &attr->attributeID_id);
451                 if (!W_ERROR_IS_OK(status)) {
452                         DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
453                                 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
454                                 win_errstr(status)));
455                         return status;
456                 }
457         }
458         GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
459         GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
460
461         GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
462
463         GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
464         GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
465         GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
466         GET_UINT32_LDB(msg, "linkID", attr, linkID);
467
468         GET_STRING_LDB(msg, "attributeSyntax", mem_ctx, attr, attributeSyntax_oid, true);
469         if (schema->num_prefixes == 0) {
470                 /* set an invalid value */
471                 attr->attributeSyntax_id = 0xFFFFFFFF;
472         } else {
473                 status = dsdb_map_oid2int(schema, attr->attributeSyntax_oid, &attr->attributeSyntax_id);
474                 if (!W_ERROR_IS_OK(status)) {
475                         DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
476                                 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
477                                 win_errstr(status)));
478                         return status;
479                 }
480         }
481         GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
482         GET_BLOB_LDB(msg, "oMObjectClass", mem_ctx, attr, oMObjectClass);
483
484         GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
485         GET_UINT32_LDB(msg, "rangeLower", attr, rangeLower);
486         GET_UINT32_LDB(msg, "rangeUpper", attr, rangeUpper);
487         GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
488
489         GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
490         GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
491
492         GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
493         GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
494         GET_STRING_LDB(msg, "adminDescription", mem_ctx, attr, adminDescription, false);
495         GET_STRING_LDB(msg, "classDisplayName", mem_ctx, attr, classDisplayName, false);
496         GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
497         GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
498         GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
499
500         attr->syntax = dsdb_syntax_for_attribute(attr);
501         if (!attr->syntax) {
502                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
503         }
504
505         return WERR_OK;
506 }
507
508 WERROR dsdb_class_from_ldb(const struct dsdb_schema *schema,
509                            struct ldb_message *msg,
510                            TALLOC_CTX *mem_ctx,
511                            struct dsdb_class *obj)
512 {
513         WERROR status;
514
515         GET_STRING_LDB(msg, "cn", mem_ctx, obj, cn, false);
516         GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
517         GET_STRING_LDB(msg, "governsID", mem_ctx, obj, governsID_oid, true);
518         if (schema->num_prefixes == 0) {
519                 /* set an invalid value */
520                 obj->governsID_id = 0xFFFFFFFF;
521         } else {
522                 status = dsdb_map_oid2int(schema, obj->governsID_oid, &obj->governsID_id);
523                 if (!W_ERROR_IS_OK(status)) {
524                         DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
525                                 __location__, obj->lDAPDisplayName, obj->governsID_oid,
526                                 win_errstr(status)));
527                         return status;
528                 }
529         }
530         GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
531
532         GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
533         GET_STRING_LDB(msg, "rDNAttID", mem_ctx, obj, rDNAttID, false);
534         GET_STRING_LDB(msg, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
535  
536         GET_STRING_LDB(msg, "subClassOf", mem_ctx, obj, subClassOf, true);
537
538         GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass, false);
539         GET_STRING_LIST_LDB(msg, "auxiliaryClass", mem_ctx, obj, auxiliaryClass, false);
540
541         GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, false);
542         GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, false);
543         GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, false);
544         GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, false);
545
546         GET_STRING_LIST_LDB(msg, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
547         GET_STRING_LIST_LDB(msg, "possSuperiors", mem_ctx, obj, possSuperiors, false);
548         GET_STRING_LIST_LDB(msg, "possibleInferiors", mem_ctx, obj, possibleInferiors, false);
549
550         GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
551
552         GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
553         GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
554
555         GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
556         GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
557         GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, false);
558         GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, false);
559         GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
560         GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
561         GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
562
563         return WERR_OK;
564 }
565
566 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
567
568 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
569                                  struct smb_iconv_convenience *iconv_convenience, 
570                                  struct ldb_result *schema_res,
571                                  struct ldb_result *attrs_res, struct ldb_result *objectclass_res, 
572                                  struct dsdb_schema **schema_out,
573                                  char **error_string)
574 {
575         WERROR status;
576         uint32_t i;
577         const struct ldb_val *prefix_val;
578         const struct ldb_val *info_val;
579         struct ldb_val info_val_default;
580         struct dsdb_schema *schema;
581
582         schema = dsdb_new_schema(mem_ctx, iconv_convenience);
583         if (!schema) {
584                 dsdb_oom(error_string, mem_ctx);
585                 return LDB_ERR_OPERATIONS_ERROR;
586         }
587
588         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
589         if (!prefix_val) {
590                 *error_string = talloc_asprintf(mem_ctx, 
591                                                 "schema_fsmo_init: no prefixMap attribute found");
592                 talloc_free(mem_ctx);
593                 return LDB_ERR_CONSTRAINT_VIOLATION;
594         }
595         info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
596         if (!info_val) {
597                 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
598                 if (!info_val_default.data) {
599                         dsdb_oom(error_string, mem_ctx);
600                         return LDB_ERR_OPERATIONS_ERROR;
601                 }
602                 talloc_steal(mem_ctx, info_val_default.data);
603                 info_val = &info_val_default;
604         }
605
606         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
607         if (!W_ERROR_IS_OK(status)) {
608                 *error_string = talloc_asprintf(mem_ctx, 
609                               "schema_fsmo_init: failed to load oid mappings: %s",
610                               win_errstr(status));
611                 talloc_free(mem_ctx);
612                 return LDB_ERR_CONSTRAINT_VIOLATION;
613         }
614
615         for (i=0; i < attrs_res->count; i++) {
616                 struct dsdb_attribute *sa;
617
618                 sa = talloc_zero(schema, struct dsdb_attribute);
619                 if (!sa) {
620                         dsdb_oom(error_string, mem_ctx);
621                         return LDB_ERR_OPERATIONS_ERROR;
622                 }
623
624                 status = dsdb_attribute_from_ldb(schema, attrs_res->msgs[i], sa, sa);
625                 if (!W_ERROR_IS_OK(status)) {
626                         *error_string = talloc_asprintf(mem_ctx, 
627                                       "schema_fsmo_init: failed to load attribute definition: %s:%s",
628                                       ldb_dn_get_linearized(attrs_res->msgs[i]->dn),
629                                       win_errstr(status));
630                         talloc_free(mem_ctx);
631                         return LDB_ERR_CONSTRAINT_VIOLATION;
632                 }
633
634                 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
635         }
636
637         for (i=0; i < objectclass_res->count; i++) {
638                 struct dsdb_class *sc;
639
640                 sc = talloc_zero(schema, struct dsdb_class);
641                 if (!sc) {
642                         dsdb_oom(error_string, mem_ctx);
643                         return LDB_ERR_OPERATIONS_ERROR;
644                 }
645
646                 status = dsdb_class_from_ldb(schema, objectclass_res->msgs[i], sc, sc);
647                 if (!W_ERROR_IS_OK(status)) {
648                         *error_string = talloc_asprintf(mem_ctx, 
649                                       "schema_fsmo_init: failed to load class definition: %s:%s",
650                                       ldb_dn_get_linearized(objectclass_res->msgs[i]->dn),
651                                       win_errstr(status));
652                         talloc_free(mem_ctx);
653                         return LDB_ERR_CONSTRAINT_VIOLATION;
654                 }
655
656                 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
657         }
658
659         schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
660         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
661                 schema->fsmo.we_are_master = true;
662         } else {
663                 schema->fsmo.we_are_master = false;
664         }
665
666         DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
667                   (schema->fsmo.we_are_master?"yes":"no")));
668
669         *schema_out = schema;
670         return LDB_SUCCESS;
671 }
672
673 /* This recursive load of the objectClasses presumes that they
674  * everything is in a strict subClassOf hirarchy.  
675  *
676  * We load this in order so we produce certain outputs (such as the
677  * exported schema for openldap, and sorted objectClass attribute) 'in
678  * order' */
679
680 static int fetch_oc_recursive(struct ldb_context *ldb, struct ldb_dn *schemadn, 
681                               TALLOC_CTX *mem_ctx, 
682                               struct ldb_result *search_from,
683                               struct ldb_result *res_list)
684 {
685         int i;
686         int ret = 0;
687         for (i=0; i < search_from->count; i++) {
688                 struct ldb_result *res;
689                 const char *name = ldb_msg_find_attr_as_string(search_from->msgs[i], 
690                                                                "lDAPDisplayname", NULL);
691
692                 ret = ldb_search_exp_fmt(ldb, mem_ctx, &res,
693                                         schemadn, LDB_SCOPE_SUBTREE, NULL,
694                                         "(&(&(objectClass=classSchema)(subClassOf=%s))(!(lDAPDisplayName=%s)))",
695                                         name, name);
696                 if (ret != LDB_SUCCESS) {
697                         printf("Search failed: %s\n", ldb_errstring(ldb));
698                         return ret;
699                 }
700                 
701                 res_list->msgs = talloc_realloc(res_list, res_list->msgs, 
702                                                 struct ldb_message *, res_list->count + 2);
703                 if (!res_list->msgs) {
704                         return LDB_ERR_OPERATIONS_ERROR;
705                 }
706                 res_list->msgs[res_list->count] = talloc_move(res_list, 
707                                                               &search_from->msgs[i]);
708                 res_list->count++;
709                 res_list->msgs[res_list->count] = NULL;
710
711                 if (res->count > 0) {
712                         ret = fetch_oc_recursive(ldb, schemadn, mem_ctx, res, res_list); 
713                 }
714                 if (ret != LDB_SUCCESS) {
715                         return ret;
716                 }
717         }
718         return ret;
719 }
720
721 static int fetch_objectclass_schema(struct ldb_context *ldb, struct ldb_dn *schemadn, 
722                                     TALLOC_CTX *mem_ctx, 
723                                     struct ldb_result **objectclasses_res)
724 {
725         TALLOC_CTX *local_ctx = talloc_new(mem_ctx);
726         struct ldb_result *top_res, *ret_res;
727         int ret;
728         if (!local_ctx) {
729                 return LDB_ERR_OPERATIONS_ERROR;
730         }
731         
732         /* Downlaod 'top' */
733         ret = ldb_search(ldb, schemadn, LDB_SCOPE_SUBTREE, 
734                          "(&(objectClass=classSchema)(lDAPDisplayName=top))", 
735                          NULL, &top_res);
736         if (ret != LDB_SUCCESS) {
737                 printf("Search failed: %s\n", ldb_errstring(ldb));
738                 return LDB_ERR_OPERATIONS_ERROR;
739         }
740
741         talloc_steal(local_ctx, top_res);
742
743         if (top_res->count != 1) {
744                 return LDB_ERR_OPERATIONS_ERROR;
745         }
746
747         ret_res = talloc_zero(local_ctx, struct ldb_result);
748         if (!ret_res) {
749                 return LDB_ERR_OPERATIONS_ERROR;
750         }
751
752         ret = fetch_oc_recursive(ldb, schemadn, local_ctx, top_res, ret_res); 
753
754         if (ret != LDB_SUCCESS) {
755                 printf("Search failed: %s\n", ldb_errstring(ldb));
756                 return LDB_ERR_OPERATIONS_ERROR;
757         }
758
759         *objectclasses_res = talloc_move(mem_ctx, &ret_res);
760         return ret;
761 }
762
763 int dsdb_schema_from_schema_dn(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
764                                struct smb_iconv_convenience *iconv_convenience, 
765                                struct ldb_dn *schema_dn,
766                                struct dsdb_schema **schema,
767                                char **error_string_out) 
768 {
769         TALLOC_CTX *tmp_ctx;
770         char *error_string;
771         int ret;
772
773         struct ldb_result *schema_res;
774         struct ldb_result *a_res;
775         struct ldb_result *c_res;
776         static const char *schema_attrs[] = {
777                 "prefixMap",
778                 "schemaInfo",
779                 "fSMORoleOwner",
780                 NULL
781         };
782
783         tmp_ctx = talloc_new(mem_ctx);
784         if (!tmp_ctx) {
785                 dsdb_oom(error_string_out, mem_ctx);
786                 return LDB_ERR_OPERATIONS_ERROR;
787         }
788
789         /*
790          * setup the prefix mappings and schema info
791          */
792         ret = ldb_search(ldb, schema_dn,
793                          LDB_SCOPE_BASE,
794                          NULL, schema_attrs,
795                          &schema_res);
796         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
797                 talloc_free(tmp_ctx);
798                 return ret;
799         } else if (ret != LDB_SUCCESS) {
800                 *error_string_out = talloc_asprintf(mem_ctx, 
801                                        "dsdb_schema: failed to search the schema head: %s",
802                                        ldb_errstring(ldb));
803                 talloc_free(tmp_ctx);
804                 return ret;
805         }
806         talloc_steal(tmp_ctx, schema_res);
807         if (schema_res->count != 1) {
808                 *error_string_out = talloc_asprintf(mem_ctx, 
809                               "dsdb_schema: [%u] schema heads found on a base search",
810                               schema_res->count);
811                 talloc_free(tmp_ctx);
812                 return LDB_ERR_CONSTRAINT_VIOLATION;
813         }
814
815         /*
816          * load the attribute definitions
817          */
818         ret = ldb_search(ldb, schema_dn,
819                          LDB_SCOPE_ONELEVEL,
820                          "(objectClass=attributeSchema)", NULL,
821                          &a_res);
822         if (ret != LDB_SUCCESS) {
823                 *error_string_out = talloc_asprintf(mem_ctx, 
824                                        "dsdb_schema: failed to search attributeSchema objects: %s",
825                                        ldb_errstring(ldb));
826                 talloc_free(tmp_ctx);
827                 return ret;
828         }
829         talloc_steal(tmp_ctx, a_res);
830
831         /*
832          * load the objectClass definitions
833          */
834         ret = fetch_objectclass_schema(ldb, schema_dn, tmp_ctx, &c_res);
835         if (ret != LDB_SUCCESS) {
836                 *error_string_out = talloc_asprintf(mem_ctx, 
837                                        "Failed to fetch objectClass schema elements: %s\n", ldb_errstring(ldb));
838                 talloc_free(tmp_ctx);
839                 return ret;
840         }
841
842         ret = dsdb_schema_from_ldb_results(tmp_ctx, ldb,
843                                            lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
844                                            schema_res, a_res, c_res, schema, &error_string);
845         if (ret != LDB_SUCCESS) {
846                 *error_string_out = talloc_asprintf(mem_ctx, 
847                                                     "dsdb_schema load failed: %s",
848                                                     error_string);
849                 talloc_free(tmp_ctx);
850                 return ret;
851         }
852         talloc_steal(mem_ctx, *schema);
853         talloc_free(tmp_ctx);
854
855         return LDB_SUCCESS;
856 }       
857
858
859 static const struct {
860         const char *name;
861         const char *oid;
862 } name_mappings[] = {
863         { "cn",                                 "2.5.4.3" },
864         { "name",                               "1.2.840.113556.1.4.1" },
865         { "lDAPDisplayName",                    "1.2.840.113556.1.2.460" },
866         { "attributeID",                        "1.2.840.113556.1.2.30" },
867         { "schemaIDGUID",                       "1.2.840.113556.1.4.148" },
868         { "mAPIID",                             "1.2.840.113556.1.2.49" },
869         { "attributeSecurityGUID",              "1.2.840.113556.1.4.149" },
870         { "searchFlags",                        "1.2.840.113556.1.2.334" },
871         { "systemFlags",                        "1.2.840.113556.1.4.375" },
872         { "isMemberOfPartialAttributeSet",      "1.2.840.113556.1.4.639" },
873         { "linkID",                             "1.2.840.113556.1.2.50" },
874         { "attributeSyntax",                    "1.2.840.113556.1.2.32" },
875         { "oMSyntax",                           "1.2.840.113556.1.2.231" },
876         { "oMObjectClass",                      "1.2.840.113556.1.2.218" },
877         { "isSingleValued",                     "1.2.840.113556.1.2.33" },
878         { "rangeLower",                         "1.2.840.113556.1.2.34" },
879         { "rangeUpper",                         "1.2.840.113556.1.2.35" },
880         { "extendedCharsAllowed",               "1.2.840.113556.1.2.380" },
881         { "schemaFlagsEx",                      "1.2.840.113556.1.4.120" },
882         { "msDs-Schema-Extensions",             "1.2.840.113556.1.4.1440" },
883         { "showInAdvancedViewOnly",             "1.2.840.113556.1.2.169" },
884         { "adminDisplayName",                   "1.2.840.113556.1.2.194" },
885         { "adminDescription",                   "1.2.840.113556.1.2.226" },
886         { "classDisplayName",                   "1.2.840.113556.1.4.610" },
887         { "isEphemeral",                        "1.2.840.113556.1.4.1212" },
888         { "isDefunct",                          "1.2.840.113556.1.4.661" },
889         { "systemOnly",                         "1.2.840.113556.1.4.170" },
890         { "governsID",                          "1.2.840.113556.1.2.22" },
891         { "objectClassCategory",                "1.2.840.113556.1.2.370" },
892         { "rDNAttID",                           "1.2.840.113556.1.2.26" },
893         { "defaultObjectCategory",              "1.2.840.113556.1.4.783" },
894         { "subClassOf",                         "1.2.840.113556.1.2.21" },
895         { "systemAuxiliaryClass",               "1.2.840.113556.1.4.198" },
896         { "systemPossSuperiors",                "1.2.840.113556.1.4.195" },
897         { "systemMustContain",                  "1.2.840.113556.1.4.197" },
898         { "systemMayContain",                   "1.2.840.113556.1.4.196" },
899         { "auxiliaryClass",                     "1.2.840.113556.1.2.351" },
900         { "possSuperiors",                      "1.2.840.113556.1.2.8" },
901         { "mustContain",                        "1.2.840.113556.1.2.24" },
902         { "mayContain",                         "1.2.840.113556.1.2.25" },
903         { "defaultSecurityDescriptor",          "1.2.840.113556.1.4.224" },
904         { "defaultHidingValue",                 "1.2.840.113556.1.4.518" },
905 };
906
907 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
908                                                                      struct drsuapi_DsReplicaObject *obj,
909                                                                      const char *name,
910                                                                      uint32_t *idx)
911 {
912         WERROR status;
913         uint32_t i, id;
914         const char *oid = NULL;
915
916         for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
917                 if (strcmp(name_mappings[i].name, name) != 0) continue;
918
919                 oid = name_mappings[i].oid;
920                 break;
921         }
922
923         if (!oid) {
924                 return NULL;
925         }
926
927         status = dsdb_map_oid2int(schema, oid, &id);
928         if (!W_ERROR_IS_OK(status)) {
929                 return NULL;
930         }
931
932         for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
933                 if (obj->attribute_ctr.attributes[i].attid != id) continue;
934
935                 if (idx) *idx = i;
936                 return &obj->attribute_ctr.attributes[i];
937         }
938
939         return NULL;
940 }
941
942 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
943         struct drsuapi_DsReplicaAttribute *_a; \
944         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
945         if (strict && !_a) { \
946                 d_printf("%s: %s == NULL\n", __location__, attr); \
947                 return WERR_INVALID_PARAM; \
948         } \
949         if (strict && _a->value_ctr.num_values != 1) { \
950                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
951                         _a->value_ctr.num_values); \
952                 return WERR_INVALID_PARAM; \
953         } \
954         if (_a && _a->value_ctr.num_values >= 1) { \
955                 ssize_t _ret; \
956                 _ret = convert_string_talloc(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
957                                              _a->value_ctr.values[0].blob->data, \
958                                              _a->value_ctr.values[0].blob->length, \
959                                              (void **)discard_const(&(p)->elem)); \
960                 if (_ret == -1) { \
961                         DEBUG(0,("%s: invalid data!\n", attr)); \
962                         dump_data(0, \
963                                      _a->value_ctr.values[0].blob->data, \
964                                      _a->value_ctr.values[0].blob->length); \
965                         return WERR_FOOBAR; \
966                 } \
967         } else { \
968                 (p)->elem = NULL; \
969         } \
970 } while (0)
971
972 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
973         struct drsuapi_DsReplicaAttribute *_a; \
974         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
975         if (strict && !_a) { \
976                 d_printf("%s: %s == NULL\n", __location__, attr); \
977                 return WERR_INVALID_PARAM; \
978         } \
979         if (strict && _a->value_ctr.num_values != 1) { \
980                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
981                         _a->value_ctr.num_values); \
982                 return WERR_INVALID_PARAM; \
983         } \
984         if (strict && !_a->value_ctr.values[0].blob) { \
985                 d_printf("%s: %s data == NULL\n", __location__, attr); \
986                 return WERR_INVALID_PARAM; \
987         } \
988         if (_a && _a->value_ctr.num_values >= 1 \
989             && _a->value_ctr.values[0].blob) { \
990                 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
991                 enum ndr_err_code _ndr_err; \
992                 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
993                                                       mem_ctx, s->iconv_convenience, &_id3,\
994                                                       (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
995                 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
996                         NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
997                         return ntstatus_to_werror(_nt_status); \
998                 } \
999                 (p)->elem = _id3.dn; \
1000         } else { \
1001                 (p)->elem = NULL; \
1002         } \
1003 } while (0)
1004
1005 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
1006         struct drsuapi_DsReplicaAttribute *_a; \
1007         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1008         if (strict && !_a) { \
1009                 d_printf("%s: %s == NULL\n", __location__, attr); \
1010                 return WERR_INVALID_PARAM; \
1011         } \
1012         if (strict && _a->value_ctr.num_values != 1) { \
1013                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1014                          (unsigned int)_a->value_ctr.num_values);       \
1015                 return WERR_INVALID_PARAM; \
1016         } \
1017         if (strict && !_a->value_ctr.values[0].blob) { \
1018                 d_printf("%s: %s data == NULL\n", __location__, attr); \
1019                 return WERR_INVALID_PARAM; \
1020         } \
1021         if (strict && _a->value_ctr.values[0].blob->length != 4) { \
1022                 d_printf("%s: %s length == %u\n", __location__, attr, \
1023                          (unsigned int)_a->value_ctr.values[0].blob->length); \
1024                 return WERR_INVALID_PARAM; \
1025         } \
1026         if (_a && _a->value_ctr.num_values >= 1 \
1027             && _a->value_ctr.values[0].blob \
1028             && _a->value_ctr.values[0].blob->length == 4) { \
1029                 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?true:false);\
1030         } else { \
1031                 (p)->elem = false; \
1032         } \
1033 } while (0)
1034
1035 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
1036         struct drsuapi_DsReplicaAttribute *_a; \
1037         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1038         if (_a && _a->value_ctr.num_values >= 1 \
1039             && _a->value_ctr.values[0].blob \
1040             && _a->value_ctr.values[0].blob->length == 4) { \
1041                 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1042         } else { \
1043                 (p)->elem = 0; \
1044         } \
1045 } while (0)
1046
1047 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
1048         struct drsuapi_DsReplicaAttribute *_a; \
1049         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1050         if (_a && _a->value_ctr.num_values >= 1 \
1051             && _a->value_ctr.values[0].blob \
1052             && _a->value_ctr.values[0].blob->length == 16) { \
1053                 enum ndr_err_code _ndr_err; \
1054                 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1055                                                       mem_ctx, s->iconv_convenience, &(p)->elem, \
1056                                                       (ndr_pull_flags_fn_t)ndr_pull_GUID); \
1057                 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1058                         NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1059                         return ntstatus_to_werror(_nt_status); \
1060                 } \
1061         } else { \
1062                 ZERO_STRUCT((p)->elem);\
1063         } \
1064 } while (0)
1065
1066 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
1067         struct drsuapi_DsReplicaAttribute *_a; \
1068         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1069         if (_a && _a->value_ctr.num_values >= 1 \
1070             && _a->value_ctr.values[0].blob) { \
1071                 (p)->elem = *_a->value_ctr.values[0].blob;\
1072                 talloc_steal(mem_ctx, (p)->elem.data); \
1073         } else { \
1074                 ZERO_STRUCT((p)->elem);\
1075         }\
1076 } while (0)
1077
1078 WERROR dsdb_attribute_from_drsuapi(struct dsdb_schema *schema,
1079                                    struct drsuapi_DsReplicaObject *r,
1080                                    TALLOC_CTX *mem_ctx,
1081                                    struct dsdb_attribute *attr)
1082 {
1083         WERROR status;
1084
1085         GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, true);
1086         GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
1087         GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
1088         status = dsdb_map_int2oid(schema, attr->attributeID_id, mem_ctx, &attr->attributeID_oid);
1089         if (!W_ERROR_IS_OK(status)) {
1090                 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
1091                         __location__, attr->lDAPDisplayName, attr->attributeID_id,
1092                         win_errstr(status)));
1093                 return status;
1094         }
1095         GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
1096         GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
1097
1098         GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
1099
1100         GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
1101         GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
1102         GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
1103         GET_UINT32_DS(schema, r, "linkID", attr, linkID);
1104
1105         GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
1106         status = dsdb_map_int2oid(schema, attr->attributeSyntax_id, mem_ctx, &attr->attributeSyntax_oid);
1107         if (!W_ERROR_IS_OK(status)) {
1108                 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
1109                         __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
1110                         win_errstr(status)));
1111                 return status;
1112         }
1113         GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
1114         GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
1115
1116         GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, true);
1117         GET_UINT32_DS(schema, r, "rangeLower", attr, rangeLower);
1118         GET_UINT32_DS(schema, r, "rangeUpper", attr, rangeUpper);
1119         GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
1120
1121         GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
1122         GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
1123
1124         GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
1125         GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
1126         GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, false);
1127         GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, false);
1128         GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, false);
1129         GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, false);
1130         GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, false);
1131
1132         attr->syntax = dsdb_syntax_for_attribute(attr);
1133         if (!attr->syntax) {
1134                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1135         }
1136
1137         return WERR_OK;
1138 }
1139
1140 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
1141                                struct drsuapi_DsReplicaObject *r,
1142                                TALLOC_CTX *mem_ctx,
1143                                struct dsdb_class *obj)
1144 {
1145         WERROR status;
1146
1147         GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, true);
1148         GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
1149         GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
1150         status = dsdb_map_int2oid(schema, obj->governsID_id, mem_ctx, &obj->governsID_oid);
1151         if (!W_ERROR_IS_OK(status)) {
1152                 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
1153                         __location__, obj->lDAPDisplayName, obj->governsID_id,
1154                         win_errstr(status)));
1155                 return status;
1156         }
1157         GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
1158
1159         GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
1160         GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
1161         GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
1162
1163         GET_STRING_DS(schema, r, "subClassOf", mem_ctx, obj, subClassOf, true);
1164
1165         obj->systemAuxiliaryClass       = NULL;
1166         obj->systemPossSuperiors        = NULL;
1167         obj->systemMustContain          = NULL;
1168         obj->systemMayContain           = NULL;
1169
1170         obj->auxiliaryClass             = NULL;
1171         obj->possSuperiors              = NULL;
1172         obj->mustContain                = NULL;
1173         obj->mayContain                 = NULL;
1174
1175         obj->possibleInferiors          = NULL;
1176
1177         GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
1178
1179         GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
1180         GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
1181
1182         GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
1183         GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
1184         GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, false);
1185         GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, false);
1186         GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, false);
1187         GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, false);
1188         GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, false);
1189
1190         return WERR_OK;
1191 }
1192
1193 const struct dsdb_attribute *dsdb_attribute_by_attributeID_id(const struct dsdb_schema *schema,
1194                                                               uint32_t id)
1195 {
1196         struct dsdb_attribute *cur;
1197
1198         /*
1199          * 0xFFFFFFFF is used as value when no mapping table is available,
1200          * so don't try to match with it
1201          */
1202         if (id == 0xFFFFFFFF) return NULL;
1203
1204         /* TODO: add binary search */
1205         for (cur = schema->attributes; cur; cur = cur->next) {
1206                 if (cur->attributeID_id != id) continue;
1207
1208                 return cur;
1209         }
1210
1211         return NULL;
1212 }
1213
1214 const struct dsdb_attribute *dsdb_attribute_by_attributeID_oid(const struct dsdb_schema *schema,
1215                                                                const char *oid)
1216 {
1217         struct dsdb_attribute *cur;
1218
1219         if (!oid) return NULL;
1220
1221         /* TODO: add binary search */
1222         for (cur = schema->attributes; cur; cur = cur->next) {
1223                 if (strcmp(cur->attributeID_oid, oid) != 0) continue;
1224
1225                 return cur;
1226         }
1227
1228         return NULL;
1229 }
1230
1231 const struct dsdb_attribute *dsdb_attribute_by_lDAPDisplayName(const struct dsdb_schema *schema,
1232                                                                const char *name)
1233 {
1234         struct dsdb_attribute *cur;
1235
1236         if (!name) return NULL;
1237
1238         /* TODO: add binary search */
1239         for (cur = schema->attributes; cur; cur = cur->next) {
1240                 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
1241
1242                 return cur;
1243         }
1244
1245         return NULL;
1246 }
1247
1248 const struct dsdb_attribute *dsdb_attribute_by_linkID(const struct dsdb_schema *schema,
1249                                                       int linkID)
1250 {
1251         struct dsdb_attribute *cur;
1252
1253         /* TODO: add binary search */
1254         for (cur = schema->attributes; cur; cur = cur->next) {
1255                 if (cur->linkID != linkID) continue;
1256
1257                 return cur;
1258         }
1259
1260         return NULL;
1261 }
1262
1263 const struct dsdb_class *dsdb_class_by_governsID_id(const struct dsdb_schema *schema,
1264                                                     uint32_t id)
1265 {
1266         struct dsdb_class *cur;
1267
1268         /*
1269          * 0xFFFFFFFF is used as value when no mapping table is available,
1270          * so don't try to match with it
1271          */
1272         if (id == 0xFFFFFFFF) return NULL;
1273
1274         /* TODO: add binary search */
1275         for (cur = schema->classes; cur; cur = cur->next) {
1276                 if (cur->governsID_id != id) continue;
1277
1278                 return cur;
1279         }
1280
1281         return NULL;
1282 }
1283
1284 const struct dsdb_class *dsdb_class_by_governsID_oid(const struct dsdb_schema *schema,
1285                                                      const char *oid)
1286 {
1287         struct dsdb_class *cur;
1288
1289         if (!oid) return NULL;
1290
1291         /* TODO: add binary search */
1292         for (cur = schema->classes; cur; cur = cur->next) {
1293                 if (strcmp(cur->governsID_oid, oid) != 0) continue;
1294
1295                 return cur;
1296         }
1297
1298         return NULL;
1299 }
1300
1301 const struct dsdb_class *dsdb_class_by_lDAPDisplayName(const struct dsdb_schema *schema,
1302                                                        const char *name)
1303 {
1304         struct dsdb_class *cur;
1305
1306         if (!name) return NULL;
1307
1308         /* TODO: add binary search */
1309         for (cur = schema->classes; cur; cur = cur->next) {
1310                 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
1311
1312                 return cur;
1313         }
1314
1315         return NULL;
1316 }
1317
1318 const struct dsdb_class *dsdb_class_by_cn(const struct dsdb_schema *schema,
1319                                           const char *cn)
1320 {
1321         struct dsdb_class *cur;
1322
1323         if (!cn) return NULL;
1324
1325         /* TODO: add binary search */
1326         for (cur = schema->classes; cur; cur = cur->next) {
1327                 if (strcasecmp(cur->cn, cn) != 0) continue;
1328
1329                 return cur;
1330         }
1331
1332         return NULL;
1333 }
1334
1335 const char *dsdb_lDAPDisplayName_by_id(const struct dsdb_schema *schema,
1336                                        uint32_t id)
1337 {
1338         const struct dsdb_attribute *a;
1339         const struct dsdb_class *c;
1340
1341         /* TODO: add binary search */
1342         a = dsdb_attribute_by_attributeID_id(schema, id);
1343         if (a) {
1344                 return a->lDAPDisplayName;
1345         }
1346
1347         c = dsdb_class_by_governsID_id(schema, id);
1348         if (c) {
1349                 return c->lDAPDisplayName;
1350         }
1351
1352         return NULL;
1353 }
1354
1355 /** 
1356     Return a list of linked attributes, in lDAPDisplayName format.
1357
1358     This may be used to determine if a modification would require
1359     backlinks to be updated, for example
1360 */
1361
1362 WERROR dsdb_linked_attribute_lDAPDisplayName_list(const struct dsdb_schema *schema, TALLOC_CTX *mem_ctx, const char ***attr_list_ret)
1363 {
1364         const char **attr_list = NULL;
1365         struct dsdb_attribute *cur;
1366         int i = 0;
1367         for (cur = schema->attributes; cur; cur = cur->next) {
1368                 if (cur->linkID == 0) continue;
1369                 
1370                 attr_list = talloc_realloc(mem_ctx, attr_list, const char *, i+2);
1371                 if (!attr_list) {
1372                         return WERR_NOMEM;
1373                 }
1374                 attr_list[i] = cur->lDAPDisplayName;
1375                 i++;
1376         }
1377         attr_list[i] = NULL;
1378         *attr_list_ret = attr_list;
1379         return WERR_OK;
1380 }
1381
1382 static char **merge_attr_list(TALLOC_CTX *mem_ctx, 
1383                               char **attrs, const char **new_attrs) 
1384 {
1385         char **ret_attrs;
1386         int i;
1387         size_t new_len, orig_len = str_list_length((const char **)attrs);
1388         if (!new_attrs) {
1389                 return attrs;
1390         }
1391
1392         ret_attrs = talloc_realloc(mem_ctx, 
1393                                    attrs, char *, orig_len + str_list_length(new_attrs) + 1);
1394         if (ret_attrs) {
1395                 for (i=0; i < str_list_length(new_attrs); i++) {
1396                         ret_attrs[orig_len + i] = new_attrs[i];
1397                 }
1398                 new_len = orig_len + str_list_length(new_attrs);
1399
1400                 ret_attrs[new_len] = NULL;
1401
1402         }
1403
1404         return ret_attrs;
1405 }
1406
1407 char **dsdb_full_attribute_list_internal(TALLOC_CTX *mem_ctx, 
1408                                          const struct dsdb_schema *schema, 
1409                                          const char **class_list,
1410                                          enum dsdb_attr_list_query query)
1411 {
1412         int i;
1413         const struct dsdb_class *class;
1414         
1415         char **attr_list = NULL;
1416         char **recursive_list;
1417
1418         for (i=0; class_list && class_list[i]; i++) {
1419                 class = dsdb_class_by_lDAPDisplayName(schema, class_list[i]);
1420                 
1421                 switch (query) {
1422                 case DSDB_SCHEMA_ALL_MAY:
1423                         attr_list = merge_attr_list(mem_ctx, attr_list, class->mayContain);
1424                         attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMayContain);
1425                         break;
1426
1427                 case DSDB_SCHEMA_ALL_MUST:
1428                         attr_list = merge_attr_list(mem_ctx, attr_list, class->mustContain);
1429                         attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMustContain);
1430                         break;
1431
1432                 case DSDB_SCHEMA_SYS_MAY:
1433                         attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMayContain);
1434                         break;
1435
1436                 case DSDB_SCHEMA_SYS_MUST:
1437                         attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMustContain);
1438                         break;
1439
1440                 case DSDB_SCHEMA_MAY:
1441                         attr_list = merge_attr_list(mem_ctx, attr_list, class->mayContain);
1442                         break;
1443
1444                 case DSDB_SCHEMA_MUST:
1445                         attr_list = merge_attr_list(mem_ctx, attr_list, class->mustContain);
1446                         break;
1447
1448                 case DSDB_SCHEMA_ALL:
1449                         attr_list = merge_attr_list(mem_ctx, attr_list, class->mayContain);
1450                         attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMayContain);
1451                         attr_list = merge_attr_list(mem_ctx, attr_list, class->mustContain);
1452                         attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMustContain);
1453                         break;
1454                 }
1455
1456                 recursive_list = dsdb_full_attribute_list_internal(mem_ctx, schema, 
1457                                                                    class->systemAuxiliaryClass, 
1458                                                                    query);
1459                 
1460                 attr_list = merge_attr_list(mem_ctx, attr_list, (const char **)recursive_list);
1461                 
1462                 recursive_list = dsdb_full_attribute_list_internal(mem_ctx, schema, 
1463                                                                    class->auxiliaryClass, 
1464                                                                    query);
1465
1466                 attr_list = merge_attr_list(mem_ctx, attr_list, (const char **)recursive_list);
1467                 
1468         }
1469         return attr_list;
1470 }
1471
1472 char **dsdb_full_attribute_list(TALLOC_CTX *mem_ctx, 
1473                                 const struct dsdb_schema *schema, 
1474                                 const char **class_list,
1475                                 enum dsdb_attr_list_query query)
1476 {
1477         char **attr_list = dsdb_full_attribute_list_internal(mem_ctx, schema, class_list, query);
1478         size_t new_len = str_list_length((const char **)attr_list);
1479
1480         /* Remove duplicates */
1481         if (new_len > 1) {
1482                 int i;
1483                 qsort(attr_list, new_len,
1484                       sizeof(*attr_list),
1485                       (comparison_fn_t)strcasecmp);
1486                 
1487                 for (i=1 ; i < new_len; i++) {
1488                         char **val1 = &attr_list[i-1];
1489                         char **val2 = &attr_list[i];
1490                         if (ldb_attr_cmp(*val1, *val2) == 0) {
1491                                 memmove(val1, val2, (new_len - i) * sizeof( *attr_list)); 
1492                                 new_len--;
1493                                 i--;
1494                         }
1495                 }
1496         }
1497         return attr_list;
1498 }
1499 /**
1500  * Attach the schema to an opaque pointer on the ldb, so ldb modules
1501  * can find it 
1502  */
1503
1504 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
1505 {
1506         int ret;
1507
1508         ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
1509         if (ret != LDB_SUCCESS) {
1510                 return ret;
1511         }
1512
1513         talloc_steal(ldb, schema);
1514
1515         return LDB_SUCCESS;
1516 }
1517
1518 /**
1519  * Global variable to hold one copy of the schema, used to avoid memory bloat
1520  */
1521 static struct dsdb_schema *global_schema;
1522
1523 /**
1524  * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
1525  */
1526 int dsdb_set_global_schema(struct ldb_context *ldb)
1527 {
1528         int ret;
1529         if (!global_schema) {
1530                 return LDB_SUCCESS;
1531         }
1532         ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
1533         if (ret != LDB_SUCCESS) {
1534                 return ret;
1535         }
1536
1537         return LDB_SUCCESS;
1538 }
1539
1540 /**
1541  * Find the schema object for this ldb
1542  */
1543
1544 struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb)
1545 {
1546         const void *p;
1547         struct dsdb_schema *schema;
1548
1549         /* see if we have a cached copy */
1550         p = ldb_get_opaque(ldb, "dsdb_schema");
1551         if (!p) {
1552                 return NULL;
1553         }
1554
1555         schema = talloc_get_type(p, struct dsdb_schema);
1556         if (!schema) {
1557                 return NULL;
1558         }
1559
1560         return schema;
1561 }
1562
1563 /**
1564  * Make the schema found on this ldb the 'global' schema
1565  */
1566
1567 void dsdb_make_schema_global(struct ldb_context *ldb)
1568 {
1569         struct dsdb_schema *schema = dsdb_get_schema(ldb);
1570         if (!schema) {
1571                 return;
1572         }
1573
1574         talloc_steal(talloc_autofree_context(), schema);
1575         global_schema = schema;
1576
1577         dsdb_set_global_schema(ldb);
1578 }
1579
1580
1581 /**
1582  * Rather than read a schema from the LDB itself, read it from an ldif
1583  * file.  This allows schema to be loaded and used while adding the
1584  * schema itself to the directory.
1585  */
1586
1587 WERROR dsdb_attach_schema_from_ldif_file(struct ldb_context *ldb, const char *pf, const char *df)
1588 {
1589         struct ldb_ldif *ldif;
1590         struct ldb_message *msg;
1591         TALLOC_CTX *mem_ctx;
1592         WERROR status;
1593         int ret;
1594         struct dsdb_schema *schema;
1595         const struct ldb_val *prefix_val;
1596         const struct ldb_val *info_val;
1597         struct ldb_val info_val_default;
1598
1599         mem_ctx = talloc_new(ldb);
1600         if (!mem_ctx) {
1601                 goto nomem;
1602         }
1603
1604         schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")));
1605
1606         schema->fsmo.we_are_master = true;
1607         schema->fsmo.master_dn = ldb_dn_new_fmt(schema, ldb, "@PROVISION_SCHEMA_MASTER");
1608         if (!schema->fsmo.master_dn) {
1609                 goto nomem;
1610         }
1611
1612         /*
1613          * load the prefixMap attribute from pf
1614          */
1615         ldif = ldb_ldif_read_string(ldb, &pf);
1616         if (!ldif) {
1617                 status = WERR_INVALID_PARAM;
1618                 goto failed;
1619         }
1620         talloc_steal(mem_ctx, ldif);
1621
1622         msg = ldb_msg_canonicalize(ldb, ldif->msg);
1623         if (!msg) {
1624                 goto nomem;
1625         }
1626         talloc_steal(mem_ctx, msg);
1627         talloc_free(ldif);
1628
1629         prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
1630         if (!prefix_val) {
1631                 status = WERR_INVALID_PARAM;
1632                 goto failed;
1633         }
1634
1635         info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
1636         if (!info_val) {
1637                 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
1638                 if (!info_val_default.data) {
1639                         goto nomem;
1640                 }
1641                 talloc_steal(mem_ctx, info_val_default.data);
1642                 info_val = &info_val_default;
1643         }
1644
1645         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
1646         if (!W_ERROR_IS_OK(status)) {
1647                 goto failed;
1648         }
1649
1650         /*
1651          * load the attribute and class definitions outof df
1652          */
1653         while ((ldif = ldb_ldif_read_string(ldb, &df))) {
1654                 bool is_sa;
1655                 bool is_sc;
1656
1657                 talloc_steal(mem_ctx, ldif);
1658
1659                 msg = ldb_msg_canonicalize(ldb, ldif->msg);
1660                 if (!msg) {
1661                         goto nomem;
1662                 }
1663
1664                 talloc_steal(mem_ctx, msg);
1665                 talloc_free(ldif);
1666
1667                 is_sa = ldb_msg_check_string_attribute(msg, "objectClass", "attributeSchema");
1668                 is_sc = ldb_msg_check_string_attribute(msg, "objectClass", "classSchema");
1669
1670                 if (is_sa) {
1671                         struct dsdb_attribute *sa;
1672
1673                         sa = talloc_zero(schema, struct dsdb_attribute);
1674                         if (!sa) {
1675                                 goto nomem;
1676                         }
1677
1678                         status = dsdb_attribute_from_ldb(schema, msg, sa, sa);
1679                         if (!W_ERROR_IS_OK(status)) {
1680                                 goto failed;
1681                         }
1682
1683                         DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
1684                 } else if (is_sc) {
1685                         struct dsdb_class *sc;
1686
1687                         sc = talloc_zero(schema, struct dsdb_class);
1688                         if (!sc) {
1689                                 goto nomem;
1690                         }
1691
1692                         status = dsdb_class_from_ldb(schema, msg, sc, sc);
1693                         if (!W_ERROR_IS_OK(status)) {
1694                                 goto failed;
1695                         }
1696
1697                         DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
1698                 }
1699         }
1700
1701         ret = dsdb_set_schema(ldb, schema);
1702         if (ret != LDB_SUCCESS) {
1703                 status = WERR_FOOBAR;
1704                 goto failed;
1705         }
1706
1707         goto done;
1708
1709 nomem:
1710         status = WERR_NOMEM;
1711 failed:
1712 done:
1713         talloc_free(mem_ctx);
1714         return status;
1715 }