repl5_connection.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  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. int msgid = 0;
  437. server_controls[0] = &manageDSAITControl;
  438. server_controls[1] = update_control;
  439. server_controls[2] = NULL;
  440. if (conn_connected(conn))
  441. {
  442. int setlevel = 0;
  443. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  444. /* Because the SDK isn't really thread-safe (it can deadlock between
  445. * a thread sending an operation and a thread trying to retrieve a response
  446. * on the same connection), we need to _first_ verify that the connection
  447. * is writable. If it isn't, we can deadlock if we proceed any further...
  448. */
  449. {
  450. struct PRPollDesc pr_pd;
  451. PRIntervalTime timeout = PR_SecondsToInterval(conn->timeout.tv_sec);
  452. PRFileDesc *prfd = NULL;
  453. struct lextiof_socket_private *socketargp = NULL;
  454. PRLDAPSocketInfo soi;
  455. if ( (rc = ldap_get_option(conn->ld, LDAP_X_OPT_SOCKETARG, &socketargp)) != LDAP_SUCCESS)
  456. {
  457. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  458. "%s: Failed call to ldap_get_option in perform_operation: LDAP error %d (%s)\n",
  459. agmt_get_long_name(conn->agmt),
  460. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  461. conn->last_ldap_error = rc;
  462. return CONN_OPERATION_FAILED;
  463. }
  464. memset( &soi, 0, sizeof(soi));
  465. soi.soinfo_size = PRLDAP_SOCKETINFO_SIZE;
  466. if (LDAP_SUCCESS != (rc = prldap_get_socket_info(0, socketargp, &soi)))
  467. {
  468. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  469. "%s: Failed call to prldap_get_socket_info in perform_operation: LDAP error %d (%s)\n",
  470. agmt_get_long_name(conn->agmt),
  471. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  472. conn->last_ldap_error = rc;
  473. return CONN_OPERATION_FAILED;
  474. }
  475. prfd = soi.soinfo_prfd;
  476. /* Before we connect, the prfd can be null */
  477. if (prfd)
  478. {
  479. pr_pd.fd = prfd;
  480. pr_pd.in_flags = PR_POLL_WRITE;
  481. pr_pd.out_flags = 0;
  482. rc = PR_Poll(&pr_pd, 1, timeout);
  483. /* Did we time out ? */
  484. if (rc == 0)
  485. {
  486. return CONN_TIMEOUT;
  487. }
  488. }
  489. }
  490. conn->last_operation = optype;
  491. switch (optype)
  492. {
  493. case CONN_ADD:
  494. conn->status = STATUS_PROCESSING_ADD;
  495. op_string = "add";
  496. rc = ldap_add_ext(conn->ld, dn, attrs, server_controls,
  497. NULL /* clientctls */, &msgid);
  498. break;
  499. case CONN_MODIFY:
  500. conn->status = STATUS_PROCESSING_MODIFY;
  501. op_string = "modify";
  502. rc = ldap_modify_ext(conn->ld, dn, attrs, server_controls,
  503. NULL /* clientctls */, &msgid);
  504. break;
  505. case CONN_DELETE:
  506. conn->status = STATUS_PROCESSING_DELETE;
  507. op_string = "delete";
  508. rc = ldap_delete_ext(conn->ld, dn, server_controls,
  509. NULL /* clientctls */, &msgid);
  510. break;
  511. case CONN_RENAME:
  512. conn->status = STATUS_PROCESSING_RENAME;
  513. op_string = "rename";
  514. rc = ldap_rename(conn->ld, dn, newrdn, newparent, deleteoldrdn,
  515. server_controls, NULL /* clientctls */, &msgid);
  516. break;
  517. case CONN_EXTENDED_OPERATION:
  518. conn->status = STATUS_PROCESSING_EXTENDED_OPERATION;
  519. op_string = "extended";
  520. rc = ldap_extended_operation(conn->ld, extop_oid, extop_payload,
  521. server_controls, NULL /* clientctls */, &msgid);
  522. }
  523. repl5_stop_debug_timeout(eqctx, &setlevel);
  524. if (LDAP_SUCCESS == rc)
  525. {
  526. /* DBDB: The code that used to be here has been moved for async operation
  527. * Results are now picked up in another thread. All we need to do here is
  528. * queue the operation details in the outstanding operation list.
  529. */
  530. return_value = CONN_OPERATION_SUCCESS;
  531. }
  532. else
  533. {
  534. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  535. "%s: Failed to send %s operation: LDAP error %d (%s)\n",
  536. agmt_get_long_name(conn->agmt),
  537. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  538. conn->last_ldap_error = rc;
  539. if (IS_DISCONNECT_ERROR(rc))
  540. {
  541. conn_disconnect(conn);
  542. return_value = CONN_NOT_CONNECTED;
  543. }
  544. else
  545. {
  546. conn->status = STATUS_CONNECTED;
  547. return_value = CONN_OPERATION_FAILED;
  548. }
  549. }
  550. }
  551. else
  552. {
  553. /* conn->last_ldap_error has been set to a more specific value
  554. * in conn_connected()
  555. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  556. */
  557. return_value = CONN_NOT_CONNECTED;
  558. }
  559. if (message_id)
  560. {
  561. *message_id = msgid;
  562. }
  563. return return_value;
  564. }
  565. /*
  566. * Send an LDAP add operation.
  567. */
  568. ConnResult
  569. conn_send_add(Repl_Connection *conn, const char *dn, LDAPMod **attrs,
  570. LDAPControl *update_control, int *message_id)
  571. {
  572. return perform_operation(conn, CONN_ADD, dn, attrs, NULL /* newrdn */,
  573. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  574. NULL /* extop OID */, NULL /* extop payload */, message_id);
  575. }
  576. /*
  577. * Send an LDAP delete operation.
  578. */
  579. ConnResult
  580. conn_send_delete(Repl_Connection *conn, const char *dn,
  581. LDAPControl *update_control, int *message_id)
  582. {
  583. return perform_operation(conn, CONN_DELETE, dn, NULL /* attrs */,
  584. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  585. update_control, NULL /* extop OID */, NULL /* extop payload */, message_id);
  586. }
  587. /*
  588. * Send an LDAP modify operation.
  589. */
  590. ConnResult
  591. conn_send_modify(Repl_Connection *conn, const char *dn, LDAPMod **mods,
  592. LDAPControl *update_control, int *message_id)
  593. {
  594. return perform_operation(conn, CONN_MODIFY, dn, mods, NULL /* newrdn */,
  595. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  596. NULL /* extop OID */, NULL /* extop payload */, message_id);
  597. }
  598. /*
  599. * Send an LDAP moddn operation.
  600. */
  601. ConnResult
  602. conn_send_rename(Repl_Connection *conn, const char *dn,
  603. const char *newrdn, const char *newparent, int deleteoldrdn,
  604. LDAPControl *update_control, int *message_id)
  605. {
  606. return perform_operation(conn, CONN_RENAME, dn, NULL /* attrs */,
  607. newrdn, newparent, deleteoldrdn, update_control,
  608. NULL /* extop OID */, NULL /* extop payload */, message_id);
  609. }
  610. /*
  611. * Send an LDAP extended operation.
  612. */
  613. ConnResult
  614. conn_send_extended_operation(Repl_Connection *conn, const char *extop_oid,
  615. struct berval *payload,
  616. LDAPControl *update_control, int *message_id)
  617. {
  618. return perform_operation(conn, CONN_EXTENDED_OPERATION, NULL /* dn */, NULL /* attrs */,
  619. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  620. update_control, extop_oid, payload, message_id);
  621. }
  622. /*
  623. * Synchronously read an entry and return a specific attribute's values.
  624. * Returns CONN_OPERATION_SUCCESS if successful. Returns
  625. * CONN_OPERATION_FAILED if the operation was sent but an LDAP error
  626. * occurred (conn->last_ldap_error is set in this case), and
  627. * CONN_NOT_CONNECTED if no connection was active.
  628. *
  629. * The caller must free the returned_bvals.
  630. */
  631. ConnResult
  632. conn_read_entry_attribute(Repl_Connection *conn, const char *dn,
  633. char *type, struct berval ***returned_bvals)
  634. {
  635. ConnResult return_value;
  636. int ldap_rc;
  637. LDAPControl *server_controls[2];
  638. LDAPMessage *res = NULL;
  639. char *attrs[2];
  640. PR_ASSERT(NULL != type);
  641. if (conn_connected(conn))
  642. {
  643. server_controls[0] = &manageDSAITControl;
  644. server_controls[1] = NULL;
  645. attrs[0] = type;
  646. attrs[1] = NULL;
  647. ldap_rc = ldap_search_ext_s(conn->ld, dn, LDAP_SCOPE_BASE,
  648. "(objectclass=*)", attrs, 0 /* attrsonly */,
  649. server_controls, NULL /* client controls */,
  650. &conn->timeout, 0 /* sizelimit */, &res);
  651. if (LDAP_SUCCESS == ldap_rc)
  652. {
  653. LDAPMessage *entry = ldap_first_entry(conn->ld, res);
  654. if (NULL != entry)
  655. {
  656. *returned_bvals = ldap_get_values_len(conn->ld, entry, type);
  657. }
  658. return_value = CONN_OPERATION_SUCCESS;
  659. }
  660. else if (IS_DISCONNECT_ERROR(ldap_rc))
  661. {
  662. conn_disconnect(conn);
  663. return_value = CONN_NOT_CONNECTED;
  664. }
  665. else
  666. {
  667. return_value = CONN_OPERATION_FAILED;
  668. }
  669. conn->last_ldap_error = ldap_rc;
  670. if (NULL != res)
  671. {
  672. ldap_msgfree(res);
  673. res = NULL;
  674. }
  675. }
  676. else
  677. {
  678. return_value = CONN_NOT_CONNECTED;
  679. }
  680. return return_value;
  681. }
  682. /*
  683. * Return an pointer to a string describing the connection's status.
  684. */
  685. const char *
  686. conn_get_status(Repl_Connection *conn)
  687. {
  688. return conn->status;
  689. }
  690. /*
  691. * Cancel any outstanding linger timer. Should be called when
  692. * a replication session is beginning.
  693. */
  694. void
  695. conn_cancel_linger(Repl_Connection *conn)
  696. {
  697. PR_ASSERT(NULL != conn);
  698. PR_Lock(conn->lock);
  699. if (conn->linger_active)
  700. {
  701. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  702. "%s: Cancelling linger on the connection\n",
  703. agmt_get_long_name(conn->agmt));
  704. conn->linger_active = PR_FALSE;
  705. if (slapi_eq_cancel(conn->linger_event) == 1)
  706. {
  707. conn->refcnt--;
  708. }
  709. conn->linger_event = NULL;
  710. conn->status = STATUS_CONNECTED;
  711. }
  712. else
  713. {
  714. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  715. "%s: No linger to cancel on the connection\n",
  716. agmt_get_long_name(conn->agmt));
  717. }
  718. PR_Unlock(conn->lock);
  719. }
  720. /*
  721. * Called when our linger timeout timer expires. This means
  722. * we should check to see if perhaps the connection's become
  723. * active again, in which case we do nothing. Otherwise,
  724. * we close the connection.
  725. */
  726. static void
  727. linger_timeout(time_t event_time, void *arg)
  728. {
  729. PRBool delete_now;
  730. Repl_Connection *conn = (Repl_Connection *)arg;
  731. PR_ASSERT(NULL != conn);
  732. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  733. "%s: Linger timeout has expired on the connection\n",
  734. agmt_get_long_name(conn->agmt));
  735. PR_Lock(conn->lock);
  736. if (conn->linger_active)
  737. {
  738. conn->linger_active = PR_FALSE;
  739. conn->linger_event = NULL;
  740. close_connection_internal(conn);
  741. }
  742. delete_now = conn->delete_after_linger;
  743. PR_Unlock(conn->lock);
  744. if (delete_now)
  745. {
  746. conn_delete_internal(conn);
  747. }
  748. }
  749. /*
  750. * Indicate that a session is ending. The linger timer starts when
  751. * this function is called.
  752. */
  753. void
  754. conn_start_linger(Repl_Connection *conn)
  755. {
  756. time_t now;
  757. PR_ASSERT(NULL != conn);
  758. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  759. "%s: Beginning linger on the connection\n",
  760. agmt_get_long_name(conn->agmt));
  761. if (!conn_connected(conn))
  762. {
  763. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  764. "%s: No linger on the closed conn\n",
  765. agmt_get_long_name(conn->agmt));
  766. return;
  767. }
  768. time(&now);
  769. PR_Lock(conn->lock);
  770. if (conn->linger_active)
  771. {
  772. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  773. "%s: Linger already active on the connection\n",
  774. agmt_get_long_name(conn->agmt));
  775. }
  776. else
  777. {
  778. conn->linger_active = PR_TRUE;
  779. conn->linger_event = slapi_eq_once(linger_timeout, conn, now + conn->linger_time);
  780. conn->status = STATUS_LINGERING;
  781. }
  782. PR_Unlock(conn->lock);
  783. }
  784. /*
  785. * If no connection is currently active, opens a connection and binds to
  786. * the remote server. If a connection is open (e.g. lingering) then
  787. * this is a no-op.
  788. *
  789. * Returns CONN_OPERATION_SUCCESS on success, or CONN_OPERATION_FAILED
  790. * on failure. Sets conn->last_ldap_error and conn->last_operation;
  791. */
  792. ConnResult
  793. conn_connect(Repl_Connection *conn)
  794. {
  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_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_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. #ifdef FOR_DEBUGGING
  1555. static time_t
  1556. PRTime2time_t (PRTime tm)
  1557. {
  1558. PRInt64 rt;
  1559. PR_ASSERT (tm);
  1560. LL_DIV(rt, tm, PR_USEC_PER_SEC);
  1561. return (time_t)rt;
  1562. }
  1563. #endif
  1564. static Slapi_Eq_Context
  1565. repl5_start_debug_timeout(int *setlevel)
  1566. {
  1567. Slapi_Eq_Context eqctx = 0;
  1568. if (s_debug_timeout && s_debug_level) {
  1569. time_t now = time(NULL);
  1570. eqctx = slapi_eq_once(repl5_debug_timeout_callback, setlevel,
  1571. s_debug_timeout + now);
  1572. }
  1573. return eqctx;
  1574. }
  1575. static void
  1576. repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel)
  1577. {
  1578. char buf[20];
  1579. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1580. if (eqctx && !*setlevel) {
  1581. int found = slapi_eq_cancel(eqctx);
  1582. }
  1583. if (s_debug_timeout && s_debug_level && *setlevel) {
  1584. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  1585. sprintf(buf, "%d", 0);
  1586. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1587. }
  1588. }
  1589. static void
  1590. repl5_debug_timeout_callback(time_t when, void *arg)
  1591. {
  1592. int *setlevel = (int *)arg;
  1593. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  1594. char buf[20];
  1595. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1596. *setlevel = 1;
  1597. sprintf(buf, "%d", s_debug_level);
  1598. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1599. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1600. "repl5_debug_timeout_callback: set debug level to %d at %d\n",
  1601. s_debug_level, when);
  1602. }