repl5_replica_config.c 116 KB

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