repl5_replica_config.c 128 KB

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