Merge branch 'master' of ssh://git.samba.org/data/git/samba
[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 "dsdb/samdb/samdb.h"
25 #include "lib/ldb/include/ldb_module.h"
26 #include "param/param.h"
27
28
29 static int dsdb_schema_set_attributes(struct ldb_context *ldb, struct dsdb_schema *schema, bool write_attributes)
30 {
31         int ret = LDB_SUCCESS;
32         struct ldb_result *res;
33         struct ldb_result *res_idx;
34         struct dsdb_attribute *attr;
35         struct ldb_message *mod_msg;
36         TALLOC_CTX *mem_ctx = talloc_new(ldb);
37         
38         struct ldb_message *msg;
39         struct ldb_message *msg_idx;
40
41         if (!mem_ctx) {
42                 return LDB_ERR_OPERATIONS_ERROR;
43         }
44
45         msg = ldb_msg_new(mem_ctx);
46         if (!msg) {
47                 ldb_oom(ldb);
48                 return LDB_ERR_OPERATIONS_ERROR;
49         }
50         msg_idx = ldb_msg_new(mem_ctx);
51         if (!msg_idx) {
52                 ldb_oom(ldb);
53                 return LDB_ERR_OPERATIONS_ERROR;
54         }
55         msg->dn = ldb_dn_new(msg, ldb, "@ATTRIBUTES");
56         if (!msg->dn) {
57                 ldb_oom(ldb);
58                 return LDB_ERR_OPERATIONS_ERROR;
59         }
60         msg_idx->dn = ldb_dn_new(msg, ldb, "@INDEXLIST");
61         if (!msg_idx->dn) {
62                 ldb_oom(ldb);
63                 return LDB_ERR_OPERATIONS_ERROR;
64         }
65
66         for (attr = schema->attributes; attr; attr = attr->next) {
67                 const struct ldb_schema_syntax *s;
68                 const char *syntax = attr->syntax->ldb_syntax;
69                 if (!syntax) {
70                         syntax = attr->syntax->ldap_oid;
71                 }
72
73                 /* Write out a rough approximation of the schema as an @ATTRIBUTES value, for bootstrapping */
74                 if (strcmp(syntax, LDB_SYNTAX_INTEGER) == 0) {
75                         ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "INTEGER");
76                 } else if (strcmp(syntax, LDB_SYNTAX_DIRECTORY_STRING) == 0) {
77                         ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "CASE_INSENSITIVE");
78                 } 
79                 if (ret != LDB_SUCCESS) {
80                         break;
81                 }
82
83                 if (attr->searchFlags & SEARCH_FLAG_ATTINDEX) {
84                         ret = ldb_msg_add_string(msg_idx, "@IDXATTR", attr->lDAPDisplayName);
85                         if (ret != LDB_SUCCESS) {
86                                 break;
87                         }
88                 }
89
90                 if (!attr->syntax) {
91                         continue;
92                 }
93
94                 ret = ldb_schema_attribute_add(ldb, attr->lDAPDisplayName, LDB_ATTR_FLAG_FIXED,
95                                                syntax);
96                 if (ret != LDB_SUCCESS) {
97                         s = ldb_samba_syntax_by_name(ldb, attr->syntax->ldap_oid);
98                         if (s) {
99                                 ret = ldb_schema_attribute_add_with_syntax(ldb, attr->lDAPDisplayName, LDB_ATTR_FLAG_FIXED, s);
100                         } else {
101                                 ret = LDB_SUCCESS; /* Nothing to do here */
102                         }
103                 }
104                 
105                 if (ret != LDB_SUCCESS) {
106                         break;
107                 }
108         }
109
110         if (!write_attributes || ret != LDB_SUCCESS) {
111                 talloc_free(mem_ctx);
112                 return ret;
113         }
114
115
116         /* Try to avoid churning the attributes too much - we only want to do this if they have changed */
117         ret = ldb_search(ldb, mem_ctx, &res, msg->dn, LDB_SCOPE_BASE, NULL, "dn=%s", ldb_dn_get_linearized(msg->dn));
118         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
119                 ret = ldb_add(ldb, msg);
120         } else if (ret != LDB_SUCCESS) {
121         } else if (res->count != 1) {
122                 ret = ldb_add(ldb, msg);
123         } else {
124                 ret = LDB_SUCCESS;
125                 /* Annoyingly added to our search results */
126                 ldb_msg_remove_attr(res->msgs[0], "distinguishedName");
127                 
128                 mod_msg = ldb_msg_diff(ldb, res->msgs[0], msg);
129                 if (mod_msg->num_elements > 0) {
130                         ret = ldb_modify(ldb, mod_msg);
131                 }
132         }
133
134         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
135                 /* We might be on a read-only DB */
136                 ret = LDB_SUCCESS;
137         }
138         if (ret != LDB_SUCCESS) {
139                 talloc_free(mem_ctx);
140                 return ret;
141         }
142
143         /* Now write out the indexs, as found in the schema (if they have changed) */
144
145         ret = ldb_search(ldb, mem_ctx, &res_idx, msg_idx->dn, LDB_SCOPE_BASE, NULL, "dn=%s", ldb_dn_get_linearized(msg_idx->dn));
146         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
147                 ret = ldb_add(ldb, msg_idx);
148         } else if (ret != LDB_SUCCESS) {
149         } else if (res->count != 1) {
150                 ret = ldb_add(ldb, msg_idx);
151         } else {
152                 ret = LDB_SUCCESS;
153                 /* Annoyingly added to our search results */
154                 ldb_msg_remove_attr(res_idx->msgs[0], "distinguishedName");
155
156                 mod_msg = ldb_msg_diff(ldb, res_idx->msgs[0], msg_idx);
157                 if (mod_msg->num_elements > 0) {
158                         ret = ldb_modify(ldb, mod_msg);
159                 }
160         }
161         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
162                 /* We might be on a read-only DB */
163                 ret = LDB_SUCCESS;
164         }
165         talloc_free(mem_ctx);
166         return ret;
167 }
168
169
170 /**
171  * Attach the schema to an opaque pointer on the ldb, so ldb modules
172  * can find it 
173  */
174
175 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
176 {
177         int ret;
178
179         ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
180         if (ret != LDB_SUCCESS) {
181                 return ret;
182         }
183
184         /* Set the new attributes based on the new schema */
185         ret = dsdb_schema_set_attributes(ldb, schema, true);
186         if (ret != LDB_SUCCESS) {
187                 return ret;
188         }
189
190         talloc_steal(ldb, schema);
191
192         return LDB_SUCCESS;
193 }
194
195 /**
196  * Global variable to hold one copy of the schema, used to avoid memory bloat
197  */
198 static struct dsdb_schema *global_schema;
199
200 /**
201  * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
202  */
203 int dsdb_set_global_schema(struct ldb_context *ldb)
204 {
205         int ret;
206         if (!global_schema) {
207                 return LDB_SUCCESS;
208         }
209         ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
210         if (ret != LDB_SUCCESS) {
211                 return ret;
212         }
213
214         /* Set the new attributes based on the new schema */
215         ret = dsdb_schema_set_attributes(ldb, global_schema, false);
216         if (ret != LDB_SUCCESS) {
217                 return ret;
218         }
219
220         /* Keep a reference to this schema, just incase the global copy is replaced */
221         if (talloc_reference(ldb, global_schema) == NULL) {
222                 return LDB_ERR_OPERATIONS_ERROR;
223         }
224
225         return LDB_SUCCESS;
226 }
227
228 /**
229  * Find the schema object for this ldb
230  */
231
232 struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb)
233 {
234         const void *p;
235         struct dsdb_schema *schema;
236
237         /* see if we have a cached copy */
238         p = ldb_get_opaque(ldb, "dsdb_schema");
239         if (!p) {
240                 return NULL;
241         }
242
243         schema = talloc_get_type(p, struct dsdb_schema);
244         if (!schema) {
245                 return NULL;
246         }
247
248         return schema;
249 }
250
251 /**
252  * Make the schema found on this ldb the 'global' schema
253  */
254
255 void dsdb_make_schema_global(struct ldb_context *ldb)
256 {
257         struct dsdb_schema *schema = dsdb_get_schema(ldb);
258         if (!schema) {
259                 return;
260         }
261
262         if (global_schema) {
263                 talloc_unlink(talloc_autofree_context(), schema);
264         }
265
266         talloc_steal(talloc_autofree_context(), schema);
267         global_schema = schema;
268
269         dsdb_set_global_schema(ldb);
270 }
271
272
273 /**
274  * Rather than read a schema from the LDB itself, read it from an ldif
275  * file.  This allows schema to be loaded and used while adding the
276  * schema itself to the directory.
277  */
278
279 WERROR dsdb_attach_schema_from_ldif_file(struct ldb_context *ldb, const char *pf, const char *df)
280 {
281         struct ldb_ldif *ldif;
282         struct ldb_message *msg;
283         TALLOC_CTX *mem_ctx;
284         WERROR status;
285         int ret;
286         struct dsdb_schema *schema;
287         const struct ldb_val *prefix_val;
288         const struct ldb_val *info_val;
289         struct ldb_val info_val_default;
290
291         mem_ctx = talloc_new(ldb);
292         if (!mem_ctx) {
293                 goto nomem;
294         }
295
296         schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")));
297
298         schema->fsmo.we_are_master = true;
299         schema->fsmo.master_dn = ldb_dn_new_fmt(schema, ldb, "@PROVISION_SCHEMA_MASTER");
300         if (!schema->fsmo.master_dn) {
301                 goto nomem;
302         }
303
304         /*
305          * load the prefixMap attribute from pf
306          */
307         ldif = ldb_ldif_read_string(ldb, &pf);
308         if (!ldif) {
309                 status = WERR_INVALID_PARAM;
310                 goto failed;
311         }
312         talloc_steal(mem_ctx, ldif);
313
314         msg = ldb_msg_canonicalize(ldb, ldif->msg);
315         if (!msg) {
316                 goto nomem;
317         }
318         talloc_steal(mem_ctx, msg);
319         talloc_free(ldif);
320
321         prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
322         if (!prefix_val) {
323                 status = WERR_INVALID_PARAM;
324                 goto failed;
325         }
326
327         info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
328         if (!info_val) {
329                 info_val_default = strhex_to_data_blob(mem_ctx, "FF0000000000000000000000000000000000000000");
330                 if (!info_val_default.data) {
331                         goto nomem;
332                 }
333                 info_val = &info_val_default;
334         }
335
336         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
337         if (!W_ERROR_IS_OK(status)) {
338                 goto failed;
339         }
340
341         /*
342          * load the attribute and class definitions outof df
343          */
344         while ((ldif = ldb_ldif_read_string(ldb, &df))) {
345                 bool is_sa;
346                 bool is_sc;
347
348                 talloc_steal(mem_ctx, ldif);
349
350                 msg = ldb_msg_canonicalize(ldb, ldif->msg);
351                 if (!msg) {
352                         goto nomem;
353                 }
354
355                 talloc_steal(mem_ctx, msg);
356                 talloc_free(ldif);
357
358                 is_sa = ldb_msg_check_string_attribute(msg, "objectClass", "attributeSchema");
359                 is_sc = ldb_msg_check_string_attribute(msg, "objectClass", "classSchema");
360
361                 if (is_sa) {
362                         struct dsdb_attribute *sa;
363
364                         sa = talloc_zero(schema, struct dsdb_attribute);
365                         if (!sa) {
366                                 goto nomem;
367                         }
368
369                         status = dsdb_attribute_from_ldb(schema, msg, sa, sa);
370                         if (!W_ERROR_IS_OK(status)) {
371                                 goto failed;
372                         }
373
374                         DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
375                 } else if (is_sc) {
376                         struct dsdb_class *sc;
377
378                         sc = talloc_zero(schema, struct dsdb_class);
379                         if (!sc) {
380                                 goto nomem;
381                         }
382
383                         status = dsdb_class_from_ldb(schema, msg, sc, sc);
384                         if (!W_ERROR_IS_OK(status)) {
385                                 goto failed;
386                         }
387
388                         DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
389                 }
390         }
391
392         ret = dsdb_set_schema(ldb, schema);
393         if (ret != LDB_SUCCESS) {
394                 status = WERR_FOOBAR;
395                 goto failed;
396         }
397
398         goto done;
399
400 nomem:
401         status = WERR_NOMEM;
402 failed:
403 done:
404         talloc_free(mem_ctx);
405         return status;
406 }