xref: /onnv-gate/usr/src/lib/libldap4/common/unbind.c (revision 3857:21b9b714e4ab)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  *
3*3857Sstevel  * Portions Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
4*3857Sstevel  * Use is subject to license terms.
50Sstevel@tonic-gate  *
60Sstevel@tonic-gate  */
70Sstevel@tonic-gate 
80Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
90Sstevel@tonic-gate 
100Sstevel@tonic-gate /*
110Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
120Sstevel@tonic-gate  *  All rights reserved.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  *  unbind.c
150Sstevel@tonic-gate  */
160Sstevel@tonic-gate 
170Sstevel@tonic-gate #ifndef lint
180Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
190Sstevel@tonic-gate #endif
200Sstevel@tonic-gate 
210Sstevel@tonic-gate #include <stdio.h>
220Sstevel@tonic-gate #include <string.h>
230Sstevel@tonic-gate #ifdef MACOS
240Sstevel@tonic-gate #include <stdlib.h>
250Sstevel@tonic-gate #include "macos.h"
260Sstevel@tonic-gate #else /* MACOS */
270Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
280Sstevel@tonic-gate #include "msdos.h"
290Sstevel@tonic-gate #ifdef NCSA
300Sstevel@tonic-gate #include "externs.h"
310Sstevel@tonic-gate #endif /* NCSA */
320Sstevel@tonic-gate #else /* DOS */
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/time.h>
350Sstevel@tonic-gate #include <sys/socket.h>
360Sstevel@tonic-gate #endif /* DOS */
370Sstevel@tonic-gate #endif /* MACOS */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include "lber.h"
400Sstevel@tonic-gate #include "ldap.h"
410Sstevel@tonic-gate #include "ldap-private.h"
420Sstevel@tonic-gate #include "ldap-int.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 
450Sstevel@tonic-gate int
ldap_unbind(LDAP * ld)460Sstevel@tonic-gate ldap_unbind( LDAP *ld )
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 259, "ldap_unbind\n"), 0, 0, 0 );
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	return( ldap_ld_free( ld, 1 ));
510Sstevel@tonic-gate }
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 
540Sstevel@tonic-gate int
ldap_ld_free(LDAP * ld,int close)550Sstevel@tonic-gate ldap_ld_free( LDAP *ld, int close )
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	LDAPMessage	*lm, *next;
580Sstevel@tonic-gate 	int		err = LDAP_SUCCESS;
590Sstevel@tonic-gate 	LDAPRequest	*lr, *nextlr;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #ifdef _REENTRANT
620Sstevel@tonic-gate         LOCK_LDAP(ld);
630Sstevel@tonic-gate #endif
640Sstevel@tonic-gate 	if ( ld->ld_sb.sb_naddr == 0 ) {
650Sstevel@tonic-gate 		/* free LDAP structure and outstanding requests/responses */
660Sstevel@tonic-gate 		for ( lr = ld->ld_requests; lr != NULL; lr = nextlr ) {
670Sstevel@tonic-gate 			nextlr = lr->lr_next;
680Sstevel@tonic-gate 			free_request( ld, lr );
690Sstevel@tonic-gate 		}
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 		/* free and unbind from all open connections */
720Sstevel@tonic-gate 		while ( ld->ld_conns != NULL ) {
730Sstevel@tonic-gate 			free_connection( ld, ld->ld_conns, 1, close );
740Sstevel@tonic-gate 		}
750Sstevel@tonic-gate 	} else {
760Sstevel@tonic-gate 		int	i;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 		for ( i = 0; i < ld->ld_sb.sb_naddr; ++i ) {
790Sstevel@tonic-gate 			free( ld->ld_sb.sb_addrs[ i ] );
800Sstevel@tonic-gate 		}
810Sstevel@tonic-gate 		free( ld->ld_sb.sb_addrs );
820Sstevel@tonic-gate 		free( ld->ld_sb.sb_fromaddr );
830Sstevel@tonic-gate #ifdef LDAP_SSL
840Sstevel@tonic-gate 		if (ld->ld_sb.sb_ssl){
850Sstevel@tonic-gate 			SSL_delete(ld->ld_sb.sb_ssl);
860Sstevel@tonic-gate 		}
870Sstevel@tonic-gate 			ld->ld_sb.sb_ssl = NULL;
880Sstevel@tonic-gate 			ld->ld_sb.sb_ssl_tls = 0;
890Sstevel@tonic-gate #endif
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	if (ld->ld_sb.sb_ber.ber_buf) {
930Sstevel@tonic-gate 		free(ld->ld_sb.sb_ber.ber_buf);
940Sstevel@tonic-gate 		ld->ld_sb.sb_ber.ber_buf = NULL;
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 
970Sstevel@tonic-gate #ifdef _REENTRANT
980Sstevel@tonic-gate 	LOCK_RESPONSE(ld);
990Sstevel@tonic-gate #endif
1000Sstevel@tonic-gate 	for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
1010Sstevel@tonic-gate 		next = lm->lm_next;
1020Sstevel@tonic-gate 		ldap_msgfree( lm );
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate #ifdef _REENTRANT
1060Sstevel@tonic-gate 	UNLOCK_RESPONSE(ld);
1070Sstevel@tonic-gate #endif
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #ifndef NO_CACHE
1100Sstevel@tonic-gate 	if ( ld->ld_cache != NULL )
1110Sstevel@tonic-gate 		ldap_destroy_cache( ld );
1120Sstevel@tonic-gate #endif /* !NO_CACHE */
1130Sstevel@tonic-gate 	if ( ld->ld_error != NULL )
1140Sstevel@tonic-gate 		free( ld->ld_error );
1150Sstevel@tonic-gate 	if ( ld->ld_matched != NULL )
1160Sstevel@tonic-gate 		free( ld->ld_matched );
1170Sstevel@tonic-gate 	if ( ld->ld_host != NULL )
1180Sstevel@tonic-gate 		free( ld->ld_host );
1190Sstevel@tonic-gate 	if ( ld->ld_ufnprefix != NULL )
1200Sstevel@tonic-gate 		free( ld->ld_ufnprefix );
1210Sstevel@tonic-gate 	if ( ld->ld_filtd != NULL )
1220Sstevel@tonic-gate 		ldap_getfilter_free( ld->ld_filtd );
1230Sstevel@tonic-gate 	if ( ld->ld_abandoned != NULL )
1240Sstevel@tonic-gate 		free( ld->ld_abandoned );
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	if ( ld->ld_selectinfo != NULL )
1270Sstevel@tonic-gate 		free_select_info( ld->ld_selectinfo );
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	if ( ld->ld_defhost != NULL )
1300Sstevel@tonic-gate 		free( ld->ld_defhost );
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate #ifdef LDAP_SSL
1330Sstevel@tonic-gate 	if (ld->ld_ssl_key != NULL)
1340Sstevel@tonic-gate 		free(ld->ld_ssl_key);
1350Sstevel@tonic-gate #endif
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate #undef ld_attrbuffer
1380Sstevel@tonic-gate 	{
1390Sstevel@tonic-gate 		/* free thread-specific attr buffers */
1400Sstevel@tonic-gate 		int i;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		for (i = 0; i < MAX_THREAD_ID; i++)
1430Sstevel@tonic-gate 			if (ld->ld_attrbuffer[i] != NULL) {
1440Sstevel@tonic-gate 				free(ld->ld_attrbuffer[i]);
1450Sstevel@tonic-gate 				ld->ld_attrbuffer[i] = NULL;
1460Sstevel@tonic-gate 			}
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate /* free ldapv3 stuff */
1490Sstevel@tonic-gate 	if (ld->ld_srvctrls != NULL)
1500Sstevel@tonic-gate 		ldap_controls_free(ld->ld_srvctrls);
1510Sstevel@tonic-gate 	if (ld->ld_cltctrls != NULL)
1520Sstevel@tonic-gate 		ldap_controls_free(ld->ld_cltctrls);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate #ifdef _REENTRANT
1550Sstevel@tonic-gate         UNLOCK_LDAP(ld);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (ld->ld_lockcount != 0)
1580Sstevel@tonic-gate 		Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 260, "Mutex problem: ld_lockcount not equal to zero when freeing context\n"), 0, 0, 0 );
1590Sstevel@tonic-gate #endif
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	free( (char *) ld );
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	return( err );
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate int
ldap_unbind_s(LDAP * ld)1670Sstevel@tonic-gate ldap_unbind_s( LDAP *ld )
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate 	return( ldap_ld_free( ld, 1 ));
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate int
send_unbind(LDAP * ld,Sockbuf * sb)1740Sstevel@tonic-gate send_unbind( LDAP *ld, Sockbuf *sb )
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	BerElement	*ber;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT )
1790Sstevel@tonic-gate         LOCK_LDAP(ld);
1800Sstevel@tonic-gate #endif
1810Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 261, "send_unbind\n"), 0, 0, 0 );
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	/* create a message to send */
1840Sstevel@tonic-gate 	if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
1850Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT )
1860Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1870Sstevel@tonic-gate #endif
1880Sstevel@tonic-gate 		return( ld->ld_errno );
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/* fill it in */
1920Sstevel@tonic-gate 	if ( ber_printf( ber, "{itn}", ++ld->ld_msgid,
1930Sstevel@tonic-gate 	    LDAP_REQ_UNBIND ) == -1 ) {
1940Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
1950Sstevel@tonic-gate 		ber_free( ber, 1 );
1960Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT )
1970Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1980Sstevel@tonic-gate #endif
1990Sstevel@tonic-gate 		return( ld->ld_errno );
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/* send the message */
2030Sstevel@tonic-gate 	if ( ber_flush( sb, ber, 1 ) == -1 ) {
2040Sstevel@tonic-gate 		ld->ld_errno = LDAP_SERVER_DOWN;
2050Sstevel@tonic-gate 		ber_free( ber, 1 );
2060Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT )
2070Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
2080Sstevel@tonic-gate #endif
2090Sstevel@tonic-gate 		return( ld->ld_errno );
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT )
2130Sstevel@tonic-gate         UNLOCK_LDAP(ld);
2140Sstevel@tonic-gate #endif
2150Sstevel@tonic-gate 	return( LDAP_SUCCESS );
2160Sstevel@tonic-gate }
217