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);
59 static struct ldb_backend {
61 ldb_connect_fn connect_fn;
62 struct ldb_backend *prev, *next;
63 } *ldb_backends = NULL;
65 register a new ldb backend
67 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
69 struct ldb_backend *backend = talloc(talloc_autofree_context(), struct ldb_backend);
71 /* Maybe check for duplicity here later on? */
73 backend->name = talloc_strdup(backend, url_prefix);
74 backend->connect_fn = connectfn;
75 DLIST_ADD(ldb_backends, backend);
80 static ldb_connect_fn ldb_find_backend(const char *url)
82 struct ldb_backend *backend;
84 for (backend = ldb_backends; backend; backend = backend->next) {
85 if (strncmp(backend->name, url, strlen(backend->name)) == 0) {
86 return backend->connect_fn;
94 connect to a database. The URL can either be one of the following forms
98 flags is made up of LDB_FLG_*
100 the options are passed uninterpreted to the backend, and are
103 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
109 if (strchr(url, ':') != NULL) {
110 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
113 backend = talloc_strdup(ldb, "tdb");
116 fn = ldb_find_backend(backend);
119 if (ldb_try_load_dso(ldb, backend) == 0) {
120 fn = ldb_find_backend(backend);
124 talloc_free(backend);
127 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
128 return LDB_ERR_OTHER;
131 ret = fn(ldb, url, flags, options);
133 if (ret != LDB_SUCCESS) {
134 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
138 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
139 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for '%s'\n", url);
140 return LDB_ERR_OTHER;
146 void ldb_set_errstring(struct ldb_context *ldb, char *err_string)
148 if (ldb->err_string) {
149 talloc_free(ldb->err_string);
151 ldb->err_string = talloc_steal(ldb, err_string);
154 void ldb_reset_err_string(struct ldb_context *ldb)
156 if (ldb->err_string) {
157 talloc_free(ldb->err_string);
158 ldb->err_string = NULL;
162 #define FIRST_OP(ldb, op) do { \
163 module = ldb->modules; \
164 while (module && module->ops->op == NULL) module = module->next; \
165 if (module == NULL) return LDB_ERR_OPERATIONS_ERROR; \
171 int ldb_transaction_start(struct ldb_context *ldb)
173 struct ldb_module *module;
175 FIRST_OP(ldb, start_transaction);
177 ldb->transaction_active++;
179 ldb_reset_err_string(ldb);
181 status = module->ops->start_transaction(module);
182 if (status != LDB_SUCCESS) {
183 if (ldb->err_string == NULL) {
184 /* no error string was setup by the backend */
185 ldb_set_errstring(ldb,
186 talloc_asprintf(ldb, "ldb transaction start error %d", status));
195 int ldb_transaction_commit(struct ldb_context *ldb)
197 struct ldb_module *module;
199 FIRST_OP(ldb, end_transaction);
201 if (ldb->transaction_active > 0) {
202 ldb->transaction_active--;
204 return LDB_ERR_OPERATIONS_ERROR;
207 ldb_reset_err_string(ldb);
209 status = module->ops->end_transaction(module);
210 if (status != LDB_SUCCESS) {
211 if (ldb->err_string == NULL) {
212 /* no error string was setup by the backend */
213 ldb_set_errstring(ldb,
214 talloc_asprintf(ldb, "ldb transaction commit error %d", status));
223 int ldb_transaction_cancel(struct ldb_context *ldb)
225 struct ldb_module *module;
227 FIRST_OP(ldb, del_transaction);
229 if (ldb->transaction_active > 0) {
230 ldb->transaction_active--;
232 return LDB_ERR_OPERATIONS_ERROR;
235 status = module->ops->del_transaction(module);
236 if (status != LDB_SUCCESS) {
237 if (ldb->err_string == NULL) {
238 /* no error string was setup by the backend */
239 ldb_set_errstring(ldb,
240 talloc_asprintf(ldb, "ldb transaction cancel error %d", status));
246 int ldb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
252 return handle->module->ops->async_wait(handle, type);
257 check for an error return from an op
258 if an op fails, but has not setup an error string, then setup one now
260 static int ldb_op_finish(struct ldb_context *ldb, int status)
262 if (status == LDB_SUCCESS) {
263 return ldb_transaction_commit(ldb);
265 if (ldb->err_string == NULL) {
266 /* no error string was setup by the backend */
267 ldb_set_errstring(ldb,
268 talloc_asprintf(ldb, "%s (%d)",
269 ldb_strerror(status), status));
271 ldb_transaction_cancel(ldb);
277 NOTE: the request must be a talloc context.
278 returns LDB_ERR_* on errors.
280 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
282 struct ldb_module *module;
285 ldb_reset_err_string(ldb);
287 /* call the first module in the chain */
288 switch (req->operation) {
290 FIRST_OP(ldb, search);
291 ret = module->ops->search(module, req);
295 ret = module->ops->add(module, req);
298 FIRST_OP(ldb, modify);
299 ret = module->ops->modify(module, req);
303 ret = module->ops->del(module, req);
306 FIRST_OP(ldb, rename);
307 ret = module->ops->rename(module, req);
310 FIRST_OP(ldb, request);
311 ret = module->ops->request(module, req);
319 search the database given a LDAP-like search expression
321 returns an LDB error code
323 Use talloc_free to free the ldb_message returned in 'res', if successful
326 static int ldb_search_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
328 struct ldb_result *res;
332 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context in callback"));
333 return LDB_ERR_OPERATIONS_ERROR;
336 res = *((struct ldb_result **)context);
342 if (ares->type == LDB_REPLY_ENTRY) {
343 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
348 res->msgs[res->count + 1] = NULL;
350 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
351 if (! res->msgs[res->count]) {
358 if (ares->type == LDB_REPLY_REFERRAL) {
360 for (n = 0; res->refs[n]; n++) /*noop*/ ;
365 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
370 res->refs[n] = talloc_steal(res->refs, ares->referral);
371 res->refs[n + 1] = NULL;
374 if (ares->controls) {
375 res->controls = talloc_steal(res, ares->controls);
376 if (! res->controls) {
387 *((struct ldb_result **)context) = NULL;
388 return LDB_ERR_OPERATIONS_ERROR;
391 int ldb_search(struct ldb_context *ldb,
392 const struct ldb_dn *base,
393 enum ldb_scope scope,
394 const char *expression,
395 const char * const *attrs,
396 struct ldb_result **res)
398 struct ldb_request *req;
401 *res = talloc_zero(ldb, struct ldb_result);
403 return LDB_ERR_OPERATIONS_ERROR;
406 req = talloc(ldb, struct ldb_request);
408 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
409 return LDB_ERR_OPERATIONS_ERROR;
412 req->operation = LDB_SEARCH;
413 req->op.search.base = base;
414 req->op.search.scope = scope;
416 req->op.search.tree = ldb_parse_tree(req, expression);
417 if (req->op.search.tree == NULL) {
418 ldb_set_errstring(ldb, talloc_strdup(ldb, "Unable to parse search expression"));
420 return LDB_ERR_OPERATIONS_ERROR;
423 req->op.search.attrs = attrs;
424 req->controls = NULL;
425 req->async.context = res;
426 req->async.callback = ldb_search_callback;
427 req->async.timeout = 600; /* 10 minutes */
429 ret = ldb_request(ldb, req);
431 if (ret == LDB_SUCCESS) {
432 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
435 if (ret != LDB_SUCCESS) {
444 /* autostarts a transacion if none active */
445 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
447 int ret, close_transaction;
449 close_transaction = 0;
450 if (!ldb->transaction_active) {
451 ret = ldb_transaction_start(ldb);
452 if (ret != LDB_SUCCESS) {
455 close_transaction = 1;
458 ret = ldb_request(ldb, req);
459 if (ret == LDB_SUCCESS) {
460 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
463 if (close_transaction) {
464 return ldb_op_finish(ldb, ret);
467 if (ldb->err_string == NULL) {
468 /* no error string was setup by the backend */
469 ldb_set_errstring(ldb,
470 talloc_asprintf(ldb, "%s (%d)",
471 ldb_strerror(ret), ret));
479 add a record to the database. Will fail if a record with the given class and key
482 int ldb_add(struct ldb_context *ldb,
483 const struct ldb_message *message)
485 struct ldb_request *req;
488 ret = ldb_msg_sanity_check(message);
489 if (ret != LDB_SUCCESS) return ret;
491 req = talloc(ldb, struct ldb_request);
493 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
494 return LDB_ERR_OPERATIONS_ERROR;
497 req->operation = LDB_ADD;
498 req->op.add.message = message;
499 req->controls = NULL;
500 req->async.context = NULL;
501 req->async.callback = NULL;
502 req->async.timeout = 600; /* 10 minutes */
504 /* do request and autostart a transaction */
505 ret = ldb_autotransaction_request(ldb, req);
512 modify the specified attributes of a record
514 int ldb_modify(struct ldb_context *ldb,
515 const struct ldb_message *message)
517 struct ldb_request *req;
520 ret = ldb_msg_sanity_check(message);
521 if (ret != LDB_SUCCESS) return ret;
523 req = talloc(ldb, struct ldb_request);
525 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
526 return LDB_ERR_OPERATIONS_ERROR;
529 req->operation = LDB_MODIFY;
530 req->op.add.message = message;
531 req->controls = NULL;
532 req->async.context = NULL;
533 req->async.callback = NULL;
534 req->async.timeout = 600; /* 10 minutes */
536 /* do request and autostart a transaction */
537 ret = ldb_autotransaction_request(ldb, req);
545 delete a record from the database
547 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
549 struct ldb_request *req;
552 req = talloc(ldb, struct ldb_request);
554 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
555 return LDB_ERR_OPERATIONS_ERROR;
558 req->operation = LDB_DELETE;
560 req->controls = NULL;
561 req->async.context = NULL;
562 req->async.callback = NULL;
563 req->async.timeout = 600; /* 10 minutes */
565 /* do request and autostart a transaction */
566 ret = ldb_autotransaction_request(ldb, req);
573 rename a record in the database
575 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
577 struct ldb_request *req;
580 req = talloc(ldb, struct ldb_request);
582 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
583 return LDB_ERR_OPERATIONS_ERROR;
586 req->operation = LDB_RENAME;
587 req->op.rename.olddn = olddn;
588 req->op.rename.newdn = newdn;
589 req->controls = NULL;
590 req->async.context = NULL;
591 req->async.callback = NULL;
592 req->async.timeout = 600; /* 10 minutes */
594 /* do request and autostart a transaction */
595 ret = ldb_autotransaction_request(ldb, req);
604 return extended error information
606 const char *ldb_errstring(struct ldb_context *ldb)
608 if (ldb->err_string) {
609 return ldb->err_string;
616 return a string explaining what a ldb error constant meancs
618 const char *ldb_strerror(int ldb_err)
623 case LDB_ERR_OPERATIONS_ERROR:
624 return "Operations error";
625 case LDB_ERR_PROTOCOL_ERROR:
626 return "Protocol error";
627 case LDB_ERR_TIME_LIMIT_EXCEEDED:
628 return "Time limit exceeded";
629 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
630 return "Size limit exceeded";
631 case LDB_ERR_COMPARE_FALSE:
632 return "Compare false";
633 case LDB_ERR_COMPARE_TRUE:
634 return "Compare true";
635 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
636 return "Auth method not supported";
637 case LDB_ERR_STRONG_AUTH_REQUIRED:
638 return "Strong auth required";
640 case LDB_ERR_REFERRAL:
641 return "Referral error";
642 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
643 return "Admin limit exceeded";
644 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
645 return "Unsupported critical extension";
646 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
647 return "Confidentiality required";
648 case LDB_ERR_SASL_BIND_IN_PROGRESS:
649 return "SASL bind in progress";
650 case LDB_ERR_NO_SUCH_ATTRIBUTE:
651 return "No such attribute";
652 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
653 return "Undefined attribute type";
654 case LDB_ERR_INAPPROPRIATE_MATCHING:
655 return "Inappropriate matching";
656 case LDB_ERR_CONSTRAINT_VIOLATION:
657 return "Constraint violation";
658 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
659 return "Attribute or value exists";
660 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
661 return "Invalid attribute syntax";
663 case LDB_ERR_NO_SUCH_OBJECT:
664 return "No such object";
665 case LDB_ERR_ALIAS_PROBLEM:
666 return "Alias problem";
667 case LDB_ERR_INVALID_DN_SYNTAX:
668 return "Invalid DN syntax";
670 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
671 return "Alias dereferencing problem";
673 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
674 return "Inappropriate authentication";
675 case LDB_ERR_INVALID_CREDENTIALS:
676 return "Invalid credentials";
677 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
678 return "insufficient access rights";
681 case LDB_ERR_UNAVAILABLE:
682 return "Unavailable";
683 case LDB_ERR_UNWILLING_TO_PERFORM:
684 return "Unwilling to perform";
685 case LDB_ERR_LOOP_DETECT:
686 return "Loop detect";
688 case LDB_ERR_NAMING_VIOLATION:
689 return "Naming violation";
690 case LDB_ERR_OBJECT_CLASS_VIOLATION:
691 return "Object class violation";
692 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
693 return "Not allowed on non-leaf";
694 case LDB_ERR_NOT_ALLOWED_ON_RDN:
695 return "Not allowed on RDN";
696 case LDB_ERR_ENTRY_ALREADY_EXISTS:
697 return "Entry already exists";
698 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
699 return "Object class mods prohibited";
700 /* 70 RESERVED FOR CLDAP */
701 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
702 return "Affects multiple DSAs";
708 return "Unknown error";
712 set backend specific opaque parameters
714 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
716 struct ldb_opaque *o;
718 /* allow updating an existing value */
719 for (o=ldb->opaque;o;o=o->next) {
720 if (strcmp(o->name, name) == 0) {
726 o = talloc(ldb, struct ldb_opaque);
729 return LDB_ERR_OTHER;
731 o->next = ldb->opaque;
739 get a previously set opaque value
741 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
743 struct ldb_opaque *o;
744 for (o=ldb->opaque;o;o=o->next) {
745 if (strcmp(o->name, name) == 0) {