Remove the complexity of transactions from the attributes-setting code.
[samba.git] / source / 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                         return ret;
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                                 return ret;
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                         return ret;
109                 }
110         }
111
112         if (!write_attributes) {
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                 talloc_free(mem_ctx);
124                 return ret;
125         } else {
126
127                 if (res->count != 1) {
128                         talloc_free(mem_ctx);
129                         return LDB_ERR_NO_SUCH_OBJECT;
130                 }
131                 
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 = ldb_modify(ldb, mod_msg);
139                 }
140         }
141
142         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
143                 /* We might be on a read-only DB */
144                 talloc_free(mem_ctx);
145                 return ret;
146         } else if (ret != LDB_SUCCESS) {
147                 return ret;
148         }
149
150         /* Now write out the indexs, as found in the schema (if they have changed) */
151
152         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));
153         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
154                 ret = ldb_add(ldb, msg_idx);
155         } else if (ret != LDB_SUCCESS) {
156                 talloc_free(mem_ctx);
157                 return ret;
158         } else {
159                 if (res_idx->count != 1) {
160                         talloc_free(mem_ctx);
161                         return LDB_ERR_NO_SUCH_OBJECT;
162                 }
163                 
164                 /* Annoyingly added to our search results */
165                 ldb_msg_remove_attr(res_idx->msgs[0], "distinguishedName");
166
167                 mod_msg = ldb_msg_diff(ldb, res_idx->msgs[0], msg_idx);
168                 if (mod_msg->num_elements > 0) {
169                         ret = ldb_modify(ldb, mod_msg);
170                 }
171         }
172         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
173                 /* We might be on a read-only DB */
174                 talloc_free(mem_ctx);
175                 ret = LDB_SUCCESS;
176         }
177         talloc_free(mem_ctx);
178         return ret;
179 }
180
181
182 /**
183  * Attach the schema to an opaque pointer on the ldb, so ldb modules
184  * can find it 
185  */
186
187 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
188 {
189         int ret;
190
191         ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
192         if (ret != LDB_SUCCESS) {
193                 return ret;
194         }
195
196         /* Set the new attributes based on the new schema */
197         ret = dsdb_schema_set_attributes(ldb, schema, true);
198         if (ret != LDB_SUCCESS) {
199                 return ret;
200         }
201
202         talloc_steal(ldb, schema);
203
204         return LDB_SUCCESS;
205 }
206
207 /**
208  * Global variable to hold one copy of the schema, used to avoid memory bloat
209  */
210 static struct dsdb_schema *global_schema;
211
212 /**
213  * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
214  */
215 int dsdb_set_global_schema(struct ldb_context *ldb)
216 {
217         int ret;
218         if (!global_schema) {
219                 return LDB_SUCCESS;
220         }
221         ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
222         if (ret != LDB_SUCCESS) {
223                 return ret;
224         }
225
226         /* Set the new attributes based on the new schema */
227         ret = dsdb_schema_set_attributes(ldb, global_schema, false);
228         if (ret != LDB_SUCCESS) {
229                 return ret;
230         }
231
232         /* Keep a reference to this schema, just incase the global copy is replaced */
233         if (talloc_reference(ldb, global_schema) == NULL) {
234                 return LDB_ERR_OPERATIONS_ERROR;
235         }
236
237         return LDB_SUCCESS;
238 }
239
240 /**
241  * Find the schema object for this ldb
242  */
243
244 struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb)
245 {
246         const void *p;
247         struct dsdb_schema *schema;
248
249         /* see if we have a cached copy */
250         p = ldb_get_opaque(ldb, "dsdb_schema");
251         if (!p) {
252                 return NULL;
253         }
254
255         schema = talloc_get_type(p, struct dsdb_schema);
256         if (!schema) {
257                 return NULL;
258         }
259
260         return schema;
261 }
262
263 /**
264  * Make the schema found on this ldb the 'global' schema
265  */
266
267 void dsdb_make_schema_global(struct ldb_context *ldb)
268 {
269         struct dsdb_schema *schema = dsdb_get_schema(ldb);
270         if (!schema) {
271                 return;
272         }
273
274         if (global_schema) {
275                 talloc_unlink(talloc_autofree_context(), schema);
276         }
277
278         talloc_steal(talloc_autofree_context(), schema);
279         global_schema = schema;
280
281         dsdb_set_global_schema(ldb);
282 }
283
284
285 /**
286  * Rather than read a schema from the LDB itself, read it from an ldif
287  * file.  This allows schema to be loaded and used while adding the
288  * schema itself to the directory.
289  */
290
291 WERROR dsdb_attach_schema_from_ldif_file(struct ldb_context *ldb, const char *pf, const char *df)
292 {
293         struct ldb_ldif *ldif;
294         struct ldb_message *msg;
295         TALLOC_CTX *mem_ctx;
296         WERROR status;
297         int ret;
298         struct dsdb_schema *schema;
299         const struct ldb_val *prefix_val;
300         const struct ldb_val *info_val;
301         struct ldb_val info_val_default;
302
303         mem_ctx = talloc_new(ldb);
304         if (!mem_ctx) {
305                 goto nomem;
306         }
307
308         schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")));
309
310         schema->fsmo.we_are_master = true;
311         schema->fsmo.master_dn = ldb_dn_new_fmt(schema, ldb, "@PROVISION_SCHEMA_MASTER");
312         if (!schema->fsmo.master_dn) {
313                 goto nomem;
314         }
315
316         /*
317          * load the prefixMap attribute from pf
318          */
319         ldif = ldb_ldif_read_string(ldb, &pf);
320         if (!ldif) {
321                 status = WERR_INVALID_PARAM;
322                 goto failed;
323         }
324         talloc_steal(mem_ctx, ldif);
325
326         msg = ldb_msg_canonicalize(ldb, ldif->msg);
327         if (!msg) {
328                 goto nomem;
329         }
330         talloc_steal(mem_ctx, msg);
331         talloc_free(ldif);
332
333         prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
334         if (!prefix_val) {
335                 status = WERR_INVALID_PARAM;
336                 goto failed;
337         }
338
339         info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
340         if (!info_val) {
341                 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
342                 if (!info_val_default.data) {
343                         goto nomem;
344                 }
345                 talloc_steal(mem_ctx, info_val_default.data);
346                 info_val = &info_val_default;
347         }
348
349         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
350         if (!W_ERROR_IS_OK(status)) {
351                 goto failed;
352         }
353
354         /*
355          * load the attribute and class definitions outof df
356          */
357         while ((ldif = ldb_ldif_read_string(ldb, &df))) {
358                 bool is_sa;
359                 bool is_sc;
360
361                 talloc_steal(mem_ctx, ldif);
362
363                 msg = ldb_msg_canonicalize(ldb, ldif->msg);
364                 if (!msg) {
365                         goto nomem;
366                 }
367
368                 talloc_steal(mem_ctx, msg);
369                 talloc_free(ldif);
370
371                 is_sa = ldb_msg_check_string_attribute(msg, "objectClass", "attributeSchema");
372                 is_sc = ldb_msg_check_string_attribute(msg, "objectClass", "classSchema");
373
374                 if (is_sa) {
375                         struct dsdb_attribute *sa;
376
377                         sa = talloc_zero(schema, struct dsdb_attribute);
378                         if (!sa) {
379                                 goto nomem;
380                         }
381
382                         status = dsdb_attribute_from_ldb(schema, msg, sa, sa);
383                         if (!W_ERROR_IS_OK(status)) {
384                                 goto failed;
385                         }
386
387                         DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
388                 } else if (is_sc) {
389                         struct dsdb_class *sc;
390
391                         sc = talloc_zero(schema, struct dsdb_class);
392                         if (!sc) {
393                                 goto nomem;
394                         }
395
396                         status = dsdb_class_from_ldb(schema, msg, sc, sc);
397                         if (!W_ERROR_IS_OK(status)) {
398                                 goto failed;
399                         }
400
401                         DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
402                 }
403         }
404
405         ret = dsdb_set_schema(ldb, schema);
406         if (ret != LDB_SUCCESS) {
407                 status = WERR_FOOBAR;
408                 goto failed;
409         }
410
411         goto done;
412
413 nomem:
414         status = WERR_NOMEM;
415 failed:
416 done:
417         talloc_free(mem_ctx);
418         return status;
419 }