17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
87c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
97c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
107c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
137c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
147c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
157c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
167c478bd9Sstevel@tonic-gate  *
177c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
187c478bd9Sstevel@tonic-gate  * March 31, 1998.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
217c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
227c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
237c478bd9Sstevel@tonic-gate  * Rights Reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Contributor(s):
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
297c478bd9Sstevel@tonic-gate  *  All rights reserved.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *  unbind.c
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #if 0
37*1da57d55SToomas Soome #ifndef lint
387c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "ldap-int.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate int
457c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_unbind(LDAP * ld)467c478bd9Sstevel@tonic-gate ldap_unbind( LDAP *ld )
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	return( ldap_ld_free( ld, NULL, NULL, 1 ) );
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate int
557c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_unbind_s(LDAP * ld)567c478bd9Sstevel@tonic-gate ldap_unbind_s( LDAP *ld )
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	return( ldap_ld_free( ld, NULL, NULL, 1 ));
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate int
637c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_unbind_ext(LDAP * ld,LDAPControl ** serverctrls,LDAPControl ** clientctrls)647c478bd9Sstevel@tonic-gate ldap_unbind_ext( LDAP *ld, LDAPControl **serverctrls,
657c478bd9Sstevel@tonic-gate     LDAPControl **clientctrls )
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	return( ldap_ld_free( ld, serverctrls, clientctrls, 1 ));
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Dispose of the LDAP session ld, including all associated connections
737c478bd9Sstevel@tonic-gate  * and resources.  If close is non-zero, an unbind() request is sent as well.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate int
ldap_ld_free(LDAP * ld,LDAPControl ** serverctrls,LDAPControl ** clientctrls,int close)767c478bd9Sstevel@tonic-gate ldap_ld_free( LDAP *ld, LDAPControl **serverctrls,
777c478bd9Sstevel@tonic-gate     LDAPControl **clientctrls, int close )
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	LDAPMessage	*lm, *next;
807c478bd9Sstevel@tonic-gate 	int		err = LDAP_SUCCESS;
817c478bd9Sstevel@tonic-gate 	LDAPRequest	*lr, *nextlr;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
847c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if ( ld->ld_sbp->sb_naddr == 0 ) {
887c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_LOCK( ld, LDAP_REQ_LOCK );
897c478bd9Sstevel@tonic-gate 		/* free LDAP structure and outstanding requests/responses */
907c478bd9Sstevel@tonic-gate 		for ( lr = ld->ld_requests; lr != NULL; lr = nextlr ) {
917c478bd9Sstevel@tonic-gate 			nextlr = lr->lr_next;
927c478bd9Sstevel@tonic-gate 			nsldapi_free_request( ld, lr, 0 );
937c478bd9Sstevel@tonic-gate 		}
947c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_UNLOCK( ld, LDAP_REQ_LOCK );
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 		/* free and unbind from all open connections */
977c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
987c478bd9Sstevel@tonic-gate 		while ( ld->ld_conns != NULL ) {
997c478bd9Sstevel@tonic-gate 			nsldapi_free_connection( ld, ld->ld_conns, serverctrls,
1007c478bd9Sstevel@tonic-gate 			    clientctrls, 1, close );
1017c478bd9Sstevel@tonic-gate 		}
1027c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	} else {
1057c478bd9Sstevel@tonic-gate 		int	i;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		for ( i = 0; i < ld->ld_sbp->sb_naddr; ++i ) {
1087c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( ld->ld_sbp->sb_addrs[ i ] );
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_sbp->sb_addrs );
1117c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_sbp->sb_fromaddr );
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_RESP_LOCK );
1157c478bd9Sstevel@tonic-gate 	for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
1167c478bd9Sstevel@tonic-gate 		next = lm->lm_next;
1177c478bd9Sstevel@tonic-gate 		ldap_msgfree( lm );
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_RESP_LOCK );
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/* call cache unbind function to allow it to clean up after itself */
1227c478bd9Sstevel@tonic-gate 	if ( ld->ld_cache_unbind != NULL ) {
1237c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
1247c478bd9Sstevel@tonic-gate 		(void)ld->ld_cache_unbind( ld, 0, 0 );
1257c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	/* call the dispose handle I/O callback if one is defined */
1297c478bd9Sstevel@tonic-gate 	if ( ld->ld_extdisposehandle_fn != NULL ) {
1307c478bd9Sstevel@tonic-gate 	    /*
1317c478bd9Sstevel@tonic-gate 	     * We always pass the session extended I/O argument to
1327c478bd9Sstevel@tonic-gate 	     * the dispose handle callback.
1337c478bd9Sstevel@tonic-gate 	     */
1347c478bd9Sstevel@tonic-gate 	    ld->ld_extdisposehandle_fn( ld, ld->ld_ext_session_arg );
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if ( ld->ld_error != NULL )
1387c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_error );
1397c478bd9Sstevel@tonic-gate 	if ( ld->ld_matched != NULL )
1407c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_matched );
1417c478bd9Sstevel@tonic-gate 	if ( ld->ld_host != NULL )
1427c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_host );
1437c478bd9Sstevel@tonic-gate 	if ( ld->ld_ufnprefix != NULL )
1447c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_ufnprefix );
1457c478bd9Sstevel@tonic-gate 	if ( ld->ld_filtd != NULL )
1467c478bd9Sstevel@tonic-gate 		ldap_getfilter_free( ld->ld_filtd );
1477c478bd9Sstevel@tonic-gate 	if ( ld->ld_abandoned != NULL )
1487c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_abandoned );
1497c478bd9Sstevel@tonic-gate 	if ( ld->ld_sbp != NULL )
1507c478bd9Sstevel@tonic-gate 		ber_sockbuf_free( ld->ld_sbp );
1517c478bd9Sstevel@tonic-gate 	if ( ld->ld_defhost != NULL )
1527c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_defhost );
1537c478bd9Sstevel@tonic-gate 	if ( ld->ld_servercontrols != NULL )
1547c478bd9Sstevel@tonic-gate 		ldap_controls_free( ld->ld_servercontrols );
1557c478bd9Sstevel@tonic-gate 	if ( ld->ld_clientcontrols != NULL )
1567c478bd9Sstevel@tonic-gate 		ldap_controls_free( ld->ld_clientcontrols );
1577c478bd9Sstevel@tonic-gate 	if ( ld->ld_preferred_language != NULL )
1587c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_preferred_language );
1597c478bd9Sstevel@tonic-gate 	nsldapi_iostatus_free( ld );
1607c478bd9Sstevel@tonic-gate #ifdef LDAP_SASLIO_HOOKS
1617c478bd9Sstevel@tonic-gate 	if ( ld->ld_def_sasl_mech != NULL )
1627c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_def_sasl_mech );
1637c478bd9Sstevel@tonic-gate 	if ( ld->ld_def_sasl_realm != NULL )
1647c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_def_sasl_realm );
1657c478bd9Sstevel@tonic-gate 	if ( ld->ld_def_sasl_authcid != NULL )
1667c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_def_sasl_authcid );
1677c478bd9Sstevel@tonic-gate 	if ( ld->ld_def_sasl_authzid != NULL )
1687c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_def_sasl_authzid );
1697c478bd9Sstevel@tonic-gate #endif
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * XXXmcs: should use cache function pointers to hook in memcache
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	if ( ld->ld_memcache != NULL ) {
1757c478bd9Sstevel@tonic-gate 		ldap_memcache_set( ld, NULL );
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate         /* free all mutexes we have allocated */
1797c478bd9Sstevel@tonic-gate         nsldapi_mutex_free_all( ld );
1807c478bd9Sstevel@tonic-gate         NSLDAPI_FREE( ld->ld_mutex );
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate         NSLDAPI_FREE( (char *) ld );
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	return( err );
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate int
nsldapi_send_unbind(LDAP * ld,Sockbuf * sb,LDAPControl ** serverctrls,LDAPControl ** clientctrls)1907c478bd9Sstevel@tonic-gate nsldapi_send_unbind( LDAP *ld, Sockbuf *sb, LDAPControl **serverctrls,
1917c478bd9Sstevel@tonic-gate     LDAPControl **clientctrls )
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	BerElement	*ber;
1947c478bd9Sstevel@tonic-gate 	int		err, msgid;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_send_unbind\n", 0, 0, 0 );
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/* create a message to send */
1997c478bd9Sstevel@tonic-gate 	if (( err = nsldapi_alloc_ber_with_options( ld, &ber ))
2007c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
2017c478bd9Sstevel@tonic-gate 		return( err );
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/* fill it in */
2057c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
2067c478bd9Sstevel@tonic-gate 	msgid = ++ld->ld_msgid;
2077c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if ( ber_printf( ber, "{itn", msgid, LDAP_REQ_UNBIND ) == -1 ) {
2107c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
2117c478bd9Sstevel@tonic-gate 		err = LDAP_ENCODING_ERROR;
2127c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, err, NULL, NULL );
2137c478bd9Sstevel@tonic-gate 		return( err );
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (( err = nsldapi_put_controls( ld, serverctrls, 1, ber ))
2177c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
2187c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
2197c478bd9Sstevel@tonic-gate 		return( err );
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* send the message */
2237c478bd9Sstevel@tonic-gate 	if ( nsldapi_ber_flush( ld, sb, ber, 1, 0 ) != 0 ) {
2247c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
2257c478bd9Sstevel@tonic-gate 		err = LDAP_SERVER_DOWN;
2267c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, err, NULL, NULL );
2277c478bd9Sstevel@tonic-gate 		return( err );
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	return( LDAP_SUCCESS );
2317c478bd9Sstevel@tonic-gate }
232