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