xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/unbind.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	$NetBSD: unbind.c,v 1.1.1.3 2010/12/12 15:21:40 adam Exp $	*/
2 
3 /* OpenLDAP: pkg/ldap/libraries/libldap/unbind.c,v 1.56.2.8 2010/06/10 17:39:48 quanah Exp */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2010 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
18  * All rights reserved.
19  */
20 
21 #include "portable.h"
22 
23 #include <stdio.h>
24 #include <ac/stdlib.h>
25 
26 #include <ac/socket.h>
27 #include <ac/string.h>
28 #include <ac/time.h>
29 
30 #include "ldap-int.h"
31 
32 /* An Unbind Request looks like this:
33  *
34  *	UnbindRequest ::= [APPLICATION 2] NULL
35  *
36  * and has no response.  (Source: RFC 4511)
37  */
38 
39 int
40 ldap_unbind_ext(
41 	LDAP *ld,
42 	LDAPControl **sctrls,
43 	LDAPControl **cctrls )
44 {
45 	int rc;
46 
47 	assert( ld != NULL );
48 	assert( LDAP_VALID( ld ) );
49 
50 	/* check client controls */
51 	rc = ldap_int_client_controls( ld, cctrls );
52 	if( rc != LDAP_SUCCESS ) return rc;
53 
54 	return ldap_ld_free( ld, 1, sctrls, cctrls );
55 }
56 
57 int
58 ldap_unbind_ext_s(
59 	LDAP *ld,
60 	LDAPControl **sctrls,
61 	LDAPControl **cctrls )
62 {
63 	return ldap_unbind_ext( ld, sctrls, cctrls );
64 }
65 
66 int
67 ldap_unbind( LDAP *ld )
68 {
69 	Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
70 
71 	return( ldap_unbind_ext( ld, NULL, NULL ) );
72 }
73 
74 
75 int
76 ldap_ld_free(
77 	LDAP *ld,
78 	int close,
79 	LDAPControl **sctrls,
80 	LDAPControl **cctrls )
81 {
82 	LDAPMessage	*lm, *next;
83 	int		err = LDAP_SUCCESS;
84 
85 	/* free LDAP structure and outstanding requests/responses */
86 #ifdef LDAP_R_COMPILE
87 	ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
88 #endif
89 	while ( ld->ld_requests != NULL ) {
90 		ldap_free_request( ld, ld->ld_requests );
91 	}
92 
93 	/* free and unbind from all open connections */
94 	while ( ld->ld_conns != NULL ) {
95 		ldap_free_connection( ld, ld->ld_conns, 1, close );
96 	}
97 #ifdef LDAP_R_COMPILE
98 	ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
99 #endif
100 
101 #ifdef LDAP_R_COMPILE
102 	ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
103 #endif
104 	for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
105 		next = lm->lm_next;
106 		ldap_msgfree( lm );
107 	}
108 
109 	if ( ld->ld_abandoned != NULL ) {
110 		LDAP_FREE( ld->ld_abandoned );
111 		ld->ld_abandoned = NULL;
112 	}
113 #ifdef LDAP_R_COMPILE
114 	ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
115 #endif
116 
117 	/* final close callbacks */
118 	{
119 		ldaplist *ll, *next;
120 
121 		for ( ll = ld->ld_options.ldo_conn_cbs; ll; ll = next ) {
122 			ldap_conncb *cb = ll->ll_data;
123 			next = ll->ll_next;
124 			cb->lc_del( ld, NULL, cb );
125 			LDAP_FREE( ll );
126 		}
127 	}
128 
129 	if ( ld->ld_error != NULL ) {
130 		LDAP_FREE( ld->ld_error );
131 		ld->ld_error = NULL;
132 	}
133 
134 	if ( ld->ld_matched != NULL ) {
135 		LDAP_FREE( ld->ld_matched );
136 		ld->ld_matched = NULL;
137 	}
138 
139 	if( ld->ld_referrals != NULL) {
140 		LDAP_VFREE(ld->ld_referrals);
141 		ld->ld_referrals = NULL;
142 	}
143 
144 	if ( ld->ld_selectinfo != NULL ) {
145 		ldap_free_select_info( ld->ld_selectinfo );
146 		ld->ld_selectinfo = NULL;
147 	}
148 
149 	if ( ld->ld_options.ldo_defludp != NULL ) {
150 		ldap_free_urllist( ld->ld_options.ldo_defludp );
151 		ld->ld_options.ldo_defludp = NULL;
152 	}
153 
154 #ifdef LDAP_CONNECTIONLESS
155 	if ( ld->ld_options.ldo_peer != NULL ) {
156 		LDAP_FREE( ld->ld_options.ldo_peer );
157 		ld->ld_options.ldo_peer = NULL;
158 	}
159 
160 	if ( ld->ld_options.ldo_cldapdn != NULL ) {
161 		LDAP_FREE( ld->ld_options.ldo_cldapdn );
162 		ld->ld_options.ldo_cldapdn = NULL;
163 	}
164 #endif
165 
166 #ifdef HAVE_CYRUS_SASL
167 	if ( ld->ld_options.ldo_def_sasl_mech != NULL ) {
168 		LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
169 		ld->ld_options.ldo_def_sasl_mech = NULL;
170 	}
171 
172 	if ( ld->ld_options.ldo_def_sasl_realm != NULL ) {
173 		LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
174 		ld->ld_options.ldo_def_sasl_realm = NULL;
175 	}
176 
177 	if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) {
178 		LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
179 		ld->ld_options.ldo_def_sasl_authcid = NULL;
180 	}
181 
182 	if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) {
183 		LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
184 		ld->ld_options.ldo_def_sasl_authzid = NULL;
185 	}
186 #endif
187 
188 #ifdef HAVE_TLS
189 	ldap_int_tls_destroy( &ld->ld_options );
190 #endif
191 
192 	if ( ld->ld_options.ldo_sctrls != NULL ) {
193 		ldap_controls_free( ld->ld_options.ldo_sctrls );
194 		ld->ld_options.ldo_sctrls = NULL;
195 	}
196 
197 	if ( ld->ld_options.ldo_cctrls != NULL ) {
198 		ldap_controls_free( ld->ld_options.ldo_cctrls );
199 		ld->ld_options.ldo_cctrls = NULL;
200 	}
201 
202 	ber_sockbuf_free( ld->ld_sb );
203 
204 #ifdef LDAP_R_COMPILE
205 	ldap_pvt_thread_mutex_destroy( &ld->ld_req_mutex );
206 	ldap_pvt_thread_mutex_destroy( &ld->ld_res_mutex );
207 	ldap_pvt_thread_mutex_destroy( &ld->ld_conn_mutex );
208 #endif
209 #ifndef NDEBUG
210 	LDAP_TRASH(ld);
211 #endif
212 	LDAP_FREE( (char *) ld );
213 
214 	return( err );
215 }
216 
217 int
218 ldap_unbind_s( LDAP *ld )
219 {
220 	return( ldap_unbind_ext( ld, NULL, NULL ) );
221 }
222 
223 /* FIXME: this function is called only by ldap_free_connection(),
224  * which, most of the times, is called with ld_req_mutex locked */
225 int
226 ldap_send_unbind(
227 	LDAP *ld,
228 	Sockbuf *sb,
229 	LDAPControl **sctrls,
230 	LDAPControl **cctrls )
231 {
232 	BerElement	*ber;
233 	ber_int_t	id;
234 
235 	Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
236 
237 #ifdef LDAP_CONNECTIONLESS
238 	if (LDAP_IS_UDP(ld))
239 		return LDAP_SUCCESS;
240 #endif
241 	/* create a message to send */
242 	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
243 		return( ld->ld_errno );
244 	}
245 
246 	id = ++(ld)->ld_msgid;
247 
248 	/* fill it in */
249 	if ( ber_printf( ber, "{itn" /*}*/, id,
250 	    LDAP_REQ_UNBIND ) == -1 ) {
251 		ld->ld_errno = LDAP_ENCODING_ERROR;
252 		ber_free( ber, 1 );
253 		return( ld->ld_errno );
254 	}
255 
256 	/* Put Server Controls */
257 	if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
258 		ber_free( ber, 1 );
259 		return ld->ld_errno;
260 	}
261 
262 	if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
263 		ld->ld_errno = LDAP_ENCODING_ERROR;
264 		ber_free( ber, 1 );
265 		return( ld->ld_errno );
266 	}
267 
268 	ld->ld_errno = LDAP_SUCCESS;
269 	/* send the message */
270 	if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) == -1 ) {
271 		ld->ld_errno = LDAP_SERVER_DOWN;
272 	}
273 
274 	return( ld->ld_errno );
275 }
276