repl5_connection.c 50 KB

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