repl5_connection.c 48 KB

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