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
[])
235 /* We seem to need to do this here, or else some utilities don't get ldb backends */
240 ret
= ldb_connect_backend(ldb
, url
, options
, &ldb
->modules
);
241 if (ret
!= LDB_SUCCESS
) {
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
);
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
, ...)
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
);
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; \
301 static int ldb_transaction_start_internal(struct ldb_context
*ldb
)
303 struct ldb_module
*module
;
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
),
325 static int ldb_transaction_commit_internal(struct ldb_context
*ldb
)
327 struct ldb_module
*module
;
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
),
349 static int ldb_transaction_cancel_internal(struct ldb_context
*ldb
)
351 struct ldb_module
*module
;
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
),
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
)
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
)
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
)
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
)
426 ret
= ldb_autotransaction_start(ldb
);
427 if (ret
!= LDB_SUCCESS
) {
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
);
449 int ldb_wait(struct ldb_handle
*handle
, enum ldb_wait_type type
)
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
;
465 req
->timeout
= timeout
;
467 req
->timeout
= ldb
->default_timeout
;
469 req
->starttime
= time(NULL
);
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
)
479 if (newreq
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
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
);
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
;
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
;
515 ldb_reset_err_string(ldb
);
517 /* call the first module in the chain */
518 switch (req
->operation
) {
520 FIRST_OP(ldb
, search
);
521 ret
= module
->ops
->search(module
, req
);
525 ret
= module
->ops
->add(module
, req
);
528 FIRST_OP(ldb
, modify
);
529 ret
= module
->ops
->modify(module
, req
);
533 ret
= module
->ops
->del(module
, req
);
536 FIRST_OP(ldb
, rename
);
537 ret
= module
->ops
->rename(module
, req
);
540 FIRST_OP(ldb
, extended
);
541 ret
= module
->ops
->extended(module
, req
);
543 case LDB_SEQUENCE_NUMBER
:
544 FIRST_OP(ldb
, sequence_number
);
545 ret
= module
->ops
->sequence_number(module
, req
);
548 FIRST_OP(ldb
, request
);
549 ret
= module
->ops
->request(module
, req
);
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
;
570 ldb_set_errstring(ldb
, "NULL Context in callback");
571 return LDB_ERR_OPERATIONS_ERROR
;
574 res
= talloc_get_type(context
, struct ldb_result
);
577 ldb_set_errstring(ldb
, "NULL res or ares in callback");
581 switch (ares
->type
) {
582 case LDB_REPLY_ENTRY
:
583 res
->msgs
= talloc_realloc(res
, res
->msgs
, struct ldb_message
*, res
->count
+ 2);
588 res
->msgs
[res
->count
+ 1] = NULL
;
590 res
->msgs
[res
->count
] = talloc_move(res
->msgs
, &ares
->message
);
593 case LDB_REPLY_REFERRAL
:
595 for (n
= 0; res
->refs
[n
]; n
++) /*noop*/ ;
600 res
->refs
= talloc_realloc(res
, res
->refs
, char *, n
+ 2);
605 res
->refs
[n
] = talloc_move(res
->refs
, &ares
->referral
);
606 res
->refs
[n
+ 1] = NULL
;
608 case LDB_REPLY_EXTENDED
:
610 /* TODO: we should really support controls on entries and referrals too! */
611 res
->controls
= talloc_move(res
, &ares
->controls
);
619 return LDB_ERR_OPERATIONS_ERROR
;
622 int ldb_build_search_req(struct ldb_request
**ret_req
,
623 struct ldb_context
*ldb
,
626 enum ldb_scope scope
,
627 const char *expression
,
628 const char * const *attrs
,
629 struct ldb_control
**controls
,
631 ldb_request_callback_t callback
)
633 struct ldb_request
*req
;
637 req
= talloc(mem_ctx
, struct ldb_request
);
639 ldb_set_errstring(ldb
, "Out of Memory");
640 return LDB_ERR_OPERATIONS_ERROR
;
643 req
->operation
= LDB_SEARCH
;
645 req
->op
.search
.base
= ldb_dn_new(req
, ldb
, NULL
);
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");
655 return LDB_ERR_OPERATIONS_ERROR
;
658 req
->op
.search
.attrs
= attrs
;
659 req
->controls
= controls
;
660 req
->context
= context
;
661 req
->callback
= callback
;
667 int ldb_build_add_req(struct ldb_request
**ret_req
,
668 struct ldb_context
*ldb
,
670 const struct ldb_message
*message
,
671 struct ldb_control
**controls
,
673 ldb_request_callback_t callback
)
675 struct ldb_request
*req
;
679 req
= talloc(mem_ctx
, struct ldb_request
);
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
;
696 int ldb_build_mod_req(struct ldb_request
**ret_req
,
697 struct ldb_context
*ldb
,
699 const struct ldb_message
*message
,
700 struct ldb_control
**controls
,
702 ldb_request_callback_t callback
)
704 struct ldb_request
*req
;
708 req
= talloc(mem_ctx
, struct ldb_request
);
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
;
725 int ldb_build_del_req(struct ldb_request
**ret_req
,
726 struct ldb_context
*ldb
,
729 struct ldb_control
**controls
,
731 ldb_request_callback_t callback
)
733 struct ldb_request
*req
;
737 req
= talloc(mem_ctx
, struct ldb_request
);
739 ldb_set_errstring(ldb
, "Out of Memory");
740 return LDB_ERR_OPERATIONS_ERROR
;
743 req
->operation
= LDB_DELETE
;
745 req
->controls
= controls
;
746 req
->context
= context
;
747 req
->callback
= callback
;
754 int ldb_build_rename_req(struct ldb_request
**ret_req
,
755 struct ldb_context
*ldb
,
757 struct ldb_dn
*olddn
,
758 struct ldb_dn
*newdn
,
759 struct ldb_control
**controls
,
761 ldb_request_callback_t callback
)
763 struct ldb_request
*req
;
767 req
= talloc(mem_ctx
, struct ldb_request
);
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
;
785 int ldb_extended_default_callback(struct ldb_context
*ldb
, void *context
, struct ldb_reply
*ares
)
787 struct ldb_result
*res
;
790 ldb_set_errstring(ldb
, "NULL Context in callback");
791 return LDB_ERR_OPERATIONS_ERROR
;
794 res
= talloc_get_type(context
, struct ldb_result
);
796 ldb_set_errstring(ldb
, "NULL res or ares in callback");
800 switch (ares
->type
) {
801 case LDB_REPLY_ENTRY
:
802 case LDB_REPLY_REFERRAL
:
804 ldb_set_errstring(ldb
, "invalid ares type in callback");
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
);
817 return LDB_ERR_OPERATIONS_ERROR
;
820 int ldb_build_extended_req(struct ldb_request
**ret_req
,
821 struct ldb_context
*ldb
,
825 struct ldb_control
**controls
,
827 ldb_request_callback_t callback
)
829 struct ldb_request
*req
;
833 req
= talloc(mem_ctx
, struct ldb_request
);
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
;
851 int ldb_extended(struct ldb_context
*ldb
,
854 struct ldb_result
**_res
)
856 struct ldb_request
*req
;
858 struct ldb_result
*res
;
862 res
= talloc_zero(ldb
, struct ldb_result
);
864 return LDB_ERR_OPERATIONS_ERROR
;
867 ret
= ldb_build_extended_req(&req
, ldb
, ldb
,
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
);
883 if (ret
!= LDB_SUCCESS
) {
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
,
897 enum ldb_scope scope
,
898 const char *expression
,
899 const char * const *attrs
,
900 struct ldb_result
**_res
)
902 struct ldb_request
*req
;
904 struct ldb_result
*res
;
908 res
= talloc_zero(ldb
, struct ldb_result
);
910 return LDB_ERR_OPERATIONS_ERROR
;
913 ret
= ldb_build_search_req(&req
, ldb
, ldb
,
914 base
?base
:ldb_get_default_basedn(ldb
),
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
);
935 if (ret
!= LDB_SUCCESS
) {
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
;
960 va_start(ap
, exp_fmt
);
961 expression
= talloc_vasprintf(mem_ctx
, exp_fmt
, ap
);
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
);
975 talloc_free(expression
);
981 add a record to the database. Will fail if a record with the given class and key
984 int ldb_add(struct ldb_context
*ldb
,
985 const struct ldb_message
*message
)
987 struct ldb_request
*req
;
990 ret
= ldb_msg_sanity_check(ldb
, message
);
991 if (ret
!= LDB_SUCCESS
) {
995 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
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
);
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
;
1021 ret
= ldb_msg_sanity_check(ldb
, message
);
1022 if (ret
!= LDB_SUCCESS
) {
1026 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
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
);
1045 delete a record from the database
1047 int ldb_delete(struct ldb_context
*ldb
, struct ldb_dn
*dn
)
1049 struct ldb_request
*req
;
1052 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
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
);
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
;
1077 ret
= ldb_build_rename_req(&req
, ldb
, ldb
,
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
);
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
;
1104 req
= talloc(ldb
, struct ldb_request
);
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
;
1131 return extended error information
1133 const char *ldb_errstring(struct ldb_context
*ldb
)
1135 if (ldb
->err_string
) {
1136 return ldb
->err_string
;
1143 return a string explaining what a ldb error constant meancs
1145 const char *ldb_strerror(int ldb_err
)
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";
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";
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";
1197 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
1198 return "Alias dereferencing problem";
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";
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";
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";
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) {
1253 o
= talloc(ldb
, struct ldb_opaque
);
1256 return LDB_ERR_OTHER
;
1258 o
->next
= ldb
->opaque
;
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) {