2 Unix SMB/CIFS mplementation.
4 The module that handles the Schema FSMO Role Owner
5 checkings, it also loads the dsdb_schema.
7 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009-2010
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.
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.
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/>.
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"
33 #include "lib/tdb_wrap/tdb_wrap.h"
34 #include "dsdb/samdb/ldb_modules/util.h"
36 #include "system/filesys.h"
37 struct schema_load_private_data {
38 struct ldb_module *module;
40 struct tdb_wrap *metadata;
41 uint64_t schema_seq_num_cache;
45 static int dsdb_schema_from_db(struct ldb_module *module,
47 uint64_t schema_seq_num,
48 struct dsdb_schema **schema);
51 * Open sam.ldb.d/metadata.tdb.
53 static int schema_metadata_open(struct ldb_module *module)
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);
58 struct loadparm_context *lp_ctx;
65 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
66 "schema_load: metadata not initialized");
68 data->metadata = NULL;
70 tmp_ctx = talloc_new(NULL);
71 if (tmp_ctx == NULL) {
72 return ldb_module_oom(module);
75 sam_name = (const char *)ldb_get_opaque(ldb, "ldb_url");
78 return ldb_operr(ldb);
80 if (strncmp("tdb://", sam_name, 6) == 0) {
83 filename = talloc_asprintf(tmp_ctx, "%s.d/metadata.tdb", sam_name);
90 if (stat(filename, &statbuf) != 0) {
92 return LDB_ERR_OPERATIONS_ERROR;
95 lp_ctx = talloc_get_type_abort(ldb_get_opaque(ldb, "loadparm"),
96 struct loadparm_context);
98 data->metadata = tdb_wrap_open(data, filename, 10,
99 lpcfg_tdb_flags(lp_ctx, TDB_DEFAULT|TDB_SEQNUM),
101 if (data->metadata == NULL) {
102 talloc_free(tmp_ctx);
103 return LDB_ERR_OPERATIONS_ERROR;
106 talloc_free(tmp_ctx);
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)
114 struct tdb_context *tdb;
115 TDB_DATA tdb_key, tdb_data;
121 *value = default_value;
125 if (!data->metadata) {
126 return LDB_ERR_OPERATIONS_ERROR;
129 tdb_seqnum = tdb_get_seqnum(data->metadata->tdb);
130 if (tdb_seqnum == data->tdb_seqnum) {
131 *value = data->schema_seq_num_cache;
135 tmp_ctx = talloc_new(NULL);
136 if (tmp_ctx == NULL) {
137 return ldb_module_oom(data->module);
140 tdb = data->metadata->tdb;
142 tdb_key.dptr = (uint8_t *)discard_const_p(char, key);
143 tdb_key.dsize = strlen(key);
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);
152 talloc_free(tmp_ctx);
153 return ldb_module_error(data->module, LDB_ERR_OPERATIONS_ERROR,
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);
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
170 data->tdb_seqnum = tdb_seqnum;
171 data->schema_seq_num_cache = strtoull(value_str, NULL, 10);
173 *value = data->schema_seq_num_cache;
175 SAFE_FREE(tdb_data.dptr);
176 talloc_free(tmp_ctx);
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)
185 uint64_t schema_seq_num = 0;
187 struct ldb_context *ldb = ldb_module_get_ctx(module);
188 struct dsdb_schema *new_schema;
190 struct schema_load_private_data *private_data = talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
192 /* We can't refresh until the init function has run */
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) {
201 SMB_ASSERT(ev == ldb_get_event_context(ldb));
203 mem_ctx = talloc_new(module);
204 if (mem_ctx == NULL) {
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.
216 ret = schema_metadata_get_uint64(private_data, DSDB_METADATA_SCHEMA_SEQ_NUM, &schema_seq_num, 0);
218 if (schema != NULL) {
219 if (ret == LDB_SUCCESS) {
220 if (schema->metadata_usn == schema_seq_num) {
221 TALLOC_FREE(mem_ctx);
224 DEBUG(3, ("Schema refresh needed %lld != %lld\n",
225 (unsigned long long)schema->metadata_usn,
226 (unsigned long long)schema_seq_num));
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);
236 DEBUG(10, ("Initial schema load needed, as we have no existing schema, seq_num: %lld\n",
237 (unsigned long long)schema_seq_num));
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);
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);
257 if (is_global_schema) {
258 dsdb_make_schema_global(ldb, new_schema);
260 TALLOC_FREE(mem_ctx);
266 Given an LDB module (pointing at the schema DB), and the DN, set the populated schema
269 static int dsdb_schema_from_db(struct ldb_module *module,
271 uint64_t schema_seq_num,
272 struct dsdb_schema **schema)
274 struct ldb_context *ldb = ldb_module_get_ctx(module);
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[] = {
287 static const char *schema_attrs[] = {
288 DSDB_SCHEMA_COMMON_ATTRS,
289 DSDB_SCHEMA_ATTR_ATTRS,
290 DSDB_SCHEMA_CLASS_ATTRS,
295 tmp_ctx = talloc_new(module);
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);
305 * setup the prefix mappings and schema info
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");
315 } else if (ret != LDB_SUCCESS) {
316 ldb_asprintf_errstring(ldb,
317 "dsdb_schema: failed to search the schema head: %s",
323 * load the attribute definitions.
325 ret = dsdb_module_search(module, tmp_ctx, &res,
326 schema_dn, LDB_SCOPE_ONELEVEL,
328 DSDB_FLAG_NEXT_MODULE |
329 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT,
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",
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",
348 (*schema)->metadata_usn = schema_seq_num;
350 talloc_steal(mem_ctx, *schema);
353 if (flags & LDB_FLG_ENABLE_TRACING) {
354 flags = ldb_get_flags(ldb);
355 ldb_set_flags(ldb, flags | LDB_FLG_ENABLE_TRACING);
357 talloc_free(tmp_ctx);
362 static int schema_load_init(struct ldb_module *module)
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;
370 private_data = talloc_zero(module, struct schema_load_private_data);
371 if (private_data == NULL) {
374 private_data->module = module;
376 ldb_module_set_private(module, private_data);
378 ret = ldb_next_init(module);
379 if (ret != LDB_SUCCESS) {
383 schema = dsdb_get_schema(ldb, NULL);
385 metadata_ret = schema_metadata_open(module);
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);
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));
404 readOnlySchema = ldb_get_opaque(ldb, "readOnlySchema");
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));
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));
428 } else if (metadata_ret == LDB_SUCCESS) {
429 ret = dsdb_set_schema_refresh_function(ldb, dsdb_schema_refresh, module);
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));
438 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
439 "schema_load_init: failed to open metadata.tdb");
443 schema = dsdb_get_schema(ldb, NULL);
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;
455 static int schema_search(struct ldb_module *module, struct ldb_request *req)
457 struct ldb_context *ldb = ldb_module_get_ctx(module);
459 /* Try the schema refresh now */
460 dsdb_get_schema(ldb, NULL);
462 return ldb_next_request(module, req);
465 static int schema_load_start_transaction(struct ldb_module *module)
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;
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;
479 private_data->in_transaction = true;
481 return ldb_next_start_trans(module);
484 static int schema_load_end_transaction(struct ldb_module *module)
486 struct schema_load_private_data *private_data =
487 talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
489 private_data->in_transaction = false;
491 return ldb_next_end_trans(module);
494 static int schema_load_del_transaction(struct ldb_module *module)
496 struct schema_load_private_data *private_data =
497 talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
499 private_data->in_transaction = false;
501 return ldb_next_del_trans(module);
504 static int schema_load_extended(struct ldb_module *module, struct ldb_request *req)
506 struct ldb_context *ldb = ldb_module_get_ctx(module);
508 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID) != 0) {
509 return ldb_next_request(module, req);
511 /* Force a refresh */
512 dsdb_get_schema(ldb, NULL);
514 /* Pass to next module, the partition one should finish the chain */
515 return ldb_next_request(module, req);
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,
529 int ldb_schema_load_module_init(const char *version)
531 LDB_MODULE_CHECK_VERSION(version);
532 return ldb_register_module(&ldb_schema_load_module_ops);