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