Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into 4-0-abartlet
[tprouty/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         return dsdb_find_prefix_for_oid(schema->num_prefixes, schema->prefixes, in, out);
272 }
273
274
275 WERROR dsdb_map_int2oid(const struct dsdb_schema *schema, uint32_t in, TALLOC_CTX *mem_ctx, const char **out)
276 {
277         uint32_t i;
278
279         for (i=0; i < schema->num_prefixes; i++) {
280                 const char *val;
281                 if (schema->prefixes[i].id != (in & 0xFFFF0000)) {
282                         continue;
283                 }
284
285                 val = talloc_asprintf(mem_ctx, "%s%u",
286                                       schema->prefixes[i].oid,
287                                       in & 0xFFFF);
288                 W_ERROR_HAVE_NO_MEMORY(val);
289
290                 *out = val;
291                 return WERR_OK;
292         }
293
294         return WERR_DS_NO_MSDS_INTID;
295 }
296
297 /*
298  * this function is called from within a ldb transaction from the schema_fsmo module
299  */
300 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
301 {
302         WERROR status;
303         uint32_t num_prefixes;
304         struct dsdb_schema_oid_prefix *prefixes;
305         TALLOC_CTX *mem_ctx;
306         uint32_t out;
307
308         mem_ctx = talloc_new(ldb);
309         W_ERROR_HAVE_NO_MEMORY(mem_ctx);
310
311         /* Read prefixes from disk*/
312         status = dsdb_read_prefixes_from_ldb( mem_ctx, ldb, &num_prefixes, &prefixes ); 
313         if (!W_ERROR_IS_OK(status)) {
314                 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
315                         win_errstr(status)));
316                 talloc_free(mem_ctx);
317                 return status;
318         }
319
320         /* Check if there is a prefix for the oid in the prefixes array*/
321         status = dsdb_find_prefix_for_oid( num_prefixes, prefixes, full_oid, &out ); 
322         if (W_ERROR_IS_OK(status)) {
323                 /* prefix found*/
324                 talloc_free(mem_ctx);
325                 return status;
326         } else if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
327                 /* error */
328                 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
329                         win_errstr(status)));
330                 talloc_free(mem_ctx);
331                 return status;
332         }
333
334         /* Create the new mapping for the prefix of full_oid */
335         status = dsdb_prefix_map_update(mem_ctx, &num_prefixes, &prefixes, full_oid);
336         if (!W_ERROR_IS_OK(status)) {
337                 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_prefix_map_update: %s\n",
338                         win_errstr(status)));
339                 talloc_free(mem_ctx);
340                 return status;
341         }
342
343         /* Update prefixMap in ldb*/
344         status = dsdb_write_prefixes_to_ldb(mem_ctx, ldb, num_prefixes, prefixes);
345         if (!W_ERROR_IS_OK(status)) {
346                 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
347                         win_errstr(status)));
348                 talloc_free(mem_ctx);
349                 return status;
350         }
351
352         talloc_free(mem_ctx);
353         return status;
354 }
355
356 WERROR dsdb_prefix_map_update(TALLOC_CTX *mem_ctx, uint32_t *num_prefixes, struct dsdb_schema_oid_prefix **prefixes, const char *oid)
357 {
358         uint32_t new_num_prefixes, index_new_prefix, new_entry_id;
359         const char* lastDotOffset;
360         size_t size;
361         
362         new_num_prefixes = *num_prefixes + 1;
363         index_new_prefix = *num_prefixes;
364
365         /*
366          * this is the algorithm we use to create new mappings for now
367          *
368          * TODO: find what algorithm windows use
369          */
370         new_entry_id = (*num_prefixes)<<16;
371
372         /* Extract the prefix from the oid*/
373         lastDotOffset = strrchr(oid, '.');
374         if (lastDotOffset == NULL) {
375                 DEBUG(0,("dsdb_prefix_map_update: failed to find the last dot\n"));
376                 return WERR_NOT_FOUND;
377         }
378
379         /* Calculate the size of the remainig string that should be the prefix of it */
380         size = strlen(oid) - strlen(lastDotOffset);
381         if (size <= 0) {
382                 DEBUG(0,("dsdb_prefix_map_update: size of the remaining string invalid\n"));
383                 return WERR_FOOBAR;
384         }
385         /* Add one because we need to copy the dot */
386         size += 1;
387
388         /* Create a spot in the prefixMap for one more prefix*/
389         (*prefixes) = talloc_realloc(mem_ctx, *prefixes, struct dsdb_schema_oid_prefix, new_num_prefixes);
390         W_ERROR_HAVE_NO_MEMORY(*prefixes);
391
392         /* Add the new prefix entry*/
393         (*prefixes)[index_new_prefix].id = new_entry_id;
394         (*prefixes)[index_new_prefix].oid = talloc_strndup(mem_ctx, oid, size);
395         (*prefixes)[index_new_prefix].oid_len = strlen((*prefixes)[index_new_prefix].oid);
396
397         /* Increase num_prefixes because new prefix has been added */
398         ++(*num_prefixes);
399
400         return WERR_OK;
401 }
402
403 WERROR dsdb_find_prefix_for_oid(uint32_t num_prefixes, const struct dsdb_schema_oid_prefix *prefixes, const char *in, uint32_t *out)
404 {
405         uint32_t i;
406
407         for (i=0; i < num_prefixes; i++) {
408                 const char *val_str;
409                 char *end_str;
410                 unsigned val;
411
412                 if (strncmp(prefixes[i].oid, in, prefixes[i].oid_len) != 0) {
413                         continue;
414                 }
415
416                 val_str = in + prefixes[i].oid_len;
417                 end_str = NULL;
418                 errno = 0;
419
420                 if (val_str[0] == '\0') {
421                         return WERR_INVALID_PARAM;
422                 }
423
424                 /* two '.' chars are invalid */
425                 if (val_str[0] == '.') {
426                         return WERR_INVALID_PARAM;
427                 }
428
429                 val = strtoul(val_str, &end_str, 10);
430                 if (end_str[0] == '.' && end_str[1] != '\0') {
431                         /*
432                          * if it's a '.' and not the last char
433                          * then maybe an other mapping apply
434                          */
435                         continue;
436                 } else if (end_str[0] != '\0') {
437                         return WERR_INVALID_PARAM;
438                 } else if (val > 0xFFFF) {
439                         return WERR_INVALID_PARAM;
440                 }
441
442                 *out = prefixes[i].id | val;
443                 return WERR_OK;
444         }
445
446         return WERR_DS_NO_MSDS_INTID;
447 }
448
449 WERROR dsdb_write_prefixes_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
450                                   uint32_t num_prefixes,
451                                   const struct dsdb_schema_oid_prefix *prefixes)
452 {
453         struct ldb_message msg;
454         struct ldb_dn *schema_dn;
455         struct ldb_message_element el;
456         struct prefixMapBlob pm;
457         struct ldb_val ndr_blob;
458         enum ndr_err_code ndr_err;
459         uint32_t i;
460         int ret;
461         
462         schema_dn = samdb_schema_dn(ldb);
463         if (!schema_dn) {
464                 DEBUG(0,("dsdb_write_prefixes_to_ldb: no schema dn present\n"));        
465                 return WERR_FOOBAR;
466         }
467
468         pm.version                      = PREFIX_MAP_VERSION_DSDB;
469         pm.ctr.dsdb.num_mappings        = num_prefixes;
470         pm.ctr.dsdb.mappings            = talloc_array(mem_ctx,
471                                                 struct drsuapi_DsReplicaOIDMapping,
472                                                 pm.ctr.dsdb.num_mappings);
473         if (!pm.ctr.dsdb.mappings) {
474                 return WERR_NOMEM;
475         }
476
477         for (i=0; i < num_prefixes; i++) {
478                 pm.ctr.dsdb.mappings[i].id_prefix = prefixes[i].id>>16;
479                 pm.ctr.dsdb.mappings[i].oid.oid = talloc_strdup(pm.ctr.dsdb.mappings, prefixes[i].oid);
480         }
481
482         ndr_err = ndr_push_struct_blob(&ndr_blob, ldb,
483                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
484                                        &pm,
485                                        (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
486         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
487                 return WERR_FOOBAR;
488         }
489  
490         el.num_values = 1;
491         el.values = &ndr_blob;
492         el.flags = LDB_FLAG_MOD_REPLACE;
493         el.name = talloc_strdup(mem_ctx, "prefixMap");
494  
495         msg.dn = ldb_dn_copy(mem_ctx, schema_dn);
496         msg.num_elements = 1;
497         msg.elements = &el;
498  
499         ret = ldb_modify( ldb, &msg );
500         if (ret != 0) {
501                 DEBUG(0,("dsdb_write_prefixes_to_ldb: ldb_modify failed\n"));   
502                 return WERR_FOOBAR;
503         }
504  
505         return WERR_OK;
506 }
507
508 WERROR dsdb_read_prefixes_from_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, uint32_t* num_prefixes, struct dsdb_schema_oid_prefix **prefixes)
509 {
510         struct prefixMapBlob *blob;
511         enum ndr_err_code ndr_err;
512         uint32_t i;
513         const struct ldb_val *prefix_val;
514         struct ldb_dn *schema_dn;
515         struct ldb_result *schema_res;
516         int ret;    
517         static const char *schema_attrs[] = {
518                 "prefixMap",
519                 NULL
520         };
521
522         schema_dn = samdb_schema_dn(ldb);
523         if (!schema_dn) {
524                 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
525                 return WERR_FOOBAR;
526         }
527
528         ret = ldb_search(ldb, schema_dn, LDB_SCOPE_BASE,NULL, schema_attrs,&schema_res);
529         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
530                 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
531                 return WERR_FOOBAR;
532         } else if (ret != LDB_SUCCESS) {
533                 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
534                 return WERR_FOOBAR;
535         }
536
537         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
538         if (!prefix_val) {
539                 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
540                 return WERR_FOOBAR;
541         }
542
543         blob = talloc(mem_ctx, struct prefixMapBlob);
544         W_ERROR_HAVE_NO_MEMORY(blob);
545
546         ndr_err = ndr_pull_struct_blob(prefix_val, blob, 
547                                            lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
548                                            blob,
549                                            (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
550         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
551                 DEBUG(0,("dsdb_read_prefixes_from_ldb: ndr_pull_struct_blob failed\n"));
552                 talloc_free(blob);
553                 return WERR_FOOBAR;
554         }
555
556         if (blob->version != PREFIX_MAP_VERSION_DSDB) {
557                 DEBUG(0,("dsdb_read_prefixes_from_ldb: blob->version incorect\n"));
558                 talloc_free(blob);
559                 return WERR_FOOBAR;
560         }
561         
562         *num_prefixes = blob->ctr.dsdb.num_mappings;
563         *prefixes = talloc_array(mem_ctx, struct dsdb_schema_oid_prefix, *num_prefixes);
564         if(!(*prefixes)) {
565                 talloc_free(blob);
566                 return WERR_NOMEM;
567         }
568         for (i=0; i < blob->ctr.dsdb.num_mappings; i++) {
569                 char *oid;
570                 (*prefixes)[i].id = blob->ctr.dsdb.mappings[i].id_prefix<<16;
571                 oid = talloc_strdup(mem_ctx, blob->ctr.dsdb.mappings[i].oid.oid);
572                 (*prefixes)[i].oid = talloc_asprintf_append(oid, "."); 
573                 (*prefixes)[i].oid_len = strlen(blob->ctr.dsdb.mappings[i].oid.oid);
574         }
575
576         talloc_free(blob);
577         return WERR_OK;
578 }
579
580 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
581         (p)->elem = samdb_result_string(msg, attr, NULL);\
582         if (strict && (p)->elem == NULL) { \
583                 d_printf("%s: %s == NULL\n", __location__, attr); \
584                 return WERR_INVALID_PARAM; \
585         } \
586         talloc_steal(mem_ctx, (p)->elem); \
587 } while (0)
588
589 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do {   \
590         int get_string_list_counter;                                    \
591         struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
592         if (get_string_list_el == NULL) {                               \
593                 if (strict) {                                           \
594                         d_printf("%s: %s == NULL\n", __location__, attr); \
595                         return WERR_INVALID_PARAM;                      \
596                 } else {                                                \
597                         (p)->elem = NULL;                               \
598                         break;                                          \
599                 }                                                       \
600         }                                                               \
601         (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
602         for (get_string_list_counter=0;                                 \
603              get_string_list_counter < get_string_list_el->num_values;  \
604              get_string_list_counter++) {                               \
605                 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
606                                                                     (const char *)get_string_list_el->values[get_string_list_counter].data, \
607                                                                     get_string_list_el->values[get_string_list_counter].length); \
608                 if (!(p)->elem[get_string_list_counter]) {              \
609                         d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
610                         return WERR_NOMEM;                              \
611                 }                                                       \
612                 (p)->elem[get_string_list_counter+1] = NULL;            \
613         }                                                               \
614         talloc_steal(mem_ctx, (p)->elem);                               \
615 } while (0)
616
617 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
618         const char *str; \
619         str = samdb_result_string(msg, attr, NULL);\
620         if (str == NULL) { \
621                 if (strict) { \
622                         d_printf("%s: %s == NULL\n", __location__, attr); \
623                         return WERR_INVALID_PARAM; \
624                 } else { \
625                         (p)->elem = false; \
626                 } \
627         } else if (strcasecmp("TRUE", str) == 0) { \
628                 (p)->elem = true; \
629         } else if (strcasecmp("FALSE", str) == 0) { \
630                 (p)->elem = false; \
631         } else { \
632                 d_printf("%s: %s == %s\n", __location__, attr, str); \
633                 return WERR_INVALID_PARAM; \
634         } \
635 } while (0)
636
637 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
638         (p)->elem = samdb_result_uint(msg, attr, 0);\
639 } while (0)
640
641 #define GET_GUID_LDB(msg, attr, p, elem) do { \
642         (p)->elem = samdb_result_guid(msg, attr);\
643 } while (0)
644
645 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
646         const struct ldb_val *_val;\
647         _val = ldb_msg_find_ldb_val(msg, attr);\
648         if (_val) {\
649                 (p)->elem = *_val;\
650                 talloc_steal(mem_ctx, (p)->elem.data);\
651         } else {\
652                 ZERO_STRUCT((p)->elem);\
653         }\
654 } while (0)
655
656 WERROR dsdb_attribute_from_ldb(const struct dsdb_schema *schema,
657                                struct ldb_message *msg,
658                                TALLOC_CTX *mem_ctx,
659                                struct dsdb_attribute *attr)
660 {
661         WERROR status;
662
663         GET_STRING_LDB(msg, "cn", mem_ctx, attr, cn, false);
664         GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
665         GET_STRING_LDB(msg, "attributeID", mem_ctx, attr, attributeID_oid, true);
666         if (schema->num_prefixes == 0) {
667                 /* set an invalid value */
668                 attr->attributeID_id = 0xFFFFFFFF;
669         } else {
670                 status = dsdb_map_oid2int(schema, attr->attributeID_oid, &attr->attributeID_id);
671                 if (!W_ERROR_IS_OK(status)) {
672                         DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
673                                 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
674                                 win_errstr(status)));
675                         return status;
676                 }
677         }
678         GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
679         GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
680
681         GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
682
683         GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
684         GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
685         GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
686         GET_UINT32_LDB(msg, "linkID", attr, linkID);
687
688         GET_STRING_LDB(msg, "attributeSyntax", mem_ctx, attr, attributeSyntax_oid, true);
689         if (schema->num_prefixes == 0) {
690                 /* set an invalid value */
691                 attr->attributeSyntax_id = 0xFFFFFFFF;
692         } else {
693                 status = dsdb_map_oid2int(schema, attr->attributeSyntax_oid, &attr->attributeSyntax_id);
694                 if (!W_ERROR_IS_OK(status)) {
695                         DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
696                                 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
697                                 win_errstr(status)));
698                         return status;
699                 }
700         }
701         GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
702         GET_BLOB_LDB(msg, "oMObjectClass", mem_ctx, attr, oMObjectClass);
703
704         GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
705         GET_UINT32_LDB(msg, "rangeLower", attr, rangeLower);
706         GET_UINT32_LDB(msg, "rangeUpper", attr, rangeUpper);
707         GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
708
709         GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
710         GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
711
712         GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
713         GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
714         GET_STRING_LDB(msg, "adminDescription", mem_ctx, attr, adminDescription, false);
715         GET_STRING_LDB(msg, "classDisplayName", mem_ctx, attr, classDisplayName, false);
716         GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
717         GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
718         GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
719
720         attr->syntax = dsdb_syntax_for_attribute(attr);
721         if (!attr->syntax) {
722                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
723         }
724
725         return WERR_OK;
726 }
727
728 WERROR dsdb_class_from_ldb(const struct dsdb_schema *schema,
729                            struct ldb_message *msg,
730                            TALLOC_CTX *mem_ctx,
731                            struct dsdb_class *obj)
732 {
733         WERROR status;
734
735         GET_STRING_LDB(msg, "cn", mem_ctx, obj, cn, false);
736         GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
737         GET_STRING_LDB(msg, "governsID", mem_ctx, obj, governsID_oid, true);
738         if (schema->num_prefixes == 0) {
739                 /* set an invalid value */
740                 obj->governsID_id = 0xFFFFFFFF;
741         } else {
742                 status = dsdb_map_oid2int(schema, obj->governsID_oid, &obj->governsID_id);
743                 if (!W_ERROR_IS_OK(status)) {
744                         DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
745                                 __location__, obj->lDAPDisplayName, obj->governsID_oid,
746                                 win_errstr(status)));
747                         return status;
748                 }
749         }
750         GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
751
752         GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
753         GET_STRING_LDB(msg, "rDNAttID", mem_ctx, obj, rDNAttID, false);
754         GET_STRING_LDB(msg, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
755  
756         GET_STRING_LDB(msg, "subClassOf", mem_ctx, obj, subClassOf, true);
757
758         GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass, false);
759         GET_STRING_LIST_LDB(msg, "auxiliaryClass", mem_ctx, obj, auxiliaryClass, false);
760
761         GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, false);
762         GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, false);
763         GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, false);
764         GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, false);
765
766         GET_STRING_LIST_LDB(msg, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
767         GET_STRING_LIST_LDB(msg, "possSuperiors", mem_ctx, obj, possSuperiors, false);
768         GET_STRING_LIST_LDB(msg, "possibleInferiors", mem_ctx, obj, possibleInferiors, false);
769
770         GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
771
772         GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
773         GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
774
775         GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
776         GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
777         GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, false);
778         GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, false);
779         GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
780         GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
781         GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
782
783         return WERR_OK;
784 }
785
786 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
787
788 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
789                                  struct smb_iconv_convenience *iconv_convenience, 
790                                  struct ldb_result *schema_res,
791                                  struct ldb_result *attrs_res, struct ldb_result *objectclass_res, 
792                                  struct dsdb_schema **schema_out,
793                                  char **error_string)
794 {
795         WERROR status;
796         uint32_t i;
797         const struct ldb_val *prefix_val;
798         const struct ldb_val *info_val;
799         struct ldb_val info_val_default;
800         struct dsdb_schema *schema;
801
802         schema = dsdb_new_schema(mem_ctx, iconv_convenience);
803         if (!schema) {
804                 dsdb_oom(error_string, mem_ctx);
805                 return LDB_ERR_OPERATIONS_ERROR;
806         }
807
808         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
809         if (!prefix_val) {
810                 *error_string = talloc_asprintf(mem_ctx, 
811                                                 "schema_fsmo_init: no prefixMap attribute found");
812                 return LDB_ERR_CONSTRAINT_VIOLATION;
813         }
814         info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
815         if (!info_val) {
816                 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
817                 if (!info_val_default.data) {
818                         dsdb_oom(error_string, mem_ctx);
819                         return LDB_ERR_OPERATIONS_ERROR;
820                 }
821                 talloc_steal(mem_ctx, info_val_default.data);
822                 info_val = &info_val_default;
823         }
824
825         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
826         if (!W_ERROR_IS_OK(status)) {
827                 *error_string = talloc_asprintf(mem_ctx, 
828                               "schema_fsmo_init: failed to load oid mappings: %s",
829                               win_errstr(status));
830                 return LDB_ERR_CONSTRAINT_VIOLATION;
831         }
832
833         for (i=0; i < attrs_res->count; i++) {
834                 struct dsdb_attribute *sa;
835
836                 sa = talloc_zero(schema, struct dsdb_attribute);
837                 if (!sa) {
838                         dsdb_oom(error_string, mem_ctx);
839                         return LDB_ERR_OPERATIONS_ERROR;
840                 }
841
842                 status = dsdb_attribute_from_ldb(schema, attrs_res->msgs[i], sa, sa);
843                 if (!W_ERROR_IS_OK(status)) {
844                         *error_string = talloc_asprintf(mem_ctx, 
845                                       "schema_fsmo_init: failed to load attribute definition: %s:%s",
846                                       ldb_dn_get_linearized(attrs_res->msgs[i]->dn),
847                                       win_errstr(status));
848                         return LDB_ERR_CONSTRAINT_VIOLATION;
849                 }
850
851                 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
852         }
853
854         for (i=0; i < objectclass_res->count; i++) {
855                 struct dsdb_class *sc;
856
857                 sc = talloc_zero(schema, struct dsdb_class);
858                 if (!sc) {
859                         dsdb_oom(error_string, mem_ctx);
860                         return LDB_ERR_OPERATIONS_ERROR;
861                 }
862
863                 status = dsdb_class_from_ldb(schema, objectclass_res->msgs[i], sc, sc);
864                 if (!W_ERROR_IS_OK(status)) {
865                         *error_string = talloc_asprintf(mem_ctx, 
866                                       "schema_fsmo_init: failed to load class definition: %s:%s",
867                                       ldb_dn_get_linearized(objectclass_res->msgs[i]->dn),
868                                       win_errstr(status));
869                         return LDB_ERR_CONSTRAINT_VIOLATION;
870                 }
871
872                 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
873         }
874
875         schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
876         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
877                 schema->fsmo.we_are_master = true;
878         } else {
879                 schema->fsmo.we_are_master = false;
880         }
881
882         DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
883                   (schema->fsmo.we_are_master?"yes":"no")));
884
885         *schema_out = schema;
886         return LDB_SUCCESS;
887 }
888
889 /* This recursive load of the objectClasses presumes that they
890  * everything is in a strict subClassOf hirarchy.  
891  *
892  * We load this in order so we produce certain outputs (such as the
893  * exported schema for openldap, and sorted objectClass attribute) 'in
894  * order' */
895
896 static int fetch_oc_recursive(struct ldb_context *ldb, struct ldb_dn *schemadn, 
897                               TALLOC_CTX *mem_ctx, 
898                               struct ldb_result *search_from,
899                               struct ldb_result *res_list)
900 {
901         int i;
902         int ret = 0;
903         for (i=0; i < search_from->count; i++) {
904                 struct ldb_result *res;
905                 const char *name = ldb_msg_find_attr_as_string(search_from->msgs[i], 
906                                                                "lDAPDisplayname", NULL);
907
908                 ret = ldb_search_exp_fmt(ldb, mem_ctx, &res,
909                                         schemadn, LDB_SCOPE_SUBTREE, NULL,
910                                         "(&(&(objectClass=classSchema)(subClassOf=%s))(!(lDAPDisplayName=%s)))",
911                                         name, name);
912                 if (ret != LDB_SUCCESS) {
913                         return ret;
914                 }
915                 
916                 res_list->msgs = talloc_realloc(res_list, res_list->msgs, 
917                                                 struct ldb_message *, res_list->count + 2);
918                 if (!res_list->msgs) {
919                         return LDB_ERR_OPERATIONS_ERROR;
920                 }
921                 res_list->msgs[res_list->count] = talloc_move(res_list, 
922                                                               &search_from->msgs[i]);
923                 res_list->count++;
924                 res_list->msgs[res_list->count] = NULL;
925
926                 if (res->count > 0) {
927                         ret = fetch_oc_recursive(ldb, schemadn, mem_ctx, res, res_list); 
928                 }
929                 if (ret != LDB_SUCCESS) {
930                         return ret;
931                 }
932         }
933         return ret;
934 }
935
936 static int fetch_objectclass_schema(struct ldb_context *ldb, struct ldb_dn *schemadn, 
937                                     TALLOC_CTX *mem_ctx, 
938                                     struct ldb_result **objectclasses_res,
939                                     char **error_string)
940 {
941         TALLOC_CTX *local_ctx = talloc_new(mem_ctx);
942         struct ldb_result *top_res, *ret_res;
943         int ret;
944         if (!local_ctx) {
945                 return LDB_ERR_OPERATIONS_ERROR;
946         }
947         
948         /* Download 'top' */
949         ret = ldb_search(ldb, schemadn, LDB_SCOPE_SUBTREE, 
950                          "(&(objectClass=classSchema)(lDAPDisplayName=top))", 
951                          NULL, &top_res);
952         if (ret != LDB_SUCCESS) {
953                 *error_string = talloc_asprintf(mem_ctx, 
954                                                 "dsdb_schema: failed to search for top classSchema object: %s",
955                                                 ldb_errstring(ldb));
956                 return ret;
957         }
958
959         talloc_steal(local_ctx, top_res);
960
961         if (top_res->count != 1) {
962                 *error_string = talloc_asprintf(mem_ctx, 
963                                                 "dsdb_schema: failed to find top classSchema object");
964                 return LDB_ERR_NO_SUCH_OBJECT;
965         }
966
967         ret_res = talloc_zero(local_ctx, struct ldb_result);
968         if (!ret_res) {
969                 return LDB_ERR_OPERATIONS_ERROR;
970         }
971
972         ret = fetch_oc_recursive(ldb, schemadn, local_ctx, top_res, ret_res); 
973
974         if (ret != LDB_SUCCESS) {
975                 return ret;
976         }
977
978         *objectclasses_res = talloc_move(mem_ctx, &ret_res);
979         return ret;
980 }
981
982 int dsdb_schema_from_schema_dn(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
983                                struct smb_iconv_convenience *iconv_convenience, 
984                                struct ldb_dn *schema_dn,
985                                struct dsdb_schema **schema,
986                                char **error_string_out) 
987 {
988         TALLOC_CTX *tmp_ctx;
989         char *error_string;
990         int ret;
991
992         struct ldb_result *schema_res;
993         struct ldb_result *a_res;
994         struct ldb_result *c_res;
995         static const char *schema_attrs[] = {
996                 "prefixMap",
997                 "schemaInfo",
998                 "fSMORoleOwner",
999                 NULL
1000         };
1001
1002         tmp_ctx = talloc_new(mem_ctx);
1003         if (!tmp_ctx) {
1004                 dsdb_oom(error_string_out, mem_ctx);
1005                 return LDB_ERR_OPERATIONS_ERROR;
1006         }
1007
1008         /*
1009          * setup the prefix mappings and schema info
1010          */
1011         ret = ldb_search(ldb, schema_dn,
1012                          LDB_SCOPE_BASE,
1013                          NULL, schema_attrs,
1014                          &schema_res);
1015         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1016                 talloc_free(tmp_ctx);
1017                 return ret;
1018         } else if (ret != LDB_SUCCESS) {
1019                 *error_string_out = talloc_asprintf(mem_ctx, 
1020                                        "dsdb_schema: failed to search the schema head: %s",
1021                                        ldb_errstring(ldb));
1022                 talloc_free(tmp_ctx);
1023                 return ret;
1024         }
1025         talloc_steal(tmp_ctx, schema_res);
1026         if (schema_res->count != 1) {
1027                 *error_string_out = talloc_asprintf(mem_ctx, 
1028                               "dsdb_schema: [%u] schema heads found on a base search",
1029                               schema_res->count);
1030                 talloc_free(tmp_ctx);
1031                 return LDB_ERR_CONSTRAINT_VIOLATION;
1032         }
1033
1034         /*
1035          * load the attribute definitions
1036          */
1037         ret = ldb_search(ldb, schema_dn,
1038                          LDB_SCOPE_ONELEVEL,
1039                          "(objectClass=attributeSchema)", NULL,
1040                          &a_res);
1041         if (ret != LDB_SUCCESS) {
1042                 *error_string_out = talloc_asprintf(mem_ctx, 
1043                                        "dsdb_schema: failed to search attributeSchema objects: %s",
1044                                        ldb_errstring(ldb));
1045                 talloc_free(tmp_ctx);
1046                 return ret;
1047         }
1048         talloc_steal(tmp_ctx, a_res);
1049
1050         /*
1051          * load the objectClass definitions
1052          */
1053         ret = fetch_objectclass_schema(ldb, schema_dn, tmp_ctx, &c_res, &error_string);
1054         if (ret != LDB_SUCCESS) {
1055                 *error_string_out = talloc_asprintf(mem_ctx, 
1056                                        "Failed to fetch objectClass schema elements: %s", error_string);
1057                 talloc_free(tmp_ctx);
1058                 return ret;
1059         }
1060
1061         ret = dsdb_schema_from_ldb_results(tmp_ctx, ldb,
1062                                            lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
1063                                            schema_res, a_res, c_res, schema, &error_string);
1064         if (ret != LDB_SUCCESS) {
1065                 *error_string_out = talloc_asprintf(mem_ctx, 
1066                                                     "dsdb_schema load failed: %s",
1067                                                     error_string);
1068                 talloc_free(tmp_ctx);
1069                 return ret;
1070         }
1071         talloc_steal(mem_ctx, *schema);
1072         talloc_free(tmp_ctx);
1073
1074         return LDB_SUCCESS;
1075 }       
1076
1077
1078 static const struct {
1079         const char *name;
1080         const char *oid;
1081 } name_mappings[] = {
1082         { "cn",                                 "2.5.4.3" },
1083         { "name",                               "1.2.840.113556.1.4.1" },
1084         { "lDAPDisplayName",                    "1.2.840.113556.1.2.460" },
1085         { "attributeID",                        "1.2.840.113556.1.2.30" },
1086         { "schemaIDGUID",                       "1.2.840.113556.1.4.148" },
1087         { "mAPIID",                             "1.2.840.113556.1.2.49" },
1088         { "attributeSecurityGUID",              "1.2.840.113556.1.4.149" },
1089         { "searchFlags",                        "1.2.840.113556.1.2.334" },
1090         { "systemFlags",                        "1.2.840.113556.1.4.375" },
1091         { "isMemberOfPartialAttributeSet",      "1.2.840.113556.1.4.639" },
1092         { "linkID",                             "1.2.840.113556.1.2.50" },
1093         { "attributeSyntax",                    "1.2.840.113556.1.2.32" },
1094         { "oMSyntax",                           "1.2.840.113556.1.2.231" },
1095         { "oMObjectClass",                      "1.2.840.113556.1.2.218" },
1096         { "isSingleValued",                     "1.2.840.113556.1.2.33" },
1097         { "rangeLower",                         "1.2.840.113556.1.2.34" },
1098         { "rangeUpper",                         "1.2.840.113556.1.2.35" },
1099         { "extendedCharsAllowed",               "1.2.840.113556.1.2.380" },
1100         { "schemaFlagsEx",                      "1.2.840.113556.1.4.120" },
1101         { "msDs-Schema-Extensions",             "1.2.840.113556.1.4.1440" },
1102         { "showInAdvancedViewOnly",             "1.2.840.113556.1.2.169" },
1103         { "adminDisplayName",                   "1.2.840.113556.1.2.194" },
1104         { "adminDescription",                   "1.2.840.113556.1.2.226" },
1105         { "classDisplayName",                   "1.2.840.113556.1.4.610" },
1106         { "isEphemeral",                        "1.2.840.113556.1.4.1212" },
1107         { "isDefunct",                          "1.2.840.113556.1.4.661" },
1108         { "systemOnly",                         "1.2.840.113556.1.4.170" },
1109         { "governsID",                          "1.2.840.113556.1.2.22" },
1110         { "objectClassCategory",                "1.2.840.113556.1.2.370" },
1111         { "rDNAttID",                           "1.2.840.113556.1.2.26" },
1112         { "defaultObjectCategory",              "1.2.840.113556.1.4.783" },
1113         { "subClassOf",                         "1.2.840.113556.1.2.21" },
1114         { "systemAuxiliaryClass",               "1.2.840.113556.1.4.198" },
1115         { "systemPossSuperiors",                "1.2.840.113556.1.4.195" },
1116         { "systemMustContain",                  "1.2.840.113556.1.4.197" },
1117         { "systemMayContain",                   "1.2.840.113556.1.4.196" },
1118         { "auxiliaryClass",                     "1.2.840.113556.1.2.351" },
1119         { "possSuperiors",                      "1.2.840.113556.1.2.8" },
1120         { "mustContain",                        "1.2.840.113556.1.2.24" },
1121         { "mayContain",                         "1.2.840.113556.1.2.25" },
1122         { "defaultSecurityDescriptor",          "1.2.840.113556.1.4.224" },
1123         { "defaultHidingValue",                 "1.2.840.113556.1.4.518" },
1124 };
1125
1126 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
1127                                                                      struct drsuapi_DsReplicaObject *obj,
1128                                                                      const char *name,
1129                                                                      uint32_t *idx)
1130 {
1131         WERROR status;
1132         uint32_t i, id;
1133         const char *oid = NULL;
1134
1135         for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
1136                 if (strcmp(name_mappings[i].name, name) != 0) continue;
1137
1138                 oid = name_mappings[i].oid;
1139                 break;
1140         }
1141
1142         if (!oid) {
1143                 return NULL;
1144         }
1145
1146         status = dsdb_map_oid2int(schema, oid, &id);
1147         if (!W_ERROR_IS_OK(status)) {
1148                 return NULL;
1149         }
1150
1151         for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
1152                 if (obj->attribute_ctr.attributes[i].attid != id) continue;
1153
1154                 if (idx) *idx = i;
1155                 return &obj->attribute_ctr.attributes[i];
1156         }
1157
1158         return NULL;
1159 }
1160
1161 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1162         struct drsuapi_DsReplicaAttribute *_a; \
1163         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1164         if (strict && !_a) { \
1165                 d_printf("%s: %s == NULL\n", __location__, attr); \
1166                 return WERR_INVALID_PARAM; \
1167         } \
1168         if (strict && _a->value_ctr.num_values != 1) { \
1169                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1170                         _a->value_ctr.num_values); \
1171                 return WERR_INVALID_PARAM; \
1172         } \
1173         if (_a && _a->value_ctr.num_values >= 1) { \
1174                 ssize_t _ret; \
1175                 _ret = convert_string_talloc(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
1176                                              _a->value_ctr.values[0].blob->data, \
1177                                              _a->value_ctr.values[0].blob->length, \
1178                                              (void **)discard_const(&(p)->elem)); \
1179                 if (_ret == -1) { \
1180                         DEBUG(0,("%s: invalid data!\n", attr)); \
1181                         dump_data(0, \
1182                                      _a->value_ctr.values[0].blob->data, \
1183                                      _a->value_ctr.values[0].blob->length); \
1184                         return WERR_FOOBAR; \
1185                 } \
1186         } else { \
1187                 (p)->elem = NULL; \
1188         } \
1189 } while (0)
1190
1191 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1192         struct drsuapi_DsReplicaAttribute *_a; \
1193         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1194         if (strict && !_a) { \
1195                 d_printf("%s: %s == NULL\n", __location__, attr); \
1196                 return WERR_INVALID_PARAM; \
1197         } \
1198         if (strict && _a->value_ctr.num_values != 1) { \
1199                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1200                         _a->value_ctr.num_values); \
1201                 return WERR_INVALID_PARAM; \
1202         } \
1203         if (strict && !_a->value_ctr.values[0].blob) { \
1204                 d_printf("%s: %s data == NULL\n", __location__, attr); \
1205                 return WERR_INVALID_PARAM; \
1206         } \
1207         if (_a && _a->value_ctr.num_values >= 1 \
1208             && _a->value_ctr.values[0].blob) { \
1209                 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
1210                 enum ndr_err_code _ndr_err; \
1211                 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1212                                                       mem_ctx, s->iconv_convenience, &_id3,\
1213                                                       (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
1214                 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1215                         NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1216                         return ntstatus_to_werror(_nt_status); \
1217                 } \
1218                 (p)->elem = _id3.dn; \
1219         } else { \
1220                 (p)->elem = NULL; \
1221         } \
1222 } while (0)
1223
1224 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
1225         struct drsuapi_DsReplicaAttribute *_a; \
1226         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1227         if (strict && !_a) { \
1228                 d_printf("%s: %s == NULL\n", __location__, attr); \
1229                 return WERR_INVALID_PARAM; \
1230         } \
1231         if (strict && _a->value_ctr.num_values != 1) { \
1232                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1233                          (unsigned int)_a->value_ctr.num_values);       \
1234                 return WERR_INVALID_PARAM; \
1235         } \
1236         if (strict && !_a->value_ctr.values[0].blob) { \
1237                 d_printf("%s: %s data == NULL\n", __location__, attr); \
1238                 return WERR_INVALID_PARAM; \
1239         } \
1240         if (strict && _a->value_ctr.values[0].blob->length != 4) { \
1241                 d_printf("%s: %s length == %u\n", __location__, attr, \
1242                          (unsigned int)_a->value_ctr.values[0].blob->length); \
1243                 return WERR_INVALID_PARAM; \
1244         } \
1245         if (_a && _a->value_ctr.num_values >= 1 \
1246             && _a->value_ctr.values[0].blob \
1247             && _a->value_ctr.values[0].blob->length == 4) { \
1248                 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?true:false);\
1249         } else { \
1250                 (p)->elem = false; \
1251         } \
1252 } while (0)
1253
1254 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
1255         struct drsuapi_DsReplicaAttribute *_a; \
1256         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1257         if (_a && _a->value_ctr.num_values >= 1 \
1258             && _a->value_ctr.values[0].blob \
1259             && _a->value_ctr.values[0].blob->length == 4) { \
1260                 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1261         } else { \
1262                 (p)->elem = 0; \
1263         } \
1264 } while (0)
1265
1266 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
1267         struct drsuapi_DsReplicaAttribute *_a; \
1268         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1269         if (_a && _a->value_ctr.num_values >= 1 \
1270             && _a->value_ctr.values[0].blob \
1271             && _a->value_ctr.values[0].blob->length == 16) { \
1272                 enum ndr_err_code _ndr_err; \
1273                 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1274                                                       mem_ctx, s->iconv_convenience, &(p)->elem, \
1275                                                       (ndr_pull_flags_fn_t)ndr_pull_GUID); \
1276                 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1277                         NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1278                         return ntstatus_to_werror(_nt_status); \
1279                 } \
1280         } else { \
1281                 ZERO_STRUCT((p)->elem);\
1282         } \
1283 } while (0)
1284
1285 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
1286         struct drsuapi_DsReplicaAttribute *_a; \
1287         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1288         if (_a && _a->value_ctr.num_values >= 1 \
1289             && _a->value_ctr.values[0].blob) { \
1290                 (p)->elem = *_a->value_ctr.values[0].blob;\
1291                 talloc_steal(mem_ctx, (p)->elem.data); \
1292         } else { \
1293                 ZERO_STRUCT((p)->elem);\
1294         }\
1295 } while (0)
1296
1297 WERROR dsdb_attribute_from_drsuapi(struct dsdb_schema *schema,
1298                                    struct drsuapi_DsReplicaObject *r,
1299                                    TALLOC_CTX *mem_ctx,
1300                                    struct dsdb_attribute *attr)
1301 {
1302         WERROR status;
1303
1304         GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, true);
1305         GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
1306         GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
1307         status = dsdb_map_int2oid(schema, attr->attributeID_id, mem_ctx, &attr->attributeID_oid);
1308         if (!W_ERROR_IS_OK(status)) {
1309                 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
1310                         __location__, attr->lDAPDisplayName, attr->attributeID_id,
1311                         win_errstr(status)));
1312                 return status;
1313         }
1314         GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
1315         GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
1316
1317         GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
1318
1319         GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
1320         GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
1321         GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
1322         GET_UINT32_DS(schema, r, "linkID", attr, linkID);
1323
1324         GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
1325         status = dsdb_map_int2oid(schema, attr->attributeSyntax_id, mem_ctx, &attr->attributeSyntax_oid);
1326         if (!W_ERROR_IS_OK(status)) {
1327                 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
1328                         __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
1329                         win_errstr(status)));
1330                 return status;
1331         }
1332         GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
1333         GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
1334
1335         GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, true);
1336         GET_UINT32_DS(schema, r, "rangeLower", attr, rangeLower);
1337         GET_UINT32_DS(schema, r, "rangeUpper", attr, rangeUpper);
1338         GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
1339
1340         GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
1341         GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
1342
1343         GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
1344         GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
1345         GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, false);
1346         GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, false);
1347         GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, false);
1348         GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, false);
1349         GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, false);
1350
1351         attr->syntax = dsdb_syntax_for_attribute(attr);
1352         if (!attr->syntax) {
1353                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1354         }
1355
1356         return WERR_OK;
1357 }
1358
1359 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
1360                                struct drsuapi_DsReplicaObject *r,
1361                                TALLOC_CTX *mem_ctx,
1362                                struct dsdb_class *obj)
1363 {
1364         WERROR status;
1365
1366         GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, true);
1367         GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
1368         GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
1369         status = dsdb_map_int2oid(schema, obj->governsID_id, mem_ctx, &obj->governsID_oid);
1370         if (!W_ERROR_IS_OK(status)) {
1371                 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
1372                         __location__, obj->lDAPDisplayName, obj->governsID_id,
1373                         win_errstr(status)));
1374                 return status;
1375         }
1376         GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
1377
1378         GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
1379         GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
1380         GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
1381
1382         GET_STRING_DS(schema, r, "subClassOf", mem_ctx, obj, subClassOf, true);
1383
1384         obj->systemAuxiliaryClass       = NULL;
1385         obj->systemPossSuperiors        = NULL;
1386         obj->systemMustContain          = NULL;
1387         obj->systemMayContain           = NULL;
1388
1389         obj->auxiliaryClass             = NULL;
1390         obj->possSuperiors              = NULL;
1391         obj->mustContain                = NULL;
1392         obj->mayContain                 = NULL;
1393
1394         obj->possibleInferiors          = NULL;
1395
1396         GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
1397
1398         GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
1399         GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
1400
1401         GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
1402         GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
1403         GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, false);
1404         GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, false);
1405         GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, false);
1406         GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, false);
1407         GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, false);
1408
1409         return WERR_OK;
1410 }
1411
1412 const struct dsdb_attribute *dsdb_attribute_by_attributeID_id(const struct dsdb_schema *schema,
1413                                                               uint32_t id)
1414 {
1415         struct dsdb_attribute *cur;
1416
1417         /*
1418          * 0xFFFFFFFF is used as value when no mapping table is available,
1419          * so don't try to match with it
1420          */
1421         if (id == 0xFFFFFFFF) return NULL;
1422
1423         /* TODO: add binary search */
1424         for (cur = schema->attributes; cur; cur = cur->next) {
1425                 if (cur->attributeID_id != id) continue;
1426
1427                 return cur;
1428         }
1429
1430         return NULL;
1431 }
1432
1433 const struct dsdb_attribute *dsdb_attribute_by_attributeID_oid(const struct dsdb_schema *schema,
1434                                                                const char *oid)
1435 {
1436         struct dsdb_attribute *cur;
1437
1438         if (!oid) return NULL;
1439
1440         /* TODO: add binary search */
1441         for (cur = schema->attributes; cur; cur = cur->next) {
1442                 if (strcmp(cur->attributeID_oid, oid) != 0) continue;
1443
1444                 return cur;
1445         }
1446
1447         return NULL;
1448 }
1449
1450 const struct dsdb_attribute *dsdb_attribute_by_lDAPDisplayName(const struct dsdb_schema *schema,
1451                                                                const char *name)
1452 {
1453         struct dsdb_attribute *cur;
1454
1455         if (!name) return NULL;
1456
1457         /* TODO: add binary search */
1458         for (cur = schema->attributes; cur; cur = cur->next) {
1459                 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
1460
1461                 return cur;
1462         }
1463
1464         return NULL;
1465 }
1466
1467 const struct dsdb_attribute *dsdb_attribute_by_linkID(const struct dsdb_schema *schema,
1468                                                       int linkID)
1469 {
1470         struct dsdb_attribute *cur;
1471
1472         /* TODO: add binary search */
1473         for (cur = schema->attributes; cur; cur = cur->next) {
1474                 if (cur->linkID != linkID) continue;
1475
1476                 return cur;
1477         }
1478
1479         return NULL;
1480 }
1481
1482 const struct dsdb_class *dsdb_class_by_governsID_id(const struct dsdb_schema *schema,
1483                                                     uint32_t id)
1484 {
1485         struct dsdb_class *cur;
1486
1487         /*
1488          * 0xFFFFFFFF is used as value when no mapping table is available,
1489          * so don't try to match with it
1490          */
1491         if (id == 0xFFFFFFFF) return NULL;
1492
1493         /* TODO: add binary search */
1494         for (cur = schema->classes; cur; cur = cur->next) {
1495                 if (cur->governsID_id != id) continue;
1496
1497                 return cur;
1498         }
1499
1500         return NULL;
1501 }
1502
1503 const struct dsdb_class *dsdb_class_by_governsID_oid(const struct dsdb_schema *schema,
1504                                                      const char *oid)
1505 {
1506         struct dsdb_class *cur;
1507
1508         if (!oid) return NULL;
1509
1510         /* TODO: add binary search */
1511         for (cur = schema->classes; cur; cur = cur->next) {
1512                 if (strcmp(cur->governsID_oid, oid) != 0) continue;
1513
1514                 return cur;
1515         }
1516
1517         return NULL;
1518 }
1519
1520 const struct dsdb_class *dsdb_class_by_lDAPDisplayName(const struct dsdb_schema *schema,
1521                                                        const char *name)
1522 {
1523         struct dsdb_class *cur;
1524
1525         if (!name) return NULL;
1526
1527         /* TODO: add binary search */
1528         for (cur = schema->classes; cur; cur = cur->next) {
1529                 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
1530
1531                 return cur;
1532         }
1533
1534         return NULL;
1535 }
1536
1537 const struct dsdb_class *dsdb_class_by_cn(const struct dsdb_schema *schema,
1538                                           const char *cn)
1539 {
1540         struct dsdb_class *cur;
1541
1542         if (!cn) return NULL;
1543
1544         /* TODO: add binary search */
1545         for (cur = schema->classes; cur; cur = cur->next) {
1546                 if (strcasecmp(cur->cn, cn) != 0) continue;
1547
1548                 return cur;
1549         }
1550
1551         return NULL;
1552 }
1553
1554 const char *dsdb_lDAPDisplayName_by_id(const struct dsdb_schema *schema,
1555                                        uint32_t id)
1556 {
1557         const struct dsdb_attribute *a;
1558         const struct dsdb_class *c;
1559
1560         /* TODO: add binary search */
1561         a = dsdb_attribute_by_attributeID_id(schema, id);
1562         if (a) {
1563                 return a->lDAPDisplayName;
1564         }
1565
1566         c = dsdb_class_by_governsID_id(schema, id);
1567         if (c) {
1568                 return c->lDAPDisplayName;
1569         }
1570
1571         return NULL;
1572 }
1573
1574 /** 
1575     Return a list of linked attributes, in lDAPDisplayName format.
1576
1577     This may be used to determine if a modification would require
1578     backlinks to be updated, for example
1579 */
1580
1581 WERROR dsdb_linked_attribute_lDAPDisplayName_list(const struct dsdb_schema *schema, TALLOC_CTX *mem_ctx, const char ***attr_list_ret)
1582 {
1583         const char **attr_list = NULL;
1584         struct dsdb_attribute *cur;
1585         int i = 0;
1586         for (cur = schema->attributes; cur; cur = cur->next) {
1587                 if (cur->linkID == 0) continue;
1588                 
1589                 attr_list = talloc_realloc(mem_ctx, attr_list, const char *, i+2);
1590                 if (!attr_list) {
1591                         return WERR_NOMEM;
1592                 }
1593                 attr_list[i] = cur->lDAPDisplayName;
1594                 i++;
1595         }
1596         attr_list[i] = NULL;
1597         *attr_list_ret = attr_list;
1598         return WERR_OK;
1599 }
1600
1601 char **merge_attr_list(TALLOC_CTX *mem_ctx, 
1602                        char **attrs, const char **new_attrs) 
1603 {
1604         char **ret_attrs;
1605         int i;
1606         size_t new_len, orig_len = str_list_length((const char **)attrs);
1607         if (!new_attrs) {
1608                 return attrs;
1609         }
1610
1611         ret_attrs = talloc_realloc(mem_ctx, 
1612                                    attrs, char *, orig_len + str_list_length(new_attrs) + 1);
1613         if (ret_attrs) {
1614                 for (i=0; i < str_list_length(new_attrs); i++) {
1615                         ret_attrs[orig_len + i] = new_attrs[i];
1616                 }
1617                 new_len = orig_len + str_list_length(new_attrs);
1618
1619                 ret_attrs[new_len] = NULL;
1620         }
1621
1622         return ret_attrs;
1623 }
1624
1625 /*
1626   Return a merged list of the attributes of exactly one class (not
1627   considering subclasses, auxillary classes etc)
1628 */
1629
1630 char **dsdb_attribute_list(TALLOC_CTX *mem_ctx, const struct dsdb_class *class, enum dsdb_attr_list_query query)
1631 {
1632         char **attr_list = NULL;
1633         switch (query) {
1634         case DSDB_SCHEMA_ALL_MAY:
1635                 attr_list = merge_attr_list(mem_ctx, attr_list, class->mayContain);
1636                 attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMayContain);
1637                 break;
1638                 
1639         case DSDB_SCHEMA_ALL_MUST:
1640                 attr_list = merge_attr_list(mem_ctx, attr_list, class->mustContain);
1641                 attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMustContain);
1642                 break;
1643                 
1644         case DSDB_SCHEMA_SYS_MAY:
1645                 attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMayContain);
1646                 break;
1647                 
1648         case DSDB_SCHEMA_SYS_MUST:
1649                 attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMustContain);
1650                 break;
1651                 
1652         case DSDB_SCHEMA_MAY:
1653                 attr_list = merge_attr_list(mem_ctx, attr_list, class->mayContain);
1654                 break;
1655                 
1656         case DSDB_SCHEMA_MUST:
1657                 attr_list = merge_attr_list(mem_ctx, attr_list, class->mustContain);
1658                 break;
1659                 
1660         case DSDB_SCHEMA_ALL:
1661                 attr_list = merge_attr_list(mem_ctx, attr_list, class->mayContain);
1662                 attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMayContain);
1663                 attr_list = merge_attr_list(mem_ctx, attr_list, class->mustContain);
1664                 attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMustContain);
1665                 break;
1666         }
1667         return attr_list;
1668 }
1669
1670 static char **dsdb_full_attribute_list_internal(TALLOC_CTX *mem_ctx, 
1671                                                 const struct dsdb_schema *schema, 
1672                                                 const char **class_list,
1673                                                 enum dsdb_attr_list_query query)
1674 {
1675         int i;
1676         const struct dsdb_class *class;
1677         
1678         char **attr_list = NULL;
1679         char **this_class_list;
1680         char **recursive_list;
1681
1682         for (i=0; class_list && class_list[i]; i++) {
1683                 class = dsdb_class_by_lDAPDisplayName(schema, class_list[i]);
1684                 
1685                 this_class_list = dsdb_attribute_list(mem_ctx, class, query);
1686                 attr_list = merge_attr_list(mem_ctx, attr_list, (const char **)this_class_list);
1687
1688                 recursive_list = dsdb_full_attribute_list_internal(mem_ctx, schema, 
1689                                                                    class->systemAuxiliaryClass, 
1690                                                                    query);
1691                 
1692                 attr_list = merge_attr_list(mem_ctx, attr_list, (const char **)recursive_list);
1693                 
1694                 recursive_list = dsdb_full_attribute_list_internal(mem_ctx, schema, 
1695                                                                    class->auxiliaryClass, 
1696                                                                    query);
1697                 
1698                 attr_list = merge_attr_list(mem_ctx, attr_list, (const char **)recursive_list);
1699                 
1700         }
1701         return attr_list;
1702 }
1703
1704 char **dsdb_full_attribute_list(TALLOC_CTX *mem_ctx, 
1705                                 const struct dsdb_schema *schema, 
1706                                 const char **class_list,
1707                                 enum dsdb_attr_list_query query)
1708 {
1709         char **attr_list = dsdb_full_attribute_list_internal(mem_ctx, schema, class_list, query);
1710         size_t new_len = str_list_length((const char **)attr_list);
1711
1712         /* Remove duplicates */
1713         if (new_len > 1) {
1714                 int i;
1715                 qsort(attr_list, new_len,
1716                       sizeof(*attr_list),
1717                       (comparison_fn_t)strcasecmp);
1718                 
1719                 for (i=1 ; i < new_len; i++) {
1720                         char **val1 = &attr_list[i-1];
1721                         char **val2 = &attr_list[i];
1722                         if (ldb_attr_cmp(*val1, *val2) == 0) {
1723                                 memmove(val1, val2, (new_len - i) * sizeof( *attr_list)); 
1724                                 new_len--;
1725                                 i--;
1726                         }
1727                 }
1728         }
1729         return attr_list;
1730 }
1731 /**
1732  * Attach the schema to an opaque pointer on the ldb, so ldb modules
1733  * can find it 
1734  */
1735
1736 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
1737 {
1738         int ret;
1739
1740         ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
1741         if (ret != LDB_SUCCESS) {
1742                 return ret;
1743         }
1744
1745         talloc_steal(ldb, schema);
1746
1747         return LDB_SUCCESS;
1748 }
1749
1750 /**
1751  * Global variable to hold one copy of the schema, used to avoid memory bloat
1752  */
1753 static struct dsdb_schema *global_schema;
1754
1755 /**
1756  * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
1757  */
1758 int dsdb_set_global_schema(struct ldb_context *ldb)
1759 {
1760         int ret;
1761         if (!global_schema) {
1762                 return LDB_SUCCESS;
1763         }
1764         ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
1765         if (ret != LDB_SUCCESS) {
1766                 return ret;
1767         }
1768
1769         /* Keep a reference to this schema, just incase the global copy is replaced */
1770         if (talloc_reference(ldb, global_schema) == NULL) {
1771                 return LDB_ERR_OPERATIONS_ERROR;
1772         }
1773
1774         return LDB_SUCCESS;
1775 }
1776
1777 /**
1778  * Find the schema object for this ldb
1779  */
1780
1781 struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb)
1782 {
1783         const void *p;
1784         struct dsdb_schema *schema;
1785
1786         /* see if we have a cached copy */
1787         p = ldb_get_opaque(ldb, "dsdb_schema");
1788         if (!p) {
1789                 return NULL;
1790         }
1791
1792         schema = talloc_get_type(p, struct dsdb_schema);
1793         if (!schema) {
1794                 return NULL;
1795         }
1796
1797         return schema;
1798 }
1799
1800 /**
1801  * Make the schema found on this ldb the 'global' schema
1802  */
1803
1804 void dsdb_make_schema_global(struct ldb_context *ldb)
1805 {
1806         struct dsdb_schema *schema = dsdb_get_schema(ldb);
1807         if (!schema) {
1808                 return;
1809         }
1810
1811         if (global_schema) {
1812                 talloc_unlink(talloc_autofree_context(), schema);
1813         }
1814
1815         talloc_steal(talloc_autofree_context(), schema);
1816         global_schema = schema;
1817
1818         dsdb_set_global_schema(ldb);
1819 }
1820
1821
1822 /**
1823  * Rather than read a schema from the LDB itself, read it from an ldif
1824  * file.  This allows schema to be loaded and used while adding the
1825  * schema itself to the directory.
1826  */
1827
1828 WERROR dsdb_attach_schema_from_ldif_file(struct ldb_context *ldb, const char *pf, const char *df)
1829 {
1830         struct ldb_ldif *ldif;
1831         struct ldb_message *msg;
1832         TALLOC_CTX *mem_ctx;
1833         WERROR status;
1834         int ret;
1835         struct dsdb_schema *schema;
1836         const struct ldb_val *prefix_val;
1837         const struct ldb_val *info_val;
1838         struct ldb_val info_val_default;
1839
1840         mem_ctx = talloc_new(ldb);
1841         if (!mem_ctx) {
1842                 goto nomem;
1843         }
1844
1845         schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")));
1846
1847         schema->fsmo.we_are_master = true;
1848         schema->fsmo.master_dn = ldb_dn_new_fmt(schema, ldb, "@PROVISION_SCHEMA_MASTER");
1849         if (!schema->fsmo.master_dn) {
1850                 goto nomem;
1851         }
1852
1853         /*
1854          * load the prefixMap attribute from pf
1855          */
1856         ldif = ldb_ldif_read_string(ldb, &pf);
1857         if (!ldif) {
1858                 status = WERR_INVALID_PARAM;
1859                 goto failed;
1860         }
1861         talloc_steal(mem_ctx, ldif);
1862
1863         msg = ldb_msg_canonicalize(ldb, ldif->msg);
1864         if (!msg) {
1865                 goto nomem;
1866         }
1867         talloc_steal(mem_ctx, msg);
1868         talloc_free(ldif);
1869
1870         prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
1871         if (!prefix_val) {
1872                 status = WERR_INVALID_PARAM;
1873                 goto failed;
1874         }
1875
1876         info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
1877         if (!info_val) {
1878                 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
1879                 if (!info_val_default.data) {
1880                         goto nomem;
1881                 }
1882                 talloc_steal(mem_ctx, info_val_default.data);
1883                 info_val = &info_val_default;
1884         }
1885
1886         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
1887         if (!W_ERROR_IS_OK(status)) {
1888                 goto failed;
1889         }
1890
1891         /*
1892          * load the attribute and class definitions outof df
1893          */
1894         while ((ldif = ldb_ldif_read_string(ldb, &df))) {
1895                 bool is_sa;
1896                 bool is_sc;
1897
1898                 talloc_steal(mem_ctx, ldif);
1899
1900                 msg = ldb_msg_canonicalize(ldb, ldif->msg);
1901                 if (!msg) {
1902                         goto nomem;
1903                 }
1904
1905                 talloc_steal(mem_ctx, msg);
1906                 talloc_free(ldif);
1907
1908                 is_sa = ldb_msg_check_string_attribute(msg, "objectClass", "attributeSchema");
1909                 is_sc = ldb_msg_check_string_attribute(msg, "objectClass", "classSchema");
1910
1911                 if (is_sa) {
1912                         struct dsdb_attribute *sa;
1913
1914                         sa = talloc_zero(schema, struct dsdb_attribute);
1915                         if (!sa) {
1916                                 goto nomem;
1917                         }
1918
1919                         status = dsdb_attribute_from_ldb(schema, msg, sa, sa);
1920                         if (!W_ERROR_IS_OK(status)) {
1921                                 goto failed;
1922                         }
1923
1924                         DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
1925                 } else if (is_sc) {
1926                         struct dsdb_class *sc;
1927
1928                         sc = talloc_zero(schema, struct dsdb_class);
1929                         if (!sc) {
1930                                 goto nomem;
1931                         }
1932
1933                         status = dsdb_class_from_ldb(schema, msg, sc, sc);
1934                         if (!W_ERROR_IS_OK(status)) {
1935                                 goto failed;
1936                         }
1937
1938                         DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
1939                 }
1940         }
1941
1942         ret = dsdb_set_schema(ldb, schema);
1943         if (ret != LDB_SUCCESS) {
1944                 status = WERR_FOOBAR;
1945                 goto failed;
1946         }
1947
1948         goto done;
1949
1950 nomem:
1951         status = WERR_NOMEM;
1952 failed:
1953 done:
1954         talloc_free(mem_ctx);
1955         return status;
1956 }