repl5_replica_config.c 128 KB

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