repl5_replica_config.c 122 KB

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