1 /* $NetBSD: unbind.c,v 1.1.1.7 2019/08/08 13:31:15 christos Exp $ */ 2 3 /* $OpenLDAP$ */ 4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 5 * 6 * Copyright 1998-2019 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.7 2019/08/08 13:31:15 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 /* Should already be closed by ldap_free_connection which knows not to free 140 * this one */ 141 ber_int_sb_destroy( ld->ld_sb ); 142 143 LDAP_MUTEX_LOCK( &ld->ld_ldopts_mutex ); 144 145 /* final close callbacks */ 146 { 147 ldaplist *ll, *next; 148 149 for ( ll = ld->ld_options.ldo_conn_cbs; ll; ll = next ) { 150 ldap_conncb *cb = ll->ll_data; 151 next = ll->ll_next; 152 cb->lc_del( ld, NULL, cb ); 153 LDAP_FREE( ll ); 154 } 155 } 156 157 if ( ld->ld_error != NULL ) { 158 LDAP_FREE( ld->ld_error ); 159 ld->ld_error = NULL; 160 } 161 162 if ( ld->ld_matched != NULL ) { 163 LDAP_FREE( ld->ld_matched ); 164 ld->ld_matched = NULL; 165 } 166 167 if ( ld->ld_referrals != NULL) { 168 LDAP_VFREE(ld->ld_referrals); 169 ld->ld_referrals = NULL; 170 } 171 172 if ( ld->ld_selectinfo != NULL ) { 173 ldap_free_select_info( ld->ld_selectinfo ); 174 ld->ld_selectinfo = NULL; 175 } 176 177 if ( ld->ld_options.ldo_defludp != NULL ) { 178 ldap_free_urllist( ld->ld_options.ldo_defludp ); 179 ld->ld_options.ldo_defludp = NULL; 180 } 181 182 #ifdef LDAP_CONNECTIONLESS 183 if ( ld->ld_options.ldo_peer != NULL ) { 184 LDAP_FREE( ld->ld_options.ldo_peer ); 185 ld->ld_options.ldo_peer = NULL; 186 } 187 188 if ( ld->ld_options.ldo_cldapdn != NULL ) { 189 LDAP_FREE( ld->ld_options.ldo_cldapdn ); 190 ld->ld_options.ldo_cldapdn = NULL; 191 } 192 #endif 193 194 #ifdef HAVE_CYRUS_SASL 195 if ( ld->ld_options.ldo_def_sasl_mech != NULL ) { 196 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech ); 197 ld->ld_options.ldo_def_sasl_mech = NULL; 198 } 199 200 if ( ld->ld_options.ldo_def_sasl_realm != NULL ) { 201 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm ); 202 ld->ld_options.ldo_def_sasl_realm = NULL; 203 } 204 205 if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) { 206 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid ); 207 ld->ld_options.ldo_def_sasl_authcid = NULL; 208 } 209 210 if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) { 211 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid ); 212 ld->ld_options.ldo_def_sasl_authzid = NULL; 213 } 214 #endif 215 216 #ifdef HAVE_TLS 217 ldap_int_tls_destroy( &ld->ld_options ); 218 #endif 219 220 if ( ld->ld_options.ldo_sctrls != NULL ) { 221 ldap_controls_free( ld->ld_options.ldo_sctrls ); 222 ld->ld_options.ldo_sctrls = NULL; 223 } 224 225 if ( ld->ld_options.ldo_cctrls != NULL ) { 226 ldap_controls_free( ld->ld_options.ldo_cctrls ); 227 ld->ld_options.ldo_cctrls = NULL; 228 } 229 LDAP_MUTEX_UNLOCK( &ld->ld_ldopts_mutex ); 230 231 #ifdef LDAP_R_COMPILE 232 ldap_pvt_thread_mutex_destroy( &ld->ld_msgid_mutex ); 233 ldap_pvt_thread_mutex_destroy( &ld->ld_conn_mutex ); 234 ldap_pvt_thread_mutex_destroy( &ld->ld_req_mutex ); 235 ldap_pvt_thread_mutex_destroy( &ld->ld_res_mutex ); 236 ldap_pvt_thread_mutex_destroy( &ld->ld_abandon_mutex ); 237 ldap_pvt_thread_mutex_destroy( &ld->ld_ldopts_mutex ); 238 ldap_pvt_thread_mutex_destroy( &ld->ld_ldcmutex ); 239 #endif 240 #ifndef NDEBUG 241 LDAP_TRASH(ld); 242 #endif 243 LDAP_FREE( (char *) ld->ldc ); 244 LDAP_FREE( (char *) ld ); 245 246 return( err ); 247 } 248 249 int 250 ldap_destroy( LDAP *ld ) 251 { 252 return ( ldap_ld_free( ld, 1, NULL, NULL ) ); 253 } 254 255 int 256 ldap_unbind_s( LDAP *ld ) 257 { 258 return( ldap_unbind_ext( ld, NULL, NULL ) ); 259 } 260 261 /* FIXME: this function is called only by ldap_free_connection(), 262 * which, most of the times, is called with ld_req_mutex locked */ 263 int 264 ldap_send_unbind( 265 LDAP *ld, 266 Sockbuf *sb, 267 LDAPControl **sctrls, 268 LDAPControl **cctrls ) 269 { 270 BerElement *ber; 271 ber_int_t id; 272 273 Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 ); 274 275 #ifdef LDAP_CONNECTIONLESS 276 if (LDAP_IS_UDP(ld)) 277 return LDAP_SUCCESS; 278 #endif 279 /* create a message to send */ 280 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) { 281 return( ld->ld_errno ); 282 } 283 284 LDAP_NEXT_MSGID(ld, id); 285 286 /* fill it in */ 287 if ( ber_printf( ber, "{itn" /*}*/, id, 288 LDAP_REQ_UNBIND ) == -1 ) { 289 ld->ld_errno = LDAP_ENCODING_ERROR; 290 ber_free( ber, 1 ); 291 return( ld->ld_errno ); 292 } 293 294 /* Put Server Controls */ 295 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) { 296 ber_free( ber, 1 ); 297 return ld->ld_errno; 298 } 299 300 if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) { 301 ld->ld_errno = LDAP_ENCODING_ERROR; 302 ber_free( ber, 1 ); 303 return( ld->ld_errno ); 304 } 305 306 ld->ld_errno = LDAP_SUCCESS; 307 /* send the message */ 308 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) == -1 ) { 309 ld->ld_errno = LDAP_SERVER_DOWN; 310 } 311 312 return( ld->ld_errno ); 313 } 314