]> git.sur5r.net Git - openldap/blob - libraries/libldap/unbind.c
Merge remote branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / libraries / libldap / unbind.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2012 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16  * All rights reserved.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22 #include <ac/stdlib.h>
23
24 #include <ac/socket.h>
25 #include <ac/string.h>
26 #include <ac/time.h>
27
28 #include "ldap-int.h"
29
30 /* An Unbind Request looks like this:
31  *
32  *      UnbindRequest ::= [APPLICATION 2] NULL
33  *
34  * and has no response.  (Source: RFC 4511)
35  */
36
37 int
38 ldap_unbind_ext(
39         LDAP *ld,
40         LDAPControl **sctrls,
41         LDAPControl **cctrls )
42 {
43         int rc;
44
45         assert( ld != NULL );
46         assert( LDAP_VALID( ld ) );
47
48         /* check client controls */
49         rc = ldap_int_client_controls( ld, cctrls );
50         if( rc != LDAP_SUCCESS ) return rc;
51
52         return ldap_ld_free( ld, 1, sctrls, cctrls );
53 }
54
55 int
56 ldap_unbind_ext_s(
57         LDAP *ld,
58         LDAPControl **sctrls,
59         LDAPControl **cctrls )
60 {
61         return ldap_unbind_ext( ld, sctrls, cctrls );
62 }
63
64 int
65 ldap_unbind( LDAP *ld )
66 {
67         Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
68
69         return( ldap_unbind_ext( ld, NULL, NULL ) );
70 }
71
72
73 int
74 ldap_ld_free(
75         LDAP *ld,
76         int close,
77         LDAPControl **sctrls,
78         LDAPControl **cctrls )
79 {
80         LDAPMessage     *lm, *next;
81         int             err = LDAP_SUCCESS;
82
83         LDAP_MUTEX_LOCK( &ld->ld_ldcmutex );
84         /* Someone else is still using this ld. */
85         if (ld->ld_ldcrefcnt > 1) {     /* but not last thread */
86                 /* clean up self only */
87                 ld->ld_ldcrefcnt--;
88                 if ( ld->ld_error != NULL ) {
89                         LDAP_FREE( ld->ld_error );
90                         ld->ld_error = NULL;
91                 }
92
93                 if ( ld->ld_matched != NULL ) {
94                         LDAP_FREE( ld->ld_matched );
95                         ld->ld_matched = NULL;
96                 }
97                 if ( ld->ld_referrals != NULL) {
98                         LDAP_VFREE(ld->ld_referrals);
99                         ld->ld_referrals = NULL;
100                 }  
101                 LDAP_MUTEX_UNLOCK( &ld->ld_ldcmutex );
102                 LDAP_FREE( (char *) ld );
103                 return( err );
104         }
105
106         /* This ld is the last thread. */
107
108         /* free LDAP structure and outstanding requests/responses */
109         LDAP_MUTEX_LOCK( &ld->ld_req_mutex );
110         while ( ld->ld_requests != NULL ) {
111                 ldap_free_request( ld, ld->ld_requests );
112         }
113         LDAP_MUTEX_UNLOCK( &ld->ld_req_mutex );
114         LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
115
116         /* free and unbind from all open connections */
117         while ( ld->ld_conns != NULL ) {
118                 ldap_free_connection( ld, ld->ld_conns, 1, close );
119         }
120         LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
121         LDAP_MUTEX_LOCK( &ld->ld_res_mutex );
122         for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
123                 next = lm->lm_next;
124                 ldap_msgfree( lm );
125         }
126     
127         if ( ld->ld_abandoned != NULL ) {
128                 LDAP_FREE( ld->ld_abandoned );
129                 ld->ld_abandoned = NULL;
130         }
131         LDAP_MUTEX_UNLOCK( &ld->ld_res_mutex );
132         LDAP_MUTEX_LOCK( &ld->ld_ldopts_mutex );
133
134         /* final close callbacks */
135         {
136                 ldaplist *ll, *next;
137
138                 for ( ll = ld->ld_options.ldo_conn_cbs; ll; ll = next ) {
139                         ldap_conncb *cb = ll->ll_data;
140                         next = ll->ll_next;
141                         cb->lc_del( ld, NULL, cb );
142                         LDAP_FREE( ll );
143                 }
144         }
145
146         if ( ld->ld_error != NULL ) {
147                 LDAP_FREE( ld->ld_error );
148                 ld->ld_error = NULL;
149         }
150
151         if ( ld->ld_matched != NULL ) {
152                 LDAP_FREE( ld->ld_matched );
153                 ld->ld_matched = NULL;
154         }
155
156         if ( ld->ld_referrals != NULL) {
157                 LDAP_VFREE(ld->ld_referrals);
158                 ld->ld_referrals = NULL;
159         }  
160     
161         if ( ld->ld_selectinfo != NULL ) {
162                 ldap_free_select_info( ld->ld_selectinfo );
163                 ld->ld_selectinfo = NULL;
164         }
165
166         if ( ld->ld_options.ldo_defludp != NULL ) {
167                 ldap_free_urllist( ld->ld_options.ldo_defludp );
168                 ld->ld_options.ldo_defludp = NULL;
169         }
170
171 #ifdef LDAP_CONNECTIONLESS
172         if ( ld->ld_options.ldo_peer != NULL ) {
173                 LDAP_FREE( ld->ld_options.ldo_peer );
174                 ld->ld_options.ldo_peer = NULL;
175         }
176
177         if ( ld->ld_options.ldo_cldapdn != NULL ) {
178                 LDAP_FREE( ld->ld_options.ldo_cldapdn );
179                 ld->ld_options.ldo_cldapdn = NULL;
180         }
181 #endif
182
183 #ifdef HAVE_CYRUS_SASL
184         if ( ld->ld_options.ldo_def_sasl_mech != NULL ) {
185                 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
186                 ld->ld_options.ldo_def_sasl_mech = NULL;
187         }
188
189         if ( ld->ld_options.ldo_def_sasl_realm != NULL ) {
190                 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
191                 ld->ld_options.ldo_def_sasl_realm = NULL;
192         }
193
194         if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) {
195                 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
196                 ld->ld_options.ldo_def_sasl_authcid = NULL;
197         }
198
199         if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) {
200                 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
201                 ld->ld_options.ldo_def_sasl_authzid = NULL;
202         }
203 #endif
204
205 #ifdef HAVE_TLS
206         ldap_int_tls_destroy( &ld->ld_options );
207 #endif
208
209         if ( ld->ld_options.ldo_sctrls != NULL ) {
210                 ldap_controls_free( ld->ld_options.ldo_sctrls );
211                 ld->ld_options.ldo_sctrls = NULL;
212         }
213
214         if ( ld->ld_options.ldo_cctrls != NULL ) {
215                 ldap_controls_free( ld->ld_options.ldo_cctrls );
216                 ld->ld_options.ldo_cctrls = NULL;
217         }
218         LDAP_MUTEX_UNLOCK( &ld->ld_ldopts_mutex );
219
220         ber_sockbuf_free( ld->ld_sb );   
221    
222 #ifdef LDAP_R_COMPILE
223         ldap_pvt_thread_mutex_destroy( &ld->ld_msgid_mutex );
224         ldap_pvt_thread_mutex_destroy( &ld->ld_conn_mutex );
225         ldap_pvt_thread_mutex_destroy( &ld->ld_req_mutex );
226         ldap_pvt_thread_mutex_destroy( &ld->ld_res_mutex );
227         ldap_pvt_thread_mutex_destroy( &ld->ld_abandon_mutex );
228         ldap_pvt_thread_mutex_destroy( &ld->ld_ldopts_mutex );
229         ldap_pvt_thread_mutex_unlock( &ld->ld_ldcmutex );
230         ldap_pvt_thread_mutex_destroy( &ld->ld_ldcmutex );
231 #endif
232 #ifndef NDEBUG
233         LDAP_TRASH(ld);
234 #endif
235         LDAP_FREE( (char *) ld->ldc );
236         LDAP_FREE( (char *) ld );
237    
238         return( err );
239 }
240
241 int
242 ldap_destroy( LDAP *ld )
243 {
244         return ( ldap_ld_free( ld, 1, NULL, NULL ) );
245 }
246
247 int
248 ldap_unbind_s( LDAP *ld )
249 {
250         return( ldap_unbind_ext( ld, NULL, NULL ) );
251 }
252
253 /* FIXME: this function is called only by ldap_free_connection(),
254  * which, most of the times, is called with ld_req_mutex locked */
255 int
256 ldap_send_unbind(
257         LDAP *ld,
258         Sockbuf *sb,
259         LDAPControl **sctrls,
260         LDAPControl **cctrls )
261 {
262         BerElement      *ber;
263         ber_int_t       id;
264
265         Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
266
267 #ifdef LDAP_CONNECTIONLESS
268         if (LDAP_IS_UDP(ld))
269                 return LDAP_SUCCESS;
270 #endif
271         /* create a message to send */
272         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
273                 return( ld->ld_errno );
274         }
275
276         LDAP_NEXT_MSGID(ld, id);
277
278         /* fill it in */
279         if ( ber_printf( ber, "{itn" /*}*/, id,
280             LDAP_REQ_UNBIND ) == -1 ) {
281                 ld->ld_errno = LDAP_ENCODING_ERROR;
282                 ber_free( ber, 1 );
283                 return( ld->ld_errno );
284         }
285
286         /* Put Server Controls */
287         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
288                 ber_free( ber, 1 );
289                 return ld->ld_errno;
290         }
291
292         if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
293                 ld->ld_errno = LDAP_ENCODING_ERROR;
294                 ber_free( ber, 1 );
295                 return( ld->ld_errno );
296         }
297
298         ld->ld_errno = LDAP_SUCCESS;
299         /* send the message */
300         if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) == -1 ) {
301                 ld->ld_errno = LDAP_SERVER_DOWN;
302         }
303
304         return( ld->ld_errno );
305 }