cb_conn_stateless.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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. #include "cb.h"
  42. /*
  43. * Most of the complicated connection-related code lives in this file. Some
  44. * general notes about how we manage our connections to "remote" LDAP servers:
  45. *
  46. * 1) Each farm server we have a relationship with is managed independently.
  47. *
  48. * 2) We may simultaneously issue multiple requests on a single LDAP
  49. * connection. Each server has a "maxconcurrency" configuration
  50. * parameter associated with it that caps the number of outstanding operations
  51. * per connection. For each connection we maintain a "usecount"
  52. * which is used to track the number of threads using the connection.
  53. *
  54. * 3) IMPORTANT NOTE: This connexion management is stateless i.e there is no garanty that
  55. * operation from the same incoming client connections are sent to the same
  56. * outgoing connection to the farm server. Today, this is not a problem because
  57. * all controls we support are stateless. The implementation of the abandon
  58. * operation takes this limitation into account.
  59. *
  60. * 4) We may open more than one connection to a server. Each farm server
  61. * has a "maxconnections" configuration parameter associated with it
  62. * that caps the number of connections.
  63. *
  64. * 5) If no connection is available to service a request , threads
  65. * go to sleep on a condition variable and one is woken up each time
  66. * a connection's "usecount" is decremented.
  67. *
  68. * 6) If we see an LDAP_CONNECT_ERROR or LDAP_SERVER_DOWN error on a
  69. * session handle, we mark its status as CB_LDAP_STATUS_DOWN and
  70. * close it as soon as all threads using it release it. Connections
  71. * marked as "down" are not counted against the "maxconnections" limit.
  72. *
  73. * 7) We close and reopen connections that have been open for more than
  74. * the server's configured connection lifetime. This is done to ensure
  75. * that we reconnect to a primary server after failover occurs. If no
  76. * lifetime is configured or it is set to 0, we never close and reopen
  77. * connections.
  78. */
  79. static void cb_close_and_dispose_connection ( cb_outgoing_conn * conn );
  80. static void cb_check_for_stale_connections(cb_conn_pool * pool);
  81. PRUint32 PR_GetThreadID(PRThread *thread);
  82. /* returns the threadId of the current thread modulo MAX_CONN_ARRAY
  83. => gives the position of the thread in the array of secure connections */
  84. static int PR_ThreadSelf() {
  85. PRThread *thr = PR_GetCurrentThread();
  86. PRUint32 myself = PR_GetThreadID(thr);
  87. myself &= 0x000007FF ;
  88. return myself;
  89. }
  90. static int PR_MyThreadId() {
  91. PRThread *thr = PR_GetCurrentThread();
  92. PRUint32 myself = PR_GetThreadID(thr);
  93. return myself;
  94. }
  95. /*
  96. ** Close outgoing connections
  97. */
  98. void cb_close_conn_pool(cb_conn_pool * pool) {
  99. cb_outgoing_conn *conn, *nextconn;
  100. int secure = pool->secure;
  101. int i = 0;
  102. slapi_lock_mutex( pool->conn.conn_list_mutex );
  103. if (secure) {
  104. for (i=0; i< MAX_CONN_ARRAY; i++) {
  105. for (conn = pool->connarray[i]; conn != NULL; conn = nextconn) {
  106. if ( conn->status != CB_CONNSTATUS_OK ) {
  107. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  108. "cb_close_conn_pool: unexpected connection state (%d)\n",conn->status);
  109. }
  110. nextconn=conn->next;
  111. cb_close_and_dispose_connection(conn);
  112. }
  113. }
  114. }
  115. else {
  116. for ( conn = pool->conn.conn_list; conn != NULL; conn = nextconn ) {
  117. if ( conn->status != CB_CONNSTATUS_OK ) {
  118. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  119. "cb_close_conn_pool: unexpected connection state (%d)\n",conn->status);
  120. }
  121. nextconn=conn->next;
  122. cb_close_and_dispose_connection(conn);
  123. }
  124. }
  125. pool->conn.conn_list=NULL;
  126. pool->conn.conn_list_count=0;
  127. slapi_unlock_mutex( pool->conn.conn_list_mutex );
  128. }
  129. /*
  130. * Get an LDAP session handle for communicating with the farm servers.
  131. *
  132. * Returns an LDAP eror code, typically:
  133. * LDAP_SUCCESS
  134. * LDAP_TIMELIMIT_EXCEEDED
  135. * LDAP_CONNECT_ERROR
  136. * NOTE : if maxtime NULL, use operation timeout
  137. */
  138. int
  139. cb_get_connection(cb_conn_pool * pool,
  140. LDAP ** lld,
  141. cb_outgoing_conn ** cc,
  142. struct timeval * maxtime,
  143. char **errmsg)
  144. {
  145. int rc=LDAP_SUCCESS; /* optimistic */
  146. cb_outgoing_conn *conn=NULL;
  147. cb_outgoing_conn *connprev=NULL;
  148. LDAP *ld=NULL;
  149. time_t endbefore=0;
  150. int checktime=0;
  151. struct timeval bind_to, op_to;
  152. unsigned int maxconcurrency,maxconnections;
  153. char *password,*binddn,*hostname;
  154. unsigned int port;
  155. int secure;
  156. char *mech = NULL;;
  157. static char *error1="Can't contact remote server : %s";
  158. int isMultiThread = ENABLE_MULTITHREAD_PER_CONN ; /* by default, we enable multiple operations per connection */
  159. /*
  160. ** return an error if we can't get a connection
  161. ** before the operation timeout has expired
  162. ** bind_timeout: timeout for the bind operation (if bind needed)
  163. ** ( checked in ldap_result )
  164. ** op_timeout: timeout for the op that needs a connection
  165. ** ( checked in the loop )
  166. */
  167. *cc=NULL;
  168. PR_RWLock_Rlock(pool->rwl_config_lock);
  169. maxconcurrency=pool->conn.maxconcurrency;
  170. maxconnections=pool->conn.maxconnections;
  171. bind_to.tv_sec = pool->conn.bind_timeout.tv_sec;
  172. bind_to.tv_usec = pool->conn.bind_timeout.tv_usec;
  173. op_to.tv_sec = pool->conn.op_timeout.tv_sec;
  174. op_to.tv_usec = pool->conn.op_timeout.tv_usec;
  175. /* SD 02/10/2000 temp fix */
  176. /* allow dynamic update of the binddn & password */
  177. /* host, port and security mode */
  178. /* previous values are NOT freed when changed */
  179. /* won't likely to be changed often */
  180. /* pointers put in the waste basket fields and */
  181. /* freed when the backend is stopped. */
  182. password=pool->password;
  183. binddn=pool->binddn;
  184. hostname=pool->hostname;
  185. port=pool->port;
  186. secure=pool->secure;
  187. if (pool->starttls) {
  188. secure = 2;
  189. }
  190. mech=pool->mech;
  191. PR_RWLock_Unlock(pool->rwl_config_lock);
  192. if (secure) {
  193. isMultiThread = DISABLE_MULTITHREAD_PER_CONN ;
  194. }
  195. /* For stupid admins */
  196. if (maxconnections <=0) {
  197. static int warned_maxconn = 0;
  198. if (!warned_maxconn) {
  199. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  200. "<== cb_get_connection error (no connection available)\n");
  201. warned_maxconn = 1;
  202. }
  203. if ( errmsg ) {
  204. *errmsg = slapi_ch_smprintf("%s", ENDUSERMSG);
  205. }
  206. return LDAP_CONNECT_ERROR;
  207. }
  208. if (maxtime) {
  209. if (maxtime->tv_sec != 0) {
  210. checktime=1;
  211. endbefore = current_time() + maxtime->tv_sec;
  212. /* make sure bind to <= operation timeout */
  213. if ((bind_to.tv_sec==0) || (bind_to.tv_sec > maxtime->tv_sec))
  214. bind_to.tv_sec=maxtime->tv_sec;
  215. }
  216. } else {
  217. if (op_to.tv_sec != 0) {
  218. checktime=1;
  219. endbefore = current_time() + op_to.tv_sec;
  220. /* make sure bind to <= operation timeout */
  221. if ((bind_to.tv_sec==0) || (bind_to.tv_sec > op_to.tv_sec))
  222. bind_to.tv_sec=op_to.tv_sec;
  223. }
  224. }
  225. /*
  226. * Close (or mark to be closed) any connections for this farm server that have
  227. * exceeded the maximum connection lifetime.
  228. */
  229. cb_check_for_stale_connections(pool);
  230. /*
  231. * Look for an available, already open connection
  232. */
  233. slapi_lock_mutex( pool->conn.conn_list_mutex );
  234. if (cb_debug_on()) {
  235. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  236. "==> cb_get_connection server %s conns: %d maxconns: %d\n",
  237. hostname, pool->conn.conn_list_count, maxconnections );
  238. }
  239. for (;;) {
  240. /* time limit mgmt */
  241. if (checktime) {
  242. if (current_time() > endbefore ) {
  243. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  244. "cb_get_connection server %s expired.\n", hostname );
  245. if ( errmsg ) {
  246. *errmsg = PR_smprintf(error1,"timelimit exceeded");
  247. }
  248. rc=LDAP_TIMELIMIT_EXCEEDED;
  249. conn=NULL;
  250. ld=NULL;
  251. goto unlock_and_return;
  252. }
  253. }
  254. /*
  255. * First, look for an available, already open/bound connection
  256. */
  257. if (secure) {
  258. for (conn = pool->connarray[PR_ThreadSelf()]; conn != NULL; conn = conn->next) {
  259. if ((conn->ThreadId == PR_MyThreadId()) && (conn->status == CB_CONNSTATUS_OK &&
  260. conn->refcount < maxconcurrency)){
  261. if (cb_debug_on()) {
  262. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  263. "<= cb_get_connection server found conn 0x%p to use)\n", conn );
  264. }
  265. goto unlock_and_return; /* found one */
  266. }
  267. }
  268. }
  269. else {
  270. connprev = NULL;
  271. for ( conn = pool->conn.conn_list; conn != NULL; conn = conn->next ) {
  272. if (cb_debug_on()) {
  273. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  274. "list: conn 0x%p status %d refcount %lu\n", conn,
  275. conn->status, conn->refcount );
  276. }
  277. if ( conn->status == CB_CONNSTATUS_OK
  278. && conn->refcount < maxconcurrency ) {
  279. if (cb_debug_on()) {
  280. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  281. "<= cb_get_connection server found conn 0x%p to use)\n", conn );
  282. }
  283. goto unlock_and_return; /* found one */
  284. }
  285. connprev = conn;
  286. }
  287. }
  288. if ( secure || pool->conn.conn_list_count <maxconnections) {
  289. int version=LDAP_VERSION3;
  290. /*
  291. * we have not exceeded the maximum number of connections allowed,
  292. * so we initialize a new one and add it to the end of our list.
  293. */
  294. /* No need to lock. url can't be changed dynamically */
  295. ld = slapi_ldap_init(hostname, port, secure, isMultiThread);
  296. if (NULL == ld) {
  297. static int warned_init = 0;
  298. if (!warned_init) {
  299. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  300. "Can't contact server <%s> port <%d>.\n",
  301. hostname, port );
  302. warned_init = 1;
  303. }
  304. if ( errmsg ) {
  305. *errmsg = slapi_ch_smprintf("%s", ENDUSERMSG);
  306. }
  307. rc = LDAP_CONNECT_ERROR;
  308. goto unlock_and_return;
  309. }
  310. ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
  311. /* Don't chase referrals */
  312. ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
  313. /* no controls and simple bind only */
  314. /* For now, bind even if no user to detect error */
  315. /* earlier */
  316. if (pool->bindit) {
  317. PRErrorCode prerr = 0;
  318. LDAPControl **serverctrls=NULL;
  319. char *plain = NULL;
  320. int ret = -1;
  321. rc=LDAP_SUCCESS;
  322. if (cb_debug_on()) {
  323. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  324. "Bind to to server <%s> port <%d> as <%s>\n",
  325. hostname, port, binddn);
  326. }
  327. ret = pw_rever_decode(password, &plain, CB_CONFIG_USERPASSWORD);
  328. /* Pb occured in decryption: stop now, binding will fail */
  329. if ( ret == -1 )
  330. {
  331. static int warned_pw = 0;
  332. if (!warned_pw) {
  333. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  334. "Internal credentials decoding error; "
  335. "password storage schemes do not match or "
  336. "encrypted password is corrupted.\n");
  337. warned_pw = 1;
  338. }
  339. if ( errmsg ) {
  340. *errmsg = slapi_ch_smprintf("%s", ENDUSERMSG);
  341. }
  342. rc = LDAP_INVALID_CREDENTIALS;
  343. goto unlock_and_return;
  344. }
  345. /* Password-based client authentication */
  346. rc = slapi_ldap_bind(ld, binddn, plain, mech, NULL, &serverctrls,
  347. &bind_to, NULL);
  348. if ( ret == 0 ) slapi_ch_free_string(&plain); /* free plain only if it has been duplicated */
  349. if ( rc == LDAP_TIMEOUT ) {
  350. static int warned_bind_timeout = 0;
  351. if (!warned_bind_timeout) {
  352. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  353. "Can't bind to server <%s> port <%d>. (%s)\n",
  354. hostname, port, "time-out expired");
  355. warned_bind_timeout = 1;
  356. }
  357. if ( errmsg ) {
  358. *errmsg = slapi_ch_smprintf("%s", ENDUSERMSG);
  359. }
  360. rc = LDAP_CONNECT_ERROR;
  361. goto unlock_and_return;
  362. } else if ( rc != LDAP_SUCCESS ) {
  363. prerr=PR_GetError();
  364. static int warned_bind_err = 0;
  365. if (!warned_bind_err) {
  366. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  367. "Can't bind to server <%s> port <%d>. "
  368. "(LDAP error %d - %s; "
  369. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)\n",
  370. hostname, port, rc,
  371. ldap_err2string(rc),
  372. prerr, slapd_pr_strerror(prerr));
  373. warned_bind_err = 1;
  374. }
  375. if ( errmsg ) {
  376. *errmsg = slapi_ch_smprintf("%s", ENDUSERMSG);
  377. }
  378. rc = LDAP_CONNECT_ERROR;
  379. goto unlock_and_return;
  380. }
  381. if ( serverctrls )
  382. {
  383. int i;
  384. for( i = 0; serverctrls[ i ] != NULL; ++i )
  385. {
  386. if ( !(strcmp( serverctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRED)) )
  387. {
  388. /* Bind is successful but password has expired */
  389. slapi_log_error(SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  390. "Successfully bound as %s to remote server %s:%d, "
  391. "but password has expired.\n",
  392. binddn, hostname, port);
  393. }
  394. else if ( !(strcmp( serverctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRING)) )
  395. {
  396. /* The password is expiring in n seconds */
  397. if ( (serverctrls[ i ]->ldctl_value.bv_val != NULL) &&
  398. (serverctrls[ i ]->ldctl_value.bv_len > 0) )
  399. {
  400. int password_expiring = atoi( serverctrls[ i ]->ldctl_value.bv_val );
  401. slapi_log_error(SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  402. "Successfully bound as %s to remote server %s:%d, "
  403. "but password is expiring in %d seconds.\n",
  404. binddn, hostname, port, password_expiring);
  405. }
  406. }
  407. }
  408. ldap_controls_free(serverctrls);
  409. }
  410. }
  411. conn = (cb_outgoing_conn *) slapi_ch_malloc(sizeof(cb_outgoing_conn));
  412. conn->ld=ld;
  413. conn->status=CB_CONNSTATUS_OK;
  414. conn->refcount=0; /* incremented below */
  415. conn->opentime=current_time();
  416. conn->ThreadId=PR_MyThreadId(); /* store the thread id */
  417. conn->next=NULL;
  418. if (secure) {
  419. if (pool->connarray[PR_ThreadSelf()] == NULL) {
  420. pool->connarray[PR_ThreadSelf()] = conn;
  421. }
  422. else {
  423. conn->next = pool->connarray[PR_ThreadSelf()];
  424. pool->connarray[PR_ThreadSelf()] = conn ;
  425. }
  426. }
  427. else {
  428. if ( NULL == connprev ) {
  429. pool->conn.conn_list = conn;
  430. } else {
  431. connprev->next=conn;
  432. }
  433. }
  434. ++pool->conn.conn_list_count;
  435. if (cb_debug_on()) {
  436. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  437. "<= cb_get_connection added new conn 0x%p, "
  438. "conn count now %d\n", conn->ld, pool->conn.conn_list_count );
  439. }
  440. goto unlock_and_return; /* got a new one */
  441. }
  442. if (cb_debug_on()) {
  443. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  444. "... cb_get_connection waiting for conn to free up\n" );
  445. }
  446. if (!secure) slapi_wait_condvar( pool->conn.conn_list_cv, NULL );
  447. if (cb_debug_on()) {
  448. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  449. "... cb_get_connection awake again\n" );
  450. }
  451. }
  452. unlock_and_return:
  453. if ( conn != NULL ) {
  454. ++conn->refcount;
  455. *lld=conn->ld;
  456. *cc=conn;
  457. if (cb_debug_on()) {
  458. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  459. "<== cb_get_connection ld=0x%p (concurrency now %lu)\n",*lld, conn->refcount );
  460. }
  461. } else {
  462. if ( NULL != ld ) {
  463. slapi_ldap_unbind( ld );
  464. }
  465. if (cb_debug_on()) {
  466. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  467. "<== cb_get_connection error %d\n", rc );
  468. }
  469. }
  470. slapi_unlock_mutex(pool->conn.conn_list_mutex);
  471. return( rc );
  472. }
  473. /*
  474. * We are done with the connection handle because the
  475. * LDAP operation has completed.
  476. */
  477. void cb_release_op_connection(cb_conn_pool* pool, LDAP *lld, int dispose) {
  478. cb_outgoing_conn *conn;
  479. cb_outgoing_conn *connprev = NULL;
  480. int secure = pool->secure;
  481. int myself = 0;
  482. slapi_lock_mutex(pool->conn.conn_list_mutex);
  483. /*
  484. * find the connection structure this ld is part of
  485. */
  486. if (secure) {
  487. myself = PR_ThreadSelf();
  488. for (conn = pool->connarray[myself]; conn != NULL; conn = conn->next ) {
  489. if ( lld == conn->ld )
  490. break;
  491. connprev = conn;
  492. }
  493. }
  494. else {
  495. for ( conn = pool->conn.conn_list; conn != NULL; conn = conn->next ){
  496. if ( lld == conn->ld )
  497. break;
  498. connprev = conn;
  499. }
  500. }
  501. if ( conn == NULL ) { /* ld not found -- unexpected */
  502. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  503. "==> cb_release_op_connection ld=0x%p not found\n", lld );
  504. } else {
  505. --conn->refcount;
  506. if (cb_debug_on()) {
  507. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  508. "release conn 0x%p status %d refcount after release %lu\n", conn,
  509. conn->status, conn->refcount );
  510. }
  511. if ( dispose ) {
  512. conn->status = CB_CONNSTATUS_DOWN;
  513. }
  514. if ( conn->status != CB_CONNSTATUS_OK && conn->refcount == 0 ) {
  515. /*
  516. * remove from server's connection list
  517. */
  518. if (!secure) {
  519. if ( connprev == NULL ) {
  520. pool->conn.conn_list = conn->next;
  521. } else {
  522. connprev->next = conn->next;
  523. }
  524. }
  525. else {
  526. if ( connprev == NULL ) {
  527. pool->connarray[myself] = conn->next;
  528. } else {
  529. connprev->next = conn->next;
  530. }
  531. }
  532. --pool->conn.conn_list_count;
  533. /*
  534. * close connection and free memory
  535. */
  536. cb_close_and_dispose_connection( conn );
  537. }
  538. }
  539. /*
  540. * wake up a thread that is waiting for a connection
  541. */
  542. if (!secure) slapi_notify_condvar( pool->conn.conn_list_cv, 0 );
  543. slapi_unlock_mutex( pool->conn.conn_list_mutex );
  544. }
  545. static void
  546. cb_close_and_dispose_connection( cb_outgoing_conn *conn )
  547. {
  548. slapi_ldap_unbind( conn->ld );
  549. conn->ld = NULL;
  550. slapi_ch_free( (void **)&conn );
  551. }
  552. static void cb_check_for_stale_connections(cb_conn_pool * pool) {
  553. cb_outgoing_conn * connprev, *conn, *conn_next;
  554. time_t curtime;
  555. int connlifetime;
  556. int myself;
  557. PR_RWLock_Rlock(pool->rwl_config_lock);
  558. connlifetime=pool->conn.connlifetime;
  559. PR_RWLock_Unlock(pool->rwl_config_lock);
  560. connprev = NULL;
  561. conn_next = NULL;
  562. slapi_lock_mutex(pool->conn.conn_list_mutex);
  563. if (connlifetime > 0)
  564. curtime=current_time();
  565. if (pool->secure) {
  566. myself = PR_ThreadSelf();
  567. for (conn = pool->connarray[myself]; conn != NULL; conn = conn_next){
  568. if ((conn->status == CB_CONNSTATUS_STALE) ||
  569. (( connlifetime > 0) && (curtime - conn->opentime > connlifetime))) {
  570. if ( conn->refcount == 0 ) {
  571. if (cb_debug_on()) {
  572. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  573. "cb_check_for_stale_connections: conn 0x%p idle and stale\n",conn);
  574. }
  575. --pool->conn.conn_list_count;
  576. if (connprev == NULL) {
  577. pool->connarray[myself] = conn->next ;
  578. }
  579. else {
  580. connprev->next = conn->next ;
  581. }
  582. conn_next = conn->next ;
  583. cb_close_and_dispose_connection( conn );
  584. continue;
  585. }
  586. /* Connection is stale but in use */
  587. /* Mark to be disposed later but let it in the backend list */
  588. /* so that it is counted as a valid connection */
  589. else {
  590. conn->status = CB_CONNSTATUS_STALE;
  591. }
  592. if (cb_debug_on()) {
  593. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  594. "cb_check_for_stale_connections: conn 0x%p stale\n",conn);
  595. }
  596. }
  597. connprev = conn ;
  598. conn_next = conn->next;
  599. }
  600. slapi_unlock_mutex(pool->conn.conn_list_mutex);
  601. return;
  602. }
  603. for ( conn = pool->conn.conn_list; conn != NULL; conn=conn_next ) {
  604. if ((conn->status == CB_CONNSTATUS_STALE) ||
  605. (( connlifetime > 0) && (curtime - conn->opentime > connlifetime))) {
  606. if ( conn->refcount == 0 ) {
  607. /* Connection idle & stale. Remove and free. */
  608. if ( NULL == connprev )
  609. pool->conn.conn_list = conn->next;
  610. else
  611. connprev->next=conn->next;
  612. if (cb_debug_on()) {
  613. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  614. "cb_check_for_stale_connections: conn 0x%p idle and stale\n",conn);
  615. }
  616. --pool->conn.conn_list_count;
  617. conn_next=conn->next;
  618. cb_close_and_dispose_connection( conn );
  619. continue;
  620. }
  621. /* Connection is stale but in use */
  622. /* Mark to be disposed later but let it in the backend list */
  623. /* so that it is counted as a valid connection */
  624. else {
  625. conn->status = CB_CONNSTATUS_STALE;
  626. }
  627. if (cb_debug_on()) {
  628. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  629. "cb_check_for_stale_connections: conn 0x%p stale\n",conn);
  630. }
  631. }
  632. connprev = conn;
  633. conn_next=conn->next;
  634. }
  635. /* Generate an event to wake up threads waiting */
  636. /* for a conn to be released. Useful to detect */
  637. /* exceeded time limit. May be expensive */
  638. slapi_notify_condvar( pool->conn.conn_list_cv, 0 );
  639. slapi_unlock_mutex(pool->conn.conn_list_mutex);
  640. }
  641. /*
  642. * close all open connections in preparation for server shutdown, etc.
  643. * WARNING: Don't wait for current operations to complete
  644. */
  645. void
  646. cb_close_all_connections( Slapi_Backend * be )
  647. {
  648. cb_outgoing_conn *conn, *next_conn;
  649. cb_backend_instance * cb= cb_get_instance(be);
  650. int i;
  651. slapi_lock_mutex(cb->pool->conn.conn_list_mutex);
  652. if (cb->pool->secure) {
  653. for (i=0; i< MAX_CONN_ARRAY; i++) {
  654. for (conn = cb->pool->connarray[i]; conn != NULL; conn = next_conn ){
  655. next_conn = conn->next;
  656. cb_close_and_dispose_connection(conn);
  657. }
  658. }
  659. } else {
  660. for ( conn = cb->pool->conn.conn_list; conn != NULL; conn = next_conn ) {
  661. next_conn=conn->next;
  662. cb_close_and_dispose_connection(conn);
  663. }
  664. }
  665. slapi_unlock_mutex(cb->pool->conn.conn_list_mutex);
  666. slapi_lock_mutex(cb->bind_pool->conn.conn_list_mutex);
  667. if (cb->bind_pool->secure) {
  668. for (i=0; i< MAX_CONN_ARRAY; i++) {
  669. for (conn = cb->bind_pool->connarray[i]; conn != NULL; conn = next_conn ){
  670. next_conn=conn->next;
  671. cb_close_and_dispose_connection(conn);
  672. }
  673. }
  674. } else {
  675. for ( conn = cb->bind_pool->conn.conn_list; conn != NULL; conn = next_conn ) {
  676. next_conn=conn->next;
  677. cb_close_and_dispose_connection(conn);
  678. }
  679. }
  680. slapi_unlock_mutex(cb->bind_pool->conn.conn_list_mutex);
  681. }
  682. /* Mark used connections as stale and close unsued connections */
  683. /* Called when the target farm url has changed */
  684. void cb_stale_all_connections( cb_backend_instance * cb)
  685. {
  686. cb_outgoing_conn *conn, *next_conn, *prev_conn;
  687. int notify=0;
  688. int i, j;
  689. cb_conn_pool *pools[3];
  690. pools[0]=cb->pool;
  691. pools[1]=cb->bind_pool;
  692. pools[2]=NULL;
  693. for (i=0; pools[i]; i++) {
  694. slapi_lock_mutex(pools[i]->conn.conn_list_mutex);
  695. for (j=0; j< MAX_CONN_ARRAY; j++) {
  696. prev_conn=NULL;
  697. for (conn = pools[i]->connarray[j]; conn != NULL; conn=next_conn) {
  698. next_conn=conn->next;
  699. if (conn->refcount > 0) {
  700. /*
  701. ** Connection is stale but in use
  702. ** Mark to be disposed later but let it in the backend list
  703. ** so that it is counted as a valid connection
  704. */
  705. conn->status = CB_CONNSTATUS_STALE;
  706. prev_conn=conn;
  707. } else {
  708. if (prev_conn == NULL) {
  709. pools[i]->connarray[j]=next_conn;
  710. } else {
  711. prev_conn->next=next_conn;
  712. }
  713. cb_close_and_dispose_connection(conn);
  714. pools[i]->conn.conn_list_count--;
  715. }
  716. }
  717. }
  718. prev_conn = NULL ;
  719. for ( conn = pools[i]->conn.conn_list; conn != NULL; conn = next_conn ) {
  720. next_conn=conn->next;
  721. if (conn->refcount > 0) {
  722. /*
  723. ** Connection is stale but in use
  724. ** Mark to be disposed later but let it in the backend list
  725. ** so that it is counted as a valid connection
  726. */
  727. conn->status = CB_CONNSTATUS_STALE;
  728. prev_conn=conn;
  729. }
  730. else {
  731. if (conn==pools[i]->conn.conn_list) {
  732. pools[i]->conn.conn_list=next_conn;
  733. } else {
  734. prev_conn->next=next_conn;
  735. }
  736. cb_close_and_dispose_connection(conn);
  737. pools[i]->conn.conn_list_count--;
  738. notify=1;
  739. }
  740. }
  741. if (notify && (! pools[i]->secure)) {
  742. slapi_notify_condvar( pools[i]->conn.conn_list_cv, 0 );
  743. }
  744. slapi_unlock_mutex(pools[i]->conn.conn_list_mutex);
  745. }
  746. }
  747. /* Try to figure out if a farm server is still alive */
  748. int cb_ping_farm(cb_backend_instance *cb, cb_outgoing_conn * cnx,time_t end_time) {
  749. char *attrs[] ={"1.1",NULL};
  750. int rc;
  751. struct timeval timeout;
  752. LDAP *ld;
  753. LDAPMessage *result;
  754. time_t now;
  755. int secure;
  756. if (cb->max_idle_time <=0) /* Heart-beat disabled */
  757. return LDAP_SUCCESS;
  758. if (cnx && (cnx->status != CB_CONNSTATUS_OK )) /* Known problem */
  759. return LDAP_SERVER_DOWN;
  760. now = current_time();
  761. if (end_time && ((now <= end_time) || (end_time <0))) return LDAP_SUCCESS;
  762. secure = cb->pool->secure;
  763. if (cb->pool->starttls) {
  764. secure = 2;
  765. }
  766. ld=slapi_ldap_init(cb->pool->hostname,cb->pool->port,secure,0);
  767. if (NULL == ld) {
  768. cb_update_failed_conn_cpt( cb );
  769. return LDAP_SERVER_DOWN;
  770. }
  771. timeout.tv_sec=cb->max_test_time;
  772. timeout.tv_usec=0;
  773. /* NOTE: This will fail if we implement the ability to disable
  774. anonymous bind */
  775. rc=ldap_search_ext_s(ld ,NULL,LDAP_SCOPE_BASE,"objectclass=*",attrs,1,NULL,
  776. NULL, &timeout, 1,&result);
  777. if ( LDAP_SUCCESS != rc ) {
  778. slapi_ldap_unbind( ld );
  779. cb_update_failed_conn_cpt( cb );
  780. return LDAP_SERVER_DOWN;
  781. }
  782. ldap_msgfree(result);
  783. slapi_ldap_unbind( ld );
  784. cb_reset_conn_cpt( cb );
  785. return LDAP_SUCCESS;
  786. }
  787. void cb_update_failed_conn_cpt ( cb_backend_instance *cb ) {
  788. /* if the chaining BE is already unavailable, we do nothing*/
  789. time_t now;
  790. if (cb->monitor_availability.farmserver_state == FARMSERVER_AVAILABLE) {
  791. slapi_lock_mutex(cb->monitor_availability.cpt_lock);
  792. cb->monitor_availability.cpt ++;
  793. slapi_unlock_mutex(cb->monitor_availability.cpt_lock);
  794. if (cb->monitor_availability.cpt >= CB_NUM_CONN_BEFORE_UNAVAILABILITY ) {
  795. /* we reach the limit of authorized failed connections => we setup the chaining BE state to unavailable */
  796. now = current_time();
  797. slapi_lock_mutex(cb->monitor_availability.lock_timeLimit);
  798. cb->monitor_availability.unavailableTimeLimit = now + CB_UNAVAILABLE_PERIOD ;
  799. slapi_unlock_mutex(cb->monitor_availability.lock_timeLimit);
  800. cb->monitor_availability.farmserver_state = FARMSERVER_UNAVAILABLE ;
  801. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  802. "cb_update_failed_conn_cpt: Farm server unavailable");
  803. }
  804. }
  805. }
  806. void cb_reset_conn_cpt( cb_backend_instance *cb ) {
  807. if (cb->monitor_availability.cpt > 0) {
  808. slapi_lock_mutex(cb->monitor_availability.cpt_lock);
  809. cb->monitor_availability.cpt = 0 ;
  810. if (cb->monitor_availability.farmserver_state == FARMSERVER_UNAVAILABLE) {
  811. cb->monitor_availability.farmserver_state = FARMSERVER_AVAILABLE ;
  812. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  813. "cb_reset_conn_cpt: Farm server is back");
  814. }
  815. slapi_unlock_mutex(cb->monitor_availability.cpt_lock);
  816. }
  817. }
  818. int cb_check_availability( cb_backend_instance *cb, Slapi_PBlock *pb ) {
  819. /* check wether the farmserver is available or not */
  820. time_t now ;
  821. if ( cb->monitor_availability.farmserver_state == FARMSERVER_UNAVAILABLE ){
  822. slapi_lock_mutex(cb->monitor_availability.lock_timeLimit);
  823. now = current_time();
  824. if (now >= cb->monitor_availability.unavailableTimeLimit) {
  825. cb->monitor_availability.unavailableTimeLimit = now + CB_INFINITE_TIME ; /* to be sure only one thread can do the test */
  826. slapi_unlock_mutex(cb->monitor_availability.lock_timeLimit);
  827. }
  828. else {
  829. slapi_unlock_mutex(cb->monitor_availability.lock_timeLimit);
  830. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL) ;
  831. return FARMSERVER_UNAVAILABLE ;
  832. }
  833. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  834. "cb_check_availability: ping the farm server and check if it's still unavailable");
  835. if (cb_ping_farm(cb, NULL, 0) != LDAP_SUCCESS) { /* farm still unavailable... Just change the timelimit */
  836. slapi_lock_mutex(cb->monitor_availability.lock_timeLimit);
  837. now = current_time();
  838. cb->monitor_availability.unavailableTimeLimit = now + CB_UNAVAILABLE_PERIOD ;
  839. slapi_unlock_mutex(cb->monitor_availability.lock_timeLimit);
  840. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL) ;
  841. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  842. "cb_check_availability: Farm server still unavailable");
  843. return FARMSERVER_UNAVAILABLE ;
  844. }
  845. else {
  846. /* farm is back !*/
  847. slapi_lock_mutex(cb->monitor_availability.lock_timeLimit);
  848. now = current_time();
  849. cb->monitor_availability.unavailableTimeLimit = now ; /* the unavailable period is finished */
  850. slapi_unlock_mutex(cb->monitor_availability.lock_timeLimit);
  851. /* The farmer server state backs to FARMSERVER_AVAILABLE, but this already done in cb_ping_farm, and also the reset of cpt*/
  852. return FARMSERVER_AVAILABLE ;
  853. }
  854. }
  855. return FARMSERVER_AVAILABLE ;
  856. }