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