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