repl5_replica_config.c 124 KB

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