repl5_replica_config.c 116 KB

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