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 709981 : static int ldb_context_destructor(void *ptr)
40 : {
41 709981 : struct ldb_context *ldb = talloc_get_type(ptr, struct ldb_context);
42 :
43 709981 : 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 709981 : 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 1048727550 : static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
59 : const char *fmt, va_list ap)
60 : {
61 1048727550 : struct ldb_context *ldb = talloc_get_type(context, struct ldb_context);
62 1048727550 : enum ldb_debug_level ldb_level = LDB_DEBUG_FATAL;
63 :
64 1048727550 : 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 1020396333 : case TEVENT_DEBUG_TRACE:
75 1020396333 : ldb_level = LDB_DEBUG_TRACE;
76 1020396333 : break;
77 28331217 : };
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 1048727550 : ldb_vdebug(ldb, ldb_level, fmt, ap);
83 1048727550 : }
84 :
85 : /*
86 : initialise a ldb context
87 : The mem_ctx is required
88 : The event_ctx is required
89 : */
90 749029 : struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
91 : {
92 13261 : struct ldb_context *ldb;
93 13261 : int ret;
94 749029 : const char *modules_path = getenv("LDB_MODULES_PATH");
95 :
96 749029 : if (modules_path == NULL) {
97 745113 : modules_path = LDB_MODULESDIR;
98 : }
99 :
100 749029 : ret = ldb_modules_load(modules_path, LDB_VERSION);
101 749029 : if (ret != LDB_SUCCESS) {
102 0 : return NULL;
103 : }
104 :
105 749029 : ldb = talloc_zero(mem_ctx, struct ldb_context);
106 749029 : 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 749029 : if (ev_ctx == NULL) {
114 318687 : ev_ctx = tevent_context_init(ldb);
115 318687 : if (ev_ctx == NULL) {
116 0 : talloc_free(ldb);
117 0 : return NULL;
118 : }
119 318687 : tevent_set_debug(ev_ctx, ldb_tevent_debug, ldb);
120 318687 : tevent_set_max_debug_level(ev_ctx, TEVENT_DEBUG_TRACE);
121 318687 : tevent_loop_allow_nesting(ev_ctx);
122 : }
123 :
124 749029 : ret = ldb_setup_wellknown_attributes(ldb);
125 749029 : if (ret != LDB_SUCCESS) {
126 0 : talloc_free(ldb);
127 0 : return NULL;
128 : }
129 :
130 749029 : ldb_set_utf8_default(ldb);
131 749029 : ldb_set_create_perms(ldb, 0666);
132 749029 : ldb_set_modules_dir(ldb, LDB_MODULESDIR);
133 749029 : ldb_set_event_context(ldb, ev_ctx);
134 749029 : ret = ldb_register_extended_match_rules(ldb);
135 749029 : 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 749029 : ldb->default_timeout = 300; /* set default to 5 minutes */
142 :
143 749029 : talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
144 :
145 749029 : 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 1147996 : void ldb_set_default_dns(struct ldb_context *ldb)
154 : {
155 20022 : TALLOC_CTX *tmp_ctx;
156 20022 : int ret;
157 20022 : struct ldb_result *res;
158 1147996 : struct ldb_dn *tmp_dn=NULL;
159 20022 : static const char *attrs[] = {
160 : "rootDomainNamingContext",
161 : "configurationNamingContext",
162 : "schemaNamingContext",
163 : "defaultNamingContext",
164 : NULL
165 : };
166 :
167 1147996 : tmp_ctx = talloc_new(ldb);
168 1147996 : ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
169 : LDB_SCOPE_BASE, attrs, "(objectClass=*)");
170 1147996 : if (ret != LDB_SUCCESS) {
171 318911 : talloc_free(tmp_ctx);
172 324961 : return;
173 : }
174 :
175 829085 : if (res->count != 1) {
176 6 : talloc_free(tmp_ctx);
177 6 : return;
178 : }
179 :
180 829079 : if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
181 427471 : tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
182 : "rootDomainNamingContext");
183 427471 : ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
184 : }
185 :
186 829079 : if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
187 427471 : tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
188 : "configurationNamingContext");
189 427471 : ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
190 : }
191 :
192 829079 : if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
193 427471 : tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
194 : "schemaNamingContext");
195 427471 : ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
196 : }
197 :
198 829079 : if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
199 427471 : tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
200 : "defaultNamingContext");
201 427471 : ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
202 : }
203 :
204 829079 : talloc_free(tmp_ctx);
205 : }
206 :
207 1752845 : struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
208 : {
209 1752845 : void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
210 1752845 : return talloc_get_type(opaque, struct ldb_dn);
211 : }
212 :
213 5318545 : struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
214 : {
215 5318545 : void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
216 5318545 : return talloc_get_type(opaque, struct ldb_dn);
217 : }
218 :
219 4075555 : struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
220 : {
221 4075555 : void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
222 4075555 : return talloc_get_type(opaque, struct ldb_dn);
223 : }
224 :
225 7514208 : struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
226 : {
227 7514208 : void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
228 7514208 : 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 748024 : int ldb_connect(struct ldb_context *ldb, const char *url,
242 : unsigned int flags, const char *options[])
243 : {
244 13102 : int ret;
245 13102 : char *url2;
246 : /* We seem to need to do this here, or else some utilities don't
247 : * get ldb backends */
248 :
249 748024 : ldb->flags = flags;
250 :
251 748024 : url2 = talloc_strdup(ldb, url);
252 748024 : if (!url2) {
253 0 : ldb_oom(ldb);
254 0 : return LDB_ERR_OPERATIONS_ERROR;
255 : }
256 748024 : ret = ldb_set_opaque(ldb, "ldb_url", url2);
257 748024 : if (ret != LDB_SUCCESS) {
258 0 : return ret;
259 : }
260 :
261 : /*
262 : * Take a copy of the options.
263 : */
264 748024 : ldb->options = ldb_options_copy(ldb, options);
265 748024 : if (ldb->options == NULL && options != NULL) {
266 0 : ldb_oom(ldb);
267 0 : return LDB_ERR_OPERATIONS_ERROR;
268 : }
269 :
270 748024 : ret = ldb_module_connect_backend(ldb, url, options, &ldb->modules);
271 748024 : if (ret != LDB_SUCCESS) {
272 1625 : return ret;
273 : }
274 :
275 746395 : ret = ldb_load_modules(ldb, options);
276 746395 : 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 746388 : ldb_set_default_dns(ldb);
285 :
286 746388 : return LDB_SUCCESS;
287 : }
288 :
289 1268451 : void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
290 : {
291 1268451 : ldb_asprintf_errstring(ldb, "%s", err_string);
292 1268451 : }
293 :
294 4849383 : void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
295 : {
296 480203 : va_list ap;
297 4849383 : char *old_err_string = NULL;
298 4849383 : if (ldb->err_string) {
299 303781 : old_err_string = ldb->err_string;
300 : }
301 :
302 4849383 : va_start(ap, format);
303 4849383 : ldb->err_string = talloc_vasprintf(ldb, format, ap);
304 4849383 : va_end(ap);
305 :
306 4849383 : TALLOC_FREE(old_err_string);
307 :
308 4849383 : 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 4849383 : }
313 :
314 59348656 : void ldb_reset_err_string(struct ldb_context *ldb)
315 : {
316 59348656 : TALLOC_FREE(ldb->err_string);
317 59348656 : }
318 :
319 :
320 :
321 : /*
322 : set an ldb error based on file:line
323 : */
324 474301 : int ldb_error_at(struct ldb_context *ldb, int ecode,
325 : const char *reason, const char *file, int line)
326 : {
327 474301 : if (reason == NULL) {
328 0 : reason = ldb_strerror(ecode);
329 : }
330 474301 : ldb_asprintf_errstring(ldb, "%s at %s:%d", reason, file, line);
331 474301 : 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 1927365 : int ldb_transaction_start(struct ldb_context *ldb)
359 : {
360 120263 : struct ldb_module *next_module;
361 120263 : int status;
362 :
363 1927365 : 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 1927365 : if (ldb->transaction_active) {
369 829188 : ldb->transaction_active++;
370 829188 : return LDB_SUCCESS;
371 : }
372 :
373 : /* start a new transaction */
374 1098177 : ldb->transaction_active++;
375 1098177 : ldb->prepare_commit_done = false;
376 :
377 2390119 : FIRST_OP(ldb, start_transaction);
378 :
379 1098177 : ldb_reset_err_string(ldb);
380 :
381 1098177 : status = next_module->ops->start_transaction(next_module);
382 1098177 : 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 1098155 : 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 1087402 : return status;
401 : }
402 :
403 : /*
404 : prepare for transaction commit (first phase of two phase commit)
405 : */
406 1793196 : int ldb_transaction_prepare_commit(struct ldb_context *ldb)
407 : {
408 119427 : struct ldb_module *next_module;
409 119427 : int status;
410 :
411 1793196 : if (ldb->prepare_commit_done) {
412 2703 : return LDB_SUCCESS;
413 : }
414 :
415 : /* commit only when all nested transactions are complete */
416 1790489 : if (ldb->transaction_active > 1) {
417 720251 : return LDB_SUCCESS;
418 : }
419 :
420 960773 : ldb->prepare_commit_done = true;
421 :
422 960773 : 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 8284930 : FIRST_OP_NOERR(ldb, prepare_commit);
431 960773 : if (next_module == NULL) {
432 153482 : return LDB_SUCCESS;
433 : }
434 :
435 807075 : ldb_reset_err_string(ldb);
436 :
437 807075 : status = next_module->ops->prepare_commit(next_module);
438 807075 : 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 797333 : return status;
458 : }
459 :
460 :
461 : /*
462 : commit a transaction
463 : */
464 1789148 : int ldb_transaction_commit(struct ldb_context *ldb)
465 : {
466 119423 : struct ldb_module *next_module;
467 119423 : int status;
468 :
469 1789148 : status = ldb_transaction_prepare_commit(ldb);
470 1789148 : if (status != LDB_SUCCESS) {
471 10 : return status;
472 : }
473 :
474 1789138 : ldb->transaction_active--;
475 :
476 1789138 : 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 1789138 : if (ldb->transaction_active > 0) {
482 718914 : return LDB_SUCCESS;
483 : }
484 :
485 960759 : 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 960759 : ldb_reset_err_string(ldb);
493 :
494 2148065 : FIRST_OP(ldb, end_transaction);
495 960759 : status = next_module->ops->end_transaction(next_module);
496 960759 : 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 950801 : return status;
510 : }
511 :
512 :
513 : /*
514 : cancel a transaction
515 : */
516 138174 : int ldb_transaction_cancel(struct ldb_context *ldb)
517 : {
518 838 : struct ldb_module *next_module;
519 838 : int status;
520 :
521 138174 : ldb->transaction_active--;
522 :
523 138174 : 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 138174 : if (ldb->transaction_active > 0) {
529 786 : return LDB_SUCCESS;
530 : }
531 :
532 137365 : 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 241955 : FIRST_OP(ldb, del_transaction);
540 :
541 137365 : status = next_module->ops->del_transaction(next_module);
542 137365 : 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 136550 : 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 419319 : static int ldb_autotransaction_request(struct ldb_context *ldb,
573 : struct ldb_request *req)
574 : {
575 11430 : int ret;
576 :
577 419319 : ret = ldb_transaction_start(ldb);
578 419319 : if (ret != LDB_SUCCESS) {
579 20 : return ret;
580 : }
581 :
582 419299 : ret = ldb_request(ldb, req);
583 419299 : if (ret == LDB_SUCCESS) {
584 419168 : ret = ldb_wait(req->handle, LDB_WAIT_ALL);
585 : }
586 :
587 419299 : if (ret == LDB_SUCCESS) {
588 375231 : return ldb_transaction_commit(ldb);
589 : }
590 44068 : ldb_transaction_cancel(ldb);
591 :
592 44068 : return ret;
593 : }
594 :
595 114236561 : int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
596 : {
597 3687920 : struct tevent_context *ev;
598 3687920 : int ret;
599 :
600 114236561 : if (handle == NULL) {
601 0 : return LDB_ERR_UNAVAILABLE;
602 : }
603 :
604 114236561 : if (handle->state == LDB_ASYNC_DONE) {
605 13945867 : 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 13945867 : return handle->status;
615 : }
616 :
617 100290694 : ev = ldb_handle_get_event_context(handle);
618 100290694 : if (NULL == ev) {
619 0 : return ldb_oom(handle->ldb);
620 : }
621 :
622 100290694 : switch (type) {
623 3471078 : case LDB_WAIT_NONE:
624 3471078 : ret = tevent_loop_once(ev);
625 3471078 : if (ret != 0) {
626 0 : return ldb_operr(handle->ldb);
627 : }
628 3471078 : if (handle->status == LDB_SUCCESS) {
629 3469712 : 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 93456115 : case LDB_WAIT_ALL:
645 306082200 : while (handle->state != LDB_ASYNC_DONE) {
646 213161547 : ret = tevent_loop_once(ev);
647 213161535 : if (ret != 0) {
648 0 : return ldb_operr(handle->ldb);
649 : }
650 213161535 : if (handle->status != LDB_SUCCESS) {
651 3898951 : if (handle->ldb->err_string != NULL) {
652 3419288 : 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 92920653 : if (handle->status == LDB_SUCCESS) {
668 90003893 : 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 82810830 : int ldb_set_timeout(struct ldb_context *ldb,
690 : struct ldb_request *req,
691 : int timeout)
692 : {
693 82810830 : if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
694 :
695 82810830 : if (timeout != 0) {
696 364911 : req->timeout = timeout;
697 : } else {
698 82445919 : req->timeout = ldb->default_timeout;
699 : }
700 82810830 : req->starttime = time(NULL);
701 :
702 82810830 : return LDB_SUCCESS;
703 : }
704 :
705 : /* calculates the new timeout based on the previous starttime and timeout */
706 598043789 : int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
707 : struct ldb_request *oldreq,
708 : struct ldb_request *newreq)
709 : {
710 598043789 : if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
711 :
712 598043789 : if (oldreq == NULL) {
713 69540458 : return ldb_set_timeout(ldb, newreq, 0);
714 : }
715 :
716 528503331 : newreq->starttime = oldreq->starttime;
717 528503331 : newreq->timeout = oldreq->timeout;
718 :
719 528503331 : return LDB_SUCCESS;
720 : }
721 :
722 :
723 80956760 : struct ldb_handle *ldb_handle_new(TALLOC_CTX *mem_ctx, struct ldb_context *ldb)
724 : {
725 3046991 : struct ldb_handle *h;
726 :
727 80956760 : h = talloc_zero(mem_ctx, struct ldb_handle);
728 80956760 : if (h == NULL) {
729 0 : ldb_set_errstring(ldb, "Out of Memory");
730 0 : return NULL;
731 : }
732 :
733 80956760 : h->status = LDB_SUCCESS;
734 80956760 : h->state = LDB_ASYNC_INIT;
735 80956760 : h->ldb = ldb;
736 80956760 : h->flags = 0;
737 80956760 : h->location = NULL;
738 80956760 : h->parent = NULL;
739 :
740 80956760 : if (h->ldb->require_private_event_context == true) {
741 80390288 : h->event_context = tevent_context_init(h);
742 80390288 : 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 80390288 : tevent_set_debug(h->event_context, ldb_tevent_debug, ldb);
749 80390288 : tevent_set_max_debug_level(h->event_context, TEVENT_DEBUG_TRACE);
750 80390288 : tevent_loop_allow_nesting(h->event_context);
751 : }
752 :
753 77909769 : return h;
754 : }
755 :
756 528503331 : static struct ldb_handle *ldb_handle_new_child(TALLOC_CTX *mem_ctx,
757 : struct ldb_request *parent_req)
758 : {
759 18154806 : struct ldb_handle *h;
760 :
761 528503331 : h = talloc_zero(mem_ctx, struct ldb_handle);
762 528503331 : if (h == NULL) {
763 0 : ldb_set_errstring(parent_req->handle->ldb,
764 : "Out of Memory");
765 0 : return NULL;
766 : }
767 :
768 528503331 : h->status = LDB_SUCCESS;
769 528503331 : h->state = LDB_ASYNC_INIT;
770 528503331 : h->ldb = parent_req->handle->ldb;
771 528503331 : h->parent = parent_req;
772 528503331 : h->nesting = parent_req->handle->nesting + 1;
773 528503331 : h->flags = parent_req->handle->flags;
774 528503331 : h->custom_flags = parent_req->handle->custom_flags;
775 528503331 : h->event_context = parent_req->handle->event_context;
776 :
777 528503331 : 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 1490361 : void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
785 : {
786 1490361 : ldb->create_perms = perms;
787 1490361 : }
788 :
789 2158651 : unsigned int ldb_get_create_perms(struct ldb_context *ldb)
790 : {
791 2158651 : return ldb->create_perms;
792 : }
793 :
794 1768137 : void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
795 : {
796 1768137 : ldb->ev_ctx = ev;
797 1768137 : }
798 :
799 273146218 : struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
800 : {
801 273146218 : return ldb->ev_ctx;
802 : }
803 :
804 208591645 : void ldb_request_set_state(struct ldb_request *req, int state)
805 : {
806 208591645 : req->handle->state = state;
807 208591645 : }
808 :
809 207921199 : int ldb_request_get_status(struct ldb_request *req)
810 : {
811 207921199 : 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 308316967 : struct tevent_context *ldb_handle_get_event_context(struct ldb_handle *handle)
820 : {
821 308316967 : if (handle->event_context != NULL) {
822 295252624 : return handle->event_context;
823 : }
824 4035086 : 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 2661049 : void ldb_set_require_private_event_context(struct ldb_context *ldb)
843 : {
844 2661049 : ldb->require_private_event_context = true;
845 2661049 : }
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 1519975 : static int ldb_msg_check_element_flags(struct ldb_context *ldb,
963 : const struct ldb_message *message)
964 : {
965 114232 : unsigned i;
966 8811700 : for (i=0; i<message->num_elements; i++) {
967 7291725 : 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 1405743 : 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 28880932 : static int ldb_db_lock_destructor(struct ldb_db_lock_context *lock_context)
994 : {
995 1304342 : int ret;
996 1304342 : struct ldb_module *next_module;
997 133233835 : FIRST_OP_NOERR(lock_context->ldb, read_unlock);
998 28880932 : if (next_module != NULL) {
999 28880932 : ret = next_module->ops->read_unlock(next_module);
1000 : } else {
1001 0 : ret = LDB_SUCCESS;
1002 : }
1003 :
1004 28880932 : 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 28880932 : return 0;
1012 : }
1013 :
1014 60219134 : static int ldb_lock_backend_callback(struct ldb_request *req,
1015 : struct ldb_reply *ares)
1016 : {
1017 2421780 : struct ldb_db_lock_context *lock_context;
1018 2421780 : int ret;
1019 :
1020 60219134 : 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 60219134 : lock_context = talloc_get_type(req->context,
1028 : struct ldb_db_lock_context);
1029 :
1030 60219134 : if (!ares) {
1031 0 : return ldb_module_done(lock_context->req, NULL, NULL,
1032 : LDB_ERR_OPERATIONS_ERROR);
1033 : }
1034 60219134 : if (ares->error != LDB_SUCCESS || ares->type == LDB_REPLY_DONE) {
1035 28880893 : 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 28880891 : TALLOC_FREE(req->context);
1042 28880891 : return ret;
1043 : }
1044 :
1045 : /* Otherwise pass on the callback */
1046 31338241 : switch (ares->type) {
1047 26554324 : case LDB_REPLY_ENTRY:
1048 26554324 : return ldb_module_send_entry(lock_context->req, ares->message,
1049 : ares->controls);
1050 :
1051 4783917 : case LDB_REPLY_REFERRAL:
1052 4783917 : 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 29216177 : 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 29216177 : struct ldb_module *next_module = NULL;
1068 29216177 : struct ldb_request *down_req = NULL;
1069 1304644 : struct ldb_db_lock_context *lock_context;
1070 29216177 : struct ldb_context *ldb = ldb_module_get_ctx(lock_module);
1071 1304644 : int ret;
1072 :
1073 29216177 : lock_context = talloc(req, struct ldb_db_lock_context);
1074 29216177 : if (lock_context == NULL) {
1075 0 : return ldb_oom(ldb);
1076 : }
1077 :
1078 29216177 : lock_context->ldb = ldb;
1079 29216177 : lock_context->req = req;
1080 :
1081 29216177 : 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 29216177 : LDB_REQ_SET_LOCATION(down_req);
1091 29216177 : if (ret != LDB_SUCCESS) {
1092 0 : return ret;
1093 : }
1094 :
1095 : /* call DB lock */
1096 133974476 : FIRST_OP_NOERR(ldb, read_lock);
1097 29216177 : if (next_module != NULL) {
1098 28880946 : ret = next_module->ops->read_lock(next_module);
1099 : } else {
1100 334929 : ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1101 : }
1102 :
1103 29215875 : if (ret == LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION) {
1104 : /* We might be talking LDAP */
1105 335231 : ldb_reset_err_string(ldb);
1106 335231 : TALLOC_FREE(lock_context);
1107 :
1108 335231 : return ldb_next_request(lock_module, req);
1109 28880946 : } 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 28880946 : talloc_set_destructor(lock_context, ldb_db_lock_destructor);
1115 : }
1116 :
1117 28880946 : if (ret != LDB_SUCCESS) {
1118 2 : return ret;
1119 : }
1120 :
1121 28880944 : 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 44295027 : int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
1130 : {
1131 1714652 : struct ldb_module *next_module;
1132 1714652 : int ret;
1133 :
1134 44295027 : 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 44295027 : ldb_reset_err_string(ldb);
1140 :
1141 44295027 : 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 44295027 : switch (req->operation) {
1147 29216321 : 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 1304644 : static const struct ldb_module_ops lock_module_ops = {
1154 : .name = "lock_searches",
1155 : .search = lock_search
1156 : };
1157 29216321 : struct ldb_module lock_module = {
1158 : .ldb = ldb,
1159 29216321 : .next = ldb->modules,
1160 : .ops = &lock_module_ops
1161 : };
1162 29216321 : next_module = &lock_module;
1163 :
1164 : /* due to "ldb_build_search_req" base DN always != NULL */
1165 29216321 : 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 29216177 : ret = next_module->ops->search(next_module, req);
1172 29216177 : break;
1173 : }
1174 924608 : case LDB_ADD:
1175 924608 : 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 1016836 : ret = ldb_msg_normalize(ldb, req, req->op.add.message,
1186 924588 : discard_const(&req->op.add.message));
1187 924588 : if (ret != LDB_SUCCESS) {
1188 0 : ldb_oom(ldb);
1189 0 : return ret;
1190 : }
1191 1529748 : FIRST_OP(ldb, add);
1192 924588 : ret = ldb_msg_check_element_flags(ldb, req->op.add.message);
1193 924588 : if (ret != LDB_SUCCESS) {
1194 : /*
1195 : * "ldb_msg_check_element_flags" generates an error
1196 : * string
1197 : */
1198 0 : return ret;
1199 : }
1200 924588 : ret = next_module->ops->add(next_module, req);
1201 924588 : break;
1202 595387 : case LDB_MODIFY:
1203 595387 : 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 1046256 : FIRST_OP(ldb, modify);
1209 595387 : ret = ldb_msg_check_element_flags(ldb, req->op.mod.message);
1210 595387 : if (ret != LDB_SUCCESS) {
1211 : /*
1212 : * "ldb_msg_check_element_flags" generates an error
1213 : * string
1214 : */
1215 0 : return ret;
1216 : }
1217 595387 : ret = next_module->ops->modify(next_module, req);
1218 595387 : break;
1219 283516 : case LDB_DELETE:
1220 283516 : 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 595496 : FIRST_OP(ldb, del);
1226 283512 : ret = next_module->ops->del(next_module, req);
1227 283512 : 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 1856909 : case LDB_EXTENDED:
1243 5651360 : FIRST_OP(ldb, extended);
1244 1856909 : ret = next_module->ops->extended(next_module, req);
1245 1856909 : break;
1246 11416302 : default:
1247 34626322 : FIRST_OP(ldb, request);
1248 11097525 : ret = next_module->ops->request(next_module, req);
1249 11097525 : break;
1250 : }
1251 :
1252 43976035 : 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 42267429 : return ret;
1259 : }
1260 :
1261 110776136 : int ldb_request_done(struct ldb_request *req, int status)
1262 : {
1263 110776136 : req->handle->state = LDB_ASYNC_DONE;
1264 110776136 : req->handle->status = status;
1265 110776136 : 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 213293143 : int ldb_search_default_callback(struct ldb_request *req,
1277 : struct ldb_reply *ares)
1278 : {
1279 5223976 : struct ldb_result *res;
1280 5223976 : unsigned int n;
1281 :
1282 213293143 : res = talloc_get_type(req->context, struct ldb_result);
1283 :
1284 213293143 : if (!ares) {
1285 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1286 : }
1287 213293143 : if (ares->error != LDB_SUCCESS) {
1288 2047021 : return ldb_request_done(req, ares->error);
1289 : }
1290 :
1291 211246122 : switch (ares->type) {
1292 147504821 : case LDB_REPLY_ENTRY:
1293 147504821 : res->msgs = talloc_realloc(res, res->msgs,
1294 : struct ldb_message *, res->count + 2);
1295 147504821 : if (! res->msgs) {
1296 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1297 : }
1298 :
1299 147504821 : res->msgs[res->count + 1] = NULL;
1300 :
1301 147504821 : res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
1302 147504821 : res->count++;
1303 147504821 : break;
1304 :
1305 5586291 : case LDB_REPLY_REFERRAL:
1306 5586291 : if (res->refs) {
1307 8706410 : for (n = 0; res->refs[n]; n++) /*noop*/ ;
1308 : } else {
1309 2050973 : n = 0;
1310 : }
1311 :
1312 5586291 : res->refs = talloc_realloc(res, res->refs, char *, n + 2);
1313 5586291 : if (! res->refs) {
1314 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1315 : }
1316 :
1317 5586291 : res->refs[n] = talloc_move(res->refs, &ares->referral);
1318 5586291 : res->refs[n + 1] = NULL;
1319 5586291 : break;
1320 :
1321 58155010 : case LDB_REPLY_DONE:
1322 : /* TODO: we should really support controls on entries
1323 : * and referrals too! */
1324 58155010 : 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 58155010 : talloc_free(ares);
1330 58155010 : return ldb_request_done(req, LDB_SUCCESS);
1331 : }
1332 :
1333 153091112 : talloc_free(ares);
1334 :
1335 153091112 : return LDB_SUCCESS;
1336 : }
1337 :
1338 2577543 : 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 2577543 : res = talloc_get_type(req->context, struct ldb_result);
1345 :
1346 2577543 : if (!ares) {
1347 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1348 : }
1349 :
1350 2577543 : if (ares->error != LDB_SUCCESS) {
1351 46988 : ret = ares->error;
1352 46988 : talloc_free(ares);
1353 46988 : return ldb_request_done(req, ret);
1354 : }
1355 :
1356 2530555 : 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 2530546 : case LDB_REPLY_DONE:
1374 2530546 : talloc_free(ares);
1375 2530546 : 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 12628111 : int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
1387 : {
1388 308182 : int ret;
1389 :
1390 12628111 : if (!ares) {
1391 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1392 : }
1393 :
1394 12628111 : if (ares->error != LDB_SUCCESS) {
1395 93103 : ret = ares->error;
1396 93103 : talloc_free(ares);
1397 93103 : return ldb_request_done(req, ret);
1398 : }
1399 :
1400 12535008 : 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 12534644 : talloc_free(ares);
1407 12534644 : return ldb_request_done(req, LDB_SUCCESS);
1408 : }
1409 :
1410 598043789 : 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 598043789 : struct ldb_request *req = NULL;
1418 :
1419 598043789 : req = talloc_zero(mem_ctx, struct ldb_request);
1420 598043789 : if (req == NULL) {
1421 0 : return NULL;
1422 : }
1423 598043789 : req->controls = controls;
1424 598043789 : req->context = context;
1425 598043789 : req->callback = callback;
1426 :
1427 598043789 : ldb_set_timeout_from_prev_req(ldb, parent, req);
1428 :
1429 598043789 : if (parent != NULL) {
1430 528503331 : req->handle = ldb_handle_new_child(req, parent);
1431 528503331 : if (req->handle == NULL) {
1432 0 : TALLOC_FREE(req);
1433 0 : return NULL;
1434 : }
1435 : } else {
1436 69540458 : req->handle = ldb_handle_new(req, ldb);
1437 69540458 : if (req->handle == NULL) {
1438 0 : TALLOC_FREE(req);
1439 0 : return NULL;
1440 : }
1441 : }
1442 :
1443 577040692 : return req;
1444 : }
1445 :
1446 554047188 : 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 18741741 : struct ldb_request *req;
1459 :
1460 554047188 : *ret_req = NULL;
1461 :
1462 554047188 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1463 : context, callback, parent);
1464 554047188 : if (req == NULL) {
1465 0 : ldb_oom(ldb);
1466 0 : return LDB_ERR_OPERATIONS_ERROR;
1467 : }
1468 :
1469 554047188 : req->operation = LDB_SEARCH;
1470 554047188 : if (base == NULL) {
1471 27197902 : req->op.search.base = ldb_dn_new(req, ldb, NULL);
1472 27197902 : if (req->op.search.base == NULL) {
1473 0 : ldb_oom(ldb);
1474 0 : return LDB_ERR_OPERATIONS_ERROR;
1475 : }
1476 : } else {
1477 526849286 : req->op.search.base = base;
1478 : }
1479 554047188 : req->op.search.scope = scope;
1480 :
1481 554047188 : req->op.search.tree = tree;
1482 554047188 : 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 554047188 : req->op.search.attrs = attrs;
1489 554047188 : *ret_req = req;
1490 554047188 : return LDB_SUCCESS;
1491 : }
1492 :
1493 70332825 : 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 2621177 : struct ldb_parse_tree *tree;
1506 2621177 : int ret;
1507 :
1508 70332825 : tree = ldb_parse_tree(mem_ctx, expression);
1509 70332825 : if (tree == NULL) {
1510 12 : ldb_set_errstring(ldb, "Unable to parse search expression");
1511 12 : return LDB_ERR_OPERATIONS_ERROR;
1512 : }
1513 :
1514 70332813 : ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
1515 : scope, tree, attrs, controls,
1516 : context, callback, parent);
1517 70332813 : if (ret == LDB_SUCCESS) {
1518 70332813 : talloc_steal(*ret_req, tree);
1519 : }
1520 67711639 : return ret;
1521 : }
1522 :
1523 6865324 : 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 6865324 : *ret_req = NULL;
1535 :
1536 6865324 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1537 : context, callback, parent);
1538 6865324 : if (req == NULL) {
1539 0 : ldb_set_errstring(ldb, "Out of Memory");
1540 0 : return LDB_ERR_OPERATIONS_ERROR;
1541 : }
1542 :
1543 6865324 : req->operation = LDB_ADD;
1544 6865324 : req->op.add.message = message;
1545 6865324 : *ret_req = req;
1546 6865324 : return LDB_SUCCESS;
1547 : }
1548 :
1549 7272621 : 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 7272621 : *ret_req = NULL;
1561 :
1562 7272621 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1563 : context, callback, parent);
1564 7272621 : if (req == NULL) {
1565 0 : ldb_set_errstring(ldb, "Out of Memory");
1566 0 : return LDB_ERR_OPERATIONS_ERROR;
1567 : }
1568 :
1569 7272621 : req->operation = LDB_MODIFY;
1570 7272621 : req->op.mod.message = message;
1571 :
1572 7272621 : *ret_req = req;
1573 7272621 : return LDB_SUCCESS;
1574 : }
1575 :
1576 633100 : 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 633100 : *ret_req = NULL;
1588 :
1589 633100 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1590 : context, callback, parent);
1591 633100 : if (req == NULL) {
1592 0 : ldb_set_errstring(ldb, "Out of Memory");
1593 0 : return LDB_ERR_OPERATIONS_ERROR;
1594 : }
1595 :
1596 633100 : req->operation = LDB_DELETE;
1597 633100 : req->op.del.dn = dn;
1598 633100 : *ret_req = req;
1599 633100 : return LDB_SUCCESS;
1600 : }
1601 :
1602 339380 : 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 339380 : *ret_req = NULL;
1615 :
1616 339380 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1617 : context, callback, parent);
1618 339380 : if (req == NULL) {
1619 0 : ldb_set_errstring(ldb, "Out of Memory");
1620 0 : return LDB_ERR_OPERATIONS_ERROR;
1621 : }
1622 :
1623 339380 : req->operation = LDB_RENAME;
1624 339380 : req->op.rename.olddn = olddn;
1625 339380 : req->op.rename.newdn = newdn;
1626 339380 : *ret_req = req;
1627 339380 : return LDB_SUCCESS;
1628 : }
1629 :
1630 28881182 : int ldb_extended_default_callback(struct ldb_request *req,
1631 : struct ldb_reply *ares)
1632 : {
1633 1250269 : struct ldb_result *res;
1634 :
1635 28881182 : res = talloc_get_type(req->context, struct ldb_result);
1636 :
1637 28881182 : if (!ares) {
1638 0 : return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1639 : }
1640 28881182 : if (ares->error != LDB_SUCCESS) {
1641 397 : return ldb_request_done(req, ares->error);
1642 : }
1643 :
1644 28880785 : if (ares->type == LDB_REPLY_DONE) {
1645 :
1646 : /* TODO: we should really support controls on entries and referrals too! */
1647 28880785 : res->extended = talloc_move(res, &ares->response);
1648 28880785 : res->controls = talloc_move(res, &ares->controls);
1649 :
1650 28880785 : talloc_free(ares);
1651 28880785 : 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 28886176 : 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 1250377 : struct ldb_request *req;
1670 :
1671 28886176 : *ret_req = NULL;
1672 :
1673 28886176 : req = ldb_build_req_common(mem_ctx, ldb, controls,
1674 : context, callback, parent);
1675 28886176 : if (req == NULL) {
1676 0 : ldb_set_errstring(ldb, "Out of Memory");
1677 0 : return LDB_ERR_OPERATIONS_ERROR;
1678 : }
1679 :
1680 28886176 : req->operation = LDB_EXTENDED;
1681 28886176 : req->op.extended.oid = oid;
1682 28886176 : req->op.extended.data = data;
1683 28886176 : *ret_req = req;
1684 28886176 : return LDB_SUCCESS;
1685 : }
1686 :
1687 1485299 : 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 1485299 : *_res = NULL;
1697 1485299 : req = NULL;
1698 :
1699 1485299 : res = talloc_zero(ldb, struct ldb_result);
1700 1485299 : if (!res) {
1701 0 : return LDB_ERR_OPERATIONS_ERROR;
1702 : }
1703 :
1704 1485299 : ret = ldb_build_extended_req(&req, ldb, ldb,
1705 : oid, data, NULL,
1706 : res, ldb_extended_default_callback,
1707 : NULL);
1708 1485299 : ldb_req_set_location(req, "ldb_extended");
1709 :
1710 1485299 : if (ret != LDB_SUCCESS) goto done;
1711 :
1712 1485299 : ldb_set_timeout(ldb, req, 0); /* use default timeout */
1713 :
1714 1485299 : ret = ldb_request(ldb, req);
1715 :
1716 1485299 : if (ret == LDB_SUCCESS) {
1717 1485293 : ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1718 : }
1719 :
1720 6 : done:
1721 1485299 : if (ret != LDB_SUCCESS) {
1722 397 : talloc_free(res);
1723 397 : res = NULL;
1724 : }
1725 :
1726 1485299 : talloc_free(req);
1727 :
1728 1485299 : *_res = res;
1729 1485299 : 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 7391936 : 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 255436 : struct ldb_request *req;
1742 255436 : struct ldb_result *res;
1743 255436 : char *expression;
1744 255436 : va_list ap;
1745 255436 : int ret;
1746 :
1747 7391936 : expression = NULL;
1748 7391936 : *result = NULL;
1749 7391936 : req = NULL;
1750 :
1751 7391936 : res = talloc_zero(mem_ctx, struct ldb_result);
1752 7391936 : if (!res) {
1753 0 : return LDB_ERR_OPERATIONS_ERROR;
1754 : }
1755 :
1756 7391936 : if (exp_fmt) {
1757 4764538 : va_start(ap, exp_fmt);
1758 4764538 : expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1759 4764538 : va_end(ap);
1760 :
1761 4764538 : if (!expression) {
1762 0 : talloc_free(res);
1763 0 : return LDB_ERR_OPERATIONS_ERROR;
1764 : }
1765 : }
1766 :
1767 9035310 : ret = ldb_build_search_req(&req, ldb, mem_ctx,
1768 1643374 : 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 7391936 : ldb_req_set_location(req, "ldb_search");
1777 :
1778 7391936 : if (ret != LDB_SUCCESS) goto done;
1779 :
1780 7391936 : ret = ldb_request(ldb, req);
1781 :
1782 7391936 : if (ret == LDB_SUCCESS) {
1783 7390954 : ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1784 : }
1785 :
1786 982 : done:
1787 7391936 : if (ret != LDB_SUCCESS) {
1788 321160 : talloc_free(res);
1789 321160 : res = NULL;
1790 : }
1791 :
1792 7391936 : talloc_free(expression);
1793 7391936 : talloc_free(req);
1794 :
1795 7391936 : *result = res;
1796 7391936 : 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 162433 : 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 162433 : ret = ldb_msg_sanity_check(ldb, message);
1810 162433 : if (ret != LDB_SUCCESS) {
1811 0 : return ret;
1812 : }
1813 :
1814 162433 : ret = ldb_build_add_req(&req, ldb, ldb,
1815 : message,
1816 : NULL,
1817 : NULL,
1818 : ldb_op_default_callback,
1819 : NULL);
1820 162433 : ldb_req_set_location(req, "ldb_add");
1821 :
1822 162433 : if (ret != LDB_SUCCESS) return ret;
1823 :
1824 : /* do request and autostart a transaction */
1825 162433 : ret = ldb_autotransaction_request(ldb, req);
1826 :
1827 162433 : talloc_free(req);
1828 162433 : return ret;
1829 : }
1830 :
1831 : /*
1832 : modify the specified attributes of a record
1833 : */
1834 170228 : 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 170228 : ret = ldb_msg_sanity_check(ldb, message);
1841 170228 : if (ret != LDB_SUCCESS) {
1842 0 : return ret;
1843 : }
1844 :
1845 170228 : ret = ldb_build_mod_req(&req, ldb, ldb,
1846 : message,
1847 : NULL,
1848 : NULL,
1849 : ldb_op_default_callback,
1850 : NULL);
1851 170228 : ldb_req_set_location(req, "ldb_modify");
1852 :
1853 170228 : if (ret != LDB_SUCCESS) return ret;
1854 :
1855 : /* do request and autostart a transaction */
1856 170228 : ret = ldb_autotransaction_request(ldb, req);
1857 :
1858 170228 : talloc_free(req);
1859 170228 : return ret;
1860 : }
1861 :
1862 :
1863 : /*
1864 : delete a record from the database
1865 : */
1866 86558 : int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1867 : {
1868 529 : struct ldb_request *req;
1869 529 : int ret;
1870 :
1871 86558 : ret = ldb_build_del_req(&req, ldb, ldb,
1872 : dn,
1873 : NULL,
1874 : NULL,
1875 : ldb_op_default_callback,
1876 : NULL);
1877 86558 : ldb_req_set_location(req, "ldb_delete");
1878 :
1879 86558 : if (ret != LDB_SUCCESS) return ret;
1880 :
1881 : /* do request and autostart a transaction */
1882 86558 : ret = ldb_autotransaction_request(ldb, req);
1883 :
1884 86558 : talloc_free(req);
1885 86558 : 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 1479367 : 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 1479367 : *seq_num = 0;
1929 :
1930 1479367 : tmp_ctx = talloc_zero(ldb, struct ldb_request);
1931 1479367 : if (tmp_ctx == NULL) {
1932 0 : ldb_set_errstring(ldb, "Out of Memory");
1933 0 : return LDB_ERR_OPERATIONS_ERROR;
1934 : }
1935 1479367 : seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1936 1479367 : 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 1479367 : seq->type = type;
1942 :
1943 1479367 : ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1944 1479367 : if (ret != LDB_SUCCESS) {
1945 315 : goto done;
1946 : }
1947 1479052 : talloc_steal(tmp_ctx, res);
1948 :
1949 1479052 : 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 1479052 : seqr = talloc_get_type(res->extended->data,
1955 : struct ldb_seqnum_result);
1956 1479052 : *seq_num = seqr->seq_num;
1957 :
1958 1479367 : done:
1959 1479367 : talloc_free(tmp_ctx);
1960 1479367 : return ret;
1961 : }
1962 :
1963 : /*
1964 : return extended error information
1965 : */
1966 494821 : const char *ldb_errstring(struct ldb_context *ldb)
1967 : {
1968 494821 : return ldb->err_string;
1969 : }
1970 :
1971 : /*
1972 : return a string explaining what a ldb error constant means
1973 : */
1974 771637 : const char *ldb_strerror(int ldb_err)
1975 : {
1976 771637 : switch (ldb_err) {
1977 702265 : case LDB_SUCCESS:
1978 702265 : return "Success";
1979 224 : case LDB_ERR_OPERATIONS_ERROR:
1980 224 : return "Operations error";
1981 5 : case LDB_ERR_PROTOCOL_ERROR:
1982 5 : return "Protocol error";
1983 9 : case LDB_ERR_TIME_LIMIT_EXCEEDED:
1984 9 : return "Time limit exceeded";
1985 0 : case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1986 0 : return "Size limit exceeded";
1987 0 : case LDB_ERR_COMPARE_FALSE:
1988 0 : return "Compare false";
1989 0 : case LDB_ERR_COMPARE_TRUE:
1990 0 : return "Compare true";
1991 0 : case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1992 0 : return "Auth method not supported";
1993 0 : case LDB_ERR_STRONG_AUTH_REQUIRED:
1994 0 : return "Strong auth required";
1995 : /* 9 RESERVED */
1996 8 : case LDB_ERR_REFERRAL:
1997 8 : return "Referral error";
1998 0 : case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1999 0 : return "Admin limit exceeded";
2000 10 : case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
2001 10 : return "Unsupported critical extension";
2002 0 : case LDB_ERR_CONFIDENTIALITY_REQUIRED:
2003 0 : return "Confidentiality required";
2004 0 : case LDB_ERR_SASL_BIND_IN_PROGRESS:
2005 0 : return "SASL bind in progress";
2006 577 : case LDB_ERR_NO_SUCH_ATTRIBUTE:
2007 577 : return "No such attribute";
2008 2 : case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
2009 2 : return "Undefined attribute type";
2010 0 : case LDB_ERR_INAPPROPRIATE_MATCHING:
2011 0 : return "Inappropriate matching";
2012 3993 : case LDB_ERR_CONSTRAINT_VIOLATION:
2013 3993 : return "Constraint violation";
2014 131 : case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
2015 131 : return "Attribute or value exists";
2016 68 : case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
2017 68 : return "Invalid attribute syntax";
2018 : /* 22-31 unused */
2019 50582 : case LDB_ERR_NO_SUCH_OBJECT:
2020 50582 : return "No such object";
2021 0 : case LDB_ERR_ALIAS_PROBLEM:
2022 0 : return "Alias problem";
2023 188 : case LDB_ERR_INVALID_DN_SYNTAX:
2024 188 : return "Invalid DN syntax";
2025 : /* 35 RESERVED */
2026 0 : case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
2027 0 : return "Alias dereferencing problem";
2028 : /* 37-47 unused */
2029 0 : case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
2030 0 : return "Inappropriate authentication";
2031 0 : case LDB_ERR_INVALID_CREDENTIALS:
2032 0 : return "Invalid credentials";
2033 1815 : case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
2034 1815 : return "insufficient access rights";
2035 0 : case LDB_ERR_BUSY:
2036 0 : return "Busy";
2037 0 : case LDB_ERR_UNAVAILABLE:
2038 0 : return "Unavailable";
2039 530 : case LDB_ERR_UNWILLING_TO_PERFORM:
2040 530 : return "Unwilling to perform";
2041 0 : case LDB_ERR_LOOP_DETECT:
2042 0 : return "Loop detect";
2043 : /* 55-63 unused */
2044 5 : case LDB_ERR_NAMING_VIOLATION:
2045 5 : return "Naming violation";
2046 421 : case LDB_ERR_OBJECT_CLASS_VIOLATION:
2047 421 : return "Object class violation";
2048 26 : case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
2049 26 : return "Not allowed on non-leaf";
2050 2 : case LDB_ERR_NOT_ALLOWED_ON_RDN:
2051 2 : return "Not allowed on RDN";
2052 292 : case LDB_ERR_ENTRY_ALREADY_EXISTS:
2053 292 : return "Entry already exists";
2054 0 : case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
2055 0 : return "Object class mods prohibited";
2056 : /* 70 RESERVED FOR CLDAP */
2057 4 : case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
2058 4 : return "Affects multiple DSAs";
2059 : /* 72-79 unused */
2060 97 : case LDB_ERR_OTHER:
2061 97 : return "Other";
2062 : }
2063 :
2064 3 : return "Unknown error";
2065 : }
2066 :
2067 : /*
2068 : set backend specific opaque parameters
2069 : */
2070 441707295 : int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
2071 : {
2072 22131908 : struct ldb_opaque *o;
2073 :
2074 : /* allow updating an existing value */
2075 3899993022 : for (o=ldb->opaque;o;o=o->next) {
2076 3889427821 : if (strcmp(o->name, name) == 0) {
2077 431142094 : o->value = value;
2078 431142094 : return LDB_SUCCESS;
2079 : }
2080 : }
2081 :
2082 10565201 : o = talloc(ldb, struct ldb_opaque);
2083 10565201 : if (o == NULL) {
2084 0 : ldb_oom(ldb);
2085 0 : return LDB_ERR_OTHER;
2086 : }
2087 10565201 : o->next = ldb->opaque;
2088 10565201 : o->name = name;
2089 10565201 : o->value = value;
2090 10565201 : ldb->opaque = o;
2091 10565201 : return LDB_SUCCESS;
2092 : }
2093 :
2094 : /*
2095 : get a previously set opaque value
2096 : */
2097 1180040635 : void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
2098 : {
2099 62668775 : struct ldb_opaque *o;
2100 17343116398 : for (o=ldb->opaque;o;o=o->next) {
2101 17126004329 : if (strcmp(o->name, name) == 0) {
2102 962928566 : return o->value;
2103 : }
2104 : }
2105 205669082 : return NULL;
2106 : }
2107 :
2108 0 : int ldb_global_init(void)
2109 : {
2110 : /* Provided for compatibility with some older versions of ldb */
2111 0 : return 0;
2112 : }
2113 :
2114 : /* return the ldb flags */
2115 87215 : unsigned int ldb_get_flags(struct ldb_context *ldb)
2116 : {
2117 87215 : return ldb->flags;
2118 : }
2119 :
2120 : /* set the ldb flags */
2121 53929 : void ldb_set_flags(struct ldb_context *ldb, unsigned flags)
2122 : {
2123 53929 : ldb->flags = flags;
2124 53929 : }
2125 :
2126 :
2127 : /*
2128 : set the location in a ldb request. Used for debugging
2129 : */
2130 572361705 : void ldb_req_set_location(struct ldb_request *req, const char *location)
2131 : {
2132 572361705 : if (req && req->handle) {
2133 572361705 : req->handle->location = location;
2134 : }
2135 572361705 : }
2136 :
2137 : /*
2138 : return the location set with dsdb_req_set_location
2139 : */
2140 0 : const char *ldb_req_location(struct ldb_request *req)
2141 : {
2142 0 : return req->handle->location;
2143 : }
2144 :
2145 : /**
2146 : mark a request as untrusted. This tells the rootdse module to remove
2147 : unregistered controls
2148 : */
2149 594789 : void ldb_req_mark_untrusted(struct ldb_request *req)
2150 : {
2151 594789 : req->handle->flags |= LDB_HANDLE_FLAG_UNTRUSTED;
2152 594789 : }
2153 :
2154 : /**
2155 : mark a request as trusted.
2156 : */
2157 1473133 : void ldb_req_mark_trusted(struct ldb_request *req)
2158 : {
2159 1473133 : req->handle->flags &= ~LDB_HANDLE_FLAG_UNTRUSTED;
2160 1473133 : }
2161 :
2162 : /**
2163 : set custom flags. Those flags are set by applications using ldb,
2164 : they are application dependent and the same bit can have different
2165 : meaning in different application.
2166 : */
2167 0 : void ldb_req_set_custom_flags(struct ldb_request *req, uint32_t flags)
2168 : {
2169 0 : if (req != NULL && req->handle != NULL) {
2170 0 : req->handle->custom_flags = flags;
2171 : }
2172 0 : }
2173 :
2174 :
2175 : /**
2176 : get custom flags. Those flags are set by applications using ldb,
2177 : they are application dependent and the same bit can have different
2178 : meaning in different application.
2179 : */
2180 0 : uint32_t ldb_req_get_custom_flags(struct ldb_request *req)
2181 : {
2182 0 : if (req != NULL && req->handle != NULL) {
2183 0 : return req->handle->custom_flags;
2184 : }
2185 :
2186 : /*
2187 : * 0 is not something any better or worse than
2188 : * anything else as req or the handle is NULL
2189 : */
2190 0 : return 0;
2191 : }
2192 :
2193 :
2194 : /**
2195 : * return true if a request is untrusted
2196 : */
2197 100735332 : bool ldb_req_is_untrusted(struct ldb_request *req)
2198 : {
2199 100735332 : return (req->handle->flags & LDB_HANDLE_FLAG_UNTRUSTED) != 0;
2200 : }
|