r22474: If ldb does not return sucess, then the res variable may not be valid.
[Samba.git] / source / lib / ldb / common / ldb.c
blobc317fe57faff77316143a32d1db34c98135b2df7
1 /*
2 ldb database library
4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Simo Sorce 2005-2006
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
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 2 of the License, or (at your option) any later version.
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.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Name: ldb
29 * Component: ldb core API
31 * Description: core API routines interfacing to ldb backends
33 * Author: Andrew Tridgell
36 #include "includes.h"
37 #include "ldb/include/includes.h"
39 /*
40 initialise a ldb context
41 The mem_ctx is optional
43 struct ldb_context *ldb_init(void *mem_ctx)
45 struct ldb_context *ldb = talloc_zero(mem_ctx, struct ldb_context);
46 int ret;
48 ret = ldb_setup_wellknown_attributes(ldb);
49 if (ret != 0) {
50 talloc_free(ldb);
51 return NULL;
54 ldb_set_utf8_default(ldb);
55 ldb_set_create_perms(ldb, 0666);
57 return ldb;
60 static struct ldb_backend {
61 const char *name;
62 ldb_connect_fn connect_fn;
63 struct ldb_backend *prev, *next;
64 } *ldb_backends = NULL;
67 static ldb_connect_fn ldb_find_backend(const char *url)
69 struct ldb_backend *backend;
71 for (backend = ldb_backends; backend; backend = backend->next) {
72 if (strncmp(backend->name, url, strlen(backend->name)) == 0) {
73 return backend->connect_fn;
77 return NULL;
81 register a new ldb backend
83 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
85 struct ldb_backend *backend = talloc(talloc_autofree_context(), struct ldb_backend);
87 if (ldb_find_backend(url_prefix)) {
88 return LDB_SUCCESS;
91 /* Maybe check for duplicity here later on? */
93 backend->name = talloc_strdup(backend, url_prefix);
94 backend->connect_fn = connectfn;
95 DLIST_ADD(ldb_backends, backend);
97 return LDB_SUCCESS;
101 Return the ldb module form of a database. The URL can either be one of the following forms
102 ldb://path
103 ldapi://path
105 flags is made up of LDB_FLG_*
107 the options are passed uninterpreted to the backend, and are
108 backend specific.
110 This allows modules to get at only the backend module, for example where a module
111 may wish to direct certain requests at a particular backend.
113 int ldb_connect_backend(struct ldb_context *ldb, const char *url, const char *options[],
114 struct ldb_module **backend_module)
116 int ret;
117 char *backend;
118 ldb_connect_fn fn;
120 if (strchr(url, ':') != NULL) {
121 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
122 } else {
123 /* Default to tdb */
124 backend = talloc_strdup(ldb, "tdb");
127 fn = ldb_find_backend(backend);
129 if (fn == NULL) {
130 if (ldb_try_load_dso(ldb, backend) == 0) {
131 fn = ldb_find_backend(backend);
135 talloc_free(backend);
137 if (fn == NULL) {
138 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
139 return LDB_ERR_OTHER;
142 ret = fn(ldb, url, ldb->flags, options, backend_module);
144 if (ret != LDB_SUCCESS) {
145 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
146 return ret;
148 return ret;
152 try to autodetect a basedn if none specified. This fixes one of my
153 pet hates about ldapsearch, which is that you have to get a long,
154 complex basedn right to make any use of it.
156 void ldb_set_default_dns(struct ldb_context *ldb)
158 TALLOC_CTX *tmp_ctx;
159 int ret;
160 struct ldb_result *res;
161 struct ldb_dn *tmp_dn=NULL;
162 static const char *attrs[] = {
163 "rootDomainNamingContext",
164 "configurationNamingContext",
165 "schemaNamingContext",
166 "defaultNamingContext",
167 NULL
170 tmp_ctx = talloc_new(ldb);
171 ret = ldb_search(ldb, ldb_dn_new(tmp_ctx, ldb, NULL), LDB_SCOPE_BASE,
172 "(objectClass=*)", attrs, &res);
173 if (ret == LDB_SUCCESS) {
174 if (res->count == 1) {
175 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
176 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "rootDomainNamingContext");
177 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
180 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
181 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "configurationNamingContext");
182 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
185 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
186 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "schemaNamingContext");
187 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
190 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
191 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "defaultNamingContext");
192 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
195 talloc_free(res);
198 talloc_free(tmp_ctx);
201 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
203 return talloc_get_type(ldb_get_opaque(ldb, "rootDomainNamingContext"), struct ldb_dn);
206 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
208 return talloc_get_type(ldb_get_opaque(ldb, "configurationNamingContext"), struct ldb_dn);
211 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
213 return talloc_get_type(ldb_get_opaque(ldb, "schemaNamingContext"), struct ldb_dn);
216 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
218 return talloc_get_type(ldb_get_opaque(ldb, "defaultNamingContext"), struct ldb_dn);
222 connect to a database. The URL can either be one of the following forms
223 ldb://path
224 ldapi://path
226 flags is made up of LDB_FLG_*
228 the options are passed uninterpreted to the backend, and are
229 backend specific
231 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
233 int ret;
235 /* We seem to need to do this here, or else some utilities don't get ldb backends */
236 ldb_global_init();
238 ldb->flags = flags;
240 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
241 if (ret != LDB_SUCCESS) {
242 return ret;
245 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
246 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for %s: %s\n",
247 url, ldb_errstring(ldb));
248 return LDB_ERR_OTHER;
251 /* TODO: get timeout from options if available there */
252 ldb->default_timeout = 300; /* set default to 5 minutes */
254 /* set the default base dn */
255 ldb_set_default_dns(ldb);
257 return LDB_SUCCESS;
260 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
262 if (ldb->err_string) {
263 talloc_free(ldb->err_string);
265 ldb->err_string = talloc_strdup(ldb, err_string);
268 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
270 va_list ap;
272 if (ldb->err_string) {
273 talloc_free(ldb->err_string);
276 va_start(ap, format);
277 ldb->err_string = talloc_vasprintf(ldb, format, ap);
278 va_end(ap);
281 void ldb_reset_err_string(struct ldb_context *ldb)
283 if (ldb->err_string) {
284 talloc_free(ldb->err_string);
285 ldb->err_string = NULL;
289 #define FIRST_OP(ldb, op) do { \
290 module = ldb->modules; \
291 while (module && module->ops->op == NULL) module = module->next; \
292 if (module == NULL) { \
293 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
294 return LDB_ERR_OPERATIONS_ERROR; \
296 } while (0)
299 start a transaction
301 static int ldb_transaction_start_internal(struct ldb_context *ldb)
303 struct ldb_module *module;
304 int status;
305 FIRST_OP(ldb, start_transaction);
307 ldb_reset_err_string(ldb);
309 status = module->ops->start_transaction(module);
310 if (status != LDB_SUCCESS) {
311 if (ldb->err_string == NULL) {
312 /* no error string was setup by the backend */
313 ldb_asprintf_errstring(ldb,
314 "ldb transaction start: %s (%d)",
315 ldb_strerror(status),
316 status);
319 return status;
323 commit a transaction
325 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
327 struct ldb_module *module;
328 int status;
329 FIRST_OP(ldb, end_transaction);
331 ldb_reset_err_string(ldb);
333 status = module->ops->end_transaction(module);
334 if (status != LDB_SUCCESS) {
335 if (ldb->err_string == NULL) {
336 /* no error string was setup by the backend */
337 ldb_asprintf_errstring(ldb,
338 "ldb transaction commit: %s (%d)",
339 ldb_strerror(status),
340 status);
343 return status;
347 cancel a transaction
349 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
351 struct ldb_module *module;
352 int status;
353 FIRST_OP(ldb, del_transaction);
355 status = module->ops->del_transaction(module);
356 if (status != LDB_SUCCESS) {
357 if (ldb->err_string == NULL) {
358 /* no error string was setup by the backend */
359 ldb_asprintf_errstring(ldb,
360 "ldb transaction cancel: %s (%d)",
361 ldb_strerror(status),
362 status);
365 return status;
368 int ldb_transaction_start(struct ldb_context *ldb)
370 /* disable autotransactions */
371 ldb->transaction_active++;
373 return ldb_transaction_start_internal(ldb);
376 int ldb_transaction_commit(struct ldb_context *ldb)
378 /* renable autotransactions (when we reach 0) */
379 if (ldb->transaction_active > 0)
380 ldb->transaction_active--;
382 return ldb_transaction_commit_internal(ldb);
385 int ldb_transaction_cancel(struct ldb_context *ldb)
387 /* renable autotransactions (when we reach 0) */
388 if (ldb->transaction_active > 0)
389 ldb->transaction_active--;
391 return ldb_transaction_cancel_internal(ldb);
394 static int ldb_autotransaction_start(struct ldb_context *ldb)
396 /* explicit transaction active, ignore autotransaction request */
397 if (ldb->transaction_active)
398 return LDB_SUCCESS;
400 return ldb_transaction_start_internal(ldb);
403 static int ldb_autotransaction_commit(struct ldb_context *ldb)
405 /* explicit transaction active, ignore autotransaction request */
406 if (ldb->transaction_active)
407 return LDB_SUCCESS;
409 return ldb_transaction_commit_internal(ldb);
412 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
414 /* explicit transaction active, ignore autotransaction request */
415 if (ldb->transaction_active)
416 return LDB_SUCCESS;
418 return ldb_transaction_cancel_internal(ldb);
421 /* autostarts a transacion if none active */
422 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
424 int ret;
426 ret = ldb_autotransaction_start(ldb);
427 if (ret != LDB_SUCCESS) {
428 return ret;
431 ret = ldb_request(ldb, req);
432 if (ret == LDB_SUCCESS) {
433 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
436 if (ret == LDB_SUCCESS) {
437 return ldb_autotransaction_commit(ldb);
439 ldb_autotransaction_cancel(ldb);
441 if (ldb->err_string == NULL) {
442 /* no error string was setup by the backend */
443 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
446 return ret;
449 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
451 if (!handle) {
452 return LDB_SUCCESS;
455 return handle->module->ops->wait(handle, type);
458 /* set the specified timeout or, if timeout is 0 set the default timeout */
459 /* timeout == -1 means no timeout */
460 int ldb_set_timeout(struct ldb_context *ldb, struct ldb_request *req, int timeout)
462 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
464 if (timeout != 0) {
465 req->timeout = timeout;
466 } else {
467 req->timeout = ldb->default_timeout;
469 req->starttime = time(NULL);
471 return LDB_SUCCESS;
474 /* calculates the new timeout based on the previous starttime and timeout */
475 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb, struct ldb_request *oldreq, struct ldb_request *newreq)
477 time_t now;
479 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
481 now = time(NULL);
483 if (oldreq == NULL)
484 return ldb_set_timeout(ldb, newreq, 0);
486 if ((now - oldreq->starttime) > oldreq->timeout) {
487 return LDB_ERR_TIME_LIMIT_EXCEEDED;
489 newreq->starttime = oldreq->starttime;
490 newreq->timeout = oldreq->timeout - (now - oldreq->starttime);
492 return LDB_SUCCESS;
497 set the permissions for new files to be passed to open() in
498 backends that use local files
500 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
502 ldb->create_perms = perms;
506 start an ldb request
507 NOTE: the request must be a talloc context.
508 returns LDB_ERR_* on errors.
510 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
512 struct ldb_module *module;
513 int ret;
515 ldb_reset_err_string(ldb);
517 /* call the first module in the chain */
518 switch (req->operation) {
519 case LDB_SEARCH:
520 FIRST_OP(ldb, search);
521 ret = module->ops->search(module, req);
522 break;
523 case LDB_ADD:
524 FIRST_OP(ldb, add);
525 ret = module->ops->add(module, req);
526 break;
527 case LDB_MODIFY:
528 FIRST_OP(ldb, modify);
529 ret = module->ops->modify(module, req);
530 break;
531 case LDB_DELETE:
532 FIRST_OP(ldb, del);
533 ret = module->ops->del(module, req);
534 break;
535 case LDB_RENAME:
536 FIRST_OP(ldb, rename);
537 ret = module->ops->rename(module, req);
538 break;
539 case LDB_EXTENDED:
540 FIRST_OP(ldb, extended);
541 ret = module->ops->extended(module, req);
542 break;
543 case LDB_SEQUENCE_NUMBER:
544 FIRST_OP(ldb, sequence_number);
545 ret = module->ops->sequence_number(module, req);
546 break;
547 default:
548 FIRST_OP(ldb, request);
549 ret = module->ops->request(module, req);
550 break;
553 return ret;
557 search the database given a LDAP-like search expression
559 returns an LDB error code
561 Use talloc_free to free the ldb_message returned in 'res', if successful
564 int ldb_search_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
566 struct ldb_result *res;
567 int n;
569 if (!context) {
570 ldb_set_errstring(ldb, "NULL Context in callback");
571 return LDB_ERR_OPERATIONS_ERROR;
574 res = talloc_get_type(context, struct ldb_result);
576 if (!res || !ares) {
577 ldb_set_errstring(ldb, "NULL res or ares in callback");
578 goto error;
581 switch (ares->type) {
582 case LDB_REPLY_ENTRY:
583 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
584 if (! res->msgs) {
585 goto error;
588 res->msgs[res->count + 1] = NULL;
590 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
591 res->count++;
592 break;
593 case LDB_REPLY_REFERRAL:
594 if (res->refs) {
595 for (n = 0; res->refs[n]; n++) /*noop*/ ;
596 } else {
597 n = 0;
600 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
601 if (! res->refs) {
602 goto error;
605 res->refs[n] = talloc_move(res->refs, &ares->referral);
606 res->refs[n + 1] = NULL;
607 break;
608 case LDB_REPLY_EXTENDED:
609 case LDB_REPLY_DONE:
610 /* TODO: we should really support controls on entries and referrals too! */
611 res->controls = talloc_move(res, &ares->controls);
612 break;
614 talloc_free(ares);
615 return LDB_SUCCESS;
617 error:
618 talloc_free(ares);
619 return LDB_ERR_OPERATIONS_ERROR;
622 int ldb_build_search_req(struct ldb_request **ret_req,
623 struct ldb_context *ldb,
624 void *mem_ctx,
625 struct ldb_dn *base,
626 enum ldb_scope scope,
627 const char *expression,
628 const char * const *attrs,
629 struct ldb_control **controls,
630 void *context,
631 ldb_request_callback_t callback)
633 struct ldb_request *req;
635 *ret_req = NULL;
637 req = talloc(mem_ctx, struct ldb_request);
638 if (req == NULL) {
639 ldb_set_errstring(ldb, "Out of Memory");
640 return LDB_ERR_OPERATIONS_ERROR;
643 req->operation = LDB_SEARCH;
644 if (base == NULL) {
645 req->op.search.base = ldb_dn_new(req, ldb, NULL);
646 } else {
647 req->op.search.base = base;
649 req->op.search.scope = scope;
651 req->op.search.tree = ldb_parse_tree(req, expression);
652 if (req->op.search.tree == NULL) {
653 ldb_set_errstring(ldb, "Unable to parse search expression");
654 talloc_free(req);
655 return LDB_ERR_OPERATIONS_ERROR;
658 req->op.search.attrs = attrs;
659 req->controls = controls;
660 req->context = context;
661 req->callback = callback;
663 *ret_req = req;
664 return LDB_SUCCESS;
667 int ldb_build_add_req(struct ldb_request **ret_req,
668 struct ldb_context *ldb,
669 void *mem_ctx,
670 const struct ldb_message *message,
671 struct ldb_control **controls,
672 void *context,
673 ldb_request_callback_t callback)
675 struct ldb_request *req;
677 *ret_req = NULL;
679 req = talloc(mem_ctx, struct ldb_request);
680 if (req == NULL) {
681 ldb_set_errstring(ldb, "Out of Memory");
682 return LDB_ERR_OPERATIONS_ERROR;
685 req->operation = LDB_ADD;
686 req->op.add.message = message;
687 req->controls = controls;
688 req->context = context;
689 req->callback = callback;
691 *ret_req = req;
693 return LDB_SUCCESS;
696 int ldb_build_mod_req(struct ldb_request **ret_req,
697 struct ldb_context *ldb,
698 void *mem_ctx,
699 const struct ldb_message *message,
700 struct ldb_control **controls,
701 void *context,
702 ldb_request_callback_t callback)
704 struct ldb_request *req;
706 *ret_req = NULL;
708 req = talloc(mem_ctx, struct ldb_request);
709 if (req == NULL) {
710 ldb_set_errstring(ldb, "Out of Memory");
711 return LDB_ERR_OPERATIONS_ERROR;
714 req->operation = LDB_MODIFY;
715 req->op.mod.message = message;
716 req->controls = controls;
717 req->context = context;
718 req->callback = callback;
720 *ret_req = req;
722 return LDB_SUCCESS;
725 int ldb_build_del_req(struct ldb_request **ret_req,
726 struct ldb_context *ldb,
727 void *mem_ctx,
728 struct ldb_dn *dn,
729 struct ldb_control **controls,
730 void *context,
731 ldb_request_callback_t callback)
733 struct ldb_request *req;
735 *ret_req = NULL;
737 req = talloc(mem_ctx, struct ldb_request);
738 if (req == NULL) {
739 ldb_set_errstring(ldb, "Out of Memory");
740 return LDB_ERR_OPERATIONS_ERROR;
743 req->operation = LDB_DELETE;
744 req->op.del.dn = dn;
745 req->controls = controls;
746 req->context = context;
747 req->callback = callback;
749 *ret_req = req;
751 return LDB_SUCCESS;
754 int ldb_build_rename_req(struct ldb_request **ret_req,
755 struct ldb_context *ldb,
756 void *mem_ctx,
757 struct ldb_dn *olddn,
758 struct ldb_dn *newdn,
759 struct ldb_control **controls,
760 void *context,
761 ldb_request_callback_t callback)
763 struct ldb_request *req;
765 *ret_req = NULL;
767 req = talloc(mem_ctx, struct ldb_request);
768 if (req == NULL) {
769 ldb_set_errstring(ldb, "Out of Memory");
770 return LDB_ERR_OPERATIONS_ERROR;
773 req->operation = LDB_RENAME;
774 req->op.rename.olddn = olddn;
775 req->op.rename.newdn = newdn;
776 req->controls = controls;
777 req->context = context;
778 req->callback = callback;
780 *ret_req = req;
782 return LDB_SUCCESS;
785 int ldb_extended_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
787 struct ldb_result *res;
789 if (!context) {
790 ldb_set_errstring(ldb, "NULL Context in callback");
791 return LDB_ERR_OPERATIONS_ERROR;
794 res = talloc_get_type(context, struct ldb_result);
795 if (!res || !ares) {
796 ldb_set_errstring(ldb, "NULL res or ares in callback");
797 goto error;
800 switch (ares->type) {
801 case LDB_REPLY_ENTRY:
802 case LDB_REPLY_REFERRAL:
803 case LDB_REPLY_DONE:
804 ldb_set_errstring(ldb, "invalid ares type in callback");
805 goto error;
806 case LDB_REPLY_EXTENDED:
807 /* TODO: we should really support controls on entries and referrals too! */
808 res->extended = talloc_move(res, &ares->response);
809 res->controls = talloc_move(res, &ares->controls);
810 break;
812 talloc_free(ares);
813 return LDB_SUCCESS;
815 error:
816 talloc_free(ares);
817 return LDB_ERR_OPERATIONS_ERROR;
820 int ldb_build_extended_req(struct ldb_request **ret_req,
821 struct ldb_context *ldb,
822 void *mem_ctx,
823 const char *oid,
824 void *data,
825 struct ldb_control **controls,
826 void *context,
827 ldb_request_callback_t callback)
829 struct ldb_request *req;
831 *ret_req = NULL;
833 req = talloc(mem_ctx, struct ldb_request);
834 if (req == NULL) {
835 ldb_set_errstring(ldb, "Out of Memory");
836 return LDB_ERR_OPERATIONS_ERROR;
839 req->operation = LDB_EXTENDED;
840 req->op.extended.oid = oid;
841 req->op.extended.data = data;
842 req->controls = controls;
843 req->context = context;
844 req->callback = callback;
846 *ret_req = req;
848 return LDB_SUCCESS;
851 int ldb_extended(struct ldb_context *ldb,
852 const char *oid,
853 void *data,
854 struct ldb_result **_res)
856 struct ldb_request *req;
857 int ret;
858 struct ldb_result *res;
860 *_res = NULL;
862 res = talloc_zero(ldb, struct ldb_result);
863 if (!res) {
864 return LDB_ERR_OPERATIONS_ERROR;
867 ret = ldb_build_extended_req(&req, ldb, ldb,
868 oid, data, NULL,
869 res, ldb_extended_default_callback);
870 if (ret != LDB_SUCCESS) goto done;
872 ldb_set_timeout(ldb, req, 0); /* use default timeout */
874 ret = ldb_request(ldb, req);
876 if (ret == LDB_SUCCESS) {
877 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
880 talloc_free(req);
882 done:
883 if (ret != LDB_SUCCESS) {
884 talloc_free(res);
887 *_res = res;
888 return ret;
892 note that ldb_search() will automatically replace a NULL 'base' value with the
893 defaultNamingContext from the rootDSE if available.
895 int ldb_search(struct ldb_context *ldb,
896 struct ldb_dn *base,
897 enum ldb_scope scope,
898 const char *expression,
899 const char * const *attrs,
900 struct ldb_result **_res)
902 struct ldb_request *req;
903 int ret;
904 struct ldb_result *res;
906 *_res = NULL;
908 res = talloc_zero(ldb, struct ldb_result);
909 if (!res) {
910 return LDB_ERR_OPERATIONS_ERROR;
913 ret = ldb_build_search_req(&req, ldb, ldb,
914 base?base:ldb_get_default_basedn(ldb),
915 scope,
916 expression,
917 attrs,
918 NULL,
919 res,
920 ldb_search_default_callback);
922 if (ret != LDB_SUCCESS) goto done;
924 ldb_set_timeout(ldb, req, 0); /* use default timeout */
926 ret = ldb_request(ldb, req);
928 if (ret == LDB_SUCCESS) {
929 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
932 talloc_free(req);
934 done:
935 if (ret != LDB_SUCCESS) {
936 talloc_free(res);
939 *_res = res;
940 return ret;
944 a useful search function where you can easily define the expression and that
945 takes a memory context where results are allocated
948 int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_result **result,
949 struct ldb_dn *base, enum ldb_scope scope, const char * const *attrs,
950 const char *exp_fmt, ...)
952 struct ldb_result *res;
953 char *expression;
954 va_list ap;
955 int ret;
957 res = NULL;
958 *result = NULL;
960 va_start(ap, exp_fmt);
961 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
962 va_end(ap);
964 if ( ! expression) {
965 return LDB_ERR_OPERATIONS_ERROR;
968 ret = ldb_search(ldb, base, scope, expression, attrs, &res);
970 if (ret == LDB_SUCCESS) {
971 talloc_steal(mem_ctx, res);
972 *result = res;
975 talloc_free(expression);
977 return ret;
981 add a record to the database. Will fail if a record with the given class and key
982 already exists
984 int ldb_add(struct ldb_context *ldb,
985 const struct ldb_message *message)
987 struct ldb_request *req;
988 int ret;
990 ret = ldb_msg_sanity_check(ldb, message);
991 if (ret != LDB_SUCCESS) {
992 return ret;
995 ret = ldb_build_add_req(&req, ldb, ldb,
996 message,
997 NULL,
998 NULL,
999 NULL);
1001 if (ret != LDB_SUCCESS) return ret;
1003 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1005 /* do request and autostart a transaction */
1006 ret = ldb_autotransaction_request(ldb, req);
1008 talloc_free(req);
1009 return ret;
1013 modify the specified attributes of a record
1015 int ldb_modify(struct ldb_context *ldb,
1016 const struct ldb_message *message)
1018 struct ldb_request *req;
1019 int ret;
1021 ret = ldb_msg_sanity_check(ldb, message);
1022 if (ret != LDB_SUCCESS) {
1023 return ret;
1026 ret = ldb_build_mod_req(&req, ldb, ldb,
1027 message,
1028 NULL,
1029 NULL,
1030 NULL);
1032 if (ret != LDB_SUCCESS) return ret;
1034 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1036 /* do request and autostart a transaction */
1037 ret = ldb_autotransaction_request(ldb, req);
1039 talloc_free(req);
1040 return ret;
1045 delete a record from the database
1047 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1049 struct ldb_request *req;
1050 int ret;
1052 ret = ldb_build_del_req(&req, ldb, ldb,
1054 NULL,
1055 NULL,
1056 NULL);
1058 if (ret != LDB_SUCCESS) return ret;
1060 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1062 /* do request and autostart a transaction */
1063 ret = ldb_autotransaction_request(ldb, req);
1065 talloc_free(req);
1066 return ret;
1070 rename a record in the database
1072 int ldb_rename(struct ldb_context *ldb, struct ldb_dn *olddn, struct ldb_dn *newdn)
1074 struct ldb_request *req;
1075 int ret;
1077 ret = ldb_build_rename_req(&req, ldb, ldb,
1078 olddn,
1079 newdn,
1080 NULL,
1081 NULL,
1082 NULL);
1084 if (ret != LDB_SUCCESS) return ret;
1086 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1088 /* do request and autostart a transaction */
1089 ret = ldb_autotransaction_request(ldb, req);
1091 talloc_free(req);
1092 return ret;
1097 return the global sequence number
1099 int ldb_sequence_number(struct ldb_context *ldb, enum ldb_sequence_type type, uint64_t *seq_num)
1101 struct ldb_request *req;
1102 int ret;
1104 req = talloc(ldb, struct ldb_request);
1105 if (req == NULL) {
1106 ldb_set_errstring(ldb, "Out of Memory");
1107 return LDB_ERR_OPERATIONS_ERROR;
1110 req->operation = LDB_SEQUENCE_NUMBER;
1111 req->controls = NULL;
1112 req->context = NULL;
1113 req->callback = NULL;
1114 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1116 req->op.seq_num.type = type;
1117 /* do request and autostart a transaction */
1118 ret = ldb_request(ldb, req);
1120 if (ret == LDB_SUCCESS) {
1121 *seq_num = req->op.seq_num.seq_num;
1124 talloc_free(req);
1125 return ret;
1131 return extended error information
1133 const char *ldb_errstring(struct ldb_context *ldb)
1135 if (ldb->err_string) {
1136 return ldb->err_string;
1139 return NULL;
1143 return a string explaining what a ldb error constant meancs
1145 const char *ldb_strerror(int ldb_err)
1147 switch (ldb_err) {
1148 case LDB_SUCCESS:
1149 return "Success";
1150 case LDB_ERR_OPERATIONS_ERROR:
1151 return "Operations error";
1152 case LDB_ERR_PROTOCOL_ERROR:
1153 return "Protocol error";
1154 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1155 return "Time limit exceeded";
1156 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1157 return "Size limit exceeded";
1158 case LDB_ERR_COMPARE_FALSE:
1159 return "Compare false";
1160 case LDB_ERR_COMPARE_TRUE:
1161 return "Compare true";
1162 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1163 return "Auth method not supported";
1164 case LDB_ERR_STRONG_AUTH_REQUIRED:
1165 return "Strong auth required";
1166 /* 9 RESERVED */
1167 case LDB_ERR_REFERRAL:
1168 return "Referral error";
1169 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1170 return "Admin limit exceeded";
1171 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1172 return "Unsupported critical extension";
1173 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1174 return "Confidentiality required";
1175 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1176 return "SASL bind in progress";
1177 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1178 return "No such attribute";
1179 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1180 return "Undefined attribute type";
1181 case LDB_ERR_INAPPROPRIATE_MATCHING:
1182 return "Inappropriate matching";
1183 case LDB_ERR_CONSTRAINT_VIOLATION:
1184 return "Constraint violation";
1185 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1186 return "Attribute or value exists";
1187 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1188 return "Invalid attribute syntax";
1189 /* 22-31 unused */
1190 case LDB_ERR_NO_SUCH_OBJECT:
1191 return "No such object";
1192 case LDB_ERR_ALIAS_PROBLEM:
1193 return "Alias problem";
1194 case LDB_ERR_INVALID_DN_SYNTAX:
1195 return "Invalid DN syntax";
1196 /* 35 RESERVED */
1197 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1198 return "Alias dereferencing problem";
1199 /* 37-47 unused */
1200 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1201 return "Inappropriate authentication";
1202 case LDB_ERR_INVALID_CREDENTIALS:
1203 return "Invalid credentials";
1204 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1205 return "insufficient access rights";
1206 case LDB_ERR_BUSY:
1207 return "Busy";
1208 case LDB_ERR_UNAVAILABLE:
1209 return "Unavailable";
1210 case LDB_ERR_UNWILLING_TO_PERFORM:
1211 return "Unwilling to perform";
1212 case LDB_ERR_LOOP_DETECT:
1213 return "Loop detect";
1214 /* 55-63 unused */
1215 case LDB_ERR_NAMING_VIOLATION:
1216 return "Naming violation";
1217 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1218 return "Object class violation";
1219 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1220 return "Not allowed on non-leaf";
1221 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1222 return "Not allowed on RDN";
1223 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1224 return "Entry already exists";
1225 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1226 return "Object class mods prohibited";
1227 /* 70 RESERVED FOR CLDAP */
1228 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1229 return "Affects multiple DSAs";
1230 /* 72-79 unused */
1231 case LDB_ERR_OTHER:
1232 return "Other";
1235 return "Unknown error";
1239 set backend specific opaque parameters
1241 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1243 struct ldb_opaque *o;
1245 /* allow updating an existing value */
1246 for (o=ldb->opaque;o;o=o->next) {
1247 if (strcmp(o->name, name) == 0) {
1248 o->value = value;
1249 return LDB_SUCCESS;
1253 o = talloc(ldb, struct ldb_opaque);
1254 if (o == NULL) {
1255 ldb_oom(ldb);
1256 return LDB_ERR_OTHER;
1258 o->next = ldb->opaque;
1259 o->name = name;
1260 o->value = value;
1261 ldb->opaque = o;
1262 return LDB_SUCCESS;
1266 get a previously set opaque value
1268 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1270 struct ldb_opaque *o;
1271 for (o=ldb->opaque;o;o=o->next) {
1272 if (strcmp(o->name, name) == 0) {
1273 return o->value;
1276 return NULL;