repl5_connection.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  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. /* repl5_connection.c */
  42. /*
  43. The connection object manages a connection to a single replication
  44. consumer.
  45. XXXggood what to do on timeout? If we close connection, then we won't leave a
  46. replica locked. Seems like right thing to do.
  47. */
  48. #include "repl5.h"
  49. #if defined(USE_OPENLDAP)
  50. #include "ldap.h"
  51. #else
  52. #include "ldappr.h"
  53. #include "ldap-extension.h"
  54. #endif
  55. #include "nspr.h"
  56. #include "private/pprio.h"
  57. #include "nss.h"
  58. typedef struct repl_connection
  59. {
  60. char *hostname;
  61. int port;
  62. char *binddn;
  63. int bindmethod;
  64. int state;
  65. int last_operation;
  66. int last_ldap_error;
  67. const char *status;
  68. char *last_ldap_errmsg;
  69. PRUint32 transport_flags;
  70. LDAP *ld;
  71. int supports_ldapv3; /* 1 if does, 0 if doesn't, -1 if not determined */
  72. int supports_ds50_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  73. int supports_ds40_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  74. int supports_ds71_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  75. int linger_time; /* time in seconds to leave an idle connection open */
  76. PRBool linger_active;
  77. Slapi_Eq_Context *linger_event;
  78. PRBool delete_after_linger;
  79. int refcnt;
  80. const Repl_Agmt *agmt;
  81. PRLock *lock;
  82. struct timeval timeout;
  83. int flag_agmt_changed;
  84. char *plain;
  85. } repl_connection;
  86. /* #define DEFAULT_LINGER_TIME (5 * 60) */ /* 5 minutes */
  87. #define DEFAULT_LINGER_TIME (60)
  88. /* Controls we add on every outbound operation */
  89. static LDAPControl manageDSAITControl = {LDAP_CONTROL_MANAGEDSAIT, {0, ""}, '\0'};
  90. static int attribute_string_value_present(LDAP *ld, LDAPMessage *entry,
  91. const char *type, const char *value);
  92. static int bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password);
  93. static int s_debug_timeout = 0;
  94. static int s_debug_level = 0;
  95. static Slapi_Eq_Context repl5_start_debug_timeout(int *setlevel);
  96. static void repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel);
  97. static void repl5_debug_timeout_callback(time_t when, void *arg);
  98. #define STATE_CONNECTED 600
  99. #define STATE_DISCONNECTED 601
  100. #define STATUS_DISCONNECTED "disconnected"
  101. #define STATUS_CONNECTED "connected"
  102. #define STATUS_PROCESSING_ADD "processing add operation"
  103. #define STATUS_PROCESSING_DELETE "processing delete operation"
  104. #define STATUS_PROCESSING_MODIFY "processing modify operation"
  105. #define STATUS_PROCESSING_RENAME "processing rename operation"
  106. #define STATUS_PROCESSING_EXTENDED_OPERATION "processing extended operation"
  107. #define STATUS_LINGERING "lingering"
  108. #define STATUS_SHUTTING_DOWN "shutting down"
  109. #define STATUS_BINDING "connecting and binding"
  110. #define STATUS_SEARCHING "processing search operation"
  111. #define CONN_NO_OPERATION 0
  112. #define CONN_ADD 1
  113. #define CONN_DELETE 2
  114. #define CONN_MODIFY 3
  115. #define CONN_RENAME 4
  116. #define CONN_EXTENDED_OPERATION 5
  117. #define CONN_BIND 6
  118. #define CONN_INIT 7
  119. /* These are errors returned from ldap operations which should cause us to disconnect and
  120. retry the connection later */
  121. #define IS_DISCONNECT_ERROR(rc) (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR || rc == LDAP_INVALID_CREDENTIALS || rc == LDAP_INAPPROPRIATE_AUTH || rc == LDAP_LOCAL_ERROR)
  122. /* Forward declarations */
  123. static void close_connection_internal(Repl_Connection *conn);
  124. /*
  125. * Create a new conenction object. Returns a pointer to the object, or
  126. * NULL if an error occurs.
  127. */
  128. Repl_Connection *
  129. conn_new(Repl_Agmt *agmt)
  130. {
  131. Repl_Connection *rpc;
  132. rpc = (Repl_Connection *)slapi_ch_malloc(sizeof(repl_connection));
  133. if ((rpc->lock = PR_NewLock()) == NULL)
  134. {
  135. goto loser;
  136. }
  137. rpc->hostname = agmt_get_hostname(agmt);
  138. rpc->port = agmt_get_port(agmt);
  139. rpc->binddn = agmt_get_binddn(agmt);
  140. rpc->bindmethod = agmt_get_bindmethod(agmt);
  141. rpc->transport_flags = agmt_get_transport_flags(agmt);
  142. rpc->ld = NULL;
  143. rpc->state = STATE_DISCONNECTED;
  144. rpc->last_operation = CONN_NO_OPERATION;
  145. rpc->last_ldap_error = LDAP_SUCCESS;
  146. rpc->last_ldap_errmsg = NULL;
  147. rpc->supports_ldapv3 = -1;
  148. rpc->supports_ds40_repl = -1;
  149. rpc->supports_ds50_repl = -1;
  150. rpc->supports_ds71_repl = -1;
  151. rpc->linger_active = PR_FALSE;
  152. rpc->delete_after_linger = PR_FALSE;
  153. rpc->linger_event = NULL;
  154. rpc->linger_time = DEFAULT_LINGER_TIME;
  155. rpc->status = STATUS_DISCONNECTED;
  156. rpc->agmt = agmt;
  157. rpc->refcnt = 1;
  158. rpc->timeout.tv_sec = agmt_get_timeout(agmt);
  159. rpc->timeout.tv_usec = 0;
  160. rpc->flag_agmt_changed = 0;
  161. rpc->plain = NULL;
  162. return rpc;
  163. loser:
  164. conn_delete(rpc);
  165. return NULL;
  166. }
  167. /*
  168. * Return PR_TRUE if the connection is in the connected state
  169. */
  170. static PRBool
  171. conn_connected(Repl_Connection *conn)
  172. {
  173. PRBool return_value;
  174. PR_Lock(conn->lock);
  175. return_value = STATE_CONNECTED == conn->state;
  176. PR_Unlock(conn->lock);
  177. return return_value;
  178. }
  179. /*
  180. * Destroy a connection object.
  181. */
  182. static void
  183. conn_delete_internal(Repl_Connection *conn)
  184. {
  185. PR_ASSERT(NULL != conn);
  186. close_connection_internal(conn);
  187. /* slapi_ch_free accepts NULL pointer */
  188. slapi_ch_free((void **)&conn->hostname);
  189. slapi_ch_free((void **)&conn->binddn);
  190. slapi_ch_free((void **)&conn->plain);
  191. }
  192. /*
  193. * Destroy a connection. It is an error to use the connection object
  194. * after conn_delete() has been called.
  195. */
  196. void
  197. conn_delete(Repl_Connection *conn)
  198. {
  199. PRBool destroy_it = PR_FALSE;
  200. PR_ASSERT(NULL != conn);
  201. PR_Lock(conn->lock);
  202. if (conn->linger_active)
  203. {
  204. if (slapi_eq_cancel(conn->linger_event) == 1)
  205. {
  206. /* Event was found and cancelled. Destroy the connection object. */
  207. PR_Unlock(conn->lock);
  208. destroy_it = PR_TRUE;
  209. }
  210. else
  211. {
  212. /*
  213. * The event wasn't found, but we think it's still active.
  214. * That means an event is in the process of being fired
  215. * off, so arrange for the event to destroy the object .
  216. */
  217. conn->delete_after_linger = PR_TRUE;
  218. PR_Unlock(conn->lock);
  219. }
  220. }
  221. if (destroy_it)
  222. {
  223. conn_delete_internal(conn);
  224. }
  225. }
  226. /*
  227. * Return the last operation type processed by the connection
  228. * object, and the LDAP error encountered.
  229. */
  230. void
  231. conn_get_error(Repl_Connection *conn, int *operation, int *error)
  232. {
  233. PR_Lock(conn->lock);
  234. *operation = conn->last_operation;
  235. *error = conn->last_ldap_error;
  236. PR_Unlock(conn->lock);
  237. }
  238. /*
  239. * Return the last operation type processed by the connection
  240. * object, and the LDAP error encountered.
  241. * Beware that the error string will only be in scope and valid
  242. * before the next operation result has been read from the connection
  243. * (so don't alias the pointer).
  244. */
  245. void
  246. conn_get_error_ex(Repl_Connection *conn, int *operation, int *error, char **error_string)
  247. {
  248. PR_Lock(conn->lock);
  249. *operation = conn->last_operation;
  250. *error = conn->last_ldap_error;
  251. *error_string = conn->last_ldap_errmsg;
  252. PR_Unlock(conn->lock);
  253. }
  254. /* Returns the result (asyncronously) from an opertation and also returns that operations message ID */
  255. /* The _ex version handles a bunch of parameters (retoidp et al) that were present in the original
  256. * sync operation functions, but were never actually used) */
  257. ConnResult
  258. conn_read_result_ex(Repl_Connection *conn, char **retoidp, struct berval **retdatap, LDAPControl ***returned_controls, int *message_id, int block)
  259. {
  260. LDAPMessage *res = NULL;
  261. int setlevel = 0;
  262. int rc = 0;
  263. int return_value = 0;
  264. LDAPControl **loc_returned_controls = NULL;
  265. struct timeval local_timeout = {0};
  266. time_t time_now = 0;
  267. time_t start_time = time( NULL );
  268. int backoff_time = 1;
  269. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  270. /* Here, we want to not block inside ldap_result().
  271. * Reason is that blocking there will deadlock with a
  272. * concurrent sender. We send concurrently, and hence
  273. * blocking is not good : deadlock results.
  274. * So, instead, we call ldap_result() with a zero timeout.
  275. * This makes it do a non-blocking poll and return to us
  276. * if there's no data to read.
  277. * We can then handle our timeout here by sleeping and re-trying.
  278. * In order that we do pickup results reasonably quickly,
  279. * we implement a backoff algorithm for the sleep: if we
  280. * keep getting results quickly then we won't spend much time sleeping.
  281. */
  282. while (1)
  283. {
  284. /* we have to make sure the update sending thread does not
  285. attempt to call conn_disconnect while we are reading
  286. results - so lock the conn while we get the results */
  287. PR_Lock(conn->lock);
  288. if ((STATE_CONNECTED != conn->state) || !conn->ld) {
  289. rc = -1;
  290. return_value = CONN_NOT_CONNECTED;
  291. PR_Unlock(conn->lock);
  292. break;
  293. }
  294. rc = ldap_result(conn->ld, LDAP_RES_ANY , 1, &local_timeout, &res);
  295. PR_Unlock(conn->lock);
  296. if (0 != rc)
  297. {
  298. /* Something other than a timeout happened */
  299. break;
  300. }
  301. if (block)
  302. {
  303. /* Did the connection's timeout expire ? */
  304. time_now = time( NULL );
  305. if (conn->timeout.tv_sec <= ( time_now - start_time ))
  306. {
  307. /* We timed out */
  308. rc = 0;
  309. break;
  310. }
  311. /* Otherwise we backoff */
  312. DS_Sleep(PR_MillisecondsToInterval(backoff_time));
  313. if (backoff_time < 1000)
  314. {
  315. backoff_time <<= 1;
  316. }
  317. } else
  318. {
  319. rc = 0;
  320. break;
  321. }
  322. }
  323. repl5_stop_debug_timeout(eqctx, &setlevel);
  324. PR_Lock(conn->lock);
  325. /* we have to check again since the connection may have
  326. been closed in the meantime
  327. acquire the lock for the rest of the function
  328. to protect against another attempt to close
  329. the conn while we are using it
  330. */
  331. if ((STATE_CONNECTED != conn->state) || !conn->ld) {
  332. rc = -1;
  333. return_value = CONN_NOT_CONNECTED;
  334. }
  335. if (0 == rc)
  336. {
  337. /* Timeout */
  338. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  339. conn->last_ldap_error = LDAP_TIMEOUT;
  340. return_value = CONN_TIMEOUT;
  341. }
  342. else if ((-1 == rc) && (CONN_NOT_CONNECTED == return_value))
  343. {
  344. /* must not access conn->ld if disconnected in another thread */
  345. /* the other thread that actually did the conn_disconnect() */
  346. /* will set the status and error info */
  347. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  348. "%s: Connection disconnected by another thread\n",
  349. agmt_get_long_name(conn->agmt));
  350. }
  351. else if (-1 == rc)
  352. {
  353. /* Error */
  354. char *s = NULL;
  355. rc = slapi_ldap_get_lderrno(conn->ld, NULL, &s);
  356. conn->last_ldap_errmsg = s;
  357. conn->last_ldap_error = rc;
  358. /* some errors will require a disconnect and retry the connection
  359. later */
  360. if (IS_DISCONNECT_ERROR(rc))
  361. {
  362. close_connection_internal(conn); /* we already have the lock */
  363. return_value = CONN_NOT_CONNECTED;
  364. }
  365. else
  366. {
  367. conn->status = STATUS_CONNECTED;
  368. return_value = CONN_OPERATION_FAILED;
  369. }
  370. }
  371. else
  372. {
  373. int err;
  374. char *errmsg = NULL;
  375. char **referrals = NULL;
  376. char *matched = NULL;
  377. if (message_id)
  378. {
  379. *message_id = ldap_msgid(res);
  380. }
  381. rc = ldap_parse_result(conn->ld, res, &err, &matched,
  382. &errmsg, &referrals, &loc_returned_controls,
  383. 0 /* Don't free the result */);
  384. if (IS_DISCONNECT_ERROR(rc))
  385. {
  386. conn->last_ldap_error = rc;
  387. close_connection_internal(conn); /* we already have the lock */
  388. return_value = CONN_NOT_CONNECTED;
  389. }
  390. else if (IS_DISCONNECT_ERROR(err))
  391. {
  392. conn->last_ldap_error = err;
  393. close_connection_internal(conn); /* we already have the lock */
  394. return_value = CONN_NOT_CONNECTED;
  395. }
  396. /* Got a result */
  397. if ((rc == LDAP_SUCCESS) && (err == LDAP_BUSY))
  398. return_value = CONN_BUSY;
  399. else if (retoidp)
  400. {
  401. if (!((rc == LDAP_SUCCESS) && (err == LDAP_BUSY)))
  402. {
  403. if (rc == LDAP_SUCCESS) {
  404. rc = ldap_parse_extended_result(conn->ld, res, retoidp,
  405. retdatap, 0 /* Don't Free it */);
  406. }
  407. conn->last_ldap_error = rc;
  408. return_value = (LDAP_SUCCESS == conn->last_ldap_error ?
  409. CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED);
  410. }
  411. }
  412. else /* regular operation, result returned */
  413. {
  414. if (NULL != returned_controls)
  415. {
  416. *returned_controls = loc_returned_controls;
  417. }
  418. if (LDAP_SUCCESS != rc)
  419. {
  420. conn->last_ldap_error = rc;
  421. }
  422. else
  423. {
  424. conn->last_ldap_error = err;
  425. }
  426. return_value = LDAP_SUCCESS == conn->last_ldap_error ? CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED;
  427. }
  428. /*
  429. * XXXggood do I need to free matched, referrals,
  430. * anything else? Or can I pass NULL for the args
  431. * I'm not interested in?
  432. */
  433. /* Good question! Meanwhile, as RTM aproaches, let's free them... */
  434. slapi_ch_free((void **) &errmsg);
  435. slapi_ch_free((void **) &matched);
  436. charray_free(referrals);
  437. conn->status = STATUS_CONNECTED;
  438. }
  439. if (res) ldap_msgfree(res);
  440. PR_Unlock(conn->lock); /* release the conn lock */
  441. return return_value;
  442. }
  443. ConnResult
  444. conn_read_result(Repl_Connection *conn, int *message_id)
  445. {
  446. return conn_read_result_ex(conn,NULL,NULL,NULL,message_id,1);
  447. }
  448. /* Because the SDK isn't really thread-safe (it can deadlock between
  449. * a thread sending an operation and a thread trying to retrieve a response
  450. * on the same connection), we need to _first_ verify that the connection
  451. * is writable. If it isn't, we can deadlock if we proceed any further...
  452. */
  453. #if defined(USE_OPENLDAP)
  454. /* openldap has LBER_SB_OPT_DATA_READY but that doesn't really
  455. work for our purposes - so we grab the openldap fd from the
  456. ber sockbuf layer, import it into a PR Poll FD, then
  457. do the poll
  458. */
  459. static ConnResult
  460. see_if_write_available(Repl_Connection *conn, PRIntervalTime timeout)
  461. {
  462. PRFileDesc *pollfd = NULL;
  463. PRPollDesc polldesc;
  464. ber_socket_t fd = 0;
  465. int rc;
  466. /* get the sockbuf */
  467. ldap_get_option(conn->ld, LDAP_OPT_DESC, &fd);
  468. if (fd <= 0) {
  469. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  470. "%s: invalid connection insee_if_write_available \n",
  471. agmt_get_long_name(conn->agmt));
  472. conn->last_ldap_error = LDAP_PARAM_ERROR;
  473. return CONN_OPERATION_FAILED;
  474. }
  475. /* wrap the sockbuf fd with a NSPR FD created especially
  476. for use with polling, and only with polling */
  477. pollfd = PR_CreateSocketPollFd(fd);
  478. polldesc.fd = pollfd;
  479. polldesc.in_flags = PR_POLL_WRITE|PR_POLL_EXCEPT;
  480. polldesc.out_flags = 0;
  481. /* do the poll */
  482. rc = PR_Poll(&polldesc, 1, timeout);
  483. /* unwrap the socket */
  484. PR_DestroySocketPollFd(pollfd);
  485. /* check */
  486. if (rc == 0) { /* timeout */
  487. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  488. "%s: poll timed out - poll interval [%d]\n",
  489. agmt_get_long_name(conn->agmt),
  490. timeout);
  491. return CONN_TIMEOUT;
  492. } else if ((rc < 0) || ((polldesc.out_flags|PR_POLL_WRITE) == 0)) { /* error */
  493. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  494. "%s: error during poll attempt [%d:%s]\n",
  495. agmt_get_long_name(conn->agmt),
  496. PR_GetError(), slapd_pr_strerror(PR_GetError()));
  497. conn->last_ldap_error = LDAP_PARAM_ERROR;
  498. return CONN_OPERATION_FAILED;
  499. }
  500. return CONN_OPERATION_SUCCESS;
  501. }
  502. #else /* ! USE_OPENLDAP */
  503. /* Since we're poking around with ldap c sdk internals, we have to
  504. be careful since the PR layer stores different session and socket
  505. info than the NSS SSL layer than the SASL layer - and they all
  506. use different poll functions too
  507. */
  508. static ConnResult
  509. see_if_write_available(Repl_Connection *conn, PRIntervalTime timeout)
  510. {
  511. LDAP_X_PollFD pollstr;
  512. int nfds = 1;
  513. struct ldap_x_ext_io_fns iofns;
  514. int rc = LDAP_SUCCESS;
  515. LDAP_X_EXTIOF_POLL_CALLBACK *ldap_poll;
  516. struct lextiof_session_private *private;
  517. /* get the poll function to use */
  518. memset(&iofns, 0, sizeof(iofns));
  519. iofns.lextiof_size = LDAP_X_EXTIO_FNS_SIZE;
  520. if (ldap_get_option(conn->ld, LDAP_X_OPT_EXTIO_FN_PTRS, &iofns) < 0) {
  521. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  522. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  523. "%s: Failed call to ldap_get_option to get extiofns in "
  524. "see_if_write_available: LDAP error %d (%s)\n",
  525. agmt_get_long_name(conn->agmt),
  526. rc, ldap_err2string(rc));
  527. conn->last_ldap_error = rc;
  528. return CONN_OPERATION_FAILED;
  529. }
  530. ldap_poll = iofns.lextiof_poll;
  531. /* set up the poll structure */
  532. if (ldap_get_option(conn->ld, LDAP_OPT_DESC, &pollstr.lpoll_fd) < 0) {
  533. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  534. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  535. "%s: Failed call to ldap_get_option for poll_fd in "
  536. "see_if_write_available: LDAP error %d (%s)\n",
  537. agmt_get_long_name(conn->agmt),
  538. rc, ldap_err2string(rc));
  539. conn->last_ldap_error = rc;
  540. return CONN_OPERATION_FAILED;
  541. }
  542. if (ldap_get_option(conn->ld, LDAP_X_OPT_SOCKETARG,
  543. &pollstr.lpoll_socketarg) < 0) {
  544. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  545. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  546. "%s: Failed call to ldap_get_option for socketarg in "
  547. "see_if_write_available: LDAP error %d (%s)\n",
  548. agmt_get_long_name(conn->agmt),
  549. rc, ldap_err2string(rc));
  550. conn->last_ldap_error = rc;
  551. return CONN_OPERATION_FAILED;
  552. }
  553. pollstr.lpoll_events = LDAP_X_POLLOUT;
  554. pollstr.lpoll_revents = 0;
  555. private = iofns.lextiof_session_arg;
  556. if (0 == (*ldap_poll)(&pollstr, nfds, timeout, private)) {
  557. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  558. "%s: poll timed out - poll interval [%d]\n",
  559. agmt_get_long_name(conn->agmt),
  560. timeout);
  561. return CONN_TIMEOUT;
  562. }
  563. return CONN_OPERATION_SUCCESS;
  564. }
  565. #endif /* ! USE_OPENLDAP */
  566. /*
  567. * Common code to send an LDAPv3 operation and collect the result.
  568. * Return values:
  569. * CONN_OPERATION_SUCCESS - the operation succeeded
  570. * CONN_OPERATION_FAILED - the operation was sent to the consumer
  571. * and failed. Use conn_get_error() to determine the LDAP error
  572. * code.
  573. * CONN_NOT_CONNECTED - no connection is active. The caller should
  574. * use conn_connect() to connect to the replica and bind, then should
  575. * reacquire the replica (if needed).
  576. * CONN_BUSY - the server is busy with previous requests, must wait for a while
  577. * before retrying
  578. * DBDB: also returns the operation's message ID, if it was successfully sent, now that
  579. * we're reading results async.
  580. */
  581. static ConnResult
  582. perform_operation(Repl_Connection *conn, int optype, const char *dn,
  583. LDAPMod **attrs, const char *newrdn, const char *newparent,
  584. int deleteoldrdn, LDAPControl *update_control,
  585. const char *extop_oid, struct berval *extop_payload, int *message_id)
  586. {
  587. int rc;
  588. ConnResult return_value = CONN_OPERATION_FAILED;
  589. LDAPControl *server_controls[3];
  590. /* LDAPControl **loc_returned_controls; */
  591. const char *op_string = NULL;
  592. int msgid = 0;
  593. server_controls[0] = &manageDSAITControl;
  594. server_controls[1] = update_control;
  595. server_controls[2] = NULL;
  596. /* lock the conn to prevent the result reader thread
  597. from closing the connection out from under us */
  598. PR_Lock(conn->lock);
  599. if (STATE_CONNECTED == conn->state)
  600. {
  601. int setlevel = 0;
  602. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  603. return_value = see_if_write_available(
  604. conn, PR_SecondsToInterval(conn->timeout.tv_sec));
  605. if (return_value != CONN_OPERATION_SUCCESS) {
  606. PR_Unlock(conn->lock);
  607. return return_value;
  608. }
  609. conn->last_operation = optype;
  610. switch (optype)
  611. {
  612. case CONN_ADD:
  613. conn->status = STATUS_PROCESSING_ADD;
  614. op_string = "add";
  615. rc = ldap_add_ext(conn->ld, dn, attrs, server_controls,
  616. NULL /* clientctls */, &msgid);
  617. break;
  618. case CONN_MODIFY:
  619. conn->status = STATUS_PROCESSING_MODIFY;
  620. op_string = "modify";
  621. rc = ldap_modify_ext(conn->ld, dn, attrs, server_controls,
  622. NULL /* clientctls */, &msgid);
  623. break;
  624. case CONN_DELETE:
  625. conn->status = STATUS_PROCESSING_DELETE;
  626. op_string = "delete";
  627. rc = ldap_delete_ext(conn->ld, dn, server_controls,
  628. NULL /* clientctls */, &msgid);
  629. break;
  630. case CONN_RENAME:
  631. conn->status = STATUS_PROCESSING_RENAME;
  632. op_string = "rename";
  633. rc = ldap_rename(conn->ld, dn, newrdn, newparent, deleteoldrdn,
  634. server_controls, NULL /* clientctls */, &msgid);
  635. break;
  636. case CONN_EXTENDED_OPERATION:
  637. conn->status = STATUS_PROCESSING_EXTENDED_OPERATION;
  638. op_string = "extended";
  639. rc = ldap_extended_operation(conn->ld, extop_oid, extop_payload,
  640. server_controls, NULL /* clientctls */, &msgid);
  641. }
  642. repl5_stop_debug_timeout(eqctx, &setlevel);
  643. if (LDAP_SUCCESS == rc)
  644. {
  645. /* DBDB: The code that used to be here has been moved for async operation
  646. * Results are now picked up in another thread. All we need to do here is
  647. * queue the operation details in the outstanding operation list.
  648. */
  649. return_value = CONN_OPERATION_SUCCESS;
  650. }
  651. else
  652. {
  653. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  654. "%s: Failed to send %s operation: LDAP error %d (%s)\n",
  655. agmt_get_long_name(conn->agmt),
  656. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  657. conn->last_ldap_error = rc;
  658. if (IS_DISCONNECT_ERROR(rc))
  659. {
  660. close_connection_internal(conn); /* already have the lock */
  661. return_value = CONN_NOT_CONNECTED;
  662. }
  663. else
  664. {
  665. conn->status = STATUS_CONNECTED;
  666. return_value = CONN_OPERATION_FAILED;
  667. }
  668. }
  669. }
  670. else
  671. {
  672. /* conn->last_ldap_error has been set to a more specific value
  673. * in the thread that did the disconnection
  674. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  675. */
  676. return_value = CONN_NOT_CONNECTED;
  677. }
  678. PR_Unlock(conn->lock); /* release the lock */
  679. if (message_id)
  680. {
  681. *message_id = msgid;
  682. }
  683. return return_value;
  684. }
  685. /*
  686. * Send an LDAP add operation.
  687. */
  688. ConnResult
  689. conn_send_add(Repl_Connection *conn, const char *dn, LDAPMod **attrs,
  690. LDAPControl *update_control, int *message_id)
  691. {
  692. return perform_operation(conn, CONN_ADD, dn, attrs, NULL /* newrdn */,
  693. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  694. NULL /* extop OID */, NULL /* extop payload */, message_id);
  695. }
  696. /*
  697. * Send an LDAP delete operation.
  698. */
  699. ConnResult
  700. conn_send_delete(Repl_Connection *conn, const char *dn,
  701. LDAPControl *update_control, int *message_id)
  702. {
  703. return perform_operation(conn, CONN_DELETE, dn, NULL /* attrs */,
  704. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  705. update_control, NULL /* extop OID */, NULL /* extop payload */, message_id);
  706. }
  707. /*
  708. * Send an LDAP modify operation.
  709. */
  710. ConnResult
  711. conn_send_modify(Repl_Connection *conn, const char *dn, LDAPMod **mods,
  712. LDAPControl *update_control, int *message_id)
  713. {
  714. return perform_operation(conn, CONN_MODIFY, dn, mods, NULL /* newrdn */,
  715. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  716. NULL /* extop OID */, NULL /* extop payload */, message_id);
  717. }
  718. /*
  719. * Send an LDAP moddn operation.
  720. */
  721. ConnResult
  722. conn_send_rename(Repl_Connection *conn, const char *dn,
  723. const char *newrdn, const char *newparent, int deleteoldrdn,
  724. LDAPControl *update_control, int *message_id)
  725. {
  726. return perform_operation(conn, CONN_RENAME, dn, NULL /* attrs */,
  727. newrdn, newparent, deleteoldrdn, update_control,
  728. NULL /* extop OID */, NULL /* extop payload */, message_id);
  729. }
  730. /*
  731. * Send an LDAP extended operation.
  732. */
  733. ConnResult
  734. conn_send_extended_operation(Repl_Connection *conn, const char *extop_oid,
  735. struct berval *payload,
  736. LDAPControl *update_control, int *message_id)
  737. {
  738. return perform_operation(conn, CONN_EXTENDED_OPERATION, NULL /* dn */, NULL /* attrs */,
  739. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  740. update_control, extop_oid, payload, message_id);
  741. }
  742. /*
  743. * Synchronously read an entry and return a specific attribute's values.
  744. * Returns CONN_OPERATION_SUCCESS if successful. Returns
  745. * CONN_OPERATION_FAILED if the operation was sent but an LDAP error
  746. * occurred (conn->last_ldap_error is set in this case), and
  747. * CONN_NOT_CONNECTED if no connection was active.
  748. *
  749. * The caller must free the returned_bvals.
  750. */
  751. ConnResult
  752. conn_read_entry_attribute(Repl_Connection *conn, const char *dn,
  753. char *type, struct berval ***returned_bvals)
  754. {
  755. ConnResult return_value;
  756. int ldap_rc;
  757. LDAPControl *server_controls[2];
  758. LDAPMessage *res = NULL;
  759. char *attrs[2];
  760. PR_ASSERT(NULL != type);
  761. if (conn_connected(conn))
  762. {
  763. server_controls[0] = &manageDSAITControl;
  764. server_controls[1] = NULL;
  765. attrs[0] = type;
  766. attrs[1] = NULL;
  767. ldap_rc = ldap_search_ext_s(conn->ld, dn, LDAP_SCOPE_BASE,
  768. "(objectclass=*)", attrs, 0 /* attrsonly */,
  769. server_controls, NULL /* client controls */,
  770. &conn->timeout, 0 /* sizelimit */, &res);
  771. if (LDAP_SUCCESS == ldap_rc)
  772. {
  773. LDAPMessage *entry = ldap_first_entry(conn->ld, res);
  774. if (NULL != entry)
  775. {
  776. *returned_bvals = ldap_get_values_len(conn->ld, entry, type);
  777. }
  778. return_value = CONN_OPERATION_SUCCESS;
  779. }
  780. else if (IS_DISCONNECT_ERROR(ldap_rc))
  781. {
  782. conn_disconnect(conn);
  783. return_value = CONN_NOT_CONNECTED;
  784. }
  785. else
  786. {
  787. return_value = CONN_OPERATION_FAILED;
  788. }
  789. conn->last_ldap_error = ldap_rc;
  790. if (NULL != res)
  791. {
  792. ldap_msgfree(res);
  793. res = NULL;
  794. }
  795. }
  796. else
  797. {
  798. return_value = CONN_NOT_CONNECTED;
  799. }
  800. return return_value;
  801. }
  802. /*
  803. * Return an pointer to a string describing the connection's status.
  804. */
  805. const char *
  806. conn_get_status(Repl_Connection *conn)
  807. {
  808. return conn->status;
  809. }
  810. /*
  811. * Cancel any outstanding linger timer. Should be called when
  812. * a replication session is beginning.
  813. */
  814. void
  815. conn_cancel_linger(Repl_Connection *conn)
  816. {
  817. PR_ASSERT(NULL != conn);
  818. PR_Lock(conn->lock);
  819. if (conn->linger_active)
  820. {
  821. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  822. "%s: Cancelling linger on the connection\n",
  823. agmt_get_long_name(conn->agmt));
  824. conn->linger_active = PR_FALSE;
  825. if (slapi_eq_cancel(conn->linger_event) == 1)
  826. {
  827. conn->refcnt--;
  828. }
  829. conn->linger_event = NULL;
  830. conn->status = STATUS_CONNECTED;
  831. }
  832. else
  833. {
  834. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  835. "%s: No linger to cancel on the connection\n",
  836. agmt_get_long_name(conn->agmt));
  837. }
  838. PR_Unlock(conn->lock);
  839. }
  840. /*
  841. * Called when our linger timeout timer expires. This means
  842. * we should check to see if perhaps the connection's become
  843. * active again, in which case we do nothing. Otherwise,
  844. * we close the connection.
  845. */
  846. static void
  847. linger_timeout(time_t event_time, void *arg)
  848. {
  849. PRBool delete_now;
  850. Repl_Connection *conn = (Repl_Connection *)arg;
  851. PR_ASSERT(NULL != conn);
  852. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  853. "%s: Linger timeout has expired on the connection\n",
  854. agmt_get_long_name(conn->agmt));
  855. PR_Lock(conn->lock);
  856. if (conn->linger_active)
  857. {
  858. conn->linger_active = PR_FALSE;
  859. conn->linger_event = NULL;
  860. close_connection_internal(conn);
  861. }
  862. delete_now = conn->delete_after_linger;
  863. PR_Unlock(conn->lock);
  864. if (delete_now)
  865. {
  866. conn_delete_internal(conn);
  867. }
  868. }
  869. /*
  870. * Indicate that a session is ending. The linger timer starts when
  871. * this function is called.
  872. */
  873. void
  874. conn_start_linger(Repl_Connection *conn)
  875. {
  876. time_t now;
  877. PR_ASSERT(NULL != conn);
  878. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  879. "%s: Beginning linger on the connection\n",
  880. agmt_get_long_name(conn->agmt));
  881. if (!conn_connected(conn))
  882. {
  883. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  884. "%s: No linger on the closed conn\n",
  885. agmt_get_long_name(conn->agmt));
  886. return;
  887. }
  888. time(&now);
  889. PR_Lock(conn->lock);
  890. if (conn->linger_active)
  891. {
  892. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  893. "%s: Linger already active on the connection\n",
  894. agmt_get_long_name(conn->agmt));
  895. }
  896. else
  897. {
  898. conn->linger_active = PR_TRUE;
  899. conn->linger_event = slapi_eq_once(linger_timeout, conn, now + conn->linger_time);
  900. conn->status = STATUS_LINGERING;
  901. }
  902. PR_Unlock(conn->lock);
  903. }
  904. /*
  905. * If no connection is currently active, opens a connection and binds to
  906. * the remote server. If a connection is open (e.g. lingering) then
  907. * this is a no-op.
  908. *
  909. * Returns CONN_OPERATION_SUCCESS on success, or CONN_OPERATION_FAILED
  910. * on failure. Sets conn->last_ldap_error and conn->last_operation;
  911. */
  912. ConnResult
  913. conn_connect(Repl_Connection *conn)
  914. {
  915. int optdata;
  916. int secure = 0;
  917. char* binddn = NULL;
  918. struct berval *creds;
  919. ConnResult return_value = CONN_OPERATION_SUCCESS;
  920. int pw_ret = 1;
  921. /** Connection already open just return SUCCESS **/
  922. if(conn->state == STATE_CONNECTED) return return_value;
  923. PR_Lock(conn->lock);
  924. if (conn->flag_agmt_changed) {
  925. /* So far we cannot change Hostname and Port */
  926. /* slapi_ch_free((void **)&conn->hostname); */
  927. /* conn->hostname = agmt_get_hostname(conn->agmt); */
  928. /* conn->port = agmt_get_port(conn->agmt); */
  929. slapi_ch_free((void **)&conn->binddn);
  930. conn->binddn = agmt_get_binddn(conn->agmt);
  931. conn->bindmethod = agmt_get_bindmethod(conn->agmt);
  932. conn->transport_flags = agmt_get_transport_flags(conn->agmt);
  933. conn->timeout.tv_sec = agmt_get_timeout(conn->agmt);
  934. conn->flag_agmt_changed = 0;
  935. slapi_ch_free((void **)&conn->plain);
  936. }
  937. PR_Unlock(conn->lock);
  938. creds = agmt_get_credentials(conn->agmt);
  939. if (conn->plain == NULL) {
  940. char *plain = NULL;
  941. /* kexcoff: for reversible encryption */
  942. /* We need to test the return code of pw_rever_decode in order to decide
  943. * if a free for plain will be needed (pw_ret == 0) or not (pw_ret != 0) */
  944. pw_ret = pw_rever_decode(creds->bv_val, &plain, type_nsds5ReplicaCredentials);
  945. /* Pb occured in decryption: stop now, binding will fail */
  946. if ( pw_ret == -1 )
  947. {
  948. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  949. "%s: Decoding of the credentials failed.\n",
  950. agmt_get_long_name(conn->agmt));
  951. return_value = CONN_OPERATION_FAILED;
  952. conn->last_ldap_error = LDAP_INVALID_CREDENTIALS;
  953. conn->state = STATE_DISCONNECTED;
  954. return (return_value);
  955. } /* Else, does not mean that the plain is correct, only means the we had no internal
  956. decoding pb */
  957. conn->plain = slapi_ch_strdup (plain);
  958. if (!pw_ret) slapi_ch_free((void**)&plain);
  959. }
  960. /* ugaston: if SSL has been selected in the replication agreement, SSL client
  961. * initialisation should be done before ever trying to open any connection at all.
  962. */
  963. if (conn->transport_flags == TRANSPORT_FLAG_TLS) {
  964. secure = 2;
  965. } else if (conn->transport_flags == TRANSPORT_FLAG_SSL) {
  966. secure = 1;
  967. }
  968. if (secure > 0) {
  969. if (!NSS_IsInitialized()) {
  970. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  971. "%s: SSL Not Initialized, Replication over SSL FAILED\n",
  972. agmt_get_long_name(conn->agmt));
  973. conn->last_ldap_error = LDAP_INAPPROPRIATE_AUTH;
  974. conn->last_operation = CONN_INIT;
  975. ber_bvfree(creds);
  976. creds = NULL;
  977. return CONN_SSL_NOT_ENABLED;
  978. }
  979. }
  980. if (return_value == CONN_OPERATION_SUCCESS) {
  981. #if !defined(USE_OPENLDAP)
  982. int io_timeout_ms;
  983. #endif
  984. /* Now we initialize the LDAP Structure and set options */
  985. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  986. "%s: Trying %s%s slapi_ldap_init_ext\n",
  987. agmt_get_long_name(conn->agmt),
  988. secure ? "secure" : "non-secure",
  989. (secure == 2) ? " startTLS" : "");
  990. /* shared = 1 because we will read results from a second thread */
  991. conn->ld = slapi_ldap_init_ext(NULL, conn->hostname, conn->port, secure, 1, NULL);
  992. if (NULL == conn->ld)
  993. {
  994. return_value = CONN_OPERATION_FAILED;
  995. conn->state = STATE_DISCONNECTED;
  996. conn->last_operation = CONN_INIT;
  997. conn->last_ldap_error = LDAP_LOCAL_ERROR;
  998. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  999. "%s: Failed to establish %s%sconnection to the consumer\n",
  1000. agmt_get_long_name(conn->agmt),
  1001. secure ? "secure " : "",
  1002. (secure == 2) ? "startTLS " : "");
  1003. ber_bvfree(creds);
  1004. creds = NULL;
  1005. return return_value;
  1006. }
  1007. /* slapi_ch_strdup is OK with NULL strings */
  1008. binddn = slapi_ch_strdup(conn->binddn);
  1009. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1010. "%s: binddn = %s, passwd = %s\n",
  1011. agmt_get_long_name(conn->agmt),
  1012. binddn?binddn:"NULL", creds->bv_val?creds->bv_val:"NULL");
  1013. /* Set some options for the connection. */
  1014. optdata = LDAP_DEREF_NEVER; /* Don't dereference aliases */
  1015. ldap_set_option(conn->ld, LDAP_OPT_DEREF, &optdata);
  1016. optdata = LDAP_VERSION3; /* We need LDAP version 3 */
  1017. ldap_set_option(conn->ld, LDAP_OPT_PROTOCOL_VERSION, &optdata);
  1018. /* Don't chase any referrals (although we shouldn't get any) */
  1019. ldap_set_option(conn->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1020. /* override the default timeout with the specified timeout */
  1021. #if defined(USE_OPENLDAP)
  1022. ldap_set_option(conn->ld, LDAP_OPT_NETWORK_TIMEOUT, &conn->timeout);
  1023. #else
  1024. io_timeout_ms = conn->timeout.tv_sec * 1000 + conn->timeout.tv_usec / 1000;
  1025. prldap_set_session_option(conn->ld, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT,
  1026. io_timeout_ms);
  1027. #endif
  1028. /* We've got an ld. Now bind to the server. */
  1029. conn->last_operation = CONN_BIND;
  1030. }
  1031. if ( bind_and_check_pwp(conn, binddn, conn->plain) == CONN_OPERATION_FAILED )
  1032. {
  1033. conn->last_ldap_error = slapi_ldap_get_lderrno (conn->ld, NULL, NULL);
  1034. conn->state = STATE_DISCONNECTED;
  1035. return_value = CONN_OPERATION_FAILED;
  1036. }
  1037. else
  1038. {
  1039. conn->last_ldap_error = LDAP_SUCCESS;
  1040. conn->state = STATE_CONNECTED;
  1041. return_value = CONN_OPERATION_SUCCESS;
  1042. }
  1043. ber_bvfree(creds);
  1044. creds = NULL;
  1045. slapi_ch_free((void**)&binddn);
  1046. if(return_value == CONN_OPERATION_FAILED)
  1047. {
  1048. close_connection_internal(conn);
  1049. } else
  1050. {
  1051. conn->last_ldap_error = LDAP_SUCCESS;
  1052. conn->state = STATE_CONNECTED;
  1053. }
  1054. return return_value;
  1055. }
  1056. static void
  1057. close_connection_internal(Repl_Connection *conn)
  1058. {
  1059. conn->state = STATE_DISCONNECTED;
  1060. conn->status = STATUS_DISCONNECTED;
  1061. conn->supports_ds50_repl = -1;
  1062. conn->supports_ds71_repl = -1;
  1063. /* do this last, to minimize the chance that another thread
  1064. might read conn->state as not disconnected and attempt
  1065. to use conn->ld */
  1066. if (NULL != conn->ld)
  1067. {
  1068. /* Since we call slapi_ldap_init,
  1069. we must call slapi_ldap_unbind */
  1070. slapi_ldap_unbind(conn->ld);
  1071. }
  1072. conn->ld = NULL;
  1073. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1074. "%s: Disconnected from the consumer\n", agmt_get_long_name(conn->agmt));
  1075. }
  1076. void
  1077. conn_disconnect(Repl_Connection *conn)
  1078. {
  1079. PR_ASSERT(NULL != conn);
  1080. PR_Lock(conn->lock);
  1081. close_connection_internal(conn);
  1082. PR_Unlock(conn->lock);
  1083. }
  1084. /*
  1085. * Determine if the remote replica supports DS 5.0 replication.
  1086. * Return codes:
  1087. * CONN_SUPPORTS_DS5_REPL - the remote replica suport DS5 replication
  1088. * CONN_DOES_NOT_SUPPORT_DS5_REPL - the remote replica does not
  1089. * support DS5 replication.
  1090. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1091. * replica supports DS5 replication.
  1092. * CONN_NOT_CONNECTED - no connection was active.
  1093. */
  1094. ConnResult
  1095. conn_replica_supports_ds5_repl(Repl_Connection *conn)
  1096. {
  1097. ConnResult return_value;
  1098. int ldap_rc;
  1099. if (conn_connected(conn))
  1100. {
  1101. if (conn->supports_ds50_repl == -1) {
  1102. LDAPMessage *res = NULL;
  1103. LDAPMessage *entry = NULL;
  1104. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1105. conn->status = STATUS_SEARCHING;
  1106. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1107. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1108. NULL /* server controls */, NULL /* client controls */,
  1109. &conn->timeout, LDAP_NO_LIMIT, &res);
  1110. if (LDAP_SUCCESS == ldap_rc)
  1111. {
  1112. conn->supports_ds50_repl = 0;
  1113. entry = ldap_first_entry(conn->ld, res);
  1114. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_NSDS50_UPDATE_INFO_CONTROL_OID))
  1115. {
  1116. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1117. }
  1118. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS50_REPLICATION_REQUEST_OID))
  1119. {
  1120. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1121. }
  1122. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_END_NSDS50_REPLICATION_REQUEST_OID))
  1123. {
  1124. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1125. }
  1126. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID))
  1127. {
  1128. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1129. }
  1130. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_RESPONSE_OID))
  1131. {
  1132. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1133. }
  1134. else
  1135. {
  1136. conn->supports_ds50_repl = 1;
  1137. return_value = CONN_SUPPORTS_DS5_REPL;
  1138. }
  1139. }
  1140. else
  1141. {
  1142. if (IS_DISCONNECT_ERROR(ldap_rc))
  1143. {
  1144. conn->last_ldap_error = ldap_rc; /* specific reason */
  1145. conn_disconnect(conn);
  1146. return_value = CONN_NOT_CONNECTED;
  1147. }
  1148. else
  1149. {
  1150. return_value = CONN_OPERATION_FAILED;
  1151. }
  1152. }
  1153. if (NULL != res)
  1154. ldap_msgfree(res);
  1155. }
  1156. else {
  1157. return_value = conn->supports_ds50_repl ? CONN_SUPPORTS_DS5_REPL : CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1158. }
  1159. }
  1160. else
  1161. {
  1162. /* Not connected */
  1163. return_value = CONN_NOT_CONNECTED;
  1164. }
  1165. return return_value;
  1166. }
  1167. /*
  1168. * Determine if the remote replica supports DS 5.0 replication.
  1169. * Return codes:
  1170. * CONN_SUPPORTS_DS71_REPL - the remote replica suport DS5 replication
  1171. * CONN_DOES_NOT_SUPPORT_DS71_REPL - the remote replica does not
  1172. * support DS5 replication.
  1173. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1174. * replica supports DS5 replication.
  1175. * CONN_NOT_CONNECTED - no connection was active.
  1176. */
  1177. ConnResult
  1178. conn_replica_supports_ds71_repl(Repl_Connection *conn)
  1179. {
  1180. ConnResult return_value;
  1181. int ldap_rc;
  1182. if (conn_connected(conn))
  1183. {
  1184. if (conn->supports_ds71_repl == -1) {
  1185. LDAPMessage *res = NULL;
  1186. LDAPMessage *entry = NULL;
  1187. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1188. conn->status = STATUS_SEARCHING;
  1189. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1190. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1191. NULL /* server controls */, NULL /* client controls */,
  1192. &conn->timeout, LDAP_NO_LIMIT, &res);
  1193. if (LDAP_SUCCESS == ldap_rc)
  1194. {
  1195. conn->supports_ds71_repl = 0;
  1196. entry = ldap_first_entry(conn->ld, res);
  1197. if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS71_REPLICATION_ENTRY_REQUEST_OID))
  1198. {
  1199. return_value = CONN_DOES_NOT_SUPPORT_DS71_REPL;
  1200. }
  1201. else
  1202. {
  1203. conn->supports_ds71_repl = 1;
  1204. return_value = CONN_SUPPORTS_DS71_REPL;
  1205. }
  1206. }
  1207. else
  1208. {
  1209. if (IS_DISCONNECT_ERROR(ldap_rc))
  1210. {
  1211. conn->last_ldap_error = ldap_rc; /* specific reason */
  1212. conn_disconnect(conn);
  1213. return_value = CONN_NOT_CONNECTED;
  1214. }
  1215. else
  1216. {
  1217. return_value = CONN_OPERATION_FAILED;
  1218. }
  1219. }
  1220. if (NULL != res)
  1221. ldap_msgfree(res);
  1222. }
  1223. else {
  1224. return_value = conn->supports_ds71_repl ? CONN_SUPPORTS_DS71_REPL : CONN_DOES_NOT_SUPPORT_DS71_REPL;
  1225. }
  1226. }
  1227. else
  1228. {
  1229. /* Not connected */
  1230. return_value = CONN_NOT_CONNECTED;
  1231. }
  1232. return return_value;
  1233. }
  1234. /* Determine if the replica is read-only */
  1235. ConnResult
  1236. conn_replica_is_readonly(Repl_Connection *conn)
  1237. {
  1238. ReplicaId rid = agmt_get_consumer_rid( (Repl_Agmt *) conn->agmt, conn );
  1239. if (rid == READ_ONLY_REPLICA_ID)
  1240. {
  1241. return CONN_IS_READONLY;
  1242. } else
  1243. {
  1244. return CONN_IS_NOT_READONLY;
  1245. }
  1246. }
  1247. /*
  1248. * Return 1 if "value" is a value of attribute type "type" in entry "entry".
  1249. * Otherwise, return 0.
  1250. */
  1251. static int
  1252. attribute_string_value_present(LDAP *ld, LDAPMessage *entry, const char *type,
  1253. const char *value)
  1254. {
  1255. int return_value = 0;
  1256. ber_len_t vallen;
  1257. if (NULL != entry)
  1258. {
  1259. char *atype = NULL;
  1260. BerElement *ber = NULL;
  1261. vallen = strlen(value);
  1262. atype = ldap_first_attribute(ld, entry, &ber);
  1263. while (NULL != atype && 0 == return_value)
  1264. {
  1265. if (strcasecmp(atype, type) == 0)
  1266. {
  1267. struct berval **vals = ldap_get_values_len(ld, entry, atype);
  1268. int i;
  1269. for (i = 0; return_value == 0 && NULL != vals && NULL != vals[i]; i++)
  1270. {
  1271. if ((vallen == vals[i]->bv_len) && !strncmp(vals[i]->bv_val, value, vallen))
  1272. {
  1273. return_value = 1;
  1274. }
  1275. }
  1276. if (NULL != vals)
  1277. {
  1278. ldap_value_free_len(vals);
  1279. }
  1280. }
  1281. ldap_memfree(atype);
  1282. atype = ldap_next_attribute(ld, entry, ber);
  1283. }
  1284. if (NULL != ber)
  1285. ber_free(ber, 0);
  1286. /* The last atype has not been freed yet */
  1287. if (NULL != atype)
  1288. ldap_memfree(atype);
  1289. }
  1290. return return_value;
  1291. }
  1292. /*
  1293. * Read the remote server's schema entry, then read the local schema entry,
  1294. * and compare the nsschemacsn attribute. If the local csn is newer, or
  1295. * the remote csn is absent, push the schema down to the consumer.
  1296. * Return codes:
  1297. * CONN_SCHEMA_UPDATED if the schema was pushed successfully
  1298. * CONN_SCHEMA_NO_UPDATE_NEEDED if the schema was as new or newer than
  1299. * the local server's schema
  1300. * CONN_OPERATION_FAILED if an error occurred
  1301. * CONN_NOT_CONNECTED if no connection was active
  1302. * NOTE: Should only be called when a replication session has been
  1303. * established by sending a startReplication extended operation.
  1304. */
  1305. ConnResult
  1306. conn_push_schema(Repl_Connection *conn, CSN **remotecsn)
  1307. {
  1308. ConnResult return_value = CONN_OPERATION_SUCCESS;
  1309. char *nsschemacsn = "nsschemacsn";
  1310. Slapi_Entry **entries = NULL;
  1311. Slapi_Entry *schema_entry = NULL;
  1312. CSN *localcsn = NULL;
  1313. Slapi_PBlock *spb = NULL;
  1314. char localcsnstr[CSN_STRSIZE + 1] = {0};
  1315. if (!conn_connected(conn))
  1316. {
  1317. return_value = CONN_NOT_CONNECTED;
  1318. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1319. "%s: Schema replication update failed: not connected to consumer\n",
  1320. agmt_get_long_name(conn->agmt));
  1321. }
  1322. else
  1323. {
  1324. localcsn = dup_global_schema_csn();
  1325. if (NULL == localcsn)
  1326. {
  1327. /* Local server has epoch CSN, so don't push schema */
  1328. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1329. }
  1330. else if ( remotecsn && *remotecsn && csn_compare(localcsn, *remotecsn) <= 0 )
  1331. {
  1332. /* Local server schema is not newer than the remote one */
  1333. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1334. }
  1335. else
  1336. {
  1337. struct berval **remote_schema_csn_bervals = NULL;
  1338. /* Get remote server's schema */
  1339. return_value = conn_read_entry_attribute(conn, "cn=schema", nsschemacsn,
  1340. &remote_schema_csn_bervals);
  1341. if (CONN_OPERATION_SUCCESS == return_value)
  1342. {
  1343. if (NULL != remote_schema_csn_bervals && NULL != remote_schema_csn_bervals[0])
  1344. {
  1345. char remotecsnstr[CSN_STRSIZE + 1] = {0};
  1346. memcpy(remotecsnstr, remote_schema_csn_bervals[0]->bv_val,
  1347. remote_schema_csn_bervals[0]->bv_len);
  1348. remotecsnstr[remote_schema_csn_bervals[0]->bv_len] = '\0';
  1349. *remotecsn = csn_new_by_string(remotecsnstr);
  1350. if (NULL != remotecsn && (csn_compare(localcsn, *remotecsn) <= 0))
  1351. {
  1352. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1353. }
  1354. /* Need to free the remote_schema_csn_bervals */
  1355. ber_bvecfree(remote_schema_csn_bervals);
  1356. }
  1357. }
  1358. }
  1359. }
  1360. if (CONN_OPERATION_SUCCESS == return_value)
  1361. {
  1362. /* We know we need to push the schema out. */
  1363. LDAPMod ocmod = {0};
  1364. LDAPMod atmod = {0};
  1365. LDAPMod csnmod = {0};
  1366. LDAPMod *attrs[4] = {0};
  1367. int numvalues = 0;
  1368. Slapi_Attr *attr = NULL;
  1369. char *csnvalues[2];
  1370. ocmod.mod_type = "objectclasses";
  1371. ocmod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1372. ocmod.mod_bvalues = NULL;
  1373. atmod.mod_type = "attributetypes";
  1374. atmod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1375. atmod.mod_bvalues = NULL;
  1376. csnmod.mod_type = nsschemacsn;
  1377. csnmod.mod_op = LDAP_MOD_REPLACE;
  1378. csn_as_string (localcsn, PR_FALSE, localcsnstr);
  1379. csnvalues[0] = localcsnstr;
  1380. csnvalues[1] = NULL;
  1381. csnmod.mod_values = csnvalues;
  1382. attrs[0] = &ocmod;
  1383. attrs[1] = &atmod;
  1384. attrs[2] = &csnmod;
  1385. attrs[3] = NULL;
  1386. return_value = CONN_OPERATION_FAILED; /* assume failure */
  1387. /* Get local schema */
  1388. spb = slapi_search_internal("cn=schema", LDAP_SCOPE_BASE, "(objectclass=*)",
  1389. NULL /* controls */, NULL /* schema_csn_attrs */, 0 /* attrsonly */);
  1390. slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1391. if (NULL == entries || NULL == entries[0])
  1392. {
  1393. /* Whoops - couldn't read our own schema! */
  1394. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1395. "%s: Error: unable to read local schema definitions.\n",
  1396. agmt_get_long_name(conn->agmt));
  1397. return_value = CONN_OPERATION_FAILED;
  1398. }
  1399. else
  1400. {
  1401. schema_entry = entries[0];
  1402. if (slapi_entry_attr_find(schema_entry, "objectclasses", &attr) != -1)
  1403. {
  1404. int i, ind;
  1405. Slapi_Value *value;
  1406. slapi_attr_get_numvalues(attr, &numvalues);
  1407. ocmod.mod_bvalues = (struct berval **)slapi_ch_malloc((numvalues + 1) *
  1408. sizeof(struct berval *));
  1409. for (i = 0, ind = slapi_attr_first_value(attr, &value);
  1410. ind != -1; ind = slapi_attr_next_value(attr, ind, &value), i++)
  1411. {
  1412. /* XXXggood had to cast away const below */
  1413. ocmod.mod_bvalues[i] = (struct berval *)slapi_value_get_berval(value);
  1414. }
  1415. ocmod.mod_bvalues[numvalues] = NULL;
  1416. if (slapi_entry_attr_find(schema_entry, "attributetypes", &attr) != -1)
  1417. {
  1418. ConnResult result;
  1419. slapi_attr_get_numvalues(attr, &numvalues);
  1420. atmod.mod_bvalues = (struct berval **)slapi_ch_malloc((numvalues + 1) *
  1421. sizeof(struct berval *));
  1422. for (i = 0, ind = slapi_attr_first_value(attr, &value);
  1423. ind != -1; ind = slapi_attr_next_value(attr, ind, &value), i++)
  1424. {
  1425. /* XXXggood had to cast away const below */
  1426. atmod.mod_bvalues[i] = (struct berval *)slapi_value_get_berval(value);
  1427. }
  1428. atmod.mod_bvalues[numvalues] = NULL;
  1429. result = conn_send_modify(conn, "cn=schema", attrs, NULL, NULL); /* DBDB: this needs to be fixed to use async */
  1430. result = conn_read_result(conn,NULL);
  1431. switch (result)
  1432. {
  1433. case CONN_OPERATION_FAILED:
  1434. {
  1435. int ldaperr = -1, optype = -1;
  1436. conn_get_error(conn, &optype, &ldaperr);
  1437. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1438. "%s: Schema replication update failed: %s\n",
  1439. agmt_get_long_name(conn->agmt),
  1440. ldaperr == -1 ? "Unknown Error" : ldap_err2string(ldaperr));
  1441. return_value = CONN_OPERATION_FAILED;
  1442. break;
  1443. }
  1444. case CONN_NOT_CONNECTED:
  1445. return_value = CONN_NOT_CONNECTED;
  1446. break;
  1447. case CONN_OPERATION_SUCCESS:
  1448. return_value = CONN_SCHEMA_UPDATED;
  1449. break;
  1450. default:
  1451. break;
  1452. }
  1453. }
  1454. }
  1455. else
  1456. {
  1457. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1458. "%s: Schema replication update failed: "
  1459. "unable to prepare schema entry for transmission.\n",
  1460. agmt_get_long_name(conn->agmt));
  1461. }
  1462. }
  1463. /* slapi_ch_free accepts NULL pointer */
  1464. slapi_ch_free((void **)&ocmod.mod_bvalues);
  1465. slapi_ch_free((void **)&atmod.mod_bvalues);
  1466. }
  1467. if (NULL != spb)
  1468. {
  1469. slapi_free_search_results_internal(spb);
  1470. slapi_pblock_destroy(spb);
  1471. spb = NULL;
  1472. }
  1473. if (NULL != localcsn)
  1474. {
  1475. csn_free(&localcsn);
  1476. }
  1477. return return_value;
  1478. }
  1479. void
  1480. conn_set_timeout(Repl_Connection *conn, long timeout)
  1481. {
  1482. PR_ASSERT(NULL != conn);
  1483. PR_ASSERT(timeout >= 0);
  1484. PR_Lock(conn->lock);
  1485. conn->timeout.tv_sec = timeout;
  1486. PR_Unlock(conn->lock);
  1487. }
  1488. long
  1489. conn_get_timeout(Repl_Connection *conn)
  1490. {
  1491. long retval = 0;
  1492. PR_ASSERT(NULL != conn);
  1493. retval = conn->timeout.tv_sec;
  1494. return retval;
  1495. }
  1496. void conn_set_agmt_changed(Repl_Connection *conn)
  1497. {
  1498. PR_ASSERT(NULL != conn);
  1499. PR_Lock(conn->lock);
  1500. if (NULL != conn->agmt)
  1501. conn->flag_agmt_changed = 1;
  1502. PR_Unlock(conn->lock);
  1503. }
  1504. static const char *
  1505. bind_method_to_mech(int bindmethod)
  1506. {
  1507. switch (bindmethod) {
  1508. case BINDMETHOD_SSL_CLIENTAUTH:
  1509. return LDAP_SASL_EXTERNAL;
  1510. break;
  1511. case BINDMETHOD_SASL_GSSAPI:
  1512. return "GSSAPI";
  1513. break;
  1514. case BINDMETHOD_SASL_DIGEST_MD5:
  1515. return "DIGEST-MD5";
  1516. break;
  1517. default: /* anything else */
  1518. return LDAP_SASL_SIMPLE;
  1519. }
  1520. return LDAP_SASL_SIMPLE;
  1521. }
  1522. /*
  1523. * Check the result of an ldap BIND operation to see we it
  1524. * contains the expiration controls
  1525. * return: -1 error, not bound
  1526. * 0, OK bind has succeeded
  1527. */
  1528. static int
  1529. bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password)
  1530. {
  1531. LDAPControl **ctrls = NULL;
  1532. LDAP *ld = conn->ld;
  1533. int rc;
  1534. const char *mech = bind_method_to_mech(conn->bindmethod);
  1535. rc = slapi_ldap_bind(conn->ld, binddn, password, mech, NULL,
  1536. &ctrls, NULL, NULL);
  1537. if ( rc == LDAP_SUCCESS )
  1538. {
  1539. if (conn->last_ldap_error != rc)
  1540. {
  1541. conn->last_ldap_error = rc;
  1542. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1543. "%s: Replication bind with %s auth resumed\n",
  1544. agmt_get_long_name(conn->agmt),
  1545. mech ? mech : "SIMPLE");
  1546. }
  1547. if ( ctrls )
  1548. {
  1549. int i;
  1550. for( i = 0; ctrls[ i ] != NULL; ++i )
  1551. {
  1552. if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRED)) )
  1553. {
  1554. /* Bind is successfull but password has expired */
  1555. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1556. "%s: Succesfully bound %s to consumer, "
  1557. "but password has expired on consumer.\n",
  1558. agmt_get_long_name(conn->agmt), binddn);
  1559. }
  1560. else if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRING)) )
  1561. {
  1562. /* The password is expiring in n seconds */
  1563. if ( (ctrls[ i ]->ldctl_value.bv_val != NULL) &&
  1564. (ctrls[ i ]->ldctl_value.bv_len > 0) )
  1565. {
  1566. int password_expiring = atoi( ctrls[ i ]->ldctl_value.bv_val );
  1567. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1568. "%s: Succesfully bound %s to consumer, "
  1569. "but password is expiring on consumer in %d seconds.\n",
  1570. agmt_get_long_name(conn->agmt), binddn, password_expiring);
  1571. }
  1572. }
  1573. }
  1574. ldap_controls_free( ctrls );
  1575. }
  1576. return (CONN_OPERATION_SUCCESS);
  1577. }
  1578. else
  1579. {
  1580. ldap_controls_free( ctrls );
  1581. /* Do not report the same error over and over again */
  1582. if (conn->last_ldap_error != rc)
  1583. {
  1584. char *errmsg = NULL;
  1585. conn->last_ldap_error = rc;
  1586. /* errmsg is a pointer directly into the ld structure - do not free */
  1587. rc = slapi_ldap_get_lderrno( ld, NULL, &errmsg );
  1588. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1589. "%s: Replication bind with %s auth failed: LDAP error %d (%s) (%s)\n",
  1590. agmt_get_long_name(conn->agmt),
  1591. mech ? mech : "SIMPLE", rc,
  1592. ldap_err2string(rc), errmsg);
  1593. }
  1594. return (CONN_OPERATION_FAILED);
  1595. }
  1596. }
  1597. void
  1598. repl5_set_debug_timeout(const char *val)
  1599. {
  1600. /* val looks like this: seconds[:debuglevel] */
  1601. /* seconds is the number of seconds to wait until turning on the debug level */
  1602. /* this should be less than the ldap connection timeout (default 10 minutes) */
  1603. /* the optional debug level is the error log debugging level to use (default repl) */
  1604. if (val) {
  1605. const char *p = strchr(val, ':');
  1606. s_debug_timeout = atoi(val);
  1607. if (p) {
  1608. s_debug_level = atoi(p+1);
  1609. } else {
  1610. s_debug_level = 8192;
  1611. }
  1612. }
  1613. }
  1614. #ifdef FOR_DEBUGGING
  1615. static time_t
  1616. PRTime2time_t (PRTime tm)
  1617. {
  1618. PRInt64 rt;
  1619. PR_ASSERT (tm);
  1620. LL_DIV(rt, tm, PR_USEC_PER_SEC);
  1621. return (time_t)rt;
  1622. }
  1623. #endif
  1624. static Slapi_Eq_Context
  1625. repl5_start_debug_timeout(int *setlevel)
  1626. {
  1627. Slapi_Eq_Context eqctx = 0;
  1628. if (s_debug_timeout && s_debug_level) {
  1629. time_t now = time(NULL);
  1630. eqctx = slapi_eq_once(repl5_debug_timeout_callback, setlevel,
  1631. s_debug_timeout + now);
  1632. }
  1633. return eqctx;
  1634. }
  1635. static void
  1636. repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel)
  1637. {
  1638. char buf[20];
  1639. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1640. if (eqctx && !*setlevel) {
  1641. (void)slapi_eq_cancel(eqctx);
  1642. }
  1643. if (s_debug_timeout && s_debug_level && *setlevel) {
  1644. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  1645. sprintf(buf, "%d", 0);
  1646. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1647. }
  1648. }
  1649. static void
  1650. repl5_debug_timeout_callback(time_t when, void *arg)
  1651. {
  1652. int *setlevel = (int *)arg;
  1653. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  1654. char buf[20];
  1655. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1656. *setlevel = 1;
  1657. sprintf(buf, "%d", s_debug_level);
  1658. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1659. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1660. "repl5_debug_timeout_callback: set debug level to %d at %ld\n",
  1661. s_debug_level, when);
  1662. }