repl5_replica_config.c 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710
  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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  517. break;
  518. }
  519. replica_set_backoff_max(r, val);
  520. }
  521. }
  522. else if (strcasecmp (config_attr, type_replicaPrecisePurge) == 0 )
  523. {
  524. if (apply_mods)
  525. {
  526. if (config_attr_value[0]) {
  527. PRUint64 on_off = 0;
  528. if (strcasecmp(config_attr_value, "on") == 0){
  529. on_off = 1;
  530. } else if (strcasecmp(config_attr_value, "off") == 0){
  531. on_off = 0;
  532. } else{
  533. /* Invalid value */
  534. *returncode = LDAP_UNWILLING_TO_PERFORM;
  535. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  536. "Invalid value for %s: %s Value should be \"on\" or \"off\"\n",
  537. type_replicaPrecisePurge, config_attr_value);
  538. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  539. "Invalid value for %s: %s Value should be \"on\" or \"off\")\n",
  540. type_replicaPrecisePurge, config_attr_value);
  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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "replica_config_delete: "
  740. "Warning: 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1135. "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_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1140. "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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1198. "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_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1204. "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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "replica_config_add: "
  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_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "replica_config_add: "
  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_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name, "cleanruv_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_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name, "cleanruv_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_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "cleanruv_task: task failed(%d)\n",rc);
  1313. return LDAP_OPERATIONS_ERROR;
  1314. }
  1315. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name, "cleanruv_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_error(SLAPI_LOG_FATAL, 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, "%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, "%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, "%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, "%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, "%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, "%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, "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,"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,
  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, "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, "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, "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, "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, "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, "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, "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_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, 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, "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, "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, "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, "Waiting to process all the updates from the deleted replica...");
  1642. ruv_obj = replica_get_ruv(data->replica);
  1643. ruv = object_get_data (ruv_obj);
  1644. while(data->maxcsn && !is_task_aborted(data->rid) && !is_cleaned_rid(data->rid) && !slapi_is_shutting_down()){
  1645. if(csn_get_replicaid(data->maxcsn) == 0 ||
  1646. ruv_covers_csn_cleanallruv(ruv,data->maxcsn) ||
  1647. strcasecmp(data->force,"yes") == 0)
  1648. {
  1649. /* We are caught up, now we can clean the ruv's */
  1650. break;
  1651. }
  1652. PR_Lock( notify_lock );
  1653. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(5) );
  1654. PR_Unlock( notify_lock );
  1655. }
  1656. object_release(ruv_obj);
  1657. /*
  1658. * Next, make sure all the replicas are up and running before sending off the clean ruv tasks
  1659. *
  1660. * Even if we are forcing the cleaning, the replicas still need to be up
  1661. */
  1662. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Waiting for all the replicas to be online...");
  1663. if(strcasecmp(data->force, "no") == 0 && check_agmts_are_alive(data->replica, data->rid, data->task)){
  1664. /* error, aborted or shutdown */
  1665. aborted = 1;
  1666. goto done;
  1667. }
  1668. /*
  1669. * Make sure all the replicas have seen the max csn
  1670. */
  1671. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Waiting for all the replicas to receive all the deleted replica updates...");
  1672. if(strcasecmp(data->force,"no") == 0 && check_agmts_are_caught_up(data, csnstr)){
  1673. /* error, aborted or shutdown */
  1674. aborted = 1;
  1675. goto done;
  1676. }
  1677. /*
  1678. * Set the rid as notified - this blocks the changelog from sending out updates
  1679. * during this process, as well as prevents the db ruv from getting polluted.
  1680. */
  1681. set_cleaned_rid(data->rid);
  1682. /*
  1683. * Now send the cleanruv extended op to all the agreements
  1684. */
  1685. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Sending cleanAllRUV task to all the replicas...");
  1686. while(agmt_not_notified && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1687. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1688. if(agmt_obj == NULL){
  1689. /* no agmts, just clean this replica */
  1690. break;
  1691. }
  1692. while (agmt_obj && !slapi_is_shutting_down()){
  1693. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1694. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1695. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1696. agmt_not_notified = 0;
  1697. continue;
  1698. }
  1699. if(replica_cleanallruv_send_extop(agmt, data, 1) == 0){
  1700. agmt_not_notified = 0;
  1701. } else {
  1702. agmt_not_notified = 1;
  1703. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Failed to send task to replica (%s)",agmt_get_long_name(agmt));
  1704. if(strcasecmp(data->force,"no") == 0){
  1705. break;
  1706. }
  1707. }
  1708. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1709. }
  1710. if(is_task_aborted(data->rid)){
  1711. aborted = 1;
  1712. goto done;
  1713. }
  1714. if(agmt_not_notified == 0 || strcasecmp(data->force, "yes") == 0){
  1715. break;
  1716. }
  1717. /*
  1718. * need to sleep between passes
  1719. */
  1720. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Not all replicas have received the "
  1721. "cleanallruv extended op, retrying in %d seconds",interval);
  1722. if(!slapi_is_shutting_down()){
  1723. PR_Lock( notify_lock );
  1724. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1725. PR_Unlock( notify_lock );
  1726. }
  1727. if(interval < 14400){ /* 4 hour max */
  1728. interval = interval * 2;
  1729. } else {
  1730. interval = 14400;
  1731. }
  1732. }
  1733. /*
  1734. * Run the CLEANRUV task
  1735. */
  1736. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Cleaning local ruv's...");
  1737. replica_execute_cleanruv_task (data->repl_obj, data->rid, returntext);
  1738. /*
  1739. * Wait for all the replicas to be cleaned
  1740. */
  1741. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  1742. "Waiting for all the replicas to be cleaned...");
  1743. interval = 10;
  1744. while(found_dirty_rid && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1745. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1746. if(agmt_obj == NULL){
  1747. break;
  1748. }
  1749. while (agmt_obj && !slapi_is_shutting_down()){
  1750. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1751. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1752. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1753. found_dirty_rid = 0;
  1754. continue;
  1755. }
  1756. if(replica_cleanallruv_check_ruv(data->repl_root, agmt, rid_text, data->task, data->force) == 0){
  1757. found_dirty_rid = 0;
  1758. } else {
  1759. found_dirty_rid = 1;
  1760. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Replica is not cleaned yet (%s)",
  1761. agmt_get_long_name(agmt));
  1762. break;
  1763. }
  1764. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1765. }
  1766. /* If the task is abort or everyone is cleaned, break out */
  1767. if(is_task_aborted(data->rid)){
  1768. aborted = 1;
  1769. goto done;
  1770. }
  1771. if(found_dirty_rid == 0 || strcasecmp(data->force, "yes") == 0){
  1772. break;
  1773. }
  1774. /*
  1775. * Need to sleep between passes unless we are shutting down
  1776. */
  1777. if (!slapi_is_shutting_down()){
  1778. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Replicas have not been cleaned yet, "
  1779. "retrying in %d seconds", interval);
  1780. PR_Lock( notify_lock );
  1781. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1782. PR_Unlock( notify_lock );
  1783. }
  1784. if(interval < 14400){ /* 4 hour max */
  1785. interval = interval * 2;
  1786. } else {
  1787. interval = 14400;
  1788. }
  1789. } /* while */
  1790. done:
  1791. /*
  1792. * If the replicas are cleaned, release the rid
  1793. */
  1794. if(!aborted && !slapi_is_shutting_down()){
  1795. /*
  1796. * Success - the rid has been cleaned!
  1797. *
  1798. * Delete the cleaned rid config.
  1799. * Make sure all the replicas have been "pre_cleaned"
  1800. * Remove the keep alive entry if present
  1801. * Clean the agreements' RUV
  1802. * Remove the rid from the internal clean list
  1803. */
  1804. delete_cleaned_rid_config(data);
  1805. check_replicas_are_done_cleaning(data);
  1806. remove_keep_alive_entry(data->task, data->rid, data->repl_root);
  1807. clean_agmts(data);
  1808. remove_cleaned_rid(data->rid);
  1809. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Successfully cleaned rid(%d).", data->rid);
  1810. } else {
  1811. /*
  1812. * Shutdown or abort
  1813. */
  1814. if(!is_task_aborted(data->rid) || slapi_is_shutting_down()){
  1815. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Server shutting down. Process will resume at server startup");
  1816. } else {
  1817. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Task aborted for rid(%d).",data->rid);
  1818. delete_cleaned_rid_config(data);
  1819. remove_cleaned_rid(data->rid);
  1820. }
  1821. }
  1822. if(data->task){
  1823. slapi_task_finish(data->task, rc);
  1824. slapi_task_dec_refcount(data->task);
  1825. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, repl_plugin_name, "replica_cleanallruv_thread <-- refcount decremented.\n");
  1826. }
  1827. if(data->payload){
  1828. ber_bvfree(data->payload);
  1829. }
  1830. if(data->repl_obj && free_obj){
  1831. object_release(data->repl_obj);
  1832. }
  1833. csn_free(&data->maxcsn);
  1834. slapi_sdn_free(&data->sdn);
  1835. slapi_ch_free_string(&data->repl_root);
  1836. slapi_ch_free_string(&data->force);
  1837. slapi_ch_free_string(&rid_text);
  1838. slapi_ch_free((void **)&data);
  1839. }
  1840. /*
  1841. * Clean the RUV attributes from all the agreements
  1842. */
  1843. static void
  1844. clean_agmts(cleanruv_data *data)
  1845. {
  1846. Object *agmt_obj = NULL;
  1847. Repl_Agmt *agmt = NULL;
  1848. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1849. if(agmt_obj == NULL){
  1850. return;
  1851. }
  1852. while (agmt_obj && !slapi_is_shutting_down()){
  1853. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1854. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1855. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1856. continue;
  1857. }
  1858. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Cleaning agmt...");
  1859. agmt_stop(agmt);
  1860. agmt_update_consumer_ruv(agmt);
  1861. agmt_start(agmt);
  1862. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1863. }
  1864. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Cleaned replication agreements.");
  1865. }
  1866. /*
  1867. * Remove the "Keep-Alive" replication entry.
  1868. */
  1869. static void
  1870. remove_keep_alive_entry(Slapi_Task *task, ReplicaId rid, const char *repl_root)
  1871. {
  1872. Slapi_PBlock *delete_pb = NULL;
  1873. char *keep_alive_dn = NULL;
  1874. int rc = 0;
  1875. /* Construct the repl keep alive dn from the rid and replication suffix */
  1876. keep_alive_dn = PR_smprintf("cn=repl keep alive %d,%s", (int)rid, repl_root);
  1877. delete_pb = slapi_pblock_new();
  1878. slapi_delete_internal_set_pb(delete_pb, keep_alive_dn ,NULL, NULL,
  1879. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  1880. slapi_delete_internal_pb(delete_pb);
  1881. slapi_pblock_get(delete_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1882. if (rc == LDAP_NO_SUCH_OBJECT) {
  1883. /* No problem, it's not always there */
  1884. cleanruv_log(task, rid, CLEANALLRUV_ID, "No Keep-Alive entry to remove (%s)",
  1885. keep_alive_dn);
  1886. } else if (rc != LDAP_SUCCESS){
  1887. /* Failed to delete the entry */
  1888. cleanruv_log(task, rid, CLEANALLRUV_ID, "Failed to delete Keep-Alive entry (%s) "
  1889. "Error (%d) This entry will need to be manually removed",
  1890. keep_alive_dn, rc);
  1891. } else {
  1892. /* Success */
  1893. cleanruv_log(task, rid, CLEANALLRUV_ID, "Removed Keep-Alive entry (%s)", keep_alive_dn);
  1894. }
  1895. slapi_pblock_destroy(delete_pb);
  1896. slapi_ch_free_string(&keep_alive_dn);
  1897. }
  1898. static void
  1899. replica_cleanall_ruv_destructor(Slapi_Task *task)
  1900. {
  1901. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, repl_plugin_name,
  1902. "replica_cleanall_ruv_destructor -->\n" );
  1903. stop_ruv_cleaning();
  1904. if (task) {
  1905. while (slapi_task_get_refcount(task) > 0) {
  1906. /* Yield to wait for the fixup task finishes. */
  1907. DS_Sleep (PR_MillisecondsToInterval(100));
  1908. }
  1909. }
  1910. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, repl_plugin_name,
  1911. "replica_cleanall_ruv_destructor <--\n" );
  1912. }
  1913. static void
  1914. replica_cleanall_ruv_abort_destructor(Slapi_Task *task)
  1915. {
  1916. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, repl_plugin_name,
  1917. "replica_cleanall_ruv_abort_destructor -->\n" );
  1918. stop_ruv_cleaning();
  1919. if (task) {
  1920. while (slapi_task_get_refcount(task) > 0) {
  1921. /* Yield to wait for the fixup task finishes. */
  1922. DS_Sleep (PR_MillisecondsToInterval(100));
  1923. }
  1924. }
  1925. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, repl_plugin_name,
  1926. "replica_cleanall_ruv_abort_destructor <--\n" );
  1927. }
  1928. /*
  1929. * Loop over the agmts, and check if they are in the last phase of cleaning, meaning they have
  1930. * released cleanallruv data from the config
  1931. */
  1932. static void
  1933. check_replicas_are_done_cleaning(cleanruv_data *data )
  1934. {
  1935. Object *agmt_obj;
  1936. Repl_Agmt *agmt;
  1937. char csnstr[CSN_STRSIZE];
  1938. char *filter = NULL;
  1939. int not_all_cleaned = 1;
  1940. int interval = 10;
  1941. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  1942. "Waiting for all the replicas to finish cleaning...");
  1943. csn_as_string(data->maxcsn, PR_FALSE, csnstr);
  1944. filter = PR_smprintf("(%s=%d:%s:%s)", type_replicaCleanRUV,(int)data->rid, csnstr, data->force);
  1945. while(not_all_cleaned && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1946. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1947. if(agmt_obj == NULL){
  1948. not_all_cleaned = 0;
  1949. break;
  1950. }
  1951. while (agmt_obj && !slapi_is_shutting_down()){
  1952. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1953. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1954. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1955. not_all_cleaned = 0;
  1956. continue;
  1957. }
  1958. if(replica_cleanallruv_is_finished(agmt, filter, data->task) == 0){
  1959. not_all_cleaned = 0;
  1960. } else {
  1961. not_all_cleaned = 1;
  1962. break;
  1963. }
  1964. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1965. }
  1966. if(not_all_cleaned == 0 ||
  1967. is_task_aborted(data->rid) ||
  1968. strcasecmp(data->force, "yes") == 0)
  1969. {
  1970. break;
  1971. }
  1972. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  1973. "Not all replicas finished cleaning, retrying in %d seconds",
  1974. interval);
  1975. if(!slapi_is_shutting_down()){
  1976. PR_Lock( notify_lock );
  1977. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1978. PR_Unlock( notify_lock );
  1979. }
  1980. if(interval < 14400){ /* 4 hour max */
  1981. interval = interval * 2;
  1982. } else {
  1983. interval = 14400;
  1984. }
  1985. }
  1986. slapi_ch_free_string(&filter);
  1987. }
  1988. /*
  1989. * Search this replica for the nsds5ReplicaCleanruv attribute, we don't return
  1990. * an entry, we know it has finished cleaning.
  1991. */
  1992. static int
  1993. replica_cleanallruv_is_finished(Repl_Agmt *agmt, char *filter, Slapi_Task *task)
  1994. {
  1995. Repl_Connection *conn = NULL;
  1996. ConnResult crc = 0;
  1997. struct berval *payload = NULL;
  1998. int msgid = 0;
  1999. int rc = -1;
  2000. if((conn = conn_new(agmt)) == NULL){
  2001. return rc;
  2002. }
  2003. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2004. payload = create_cleanruv_payload(filter);
  2005. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_CHECK_STATUS_OID, payload, NULL, &msgid);
  2006. if(crc == CONN_OPERATION_SUCCESS){
  2007. struct berval *retsdata = NULL;
  2008. char *retoid = NULL;
  2009. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  2010. if (CONN_OPERATION_SUCCESS == crc ){
  2011. char *response = NULL;
  2012. decode_cleanruv_payload(retsdata, &response);
  2013. if(response == NULL){
  2014. /* this replica does not support cleanallruv */
  2015. rc = 0;
  2016. } else if(strcmp(response,CLEANRUV_FINISHED) == 0){
  2017. /* finished cleaning */
  2018. rc = 0;
  2019. }
  2020. if (NULL != retsdata)
  2021. ber_bvfree(retsdata);
  2022. slapi_ch_free_string(&response);
  2023. slapi_ch_free_string(&retoid);
  2024. }
  2025. }
  2026. }
  2027. conn_delete_internal_ext(conn);
  2028. if(payload)
  2029. ber_bvfree(payload);
  2030. return rc;
  2031. }
  2032. /*
  2033. * Loop over the agmts, and check if they are in the last phase of aborting, meaning they have
  2034. * released the abort cleanallruv data from the config
  2035. */
  2036. static void
  2037. check_replicas_are_done_aborting(cleanruv_data *data )
  2038. {
  2039. Object *agmt_obj;
  2040. Repl_Agmt *agmt;
  2041. char *filter = NULL;
  2042. int not_all_aborted = 1;
  2043. int interval = 10;
  2044. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Waiting for all the replicas to finish aborting...");
  2045. filter = PR_smprintf("(%s=%d:%s)", type_replicaAbortCleanRUV, data->rid, data->repl_root);
  2046. while(not_all_aborted && !slapi_is_shutting_down()){
  2047. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  2048. if(agmt_obj == NULL){
  2049. not_all_aborted = 0;
  2050. break;
  2051. }
  2052. while (agmt_obj && !slapi_is_shutting_down()){
  2053. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2054. if(get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2055. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2056. not_all_aborted = 0;
  2057. continue;
  2058. }
  2059. if(replica_cleanallruv_is_finished(agmt, filter, data->task) == 0){
  2060. not_all_aborted = 0;
  2061. } else {
  2062. not_all_aborted = 1;
  2063. break;
  2064. }
  2065. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2066. }
  2067. if(not_all_aborted == 0){
  2068. break;
  2069. }
  2070. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Not all replicas finished aborting, retrying in %d seconds",interval);
  2071. if(!slapi_is_shutting_down()){
  2072. PR_Lock( notify_lock );
  2073. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2074. PR_Unlock( notify_lock );
  2075. }
  2076. if(interval < 14400){ /* 4 hour max */
  2077. interval = interval * 2;
  2078. } else {
  2079. interval = 14400;
  2080. }
  2081. }
  2082. slapi_ch_free_string(&filter);
  2083. }
  2084. /*
  2085. * Waits for all the repl agmts to be have have the maxcsn. Returns error only on abort or shutdown
  2086. */
  2087. static int
  2088. check_agmts_are_caught_up(cleanruv_data *data, char *maxcsn)
  2089. {
  2090. Object *agmt_obj;
  2091. Repl_Agmt *agmt;
  2092. char *rid_text;
  2093. int not_all_caughtup = 1;
  2094. int interval = 10;
  2095. rid_text = slapi_ch_smprintf("%d", data->rid);
  2096. while(not_all_caughtup && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  2097. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  2098. if(agmt_obj == NULL){
  2099. not_all_caughtup = 0;
  2100. break;
  2101. }
  2102. while (agmt_obj && !slapi_is_shutting_down()){
  2103. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2104. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2105. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2106. not_all_caughtup = 0;
  2107. continue;
  2108. }
  2109. if(replica_cleanallruv_check_maxcsn(agmt, data->repl_root, rid_text, maxcsn, data->task) == 0){
  2110. not_all_caughtup = 0;
  2111. } else {
  2112. not_all_caughtup = 1;
  2113. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Replica not caught up (%s)",agmt_get_long_name(agmt));
  2114. break;
  2115. }
  2116. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2117. }
  2118. if(not_all_caughtup == 0 || is_task_aborted(data->rid) ){
  2119. break;
  2120. }
  2121. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  2122. "Not all replicas caught up, retrying in %d seconds",interval);
  2123. if(!slapi_is_shutting_down()){
  2124. PR_Lock( notify_lock );
  2125. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2126. PR_Unlock( notify_lock );
  2127. }
  2128. if(interval < 14400){ /* 4 hour max */
  2129. interval = interval * 2;
  2130. } else {
  2131. interval = 14400;
  2132. }
  2133. }
  2134. slapi_ch_free_string(&rid_text);
  2135. if(is_task_aborted(data->rid)){
  2136. return -1;
  2137. }
  2138. return not_all_caughtup;
  2139. }
  2140. /*
  2141. * Waits for all the repl agmts to be online. Returns error only on abort or shutdown
  2142. */
  2143. static int
  2144. check_agmts_are_alive(Replica *replica, ReplicaId rid, Slapi_Task *task)
  2145. {
  2146. Object *agmt_obj;
  2147. Repl_Agmt *agmt;
  2148. int not_all_alive = 1;
  2149. int interval = 10;
  2150. while(not_all_alive && is_task_aborted(rid) == 0 && !slapi_is_shutting_down()){
  2151. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  2152. if(agmt_obj == NULL){
  2153. not_all_alive = 0;
  2154. break;
  2155. }
  2156. while (agmt_obj && !slapi_is_shutting_down()){
  2157. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2158. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2159. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2160. not_all_alive = 0;
  2161. continue;
  2162. }
  2163. if(replica_cleanallruv_replica_alive(agmt) == 0){
  2164. not_all_alive = 0;
  2165. } else {
  2166. not_all_alive = 1;
  2167. cleanruv_log(task, rid, CLEANALLRUV_ID, "Replica not online (%s)",
  2168. agmt_get_long_name(agmt));
  2169. break;
  2170. }
  2171. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2172. }
  2173. if(not_all_alive == 0 || is_task_aborted(rid)){
  2174. break;
  2175. }
  2176. cleanruv_log(task, rid, CLEANALLRUV_ID, "Not all replicas online, retrying in %d seconds...",
  2177. interval);
  2178. if(!slapi_is_shutting_down()){
  2179. PR_Lock( notify_lock );
  2180. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2181. PR_Unlock( notify_lock );
  2182. }
  2183. if(interval < 14400){ /* 4 hour max */
  2184. interval = interval * 2;
  2185. } else {
  2186. interval = 14400;
  2187. }
  2188. }
  2189. if(is_task_aborted(rid)){
  2190. return -1;
  2191. }
  2192. return not_all_alive;
  2193. }
  2194. /*
  2195. * Create the CLEANALLRUV extended op payload
  2196. */
  2197. struct berval *
  2198. create_cleanruv_payload(char *value)
  2199. {
  2200. struct berval *req_data = NULL;
  2201. BerElement *tmp_bere = NULL;
  2202. if ((tmp_bere = der_alloc()) == NULL){
  2203. goto error;
  2204. }
  2205. if (ber_printf(tmp_bere, "{s}", value) == -1){
  2206. goto error;
  2207. }
  2208. if (ber_flatten(tmp_bere, &req_data) == -1){
  2209. goto error;
  2210. }
  2211. goto done;
  2212. error:
  2213. if (NULL != req_data){
  2214. ber_bvfree(req_data);
  2215. req_data = NULL;
  2216. }
  2217. done:
  2218. if (NULL != tmp_bere){
  2219. ber_free(tmp_bere, 1);
  2220. tmp_bere = NULL;
  2221. }
  2222. return req_data;
  2223. }
  2224. /*
  2225. * Manually add the CLEANRUV task to replicas that do not support
  2226. * the CLEANALLRUV task.
  2227. */
  2228. static void
  2229. replica_send_cleanruv_task(Repl_Agmt *agmt, cleanruv_data *clean_data)
  2230. {
  2231. Repl_Connection *conn;
  2232. ConnResult crc = 0;
  2233. LDAP *ld;
  2234. Slapi_DN *sdn;
  2235. struct berval *vals[2];
  2236. struct berval val;
  2237. LDAPMod *mods[2];
  2238. LDAPMod mod;
  2239. char *repl_dn = NULL;
  2240. char data[15];
  2241. int rc;
  2242. if((conn = conn_new(agmt)) == NULL){
  2243. return;
  2244. }
  2245. crc = conn_connect(conn);
  2246. if (CONN_OPERATION_SUCCESS != crc){
  2247. conn_delete_internal_ext(conn);
  2248. return;
  2249. }
  2250. ld = conn_get_ldap(conn);
  2251. if(ld == NULL){
  2252. conn_delete_internal_ext(conn);
  2253. return;
  2254. }
  2255. sdn = agmt_get_replarea(agmt);
  2256. if (!sdn) {
  2257. conn_delete_internal_ext(conn);
  2258. return;
  2259. }
  2260. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2261. mod.mod_type = "nsds5task";
  2262. mod.mod_bvalues = vals;
  2263. vals [0] = &val;
  2264. vals [1] = NULL;
  2265. val.bv_len = PR_snprintf(data, sizeof(data), "CLEANRUV%d", clean_data->rid);
  2266. val.bv_val = data;
  2267. mods[0] = &mod;
  2268. mods[1] = NULL;
  2269. repl_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", slapi_sdn_get_dn(sdn));
  2270. /*
  2271. * Add task to remote replica
  2272. */
  2273. rc = ldap_modify_ext_s( ld, repl_dn, mods, NULL, NULL);
  2274. if(rc != LDAP_SUCCESS){
  2275. char *hostname = agmt_get_hostname(agmt);
  2276. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2277. "Failed to add CLEANRUV task (%s) to replica (%s). You will need "
  2278. "to manually run the CLEANRUV task on this replica (%s) error (%d)",
  2279. repl_dn, agmt_get_long_name(agmt), hostname, rc);
  2280. slapi_ch_free_string(&hostname);
  2281. }
  2282. slapi_ch_free_string(&repl_dn);
  2283. slapi_sdn_free(&sdn);
  2284. conn_delete_internal_ext(conn);
  2285. }
  2286. /*
  2287. * Check if the rid is in our list of "cleaned" rids
  2288. */
  2289. int
  2290. is_cleaned_rid(ReplicaId rid)
  2291. {
  2292. int i;
  2293. slapi_rwlock_rdlock(rid_lock);
  2294. for(i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != 0; i++){
  2295. if(rid == cleaned_rids[i]){
  2296. slapi_rwlock_unlock(rid_lock);
  2297. return 1;
  2298. }
  2299. }
  2300. slapi_rwlock_unlock(rid_lock);
  2301. return 0;
  2302. }
  2303. int
  2304. is_pre_cleaned_rid(ReplicaId rid)
  2305. {
  2306. int i;
  2307. slapi_rwlock_rdlock(rid_lock);
  2308. for(i = 0; i < CLEANRIDSIZ && pre_cleaned_rids[i] != 0; i++){
  2309. if(rid == pre_cleaned_rids[i]){
  2310. slapi_rwlock_unlock(rid_lock);
  2311. return 1;
  2312. }
  2313. }
  2314. slapi_rwlock_unlock(rid_lock);
  2315. return 0;
  2316. }
  2317. int
  2318. is_task_aborted(ReplicaId rid)
  2319. {
  2320. int i;
  2321. if(rid == 0){
  2322. return 0;
  2323. }
  2324. slapi_rwlock_rdlock(abort_rid_lock);
  2325. for(i = 0; i < CLEANRIDSIZ && aborted_rids[i] != 0; i++){
  2326. if(rid == aborted_rids[i]){
  2327. slapi_rwlock_unlock(abort_rid_lock);
  2328. return 1;
  2329. }
  2330. }
  2331. slapi_rwlock_unlock(abort_rid_lock);
  2332. return 0;
  2333. }
  2334. static void
  2335. preset_cleaned_rid(ReplicaId rid)
  2336. {
  2337. int i;
  2338. slapi_rwlock_wrlock(rid_lock);
  2339. for(i = 0; i < CLEANRIDSIZ; i++){
  2340. if(pre_cleaned_rids[i] == 0){
  2341. pre_cleaned_rids[i] = rid;
  2342. pre_cleaned_rids[i + 1] = 0;
  2343. break;
  2344. }
  2345. }
  2346. slapi_rwlock_unlock(rid_lock);
  2347. }
  2348. /*
  2349. * Just add the rid to the in memory, as we don't want it to survive after a restart,
  2350. * This prevent the changelog from sending updates from this rid, and the local ruv
  2351. * will not be updated either.
  2352. */
  2353. void
  2354. set_cleaned_rid(ReplicaId rid)
  2355. {
  2356. int i;
  2357. slapi_rwlock_wrlock(rid_lock);
  2358. for(i = 0; i < CLEANRIDSIZ; i++){
  2359. if(cleaned_rids[i] == 0){
  2360. cleaned_rids[i] = rid;
  2361. cleaned_rids[i + 1] = 0;
  2362. }
  2363. }
  2364. slapi_rwlock_unlock(rid_lock);
  2365. }
  2366. /*
  2367. * Add the rid and maxcsn to the repl config (so we can resume after a server restart)
  2368. */
  2369. void
  2370. add_cleaned_rid(ReplicaId rid, Replica *r, char *maxcsn, char *forcing)
  2371. {
  2372. Slapi_PBlock *pb;
  2373. struct berval *vals[2];
  2374. struct berval val;
  2375. LDAPMod *mods[2];
  2376. LDAPMod mod;
  2377. char data[CSN_STRSIZE + 10];
  2378. char *dn;
  2379. int rc;
  2380. if(r == NULL || maxcsn == NULL){
  2381. return;
  2382. }
  2383. /*
  2384. * Write the rid & maxcsn to the config entry
  2385. */
  2386. val.bv_len = PR_snprintf(data, sizeof(data),"%d:%s:%s", rid, maxcsn, forcing);
  2387. dn = replica_get_dn(r);
  2388. pb = slapi_pblock_new();
  2389. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2390. mod.mod_type = (char *)type_replicaCleanRUV;
  2391. mod.mod_bvalues = vals;
  2392. vals [0] = &val;
  2393. vals [1] = NULL;
  2394. val.bv_val = data;
  2395. mods[0] = &mod;
  2396. mods[1] = NULL;
  2397. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  2398. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2399. slapi_modify_internal_pb (pb);
  2400. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2401. if (rc != LDAP_SUCCESS && rc != LDAP_TYPE_OR_VALUE_EXISTS && rc != LDAP_NO_SUCH_OBJECT){
  2402. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "CleanAllRUV Task: failed to update replica "
  2403. "config (%d), rid (%d)\n", rc, rid);
  2404. }
  2405. slapi_ch_free_string(&dn);
  2406. slapi_pblock_destroy(pb);
  2407. }
  2408. /*
  2409. * Add aborted rid and repl root to config in case of a server restart
  2410. */
  2411. void
  2412. add_aborted_rid(ReplicaId rid, Replica *r, char *repl_root)
  2413. {
  2414. Slapi_PBlock *pb;
  2415. struct berval *vals[2];
  2416. struct berval val;
  2417. LDAPMod *mods[2];
  2418. LDAPMod mod;
  2419. char *data;
  2420. char *dn;
  2421. int rc;
  2422. int i;
  2423. slapi_rwlock_wrlock(abort_rid_lock);
  2424. for(i = 0; i < CLEANRIDSIZ; i++){
  2425. if(aborted_rids[i] == 0){
  2426. aborted_rids[i] = rid;
  2427. aborted_rids[i + 1] = 0;
  2428. break;
  2429. }
  2430. }
  2431. slapi_rwlock_unlock(abort_rid_lock);
  2432. /*
  2433. * Write the rid to the config entry
  2434. */
  2435. dn = replica_get_dn(r);
  2436. pb = slapi_pblock_new();
  2437. data = PR_smprintf("%d:%s", rid, repl_root);
  2438. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2439. mod.mod_type = (char *)type_replicaAbortCleanRUV;
  2440. mod.mod_bvalues = vals;
  2441. vals [0] = &val;
  2442. vals [1] = NULL;
  2443. val.bv_val = data;
  2444. val.bv_len = strlen (data);
  2445. mods[0] = &mod;
  2446. mods[1] = NULL;
  2447. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  2448. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2449. slapi_modify_internal_pb (pb);
  2450. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2451. if (rc != LDAP_SUCCESS && rc != LDAP_TYPE_OR_VALUE_EXISTS && rc != LDAP_NO_SUCH_OBJECT){
  2452. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "Abort CleanAllRUV Task: failed to update "
  2453. "replica config (%d), rid (%d)\n", rc, rid);
  2454. }
  2455. slapi_ch_free_string(&dn);
  2456. slapi_ch_free_string(&data);
  2457. slapi_pblock_destroy(pb);
  2458. }
  2459. void
  2460. delete_aborted_rid(Replica *r, ReplicaId rid, char *repl_root, int skip){
  2461. Slapi_PBlock *pb;
  2462. LDAPMod *mods[2];
  2463. LDAPMod mod;
  2464. struct berval *vals[2];
  2465. struct berval val;
  2466. char *data;
  2467. char *dn;
  2468. int rc;
  2469. int i;
  2470. if(r == NULL)
  2471. return;
  2472. if(skip){
  2473. /* skip the deleting of the config, and just remove the in memory rid */
  2474. slapi_rwlock_wrlock(abort_rid_lock);
  2475. for(i = 0; i < CLEANRIDSIZ && aborted_rids[i] != rid; i++); /* found rid, stop */
  2476. for(; i < CLEANRIDSIZ; i++){
  2477. /* rewrite entire array */
  2478. aborted_rids[i] = aborted_rids[i + 1];
  2479. }
  2480. slapi_rwlock_unlock(abort_rid_lock);
  2481. } else {
  2482. /* only remove the config, leave the in-memory rid */
  2483. dn = replica_get_dn(r);
  2484. data = PR_smprintf("%d:%s", (int)rid, repl_root);
  2485. mod.mod_op = LDAP_MOD_DELETE|LDAP_MOD_BVALUES;
  2486. mod.mod_type = (char *)type_replicaAbortCleanRUV;
  2487. mod.mod_bvalues = vals;
  2488. vals [0] = &val;
  2489. vals [1] = NULL;
  2490. val.bv_val = data;
  2491. val.bv_len = strlen (data);
  2492. mods[0] = &mod;
  2493. mods[1] = NULL;
  2494. pb = slapi_pblock_new();
  2495. slapi_modify_internal_set_pb(pb, dn, mods, NULL, NULL, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2496. slapi_modify_internal_pb (pb);
  2497. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2498. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2499. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "Abort CleanAllRUV Task: failed to remove replica "
  2500. "config (%d), rid (%d)\n", rc, rid);
  2501. }
  2502. slapi_pblock_destroy (pb);
  2503. slapi_ch_free_string(&dn);
  2504. slapi_ch_free_string(&data);
  2505. }
  2506. }
  2507. /*
  2508. * Just remove the dse.ldif config, but we need to keep the cleaned rids in memory until we know we are done
  2509. */
  2510. static void
  2511. delete_cleaned_rid_config(cleanruv_data *clean_data)
  2512. {
  2513. Slapi_PBlock *pb, *modpb;
  2514. Slapi_Entry **entries = NULL;
  2515. LDAPMod *mods[2];
  2516. LDAPMod mod;
  2517. struct berval *vals[5]= {0, 0, 0, 0, 0}; /* maximum of 4 tasks */
  2518. struct berval val[5];
  2519. char *iter = NULL;
  2520. char *dn = NULL;
  2521. int i, ii;
  2522. int rc = -1, ret, rid;
  2523. if(clean_data == NULL){
  2524. cleanruv_log(NULL, -1, CLEANALLRUV_ID, "delete_cleaned_rid_config: cleanruv data is NULL, "
  2525. "failed to clean the config.");
  2526. return;
  2527. }
  2528. /*
  2529. * Search the config for the exact attribute value to delete
  2530. */
  2531. pb = slapi_pblock_new();
  2532. if(clean_data->replica){
  2533. dn = replica_get_dn(clean_data->replica);
  2534. } else {
  2535. goto bail;
  2536. }
  2537. slapi_search_internal_set_pb(pb, dn, LDAP_SCOPE_SUBTREE, "nsds5ReplicaCleanRUV=*", NULL, 0, NULL, NULL,
  2538. (void *)plugin_get_default_component_id(), 0);
  2539. slapi_search_internal_pb(pb);
  2540. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2541. if (ret != LDAP_SUCCESS){
  2542. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2543. "delete_cleaned_rid_config: internal search failed(%d).",ret);
  2544. goto bail;
  2545. } else {
  2546. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2547. if (entries == NULL || entries[0] == NULL){
  2548. /*
  2549. * No matching entries!
  2550. */
  2551. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2552. "delete_cleaned_rid_config: failed to find any "
  2553. "entries with nsds5ReplicaCleanRUV under (%s)", dn);
  2554. goto bail;
  2555. } else {
  2556. /*
  2557. * Clean all the matching entries
  2558. */
  2559. for(i = 0; entries[i] != NULL;i++){
  2560. char **attr_val = slapi_entry_attr_get_charray(entries[i], type_replicaCleanRUV);
  2561. char *edn = slapi_entry_get_dn(entries[i]);
  2562. int count = 0;
  2563. for (ii = 0; attr_val && attr_val[ii] && i < 5; ii++){
  2564. /* make a copy to retain the full value after toking */
  2565. char *aval = slapi_ch_strdup(attr_val[ii]);
  2566. rid = atoi(ldap_utf8strtok_r(attr_val[ii], ":", &iter));
  2567. if(rid == clean_data->rid){
  2568. val[count].bv_len = strlen(aval);
  2569. val[count].bv_val = aval;
  2570. vals[count] = &val[count];
  2571. count++;
  2572. } else {
  2573. slapi_ch_free_string(&aval);
  2574. }
  2575. }
  2576. slapi_ch_array_free(attr_val);
  2577. /*
  2578. * Now delete the attribute
  2579. */
  2580. vals[4] = NULL;
  2581. mod.mod_op = LDAP_MOD_DELETE|LDAP_MOD_BVALUES;
  2582. mod.mod_type = (char *)type_replicaCleanRUV;
  2583. mod.mod_bvalues = vals;
  2584. mods[0] = &mod;
  2585. mods[1] = NULL;
  2586. modpb = slapi_pblock_new();
  2587. slapi_modify_internal_set_pb(modpb, edn, mods, NULL, NULL,
  2588. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2589. slapi_modify_internal_pb (modpb);
  2590. slapi_pblock_get(modpb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2591. slapi_pblock_destroy(modpb);
  2592. /* free the attr vals */
  2593. for (ii = 0; ii < count; ii++){
  2594. slapi_ch_free_string(&val[ii].bv_val);
  2595. }
  2596. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2597. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2598. "delete_cleaned_rid_config: failed to remove task data "
  2599. "from (%s) error (%d), rid (%d)", edn, rc, clean_data->rid);
  2600. goto bail;
  2601. }
  2602. }
  2603. }
  2604. }
  2605. bail:
  2606. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2607. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2608. "delete_cleaned_rid_config: failed to remove replica config "
  2609. "(%d), rid (%d)", rc, clean_data->rid);
  2610. }
  2611. slapi_free_search_results_internal(pb);
  2612. slapi_pblock_destroy(pb);
  2613. slapi_ch_free_string(&dn);
  2614. }
  2615. /*
  2616. * Remove the rid from our list, and the config
  2617. */
  2618. void
  2619. remove_cleaned_rid(ReplicaId rid)
  2620. {
  2621. int i;
  2622. /*
  2623. * Remove this rid, and optimize the array
  2624. */
  2625. slapi_rwlock_wrlock(rid_lock);
  2626. for(i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != rid; i++); /* found rid, stop */
  2627. for(; i < CLEANRIDSIZ; i++){
  2628. /* rewrite entire array */
  2629. cleaned_rids[i] = cleaned_rids[i + 1];
  2630. }
  2631. /* now do the preset cleaned rids */
  2632. for(i = 0; i < CLEANRIDSIZ && pre_cleaned_rids[i] != rid; i++); /* found rid, stop */
  2633. for(; i < CLEANRIDSIZ; i++){
  2634. /* rewrite entire array */
  2635. pre_cleaned_rids[i] = pre_cleaned_rids[i + 1];
  2636. }
  2637. slapi_rwlock_unlock(rid_lock);
  2638. }
  2639. /*
  2640. * Abort the CLEANALLRUV task
  2641. */
  2642. int
  2643. replica_cleanall_ruv_abort(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  2644. int *returncode, char *returntext, void *arg)
  2645. {
  2646. struct berval *payload = NULL;
  2647. cleanruv_data *data = NULL;
  2648. PRThread *thread = NULL;
  2649. Slapi_Task *task = NULL;
  2650. Slapi_DN *sdn = NULL;
  2651. Replica *replica;
  2652. ReplicaId rid = -1;
  2653. Object *r;
  2654. const char *certify_all;
  2655. const char *base_dn;
  2656. const char *rid_str;
  2657. char *ridstr = NULL;
  2658. int rc = SLAPI_DSE_CALLBACK_OK;
  2659. if(get_abort_cleanruv_task_count() >= CLEANRIDSIZ){
  2660. /* we are already running the maximum number of tasks */
  2661. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  2662. "Exceeded maximum number of active ABORT CLEANALLRUV tasks(%d)",
  2663. CLEANRIDSIZ);
  2664. cleanruv_log(task, -1, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2665. *returncode = LDAP_OPERATIONS_ERROR;
  2666. return SLAPI_DSE_CALLBACK_ERROR;
  2667. }
  2668. /* allocate new task now */
  2669. task = slapi_new_task(slapi_entry_get_ndn(e));
  2670. /* register our destructor for waiting the task is done */
  2671. slapi_task_set_destructor_fn(task, replica_cleanall_ruv_abort_destructor);
  2672. /*
  2673. * Get our task settings
  2674. */
  2675. if ((rid_str = fetch_attr(e, "replica-id", 0)) == NULL){
  2676. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing required attr \"replica-id\"");
  2677. cleanruv_log(task, -1, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2678. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2679. rc = SLAPI_DSE_CALLBACK_ERROR;
  2680. goto out;
  2681. }
  2682. certify_all = fetch_attr(e, "replica-certify-all", 0);
  2683. /*
  2684. * Check the rid
  2685. */
  2686. rid = atoi(rid_str);
  2687. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  2688. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)",
  2689. rid, slapi_sdn_get_dn(slapi_entry_get_sdn(e)));
  2690. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"%s", returntext);
  2691. *returncode = LDAP_OPERATIONS_ERROR;
  2692. rc = SLAPI_DSE_CALLBACK_ERROR;
  2693. goto out;
  2694. }
  2695. if ((base_dn = fetch_attr(e, "replica-base-dn", 0)) == NULL){
  2696. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing required attr \"replica-base-dn\"");
  2697. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2698. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2699. rc = SLAPI_DSE_CALLBACK_ERROR;
  2700. goto out;
  2701. }
  2702. if(!is_cleaned_rid(rid) && !is_pre_cleaned_rid(rid)){
  2703. /* we are not cleaning this rid */
  2704. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is not being cleaned, nothing to abort.", rid);
  2705. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2706. *returncode = LDAP_UNWILLING_TO_PERFORM;
  2707. rc = SLAPI_DSE_CALLBACK_ERROR;
  2708. goto out;
  2709. }
  2710. if(is_task_aborted(rid)){
  2711. /* we are already aborting this rid */
  2712. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is already being aborted", rid);
  2713. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2714. *returncode = LDAP_UNWILLING_TO_PERFORM;
  2715. rc = SLAPI_DSE_CALLBACK_ERROR;
  2716. goto out;
  2717. }
  2718. /*
  2719. * Get the replica object
  2720. */
  2721. sdn = slapi_sdn_new_dn_byval(base_dn);
  2722. if((r = replica_get_replica_from_dn(sdn)) == NULL){
  2723. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Failed to find replica from dn(%s)", base_dn);
  2724. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2725. *returncode = LDAP_OPERATIONS_ERROR;
  2726. rc = SLAPI_DSE_CALLBACK_ERROR;
  2727. goto out;
  2728. }
  2729. /*
  2730. * Check certify value
  2731. */
  2732. if(certify_all){
  2733. if(strcasecmp(certify_all,"yes") && strcasecmp(certify_all,"no")){
  2734. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid value for \"replica-certify-all\", the value "
  2735. "must be \"yes\" or \"no\".");
  2736. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2737. *returncode = LDAP_OPERATIONS_ERROR;
  2738. rc = SLAPI_DSE_CALLBACK_ERROR;
  2739. goto out;
  2740. }
  2741. } else {
  2742. /*
  2743. * The default should be not to certify all the replicas, because
  2744. * we might be trying to abort a clean task that is "hanging" due
  2745. * to unreachable replicas. If the default is "yes" then the abort
  2746. * task will run into the same issue.
  2747. */
  2748. certify_all = "no";
  2749. }
  2750. /*
  2751. * Create payload
  2752. */
  2753. ridstr = slapi_ch_smprintf("%d:%s:%s", rid, base_dn, certify_all);
  2754. payload = create_cleanruv_payload(ridstr);
  2755. if(payload == NULL){
  2756. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "Failed to create extended op payload, aborting task");
  2757. *returncode = LDAP_OPERATIONS_ERROR;
  2758. rc = SLAPI_DSE_CALLBACK_ERROR;
  2759. goto out;
  2760. }
  2761. /*
  2762. * Stop the cleaning, and delete the rid
  2763. */
  2764. replica = (Replica*)object_get_data (r);
  2765. add_aborted_rid(rid, replica, (char *)base_dn);
  2766. stop_ruv_cleaning();
  2767. /*
  2768. * Prepare the abort struct and fire off the thread
  2769. */
  2770. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  2771. if (data == NULL) {
  2772. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"Failed to allocate abort_cleanruv_data. Aborting task.");
  2773. *returncode = LDAP_OPERATIONS_ERROR;
  2774. rc = SLAPI_DSE_CALLBACK_ERROR;
  2775. goto out;
  2776. }
  2777. data->repl_obj = r; /* released in replica_abort_task_thread() */
  2778. data->replica = replica;
  2779. data->task = task;
  2780. data->payload = payload;
  2781. data->rid = rid;
  2782. data->repl_root = slapi_ch_strdup(base_dn);
  2783. data->sdn = NULL;
  2784. data->certify = slapi_ch_strdup(certify_all);
  2785. thread = PR_CreateThread(PR_USER_THREAD, replica_abort_task_thread,
  2786. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2787. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2788. if (thread == NULL) {
  2789. object_release(r);
  2790. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"Unable to create abort thread. Aborting task.");
  2791. *returncode = LDAP_OPERATIONS_ERROR;
  2792. slapi_ch_free_string(&data->certify);
  2793. rc = SLAPI_DSE_CALLBACK_ERROR;
  2794. }
  2795. out:
  2796. slapi_ch_free_string(&ridstr);
  2797. slapi_sdn_free(&sdn);
  2798. if(rc != SLAPI_DSE_CALLBACK_OK){
  2799. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "Abort Task failed (%d)", rc);
  2800. slapi_task_finish(task, rc);
  2801. }
  2802. return rc;
  2803. }
  2804. /*
  2805. * Abort CLEANALLRUV task thread
  2806. */
  2807. void
  2808. replica_abort_task_thread(void *arg)
  2809. {
  2810. cleanruv_data *data = (cleanruv_data *)arg;
  2811. Repl_Agmt *agmt;
  2812. Object *agmt_obj;
  2813. int agmt_not_notified = 1;
  2814. int interval = 10;
  2815. int release_it = 0;
  2816. int count = 0, rc = 0;
  2817. if (!data) {
  2818. return; /* no data */
  2819. }
  2820. if (data->task) {
  2821. slapi_task_inc_refcount(data->task);
  2822. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, repl_plugin_name, "replica_abort_task_thread --> refcount incremented.\n");
  2823. }
  2824. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Aborting task for rid(%d)...",data->rid);
  2825. /*
  2826. * Need to build the replica from the dn
  2827. */
  2828. if(data->replica == NULL && data->repl_obj == NULL){
  2829. /*
  2830. * This thread was initiated at startup because the process did not finish. Due
  2831. * to timing issues, we need to wait to grab the replica obj until we get here.
  2832. */
  2833. if((data->repl_obj = replica_get_replica_from_dn(data->sdn)) == NULL){
  2834. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Failed to get replica object from dn (%s).", slapi_sdn_get_dn(data->sdn));
  2835. goto done;
  2836. }
  2837. if(data->replica == NULL && data->repl_obj){
  2838. data->replica = (Replica*)object_get_data(data->repl_obj);
  2839. }
  2840. release_it = 1;
  2841. }
  2842. /*
  2843. * Now send the abort cleanruv extended op to all the agreements
  2844. */
  2845. while(agmt_not_notified && !slapi_is_shutting_down()){
  2846. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  2847. if(agmt_obj == NULL){
  2848. agmt_not_notified = 0;
  2849. break;
  2850. }
  2851. while (agmt_obj && !slapi_is_shutting_down()){
  2852. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2853. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2854. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2855. agmt_not_notified = 0;
  2856. continue;
  2857. }
  2858. if(replica_cleanallruv_send_abort_extop(agmt, data->task, data->payload)){
  2859. if(strcasecmp(data->certify,"yes") == 0){
  2860. /* we are verifying all the replicas receive the abort task */
  2861. agmt_not_notified = 1;
  2862. break;
  2863. } else {
  2864. /* we do not care if we could not reach a replica, just continue as if we did */
  2865. agmt_not_notified = 0;
  2866. }
  2867. } else {
  2868. /* success */
  2869. agmt_not_notified = 0;
  2870. }
  2871. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2872. } /* while loop for agmts */
  2873. if(agmt_not_notified == 0){
  2874. /* everybody has been contacted */
  2875. break;
  2876. }
  2877. /*
  2878. * Need to sleep between passes. unless we are shutting down
  2879. */
  2880. if (!slapi_is_shutting_down()){
  2881. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Retrying in %d seconds",interval);
  2882. PR_Lock( notify_lock );
  2883. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2884. PR_Unlock( notify_lock );
  2885. }
  2886. if(interval < 14400){ /* 4 hour max */
  2887. interval = interval * 2;
  2888. } else {
  2889. interval = 14400;
  2890. }
  2891. } /* while */
  2892. done:
  2893. if(agmt_not_notified || slapi_is_shutting_down()){
  2894. /* failure */
  2895. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Abort task failed, will resume the task at the next server startup.");
  2896. } else {
  2897. /*
  2898. * Wait for this server to stop its cleanallruv task(which removes the rid from the cleaned list)
  2899. */
  2900. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Waiting for CleanAllRUV task to abort...");
  2901. while(is_cleaned_rid(data->rid) && !slapi_is_shutting_down()){
  2902. DS_Sleep(PR_SecondsToInterval(1));
  2903. count++;
  2904. if(count == 60){ /* it should not take this long */
  2905. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "CleanAllRUV task failed to abort. You might need to "
  2906. "rerun the task.");
  2907. rc = -1;
  2908. break;
  2909. }
  2910. }
  2911. /*
  2912. * Clean up the config
  2913. */
  2914. delete_aborted_rid(data->replica, data->rid, data->repl_root, 1); /* delete just the config, leave rid in memory */
  2915. if(strcasecmp(data->certify, "yes") == 0){
  2916. check_replicas_are_done_aborting(data);
  2917. }
  2918. delete_aborted_rid(data->replica, data->rid, data->repl_root, 0); /* remove the in-memory aborted rid */
  2919. if(rc == 0){
  2920. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Successfully aborted task for rid(%d)", data->rid);
  2921. } else {
  2922. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Failed to abort task for rid(%d)",data->rid);
  2923. }
  2924. }
  2925. if(data->task){
  2926. slapi_task_finish(data->task, agmt_not_notified);
  2927. slapi_task_dec_refcount(data->task);
  2928. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, repl_plugin_name, "replica_abort_task_thread <-- refcount incremented.\n");
  2929. }
  2930. if(data->repl_obj && release_it)
  2931. object_release(data->repl_obj);
  2932. if(data->payload){
  2933. ber_bvfree(data->payload);
  2934. }
  2935. slapi_ch_free_string(&data->repl_root);
  2936. slapi_ch_free_string(&data->certify);
  2937. slapi_sdn_free(&data->sdn);
  2938. slapi_ch_free((void **)&data);
  2939. }
  2940. static int
  2941. replica_cleanallruv_send_abort_extop(Repl_Agmt *ra, Slapi_Task *task, struct berval *payload)
  2942. {
  2943. Repl_Connection *conn = NULL;
  2944. ConnResult crc = 0;
  2945. int msgid = 0;
  2946. int rc = -1;
  2947. if((conn = conn_new(ra)) == NULL){
  2948. return rc;
  2949. }
  2950. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2951. crc = conn_send_extended_operation(conn, REPL_ABORT_CLEANRUV_OID, payload, NULL, &msgid);
  2952. /*
  2953. * success or failure, just return the error code
  2954. */
  2955. rc = crc;
  2956. if(rc){
  2957. cleanruv_log(task, agmt_get_consumer_rid(ra, conn), ABORT_CLEANALLRUV_ID,
  2958. "Failed to send extop to replica(%s).", agmt_get_long_name(ra));
  2959. }
  2960. } else {
  2961. cleanruv_log(task, agmt_get_consumer_rid(ra, conn), ABORT_CLEANALLRUV_ID,
  2962. "Failed to connect to replica(%s).", agmt_get_long_name(ra));
  2963. rc = -1;
  2964. }
  2965. conn_delete_internal_ext(conn);
  2966. return rc;
  2967. }
  2968. static int
  2969. replica_cleanallruv_send_extop(Repl_Agmt *ra, cleanruv_data *clean_data, int check_result)
  2970. {
  2971. Repl_Connection *conn = NULL;
  2972. ConnResult crc = 0;
  2973. int msgid = 0;
  2974. int rc = -1;
  2975. if((conn = conn_new(ra)) == NULL){
  2976. return rc;
  2977. }
  2978. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2979. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_OID, clean_data->payload, NULL, &msgid);
  2980. if(crc == CONN_OPERATION_SUCCESS && check_result){
  2981. struct berval *retsdata = NULL;
  2982. char *retoid = NULL;
  2983. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  2984. if (CONN_OPERATION_SUCCESS == crc ){
  2985. char *response = NULL;
  2986. decode_cleanruv_payload(retsdata, &response);
  2987. if(response && strcmp(response,CLEANRUV_ACCEPTED) == 0){
  2988. /* extop was accepted */
  2989. rc = 0;
  2990. } else {
  2991. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,"Replica %s does not support the CLEANALLRUV task. "
  2992. "Sending replica CLEANRUV task...", slapi_sdn_get_dn(agmt_get_dn_byref(ra)));
  2993. /*
  2994. * Ok, this replica doesn't know about CLEANALLRUV, so just manually
  2995. * add the CLEANRUV task to the replica.
  2996. */
  2997. replica_send_cleanruv_task(ra, clean_data);
  2998. rc = 0;
  2999. }
  3000. if (NULL != retsdata)
  3001. ber_bvfree(retsdata);
  3002. slapi_ch_free_string(&retoid);
  3003. slapi_ch_free_string(&response);
  3004. }
  3005. } else {
  3006. /*
  3007. * success or failure, just return the error code
  3008. */
  3009. rc = crc;
  3010. }
  3011. }
  3012. conn_delete_internal_ext(conn);
  3013. return rc;
  3014. }
  3015. static CSN*
  3016. replica_cleanallruv_find_maxcsn(Replica *replica, ReplicaId rid, char *basedn)
  3017. {
  3018. Object *agmt_obj;
  3019. Repl_Agmt *agmt;
  3020. CSN *maxcsn = NULL, *topcsn = NULL;
  3021. char *rid_text = slapi_ch_smprintf("%d", rid);
  3022. char *csnstr = NULL;
  3023. /* start with the local maxcsn */
  3024. csnstr = replica_cleanallruv_get_local_maxcsn(rid, basedn);
  3025. if(csnstr){
  3026. topcsn = csn_new();
  3027. csn_init_by_string(topcsn, csnstr);
  3028. slapi_ch_free_string(&csnstr);
  3029. }
  3030. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  3031. if(agmt_obj == NULL){ /* no agreements */
  3032. goto done;
  3033. }
  3034. while (agmt_obj && !slapi_is_shutting_down()){
  3035. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  3036. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  3037. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  3038. continue;
  3039. }
  3040. if(replica_cleanallruv_get_replica_maxcsn(agmt, rid_text, basedn, &maxcsn) == 0){
  3041. if(maxcsn == NULL){
  3042. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  3043. continue;
  3044. }
  3045. if(topcsn == NULL){
  3046. topcsn = maxcsn;
  3047. } else {
  3048. if(csn_compare(topcsn, maxcsn) < 0){
  3049. csn_free(&topcsn);
  3050. topcsn = maxcsn;
  3051. } else {
  3052. csn_free(&maxcsn);
  3053. }
  3054. }
  3055. }
  3056. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  3057. }
  3058. done:
  3059. slapi_ch_free_string(&rid_text);
  3060. return topcsn;
  3061. }
  3062. static int
  3063. replica_cleanallruv_get_replica_maxcsn(Repl_Agmt *agmt, char *rid_text, char *basedn, CSN **csn)
  3064. {
  3065. Repl_Connection *conn = NULL;
  3066. ConnResult crc = -1;
  3067. struct berval *payload = NULL;
  3068. CSN *maxcsn = NULL;
  3069. char *data = NULL;
  3070. int msgid = 0;
  3071. if((conn = conn_new(agmt)) == NULL){
  3072. return -1;
  3073. }
  3074. data = slapi_ch_smprintf("%s:%s",rid_text, basedn);
  3075. payload = create_cleanruv_payload(data);
  3076. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3077. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  3078. if(crc == CONN_OPERATION_SUCCESS){
  3079. struct berval *retsdata = NULL;
  3080. char *retoid = NULL;
  3081. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3082. if (CONN_OPERATION_SUCCESS == crc ){
  3083. char *remote_maxcsn = NULL;
  3084. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3085. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3086. maxcsn = csn_new();
  3087. csn_init_by_string(maxcsn, remote_maxcsn);
  3088. *csn = maxcsn;
  3089. } else {
  3090. /* no csn */
  3091. *csn = NULL;
  3092. }
  3093. slapi_ch_free_string(&retoid);
  3094. slapi_ch_free_string(&remote_maxcsn);
  3095. if (NULL != retsdata)
  3096. ber_bvfree(retsdata);
  3097. }
  3098. }
  3099. }
  3100. conn_delete_internal_ext(conn);
  3101. slapi_ch_free_string(&data);
  3102. if(payload)
  3103. ber_bvfree(payload);
  3104. return (int)crc;
  3105. }
  3106. static int
  3107. replica_cleanallruv_check_maxcsn(Repl_Agmt *agmt, char *basedn, char *rid_text, char *maxcsn, Slapi_Task *task)
  3108. {
  3109. Repl_Connection *conn = NULL;
  3110. ConnResult crc = 0;
  3111. struct berval *payload = NULL;
  3112. char *data = NULL;
  3113. int msgid = 0;
  3114. int rc = -1;
  3115. if((conn = conn_new(agmt)) == NULL){
  3116. return -1;
  3117. }
  3118. data = slapi_ch_smprintf("%s:%s",rid_text, basedn);
  3119. payload = create_cleanruv_payload(data);
  3120. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3121. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  3122. if(crc == CONN_OPERATION_SUCCESS){
  3123. struct berval *retsdata = NULL;
  3124. char *retoid = NULL;
  3125. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3126. if (CONN_OPERATION_SUCCESS == crc ){
  3127. char *remote_maxcsn = NULL;
  3128. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3129. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3130. CSN *max, *repl_max;
  3131. max = csn_new();
  3132. repl_max = csn_new();
  3133. csn_init_by_string(max, maxcsn);
  3134. csn_init_by_string(repl_max, remote_maxcsn);
  3135. if(csn_compare (repl_max, max) < 0){
  3136. /* we are not caught up yet, free, and return */
  3137. cleanruv_log(task, atoi(rid_text), CLEANALLRUV_ID,"Replica maxcsn (%s) is not caught up with deleted replica's maxcsn(%s)",
  3138. remote_maxcsn, maxcsn);
  3139. rc = -1;
  3140. } else {
  3141. /* ok this replica is caught up */
  3142. rc = 0;
  3143. }
  3144. csn_free(&max);
  3145. csn_free(&repl_max);
  3146. } else {
  3147. /* no remote_maxcsn - return success */
  3148. rc = 0;
  3149. }
  3150. slapi_ch_free_string(&retoid);
  3151. slapi_ch_free_string(&remote_maxcsn);
  3152. if (NULL != retsdata)
  3153. ber_bvfree(retsdata);
  3154. }
  3155. }
  3156. }
  3157. conn_delete_internal_ext(conn);
  3158. slapi_ch_free_string(&data);
  3159. if(payload)
  3160. ber_bvfree(payload);
  3161. return rc;
  3162. }
  3163. static int
  3164. replica_cleanallruv_replica_alive(Repl_Agmt *agmt)
  3165. {
  3166. Repl_Connection *conn = NULL;
  3167. LDAP *ld = NULL;
  3168. LDAPMessage *result = NULL;
  3169. int rc = -1;
  3170. if((conn = conn_new(agmt)) == NULL){
  3171. return rc;
  3172. }
  3173. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3174. ld = conn_get_ldap(conn);
  3175. if(ld == NULL){
  3176. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name, "CleanAllRUV_task: failed to get LDAP "
  3177. "handle from the replication agmt (%s). Moving on to the next agmt.\n",agmt_get_long_name(agmt));
  3178. conn_delete_internal_ext(conn);
  3179. return -1;
  3180. }
  3181. if(ldap_search_ext_s(ld, "", LDAP_SCOPE_BASE, "objectclass=top",
  3182. NULL, 0, NULL, NULL, NULL, 0, &result) == LDAP_SUCCESS)
  3183. {
  3184. rc = 0;
  3185. } else {
  3186. rc = -1;
  3187. }
  3188. if(result)
  3189. ldap_msgfree( result );
  3190. }
  3191. conn_delete_internal_ext(conn);
  3192. return rc;
  3193. }
  3194. static int
  3195. replica_cleanallruv_check_ruv(char *repl_root, Repl_Agmt *agmt, char *rid_text, Slapi_Task *task, char *force)
  3196. {
  3197. Repl_Connection *conn = NULL;
  3198. ConnResult crc = 0;
  3199. struct berval *payload = NULL;
  3200. char *data = NULL;
  3201. int msgid = 0;
  3202. int rc = -1;
  3203. if((conn = conn_new(agmt)) == NULL){
  3204. if(strcasecmp(force, "yes") == 0){
  3205. return 0;
  3206. }
  3207. return rc;
  3208. }
  3209. data = slapi_ch_smprintf("%s:%s",rid_text, repl_root);
  3210. payload = create_cleanruv_payload(data);
  3211. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3212. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  3213. if(crc == CONN_OPERATION_SUCCESS){
  3214. struct berval *retsdata = NULL;
  3215. char *retoid = NULL;
  3216. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3217. if (CONN_OPERATION_SUCCESS == crc ){
  3218. char *remote_maxcsn = NULL;
  3219. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3220. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3221. /* remote replica still has dirty RUV element */
  3222. rc = -1;
  3223. } else {
  3224. /* no maxcsn = we're clean */
  3225. rc = 0;
  3226. }
  3227. slapi_ch_free_string(&retoid);
  3228. slapi_ch_free_string(&remote_maxcsn);
  3229. if (NULL != retsdata)
  3230. ber_bvfree(retsdata);
  3231. }
  3232. }
  3233. } else {
  3234. if (strcasecmp(force, "yes") == 0){
  3235. /* We are forcing, we don't care that the replica is not online */
  3236. rc = 0;
  3237. }
  3238. }
  3239. conn_delete_internal_ext(conn);
  3240. slapi_ch_free_string(&data);
  3241. if(payload)
  3242. ber_bvfree(payload);
  3243. return rc;
  3244. }
  3245. static int
  3246. get_cleanruv_task_count(void)
  3247. {
  3248. int i, count = 0;
  3249. slapi_rwlock_wrlock(rid_lock);
  3250. for(i = 0; i < CLEANRIDSIZ; i++){
  3251. if(pre_cleaned_rids[i] != 0){
  3252. count++;
  3253. }
  3254. }
  3255. slapi_rwlock_unlock(rid_lock);
  3256. return count;
  3257. }
  3258. static int
  3259. get_abort_cleanruv_task_count(void)
  3260. {
  3261. int i, count = 0;
  3262. slapi_rwlock_wrlock(rid_lock);
  3263. for(i = 0; i < CLEANRIDSIZ; i++){
  3264. if(aborted_rids[i] != 0){
  3265. count++;
  3266. }
  3267. }
  3268. slapi_rwlock_unlock(rid_lock);
  3269. return count;
  3270. }
  3271. /*
  3272. * Notify sleeping CLEANALLRUV threads to stop
  3273. */
  3274. void
  3275. stop_ruv_cleaning()
  3276. {
  3277. if(notify_lock){
  3278. PR_Lock( notify_lock );
  3279. PR_NotifyCondVar( notify_cvar );
  3280. PR_Unlock( notify_lock );
  3281. }
  3282. }
  3283. /*
  3284. * Write our logging to the task and error log
  3285. */
  3286. void
  3287. cleanruv_log(Slapi_Task *task, int rid, char *task_type, char *fmt, ...)
  3288. {
  3289. va_list ap1;
  3290. va_list ap2;
  3291. va_list ap3;
  3292. va_list ap4;
  3293. char *errlog_fmt;
  3294. va_start(ap1, fmt);
  3295. va_start(ap2, fmt);
  3296. va_start(ap3, fmt);
  3297. va_start(ap4, fmt);
  3298. if(task){
  3299. slapi_task_log_notice_ext(task, fmt, ap1);
  3300. slapi_task_log_status_ext(task, fmt, ap2);
  3301. slapi_task_inc_progress(task);
  3302. }
  3303. errlog_fmt = PR_smprintf("%s (rid %d): %s \n",task_type, rid, fmt);
  3304. slapi_log_error_ext(SLAPI_LOG_FATAL, repl_plugin_name, errlog_fmt, ap3, ap4);
  3305. slapi_ch_free_string(&errlog_fmt);
  3306. va_end(ap1);
  3307. va_end(ap2);
  3308. va_end(ap3);
  3309. va_end(ap4);
  3310. }
  3311. char *
  3312. replica_cleanallruv_get_local_maxcsn(ReplicaId rid, char *base_dn)
  3313. {
  3314. Slapi_PBlock *search_pb = NULL;
  3315. Slapi_Entry **entries = NULL;
  3316. char **ruv_elements = NULL;
  3317. char *maxcsn = NULL;
  3318. char *filter = NULL;
  3319. char *ridstr = NULL;
  3320. char *iter = NULL;
  3321. char *attrs[2];
  3322. char *ruv_part = NULL;
  3323. int part_count = 0;
  3324. int res, i;
  3325. /*
  3326. * Get the maxruv from the database tombstone entry
  3327. */
  3328. filter = "(&(nsuniqueid=ffffffff-ffffffff-ffffffff-ffffffff)(objectclass=nstombstone))";
  3329. attrs[0] = "nsds50ruv";
  3330. attrs[1] = NULL;
  3331. ridstr = slapi_ch_smprintf("{replica %d ldap", rid);
  3332. search_pb = slapi_pblock_new();
  3333. 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);
  3334. slapi_search_internal_pb (search_pb);
  3335. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  3336. if ( LDAP_SUCCESS == res ) {
  3337. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  3338. if (NULL == entries || entries[0] == NULL) {
  3339. /* Hmmm, no tombstone! Error out */
  3340. } else {
  3341. /* find the right ruv element, and find the maxcsn */
  3342. ruv_elements = slapi_entry_attr_get_charray(entries[0],attrs[0]);
  3343. for(i = 0; ruv_elements && ruv_elements[i] ; i++){
  3344. if(strstr(ruv_elements[i], ridstr)){
  3345. /* get the max csn */
  3346. ruv_part = ldap_utf8strtok_r(ruv_elements[i], " ", &iter);
  3347. for(part_count = 1; ruv_part && part_count < 5; part_count++){
  3348. ruv_part = ldap_utf8strtok_r(iter, " ", &iter);
  3349. }
  3350. if(part_count == 5 && ruv_part){/* we have the maxcsn */
  3351. maxcsn = slapi_ch_strdup(ruv_part);
  3352. break;
  3353. }
  3354. }
  3355. }
  3356. slapi_ch_array_free(ruv_elements);
  3357. }
  3358. } else {
  3359. /* internal search failed */
  3360. cleanruv_log(NULL, (int)rid, CLEANALLRUV_ID, "replica_cleanallruv_get_local_maxcsn: internal search failed (%d)\n", res);
  3361. }
  3362. slapi_free_search_results_internal(search_pb);
  3363. slapi_pblock_destroy(search_pb);
  3364. slapi_ch_free_string(&ridstr);
  3365. return maxcsn;
  3366. }