]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
7286cd222a76d0e2c334e7f69f9cdc74ba1388f3
[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-2005 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         bi->bi_open = ldap_back_open;
45         bi->bi_config = 0;
46         bi->bi_close = 0;
47         bi->bi_destroy = 0;
48
49         bi->bi_db_init = ldap_back_db_init;
50         bi->bi_db_config = ldap_back_db_config;
51         bi->bi_db_open = ldap_back_db_open;
52         bi->bi_db_close = 0;
53         bi->bi_db_destroy = ldap_back_db_destroy;
54
55         bi->bi_op_bind = ldap_back_bind;
56         bi->bi_op_unbind = 0;
57         bi->bi_op_search = ldap_back_search;
58         bi->bi_op_compare = ldap_back_compare;
59         bi->bi_op_modify = ldap_back_modify;
60         bi->bi_op_modrdn = ldap_back_modrdn;
61         bi->bi_op_add = ldap_back_add;
62         bi->bi_op_delete = ldap_back_delete;
63         bi->bi_op_abandon = 0;
64
65         bi->bi_extended = ldap_back_extended;
66
67         bi->bi_chk_referrals = 0;
68         bi->bi_entry_get_rw = ldap_back_entry_get;
69
70         bi->bi_connection_init = 0;
71         bi->bi_connection_destroy = ldap_back_conn_destroy;
72
73         if ( chain_init( ) ) {
74                 return -1;
75         }
76
77         return 0;
78 }
79
80 int
81 ldap_back_db_init( Backend *be )
82 {
83         struct ldapinfo *li;
84
85         li = (struct ldapinfo *)ch_calloc( 1, sizeof( struct ldapinfo ) );
86         if ( li == NULL ) {
87                 return -1;
88         }
89
90         BER_BVZERO( &li->acl_authcID );
91         BER_BVZERO( &li->acl_authcDN );
92         BER_BVZERO( &li->acl_passwd );
93
94         li->acl_authmethod = LDAP_AUTH_SIMPLE;
95         BER_BVZERO( &li->acl_sasl_mech );
96
97         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
98
99         BER_BVZERO( &li->idassert_authcID );
100         BER_BVZERO( &li->idassert_authcDN );
101         BER_BVZERO( &li->idassert_passwd );
102
103         BER_BVZERO( &li->idassert_authzID );
104
105         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
106         BER_BVZERO( &li->idassert_sasl_mech );
107
108         /* by default, use proxyAuthz control on each operation */
109         li->idassert_flags = LDAP_BACK_AUTH_NONE;
110
111         li->idassert_authz = NULL;
112
113         /* initialize flags */
114         li->flags = LDAP_BACK_F_CHASE_REFERRALS;
115
116         /* initialize version */
117         li->version = LDAP_VERSION3;
118
119         ldap_pvt_thread_mutex_init( &li->conn_mutex );
120
121         be->be_private = li;
122         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_NOLASTMOD;
123
124         return 0;
125 }
126
127 int
128 ldap_back_discover_t_f_support( const char *uri, int version )
129 {
130         LDAP            *ld;
131         LDAPMessage     *res = NULL, *entry;
132         int             rc, i;
133         struct berval   cred = BER_BVC( "" ),
134                         absoluteFilters = BER_BVC( LDAP_FEATURE_ABSOLUTE_FILTERS ),
135                         **values = NULL;
136         char            *attrs[ 2 ] = { "supportedFeatures", NULL };
137
138         rc = ldap_initialize( &ld, uri );
139         if ( rc != LDAP_SUCCESS ) {
140                 return rc;
141         }
142
143         rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
144         if ( rc != LDAP_SUCCESS ) {
145                 goto done;
146         }
147
148         rc = ldap_sasl_bind_s( ld, "", LDAP_SASL_SIMPLE,
149                         &cred, NULL, NULL, NULL );
150         if ( rc != LDAP_SUCCESS ) {
151                 goto done;
152         }
153
154         rc = ldap_search_ext_s( ld, "", LDAP_SCOPE_BASE, "(objectClass=*)",
155                         attrs, 0, NULL, NULL, NULL, 0, &res );
156         if ( rc != LDAP_SUCCESS ) {
157                 goto done;
158         }
159
160         entry = ldap_first_entry( ld, res );
161         if ( entry == NULL ) {
162                 goto done;
163         }
164
165         values = ldap_get_values_len( ld, entry, attrs[ 0 ] );
166         if ( values == NULL ) {
167                 rc = LDAP_NO_SUCH_ATTRIBUTE;
168                 goto done;
169         }
170
171         for ( i = 0; values[ i ] != NULL; i++ ) {
172                 if ( bvmatch( &absoluteFilters, values[ i ] ) ) {
173                         rc = LDAP_COMPARE_TRUE;
174                         goto done;
175                 }
176         }
177
178         rc = LDAP_COMPARE_FALSE;
179
180 done:;
181         if ( values != NULL ) {
182                 ldap_value_free_len( values );
183         }
184
185         if ( res != NULL ) {
186                 ldap_msgfree( res );
187         }
188
189         ldap_unbind_ext( ld, NULL, NULL );
190
191         return rc;
192 }
193
194 int
195 ldap_back_db_open( BackendDB *be )
196 {
197         struct ldapinfo *li = (struct ldapinfo *)be->be_private;
198
199         Debug( LDAP_DEBUG_TRACE,
200                 "ldap_back_db_open: URI=%s\n",
201                 li->url != NULL ? li->url : "", 0, 0 );
202
203         /* by default, use proxyAuthz control on each operation */
204         switch ( li->idassert_mode ) {
205         case LDAP_BACK_IDASSERT_LEGACY:
206         case LDAP_BACK_IDASSERT_SELF:
207                 /* however, since admin connections are pooled and shared,
208                  * only static authzIDs can be native */
209                 li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
210                 break;
211
212         default:
213                 break;
214         }
215
216 #if 0 && defined(SLAPD_MONITOR)
217         {
218                 /* FIXME: disabled because namingContexts doesn't have
219                  * a matching rule, and using an MRA filter doesn't work
220                  * because the normalized assertion is compared to the 
221                  * non-normalized value, which in general differs from
222                  * the normalized one.  See ITS#3406 */
223                 struct berval   filter,
224                                 base = BER_BVC( "cn=Databases," SLAPD_MONITOR );
225                 struct berval   vals[ 2 ];
226                 Attribute       a = { 0 };
227
228                 filter.bv_len = STRLENOF( "(&(namingContexts:distinguishedNameMatch:=)(monitoredInfo=ldap))" )
229                         + be->be_nsuffix[ 0 ].bv_len;
230                 filter.bv_val = ch_malloc( filter.bv_len + 1 );
231                 snprintf( filter.bv_val, filter.bv_len + 1,
232                                 "(&(namingContexts:distinguishedNameMatch:=%s)(monitoredInfo=ldap))",
233                                 be->be_nsuffix[ 0 ].bv_val );
234
235                 a.a_desc = slap_schema.si_ad_labeledURI;
236                 ber_str2bv( li->url, 0, 0, &vals[ 0 ] );
237                 BER_BVZERO( &vals[ 1 ] );
238                 a.a_vals = vals;
239                 a.a_nvals = vals;
240                 if ( monitor_back_register_entry_attrs( NULL, &a, NULL, &base, LDAP_SCOPE_SUBTREE, &filter ) ) {
241                         /* error */
242                 }
243
244                 ch_free( filter.bv_val );
245         }
246 #endif /* SLAPD_MONITOR */
247
248         if ( li->flags & LDAP_BACK_F_SUPPORT_T_F_DISCOVER ) {
249                 int             rc;
250
251                 li->flags &= ~LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
252
253                 rc = ldap_back_discover_t_f_support( li->url, li->version );
254                 if ( rc == LDAP_COMPARE_TRUE ) {
255                         li->flags |= LDAP_BACK_F_SUPPORT_T_F;
256                 }
257         }
258
259         return 0;
260 }
261
262 void
263 ldap_back_conn_free( void *v_lc )
264 {
265         struct ldapconn *lc = v_lc;
266         
267         ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
268         if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
269                 ch_free( lc->lc_bound_ndn.bv_val );
270         }
271         if ( !BER_BVISNULL( &lc->lc_cred ) ) {
272                 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
273                 ch_free( lc->lc_cred.bv_val );
274         }
275         if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
276                 ch_free( lc->lc_local_ndn.bv_val );
277         }
278         ldap_pvt_thread_mutex_destroy( &lc->lc_mutex );
279         ch_free( lc );
280 }
281
282 int
283 ldap_back_db_destroy(
284     Backend     *be
285 )
286 {
287         struct ldapinfo *li;
288
289         if ( be->be_private ) {
290                 li = ( struct ldapinfo * )be->be_private;
291
292                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
293
294                 if ( li->url != NULL ) {
295                         ch_free( li->url );
296                         li->url = NULL;
297                 }
298                 if ( li->lud ) {
299                         ldap_free_urldesc( li->lud );
300                         li->lud = NULL;
301                 }
302                 if ( !BER_BVISNULL( &li->acl_authcID ) ) {
303                         ch_free( li->acl_authcID.bv_val );
304                         BER_BVZERO( &li->acl_authcID );
305                 }
306                 if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
307                         ch_free( li->acl_authcDN.bv_val );
308                         BER_BVZERO( &li->acl_authcDN );
309                 }
310                 if ( !BER_BVISNULL( &li->acl_passwd ) ) {
311                         ch_free( li->acl_passwd.bv_val );
312                         BER_BVZERO( &li->acl_passwd );
313                 }
314                 if ( !BER_BVISNULL( &li->acl_sasl_mech ) ) {
315                         ch_free( li->acl_sasl_mech.bv_val );
316                         BER_BVZERO( &li->acl_sasl_mech );
317                 }
318                 if ( !BER_BVISNULL( &li->acl_sasl_realm ) ) {
319                         ch_free( li->acl_sasl_realm.bv_val );
320                         BER_BVZERO( &li->acl_sasl_realm );
321                 }
322                 if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
323                         ch_free( li->idassert_authcID.bv_val );
324                         BER_BVZERO( &li->idassert_authcID );
325                 }
326                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
327                         ch_free( li->idassert_authcDN.bv_val );
328                         BER_BVZERO( &li->idassert_authcDN );
329                 }
330                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
331                         ch_free( li->idassert_passwd.bv_val );
332                         BER_BVZERO( &li->idassert_passwd );
333                 }
334                 if ( !BER_BVISNULL( &li->idassert_authzID ) ) {
335                         ch_free( li->idassert_authzID.bv_val );
336                         BER_BVZERO( &li->idassert_authzID );
337                 }
338                 if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
339                         ch_free( li->idassert_sasl_mech.bv_val );
340                         BER_BVZERO( &li->idassert_sasl_mech );
341                 }
342                 if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
343                         ch_free( li->idassert_sasl_realm.bv_val );
344                         BER_BVZERO( &li->idassert_sasl_realm );
345                 }
346                 if ( li->conntree ) {
347                         avl_free( li->conntree, ldap_back_conn_free );
348                 }
349
350                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
351                 ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
352         }
353
354         ch_free( be->be_private );
355
356         return 0;
357 }
358
359 #if SLAPD_LDAP == SLAPD_MOD_DYNAMIC
360
361 /* conditionally define the init_module() function */
362 SLAP_BACKEND_INIT_MODULE( ldap )
363
364 #endif /* SLAPD_LDAP == SLAPD_MOD_DYNAMIC */
365