repl5_connection.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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. if (!conn_connected(conn)) {
  279. rc = -1;
  280. return_value = CONN_NOT_CONNECTED;
  281. break;
  282. }
  283. rc = ldap_result(conn->ld, LDAP_RES_ANY , 1, &local_timeout, &res);
  284. if (0 != rc)
  285. {
  286. /* Something other than a timeout happened */
  287. break;
  288. }
  289. if (block)
  290. {
  291. /* Did the connection's timeout expire ? */
  292. time_now = time( NULL );
  293. if (conn->timeout.tv_sec <= ( time_now - start_time ))
  294. {
  295. /* We timed out */
  296. rc = 0;
  297. break;
  298. }
  299. /* Otherwise we backoff */
  300. DS_Sleep(PR_MillisecondsToInterval(backoff_time));
  301. if (backoff_time < 1000)
  302. {
  303. backoff_time <<= 1;
  304. }
  305. } else
  306. {
  307. rc = 0;
  308. break;
  309. }
  310. }
  311. repl5_stop_debug_timeout(eqctx, &setlevel);
  312. if (0 == rc)
  313. {
  314. /* Timeout */
  315. rc = ldap_get_lderrno(conn->ld, NULL, NULL);
  316. conn->last_ldap_error = LDAP_TIMEOUT;
  317. return_value = CONN_TIMEOUT;
  318. }
  319. else if ((-1 == rc) && (CONN_NOT_CONNECTED == return_value))
  320. {
  321. /* must not access conn->ld if disconnected in another thread */
  322. /* the other thread that actually did the conn_disconnect() */
  323. /* will set the status and error info */
  324. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  325. "%s: Connection disconnected by another thread\n",
  326. agmt_get_long_name(conn->agmt));
  327. }
  328. else if (-1 == rc)
  329. {
  330. /* Error */
  331. char *s = NULL;
  332. rc = ldap_get_lderrno(conn->ld, NULL, &s);
  333. conn->last_ldap_errmsg = s;
  334. conn->last_ldap_error = rc;
  335. /* some errors will require a disconnect and retry the connection
  336. later */
  337. if (IS_DISCONNECT_ERROR(rc))
  338. {
  339. conn_disconnect(conn);
  340. return_value = CONN_NOT_CONNECTED;
  341. }
  342. else
  343. {
  344. conn->status = STATUS_CONNECTED;
  345. return_value = CONN_OPERATION_FAILED;
  346. }
  347. }
  348. else
  349. {
  350. int err;
  351. char *errmsg = NULL;
  352. char **referrals = NULL;
  353. char *matched = NULL;
  354. if (message_id)
  355. {
  356. *message_id = ldap_msgid(res);
  357. }
  358. rc = ldap_parse_result(conn->ld, res, &err, &matched,
  359. &errmsg, &referrals, &loc_returned_controls,
  360. 0 /* Don't free the result */);
  361. if (IS_DISCONNECT_ERROR(rc))
  362. {
  363. conn->last_ldap_error = rc;
  364. conn_disconnect(conn);
  365. return_value = CONN_NOT_CONNECTED;
  366. }
  367. else if (IS_DISCONNECT_ERROR(err))
  368. {
  369. conn->last_ldap_error = err;
  370. conn_disconnect(conn);
  371. return_value = CONN_NOT_CONNECTED;
  372. }
  373. /* Got a result */
  374. if ((rc == LDAP_SUCCESS) && (err == LDAP_BUSY))
  375. return_value = CONN_BUSY;
  376. else if (retoidp)
  377. {
  378. if (!((rc == LDAP_SUCCESS) && (err == LDAP_BUSY)))
  379. {
  380. if (rc == LDAP_SUCCESS) {
  381. rc = ldap_parse_extended_result(conn->ld, res, retoidp,
  382. retdatap, 0 /* Don't Free it */);
  383. }
  384. conn->last_ldap_error = rc;
  385. return_value = (LDAP_SUCCESS == conn->last_ldap_error ?
  386. CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED);
  387. }
  388. }
  389. else /* regular operation, result returned */
  390. {
  391. if (NULL != returned_controls)
  392. {
  393. *returned_controls = loc_returned_controls;
  394. }
  395. if (LDAP_SUCCESS != rc)
  396. {
  397. conn->last_ldap_error = rc;
  398. }
  399. else
  400. {
  401. conn->last_ldap_error = err;
  402. }
  403. return_value = LDAP_SUCCESS == conn->last_ldap_error ? CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED;
  404. }
  405. /*
  406. * XXXggood do I need to free matched, referrals,
  407. * anything else? Or can I pass NULL for the args
  408. * I'm not interested in?
  409. */
  410. /* Good question! Meanwhile, as RTM aproaches, let's free them... */
  411. slapi_ch_free((void **) &errmsg);
  412. slapi_ch_free((void **) &matched);
  413. charray_free(referrals);
  414. conn->status = STATUS_CONNECTED;
  415. }
  416. if (res) ldap_msgfree(res);
  417. return return_value;
  418. }
  419. ConnResult
  420. conn_read_result(Repl_Connection *conn, int *message_id)
  421. {
  422. return conn_read_result_ex(conn,NULL,NULL,NULL,message_id,1);
  423. }
  424. /* Because the SDK isn't really thread-safe (it can deadlock between
  425. * a thread sending an operation and a thread trying to retrieve a response
  426. * on the same connection), we need to _first_ verify that the connection
  427. * is writable. If it isn't, we can deadlock if we proceed any further...
  428. */
  429. /* Since we're poking around with ldap c sdk internals, we have to
  430. be careful since the PR layer stores different session and socket
  431. info than the NSS SSL layer than the SASL layer - and they all
  432. use different poll functions too
  433. */
  434. static ConnResult
  435. see_if_write_available(Repl_Connection *conn, PRIntervalTime timeout)
  436. {
  437. LDAP_X_PollFD pollstr;
  438. int nfds = 1;
  439. struct ldap_x_ext_io_fns iofns;
  440. int rc = LDAP_SUCCESS;
  441. LDAP_X_EXTIOF_POLL_CALLBACK *ldap_poll;
  442. struct lextiof_session_private *private;
  443. /* get the poll function to use */
  444. memset(&iofns, 0, sizeof(iofns));
  445. iofns.lextiof_size = LDAP_X_EXTIO_FNS_SIZE;
  446. if (ldap_get_option(conn->ld, LDAP_X_OPT_EXTIO_FN_PTRS, &iofns) < 0) {
  447. rc = ldap_get_lderrno(conn->ld, NULL, NULL);
  448. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  449. "%s: Failed call to ldap_get_option to get extiofns in "
  450. "see_if_write_available: LDAP error %d (%s)\n",
  451. agmt_get_long_name(conn->agmt),
  452. rc, ldap_err2string(rc));
  453. conn->last_ldap_error = rc;
  454. return CONN_OPERATION_FAILED;
  455. }
  456. ldap_poll = iofns.lextiof_poll;
  457. /* set up the poll structure */
  458. if (ldap_get_option(conn->ld, LDAP_OPT_DESC, &pollstr.lpoll_fd) < 0) {
  459. rc = ldap_get_lderrno(conn->ld, NULL, NULL);
  460. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  461. "%s: Failed call to ldap_get_option for poll_fd in "
  462. "see_if_write_available: LDAP error %d (%s)\n",
  463. agmt_get_long_name(conn->agmt),
  464. rc, ldap_err2string(rc));
  465. conn->last_ldap_error = rc;
  466. return CONN_OPERATION_FAILED;
  467. }
  468. if (ldap_get_option(conn->ld, LDAP_X_OPT_SOCKETARG,
  469. &pollstr.lpoll_socketarg) < 0) {
  470. rc = ldap_get_lderrno(conn->ld, NULL, NULL);
  471. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  472. "%s: Failed call to ldap_get_option for socketarg in "
  473. "see_if_write_available: LDAP error %d (%s)\n",
  474. agmt_get_long_name(conn->agmt),
  475. rc, ldap_err2string(rc));
  476. conn->last_ldap_error = rc;
  477. return CONN_OPERATION_FAILED;
  478. }
  479. pollstr.lpoll_events = LDAP_X_POLLOUT;
  480. pollstr.lpoll_revents = 0;
  481. private = iofns.lextiof_session_arg;
  482. if (0 == (*ldap_poll)(&pollstr, nfds, timeout, private)) {
  483. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  484. "%s: poll timed out - poll interval [%d]\n",
  485. agmt_get_long_name(conn->agmt),
  486. timeout);
  487. return CONN_TIMEOUT;
  488. }
  489. return CONN_OPERATION_SUCCESS;
  490. }
  491. /*
  492. * Common code to send an LDAPv3 operation and collect the result.
  493. * Return values:
  494. * CONN_OPERATION_SUCCESS - the operation succeeded
  495. * CONN_OPERATION_FAILED - the operation was sent to the consumer
  496. * and failed. Use conn_get_error() to determine the LDAP error
  497. * code.
  498. * CONN_NOT_CONNECTED - no connection is active. The caller should
  499. * use conn_connect() to connect to the replica and bind, then should
  500. * reacquire the replica (if needed).
  501. * CONN_BUSY - the server is busy with previous requests, must wait for a while
  502. * before retrying
  503. * DBDB: also returns the operation's message ID, if it was successfully sent, now that
  504. * we're reading results async.
  505. */
  506. static ConnResult
  507. perform_operation(Repl_Connection *conn, int optype, const char *dn,
  508. LDAPMod **attrs, const char *newrdn, const char *newparent,
  509. int deleteoldrdn, LDAPControl *update_control,
  510. const char *extop_oid, struct berval *extop_payload, int *message_id)
  511. {
  512. int rc;
  513. ConnResult return_value = CONN_OPERATION_FAILED;
  514. LDAPControl *server_controls[3];
  515. /* LDAPControl **loc_returned_controls; */
  516. const char *op_string = NULL;
  517. int msgid = 0;
  518. server_controls[0] = &manageDSAITControl;
  519. server_controls[1] = update_control;
  520. server_controls[2] = NULL;
  521. if (conn_connected(conn))
  522. {
  523. int setlevel = 0;
  524. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  525. return_value = see_if_write_available(
  526. conn, PR_SecondsToInterval(conn->timeout.tv_sec));
  527. if (return_value != CONN_OPERATION_SUCCESS) {
  528. return return_value;
  529. }
  530. conn->last_operation = optype;
  531. switch (optype)
  532. {
  533. case CONN_ADD:
  534. conn->status = STATUS_PROCESSING_ADD;
  535. op_string = "add";
  536. rc = ldap_add_ext(conn->ld, dn, attrs, server_controls,
  537. NULL /* clientctls */, &msgid);
  538. break;
  539. case CONN_MODIFY:
  540. conn->status = STATUS_PROCESSING_MODIFY;
  541. op_string = "modify";
  542. rc = ldap_modify_ext(conn->ld, dn, attrs, server_controls,
  543. NULL /* clientctls */, &msgid);
  544. break;
  545. case CONN_DELETE:
  546. conn->status = STATUS_PROCESSING_DELETE;
  547. op_string = "delete";
  548. rc = ldap_delete_ext(conn->ld, dn, server_controls,
  549. NULL /* clientctls */, &msgid);
  550. break;
  551. case CONN_RENAME:
  552. conn->status = STATUS_PROCESSING_RENAME;
  553. op_string = "rename";
  554. rc = ldap_rename(conn->ld, dn, newrdn, newparent, deleteoldrdn,
  555. server_controls, NULL /* clientctls */, &msgid);
  556. break;
  557. case CONN_EXTENDED_OPERATION:
  558. conn->status = STATUS_PROCESSING_EXTENDED_OPERATION;
  559. op_string = "extended";
  560. rc = ldap_extended_operation(conn->ld, extop_oid, extop_payload,
  561. server_controls, NULL /* clientctls */, &msgid);
  562. }
  563. repl5_stop_debug_timeout(eqctx, &setlevel);
  564. if (LDAP_SUCCESS == rc)
  565. {
  566. /* DBDB: The code that used to be here has been moved for async operation
  567. * Results are now picked up in another thread. All we need to do here is
  568. * queue the operation details in the outstanding operation list.
  569. */
  570. return_value = CONN_OPERATION_SUCCESS;
  571. }
  572. else
  573. {
  574. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  575. "%s: Failed to send %s operation: LDAP error %d (%s)\n",
  576. agmt_get_long_name(conn->agmt),
  577. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  578. conn->last_ldap_error = rc;
  579. if (IS_DISCONNECT_ERROR(rc))
  580. {
  581. conn_disconnect(conn);
  582. return_value = CONN_NOT_CONNECTED;
  583. }
  584. else
  585. {
  586. conn->status = STATUS_CONNECTED;
  587. return_value = CONN_OPERATION_FAILED;
  588. }
  589. }
  590. }
  591. else
  592. {
  593. /* conn->last_ldap_error has been set to a more specific value
  594. * in conn_connected()
  595. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  596. */
  597. return_value = CONN_NOT_CONNECTED;
  598. }
  599. if (message_id)
  600. {
  601. *message_id = msgid;
  602. }
  603. return return_value;
  604. }
  605. /*
  606. * Send an LDAP add operation.
  607. */
  608. ConnResult
  609. conn_send_add(Repl_Connection *conn, const char *dn, LDAPMod **attrs,
  610. LDAPControl *update_control, int *message_id)
  611. {
  612. return perform_operation(conn, CONN_ADD, dn, attrs, NULL /* newrdn */,
  613. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  614. NULL /* extop OID */, NULL /* extop payload */, message_id);
  615. }
  616. /*
  617. * Send an LDAP delete operation.
  618. */
  619. ConnResult
  620. conn_send_delete(Repl_Connection *conn, const char *dn,
  621. LDAPControl *update_control, int *message_id)
  622. {
  623. return perform_operation(conn, CONN_DELETE, dn, NULL /* attrs */,
  624. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  625. update_control, NULL /* extop OID */, NULL /* extop payload */, message_id);
  626. }
  627. /*
  628. * Send an LDAP modify operation.
  629. */
  630. ConnResult
  631. conn_send_modify(Repl_Connection *conn, const char *dn, LDAPMod **mods,
  632. LDAPControl *update_control, int *message_id)
  633. {
  634. return perform_operation(conn, CONN_MODIFY, dn, mods, NULL /* newrdn */,
  635. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  636. NULL /* extop OID */, NULL /* extop payload */, message_id);
  637. }
  638. /*
  639. * Send an LDAP moddn operation.
  640. */
  641. ConnResult
  642. conn_send_rename(Repl_Connection *conn, const char *dn,
  643. const char *newrdn, const char *newparent, int deleteoldrdn,
  644. LDAPControl *update_control, int *message_id)
  645. {
  646. return perform_operation(conn, CONN_RENAME, dn, NULL /* attrs */,
  647. newrdn, newparent, deleteoldrdn, update_control,
  648. NULL /* extop OID */, NULL /* extop payload */, message_id);
  649. }
  650. /*
  651. * Send an LDAP extended operation.
  652. */
  653. ConnResult
  654. conn_send_extended_operation(Repl_Connection *conn, const char *extop_oid,
  655. struct berval *payload,
  656. LDAPControl *update_control, int *message_id)
  657. {
  658. return perform_operation(conn, CONN_EXTENDED_OPERATION, NULL /* dn */, NULL /* attrs */,
  659. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  660. update_control, extop_oid, payload, message_id);
  661. }
  662. /*
  663. * Synchronously read an entry and return a specific attribute's values.
  664. * Returns CONN_OPERATION_SUCCESS if successful. Returns
  665. * CONN_OPERATION_FAILED if the operation was sent but an LDAP error
  666. * occurred (conn->last_ldap_error is set in this case), and
  667. * CONN_NOT_CONNECTED if no connection was active.
  668. *
  669. * The caller must free the returned_bvals.
  670. */
  671. ConnResult
  672. conn_read_entry_attribute(Repl_Connection *conn, const char *dn,
  673. char *type, struct berval ***returned_bvals)
  674. {
  675. ConnResult return_value;
  676. int ldap_rc;
  677. LDAPControl *server_controls[2];
  678. LDAPMessage *res = NULL;
  679. char *attrs[2];
  680. PR_ASSERT(NULL != type);
  681. if (conn_connected(conn))
  682. {
  683. server_controls[0] = &manageDSAITControl;
  684. server_controls[1] = NULL;
  685. attrs[0] = type;
  686. attrs[1] = NULL;
  687. ldap_rc = ldap_search_ext_s(conn->ld, dn, LDAP_SCOPE_BASE,
  688. "(objectclass=*)", attrs, 0 /* attrsonly */,
  689. server_controls, NULL /* client controls */,
  690. &conn->timeout, 0 /* sizelimit */, &res);
  691. if (LDAP_SUCCESS == ldap_rc)
  692. {
  693. LDAPMessage *entry = ldap_first_entry(conn->ld, res);
  694. if (NULL != entry)
  695. {
  696. *returned_bvals = ldap_get_values_len(conn->ld, entry, type);
  697. }
  698. return_value = CONN_OPERATION_SUCCESS;
  699. }
  700. else if (IS_DISCONNECT_ERROR(ldap_rc))
  701. {
  702. conn_disconnect(conn);
  703. return_value = CONN_NOT_CONNECTED;
  704. }
  705. else
  706. {
  707. return_value = CONN_OPERATION_FAILED;
  708. }
  709. conn->last_ldap_error = ldap_rc;
  710. if (NULL != res)
  711. {
  712. ldap_msgfree(res);
  713. res = NULL;
  714. }
  715. }
  716. else
  717. {
  718. return_value = CONN_NOT_CONNECTED;
  719. }
  720. return return_value;
  721. }
  722. /*
  723. * Return an pointer to a string describing the connection's status.
  724. */
  725. const char *
  726. conn_get_status(Repl_Connection *conn)
  727. {
  728. return conn->status;
  729. }
  730. /*
  731. * Cancel any outstanding linger timer. Should be called when
  732. * a replication session is beginning.
  733. */
  734. void
  735. conn_cancel_linger(Repl_Connection *conn)
  736. {
  737. PR_ASSERT(NULL != conn);
  738. PR_Lock(conn->lock);
  739. if (conn->linger_active)
  740. {
  741. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  742. "%s: Cancelling linger on the connection\n",
  743. agmt_get_long_name(conn->agmt));
  744. conn->linger_active = PR_FALSE;
  745. if (slapi_eq_cancel(conn->linger_event) == 1)
  746. {
  747. conn->refcnt--;
  748. }
  749. conn->linger_event = NULL;
  750. conn->status = STATUS_CONNECTED;
  751. }
  752. else
  753. {
  754. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  755. "%s: No linger to cancel on the connection\n",
  756. agmt_get_long_name(conn->agmt));
  757. }
  758. PR_Unlock(conn->lock);
  759. }
  760. /*
  761. * Called when our linger timeout timer expires. This means
  762. * we should check to see if perhaps the connection's become
  763. * active again, in which case we do nothing. Otherwise,
  764. * we close the connection.
  765. */
  766. static void
  767. linger_timeout(time_t event_time, void *arg)
  768. {
  769. PRBool delete_now;
  770. Repl_Connection *conn = (Repl_Connection *)arg;
  771. PR_ASSERT(NULL != conn);
  772. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  773. "%s: Linger timeout has expired on the connection\n",
  774. agmt_get_long_name(conn->agmt));
  775. PR_Lock(conn->lock);
  776. if (conn->linger_active)
  777. {
  778. conn->linger_active = PR_FALSE;
  779. conn->linger_event = NULL;
  780. close_connection_internal(conn);
  781. }
  782. delete_now = conn->delete_after_linger;
  783. PR_Unlock(conn->lock);
  784. if (delete_now)
  785. {
  786. conn_delete_internal(conn);
  787. }
  788. }
  789. /*
  790. * Indicate that a session is ending. The linger timer starts when
  791. * this function is called.
  792. */
  793. void
  794. conn_start_linger(Repl_Connection *conn)
  795. {
  796. time_t now;
  797. PR_ASSERT(NULL != conn);
  798. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  799. "%s: Beginning linger on the connection\n",
  800. agmt_get_long_name(conn->agmt));
  801. if (!conn_connected(conn))
  802. {
  803. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  804. "%s: No linger on the closed conn\n",
  805. agmt_get_long_name(conn->agmt));
  806. return;
  807. }
  808. time(&now);
  809. PR_Lock(conn->lock);
  810. if (conn->linger_active)
  811. {
  812. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  813. "%s: Linger already active on the connection\n",
  814. agmt_get_long_name(conn->agmt));
  815. }
  816. else
  817. {
  818. conn->linger_active = PR_TRUE;
  819. conn->linger_event = slapi_eq_once(linger_timeout, conn, now + conn->linger_time);
  820. conn->status = STATUS_LINGERING;
  821. }
  822. PR_Unlock(conn->lock);
  823. }
  824. /*
  825. * If no connection is currently active, opens a connection and binds to
  826. * the remote server. If a connection is open (e.g. lingering) then
  827. * this is a no-op.
  828. *
  829. * Returns CONN_OPERATION_SUCCESS on success, or CONN_OPERATION_FAILED
  830. * on failure. Sets conn->last_ldap_error and conn->last_operation;
  831. */
  832. ConnResult
  833. conn_connect(Repl_Connection *conn)
  834. {
  835. int optdata;
  836. int secure = 0;
  837. char* binddn = NULL;
  838. struct berval *creds;
  839. ConnResult return_value = CONN_OPERATION_SUCCESS;
  840. int pw_ret = 1;
  841. /** Connection already open just return SUCCESS **/
  842. if(conn->state == STATE_CONNECTED) return return_value;
  843. PR_Lock(conn->lock);
  844. if (conn->flag_agmt_changed) {
  845. /* So far we cannot change Hostname and Port */
  846. /* slapi_ch_free((void **)&conn->hostname); */
  847. /* conn->hostname = agmt_get_hostname(conn->agmt); */
  848. /* conn->port = agmt_get_port(conn->agmt); */
  849. slapi_ch_free((void **)&conn->binddn);
  850. conn->binddn = agmt_get_binddn(conn->agmt);
  851. conn->bindmethod = agmt_get_bindmethod(conn->agmt);
  852. conn->transport_flags = agmt_get_transport_flags(conn->agmt);
  853. conn->timeout.tv_sec = agmt_get_timeout(conn->agmt);
  854. conn->flag_agmt_changed = 0;
  855. slapi_ch_free((void **)&conn->plain);
  856. }
  857. PR_Unlock(conn->lock);
  858. creds = agmt_get_credentials(conn->agmt);
  859. if (conn->plain == NULL) {
  860. char *plain = NULL;
  861. /* kexcoff: for reversible encryption */
  862. /* We need to test the return code of pw_rever_decode in order to decide
  863. * if a free for plain will be needed (pw_ret == 0) or not (pw_ret != 0) */
  864. pw_ret = pw_rever_decode(creds->bv_val, &plain, type_nsds5ReplicaCredentials);
  865. /* Pb occured in decryption: stop now, binding will fail */
  866. if ( pw_ret == -1 )
  867. {
  868. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  869. "%s: Decoding of the credentials failed.\n",
  870. agmt_get_long_name(conn->agmt));
  871. return_value = CONN_OPERATION_FAILED;
  872. conn->last_ldap_error = LDAP_INVALID_CREDENTIALS;
  873. conn->state = STATE_DISCONNECTED;
  874. return (return_value);
  875. } /* Else, does not mean that the plain is correct, only means the we had no internal
  876. decoding pb */
  877. conn->plain = slapi_ch_strdup (plain);
  878. if (!pw_ret) slapi_ch_free((void**)&plain);
  879. }
  880. /* ugaston: if SSL has been selected in the replication agreement, SSL client
  881. * initialisation should be done before ever trying to open any connection at all.
  882. */
  883. if (conn->transport_flags == TRANSPORT_FLAG_TLS) {
  884. secure = 2;
  885. } else if (conn->transport_flags == TRANSPORT_FLAG_SSL) {
  886. secure = 1;
  887. }
  888. if (secure > 0) {
  889. if (!NSS_IsInitialized()) {
  890. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  891. "%s: SSL Not Initialized, Replication over SSL FAILED\n",
  892. agmt_get_long_name(conn->agmt));
  893. conn->last_ldap_error = LDAP_INAPPROPRIATE_AUTH;
  894. conn->last_operation = CONN_INIT;
  895. ber_bvfree(creds);
  896. creds = NULL;
  897. return CONN_SSL_NOT_ENABLED;
  898. }
  899. }
  900. if (return_value == CONN_OPERATION_SUCCESS) {
  901. int io_timeout_ms;
  902. /* Now we initialize the LDAP Structure and set options */
  903. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  904. "%s: Trying %s%s slapi_ldap_init_ext\n",
  905. agmt_get_long_name(conn->agmt),
  906. secure ? "secure" : "non-secure",
  907. (secure == 2) ? " startTLS" : "");
  908. /* shared = 1 because we will read results from a second thread */
  909. conn->ld = slapi_ldap_init_ext(NULL, conn->hostname, conn->port, secure, 1, NULL);
  910. if (NULL == conn->ld)
  911. {
  912. return_value = CONN_OPERATION_FAILED;
  913. conn->state = STATE_DISCONNECTED;
  914. conn->last_operation = CONN_INIT;
  915. conn->last_ldap_error = LDAP_LOCAL_ERROR;
  916. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  917. "%s: Failed to establish %s%sconnection to the consumer\n",
  918. agmt_get_long_name(conn->agmt),
  919. secure ? "secure " : "",
  920. (secure == 2) ? "startTLS " : "");
  921. ber_bvfree(creds);
  922. creds = NULL;
  923. return return_value;
  924. }
  925. /* slapi_ch_strdup is OK with NULL strings */
  926. binddn = slapi_ch_strdup(conn->binddn);
  927. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  928. "%s: binddn = %s, passwd = %s\n",
  929. agmt_get_long_name(conn->agmt),
  930. binddn?binddn:"NULL", creds->bv_val?creds->bv_val:"NULL");
  931. /* Set some options for the connection. */
  932. optdata = LDAP_DEREF_NEVER; /* Don't dereference aliases */
  933. ldap_set_option(conn->ld, LDAP_OPT_DEREF, &optdata);
  934. optdata = LDAP_VERSION3; /* We need LDAP version 3 */
  935. ldap_set_option(conn->ld, LDAP_OPT_PROTOCOL_VERSION, &optdata);
  936. /* Don't chase any referrals (although we shouldn't get any) */
  937. ldap_set_option(conn->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  938. /* override the default timeout with the specified timeout */
  939. io_timeout_ms = conn->timeout.tv_sec * 1000 + conn->timeout.tv_usec / 1000;
  940. prldap_set_session_option(conn->ld, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT,
  941. io_timeout_ms);
  942. /* We've got an ld. Now bind to the server. */
  943. conn->last_operation = CONN_BIND;
  944. }
  945. if ( bind_and_check_pwp(conn, binddn, conn->plain) == CONN_OPERATION_FAILED )
  946. {
  947. conn->last_ldap_error = ldap_get_lderrno (conn->ld, NULL, NULL);
  948. conn->state = STATE_DISCONNECTED;
  949. return_value = CONN_OPERATION_FAILED;
  950. }
  951. else
  952. {
  953. conn->last_ldap_error = LDAP_SUCCESS;
  954. conn->state = STATE_CONNECTED;
  955. return_value = CONN_OPERATION_SUCCESS;
  956. }
  957. ber_bvfree(creds);
  958. creds = NULL;
  959. slapi_ch_free((void**)&binddn);
  960. if(return_value == CONN_OPERATION_FAILED)
  961. {
  962. close_connection_internal(conn);
  963. } else
  964. {
  965. conn->last_ldap_error = LDAP_SUCCESS;
  966. conn->state = STATE_CONNECTED;
  967. }
  968. return return_value;
  969. }
  970. static void
  971. close_connection_internal(Repl_Connection *conn)
  972. {
  973. if (NULL != conn->ld)
  974. {
  975. /* Since we call slapi_ldap_init,
  976. we must call slapi_ldap_unbind */
  977. slapi_ldap_unbind(conn->ld);
  978. }
  979. conn->ld = NULL;
  980. conn->state = STATE_DISCONNECTED;
  981. conn->status = STATUS_DISCONNECTED;
  982. conn->supports_ds50_repl = -1;
  983. conn->supports_ds71_repl = -1;
  984. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  985. "%s: Disconnected from the consumer\n", agmt_get_long_name(conn->agmt));
  986. }
  987. void
  988. conn_disconnect(Repl_Connection *conn)
  989. {
  990. PR_ASSERT(NULL != conn);
  991. PR_Lock(conn->lock);
  992. close_connection_internal(conn);
  993. PR_Unlock(conn->lock);
  994. }
  995. /*
  996. * Determine if the remote replica supports DS 5.0 replication.
  997. * Return codes:
  998. * CONN_SUPPORTS_DS5_REPL - the remote replica suport DS5 replication
  999. * CONN_DOES_NOT_SUPPORT_DS5_REPL - the remote replica does not
  1000. * support DS5 replication.
  1001. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1002. * replica supports DS5 replication.
  1003. * CONN_NOT_CONNECTED - no connection was active.
  1004. */
  1005. ConnResult
  1006. conn_replica_supports_ds5_repl(Repl_Connection *conn)
  1007. {
  1008. ConnResult return_value;
  1009. int ldap_rc;
  1010. if (conn_connected(conn))
  1011. {
  1012. if (conn->supports_ds50_repl == -1) {
  1013. LDAPMessage *res = NULL;
  1014. LDAPMessage *entry = NULL;
  1015. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1016. conn->status = STATUS_SEARCHING;
  1017. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1018. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1019. NULL /* server controls */, NULL /* client controls */,
  1020. &conn->timeout, LDAP_NO_LIMIT, &res);
  1021. if (LDAP_SUCCESS == ldap_rc)
  1022. {
  1023. conn->supports_ds50_repl = 0;
  1024. entry = ldap_first_entry(conn->ld, res);
  1025. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_NSDS50_UPDATE_INFO_CONTROL_OID))
  1026. {
  1027. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1028. }
  1029. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS50_REPLICATION_REQUEST_OID))
  1030. {
  1031. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1032. }
  1033. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_END_NSDS50_REPLICATION_REQUEST_OID))
  1034. {
  1035. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1036. }
  1037. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID))
  1038. {
  1039. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1040. }
  1041. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_RESPONSE_OID))
  1042. {
  1043. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1044. }
  1045. else
  1046. {
  1047. conn->supports_ds50_repl = 1;
  1048. return_value = CONN_SUPPORTS_DS5_REPL;
  1049. }
  1050. }
  1051. else
  1052. {
  1053. if (IS_DISCONNECT_ERROR(ldap_rc))
  1054. {
  1055. conn->last_ldap_error = ldap_rc; /* specific reason */
  1056. conn_disconnect(conn);
  1057. return_value = CONN_NOT_CONNECTED;
  1058. }
  1059. else
  1060. {
  1061. return_value = CONN_OPERATION_FAILED;
  1062. }
  1063. }
  1064. if (NULL != res)
  1065. ldap_msgfree(res);
  1066. }
  1067. else {
  1068. return_value = conn->supports_ds50_repl ? CONN_SUPPORTS_DS5_REPL : CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1069. }
  1070. }
  1071. else
  1072. {
  1073. /* Not connected */
  1074. return_value = CONN_NOT_CONNECTED;
  1075. }
  1076. return return_value;
  1077. }
  1078. /*
  1079. * Determine if the remote replica supports DS 5.0 replication.
  1080. * Return codes:
  1081. * CONN_SUPPORTS_DS71_REPL - the remote replica suport DS5 replication
  1082. * CONN_DOES_NOT_SUPPORT_DS71_REPL - the remote replica does not
  1083. * support DS5 replication.
  1084. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1085. * replica supports DS5 replication.
  1086. * CONN_NOT_CONNECTED - no connection was active.
  1087. */
  1088. ConnResult
  1089. conn_replica_supports_ds71_repl(Repl_Connection *conn)
  1090. {
  1091. ConnResult return_value;
  1092. int ldap_rc;
  1093. if (conn_connected(conn))
  1094. {
  1095. if (conn->supports_ds71_repl == -1) {
  1096. LDAPMessage *res = NULL;
  1097. LDAPMessage *entry = NULL;
  1098. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1099. conn->status = STATUS_SEARCHING;
  1100. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1101. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1102. NULL /* server controls */, NULL /* client controls */,
  1103. &conn->timeout, LDAP_NO_LIMIT, &res);
  1104. if (LDAP_SUCCESS == ldap_rc)
  1105. {
  1106. conn->supports_ds71_repl = 0;
  1107. entry = ldap_first_entry(conn->ld, res);
  1108. if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS71_REPLICATION_ENTRY_REQUEST_OID))
  1109. {
  1110. return_value = CONN_DOES_NOT_SUPPORT_DS71_REPL;
  1111. }
  1112. else
  1113. {
  1114. conn->supports_ds71_repl = 1;
  1115. return_value = CONN_SUPPORTS_DS71_REPL;
  1116. }
  1117. }
  1118. else
  1119. {
  1120. if (IS_DISCONNECT_ERROR(ldap_rc))
  1121. {
  1122. conn->last_ldap_error = ldap_rc; /* specific reason */
  1123. conn_disconnect(conn);
  1124. return_value = CONN_NOT_CONNECTED;
  1125. }
  1126. else
  1127. {
  1128. return_value = CONN_OPERATION_FAILED;
  1129. }
  1130. }
  1131. if (NULL != res)
  1132. ldap_msgfree(res);
  1133. }
  1134. else {
  1135. return_value = conn->supports_ds71_repl ? CONN_SUPPORTS_DS71_REPL : CONN_DOES_NOT_SUPPORT_DS71_REPL;
  1136. }
  1137. }
  1138. else
  1139. {
  1140. /* Not connected */
  1141. return_value = CONN_NOT_CONNECTED;
  1142. }
  1143. return return_value;
  1144. }
  1145. /* Determine if the replica is read-only */
  1146. ConnResult
  1147. conn_replica_is_readonly(Repl_Connection *conn)
  1148. {
  1149. ReplicaId rid = agmt_get_consumer_rid( (Repl_Agmt *) conn->agmt, conn );
  1150. if (rid == READ_ONLY_REPLICA_ID)
  1151. {
  1152. return CONN_IS_READONLY;
  1153. } else
  1154. {
  1155. return CONN_IS_NOT_READONLY;
  1156. }
  1157. }
  1158. /*
  1159. * Return 1 if "value" is a value of attribute type "type" in entry "entry".
  1160. * Otherwise, return 0.
  1161. */
  1162. static int
  1163. attribute_string_value_present(LDAP *ld, LDAPMessage *entry, const char *type,
  1164. const char *value)
  1165. {
  1166. int return_value = 0;
  1167. if (NULL != entry)
  1168. {
  1169. char *atype = NULL;
  1170. BerElement *ber = NULL;
  1171. atype = ldap_first_attribute(ld, entry, &ber);
  1172. while (NULL != atype && 0 == return_value)
  1173. {
  1174. if (strcasecmp(atype, type) == 0)
  1175. {
  1176. char **strvals = ldap_get_values(ld, entry, atype);
  1177. int i;
  1178. for (i = 0; return_value == 0 && NULL != strvals && NULL != strvals[i]; i++)
  1179. {
  1180. if (strcmp(strvals[i], value) == 0)
  1181. {
  1182. return_value = 1;
  1183. }
  1184. }
  1185. if (NULL != strvals)
  1186. {
  1187. ldap_value_free(strvals);
  1188. }
  1189. }
  1190. ldap_memfree(atype);
  1191. atype = ldap_next_attribute(ld, entry, ber);
  1192. }
  1193. if (NULL != ber)
  1194. ldap_ber_free(ber, 0);
  1195. /* The last atype has not been freed yet */
  1196. if (NULL != atype)
  1197. ldap_memfree(atype);
  1198. }
  1199. return return_value;
  1200. }
  1201. /*
  1202. * Read the remote server's schema entry, then read the local schema entry,
  1203. * and compare the nsschemacsn attribute. If the local csn is newer, or
  1204. * the remote csn is absent, push the schema down to the consumer.
  1205. * Return codes:
  1206. * CONN_SCHEMA_UPDATED if the schema was pushed successfully
  1207. * CONN_SCHEMA_NO_UPDATE_NEEDED if the schema was as new or newer than
  1208. * the local server's schema
  1209. * CONN_OPERATION_FAILED if an error occurred
  1210. * CONN_NOT_CONNECTED if no connection was active
  1211. * NOTE: Should only be called when a replication session has been
  1212. * established by sending a startReplication extended operation.
  1213. */
  1214. ConnResult
  1215. conn_push_schema(Repl_Connection *conn, CSN **remotecsn)
  1216. {
  1217. ConnResult return_value = CONN_OPERATION_SUCCESS;
  1218. char *nsschemacsn = "nsschemacsn";
  1219. Slapi_Entry **entries = NULL;
  1220. Slapi_Entry *schema_entry = NULL;
  1221. CSN *localcsn = NULL;
  1222. Slapi_PBlock *spb = NULL;
  1223. char localcsnstr[CSN_STRSIZE + 1] = {0};
  1224. if (!conn_connected(conn))
  1225. {
  1226. return_value = CONN_NOT_CONNECTED;
  1227. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1228. "%s: Schema replication update failed: not connected to consumer\n",
  1229. agmt_get_long_name(conn->agmt));
  1230. }
  1231. else
  1232. {
  1233. localcsn = dup_global_schema_csn();
  1234. if (NULL == localcsn)
  1235. {
  1236. /* Local server has epoch CSN, so don't push schema */
  1237. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1238. }
  1239. else if ( remotecsn && *remotecsn && csn_compare(localcsn, *remotecsn) <= 0 )
  1240. {
  1241. /* Local server schema is not newer than the remote one */
  1242. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1243. }
  1244. else
  1245. {
  1246. struct berval **remote_schema_csn_bervals = NULL;
  1247. /* Get remote server's schema */
  1248. return_value = conn_read_entry_attribute(conn, "cn=schema", nsschemacsn,
  1249. &remote_schema_csn_bervals);
  1250. if (CONN_OPERATION_SUCCESS == return_value)
  1251. {
  1252. if (NULL != remote_schema_csn_bervals && NULL != remote_schema_csn_bervals[0])
  1253. {
  1254. char remotecsnstr[CSN_STRSIZE + 1] = {0};
  1255. memcpy(remotecsnstr, remote_schema_csn_bervals[0]->bv_val,
  1256. remote_schema_csn_bervals[0]->bv_len);
  1257. remotecsnstr[remote_schema_csn_bervals[0]->bv_len] = '\0';
  1258. *remotecsn = csn_new_by_string(remotecsnstr);
  1259. if (NULL != remotecsn && (csn_compare(localcsn, *remotecsn) <= 0))
  1260. {
  1261. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1262. }
  1263. /* Need to free the remote_schema_csn_bervals */
  1264. ber_bvecfree(remote_schema_csn_bervals);
  1265. }
  1266. }
  1267. }
  1268. }
  1269. if (CONN_OPERATION_SUCCESS == return_value)
  1270. {
  1271. /* We know we need to push the schema out. */
  1272. LDAPMod ocmod = {0};
  1273. LDAPMod atmod = {0};
  1274. LDAPMod csnmod = {0};
  1275. LDAPMod *attrs[4] = {0};
  1276. int numvalues = 0;
  1277. Slapi_Attr *attr = NULL;
  1278. char *csnvalues[2];
  1279. ocmod.mod_type = "objectclasses";
  1280. ocmod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1281. ocmod.mod_bvalues = NULL;
  1282. atmod.mod_type = "attributetypes";
  1283. atmod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1284. atmod.mod_bvalues = NULL;
  1285. csnmod.mod_type = nsschemacsn;
  1286. csnmod.mod_op = LDAP_MOD_REPLACE;
  1287. csn_as_string (localcsn, PR_FALSE, localcsnstr);
  1288. csnvalues[0] = localcsnstr;
  1289. csnvalues[1] = NULL;
  1290. csnmod.mod_values = csnvalues;
  1291. attrs[0] = &ocmod;
  1292. attrs[1] = &atmod;
  1293. attrs[2] = &csnmod;
  1294. attrs[3] = NULL;
  1295. return_value = CONN_OPERATION_FAILED; /* assume failure */
  1296. /* Get local schema */
  1297. spb = slapi_search_internal("cn=schema", LDAP_SCOPE_BASE, "(objectclass=*)",
  1298. NULL /* controls */, NULL /* schema_csn_attrs */, 0 /* attrsonly */);
  1299. slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1300. if (NULL == entries || NULL == entries[0])
  1301. {
  1302. /* Whoops - couldn't read our own schema! */
  1303. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1304. "%s: Error: unable to read local schema definitions.\n",
  1305. agmt_get_long_name(conn->agmt));
  1306. return_value = CONN_OPERATION_FAILED;
  1307. }
  1308. else
  1309. {
  1310. schema_entry = entries[0];
  1311. if (slapi_entry_attr_find(schema_entry, "objectclasses", &attr) != -1)
  1312. {
  1313. int i, ind;
  1314. Slapi_Value *value;
  1315. slapi_attr_get_numvalues(attr, &numvalues);
  1316. ocmod.mod_bvalues = (struct berval **)slapi_ch_malloc((numvalues + 1) *
  1317. sizeof(struct berval *));
  1318. for (i = 0, ind = slapi_attr_first_value(attr, &value);
  1319. ind != -1; ind = slapi_attr_next_value(attr, ind, &value), i++)
  1320. {
  1321. /* XXXggood had to cast away const below */
  1322. ocmod.mod_bvalues[i] = (struct berval *)slapi_value_get_berval(value);
  1323. }
  1324. ocmod.mod_bvalues[numvalues] = NULL;
  1325. if (slapi_entry_attr_find(schema_entry, "attributetypes", &attr) != -1)
  1326. {
  1327. ConnResult result;
  1328. slapi_attr_get_numvalues(attr, &numvalues);
  1329. atmod.mod_bvalues = (struct berval **)slapi_ch_malloc((numvalues + 1) *
  1330. sizeof(struct berval *));
  1331. for (i = 0, ind = slapi_attr_first_value(attr, &value);
  1332. ind != -1; ind = slapi_attr_next_value(attr, ind, &value), i++)
  1333. {
  1334. /* XXXggood had to cast away const below */
  1335. atmod.mod_bvalues[i] = (struct berval *)slapi_value_get_berval(value);
  1336. }
  1337. atmod.mod_bvalues[numvalues] = NULL;
  1338. result = conn_send_modify(conn, "cn=schema", attrs, NULL, NULL); /* DBDB: this needs to be fixed to use async */
  1339. result = conn_read_result(conn,NULL);
  1340. switch (result)
  1341. {
  1342. case CONN_OPERATION_FAILED:
  1343. {
  1344. int ldaperr = -1, optype = -1;
  1345. conn_get_error(conn, &optype, &ldaperr);
  1346. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1347. "%s: Schema replication update failed: %s\n",
  1348. agmt_get_long_name(conn->agmt),
  1349. ldaperr == -1 ? "Unknown Error" : ldap_err2string(ldaperr));
  1350. return_value = CONN_OPERATION_FAILED;
  1351. break;
  1352. }
  1353. case CONN_NOT_CONNECTED:
  1354. return_value = CONN_NOT_CONNECTED;
  1355. break;
  1356. case CONN_OPERATION_SUCCESS:
  1357. return_value = CONN_SCHEMA_UPDATED;
  1358. break;
  1359. default:
  1360. break;
  1361. }
  1362. }
  1363. }
  1364. else
  1365. {
  1366. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1367. "%s: Schema replication update failed: "
  1368. "unable to prepare schema entry for transmission.\n",
  1369. agmt_get_long_name(conn->agmt));
  1370. }
  1371. }
  1372. /* slapi_ch_free accepts NULL pointer */
  1373. slapi_ch_free((void **)&ocmod.mod_bvalues);
  1374. slapi_ch_free((void **)&atmod.mod_bvalues);
  1375. }
  1376. if (NULL != spb)
  1377. {
  1378. slapi_free_search_results_internal(spb);
  1379. slapi_pblock_destroy(spb);
  1380. spb = NULL;
  1381. }
  1382. if (NULL != localcsn)
  1383. {
  1384. csn_free(&localcsn);
  1385. }
  1386. return return_value;
  1387. }
  1388. void
  1389. conn_set_timeout(Repl_Connection *conn, long timeout)
  1390. {
  1391. PR_ASSERT(NULL != conn);
  1392. PR_ASSERT(timeout >= 0);
  1393. PR_Lock(conn->lock);
  1394. conn->timeout.tv_sec = timeout;
  1395. PR_Unlock(conn->lock);
  1396. }
  1397. long
  1398. conn_get_timeout(Repl_Connection *conn)
  1399. {
  1400. long retval = 0;
  1401. PR_ASSERT(NULL != conn);
  1402. retval = conn->timeout.tv_sec;
  1403. return retval;
  1404. }
  1405. void conn_set_agmt_changed(Repl_Connection *conn)
  1406. {
  1407. PR_ASSERT(NULL != conn);
  1408. PR_Lock(conn->lock);
  1409. if (NULL != conn->agmt)
  1410. conn->flag_agmt_changed = 1;
  1411. PR_Unlock(conn->lock);
  1412. }
  1413. static const char *
  1414. bind_method_to_mech(int bindmethod)
  1415. {
  1416. switch (bindmethod) {
  1417. case BINDMETHOD_SSL_CLIENTAUTH:
  1418. return LDAP_SASL_EXTERNAL;
  1419. break;
  1420. case BINDMETHOD_SASL_GSSAPI:
  1421. return "GSSAPI";
  1422. break;
  1423. case BINDMETHOD_SASL_DIGEST_MD5:
  1424. return "DIGEST-MD5";
  1425. break;
  1426. default: /* anything else */
  1427. return LDAP_SASL_SIMPLE;
  1428. }
  1429. return LDAP_SASL_SIMPLE;
  1430. }
  1431. /*
  1432. * Check the result of an ldap BIND operation to see we it
  1433. * contains the expiration controls
  1434. * return: -1 error, not bound
  1435. * 0, OK bind has succeeded
  1436. */
  1437. static int
  1438. bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password)
  1439. {
  1440. LDAPControl **ctrls = NULL;
  1441. LDAP *ld = conn->ld;
  1442. int rc;
  1443. const char *mech = bind_method_to_mech(conn->bindmethod);
  1444. rc = slapi_ldap_bind(conn->ld, binddn, password, mech, NULL,
  1445. &ctrls, NULL, NULL);
  1446. if ( rc == LDAP_SUCCESS )
  1447. {
  1448. if (conn->last_ldap_error != rc)
  1449. {
  1450. conn->last_ldap_error = rc;
  1451. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1452. "%s: Replication bind with %s auth resumed\n",
  1453. agmt_get_long_name(conn->agmt),
  1454. mech ? mech : "SIMPLE");
  1455. }
  1456. if ( ctrls )
  1457. {
  1458. int i;
  1459. for( i = 0; ctrls[ i ] != NULL; ++i )
  1460. {
  1461. if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRED)) )
  1462. {
  1463. /* Bind is successfull but password has expired */
  1464. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1465. "%s: Succesfully bound %s to consumer, "
  1466. "but password has expired on consumer.\n",
  1467. agmt_get_long_name(conn->agmt), binddn);
  1468. }
  1469. else if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRING)) )
  1470. {
  1471. /* The password is expiring in n seconds */
  1472. if ( (ctrls[ i ]->ldctl_value.bv_val != NULL) &&
  1473. (ctrls[ i ]->ldctl_value.bv_len > 0) )
  1474. {
  1475. int password_expiring = atoi( ctrls[ i ]->ldctl_value.bv_val );
  1476. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1477. "%s: Succesfully bound %s to consumer, "
  1478. "but password is expiring on consumer in %d seconds.\n",
  1479. agmt_get_long_name(conn->agmt), binddn, password_expiring);
  1480. }
  1481. }
  1482. }
  1483. ldap_controls_free( ctrls );
  1484. }
  1485. return (CONN_OPERATION_SUCCESS);
  1486. }
  1487. else
  1488. {
  1489. ldap_controls_free( ctrls );
  1490. /* Do not report the same error over and over again */
  1491. if (conn->last_ldap_error != rc)
  1492. {
  1493. char *errmsg = NULL;
  1494. conn->last_ldap_error = rc;
  1495. /* errmsg is a pointer directly into the ld structure - do not free */
  1496. rc = ldap_get_lderrno( ld, NULL, &errmsg );
  1497. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1498. "%s: Replication bind with %s auth failed: LDAP error %d (%s) (%s)\n",
  1499. agmt_get_long_name(conn->agmt),
  1500. mech ? mech : "SIMPLE", rc,
  1501. ldap_err2string(rc), errmsg);
  1502. }
  1503. return (CONN_OPERATION_FAILED);
  1504. }
  1505. }
  1506. void
  1507. repl5_set_debug_timeout(const char *val)
  1508. {
  1509. /* val looks like this: seconds[:debuglevel] */
  1510. /* seconds is the number of seconds to wait until turning on the debug level */
  1511. /* this should be less than the ldap connection timeout (default 10 minutes) */
  1512. /* the optional debug level is the error log debugging level to use (default repl) */
  1513. if (val) {
  1514. const char *p = strchr(val, ':');
  1515. s_debug_timeout = atoi(val);
  1516. if (p) {
  1517. s_debug_level = atoi(p+1);
  1518. } else {
  1519. s_debug_level = 8192;
  1520. }
  1521. }
  1522. }
  1523. #ifdef FOR_DEBUGGING
  1524. static time_t
  1525. PRTime2time_t (PRTime tm)
  1526. {
  1527. PRInt64 rt;
  1528. PR_ASSERT (tm);
  1529. LL_DIV(rt, tm, PR_USEC_PER_SEC);
  1530. return (time_t)rt;
  1531. }
  1532. #endif
  1533. static Slapi_Eq_Context
  1534. repl5_start_debug_timeout(int *setlevel)
  1535. {
  1536. Slapi_Eq_Context eqctx = 0;
  1537. if (s_debug_timeout && s_debug_level) {
  1538. time_t now = time(NULL);
  1539. eqctx = slapi_eq_once(repl5_debug_timeout_callback, setlevel,
  1540. s_debug_timeout + now);
  1541. }
  1542. return eqctx;
  1543. }
  1544. static void
  1545. repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel)
  1546. {
  1547. char buf[20];
  1548. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1549. if (eqctx && !*setlevel) {
  1550. (void)slapi_eq_cancel(eqctx);
  1551. }
  1552. if (s_debug_timeout && s_debug_level && *setlevel) {
  1553. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  1554. sprintf(buf, "%d", 0);
  1555. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1556. }
  1557. }
  1558. static void
  1559. repl5_debug_timeout_callback(time_t when, void *arg)
  1560. {
  1561. int *setlevel = (int *)arg;
  1562. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  1563. char buf[20];
  1564. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1565. *setlevel = 1;
  1566. sprintf(buf, "%d", s_debug_level);
  1567. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1568. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1569. "repl5_debug_timeout_callback: set debug level to %d at %ld\n",
  1570. s_debug_level, when);
  1571. }