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