1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 15:16:36 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2015-02-12 05:36:57 +08:00
|
|
|
* Copyright 1998-2015 The OpenLDAP Foundation.
|
2003-11-26 15:16:36 +08:00
|
|
|
* All rights reserved.
|
1998-08-09 08:43:13 +08:00
|
|
|
*
|
2003-11-26 15:16:36 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* Portions Copyright (c) 1990 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
1998-08-09 08:43:13 +08:00
|
|
|
*/
|
1999-06-04 03:22:33 +08:00
|
|
|
|
1998-10-25 11:13:43 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
#include <ac/stdlib.h>
|
1998-08-21 03:42:38 +08:00
|
|
|
|
1998-10-25 11:13:43 +08:00
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include "ldap-int.h"
|
|
|
|
|
2006-10-28 03:37:04 +08:00
|
|
|
/* An Unbind Request looks like this:
|
|
|
|
*
|
|
|
|
* UnbindRequest ::= [APPLICATION 2] NULL
|
|
|
|
*
|
|
|
|
* and has no response. (Source: RFC 4511)
|
|
|
|
*/
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
int
|
|
|
|
ldap_unbind_ext(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPControl **sctrls,
|
|
|
|
LDAPControl **cctrls )
|
|
|
|
{
|
2001-07-06 11:13:42 +08:00
|
|
|
int rc;
|
|
|
|
|
2001-07-25 00:38:42 +08:00
|
|
|
assert( ld != NULL );
|
|
|
|
assert( LDAP_VALID( ld ) );
|
|
|
|
|
2001-07-06 11:13:42 +08:00
|
|
|
/* check client controls */
|
|
|
|
rc = ldap_int_client_controls( ld, cctrls );
|
|
|
|
if( rc != LDAP_SUCCESS ) return rc;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
return ldap_ld_free( ld, 1, sctrls, cctrls );
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-06-11 01:29:10 +08:00
|
|
|
int
|
|
|
|
ldap_unbind_ext_s(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPControl **sctrls,
|
|
|
|
LDAPControl **cctrls )
|
|
|
|
{
|
|
|
|
return ldap_unbind_ext( ld, sctrls, cctrls );
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
int
|
|
|
|
ldap_unbind( LDAP *ld )
|
|
|
|
{
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
return( ldap_unbind_ext( ld, NULL, NULL ) );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_ld_free(
|
|
|
|
LDAP *ld,
|
|
|
|
int close,
|
|
|
|
LDAPControl **sctrls,
|
|
|
|
LDAPControl **cctrls )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
LDAPMessage *lm, *next;
|
|
|
|
int err = LDAP_SUCCESS;
|
|
|
|
|
2010-10-23 05:45:48 +08:00
|
|
|
LDAP_MUTEX_LOCK( &ld->ld_ldcmutex );
|
|
|
|
/* Someone else is still using this ld. */
|
|
|
|
if (ld->ld_ldcrefcnt > 1) { /* but not last thread */
|
|
|
|
/* clean up self only */
|
|
|
|
ld->ld_ldcrefcnt--;
|
|
|
|
if ( ld->ld_error != NULL ) {
|
|
|
|
LDAP_FREE( ld->ld_error );
|
|
|
|
ld->ld_error = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ld->ld_matched != NULL ) {
|
|
|
|
LDAP_FREE( ld->ld_matched );
|
|
|
|
ld->ld_matched = NULL;
|
|
|
|
}
|
|
|
|
if ( ld->ld_referrals != NULL) {
|
|
|
|
LDAP_VFREE(ld->ld_referrals);
|
|
|
|
ld->ld_referrals = NULL;
|
2015-02-04 11:16:20 +08:00
|
|
|
}
|
2010-10-23 05:45:48 +08:00
|
|
|
LDAP_MUTEX_UNLOCK( &ld->ld_ldcmutex );
|
|
|
|
LDAP_FREE( (char *) ld );
|
|
|
|
return( err );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This ld is the last thread. */
|
2015-02-04 11:16:20 +08:00
|
|
|
LDAP_MUTEX_UNLOCK( &ld->ld_ldcmutex );
|
2010-10-23 05:45:48 +08:00
|
|
|
|
2001-07-25 00:38:42 +08:00
|
|
|
/* free LDAP structure and outstanding requests/responses */
|
2010-10-13 16:43:15 +08:00
|
|
|
LDAP_MUTEX_LOCK( &ld->ld_req_mutex );
|
2001-09-25 02:50:44 +08:00
|
|
|
while ( ld->ld_requests != NULL ) {
|
|
|
|
ldap_free_request( ld, ld->ld_requests );
|
2001-07-25 00:38:42 +08:00
|
|
|
}
|
2010-10-23 05:45:48 +08:00
|
|
|
LDAP_MUTEX_UNLOCK( &ld->ld_req_mutex );
|
|
|
|
LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2001-07-25 00:38:42 +08:00
|
|
|
/* free and unbind from all open connections */
|
|
|
|
while ( ld->ld_conns != NULL ) {
|
|
|
|
ldap_free_connection( ld, ld->ld_conns, 1, close );
|
|
|
|
}
|
2010-10-23 05:45:48 +08:00
|
|
|
LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
|
2010-10-13 16:43:15 +08:00
|
|
|
LDAP_MUTEX_LOCK( &ld->ld_res_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
|
|
|
|
next = lm->lm_next;
|
|
|
|
ldap_msgfree( lm );
|
|
|
|
}
|
2015-02-04 11:16:20 +08:00
|
|
|
|
2006-04-01 08:30:07 +08:00
|
|
|
if ( ld->ld_abandoned != NULL ) {
|
|
|
|
LDAP_FREE( ld->ld_abandoned );
|
|
|
|
ld->ld_abandoned = NULL;
|
|
|
|
}
|
2010-10-13 16:43:15 +08:00
|
|
|
LDAP_MUTEX_UNLOCK( &ld->ld_res_mutex );
|
2012-04-12 23:34:40 +08:00
|
|
|
|
|
|
|
ber_sockbuf_free( ld->ld_sb );
|
|
|
|
|
2010-10-23 05:45:48 +08:00
|
|
|
LDAP_MUTEX_LOCK( &ld->ld_ldopts_mutex );
|
1999-05-21 03:38:03 +08:00
|
|
|
|
2008-08-14 12:54:32 +08:00
|
|
|
/* final close callbacks */
|
|
|
|
{
|
|
|
|
ldaplist *ll, *next;
|
|
|
|
|
|
|
|
for ( ll = ld->ld_options.ldo_conn_cbs; ll; ll = next ) {
|
|
|
|
ldap_conncb *cb = ll->ll_data;
|
|
|
|
next = ll->ll_next;
|
|
|
|
cb->lc_del( ld, NULL, cb );
|
|
|
|
LDAP_FREE( ll );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-05-21 03:38:03 +08:00
|
|
|
if ( ld->ld_error != NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( ld->ld_error );
|
1999-05-21 03:38:03 +08:00
|
|
|
ld->ld_error = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ld->ld_matched != NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( ld->ld_matched );
|
1999-05-21 03:38:03 +08:00
|
|
|
ld->ld_matched = NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-23 05:45:48 +08:00
|
|
|
if ( ld->ld_referrals != NULL) {
|
2002-10-03 03:14:02 +08:00
|
|
|
LDAP_VFREE(ld->ld_referrals);
|
|
|
|
ld->ld_referrals = NULL;
|
2015-02-04 11:16:20 +08:00
|
|
|
}
|
|
|
|
|
1999-05-21 03:38:03 +08:00
|
|
|
if ( ld->ld_selectinfo != NULL ) {
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_free_select_info( ld->ld_selectinfo );
|
1999-05-21 03:38:03 +08:00
|
|
|
ld->ld_selectinfo = NULL;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-12-08 02:52:05 +08:00
|
|
|
if ( ld->ld_options.ldo_defludp != NULL ) {
|
|
|
|
ldap_free_urllist( ld->ld_options.ldo_defludp );
|
|
|
|
ld->ld_options.ldo_defludp = NULL;
|
1999-05-21 03:38:03 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2005-09-30 14:04:38 +08:00
|
|
|
#ifdef LDAP_CONNECTIONLESS
|
|
|
|
if ( ld->ld_options.ldo_peer != NULL ) {
|
|
|
|
LDAP_FREE( ld->ld_options.ldo_peer );
|
|
|
|
ld->ld_options.ldo_peer = NULL;
|
|
|
|
}
|
2010-06-02 11:10:39 +08:00
|
|
|
|
|
|
|
if ( ld->ld_options.ldo_cldapdn != NULL ) {
|
|
|
|
LDAP_FREE( ld->ld_options.ldo_cldapdn );
|
|
|
|
ld->ld_options.ldo_cldapdn = NULL;
|
|
|
|
}
|
2005-09-30 14:04:38 +08:00
|
|
|
#endif
|
|
|
|
|
2001-11-09 04:08:36 +08:00
|
|
|
#ifdef HAVE_CYRUS_SASL
|
2001-11-07 01:05:51 +08:00
|
|
|
if ( ld->ld_options.ldo_def_sasl_mech != NULL ) {
|
|
|
|
LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
|
|
|
|
ld->ld_options.ldo_def_sasl_mech = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ld->ld_options.ldo_def_sasl_realm != NULL ) {
|
|
|
|
LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
|
|
|
|
ld->ld_options.ldo_def_sasl_realm = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) {
|
|
|
|
LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
|
|
|
|
ld->ld_options.ldo_def_sasl_authcid = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) {
|
|
|
|
LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
|
|
|
|
ld->ld_options.ldo_def_sasl_authzid = NULL;
|
|
|
|
}
|
2001-11-09 04:08:36 +08:00
|
|
|
#endif
|
2001-11-07 01:05:51 +08:00
|
|
|
|
2006-04-07 08:52:38 +08:00
|
|
|
#ifdef HAVE_TLS
|
|
|
|
ldap_int_tls_destroy( &ld->ld_options );
|
|
|
|
#endif
|
|
|
|
|
2005-08-01 11:51:53 +08:00
|
|
|
if ( ld->ld_options.ldo_sctrls != NULL ) {
|
|
|
|
ldap_controls_free( ld->ld_options.ldo_sctrls );
|
|
|
|
ld->ld_options.ldo_sctrls = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ld->ld_options.ldo_cctrls != NULL ) {
|
|
|
|
ldap_controls_free( ld->ld_options.ldo_cctrls );
|
|
|
|
ld->ld_options.ldo_cctrls = NULL;
|
|
|
|
}
|
2010-10-23 05:45:48 +08:00
|
|
|
LDAP_MUTEX_UNLOCK( &ld->ld_ldopts_mutex );
|
2005-08-01 11:51:53 +08:00
|
|
|
|
2003-02-13 18:43:16 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
2010-10-23 05:45:48 +08:00
|
|
|
ldap_pvt_thread_mutex_destroy( &ld->ld_msgid_mutex );
|
|
|
|
ldap_pvt_thread_mutex_destroy( &ld->ld_conn_mutex );
|
2003-02-13 18:43:16 +08:00
|
|
|
ldap_pvt_thread_mutex_destroy( &ld->ld_req_mutex );
|
|
|
|
ldap_pvt_thread_mutex_destroy( &ld->ld_res_mutex );
|
2010-10-23 05:45:48 +08:00
|
|
|
ldap_pvt_thread_mutex_destroy( &ld->ld_abandon_mutex );
|
|
|
|
ldap_pvt_thread_mutex_destroy( &ld->ld_ldopts_mutex );
|
|
|
|
ldap_pvt_thread_mutex_destroy( &ld->ld_ldcmutex );
|
2005-12-13 23:53:10 +08:00
|
|
|
#endif
|
|
|
|
#ifndef NDEBUG
|
|
|
|
LDAP_TRASH(ld);
|
2003-02-13 18:43:16 +08:00
|
|
|
#endif
|
2010-10-23 05:45:48 +08:00
|
|
|
LDAP_FREE( (char *) ld->ldc );
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( (char *) ld );
|
2015-02-04 11:16:20 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( err );
|
|
|
|
}
|
|
|
|
|
2010-10-23 05:45:48 +08:00
|
|
|
int
|
|
|
|
ldap_destroy( LDAP *ld )
|
|
|
|
{
|
|
|
|
return ( ldap_ld_free( ld, 1, NULL, NULL ) );
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
int
|
|
|
|
ldap_unbind_s( LDAP *ld )
|
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
return( ldap_unbind_ext( ld, NULL, NULL ) );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2005-08-12 01:11:41 +08:00
|
|
|
/* FIXME: this function is called only by ldap_free_connection(),
|
|
|
|
* which, most of the times, is called with ld_req_mutex locked */
|
1998-08-09 08:43:13 +08:00
|
|
|
int
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_send_unbind(
|
|
|
|
LDAP *ld,
|
|
|
|
Sockbuf *sb,
|
|
|
|
LDAPControl **sctrls,
|
|
|
|
LDAPControl **cctrls )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
BerElement *ber;
|
2003-02-14 07:02:31 +08:00
|
|
|
ber_int_t id;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2001-09-28 08:18:40 +08:00
|
|
|
#ifdef LDAP_CONNECTIONLESS
|
|
|
|
if (LDAP_IS_UDP(ld))
|
|
|
|
return LDAP_SUCCESS;
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
/* create a message to send */
|
1999-06-19 07:53:05 +08:00
|
|
|
if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( ld->ld_errno );
|
|
|
|
}
|
|
|
|
|
2010-10-23 05:45:48 +08:00
|
|
|
LDAP_NEXT_MSGID(ld, id);
|
2005-08-12 01:11:41 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/* fill it in */
|
2003-02-14 07:02:31 +08:00
|
|
|
if ( ber_printf( ber, "{itn" /*}*/, id,
|
1999-05-19 09:12:33 +08:00
|
|
|
LDAP_REQ_UNBIND ) == -1 ) {
|
|
|
|
ld->ld_errno = LDAP_ENCODING_ERROR;
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
return( ld->ld_errno );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put Server Controls */
|
|
|
|
if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
return ld->ld_errno;
|
|
|
|
}
|
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_ENCODING_ERROR;
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
return( ld->ld_errno );
|
|
|
|
}
|
|
|
|
|
2003-02-13 18:43:16 +08:00
|
|
|
ld->ld_errno = LDAP_SUCCESS;
|
1998-08-09 08:43:13 +08:00
|
|
|
/* send the message */
|
2006-04-10 06:29:42 +08:00
|
|
|
if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) == -1 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_SERVER_DOWN;
|
|
|
|
}
|
|
|
|
|
2003-02-13 18:43:16 +08:00
|
|
|
return( ld->ld_errno );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|