]> git.sur5r.net Git - openldap/blob - libraries/libldap/unbind.c
ITS#6568
[openldap] / libraries / libldap / unbind.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2010 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         /* free LDAP structure and outstanding requests/responses */
84 #ifdef LDAP_R_COMPILE
85         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
86 #endif
87         while ( ld->ld_requests != NULL ) {
88                 ldap_free_request( ld, ld->ld_requests );
89         }
90
91         /* free and unbind from all open connections */
92         while ( ld->ld_conns != NULL ) {
93                 ldap_free_connection( ld, ld->ld_conns, 1, close );
94         }
95 #ifdef LDAP_R_COMPILE
96         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
97 #endif
98
99 #ifdef LDAP_R_COMPILE
100         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
101 #endif
102         for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
103                 next = lm->lm_next;
104                 ldap_msgfree( lm );
105         }
106     
107         if ( ld->ld_abandoned != NULL ) {
108                 LDAP_FREE( ld->ld_abandoned );
109                 ld->ld_abandoned = NULL;
110         }
111 #ifdef LDAP_R_COMPILE
112         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
113 #endif
114
115         /* final close callbacks */
116         {
117                 ldaplist *ll, *next;
118
119                 for ( ll = ld->ld_options.ldo_conn_cbs; ll; ll = next ) {
120                         ldap_conncb *cb = ll->ll_data;
121                         next = ll->ll_next;
122                         cb->lc_del( ld, NULL, cb );
123                         LDAP_FREE( ll );
124                 }
125         }
126
127         if ( ld->ld_error != NULL ) {
128                 LDAP_FREE( ld->ld_error );
129                 ld->ld_error = NULL;
130         }
131
132         if ( ld->ld_matched != NULL ) {
133                 LDAP_FREE( ld->ld_matched );
134                 ld->ld_matched = NULL;
135         }
136
137         if( ld->ld_referrals != NULL) {
138                 LDAP_VFREE(ld->ld_referrals);
139                 ld->ld_referrals = NULL;
140         }  
141     
142         if ( ld->ld_selectinfo != NULL ) {
143                 ldap_free_select_info( ld->ld_selectinfo );
144                 ld->ld_selectinfo = NULL;
145         }
146
147         if ( ld->ld_options.ldo_defludp != NULL ) {
148                 ldap_free_urllist( ld->ld_options.ldo_defludp );
149                 ld->ld_options.ldo_defludp = NULL;
150         }
151
152 #ifdef LDAP_CONNECTIONLESS
153         if ( ld->ld_options.ldo_peer != NULL ) {
154                 LDAP_FREE( ld->ld_options.ldo_peer );
155                 ld->ld_options.ldo_peer = NULL;
156         }
157
158         if ( ld->ld_options.ldo_cldapdn != NULL ) {
159                 LDAP_FREE( ld->ld_options.ldo_cldapdn );
160                 ld->ld_options.ldo_cldapdn = NULL;
161         }
162 #endif
163
164 #ifdef HAVE_CYRUS_SASL
165         if ( ld->ld_options.ldo_def_sasl_mech != NULL ) {
166                 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
167                 ld->ld_options.ldo_def_sasl_mech = NULL;
168         }
169
170         if ( ld->ld_options.ldo_def_sasl_realm != NULL ) {
171                 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
172                 ld->ld_options.ldo_def_sasl_realm = NULL;
173         }
174
175         if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) {
176                 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
177                 ld->ld_options.ldo_def_sasl_authcid = NULL;
178         }
179
180         if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) {
181                 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
182                 ld->ld_options.ldo_def_sasl_authzid = NULL;
183         }
184 #endif
185
186 #ifdef HAVE_TLS
187         ldap_int_tls_destroy( &ld->ld_options );
188 #endif
189
190         if ( ld->ld_options.ldo_sctrls != NULL ) {
191                 ldap_controls_free( ld->ld_options.ldo_sctrls );
192                 ld->ld_options.ldo_sctrls = NULL;
193         }
194
195         if ( ld->ld_options.ldo_cctrls != NULL ) {
196                 ldap_controls_free( ld->ld_options.ldo_cctrls );
197                 ld->ld_options.ldo_cctrls = NULL;
198         }
199
200         ber_sockbuf_free( ld->ld_sb );   
201    
202 #ifdef LDAP_R_COMPILE
203         ldap_pvt_thread_mutex_destroy( &ld->ld_req_mutex );
204         ldap_pvt_thread_mutex_destroy( &ld->ld_res_mutex );
205         ldap_pvt_thread_mutex_destroy( &ld->ld_conn_mutex );
206 #endif
207 #ifndef NDEBUG
208         LDAP_TRASH(ld);
209 #endif
210         LDAP_FREE( (char *) ld );
211    
212         return( err );
213 }
214
215 int
216 ldap_unbind_s( LDAP *ld )
217 {
218         return( ldap_unbind_ext( ld, NULL, NULL ) );
219 }
220
221 /* FIXME: this function is called only by ldap_free_connection(),
222  * which, most of the times, is called with ld_req_mutex locked */
223 int
224 ldap_send_unbind(
225         LDAP *ld,
226         Sockbuf *sb,
227         LDAPControl **sctrls,
228         LDAPControl **cctrls )
229 {
230         BerElement      *ber;
231         ber_int_t       id;
232
233         Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
234
235 #ifdef LDAP_CONNECTIONLESS
236         if (LDAP_IS_UDP(ld))
237                 return LDAP_SUCCESS;
238 #endif
239         /* create a message to send */
240         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
241                 return( ld->ld_errno );
242         }
243
244         id = ++(ld)->ld_msgid;
245
246         /* fill it in */
247         if ( ber_printf( ber, "{itn" /*}*/, id,
248             LDAP_REQ_UNBIND ) == -1 ) {
249                 ld->ld_errno = LDAP_ENCODING_ERROR;
250                 ber_free( ber, 1 );
251                 return( ld->ld_errno );
252         }
253
254         /* Put Server Controls */
255         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
256                 ber_free( ber, 1 );
257                 return ld->ld_errno;
258         }
259
260         if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
261                 ld->ld_errno = LDAP_ENCODING_ERROR;
262                 ber_free( ber, 1 );
263                 return( ld->ld_errno );
264         }
265
266         ld->ld_errno = LDAP_SUCCESS;
267         /* send the message */
268         if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) == -1 ) {
269                 ld->ld_errno = LDAP_SERVER_DOWN;
270         }
271
272         return( ld->ld_errno );
273 }