repl5_replica_config.c 119 KB

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