]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
fix previous commit :)
[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 = 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         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_NONE;
95         BER_BVZERO( &li->acl_sasl_mech );
96         li->acl_sb.sb_tls = SB_TLS_DEFAULT;
97
98         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
99
100         BER_BVZERO( &li->idassert_authcID );
101         BER_BVZERO( &li->idassert_authcDN );
102         BER_BVZERO( &li->idassert_passwd );
103
104         BER_BVZERO( &li->idassert_authzID );
105
106         li->idassert_authmethod = LDAP_AUTH_NONE;
107         BER_BVZERO( &li->idassert_sasl_mech );
108         li->idassert_sb.sb_tls = SB_TLS_DEFAULT;
109
110         /* by default, use proxyAuthz control on each operation */
111         li->idassert_flags = LDAP_BACK_AUTH_NONE;
112
113         li->idassert_authz = NULL;
114
115         /* initialize flags */
116         li->flags = LDAP_BACK_F_CHASE_REFERRALS;
117
118         /* initialize version */
119         li->version = LDAP_VERSION3;
120
121         ldap_pvt_thread_mutex_init( &li->conn_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         struct ldapinfo *li = (struct ldapinfo *)be->be_private;
135
136         Debug( LDAP_DEBUG_TRACE,
137                 "ldap_back_db_open: URI=%s\n",
138                 li->url != NULL ? li->url : "", 0, 0 );
139
140         /* by default, use proxyAuthz control on each operation */
141         switch ( 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->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                 struct berval   vals[ 2 ];
163                 Attribute       a = { 0 };
164
165                 filter.bv_len = STRLENOF( "(&(namingContexts:distinguishedNameMatch:=)(monitoredInfo=ldap))" )
166                         + be->be_nsuffix[ 0 ].bv_len;
167                 filter.bv_val = ch_malloc( filter.bv_len + 1 );
168                 snprintf( filter.bv_val, filter.bv_len + 1,
169                                 "(&(namingContexts:distinguishedNameMatch:=%s)(monitoredInfo=ldap))",
170                                 be->be_nsuffix[ 0 ].bv_val );
171
172                 a.a_desc = slap_schema.si_ad_labeledURI;
173                 ber_str2bv( li->url, 0, 0, &vals[ 0 ] );
174                 BER_BVZERO( &vals[ 1 ] );
175                 a.a_vals = vals;
176                 a.a_nvals = vals;
177                 if ( monitor_back_register_entry_attrs( NULL, &a, NULL, &base, LDAP_SCOPE_SUBTREE, &filter ) ) {
178                         /* error */
179                 }
180
181                 ch_free( filter.bv_val );
182         }
183 #endif /* SLAPD_MONITOR */
184
185         if ( li->flags & LDAP_BACK_F_SUPPORT_T_F_DISCOVER ) {
186                 int             rc;
187
188                 li->flags &= ~LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
189
190                 rc = slap_discover_feature( li->url, li->version,
191                                 slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
192                                 LDAP_FEATURE_ABSOLUTE_FILTERS );
193                 if ( rc == LDAP_COMPARE_TRUE ) {
194                         li->flags |= LDAP_BACK_F_SUPPORT_T_F;
195                 }
196         }
197
198         return 0;
199 }
200
201 void
202 ldap_back_conn_free( void *v_lc )
203 {
204         struct ldapconn *lc = v_lc;
205         
206         ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
207         if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
208                 ch_free( lc->lc_bound_ndn.bv_val );
209         }
210         if ( !BER_BVISNULL( &lc->lc_cred ) ) {
211                 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
212                 ch_free( lc->lc_cred.bv_val );
213         }
214         if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
215                 ch_free( lc->lc_local_ndn.bv_val );
216         }
217         ldap_pvt_thread_mutex_destroy( &lc->lc_mutex );
218         ch_free( lc );
219 }
220
221 int
222 ldap_back_db_destroy(
223     Backend     *be
224 )
225 {
226         struct ldapinfo *li;
227
228         if ( be->be_private ) {
229                 li = ( struct ldapinfo * )be->be_private;
230
231                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
232
233                 if ( li->url != NULL ) {
234                         ch_free( li->url );
235                         li->url = NULL;
236                 }
237                 if ( li->lud ) {
238                         ldap_free_urldesc( li->lud );
239                         li->lud = NULL;
240                 }
241                 if ( !BER_BVISNULL( &li->acl_authcID ) ) {
242                         ch_free( li->acl_authcID.bv_val );
243                         BER_BVZERO( &li->acl_authcID );
244                 }
245                 if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
246                         ch_free( li->acl_authcDN.bv_val );
247                         BER_BVZERO( &li->acl_authcDN );
248                 }
249                 if ( !BER_BVISNULL( &li->acl_passwd ) ) {
250                         ch_free( li->acl_passwd.bv_val );
251                         BER_BVZERO( &li->acl_passwd );
252                 }
253                 if ( !BER_BVISNULL( &li->acl_sasl_mech ) ) {
254                         ch_free( li->acl_sasl_mech.bv_val );
255                         BER_BVZERO( &li->acl_sasl_mech );
256                 }
257                 if ( !BER_BVISNULL( &li->acl_sasl_realm ) ) {
258                         ch_free( li->acl_sasl_realm.bv_val );
259                         BER_BVZERO( &li->acl_sasl_realm );
260                 }
261                 if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
262                         ch_free( li->idassert_authcID.bv_val );
263                         BER_BVZERO( &li->idassert_authcID );
264                 }
265                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
266                         ch_free( li->idassert_authcDN.bv_val );
267                         BER_BVZERO( &li->idassert_authcDN );
268                 }
269                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
270                         ch_free( li->idassert_passwd.bv_val );
271                         BER_BVZERO( &li->idassert_passwd );
272                 }
273                 if ( !BER_BVISNULL( &li->idassert_authzID ) ) {
274                         ch_free( li->idassert_authzID.bv_val );
275                         BER_BVZERO( &li->idassert_authzID );
276                 }
277                 if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
278                         ch_free( li->idassert_sasl_mech.bv_val );
279                         BER_BVZERO( &li->idassert_sasl_mech );
280                 }
281                 if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
282                         ch_free( li->idassert_sasl_realm.bv_val );
283                         BER_BVZERO( &li->idassert_sasl_realm );
284                 }
285                 if ( li->conntree ) {
286                         avl_free( li->conntree, ldap_back_conn_free );
287                 }
288
289                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
290                 ldap_pvt_thread_mutex_destroy( &li->conn_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