1 /* init.c - initialize ldap backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2010 The OpenLDAP Foundation.
6 * Portions Copyright 1999-2003 Howard Chu.
7 * Portions Copyright 2000-2003 Pierangelo Masarati.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by the Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
28 #include <ac/string.h>
29 #include <ac/socket.h>
33 #include "back-ldap.h"
35 static const ldap_extra_t ldap_extra = {
36 ldap_back_proxy_authz_ctrl,
37 ldap_back_controls_free,
38 slap_idassert_authzfrom_parse_cf,
39 slap_idassert_passthru_parse_cf,
40 slap_idassert_parse_cf,
41 slap_retry_info_destroy,
42 slap_retry_info_parse,
43 slap_retry_info_unparse,
48 ldap_back_open( BackendInfo *bi )
50 bi->bi_controls = slap_known_controls;
55 ldap_back_initialize( BackendInfo *bi )
60 #ifdef LDAP_DYNAMIC_OBJECTS
61 /* this is set because all the support a proxy has to provide
62 * is the capability to forward the refresh exop, and to
63 * pass thru entries that contain the dynamicObject class
64 * and the entryTtl attribute */
66 #endif /* LDAP_DYNAMIC_OBJECTS */
68 /* back-ldap recognizes RFC4525 increment;
69 * let the remote server complain, if needed (ITS#5912) */
72 bi->bi_open = ldap_back_open;
77 bi->bi_db_init = ldap_back_db_init;
78 bi->bi_db_config = config_generic_wrapper;
79 bi->bi_db_open = ldap_back_db_open;
80 bi->bi_db_close = ldap_back_db_close;
81 bi->bi_db_destroy = ldap_back_db_destroy;
83 bi->bi_op_bind = ldap_back_bind;
85 bi->bi_op_search = ldap_back_search;
86 bi->bi_op_compare = ldap_back_compare;
87 bi->bi_op_modify = ldap_back_modify;
88 bi->bi_op_modrdn = ldap_back_modrdn;
89 bi->bi_op_add = ldap_back_add;
90 bi->bi_op_delete = ldap_back_delete;
91 bi->bi_op_abandon = 0;
93 bi->bi_extended = ldap_back_extended;
95 bi->bi_chk_referrals = 0;
96 bi->bi_entry_get_rw = ldap_back_entry_get;
98 bi->bi_connection_init = 0;
99 bi->bi_connection_destroy = ldap_back_conn_destroy;
101 bi->bi_extra = (void *)&ldap_extra;
103 rc = chain_initialize();
108 rc = pbind_initialize();
114 rc = distproc_initialize();
120 return ldap_back_init_cf( bi );
124 ldap_back_db_init( Backend *be, ConfigReply *cr )
130 li = (ldapinfo_t *)ch_calloc( 1, sizeof( ldapinfo_t ) );
135 li->li_rebind_f = ldap_back_default_rebind;
136 li->li_urllist_f = ldap_back_default_urllist;
137 li->li_urllist_p = li;
138 ldap_pvt_thread_mutex_init( &li->li_uri_mutex );
140 BER_BVZERO( &li->li_acl_authcID );
141 BER_BVZERO( &li->li_acl_authcDN );
142 BER_BVZERO( &li->li_acl_passwd );
144 li->li_acl_authmethod = LDAP_AUTH_NONE;
145 BER_BVZERO( &li->li_acl_sasl_mech );
146 li->li_acl.sb_tls = SB_TLS_DEFAULT;
148 li->li_idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
150 BER_BVZERO( &li->li_idassert_authcID );
151 BER_BVZERO( &li->li_idassert_authcDN );
152 BER_BVZERO( &li->li_idassert_passwd );
154 BER_BVZERO( &li->li_idassert_authzID );
156 li->li_idassert_authmethod = LDAP_AUTH_NONE;
157 BER_BVZERO( &li->li_idassert_sasl_mech );
158 li->li_idassert_tls = SB_TLS_DEFAULT;
160 /* by default, use proxyAuthz control on each operation */
161 li->li_idassert_flags = LDAP_BACK_AUTH_PRESCRIPTIVE;
163 li->li_idassert_authz = NULL;
165 /* initialize flags */
166 li->li_flags = LDAP_BACK_F_CHASE_REFERRALS;
168 /* initialize version */
169 li->li_version = LDAP_VERSION3;
171 ldap_pvt_thread_mutex_init( &li->li_conninfo.lai_mutex );
173 for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
174 li->li_conn_priv[ i ].lic_num = 0;
175 LDAP_TAILQ_INIT( &li->li_conn_priv[ i ].lic_priv );
177 li->li_conn_priv_max = LDAP_BACK_CONN_PRIV_DEFAULT;
180 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_NOLASTMOD;
182 be->be_cf_ocs = be->bd_info->bi_cf_ocs;
184 rc = ldap_back_monitor_db_init( be );
194 ldap_back_db_open( BackendDB *be, ConfigReply *cr )
196 ldapinfo_t *li = (ldapinfo_t *)be->be_private;
198 slap_bindconf sb = { BER_BVNULL };
201 Debug( LDAP_DEBUG_TRACE,
202 "ldap_back_db_open: URI=%s\n",
203 li->li_uri != NULL ? li->li_uri : "", 0, 0 );
205 /* by default, use proxyAuthz control on each operation */
206 switch ( li->li_idassert_mode ) {
207 case LDAP_BACK_IDASSERT_LEGACY:
208 case LDAP_BACK_IDASSERT_SELF:
209 /* however, since admin connections are pooled and shared,
210 * only static authzIDs can be native */
211 li->li_idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
218 ber_str2bv( li->li_uri, 0, 0, &sb.sb_uri );
219 sb.sb_version = li->li_version;
220 sb.sb_method = LDAP_AUTH_SIMPLE;
221 BER_BVSTR( &sb.sb_binddn, "" );
223 if ( LDAP_BACK_T_F_DISCOVER( li ) && !LDAP_BACK_T_F( li ) ) {
224 rc = slap_discover_feature( &sb,
225 slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
226 LDAP_FEATURE_ABSOLUTE_FILTERS );
227 if ( rc == LDAP_COMPARE_TRUE ) {
228 li->li_flags |= LDAP_BACK_F_T_F;
232 if ( LDAP_BACK_CANCEL_DISCOVER( li ) && !LDAP_BACK_CANCEL( li ) ) {
233 rc = slap_discover_feature( &sb,
234 slap_schema.si_ad_supportedExtension->ad_cname.bv_val,
236 if ( rc == LDAP_COMPARE_TRUE ) {
237 li->li_flags |= LDAP_BACK_F_CANCEL_EXOP;
242 rc = ldap_back_monitor_db_open( be );
248 li->li_flags |= LDAP_BACK_F_ISOPEN;
254 ldap_back_conn_free( void *v_lc )
256 ldapconn_t *lc = v_lc;
258 if ( lc->lc_ld != NULL ) {
259 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
261 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
262 ch_free( lc->lc_bound_ndn.bv_val );
264 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
265 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
266 ch_free( lc->lc_cred.bv_val );
268 if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
269 ch_free( lc->lc_local_ndn.bv_val );
271 lc->lc_q.tqe_prev = NULL;
272 lc->lc_q.tqe_next = NULL;
277 ldap_back_db_close( Backend *be, ConfigReply *cr )
281 if ( be->be_private ) {
282 rc = ldap_back_monitor_db_close( be );
289 ldap_back_db_destroy( Backend *be, ConfigReply *cr )
291 if ( be->be_private ) {
292 ldapinfo_t *li = ( ldapinfo_t * )be->be_private;
295 (void)ldap_back_monitor_db_destroy( be );
297 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
299 if ( li->li_uri != NULL ) {
300 ch_free( li->li_uri );
303 assert( li->li_bvuri != NULL );
304 ber_bvarray_free( li->li_bvuri );
308 bindconf_free( &li->li_tls );
309 bindconf_free( &li->li_acl );
310 bindconf_free( &li->li_idassert.si_bc );
312 if ( li->li_idassert_authz != NULL ) {
313 ber_bvarray_free( li->li_idassert_authz );
314 li->li_idassert_authz = NULL;
316 if ( li->li_conninfo.lai_tree ) {
317 avl_free( li->li_conninfo.lai_tree, ldap_back_conn_free );
319 for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
320 while ( !LDAP_TAILQ_EMPTY( &li->li_conn_priv[ i ].lic_priv ) ) {
321 ldapconn_t *lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ i ].lic_priv );
323 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ i ].lic_priv, lc, lc_q );
324 ldap_back_conn_free( lc );
327 if ( LDAP_BACK_QUARANTINE( li ) ) {
328 slap_retry_info_destroy( &li->li_quarantine );
329 ldap_pvt_thread_mutex_destroy( &li->li_quarantine_mutex );
332 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
333 ldap_pvt_thread_mutex_destroy( &li->li_conninfo.lai_mutex );
334 ldap_pvt_thread_mutex_destroy( &li->li_uri_mutex );
337 ch_free( be->be_private );
342 #if SLAPD_LDAP == SLAPD_MOD_DYNAMIC
344 /* conditionally define the init_module() function */
345 SLAP_BACKEND_INIT_MODULE( ldap )
347 #endif /* SLAPD_LDAP == SLAPD_MOD_DYNAMIC */