repl5_plugins.c 52 KB

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