4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Simo Sorce 2005-2008
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 3 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, see <http://www.gnu.org/licenses/>.
28 * Component: ldb core API
30 * Description: core API routines interfacing to ldb backends
32 * Author: Andrew Tridgell
35 #include "ldb_includes.h"
38 initialise a ldb context
39 The mem_ctx is required
40 The event_ctx is required
42 struct ldb_context
*ldb_init(TALLOC_CTX
*mem_ctx
, struct event_context
*ev_ctx
)
44 struct ldb_context
*ldb
;
47 ldb
= talloc_zero(mem_ctx
, struct ldb_context
);
48 /* FIXME: Hack a new event context so that CMD line utilities work
49 * until we have them all converted */
51 ev_ctx
= event_context_init(talloc_autofree_context());
54 ret
= ldb_setup_wellknown_attributes(ldb
);
60 ldb_set_utf8_default(ldb
);
61 ldb_set_create_perms(ldb
, 0666);
62 ldb_set_modules_dir(ldb
, LDB_MODULESDIR
);
63 ldb_set_event_context(ldb
, ev_ctx
);
65 /* TODO: get timeout from options if available there */
66 ldb
->default_timeout
= 300; /* set default to 5 minutes */
72 try to autodetect a basedn if none specified. This fixes one of my
73 pet hates about ldapsearch, which is that you have to get a long,
74 complex basedn right to make any use of it.
76 void ldb_set_default_dns(struct ldb_context
*ldb
)
80 struct ldb_result
*res
;
81 struct ldb_dn
*tmp_dn
=NULL
;
82 static const char *attrs
[] = {
83 "rootDomainNamingContext",
84 "configurationNamingContext",
85 "schemaNamingContext",
86 "defaultNamingContext",
90 tmp_ctx
= talloc_new(ldb
);
91 ret
= ldb_search(ldb
, tmp_ctx
, &res
, ldb_dn_new(tmp_ctx
, ldb
, NULL
),
92 LDB_SCOPE_BASE
, attrs
, "(objectClass=*)");
93 if (ret
!= LDB_SUCCESS
) {
98 if (res
->count
!= 1) {
103 if (!ldb_get_opaque(ldb
, "rootDomainNamingContext")) {
104 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
105 "rootDomainNamingContext");
106 ldb_set_opaque(ldb
, "rootDomainNamingContext", tmp_dn
);
109 if (!ldb_get_opaque(ldb
, "configurationNamingContext")) {
110 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
111 "configurationNamingContext");
112 ldb_set_opaque(ldb
, "configurationNamingContext", tmp_dn
);
115 if (!ldb_get_opaque(ldb
, "schemaNamingContext")) {
116 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
117 "schemaNamingContext");
118 ldb_set_opaque(ldb
, "schemaNamingContext", tmp_dn
);
121 if (!ldb_get_opaque(ldb
, "defaultNamingContext")) {
122 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
123 "defaultNamingContext");
124 ldb_set_opaque(ldb
, "defaultNamingContext", tmp_dn
);
127 talloc_free(tmp_ctx
);
130 struct ldb_dn
*ldb_get_root_basedn(struct ldb_context
*ldb
)
132 void *opaque
= ldb_get_opaque(ldb
, "rootDomainNamingContext");
133 return talloc_get_type(opaque
, struct ldb_dn
);
136 struct ldb_dn
*ldb_get_config_basedn(struct ldb_context
*ldb
)
138 void *opaque
= ldb_get_opaque(ldb
, "configurationNamingContext");
139 return talloc_get_type(opaque
, struct ldb_dn
);
142 struct ldb_dn
*ldb_get_schema_basedn(struct ldb_context
*ldb
)
144 void *opaque
= ldb_get_opaque(ldb
, "schemaNamingContext");
145 return talloc_get_type(opaque
, struct ldb_dn
);
148 struct ldb_dn
*ldb_get_default_basedn(struct ldb_context
*ldb
)
150 void *opaque
= ldb_get_opaque(ldb
, "defaultNamingContext");
151 return talloc_get_type(opaque
, struct ldb_dn
);
155 connect to a database. The URL can either be one of the following forms
159 flags is made up of LDB_FLG_*
161 the options are passed uninterpreted to the backend, and are
164 int ldb_connect(struct ldb_context
*ldb
, const char *url
,
165 unsigned int flags
, const char *options
[])
169 /* We seem to need to do this here, or else some utilities don't
170 * get ldb backends */
174 url2
= talloc_strdup(ldb
, url
);
177 return LDB_ERR_OPERATIONS_ERROR
;
179 ret
= ldb_set_opaque(ldb
, "ldb_url", talloc_strdup(ldb
, url2
));
180 if (ret
!= LDB_SUCCESS
) {
184 ret
= ldb_connect_backend(ldb
, url
, options
, &ldb
->modules
);
185 if (ret
!= LDB_SUCCESS
) {
189 if (ldb_load_modules(ldb
, options
) != LDB_SUCCESS
) {
190 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
191 "Unable to load modules for %s: %s\n",
192 url
, ldb_errstring(ldb
));
193 return LDB_ERR_OTHER
;
196 /* TODO: get timeout from options if available there */
197 ldb
->default_timeout
= 300; /* set default to 5 minutes */
199 /* set the default base dn */
200 ldb_set_default_dns(ldb
);
205 void ldb_set_errstring(struct ldb_context
*ldb
, const char *err_string
)
207 if (ldb
->err_string
) {
208 talloc_free(ldb
->err_string
);
210 ldb
->err_string
= talloc_strdup(ldb
, err_string
);
213 void ldb_asprintf_errstring(struct ldb_context
*ldb
, const char *format
, ...)
216 char *old_string
= NULL
;
218 if (ldb
->err_string
) {
219 old_string
= ldb
->err_string
;
222 va_start(ap
, format
);
223 ldb
->err_string
= talloc_vasprintf(ldb
, format
, ap
);
225 talloc_free(old_string
);
228 void ldb_reset_err_string(struct ldb_context
*ldb
)
230 if (ldb
->err_string
) {
231 talloc_free(ldb
->err_string
);
232 ldb
->err_string
= NULL
;
236 #define FIRST_OP(ldb, op) do { \
237 module = ldb->modules; \
238 while (module && module->ops->op == NULL) module = module->next; \
239 if (module == NULL) { \
240 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
241 return LDB_ERR_OPERATIONS_ERROR; \
248 static int ldb_transaction_start_internal(struct ldb_context
*ldb
)
250 struct ldb_module
*module
;
252 FIRST_OP(ldb
, start_transaction
);
254 ldb_reset_err_string(ldb
);
256 status
= module
->ops
->start_transaction(module
);
257 if (status
!= LDB_SUCCESS
) {
258 if (ldb
->err_string
== NULL
) {
259 /* no error string was setup by the backend */
260 ldb_asprintf_errstring(ldb
,
261 "ldb transaction start: %s (%d)",
262 ldb_strerror(status
),
272 static int ldb_transaction_commit_internal(struct ldb_context
*ldb
)
274 struct ldb_module
*module
;
276 FIRST_OP(ldb
, end_transaction
);
278 ldb_reset_err_string(ldb
);
280 status
= module
->ops
->end_transaction(module
);
281 if (status
!= LDB_SUCCESS
) {
282 if (ldb
->err_string
== NULL
) {
283 /* no error string was setup by the backend */
284 ldb_asprintf_errstring(ldb
,
285 "ldb transaction commit: %s (%d)",
286 ldb_strerror(status
),
296 static int ldb_transaction_cancel_internal(struct ldb_context
*ldb
)
298 struct ldb_module
*module
;
300 FIRST_OP(ldb
, del_transaction
);
302 status
= module
->ops
->del_transaction(module
);
303 if (status
!= LDB_SUCCESS
) {
304 if (ldb
->err_string
== NULL
) {
305 /* no error string was setup by the backend */
306 ldb_asprintf_errstring(ldb
,
307 "ldb transaction cancel: %s (%d)",
308 ldb_strerror(status
),
315 int ldb_transaction_start(struct ldb_context
*ldb
)
317 /* disable autotransactions */
318 ldb
->transaction_active
++;
320 return ldb_transaction_start_internal(ldb
);
323 int ldb_transaction_commit(struct ldb_context
*ldb
)
325 /* renable autotransactions (when we reach 0) */
326 if (ldb
->transaction_active
> 0)
327 ldb
->transaction_active
--;
329 return ldb_transaction_commit_internal(ldb
);
332 int ldb_transaction_cancel(struct ldb_context
*ldb
)
334 /* renable autotransactions (when we reach 0) */
335 if (ldb
->transaction_active
> 0)
336 ldb
->transaction_active
--;
338 return ldb_transaction_cancel_internal(ldb
);
341 static int ldb_autotransaction_start(struct ldb_context
*ldb
)
343 /* explicit transaction active, ignore autotransaction request */
344 if (ldb
->transaction_active
)
347 return ldb_transaction_start_internal(ldb
);
350 static int ldb_autotransaction_commit(struct ldb_context
*ldb
)
352 /* explicit transaction active, ignore autotransaction request */
353 if (ldb
->transaction_active
)
356 return ldb_transaction_commit_internal(ldb
);
359 static int ldb_autotransaction_cancel(struct ldb_context
*ldb
)
361 /* explicit transaction active, ignore autotransaction request */
362 if (ldb
->transaction_active
)
365 return ldb_transaction_cancel_internal(ldb
);
368 /* autostarts a transacion if none active */
369 static int ldb_autotransaction_request(struct ldb_context
*ldb
,
370 struct ldb_request
*req
)
374 ret
= ldb_autotransaction_start(ldb
);
375 if (ret
!= LDB_SUCCESS
) {
379 ret
= ldb_request(ldb
, req
);
380 if (ret
== LDB_SUCCESS
) {
381 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
384 if (ret
== LDB_SUCCESS
) {
385 return ldb_autotransaction_commit(ldb
);
387 ldb_autotransaction_cancel(ldb
);
389 if (ldb
->err_string
== NULL
) {
390 /* no error string was setup by the backend */
391 ldb_asprintf_errstring(ldb
, "%s (%d)", ldb_strerror(ret
), ret
);
397 int ldb_wait(struct ldb_handle
*handle
, enum ldb_wait_type type
)
404 ret
= handle
->module
->ops
->wait(handle
, type
);
405 if (!ldb_errstring(handle
->module
->ldb
)) {
406 /* Set a default error string, to place the blame somewhere */
407 ldb_asprintf_errstring(handle
->module
->ldb
,
408 "error waiting on module %s: %s (%d)",
409 handle
->module
->ops
->name
,
410 ldb_strerror(ret
), ret
);
415 /* set the specified timeout or, if timeout is 0 set the default timeout */
416 /* timeout == -1 means no timeout */
417 int ldb_set_timeout(struct ldb_context
*ldb
,
418 struct ldb_request
*req
,
421 if (req
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
424 req
->timeout
= timeout
;
426 req
->timeout
= ldb
->default_timeout
;
428 req
->starttime
= time(NULL
);
433 /* calculates the new timeout based on the previous starttime and timeout */
434 int ldb_set_timeout_from_prev_req(struct ldb_context
*ldb
,
435 struct ldb_request
*oldreq
,
436 struct ldb_request
*newreq
)
440 if (newreq
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
445 return ldb_set_timeout(ldb
, newreq
, 0);
447 if ((now
- oldreq
->starttime
) > oldreq
->timeout
) {
448 return LDB_ERR_TIME_LIMIT_EXCEEDED
;
450 newreq
->starttime
= oldreq
->starttime
;
451 newreq
->timeout
= oldreq
->timeout
- (now
- oldreq
->starttime
);
458 set the permissions for new files to be passed to open() in
459 backends that use local files
461 void ldb_set_create_perms(struct ldb_context
*ldb
, unsigned int perms
)
463 ldb
->create_perms
= perms
;
466 void ldb_set_event_context(struct ldb_context
*ldb
, struct event_context
*ev
)
471 struct event_context
* ldb_get_event_context(struct ldb_context
*ldb
)
478 NOTE: the request must be a talloc context.
479 returns LDB_ERR_* on errors.
481 int ldb_request(struct ldb_context
*ldb
, struct ldb_request
*req
)
483 struct ldb_module
*module
;
486 ldb_reset_err_string(ldb
);
488 /* call the first module in the chain */
489 switch (req
->operation
) {
491 FIRST_OP(ldb
, search
);
492 ret
= module
->ops
->search(module
, req
);
496 ret
= module
->ops
->add(module
, req
);
499 FIRST_OP(ldb
, modify
);
500 ret
= module
->ops
->modify(module
, req
);
504 ret
= module
->ops
->del(module
, req
);
507 FIRST_OP(ldb
, rename
);
508 ret
= module
->ops
->rename(module
, req
);
511 FIRST_OP(ldb
, extended
);
512 ret
= module
->ops
->extended(module
, req
);
514 case LDB_SEQUENCE_NUMBER
:
515 FIRST_OP(ldb
, sequence_number
);
516 ret
= module
->ops
->sequence_number(module
, req
);
519 FIRST_OP(ldb
, request
);
520 ret
= module
->ops
->request(module
, req
);
528 search the database given a LDAP-like search expression
530 returns an LDB error code
532 Use talloc_free to free the ldb_message returned in 'res', if successful
535 int ldb_search_default_callback(struct ldb_context
*ldb
,
537 struct ldb_reply
*ares
)
539 struct ldb_result
*res
;
543 ldb_set_errstring(ldb
, "NULL Context in callback");
544 return LDB_ERR_OPERATIONS_ERROR
;
547 res
= talloc_get_type(context
, struct ldb_result
);
550 ldb_set_errstring(ldb
, "NULL res or ares in callback");
554 switch (ares
->type
) {
555 case LDB_REPLY_ENTRY
:
556 res
->msgs
= talloc_realloc(res
, res
->msgs
,
557 struct ldb_message
*,
563 res
->msgs
[res
->count
+ 1] = NULL
;
565 res
->msgs
[res
->count
] = talloc_move(res
->msgs
, &ares
->message
);
568 case LDB_REPLY_REFERRAL
:
570 for (n
= 0; res
->refs
[n
]; n
++) /*noop*/ ;
575 res
->refs
= talloc_realloc(res
, res
->refs
, char *, n
+ 2);
580 res
->refs
[n
] = talloc_move(res
->refs
, &ares
->referral
);
581 res
->refs
[n
+ 1] = NULL
;
583 case LDB_REPLY_EXTENDED
:
585 /* TODO: we should really support controls on entries
586 * and referrals too! */
587 res
->controls
= talloc_move(res
, &ares
->controls
);
595 return LDB_ERR_OPERATIONS_ERROR
;
598 int ldb_build_search_req(struct ldb_request
**ret_req
,
599 struct ldb_context
*ldb
,
602 enum ldb_scope scope
,
603 const char *expression
,
604 const char * const *attrs
,
605 struct ldb_control
**controls
,
607 ldb_request_callback_t callback
)
609 struct ldb_request
*req
;
613 req
= talloc(mem_ctx
, struct ldb_request
);
615 ldb_set_errstring(ldb
, "Out of Memory");
616 return LDB_ERR_OPERATIONS_ERROR
;
619 req
->operation
= LDB_SEARCH
;
621 req
->op
.search
.base
= ldb_dn_new(req
, ldb
, NULL
);
623 req
->op
.search
.base
= base
;
625 req
->op
.search
.scope
= scope
;
627 req
->op
.search
.tree
= ldb_parse_tree(req
, expression
);
628 if (req
->op
.search
.tree
== NULL
) {
629 ldb_set_errstring(ldb
, "Unable to parse search expression");
631 return LDB_ERR_OPERATIONS_ERROR
;
634 req
->op
.search
.attrs
= attrs
;
635 req
->controls
= controls
;
636 req
->context
= context
;
637 req
->callback
= callback
;
643 int ldb_build_add_req(struct ldb_request
**ret_req
,
644 struct ldb_context
*ldb
,
646 const struct ldb_message
*message
,
647 struct ldb_control
**controls
,
649 ldb_request_callback_t callback
)
651 struct ldb_request
*req
;
655 req
= talloc(mem_ctx
, struct ldb_request
);
657 ldb_set_errstring(ldb
, "Out of Memory");
658 return LDB_ERR_OPERATIONS_ERROR
;
661 req
->operation
= LDB_ADD
;
662 req
->op
.add
.message
= message
;
663 req
->controls
= controls
;
664 req
->context
= context
;
665 req
->callback
= callback
;
672 int ldb_build_mod_req(struct ldb_request
**ret_req
,
673 struct ldb_context
*ldb
,
675 const struct ldb_message
*message
,
676 struct ldb_control
**controls
,
678 ldb_request_callback_t callback
)
680 struct ldb_request
*req
;
684 req
= talloc(mem_ctx
, struct ldb_request
);
686 ldb_set_errstring(ldb
, "Out of Memory");
687 return LDB_ERR_OPERATIONS_ERROR
;
690 req
->operation
= LDB_MODIFY
;
691 req
->op
.mod
.message
= message
;
692 req
->controls
= controls
;
693 req
->context
= context
;
694 req
->callback
= callback
;
701 int ldb_build_del_req(struct ldb_request
**ret_req
,
702 struct ldb_context
*ldb
,
705 struct ldb_control
**controls
,
707 ldb_request_callback_t callback
)
709 struct ldb_request
*req
;
713 req
= talloc(mem_ctx
, struct ldb_request
);
715 ldb_set_errstring(ldb
, "Out of Memory");
716 return LDB_ERR_OPERATIONS_ERROR
;
719 req
->operation
= LDB_DELETE
;
721 req
->controls
= controls
;
722 req
->context
= context
;
723 req
->callback
= callback
;
730 int ldb_build_rename_req(struct ldb_request
**ret_req
,
731 struct ldb_context
*ldb
,
733 struct ldb_dn
*olddn
,
734 struct ldb_dn
*newdn
,
735 struct ldb_control
**controls
,
737 ldb_request_callback_t callback
)
739 struct ldb_request
*req
;
743 req
= talloc(mem_ctx
, struct ldb_request
);
745 ldb_set_errstring(ldb
, "Out of Memory");
746 return LDB_ERR_OPERATIONS_ERROR
;
749 req
->operation
= LDB_RENAME
;
750 req
->op
.rename
.olddn
= olddn
;
751 req
->op
.rename
.newdn
= newdn
;
752 req
->controls
= controls
;
753 req
->context
= context
;
754 req
->callback
= callback
;
761 int ldb_extended_default_callback(struct ldb_context
*ldb
,
763 struct ldb_reply
*ares
)
765 struct ldb_result
*res
;
768 ldb_set_errstring(ldb
, "NULL Context in callback");
769 return LDB_ERR_OPERATIONS_ERROR
;
772 res
= talloc_get_type(context
, struct ldb_result
);
774 ldb_set_errstring(ldb
, "NULL res or ares in callback");
778 switch (ares
->type
) {
779 case LDB_REPLY_ENTRY
:
780 case LDB_REPLY_REFERRAL
:
782 ldb_set_errstring(ldb
, "invalid ares type in callback");
784 case LDB_REPLY_EXTENDED
:
785 /* TODO: we should really support controls on entries and
787 res
->extended
= talloc_move(res
, &ares
->response
);
788 res
->controls
= talloc_move(res
, &ares
->controls
);
796 return LDB_ERR_OPERATIONS_ERROR
;
799 int ldb_build_extended_req(struct ldb_request
**ret_req
,
800 struct ldb_context
*ldb
,
804 struct ldb_control
**controls
,
806 ldb_request_callback_t callback
)
808 struct ldb_request
*req
;
812 req
= talloc(mem_ctx
, struct ldb_request
);
814 ldb_set_errstring(ldb
, "Out of Memory");
815 return LDB_ERR_OPERATIONS_ERROR
;
818 req
->operation
= LDB_EXTENDED
;
819 req
->op
.extended
.oid
= oid
;
820 req
->op
.extended
.data
= data
;
821 req
->controls
= controls
;
822 req
->context
= context
;
823 req
->callback
= callback
;
830 int ldb_extended(struct ldb_context
*ldb
,
833 struct ldb_result
**_res
)
835 struct ldb_request
*req
;
837 struct ldb_result
*res
;
841 res
= talloc_zero(ldb
, struct ldb_result
);
843 return LDB_ERR_OPERATIONS_ERROR
;
846 ret
= ldb_build_extended_req(&req
, ldb
, ldb
,
848 res
, ldb_extended_default_callback
);
849 if (ret
!= LDB_SUCCESS
) goto done
;
851 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
853 ret
= ldb_request(ldb
, req
);
855 if (ret
== LDB_SUCCESS
) {
856 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
862 if (ret
!= LDB_SUCCESS
) {
871 note that ldb_search() will automatically replace a NULL 'base' value
872 with the defaultNamingContext from the rootDSE if available.
874 int ldb_search(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
875 struct ldb_result
**result
, struct ldb_dn
*base
,
876 enum ldb_scope scope
, const char * const *attrs
,
877 const char *exp_fmt
, ...)
879 struct ldb_request
*req
;
880 struct ldb_result
*res
;
889 res
= talloc_zero(mem_ctx
, struct ldb_result
);
891 return LDB_ERR_OPERATIONS_ERROR
;
895 va_start(ap
, exp_fmt
);
896 expression
= talloc_vasprintf(mem_ctx
, exp_fmt
, ap
);
901 return LDB_ERR_OPERATIONS_ERROR
;
905 ret
= ldb_build_search_req(&req
, ldb
, mem_ctx
,
906 base
?base
:ldb_get_default_basedn(ldb
),
912 ldb_search_default_callback
);
914 if (ret
!= LDB_SUCCESS
) goto done
;
916 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
918 ret
= ldb_request(ldb
, req
);
920 if (ret
== LDB_SUCCESS
) {
921 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
925 if (ret
!= LDB_SUCCESS
) {
930 talloc_free(expression
);
938 add a record to the database. Will fail if a record with the
939 given class and key already exists
941 int ldb_add(struct ldb_context
*ldb
,
942 const struct ldb_message
*message
)
944 struct ldb_request
*req
;
947 ret
= ldb_msg_sanity_check(ldb
, message
);
948 if (ret
!= LDB_SUCCESS
) {
952 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
958 if (ret
!= LDB_SUCCESS
) return ret
;
960 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
962 /* do request and autostart a transaction */
963 ret
= ldb_autotransaction_request(ldb
, req
);
970 modify the specified attributes of a record
972 int ldb_modify(struct ldb_context
*ldb
,
973 const struct ldb_message
*message
)
975 struct ldb_request
*req
;
978 ret
= ldb_msg_sanity_check(ldb
, message
);
979 if (ret
!= LDB_SUCCESS
) {
983 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
989 if (ret
!= LDB_SUCCESS
) return ret
;
991 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
993 /* do request and autostart a transaction */
994 ret
= ldb_autotransaction_request(ldb
, req
);
1002 delete a record from the database
1004 int ldb_delete(struct ldb_context
*ldb
, struct ldb_dn
*dn
)
1006 struct ldb_request
*req
;
1009 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
1015 if (ret
!= LDB_SUCCESS
) return ret
;
1017 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1019 /* do request and autostart a transaction */
1020 ret
= ldb_autotransaction_request(ldb
, req
);
1027 rename a record in the database
1029 int ldb_rename(struct ldb_context
*ldb
,
1030 struct ldb_dn
*olddn
, struct ldb_dn
*newdn
)
1032 struct ldb_request
*req
;
1035 ret
= ldb_build_rename_req(&req
, ldb
, ldb
,
1042 if (ret
!= LDB_SUCCESS
) return ret
;
1044 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1046 /* do request and autostart a transaction */
1047 ret
= ldb_autotransaction_request(ldb
, req
);
1055 return the global sequence number
1057 int ldb_sequence_number(struct ldb_context
*ldb
,
1058 enum ldb_sequence_type type
,
1061 struct ldb_request
*req
;
1064 req
= talloc(ldb
, struct ldb_request
);
1066 ldb_set_errstring(ldb
, "Out of Memory");
1067 return LDB_ERR_OPERATIONS_ERROR
;
1070 req
->operation
= LDB_SEQUENCE_NUMBER
;
1071 req
->controls
= NULL
;
1072 req
->context
= NULL
;
1073 req
->callback
= NULL
;
1074 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1076 req
->op
.seq_num
.type
= type
;
1077 /* do request and autostart a transaction */
1078 ret
= ldb_request(ldb
, req
);
1080 if (ret
== LDB_SUCCESS
) {
1081 *seq_num
= req
->op
.seq_num
.seq_num
;
1091 return extended error information
1093 const char *ldb_errstring(struct ldb_context
*ldb
)
1095 if (ldb
->err_string
) {
1096 return ldb
->err_string
;
1103 return a string explaining what a ldb error constant meancs
1105 const char *ldb_strerror(int ldb_err
)
1110 case LDB_ERR_OPERATIONS_ERROR
:
1111 return "Operations error";
1112 case LDB_ERR_PROTOCOL_ERROR
:
1113 return "Protocol error";
1114 case LDB_ERR_TIME_LIMIT_EXCEEDED
:
1115 return "Time limit exceeded";
1116 case LDB_ERR_SIZE_LIMIT_EXCEEDED
:
1117 return "Size limit exceeded";
1118 case LDB_ERR_COMPARE_FALSE
:
1119 return "Compare false";
1120 case LDB_ERR_COMPARE_TRUE
:
1121 return "Compare true";
1122 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
:
1123 return "Auth method not supported";
1124 case LDB_ERR_STRONG_AUTH_REQUIRED
:
1125 return "Strong auth required";
1127 case LDB_ERR_REFERRAL
:
1128 return "Referral error";
1129 case LDB_ERR_ADMIN_LIMIT_EXCEEDED
:
1130 return "Admin limit exceeded";
1131 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
:
1132 return "Unsupported critical extension";
1133 case LDB_ERR_CONFIDENTIALITY_REQUIRED
:
1134 return "Confidentiality required";
1135 case LDB_ERR_SASL_BIND_IN_PROGRESS
:
1136 return "SASL bind in progress";
1137 case LDB_ERR_NO_SUCH_ATTRIBUTE
:
1138 return "No such attribute";
1139 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
:
1140 return "Undefined attribute type";
1141 case LDB_ERR_INAPPROPRIATE_MATCHING
:
1142 return "Inappropriate matching";
1143 case LDB_ERR_CONSTRAINT_VIOLATION
:
1144 return "Constraint violation";
1145 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
:
1146 return "Attribute or value exists";
1147 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
:
1148 return "Invalid attribute syntax";
1150 case LDB_ERR_NO_SUCH_OBJECT
:
1151 return "No such object";
1152 case LDB_ERR_ALIAS_PROBLEM
:
1153 return "Alias problem";
1154 case LDB_ERR_INVALID_DN_SYNTAX
:
1155 return "Invalid DN syntax";
1157 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
1158 return "Alias dereferencing problem";
1160 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION
:
1161 return "Inappropriate authentication";
1162 case LDB_ERR_INVALID_CREDENTIALS
:
1163 return "Invalid credentials";
1164 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
1165 return "insufficient access rights";
1168 case LDB_ERR_UNAVAILABLE
:
1169 return "Unavailable";
1170 case LDB_ERR_UNWILLING_TO_PERFORM
:
1171 return "Unwilling to perform";
1172 case LDB_ERR_LOOP_DETECT
:
1173 return "Loop detect";
1175 case LDB_ERR_NAMING_VIOLATION
:
1176 return "Naming violation";
1177 case LDB_ERR_OBJECT_CLASS_VIOLATION
:
1178 return "Object class violation";
1179 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
:
1180 return "Not allowed on non-leaf";
1181 case LDB_ERR_NOT_ALLOWED_ON_RDN
:
1182 return "Not allowed on RDN";
1183 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
1184 return "Entry already exists";
1185 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
:
1186 return "Object class mods prohibited";
1187 /* 70 RESERVED FOR CLDAP */
1188 case LDB_ERR_AFFECTS_MULTIPLE_DSAS
:
1189 return "Affects multiple DSAs";
1195 return "Unknown error";
1199 set backend specific opaque parameters
1201 int ldb_set_opaque(struct ldb_context
*ldb
, const char *name
, void *value
)
1203 struct ldb_opaque
*o
;
1205 /* allow updating an existing value */
1206 for (o
=ldb
->opaque
;o
;o
=o
->next
) {
1207 if (strcmp(o
->name
, name
) == 0) {
1213 o
= talloc(ldb
, struct ldb_opaque
);
1216 return LDB_ERR_OTHER
;
1218 o
->next
= ldb
->opaque
;
1226 get a previously set opaque value
1228 void *ldb_get_opaque(struct ldb_context
*ldb
, const char *name
)
1230 struct ldb_opaque
*o
;
1231 for (o
=ldb
->opaque
;o
;o
=o
->next
) {
1232 if (strcmp(o
->name
, name
) == 0) {