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
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
29 * Component: ldb core API
31 * Description: core API routines interfacing to ldb backends
33 * Author: Andrew Tridgell
37 #include "ldb/include/includes.h"
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);
48 ret = ldb_setup_wellknown_attributes(ldb);
54 ldb_set_utf8_default(ldb);
55 ldb_set_create_perms(ldb, 0666);
60 static struct ldb_backend {
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;
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)) {
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);
101 Return the ldb module form of a database. The URL can either be one of the following forms
105 flags is made up of LDB_FLG_*
107 the options are passed uninterpreted to the backend, and are
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)
120 if (strchr(url, ':') != NULL) {
121 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
124 backend = talloc_strdup(ldb, "tdb");
127 fn = ldb_find_backend(backend);
130 if (ldb_try_load_dso(ldb, backend) == 0) {
131 fn = ldb_find_backend(backend);
135 talloc_free(backend);
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);
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)
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",
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);
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
226 flags is made up of LDB_FLG_*
228 the options are passed uninterpreted to the backend, and are
231 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
237 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
238 if (ret != LDB_SUCCESS) {
242 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
243 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for %s: %s\n",
244 url, ldb_errstring(ldb));
245 return LDB_ERR_OTHER;
248 /* TODO: get timeout from options if available there */
249 ldb->default_timeout = 300; /* set default to 5 minutes */
251 /* set the default base dn */
252 ldb_set_default_dns(ldb);
257 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
259 if (ldb->err_string) {
260 talloc_free(ldb->err_string);
262 ldb->err_string = talloc_strdup(ldb, err_string);
265 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
269 if (ldb->err_string) {
270 talloc_free(ldb->err_string);
273 va_start(ap, format);
274 ldb->err_string = talloc_vasprintf(ldb, format, ap);
278 void ldb_reset_err_string(struct ldb_context *ldb)
280 if (ldb->err_string) {
281 talloc_free(ldb->err_string);
282 ldb->err_string = NULL;
286 #define FIRST_OP(ldb, op) do { \
287 module = ldb->modules; \
288 while (module && module->ops->op == NULL) module = module->next; \
289 if (module == NULL) { \
290 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
291 return LDB_ERR_OPERATIONS_ERROR; \
298 static int ldb_transaction_start_internal(struct ldb_context *ldb)
300 struct ldb_module *module;
302 FIRST_OP(ldb, start_transaction);
304 ldb_reset_err_string(ldb);
306 status = module->ops->start_transaction(module);
307 if (status != LDB_SUCCESS) {
308 if (ldb->err_string == NULL) {
309 /* no error string was setup by the backend */
310 ldb_asprintf_errstring(ldb,
311 "ldb transaction start: %s (%d)",
312 ldb_strerror(status),
322 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
324 struct ldb_module *module;
326 FIRST_OP(ldb, end_transaction);
328 ldb_reset_err_string(ldb);
330 status = module->ops->end_transaction(module);
331 if (status != LDB_SUCCESS) {
332 if (ldb->err_string == NULL) {
333 /* no error string was setup by the backend */
334 ldb_asprintf_errstring(ldb,
335 "ldb transaction commit: %s (%d)",
336 ldb_strerror(status),
346 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
348 struct ldb_module *module;
350 FIRST_OP(ldb, del_transaction);
352 status = module->ops->del_transaction(module);
353 if (status != LDB_SUCCESS) {
354 if (ldb->err_string == NULL) {
355 /* no error string was setup by the backend */
356 ldb_asprintf_errstring(ldb,
357 "ldb transaction cancel: %s (%d)",
358 ldb_strerror(status),
365 int ldb_transaction_start(struct ldb_context *ldb)
367 /* disable autotransactions */
368 ldb->transaction_active++;
370 return ldb_transaction_start_internal(ldb);
373 int ldb_transaction_commit(struct ldb_context *ldb)
375 /* renable autotransactions (when we reach 0) */
376 if (ldb->transaction_active > 0)
377 ldb->transaction_active--;
379 return ldb_transaction_commit_internal(ldb);
382 int ldb_transaction_cancel(struct ldb_context *ldb)
384 /* renable autotransactions (when we reach 0) */
385 if (ldb->transaction_active > 0)
386 ldb->transaction_active--;
388 return ldb_transaction_cancel_internal(ldb);
391 static int ldb_autotransaction_start(struct ldb_context *ldb)
393 /* explicit transaction active, ignore autotransaction request */
394 if (ldb->transaction_active)
397 return ldb_transaction_start_internal(ldb);
400 static int ldb_autotransaction_commit(struct ldb_context *ldb)
402 /* explicit transaction active, ignore autotransaction request */
403 if (ldb->transaction_active)
406 return ldb_transaction_commit_internal(ldb);
409 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
411 /* explicit transaction active, ignore autotransaction request */
412 if (ldb->transaction_active)
415 return ldb_transaction_cancel_internal(ldb);
418 /* autostarts a transacion if none active */
419 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
423 ret = ldb_autotransaction_start(ldb);
424 if (ret != LDB_SUCCESS) {
428 ret = ldb_request(ldb, req);
429 if (ret == LDB_SUCCESS) {
430 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
433 if (ret == LDB_SUCCESS) {
434 return ldb_autotransaction_commit(ldb);
436 ldb_autotransaction_cancel(ldb);
438 if (ldb->err_string == NULL) {
439 /* no error string was setup by the backend */
440 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
446 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
452 return handle->module->ops->wait(handle, type);
455 /* set the specified timeout or, if timeout is 0 set the default timeout */
456 /* timeout == -1 means no timeout */
457 int ldb_set_timeout(struct ldb_context *ldb, struct ldb_request *req, int timeout)
459 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
462 req->timeout = timeout;
464 req->timeout = ldb->default_timeout;
466 req->starttime = time(NULL);
471 /* calculates the new timeout based on the previous starttime and timeout */
472 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb, struct ldb_request *oldreq, struct ldb_request *newreq)
476 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
481 return ldb_set_timeout(ldb, newreq, 0);
483 if ((now - oldreq->starttime) > oldreq->timeout) {
484 return LDB_ERR_TIME_LIMIT_EXCEEDED;
486 newreq->starttime = oldreq->starttime;
487 newreq->timeout = oldreq->timeout - (now - oldreq->starttime);
494 set the permissions for new files to be passed to open() in
495 backends that use local files
497 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
499 ldb->create_perms = perms;
504 NOTE: the request must be a talloc context.
505 returns LDB_ERR_* on errors.
507 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
509 struct ldb_module *module;
512 ldb_reset_err_string(ldb);
514 /* call the first module in the chain */
515 switch (req->operation) {
517 FIRST_OP(ldb, search);
518 ret = module->ops->search(module, req);
522 ret = module->ops->add(module, req);
525 FIRST_OP(ldb, modify);
526 ret = module->ops->modify(module, req);
530 ret = module->ops->del(module, req);
533 FIRST_OP(ldb, rename);
534 ret = module->ops->rename(module, req);
537 FIRST_OP(ldb, extended);
538 ret = module->ops->extended(module, req);
540 case LDB_SEQUENCE_NUMBER:
541 FIRST_OP(ldb, sequence_number);
542 ret = module->ops->sequence_number(module, req);
545 FIRST_OP(ldb, request);
546 ret = module->ops->request(module, req);
554 search the database given a LDAP-like search expression
556 returns an LDB error code
558 Use talloc_free to free the ldb_message returned in 'res', if successful
561 int ldb_search_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
563 struct ldb_result *res;
567 ldb_set_errstring(ldb, "NULL Context in callback");
568 return LDB_ERR_OPERATIONS_ERROR;
571 res = talloc_get_type(context, struct ldb_result);
574 ldb_set_errstring(ldb, "NULL res or ares in callback");
578 switch (ares->type) {
579 case LDB_REPLY_ENTRY:
580 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
585 res->msgs[res->count + 1] = NULL;
587 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
590 case LDB_REPLY_REFERRAL:
592 for (n = 0; res->refs[n]; n++) /*noop*/ ;
597 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
602 res->refs[n] = talloc_move(res->refs, &ares->referral);
603 res->refs[n + 1] = NULL;
605 case LDB_REPLY_EXTENDED:
607 /* TODO: we should really support controls on entries and referrals too! */
608 res->controls = talloc_move(res, &ares->controls);
616 return LDB_ERR_OPERATIONS_ERROR;
619 int ldb_build_search_req(struct ldb_request **ret_req,
620 struct ldb_context *ldb,
623 enum ldb_scope scope,
624 const char *expression,
625 const char * const *attrs,
626 struct ldb_control **controls,
628 ldb_request_callback_t callback)
630 struct ldb_request *req;
634 req = talloc(mem_ctx, struct ldb_request);
636 ldb_set_errstring(ldb, "Out of Memory");
637 return LDB_ERR_OPERATIONS_ERROR;
640 req->operation = LDB_SEARCH;
642 req->op.search.base = ldb_dn_new(req, ldb, NULL);
644 req->op.search.base = base;
646 req->op.search.scope = scope;
648 req->op.search.tree = ldb_parse_tree(req, expression);
649 if (req->op.search.tree == NULL) {
650 ldb_set_errstring(ldb, "Unable to parse search expression");
652 return LDB_ERR_OPERATIONS_ERROR;
655 req->op.search.attrs = attrs;
656 req->controls = controls;
657 req->context = context;
658 req->callback = callback;
664 int ldb_build_add_req(struct ldb_request **ret_req,
665 struct ldb_context *ldb,
667 const struct ldb_message *message,
668 struct ldb_control **controls,
670 ldb_request_callback_t callback)
672 struct ldb_request *req;
676 req = talloc(mem_ctx, struct ldb_request);
678 ldb_set_errstring(ldb, "Out of Memory");
679 return LDB_ERR_OPERATIONS_ERROR;
682 req->operation = LDB_ADD;
683 req->op.add.message = message;
684 req->controls = controls;
685 req->context = context;
686 req->callback = callback;
693 int ldb_build_mod_req(struct ldb_request **ret_req,
694 struct ldb_context *ldb,
696 const struct ldb_message *message,
697 struct ldb_control **controls,
699 ldb_request_callback_t callback)
701 struct ldb_request *req;
705 req = talloc(mem_ctx, struct ldb_request);
707 ldb_set_errstring(ldb, "Out of Memory");
708 return LDB_ERR_OPERATIONS_ERROR;
711 req->operation = LDB_MODIFY;
712 req->op.mod.message = message;
713 req->controls = controls;
714 req->context = context;
715 req->callback = callback;
722 int ldb_build_del_req(struct ldb_request **ret_req,
723 struct ldb_context *ldb,
726 struct ldb_control **controls,
728 ldb_request_callback_t callback)
730 struct ldb_request *req;
734 req = talloc(mem_ctx, struct ldb_request);
736 ldb_set_errstring(ldb, "Out of Memory");
737 return LDB_ERR_OPERATIONS_ERROR;
740 req->operation = LDB_DELETE;
742 req->controls = controls;
743 req->context = context;
744 req->callback = callback;
751 int ldb_build_rename_req(struct ldb_request **ret_req,
752 struct ldb_context *ldb,
754 struct ldb_dn *olddn,
755 struct ldb_dn *newdn,
756 struct ldb_control **controls,
758 ldb_request_callback_t callback)
760 struct ldb_request *req;
764 req = talloc(mem_ctx, struct ldb_request);
766 ldb_set_errstring(ldb, "Out of Memory");
767 return LDB_ERR_OPERATIONS_ERROR;
770 req->operation = LDB_RENAME;
771 req->op.rename.olddn = olddn;
772 req->op.rename.newdn = newdn;
773 req->controls = controls;
774 req->context = context;
775 req->callback = callback;
782 int ldb_extended_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
784 struct ldb_result *res;
787 ldb_set_errstring(ldb, "NULL Context in callback");
788 return LDB_ERR_OPERATIONS_ERROR;
791 res = talloc_get_type(context, struct ldb_result);
793 ldb_set_errstring(ldb, "NULL res or ares in callback");
797 switch (ares->type) {
798 case LDB_REPLY_ENTRY:
799 case LDB_REPLY_REFERRAL:
801 ldb_set_errstring(ldb, "invalid ares type in callback");
803 case LDB_REPLY_EXTENDED:
804 /* TODO: we should really support controls on entries and referrals too! */
805 res->extended = talloc_move(res, &ares->response);
806 res->controls = talloc_move(res, &ares->controls);
814 return LDB_ERR_OPERATIONS_ERROR;
817 int ldb_build_extended_req(struct ldb_request **ret_req,
818 struct ldb_context *ldb,
822 struct ldb_control **controls,
824 ldb_request_callback_t callback)
826 struct ldb_request *req;
830 req = talloc(mem_ctx, struct ldb_request);
832 ldb_set_errstring(ldb, "Out of Memory");
833 return LDB_ERR_OPERATIONS_ERROR;
836 req->operation = LDB_EXTENDED;
837 req->op.extended.oid = oid;
838 req->op.extended.data = data;
839 req->controls = controls;
840 req->context = context;
841 req->callback = callback;
848 int ldb_extended(struct ldb_context *ldb,
851 struct ldb_result **_res)
853 struct ldb_request *req;
855 struct ldb_result *res;
859 res = talloc_zero(ldb, struct ldb_result);
861 return LDB_ERR_OPERATIONS_ERROR;
864 ret = ldb_build_extended_req(&req, ldb, ldb,
866 res, ldb_extended_default_callback);
867 if (ret != LDB_SUCCESS) goto done;
869 ldb_set_timeout(ldb, req, 0); /* use default timeout */
871 ret = ldb_request(ldb, req);
873 if (ret == LDB_SUCCESS) {
874 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
880 if (ret != LDB_SUCCESS) {
889 note that ldb_search() will automatically replace a NULL 'base' value with the
890 defaultNamingContext from the rootDSE if available.
892 int ldb_search(struct ldb_context *ldb,
894 enum ldb_scope scope,
895 const char *expression,
896 const char * const *attrs,
897 struct ldb_result **_res)
899 struct ldb_request *req;
901 struct ldb_result *res;
905 res = talloc_zero(ldb, struct ldb_result);
907 return LDB_ERR_OPERATIONS_ERROR;
910 ret = ldb_build_search_req(&req, ldb, ldb,
911 base?base:ldb_get_default_basedn(ldb),
917 ldb_search_default_callback);
919 if (ret != LDB_SUCCESS) goto done;
921 ldb_set_timeout(ldb, req, 0); /* use default timeout */
923 ret = ldb_request(ldb, req);
925 if (ret == LDB_SUCCESS) {
926 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
932 if (ret != LDB_SUCCESS) {
941 a useful search function where you can easily define the expression and that
942 takes a memory context where results are allocated
945 int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_result **result,
946 struct ldb_dn *base, enum ldb_scope scope, const char * const *attrs,
947 const char *exp_fmt, ...)
949 struct ldb_result *res;
957 va_start(ap, exp_fmt);
958 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
962 return LDB_ERR_OPERATIONS_ERROR;
965 ret = ldb_search(ldb, base, scope, expression, attrs, &res);
967 if (ret == LDB_SUCCESS) {
968 talloc_steal(mem_ctx, res);
974 talloc_free(expression);
980 add a record to the database. Will fail if a record with the given class and key
983 int ldb_add(struct ldb_context *ldb,
984 const struct ldb_message *message)
986 struct ldb_request *req;
989 ret = ldb_msg_sanity_check(ldb, message);
990 if (ret != LDB_SUCCESS) {
994 ret = ldb_build_add_req(&req, ldb, ldb,
1000 if (ret != LDB_SUCCESS) return ret;
1002 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1004 /* do request and autostart a transaction */
1005 ret = ldb_autotransaction_request(ldb, req);
1012 modify the specified attributes of a record
1014 int ldb_modify(struct ldb_context *ldb,
1015 const struct ldb_message *message)
1017 struct ldb_request *req;
1020 ret = ldb_msg_sanity_check(ldb, message);
1021 if (ret != LDB_SUCCESS) {
1025 ret = ldb_build_mod_req(&req, ldb, ldb,
1031 if (ret != LDB_SUCCESS) return ret;
1033 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1035 /* do request and autostart a transaction */
1036 ret = ldb_autotransaction_request(ldb, req);
1044 delete a record from the database
1046 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1048 struct ldb_request *req;
1051 ret = ldb_build_del_req(&req, ldb, ldb,
1057 if (ret != LDB_SUCCESS) return ret;
1059 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1061 /* do request and autostart a transaction */
1062 ret = ldb_autotransaction_request(ldb, req);
1069 rename a record in the database
1071 int ldb_rename(struct ldb_context *ldb, struct ldb_dn *olddn, struct ldb_dn *newdn)
1073 struct ldb_request *req;
1076 ret = ldb_build_rename_req(&req, ldb, ldb,
1083 if (ret != LDB_SUCCESS) return ret;
1085 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1087 /* do request and autostart a transaction */
1088 ret = ldb_autotransaction_request(ldb, req);
1096 return the global sequence number
1098 int ldb_sequence_number(struct ldb_context *ldb, enum ldb_sequence_type type, uint64_t *seq_num)
1100 struct ldb_request *req;
1103 req = talloc(ldb, struct ldb_request);
1105 ldb_set_errstring(ldb, "Out of Memory");
1106 return LDB_ERR_OPERATIONS_ERROR;
1109 req->operation = LDB_SEQUENCE_NUMBER;
1110 req->controls = NULL;
1111 req->context = NULL;
1112 req->callback = NULL;
1113 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1115 req->op.seq_num.type = type;
1116 /* do request and autostart a transaction */
1117 ret = ldb_request(ldb, req);
1119 if (ret == LDB_SUCCESS) {
1120 *seq_num = req->op.seq_num.seq_num;
1130 return extended error information
1132 const char *ldb_errstring(struct ldb_context *ldb)
1134 if (ldb->err_string) {
1135 return ldb->err_string;
1142 return a string explaining what a ldb error constant meancs
1144 const char *ldb_strerror(int ldb_err)
1149 case LDB_ERR_OPERATIONS_ERROR:
1150 return "Operations error";
1151 case LDB_ERR_PROTOCOL_ERROR:
1152 return "Protocol error";
1153 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1154 return "Time limit exceeded";
1155 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1156 return "Size limit exceeded";
1157 case LDB_ERR_COMPARE_FALSE:
1158 return "Compare false";
1159 case LDB_ERR_COMPARE_TRUE:
1160 return "Compare true";
1161 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1162 return "Auth method not supported";
1163 case LDB_ERR_STRONG_AUTH_REQUIRED:
1164 return "Strong auth required";
1166 case LDB_ERR_REFERRAL:
1167 return "Referral error";
1168 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1169 return "Admin limit exceeded";
1170 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1171 return "Unsupported critical extension";
1172 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1173 return "Confidentiality required";
1174 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1175 return "SASL bind in progress";
1176 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1177 return "No such attribute";
1178 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1179 return "Undefined attribute type";
1180 case LDB_ERR_INAPPROPRIATE_MATCHING:
1181 return "Inappropriate matching";
1182 case LDB_ERR_CONSTRAINT_VIOLATION:
1183 return "Constraint violation";
1184 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1185 return "Attribute or value exists";
1186 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1187 return "Invalid attribute syntax";
1189 case LDB_ERR_NO_SUCH_OBJECT:
1190 return "No such object";
1191 case LDB_ERR_ALIAS_PROBLEM:
1192 return "Alias problem";
1193 case LDB_ERR_INVALID_DN_SYNTAX:
1194 return "Invalid DN syntax";
1196 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1197 return "Alias dereferencing problem";
1199 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1200 return "Inappropriate authentication";
1201 case LDB_ERR_INVALID_CREDENTIALS:
1202 return "Invalid credentials";
1203 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1204 return "insufficient access rights";
1207 case LDB_ERR_UNAVAILABLE:
1208 return "Unavailable";
1209 case LDB_ERR_UNWILLING_TO_PERFORM:
1210 return "Unwilling to perform";
1211 case LDB_ERR_LOOP_DETECT:
1212 return "Loop detect";
1214 case LDB_ERR_NAMING_VIOLATION:
1215 return "Naming violation";
1216 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1217 return "Object class violation";
1218 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1219 return "Not allowed on non-leaf";
1220 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1221 return "Not allowed on RDN";
1222 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1223 return "Entry already exists";
1224 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1225 return "Object class mods prohibited";
1226 /* 70 RESERVED FOR CLDAP */
1227 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1228 return "Affects multiple DSAs";
1234 return "Unknown error";
1238 set backend specific opaque parameters
1240 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1242 struct ldb_opaque *o;
1244 /* allow updating an existing value */
1245 for (o=ldb->opaque;o;o=o->next) {
1246 if (strcmp(o->name, name) == 0) {
1252 o = talloc(ldb, struct ldb_opaque);
1255 return LDB_ERR_OTHER;
1257 o->next = ldb->opaque;
1265 get a previously set opaque value
1267 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1269 struct ldb_opaque *o;
1270 for (o=ldb->opaque;o;o=o->next) {
1271 if (strcmp(o->name, name) == 0) {