1 /* init.c - initialize ldap backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2008 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>
32 #include "back-ldap.h"
35 ldap_back_open( BackendInfo *bi )
37 bi->bi_controls = slap_known_controls;
42 ldap_back_initialize( BackendInfo *bi )
44 bi->bi_open = ldap_back_open;
49 bi->bi_db_init = ldap_back_db_init;
50 bi->bi_db_config = config_generic_wrapper;
51 bi->bi_db_open = ldap_back_db_open;
53 bi->bi_db_destroy = ldap_back_db_destroy;
55 bi->bi_op_bind = ldap_back_bind;
57 bi->bi_op_search = ldap_back_search;
58 bi->bi_op_compare = ldap_back_compare;
59 bi->bi_op_modify = ldap_back_modify;
60 bi->bi_op_modrdn = ldap_back_modrdn;
61 bi->bi_op_add = ldap_back_add;
62 bi->bi_op_delete = ldap_back_delete;
63 bi->bi_op_abandon = 0;
65 bi->bi_extended = ldap_back_extended;
67 bi->bi_chk_referrals = 0;
68 bi->bi_entry_get_rw = ldap_back_entry_get;
70 bi->bi_connection_init = 0;
71 bi->bi_connection_destroy = ldap_back_conn_destroy;
77 return ldap_back_init_cf( bi );
81 ldap_back_db_init( Backend *be )
86 li = (ldapinfo_t *)ch_calloc( 1, sizeof( ldapinfo_t ) );
91 li->li_rebind_f = ldap_back_default_rebind;
92 ldap_pvt_thread_mutex_init( &li->li_uri_mutex );
94 BER_BVZERO( &li->li_acl_authcID );
95 BER_BVZERO( &li->li_acl_authcDN );
96 BER_BVZERO( &li->li_acl_passwd );
98 li->li_acl_authmethod = LDAP_AUTH_NONE;
99 BER_BVZERO( &li->li_acl_sasl_mech );
100 li->li_acl.sb_tls = SB_TLS_DEFAULT;
102 li->li_idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
104 BER_BVZERO( &li->li_idassert_authcID );
105 BER_BVZERO( &li->li_idassert_authcDN );
106 BER_BVZERO( &li->li_idassert_passwd );
108 BER_BVZERO( &li->li_idassert_authzID );
110 li->li_idassert_authmethod = LDAP_AUTH_NONE;
111 BER_BVZERO( &li->li_idassert_sasl_mech );
112 li->li_idassert_tls = SB_TLS_DEFAULT;
114 /* by default, use proxyAuthz control on each operation */
115 li->li_idassert_flags = LDAP_BACK_AUTH_PRESCRIPTIVE;
117 li->li_idassert_authz = NULL;
119 /* initialize flags */
120 li->li_flags = LDAP_BACK_F_CHASE_REFERRALS;
122 /* initialize version */
123 li->li_version = LDAP_VERSION3;
125 ldap_pvt_thread_mutex_init( &li->li_conninfo.lai_mutex );
127 for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
128 li->li_conn_priv[ i ].lic_num = 0;
129 LDAP_TAILQ_INIT( &li->li_conn_priv[ i ].lic_priv );
131 li->li_conn_priv_max = LDAP_BACK_CONN_PRIV_DEFAULT;
134 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_NOLASTMOD;
136 be->be_cf_ocs = be->bd_info->bi_cf_ocs;
142 ldap_back_db_open( BackendDB *be )
144 ldapinfo_t *li = (ldapinfo_t *)be->be_private;
146 Debug( LDAP_DEBUG_TRACE,
147 "ldap_back_db_open: URI=%s\n",
148 li->li_uri != NULL ? li->li_uri : "", 0, 0 );
150 /* by default, use proxyAuthz control on each operation */
151 switch ( li->li_idassert_mode ) {
152 case LDAP_BACK_IDASSERT_LEGACY:
153 case LDAP_BACK_IDASSERT_SELF:
154 /* however, since admin connections are pooled and shared,
155 * only static authzIDs can be native */
156 li->li_idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
163 if ( LDAP_BACK_T_F_DISCOVER( li ) && !LDAP_BACK_T_F( li ) ) {
166 rc = slap_discover_feature( li->li_uri, li->li_version,
167 slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
168 LDAP_FEATURE_ABSOLUTE_FILTERS );
169 if ( rc == LDAP_COMPARE_TRUE ) {
170 li->li_flags |= LDAP_BACK_F_T_F;
174 if ( LDAP_BACK_CANCEL_DISCOVER( li ) && !LDAP_BACK_CANCEL( li ) ) {
177 rc = slap_discover_feature( li->li_uri, li->li_version,
178 slap_schema.si_ad_supportedExtension->ad_cname.bv_val,
180 if ( rc == LDAP_COMPARE_TRUE ) {
181 li->li_flags |= LDAP_BACK_F_CANCEL_EXOP;
185 li->li_flags |= LDAP_BACK_F_ISOPEN;
191 ldap_back_conn_free( void *v_lc )
193 ldapconn_t *lc = v_lc;
195 if ( lc->lc_ld != NULL ) {
196 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
198 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
199 ch_free( lc->lc_bound_ndn.bv_val );
201 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
202 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
203 ch_free( lc->lc_cred.bv_val );
205 if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
206 ch_free( lc->lc_local_ndn.bv_val );
208 lc->lc_q.tqe_prev = NULL;
209 lc->lc_q.tqe_next = NULL;
214 ldap_back_db_destroy( Backend *be )
216 if ( be->be_private ) {
217 ldapinfo_t *li = ( ldapinfo_t * )be->be_private;
220 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
222 if ( li->li_uri != NULL ) {
223 ch_free( li->li_uri );
226 assert( li->li_bvuri != NULL );
227 ber_bvarray_free( li->li_bvuri );
230 if ( !BER_BVISNULL( &li->li_acl_authcID ) ) {
231 ch_free( li->li_acl_authcID.bv_val );
232 BER_BVZERO( &li->li_acl_authcID );
234 if ( !BER_BVISNULL( &li->li_acl_authcDN ) ) {
235 ch_free( li->li_acl_authcDN.bv_val );
236 BER_BVZERO( &li->li_acl_authcDN );
238 if ( !BER_BVISNULL( &li->li_acl_passwd ) ) {
239 ch_free( li->li_acl_passwd.bv_val );
240 BER_BVZERO( &li->li_acl_passwd );
242 if ( !BER_BVISNULL( &li->li_acl_sasl_mech ) ) {
243 ch_free( li->li_acl_sasl_mech.bv_val );
244 BER_BVZERO( &li->li_acl_sasl_mech );
246 if ( !BER_BVISNULL( &li->li_acl_sasl_realm ) ) {
247 ch_free( li->li_acl_sasl_realm.bv_val );
248 BER_BVZERO( &li->li_acl_sasl_realm );
250 if ( !BER_BVISNULL( &li->li_idassert_authcID ) ) {
251 ch_free( li->li_idassert_authcID.bv_val );
252 BER_BVZERO( &li->li_idassert_authcID );
254 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
255 ch_free( li->li_idassert_authcDN.bv_val );
256 BER_BVZERO( &li->li_idassert_authcDN );
258 if ( !BER_BVISNULL( &li->li_idassert_passwd ) ) {
259 ch_free( li->li_idassert_passwd.bv_val );
260 BER_BVZERO( &li->li_idassert_passwd );
262 if ( !BER_BVISNULL( &li->li_idassert_authzID ) ) {
263 ch_free( li->li_idassert_authzID.bv_val );
264 BER_BVZERO( &li->li_idassert_authzID );
266 if ( !BER_BVISNULL( &li->li_idassert_sasl_mech ) ) {
267 ch_free( li->li_idassert_sasl_mech.bv_val );
268 BER_BVZERO( &li->li_idassert_sasl_mech );
270 if ( !BER_BVISNULL( &li->li_idassert_sasl_realm ) ) {
271 ch_free( li->li_idassert_sasl_realm.bv_val );
272 BER_BVZERO( &li->li_idassert_sasl_realm );
274 if ( li->li_idassert_authz != NULL ) {
275 ber_bvarray_free( li->li_idassert_authz );
276 li->li_idassert_authz = NULL;
278 if ( li->li_conninfo.lai_tree ) {
279 avl_free( li->li_conninfo.lai_tree, ldap_back_conn_free );
281 for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
282 while ( !LDAP_TAILQ_EMPTY( &li->li_conn_priv[ i ].lic_priv ) ) {
283 ldapconn_t *lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ i ].lic_priv );
285 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ i ].lic_priv, lc, lc_q );
286 ldap_back_conn_free( lc );
289 if ( LDAP_BACK_QUARANTINE( li ) ) {
290 slap_retry_info_destroy( &li->li_quarantine );
291 ldap_pvt_thread_mutex_destroy( &li->li_quarantine_mutex );
294 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
295 ldap_pvt_thread_mutex_destroy( &li->li_conninfo.lai_mutex );
296 ldap_pvt_thread_mutex_destroy( &li->li_uri_mutex );
299 ch_free( be->be_private );
304 #if SLAPD_LDAP == SLAPD_MOD_DYNAMIC
306 /* conditionally define the init_module() function */
307 SLAP_BACKEND_INIT_MODULE( ldap )
309 #endif /* SLAPD_LDAP == SLAPD_MOD_DYNAMIC */