repl5_replica_config.c 92 KB

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