4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Simo Sorce 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 ldap backend
31 * Description: core files for LDAP backend
33 * Author: Andrew Tridgell
37 * - description: make the module use asyncronous calls
43 #include "ldb/include/includes.h"
45 #define LDAP_DEPRECATED 1
53 struct ldb_module
*module
;
58 int (*callback
)(struct ldb_context
*, void *, struct ldb_reply
*);
61 static int lldb_ldap_to_ldb(int err
) {
62 /* Ldap errors and ldb errors are defined to the same values */
66 static struct ldb_handle
*init_handle(struct lldb_private
*lldb
, struct ldb_module
*module
,
68 int (*callback
)(struct ldb_context
*, void *, struct ldb_reply
*),
69 int timeout
, time_t starttime
)
71 struct lldb_context
*ac
;
74 h
= talloc_zero(lldb
, struct ldb_handle
);
76 ldb_set_errstring(module
->ldb
, "Out of Memory");
82 ac
= talloc(h
, struct lldb_context
);
84 ldb_set_errstring(module
->ldb
, "Out of Memory");
89 h
->private_data
= (void *)ac
;
91 h
->state
= LDB_ASYNC_INIT
;
92 h
->status
= LDB_SUCCESS
;
95 ac
->context
= context
;
96 ac
->callback
= callback
;
97 ac
->timeout
= timeout
;
98 ac
->starttime
= starttime
;
104 convert a ldb_message structure to a list of LDAPMod structures
105 ready for ldap_add() or ldap_modify()
107 static LDAPMod
**lldb_msg_to_mods(void *mem_ctx
, const struct ldb_message
*msg
, int use_flags
)
113 /* allocate maximum number of elements needed */
114 mods
= talloc_array(mem_ctx
, LDAPMod
*, msg
->num_elements
+1);
121 for (i
=0;i
<msg
->num_elements
;i
++) {
122 const struct ldb_message_element
*el
= &msg
->elements
[i
];
124 mods
[num_mods
] = talloc(mods
, LDAPMod
);
125 if (!mods
[num_mods
]) {
128 mods
[num_mods
+1] = NULL
;
129 mods
[num_mods
]->mod_op
= LDAP_MOD_BVALUES
;
131 switch (el
->flags
& LDB_FLAG_MOD_MASK
) {
132 case LDB_FLAG_MOD_ADD
:
133 mods
[num_mods
]->mod_op
|= LDAP_MOD_ADD
;
135 case LDB_FLAG_MOD_DELETE
:
136 mods
[num_mods
]->mod_op
|= LDAP_MOD_DELETE
;
138 case LDB_FLAG_MOD_REPLACE
:
139 mods
[num_mods
]->mod_op
|= LDAP_MOD_REPLACE
;
143 mods
[num_mods
]->mod_type
= discard_const_p(char, el
->name
);
144 mods
[num_mods
]->mod_vals
.modv_bvals
= talloc_array(mods
[num_mods
],
147 if (!mods
[num_mods
]->mod_vals
.modv_bvals
) {
151 for (j
=0;j
<el
->num_values
;j
++) {
152 mods
[num_mods
]->mod_vals
.modv_bvals
[j
] = talloc(mods
[num_mods
]->mod_vals
.modv_bvals
,
154 if (!mods
[num_mods
]->mod_vals
.modv_bvals
[j
]) {
157 mods
[num_mods
]->mod_vals
.modv_bvals
[j
]->bv_val
=
158 (char *)el
->values
[j
].data
;
159 mods
[num_mods
]->mod_vals
.modv_bvals
[j
]->bv_len
= el
->values
[j
].length
;
161 mods
[num_mods
]->mod_vals
.modv_bvals
[j
] = NULL
;
173 add a single set of ldap message values to a ldb_message
175 static int lldb_add_msg_attr(struct ldb_context
*ldb
,
176 struct ldb_message
*msg
,
177 const char *attr
, struct berval
**bval
)
180 struct ldb_message_element
*el
;
182 count
= ldap_count_values_len(bval
);
188 el
= talloc_realloc(msg
, msg
->elements
, struct ldb_message_element
,
189 msg
->num_elements
+ 1);
197 el
= &msg
->elements
[msg
->num_elements
];
199 el
->name
= talloc_strdup(msg
->elements
, attr
);
207 el
->values
= talloc_array(msg
->elements
, struct ldb_val
, count
);
213 for (i
=0;i
<count
;i
++) {
214 /* we have to ensure this is null terminated so that
215 ldb_msg_find_attr_as_string() can work */
217 (uint8_t *)talloc_size(el
->values
, bval
[i
]->bv_len
+1);
218 if (!el
->values
[i
].data
) {
222 memcpy(el
->values
[i
].data
, bval
[i
]->bv_val
, bval
[i
]->bv_len
);
223 el
->values
[i
].data
[bval
[i
]->bv_len
] = 0;
224 el
->values
[i
].length
= bval
[i
]->bv_len
;
234 search for matching records
236 static int lldb_search(struct ldb_module
*module
, struct ldb_request
*req
)
238 struct lldb_private
*lldb
= talloc_get_type(module
->private_data
, struct lldb_private
);
239 struct lldb_context
*lldb_ac
;
246 if (!req
->callback
|| !req
->context
) {
247 ldb_set_errstring(module
->ldb
, "Async interface called with NULL callback function or NULL context");
248 return LDB_ERR_OPERATIONS_ERROR
;
251 if (req
->op
.search
.tree
== NULL
) {
252 ldb_set_errstring(module
->ldb
, "Invalid expression parse tree");
253 return LDB_ERR_OPERATIONS_ERROR
;
256 if (req
->controls
!= NULL
) {
257 ldb_debug(module
->ldb
, LDB_DEBUG_WARNING
, "Controls are not yet supported by ldb_ldap backend!\n");
260 req
->handle
= init_handle(lldb
, module
, req
->context
, req
->callback
, req
->timeout
, req
->starttime
);
261 if (req
->handle
== NULL
) {
262 return LDB_ERR_OPERATIONS_ERROR
;
265 lldb_ac
= talloc_get_type(req
->handle
->private_data
, struct lldb_context
);
267 search_base
= ldb_dn_linearize(lldb_ac
, req
->op
.search
.base
);
268 if (req
->op
.search
.base
== NULL
) {
269 search_base
= talloc_strdup(lldb_ac
, "");
271 if (search_base
== NULL
) {
272 return LDB_ERR_OPERATIONS_ERROR
;
275 expression
= ldb_filter_from_tree(
277 discard_const_p(struct ldb_parse_tree
, req
->op
.search
.tree
));
278 if (expression
== NULL
) {
279 return LDB_ERR_OPERATIONS_ERROR
;
282 switch (req
->op
.search
.scope
) {
284 ldap_scope
= LDAP_SCOPE_BASE
;
286 case LDB_SCOPE_ONELEVEL
:
287 ldap_scope
= LDAP_SCOPE_ONELEVEL
;
290 ldap_scope
= LDAP_SCOPE_SUBTREE
;
294 tv
.tv_sec
= req
->timeout
;
297 ret
= ldap_search_ext(lldb
->ldap
, search_base
, ldap_scope
,
299 discard_const_p(char *, req
->op
.search
.attrs
),
307 if (ret
!= LDAP_SUCCESS
) {
308 ldb_set_errstring(module
->ldb
, ldap_err2string(ret
));
311 return lldb_ldap_to_ldb(ret
);
317 static int lldb_add(struct ldb_module
*module
, struct ldb_request
*req
)
319 struct lldb_private
*lldb
= talloc_get_type(module
->private_data
, struct lldb_private
);
320 struct lldb_context
*lldb_ac
;
325 /* ltdb specials should not reach this point */
326 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
327 return LDB_ERR_INVALID_DN_SYNTAX
;
330 req
->handle
= init_handle(lldb
, module
, req
->context
, req
->callback
, req
->timeout
, req
->starttime
);
331 if (req
->handle
== NULL
) {
332 return LDB_ERR_OPERATIONS_ERROR
;
335 lldb_ac
= talloc_get_type(req
->handle
->private_data
, struct lldb_context
);
337 mods
= lldb_msg_to_mods(lldb_ac
, req
->op
.add
.message
, 0);
339 return LDB_ERR_OPERATIONS_ERROR
;
342 dn
= ldb_dn_linearize(lldb_ac
, req
->op
.add
.message
->dn
);
344 return LDB_ERR_OPERATIONS_ERROR
;
347 ret
= ldap_add_ext(lldb
->ldap
, dn
, mods
,
352 if (ret
!= LDAP_SUCCESS
) {
353 ldb_set_errstring(module
->ldb
, ldap_err2string(ret
));
356 return lldb_ldap_to_ldb(ret
);
362 static int lldb_modify(struct ldb_module
*module
, struct ldb_request
*req
)
364 struct lldb_private
*lldb
= talloc_get_type(module
->private_data
, struct lldb_private
);
365 struct lldb_context
*lldb_ac
;
370 /* ltdb specials should not reach this point */
371 if (ldb_dn_is_special(req
->op
.mod
.message
->dn
)) {
372 return LDB_ERR_INVALID_DN_SYNTAX
;
375 req
->handle
= init_handle(lldb
, module
, req
->context
, req
->callback
, req
->timeout
, req
->starttime
);
376 if (req
->handle
== NULL
) {
377 return LDB_ERR_OPERATIONS_ERROR
;
380 lldb_ac
= talloc_get_type(req
->handle
->private_data
, struct lldb_context
);
382 mods
= lldb_msg_to_mods(lldb_ac
, req
->op
.mod
.message
, 1);
384 return LDB_ERR_OPERATIONS_ERROR
;
387 dn
= ldb_dn_linearize(lldb_ac
, req
->op
.mod
.message
->dn
);
389 return LDB_ERR_OPERATIONS_ERROR
;
392 ret
= ldap_modify_ext(lldb
->ldap
, dn
, mods
,
397 if (ret
!= LDAP_SUCCESS
) {
398 ldb_set_errstring(module
->ldb
, ldap_err2string(ret
));
401 return lldb_ldap_to_ldb(ret
);
407 static int lldb_delete(struct ldb_module
*module
, struct ldb_request
*req
)
409 struct lldb_private
*lldb
= talloc_get_type(module
->private_data
, struct lldb_private
);
410 struct lldb_context
*lldb_ac
;
414 /* ltdb specials should not reach this point */
415 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
416 return LDB_ERR_INVALID_DN_SYNTAX
;
419 req
->handle
= init_handle(lldb
, module
, req
->context
, req
->callback
, req
->timeout
, req
->starttime
);
420 if (req
->handle
== NULL
) {
421 return LDB_ERR_OPERATIONS_ERROR
;
424 lldb_ac
= talloc_get_type(req
->handle
->private_data
, struct lldb_context
);
426 dnstr
= ldb_dn_linearize(lldb_ac
, req
->op
.del
.dn
);
428 ret
= ldap_delete_ext(lldb
->ldap
, dnstr
,
433 if (ret
!= LDAP_SUCCESS
) {
434 ldb_set_errstring(module
->ldb
, ldap_err2string(ret
));
437 return lldb_ldap_to_ldb(ret
);
443 static int lldb_rename(struct ldb_module
*module
, struct ldb_request
*req
)
445 struct lldb_private
*lldb
= talloc_get_type(module
->private_data
, struct lldb_private
);
446 struct lldb_context
*lldb_ac
;
452 /* ltdb specials should not reach this point */
453 if (ldb_dn_is_special(req
->op
.rename
.olddn
) || ldb_dn_is_special(req
->op
.rename
.newdn
)) {
454 return LDB_ERR_INVALID_DN_SYNTAX
;
457 req
->handle
= init_handle(lldb
, module
, req
->context
, req
->callback
, req
->timeout
, req
->starttime
);
458 if (req
->handle
== NULL
) {
459 return LDB_ERR_OPERATIONS_ERROR
;
462 lldb_ac
= talloc_get_type(req
->handle
->private_data
, struct lldb_context
);
464 old_dn
= ldb_dn_linearize(lldb_ac
, req
->op
.rename
.olddn
);
465 if (old_dn
== NULL
) {
466 return LDB_ERR_OPERATIONS_ERROR
;
469 newrdn
= talloc_asprintf(lldb_ac
, "%s=%s",
470 ldb_dn_get_rdn_name(req
->op
.rename
.newdn
),
471 ldb_dn_escape_value(lldb
, *(ldb_dn_get_rdn_val(req
->op
.rename
.newdn
))));
473 return LDB_ERR_OPERATIONS_ERROR
;
476 parentdn
= ldb_dn_linearize(lldb_ac
, ldb_dn_get_parent(lldb_ac
, req
->op
.rename
.newdn
));
478 return LDB_ERR_OPERATIONS_ERROR
;
481 ret
= ldap_rename(lldb
->ldap
, old_dn
, newrdn
, parentdn
,
485 if (ret
!= LDAP_SUCCESS
) {
486 ldb_set_errstring(module
->ldb
, ldap_err2string(ret
));
489 return lldb_ldap_to_ldb(ret
);
492 static int lldb_parse_result(struct ldb_handle
*handle
, LDAPMessage
*result
)
494 struct lldb_context
*ac
= talloc_get_type(handle
->private_data
, struct lldb_context
);
495 struct lldb_private
*lldb
= talloc_get_type(ac
->module
->private_data
, struct lldb_private
);
496 struct ldb_reply
*ares
= NULL
;
499 char *matcheddnp
= NULL
;
500 char *errmsgp
= NULL
;
501 char **referralsp
= NULL
;
502 LDAPControl
**serverctrlsp
= NULL
;
503 int ret
= LDB_SUCCESS
;
505 type
= ldap_msgtype(result
);
511 case LDAP_RES_SEARCH_ENTRY
:
512 msg
= ldap_first_entry(lldb
->ldap
, result
);
514 BerElement
*berptr
= NULL
;
517 ares
= talloc_zero(ac
, struct ldb_reply
);
519 ret
= LDB_ERR_OPERATIONS_ERROR
;
523 ares
->message
= ldb_msg_new(ares
);
524 if (!ares
->message
) {
525 ret
= LDB_ERR_OPERATIONS_ERROR
;
529 dn
= ldap_get_dn(lldb
->ldap
, msg
);
531 ret
= LDB_ERR_OPERATIONS_ERROR
;
534 ares
->message
->dn
= ldb_dn_explode_or_special(ares
->message
, dn
);
535 if (ares
->message
->dn
== NULL
) {
536 ret
= LDB_ERR_OPERATIONS_ERROR
;
541 ares
->message
->num_elements
= 0;
542 ares
->message
->elements
= NULL
;
543 ares
->message
->private_data
= NULL
;
545 /* loop over all attributes */
546 for (attr
=ldap_first_attribute(lldb
->ldap
, msg
, &berptr
);
548 attr
=ldap_next_attribute(lldb
->ldap
, msg
, berptr
)) {
549 struct berval
**bval
;
550 bval
= ldap_get_values_len(lldb
->ldap
, msg
, attr
);
553 lldb_add_msg_attr(ac
->module
->ldb
, ares
->message
, attr
, bval
);
554 ldap_value_free_len(bval
);
557 if (berptr
) ber_free(berptr
, 0);
560 ares
->type
= LDB_REPLY_ENTRY
;
561 ret
= ac
->callback(ac
->module
->ldb
, ac
->context
, ares
);
563 handle
->status
= LDB_ERR_PROTOCOL_ERROR
;
564 handle
->state
= LDB_ASYNC_DONE
;
568 case LDAP_RES_SEARCH_REFERENCE
:
569 if (ldap_parse_result(lldb
->ldap
, result
, &handle
->status
,
570 &matcheddnp
, &errmsgp
,
571 &referralsp
, &serverctrlsp
, 0) != LDAP_SUCCESS
) {
572 ret
= LDB_ERR_OPERATIONS_ERROR
;
575 if (referralsp
== NULL
) {
576 handle
->status
= LDB_ERR_PROTOCOL_ERROR
;
580 ares
= talloc_zero(ac
, struct ldb_reply
);
582 ret
= LDB_ERR_OPERATIONS_ERROR
;
586 ares
->referral
= talloc_strdup(ares
, *referralsp
);
587 ares
->type
= LDB_REPLY_REFERRAL
;
588 ret
= ac
->callback(ac
->module
->ldb
, ac
->context
, ares
);
592 case LDAP_RES_SEARCH_RESULT
:
593 if (ldap_parse_result(lldb
->ldap
, result
, &handle
->status
,
594 &matcheddnp
, &errmsgp
,
595 &referralsp
, &serverctrlsp
, 0) != LDAP_SUCCESS
) {
596 handle
->status
= LDB_ERR_OPERATIONS_ERROR
;
600 ares
= talloc_zero(ac
, struct ldb_reply
);
602 ret
= LDB_ERR_OPERATIONS_ERROR
;
606 if (serverctrlsp
!= NULL
) {
607 /* FIXME: transform the LDAPControl list into an ldb_control one */
608 ares
->controls
= NULL
;
611 ares
->type
= LDB_REPLY_DONE
;
612 handle
->state
= LDB_ASYNC_DONE
;
613 ret
= ac
->callback(ac
->module
->ldb
, ac
->context
, ares
);
617 case LDAP_RES_MODIFY
:
619 case LDAP_RES_DELETE
:
621 if (ldap_parse_result(lldb
->ldap
, result
, &handle
->status
,
622 &matcheddnp
, &errmsgp
,
623 &referralsp
, &serverctrlsp
, 0) != LDAP_SUCCESS
) {
624 handle
->status
= LDB_ERR_OPERATIONS_ERROR
;
627 if (ac
->callback
&& handle
->status
== LDB_SUCCESS
) {
628 ares
= NULL
; /* FIXME: build a corresponding ares to pass on */
629 ret
= ac
->callback(ac
->module
->ldb
, ac
->context
, ares
);
631 handle
->state
= LDB_ASYNC_DONE
;
635 ret
= LDB_ERR_PROTOCOL_ERROR
;
639 if (matcheddnp
) ldap_memfree(matcheddnp
);
640 if (errmsgp
&& *errmsgp
) {
641 ldb_set_errstring(ac
->module
->ldb
, errmsgp
);
642 } else if (handle
->status
) {
643 ldb_set_errstring(ac
->module
->ldb
, ldap_err2string(handle
->status
));
646 ldap_memfree(errmsgp
);
648 if (referralsp
) ldap_value_free(referralsp
);
649 if (serverctrlsp
) ldap_controls_free(serverctrlsp
);
651 ldap_msgfree(result
);
652 return lldb_ldap_to_ldb(handle
->status
);
655 handle
->state
= LDB_ASYNC_DONE
;
656 ldap_msgfree(result
);
660 static int lldb_wait(struct ldb_handle
*handle
, enum ldb_wait_type type
)
662 struct lldb_context
*ac
= talloc_get_type(handle
->private_data
, struct lldb_context
);
663 struct lldb_private
*lldb
= talloc_get_type(handle
->module
->private_data
, struct lldb_private
);
664 struct timeval timeout
;
668 if (handle
->state
== LDB_ASYNC_DONE
) {
669 return handle
->status
;
672 if (!ac
|| !ac
->msgid
) {
673 return LDB_ERR_OPERATIONS_ERROR
;
676 handle
->state
= LDB_ASYNC_PENDING
;
677 handle
->status
= LDB_SUCCESS
;
682 if ((ac
->timeout
!= -1) &&
683 ((ac
->starttime
+ ac
->timeout
) > time(NULL
))) {
684 return LDB_ERR_TIME_LIMIT_EXCEEDED
;
690 lret
= ldap_result(lldb
->ldap
, ac
->msgid
, 0, &timeout
, &result
);
692 return LDB_ERR_OPERATIONS_ERROR
;
699 return lldb_parse_result(handle
, result
);
703 ret
= LDB_ERR_OPERATIONS_ERROR
;
705 while (handle
->status
== LDB_SUCCESS
&& handle
->state
!= LDB_ASYNC_DONE
) {
707 if (ac
->timeout
== -1) {
708 lret
= ldap_result(lldb
->ldap
, ac
->msgid
, 0, NULL
, &result
);
710 timeout
.tv_sec
= ac
->timeout
- (time(NULL
) - ac
->starttime
);
711 if (timeout
.tv_sec
<= 0)
712 return LDB_ERR_TIME_LIMIT_EXCEEDED
;
713 lret
= ldap_result(lldb
->ldap
, ac
->msgid
, 0, &timeout
, &result
);
716 return LDB_ERR_OPERATIONS_ERROR
;
719 return LDB_ERR_TIME_LIMIT_EXCEEDED
;
722 ret
= lldb_parse_result(handle
, result
);
723 if (ret
!= LDB_SUCCESS
) {
731 handle
->state
= LDB_ASYNC_DONE
;
732 ret
= LDB_ERR_OPERATIONS_ERROR
;
739 static int lldb_start_trans(struct ldb_module
*module
)
741 /* TODO implement a local transaction mechanism here */
746 static int lldb_end_trans(struct ldb_module
*module
)
748 /* TODO implement a local transaction mechanism here */
753 static int lldb_del_trans(struct ldb_module
*module
)
755 /* TODO implement a local transaction mechanism here */
760 static int lldb_request(struct ldb_module
*module
, struct ldb_request
*req
)
762 return LDB_ERR_OPERATIONS_ERROR
;
765 static const struct ldb_module_ops lldb_ops
= {
767 .search
= lldb_search
,
769 .modify
= lldb_modify
,
771 .rename
= lldb_rename
,
772 .request
= lldb_request
,
773 .start_transaction
= lldb_start_trans
,
774 .end_transaction
= lldb_end_trans
,
775 .del_transaction
= lldb_del_trans
,
780 static int lldb_destructor(struct lldb_private
*lldb
)
782 ldap_unbind(lldb
->ldap
);
787 connect to the database
789 static int lldb_connect(struct ldb_context
*ldb
,
792 const char *options
[],
793 struct ldb_module
**module
)
795 struct lldb_private
*lldb
= NULL
;
799 lldb
= talloc(ldb
, struct lldb_private
);
807 ret
= ldap_initialize(&lldb
->ldap
, url
);
808 if (ret
!= LDAP_SUCCESS
) {
809 ldb_debug(ldb
, LDB_DEBUG_FATAL
, "ldap_initialize failed for URL '%s' - %s\n",
810 url
, ldap_err2string(ret
));
814 talloc_set_destructor(lldb
, lldb_destructor
);
816 ret
= ldap_set_option(lldb
->ldap
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
817 if (ret
!= LDAP_SUCCESS
) {
818 ldb_debug(ldb
, LDB_DEBUG_FATAL
, "ldap_set_option failed - %s\n",
819 ldap_err2string(ret
));
823 *module
= talloc(ldb
, struct ldb_module
);
824 if (*module
== NULL
) {
829 talloc_set_name_const(*module
, "ldb_ldap backend");
830 (*module
)->ldb
= ldb
;
831 (*module
)->prev
= (*module
)->next
= NULL
;
832 (*module
)->private_data
= lldb
;
833 (*module
)->ops
= &lldb_ops
;
842 int ldb_ldap_init(void)
844 return ldb_register_backend("ldap", lldb_connect
) +
845 ldb_register_backend("ldapi", lldb_connect
) +
846 ldb_register_backend("ldaps", lldb_connect
);