repl5_connection.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* repl5_connection.c */
  13. /*
  14. The connection object manages a connection to a single replication
  15. consumer.
  16. XXXggood what to do on timeout? If we close connection, then we won't leave a
  17. replica locked. Seems like right thing to do.
  18. */
  19. #include "repl5.h"
  20. #include "repl5_prot_private.h"
  21. #include "slapi-private.h"
  22. #if defined(USE_OPENLDAP)
  23. #include "ldap.h"
  24. #else
  25. #include "ldappr.h"
  26. #include "ldap-extension.h"
  27. #endif
  28. #include "nspr.h"
  29. #include "private/pprio.h"
  30. #include "nss.h"
  31. typedef struct repl_connection
  32. {
  33. char *hostname;
  34. int port;
  35. char *binddn;
  36. int bindmethod;
  37. int state;
  38. int last_operation;
  39. int last_ldap_error;
  40. const char *status;
  41. char *last_ldap_errmsg;
  42. PRUint32 transport_flags;
  43. LDAP *ld;
  44. int supports_ldapv3; /* 1 if does, 0 if doesn't, -1 if not determined */
  45. int supports_ds50_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  46. int supports_ds40_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  47. int supports_ds71_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  48. int supports_ds90_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  49. int linger_time; /* time in seconds to leave an idle connection open */
  50. PRBool linger_active;
  51. Slapi_Eq_Context *linger_event;
  52. PRBool delete_after_linger;
  53. int refcnt;
  54. const Repl_Agmt *agmt;
  55. PRLock *lock;
  56. struct timeval timeout;
  57. int flag_agmt_changed;
  58. char *plain;
  59. void *tot_init_callback; /* Used during total update to do flow control */
  60. } repl_connection;
  61. /* #define DEFAULT_LINGER_TIME (5 * 60) */ /* 5 minutes */
  62. #define DEFAULT_LINGER_TIME (60)
  63. /*** from proto-slap.h ***/
  64. int schema_objectclasses_superset_check(struct berval **remote_schema, char *type);
  65. int schema_attributetypes_superset_check(struct berval **remote_schema, char *type);
  66. void supplier_learn_new_definitions(struct berval **objectclasses, struct berval **attributetypes);
  67. /* Controls we add on every outbound operation */
  68. static LDAPControl manageDSAITControl = {LDAP_CONTROL_MANAGEDSAIT, {0, ""}, '\0'};
  69. static int attribute_string_value_present(LDAP *ld, LDAPMessage *entry,
  70. const char *type, const char *value);
  71. static int bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password);
  72. static int s_debug_timeout = 0;
  73. static int s_debug_level = 0;
  74. static Slapi_Eq_Context repl5_start_debug_timeout(int *setlevel);
  75. static void repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel);
  76. static void repl5_debug_timeout_callback(time_t when, void *arg);
  77. #define STATE_CONNECTED 600
  78. #define STATE_DISCONNECTED 601
  79. #define STATUS_DISCONNECTED "disconnected"
  80. #define STATUS_CONNECTED "connected"
  81. #define STATUS_PROCESSING_ADD "processing add operation"
  82. #define STATUS_PROCESSING_DELETE "processing delete operation"
  83. #define STATUS_PROCESSING_MODIFY "processing modify operation"
  84. #define STATUS_PROCESSING_RENAME "processing rename operation"
  85. #define STATUS_PROCESSING_EXTENDED_OPERATION "processing extended operation"
  86. #define STATUS_LINGERING "lingering"
  87. #define STATUS_SHUTTING_DOWN "shutting down"
  88. #define STATUS_BINDING "connecting and binding"
  89. #define STATUS_SEARCHING "processing search operation"
  90. #define CONN_NO_OPERATION 0
  91. #define CONN_ADD 1
  92. #define CONN_DELETE 2
  93. #define CONN_MODIFY 3
  94. #define CONN_RENAME 4
  95. #define CONN_EXTENDED_OPERATION 5
  96. #define CONN_BIND 6
  97. #define CONN_INIT 7
  98. /* These are errors returned from ldap operations which should cause us to disconnect and
  99. retry the connection later */
  100. #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)
  101. /* Forward declarations */
  102. static void close_connection_internal(Repl_Connection *conn);
  103. static void conn_delete_internal(Repl_Connection *conn);
  104. /* converts consumer's response to a string */
  105. char *
  106. conn_result2string (int result)
  107. {
  108. switch (result)
  109. {
  110. case CONN_OPERATION_SUCCESS: return "operation success";
  111. case CONN_OPERATION_FAILED: return "operation failure";
  112. case CONN_NOT_CONNECTED: return "not connected";
  113. case CONN_SUPPORTS_DS5_REPL: return "consumer supports all DS5 extop";
  114. case CONN_DOES_NOT_SUPPORT_DS5_REPL: return "consumer does not support all DS5 extop";
  115. case CONN_SCHEMA_UPDATED: return "consumer schema updated";
  116. case CONN_SCHEMA_NO_UPDATE_NEEDED: return "consumer schema up to date";
  117. case CONN_LOCAL_ERROR: return "local error";
  118. case CONN_BUSY: return "consumer is busy";
  119. case CONN_SSL_NOT_ENABLED: return "NSS not initialized";
  120. case CONN_TIMEOUT: return "time out";
  121. case CONN_SUPPORTS_DS71_REPL: return "consumer supports all DS71 extop";
  122. case CONN_DOES_NOT_SUPPORT_DS71_REPL: return "consumer does not support all DS7.1 extop";
  123. case CONN_IS_READONLY: return "consumer is read only";
  124. case CONN_IS_NOT_READONLY: return "consumer is not read only";
  125. case CONN_SUPPORTS_DIRSYNC: return "consumer supports DIRSYNC control";
  126. case CONN_DOES_NOT_SUPPORT_DIRSYNC: return "consumer does not support DIRSYNC control";
  127. case CONN_IS_WIN2K3: return "consumer is W2K3 or after";
  128. case CONN_NOT_WIN2K3: return "consumer is before W2K3";
  129. case CONN_SUPPORTS_DS90_REPL: return "consumer supports all DS90 extop";
  130. case CONN_DOES_NOT_SUPPORT_DS90_REPL: return "consumer does not support all DS90 extop";
  131. default: return NULL;
  132. }
  133. }
  134. /*
  135. * Create a new connection object. Returns a pointer to the object, or
  136. * NULL if an error occurs.
  137. */
  138. Repl_Connection *
  139. conn_new(Repl_Agmt *agmt)
  140. {
  141. Repl_Connection *rpc;
  142. rpc = (Repl_Connection *)slapi_ch_malloc(sizeof(repl_connection));
  143. if ((rpc->lock = PR_NewLock()) == NULL)
  144. {
  145. goto loser;
  146. }
  147. rpc->hostname = agmt_get_hostname(agmt);
  148. rpc->port = agmt_get_port(agmt);
  149. rpc->binddn = agmt_get_binddn(agmt);
  150. rpc->bindmethod = agmt_get_bindmethod(agmt);
  151. rpc->transport_flags = agmt_get_transport_flags(agmt);
  152. rpc->ld = NULL;
  153. rpc->state = STATE_DISCONNECTED;
  154. rpc->last_operation = CONN_NO_OPERATION;
  155. rpc->last_ldap_error = LDAP_SUCCESS;
  156. rpc->last_ldap_errmsg = NULL;
  157. rpc->supports_ldapv3 = -1;
  158. rpc->supports_ds40_repl = -1;
  159. rpc->supports_ds50_repl = -1;
  160. rpc->supports_ds71_repl = -1;
  161. rpc->supports_ds90_repl = -1;
  162. rpc->linger_active = PR_FALSE;
  163. rpc->delete_after_linger = PR_FALSE;
  164. rpc->linger_event = NULL;
  165. rpc->linger_time = DEFAULT_LINGER_TIME;
  166. rpc->status = STATUS_DISCONNECTED;
  167. rpc->agmt = agmt;
  168. rpc->refcnt = 1;
  169. rpc->timeout.tv_sec = agmt_get_timeout(agmt);
  170. rpc->timeout.tv_usec = 0;
  171. rpc->flag_agmt_changed = 0;
  172. rpc->plain = NULL;
  173. return rpc;
  174. loser:
  175. conn_delete_internal(rpc);
  176. slapi_ch_free((void**)&rpc);
  177. return NULL;
  178. }
  179. static PRBool
  180. conn_connected_locked(Repl_Connection *conn, int locked)
  181. {
  182. PRBool return_value;
  183. if(!locked) PR_Lock(conn->lock);
  184. return_value = STATE_CONNECTED == conn->state;
  185. if(!locked) PR_Unlock(conn->lock);
  186. return return_value;
  187. }
  188. /*
  189. * Return PR_TRUE if the connection is in the connected state
  190. */
  191. static PRBool
  192. conn_connected(Repl_Connection *conn)
  193. {
  194. return conn_connected_locked(conn, 1);
  195. }
  196. /*
  197. * Destroy a connection object.
  198. */
  199. static void
  200. conn_delete_internal(Repl_Connection *conn)
  201. {
  202. PR_ASSERT(NULL != conn);
  203. close_connection_internal(conn);
  204. /* slapi_ch_free accepts NULL pointer */
  205. slapi_ch_free((void **)&conn->hostname);
  206. slapi_ch_free((void **)&conn->binddn);
  207. slapi_ch_free((void **)&conn->plain);
  208. }
  209. /*
  210. * Used by CLEANALLRUV - free it all!
  211. */
  212. void
  213. conn_delete_internal_ext(Repl_Connection *conn)
  214. {
  215. conn_delete_internal(conn);
  216. PR_DestroyLock(conn->lock);
  217. slapi_ch_free((void **)&conn);
  218. }
  219. /*
  220. * Destroy a connection. It is an error to use the connection object
  221. * after conn_delete() has been called.
  222. */
  223. void
  224. conn_delete(Repl_Connection *conn)
  225. {
  226. PRBool destroy_it = PR_FALSE;
  227. PR_ASSERT(NULL != conn);
  228. PR_Lock(conn->lock);
  229. if (conn->linger_active)
  230. {
  231. if (slapi_eq_cancel(conn->linger_event) == 1)
  232. {
  233. /* Event was found and cancelled. Destroy the connection object. */
  234. destroy_it = PR_TRUE;
  235. }
  236. else
  237. {
  238. /*
  239. * The event wasn't found, but we think it's still active.
  240. * That means an event is in the process of being fired
  241. * off, so arrange for the event to destroy the object .
  242. */
  243. conn->delete_after_linger = PR_TRUE;
  244. }
  245. }
  246. if (destroy_it)
  247. {
  248. conn_delete_internal(conn);
  249. }
  250. PR_Unlock(conn->lock);
  251. }
  252. void
  253. conn_set_tot_update_cb_nolock(Repl_Connection *conn, void *cb_data)
  254. {
  255. conn->tot_init_callback = (void *) cb_data;
  256. }
  257. void
  258. conn_set_tot_update_cb(Repl_Connection *conn, void *cb_data)
  259. {
  260. PR_Lock(conn->lock);
  261. conn_set_tot_update_cb_nolock(conn, cb_data);
  262. PR_Unlock(conn->lock);
  263. }
  264. void
  265. conn_get_tot_update_cb_nolock(Repl_Connection *conn, void **cb_data)
  266. {
  267. *cb_data = (void *) conn->tot_init_callback;
  268. }
  269. void
  270. conn_get_tot_update_cb(Repl_Connection *conn, void **cb_data)
  271. {
  272. PR_Lock(conn->lock);
  273. conn_get_tot_update_cb_nolock(conn, cb_data);
  274. PR_Unlock(conn->lock);
  275. }
  276. /*
  277. * Return the last operation type processed by the connection
  278. * object, and the LDAP error encountered.
  279. */
  280. void
  281. conn_get_error(Repl_Connection *conn, int *operation, int *error)
  282. {
  283. PR_Lock(conn->lock);
  284. *operation = conn->last_operation;
  285. *error = conn->last_ldap_error;
  286. PR_Unlock(conn->lock);
  287. }
  288. /*
  289. * Return the last operation type processed by the connection
  290. * object, and the LDAP error encountered.
  291. * Beware that the error string will only be in scope and valid
  292. * before the next operation result has been read from the connection
  293. * (so don't alias the pointer).
  294. */
  295. void
  296. conn_get_error_ex(Repl_Connection *conn, int *operation, int *error, char **error_string)
  297. {
  298. PR_Lock(conn->lock);
  299. *operation = conn->last_operation;
  300. *error = conn->last_ldap_error;
  301. *error_string = conn->last_ldap_errmsg;
  302. PR_Unlock(conn->lock);
  303. }
  304. /* Returns the result (asyncronously) from an opertation and also returns that operations message ID */
  305. /* The _ex version handles a bunch of parameters (retoidp et al) that were present in the original
  306. * sync operation functions, but were never actually used) */
  307. ConnResult
  308. conn_read_result_ex(Repl_Connection *conn, char **retoidp, struct berval **retdatap, LDAPControl ***returned_controls, int send_msgid, int *resp_msgid, int block)
  309. {
  310. LDAPMessage *res = NULL;
  311. int setlevel = 0;
  312. int rc = 0;
  313. int return_value = 0;
  314. LDAPControl **loc_returned_controls = NULL;
  315. struct timeval local_timeout = {0};
  316. time_t time_now = 0;
  317. time_t start_time = time( NULL );
  318. int backoff_time = 1;
  319. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  320. /* Here, we want to not block inside ldap_result().
  321. * Reason is that blocking there will deadlock with a
  322. * concurrent sender. We send concurrently, and hence
  323. * blocking is not good : deadlock results.
  324. * So, instead, we call ldap_result() with a zero timeout.
  325. * This makes it do a non-blocking poll and return to us
  326. * if there's no data to read.
  327. * We can then handle our timeout here by sleeping and re-trying.
  328. * In order that we do pickup results reasonably quickly,
  329. * we implement a backoff algorithm for the sleep: if we
  330. * keep getting results quickly then we won't spend much time sleeping.
  331. */
  332. while (!slapi_is_shutting_down())
  333. {
  334. /* we have to make sure the update sending thread does not
  335. attempt to close connection while we are reading
  336. results - so lock the conn while we get the results */
  337. PR_Lock(conn->lock);
  338. if ((STATE_CONNECTED != conn->state) || !conn->ld) {
  339. rc = -1;
  340. return_value = CONN_NOT_CONNECTED;
  341. PR_Unlock(conn->lock);
  342. break;
  343. }
  344. rc = ldap_result(conn->ld, send_msgid, 1, &local_timeout, &res);
  345. PR_Unlock(conn->lock);
  346. if (0 != rc)
  347. {
  348. /* Something other than a timeout happened */
  349. break;
  350. }
  351. if (block)
  352. {
  353. /* Did the connection's timeout expire ? */
  354. time_now = time( NULL );
  355. if (conn->timeout.tv_sec <= ( time_now - start_time ))
  356. {
  357. /* We timed out */
  358. rc = 0;
  359. break;
  360. }
  361. /* Otherwise we backoff */
  362. DS_Sleep(PR_MillisecondsToInterval(backoff_time));
  363. if (backoff_time < 1000)
  364. {
  365. backoff_time <<= 1;
  366. }
  367. } else
  368. {
  369. rc = 0;
  370. break;
  371. }
  372. }
  373. repl5_stop_debug_timeout(eqctx, &setlevel);
  374. PR_Lock(conn->lock);
  375. /* we have to check again since the connection may have
  376. been closed in the meantime
  377. acquire the lock for the rest of the function
  378. to protect against another attempt to close
  379. the conn while we are using it
  380. */
  381. if ((STATE_CONNECTED != conn->state) || !conn->ld) {
  382. rc = -1;
  383. return_value = CONN_NOT_CONNECTED;
  384. }
  385. if (0 == rc)
  386. {
  387. /* Timeout */
  388. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  389. conn->last_ldap_error = LDAP_TIMEOUT;
  390. return_value = CONN_TIMEOUT;
  391. }
  392. else if ((-1 == rc) && (CONN_NOT_CONNECTED == return_value))
  393. {
  394. /* must not access conn->ld if disconnected in another thread */
  395. /* the other thread that actually did the conn_disconnect() */
  396. /* will set the status and error info */
  397. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  398. "%s: Connection disconnected by another thread\n",
  399. agmt_get_long_name(conn->agmt));
  400. }
  401. else if (-1 == rc)
  402. {
  403. /* Error */
  404. char *s = NULL;
  405. rc = slapi_ldap_get_lderrno(conn->ld, NULL, &s);
  406. conn->last_ldap_errmsg = s;
  407. conn->last_ldap_error = rc;
  408. /* some errors will require a disconnect and retry the connection
  409. later */
  410. if (IS_DISCONNECT_ERROR(rc))
  411. {
  412. close_connection_internal(conn); /* we already have the lock */
  413. return_value = CONN_NOT_CONNECTED;
  414. }
  415. else
  416. {
  417. conn->status = STATUS_CONNECTED;
  418. return_value = CONN_OPERATION_FAILED;
  419. }
  420. }
  421. else
  422. {
  423. int err;
  424. char *errmsg = NULL;
  425. char **referrals = NULL;
  426. char *matched = NULL;
  427. if (resp_msgid)
  428. {
  429. *resp_msgid = ldap_msgid(res);
  430. }
  431. rc = ldap_parse_result(conn->ld, res, &err, &matched,
  432. &errmsg, &referrals, &loc_returned_controls,
  433. 0 /* Don't free the result */);
  434. if (IS_DISCONNECT_ERROR(rc))
  435. {
  436. conn->last_ldap_error = rc;
  437. close_connection_internal(conn); /* we already have the lock */
  438. return_value = CONN_NOT_CONNECTED;
  439. goto done;
  440. }
  441. else if (IS_DISCONNECT_ERROR(err))
  442. {
  443. conn->last_ldap_error = err;
  444. close_connection_internal(conn); /* we already have the lock */
  445. return_value = CONN_NOT_CONNECTED;
  446. goto done;
  447. }
  448. /* Got a result */
  449. if (retoidp /* total update */)
  450. {
  451. if (!((rc == LDAP_SUCCESS) && (err == LDAP_BUSY)))
  452. {
  453. if (rc == LDAP_SUCCESS) {
  454. rc = ldap_parse_extended_result(conn->ld, res, retoidp,
  455. retdatap, 0 /* Don't Free it */);
  456. }
  457. conn->last_ldap_error = rc;
  458. return_value = (LDAP_SUCCESS == conn->last_ldap_error ?
  459. CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED);
  460. }
  461. }
  462. else /* regular operation, result returned */
  463. {
  464. if (NULL != returned_controls)
  465. {
  466. *returned_controls = loc_returned_controls;
  467. }
  468. if (LDAP_SUCCESS != rc)
  469. {
  470. conn->last_ldap_error = rc;
  471. }
  472. else
  473. {
  474. conn->last_ldap_error = err;
  475. }
  476. return_value = LDAP_SUCCESS == conn->last_ldap_error ? CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED;
  477. }
  478. conn->status = STATUS_CONNECTED;
  479. done:
  480. slapi_ch_free_string(&errmsg);
  481. slapi_ch_free_string(&matched);
  482. charray_free(referrals);
  483. }
  484. if (res) ldap_msgfree(res);
  485. PR_Unlock(conn->lock); /* release the conn lock */
  486. return return_value;
  487. }
  488. ConnResult
  489. conn_read_result(Repl_Connection *conn, int *message_id)
  490. {
  491. return conn_read_result_ex(conn,NULL,NULL,NULL,LDAP_RES_ANY,message_id,1);
  492. }
  493. /* Because the SDK isn't really thread-safe (it can deadlock between
  494. * a thread sending an operation and a thread trying to retrieve a response
  495. * on the same connection), we need to _first_ verify that the connection
  496. * is writable. If it isn't, we can deadlock if we proceed any further...
  497. */
  498. #if defined(USE_OPENLDAP)
  499. /* openldap has LBER_SB_OPT_DATA_READY but that doesn't really
  500. work for our purposes - so we grab the openldap fd from the
  501. ber sockbuf layer, import it into a PR Poll FD, then
  502. do the poll
  503. */
  504. static ConnResult
  505. see_if_write_available(Repl_Connection *conn, PRIntervalTime timeout)
  506. {
  507. PRFileDesc *pollfd = NULL;
  508. PRPollDesc polldesc;
  509. ber_socket_t fd = 0;
  510. int rc;
  511. /* get the sockbuf */
  512. if ((ldap_get_option(conn->ld, LDAP_OPT_DESC, &fd) != LDAP_OPT_SUCCESS) || (fd <= 0)) {
  513. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  514. "%s: invalid connection insee_if_write_available \n",
  515. agmt_get_long_name(conn->agmt));
  516. conn->last_ldap_error = LDAP_PARAM_ERROR;
  517. return CONN_OPERATION_FAILED;
  518. }
  519. /* wrap the sockbuf fd with a NSPR FD created especially
  520. for use with polling, and only with polling */
  521. pollfd = PR_CreateSocketPollFd(fd);
  522. polldesc.fd = pollfd;
  523. polldesc.in_flags = PR_POLL_WRITE|PR_POLL_EXCEPT;
  524. polldesc.out_flags = 0;
  525. /* do the poll */
  526. rc = PR_Poll(&polldesc, 1, timeout);
  527. /* unwrap the socket */
  528. PR_DestroySocketPollFd(pollfd);
  529. /* check */
  530. if (rc == 0) { /* timeout */
  531. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  532. "%s: poll timed out - poll interval [%d]\n",
  533. agmt_get_long_name(conn->agmt),
  534. timeout);
  535. return CONN_TIMEOUT;
  536. } else if ((rc < 0) || (!(polldesc.out_flags&PR_POLL_WRITE))) { /* error */
  537. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  538. "%s: error during poll attempt [%d:%s]\n",
  539. agmt_get_long_name(conn->agmt),
  540. PR_GetError(), slapd_pr_strerror(PR_GetError()));
  541. conn->last_ldap_error = LDAP_PARAM_ERROR;
  542. return CONN_OPERATION_FAILED;
  543. }
  544. return CONN_OPERATION_SUCCESS;
  545. }
  546. #else /* ! USE_OPENLDAP */
  547. /* Since we're poking around with ldap c sdk internals, we have to
  548. be careful since the PR layer stores different session and socket
  549. info than the NSS SSL layer than the SASL layer - and they all
  550. use different poll functions too
  551. */
  552. static ConnResult
  553. see_if_write_available(Repl_Connection *conn, PRIntervalTime timeout)
  554. {
  555. LDAP_X_PollFD pollstr;
  556. int nfds = 1;
  557. struct ldap_x_ext_io_fns iofns;
  558. int rc = LDAP_SUCCESS;
  559. LDAP_X_EXTIOF_POLL_CALLBACK *ldap_poll;
  560. struct lextiof_session_private *private;
  561. /* get the poll function to use */
  562. memset(&iofns, 0, sizeof(iofns));
  563. iofns.lextiof_size = LDAP_X_EXTIO_FNS_SIZE;
  564. if (ldap_get_option(conn->ld, LDAP_X_OPT_EXTIO_FN_PTRS, &iofns) < 0) {
  565. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  566. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  567. "%s: Failed call to ldap_get_option to get extiofns in "
  568. "see_if_write_available: LDAP error %d (%s)\n",
  569. agmt_get_long_name(conn->agmt),
  570. rc, ldap_err2string(rc));
  571. conn->last_ldap_error = rc;
  572. return CONN_OPERATION_FAILED;
  573. }
  574. ldap_poll = iofns.lextiof_poll;
  575. /* set up the poll structure */
  576. if (ldap_get_option(conn->ld, LDAP_OPT_DESC, &pollstr.lpoll_fd) < 0) {
  577. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  578. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  579. "%s: Failed call to ldap_get_option for poll_fd in "
  580. "see_if_write_available: LDAP error %d (%s)\n",
  581. agmt_get_long_name(conn->agmt),
  582. rc, ldap_err2string(rc));
  583. conn->last_ldap_error = rc;
  584. return CONN_OPERATION_FAILED;
  585. }
  586. if (ldap_get_option(conn->ld, LDAP_X_OPT_SOCKETARG,
  587. &pollstr.lpoll_socketarg) < 0) {
  588. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  589. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  590. "%s: Failed call to ldap_get_option for socketarg in "
  591. "see_if_write_available: LDAP error %d (%s)\n",
  592. agmt_get_long_name(conn->agmt),
  593. rc, ldap_err2string(rc));
  594. conn->last_ldap_error = rc;
  595. return CONN_OPERATION_FAILED;
  596. }
  597. pollstr.lpoll_events = LDAP_X_POLLOUT;
  598. pollstr.lpoll_revents = 0;
  599. private = iofns.lextiof_session_arg;
  600. if (0 == (*ldap_poll)(&pollstr, nfds, timeout, private)) {
  601. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  602. "%s: poll timed out - poll interval [%d]\n",
  603. agmt_get_long_name(conn->agmt),
  604. timeout);
  605. return CONN_TIMEOUT;
  606. }
  607. return CONN_OPERATION_SUCCESS;
  608. }
  609. #endif /* ! USE_OPENLDAP */
  610. /*
  611. * During a total update, this function checks how much entries
  612. * have been sent to the consumer without having received their acknowledgment.
  613. * Basically it checks how late is the consumer.
  614. *
  615. * If the consumer is too late, it pause the RA.sender (releasing the lock) to
  616. * let the consumer to catch up and RA.reader to receive the acknowledgments.
  617. *
  618. * Caller must hold conn->lock
  619. */
  620. static void
  621. check_flow_control_tot_init(Repl_Connection *conn, int optype, const char *extop_oid, int sent_msgid)
  622. {
  623. int rcv_msgid;
  624. int once;
  625. if ((sent_msgid != 0) && (optype == CONN_EXTENDED_OPERATION) && (strcmp(extop_oid, REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID) == 0)) {
  626. /* We are sending entries part of the total update of a consumer
  627. * Wait a bit if the consumer needs to catchup from the current sent entries
  628. */
  629. rcv_msgid = repl5_tot_last_rcv_msgid(conn);
  630. if (rcv_msgid == -1) {
  631. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  632. "%s: check_flow_control_tot_init no callback data [ msgid sent: %d]\n",
  633. agmt_get_long_name(conn->agmt),
  634. sent_msgid);
  635. } else if (sent_msgid < rcv_msgid) {
  636. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  637. "%s: check_flow_control_tot_init invalid message ids [ msgid sent: %d, rcv: %d]\n",
  638. agmt_get_long_name(conn->agmt),
  639. sent_msgid,
  640. rcv_msgid);
  641. } else if ((sent_msgid - rcv_msgid) > agmt_get_flowcontrolwindow(conn->agmt)) {
  642. int totalUpdatePause;
  643. totalUpdatePause = agmt_get_flowcontrolpause(conn->agmt);
  644. if (totalUpdatePause) {
  645. /* The consumer is late. Last sent entry compare to last acknowledged entry
  646. * overpass the allowed limit (flowcontrolwindow)
  647. * Give some time to the consumer to catch up
  648. */
  649. once = repl5_tot_flowcontrol_detection(conn, 1);
  650. PR_Unlock(conn->lock);
  651. if (once == 1) {
  652. /* This is the first time we hit total update flow control.
  653. * Log it at least once to inform administrator there is
  654. * a potential configuration issue here
  655. */
  656. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  657. "%s: Total update flow control gives time (%d msec) to the consumer before sending more entries [ msgid sent: %d, rcv: %d])\n"
  658. "If total update fails you can try to increase %s and/or decrease %s in the replica agreement configuration\n",
  659. agmt_get_long_name(conn->agmt),
  660. totalUpdatePause,
  661. sent_msgid,
  662. rcv_msgid,
  663. type_nsds5ReplicaFlowControlPause,
  664. type_nsds5ReplicaFlowControlWindow);
  665. }
  666. DS_Sleep(PR_MillisecondsToInterval(totalUpdatePause));
  667. PR_Lock(conn->lock);
  668. }
  669. }
  670. }
  671. }
  672. /*
  673. * Test if the connection is available to do a write.
  674. * This function is doing a periodic polling of the connection.
  675. * If the polling times out:
  676. * - it releases the connection lock (to let other thread ,i.e.
  677. * replication result thread, the opportunity to use the connection)
  678. * - Sleeps for a short period (100ms)
  679. * - acquires the connection lock
  680. *
  681. * It loops until
  682. * - it is available
  683. * - exceeds RA complete timeout
  684. * - server is shutdown
  685. * - connection is disconnected (Disable, stop, delete the RA
  686. * 'terminate' the replication protocol and disconnect the connection)
  687. *
  688. * Return:
  689. * - CONN_OPERATION_SUCCESS if the connection is available
  690. * - CONN_TIMEOUT if the overall polling/sleeping delay exceeds RA timeout
  691. * - CONN_NOT_CONNECTED if the replication connection state is disconnected
  692. * - other ConnResult
  693. *
  694. * Caller must hold conn->Lock. At the exit, conn->lock is held
  695. */
  696. static ConnResult
  697. conn_is_available(Repl_Connection *conn)
  698. {
  699. time_t poll_timeout_sec = 1; /* Polling for 1sec */
  700. time_t yield_delay_msec = 100; /* Delay to wait */
  701. time_t start_time = time( NULL );
  702. time_t time_now;
  703. ConnResult return_value = CONN_OPERATION_SUCCESS;
  704. while (!slapi_is_shutting_down() && (conn->state != STATE_DISCONNECTED)) {
  705. return_value = see_if_write_available(conn, PR_SecondsToInterval(poll_timeout_sec));
  706. if (return_value == CONN_TIMEOUT) {
  707. /* in case of timeout we return CONN_TIMEOUT only
  708. * if the RA.timeout is exceeded
  709. */
  710. time_now = time(NULL);
  711. if (conn->timeout.tv_sec <= (time_now - start_time)) {
  712. break;
  713. } else {
  714. /* Else give connection to others threads */
  715. PR_Unlock(conn->lock);
  716. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  717. "%s: perform_operation transient timeout. retry)\n",
  718. agmt_get_long_name(conn->agmt));
  719. DS_Sleep(PR_MillisecondsToInterval(yield_delay_msec));
  720. PR_Lock(conn->lock);
  721. }
  722. } else {
  723. break;
  724. }
  725. }
  726. if (conn->state == STATE_DISCONNECTED) {
  727. return_value = CONN_NOT_CONNECTED;
  728. }
  729. return return_value;
  730. }
  731. /*
  732. * Common code to send an LDAPv3 operation and collect the result.
  733. * Return values:
  734. * CONN_OPERATION_SUCCESS - the operation succeeded
  735. * CONN_OPERATION_FAILED - the operation was sent to the consumer
  736. * and failed. Use conn_get_error() to determine the LDAP error
  737. * code.
  738. * CONN_NOT_CONNECTED - no connection is active. The caller should
  739. * use conn_connect() to connect to the replica and bind, then should
  740. * reacquire the replica (if needed).
  741. * CONN_BUSY - the server is busy with previous requests, must wait for a while
  742. * before retrying
  743. * DBDB: also returns the operation's message ID, if it was successfully sent, now that
  744. * we're reading results async.
  745. */
  746. static ConnResult
  747. perform_operation(Repl_Connection *conn, int optype, const char *dn,
  748. LDAPMod **attrs, const char *newrdn, const char *newparent,
  749. int deleteoldrdn, LDAPControl *update_control,
  750. const char *extop_oid, struct berval *extop_payload, int *message_id)
  751. {
  752. int rc = -1;
  753. ConnResult return_value = CONN_OPERATION_FAILED;
  754. LDAPControl *server_controls[3];
  755. /* LDAPControl **loc_returned_controls; */
  756. const char *op_string = NULL;
  757. int msgid = 0;
  758. server_controls[0] = &manageDSAITControl;
  759. server_controls[1] = update_control;
  760. server_controls[2] = NULL;
  761. /*
  762. * Lock the conn to prevent the result reader thread
  763. * from closing the connection out from under us.
  764. */
  765. PR_Lock(conn->lock);
  766. if (STATE_CONNECTED == conn->state)
  767. {
  768. int setlevel = 0;
  769. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  770. return_value = conn_is_available(conn);
  771. if (return_value != CONN_OPERATION_SUCCESS) {
  772. PR_Unlock(conn->lock);
  773. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  774. "%s: perform_operation connection is not available (%d)\n",
  775. agmt_get_long_name(conn->agmt),
  776. return_value);
  777. return return_value;
  778. }
  779. conn->last_operation = optype;
  780. switch (optype)
  781. {
  782. case CONN_ADD:
  783. conn->status = STATUS_PROCESSING_ADD;
  784. op_string = "add";
  785. rc = ldap_add_ext(conn->ld, dn, attrs, server_controls,
  786. NULL /* clientctls */, &msgid);
  787. break;
  788. case CONN_MODIFY:
  789. conn->status = STATUS_PROCESSING_MODIFY;
  790. op_string = "modify";
  791. rc = ldap_modify_ext(conn->ld, dn, attrs, server_controls,
  792. NULL /* clientctls */, &msgid);
  793. break;
  794. case CONN_DELETE:
  795. conn->status = STATUS_PROCESSING_DELETE;
  796. op_string = "delete";
  797. rc = ldap_delete_ext(conn->ld, dn, server_controls,
  798. NULL /* clientctls */, &msgid);
  799. break;
  800. case CONN_RENAME:
  801. conn->status = STATUS_PROCESSING_RENAME;
  802. op_string = "rename";
  803. rc = ldap_rename(conn->ld, dn, newrdn, newparent, deleteoldrdn,
  804. server_controls, NULL /* clientctls */, &msgid);
  805. break;
  806. case CONN_EXTENDED_OPERATION:
  807. conn->status = STATUS_PROCESSING_EXTENDED_OPERATION;
  808. op_string = "extended";
  809. rc = ldap_extended_operation(conn->ld, extop_oid, extop_payload,
  810. server_controls, NULL /* clientctls */, &msgid);
  811. }
  812. repl5_stop_debug_timeout(eqctx, &setlevel);
  813. if (LDAP_SUCCESS == rc)
  814. {
  815. /* DBDB: The code that used to be here has been moved for async operation
  816. * Results are now picked up in another thread. All we need to do here is
  817. * queue the operation details in the outstanding operation list.
  818. */
  819. return_value = CONN_OPERATION_SUCCESS;
  820. }
  821. else
  822. {
  823. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  824. "%s: Failed to send %s operation: LDAP error %d (%s)\n",
  825. agmt_get_long_name(conn->agmt),
  826. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  827. conn->last_ldap_error = rc;
  828. if (IS_DISCONNECT_ERROR(rc))
  829. {
  830. close_connection_internal(conn); /* already have the lock */
  831. return_value = CONN_NOT_CONNECTED;
  832. }
  833. else
  834. {
  835. conn->status = STATUS_CONNECTED;
  836. return_value = CONN_OPERATION_FAILED;
  837. }
  838. }
  839. }
  840. else
  841. {
  842. /* conn->last_ldap_error has been set to a more specific value
  843. * in the thread that did the disconnection
  844. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  845. */
  846. return_value = CONN_NOT_CONNECTED;
  847. }
  848. check_flow_control_tot_init(conn, optype, extop_oid, msgid);
  849. PR_Unlock(conn->lock); /* release the lock */
  850. if (message_id)
  851. {
  852. *message_id = msgid;
  853. }
  854. return return_value;
  855. }
  856. /*
  857. * Send an LDAP add operation.
  858. */
  859. ConnResult
  860. conn_send_add(Repl_Connection *conn, const char *dn, LDAPMod **attrs,
  861. LDAPControl *update_control, int *message_id)
  862. {
  863. return perform_operation(conn, CONN_ADD, dn, attrs, NULL /* newrdn */,
  864. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  865. NULL /* extop OID */, NULL /* extop payload */, message_id);
  866. }
  867. /*
  868. * Send an LDAP delete operation.
  869. */
  870. ConnResult
  871. conn_send_delete(Repl_Connection *conn, const char *dn,
  872. LDAPControl *update_control, int *message_id)
  873. {
  874. return perform_operation(conn, CONN_DELETE, dn, NULL /* attrs */,
  875. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  876. update_control, NULL /* extop OID */, NULL /* extop payload */, message_id);
  877. }
  878. /*
  879. * Send an LDAP modify operation.
  880. */
  881. ConnResult
  882. conn_send_modify(Repl_Connection *conn, const char *dn, LDAPMod **mods,
  883. LDAPControl *update_control, int *message_id)
  884. {
  885. return perform_operation(conn, CONN_MODIFY, dn, mods, NULL /* newrdn */,
  886. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  887. NULL /* extop OID */, NULL /* extop payload */, message_id);
  888. }
  889. /*
  890. * Send an LDAP moddn operation.
  891. */
  892. ConnResult
  893. conn_send_rename(Repl_Connection *conn, const char *dn,
  894. const char *newrdn, const char *newparent, int deleteoldrdn,
  895. LDAPControl *update_control, int *message_id)
  896. {
  897. return perform_operation(conn, CONN_RENAME, dn, NULL /* attrs */,
  898. newrdn, newparent, deleteoldrdn, update_control,
  899. NULL /* extop OID */, NULL /* extop payload */, message_id);
  900. }
  901. /*
  902. * Send an LDAP extended operation.
  903. */
  904. ConnResult
  905. conn_send_extended_operation(Repl_Connection *conn, const char *extop_oid,
  906. struct berval *payload,
  907. LDAPControl *update_control, int *message_id)
  908. {
  909. return perform_operation(conn, CONN_EXTENDED_OPERATION, NULL /* dn */, NULL /* attrs */,
  910. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  911. update_control, extop_oid, payload, message_id);
  912. }
  913. /*
  914. * Synchronously read an entry and return a specific attribute's values.
  915. * Returns CONN_OPERATION_SUCCESS if successful. Returns
  916. * CONN_OPERATION_FAILED if the operation was sent but an LDAP error
  917. * occurred (conn->last_ldap_error is set in this case), and
  918. * CONN_NOT_CONNECTED if no connection was active.
  919. *
  920. * The caller must free the returned_bvals.
  921. */
  922. ConnResult
  923. conn_read_entry_attribute(Repl_Connection *conn, const char *dn,
  924. char *type, struct berval ***returned_bvals)
  925. {
  926. ConnResult return_value;
  927. int ldap_rc;
  928. LDAPControl *server_controls[2];
  929. LDAPMessage *res = NULL;
  930. char *attrs[2];
  931. PR_Lock(conn->lock);
  932. PR_ASSERT(NULL != type);
  933. if (conn_connected(conn))
  934. {
  935. server_controls[0] = &manageDSAITControl;
  936. server_controls[1] = NULL;
  937. attrs[0] = type;
  938. attrs[1] = NULL;
  939. ldap_rc = ldap_search_ext_s(conn->ld, dn, LDAP_SCOPE_BASE,
  940. "(objectclass=*)", attrs, 0 /* attrsonly */,
  941. server_controls, NULL /* client controls */,
  942. &conn->timeout, 0 /* sizelimit */, &res);
  943. if (LDAP_SUCCESS == ldap_rc)
  944. {
  945. LDAPMessage *entry = ldap_first_entry(conn->ld, res);
  946. if (NULL != entry)
  947. {
  948. *returned_bvals = ldap_get_values_len(conn->ld, entry, type);
  949. }
  950. return_value = CONN_OPERATION_SUCCESS;
  951. }
  952. else if (IS_DISCONNECT_ERROR(ldap_rc))
  953. {
  954. close_connection_internal(conn);
  955. return_value = CONN_NOT_CONNECTED;
  956. }
  957. else
  958. {
  959. return_value = CONN_OPERATION_FAILED;
  960. }
  961. conn->last_ldap_error = ldap_rc;
  962. if (NULL != res)
  963. {
  964. ldap_msgfree(res);
  965. res = NULL;
  966. }
  967. }
  968. else
  969. {
  970. return_value = CONN_NOT_CONNECTED;
  971. }
  972. PR_Unlock(conn->lock);
  973. return return_value;
  974. }
  975. /*
  976. * Return an pointer to a string describing the connection's status.
  977. */
  978. const char *
  979. conn_get_status(Repl_Connection *conn)
  980. {
  981. return conn->status;
  982. }
  983. /*
  984. * Cancel any outstanding linger timer. Should be called when
  985. * a replication session is beginning.
  986. */
  987. void
  988. conn_cancel_linger(Repl_Connection *conn)
  989. {
  990. PR_ASSERT(NULL != conn);
  991. PR_Lock(conn->lock);
  992. if (conn->linger_active)
  993. {
  994. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  995. "%s: Cancelling linger on the connection\n",
  996. agmt_get_long_name(conn->agmt));
  997. conn->linger_active = PR_FALSE;
  998. if (slapi_eq_cancel(conn->linger_event) == 1)
  999. {
  1000. conn->refcnt--;
  1001. }
  1002. conn->linger_event = NULL;
  1003. conn->status = STATUS_CONNECTED;
  1004. }
  1005. else
  1006. {
  1007. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1008. "%s: No linger to cancel on the connection\n",
  1009. agmt_get_long_name(conn->agmt));
  1010. }
  1011. PR_Unlock(conn->lock);
  1012. }
  1013. /*
  1014. * Called when our linger timeout timer expires. This means
  1015. * we should check to see if perhaps the connection's become
  1016. * active again, in which case we do nothing. Otherwise,
  1017. * we close the connection.
  1018. */
  1019. static void
  1020. linger_timeout(time_t event_time, void *arg)
  1021. {
  1022. PRBool delete_now;
  1023. Repl_Connection *conn = (Repl_Connection *)arg;
  1024. PR_ASSERT(NULL != conn);
  1025. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1026. "%s: Linger timeout has expired on the connection\n",
  1027. agmt_get_long_name(conn->agmt));
  1028. PR_Lock(conn->lock);
  1029. if (conn->linger_active)
  1030. {
  1031. conn->linger_active = PR_FALSE;
  1032. conn->linger_event = NULL;
  1033. close_connection_internal(conn);
  1034. }
  1035. delete_now = conn->delete_after_linger;
  1036. PR_Unlock(conn->lock);
  1037. if (delete_now)
  1038. {
  1039. conn_delete_internal(conn);
  1040. }
  1041. }
  1042. /*
  1043. * Indicate that a session is ending. The linger timer starts when
  1044. * this function is called.
  1045. */
  1046. void
  1047. conn_start_linger(Repl_Connection *conn)
  1048. {
  1049. time_t now;
  1050. PR_ASSERT(NULL != conn);
  1051. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1052. "%s: Beginning linger on the connection\n",
  1053. agmt_get_long_name(conn->agmt));
  1054. if (!conn_connected(conn))
  1055. {
  1056. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1057. "%s: No linger on the closed conn\n",
  1058. agmt_get_long_name(conn->agmt));
  1059. return;
  1060. }
  1061. time(&now);
  1062. PR_Lock(conn->lock);
  1063. if (conn->linger_active)
  1064. {
  1065. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1066. "%s: Linger already active on the connection\n",
  1067. agmt_get_long_name(conn->agmt));
  1068. }
  1069. else
  1070. {
  1071. conn->linger_active = PR_TRUE;
  1072. conn->linger_event = slapi_eq_once(linger_timeout, conn, now + conn->linger_time);
  1073. conn->status = STATUS_LINGERING;
  1074. }
  1075. PR_Unlock(conn->lock);
  1076. }
  1077. /*
  1078. * If no connection is currently active, opens a connection and binds to
  1079. * the remote server. If a connection is open (e.g. lingering) then
  1080. * this is a no-op.
  1081. *
  1082. * Returns CONN_OPERATION_SUCCESS on success, or CONN_OPERATION_FAILED
  1083. * on failure. Sets conn->last_ldap_error and conn->last_operation;
  1084. */
  1085. ConnResult
  1086. conn_connect(Repl_Connection *conn)
  1087. {
  1088. int optdata;
  1089. int secure = 0;
  1090. char* binddn = NULL;
  1091. struct berval *creds = NULL;
  1092. ConnResult return_value = CONN_OPERATION_SUCCESS;
  1093. int pw_ret = 1;
  1094. PR_Lock(conn->lock);
  1095. /* Connection already open, just return SUCCESS */
  1096. if(conn->state == STATE_CONNECTED){
  1097. PR_Unlock(conn->lock);
  1098. return return_value;
  1099. }
  1100. if (conn->flag_agmt_changed) {
  1101. /* So far we cannot change Hostname and Port */
  1102. /* slapi_ch_free((void **)&conn->hostname); */
  1103. /* conn->hostname = agmt_get_hostname(conn->agmt); */
  1104. /* conn->port = agmt_get_port(conn->agmt); */
  1105. slapi_ch_free((void **)&conn->binddn);
  1106. conn->binddn = agmt_get_binddn(conn->agmt);
  1107. conn->bindmethod = agmt_get_bindmethod(conn->agmt);
  1108. conn->transport_flags = agmt_get_transport_flags(conn->agmt);
  1109. conn->timeout.tv_sec = agmt_get_timeout(conn->agmt);
  1110. conn->flag_agmt_changed = 0;
  1111. conn->port = agmt_get_port(conn->agmt); /* port could be updated */
  1112. slapi_ch_free((void **)&conn->plain);
  1113. }
  1114. creds = agmt_get_credentials(conn->agmt);
  1115. if (conn->plain == NULL) {
  1116. char *plain = NULL;
  1117. /* kexcoff: for reversible encryption */
  1118. /* We need to test the return code of pw_rever_decode in order to decide
  1119. * if a free for plain will be needed (pw_ret == 0) or not (pw_ret != 0) */
  1120. pw_ret = pw_rever_decode(creds->bv_val, &plain, type_nsds5ReplicaCredentials);
  1121. /* Pb occured in decryption: stop now, binding will fail */
  1122. if ( pw_ret == -1 )
  1123. {
  1124. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1125. "%s: Decoding of the credentials failed.\n",
  1126. agmt_get_long_name(conn->agmt));
  1127. return_value = CONN_OPERATION_FAILED;
  1128. conn->last_ldap_error = LDAP_INVALID_CREDENTIALS;
  1129. conn->state = STATE_DISCONNECTED;
  1130. goto done;
  1131. } /* Else, does not mean that the plain is correct, only means the we had no internal
  1132. decoding pb */
  1133. conn->plain = slapi_ch_strdup (plain);
  1134. if (!pw_ret) slapi_ch_free((void**)&plain);
  1135. }
  1136. /* ugaston: if SSL has been selected in the replication agreement, SSL client
  1137. * initialisation should be done before ever trying to open any connection at all.
  1138. */
  1139. if (conn->transport_flags == TRANSPORT_FLAG_TLS) {
  1140. secure = SLAPI_LDAP_INIT_FLAG_startTLS;
  1141. } else if (conn->transport_flags == TRANSPORT_FLAG_SSL) {
  1142. secure = SLAPI_LDAP_INIT_FLAG_SSL;
  1143. }
  1144. if (secure > 0) {
  1145. if (!NSS_IsInitialized()) {
  1146. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1147. "%s: SSL Not Initialized, Replication over SSL FAILED\n",
  1148. agmt_get_long_name(conn->agmt));
  1149. conn->last_ldap_error = LDAP_INAPPROPRIATE_AUTH;
  1150. conn->last_operation = CONN_INIT;
  1151. return_value = CONN_SSL_NOT_ENABLED;
  1152. goto done;
  1153. }
  1154. }
  1155. if (return_value == CONN_OPERATION_SUCCESS) {
  1156. #if !defined(USE_OPENLDAP)
  1157. int io_timeout_ms;
  1158. #endif
  1159. /* Now we initialize the LDAP Structure and set options */
  1160. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1161. "%s: Trying %s%s slapi_ldap_init_ext\n",
  1162. agmt_get_long_name(conn->agmt),
  1163. secure ? "secure" : "non-secure",
  1164. (secure == SLAPI_LDAP_INIT_FLAG_startTLS) ? " startTLS" : "");
  1165. /* shared = 1 because we will read results from a second thread */
  1166. if (conn->ld) {
  1167. /* Since we call slapi_ldap_init, we must call slapi_ldap_unbind */
  1168. /* ldap_unbind internally calls ldap_ld_free */
  1169. slapi_ldap_unbind(conn->ld);
  1170. }
  1171. conn->ld = slapi_ldap_init_ext(NULL, conn->hostname, conn->port, secure, 1, NULL);
  1172. if (NULL == conn->ld)
  1173. {
  1174. return_value = CONN_OPERATION_FAILED;
  1175. conn->state = STATE_DISCONNECTED;
  1176. conn->last_operation = CONN_INIT;
  1177. conn->last_ldap_error = LDAP_LOCAL_ERROR;
  1178. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1179. "%s: Failed to establish %s%sconnection to the consumer\n",
  1180. agmt_get_long_name(conn->agmt),
  1181. secure ? "secure " : "",
  1182. (secure == SLAPI_LDAP_INIT_FLAG_startTLS) ? "startTLS " : "");
  1183. goto done;
  1184. }
  1185. /* slapi_ch_strdup is OK with NULL strings */
  1186. binddn = slapi_ch_strdup(conn->binddn);
  1187. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1188. "%s: binddn = %s, passwd = %s\n",
  1189. agmt_get_long_name(conn->agmt),
  1190. binddn?binddn:"NULL", creds->bv_val?creds->bv_val:"NULL");
  1191. /* Set some options for the connection. */
  1192. optdata = LDAP_DEREF_NEVER; /* Don't dereference aliases */
  1193. ldap_set_option(conn->ld, LDAP_OPT_DEREF, &optdata);
  1194. optdata = LDAP_VERSION3; /* We need LDAP version 3 */
  1195. ldap_set_option(conn->ld, LDAP_OPT_PROTOCOL_VERSION, &optdata);
  1196. /* Don't chase any referrals (although we shouldn't get any) */
  1197. ldap_set_option(conn->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1198. /* override the default timeout with the specified timeout */
  1199. #if defined(USE_OPENLDAP)
  1200. ldap_set_option(conn->ld, LDAP_OPT_NETWORK_TIMEOUT, &conn->timeout);
  1201. #else
  1202. io_timeout_ms = conn->timeout.tv_sec * 1000 + conn->timeout.tv_usec / 1000;
  1203. prldap_set_session_option(conn->ld, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT,
  1204. io_timeout_ms);
  1205. #endif
  1206. /* We've got an ld. Now bind to the server. */
  1207. conn->last_operation = CONN_BIND;
  1208. }
  1209. if ( bind_and_check_pwp(conn, binddn, conn->plain) == CONN_OPERATION_FAILED )
  1210. {
  1211. conn->last_ldap_error = slapi_ldap_get_lderrno (conn->ld, NULL, NULL);
  1212. conn->state = STATE_DISCONNECTED;
  1213. return_value = CONN_OPERATION_FAILED;
  1214. }
  1215. else
  1216. {
  1217. conn->last_ldap_error = LDAP_SUCCESS;
  1218. conn->state = STATE_CONNECTED;
  1219. return_value = CONN_OPERATION_SUCCESS;
  1220. }
  1221. done:
  1222. ber_bvfree(creds);
  1223. creds = NULL;
  1224. slapi_ch_free((void**)&binddn);
  1225. if(return_value == CONN_OPERATION_SUCCESS)
  1226. {
  1227. conn->last_ldap_error = LDAP_SUCCESS;
  1228. conn->state = STATE_CONNECTED;
  1229. } else
  1230. {
  1231. close_connection_internal(conn);
  1232. }
  1233. PR_Unlock(conn->lock);
  1234. return return_value;
  1235. }
  1236. static void
  1237. close_connection_internal(Repl_Connection *conn)
  1238. {
  1239. conn->state = STATE_DISCONNECTED;
  1240. conn->status = STATUS_DISCONNECTED;
  1241. conn->supports_ds50_repl = -1;
  1242. conn->supports_ds71_repl = -1;
  1243. conn->supports_ds90_repl = -1;
  1244. /* do this last, to minimize the chance that another thread
  1245. might read conn->state as not disconnected and attempt
  1246. to use conn->ld */
  1247. if (NULL != conn->ld)
  1248. {
  1249. /* Since we call slapi_ldap_init, we must call slapi_ldap_unbind */
  1250. slapi_ldap_unbind(conn->ld);
  1251. }
  1252. conn->ld = NULL;
  1253. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1254. "%s: Disconnected from the consumer\n", agmt_get_long_name(conn->agmt));
  1255. }
  1256. void
  1257. conn_disconnect(Repl_Connection *conn)
  1258. {
  1259. PR_ASSERT(NULL != conn);
  1260. PR_Lock(conn->lock);
  1261. close_connection_internal(conn);
  1262. PR_Unlock(conn->lock);
  1263. }
  1264. /*
  1265. * Determine if the remote replica supports DS 5.0 replication.
  1266. * Return codes:
  1267. * CONN_SUPPORTS_DS5_REPL - the remote replica suport DS5 replication
  1268. * CONN_DOES_NOT_SUPPORT_DS5_REPL - the remote replica does not
  1269. * support DS5 replication.
  1270. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1271. * replica supports DS5 replication.
  1272. * CONN_NOT_CONNECTED - no connection was active.
  1273. */
  1274. ConnResult
  1275. conn_replica_supports_ds5_repl(Repl_Connection *conn)
  1276. {
  1277. ConnResult return_value;
  1278. int ldap_rc;
  1279. PR_Lock(conn->lock);
  1280. if (conn_connected(conn))
  1281. {
  1282. if (conn->supports_ds50_repl == -1) {
  1283. LDAPMessage *res = NULL;
  1284. LDAPMessage *entry = NULL;
  1285. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1286. conn->status = STATUS_SEARCHING;
  1287. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1288. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1289. NULL /* server controls */, NULL /* client controls */,
  1290. &conn->timeout, LDAP_NO_LIMIT, &res);
  1291. if (LDAP_SUCCESS == ldap_rc)
  1292. {
  1293. conn->supports_ds50_repl = 0;
  1294. entry = ldap_first_entry(conn->ld, res);
  1295. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_NSDS50_UPDATE_INFO_CONTROL_OID))
  1296. {
  1297. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1298. }
  1299. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS50_REPLICATION_REQUEST_OID))
  1300. {
  1301. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1302. }
  1303. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_END_NSDS50_REPLICATION_REQUEST_OID))
  1304. {
  1305. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1306. }
  1307. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID))
  1308. {
  1309. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1310. }
  1311. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_RESPONSE_OID))
  1312. {
  1313. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1314. }
  1315. else
  1316. {
  1317. conn->supports_ds50_repl = 1;
  1318. return_value = CONN_SUPPORTS_DS5_REPL;
  1319. }
  1320. }
  1321. else
  1322. {
  1323. if (IS_DISCONNECT_ERROR(ldap_rc))
  1324. {
  1325. conn->last_ldap_error = ldap_rc; /* specific reason */
  1326. close_connection_internal(conn);
  1327. return_value = CONN_NOT_CONNECTED;
  1328. }
  1329. else
  1330. {
  1331. return_value = CONN_OPERATION_FAILED;
  1332. }
  1333. }
  1334. if (NULL != res)
  1335. ldap_msgfree(res);
  1336. }
  1337. else {
  1338. return_value = conn->supports_ds50_repl ? CONN_SUPPORTS_DS5_REPL : CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1339. }
  1340. }
  1341. else
  1342. {
  1343. /* Not connected */
  1344. return_value = CONN_NOT_CONNECTED;
  1345. }
  1346. PR_Unlock(conn->lock);
  1347. return return_value;
  1348. }
  1349. /*
  1350. * Determine if the remote replica supports DS 7.1 replication.
  1351. * Return codes:
  1352. * CONN_SUPPORTS_DS71_REPL - the remote replica suport DS7.1 replication
  1353. * CONN_DOES_NOT_SUPPORT_DS71_REPL - the remote replica does not
  1354. * support DS7.1 replication.
  1355. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1356. * replica supports DS5 replication.
  1357. * CONN_NOT_CONNECTED - no connection was active.
  1358. */
  1359. ConnResult
  1360. conn_replica_supports_ds71_repl(Repl_Connection *conn)
  1361. {
  1362. ConnResult return_value;
  1363. int ldap_rc;
  1364. PR_Lock(conn->lock);
  1365. if (conn_connected(conn))
  1366. {
  1367. if (conn->supports_ds71_repl == -1) {
  1368. LDAPMessage *res = NULL;
  1369. LDAPMessage *entry = NULL;
  1370. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1371. conn->status = STATUS_SEARCHING;
  1372. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1373. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1374. NULL /* server controls */, NULL /* client controls */,
  1375. &conn->timeout, LDAP_NO_LIMIT, &res);
  1376. if (LDAP_SUCCESS == ldap_rc)
  1377. {
  1378. conn->supports_ds71_repl = 0;
  1379. entry = ldap_first_entry(conn->ld, res);
  1380. if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS71_REPLICATION_ENTRY_REQUEST_OID))
  1381. {
  1382. return_value = CONN_DOES_NOT_SUPPORT_DS71_REPL;
  1383. }
  1384. else
  1385. {
  1386. conn->supports_ds71_repl = 1;
  1387. return_value = CONN_SUPPORTS_DS71_REPL;
  1388. }
  1389. }
  1390. else
  1391. {
  1392. if (IS_DISCONNECT_ERROR(ldap_rc))
  1393. {
  1394. conn->last_ldap_error = ldap_rc; /* specific reason */
  1395. close_connection_internal(conn);
  1396. return_value = CONN_NOT_CONNECTED;
  1397. }
  1398. else
  1399. {
  1400. return_value = CONN_OPERATION_FAILED;
  1401. }
  1402. }
  1403. if (NULL != res)
  1404. ldap_msgfree(res);
  1405. }
  1406. else {
  1407. return_value = conn->supports_ds71_repl ? CONN_SUPPORTS_DS71_REPL : CONN_DOES_NOT_SUPPORT_DS71_REPL;
  1408. }
  1409. }
  1410. else
  1411. {
  1412. /* Not connected */
  1413. return_value = CONN_NOT_CONNECTED;
  1414. }
  1415. PR_Unlock(conn->lock);
  1416. return return_value;
  1417. }
  1418. /*
  1419. * Determine if the remote replica supports DS 9.0 replication.
  1420. * Return codes:
  1421. * CONN_SUPPORTS_DS90_REPL - the remote replica suport DS5 replication
  1422. * CONN_DOES_NOT_SUPPORT_DS90_REPL - the remote replica does not
  1423. * support DS9.0 replication.
  1424. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1425. * replica supports DS9.0 replication.
  1426. * CONN_NOT_CONNECTED - no connection was active.
  1427. */
  1428. ConnResult
  1429. conn_replica_supports_ds90_repl(Repl_Connection *conn)
  1430. {
  1431. ConnResult return_value;
  1432. int ldap_rc;
  1433. PR_Lock(conn->lock);
  1434. if (conn_connected(conn))
  1435. {
  1436. if (conn->supports_ds90_repl == -1) {
  1437. LDAPMessage *res = NULL;
  1438. LDAPMessage *entry = NULL;
  1439. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1440. conn->status = STATUS_SEARCHING;
  1441. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1442. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1443. NULL /* server controls */, NULL /* client controls */,
  1444. &conn->timeout, LDAP_NO_LIMIT, &res);
  1445. if (LDAP_SUCCESS == ldap_rc)
  1446. {
  1447. conn->supports_ds90_repl = 0;
  1448. entry = ldap_first_entry(conn->ld, res);
  1449. if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS90_REPLICATION_REQUEST_OID))
  1450. {
  1451. return_value = CONN_DOES_NOT_SUPPORT_DS90_REPL;
  1452. }
  1453. else
  1454. {
  1455. conn->supports_ds90_repl = 1;
  1456. return_value = CONN_SUPPORTS_DS90_REPL;
  1457. }
  1458. }
  1459. else
  1460. {
  1461. if (IS_DISCONNECT_ERROR(ldap_rc))
  1462. {
  1463. conn->last_ldap_error = ldap_rc; /* specific reason */
  1464. close_connection_internal(conn);
  1465. return_value = CONN_NOT_CONNECTED;
  1466. }
  1467. else
  1468. {
  1469. return_value = CONN_OPERATION_FAILED;
  1470. }
  1471. }
  1472. if (NULL != res)
  1473. ldap_msgfree(res);
  1474. }
  1475. else
  1476. {
  1477. return_value = conn->supports_ds90_repl ? CONN_SUPPORTS_DS90_REPL : CONN_DOES_NOT_SUPPORT_DS90_REPL;
  1478. }
  1479. }
  1480. else
  1481. {
  1482. /* Not connected */
  1483. return_value = CONN_NOT_CONNECTED;
  1484. }
  1485. PR_Unlock(conn->lock);
  1486. return return_value;
  1487. }
  1488. /* Determine if the replica is read-only */
  1489. ConnResult
  1490. conn_replica_is_readonly(Repl_Connection *conn)
  1491. {
  1492. ReplicaId rid = agmt_get_consumer_rid( (Repl_Agmt *) conn->agmt, conn );
  1493. if (rid == READ_ONLY_REPLICA_ID)
  1494. {
  1495. return CONN_IS_READONLY;
  1496. } else
  1497. {
  1498. return CONN_IS_NOT_READONLY;
  1499. }
  1500. }
  1501. /*
  1502. * Return 1 if "value" is a value of attribute type "type" in entry "entry".
  1503. * Otherwise, return 0.
  1504. */
  1505. static int
  1506. attribute_string_value_present(LDAP *ld, LDAPMessage *entry, const char *type,
  1507. const char *value)
  1508. {
  1509. int return_value = 0;
  1510. ber_len_t vallen;
  1511. if (NULL != entry)
  1512. {
  1513. char *atype = NULL;
  1514. BerElement *ber = NULL;
  1515. vallen = strlen(value);
  1516. atype = ldap_first_attribute(ld, entry, &ber);
  1517. while (NULL != atype && 0 == return_value)
  1518. {
  1519. if (strcasecmp(atype, type) == 0)
  1520. {
  1521. struct berval **vals = ldap_get_values_len(ld, entry, atype);
  1522. int i;
  1523. for (i = 0; return_value == 0 && NULL != vals && NULL != vals[i]; i++)
  1524. {
  1525. if ((vallen == vals[i]->bv_len) && !strncmp(vals[i]->bv_val, value, vallen))
  1526. {
  1527. return_value = 1;
  1528. }
  1529. }
  1530. if (NULL != vals)
  1531. {
  1532. ldap_value_free_len(vals);
  1533. }
  1534. }
  1535. ldap_memfree(atype);
  1536. atype = ldap_next_attribute(ld, entry, ber);
  1537. }
  1538. if (NULL != ber)
  1539. ber_free(ber, 0);
  1540. /* The last atype has not been freed yet */
  1541. if (NULL != atype)
  1542. ldap_memfree(atype);
  1543. }
  1544. return return_value;
  1545. }
  1546. /* It returns the objectclasses and attributetypes of the remote schema
  1547. * in the form of berval arrays.
  1548. * In case of success, the caller must free those berval arrays with ber_bvecfree
  1549. * */
  1550. static ConnResult
  1551. supplier_read_consumer_definitions(Repl_Connection *conn, struct berval ***remote_objectclasses, struct berval ***remote_attributetypes)
  1552. {
  1553. ConnResult return_value = CONN_OPERATION_SUCCESS;
  1554. struct berval **remote_schema_objectclasses_bervals = NULL;
  1555. struct berval **remote_schema_attributetypes_bervals = NULL;
  1556. *remote_objectclasses = NULL;
  1557. *remote_attributetypes = NULL;
  1558. /* read the objectclass then the attribytetype from the remote schema */
  1559. return_value = conn_read_entry_attribute(conn, "cn=schema", "objectclasses", &remote_schema_objectclasses_bervals);
  1560. if (return_value == CONN_OPERATION_SUCCESS) {
  1561. *remote_objectclasses = remote_schema_objectclasses_bervals;
  1562. return_value = conn_read_entry_attribute(conn, "cn=schema", "attributetypes", &remote_schema_attributetypes_bervals);
  1563. if (return_value == CONN_OPERATION_SUCCESS) {
  1564. *remote_attributetypes = remote_schema_attributetypes_bervals;
  1565. } else {
  1566. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1567. "%s: Fail to retrieve the remote schema attributetypes\n",
  1568. agmt_get_long_name(conn->agmt));
  1569. }
  1570. } else {
  1571. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1572. "%s: Fail to retrieve the remote schema objectclasses\n",
  1573. agmt_get_long_name(conn->agmt));
  1574. }
  1575. if (return_value != CONN_OPERATION_SUCCESS) {
  1576. /* in case of failure free everything */
  1577. *remote_objectclasses = NULL;
  1578. if (remote_schema_objectclasses_bervals) {
  1579. ber_bvecfree(remote_schema_objectclasses_bervals);
  1580. }
  1581. *remote_attributetypes = NULL;
  1582. if (remote_schema_attributetypes_bervals) {
  1583. ber_bvecfree(remote_schema_attributetypes_bervals);
  1584. }
  1585. }
  1586. return return_value;
  1587. }
  1588. //
  1589. static PRBool
  1590. update_consumer_schema(Repl_Connection *conn)
  1591. {
  1592. struct berval **remote_schema_objectclasses_bervals = NULL;
  1593. struct berval **remote_schema_attributetypes_bervals = NULL;
  1594. PRBool ok_to_send_schema = PR_TRUE;
  1595. if (supplier_read_consumer_definitions(conn, &remote_schema_objectclasses_bervals, &remote_schema_attributetypes_bervals) == CONN_OPERATION_SUCCESS) {
  1596. if (schema_objectclasses_superset_check(remote_schema_objectclasses_bervals, OC_SUPPLIER) ||
  1597. schema_attributetypes_superset_check(remote_schema_attributetypes_bervals, OC_SUPPLIER)) {
  1598. /* The consumer contains definitions that needs to be learned */
  1599. supplier_learn_new_definitions(remote_schema_objectclasses_bervals, remote_schema_attributetypes_bervals);
  1600. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1601. "[S] Schema %s must not be overwritten (set replication log for additional info)\n",
  1602. agmt_get_long_name(conn->agmt));
  1603. ok_to_send_schema = PR_FALSE;
  1604. }
  1605. ber_bvecfree(remote_schema_objectclasses_bervals);
  1606. ber_bvecfree(remote_schema_attributetypes_bervals);
  1607. } else {
  1608. /* We can not be sure, be conservative and not send the schema */
  1609. ok_to_send_schema = PR_FALSE;
  1610. }
  1611. return ok_to_send_schema;
  1612. }
  1613. /*
  1614. * Read the remote server's schema entry, then read the local schema entry,
  1615. * and compare the nsschemacsn attribute. If the local csn is newer, or
  1616. * the remote csn is absent, push the schema down to the consumer.
  1617. * Return codes:
  1618. * CONN_SCHEMA_UPDATED if the schema was pushed successfully
  1619. * CONN_SCHEMA_NO_UPDATE_NEEDED if the schema was as new or newer than
  1620. * the local server's schema
  1621. * CONN_OPERATION_FAILED if an error occurred
  1622. * CONN_NOT_CONNECTED if no connection was active
  1623. * NOTE: Should only be called when a replication session has been
  1624. * established by sending a startReplication extended operation.
  1625. */
  1626. ConnResult
  1627. conn_push_schema(Repl_Connection *conn, CSN **remotecsn)
  1628. {
  1629. ConnResult return_value = CONN_OPERATION_SUCCESS;
  1630. char *nsschemacsn = "nsschemacsn";
  1631. Slapi_Entry **entries = NULL;
  1632. Slapi_Entry *schema_entry = NULL;
  1633. CSN *localcsn = NULL;
  1634. Slapi_PBlock *spb = NULL;
  1635. char localcsnstr[CSN_STRSIZE + 1] = {0};
  1636. char remotecnsstr[CSN_STRSIZE+1] = {0};
  1637. if (!remotecsn)
  1638. {
  1639. return_value = CONN_OPERATION_FAILED;
  1640. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "NULL remote CSN\n");
  1641. }
  1642. else if (!conn_connected_locked(conn, 0 /* not locked */))
  1643. {
  1644. return_value = CONN_NOT_CONNECTED;
  1645. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1646. "%s: Schema replication update failed: not connected to consumer\n",
  1647. agmt_get_long_name(conn->agmt));
  1648. }
  1649. else
  1650. {
  1651. localcsn = dup_global_schema_csn();
  1652. if (NULL == localcsn)
  1653. {
  1654. /* Local server has epoch CSN, so don't push schema */
  1655. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1656. }
  1657. else if ( *remotecsn && csn_compare(localcsn, *remotecsn) <= 0 )
  1658. {
  1659. /* Local server schema is not newer than the remote one */
  1660. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1661. }
  1662. else
  1663. {
  1664. if (*remotecsn) {
  1665. csn_as_string (*remotecsn, PR_FALSE, remotecnsstr);
  1666. csn_as_string (localcsn, PR_FALSE, localcsnstr);
  1667. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1668. "[S] Checking consumer schema localcsn:%s / remotecsn:%s\n", localcsnstr, remotecnsstr);
  1669. } else {
  1670. csn_as_string (localcsn, PR_FALSE, localcsnstr);
  1671. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1672. "[S] Checking consumer schema localcsn:%s / remotecsn:NULL\n", localcsnstr);
  1673. }
  1674. if (!update_consumer_schema(conn)) {
  1675. /* At least one schema definition (attributetypes/objectclasses) of the consumer
  1676. * is a superset of the supplier.
  1677. * It is not possible push the schema immediately.
  1678. * Note: in update_consumer_schema, it may update the local supplier schema.
  1679. * So it could be possible that a second attempt (right now) of update_consumer_schema
  1680. * would be successful
  1681. */
  1682. slapi_log_error(SLAPI_LOG_REPL, "schema",
  1683. "[S] schema definitions may have been learned\n");
  1684. if (!update_consumer_schema(conn)) {
  1685. slapi_log_error(SLAPI_LOG_REPL, "schema",
  1686. "[S] learned definitions are not suffisant to try to push the schema \n");
  1687. return_value = CONN_OPERATION_FAILED;
  1688. }
  1689. }
  1690. if (return_value == CONN_OPERATION_SUCCESS) {
  1691. struct berval **remote_schema_csn_bervals = NULL;
  1692. /* Get remote server's schema */
  1693. return_value = conn_read_entry_attribute(conn, "cn=schema", nsschemacsn,
  1694. &remote_schema_csn_bervals);
  1695. if (CONN_OPERATION_SUCCESS == return_value) {
  1696. if (NULL != remote_schema_csn_bervals && NULL != remote_schema_csn_bervals[0]) {
  1697. char remotecsnstr[CSN_STRSIZE + 1] = {0};
  1698. memcpy(remotecsnstr, remote_schema_csn_bervals[0]->bv_val,
  1699. remote_schema_csn_bervals[0]->bv_len);
  1700. remotecsnstr[remote_schema_csn_bervals[0]->bv_len] = '\0';
  1701. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1702. "[S] Reread remotecsn:%s\n", remotecsnstr);
  1703. *remotecsn = csn_new_by_string(remotecsnstr);
  1704. if (*remotecsn && (csn_compare(localcsn, *remotecsn) <= 0)) {
  1705. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1706. }
  1707. /* Need to free the remote_schema_csn_bervals */
  1708. ber_bvecfree(remote_schema_csn_bervals);
  1709. }
  1710. if (return_value == CONN_OPERATION_SUCCESS) {
  1711. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1712. "Schema checking successful: ok to push the schema (%s)\n", agmt_get_long_name(conn->agmt));
  1713. }
  1714. }
  1715. }
  1716. }
  1717. }
  1718. if (CONN_OPERATION_SUCCESS == return_value)
  1719. {
  1720. /* We know we need to push the schema out. */
  1721. LDAPMod ocmod = {0};
  1722. LDAPMod atmod = {0};
  1723. LDAPMod csnmod = {0};
  1724. LDAPMod *attrs[4] = {0};
  1725. int numvalues = 0;
  1726. Slapi_Attr *attr = NULL;
  1727. char *csnvalues[2];
  1728. ocmod.mod_type = "objectclasses";
  1729. ocmod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1730. ocmod.mod_bvalues = NULL;
  1731. atmod.mod_type = "attributetypes";
  1732. atmod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1733. atmod.mod_bvalues = NULL;
  1734. csnmod.mod_type = nsschemacsn;
  1735. csnmod.mod_op = LDAP_MOD_REPLACE;
  1736. csn_as_string (localcsn, PR_FALSE, localcsnstr);
  1737. csnvalues[0] = localcsnstr;
  1738. csnvalues[1] = NULL;
  1739. csnmod.mod_values = csnvalues;
  1740. attrs[0] = &ocmod;
  1741. attrs[1] = &atmod;
  1742. attrs[2] = &csnmod;
  1743. attrs[3] = NULL;
  1744. return_value = CONN_OPERATION_FAILED; /* assume failure */
  1745. /* Get local schema */
  1746. spb = slapi_search_internal("cn=schema", LDAP_SCOPE_BASE, "(objectclass=*)",
  1747. NULL /* controls */, NULL /* schema_csn_attrs */, 0 /* attrsonly */);
  1748. slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1749. if (NULL == entries || NULL == entries[0])
  1750. {
  1751. /* Whoops - couldn't read our own schema! */
  1752. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1753. "%s: Error: unable to read local schema definitions.\n",
  1754. agmt_get_long_name(conn->agmt));
  1755. return_value = CONN_OPERATION_FAILED;
  1756. }
  1757. else
  1758. {
  1759. schema_entry = entries[0];
  1760. if (slapi_entry_attr_find(schema_entry, "objectclasses", &attr) != -1)
  1761. {
  1762. int i, ind;
  1763. Slapi_Value *value;
  1764. slapi_attr_get_numvalues(attr, &numvalues);
  1765. ocmod.mod_bvalues = (struct berval **)slapi_ch_malloc((numvalues + 1) *
  1766. sizeof(struct berval *));
  1767. for (i = 0, ind = slapi_attr_first_value(attr, &value);
  1768. ind != -1; ind = slapi_attr_next_value(attr, ind, &value), i++)
  1769. {
  1770. /* XXXggood had to cast away const below */
  1771. ocmod.mod_bvalues[i] = (struct berval *)slapi_value_get_berval(value);
  1772. }
  1773. ocmod.mod_bvalues[numvalues] = NULL;
  1774. if (slapi_entry_attr_find(schema_entry, "attributetypes", &attr) != -1)
  1775. {
  1776. ConnResult result;
  1777. slapi_attr_get_numvalues(attr, &numvalues);
  1778. atmod.mod_bvalues = (struct berval **)slapi_ch_malloc((numvalues + 1) *
  1779. sizeof(struct berval *));
  1780. for (i = 0, ind = slapi_attr_first_value(attr, &value);
  1781. ind != -1; ind = slapi_attr_next_value(attr, ind, &value), i++)
  1782. {
  1783. /* XXXggood had to cast away const below */
  1784. atmod.mod_bvalues[i] = (struct berval *)slapi_value_get_berval(value);
  1785. }
  1786. atmod.mod_bvalues[numvalues] = NULL;
  1787. result = conn_send_modify(conn, "cn=schema", attrs, NULL, NULL); /* DBDB: this needs to be fixed to use async */
  1788. result = conn_read_result(conn,NULL);
  1789. switch (result)
  1790. {
  1791. case CONN_OPERATION_FAILED:
  1792. {
  1793. int ldaperr = -1, optype = -1;
  1794. conn_get_error(conn, &optype, &ldaperr);
  1795. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1796. "%s: Schema replication update failed: %s\n",
  1797. agmt_get_long_name(conn->agmt),
  1798. ldaperr == -1 ? "Unknown Error" : ldap_err2string(ldaperr));
  1799. return_value = CONN_OPERATION_FAILED;
  1800. break;
  1801. }
  1802. case CONN_NOT_CONNECTED:
  1803. return_value = CONN_NOT_CONNECTED;
  1804. break;
  1805. case CONN_OPERATION_SUCCESS:
  1806. return_value = CONN_SCHEMA_UPDATED;
  1807. break;
  1808. default:
  1809. break;
  1810. }
  1811. }
  1812. }
  1813. else
  1814. {
  1815. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1816. "%s: Schema replication update failed: "
  1817. "unable to prepare schema entry for transmission.\n",
  1818. agmt_get_long_name(conn->agmt));
  1819. }
  1820. }
  1821. /* slapi_ch_free accepts NULL pointer */
  1822. slapi_ch_free((void **)&ocmod.mod_bvalues);
  1823. slapi_ch_free((void **)&atmod.mod_bvalues);
  1824. }
  1825. if (NULL != spb)
  1826. {
  1827. slapi_free_search_results_internal(spb);
  1828. slapi_pblock_destroy(spb);
  1829. spb = NULL;
  1830. }
  1831. if (NULL != localcsn)
  1832. {
  1833. csn_free(&localcsn);
  1834. }
  1835. return return_value;
  1836. }
  1837. void
  1838. conn_set_timeout(Repl_Connection *conn, long timeout)
  1839. {
  1840. PR_ASSERT(NULL != conn);
  1841. PR_ASSERT(timeout >= 0);
  1842. PR_Lock(conn->lock);
  1843. conn->timeout.tv_sec = timeout;
  1844. PR_Unlock(conn->lock);
  1845. }
  1846. long
  1847. conn_get_timeout(Repl_Connection *conn)
  1848. {
  1849. long retval = 0;
  1850. PR_ASSERT(NULL != conn);
  1851. retval = conn->timeout.tv_sec;
  1852. return retval;
  1853. }
  1854. LDAP *
  1855. conn_get_ldap(Repl_Connection *conn)
  1856. {
  1857. if(conn){
  1858. return conn->ld;
  1859. } else {
  1860. return NULL;
  1861. }
  1862. }
  1863. void conn_set_agmt_changed(Repl_Connection *conn)
  1864. {
  1865. PR_ASSERT(NULL != conn);
  1866. PR_Lock(conn->lock);
  1867. if (NULL != conn->agmt)
  1868. conn->flag_agmt_changed = 1;
  1869. PR_Unlock(conn->lock);
  1870. }
  1871. static const char *
  1872. bind_method_to_mech(int bindmethod)
  1873. {
  1874. switch (bindmethod) {
  1875. case BINDMETHOD_SSL_CLIENTAUTH:
  1876. return LDAP_SASL_EXTERNAL;
  1877. break;
  1878. case BINDMETHOD_SASL_GSSAPI:
  1879. return "GSSAPI";
  1880. break;
  1881. case BINDMETHOD_SASL_DIGEST_MD5:
  1882. return "DIGEST-MD5";
  1883. break;
  1884. default: /* anything else */
  1885. return LDAP_SASL_SIMPLE;
  1886. }
  1887. return LDAP_SASL_SIMPLE;
  1888. }
  1889. const char*
  1890. conn_get_bindmethod(Repl_Connection *conn)
  1891. {
  1892. return (bind_method_to_mech(conn->bindmethod));
  1893. }
  1894. /*
  1895. * Check the result of an ldap BIND operation to see we it
  1896. * contains the expiration controls
  1897. * return: -1 error, not bound
  1898. * 0, OK bind has succeeded
  1899. */
  1900. static int
  1901. bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password)
  1902. {
  1903. LDAPControl **ctrls = NULL;
  1904. LDAP *ld = conn->ld;
  1905. int rc;
  1906. const char *mech = bind_method_to_mech(conn->bindmethod);
  1907. rc = slapi_ldap_bind(conn->ld, binddn, password, mech, NULL,
  1908. &ctrls, &conn->timeout, NULL);
  1909. if ( rc == LDAP_SUCCESS )
  1910. {
  1911. if (conn->last_ldap_error != rc)
  1912. {
  1913. int log_level = SLAPI_LOG_FATAL;
  1914. if (conn->last_ldap_error == LDAP_LOCAL_ERROR){
  1915. /*
  1916. * Local errors are not logged by default, so when we recover
  1917. * from local errors we must also not log it by default.
  1918. */
  1919. log_level = SLAPI_LOG_REPL;
  1920. }
  1921. conn->last_ldap_error = rc;
  1922. slapi_log_error(log_level, repl_plugin_name,
  1923. "%s: Replication bind with %s auth resumed\n",
  1924. agmt_get_long_name(conn->agmt),
  1925. mech ? mech : "SIMPLE");
  1926. }
  1927. if ( ctrls )
  1928. {
  1929. int i;
  1930. for( i = 0; ctrls[ i ] != NULL; ++i )
  1931. {
  1932. if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRED)) )
  1933. {
  1934. /* Bind is successfull but password has expired */
  1935. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1936. "%s: Successfully bound %s to consumer, "
  1937. "but password has expired on consumer.\n",
  1938. agmt_get_long_name(conn->agmt), binddn);
  1939. }
  1940. else if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRING)) )
  1941. {
  1942. /* The password is expiring in n seconds */
  1943. if ( (ctrls[ i ]->ldctl_value.bv_val != NULL) &&
  1944. (ctrls[ i ]->ldctl_value.bv_len > 0) )
  1945. {
  1946. int password_expiring = atoi( ctrls[ i ]->ldctl_value.bv_val );
  1947. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1948. "%s: Successfully bound %s to consumer, "
  1949. "but password is expiring on consumer in %d seconds.\n",
  1950. agmt_get_long_name(conn->agmt), binddn, password_expiring);
  1951. }
  1952. }
  1953. }
  1954. ldap_controls_free( ctrls );
  1955. }
  1956. return (CONN_OPERATION_SUCCESS);
  1957. }
  1958. else
  1959. {
  1960. ldap_controls_free( ctrls );
  1961. /* Do not report the same error over and over again
  1962. * unless replication level logging is enabled. */
  1963. if (conn->last_ldap_error != rc)
  1964. {
  1965. char *errmsg = NULL;
  1966. conn->last_ldap_error = rc;
  1967. /* errmsg is a pointer directly into the ld structure - do not free */
  1968. rc = slapi_ldap_get_lderrno( ld, NULL, &errmsg );
  1969. slapi_log_error(rc == LDAP_LOCAL_ERROR ? SLAPI_LOG_REPL : SLAPI_LOG_FATAL,
  1970. repl_plugin_name,
  1971. "%s: Replication bind with %s auth failed: LDAP error %d (%s) (%s)\n",
  1972. agmt_get_long_name(conn->agmt),
  1973. mech ? mech : "SIMPLE", rc,
  1974. ldap_err2string(rc), errmsg ? errmsg : "");
  1975. } else {
  1976. char *errmsg = NULL;
  1977. /* errmsg is a pointer directly into the ld structure - do not free */
  1978. rc = slapi_ldap_get_lderrno( ld, NULL, &errmsg );
  1979. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1980. "%s: Replication bind with %s auth failed: LDAP error %d (%s) (%s)\n",
  1981. agmt_get_long_name(conn->agmt),
  1982. mech ? mech : "SIMPLE", rc,
  1983. ldap_err2string(rc), errmsg ? errmsg : "");
  1984. }
  1985. return (CONN_OPERATION_FAILED);
  1986. }
  1987. }
  1988. void
  1989. repl5_set_debug_timeout(const char *val)
  1990. {
  1991. /* val looks like this: seconds[:debuglevel] */
  1992. /* seconds is the number of seconds to wait until turning on the debug level */
  1993. /* this should be less than the ldap connection timeout (default 10 minutes) */
  1994. /* the optional debug level is the error log debugging level to use (default repl) */
  1995. if (val) {
  1996. const char *p = strchr(val, ':');
  1997. s_debug_timeout = atoi(val);
  1998. if (p) {
  1999. s_debug_level = atoi(p+1);
  2000. } else {
  2001. s_debug_level = 8192;
  2002. }
  2003. }
  2004. }
  2005. #ifdef FOR_DEBUGGING
  2006. static time_t
  2007. PRTime2time_t (PRTime tm)
  2008. {
  2009. PRInt64 rt;
  2010. PR_ASSERT (tm);
  2011. LL_DIV(rt, tm, PR_USEC_PER_SEC);
  2012. return (time_t)rt;
  2013. }
  2014. #endif
  2015. static Slapi_Eq_Context
  2016. repl5_start_debug_timeout(int *setlevel)
  2017. {
  2018. Slapi_Eq_Context eqctx = 0;
  2019. if (s_debug_timeout && s_debug_level) {
  2020. time_t now = time(NULL);
  2021. eqctx = slapi_eq_once(repl5_debug_timeout_callback, setlevel,
  2022. s_debug_timeout + now);
  2023. }
  2024. return eqctx;
  2025. }
  2026. static void
  2027. repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel)
  2028. {
  2029. char buf[20];
  2030. if (eqctx && !*setlevel) {
  2031. (void)slapi_eq_cancel(eqctx);
  2032. }
  2033. if (s_debug_timeout && s_debug_level && *setlevel) {
  2034. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  2035. sprintf(buf, "%d", 0);
  2036. config_set_errorlog_level("nsslapd-errorlog-level", buf, NULL, 1);
  2037. }
  2038. }
  2039. static void
  2040. repl5_debug_timeout_callback(time_t when, void *arg)
  2041. {
  2042. int *setlevel = (int *)arg;
  2043. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  2044. char buf[20];
  2045. *setlevel = 1;
  2046. sprintf(buf, "%d", s_debug_level);
  2047. config_set_errorlog_level("nsslapd-errorlog-level", buf, NULL, 1);
  2048. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2049. "repl5_debug_timeout_callback: set debug level to %d at %ld\n",
  2050. s_debug_level, when);
  2051. }
  2052. void
  2053. conn_lock(Repl_Connection *conn)
  2054. {
  2055. if(conn){
  2056. PR_Lock(conn->lock);
  2057. }
  2058. }
  2059. void
  2060. conn_unlock(Repl_Connection *conn)
  2061. {
  2062. if(conn){
  2063. PR_Unlock(conn->lock);
  2064. }
  2065. }