2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2010 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.
22 #include <ac/stdlib.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
30 /* An Unbind Request looks like this:
32 * UnbindRequest ::= [APPLICATION 2] NULL
34 * and has no response. (Source: RFC 4511)
41 LDAPControl **cctrls )
46 assert( LDAP_VALID( ld ) );
48 /* check client controls */
49 rc = ldap_int_client_controls( ld, cctrls );
50 if( rc != LDAP_SUCCESS ) return rc;
52 return ldap_ld_free( ld, 1, sctrls, cctrls );
59 LDAPControl **cctrls )
61 return ldap_unbind_ext( ld, sctrls, cctrls );
65 ldap_unbind( LDAP *ld )
67 Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
69 return( ldap_unbind_ext( ld, NULL, NULL ) );
78 LDAPControl **cctrls )
80 LDAPMessage *lm, *next;
81 int err = LDAP_SUCCESS;
83 /* free LDAP structure and outstanding requests/responses */
85 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
87 while ( ld->ld_requests != NULL ) {
88 ldap_free_request( ld, ld->ld_requests );
91 /* free and unbind from all open connections */
92 while ( ld->ld_conns != NULL ) {
93 ldap_free_connection( ld, ld->ld_conns, 1, close );
96 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
100 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
102 for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
107 if ( ld->ld_abandoned != NULL ) {
108 LDAP_FREE( ld->ld_abandoned );
109 ld->ld_abandoned = NULL;
111 #ifdef LDAP_R_COMPILE
112 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
115 /* final close callbacks */
119 for ( ll = ld->ld_options.ldo_conn_cbs; ll; ll = next ) {
120 ldap_conncb *cb = ll->ll_data;
122 cb->lc_del( ld, NULL, cb );
127 if ( ld->ld_error != NULL ) {
128 LDAP_FREE( ld->ld_error );
132 if ( ld->ld_matched != NULL ) {
133 LDAP_FREE( ld->ld_matched );
134 ld->ld_matched = NULL;
137 if( ld->ld_referrals != NULL) {
138 LDAP_VFREE(ld->ld_referrals);
139 ld->ld_referrals = NULL;
142 if ( ld->ld_selectinfo != NULL ) {
143 ldap_free_select_info( ld->ld_selectinfo );
144 ld->ld_selectinfo = NULL;
147 if ( ld->ld_options.ldo_defludp != NULL ) {
148 ldap_free_urllist( ld->ld_options.ldo_defludp );
149 ld->ld_options.ldo_defludp = NULL;
152 #ifdef LDAP_CONNECTIONLESS
153 if ( ld->ld_options.ldo_peer != NULL ) {
154 LDAP_FREE( ld->ld_options.ldo_peer );
155 ld->ld_options.ldo_peer = NULL;
159 #ifdef HAVE_CYRUS_SASL
160 if ( ld->ld_options.ldo_def_sasl_mech != NULL ) {
161 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
162 ld->ld_options.ldo_def_sasl_mech = NULL;
165 if ( ld->ld_options.ldo_def_sasl_realm != NULL ) {
166 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
167 ld->ld_options.ldo_def_sasl_realm = NULL;
170 if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) {
171 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
172 ld->ld_options.ldo_def_sasl_authcid = NULL;
175 if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) {
176 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
177 ld->ld_options.ldo_def_sasl_authzid = NULL;
182 ldap_int_tls_destroy( &ld->ld_options );
185 if ( ld->ld_options.ldo_sctrls != NULL ) {
186 ldap_controls_free( ld->ld_options.ldo_sctrls );
187 ld->ld_options.ldo_sctrls = NULL;
190 if ( ld->ld_options.ldo_cctrls != NULL ) {
191 ldap_controls_free( ld->ld_options.ldo_cctrls );
192 ld->ld_options.ldo_cctrls = NULL;
195 ber_sockbuf_free( ld->ld_sb );
197 #ifdef LDAP_R_COMPILE
198 ldap_pvt_thread_mutex_destroy( &ld->ld_req_mutex );
199 ldap_pvt_thread_mutex_destroy( &ld->ld_res_mutex );
200 ldap_pvt_thread_mutex_destroy( &ld->ld_conn_mutex );
205 LDAP_FREE( (char *) ld );
211 ldap_unbind_s( LDAP *ld )
213 return( ldap_unbind_ext( ld, NULL, NULL ) );
216 /* FIXME: this function is called only by ldap_free_connection(),
217 * which, most of the times, is called with ld_req_mutex locked */
222 LDAPControl **sctrls,
223 LDAPControl **cctrls )
228 Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
230 #ifdef LDAP_CONNECTIONLESS
234 /* create a message to send */
235 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
236 return( ld->ld_errno );
239 id = ++(ld)->ld_msgid;
242 if ( ber_printf( ber, "{itn" /*}*/, id,
243 LDAP_REQ_UNBIND ) == -1 ) {
244 ld->ld_errno = LDAP_ENCODING_ERROR;
246 return( ld->ld_errno );
249 /* Put Server Controls */
250 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
255 if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
256 ld->ld_errno = LDAP_ENCODING_ERROR;
258 return( ld->ld_errno );
261 ld->ld_errno = LDAP_SUCCESS;
262 /* send the message */
263 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) == -1 ) {
264 ld->ld_errno = LDAP_SERVER_DOWN;
267 return( ld->ld_errno );