]> git.sur5r.net Git - openldap/blob - libraries/libldap/unbind.c
Happy new year
[openldap] / libraries / libldap / unbind.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 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         if ( ld->ld_options.ldo_tm_api != NULL ) {
148                 LDAP_FREE( ld->ld_options.ldo_tm_api );
149                 ld->ld_options.ldo_tm_api = NULL;
150         }
151
152         if ( ld->ld_options.ldo_tm_net != NULL ) {
153                 LDAP_FREE( ld->ld_options.ldo_tm_net );
154                 ld->ld_options.ldo_tm_net = NULL;
155         }
156
157 #ifdef HAVE_CYRUS_SASL
158         if ( ld->ld_options.ldo_def_sasl_mech != NULL ) {
159                 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
160                 ld->ld_options.ldo_def_sasl_mech = NULL;
161         }
162
163         if ( ld->ld_options.ldo_def_sasl_realm != NULL ) {
164                 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
165                 ld->ld_options.ldo_def_sasl_realm = NULL;
166         }
167
168         if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) {
169                 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
170                 ld->ld_options.ldo_def_sasl_authcid = NULL;
171         }
172
173         if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) {
174                 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
175                 ld->ld_options.ldo_def_sasl_authzid = NULL;
176         }
177 #endif
178
179         ber_sockbuf_free( ld->ld_sb );   
180    
181 #ifdef LDAP_R_COMPILE
182         ldap_pvt_thread_mutex_destroy( &ld->ld_req_mutex );
183         ldap_pvt_thread_mutex_destroy( &ld->ld_res_mutex );
184 #endif
185         LDAP_FREE( (char *) ld );
186    
187         return( err );
188 }
189
190 int
191 ldap_unbind_s( LDAP *ld )
192 {
193         return( ldap_unbind_ext( ld, NULL, NULL ) );
194 }
195
196
197 int
198 ldap_send_unbind(
199         LDAP *ld,
200         Sockbuf *sb,
201         LDAPControl **sctrls,
202         LDAPControl **cctrls )
203 {
204         BerElement      *ber;
205         ber_int_t       id;
206
207 #ifdef NEW_LOGGING
208         LDAP_LOG ( OPERATION, ENTRY, "ldap_send_unbind\n", 0, 0, 0 );
209 #else
210         Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
211 #endif
212
213 #ifdef LDAP_CONNECTIONLESS
214         if (LDAP_IS_UDP(ld))
215                 return LDAP_SUCCESS;
216 #endif
217         /* create a message to send */
218         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
219                 return( ld->ld_errno );
220         }
221
222         LDAP_NEXT_MSGID( ld, id );
223         /* fill it in */
224         if ( ber_printf( ber, "{itn" /*}*/, id,
225             LDAP_REQ_UNBIND ) == -1 ) {
226                 ld->ld_errno = LDAP_ENCODING_ERROR;
227                 ber_free( ber, 1 );
228                 return( ld->ld_errno );
229         }
230
231         /* Put Server Controls */
232         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
233                 ber_free( ber, 1 );
234                 return ld->ld_errno;
235         }
236
237         if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
238                 ld->ld_errno = LDAP_ENCODING_ERROR;
239                 ber_free( ber, 1 );
240                 return( ld->ld_errno );
241         }
242
243 #ifdef LDAP_R_COMPILE
244         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
245 #endif
246         ld->ld_errno = LDAP_SUCCESS;
247         /* send the message */
248         if ( ber_flush( sb, ber, 1 ) == -1 ) {
249                 ld->ld_errno = LDAP_SERVER_DOWN;
250                 ber_free( ber, 1 );
251         }
252 #ifdef LDAP_R_COMPILE
253         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
254 #endif
255
256         return( ld->ld_errno );
257 }