1
0

repl5_connection.c 56 KB

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