977941e584e91c815a3c1e994eb6b7708bac9a5f
[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 "dsdb/common/util.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "../lib/util/dlinklist.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "param/param.h"
32 #include "lib/ldb/include/ldb_module.h"
33 #include "../lib/util/asn1.h"
34
35
36 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx)
37 {
38         struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
39         if (!schema) {
40                 return NULL;
41         }
42
43         return schema;
44 }
45
46 struct dsdb_schema *dsdb_schema_copy_shallow(TALLOC_CTX *mem_ctx,
47                                              struct ldb_context *ldb,
48                                              const struct dsdb_schema *schema)
49 {
50         int ret;
51         struct dsdb_class *cls;
52         struct dsdb_attribute *attr;
53         struct dsdb_schema *schema_copy;
54
55         schema_copy = dsdb_new_schema(mem_ctx);
56         if (!schema_copy) {
57                 return NULL;
58         }
59
60         /* copy prexiMap & schemaInfo */
61         schema_copy->prefixmap = dsdb_schema_pfm_copy_shallow(schema_copy,
62                                                               schema->prefixmap);
63         if (!schema_copy->prefixmap) {
64                 return NULL;
65         }
66
67         schema_copy->schema_info = talloc_strdup(schema_copy, schema->schema_info);
68
69         /* copy classes and attributes*/
70         for (cls = schema->classes; cls; cls = cls->next) {
71                 struct dsdb_class *class_copy = talloc_memdup(schema_copy,
72                                                               cls, sizeof(*cls));
73                 if (!class_copy) {
74                         goto failed;
75                 }
76                 DLIST_ADD(schema_copy->classes, class_copy);
77         }
78         schema_copy->num_classes = schema->num_classes;
79
80         for (attr = schema->attributes; attr; attr = attr->next) {
81                 struct dsdb_attribute *a_copy = talloc_memdup(schema_copy,
82                                                               attr, sizeof(*attr));
83                 if (!a_copy) {
84                         goto failed;
85                 }
86                 DLIST_ADD(schema_copy->attributes, a_copy);
87         }
88         schema_copy->num_attributes = schema->num_attributes;
89
90         /* rebuild indexes */
91         ret = dsdb_setup_sorted_accessors(ldb, schema_copy);
92         if (ret != LDB_SUCCESS) {
93                 goto failed;
94         }
95
96         return schema_copy;
97
98 failed:
99         talloc_free(schema_copy);
100         return NULL;
101 }
102
103
104 WERROR dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema *schema,
105                                         const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
106 {
107         WERROR werr;
108         const char *schema_info;
109         struct dsdb_schema_prefixmap *pfm;
110
111         werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, schema, &pfm, &schema_info);
112         W_ERROR_NOT_OK_RETURN(werr);
113
114         /* set loaded prefixMap */
115         talloc_free(schema->prefixmap);
116         schema->prefixmap = pfm;
117
118         talloc_free(discard_const(schema->schema_info));
119         schema->schema_info = schema_info;
120
121         return WERR_OK;
122 }
123
124 static WERROR _dsdb_prefixmap_from_ldb_val(const struct ldb_val *pfm_ldb_val,
125                                            TALLOC_CTX *mem_ctx,
126                                            struct dsdb_schema_prefixmap **_pfm)
127 {
128         WERROR werr;
129         enum ndr_err_code ndr_err;
130         struct prefixMapBlob pfm_blob;
131
132         TALLOC_CTX *temp_ctx = talloc_new(mem_ctx);
133         W_ERROR_HAVE_NO_MEMORY(temp_ctx);
134
135         ndr_err = ndr_pull_struct_blob(pfm_ldb_val, temp_ctx,
136                                 &pfm_blob,
137                                 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
138         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
139                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
140                 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: Failed to parse prefixmap of length %u: %s\n",
141                          (unsigned int)pfm_ldb_val->length, ndr_map_error2string(ndr_err)));
142                 talloc_free(temp_ctx);
143                 return ntstatus_to_werror(nt_status);
144         }
145
146         if (pfm_blob.version != PREFIX_MAP_VERSION_DSDB) {
147                 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version %u incorrect\n", (unsigned int)pfm_blob.version));
148                 talloc_free(temp_ctx);
149                 return WERR_VERSION_PARSE_ERROR;
150         }
151
152         /* call the drsuapi version */
153         werr = dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob.ctr.dsdb, false, mem_ctx, _pfm, NULL);
154         if (!W_ERROR_IS_OK(werr)) {
155                 DEBUG(0, (__location__ " dsdb_schema_pfm_from_drsuapi_pfm failed: %s\n", win_errstr(werr)));
156                 talloc_free(temp_ctx);
157                 return werr;
158         }
159
160         talloc_free(temp_ctx);
161
162         return werr;
163 }
164
165 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
166                                   const struct ldb_val *prefixMap,
167                                   const struct ldb_val *schemaInfo)
168 {
169         WERROR werr;
170         const char *schema_info;
171         struct dsdb_schema_prefixmap *pfm;
172         TALLOC_CTX *mem_ctx;
173
174         /* verify schemaInfo blob is valid one */
175         if (!dsdb_schema_info_blob_is_valid(schemaInfo)) {
176                 DEBUG(0,(__location__": dsdb_schema_info_blob_is_valid() failed.\n"));
177                 return WERR_INVALID_PARAMETER;
178         }
179
180         mem_ctx = talloc_new(schema);
181         W_ERROR_HAVE_NO_MEMORY(mem_ctx);
182
183         /* fetch prefixMap */
184         werr = _dsdb_prefixmap_from_ldb_val(prefixMap,
185                                             mem_ctx, &pfm);
186         if (!W_ERROR_IS_OK(werr)) {
187                 DEBUG(0, (__location__ " _dsdb_prefixmap_from_ldb_val failed: %s\n", win_errstr(werr)));
188                 talloc_free(mem_ctx);
189                 return werr;
190         }
191
192         /* decode schema_info */
193         schema_info = hex_encode_talloc(mem_ctx,
194                                         schemaInfo->data,
195                                         schemaInfo->length);
196         if (!schema_info) {
197                 talloc_free(mem_ctx);
198                 return WERR_NOMEM;
199         }
200
201         /* store prefixMap and schema_info into cached Schema */
202         talloc_free(schema->prefixmap);
203         schema->prefixmap = talloc_steal(schema, pfm);
204
205         talloc_free(discard_const(schema->schema_info));
206         schema->schema_info = talloc_steal(schema, schema_info);
207
208         /* clean up locally allocated mem */
209         talloc_free(mem_ctx);
210
211         return WERR_OK;
212 }
213
214 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
215                                      bool include_schema_info,
216                                      TALLOC_CTX *mem_ctx,
217                                      struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
218 {
219         return dsdb_drsuapi_pfm_from_schema_pfm(schema->prefixmap,
220                                                 include_schema_info ? schema->schema_info : NULL,
221                                                 mem_ctx, _ctr);
222 }
223
224 WERROR dsdb_get_drsuapi_prefixmap_as_blob(const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr,
225                                           TALLOC_CTX *mem_ctx,
226                                           struct ldb_val *prefixMap)
227 {
228         struct prefixMapBlob pfm;
229         enum ndr_err_code ndr_err;
230         pfm.version     = PREFIX_MAP_VERSION_DSDB;
231         pfm.reserved    = 0;
232         pfm.ctr.dsdb    = *ctr;
233
234         ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, &pfm,
235                                         (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
236         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
237                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
238                 return ntstatus_to_werror(nt_status);
239         }
240         return WERR_OK;
241 }
242
243 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
244                                  TALLOC_CTX *mem_ctx,
245                                  struct ldb_val *prefixMap,
246                                  struct ldb_val *schemaInfo)
247 {
248         WERROR status;
249         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
250
251         status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
252         W_ERROR_NOT_OK_RETURN(status);
253
254         status = dsdb_get_drsuapi_prefixmap_as_blob(ctr, mem_ctx, prefixMap);
255         talloc_free(ctr);
256         W_ERROR_NOT_OK_RETURN(status);
257
258         *schemaInfo = strhex_to_data_blob(mem_ctx, schema->schema_info);
259         W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
260
261         return WERR_OK;
262 }
263
264
265 /*
266  * this function is called from within a ldb transaction from the schema_fsmo module
267  */
268 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
269 {
270         WERROR status;
271         uint32_t attid;
272         TALLOC_CTX *mem_ctx;
273         struct dsdb_schema_prefixmap *pfm;
274
275         mem_ctx = talloc_new(ldb);
276         W_ERROR_HAVE_NO_MEMORY(mem_ctx);
277
278         /* Read prefixes from disk*/
279         status = dsdb_read_prefixes_from_ldb(ldb, mem_ctx, &pfm);
280         if (!W_ERROR_IS_OK(status)) {
281                 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
282                         win_errstr(status)));
283                 talloc_free(mem_ctx);
284                 return status;
285         }
286
287         /* Check if there is a prefix for the oid in the prefixes array*/
288         status = dsdb_schema_pfm_find_oid(pfm, full_oid, NULL);
289         if (W_ERROR_IS_OK(status)) {
290                 /* prefix found*/
291                 talloc_free(mem_ctx);
292                 return status;
293         } else if (!W_ERROR_EQUAL(status, WERR_NOT_FOUND)) {
294                 /* error */
295                 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
296                         win_errstr(status)));
297                 talloc_free(mem_ctx);
298                 return status;
299         }
300
301         /* Create the new mapping for the prefix of full_oid */
302         status = dsdb_schema_pfm_make_attid(pfm, full_oid, &attid);
303         if (!W_ERROR_IS_OK(status)) {
304                 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_schema_pfm_make_attid: %s\n",
305                         win_errstr(status)));
306                 talloc_free(mem_ctx);
307                 return status;
308         }
309
310         talloc_unlink(schema, schema->prefixmap);
311         schema->prefixmap = talloc_steal(schema, pfm);
312
313         /* Update prefixMap in ldb*/
314         status = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, ldb, schema);
315         if (!W_ERROR_IS_OK(status)) {
316                 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
317                         win_errstr(status)));
318                 talloc_free(mem_ctx);
319                 return status;
320         }
321
322         DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
323                  full_oid, schema->prefixmap->length));
324
325         talloc_free(mem_ctx);
326         return status;
327 }
328
329
330 WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
331                                               const struct dsdb_schema *schema)
332 {
333         WERROR status;
334         int ldb_ret;
335         struct ldb_message *msg;
336         struct ldb_dn *schema_dn;
337         struct prefixMapBlob pfm_blob;
338         struct ldb_val ndr_blob;
339         enum ndr_err_code ndr_err;
340         TALLOC_CTX *temp_ctx;
341         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
342
343         schema_dn = ldb_get_schema_basedn(ldb);
344         if (!schema_dn) {
345                 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));
346                 return WERR_FOOBAR;
347         }
348
349         temp_ctx = talloc_new(mem_ctx);
350         W_ERROR_HAVE_NO_MEMORY(temp_ctx);
351
352         /* convert schema_prefixMap to prefixMap blob */
353         status = dsdb_get_oid_mappings_drsuapi(schema, false, temp_ctx, &ctr);
354         if (!W_ERROR_IS_OK(status)) {
355                 talloc_free(temp_ctx);
356                 return status;
357         }
358
359         pfm_blob.version        = PREFIX_MAP_VERSION_DSDB;
360         pfm_blob.ctr.dsdb       = *ctr;
361
362         ndr_err = ndr_push_struct_blob(&ndr_blob, temp_ctx,
363                                        &pfm_blob,
364                                        (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
365         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
366                 talloc_free(temp_ctx);
367                 return WERR_FOOBAR;
368         }
369  
370         /* write serialized prefixMap into LDB */
371         msg = ldb_msg_new(temp_ctx);
372         if (!msg) {
373                 talloc_free(temp_ctx);
374                 return WERR_NOMEM;
375         }
376
377         msg->dn = schema_dn;
378         ldb_ret = ldb_msg_add_value(msg, "prefixMap", &ndr_blob, NULL);
379         if (ldb_ret != 0) {
380                 talloc_free(temp_ctx);
381                 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));        
382                 return WERR_NOMEM;
383         }
384  
385         ldb_ret = dsdb_replace(ldb, msg, DSDB_FLAG_AS_SYSTEM);
386
387         talloc_free(temp_ctx);
388
389         if (ldb_ret != 0) {
390                 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: dsdb_replace failed\n"));
391                 return WERR_FOOBAR;
392         }
393  
394         return WERR_OK;
395 }
396
397 WERROR dsdb_read_prefixes_from_ldb(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct dsdb_schema_prefixmap **_pfm)
398 {
399         WERROR werr;
400         int ldb_ret;
401         const struct ldb_val *prefix_val;
402         struct ldb_dn *schema_dn;
403         struct ldb_result *schema_res = NULL;
404         static const char *schema_attrs[] = {
405                 "prefixMap",
406                 NULL
407         };
408
409         schema_dn = ldb_get_schema_basedn(ldb);
410         if (!schema_dn) {
411                 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
412                 return WERR_FOOBAR;
413         }
414
415         ldb_ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
416         if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
417                 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
418                 talloc_free(schema_res);
419                 return WERR_FOOBAR;
420         } else if (ldb_ret != LDB_SUCCESS) {
421                 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
422                 talloc_free(schema_res);
423                 return WERR_FOOBAR;
424         }
425
426         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
427         if (!prefix_val) {
428                 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
429                 talloc_free(schema_res);
430                 return WERR_FOOBAR;
431         }
432
433         werr = _dsdb_prefixmap_from_ldb_val(prefix_val,
434                                             mem_ctx,
435                                             _pfm);
436         talloc_free(schema_res);
437         W_ERROR_NOT_OK_RETURN(werr);
438
439         return WERR_OK;
440 }
441
442 /*
443   this will be replaced with something that looks at the right part of
444   the schema once we know where unique indexing information is hidden
445  */
446 static bool dsdb_schema_unique_attribute(const char *attr)
447 {
448         const char *attrs[] = { "objectGUID", "objectSid" , NULL };
449         unsigned int i;
450         for (i=0;attrs[i];i++) {
451                 if (strcasecmp(attr, attrs[i]) == 0) {
452                         return true;
453                 }
454         }
455         return false;
456 }
457
458
459 /*
460   setup the ldb_schema_attribute field for a dsdb_attribute
461  */
462 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb, 
463                                                   struct dsdb_attribute *attr)
464 {
465         const char *syntax = attr->syntax->ldb_syntax;
466         const struct ldb_schema_syntax *s;
467         struct ldb_schema_attribute *a;
468
469         if (!syntax) {
470                 syntax = attr->syntax->ldap_oid;
471         }
472
473         s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
474         if (s == NULL) {
475                 s = ldb_samba_syntax_by_name(ldb, syntax);
476         }
477         if (s == NULL) {
478                 s = ldb_standard_syntax_by_name(ldb, syntax);
479         }
480
481         if (s == NULL) {
482                 return ldb_operr(ldb);
483         }
484
485         attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
486         if (attr->ldb_schema_attribute == NULL) {
487                 return ldb_oom(ldb);
488         }
489
490         a->name = attr->lDAPDisplayName;
491         a->flags = 0;
492         a->syntax = s;
493
494         if (dsdb_schema_unique_attribute(a->name)) {
495                 a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
496         }
497         if (attr->isSingleValued) {
498                 a->flags |= LDB_ATTR_FLAG_SINGLE_VALUE;
499         }
500         
501         
502         return LDB_SUCCESS;
503 }
504
505
506 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
507         const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
508         if (get_string_val == NULL) { \
509                 if (strict) {                                     \
510                         d_printf("%s: %s == NULL in %s\n", __location__, attr, ldb_dn_get_linearized(msg->dn)); \
511                         return WERR_INVALID_PARAM;                      \
512                 } else {                                                \
513                         (p)->elem = NULL;                               \
514                 }                                                       \
515         } else {                                                        \
516                 (p)->elem = talloc_strndup(mem_ctx,                     \
517                                            (const char *)get_string_val->data, \
518                                            get_string_val->length); \
519                 if (!(p)->elem) {                                       \
520                         d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
521                         return WERR_NOMEM;                              \
522                 }                                                       \
523         }                                                               \
524 } while (0)
525
526 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem) do {   \
527         int get_string_list_counter;                                    \
528         struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
529         /* We may get empty attributes over the replication channel */  \
530         if (get_string_list_el == NULL || get_string_list_el->num_values == 0) {                                \
531                 (p)->elem = NULL;                                       \
532                 break;                                                  \
533         }                                                               \
534         (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
535         for (get_string_list_counter=0;                                 \
536              get_string_list_counter < get_string_list_el->num_values;  \
537              get_string_list_counter++) {                               \
538                 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
539                                                                     (const char *)get_string_list_el->values[get_string_list_counter].data, \
540                                                                     get_string_list_el->values[get_string_list_counter].length); \
541                 if (!(p)->elem[get_string_list_counter]) {              \
542                         d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
543                         return WERR_NOMEM;                              \
544                 }                                                       \
545                 (p)->elem[get_string_list_counter+1] = NULL;            \
546         }                                                               \
547         talloc_steal(mem_ctx, (p)->elem);                               \
548 } while (0)
549
550 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
551         const char *str; \
552         str = ldb_msg_find_attr_as_string(msg, attr, NULL);\
553         if (str == NULL) { \
554                 if (strict) { \
555                         d_printf("%s: %s == NULL\n", __location__, attr); \
556                         return WERR_INVALID_PARAM; \
557                 } else { \
558                         (p)->elem = false; \
559                 } \
560         } else if (strcasecmp("TRUE", str) == 0) { \
561                 (p)->elem = true; \
562         } else if (strcasecmp("FALSE", str) == 0) { \
563                 (p)->elem = false; \
564         } else { \
565                 d_printf("%s: %s == %s\n", __location__, attr, str); \
566                 return WERR_INVALID_PARAM; \
567         } \
568 } while (0)
569
570 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
571         (p)->elem = ldb_msg_find_attr_as_uint(msg, attr, 0);\
572 } while (0)
573
574 #define GET_UINT32_PTR_LDB(msg, attr, mem_ctx, p, elem) do {            \
575         uint64_t _v = ldb_msg_find_attr_as_uint64(msg, attr, UINT64_MAX);\
576         if (_v == UINT64_MAX) { \
577                 (p)->elem = NULL; \
578         } else if (_v > UINT32_MAX) { \
579                 d_printf("%s: %s == 0x%llX\n", __location__, \
580                          attr, (unsigned long long)_v); \
581                 return WERR_INVALID_PARAM; \
582         } else { \
583                 (p)->elem = talloc(mem_ctx, uint32_t); \
584                 if (!(p)->elem) { \
585                         d_printf("%s: talloc failed for %s\n", __location__, attr); \
586                         return WERR_NOMEM; \
587                 } \
588                 *(p)->elem = (uint32_t)_v; \
589         } \
590 } while (0)
591
592 #define GET_GUID_LDB(msg, attr, p, elem) do { \
593         (p)->elem = samdb_result_guid(msg, attr);\
594 } while (0)
595
596 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
597         const struct ldb_val *_val;\
598         _val = ldb_msg_find_ldb_val(msg, attr);\
599         if (_val) {\
600                 (p)->elem = *_val;\
601                 talloc_steal(mem_ctx, (p)->elem.data);\
602         } else {\
603                 ZERO_STRUCT((p)->elem);\
604         }\
605 } while (0)
606
607 WERROR dsdb_attribute_from_ldb(struct ldb_context *ldb,
608                                struct dsdb_schema *schema,
609                                struct ldb_message *msg)
610 {
611         WERROR status;
612         struct dsdb_attribute *attr = talloc_zero(schema, struct dsdb_attribute);
613         if (!attr) {
614                 return WERR_NOMEM;
615         }
616
617         GET_STRING_LDB(msg, "cn", attr, attr, cn, false);
618         GET_STRING_LDB(msg, "lDAPDisplayName", attr, attr, lDAPDisplayName, true);
619         GET_STRING_LDB(msg, "attributeID", attr, attr, attributeID_oid, true);
620         if (!schema->prefixmap || schema->prefixmap->length == 0) {
621                 /* set an invalid value */
622                 attr->attributeID_id = DRSUAPI_ATTID_INVALID;
623         } else {
624                 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
625                                                     attr->attributeID_oid,
626                                                     &attr->attributeID_id);
627                 if (!W_ERROR_IS_OK(status)) {
628                         DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
629                                 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
630                                 win_errstr(status)));
631                         return status;
632                 }
633         }
634         /* fetch msDS-IntId to be used in resolving ATTRTYP values */
635         GET_UINT32_LDB(msg, "msDS-IntId", attr, msDS_IntId);
636
637         GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
638         GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
639
640         GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
641
642         GET_GUID_LDB(msg, "objectGUID", attr, objectGUID);
643
644         GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
645         GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
646         GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
647         GET_UINT32_LDB(msg, "linkID", attr, linkID);
648
649         GET_STRING_LDB(msg, "attributeSyntax", attr, attr, attributeSyntax_oid, true);
650         if (!schema->prefixmap || schema->prefixmap->length == 0) {
651                 /* set an invalid value */
652                 attr->attributeSyntax_id = DRSUAPI_ATTID_INVALID;
653         } else {
654                 status = dsdb_schema_pfm_attid_from_oid(schema->prefixmap,
655                                                         attr->attributeSyntax_oid,
656                                                         &attr->attributeSyntax_id);
657                 if (!W_ERROR_IS_OK(status)) {
658                         DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
659                                 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
660                                 win_errstr(status)));
661                         return status;
662                 }
663         }
664         GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
665         GET_BLOB_LDB(msg, "oMObjectClass", attr, attr, oMObjectClass);
666
667         GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
668         GET_UINT32_PTR_LDB(msg, "rangeLower", attr, attr, rangeLower);
669         GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, attr, rangeUpper);
670         GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
671
672         GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
673         GET_BLOB_LDB(msg, "msDs-Schema-Extensions", attr, attr, msDs_Schema_Extensions);
674
675         GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
676         GET_STRING_LDB(msg, "adminDisplayName", attr, attr, adminDisplayName, false);
677         GET_STRING_LDB(msg, "adminDescription", attr, attr, adminDescription, false);
678         GET_STRING_LDB(msg, "classDisplayName", attr, attr, classDisplayName, false);
679         GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
680         GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
681         GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
682
683         attr->syntax = dsdb_syntax_for_attribute(attr);
684         if (!attr->syntax) {
685                 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
686                          attr->lDAPDisplayName));
687                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
688         }
689
690         if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
691                 DEBUG(0,(__location__ ": Unknown schema syntax for %s - ldb_syntax: %s, ldap_oid: %s\n",
692                          attr->lDAPDisplayName,
693                          attr->syntax->ldb_syntax,
694                          attr->syntax->ldap_oid));
695                 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
696         }
697
698         DLIST_ADD(schema->attributes, attr);
699         return WERR_OK;
700 }
701
702 WERROR dsdb_class_from_ldb(struct dsdb_schema *schema,
703                            struct ldb_message *msg)
704 {
705         WERROR status;
706         struct dsdb_class *obj = talloc_zero(schema, struct dsdb_class);
707         if (!obj) {
708                 return WERR_NOMEM;
709         }
710         GET_STRING_LDB(msg, "cn", obj, obj, cn, false);
711         GET_STRING_LDB(msg, "lDAPDisplayName", obj, obj, lDAPDisplayName, true);
712         GET_STRING_LDB(msg, "governsID", obj, obj, governsID_oid, true);
713         if (!schema->prefixmap || schema->prefixmap->length == 0) {
714                 /* set an invalid value */
715                 obj->governsID_id = DRSUAPI_ATTID_INVALID;
716         } else {
717                 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
718                                                     obj->governsID_oid,
719                                                     &obj->governsID_id);
720                 if (!W_ERROR_IS_OK(status)) {
721                         DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
722                                 __location__, obj->lDAPDisplayName, obj->governsID_oid,
723                                 win_errstr(status)));
724                         return status;
725                 }
726         }
727         GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
728         GET_GUID_LDB(msg, "objectGUID", obj, objectGUID);
729
730         GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
731         GET_STRING_LDB(msg, "rDNAttID", obj, obj, rDNAttID, false);
732         GET_STRING_LDB(msg, "defaultObjectCategory", obj, obj, defaultObjectCategory, true);
733  
734         GET_STRING_LDB(msg, "subClassOf", obj, obj, subClassOf, true);
735
736         GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", obj, obj, systemAuxiliaryClass);
737         GET_STRING_LIST_LDB(msg, "auxiliaryClass", obj, obj, auxiliaryClass);
738
739         GET_STRING_LIST_LDB(msg, "systemMustContain", obj, obj, systemMustContain);
740         GET_STRING_LIST_LDB(msg, "systemMayContain", obj, obj, systemMayContain);
741         GET_STRING_LIST_LDB(msg, "mustContain", obj, obj, mustContain);
742         GET_STRING_LIST_LDB(msg, "mayContain", obj, obj, mayContain);
743
744         GET_STRING_LIST_LDB(msg, "systemPossSuperiors", obj, obj, systemPossSuperiors);
745         GET_STRING_LIST_LDB(msg, "possSuperiors", obj, obj, possSuperiors);
746
747         GET_STRING_LDB(msg, "defaultSecurityDescriptor", obj, obj, defaultSecurityDescriptor, false);
748
749         GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
750         GET_BLOB_LDB(msg, "msDs-Schema-Extensions", obj, obj, msDs_Schema_Extensions);
751
752         GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
753         GET_STRING_LDB(msg, "adminDisplayName", obj, obj, adminDisplayName, false);
754         GET_STRING_LDB(msg, "adminDescription", obj, obj, adminDescription, false);
755         GET_STRING_LDB(msg, "classDisplayName", obj, obj, classDisplayName, false);
756         GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
757         GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
758         GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
759
760         DLIST_ADD(schema->classes, obj);
761         return WERR_OK;
762 }
763
764 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
765
766 /* 
767  Create a DSDB schema from the ldb results provided.  This is called
768  directly when the schema is provisioned from an on-disk LDIF file, or
769  from dsdb_schema_from_schema_dn in schema_fsmo
770 */
771
772 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
773                                  struct ldb_result *schema_res,
774                                  struct ldb_result *attrs_res, struct ldb_result *objectclass_res, 
775                                  struct dsdb_schema **schema_out,
776                                  char **error_string)
777 {
778         WERROR status;
779         unsigned int i;
780         const struct ldb_val *prefix_val;
781         const struct ldb_val *info_val;
782         struct ldb_val info_val_default;
783         struct dsdb_schema *schema;
784
785         schema = dsdb_new_schema(mem_ctx);
786         if (!schema) {
787                 dsdb_oom(error_string, mem_ctx);
788                 return ldb_operr(ldb);
789         }
790
791         schema->base_dn = talloc_steal(schema, schema_res->msgs[0]->dn);
792
793         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
794         if (!prefix_val) {
795                 *error_string = talloc_asprintf(mem_ctx, 
796                                                 "schema_fsmo_init: no prefixMap attribute found");
797                 DEBUG(0,(__location__ ": %s\n", *error_string));
798                 return LDB_ERR_CONSTRAINT_VIOLATION;
799         }
800         info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
801         if (!info_val) {
802                 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
803                 if (!W_ERROR_IS_OK(status)) {
804                         *error_string = talloc_asprintf(mem_ctx,
805                                                         "schema_fsmo_init: dsdb_schema_info_blob_new() failed - %s",
806                                                         win_errstr(status));
807                         DEBUG(0,(__location__ ": %s\n", *error_string));
808                         return ldb_operr(ldb);
809                 }
810                 info_val = &info_val_default;
811         }
812
813         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
814         if (!W_ERROR_IS_OK(status)) {
815                 *error_string = talloc_asprintf(mem_ctx, 
816                               "schema_fsmo_init: failed to load oid mappings: %s",
817                               win_errstr(status));
818                 DEBUG(0,(__location__ ": %s\n", *error_string));
819                 return LDB_ERR_CONSTRAINT_VIOLATION;
820         }
821
822         for (i=0; i < attrs_res->count; i++) {
823                 status = dsdb_attribute_from_ldb(ldb, schema, attrs_res->msgs[i]);
824                 if (!W_ERROR_IS_OK(status)) {
825                         *error_string = talloc_asprintf(mem_ctx, 
826                                       "schema_fsmo_init: failed to load attribute definition: %s:%s",
827                                       ldb_dn_get_linearized(attrs_res->msgs[i]->dn),
828                                       win_errstr(status));
829                         DEBUG(0,(__location__ ": %s\n", *error_string));
830                         return LDB_ERR_CONSTRAINT_VIOLATION;
831                 }
832         }
833
834         for (i=0; i < objectclass_res->count; i++) {
835                 status = dsdb_class_from_ldb(schema, objectclass_res->msgs[i]);
836                 if (!W_ERROR_IS_OK(status)) {
837                         *error_string = talloc_asprintf(mem_ctx, 
838                                       "schema_fsmo_init: failed to load class definition: %s:%s",
839                                       ldb_dn_get_linearized(objectclass_res->msgs[i]->dn),
840                                       win_errstr(status));
841                         DEBUG(0,(__location__ ": %s\n", *error_string));
842                         return LDB_ERR_CONSTRAINT_VIOLATION;
843                 }
844         }
845
846         schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
847         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
848                 schema->fsmo.we_are_master = true;
849         } else {
850                 schema->fsmo.we_are_master = false;
851         }
852
853         DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
854                   (schema->fsmo.we_are_master?"yes":"no")));
855
856         *schema_out = schema;
857         return LDB_SUCCESS;
858 }