Debian Codegraph @

servers/slapd/back-ldap/init.c

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