1
0

cb_conn_stateless.c 32 KB

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