repl5_replica_config.c 122 KB

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