repl5_replica_config.c 125 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* repl5_replica_config.c - replica configuration over ldap */
  13. #include <ctype.h> /* for isdigit() */
  14. #include "repl.h" /* ONREPL - this is bad */
  15. #include "repl5.h"
  16. #include "cl5_api.h"
  17. #include "cl5.h"
  18. #include "slap.h"
  19. /* CONFIG_BASE: no need to optimize */
  20. #define CONFIG_BASE "cn=mapping tree,cn=config"
  21. #define CONFIG_FILTER "(objectclass=nsDS5Replica)"
  22. #define TASK_ATTR "nsds5Task"
  23. #define CL2LDIF_TASK "CL2LDIF"
  24. #define LDIF2CL_TASK "LDIF2CL"
  25. #define CLEANRUV "CLEANRUV"
  26. #define CLEANRUVLEN 8
  27. #define CLEANALLRUV "CLEANALLRUV"
  28. #define CLEANALLRUVLEN 11
  29. #define REPLICA_RDN "cn=replica"
  30. #define CLEANALLRUV_ID "CleanAllRUV Task"
  31. #define ABORT_CLEANALLRUV_ID "Abort CleanAllRUV Task"
  32. int slapi_log_urp = SLAPI_LOG_REPL;
  33. static ReplicaId cleaned_rids[CLEANRIDSIZ + 1] = {0};
  34. static ReplicaId pre_cleaned_rids[CLEANRIDSIZ + 1] = {0};
  35. static ReplicaId aborted_rids[CLEANRIDSIZ + 1] = {0};
  36. static Slapi_RWLock *rid_lock = NULL;
  37. static Slapi_RWLock *abort_rid_lock = NULL;
  38. static PRLock *notify_lock = NULL;
  39. static PRCondVar *notify_cvar = NULL;
  40. /* Forward Declartions */
  41. static int replica_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  42. static int replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  43. static int replica_config_post_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  44. static int replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  45. static int replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  46. static int replica_cleanall_ruv_task(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  47. static int replica_config_change_type_and_id (Replica *r, const char *new_type, const char *new_id, char *returntext, int apply_mods);
  48. static int replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext, int apply_mods);
  49. static int replica_config_change_updatedngroup (Replica *r, const LDAPMod *mod, char *returntext, int apply_mods);
  50. static int replica_config_change_flags (Replica *r, const char *new_flags, char *returntext, int apply_mods);
  51. static int replica_execute_task (Object *r, const char *task_name, char *returntext, int apply_mods);
  52. static int replica_execute_cl2ldif_task (Object *r, char *returntext);
  53. static int replica_execute_ldif2cl_task (Object *r, char *returntext);
  54. static int replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext);
  55. static int replica_execute_cleanall_ruv_task (Object *r, ReplicaId rid, Slapi_Task *task, const char *force_cleaning, char *returntext);
  56. static void replica_cleanallruv_thread(void *arg);
  57. static void replica_send_cleanruv_task(Repl_Agmt *agmt, cleanruv_data *clean_data);
  58. static int check_agmts_are_alive(Replica *replica, ReplicaId rid, Slapi_Task *task);
  59. static int check_agmts_are_caught_up(cleanruv_data *data, char *maxcsn);
  60. static int replica_cleanallruv_send_extop(Repl_Agmt *ra, cleanruv_data *data, int check_result);
  61. static int replica_cleanallruv_send_abort_extop(Repl_Agmt *ra, Slapi_Task *task, struct berval *payload);
  62. static int replica_cleanallruv_check_maxcsn(Repl_Agmt *agmt, char *basedn, char *rid_text, char *maxcsn, Slapi_Task *task);
  63. static int replica_cleanallruv_replica_alive(Repl_Agmt *agmt);
  64. static int replica_cleanallruv_check_ruv(char *repl_root, Repl_Agmt *ra, char *rid_text, Slapi_Task *task, char *force);
  65. static int get_cleanruv_task_count();
  66. static int get_abort_cleanruv_task_count();
  67. static int replica_cleanup_task (Object *r, const char *task_name, char *returntext, int apply_mods);
  68. static int replica_task_done(Replica *replica);
  69. static void delete_cleaned_rid_config(cleanruv_data *data);
  70. static int replica_cleanallruv_is_finished(Repl_Agmt *agmt, char *filter, Slapi_Task *task);
  71. static void check_replicas_are_done_cleaning(cleanruv_data *data);
  72. static void check_replicas_are_done_aborting(cleanruv_data *data );
  73. static CSN* replica_cleanallruv_find_maxcsn(Replica *replica, ReplicaId rid, char *basedn);
  74. static int replica_cleanallruv_get_replica_maxcsn(Repl_Agmt *agmt, char *rid_text, char *basedn, CSN **csn);
  75. static void preset_cleaned_rid(ReplicaId rid);
  76. static multimaster_mtnode_extension * _replica_config_get_mtnode_ext (const Slapi_Entry *e);
  77. static void replica_cleanall_ruv_destructor(Slapi_Task *task);
  78. static void replica_cleanall_ruv_abort_destructor(Slapi_Task *task);
  79. static void remove_keep_alive_entry(Slapi_Task *task, ReplicaId rid, const char *repl_root);
  80. /*
  81. * Note: internal add/modify/delete operations should not be run while
  82. * s_configLock is held. E.g., slapi_modify_internal_pb via replica_task_done
  83. * in replica_config_post_modify.
  84. */
  85. static PRLock *s_configLock;
  86. static int
  87. dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  88. {
  89. *returncode = LDAP_UNWILLING_TO_PERFORM;
  90. return SLAPI_DSE_CALLBACK_ERROR;
  91. }
  92. int
  93. replica_config_init()
  94. {
  95. s_configLock = PR_NewLock ();
  96. if (s_configLock == NULL)
  97. {
  98. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  99. "failed to create configuration lock; NSPR error - %d\n",
  100. PR_GetError ());
  101. return -1;
  102. }
  103. rid_lock = slapi_new_rwlock();
  104. if(rid_lock == NULL)
  105. {
  106. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  107. "failed to create rid_lock; NSPR error - %d\n", PR_GetError ());
  108. return -1;
  109. }
  110. abort_rid_lock = slapi_new_rwlock();
  111. if(abort_rid_lock == NULL)
  112. {
  113. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  114. "failed to create abort_rid_lock; NSPR error - %d\n", PR_GetError ());
  115. return -1;
  116. }
  117. if ( ( notify_lock = PR_NewLock()) == NULL ) {
  118. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  119. "failed to create notify lock; NSPR error - %d\n", PR_GetError ());
  120. return -1;
  121. }
  122. if ( ( notify_cvar = PR_NewCondVar( notify_lock )) == NULL ) {
  123. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  124. "failed to create notify cond var; NSPR error - %d\n", PR_GetError ());
  125. return -1;
  126. }
  127. /* config DSE must be initialized before we get here */
  128. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  129. CONFIG_FILTER, replica_config_add, NULL);
  130. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  131. CONFIG_FILTER, replica_config_modify,NULL);
  132. slapi_config_register_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  133. CONFIG_FILTER, dont_allow_that, NULL);
  134. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  135. CONFIG_FILTER, replica_config_delete,NULL);
  136. slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  137. CONFIG_FILTER, replica_config_search,NULL);
  138. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_POSTOP,
  139. CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  140. CONFIG_FILTER, replica_config_post_modify,
  141. NULL);
  142. /* register the CLEANALLRUV & ABORT task */
  143. slapi_task_register_handler("cleanallruv", replica_cleanall_ruv_task);
  144. slapi_task_register_handler("abort cleanallruv", replica_cleanall_ruv_abort);
  145. return 0;
  146. }
  147. void
  148. replica_config_destroy ()
  149. {
  150. if (s_configLock)
  151. {
  152. PR_DestroyLock (s_configLock);
  153. s_configLock = NULL;
  154. }
  155. /* config DSE must be initialized before we get here */
  156. slapi_config_remove_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  157. CONFIG_FILTER, replica_config_add);
  158. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  159. CONFIG_FILTER, replica_config_modify);
  160. slapi_config_remove_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  161. CONFIG_FILTER, dont_allow_that);
  162. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  163. CONFIG_FILTER, replica_config_delete);
  164. slapi_config_remove_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  165. CONFIG_FILTER, replica_config_search);
  166. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  167. CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  168. CONFIG_FILTER, replica_config_post_modify);
  169. }
  170. static int
  171. replica_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  172. int *returncode, char *errorbuf, void *arg)
  173. {
  174. Replica *r = NULL;
  175. multimaster_mtnode_extension *mtnode_ext;
  176. char *replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  177. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  178. char *errortext = errorbuf ? errorbuf : buf;
  179. if (errorbuf)
  180. {
  181. errorbuf[0] = '\0';
  182. }
  183. *returncode = LDAP_SUCCESS;
  184. PR_Lock (s_configLock);
  185. /* add the dn to the dn hash so we can tell this replica is being configured */
  186. replica_add_by_dn(replica_root);
  187. mtnode_ext = _replica_config_get_mtnode_ext (e);
  188. PR_ASSERT (mtnode_ext);
  189. if (mtnode_ext->replica)
  190. {
  191. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "replica already configured for %s", replica_root);
  192. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: %s\n", errortext);
  193. *returncode = LDAP_UNWILLING_TO_PERFORM;
  194. goto done;
  195. }
  196. /* create replica object */
  197. r = replica_new_from_entry (e, errortext, PR_TRUE /* is a newly added entry */);
  198. if (r == NULL)
  199. {
  200. *returncode = LDAP_OPERATIONS_ERROR;
  201. goto done;
  202. }
  203. /* Set the mapping tree node state, and the referrals from the RUV */
  204. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  205. if (!replica_is_legacy_consumer (r))
  206. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  207. /* ONREPL if replica is added as writable we need to execute protocol that
  208. introduces new writable replica to the topology */
  209. mtnode_ext->replica = object_new (r, replica_destroy); /* Refcnt is 1 */
  210. /* add replica object to the hash */
  211. *returncode = replica_add_by_name (replica_get_name (r), mtnode_ext->replica); /* Increments object refcnt */
  212. /* delete the dn from the dn hash - done with configuration */
  213. replica_delete_by_dn(replica_root);
  214. done:
  215. PR_Unlock (s_configLock);
  216. /* slapi_ch_free accepts NULL pointer */
  217. slapi_ch_free ((void**)&replica_root);
  218. if (*returncode != LDAP_SUCCESS)
  219. {
  220. if (mtnode_ext->replica)
  221. object_release (mtnode_ext->replica);
  222. return SLAPI_DSE_CALLBACK_ERROR;
  223. }
  224. else
  225. return SLAPI_DSE_CALLBACK_OK;
  226. }
  227. static int
  228. replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  229. int *returncode, char *returntext, void *arg)
  230. {
  231. int rc= 0;
  232. LDAPMod **mods;
  233. int i, apply_mods;
  234. multimaster_mtnode_extension *mtnode_ext;
  235. Replica *r = NULL;
  236. char *replica_root = NULL;
  237. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  238. char *errortext = returntext ? returntext : buf;
  239. char *config_attr, *config_attr_value;
  240. Slapi_Operation *op;
  241. void *identity;
  242. if (returntext)
  243. {
  244. returntext[0] = '\0';
  245. }
  246. *returncode = LDAP_SUCCESS;
  247. /* just let internal operations originated from replication plugin to go through */
  248. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  249. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  250. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  251. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  252. {
  253. *returncode = LDAP_SUCCESS;
  254. return SLAPI_DSE_CALLBACK_OK;
  255. }
  256. replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  257. PR_Lock (s_configLock);
  258. mtnode_ext = _replica_config_get_mtnode_ext (e);
  259. PR_ASSERT (mtnode_ext);
  260. if (mtnode_ext->replica)
  261. object_acquire (mtnode_ext->replica);
  262. if (mtnode_ext->replica == NULL)
  263. {
  264. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "replica does not exist for %s", replica_root);
  265. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  266. errortext);
  267. *returncode = LDAP_OPERATIONS_ERROR;
  268. goto done;
  269. }
  270. r = object_get_data (mtnode_ext->replica);
  271. PR_ASSERT (r);
  272. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  273. for (apply_mods = 0; apply_mods <= 1; apply_mods++)
  274. {
  275. /* we only allow the replica ID and type to be modified together e.g.
  276. if converting a read only replica to a master or vice versa -
  277. we will need to change both the replica ID and the type at the same
  278. time - we must disallow changing the replica ID if the type is not
  279. being changed and vice versa
  280. */
  281. char *new_repl_id = NULL;
  282. char *new_repl_type = NULL;
  283. if (*returncode != LDAP_SUCCESS)
  284. break;
  285. for (i = 0; mods && (mods[i] && (LDAP_SUCCESS == rc)); i++)
  286. {
  287. if (*returncode != LDAP_SUCCESS)
  288. break;
  289. config_attr = (char *) mods[i]->mod_type;
  290. PR_ASSERT (config_attr);
  291. /* disallow modifications or removal of replica root,
  292. replica name and replica state attributes */
  293. if (strcasecmp (config_attr, attr_replicaRoot) == 0 ||
  294. strcasecmp (config_attr, attr_replicaName) == 0 ||
  295. strcasecmp (config_attr, attr_state) == 0)
  296. {
  297. *returncode = LDAP_UNWILLING_TO_PERFORM;
  298. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "modification of %s attribute is not allowed",
  299. config_attr);
  300. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  301. errortext);
  302. }
  303. /* this is a request to delete an attribute */
  304. else if ((mods[i]->mod_op & LDAP_MOD_DELETE) || mods[i]->mod_bvalues == NULL
  305. || mods[i]->mod_bvalues[0]->bv_val == NULL)
  306. {
  307. /*
  308. * Where possible/allowed return replica config settings to their
  309. * default values.
  310. */
  311. if (strcasecmp (config_attr, attr_replicaBindDn) == 0)
  312. {
  313. *returncode = replica_config_change_updatedn (r, mods[i], errortext, apply_mods);
  314. }
  315. else if (strcasecmp (config_attr, attr_replicaBindDnGroup) == 0)
  316. {
  317. *returncode = replica_config_change_updatedngroup (r, mods[i], errortext, apply_mods);
  318. }
  319. else if (strcasecmp (config_attr, attr_replicaBindDnGroupCheckInterval) == 0)
  320. {
  321. replica_set_groupdn_checkinterval (r, -1);
  322. }
  323. else if (strcasecmp (config_attr, attr_replicaReferral) == 0)
  324. {
  325. if (apply_mods) {
  326. replica_set_referrals(r, NULL);
  327. if (!replica_is_legacy_consumer (r)) {
  328. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  329. }
  330. }
  331. }
  332. else if (strcasecmp (config_attr, type_replicaLegacyConsumer) == 0)
  333. {
  334. if (apply_mods)
  335. replica_set_legacy_consumer (r, PR_FALSE);
  336. }
  337. else if (strcasecmp (config_attr, type_replicaCleanRUV) == 0 ||
  338. strcasecmp (config_attr, type_replicaAbortCleanRUV) == 0)
  339. {
  340. /*
  341. * Nothing to do in this case, allow it, and continue.
  342. */
  343. continue;
  344. }
  345. else if (strcasecmp (config_attr, type_replicaProtocolTimeout) == 0 )
  346. {
  347. if (apply_mods)
  348. replica_set_protocol_timeout(r, DEFAULT_PROTOCOL_TIMEOUT);
  349. }
  350. else if(strcasecmp (config_attr, type_replicaBackoffMin) == 0 )
  351. {
  352. if (apply_mods)
  353. replica_set_backoff_min(r, PROTOCOL_BACKOFF_MINIMUM);
  354. }
  355. else if (strcasecmp (config_attr, type_replicaBackoffMax) == 0 )
  356. {
  357. if (apply_mods)
  358. replica_set_backoff_max(r, PROTOCOL_BACKOFF_MAXIMUM);
  359. }
  360. else if (strcasecmp (config_attr, type_replicaPrecisePurge) == 0 )
  361. {
  362. if (apply_mods)
  363. replica_set_precise_purging(r, 0);
  364. }
  365. else if (strcasecmp (config_attr, type_replicaReleaseTimeout) == 0 )
  366. {
  367. if (apply_mods)
  368. replica_set_release_timeout(r, 0);
  369. }
  370. else
  371. {
  372. *returncode = LDAP_UNWILLING_TO_PERFORM;
  373. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "deletion of %s attribute is not allowed", config_attr);
  374. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  375. }
  376. }
  377. else /* modify an attribute */
  378. {
  379. config_attr_value = (char *) mods[i]->mod_bvalues[0]->bv_val;
  380. if (NULL == config_attr_value) {
  381. *returncode = LDAP_UNWILLING_TO_PERFORM;
  382. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "attribute %s value is NULL.\n",
  383. config_attr);
  384. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  385. errortext);
  386. break;
  387. }
  388. if (strcasecmp (config_attr, attr_replicaBindDn) == 0) {
  389. *returncode = replica_config_change_updatedn (r, mods[i], errortext, apply_mods);
  390. }
  391. else if (strcasecmp (config_attr, attr_replicaBindDnGroup) == 0)
  392. {
  393. *returncode = replica_config_change_updatedngroup (r, mods[i], errortext, apply_mods);
  394. }
  395. else if (strcasecmp (config_attr, attr_replicaBindDnGroupCheckInterval) == 0)
  396. {
  397. int interval = atoi(config_attr_value);
  398. replica_set_groupdn_checkinterval (r, interval);
  399. }
  400. else if (strcasecmp (config_attr, attr_replicaType) == 0)
  401. {
  402. slapi_ch_free_string(&new_repl_type);
  403. new_repl_type = slapi_ch_strdup(config_attr_value);
  404. }
  405. else if (strcasecmp (config_attr, attr_replicaId) == 0)
  406. {
  407. slapi_ch_free_string(&new_repl_id);
  408. new_repl_id = slapi_ch_strdup(config_attr_value);
  409. }
  410. else if (strcasecmp (config_attr, attr_flags) == 0)
  411. {
  412. *returncode = replica_config_change_flags (r, config_attr_value, errortext, apply_mods);
  413. }
  414. else if (strcasecmp (config_attr, TASK_ATTR) == 0)
  415. {
  416. *returncode = replica_execute_task (mtnode_ext->replica, config_attr_value, errortext, apply_mods);
  417. }
  418. else if (strcasecmp (config_attr, attr_replicaReferral) == 0)
  419. {
  420. if (apply_mods)
  421. {
  422. Slapi_Mod smod;
  423. Slapi_ValueSet *vs= slapi_valueset_new();
  424. slapi_mod_init_byref(&smod,mods[i]);
  425. slapi_valueset_set_from_smod(vs, &smod);
  426. replica_set_referrals (r, vs);
  427. slapi_mod_done(&smod);
  428. slapi_valueset_free(vs);
  429. if (!replica_is_legacy_consumer (r)) {
  430. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  431. }
  432. }
  433. }
  434. else if (strcasecmp (config_attr, type_replicaPurgeDelay) == 0)
  435. {
  436. if (apply_mods && config_attr_value[0])
  437. {
  438. PRUint32 delay;
  439. if (isdigit (config_attr_value[0]))
  440. {
  441. delay = (unsigned int)atoi(config_attr_value);
  442. replica_set_purge_delay(r, delay);
  443. }
  444. else
  445. *returncode = LDAP_OPERATIONS_ERROR;
  446. }
  447. }
  448. else if (strcasecmp (config_attr, type_replicaTombstonePurgeInterval) == 0)
  449. {
  450. if (apply_mods && config_attr_value[0])
  451. {
  452. long interval;
  453. interval = atol (config_attr_value);
  454. replica_set_tombstone_reap_interval (r, interval);
  455. }
  456. }
  457. else if (strcasecmp (config_attr, type_replicaLegacyConsumer) == 0)
  458. {
  459. if (apply_mods && config_attr_value[0])
  460. {
  461. PRBool legacy = (strcasecmp (config_attr_value, "on") == 0) ||
  462. (strcasecmp (config_attr_value, "true") == 0) ||
  463. (strcasecmp (config_attr_value, "yes") == 0) ||
  464. (strcasecmp (config_attr_value, "1") == 0);
  465. replica_set_legacy_consumer (r, legacy);
  466. }
  467. }
  468. /* ignore modifiers attributes added by the server */
  469. else if (slapi_attr_is_last_mod(config_attr))
  470. {
  471. *returncode = LDAP_SUCCESS;
  472. }
  473. else if (strcasecmp (config_attr, type_replicaProtocolTimeout) == 0 ){
  474. if (apply_mods)
  475. {
  476. PRUint64 ptimeout = 0;
  477. ptimeout = atoll(config_attr_value);
  478. if(ptimeout <= 0){
  479. *returncode = LDAP_UNWILLING_TO_PERFORM;
  480. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  481. "attribute %s value (%s) is invalid, must be a number greater than zero.\n",
  482. config_attr, config_attr_value);
  483. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  484. break;
  485. }
  486. replica_set_protocol_timeout(r, ptimeout);
  487. }
  488. }
  489. else if(strcasecmp (config_attr, type_replicaBackoffMin) == 0 )
  490. {
  491. if (apply_mods)
  492. {
  493. PRUint64 val = atoll(config_attr_value);
  494. if(val <= 0){
  495. *returncode = LDAP_UNWILLING_TO_PERFORM;
  496. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  497. "attribute %s value (%s) is invalid, must be a number greater than zero.\n",
  498. config_attr, config_attr_value);
  499. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  500. break;
  501. }
  502. replica_set_backoff_min(r, val);
  503. }
  504. }
  505. else if (strcasecmp (config_attr, type_replicaBackoffMax) == 0 )
  506. {
  507. if (apply_mods)
  508. {
  509. PRUint64 val = atoll(config_attr_value);
  510. if(val <= 0){
  511. *returncode = LDAP_UNWILLING_TO_PERFORM;
  512. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  513. "attribute %s value (%s) is invalid, must be a number greater than zero.\n",
  514. config_attr, config_attr_value);
  515. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  516. break;
  517. }
  518. replica_set_backoff_max(r, val);
  519. }
  520. }
  521. else if (strcasecmp (config_attr, type_replicaPrecisePurge) == 0 )
  522. {
  523. if (apply_mods)
  524. {
  525. if (config_attr_value[0]) {
  526. PRUint64 on_off = 0;
  527. if (strcasecmp(config_attr_value, "on") == 0){
  528. on_off = 1;
  529. } else if (strcasecmp(config_attr_value, "off") == 0){
  530. on_off = 0;
  531. } else{
  532. /* Invalid value */
  533. *returncode = LDAP_UNWILLING_TO_PERFORM;
  534. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  535. "Invalid value for %s: %s Value should be \"on\" or \"off\"\n",
  536. type_replicaPrecisePurge, config_attr_value);
  537. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  538. "Invalid value for %s: %s Value should be \"on\" or \"off\")\n",
  539. type_replicaPrecisePurge, config_attr_value);
  540. break;
  541. }
  542. replica_set_precise_purging(r, on_off);
  543. } else {
  544. replica_set_precise_purging(r, 0);
  545. }
  546. }
  547. }
  548. else if (strcasecmp (config_attr, type_replicaReleaseTimeout) == 0 )
  549. {
  550. if (apply_mods)
  551. {
  552. long val = atol(config_attr_value);
  553. if (val < 0){
  554. *returncode = LDAP_UNWILLING_TO_PERFORM;
  555. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  556. "attribute %s value (%s) is invalid, must be a number zero or greater.\n",
  557. config_attr, config_attr_value);
  558. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  559. "replica_config_modify: %s\n", errortext);
  560. break;
  561. } else {
  562. /* Set the timeout */
  563. replica_set_release_timeout(r, val);
  564. }
  565. }
  566. }
  567. else
  568. {
  569. *returncode = LDAP_UNWILLING_TO_PERFORM;
  570. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  571. "modification of attribute %s is not allowed in replica entry", config_attr);
  572. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  573. }
  574. }
  575. }
  576. if (new_repl_id || new_repl_type)
  577. {
  578. *returncode = replica_config_change_type_and_id(r, new_repl_type, new_repl_id, errortext, apply_mods);
  579. PR_Unlock (s_configLock);
  580. replica_update_state(0, (void *)replica_get_name(r));
  581. PR_Lock (s_configLock);
  582. slapi_ch_free_string(&new_repl_id);
  583. slapi_ch_free_string(&new_repl_type);
  584. agmtlist_notify_all(pb);
  585. }
  586. }
  587. done:
  588. if (mtnode_ext->replica) {
  589. object_release (mtnode_ext->replica);
  590. }
  591. /* slapi_ch_free accepts NULL pointer */
  592. slapi_ch_free_string(&replica_root);
  593. PR_Unlock (s_configLock);
  594. if (*returncode != LDAP_SUCCESS)
  595. {
  596. return SLAPI_DSE_CALLBACK_ERROR;
  597. }
  598. else
  599. {
  600. return SLAPI_DSE_CALLBACK_OK;
  601. }
  602. }
  603. static int
  604. replica_config_post_modify(Slapi_PBlock *pb,
  605. Slapi_Entry* entryBefore,
  606. Slapi_Entry* e,
  607. int *returncode,
  608. char *returntext,
  609. void *arg)
  610. {
  611. int rc= 0;
  612. LDAPMod **mods;
  613. int i, apply_mods;
  614. multimaster_mtnode_extension *mtnode_ext;
  615. char *replica_root = NULL;
  616. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  617. char *errortext = returntext ? returntext : buf;
  618. char *config_attr, *config_attr_value;
  619. Slapi_Operation *op;
  620. void *identity;
  621. int flag_need_cleanup = 0;
  622. if (returntext)
  623. {
  624. returntext[0] = '\0';
  625. }
  626. *returncode = LDAP_SUCCESS;
  627. /* just let internal operations originated from replication plugin to go through */
  628. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  629. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  630. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  631. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  632. {
  633. *returncode = LDAP_SUCCESS;
  634. return SLAPI_DSE_CALLBACK_OK;
  635. }
  636. replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  637. PR_Lock (s_configLock);
  638. mtnode_ext = _replica_config_get_mtnode_ext (e);
  639. PR_ASSERT (mtnode_ext);
  640. if (mtnode_ext->replica)
  641. object_acquire (mtnode_ext->replica);
  642. if (mtnode_ext->replica == NULL)
  643. {
  644. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  645. "replica does not exist for %s", replica_root);
  646. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  647. "replica_config_post_modify: %s\n",
  648. errortext);
  649. *returncode = LDAP_OPERATIONS_ERROR;
  650. goto done;
  651. }
  652. PR_ASSERT (object_get_data (mtnode_ext->replica) != NULL);
  653. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  654. for (apply_mods = 0; apply_mods <= 1; apply_mods++)
  655. {
  656. /* we only allow the replica ID and type to be modified together e.g.
  657. if converting a read only replica to a master or vice versa -
  658. we will need to change both the replica ID and the type at the same
  659. time - we must disallow changing the replica ID if the type is not
  660. being changed and vice versa
  661. */
  662. if (*returncode != LDAP_SUCCESS)
  663. break;
  664. for (i = 0; mods && (mods[i] && (LDAP_SUCCESS == rc)); i++)
  665. {
  666. if (*returncode != LDAP_SUCCESS)
  667. break;
  668. config_attr = (char *) mods[i]->mod_type;
  669. PR_ASSERT (config_attr);
  670. /* disallow modifications or removal of replica root,
  671. replica name and replica state attributes */
  672. if (strcasecmp (config_attr, attr_replicaRoot) == 0 ||
  673. strcasecmp (config_attr, attr_replicaName) == 0 ||
  674. strcasecmp (config_attr, attr_state) == 0)
  675. {
  676. *returncode = LDAP_UNWILLING_TO_PERFORM;
  677. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  678. "modification of %s attribute is not allowed",
  679. config_attr);
  680. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  681. "replica_config_post_modify: %s\n",
  682. errortext);
  683. }
  684. /* this is a request to delete an attribute */
  685. else if ((mods[i]->mod_op & LDAP_MOD_DELETE) ||
  686. mods[i]->mod_bvalues == NULL ||
  687. mods[i]->mod_bvalues[0]->bv_val == NULL)
  688. {
  689. ;
  690. }
  691. else /* modify an attribute */
  692. {
  693. config_attr_value = (char *) mods[i]->mod_bvalues[0]->bv_val;
  694. if (strcasecmp (config_attr, TASK_ATTR) == 0)
  695. {
  696. flag_need_cleanup = 1;
  697. }
  698. }
  699. }
  700. }
  701. done:
  702. PR_Unlock (s_configLock);
  703. /* slapi_ch_free accepts NULL pointer */
  704. slapi_ch_free_string (&replica_root);
  705. /* Call replica_cleanup_task after s_configLock is reliesed */
  706. if (flag_need_cleanup)
  707. {
  708. *returncode = replica_cleanup_task(mtnode_ext->replica,
  709. config_attr_value,
  710. errortext, apply_mods);
  711. }
  712. if (mtnode_ext->replica)
  713. object_release (mtnode_ext->replica);
  714. if (*returncode != LDAP_SUCCESS)
  715. {
  716. return SLAPI_DSE_CALLBACK_ERROR;
  717. }
  718. else
  719. {
  720. return SLAPI_DSE_CALLBACK_OK;
  721. }
  722. }
  723. static int
  724. replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  725. int *returncode, char *returntext, void *arg)
  726. {
  727. multimaster_mtnode_extension *mtnode_ext;
  728. Replica *r;
  729. PR_Lock (s_configLock);
  730. mtnode_ext = _replica_config_get_mtnode_ext (e);
  731. PR_ASSERT (mtnode_ext);
  732. if (mtnode_ext->replica)
  733. {
  734. /* remove object from the hash */
  735. r = (Replica*)object_get_data (mtnode_ext->replica);
  736. PR_ASSERT (r);
  737. /* The changelog for this replica is no longer valid, so we should remove it. */
  738. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_delete: "
  739. "Warning: The changelog for replica %s is no longer valid since "
  740. "the replica config is being deleted. Removing the changelog.\n",
  741. slapi_sdn_get_dn(replica_get_root(r)));
  742. cl5DeleteDBSync(mtnode_ext->replica);
  743. replica_delete_by_name (replica_get_name (r));
  744. object_release (mtnode_ext->replica);
  745. mtnode_ext->replica = NULL;
  746. }
  747. PR_Unlock (s_configLock);
  748. *returncode = LDAP_SUCCESS;
  749. return SLAPI_DSE_CALLBACK_OK;
  750. }
  751. static void
  752. replica_config_search_last_modified(Slapi_PBlock *pb, Slapi_Entry* e, Replica *replica)
  753. {
  754. Object *ruv_obj = NULL;
  755. RUV *ruv = NULL;
  756. Slapi_Value **values;
  757. if (replica == NULL)
  758. return;
  759. ruv_obj = replica_get_ruv(replica);
  760. ruv = object_get_data(ruv_obj);
  761. if ((values = ruv_last_modified_to_valuearray(ruv)) != NULL) {
  762. slapi_entry_add_values_sv(e, type_ruvElementUpdatetime, values);
  763. valuearray_free(&values);
  764. }
  765. object_release(ruv_obj);
  766. }
  767. static void
  768. replica_config_search_ruv(Slapi_PBlock *pb, Slapi_Entry* e, Replica *replica)
  769. {
  770. Object *ruv_obj = NULL;
  771. RUV *ruv = NULL;
  772. Slapi_Value **values;
  773. if (replica == NULL)
  774. return;
  775. ruv_obj = replica_get_ruv(replica);
  776. ruv = object_get_data(ruv_obj);
  777. if ((values = ruv_to_valuearray(ruv)) != NULL) {
  778. slapi_entry_add_values_sv(e, type_ruvElement, values);
  779. valuearray_free(&values);
  780. }
  781. object_release(ruv_obj);
  782. /* now add all the repl agmts maxcsnsruv */
  783. add_agmt_maxcsns(e, replica);
  784. }
  785. /* Returns PR_TRUE if 'attr' is present in the search requested attributes
  786. * else it returns PR_FALSE
  787. */
  788. static PRBool
  789. search_requested_attr(Slapi_PBlock *pb, const char *attr)
  790. {
  791. char **attrs = NULL;
  792. int i;
  793. slapi_pblock_get(pb, SLAPI_SEARCH_ATTRS, &attrs);
  794. if ((attr == NULL) || (attrs == NULL))
  795. return PR_FALSE;
  796. for (i = 0; attrs[i] != NULL; i++) {
  797. if (strcasecmp(attrs[i], attr) == 0) {
  798. return PR_TRUE;
  799. }
  800. }
  801. return PR_FALSE;
  802. }
  803. static int
  804. replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode,
  805. char *returntext, void *arg)
  806. {
  807. multimaster_mtnode_extension *mtnode_ext;
  808. int changeCount = 0;
  809. PRBool reapActive = PR_FALSE;
  810. char val [64];
  811. /* add attribute that contains number of entries in the changelog for this replica */
  812. PR_Lock (s_configLock);
  813. mtnode_ext = _replica_config_get_mtnode_ext (e);
  814. PR_ASSERT (mtnode_ext);
  815. if (mtnode_ext->replica) {
  816. Replica *replica;
  817. object_acquire (mtnode_ext->replica);
  818. if (cl5GetState () == CL5_STATE_OPEN) {
  819. changeCount = cl5GetOperationCount (mtnode_ext->replica);
  820. }
  821. replica = (Replica*)object_get_data (mtnode_ext->replica);
  822. if (replica) {
  823. reapActive = replica_get_tombstone_reap_active(replica);
  824. }
  825. /* Check if the in memory ruv is requested */
  826. if (search_requested_attr(pb, type_ruvElement)) {
  827. replica_config_search_ruv(pb, e, replica);
  828. }
  829. /* Check if the last update time is requested */
  830. if (search_requested_attr(pb, type_ruvElementUpdatetime)) {
  831. replica_config_search_last_modified(pb, e, replica);
  832. }
  833. object_release (mtnode_ext->replica);
  834. }
  835. sprintf (val, "%d", changeCount);
  836. slapi_entry_add_string (e, type_replicaChangeCount, val);
  837. slapi_entry_attr_set_int(e, "nsds5replicaReapActive", (int)reapActive);
  838. PR_Unlock (s_configLock);
  839. return SLAPI_DSE_CALLBACK_OK;
  840. }
  841. static int
  842. replica_config_change_type_and_id (Replica *r, const char *new_type,
  843. const char *new_id, char *returntext,
  844. int apply_mods)
  845. {
  846. int type;
  847. ReplicaType oldtype;
  848. ReplicaId rid;
  849. ReplicaId oldrid;
  850. PR_ASSERT (r);
  851. oldtype = replica_get_type(r);
  852. oldrid = replica_get_rid(r);
  853. if (new_type == NULL) /* by default - replica is read-only */
  854. {
  855. type = REPLICA_TYPE_READONLY;
  856. }
  857. else
  858. {
  859. type = atoi (new_type);
  860. if (type <= REPLICA_TYPE_UNKNOWN || type >= REPLICA_TYPE_END)
  861. {
  862. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "invalid replica type %d", type);
  863. return LDAP_OPERATIONS_ERROR;
  864. }
  865. }
  866. /* disallow changing type to itself just to permit a replica ID change */
  867. if (oldtype == type)
  868. {
  869. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica type is already %d - not changing", type);
  870. return LDAP_OPERATIONS_ERROR;
  871. }
  872. if (type == REPLICA_TYPE_READONLY)
  873. {
  874. rid = READ_ONLY_REPLICA_ID; /* default rid for read only */
  875. }
  876. else if (!new_id)
  877. {
  878. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "a replica ID is required when changing replica type to read-write");
  879. return LDAP_UNWILLING_TO_PERFORM;
  880. }
  881. else
  882. {
  883. int temprid = atoi (new_id);
  884. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID)
  885. {
  886. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  887. "attribute %s must have a value greater than 0 "
  888. "and less than %d",
  889. attr_replicaId, READ_ONLY_REPLICA_ID);
  890. return LDAP_UNWILLING_TO_PERFORM;
  891. }
  892. else
  893. {
  894. rid = (ReplicaId)temprid;
  895. }
  896. }
  897. /* error if old rid == new rid */
  898. if (oldrid == rid)
  899. {
  900. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica ID is already %d - not changing", rid);
  901. return LDAP_OPERATIONS_ERROR;
  902. }
  903. if (apply_mods)
  904. {
  905. Object *ruv_obj, *gen_obj;
  906. RUV *ruv;
  907. CSNGen *gen;
  908. ruv_obj = replica_get_ruv(r);
  909. if(ruv_obj){
  910. /* we need to rewrite the repl_csngen with the new rid */
  911. ruv = object_get_data (ruv_obj);
  912. gen_obj = replica_get_csngen (r);
  913. if(gen_obj){
  914. const char *purl = multimaster_get_local_purl();
  915. gen = (CSNGen*) object_get_data (gen_obj);
  916. csngen_rewrite_rid(gen, rid);
  917. if(purl && type == REPLICA_TYPE_UPDATABLE){
  918. ruv_add_replica(ruv, rid, purl);
  919. ruv_move_local_supplier_to_first(ruv, rid);
  920. replica_reset_csn_pl(r);
  921. }
  922. ruv_delete_replica(ruv, oldrid);
  923. replica_set_ruv_dirty(r);
  924. cl5CleanRUV(oldrid);
  925. replica_set_csn_assigned(r);
  926. }
  927. object_release(ruv_obj);
  928. }
  929. replica_set_type (r, type);
  930. replica_set_rid(r, rid);
  931. /* Set the mapping tree node, and the list of referrals */
  932. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  933. if (!replica_is_legacy_consumer(r))
  934. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  935. }
  936. return LDAP_SUCCESS;
  937. }
  938. static int
  939. replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext,
  940. int apply_mods)
  941. {
  942. PR_ASSERT (r);
  943. if (apply_mods)
  944. {
  945. Slapi_Mod smod;
  946. Slapi_ValueSet *vs= slapi_valueset_new();
  947. slapi_mod_init_byref(&smod, (LDAPMod *)mod); /* cast away const */
  948. slapi_valueset_set_from_smod(vs, &smod);
  949. replica_set_updatedn(r, vs, mod->mod_op);
  950. slapi_mod_done(&smod);
  951. slapi_valueset_free(vs);
  952. }
  953. return LDAP_SUCCESS;
  954. }
  955. static int
  956. replica_config_change_updatedngroup (Replica *r, const LDAPMod *mod, char *returntext,
  957. int apply_mods)
  958. {
  959. PR_ASSERT (r);
  960. if (apply_mods)
  961. {
  962. Slapi_Mod smod;
  963. Slapi_ValueSet *vs= slapi_valueset_new();
  964. slapi_mod_init_byref(&smod, (LDAPMod *)mod); /* cast away const */
  965. slapi_valueset_set_from_smod(vs, &smod);
  966. replica_set_groupdn(r, vs, mod->mod_op);
  967. slapi_mod_done(&smod);
  968. slapi_valueset_free(vs);
  969. }
  970. return LDAP_SUCCESS;
  971. }
  972. static int replica_config_change_flags (Replica *r, const char *new_flags,
  973. char *returntext, int apply_mods)
  974. {
  975. PR_ASSERT (r);
  976. if (apply_mods)
  977. {
  978. PRUint32 flags;
  979. flags = atol (new_flags);
  980. replica_replace_flags (r, flags);
  981. }
  982. return LDAP_SUCCESS;
  983. }
  984. static int replica_execute_task (Object *r, const char *task_name, char *returntext,
  985. int apply_mods)
  986. {
  987. if (strcasecmp (task_name, CL2LDIF_TASK) == 0)
  988. {
  989. if (apply_mods)
  990. {
  991. return replica_execute_cl2ldif_task (r, returntext);
  992. }
  993. else
  994. return LDAP_SUCCESS;
  995. }
  996. else if (strcasecmp (task_name, LDIF2CL_TASK) == 0)
  997. {
  998. if (apply_mods)
  999. {
  1000. return replica_execute_ldif2cl_task (r, returntext);
  1001. }
  1002. else
  1003. return LDAP_SUCCESS;
  1004. }
  1005. else if (strncasecmp (task_name, CLEANRUV, CLEANRUVLEN) == 0)
  1006. {
  1007. int temprid = atoi(&(task_name[CLEANRUVLEN]));
  1008. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  1009. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - %s", temprid, task_name);
  1010. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_execute_task: %s\n", returntext);
  1011. return LDAP_OPERATIONS_ERROR;
  1012. }
  1013. if (apply_mods)
  1014. {
  1015. return replica_execute_cleanruv_task (r, (ReplicaId)temprid, returntext);
  1016. }
  1017. else
  1018. return LDAP_SUCCESS;
  1019. }
  1020. else if (strncasecmp (task_name, CLEANALLRUV, CLEANALLRUVLEN) == 0)
  1021. {
  1022. int temprid = atoi(&(task_name[CLEANALLRUVLEN]));
  1023. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  1024. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)", temprid, task_name);
  1025. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_execute_task: %s\n", returntext);
  1026. return LDAP_OPERATIONS_ERROR;
  1027. }
  1028. if (apply_mods)
  1029. {
  1030. Slapi_Task *empty_task = NULL;
  1031. return replica_execute_cleanall_ruv_task(r, (ReplicaId)temprid, empty_task, returntext, "no");
  1032. }
  1033. else
  1034. return LDAP_SUCCESS;
  1035. }
  1036. else
  1037. {
  1038. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "unsupported replica task - %s", task_name);
  1039. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1040. "replica_execute_task: %s\n", returntext);
  1041. return LDAP_OPERATIONS_ERROR;
  1042. }
  1043. }
  1044. static int
  1045. replica_cleanup_task (Object *r, const char *task_name, char *returntext,
  1046. int apply_mods)
  1047. {
  1048. int rc = LDAP_SUCCESS;
  1049. if (apply_mods) {
  1050. Replica *replica = (Replica*)object_get_data (r);
  1051. if (NULL == replica) {
  1052. rc = LDAP_OPERATIONS_ERROR;
  1053. } else {
  1054. rc = replica_task_done(replica);
  1055. }
  1056. }
  1057. return rc;
  1058. }
  1059. static int
  1060. replica_task_done(Replica *replica)
  1061. {
  1062. int rc = LDAP_OPERATIONS_ERROR;
  1063. char *replica_dn = NULL;
  1064. Slapi_DN *replica_sdn = NULL;
  1065. Slapi_PBlock *pb = NULL;
  1066. LDAPMod *mods [2];
  1067. LDAPMod mod;
  1068. if (NULL == replica) {
  1069. return rc;
  1070. }
  1071. /* dn: cn=replica,cn=dc\3Dexample\2Cdc\3Dcom,cn=mapping tree,cn=config */
  1072. replica_dn = slapi_ch_smprintf("%s,cn=\"%s\",%s",
  1073. REPLICA_RDN,
  1074. slapi_sdn_get_dn(replica_get_root(replica)),
  1075. CONFIG_BASE);
  1076. if (NULL == replica_dn) {
  1077. return rc;
  1078. }
  1079. replica_sdn = slapi_sdn_new_dn_passin(replica_dn);
  1080. pb = slapi_pblock_new();
  1081. mods[0] = &mod;
  1082. mods[1] = NULL;
  1083. mod.mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
  1084. mod.mod_type = (char *)TASK_ATTR;
  1085. mod.mod_bvalues = NULL;
  1086. slapi_modify_internal_set_pb_ext(pb, replica_sdn, mods, NULL/* controls */,
  1087. NULL/* uniqueid */,
  1088. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  1089. 0/* flags */);
  1090. slapi_modify_internal_pb (pb);
  1091. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1092. if ((rc != LDAP_SUCCESS) && (rc != LDAP_NO_SUCH_ATTRIBUTE)) {
  1093. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1094. "replica_task_done: "
  1095. "failed to remove (%s) attribute from (%s) entry; "
  1096. "LDAP error - %d\n",
  1097. TASK_ATTR, replica_dn, rc);
  1098. }
  1099. slapi_pblock_destroy (pb);
  1100. slapi_sdn_free(&replica_sdn);
  1101. return rc;
  1102. }
  1103. static int replica_execute_cl2ldif_task (Object *r, char *returntext)
  1104. {
  1105. int rc;
  1106. Object *rlist [2];
  1107. Replica *replica;
  1108. char fName [MAXPATHLEN];
  1109. char *clDir = NULL;
  1110. if (cl5GetState () != CL5_STATE_OPEN)
  1111. {
  1112. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  1113. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1114. "replica_execute_cl2ldif_task: %s\n", returntext);
  1115. rc = LDAP_OPERATIONS_ERROR;
  1116. goto bail;
  1117. }
  1118. rlist[0] = r;
  1119. rlist[1] = NULL;
  1120. /* file is stored in the changelog directory and is named
  1121. <replica name>.ldif */
  1122. clDir = cl5GetDir ();
  1123. if (NULL == clDir) {
  1124. rc = LDAP_OPERATIONS_ERROR;
  1125. goto bail;
  1126. }
  1127. replica = (Replica*)object_get_data (r);
  1128. if (NULL == replica) {
  1129. rc = LDAP_OPERATIONS_ERROR;
  1130. goto bail;
  1131. }
  1132. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  1133. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1134. "Beginning changelog export of replica \"%s\"\n",
  1135. replica_get_name(replica));
  1136. rc = cl5ExportLDIF (fName, rlist);
  1137. if (rc == CL5_SUCCESS) {
  1138. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1139. "Finished changelog export of replica \"%s\"\n",
  1140. replica_get_name(replica));
  1141. rc = LDAP_SUCCESS;
  1142. } else {
  1143. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1144. "Failed changelog export replica %s; "
  1145. "changelog error - %d", replica_get_name(replica), rc);
  1146. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1147. "replica_execute_cl2ldif_task: %s\n", returntext);
  1148. rc = LDAP_OPERATIONS_ERROR;
  1149. }
  1150. bail:
  1151. slapi_ch_free_string (&clDir);
  1152. return rc;
  1153. }
  1154. static int replica_execute_ldif2cl_task (Object *r, char *returntext)
  1155. {
  1156. int rc, imprc = 0;
  1157. Object *rlist [2];
  1158. Replica *replica;
  1159. char fName [MAXPATHLEN];
  1160. char *clDir = NULL;
  1161. changelog5Config config;
  1162. if (cl5GetState () != CL5_STATE_OPEN)
  1163. {
  1164. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  1165. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1166. "replica_execute_ldif2cl_task: %s\n", returntext);
  1167. rc = LDAP_OPERATIONS_ERROR;
  1168. goto bail;
  1169. }
  1170. rlist[0] = r;
  1171. rlist[1] = NULL;
  1172. /* file is stored in the changelog directory and is named
  1173. <replica name>.ldif */
  1174. clDir = cl5GetDir ();
  1175. if (NULL == clDir) {
  1176. rc = LDAP_OPERATIONS_ERROR;
  1177. goto bail;
  1178. }
  1179. replica = (Replica*)object_get_data (r);
  1180. if (NULL == replica) {
  1181. rc = LDAP_OPERATIONS_ERROR;
  1182. goto bail;
  1183. }
  1184. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  1185. rc = cl5Close();
  1186. if (rc != CL5_SUCCESS)
  1187. {
  1188. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1189. "failed to close changelog to import changelog data; "
  1190. "changelog error - %d", rc);
  1191. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1192. "replica_execute_ldif2cl_task: %s\n", returntext);
  1193. rc = LDAP_OPERATIONS_ERROR;
  1194. goto bail;
  1195. }
  1196. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1197. "Beginning changelog import of replica \"%s\"\n",
  1198. replica_get_name(replica));
  1199. imprc = cl5ImportLDIF (clDir, fName, rlist);
  1200. if (CL5_SUCCESS == imprc)
  1201. {
  1202. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1203. "Finished changelog import of replica \"%s\"\n",
  1204. replica_get_name(replica));
  1205. }
  1206. else
  1207. {
  1208. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1209. "Failed changelog import replica %s; "
  1210. "changelog error - %d", replica_get_name(replica), rc);
  1211. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1212. "replica_execute_ldif2cl_task: %s\n", returntext);
  1213. imprc = LDAP_OPERATIONS_ERROR;
  1214. }
  1215. changelog5_read_config (&config);
  1216. /* restart changelog */
  1217. rc = cl5Open (config.dir, &config.dbconfig);
  1218. if (CL5_SUCCESS == rc)
  1219. {
  1220. rc = LDAP_SUCCESS;
  1221. }
  1222. else
  1223. {
  1224. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1225. "replica_execute_ldif2cl_task: failed to start changelog at %s\n",
  1226. config.dir?config.dir:"null config dir");
  1227. rc = LDAP_OPERATIONS_ERROR;
  1228. }
  1229. bail:
  1230. slapi_ch_free_string(&clDir);
  1231. changelog5_config_done(&config);
  1232. /* if cl5ImportLDIF returned an error, report it first. */
  1233. return imprc?imprc:rc;
  1234. }
  1235. static multimaster_mtnode_extension *
  1236. _replica_config_get_mtnode_ext (const Slapi_Entry *e)
  1237. {
  1238. const char *replica_root;
  1239. Slapi_DN *sdn = NULL;
  1240. mapping_tree_node *mtnode;
  1241. multimaster_mtnode_extension *ext = NULL;
  1242. /* retirve root of the tree for which replica is configured */
  1243. replica_root = slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  1244. if (replica_root == NULL)
  1245. {
  1246. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  1247. "configuration entry %s missing %s attribute\n",
  1248. slapi_entry_get_dn((Slapi_Entry *)e),
  1249. attr_replicaRoot);
  1250. return NULL;
  1251. }
  1252. sdn = slapi_sdn_new_dn_passin (replica_root);
  1253. /* locate mapping tree node for the specified subtree */
  1254. mtnode = slapi_get_mapping_tree_node_by_dn (sdn);
  1255. if (mtnode == NULL)
  1256. {
  1257. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  1258. "failed to locate mapping tree node for dn %s\n",
  1259. slapi_sdn_get_dn(sdn));
  1260. }
  1261. else
  1262. {
  1263. /* check if replica object already exists for the specified subtree */
  1264. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  1265. }
  1266. slapi_sdn_free (&sdn);
  1267. return ext;
  1268. }
  1269. int
  1270. replica_execute_cleanruv_task_ext(Object *r, ReplicaId rid)
  1271. {
  1272. return replica_execute_cleanruv_task(r, rid, NULL);
  1273. }
  1274. static int
  1275. replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext /* not used */)
  1276. {
  1277. Object *RUVObj;
  1278. RUV *local_ruv = NULL;
  1279. Replica *replica = (Replica*)object_get_data (r);
  1280. int rc = 0;
  1281. PR_ASSERT (replica);
  1282. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: cleaning rid (%d)...\n",(int)rid);
  1283. RUVObj = replica_get_ruv(replica);
  1284. PR_ASSERT(RUVObj);
  1285. local_ruv = (RUV*)object_get_data (RUVObj);
  1286. /* Need to check that :
  1287. * - rid is not the local one
  1288. * - rid is not the last one
  1289. */
  1290. if ((replica_get_rid(replica) == rid) ||
  1291. (ruv_replica_count(local_ruv) <= 1)) {
  1292. return LDAP_UNWILLING_TO_PERFORM;
  1293. }
  1294. rc = ruv_delete_replica(local_ruv, rid);
  1295. replica_set_ruv_dirty(replica);
  1296. if (replica_write_ruv(replica)) {
  1297. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: could not write RUV\n");
  1298. }
  1299. object_release(RUVObj);
  1300. /* Update Mapping Tree to reflect RUV changes */
  1301. consumer5_set_mapping_tree_state_for_replica(replica, NULL);
  1302. /*
  1303. * Clean the changelog RUV's
  1304. */
  1305. cl5CleanRUV(rid);
  1306. /*
  1307. * Now purge the changelog
  1308. */
  1309. trigger_cl_purging(replica);
  1310. if (rc != RUV_SUCCESS){
  1311. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanruv_task: task failed(%d)\n",rc);
  1312. return LDAP_OPERATIONS_ERROR;
  1313. }
  1314. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: finished successfully\n");
  1315. return LDAP_SUCCESS;
  1316. }
  1317. const char *
  1318. fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val)
  1319. {
  1320. Slapi_Attr *attr;
  1321. Slapi_Value *val = NULL;
  1322. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  1323. return default_val;
  1324. slapi_attr_first_value(attr, &val);
  1325. return slapi_value_get_string(val);
  1326. }
  1327. static int
  1328. replica_cleanall_ruv_task(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1329. int *returncode, char *returntext, void *arg)
  1330. {
  1331. Slapi_Task *task = NULL;
  1332. const Slapi_DN *task_dn;
  1333. Slapi_DN *dn = NULL;
  1334. ReplicaId rid = -1;
  1335. Object *r;
  1336. const char *force_cleaning;
  1337. const char *base_dn;
  1338. const char *rid_str;
  1339. int rc = SLAPI_DSE_CALLBACK_OK;
  1340. /* allocate new task now */
  1341. task = slapi_new_task(slapi_entry_get_ndn(e));
  1342. task_dn = slapi_entry_get_sdn(e);
  1343. if(task == NULL){
  1344. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV_task: Failed to create new task\n");
  1345. rc = SLAPI_DSE_CALLBACK_ERROR;
  1346. goto out;
  1347. }
  1348. /* register our destructor for waiting the task is done */
  1349. slapi_task_set_destructor_fn(task, replica_cleanall_ruv_destructor);
  1350. /*
  1351. * Get our task settings
  1352. */
  1353. if ((rid_str = fetch_attr(e, "replica-id", 0)) == NULL){
  1354. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing replica-id attribute");
  1355. cleanruv_log(task, -1, CLEANALLRUV_ID, "%s", returntext);
  1356. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1357. rc = SLAPI_DSE_CALLBACK_ERROR;
  1358. goto out;
  1359. }
  1360. rid = atoi(rid_str);
  1361. if ((base_dn = fetch_attr(e, "replica-base-dn", 0)) == NULL){
  1362. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing replica-base-dn attribute");
  1363. cleanruv_log(task, (int)rid, CLEANALLRUV_ID, "%s", returntext);
  1364. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1365. rc = SLAPI_DSE_CALLBACK_ERROR;
  1366. goto out;
  1367. }
  1368. if ((force_cleaning = fetch_attr(e, "replica-force-cleaning", 0)) != NULL){
  1369. if(strcasecmp(force_cleaning,"yes") != 0 && strcasecmp(force_cleaning,"no") != 0){
  1370. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid value for replica-force-cleaning "
  1371. "(%s). Value must be \"yes\" or \"no\" for task - (%s)",
  1372. force_cleaning, slapi_sdn_get_dn(task_dn));
  1373. cleanruv_log(task, (int)rid, CLEANALLRUV_ID, "%s", returntext);
  1374. *returncode = LDAP_OPERATIONS_ERROR;
  1375. rc = SLAPI_DSE_CALLBACK_ERROR;
  1376. goto out;
  1377. }
  1378. } else {
  1379. force_cleaning = "no";
  1380. }
  1381. /*
  1382. * Check the rid
  1383. */
  1384. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  1385. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)",
  1386. rid, slapi_sdn_get_dn(task_dn));
  1387. cleanruv_log(task, rid, CLEANALLRUV_ID, "%s", returntext);
  1388. *returncode = LDAP_OPERATIONS_ERROR;
  1389. rc = SLAPI_DSE_CALLBACK_ERROR;
  1390. goto out;
  1391. }
  1392. if(is_cleaned_rid(rid)){
  1393. /* we are already cleaning this rid */
  1394. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is already being cleaned", rid);
  1395. cleanruv_log(task, rid, CLEANALLRUV_ID, "%s", returntext);
  1396. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1397. rc = SLAPI_DSE_CALLBACK_ERROR;
  1398. goto out;
  1399. }
  1400. /*
  1401. * Get the replica object
  1402. */
  1403. dn = slapi_sdn_new_dn_byval(base_dn);
  1404. if((r = replica_get_replica_from_dn(dn)) == NULL){
  1405. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Could not find replica from dn(%s)",slapi_sdn_get_dn(dn));
  1406. cleanruv_log(task, rid, CLEANALLRUV_ID, "%s", returntext);
  1407. *returncode = LDAP_OPERATIONS_ERROR;
  1408. rc = SLAPI_DSE_CALLBACK_ERROR;
  1409. goto out;
  1410. }
  1411. /* clean the RUV's */
  1412. rc = replica_execute_cleanall_ruv_task (r, rid, task, force_cleaning, returntext);
  1413. out:
  1414. if(rc){
  1415. cleanruv_log(task, rid, CLEANALLRUV_ID, "Task failed...(%d)", rc);
  1416. slapi_task_finish(task, *returncode);
  1417. } else {
  1418. rc = SLAPI_DSE_CALLBACK_OK;
  1419. }
  1420. slapi_sdn_free(&dn);
  1421. return rc;
  1422. }
  1423. /*
  1424. * CLEANALLRUV task
  1425. *
  1426. * [1] Get the maxcsn from the RUV of the rid we want to clean
  1427. * [2] Create the payload for the "cleanallruv" extended ops
  1428. * [3] Create "monitor" thread to do the real work.
  1429. *
  1430. */
  1431. static int
  1432. replica_execute_cleanall_ruv_task (Object *r, ReplicaId rid, Slapi_Task *task, const char* force_cleaning, char *returntext)
  1433. {
  1434. struct berval *payload = NULL;
  1435. Slapi_Task *pre_task = NULL; /* this is supposed to be null for logging */
  1436. cleanruv_data *data = NULL;
  1437. PRThread *thread = NULL;
  1438. CSN *maxcsn = NULL;
  1439. Replica *replica;
  1440. char csnstr[CSN_STRSIZE];
  1441. char *ridstr = NULL;
  1442. char *basedn = NULL;
  1443. int rc = 0;
  1444. cleanruv_log(pre_task, rid, CLEANALLRUV_ID,"Initiating CleanAllRUV Task...");
  1445. if(get_cleanruv_task_count() >= CLEANRIDSIZ){
  1446. /* we are already running the maximum number of tasks */
  1447. cleanruv_log(pre_task, rid, CLEANALLRUV_ID,
  1448. "Exceeded maximum number of active CLEANALLRUV tasks(%d)",CLEANRIDSIZ);
  1449. return LDAP_UNWILLING_TO_PERFORM;
  1450. }
  1451. /*
  1452. * Grab the replica
  1453. */
  1454. if(r){
  1455. replica = (Replica*)object_get_data (r);
  1456. } else {
  1457. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Replica object is NULL, aborting task");
  1458. return -1;
  1459. }
  1460. /*
  1461. * Check if this is a consumer
  1462. */
  1463. if(replica_get_type(replica) == REPLICA_TYPE_READONLY){
  1464. /* this is a consumer, send error */
  1465. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to clean rid (%d), task can not be run on a consumer",rid);
  1466. if(task){
  1467. rc = -1;
  1468. slapi_task_finish(task, rc);
  1469. }
  1470. return -1;
  1471. }
  1472. /*
  1473. * Grab the max csn of the deleted replica
  1474. */
  1475. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Retrieving maxcsn...");
  1476. basedn = (char *)slapi_sdn_get_dn(replica_get_root(replica));
  1477. maxcsn = replica_cleanallruv_find_maxcsn(replica, rid, basedn);
  1478. if(maxcsn == NULL || csn_get_replicaid(maxcsn) == 0){
  1479. /*
  1480. * This is for consistency with extop csn creation, where
  1481. * we want the csn string to be "0000000000000000000" not ""
  1482. */
  1483. csn_free(&maxcsn);
  1484. maxcsn = csn_new();
  1485. csn_init_by_string(maxcsn, "");
  1486. }
  1487. csn_as_string(maxcsn, PR_FALSE, csnstr);
  1488. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Found maxcsn (%s)",csnstr);
  1489. /*
  1490. * Create payload
  1491. */
  1492. ridstr = slapi_ch_smprintf("%d:%s:%s:%s", rid, basedn, csnstr, force_cleaning);
  1493. payload = create_cleanruv_payload(ridstr);
  1494. slapi_ch_free_string(&ridstr);
  1495. if(payload == NULL){
  1496. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to create extended op payload, aborting task");
  1497. rc = -1;
  1498. goto fail;
  1499. }
  1500. /*
  1501. * Launch the cleanallruv thread. Once all the replicas are cleaned it will release the rid
  1502. */
  1503. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  1504. if (data == NULL) {
  1505. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to allocate cleanruv_data. Aborting task.");
  1506. rc = -1;
  1507. goto fail;
  1508. }
  1509. data->repl_obj = r;
  1510. data->replica = replica;
  1511. data->rid = rid;
  1512. data->task = task;
  1513. data->payload = payload;
  1514. data->sdn = NULL;
  1515. data->maxcsn = maxcsn;
  1516. data->repl_root = slapi_ch_strdup(basedn);
  1517. data->force = slapi_ch_strdup(force_cleaning);
  1518. thread = PR_CreateThread(PR_USER_THREAD, replica_cleanallruv_thread,
  1519. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1520. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1521. if (thread == NULL) {
  1522. rc = -1;
  1523. slapi_ch_free_string(&data->force);
  1524. slapi_ch_free_string(&data->repl_root);
  1525. goto fail;
  1526. } else {
  1527. goto done;
  1528. }
  1529. fail:
  1530. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to clean rid (%d)", rid);
  1531. if(task){
  1532. slapi_task_finish(task, rc);
  1533. }
  1534. if(payload){
  1535. ber_bvfree(payload);
  1536. }
  1537. csn_free(&maxcsn);
  1538. if(task) /* only the task acquires the r obj */
  1539. object_release (r);
  1540. done:
  1541. return rc;
  1542. }
  1543. void
  1544. replica_cleanallruv_thread_ext(void *arg)
  1545. {
  1546. replica_cleanallruv_thread(arg);
  1547. }
  1548. /*
  1549. * CLEANALLRUV Thread
  1550. *
  1551. * [1] Wait for the maxcsn to be covered
  1552. * [2] Make sure all the replicas are alive
  1553. * [3] Set the cleaned rid
  1554. * [4] Send the cleanAllRUV extop to all the replicas
  1555. * [5] Manually send the CLEANRUV task to replicas that do not support CLEANALLRUV
  1556. * [6] Wait for all the replicas to be cleaned.
  1557. * [7] Trigger cl trimming, release the rid, and remove all the "cleanallruv" attributes
  1558. * from the config.
  1559. */
  1560. static void
  1561. replica_cleanallruv_thread(void *arg)
  1562. {
  1563. cleanruv_data *data = arg;
  1564. Object *agmt_obj = NULL;
  1565. Object *ruv_obj = NULL;
  1566. Repl_Agmt *agmt = NULL;
  1567. RUV *ruv = NULL;
  1568. char csnstr[CSN_STRSIZE];
  1569. char *returntext = NULL;
  1570. char *rid_text = NULL;
  1571. int agmt_not_notified = 1;
  1572. int found_dirty_rid = 1;
  1573. int interval = 10;
  1574. int free_obj = 0;
  1575. int aborted = 0;
  1576. int rc = 0;
  1577. if (!data) {
  1578. return; /* no data */
  1579. }
  1580. if (data->task) {
  1581. slapi_task_inc_refcount(data->task);
  1582. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name,
  1583. "replica_cleanallruv_thread --> refcount incremented (%d).\n",
  1584. data->task->task_refcount);
  1585. }
  1586. /*
  1587. * Initialize our settings
  1588. */
  1589. if(data->replica == NULL && data->repl_obj == NULL){
  1590. /*
  1591. * This thread was initiated at startup because the process did not finish. Due
  1592. * to startup timing issues, we need to wait before grabbing the replica obj, as
  1593. * the backends might not be online yet.
  1594. */
  1595. PR_Lock( notify_lock );
  1596. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(10) );
  1597. PR_Unlock( notify_lock );
  1598. data->repl_obj = replica_get_replica_from_dn(data->sdn);
  1599. if(data->repl_obj == NULL){
  1600. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Unable to retrieve repl object from dn(%s).", data->sdn);
  1601. aborted = 1;
  1602. goto done;
  1603. }
  1604. data->replica = (Replica*)object_get_data(data->repl_obj);
  1605. free_obj = 1;
  1606. } else if(data->replica == NULL && data->repl_obj){
  1607. data->replica = (Replica*)object_get_data(data->repl_obj);
  1608. } else if( data->repl_obj == NULL && data->replica){
  1609. data->repl_obj = object_new(data->replica, NULL);
  1610. free_obj = 1;
  1611. }
  1612. /* verify we have set our repl objects */
  1613. if(data->repl_obj == NULL || data->replica == NULL){
  1614. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Unable to set the replica objects.");
  1615. aborted = 1;
  1616. goto done;
  1617. }
  1618. if(data->repl_root == NULL){
  1619. /* we must have resumed from start up, fill in the repl root */
  1620. data->repl_root = slapi_ch_strdup(slapi_sdn_get_dn(replica_get_root(data->replica)));
  1621. }
  1622. if(data->task){
  1623. slapi_task_begin(data->task, 1);
  1624. }
  1625. /*
  1626. * Presetting the rid prevents duplicate thread creation, but allows the db and changelog to still
  1627. * process updates from the rid. set_cleaned_rid() blocks updates, so we don't want to do that... yet.
  1628. */
  1629. preset_cleaned_rid(data->rid);
  1630. rid_text = slapi_ch_smprintf("%d", data->rid);
  1631. csn_as_string(data->maxcsn, PR_FALSE, csnstr);
  1632. /*
  1633. * Add the cleanallruv task to the repl config - so we can handle restarts
  1634. */
  1635. add_cleaned_rid(data->rid, data->replica, csnstr, data->force); /* marks config that we started cleaning a rid */
  1636. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Cleaning rid (%d)...", data->rid);
  1637. /*
  1638. * First, wait for the maxcsn to be covered
  1639. */
  1640. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Waiting to process all the updates from the deleted replica...");
  1641. ruv_obj = replica_get_ruv(data->replica);
  1642. ruv = object_get_data (ruv_obj);
  1643. while(data->maxcsn && !is_task_aborted(data->rid) && !is_cleaned_rid(data->rid) && !slapi_is_shutting_down()){
  1644. if(csn_get_replicaid(data->maxcsn) == 0 ||
  1645. ruv_covers_csn_cleanallruv(ruv,data->maxcsn) ||
  1646. strcasecmp(data->force,"yes") == 0)
  1647. {
  1648. /* We are caught up, now we can clean the ruv's */
  1649. break;
  1650. }
  1651. PR_Lock( notify_lock );
  1652. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(5) );
  1653. PR_Unlock( notify_lock );
  1654. }
  1655. object_release(ruv_obj);
  1656. /*
  1657. * Next, make sure all the replicas are up and running before sending off the clean ruv tasks
  1658. *
  1659. * Even if we are forcing the cleaning, the replicas still need to be up
  1660. */
  1661. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Waiting for all the replicas to be online...");
  1662. if(strcasecmp(data->force, "no") == 0 && check_agmts_are_alive(data->replica, data->rid, data->task)){
  1663. /* error, aborted or shutdown */
  1664. aborted = 1;
  1665. goto done;
  1666. }
  1667. /*
  1668. * Make sure all the replicas have seen the max csn
  1669. */
  1670. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Waiting for all the replicas to receive all the deleted replica updates...");
  1671. if(strcasecmp(data->force,"no") == 0 && check_agmts_are_caught_up(data, csnstr)){
  1672. /* error, aborted or shutdown */
  1673. aborted = 1;
  1674. goto done;
  1675. }
  1676. /*
  1677. * Set the rid as notified - this blocks the changelog from sending out updates
  1678. * during this process, as well as prevents the db ruv from getting polluted.
  1679. */
  1680. set_cleaned_rid(data->rid);
  1681. /*
  1682. * Now send the cleanruv extended op to all the agreements
  1683. */
  1684. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Sending cleanAllRUV task to all the replicas...");
  1685. while(agmt_not_notified && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1686. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1687. if(agmt_obj == NULL){
  1688. /* no agmts, just clean this replica */
  1689. break;
  1690. }
  1691. while (agmt_obj && !slapi_is_shutting_down()){
  1692. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1693. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1694. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1695. agmt_not_notified = 0;
  1696. continue;
  1697. }
  1698. if(replica_cleanallruv_send_extop(agmt, data, 1) == 0){
  1699. agmt_not_notified = 0;
  1700. } else {
  1701. agmt_not_notified = 1;
  1702. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Failed to send task to replica (%s)",agmt_get_long_name(agmt));
  1703. if(strcasecmp(data->force,"no") == 0){
  1704. break;
  1705. }
  1706. }
  1707. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1708. }
  1709. if(is_task_aborted(data->rid)){
  1710. aborted = 1;
  1711. goto done;
  1712. }
  1713. if(agmt_not_notified == 0 || strcasecmp(data->force, "yes") == 0){
  1714. break;
  1715. }
  1716. /*
  1717. * need to sleep between passes
  1718. */
  1719. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Not all replicas have received the "
  1720. "cleanallruv extended op, retrying in %d seconds",interval);
  1721. if(!slapi_is_shutting_down()){
  1722. PR_Lock( notify_lock );
  1723. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1724. PR_Unlock( notify_lock );
  1725. }
  1726. if(interval < 14400){ /* 4 hour max */
  1727. interval = interval * 2;
  1728. } else {
  1729. interval = 14400;
  1730. }
  1731. }
  1732. /*
  1733. * Run the CLEANRUV task
  1734. */
  1735. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Cleaning local ruv's...");
  1736. replica_execute_cleanruv_task (data->repl_obj, data->rid, returntext);
  1737. /*
  1738. * Wait for all the replicas to be cleaned
  1739. */
  1740. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  1741. "Waiting for all the replicas to be cleaned...");
  1742. interval = 10;
  1743. while(found_dirty_rid && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1744. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1745. if(agmt_obj == NULL){
  1746. break;
  1747. }
  1748. while (agmt_obj && !slapi_is_shutting_down()){
  1749. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1750. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1751. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1752. found_dirty_rid = 0;
  1753. continue;
  1754. }
  1755. if(replica_cleanallruv_check_ruv(data->repl_root, agmt, rid_text, data->task, data->force) == 0){
  1756. found_dirty_rid = 0;
  1757. } else {
  1758. found_dirty_rid = 1;
  1759. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Replica is not cleaned yet (%s)",
  1760. agmt_get_long_name(agmt));
  1761. break;
  1762. }
  1763. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1764. }
  1765. /* If the task is abort or everyone is cleaned, break out */
  1766. if(is_task_aborted(data->rid)){
  1767. aborted = 1;
  1768. goto done;
  1769. }
  1770. if(found_dirty_rid == 0 || strcasecmp(data->force, "yes") == 0){
  1771. break;
  1772. }
  1773. /*
  1774. * Need to sleep between passes unless we are shutting down
  1775. */
  1776. if (!slapi_is_shutting_down()){
  1777. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Replicas have not been cleaned yet, "
  1778. "retrying in %d seconds", interval);
  1779. PR_Lock( notify_lock );
  1780. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1781. PR_Unlock( notify_lock );
  1782. }
  1783. if(interval < 14400){ /* 4 hour max */
  1784. interval = interval * 2;
  1785. } else {
  1786. interval = 14400;
  1787. }
  1788. } /* while */
  1789. done:
  1790. /*
  1791. * If the replicas are cleaned, release the rid
  1792. */
  1793. if(!aborted && !slapi_is_shutting_down()){
  1794. /*
  1795. * Success - the rid has been cleaned!
  1796. *
  1797. * Delete the cleaned rid config.
  1798. * Make sure all the replicas have been "pre_cleaned"
  1799. * Remove the keep alive entry if present
  1800. * Remove the rid from the internal clean list
  1801. */
  1802. delete_cleaned_rid_config(data);
  1803. check_replicas_are_done_cleaning(data);
  1804. remove_keep_alive_entry(data->task, data->rid, data->repl_root);
  1805. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Successfully cleaned rid(%d).", data->rid);
  1806. remove_cleaned_rid(data->rid);
  1807. } else {
  1808. /*
  1809. * Shutdown or abort
  1810. */
  1811. if(!is_task_aborted(data->rid) || slapi_is_shutting_down()){
  1812. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Server shutting down. Process will resume at server startup");
  1813. } else {
  1814. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Task aborted for rid(%d).",data->rid);
  1815. delete_cleaned_rid_config(data);
  1816. remove_cleaned_rid(data->rid);
  1817. }
  1818. }
  1819. if(data->task){
  1820. slapi_task_finish(data->task, rc);
  1821. slapi_task_dec_refcount(data->task);
  1822. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_cleanallruv_thread <-- refcount decremented.\n");
  1823. }
  1824. if(data->payload){
  1825. ber_bvfree(data->payload);
  1826. }
  1827. if(data->repl_obj && free_obj){
  1828. object_release(data->repl_obj);
  1829. }
  1830. csn_free(&data->maxcsn);
  1831. slapi_sdn_free(&data->sdn);
  1832. slapi_ch_free_string(&data->repl_root);
  1833. slapi_ch_free_string(&data->force);
  1834. slapi_ch_free_string(&rid_text);
  1835. slapi_ch_free((void **)&data);
  1836. }
  1837. /*
  1838. * Remove the "Keep-Alive" replication entry.
  1839. */
  1840. static void
  1841. remove_keep_alive_entry(Slapi_Task *task, ReplicaId rid, const char *repl_root)
  1842. {
  1843. Slapi_PBlock *delete_pb = NULL;
  1844. char *keep_alive_dn = NULL;
  1845. int rc = 0;
  1846. /* Construct the repl keep alive dn from the rid and replication suffix */
  1847. keep_alive_dn = PR_smprintf("cn=repl keep alive %d,%s", (int)rid, repl_root);
  1848. delete_pb = slapi_pblock_new();
  1849. slapi_delete_internal_set_pb(delete_pb, keep_alive_dn ,NULL, NULL,
  1850. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  1851. slapi_delete_internal_pb(delete_pb);
  1852. slapi_pblock_get(delete_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1853. if (rc == LDAP_NO_SUCH_OBJECT) {
  1854. /* No problem, it's not always there */
  1855. cleanruv_log(task, rid, CLEANALLRUV_ID, "No Keep-Alive entry to remove (%s)",
  1856. keep_alive_dn);
  1857. } else if (rc != LDAP_SUCCESS){
  1858. /* Failed to delete the entry */
  1859. cleanruv_log(task, rid, CLEANALLRUV_ID, "Failed to delete Keep-Alive entry (%s) "
  1860. "Error (%d) This entry will need to be manually removed",
  1861. keep_alive_dn, rc);
  1862. } else {
  1863. /* Success */
  1864. cleanruv_log(task, rid, CLEANALLRUV_ID, "Removed Keep-Alive entry (%s)", keep_alive_dn);
  1865. }
  1866. slapi_pblock_destroy(delete_pb);
  1867. slapi_ch_free_string(&keep_alive_dn);
  1868. }
  1869. static void
  1870. replica_cleanall_ruv_destructor(Slapi_Task *task)
  1871. {
  1872. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1873. "replica_cleanall_ruv_destructor -->\n" );
  1874. stop_ruv_cleaning();
  1875. if (task) {
  1876. while (slapi_task_get_refcount(task) > 0) {
  1877. /* Yield to wait for the fixup task finishes. */
  1878. DS_Sleep (PR_MillisecondsToInterval(100));
  1879. }
  1880. }
  1881. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1882. "replica_cleanall_ruv_destructor <--\n" );
  1883. }
  1884. static void
  1885. replica_cleanall_ruv_abort_destructor(Slapi_Task *task)
  1886. {
  1887. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1888. "replica_cleanall_ruv_abort_destructor -->\n" );
  1889. stop_ruv_cleaning();
  1890. if (task) {
  1891. while (slapi_task_get_refcount(task) > 0) {
  1892. /* Yield to wait for the fixup task finishes. */
  1893. DS_Sleep (PR_MillisecondsToInterval(100));
  1894. }
  1895. }
  1896. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1897. "replica_cleanall_ruv_abort_destructor <--\n" );
  1898. }
  1899. /*
  1900. * Loop over the agmts, and check if they are in the last phase of cleaning, meaning they have
  1901. * released cleanallruv data from the config
  1902. */
  1903. static void
  1904. check_replicas_are_done_cleaning(cleanruv_data *data )
  1905. {
  1906. Object *agmt_obj;
  1907. Repl_Agmt *agmt;
  1908. char csnstr[CSN_STRSIZE];
  1909. char *filter = NULL;
  1910. int not_all_cleaned = 1;
  1911. int interval = 10;
  1912. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  1913. "Waiting for all the replicas to finish cleaning...");
  1914. csn_as_string(data->maxcsn, PR_FALSE, csnstr);
  1915. filter = PR_smprintf("(%s=%d:%s:%s)", type_replicaCleanRUV,(int)data->rid, csnstr, data->force);
  1916. while(not_all_cleaned && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1917. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1918. if(agmt_obj == NULL){
  1919. not_all_cleaned = 0;
  1920. break;
  1921. }
  1922. while (agmt_obj && !slapi_is_shutting_down()){
  1923. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1924. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1925. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1926. not_all_cleaned = 0;
  1927. continue;
  1928. }
  1929. if(replica_cleanallruv_is_finished(agmt, filter, data->task) == 0){
  1930. not_all_cleaned = 0;
  1931. } else {
  1932. not_all_cleaned = 1;
  1933. break;
  1934. }
  1935. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1936. }
  1937. if(not_all_cleaned == 0 ||
  1938. is_task_aborted(data->rid) ||
  1939. strcasecmp(data->force, "yes") == 0)
  1940. {
  1941. break;
  1942. }
  1943. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  1944. "Not all replicas finished cleaning, retrying in %d seconds",
  1945. interval);
  1946. if(!slapi_is_shutting_down()){
  1947. PR_Lock( notify_lock );
  1948. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1949. PR_Unlock( notify_lock );
  1950. }
  1951. if(interval < 14400){ /* 4 hour max */
  1952. interval = interval * 2;
  1953. } else {
  1954. interval = 14400;
  1955. }
  1956. }
  1957. slapi_ch_free_string(&filter);
  1958. }
  1959. /*
  1960. * Search this replica for the nsds5ReplicaCleanruv attribute, we don't return
  1961. * an entry, we know it has finished cleaning.
  1962. */
  1963. static int
  1964. replica_cleanallruv_is_finished(Repl_Agmt *agmt, char *filter, Slapi_Task *task)
  1965. {
  1966. Repl_Connection *conn = NULL;
  1967. ConnResult crc = 0;
  1968. struct berval *payload = NULL;
  1969. int msgid = 0;
  1970. int rc = -1;
  1971. if((conn = conn_new(agmt)) == NULL){
  1972. return rc;
  1973. }
  1974. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  1975. payload = create_cleanruv_payload(filter);
  1976. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_CHECK_STATUS_OID, payload, NULL, &msgid);
  1977. if(crc == CONN_OPERATION_SUCCESS){
  1978. struct berval *retsdata = NULL;
  1979. char *retoid = NULL;
  1980. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  1981. if (CONN_OPERATION_SUCCESS == crc ){
  1982. char *response = NULL;
  1983. decode_cleanruv_payload(retsdata, &response);
  1984. if(response == NULL){
  1985. /* this replica does not support cleanallruv */
  1986. rc = 0;
  1987. } else if(strcmp(response,CLEANRUV_FINISHED) == 0){
  1988. /* finished cleaning */
  1989. rc = 0;
  1990. }
  1991. if (NULL != retsdata)
  1992. ber_bvfree(retsdata);
  1993. slapi_ch_free_string(&response);
  1994. slapi_ch_free_string(&retoid);
  1995. }
  1996. }
  1997. }
  1998. conn_delete_internal_ext(conn);
  1999. if(payload)
  2000. ber_bvfree(payload);
  2001. return rc;
  2002. }
  2003. /*
  2004. * Loop over the agmts, and check if they are in the last phase of aborting, meaning they have
  2005. * released the abort cleanallruv data from the config
  2006. */
  2007. static void
  2008. check_replicas_are_done_aborting(cleanruv_data *data )
  2009. {
  2010. Object *agmt_obj;
  2011. Repl_Agmt *agmt;
  2012. char *filter = NULL;
  2013. int not_all_aborted = 1;
  2014. int interval = 10;
  2015. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Waiting for all the replicas to finish aborting...");
  2016. filter = PR_smprintf("(%s=%d:%s)", type_replicaAbortCleanRUV, data->rid, data->repl_root);
  2017. while(not_all_aborted && !slapi_is_shutting_down()){
  2018. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  2019. if(agmt_obj == NULL){
  2020. not_all_aborted = 0;
  2021. break;
  2022. }
  2023. while (agmt_obj && !slapi_is_shutting_down()){
  2024. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2025. if(get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2026. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2027. not_all_aborted = 0;
  2028. continue;
  2029. }
  2030. if(replica_cleanallruv_is_finished(agmt, filter, data->task) == 0){
  2031. not_all_aborted = 0;
  2032. } else {
  2033. not_all_aborted = 1;
  2034. break;
  2035. }
  2036. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2037. }
  2038. if(not_all_aborted == 0){
  2039. break;
  2040. }
  2041. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Not all replicas finished aborting, retrying in %d seconds",interval);
  2042. if(!slapi_is_shutting_down()){
  2043. PR_Lock( notify_lock );
  2044. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2045. PR_Unlock( notify_lock );
  2046. }
  2047. if(interval < 14400){ /* 4 hour max */
  2048. interval = interval * 2;
  2049. } else {
  2050. interval = 14400;
  2051. }
  2052. }
  2053. slapi_ch_free_string(&filter);
  2054. }
  2055. /*
  2056. * Waits for all the repl agmts to be have have the maxcsn. Returns error only on abort or shutdown
  2057. */
  2058. static int
  2059. check_agmts_are_caught_up(cleanruv_data *data, char *maxcsn)
  2060. {
  2061. Object *agmt_obj;
  2062. Repl_Agmt *agmt;
  2063. char *rid_text;
  2064. int not_all_caughtup = 1;
  2065. int interval = 10;
  2066. rid_text = slapi_ch_smprintf("%d", data->rid);
  2067. while(not_all_caughtup && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  2068. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  2069. if(agmt_obj == NULL){
  2070. not_all_caughtup = 0;
  2071. break;
  2072. }
  2073. while (agmt_obj && !slapi_is_shutting_down()){
  2074. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2075. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2076. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2077. not_all_caughtup = 0;
  2078. continue;
  2079. }
  2080. if(replica_cleanallruv_check_maxcsn(agmt, data->repl_root, rid_text, maxcsn, data->task) == 0){
  2081. not_all_caughtup = 0;
  2082. } else {
  2083. not_all_caughtup = 1;
  2084. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Replica not caught up (%s)",agmt_get_long_name(agmt));
  2085. break;
  2086. }
  2087. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2088. }
  2089. if(not_all_caughtup == 0 || is_task_aborted(data->rid) ){
  2090. break;
  2091. }
  2092. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  2093. "Not all replicas caught up, retrying in %d seconds",interval);
  2094. if(!slapi_is_shutting_down()){
  2095. PR_Lock( notify_lock );
  2096. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2097. PR_Unlock( notify_lock );
  2098. }
  2099. if(interval < 14400){ /* 4 hour max */
  2100. interval = interval * 2;
  2101. } else {
  2102. interval = 14400;
  2103. }
  2104. }
  2105. slapi_ch_free_string(&rid_text);
  2106. if(is_task_aborted(data->rid)){
  2107. return -1;
  2108. }
  2109. return not_all_caughtup;
  2110. }
  2111. /*
  2112. * Waits for all the repl agmts to be online. Returns error only on abort or shutdown
  2113. */
  2114. static int
  2115. check_agmts_are_alive(Replica *replica, ReplicaId rid, Slapi_Task *task)
  2116. {
  2117. Object *agmt_obj;
  2118. Repl_Agmt *agmt;
  2119. int not_all_alive = 1;
  2120. int interval = 10;
  2121. while(not_all_alive && is_task_aborted(rid) == 0 && !slapi_is_shutting_down()){
  2122. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  2123. if(agmt_obj == NULL){
  2124. not_all_alive = 0;
  2125. break;
  2126. }
  2127. while (agmt_obj && !slapi_is_shutting_down()){
  2128. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2129. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2130. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2131. not_all_alive = 0;
  2132. continue;
  2133. }
  2134. if(replica_cleanallruv_replica_alive(agmt) == 0){
  2135. not_all_alive = 0;
  2136. } else {
  2137. not_all_alive = 1;
  2138. cleanruv_log(task, rid, CLEANALLRUV_ID, "Replica not online (%s)",
  2139. agmt_get_long_name(agmt));
  2140. break;
  2141. }
  2142. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2143. }
  2144. if(not_all_alive == 0 || is_task_aborted(rid)){
  2145. break;
  2146. }
  2147. cleanruv_log(task, rid, CLEANALLRUV_ID, "Not all replicas online, retrying in %d seconds...",
  2148. interval);
  2149. if(!slapi_is_shutting_down()){
  2150. PR_Lock( notify_lock );
  2151. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2152. PR_Unlock( notify_lock );
  2153. }
  2154. if(interval < 14400){ /* 4 hour max */
  2155. interval = interval * 2;
  2156. } else {
  2157. interval = 14400;
  2158. }
  2159. }
  2160. if(is_task_aborted(rid)){
  2161. return -1;
  2162. }
  2163. return not_all_alive;
  2164. }
  2165. /*
  2166. * Create the CLEANALLRUV extended op payload
  2167. */
  2168. struct berval *
  2169. create_cleanruv_payload(char *value)
  2170. {
  2171. struct berval *req_data = NULL;
  2172. BerElement *tmp_bere = NULL;
  2173. if ((tmp_bere = der_alloc()) == NULL){
  2174. goto error;
  2175. }
  2176. if (ber_printf(tmp_bere, "{s}", value) == -1){
  2177. goto error;
  2178. }
  2179. if (ber_flatten(tmp_bere, &req_data) == -1){
  2180. goto error;
  2181. }
  2182. goto done;
  2183. error:
  2184. if (NULL != req_data){
  2185. ber_bvfree(req_data);
  2186. req_data = NULL;
  2187. }
  2188. done:
  2189. if (NULL != tmp_bere){
  2190. ber_free(tmp_bere, 1);
  2191. tmp_bere = NULL;
  2192. }
  2193. return req_data;
  2194. }
  2195. /*
  2196. * Manually add the CLEANRUV task to replicas that do not support
  2197. * the CLEANALLRUV task.
  2198. */
  2199. static void
  2200. replica_send_cleanruv_task(Repl_Agmt *agmt, cleanruv_data *clean_data)
  2201. {
  2202. Repl_Connection *conn;
  2203. ConnResult crc = 0;
  2204. LDAP *ld;
  2205. Slapi_DN *sdn;
  2206. struct berval *vals[2];
  2207. struct berval val;
  2208. LDAPMod *mods[2];
  2209. LDAPMod mod;
  2210. char *repl_dn = NULL;
  2211. char data[15];
  2212. int rc;
  2213. if((conn = conn_new(agmt)) == NULL){
  2214. return;
  2215. }
  2216. crc = conn_connect(conn);
  2217. if (CONN_OPERATION_SUCCESS != crc){
  2218. conn_delete_internal_ext(conn);
  2219. return;
  2220. }
  2221. ld = conn_get_ldap(conn);
  2222. if(ld == NULL){
  2223. conn_delete_internal_ext(conn);
  2224. return;
  2225. }
  2226. sdn = agmt_get_replarea(agmt);
  2227. if (!sdn) {
  2228. conn_delete_internal_ext(conn);
  2229. return;
  2230. }
  2231. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2232. mod.mod_type = "nsds5task";
  2233. mod.mod_bvalues = vals;
  2234. vals [0] = &val;
  2235. vals [1] = NULL;
  2236. val.bv_len = PR_snprintf(data, sizeof(data), "CLEANRUV%d", clean_data->rid);
  2237. val.bv_val = data;
  2238. mods[0] = &mod;
  2239. mods[1] = NULL;
  2240. repl_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", slapi_sdn_get_dn(sdn));
  2241. /*
  2242. * Add task to remote replica
  2243. */
  2244. rc = ldap_modify_ext_s( ld, repl_dn, mods, NULL, NULL);
  2245. if(rc != LDAP_SUCCESS){
  2246. char *hostname = agmt_get_hostname(agmt);
  2247. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2248. "Failed to add CLEANRUV task (%s) to replica (%s). You will need "
  2249. "to manually run the CLEANRUV task on this replica (%s) error (%d)",
  2250. repl_dn, agmt_get_long_name(agmt), hostname, rc);
  2251. slapi_ch_free_string(&hostname);
  2252. }
  2253. slapi_ch_free_string(&repl_dn);
  2254. slapi_sdn_free(&sdn);
  2255. conn_delete_internal_ext(conn);
  2256. }
  2257. /*
  2258. * Check if the rid is in our list of "cleaned" rids
  2259. */
  2260. int
  2261. is_cleaned_rid(ReplicaId rid)
  2262. {
  2263. int i;
  2264. slapi_rwlock_rdlock(rid_lock);
  2265. for(i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != 0; i++){
  2266. if(rid == cleaned_rids[i]){
  2267. slapi_rwlock_unlock(rid_lock);
  2268. return 1;
  2269. }
  2270. }
  2271. slapi_rwlock_unlock(rid_lock);
  2272. return 0;
  2273. }
  2274. int
  2275. is_pre_cleaned_rid(ReplicaId rid)
  2276. {
  2277. int i;
  2278. slapi_rwlock_rdlock(rid_lock);
  2279. for(i = 0; i < CLEANRIDSIZ && pre_cleaned_rids[i] != 0; i++){
  2280. if(rid == pre_cleaned_rids[i]){
  2281. slapi_rwlock_unlock(rid_lock);
  2282. return 1;
  2283. }
  2284. }
  2285. slapi_rwlock_unlock(rid_lock);
  2286. return 0;
  2287. }
  2288. int
  2289. is_task_aborted(ReplicaId rid)
  2290. {
  2291. int i;
  2292. if(rid == 0){
  2293. return 0;
  2294. }
  2295. slapi_rwlock_rdlock(abort_rid_lock);
  2296. for(i = 0; i < CLEANRIDSIZ && aborted_rids[i] != 0; i++){
  2297. if(rid == aborted_rids[i]){
  2298. slapi_rwlock_unlock(abort_rid_lock);
  2299. return 1;
  2300. }
  2301. }
  2302. slapi_rwlock_unlock(abort_rid_lock);
  2303. return 0;
  2304. }
  2305. static void
  2306. preset_cleaned_rid(ReplicaId rid)
  2307. {
  2308. int i;
  2309. slapi_rwlock_wrlock(rid_lock);
  2310. for(i = 0; i < CLEANRIDSIZ; i++){
  2311. if(pre_cleaned_rids[i] == 0){
  2312. pre_cleaned_rids[i] = rid;
  2313. pre_cleaned_rids[i + 1] = 0;
  2314. break;
  2315. }
  2316. }
  2317. slapi_rwlock_unlock(rid_lock);
  2318. }
  2319. /*
  2320. * Just add the rid to the in memory, as we don't want it to survive after a restart,
  2321. * This prevent the changelog from sending updates from this rid, and the local ruv
  2322. * will not be updated either.
  2323. */
  2324. void
  2325. set_cleaned_rid(ReplicaId rid)
  2326. {
  2327. int i;
  2328. slapi_rwlock_wrlock(rid_lock);
  2329. for(i = 0; i < CLEANRIDSIZ; i++){
  2330. if(cleaned_rids[i] == 0){
  2331. cleaned_rids[i] = rid;
  2332. cleaned_rids[i + 1] = 0;
  2333. }
  2334. }
  2335. slapi_rwlock_unlock(rid_lock);
  2336. }
  2337. /*
  2338. * Add the rid and maxcsn to the repl config (so we can resume after a server restart)
  2339. */
  2340. void
  2341. add_cleaned_rid(ReplicaId rid, Replica *r, char *maxcsn, char *forcing)
  2342. {
  2343. Slapi_PBlock *pb;
  2344. struct berval *vals[2];
  2345. struct berval val;
  2346. LDAPMod *mods[2];
  2347. LDAPMod mod;
  2348. char data[CSN_STRSIZE + 10];
  2349. char *dn;
  2350. int rc;
  2351. if(r == NULL || maxcsn == NULL){
  2352. return;
  2353. }
  2354. /*
  2355. * Write the rid & maxcsn to the config entry
  2356. */
  2357. val.bv_len = PR_snprintf(data, sizeof(data),"%d:%s:%s", rid, maxcsn, forcing);
  2358. dn = replica_get_dn(r);
  2359. pb = slapi_pblock_new();
  2360. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2361. mod.mod_type = (char *)type_replicaCleanRUV;
  2362. mod.mod_bvalues = vals;
  2363. vals [0] = &val;
  2364. vals [1] = NULL;
  2365. val.bv_val = data;
  2366. mods[0] = &mod;
  2367. mods[1] = NULL;
  2368. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  2369. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2370. slapi_modify_internal_pb (pb);
  2371. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2372. if (rc != LDAP_SUCCESS && rc != LDAP_TYPE_OR_VALUE_EXISTS && rc != LDAP_NO_SUCH_OBJECT){
  2373. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: failed to update replica "
  2374. "config (%d), rid (%d)\n", rc, rid);
  2375. }
  2376. slapi_ch_free_string(&dn);
  2377. slapi_pblock_destroy(pb);
  2378. }
  2379. /*
  2380. * Add aborted rid and repl root to config in case of a server restart
  2381. */
  2382. void
  2383. add_aborted_rid(ReplicaId rid, Replica *r, char *repl_root)
  2384. {
  2385. Slapi_PBlock *pb;
  2386. struct berval *vals[2];
  2387. struct berval val;
  2388. LDAPMod *mods[2];
  2389. LDAPMod mod;
  2390. char *data;
  2391. char *dn;
  2392. int rc;
  2393. int i;
  2394. slapi_rwlock_wrlock(abort_rid_lock);
  2395. for(i = 0; i < CLEANRIDSIZ; i++){
  2396. if(aborted_rids[i] == 0){
  2397. aborted_rids[i] = rid;
  2398. aborted_rids[i + 1] = 0;
  2399. break;
  2400. }
  2401. }
  2402. slapi_rwlock_unlock(abort_rid_lock);
  2403. /*
  2404. * Write the rid to the config entry
  2405. */
  2406. dn = replica_get_dn(r);
  2407. pb = slapi_pblock_new();
  2408. data = PR_smprintf("%d:%s", rid, repl_root);
  2409. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2410. mod.mod_type = (char *)type_replicaAbortCleanRUV;
  2411. mod.mod_bvalues = vals;
  2412. vals [0] = &val;
  2413. vals [1] = NULL;
  2414. val.bv_val = data;
  2415. val.bv_len = strlen (data);
  2416. mods[0] = &mod;
  2417. mods[1] = NULL;
  2418. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  2419. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2420. slapi_modify_internal_pb (pb);
  2421. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2422. if (rc != LDAP_SUCCESS && rc != LDAP_TYPE_OR_VALUE_EXISTS && rc != LDAP_NO_SUCH_OBJECT){
  2423. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to update "
  2424. "replica config (%d), rid (%d)\n", rc, rid);
  2425. }
  2426. slapi_ch_free_string(&dn);
  2427. slapi_ch_free_string(&data);
  2428. slapi_pblock_destroy(pb);
  2429. }
  2430. void
  2431. delete_aborted_rid(Replica *r, ReplicaId rid, char *repl_root, int skip){
  2432. Slapi_PBlock *pb;
  2433. LDAPMod *mods[2];
  2434. LDAPMod mod;
  2435. struct berval *vals[2];
  2436. struct berval val;
  2437. char *data;
  2438. char *dn;
  2439. int rc;
  2440. int i;
  2441. if(r == NULL)
  2442. return;
  2443. if(skip){
  2444. /* skip the deleting of the config, and just remove the in memory rid */
  2445. slapi_rwlock_wrlock(abort_rid_lock);
  2446. for(i = 0; i < CLEANRIDSIZ && aborted_rids[i] != rid; i++); /* found rid, stop */
  2447. for(; i < CLEANRIDSIZ; i++){
  2448. /* rewrite entire array */
  2449. aborted_rids[i] = aborted_rids[i + 1];
  2450. }
  2451. slapi_rwlock_unlock(abort_rid_lock);
  2452. } else {
  2453. /* only remove the config, leave the in-memory rid */
  2454. dn = replica_get_dn(r);
  2455. data = PR_smprintf("%d:%s", (int)rid, repl_root);
  2456. mod.mod_op = LDAP_MOD_DELETE|LDAP_MOD_BVALUES;
  2457. mod.mod_type = (char *)type_replicaAbortCleanRUV;
  2458. mod.mod_bvalues = vals;
  2459. vals [0] = &val;
  2460. vals [1] = NULL;
  2461. val.bv_val = data;
  2462. val.bv_len = strlen (data);
  2463. mods[0] = &mod;
  2464. mods[1] = NULL;
  2465. pb = slapi_pblock_new();
  2466. slapi_modify_internal_set_pb(pb, dn, mods, NULL, NULL, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2467. slapi_modify_internal_pb (pb);
  2468. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2469. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2470. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to remove replica "
  2471. "config (%d), rid (%d)\n", rc, rid);
  2472. }
  2473. slapi_pblock_destroy (pb);
  2474. slapi_ch_free_string(&dn);
  2475. slapi_ch_free_string(&data);
  2476. }
  2477. }
  2478. /*
  2479. * Just remove the dse.ldif config, but we need to keep the cleaned rids in memory until we know we are done
  2480. */
  2481. static void
  2482. delete_cleaned_rid_config(cleanruv_data *clean_data)
  2483. {
  2484. Slapi_PBlock *pb, *modpb;
  2485. Slapi_Entry **entries = NULL;
  2486. LDAPMod *mods[2];
  2487. LDAPMod mod;
  2488. struct berval *vals[5]= {0, 0, 0, 0, 0}; /* maximum of 4 tasks */
  2489. struct berval val[5];
  2490. char *iter = NULL;
  2491. char *dn = NULL;
  2492. int i, ii;
  2493. int rc = -1, ret, rid;
  2494. if(clean_data == NULL){
  2495. cleanruv_log(NULL, -1, CLEANALLRUV_ID, "delete_cleaned_rid_config: cleanruv data is NULL, "
  2496. "failed to clean the config.");
  2497. return;
  2498. }
  2499. /*
  2500. * Search the config for the exact attribute value to delete
  2501. */
  2502. pb = slapi_pblock_new();
  2503. if(clean_data->replica){
  2504. dn = replica_get_dn(clean_data->replica);
  2505. } else {
  2506. goto bail;
  2507. }
  2508. slapi_search_internal_set_pb(pb, dn, LDAP_SCOPE_SUBTREE, "nsds5ReplicaCleanRUV=*", NULL, 0, NULL, NULL,
  2509. (void *)plugin_get_default_component_id(), 0);
  2510. slapi_search_internal_pb(pb);
  2511. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2512. if (ret != LDAP_SUCCESS){
  2513. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2514. "delete_cleaned_rid_config: internal search failed(%d).",ret);
  2515. goto bail;
  2516. } else {
  2517. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2518. if (entries == NULL || entries[0] == NULL){
  2519. /*
  2520. * No matching entries!
  2521. */
  2522. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2523. "delete_cleaned_rid_config: failed to find any "
  2524. "entries with nsds5ReplicaCleanRUV under (%s)", dn);
  2525. goto bail;
  2526. } else {
  2527. /*
  2528. * Clean all the matching entries
  2529. */
  2530. for(i = 0; entries[i] != NULL;i++){
  2531. char **attr_val = slapi_entry_attr_get_charray(entries[i], type_replicaCleanRUV);
  2532. char *edn = slapi_entry_get_dn(entries[i]);
  2533. int count = 0;
  2534. for (ii = 0; attr_val && attr_val[ii] && i < 5; ii++){
  2535. /* make a copy to retain the full value after toking */
  2536. char *aval = slapi_ch_strdup(attr_val[ii]);
  2537. rid = atoi(ldap_utf8strtok_r(attr_val[ii], ":", &iter));
  2538. if(rid == clean_data->rid){
  2539. val[count].bv_len = strlen(aval);
  2540. val[count].bv_val = aval;
  2541. vals[count] = &val[count];
  2542. count++;
  2543. } else {
  2544. slapi_ch_free_string(&aval);
  2545. }
  2546. }
  2547. slapi_ch_array_free(attr_val);
  2548. /*
  2549. * Now delete the attribute
  2550. */
  2551. vals[4] = NULL;
  2552. mod.mod_op = LDAP_MOD_DELETE|LDAP_MOD_BVALUES;
  2553. mod.mod_type = (char *)type_replicaCleanRUV;
  2554. mod.mod_bvalues = vals;
  2555. mods[0] = &mod;
  2556. mods[1] = NULL;
  2557. modpb = slapi_pblock_new();
  2558. slapi_modify_internal_set_pb(modpb, edn, mods, NULL, NULL,
  2559. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2560. slapi_modify_internal_pb (modpb);
  2561. slapi_pblock_get(modpb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2562. slapi_pblock_destroy(modpb);
  2563. /* free the attr vals */
  2564. for (ii = 0; ii < count; ii++){
  2565. slapi_ch_free_string(&val[ii].bv_val);
  2566. }
  2567. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2568. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2569. "delete_cleaned_rid_config: failed to remove task data "
  2570. "from (%s) error (%d), rid (%d)", edn, rc, clean_data->rid);
  2571. goto bail;
  2572. }
  2573. }
  2574. }
  2575. }
  2576. bail:
  2577. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2578. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2579. "delete_cleaned_rid_config: failed to remove replica config "
  2580. "(%d), rid (%d)", rc, clean_data->rid);
  2581. }
  2582. slapi_free_search_results_internal(pb);
  2583. slapi_pblock_destroy(pb);
  2584. slapi_ch_free_string(&dn);
  2585. }
  2586. /*
  2587. * Remove the rid from our list, and the config
  2588. */
  2589. void
  2590. remove_cleaned_rid(ReplicaId rid)
  2591. {
  2592. int i;
  2593. /*
  2594. * Remove this rid, and optimize the array
  2595. */
  2596. slapi_rwlock_wrlock(rid_lock);
  2597. for(i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != rid; i++); /* found rid, stop */
  2598. for(; i < CLEANRIDSIZ; i++){
  2599. /* rewrite entire array */
  2600. cleaned_rids[i] = cleaned_rids[i + 1];
  2601. }
  2602. /* now do the preset cleaned rids */
  2603. for(i = 0; i < CLEANRIDSIZ && pre_cleaned_rids[i] != rid; i++); /* found rid, stop */
  2604. for(; i < CLEANRIDSIZ; i++){
  2605. /* rewrite entire array */
  2606. pre_cleaned_rids[i] = pre_cleaned_rids[i + 1];
  2607. }
  2608. slapi_rwlock_unlock(rid_lock);
  2609. }
  2610. /*
  2611. * Abort the CLEANALLRUV task
  2612. */
  2613. int
  2614. replica_cleanall_ruv_abort(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  2615. int *returncode, char *returntext, void *arg)
  2616. {
  2617. struct berval *payload = NULL;
  2618. cleanruv_data *data = NULL;
  2619. PRThread *thread = NULL;
  2620. Slapi_Task *task = NULL;
  2621. Slapi_DN *sdn = NULL;
  2622. Replica *replica;
  2623. ReplicaId rid = -1;
  2624. Object *r;
  2625. const char *certify_all;
  2626. const char *base_dn;
  2627. const char *rid_str;
  2628. char *ridstr = NULL;
  2629. int rc = SLAPI_DSE_CALLBACK_OK;
  2630. if(get_abort_cleanruv_task_count() >= CLEANRIDSIZ){
  2631. /* we are already running the maximum number of tasks */
  2632. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  2633. "Exceeded maximum number of active ABORT CLEANALLRUV tasks(%d)",
  2634. CLEANRIDSIZ);
  2635. cleanruv_log(task, -1, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2636. *returncode = LDAP_OPERATIONS_ERROR;
  2637. return SLAPI_DSE_CALLBACK_ERROR;
  2638. }
  2639. /* allocate new task now */
  2640. task = slapi_new_task(slapi_entry_get_ndn(e));
  2641. /* register our destructor for waiting the task is done */
  2642. slapi_task_set_destructor_fn(task, replica_cleanall_ruv_abort_destructor);
  2643. /*
  2644. * Get our task settings
  2645. */
  2646. if ((rid_str = fetch_attr(e, "replica-id", 0)) == NULL){
  2647. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing required attr \"replica-id\"");
  2648. cleanruv_log(task, -1, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2649. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2650. rc = SLAPI_DSE_CALLBACK_ERROR;
  2651. goto out;
  2652. }
  2653. certify_all = fetch_attr(e, "replica-certify-all", 0);
  2654. /*
  2655. * Check the rid
  2656. */
  2657. rid = atoi(rid_str);
  2658. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  2659. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)",
  2660. rid, slapi_sdn_get_dn(slapi_entry_get_sdn(e)));
  2661. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"%s", returntext);
  2662. *returncode = LDAP_OPERATIONS_ERROR;
  2663. rc = SLAPI_DSE_CALLBACK_ERROR;
  2664. goto out;
  2665. }
  2666. if ((base_dn = fetch_attr(e, "replica-base-dn", 0)) == NULL){
  2667. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing required attr \"replica-base-dn\"");
  2668. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2669. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2670. rc = SLAPI_DSE_CALLBACK_ERROR;
  2671. goto out;
  2672. }
  2673. if(!is_cleaned_rid(rid) && !is_pre_cleaned_rid(rid)){
  2674. /* we are not cleaning this rid */
  2675. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is not being cleaned, nothing to abort.", rid);
  2676. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2677. *returncode = LDAP_UNWILLING_TO_PERFORM;
  2678. rc = SLAPI_DSE_CALLBACK_ERROR;
  2679. goto out;
  2680. }
  2681. if(is_task_aborted(rid)){
  2682. /* we are already aborting this rid */
  2683. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is already being aborted", rid);
  2684. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2685. *returncode = LDAP_UNWILLING_TO_PERFORM;
  2686. rc = SLAPI_DSE_CALLBACK_ERROR;
  2687. goto out;
  2688. }
  2689. /*
  2690. * Get the replica object
  2691. */
  2692. sdn = slapi_sdn_new_dn_byval(base_dn);
  2693. if((r = replica_get_replica_from_dn(sdn)) == NULL){
  2694. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Failed to find replica from dn(%s)", base_dn);
  2695. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2696. *returncode = LDAP_OPERATIONS_ERROR;
  2697. rc = SLAPI_DSE_CALLBACK_ERROR;
  2698. goto out;
  2699. }
  2700. /*
  2701. * Check certify value
  2702. */
  2703. if(certify_all){
  2704. if(strcasecmp(certify_all,"yes") && strcasecmp(certify_all,"no")){
  2705. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid value for \"replica-certify-all\", the value "
  2706. "must be \"yes\" or \"no\".");
  2707. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2708. *returncode = LDAP_OPERATIONS_ERROR;
  2709. rc = SLAPI_DSE_CALLBACK_ERROR;
  2710. goto out;
  2711. }
  2712. } else {
  2713. /*
  2714. * The default should be not to certify all the replicas, because
  2715. * we might be trying to abort a clean task that is "hanging" due
  2716. * to unreachable replicas. If the default is "yes" then the abort
  2717. * task will run into the same issue.
  2718. */
  2719. certify_all = "no";
  2720. }
  2721. /*
  2722. * Create payload
  2723. */
  2724. ridstr = slapi_ch_smprintf("%d:%s:%s", rid, base_dn, certify_all);
  2725. payload = create_cleanruv_payload(ridstr);
  2726. if(payload == NULL){
  2727. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "Failed to create extended op payload, aborting task");
  2728. *returncode = LDAP_OPERATIONS_ERROR;
  2729. rc = SLAPI_DSE_CALLBACK_ERROR;
  2730. goto out;
  2731. }
  2732. /*
  2733. * Stop the cleaning, and delete the rid
  2734. */
  2735. replica = (Replica*)object_get_data (r);
  2736. add_aborted_rid(rid, replica, (char *)base_dn);
  2737. stop_ruv_cleaning();
  2738. /*
  2739. * Prepare the abort struct and fire off the thread
  2740. */
  2741. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  2742. if (data == NULL) {
  2743. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"Failed to allocate abort_cleanruv_data. Aborting task.");
  2744. *returncode = LDAP_OPERATIONS_ERROR;
  2745. rc = SLAPI_DSE_CALLBACK_ERROR;
  2746. goto out;
  2747. }
  2748. data->repl_obj = r; /* released in replica_abort_task_thread() */
  2749. data->replica = replica;
  2750. data->task = task;
  2751. data->payload = payload;
  2752. data->rid = rid;
  2753. data->repl_root = slapi_ch_strdup(base_dn);
  2754. data->sdn = NULL;
  2755. data->certify = slapi_ch_strdup(certify_all);
  2756. thread = PR_CreateThread(PR_USER_THREAD, replica_abort_task_thread,
  2757. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2758. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2759. if (thread == NULL) {
  2760. object_release(r);
  2761. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"Unable to create abort thread. Aborting task.");
  2762. *returncode = LDAP_OPERATIONS_ERROR;
  2763. slapi_ch_free_string(&data->certify);
  2764. rc = SLAPI_DSE_CALLBACK_ERROR;
  2765. }
  2766. out:
  2767. slapi_ch_free_string(&ridstr);
  2768. slapi_sdn_free(&sdn);
  2769. if(rc != SLAPI_DSE_CALLBACK_OK){
  2770. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "Abort Task failed (%d)", rc);
  2771. slapi_task_finish(task, rc);
  2772. }
  2773. return rc;
  2774. }
  2775. /*
  2776. * Abort CLEANALLRUV task thread
  2777. */
  2778. void
  2779. replica_abort_task_thread(void *arg)
  2780. {
  2781. cleanruv_data *data = (cleanruv_data *)arg;
  2782. Repl_Agmt *agmt;
  2783. Object *agmt_obj;
  2784. int agmt_not_notified = 1;
  2785. int interval = 10;
  2786. int release_it = 0;
  2787. int count = 0, rc = 0;
  2788. if (!data) {
  2789. return; /* no data */
  2790. }
  2791. if (data->task) {
  2792. slapi_task_inc_refcount(data->task);
  2793. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_abort_task_thread --> refcount incremented.\n");
  2794. }
  2795. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Aborting task for rid(%d)...",data->rid);
  2796. /*
  2797. * Need to build the replica from the dn
  2798. */
  2799. if(data->replica == NULL && data->repl_obj == NULL){
  2800. /*
  2801. * This thread was initiated at startup because the process did not finish. Due
  2802. * to timing issues, we need to wait to grab the replica obj until we get here.
  2803. */
  2804. if((data->repl_obj = replica_get_replica_from_dn(data->sdn)) == NULL){
  2805. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Failed to get replica object from dn (%s).", slapi_sdn_get_dn(data->sdn));
  2806. goto done;
  2807. }
  2808. if(data->replica == NULL && data->repl_obj){
  2809. data->replica = (Replica*)object_get_data(data->repl_obj);
  2810. }
  2811. release_it = 1;
  2812. }
  2813. /*
  2814. * Now send the abort cleanruv extended op to all the agreements
  2815. */
  2816. while(agmt_not_notified && !slapi_is_shutting_down()){
  2817. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  2818. if(agmt_obj == NULL){
  2819. agmt_not_notified = 0;
  2820. break;
  2821. }
  2822. while (agmt_obj && !slapi_is_shutting_down()){
  2823. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2824. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2825. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2826. agmt_not_notified = 0;
  2827. continue;
  2828. }
  2829. if(replica_cleanallruv_send_abort_extop(agmt, data->task, data->payload)){
  2830. if(strcasecmp(data->certify,"yes") == 0){
  2831. /* we are verifying all the replicas receive the abort task */
  2832. agmt_not_notified = 1;
  2833. break;
  2834. } else {
  2835. /* we do not care if we could not reach a replica, just continue as if we did */
  2836. agmt_not_notified = 0;
  2837. }
  2838. } else {
  2839. /* success */
  2840. agmt_not_notified = 0;
  2841. }
  2842. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2843. } /* while loop for agmts */
  2844. if(agmt_not_notified == 0){
  2845. /* everybody has been contacted */
  2846. break;
  2847. }
  2848. /*
  2849. * Need to sleep between passes. unless we are shutting down
  2850. */
  2851. if (!slapi_is_shutting_down()){
  2852. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Retrying in %d seconds",interval);
  2853. PR_Lock( notify_lock );
  2854. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2855. PR_Unlock( notify_lock );
  2856. }
  2857. if(interval < 14400){ /* 4 hour max */
  2858. interval = interval * 2;
  2859. } else {
  2860. interval = 14400;
  2861. }
  2862. } /* while */
  2863. done:
  2864. if(agmt_not_notified || slapi_is_shutting_down()){
  2865. /* failure */
  2866. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Abort task failed, will resume the task at the next server startup.");
  2867. } else {
  2868. /*
  2869. * Wait for this server to stop its cleanallruv task(which removes the rid from the cleaned list)
  2870. */
  2871. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Waiting for CleanAllRUV task to abort...");
  2872. while(is_cleaned_rid(data->rid) && !slapi_is_shutting_down()){
  2873. DS_Sleep(PR_SecondsToInterval(1));
  2874. count++;
  2875. if(count == 60){ /* it should not take this long */
  2876. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "CleanAllRUV task failed to abort. You might need to "
  2877. "rerun the task.");
  2878. rc = -1;
  2879. break;
  2880. }
  2881. }
  2882. /*
  2883. * Clean up the config
  2884. */
  2885. delete_aborted_rid(data->replica, data->rid, data->repl_root, 1); /* delete just the config, leave rid in memory */
  2886. if(strcasecmp(data->certify, "yes") == 0){
  2887. check_replicas_are_done_aborting(data);
  2888. }
  2889. delete_aborted_rid(data->replica, data->rid, data->repl_root, 0); /* remove the in-memory aborted rid */
  2890. if(rc == 0){
  2891. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Successfully aborted task for rid(%d)", data->rid);
  2892. } else {
  2893. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Failed to abort task for rid(%d)",data->rid);
  2894. }
  2895. }
  2896. if(data->task){
  2897. slapi_task_finish(data->task, agmt_not_notified);
  2898. slapi_task_dec_refcount(data->task);
  2899. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_abort_task_thread <-- refcount incremented.\n");
  2900. }
  2901. if(data->repl_obj && release_it)
  2902. object_release(data->repl_obj);
  2903. if(data->payload){
  2904. ber_bvfree(data->payload);
  2905. }
  2906. slapi_ch_free_string(&data->repl_root);
  2907. slapi_ch_free_string(&data->certify);
  2908. slapi_sdn_free(&data->sdn);
  2909. slapi_ch_free((void **)&data);
  2910. }
  2911. static int
  2912. replica_cleanallruv_send_abort_extop(Repl_Agmt *ra, Slapi_Task *task, struct berval *payload)
  2913. {
  2914. Repl_Connection *conn = NULL;
  2915. ConnResult crc = 0;
  2916. int msgid = 0;
  2917. int rc = -1;
  2918. if((conn = conn_new(ra)) == NULL){
  2919. return rc;
  2920. }
  2921. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2922. crc = conn_send_extended_operation(conn, REPL_ABORT_CLEANRUV_OID, payload, NULL, &msgid);
  2923. /*
  2924. * success or failure, just return the error code
  2925. */
  2926. rc = crc;
  2927. if(rc){
  2928. cleanruv_log(task, agmt_get_consumer_rid(ra, conn), ABORT_CLEANALLRUV_ID,
  2929. "Failed to send extop to replica(%s).", agmt_get_long_name(ra));
  2930. }
  2931. } else {
  2932. cleanruv_log(task, agmt_get_consumer_rid(ra, conn), ABORT_CLEANALLRUV_ID,
  2933. "Failed to connect to replica(%s).", agmt_get_long_name(ra));
  2934. rc = -1;
  2935. }
  2936. conn_delete_internal_ext(conn);
  2937. return rc;
  2938. }
  2939. static int
  2940. replica_cleanallruv_send_extop(Repl_Agmt *ra, cleanruv_data *clean_data, int check_result)
  2941. {
  2942. Repl_Connection *conn = NULL;
  2943. ConnResult crc = 0;
  2944. int msgid = 0;
  2945. int rc = -1;
  2946. if((conn = conn_new(ra)) == NULL){
  2947. return rc;
  2948. }
  2949. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2950. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_OID, clean_data->payload, NULL, &msgid);
  2951. if(crc == CONN_OPERATION_SUCCESS && check_result){
  2952. struct berval *retsdata = NULL;
  2953. char *retoid = NULL;
  2954. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  2955. if (CONN_OPERATION_SUCCESS == crc ){
  2956. char *response = NULL;
  2957. decode_cleanruv_payload(retsdata, &response);
  2958. if(response && strcmp(response,CLEANRUV_ACCEPTED) == 0){
  2959. /* extop was accepted */
  2960. rc = 0;
  2961. } else {
  2962. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,"Replica %s does not support the CLEANALLRUV task. "
  2963. "Sending replica CLEANRUV task...", slapi_sdn_get_dn(agmt_get_dn_byref(ra)));
  2964. /*
  2965. * Ok, this replica doesn't know about CLEANALLRUV, so just manually
  2966. * add the CLEANRUV task to the replica.
  2967. */
  2968. replica_send_cleanruv_task(ra, clean_data);
  2969. rc = 0;
  2970. }
  2971. if (NULL != retsdata)
  2972. ber_bvfree(retsdata);
  2973. slapi_ch_free_string(&retoid);
  2974. slapi_ch_free_string(&response);
  2975. }
  2976. } else {
  2977. /*
  2978. * success or failure, just return the error code
  2979. */
  2980. rc = crc;
  2981. }
  2982. }
  2983. conn_delete_internal_ext(conn);
  2984. return rc;
  2985. }
  2986. static CSN*
  2987. replica_cleanallruv_find_maxcsn(Replica *replica, ReplicaId rid, char *basedn)
  2988. {
  2989. Object *agmt_obj;
  2990. Repl_Agmt *agmt;
  2991. CSN *maxcsn = NULL, *topcsn = NULL;
  2992. char *rid_text = slapi_ch_smprintf("%d", rid);
  2993. char *csnstr = NULL;
  2994. /* start with the local maxcsn */
  2995. csnstr = replica_cleanallruv_get_local_maxcsn(rid, basedn);
  2996. if(csnstr){
  2997. topcsn = csn_new();
  2998. csn_init_by_string(topcsn, csnstr);
  2999. slapi_ch_free_string(&csnstr);
  3000. }
  3001. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  3002. if(agmt_obj == NULL){ /* no agreements */
  3003. goto done;
  3004. }
  3005. while (agmt_obj && !slapi_is_shutting_down()){
  3006. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  3007. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  3008. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  3009. continue;
  3010. }
  3011. if(replica_cleanallruv_get_replica_maxcsn(agmt, rid_text, basedn, &maxcsn) == 0){
  3012. if(maxcsn == NULL){
  3013. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  3014. continue;
  3015. }
  3016. if(topcsn == NULL){
  3017. topcsn = maxcsn;
  3018. } else {
  3019. if(csn_compare(topcsn, maxcsn) < 0){
  3020. csn_free(&topcsn);
  3021. topcsn = maxcsn;
  3022. } else {
  3023. csn_free(&maxcsn);
  3024. }
  3025. }
  3026. }
  3027. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  3028. }
  3029. done:
  3030. slapi_ch_free_string(&rid_text);
  3031. return topcsn;
  3032. }
  3033. static int
  3034. replica_cleanallruv_get_replica_maxcsn(Repl_Agmt *agmt, char *rid_text, char *basedn, CSN **csn)
  3035. {
  3036. Repl_Connection *conn = NULL;
  3037. ConnResult crc = -1;
  3038. struct berval *payload = NULL;
  3039. CSN *maxcsn = NULL;
  3040. char *data = NULL;
  3041. int msgid = 0;
  3042. if((conn = conn_new(agmt)) == NULL){
  3043. return -1;
  3044. }
  3045. data = slapi_ch_smprintf("%s:%s",rid_text, basedn);
  3046. payload = create_cleanruv_payload(data);
  3047. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3048. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  3049. if(crc == CONN_OPERATION_SUCCESS){
  3050. struct berval *retsdata = NULL;
  3051. char *retoid = NULL;
  3052. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3053. if (CONN_OPERATION_SUCCESS == crc ){
  3054. char *remote_maxcsn = NULL;
  3055. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3056. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3057. maxcsn = csn_new();
  3058. csn_init_by_string(maxcsn, remote_maxcsn);
  3059. *csn = maxcsn;
  3060. } else {
  3061. /* no csn */
  3062. *csn = NULL;
  3063. }
  3064. slapi_ch_free_string(&retoid);
  3065. slapi_ch_free_string(&remote_maxcsn);
  3066. if (NULL != retsdata)
  3067. ber_bvfree(retsdata);
  3068. }
  3069. }
  3070. }
  3071. conn_delete_internal_ext(conn);
  3072. slapi_ch_free_string(&data);
  3073. if(payload)
  3074. ber_bvfree(payload);
  3075. return (int)crc;
  3076. }
  3077. static int
  3078. replica_cleanallruv_check_maxcsn(Repl_Agmt *agmt, char *basedn, char *rid_text, char *maxcsn, Slapi_Task *task)
  3079. {
  3080. Repl_Connection *conn = NULL;
  3081. ConnResult crc = 0;
  3082. struct berval *payload = NULL;
  3083. char *data = NULL;
  3084. int msgid = 0;
  3085. int rc = -1;
  3086. if((conn = conn_new(agmt)) == NULL){
  3087. return -1;
  3088. }
  3089. data = slapi_ch_smprintf("%s:%s",rid_text, basedn);
  3090. payload = create_cleanruv_payload(data);
  3091. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3092. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  3093. if(crc == CONN_OPERATION_SUCCESS){
  3094. struct berval *retsdata = NULL;
  3095. char *retoid = NULL;
  3096. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3097. if (CONN_OPERATION_SUCCESS == crc ){
  3098. char *remote_maxcsn = NULL;
  3099. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3100. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3101. CSN *max, *repl_max;
  3102. max = csn_new();
  3103. repl_max = csn_new();
  3104. csn_init_by_string(max, maxcsn);
  3105. csn_init_by_string(repl_max, remote_maxcsn);
  3106. if(csn_compare (repl_max, max) < 0){
  3107. /* we are not caught up yet, free, and return */
  3108. cleanruv_log(task, atoi(rid_text), CLEANALLRUV_ID,"Replica maxcsn (%s) is not caught up with deleted replica's maxcsn(%s)",
  3109. remote_maxcsn, maxcsn);
  3110. rc = -1;
  3111. } else {
  3112. /* ok this replica is caught up */
  3113. rc = 0;
  3114. }
  3115. csn_free(&max);
  3116. csn_free(&repl_max);
  3117. } else {
  3118. /* no remote_maxcsn - return success */
  3119. rc = 0;
  3120. }
  3121. slapi_ch_free_string(&retoid);
  3122. slapi_ch_free_string(&remote_maxcsn);
  3123. if (NULL != retsdata)
  3124. ber_bvfree(retsdata);
  3125. }
  3126. }
  3127. }
  3128. conn_delete_internal_ext(conn);
  3129. slapi_ch_free_string(&data);
  3130. if(payload)
  3131. ber_bvfree(payload);
  3132. return rc;
  3133. }
  3134. static int
  3135. replica_cleanallruv_replica_alive(Repl_Agmt *agmt)
  3136. {
  3137. Repl_Connection *conn = NULL;
  3138. LDAP *ld = NULL;
  3139. LDAPMessage *result = NULL;
  3140. int rc = -1;
  3141. if((conn = conn_new(agmt)) == NULL){
  3142. return rc;
  3143. }
  3144. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3145. ld = conn_get_ldap(conn);
  3146. if(ld == NULL){
  3147. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "CleanAllRUV_task: failed to get LDAP "
  3148. "handle from the replication agmt (%s). Moving on to the next agmt.\n",agmt_get_long_name(agmt));
  3149. conn_delete_internal_ext(conn);
  3150. return -1;
  3151. }
  3152. if(ldap_search_ext_s(ld, "", LDAP_SCOPE_BASE, "objectclass=top",
  3153. NULL, 0, NULL, NULL, NULL, 0, &result) == LDAP_SUCCESS)
  3154. {
  3155. rc = 0;
  3156. } else {
  3157. rc = -1;
  3158. }
  3159. if(result)
  3160. ldap_msgfree( result );
  3161. }
  3162. conn_delete_internal_ext(conn);
  3163. return rc;
  3164. }
  3165. static int
  3166. replica_cleanallruv_check_ruv(char *repl_root, Repl_Agmt *agmt, char *rid_text, Slapi_Task *task, char *force)
  3167. {
  3168. Repl_Connection *conn = NULL;
  3169. ConnResult crc = 0;
  3170. struct berval *payload = NULL;
  3171. char *data = NULL;
  3172. int msgid = 0;
  3173. int rc = -1;
  3174. if((conn = conn_new(agmt)) == NULL){
  3175. if(strcasecmp(force, "yes") == 0){
  3176. return 0;
  3177. }
  3178. return rc;
  3179. }
  3180. data = slapi_ch_smprintf("%s:%s",rid_text, repl_root);
  3181. payload = create_cleanruv_payload(data);
  3182. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3183. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  3184. if(crc == CONN_OPERATION_SUCCESS){
  3185. struct berval *retsdata = NULL;
  3186. char *retoid = NULL;
  3187. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3188. if (CONN_OPERATION_SUCCESS == crc ){
  3189. char *remote_maxcsn = NULL;
  3190. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3191. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3192. /* remote replica still has dirty RUV element */
  3193. rc = -1;
  3194. } else {
  3195. /* no maxcsn = we're clean */
  3196. rc = 0;
  3197. }
  3198. slapi_ch_free_string(&retoid);
  3199. slapi_ch_free_string(&remote_maxcsn);
  3200. if (NULL != retsdata)
  3201. ber_bvfree(retsdata);
  3202. }
  3203. }
  3204. } else {
  3205. if (strcasecmp(force, "yes") == 0){
  3206. /* We are forcing, we don't care that the replica is not online */
  3207. rc = 0;
  3208. }
  3209. }
  3210. conn_delete_internal_ext(conn);
  3211. slapi_ch_free_string(&data);
  3212. if(payload)
  3213. ber_bvfree(payload);
  3214. return rc;
  3215. }
  3216. static int
  3217. get_cleanruv_task_count()
  3218. {
  3219. int i, count = 0;
  3220. slapi_rwlock_wrlock(rid_lock);
  3221. for(i = 0; i < CLEANRIDSIZ; i++){
  3222. if(pre_cleaned_rids[i] != 0){
  3223. count++;
  3224. }
  3225. }
  3226. slapi_rwlock_unlock(rid_lock);
  3227. return count;
  3228. }
  3229. static int
  3230. get_abort_cleanruv_task_count()
  3231. {
  3232. int i, count = 0;
  3233. slapi_rwlock_wrlock(rid_lock);
  3234. for(i = 0; i < CLEANRIDSIZ; i++){
  3235. if(aborted_rids[i] != 0){
  3236. count++;
  3237. }
  3238. }
  3239. slapi_rwlock_unlock(rid_lock);
  3240. return count;
  3241. }
  3242. /*
  3243. * Notify sleeping CLEANALLRUV threads to stop
  3244. */
  3245. void
  3246. stop_ruv_cleaning()
  3247. {
  3248. if(notify_lock){
  3249. PR_Lock( notify_lock );
  3250. PR_NotifyCondVar( notify_cvar );
  3251. PR_Unlock( notify_lock );
  3252. }
  3253. }
  3254. /*
  3255. * Write our logging to the task and error log
  3256. */
  3257. void
  3258. cleanruv_log(Slapi_Task *task, int rid, char *task_type, char *fmt, ...)
  3259. {
  3260. va_list ap1;
  3261. va_list ap2;
  3262. va_list ap3;
  3263. va_list ap4;
  3264. char *errlog_fmt;
  3265. va_start(ap1, fmt);
  3266. va_start(ap2, fmt);
  3267. va_start(ap3, fmt);
  3268. va_start(ap4, fmt);
  3269. if(task){
  3270. slapi_task_log_notice_ext(task, fmt, ap1);
  3271. slapi_task_log_status_ext(task, fmt, ap2);
  3272. slapi_task_inc_progress(task);
  3273. }
  3274. errlog_fmt = PR_smprintf("%s (rid %d): %s \n",task_type, rid, fmt);
  3275. slapi_log_error_ext(SLAPI_LOG_FATAL, repl_plugin_name, errlog_fmt, ap3, ap4);
  3276. slapi_ch_free_string(&errlog_fmt);
  3277. va_end(ap1);
  3278. va_end(ap2);
  3279. va_end(ap3);
  3280. va_end(ap4);
  3281. }
  3282. char *
  3283. replica_cleanallruv_get_local_maxcsn(ReplicaId rid, char *base_dn)
  3284. {
  3285. Slapi_PBlock *search_pb = NULL;
  3286. Slapi_Entry **entries = NULL;
  3287. char **ruv_elements = NULL;
  3288. char *maxcsn = NULL;
  3289. char *filter = NULL;
  3290. char *ridstr = NULL;
  3291. char *iter = NULL;
  3292. char *attrs[2];
  3293. char *ruv_part = NULL;
  3294. int part_count = 0;
  3295. int res, i;
  3296. /*
  3297. * Get the maxruv from the database tombstone entry
  3298. */
  3299. filter = "(&(nsuniqueid=ffffffff-ffffffff-ffffffff-ffffffff)(objectclass=nstombstone))";
  3300. attrs[0] = "nsds50ruv";
  3301. attrs[1] = NULL;
  3302. ridstr = slapi_ch_smprintf("{replica %d ldap", rid);
  3303. search_pb = slapi_pblock_new();
  3304. slapi_search_internal_set_pb(search_pb, base_dn, LDAP_SCOPE_SUBTREE, filter, attrs, 0, NULL, NULL, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  3305. slapi_search_internal_pb (search_pb);
  3306. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  3307. if ( LDAP_SUCCESS == res ) {
  3308. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  3309. if (NULL == entries || entries[0] == NULL) {
  3310. /* Hmmm, no tombstone! Error out */
  3311. } else {
  3312. /* find the right ruv element, and find the maxcsn */
  3313. ruv_elements = slapi_entry_attr_get_charray(entries[0],attrs[0]);
  3314. for(i = 0; ruv_elements && ruv_elements[i] ; i++){
  3315. if(strstr(ruv_elements[i], ridstr)){
  3316. /* get the max csn */
  3317. ruv_part = ldap_utf8strtok_r(ruv_elements[i], " ", &iter);
  3318. for(part_count = 1; ruv_part && part_count < 5; part_count++){
  3319. ruv_part = ldap_utf8strtok_r(iter, " ", &iter);
  3320. }
  3321. if(part_count == 5 && ruv_part){/* we have the maxcsn */
  3322. maxcsn = slapi_ch_strdup(ruv_part);
  3323. break;
  3324. }
  3325. }
  3326. }
  3327. slapi_ch_array_free(ruv_elements);
  3328. }
  3329. } else {
  3330. /* internal search failed */
  3331. cleanruv_log(NULL, (int)rid, CLEANALLRUV_ID, "replica_cleanallruv_get_local_maxcsn: internal search failed (%d)\n", res);
  3332. }
  3333. slapi_free_search_results_internal(search_pb);
  3334. slapi_pblock_destroy(search_pb);
  3335. slapi_ch_free_string(&ridstr);
  3336. return maxcsn;
  3337. }