2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16 * All rights reserved.
18 /* Portions Copyright (C) The Internet Society (1997)
19 * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
22 /* An Unbind Request looks like this:
24 * UnbindRequest ::= NULL
26 * and has no response.
32 #include <ac/stdlib.h>
34 #include <ac/socket.h>
35 #include <ac/string.h>
44 LDAPControl **cctrls )
49 assert( LDAP_VALID( ld ) );
51 /* check client controls */
52 rc = ldap_int_client_controls( ld, cctrls );
53 if( rc != LDAP_SUCCESS ) return rc;
55 return ldap_ld_free( ld, 1, sctrls, cctrls );
62 LDAPControl **cctrls )
64 return ldap_unbind_ext( ld, sctrls, cctrls );
68 ldap_unbind( LDAP *ld )
70 Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
72 return( ldap_unbind_ext( ld, NULL, NULL ) );
81 LDAPControl **cctrls )
83 LDAPMessage *lm, *next;
84 int err = LDAP_SUCCESS;
86 /* free LDAP structure and outstanding requests/responses */
88 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
90 while ( ld->ld_requests != NULL ) {
91 ldap_free_request( ld, ld->ld_requests );
94 /* free and unbind from all open connections */
95 while ( ld->ld_conns != NULL ) {
96 ldap_free_connection( ld, ld->ld_conns, 1, close );
99 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
102 #ifdef LDAP_R_COMPILE
103 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
105 for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
110 if ( ld->ld_abandoned != NULL ) {
111 LDAP_FREE( ld->ld_abandoned );
112 ld->ld_abandoned = NULL;
114 #ifdef LDAP_R_COMPILE
115 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
118 if ( ld->ld_error != NULL ) {
119 LDAP_FREE( ld->ld_error );
123 if ( ld->ld_matched != NULL ) {
124 LDAP_FREE( ld->ld_matched );
125 ld->ld_matched = NULL;
128 if( ld->ld_referrals != NULL) {
129 LDAP_VFREE(ld->ld_referrals);
130 ld->ld_referrals = NULL;
133 if ( ld->ld_selectinfo != NULL ) {
134 ldap_free_select_info( ld->ld_selectinfo );
135 ld->ld_selectinfo = NULL;
138 if ( ld->ld_options.ldo_defludp != NULL ) {
139 ldap_free_urllist( ld->ld_options.ldo_defludp );
140 ld->ld_options.ldo_defludp = NULL;
143 #ifdef LDAP_CONNECTIONLESS
144 if ( ld->ld_options.ldo_peer != NULL ) {
145 LDAP_FREE( ld->ld_options.ldo_peer );
146 ld->ld_options.ldo_peer = NULL;
150 if ( ld->ld_options.ldo_tm_api != NULL ) {
151 LDAP_FREE( ld->ld_options.ldo_tm_api );
152 ld->ld_options.ldo_tm_api = NULL;
155 if ( ld->ld_options.ldo_tm_net != NULL ) {
156 LDAP_FREE( ld->ld_options.ldo_tm_net );
157 ld->ld_options.ldo_tm_net = NULL;
160 #ifdef HAVE_CYRUS_SASL
161 if ( ld->ld_options.ldo_def_sasl_mech != NULL ) {
162 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
163 ld->ld_options.ldo_def_sasl_mech = NULL;
166 if ( ld->ld_options.ldo_def_sasl_realm != NULL ) {
167 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
168 ld->ld_options.ldo_def_sasl_realm = NULL;
171 if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) {
172 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
173 ld->ld_options.ldo_def_sasl_authcid = NULL;
176 if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) {
177 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
178 ld->ld_options.ldo_def_sasl_authzid = NULL;
183 ldap_int_tls_destroy( &ld->ld_options );
186 if ( ld->ld_options.ldo_sctrls != NULL ) {
187 ldap_controls_free( ld->ld_options.ldo_sctrls );
188 ld->ld_options.ldo_sctrls = NULL;
191 if ( ld->ld_options.ldo_cctrls != NULL ) {
192 ldap_controls_free( ld->ld_options.ldo_cctrls );
193 ld->ld_options.ldo_cctrls = NULL;
196 ber_sockbuf_free( ld->ld_sb );
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 ldap_pvt_thread_mutex_destroy( &ld->ld_conn_mutex );
206 LDAP_FREE( (char *) ld );
212 ldap_unbind_s( LDAP *ld )
214 return( ldap_unbind_ext( ld, NULL, NULL ) );
217 /* FIXME: this function is called only by ldap_free_connection(),
218 * which, most of the times, is called with ld_req_mutex locked */
223 LDAPControl **sctrls,
224 LDAPControl **cctrls )
229 Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
231 #ifdef LDAP_CONNECTIONLESS
235 /* create a message to send */
236 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
237 return( ld->ld_errno );
240 id = ++(ld)->ld_msgid;
243 if ( ber_printf( ber, "{itn" /*}*/, id,
244 LDAP_REQ_UNBIND ) == -1 ) {
245 ld->ld_errno = LDAP_ENCODING_ERROR;
247 return( ld->ld_errno );
250 /* Put Server Controls */
251 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
256 if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
257 ld->ld_errno = LDAP_ENCODING_ERROR;
259 return( ld->ld_errno );
262 ld->ld_errno = LDAP_SUCCESS;
263 /* send the message */
264 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) == -1 ) {
265 ld->ld_errno = LDAP_SERVER_DOWN;
268 return( ld->ld_errno );