dsdb-schema: do not reload more often than schema_reload_interval
[Samba/gbeck.git] / source4 / dsdb / samdb / ldb_modules / schema_load.c
blobf922aabba90e2e96c02b5245e56bf505fcd7e935
1 /*
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/>.
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 "dsdb/samdb/ldb_modules/util.h"
34 struct schema_load_private_data {
35 bool in_transaction;
38 static int dsdb_schema_from_db(struct ldb_module *module, struct ldb_dn *schema_dn, uint64_t current_usn,
39 struct dsdb_schema **schema);
41 static struct dsdb_schema *dsdb_schema_refresh(struct ldb_module *module, struct dsdb_schema *schema, bool is_global_schema)
43 uint64_t current_usn;
44 int ret;
45 struct ldb_result *res;
46 struct ldb_request *treq;
47 struct ldb_seqnum_request *tseq;
48 struct ldb_seqnum_result *tseqr;
49 struct dsdb_control_current_partition *ctrl;
50 struct ldb_context *ldb = ldb_module_get_ctx(module);
51 struct dsdb_schema *new_schema;
52 int interval;
53 time_t ts, lastts;
54 struct loadparm_context *lp_ctx =
55 (struct loadparm_context *)ldb_get_opaque(ldb, "loadparm");
57 struct schema_load_private_data *private_data = talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
58 if (!private_data) {
59 /* We can't refresh until the init function has run */
60 return schema;
63 /* We don't allow a schema reload during a transaction - nobody else can modify our schema behind our backs */
64 if (private_data->in_transaction) {
65 return schema;
68 lastts = schema->last_refresh;
69 ts = time(NULL);
70 interval = lpcfg_parm_int(lp_ctx, NULL, "dsdb", "schema_reload_interval", 120);
71 if (lastts > (ts - interval)) {
72 DEBUG(11, ("Less than %d seconds since last reload, returning cached version ts = %d\n", interval, (int)lastts));
73 return schema;
76 res = talloc_zero(schema, struct ldb_result);
77 if (res == NULL) {
78 return NULL;
80 tseq = talloc_zero(res, struct ldb_seqnum_request);
81 if (tseq == NULL) {
82 talloc_free(res);
83 return NULL;
85 tseq->type = LDB_SEQ_HIGHEST_SEQ;
87 ret = ldb_build_extended_req(&treq, ldb_module_get_ctx(module), res,
88 LDB_EXTENDED_SEQUENCE_NUMBER,
89 tseq,
90 NULL,
91 res,
92 ldb_extended_default_callback,
93 NULL);
94 LDB_REQ_SET_LOCATION(treq);
95 if (ret != LDB_SUCCESS) {
96 talloc_free(res);
97 return NULL;
101 * We update right now the last refresh timestamp so that if
102 * the schema partition hasn't change we don't keep on retrying.
103 * Otherwise if the timestamp was update only when the schema has
104 * actually changed (and therefor completely reloaded) we would
105 * continue to hit the database to get the highest USN.
107 schema->last_refresh = ts;
109 ctrl = talloc(treq, struct dsdb_control_current_partition);
110 if (!ctrl) {
111 talloc_free(res);
112 return NULL;
114 ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
115 ctrl->dn = schema->base_dn;
117 ret = ldb_request_add_control(treq,
118 DSDB_CONTROL_CURRENT_PARTITION_OID,
119 false, ctrl);
120 if (ret != LDB_SUCCESS) {
121 talloc_free(res);
122 return NULL;
125 ret = ldb_next_request(module, treq);
126 if (ret != LDB_SUCCESS) {
127 talloc_free(res);
128 return NULL;
130 ret = ldb_wait(treq->handle, LDB_WAIT_ALL);
131 if (ret != LDB_SUCCESS) {
132 talloc_free(res);
133 return NULL;
135 tseqr = talloc_get_type(res->extended->data,
136 struct ldb_seqnum_result);
137 if (tseqr->seq_num == schema->reload_seq_number) {
138 talloc_free(res);
139 return schema;
142 schema->reload_seq_number = tseqr->seq_num;
143 talloc_free(res);
145 ret = dsdb_module_load_partition_usn(module, schema->base_dn, &current_usn, NULL, NULL);
146 if (ret != LDB_SUCCESS || current_usn == schema->loaded_usn) {
147 return schema;
150 ret = dsdb_schema_from_db(module, schema->base_dn, current_usn, &new_schema);
151 if (ret != LDB_SUCCESS) {
152 return schema;
155 if (is_global_schema) {
156 dsdb_make_schema_global(ldb, new_schema);
158 return new_schema;
163 Given an LDB module (pointing at the schema DB), and the DN, set the populated schema
166 static int dsdb_schema_from_db(struct ldb_module *module, struct ldb_dn *schema_dn, uint64_t current_usn,
167 struct dsdb_schema **schema)
169 struct ldb_context *ldb = ldb_module_get_ctx(module);
170 TALLOC_CTX *tmp_ctx;
171 char *error_string;
172 int ret;
173 struct ldb_result *schema_res;
174 struct ldb_result *res;
175 static const char *schema_attrs[] = {
176 "prefixMap",
177 "schemaInfo",
178 "fSMORoleOwner",
179 NULL
181 unsigned flags;
183 tmp_ctx = talloc_new(module);
184 if (!tmp_ctx) {
185 return ldb_oom(ldb);
188 /* we don't want to trace the schema load */
189 flags = ldb_get_flags(ldb);
190 ldb_set_flags(ldb, flags & ~LDB_FLG_ENABLE_TRACING);
193 * setup the prefix mappings and schema info
195 ret = dsdb_module_search_dn(module, tmp_ctx, &schema_res,
196 schema_dn, schema_attrs,
197 DSDB_FLAG_NEXT_MODULE, NULL);
198 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
199 ldb_reset_err_string(ldb);
200 ldb_debug(ldb, LDB_DEBUG_WARNING,
201 "schema_load_init: no schema head present: (skip schema loading)\n");
202 goto failed;
203 } else if (ret != LDB_SUCCESS) {
204 ldb_asprintf_errstring(ldb,
205 "dsdb_schema: failed to search the schema head: %s",
206 ldb_errstring(ldb));
207 goto failed;
211 * load the attribute definitions
213 ret = dsdb_module_search(module, tmp_ctx, &res,
214 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
215 DSDB_FLAG_NEXT_MODULE |
216 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT,
217 NULL,
218 "(|(objectClass=attributeSchema)(objectClass=classSchema))");
219 if (ret != LDB_SUCCESS) {
220 ldb_asprintf_errstring(ldb,
221 "dsdb_schema: failed to search attributeSchema and classSchema objects: %s",
222 ldb_errstring(ldb));
223 goto failed;
226 ret = dsdb_schema_from_ldb_results(tmp_ctx, ldb,
227 schema_res, res, schema, &error_string);
228 if (ret != LDB_SUCCESS) {
229 ldb_asprintf_errstring(ldb,
230 "dsdb_schema load failed: %s",
231 error_string);
232 goto failed;
235 (*schema)->refresh_in_progress = true;
237 /* If we have the readOnlySchema opaque, then don't check for
238 * runtime schema updates, as they are not permitted (we would
239 * have to update the backend server schema too */
240 if (!ldb_get_opaque(ldb, "readOnlySchema")) {
241 (*schema)->refresh_fn = dsdb_schema_refresh;
242 (*schema)->loaded_from_module = module;
243 (*schema)->loaded_usn = current_usn;
246 /* "dsdb_set_schema()" steals schema into the ldb_context */
247 ret = dsdb_set_schema(ldb, (*schema));
249 (*schema)->refresh_in_progress = false;
250 (*schema)->last_refresh = time(NULL);
252 if (ret != LDB_SUCCESS) {
253 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
254 "schema_load_init: dsdb_set_schema() failed: %d:%s: %s",
255 ret, ldb_strerror(ret), ldb_errstring(ldb));
256 goto failed;
259 /* Ensure this module won't go away before the callback */
260 if (talloc_reference(*schema, ldb) == NULL) {
261 ldb_oom(ldb);
262 ret = LDB_ERR_OPERATIONS_ERROR;
265 failed:
266 if (flags & LDB_FLG_ENABLE_TRACING) {
267 flags = ldb_get_flags(ldb);
268 ldb_set_flags(ldb, flags | LDB_FLG_ENABLE_TRACING);
270 talloc_free(tmp_ctx);
271 return ret;
275 static int schema_load_init(struct ldb_module *module)
277 struct schema_load_private_data *private_data;
278 struct dsdb_schema *schema;
279 struct ldb_context *ldb = ldb_module_get_ctx(module);
280 int ret;
281 uint64_t current_usn;
282 struct ldb_dn *schema_dn;
284 private_data = talloc_zero(module, struct schema_load_private_data);
285 if (private_data == NULL) {
286 return ldb_oom(ldb);
289 ldb_module_set_private(module, private_data);
291 ret = ldb_next_init(module);
292 if (ret != LDB_SUCCESS) {
293 return ret;
296 if (dsdb_get_schema(ldb, NULL)) {
297 return LDB_SUCCESS;
300 schema_dn = ldb_get_schema_basedn(ldb);
301 if (!schema_dn) {
302 ldb_reset_err_string(ldb);
303 ldb_debug(ldb, LDB_DEBUG_WARNING,
304 "schema_load_init: no schema dn present: (skip schema loading)\n");
305 return LDB_SUCCESS;
308 ret = dsdb_module_load_partition_usn(module, schema_dn, &current_usn, NULL, NULL);
309 if (ret != LDB_SUCCESS) {
310 /* Ignore the error and just reload the DB more often */
311 current_usn = 0;
314 return dsdb_schema_from_db(module, schema_dn, current_usn, &schema);
317 static int schema_load_start_transaction(struct ldb_module *module)
319 struct schema_load_private_data *private_data =
320 talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
322 private_data->in_transaction = true;
324 return ldb_next_start_trans(module);
327 static int schema_load_end_transaction(struct ldb_module *module)
329 struct schema_load_private_data *private_data =
330 talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
332 private_data->in_transaction = false;
334 return ldb_next_end_trans(module);
337 static int schema_load_del_transaction(struct ldb_module *module)
339 struct schema_load_private_data *private_data =
340 talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
342 private_data->in_transaction = false;
344 return ldb_next_del_trans(module);
347 static int schema_load_extended(struct ldb_module *module, struct ldb_request *req)
349 time_t *lastts;
350 struct ldb_context *ldb = ldb_module_get_ctx(module);
351 struct dsdb_schema *schema;
353 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID) != 0) {
354 return ldb_next_request(module, req);
356 lastts = (time_t *)ldb_get_opaque(ldb, DSDB_OPAQUE_LAST_SCHEMA_UPDATE_MSG_OPAQUE_NAME);
357 if (!lastts) {
358 lastts = talloc(ldb, time_t);
360 schema = dsdb_get_schema(ldb, NULL);
361 /* Force a refresh */
362 schema->last_refresh = 0;
363 *lastts = 0;
364 ldb_set_opaque(ldb, DSDB_OPAQUE_LAST_SCHEMA_UPDATE_MSG_OPAQUE_NAME, lastts);
366 /* This is a no-op. We reload as soon as we can */
367 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
371 static const struct ldb_module_ops ldb_schema_load_module_ops = {
372 .name = "schema_load",
373 .init_context = schema_load_init,
374 .extended = schema_load_extended,
375 .start_transaction = schema_load_start_transaction,
376 .end_transaction = schema_load_end_transaction,
377 .del_transaction = schema_load_del_transaction,
380 int ldb_schema_load_module_init(const char *version)
382 LDB_MODULE_CHECK_VERSION(version);
383 return ldb_register_module(&ldb_schema_load_module_ops);