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