]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
Sync with HEAD
[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_authcDN );
91         BER_BVZERO( &li->acl_passwd );
92
93         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
94
95         BER_BVZERO( &li->idassert_authcID );
96         BER_BVZERO( &li->idassert_authcDN );
97         BER_BVZERO( &li->idassert_passwd );
98
99         BER_BVZERO( &li->idassert_authzID );
100         li->idassert_authz = NULL;
101
102         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
103         li->idassert_sasl_flags = LDAP_SASL_QUIET;
104         BER_BVZERO( &li->idassert_sasl_mech );
105         BER_BVZERO( &li->idassert_sasl_realm );
106
107         li->idassert_ppolicy = 0;
108
109         /* by default, use proxyAuthz control on each operation */
110         li->idassert_flags = LDAP_BACK_AUTH_NONE;
111
112         /* initialize flags */
113         li->flags = LDAP_BACK_F_CHASE_REFERRALS;
114
115         ldap_pvt_thread_mutex_init( &li->conn_mutex );
116
117         be->be_private = li;
118         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_NOLASTMOD;
119
120         return 0;
121 }
122
123 int
124 ldap_back_db_open( BackendDB *be )
125 {
126         struct ldapinfo *li = (struct ldapinfo *)be->be_private;
127
128         Debug( LDAP_DEBUG_TRACE,
129                 "ldap_back_db_open: URI=%s\n",
130                 li->url != NULL ? li->url : "", 0, 0 );
131
132         /* by default, use proxyAuthz control on each operation */
133         switch ( li->idassert_mode ) {
134         case LDAP_BACK_IDASSERT_LEGACY:
135         case LDAP_BACK_IDASSERT_SELF:
136                 /* however, since admin connections are pooled and shared,
137                  * only static authzIDs can be native */
138                 li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
139                 break;
140
141         default:
142                 break;
143         }
144
145 #if 0 && defined(SLAPD_MONITOR)
146         {
147                 /* FIXME: disabled because namingContexts doesn't have
148                  * a matching rule, and using an MRA filter doesn't work
149                  * because the normalized assertion is compared to the 
150                  * non-normalized value, which in general differs from
151                  * the normalized one.  See ITS#3406 */
152                 struct berval   filter,
153                                 base = BER_BVC( "cn=Databases," SLAPD_MONITOR );
154                 struct berval   vals[ 2 ];
155                 Attribute       a = { 0 };
156
157                 filter.bv_len = STRLENOF( "(&(namingContexts:distinguishedNameMatch:=)(monitoredInfo=ldap))" )
158                         + be->be_nsuffix[ 0 ].bv_len;
159                 filter.bv_val = ch_malloc( filter.bv_len + 1 );
160                 snprintf( filter.bv_val, filter.bv_len + 1,
161                                 "(&(namingContexts:distinguishedNameMatch:=%s)(monitoredInfo=ldap))",
162                                 be->be_nsuffix[ 0 ].bv_val );
163
164                 a.a_desc = slap_schema.si_ad_labeledURI;
165                 ber_str2bv( li->url, 0, 0, &vals[ 0 ] );
166                 BER_BVZERO( &vals[ 1 ] );
167                 a.a_vals = vals;
168                 a.a_nvals = vals;
169                 if ( monitor_back_register_entry_attrs( NULL, &a, NULL, &base, LDAP_SCOPE_SUBTREE, &filter ) ) {
170                         /* error */
171                 }
172
173                 ch_free( filter.bv_val );
174         }
175 #endif /* SLAPD_MONITOR */
176
177         return 0;
178 }
179
180 void
181 ldap_back_conn_free( void *v_lc )
182 {
183         struct ldapconn *lc = v_lc;
184         
185         ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
186         if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
187                 ch_free( lc->lc_bound_ndn.bv_val );
188         }
189         if ( !BER_BVISNULL( &lc->lc_cred ) ) {
190                 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
191                 ch_free( lc->lc_cred.bv_val );
192         }
193         if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
194                 ch_free( lc->lc_local_ndn.bv_val );
195         }
196         ldap_pvt_thread_mutex_destroy( &lc->lc_mutex );
197         ch_free( lc );
198 }
199
200 int
201 ldap_back_db_destroy(
202     Backend     *be
203 )
204 {
205         struct ldapinfo *li;
206
207         if ( be->be_private ) {
208                 li = ( struct ldapinfo * )be->be_private;
209
210                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
211
212                 if ( li->url != NULL ) {
213                         ch_free( li->url );
214                         li->url = NULL;
215                 }
216                 if ( li->lud ) {
217                         ldap_free_urldesc( li->lud );
218                         li->lud = NULL;
219                 }
220                 if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
221                         ch_free( li->acl_authcDN.bv_val );
222                         BER_BVZERO( &li->acl_authcDN );
223                 }
224                 if ( !BER_BVISNULL( &li->acl_passwd ) ) {
225                         ch_free( li->acl_passwd.bv_val );
226                         BER_BVZERO( &li->acl_passwd );
227                 }
228                 if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
229                         ch_free( li->idassert_authcID.bv_val );
230                         BER_BVZERO( &li->idassert_authcID );
231                 }
232                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
233                         ch_free( li->idassert_authcDN.bv_val );
234                         BER_BVZERO( &li->idassert_authcDN );
235                 }
236                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
237                         ch_free( li->idassert_passwd.bv_val );
238                         BER_BVZERO( &li->idassert_passwd );
239                 }
240                 if ( !BER_BVISNULL( &li->idassert_authzID ) ) {
241                         ch_free( li->idassert_authzID.bv_val );
242                         BER_BVZERO( &li->idassert_authzID );
243                 }
244                 if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
245                         ch_free( li->idassert_sasl_mech.bv_val );
246                         BER_BVZERO( &li->idassert_sasl_mech );
247                 }
248                 if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
249                         ch_free( li->idassert_sasl_realm.bv_val );
250                         BER_BVZERO( &li->idassert_sasl_realm );
251                 }
252                 if ( li->conntree ) {
253                         avl_free( li->conntree, ldap_back_conn_free );
254                 }
255
256                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
257                 ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
258         }
259
260         ch_free( be->be_private );
261
262         return 0;
263 }
264
265 #if SLAPD_LDAP == SLAPD_MOD_DYNAMIC
266
267 /* conditionally define the init_module() function */
268 SLAP_BACKEND_INIT_MODULE( ldap )
269
270 #endif /* SLAPD_LDAP == SLAPD_MOD_DYNAMIC */
271