repl5_connection.c 50 KB

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