repl5_connection.c 49 KB

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