dsdb: specify attributes when loading schema
[samba.git] / source4 / dsdb / samdb / ldb_modules / schema_load.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    The module that handles the Schema FSMO Role Owner
5    checkings, it also loads the dsdb_schema.
6    
7    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009-2010
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22    
23 */
24
25 #include "includes.h"
26 #include "ldb_module.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "param/param.h"
32 #include <tdb.h>
33 #include "lib/tdb_wrap/tdb_wrap.h"
34 #include "dsdb/samdb/ldb_modules/util.h"
35
36 #include "system/filesys.h"
37 struct schema_load_private_data {
38         struct ldb_module *module;
39         bool in_transaction;
40         struct tdb_wrap *metadata;
41         uint64_t schema_seq_num_cache;
42         int tdb_seqnum;
43 };
44
45 static int dsdb_schema_from_db(struct ldb_module *module,
46                                TALLOC_CTX *mem_ctx,
47                                uint64_t schema_seq_num,
48                                struct dsdb_schema **schema);
49
50 /*
51  * Open sam.ldb.d/metadata.tdb.
52  */
53 static int schema_metadata_open(struct ldb_module *module)
54 {
55         struct schema_load_private_data *data = talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
56         struct ldb_context *ldb = ldb_module_get_ctx(module);
57         TALLOC_CTX *tmp_ctx;
58         struct loadparm_context *lp_ctx;
59         const char *sam_name;
60         char *filename;
61         int open_flags;
62         struct stat statbuf;
63
64         if (!data) {
65                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
66                                         "schema_load: metadata not initialized");
67         }
68         data->metadata = NULL;
69
70         tmp_ctx = talloc_new(NULL);
71         if (tmp_ctx == NULL) {
72                 return ldb_module_oom(module);
73         }
74
75         sam_name = (const char *)ldb_get_opaque(ldb, "ldb_url");
76         if (!sam_name) {
77                 talloc_free(tmp_ctx);
78                 return ldb_operr(ldb);
79         }
80         if (strncmp("tdb://", sam_name, 6) == 0) {
81                 sam_name += 6;
82         }
83         filename = talloc_asprintf(tmp_ctx, "%s.d/metadata.tdb", sam_name);
84         if (!filename) {
85                 talloc_free(tmp_ctx);
86                 return ldb_oom(ldb);
87         }
88
89         open_flags = O_RDWR;
90         if (stat(filename, &statbuf) != 0) {
91                 talloc_free(tmp_ctx);
92                 return LDB_ERR_OPERATIONS_ERROR;
93         }
94
95         lp_ctx = talloc_get_type_abort(ldb_get_opaque(ldb, "loadparm"),
96                                        struct loadparm_context);
97
98         data->metadata = tdb_wrap_open(data, filename, 10,
99                                        lpcfg_tdb_flags(lp_ctx, TDB_DEFAULT|TDB_SEQNUM),
100                                        open_flags, 0660);
101         if (data->metadata == NULL) {
102                 talloc_free(tmp_ctx);
103                 return LDB_ERR_OPERATIONS_ERROR;
104         }
105
106         talloc_free(tmp_ctx);
107         return LDB_SUCCESS;
108 }
109
110 static int schema_metadata_get_uint64(struct schema_load_private_data *data,
111                                          const char *key, uint64_t *value,
112                                          uint64_t default_value)
113 {
114         struct tdb_context *tdb;
115         TDB_DATA tdb_key, tdb_data;
116         char *value_str;
117         TALLOC_CTX *tmp_ctx;
118         int tdb_seqnum;
119
120         if (!data) {
121                 *value = default_value;
122                 return LDB_SUCCESS;
123         }
124
125         if (!data->metadata) {
126                 return LDB_ERR_OPERATIONS_ERROR;
127         }
128
129         tdb_seqnum = tdb_get_seqnum(data->metadata->tdb);
130         if (tdb_seqnum == data->tdb_seqnum) {
131                 *value = data->schema_seq_num_cache;
132                 return LDB_SUCCESS;
133         }
134
135         tmp_ctx = talloc_new(NULL);
136         if (tmp_ctx == NULL) {
137                 return ldb_module_oom(data->module);
138         }
139
140         tdb = data->metadata->tdb;
141
142         tdb_key.dptr = (uint8_t *)discard_const_p(char, key);
143         tdb_key.dsize = strlen(key);
144
145         tdb_data = tdb_fetch(tdb, tdb_key);
146         if (!tdb_data.dptr) {
147                 if (tdb_error(tdb) == TDB_ERR_NOEXIST) {
148                         *value = default_value;
149                         talloc_free(tmp_ctx);
150                         return LDB_SUCCESS;
151                 } else {
152                         talloc_free(tmp_ctx);
153                         return ldb_module_error(data->module, LDB_ERR_OPERATIONS_ERROR,
154                                                 tdb_errorstr(tdb));
155                 }
156         }
157
158         value_str = talloc_strndup(tmp_ctx, (char *)tdb_data.dptr, tdb_data.dsize);
159         if (value_str == NULL) {
160                 SAFE_FREE(tdb_data.dptr);
161                 talloc_free(tmp_ctx);
162                 return ldb_module_oom(data->module);
163         }
164
165         /*
166          * Now store it in the cache.  We don't mind that tdb_seqnum
167          * may be stale now, that just means the cache won't be used
168          * next time
169          */
170         data->tdb_seqnum = tdb_seqnum;
171         data->schema_seq_num_cache = strtoull(value_str, NULL, 10);
172
173         *value = data->schema_seq_num_cache;
174
175         SAFE_FREE(tdb_data.dptr);
176         talloc_free(tmp_ctx);
177
178         return LDB_SUCCESS;
179 }
180
181 static struct dsdb_schema *dsdb_schema_refresh(struct ldb_module *module, struct tevent_context *ev,
182                                                struct dsdb_schema *schema, bool is_global_schema)
183 {
184         TALLOC_CTX *mem_ctx;
185         uint64_t schema_seq_num = 0;
186         int ret;
187         struct ldb_context *ldb = ldb_module_get_ctx(module);
188         struct dsdb_schema *new_schema;
189         
190         struct schema_load_private_data *private_data = talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
191         if (!private_data) {
192                 /* We can't refresh until the init function has run */
193                 return schema;
194         }
195
196         /* We don't allow a schema reload during a transaction - nobody else can modify our schema behind our backs */
197         if (private_data->in_transaction) {
198                 return schema;
199         }
200
201         SMB_ASSERT(ev == ldb_get_event_context(ldb));
202
203         mem_ctx = talloc_new(module);
204         if (mem_ctx == NULL) {
205                 return NULL;
206         }
207
208         /*
209          * We update right now the last refresh timestamp so that if
210          * the schema partition hasn't change we don't keep on retrying.
211          * Otherwise if the timestamp was update only when the schema has
212          * actually changed (and therefor completely reloaded) we would
213          * continue to hit the database to get the highest USN.
214          */
215
216         ret = schema_metadata_get_uint64(private_data, DSDB_METADATA_SCHEMA_SEQ_NUM, &schema_seq_num, 0);
217
218         if (schema != NULL) {
219                 if (ret == LDB_SUCCESS) {
220                         if (schema->metadata_usn == schema_seq_num) {
221                                 TALLOC_FREE(mem_ctx);
222                                 return schema;
223                         } else {
224                                 DEBUG(3, ("Schema refresh needed %lld != %lld\n",
225                                           (unsigned long long)schema->metadata_usn,
226                                           (unsigned long long)schema_seq_num));
227                         }
228                 } else {
229                         /* From an old provision it can happen that the tdb didn't exists yet */
230                         DEBUG(0, ("Error while searching for the schema usn in the metadata ignoring: %d:%s:%s\n",
231                               ret, ldb_strerror(ret), ldb_errstring(ldb)));
232                         TALLOC_FREE(mem_ctx);
233                         return schema;
234                 }
235         } else {
236                 DEBUG(10, ("Initial schema load needed, as we have no existing schema, seq_num: %lld\n",
237                           (unsigned long long)schema_seq_num));
238         }
239
240         ret = dsdb_schema_from_db(module, mem_ctx, schema_seq_num, &new_schema);
241         if (ret != LDB_SUCCESS) {
242                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
243                               "dsdb_schema_from_db() failed: %d:%s: %s",
244                               ret, ldb_strerror(ret), ldb_errstring(ldb));
245                 TALLOC_FREE(mem_ctx);
246                 return schema;
247         }
248
249         ret = dsdb_set_schema(ldb, new_schema);
250         if (ret != LDB_SUCCESS) {
251                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
252                               "dsdb_set_schema() failed: %d:%s: %s",
253                               ret, ldb_strerror(ret), ldb_errstring(ldb));
254                 TALLOC_FREE(mem_ctx);
255                 return schema;
256         }
257         if (is_global_schema) {
258                 dsdb_make_schema_global(ldb, new_schema);
259         }
260         TALLOC_FREE(mem_ctx);
261         return new_schema;
262 }
263
264
265 /*
266   Given an LDB module (pointing at the schema DB), and the DN, set the populated schema
267 */
268
269 static int dsdb_schema_from_db(struct ldb_module *module,
270                                TALLOC_CTX *mem_ctx,
271                                uint64_t schema_seq_num,
272                                struct dsdb_schema **schema)
273 {
274         struct ldb_context *ldb = ldb_module_get_ctx(module);
275         TALLOC_CTX *tmp_ctx;
276         char *error_string;
277         int ret;
278         struct ldb_dn *schema_dn = ldb_get_schema_basedn(ldb);
279         struct ldb_result *schema_res;
280         struct ldb_result *res;
281         static const char *schema_head_attrs[] = {
282                 "prefixMap",
283                 "schemaInfo",
284                 "fSMORoleOwner",
285                 NULL
286         };
287         static const char *schema_attrs[] = {
288                 DSDB_SCHEMA_COMMON_ATTRS,
289                 DSDB_SCHEMA_ATTR_ATTRS,
290                 DSDB_SCHEMA_CLASS_ATTRS,
291                 NULL
292         };
293         unsigned flags;
294
295         tmp_ctx = talloc_new(module);
296         if (!tmp_ctx) {
297                 return ldb_oom(ldb);
298         }
299
300         /* we don't want to trace the schema load */
301         flags = ldb_get_flags(ldb);
302         ldb_set_flags(ldb, flags & ~LDB_FLG_ENABLE_TRACING);
303
304         /*
305          * setup the prefix mappings and schema info
306          */
307         ret = dsdb_module_search_dn(module, tmp_ctx, &schema_res,
308                                     schema_dn, schema_head_attrs,
309                                     DSDB_FLAG_NEXT_MODULE, NULL);
310         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
311                 ldb_reset_err_string(ldb);
312                 ldb_debug(ldb, LDB_DEBUG_WARNING,
313                           "schema_load_init: no schema head present: (skip schema loading)\n");
314                 goto failed;
315         } else if (ret != LDB_SUCCESS) {
316                 ldb_asprintf_errstring(ldb, 
317                                        "dsdb_schema: failed to search the schema head: %s",
318                                        ldb_errstring(ldb));
319                 goto failed;
320         }
321
322         /*
323          * load the attribute definitions.
324          */
325         ret = dsdb_module_search(module, tmp_ctx, &res,
326                                  schema_dn, LDB_SCOPE_ONELEVEL,
327                                  schema_attrs,
328                                  DSDB_FLAG_NEXT_MODULE |
329                                  DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT,
330                                  NULL,
331                                  "(|(objectClass=attributeSchema)(objectClass=classSchema))");
332         if (ret != LDB_SUCCESS) {
333                 ldb_asprintf_errstring(ldb, 
334                                        "dsdb_schema: failed to search attributeSchema and classSchema objects: %s",
335                                        ldb_errstring(ldb));
336                 goto failed;
337         }
338
339         ret = dsdb_schema_from_ldb_results(tmp_ctx, ldb,
340                                            schema_res, res, schema, &error_string);
341         if (ret != LDB_SUCCESS) {
342                 ldb_asprintf_errstring(ldb, 
343                                        "dsdb_schema load failed: %s",
344                                        error_string);
345                 goto failed;
346         }
347
348         (*schema)->metadata_usn = schema_seq_num;
349
350         talloc_steal(mem_ctx, *schema);
351
352 failed:
353         if (flags & LDB_FLG_ENABLE_TRACING) {
354                 flags = ldb_get_flags(ldb);
355                 ldb_set_flags(ldb, flags | LDB_FLG_ENABLE_TRACING);
356         }
357         talloc_free(tmp_ctx);
358         return ret;
359 }       
360
361
362 static int schema_load_init(struct ldb_module *module)
363 {
364         struct schema_load_private_data *private_data;
365         struct ldb_context *ldb = ldb_module_get_ctx(module);
366         struct dsdb_schema *schema;
367         void *readOnlySchema;
368         int ret, metadata_ret;
369
370         private_data = talloc_zero(module, struct schema_load_private_data);
371         if (private_data == NULL) {
372                 return ldb_oom(ldb);
373         }
374         private_data->module = module;
375         
376         ldb_module_set_private(module, private_data);
377
378         ret = ldb_next_init(module);
379         if (ret != LDB_SUCCESS) {
380                 return ret;
381         }
382
383         schema = dsdb_get_schema(ldb, NULL);
384
385         metadata_ret = schema_metadata_open(module);
386
387         /* We might already have a schema */
388         if (schema != NULL) {
389                 /* If we have the metadata.tdb, then hook up the refresh function */
390                 if (metadata_ret == LDB_SUCCESS && dsdb_uses_global_schema(ldb)) {
391                         ret = dsdb_set_schema_refresh_function(ldb, dsdb_schema_refresh, module);
392
393                         if (ret != LDB_SUCCESS) {
394                                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
395                                               "schema_load_init: dsdb_set_schema_refresh_fns() failed: %d:%s: %s",
396                                               ret, ldb_strerror(ret), ldb_errstring(ldb));
397                                 return ret;
398                         }
399                 }
400
401                 return LDB_SUCCESS;
402         }
403
404         readOnlySchema = ldb_get_opaque(ldb, "readOnlySchema");
405
406         /* If we have the readOnlySchema opaque, then don't check for
407          * runtime schema updates, as they are not permitted (we would
408          * have to update the backend server schema too */
409         if (readOnlySchema != NULL) {
410                 struct dsdb_schema *new_schema;
411                 ret = dsdb_schema_from_db(module, private_data, 0, &new_schema);
412                 if (ret != LDB_SUCCESS) {
413                         ldb_debug_set(ldb, LDB_DEBUG_FATAL,
414                                       "schema_load_init: dsdb_schema_from_db() failed: %d:%s: %s",
415                                       ret, ldb_strerror(ret), ldb_errstring(ldb));
416                         return ret;
417                 }
418
419                 /* "dsdb_set_schema()" steals schema into the ldb_context */
420                 ret = dsdb_set_schema(ldb, new_schema);
421                 if (ret != LDB_SUCCESS) {
422                         ldb_debug_set(ldb, LDB_DEBUG_FATAL,
423                                       "schema_load_init: dsdb_set_schema() failed: %d:%s: %s",
424                                       ret, ldb_strerror(ret), ldb_errstring(ldb));
425                         return ret;
426                 }
427
428         } else if (metadata_ret == LDB_SUCCESS) {
429                 ret = dsdb_set_schema_refresh_function(ldb, dsdb_schema_refresh, module);
430
431                 if (ret != LDB_SUCCESS) {
432                         ldb_debug_set(ldb, LDB_DEBUG_FATAL,
433                                       "schema_load_init: dsdb_set_schema_refresh_fns() failed: %d:%s: %s",
434                                       ret, ldb_strerror(ret), ldb_errstring(ldb));
435                         return ret;
436                 }
437         } else {
438                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
439                               "schema_load_init: failed to open metadata.tdb");
440                 return metadata_ret;
441         }
442
443         schema = dsdb_get_schema(ldb, NULL);
444
445         /* We do this, invoking the refresh handler, so we know that it works */
446         if (schema == NULL) {
447                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
448                               "schema_load_init: dsdb_get_schema failed");
449                 return LDB_ERR_OPERATIONS_ERROR;
450         }
451
452         return ret;
453 }
454
455 static int schema_search(struct ldb_module *module, struct ldb_request *req)
456 {
457         struct ldb_context *ldb = ldb_module_get_ctx(module);
458
459         /* Try the schema refresh now */
460         dsdb_get_schema(ldb, NULL);
461
462         return ldb_next_request(module, req);
463 }
464
465 static int schema_load_start_transaction(struct ldb_module *module)
466 {
467         struct schema_load_private_data *private_data =
468                 talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
469         struct ldb_context *ldb = ldb_module_get_ctx(module);
470         struct dsdb_schema *schema;
471
472         /* Try the schema refresh now */
473         schema = dsdb_get_schema(ldb, NULL);
474         if (schema == NULL) {
475                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
476                               "schema_load_init: dsdb_get_schema failed");
477                 return LDB_ERR_OPERATIONS_ERROR;
478         }
479         private_data->in_transaction = true;
480
481         return ldb_next_start_trans(module);
482 }
483
484 static int schema_load_end_transaction(struct ldb_module *module)
485 {
486         struct schema_load_private_data *private_data =
487                 talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
488
489         private_data->in_transaction = false;
490
491         return ldb_next_end_trans(module);
492 }
493
494 static int schema_load_del_transaction(struct ldb_module *module)
495 {
496         struct schema_load_private_data *private_data =
497                 talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
498
499         private_data->in_transaction = false;
500
501         return ldb_next_del_trans(module);
502 }
503
504 static int schema_load_extended(struct ldb_module *module, struct ldb_request *req)
505 {
506         struct ldb_context *ldb = ldb_module_get_ctx(module);
507
508         if (strcmp(req->op.extended.oid, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID) != 0) {
509                 return ldb_next_request(module, req);
510         }
511         /* Force a refresh */
512         dsdb_get_schema(ldb, NULL);
513
514         /* Pass to next module, the partition one should finish the chain */
515         return ldb_next_request(module, req);
516 }
517
518
519 static const struct ldb_module_ops ldb_schema_load_module_ops = {
520         .name           = "schema_load",
521         .init_context   = schema_load_init,
522         .extended       = schema_load_extended,
523         .search         = schema_search,
524         .start_transaction = schema_load_start_transaction,
525         .end_transaction   = schema_load_end_transaction,
526         .del_transaction   = schema_load_del_transaction,
527 };
528
529 int ldb_schema_load_module_init(const char *version)
530 {
531         LDB_MODULE_CHECK_VERSION(version);
532         return ldb_register_module(&ldb_schema_load_module_ops);
533 }