repl5_agmt.c 89 KB

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