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