1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  *
3*0Sstevel@tonic-gate  * Portions Copyright %G% Sun Microsystems, Inc.
4*0Sstevel@tonic-gate  * All Rights Reserved
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate /*
11*0Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
12*0Sstevel@tonic-gate  *  All rights reserved.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  *  unbind.c
15*0Sstevel@tonic-gate  */
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate #ifndef lint
18*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
19*0Sstevel@tonic-gate #endif
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #include <stdio.h>
22*0Sstevel@tonic-gate #include <string.h>
23*0Sstevel@tonic-gate #ifdef MACOS
24*0Sstevel@tonic-gate #include <stdlib.h>
25*0Sstevel@tonic-gate #include "macos.h"
26*0Sstevel@tonic-gate #else /* MACOS */
27*0Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
28*0Sstevel@tonic-gate #include "msdos.h"
29*0Sstevel@tonic-gate #ifdef NCSA
30*0Sstevel@tonic-gate #include "externs.h"
31*0Sstevel@tonic-gate #endif /* NCSA */
32*0Sstevel@tonic-gate #else /* DOS */
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/time.h>
35*0Sstevel@tonic-gate #include <sys/socket.h>
36*0Sstevel@tonic-gate #endif /* DOS */
37*0Sstevel@tonic-gate #endif /* MACOS */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include "lber.h"
40*0Sstevel@tonic-gate #include "ldap.h"
41*0Sstevel@tonic-gate #include "ldap-private.h"
42*0Sstevel@tonic-gate #include "ldap-int.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate int
46*0Sstevel@tonic-gate ldap_unbind( LDAP *ld )
47*0Sstevel@tonic-gate {
48*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 259, "ldap_unbind\n"), 0, 0, 0 );
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 	return( ldap_ld_free( ld, 1 ));
51*0Sstevel@tonic-gate }
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate int
55*0Sstevel@tonic-gate ldap_ld_free( LDAP *ld, int close )
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate 	LDAPMessage	*lm, *next;
58*0Sstevel@tonic-gate 	int		err = LDAP_SUCCESS;
59*0Sstevel@tonic-gate 	LDAPRequest	*lr, *nextlr;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #ifdef _REENTRANT
62*0Sstevel@tonic-gate         LOCK_LDAP(ld);
63*0Sstevel@tonic-gate #endif
64*0Sstevel@tonic-gate 	if ( ld->ld_sb.sb_naddr == 0 ) {
65*0Sstevel@tonic-gate 		/* free LDAP structure and outstanding requests/responses */
66*0Sstevel@tonic-gate 		for ( lr = ld->ld_requests; lr != NULL; lr = nextlr ) {
67*0Sstevel@tonic-gate 			nextlr = lr->lr_next;
68*0Sstevel@tonic-gate 			free_request( ld, lr );
69*0Sstevel@tonic-gate 		}
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 		/* free and unbind from all open connections */
72*0Sstevel@tonic-gate 		while ( ld->ld_conns != NULL ) {
73*0Sstevel@tonic-gate 			free_connection( ld, ld->ld_conns, 1, close );
74*0Sstevel@tonic-gate 		}
75*0Sstevel@tonic-gate 	} else {
76*0Sstevel@tonic-gate 		int	i;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 		for ( i = 0; i < ld->ld_sb.sb_naddr; ++i ) {
79*0Sstevel@tonic-gate 			free( ld->ld_sb.sb_addrs[ i ] );
80*0Sstevel@tonic-gate 		}
81*0Sstevel@tonic-gate 		free( ld->ld_sb.sb_addrs );
82*0Sstevel@tonic-gate 		free( ld->ld_sb.sb_fromaddr );
83*0Sstevel@tonic-gate #ifdef LDAP_SSL
84*0Sstevel@tonic-gate 		if (ld->ld_sb.sb_ssl){
85*0Sstevel@tonic-gate 			SSL_delete(ld->ld_sb.sb_ssl);
86*0Sstevel@tonic-gate 		}
87*0Sstevel@tonic-gate 			ld->ld_sb.sb_ssl = NULL;
88*0Sstevel@tonic-gate 			ld->ld_sb.sb_ssl_tls = 0;
89*0Sstevel@tonic-gate #endif
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	if (ld->ld_sb.sb_ber.ber_buf) {
93*0Sstevel@tonic-gate 		free(ld->ld_sb.sb_ber.ber_buf);
94*0Sstevel@tonic-gate 		ld->ld_sb.sb_ber.ber_buf = NULL;
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate #ifdef _REENTRANT
98*0Sstevel@tonic-gate 	LOCK_RESPONSE(ld);
99*0Sstevel@tonic-gate #endif
100*0Sstevel@tonic-gate 	for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
101*0Sstevel@tonic-gate 		next = lm->lm_next;
102*0Sstevel@tonic-gate 		ldap_msgfree( lm );
103*0Sstevel@tonic-gate 	}
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate #ifdef _REENTRANT
106*0Sstevel@tonic-gate 	UNLOCK_RESPONSE(ld);
107*0Sstevel@tonic-gate #endif
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate #ifndef NO_CACHE
110*0Sstevel@tonic-gate 	if ( ld->ld_cache != NULL )
111*0Sstevel@tonic-gate 		ldap_destroy_cache( ld );
112*0Sstevel@tonic-gate #endif /* !NO_CACHE */
113*0Sstevel@tonic-gate 	if ( ld->ld_error != NULL )
114*0Sstevel@tonic-gate 		free( ld->ld_error );
115*0Sstevel@tonic-gate 	if ( ld->ld_matched != NULL )
116*0Sstevel@tonic-gate 		free( ld->ld_matched );
117*0Sstevel@tonic-gate 	if ( ld->ld_host != NULL )
118*0Sstevel@tonic-gate 		free( ld->ld_host );
119*0Sstevel@tonic-gate 	if ( ld->ld_ufnprefix != NULL )
120*0Sstevel@tonic-gate 		free( ld->ld_ufnprefix );
121*0Sstevel@tonic-gate 	if ( ld->ld_filtd != NULL )
122*0Sstevel@tonic-gate 		ldap_getfilter_free( ld->ld_filtd );
123*0Sstevel@tonic-gate 	if ( ld->ld_abandoned != NULL )
124*0Sstevel@tonic-gate 		free( ld->ld_abandoned );
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	if ( ld->ld_selectinfo != NULL )
127*0Sstevel@tonic-gate 		free_select_info( ld->ld_selectinfo );
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	if ( ld->ld_defhost != NULL )
130*0Sstevel@tonic-gate 		free( ld->ld_defhost );
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate #ifdef LDAP_SSL
133*0Sstevel@tonic-gate 	if (ld->ld_ssl_key != NULL)
134*0Sstevel@tonic-gate 		free(ld->ld_ssl_key);
135*0Sstevel@tonic-gate #endif
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate #undef ld_attrbuffer
138*0Sstevel@tonic-gate 	{
139*0Sstevel@tonic-gate 		/* free thread-specific attr buffers */
140*0Sstevel@tonic-gate 		int i;
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 		for (i = 0; i < MAX_THREAD_ID; i++)
143*0Sstevel@tonic-gate 			if (ld->ld_attrbuffer[i] != NULL) {
144*0Sstevel@tonic-gate 				free(ld->ld_attrbuffer[i]);
145*0Sstevel@tonic-gate 				ld->ld_attrbuffer[i] = NULL;
146*0Sstevel@tonic-gate 			}
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate /* free ldapv3 stuff */
149*0Sstevel@tonic-gate 	if (ld->ld_srvctrls != NULL)
150*0Sstevel@tonic-gate 		ldap_controls_free(ld->ld_srvctrls);
151*0Sstevel@tonic-gate 	if (ld->ld_cltctrls != NULL)
152*0Sstevel@tonic-gate 		ldap_controls_free(ld->ld_cltctrls);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate #ifdef _REENTRANT
155*0Sstevel@tonic-gate         UNLOCK_LDAP(ld);
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	if (ld->ld_lockcount != 0)
158*0Sstevel@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 );
159*0Sstevel@tonic-gate #endif
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	free( (char *) ld );
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	return( err );
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate int
167*0Sstevel@tonic-gate ldap_unbind_s( LDAP *ld )
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	return( ldap_ld_free( ld, 1 ));
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate int
174*0Sstevel@tonic-gate send_unbind( LDAP *ld, Sockbuf *sb )
175*0Sstevel@tonic-gate {
176*0Sstevel@tonic-gate 	BerElement	*ber;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT )
179*0Sstevel@tonic-gate         LOCK_LDAP(ld);
180*0Sstevel@tonic-gate #endif
181*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 261, "send_unbind\n"), 0, 0, 0 );
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	/* create a message to send */
184*0Sstevel@tonic-gate 	if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
185*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT )
186*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
187*0Sstevel@tonic-gate #endif
188*0Sstevel@tonic-gate 		return( ld->ld_errno );
189*0Sstevel@tonic-gate 	}
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	/* fill it in */
192*0Sstevel@tonic-gate 	if ( ber_printf( ber, "{itn}", ++ld->ld_msgid,
193*0Sstevel@tonic-gate 	    LDAP_REQ_UNBIND ) == -1 ) {
194*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
195*0Sstevel@tonic-gate 		ber_free( ber, 1 );
196*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT )
197*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
198*0Sstevel@tonic-gate #endif
199*0Sstevel@tonic-gate 		return( ld->ld_errno );
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	/* send the message */
203*0Sstevel@tonic-gate 	if ( ber_flush( sb, ber, 1 ) == -1 ) {
204*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_SERVER_DOWN;
205*0Sstevel@tonic-gate 		ber_free( ber, 1 );
206*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT )
207*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
208*0Sstevel@tonic-gate #endif
209*0Sstevel@tonic-gate 		return( ld->ld_errno );
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT )
213*0Sstevel@tonic-gate         UNLOCK_LDAP(ld);
214*0Sstevel@tonic-gate #endif
215*0Sstevel@tonic-gate 	return( LDAP_SUCCESS );
216*0Sstevel@tonic-gate }
217