repl5_connection.c 54 KB

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