1
0

cb_conn_stateless.c 32 KB

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