repl5_replica_config.c 106 KB

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