repl5_plugins.c 52 KB

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