repl5_agmt.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098
  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_agmt.c */
  42. /*
  43. Support for 5.0-style replication agreements.
  44. Directory Server 5.0 replication agreements contain information about
  45. replication consumers that we are supplying.
  46. This module encapsulates the methods available for adding, deleting,
  47. modifying, and firing replication agreements.
  48. Methods:
  49. agmt_new - Create a new replication agreement, in response to a new
  50. replication agreement being added over LDAP.
  51. agmt_delete - Destroy an agreement. It is an error to destroy an
  52. agreement that has not been stopped.
  53. agmt_getstatus - get the status of this replication agreement.
  54. agmt_replicate_now - initiate a replication session asap, even if the
  55. schedule says we shouldn't.
  56. agmt_start - start replicating, according to schedule. Starts a new
  57. thread to handle replication.
  58. agmt_stop - stop replicating asap and end replication thread.
  59. agmt_notify_change - notify the replication agreement about a change that
  60. has been logged. The replication agreement will
  61. decide if it needs to take some action, e.g. start a
  62. replication session.
  63. agmt_initialize_replica - start a complete replica refresh.
  64. agmt_set_schedule_from_entry - (re)set the schedule associated with this
  65. replication agreement based on a RA entry's contents.
  66. agmt_set_credentials_from_entry - (re)set the credentials used to bind
  67. to the remote replica.
  68. agmt_set_binddn_from_entry - (re)set the DN used to bind
  69. to the remote replica.
  70. agmt_set_bind_method_from_entry - (re)set the bind method used to bind
  71. to the remote replica (SIMPLE or SSLCLIENTAUTH).
  72. agmt_set_transportinfo_from_entry - (re)set the transport used to bind
  73. to the remote replica (SSL or not)
  74. */
  75. #include "repl5.h"
  76. #include "repl5_prot_private.h"
  77. #include "cl5_api.h"
  78. #include "slapi-plugin.h"
  79. #define DEFAULT_TIMEOUT 600 /* (seconds) default outbound LDAP connection */
  80. #define STATUS_LEN 1024
  81. struct changecounter {
  82. ReplicaId rid;
  83. PRUint32 num_replayed;
  84. PRUint32 num_skipped;
  85. };
  86. typedef struct repl5agmt {
  87. char *hostname; /* remote hostname */
  88. int port; /* port of remote server */
  89. PRUint32 transport_flags; /* SSL, TLS, etc. */
  90. char *binddn; /* DN to bind as */
  91. struct berval *creds; /* Password, or certificate */
  92. int bindmethod; /* Bind method - simple, SSL */
  93. Slapi_DN *replarea; /* DN of replicated area */
  94. char **frac_attrs; /* list of fractional attributes to be replicated */
  95. char **frac_attrs_total; /* list of fractional attributes to be replicated for total update protocol */
  96. Schedule *schedule; /* Scheduling information */
  97. int auto_initialize; /* 1 = automatically re-initialize replica */
  98. const Slapi_DN *dn; /* DN of replication agreement entry */
  99. const Slapi_RDN *rdn; /* RDN of replication agreement entry */
  100. char *long_name; /* Long name (rdn + host, port) of entry, for logging */
  101. Repl_Protocol *protocol; /* Protocol object - manages protocol */
  102. struct changecounter *changecounters[MAX_NUM_OF_MASTERS]; /* changes sent/skipped since server start up */
  103. int num_changecounters;
  104. time_t last_update_start_time; /* Local start time of last update session */
  105. time_t last_update_end_time; /* Local end time of last update session */
  106. char last_update_status[STATUS_LEN]; /* Status of last update. Format = numeric code <space> textual description */
  107. PRBool update_in_progress;
  108. PRBool is_enabled;
  109. time_t last_init_start_time; /* Local start time of last total init */
  110. time_t last_init_end_time; /* Local end time of last total init */
  111. char last_init_status[STATUS_LEN]; /* Status of last total init. Format = numeric code <space> textual description */
  112. PRLock *lock;
  113. Object *consumerRUV; /* last RUV received from the consumer - used for changelog purging */
  114. CSN *consumerSchemaCSN; /* last schema CSN received from the consumer */
  115. ReplicaId consumerRID; /* indicates if the consumer is the originator of a CSN */
  116. int tmpConsumerRID; /* Indicates the consumer rid was set from the agmt maxcsn - it should be refreshed */
  117. long timeout; /* timeout (in seconds) for outbound LDAP connections to remote server */
  118. PRBool stop_in_progress; /* set by agmt_stop when shutting down */
  119. long busywaittime; /* time in seconds to wait after getting a REPLICA BUSY from the consumer -
  120. to allow another supplier to finish sending its updates -
  121. if set to 0, this means to use the default value if we get a busy
  122. signal from the consumer */
  123. long pausetime; /* time in seconds to pause after sending updates -
  124. to allow another supplier to send its updates -
  125. should be greater than busywaittime -
  126. if set to 0, this means do not pause */
  127. void *priv; /* private data, used for windows-specific agreement data
  128. for sync agreements or for replication session plug-in
  129. private data for normal replication agreements */
  130. char **attrs_to_strip; /* for fractional replication, if a "mod" is empty, strip out these attributes:
  131. * modifiersname, modifytimestamp, internalModifiersname, internalModifyTimestamp, etc */
  132. int agreement_type;
  133. PRUint64 protocol_timeout;
  134. char *maxcsn; /* agmt max csn */
  135. Slapi_RWLock *attr_lock; /* RW lock for all the stripped attrs */
  136. } repl5agmt;
  137. /* Forward declarations */
  138. void agmt_delete(void **rap);
  139. static void update_window_state_change_callback (void *arg, PRBool opened);
  140. static int get_agmt_status(Slapi_PBlock *pb, Slapi_Entry* e,
  141. Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  142. static int agmt_set_bind_method_no_lock(Repl_Agmt *ra, const Slapi_Entry *e);
  143. static int agmt_set_transportinfo_no_lock(Repl_Agmt *ra, const Slapi_Entry *e);
  144. static ReplicaId agmt_maxcsn_get_rid(char *maxcsn);
  145. /*
  146. Schema for replication agreement:
  147. cn
  148. nsds5ReplicaHost - hostname
  149. nsds5ReplicaPort - port number
  150. nsds5ReplicaTransportInfo - "SSL", "startTLS", or may be absent;
  151. nsds5ReplicaBindDN
  152. nsds5ReplicaCredentials
  153. nsds5ReplicaBindMethod - "SIMPLE" or "SSLCLIENTAUTH".
  154. nsds5ReplicaRoot - Replicated suffix
  155. nsds5ReplicatedAttributeList - Fractional attrs for incremental update protocol (and total if not separately defined)
  156. nsds5ReplicatedAttributeListTotal - Fractional attrs for total update protocol
  157. nsds5ReplicaUpdateSchedule
  158. nsds5ReplicaTimeout - Outbound repl operations timeout
  159. nsds50ruv - consumer's RUV
  160. nsds5ReplicaBusyWaitTime - time to wait after getting a REPLICA BUSY from the consumer
  161. nsds5ReplicaSessionPauseTime - time to pause after sending updates to allow another supplier to send
  162. */
  163. /*
  164. * Validate an agreement, making sure that it's valid.
  165. * Return 1 if the agreement is valid, 0 otherwise.
  166. */
  167. static int
  168. agmt_is_valid(Repl_Agmt *ra)
  169. {
  170. int return_value = 1; /* assume valid, initially */
  171. PR_ASSERT(NULL != ra);
  172. PR_ASSERT(NULL != ra->dn);
  173. if (NULL == ra->hostname)
  174. {
  175. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  176. "is malformed: missing host name.\n", slapi_sdn_get_dn(ra->dn));
  177. return_value = 0;
  178. }
  179. if (ra->port <= 0)
  180. {
  181. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  182. "is malformed: invalid port number %d.\n", slapi_sdn_get_dn(ra->dn), ra->port);
  183. return_value = 0;
  184. }
  185. if (ra->timeout < 0)
  186. {
  187. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  188. "is malformed: invalid timeout %ld.\n", slapi_sdn_get_dn(ra->dn), ra->timeout);
  189. return_value = 0;
  190. }
  191. if (ra->busywaittime < 0)
  192. {
  193. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  194. "is malformed: invalid busy wait time %ld.\n", slapi_sdn_get_dn(ra->dn), ra->busywaittime);
  195. return_value = 0;
  196. }
  197. if (ra->pausetime < 0)
  198. {
  199. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  200. "is malformed: invalid pausetime %ld.\n", slapi_sdn_get_dn(ra->dn), ra->pausetime);
  201. return_value = 0;
  202. }
  203. if ((0 == ra->transport_flags) && (BINDMETHOD_SSL_CLIENTAUTH == ra->bindmethod)) {
  204. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  205. " is malformed: cannot use SSLCLIENTAUTH if using plain LDAP - please "
  206. "change %s to SSL or TLS before changing %s to use SSLCLIENTAUTH\n",
  207. slapi_sdn_get_dn(ra->dn), type_nsds5TransportInfo, type_nsds5ReplicaBindMethod);
  208. return_value = 0;
  209. }
  210. /*
  211. * If we are not using GSSAPI or SSL Client Auth, then a bind dn and password must be present
  212. */
  213. if(BINDMETHOD_SASL_GSSAPI != ra->bindmethod && BINDMETHOD_SSL_CLIENTAUTH != ra->bindmethod){
  214. if(strcmp(ra->binddn,"") == 0 || ra->creds->bv_val == NULL){
  215. char *auth_mech;
  216. if(ra->bindmethod == BINDMETHOD_SIMPLE_AUTH){
  217. auth_mech = "SIMPLE";
  218. } else if (ra->bindmethod == BINDMETHOD_SASL_DIGEST_MD5){
  219. auth_mech = "SASL/DIGEST-MD5";
  220. } else {
  221. auth_mech = "Unknown";
  222. }
  223. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  224. "is malformed: a bind DN and password must be supplied for authentication "
  225. "method \"%s\"\n", slapi_sdn_get_dn(ra->dn), auth_mech);
  226. return_value = 0;
  227. }
  228. }
  229. return return_value;
  230. }
  231. Repl_Agmt *
  232. agmt_new_from_entry(Slapi_Entry *e)
  233. {
  234. Repl_Agmt *ra;
  235. Slapi_Attr *sattr;
  236. char *tmpstr;
  237. char **denied_attrs = NULL;
  238. char *auto_initialize = NULL;
  239. char *val_nsds5BeginReplicaRefresh = "start";
  240. ra = (Repl_Agmt *)slapi_ch_calloc(1, sizeof(repl5agmt));
  241. if ((ra->lock = PR_NewLock()) == NULL)
  242. {
  243. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Unable to create new lock "
  244. "for replication agreement \"%s\" - agreement ignored.\n",
  245. slapi_entry_get_dn_const(e));
  246. goto loser;
  247. }
  248. if ((ra->attr_lock = slapi_new_rwlock()) == NULL)
  249. {
  250. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Unable to create new attr lock "
  251. "for replication agreement \"%s\" - agreement ignored.\n",
  252. slapi_entry_get_dn_const(e));
  253. goto loser;
  254. }
  255. /* Find all the stuff we need for the agreement */
  256. /* To Allow Consumer Initialisation when adding an agreement: */
  257. /*
  258. Using 'auto_initialize' member of 'repl5agmt' structure to
  259. store the effect of 'nsds5BeginReplicaRefresh' attribute's value
  260. in it.
  261. */
  262. auto_initialize = slapi_entry_attr_get_charptr(e, type_nsds5BeginReplicaRefresh);
  263. if ((auto_initialize != NULL) && (strcasecmp(auto_initialize, val_nsds5BeginReplicaRefresh) == 0))
  264. {
  265. ra->auto_initialize = STATE_PERFORMING_TOTAL_UPDATE;
  266. }
  267. else
  268. {
  269. ra->auto_initialize = STATE_PERFORMING_INCREMENTAL_UPDATE;
  270. }
  271. if (auto_initialize)
  272. {
  273. slapi_ch_free_string (&auto_initialize);
  274. }
  275. /* Host name of remote replica */
  276. ra->hostname = slapi_entry_attr_get_charptr(e, type_nsds5ReplicaHost);
  277. /* Port number for remote replica instance */
  278. ra->port = slapi_entry_attr_get_int(e, type_nsds5ReplicaPort);
  279. /* SSL, TLS, or other transport stuff */
  280. ra->transport_flags = 0;
  281. agmt_set_transportinfo_no_lock(ra, e);
  282. /* DN to use when binding. May be empty if certain SASL auth is to be used e.g. EXTERNAL GSSAPI. */
  283. ra->binddn = slapi_entry_attr_get_charptr(e, type_nsds5ReplicaBindDN);
  284. if (NULL == ra->binddn)
  285. {
  286. ra->binddn = slapi_ch_strdup("");
  287. }
  288. /* Credentials to use when binding. */
  289. ra->creds = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  290. ra->creds->bv_val = NULL;
  291. ra->creds->bv_len = 0;
  292. if (slapi_entry_attr_find(e, type_nsds5ReplicaCredentials, &sattr) == 0)
  293. {
  294. Slapi_Value *sval;
  295. if (slapi_attr_first_value(sattr, &sval) == 0)
  296. {
  297. const struct berval *bv = slapi_value_get_berval(sval);
  298. if (NULL != bv)
  299. {
  300. slapi_ber_bvcpy(ra->creds, bv);
  301. }
  302. }
  303. }
  304. /* How to bind */
  305. (void)agmt_set_bind_method_no_lock(ra, e);
  306. /* timeout. */
  307. ra->timeout = DEFAULT_TIMEOUT;
  308. if (slapi_entry_attr_find(e, type_nsds5ReplicaTimeout, &sattr) == 0)
  309. {
  310. Slapi_Value *sval;
  311. if (slapi_attr_first_value(sattr, &sval) == 0)
  312. {
  313. ra->timeout = slapi_value_get_long(sval);
  314. }
  315. }
  316. /* DN of entry at root of replicated area */
  317. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds5ReplicaRoot);
  318. if (NULL != tmpstr)
  319. {
  320. Object *repl_obj;
  321. Replica *replica;
  322. ra->replarea = slapi_sdn_new_dn_passin(tmpstr);
  323. /* now that we set the repl area, when can bump our agmt count */
  324. if((repl_obj = replica_get_replica_from_dn(ra->replarea))){
  325. if((replica = (Replica*)object_get_data (repl_obj))){
  326. replica_incr_agmt_count(replica);
  327. }
  328. }
  329. /* If this agmt has its own timeout, grab it, otherwise use the replica's protocol timeout */
  330. ra->protocol_timeout = slapi_entry_attr_get_int(e, type_replicaProtocolTimeout);
  331. if(ra->protocol_timeout == 0){
  332. /* grab the replica protocol timeout */
  333. Object *replobj = replica_get_replica_from_dn(ra->replarea);
  334. if(replobj){
  335. Replica *replica =(Replica*)object_get_data (replobj);
  336. ra->protocol_timeout = replica_get_protocol_timeout(replica);
  337. } else {
  338. ra->protocol_timeout = DEFAULT_PROTOCOL_TIMEOUT;
  339. }
  340. }
  341. }
  342. /* Replica enabled */
  343. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds5ReplicaEnabled);
  344. if (NULL != tmpstr)
  345. {
  346. if(strcasecmp(tmpstr, "off") == 0){
  347. ra->is_enabled = PR_FALSE;
  348. } else if(strcasecmp(tmpstr, "on") == 0){
  349. ra->is_enabled = PR_TRUE;
  350. } else {
  351. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Warning invalid value "
  352. "for nsds5ReplicaEnabled (%s), value must be \"on\" or \"off\". Ignoring "
  353. "this repl agreement.\n",tmpstr);
  354. slapi_ch_free_string(&tmpstr);
  355. goto loser;
  356. }
  357. slapi_ch_free_string(&tmpstr);
  358. } else {
  359. ra->is_enabled = PR_TRUE;
  360. }
  361. /* Replication schedule */
  362. ra->schedule = schedule_new(update_window_state_change_callback, ra, agmt_get_long_name(ra));
  363. if (slapi_entry_attr_find(e, type_nsds5ReplicaUpdateSchedule, &sattr) == 0)
  364. {
  365. schedule_set(ra->schedule, sattr);
  366. }
  367. /* busy wait time - time to wait after getting REPLICA BUSY from consumer */
  368. ra->busywaittime = slapi_entry_attr_get_long(e, type_nsds5ReplicaBusyWaitTime);
  369. /* pause time - time to pause after a session has ended */
  370. ra->pausetime = slapi_entry_attr_get_long(e, type_nsds5ReplicaSessionPauseTime);
  371. /* consumer's RUV */
  372. if (slapi_entry_attr_find(e, type_ruvElement, &sattr) == 0)
  373. {
  374. RUV *ruv;
  375. if (ruv_init_from_slapi_attr(sattr, &ruv) == 0)
  376. {
  377. ra->consumerRUV = object_new (ruv, (FNFree)ruv_destroy);
  378. }
  379. }
  380. ra->consumerRID = 0;
  381. /* DN and RDN of the replication agreement entry itself */
  382. ra->dn = slapi_sdn_dup(slapi_entry_get_sdn((Slapi_Entry *)e));
  383. ra->rdn = slapi_rdn_new_sdn(ra->dn);
  384. /* Compute long name */
  385. {
  386. const char *agmtname = slapi_rdn_get_rdn(ra->rdn);
  387. char hostname[128];
  388. char *dot;
  389. strncpy(hostname, ra->hostname ? ra->hostname : "(unknown)", sizeof(hostname));
  390. hostname[sizeof(hostname)-1] = '\0';
  391. dot = strchr(hostname, '.');
  392. if (dot) {
  393. *dot = '\0';
  394. }
  395. ra->long_name = slapi_ch_smprintf("agmt=\"%s\" (%s:%d)", agmtname, hostname, ra->port);
  396. }
  397. /* DBDB: review this code */
  398. if (slapi_entry_attr_hasvalue(e, "objectclass", "nsDSWindowsReplicationAgreement"))
  399. {
  400. ra->agreement_type = REPLICA_TYPE_WINDOWS;
  401. windows_init_agreement_from_entry(ra,e);
  402. }
  403. else
  404. {
  405. ra->agreement_type = REPLICA_TYPE_MULTIMASTER;
  406. repl_session_plugin_call_agmt_init_cb(ra);
  407. }
  408. /* Initialize status information */
  409. ra->last_update_start_time = 0UL;
  410. ra->last_update_end_time = 0UL;
  411. ra->num_changecounters = 0;
  412. ra->last_update_status[0] = '\0';
  413. ra->update_in_progress = PR_FALSE;
  414. ra->stop_in_progress = PR_FALSE;
  415. ra->last_init_end_time = 0UL;
  416. ra->last_init_start_time = 0UL;
  417. ra->last_init_status[0] = '\0';
  418. /* Fractional attributes */
  419. slapi_entry_attr_find(e, type_nsds5ReplicatedAttributeList, &sattr);
  420. /* New set of excluded attributes */
  421. /* Note: even if sattrs is empty, we have to call this func since there
  422. * could be a default excluded attr list in cn=plugin default config */
  423. if (agmt_set_replicated_attributes_from_attr(ra, sattr) != 0)
  424. {
  425. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  426. "agmtlist_add_callback: failed to parse "
  427. "replicated attributes for agreement %s\n",
  428. agmt_get_long_name(ra));
  429. }
  430. /* Check that there are no verboten attributes in the exclude list */
  431. denied_attrs = agmt_validate_replicated_attributes(ra, 0 /* incremental */);
  432. if (denied_attrs)
  433. {
  434. /* Report the error to the client */
  435. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  436. "WARNING: Attempt to exclude illegal attributes "
  437. "from a fractional agreement\n");
  438. /* Free the list */
  439. slapi_ch_array_free(denied_attrs);
  440. goto loser;
  441. }
  442. /* Total update fractional attributes */
  443. slapi_entry_attr_find(e, type_nsds5ReplicatedAttributeListTotal, &sattr);
  444. if (sattr && agmt_set_replicated_attributes_total_from_attr(ra, sattr) != 0)
  445. {
  446. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  447. "agmtlist_add_callback: failed to parse total "
  448. "update replicated attributes for agreement %s\n",
  449. agmt_get_long_name(ra));
  450. }
  451. /* Check that there are no verboten attributes in the exclude list */
  452. denied_attrs = agmt_validate_replicated_attributes(ra, 1 /* total */);
  453. if (denied_attrs)
  454. {
  455. /* Report the error to the client */
  456. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  457. "WARNING: Attempt to exclude illegal attributes "
  458. "from a fractional agreement for total update protocol\n");
  459. /* Free the list */
  460. slapi_ch_array_free(denied_attrs);
  461. goto loser;
  462. }
  463. /*
  464. * Extract the attributes to strip for "empty" mods
  465. */
  466. ra->attrs_to_strip = NULL;
  467. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds5ReplicaStripAttrs);
  468. if (NULL != tmpstr)
  469. {
  470. ra->attrs_to_strip = slapi_str2charray_ext(tmpstr, " ", 0);
  471. slapi_ch_free_string(&tmpstr);
  472. }
  473. if (!agmt_is_valid(ra))
  474. {
  475. goto loser;
  476. }
  477. /* Now that the agreement is done, just check if changelog is configured */
  478. if (cl5GetState() != CL5_STATE_OPEN) {
  479. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "WARNING: "
  480. "Replication agreement added but there is no changelog configured. "
  481. "No change will be replicated until a changelog is configured.\n");
  482. }
  483. /*
  484. * Establish a callback for this agreement's entry, so we can
  485. * adorn it with status information when read.
  486. */
  487. slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, slapi_sdn_get_ndn(ra->dn),
  488. LDAP_SCOPE_BASE, "(objectclass=*)", get_agmt_status, ra);
  489. return ra;
  490. loser:
  491. agmt_delete((void **)&ra);
  492. return NULL;
  493. }
  494. Repl_Agmt *
  495. agmt_new_from_pblock(Slapi_PBlock *pb)
  496. {
  497. Slapi_Entry *e;
  498. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  499. return agmt_new_from_entry(e);
  500. }
  501. /*
  502. This should never be called directly - only should be called
  503. as a destructor. XXXggood this is not finished
  504. */
  505. void
  506. agmt_delete(void **rap)
  507. {
  508. Repl_Agmt *ra;
  509. Replica *replica = NULL;
  510. Object *repl_obj = NULL;
  511. PR_ASSERT(NULL != rap);
  512. PR_ASSERT(NULL != *rap);
  513. ra = (Repl_Agmt *)*rap;
  514. /* do prot_delete first - we may be doing some processing using this
  515. replication agreement, and prot_delete will make sure the
  516. processing is complete - then it should be safe to clean up the
  517. other fields below
  518. */
  519. prot_delete(&ra->protocol);
  520. /*
  521. * Remove the callback for this agreement's entry
  522. */
  523. slapi_config_remove_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP,
  524. slapi_sdn_get_ndn(ra->dn),
  525. LDAP_SCOPE_BASE, "(objectclass=*)",
  526. get_agmt_status);
  527. /*
  528. * Call the replication session cleanup callback. We
  529. * need to do this before we free replarea.
  530. */
  531. if (ra->agreement_type != REPLICA_TYPE_WINDOWS) {
  532. repl_session_plugin_call_destroy_agmt_cb(ra);
  533. }
  534. slapi_ch_free_string(&ra->hostname);
  535. slapi_ch_free_string(&ra->binddn);
  536. slapi_ch_array_free(ra->frac_attrs);
  537. slapi_ch_array_free(ra->frac_attrs_total);
  538. if (NULL != ra->creds)
  539. {
  540. ber_bvfree(ra->creds);
  541. }
  542. if (NULL != ra->replarea)
  543. {
  544. /*
  545. * Get the replica for this agreement from the repl area
  546. * so we can decrement the agmt count
  547. */
  548. repl_obj = replica_get_replica_from_dn(ra->replarea);
  549. if (repl_obj) {
  550. replica = (Replica*)object_get_data (repl_obj);
  551. replica_decr_agmt_count(replica);
  552. object_release(repl_obj);
  553. }
  554. slapi_sdn_free(&ra->replarea);
  555. }
  556. if (NULL != ra->consumerRUV)
  557. {
  558. object_release (ra->consumerRUV);
  559. }
  560. csn_free (&ra->consumerSchemaCSN);
  561. while ( --(ra->num_changecounters) >= 0 )
  562. {
  563. slapi_ch_free((void **)&ra->changecounters[ra->num_changecounters]);
  564. }
  565. if (ra->agreement_type == REPLICA_TYPE_WINDOWS)
  566. {
  567. windows_agreement_delete(ra);
  568. }
  569. if(ra->attrs_to_strip){
  570. slapi_ch_array_free(ra->attrs_to_strip);
  571. }
  572. if(ra->maxcsn){
  573. slapi_ch_free_string(&ra->maxcsn);
  574. }
  575. schedule_destroy(ra->schedule);
  576. slapi_ch_free_string(&ra->long_name);
  577. /* free the locks */
  578. PR_DestroyLock(ra->lock);
  579. slapi_destroy_rwlock(ra->attr_lock);
  580. slapi_ch_free((void **)rap);
  581. }
  582. /*
  583. * Allow replication for this replica to begin. Replication will
  584. * occur at the next scheduled time. Returns 0 on success, -1 on
  585. * failure.
  586. */
  587. int
  588. agmt_start(Repl_Agmt *ra)
  589. {
  590. Repl_Protocol *prot = NULL;
  591. int protocol_state;
  592. /* To Allow Consumer Initialisation when adding an agreement: */
  593. if (ra->auto_initialize == STATE_PERFORMING_TOTAL_UPDATE)
  594. {
  595. protocol_state = STATE_PERFORMING_TOTAL_UPDATE;
  596. }
  597. else
  598. {
  599. protocol_state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  600. }
  601. /* First, create a new protocol object */
  602. if ((prot = prot_new(ra, protocol_state)) == NULL) {
  603. return -1;
  604. }
  605. /* Now it is safe to own the agreement lock */
  606. PR_Lock(ra->lock);
  607. /* Check that replication is not already started */
  608. if (ra->protocol != NULL) {
  609. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replication already started for agreement \"%s\"\n", agmt_get_long_name(ra));
  610. PR_Unlock(ra->lock);
  611. prot_free(&prot);
  612. return 0;
  613. }
  614. ra->protocol = prot;
  615. /* Start the protocol thread */
  616. prot_start(ra->protocol);
  617. agmt_set_maxcsn(ra);
  618. PR_Unlock(ra->lock);
  619. return 0;
  620. }
  621. /*
  622. * Allow replication for this replica to begin. Replication will
  623. * occur at the next scheduled time. Returns 0 on success, -1 on
  624. * failure.
  625. */
  626. int
  627. windows_agmt_start(Repl_Agmt *ra)
  628. {
  629. Repl_Protocol *prot = NULL;
  630. int protocol_state;
  631. /* To Allow Consumer Initialisation when adding an agreement: */
  632. if (ra->auto_initialize == STATE_PERFORMING_TOTAL_UPDATE)
  633. {
  634. protocol_state = STATE_PERFORMING_TOTAL_UPDATE;
  635. }
  636. else
  637. {
  638. protocol_state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  639. }
  640. /* First, create a new protocol object */
  641. if ((prot = prot_new(ra, protocol_state)) == NULL) {
  642. return -1;
  643. }
  644. /* Now it is safe to own the agreement lock */
  645. PR_Lock(ra->lock);
  646. /* Check that replication is not already started */
  647. if (ra->protocol != NULL) {
  648. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replication already started for agreement \"%s\"\n", agmt_get_long_name(ra));
  649. PR_Unlock(ra->lock);
  650. prot_free(&prot);
  651. return 0;
  652. }
  653. ra->protocol = prot;
  654. /* Start the protocol thread */
  655. prot_start(ra->protocol);
  656. PR_Unlock(ra->lock);
  657. return 0;
  658. }
  659. /*
  660. Cease replicating to this replica as soon as possible.
  661. */
  662. int
  663. agmt_stop(Repl_Agmt *ra)
  664. {
  665. int return_value = 0;
  666. Repl_Protocol *rp = NULL;
  667. PR_Lock(ra->lock);
  668. if (ra->stop_in_progress)
  669. {
  670. PR_Unlock(ra->lock);
  671. return return_value;
  672. }
  673. ra->stop_in_progress = PR_TRUE;
  674. rp = ra->protocol;
  675. PR_Unlock(ra->lock);
  676. if (NULL != rp) /* we use this pointer outside the lock - dangerous? */
  677. {
  678. prot_stop(rp);
  679. }
  680. PR_Lock(ra->lock);
  681. ra->stop_in_progress = PR_FALSE;
  682. /* we do not reuse the protocol object so free it */
  683. prot_free(&ra->protocol);
  684. PR_Unlock(ra->lock);
  685. return return_value;
  686. }
  687. /*
  688. Send any pending updates as soon as possible, ignoring any replication
  689. schedules.
  690. */
  691. int
  692. agmt_replicate_now(Repl_Agmt *ra)
  693. {
  694. int return_value = 0;
  695. return return_value;
  696. }
  697. /*
  698. * Return a copy of the remote replica's hostname.
  699. */
  700. char *
  701. agmt_get_hostname(const Repl_Agmt *ra)
  702. {
  703. char *return_value;
  704. PR_ASSERT(NULL != ra);
  705. PR_Lock(ra->lock);
  706. return_value = slapi_ch_strdup(ra->hostname);
  707. PR_Unlock(ra->lock);
  708. return return_value;
  709. }
  710. /*
  711. * Return the port number of the remote replica's instance.
  712. */
  713. int
  714. agmt_get_port(const Repl_Agmt *ra)
  715. {
  716. int return_value;
  717. PR_ASSERT(NULL != ra);
  718. PR_Lock(ra->lock);
  719. return_value = ra->port;
  720. PR_Unlock(ra->lock);
  721. return return_value;
  722. }
  723. /*
  724. * Return the transport flags for this agreement.
  725. */
  726. PRUint32
  727. agmt_get_transport_flags(const Repl_Agmt *ra)
  728. {
  729. unsigned int return_value;
  730. PR_ASSERT(NULL != ra);
  731. PR_Lock(ra->lock);
  732. return_value = ra->transport_flags;
  733. PR_Unlock(ra->lock);
  734. return return_value;
  735. }
  736. /*
  737. * Return a copy of the bind dn to be used with this
  738. * agreement (may return NULL if no binddn is required,
  739. * e.g. SSL client auth.
  740. */
  741. char *
  742. agmt_get_binddn(const Repl_Agmt *ra)
  743. {
  744. char *return_value;
  745. PR_ASSERT(NULL != ra);
  746. PR_Lock(ra->lock);
  747. return_value = ra->binddn == NULL ? NULL : slapi_ch_strdup(ra->binddn);
  748. PR_Unlock(ra->lock);
  749. return return_value;
  750. }
  751. /*
  752. * Return a copy of the credentials.
  753. */
  754. struct berval *
  755. agmt_get_credentials(const Repl_Agmt *ra)
  756. {
  757. struct berval *return_value;
  758. PR_ASSERT(NULL != ra);
  759. PR_Lock(ra->lock);
  760. return_value = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  761. return_value->bv_val = (char *)slapi_ch_malloc(ra->creds->bv_len + 1);
  762. return_value->bv_len = ra->creds->bv_len;
  763. memcpy(return_value->bv_val, ra->creds->bv_val, ra->creds->bv_len);
  764. return_value->bv_val[return_value->bv_len] = '\0'; /* just in case */
  765. PR_Unlock(ra->lock);
  766. return return_value;
  767. }
  768. int
  769. agmt_get_bindmethod(const Repl_Agmt *ra)
  770. {
  771. int return_value;
  772. PR_ASSERT(NULL != ra);
  773. PR_Lock(ra->lock);
  774. return_value = ra->bindmethod;
  775. PR_Unlock(ra->lock);
  776. return return_value;
  777. }
  778. /*
  779. * Return a copy of the dn at the top of the replicated area.
  780. */
  781. Slapi_DN *
  782. agmt_get_replarea(const Repl_Agmt *ra)
  783. {
  784. Slapi_DN *return_value;
  785. PR_ASSERT(NULL != ra);
  786. PR_Lock(ra->lock);
  787. return_value = slapi_sdn_new();
  788. slapi_sdn_copy(ra->replarea, return_value);
  789. PR_Unlock(ra->lock);
  790. return return_value;
  791. }
  792. int
  793. agmt_is_fractional(const Repl_Agmt *ra)
  794. {
  795. int return_value;
  796. PR_ASSERT(NULL != ra);
  797. PR_Lock(ra->lock);
  798. return_value = ra->frac_attrs != NULL;
  799. PR_Unlock(ra->lock);
  800. return return_value;
  801. }
  802. /* Returns a COPY of the attr list, remember to free it */
  803. char **
  804. agmt_get_fractional_attrs(const Repl_Agmt *ra)
  805. {
  806. char ** return_value = NULL;
  807. PR_ASSERT(NULL != ra);
  808. if (NULL == ra->frac_attrs)
  809. {
  810. return NULL;
  811. }
  812. slapi_rwlock_rdlock(ra->attr_lock);
  813. return_value = charray_dup(ra->frac_attrs);
  814. slapi_rwlock_unlock(ra->attr_lock);
  815. return return_value;
  816. }
  817. /* Returns a COPY of the attr list, remember to free it */
  818. char **
  819. agmt_get_fractional_attrs_total(const Repl_Agmt *ra)
  820. {
  821. char ** return_value = NULL;
  822. PR_ASSERT(NULL != ra);
  823. if (NULL == ra->frac_attrs_total)
  824. {
  825. return agmt_get_fractional_attrs(ra);
  826. }
  827. PR_Lock(ra->lock);
  828. return_value = charray_dup(ra->frac_attrs_total);
  829. PR_Unlock(ra->lock);
  830. return return_value;
  831. }
  832. int
  833. agmt_is_fractional_attr(const Repl_Agmt *ra, const char *attrname)
  834. {
  835. int return_value;
  836. PR_ASSERT(NULL != ra);
  837. if (NULL == ra->frac_attrs)
  838. {
  839. return 0;
  840. }
  841. slapi_rwlock_rdlock(ra->attr_lock);
  842. /* Scan the list looking for a match */
  843. return_value = charray_inlist(ra->frac_attrs,(char*)attrname);
  844. slapi_rwlock_unlock(ra->attr_lock);
  845. return return_value;
  846. }
  847. int agmt_is_fractional_attr_total(const Repl_Agmt *ra, const char *attrname)
  848. {
  849. int return_value;
  850. PR_ASSERT(NULL != ra);
  851. if (NULL == ra->frac_attrs_total)
  852. {
  853. return agmt_is_fractional_attr(ra, attrname);
  854. }
  855. PR_Lock(ra->lock);
  856. /* Scan the list looking for a match */
  857. return_value = charray_inlist(ra->frac_attrs_total,(char*)attrname);
  858. PR_Unlock(ra->lock);
  859. return return_value;
  860. }
  861. int
  862. agmt_get_auto_initialize(const Repl_Agmt *ra)
  863. {
  864. int return_value;
  865. PR_ASSERT(NULL != ra);
  866. PR_Lock(ra->lock);
  867. return_value = ra->auto_initialize;
  868. PR_Unlock(ra->lock);
  869. return return_value;
  870. }
  871. long
  872. agmt_get_timeout(const Repl_Agmt *ra)
  873. {
  874. long return_value;
  875. PR_ASSERT(NULL != ra);
  876. PR_Lock(ra->lock);
  877. return_value = ra->timeout;
  878. PR_Unlock(ra->lock);
  879. return return_value;
  880. }
  881. long
  882. agmt_get_busywaittime(const Repl_Agmt *ra)
  883. {
  884. long return_value;
  885. PR_ASSERT(NULL != ra);
  886. PR_Lock(ra->lock);
  887. return_value = ra->busywaittime;
  888. PR_Unlock(ra->lock);
  889. return return_value;
  890. }
  891. long
  892. agmt_get_pausetime(const Repl_Agmt *ra)
  893. {
  894. long return_value;
  895. PR_ASSERT(NULL != ra);
  896. PR_Lock(ra->lock);
  897. return_value = ra->pausetime;
  898. PR_Unlock(ra->lock);
  899. return return_value;
  900. }
  901. /*
  902. * Warning - reference to the long name of the agreement is returned.
  903. * The long name of an agreement is the DN of the agreement entry,
  904. * followed by the host/port for the replica.
  905. */
  906. const char *
  907. agmt_get_long_name(const Repl_Agmt *ra)
  908. {
  909. char *return_value = NULL;
  910. return_value = ra ? ra->long_name : "";
  911. return return_value;
  912. }
  913. /*
  914. * Warning - reference to dn is returned. However, since the dn of
  915. * the replication agreement is its name, it won't change during the
  916. * lifetime of the replication agreement object.
  917. */
  918. const Slapi_DN *
  919. agmt_get_dn_byref(const Repl_Agmt *ra)
  920. {
  921. const Slapi_DN *return_value = NULL;
  922. PR_ASSERT(NULL != ra);
  923. if (NULL != ra)
  924. {
  925. return_value = ra->dn;
  926. }
  927. return return_value;
  928. }
  929. /* Return 1 if name matches the replication Dn, 0 otherwise */
  930. int
  931. agmt_matches_name(const Repl_Agmt *ra, const Slapi_DN *name)
  932. {
  933. int return_value = 0;
  934. PR_ASSERT(NULL != ra);
  935. if (NULL != ra)
  936. {
  937. PR_Lock(ra->lock);
  938. if (slapi_sdn_compare(name, ra->dn) == 0)
  939. {
  940. return_value = 1;
  941. }
  942. PR_Unlock(ra->lock);
  943. }
  944. return return_value;
  945. }
  946. /* Return 1 if name matches the replication area, 0 otherwise */
  947. int
  948. agmt_replarea_matches(const Repl_Agmt *ra, const Slapi_DN *name)
  949. {
  950. int return_value = 0;
  951. PR_ASSERT(NULL != ra);
  952. if (NULL != ra)
  953. {
  954. PR_Lock(ra->lock);
  955. if (slapi_sdn_compare(name, ra->replarea) == 0)
  956. {
  957. return_value = 1;
  958. }
  959. PR_Unlock(ra->lock);
  960. }
  961. return return_value;
  962. }
  963. int
  964. agmt_schedule_in_window_now(const Repl_Agmt *ra)
  965. {
  966. int return_value;
  967. PR_ASSERT(NULL != ra);
  968. PR_Lock(ra->lock);
  969. if (NULL != ra->schedule && schedule_in_window_now(ra->schedule))
  970. {
  971. return_value = 1;
  972. }
  973. else
  974. {
  975. return_value = 0;
  976. }
  977. PR_Unlock(ra->lock);
  978. return return_value;
  979. }
  980. /*
  981. * Set or reset the credentials used to bind to the remote replica.
  982. *
  983. * Returns 0 if credentials set, or -1 if an error occurred.
  984. */
  985. int
  986. agmt_set_credentials_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  987. {
  988. Slapi_Attr *sattr = NULL;
  989. int return_value = 0;
  990. PR_ASSERT(NULL != ra);
  991. slapi_entry_attr_find(e, type_nsds5ReplicaCredentials, &sattr);
  992. PR_Lock(ra->lock);
  993. slapi_ber_bvdone(ra->creds);
  994. if (NULL != sattr)
  995. {
  996. Slapi_Value *sval = NULL;
  997. slapi_attr_first_value(sattr, &sval);
  998. if (NULL != sval)
  999. {
  1000. const struct berval *bv = slapi_value_get_berval(sval);
  1001. slapi_ber_bvcpy(ra->creds, bv);
  1002. }
  1003. }
  1004. /* If no credentials set, set to zero-length string */
  1005. ra->creds->bv_val = NULL == ra->creds->bv_val ? slapi_ch_strdup("") : ra->creds->bv_val;
  1006. PR_Unlock(ra->lock);
  1007. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1008. return return_value;
  1009. }
  1010. /*
  1011. * Set or reset the DN used to bind to the remote replica.
  1012. *
  1013. * Returns 0 if DN set, or -1 if an error occurred.
  1014. */
  1015. int
  1016. agmt_set_binddn_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1017. {
  1018. Slapi_Attr *sattr = NULL;
  1019. int return_value = 0;
  1020. PR_ASSERT(NULL != ra);
  1021. slapi_entry_attr_find(e, type_nsds5ReplicaBindDN, &sattr);
  1022. PR_Lock(ra->lock);
  1023. slapi_ch_free((void **)&ra->binddn);
  1024. ra->binddn = NULL;
  1025. if (NULL != sattr)
  1026. {
  1027. Slapi_Value *sval = NULL;
  1028. slapi_attr_first_value(sattr, &sval);
  1029. if (NULL != sval)
  1030. {
  1031. const char *val = slapi_value_get_string(sval);
  1032. ra->binddn = slapi_ch_strdup(val);
  1033. }
  1034. }
  1035. /* If no BindDN set, set to zero-length string */
  1036. if (ra->binddn == NULL) {
  1037. ra->binddn = slapi_ch_strdup("");
  1038. }
  1039. PR_Unlock(ra->lock);
  1040. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1041. return return_value;
  1042. }
  1043. /*
  1044. * Reset the port number of the remote replica.
  1045. *
  1046. * Returns 0 if port set, or -1 if an error occurred.
  1047. */
  1048. int
  1049. agmt_set_port_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1050. {
  1051. Slapi_Attr *sattr = NULL;
  1052. int return_value = -1;
  1053. PR_ASSERT(NULL != ra);
  1054. slapi_entry_attr_find(e, type_nsds5ReplicaPort, &sattr);
  1055. PR_Lock(ra->lock);
  1056. if (NULL != sattr)
  1057. {
  1058. Slapi_Value *sval = NULL;
  1059. slapi_attr_first_value(sattr, &sval);
  1060. if (NULL != sval)
  1061. {
  1062. ra->port = slapi_value_get_int(sval);
  1063. return_value = 0;
  1064. }
  1065. }
  1066. PR_Unlock(ra->lock);
  1067. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1068. return return_value;
  1069. }
  1070. static int
  1071. agmt_parse_excluded_attrs_filter(const char *attr_string, size_t *offset)
  1072. {
  1073. char *filterstring = "(objectclass=*) ";
  1074. size_t filterstringlen = strlen(filterstring);
  1075. int retval = 0;
  1076. if (strncmp(attr_string + *offset,filterstring,filterstringlen) == 0)
  1077. {
  1078. (*offset) += filterstringlen;
  1079. } else
  1080. {
  1081. retval = -1;
  1082. }
  1083. return retval;
  1084. }
  1085. static int
  1086. agmt_parse_excluded_attrs_exclude(const char *attr_string, size_t *offset)
  1087. {
  1088. char *excludestring = "$ EXCLUDE ";
  1089. size_t excludestringlen = strlen(excludestring);
  1090. int retval = 0;
  1091. if (strncmp(attr_string + *offset,excludestring,excludestringlen) == 0)
  1092. {
  1093. (*offset) += excludestringlen;
  1094. } else
  1095. {
  1096. retval = -1;
  1097. }
  1098. return retval;
  1099. }
  1100. static int
  1101. agmt_parse_excluded_attrs_next(const char *attr_string, size_t *offset, char*** attrs)
  1102. {
  1103. int retval = 0;
  1104. char *beginstr = ((char*) attr_string) + *offset;
  1105. char *tmpstr = NULL;
  1106. size_t stringlen = 0;
  1107. char c = 0;
  1108. /* Find the end of the current attribute name, if one is present */
  1109. while (1)
  1110. {
  1111. c = *(beginstr + stringlen);
  1112. if ('\0' == c || ' ' == c)
  1113. {
  1114. break;
  1115. }
  1116. stringlen++;
  1117. }
  1118. if (0 != stringlen)
  1119. {
  1120. tmpstr = slapi_ch_malloc(stringlen + 1);
  1121. strncpy(tmpstr,beginstr,stringlen);
  1122. tmpstr[stringlen] = '\0';
  1123. if (charray_inlist(*attrs, tmpstr)) /* tmpstr is already in attrs */
  1124. {
  1125. slapi_ch_free_string(&tmpstr);
  1126. }
  1127. else
  1128. {
  1129. charray_add(attrs,tmpstr);
  1130. }
  1131. (*offset) += stringlen;
  1132. /* Skip a delimiting space */
  1133. if (c == ' ')
  1134. {
  1135. (*offset)++;
  1136. }
  1137. } else
  1138. {
  1139. retval = -1;
  1140. }
  1141. return retval;
  1142. }
  1143. /* It looks like this:
  1144. * nsDS5ReplicatedAttributeList: (objectclass=*) $ EXCLUDE jpegPhoto telephoneNumber
  1145. * This function could be called multiple times: to set excluded attrs in the
  1146. * plugin default config and to set the ones in the replica agreement.
  1147. * The excluded attrs from replica agreement are added to the ones from
  1148. * default config. (Therefore, *attrs should not be initialized in this
  1149. * function.)
  1150. */
  1151. static int
  1152. agmt_parse_excluded_attrs_config_attr(const char *attr_string, char ***attrs)
  1153. {
  1154. int retval = 0;
  1155. size_t offset = 0;
  1156. char **new_attrs = NULL;
  1157. /* First parse and skip the filter */
  1158. retval = agmt_parse_excluded_attrs_filter(attr_string, &offset);
  1159. if (retval)
  1160. {
  1161. goto error;
  1162. }
  1163. /* Now look for the 'EXCLUDE' keyword */
  1164. retval = agmt_parse_excluded_attrs_exclude(attr_string, &offset);
  1165. if (retval)
  1166. {
  1167. goto error;
  1168. }
  1169. /* Finally walk the list of attrs, storing in our chararray */
  1170. while (!retval)
  1171. {
  1172. retval = agmt_parse_excluded_attrs_next(attr_string, &offset, &new_attrs);
  1173. }
  1174. /* If we got to here, we can't have an error */
  1175. retval = 0;
  1176. if (new_attrs)
  1177. {
  1178. charray_merge_nodup(attrs, new_attrs, 1);
  1179. slapi_ch_array_free(new_attrs);
  1180. }
  1181. error:
  1182. return retval;
  1183. }
  1184. /*
  1185. * _agmt_set_default_fractional_attrs
  1186. * helper function to set nsds5ReplicatedAttributeList value (from cn=plugin
  1187. * default config,cn=config) to frac_attrs in Repl_Agmt.
  1188. * nsds5ReplicatedAttributeList set in each agreement is added to the
  1189. * default list set in this function.
  1190. */
  1191. static int
  1192. _agmt_set_default_fractional_attrs(Repl_Agmt *ra)
  1193. {
  1194. Slapi_PBlock *newpb = NULL;
  1195. Slapi_Entry **entries = NULL;
  1196. int rc = LDAP_SUCCESS;
  1197. char *attrs[2];
  1198. attrs[0] = (char *)type_nsds5ReplicatedAttributeList;
  1199. attrs[1] = NULL;
  1200. newpb = slapi_pblock_new();
  1201. slapi_search_internal_set_pb(newpb,
  1202. SLAPI_PLUGIN_DEFAULT_CONFIG, /* Base DN */
  1203. LDAP_SCOPE_BASE,
  1204. "(objectclass=*)",
  1205. attrs, /* Attrs */
  1206. 0, /* AttrOnly */
  1207. NULL, /* Controls */
  1208. NULL, /* UniqueID */
  1209. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  1210. 0);
  1211. slapi_search_internal_pb(newpb);
  1212. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1213. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1214. ra->frac_attrs = NULL;
  1215. if (LDAP_SUCCESS == rc && entries && *entries) /* default config entry exists */
  1216. {
  1217. Slapi_Attr *attr;
  1218. Slapi_Value *sval = NULL;
  1219. if (0 == slapi_entry_attr_find(*entries,
  1220. type_nsds5ReplicatedAttributeList, &attr))
  1221. {
  1222. int i;
  1223. const char *val = NULL;
  1224. for (i = slapi_attr_first_value(attr, &sval);
  1225. i >= 0; i = slapi_attr_next_value(attr, i, &sval)) {
  1226. val = slapi_value_get_string(sval);
  1227. rc = agmt_parse_excluded_attrs_config_attr(val,
  1228. &(ra->frac_attrs));
  1229. if (0 != rc) {
  1230. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1231. "_agmt_set_default_fractional_attrs: failed to "
  1232. "parse default config (%s) attribute %s value: %s\n",
  1233. SLAPI_PLUGIN_DEFAULT_CONFIG,
  1234. type_nsds5ReplicatedAttributeList, val);
  1235. }
  1236. }
  1237. }
  1238. }
  1239. slapi_free_search_results_internal(newpb);
  1240. slapi_pblock_destroy(newpb);
  1241. return rc;
  1242. }
  1243. /*
  1244. * Set or reset the set of replicated attributes.
  1245. *
  1246. * Returns 0 if DN set, or -1 if an error occurred.
  1247. */
  1248. int
  1249. agmt_set_replicated_attributes_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1250. {
  1251. Slapi_Attr *sattr = NULL;
  1252. int return_value = 0;
  1253. PR_ASSERT(NULL != ra);
  1254. slapi_entry_attr_find(e, type_nsds5ReplicatedAttributeList, &sattr);
  1255. slapi_rwlock_wrlock(ra->attr_lock);
  1256. if (ra->frac_attrs)
  1257. {
  1258. slapi_ch_array_free(ra->frac_attrs);
  1259. ra->frac_attrs = NULL;
  1260. }
  1261. _agmt_set_default_fractional_attrs(ra);
  1262. if (NULL != sattr)
  1263. {
  1264. Slapi_Value *sval = NULL;
  1265. slapi_attr_first_value(sattr, &sval);
  1266. if (NULL != sval)
  1267. {
  1268. const char *val = slapi_value_get_string(sval);
  1269. return_value = agmt_parse_excluded_attrs_config_attr(val,&(ra->frac_attrs));
  1270. }
  1271. }
  1272. slapi_rwlock_unlock(ra->attr_lock);
  1273. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1274. return return_value;
  1275. }
  1276. /*
  1277. * Set or reset the set of total update replicated attributes.
  1278. *
  1279. * Returns 0 if DN set, or -1 if an error occurred.
  1280. */
  1281. int
  1282. agmt_set_replicated_attributes_total_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1283. {
  1284. Slapi_Attr *sattr = NULL;
  1285. int return_value = 0;
  1286. PR_ASSERT(NULL != ra);
  1287. slapi_entry_attr_find(e, type_nsds5ReplicatedAttributeListTotal, &sattr);
  1288. PR_Lock(ra->lock);
  1289. if (ra->frac_attrs_total)
  1290. {
  1291. slapi_ch_array_free(ra->frac_attrs_total);
  1292. ra->frac_attrs_total = NULL;
  1293. }
  1294. if (NULL != sattr)
  1295. {
  1296. Slapi_Value *sval = NULL;
  1297. slapi_attr_first_value(sattr, &sval);
  1298. if (NULL != sval)
  1299. {
  1300. const char *val = slapi_value_get_string(sval);
  1301. return_value = agmt_parse_excluded_attrs_config_attr(val,&(ra->frac_attrs_total));
  1302. }
  1303. }
  1304. PR_Unlock(ra->lock);
  1305. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1306. return return_value;
  1307. }
  1308. /*
  1309. * Set or reset the set of replicated attributes.
  1310. *
  1311. * Returns 0 if DN set, or -1 if an error occurred.
  1312. */
  1313. int
  1314. agmt_set_replicated_attributes_from_attr(Repl_Agmt *ra, Slapi_Attr *sattr)
  1315. {
  1316. int return_value = 0;
  1317. PR_ASSERT(NULL != ra);
  1318. slapi_rwlock_wrlock(ra->attr_lock);
  1319. if (ra->frac_attrs)
  1320. {
  1321. slapi_ch_array_free(ra->frac_attrs);
  1322. ra->frac_attrs = NULL;
  1323. }
  1324. _agmt_set_default_fractional_attrs(ra);
  1325. if (NULL != sattr)
  1326. {
  1327. Slapi_Value *sval = NULL;
  1328. slapi_attr_first_value(sattr, &sval);
  1329. if (NULL != sval)
  1330. {
  1331. const char *val = slapi_value_get_string(sval);
  1332. return_value = agmt_parse_excluded_attrs_config_attr(val,&(ra->frac_attrs));
  1333. }
  1334. }
  1335. slapi_rwlock_unlock(ra->attr_lock);
  1336. return return_value;
  1337. }
  1338. /*
  1339. * Set or reset the set of total update replicated attributes.
  1340. *
  1341. * Returns 0 if DN set, or -1 if an error occurred.
  1342. */
  1343. int
  1344. agmt_set_replicated_attributes_total_from_attr(Repl_Agmt *ra, Slapi_Attr *sattr)
  1345. {
  1346. int return_value = 0;
  1347. PR_ASSERT(NULL != ra);
  1348. PR_Lock(ra->lock);
  1349. if (ra->frac_attrs_total)
  1350. {
  1351. slapi_ch_array_free(ra->frac_attrs_total);
  1352. ra->frac_attrs_total = NULL;
  1353. }
  1354. if (NULL != sattr)
  1355. {
  1356. Slapi_Value *sval = NULL;
  1357. slapi_attr_first_value(sattr, &sval);
  1358. if (NULL != sval)
  1359. {
  1360. const char *val = slapi_value_get_string(sval);
  1361. return_value = agmt_parse_excluded_attrs_config_attr(val,&(ra->frac_attrs_total));
  1362. }
  1363. }
  1364. PR_Unlock(ra->lock);
  1365. return return_value;
  1366. }
  1367. char **
  1368. agmt_validate_replicated_attributes(Repl_Agmt *ra, int total)
  1369. {
  1370. static char* verbotten_attrs[] = {
  1371. "nsuniqueid",
  1372. "modifiersname",
  1373. "lastmodifiedtime",
  1374. "dc", "o", "ou", "cn", "objectclass",
  1375. NULL
  1376. };
  1377. char **retval = NULL;
  1378. char **frac_attrs = NULL;
  1379. /* If checking for total update, use the total attr list
  1380. * if it exists. If oen is not set, use the incremental
  1381. * attr list. */
  1382. if (total && ra->frac_attrs_total)
  1383. {
  1384. frac_attrs = ra->frac_attrs_total;
  1385. }
  1386. else
  1387. {
  1388. frac_attrs = ra->frac_attrs;
  1389. }
  1390. /* Iterate over the frac attrs */
  1391. if (frac_attrs)
  1392. {
  1393. char *this_attr = NULL;
  1394. int i = 0;
  1395. for (i = 0; (this_attr = frac_attrs[i]); i++)
  1396. {
  1397. if (charray_inlist(verbotten_attrs,this_attr)) {
  1398. int k = 0;
  1399. charray_add(&retval,this_attr);
  1400. /* Remove this attr from the list */
  1401. for (k = i; frac_attrs[k] ; k++)
  1402. {
  1403. frac_attrs[k] = frac_attrs[k+1];
  1404. }
  1405. i--;
  1406. }
  1407. }
  1408. }
  1409. return retval;
  1410. }
  1411. /*
  1412. * Set or reset the bind method used to bind to the remote replica.
  1413. *
  1414. * Returns 0 if bind method set, or -1 if an error occurred.
  1415. */
  1416. static int
  1417. agmt_set_bind_method_no_lock(Repl_Agmt *ra, const Slapi_Entry *e)
  1418. {
  1419. char *tmpstr = NULL;
  1420. int return_value = 0;
  1421. PR_ASSERT(NULL != ra);
  1422. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds5ReplicaBindMethod);
  1423. if (NULL == tmpstr || strcasecmp(tmpstr, "SIMPLE") == 0)
  1424. {
  1425. ra->bindmethod = BINDMETHOD_SIMPLE_AUTH;
  1426. }
  1427. else if (strcasecmp(tmpstr, "SSLCLIENTAUTH") == 0)
  1428. {
  1429. ra->bindmethod = BINDMETHOD_SSL_CLIENTAUTH;
  1430. }
  1431. else if (strcasecmp(tmpstr, "SASL/GSSAPI") == 0)
  1432. {
  1433. ra->bindmethod = BINDMETHOD_SASL_GSSAPI;
  1434. }
  1435. else if (strcasecmp(tmpstr, "SASL/DIGEST-MD5") == 0)
  1436. {
  1437. ra->bindmethod = BINDMETHOD_SASL_DIGEST_MD5;
  1438. }
  1439. else
  1440. {
  1441. ra->bindmethod = BINDMETHOD_SIMPLE_AUTH;
  1442. }
  1443. slapi_ch_free((void **)&tmpstr);
  1444. return return_value;
  1445. }
  1446. int
  1447. agmt_set_bind_method_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1448. {
  1449. int return_value = 0;
  1450. PR_ASSERT(NULL != ra);
  1451. PR_Lock(ra->lock);
  1452. if (ra->stop_in_progress)
  1453. {
  1454. PR_Unlock(ra->lock);
  1455. return return_value;
  1456. }
  1457. return_value = agmt_set_bind_method_no_lock(ra, e);
  1458. PR_Unlock(ra->lock);
  1459. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1460. return return_value;
  1461. }
  1462. /*
  1463. * Set or reset the transport used to bind to the remote replica.
  1464. *
  1465. * Returns 0 if transport set, or -1 if an error occurred.
  1466. */
  1467. static int
  1468. agmt_set_transportinfo_no_lock(Repl_Agmt *ra, const Slapi_Entry *e)
  1469. {
  1470. char *tmpstr;
  1471. int rc = 0;
  1472. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds5TransportInfo);
  1473. if (!tmpstr || !strcasecmp(tmpstr, "LDAP")) {
  1474. ra->transport_flags = 0;
  1475. } else if (strcasecmp(tmpstr, "SSL") == 0) {
  1476. ra->transport_flags = TRANSPORT_FLAG_SSL;
  1477. } else if (strcasecmp(tmpstr, "TLS") == 0) {
  1478. ra->transport_flags = TRANSPORT_FLAG_TLS;
  1479. }
  1480. /* else do nothing - invalid value is a no-op */
  1481. slapi_ch_free_string(&tmpstr);
  1482. return (rc);
  1483. }
  1484. int
  1485. agmt_set_transportinfo_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1486. {
  1487. int return_value = 0;
  1488. PR_ASSERT(NULL != ra);
  1489. PR_Lock(ra->lock);
  1490. if (ra->stop_in_progress)
  1491. {
  1492. PR_Unlock(ra->lock);
  1493. return return_value;
  1494. }
  1495. return_value = agmt_set_transportinfo_no_lock(ra, e);
  1496. PR_Unlock(ra->lock);
  1497. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1498. return return_value;
  1499. }
  1500. /*
  1501. * Set or reset the replication schedule. Notify the protocol handler
  1502. * that a change has been made.
  1503. *
  1504. * Returns 0 if schedule was set or -1 if an error occurred.
  1505. */
  1506. int
  1507. agmt_set_schedule_from_entry( Repl_Agmt *ra, const Slapi_Entry *e )
  1508. {
  1509. Slapi_Attr *sattr;
  1510. int return_value = 0;
  1511. PR_ASSERT(NULL != ra);
  1512. PR_Lock(ra->lock);
  1513. if (ra->stop_in_progress)
  1514. {
  1515. PR_Unlock(ra->lock);
  1516. return return_value;
  1517. }
  1518. PR_Unlock(ra->lock);
  1519. if (slapi_entry_attr_find(e, type_nsds5ReplicaUpdateSchedule, &sattr) != 0)
  1520. {
  1521. sattr = NULL; /* no schedule ==> delete any existing one */
  1522. }
  1523. /* make it so */
  1524. return_value = schedule_set(ra->schedule, sattr);
  1525. if ( 0 == return_value ) {
  1526. /* schedule set OK -- spread the news */
  1527. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1528. }
  1529. return return_value;
  1530. }
  1531. /*
  1532. * Set or reset the timeout used to bind to the remote replica.
  1533. *
  1534. * Returns 0 if timeout set, or -1 if an error occurred.
  1535. */
  1536. int
  1537. agmt_set_timeout_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1538. {
  1539. Slapi_Attr *sattr = NULL;
  1540. int return_value = -1;
  1541. PR_ASSERT(NULL != ra);
  1542. PR_Lock(ra->lock);
  1543. if (ra->stop_in_progress)
  1544. {
  1545. PR_Unlock(ra->lock);
  1546. return return_value;
  1547. }
  1548. slapi_entry_attr_find(e, type_nsds5ReplicaTimeout, &sattr);
  1549. if (NULL != sattr)
  1550. {
  1551. Slapi_Value *sval = NULL;
  1552. slapi_attr_first_value(sattr, &sval);
  1553. if (NULL != sval)
  1554. {
  1555. long tmpval = slapi_value_get_long(sval);
  1556. if (tmpval >= 0) {
  1557. ra->timeout = tmpval;
  1558. return_value = 0; /* success! */
  1559. }
  1560. }
  1561. }
  1562. PR_Unlock(ra->lock);
  1563. if (return_value == 0)
  1564. {
  1565. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1566. }
  1567. return return_value;
  1568. }
  1569. int
  1570. agmt_set_timeout(Repl_Agmt *ra, long timeout)
  1571. {
  1572. PR_Lock(ra->lock);
  1573. if (ra->stop_in_progress){
  1574. PR_Unlock(ra->lock);
  1575. return -1;
  1576. }
  1577. ra->timeout = timeout;
  1578. PR_Unlock(ra->lock);
  1579. return 0;
  1580. }
  1581. /*
  1582. * Set or reset the busywaittime
  1583. *
  1584. * Returns 0 if busywaittime set, or -1 if an error occurred.
  1585. */
  1586. int
  1587. agmt_set_busywaittime_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1588. {
  1589. Slapi_Attr *sattr = NULL;
  1590. int return_value = -1;
  1591. PR_ASSERT(NULL != ra);
  1592. PR_Lock(ra->lock);
  1593. if (ra->stop_in_progress)
  1594. {
  1595. PR_Unlock(ra->lock);
  1596. return return_value;
  1597. }
  1598. slapi_entry_attr_find(e, type_nsds5ReplicaBusyWaitTime, &sattr);
  1599. if (NULL != sattr)
  1600. {
  1601. Slapi_Value *sval = NULL;
  1602. slapi_attr_first_value(sattr, &sval);
  1603. if (NULL != sval)
  1604. {
  1605. long tmpval = slapi_value_get_long(sval);
  1606. if (tmpval >= 0) {
  1607. ra->busywaittime = tmpval;
  1608. return_value = 0; /* success! */
  1609. }
  1610. }
  1611. }
  1612. PR_Unlock(ra->lock);
  1613. if (return_value == 0)
  1614. {
  1615. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1616. }
  1617. return return_value;
  1618. }
  1619. /*
  1620. * Set or reset the pausetime
  1621. *
  1622. * Returns 0 if pausetime set, or -1 if an error occurred.
  1623. */
  1624. int
  1625. agmt_set_pausetime_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1626. {
  1627. Slapi_Attr *sattr = NULL;
  1628. int return_value = -1;
  1629. PR_ASSERT(NULL != ra);
  1630. PR_Lock(ra->lock);
  1631. if (ra->stop_in_progress)
  1632. {
  1633. PR_Unlock(ra->lock);
  1634. return return_value;
  1635. }
  1636. slapi_entry_attr_find(e, type_nsds5ReplicaSessionPauseTime, &sattr);
  1637. if (NULL != sattr)
  1638. {
  1639. Slapi_Value *sval = NULL;
  1640. slapi_attr_first_value(sattr, &sval);
  1641. if (NULL != sval)
  1642. {
  1643. long tmpval = slapi_value_get_long(sval);
  1644. if (tmpval >= 0) {
  1645. ra->pausetime = tmpval;
  1646. return_value = 0; /* success! */
  1647. }
  1648. }
  1649. }
  1650. PR_Unlock(ra->lock);
  1651. if (return_value == 0)
  1652. {
  1653. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1654. }
  1655. return return_value;
  1656. }
  1657. /* XXXggood - also make this pass an arg that tells if there was
  1658. * an update to a priority attribute */
  1659. void
  1660. agmt_notify_change(Repl_Agmt *agmt, Slapi_PBlock *pb)
  1661. {
  1662. if (NULL != pb)
  1663. {
  1664. /* Is the entry within our replicated area? */
  1665. Slapi_DN *target_sdn = NULL;
  1666. int change_is_relevant = 0;
  1667. PR_ASSERT(NULL != agmt);
  1668. PR_Lock(agmt->lock);
  1669. if (agmt->stop_in_progress)
  1670. {
  1671. PR_Unlock(agmt->lock);
  1672. return;
  1673. }
  1674. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &target_sdn);
  1675. if (NULL == target_sdn) {
  1676. PR_Unlock(agmt->lock);
  1677. return;
  1678. }
  1679. if (slapi_sdn_issuffix(target_sdn, agmt->replarea))
  1680. {
  1681. /*
  1682. * Yep, it's in our replicated area. Is this a fractional
  1683. * replication agreement?
  1684. */
  1685. if (NULL != agmt->frac_attrs)
  1686. {
  1687. /*
  1688. * Yep, it's fractional. See if the change should be
  1689. * tossed because it doesn't affect any of the replicated
  1690. * attributes.
  1691. */
  1692. int optype;
  1693. int affects_non_fractional_attribute = 0;
  1694. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &optype);
  1695. if (SLAPI_OPERATION_MODIFY == optype)
  1696. {
  1697. LDAPMod **mods;
  1698. int i, j;
  1699. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1700. slapi_rwlock_rdlock(agmt->attr_lock);
  1701. for (i = 0; !affects_non_fractional_attribute && NULL != agmt->frac_attrs[i]; i++)
  1702. {
  1703. for (j = 0; !affects_non_fractional_attribute && NULL != mods[j]; j++)
  1704. {
  1705. if (!slapi_attr_types_equivalent(agmt->frac_attrs[i],
  1706. mods[j]->mod_type))
  1707. {
  1708. affects_non_fractional_attribute = 1;
  1709. }
  1710. }
  1711. }
  1712. slapi_rwlock_unlock(agmt->attr_lock);
  1713. }
  1714. else
  1715. {
  1716. /*
  1717. * Add, delete, and modrdn always cause some sort of
  1718. * operation replay, even if agreement is fractional.
  1719. */
  1720. affects_non_fractional_attribute = 1;
  1721. }
  1722. if (affects_non_fractional_attribute)
  1723. {
  1724. change_is_relevant = 1;
  1725. }
  1726. }
  1727. else
  1728. {
  1729. /* Not a fractional agreement */
  1730. change_is_relevant = 1;
  1731. }
  1732. }
  1733. PR_Unlock(agmt->lock);
  1734. if (change_is_relevant)
  1735. {
  1736. /* Notify the protocol that a change has occurred */
  1737. prot_notify_update(agmt->protocol);
  1738. }
  1739. }
  1740. }
  1741. int
  1742. agmt_is_50_mm_protocol(const Repl_Agmt *agmt)
  1743. {
  1744. return 1; /* XXXggood could support > 1 protocol */
  1745. }
  1746. int
  1747. agmt_initialize_replica(const Repl_Agmt *agmt)
  1748. {
  1749. PR_ASSERT(NULL != agmt);
  1750. PR_Lock(agmt->lock);
  1751. if (agmt->stop_in_progress)
  1752. {
  1753. PR_Unlock(agmt->lock);
  1754. return 0;
  1755. }
  1756. PR_Unlock(agmt->lock);
  1757. /* Call prot_initialize_replica only if the suffix is enabled (agmt->protocol != NULL) */
  1758. if (NULL != agmt->protocol) {
  1759. prot_initialize_replica(agmt->protocol);
  1760. }
  1761. else {
  1762. /* agmt->protocol == NULL --> Suffix is disabled */
  1763. return -1;
  1764. }
  1765. return 0;
  1766. }
  1767. /* delete nsds5BeginReplicaRefresh attribute to indicate to the clients
  1768. that replica initialization have completed */
  1769. void
  1770. agmt_replica_init_done (const Repl_Agmt *agmt)
  1771. {
  1772. int rc;
  1773. Slapi_PBlock *pb = slapi_pblock_new ();
  1774. LDAPMod *mods [2];
  1775. LDAPMod mod;
  1776. mods[0] = &mod;
  1777. mods[1] = NULL;
  1778. mod.mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
  1779. mod.mod_type = (char*)type_nsds5ReplicaInitialize;
  1780. mod.mod_bvalues = NULL;
  1781. slapi_modify_internal_set_pb_ext(pb, agmt->dn, mods, NULL/* controls */,
  1782. NULL/* uniqueid */, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0/* flags */);
  1783. slapi_modify_internal_pb (pb);
  1784. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1785. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE)
  1786. {
  1787. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmt_replica_init_done: "
  1788. "failed to remove (%s) attribute from (%s) entry; LDAP error - %d\n",
  1789. type_nsds5ReplicaInitialize, slapi_sdn_get_ndn (agmt->dn), rc);
  1790. }
  1791. slapi_pblock_destroy (pb);
  1792. }
  1793. /* Agreement object is acquired on behalf of the caller.
  1794. The caller is responsible for releasing the object
  1795. when it is no longer used */
  1796. Object*
  1797. agmt_get_consumer_ruv (Repl_Agmt *ra)
  1798. {
  1799. Object *rt = NULL;
  1800. PR_ASSERT(NULL != ra);
  1801. PR_Lock(ra->lock);
  1802. if (ra->consumerRUV)
  1803. {
  1804. object_acquire (ra->consumerRUV);
  1805. rt = ra->consumerRUV;
  1806. }
  1807. PR_Unlock(ra->lock);
  1808. return rt;
  1809. }
  1810. int
  1811. agmt_set_consumer_ruv (Repl_Agmt *ra, RUV *ruv)
  1812. {
  1813. if (ra == NULL || ruv == NULL)
  1814. {
  1815. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmt_set_consumer_ruv: invalid argument"
  1816. " agmt - %p, ruv - %p\n", ra, ruv);
  1817. return -1;
  1818. }
  1819. PR_Lock(ra->lock);
  1820. if (ra->consumerRUV)
  1821. {
  1822. object_release (ra->consumerRUV);
  1823. }
  1824. ra->consumerRUV = object_new (ruv_dup (ruv), (FNFree)ruv_destroy);
  1825. PR_Unlock(ra->lock);
  1826. return 0;
  1827. }
  1828. void
  1829. agmt_update_consumer_ruv (Repl_Agmt *ra)
  1830. {
  1831. int rc;
  1832. RUV *ruv;
  1833. Slapi_Mod smod;
  1834. Slapi_Mod smod_last_modified;
  1835. Slapi_PBlock *pb;
  1836. LDAPMod *mods[3];
  1837. PR_ASSERT (ra);
  1838. PR_Lock(ra->lock);
  1839. if (ra->consumerRUV)
  1840. {
  1841. ruv = (RUV*) object_get_data (ra->consumerRUV);
  1842. PR_ASSERT (ruv);
  1843. ruv_to_smod(ruv, &smod);
  1844. ruv_last_modified_to_smod(ruv, &smod_last_modified);
  1845. /* it is ok to release the lock here because we are done with the agreement data.
  1846. we have to do it before issuing the modify operation because it causes
  1847. agmtlist_notify_all to be called which uses the same lock - hence the deadlock */
  1848. PR_Unlock(ra->lock);
  1849. pb = slapi_pblock_new ();
  1850. mods[0] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod);
  1851. mods[1] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod_last_modified);
  1852. mods[2] = NULL;
  1853. slapi_modify_internal_set_pb_ext (pb, ra->dn, mods, NULL, NULL,
  1854. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  1855. slapi_modify_internal_pb (pb);
  1856. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1857. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE)
  1858. {
  1859. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: agmt_update_consumer_ruv: "
  1860. "failed to update consumer's RUV; LDAP error - %d\n",
  1861. ra->long_name, rc);
  1862. }
  1863. slapi_mod_done (&smod);
  1864. slapi_mod_done (&smod_last_modified);
  1865. slapi_pblock_destroy (pb);
  1866. }
  1867. else
  1868. PR_Unlock(ra->lock);
  1869. }
  1870. CSN*
  1871. agmt_get_consumer_schema_csn (Repl_Agmt *ra)
  1872. {
  1873. CSN *rt;
  1874. PR_ASSERT(NULL != ra);
  1875. PR_Lock(ra->lock);
  1876. rt = ra->consumerSchemaCSN;
  1877. PR_Unlock(ra->lock);
  1878. return rt;
  1879. }
  1880. void
  1881. agmt_set_consumer_schema_csn (Repl_Agmt *ra, CSN *csn)
  1882. {
  1883. PR_ASSERT(NULL != ra);
  1884. PR_Lock(ra->lock);
  1885. csn_free(&ra->consumerSchemaCSN);
  1886. ra->consumerSchemaCSN = csn;
  1887. PR_Unlock(ra->lock);
  1888. }
  1889. void
  1890. agmt_set_last_update_start (Repl_Agmt *ra, time_t start_time)
  1891. {
  1892. PR_ASSERT(NULL != ra);
  1893. if (NULL != ra)
  1894. {
  1895. ra->last_update_start_time = start_time;
  1896. ra->last_update_end_time = 0UL;
  1897. }
  1898. }
  1899. void
  1900. agmt_set_last_update_end (Repl_Agmt *ra, time_t end_time)
  1901. {
  1902. PR_ASSERT(NULL != ra);
  1903. if (NULL != ra)
  1904. {
  1905. ra->last_update_end_time = end_time;
  1906. }
  1907. }
  1908. void
  1909. agmt_set_last_init_start (Repl_Agmt *ra, time_t start_time)
  1910. {
  1911. PR_ASSERT(NULL != ra);
  1912. if (NULL != ra)
  1913. {
  1914. ra->last_init_start_time = start_time;
  1915. ra->last_init_end_time = 0UL;
  1916. }
  1917. }
  1918. void
  1919. agmt_set_last_init_end (Repl_Agmt *ra, time_t end_time)
  1920. {
  1921. PR_ASSERT(NULL != ra);
  1922. if (NULL != ra)
  1923. {
  1924. ra->last_init_end_time = end_time;
  1925. }
  1926. }
  1927. void
  1928. agmt_set_last_update_status (Repl_Agmt *ra, int ldaprc, int replrc, const char *message)
  1929. {
  1930. PR_ASSERT(NULL != ra);
  1931. if (NULL != ra)
  1932. {
  1933. if (replrc == NSDS50_REPL_UPTODATE)
  1934. {
  1935. /* no session started, no status update */
  1936. }
  1937. else if (ldaprc != LDAP_SUCCESS)
  1938. {
  1939. char *replmsg = NULL;
  1940. if ( replrc ) {
  1941. replmsg = protocol_response2string(replrc);
  1942. /* Do not mix the unknown replication error with the known ldap one */
  1943. if ( strcasecmp(replmsg, "unknown error") == 0 ) {
  1944. replmsg = NULL;
  1945. }
  1946. }
  1947. PR_snprintf(ra->last_update_status, STATUS_LEN, "%d %s%sLDAP error: %s%s%s",
  1948. ldaprc, message?message:"",message?"":" - ",
  1949. slapi_err2string(ldaprc), replmsg ? " - " : "", replmsg ? replmsg : "");
  1950. }
  1951. /* ldaprc == LDAP_SUCCESS */
  1952. else if (replrc != 0)
  1953. {
  1954. if (replrc == NSDS50_REPL_REPLICA_BUSY)
  1955. {
  1956. PR_snprintf(ra->last_update_status, STATUS_LEN,
  1957. "%d Can't acquire busy replica", replrc );
  1958. }
  1959. else if (replrc == NSDS50_REPL_REPLICA_RELEASE_SUCCEEDED)
  1960. {
  1961. PR_snprintf(ra->last_update_status, STATUS_LEN, "%d %s",
  1962. ldaprc, "Replication session successful");
  1963. }
  1964. else if (replrc == NSDS50_REPL_DISABLED)
  1965. {
  1966. PR_snprintf(ra->last_update_status, STATUS_LEN, "%d Incremental update aborted: "
  1967. "Replication agreement for %s\n can not be updated while the replica is disabled.\n"
  1968. "(If the suffix is disabled you must enable it then restart the server for replication to take place).",
  1969. replrc, ra->long_name ? ra->long_name : "a replica");
  1970. /* Log into the errors log, as "ra->long_name" is not accessible from the caller */
  1971. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1972. "Incremental update aborted: Replication agreement for \"%s\" "
  1973. "can not be updated while the replica is disabled\n", ra->long_name ? ra->long_name : "a replica");
  1974. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1975. "(If the suffix is disabled you must enable it then restart the server for replication to take place).\n");
  1976. }
  1977. else
  1978. {
  1979. PR_snprintf(ra->last_update_status, STATUS_LEN,
  1980. "%d Replication error acquiring replica: %s%s%s",
  1981. replrc, protocol_response2string(replrc),
  1982. message?" - ":"",message?message:"");
  1983. }
  1984. }
  1985. else if (message != NULL) /* replrc == NSDS50_REPL_REPLICA_READY == 0 */
  1986. {
  1987. PR_snprintf(ra->last_update_status, STATUS_LEN,
  1988. "%d Replica acquired successfully: %s",
  1989. ldaprc, message);
  1990. }
  1991. else
  1992. { /* agmt_set_last_update_status(0,0,NULL) to reset agmt */
  1993. PR_snprintf(ra->last_update_status, STATUS_LEN, "%d", ldaprc);
  1994. }
  1995. }
  1996. }
  1997. void
  1998. agmt_set_last_init_status (Repl_Agmt *ra, int ldaprc, int replrc, const char *message)
  1999. {
  2000. PR_ASSERT(NULL != ra);
  2001. if (NULL != ra)
  2002. {
  2003. if (ldaprc != LDAP_SUCCESS)
  2004. {
  2005. char *replmsg = NULL;
  2006. if ( replrc ) {
  2007. replmsg = protocol_response2string(replrc);
  2008. /* Do not mix the unknown replication error with the known ldap one */
  2009. if ( strcasecmp(replmsg, "unknown error") == 0 ) {
  2010. replmsg = NULL;
  2011. }
  2012. }
  2013. PR_snprintf(ra->last_init_status, STATUS_LEN, "%d %s%sLDAP error: %s%s%s",
  2014. ldaprc, message?message:"",message?"":" - ",
  2015. slapi_err2string(ldaprc), replmsg ? " - " : "", replmsg ? replmsg : "");
  2016. }
  2017. /* ldaprc == LDAP_SUCCESS */
  2018. else if (replrc != 0)
  2019. {
  2020. if (replrc == NSDS50_REPL_REPLICA_RELEASE_SUCCEEDED)
  2021. {
  2022. PR_snprintf(ra->last_init_status, STATUS_LEN, "%d %s",
  2023. ldaprc, "Replication session successful");
  2024. }
  2025. else if (replrc == NSDS50_REPL_DISABLED)
  2026. {
  2027. if(agmt_is_enabled(ra)){
  2028. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Total update aborted: "
  2029. "Replication agreement for \"%s\" can not be updated while the suffix is disabled.\n"
  2030. "You must enable it then restart the server for replication to take place).\n",
  2031. ra->long_name ? ra->long_name : "a replica");
  2032. PR_snprintf(ra->last_init_status, STATUS_LEN, "%d Total update aborted: "
  2033. "Replication agreement for \"%s\" can not be updated while the suffix is disabled.\n"
  2034. "You must enable it then restart the server for replication to take place).",
  2035. replrc, ra->long_name ? ra->long_name : "a replica");
  2036. } else {
  2037. /* You do not need to restart the server after enabling the agreement */
  2038. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Total update aborted: "
  2039. "Replication agreement for \"%s\" can not be updated while the agreement is disabled\n",
  2040. ra->long_name ? ra->long_name : "a replica");
  2041. PR_snprintf(ra->last_init_status, STATUS_LEN, "%d Total update aborted: "
  2042. "Replication agreement for \"%s\" can not be updated while the agreement is disabled.",
  2043. replrc, ra->long_name ? ra->long_name : "a replica");
  2044. }
  2045. }
  2046. else
  2047. {
  2048. PR_snprintf(ra->last_init_status, STATUS_LEN,
  2049. "%d Replication error acquiring replica: %s%s%s",
  2050. replrc, protocol_response2string(replrc),
  2051. message?" - ":"",message?message:"");
  2052. }
  2053. }
  2054. else if (message != NULL) /* replrc == NSDS50_REPL_REPLICA_READY == 0 */
  2055. {
  2056. PR_snprintf(ra->last_init_status, STATUS_LEN,
  2057. "%d %s",
  2058. ldaprc, message);
  2059. }
  2060. else
  2061. { /* agmt_set_last_init_status(0,0,NULL) to reset agmt */
  2062. PR_snprintf(ra->last_init_status, STATUS_LEN, "%d", ldaprc);
  2063. }
  2064. }
  2065. }
  2066. void
  2067. agmt_set_update_in_progress (Repl_Agmt *ra, PRBool in_progress)
  2068. {
  2069. PR_ASSERT(NULL != ra);
  2070. if (NULL != ra)
  2071. {
  2072. ra->update_in_progress = in_progress;
  2073. }
  2074. }
  2075. PRBool
  2076. agmt_get_update_in_progress (const Repl_Agmt *ra)
  2077. {
  2078. PR_ASSERT(NULL != ra);
  2079. if (NULL != ra)
  2080. {
  2081. return ra->update_in_progress;
  2082. }
  2083. return PR_FALSE;
  2084. }
  2085. void
  2086. agmt_inc_last_update_changecount (Repl_Agmt *ra, ReplicaId rid, int skipped)
  2087. {
  2088. PR_ASSERT(NULL != ra);
  2089. if (NULL != ra)
  2090. {
  2091. int i;
  2092. for ( i = 0; i < ra->num_changecounters; i++ )
  2093. {
  2094. if ( ra->changecounters[i]->rid == rid )
  2095. break;
  2096. }
  2097. if ( i < ra->num_changecounters )
  2098. {
  2099. if ( skipped )
  2100. ra->changecounters[i]->num_skipped ++;
  2101. else
  2102. ra->changecounters[i]->num_replayed ++;
  2103. }
  2104. else
  2105. {
  2106. ra->num_changecounters ++;
  2107. ra->changecounters[i] = (struct changecounter*) slapi_ch_calloc(1, sizeof(struct changecounter));
  2108. ra->changecounters[i]->rid = rid;
  2109. if ( skipped )
  2110. ra->changecounters[i]->num_skipped = 1;
  2111. else
  2112. ra->changecounters[i]->num_replayed = 1;
  2113. }
  2114. }
  2115. }
  2116. void
  2117. agmt_get_changecount_string (Repl_Agmt *ra, char *buf, int bufsize)
  2118. {
  2119. char tmp_buf[32]; /* 5 digit RID, 10 digit each replayed and skipped */
  2120. int i;
  2121. int buflen = 0;
  2122. *buf = '\0';
  2123. if (NULL != ra)
  2124. {
  2125. for ( i = 0; i < ra->num_changecounters; i++ )
  2126. {
  2127. PR_snprintf (tmp_buf, sizeof(tmp_buf), "%u:%u/%u ",
  2128. ra->changecounters[i]->rid,
  2129. ra->changecounters[i]->num_replayed,
  2130. ra->changecounters[i]->num_skipped);
  2131. PR_snprintf (buf+buflen, bufsize-buflen, "%s", tmp_buf);
  2132. buflen += strlen (tmp_buf);
  2133. }
  2134. }
  2135. }
  2136. static int
  2137. get_agmt_status(Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  2138. int *returncode, char *returntext, void *arg)
  2139. {
  2140. char *time_tmp = NULL;
  2141. char changecount_string[BUFSIZ];
  2142. Repl_Agmt *ra = (Repl_Agmt *)arg;
  2143. PR_ASSERT(NULL != ra);
  2144. if (NULL != ra)
  2145. {
  2146. PRBool reapActive = PR_FALSE;
  2147. Slapi_DN *replarea_sdn = NULL;
  2148. Object *repl_obj = NULL;
  2149. replarea_sdn = agmt_get_replarea(ra);
  2150. repl_obj = replica_get_replica_from_dn(replarea_sdn);
  2151. slapi_sdn_free(&replarea_sdn);
  2152. if (repl_obj) {
  2153. Replica *replica = (Replica*)object_get_data (repl_obj);
  2154. reapActive = replica_get_tombstone_reap_active(replica);
  2155. object_release(repl_obj);
  2156. }
  2157. slapi_entry_attr_set_int(e, "nsds5replicaReapActive", (int)reapActive);
  2158. /* these values persist in the dse.ldif file, so we delete them
  2159. here to avoid multi valued attributes */
  2160. slapi_entry_attr_delete(e, "nsds5replicaLastUpdateStart");
  2161. slapi_entry_attr_delete(e, "nsds5replicaLastUpdateEnd");
  2162. slapi_entry_attr_delete(e, "nsds5replicaChangesSentSinceStartup");
  2163. slapi_entry_attr_delete(e, "nsds5replicaLastUpdateStatus");
  2164. slapi_entry_attr_delete(e, "nsds5replicaUpdateInProgress");
  2165. slapi_entry_attr_delete(e, "nsds5replicaLastInitStart");
  2166. slapi_entry_attr_delete(e, "nsds5replicaLastInitStatus");
  2167. slapi_entry_attr_delete(e, "nsds5replicaLastInitEnd");
  2168. /* now, add the real values (singly) */
  2169. if (ra->last_update_start_time == 0)
  2170. {
  2171. slapi_entry_add_string(e, "nsds5replicaLastUpdateStart", "0");
  2172. }
  2173. else
  2174. {
  2175. time_tmp = format_genTime(ra->last_update_start_time);
  2176. slapi_entry_add_string(e, "nsds5replicaLastUpdateStart", time_tmp);
  2177. slapi_ch_free((void **)&time_tmp);
  2178. }
  2179. if (ra->last_update_end_time == 0)
  2180. {
  2181. slapi_entry_add_string(e, "nsds5replicaLastUpdateEnd", "0");
  2182. }
  2183. else
  2184. {
  2185. time_tmp = format_genTime(ra->last_update_end_time);
  2186. slapi_entry_add_string(e, "nsds5replicaLastUpdateEnd", time_tmp);
  2187. slapi_ch_free((void **)&time_tmp);
  2188. }
  2189. agmt_get_changecount_string (ra, changecount_string, sizeof (changecount_string) );
  2190. slapi_entry_add_string(e, "nsds5replicaChangesSentSinceStartup", changecount_string);
  2191. if (ra->last_update_status[0] == '\0')
  2192. {
  2193. slapi_entry_add_string(e, "nsds5replicaLastUpdateStatus", "0 No replication sessions started since server startup");
  2194. }
  2195. else
  2196. {
  2197. slapi_entry_add_string(e, "nsds5replicaLastUpdateStatus", ra->last_update_status);
  2198. }
  2199. slapi_entry_add_string(e, "nsds5replicaUpdateInProgress", ra->update_in_progress ? "TRUE" : "FALSE");
  2200. if (ra->last_init_start_time == 0)
  2201. {
  2202. slapi_entry_add_string(e, "nsds5replicaLastInitStart", "0");
  2203. }
  2204. else
  2205. {
  2206. time_tmp = format_genTime(ra->last_init_start_time);
  2207. slapi_entry_add_string(e, "nsds5replicaLastInitStart", time_tmp);
  2208. slapi_ch_free((void **)&time_tmp);
  2209. }
  2210. if (ra->last_init_end_time == 0)
  2211. {
  2212. slapi_entry_add_string(e, "nsds5replicaLastInitEnd", "0");
  2213. }
  2214. else
  2215. {
  2216. time_tmp = format_genTime(ra->last_init_end_time);
  2217. slapi_entry_add_string(e, "nsds5replicaLastInitEnd", time_tmp);
  2218. slapi_ch_free((void **)&time_tmp);
  2219. }
  2220. if (ra->last_init_status[0] != '\0')
  2221. {
  2222. slapi_entry_add_string(e, "nsds5replicaLastInitStatus", ra->last_init_status);
  2223. }
  2224. }
  2225. return SLAPI_DSE_CALLBACK_OK;
  2226. }
  2227. static void
  2228. update_window_state_change_callback (void *arg, PRBool opened)
  2229. {
  2230. Repl_Agmt *agmt = (Repl_Agmt*)arg;
  2231. PR_ASSERT (agmt);
  2232. if (opened)
  2233. {
  2234. prot_notify_window_opened (agmt->protocol);
  2235. }
  2236. else
  2237. {
  2238. prot_notify_window_closed (agmt->protocol);
  2239. }
  2240. }
  2241. ReplicaId
  2242. agmt_get_consumer_rid ( Repl_Agmt *agmt, void *conn )
  2243. {
  2244. if ( agmt->consumerRID <= 0 || agmt->tmpConsumerRID) {
  2245. char *mapping_tree_node = NULL;
  2246. struct berval **bvals = NULL;
  2247. /* This function converts the old style DN to the new one. */
  2248. mapping_tree_node =
  2249. slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config",
  2250. slapi_sdn_get_dn (agmt->replarea) );
  2251. if (NULL == mapping_tree_node) {
  2252. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2253. "agmt_get_consumer_rid: failed to normalize "
  2254. "replica dn for %s\n",
  2255. slapi_sdn_get_dn (agmt->replarea));
  2256. agmt->consumerRID = 0;
  2257. }
  2258. conn_read_entry_attribute ( conn, mapping_tree_node, "nsDS5ReplicaID", &bvals );
  2259. if ( NULL != bvals && NULL != bvals[0] ) {
  2260. char *ridstr = slapi_ch_malloc( bvals[0]->bv_len + 1 );
  2261. memcpy ( ridstr, bvals[0]->bv_val, bvals[0]->bv_len );
  2262. ridstr[bvals[0]->bv_len] = '\0';
  2263. agmt->consumerRID = atoi (ridstr);
  2264. slapi_ch_free ( (void**) &ridstr );
  2265. ber_bvecfree ( bvals );
  2266. }
  2267. slapi_ch_free_string(&mapping_tree_node);
  2268. }
  2269. agmt->tmpConsumerRID = 0;
  2270. return agmt->consumerRID;
  2271. }
  2272. int
  2273. get_agmt_agreement_type( Repl_Agmt *agmt)
  2274. {
  2275. PR_ASSERT (agmt);
  2276. return agmt->agreement_type;
  2277. }
  2278. void* agmt_get_priv (const Repl_Agmt *agmt)
  2279. {
  2280. PR_ASSERT (agmt);
  2281. return agmt->priv;
  2282. }
  2283. void agmt_set_priv (Repl_Agmt *agmt, void* priv)
  2284. {
  2285. PR_ASSERT (agmt);
  2286. agmt->priv = priv;
  2287. }
  2288. ReplicaId agmt_get_consumerRID(Repl_Agmt *ra)
  2289. {
  2290. return ra->consumerRID;
  2291. }
  2292. void* agmt_get_connection(Repl_Agmt *ra)
  2293. {
  2294. if(ra->protocol){
  2295. return (void *)prot_get_connection(ra->protocol);
  2296. } else {
  2297. return NULL;
  2298. }
  2299. }
  2300. Repl_Protocol *
  2301. agmt_get_protocol(Repl_Agmt *ra)
  2302. {
  2303. if(ra){
  2304. return ra->protocol;
  2305. } else {
  2306. return NULL;
  2307. }
  2308. }
  2309. int
  2310. agmt_has_protocol(Repl_Agmt *agmt)
  2311. {
  2312. if (agmt) {
  2313. return NULL != agmt->protocol;
  2314. }
  2315. return 0;
  2316. }
  2317. PRBool
  2318. agmt_is_enabled(Repl_Agmt *ra)
  2319. {
  2320. PRBool state;
  2321. PR_Lock(ra->lock);
  2322. state = ra->is_enabled;
  2323. PR_Unlock(ra->lock);
  2324. return state;
  2325. }
  2326. int
  2327. agmt_set_enabled_from_entry(Repl_Agmt *ra, Slapi_Entry *e, char *returntext){
  2328. char *attr_val = NULL;
  2329. int rc = 0;
  2330. if(ra == NULL){
  2331. return -1;
  2332. }
  2333. PR_Lock(ra->lock);
  2334. attr_val = slapi_entry_attr_get_charptr(e, type_nsds5ReplicaEnabled);
  2335. if(attr_val){
  2336. PRBool is_enabled = PR_TRUE;
  2337. if(strcasecmp(attr_val,"off") == 0){
  2338. is_enabled = PR_FALSE;
  2339. } else if(strcasecmp(attr_val,"on") == 0){
  2340. is_enabled = PR_TRUE;
  2341. } else {
  2342. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmt_set_enabled_from_entry: invalid "
  2343. "value for nsds5ReplicaEnabled (%s), the value must be \"on\" or \"off\".\n", attr_val);
  2344. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid value for nsds5ReplicaEnabled, "
  2345. "the value must be \"on\" or \"off\".\n");
  2346. slapi_ch_free_string(&attr_val);
  2347. PR_Unlock(ra->lock);
  2348. return -1;
  2349. }
  2350. slapi_ch_free_string(&attr_val);
  2351. if(is_enabled){
  2352. if(!ra->is_enabled){
  2353. ra->is_enabled = PR_TRUE;
  2354. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmt_set_enabled_from_entry: "
  2355. "agreement is now enabled (%s)\n",ra->long_name);
  2356. PR_Unlock(ra->lock);
  2357. agmt_start(ra);
  2358. return rc;
  2359. }
  2360. } else {
  2361. if(ra->is_enabled){
  2362. ra->is_enabled = PR_FALSE;
  2363. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmt_set_enabled_from_entry: "
  2364. "agreement is now disabled (%s)\n",ra->long_name);
  2365. PR_Unlock(ra->lock);
  2366. agmt_stop(ra);
  2367. agmt_update_consumer_ruv(ra);
  2368. agmt_set_last_update_status(ra,0,0,"agreement disabled");
  2369. return rc;
  2370. }
  2371. }
  2372. } else {
  2373. rc = -1;
  2374. }
  2375. PR_Unlock(ra->lock);
  2376. return rc;
  2377. }
  2378. char **
  2379. agmt_get_attrs_to_strip(Repl_Agmt *ra)
  2380. {
  2381. if(ra){
  2382. return ra->attrs_to_strip;
  2383. } else {
  2384. return NULL;
  2385. }
  2386. }
  2387. int
  2388. agmt_set_attrs_to_strip(Repl_Agmt *ra, Slapi_Entry *e)
  2389. {
  2390. char *tmpstr = NULL;
  2391. PR_Lock(ra->lock);
  2392. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds5ReplicaStripAttrs);
  2393. if (NULL != tmpstr){
  2394. if(ra->attrs_to_strip){
  2395. slapi_ch_array_free(ra->attrs_to_strip);
  2396. }
  2397. ra->attrs_to_strip = slapi_str2charray_ext(tmpstr, " ", 0);
  2398. PR_Unlock(ra->lock);
  2399. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  2400. slapi_ch_free_string(&tmpstr);
  2401. return 0;
  2402. }
  2403. PR_Unlock(ra->lock);
  2404. return -1;
  2405. }
  2406. /* this is called whenever an update (total/incremental)
  2407. is completed */
  2408. void
  2409. agmt_update_done(Repl_Agmt *agmt, int is_total)
  2410. {
  2411. /* we could do a lot of stuff here - consolidate all of the other stuff that gets
  2412. done at the end of an update - setting status, times, etc.
  2413. but for now, all we want to do is "flush" any pending changes made
  2414. during the update into the proper structures so they are in place for the
  2415. next run
  2416. */
  2417. if(get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2418. windows_update_done(agmt, is_total);
  2419. }
  2420. }
  2421. int
  2422. agmt_get_protocol_timeout(Repl_Agmt *agmt)
  2423. {
  2424. return (int)agmt->protocol_timeout;
  2425. }
  2426. /*
  2427. * Check if all the mods are being removed by fractional/stripped attributes
  2428. */
  2429. void
  2430. agmt_update_maxcsn(Replica *r, Slapi_DN *sdn, int op, LDAPMod **mods, CSN *csn)
  2431. {
  2432. Object *agmt_obj;
  2433. Repl_Agmt *agmt;
  2434. ReplicaId rid = replica_get_rid(r);
  2435. int mod_count = 0, excluded_count = 0;
  2436. agmt_obj = agmtlist_get_first_agreement_for_replica (r);
  2437. if(agmt_obj == NULL){ /* no agreements */
  2438. return;
  2439. }
  2440. while (agmt_obj){
  2441. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2442. if(!agmt_is_enabled(agmt) ||
  2443. !slapi_sdn_issuffix(sdn, agmt->replarea) ||
  2444. get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS)
  2445. {
  2446. agmt_obj = agmtlist_get_next_agreement_for_replica (r, agmt_obj);
  2447. continue;
  2448. }
  2449. if(op == SLAPI_OPERATION_MODIFY)
  2450. {
  2451. slapi_rwlock_rdlock(agmt->attr_lock);
  2452. for ( excluded_count = 0, mod_count = 0; NULL != mods[ mod_count ]; mod_count++){
  2453. if(charray_inlist(agmt->frac_attrs, mods[mod_count]->mod_type)){
  2454. excluded_count++;
  2455. } else if(charray_inlist(agmt->attrs_to_strip, mods[mod_count]->mod_type)){
  2456. excluded_count++;
  2457. }
  2458. }
  2459. slapi_rwlock_unlock(agmt->attr_lock);
  2460. }
  2461. if(excluded_count == 0 || excluded_count != mod_count){
  2462. /*
  2463. * This update has not been completely stripped down, update
  2464. * the agmt maxcsn - if the update did not originate from the consumer.
  2465. */
  2466. char maxcsn[CSN_STRSIZE];
  2467. ReplicaId oprid = csn_get_replicaid(csn);
  2468. csn_as_string(csn, PR_FALSE, maxcsn);
  2469. PR_Lock(agmt->lock);
  2470. if(!agmt->consumerRID){
  2471. /*
  2472. * If the RID is 0, that means this is the first update since the
  2473. * agmt was created. Since we have not contacted the consumer yet,
  2474. * we don't know what its rid is. The consumerRID will be set once
  2475. * this update is sent, but until then we don't know it. So for now
  2476. * temporarily mark it as "unavailable".
  2477. */
  2478. slapi_ch_free_string(&agmt->maxcsn);
  2479. agmt->maxcsn = slapi_ch_smprintf("%s;%s;%s;%d;unavailable", slapi_sdn_get_dn(agmt->replarea),
  2480. slapi_rdn_get_value_by_ref(slapi_rdn_get_rdn(agmt->rdn)), agmt->hostname, agmt->port);
  2481. } else if(rid == oprid){
  2482. slapi_ch_free_string(&agmt->maxcsn);
  2483. agmt->maxcsn = slapi_ch_smprintf("%s;%s;%s;%d;%d;%s", slapi_sdn_get_dn(agmt->replarea),
  2484. slapi_rdn_get_value_by_ref(slapi_rdn_get_rdn(agmt->rdn)), agmt->hostname,
  2485. agmt->port, agmt->consumerRID, maxcsn);
  2486. }
  2487. PR_Unlock(agmt->lock);
  2488. }
  2489. agmt_obj = agmtlist_get_next_agreement_for_replica (r, agmt_obj);
  2490. }
  2491. }
  2492. /*
  2493. * Returns the in-memory agmt maxcsn's
  2494. */
  2495. void
  2496. add_agmt_maxcsns(Slapi_Entry *e, Replica *r)
  2497. {
  2498. Object *agmt_obj;
  2499. Repl_Agmt *agmt;
  2500. agmt_obj = agmtlist_get_first_agreement_for_replica (r);
  2501. if(agmt_obj == NULL){ /* no agreements */
  2502. return;
  2503. }
  2504. while (agmt_obj){
  2505. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2506. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2507. agmt_obj = agmtlist_get_next_agreement_for_replica (r, agmt_obj);
  2508. continue;
  2509. }
  2510. PR_Lock(agmt->lock);
  2511. if(agmt->maxcsn){
  2512. slapi_entry_add_string(e, type_agmtMaxCSN, agmt->maxcsn);
  2513. }
  2514. PR_Unlock(agmt->lock);
  2515. agmt_obj = agmtlist_get_next_agreement_for_replica (r, agmt_obj);
  2516. }
  2517. }
  2518. /*
  2519. * Create a smod of all the agmt maxcsns to add to the tombstone entry
  2520. */
  2521. int
  2522. agmt_maxcsn_to_smod (Replica *r, Slapi_Mod *smod)
  2523. {
  2524. Object *agmt_obj;
  2525. Repl_Agmt *agmt;
  2526. int rc = 1;
  2527. agmt_obj = agmtlist_get_first_agreement_for_replica (r);
  2528. if(agmt_obj == NULL){ /* no agreements */
  2529. return rc;
  2530. }
  2531. slapi_mod_init (smod, replica_get_agmt_count(r) + 1);
  2532. slapi_mod_set_type (smod, type_agmtMaxCSN);
  2533. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  2534. while (agmt_obj){
  2535. struct berval val;
  2536. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2537. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2538. agmt_obj = agmtlist_get_next_agreement_for_replica (r, agmt_obj);
  2539. continue;
  2540. }
  2541. PR_Lock(agmt->lock);
  2542. if(agmt->maxcsn == NULL){
  2543. PR_Unlock(agmt->lock);
  2544. agmt_obj = agmtlist_get_next_agreement_for_replica (r, agmt_obj);
  2545. continue;
  2546. }
  2547. val.bv_val = agmt->maxcsn;
  2548. val.bv_len = strlen(val.bv_val);
  2549. slapi_mod_add_value(smod, &val);
  2550. PR_Unlock(agmt->lock);
  2551. rc = 0;
  2552. agmt_obj = agmtlist_get_next_agreement_for_replica (r, agmt_obj);
  2553. }
  2554. return rc;
  2555. }
  2556. /*
  2557. * Called when we start a repl agmt
  2558. */
  2559. void
  2560. agmt_set_maxcsn(Repl_Agmt *ra)
  2561. {
  2562. Slapi_PBlock *pb = NULL;
  2563. Slapi_Entry **entries = NULL;
  2564. Replica *r = NULL;
  2565. Object *repl_obj;
  2566. const Slapi_DN *tombstone_sdn = NULL;
  2567. char *attrs[2];
  2568. int rc;
  2569. /* read ruv state from the ruv tombstone entry */
  2570. pb = slapi_pblock_new();
  2571. if (!pb) {
  2572. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmt_set_maxcsn: Out of memory\n");
  2573. goto done;
  2574. }
  2575. repl_obj = prot_get_replica_object(ra->protocol);
  2576. if(repl_obj){
  2577. r = (Replica *)object_get_data(repl_obj);
  2578. tombstone_sdn = replica_get_root(r);
  2579. }
  2580. ra->maxcsn = NULL;
  2581. attrs[0] = (char*)type_agmtMaxCSN;
  2582. attrs[1] = NULL;
  2583. slapi_search_internal_set_pb_ext(
  2584. pb,
  2585. (Slapi_DN *)tombstone_sdn,
  2586. LDAP_SCOPE_BASE,
  2587. "objectclass=*",
  2588. attrs,
  2589. 0, /* attrsonly */
  2590. NULL, /* controls */
  2591. RUV_STORAGE_ENTRY_UNIQUEID,
  2592. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  2593. OP_FLAG_REPLICATED); /* flags */
  2594. slapi_search_internal_pb (pb);
  2595. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2596. if (rc == LDAP_SUCCESS){
  2597. Replica *r;
  2598. Object *repl_obj;
  2599. char **maxcsns;
  2600. int i;
  2601. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2602. if (NULL == entries || NULL == entries[0]){
  2603. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2604. "agmt_set_maxcsn: replica ruv tombstone entry for "
  2605. "replica %s not found\n",
  2606. slapi_sdn_get_dn(ra->replarea));
  2607. goto done;
  2608. }
  2609. maxcsns = slapi_entry_attr_get_charray(entries[0], type_agmtMaxCSN);
  2610. repl_obj = prot_get_replica_object(ra->protocol);
  2611. if(repl_obj && maxcsns){
  2612. r = (Replica *)object_get_data(repl_obj);
  2613. if(r){
  2614. /*
  2615. * Loop over all the agmt maxcsns and find ours
  2616. */
  2617. for(i = 0; maxcsns[i]; i++){
  2618. char buf[BUFSIZ];
  2619. char unavail_buf[BUFSIZ];
  2620. PR_snprintf(buf,BUFSIZ,"%s;%s;%s;%d;",slapi_sdn_get_dn(ra->replarea),
  2621. slapi_rdn_get_value_by_ref(slapi_rdn_get_rdn(ra->rdn)),
  2622. ra->hostname, ra->port);
  2623. PR_snprintf(unavail_buf, BUFSIZ,"%s;%s;%s;%d;unavailable", slapi_sdn_get_dn(ra->replarea),
  2624. slapi_rdn_get_value_by_ref(slapi_rdn_get_rdn(ra->rdn)),
  2625. ra->hostname, ra->port);
  2626. if(strstr(maxcsns[i], buf) || strstr(maxcsns[i], unavail_buf)){
  2627. ra->maxcsn = slapi_ch_strdup(maxcsns[i]);
  2628. ra->consumerRID = agmt_maxcsn_get_rid(maxcsns[i]);
  2629. ra->tmpConsumerRID = 1;
  2630. break;
  2631. }
  2632. }
  2633. }
  2634. }
  2635. slapi_ch_array_free(maxcsns);
  2636. }
  2637. done:
  2638. if (NULL != pb){
  2639. slapi_free_search_results_internal(pb);
  2640. slapi_pblock_destroy (pb);
  2641. }
  2642. }
  2643. /*
  2644. * Parse out the consumer replicaID from the agmt maxcsn
  2645. *
  2646. * "repl area;agmt_rdn;hostname;port;consumer_rid;maxcsn"
  2647. */
  2648. static ReplicaId
  2649. agmt_maxcsn_get_rid(char *maxcsn)
  2650. {
  2651. ReplicaId rid = 0;
  2652. char *token = NULL;
  2653. char *iter = NULL;
  2654. char *value = slapi_ch_strdup(maxcsn);
  2655. token = ldap_utf8strtok_r(value, ";", &iter); /* repl area */
  2656. token = ldap_utf8strtok_r(iter, ";", &iter); /* agmt rdn */
  2657. token = ldap_utf8strtok_r(iter, ";", &iter); /* host */
  2658. token = ldap_utf8strtok_r(iter, ";", &iter); /* port */
  2659. token = ldap_utf8strtok_r(iter, ";", &iter); /* rid */
  2660. if(token && strcmp(token, "Unavailable")){
  2661. rid = atoi(token);
  2662. }
  2663. slapi_ch_free_string(&value);
  2664. return rid;
  2665. }
  2666. /*
  2667. * Agmt being deleted, remove the agmt maxcsn from the local ruv.
  2668. */
  2669. void
  2670. agmt_remove_maxcsn(Repl_Agmt *ra)
  2671. {
  2672. Slapi_PBlock *pb = NULL;
  2673. Slapi_PBlock *modpb = NULL;
  2674. Slapi_Entry **entries = NULL;
  2675. Replica *r = NULL;
  2676. Object *repl_obj;
  2677. const Slapi_DN *tombstone_sdn = NULL;
  2678. char *attrs[2];
  2679. int rc;
  2680. pb = slapi_pblock_new();
  2681. if (!pb) {
  2682. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmt_set_maxcsn: Out of memory\n");
  2683. goto done;
  2684. }
  2685. ra->maxcsn = NULL;
  2686. repl_obj = prot_get_replica_object(ra->protocol);
  2687. if(repl_obj){
  2688. r = (Replica *)object_get_data(repl_obj);
  2689. tombstone_sdn = replica_get_root(r);
  2690. } else {
  2691. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmt_set_maxcsn: Failed to get repl object.\n");
  2692. goto done;
  2693. }
  2694. attrs[0] = (char*)type_agmtMaxCSN;
  2695. attrs[1] = NULL;
  2696. slapi_search_internal_set_pb_ext(
  2697. pb,
  2698. (Slapi_DN *)tombstone_sdn,
  2699. LDAP_SCOPE_BASE,
  2700. "objectclass=*",
  2701. attrs,
  2702. 0, /* attrsonly */
  2703. NULL, /* controls */
  2704. RUV_STORAGE_ENTRY_UNIQUEID,
  2705. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  2706. OP_FLAG_REPLICATED); /* flags */
  2707. slapi_search_internal_pb (pb);
  2708. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2709. if (rc == LDAP_SUCCESS){
  2710. /*
  2711. * Ok we have the db tombstone entry, start looking through the agmt maxcsns
  2712. * for a match to this replica agmt.
  2713. */
  2714. Slapi_Mod smod;
  2715. LDAPMod *mods[2];
  2716. char **maxcsns = NULL;
  2717. int i;
  2718. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2719. if (NULL == entries || NULL == entries[0]){
  2720. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2721. "agmt_set_maxcsn: replica ruv tombstone entry for "
  2722. "replica %s not found\n", slapi_sdn_get_dn(ra->replarea));
  2723. goto done;
  2724. }
  2725. maxcsns = slapi_entry_attr_get_charray(entries[0], type_agmtMaxCSN);
  2726. repl_obj = prot_get_replica_object(ra->protocol);
  2727. if(repl_obj && maxcsns){
  2728. r = (Replica *)object_get_data(repl_obj);
  2729. if(r){
  2730. /*
  2731. * Loop over all the agmt maxcsns and find ours...
  2732. */
  2733. for(i = 0; maxcsns[i]; i++){
  2734. char buf[BUFSIZ];
  2735. char unavail_buf[BUFSIZ];
  2736. struct berval val;
  2737. PR_snprintf(buf, BUFSIZ,"%s;%s;%s;%d;",slapi_sdn_get_dn(ra->replarea),
  2738. slapi_rdn_get_value_by_ref(slapi_rdn_get_rdn(ra->rdn)),
  2739. ra->hostname, ra->port);
  2740. PR_snprintf(unavail_buf, BUFSIZ, "%s;%s;%s;%d;unavailable",
  2741. slapi_sdn_get_dn(ra->replarea),
  2742. slapi_rdn_get_value_by_ref(slapi_rdn_get_rdn(ra->rdn)),
  2743. ra->hostname, ra->port);
  2744. if(strstr(maxcsns[i], buf) || strstr(maxcsns[i], unavail_buf)){
  2745. /*
  2746. * We found the matching agmt maxcsn, now remove agmt maxcsn
  2747. * from the tombstone entry.
  2748. */
  2749. val.bv_val = maxcsns[i];
  2750. val.bv_len = strlen(maxcsns[i]);
  2751. slapi_mod_init (&smod, 2);
  2752. slapi_mod_set_type (&smod, type_agmtMaxCSN);
  2753. slapi_mod_set_operation (&smod, LDAP_MOD_DELETE | LDAP_MOD_BVALUES);
  2754. slapi_mod_add_value(&smod, &val);
  2755. mods [0] = smod.mod;
  2756. mods [1] = NULL;
  2757. modpb = slapi_pblock_new();
  2758. slapi_modify_internal_set_pb_ext(
  2759. modpb,
  2760. tombstone_sdn,
  2761. mods,
  2762. NULL, /* controls */
  2763. RUV_STORAGE_ENTRY_UNIQUEID,
  2764. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  2765. /* Add OP_FLAG_TOMBSTONE_ENTRY so that this doesn't get logged in the Retro ChangeLog */
  2766. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | OP_FLAG_TOMBSTONE_ENTRY |
  2767. OP_FLAG_REPL_RUV );
  2768. slapi_modify_internal_pb(modpb);
  2769. slapi_pblock_get(modpb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2770. if (rc != LDAP_SUCCESS){
  2771. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2772. "agmt_set_maxcsn: failed to remove agmt maxcsn (%s), error(%d)\n",maxcsns[i], rc);
  2773. }
  2774. slapi_mod_done(&smod);
  2775. slapi_pblock_destroy(modpb);
  2776. break;
  2777. }
  2778. }
  2779. }
  2780. }
  2781. slapi_ch_array_free(maxcsns);
  2782. }
  2783. done:
  2784. if (NULL != pb){
  2785. slapi_free_search_results_internal(pb);
  2786. slapi_pblock_destroy (pb);
  2787. }
  2788. }