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