repl5_replica_config.c 128 KB

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