repl5_plugins.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  5. * All rights reserved.
  6. *
  7. * License: GPL (version 3 or any later version).
  8. * See LICENSE for details.
  9. * END COPYRIGHT BLOCK **/
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. /*
  14. * repl5_consumer.c - consumer plugin functions
  15. */
  16. /*
  17. * LDAP Data Model Constraints...
  18. *
  19. * 1) Parent entry must exist.
  20. * 2) RDN must be unique.
  21. * 3) RDN components must be attribute values.
  22. * 4) Must conform to the schema constraints - Single Valued Attributes.
  23. * 5) Must conform to the schema constraints - Required Attributes.
  24. * 6) Must conform to the schema constraints - No Extra Attributes.
  25. * 7) Duplicate attribute values are not permited.
  26. * 8) Cycle in the ancestry graph not permitted.
  27. *
  28. * Update Resolution Procedures...
  29. * 1) ...
  30. * 2) Add the UniqueID to the RDN.
  31. * 3) Remove the not present RDN component.
  32. * Use the UniqueID if the RDN becomes empty.
  33. * 4) Keep the most recent value.
  34. * 5) Ignore.
  35. * 6) Ignore.
  36. * 7) Keep the largest CSN for the duplicate value.
  37. * 8) Don't check for this.
  38. */
  39. #include "repl5.h"
  40. #include "cl5_api.h"
  41. #include "urp.h"
  42. #include "csnpl.h"
  43. static char *local_purl = NULL;
  44. static char *purl_attrs[] = {"nsslapd-localhost", "nsslapd-port", "nsslapd-secureport", NULL};
  45. /* Forward declarations */
  46. static void copy_operation_parameters(Slapi_PBlock *pb);
  47. static int write_changelog_and_ruv(Slapi_PBlock *pb);
  48. static int process_postop(Slapi_PBlock *pb);
  49. static int cancel_opcsn(Slapi_PBlock *pb);
  50. static int ruv_tombstone_op(Slapi_PBlock *pb);
  51. static PRBool process_operation(Slapi_PBlock *pb, const CSN *csn);
  52. static PRBool is_mmr_replica(Slapi_PBlock *pb);
  53. static const char *replica_get_purl_for_op(const Replica *r, Slapi_PBlock *pb, const CSN *opcsn);
  54. /*
  55. * XXXggood - what to do if both ssl and non-ssl ports available? How
  56. * do you know which to use? Offer a choice in replication config?
  57. */
  58. int
  59. multimaster_set_local_purl()
  60. {
  61. int rc = 0;
  62. Slapi_Entry **entries;
  63. Slapi_PBlock *pb = NULL;
  64. pb = slapi_pblock_new();
  65. slapi_search_internal_set_pb(pb, "cn=config", LDAP_SCOPE_BASE,
  66. "objectclass=*", purl_attrs, 0, NULL, NULL,
  67. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  68. slapi_search_internal_pb(pb);
  69. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  70. if (rc != 0) {
  71. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "multimaster_set_local_purl - "
  72. "unable to read server configuration: error %d\n",
  73. rc);
  74. } else {
  75. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  76. if (NULL == entries || NULL == entries[0]) {
  77. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "multimaster_set_local_purl - "
  78. "Server configuration missing\n");
  79. rc = -1;
  80. } else {
  81. char *host = slapi_entry_attr_get_charptr(entries[0], "nsslapd-localhost");
  82. char *port = slapi_entry_attr_get_charptr(entries[0], "nsslapd-port");
  83. char *sslport = slapi_entry_attr_get_charptr(entries[0], "nsslapd-secureport");
  84. if (host == NULL || ((port == NULL && sslport == NULL))) {
  85. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name,
  86. "multimaster_set_local_purl - Invalid server "
  87. "configuration\n");
  88. } else {
  89. if (slapi_is_ipv6_addr(host)) {
  90. /* need to put brackets around the ipv6 address */
  91. local_purl = slapi_ch_smprintf("ldap://[%s]:%s", host, port);
  92. } else {
  93. local_purl = slapi_ch_smprintf("ldap://%s:%s", host, port);
  94. }
  95. }
  96. /* slapi_ch_free acceptS NULL pointer */
  97. slapi_ch_free((void **)&host);
  98. slapi_ch_free((void **)&port);
  99. slapi_ch_free((void **)&sslport);
  100. }
  101. }
  102. slapi_free_search_results_internal(pb);
  103. slapi_pblock_destroy(pb);
  104. return rc;
  105. }
  106. const char *
  107. multimaster_get_local_purl()
  108. {
  109. return local_purl;
  110. }
  111. /* ================= Multimaster Pre-Op Plugin Points ================== */
  112. int
  113. multimaster_preop_bind(Slapi_PBlock *pb __attribute__((unused)))
  114. {
  115. return 0;
  116. }
  117. int
  118. multimaster_preop_add(Slapi_PBlock *pb)
  119. {
  120. Slapi_Operation *op;
  121. int is_replicated_operation;
  122. int is_fixup_operation;
  123. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  124. if (!is_mmr_replica(pb)) {
  125. copy_operation_parameters(pb);
  126. return SLAPI_PLUGIN_SUCCESS;
  127. }
  128. is_replicated_operation = operation_is_flag_set(op, OP_FLAG_REPLICATED);
  129. is_fixup_operation = operation_is_flag_set(op, OP_FLAG_REPL_FIXUP);
  130. if (is_replicated_operation) {
  131. if (!is_fixup_operation) {
  132. LDAPControl **ctrlp;
  133. char sessionid[REPL_SESSION_ID_SIZE];
  134. get_repl_session_id(pb, sessionid, NULL);
  135. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &ctrlp);
  136. if (NULL != ctrlp) {
  137. CSN *csn = NULL;
  138. char *target_uuid = NULL;
  139. char *superior_uuid = NULL;
  140. int drc = decode_NSDS50ReplUpdateInfoControl(ctrlp, &target_uuid, &superior_uuid, &csn, NULL /* modrdn_mods */);
  141. if (-1 == drc) {
  142. slapi_log_err(SLAPI_LOG_ERR, REPLICATION_SUBSYSTEM,
  143. "multimaster_preop_add - %s An error occurred while decoding the replication update "
  144. "control - Add\n",
  145. sessionid);
  146. } else if (1 == drc) {
  147. /*
  148. * For add operations, we just set the operation csn. The entry's
  149. * uniqueid should already be an attribute of the replicated entry.
  150. */
  151. struct slapi_operation_parameters *op_params;
  152. /* we don't want to process replicated operations with csn smaller
  153. than the corresponding csn in the consumer's ruv */
  154. if (!process_operation(pb, csn)) {
  155. slapi_send_ldap_result(pb, LDAP_SUCCESS, 0,
  156. "replication operation not processed, replica unavailable "
  157. "or csn ignored",
  158. 0, 0);
  159. csn_free(&csn);
  160. slapi_ch_free((void **)&target_uuid);
  161. slapi_ch_free((void **)&superior_uuid);
  162. return SLAPI_PLUGIN_FAILURE;
  163. }
  164. operation_set_csn(op, csn);
  165. slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, target_uuid);
  166. slapi_pblock_get(pb, SLAPI_OPERATION_PARAMETERS, &op_params);
  167. /* JCMREPL - Complain if there's no superior uuid */
  168. op_params->p.p_add.parentuniqueid = superior_uuid; /* JCMREPL - Not very elegant */
  169. /* JCMREPL - When do these things get free'd? */
  170. if (target_uuid != NULL) {
  171. /*
  172. * Make sure that the Unique Identifier in the Control is the
  173. * same as the one in the entry.
  174. */
  175. Slapi_Entry *addentry;
  176. char *entry_uuid;
  177. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &addentry);
  178. entry_uuid = slapi_entry_attr_get_charptr(addentry, SLAPI_ATTR_UNIQUEID);
  179. if (entry_uuid == NULL) {
  180. /* Odd that the entry doesn't have a Unique Identifier. But, we can fix it. */
  181. slapi_entry_set_uniqueid(addentry, slapi_ch_strdup(target_uuid)); /* JCMREPL - strdup EVIL! There should be a uuid dup function. */
  182. } else {
  183. if (strcasecmp(entry_uuid, target_uuid) != 0) {
  184. slapi_log_err(SLAPI_LOG_WARNING, REPLICATION_SUBSYSTEM,
  185. "multimaster_preop_add - %s Replicated Add received with Control_UUID=%s and Entry_UUID=%s.\n",
  186. sessionid, target_uuid, entry_uuid);
  187. }
  188. slapi_ch_free((void **)&entry_uuid);
  189. }
  190. }
  191. }
  192. } else {
  193. PR_ASSERT(0); /* JCMREPL - A Replicated Operation with no Repl Baggage control... What does that mean? */
  194. }
  195. } else {
  196. /* Replicated & Fixup Operation */
  197. }
  198. } else {
  199. slapi_operation_set_csngen_handler(op, (void *)replica_generate_next_csn);
  200. }
  201. copy_operation_parameters(pb);
  202. return SLAPI_PLUGIN_SUCCESS;
  203. }
  204. int
  205. multimaster_preop_delete(Slapi_PBlock *pb)
  206. {
  207. Slapi_Operation *op;
  208. int is_replicated_operation;
  209. int is_fixup_operation;
  210. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  211. if (!is_mmr_replica(pb)) {
  212. copy_operation_parameters(pb);
  213. return SLAPI_PLUGIN_SUCCESS;
  214. }
  215. is_replicated_operation = operation_is_flag_set(op, OP_FLAG_REPLICATED);
  216. is_fixup_operation = operation_is_flag_set(op, OP_FLAG_REPL_FIXUP);
  217. if (is_replicated_operation) {
  218. if (!is_fixup_operation) {
  219. LDAPControl **ctrlp;
  220. char sessionid[REPL_SESSION_ID_SIZE];
  221. get_repl_session_id(pb, sessionid, NULL);
  222. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &ctrlp);
  223. if (NULL != ctrlp) {
  224. CSN *csn = NULL;
  225. char *target_uuid = NULL;
  226. int drc = decode_NSDS50ReplUpdateInfoControl(ctrlp, &target_uuid, NULL, &csn, NULL /* modrdn_mods */);
  227. if (-1 == drc) {
  228. slapi_log_err(SLAPI_LOG_ERR, REPLICATION_SUBSYSTEM,
  229. "multimaster_preop_delete - %s An error occurred while decoding the replication update "
  230. "control - Delete\n",
  231. sessionid);
  232. } else if (1 == drc) {
  233. /* we don't want to process replicated operations with csn smaller
  234. than the corresponding csn in the consumer's ruv */
  235. if (!process_operation(pb, csn)) {
  236. slapi_send_ldap_result(pb, LDAP_SUCCESS, 0,
  237. "replication operation not processed, replica unavailable "
  238. "or csn ignored",
  239. 0, 0);
  240. slapi_log_err(SLAPI_LOG_REPL, REPLICATION_SUBSYSTEM,
  241. "multimaster_preop_delete - %s replication operation not processed, replica unavailable "
  242. "or csn ignored\n",
  243. sessionid);
  244. csn_free(&csn);
  245. slapi_ch_free((void **)&target_uuid);
  246. return SLAPI_PLUGIN_FAILURE;
  247. }
  248. /*
  249. * For delete operations, we pass the uniqueid of the deleted entry
  250. * to the backend and let it sort out which entry to really delete.
  251. * We also set the operation csn.
  252. */
  253. operation_set_csn(op, csn);
  254. slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, target_uuid);
  255. }
  256. } else {
  257. PR_ASSERT(0); /* JCMREPL - A Replicated Operation with no Repl Baggage control... What does that mean? */
  258. }
  259. } else {
  260. /* Replicated & Fixup Operation */
  261. }
  262. } else {
  263. slapi_operation_set_csngen_handler(op, (void *)replica_generate_next_csn);
  264. }
  265. copy_operation_parameters(pb);
  266. slapi_operation_set_replica_attr_handler(op, (void *)replica_get_attr);
  267. return SLAPI_PLUGIN_SUCCESS;
  268. }
  269. int
  270. multimaster_preop_modify(Slapi_PBlock *pb)
  271. {
  272. Slapi_Operation *op;
  273. int is_replicated_operation;
  274. int is_fixup_operation;
  275. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  276. if (!is_mmr_replica(pb)) {
  277. copy_operation_parameters(pb);
  278. return SLAPI_PLUGIN_SUCCESS;
  279. }
  280. is_replicated_operation = operation_is_flag_set(op, OP_FLAG_REPLICATED);
  281. is_fixup_operation = operation_is_flag_set(op, OP_FLAG_REPL_FIXUP);
  282. if (is_replicated_operation) {
  283. if (!is_fixup_operation) {
  284. LDAPControl **ctrlp;
  285. char sessionid[REPL_SESSION_ID_SIZE];
  286. get_repl_session_id(pb, sessionid, NULL);
  287. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &ctrlp);
  288. if (NULL != ctrlp) {
  289. CSN *csn = NULL;
  290. char *target_uuid = NULL;
  291. int drc = decode_NSDS50ReplUpdateInfoControl(ctrlp, &target_uuid, NULL, &csn, NULL /* modrdn_mods */);
  292. if (-1 == drc) {
  293. slapi_log_err(SLAPI_LOG_ERR, REPLICATION_SUBSYSTEM,
  294. "multimaster_preop_modify - %s An error occurred while decoding the replication update "
  295. "control- Modify\n",
  296. sessionid);
  297. } else if (1 == drc) {
  298. /* we don't want to process replicated operations with csn smaller
  299. than the corresponding csn in the consumer's ruv */
  300. if (!process_operation(pb, csn)) {
  301. slapi_send_ldap_result(pb, LDAP_SUCCESS, 0,
  302. "replication operation not processed, replica unavailable "
  303. "or csn ignored",
  304. 0, 0);
  305. slapi_log_err(SLAPI_LOG_REPL, REPLICATION_SUBSYSTEM,
  306. "multimaster_preop_modify - %s replication operation not processed, replica unavailable "
  307. "or csn ignored\n",
  308. sessionid);
  309. csn_free(&csn);
  310. slapi_ch_free((void **)&target_uuid);
  311. return SLAPI_PLUGIN_FAILURE;
  312. }
  313. /*
  314. * For modify operations, we pass the uniqueid of the modified entry
  315. * to the backend and let it sort out which entry to really modify.
  316. * We also set the operation csn.
  317. */
  318. operation_set_csn(op, csn);
  319. slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, target_uuid);
  320. }
  321. } else {
  322. /* PR_ASSERT(0); JCMREPL - A Replicated Operation with no Repl Baggage control... What does that mean? */
  323. /*
  324. * This could be RI plugin responding to a replicated update from AD or some other master that is not
  325. * using the RI plugin, so don't PR_ASSERT here. This only happens if we configure the RI plugin with
  326. * "nsslapd-pluginAllowReplUpdates: on", also the RI plugin only issues "modifies".
  327. */
  328. }
  329. } else {
  330. /* Replicated & Fixup Operation */
  331. }
  332. } else {
  333. slapi_operation_set_csngen_handler(op, (void *)replica_generate_next_csn);
  334. }
  335. copy_operation_parameters(pb);
  336. return SLAPI_PLUGIN_SUCCESS;
  337. }
  338. int
  339. multimaster_preop_modrdn(Slapi_PBlock *pb)
  340. {
  341. Slapi_Operation *op;
  342. int is_replicated_operation;
  343. int is_fixup_operation;
  344. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  345. if (!is_mmr_replica(pb)) {
  346. copy_operation_parameters(pb);
  347. return SLAPI_PLUGIN_SUCCESS;
  348. }
  349. is_replicated_operation = operation_is_flag_set(op, OP_FLAG_REPLICATED);
  350. is_fixup_operation = operation_is_flag_set(op, OP_FLAG_REPL_FIXUP);
  351. if (is_replicated_operation) {
  352. if (!is_fixup_operation) {
  353. LDAPControl **ctrlp;
  354. char sessionid[REPL_SESSION_ID_SIZE];
  355. get_repl_session_id(pb, sessionid, NULL);
  356. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &ctrlp);
  357. if (NULL != ctrlp) {
  358. CSN *csn = NULL;
  359. char *target_uuid = NULL;
  360. char *newsuperior_uuid = NULL;
  361. LDAPMod **modrdn_mods = NULL;
  362. int drc = decode_NSDS50ReplUpdateInfoControl(ctrlp, &target_uuid, &newsuperior_uuid,
  363. &csn, &modrdn_mods);
  364. if (-1 == drc) {
  365. slapi_log_err(SLAPI_LOG_ERR, REPLICATION_SUBSYSTEM,
  366. "multimaster_preop_modrdn - %s An error occurred while decoding the replication update "
  367. "control - ModRDN\n",
  368. sessionid);
  369. } else if (1 == drc) {
  370. /*
  371. * For modrdn operations, we pass the uniqueid of the entry being
  372. * renamed to the backend and let it sort out which entry to really
  373. * rename. We also set the operation csn, and if the newsuperior_uuid
  374. * was sent, we decode that as well.
  375. */
  376. struct slapi_operation_parameters *op_params;
  377. /* we don't want to process replicated operations with csn smaller
  378. than the corresponding csn in the consumer's ruv */
  379. if (!process_operation(pb, csn)) {
  380. slapi_send_ldap_result(pb, LDAP_SUCCESS, 0,
  381. "replication operation not processed, replica unavailable "
  382. "or csn ignored",
  383. 0, 0);
  384. csn_free(&csn);
  385. slapi_ch_free((void **)&target_uuid);
  386. slapi_ch_free((void **)&newsuperior_uuid);
  387. ldap_mods_free(modrdn_mods, 1);
  388. return SLAPI_PLUGIN_FAILURE;
  389. }
  390. operation_set_csn(op, csn);
  391. slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, target_uuid);
  392. slapi_pblock_get(pb, SLAPI_OPERATION_PARAMETERS, &op_params);
  393. op_params->p.p_modrdn.modrdn_newsuperior_address.uniqueid = newsuperior_uuid; /* JCMREPL - Not very elegant */
  394. }
  395. /*
  396. * The replicated modrdn operation may also contain a sequence
  397. * that contains side effects of the modrdn operation, e.g. the
  398. * modifiersname and modifytimestamp are updated.
  399. */
  400. if (NULL != modrdn_mods) {
  401. LDAPMod **mods;
  402. Slapi_Mods smods;
  403. int i;
  404. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  405. slapi_mods_init_passin(&smods, mods);
  406. for (i = 0; NULL != modrdn_mods[i]; i++) {
  407. slapi_mods_add_ldapmod(&smods, modrdn_mods[i]); /* Does not copy mod */
  408. }
  409. mods = slapi_mods_get_ldapmods_passout(&smods);
  410. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  411. slapi_mods_done(&smods);
  412. slapi_ch_free((void **)&modrdn_mods); /* Free container only - contents are referred to by array "mods" */
  413. }
  414. } else {
  415. PR_ASSERT(0); /* JCMREPL - A Replicated Operation with no Repl Baggage control... What does that mean? */
  416. }
  417. } else {
  418. /* Replicated & Fixup Operation */
  419. }
  420. } else {
  421. slapi_operation_set_csngen_handler(op, (void *)replica_generate_next_csn);
  422. }
  423. copy_operation_parameters(pb);
  424. return SLAPI_PLUGIN_SUCCESS;
  425. }
  426. int
  427. multimaster_preop_search(Slapi_PBlock *pb __attribute__((unused)))
  428. {
  429. return SLAPI_PLUGIN_SUCCESS;
  430. }
  431. int
  432. multimaster_preop_compare(Slapi_PBlock *pb __attribute__((unused)))
  433. {
  434. return SLAPI_PLUGIN_SUCCESS;
  435. }
  436. int
  437. multimaster_ruv_search(Slapi_PBlock *pb)
  438. {
  439. Slapi_Entry *e, *e_alt;
  440. Slapi_DN *suffix_sdn;
  441. Slapi_Operation *operation;
  442. slapi_pblock_get(pb, SLAPI_SEARCH_ENTRY_ORIG, &e);
  443. slapi_pblock_get(pb, SLAPI_OPERATION, &operation);
  444. if ((e == NULL) || (operation == NULL))
  445. return SLAPI_PLUGIN_SUCCESS;
  446. if (!operation_is_flag_set(operation, OP_FLAG_INTERNAL) && is_ruv_tombstone_entry(e)) {
  447. /* We are about to send back the database RUV, we need to return
  448. * in memory RUV instead
  449. */
  450. /* Retrieve the suffix DN from the RUV entry */
  451. suffix_sdn = slapi_sdn_new();
  452. slapi_sdn_get_parent(slapi_entry_get_sdn(e), suffix_sdn);
  453. /* Now set the in memory RUV into the pblock */
  454. if ((e_alt = get_in_memory_ruv(suffix_sdn)) != NULL) {
  455. slapi_pblock_set(pb, SLAPI_SEARCH_ENTRY_COPY, e_alt);
  456. }
  457. slapi_sdn_free(&suffix_sdn);
  458. }
  459. return SLAPI_PLUGIN_SUCCESS;
  460. }
  461. static void
  462. purge_entry_state_information(Slapi_PBlock *pb)
  463. {
  464. CSN *purge_csn = NULL;
  465. Object *repl_obj;
  466. Replica *replica;
  467. /* we don't want to trim RUV tombstone because we will
  468. deadlock with ourself */
  469. if (ruv_tombstone_op(pb))
  470. return;
  471. repl_obj = replica_get_replica_for_op(pb);
  472. if (NULL != repl_obj) {
  473. replica = object_get_data(repl_obj);
  474. if (NULL != replica) {
  475. purge_csn = replica_get_purge_csn(replica);
  476. }
  477. if (NULL != purge_csn) {
  478. Slapi_Entry *e;
  479. int optype;
  480. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &optype);
  481. switch (optype) {
  482. case SLAPI_OPERATION_MODIFY:
  483. slapi_pblock_get(pb, SLAPI_MODIFY_EXISTING_ENTRY, &e);
  484. break;
  485. case SLAPI_OPERATION_MODRDN:
  486. /* XXXggood - the following always gives a NULL entry - why? */
  487. slapi_pblock_get(pb, SLAPI_MODRDN_EXISTING_ENTRY, &e);
  488. break;
  489. case SLAPI_OPERATION_DELETE:
  490. slapi_pblock_get(pb, SLAPI_DELETE_EXISTING_ENTRY, &e);
  491. break;
  492. default:
  493. e = NULL; /* Don't purge on ADD - not needed */
  494. break;
  495. }
  496. if (NULL != e) {
  497. entry_purge_state_information(e, purge_csn);
  498. /* conntion is always null */
  499. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  500. char csn_str[CSN_STRSIZE];
  501. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  502. "purge_entry_state_information - From entry %s up to "
  503. "CSN %s\n",
  504. slapi_entry_get_dn(e),
  505. csn_as_string(purge_csn, PR_FALSE, csn_str));
  506. }
  507. }
  508. csn_free(&purge_csn);
  509. }
  510. object_release(repl_obj);
  511. }
  512. }
  513. /* pure bepreop's -- should be done before transaction starts */
  514. int
  515. multimaster_bepreop_add(Slapi_PBlock *pb)
  516. {
  517. int rc = SLAPI_PLUGIN_SUCCESS;
  518. Slapi_Operation *op;
  519. int is_replicated_operation;
  520. int is_fixup_operation;
  521. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  522. is_replicated_operation = operation_is_flag_set(op, OP_FLAG_REPLICATED);
  523. is_fixup_operation = operation_is_flag_set(op, OP_FLAG_REPL_FIXUP);
  524. /* For replicated operations, apply URP algorithm */
  525. if (!is_fixup_operation) {
  526. slapi_pblock_set(pb, SLAPI_TXN_RUV_MODS_FN,
  527. (void *)replica_ruv_smods_for_op);
  528. if (is_replicated_operation) {
  529. rc = urp_add_operation(pb);
  530. }
  531. }
  532. return rc;
  533. }
  534. int
  535. multimaster_bepreop_delete(Slapi_PBlock *pb)
  536. {
  537. int rc = SLAPI_PLUGIN_SUCCESS;
  538. Slapi_Operation *op;
  539. int is_replicated_operation;
  540. int is_fixup_operation;
  541. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  542. is_replicated_operation = operation_is_flag_set(op, OP_FLAG_REPLICATED);
  543. is_fixup_operation = operation_is_flag_set(op, OP_FLAG_REPL_FIXUP);
  544. /* For replicated operations, apply URP algorithm */
  545. if (!is_fixup_operation) {
  546. slapi_pblock_set(pb, SLAPI_TXN_RUV_MODS_FN,
  547. (void *)replica_ruv_smods_for_op);
  548. if (is_replicated_operation) {
  549. rc = urp_delete_operation(pb);
  550. }
  551. }
  552. return rc;
  553. }
  554. int
  555. multimaster_bepreop_modify(Slapi_PBlock *pb)
  556. {
  557. int rc = SLAPI_PLUGIN_SUCCESS;
  558. Slapi_Operation *op;
  559. int is_replicated_operation;
  560. int is_fixup_operation;
  561. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  562. is_replicated_operation = operation_is_flag_set(op, OP_FLAG_REPLICATED);
  563. is_fixup_operation = operation_is_flag_set(op, OP_FLAG_REPL_FIXUP);
  564. /* For replicated operations, apply URP algorithm */
  565. if (!is_fixup_operation) {
  566. slapi_pblock_set(pb, SLAPI_TXN_RUV_MODS_FN,
  567. (void *)replica_ruv_smods_for_op);
  568. if (is_replicated_operation) {
  569. rc = urp_modify_operation(pb);
  570. }
  571. }
  572. /* Clean up old state information */
  573. purge_entry_state_information(pb);
  574. return rc;
  575. }
  576. int
  577. multimaster_bepreop_modrdn(Slapi_PBlock *pb)
  578. {
  579. int rc = SLAPI_PLUGIN_SUCCESS;
  580. Slapi_Operation *op;
  581. int is_replicated_operation;
  582. int is_fixup_operation;
  583. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  584. is_replicated_operation = operation_is_flag_set(op, OP_FLAG_REPLICATED);
  585. is_fixup_operation = operation_is_flag_set(op, OP_FLAG_REPL_FIXUP);
  586. /* For replicated operations, apply URP algorithm */
  587. if (!is_fixup_operation) {
  588. slapi_pblock_set(pb, SLAPI_TXN_RUV_MODS_FN,
  589. (void *)replica_ruv_smods_for_op);
  590. if (is_replicated_operation) {
  591. rc = urp_modrdn_operation(pb);
  592. }
  593. }
  594. /* Clean up old state information */
  595. purge_entry_state_information(pb);
  596. return rc;
  597. }
  598. int
  599. multimaster_bepostop_modrdn(Slapi_PBlock *pb)
  600. {
  601. Slapi_Operation *op;
  602. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  603. if (!operation_is_flag_set(op, OP_FLAG_REPL_FIXUP)) {
  604. urp_post_modrdn_operation(pb);
  605. }
  606. return SLAPI_PLUGIN_SUCCESS;
  607. }
  608. int
  609. multimaster_bepostop_delete(Slapi_PBlock *pb)
  610. {
  611. Slapi_Operation *op;
  612. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  613. if (!operation_is_flag_set(op, OP_FLAG_REPL_FIXUP)) {
  614. urp_post_delete_operation(pb);
  615. }
  616. return SLAPI_PLUGIN_SUCCESS;
  617. }
  618. /* postop - write to changelog */
  619. int
  620. multimaster_postop_bind(Slapi_PBlock *pb __attribute__((unused)))
  621. {
  622. return SLAPI_PLUGIN_SUCCESS;
  623. }
  624. int
  625. multimaster_postop_add(Slapi_PBlock *pb)
  626. {
  627. return process_postop(pb);
  628. }
  629. int
  630. multimaster_postop_delete(Slapi_PBlock *pb)
  631. {
  632. return process_postop(pb);
  633. }
  634. int
  635. multimaster_postop_modify(Slapi_PBlock *pb)
  636. {
  637. return process_postop(pb);
  638. }
  639. int
  640. multimaster_postop_modrdn(Slapi_PBlock *pb)
  641. {
  642. return process_postop(pb);
  643. }
  644. int
  645. multimaster_betxnpostop_delete(Slapi_PBlock *pb)
  646. {
  647. return write_changelog_and_ruv(pb);
  648. }
  649. int
  650. multimaster_betxnpostop_modrdn(Slapi_PBlock *pb)
  651. {
  652. return write_changelog_and_ruv(pb);
  653. }
  654. int
  655. multimaster_betxnpostop_add(Slapi_PBlock *pb)
  656. {
  657. return write_changelog_and_ruv(pb);
  658. }
  659. int
  660. multimaster_betxnpostop_modify(Slapi_PBlock *pb)
  661. {
  662. return write_changelog_and_ruv(pb);
  663. }
  664. /* If nsslapd-pluginbetxn is on */
  665. int
  666. multimaster_be_betxnpostop_delete(Slapi_PBlock *pb)
  667. {
  668. int rc = SLAPI_PLUGIN_SUCCESS;
  669. /* original betxnpost */
  670. rc = write_changelog_and_ruv(pb);
  671. /* original bepost */
  672. rc |= multimaster_bepostop_delete(pb);
  673. return rc;
  674. }
  675. int
  676. multimaster_be_betxnpostop_modrdn(Slapi_PBlock *pb)
  677. {
  678. int rc = 0;
  679. /* original betxnpost */
  680. rc = write_changelog_and_ruv(pb);
  681. /* original bepost */
  682. rc |= multimaster_bepostop_modrdn(pb);
  683. return rc;
  684. }
  685. int
  686. multimaster_be_betxnpostop_add(Slapi_PBlock *pb)
  687. {
  688. int rc = 0;
  689. /* original betxnpost */
  690. rc = write_changelog_and_ruv(pb);
  691. return rc;
  692. }
  693. int
  694. multimaster_be_betxnpostop_modify(Slapi_PBlock *pb)
  695. {
  696. int rc = 0;
  697. /* original betxnpost */
  698. rc = write_changelog_and_ruv(pb);
  699. return rc;
  700. }
  701. /* Helper functions */
  702. /*
  703. * This function makes a copy of the operation parameters
  704. * and stashes them in the consumer operation extension.
  705. * This is where the 5.0 Change Log will get the operation
  706. * details from.
  707. */
  708. static void
  709. copy_operation_parameters(Slapi_PBlock *pb)
  710. {
  711. Slapi_Operation *op = NULL;
  712. struct slapi_operation_parameters *op_params;
  713. supplier_operation_extension *opext;
  714. Object *repl_obj;
  715. Replica *replica;
  716. repl_obj = replica_get_replica_for_op(pb);
  717. /* we are only interested in the updates to replicas */
  718. if (repl_obj) {
  719. /* we only save the original operation parameters for replicated operations
  720. since client operations don't go through urp engine and pblock data can be logged */
  721. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  722. if (NULL == op) {
  723. slapi_log_err(SLAPI_LOG_REPL, REPLICATION_SUBSYSTEM,
  724. "copy_operation_parameters - operation is null.\n");
  725. return;
  726. }
  727. replica = (Replica *)object_get_data(repl_obj);
  728. if (NULL == replica) {
  729. slapi_log_err(SLAPI_LOG_REPL, REPLICATION_SUBSYSTEM,
  730. "copy_operation_parameters - replica is null.\n");
  731. return;
  732. }
  733. opext = (supplier_operation_extension *)repl_sup_get_ext(REPL_SUP_EXT_OP, op);
  734. if (operation_is_flag_set(op, OP_FLAG_REPLICATED) &&
  735. !operation_is_flag_set(op, OP_FLAG_REPL_FIXUP)) {
  736. slapi_pblock_get(pb, SLAPI_OPERATION_PARAMETERS, &op_params);
  737. opext->operation_parameters = operation_parameters_dup(op_params);
  738. }
  739. /* this condition is needed to avoid re-entering backend serial lock
  740. when ruv state is updated */
  741. if (!operation_is_flag_set(op, OP_FLAG_REPL_FIXUP)) {
  742. /* save replica generation in case it changes */
  743. opext->repl_gen = replica_get_generation(replica);
  744. }
  745. object_release(repl_obj);
  746. }
  747. }
  748. /*
  749. * Helper function: update the RUV so that it reflects the
  750. * locally-processed update. This is called for both replicated
  751. * and non-replicated operations.
  752. */
  753. static int
  754. update_ruv_component(Replica *replica, CSN *opcsn, Slapi_PBlock *pb)
  755. {
  756. char *purl;
  757. int rc = RUV_NOTFOUND;
  758. if (!replica || !opcsn)
  759. return rc;
  760. /* Replica configured, so update its ruv */
  761. purl = (char *)replica_get_purl_for_op(replica, pb, opcsn);
  762. rc = replica_update_ruv(replica, opcsn, purl);
  763. return rc;
  764. }
  765. /*
  766. * Write the changelog. Note: it is an error to call write_changelog_and_ruv() for fixup
  767. * operations. The caller should avoid calling this function if the operation is
  768. * a fixup op.
  769. * Also update the ruv - we need to do this while we have the replica lock acquired
  770. * so that the csn is written to the changelog and the ruv is updated with the csn
  771. * in one atomic operation - if there is no changelog, we still need to update
  772. * the ruv (e.g. for consumers)
  773. */
  774. static int
  775. write_changelog_and_ruv(Slapi_PBlock *pb)
  776. {
  777. Slapi_Operation *op = NULL;
  778. CSN *opcsn;
  779. CSNPL_CTX *prim_csn;
  780. int rc;
  781. slapi_operation_parameters *op_params = NULL;
  782. Object *repl_obj = NULL;
  783. int return_value = SLAPI_PLUGIN_SUCCESS;
  784. Replica *r;
  785. Slapi_Backend *be;
  786. int is_replicated_operation = 0;
  787. /* we just let fixup operations through */
  788. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  789. if (NULL == op) {
  790. return return_value;
  791. }
  792. if ((operation_is_flag_set(op, OP_FLAG_REPL_FIXUP)) ||
  793. (operation_is_flag_set(op, OP_FLAG_TOMBSTONE_ENTRY))) {
  794. return return_value;
  795. }
  796. /* ignore operations intended for chaining backends - they will be
  797. replicated back to us or should be ignored anyway
  798. replicated operations should be processed normally, as they should
  799. be going to a local backend */
  800. is_replicated_operation = operation_is_flag_set(op, OP_FLAG_REPLICATED);
  801. slapi_pblock_get(pb, SLAPI_BACKEND, &be);
  802. if (!is_replicated_operation &&
  803. slapi_be_is_flag_set(be, SLAPI_BE_FLAG_REMOTE_DATA)) {
  804. return return_value;
  805. }
  806. /* we only log changes for operations applied to a replica */
  807. repl_obj = replica_get_replica_for_op(pb);
  808. if (repl_obj == NULL)
  809. return return_value;
  810. r = (Replica *)object_get_data(repl_obj);
  811. PR_ASSERT(r);
  812. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &rc);
  813. if (rc) { /* op failed - just return */
  814. cancel_opcsn(pb);
  815. goto common_return;
  816. }
  817. replica_check_release_timeout(r, pb);
  818. if (replica_is_flag_set(r, REPLICA_LOG_CHANGES) &&
  819. (cl5GetState() == CL5_STATE_OPEN)) {
  820. supplier_operation_extension *opext = NULL;
  821. const char *repl_name;
  822. char *repl_gen;
  823. opext = (supplier_operation_extension *)repl_sup_get_ext(REPL_SUP_EXT_OP, op);
  824. PR_ASSERT(opext);
  825. /* get replica generation and replica name to pass to the write function */
  826. repl_name = replica_get_name(r);
  827. repl_gen = opext->repl_gen;
  828. PR_ASSERT(repl_name && repl_gen);
  829. /* for replicated operations, we log the original, non-urp data which is
  830. saved in the operation extension */
  831. if (operation_is_flag_set(op, OP_FLAG_REPLICATED)) {
  832. PR_ASSERT(opext->operation_parameters);
  833. op_params = opext->operation_parameters;
  834. } else /* since client operations don't go through urp, we log the operation data in pblock */
  835. {
  836. Slapi_Entry *e = NULL;
  837. const char *uniqueid = NULL;
  838. slapi_pblock_get(pb, SLAPI_OPERATION_PARAMETERS, &op_params);
  839. if (NULL == op_params) {
  840. goto common_return;
  841. }
  842. /* need to set uniqueid operation parameter */
  843. /* we try to use the appropriate entry (pre op or post op)
  844. depending on the type of operation (add, delete, modify)
  845. However, in some cases, the backend operation may fail in
  846. a non fatal way (e.g. attempt to remove an attribute value
  847. that does not exist) but we still need to log the change.
  848. So, the POST_OP entry may not have been set in the FE modify
  849. code. In that case, we use the PRE_OP entry.
  850. */
  851. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  852. if ((e == NULL) ||
  853. (op_params->operation_type == SLAPI_OPERATION_DELETE)) {
  854. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &e);
  855. }
  856. if (NULL == e) {
  857. goto common_return;
  858. }
  859. uniqueid = slapi_entry_get_uniqueid(e);
  860. if (NULL == uniqueid) {
  861. goto common_return;
  862. }
  863. op_params->target_address.uniqueid = slapi_ch_strdup(uniqueid);
  864. }
  865. if (op_params->csn && is_cleaned_rid(csn_get_replicaid(op_params->csn))) {
  866. /* this RID has been cleaned */
  867. goto common_return;
  868. }
  869. /* we might have stripped all the mods - in that case we do not
  870. log the operation */
  871. if (op_params->operation_type != SLAPI_OPERATION_MODIFY ||
  872. op_params->p.p_modify.modify_mods != NULL) {
  873. void *txn = NULL;
  874. if (cl5_is_diskfull() && !cl5_diskspace_is_available()) {
  875. slapi_log_err(SLAPI_LOG_CRIT, repl_plugin_name,
  876. "write_changelog_and_ruv - Skipped due to DISKFULL\n");
  877. goto common_return;
  878. }
  879. slapi_pblock_get(pb, SLAPI_TXN, &txn);
  880. rc = cl5WriteOperationTxn(repl_name, repl_gen, op_params,
  881. !operation_is_flag_set(op, OP_FLAG_REPLICATED), txn);
  882. if (rc != CL5_SUCCESS) {
  883. char csn_str[CSN_STRSIZE];
  884. /* ONREPL - log error */
  885. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  886. "write_changelog_and_ruv - Can't add a change for "
  887. "%s (uniqid: %s, optype: %lu) to changelog csn %s\n",
  888. REPL_GET_DN(&op_params->target_address),
  889. op_params->target_address.uniqueid,
  890. op_params->operation_type,
  891. csn_as_string(op_params->csn, PR_FALSE, csn_str));
  892. return_value = SLAPI_PLUGIN_FAILURE;
  893. }
  894. }
  895. if (!operation_is_flag_set(op, OP_FLAG_REPLICATED)) {
  896. slapi_ch_free((void **)&op_params->target_address.uniqueid);
  897. }
  898. }
  899. /*
  900. This was moved because we need to write the changelog and update
  901. the ruv in one atomic operation - I have seen cases where the inc
  902. protocol thread interrupts this thread between the time the changelog
  903. is written and the ruv is updated - this causes confusion in several
  904. places, especially in _cl5SkipReplayEntry since it cannot find the csn it
  905. just read from the changelog in either the supplier or consumer ruv
  906. */
  907. if (0 == return_value) {
  908. char csn_str[CSN_STRSIZE] = {'\0'};
  909. int rc;
  910. const char *dn = op_params ? REPL_GET_DN(&op_params->target_address) : "unknown";
  911. Slapi_DN *sdn = op_params ? (&op_params->target_address)->sdn : NULL;
  912. char *uniqueid = op_params ? op_params->target_address.uniqueid : "unknown";
  913. unsigned long optype = op_params ? op_params->operation_type : 0;
  914. CSN *oppcsn = op_params ? op_params->csn : NULL;
  915. LDAPMod **mods = op_params ? op_params->p.p_modify.modify_mods : NULL;
  916. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  917. opcsn = operation_get_csn(op);
  918. /* Update each agmt's maxcsn */
  919. if (op_params && sdn) {
  920. agmt_update_maxcsn(r, sdn, op_params->operation_type, mods, opcsn);
  921. }
  922. rc = update_ruv_component(r, opcsn, pb);
  923. if (RUV_COVERS_CSN == rc) {
  924. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  925. "write_changelog_and_ruv - RUV already covers csn for "
  926. "%s (uniqid: %s, optype: %lu) csn %s\n",
  927. dn, uniqueid, optype,
  928. csn_as_string(oppcsn, PR_FALSE, csn_str));
  929. } else if ((rc != RUV_SUCCESS) && (rc != RUV_NOTFOUND)) {
  930. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  931. "write_changelog_and_ruv - Failed to update RUV for "
  932. "%s (uniqid: %s, optype: %lu) to changelog csn %s - rc %d\n",
  933. dn, uniqueid, optype,
  934. csn_as_string(oppcsn, PR_FALSE, csn_str), rc);
  935. }
  936. }
  937. common_return:
  938. opcsn = operation_get_csn(op);
  939. prim_csn = get_thread_primary_csn();
  940. if (csn_primary(r, opcsn, prim_csn)) {
  941. if (return_value == 0) {
  942. /* the primary csn was succesfully committed
  943. * unset it in the thread local data
  944. */
  945. set_thread_primary_csn(NULL, NULL);
  946. }
  947. }
  948. if (repl_obj) {
  949. object_release(repl_obj);
  950. }
  951. return return_value;
  952. }
  953. /*
  954. * Postop processing - write the changelog if the operation resulted in
  955. * an LDAP_SUCCESS result code, update the RUV, and notify the replication
  956. * agreements about the change.
  957. * If the result code is not LDAP_SUCCESS, then cancel the operation CSN.
  958. */
  959. static int
  960. process_postop(Slapi_PBlock *pb)
  961. {
  962. Slapi_Operation *op;
  963. Slapi_Backend *be;
  964. int is_replicated_operation = 0;
  965. CSN *opcsn = NULL;
  966. char sessionid[REPL_SESSION_ID_SIZE];
  967. int retval = LDAP_SUCCESS;
  968. int rc = 0;
  969. /* we just let fixup operations through */
  970. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  971. if ((operation_is_flag_set(op, OP_FLAG_REPL_FIXUP)) ||
  972. (operation_is_flag_set(op, OP_FLAG_TOMBSTONE_ENTRY))) {
  973. return SLAPI_PLUGIN_SUCCESS;
  974. }
  975. /* ignore operations intended for chaining backends - they will be
  976. replicated back to us or should be ignored anyway
  977. replicated operations should be processed normally, as they should
  978. be going to a local backend */
  979. is_replicated_operation = operation_is_flag_set(op, OP_FLAG_REPLICATED);
  980. slapi_pblock_get(pb, SLAPI_BACKEND, &be);
  981. if (!is_replicated_operation &&
  982. slapi_be_is_flag_set(be, SLAPI_BE_FLAG_REMOTE_DATA)) {
  983. return SLAPI_PLUGIN_SUCCESS;
  984. }
  985. get_repl_session_id(pb, sessionid, &opcsn);
  986. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &retval);
  987. if (retval == LDAP_SUCCESS) {
  988. agmtlist_notify_all(pb);
  989. rc = SLAPI_PLUGIN_SUCCESS;
  990. } else if (opcsn) {
  991. rc = cancel_opcsn(pb);
  992. /* Don't try to get session id since conn is always null */
  993. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  994. "process postop - %s canceling operation csn\n", sessionid);
  995. } else {
  996. rc = SLAPI_PLUGIN_FAILURE;
  997. }
  998. /* the target unique id is set in the modify_preop above, so
  999. we need to free it */
  1000. /* The following bunch of frees code does not belong to this place
  1001. * but rather to operation_free who should be responsible for calling
  1002. * operation_parameters_free and it doesn't. I guess this is like this
  1003. * because several crashes happened in the past regarding some opparams
  1004. * that were getting individually freed before they should. Whatever
  1005. * the case, this is not the place and we should make sure that this
  1006. * code gets removed for 5.next and substituted by the strategy (operation_free).
  1007. * For 5.0, such change is too risky, so this will be done here */
  1008. if (is_replicated_operation) {
  1009. slapi_operation_parameters *op_params = NULL;
  1010. int optype = 0;
  1011. /* target uid and csn are set for all repl operations. Free them */
  1012. char *target_uuid = NULL;
  1013. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &optype);
  1014. slapi_pblock_get(pb, SLAPI_TARGET_UNIQUEID, &target_uuid);
  1015. slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, NULL);
  1016. slapi_ch_free((void **)&target_uuid);
  1017. if (optype == SLAPI_OPERATION_ADD) {
  1018. slapi_pblock_get(pb, SLAPI_OPERATION_PARAMETERS, &op_params);
  1019. slapi_ch_free((void **)&op_params->p.p_add.parentuniqueid);
  1020. }
  1021. if (optype == SLAPI_OPERATION_MODRDN) {
  1022. slapi_pblock_get(pb, SLAPI_OPERATION_PARAMETERS, &op_params);
  1023. slapi_ch_free((void **)&op_params->p.p_modrdn.modrdn_newsuperior_address.uniqueid);
  1024. }
  1025. if (!ignore_error_and_keep_going(retval)) {
  1026. /*
  1027. * We have an error we can't ignore. Release the replica and close
  1028. * the connection to stop the replication session.
  1029. */
  1030. consumer_connection_extension *connext = NULL;
  1031. Slapi_Connection *conn = NULL;
  1032. char csn_str[CSN_STRSIZE] = {'\0'};
  1033. PRUint64 connid = 0;
  1034. int opid = 0;
  1035. slapi_pblock_get(pb, SLAPI_CONNECTION, &conn);
  1036. slapi_pblock_get(pb, SLAPI_OPERATION_ID, &opid);
  1037. slapi_pblock_get(pb, SLAPI_CONN_ID, &connid);
  1038. if (conn) {
  1039. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  1040. "process_postop - Failed to apply update (%s) error (%d). "
  1041. "Aborting replication session(conn=%" PRIu64 " op=%d)\n",
  1042. csn_as_string(opcsn, PR_FALSE, csn_str), retval,
  1043. connid, opid);
  1044. /*
  1045. * Release this replica so new sessions can begin
  1046. */
  1047. connext = consumer_connection_extension_acquire_exclusive_access(conn, connid, opid);
  1048. if (connext && connext->replica_acquired) {
  1049. int zero = 0;
  1050. Replica *r = (Replica *)object_get_data((Object *)connext->replica_acquired);
  1051. replica_relinquish_exclusive_access(r, connid, opid);
  1052. object_release((Object *)connext->replica_acquired);
  1053. connext->replica_acquired = NULL;
  1054. connext->isreplicationsession = 0;
  1055. slapi_pblock_set(pb, SLAPI_CONN_IS_REPLICATION_SESSION, &zero);
  1056. }
  1057. if (connext) {
  1058. consumer_connection_extension_relinquish_exclusive_access(conn, connid, opid, PR_FALSE);
  1059. }
  1060. /*
  1061. * Close the connection to end the current session with the
  1062. * supplier. This prevents new updates from coming in and
  1063. * updating the consumer RUV - which would cause this failed
  1064. * update to be never be replayed.
  1065. */
  1066. slapi_disconnect_server(conn);
  1067. }
  1068. }
  1069. }
  1070. if (NULL == opcsn)
  1071. opcsn = operation_get_csn(op);
  1072. if (opcsn)
  1073. csn_free(&opcsn);
  1074. return rc;
  1075. }
  1076. /*
  1077. * Cancel an operation CSN. This removes it from any CSN pending lists.
  1078. * This function is called when a previously-generated CSN will not
  1079. * be needed, e.g. if the update operation produced an error.
  1080. */
  1081. static int
  1082. cancel_opcsn(Slapi_PBlock *pb)
  1083. {
  1084. Object *repl_obj;
  1085. Slapi_Operation *op = NULL;
  1086. if (NULL == pb) {
  1087. return SLAPI_PLUGIN_SUCCESS;
  1088. }
  1089. repl_obj = replica_get_replica_for_op(pb);
  1090. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  1091. if (NULL == op) {
  1092. return SLAPI_PLUGIN_SUCCESS;
  1093. }
  1094. if (repl_obj) {
  1095. Replica *r;
  1096. Object *gen_obj;
  1097. CSNGen *gen;
  1098. CSN *opcsn;
  1099. r = (Replica *)object_get_data(repl_obj);
  1100. PR_ASSERT(r);
  1101. opcsn = operation_get_csn(op);
  1102. if (!operation_is_flag_set(op, OP_FLAG_REPLICATED)) {
  1103. /* get csn generator for the replica */
  1104. gen_obj = replica_get_csngen(r);
  1105. PR_ASSERT(gen_obj);
  1106. gen = (CSNGen *)object_get_data(gen_obj);
  1107. if (NULL != opcsn) {
  1108. csngen_abort_csn(gen, operation_get_csn(op));
  1109. }
  1110. object_release(gen_obj);
  1111. } else if (!operation_is_flag_set(op, OP_FLAG_REPL_FIXUP)) {
  1112. Object *ruv_obj;
  1113. ruv_obj = replica_get_ruv(r);
  1114. PR_ASSERT(ruv_obj);
  1115. ruv_cancel_csn_inprogress(r, (RUV *)object_get_data(ruv_obj), opcsn, replica_get_rid(r));
  1116. object_release(ruv_obj);
  1117. }
  1118. object_release(repl_obj);
  1119. }
  1120. return SLAPI_PLUGIN_SUCCESS;
  1121. }
  1122. /*
  1123. * Return non-zero if the target entry DN is the DN of the RUV tombstone
  1124. * entry.
  1125. * The entry has rdn of nsuniqueid = ffffffff-ffffffff-ffffffff-ffffffff
  1126. */
  1127. static int
  1128. ruv_tombstone_op(Slapi_PBlock *pb)
  1129. {
  1130. char *uniqueid;
  1131. int rc;
  1132. slapi_pblock_get(pb, SLAPI_TARGET_UNIQUEID, &uniqueid);
  1133. rc = uniqueid && strcasecmp(uniqueid, RUV_STORAGE_ENTRY_UNIQUEID) == 0;
  1134. return rc;
  1135. }
  1136. /* we don't want to process replicated operations with csn smaller
  1137. than the corresponding csn in the consumer's ruv */
  1138. static PRBool
  1139. process_operation(Slapi_PBlock *pb, const CSN *csn)
  1140. {
  1141. Object *r_obj;
  1142. Replica *r;
  1143. Object *ruv_obj;
  1144. RUV *ruv;
  1145. int rc;
  1146. r_obj = replica_get_replica_for_op(pb);
  1147. if (r_obj == NULL) {
  1148. char sessionid[REPL_SESSION_ID_SIZE];
  1149. get_repl_session_id(pb, sessionid, NULL);
  1150. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "process_operation - "
  1151. "%s - Can't locate replica for the replicated operation\n",
  1152. sessionid);
  1153. return PR_FALSE;
  1154. }
  1155. r = (Replica *)object_get_data(r_obj);
  1156. PR_ASSERT(r);
  1157. ruv_obj = replica_get_ruv(r);
  1158. PR_ASSERT(ruv_obj);
  1159. ruv = (RUV *)object_get_data(ruv_obj);
  1160. PR_ASSERT(ruv);
  1161. rc = ruv_add_csn_inprogress(r, ruv, csn);
  1162. object_release(ruv_obj);
  1163. object_release(r_obj);
  1164. return (rc == RUV_SUCCESS);
  1165. }
  1166. static PRBool
  1167. is_mmr_replica(Slapi_PBlock *pb)
  1168. {
  1169. Object *r_obj;
  1170. r_obj = replica_get_replica_for_op(pb);
  1171. if (r_obj == NULL) {
  1172. return PR_FALSE;
  1173. }
  1174. object_release(r_obj);
  1175. return PR_TRUE;
  1176. }
  1177. static const char *
  1178. replica_get_purl_for_op(const Replica *r __attribute__((unused)), Slapi_PBlock *pb, const CSN *opcsn)
  1179. {
  1180. int is_replicated_op;
  1181. const char *purl = NULL;
  1182. slapi_pblock_get(pb, SLAPI_IS_MMR_REPLICATED_OPERATION, &is_replicated_op);
  1183. if (!is_replicated_op) {
  1184. purl = multimaster_get_local_purl();
  1185. } else {
  1186. /* Get the appropriate partial URL from the supplier RUV */
  1187. Slapi_Connection *conn;
  1188. consumer_connection_extension *connext;
  1189. slapi_pblock_get(pb, SLAPI_CONNECTION, &conn);
  1190. /* TEL 20120531: There is a slim chance we want to take exclusive access
  1191. * to this instead. However, it isn't clear to me that it is worth the
  1192. * risk of changing this working code. */
  1193. connext = (consumer_connection_extension *)repl_con_get_ext(
  1194. REPL_CON_EXT_CONN, conn);
  1195. if (NULL == connext || NULL == connext->supplier_ruv) {
  1196. char sessionid[REPL_SESSION_ID_SIZE];
  1197. get_repl_session_id(pb, sessionid, NULL);
  1198. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_get_purl_for_op - "
  1199. "%s - Cannot obtain consumer connection extension or supplier_ruv.\n",
  1200. sessionid);
  1201. } else {
  1202. purl = ruv_get_purl_for_replica(connext->supplier_ruv,
  1203. csn_get_replicaid(opcsn));
  1204. }
  1205. }
  1206. return purl;
  1207. }
  1208. /* this function is called when state of a backend changes */
  1209. void
  1210. multimaster_be_state_change(void *handle __attribute__((unused)), char *be_name, int old_be_state, int new_be_state)
  1211. {
  1212. Object *r_obj;
  1213. Replica *r;
  1214. /* check if we have replica associated with the backend */
  1215. r_obj = replica_get_for_backend(be_name);
  1216. if (r_obj == NULL)
  1217. return;
  1218. r = (Replica *)object_get_data(r_obj);
  1219. PR_ASSERT(r);
  1220. if (new_be_state == SLAPI_BE_STATE_ON) {
  1221. /* backend came back online - restart replication */
  1222. slapi_log_err(SLAPI_LOG_NOTICE, repl_plugin_name, "multimaster_be_state_change - "
  1223. "Replica %s is coming online; enabling replication\n",
  1224. slapi_sdn_get_ndn(replica_get_root(r)));
  1225. replica_enable_replication(r);
  1226. } else if (new_be_state == SLAPI_BE_STATE_OFFLINE) {
  1227. /* backend is about to be taken down - disable replication */
  1228. slapi_log_err(SLAPI_LOG_NOTICE, repl_plugin_name, "multimaster_be_state_change - "
  1229. "Replica %s is going offline; disabling replication\n",
  1230. slapi_sdn_get_ndn(replica_get_root(r)));
  1231. replica_disable_replication(r, r_obj);
  1232. } else if (new_be_state == SLAPI_BE_STATE_DELETE) {
  1233. /* backend is about to be removed - disable replication */
  1234. if (old_be_state == SLAPI_BE_STATE_ON) {
  1235. slapi_log_err(SLAPI_LOG_NOTICE, repl_plugin_name, "multimaster_be_state_change - "
  1236. "Replica %s is about to be deleted; disabling replication\n",
  1237. slapi_sdn_get_ndn(replica_get_root(r)));
  1238. replica_disable_replication(r, r_obj);
  1239. }
  1240. }
  1241. object_release(r_obj);
  1242. }