]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
ITS#5728 release entries before send_ldap_ber()
[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-2008 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         unsigned        i;
85
86         li = (ldapinfo_t *)ch_calloc( 1, sizeof( ldapinfo_t ) );
87         if ( li == NULL ) {
88                 return -1;
89         }
90
91         li->li_rebind_f = ldap_back_default_rebind;
92         ldap_pvt_thread_mutex_init( &li->li_uri_mutex );
93
94         BER_BVZERO( &li->li_acl_authcID );
95         BER_BVZERO( &li->li_acl_authcDN );
96         BER_BVZERO( &li->li_acl_passwd );
97
98         li->li_acl_authmethod = LDAP_AUTH_NONE;
99         BER_BVZERO( &li->li_acl_sasl_mech );
100         li->li_acl.sb_tls = SB_TLS_DEFAULT;
101
102         li->li_idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
103
104         BER_BVZERO( &li->li_idassert_authcID );
105         BER_BVZERO( &li->li_idassert_authcDN );
106         BER_BVZERO( &li->li_idassert_passwd );
107
108         BER_BVZERO( &li->li_idassert_authzID );
109
110         li->li_idassert_authmethod = LDAP_AUTH_NONE;
111         BER_BVZERO( &li->li_idassert_sasl_mech );
112         li->li_idassert_tls = SB_TLS_DEFAULT;
113
114         /* by default, use proxyAuthz control on each operation */
115         li->li_idassert_flags = LDAP_BACK_AUTH_PRESCRIPTIVE;
116
117         li->li_idassert_authz = NULL;
118
119         /* initialize flags */
120         li->li_flags = LDAP_BACK_F_CHASE_REFERRALS;
121
122         /* initialize version */
123         li->li_version = LDAP_VERSION3;
124
125         ldap_pvt_thread_mutex_init( &li->li_conninfo.lai_mutex );
126
127         for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
128                 li->li_conn_priv[ i ].lic_num = 0;
129                 LDAP_TAILQ_INIT( &li->li_conn_priv[ i ].lic_priv );
130         }
131         li->li_conn_priv_max = LDAP_BACK_CONN_PRIV_DEFAULT;
132
133         be->be_private = li;
134         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_NOLASTMOD;
135
136         be->be_cf_ocs = be->bd_info->bi_cf_ocs;
137
138         return 0;
139 }
140
141 int
142 ldap_back_db_open( BackendDB *be )
143 {
144         ldapinfo_t      *li = (ldapinfo_t *)be->be_private;
145
146         Debug( LDAP_DEBUG_TRACE,
147                 "ldap_back_db_open: URI=%s\n",
148                 li->li_uri != NULL ? li->li_uri : "", 0, 0 );
149
150         /* by default, use proxyAuthz control on each operation */
151         switch ( li->li_idassert_mode ) {
152         case LDAP_BACK_IDASSERT_LEGACY:
153         case LDAP_BACK_IDASSERT_SELF:
154                 /* however, since admin connections are pooled and shared,
155                  * only static authzIDs can be native */
156                 li->li_idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
157                 break;
158
159         default:
160                 break;
161         }
162
163         if ( LDAP_BACK_T_F_DISCOVER( li ) && !LDAP_BACK_T_F( li ) ) {
164                 int             rc;
165
166                 rc = slap_discover_feature( li->li_uri, li->li_version,
167                                 slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
168                                 LDAP_FEATURE_ABSOLUTE_FILTERS );
169                 if ( rc == LDAP_COMPARE_TRUE ) {
170                         li->li_flags |= LDAP_BACK_F_T_F;
171                 }
172         }
173
174         if ( LDAP_BACK_CANCEL_DISCOVER( li ) && !LDAP_BACK_CANCEL( li ) ) {
175                 int             rc;
176
177                 rc = slap_discover_feature( li->li_uri, li->li_version,
178                                 slap_schema.si_ad_supportedExtension->ad_cname.bv_val,
179                                 LDAP_EXOP_CANCEL );
180                 if ( rc == LDAP_COMPARE_TRUE ) {
181                         li->li_flags |= LDAP_BACK_F_CANCEL_EXOP;
182                 }
183         }
184
185         li->li_flags |= LDAP_BACK_F_ISOPEN;
186
187         return 0;
188 }
189
190 void
191 ldap_back_conn_free( void *v_lc )
192 {
193         ldapconn_t      *lc = v_lc;
194
195         if ( lc->lc_ld != NULL ) {      
196                 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
197         }
198         if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
199                 ch_free( lc->lc_bound_ndn.bv_val );
200         }
201         if ( !BER_BVISNULL( &lc->lc_cred ) ) {
202                 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
203                 ch_free( lc->lc_cred.bv_val );
204         }
205         if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
206                 ch_free( lc->lc_local_ndn.bv_val );
207         }
208         lc->lc_q.tqe_prev = NULL;
209         lc->lc_q.tqe_next = NULL;
210         ch_free( lc );
211 }
212
213 int
214 ldap_back_db_destroy( Backend *be )
215 {
216         if ( be->be_private ) {
217                 ldapinfo_t      *li = ( ldapinfo_t * )be->be_private;
218                 unsigned        i;
219
220                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
221
222                 if ( li->li_uri != NULL ) {
223                         ch_free( li->li_uri );
224                         li->li_uri = NULL;
225
226                         assert( li->li_bvuri != NULL );
227                         ber_bvarray_free( li->li_bvuri );
228                         li->li_bvuri = NULL;
229                 }
230                 if ( !BER_BVISNULL( &li->li_acl_authcID ) ) {
231                         ch_free( li->li_acl_authcID.bv_val );
232                         BER_BVZERO( &li->li_acl_authcID );
233                 }
234                 if ( !BER_BVISNULL( &li->li_acl_authcDN ) ) {
235                         ch_free( li->li_acl_authcDN.bv_val );
236                         BER_BVZERO( &li->li_acl_authcDN );
237                 }
238                 if ( !BER_BVISNULL( &li->li_acl_passwd ) ) {
239                         ch_free( li->li_acl_passwd.bv_val );
240                         BER_BVZERO( &li->li_acl_passwd );
241                 }
242                 if ( !BER_BVISNULL( &li->li_acl_sasl_mech ) ) {
243                         ch_free( li->li_acl_sasl_mech.bv_val );
244                         BER_BVZERO( &li->li_acl_sasl_mech );
245                 }
246                 if ( !BER_BVISNULL( &li->li_acl_sasl_realm ) ) {
247                         ch_free( li->li_acl_sasl_realm.bv_val );
248                         BER_BVZERO( &li->li_acl_sasl_realm );
249                 }
250                 if ( !BER_BVISNULL( &li->li_idassert_authcID ) ) {
251                         ch_free( li->li_idassert_authcID.bv_val );
252                         BER_BVZERO( &li->li_idassert_authcID );
253                 }
254                 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
255                         ch_free( li->li_idassert_authcDN.bv_val );
256                         BER_BVZERO( &li->li_idassert_authcDN );
257                 }
258                 if ( !BER_BVISNULL( &li->li_idassert_passwd ) ) {
259                         ch_free( li->li_idassert_passwd.bv_val );
260                         BER_BVZERO( &li->li_idassert_passwd );
261                 }
262                 if ( !BER_BVISNULL( &li->li_idassert_authzID ) ) {
263                         ch_free( li->li_idassert_authzID.bv_val );
264                         BER_BVZERO( &li->li_idassert_authzID );
265                 }
266                 if ( !BER_BVISNULL( &li->li_idassert_sasl_mech ) ) {
267                         ch_free( li->li_idassert_sasl_mech.bv_val );
268                         BER_BVZERO( &li->li_idassert_sasl_mech );
269                 }
270                 if ( !BER_BVISNULL( &li->li_idassert_sasl_realm ) ) {
271                         ch_free( li->li_idassert_sasl_realm.bv_val );
272                         BER_BVZERO( &li->li_idassert_sasl_realm );
273                 }
274                 if ( li->li_idassert_authz != NULL ) {
275                         ber_bvarray_free( li->li_idassert_authz );
276                         li->li_idassert_authz = NULL;
277                 }
278                 if ( li->li_conninfo.lai_tree ) {
279                         avl_free( li->li_conninfo.lai_tree, ldap_back_conn_free );
280                 }
281                 for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
282                         while ( !LDAP_TAILQ_EMPTY( &li->li_conn_priv[ i ].lic_priv ) ) {
283                                 ldapconn_t      *lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ i ].lic_priv );
284
285                                 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ i ].lic_priv, lc, lc_q );
286                                 ldap_back_conn_free( lc );
287                         }
288                 }
289                 if ( LDAP_BACK_QUARANTINE( li ) ) {
290                         slap_retry_info_destroy( &li->li_quarantine );
291                         ldap_pvt_thread_mutex_destroy( &li->li_quarantine_mutex );
292                 }
293
294                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
295                 ldap_pvt_thread_mutex_destroy( &li->li_conninfo.lai_mutex );
296                 ldap_pvt_thread_mutex_destroy( &li->li_uri_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