2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2011 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 LDAP_MUTEX_LOCK( &ld->ld_ldcmutex );
84 /* Someone else is still using this ld. */
85 if (ld->ld_ldcrefcnt > 1) { /* but not last thread */
86 /* clean up self only */
88 if ( ld->ld_error != NULL ) {
89 LDAP_FREE( ld->ld_error );
93 if ( ld->ld_matched != NULL ) {
94 LDAP_FREE( ld->ld_matched );
95 ld->ld_matched = NULL;
97 if ( ld->ld_referrals != NULL) {
98 LDAP_VFREE(ld->ld_referrals);
99 ld->ld_referrals = NULL;
101 LDAP_MUTEX_UNLOCK( &ld->ld_ldcmutex );
102 LDAP_FREE( (char *) ld );
106 /* This ld is the last thread. */
108 /* free LDAP structure and outstanding requests/responses */
109 LDAP_MUTEX_LOCK( &ld->ld_req_mutex );
110 while ( ld->ld_requests != NULL ) {
111 ldap_free_request( ld, ld->ld_requests );
113 LDAP_MUTEX_UNLOCK( &ld->ld_req_mutex );
114 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
116 /* free and unbind from all open connections */
117 while ( ld->ld_conns != NULL ) {
118 ldap_free_connection( ld, ld->ld_conns, 1, close );
120 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
121 LDAP_MUTEX_LOCK( &ld->ld_res_mutex );
122 for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
127 if ( ld->ld_abandoned != NULL ) {
128 LDAP_FREE( ld->ld_abandoned );
129 ld->ld_abandoned = NULL;
131 LDAP_MUTEX_UNLOCK( &ld->ld_res_mutex );
132 LDAP_MUTEX_LOCK( &ld->ld_ldopts_mutex );
134 /* final close callbacks */
138 for ( ll = ld->ld_options.ldo_conn_cbs; ll; ll = next ) {
139 ldap_conncb *cb = ll->ll_data;
141 cb->lc_del( ld, NULL, cb );
146 if ( ld->ld_error != NULL ) {
147 LDAP_FREE( ld->ld_error );
151 if ( ld->ld_matched != NULL ) {
152 LDAP_FREE( ld->ld_matched );
153 ld->ld_matched = NULL;
156 if ( ld->ld_referrals != NULL) {
157 LDAP_VFREE(ld->ld_referrals);
158 ld->ld_referrals = NULL;
161 if ( ld->ld_selectinfo != NULL ) {
162 ldap_free_select_info( ld->ld_selectinfo );
163 ld->ld_selectinfo = NULL;
166 if ( ld->ld_options.ldo_defludp != NULL ) {
167 ldap_free_urllist( ld->ld_options.ldo_defludp );
168 ld->ld_options.ldo_defludp = NULL;
171 #ifdef LDAP_CONNECTIONLESS
172 if ( ld->ld_options.ldo_peer != NULL ) {
173 LDAP_FREE( ld->ld_options.ldo_peer );
174 ld->ld_options.ldo_peer = NULL;
177 if ( ld->ld_options.ldo_cldapdn != NULL ) {
178 LDAP_FREE( ld->ld_options.ldo_cldapdn );
179 ld->ld_options.ldo_cldapdn = NULL;
183 #ifdef HAVE_CYRUS_SASL
184 if ( ld->ld_options.ldo_def_sasl_mech != NULL ) {
185 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
186 ld->ld_options.ldo_def_sasl_mech = NULL;
189 if ( ld->ld_options.ldo_def_sasl_realm != NULL ) {
190 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
191 ld->ld_options.ldo_def_sasl_realm = NULL;
194 if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) {
195 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
196 ld->ld_options.ldo_def_sasl_authcid = NULL;
199 if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) {
200 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
201 ld->ld_options.ldo_def_sasl_authzid = NULL;
206 ldap_int_tls_destroy( &ld->ld_options );
209 if ( ld->ld_options.ldo_sctrls != NULL ) {
210 ldap_controls_free( ld->ld_options.ldo_sctrls );
211 ld->ld_options.ldo_sctrls = NULL;
214 if ( ld->ld_options.ldo_cctrls != NULL ) {
215 ldap_controls_free( ld->ld_options.ldo_cctrls );
216 ld->ld_options.ldo_cctrls = NULL;
218 LDAP_MUTEX_UNLOCK( &ld->ld_ldopts_mutex );
220 ber_sockbuf_free( ld->ld_sb );
222 #ifdef LDAP_R_COMPILE
223 ldap_pvt_thread_mutex_destroy( &ld->ld_msgid_mutex );
224 ldap_pvt_thread_mutex_destroy( &ld->ld_conn_mutex );
225 ldap_pvt_thread_mutex_destroy( &ld->ld_req_mutex );
226 ldap_pvt_thread_mutex_destroy( &ld->ld_res_mutex );
227 ldap_pvt_thread_mutex_destroy( &ld->ld_abandon_mutex );
228 ldap_pvt_thread_mutex_destroy( &ld->ld_ldopts_mutex );
229 ldap_pvt_thread_mutex_unlock( &ld->ld_ldcmutex );
230 ldap_pvt_thread_mutex_destroy( &ld->ld_ldcmutex );
235 LDAP_FREE( (char *) ld->ldc );
236 LDAP_FREE( (char *) ld );
242 ldap_destroy( LDAP *ld )
244 return ( ldap_ld_free( ld, 1, NULL, NULL ) );
248 ldap_unbind_s( LDAP *ld )
250 return( ldap_unbind_ext( ld, NULL, NULL ) );
253 /* FIXME: this function is called only by ldap_free_connection(),
254 * which, most of the times, is called with ld_req_mutex locked */
259 LDAPControl **sctrls,
260 LDAPControl **cctrls )
265 Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
267 #ifdef LDAP_CONNECTIONLESS
271 /* create a message to send */
272 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
273 return( ld->ld_errno );
276 LDAP_NEXT_MSGID(ld, id);
279 if ( ber_printf( ber, "{itn" /*}*/, id,
280 LDAP_REQ_UNBIND ) == -1 ) {
281 ld->ld_errno = LDAP_ENCODING_ERROR;
283 return( ld->ld_errno );
286 /* Put Server Controls */
287 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
292 if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
293 ld->ld_errno = LDAP_ENCODING_ERROR;
295 return( ld->ld_errno );
298 ld->ld_errno = LDAP_SUCCESS;
299 /* send the message */
300 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) == -1 ) {
301 ld->ld_errno = LDAP_SERVER_DOWN;
304 return( ld->ld_errno );