unbind.c 7.3 KB

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