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