]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
remove yields
[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_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 = config_generic_wrapper;
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 ldap_back_init_cf( bi );
78 }
79
80 int
81 ldap_back_db_init( Backend *be )
82 {
83         ldapinfo_t      *li;
84
85         li = (ldapinfo_t *)ch_calloc( 1, sizeof( ldapinfo_t ) );
86         if ( li == NULL ) {
87                 return -1;
88         }
89
90         BER_BVZERO( &li->li_acl_authcID );
91         BER_BVZERO( &li->li_acl_authcDN );
92         BER_BVZERO( &li->li_acl_passwd );
93
94         li->li_acl_authmethod = LDAP_AUTH_NONE;
95         BER_BVZERO( &li->li_acl_sasl_mech );
96         li->li_acl.sb_tls = SB_TLS_DEFAULT;
97
98         li->li_idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
99
100         BER_BVZERO( &li->li_idassert_authcID );
101         BER_BVZERO( &li->li_idassert_authcDN );
102         BER_BVZERO( &li->li_idassert_passwd );
103
104         BER_BVZERO( &li->li_idassert_authzID );
105
106         li->li_idassert_authmethod = LDAP_AUTH_NONE;
107         BER_BVZERO( &li->li_idassert_sasl_mech );
108         li->li_idassert.sb_tls = SB_TLS_DEFAULT;
109
110         /* by default, use proxyAuthz control on each operation */
111         li->li_idassert_flags = LDAP_BACK_AUTH_PRESCRIPTIVE;
112
113         li->li_idassert_authz = NULL;
114
115         /* initialize flags */
116         li->li_flags = LDAP_BACK_F_CHASE_REFERRALS;
117
118         /* initialize version */
119         li->li_version = LDAP_VERSION3;
120
121         ldap_pvt_thread_mutex_init( &li->li_conninfo.lai_mutex );
122
123         be->be_private = li;
124         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_NOLASTMOD;
125
126         be->be_cf_ocs = be->bd_info->bi_cf_ocs;
127
128         return 0;
129 }
130
131 int
132 ldap_back_db_open( BackendDB *be )
133 {
134         ldapinfo_t      *li = (ldapinfo_t *)be->be_private;
135
136         Debug( LDAP_DEBUG_TRACE,
137                 "ldap_back_db_open: URI=%s\n",
138                 li->li_uri != NULL ? li->li_uri : "", 0, 0 );
139
140         /* by default, use proxyAuthz control on each operation */
141         switch ( li->li_idassert_mode ) {
142         case LDAP_BACK_IDASSERT_LEGACY:
143         case LDAP_BACK_IDASSERT_SELF:
144                 /* however, since admin connections are pooled and shared,
145                  * only static authzIDs can be native */
146                 li->li_idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
147                 break;
148
149         default:
150                 break;
151         }
152
153 #if 0 && defined(SLAPD_MONITOR)
154         {
155                 /* FIXME: disabled because namingContexts doesn't have
156                  * a matching rule, and using an MRA filter doesn't work
157                  * because the normalized assertion is compared to the 
158                  * non-normalized value, which in general differs from
159                  * the normalized one.  See ITS#3406 */
160                 struct berval   filter,
161                                 base = BER_BVC( "cn=Databases," SLAPD_MONITOR );
162                 Attribute       a = { 0 };
163
164                 filter.bv_len = STRLENOF( "(&(namingContexts:distinguishedNameMatch:=)(monitoredInfo=ldap))" )
165                         + be->be_nsuffix[ 0 ].bv_len;
166                 filter.bv_val = ch_malloc( filter.bv_len + 1 );
167                 snprintf( filter.bv_val, filter.bv_len + 1,
168                                 "(&(namingContexts:distinguishedNameMatch:=%s)(monitoredInfo=ldap))",
169                                 be->be_nsuffix[ 0 ].bv_val );
170
171                 a.a_desc = slap_schema.si_ad_labeledURI;
172                 a.a_vals = li->li_bvuri;
173                 a.a_nvals = li->li_bvuri;
174                 if ( monitor_back_register_entry_attrs( NULL, &a, NULL, &base, LDAP_SCOPE_SUBTREE, &filter ) ) {
175                         /* error */
176                 }
177
178                 ch_free( filter.bv_val );
179         }
180 #endif /* SLAPD_MONITOR */
181
182         if ( li->li_flags & LDAP_BACK_F_SUPPORT_T_F_DISCOVER ) {
183                 int             rc;
184
185                 li->li_flags &= ~LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
186
187                 rc = slap_discover_feature( li->li_uri, li->li_version,
188                                 slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
189                                 LDAP_FEATURE_ABSOLUTE_FILTERS );
190                 if ( rc == LDAP_COMPARE_TRUE ) {
191                         li->li_flags |= LDAP_BACK_F_SUPPORT_T_F;
192                 }
193         }
194
195         return 0;
196 }
197
198 void
199 ldap_back_conn_free( void *v_lc )
200 {
201         ldapconn_t      *lc = v_lc;
202
203         if ( lc->lc_ld != NULL ) {      
204                 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
205         }
206         if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
207                 ch_free( lc->lc_bound_ndn.bv_val );
208         }
209         if ( !BER_BVISNULL( &lc->lc_cred ) ) {
210                 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
211                 ch_free( lc->lc_cred.bv_val );
212         }
213         if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
214                 ch_free( lc->lc_local_ndn.bv_val );
215         }
216         ch_free( lc );
217 }
218
219 int
220 ldap_back_db_destroy(
221     Backend     *be
222 )
223 {
224         if ( be->be_private ) {
225                 ldapinfo_t      *li = ( ldapinfo_t * )be->be_private;
226
227                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
228
229                 if ( li->li_uri != NULL ) {
230                         ch_free( li->li_uri );
231                         li->li_uri = NULL;
232
233                         assert( li->li_bvuri != NULL );
234                         ber_bvarray_free( li->li_bvuri );
235                         li->li_bvuri = NULL;
236                 }
237                 if ( !BER_BVISNULL( &li->li_acl_authcID ) ) {
238                         ch_free( li->li_acl_authcID.bv_val );
239                         BER_BVZERO( &li->li_acl_authcID );
240                 }
241                 if ( !BER_BVISNULL( &li->li_acl_authcDN ) ) {
242                         ch_free( li->li_acl_authcDN.bv_val );
243                         BER_BVZERO( &li->li_acl_authcDN );
244                 }
245                 if ( !BER_BVISNULL( &li->li_acl_passwd ) ) {
246                         ch_free( li->li_acl_passwd.bv_val );
247                         BER_BVZERO( &li->li_acl_passwd );
248                 }
249                 if ( !BER_BVISNULL( &li->li_acl_sasl_mech ) ) {
250                         ch_free( li->li_acl_sasl_mech.bv_val );
251                         BER_BVZERO( &li->li_acl_sasl_mech );
252                 }
253                 if ( !BER_BVISNULL( &li->li_acl_sasl_realm ) ) {
254                         ch_free( li->li_acl_sasl_realm.bv_val );
255                         BER_BVZERO( &li->li_acl_sasl_realm );
256                 }
257                 if ( !BER_BVISNULL( &li->li_idassert_authcID ) ) {
258                         ch_free( li->li_idassert_authcID.bv_val );
259                         BER_BVZERO( &li->li_idassert_authcID );
260                 }
261                 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
262                         ch_free( li->li_idassert_authcDN.bv_val );
263                         BER_BVZERO( &li->li_idassert_authcDN );
264                 }
265                 if ( !BER_BVISNULL( &li->li_idassert_passwd ) ) {
266                         ch_free( li->li_idassert_passwd.bv_val );
267                         BER_BVZERO( &li->li_idassert_passwd );
268                 }
269                 if ( !BER_BVISNULL( &li->li_idassert_authzID ) ) {
270                         ch_free( li->li_idassert_authzID.bv_val );
271                         BER_BVZERO( &li->li_idassert_authzID );
272                 }
273                 if ( !BER_BVISNULL( &li->li_idassert_sasl_mech ) ) {
274                         ch_free( li->li_idassert_sasl_mech.bv_val );
275                         BER_BVZERO( &li->li_idassert_sasl_mech );
276                 }
277                 if ( !BER_BVISNULL( &li->li_idassert_sasl_realm ) ) {
278                         ch_free( li->li_idassert_sasl_realm.bv_val );
279                         BER_BVZERO( &li->li_idassert_sasl_realm );
280                 }
281                 if ( li->li_idassert_authz != NULL ) {
282                         ber_bvarray_free( li->li_idassert_authz );
283                         li->li_idassert_authz = NULL;
284                 }
285                 if ( li->li_conninfo.lai_tree ) {
286                         avl_free( li->li_conninfo.lai_tree, ldap_back_conn_free );
287                 }
288
289                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
290                 ldap_pvt_thread_mutex_destroy( &li->li_conninfo.lai_mutex );
291         }
292
293         ch_free( be->be_private );
294
295         return 0;
296 }
297
298 #if SLAPD_LDAP == SLAPD_MOD_DYNAMIC
299
300 /* conditionally define the init_module() function */
301 SLAP_BACKEND_INIT_MODULE( ldap )
302
303 #endif /* SLAPD_LDAP == SLAPD_MOD_DYNAMIC */
304