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