xref: /titanic_50/usr/src/lib/libldap5/sources/ldap/common/unbind.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*7c478bd9Sstevel@tonic-gate 
8*7c478bd9Sstevel@tonic-gate /*
9*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
10*7c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
11*7c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
12*7c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
15*7c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
16*7c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
17*7c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
18*7c478bd9Sstevel@tonic-gate  *
19*7c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
20*7c478bd9Sstevel@tonic-gate  * March 31, 1998.
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
23*7c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
24*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25*7c478bd9Sstevel@tonic-gate  * Rights Reserved.
26*7c478bd9Sstevel@tonic-gate  *
27*7c478bd9Sstevel@tonic-gate  * Contributor(s):
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
31*7c478bd9Sstevel@tonic-gate  *  All rights reserved.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  *  unbind.c
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #if 0
39*7c478bd9Sstevel@tonic-gate #ifndef lint
40*7c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate #endif
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate int
47*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_unbind(LDAP * ld)48*7c478bd9Sstevel@tonic-gate ldap_unbind( LDAP *ld )
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	return( ldap_ld_free( ld, NULL, NULL, 1 ) );
53*7c478bd9Sstevel@tonic-gate }
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate int
57*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_unbind_s(LDAP * ld)58*7c478bd9Sstevel@tonic-gate ldap_unbind_s( LDAP *ld )
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate 	return( ldap_ld_free( ld, NULL, NULL, 1 ));
61*7c478bd9Sstevel@tonic-gate }
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate int
65*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_unbind_ext(LDAP * ld,LDAPControl ** serverctrls,LDAPControl ** clientctrls)66*7c478bd9Sstevel@tonic-gate ldap_unbind_ext( LDAP *ld, LDAPControl **serverctrls,
67*7c478bd9Sstevel@tonic-gate     LDAPControl **clientctrls )
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate 	return( ldap_ld_free( ld, serverctrls, clientctrls, 1 ));
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /*
74*7c478bd9Sstevel@tonic-gate  * Dispose of the LDAP session ld, including all associated connections
75*7c478bd9Sstevel@tonic-gate  * and resources.  If close is non-zero, an unbind() request is sent as well.
76*7c478bd9Sstevel@tonic-gate  */
77*7c478bd9Sstevel@tonic-gate int
ldap_ld_free(LDAP * ld,LDAPControl ** serverctrls,LDAPControl ** clientctrls,int close)78*7c478bd9Sstevel@tonic-gate ldap_ld_free( LDAP *ld, LDAPControl **serverctrls,
79*7c478bd9Sstevel@tonic-gate     LDAPControl **clientctrls, int close )
80*7c478bd9Sstevel@tonic-gate {
81*7c478bd9Sstevel@tonic-gate 	LDAPMessage	*lm, *next;
82*7c478bd9Sstevel@tonic-gate 	int		err = LDAP_SUCCESS;
83*7c478bd9Sstevel@tonic-gate 	LDAPRequest	*lr, *nextlr;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
86*7c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
87*7c478bd9Sstevel@tonic-gate 	}
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_sbp->sb_naddr == 0 ) {
90*7c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_LOCK( ld, LDAP_REQ_LOCK );
91*7c478bd9Sstevel@tonic-gate 		/* free LDAP structure and outstanding requests/responses */
92*7c478bd9Sstevel@tonic-gate 		for ( lr = ld->ld_requests; lr != NULL; lr = nextlr ) {
93*7c478bd9Sstevel@tonic-gate 			nextlr = lr->lr_next;
94*7c478bd9Sstevel@tonic-gate 			nsldapi_free_request( ld, lr, 0 );
95*7c478bd9Sstevel@tonic-gate 		}
96*7c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_UNLOCK( ld, LDAP_REQ_LOCK );
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 		/* free and unbind from all open connections */
99*7c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
100*7c478bd9Sstevel@tonic-gate 		while ( ld->ld_conns != NULL ) {
101*7c478bd9Sstevel@tonic-gate 			nsldapi_free_connection( ld, ld->ld_conns, serverctrls,
102*7c478bd9Sstevel@tonic-gate 			    clientctrls, 1, close );
103*7c478bd9Sstevel@tonic-gate 		}
104*7c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	} else {
107*7c478bd9Sstevel@tonic-gate 		int	i;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		for ( i = 0; i < ld->ld_sbp->sb_naddr; ++i ) {
110*7c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( ld->ld_sbp->sb_addrs[ i ] );
111*7c478bd9Sstevel@tonic-gate 		}
112*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_sbp->sb_addrs );
113*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_sbp->sb_fromaddr );
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_RESP_LOCK );
117*7c478bd9Sstevel@tonic-gate 	for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
118*7c478bd9Sstevel@tonic-gate 		next = lm->lm_next;
119*7c478bd9Sstevel@tonic-gate 		ldap_msgfree( lm );
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_RESP_LOCK );
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	/* call cache unbind function to allow it to clean up after itself */
124*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_cache_unbind != NULL ) {
125*7c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
126*7c478bd9Sstevel@tonic-gate 		(void)ld->ld_cache_unbind( ld, 0, 0 );
127*7c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	/* call the dispose handle I/O callback if one is defined */
131*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_extdisposehandle_fn != NULL ) {
132*7c478bd9Sstevel@tonic-gate 	    /*
133*7c478bd9Sstevel@tonic-gate 	     * We always pass the session extended I/O argument to
134*7c478bd9Sstevel@tonic-gate 	     * the dispose handle callback.
135*7c478bd9Sstevel@tonic-gate 	     */
136*7c478bd9Sstevel@tonic-gate 	    ld->ld_extdisposehandle_fn( ld, ld->ld_ext_session_arg );
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_error != NULL )
140*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_error );
141*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_matched != NULL )
142*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_matched );
143*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_host != NULL )
144*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_host );
145*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_ufnprefix != NULL )
146*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_ufnprefix );
147*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_filtd != NULL )
148*7c478bd9Sstevel@tonic-gate 		ldap_getfilter_free( ld->ld_filtd );
149*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_abandoned != NULL )
150*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_abandoned );
151*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_sbp != NULL )
152*7c478bd9Sstevel@tonic-gate 		ber_sockbuf_free( ld->ld_sbp );
153*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_defhost != NULL )
154*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_defhost );
155*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_servercontrols != NULL )
156*7c478bd9Sstevel@tonic-gate 		ldap_controls_free( ld->ld_servercontrols );
157*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_clientcontrols != NULL )
158*7c478bd9Sstevel@tonic-gate 		ldap_controls_free( ld->ld_clientcontrols );
159*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_preferred_language != NULL )
160*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_preferred_language );
161*7c478bd9Sstevel@tonic-gate 	nsldapi_iostatus_free( ld );
162*7c478bd9Sstevel@tonic-gate #ifdef LDAP_SASLIO_HOOKS
163*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_def_sasl_mech != NULL )
164*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_def_sasl_mech );
165*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_def_sasl_realm != NULL )
166*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_def_sasl_realm );
167*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_def_sasl_authcid != NULL )
168*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_def_sasl_authcid );
169*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_def_sasl_authzid != NULL )
170*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_def_sasl_authzid );
171*7c478bd9Sstevel@tonic-gate #endif
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	/*
174*7c478bd9Sstevel@tonic-gate 	 * XXXmcs: should use cache function pointers to hook in memcache
175*7c478bd9Sstevel@tonic-gate 	 */
176*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_memcache != NULL ) {
177*7c478bd9Sstevel@tonic-gate 		ldap_memcache_set( ld, NULL );
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate         /* free all mutexes we have allocated */
181*7c478bd9Sstevel@tonic-gate         nsldapi_mutex_free_all( ld );
182*7c478bd9Sstevel@tonic-gate         NSLDAPI_FREE( ld->ld_mutex );
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate         NSLDAPI_FREE( (char *) ld );
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	return( err );
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate int
nsldapi_send_unbind(LDAP * ld,Sockbuf * sb,LDAPControl ** serverctrls,LDAPControl ** clientctrls)192*7c478bd9Sstevel@tonic-gate nsldapi_send_unbind( LDAP *ld, Sockbuf *sb, LDAPControl **serverctrls,
193*7c478bd9Sstevel@tonic-gate     LDAPControl **clientctrls )
194*7c478bd9Sstevel@tonic-gate {
195*7c478bd9Sstevel@tonic-gate 	BerElement	*ber;
196*7c478bd9Sstevel@tonic-gate 	int		err, msgid;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_send_unbind\n", 0, 0, 0 );
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	/* create a message to send */
201*7c478bd9Sstevel@tonic-gate 	if (( err = nsldapi_alloc_ber_with_options( ld, &ber ))
202*7c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
203*7c478bd9Sstevel@tonic-gate 		return( err );
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	/* fill it in */
207*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
208*7c478bd9Sstevel@tonic-gate 	msgid = ++ld->ld_msgid;
209*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	if ( ber_printf( ber, "{itn", msgid, LDAP_REQ_UNBIND ) == -1 ) {
212*7c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
213*7c478bd9Sstevel@tonic-gate 		err = LDAP_ENCODING_ERROR;
214*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, err, NULL, NULL );
215*7c478bd9Sstevel@tonic-gate 		return( err );
216*7c478bd9Sstevel@tonic-gate 	}
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	if (( err = nsldapi_put_controls( ld, serverctrls, 1, ber ))
219*7c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
220*7c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
221*7c478bd9Sstevel@tonic-gate 		return( err );
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	/* send the message */
225*7c478bd9Sstevel@tonic-gate 	if ( nsldapi_ber_flush( ld, sb, ber, 1, 0 ) != 0 ) {
226*7c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
227*7c478bd9Sstevel@tonic-gate 		err = LDAP_SERVER_DOWN;
228*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, err, NULL, NULL );
229*7c478bd9Sstevel@tonic-gate 		return( err );
230*7c478bd9Sstevel@tonic-gate 	}
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	return( LDAP_SUCCESS );
233*7c478bd9Sstevel@tonic-gate }
234