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