ptconn.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * ptconn.c - LDAP connection-related code for Pass Through Authentication
  43. *
  44. */
  45. #include "passthru.h"
  46. /*
  47. * function prototypes
  48. */
  49. static int dn_is_underneath_suffix( PassThruSuffix *suffix, char *normdn,
  50. int dnlen );
  51. static void close_and_dispose_connection( PassThruConnection *conn );
  52. static void check_for_stale_connections( PassThruServer *srvr );
  53. /*
  54. * Most of the complicated connection-related code lives in this file. Some
  55. * general notes about how we manage our connections to "remote" LDAP servers:
  56. *
  57. * 1) Each server we have a relationship with is managed independently.
  58. *
  59. * 2) We may simultaneously issue multiple bind requests on a single LDAP
  60. * connection. Each server has a "maxconcurrency" configuration
  61. * parameter associated with it that caps the number of outstanding
  62. * binds per connection. For each connection we maintain a "usecount"
  63. * which is used to track the number of threads using the connection.
  64. *
  65. * 3) We may open more than one connection to a server. This is only done
  66. * when "maxconcurrency" is exceeded for all the connections we already
  67. * have open. Each server has a "maxconnections" configuration
  68. * parameter associated with it that caps the number of connections.
  69. * We also maintain a "connlist_count" for each server so we know when
  70. * we have reached the maximum number of open connections allowed.
  71. *
  72. * 4) If no connection is available to service a request (and we have
  73. * reached the limit of how many we are supposed to open), threads
  74. * go to sleep on a condition variable and one is woken up each time
  75. * a connection's "usecount" is decremented.
  76. *
  77. * 5) If we see an LDAP_CONNECT_ERROR or LDAP_SERVER_DOWN error on a
  78. * session handle, we mark its status as PASSTHRU_CONNSTATUS_DOWN and
  79. * close it as soon as all threads using it release it. Connections
  80. * marked as "down" are not counted against the "maxconnections" limit.
  81. *
  82. * 6) We close and reopen connections that have been open for more than
  83. * the server's configured connection lifetime. This is done to ensure
  84. * that we reconnect to a primary server after failover occurs. If no
  85. * lifetime is configured or it is set to 0, we never close and reopen
  86. * connections.
  87. */
  88. /*
  89. * Given a normalized target dn, see if it we should "pass through"
  90. * authentication to another LDAP server. The answer is "yes" if the
  91. * target dn resides under one of the suffixes we have that is associated
  92. * with an LDAP server we know about.
  93. *
  94. * This function assumes that normdn is normalized and the the suffixes in the
  95. * cfg structure have also been normalized.
  96. *
  97. * Returns an LDAP error code, typically:
  98. * LDAP_SUCCESS should pass though; *srvrp set.
  99. * LDAP_NO_SUCH_OBJECT let this server handle the bind.
  100. */
  101. int
  102. passthru_dn2server( PassThruConfig *cfg, char *normdn, PassThruServer **srvrp )
  103. {
  104. PassThruServer *ptsrvr;
  105. PassThruSuffix *ptsuffix;
  106. int dnlen;
  107. PASSTHRU_ASSERT( cfg != NULL );
  108. PASSTHRU_ASSERT( normdn != NULL );
  109. PASSTHRU_ASSERT( srvrp != NULL );
  110. dnlen = strlen( normdn );
  111. for ( ptsrvr = cfg->ptconfig_serverlist; ptsrvr != NULL;
  112. ptsrvr = ptsrvr->ptsrvr_next ) {
  113. for ( ptsuffix = ptsrvr->ptsrvr_suffixes; ptsuffix != NULL;
  114. ptsuffix = ptsuffix->ptsuffix_next ) {
  115. if ( dn_is_underneath_suffix( ptsuffix, normdn, dnlen )) {
  116. *srvrp = ptsrvr;
  117. return( LDAP_SUCCESS ); /* got it */
  118. }
  119. }
  120. }
  121. *srvrp = NULL;
  122. return( LDAP_NO_SUCH_OBJECT ); /* no match */
  123. }
  124. /*
  125. * Get an LDAP session handle for communicating with srvr.
  126. *
  127. * Returns an LDAP eror code, typically:
  128. * LDAP_SUCCESS
  129. * other
  130. */
  131. int
  132. passthru_get_connection( PassThruServer *srvr, LDAP **ldp )
  133. {
  134. int rc;
  135. PassThruConnection *conn, *connprev;
  136. LDAP *ld;
  137. PASSTHRU_ASSERT( srvr != NULL );
  138. PASSTHRU_ASSERT( ldp != NULL );
  139. check_for_stale_connections( srvr );
  140. slapi_lock_mutex( srvr->ptsrvr_connlist_mutex );
  141. rc = LDAP_SUCCESS; /* optimistic */
  142. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  143. "=> passthru_get_connection server %s:%d conns: %d maxconns: %d\n",
  144. srvr->ptsrvr_hostname, srvr->ptsrvr_port, srvr->ptsrvr_connlist_count,
  145. srvr->ptsrvr_maxconnections );
  146. for ( ;; ) {
  147. /*
  148. * look for an available, already open connection
  149. */
  150. connprev = NULL;
  151. for ( conn = srvr->ptsrvr_connlist; conn != NULL;
  152. conn = conn->ptconn_next ) {
  153. if ( conn->ptconn_status == PASSTHRU_CONNSTATUS_OK
  154. && conn->ptconn_usecount < srvr->ptsrvr_maxconcurrency ) {
  155. #ifdef PASSTHRU_VERBOSE_LOGGING
  156. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  157. "<= passthru_get_connection server found "
  158. "conn 0x%x to use)\n", conn->ptconn_ld );
  159. #endif
  160. goto unlock_and_return; /* found one */
  161. }
  162. connprev = conn;
  163. }
  164. if ( srvr->ptsrvr_connlist_count < srvr->ptsrvr_maxconnections ) {
  165. /*
  166. * we have not exceeded the maximum number of connections allowed,
  167. * so we initialize a new one and add it to the end of our list.
  168. */
  169. if (( ld = slapi_ldap_init( srvr->ptsrvr_hostname,
  170. srvr->ptsrvr_port, srvr->ptsrvr_secure, 1 )) == NULL ) {
  171. #ifdef PASSTHRU_VERBOSE_LOGGING
  172. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  173. "<= passthru_get_connection slapi_ldap_init failed\n" );
  174. #endif
  175. rc = LDAP_LOCAL_ERROR;
  176. goto unlock_and_return;
  177. }
  178. /*
  179. * set protocol version to correct value for this server
  180. */
  181. if ( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
  182. &srvr->ptsrvr_ldapversion ) != 0 ) {
  183. slapi_ldap_unbind( ld );
  184. }
  185. conn = (PassThruConnection *)slapi_ch_malloc(
  186. sizeof( PassThruConnection ));
  187. conn->ptconn_ld = ld;
  188. conn->ptconn_status = PASSTHRU_CONNSTATUS_OK;
  189. time( &conn->ptconn_opentime );
  190. conn->ptconn_ldapversion = srvr->ptsrvr_ldapversion;
  191. conn->ptconn_usecount = 0;
  192. conn->ptconn_next = NULL;
  193. conn->ptconn_prev = connprev;
  194. if ( connprev == NULL ) {
  195. srvr->ptsrvr_connlist = conn;
  196. conn->ptconn_prev = NULL;
  197. } else {
  198. connprev->ptconn_next = conn;
  199. conn->ptconn_prev = connprev;
  200. }
  201. ++srvr->ptsrvr_connlist_count;
  202. #ifdef PASSTHRU_VERBOSE_LOGGING
  203. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  204. "<= passthru_get_connection added new conn 0x%x, "
  205. "conn count now %d\n", ld, srvr->ptsrvr_connlist_count );
  206. #endif
  207. goto unlock_and_return; /* got a new one */
  208. }
  209. #ifdef PASSTHRU_VERBOSE_LOGGING
  210. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  211. "... passthru_get_connection waiting for conn to free up\n" );
  212. #endif
  213. slapi_wait_condvar( srvr->ptsrvr_connlist_cv, NULL );
  214. #ifdef PASSTHRU_VERBOSE_LOGGING
  215. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  216. "... passthru_get_connection awake again\n" );
  217. #endif
  218. }
  219. unlock_and_return:
  220. if ( conn != NULL ) {
  221. ++conn->ptconn_usecount;
  222. *ldp = conn->ptconn_ld;
  223. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  224. "<= passthru_get_connection ld=0x%p (concurrency now %d)\n",
  225. *ldp, conn->ptconn_usecount );
  226. } else {
  227. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  228. "<= passthru_get_connection error %d\n", rc );
  229. }
  230. slapi_unlock_mutex( srvr->ptsrvr_connlist_mutex );
  231. return( rc );
  232. }
  233. /*
  234. * Mark the connection ld is associated with as free to be used again.
  235. * If dispose is non-zero, we mark the connection as "bad" and dispose
  236. * of it and its ld once the use count becomes zero.
  237. */
  238. void
  239. passthru_release_connection( PassThruServer *srvr, LDAP *ld, int dispose )
  240. {
  241. PassThruConnection *conn, *connprev;
  242. PASSTHRU_ASSERT( srvr != NULL );
  243. PASSTHRU_ASSERT( ld != NULL );
  244. #ifdef PASSTHRU_VERBOSE_LOGGING
  245. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  246. "=> passthru_release_connection ld=0x%x%s\n", ld,
  247. dispose ? " (disposing)" : "" );
  248. #endif
  249. slapi_lock_mutex( srvr->ptsrvr_connlist_mutex );
  250. /*
  251. * find the connection structure this ld is part of
  252. */
  253. connprev = NULL;
  254. for ( conn = srvr->ptsrvr_connlist; conn != NULL;
  255. conn = conn->ptconn_next ) {
  256. if ( ld == conn->ptconn_ld ) {
  257. break;
  258. }
  259. connprev = conn;
  260. }
  261. if ( conn == NULL ) { /* ld not found -- unexpected */
  262. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  263. "=> passthru_release_connection ld=0x%p not found\n", ld );
  264. } else {
  265. PASSTHRU_ASSERT( conn->ptconn_usecount > 0 );
  266. --conn->ptconn_usecount;
  267. if ( dispose ) {
  268. conn->ptconn_status = PASSTHRU_CONNSTATUS_DOWN;
  269. }
  270. if ( conn->ptconn_status != PASSTHRU_CONNSTATUS_OK
  271. && conn->ptconn_usecount == 0 ) {
  272. /*
  273. * remove from server's connection list
  274. */
  275. if ( connprev == NULL ) {
  276. srvr->ptsrvr_connlist = conn->ptconn_next;
  277. } else {
  278. connprev->ptconn_next = conn->ptconn_next;
  279. }
  280. --srvr->ptsrvr_connlist_count;
  281. /*
  282. * close connection and free memory
  283. */
  284. close_and_dispose_connection( conn );
  285. }
  286. }
  287. /*
  288. * wake up a thread that is waiting for a connection (there may not be
  289. * any but the slapi_notify_condvar() call should be cheap in any event).
  290. */
  291. slapi_notify_condvar( srvr->ptsrvr_connlist_cv, 0 );
  292. /*
  293. * unlock and return
  294. */
  295. slapi_unlock_mutex( srvr->ptsrvr_connlist_mutex );
  296. }
  297. /*
  298. * close all open connections in preparation for server shutdown, etc.
  299. */
  300. void
  301. passthru_close_all_connections( PassThruConfig *cfg )
  302. {
  303. PassThruServer *srvr;
  304. PassThruConnection *conn, *nextconn;
  305. PASSTHRU_ASSERT( cfg != NULL );
  306. for ( srvr = cfg->ptconfig_serverlist; srvr != NULL;
  307. srvr = srvr->ptsrvr_next ) {
  308. for ( conn = srvr->ptsrvr_connlist; conn != NULL; conn = nextconn ) {
  309. nextconn = conn->ptconn_next;
  310. close_and_dispose_connection( conn );
  311. }
  312. }
  313. }
  314. /*
  315. * return non-zero value if normdn falls underneath a suffix
  316. */
  317. static int
  318. dn_is_underneath_suffix( PassThruSuffix *suffix, char *normdn, int dnlen )
  319. {
  320. PASSTHRU_ASSERT( suffix != NULL );
  321. PASSTHRU_ASSERT( normdn != NULL );
  322. PASSTHRU_ASSERT( dnlen >= 0 );
  323. return ( suffix->ptsuffix_len <= dnlen &&
  324. slapi_UTF8CASECMP( suffix->ptsuffix_normsuffix,
  325. normdn + ( dnlen - suffix->ptsuffix_len )) == 0 );
  326. }
  327. /*
  328. * Unbind from server and dispose of a connection.
  329. */
  330. static void
  331. close_and_dispose_connection( PassThruConnection *conn )
  332. {
  333. PASSTHRU_ASSERT( conn != NULL );
  334. PASSTHRU_ASSERT( conn->ptconn_ld != NULL );
  335. slapi_ldap_unbind( conn->ptconn_ld );
  336. conn->ptconn_ld = NULL;
  337. slapi_ch_free( (void **)&conn );
  338. }
  339. /*
  340. * Close (or mark to be closed) any connections for this srvr that have
  341. * exceeded the maximum connection lifetime.
  342. */
  343. static void
  344. check_for_stale_connections( PassThruServer *srvr )
  345. {
  346. PassThruConnection *conn, *prevconn, *nextconn;
  347. time_t curtime;
  348. PASSTHRU_ASSERT( srvr != NULL );
  349. #ifdef PASSTHRU_VERBOSE_LOGGING
  350. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  351. "check_for_stale_connections: server %s (lifetime %d secs)\n",
  352. srvr->ptsrvr_url, srvr->ptsrvr_connlifetime );
  353. #endif
  354. if ( srvr->ptsrvr_connlifetime <= 0 ) {
  355. return;
  356. }
  357. time( &curtime );
  358. slapi_lock_mutex( srvr->ptsrvr_connlist_mutex );
  359. prevconn = NULL;
  360. for ( conn = srvr->ptsrvr_connlist; conn != NULL; conn = nextconn ) {
  361. nextconn = conn->ptconn_next;
  362. if ( curtime - conn->ptconn_opentime > srvr->ptsrvr_connlifetime ) {
  363. if ( conn->ptconn_usecount == 0 ) {
  364. /*
  365. * connection is idle and stale -- remove from server's list
  366. */
  367. #ifdef PASSTHRU_VERBOSE_LOGGING
  368. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  369. "check_for_stale_connections: discarding idle, "
  370. "stale connection 0x%x\n", conn->ptconn_ld );
  371. #endif
  372. if ( prevconn == NULL ) {
  373. srvr->ptsrvr_connlist = nextconn;
  374. } else {
  375. prevconn->ptconn_next = nextconn;
  376. }
  377. --srvr->ptsrvr_connlist_count;
  378. close_and_dispose_connection( conn );
  379. } else {
  380. /*
  381. * connection is stale but in use -- mark to be disposed later
  382. */
  383. #ifdef PASSTHRU_VERBOSE_LOGGING
  384. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  385. "check_for_stale_connections: marking connection 0x%x "
  386. "stale (use count %d)\n", conn->ptconn_ld,
  387. conn->ptconn_usecount );
  388. #endif
  389. conn->ptconn_status = PASSTHRU_CONNSTATUS_STALE;
  390. prevconn = conn;
  391. }
  392. } else {
  393. prevconn = conn;
  394. }
  395. }
  396. slapi_unlock_mutex( srvr->ptsrvr_connlist_mutex );
  397. }