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