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>
33 #include "back-ldap.h"
35 static const ldap_extra_t ldap_extra = {
36 ldap_back_proxy_authz_ctrl,
37 ldap_back_controls_free
41 ldap_back_open( BackendInfo *bi )
43 bi->bi_controls = slap_known_controls;
48 ldap_back_initialize( BackendInfo *bi )
53 #ifdef LDAP_DYNAMIC_OBJECTS
54 /* this is set because all the support a proxy has to provide
55 * is the capability to forward the refresh exop, and to
56 * pass thru entries that contain the dynamicObject class
57 * and the entryTtl attribute */
59 #endif /* LDAP_DYNAMIC_OBJECTS */
62 bi->bi_open = ldap_back_open;
67 bi->bi_db_init = ldap_back_db_init;
68 bi->bi_db_config = config_generic_wrapper;
69 bi->bi_db_open = ldap_back_db_open;
70 bi->bi_db_close = ldap_back_db_close;
71 bi->bi_db_destroy = ldap_back_db_destroy;
73 bi->bi_op_bind = ldap_back_bind;
75 bi->bi_op_search = ldap_back_search;
76 bi->bi_op_compare = ldap_back_compare;
77 bi->bi_op_modify = ldap_back_modify;
78 bi->bi_op_modrdn = ldap_back_modrdn;
79 bi->bi_op_add = ldap_back_add;
80 bi->bi_op_delete = ldap_back_delete;
81 bi->bi_op_abandon = 0;
83 bi->bi_extended = ldap_back_extended;
85 bi->bi_chk_referrals = 0;
86 bi->bi_entry_get_rw = ldap_back_entry_get;
88 bi->bi_connection_init = 0;
89 bi->bi_connection_destroy = ldap_back_conn_destroy;
91 bi->bi_extra = (void *)&ldap_extra;
93 rc = chain_initialize();
99 rc = distproc_initialize();
105 return ldap_back_init_cf( bi );
109 ldap_back_db_init( Backend *be, ConfigReply *cr )
115 li = (ldapinfo_t *)ch_calloc( 1, sizeof( ldapinfo_t ) );
120 li->li_rebind_f = ldap_back_default_rebind;
121 li->li_urllist_f = ldap_back_default_urllist;
122 li->li_urllist_p = li;
123 ldap_pvt_thread_mutex_init( &li->li_uri_mutex );
125 BER_BVZERO( &li->li_acl_authcID );
126 BER_BVZERO( &li->li_acl_authcDN );
127 BER_BVZERO( &li->li_acl_passwd );
129 li->li_acl_authmethod = LDAP_AUTH_NONE;
130 BER_BVZERO( &li->li_acl_sasl_mech );
131 li->li_acl.sb_tls = SB_TLS_DEFAULT;
133 li->li_idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
135 BER_BVZERO( &li->li_idassert_authcID );
136 BER_BVZERO( &li->li_idassert_authcDN );
137 BER_BVZERO( &li->li_idassert_passwd );
139 BER_BVZERO( &li->li_idassert_authzID );
141 li->li_idassert_authmethod = LDAP_AUTH_NONE;
142 BER_BVZERO( &li->li_idassert_sasl_mech );
143 li->li_idassert_tls = SB_TLS_DEFAULT;
145 /* by default, use proxyAuthz control on each operation */
146 li->li_idassert_flags = LDAP_BACK_AUTH_PRESCRIPTIVE;
148 li->li_idassert_authz = NULL;
150 /* initialize flags */
151 li->li_flags = LDAP_BACK_F_CHASE_REFERRALS;
153 /* initialize version */
154 li->li_version = LDAP_VERSION3;
156 ldap_pvt_thread_mutex_init( &li->li_conninfo.lai_mutex );
158 for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
159 li->li_conn_priv[ i ].lic_num = 0;
160 LDAP_TAILQ_INIT( &li->li_conn_priv[ i ].lic_priv );
162 li->li_conn_priv_max = LDAP_BACK_CONN_PRIV_DEFAULT;
165 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_NOLASTMOD;
167 be->be_cf_ocs = be->bd_info->bi_cf_ocs;
169 rc = ldap_back_monitor_db_init( be );
179 ldap_back_db_open( BackendDB *be, ConfigReply *cr )
181 ldapinfo_t *li = (ldapinfo_t *)be->be_private;
183 slap_bindconf sb = { BER_BVNULL };
186 Debug( LDAP_DEBUG_TRACE,
187 "ldap_back_db_open: URI=%s\n",
188 li->li_uri != NULL ? li->li_uri : "", 0, 0 );
190 /* by default, use proxyAuthz control on each operation */
191 switch ( li->li_idassert_mode ) {
192 case LDAP_BACK_IDASSERT_LEGACY:
193 case LDAP_BACK_IDASSERT_SELF:
194 /* however, since admin connections are pooled and shared,
195 * only static authzIDs can be native */
196 li->li_idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
203 ber_str2bv( li->li_uri, 0, 0, &sb.sb_uri );
204 sb.sb_version = li->li_version;
205 sb.sb_method = LDAP_AUTH_SIMPLE;
206 BER_BVSTR( &sb.sb_binddn, "" );
208 if ( LDAP_BACK_T_F_DISCOVER( li ) && !LDAP_BACK_T_F( li ) ) {
209 rc = slap_discover_feature( &sb,
210 slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
211 LDAP_FEATURE_ABSOLUTE_FILTERS );
212 if ( rc == LDAP_COMPARE_TRUE ) {
213 li->li_flags |= LDAP_BACK_F_T_F;
217 if ( LDAP_BACK_CANCEL_DISCOVER( li ) && !LDAP_BACK_CANCEL( li ) ) {
218 rc = slap_discover_feature( &sb,
219 slap_schema.si_ad_supportedExtension->ad_cname.bv_val,
221 if ( rc == LDAP_COMPARE_TRUE ) {
222 li->li_flags |= LDAP_BACK_F_CANCEL_EXOP;
227 rc = ldap_back_monitor_db_open( be );
236 li->li_flags |= LDAP_BACK_F_ISOPEN;
243 ldap_back_conn_free( void *v_lc )
245 ldapconn_t *lc = v_lc;
247 if ( lc->lc_ld != NULL ) {
248 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
250 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
251 ch_free( lc->lc_bound_ndn.bv_val );
253 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
254 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
255 ch_free( lc->lc_cred.bv_val );
257 if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
258 ch_free( lc->lc_local_ndn.bv_val );
260 lc->lc_q.tqe_prev = NULL;
261 lc->lc_q.tqe_next = NULL;
266 ldap_back_db_close( Backend *be, ConfigReply *cr )
270 if ( be->be_private ) {
271 rc = ldap_back_monitor_db_close( be );
278 ldap_back_db_destroy( Backend *be, ConfigReply *cr )
280 if ( be->be_private ) {
281 ldapinfo_t *li = ( ldapinfo_t * )be->be_private;
284 (void)ldap_back_monitor_db_destroy( be );
286 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
288 if ( li->li_uri != NULL ) {
289 ch_free( li->li_uri );
292 assert( li->li_bvuri != NULL );
293 ber_bvarray_free( li->li_bvuri );
296 if ( !BER_BVISNULL( &li->li_acl_authcID ) ) {
297 ch_free( li->li_acl_authcID.bv_val );
298 BER_BVZERO( &li->li_acl_authcID );
300 if ( !BER_BVISNULL( &li->li_acl_authcDN ) ) {
301 ch_free( li->li_acl_authcDN.bv_val );
302 BER_BVZERO( &li->li_acl_authcDN );
304 if ( !BER_BVISNULL( &li->li_acl_passwd ) ) {
305 ch_free( li->li_acl_passwd.bv_val );
306 BER_BVZERO( &li->li_acl_passwd );
308 if ( !BER_BVISNULL( &li->li_acl_sasl_mech ) ) {
309 ch_free( li->li_acl_sasl_mech.bv_val );
310 BER_BVZERO( &li->li_acl_sasl_mech );
312 if ( !BER_BVISNULL( &li->li_acl_sasl_realm ) ) {
313 ch_free( li->li_acl_sasl_realm.bv_val );
314 BER_BVZERO( &li->li_acl_sasl_realm );
316 if ( !BER_BVISNULL( &li->li_idassert_authcID ) ) {
317 ch_free( li->li_idassert_authcID.bv_val );
318 BER_BVZERO( &li->li_idassert_authcID );
320 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
321 ch_free( li->li_idassert_authcDN.bv_val );
322 BER_BVZERO( &li->li_idassert_authcDN );
324 if ( !BER_BVISNULL( &li->li_idassert_passwd ) ) {
325 ch_free( li->li_idassert_passwd.bv_val );
326 BER_BVZERO( &li->li_idassert_passwd );
328 if ( !BER_BVISNULL( &li->li_idassert_authzID ) ) {
329 ch_free( li->li_idassert_authzID.bv_val );
330 BER_BVZERO( &li->li_idassert_authzID );
332 if ( !BER_BVISNULL( &li->li_idassert_sasl_mech ) ) {
333 ch_free( li->li_idassert_sasl_mech.bv_val );
334 BER_BVZERO( &li->li_idassert_sasl_mech );
336 if ( !BER_BVISNULL( &li->li_idassert_sasl_realm ) ) {
337 ch_free( li->li_idassert_sasl_realm.bv_val );
338 BER_BVZERO( &li->li_idassert_sasl_realm );
340 if ( li->li_idassert_authz != NULL ) {
341 ber_bvarray_free( li->li_idassert_authz );
342 li->li_idassert_authz = NULL;
344 if ( li->li_conninfo.lai_tree ) {
345 avl_free( li->li_conninfo.lai_tree, ldap_back_conn_free );
347 for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
348 while ( !LDAP_TAILQ_EMPTY( &li->li_conn_priv[ i ].lic_priv ) ) {
349 ldapconn_t *lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ i ].lic_priv );
351 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ i ].lic_priv, lc, lc_q );
352 ldap_back_conn_free( lc );
355 if ( LDAP_BACK_QUARANTINE( li ) ) {
356 slap_retry_info_destroy( &li->li_quarantine );
357 ldap_pvt_thread_mutex_destroy( &li->li_quarantine_mutex );
360 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
361 ldap_pvt_thread_mutex_destroy( &li->li_conninfo.lai_mutex );
362 ldap_pvt_thread_mutex_destroy( &li->li_uri_mutex );
365 ch_free( be->be_private );
370 #if SLAPD_LDAP == SLAPD_MOD_DYNAMIC
372 /* conditionally define the init_module() function */
373 SLAP_BACKEND_INIT_MODULE( ldap )
375 #endif /* SLAPD_LDAP == SLAPD_MOD_DYNAMIC */