Ensure users with SeAddUser privs get full access to
[jra/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 #include "lib/ldb/include/ldb_module.h"
32
33 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
34 {
35         struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
36         if (!schema) {
37                 return NULL;
38         }
39
40         schema->iconv_convenience = iconv_convenience;
41         return schema;
42 }
43
44
45 WERROR dsdb_load_oid_mappings_drsuapi(struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
46 {
47         uint32_t i,j;
48
49         schema->prefixes = talloc_array(schema, struct dsdb_schema_oid_prefix, ctr->num_mappings);
50         W_ERROR_HAVE_NO_MEMORY(schema->prefixes);
51
52         for (i=0, j=0; i < ctr->num_mappings; i++) {
53                 if (ctr->mappings[i].oid.oid == NULL) {
54                         return WERR_INVALID_PARAM;
55                 }
56
57                 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
58                         if (ctr->mappings[i].id_prefix != 0) {
59                                 return WERR_INVALID_PARAM;
60                         }
61
62                         /* the magic value should be in the last array member */
63                         if (i != (ctr->num_mappings - 1)) {
64                                 return WERR_INVALID_PARAM;
65                         }
66
67                         if (ctr->mappings[i].oid.__ndr_size != 21) {
68                                 return WERR_INVALID_PARAM;
69                         }
70
71                         schema->schema_info = talloc_strdup(schema, ctr->mappings[i].oid.oid);
72                         W_ERROR_HAVE_NO_MEMORY(schema->schema_info);
73                 } else {
74                         /* the last array member should contain the magic value not a oid */
75                         if (i == (ctr->num_mappings - 1)) {
76                                 return WERR_INVALID_PARAM;
77                         }
78
79                         schema->prefixes[j].id  = ctr->mappings[i].id_prefix<<16;
80                         schema->prefixes[j].oid = talloc_asprintf(schema->prefixes, "%s.",
81                                                                   ctr->mappings[i].oid.oid);
82                         W_ERROR_HAVE_NO_MEMORY(schema->prefixes[j].oid);
83                         schema->prefixes[j].oid_len = strlen(schema->prefixes[j].oid);
84                         j++;
85                 }
86         }
87
88         schema->num_prefixes = j;
89         return WERR_OK;
90 }
91
92 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
93                                   const struct ldb_val *prefixMap,
94                                   const struct ldb_val *schemaInfo)
95 {
96         WERROR status;
97         enum ndr_err_code ndr_err;
98         struct prefixMapBlob pfm;
99         char *schema_info;
100
101         TALLOC_CTX *mem_ctx = talloc_new(schema);
102         W_ERROR_HAVE_NO_MEMORY(mem_ctx);
103         
104         ndr_err = ndr_pull_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
105         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
106                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
107                 talloc_free(mem_ctx);
108                 return ntstatus_to_werror(nt_status);
109         }
110
111         if (pfm.version != PREFIX_MAP_VERSION_DSDB) {
112                 talloc_free(mem_ctx);
113                 return WERR_FOOBAR;
114         }
115
116         if (schemaInfo->length != 21 && schemaInfo->data[0] == 0xFF) {
117                 talloc_free(mem_ctx);
118                 return WERR_FOOBAR;
119         }
120
121         /* append the schema info as last element */
122         pfm.ctr.dsdb.num_mappings++;
123         pfm.ctr.dsdb.mappings = talloc_realloc(mem_ctx, pfm.ctr.dsdb.mappings,
124                                                struct drsuapi_DsReplicaOIDMapping,
125                                                pfm.ctr.dsdb.num_mappings);
126         W_ERROR_HAVE_NO_MEMORY(pfm.ctr.dsdb.mappings);
127
128         schema_info = data_blob_hex_string(pfm.ctr.dsdb.mappings, schemaInfo);
129         W_ERROR_HAVE_NO_MEMORY(schema_info);
130
131         pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].id_prefix          = 0;    
132         pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.__ndr_size     = schemaInfo->length;
133         pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.oid            = schema_info;
134
135         /* call the drsuapi version */
136         status = dsdb_load_oid_mappings_drsuapi(schema, &pfm.ctr.dsdb);
137         talloc_free(mem_ctx);
138
139         W_ERROR_NOT_OK_RETURN(status);
140
141         return WERR_OK;
142 }
143
144 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
145                                      bool include_schema_info,
146                                      TALLOC_CTX *mem_ctx,
147                                      struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
148 {
149         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
150         uint32_t i;
151
152         ctr = talloc(mem_ctx, struct drsuapi_DsReplicaOIDMapping_Ctr);
153         W_ERROR_HAVE_NO_MEMORY(ctr);
154
155         ctr->num_mappings       = schema->num_prefixes;
156         if (include_schema_info) ctr->num_mappings++;
157         ctr->mappings = talloc_array(schema, struct drsuapi_DsReplicaOIDMapping, ctr->num_mappings);
158         W_ERROR_HAVE_NO_MEMORY(ctr->mappings);
159
160         for (i=0; i < schema->num_prefixes; i++) {
161                 ctr->mappings[i].id_prefix      = schema->prefixes[i].id>>16;
162                 ctr->mappings[i].oid.oid        = talloc_strndup(ctr->mappings,
163                                                                  schema->prefixes[i].oid,
164                                                                  schema->prefixes[i].oid_len - 1);
165                 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
166         }
167
168         if (include_schema_info) {
169                 ctr->mappings[i].id_prefix      = 0;
170                 ctr->mappings[i].oid.oid        = talloc_strdup(ctr->mappings,
171                                                                 schema->schema_info);
172                 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
173         }
174
175         *_ctr = ctr;
176         return WERR_OK;
177 }
178
179 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
180                                  TALLOC_CTX *mem_ctx,
181                                  struct ldb_val *prefixMap,
182                                  struct ldb_val *schemaInfo)
183 {
184         WERROR status;
185         enum ndr_err_code ndr_err;
186         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
187         struct prefixMapBlob pfm;
188
189         status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
190         W_ERROR_NOT_OK_RETURN(status);
191
192         pfm.version     = PREFIX_MAP_VERSION_DSDB;
193         pfm.reserved    = 0;
194         pfm.ctr.dsdb    = *ctr;
195
196         ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
197         talloc_free(ctr);
198         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
199                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
200                 return ntstatus_to_werror(nt_status);
201         }
202
203         *schemaInfo = strhex_to_data_blob(mem_ctx, schema->schema_info);
204         W_ERROR_HAVE_NO_MEMORY(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, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
529         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
530                 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
531                 talloc_free(schema_res);
532                 return WERR_FOOBAR;
533         } else if (ret != LDB_SUCCESS) {
534                 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
535                 talloc_free(schema_res);
536                 return WERR_FOOBAR;
537         }
538
539         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
540         if (!prefix_val) {
541                 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
542                 talloc_free(schema_res);
543                 return WERR_FOOBAR;
544         }
545
546         blob = talloc(mem_ctx, struct prefixMapBlob);
547         W_ERROR_HAVE_NO_MEMORY(blob);
548
549         ndr_err = ndr_pull_struct_blob(prefix_val, blob, 
550                                            lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
551                                            blob,
552                                            (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
553         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
554                 DEBUG(0,("dsdb_read_prefixes_from_ldb: ndr_pull_struct_blob failed\n"));
555                 talloc_free(blob);
556                 talloc_free(schema_res);
557                 return WERR_FOOBAR;
558         }
559
560         talloc_free(schema_res);
561
562         if (blob->version != PREFIX_MAP_VERSION_DSDB) {
563                 DEBUG(0,("dsdb_read_prefixes_from_ldb: blob->version incorect\n"));
564                 talloc_free(blob);
565                 return WERR_FOOBAR;
566         }
567         
568         *num_prefixes = blob->ctr.dsdb.num_mappings;
569         *prefixes = talloc_array(mem_ctx, struct dsdb_schema_oid_prefix, *num_prefixes);
570         if(!(*prefixes)) {
571                 talloc_free(blob);
572                 return WERR_NOMEM;
573         }
574         for (i=0; i < blob->ctr.dsdb.num_mappings; i++) {
575                 char *oid;
576                 (*prefixes)[i].id = blob->ctr.dsdb.mappings[i].id_prefix<<16;
577                 oid = talloc_strdup(mem_ctx, blob->ctr.dsdb.mappings[i].oid.oid);
578                 (*prefixes)[i].oid = talloc_asprintf_append(oid, "."); 
579                 (*prefixes)[i].oid_len = strlen(blob->ctr.dsdb.mappings[i].oid.oid);
580         }
581
582         talloc_free(blob);
583         return WERR_OK;
584 }
585
586
587 /*
588   setup the ldb_schema_attribute field for a dsdb_attribute
589  */
590 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb, 
591                                                   struct dsdb_attribute *attr)
592 {
593         const char *syntax = attr->syntax->ldb_syntax;
594         const struct ldb_schema_syntax *s;
595         struct ldb_schema_attribute *a;
596
597         if (!syntax) {
598                 syntax = attr->syntax->ldap_oid;
599         }
600
601         s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
602         if (s == NULL) {
603                 s = ldb_samba_syntax_by_name(ldb, syntax);
604         }
605         if (s == NULL) {
606                 s = ldb_standard_syntax_by_name(ldb, syntax);
607         }
608
609         if (s == NULL) {
610                 return LDB_ERR_OPERATIONS_ERROR;                
611         }
612
613         attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
614         if (attr->ldb_schema_attribute == NULL) {
615                 ldb_oom(ldb);
616                 return LDB_ERR_OPERATIONS_ERROR;
617         }
618
619         a->name = attr->lDAPDisplayName;
620         a->flags = 0;
621         a->syntax = s;
622         
623         return LDB_SUCCESS;
624 }
625
626
627
628 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
629         (p)->elem = samdb_result_string(msg, attr, NULL);\
630         if (strict && (p)->elem == NULL) { \
631                 d_printf("%s: %s == NULL\n", __location__, attr); \
632                 return WERR_INVALID_PARAM; \
633         } \
634         talloc_steal(mem_ctx, (p)->elem); \
635 } while (0)
636
637 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do {   \
638         int get_string_list_counter;                                    \
639         struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
640         if (get_string_list_el == NULL) {                               \
641                 if (strict) {                                           \
642                         d_printf("%s: %s == NULL\n", __location__, attr); \
643                         return WERR_INVALID_PARAM;                      \
644                 } else {                                                \
645                         (p)->elem = NULL;                               \
646                         break;                                          \
647                 }                                                       \
648         }                                                               \
649         (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
650         for (get_string_list_counter=0;                                 \
651              get_string_list_counter < get_string_list_el->num_values;  \
652              get_string_list_counter++) {                               \
653                 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
654                                                                     (const char *)get_string_list_el->values[get_string_list_counter].data, \
655                                                                     get_string_list_el->values[get_string_list_counter].length); \
656                 if (!(p)->elem[get_string_list_counter]) {              \
657                         d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
658                         return WERR_NOMEM;                              \
659                 }                                                       \
660                 (p)->elem[get_string_list_counter+1] = NULL;            \
661         }                                                               \
662         talloc_steal(mem_ctx, (p)->elem);                               \
663 } while (0)
664
665 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
666         const char *str; \
667         str = samdb_result_string(msg, attr, NULL);\
668         if (str == NULL) { \
669                 if (strict) { \
670                         d_printf("%s: %s == NULL\n", __location__, attr); \
671                         return WERR_INVALID_PARAM; \
672                 } else { \
673                         (p)->elem = false; \
674                 } \
675         } else if (strcasecmp("TRUE", str) == 0) { \
676                 (p)->elem = true; \
677         } else if (strcasecmp("FALSE", str) == 0) { \
678                 (p)->elem = false; \
679         } else { \
680                 d_printf("%s: %s == %s\n", __location__, attr, str); \
681                 return WERR_INVALID_PARAM; \
682         } \
683 } while (0)
684
685 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
686         (p)->elem = samdb_result_uint(msg, attr, 0);\
687 } while (0)
688
689 #define GET_UINT32_PTR_LDB(msg, attr, p, elem) do { \
690         uint64_t _v = samdb_result_uint64(msg, attr, UINT64_MAX);\
691         if (_v == UINT64_MAX) { \
692                 (p)->elem = NULL; \
693         } else if (_v > UINT32_MAX) { \
694                 d_printf("%s: %s == 0x%llX\n", __location__, \
695                          attr, (unsigned long long)_v); \
696                 return WERR_INVALID_PARAM; \
697         } else { \
698                 (p)->elem = talloc(mem_ctx, uint32_t); \
699                 if (!(p)->elem) { \
700                         d_printf("%s: talloc failed for %s\n", __location__, attr); \
701                         return WERR_NOMEM; \
702                 } \
703                 *(p)->elem = (uint32_t)_v; \
704         } \
705 } while (0)
706
707 #define GET_GUID_LDB(msg, attr, p, elem) do { \
708         (p)->elem = samdb_result_guid(msg, attr);\
709 } while (0)
710
711 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
712         const struct ldb_val *_val;\
713         _val = ldb_msg_find_ldb_val(msg, attr);\
714         if (_val) {\
715                 (p)->elem = *_val;\
716                 talloc_steal(mem_ctx, (p)->elem.data);\
717         } else {\
718                 ZERO_STRUCT((p)->elem);\
719         }\
720 } while (0)
721
722 WERROR dsdb_attribute_from_ldb(struct ldb_context *ldb,
723                                const struct dsdb_schema *schema,
724                                struct ldb_message *msg,
725                                TALLOC_CTX *mem_ctx,
726                                struct dsdb_attribute *attr)
727 {
728         WERROR status;
729
730         GET_STRING_LDB(msg, "cn", mem_ctx, attr, cn, false);
731         GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
732         GET_STRING_LDB(msg, "attributeID", mem_ctx, attr, attributeID_oid, true);
733         if (schema->num_prefixes == 0) {
734                 /* set an invalid value */
735                 attr->attributeID_id = 0xFFFFFFFF;
736         } else {
737                 status = dsdb_map_oid2int(schema, attr->attributeID_oid, &attr->attributeID_id);
738                 if (!W_ERROR_IS_OK(status)) {
739                         DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
740                                 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
741                                 win_errstr(status)));
742                         return status;
743                 }
744         }
745         GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
746         GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
747
748         GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
749
750         GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
751         GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
752         GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
753         GET_UINT32_LDB(msg, "linkID", attr, linkID);
754
755         GET_STRING_LDB(msg, "attributeSyntax", mem_ctx, attr, attributeSyntax_oid, true);
756         if (schema->num_prefixes == 0) {
757                 /* set an invalid value */
758                 attr->attributeSyntax_id = 0xFFFFFFFF;
759         } else {
760                 status = dsdb_map_oid2int(schema, attr->attributeSyntax_oid, &attr->attributeSyntax_id);
761                 if (!W_ERROR_IS_OK(status)) {
762                         DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
763                                 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
764                                 win_errstr(status)));
765                         return status;
766                 }
767         }
768         GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
769         GET_BLOB_LDB(msg, "oMObjectClass", mem_ctx, attr, oMObjectClass);
770
771         GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
772         GET_UINT32_PTR_LDB(msg, "rangeLower", attr, rangeLower);
773         GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, rangeUpper);
774         GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
775
776         GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
777         GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
778
779         GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
780         GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
781         GET_STRING_LDB(msg, "adminDescription", mem_ctx, attr, adminDescription, false);
782         GET_STRING_LDB(msg, "classDisplayName", mem_ctx, attr, classDisplayName, false);
783         GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
784         GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
785         GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
786
787         attr->syntax = dsdb_syntax_for_attribute(attr);
788         if (!attr->syntax) {
789                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
790         }
791
792         if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
793                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
794         }
795
796         return WERR_OK;
797 }
798
799 WERROR dsdb_class_from_ldb(const struct dsdb_schema *schema,
800                            struct ldb_message *msg,
801                            TALLOC_CTX *mem_ctx,
802                            struct dsdb_class *obj)
803 {
804         WERROR status;
805
806         GET_STRING_LDB(msg, "cn", mem_ctx, obj, cn, false);
807         GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
808         GET_STRING_LDB(msg, "governsID", mem_ctx, obj, governsID_oid, true);
809         if (schema->num_prefixes == 0) {
810                 /* set an invalid value */
811                 obj->governsID_id = 0xFFFFFFFF;
812         } else {
813                 status = dsdb_map_oid2int(schema, obj->governsID_oid, &obj->governsID_id);
814                 if (!W_ERROR_IS_OK(status)) {
815                         DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
816                                 __location__, obj->lDAPDisplayName, obj->governsID_oid,
817                                 win_errstr(status)));
818                         return status;
819                 }
820         }
821         GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
822
823         GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
824         GET_STRING_LDB(msg, "rDNAttID", mem_ctx, obj, rDNAttID, false);
825         GET_STRING_LDB(msg, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
826  
827         GET_STRING_LDB(msg, "subClassOf", mem_ctx, obj, subClassOf, true);
828
829         GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass, false);
830         GET_STRING_LIST_LDB(msg, "auxiliaryClass", mem_ctx, obj, auxiliaryClass, false);
831
832         GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, false);
833         GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, false);
834         GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, false);
835         GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, false);
836
837         GET_STRING_LIST_LDB(msg, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
838         GET_STRING_LIST_LDB(msg, "possSuperiors", mem_ctx, obj, possSuperiors, false);
839
840         GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
841
842         GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
843         GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
844
845         GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
846         GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
847         GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, false);
848         GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, false);
849         GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
850         GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
851         GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
852
853         return WERR_OK;
854 }
855
856 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
857
858 /* 
859  Create a DSDB schema from the ldb results provided.  This is called
860  directly when the schema is provisioned from an on-disk LDIF file, or
861  from dsdb_schema_from_schema_dn below
862 */
863
864 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
865                                  struct smb_iconv_convenience *iconv_convenience, 
866                                  struct ldb_result *schema_res,
867                                  struct ldb_result *attrs_res, struct ldb_result *objectclass_res, 
868                                  struct dsdb_schema **schema_out,
869                                  char **error_string)
870 {
871         WERROR status;
872         uint32_t i;
873         const struct ldb_val *prefix_val;
874         const struct ldb_val *info_val;
875         struct ldb_val info_val_default;
876         struct dsdb_schema *schema;
877
878         schema = dsdb_new_schema(mem_ctx, iconv_convenience);
879         if (!schema) {
880                 dsdb_oom(error_string, mem_ctx);
881                 return LDB_ERR_OPERATIONS_ERROR;
882         }
883
884         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
885         if (!prefix_val) {
886                 *error_string = talloc_asprintf(mem_ctx, 
887                                                 "schema_fsmo_init: no prefixMap attribute found");
888                 return LDB_ERR_CONSTRAINT_VIOLATION;
889         }
890         info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
891         if (!info_val) {
892                 info_val_default = strhex_to_data_blob(mem_ctx, "FF0000000000000000000000000000000000000000");
893                 if (!info_val_default.data) {
894                         dsdb_oom(error_string, mem_ctx);
895                         return LDB_ERR_OPERATIONS_ERROR;
896                 }
897                 info_val = &info_val_default;
898         }
899
900         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
901         if (!W_ERROR_IS_OK(status)) {
902                 *error_string = talloc_asprintf(mem_ctx, 
903                               "schema_fsmo_init: failed to load oid mappings: %s",
904                               win_errstr(status));
905                 return LDB_ERR_CONSTRAINT_VIOLATION;
906         }
907
908         for (i=0; i < attrs_res->count; i++) {
909                 struct dsdb_attribute *sa;
910
911                 sa = talloc_zero(schema, struct dsdb_attribute);
912                 if (!sa) {
913                         dsdb_oom(error_string, mem_ctx);
914                         return LDB_ERR_OPERATIONS_ERROR;
915                 }
916
917                 status = dsdb_attribute_from_ldb(ldb, schema, attrs_res->msgs[i], sa, sa);
918                 if (!W_ERROR_IS_OK(status)) {
919                         *error_string = talloc_asprintf(mem_ctx, 
920                                       "schema_fsmo_init: failed to load attribute definition: %s:%s",
921                                       ldb_dn_get_linearized(attrs_res->msgs[i]->dn),
922                                       win_errstr(status));
923                         return LDB_ERR_CONSTRAINT_VIOLATION;
924                 }
925
926                 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
927         }
928
929         for (i=0; i < objectclass_res->count; i++) {
930                 struct dsdb_class *sc;
931
932                 sc = talloc_zero(schema, struct dsdb_class);
933                 if (!sc) {
934                         dsdb_oom(error_string, mem_ctx);
935                         return LDB_ERR_OPERATIONS_ERROR;
936                 }
937
938                 status = dsdb_class_from_ldb(schema, objectclass_res->msgs[i], sc, sc);
939                 if (!W_ERROR_IS_OK(status)) {
940                         *error_string = talloc_asprintf(mem_ctx, 
941                                       "schema_fsmo_init: failed to load class definition: %s:%s",
942                                       ldb_dn_get_linearized(objectclass_res->msgs[i]->dn),
943                                       win_errstr(status));
944                         return LDB_ERR_CONSTRAINT_VIOLATION;
945                 }
946
947                 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
948         }
949
950         schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
951         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
952                 schema->fsmo.we_are_master = true;
953         } else {
954                 schema->fsmo.we_are_master = false;
955         }
956
957         DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
958                   (schema->fsmo.we_are_master?"yes":"no")));
959
960         *schema_out = schema;
961         return LDB_SUCCESS;
962 }
963
964 /*
965   Given an LDB, and the DN, return a populated schema
966 */
967
968 int dsdb_schema_from_schema_dn(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
969                                struct smb_iconv_convenience *iconv_convenience, 
970                                struct ldb_dn *schema_dn,
971                                struct dsdb_schema **schema,
972                                char **error_string_out) 
973 {
974         TALLOC_CTX *tmp_ctx;
975         char *error_string;
976         int ret;
977
978         struct ldb_result *schema_res;
979         struct ldb_result *a_res;
980         struct ldb_result *c_res;
981         static const char *schema_attrs[] = {
982                 "prefixMap",
983                 "schemaInfo",
984                 "fSMORoleOwner",
985                 NULL
986         };
987
988         tmp_ctx = talloc_new(mem_ctx);
989         if (!tmp_ctx) {
990                 dsdb_oom(error_string_out, mem_ctx);
991                 return LDB_ERR_OPERATIONS_ERROR;
992         }
993
994         /*
995          * setup the prefix mappings and schema info
996          */
997         ret = ldb_search(ldb, tmp_ctx, &schema_res,
998                          schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
999         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1000                 talloc_free(tmp_ctx);
1001                 return ret;
1002         } else if (ret != LDB_SUCCESS) {
1003                 *error_string_out = talloc_asprintf(mem_ctx, 
1004                                        "dsdb_schema: failed to search the schema head: %s",
1005                                        ldb_errstring(ldb));
1006                 talloc_free(tmp_ctx);
1007                 return ret;
1008         }
1009         if (schema_res->count != 1) {
1010                 *error_string_out = talloc_asprintf(mem_ctx, 
1011                               "dsdb_schema: [%u] schema heads found on a base search",
1012                               schema_res->count);
1013                 talloc_free(tmp_ctx);
1014                 return LDB_ERR_CONSTRAINT_VIOLATION;
1015         }
1016
1017         /*
1018          * load the attribute definitions
1019          */
1020         ret = ldb_search(ldb, tmp_ctx, &a_res,
1021                          schema_dn, LDB_SCOPE_ONELEVEL, NULL,
1022                          "(objectClass=attributeSchema)");
1023         if (ret != LDB_SUCCESS) {
1024                 *error_string_out = talloc_asprintf(mem_ctx, 
1025                                        "dsdb_schema: failed to search attributeSchema objects: %s",
1026                                        ldb_errstring(ldb));
1027                 talloc_free(tmp_ctx);
1028                 return ret;
1029         }
1030
1031         /*
1032          * load the objectClass definitions
1033          */
1034         ret = ldb_search(ldb, tmp_ctx, &c_res,
1035                          schema_dn, LDB_SCOPE_ONELEVEL, NULL,
1036                          "(objectClass=classSchema)");
1037         if (ret != LDB_SUCCESS) {
1038                 *error_string_out = talloc_asprintf(mem_ctx, 
1039                                        "dsdb_schema: failed to search attributeSchema objects: %s",
1040                                        ldb_errstring(ldb));
1041                 talloc_free(tmp_ctx);
1042                 return ret;
1043         }
1044
1045         ret = dsdb_schema_from_ldb_results(tmp_ctx, ldb,
1046                                            lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
1047                                            schema_res, a_res, c_res, schema, &error_string);
1048         if (ret != LDB_SUCCESS) {
1049                 *error_string_out = talloc_asprintf(mem_ctx, 
1050                                                     "dsdb_schema load failed: %s",
1051                                                     error_string);
1052                 talloc_free(tmp_ctx);
1053                 return ret;
1054         }
1055         talloc_steal(mem_ctx, *schema);
1056         talloc_free(tmp_ctx);
1057
1058         return LDB_SUCCESS;
1059 }       
1060
1061
1062 static const struct {
1063         const char *name;
1064         const char *oid;
1065 } name_mappings[] = {
1066         { "cn",                                 "2.5.4.3" },
1067         { "name",                               "1.2.840.113556.1.4.1" },
1068         { "lDAPDisplayName",                    "1.2.840.113556.1.2.460" },
1069         { "attributeID",                        "1.2.840.113556.1.2.30" },
1070         { "schemaIDGUID",                       "1.2.840.113556.1.4.148" },
1071         { "mAPIID",                             "1.2.840.113556.1.2.49" },
1072         { "attributeSecurityGUID",              "1.2.840.113556.1.4.149" },
1073         { "searchFlags",                        "1.2.840.113556.1.2.334" },
1074         { "systemFlags",                        "1.2.840.113556.1.4.375" },
1075         { "isMemberOfPartialAttributeSet",      "1.2.840.113556.1.4.639" },
1076         { "linkID",                             "1.2.840.113556.1.2.50" },
1077         { "attributeSyntax",                    "1.2.840.113556.1.2.32" },
1078         { "oMSyntax",                           "1.2.840.113556.1.2.231" },
1079         { "oMObjectClass",                      "1.2.840.113556.1.2.218" },
1080         { "isSingleValued",                     "1.2.840.113556.1.2.33" },
1081         { "rangeLower",                         "1.2.840.113556.1.2.34" },
1082         { "rangeUpper",                         "1.2.840.113556.1.2.35" },
1083         { "extendedCharsAllowed",               "1.2.840.113556.1.2.380" },
1084         { "schemaFlagsEx",                      "1.2.840.113556.1.4.120" },
1085         { "msDs-Schema-Extensions",             "1.2.840.113556.1.4.1440" },
1086         { "showInAdvancedViewOnly",             "1.2.840.113556.1.2.169" },
1087         { "adminDisplayName",                   "1.2.840.113556.1.2.194" },
1088         { "adminDescription",                   "1.2.840.113556.1.2.226" },
1089         { "classDisplayName",                   "1.2.840.113556.1.4.610" },
1090         { "isEphemeral",                        "1.2.840.113556.1.4.1212" },
1091         { "isDefunct",                          "1.2.840.113556.1.4.661" },
1092         { "systemOnly",                         "1.2.840.113556.1.4.170" },
1093         { "governsID",                          "1.2.840.113556.1.2.22" },
1094         { "objectClassCategory",                "1.2.840.113556.1.2.370" },
1095         { "rDNAttID",                           "1.2.840.113556.1.2.26" },
1096         { "defaultObjectCategory",              "1.2.840.113556.1.4.783" },
1097         { "subClassOf",                         "1.2.840.113556.1.2.21" },
1098         { "systemAuxiliaryClass",               "1.2.840.113556.1.4.198" },
1099         { "systemPossSuperiors",                "1.2.840.113556.1.4.195" },
1100         { "systemMustContain",                  "1.2.840.113556.1.4.197" },
1101         { "systemMayContain",                   "1.2.840.113556.1.4.196" },
1102         { "auxiliaryClass",                     "1.2.840.113556.1.2.351" },
1103         { "possSuperiors",                      "1.2.840.113556.1.2.8" },
1104         { "mustContain",                        "1.2.840.113556.1.2.24" },
1105         { "mayContain",                         "1.2.840.113556.1.2.25" },
1106         { "defaultSecurityDescriptor",          "1.2.840.113556.1.4.224" },
1107         { "defaultHidingValue",                 "1.2.840.113556.1.4.518" },
1108 };
1109
1110 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
1111                                                                      struct drsuapi_DsReplicaObject *obj,
1112                                                                      const char *name,
1113                                                                      uint32_t *idx)
1114 {
1115         WERROR status;
1116         uint32_t i, id;
1117         const char *oid = NULL;
1118
1119         for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
1120                 if (strcmp(name_mappings[i].name, name) != 0) continue;
1121
1122                 oid = name_mappings[i].oid;
1123                 break;
1124         }
1125
1126         if (!oid) {
1127                 return NULL;
1128         }
1129
1130         status = dsdb_map_oid2int(schema, oid, &id);
1131         if (!W_ERROR_IS_OK(status)) {
1132                 return NULL;
1133         }
1134
1135         for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
1136                 if (obj->attribute_ctr.attributes[i].attid != id) continue;
1137
1138                 if (idx) *idx = i;
1139                 return &obj->attribute_ctr.attributes[i];
1140         }
1141
1142         return NULL;
1143 }
1144
1145 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1146         struct drsuapi_DsReplicaAttribute *_a; \
1147         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1148         if (strict && !_a) { \
1149                 d_printf("%s: %s == NULL\n", __location__, attr); \
1150                 return WERR_INVALID_PARAM; \
1151         } \
1152         if (strict && _a->value_ctr.num_values != 1) { \
1153                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1154                         _a->value_ctr.num_values); \
1155                 return WERR_INVALID_PARAM; \
1156         } \
1157         if (_a && _a->value_ctr.num_values >= 1) { \
1158                 size_t _ret; \
1159                 if (!convert_string_talloc_convenience(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
1160                                              _a->value_ctr.values[0].blob->data, \
1161                                              _a->value_ctr.values[0].blob->length, \
1162                                              (void **)discard_const(&(p)->elem), &_ret, false)) { \
1163                         DEBUG(0,("%s: invalid data!\n", attr)); \
1164                         dump_data(0, \
1165                                      _a->value_ctr.values[0].blob->data, \
1166                                      _a->value_ctr.values[0].blob->length); \
1167                         return WERR_FOOBAR; \
1168                 } \
1169         } else { \
1170                 (p)->elem = NULL; \
1171         } \
1172 } while (0)
1173
1174 #define GET_STRING_LIST_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1175         int get_string_list_counter;                                    \
1176         struct drsuapi_DsReplicaAttribute *_a; \
1177         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1178         if (strict && !_a) { \
1179                 d_printf("%s: %s == NULL\n", __location__, attr); \
1180                 return WERR_INVALID_PARAM; \
1181         } \
1182         (p)->elem = _a ? talloc_array(mem_ctx, const char *, _a->value_ctr.num_values + 1) : NULL; \
1183         for (get_string_list_counter=0;                                 \
1184              _a && get_string_list_counter < _a->value_ctr.num_values;  \
1185              get_string_list_counter++) {                               \
1186                 size_t _ret;                                            \
1187                 if (!convert_string_talloc_convenience(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
1188                                              _a->value_ctr.values[get_string_list_counter].blob->data, \
1189                                              _a->value_ctr.values[get_string_list_counter].blob->length, \
1190                                                        (void **)discard_const(&(p)->elem[get_string_list_counter]), &_ret, false)) { \
1191                         DEBUG(0,("%s: invalid data!\n", attr)); \
1192                         dump_data(0, \
1193                                      _a->value_ctr.values[get_string_list_counter].blob->data, \
1194                                      _a->value_ctr.values[get_string_list_counter].blob->length); \
1195                         return WERR_FOOBAR; \
1196                 } \
1197                 (p)->elem[get_string_list_counter+1] = NULL;            \
1198         }                                                               \
1199         talloc_steal(mem_ctx, (p)->elem);                               \
1200 } while (0)
1201
1202 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1203         struct drsuapi_DsReplicaAttribute *_a; \
1204         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1205         if (strict && !_a) { \
1206                 d_printf("%s: %s == NULL\n", __location__, attr); \
1207                 return WERR_INVALID_PARAM; \
1208         } \
1209         if (strict && _a->value_ctr.num_values != 1) { \
1210                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1211                         _a->value_ctr.num_values); \
1212                 return WERR_INVALID_PARAM; \
1213         } \
1214         if (strict && !_a->value_ctr.values[0].blob) { \
1215                 d_printf("%s: %s data == NULL\n", __location__, attr); \
1216                 return WERR_INVALID_PARAM; \
1217         } \
1218         if (_a && _a->value_ctr.num_values >= 1 \
1219             && _a->value_ctr.values[0].blob) { \
1220                 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
1221                 enum ndr_err_code _ndr_err; \
1222                 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1223                                                       mem_ctx, s->iconv_convenience, &_id3,\
1224                                                       (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
1225                 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1226                         NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1227                         return ntstatus_to_werror(_nt_status); \
1228                 } \
1229                 (p)->elem = _id3.dn; \
1230         } else { \
1231                 (p)->elem = NULL; \
1232         } \
1233 } while (0)
1234
1235 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
1236         struct drsuapi_DsReplicaAttribute *_a; \
1237         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1238         if (strict && !_a) { \
1239                 d_printf("%s: %s == NULL\n", __location__, attr); \
1240                 return WERR_INVALID_PARAM; \
1241         } \
1242         if (strict && _a->value_ctr.num_values != 1) { \
1243                 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1244                          (unsigned int)_a->value_ctr.num_values);       \
1245                 return WERR_INVALID_PARAM; \
1246         } \
1247         if (strict && !_a->value_ctr.values[0].blob) { \
1248                 d_printf("%s: %s data == NULL\n", __location__, attr); \
1249                 return WERR_INVALID_PARAM; \
1250         } \
1251         if (strict && _a->value_ctr.values[0].blob->length != 4) { \
1252                 d_printf("%s: %s length == %u\n", __location__, attr, \
1253                          (unsigned int)_a->value_ctr.values[0].blob->length); \
1254                 return WERR_INVALID_PARAM; \
1255         } \
1256         if (_a && _a->value_ctr.num_values >= 1 \
1257             && _a->value_ctr.values[0].blob \
1258             && _a->value_ctr.values[0].blob->length == 4) { \
1259                 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?true:false);\
1260         } else { \
1261                 (p)->elem = false; \
1262         } \
1263 } while (0)
1264
1265 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
1266         struct drsuapi_DsReplicaAttribute *_a; \
1267         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1268         if (_a && _a->value_ctr.num_values >= 1 \
1269             && _a->value_ctr.values[0].blob \
1270             && _a->value_ctr.values[0].blob->length == 4) { \
1271                 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1272         } else { \
1273                 (p)->elem = 0; \
1274         } \
1275 } while (0)
1276
1277 #define GET_UINT32_PTR_DS(s, r, attr, p, elem) do { \
1278         struct drsuapi_DsReplicaAttribute *_a; \
1279         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1280         if (_a && _a->value_ctr.num_values >= 1 \
1281             && _a->value_ctr.values[0].blob \
1282             && _a->value_ctr.values[0].blob->length == 4) { \
1283                 (p)->elem = talloc(mem_ctx, uint32_t); \
1284                 if (!(p)->elem) { \
1285                         d_printf("%s: talloc failed for %s\n", __location__, attr); \
1286                         return WERR_NOMEM; \
1287                 } \
1288                 *(p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1289         } else { \
1290                 (p)->elem = NULL; \
1291         } \
1292 } while (0)
1293
1294 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
1295         struct drsuapi_DsReplicaAttribute *_a; \
1296         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1297         if (_a && _a->value_ctr.num_values >= 1 \
1298             && _a->value_ctr.values[0].blob \
1299             && _a->value_ctr.values[0].blob->length == 16) { \
1300                 enum ndr_err_code _ndr_err; \
1301                 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1302                                                       mem_ctx, s->iconv_convenience, &(p)->elem, \
1303                                                       (ndr_pull_flags_fn_t)ndr_pull_GUID); \
1304                 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1305                         NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1306                         return ntstatus_to_werror(_nt_status); \
1307                 } \
1308         } else { \
1309                 ZERO_STRUCT((p)->elem);\
1310         } \
1311 } while (0)
1312
1313 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
1314         struct drsuapi_DsReplicaAttribute *_a; \
1315         _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1316         if (_a && _a->value_ctr.num_values >= 1 \
1317             && _a->value_ctr.values[0].blob) { \
1318                 (p)->elem = *_a->value_ctr.values[0].blob;\
1319                 talloc_steal(mem_ctx, (p)->elem.data); \
1320         } else { \
1321                 ZERO_STRUCT((p)->elem);\
1322         }\
1323 } while (0)
1324
1325 WERROR dsdb_attribute_from_drsuapi(struct ldb_context *ldb,
1326                                    struct dsdb_schema *schema,
1327                                    struct drsuapi_DsReplicaObject *r,
1328                                    TALLOC_CTX *mem_ctx,
1329                                    struct dsdb_attribute *attr)
1330 {
1331         WERROR status;
1332
1333         GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, true);
1334         GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
1335         GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
1336         status = dsdb_map_int2oid(schema, attr->attributeID_id, mem_ctx, &attr->attributeID_oid);
1337         if (!W_ERROR_IS_OK(status)) {
1338                 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
1339                         __location__, attr->lDAPDisplayName, attr->attributeID_id,
1340                         win_errstr(status)));
1341                 return status;
1342         }
1343         GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
1344         GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
1345
1346         GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
1347
1348         GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
1349         GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
1350         GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
1351         GET_UINT32_DS(schema, r, "linkID", attr, linkID);
1352
1353         GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
1354         status = dsdb_map_int2oid(schema, attr->attributeSyntax_id, mem_ctx, &attr->attributeSyntax_oid);
1355         if (!W_ERROR_IS_OK(status)) {
1356                 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
1357                         __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
1358                         win_errstr(status)));
1359                 return status;
1360         }
1361         GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
1362         GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
1363
1364         GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, true);
1365         GET_UINT32_PTR_DS(schema, r, "rangeLower", attr, rangeLower);
1366         GET_UINT32_PTR_DS(schema, r, "rangeUpper", attr, rangeUpper);
1367         GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
1368
1369         GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
1370         GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
1371
1372         GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
1373         GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
1374         GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, false);
1375         GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, false);
1376         GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, false);
1377         GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, false);
1378         GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, false);
1379
1380         attr->syntax = dsdb_syntax_for_attribute(attr);
1381         if (!attr->syntax) {
1382                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1383         }
1384
1385         if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
1386                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1387         }
1388
1389         return WERR_OK;
1390 }
1391
1392 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
1393                                struct drsuapi_DsReplicaObject *r,
1394                                TALLOC_CTX *mem_ctx,
1395                                struct dsdb_class *obj)
1396 {
1397         WERROR status;
1398
1399         GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, true);
1400         GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
1401         GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
1402         status = dsdb_map_int2oid(schema, obj->governsID_id, mem_ctx, &obj->governsID_oid);
1403         if (!W_ERROR_IS_OK(status)) {
1404                 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
1405                         __location__, obj->lDAPDisplayName, obj->governsID_id,
1406                         win_errstr(status)));
1407                 return status;
1408         }
1409         GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
1410
1411         GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
1412         GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
1413         GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
1414
1415         GET_STRING_DS(schema, r, "subClassOf", mem_ctx, obj, subClassOf, true);
1416
1417
1418         GET_STRING_LIST_DS(schema, r, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass, false);
1419         GET_STRING_LIST_DS(schema, r, "auxiliaryClass", mem_ctx, obj, auxiliaryClass, false);
1420
1421         GET_STRING_LIST_DS(schema, r, "systemMustContain", mem_ctx, obj, systemMustContain, false);
1422         GET_STRING_LIST_DS(schema, r, "systemMayContain", mem_ctx, obj, systemMayContain, false);
1423         GET_STRING_LIST_DS(schema, r, "mustContain", mem_ctx, obj, mustContain, false);
1424         GET_STRING_LIST_DS(schema, r, "mayContain", mem_ctx, obj, mayContain, false);
1425
1426         GET_STRING_LIST_DS(schema, r, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
1427         GET_STRING_LIST_DS(schema, r, "possSuperiors", mem_ctx, obj, possSuperiors, false);
1428
1429         GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
1430
1431         GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
1432         GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
1433
1434         GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
1435         GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
1436         GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, false);
1437         GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, false);
1438         GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, false);
1439         GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, false);
1440         GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, false);
1441
1442         return WERR_OK;
1443 }
1444