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