repl5_connection.c 54 KB

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