]> git.sur5r.net Git - openldap/blob - libraries/libldap/unbind.c
ITS#4052,4053 plug mem leaks
[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 #ifdef NEW_LOGGING
71         LDAP_LOG ( OPERATION, ENTRY, "ldap_unbind\n", 0, 0, 0 );
72 #else
73         Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
74 #endif
75
76         return( ldap_unbind_ext( ld, NULL, NULL ) );
77 }
78
79
80 int
81 ldap_ld_free(
82         LDAP *ld,
83         int close,
84         LDAPControl **sctrls,
85         LDAPControl **cctrls )
86 {
87         LDAPMessage     *lm, *next;
88         int             err = LDAP_SUCCESS;
89
90         /* free LDAP structure and outstanding requests/responses */
91 #ifdef LDAP_R_COMPILE
92         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
93 #endif
94         while ( ld->ld_requests != NULL ) {
95                 ldap_free_request( ld, ld->ld_requests );
96         }
97 #ifdef LDAP_R_COMPILE
98         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
99 #endif
100
101         /* free and unbind from all open connections */
102         while ( ld->ld_conns != NULL ) {
103                 ldap_free_connection( ld, ld->ld_conns, 1, close );
104         }
105
106 #ifdef LDAP_R_COMPILE
107         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
108 #endif
109         for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
110                 next = lm->lm_next;
111                 ldap_msgfree( lm );
112         }
113 #ifdef LDAP_R_COMPILE
114         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
115 #endif
116
117         if ( ld->ld_error != NULL ) {
118                 LDAP_FREE( ld->ld_error );
119                 ld->ld_error = NULL;
120         }
121
122         if ( ld->ld_matched != NULL ) {
123                 LDAP_FREE( ld->ld_matched );
124                 ld->ld_matched = NULL;
125         }
126
127         if( ld->ld_referrals != NULL) {
128                 LDAP_VFREE(ld->ld_referrals);
129                 ld->ld_referrals = NULL;
130         }  
131     
132         if ( ld->ld_abandoned != NULL ) {
133                 LDAP_FREE( ld->ld_abandoned );
134                 ld->ld_abandoned = NULL;
135         }
136
137         if ( ld->ld_selectinfo != NULL ) {
138                 ldap_free_select_info( ld->ld_selectinfo );
139                 ld->ld_selectinfo = NULL;
140         }
141
142         if ( ld->ld_options.ldo_defludp != NULL ) {
143                 ldap_free_urllist( ld->ld_options.ldo_defludp );
144                 ld->ld_options.ldo_defludp = NULL;
145         }
146
147 #ifdef LDAP_CONNECTIONLESS
148         if ( ld->ld_options.ldo_peer != NULL ) {
149                 LDAP_FREE( ld->ld_options.ldo_peer );
150                 ld->ld_options.ldo_peer = NULL;
151         }
152 #endif
153
154         if ( ld->ld_options.ldo_tm_api != NULL ) {
155                 LDAP_FREE( ld->ld_options.ldo_tm_api );
156                 ld->ld_options.ldo_tm_api = NULL;
157         }
158
159         if ( ld->ld_options.ldo_tm_net != NULL ) {
160                 LDAP_FREE( ld->ld_options.ldo_tm_net );
161                 ld->ld_options.ldo_tm_net = NULL;
162         }
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         if ( ld->ld_options.ldo_sctrls != NULL ) {
187                 ldap_controls_free( ld->ld_options.ldo_sctrls );
188                 ld->ld_options.ldo_sctrls = NULL;
189         }
190
191         if ( ld->ld_options.ldo_cctrls != NULL ) {
192                 ldap_controls_free( ld->ld_options.ldo_cctrls );
193                 ld->ld_options.ldo_cctrls = NULL;
194         }
195
196         ber_sockbuf_free( ld->ld_sb );   
197    
198 #ifdef LDAP_R_COMPILE
199         ldap_pvt_thread_mutex_destroy( &ld->ld_req_mutex );
200         ldap_pvt_thread_mutex_destroy( &ld->ld_res_mutex );
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
214 int
215 ldap_send_unbind(
216         LDAP *ld,
217         Sockbuf *sb,
218         LDAPControl **sctrls,
219         LDAPControl **cctrls )
220 {
221         BerElement      *ber;
222         ber_int_t       id;
223
224 #ifdef NEW_LOGGING
225         LDAP_LOG ( OPERATION, ENTRY, "ldap_send_unbind\n", 0, 0, 0 );
226 #else
227         Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
228 #endif
229
230 #ifdef LDAP_CONNECTIONLESS
231         if (LDAP_IS_UDP(ld))
232                 return LDAP_SUCCESS;
233 #endif
234         /* create a message to send */
235         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
236                 return( ld->ld_errno );
237         }
238
239         LDAP_NEXT_MSGID( ld, id );
240         /* fill it in */
241         if ( ber_printf( ber, "{itn" /*}*/, id,
242             LDAP_REQ_UNBIND ) == -1 ) {
243                 ld->ld_errno = LDAP_ENCODING_ERROR;
244                 ber_free( ber, 1 );
245                 return( ld->ld_errno );
246         }
247
248         /* Put Server Controls */
249         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
250                 ber_free( ber, 1 );
251                 return ld->ld_errno;
252         }
253
254         if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
255                 ld->ld_errno = LDAP_ENCODING_ERROR;
256                 ber_free( ber, 1 );
257                 return( ld->ld_errno );
258         }
259
260 #ifdef LDAP_R_COMPILE
261         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
262 #endif
263         ld->ld_errno = LDAP_SUCCESS;
264         /* send the message */
265         if ( ber_flush( sb, ber, 1 ) == -1 ) {
266                 ld->ld_errno = LDAP_SERVER_DOWN;
267                 ber_free( ber, 1 );
268         }
269 #ifdef LDAP_R_COMPILE
270         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
271 #endif
272
273         return( ld->ld_errno );
274 }