]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
8c2110b38359261365bfa11f2e10edd300abaa58
[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         rc = pbind_initialize();
108         if ( rc ) {
109                 return rc;
110         }
111
112 #ifdef SLAP_DISTPROC
113         rc = distproc_initialize();
114         if ( rc ) {
115                 return rc;
116         }
117 #endif
118
119         return ldap_back_init_cf( bi );
120 }
121
122 int
123 ldap_back_db_init( Backend *be, ConfigReply *cr )
124 {
125         ldapinfo_t      *li;
126         int             rc;
127         unsigned        i;
128
129         li = (ldapinfo_t *)ch_calloc( 1, sizeof( ldapinfo_t ) );
130         if ( li == NULL ) {
131                 return -1;
132         }
133
134         li->li_rebind_f = ldap_back_default_rebind;
135         li->li_urllist_f = ldap_back_default_urllist;
136         li->li_urllist_p = li;
137         ldap_pvt_thread_mutex_init( &li->li_uri_mutex );
138
139         BER_BVZERO( &li->li_acl_authcID );
140         BER_BVZERO( &li->li_acl_authcDN );
141         BER_BVZERO( &li->li_acl_passwd );
142
143         li->li_acl_authmethod = LDAP_AUTH_NONE;
144         BER_BVZERO( &li->li_acl_sasl_mech );
145         li->li_acl.sb_tls = SB_TLS_DEFAULT;
146
147         li->li_idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
148
149         BER_BVZERO( &li->li_idassert_authcID );
150         BER_BVZERO( &li->li_idassert_authcDN );
151         BER_BVZERO( &li->li_idassert_passwd );
152
153         BER_BVZERO( &li->li_idassert_authzID );
154
155         li->li_idassert_authmethod = LDAP_AUTH_NONE;
156         BER_BVZERO( &li->li_idassert_sasl_mech );
157         li->li_idassert_tls = SB_TLS_DEFAULT;
158
159         /* by default, use proxyAuthz control on each operation */
160         li->li_idassert_flags = LDAP_BACK_AUTH_PRESCRIPTIVE;
161
162         li->li_idassert_authz = NULL;
163
164         /* initialize flags */
165         li->li_flags = LDAP_BACK_F_CHASE_REFERRALS;
166
167         /* initialize version */
168         li->li_version = LDAP_VERSION3;
169
170         ldap_pvt_thread_mutex_init( &li->li_conninfo.lai_mutex );
171
172         for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
173                 li->li_conn_priv[ i ].lic_num = 0;
174                 LDAP_TAILQ_INIT( &li->li_conn_priv[ i ].lic_priv );
175         }
176         li->li_conn_priv_max = LDAP_BACK_CONN_PRIV_DEFAULT;
177
178         be->be_private = li;
179         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_NOLASTMOD;
180
181         be->be_cf_ocs = be->bd_info->bi_cf_ocs;
182
183         rc = ldap_back_monitor_db_init( be );
184         if ( rc != 0 ) {
185                 /* ignore, by now */
186                 rc = 0;
187         }
188
189         return rc;
190 }
191
192 int
193 ldap_back_db_open( BackendDB *be, ConfigReply *cr )
194 {
195         ldapinfo_t      *li = (ldapinfo_t *)be->be_private;
196
197         slap_bindconf   sb = { BER_BVNULL };
198         int             rc = 0;
199
200         Debug( LDAP_DEBUG_TRACE,
201                 "ldap_back_db_open: URI=%s\n",
202                 li->li_uri != NULL ? li->li_uri : "", 0, 0 );
203
204         /* by default, use proxyAuthz control on each operation */
205         switch ( li->li_idassert_mode ) {
206         case LDAP_BACK_IDASSERT_LEGACY:
207         case LDAP_BACK_IDASSERT_SELF:
208                 /* however, since admin connections are pooled and shared,
209                  * only static authzIDs can be native */
210                 li->li_idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
211                 break;
212
213         default:
214                 break;
215         }
216
217         ber_str2bv( li->li_uri, 0, 0, &sb.sb_uri );
218         sb.sb_version = li->li_version;
219         sb.sb_method = LDAP_AUTH_SIMPLE;
220         BER_BVSTR( &sb.sb_binddn, "" );
221
222         if ( LDAP_BACK_T_F_DISCOVER( li ) && !LDAP_BACK_T_F( li ) ) {
223                 rc = slap_discover_feature( &sb,
224                                 slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
225                                 LDAP_FEATURE_ABSOLUTE_FILTERS );
226                 if ( rc == LDAP_COMPARE_TRUE ) {
227                         li->li_flags |= LDAP_BACK_F_T_F;
228                 }
229         }
230
231         if ( LDAP_BACK_CANCEL_DISCOVER( li ) && !LDAP_BACK_CANCEL( li ) ) {
232                 rc = slap_discover_feature( &sb,
233                                 slap_schema.si_ad_supportedExtension->ad_cname.bv_val,
234                                 LDAP_EXOP_CANCEL );
235                 if ( rc == LDAP_COMPARE_TRUE ) {
236                         li->li_flags |= LDAP_BACK_F_CANCEL_EXOP;
237                 }
238         }
239
240         /* monitor setup */
241         rc = ldap_back_monitor_db_open( be );
242         if ( rc != 0 ) {
243                 /* ignore by now */
244                 rc = 0;
245         }
246
247         li->li_flags |= LDAP_BACK_F_ISOPEN;
248
249         return rc;
250 }
251
252 void
253 ldap_back_conn_free( void *v_lc )
254 {
255         ldapconn_t      *lc = v_lc;
256
257         if ( lc->lc_ld != NULL ) {      
258                 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
259         }
260         if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
261                 ch_free( lc->lc_bound_ndn.bv_val );
262         }
263         if ( !BER_BVISNULL( &lc->lc_cred ) ) {
264                 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
265                 ch_free( lc->lc_cred.bv_val );
266         }
267         if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
268                 ch_free( lc->lc_local_ndn.bv_val );
269         }
270         lc->lc_q.tqe_prev = NULL;
271         lc->lc_q.tqe_next = NULL;
272         ch_free( lc );
273 }
274
275 int
276 ldap_back_db_close( Backend *be, ConfigReply *cr )
277 {
278         int             rc = 0;
279
280         if ( be->be_private ) {
281                 rc = ldap_back_monitor_db_close( be );
282         }
283
284         return rc;
285 }
286
287 int
288 ldap_back_db_destroy( Backend *be, ConfigReply *cr )
289 {
290         if ( be->be_private ) {
291                 ldapinfo_t      *li = ( ldapinfo_t * )be->be_private;
292                 unsigned        i;
293
294                 (void)ldap_back_monitor_db_destroy( be );
295
296                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
297
298                 if ( li->li_uri != NULL ) {
299                         ch_free( li->li_uri );
300                         li->li_uri = NULL;
301
302                         assert( li->li_bvuri != NULL );
303                         ber_bvarray_free( li->li_bvuri );
304                         li->li_bvuri = NULL;
305                 }
306
307                 bindconf_free( &li->li_tls );
308                 bindconf_free( &li->li_acl );
309                 bindconf_free( &li->li_idassert.si_bc );
310
311                 if ( li->li_idassert_authz != NULL ) {
312                         ber_bvarray_free( li->li_idassert_authz );
313                         li->li_idassert_authz = NULL;
314                 }
315                 if ( li->li_conninfo.lai_tree ) {
316                         avl_free( li->li_conninfo.lai_tree, ldap_back_conn_free );
317                 }
318                 for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
319                         while ( !LDAP_TAILQ_EMPTY( &li->li_conn_priv[ i ].lic_priv ) ) {
320                                 ldapconn_t      *lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ i ].lic_priv );
321
322                                 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ i ].lic_priv, lc, lc_q );
323                                 ldap_back_conn_free( lc );
324                         }
325                 }
326                 if ( LDAP_BACK_QUARANTINE( li ) ) {
327                         slap_retry_info_destroy( &li->li_quarantine );
328                         ldap_pvt_thread_mutex_destroy( &li->li_quarantine_mutex );
329                 }
330
331                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
332                 ldap_pvt_thread_mutex_destroy( &li->li_conninfo.lai_mutex );
333                 ldap_pvt_thread_mutex_destroy( &li->li_uri_mutex );
334         }
335
336         ch_free( be->be_private );
337
338         return 0;
339 }
340
341 #if SLAPD_LDAP == SLAPD_MOD_DYNAMIC
342
343 /* conditionally define the init_module() function */
344 SLAP_BACKEND_INIT_MODULE( ldap )
345
346 #endif /* SLAPD_LDAP == SLAPD_MOD_DYNAMIC */
347