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