cb_conn_stateless.c 31 KB

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