1 /* init.c - initialize ldap backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2007 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"
36 ldap_back_open( BackendInfo *bi )
38 bi->bi_controls = slap_known_controls;
43 ldap_back_initialize( BackendInfo *bi )
48 #ifdef LDAP_DYNAMIC_OBJECTS
49 /* this is set because all the support a proxy has to provide
50 * is the capability to forward the refresh exop, and to
51 * pass thru entries that contain the dynamicObject class
52 * and the entryTtl attribute */
54 #endif /* LDAP_DYNAMIC_OBJECTS */
57 bi->bi_open = ldap_back_open;
62 bi->bi_db_init = ldap_back_db_init;
63 bi->bi_db_config = config_generic_wrapper;
64 bi->bi_db_open = ldap_back_db_open;
65 bi->bi_db_close = ldap_back_db_close;
66 bi->bi_db_destroy = ldap_back_db_destroy;
68 bi->bi_op_bind = ldap_back_bind;
70 bi->bi_op_search = ldap_back_search;
71 bi->bi_op_compare = ldap_back_compare;
72 bi->bi_op_modify = ldap_back_modify;
73 bi->bi_op_modrdn = ldap_back_modrdn;
74 bi->bi_op_add = ldap_back_add;
75 bi->bi_op_delete = ldap_back_delete;
76 bi->bi_op_abandon = 0;
78 bi->bi_extended = ldap_back_extended;
80 bi->bi_chk_referrals = 0;
81 bi->bi_entry_get_rw = ldap_back_entry_get;
83 bi->bi_connection_init = 0;
84 bi->bi_connection_destroy = ldap_back_conn_destroy;
86 rc = chain_initialize();
92 rc = distproc_initialize();
98 return ldap_back_init_cf( bi );
102 ldap_back_db_init( Backend *be, ConfigReply *cr )
108 li = (ldapinfo_t *)ch_calloc( 1, sizeof( ldapinfo_t ) );
113 li->li_rebind_f = ldap_back_default_rebind;
114 li->li_urllist_f = ldap_back_default_urllist;
115 li->li_urllist_p = li;
116 ldap_pvt_thread_mutex_init( &li->li_uri_mutex );
118 BER_BVZERO( &li->li_acl_authcID );
119 BER_BVZERO( &li->li_acl_authcDN );
120 BER_BVZERO( &li->li_acl_passwd );
122 li->li_acl_authmethod = LDAP_AUTH_NONE;
123 BER_BVZERO( &li->li_acl_sasl_mech );
124 li->li_acl.sb_tls = SB_TLS_DEFAULT;
126 li->li_idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
128 BER_BVZERO( &li->li_idassert_authcID );
129 BER_BVZERO( &li->li_idassert_authcDN );
130 BER_BVZERO( &li->li_idassert_passwd );
132 BER_BVZERO( &li->li_idassert_authzID );
134 li->li_idassert_authmethod = LDAP_AUTH_NONE;
135 BER_BVZERO( &li->li_idassert_sasl_mech );
136 li->li_idassert_tls = SB_TLS_DEFAULT;
138 /* by default, use proxyAuthz control on each operation */
139 li->li_idassert_flags = LDAP_BACK_AUTH_PRESCRIPTIVE;
141 li->li_idassert_authz = NULL;
143 /* initialize flags */
144 li->li_flags = LDAP_BACK_F_CHASE_REFERRALS;
146 /* initialize version */
147 li->li_version = LDAP_VERSION3;
149 ldap_pvt_thread_mutex_init( &li->li_conninfo.lai_mutex );
151 for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
152 li->li_conn_priv[ i ].lic_num = 0;
153 LDAP_TAILQ_INIT( &li->li_conn_priv[ i ].lic_priv );
155 li->li_conn_priv_max = LDAP_BACK_CONN_PRIV_DEFAULT;
158 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_NOLASTMOD;
160 be->be_cf_ocs = be->bd_info->bi_cf_ocs;
162 rc = ldap_back_monitor_db_init( be );
172 ldap_back_db_open( BackendDB *be, ConfigReply *cr )
174 ldapinfo_t *li = (ldapinfo_t *)be->be_private;
176 slap_bindconf sb = { BER_BVNULL };
179 Debug( LDAP_DEBUG_TRACE,
180 "ldap_back_db_open: URI=%s\n",
181 li->li_uri != NULL ? li->li_uri : "", 0, 0 );
183 /* by default, use proxyAuthz control on each operation */
184 switch ( li->li_idassert_mode ) {
185 case LDAP_BACK_IDASSERT_LEGACY:
186 case LDAP_BACK_IDASSERT_SELF:
187 /* however, since admin connections are pooled and shared,
188 * only static authzIDs can be native */
189 li->li_idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
196 ber_str2bv( li->li_uri, 0, 0, &sb.sb_uri );
197 sb.sb_version = li->li_version;
198 sb.sb_method = LDAP_AUTH_SIMPLE;
199 BER_BVSTR( &sb.sb_binddn, "" );
201 if ( LDAP_BACK_T_F_DISCOVER( li ) && !LDAP_BACK_T_F( li ) ) {
202 rc = slap_discover_feature( &sb,
203 slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
204 LDAP_FEATURE_ABSOLUTE_FILTERS );
205 if ( rc == LDAP_COMPARE_TRUE ) {
206 li->li_flags |= LDAP_BACK_F_T_F;
210 if ( LDAP_BACK_CANCEL_DISCOVER( li ) && !LDAP_BACK_CANCEL( li ) ) {
211 rc = slap_discover_feature( &sb,
212 slap_schema.si_ad_supportedExtension->ad_cname.bv_val,
214 if ( rc == LDAP_COMPARE_TRUE ) {
215 li->li_flags |= LDAP_BACK_F_CANCEL_EXOP;
220 rc = ldap_back_monitor_db_open( be );
229 li->li_flags |= LDAP_BACK_F_ISOPEN;
236 ldap_back_conn_free( void *v_lc )
238 ldapconn_t *lc = v_lc;
240 if ( lc->lc_ld != NULL ) {
241 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
243 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
244 ch_free( lc->lc_bound_ndn.bv_val );
246 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
247 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
248 ch_free( lc->lc_cred.bv_val );
250 if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
251 ch_free( lc->lc_local_ndn.bv_val );
253 lc->lc_q.tqe_prev = NULL;
254 lc->lc_q.tqe_next = NULL;
259 ldap_back_db_close( Backend *be, ConfigReply *cr )
263 if ( be->be_private ) {
264 rc = ldap_back_monitor_db_close( be );
271 ldap_back_db_destroy( Backend *be, ConfigReply *cr )
273 if ( be->be_private ) {
274 ldapinfo_t *li = ( ldapinfo_t * )be->be_private;
277 (void)ldap_back_monitor_db_destroy( be );
279 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
281 if ( li->li_uri != NULL ) {
282 ch_free( li->li_uri );
285 assert( li->li_bvuri != NULL );
286 ber_bvarray_free( li->li_bvuri );
289 if ( !BER_BVISNULL( &li->li_acl_authcID ) ) {
290 ch_free( li->li_acl_authcID.bv_val );
291 BER_BVZERO( &li->li_acl_authcID );
293 if ( !BER_BVISNULL( &li->li_acl_authcDN ) ) {
294 ch_free( li->li_acl_authcDN.bv_val );
295 BER_BVZERO( &li->li_acl_authcDN );
297 if ( !BER_BVISNULL( &li->li_acl_passwd ) ) {
298 ch_free( li->li_acl_passwd.bv_val );
299 BER_BVZERO( &li->li_acl_passwd );
301 if ( !BER_BVISNULL( &li->li_acl_sasl_mech ) ) {
302 ch_free( li->li_acl_sasl_mech.bv_val );
303 BER_BVZERO( &li->li_acl_sasl_mech );
305 if ( !BER_BVISNULL( &li->li_acl_sasl_realm ) ) {
306 ch_free( li->li_acl_sasl_realm.bv_val );
307 BER_BVZERO( &li->li_acl_sasl_realm );
309 if ( !BER_BVISNULL( &li->li_idassert_authcID ) ) {
310 ch_free( li->li_idassert_authcID.bv_val );
311 BER_BVZERO( &li->li_idassert_authcID );
313 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
314 ch_free( li->li_idassert_authcDN.bv_val );
315 BER_BVZERO( &li->li_idassert_authcDN );
317 if ( !BER_BVISNULL( &li->li_idassert_passwd ) ) {
318 ch_free( li->li_idassert_passwd.bv_val );
319 BER_BVZERO( &li->li_idassert_passwd );
321 if ( !BER_BVISNULL( &li->li_idassert_authzID ) ) {
322 ch_free( li->li_idassert_authzID.bv_val );
323 BER_BVZERO( &li->li_idassert_authzID );
325 if ( !BER_BVISNULL( &li->li_idassert_sasl_mech ) ) {
326 ch_free( li->li_idassert_sasl_mech.bv_val );
327 BER_BVZERO( &li->li_idassert_sasl_mech );
329 if ( !BER_BVISNULL( &li->li_idassert_sasl_realm ) ) {
330 ch_free( li->li_idassert_sasl_realm.bv_val );
331 BER_BVZERO( &li->li_idassert_sasl_realm );
333 if ( li->li_idassert_authz != NULL ) {
334 ber_bvarray_free( li->li_idassert_authz );
335 li->li_idassert_authz = NULL;
337 if ( li->li_conninfo.lai_tree ) {
338 avl_free( li->li_conninfo.lai_tree, ldap_back_conn_free );
340 for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
341 while ( !LDAP_TAILQ_EMPTY( &li->li_conn_priv[ i ].lic_priv ) ) {
342 ldapconn_t *lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ i ].lic_priv );
344 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ i ].lic_priv, lc, lc_q );
345 ldap_back_conn_free( lc );
348 if ( LDAP_BACK_QUARANTINE( li ) ) {
349 slap_retry_info_destroy( &li->li_quarantine );
350 ldap_pvt_thread_mutex_destroy( &li->li_quarantine_mutex );
353 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
354 ldap_pvt_thread_mutex_destroy( &li->li_conninfo.lai_mutex );
355 ldap_pvt_thread_mutex_destroy( &li->li_uri_mutex );
358 ch_free( be->be_private );
363 #if SLAPD_LDAP == SLAPD_MOD_DYNAMIC
365 /* conditionally define the init_module() function */
366 SLAP_BACKEND_INIT_MODULE( ldap )
368 #endif /* SLAPD_LDAP == SLAPD_MOD_DYNAMIC */