repl5_connection.c 62 KB

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