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