repl5_plugins.c 52 KB

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