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