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 567924 : static int ldb_context_destructor(void *ptr)
40 : {
41 567924 : struct ldb_context *ldb = talloc_get_type(ptr, struct ldb_context);
42 :
43 567924 : 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 567924 : 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 718317828 : static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
59 : const char *fmt, va_list ap)
60 : {
61 718317828 : struct ldb_context *ldb = talloc_get_type(context, struct ldb_context);
62 718317828 : enum ldb_debug_level ldb_level = LDB_DEBUG_FATAL;
63 :
64 718317828 : 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 718317828 : case TEVENT_DEBUG_TRACE:
75 718317828 : ldb_level = LDB_DEBUG_TRACE;
76 718317828 : 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 718317828 : ldb_vdebug(ldb, ldb_level, fmt, ap);
83 718317828 : }
84 :
85 : /*
86 : initialise a ldb context
87 : The mem_ctx is required
88 : The event_ctx is required
89 : */
90 583431 : struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
91 : {
92 : struct ldb_context *ldb;
93 : int ret;
94 583431 : const char *modules_path = getenv("LDB_MODULES_PATH");
95 :
96 583431 : if (modules_path == NULL) {
97 579515 : modules_path = LDB_MODULESDIR;
98 : }
99 :
100 583431 : ret = ldb_modules_load(modules_path, LDB_VERSION);
101 583431 : if (ret != LDB_SUCCESS) {
102 0 : return NULL;
103 : }
104 :
105 583431 : ldb = talloc_zero(mem_ctx, struct ldb_context);
106 583431 : 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 583431 : if (ev_ctx == NULL) {
114 246076 : ev_ctx = tevent_context_init(ldb);
115 246076 : if (ev_ctx == NULL) {
116 0 : talloc_free(ldb);
117 0 : return NULL;
118 : }
119 246076 : tevent_set_debug(ev_ctx, ldb_tevent_debug, ldb);
120 246076 : tevent_loop_allow_nesting(ev_ctx);
121 : }
122 :
123 583431 : ret = ldb_setup_wellknown_attributes(ldb);
124 583431 : if (ret != LDB_SUCCESS) {
125 0 : talloc_free(ldb);
126 0 : return NULL;
127 : }
128 :
129 583431 : ldb_set_utf8_default(ldb);
130 583431 : ldb_set_create_perms(ldb, 0666);
131 583431 : ldb_set_modules_dir(ldb, LDB_MODULESDIR);
132 583431 : ldb_set_event_context(ldb, ev_ctx);
133 583431 : ret = ldb_register_extended_match_rules(ldb);
134 583431 : 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 583431 : ldb->default_timeout = 300; /* set default to 5 minutes */
141 :
142 583431 : talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
143 :
144 583431 : 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 905722 : void ldb_set_default_dns(struct ldb_context *ldb)
153 : {
154 : TALLOC_CTX *tmp_ctx;
155 : int ret;
156 : struct ldb_result *res;
157 905722 : struct ldb_dn *tmp_dn=NULL;
158 : static const char *attrs[] = {
159 : "rootDomainNamingContext",
160 : "configurationNamingContext",
161 : "schemaNamingContext",
162 : "defaultNamingContext",
163 : NULL
164 : };
165 :
166 905722 : tmp_ctx = talloc_new(ldb);
167 905722 : ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
168 : LDB_SCOPE_BASE, attrs, "(objectClass=*)");
169 905722 : if (ret != LDB_SUCCESS) {
170 235313 : talloc_free(tmp_ctx);
171 235313 : return;
172 : }
173 :
174 670409 : if (res->count != 1) {
175 0 : talloc_free(tmp_ctx);
176 0 : return;
177 : }
178 :
179 670409 : if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
180 346045 : tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
181 : "rootDomainNamingContext");
182 346045 : ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
183 : }
184 :
185 670409 : if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
186 346045 : tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
187 : "configurationNamingContext");
188 346045 : ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
189 : }
190 :
191 670409 : if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
192 346045 : tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
193 : "schemaNamingContext");
194 346045 : ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
195 : }
196 :
197 670409 : if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
198 346045 : tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
199 : "defaultNamingContext");
200 346045 : ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
201 : }
202 :
203 670409 : talloc_free(tmp_ctx);
204 : }
205 :
206 1194757 : struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
207 : {
208 1194757 : void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
209 1194757 : return talloc_get_type(opaque, struct ldb_dn);
210 : }
211 :
212 3694400 : struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
213 : {
214 3694400 : void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
215 3694400 : return talloc_get_type(opaque, struct ldb_dn);
216 : }
217 :
218 2578066 : struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
219 : {
220 2578066 : void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
221 2578066 : return talloc_get_type(opaque, struct ldb_dn);
222 : }
223 :
224 5039546 : struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
225 : {
226 5039546 : void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
227 5039546 : 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 582630 : 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 582630 : ldb->flags = flags;
249 :
250 582630 : url2 = talloc_strdup(ldb, url);
251 582630 : if (!url2) {
252 0 : ldb_oom(ldb);
253 0 : return LDB_ERR_OPERATIONS_ERROR;
254 : }
255 582630 : ret = ldb_set_opaque(ldb, "ldb_url", url2);
256 582630 : if (ret != LDB_SUCCESS) {
257 0 : return ret;
258 : }
259 :
260 : /*
261 : * Take a copy of the options.
262 : */
263 582630 : ldb->options = ldb_options_copy(ldb, options);
264 582630 : if (ldb->options == NULL && options != NULL) {
265 0 : ldb_oom(ldb);
266 0 : return LDB_ERR_OPERATIONS_ERROR;
267 : }
268 :
269 582630 : ret = ldb_module_connect_backend(ldb, url, options, &ldb->modules);
270 582630 : if (ret != LDB_SUCCESS) {
271 1266 : return ret;
272 : }
273 :
274 581364 : ret = ldb_load_modules(ldb, options);
275 581364 : 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 581358 : ldb_set_default_dns(ldb);
284 :
285 581358 : return LDB_SUCCESS;
286 : }
287 :
288 724885 : void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
289 : {
290 724885 : ldb_asprintf_errstring(ldb, "%s", err_string);
291 724885 : }
292 :
293 3485462 : void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
294 : {
295 : va_list ap;
296 3485462 : char *old_err_string = NULL;
297 3485462 : if (ldb->err_string) {
298 698428 : old_err_string = ldb->err_string;
299 : }
300 :
301 3485462 : va_start(ap, format);
302 3485462 : ldb->err_string = talloc_vasprintf(ldb, format, ap);
303 3485462 : va_end(ap);
304 :
305 3485462 : TALLOC_FREE(old_err_string);
306 :
307 3485462 : 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 3485462 : }
312 :
313 41640612 : void ldb_reset_err_string(struct ldb_context *ldb)
314 : {
315 41640612 : TALLOC_FREE(ldb->err_string);
316 41640612 : }
317 :
318 :
319 :
320 : /*
321 : set an ldb error based on file:line
322 : */
323 180478 : int ldb_error_at(struct ldb_context *ldb, int ecode,
324 : const char *reason, const char *file, int line)
325 : {
326 180478 : if (reason == NULL) {
327 0 : reason = ldb_strerror(ecode);
328 : }
329 180478 : ldb_asprintf_errstring(ldb, "%s at %s:%d", reason, file, line);
330 180478 : 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 1351985 : int ldb_transaction_start(struct ldb_context *ldb)
358 : {
359 : struct ldb_module *next_module;
360 : int status;
361 :
362 1351985 : 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 1351985 : if (ldb->transaction_active) {
368 509311 : ldb->transaction_active++;
369 509311 : return LDB_SUCCESS;
370 : }
371 :
372 : /* start a new transaction */
373 842674 : ldb->transaction_active++;
374 842674 : ldb->prepare_commit_done = false;
375 :
376 1943705 : FIRST_OP(ldb, start_transaction);
377 :
378 842674 : ldb_reset_err_string(ldb);
379 :
380 842674 : status = next_module->ops->start_transaction(next_module);
381 842674 : 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 842652 : 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 842674 : return status;
400 : }
401 :
402 : /*
403 : prepare for transaction commit (first phase of two phase commit)
404 : */
405 1258082 : int ldb_transaction_prepare_commit(struct ldb_context *ldb)
406 : {
407 : struct ldb_module *next_module;
408 : int status;
409 :
410 1258082 : if (ldb->prepare_commit_done) {
411 2413 : return LDB_SUCCESS;
412 : }
413 :
414 : /* commit only when all nested transactions are complete */
415 1255669 : if (ldb->transaction_active > 1) {
416 509944 : return LDB_SUCCESS;
417 : }
418 :
419 745725 : ldb->prepare_commit_done = true;
420 :
421 745725 : 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 7021006 : FIRST_OP_NOERR(ldb, prepare_commit);
430 745725 : if (next_module == NULL) {
431 124253 : return LDB_SUCCESS;
432 : }
433 :
434 621472 : ldb_reset_err_string(ldb);
435 :
436 621472 : status = next_module->ops->prepare_commit(next_module);
437 621472 : if (status != LDB_SUCCESS) {
438 10 : ldb->transaction_active--;
439 : /* if a next_module fails the prepare then we need
440 : to call the end transaction for everyone */
441 12 : FIRST_OP(ldb, del_transaction);
442 10 : next_module->ops->del_transaction(next_module);
443 10 : 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 10 : 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 621472 : return status;
457 : }
458 :
459 :
460 : /*
461 : commit a transaction
462 : */
463 1254485 : int ldb_transaction_commit(struct ldb_context *ldb)
464 : {
465 : struct ldb_module *next_module;
466 : int status;
467 :
468 1254485 : status = ldb_transaction_prepare_commit(ldb);
469 1254485 : if (status != LDB_SUCCESS) {
470 10 : return status;
471 : }
472 :
473 1254475 : ldb->transaction_active--;
474 :
475 1254475 : 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 1254475 : if (ldb->transaction_active > 0) {
481 508764 : return LDB_SUCCESS;
482 : }
483 :
484 745711 : 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 745711 : ldb_reset_err_string(ldb);
492 :
493 1756008 : FIRST_OP(ldb, end_transaction);
494 745711 : status = next_module->ops->end_transaction(next_module);
495 745711 : if (status != LDB_SUCCESS) {
496 3 : 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 3 : 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 745711 : return status;
509 : }
510 :
511 :
512 : /*
513 : cancel a transaction
514 : */
515 97459 : int ldb_transaction_cancel(struct ldb_context *ldb)
516 : {
517 : struct ldb_module *next_module;
518 : int status;
519 :
520 97459 : ldb->transaction_active--;
521 :
522 97459 : 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 97459 : if (ldb->transaction_active > 0) {
528 547 : return LDB_SUCCESS;
529 : }
530 :
531 96912 : 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 187628 : FIRST_OP(ldb, del_transaction);
539 :
540 96912 : status = next_module->ops->del_transaction(next_module);
541 96912 : 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 96912 : 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 250900 : static int ldb_autotransaction_request(struct ldb_context *ldb,
572 : struct ldb_request *req)
573 : {
574 : int ret;
575 :
576 250900 : ret = ldb_transaction_start(ldb);
577 250900 : if (ret != LDB_SUCCESS) {
578 20 : return ret;
579 : }
580 :
581 250880 : ret = ldb_request(ldb, req);
582 250880 : if (ret == LDB_SUCCESS) {
583 250763 : ret = ldb_wait(req->handle, LDB_WAIT_ALL);
584 : }
585 :
586 250880 : if (ret == LDB_SUCCESS) {
587 233495 : return ldb_transaction_commit(ldb);
588 : }
589 17385 : ldb_transaction_cancel(ldb);
590 :
591 17385 : return ret;
592 : }
593 :
594 81778455 : int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
595 : {
596 : struct tevent_context *ev;
597 : int ret;
598 :
599 81778455 : if (handle == NULL) {
600 0 : return LDB_ERR_UNAVAILABLE;
601 : }
602 :
603 81778455 : if (handle->state == LDB_ASYNC_DONE) {
604 10947517 : 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 10947517 : return handle->status;
614 : }
615 :
616 70830938 : ev = ldb_handle_get_event_context(handle);
617 70830938 : if (NULL == ev) {
618 0 : return ldb_oom(handle->ldb);
619 : }
620 :
621 70830938 : switch (type) {
622 3450896 : case LDB_WAIT_NONE:
623 3450896 : ret = tevent_loop_once(ev);
624 3450896 : if (ret != 0) {
625 0 : return ldb_operr(handle->ldb);
626 : }
627 3450896 : if (handle->status == LDB_SUCCESS) {
628 3449371 : 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 67380042 : case LDB_WAIT_ALL:
644 211802992 : while (handle->state != LDB_ASYNC_DONE) {
645 146790322 : ret = tevent_loop_once(ev);
646 146790310 : if (ret != 0) {
647 0 : return ldb_operr(handle->ldb);
648 : }
649 146790310 : if (handle->status != LDB_SUCCESS) {
650 2367360 : if (handle->ldb->err_string != NULL) {
651 2354378 : return handle->status;
652 : }
653 : /*
654 : * if no error string was setup by the
655 : * backend
656 : */
657 12982 : 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 12982 : return handle->status;
664 : }
665 : }
666 65012670 : if (handle->status == LDB_SUCCESS) {
667 65012670 : 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 57555156 : int ldb_set_timeout(struct ldb_context *ldb,
689 : struct ldb_request *req,
690 : int timeout)
691 : {
692 57555156 : if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
693 :
694 57555156 : if (timeout != 0) {
695 305379 : req->timeout = timeout;
696 : } else {
697 57249777 : req->timeout = ldb->default_timeout;
698 : }
699 57555156 : req->starttime = time(NULL);
700 :
701 57555156 : return LDB_SUCCESS;
702 : }
703 :
704 : /* calculates the new timeout based on the previous starttime and timeout */
705 407248554 : int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
706 : struct ldb_request *oldreq,
707 : struct ldb_request *newreq)
708 : {
709 407248554 : if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
710 :
711 407248554 : if (oldreq == NULL) {
712 46977959 : return ldb_set_timeout(ldb, newreq, 0);
713 : }
714 :
715 360270595 : newreq->starttime = oldreq->starttime;
716 360270595 : newreq->timeout = oldreq->timeout;
717 :
718 360270595 : return LDB_SUCCESS;
719 : }
720 :
721 :
722 56153381 : struct ldb_handle *ldb_handle_new(TALLOC_CTX *mem_ctx, struct ldb_context *ldb)
723 : {
724 : struct ldb_handle *h;
725 :
726 56153381 : h = talloc_zero(mem_ctx, struct ldb_handle);
727 56153381 : if (h == NULL) {
728 0 : ldb_set_errstring(ldb, "Out of Memory");
729 0 : return NULL;
730 : }
731 :
732 56153381 : h->status = LDB_SUCCESS;
733 56153381 : h->state = LDB_ASYNC_INIT;
734 56153381 : h->ldb = ldb;
735 56153381 : h->flags = 0;
736 56153381 : h->location = NULL;
737 56153381 : h->parent = NULL;
738 :
739 56153381 : if (h->ldb->require_private_event_context == true) {
740 55684307 : h->event_context = tevent_context_init(h);
741 55684307 : 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 55684307 : tevent_set_debug(h->event_context, ldb_tevent_debug, ldb);
748 55684307 : tevent_loop_allow_nesting(h->event_context);
749 : }
750 :
751 56153381 : return h;
752 : }
753 :
754 360270595 : 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 360270595 : h = talloc_zero(mem_ctx, struct ldb_handle);
760 360270595 : if (h == NULL) {
761 0 : ldb_set_errstring(parent_req->handle->ldb,
762 : "Out of Memory");
763 0 : return NULL;
764 : }
765 :
766 360270595 : h->status = LDB_SUCCESS;
767 360270595 : h->state = LDB_ASYNC_INIT;
768 360270595 : h->ldb = parent_req->handle->ldb;
769 360270595 : h->parent = parent_req;
770 360270595 : h->nesting = parent_req->handle->nesting + 1;
771 360270595 : h->flags = parent_req->handle->flags;
772 360270595 : h->custom_flags = parent_req->handle->custom_flags;
773 360270595 : h->event_context = parent_req->handle->event_context;
774 :
775 360270595 : 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 1159740 : void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
783 : {
784 1159740 : ldb->create_perms = perms;
785 1159740 : }
786 :
787 1948435 : unsigned int ldb_get_create_perms(struct ldb_context *ldb)
788 : {
789 1948435 : return ldb->create_perms;
790 : }
791 :
792 1457236 : void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
793 : {
794 1457236 : ldb->ev_ctx = ev;
795 1457236 : }
796 :
797 176888199 : struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
798 : {
799 176888199 : return ldb->ev_ctx;
800 : }
801 :
802 142671605 : void ldb_request_set_state(struct ldb_request *req, int state)
803 : {
804 142671605 : req->handle->state = state;
805 142671605 : }
806 :
807 142115594 : int ldb_request_get_status(struct ldb_request *req)
808 : {
809 142115594 : 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 213034686 : struct tevent_context *ldb_handle_get_event_context(struct ldb_handle *handle)
818 : {
819 213034686 : if (handle->event_context != NULL) {
820 209117408 : return handle->event_context;
821 : }
822 3917278 : 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 2116312 : void ldb_set_require_private_event_context(struct ldb_context *ldb)
841 : {
842 2116312 : ldb->require_private_event_context = true;
843 2116312 : }
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 1069233 : static int ldb_msg_check_element_flags(struct ldb_context *ldb,
961 : const struct ldb_message *message)
962 : {
963 : unsigned i;
964 5659536 : for (i=0; i<message->num_elements; i++) {
965 4590303 : 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 1069233 : 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 18803225 : static int ldb_db_lock_destructor(struct ldb_db_lock_context *lock_context)
992 : {
993 : int ret;
994 : struct ldb_module *next_module;
995 86656695 : FIRST_OP_NOERR(lock_context->ldb, read_unlock);
996 18803225 : if (next_module != NULL) {
997 18803225 : ret = next_module->ops->read_unlock(next_module);
998 : } else {
999 0 : ret = LDB_SUCCESS;
1000 : }
1001 :
1002 18803225 : 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 18803225 : return 0;
1010 : }
1011 :
1012 39982953 : 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 39982953 : 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 39982953 : lock_context = talloc_get_type(req->context,
1026 : struct ldb_db_lock_context);
1027 :
1028 39982953 : if (!ares) {
1029 0 : return ldb_module_done(lock_context->req, NULL, NULL,
1030 : LDB_ERR_OPERATIONS_ERROR);
1031 : }
1032 39982953 : if (ares->error != LDB_SUCCESS || ares->type == LDB_REPLY_DONE) {
1033 18803196 : 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 18803194 : TALLOC_FREE(req->context);
1040 18803194 : return ret;
1041 : }
1042 :
1043 : /* Otherwise pass on the callback */
1044 21179757 : switch (ares->type) {
1045 18908848 : case LDB_REPLY_ENTRY:
1046 18908848 : return ldb_module_send_entry(lock_context->req, ares->message,
1047 : ares->controls);
1048 :
1049 2270909 : case LDB_REPLY_REFERRAL:
1050 2270909 : 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 19085001 : 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 19085001 : struct ldb_module *next_module = NULL;
1066 19085001 : struct ldb_request *down_req = NULL;
1067 : struct ldb_db_lock_context *lock_context;
1068 19085001 : struct ldb_context *ldb = ldb_module_get_ctx(lock_module);
1069 : int ret;
1070 :
1071 19085001 : lock_context = talloc(req, struct ldb_db_lock_context);
1072 19085001 : if (lock_context == NULL) {
1073 0 : return ldb_oom(ldb);
1074 : }
1075 :
1076 19085001 : lock_context->ldb = ldb;
1077 19085001 : lock_context->req = req;
1078 :
1079 19085001 : 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 19085001 : LDB_REQ_SET_LOCATION(down_req);
1089 19085001 : if (ret != LDB_SUCCESS) {
1090 0 : return ret;
1091 : }
1092 :
1093 : /* call DB lock */
1094 87284507 : FIRST_OP_NOERR(ldb, read_lock);
1095 19085001 : if (next_module != NULL) {
1096 18803239 : ret = next_module->ops->read_lock(next_module);
1097 : } else {
1098 281762 : ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1099 : }
1100 :
1101 19085001 : if (ret == LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION) {
1102 : /* We might be talking LDAP */
1103 281762 : ldb_reset_err_string(ldb);
1104 281762 : TALLOC_FREE(lock_context);
1105 :
1106 281762 : return ldb_next_request(lock_module, req);
1107 18803239 : } 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 18803239 : talloc_set_destructor(lock_context, ldb_db_lock_destructor);
1113 : }
1114 :
1115 18803239 : if (ret != LDB_SUCCESS) {
1116 2 : return ret;
1117 : }
1118 :
1119 18803237 : 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 30969441 : int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
1128 : {
1129 : struct ldb_module *next_module;
1130 : int ret;
1131 :
1132 30969441 : 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 30969441 : ldb_reset_err_string(ldb);
1138 :
1139 30969441 : 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 30969441 : switch (req->operation) {
1145 19085106 : 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 19085106 : struct ldb_module lock_module = {
1156 : .ldb = ldb,
1157 19085106 : .next = ldb->modules,
1158 : .ops = &lock_module_ops
1159 : };
1160 19085106 : next_module = &lock_module;
1161 :
1162 : /* due to "ldb_build_search_req" base DN always != NULL */
1163 19085106 : if (!ldb_dn_validate(req->op.search.base)) {
1164 105 : ldb_asprintf_errstring(ldb, "ldb_search: invalid basedn '%s'",
1165 : ldb_dn_get_linearized(req->op.search.base));
1166 105 : return LDB_ERR_INVALID_DN_SYNTAX;
1167 : }
1168 :
1169 19085001 : ret = next_module->ops->search(next_module, req);
1170 19085001 : break;
1171 : }
1172 600141 : case LDB_ADD:
1173 600141 : if (!ldb_dn_validate(req->op.add.message->dn)) {
1174 20 : ldb_asprintf_errstring(ldb, "ldb_add: invalid dn '%s'",
1175 20 : ldb_dn_get_linearized(req->op.add.message->dn));
1176 20 : 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 600121 : ret = ldb_msg_normalize(ldb, req, req->op.add.message,
1184 600121 : discard_const(&req->op.add.message));
1185 600121 : if (ret != LDB_SUCCESS) {
1186 0 : ldb_oom(ldb);
1187 0 : return ret;
1188 : }
1189 977918 : FIRST_OP(ldb, add);
1190 600121 : ret = ldb_msg_check_element_flags(ldb, req->op.add.message);
1191 600121 : if (ret != LDB_SUCCESS) {
1192 : /*
1193 : * "ldb_msg_check_element_flags" generates an error
1194 : * string
1195 : */
1196 0 : return ret;
1197 : }
1198 600121 : ret = next_module->ops->add(next_module, req);
1199 600121 : break;
1200 469112 : case LDB_MODIFY:
1201 469112 : 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 852710 : FIRST_OP(ldb, modify);
1207 469112 : ret = ldb_msg_check_element_flags(ldb, req->op.mod.message);
1208 469112 : if (ret != LDB_SUCCESS) {
1209 : /*
1210 : * "ldb_msg_check_element_flags" generates an error
1211 : * string
1212 : */
1213 0 : return ret;
1214 : }
1215 469112 : ret = next_module->ops->modify(next_module, req);
1216 469112 : break;
1217 204677 : case LDB_DELETE:
1218 204677 : 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 454385 : FIRST_OP(ldb, del);
1224 204677 : ret = next_module->ops->del(next_module, req);
1225 204677 : break;
1226 1334 : case LDB_RENAME:
1227 1334 : if (!ldb_dn_validate(req->op.rename.olddn)) {
1228 22 : ldb_asprintf_errstring(ldb, "ldb_rename: invalid olddn '%s'",
1229 : ldb_dn_get_linearized(req->op.rename.olddn));
1230 22 : return LDB_ERR_INVALID_DN_SYNTAX;
1231 : }
1232 1312 : if (!ldb_dn_validate(req->op.rename.newdn)) {
1233 25 : ldb_asprintf_errstring(ldb, "ldb_rename: invalid newdn '%s'",
1234 : ldb_dn_get_linearized(req->op.rename.newdn));
1235 25 : return LDB_ERR_INVALID_DN_SYNTAX;
1236 : }
1237 2754 : FIRST_OP(ldb, rename);
1238 1287 : ret = next_module->ops->rename(next_module, req);
1239 1287 : break;
1240 1433649 : case LDB_EXTENDED:
1241 4341639 : FIRST_OP(ldb, extended);
1242 1433334 : ret = next_module->ops->extended(next_module, req);
1243 1433334 : break;
1244 9175422 : default:
1245 27795484 : FIRST_OP(ldb, request);
1246 8940231 : ret = next_module->ops->request(next_module, req);
1247 8940231 : break;
1248 : }
1249 :
1250 30733763 : if ((ret != LDB_SUCCESS) && (ldb->err_string == NULL)) {
1251 : /* if no error string was setup by the backend */
1252 5 : ldb_asprintf_errstring(ldb, "ldb_request: %s (%d)",
1253 : ldb_strerror(ret), ret);
1254 : }
1255 :
1256 30733763 : return ret;
1257 : }
1258 :
1259 78338153 : int ldb_request_done(struct ldb_request *req, int status)
1260 : {
1261 78338153 : req->handle->state = LDB_ASYNC_DONE;
1262 78338153 : req->handle->status = status;
1263 78338153 : 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 157581091 : 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 157581091 : res = talloc_get_type(req->context, struct ldb_result);
1281 :
1282 157581091 : if (!ares) {
1283 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1284 : }
1285 157581091 : if (ares->error != LDB_SUCCESS) {
1286 1213996 : return ldb_request_done(req, ares->error);
1287 : }
1288 :
1289 156367095 : switch (ares->type) {
1290 111175693 : case LDB_REPLY_ENTRY:
1291 111175693 : res->msgs = talloc_realloc(res, res->msgs,
1292 : struct ldb_message *, res->count + 2);
1293 111175693 : if (! res->msgs) {
1294 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1295 : }
1296 :
1297 111175693 : res->msgs[res->count + 1] = NULL;
1298 :
1299 111175693 : res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
1300 111175693 : res->count++;
1301 111175693 : break;
1302 :
1303 2922112 : case LDB_REPLY_REFERRAL:
1304 2922112 : if (res->refs) {
1305 4355600 : for (n = 0; res->refs[n]; n++) /*noop*/ ;
1306 : } else {
1307 1179872 : n = 0;
1308 : }
1309 :
1310 2922112 : res->refs = talloc_realloc(res, res->refs, char *, n + 2);
1311 2922112 : if (! res->refs) {
1312 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1313 : }
1314 :
1315 2922112 : res->refs[n] = talloc_move(res->refs, &ares->referral);
1316 2922112 : res->refs[n + 1] = NULL;
1317 2922112 : break;
1318 :
1319 42269290 : case LDB_REPLY_DONE:
1320 : /* TODO: we should really support controls on entries
1321 : * and referrals too! */
1322 42269290 : 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 42269290 : talloc_free(ares);
1328 42269290 : return ldb_request_done(req, LDB_SUCCESS);
1329 : }
1330 :
1331 114097805 : talloc_free(ares);
1332 :
1333 114097805 : return LDB_SUCCESS;
1334 : }
1335 :
1336 1572067 : 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 1572067 : res = talloc_get_type(req->context, struct ldb_result);
1343 :
1344 1572067 : if (!ares) {
1345 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1346 : }
1347 :
1348 1572067 : if (ares->error != LDB_SUCCESS) {
1349 39573 : ret = ares->error;
1350 39573 : talloc_free(ares);
1351 39573 : return ldb_request_done(req, ret);
1352 : }
1353 :
1354 1532494 : 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 1532485 : case LDB_REPLY_DONE:
1372 1532485 : talloc_free(ares);
1373 1532485 : 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 9994119 : int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
1385 : {
1386 : int ret;
1387 :
1388 9994119 : if (!ares) {
1389 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1390 : }
1391 :
1392 9994119 : if (ares->error != LDB_SUCCESS) {
1393 58762 : ret = ares->error;
1394 58762 : talloc_free(ares);
1395 58762 : return ldb_request_done(req, ret);
1396 : }
1397 :
1398 9935357 : 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 9934993 : talloc_free(ares);
1405 9934993 : return ldb_request_done(req, LDB_SUCCESS);
1406 : }
1407 :
1408 407248554 : 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 407248554 : struct ldb_request *req = NULL;
1416 :
1417 407248554 : req = talloc_zero(mem_ctx, struct ldb_request);
1418 407248554 : if (req == NULL) {
1419 0 : return NULL;
1420 : }
1421 407248554 : req->controls = controls;
1422 407248554 : req->context = context;
1423 407248554 : req->callback = callback;
1424 :
1425 407248554 : ldb_set_timeout_from_prev_req(ldb, parent, req);
1426 :
1427 407248554 : if (parent != NULL) {
1428 360270595 : req->handle = ldb_handle_new_child(req, parent);
1429 360270595 : if (req->handle == NULL) {
1430 0 : TALLOC_FREE(req);
1431 0 : return NULL;
1432 : }
1433 : } else {
1434 46977959 : req->handle = ldb_handle_new(req, ldb);
1435 46977959 : if (req->handle == NULL) {
1436 0 : TALLOC_FREE(req);
1437 0 : return NULL;
1438 : }
1439 : }
1440 :
1441 407248554 : return req;
1442 : }
1443 :
1444 377880996 : 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 377880996 : *ret_req = NULL;
1459 :
1460 377880996 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1461 : context, callback, parent);
1462 377880996 : if (req == NULL) {
1463 0 : ldb_oom(ldb);
1464 0 : return LDB_ERR_OPERATIONS_ERROR;
1465 : }
1466 :
1467 377880996 : req->operation = LDB_SEARCH;
1468 377880996 : if (base == NULL) {
1469 18270407 : req->op.search.base = ldb_dn_new(req, ldb, NULL);
1470 18270407 : if (req->op.search.base == NULL) {
1471 0 : ldb_oom(ldb);
1472 0 : return LDB_ERR_OPERATIONS_ERROR;
1473 : }
1474 : } else {
1475 359610589 : req->op.search.base = base;
1476 : }
1477 377880996 : req->op.search.scope = scope;
1478 :
1479 377880996 : req->op.search.tree = tree;
1480 377880996 : if (req->op.search.tree == NULL) {
1481 0 : ldb_set_errstring(ldb, "'tree' can't be NULL");
1482 0 : talloc_free(req);
1483 0 : return LDB_ERR_OPERATIONS_ERROR;
1484 : }
1485 :
1486 377880996 : req->op.search.attrs = attrs;
1487 377880996 : *ret_req = req;
1488 377880996 : return LDB_SUCCESS;
1489 : }
1490 :
1491 48871069 : int ldb_build_search_req(struct ldb_request **ret_req,
1492 : struct ldb_context *ldb,
1493 : TALLOC_CTX *mem_ctx,
1494 : struct ldb_dn *base,
1495 : enum ldb_scope scope,
1496 : const char *expression,
1497 : const char * const *attrs,
1498 : struct ldb_control **controls,
1499 : void *context,
1500 : ldb_request_callback_t callback,
1501 : struct ldb_request *parent)
1502 : {
1503 : struct ldb_parse_tree *tree;
1504 : int ret;
1505 :
1506 48871069 : tree = ldb_parse_tree(mem_ctx, expression);
1507 48871069 : if (tree == NULL) {
1508 9 : ldb_set_errstring(ldb, "Unable to parse search expression");
1509 9 : return LDB_ERR_OPERATIONS_ERROR;
1510 : }
1511 :
1512 48871060 : ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
1513 : scope, tree, attrs, controls,
1514 : context, callback, parent);
1515 48871060 : if (ret == LDB_SUCCESS) {
1516 48871060 : talloc_steal(*ret_req, tree);
1517 : }
1518 48871060 : return ret;
1519 : }
1520 :
1521 4499584 : int ldb_build_add_req(struct ldb_request **ret_req,
1522 : struct ldb_context *ldb,
1523 : TALLOC_CTX *mem_ctx,
1524 : const struct ldb_message *message,
1525 : struct ldb_control **controls,
1526 : void *context,
1527 : ldb_request_callback_t callback,
1528 : struct ldb_request *parent)
1529 : {
1530 : struct ldb_request *req;
1531 :
1532 4499584 : *ret_req = NULL;
1533 :
1534 4499584 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1535 : context, callback, parent);
1536 4499584 : if (req == NULL) {
1537 0 : ldb_set_errstring(ldb, "Out of Memory");
1538 0 : return LDB_ERR_OPERATIONS_ERROR;
1539 : }
1540 :
1541 4499584 : req->operation = LDB_ADD;
1542 4499584 : req->op.add.message = message;
1543 4499584 : *ret_req = req;
1544 4499584 : return LDB_SUCCESS;
1545 : }
1546 :
1547 4952031 : int ldb_build_mod_req(struct ldb_request **ret_req,
1548 : struct ldb_context *ldb,
1549 : TALLOC_CTX *mem_ctx,
1550 : const struct ldb_message *message,
1551 : struct ldb_control **controls,
1552 : void *context,
1553 : ldb_request_callback_t callback,
1554 : struct ldb_request *parent)
1555 : {
1556 : struct ldb_request *req;
1557 :
1558 4952031 : *ret_req = NULL;
1559 :
1560 4952031 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1561 : context, callback, parent);
1562 4952031 : if (req == NULL) {
1563 0 : ldb_set_errstring(ldb, "Out of Memory");
1564 0 : return LDB_ERR_OPERATIONS_ERROR;
1565 : }
1566 :
1567 4952031 : req->operation = LDB_MODIFY;
1568 4952031 : req->op.mod.message = message;
1569 :
1570 4952031 : *ret_req = req;
1571 4952031 : return LDB_SUCCESS;
1572 : }
1573 :
1574 424836 : int ldb_build_del_req(struct ldb_request **ret_req,
1575 : struct ldb_context *ldb,
1576 : TALLOC_CTX *mem_ctx,
1577 : struct ldb_dn *dn,
1578 : struct ldb_control **controls,
1579 : void *context,
1580 : ldb_request_callback_t callback,
1581 : struct ldb_request *parent)
1582 : {
1583 : struct ldb_request *req;
1584 :
1585 424836 : *ret_req = NULL;
1586 :
1587 424836 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1588 : context, callback, parent);
1589 424836 : if (req == NULL) {
1590 0 : ldb_set_errstring(ldb, "Out of Memory");
1591 0 : return LDB_ERR_OPERATIONS_ERROR;
1592 : }
1593 :
1594 424836 : req->operation = LDB_DELETE;
1595 424836 : req->op.del.dn = dn;
1596 424836 : *ret_req = req;
1597 424836 : return LDB_SUCCESS;
1598 : }
1599 :
1600 269501 : int ldb_build_rename_req(struct ldb_request **ret_req,
1601 : struct ldb_context *ldb,
1602 : TALLOC_CTX *mem_ctx,
1603 : struct ldb_dn *olddn,
1604 : struct ldb_dn *newdn,
1605 : struct ldb_control **controls,
1606 : void *context,
1607 : ldb_request_callback_t callback,
1608 : struct ldb_request *parent)
1609 : {
1610 : struct ldb_request *req;
1611 :
1612 269501 : *ret_req = NULL;
1613 :
1614 269501 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1615 : context, callback, parent);
1616 269501 : if (req == NULL) {
1617 0 : ldb_set_errstring(ldb, "Out of Memory");
1618 0 : return LDB_ERR_OPERATIONS_ERROR;
1619 : }
1620 :
1621 269501 : req->operation = LDB_RENAME;
1622 269501 : req->op.rename.olddn = olddn;
1623 269501 : req->op.rename.newdn = newdn;
1624 269501 : *ret_req = req;
1625 269501 : return LDB_SUCCESS;
1626 : }
1627 :
1628 19217025 : int ldb_extended_default_callback(struct ldb_request *req,
1629 : struct ldb_reply *ares)
1630 : {
1631 : struct ldb_result *res;
1632 :
1633 19217025 : res = talloc_get_type(req->context, struct ldb_result);
1634 :
1635 19217025 : if (!ares) {
1636 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1637 : }
1638 19217025 : if (ares->error != LDB_SUCCESS) {
1639 74 : return ldb_request_done(req, ares->error);
1640 : }
1641 :
1642 19216951 : if (ares->type == LDB_REPLY_DONE) {
1643 :
1644 : /* TODO: we should really support controls on entries and referrals too! */
1645 19216951 : res->extended = talloc_move(res, &ares->response);
1646 19216951 : res->controls = talloc_move(res, &ares->controls);
1647 :
1648 19216951 : talloc_free(ares);
1649 19216951 : return ldb_request_done(req, LDB_SUCCESS);
1650 : }
1651 :
1652 0 : talloc_free(ares);
1653 0 : ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
1654 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1655 : }
1656 :
1657 19221606 : int ldb_build_extended_req(struct ldb_request **ret_req,
1658 : struct ldb_context *ldb,
1659 : TALLOC_CTX *mem_ctx,
1660 : const char *oid,
1661 : void *data,
1662 : struct ldb_control **controls,
1663 : void *context,
1664 : ldb_request_callback_t callback,
1665 : struct ldb_request *parent)
1666 : {
1667 : struct ldb_request *req;
1668 :
1669 19221606 : *ret_req = NULL;
1670 :
1671 19221606 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1672 : context, callback, parent);
1673 19221606 : if (req == NULL) {
1674 0 : ldb_set_errstring(ldb, "Out of Memory");
1675 0 : return LDB_ERR_OPERATIONS_ERROR;
1676 : }
1677 :
1678 19221606 : req->operation = LDB_EXTENDED;
1679 19221606 : req->op.extended.oid = oid;
1680 19221606 : req->op.extended.data = data;
1681 19221606 : *ret_req = req;
1682 19221606 : return LDB_SUCCESS;
1683 : }
1684 :
1685 1093559 : int ldb_extended(struct ldb_context *ldb,
1686 : const char *oid,
1687 : void *data,
1688 : struct ldb_result **_res)
1689 : {
1690 : struct ldb_request *req;
1691 : int ret;
1692 : struct ldb_result *res;
1693 :
1694 1093559 : *_res = NULL;
1695 1093559 : req = NULL;
1696 :
1697 1093559 : res = talloc_zero(ldb, struct ldb_result);
1698 1093559 : if (!res) {
1699 0 : return LDB_ERR_OPERATIONS_ERROR;
1700 : }
1701 :
1702 1093559 : ret = ldb_build_extended_req(&req, ldb, ldb,
1703 : oid, data, NULL,
1704 : res, ldb_extended_default_callback,
1705 : NULL);
1706 1093559 : ldb_req_set_location(req, "ldb_extended");
1707 :
1708 1093559 : if (ret != LDB_SUCCESS) goto done;
1709 :
1710 1093559 : ldb_set_timeout(ldb, req, 0); /* use default timeout */
1711 :
1712 1093559 : ret = ldb_request(ldb, req);
1713 :
1714 1093559 : if (ret == LDB_SUCCESS) {
1715 1093238 : ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1716 : }
1717 :
1718 321 : done:
1719 1093559 : if (ret != LDB_SUCCESS) {
1720 389 : talloc_free(res);
1721 389 : res = NULL;
1722 : }
1723 :
1724 1093559 : talloc_free(req);
1725 :
1726 1093559 : *_res = res;
1727 1093559 : return ret;
1728 : }
1729 :
1730 : /*
1731 : note that ldb_search() will automatically replace a NULL 'base' value
1732 : with the defaultNamingContext from the rootDSE if available.
1733 : */
1734 5461509 : int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1735 : struct ldb_result **result, struct ldb_dn *base,
1736 : enum ldb_scope scope, const char * const *attrs,
1737 : const char *exp_fmt, ...)
1738 : {
1739 : struct ldb_request *req;
1740 : struct ldb_result *res;
1741 : char *expression;
1742 : va_list ap;
1743 : int ret;
1744 :
1745 5461509 : expression = NULL;
1746 5461509 : *result = NULL;
1747 5461509 : req = NULL;
1748 :
1749 5461509 : res = talloc_zero(mem_ctx, struct ldb_result);
1750 5461509 : if (!res) {
1751 0 : return LDB_ERR_OPERATIONS_ERROR;
1752 : }
1753 :
1754 5461509 : if (exp_fmt) {
1755 3544756 : va_start(ap, exp_fmt);
1756 3544756 : expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1757 3544756 : va_end(ap);
1758 :
1759 3544756 : if (!expression) {
1760 0 : talloc_free(res);
1761 0 : return LDB_ERR_OPERATIONS_ERROR;
1762 : }
1763 : }
1764 :
1765 6582868 : ret = ldb_build_search_req(&req, ldb, mem_ctx,
1766 1121359 : base?base:ldb_get_default_basedn(ldb),
1767 : scope,
1768 : expression,
1769 : attrs,
1770 : NULL,
1771 : res,
1772 : ldb_search_default_callback,
1773 : NULL);
1774 5461509 : ldb_req_set_location(req, "ldb_search");
1775 :
1776 5461509 : if (ret != LDB_SUCCESS) goto done;
1777 :
1778 5461509 : ret = ldb_request(ldb, req);
1779 :
1780 5461509 : if (ret == LDB_SUCCESS) {
1781 5460670 : ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1782 : }
1783 :
1784 839 : done:
1785 5461509 : if (ret != LDB_SUCCESS) {
1786 236893 : talloc_free(res);
1787 236893 : res = NULL;
1788 : }
1789 :
1790 5461509 : talloc_free(expression);
1791 5461509 : talloc_free(req);
1792 :
1793 5461509 : *result = res;
1794 5461509 : return ret;
1795 : }
1796 :
1797 : /*
1798 : add a record to the database. Will fail if a record with the given class
1799 : and key already exists
1800 : */
1801 84575 : int ldb_add(struct ldb_context *ldb,
1802 : const struct ldb_message *message)
1803 : {
1804 : struct ldb_request *req;
1805 : int ret;
1806 :
1807 84575 : ret = ldb_msg_sanity_check(ldb, message);
1808 84575 : if (ret != LDB_SUCCESS) {
1809 0 : return ret;
1810 : }
1811 :
1812 84575 : ret = ldb_build_add_req(&req, ldb, ldb,
1813 : message,
1814 : NULL,
1815 : NULL,
1816 : ldb_op_default_callback,
1817 : NULL);
1818 84575 : ldb_req_set_location(req, "ldb_add");
1819 :
1820 84575 : if (ret != LDB_SUCCESS) return ret;
1821 :
1822 : /* do request and autostart a transaction */
1823 84575 : ret = ldb_autotransaction_request(ldb, req);
1824 :
1825 84575 : talloc_free(req);
1826 84575 : return ret;
1827 : }
1828 :
1829 : /*
1830 : modify the specified attributes of a record
1831 : */
1832 117225 : int ldb_modify(struct ldb_context *ldb,
1833 : const struct ldb_message *message)
1834 : {
1835 : struct ldb_request *req;
1836 : int ret;
1837 :
1838 117225 : ret = ldb_msg_sanity_check(ldb, message);
1839 117225 : if (ret != LDB_SUCCESS) {
1840 0 : return ret;
1841 : }
1842 :
1843 117225 : ret = ldb_build_mod_req(&req, ldb, ldb,
1844 : message,
1845 : NULL,
1846 : NULL,
1847 : ldb_op_default_callback,
1848 : NULL);
1849 117225 : ldb_req_set_location(req, "ldb_modify");
1850 :
1851 117225 : if (ret != LDB_SUCCESS) return ret;
1852 :
1853 : /* do request and autostart a transaction */
1854 117225 : ret = ldb_autotransaction_request(ldb, req);
1855 :
1856 117225 : talloc_free(req);
1857 117225 : return ret;
1858 : }
1859 :
1860 :
1861 : /*
1862 : delete a record from the database
1863 : */
1864 49004 : int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1865 : {
1866 : struct ldb_request *req;
1867 : int ret;
1868 :
1869 49004 : ret = ldb_build_del_req(&req, ldb, ldb,
1870 : dn,
1871 : NULL,
1872 : NULL,
1873 : ldb_op_default_callback,
1874 : NULL);
1875 49004 : ldb_req_set_location(req, "ldb_delete");
1876 :
1877 49004 : if (ret != LDB_SUCCESS) return ret;
1878 :
1879 : /* do request and autostart a transaction */
1880 49004 : ret = ldb_autotransaction_request(ldb, req);
1881 :
1882 49004 : talloc_free(req);
1883 49004 : return ret;
1884 : }
1885 :
1886 : /*
1887 : rename a record in the database
1888 : */
1889 96 : int ldb_rename(struct ldb_context *ldb,
1890 : struct ldb_dn *olddn, struct ldb_dn *newdn)
1891 : {
1892 : struct ldb_request *req;
1893 : int ret;
1894 :
1895 96 : ret = ldb_build_rename_req(&req, ldb, ldb,
1896 : olddn,
1897 : newdn,
1898 : NULL,
1899 : NULL,
1900 : ldb_op_default_callback,
1901 : NULL);
1902 96 : ldb_req_set_location(req, "ldb_rename");
1903 :
1904 96 : if (ret != LDB_SUCCESS) return ret;
1905 :
1906 : /* do request and autostart a transaction */
1907 96 : ret = ldb_autotransaction_request(ldb, req);
1908 :
1909 96 : talloc_free(req);
1910 96 : return ret;
1911 : }
1912 :
1913 :
1914 : /*
1915 : return the global sequence number
1916 : */
1917 1088397 : int ldb_sequence_number(struct ldb_context *ldb,
1918 : enum ldb_sequence_type type, uint64_t *seq_num)
1919 : {
1920 : struct ldb_seqnum_request *seq;
1921 : struct ldb_seqnum_result *seqr;
1922 : struct ldb_result *res;
1923 : TALLOC_CTX *tmp_ctx;
1924 : int ret;
1925 :
1926 1088397 : *seq_num = 0;
1927 :
1928 1088397 : tmp_ctx = talloc_zero(ldb, struct ldb_request);
1929 1088397 : if (tmp_ctx == NULL) {
1930 0 : ldb_set_errstring(ldb, "Out of Memory");
1931 0 : return LDB_ERR_OPERATIONS_ERROR;
1932 : }
1933 1088397 : seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1934 1088397 : if (seq == NULL) {
1935 0 : ldb_set_errstring(ldb, "Out of Memory");
1936 0 : ret = LDB_ERR_OPERATIONS_ERROR;
1937 0 : goto done;
1938 : }
1939 1088397 : seq->type = type;
1940 :
1941 1088397 : ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1942 1088397 : if (ret != LDB_SUCCESS) {
1943 315 : goto done;
1944 : }
1945 1088082 : talloc_steal(tmp_ctx, res);
1946 :
1947 1088082 : if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1948 0 : ldb_set_errstring(ldb, "Invalid OID in reply");
1949 0 : ret = LDB_ERR_OPERATIONS_ERROR;
1950 0 : goto done;
1951 : }
1952 1088082 : seqr = talloc_get_type(res->extended->data,
1953 : struct ldb_seqnum_result);
1954 1088082 : *seq_num = seqr->seq_num;
1955 :
1956 1088397 : done:
1957 1088397 : talloc_free(tmp_ctx);
1958 1088397 : return ret;
1959 : }
1960 :
1961 : /*
1962 : return extended error information
1963 : */
1964 405196 : const char *ldb_errstring(struct ldb_context *ldb)
1965 : {
1966 405196 : if (ldb->err_string) {
1967 255882 : return ldb->err_string;
1968 : }
1969 :
1970 149314 : return NULL;
1971 : }
1972 :
1973 : /*
1974 : return a string explaining what a ldb error constant meancs
1975 : */
1976 592442 : const char *ldb_strerror(int ldb_err)
1977 : {
1978 592442 : switch (ldb_err) {
1979 560239 : case LDB_SUCCESS:
1980 560239 : return "Success";
1981 15 : case LDB_ERR_OPERATIONS_ERROR:
1982 15 : return "Operations error";
1983 5 : case LDB_ERR_PROTOCOL_ERROR:
1984 5 : return "Protocol error";
1985 9 : case LDB_ERR_TIME_LIMIT_EXCEEDED:
1986 9 : return "Time limit exceeded";
1987 0 : case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1988 0 : return "Size limit exceeded";
1989 0 : case LDB_ERR_COMPARE_FALSE:
1990 0 : return "Compare false";
1991 0 : case LDB_ERR_COMPARE_TRUE:
1992 0 : return "Compare true";
1993 0 : case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1994 0 : return "Auth method not supported";
1995 0 : case LDB_ERR_STRONG_AUTH_REQUIRED:
1996 0 : return "Strong auth required";
1997 : /* 9 RESERVED */
1998 8 : case LDB_ERR_REFERRAL:
1999 8 : return "Referral error";
2000 0 : case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
2001 0 : return "Admin limit exceeded";
2002 10 : case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
2003 10 : return "Unsupported critical extension";
2004 0 : case LDB_ERR_CONFIDENTIALITY_REQUIRED:
2005 0 : return "Confidentiality required";
2006 0 : case LDB_ERR_SASL_BIND_IN_PROGRESS:
2007 0 : return "SASL bind in progress";
2008 131 : case LDB_ERR_NO_SUCH_ATTRIBUTE:
2009 131 : return "No such attribute";
2010 2 : case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
2011 2 : return "Undefined attribute type";
2012 0 : case LDB_ERR_INAPPROPRIATE_MATCHING:
2013 0 : return "Inappropriate matching";
2014 3738 : case LDB_ERR_CONSTRAINT_VIOLATION:
2015 3738 : return "Constraint violation";
2016 125 : case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
2017 125 : return "Attribute or value exists";
2018 47 : case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
2019 47 : return "Invalid attribute syntax";
2020 : /* 22-31 unused */
2021 25089 : case LDB_ERR_NO_SUCH_OBJECT:
2022 25089 : return "No such object";
2023 0 : case LDB_ERR_ALIAS_PROBLEM:
2024 0 : return "Alias problem";
2025 188 : case LDB_ERR_INVALID_DN_SYNTAX:
2026 188 : return "Invalid DN syntax";
2027 : /* 35 RESERVED */
2028 0 : case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
2029 0 : return "Alias dereferencing problem";
2030 : /* 37-47 unused */
2031 0 : case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
2032 0 : return "Inappropriate authentication";
2033 0 : case LDB_ERR_INVALID_CREDENTIALS:
2034 0 : return "Invalid credentials";
2035 1642 : case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
2036 1642 : return "insufficient access rights";
2037 0 : case LDB_ERR_BUSY:
2038 0 : return "Busy";
2039 0 : case LDB_ERR_UNAVAILABLE:
2040 0 : return "Unavailable";
2041 471 : case LDB_ERR_UNWILLING_TO_PERFORM:
2042 471 : return "Unwilling to perform";
2043 0 : case LDB_ERR_LOOP_DETECT:
2044 0 : return "Loop detect";
2045 : /* 55-63 unused */
2046 5 : case LDB_ERR_NAMING_VIOLATION:
2047 5 : return "Naming violation";
2048 421 : case LDB_ERR_OBJECT_CLASS_VIOLATION:
2049 421 : return "Object class violation";
2050 18 : case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
2051 18 : return "Not allowed on non-leaf";
2052 2 : case LDB_ERR_NOT_ALLOWED_ON_RDN:
2053 2 : return "Not allowed on RDN";
2054 173 : case LDB_ERR_ENTRY_ALREADY_EXISTS:
2055 173 : return "Entry already exists";
2056 0 : case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
2057 0 : return "Object class mods prohibited";
2058 : /* 70 RESERVED FOR CLDAP */
2059 4 : case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
2060 4 : return "Affects multiple DSAs";
2061 : /* 72-79 unused */
2062 97 : case LDB_ERR_OTHER:
2063 97 : return "Other";
2064 : }
2065 :
2066 3 : return "Unknown error";
2067 : }
2068 :
2069 : /*
2070 : set backend specific opaque parameters
2071 : */
2072 291442348 : int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
2073 : {
2074 : struct ldb_opaque *o;
2075 :
2076 : /* allow updating an existing value */
2077 2613220348 : for (o=ldb->opaque;o;o=o->next) {
2078 2604806229 : if (strcmp(o->name, name) == 0) {
2079 283028229 : o->value = value;
2080 283028229 : return LDB_SUCCESS;
2081 : }
2082 : }
2083 :
2084 8414119 : o = talloc(ldb, struct ldb_opaque);
2085 8414119 : if (o == NULL) {
2086 0 : ldb_oom(ldb);
2087 0 : return LDB_ERR_OTHER;
2088 : }
2089 8414119 : o->next = ldb->opaque;
2090 8414119 : o->name = name;
2091 8414119 : o->value = value;
2092 8414119 : ldb->opaque = o;
2093 8414119 : return LDB_SUCCESS;
2094 : }
2095 :
2096 : /*
2097 : get a previously set opaque value
2098 : */
2099 783197823 : void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
2100 : {
2101 : struct ldb_opaque *o;
2102 11618316820 : for (o=ldb->opaque;o;o=o->next) {
2103 11471563306 : if (strcmp(o->name, name) == 0) {
2104 636444309 : return o->value;
2105 : }
2106 : }
2107 146753514 : return NULL;
2108 : }
2109 :
2110 0 : int ldb_global_init(void)
2111 : {
2112 : /* Provided for compatibility with some older versions of ldb */
2113 0 : return 0;
2114 : }
2115 :
2116 : /* return the ldb flags */
2117 42688 : unsigned int ldb_get_flags(struct ldb_context *ldb)
2118 : {
2119 42688 : return ldb->flags;
2120 : }
2121 :
2122 : /* set the ldb flags */
2123 41410 : void ldb_set_flags(struct ldb_context *ldb, unsigned flags)
2124 : {
2125 41410 : ldb->flags = flags;
2126 41410 : }
2127 :
2128 :
2129 : /*
2130 : set the location in a ldb request. Used for debugging
2131 : */
2132 390692554 : void ldb_req_set_location(struct ldb_request *req, const char *location)
2133 : {
2134 390692554 : if (req && req->handle) {
2135 390692554 : req->handle->location = location;
2136 : }
2137 390692554 : }
2138 :
2139 : /*
2140 : return the location set with dsdb_req_set_location
2141 : */
2142 0 : const char *ldb_req_location(struct ldb_request *req)
2143 : {
2144 0 : return req->handle->location;
2145 : }
2146 :
2147 : /**
2148 : mark a request as untrusted. This tells the rootdse module to remove
2149 : unregistered controls
2150 : */
2151 495725 : void ldb_req_mark_untrusted(struct ldb_request *req)
2152 : {
2153 495725 : req->handle->flags |= LDB_HANDLE_FLAG_UNTRUSTED;
2154 495725 : }
2155 :
2156 : /**
2157 : mark a request as trusted.
2158 : */
2159 844560 : void ldb_req_mark_trusted(struct ldb_request *req)
2160 : {
2161 844560 : req->handle->flags &= ~LDB_HANDLE_FLAG_UNTRUSTED;
2162 844560 : }
2163 :
2164 : /**
2165 : set custom flags. Those flags are set by applications using ldb,
2166 : they are application dependent and the same bit can have different
2167 : meaning in different application.
2168 : */
2169 0 : void ldb_req_set_custom_flags(struct ldb_request *req, uint32_t flags)
2170 : {
2171 0 : if (req != NULL && req->handle != NULL) {
2172 0 : req->handle->custom_flags = flags;
2173 : }
2174 0 : }
2175 :
2176 :
2177 : /**
2178 : get custom flags. Those flags are set by applications using ldb,
2179 : they are application dependent and the same bit can have different
2180 : meaning in different application.
2181 : */
2182 0 : uint32_t ldb_req_get_custom_flags(struct ldb_request *req)
2183 : {
2184 0 : if (req != NULL && req->handle != NULL) {
2185 0 : return req->handle->custom_flags;
2186 : }
2187 :
2188 : /*
2189 : * 0 is not something any better or worse than
2190 : * anything else as req or the handle is NULL
2191 : */
2192 0 : return 0;
2193 : }
2194 :
2195 :
2196 : /**
2197 : * return true if a request is untrusted
2198 : */
2199 65981909 : bool ldb_req_is_untrusted(struct ldb_request *req)
2200 : {
2201 65981909 : return (req->handle->flags & LDB_HANDLE_FLAG_UNTRUSTED) != 0;
2202 : }
|