Merge branch 'master' of ssh://git.samba.org/data/git/samba into wspp-schema
[bbaumbach/samba-autobuild/.git] / source4 / dsdb / schema / schema_set.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 "dlinklist.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "lib/ldb/include/ldb_module.h"
27 #include "param/param.h"
28
29 /*
30   override the name to attribute handler function
31  */
32 const struct ldb_schema_attribute *dsdb_attribute_handler_override(struct ldb_context *ldb, 
33                                                                    void *private_data,
34                                                                    const char *name)
35 {
36         struct dsdb_schema *schema = talloc_get_type_abort(private_data, struct dsdb_schema);
37         const struct dsdb_attribute *a = dsdb_attribute_by_lDAPDisplayName(schema, name);
38         if (a == NULL) {
39                 /* this will fall back to ldb internal handling */
40                 return NULL;
41         }
42         return a->ldb_schema_attribute;
43 }
44
45 static int dsdb_schema_set_attributes(struct ldb_context *ldb, struct dsdb_schema *schema, bool write_attributes)
46 {
47         int ret = LDB_SUCCESS;
48         struct ldb_result *res;
49         struct ldb_result *res_idx;
50         struct dsdb_attribute *attr;
51         struct ldb_message *mod_msg;
52         TALLOC_CTX *mem_ctx;
53         struct ldb_message *msg;
54         struct ldb_message *msg_idx;
55
56         /* setup our own attribute name to schema handler */
57         ldb_schema_attribute_set_override_handler(ldb, dsdb_attribute_handler_override, schema);
58
59         if (!write_attributes) {
60                 talloc_free(mem_ctx);
61                 return ret;
62         }
63
64         mem_ctx = talloc_new(ldb);
65         if (!mem_ctx) {
66                 return LDB_ERR_OPERATIONS_ERROR;
67         }
68
69         msg = ldb_msg_new(mem_ctx);
70         if (!msg) {
71                 ldb_oom(ldb);
72                 goto op_error;
73         }
74         msg_idx = ldb_msg_new(mem_ctx);
75         if (!msg_idx) {
76                 ldb_oom(ldb);
77                 goto op_error;
78         }
79         msg->dn = ldb_dn_new(msg, ldb, "@ATTRIBUTES");
80         if (!msg->dn) {
81                 ldb_oom(ldb);
82                 goto op_error;
83         }
84         msg_idx->dn = ldb_dn_new(msg, ldb, "@INDEXLIST");
85         if (!msg_idx->dn) {
86                 ldb_oom(ldb);
87                 goto op_error;
88         }
89
90         for (attr = schema->attributes; attr; attr = attr->next) {
91                 const char *syntax = attr->syntax->ldb_syntax;
92                 
93                 if (!syntax) {
94                         syntax = attr->syntax->ldap_oid;
95                 }
96
97                 /* Write out a rough approximation of the schema as an @ATTRIBUTES value, for bootstrapping */
98                 if (strcmp(syntax, LDB_SYNTAX_INTEGER) == 0) {
99                         ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "INTEGER");
100                 } else if (strcmp(syntax, LDB_SYNTAX_DIRECTORY_STRING) == 0) {
101                         ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "CASE_INSENSITIVE");
102                 } 
103                 if (ret != LDB_SUCCESS) {
104                         break;
105                 }
106
107                 if (attr->searchFlags & SEARCH_FLAG_ATTINDEX) {
108                         ret = ldb_msg_add_string(msg_idx, "@IDXATTR", attr->lDAPDisplayName);
109                         if (ret != LDB_SUCCESS) {
110                                 break;
111                         }
112                 }
113         }
114
115         if (ret != LDB_SUCCESS) {
116                 talloc_free(mem_ctx);
117                 return ret;
118         }
119
120         /* Try to avoid churning the attributes too much - we only want to do this if they have changed */
121         ret = ldb_search(ldb, mem_ctx, &res, msg->dn, LDB_SCOPE_BASE, NULL, "dn=%s", ldb_dn_get_linearized(msg->dn));
122         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
123                 ret = ldb_add(ldb, msg);
124         } else if (ret != LDB_SUCCESS) {
125         } else if (res->count != 1) {
126                 ret = ldb_add(ldb, msg);
127         } else {
128                 ret = LDB_SUCCESS;
129                 /* Annoyingly added to our search results */
130                 ldb_msg_remove_attr(res->msgs[0], "distinguishedName");
131                 
132                 mod_msg = ldb_msg_diff(ldb, res->msgs[0], msg);
133                 if (mod_msg->num_elements > 0) {
134                         ret = ldb_modify(ldb, mod_msg);
135                 }
136         }
137
138         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
139                 /* We might be on a read-only DB */
140                 ret = LDB_SUCCESS;
141         }
142         if (ret != LDB_SUCCESS) {
143                 talloc_free(mem_ctx);
144                 return ret;
145         }
146
147         /* Now write out the indexs, as found in the schema (if they have changed) */
148
149         ret = ldb_search(ldb, mem_ctx, &res_idx, msg_idx->dn, LDB_SCOPE_BASE, NULL, "dn=%s", ldb_dn_get_linearized(msg_idx->dn));
150         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
151                 ret = ldb_add(ldb, msg_idx);
152         } else if (ret != LDB_SUCCESS) {
153         } else if (res->count != 1) {
154                 ret = ldb_add(ldb, msg_idx);
155         } else {
156                 ret = LDB_SUCCESS;
157                 /* Annoyingly added to our search results */
158                 ldb_msg_remove_attr(res_idx->msgs[0], "distinguishedName");
159
160                 mod_msg = ldb_msg_diff(ldb, res_idx->msgs[0], msg_idx);
161                 if (mod_msg->num_elements > 0) {
162                         ret = ldb_modify(ldb, mod_msg);
163                 }
164         }
165         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
166                 /* We might be on a read-only DB */
167                 ret = LDB_SUCCESS;
168         }
169         talloc_free(mem_ctx);
170         return ret;
171
172 op_error:
173         talloc_free(mem_ctx);
174         return LDB_ERR_OPERATIONS_ERROR;
175 }
176
177 static int dsdb_compare_class_by_lDAPDisplayName(struct dsdb_class **c1, struct dsdb_class **c2)
178 {
179         return strcasecmp((*c1)->lDAPDisplayName, (*c2)->lDAPDisplayName);
180 }
181 static int dsdb_compare_class_by_governsID_id(struct dsdb_class **c1, struct dsdb_class **c2)
182 {
183         return (*c1)->governsID_id - (*c2)->governsID_id;
184 }
185 static int dsdb_compare_class_by_governsID_oid(struct dsdb_class **c1, struct dsdb_class **c2)
186 {
187         return strcasecmp((*c1)->governsID_oid, (*c2)->governsID_oid);
188 }
189 static int dsdb_compare_class_by_cn(struct dsdb_class **c1, struct dsdb_class **c2)
190 {
191         return strcasecmp((*c1)->cn, (*c2)->cn);
192 }
193
194 static int dsdb_compare_attribute_by_lDAPDisplayName(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
195 {
196         return strcasecmp((*a1)->lDAPDisplayName, (*a2)->lDAPDisplayName);
197 }
198 static int dsdb_compare_attribute_by_attributeID_id(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
199 {
200         return (*a1)->attributeID_id - (*a2)->attributeID_id;
201 }
202 static int dsdb_compare_attribute_by_attributeID_oid(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
203 {
204         return strcasecmp((*a1)->attributeID_oid, (*a2)->attributeID_oid);
205 }
206 static int dsdb_compare_attribute_by_linkID(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
207 {
208         return (*a1)->linkID - (*a2)->linkID;
209 }
210
211 /*
212   create the sorted accessor arrays for the schema
213  */
214 static int dsdb_setup_sorted_accessors(struct ldb_context *ldb,
215                                        struct dsdb_schema *schema)
216 {
217         struct dsdb_class *cur;
218         struct dsdb_attribute *a;
219         uint32_t i;
220
221         talloc_free(schema->classes_by_lDAPDisplayName);
222         talloc_free(schema->classes_by_governsID_id);
223         talloc_free(schema->classes_by_governsID_oid);
224         talloc_free(schema->classes_by_cn);
225
226         /* count the classes */
227         for (i=0, cur=schema->classes; cur; i++, cur=cur->next) /* noop */ ;
228         schema->num_classes = i;
229
230         /* setup classes_by_* */
231         schema->classes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_class *, i);
232         schema->classes_by_governsID_id    = talloc_array(schema, struct dsdb_class *, i);
233         schema->classes_by_governsID_oid   = talloc_array(schema, struct dsdb_class *, i);
234         schema->classes_by_cn              = talloc_array(schema, struct dsdb_class *, i);
235         if (schema->classes_by_lDAPDisplayName == NULL ||
236             schema->classes_by_governsID_id == NULL ||
237             schema->classes_by_governsID_oid == NULL ||
238             schema->classes_by_cn == NULL) {
239                 goto failed;
240         }
241
242         for (i=0, cur=schema->classes; cur; i++, cur=cur->next) {
243                 schema->classes_by_lDAPDisplayName[i] = cur;
244                 schema->classes_by_governsID_id[i]    = cur;
245                 schema->classes_by_governsID_oid[i]   = cur;
246                 schema->classes_by_cn[i]              = cur;
247         }
248
249         /* sort the arrays */
250         qsort(schema->classes_by_lDAPDisplayName, schema->num_classes, 
251               sizeof(struct dsdb_class *), QSORT_CAST dsdb_compare_class_by_lDAPDisplayName);
252         qsort(schema->classes_by_governsID_id, schema->num_classes, 
253               sizeof(struct dsdb_class *), QSORT_CAST dsdb_compare_class_by_governsID_id);
254         qsort(schema->classes_by_governsID_oid, schema->num_classes, 
255               sizeof(struct dsdb_class *), QSORT_CAST dsdb_compare_class_by_governsID_oid);
256         qsort(schema->classes_by_cn, schema->num_classes, 
257               sizeof(struct dsdb_class *), QSORT_CAST dsdb_compare_class_by_cn);
258
259         /* now build the attribute accessor arrays */
260         talloc_free(schema->attributes_by_lDAPDisplayName);
261         talloc_free(schema->attributes_by_attributeID_id);
262         talloc_free(schema->attributes_by_attributeID_oid);
263         talloc_free(schema->attributes_by_linkID);
264
265         /* count the attributes */
266         for (i=0, a=schema->attributes; a; i++, a=a->next) /* noop */ ;
267         schema->num_attributes = i;
268
269         /* setup attributes_by_* */
270         schema->attributes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_attribute *, i);
271         schema->attributes_by_attributeID_id    = talloc_array(schema, struct dsdb_attribute *, i);
272         schema->attributes_by_attributeID_oid   = talloc_array(schema, struct dsdb_attribute *, i);
273         schema->attributes_by_linkID              = talloc_array(schema, struct dsdb_attribute *, i);
274         if (schema->attributes_by_lDAPDisplayName == NULL ||
275             schema->attributes_by_attributeID_id == NULL ||
276             schema->attributes_by_attributeID_oid == NULL ||
277             schema->attributes_by_linkID == NULL) {
278                 goto failed;
279         }
280
281         for (i=0, a=schema->attributes; a; i++, a=a->next) {
282                 schema->attributes_by_lDAPDisplayName[i] = a;
283                 schema->attributes_by_attributeID_id[i]    = a;
284                 schema->attributes_by_attributeID_oid[i]   = a;
285                 schema->attributes_by_linkID[i]          = a;
286         }
287
288         /* sort the arrays */
289         qsort(schema->attributes_by_lDAPDisplayName, schema->num_attributes, 
290               sizeof(struct dsdb_attribute *), QSORT_CAST dsdb_compare_attribute_by_lDAPDisplayName);
291         qsort(schema->attributes_by_attributeID_id, schema->num_attributes, 
292               sizeof(struct dsdb_attribute *), QSORT_CAST dsdb_compare_attribute_by_attributeID_id);
293         qsort(schema->attributes_by_attributeID_oid, schema->num_attributes, 
294               sizeof(struct dsdb_attribute *), QSORT_CAST dsdb_compare_attribute_by_attributeID_oid);
295         qsort(schema->attributes_by_linkID, schema->num_attributes, 
296               sizeof(struct dsdb_attribute *), QSORT_CAST dsdb_compare_attribute_by_linkID);
297
298         return LDB_SUCCESS;
299
300 failed:
301         schema->classes_by_lDAPDisplayName = NULL;
302         schema->classes_by_governsID_id = NULL;
303         schema->classes_by_governsID_oid = NULL;
304         schema->classes_by_cn = NULL;
305         schema->attributes_by_lDAPDisplayName = NULL;
306         schema->attributes_by_attributeID_id = NULL;
307         schema->attributes_by_attributeID_oid = NULL;
308         schema->attributes_by_linkID = NULL;
309         ldb_oom(ldb);
310         return LDB_ERR_OPERATIONS_ERROR;
311 }
312
313
314 /**
315  * Attach the schema to an opaque pointer on the ldb, so ldb modules
316  * can find it 
317  */
318
319 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
320 {
321         int ret;
322
323         ret = dsdb_setup_sorted_accessors(ldb, schema);
324         if (ret != LDB_SUCCESS) {
325                 return ret;
326         }
327
328         ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
329         if (ret != LDB_SUCCESS) {
330                 return ret;
331         }
332
333         /* Set the new attributes based on the new schema */
334         ret = dsdb_schema_set_attributes(ldb, schema, true);
335         if (ret != LDB_SUCCESS) {
336                 return ret;
337         }
338
339         talloc_steal(ldb, schema);
340
341         return LDB_SUCCESS;
342 }
343
344 /**
345  * Global variable to hold one copy of the schema, used to avoid memory bloat
346  */
347 static struct dsdb_schema *global_schema;
348
349 /**
350  * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
351  */
352 int dsdb_set_global_schema(struct ldb_context *ldb)
353 {
354         int ret;
355         if (!global_schema) {
356                 return LDB_SUCCESS;
357         }
358
359         ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
360         if (ret != LDB_SUCCESS) {
361                 return ret;
362         }
363
364         /* Set the new attributes based on the new schema */
365         ret = dsdb_schema_set_attributes(ldb, global_schema, false);
366         if (ret != LDB_SUCCESS) {
367                 return ret;
368         }
369
370         /* Keep a reference to this schema, just incase the global copy is replaced */
371         if (talloc_reference(ldb, global_schema) == NULL) {
372                 return LDB_ERR_OPERATIONS_ERROR;
373         }
374
375         return LDB_SUCCESS;
376 }
377
378 /**
379  * Find the schema object for this ldb
380  */
381
382 struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb)
383 {
384         const void *p;
385         struct dsdb_schema *schema;
386
387         /* see if we have a cached copy */
388         p = ldb_get_opaque(ldb, "dsdb_schema");
389         if (!p) {
390                 return NULL;
391         }
392
393         schema = talloc_get_type(p, struct dsdb_schema);
394         if (!schema) {
395                 return NULL;
396         }
397
398         return schema;
399 }
400
401 /**
402  * Make the schema found on this ldb the 'global' schema
403  */
404
405 void dsdb_make_schema_global(struct ldb_context *ldb)
406 {
407         struct dsdb_schema *schema = dsdb_get_schema(ldb);
408         if (!schema) {
409                 return;
410         }
411
412         if (global_schema) {
413                 talloc_unlink(talloc_autofree_context(), schema);
414         }
415
416         talloc_steal(talloc_autofree_context(), schema);
417         global_schema = schema;
418
419         dsdb_set_global_schema(ldb);
420 }
421
422
423 /**
424  * Rather than read a schema from the LDB itself, read it from an ldif
425  * file.  This allows schema to be loaded and used while adding the
426  * schema itself to the directory.
427  */
428
429 WERROR dsdb_attach_schema_from_ldif(struct ldb_context *ldb, const char *pf, const char *df)
430 {
431         struct ldb_ldif *ldif;
432         struct ldb_message *msg;
433         TALLOC_CTX *mem_ctx;
434         WERROR status;
435         int ret;
436         struct dsdb_schema *schema;
437         const struct ldb_val *prefix_val;
438         const struct ldb_val *info_val;
439         struct ldb_val info_val_default;
440
441         mem_ctx = talloc_new(ldb);
442         if (!mem_ctx) {
443                 goto nomem;
444         }
445
446         schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")));
447
448         schema->fsmo.we_are_master = true;
449         schema->fsmo.master_dn = ldb_dn_new_fmt(schema, ldb, "@PROVISION_SCHEMA_MASTER");
450         if (!schema->fsmo.master_dn) {
451                 goto nomem;
452         }
453
454         /*
455          * load the prefixMap attribute from pf
456          */
457         ldif = ldb_ldif_read_string(ldb, &pf);
458         if (!ldif) {
459                 status = WERR_INVALID_PARAM;
460                 goto failed;
461         }
462         talloc_steal(mem_ctx, ldif);
463
464         msg = ldb_msg_canonicalize(ldb, ldif->msg);
465         if (!msg) {
466                 goto nomem;
467         }
468         talloc_steal(mem_ctx, msg);
469         talloc_free(ldif);
470
471         prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
472         if (!prefix_val) {
473                 status = WERR_INVALID_PARAM;
474                 goto failed;
475         }
476
477         info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
478         if (!info_val) {
479                 info_val_default = strhex_to_data_blob(mem_ctx, "FF0000000000000000000000000000000000000000");
480                 if (!info_val_default.data) {
481                         goto nomem;
482                 }
483                 info_val = &info_val_default;
484         }
485
486         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
487         if (!W_ERROR_IS_OK(status)) {
488                 goto failed;
489         }
490
491         /*
492          * load the attribute and class definitions outof df
493          */
494         while ((ldif = ldb_ldif_read_string(ldb, &df))) {
495                 bool is_sa;
496                 bool is_sc;
497
498                 talloc_steal(mem_ctx, ldif);
499
500                 msg = ldb_msg_canonicalize(ldb, ldif->msg);
501                 if (!msg) {
502                         goto nomem;
503                 }
504
505                 talloc_steal(mem_ctx, msg);
506                 talloc_free(ldif);
507
508                 is_sa = ldb_msg_check_string_attribute(msg, "objectClass", "attributeSchema");
509                 is_sc = ldb_msg_check_string_attribute(msg, "objectClass", "classSchema");
510
511                 if (is_sa) {
512                         struct dsdb_attribute *sa;
513
514                         sa = talloc_zero(schema, struct dsdb_attribute);
515                         if (!sa) {
516                                 goto nomem;
517                         }
518
519                         status = dsdb_attribute_from_ldb(ldb, schema, msg, sa, sa);
520                         if (!W_ERROR_IS_OK(status)) {
521                                 goto failed;
522                         }
523
524                         DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
525                 } else if (is_sc) {
526                         struct dsdb_class *sc;
527
528                         sc = talloc_zero(schema, struct dsdb_class);
529                         if (!sc) {
530                                 goto nomem;
531                         }
532
533                         status = dsdb_class_from_ldb(schema, msg, sc, sc);
534                         if (!W_ERROR_IS_OK(status)) {
535                                 goto failed;
536                         }
537
538                         DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
539                 }
540         }
541
542         ret = dsdb_set_schema(ldb, schema);
543         if (ret != LDB_SUCCESS) {
544                 status = WERR_FOOBAR;
545                 goto failed;
546         }
547
548         goto done;
549
550 nomem:
551         status = WERR_NOMEM;
552 failed:
553 done:
554         talloc_free(mem_ctx);
555         return status;
556 }