1
0

repl5_replica_config.c 121 KB

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