repl5_replica_config.c 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571
  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);
  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. replica_reset_csn_pl(r);
  895. }
  896. ruv_delete_replica(ruv, oldrid);
  897. replica_set_ruv_dirty(r);
  898. cl5CleanRUV(oldrid);
  899. replica_set_csn_assigned(r);
  900. }
  901. object_release(ruv_obj);
  902. }
  903. replica_set_type (r, type);
  904. replica_set_rid(r, rid);
  905. /* Set the mapping tree node, and the list of referrals */
  906. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  907. if (!replica_is_legacy_consumer(r))
  908. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  909. }
  910. return LDAP_SUCCESS;
  911. }
  912. static int
  913. replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext,
  914. int apply_mods)
  915. {
  916. PR_ASSERT (r);
  917. if (apply_mods)
  918. {
  919. Slapi_Mod smod;
  920. Slapi_ValueSet *vs= slapi_valueset_new();
  921. slapi_mod_init_byref(&smod, (LDAPMod *)mod); /* cast away const */
  922. slapi_valueset_set_from_smod(vs, &smod);
  923. replica_set_updatedn(r, vs, mod->mod_op);
  924. slapi_mod_done(&smod);
  925. slapi_valueset_free(vs);
  926. }
  927. return LDAP_SUCCESS;
  928. }
  929. static int
  930. replica_config_change_updatedngroup (Replica *r, const LDAPMod *mod, char *returntext,
  931. int apply_mods)
  932. {
  933. PR_ASSERT (r);
  934. if (apply_mods)
  935. {
  936. Slapi_Mod smod;
  937. Slapi_ValueSet *vs= slapi_valueset_new();
  938. slapi_mod_init_byref(&smod, (LDAPMod *)mod); /* cast away const */
  939. slapi_valueset_set_from_smod(vs, &smod);
  940. replica_set_groupdn(r, vs, mod->mod_op);
  941. slapi_mod_done(&smod);
  942. slapi_valueset_free(vs);
  943. }
  944. return LDAP_SUCCESS;
  945. }
  946. static int replica_config_change_flags (Replica *r, const char *new_flags,
  947. char *returntext, int apply_mods)
  948. {
  949. PR_ASSERT (r);
  950. if (apply_mods)
  951. {
  952. PRUint32 flags;
  953. flags = atol (new_flags);
  954. replica_replace_flags (r, flags);
  955. }
  956. return LDAP_SUCCESS;
  957. }
  958. static int replica_execute_task (Object *r, const char *task_name, char *returntext,
  959. int apply_mods)
  960. {
  961. if (strcasecmp (task_name, CL2LDIF_TASK) == 0)
  962. {
  963. if (apply_mods)
  964. {
  965. return replica_execute_cl2ldif_task (r, returntext);
  966. }
  967. else
  968. return LDAP_SUCCESS;
  969. }
  970. else if (strcasecmp (task_name, LDIF2CL_TASK) == 0)
  971. {
  972. if (apply_mods)
  973. {
  974. return replica_execute_ldif2cl_task (r, returntext);
  975. }
  976. else
  977. return LDAP_SUCCESS;
  978. }
  979. else if (strncasecmp (task_name, CLEANRUV, CLEANRUVLEN) == 0)
  980. {
  981. int temprid = atoi(&(task_name[CLEANRUVLEN]));
  982. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  983. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - %s", temprid, task_name);
  984. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_execute_task: %s\n", returntext);
  985. return LDAP_OPERATIONS_ERROR;
  986. }
  987. if (apply_mods)
  988. {
  989. return replica_execute_cleanruv_task (r, (ReplicaId)temprid, returntext);
  990. }
  991. else
  992. return LDAP_SUCCESS;
  993. }
  994. else if (strncasecmp (task_name, CLEANALLRUV, CLEANALLRUVLEN) == 0)
  995. {
  996. int temprid = atoi(&(task_name[CLEANALLRUVLEN]));
  997. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  998. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)", temprid, task_name);
  999. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_execute_task: %s\n", returntext);
  1000. return LDAP_OPERATIONS_ERROR;
  1001. }
  1002. if (apply_mods)
  1003. {
  1004. Slapi_Task *empty_task = NULL;
  1005. return replica_execute_cleanall_ruv_task(r, (ReplicaId)temprid, empty_task, returntext, "no");
  1006. }
  1007. else
  1008. return LDAP_SUCCESS;
  1009. }
  1010. else
  1011. {
  1012. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "unsupported replica task - %s", task_name);
  1013. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1014. "replica_execute_task: %s\n", returntext);
  1015. return LDAP_OPERATIONS_ERROR;
  1016. }
  1017. }
  1018. static int
  1019. replica_cleanup_task (Object *r, const char *task_name, char *returntext,
  1020. int apply_mods)
  1021. {
  1022. int rc = LDAP_SUCCESS;
  1023. if (apply_mods) {
  1024. Replica *replica = (Replica*)object_get_data (r);
  1025. if (NULL == replica) {
  1026. rc = LDAP_OPERATIONS_ERROR;
  1027. } else {
  1028. rc = replica_task_done(replica);
  1029. }
  1030. }
  1031. return rc;
  1032. }
  1033. static int
  1034. replica_task_done(Replica *replica)
  1035. {
  1036. int rc = LDAP_OPERATIONS_ERROR;
  1037. char *replica_dn = NULL;
  1038. Slapi_DN *replica_sdn = NULL;
  1039. Slapi_PBlock *pb = NULL;
  1040. LDAPMod *mods [2];
  1041. LDAPMod mod;
  1042. if (NULL == replica) {
  1043. return rc;
  1044. }
  1045. /* dn: cn=replica,cn=dc\3Dexample\2Cdc\3Dcom,cn=mapping tree,cn=config */
  1046. replica_dn = slapi_ch_smprintf("%s,cn=\"%s\",%s",
  1047. REPLICA_RDN,
  1048. slapi_sdn_get_dn(replica_get_root(replica)),
  1049. CONFIG_BASE);
  1050. if (NULL == replica_dn) {
  1051. return rc;
  1052. }
  1053. replica_sdn = slapi_sdn_new_dn_passin(replica_dn);
  1054. pb = slapi_pblock_new();
  1055. mods[0] = &mod;
  1056. mods[1] = NULL;
  1057. mod.mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
  1058. mod.mod_type = (char *)TASK_ATTR;
  1059. mod.mod_bvalues = NULL;
  1060. slapi_modify_internal_set_pb_ext(pb, replica_sdn, mods, NULL/* controls */,
  1061. NULL/* uniqueid */,
  1062. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  1063. 0/* flags */);
  1064. slapi_modify_internal_pb (pb);
  1065. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1066. if ((rc != LDAP_SUCCESS) && (rc != LDAP_NO_SUCH_ATTRIBUTE)) {
  1067. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1068. "replica_task_done: "
  1069. "failed to remove (%s) attribute from (%s) entry; "
  1070. "LDAP error - %d\n",
  1071. TASK_ATTR, replica_dn, rc);
  1072. }
  1073. slapi_pblock_destroy (pb);
  1074. slapi_sdn_free(&replica_sdn);
  1075. return rc;
  1076. }
  1077. static int replica_execute_cl2ldif_task (Object *r, char *returntext)
  1078. {
  1079. int rc;
  1080. Object *rlist [2];
  1081. Replica *replica;
  1082. char fName [MAXPATHLEN];
  1083. char *clDir = NULL;
  1084. if (cl5GetState () != CL5_STATE_OPEN)
  1085. {
  1086. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  1087. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1088. "replica_execute_cl2ldif_task: %s\n", returntext);
  1089. rc = LDAP_OPERATIONS_ERROR;
  1090. goto bail;
  1091. }
  1092. rlist[0] = r;
  1093. rlist[1] = NULL;
  1094. /* file is stored in the changelog directory and is named
  1095. <replica name>.ldif */
  1096. clDir = cl5GetDir ();
  1097. if (NULL == clDir) {
  1098. rc = LDAP_OPERATIONS_ERROR;
  1099. goto bail;
  1100. }
  1101. replica = (Replica*)object_get_data (r);
  1102. if (NULL == replica) {
  1103. rc = LDAP_OPERATIONS_ERROR;
  1104. goto bail;
  1105. }
  1106. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  1107. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1108. "Beginning changelog export of replica \"%s\"\n",
  1109. replica_get_name(replica));
  1110. rc = cl5ExportLDIF (fName, rlist);
  1111. if (rc == CL5_SUCCESS) {
  1112. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1113. "Finished changelog export of replica \"%s\"\n",
  1114. replica_get_name(replica));
  1115. rc = LDAP_SUCCESS;
  1116. } else {
  1117. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1118. "Failed changelog export replica %s; "
  1119. "changelog error - %d", replica_get_name(replica), rc);
  1120. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1121. "replica_execute_cl2ldif_task: %s\n", returntext);
  1122. rc = LDAP_OPERATIONS_ERROR;
  1123. }
  1124. bail:
  1125. slapi_ch_free_string (&clDir);
  1126. return rc;
  1127. }
  1128. static int replica_execute_ldif2cl_task (Object *r, char *returntext)
  1129. {
  1130. int rc, imprc = 0;
  1131. Object *rlist [2];
  1132. Replica *replica;
  1133. char fName [MAXPATHLEN];
  1134. char *clDir = NULL;
  1135. changelog5Config config;
  1136. if (cl5GetState () != CL5_STATE_OPEN)
  1137. {
  1138. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  1139. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1140. "replica_execute_ldif2cl_task: %s\n", returntext);
  1141. rc = LDAP_OPERATIONS_ERROR;
  1142. goto bail;
  1143. }
  1144. rlist[0] = r;
  1145. rlist[1] = NULL;
  1146. /* file is stored in the changelog directory and is named
  1147. <replica name>.ldif */
  1148. clDir = cl5GetDir ();
  1149. if (NULL == clDir) {
  1150. rc = LDAP_OPERATIONS_ERROR;
  1151. goto bail;
  1152. }
  1153. replica = (Replica*)object_get_data (r);
  1154. if (NULL == replica) {
  1155. rc = LDAP_OPERATIONS_ERROR;
  1156. goto bail;
  1157. }
  1158. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  1159. rc = cl5Close();
  1160. if (rc != CL5_SUCCESS)
  1161. {
  1162. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1163. "failed to close changelog to import changelog data; "
  1164. "changelog error - %d", rc);
  1165. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1166. "replica_execute_ldif2cl_task: %s\n", returntext);
  1167. rc = LDAP_OPERATIONS_ERROR;
  1168. goto bail;
  1169. }
  1170. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1171. "Beginning changelog import of replica \"%s\"\n",
  1172. replica_get_name(replica));
  1173. imprc = cl5ImportLDIF (clDir, fName, rlist);
  1174. if (CL5_SUCCESS == imprc)
  1175. {
  1176. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1177. "Finished changelog import of replica \"%s\"\n",
  1178. replica_get_name(replica));
  1179. }
  1180. else
  1181. {
  1182. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1183. "Failed changelog import replica %s; "
  1184. "changelog error - %d", replica_get_name(replica), rc);
  1185. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1186. "replica_execute_ldif2cl_task: %s\n", returntext);
  1187. imprc = LDAP_OPERATIONS_ERROR;
  1188. }
  1189. changelog5_read_config (&config);
  1190. /* restart changelog */
  1191. rc = cl5Open (config.dir, &config.dbconfig);
  1192. if (CL5_SUCCESS == rc)
  1193. {
  1194. rc = LDAP_SUCCESS;
  1195. }
  1196. else
  1197. {
  1198. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1199. "replica_execute_ldif2cl_task: failed to start changelog at %s\n",
  1200. config.dir?config.dir:"null config dir");
  1201. rc = LDAP_OPERATIONS_ERROR;
  1202. }
  1203. bail:
  1204. slapi_ch_free_string(&clDir);
  1205. changelog5_config_done(&config);
  1206. /* if cl5ImportLDIF returned an error, report it first. */
  1207. return imprc?imprc:rc;
  1208. }
  1209. static multimaster_mtnode_extension *
  1210. _replica_config_get_mtnode_ext (const Slapi_Entry *e)
  1211. {
  1212. const char *replica_root;
  1213. Slapi_DN *sdn = NULL;
  1214. mapping_tree_node *mtnode;
  1215. multimaster_mtnode_extension *ext = NULL;
  1216. /* retirve root of the tree for which replica is configured */
  1217. replica_root = slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  1218. if (replica_root == NULL)
  1219. {
  1220. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  1221. "configuration entry %s missing %s attribute\n",
  1222. slapi_entry_get_dn((Slapi_Entry *)e),
  1223. attr_replicaRoot);
  1224. return NULL;
  1225. }
  1226. sdn = slapi_sdn_new_dn_passin (replica_root);
  1227. /* locate mapping tree node for the specified subtree */
  1228. mtnode = slapi_get_mapping_tree_node_by_dn (sdn);
  1229. if (mtnode == NULL)
  1230. {
  1231. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  1232. "failed to locate mapping tree node for dn %s\n",
  1233. slapi_sdn_get_dn(sdn));
  1234. }
  1235. else
  1236. {
  1237. /* check if replica object already exists for the specified subtree */
  1238. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  1239. }
  1240. slapi_sdn_free (&sdn);
  1241. return ext;
  1242. }
  1243. int
  1244. replica_execute_cleanruv_task_ext(Object *r, ReplicaId rid)
  1245. {
  1246. return replica_execute_cleanruv_task(r, rid, NULL);
  1247. }
  1248. static int
  1249. replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext /* not used */)
  1250. {
  1251. Object *RUVObj;
  1252. RUV *local_ruv = NULL;
  1253. Replica *replica = (Replica*)object_get_data (r);
  1254. int rc = 0;
  1255. PR_ASSERT (replica);
  1256. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: cleaning rid (%d)...\n",(int)rid);
  1257. RUVObj = replica_get_ruv(replica);
  1258. PR_ASSERT(RUVObj);
  1259. local_ruv = (RUV*)object_get_data (RUVObj);
  1260. /* Need to check that :
  1261. * - rid is not the local one
  1262. * - rid is not the last one
  1263. */
  1264. if ((replica_get_rid(replica) == rid) ||
  1265. (ruv_replica_count(local_ruv) <= 1)) {
  1266. return LDAP_UNWILLING_TO_PERFORM;
  1267. }
  1268. rc = ruv_delete_replica(local_ruv, rid);
  1269. replica_set_ruv_dirty(replica);
  1270. if (replica_write_ruv(replica)) {
  1271. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: could not write RUV\n");
  1272. }
  1273. object_release(RUVObj);
  1274. /* Update Mapping Tree to reflect RUV changes */
  1275. consumer5_set_mapping_tree_state_for_replica(replica, NULL);
  1276. /*
  1277. * Clean the changelog RUV's
  1278. */
  1279. cl5CleanRUV(rid);
  1280. /*
  1281. * Now purge the changelog
  1282. */
  1283. trigger_cl_purging(rid);
  1284. if (rc != RUV_SUCCESS){
  1285. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanruv_task: task failed(%d)\n",rc);
  1286. return LDAP_OPERATIONS_ERROR;
  1287. }
  1288. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: finished successfully\n");
  1289. return LDAP_SUCCESS;
  1290. }
  1291. const char *
  1292. fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val)
  1293. {
  1294. Slapi_Attr *attr;
  1295. Slapi_Value *val = NULL;
  1296. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  1297. return default_val;
  1298. slapi_attr_first_value(attr, &val);
  1299. return slapi_value_get_string(val);
  1300. }
  1301. static int
  1302. replica_cleanall_ruv_task(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1303. int *returncode, char *returntext, void *arg)
  1304. {
  1305. Slapi_Task *task = NULL;
  1306. const Slapi_DN *task_dn;
  1307. Slapi_DN *dn = NULL;
  1308. ReplicaId rid = -1;
  1309. Object *r;
  1310. const char *force_cleaning;
  1311. const char *base_dn;
  1312. const char *rid_str;
  1313. int rc = SLAPI_DSE_CALLBACK_OK;
  1314. /* allocate new task now */
  1315. task = slapi_new_task(slapi_entry_get_ndn(e));
  1316. task_dn = slapi_entry_get_sdn(e);
  1317. if(task == NULL){
  1318. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV_task: Failed to create new task\n");
  1319. rc = SLAPI_DSE_CALLBACK_ERROR;
  1320. goto out;
  1321. }
  1322. /* register our destructor for waiting the task is done */
  1323. slapi_task_set_destructor_fn(task, replica_cleanall_ruv_destructor);
  1324. /*
  1325. * Get our task settings
  1326. */
  1327. if ((rid_str = fetch_attr(e, "replica-id", 0)) == NULL){
  1328. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing replica-id attribute");
  1329. cleanruv_log(task, -1, CLEANALLRUV_ID, "%s", returntext);
  1330. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1331. rc = SLAPI_DSE_CALLBACK_ERROR;
  1332. goto out;
  1333. }
  1334. rid = atoi(rid_str);
  1335. if ((base_dn = fetch_attr(e, "replica-base-dn", 0)) == NULL){
  1336. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing replica-base-dn attribute");
  1337. cleanruv_log(task, (int)rid, CLEANALLRUV_ID, "%s", returntext);
  1338. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1339. rc = SLAPI_DSE_CALLBACK_ERROR;
  1340. goto out;
  1341. }
  1342. if ((force_cleaning = fetch_attr(e, "replica-force-cleaning", 0)) != NULL){
  1343. if(strcasecmp(force_cleaning,"yes") != 0 && strcasecmp(force_cleaning,"no") != 0){
  1344. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid value for replica-force-cleaning "
  1345. "(%s). Value must be \"yes\" or \"no\" for task - (%s)",
  1346. force_cleaning, slapi_sdn_get_dn(task_dn));
  1347. cleanruv_log(task, (int)rid, CLEANALLRUV_ID, "%s", returntext);
  1348. *returncode = LDAP_OPERATIONS_ERROR;
  1349. rc = SLAPI_DSE_CALLBACK_ERROR;
  1350. goto out;
  1351. }
  1352. } else {
  1353. force_cleaning = "no";
  1354. }
  1355. /*
  1356. * Check the rid
  1357. */
  1358. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  1359. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)",
  1360. rid, slapi_sdn_get_dn(task_dn));
  1361. cleanruv_log(task, rid, CLEANALLRUV_ID, "%s", returntext);
  1362. *returncode = LDAP_OPERATIONS_ERROR;
  1363. rc = SLAPI_DSE_CALLBACK_ERROR;
  1364. goto out;
  1365. }
  1366. if(is_cleaned_rid(rid)){
  1367. /* we are already cleaning this rid */
  1368. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is already being cleaned", rid);
  1369. cleanruv_log(task, rid, CLEANALLRUV_ID, "%s", returntext);
  1370. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1371. rc = SLAPI_DSE_CALLBACK_ERROR;
  1372. goto out;
  1373. }
  1374. /*
  1375. * Get the replica object
  1376. */
  1377. dn = slapi_sdn_new_dn_byval(base_dn);
  1378. if((r = replica_get_replica_from_dn(dn)) == NULL){
  1379. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Could not find replica from dn(%s)",slapi_sdn_get_dn(dn));
  1380. cleanruv_log(task, rid, CLEANALLRUV_ID, "%s", returntext);
  1381. *returncode = LDAP_OPERATIONS_ERROR;
  1382. rc = SLAPI_DSE_CALLBACK_ERROR;
  1383. goto out;
  1384. }
  1385. /* clean the RUV's */
  1386. rc = replica_execute_cleanall_ruv_task (r, rid, task, force_cleaning, returntext);
  1387. out:
  1388. if(rc){
  1389. cleanruv_log(task, rid, CLEANALLRUV_ID, "Task failed...(%d)", rc);
  1390. slapi_task_finish(task, *returncode);
  1391. } else {
  1392. rc = SLAPI_DSE_CALLBACK_OK;
  1393. }
  1394. slapi_sdn_free(&dn);
  1395. return rc;
  1396. }
  1397. /*
  1398. * CLEANALLRUV task
  1399. *
  1400. * [1] Get the maxcsn from the RUV of the rid we want to clean
  1401. * [2] Create the payload for the "cleanallruv" extended ops
  1402. * [3] Create "monitor" thread to do the real work.
  1403. *
  1404. */
  1405. static int
  1406. replica_execute_cleanall_ruv_task (Object *r, ReplicaId rid, Slapi_Task *task, const char* force_cleaning, char *returntext)
  1407. {
  1408. struct berval *payload = NULL;
  1409. Slapi_Task *pre_task = NULL; /* this is supposed to be null for logging */
  1410. cleanruv_data *data = NULL;
  1411. PRThread *thread = NULL;
  1412. CSN *maxcsn = NULL;
  1413. Replica *replica;
  1414. char csnstr[CSN_STRSIZE];
  1415. char *ridstr = NULL;
  1416. char *basedn = NULL;
  1417. int rc = 0;
  1418. cleanruv_log(pre_task, rid, CLEANALLRUV_ID,"Initiating CleanAllRUV Task...");
  1419. if(get_cleanruv_task_count() >= CLEANRIDSIZ){
  1420. /* we are already running the maximum number of tasks */
  1421. cleanruv_log(pre_task, rid, CLEANALLRUV_ID,
  1422. "Exceeded maximum number of active CLEANALLRUV tasks(%d)",CLEANRIDSIZ);
  1423. return LDAP_UNWILLING_TO_PERFORM;
  1424. }
  1425. /*
  1426. * Grab the replica
  1427. */
  1428. if(r){
  1429. replica = (Replica*)object_get_data (r);
  1430. } else {
  1431. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Replica object is NULL, aborting task");
  1432. return -1;
  1433. }
  1434. /*
  1435. * Check if this is a consumer
  1436. */
  1437. if(replica_get_type(replica) == REPLICA_TYPE_READONLY){
  1438. /* this is a consumer, send error */
  1439. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to clean rid (%d), task can not be run on a consumer",rid);
  1440. if(task){
  1441. rc = -1;
  1442. slapi_task_finish(task, rc);
  1443. }
  1444. return -1;
  1445. }
  1446. /*
  1447. * Grab the max csn of the deleted replica
  1448. */
  1449. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Retrieving maxcsn...");
  1450. basedn = (char *)slapi_sdn_get_dn(replica_get_root(replica));
  1451. maxcsn = replica_cleanallruv_find_maxcsn(replica, rid, basedn);
  1452. if(maxcsn == NULL || csn_get_replicaid(maxcsn) == 0){
  1453. /*
  1454. * This is for consistency with extop csn creation, where
  1455. * we want the csn string to be "0000000000000000000" not ""
  1456. */
  1457. csn_free(&maxcsn);
  1458. maxcsn = csn_new();
  1459. csn_init_by_string(maxcsn, "");
  1460. }
  1461. csn_as_string(maxcsn, PR_FALSE, csnstr);
  1462. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Found maxcsn (%s)",csnstr);
  1463. /*
  1464. * Create payload
  1465. */
  1466. ridstr = slapi_ch_smprintf("%d:%s:%s:%s", rid, basedn, csnstr, force_cleaning);
  1467. payload = create_cleanruv_payload(ridstr);
  1468. slapi_ch_free_string(&ridstr);
  1469. if(payload == NULL){
  1470. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to create extended op payload, aborting task");
  1471. rc = -1;
  1472. goto fail;
  1473. }
  1474. /*
  1475. * Launch the cleanallruv thread. Once all the replicas are cleaned it will release the rid
  1476. */
  1477. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  1478. if (data == NULL) {
  1479. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to allocate cleanruv_data. Aborting task.");
  1480. rc = -1;
  1481. goto fail;
  1482. }
  1483. data->repl_obj = r;
  1484. data->replica = replica;
  1485. data->rid = rid;
  1486. data->task = task;
  1487. data->payload = payload;
  1488. data->sdn = NULL;
  1489. data->maxcsn = maxcsn;
  1490. data->repl_root = slapi_ch_strdup(basedn);
  1491. data->force = slapi_ch_strdup(force_cleaning);
  1492. thread = PR_CreateThread(PR_USER_THREAD, replica_cleanallruv_thread,
  1493. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1494. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1495. if (thread == NULL) {
  1496. rc = -1;
  1497. slapi_ch_free_string(&data->force);
  1498. slapi_ch_free_string(&data->repl_root);
  1499. goto fail;
  1500. } else {
  1501. goto done;
  1502. }
  1503. fail:
  1504. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to clean rid (%d)", rid);
  1505. if(task){
  1506. slapi_task_finish(task, rc);
  1507. }
  1508. if(payload){
  1509. ber_bvfree(payload);
  1510. }
  1511. csn_free(&maxcsn);
  1512. if(task) /* only the task acquires the r obj */
  1513. object_release (r);
  1514. done:
  1515. return rc;
  1516. }
  1517. void
  1518. replica_cleanallruv_thread_ext(void *arg)
  1519. {
  1520. replica_cleanallruv_thread(arg);
  1521. }
  1522. /*
  1523. * CLEANALLRUV Thread
  1524. *
  1525. * [1] Wait for the maxcsn to be covered
  1526. * [2] Make sure all the replicas are alive
  1527. * [3] Set the cleaned rid
  1528. * [4] Send the cleanAllRUV extop to all the replicas
  1529. * [5] Manually send the CLEANRUV task to replicas that do not support CLEANALLRUV
  1530. * [6] Wait for all the replicas to be cleaned.
  1531. * [7] Trigger cl trimming, release the rid, and remove all the "cleanallruv" attributes
  1532. * from the config.
  1533. */
  1534. static void
  1535. replica_cleanallruv_thread(void *arg)
  1536. {
  1537. cleanruv_data *data = arg;
  1538. Object *agmt_obj = NULL;
  1539. Object *ruv_obj = NULL;
  1540. Repl_Agmt *agmt = NULL;
  1541. RUV *ruv = NULL;
  1542. char csnstr[CSN_STRSIZE];
  1543. char *returntext = NULL;
  1544. char *rid_text = NULL;
  1545. int agmt_not_notified = 1;
  1546. int found_dirty_rid = 1;
  1547. int interval = 10;
  1548. int free_obj = 0;
  1549. int aborted = 0;
  1550. int rc = 0;
  1551. if (!data) {
  1552. return; /* no data */
  1553. }
  1554. if (data->task) {
  1555. slapi_task_inc_refcount(data->task);
  1556. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_cleanallruv_thread --> refcount incremented.\n");
  1557. }
  1558. /*
  1559. * Initialize our settings
  1560. */
  1561. if(data->replica == NULL && data->repl_obj == NULL){
  1562. /*
  1563. * This thread was initiated at startup because the process did not finish. Due
  1564. * to startup timing issues, we need to wait before grabbing the replica obj, as
  1565. * the backends might not be online yet.
  1566. */
  1567. PR_Lock( notify_lock );
  1568. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(10) );
  1569. PR_Unlock( notify_lock );
  1570. data->repl_obj = replica_get_replica_from_dn(data->sdn);
  1571. if(data->repl_obj == NULL){
  1572. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Unable to retrieve repl object from dn(%s).", data->sdn);
  1573. aborted = 1;
  1574. goto done;
  1575. }
  1576. data->replica = (Replica*)object_get_data(data->repl_obj);
  1577. free_obj = 1;
  1578. } else if(data->replica == NULL && data->repl_obj){
  1579. data->replica = (Replica*)object_get_data(data->repl_obj);
  1580. } else if( data->repl_obj == NULL && data->replica){
  1581. data->repl_obj = object_new(data->replica, NULL);
  1582. free_obj = 1;
  1583. }
  1584. /* verify we have set our repl objects */
  1585. if(data->repl_obj == NULL || data->replica == NULL){
  1586. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Unable to set the replica objects.");
  1587. aborted = 1;
  1588. goto done;
  1589. }
  1590. if(data->repl_root == NULL){
  1591. /* we must have resumed from start up, fill in the repl root */
  1592. data->repl_root = slapi_ch_strdup(slapi_sdn_get_dn(replica_get_root(data->replica)));
  1593. }
  1594. if(data->task){
  1595. slapi_task_begin(data->task, 1);
  1596. }
  1597. /*
  1598. * Presetting the rid prevents duplicate thread creation, but allows the db and changelog to still
  1599. * process updates from the rid. set_cleaned_rid() blocks updates, so we don't want to do that... yet.
  1600. */
  1601. preset_cleaned_rid(data->rid);
  1602. rid_text = slapi_ch_smprintf("%d", data->rid);
  1603. csn_as_string(data->maxcsn, PR_FALSE, csnstr);
  1604. /*
  1605. * Add the cleanallruv task to the repl config - so we can handle restarts
  1606. */
  1607. add_cleaned_rid(data->rid, data->replica, csnstr, data->force); /* marks config that we started cleaning a rid */
  1608. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Cleaning rid (%d)...", data->rid);
  1609. /*
  1610. * First, wait for the maxcsn to be covered
  1611. */
  1612. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Waiting to process all the updates from the deleted replica...");
  1613. ruv_obj = replica_get_ruv(data->replica);
  1614. ruv = object_get_data (ruv_obj);
  1615. while(data->maxcsn && !is_task_aborted(data->rid) && !is_cleaned_rid(data->rid) && !slapi_is_shutting_down()){
  1616. if(csn_get_replicaid(data->maxcsn) == 0 || ruv_covers_csn_cleanallruv(ruv,data->maxcsn) || strcasecmp(data->force,"yes") == 0){
  1617. /* We are caught up, now we can clean the ruv's */
  1618. break;
  1619. }
  1620. PR_Lock( notify_lock );
  1621. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(5) );
  1622. PR_Unlock( notify_lock );
  1623. }
  1624. object_release(ruv_obj);
  1625. /*
  1626. * Next, make sure all the replicas are up and running before sending off the clean ruv tasks
  1627. *
  1628. * Even if we are forcing the cleaning, the replicas still need to be up
  1629. */
  1630. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Waiting for all the replicas to be online...");
  1631. if(check_agmts_are_alive(data->replica, data->rid, data->task)){
  1632. /* error, aborted or shutdown */
  1633. aborted = 1;
  1634. goto done;
  1635. }
  1636. /*
  1637. * Make sure all the replicas have seen the max csn
  1638. */
  1639. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Waiting for all the replicas to receive all the deleted replica updates...");
  1640. if(strcasecmp(data->force,"no") == 0 && check_agmts_are_caught_up(data, csnstr)){
  1641. /* error, aborted or shutdown */
  1642. aborted = 1;
  1643. goto done;
  1644. }
  1645. /*
  1646. * Set the rid as notified - this blocks the changelog from sending out updates
  1647. * during this process, as well as prevents the db ruv from getting polluted.
  1648. */
  1649. set_cleaned_rid(data->rid);
  1650. /*
  1651. * Now send the cleanruv extended op to all the agreements
  1652. */
  1653. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Sending cleanAllRUV task to all the replicas...");
  1654. while(agmt_not_notified && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1655. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1656. if(agmt_obj == NULL){
  1657. /* no agmts, just clean this replica */
  1658. break;
  1659. }
  1660. while (agmt_obj && !slapi_is_shutting_down()){
  1661. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1662. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1663. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1664. agmt_not_notified = 0;
  1665. continue;
  1666. }
  1667. if(replica_cleanallruv_send_extop(agmt, data, 1) == 0){
  1668. agmt_not_notified = 0;
  1669. } else {
  1670. agmt_not_notified = 1;
  1671. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Failed to send task to replica (%s)",agmt_get_long_name(agmt));
  1672. break;
  1673. }
  1674. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1675. }
  1676. if(is_task_aborted(data->rid)){
  1677. aborted = 1;
  1678. goto done;
  1679. }
  1680. if(agmt_not_notified == 0){
  1681. break;
  1682. }
  1683. /*
  1684. * need to sleep between passes
  1685. */
  1686. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Not all replicas have received the "
  1687. "cleanallruv extended op, retrying in %d seconds",interval);
  1688. PR_Lock( notify_lock );
  1689. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1690. PR_Unlock( notify_lock );
  1691. if(interval < 14400){ /* 4 hour max */
  1692. interval = interval * 2;
  1693. } else {
  1694. interval = 14400;
  1695. }
  1696. }
  1697. /*
  1698. * Run the CLEANRUV task
  1699. */
  1700. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Cleaning local ruv's...");
  1701. replica_execute_cleanruv_task (data->repl_obj, data->rid, returntext);
  1702. /*
  1703. * Wait for all the replicas to be cleaned
  1704. */
  1705. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  1706. "Waiting for all the replicas to be cleaned...");
  1707. interval = 10;
  1708. while(found_dirty_rid && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1709. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1710. if(agmt_obj == NULL){
  1711. break;
  1712. }
  1713. while (agmt_obj && !slapi_is_shutting_down()){
  1714. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1715. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1716. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1717. found_dirty_rid = 0;
  1718. continue;
  1719. }
  1720. if(replica_cleanallruv_check_ruv(data->repl_root, agmt, rid_text, data->task) == 0){
  1721. found_dirty_rid = 0;
  1722. } else {
  1723. found_dirty_rid = 1;
  1724. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Replica is not cleaned yet (%s)",
  1725. agmt_get_long_name(agmt));
  1726. break;
  1727. }
  1728. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1729. }
  1730. /* If the task is abort or everyone is cleaned, break out */
  1731. if(is_task_aborted(data->rid)){
  1732. aborted = 1;
  1733. goto done;
  1734. }
  1735. if(found_dirty_rid == 0){
  1736. break;
  1737. }
  1738. /*
  1739. * Need to sleep between passes unless we are shutting down
  1740. */
  1741. if (!slapi_is_shutting_down()){
  1742. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Replicas have not been cleaned yet, "
  1743. "retrying in %d seconds", interval);
  1744. PR_Lock( notify_lock );
  1745. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1746. PR_Unlock( notify_lock );
  1747. }
  1748. if(interval < 14400){ /* 4 hour max */
  1749. interval = interval * 2;
  1750. } else {
  1751. interval = 14400;
  1752. }
  1753. } /* while */
  1754. done:
  1755. /*
  1756. * If the replicas are cleaned, release the rid
  1757. */
  1758. if(!aborted){
  1759. delete_cleaned_rid_config(data);
  1760. /* make sure all the replicas have been "pre_cleaned" before finishing */
  1761. check_replicas_are_done_cleaning(data);
  1762. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Successfully cleaned rid(%d).", data->rid);
  1763. remove_cleaned_rid(data->rid);
  1764. } else {
  1765. /*
  1766. * Shutdown or abort
  1767. */
  1768. if(!is_task_aborted(data->rid) || slapi_is_shutting_down()){
  1769. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Server shutting down. Process will resume at server startup");
  1770. } else {
  1771. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Task aborted for rid(%d).",data->rid);
  1772. delete_cleaned_rid_config(data);
  1773. remove_cleaned_rid(data->rid);
  1774. }
  1775. }
  1776. if(data->task){
  1777. slapi_task_finish(data->task, rc);
  1778. slapi_task_dec_refcount(data->task);
  1779. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_cleanallruv_thread <-- refcount decremented.\n");
  1780. }
  1781. if(data->payload){
  1782. ber_bvfree(data->payload);
  1783. }
  1784. if(data->repl_obj && free_obj){
  1785. object_release(data->repl_obj);
  1786. }
  1787. csn_free(&data->maxcsn);
  1788. slapi_sdn_free(&data->sdn);
  1789. slapi_ch_free_string(&data->repl_root);
  1790. slapi_ch_free_string(&data->force);
  1791. slapi_ch_free_string(&rid_text);
  1792. slapi_ch_free((void **)&data);
  1793. }
  1794. static void
  1795. replica_cleanall_ruv_destructor(Slapi_Task *task)
  1796. {
  1797. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1798. "replica_cleanall_ruv_destructor -->\n" );
  1799. if (task) {
  1800. while (slapi_task_get_refcount(task) > 0) {
  1801. /* Yield to wait for the fixup task finishes. */
  1802. DS_Sleep (PR_MillisecondsToInterval(100));
  1803. }
  1804. }
  1805. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1806. "replica_cleanall_ruv_destructor <--\n" );
  1807. }
  1808. static void
  1809. replica_cleanall_ruv_abort_destructor(Slapi_Task *task)
  1810. {
  1811. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1812. "replica_cleanall_ruv_abort_destructor -->\n" );
  1813. if (task) {
  1814. while (slapi_task_get_refcount(task) > 0) {
  1815. /* Yield to wait for the fixup task finishes. */
  1816. DS_Sleep (PR_MillisecondsToInterval(100));
  1817. }
  1818. }
  1819. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1820. "replica_cleanall_ruv_abort_destructor <--\n" );
  1821. }
  1822. /*
  1823. * Loop over the agmts, and check if they are in the last phase of cleaning, meaning they have
  1824. * released cleanallruv data from the config
  1825. */
  1826. static void
  1827. check_replicas_are_done_cleaning(cleanruv_data *data )
  1828. {
  1829. Object *agmt_obj;
  1830. Repl_Agmt *agmt;
  1831. char csnstr[CSN_STRSIZE];
  1832. char *filter = NULL;
  1833. int not_all_cleaned = 1;
  1834. int interval = 10;
  1835. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Waiting for all the replicas to finish cleaning...");
  1836. csn_as_string(data->maxcsn, PR_FALSE, csnstr);
  1837. filter = PR_smprintf("(%s=%d:%s:%s)", type_replicaCleanRUV,(int)data->rid, csnstr, data->force);
  1838. while(not_all_cleaned && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1839. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1840. if(agmt_obj == NULL){
  1841. not_all_cleaned = 0;
  1842. break;
  1843. }
  1844. while (agmt_obj && !slapi_is_shutting_down()){
  1845. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1846. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1847. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1848. not_all_cleaned = 0;
  1849. continue;
  1850. }
  1851. if(replica_cleanallruv_is_finished(agmt, filter, data->task) == 0){
  1852. not_all_cleaned = 0;
  1853. } else {
  1854. not_all_cleaned = 1;
  1855. break;
  1856. }
  1857. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1858. }
  1859. if(not_all_cleaned == 0 || is_task_aborted(data->rid) ){
  1860. break;
  1861. }
  1862. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Not all replicas finished cleaning, retrying in %d seconds",interval);
  1863. PR_Lock( notify_lock );
  1864. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1865. PR_Unlock( notify_lock );
  1866. if(interval < 14400){ /* 4 hour max */
  1867. interval = interval * 2;
  1868. } else {
  1869. interval = 14400;
  1870. }
  1871. }
  1872. slapi_ch_free_string(&filter);
  1873. }
  1874. /*
  1875. * Search this replica for the nsds5ReplicaCleanruv attribute, we don't return
  1876. * an entry, we know it has finished cleaning.
  1877. */
  1878. static int
  1879. replica_cleanallruv_is_finished(Repl_Agmt *agmt, char *filter, Slapi_Task *task)
  1880. {
  1881. Repl_Connection *conn = NULL;
  1882. ConnResult crc = 0;
  1883. struct berval *payload = NULL;
  1884. int msgid = 0;
  1885. int rc = -1;
  1886. if((conn = conn_new(agmt)) == NULL){
  1887. return rc;
  1888. }
  1889. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  1890. payload = create_cleanruv_payload(filter);
  1891. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_CHECK_STATUS_OID, payload, NULL, &msgid);
  1892. if(crc == CONN_OPERATION_SUCCESS){
  1893. struct berval *retsdata = NULL;
  1894. char *retoid = NULL;
  1895. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  1896. if (CONN_OPERATION_SUCCESS == crc ){
  1897. char *response = NULL;
  1898. decode_cleanruv_payload(retsdata, &response);
  1899. if(response == NULL){
  1900. /* this replica does not support cleanallruv */
  1901. rc = 0;
  1902. } else if(strcmp(response,CLEANRUV_FINISHED) == 0){
  1903. /* finished cleaning */
  1904. rc = 0;
  1905. }
  1906. if (NULL != retsdata)
  1907. ber_bvfree(retsdata);
  1908. slapi_ch_free_string(&response);
  1909. slapi_ch_free_string(&retoid);
  1910. }
  1911. }
  1912. }
  1913. conn_delete_internal_ext(conn);
  1914. if(payload)
  1915. ber_bvfree(payload);
  1916. return rc;
  1917. }
  1918. /*
  1919. * Loop over the agmts, and check if they are in the last phase of aborting, meaning they have
  1920. * released the abort cleanallruv data from the config
  1921. */
  1922. static void
  1923. check_replicas_are_done_aborting(cleanruv_data *data )
  1924. {
  1925. Object *agmt_obj;
  1926. Repl_Agmt *agmt;
  1927. char *filter = NULL;
  1928. int not_all_aborted = 1;
  1929. int interval = 10;
  1930. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Waiting for all the replicas to finish aborting...");
  1931. filter = PR_smprintf("(%s=%d:%s)", type_replicaAbortCleanRUV, data->rid, data->repl_root);
  1932. while(not_all_aborted && !slapi_is_shutting_down()){
  1933. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1934. if(agmt_obj == NULL){
  1935. not_all_aborted = 0;
  1936. break;
  1937. }
  1938. while (agmt_obj && !slapi_is_shutting_down()){
  1939. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1940. if(get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1941. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1942. not_all_aborted = 0;
  1943. continue;
  1944. }
  1945. if(replica_cleanallruv_is_finished(agmt, filter, data->task) == 0){
  1946. not_all_aborted = 0;
  1947. } else {
  1948. not_all_aborted = 1;
  1949. break;
  1950. }
  1951. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1952. }
  1953. if(not_all_aborted == 0){
  1954. break;
  1955. }
  1956. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Not all replicas finished aborting, retrying in %d seconds",interval);
  1957. PR_Lock( notify_lock );
  1958. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1959. PR_Unlock( notify_lock );
  1960. if(interval < 14400){ /* 4 hour max */
  1961. interval = interval * 2;
  1962. } else {
  1963. interval = 14400;
  1964. }
  1965. }
  1966. slapi_ch_free_string(&filter);
  1967. }
  1968. /*
  1969. * Waits for all the repl agmts to be have have the maxcsn. Returns error only on abort or shutdown
  1970. */
  1971. static int
  1972. check_agmts_are_caught_up(cleanruv_data *data, char *maxcsn)
  1973. {
  1974. Object *agmt_obj;
  1975. Repl_Agmt *agmt;
  1976. char *rid_text;
  1977. int not_all_caughtup = 1;
  1978. int interval = 10;
  1979. rid_text = slapi_ch_smprintf("%d", data->rid);
  1980. while(not_all_caughtup && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1981. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1982. if(agmt_obj == NULL){
  1983. not_all_caughtup = 0;
  1984. break;
  1985. }
  1986. while (agmt_obj && !slapi_is_shutting_down()){
  1987. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1988. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1989. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1990. not_all_caughtup = 0;
  1991. continue;
  1992. }
  1993. if(replica_cleanallruv_check_maxcsn(agmt, data->repl_root, rid_text, maxcsn, data->task) == 0){
  1994. not_all_caughtup = 0;
  1995. } else {
  1996. not_all_caughtup = 1;
  1997. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Replica not caught up (%s)",agmt_get_long_name(agmt));
  1998. break;
  1999. }
  2000. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2001. }
  2002. if(not_all_caughtup == 0 || is_task_aborted(data->rid) ){
  2003. break;
  2004. }
  2005. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  2006. "Not all replicas caught up, retrying in %d seconds",interval);
  2007. PR_Lock( notify_lock );
  2008. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2009. PR_Unlock( notify_lock );
  2010. if(interval < 14400){ /* 4 hour max */
  2011. interval = interval * 2;
  2012. } else {
  2013. interval = 14400;
  2014. }
  2015. }
  2016. slapi_ch_free_string(&rid_text);
  2017. if(is_task_aborted(data->rid)){
  2018. return -1;
  2019. }
  2020. return not_all_caughtup;
  2021. }
  2022. /*
  2023. * Waits for all the repl agmts to be online. Returns error only on abort or shutdown
  2024. */
  2025. static int
  2026. check_agmts_are_alive(Replica *replica, ReplicaId rid, Slapi_Task *task)
  2027. {
  2028. Object *agmt_obj;
  2029. Repl_Agmt *agmt;
  2030. int not_all_alive = 1;
  2031. int interval = 10;
  2032. while(not_all_alive && is_task_aborted(rid) == 0 && !slapi_is_shutting_down()){
  2033. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  2034. if(agmt_obj == NULL){
  2035. not_all_alive = 0;
  2036. break;
  2037. }
  2038. while (agmt_obj && !slapi_is_shutting_down()){
  2039. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2040. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2041. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2042. not_all_alive = 0;
  2043. continue;
  2044. }
  2045. if(replica_cleanallruv_replica_alive(agmt) == 0){
  2046. not_all_alive = 0;
  2047. } else {
  2048. not_all_alive = 1;
  2049. cleanruv_log(task, rid, CLEANALLRUV_ID, "Replica not online (%s)",
  2050. agmt_get_long_name(agmt));
  2051. break;
  2052. }
  2053. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2054. }
  2055. if(not_all_alive == 0 || is_task_aborted(rid)){
  2056. break;
  2057. }
  2058. cleanruv_log(task, rid, CLEANALLRUV_ID, "Not all replicas online, retrying in %d seconds...",
  2059. interval);
  2060. PR_Lock( notify_lock );
  2061. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2062. PR_Unlock( notify_lock );
  2063. if(interval < 14400){ /* 4 hour max */
  2064. interval = interval * 2;
  2065. } else {
  2066. interval = 14400;
  2067. }
  2068. }
  2069. if(is_task_aborted(rid)){
  2070. return -1;
  2071. }
  2072. return not_all_alive;
  2073. }
  2074. /*
  2075. * Create the CLEANALLRUV extended op payload
  2076. */
  2077. struct berval *
  2078. create_cleanruv_payload(char *value)
  2079. {
  2080. struct berval *req_data = NULL;
  2081. BerElement *tmp_bere = NULL;
  2082. if ((tmp_bere = der_alloc()) == NULL){
  2083. goto error;
  2084. }
  2085. if (ber_printf(tmp_bere, "{s}", value) == -1){
  2086. goto error;
  2087. }
  2088. if (ber_flatten(tmp_bere, &req_data) == -1){
  2089. goto error;
  2090. }
  2091. goto done;
  2092. error:
  2093. if (NULL != req_data){
  2094. ber_bvfree(req_data);
  2095. req_data = NULL;
  2096. }
  2097. done:
  2098. if (NULL != tmp_bere){
  2099. ber_free(tmp_bere, 1);
  2100. tmp_bere = NULL;
  2101. }
  2102. return req_data;
  2103. }
  2104. /*
  2105. * Manually add the CLEANRUV task to replicas that do not support
  2106. * the CLEANALLRUV task.
  2107. */
  2108. static void
  2109. replica_send_cleanruv_task(Repl_Agmt *agmt, cleanruv_data *clean_data)
  2110. {
  2111. Repl_Connection *conn;
  2112. ConnResult crc = 0;
  2113. LDAP *ld;
  2114. Slapi_DN *sdn;
  2115. struct berval *vals[2];
  2116. struct berval val;
  2117. LDAPMod *mods[2];
  2118. LDAPMod mod;
  2119. char *repl_dn = NULL;
  2120. char data[15];
  2121. int rc;
  2122. if((conn = conn_new(agmt)) == NULL){
  2123. return;
  2124. }
  2125. crc = conn_connect(conn);
  2126. if (CONN_OPERATION_SUCCESS != crc){
  2127. conn_delete_internal_ext(conn);
  2128. return;
  2129. }
  2130. ld = conn_get_ldap(conn);
  2131. if(ld == NULL){
  2132. conn_delete_internal_ext(conn);
  2133. return;
  2134. }
  2135. val.bv_len = PR_snprintf(data, sizeof(data), "CLEANRUV%d", clean_data->rid);
  2136. sdn = agmt_get_replarea(agmt);
  2137. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2138. mod.mod_type = "nsds5task";
  2139. mod.mod_bvalues = vals;
  2140. vals [0] = &val;
  2141. vals [1] = NULL;
  2142. val.bv_val = data;
  2143. mods[0] = &mod;
  2144. mods[1] = NULL;
  2145. repl_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", slapi_sdn_get_dn(sdn));
  2146. /*
  2147. * Add task to remote replica
  2148. */
  2149. rc = ldap_modify_ext_s( ld, repl_dn, mods, NULL, NULL);
  2150. if(rc != LDAP_SUCCESS){
  2151. char *hostname = agmt_get_hostname(agmt);
  2152. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2153. "Failed to add CLEANRUV task (%s) to replica (%s). You will need "
  2154. "to manually run the CLEANRUV task on this replica (%s) error (%d)",
  2155. repl_dn, agmt_get_long_name(agmt), hostname, rc);
  2156. slapi_ch_free_string(&hostname);
  2157. }
  2158. slapi_ch_free_string(&repl_dn);
  2159. slapi_sdn_free(&sdn);
  2160. conn_delete_internal_ext(conn);
  2161. }
  2162. /*
  2163. * Check if the rid is in our list of "cleaned" rids
  2164. */
  2165. int
  2166. is_cleaned_rid(ReplicaId rid)
  2167. {
  2168. int i;
  2169. slapi_rwlock_rdlock(rid_lock);
  2170. for(i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != 0; i++){
  2171. if(rid == cleaned_rids[i]){
  2172. slapi_rwlock_unlock(rid_lock);
  2173. return 1;
  2174. }
  2175. }
  2176. slapi_rwlock_unlock(rid_lock);
  2177. return 0;
  2178. }
  2179. int
  2180. is_pre_cleaned_rid(ReplicaId rid)
  2181. {
  2182. int i;
  2183. slapi_rwlock_rdlock(rid_lock);
  2184. for(i = 0; i < CLEANRIDSIZ && pre_cleaned_rids[i] != 0; i++){
  2185. if(rid == pre_cleaned_rids[i]){
  2186. slapi_rwlock_unlock(rid_lock);
  2187. return 1;
  2188. }
  2189. }
  2190. slapi_rwlock_unlock(rid_lock);
  2191. return 0;
  2192. }
  2193. int
  2194. is_task_aborted(ReplicaId rid)
  2195. {
  2196. int i;
  2197. if(rid == 0){
  2198. return 0;
  2199. }
  2200. slapi_rwlock_rdlock(abort_rid_lock);
  2201. for(i = 0; i < CLEANRIDSIZ && aborted_rids[i] != 0; i++){
  2202. if(rid == aborted_rids[i]){
  2203. slapi_rwlock_unlock(abort_rid_lock);
  2204. return 1;
  2205. }
  2206. }
  2207. slapi_rwlock_unlock(abort_rid_lock);
  2208. return 0;
  2209. }
  2210. static void
  2211. preset_cleaned_rid(ReplicaId rid)
  2212. {
  2213. int i;
  2214. slapi_rwlock_wrlock(rid_lock);
  2215. for(i = 0; i < CLEANRIDSIZ; i++){
  2216. if(pre_cleaned_rids[i] == 0){
  2217. pre_cleaned_rids[i] = rid;
  2218. pre_cleaned_rids[i + 1] = 0;
  2219. break;
  2220. }
  2221. }
  2222. slapi_rwlock_unlock(rid_lock);
  2223. }
  2224. /*
  2225. * Just add the rid to the in memory, as we don't want it to survive after a restart,
  2226. * This prevent the changelog from sending updates from this rid, and the local ruv
  2227. * will not be updated either.
  2228. */
  2229. void
  2230. set_cleaned_rid(ReplicaId rid)
  2231. {
  2232. int i;
  2233. slapi_rwlock_wrlock(rid_lock);
  2234. for(i = 0; i < CLEANRIDSIZ; i++){
  2235. if(cleaned_rids[i] == 0){
  2236. cleaned_rids[i] = rid;
  2237. cleaned_rids[i + 1] = 0;
  2238. }
  2239. }
  2240. slapi_rwlock_unlock(rid_lock);
  2241. }
  2242. /*
  2243. * Add the rid and maxcsn to the repl config (so we can resume after a server restart)
  2244. */
  2245. void
  2246. add_cleaned_rid(ReplicaId rid, Replica *r, char *maxcsn, char *forcing)
  2247. {
  2248. Slapi_PBlock *pb;
  2249. struct berval *vals[2];
  2250. struct berval val;
  2251. LDAPMod *mods[2];
  2252. LDAPMod mod;
  2253. char data[CSN_STRSIZE + 10];
  2254. char *dn;
  2255. int rc;
  2256. if(r == NULL || maxcsn == NULL){
  2257. return;
  2258. }
  2259. /*
  2260. * Write the rid & maxcsn to the config entry
  2261. */
  2262. val.bv_len = PR_snprintf(data, sizeof(data),"%d:%s:%s", rid, maxcsn, forcing);
  2263. dn = replica_get_dn(r);
  2264. pb = slapi_pblock_new();
  2265. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2266. mod.mod_type = (char *)type_replicaCleanRUV;
  2267. mod.mod_bvalues = vals;
  2268. vals [0] = &val;
  2269. vals [1] = NULL;
  2270. val.bv_val = data;
  2271. mods[0] = &mod;
  2272. mods[1] = NULL;
  2273. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  2274. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2275. slapi_modify_internal_pb (pb);
  2276. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2277. if (rc != LDAP_SUCCESS && rc != LDAP_TYPE_OR_VALUE_EXISTS && rc != LDAP_NO_SUCH_OBJECT){
  2278. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: failed to update replica "
  2279. "config (%d), rid (%d)\n", rc, rid);
  2280. }
  2281. slapi_ch_free_string(&dn);
  2282. slapi_pblock_destroy(pb);
  2283. }
  2284. /*
  2285. * Add aborted rid and repl root to config in case of a server restart
  2286. */
  2287. void
  2288. add_aborted_rid(ReplicaId rid, Replica *r, char *repl_root)
  2289. {
  2290. Slapi_PBlock *pb;
  2291. struct berval *vals[2];
  2292. struct berval val;
  2293. LDAPMod *mods[2];
  2294. LDAPMod mod;
  2295. char *data;
  2296. char *dn;
  2297. int rc;
  2298. int i;
  2299. slapi_rwlock_wrlock(abort_rid_lock);
  2300. for(i = 0; i < CLEANRIDSIZ; i++){
  2301. if(aborted_rids[i] == 0){
  2302. aborted_rids[i] = rid;
  2303. aborted_rids[i + 1] = 0;
  2304. break;
  2305. }
  2306. }
  2307. slapi_rwlock_unlock(abort_rid_lock);
  2308. /*
  2309. * Write the rid to the config entry
  2310. */
  2311. dn = replica_get_dn(r);
  2312. pb = slapi_pblock_new();
  2313. data = PR_smprintf("%d:%s", rid, repl_root);
  2314. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2315. mod.mod_type = (char *)type_replicaAbortCleanRUV;
  2316. mod.mod_bvalues = vals;
  2317. vals [0] = &val;
  2318. vals [1] = NULL;
  2319. val.bv_val = data;
  2320. val.bv_len = strlen (data);
  2321. mods[0] = &mod;
  2322. mods[1] = NULL;
  2323. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  2324. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2325. slapi_modify_internal_pb (pb);
  2326. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2327. if (rc != LDAP_SUCCESS && rc != LDAP_TYPE_OR_VALUE_EXISTS && rc != LDAP_NO_SUCH_OBJECT){
  2328. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to update "
  2329. "replica config (%d), rid (%d)\n", rc, rid);
  2330. }
  2331. slapi_ch_free_string(&dn);
  2332. slapi_ch_free_string(&data);
  2333. slapi_pblock_destroy(pb);
  2334. }
  2335. void
  2336. delete_aborted_rid(Replica *r, ReplicaId rid, char *repl_root, int skip){
  2337. Slapi_PBlock *pb;
  2338. LDAPMod *mods[2];
  2339. LDAPMod mod;
  2340. struct berval *vals[2];
  2341. struct berval val;
  2342. char *data;
  2343. char *dn;
  2344. int rc;
  2345. int i;
  2346. if(r == NULL)
  2347. return;
  2348. if(skip){
  2349. /* skip the deleting of the config, and just remove the in memory rid */
  2350. slapi_rwlock_wrlock(abort_rid_lock);
  2351. for(i = 0; i < CLEANRIDSIZ && aborted_rids[i] != rid; i++); /* found rid, stop */
  2352. for(; i < CLEANRIDSIZ; i++){
  2353. /* rewrite entire array */
  2354. aborted_rids[i] = aborted_rids[i + 1];
  2355. }
  2356. slapi_rwlock_unlock(abort_rid_lock);
  2357. } else {
  2358. /* only remove the config, leave the in-memory rid */
  2359. dn = replica_get_dn(r);
  2360. data = PR_smprintf("%d:%s", (int)rid, repl_root);
  2361. mod.mod_op = LDAP_MOD_DELETE|LDAP_MOD_BVALUES;
  2362. mod.mod_type = (char *)type_replicaAbortCleanRUV;
  2363. mod.mod_bvalues = vals;
  2364. vals [0] = &val;
  2365. vals [1] = NULL;
  2366. val.bv_val = data;
  2367. val.bv_len = strlen (data);
  2368. mods[0] = &mod;
  2369. mods[1] = NULL;
  2370. pb = slapi_pblock_new();
  2371. slapi_modify_internal_set_pb(pb, dn, mods, NULL, NULL, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2372. slapi_modify_internal_pb (pb);
  2373. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2374. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2375. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to remove replica "
  2376. "config (%d), rid (%d)\n", rc, rid);
  2377. }
  2378. slapi_pblock_destroy (pb);
  2379. slapi_ch_free_string(&dn);
  2380. slapi_ch_free_string(&data);
  2381. }
  2382. }
  2383. /*
  2384. * Just remove the dse.ldif config, but we need to keep the cleaned rids in memory until we know we are done
  2385. */
  2386. static void
  2387. delete_cleaned_rid_config(cleanruv_data *clean_data)
  2388. {
  2389. Slapi_PBlock *pb, *modpb;
  2390. Slapi_Entry **entries = NULL;
  2391. LDAPMod *mods[2];
  2392. LDAPMod mod;
  2393. struct berval *vals[5]= {0, 0, 0, 0, 0}; /* maximum of 4 tasks */
  2394. struct berval val[5];
  2395. char *iter = NULL;
  2396. char *dn = NULL;
  2397. int i, ii;
  2398. int rc = -1, ret, rid;
  2399. if(clean_data == NULL){
  2400. cleanruv_log(NULL, -1, CLEANALLRUV_ID, "delete_cleaned_rid_config: cleanruv data is NULL, "
  2401. "failed to clean the config.");
  2402. return;
  2403. }
  2404. /*
  2405. * Search the config for the exact attribute value to delete
  2406. */
  2407. pb = slapi_pblock_new();
  2408. if(clean_data->replica){
  2409. dn = replica_get_dn(clean_data->replica);
  2410. } else {
  2411. goto bail;
  2412. }
  2413. slapi_search_internal_set_pb(pb, dn, LDAP_SCOPE_SUBTREE, "nsds5ReplicaCleanRUV=*", NULL, 0, NULL, NULL,
  2414. (void *)plugin_get_default_component_id(), 0);
  2415. slapi_search_internal_pb(pb);
  2416. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2417. if (ret != LDAP_SUCCESS){
  2418. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2419. "delete_cleaned_rid_config: internal search failed(%d).",ret);
  2420. goto bail;
  2421. } else {
  2422. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2423. if (entries == NULL || entries[0] == NULL){
  2424. /*
  2425. * No matching entries!
  2426. */
  2427. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2428. "delete_cleaned_rid_config: failed to find any "
  2429. "entries with nsds5ReplicaCleanRUV under (%s)", dn);
  2430. goto bail;
  2431. } else {
  2432. /*
  2433. * Clean all the matching entries
  2434. */
  2435. for(i = 0; entries[i] != NULL;i++){
  2436. char **attr_val = slapi_entry_attr_get_charray(entries[i], type_replicaCleanRUV);
  2437. char *edn = slapi_entry_get_dn(entries[i]);
  2438. int count = 0;
  2439. for (ii = 0; attr_val && attr_val[ii] && i < 5; ii++){
  2440. /* make a copy to retain the full value after toking */
  2441. char *aval = slapi_ch_strdup(attr_val[ii]);
  2442. rid = atoi(ldap_utf8strtok_r(attr_val[ii], ":", &iter));
  2443. if(rid == clean_data->rid){
  2444. val[count].bv_len = strlen(aval);
  2445. val[count].bv_val = aval;
  2446. vals[count] = &val[count];
  2447. count++;
  2448. } else {
  2449. slapi_ch_free_string(&aval);
  2450. }
  2451. }
  2452. slapi_ch_array_free(attr_val);
  2453. /*
  2454. * Now delete the attribute
  2455. */
  2456. vals[4] = NULL;
  2457. mod.mod_op = LDAP_MOD_DELETE|LDAP_MOD_BVALUES;
  2458. mod.mod_type = (char *)type_replicaCleanRUV;
  2459. mod.mod_bvalues = vals;
  2460. mods[0] = &mod;
  2461. mods[1] = NULL;
  2462. modpb = slapi_pblock_new();
  2463. slapi_modify_internal_set_pb(modpb, edn, mods, NULL, NULL,
  2464. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2465. slapi_modify_internal_pb (modpb);
  2466. slapi_pblock_get(modpb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2467. slapi_pblock_destroy(modpb);
  2468. /* free the attr vals */
  2469. for (ii = 0; ii < count; ii++){
  2470. slapi_ch_free_string(&val[ii].bv_val);
  2471. }
  2472. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2473. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2474. "delete_cleaned_rid_config: failed to remove task data "
  2475. "from (%s) error (%d), rid (%d)", edn, rc, clean_data->rid);
  2476. goto bail;
  2477. }
  2478. }
  2479. }
  2480. }
  2481. bail:
  2482. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2483. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2484. "delete_cleaned_rid_config: failed to remove replica config "
  2485. "(%d), rid (%d)", rc, clean_data->rid);
  2486. }
  2487. slapi_free_search_results_internal(pb);
  2488. slapi_pblock_destroy(pb);
  2489. slapi_ch_free_string(&dn);
  2490. }
  2491. /*
  2492. * Remove the rid from our list, and the config
  2493. */
  2494. void
  2495. remove_cleaned_rid(ReplicaId rid)
  2496. {
  2497. int i;
  2498. /*
  2499. * Remove this rid, and optimize the array
  2500. */
  2501. slapi_rwlock_wrlock(rid_lock);
  2502. for(i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != rid; i++); /* found rid, stop */
  2503. for(; i < CLEANRIDSIZ; i++){
  2504. /* rewrite entire array */
  2505. cleaned_rids[i] = cleaned_rids[i + 1];
  2506. }
  2507. /* now do the preset cleaned rids */
  2508. for(i = 0; i < CLEANRIDSIZ && pre_cleaned_rids[i] != rid; i++); /* found rid, stop */
  2509. for(; i < CLEANRIDSIZ; i++){
  2510. /* rewrite entire array */
  2511. pre_cleaned_rids[i] = pre_cleaned_rids[i + 1];
  2512. }
  2513. slapi_rwlock_unlock(rid_lock);
  2514. }
  2515. /*
  2516. * Abort the CLEANALLRUV task
  2517. */
  2518. int
  2519. replica_cleanall_ruv_abort(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  2520. int *returncode, char *returntext, void *arg)
  2521. {
  2522. struct berval *payload = NULL;
  2523. cleanruv_data *data = NULL;
  2524. PRThread *thread = NULL;
  2525. Slapi_Task *task = NULL;
  2526. Slapi_DN *sdn = NULL;
  2527. Replica *replica;
  2528. ReplicaId rid = -1;
  2529. Object *r;
  2530. const char *certify_all;
  2531. const char *base_dn;
  2532. const char *rid_str;
  2533. char *ridstr = NULL;
  2534. int rc = SLAPI_DSE_CALLBACK_OK;
  2535. if(get_abort_cleanruv_task_count() >= CLEANRIDSIZ){
  2536. /* we are already running the maximum number of tasks */
  2537. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  2538. "Exceeded maximum number of active ABORT CLEANALLRUV tasks(%d)",
  2539. CLEANRIDSIZ);
  2540. cleanruv_log(task, -1, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2541. *returncode = LDAP_OPERATIONS_ERROR;
  2542. return SLAPI_DSE_CALLBACK_ERROR;
  2543. }
  2544. /* allocate new task now */
  2545. task = slapi_new_task(slapi_entry_get_ndn(e));
  2546. /* register our destructor for waiting the task is done */
  2547. slapi_task_set_destructor_fn(task, replica_cleanall_ruv_abort_destructor);
  2548. /*
  2549. * Get our task settings
  2550. */
  2551. if ((rid_str = fetch_attr(e, "replica-id", 0)) == NULL){
  2552. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing required attr \"replica-id\"");
  2553. cleanruv_log(task, -1, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2554. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2555. rc = SLAPI_DSE_CALLBACK_ERROR;
  2556. goto out;
  2557. }
  2558. certify_all = fetch_attr(e, "replica-certify-all", 0);
  2559. /*
  2560. * Check the rid
  2561. */
  2562. rid = atoi(rid_str);
  2563. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  2564. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)",
  2565. rid, slapi_sdn_get_dn(slapi_entry_get_sdn(e)));
  2566. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"%s", returntext);
  2567. *returncode = LDAP_OPERATIONS_ERROR;
  2568. rc = SLAPI_DSE_CALLBACK_ERROR;
  2569. goto out;
  2570. }
  2571. if ((base_dn = fetch_attr(e, "replica-base-dn", 0)) == NULL){
  2572. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing required attr \"replica-base-dn\"");
  2573. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2574. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2575. rc = SLAPI_DSE_CALLBACK_ERROR;
  2576. goto out;
  2577. }
  2578. if(!is_cleaned_rid(rid) && !is_pre_cleaned_rid(rid)){
  2579. /* we are not cleaning this rid */
  2580. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is not being cleaned, nothing to abort.", rid);
  2581. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2582. *returncode = LDAP_UNWILLING_TO_PERFORM;
  2583. rc = SLAPI_DSE_CALLBACK_ERROR;
  2584. goto out;
  2585. }
  2586. if(is_task_aborted(rid)){
  2587. /* we are already aborting this rid */
  2588. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is already being aborted", rid);
  2589. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2590. *returncode = LDAP_UNWILLING_TO_PERFORM;
  2591. rc = SLAPI_DSE_CALLBACK_ERROR;
  2592. goto out;
  2593. }
  2594. /*
  2595. * Get the replica object
  2596. */
  2597. sdn = slapi_sdn_new_dn_byval(base_dn);
  2598. if((r = replica_get_replica_from_dn(sdn)) == NULL){
  2599. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Failed to find replica from dn(%s)", base_dn);
  2600. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2601. *returncode = LDAP_OPERATIONS_ERROR;
  2602. rc = SLAPI_DSE_CALLBACK_ERROR;
  2603. goto out;
  2604. }
  2605. /*
  2606. * Check certify value
  2607. */
  2608. if(certify_all){
  2609. if(strcasecmp(certify_all,"yes") && strcasecmp(certify_all,"no")){
  2610. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid value for \"replica-certify-all\", the value "
  2611. "must be \"yes\" or \"no\".");
  2612. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2613. *returncode = LDAP_OPERATIONS_ERROR;
  2614. rc = SLAPI_DSE_CALLBACK_ERROR;
  2615. goto out;
  2616. }
  2617. } else {
  2618. /*
  2619. * The default should be not to certify all the replicas, because
  2620. * we might be trying to abort a clean task that is "hanging" due
  2621. * to unreachable replicas. If the default is "yes" then the abort
  2622. * task will run into the same issue.
  2623. */
  2624. certify_all = "no";
  2625. }
  2626. /*
  2627. * Create payload
  2628. */
  2629. ridstr = slapi_ch_smprintf("%d:%s:%s", rid, base_dn, certify_all);
  2630. payload = create_cleanruv_payload(ridstr);
  2631. if(payload == NULL){
  2632. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "Failed to create extended op payload, aborting task");
  2633. *returncode = LDAP_OPERATIONS_ERROR;
  2634. rc = SLAPI_DSE_CALLBACK_ERROR;
  2635. goto out;
  2636. }
  2637. /*
  2638. * Stop the cleaning, and delete the rid
  2639. */
  2640. replica = (Replica*)object_get_data (r);
  2641. add_aborted_rid(rid, replica, (char *)base_dn);
  2642. stop_ruv_cleaning();
  2643. /*
  2644. * Prepare the abort struct and fire off the thread
  2645. */
  2646. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  2647. if (data == NULL) {
  2648. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"Failed to allocate abort_cleanruv_data. Aborting task.");
  2649. *returncode = LDAP_OPERATIONS_ERROR;
  2650. rc = SLAPI_DSE_CALLBACK_ERROR;
  2651. goto out;
  2652. }
  2653. data->repl_obj = r; /* released in replica_abort_task_thread() */
  2654. data->replica = replica;
  2655. data->task = task;
  2656. data->payload = payload;
  2657. data->rid = rid;
  2658. data->repl_root = slapi_ch_strdup(base_dn);
  2659. data->sdn = NULL;
  2660. data->certify = slapi_ch_strdup(certify_all);
  2661. thread = PR_CreateThread(PR_USER_THREAD, replica_abort_task_thread,
  2662. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2663. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2664. if (thread == NULL) {
  2665. object_release(r);
  2666. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"Unable to create abort thread. Aborting task.");
  2667. *returncode = LDAP_OPERATIONS_ERROR;
  2668. slapi_ch_free_string(&data->certify);
  2669. rc = SLAPI_DSE_CALLBACK_ERROR;
  2670. }
  2671. out:
  2672. slapi_ch_free_string(&ridstr);
  2673. slapi_sdn_free(&sdn);
  2674. if(rc != SLAPI_DSE_CALLBACK_OK){
  2675. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "Abort Task failed (%d)", rc);
  2676. slapi_task_finish(task, rc);
  2677. }
  2678. return rc;
  2679. }
  2680. /*
  2681. * Abort CLEANALLRUV task thread
  2682. */
  2683. void
  2684. replica_abort_task_thread(void *arg)
  2685. {
  2686. cleanruv_data *data = (cleanruv_data *)arg;
  2687. Repl_Agmt *agmt;
  2688. Object *agmt_obj;
  2689. int agmt_not_notified = 1;
  2690. int interval = 10;
  2691. int release_it = 0;
  2692. int count = 0, rc = 0;
  2693. if (!data) {
  2694. return; /* no data */
  2695. }
  2696. if (data->task) {
  2697. slapi_task_inc_refcount(data->task);
  2698. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_abort_task_thread --> refcount incremented.\n");
  2699. }
  2700. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Aborting task for rid(%d)...",data->rid);
  2701. /*
  2702. * Need to build the replica from the dn
  2703. */
  2704. if(data->replica == NULL && data->repl_obj == NULL){
  2705. /*
  2706. * This thread was initiated at startup because the process did not finish. Due
  2707. * to timing issues, we need to wait to grab the replica obj until we get here.
  2708. */
  2709. if((data->repl_obj = replica_get_replica_from_dn(data->sdn)) == NULL){
  2710. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Failed to get replica object from dn (%s).", slapi_sdn_get_dn(data->sdn));
  2711. goto done;
  2712. }
  2713. if(data->replica == NULL && data->repl_obj){
  2714. data->replica = (Replica*)object_get_data(data->repl_obj);
  2715. }
  2716. release_it = 1;
  2717. }
  2718. /*
  2719. * Now send the cleanruv extended op to all the agreements
  2720. */
  2721. while(agmt_not_notified && !slapi_is_shutting_down()){
  2722. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  2723. if(agmt_obj == NULL){
  2724. agmt_not_notified = 0;
  2725. break;
  2726. }
  2727. while (agmt_obj){
  2728. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2729. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2730. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2731. agmt_not_notified = 0;
  2732. continue;
  2733. }
  2734. if(replica_cleanallruv_send_abort_extop(agmt, data->task, data->payload)){
  2735. if(strcasecmp(data->certify,"yes") == 0){
  2736. /* we are verifying all the replicas receive the abort task */
  2737. agmt_not_notified = 1;
  2738. break;
  2739. } else {
  2740. /* we do not care if we could not reach a replica, just continue as if we did */
  2741. agmt_not_notified = 0;
  2742. }
  2743. } else {
  2744. /* success */
  2745. agmt_not_notified = 0;
  2746. }
  2747. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2748. } /* while loop for agmts */
  2749. if(agmt_not_notified == 0){
  2750. /* everybody has been contacted */
  2751. break;
  2752. }
  2753. /*
  2754. * Need to sleep between passes. unless we are shutting down
  2755. */
  2756. if (!slapi_is_shutting_down()){
  2757. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Retrying in %d seconds",interval);
  2758. PR_Lock( notify_lock );
  2759. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2760. PR_Unlock( notify_lock );
  2761. }
  2762. if(interval < 14400){ /* 4 hour max */
  2763. interval = interval * 2;
  2764. } else {
  2765. interval = 14400;
  2766. }
  2767. } /* while */
  2768. done:
  2769. if(agmt_not_notified){
  2770. /* failure */
  2771. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Abort task failed, will resume the task at the next server startup.");
  2772. } else {
  2773. /*
  2774. * Wait for this server to stop its cleanallruv task(which removes the rid from the cleaned list)
  2775. */
  2776. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Waiting for CleanAllRUV task to abort...");
  2777. while(is_cleaned_rid(data->rid) && !slapi_is_shutting_down()){
  2778. DS_Sleep(PR_SecondsToInterval(1));
  2779. count++;
  2780. if(count == 60){ /* it should not take this long */
  2781. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "CleanAllRUV task failed to abort. You might need to "
  2782. "rerun the task.");
  2783. rc = -1;
  2784. break;
  2785. }
  2786. }
  2787. /*
  2788. * Clean up the config
  2789. */
  2790. delete_aborted_rid(data->replica, data->rid, data->repl_root, 1); /* delete just the config, leave rid in memory */
  2791. if(strcasecmp(data->certify, "yes") == 0){
  2792. check_replicas_are_done_aborting(data);
  2793. }
  2794. delete_aborted_rid(data->replica, data->rid, data->repl_root, 0); /* remove the in-memory aborted rid */
  2795. if(rc == 0){
  2796. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Successfully aborted task for rid(%d)", data->rid);
  2797. } else {
  2798. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Failed to abort task for rid(%d)",data->rid);
  2799. }
  2800. }
  2801. if(data->task){
  2802. slapi_task_finish(data->task, agmt_not_notified);
  2803. slapi_task_dec_refcount(data->task);
  2804. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_abort_task_thread <-- refcount incremented.\n");
  2805. }
  2806. if(data->repl_obj && release_it)
  2807. object_release(data->repl_obj);
  2808. if(data->payload){
  2809. ber_bvfree(data->payload);
  2810. }
  2811. slapi_ch_free_string(&data->repl_root);
  2812. slapi_ch_free_string(&data->certify);
  2813. slapi_sdn_free(&data->sdn);
  2814. slapi_ch_free((void **)&data);
  2815. }
  2816. static int
  2817. replica_cleanallruv_send_abort_extop(Repl_Agmt *ra, Slapi_Task *task, struct berval *payload)
  2818. {
  2819. Repl_Connection *conn = NULL;
  2820. ConnResult crc = 0;
  2821. int msgid = 0;
  2822. int rc = -1;
  2823. if((conn = conn_new(ra)) == NULL){
  2824. return rc;
  2825. }
  2826. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2827. crc = conn_send_extended_operation(conn, REPL_ABORT_CLEANRUV_OID, payload, NULL, &msgid);
  2828. /*
  2829. * success or failure, just return the error code
  2830. */
  2831. rc = crc;
  2832. if(rc){
  2833. cleanruv_log(task, agmt_get_consumer_rid(ra, conn), ABORT_CLEANALLRUV_ID,
  2834. "Failed to send extop to replica(%s).", agmt_get_long_name(ra));
  2835. }
  2836. } else {
  2837. cleanruv_log(task, agmt_get_consumer_rid(ra, conn), ABORT_CLEANALLRUV_ID,
  2838. "Failed to connect to replica(%s).", agmt_get_long_name(ra));
  2839. rc = -1;
  2840. }
  2841. conn_delete_internal_ext(conn);
  2842. return rc;
  2843. }
  2844. static int
  2845. replica_cleanallruv_send_extop(Repl_Agmt *ra, cleanruv_data *clean_data, int check_result)
  2846. {
  2847. Repl_Connection *conn = NULL;
  2848. ConnResult crc = 0;
  2849. int msgid = 0;
  2850. int rc = -1;
  2851. if((conn = conn_new(ra)) == NULL){
  2852. return rc;
  2853. }
  2854. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2855. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_OID, clean_data->payload, NULL, &msgid);
  2856. if(crc == CONN_OPERATION_SUCCESS && check_result){
  2857. struct berval *retsdata = NULL;
  2858. char *retoid = NULL;
  2859. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  2860. if (CONN_OPERATION_SUCCESS == crc ){
  2861. char *response = NULL;
  2862. decode_cleanruv_payload(retsdata, &response);
  2863. if(response && strcmp(response,CLEANRUV_ACCEPTED) == 0){
  2864. /* extop was accepted */
  2865. rc = 0;
  2866. } else {
  2867. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,"Replica %s does not support the CLEANALLRUV task. "
  2868. "Sending replica CLEANRUV task...", slapi_sdn_get_dn(agmt_get_dn_byref(ra)));
  2869. /*
  2870. * Ok, this replica doesn't know about CLEANALLRUV, so just manually
  2871. * add the CLEANRUV task to the replica.
  2872. */
  2873. replica_send_cleanruv_task(ra, clean_data);
  2874. rc = 0;
  2875. }
  2876. if (NULL != retsdata)
  2877. ber_bvfree(retsdata);
  2878. slapi_ch_free_string(&retoid);
  2879. slapi_ch_free_string(&response);
  2880. }
  2881. } else {
  2882. /*
  2883. * success or failure, just return the error code
  2884. */
  2885. rc = crc;
  2886. }
  2887. }
  2888. conn_delete_internal_ext(conn);
  2889. return rc;
  2890. }
  2891. static CSN*
  2892. replica_cleanallruv_find_maxcsn(Replica *replica, ReplicaId rid, char *basedn)
  2893. {
  2894. Object *agmt_obj;
  2895. Repl_Agmt *agmt;
  2896. CSN *maxcsn = NULL, *topcsn = NULL;
  2897. char *rid_text = slapi_ch_smprintf("%d", rid);
  2898. char *csnstr = NULL;
  2899. /* start with the local maxcsn */
  2900. csnstr = replica_cleanallruv_get_local_maxcsn(rid, basedn);
  2901. if(csnstr){
  2902. topcsn = csn_new();
  2903. csn_init_by_string(topcsn, csnstr);
  2904. slapi_ch_free_string(&csnstr);
  2905. }
  2906. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  2907. if(agmt_obj == NULL){ /* no agreements */
  2908. goto done;
  2909. }
  2910. while (agmt_obj && !slapi_is_shutting_down()){
  2911. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2912. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2913. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2914. continue;
  2915. }
  2916. if(replica_cleanallruv_get_replica_maxcsn(agmt, rid_text, basedn, &maxcsn) == 0){
  2917. if(maxcsn == NULL){
  2918. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2919. continue;
  2920. }
  2921. if(topcsn == NULL){
  2922. topcsn = maxcsn;
  2923. } else {
  2924. if(csn_compare(topcsn, maxcsn) < 0){
  2925. csn_free(&topcsn);
  2926. topcsn = maxcsn;
  2927. } else {
  2928. csn_free(&maxcsn);
  2929. }
  2930. }
  2931. }
  2932. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2933. }
  2934. done:
  2935. slapi_ch_free_string(&rid_text);
  2936. return topcsn;
  2937. }
  2938. static int
  2939. replica_cleanallruv_get_replica_maxcsn(Repl_Agmt *agmt, char *rid_text, char *basedn, CSN **csn)
  2940. {
  2941. Repl_Connection *conn = NULL;
  2942. ConnResult crc = -1;
  2943. struct berval *payload = NULL;
  2944. CSN *maxcsn = NULL;
  2945. char *data = NULL;
  2946. int msgid = 0;
  2947. if((conn = conn_new(agmt)) == NULL){
  2948. return -1;
  2949. }
  2950. data = slapi_ch_smprintf("%s:%s",rid_text, basedn);
  2951. payload = create_cleanruv_payload(data);
  2952. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2953. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  2954. if(crc == CONN_OPERATION_SUCCESS){
  2955. struct berval *retsdata = NULL;
  2956. char *retoid = NULL;
  2957. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  2958. if (CONN_OPERATION_SUCCESS == crc ){
  2959. char *remote_maxcsn = NULL;
  2960. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  2961. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  2962. maxcsn = csn_new();
  2963. csn_init_by_string(maxcsn, remote_maxcsn);
  2964. *csn = maxcsn;
  2965. } else {
  2966. /* no csn */
  2967. *csn = NULL;
  2968. }
  2969. slapi_ch_free_string(&retoid);
  2970. slapi_ch_free_string(&remote_maxcsn);
  2971. if (NULL != retsdata)
  2972. ber_bvfree(retsdata);
  2973. }
  2974. }
  2975. }
  2976. conn_delete_internal_ext(conn);
  2977. slapi_ch_free_string(&data);
  2978. if(payload)
  2979. ber_bvfree(payload);
  2980. return (int)crc;
  2981. }
  2982. static int
  2983. replica_cleanallruv_check_maxcsn(Repl_Agmt *agmt, char *basedn, char *rid_text, char *maxcsn, Slapi_Task *task)
  2984. {
  2985. Repl_Connection *conn = NULL;
  2986. ConnResult crc = 0;
  2987. struct berval *payload = NULL;
  2988. char *data = NULL;
  2989. int msgid = 0;
  2990. int rc = -1;
  2991. if((conn = conn_new(agmt)) == NULL){
  2992. return -1;
  2993. }
  2994. data = slapi_ch_smprintf("%s:%s",rid_text, basedn);
  2995. payload = create_cleanruv_payload(data);
  2996. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2997. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  2998. if(crc == CONN_OPERATION_SUCCESS){
  2999. struct berval *retsdata = NULL;
  3000. char *retoid = NULL;
  3001. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3002. if (CONN_OPERATION_SUCCESS == crc ){
  3003. char *remote_maxcsn = NULL;
  3004. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3005. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3006. CSN *max, *repl_max;
  3007. max = csn_new();
  3008. repl_max = csn_new();
  3009. csn_init_by_string(max, maxcsn);
  3010. csn_init_by_string(repl_max, remote_maxcsn);
  3011. if(csn_compare (repl_max, max) < 0){
  3012. /* we are not caught up yet, free, and return */
  3013. cleanruv_log(task, atoi(rid_text), CLEANALLRUV_ID,"Replica maxcsn (%s) is not caught up with deleted replica's maxcsn(%s)",
  3014. remote_maxcsn, maxcsn);
  3015. rc = -1;
  3016. } else {
  3017. /* ok this replica is caught up */
  3018. rc = 0;
  3019. }
  3020. csn_free(&max);
  3021. csn_free(&repl_max);
  3022. } else {
  3023. /* no remote_maxcsn - return success */
  3024. rc = 0;
  3025. }
  3026. slapi_ch_free_string(&retoid);
  3027. slapi_ch_free_string(&remote_maxcsn);
  3028. if (NULL != retsdata)
  3029. ber_bvfree(retsdata);
  3030. }
  3031. }
  3032. }
  3033. conn_delete_internal_ext(conn);
  3034. slapi_ch_free_string(&data);
  3035. if(payload)
  3036. ber_bvfree(payload);
  3037. return rc;
  3038. }
  3039. static int
  3040. replica_cleanallruv_replica_alive(Repl_Agmt *agmt)
  3041. {
  3042. Repl_Connection *conn = NULL;
  3043. LDAP *ld = NULL;
  3044. LDAPMessage *result = NULL;
  3045. int rc = -1;
  3046. if((conn = conn_new(agmt)) == NULL){
  3047. return rc;
  3048. }
  3049. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3050. ld = conn_get_ldap(conn);
  3051. if(ld == NULL){
  3052. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "CleanAllRUV_task: failed to get LDAP "
  3053. "handle from the replication agmt (%s). Moving on to the next agmt.\n",agmt_get_long_name(agmt));
  3054. conn_delete_internal_ext(conn);
  3055. return -1;
  3056. }
  3057. if(ldap_search_ext_s(ld, "", LDAP_SCOPE_BASE, "objectclass=top",
  3058. NULL, 0, NULL, NULL, NULL, 0, &result) == LDAP_SUCCESS)
  3059. {
  3060. rc = 0;
  3061. } else {
  3062. rc = -1;
  3063. }
  3064. if(result)
  3065. ldap_msgfree( result );
  3066. }
  3067. conn_delete_internal_ext(conn);
  3068. return rc;
  3069. }
  3070. static int
  3071. replica_cleanallruv_check_ruv(char *repl_root, Repl_Agmt *agmt, char *rid_text, Slapi_Task *task)
  3072. {
  3073. Repl_Connection *conn = NULL;
  3074. ConnResult crc = 0;
  3075. struct berval *payload = NULL;
  3076. char *data = NULL;
  3077. int msgid = 0;
  3078. int rc = -1;
  3079. if((conn = conn_new(agmt)) == NULL){
  3080. return rc;
  3081. }
  3082. data = slapi_ch_smprintf("%s:%s",rid_text, repl_root);
  3083. payload = create_cleanruv_payload(data);
  3084. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3085. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  3086. if(crc == CONN_OPERATION_SUCCESS){
  3087. struct berval *retsdata = NULL;
  3088. char *retoid = NULL;
  3089. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3090. if (CONN_OPERATION_SUCCESS == crc ){
  3091. char *remote_maxcsn = NULL;
  3092. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3093. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3094. /* remote replica still has dirty RUV element */
  3095. rc = -1;
  3096. } else {
  3097. /* no maxcsn = we're clean */
  3098. rc = 0;
  3099. }
  3100. slapi_ch_free_string(&retoid);
  3101. slapi_ch_free_string(&remote_maxcsn);
  3102. if (NULL != retsdata)
  3103. ber_bvfree(retsdata);
  3104. }
  3105. }
  3106. }
  3107. conn_delete_internal_ext(conn);
  3108. slapi_ch_free_string(&data);
  3109. if(payload)
  3110. ber_bvfree(payload);
  3111. return rc;
  3112. }
  3113. static int
  3114. get_cleanruv_task_count()
  3115. {
  3116. int i, count = 0;
  3117. slapi_rwlock_wrlock(rid_lock);
  3118. for(i = 0; i < CLEANRIDSIZ; i++){
  3119. if(pre_cleaned_rids[i] != 0){
  3120. count++;
  3121. }
  3122. }
  3123. slapi_rwlock_unlock(rid_lock);
  3124. return count;
  3125. }
  3126. static int
  3127. get_abort_cleanruv_task_count()
  3128. {
  3129. int i, count = 0;
  3130. slapi_rwlock_wrlock(rid_lock);
  3131. for(i = 0; i < CLEANRIDSIZ; i++){
  3132. if(aborted_rids[i] != 0){
  3133. count++;
  3134. }
  3135. }
  3136. slapi_rwlock_unlock(rid_lock);
  3137. return count;
  3138. }
  3139. /*
  3140. * Notify sleeping CLEANALLRUV threads to stop
  3141. */
  3142. void
  3143. stop_ruv_cleaning()
  3144. {
  3145. if(notify_lock){
  3146. PR_Lock( notify_lock );
  3147. PR_NotifyCondVar( notify_cvar );
  3148. PR_Unlock( notify_lock );
  3149. }
  3150. }
  3151. /*
  3152. * Write our logging to the task and error log
  3153. */
  3154. void
  3155. cleanruv_log(Slapi_Task *task, int rid, char *task_type, char *fmt, ...)
  3156. {
  3157. va_list ap1;
  3158. va_list ap2;
  3159. va_list ap3;
  3160. va_list ap4;
  3161. char *errlog_fmt;
  3162. va_start(ap1, fmt);
  3163. va_start(ap2, fmt);
  3164. va_start(ap3, fmt);
  3165. va_start(ap4, fmt);
  3166. if(task){
  3167. slapi_task_log_notice_ext(task, fmt, ap1);
  3168. slapi_task_log_status_ext(task, fmt, ap2);
  3169. slapi_task_inc_progress(task);
  3170. }
  3171. errlog_fmt = PR_smprintf("%s (rid %d): %s \n",task_type, rid, fmt);
  3172. slapi_log_error_ext(SLAPI_LOG_FATAL, repl_plugin_name, errlog_fmt, ap3, ap4);
  3173. slapi_ch_free_string(&errlog_fmt);
  3174. va_end(ap1);
  3175. va_end(ap2);
  3176. va_end(ap3);
  3177. va_end(ap4);
  3178. }
  3179. char *
  3180. replica_cleanallruv_get_local_maxcsn(ReplicaId rid, char *base_dn)
  3181. {
  3182. Slapi_PBlock *search_pb = NULL;
  3183. Slapi_Entry **entries = NULL;
  3184. char **ruv_elements = NULL;
  3185. char *maxcsn = NULL;
  3186. char *filter = NULL;
  3187. char *ridstr = NULL;
  3188. char *iter = NULL;
  3189. char *attrs[2];
  3190. char *ruv_part = NULL;
  3191. int part_count = 0;
  3192. int res, i;
  3193. /*
  3194. * Get the maxruv from the database tombstone entry
  3195. */
  3196. filter = "(&(nsuniqueid=ffffffff-ffffffff-ffffffff-ffffffff)(objectclass=nstombstone))";
  3197. attrs[0] = "nsds50ruv";
  3198. attrs[1] = NULL;
  3199. ridstr = slapi_ch_smprintf("{replica %d ldap", rid);
  3200. search_pb = slapi_pblock_new();
  3201. 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);
  3202. slapi_search_internal_pb (search_pb);
  3203. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  3204. if ( LDAP_SUCCESS == res ) {
  3205. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  3206. if (NULL == entries || entries[0] == NULL) {
  3207. /* Hmmm, no tombstone! Error out */
  3208. } else {
  3209. /* find the right ruv element, and find the maxcsn */
  3210. ruv_elements = slapi_entry_attr_get_charray(entries[0],attrs[0]);
  3211. for(i = 0; ruv_elements && ruv_elements[i] ; i++){
  3212. if(strstr(ruv_elements[i], ridstr)){
  3213. /* get the max csn */
  3214. ruv_part = ldap_utf8strtok_r(ruv_elements[i], " ", &iter);
  3215. for(part_count = 1; ruv_part && part_count < 5; part_count++){
  3216. ruv_part = ldap_utf8strtok_r(iter, " ", &iter);
  3217. }
  3218. if(part_count == 5 && ruv_part){/* we have the maxcsn */
  3219. maxcsn = slapi_ch_strdup(ruv_part);
  3220. break;
  3221. }
  3222. }
  3223. }
  3224. slapi_ch_array_free(ruv_elements);
  3225. }
  3226. } else {
  3227. /* internal search failed */
  3228. cleanruv_log(NULL, (int)rid, CLEANALLRUV_ID, "replica_cleanallruv_get_local_maxcsn: internal search failed (%d)\n", res);
  3229. }
  3230. slapi_free_search_results_internal(search_pb);
  3231. slapi_pblock_destroy(search_pb);
  3232. slapi_ch_free_string(&ridstr);
  3233. return maxcsn;
  3234. }