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