cb_conn_stateless.c 33 KB

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