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, "ldb error %d", status));
270 ldb_transaction_cancel(ldb);
276 NOTE: the request must be a talloc context.
277 returns LDB_ERR_* on errors.
279 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
281 struct ldb_module *module;
284 ldb_reset_err_string(ldb);
286 /* call the first module in the chain */
287 switch (req->operation) {
289 FIRST_OP(ldb, search);
290 ret = module->ops->search(module, req);
294 ret = module->ops->add(module, req);
297 FIRST_OP(ldb, modify);
298 ret = module->ops->modify(module, req);
302 ret = module->ops->del(module, req);
305 FIRST_OP(ldb, rename);
306 ret = module->ops->rename(module, req);
309 FIRST_OP(ldb, request);
310 ret = module->ops->request(module, req);
318 search the database given a LDAP-like search expression
320 returns an LDB error code
322 Use talloc_free to free the ldb_message returned in 'res', if successful
325 static int ldb_search_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
327 struct ldb_result *res;
331 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context in callback"));
332 return LDB_ERR_OPERATIONS_ERROR;
335 res = *((struct ldb_result **)context);
341 if (ares->type == LDB_REPLY_ENTRY) {
342 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
347 res->msgs[res->count + 1] = NULL;
349 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
350 if (! res->msgs[res->count]) {
357 if (ares->type == LDB_REPLY_REFERRAL) {
359 for (n = 0; res->refs[n]; n++) /*noop*/ ;
364 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
369 res->refs[n] = talloc_steal(res->refs, ares->referral);
370 res->refs[n + 1] = NULL;
373 if (ares->controls) {
374 res->controls = talloc_steal(res, ares->controls);
375 if (! res->controls) {
386 *((struct ldb_result **)context) = NULL;
387 return LDB_ERR_OPERATIONS_ERROR;
390 int ldb_search(struct ldb_context *ldb,
391 const struct ldb_dn *base,
392 enum ldb_scope scope,
393 const char *expression,
394 const char * const *attrs,
395 struct ldb_result **res)
397 struct ldb_request *req;
400 *res = talloc_zero(ldb, struct ldb_result);
402 return LDB_ERR_OPERATIONS_ERROR;
405 req = talloc(ldb, struct ldb_request);
407 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
408 return LDB_ERR_OPERATIONS_ERROR;
411 req->operation = LDB_SEARCH;
412 req->op.search.base = base;
413 req->op.search.scope = scope;
415 req->op.search.tree = ldb_parse_tree(req, expression);
416 if (req->op.search.tree == NULL) {
417 ldb_set_errstring(ldb, talloc_strdup(ldb, "Unable to parse search expression"));
419 return LDB_ERR_OPERATIONS_ERROR;
422 req->op.search.attrs = attrs;
423 req->controls = NULL;
424 req->async.context = res;
425 req->async.callback = ldb_search_callback;
426 req->async.timeout = 600; /* 10 minutes */
428 ret = ldb_request(ldb, req);
430 if (ret == LDB_SUCCESS) {
431 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
434 if (ret != LDB_SUCCESS) {
443 /* autostarts a transacion if none active */
444 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
446 int ret, close_transaction;
448 close_transaction = 0;
449 if (!ldb->transaction_active) {
450 ret = ldb_transaction_start(ldb);
451 if (ret != LDB_SUCCESS) {
454 close_transaction = 1;
457 ret = ldb_request(ldb, req);
458 if (ret == LDB_SUCCESS) {
459 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
462 if (close_transaction) {
463 return ldb_op_finish(ldb, ret);
471 add a record to the database. Will fail if a record with the given class and key
474 int ldb_add(struct ldb_context *ldb,
475 const struct ldb_message *message)
477 struct ldb_request *req;
480 ret = ldb_msg_sanity_check(message);
481 if (ret != LDB_SUCCESS) return ret;
483 req = talloc(ldb, struct ldb_request);
485 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
486 return LDB_ERR_OPERATIONS_ERROR;
489 req->operation = LDB_ADD;
490 req->op.add.message = message;
491 req->controls = NULL;
492 req->async.context = NULL;
493 req->async.callback = NULL;
494 req->async.timeout = 600; /* 10 minutes */
496 /* do request and autostart a transaction */
497 ret = ldb_autotransaction_request(ldb, req);
504 modify the specified attributes of a record
506 int ldb_modify(struct ldb_context *ldb,
507 const struct ldb_message *message)
509 struct ldb_request *req;
512 ret = ldb_msg_sanity_check(message);
513 if (ret != LDB_SUCCESS) return ret;
515 req = talloc(ldb, struct ldb_request);
517 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
518 return LDB_ERR_OPERATIONS_ERROR;
521 req->operation = LDB_MODIFY;
522 req->op.add.message = message;
523 req->controls = NULL;
524 req->async.context = NULL;
525 req->async.callback = NULL;
526 req->async.timeout = 600; /* 10 minutes */
528 /* do request and autostart a transaction */
529 ret = ldb_autotransaction_request(ldb, req);
537 delete a record from the database
539 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
541 struct ldb_request *req;
544 req = talloc(ldb, struct ldb_request);
546 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
547 return LDB_ERR_OPERATIONS_ERROR;
550 req->operation = LDB_DELETE;
552 req->controls = NULL;
553 req->async.context = NULL;
554 req->async.callback = NULL;
555 req->async.timeout = 600; /* 10 minutes */
557 /* do request and autostart a transaction */
558 ret = ldb_autotransaction_request(ldb, req);
565 rename a record in the database
567 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
569 struct ldb_request *req;
572 req = talloc(ldb, struct ldb_request);
574 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
575 return LDB_ERR_OPERATIONS_ERROR;
578 req->operation = LDB_RENAME;
579 req->op.rename.olddn = olddn;
580 req->op.rename.newdn = newdn;
581 req->controls = NULL;
582 req->async.context = NULL;
583 req->async.callback = NULL;
584 req->async.timeout = 600; /* 10 minutes */
586 /* do request and autostart a transaction */
587 ret = ldb_autotransaction_request(ldb, req);
596 return extended error information
598 const char *ldb_errstring(struct ldb_context *ldb)
600 if (ldb->err_string) {
601 return ldb->err_string;
608 return a string explaining what a ldb error constant meancs
610 const char *ldb_strerror(int ldb_err)
615 case LDB_ERR_OPERATIONS_ERROR:
616 return "Operations error";
617 case LDB_ERR_PROTOCOL_ERROR:
618 return "Protocol error";
619 case LDB_ERR_TIME_LIMIT_EXCEEDED:
620 return "Time limit exceeded";
621 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
622 return "Size limit exceeded";
623 case LDB_ERR_COMPARE_FALSE:
624 return "Compare false";
625 case LDB_ERR_COMPARE_TRUE:
626 return "Compare true";
627 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
628 return "Auth method not supported";
629 case LDB_ERR_STRONG_AUTH_REQUIRED:
630 return "Strong auth required";
632 case LDB_ERR_REFERRAL:
633 return "Referral error";
634 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
635 return "Admin limit exceeded";
636 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
637 return "Unsupported critical extension";
638 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
639 return "Confidentiality required";
640 case LDB_ERR_SASL_BIND_IN_PROGRESS:
641 return "SASL bind in progress";
642 case LDB_ERR_NO_SUCH_ATTRIBUTE:
643 return "No such attribute";
644 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
645 return "Undefined attribute type";
646 case LDB_ERR_INAPPROPRIATE_MATCHING:
647 return "Inappropriate matching";
648 case LDB_ERR_CONSTRAINT_VIOLATION:
649 return "Constraint violation";
650 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
651 return "Attribute or value exists";
652 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
653 return "Invalid attribute syntax";
655 case LDB_ERR_NO_SUCH_OBJECT:
656 return "No such object";
657 case LDB_ERR_ALIAS_PROBLEM:
658 return "Alias problem";
659 case LDB_ERR_INVALID_DN_SYNTAX:
660 return "Invalid DN syntax";
662 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
663 return "Alias dereferencing problem";
665 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
666 return "Inappropriate authentication";
667 case LDB_ERR_INVALID_CREDENTIALS:
668 return "Invalid credentials";
669 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
670 return "insufficient access rights";
673 case LDB_ERR_UNAVAILABLE:
674 return "Unavailable";
675 case LDB_ERR_UNWILLING_TO_PERFORM:
676 return "Unwilling to perform";
677 case LDB_ERR_LOOP_DETECT:
678 return "Loop detect";
680 case LDB_ERR_NAMING_VIOLATION:
681 return "Naming violation";
682 case LDB_ERR_OBJECT_CLASS_VIOLATION:
683 return "Object class violation";
684 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
685 return "Not allowed on non-leaf";
686 case LDB_ERR_NOT_ALLOWED_ON_RDN:
687 return "Not allowed on RDN";
688 case LDB_ERR_ENTRY_ALREADY_EXISTS:
689 return "Entry already exists";
690 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
691 return "Object class mods prohibited";
692 /* 70 RESERVED FOR CLDAP */
693 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
694 return "Affects multiple DSAs";
700 return "Unknown error";
704 set backend specific opaque parameters
706 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
708 struct ldb_opaque *o;
710 /* allow updating an existing value */
711 for (o=ldb->opaque;o;o=o->next) {
712 if (strcmp(o->name, name) == 0) {
718 o = talloc(ldb, struct ldb_opaque);
721 return LDB_ERR_OTHER;
723 o->next = ldb->opaque;
731 get a previously set opaque value
733 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
735 struct ldb_opaque *o;
736 for (o=ldb->opaque;o;o=o->next) {
737 if (strcmp(o->name, name) == 0) {