]> git.sur5r.net Git - openldap/blob - libraries/libldap/unbind.c
c58d7a2a0a4df75f0b68ade6df1b8642f81ce53d
[openldap] / libraries / libldap / unbind.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 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 /* Portions Copyright (C) The Internet Society (1997)
19  * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
20  */
21
22 /* An Unbind Request looks like this:
23  *
24  *      UnbindRequest ::= NULL
25  *
26  * and has no response.
27  */
28
29 #include "portable.h"
30
31 #include <stdio.h>
32 #include <ac/stdlib.h>
33
34 #include <ac/socket.h>
35 #include <ac/string.h>
36 #include <ac/time.h>
37
38 #include "ldap-int.h"
39
40 int
41 ldap_unbind_ext(
42         LDAP *ld,
43         LDAPControl **sctrls,
44         LDAPControl **cctrls )
45 {
46         int rc;
47
48         assert( ld != NULL );
49         assert( LDAP_VALID( ld ) );
50
51         /* check client controls */
52         rc = ldap_int_client_controls( ld, cctrls );
53         if( rc != LDAP_SUCCESS ) return rc;
54
55         return ldap_ld_free( ld, 1, sctrls, cctrls );
56 }
57
58 int
59 ldap_unbind_ext_s(
60         LDAP *ld,
61         LDAPControl **sctrls,
62         LDAPControl **cctrls )
63 {
64         return ldap_unbind_ext( ld, sctrls, cctrls );
65 }
66
67 int
68 ldap_unbind( LDAP *ld )
69 {
70         Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
71
72         return( ldap_unbind_ext( ld, NULL, NULL ) );
73 }
74
75
76 int
77 ldap_ld_free(
78         LDAP *ld,
79         int close,
80         LDAPControl **sctrls,
81         LDAPControl **cctrls )
82 {
83         LDAPMessage     *lm, *next;
84         int             err = LDAP_SUCCESS;
85
86         /* free LDAP structure and outstanding requests/responses */
87 #ifdef LDAP_R_COMPILE
88         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
89 #endif
90         while ( ld->ld_requests != NULL ) {
91                 ldap_free_request( ld, ld->ld_requests );
92         }
93 #ifdef LDAP_R_COMPILE
94         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
95 #endif
96
97         /* free and unbind from all open connections */
98         while ( ld->ld_conns != NULL ) {
99                 ldap_free_connection( ld, ld->ld_conns, 1, close );
100         }
101
102 #ifdef LDAP_R_COMPILE
103         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
104 #endif
105         for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
106                 next = lm->lm_next;
107                 ldap_msgfree( lm );
108         }
109 #ifdef LDAP_R_COMPILE
110         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
111 #endif
112
113         if ( ld->ld_error != NULL ) {
114                 LDAP_FREE( ld->ld_error );
115                 ld->ld_error = NULL;
116         }
117
118         if ( ld->ld_matched != NULL ) {
119                 LDAP_FREE( ld->ld_matched );
120                 ld->ld_matched = NULL;
121         }
122
123         if( ld->ld_referrals != NULL) {
124                 LDAP_VFREE(ld->ld_referrals);
125                 ld->ld_referrals = NULL;
126         }  
127     
128         if ( ld->ld_abandoned != NULL ) {
129                 LDAP_FREE( ld->ld_abandoned );
130                 ld->ld_abandoned = NULL;
131         }
132
133         if ( ld->ld_selectinfo != NULL ) {
134                 ldap_free_select_info( ld->ld_selectinfo );
135                 ld->ld_selectinfo = NULL;
136         }
137
138         if ( ld->ld_options.ldo_defludp != NULL ) {
139                 ldap_free_urllist( ld->ld_options.ldo_defludp );
140                 ld->ld_options.ldo_defludp = NULL;
141         }
142
143         if ( ld->ld_options.ldo_tm_api != NULL ) {
144                 LDAP_FREE( ld->ld_options.ldo_tm_api );
145                 ld->ld_options.ldo_tm_api = NULL;
146         }
147
148         if ( ld->ld_options.ldo_tm_net != NULL ) {
149                 LDAP_FREE( ld->ld_options.ldo_tm_net );
150                 ld->ld_options.ldo_tm_net = NULL;
151         }
152
153 #ifdef HAVE_CYRUS_SASL
154         if ( ld->ld_options.ldo_def_sasl_mech != NULL ) {
155                 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
156                 ld->ld_options.ldo_def_sasl_mech = NULL;
157         }
158
159         if ( ld->ld_options.ldo_def_sasl_realm != NULL ) {
160                 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
161                 ld->ld_options.ldo_def_sasl_realm = NULL;
162         }
163
164         if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) {
165                 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
166                 ld->ld_options.ldo_def_sasl_authcid = NULL;
167         }
168
169         if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) {
170                 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
171                 ld->ld_options.ldo_def_sasl_authzid = NULL;
172         }
173 #endif
174
175         if ( ld->ld_options.ldo_sctrls != NULL ) {
176                 ldap_controls_free( ld->ld_options.ldo_sctrls );
177                 ld->ld_options.ldo_sctrls = NULL;
178         }
179
180         if ( ld->ld_options.ldo_cctrls != NULL ) {
181                 ldap_controls_free( ld->ld_options.ldo_cctrls );
182                 ld->ld_options.ldo_cctrls = NULL;
183         }
184
185         ber_sockbuf_free( ld->ld_sb );   
186    
187 #ifdef LDAP_R_COMPILE
188         ldap_pvt_thread_mutex_destroy( &ld->ld_req_mutex );
189         ldap_pvt_thread_mutex_destroy( &ld->ld_res_mutex );
190 #endif
191         LDAP_FREE( (char *) ld );
192    
193         return( err );
194 }
195
196 int
197 ldap_unbind_s( LDAP *ld )
198 {
199         return( ldap_unbind_ext( ld, NULL, NULL ) );
200 }
201
202
203 int
204 ldap_send_unbind(
205         LDAP *ld,
206         Sockbuf *sb,
207         LDAPControl **sctrls,
208         LDAPControl **cctrls )
209 {
210         BerElement      *ber;
211         ber_int_t       id;
212
213         Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
214
215 #ifdef LDAP_CONNECTIONLESS
216         if (LDAP_IS_UDP(ld))
217                 return LDAP_SUCCESS;
218 #endif
219         /* create a message to send */
220         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
221                 return( ld->ld_errno );
222         }
223
224         LDAP_NEXT_MSGID( ld, id );
225         /* fill it in */
226         if ( ber_printf( ber, "{itn" /*}*/, id,
227             LDAP_REQ_UNBIND ) == -1 ) {
228                 ld->ld_errno = LDAP_ENCODING_ERROR;
229                 ber_free( ber, 1 );
230                 return( ld->ld_errno );
231         }
232
233         /* Put Server Controls */
234         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
235                 ber_free( ber, 1 );
236                 return ld->ld_errno;
237         }
238
239         if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
240                 ld->ld_errno = LDAP_ENCODING_ERROR;
241                 ber_free( ber, 1 );
242                 return( ld->ld_errno );
243         }
244
245 #ifdef LDAP_R_COMPILE
246         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
247 #endif
248         ld->ld_errno = LDAP_SUCCESS;
249         /* send the message */
250         if ( ber_flush( sb, ber, 1 ) == -1 ) {
251                 ld->ld_errno = LDAP_SERVER_DOWN;
252                 ber_free( ber, 1 );
253         }
254 #ifdef LDAP_R_COMPILE
255         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
256 #endif
257
258         return( ld->ld_errno );
259 }