1
0

repl5_connection.c 49 KB

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