repl5_plugins.c 46 KB

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