repl5_plugins.c 52 KB

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