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