repl5_connection.c 50 KB

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