xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/unbind.c (revision a0698ed9d41653d7a2378819ad501a285ca0d401)
1 /*	$NetBSD: unbind.c,v 1.1.1.6 2018/02/06 01:53:08 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2017 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 <sys/cdefs.h>
22 __RCSID("$NetBSD: unbind.c,v 1.1.1.6 2018/02/06 01:53:08 christos Exp $");
23 
24 #include "portable.h"
25 
26 #include <stdio.h>
27 #include <ac/stdlib.h>
28 
29 #include <ac/socket.h>
30 #include <ac/string.h>
31 #include <ac/time.h>
32 
33 #include "ldap-int.h"
34 
35 /* An Unbind Request looks like this:
36  *
37  *	UnbindRequest ::= [APPLICATION 2] NULL
38  *
39  * and has no response.  (Source: RFC 4511)
40  */
41 
42 int
43 ldap_unbind_ext(
44 	LDAP *ld,
45 	LDAPControl **sctrls,
46 	LDAPControl **cctrls )
47 {
48 	int rc;
49 
50 	assert( ld != NULL );
51 	assert( LDAP_VALID( ld ) );
52 
53 	/* check client controls */
54 	rc = ldap_int_client_controls( ld, cctrls );
55 	if( rc != LDAP_SUCCESS ) return rc;
56 
57 	return ldap_ld_free( ld, 1, sctrls, cctrls );
58 }
59 
60 int
61 ldap_unbind_ext_s(
62 	LDAP *ld,
63 	LDAPControl **sctrls,
64 	LDAPControl **cctrls )
65 {
66 	return ldap_unbind_ext( ld, sctrls, cctrls );
67 }
68 
69 int
70 ldap_unbind( LDAP *ld )
71 {
72 	Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
73 
74 	return( ldap_unbind_ext( ld, NULL, NULL ) );
75 }
76 
77 
78 int
79 ldap_ld_free(
80 	LDAP *ld,
81 	int close,
82 	LDAPControl **sctrls,
83 	LDAPControl **cctrls )
84 {
85 	LDAPMessage	*lm, *next;
86 	int		err = LDAP_SUCCESS;
87 
88 	LDAP_MUTEX_LOCK( &ld->ld_ldcmutex );
89 	/* Someone else is still using this ld. */
90 	if (ld->ld_ldcrefcnt > 1) {	/* but not last thread */
91 		/* clean up self only */
92 		ld->ld_ldcrefcnt--;
93 		if ( ld->ld_error != NULL ) {
94 			LDAP_FREE( ld->ld_error );
95 			ld->ld_error = NULL;
96 		}
97 
98 		if ( ld->ld_matched != NULL ) {
99 			LDAP_FREE( ld->ld_matched );
100 			ld->ld_matched = NULL;
101 		}
102 		if ( ld->ld_referrals != NULL) {
103 			LDAP_VFREE(ld->ld_referrals);
104 			ld->ld_referrals = NULL;
105 		}
106 		LDAP_MUTEX_UNLOCK( &ld->ld_ldcmutex );
107 		LDAP_FREE( (char *) ld );
108 		return( err );
109 	}
110 
111 	/* This ld is the last thread. */
112 	LDAP_MUTEX_UNLOCK( &ld->ld_ldcmutex );
113 
114 	/* free LDAP structure and outstanding requests/responses */
115 	LDAP_MUTEX_LOCK( &ld->ld_req_mutex );
116 	while ( ld->ld_requests != NULL ) {
117 		ldap_free_request( ld, ld->ld_requests );
118 	}
119 	LDAP_MUTEX_UNLOCK( &ld->ld_req_mutex );
120 	LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
121 
122 	/* free and unbind from all open connections */
123 	while ( ld->ld_conns != NULL ) {
124 		ldap_free_connection( ld, ld->ld_conns, 1, close );
125 	}
126 	LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
127 	LDAP_MUTEX_LOCK( &ld->ld_res_mutex );
128 	for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
129 		next = lm->lm_next;
130 		ldap_msgfree( lm );
131 	}
132 
133 	if ( ld->ld_abandoned != NULL ) {
134 		LDAP_FREE( ld->ld_abandoned );
135 		ld->ld_abandoned = NULL;
136 	}
137 	LDAP_MUTEX_UNLOCK( &ld->ld_res_mutex );
138 
139 	ber_sockbuf_free( ld->ld_sb );
140 
141 	LDAP_MUTEX_LOCK( &ld->ld_ldopts_mutex );
142 
143 	/* final close callbacks */
144 	{
145 		ldaplist *ll, *next;
146 
147 		for ( ll = ld->ld_options.ldo_conn_cbs; ll; ll = next ) {
148 			ldap_conncb *cb = ll->ll_data;
149 			next = ll->ll_next;
150 			cb->lc_del( ld, NULL, cb );
151 			LDAP_FREE( ll );
152 		}
153 	}
154 
155 	if ( ld->ld_error != NULL ) {
156 		LDAP_FREE( ld->ld_error );
157 		ld->ld_error = NULL;
158 	}
159 
160 	if ( ld->ld_matched != NULL ) {
161 		LDAP_FREE( ld->ld_matched );
162 		ld->ld_matched = NULL;
163 	}
164 
165 	if ( ld->ld_referrals != NULL) {
166 		LDAP_VFREE(ld->ld_referrals);
167 		ld->ld_referrals = NULL;
168 	}
169 
170 	if ( ld->ld_selectinfo != NULL ) {
171 		ldap_free_select_info( ld->ld_selectinfo );
172 		ld->ld_selectinfo = NULL;
173 	}
174 
175 	if ( ld->ld_options.ldo_defludp != NULL ) {
176 		ldap_free_urllist( ld->ld_options.ldo_defludp );
177 		ld->ld_options.ldo_defludp = NULL;
178 	}
179 
180 #ifdef LDAP_CONNECTIONLESS
181 	if ( ld->ld_options.ldo_peer != NULL ) {
182 		LDAP_FREE( ld->ld_options.ldo_peer );
183 		ld->ld_options.ldo_peer = NULL;
184 	}
185 
186 	if ( ld->ld_options.ldo_cldapdn != NULL ) {
187 		LDAP_FREE( ld->ld_options.ldo_cldapdn );
188 		ld->ld_options.ldo_cldapdn = NULL;
189 	}
190 #endif
191 
192 #ifdef HAVE_CYRUS_SASL
193 	if ( ld->ld_options.ldo_def_sasl_mech != NULL ) {
194 		LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
195 		ld->ld_options.ldo_def_sasl_mech = NULL;
196 	}
197 
198 	if ( ld->ld_options.ldo_def_sasl_realm != NULL ) {
199 		LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
200 		ld->ld_options.ldo_def_sasl_realm = NULL;
201 	}
202 
203 	if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) {
204 		LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
205 		ld->ld_options.ldo_def_sasl_authcid = NULL;
206 	}
207 
208 	if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) {
209 		LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
210 		ld->ld_options.ldo_def_sasl_authzid = NULL;
211 	}
212 #endif
213 
214 #ifdef HAVE_TLS
215 	ldap_int_tls_destroy( &ld->ld_options );
216 #endif
217 
218 	if ( ld->ld_options.ldo_sctrls != NULL ) {
219 		ldap_controls_free( ld->ld_options.ldo_sctrls );
220 		ld->ld_options.ldo_sctrls = NULL;
221 	}
222 
223 	if ( ld->ld_options.ldo_cctrls != NULL ) {
224 		ldap_controls_free( ld->ld_options.ldo_cctrls );
225 		ld->ld_options.ldo_cctrls = NULL;
226 	}
227 	LDAP_MUTEX_UNLOCK( &ld->ld_ldopts_mutex );
228 
229 #ifdef LDAP_R_COMPILE
230 	ldap_pvt_thread_mutex_destroy( &ld->ld_msgid_mutex );
231 	ldap_pvt_thread_mutex_destroy( &ld->ld_conn_mutex );
232 	ldap_pvt_thread_mutex_destroy( &ld->ld_req_mutex );
233 	ldap_pvt_thread_mutex_destroy( &ld->ld_res_mutex );
234 	ldap_pvt_thread_mutex_destroy( &ld->ld_abandon_mutex );
235 	ldap_pvt_thread_mutex_destroy( &ld->ld_ldopts_mutex );
236 	ldap_pvt_thread_mutex_destroy( &ld->ld_ldcmutex );
237 #endif
238 #ifndef NDEBUG
239 	LDAP_TRASH(ld);
240 #endif
241 	LDAP_FREE( (char *) ld->ldc );
242 	LDAP_FREE( (char *) ld );
243 
244 	return( err );
245 }
246 
247 int
248 ldap_destroy( LDAP *ld )
249 {
250 	return ( ldap_ld_free( ld, 1, NULL, NULL ) );
251 }
252 
253 int
254 ldap_unbind_s( LDAP *ld )
255 {
256 	return( ldap_unbind_ext( ld, NULL, NULL ) );
257 }
258 
259 /* FIXME: this function is called only by ldap_free_connection(),
260  * which, most of the times, is called with ld_req_mutex locked */
261 int
262 ldap_send_unbind(
263 	LDAP *ld,
264 	Sockbuf *sb,
265 	LDAPControl **sctrls,
266 	LDAPControl **cctrls )
267 {
268 	BerElement	*ber;
269 	ber_int_t	id;
270 
271 	Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
272 
273 #ifdef LDAP_CONNECTIONLESS
274 	if (LDAP_IS_UDP(ld))
275 		return LDAP_SUCCESS;
276 #endif
277 	/* create a message to send */
278 	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
279 		return( ld->ld_errno );
280 	}
281 
282 	LDAP_NEXT_MSGID(ld, id);
283 
284 	/* fill it in */
285 	if ( ber_printf( ber, "{itn" /*}*/, id,
286 	    LDAP_REQ_UNBIND ) == -1 ) {
287 		ld->ld_errno = LDAP_ENCODING_ERROR;
288 		ber_free( ber, 1 );
289 		return( ld->ld_errno );
290 	}
291 
292 	/* Put Server Controls */
293 	if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
294 		ber_free( ber, 1 );
295 		return ld->ld_errno;
296 	}
297 
298 	if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
299 		ld->ld_errno = LDAP_ENCODING_ERROR;
300 		ber_free( ber, 1 );
301 		return( ld->ld_errno );
302 	}
303 
304 	ld->ld_errno = LDAP_SUCCESS;
305 	/* send the message */
306 	if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) == -1 ) {
307 		ld->ld_errno = LDAP_SERVER_DOWN;
308 	}
309 
310 	return( ld->ld_errno );
311 }
312