repl5_replica_config.c 120 KB

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