1/*	$NetBSD$	*/
2
3/* init.c - initialize ldap backend */
4/* OpenLDAP: pkg/ldap/servers/slapd/back-ldap/init.c,v 1.99.2.16 2010/04/15 20:25:48 quanah Exp */
5/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 2003-2010 The OpenLDAP Foundation.
8 * Portions Copyright 1999-2003 Howard Chu.
9 * Portions Copyright 2000-2003 Pierangelo Masarati.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted only as authorized by the OpenLDAP
14 * Public License.
15 *
16 * A copy of this license is available in the file LICENSE in the
17 * top-level directory of the distribution or, alternatively, at
18 * <http://www.OpenLDAP.org/license.html>.
19 */
20/* ACKNOWLEDGEMENTS:
21 * This work was initially developed by the Howard Chu for inclusion
22 * in OpenLDAP Software and subsequently enhanced by Pierangelo
23 * Masarati.
24 */
25
26#include "portable.h"
27
28#include <stdio.h>
29
30#include <ac/string.h>
31#include <ac/socket.h>
32
33#include "slap.h"
34#include "config.h"
35#include "back-ldap.h"
36
37static const ldap_extra_t ldap_extra = {
38	ldap_back_proxy_authz_ctrl,
39	ldap_back_controls_free,
40	slap_idassert_authzfrom_parse_cf,
41	slap_idassert_passthru_parse_cf,
42	slap_idassert_parse_cf,
43	slap_retry_info_destroy,
44	slap_retry_info_parse,
45	slap_retry_info_unparse,
46	ldap_back_connid2str
47};
48
49int
50ldap_back_open( BackendInfo	*bi )
51{
52	bi->bi_controls = slap_known_controls;
53	return 0;
54}
55
56int
57ldap_back_initialize( BackendInfo *bi )
58{
59	int		rc;
60
61	bi->bi_flags =
62#ifdef LDAP_DYNAMIC_OBJECTS
63		/* this is set because all the support a proxy has to provide
64		 * is the capability to forward the refresh exop, and to
65		 * pass thru entries that contain the dynamicObject class
66		 * and the entryTtl attribute */
67		SLAP_BFLAG_DYNAMIC |
68#endif /* LDAP_DYNAMIC_OBJECTS */
69
70		/* back-ldap recognizes RFC4525 increment;
71		 * let the remote server complain, if needed (ITS#5912) */
72		SLAP_BFLAG_INCREMENT;
73
74	bi->bi_open = ldap_back_open;
75	bi->bi_config = 0;
76	bi->bi_close = 0;
77	bi->bi_destroy = 0;
78
79	bi->bi_db_init = ldap_back_db_init;
80	bi->bi_db_config = config_generic_wrapper;
81	bi->bi_db_open = ldap_back_db_open;
82	bi->bi_db_close = ldap_back_db_close;
83	bi->bi_db_destroy = ldap_back_db_destroy;
84
85	bi->bi_op_bind = ldap_back_bind;
86	bi->bi_op_unbind = 0;
87	bi->bi_op_search = ldap_back_search;
88	bi->bi_op_compare = ldap_back_compare;
89	bi->bi_op_modify = ldap_back_modify;
90	bi->bi_op_modrdn = ldap_back_modrdn;
91	bi->bi_op_add = ldap_back_add;
92	bi->bi_op_delete = ldap_back_delete;
93	bi->bi_op_abandon = 0;
94
95	bi->bi_extended = ldap_back_extended;
96
97	bi->bi_chk_referrals = 0;
98	bi->bi_entry_get_rw = ldap_back_entry_get;
99
100	bi->bi_connection_init = 0;
101	bi->bi_connection_destroy = ldap_back_conn_destroy;
102
103	bi->bi_extra = (void *)&ldap_extra;
104
105	rc = chain_initialize();
106	if ( rc ) {
107		return rc;
108	}
109
110	rc = pbind_initialize();
111	if ( rc ) {
112		return rc;
113	}
114
115#ifdef SLAP_DISTPROC
116	rc = distproc_initialize();
117	if ( rc ) {
118		return rc;
119	}
120#endif
121
122	return ldap_back_init_cf( bi );
123}
124
125int
126ldap_back_db_init( Backend *be, ConfigReply *cr )
127{
128	ldapinfo_t	*li;
129	int		rc;
130	unsigned	i;
131
132	li = (ldapinfo_t *)ch_calloc( 1, sizeof( ldapinfo_t ) );
133	if ( li == NULL ) {
134 		return -1;
135 	}
136
137	li->li_rebind_f = ldap_back_default_rebind;
138	li->li_urllist_f = ldap_back_default_urllist;
139	li->li_urllist_p = li;
140	ldap_pvt_thread_mutex_init( &li->li_uri_mutex );
141
142	BER_BVZERO( &li->li_acl_authcID );
143	BER_BVZERO( &li->li_acl_authcDN );
144	BER_BVZERO( &li->li_acl_passwd );
145
146	li->li_acl_authmethod = LDAP_AUTH_NONE;
147	BER_BVZERO( &li->li_acl_sasl_mech );
148	li->li_acl.sb_tls = SB_TLS_DEFAULT;
149
150	li->li_idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
151
152	BER_BVZERO( &li->li_idassert_authcID );
153	BER_BVZERO( &li->li_idassert_authcDN );
154	BER_BVZERO( &li->li_idassert_passwd );
155
156	BER_BVZERO( &li->li_idassert_authzID );
157
158	li->li_idassert_authmethod = LDAP_AUTH_NONE;
159	BER_BVZERO( &li->li_idassert_sasl_mech );
160	li->li_idassert_tls = SB_TLS_DEFAULT;
161
162	/* by default, use proxyAuthz control on each operation */
163	li->li_idassert_flags = LDAP_BACK_AUTH_PRESCRIPTIVE;
164
165	li->li_idassert_authz = NULL;
166
167	/* initialize flags */
168	li->li_flags = LDAP_BACK_F_CHASE_REFERRALS;
169
170	/* initialize version */
171	li->li_version = LDAP_VERSION3;
172
173	ldap_pvt_thread_mutex_init( &li->li_conninfo.lai_mutex );
174
175	for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
176		li->li_conn_priv[ i ].lic_num = 0;
177		LDAP_TAILQ_INIT( &li->li_conn_priv[ i ].lic_priv );
178	}
179	li->li_conn_priv_max = LDAP_BACK_CONN_PRIV_DEFAULT;
180
181	be->be_private = li;
182	SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_NOLASTMOD;
183
184	be->be_cf_ocs = be->bd_info->bi_cf_ocs;
185
186	rc = ldap_back_monitor_db_init( be );
187	if ( rc != 0 ) {
188		/* ignore, by now */
189		rc = 0;
190	}
191
192	return rc;
193}
194
195int
196ldap_back_db_open( BackendDB *be, ConfigReply *cr )
197{
198	ldapinfo_t	*li = (ldapinfo_t *)be->be_private;
199
200	slap_bindconf	sb = { BER_BVNULL };
201	int		rc = 0;
202
203	Debug( LDAP_DEBUG_TRACE,
204		"ldap_back_db_open: URI=%s\n",
205		li->li_uri != NULL ? li->li_uri : "", 0, 0 );
206
207	/* by default, use proxyAuthz control on each operation */
208	switch ( li->li_idassert_mode ) {
209	case LDAP_BACK_IDASSERT_LEGACY:
210	case LDAP_BACK_IDASSERT_SELF:
211		/* however, since admin connections are pooled and shared,
212		 * only static authzIDs can be native */
213		li->li_idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
214		break;
215
216	default:
217		break;
218	}
219
220	ber_str2bv( li->li_uri, 0, 0, &sb.sb_uri );
221	sb.sb_version = li->li_version;
222	sb.sb_method = LDAP_AUTH_SIMPLE;
223	BER_BVSTR( &sb.sb_binddn, "" );
224
225	if ( LDAP_BACK_T_F_DISCOVER( li ) && !LDAP_BACK_T_F( li ) ) {
226		rc = slap_discover_feature( &sb,
227				slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
228				LDAP_FEATURE_ABSOLUTE_FILTERS );
229		if ( rc == LDAP_COMPARE_TRUE ) {
230			li->li_flags |= LDAP_BACK_F_T_F;
231		}
232	}
233
234	if ( LDAP_BACK_CANCEL_DISCOVER( li ) && !LDAP_BACK_CANCEL( li ) ) {
235		rc = slap_discover_feature( &sb,
236				slap_schema.si_ad_supportedExtension->ad_cname.bv_val,
237				LDAP_EXOP_CANCEL );
238		if ( rc == LDAP_COMPARE_TRUE ) {
239			li->li_flags |= LDAP_BACK_F_CANCEL_EXOP;
240		}
241	}
242
243	/* monitor setup */
244	rc = ldap_back_monitor_db_open( be );
245	if ( rc != 0 ) {
246		/* ignore by now */
247		rc = 0;
248	}
249
250	li->li_flags |= LDAP_BACK_F_ISOPEN;
251
252	return rc;
253}
254
255void
256ldap_back_conn_free( void *v_lc )
257{
258	ldapconn_t	*lc = v_lc;
259
260	if ( lc->lc_ld != NULL ) {
261		ldap_unbind_ext( lc->lc_ld, NULL, NULL );
262	}
263	if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
264		ch_free( lc->lc_bound_ndn.bv_val );
265	}
266	if ( !BER_BVISNULL( &lc->lc_cred ) ) {
267		memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
268		ch_free( lc->lc_cred.bv_val );
269	}
270	if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
271		ch_free( lc->lc_local_ndn.bv_val );
272	}
273	lc->lc_q.tqe_prev = NULL;
274	lc->lc_q.tqe_next = NULL;
275	ch_free( lc );
276}
277
278int
279ldap_back_db_close( Backend *be, ConfigReply *cr )
280{
281	int		rc = 0;
282
283	if ( be->be_private ) {
284		rc = ldap_back_monitor_db_close( be );
285	}
286
287	return rc;
288}
289
290int
291ldap_back_db_destroy( Backend *be, ConfigReply *cr )
292{
293	if ( be->be_private ) {
294		ldapinfo_t	*li = ( ldapinfo_t * )be->be_private;
295		unsigned	i;
296
297		(void)ldap_back_monitor_db_destroy( be );
298
299		ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
300
301		if ( li->li_uri != NULL ) {
302			ch_free( li->li_uri );
303			li->li_uri = NULL;
304
305			assert( li->li_bvuri != NULL );
306			ber_bvarray_free( li->li_bvuri );
307			li->li_bvuri = NULL;
308		}
309
310		bindconf_free( &li->li_tls );
311		bindconf_free( &li->li_acl );
312		bindconf_free( &li->li_idassert.si_bc );
313
314		if ( li->li_idassert_authz != NULL ) {
315			ber_bvarray_free( li->li_idassert_authz );
316			li->li_idassert_authz = NULL;
317		}
318               	if ( li->li_conninfo.lai_tree ) {
319			avl_free( li->li_conninfo.lai_tree, ldap_back_conn_free );
320		}
321		for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
322			while ( !LDAP_TAILQ_EMPTY( &li->li_conn_priv[ i ].lic_priv ) ) {
323				ldapconn_t	*lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ i ].lic_priv );
324
325				LDAP_TAILQ_REMOVE( &li->li_conn_priv[ i ].lic_priv, lc, lc_q );
326				ldap_back_conn_free( lc );
327			}
328		}
329		if ( LDAP_BACK_QUARANTINE( li ) ) {
330			slap_retry_info_destroy( &li->li_quarantine );
331			ldap_pvt_thread_mutex_destroy( &li->li_quarantine_mutex );
332		}
333
334		ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
335		ldap_pvt_thread_mutex_destroy( &li->li_conninfo.lai_mutex );
336		ldap_pvt_thread_mutex_destroy( &li->li_uri_mutex );
337	}
338
339	ch_free( be->be_private );
340
341	return 0;
342}
343
344#if SLAPD_LDAP == SLAPD_MOD_DYNAMIC
345
346/* conditionally define the init_module() function */
347SLAP_BACKEND_INIT_MODULE( ldap )
348
349#endif /* SLAPD_LDAP == SLAPD_MOD_DYNAMIC */
350
351