repl5_replica_config.c 112 KB

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