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