2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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 )
71 LDAP_LOG ( OPERATION, ENTRY, "ldap_unbind\n", 0, 0, 0 );
73 Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
76 return( ldap_unbind_ext( ld, NULL, NULL ) );
85 LDAPControl **cctrls )
87 LDAPMessage *lm, *next;
88 int err = LDAP_SUCCESS;
90 /* free LDAP structure and outstanding requests/responses */
92 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
94 while ( ld->ld_requests != NULL ) {
95 ldap_free_request( ld, ld->ld_requests );
98 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
101 /* free and unbind from all open connections */
102 while ( ld->ld_conns != NULL ) {
103 ldap_free_connection( ld, ld->ld_conns, 1, close );
106 #ifdef LDAP_R_COMPILE
107 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
109 for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
113 #ifdef LDAP_R_COMPILE
114 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
117 if ( ld->ld_error != NULL ) {
118 LDAP_FREE( ld->ld_error );
122 if ( ld->ld_matched != NULL ) {
123 LDAP_FREE( ld->ld_matched );
124 ld->ld_matched = NULL;
127 if( ld->ld_referrals != NULL) {
128 LDAP_VFREE(ld->ld_referrals);
129 ld->ld_referrals = NULL;
132 if ( ld->ld_abandoned != NULL ) {
133 LDAP_FREE( ld->ld_abandoned );
134 ld->ld_abandoned = NULL;
137 if ( ld->ld_selectinfo != NULL ) {
138 ldap_free_select_info( ld->ld_selectinfo );
139 ld->ld_selectinfo = NULL;
142 if ( ld->ld_options.ldo_defludp != NULL ) {
143 ldap_free_urllist( ld->ld_options.ldo_defludp );
144 ld->ld_options.ldo_defludp = NULL;
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;
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;
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;
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;
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;
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;
179 ber_sockbuf_free( ld->ld_sb );
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 );
185 LDAP_FREE( (char *) ld );
191 ldap_unbind_s( LDAP *ld )
193 return( ldap_unbind_ext( ld, NULL, NULL ) );
201 LDAPControl **sctrls,
202 LDAPControl **cctrls )
208 LDAP_LOG ( OPERATION, ENTRY, "ldap_send_unbind\n", 0, 0, 0 );
210 Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
213 #ifdef LDAP_CONNECTIONLESS
217 /* create a message to send */
218 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
219 return( ld->ld_errno );
222 LDAP_NEXT_MSGID( ld, id );
224 if ( ber_printf( ber, "{itn" /*}*/, id,
225 LDAP_REQ_UNBIND ) == -1 ) {
226 ld->ld_errno = LDAP_ENCODING_ERROR;
228 return( ld->ld_errno );
231 /* Put Server Controls */
232 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
237 if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
238 ld->ld_errno = LDAP_ENCODING_ERROR;
240 return( ld->ld_errno );
243 #ifdef LDAP_R_COMPILE
244 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
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;
252 #ifdef LDAP_R_COMPILE
253 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
256 return( ld->ld_errno );