repl5_plugins.c 44 KB

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