Line data Source code
1 : /*
2 : ldb database library
3 :
4 : Copyright (C) Andrew Tridgell 2004
5 : Copyright (C) Simo Sorce 2005-2008
6 :
7 : ** NOTE! The following LGPL license applies to the ldb
8 : ** library. This does NOT imply that all of Samba is released
9 : ** under the LGPL
10 :
11 : This library is free software; you can redistribute it and/or
12 : modify it under the terms of the GNU Lesser General Public
13 : License as published by the Free Software Foundation; either
14 : version 3 of the License, or (at your option) any later version.
15 :
16 : This library is distributed in the hope that it will be useful,
17 : but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 : Lesser General Public License for more details.
20 :
21 : You should have received a copy of the GNU Lesser General Public
22 : License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 : */
24 :
25 : /*
26 : * Name: ldb
27 : *
28 : * Component: ldb core API
29 : *
30 : * Description: core API routines interfacing to ldb backends
31 : *
32 : * Author: Andrew Tridgell
33 : */
34 :
35 : #define TEVENT_DEPRECATED 1
36 : #include "ldb_private.h"
37 : #include "ldb.h"
38 :
39 537517 : static int ldb_context_destructor(void *ptr)
40 : {
41 537517 : struct ldb_context *ldb = talloc_get_type(ptr, struct ldb_context);
42 :
43 537517 : if (ldb->transaction_active) {
44 19 : ldb_debug(ldb, LDB_DEBUG_FATAL,
45 : "A transaction is still active in ldb context [%p] on %s",
46 19 : ldb, (const char *)ldb_get_opaque(ldb, "ldb_url"));
47 : }
48 :
49 537517 : return 0;
50 : }
51 :
52 : /*
53 : this is used to catch debug messages from events
54 : */
55 : static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
56 : const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
57 :
58 688464356 : static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
59 : const char *fmt, va_list ap)
60 : {
61 688464356 : struct ldb_context *ldb = talloc_get_type(context, struct ldb_context);
62 688464356 : enum ldb_debug_level ldb_level = LDB_DEBUG_FATAL;
63 :
64 688464356 : switch (level) {
65 0 : case TEVENT_DEBUG_FATAL:
66 0 : ldb_level = LDB_DEBUG_FATAL;
67 0 : break;
68 0 : case TEVENT_DEBUG_ERROR:
69 0 : ldb_level = LDB_DEBUG_ERROR;
70 0 : break;
71 0 : case TEVENT_DEBUG_WARNING:
72 0 : ldb_level = LDB_DEBUG_WARNING;
73 0 : break;
74 688464356 : case TEVENT_DEBUG_TRACE:
75 688464356 : ldb_level = LDB_DEBUG_TRACE;
76 688464356 : break;
77 : };
78 :
79 : /* There isn't a tevent: prefix here because to add it means
80 : * actually printing the string, and most of the time we don't
81 : * want to show it */
82 688464356 : ldb_vdebug(ldb, ldb_level, fmt, ap);
83 688464356 : }
84 :
85 : /*
86 : initialise a ldb context
87 : The mem_ctx is required
88 : The event_ctx is required
89 : */
90 553031 : struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
91 : {
92 : struct ldb_context *ldb;
93 : int ret;
94 553031 : const char *modules_path = getenv("LDB_MODULES_PATH");
95 :
96 553031 : if (modules_path == NULL) {
97 550448 : modules_path = LDB_MODULESDIR;
98 : }
99 :
100 553031 : ret = ldb_modules_load(modules_path, LDB_VERSION);
101 553031 : if (ret != LDB_SUCCESS) {
102 0 : return NULL;
103 : }
104 :
105 553031 : ldb = talloc_zero(mem_ctx, struct ldb_context);
106 553031 : if (ldb == NULL) {
107 0 : return NULL;
108 : }
109 :
110 : /* A new event context so that callers who don't want ldb
111 : * operating on their global event context can work without
112 : * having to provide their own private one explicitly */
113 553031 : if (ev_ctx == NULL) {
114 234266 : ev_ctx = tevent_context_init(ldb);
115 234266 : if (ev_ctx == NULL) {
116 0 : talloc_free(ldb);
117 0 : return NULL;
118 : }
119 234266 : tevent_set_debug(ev_ctx, ldb_tevent_debug, ldb);
120 234266 : tevent_loop_allow_nesting(ev_ctx);
121 : }
122 :
123 553031 : ret = ldb_setup_wellknown_attributes(ldb);
124 553031 : if (ret != LDB_SUCCESS) {
125 0 : talloc_free(ldb);
126 0 : return NULL;
127 : }
128 :
129 553031 : ldb_set_utf8_default(ldb);
130 553031 : ldb_set_create_perms(ldb, 0666);
131 553031 : ldb_set_modules_dir(ldb, LDB_MODULESDIR);
132 553031 : ldb_set_event_context(ldb, ev_ctx);
133 553031 : ret = ldb_register_extended_match_rules(ldb);
134 553031 : if (ret != LDB_SUCCESS) {
135 0 : talloc_free(ldb);
136 0 : return NULL;
137 : }
138 :
139 : /* TODO: get timeout from options if available there */
140 553031 : ldb->default_timeout = 300; /* set default to 5 minutes */
141 :
142 553031 : talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
143 :
144 553031 : return ldb;
145 : }
146 :
147 : /*
148 : try to autodetect a basedn if none specified. This fixes one of my
149 : pet hates about ldapsearch, which is that you have to get a long,
150 : complex basedn right to make any use of it.
151 : */
152 856868 : void ldb_set_default_dns(struct ldb_context *ldb)
153 : {
154 : TALLOC_CTX *tmp_ctx;
155 : int ret;
156 : struct ldb_result *res;
157 856868 : struct ldb_dn *tmp_dn=NULL;
158 : static const char *attrs[] = {
159 : "rootDomainNamingContext",
160 : "configurationNamingContext",
161 : "schemaNamingContext",
162 : "defaultNamingContext",
163 : NULL
164 : };
165 :
166 856868 : tmp_ctx = talloc_new(ldb);
167 856868 : ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
168 : LDB_SCOPE_BASE, attrs, "(objectClass=*)");
169 856868 : if (ret != LDB_SUCCESS) {
170 226502 : talloc_free(tmp_ctx);
171 226502 : return;
172 : }
173 :
174 630366 : if (res->count != 1) {
175 0 : talloc_free(tmp_ctx);
176 0 : return;
177 : }
178 :
179 630366 : if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
180 324769 : tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
181 : "rootDomainNamingContext");
182 324769 : ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
183 : }
184 :
185 630366 : if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
186 324769 : tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
187 : "configurationNamingContext");
188 324769 : ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
189 : }
190 :
191 630366 : if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
192 324769 : tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
193 : "schemaNamingContext");
194 324769 : ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
195 : }
196 :
197 630366 : if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
198 324769 : tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
199 : "defaultNamingContext");
200 324769 : ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
201 : }
202 :
203 630366 : talloc_free(tmp_ctx);
204 : }
205 :
206 1165417 : struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
207 : {
208 1165417 : void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
209 1165417 : return talloc_get_type(opaque, struct ldb_dn);
210 : }
211 :
212 3092341 : struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
213 : {
214 3092341 : void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
215 3092341 : return talloc_get_type(opaque, struct ldb_dn);
216 : }
217 :
218 2329164 : struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
219 : {
220 2329164 : void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
221 2329164 : return talloc_get_type(opaque, struct ldb_dn);
222 : }
223 :
224 4797547 : struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
225 : {
226 4797547 : void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
227 4797547 : return talloc_get_type(opaque, struct ldb_dn);
228 : }
229 :
230 : /*
231 : connect to a database. The URL can either be one of the following forms
232 : ldb://path
233 : ldapi://path
234 :
235 : flags is made up of LDB_FLG_*
236 :
237 : the options are passed uninterpreted to the backend, and are
238 : backend specific
239 : */
240 552342 : int ldb_connect(struct ldb_context *ldb, const char *url,
241 : unsigned int flags, const char *options[])
242 : {
243 : int ret;
244 : char *url2;
245 : /* We seem to need to do this here, or else some utilities don't
246 : * get ldb backends */
247 :
248 552342 : ldb->flags = flags;
249 :
250 552342 : url2 = talloc_strdup(ldb, url);
251 552342 : if (!url2) {
252 0 : ldb_oom(ldb);
253 0 : return LDB_ERR_OPERATIONS_ERROR;
254 : }
255 552342 : ret = ldb_set_opaque(ldb, "ldb_url", url2);
256 552342 : if (ret != LDB_SUCCESS) {
257 0 : return ret;
258 : }
259 :
260 : /*
261 : * Take a copy of the options.
262 : */
263 552342 : ldb->options = ldb_options_copy(ldb, options);
264 552342 : if (ldb->options == NULL && options != NULL) {
265 0 : ldb_oom(ldb);
266 0 : return LDB_ERR_OPERATIONS_ERROR;
267 : }
268 :
269 552342 : ret = ldb_module_connect_backend(ldb, url, options, &ldb->modules);
270 552342 : if (ret != LDB_SUCCESS) {
271 1065 : return ret;
272 : }
273 :
274 551277 : ret = ldb_load_modules(ldb, options);
275 551277 : if (ret != LDB_SUCCESS) {
276 6 : ldb_debug(ldb, LDB_DEBUG_FATAL,
277 : "Unable to load modules for %s: %s",
278 : url, ldb_errstring(ldb));
279 6 : return ret;
280 : }
281 :
282 : /* set the default base dn */
283 551271 : ldb_set_default_dns(ldb);
284 :
285 551271 : return LDB_SUCCESS;
286 : }
287 :
288 719144 : void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
289 : {
290 719144 : ldb_asprintf_errstring(ldb, "%s", err_string);
291 719144 : }
292 :
293 2935947 : void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
294 : {
295 : va_list ap;
296 2935947 : char *old_err_string = NULL;
297 2935947 : if (ldb->err_string) {
298 525992 : old_err_string = ldb->err_string;
299 : }
300 :
301 2935947 : va_start(ap, format);
302 2935947 : ldb->err_string = talloc_vasprintf(ldb, format, ap);
303 2935947 : va_end(ap);
304 :
305 2935947 : TALLOC_FREE(old_err_string);
306 :
307 2935947 : if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
308 0 : ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_asprintf/set_errstring: %s",
309 : ldb->err_string);
310 : }
311 2935947 : }
312 :
313 39516438 : void ldb_reset_err_string(struct ldb_context *ldb)
314 : {
315 39516438 : TALLOC_FREE(ldb->err_string);
316 39516438 : }
317 :
318 :
319 :
320 : /*
321 : set an ldb error based on file:line
322 : */
323 200159 : int ldb_error_at(struct ldb_context *ldb, int ecode,
324 : const char *reason, const char *file, int line)
325 : {
326 200159 : if (reason == NULL) {
327 0 : reason = ldb_strerror(ecode);
328 : }
329 200159 : ldb_asprintf_errstring(ldb, "%s at %s:%d", reason, file, line);
330 200159 : return ecode;
331 : }
332 :
333 :
334 : #define FIRST_OP_NOERR(ldb, op) do { \
335 : next_module = ldb->modules; \
336 : while (next_module && next_module->ops->op == NULL) { \
337 : next_module = next_module->next; \
338 : }; \
339 : if ((ldb->flags & LDB_FLG_ENABLE_TRACING) && next_module) { \
340 : ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: (%s)->" #op, \
341 : next_module->ops->name); \
342 : } \
343 : } while (0)
344 :
345 : #define FIRST_OP(ldb, op) do { \
346 : FIRST_OP_NOERR(ldb, op); \
347 : if (next_module == NULL) { \
348 : ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
349 : return LDB_ERR_OPERATIONS_ERROR; \
350 : } \
351 : } while (0)
352 :
353 :
354 : /*
355 : start a transaction
356 : */
357 1255786 : int ldb_transaction_start(struct ldb_context *ldb)
358 : {
359 : struct ldb_module *next_module;
360 : int status;
361 :
362 1255786 : ldb_debug(ldb, LDB_DEBUG_TRACE,
363 : "start ldb transaction (nesting: %d)",
364 : ldb->transaction_active);
365 :
366 : /* explicit transaction active, count nested requests */
367 1255786 : if (ldb->transaction_active) {
368 506037 : ldb->transaction_active++;
369 506037 : return LDB_SUCCESS;
370 : }
371 :
372 : /* start a new transaction */
373 749749 : ldb->transaction_active++;
374 749749 : ldb->prepare_commit_done = false;
375 :
376 915949 : FIRST_OP(ldb, start_transaction);
377 :
378 749749 : ldb_reset_err_string(ldb);
379 :
380 749749 : status = next_module->ops->start_transaction(next_module);
381 749749 : if (status != LDB_SUCCESS) {
382 22 : if (ldb->err_string == NULL) {
383 : /* no error string was setup by the backend */
384 20 : ldb_asprintf_errstring(ldb,
385 : "ldb transaction start: %s (%d)",
386 : ldb_strerror(status),
387 : status);
388 20 : ldb->transaction_active--;
389 : }
390 22 : if ((next_module && next_module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
391 0 : ldb_debug(next_module->ldb, LDB_DEBUG_TRACE, "start ldb transaction error: %s",
392 : ldb_errstring(next_module->ldb));
393 : }
394 : } else {
395 749727 : if ((next_module && next_module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
396 0 : ldb_debug(next_module->ldb, LDB_DEBUG_TRACE, "start ldb transaction success");
397 : }
398 : }
399 749749 : return status;
400 : }
401 :
402 : /*
403 : prepare for transaction commit (first phase of two phase commit)
404 : */
405 1175400 : int ldb_transaction_prepare_commit(struct ldb_context *ldb)
406 : {
407 : struct ldb_module *next_module;
408 : int status;
409 :
410 1175400 : if (ldb->prepare_commit_done) {
411 2438 : return LDB_SUCCESS;
412 : }
413 :
414 : /* commit only when all nested transactions are complete */
415 1172962 : if (ldb->transaction_active > 1) {
416 506671 : return LDB_SUCCESS;
417 : }
418 :
419 666291 : ldb->prepare_commit_done = true;
420 :
421 666291 : if (ldb->transaction_active < 0) {
422 0 : ldb_debug(ldb, LDB_DEBUG_FATAL,
423 : "prepare commit called but no ldb transactions are active!");
424 0 : ldb->transaction_active = 0;
425 0 : return LDB_ERR_OPERATIONS_ERROR;
426 : }
427 :
428 : /* call prepare transaction if available */
429 1563298 : FIRST_OP_NOERR(ldb, prepare_commit);
430 666291 : if (next_module == NULL) {
431 108941 : return LDB_SUCCESS;
432 : }
433 :
434 557350 : ldb_reset_err_string(ldb);
435 :
436 557350 : status = next_module->ops->prepare_commit(next_module);
437 557350 : if (status != LDB_SUCCESS) {
438 8 : ldb->transaction_active--;
439 : /* if a next_module fails the prepare then we need
440 : to call the end transaction for everyone */
441 8 : FIRST_OP(ldb, del_transaction);
442 8 : next_module->ops->del_transaction(next_module);
443 8 : if (ldb->err_string == NULL) {
444 : /* no error string was setup by the backend */
445 0 : ldb_asprintf_errstring(ldb,
446 : "ldb transaction prepare commit: %s (%d)",
447 : ldb_strerror(status),
448 : status);
449 : }
450 8 : if ((next_module && next_module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
451 0 : ldb_debug(next_module->ldb, LDB_DEBUG_TRACE, "prepare commit transaction error: %s",
452 : ldb_errstring(next_module->ldb));
453 : }
454 : }
455 :
456 557350 : return status;
457 : }
458 :
459 :
460 : /*
461 : commit a transaction
462 : */
463 1171807 : int ldb_transaction_commit(struct ldb_context *ldb)
464 : {
465 : struct ldb_module *next_module;
466 : int status;
467 :
468 1171807 : status = ldb_transaction_prepare_commit(ldb);
469 1171807 : if (status != LDB_SUCCESS) {
470 8 : return status;
471 : }
472 :
473 1171799 : ldb->transaction_active--;
474 :
475 1171799 : ldb_debug(ldb, LDB_DEBUG_TRACE,
476 : "commit ldb transaction (nesting: %d)",
477 : ldb->transaction_active);
478 :
479 : /* commit only when all nested transactions are complete */
480 1171799 : if (ldb->transaction_active > 0) {
481 505520 : return LDB_SUCCESS;
482 : }
483 :
484 666279 : if (ldb->transaction_active < 0) {
485 0 : ldb_debug(ldb, LDB_DEBUG_FATAL,
486 : "commit called but no ldb transactions are active!");
487 0 : ldb->transaction_active = 0;
488 0 : return LDB_ERR_OPERATIONS_ERROR;
489 : }
490 :
491 666279 : ldb_reset_err_string(ldb);
492 :
493 805005 : FIRST_OP(ldb, end_transaction);
494 666279 : status = next_module->ops->end_transaction(next_module);
495 666279 : if (status != LDB_SUCCESS) {
496 2 : if (ldb->err_string == NULL) {
497 : /* no error string was setup by the backend */
498 0 : ldb_asprintf_errstring(ldb,
499 : "ldb transaction commit: %s (%d)",
500 : ldb_strerror(status),
501 : status);
502 : }
503 2 : if ((next_module && next_module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
504 0 : ldb_debug(next_module->ldb, LDB_DEBUG_TRACE, "commit ldb transaction error: %s",
505 : ldb_errstring(next_module->ldb));
506 : }
507 : }
508 666279 : return status;
509 : }
510 :
511 :
512 : /*
513 : cancel a transaction
514 : */
515 83938 : int ldb_transaction_cancel(struct ldb_context *ldb)
516 : {
517 : struct ldb_module *next_module;
518 : int status;
519 :
520 83938 : ldb->transaction_active--;
521 :
522 83938 : ldb_debug(ldb, LDB_DEBUG_TRACE,
523 : "cancel ldb transaction (nesting: %d)",
524 : ldb->transaction_active);
525 :
526 : /* really cancel only if all nested transactions are complete */
527 83938 : if (ldb->transaction_active > 0) {
528 517 : return LDB_SUCCESS;
529 : }
530 :
531 83421 : if (ldb->transaction_active < 0) {
532 0 : ldb_debug(ldb, LDB_DEBUG_FATAL,
533 : "cancel called but no ldb transactions are active!");
534 0 : ldb->transaction_active = 0;
535 0 : return LDB_ERR_OPERATIONS_ERROR;
536 : }
537 :
538 110895 : FIRST_OP(ldb, del_transaction);
539 :
540 83421 : status = next_module->ops->del_transaction(next_module);
541 83421 : if (status != LDB_SUCCESS) {
542 0 : if (ldb->err_string == NULL) {
543 : /* no error string was setup by the backend */
544 0 : ldb_asprintf_errstring(ldb,
545 : "ldb transaction cancel: %s (%d)",
546 : ldb_strerror(status),
547 : status);
548 : }
549 0 : if ((next_module && next_module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
550 0 : ldb_debug(next_module->ldb, LDB_DEBUG_TRACE, "cancel ldb transaction error: %s",
551 : ldb_errstring(next_module->ldb));
552 : }
553 : }
554 83421 : return status;
555 : }
556 :
557 : /*
558 : cancel a transaction with no error if no transaction is pending
559 : used when we fork() to clear any parent transactions
560 : */
561 0 : int ldb_transaction_cancel_noerr(struct ldb_context *ldb)
562 : {
563 0 : if (ldb->transaction_active > 0) {
564 0 : return ldb_transaction_cancel(ldb);
565 : }
566 0 : return LDB_SUCCESS;
567 : }
568 :
569 :
570 : /* autostarts a transaction if none active */
571 257864 : static int ldb_autotransaction_request(struct ldb_context *ldb,
572 : struct ldb_request *req)
573 : {
574 : int ret;
575 :
576 257864 : ret = ldb_transaction_start(ldb);
577 257864 : if (ret != LDB_SUCCESS) {
578 20 : return ret;
579 : }
580 :
581 257844 : ret = ldb_request(ldb, req);
582 257844 : if (ret == LDB_SUCCESS) {
583 257727 : ret = ldb_wait(req->handle, LDB_WAIT_ALL);
584 : }
585 :
586 257844 : if (ret == LDB_SUCCESS) {
587 240465 : return ldb_transaction_commit(ldb);
588 : }
589 17379 : ldb_transaction_cancel(ldb);
590 :
591 17379 : return ret;
592 : }
593 :
594 78509491 : int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
595 : {
596 : struct tevent_context *ev;
597 : int ret;
598 :
599 78509491 : if (handle == NULL) {
600 0 : return LDB_ERR_UNAVAILABLE;
601 : }
602 :
603 78509491 : if (handle->state == LDB_ASYNC_DONE) {
604 10478663 : if ((handle->status != LDB_SUCCESS) &&
605 1 : (handle->ldb->err_string == NULL)) {
606 : /* if no error string was setup by the backend */
607 0 : ldb_asprintf_errstring(handle->ldb,
608 : "ldb_wait from %s with LDB_ASYNC_DONE: %s (%d)",
609 : handle->location,
610 : ldb_strerror(handle->status),
611 : handle->status);
612 : }
613 10478662 : return handle->status;
614 : }
615 :
616 68030829 : ev = ldb_handle_get_event_context(handle);
617 68030829 : if (NULL == ev) {
618 0 : return ldb_oom(handle->ldb);
619 : }
620 :
621 68030829 : switch (type) {
622 3451189 : case LDB_WAIT_NONE:
623 3451189 : ret = tevent_loop_once(ev);
624 3451189 : if (ret != 0) {
625 0 : return ldb_operr(handle->ldb);
626 : }
627 3451189 : if (handle->status == LDB_SUCCESS) {
628 3449664 : return LDB_SUCCESS;
629 : }
630 1525 : if (handle->ldb->err_string != NULL) {
631 1516 : return handle->status;
632 : }
633 : /*
634 : * if no error string was setup by the backend
635 : */
636 9 : ldb_asprintf_errstring(handle->ldb,
637 : "ldb_wait from %s with LDB_WAIT_NONE: %s (%d)",
638 : handle->location,
639 : ldb_strerror(handle->status),
640 : handle->status);
641 9 : return handle->status;
642 :
643 64579640 : case LDB_WAIT_ALL:
644 260427883 : while (handle->state != LDB_ASYNC_DONE) {
645 140711161 : ret = tevent_loop_once(ev);
646 140711149 : if (ret != 0) {
647 0 : return ldb_operr(handle->ldb);
648 : }
649 140711149 : if (handle->status != LDB_SUCCESS) {
650 1857680 : if (handle->ldb->err_string != NULL) {
651 1844764 : return handle->status;
652 : }
653 : /*
654 : * if no error string was setup by the
655 : * backend
656 : */
657 12916 : ldb_asprintf_errstring(handle->ldb,
658 : "ldb_wait from %s with "
659 : "LDB_WAIT_ALL: %s (%d)",
660 : handle->location,
661 : ldb_strerror(handle->status),
662 : handle->status);
663 12916 : return handle->status;
664 : }
665 : }
666 62721948 : if (handle->status == LDB_SUCCESS) {
667 62721948 : return LDB_SUCCESS;
668 : }
669 0 : if (handle->ldb->err_string != NULL) {
670 0 : return handle->status;
671 : }
672 : /*
673 : * if no error string was setup by the backend
674 : */
675 0 : ldb_asprintf_errstring(handle->ldb,
676 : "ldb_wait from %s with LDB_WAIT_ALL,"
677 : " LDB_ASYNC_DONE: %s (%d)",
678 : handle->location,
679 : ldb_strerror(handle->status),
680 : handle->status);
681 0 : return handle->status;
682 : }
683 :
684 0 : return LDB_SUCCESS;
685 : }
686 :
687 : /* set the specified timeout or, if timeout is 0 set the default timeout */
688 55459749 : int ldb_set_timeout(struct ldb_context *ldb,
689 : struct ldb_request *req,
690 : int timeout)
691 : {
692 55459749 : if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
693 :
694 55459749 : if (timeout != 0) {
695 288234 : req->timeout = timeout;
696 : } else {
697 55171515 : req->timeout = ldb->default_timeout;
698 : }
699 55459749 : req->starttime = time(NULL);
700 :
701 55459749 : return LDB_SUCCESS;
702 : }
703 :
704 : /* calculates the new timeout based on the previous starttime and timeout */
705 393718805 : int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
706 : struct ldb_request *oldreq,
707 : struct ldb_request *newreq)
708 : {
709 393718805 : if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
710 :
711 393718805 : if (oldreq == NULL) {
712 45442420 : return ldb_set_timeout(ldb, newreq, 0);
713 : }
714 :
715 348276385 : newreq->starttime = oldreq->starttime;
716 348276385 : newreq->timeout = oldreq->timeout;
717 :
718 348276385 : return LDB_SUCCESS;
719 : }
720 :
721 :
722 54106425 : struct ldb_handle *ldb_handle_new(TALLOC_CTX *mem_ctx, struct ldb_context *ldb)
723 : {
724 : struct ldb_handle *h;
725 :
726 54106425 : h = talloc_zero(mem_ctx, struct ldb_handle);
727 54106425 : if (h == NULL) {
728 0 : ldb_set_errstring(ldb, "Out of Memory");
729 0 : return NULL;
730 : }
731 :
732 54106425 : h->status = LDB_SUCCESS;
733 54106425 : h->state = LDB_ASYNC_INIT;
734 54106425 : h->ldb = ldb;
735 54106425 : h->flags = 0;
736 54106425 : h->location = NULL;
737 54106425 : h->parent = NULL;
738 :
739 54106425 : if (h->ldb->require_private_event_context == true) {
740 53678314 : h->event_context = tevent_context_init(h);
741 53678314 : if (h->event_context == NULL) {
742 0 : ldb_set_errstring(ldb,
743 : "Out of Memory allocating "
744 : "event context for new handle");
745 0 : return NULL;
746 : }
747 53678314 : tevent_set_debug(h->event_context, ldb_tevent_debug, ldb);
748 53678314 : tevent_loop_allow_nesting(h->event_context);
749 : }
750 :
751 54106425 : return h;
752 : }
753 :
754 348276385 : static struct ldb_handle *ldb_handle_new_child(TALLOC_CTX *mem_ctx,
755 : struct ldb_request *parent_req)
756 : {
757 : struct ldb_handle *h;
758 :
759 348276385 : h = talloc_zero(mem_ctx, struct ldb_handle);
760 348276385 : if (h == NULL) {
761 0 : ldb_set_errstring(parent_req->handle->ldb,
762 : "Out of Memory");
763 0 : return NULL;
764 : }
765 :
766 348276385 : h->status = LDB_SUCCESS;
767 348276385 : h->state = LDB_ASYNC_INIT;
768 348276385 : h->ldb = parent_req->handle->ldb;
769 348276385 : h->parent = parent_req;
770 348276385 : h->nesting = parent_req->handle->nesting + 1;
771 348276385 : h->flags = parent_req->handle->flags;
772 348276385 : h->custom_flags = parent_req->handle->custom_flags;
773 348276385 : h->event_context = parent_req->handle->event_context;
774 :
775 348276385 : return h;
776 : }
777 :
778 : /*
779 : set the permissions for new files to be passed to open() in
780 : backends that use local files
781 : */
782 1100302 : void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
783 : {
784 1100302 : ldb->create_perms = perms;
785 1100302 : }
786 :
787 1840236 : unsigned int ldb_get_create_perms(struct ldb_context *ldb)
788 : {
789 1840236 : return ldb->create_perms;
790 : }
791 :
792 1365836 : void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
793 : {
794 1365836 : ldb->ev_ctx = ev;
795 1365836 : }
796 :
797 174437392 : struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
798 : {
799 174437392 : return ldb->ev_ctx;
800 : }
801 :
802 136773810 : void ldb_request_set_state(struct ldb_request *req, int state)
803 : {
804 136773810 : req->handle->state = state;
805 136773810 : }
806 :
807 136261042 : int ldb_request_get_status(struct ldb_request *req)
808 : {
809 136261042 : return req->handle->status;
810 : }
811 :
812 : /*
813 : * This function obtains the private event context for the handle,
814 : * which may have been created to avoid nested event loops during
815 : * ldb_tdb with the locks held
816 : */
817 204377727 : struct tevent_context *ldb_handle_get_event_context(struct ldb_handle *handle)
818 : {
819 204377727 : if (handle->event_context != NULL) {
820 200501097 : return handle->event_context;
821 : }
822 3876630 : return ldb_get_event_context(handle->ldb);
823 : }
824 :
825 : /*
826 : * This function forces a specific ldb handle to use the global event
827 : * context. This allows a nested event loop to operate, so any open
828 : * transaction also needs to be aborted.
829 : *
830 : * Any events on this event context will be lost
831 : *
832 : * This is used in Samba when sending an IRPC to another part of the
833 : * same process instead of making a local DB modification.
834 : */
835 46 : void ldb_handle_use_global_event_context(struct ldb_handle *handle)
836 : {
837 46 : TALLOC_FREE(handle->event_context);
838 46 : }
839 :
840 2004540 : void ldb_set_require_private_event_context(struct ldb_context *ldb)
841 : {
842 2004540 : ldb->require_private_event_context = true;
843 2004540 : }
844 :
845 : /*
846 : trace a ldb request
847 : */
848 0 : static void ldb_trace_request(struct ldb_context *ldb, struct ldb_request *req)
849 : {
850 0 : TALLOC_CTX *tmp_ctx = talloc_new(req);
851 : unsigned int i;
852 : struct ldb_ldif ldif;
853 :
854 0 : switch (req->operation) {
855 0 : case LDB_SEARCH:
856 0 : ldb_debug_add(ldb, "ldb_trace_request: SEARCH\n");
857 0 : ldb_debug_add(ldb, " dn: %s\n",
858 0 : ldb_dn_is_null(req->op.search.base)?"<rootDSE>":
859 0 : ldb_dn_get_linearized(req->op.search.base));
860 0 : ldb_debug_add(ldb, " scope: %s\n",
861 0 : req->op.search.scope==LDB_SCOPE_BASE?"base":
862 0 : req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
863 0 : req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN");
864 0 : ldb_debug_add(ldb, " expr: %s\n",
865 0 : ldb_filter_from_tree(tmp_ctx, req->op.search.tree));
866 0 : if (req->op.search.attrs == NULL) {
867 0 : ldb_debug_add(ldb, " attr: <ALL>\n");
868 : } else {
869 0 : for (i=0; req->op.search.attrs[i]; i++) {
870 0 : ldb_debug_add(ldb, " attr: %s\n", req->op.search.attrs[i]);
871 : }
872 : }
873 0 : break;
874 0 : case LDB_DELETE:
875 0 : ldb_debug_add(ldb, "ldb_trace_request: DELETE\n");
876 0 : ldb_debug_add(ldb, " dn: %s\n",
877 : ldb_dn_get_linearized(req->op.del.dn));
878 0 : break;
879 0 : case LDB_RENAME:
880 0 : ldb_debug_add(ldb, "ldb_trace_request: RENAME\n");
881 0 : ldb_debug_add(ldb, " olddn: %s\n",
882 : ldb_dn_get_linearized(req->op.rename.olddn));
883 0 : ldb_debug_add(ldb, " newdn: %s\n",
884 : ldb_dn_get_linearized(req->op.rename.newdn));
885 0 : break;
886 0 : case LDB_EXTENDED:
887 0 : ldb_debug_add(ldb, "ldb_trace_request: EXTENDED\n");
888 0 : ldb_debug_add(ldb, " oid: %s\n", req->op.extended.oid);
889 0 : ldb_debug_add(ldb, " data: %s\n", req->op.extended.data?"yes":"no");
890 0 : break;
891 0 : case LDB_ADD:
892 0 : ldif.changetype = LDB_CHANGETYPE_ADD;
893 0 : ldif.msg = discard_const_p(struct ldb_message, req->op.add.message);
894 :
895 0 : ldb_debug_add(ldb, "ldb_trace_request: ADD\n");
896 :
897 : /*
898 : * The choice to call
899 : * ldb_ldif_write_redacted_trace_string() is CRITICAL
900 : * for security. It ensures that we do not output
901 : * passwords into debug logs
902 : */
903 :
904 0 : ldb_debug_add(req->handle->ldb, "%s\n",
905 0 : ldb_ldif_write_redacted_trace_string(req->handle->ldb, tmp_ctx, &ldif));
906 0 : break;
907 0 : case LDB_MODIFY:
908 0 : ldif.changetype = LDB_CHANGETYPE_MODIFY;
909 0 : ldif.msg = discard_const_p(struct ldb_message, req->op.mod.message);
910 :
911 0 : ldb_debug_add(ldb, "ldb_trace_request: MODIFY\n");
912 :
913 : /*
914 : * The choice to call
915 : * ldb_ldif_write_redacted_trace_string() is CRITICAL
916 : * for security. It ensures that we do not output
917 : * passwords into debug logs
918 : */
919 :
920 0 : ldb_debug_add(req->handle->ldb, "%s\n",
921 0 : ldb_ldif_write_redacted_trace_string(req->handle->ldb, tmp_ctx, &ldif));
922 0 : break;
923 0 : case LDB_REQ_REGISTER_CONTROL:
924 0 : ldb_debug_add(ldb, "ldb_trace_request: REGISTER_CONTROL\n");
925 0 : ldb_debug_add(req->handle->ldb, "%s\n",
926 : req->op.reg_control.oid);
927 0 : break;
928 0 : case LDB_REQ_REGISTER_PARTITION:
929 0 : ldb_debug_add(ldb, "ldb_trace_request: REGISTER_PARTITION\n");
930 0 : ldb_debug_add(req->handle->ldb, "%s\n",
931 : ldb_dn_get_linearized(req->op.reg_partition.dn));
932 0 : break;
933 0 : default:
934 0 : ldb_debug_add(ldb, "ldb_trace_request: UNKNOWN(%u)\n",
935 0 : req->operation);
936 0 : break;
937 : }
938 :
939 0 : if (req->controls == NULL) {
940 0 : ldb_debug_add(ldb, " control: <NONE>\n");
941 : } else {
942 0 : for (i=0; req->controls && req->controls[i]; i++) {
943 0 : if (req->controls[i]->oid) {
944 0 : ldb_debug_add(ldb, " control: %s crit:%u data:%s\n",
945 0 : req->controls[i]->oid,
946 0 : req->controls[i]->critical,
947 0 : req->controls[i]->data?"yes":"no");
948 : }
949 : }
950 : }
951 :
952 0 : ldb_debug_end(ldb, LDB_DEBUG_TRACE);
953 :
954 0 : talloc_free(tmp_ctx);
955 0 : }
956 :
957 : /*
958 : check that the element flags don't have any internal bits set
959 : */
960 996437 : static int ldb_msg_check_element_flags(struct ldb_context *ldb,
961 : const struct ldb_message *message)
962 : {
963 : unsigned i;
964 5401449 : for (i=0; i<message->num_elements; i++) {
965 4405012 : if (message->elements[i].flags & LDB_FLAG_INTERNAL_MASK) {
966 0 : ldb_asprintf_errstring(ldb, "Invalid element flags 0x%08x on element %s in %s\n",
967 0 : message->elements[i].flags, message->elements[i].name,
968 0 : ldb_dn_get_linearized(message->dn));
969 0 : return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
970 : }
971 : }
972 996437 : return LDB_SUCCESS;
973 : }
974 :
975 : /*
976 : * This context allows us to make the unlock be a talloc destructor
977 : *
978 : * This ensures that a request started, but not waited on, will still
979 : * unlock.
980 : */
981 : struct ldb_db_lock_context {
982 : struct ldb_request *req;
983 : struct ldb_context *ldb;
984 : };
985 :
986 : /*
987 : * We have to have a the unlock on a destructor so that we unlock the
988 : * DB if a caller calls talloc_free(req). We trust that the ldb
989 : * context has not already gone away.
990 : */
991 18229600 : static int ldb_db_lock_destructor(struct ldb_db_lock_context *lock_context)
992 : {
993 : int ret;
994 : struct ldb_module *next_module;
995 24332636 : FIRST_OP_NOERR(lock_context->ldb, read_unlock);
996 18229600 : if (next_module != NULL) {
997 18229600 : ret = next_module->ops->read_unlock(next_module);
998 : } else {
999 0 : ret = LDB_SUCCESS;
1000 : }
1001 :
1002 18229600 : if (ret != LDB_SUCCESS) {
1003 2 : ldb_debug(lock_context->ldb,
1004 : LDB_DEBUG_FATAL,
1005 : "Failed to unlock db: %s / %s",
1006 : ldb_errstring(lock_context->ldb),
1007 : ldb_strerror(ret));
1008 : }
1009 18229600 : return 0;
1010 : }
1011 :
1012 39057036 : static int ldb_lock_backend_callback(struct ldb_request *req,
1013 : struct ldb_reply *ares)
1014 : {
1015 : struct ldb_db_lock_context *lock_context;
1016 : int ret;
1017 :
1018 39057036 : if (req->context == NULL) {
1019 : /*
1020 : * The usual way to get here is to ignore the return codes
1021 : * and continuing processing after an error.
1022 : */
1023 0 : abort();
1024 : }
1025 39057036 : lock_context = talloc_get_type(req->context,
1026 : struct ldb_db_lock_context);
1027 :
1028 39057036 : if (!ares) {
1029 0 : return ldb_module_done(lock_context->req, NULL, NULL,
1030 : LDB_ERR_OPERATIONS_ERROR);
1031 : }
1032 39057036 : if (ares->error != LDB_SUCCESS || ares->type == LDB_REPLY_DONE) {
1033 18229572 : ret = ldb_module_done(lock_context->req, ares->controls,
1034 : ares->response, ares->error);
1035 : /*
1036 : * If this is a LDB_REPLY_DONE or an error, unlock the
1037 : * DB by calling the destructor on this context
1038 : */
1039 18229570 : TALLOC_FREE(req->context);
1040 18229570 : return ret;
1041 : }
1042 :
1043 : /* Otherwise pass on the callback */
1044 20827464 : switch (ares->type) {
1045 18579607 : case LDB_REPLY_ENTRY:
1046 18579607 : return ldb_module_send_entry(lock_context->req, ares->message,
1047 : ares->controls);
1048 :
1049 2247857 : case LDB_REPLY_REFERRAL:
1050 2247857 : return ldb_module_send_referral(lock_context->req,
1051 : ares->referral);
1052 0 : default:
1053 : /* Can't happen */
1054 0 : return LDB_ERR_OPERATIONS_ERROR;
1055 : }
1056 : }
1057 :
1058 : /*
1059 : * Do an ldb_search() with a lock held, but release it if the request
1060 : * is freed with talloc_free()
1061 : */
1062 18496731 : static int lock_search(struct ldb_module *lock_module, struct ldb_request *req)
1063 : {
1064 : /* Used in FIRST_OP_NOERR to find where to send the lock request */
1065 18496731 : struct ldb_module *next_module = NULL;
1066 18496731 : struct ldb_request *down_req = NULL;
1067 : struct ldb_db_lock_context *lock_context;
1068 18496731 : struct ldb_context *ldb = ldb_module_get_ctx(lock_module);
1069 : int ret;
1070 :
1071 18496731 : lock_context = talloc(req, struct ldb_db_lock_context);
1072 18496731 : if (lock_context == NULL) {
1073 0 : return ldb_oom(ldb);
1074 : }
1075 :
1076 18496731 : lock_context->ldb = ldb;
1077 18496731 : lock_context->req = req;
1078 :
1079 18496731 : ret = ldb_build_search_req_ex(&down_req, ldb, req,
1080 : req->op.search.base,
1081 : req->op.search.scope,
1082 : req->op.search.tree,
1083 : req->op.search.attrs,
1084 : req->controls,
1085 : lock_context,
1086 : ldb_lock_backend_callback,
1087 : req);
1088 18496731 : LDB_REQ_SET_LOCATION(down_req);
1089 18496731 : if (ret != LDB_SUCCESS) {
1090 0 : return ret;
1091 : }
1092 :
1093 : /* call DB lock */
1094 24661035 : FIRST_OP_NOERR(ldb, read_lock);
1095 18496731 : if (next_module != NULL) {
1096 18229614 : ret = next_module->ops->read_lock(next_module);
1097 : } else {
1098 267117 : ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1099 : }
1100 :
1101 18496731 : if (ret == LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION) {
1102 : /* We might be talking LDAP */
1103 267117 : ldb_reset_err_string(ldb);
1104 267117 : TALLOC_FREE(lock_context);
1105 :
1106 267117 : return ldb_next_request(lock_module, req);
1107 18229614 : } else if ((ret != LDB_SUCCESS) && (ldb->err_string == NULL)) {
1108 : /* if no error string was setup by the backend */
1109 0 : ldb_asprintf_errstring(ldb, "Failed to get DB lock: %s (%d)",
1110 : ldb_strerror(ret), ret);
1111 : } else {
1112 18229614 : talloc_set_destructor(lock_context, ldb_db_lock_destructor);
1113 : }
1114 :
1115 18229614 : if (ret != LDB_SUCCESS) {
1116 2 : return ret;
1117 : }
1118 :
1119 18229612 : return ldb_next_request(lock_module, down_req);
1120 : }
1121 :
1122 : /*
1123 : start an ldb request
1124 : NOTE: the request must be a talloc context.
1125 : returns LDB_ERR_* on errors.
1126 : */
1127 29739763 : int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
1128 : {
1129 : struct ldb_module *next_module;
1130 : int ret;
1131 :
1132 29739763 : if (req->callback == NULL) {
1133 0 : ldb_set_errstring(ldb, "Requests MUST define callbacks");
1134 0 : return LDB_ERR_UNWILLING_TO_PERFORM;
1135 : }
1136 :
1137 29739763 : ldb_reset_err_string(ldb);
1138 :
1139 29739763 : if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
1140 0 : ldb_trace_request(ldb, req);
1141 : }
1142 :
1143 : /* call the first module in the chain */
1144 29739763 : switch (req->operation) {
1145 18496806 : case LDB_SEARCH:
1146 : {
1147 : /*
1148 : * A fake module to allow ldb_next_request() to be
1149 : * re-used and to keep the locking out of this function.
1150 : */
1151 : static const struct ldb_module_ops lock_module_ops = {
1152 : .name = "lock_searches",
1153 : .search = lock_search
1154 : };
1155 35120471 : struct ldb_module lock_module = {
1156 : .ldb = ldb,
1157 18496806 : .next = ldb->modules,
1158 : .ops = &lock_module_ops
1159 : };
1160 18496806 : next_module = &lock_module;
1161 :
1162 : /* due to "ldb_build_search_req" base DN always != NULL */
1163 18496806 : if (!ldb_dn_validate(req->op.search.base)) {
1164 75 : ldb_asprintf_errstring(ldb, "ldb_search: invalid basedn '%s'",
1165 : ldb_dn_get_linearized(req->op.search.base));
1166 75 : return LDB_ERR_INVALID_DN_SYNTAX;
1167 : }
1168 :
1169 18496731 : ret = next_module->ops->search(next_module, req);
1170 18496731 : break;
1171 : }
1172 552840 : case LDB_ADD:
1173 552840 : if (!ldb_dn_validate(req->op.add.message->dn)) {
1174 15 : ldb_asprintf_errstring(ldb, "ldb_add: invalid dn '%s'",
1175 15 : ldb_dn_get_linearized(req->op.add.message->dn));
1176 15 : return LDB_ERR_INVALID_DN_SYNTAX;
1177 : }
1178 : /*
1179 : * we have to normalize here, as so many places
1180 : * in modules and backends assume we don't have two
1181 : * elements with the same name
1182 : */
1183 552825 : ret = ldb_msg_normalize(ldb, req, req->op.add.message,
1184 552825 : discard_const(&req->op.add.message));
1185 552825 : if (ret != LDB_SUCCESS) {
1186 0 : ldb_oom(ldb);
1187 0 : return ret;
1188 : }
1189 623544 : FIRST_OP(ldb, add);
1190 552825 : ret = ldb_msg_check_element_flags(ldb, req->op.add.message);
1191 552825 : if (ret != LDB_SUCCESS) {
1192 : /*
1193 : * "ldb_msg_check_element_flags" generates an error
1194 : * string
1195 : */
1196 0 : return ret;
1197 : }
1198 552825 : ret = next_module->ops->add(next_module, req);
1199 552825 : break;
1200 443612 : case LDB_MODIFY:
1201 443612 : if (!ldb_dn_validate(req->op.mod.message->dn)) {
1202 0 : ldb_asprintf_errstring(ldb, "ldb_modify: invalid dn '%s'",
1203 0 : ldb_dn_get_linearized(req->op.mod.message->dn));
1204 0 : return LDB_ERR_INVALID_DN_SYNTAX;
1205 : }
1206 483781 : FIRST_OP(ldb, modify);
1207 443612 : ret = ldb_msg_check_element_flags(ldb, req->op.mod.message);
1208 443612 : if (ret != LDB_SUCCESS) {
1209 : /*
1210 : * "ldb_msg_check_element_flags" generates an error
1211 : * string
1212 : */
1213 0 : return ret;
1214 : }
1215 443612 : ret = next_module->ops->modify(next_module, req);
1216 443612 : break;
1217 185512 : case LDB_DELETE:
1218 185512 : if (!ldb_dn_validate(req->op.del.dn)) {
1219 0 : ldb_asprintf_errstring(ldb, "ldb_delete: invalid dn '%s'",
1220 : ldb_dn_get_linearized(req->op.del.dn));
1221 0 : return LDB_ERR_INVALID_DN_SYNTAX;
1222 : }
1223 240480 : FIRST_OP(ldb, del);
1224 185512 : ret = next_module->ops->del(next_module, req);
1225 185512 : break;
1226 1278 : case LDB_RENAME:
1227 1278 : if (!ldb_dn_validate(req->op.rename.olddn)) {
1228 16 : ldb_asprintf_errstring(ldb, "ldb_rename: invalid olddn '%s'",
1229 : ldb_dn_get_linearized(req->op.rename.olddn));
1230 16 : return LDB_ERR_INVALID_DN_SYNTAX;
1231 : }
1232 1262 : if (!ldb_dn_validate(req->op.rename.newdn)) {
1233 19 : ldb_asprintf_errstring(ldb, "ldb_rename: invalid newdn '%s'",
1234 : ldb_dn_get_linearized(req->op.rename.newdn));
1235 19 : return LDB_ERR_INVALID_DN_SYNTAX;
1236 : }
1237 1403 : FIRST_OP(ldb, rename);
1238 1243 : ret = next_module->ops->rename(next_module, req);
1239 1243 : break;
1240 1395710 : case LDB_EXTENDED:
1241 1682975 : FIRST_OP(ldb, extended);
1242 1395395 : ret = next_module->ops->extended(next_module, req);
1243 1395395 : break;
1244 8664005 : default:
1245 11738694 : FIRST_OP(ldb, request);
1246 8437620 : ret = next_module->ops->request(next_module, req);
1247 8437620 : break;
1248 : }
1249 :
1250 29512938 : if ((ret != LDB_SUCCESS) && (ldb->err_string == NULL)) {
1251 : /* if no error string was setup by the backend */
1252 4 : ldb_asprintf_errstring(ldb, "ldb_request: %s (%d)",
1253 : ldb_strerror(ret), ret);
1254 : }
1255 :
1256 29512938 : return ret;
1257 : }
1258 :
1259 75068494 : int ldb_request_done(struct ldb_request *req, int status)
1260 : {
1261 75068494 : req->handle->state = LDB_ASYNC_DONE;
1262 75068494 : req->handle->status = status;
1263 75068494 : return status;
1264 : }
1265 :
1266 : /*
1267 : search the database given a LDAP-like search expression
1268 :
1269 : returns an LDB error code
1270 :
1271 : Use talloc_free to free the ldb_message returned in 'res', if successful
1272 :
1273 : */
1274 147575706 : int ldb_search_default_callback(struct ldb_request *req,
1275 : struct ldb_reply *ares)
1276 : {
1277 : struct ldb_result *res;
1278 : unsigned int n;
1279 :
1280 147575706 : res = talloc_get_type(req->context, struct ldb_result);
1281 :
1282 147575706 : if (!ares) {
1283 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1284 : }
1285 147575706 : if (ares->error != LDB_SUCCESS) {
1286 1076343 : return ldb_request_done(req, ares->error);
1287 : }
1288 :
1289 146499363 : switch (ares->type) {
1290 103154486 : case LDB_REPLY_ENTRY:
1291 103154486 : res->msgs = talloc_realloc(res, res->msgs,
1292 : struct ldb_message *, res->count + 2);
1293 103154486 : if (! res->msgs) {
1294 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1295 : }
1296 :
1297 103154486 : res->msgs[res->count + 1] = NULL;
1298 :
1299 103154486 : res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
1300 103154486 : res->count++;
1301 103154486 : break;
1302 :
1303 2857146 : case LDB_REPLY_REFERRAL:
1304 2857146 : if (res->refs) {
1305 1961333 : for (n = 0; res->refs[n]; n++) /*noop*/ ;
1306 : } else {
1307 1150966 : n = 0;
1308 : }
1309 :
1310 2857146 : res->refs = talloc_realloc(res, res->refs, char *, n + 2);
1311 2857146 : if (! res->refs) {
1312 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1313 : }
1314 :
1315 2857146 : res->refs[n] = talloc_move(res->refs, &ares->referral);
1316 2857146 : res->refs[n + 1] = NULL;
1317 2857146 : break;
1318 :
1319 40487731 : case LDB_REPLY_DONE:
1320 : /* TODO: we should really support controls on entries
1321 : * and referrals too! */
1322 40487731 : res->controls = talloc_move(res, &ares->controls);
1323 :
1324 : /* this is the last message, and means the request is done */
1325 : /* we have to signal and eventual ldb_wait() waiting that the
1326 : * async request operation was completed */
1327 40487731 : talloc_free(ares);
1328 40487731 : return ldb_request_done(req, LDB_SUCCESS);
1329 : }
1330 :
1331 106011632 : talloc_free(ares);
1332 :
1333 106011632 : return LDB_SUCCESS;
1334 : }
1335 :
1336 1671688 : int ldb_modify_default_callback(struct ldb_request *req, struct ldb_reply *ares)
1337 : {
1338 : struct ldb_result *res;
1339 : unsigned int n;
1340 : int ret;
1341 :
1342 1671688 : res = talloc_get_type(req->context, struct ldb_result);
1343 :
1344 1671688 : if (!ares) {
1345 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1346 : }
1347 :
1348 1671688 : if (ares->error != LDB_SUCCESS) {
1349 32658 : ret = ares->error;
1350 32658 : talloc_free(ares);
1351 32658 : return ldb_request_done(req, ret);
1352 : }
1353 :
1354 1639030 : switch (ares->type) {
1355 9 : case LDB_REPLY_REFERRAL:
1356 9 : if (res->refs) {
1357 0 : for (n = 0; res->refs[n]; n++) /*noop*/ ;
1358 : } else {
1359 9 : n = 0;
1360 : }
1361 :
1362 9 : res->refs = talloc_realloc(res, res->refs, char *, n + 2);
1363 9 : if (! res->refs) {
1364 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1365 : }
1366 :
1367 9 : res->refs[n] = talloc_move(res->refs, &ares->referral);
1368 9 : res->refs[n + 1] = NULL;
1369 9 : break;
1370 :
1371 1639021 : case LDB_REPLY_DONE:
1372 1639021 : talloc_free(ares);
1373 1639021 : return ldb_request_done(req, LDB_SUCCESS);
1374 0 : default:
1375 0 : talloc_free(ares);
1376 0 : ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
1377 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1378 : }
1379 :
1380 9 : talloc_free(ares);
1381 9 : return ldb_request_done(req, LDB_SUCCESS);
1382 : }
1383 :
1384 9425824 : int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
1385 : {
1386 : int ret;
1387 :
1388 9425824 : if (!ares) {
1389 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1390 : }
1391 :
1392 9425824 : if (ares->error != LDB_SUCCESS) {
1393 51965 : ret = ares->error;
1394 51965 : talloc_free(ares);
1395 51965 : return ldb_request_done(req, ret);
1396 : }
1397 :
1398 9373859 : if (ares->type != LDB_REPLY_DONE) {
1399 364 : ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
1400 364 : TALLOC_FREE(ares);
1401 364 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1402 : }
1403 :
1404 9373495 : talloc_free(ares);
1405 9373495 : return ldb_request_done(req, LDB_SUCCESS);
1406 : }
1407 :
1408 393718805 : static struct ldb_request *ldb_build_req_common(TALLOC_CTX *mem_ctx,
1409 : struct ldb_context *ldb,
1410 : struct ldb_control **controls,
1411 : void *context,
1412 : ldb_request_callback_t callback,
1413 : struct ldb_request *parent)
1414 : {
1415 393718805 : struct ldb_request *req = NULL;
1416 :
1417 393718805 : req = talloc_zero(mem_ctx, struct ldb_request);
1418 393718805 : if (req == NULL) {
1419 0 : return NULL;
1420 : }
1421 393718805 : req->controls = controls;
1422 393718805 : req->context = context;
1423 393718805 : req->callback = callback;
1424 :
1425 393718805 : ldb_set_timeout_from_prev_req(ldb, parent, req);
1426 :
1427 393718805 : if (parent != NULL) {
1428 348276385 : req->handle = ldb_handle_new_child(req, parent);
1429 348276385 : if (req->handle == NULL) {
1430 0 : TALLOC_FREE(req);
1431 0 : return NULL;
1432 : }
1433 : } else {
1434 45442420 : req->handle = ldb_handle_new(req, ldb);
1435 45442420 : if (req->handle == NULL) {
1436 0 : TALLOC_FREE(req);
1437 0 : return NULL;
1438 : }
1439 : }
1440 :
1441 393718805 : return req;
1442 : }
1443 :
1444 365018391 : int ldb_build_search_req_ex(struct ldb_request **ret_req,
1445 : struct ldb_context *ldb,
1446 : TALLOC_CTX *mem_ctx,
1447 : struct ldb_dn *base,
1448 : enum ldb_scope scope,
1449 : struct ldb_parse_tree *tree,
1450 : const char * const *attrs,
1451 : struct ldb_control **controls,
1452 : void *context,
1453 : ldb_request_callback_t callback,
1454 : struct ldb_request *parent)
1455 : {
1456 : struct ldb_request *req;
1457 :
1458 365018391 : *ret_req = NULL;
1459 :
1460 365018391 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1461 : context, callback, parent);
1462 365018391 : if (req == NULL) {
1463 0 : ldb_oom(ldb);
1464 0 : return LDB_ERR_OPERATIONS_ERROR;
1465 : }
1466 :
1467 365018391 : req->operation = LDB_SEARCH;
1468 365018391 : if (base == NULL) {
1469 17439039 : req->op.search.base = ldb_dn_new(req, ldb, NULL);
1470 : } else {
1471 347579352 : req->op.search.base = base;
1472 : }
1473 365018391 : req->op.search.scope = scope;
1474 :
1475 365018391 : req->op.search.tree = tree;
1476 365018391 : if (req->op.search.tree == NULL) {
1477 0 : ldb_set_errstring(ldb, "'tree' can't be NULL");
1478 0 : talloc_free(req);
1479 0 : return LDB_ERR_OPERATIONS_ERROR;
1480 : }
1481 :
1482 365018391 : req->op.search.attrs = attrs;
1483 365018391 : *ret_req = req;
1484 365018391 : return LDB_SUCCESS;
1485 : }
1486 :
1487 47378002 : int ldb_build_search_req(struct ldb_request **ret_req,
1488 : struct ldb_context *ldb,
1489 : TALLOC_CTX *mem_ctx,
1490 : struct ldb_dn *base,
1491 : enum ldb_scope scope,
1492 : const char *expression,
1493 : const char * const *attrs,
1494 : struct ldb_control **controls,
1495 : void *context,
1496 : ldb_request_callback_t callback,
1497 : struct ldb_request *parent)
1498 : {
1499 : struct ldb_parse_tree *tree;
1500 : int ret;
1501 :
1502 47378002 : tree = ldb_parse_tree(mem_ctx, expression);
1503 47378002 : if (tree == NULL) {
1504 8 : ldb_set_errstring(ldb, "Unable to parse search expression");
1505 8 : return LDB_ERR_OPERATIONS_ERROR;
1506 : }
1507 :
1508 47377994 : ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
1509 : scope, tree, attrs, controls,
1510 : context, callback, parent);
1511 47377994 : if (ret == LDB_SUCCESS) {
1512 47377994 : talloc_steal(*ret_req, tree);
1513 : }
1514 47377994 : return ret;
1515 : }
1516 :
1517 4356815 : int ldb_build_add_req(struct ldb_request **ret_req,
1518 : struct ldb_context *ldb,
1519 : TALLOC_CTX *mem_ctx,
1520 : const struct ldb_message *message,
1521 : struct ldb_control **controls,
1522 : void *context,
1523 : ldb_request_callback_t callback,
1524 : struct ldb_request *parent)
1525 : {
1526 : struct ldb_request *req;
1527 :
1528 4356815 : *ret_req = NULL;
1529 :
1530 4356815 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1531 : context, callback, parent);
1532 4356815 : if (req == NULL) {
1533 0 : ldb_set_errstring(ldb, "Out of Memory");
1534 0 : return LDB_ERR_OPERATIONS_ERROR;
1535 : }
1536 :
1537 4356815 : req->operation = LDB_ADD;
1538 4356815 : req->op.add.message = message;
1539 4356815 : *ret_req = req;
1540 4356815 : return LDB_SUCCESS;
1541 : }
1542 :
1543 5100288 : int ldb_build_mod_req(struct ldb_request **ret_req,
1544 : struct ldb_context *ldb,
1545 : TALLOC_CTX *mem_ctx,
1546 : const struct ldb_message *message,
1547 : struct ldb_control **controls,
1548 : void *context,
1549 : ldb_request_callback_t callback,
1550 : struct ldb_request *parent)
1551 : {
1552 : struct ldb_request *req;
1553 :
1554 5100288 : *ret_req = NULL;
1555 :
1556 5100288 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1557 : context, callback, parent);
1558 5100288 : if (req == NULL) {
1559 0 : ldb_set_errstring(ldb, "Out of Memory");
1560 0 : return LDB_ERR_OPERATIONS_ERROR;
1561 : }
1562 :
1563 5100288 : req->operation = LDB_MODIFY;
1564 5100288 : req->op.mod.message = message;
1565 :
1566 5100288 : *ret_req = req;
1567 5100288 : return LDB_SUCCESS;
1568 : }
1569 :
1570 389882 : int ldb_build_del_req(struct ldb_request **ret_req,
1571 : struct ldb_context *ldb,
1572 : TALLOC_CTX *mem_ctx,
1573 : struct ldb_dn *dn,
1574 : struct ldb_control **controls,
1575 : void *context,
1576 : ldb_request_callback_t callback,
1577 : struct ldb_request *parent)
1578 : {
1579 : struct ldb_request *req;
1580 :
1581 389882 : *ret_req = NULL;
1582 :
1583 389882 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1584 : context, callback, parent);
1585 389882 : if (req == NULL) {
1586 0 : ldb_set_errstring(ldb, "Out of Memory");
1587 0 : return LDB_ERR_OPERATIONS_ERROR;
1588 : }
1589 :
1590 389882 : req->operation = LDB_DELETE;
1591 389882 : req->op.del.dn = dn;
1592 389882 : *ret_req = req;
1593 389882 : return LDB_SUCCESS;
1594 : }
1595 :
1596 261435 : int ldb_build_rename_req(struct ldb_request **ret_req,
1597 : struct ldb_context *ldb,
1598 : TALLOC_CTX *mem_ctx,
1599 : struct ldb_dn *olddn,
1600 : struct ldb_dn *newdn,
1601 : struct ldb_control **controls,
1602 : void *context,
1603 : ldb_request_callback_t callback,
1604 : struct ldb_request *parent)
1605 : {
1606 : struct ldb_request *req;
1607 :
1608 261435 : *ret_req = NULL;
1609 :
1610 261435 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1611 : context, callback, parent);
1612 261435 : if (req == NULL) {
1613 0 : ldb_set_errstring(ldb, "Out of Memory");
1614 0 : return LDB_ERR_OPERATIONS_ERROR;
1615 : }
1616 :
1617 261435 : req->operation = LDB_RENAME;
1618 261435 : req->op.rename.olddn = olddn;
1619 261435 : req->op.rename.newdn = newdn;
1620 261435 : *ret_req = req;
1621 261435 : return LDB_SUCCESS;
1622 : }
1623 :
1624 18587434 : int ldb_extended_default_callback(struct ldb_request *req,
1625 : struct ldb_reply *ares)
1626 : {
1627 : struct ldb_result *res;
1628 :
1629 18587434 : res = talloc_get_type(req->context, struct ldb_result);
1630 :
1631 18587434 : if (!ares) {
1632 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1633 : }
1634 18587434 : if (ares->error != LDB_SUCCESS) {
1635 67 : return ldb_request_done(req, ares->error);
1636 : }
1637 :
1638 18587367 : if (ares->type == LDB_REPLY_DONE) {
1639 :
1640 : /* TODO: we should really support controls on entries and referrals too! */
1641 18587367 : res->extended = talloc_move(res, &ares->response);
1642 18587367 : res->controls = talloc_move(res, &ares->controls);
1643 :
1644 18587367 : talloc_free(ares);
1645 18587367 : return ldb_request_done(req, LDB_SUCCESS);
1646 : }
1647 :
1648 0 : talloc_free(ares);
1649 0 : ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
1650 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1651 : }
1652 :
1653 18591994 : int ldb_build_extended_req(struct ldb_request **ret_req,
1654 : struct ldb_context *ldb,
1655 : TALLOC_CTX *mem_ctx,
1656 : const char *oid,
1657 : void *data,
1658 : struct ldb_control **controls,
1659 : void *context,
1660 : ldb_request_callback_t callback,
1661 : struct ldb_request *parent)
1662 : {
1663 : struct ldb_request *req;
1664 :
1665 18591994 : *ret_req = NULL;
1666 :
1667 18591994 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1668 : context, callback, parent);
1669 18591994 : if (req == NULL) {
1670 0 : ldb_set_errstring(ldb, "Out of Memory");
1671 0 : return LDB_ERR_OPERATIONS_ERROR;
1672 : }
1673 :
1674 18591994 : req->operation = LDB_EXTENDED;
1675 18591994 : req->op.extended.oid = oid;
1676 18591994 : req->op.extended.data = data;
1677 18591994 : *ret_req = req;
1678 18591994 : return LDB_SUCCESS;
1679 : }
1680 :
1681 1062273 : int ldb_extended(struct ldb_context *ldb,
1682 : const char *oid,
1683 : void *data,
1684 : struct ldb_result **_res)
1685 : {
1686 : struct ldb_request *req;
1687 : int ret;
1688 : struct ldb_result *res;
1689 :
1690 1062273 : *_res = NULL;
1691 1062273 : req = NULL;
1692 :
1693 1062273 : res = talloc_zero(ldb, struct ldb_result);
1694 1062273 : if (!res) {
1695 0 : return LDB_ERR_OPERATIONS_ERROR;
1696 : }
1697 :
1698 1062273 : ret = ldb_build_extended_req(&req, ldb, ldb,
1699 : oid, data, NULL,
1700 : res, ldb_extended_default_callback,
1701 : NULL);
1702 1062273 : ldb_req_set_location(req, "ldb_extended");
1703 :
1704 1062273 : if (ret != LDB_SUCCESS) goto done;
1705 :
1706 1062273 : ldb_set_timeout(ldb, req, 0); /* use default timeout */
1707 :
1708 1062273 : ret = ldb_request(ldb, req);
1709 :
1710 1062273 : if (ret == LDB_SUCCESS) {
1711 1061952 : ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1712 : }
1713 :
1714 941892 : done:
1715 1062273 : if (ret != LDB_SUCCESS) {
1716 382 : talloc_free(res);
1717 382 : res = NULL;
1718 : }
1719 :
1720 1062273 : talloc_free(req);
1721 :
1722 1062273 : *_res = res;
1723 1062273 : return ret;
1724 : }
1725 :
1726 : /*
1727 : note that ldb_search() will automatically replace a NULL 'base' value
1728 : with the defaultNamingContext from the rootDSE if available.
1729 : */
1730 5289546 : int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1731 : struct ldb_result **result, struct ldb_dn *base,
1732 : enum ldb_scope scope, const char * const *attrs,
1733 : const char *exp_fmt, ...)
1734 : {
1735 : struct ldb_request *req;
1736 : struct ldb_result *res;
1737 : char *expression;
1738 : va_list ap;
1739 : int ret;
1740 :
1741 5289546 : expression = NULL;
1742 5289546 : *result = NULL;
1743 5289546 : req = NULL;
1744 :
1745 5289546 : res = talloc_zero(mem_ctx, struct ldb_result);
1746 5289546 : if (!res) {
1747 0 : return LDB_ERR_OPERATIONS_ERROR;
1748 : }
1749 :
1750 5289546 : if (exp_fmt) {
1751 3434683 : va_start(ap, exp_fmt);
1752 3434683 : expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1753 3434683 : va_end(ap);
1754 :
1755 3434683 : if (!expression) {
1756 0 : talloc_free(res);
1757 0 : return LDB_ERR_OPERATIONS_ERROR;
1758 : }
1759 : }
1760 :
1761 5423201 : ret = ldb_build_search_req(&req, ldb, mem_ctx,
1762 133655 : base?base:ldb_get_default_basedn(ldb),
1763 : scope,
1764 : expression,
1765 : attrs,
1766 : NULL,
1767 : res,
1768 : ldb_search_default_callback,
1769 : NULL);
1770 5289546 : ldb_req_set_location(req, "ldb_search");
1771 :
1772 5289546 : if (ret != LDB_SUCCESS) goto done;
1773 :
1774 5289546 : ret = ldb_request(ldb, req);
1775 :
1776 5289546 : if (ret == LDB_SUCCESS) {
1777 5288724 : ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1778 : }
1779 :
1780 4584242 : done:
1781 5289546 : if (ret != LDB_SUCCESS) {
1782 228059 : talloc_free(res);
1783 228059 : res = NULL;
1784 : }
1785 :
1786 5289546 : talloc_free(expression);
1787 5289546 : talloc_free(req);
1788 :
1789 5289546 : *result = res;
1790 5289546 : return ret;
1791 : }
1792 :
1793 : /*
1794 : add a record to the database. Will fail if a record with the given class
1795 : and key already exists
1796 : */
1797 87973 : int ldb_add(struct ldb_context *ldb,
1798 : const struct ldb_message *message)
1799 : {
1800 : struct ldb_request *req;
1801 : int ret;
1802 :
1803 87973 : ret = ldb_msg_sanity_check(ldb, message);
1804 87973 : if (ret != LDB_SUCCESS) {
1805 0 : return ret;
1806 : }
1807 :
1808 87973 : ret = ldb_build_add_req(&req, ldb, ldb,
1809 : message,
1810 : NULL,
1811 : NULL,
1812 : ldb_op_default_callback,
1813 : NULL);
1814 87973 : ldb_req_set_location(req, "ldb_add");
1815 :
1816 87973 : if (ret != LDB_SUCCESS) return ret;
1817 :
1818 : /* do request and autostart a transaction */
1819 87973 : ret = ldb_autotransaction_request(ldb, req);
1820 :
1821 87973 : talloc_free(req);
1822 87973 : return ret;
1823 : }
1824 :
1825 : /*
1826 : modify the specified attributes of a record
1827 : */
1828 120787 : int ldb_modify(struct ldb_context *ldb,
1829 : const struct ldb_message *message)
1830 : {
1831 : struct ldb_request *req;
1832 : int ret;
1833 :
1834 120787 : ret = ldb_msg_sanity_check(ldb, message);
1835 120787 : if (ret != LDB_SUCCESS) {
1836 0 : return ret;
1837 : }
1838 :
1839 120787 : ret = ldb_build_mod_req(&req, ldb, ldb,
1840 : message,
1841 : NULL,
1842 : NULL,
1843 : ldb_op_default_callback,
1844 : NULL);
1845 120787 : ldb_req_set_location(req, "ldb_modify");
1846 :
1847 120787 : if (ret != LDB_SUCCESS) return ret;
1848 :
1849 : /* do request and autostart a transaction */
1850 120787 : ret = ldb_autotransaction_request(ldb, req);
1851 :
1852 120787 : talloc_free(req);
1853 120787 : return ret;
1854 : }
1855 :
1856 :
1857 : /*
1858 : delete a record from the database
1859 : */
1860 49008 : int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1861 : {
1862 : struct ldb_request *req;
1863 : int ret;
1864 :
1865 49008 : ret = ldb_build_del_req(&req, ldb, ldb,
1866 : dn,
1867 : NULL,
1868 : NULL,
1869 : ldb_op_default_callback,
1870 : NULL);
1871 49008 : ldb_req_set_location(req, "ldb_delete");
1872 :
1873 49008 : if (ret != LDB_SUCCESS) return ret;
1874 :
1875 : /* do request and autostart a transaction */
1876 49008 : ret = ldb_autotransaction_request(ldb, req);
1877 :
1878 49008 : talloc_free(req);
1879 49008 : return ret;
1880 : }
1881 :
1882 : /*
1883 : rename a record in the database
1884 : */
1885 96 : int ldb_rename(struct ldb_context *ldb,
1886 : struct ldb_dn *olddn, struct ldb_dn *newdn)
1887 : {
1888 : struct ldb_request *req;
1889 : int ret;
1890 :
1891 96 : ret = ldb_build_rename_req(&req, ldb, ldb,
1892 : olddn,
1893 : newdn,
1894 : NULL,
1895 : NULL,
1896 : ldb_op_default_callback,
1897 : NULL);
1898 96 : ldb_req_set_location(req, "ldb_rename");
1899 :
1900 96 : if (ret != LDB_SUCCESS) return ret;
1901 :
1902 : /* do request and autostart a transaction */
1903 96 : ret = ldb_autotransaction_request(ldb, req);
1904 :
1905 96 : talloc_free(req);
1906 96 : return ret;
1907 : }
1908 :
1909 :
1910 : /*
1911 : return the global sequence number
1912 : */
1913 1057131 : int ldb_sequence_number(struct ldb_context *ldb,
1914 : enum ldb_sequence_type type, uint64_t *seq_num)
1915 : {
1916 : struct ldb_seqnum_request *seq;
1917 : struct ldb_seqnum_result *seqr;
1918 : struct ldb_result *res;
1919 : TALLOC_CTX *tmp_ctx;
1920 : int ret;
1921 :
1922 1057131 : *seq_num = 0;
1923 :
1924 1057131 : tmp_ctx = talloc_zero(ldb, struct ldb_request);
1925 1057131 : if (tmp_ctx == NULL) {
1926 0 : ldb_set_errstring(ldb, "Out of Memory");
1927 0 : return LDB_ERR_OPERATIONS_ERROR;
1928 : }
1929 1057131 : seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1930 1057131 : if (seq == NULL) {
1931 0 : ldb_set_errstring(ldb, "Out of Memory");
1932 0 : ret = LDB_ERR_OPERATIONS_ERROR;
1933 0 : goto done;
1934 : }
1935 1057131 : seq->type = type;
1936 :
1937 1057131 : ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1938 1057131 : if (ret != LDB_SUCCESS) {
1939 315 : goto done;
1940 : }
1941 1056816 : talloc_steal(tmp_ctx, res);
1942 :
1943 1056816 : if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1944 0 : ldb_set_errstring(ldb, "Invalid OID in reply");
1945 0 : ret = LDB_ERR_OPERATIONS_ERROR;
1946 0 : goto done;
1947 : }
1948 1056816 : seqr = talloc_get_type(res->extended->data,
1949 : struct ldb_seqnum_result);
1950 1056816 : *seq_num = seqr->seq_num;
1951 :
1952 1057131 : done:
1953 1057131 : talloc_free(tmp_ctx);
1954 1057131 : return ret;
1955 : }
1956 :
1957 : /*
1958 : return extended error information
1959 : */
1960 373798 : const char *ldb_errstring(struct ldb_context *ldb)
1961 : {
1962 373798 : if (ldb->err_string) {
1963 244144 : return ldb->err_string;
1964 : }
1965 :
1966 129654 : return NULL;
1967 : }
1968 :
1969 : /*
1970 : return a string explaining what a ldb error constant meancs
1971 : */
1972 559661 : const char *ldb_strerror(int ldb_err)
1973 : {
1974 559661 : switch (ldb_err) {
1975 529850 : case LDB_SUCCESS:
1976 529850 : return "Success";
1977 14 : case LDB_ERR_OPERATIONS_ERROR:
1978 14 : return "Operations error";
1979 5 : case LDB_ERR_PROTOCOL_ERROR:
1980 5 : return "Protocol error";
1981 9 : case LDB_ERR_TIME_LIMIT_EXCEEDED:
1982 9 : return "Time limit exceeded";
1983 0 : case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1984 0 : return "Size limit exceeded";
1985 0 : case LDB_ERR_COMPARE_FALSE:
1986 0 : return "Compare false";
1987 0 : case LDB_ERR_COMPARE_TRUE:
1988 0 : return "Compare true";
1989 0 : case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1990 0 : return "Auth method not supported";
1991 0 : case LDB_ERR_STRONG_AUTH_REQUIRED:
1992 0 : return "Strong auth required";
1993 : /* 9 RESERVED */
1994 8 : case LDB_ERR_REFERRAL:
1995 8 : return "Referral error";
1996 0 : case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1997 0 : return "Admin limit exceeded";
1998 10 : case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1999 10 : return "Unsupported critical extension";
2000 0 : case LDB_ERR_CONFIDENTIALITY_REQUIRED:
2001 0 : return "Confidentiality required";
2002 0 : case LDB_ERR_SASL_BIND_IN_PROGRESS:
2003 0 : return "SASL bind in progress";
2004 131 : case LDB_ERR_NO_SUCH_ATTRIBUTE:
2005 131 : return "No such attribute";
2006 2 : case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
2007 2 : return "Undefined attribute type";
2008 0 : case LDB_ERR_INAPPROPRIATE_MATCHING:
2009 0 : return "Inappropriate matching";
2010 3682 : case LDB_ERR_CONSTRAINT_VIOLATION:
2011 3682 : return "Constraint violation";
2012 125 : case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
2013 125 : return "Attribute or value exists";
2014 47 : case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
2015 47 : return "Invalid attribute syntax";
2016 : /* 22-31 unused */
2017 22920 : case LDB_ERR_NO_SUCH_OBJECT:
2018 22920 : return "No such object";
2019 0 : case LDB_ERR_ALIAS_PROBLEM:
2020 0 : return "Alias problem";
2021 188 : case LDB_ERR_INVALID_DN_SYNTAX:
2022 188 : return "Invalid DN syntax";
2023 : /* 35 RESERVED */
2024 0 : case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
2025 0 : return "Alias dereferencing problem";
2026 : /* 37-47 unused */
2027 0 : case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
2028 0 : return "Inappropriate authentication";
2029 0 : case LDB_ERR_INVALID_CREDENTIALS:
2030 0 : return "Invalid credentials";
2031 1476 : case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
2032 1476 : return "insufficient access rights";
2033 0 : case LDB_ERR_BUSY:
2034 0 : return "Busy";
2035 0 : case LDB_ERR_UNAVAILABLE:
2036 0 : return "Unavailable";
2037 471 : case LDB_ERR_UNWILLING_TO_PERFORM:
2038 471 : return "Unwilling to perform";
2039 0 : case LDB_ERR_LOOP_DETECT:
2040 0 : return "Loop detect";
2041 : /* 55-63 unused */
2042 5 : case LDB_ERR_NAMING_VIOLATION:
2043 5 : return "Naming violation";
2044 422 : case LDB_ERR_OBJECT_CLASS_VIOLATION:
2045 422 : return "Object class violation";
2046 18 : case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
2047 18 : return "Not allowed on non-leaf";
2048 2 : case LDB_ERR_NOT_ALLOWED_ON_RDN:
2049 2 : return "Not allowed on RDN";
2050 173 : case LDB_ERR_ENTRY_ALREADY_EXISTS:
2051 173 : return "Entry already exists";
2052 0 : case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
2053 0 : return "Object class mods prohibited";
2054 : /* 70 RESERVED FOR CLDAP */
2055 4 : case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
2056 4 : return "Affects multiple DSAs";
2057 : /* 72-79 unused */
2058 97 : case LDB_ERR_OTHER:
2059 97 : return "Other";
2060 : }
2061 :
2062 2 : return "Unknown error";
2063 : }
2064 :
2065 : /*
2066 : set backend specific opaque parameters
2067 : */
2068 287542786 : int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
2069 : {
2070 : struct ldb_opaque *o;
2071 :
2072 : /* allow updating an existing value */
2073 2588030042 : for (o=ldb->opaque;o;o=o->next) {
2074 2580079824 : if (strcmp(o->name, name) == 0) {
2075 279592568 : o->value = value;
2076 279592568 : return LDB_SUCCESS;
2077 : }
2078 : }
2079 :
2080 7950218 : o = talloc(ldb, struct ldb_opaque);
2081 7950218 : if (o == NULL) {
2082 0 : ldb_oom(ldb);
2083 0 : return LDB_ERR_OTHER;
2084 : }
2085 7950218 : o->next = ldb->opaque;
2086 7950218 : o->name = name;
2087 7950218 : o->value = value;
2088 7950218 : ldb->opaque = o;
2089 7950218 : return LDB_SUCCESS;
2090 : }
2091 :
2092 : /*
2093 : get a previously set opaque value
2094 : */
2095 764312232 : void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
2096 : {
2097 : struct ldb_opaque *o;
2098 11370003075 : for (o=ldb->opaque;o;o=o->next) {
2099 11226691343 : if (strcmp(o->name, name) == 0) {
2100 621000500 : return o->value;
2101 : }
2102 : }
2103 143311732 : return NULL;
2104 : }
2105 :
2106 0 : int ldb_global_init(void)
2107 : {
2108 : /* Provided for compatibility with some older versions of ldb */
2109 0 : return 0;
2110 : }
2111 :
2112 : /* return the ldb flags */
2113 38539 : unsigned int ldb_get_flags(struct ldb_context *ldb)
2114 : {
2115 38539 : return ldb->flags;
2116 : }
2117 :
2118 : /* set the ldb flags */
2119 37190 : void ldb_set_flags(struct ldb_context *ldb, unsigned flags)
2120 : {
2121 37190 : ldb->flags = flags;
2122 37190 : }
2123 :
2124 :
2125 : /*
2126 : set the location in a ldb request. Used for debugging
2127 : */
2128 377801681 : void ldb_req_set_location(struct ldb_request *req, const char *location)
2129 : {
2130 377801681 : if (req && req->handle) {
2131 377801681 : req->handle->location = location;
2132 : }
2133 377801681 : }
2134 :
2135 : /*
2136 : return the location set with dsdb_req_set_location
2137 : */
2138 0 : const char *ldb_req_location(struct ldb_request *req)
2139 : {
2140 0 : return req->handle->location;
2141 : }
2142 :
2143 : /**
2144 : mark a request as untrusted. This tells the rootdse module to remove
2145 : unregistered controls
2146 : */
2147 452250 : void ldb_req_mark_untrusted(struct ldb_request *req)
2148 : {
2149 452250 : req->handle->flags |= LDB_HANDLE_FLAG_UNTRUSTED;
2150 452250 : }
2151 :
2152 : /**
2153 : mark a request as trusted.
2154 : */
2155 1011843 : void ldb_req_mark_trusted(struct ldb_request *req)
2156 : {
2157 1011843 : req->handle->flags &= ~LDB_HANDLE_FLAG_UNTRUSTED;
2158 1011843 : }
2159 :
2160 : /**
2161 : set custom flags. Those flags are set by applications using ldb,
2162 : they are application dependent and the same bit can have different
2163 : meaning in different application.
2164 : */
2165 0 : void ldb_req_set_custom_flags(struct ldb_request *req, uint32_t flags)
2166 : {
2167 0 : if (req != NULL && req->handle != NULL) {
2168 0 : req->handle->custom_flags = flags;
2169 : }
2170 0 : }
2171 :
2172 :
2173 : /**
2174 : get custom flags. Those flags are set by applications using ldb,
2175 : they are application dependent and the same bit can have different
2176 : meaning in different application.
2177 : */
2178 0 : uint32_t ldb_req_get_custom_flags(struct ldb_request *req)
2179 : {
2180 0 : if (req != NULL && req->handle != NULL) {
2181 0 : return req->handle->custom_flags;
2182 : }
2183 :
2184 : /*
2185 : * 0 is not something any better or worse than
2186 : * anything else as req or the handle is NULL
2187 : */
2188 0 : return 0;
2189 : }
2190 :
2191 :
2192 : /**
2193 : * return true if a request is untrusted
2194 : */
2195 64006075 : bool ldb_req_is_untrusted(struct ldb_request *req)
2196 : {
2197 64006075 : return (req->handle->flags & LDB_HANDLE_FLAG_UNTRUSTED) != 0;
2198 : }
|