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