repl5_replica_config.c 116 KB

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