repl5_plugins.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  37. * All rights reserved.
  38. * END COPYRIGHT BLOCK **/
  39. #ifdef HAVE_CONFIG_H
  40. # include <config.h>
  41. #endif
  42. /*
  43. * repl5_consumer.c - consumer plugin functions
  44. */
  45. /*
  46. * LDAP Data Model Constraints...
  47. *
  48. * 1) Parent entry must exist.
  49. * 2) RDN must be unique.
  50. * 3) RDN components must be attribute values.
  51. * 4) Must conform to the schema constraints - Single Valued Attributes.
  52. * 5) Must conform to the schema constraints - Required Attributes.
  53. * 6) Must conform to the schema constraints - No Extra Attributes.
  54. * 7) Duplicate attribute values are not permited.
  55. * 8) Cycle in the ancestry graph not permitted.
  56. *
  57. * Update Resolution Procedures...
  58. * 1) ...
  59. * 2) Add the UniqueID to the RDN.
  60. * 3) Remove the not present RDN component.
  61. * Use the UniqueID if the RDN becomes empty.
  62. * 4) Keep the most recent value.
  63. * 5) Ignore.
  64. * 6) Ignore.
  65. * 7) Keep the largest CSN for the duplicate value.
  66. * 8) Don't check for this.
  67. */
  68. #include "repl5.h"
  69. #include "repl.h"
  70. #include "cl5_api.h"
  71. #include "urp.h"
  72. static char *local_purl = NULL;
  73. static char *purl_attrs[] = {"nsslapd-localhost", "nsslapd-port", "nsslapd-secureport", NULL};
  74. /* Forward declarations */
  75. static void copy_operation_parameters(Slapi_PBlock *pb);
  76. static int write_changelog_and_ruv(Slapi_PBlock *pb);
  77. static int process_postop (Slapi_PBlock *pb);
  78. static int cancel_opcsn (Slapi_PBlock *pb);
  79. static int ruv_tombstone_op (Slapi_PBlock *pb);
  80. static PRBool process_operation (Slapi_PBlock *pb, const CSN *csn);
  81. static PRBool is_mmr_replica (Slapi_PBlock *pb);
  82. static const char *replica_get_purl_for_op (const Replica *r, Slapi_PBlock *pb, const CSN *opcsn);
  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. entry_purge_state_information(e, purge_csn);
  611. /* conntion is always null */
  612. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  613. char csn_str[CSN_STRSIZE];
  614. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  615. "Purged state information from entry %s up to "
  616. "CSN %s\n", slapi_entry_get_dn(e),
  617. csn_as_string(purge_csn, PR_FALSE, csn_str));
  618. }
  619. }
  620. csn_free(&purge_csn);
  621. }
  622. object_release(repl_obj);
  623. }
  624. }
  625. int
  626. multimaster_bepreop_add (Slapi_PBlock *pb)
  627. {
  628. int rc= 0;
  629. Slapi_Operation *op;
  630. int is_replicated_operation;
  631. int is_fixup_operation;
  632. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  633. is_replicated_operation= operation_is_flag_set(op,OP_FLAG_REPLICATED);
  634. is_fixup_operation= operation_is_flag_set(op,OP_FLAG_REPL_FIXUP);
  635. /* For replicated operations, apply URP algorithm */
  636. if (!is_fixup_operation)
  637. {
  638. slapi_pblock_set(pb, SLAPI_TXN_RUV_MODS_FN,
  639. (void *)replica_ruv_smods_for_op);
  640. if (is_replicated_operation) {
  641. rc = urp_add_operation(pb);
  642. }
  643. }
  644. return rc;
  645. }
  646. int
  647. multimaster_bepreop_delete (Slapi_PBlock *pb)
  648. {
  649. int rc= 0;
  650. Slapi_Operation *op;
  651. int is_replicated_operation;
  652. int is_fixup_operation;
  653. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  654. is_replicated_operation= operation_is_flag_set(op,OP_FLAG_REPLICATED);
  655. is_fixup_operation= operation_is_flag_set(op,OP_FLAG_REPL_FIXUP);
  656. /* For replicated operations, apply URP algorithm */
  657. if(!is_fixup_operation)
  658. {
  659. slapi_pblock_set(pb, SLAPI_TXN_RUV_MODS_FN,
  660. (void *)replica_ruv_smods_for_op);
  661. if (is_replicated_operation) {
  662. rc = urp_delete_operation(pb);
  663. }
  664. }
  665. return rc;
  666. }
  667. int
  668. multimaster_bepreop_modify (Slapi_PBlock *pb)
  669. {
  670. int rc= 0;
  671. Slapi_Operation *op;
  672. int is_replicated_operation;
  673. int is_fixup_operation;
  674. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  675. is_replicated_operation= operation_is_flag_set(op,OP_FLAG_REPLICATED);
  676. is_fixup_operation= operation_is_flag_set(op,OP_FLAG_REPL_FIXUP);
  677. /* For replicated operations, apply URP algorithm */
  678. if(!is_fixup_operation)
  679. {
  680. slapi_pblock_set(pb, SLAPI_TXN_RUV_MODS_FN,
  681. (void *)replica_ruv_smods_for_op);
  682. if (is_replicated_operation) {
  683. rc = urp_modify_operation(pb);
  684. }
  685. }
  686. /* Clean up old state information */
  687. purge_entry_state_information(pb);
  688. return rc;
  689. }
  690. int
  691. multimaster_bepreop_modrdn (Slapi_PBlock *pb)
  692. {
  693. int rc= 0;
  694. Slapi_Operation *op;
  695. int is_replicated_operation;
  696. int is_fixup_operation;
  697. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  698. is_replicated_operation= operation_is_flag_set(op,OP_FLAG_REPLICATED);
  699. is_fixup_operation= operation_is_flag_set(op,OP_FLAG_REPL_FIXUP);
  700. /* For replicated operations, apply URP algorithm */
  701. if(!is_fixup_operation)
  702. {
  703. slapi_pblock_set(pb, SLAPI_TXN_RUV_MODS_FN,
  704. (void *)replica_ruv_smods_for_op);
  705. if (is_replicated_operation) {
  706. rc = urp_modrdn_operation(pb);
  707. }
  708. }
  709. /* Clean up old state information */
  710. purge_entry_state_information(pb);
  711. return rc;
  712. }
  713. int
  714. multimaster_bepostop_modrdn (Slapi_PBlock *pb)
  715. {
  716. Slapi_Operation *op;
  717. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  718. if ( ! operation_is_flag_set (op, OP_FLAG_REPL_FIXUP) )
  719. {
  720. urp_post_modrdn_operation (pb);
  721. }
  722. return 0;
  723. }
  724. int
  725. multimaster_bepostop_delete (Slapi_PBlock *pb)
  726. {
  727. Slapi_Operation *op;
  728. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  729. if ( ! operation_is_flag_set (op, OP_FLAG_REPL_FIXUP) )
  730. {
  731. urp_post_delete_operation (pb);
  732. }
  733. return 0;
  734. }
  735. /* postop - write to changelog */
  736. int
  737. multimaster_postop_bind (Slapi_PBlock *pb)
  738. {
  739. return 0;
  740. }
  741. int
  742. multimaster_postop_add (Slapi_PBlock *pb)
  743. {
  744. return process_postop(pb);
  745. }
  746. int
  747. multimaster_postop_delete (Slapi_PBlock *pb)
  748. {
  749. return process_postop(pb);
  750. }
  751. int
  752. multimaster_postop_modify (Slapi_PBlock *pb)
  753. {
  754. return process_postop(pb);
  755. }
  756. int
  757. multimaster_postop_modrdn (Slapi_PBlock *pb)
  758. {
  759. return process_postop(pb);
  760. }
  761. int
  762. multimaster_betxnpostop_delete (Slapi_PBlock *pb)
  763. {
  764. return write_changelog_and_ruv(pb);
  765. }
  766. int
  767. multimaster_betxnpostop_modrdn (Slapi_PBlock *pb)
  768. {
  769. return write_changelog_and_ruv(pb);
  770. }
  771. int
  772. multimaster_betxnpostop_add (Slapi_PBlock *pb)
  773. {
  774. return write_changelog_and_ruv(pb);
  775. }
  776. int
  777. multimaster_betxnpostop_modify (Slapi_PBlock *pb)
  778. {
  779. return write_changelog_and_ruv(pb);
  780. }
  781. /* Helper functions */
  782. /*
  783. * This function makes a copy of the operation parameters
  784. * and stashes them in the consumer operation extension.
  785. * This is where the 5.0 Change Log will get the operation
  786. * details from.
  787. */
  788. static void
  789. copy_operation_parameters(Slapi_PBlock *pb)
  790. {
  791. Slapi_Operation *op = NULL;
  792. struct slapi_operation_parameters *op_params;
  793. supplier_operation_extension *opext;
  794. Object *repl_obj;
  795. Replica *replica;
  796. repl_obj = replica_get_replica_for_op (pb);
  797. /* we are only interested in the updates to replicas */
  798. if (repl_obj)
  799. {
  800. /* we only save the original operation parameters for replicated operations
  801. since client operations don't go through urp engine and pblock data can be logged */
  802. slapi_pblock_get( pb, SLAPI_OPERATION, &op );
  803. PR_ASSERT (op);
  804. replica = (Replica*)object_get_data (repl_obj);
  805. PR_ASSERT (replica);
  806. opext = (supplier_operation_extension*) repl_sup_get_ext (REPL_SUP_EXT_OP, op);
  807. if (operation_is_flag_set(op,OP_FLAG_REPLICATED) &&
  808. !operation_is_flag_set(op, OP_FLAG_REPL_FIXUP))
  809. {
  810. slapi_pblock_get (pb, SLAPI_OPERATION_PARAMETERS, &op_params);
  811. opext->operation_parameters= operation_parameters_dup(op_params);
  812. }
  813. /* this condition is needed to avoid re-entering backend serial lock
  814. when ruv state is updated */
  815. if (!operation_is_flag_set(op, OP_FLAG_REPL_FIXUP))
  816. {
  817. /* save replica generation in case it changes */
  818. opext->repl_gen = replica_get_generation (replica);
  819. }
  820. object_release (repl_obj);
  821. }
  822. }
  823. /*
  824. * Helper function: update the RUV so that it reflects the
  825. * locally-processed update. This is called for both replicated
  826. * and non-replicated operations.
  827. */
  828. static void
  829. update_ruv_component(Replica *replica, CSN *opcsn, Slapi_PBlock *pb)
  830. {
  831. PRBool legacy;
  832. char *purl;
  833. if (!replica || !opcsn)
  834. return;
  835. /* Replica configured, so update its ruv */
  836. legacy = replica_is_legacy_consumer (replica);
  837. if (legacy)
  838. purl = replica_get_legacy_purl (replica);
  839. else
  840. purl = (char*)replica_get_purl_for_op (replica, pb, opcsn);
  841. replica_update_ruv(replica, opcsn, purl);
  842. if (legacy)
  843. {
  844. slapi_ch_free ((void**)&purl);
  845. }
  846. }
  847. /*
  848. * Write the changelog. Note: it is an error to call write_changelog_and_ruv() for fixup
  849. * operations. The caller should avoid calling this function if the operation is
  850. * a fixup op.
  851. * Also update the ruv - we need to do this while we have the replica lock acquired
  852. * so that the csn is written to the changelog and the ruv is updated with the csn
  853. * in one atomic operation - if there is no changelog, we still need to update
  854. * the ruv (e.g. for consumers)
  855. */
  856. static int
  857. write_changelog_and_ruv (Slapi_PBlock *pb)
  858. {
  859. Slapi_Operation *op = NULL;
  860. int rc;
  861. slapi_operation_parameters *op_params = NULL;
  862. Object *repl_obj;
  863. int return_value = 0;
  864. Replica *r;
  865. Slapi_Backend *be;
  866. int is_replicated_operation = 0;
  867. /* we just let fixup operations through */
  868. slapi_pblock_get( pb, SLAPI_OPERATION, &op );
  869. if ((operation_is_flag_set(op, OP_FLAG_REPL_FIXUP)) ||
  870. (operation_is_flag_set(op, OP_FLAG_TOMBSTONE_ENTRY)))
  871. {
  872. return 0;
  873. }
  874. /* ignore operations intended for chaining backends - they will be
  875. replicated back to us or should be ignored anyway
  876. replicated operations should be processed normally, as they should
  877. be going to a local backend */
  878. is_replicated_operation= operation_is_flag_set(op,OP_FLAG_REPLICATED);
  879. slapi_pblock_get(pb, SLAPI_BACKEND, &be);
  880. if (!is_replicated_operation &&
  881. slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA))
  882. {
  883. return 0;
  884. }
  885. /* we only log changes for operations applied to a replica */
  886. repl_obj = replica_get_replica_for_op (pb);
  887. if (repl_obj == NULL)
  888. return 0;
  889. r = (Replica*)object_get_data (repl_obj);
  890. PR_ASSERT (r);
  891. /*
  892. * In case we had to run cleanruv, we don't want to continue to write
  893. * updates to the changelog/database ruv from that replica(rid).
  894. */
  895. if( is_cleaned_rid(replica_get_rid(r))){
  896. /* this RID has been cleaned, just goto done */
  897. goto done;
  898. }
  899. if (replica_is_flag_set (r, REPLICA_LOG_CHANGES) &&
  900. (cl5GetState () == CL5_STATE_OPEN))
  901. {
  902. supplier_operation_extension *opext = NULL;
  903. const char *repl_name;
  904. char *repl_gen;
  905. opext = (supplier_operation_extension*) repl_sup_get_ext (REPL_SUP_EXT_OP, op);
  906. PR_ASSERT (opext);
  907. /* get replica generation and replica name to pass to the write function */
  908. repl_name = replica_get_name (r);
  909. repl_gen = opext->repl_gen;
  910. PR_ASSERT (repl_name && repl_gen);
  911. /* for replicated operations, we log the original, non-urp data which is
  912. saved in the operation extension */
  913. if (operation_is_flag_set(op,OP_FLAG_REPLICATED))
  914. {
  915. PR_ASSERT (opext->operation_parameters);
  916. op_params = opext->operation_parameters;
  917. }
  918. else /* since client operations don't go through urp, we log the operation data in pblock */
  919. {
  920. Slapi_Entry *e = NULL;
  921. const char *uniqueid = NULL;
  922. slapi_pblock_get (pb, SLAPI_OPERATION_PARAMETERS, &op_params);
  923. PR_ASSERT (op_params);
  924. /* need to set uniqueid operation parameter */
  925. /* we try to use the appropriate entry (pre op or post op)
  926. depending on the type of operation (add, delete, modify)
  927. However, in some cases, the backend operation may fail in
  928. a non fatal way (e.g. attempt to remove an attribute value
  929. that does not exist) but we still need to log the change.
  930. So, the POST_OP entry may not have been set in the FE modify
  931. code. In that case, we use the PRE_OP entry.
  932. */
  933. slapi_pblock_get (pb, SLAPI_ENTRY_POST_OP, &e);
  934. if ((e == NULL) ||
  935. (op_params->operation_type == SLAPI_OPERATION_DELETE))
  936. {
  937. slapi_pblock_get (pb, SLAPI_ENTRY_PRE_OP, &e);
  938. }
  939. PR_ASSERT (e);
  940. uniqueid = slapi_entry_get_uniqueid (e);
  941. PR_ASSERT (uniqueid);
  942. op_params->target_address.uniqueid = slapi_ch_strdup (uniqueid);
  943. }
  944. /* we might have stripped all the mods - in that case we do not
  945. log the operation */
  946. if (op_params->operation_type != SLAPI_OPERATION_MODIFY ||
  947. op_params->p.p_modify.modify_mods != NULL)
  948. {
  949. void *txn = NULL;
  950. if (cl5_is_diskfull() && !cl5_diskspace_is_available())
  951. {
  952. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  953. "write_changelog_and_ruv: Skipped due to DISKFULL\n");
  954. return 0;
  955. }
  956. slapi_pblock_get(pb, SLAPI_TXN, &txn);
  957. rc = cl5WriteOperationTxn(repl_name, repl_gen, op_params,
  958. !operation_is_flag_set(op, OP_FLAG_REPLICATED), txn);
  959. if (rc != CL5_SUCCESS)
  960. {
  961. char csn_str[CSN_STRSIZE];
  962. /* ONREPL - log error */
  963. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  964. "write_changelog_and_ruv: can't add a change for "
  965. "%s (uniqid: %s, optype: %lu) to changelog csn %s\n",
  966. REPL_GET_DN(&op_params->target_address),
  967. op_params->target_address.uniqueid,
  968. op_params->operation_type,
  969. csn_as_string(op_params->csn, PR_FALSE, csn_str));
  970. return_value = 1;
  971. }
  972. }
  973. if (!operation_is_flag_set(op,OP_FLAG_REPLICATED))
  974. {
  975. slapi_ch_free((void**)&op_params->target_address.uniqueid);
  976. }
  977. }
  978. /*
  979. This was moved because we need to write the changelog and update
  980. the ruv in one atomic operation - I have seen cases where the inc
  981. protocol thread interrupts this thread between the time the changelog
  982. is written and the ruv is updated - this causes confusion in several
  983. places, especially in _cl5SkipReplayEntry since it cannot find the csn it
  984. just read from the changelog in either the supplier or consumer ruv
  985. */
  986. if (0 == return_value) {
  987. CSN *opcsn;
  988. slapi_pblock_get( pb, SLAPI_OPERATION, &op );
  989. opcsn = operation_get_csn(op);
  990. update_ruv_component(r, opcsn, pb);
  991. }
  992. done:
  993. object_release (repl_obj);
  994. return return_value;
  995. }
  996. /*
  997. * Postop processing - write the changelog if the operation resulted in
  998. * an LDAP_SUCCESS result code, update the RUV, and notify the replication
  999. * agreements about the change.
  1000. * If the result code is not LDAP_SUCCESS, then cancel the operation CSN.
  1001. */
  1002. static int
  1003. process_postop (Slapi_PBlock *pb)
  1004. {
  1005. int rc = LDAP_SUCCESS;
  1006. Slapi_Operation *op;
  1007. Slapi_Backend *be;
  1008. int is_replicated_operation = 0;
  1009. CSN *opcsn = NULL;
  1010. char sessionid[REPL_SESSION_ID_SIZE];
  1011. /* we just let fixup operations through */
  1012. slapi_pblock_get( pb, SLAPI_OPERATION, &op );
  1013. if ((operation_is_flag_set(op, OP_FLAG_REPL_FIXUP)) ||
  1014. (operation_is_flag_set(op, OP_FLAG_TOMBSTONE_ENTRY)))
  1015. {
  1016. return 0;
  1017. }
  1018. /* ignore operations intended for chaining backends - they will be
  1019. replicated back to us or should be ignored anyway
  1020. replicated operations should be processed normally, as they should
  1021. be going to a local backend */
  1022. is_replicated_operation= operation_is_flag_set(op,OP_FLAG_REPLICATED);
  1023. slapi_pblock_get(pb, SLAPI_BACKEND, &be);
  1024. if (!is_replicated_operation &&
  1025. slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA))
  1026. {
  1027. return 0;
  1028. }
  1029. get_repl_session_id (pb, sessionid, &opcsn);
  1030. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &rc);
  1031. if (rc == LDAP_SUCCESS)
  1032. {
  1033. agmtlist_notify_all(pb);
  1034. }
  1035. else if (opcsn)
  1036. {
  1037. rc = cancel_opcsn (pb);
  1038. /* Don't try to get session id since conn is always null */
  1039. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1040. "%s process postop: canceling operation csn\n", sessionid);
  1041. }
  1042. /* the target unique id is set in the modify_preop above, so
  1043. we need to free it */
  1044. /* The following bunch of frees code does not belong to this place
  1045. * but rather to operation_free who should be responsible for calling
  1046. * operation_parameters_free and it doesn't. I guess this is like this
  1047. * because several crashes happened in the past regarding some opparams
  1048. * that were getting individually freed before they should. Whatever
  1049. * the case, this is not the place and we should make sure that this
  1050. * code gets removed for 5.next and substituted by the strategy (operation_free).
  1051. * For 5.0, such change is too risky, so this will be done here */
  1052. if (is_replicated_operation)
  1053. {
  1054. slapi_operation_parameters *op_params = NULL;
  1055. int optype = 0;
  1056. /* target uid and csn are set for all repl operations. Free them */
  1057. char *target_uuid = NULL;
  1058. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &optype);
  1059. slapi_pblock_get(pb, SLAPI_TARGET_UNIQUEID, &target_uuid);
  1060. slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, NULL);
  1061. slapi_ch_free((void**)&target_uuid);
  1062. if (optype == SLAPI_OPERATION_ADD) {
  1063. slapi_pblock_get( pb, SLAPI_OPERATION_PARAMETERS, &op_params );
  1064. slapi_ch_free((void **) &op_params->p.p_add.parentuniqueid);
  1065. }
  1066. if (optype == SLAPI_OPERATION_MODRDN) {
  1067. slapi_pblock_get( pb, SLAPI_OPERATION_PARAMETERS, &op_params );
  1068. slapi_ch_free((void **) &op_params->p.p_modrdn.modrdn_newsuperior_address.uniqueid);
  1069. }
  1070. }
  1071. if (NULL == opcsn)
  1072. opcsn = operation_get_csn(op);
  1073. if (opcsn)
  1074. csn_free(&opcsn);
  1075. return rc;
  1076. }
  1077. /*
  1078. * Cancel an operation CSN. This removes it from any CSN pending lists.
  1079. * This function is called when a previously-generated CSN will not
  1080. * be needed, e.g. if the update operation produced an error.
  1081. */
  1082. static int
  1083. cancel_opcsn (Slapi_PBlock *pb)
  1084. {
  1085. Object *repl_obj;
  1086. Slapi_Operation *op = NULL;
  1087. PR_ASSERT (pb);
  1088. repl_obj = replica_get_replica_for_op (pb);
  1089. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  1090. PR_ASSERT (op);
  1091. if (repl_obj)
  1092. {
  1093. Replica *r;
  1094. Object *gen_obj;
  1095. CSNGen *gen;
  1096. CSN *opcsn;
  1097. r = (Replica*)object_get_data (repl_obj);
  1098. PR_ASSERT (r);
  1099. opcsn = operation_get_csn(op);
  1100. if (!operation_is_flag_set(op,OP_FLAG_REPLICATED))
  1101. {
  1102. /* get csn generator for the replica */
  1103. gen_obj = replica_get_csngen (r);
  1104. PR_ASSERT (gen_obj);
  1105. gen = (CSNGen*)object_get_data (gen_obj);
  1106. if (NULL != opcsn)
  1107. {
  1108. csngen_abort_csn (gen, operation_get_csn(op));
  1109. }
  1110. object_release (gen_obj);
  1111. }
  1112. else if (!operation_is_flag_set(op,OP_FLAG_REPL_FIXUP))
  1113. {
  1114. Object *ruv_obj;
  1115. ruv_obj = replica_get_ruv (r);
  1116. PR_ASSERT (ruv_obj);
  1117. ruv_cancel_csn_inprogress ((RUV*)object_get_data (ruv_obj), opcsn);
  1118. object_release (ruv_obj);
  1119. }
  1120. object_release (repl_obj);
  1121. }
  1122. return 0;
  1123. }
  1124. /*
  1125. * Return non-zero if the target entry DN is the DN of the RUV tombstone
  1126. * entry.
  1127. * The entry has rdn of nsuniqueid = ffffffff-ffffffff-ffffffff-ffffffff
  1128. */
  1129. static int
  1130. ruv_tombstone_op (Slapi_PBlock *pb)
  1131. {
  1132. char *uniqueid;
  1133. int rc;
  1134. slapi_pblock_get (pb, SLAPI_TARGET_UNIQUEID, &uniqueid);
  1135. rc = uniqueid && strcasecmp (uniqueid, RUV_STORAGE_ENTRY_UNIQUEID) == 0;
  1136. return rc;
  1137. }
  1138. /* we don't want to process replicated operations with csn smaller
  1139. than the corresponding csn in the consumer's ruv */
  1140. static PRBool
  1141. process_operation (Slapi_PBlock *pb, const CSN *csn)
  1142. {
  1143. Object *r_obj;
  1144. Replica *r;
  1145. Object *ruv_obj;
  1146. RUV *ruv;
  1147. int rc;
  1148. r_obj = replica_get_replica_for_op(pb);
  1149. if (r_obj == NULL)
  1150. {
  1151. char sessionid[REPL_SESSION_ID_SIZE];
  1152. get_repl_session_id (pb, sessionid, NULL);
  1153. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s process_operation: "
  1154. "can't locate replica for the replicated operation\n",
  1155. sessionid );
  1156. return PR_FALSE;
  1157. }
  1158. r = (Replica*)object_get_data (r_obj);
  1159. PR_ASSERT (r);
  1160. ruv_obj = replica_get_ruv (r);
  1161. PR_ASSERT (ruv_obj);
  1162. ruv = (RUV*)object_get_data (ruv_obj);
  1163. PR_ASSERT (ruv);
  1164. rc = ruv_add_csn_inprogress (ruv, csn);
  1165. object_release (ruv_obj);
  1166. object_release (r_obj);
  1167. return (rc == RUV_SUCCESS);
  1168. }
  1169. static PRBool
  1170. is_mmr_replica (Slapi_PBlock *pb)
  1171. {
  1172. Object *r_obj;
  1173. Replica *r;
  1174. PRBool mmr;
  1175. r_obj = replica_get_replica_for_op(pb);
  1176. if (r_obj == NULL)
  1177. {
  1178. return PR_FALSE;
  1179. }
  1180. r = (Replica*)object_get_data (r_obj);
  1181. PR_ASSERT (r);
  1182. mmr = !replica_is_legacy_consumer (r);
  1183. object_release (r_obj);
  1184. return mmr;
  1185. }
  1186. static const char *replica_get_purl_for_op (const Replica *r, Slapi_PBlock *pb, const CSN *opcsn)
  1187. {
  1188. int is_replicated_op;
  1189. const char *purl = NULL;
  1190. slapi_pblock_get(pb, SLAPI_IS_MMR_REPLICATED_OPERATION, &is_replicated_op);
  1191. if (!is_replicated_op)
  1192. {
  1193. purl = multimaster_get_local_purl();
  1194. }
  1195. else
  1196. {
  1197. /* Get the appropriate partial URL from the supplier RUV */
  1198. Slapi_Connection *conn;
  1199. consumer_connection_extension *connext;
  1200. slapi_pblock_get(pb, SLAPI_CONNECTION, &conn);
  1201. connext = (consumer_connection_extension *)repl_con_get_ext(
  1202. REPL_CON_EXT_CONN, conn);
  1203. if (NULL == connext || NULL == connext->supplier_ruv)
  1204. {
  1205. char sessionid[REPL_SESSION_ID_SIZE];
  1206. get_repl_session_id (pb, sessionid, NULL);
  1207. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s replica_get_purl_for_op: "
  1208. "cannot obtain consumer connection extension or supplier_ruv.\n",
  1209. sessionid);
  1210. }
  1211. else
  1212. {
  1213. purl = ruv_get_purl_for_replica(connext->supplier_ruv,
  1214. csn_get_replicaid(opcsn));
  1215. }
  1216. }
  1217. return purl;
  1218. }
  1219. #ifdef NOTUSED
  1220. /* ONREPL at the moment, I decided not to trim copiedFrom and copyingFrom
  1221. attributes when sending operation to replicas. This is because, each
  1222. operation results in a state information stored in the database and
  1223. if we don't replay all operations we will endup with state inconsistency.
  1224. Keeping the function just in case
  1225. */
  1226. static void strip_legacy_info (slapi_operation_parameters *op_params)
  1227. {
  1228. switch (op_params->operation_type)
  1229. {
  1230. case SLAPI_OPERATION_ADD:
  1231. slapi_entry_delete_values_sv(op_params->p.p_add.target_entry,
  1232. type_copiedFrom, NULL);
  1233. slapi_entry_delete_values_sv(op_params->p.p_add.target_entry,
  1234. type_copyingFrom, NULL);
  1235. break;
  1236. case SLAPI_OPERATION_MODIFY:
  1237. {
  1238. Slapi_Mods smods;
  1239. LDAPMod *mod;
  1240. slapi_mods_init_byref(&smods, op_params->p.p_modify.modify_mods);
  1241. mod = slapi_mods_get_first_mod(&smods);
  1242. while (mod)
  1243. {
  1244. /* modify just to update copiedFrom or copyingFrom attribute
  1245. does not contain modifiersname or modifytime - so we don't
  1246. have to strip them */
  1247. if (strcasecmp (mod->mod_type, type_copiedFrom) == 0 ||
  1248. strcasecmp (mod->mod_type, type_copyingFrom) == 0)
  1249. slapi_mods_remove(&smods);
  1250. mod = slapi_mods_get_next_mod(&smods);
  1251. }
  1252. op_params->p.p_modify.modify_mods = slapi_mods_get_ldapmods_passout (&smods);
  1253. slapi_mods_done (&smods);
  1254. break;
  1255. }
  1256. default: break;
  1257. }
  1258. }
  1259. #endif
  1260. /* this function is called when state of a backend changes */
  1261. void
  1262. multimaster_be_state_change (void *handle, char *be_name, int old_be_state, int new_be_state)
  1263. {
  1264. Object *r_obj;
  1265. Replica *r;
  1266. /* check if we have replica associated with the backend */
  1267. r_obj = replica_get_for_backend (be_name);
  1268. if (r_obj == NULL)
  1269. return;
  1270. r = (Replica*)object_get_data (r_obj);
  1271. PR_ASSERT (r);
  1272. if (new_be_state == SLAPI_BE_STATE_ON)
  1273. {
  1274. /* backend came back online - restart replication */
  1275. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "multimaster_be_state_change: "
  1276. "replica %s is coming online; enabling replication\n",
  1277. slapi_sdn_get_ndn (replica_get_root (r)));
  1278. replica_enable_replication (r);
  1279. }
  1280. else if (new_be_state == SLAPI_BE_STATE_OFFLINE)
  1281. {
  1282. /* backend is about to be taken down - disable replication */
  1283. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "multimaster_be_state_change: "
  1284. "replica %s is going offline; disabling replication\n",
  1285. slapi_sdn_get_ndn (replica_get_root (r)));
  1286. replica_disable_replication (r, r_obj);
  1287. }
  1288. else if (new_be_state == SLAPI_BE_STATE_DELETE)
  1289. {
  1290. /* backend is about to be removed - disable replication */
  1291. if (old_be_state == SLAPI_BE_STATE_ON)
  1292. {
  1293. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "multimaster_be_state_change: "
  1294. "replica %s is about to be deleted; disabling replication\n",
  1295. slapi_sdn_get_ndn (replica_get_root (r)));
  1296. replica_disable_replication (r, r_obj);
  1297. }
  1298. }
  1299. object_release (r_obj);
  1300. }