1 | /*
|
---|
2 | Partitions ldb module
|
---|
3 |
|
---|
4 | Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
|
---|
5 | Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * Name: ldb
|
---|
23 | *
|
---|
24 | * Component: ldb partitions module
|
---|
25 | *
|
---|
26 | * Description: Implement LDAP partitions
|
---|
27 | *
|
---|
28 | * Author: Andrew Bartlett
|
---|
29 | * Author: Stefan Metzmacher
|
---|
30 | */
|
---|
31 |
|
---|
32 | #include "dsdb/samdb/ldb_modules/partition.h"
|
---|
33 | #include "lib/util/tsort.h"
|
---|
34 | #include "lib/ldb-samba/ldb_wrap.h"
|
---|
35 | #include "system/filesys.h"
|
---|
36 |
|
---|
37 | static int partition_sort_compare(const void *v1, const void *v2)
|
---|
38 | {
|
---|
39 | const struct dsdb_partition *p1;
|
---|
40 | const struct dsdb_partition *p2;
|
---|
41 |
|
---|
42 | p1 = *((struct dsdb_partition * const*)v1);
|
---|
43 | p2 = *((struct dsdb_partition * const*)v2);
|
---|
44 |
|
---|
45 | return ldb_dn_compare(p1->ctrl->dn, p2->ctrl->dn);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /* Load the list of DNs that we must replicate to all partitions */
|
---|
49 | static int partition_load_replicate_dns(struct ldb_context *ldb,
|
---|
50 | struct partition_private_data *data,
|
---|
51 | struct ldb_message *msg)
|
---|
52 | {
|
---|
53 | struct ldb_message_element *replicate_attributes = ldb_msg_find_element(msg, "replicateEntries");
|
---|
54 |
|
---|
55 | talloc_free(data->replicate);
|
---|
56 | if (!replicate_attributes) {
|
---|
57 | data->replicate = NULL;
|
---|
58 | } else {
|
---|
59 | unsigned int i;
|
---|
60 | data->replicate = talloc_array(data, struct ldb_dn *, replicate_attributes->num_values + 1);
|
---|
61 | if (!data->replicate) {
|
---|
62 | return ldb_oom(ldb);
|
---|
63 | }
|
---|
64 |
|
---|
65 | for (i=0; i < replicate_attributes->num_values; i++) {
|
---|
66 | data->replicate[i] = ldb_dn_from_ldb_val(data->replicate, ldb, &replicate_attributes->values[i]);
|
---|
67 | if (!ldb_dn_validate(data->replicate[i])) {
|
---|
68 | ldb_asprintf_errstring(ldb,
|
---|
69 | "partition_init: "
|
---|
70 | "invalid DN in partition replicate record: %s",
|
---|
71 | replicate_attributes->values[i].data);
|
---|
72 | return LDB_ERR_CONSTRAINT_VIOLATION;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | data->replicate[i] = NULL;
|
---|
76 | }
|
---|
77 | return LDB_SUCCESS;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /* Load the list of modules for the partitions */
|
---|
81 | static int partition_load_modules(struct ldb_context *ldb,
|
---|
82 | struct partition_private_data *data, struct ldb_message *msg)
|
---|
83 | {
|
---|
84 | unsigned int i;
|
---|
85 | struct ldb_message_element *modules_attributes = ldb_msg_find_element(msg, "modules");
|
---|
86 | talloc_free(data->modules);
|
---|
87 | if (!modules_attributes) {
|
---|
88 | return LDB_SUCCESS;
|
---|
89 | }
|
---|
90 |
|
---|
91 | data->modules = talloc_array(data, struct partition_module *, modules_attributes->num_values + 1);
|
---|
92 | if (!data->modules) {
|
---|
93 | return ldb_oom(ldb);
|
---|
94 | }
|
---|
95 |
|
---|
96 | for (i=0; i < modules_attributes->num_values; i++) {
|
---|
97 | char *p;
|
---|
98 | DATA_BLOB dn_blob;
|
---|
99 | data->modules[i] = talloc(data->modules, struct partition_module);
|
---|
100 | if (!data->modules[i]) {
|
---|
101 | return ldb_oom(ldb);
|
---|
102 | }
|
---|
103 |
|
---|
104 | dn_blob = modules_attributes->values[i];
|
---|
105 |
|
---|
106 | p = strchr((const char *)dn_blob.data, ':');
|
---|
107 | if (!p) {
|
---|
108 | ldb_asprintf_errstring(ldb,
|
---|
109 | "partition_load_modules: "
|
---|
110 | "invalid form for partition module record (missing ':'): %s", (const char *)dn_blob.data);
|
---|
111 | return LDB_ERR_CONSTRAINT_VIOLATION;
|
---|
112 | }
|
---|
113 | /* Now trim off the filename */
|
---|
114 | dn_blob.length = ((uint8_t *)p - dn_blob.data);
|
---|
115 |
|
---|
116 | p++;
|
---|
117 | data->modules[i]->modules = ldb_modules_list_from_string(ldb, data->modules[i],
|
---|
118 | p);
|
---|
119 |
|
---|
120 | if (dn_blob.length == 1 && dn_blob.data[0] == '*') {
|
---|
121 | data->modules[i]->dn = NULL;
|
---|
122 | } else {
|
---|
123 | data->modules[i]->dn = ldb_dn_from_ldb_val(data->modules[i], ldb, &dn_blob);
|
---|
124 | if (!data->modules[i]->dn || !ldb_dn_validate(data->modules[i]->dn)) {
|
---|
125 | return ldb_operr(ldb);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 | data->modules[i] = NULL;
|
---|
130 | return LDB_SUCCESS;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static int partition_reload_metadata(struct ldb_module *module, struct partition_private_data *data,
|
---|
134 | TALLOC_CTX *mem_ctx, struct ldb_message **_msg,
|
---|
135 | struct ldb_request *parent)
|
---|
136 | {
|
---|
137 | int ret;
|
---|
138 | struct ldb_message *msg, *module_msg;
|
---|
139 | struct ldb_result *res;
|
---|
140 | struct ldb_context *ldb = ldb_module_get_ctx(module);
|
---|
141 | const char *attrs[] = { "partition", "replicateEntries", "modules", "ldapBackend", NULL };
|
---|
142 | /* perform search for @PARTITION, looking for module, replicateEntries and ldapBackend */
|
---|
143 | ret = dsdb_module_search_dn(module, mem_ctx, &res,
|
---|
144 | ldb_dn_new(mem_ctx, ldb, DSDB_PARTITION_DN),
|
---|
145 | attrs,
|
---|
146 | DSDB_FLAG_NEXT_MODULE, parent);
|
---|
147 | if (ret != LDB_SUCCESS) {
|
---|
148 | return ret;
|
---|
149 | }
|
---|
150 |
|
---|
151 | msg = res->msgs[0];
|
---|
152 |
|
---|
153 | ret = partition_load_replicate_dns(ldb, data, msg);
|
---|
154 | if (ret != LDB_SUCCESS) {
|
---|
155 | return ret;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /* When used from Samba4, this message is set by the samba4
|
---|
159 | * module, as a fixed value not read from the DB. This avoids
|
---|
160 | * listing modules in the DB */
|
---|
161 | if (data->forced_module_msg) {
|
---|
162 | module_msg = data->forced_module_msg;
|
---|
163 | } else {
|
---|
164 | module_msg = msg;
|
---|
165 | }
|
---|
166 |
|
---|
167 | ret = partition_load_modules(ldb, data, module_msg);
|
---|
168 | if (ret != LDB_SUCCESS) {
|
---|
169 | return ret;
|
---|
170 | }
|
---|
171 |
|
---|
172 | data->ldapBackend = talloc_steal(data, ldb_msg_find_attr_as_string(msg, "ldapBackend", NULL));
|
---|
173 | if (_msg) {
|
---|
174 | *_msg = msg;
|
---|
175 | } else {
|
---|
176 | talloc_free(msg);
|
---|
177 | }
|
---|
178 |
|
---|
179 | return LDB_SUCCESS;
|
---|
180 | }
|
---|
181 |
|
---|
182 | static const char **find_modules_for_dn(struct partition_private_data *data, struct ldb_dn *dn)
|
---|
183 | {
|
---|
184 | unsigned int i;
|
---|
185 | struct partition_module *default_mod = NULL;
|
---|
186 | for (i=0; data->modules && data->modules[i]; i++) {
|
---|
187 | if (!data->modules[i]->dn) {
|
---|
188 | default_mod = data->modules[i];
|
---|
189 | } else if (ldb_dn_compare(dn, data->modules[i]->dn) == 0) {
|
---|
190 | return data->modules[i]->modules;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | if (default_mod) {
|
---|
194 | return default_mod->modules;
|
---|
195 | } else {
|
---|
196 | return NULL;
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | static int new_partition_from_dn(struct ldb_context *ldb, struct partition_private_data *data,
|
---|
201 | TALLOC_CTX *mem_ctx,
|
---|
202 | struct ldb_dn *dn, const char *filename,
|
---|
203 | struct dsdb_partition **partition) {
|
---|
204 | const char *backend_url;
|
---|
205 | struct dsdb_control_current_partition *ctrl;
|
---|
206 | struct ldb_module *backend_module;
|
---|
207 | struct ldb_module *module_chain;
|
---|
208 | const char **modules;
|
---|
209 | int ret;
|
---|
210 |
|
---|
211 | (*partition) = talloc(mem_ctx, struct dsdb_partition);
|
---|
212 | if (!*partition) {
|
---|
213 | return ldb_oom(ldb);
|
---|
214 | }
|
---|
215 |
|
---|
216 | (*partition)->ctrl = ctrl = talloc((*partition), struct dsdb_control_current_partition);
|
---|
217 | if (!ctrl) {
|
---|
218 | talloc_free(*partition);
|
---|
219 | return ldb_oom(ldb);
|
---|
220 | }
|
---|
221 |
|
---|
222 | /* See if an LDAP backend has been specified */
|
---|
223 | if (data->ldapBackend) {
|
---|
224 | (*partition)->backend_url = data->ldapBackend;
|
---|
225 | } else {
|
---|
226 | /* the backend LDB is the DN (base64 encoded if not 'plain') followed by .ldb */
|
---|
227 | backend_url = ldb_relative_path(ldb,
|
---|
228 | *partition,
|
---|
229 | filename);
|
---|
230 | if (!backend_url) {
|
---|
231 | ldb_asprintf_errstring(ldb,
|
---|
232 | "partition_init: unable to determine an relative path for partition: %s", filename);
|
---|
233 | talloc_free(*partition);
|
---|
234 | return LDB_ERR_OPERATIONS_ERROR;
|
---|
235 | }
|
---|
236 | (*partition)->backend_url = talloc_steal((*partition), backend_url);
|
---|
237 |
|
---|
238 | if (!(ldb_module_flags(ldb) & LDB_FLG_RDONLY)) {
|
---|
239 | char *p;
|
---|
240 | char *backend_dir = talloc_strdup(*partition, backend_url);
|
---|
241 |
|
---|
242 | p = strrchr(backend_dir, '/');
|
---|
243 | if (p) {
|
---|
244 | p[0] = '\0';
|
---|
245 | }
|
---|
246 |
|
---|
247 | /* Failure is quite reasonable, it might alredy exist */
|
---|
248 | mkdir(backend_dir, 0700);
|
---|
249 | talloc_free(backend_dir);
|
---|
250 | }
|
---|
251 |
|
---|
252 | }
|
---|
253 |
|
---|
254 | ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
|
---|
255 | ctrl->dn = talloc_steal(ctrl, dn);
|
---|
256 |
|
---|
257 | ret = ldb_module_connect_backend(ldb, (*partition)->backend_url, NULL, &backend_module);
|
---|
258 | if (ret != LDB_SUCCESS) {
|
---|
259 | return ret;
|
---|
260 | }
|
---|
261 | talloc_steal((*partition), backend_module);
|
---|
262 |
|
---|
263 | modules = find_modules_for_dn(data, dn);
|
---|
264 |
|
---|
265 | if (!modules) {
|
---|
266 | DEBUG(0, ("Unable to load partition modules for new DN %s, perhaps you need to reprovision? See partition-upgrade.txt for instructions\n", ldb_dn_get_linearized(dn)));
|
---|
267 | talloc_free(*partition);
|
---|
268 | return LDB_ERR_CONSTRAINT_VIOLATION;
|
---|
269 | }
|
---|
270 | ret = ldb_module_load_list(ldb, modules, backend_module, &module_chain);
|
---|
271 | if (ret != LDB_SUCCESS) {
|
---|
272 | ldb_asprintf_errstring(ldb,
|
---|
273 | "partition_init: "
|
---|
274 | "loading backend for %s failed: %s",
|
---|
275 | ldb_dn_get_linearized(dn), ldb_errstring(ldb));
|
---|
276 | talloc_free(*partition);
|
---|
277 | return ret;
|
---|
278 | }
|
---|
279 | ret = ldb_module_init_chain(ldb, module_chain);
|
---|
280 | if (ret != LDB_SUCCESS) {
|
---|
281 | ldb_asprintf_errstring(ldb,
|
---|
282 | "partition_init: "
|
---|
283 | "initialising backend for %s failed: %s",
|
---|
284 | ldb_dn_get_linearized(dn), ldb_errstring(ldb));
|
---|
285 | talloc_free(*partition);
|
---|
286 | return ret;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /* This weirdness allows us to use ldb_next_request() in partition.c */
|
---|
290 | (*partition)->module = ldb_module_new(*partition, ldb, "partition_next", NULL);
|
---|
291 | if (!(*partition)->module) {
|
---|
292 | talloc_free(*partition);
|
---|
293 | return ldb_oom(ldb);
|
---|
294 | }
|
---|
295 | ldb_module_set_next((*partition)->module, talloc_steal((*partition)->module, module_chain));
|
---|
296 |
|
---|
297 | /* if we were in a transaction then we need to start a
|
---|
298 | transaction on this new partition, otherwise we'll get a
|
---|
299 | transaction mismatch when we end the transaction */
|
---|
300 | if (data->in_transaction) {
|
---|
301 | if (ldb_module_flags(ldb) & LDB_FLG_ENABLE_TRACING) {
|
---|
302 | ldb_debug(ldb, LDB_DEBUG_TRACE, "partition_start_trans() -> %s (new partition)",
|
---|
303 | ldb_dn_get_linearized((*partition)->ctrl->dn));
|
---|
304 | }
|
---|
305 | ret = ldb_next_start_trans((*partition)->module);
|
---|
306 | }
|
---|
307 |
|
---|
308 | return ret;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /* Tell the rootDSE about the new partition */
|
---|
312 | static int partition_register(struct ldb_context *ldb, struct dsdb_control_current_partition *ctrl)
|
---|
313 | {
|
---|
314 | struct ldb_request *req;
|
---|
315 | int ret;
|
---|
316 |
|
---|
317 | req = talloc_zero(NULL, struct ldb_request);
|
---|
318 | if (req == NULL) {
|
---|
319 | return ldb_oom(ldb);
|
---|
320 | }
|
---|
321 |
|
---|
322 | req->operation = LDB_REQ_REGISTER_PARTITION;
|
---|
323 | req->op.reg_partition.dn = ctrl->dn;
|
---|
324 | req->callback = ldb_op_default_callback;
|
---|
325 |
|
---|
326 | ldb_set_timeout(ldb, req, 0);
|
---|
327 |
|
---|
328 | req->handle = ldb_handle_new(req, ldb);
|
---|
329 | if (req->handle == NULL) {
|
---|
330 | talloc_free(req);
|
---|
331 | return ldb_operr(ldb);
|
---|
332 | }
|
---|
333 |
|
---|
334 | ret = ldb_request(ldb, req);
|
---|
335 | if (ret == LDB_SUCCESS) {
|
---|
336 | ret = ldb_wait(req->handle, LDB_WAIT_ALL);
|
---|
337 | }
|
---|
338 | if (ret != LDB_SUCCESS) {
|
---|
339 | ldb_debug(ldb, LDB_DEBUG_ERROR, "partition: Unable to register partition with rootdse!\n");
|
---|
340 | talloc_free(req);
|
---|
341 | return LDB_ERR_OTHER;
|
---|
342 | }
|
---|
343 | talloc_free(req);
|
---|
344 |
|
---|
345 | return LDB_SUCCESS;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /* Add a newly found partition to the global data */
|
---|
349 | static int add_partition_to_data(struct ldb_context *ldb, struct partition_private_data *data,
|
---|
350 | struct dsdb_partition *partition)
|
---|
351 | {
|
---|
352 | unsigned int i;
|
---|
353 | int ret;
|
---|
354 |
|
---|
355 | /* Count the partitions */
|
---|
356 | for (i=0; data->partitions && data->partitions[i]; i++) { /* noop */};
|
---|
357 |
|
---|
358 | /* Add partition to list of partitions */
|
---|
359 | data->partitions = talloc_realloc(data, data->partitions, struct dsdb_partition *, i + 2);
|
---|
360 | if (!data->partitions) {
|
---|
361 | return ldb_oom(ldb);
|
---|
362 | }
|
---|
363 | data->partitions[i] = talloc_steal(data->partitions, partition);
|
---|
364 | data->partitions[i+1] = NULL;
|
---|
365 |
|
---|
366 | /* Sort again (should use binary insert) */
|
---|
367 | TYPESAFE_QSORT(data->partitions, i+1, partition_sort_compare);
|
---|
368 |
|
---|
369 | ret = partition_register(ldb, partition->ctrl);
|
---|
370 | if (ret != LDB_SUCCESS) {
|
---|
371 | return ret;
|
---|
372 | }
|
---|
373 | return LDB_SUCCESS;
|
---|
374 | }
|
---|
375 |
|
---|
376 | int partition_reload_if_required(struct ldb_module *module,
|
---|
377 | struct partition_private_data *data,
|
---|
378 | struct ldb_request *parent)
|
---|
379 | {
|
---|
380 | uint64_t seq;
|
---|
381 | int ret;
|
---|
382 | unsigned int i;
|
---|
383 | struct ldb_context *ldb = ldb_module_get_ctx(module);
|
---|
384 | struct ldb_message *msg;
|
---|
385 | struct ldb_message_element *partition_attributes;
|
---|
386 | TALLOC_CTX *mem_ctx;
|
---|
387 |
|
---|
388 | if (!data) {
|
---|
389 | /* Not initialised yet */
|
---|
390 | return LDB_SUCCESS;
|
---|
391 | }
|
---|
392 |
|
---|
393 | mem_ctx = talloc_new(data);
|
---|
394 | if (!mem_ctx) {
|
---|
395 | return ldb_oom(ldb);
|
---|
396 | }
|
---|
397 |
|
---|
398 | ret = partition_primary_sequence_number(module, mem_ctx, LDB_SEQ_HIGHEST_SEQ, &seq);
|
---|
399 | if (ret != LDB_SUCCESS) {
|
---|
400 | talloc_free(mem_ctx);
|
---|
401 | return ret;
|
---|
402 | }
|
---|
403 | if (seq == data->metadata_seq) {
|
---|
404 | talloc_free(mem_ctx);
|
---|
405 | return LDB_SUCCESS;
|
---|
406 | }
|
---|
407 |
|
---|
408 | ret = partition_reload_metadata(module, data, mem_ctx, &msg, parent);
|
---|
409 | if (ret != LDB_SUCCESS) {
|
---|
410 | talloc_free(mem_ctx);
|
---|
411 | return ret;
|
---|
412 | }
|
---|
413 |
|
---|
414 | data->metadata_seq = seq;
|
---|
415 |
|
---|
416 | partition_attributes = ldb_msg_find_element(msg, "partition");
|
---|
417 |
|
---|
418 | for (i=0; partition_attributes && i < partition_attributes->num_values; i++) {
|
---|
419 | unsigned int j;
|
---|
420 | bool new_partition = true;
|
---|
421 | const char *filename = NULL;
|
---|
422 | DATA_BLOB dn_blob;
|
---|
423 | struct ldb_dn *dn;
|
---|
424 | struct dsdb_partition *partition;
|
---|
425 | struct ldb_result *dn_res;
|
---|
426 | const char *no_attrs[] = { NULL };
|
---|
427 |
|
---|
428 | for (j=0; data->partitions && data->partitions[j]; j++) {
|
---|
429 | if (data_blob_cmp(&data->partitions[j]->orig_record, &partition_attributes->values[i]) == 0) {
|
---|
430 | new_partition = false;
|
---|
431 | break;
|
---|
432 | }
|
---|
433 | }
|
---|
434 | if (new_partition == false) {
|
---|
435 | continue;
|
---|
436 | }
|
---|
437 |
|
---|
438 | dn_blob = partition_attributes->values[i];
|
---|
439 |
|
---|
440 | if (dn_blob.length > 4 &&
|
---|
441 | (strncmp((const char *)&dn_blob.data[dn_blob.length-4], ".ldb", 4) == 0)) {
|
---|
442 |
|
---|
443 | /* Look for DN:filename.ldb */
|
---|
444 | char *p = strrchr((const char *)dn_blob.data, ':');
|
---|
445 | if (!p) {
|
---|
446 | ldb_asprintf_errstring(ldb,
|
---|
447 | "partition_init: invalid DN in attempting to parse partition record: %s", (const char *)dn_blob.data);
|
---|
448 | talloc_free(mem_ctx);
|
---|
449 | return LDB_ERR_CONSTRAINT_VIOLATION;
|
---|
450 | }
|
---|
451 | filename = p+1;
|
---|
452 |
|
---|
453 | /* Now trim off the filename */
|
---|
454 | dn_blob.length = ((uint8_t *)p - dn_blob.data);
|
---|
455 | }
|
---|
456 |
|
---|
457 | dn = ldb_dn_from_ldb_val(mem_ctx, ldb, &dn_blob);
|
---|
458 | if (!dn) {
|
---|
459 | ldb_asprintf_errstring(ldb,
|
---|
460 | "partition_init: invalid DN in partition record: %s", (const char *)dn_blob.data);
|
---|
461 | talloc_free(mem_ctx);
|
---|
462 | return LDB_ERR_CONSTRAINT_VIOLATION;
|
---|
463 | }
|
---|
464 |
|
---|
465 | /* Now do a slow check with the DN compare */
|
---|
466 | for (j=0; data->partitions && data->partitions[j]; j++) {
|
---|
467 | if (ldb_dn_compare(dn, data->partitions[j]->ctrl->dn) == 0) {
|
---|
468 | new_partition = false;
|
---|
469 | break;
|
---|
470 | }
|
---|
471 | }
|
---|
472 | if (new_partition == false) {
|
---|
473 | continue;
|
---|
474 | }
|
---|
475 |
|
---|
476 | if (!filename) {
|
---|
477 | char *base64_dn = NULL;
|
---|
478 | const char *p;
|
---|
479 | for (p = ldb_dn_get_linearized(dn); *p; p++) {
|
---|
480 | /* We have such a strict check because I don't want shell metacharacters in the file name, nor ../ */
|
---|
481 | if (!(isalnum(*p) || *p == ' ' || *p == '=' || *p == ',')) {
|
---|
482 | break;
|
---|
483 | }
|
---|
484 | }
|
---|
485 | if (*p) {
|
---|
486 | base64_dn = ldb_base64_encode(data, ldb_dn_get_linearized(dn), strlen(ldb_dn_get_linearized(dn)));
|
---|
487 | filename = talloc_asprintf(mem_ctx, "%s.ldb", base64_dn);
|
---|
488 | } else {
|
---|
489 | filename = talloc_asprintf(mem_ctx, "%s.ldb", ldb_dn_get_linearized(dn));
|
---|
490 | }
|
---|
491 | }
|
---|
492 |
|
---|
493 | /* We call ldb_dn_get_linearized() because the DN in
|
---|
494 | * partition_attributes is already casefolded
|
---|
495 | * correctly. We don't want to mess that up as the
|
---|
496 | * schema isn't loaded yet */
|
---|
497 | ret = new_partition_from_dn(ldb, data, data->partitions, dn,
|
---|
498 | filename,
|
---|
499 | &partition);
|
---|
500 | if (ret != LDB_SUCCESS) {
|
---|
501 | talloc_free(mem_ctx);
|
---|
502 | return ret;
|
---|
503 | }
|
---|
504 |
|
---|
505 | talloc_steal(partition, partition_attributes->values[i].data);
|
---|
506 | partition->orig_record = partition_attributes->values[i];
|
---|
507 |
|
---|
508 | /* Get the 'correct' case of the partition DNs from the database */
|
---|
509 | ret = dsdb_module_search_dn(partition->module, data, &dn_res,
|
---|
510 | dn, no_attrs,
|
---|
511 | DSDB_FLAG_NEXT_MODULE, parent);
|
---|
512 | if (ret == LDB_SUCCESS) {
|
---|
513 | talloc_free(partition->ctrl->dn);
|
---|
514 | partition->ctrl->dn = talloc_steal(partition->ctrl, dn_res->msgs[0]->dn);
|
---|
515 | talloc_free(dn_res);
|
---|
516 | } else if (ret != LDB_ERR_NO_SUCH_OBJECT) {
|
---|
517 | ldb_asprintf_errstring(ldb,
|
---|
518 | "Failed to search for partition base %s in new partition at %s: %s",
|
---|
519 | ldb_dn_get_linearized(dn),
|
---|
520 | partition->backend_url,
|
---|
521 | ldb_errstring(ldb));
|
---|
522 | talloc_free(mem_ctx);
|
---|
523 | return ret;
|
---|
524 | }
|
---|
525 |
|
---|
526 | ret = add_partition_to_data(ldb, data, partition);
|
---|
527 | if (ret != LDB_SUCCESS) {
|
---|
528 | talloc_free(mem_ctx);
|
---|
529 | return ret;
|
---|
530 | }
|
---|
531 | }
|
---|
532 |
|
---|
533 | talloc_free(mem_ctx);
|
---|
534 | return LDB_SUCCESS;
|
---|
535 | }
|
---|
536 |
|
---|
537 | /* Copy the metadata (@OPTIONS etc) for the new partition into the partition */
|
---|
538 |
|
---|
539 | static int new_partition_set_replicated_metadata(struct ldb_context *ldb,
|
---|
540 | struct ldb_module *module, struct ldb_request *last_req,
|
---|
541 | struct partition_private_data *data,
|
---|
542 | struct dsdb_partition *partition)
|
---|
543 | {
|
---|
544 | unsigned int i;
|
---|
545 | int ret;
|
---|
546 | /* for each replicate, copy from main partition. If we get an error, we report it up the chain */
|
---|
547 | for (i=0; data->replicate && data->replicate[i]; i++) {
|
---|
548 | struct ldb_result *replicate_res;
|
---|
549 | struct ldb_request *add_req;
|
---|
550 | ret = dsdb_module_search_dn(module, last_req, &replicate_res,
|
---|
551 | data->replicate[i],
|
---|
552 | NULL,
|
---|
553 | DSDB_FLAG_NEXT_MODULE, NULL);
|
---|
554 | if (ret == LDB_ERR_NO_SUCH_OBJECT) {
|
---|
555 | continue;
|
---|
556 | }
|
---|
557 | if (ret != LDB_SUCCESS) {
|
---|
558 | ldb_asprintf_errstring(ldb,
|
---|
559 | "Failed to search for %s from " DSDB_PARTITION_DN
|
---|
560 | " replicateEntries for new partition at %s on %s: %s",
|
---|
561 | ldb_dn_get_linearized(data->replicate[i]),
|
---|
562 | partition->backend_url,
|
---|
563 | ldb_dn_get_linearized(partition->ctrl->dn),
|
---|
564 | ldb_errstring(ldb));
|
---|
565 | return ret;
|
---|
566 | }
|
---|
567 |
|
---|
568 | /* Build add request */
|
---|
569 | ret = ldb_build_add_req(&add_req, ldb, replicate_res,
|
---|
570 | replicate_res->msgs[0], NULL, NULL,
|
---|
571 | ldb_op_default_callback, last_req);
|
---|
572 | LDB_REQ_SET_LOCATION(add_req);
|
---|
573 | last_req = add_req;
|
---|
574 | if (ret != LDB_SUCCESS) {
|
---|
575 | /* return directly, this is a very unlikely error */
|
---|
576 | return ret;
|
---|
577 | }
|
---|
578 | /* do request */
|
---|
579 | ret = ldb_next_request(partition->module, add_req);
|
---|
580 | /* wait */
|
---|
581 | if (ret == LDB_SUCCESS) {
|
---|
582 | ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
|
---|
583 | }
|
---|
584 |
|
---|
585 | switch (ret) {
|
---|
586 | case LDB_SUCCESS:
|
---|
587 | break;
|
---|
588 |
|
---|
589 | case LDB_ERR_ENTRY_ALREADY_EXISTS:
|
---|
590 | /* Handle this case specially - if the
|
---|
591 | * metadata already exists, replace it */
|
---|
592 | {
|
---|
593 | struct ldb_request *del_req;
|
---|
594 |
|
---|
595 | /* Don't leave a confusing string in the ldb_errstring() */
|
---|
596 | ldb_reset_err_string(ldb);
|
---|
597 | /* Build del request */
|
---|
598 | ret = ldb_build_del_req(&del_req, ldb, replicate_res, replicate_res->msgs[0]->dn, NULL, NULL,
|
---|
599 | ldb_op_default_callback, last_req);
|
---|
600 | LDB_REQ_SET_LOCATION(del_req);
|
---|
601 | last_req = del_req;
|
---|
602 | if (ret != LDB_SUCCESS) {
|
---|
603 | /* return directly, this is a very unlikely error */
|
---|
604 | return ret;
|
---|
605 | }
|
---|
606 | /* do request */
|
---|
607 | ret = ldb_next_request(partition->module, del_req);
|
---|
608 |
|
---|
609 | /* wait */
|
---|
610 | if (ret == LDB_SUCCESS) {
|
---|
611 | ret = ldb_wait(del_req->handle, LDB_WAIT_ALL);
|
---|
612 | }
|
---|
613 | if (ret != LDB_SUCCESS) {
|
---|
614 | ldb_asprintf_errstring(ldb,
|
---|
615 | "Failed to delete (for re-add) %s from " DSDB_PARTITION_DN
|
---|
616 | " replicateEntries in new partition at %s on %s: %s",
|
---|
617 | ldb_dn_get_linearized(data->replicate[i]),
|
---|
618 | partition->backend_url,
|
---|
619 | ldb_dn_get_linearized(partition->ctrl->dn),
|
---|
620 | ldb_errstring(ldb));
|
---|
621 | return ret;
|
---|
622 | }
|
---|
623 |
|
---|
624 | /* Build add request */
|
---|
625 | ret = ldb_build_add_req(&add_req, ldb, replicate_res, replicate_res->msgs[0], NULL, NULL,
|
---|
626 | ldb_op_default_callback, last_req);
|
---|
627 | LDB_REQ_SET_LOCATION(add_req);
|
---|
628 | last_req = add_req;
|
---|
629 | if (ret != LDB_SUCCESS) {
|
---|
630 | /* return directly, this is a very unlikely error */
|
---|
631 | return ret;
|
---|
632 | }
|
---|
633 |
|
---|
634 | /* do the add again */
|
---|
635 | ret = ldb_next_request(partition->module, add_req);
|
---|
636 |
|
---|
637 | /* wait */
|
---|
638 | if (ret == LDB_SUCCESS) {
|
---|
639 | ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
|
---|
640 | }
|
---|
641 |
|
---|
642 | if (ret != LDB_SUCCESS) {
|
---|
643 | ldb_asprintf_errstring(ldb,
|
---|
644 | "Failed to add (after delete) %s from " DSDB_PARTITION_DN
|
---|
645 | " replicateEntries to new partition at %s on %s: %s",
|
---|
646 | ldb_dn_get_linearized(data->replicate[i]),
|
---|
647 | partition->backend_url,
|
---|
648 | ldb_dn_get_linearized(partition->ctrl->dn),
|
---|
649 | ldb_errstring(ldb));
|
---|
650 | return ret;
|
---|
651 | }
|
---|
652 | break;
|
---|
653 | }
|
---|
654 | default:
|
---|
655 | {
|
---|
656 | ldb_asprintf_errstring(ldb,
|
---|
657 | "Failed to add %s from " DSDB_PARTITION_DN
|
---|
658 | " replicateEntries to new partition at %s on %s: %s",
|
---|
659 | ldb_dn_get_linearized(data->replicate[i]),
|
---|
660 | partition->backend_url,
|
---|
661 | ldb_dn_get_linearized(partition->ctrl->dn),
|
---|
662 | ldb_errstring(ldb));
|
---|
663 | return ret;
|
---|
664 | }
|
---|
665 | }
|
---|
666 |
|
---|
667 | /* And around again, for the next thing we must merge */
|
---|
668 | }
|
---|
669 | return LDB_SUCCESS;
|
---|
670 | }
|
---|
671 |
|
---|
672 | /* Extended operation to create a new partition, called when
|
---|
673 | * 'new_partition' detects that one is being added based on it's
|
---|
674 | * instanceType */
|
---|
675 | int partition_create(struct ldb_module *module, struct ldb_request *req)
|
---|
676 | {
|
---|
677 | unsigned int i;
|
---|
678 | int ret;
|
---|
679 | struct ldb_context *ldb = ldb_module_get_ctx(module);
|
---|
680 | struct ldb_request *mod_req, *last_req = req;
|
---|
681 | struct ldb_message *mod_msg;
|
---|
682 | struct partition_private_data *data;
|
---|
683 | struct dsdb_partition *partition = NULL;
|
---|
684 | const char *casefold_dn;
|
---|
685 | bool new_partition = false;
|
---|
686 |
|
---|
687 | /* Check if this is already a partition */
|
---|
688 |
|
---|
689 | struct dsdb_create_partition_exop *ex_op = talloc_get_type(req->op.extended.data, struct dsdb_create_partition_exop);
|
---|
690 | struct ldb_dn *dn = ex_op->new_dn;
|
---|
691 |
|
---|
692 | data = talloc_get_type(ldb_module_get_private(module), struct partition_private_data);
|
---|
693 | if (!data) {
|
---|
694 | /* We are not going to create a partition before we are even set up */
|
---|
695 | return LDB_ERR_UNWILLING_TO_PERFORM;
|
---|
696 | }
|
---|
697 |
|
---|
698 | for (i=0; data->partitions && data->partitions[i]; i++) {
|
---|
699 | if (ldb_dn_compare(data->partitions[i]->ctrl->dn, dn) == 0) {
|
---|
700 | partition = data->partitions[i];
|
---|
701 | }
|
---|
702 | }
|
---|
703 |
|
---|
704 | if (!partition) {
|
---|
705 | char *filename;
|
---|
706 | char *partition_record;
|
---|
707 | new_partition = true;
|
---|
708 | mod_msg = ldb_msg_new(req);
|
---|
709 | if (!mod_msg) {
|
---|
710 | return ldb_oom(ldb);
|
---|
711 | }
|
---|
712 |
|
---|
713 | mod_msg->dn = ldb_dn_new(mod_msg, ldb, DSDB_PARTITION_DN);
|
---|
714 | ret = ldb_msg_add_empty(mod_msg, DSDB_PARTITION_ATTR, LDB_FLAG_MOD_ADD, NULL);
|
---|
715 | if (ret != LDB_SUCCESS) {
|
---|
716 | return ret;
|
---|
717 | }
|
---|
718 |
|
---|
719 | casefold_dn = ldb_dn_get_casefold(dn);
|
---|
720 |
|
---|
721 | {
|
---|
722 | char *escaped;
|
---|
723 | const char *p, *sam_name;
|
---|
724 | sam_name = strrchr((const char *)ldb_get_opaque(ldb, "ldb_url"), '/');
|
---|
725 | if (!sam_name) {
|
---|
726 | return ldb_operr(ldb);
|
---|
727 | }
|
---|
728 | sam_name++;
|
---|
729 |
|
---|
730 | for (p = casefold_dn; *p; p++) {
|
---|
731 | /* We have such a strict check because
|
---|
732 | * I don't want shell metacharacters
|
---|
733 | * in the file name, nor ../, but I do
|
---|
734 | * want it to be easily typed if SAFE
|
---|
735 | * to do so */
|
---|
736 | if (!(isalnum(*p) || *p == ' ' || *p == '=' || *p == ',')) {
|
---|
737 | break;
|
---|
738 | }
|
---|
739 | }
|
---|
740 | if (*p) {
|
---|
741 | escaped = rfc1738_escape_part(mod_msg, casefold_dn);
|
---|
742 | if (!escaped) {
|
---|
743 | return ldb_oom(ldb);
|
---|
744 | }
|
---|
745 | filename = talloc_asprintf(mod_msg, "%s.d/%s.ldb", sam_name, escaped);
|
---|
746 | talloc_free(escaped);
|
---|
747 | } else {
|
---|
748 | filename = talloc_asprintf(mod_msg, "%s.d/%s.ldb", sam_name, casefold_dn);
|
---|
749 | }
|
---|
750 |
|
---|
751 | if (!filename) {
|
---|
752 | return ldb_oom(ldb);
|
---|
753 | }
|
---|
754 | }
|
---|
755 | partition_record = talloc_asprintf(mod_msg, "%s:%s", casefold_dn, filename);
|
---|
756 |
|
---|
757 | ret = ldb_msg_add_steal_string(mod_msg, DSDB_PARTITION_ATTR, partition_record);
|
---|
758 | if (ret != LDB_SUCCESS) {
|
---|
759 | return ret;
|
---|
760 | }
|
---|
761 |
|
---|
762 | /* Perform modify on @PARTITION record */
|
---|
763 | ret = ldb_build_mod_req(&mod_req, ldb, req, mod_msg, NULL, NULL,
|
---|
764 | ldb_op_default_callback, req);
|
---|
765 | LDB_REQ_SET_LOCATION(mod_req);
|
---|
766 | if (ret != LDB_SUCCESS) {
|
---|
767 | return ret;
|
---|
768 | }
|
---|
769 |
|
---|
770 | last_req = mod_req;
|
---|
771 |
|
---|
772 | ret = ldb_next_request(module, mod_req);
|
---|
773 | if (ret == LDB_SUCCESS) {
|
---|
774 | ret = ldb_wait(mod_req->handle, LDB_WAIT_ALL);
|
---|
775 | }
|
---|
776 |
|
---|
777 | if (ret != LDB_SUCCESS) {
|
---|
778 | return ret;
|
---|
779 | }
|
---|
780 |
|
---|
781 | /* Make a partition structure for this new partition, so we can copy in the template structure */
|
---|
782 | ret = new_partition_from_dn(ldb, data, req, ldb_dn_copy(req, dn), filename, &partition);
|
---|
783 | if (ret != LDB_SUCCESS) {
|
---|
784 | return ret;
|
---|
785 | }
|
---|
786 | talloc_steal(partition, partition_record);
|
---|
787 | partition->orig_record = data_blob_string_const(partition_record);
|
---|
788 | }
|
---|
789 |
|
---|
790 | ret = new_partition_set_replicated_metadata(ldb, module, last_req, data, partition);
|
---|
791 | if (ret != LDB_SUCCESS) {
|
---|
792 | return ret;
|
---|
793 | }
|
---|
794 |
|
---|
795 | if (new_partition) {
|
---|
796 | ret = add_partition_to_data(ldb, data, partition);
|
---|
797 | if (ret != LDB_SUCCESS) {
|
---|
798 | return ret;
|
---|
799 | }
|
---|
800 | }
|
---|
801 |
|
---|
802 | /* send request done */
|
---|
803 | return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
|
---|
804 | }
|
---|
805 |
|
---|
806 |
|
---|
807 | int partition_init(struct ldb_module *module)
|
---|
808 | {
|
---|
809 | int ret;
|
---|
810 | TALLOC_CTX *mem_ctx = talloc_new(module);
|
---|
811 | struct ldb_context *ldb = ldb_module_get_ctx(module);
|
---|
812 | struct partition_private_data *data;
|
---|
813 |
|
---|
814 | if (!mem_ctx) {
|
---|
815 | return ldb_operr(ldb);
|
---|
816 | }
|
---|
817 |
|
---|
818 | data = talloc_zero(mem_ctx, struct partition_private_data);
|
---|
819 | if (data == NULL) {
|
---|
820 | return ldb_operr(ldb);
|
---|
821 | }
|
---|
822 |
|
---|
823 | /* When used from Samba4, this message is set by the samba4
|
---|
824 | * module, as a fixed value not read from the DB. This avoids
|
---|
825 | * listing modules in the DB */
|
---|
826 | data->forced_module_msg = talloc_get_type(
|
---|
827 | ldb_get_opaque(ldb,
|
---|
828 | DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME),
|
---|
829 | struct ldb_message);
|
---|
830 |
|
---|
831 | /* This loads the partitions */
|
---|
832 | ret = partition_reload_if_required(module, data, NULL);
|
---|
833 | if (ret != LDB_SUCCESS) {
|
---|
834 | return ret;
|
---|
835 | }
|
---|
836 |
|
---|
837 | ldb_module_set_private(module, talloc_steal(module, data));
|
---|
838 | talloc_free(mem_ctx);
|
---|
839 |
|
---|
840 | ret = ldb_mod_register_control(module, LDB_CONTROL_DOMAIN_SCOPE_OID);
|
---|
841 | if (ret != LDB_SUCCESS) {
|
---|
842 | ldb_debug(ldb, LDB_DEBUG_ERROR,
|
---|
843 | "partition: Unable to register control with rootdse!\n");
|
---|
844 | return ldb_operr(ldb);
|
---|
845 | }
|
---|
846 |
|
---|
847 | ret = ldb_mod_register_control(module, LDB_CONTROL_SEARCH_OPTIONS_OID);
|
---|
848 | if (ret != LDB_SUCCESS) {
|
---|
849 | ldb_debug(ldb, LDB_DEBUG_ERROR,
|
---|
850 | "partition: Unable to register control with rootdse!\n");
|
---|
851 | return ldb_operr(ldb);
|
---|
852 | }
|
---|
853 |
|
---|
854 | return ldb_next_init(module);
|
---|
855 | }
|
---|