1
0

repl5_agmt.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* repl5_agmt.c */
  42. /*
  43. Support for 5.0-style replication agreements.
  44. Directory Server 5.0 replication agreements contain information about
  45. replication consumers that we are supplying.
  46. This module encapsulates the methods available for adding, deleting,
  47. modifying, and firing replication agreements.
  48. Methods:
  49. agmt_new - Create a new replication agreement, in response to a new
  50. replication agreement being added over LDAP.
  51. agmt_delete - Destroy an agreement. It is an error to destroy an
  52. agreement that has not been stopped.
  53. agmt_getstatus - get the status of this replication agreement.
  54. agmt_replicate_now - initiate a replication session asap, even if the
  55. schedule says we shouldn't.
  56. agmt_start - start replicating, according to schedule. Starts a new
  57. thread to handle replication.
  58. agmt_stop - stop replicating asap and end replication thread.
  59. agmt_notify_change - notify the replication agreement about a change that
  60. has been logged. The replication agreement will
  61. decide if it needs to take some action, e.g. start a
  62. replication session.
  63. agmt_initialize_replica - start a complete replica refresh.
  64. agmt_set_schedule_from_entry - (re)set the schedule associated with this
  65. replication agreement based on a RA entry's contents.
  66. agmt_set_credentials_from_entry - (re)set the credentials used to bind
  67. to the remote replica.
  68. agmt_set_binddn_from_entry - (re)set the DN used to bind
  69. to the remote replica.
  70. agmt_set_bind_method_from_entry - (re)set the bind method used to bind
  71. to the remote replica (SIMPLE or SSLCLIENTAUTH).
  72. agmt_set_transportinfo_from_entry - (re)set the transport used to bind
  73. to the remote replica (SSL or not)
  74. */
  75. #include "repl5.h"
  76. #include "repl5_prot_private.h"
  77. #include "cl5_api.h"
  78. #include "slapi-plugin.h"
  79. #define DEFAULT_TIMEOUT 600 /* (seconds) default outbound LDAP connection */
  80. #define STATUS_LEN 1024
  81. struct changecounter {
  82. ReplicaId rid;
  83. PRUint32 num_replayed;
  84. PRUint32 num_skipped;
  85. };
  86. typedef struct repl5agmt {
  87. char *hostname; /* remote hostname */
  88. int port; /* port of remote server */
  89. PRUint32 transport_flags; /* SSL, TLS, etc. */
  90. char *binddn; /* DN to bind as */
  91. struct berval *creds; /* Password, or certificate */
  92. int bindmethod; /* Bind method - simple, SSL */
  93. Slapi_DN *replarea; /* DN of replicated area */
  94. char **frac_attrs; /* list of fractional attributes to be replicated */
  95. Schedule *schedule; /* Scheduling information */
  96. int auto_initialize; /* 1 = automatically re-initialize replica */
  97. const Slapi_DN *dn; /* DN of replication agreement entry */
  98. const Slapi_RDN *rdn; /* RDN of replication agreement entry */
  99. char *long_name; /* Long name (rdn + host, port) of entry, for logging */
  100. Repl_Protocol *protocol; /* Protocol object - manages protocol */
  101. struct changecounter *changecounters[MAX_NUM_OF_MASTERS]; /* changes sent/skipped since server start up */
  102. int num_changecounters;
  103. time_t last_update_start_time; /* Local start time of last update session */
  104. time_t last_update_end_time; /* Local end time of last update session */
  105. char last_update_status[STATUS_LEN]; /* Status of last update. Format = numeric code <space> textual description */
  106. PRBool update_in_progress;
  107. time_t last_init_start_time; /* Local start time of last total init */
  108. time_t last_init_end_time; /* Local end time of last total init */
  109. char last_init_status[STATUS_LEN]; /* Status of last total init. Format = numeric code <space> textual description */
  110. PRLock *lock;
  111. Object *consumerRUV; /* last RUV received from the consumer - used for changelog purging */
  112. CSN *consumerSchemaCSN; /* last schema CSN received from the consumer */
  113. ReplicaId consumerRID; /* indicates if the consumer is the originator of a CSN */
  114. long timeout; /* timeout (in seconds) for outbound LDAP connections to remote server */
  115. PRBool stop_in_progress; /* set by agmt_stop when shutting down */
  116. long busywaittime; /* time in seconds to wait after getting a REPLICA BUSY from the consumer -
  117. to allow another supplier to finish sending its updates -
  118. if set to 0, this means to use the default value if we get a busy
  119. signal from the consumer */
  120. long pausetime; /* time in seconds to pause after sending updates -
  121. to allow another supplier to send its updates -
  122. should be greater than busywaittime -
  123. if set to 0, this means do not pause */
  124. void *priv; /* private data, used for windows-specific agreement data
  125. for sync agreements or for replication session plug-in
  126. private data for normal replication agreements */
  127. int agreement_type;
  128. } repl5agmt;
  129. /* Forward declarations */
  130. void agmt_delete(void **rap);
  131. static void update_window_state_change_callback (void *arg, PRBool opened);
  132. static int get_agmt_status(Slapi_PBlock *pb, Slapi_Entry* e,
  133. Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  134. static int agmt_set_bind_method_no_lock(Repl_Agmt *ra, const Slapi_Entry *e);
  135. static int agmt_set_transportinfo_no_lock(Repl_Agmt *ra, const Slapi_Entry *e);
  136. /*
  137. Schema for replication agreement:
  138. cn
  139. nsds5ReplicaHost - hostname
  140. nsds5ReplicaPort - port number
  141. nsds5ReplicaTransportInfo - "SSL", "startTLS", or may be absent;
  142. nsds5ReplicaBindDN
  143. nsds5ReplicaCredentials
  144. nsds5ReplicaBindMethod - "SIMPLE" or "SSLCLIENTAUTH".
  145. nsds5ReplicaRoot - Replicated suffix
  146. nsds5ReplicatedAttributeList - Unused so far (meant for fractional repl).
  147. nsds5ReplicaUpdateSchedule
  148. nsds5ReplicaTimeout - Outbound repl operations timeout
  149. nsds50ruv - consumer's RUV
  150. nsds5ReplicaBusyWaitTime - time to wait after getting a REPLICA BUSY from the consumer
  151. nsds5ReplicaSessionPauseTime - time to pause after sending updates to allow another supplier to send
  152. */
  153. /*
  154. * Validate an agreement, making sure that it's valid.
  155. * Return 1 if the agreement is valid, 0 otherwise.
  156. */
  157. static int
  158. agmt_is_valid(Repl_Agmt *ra)
  159. {
  160. int return_value = 1; /* assume valid, initially */
  161. PR_ASSERT(NULL != ra);
  162. PR_ASSERT(NULL != ra->dn);
  163. if (NULL == ra->hostname)
  164. {
  165. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  166. "is malformed: missing host name.\n", slapi_sdn_get_dn(ra->dn));
  167. return_value = 0;
  168. }
  169. if (ra->port <= 0)
  170. {
  171. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  172. "is malformed: invalid port number %d.\n", slapi_sdn_get_dn(ra->dn), ra->port);
  173. return_value = 0;
  174. }
  175. if (ra->timeout < 0)
  176. {
  177. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  178. "is malformed: invalid timeout %ld.\n", slapi_sdn_get_dn(ra->dn), ra->timeout);
  179. return_value = 0;
  180. }
  181. if (ra->busywaittime < 0)
  182. {
  183. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  184. "is malformed: invalid busy wait time %ld.\n", slapi_sdn_get_dn(ra->dn), ra->busywaittime);
  185. return_value = 0;
  186. }
  187. if (ra->pausetime < 0)
  188. {
  189. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  190. "is malformed: invalid pausetime %ld.\n", slapi_sdn_get_dn(ra->dn), ra->pausetime);
  191. return_value = 0;
  192. }
  193. if ((0 != ra->transport_flags) && (BINDMETHOD_SASL_GSSAPI == ra->bindmethod)) {
  194. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  195. " is malformed: cannot use SASL/GSSAPI if using SSL or TLS - please "
  196. "change %s to LDAP before changing %s to use SASL/GSSAPI\n",
  197. slapi_sdn_get_dn(ra->dn), type_nsds5TransportInfo, type_nsds5ReplicaBindMethod);
  198. return_value = 0;
  199. }
  200. if ((0 == ra->transport_flags) && (BINDMETHOD_SSL_CLIENTAUTH == ra->bindmethod)) {
  201. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Replication agreement \"%s\" "
  202. " is malformed: cannot use SSLCLIENTAUTH if using plain LDAP - please "
  203. "change %s to SSL or TLS before changing %s to use SSLCLIENTAUTH\n",
  204. slapi_sdn_get_dn(ra->dn), type_nsds5TransportInfo, type_nsds5ReplicaBindMethod);
  205. return_value = 0;
  206. }
  207. return return_value;
  208. }
  209. Repl_Agmt *
  210. agmt_new_from_entry(Slapi_Entry *e)
  211. {
  212. Repl_Agmt *ra;
  213. char *tmpstr;
  214. Slapi_Attr *sattr;
  215. char **denied_attrs = NULL;
  216. char *auto_initialize = NULL;
  217. char *val_nsds5BeginReplicaRefresh = "start";
  218. ra = (Repl_Agmt *)slapi_ch_calloc(1, sizeof(repl5agmt));
  219. if ((ra->lock = PR_NewLock()) == NULL)
  220. {
  221. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Unable to create new lock "
  222. "for replication agreement \"%s\" - agreement ignored.\n",
  223. slapi_entry_get_dn_const(e));
  224. goto loser;
  225. }
  226. /* Find all the stuff we need for the agreement */
  227. /* To Allow Consumer Initialisation when adding an agreement: */
  228. /*
  229. Using 'auto_initialize' member of 'repl5agmt' structure to
  230. store the effect of 'nsds5BeginReplicaRefresh' attribute's value
  231. in it.
  232. */
  233. auto_initialize = slapi_entry_attr_get_charptr(e, type_nsds5BeginReplicaRefresh);
  234. if ((auto_initialize != NULL) && (strcasecmp(auto_initialize, val_nsds5BeginReplicaRefresh) == 0))
  235. {
  236. ra->auto_initialize = STATE_PERFORMING_TOTAL_UPDATE;
  237. }
  238. else
  239. {
  240. ra->auto_initialize = STATE_PERFORMING_INCREMENTAL_UPDATE;
  241. }
  242. if (auto_initialize)
  243. {
  244. slapi_ch_free_string (&auto_initialize);
  245. }
  246. /* Host name of remote replica */
  247. ra->hostname = slapi_entry_attr_get_charptr(e, type_nsds5ReplicaHost);
  248. /* Port number for remote replica instance */
  249. ra->port = slapi_entry_attr_get_int(e, type_nsds5ReplicaPort);
  250. /* SSL, TLS, or other transport stuff */
  251. ra->transport_flags = 0;
  252. agmt_set_transportinfo_no_lock(ra, e);
  253. /* DN to use when binding. May be empty if cert-based auth is to be used. */
  254. ra->binddn = slapi_entry_attr_get_charptr(e, type_nsds5ReplicaBindDN);
  255. if (NULL == ra->binddn)
  256. {
  257. ra->binddn = slapi_ch_strdup("");
  258. }
  259. /* Credentials to use when binding. */
  260. ra->creds = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  261. ra->creds->bv_val = NULL;
  262. ra->creds->bv_len = 0;
  263. if (slapi_entry_attr_find(e, type_nsds5ReplicaCredentials, &sattr) == 0)
  264. {
  265. Slapi_Value *sval;
  266. if (slapi_attr_first_value(sattr, &sval) == 0)
  267. {
  268. const struct berval *bv = slapi_value_get_berval(sval);
  269. if (NULL != bv)
  270. {
  271. ra->creds->bv_val = slapi_ch_malloc(bv->bv_len + 1);
  272. memcpy(ra->creds->bv_val, bv->bv_val, bv->bv_len);
  273. ra->creds->bv_len = bv->bv_len;
  274. ra->creds->bv_val[bv->bv_len] = '\0'; /* be safe */
  275. }
  276. }
  277. }
  278. /* How to bind */
  279. (void)agmt_set_bind_method_no_lock(ra, e);
  280. /* timeout. */
  281. ra->timeout = DEFAULT_TIMEOUT;
  282. if (slapi_entry_attr_find(e, type_nsds5ReplicaTimeout, &sattr) == 0)
  283. {
  284. Slapi_Value *sval;
  285. if (slapi_attr_first_value(sattr, &sval) == 0)
  286. {
  287. ra->timeout = slapi_value_get_long(sval);
  288. }
  289. }
  290. /* DN of entry at root of replicated area */
  291. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds5ReplicaRoot);
  292. if (NULL != tmpstr)
  293. {
  294. ra->replarea = slapi_sdn_new_dn_passin(tmpstr);
  295. }
  296. /* Replication schedule */
  297. ra->schedule = schedule_new(update_window_state_change_callback, ra, agmt_get_long_name(ra));
  298. if (slapi_entry_attr_find(e, type_nsds5ReplicaUpdateSchedule, &sattr) == 0)
  299. {
  300. schedule_set(ra->schedule, sattr);
  301. }
  302. /* busy wait time - time to wait after getting REPLICA BUSY from consumer */
  303. ra->busywaittime = slapi_entry_attr_get_long(e, type_nsds5ReplicaBusyWaitTime);
  304. /* pause time - time to pause after a session has ended */
  305. ra->pausetime = slapi_entry_attr_get_long(e, type_nsds5ReplicaSessionPauseTime);
  306. /* consumer's RUV */
  307. if (slapi_entry_attr_find(e, type_ruvElement, &sattr) == 0)
  308. {
  309. RUV *ruv;
  310. if (ruv_init_from_slapi_attr(sattr, &ruv) == 0)
  311. {
  312. ra->consumerRUV = object_new (ruv, (FNFree)ruv_destroy);
  313. }
  314. }
  315. ra->consumerRID = 0;
  316. /* DN and RDN of the replication agreement entry itself */
  317. ra->dn = slapi_sdn_dup(slapi_entry_get_sdn((Slapi_Entry *)e));
  318. ra->rdn = slapi_rdn_new_sdn(ra->dn);
  319. /* Compute long name */
  320. {
  321. const char *agmtname = slapi_rdn_get_rdn(ra->rdn);
  322. char hostname[128];
  323. char *dot;
  324. strncpy(hostname, ra->hostname ? ra->hostname : "(unknown)", sizeof(hostname));
  325. hostname[sizeof(hostname)-1] = '\0';
  326. dot = strchr(hostname, '.');
  327. if (dot) {
  328. *dot = '\0';
  329. }
  330. ra->long_name = slapi_ch_smprintf("agmt=\"%s\" (%s:%d)", agmtname, hostname, ra->port);
  331. }
  332. /* DBDB: review this code */
  333. if (slapi_entry_attr_hasvalue(e, "objectclass", "nsDSWindowsReplicationAgreement"))
  334. {
  335. ra->agreement_type = REPLICA_TYPE_WINDOWS;
  336. windows_init_agreement_from_entry(ra,e);
  337. }
  338. else
  339. {
  340. ra->agreement_type = REPLICA_TYPE_MULTIMASTER;
  341. repl_session_plugin_call_agmt_init_cb(ra);
  342. }
  343. /* Initialize status information */
  344. ra->last_update_start_time = 0UL;
  345. ra->last_update_end_time = 0UL;
  346. ra->num_changecounters = 0;
  347. ra->last_update_status[0] = '\0';
  348. ra->update_in_progress = PR_FALSE;
  349. ra->stop_in_progress = PR_FALSE;
  350. ra->last_init_end_time = 0UL;
  351. ra->last_init_start_time = 0UL;
  352. ra->last_init_status[0] = '\0';
  353. /* Fractional attributes */
  354. slapi_entry_attr_find(e, type_nsds5ReplicatedAttributeList, &sattr);
  355. /* New set of excluded attributes */
  356. /* Note: even if sattrs is empty, we have to call this func since there
  357. * could be a default excluded attr list in cn=plugin default config */
  358. if (agmt_set_replicated_attributes_from_attr(ra, sattr) != 0)
  359. {
  360. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  361. "agmtlist_add_callback: failed to parse "
  362. "replicated attributes for agreement %s\n",
  363. agmt_get_long_name(ra));
  364. }
  365. /* Check that there are no verboten attributes in the exclude list */
  366. denied_attrs = agmt_validate_replicated_attributes(ra);
  367. if (denied_attrs)
  368. {
  369. /* Report the error to the client */
  370. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  371. "WARNING: Attempt to exclude illegal attributes "
  372. "from a fractional agreement\n");
  373. /* Free the list */
  374. slapi_ch_array_free(denied_attrs);
  375. goto loser;
  376. }
  377. if (!agmt_is_valid(ra))
  378. {
  379. goto loser;
  380. }
  381. /* Now that the agreement is done, just check if changelog is configured */
  382. if (cl5GetState() != CL5_STATE_OPEN) {
  383. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "WARNING: "
  384. "Replication agreement added but there is no changelog configured. "
  385. "No change will be replicated until a changelog is configured.\n");
  386. }
  387. /*
  388. * Establish a callback for this agreement's entry, so we can
  389. * adorn it with status information when read.
  390. */
  391. slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, slapi_sdn_get_ndn(ra->dn),
  392. LDAP_SCOPE_BASE, "(objectclass=*)", get_agmt_status, ra);
  393. return ra;
  394. loser:
  395. agmt_delete((void **)&ra);
  396. return NULL;
  397. }
  398. Repl_Agmt *
  399. agmt_new_from_pblock(Slapi_PBlock *pb)
  400. {
  401. Slapi_Entry *e;
  402. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  403. return agmt_new_from_entry(e);
  404. }
  405. /*
  406. This should never be called directly - only should be called
  407. as a destructor. XXXggood this is not finished
  408. */
  409. void
  410. agmt_delete(void **rap)
  411. {
  412. Repl_Agmt *ra;
  413. PR_ASSERT(NULL != rap);
  414. PR_ASSERT(NULL != *rap);
  415. ra = (Repl_Agmt *)*rap;
  416. /* do prot_delete first - we may be doing some processing using this
  417. replication agreement, and prot_delete will make sure the
  418. processing is complete - then it should be safe to clean up the
  419. other fields below
  420. */
  421. prot_delete(&ra->protocol);
  422. /*
  423. * Remove the callback for this agreement's entry
  424. */
  425. slapi_config_remove_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP,
  426. slapi_sdn_get_ndn(ra->dn),
  427. LDAP_SCOPE_BASE, "(objectclass=*)",
  428. get_agmt_status);
  429. /*
  430. * Call the replication session cleanup callback. We
  431. * need to do this before we free replarea.
  432. */
  433. if (ra->agreement_type != REPLICA_TYPE_WINDOWS) {
  434. repl_session_plugin_call_destroy_agmt_cb(ra);
  435. }
  436. /* slapi_ch_free accepts NULL pointer */
  437. slapi_ch_free((void **)&(ra->hostname));
  438. slapi_ch_free((void **)&(ra->binddn));
  439. slapi_ch_array_free(ra->frac_attrs);
  440. if (NULL != ra->creds)
  441. {
  442. /* XXX free berval */
  443. }
  444. if (NULL != ra->replarea)
  445. {
  446. slapi_sdn_free(&ra->replarea);
  447. }
  448. if (NULL != ra->consumerRUV)
  449. {
  450. object_release (ra->consumerRUV);
  451. }
  452. csn_free (&ra->consumerSchemaCSN);
  453. while ( --(ra->num_changecounters) >= 0 )
  454. {
  455. slapi_ch_free((void **)&ra->changecounters[ra->num_changecounters]);
  456. }
  457. if (ra->agreement_type == REPLICA_TYPE_WINDOWS)
  458. {
  459. windows_agreement_delete(ra);
  460. }
  461. schedule_destroy(ra->schedule);
  462. slapi_ch_free((void **)&ra->long_name);
  463. slapi_ch_free((void **)rap);
  464. }
  465. /*
  466. * Allow replication for this replica to begin. Replication will
  467. * occur at the next scheduled time. Returns 0 on success, -1 on
  468. * failure.
  469. */
  470. int
  471. agmt_start(Repl_Agmt *ra)
  472. {
  473. Repl_Protocol *prot = NULL;
  474. int protocol_state;
  475. /* To Allow Consumer Initialisation when adding an agreement: */
  476. if (ra->auto_initialize == STATE_PERFORMING_TOTAL_UPDATE)
  477. {
  478. protocol_state = STATE_PERFORMING_TOTAL_UPDATE;
  479. }
  480. else
  481. {
  482. protocol_state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  483. }
  484. /* First, create a new protocol object */
  485. if ((prot = prot_new(ra, protocol_state)) == NULL) {
  486. return -1;
  487. }
  488. /* Now it is safe to own the agreement lock */
  489. PR_Lock(ra->lock);
  490. /* Check that replication is not already started */
  491. if (ra->protocol != NULL) {
  492. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replication already started for agreement \"%s\"\n", agmt_get_long_name(ra));
  493. PR_Unlock(ra->lock);
  494. prot_free(&prot);
  495. return 0;
  496. }
  497. ra->protocol = prot;
  498. /* Start the protocol thread */
  499. prot_start(ra->protocol);
  500. PR_Unlock(ra->lock);
  501. return 0;
  502. }
  503. /*
  504. * Allow replication for this replica to begin. Replication will
  505. * occur at the next scheduled time. Returns 0 on success, -1 on
  506. * failure.
  507. */
  508. int
  509. windows_agmt_start(Repl_Agmt *ra)
  510. {
  511. Repl_Protocol *prot = NULL;
  512. int protocol_state;
  513. /* To Allow Consumer Initialisation when adding an agreement: */
  514. if (ra->auto_initialize == STATE_PERFORMING_TOTAL_UPDATE)
  515. {
  516. protocol_state = STATE_PERFORMING_TOTAL_UPDATE;
  517. }
  518. else
  519. {
  520. protocol_state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  521. }
  522. /* First, create a new protocol object */
  523. if ((prot = prot_new(ra, protocol_state)) == NULL) {
  524. return -1;
  525. }
  526. /* Now it is safe to own the agreement lock */
  527. PR_Lock(ra->lock);
  528. /* Check that replication is not already started */
  529. if (ra->protocol != NULL) {
  530. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replication already started for agreement \"%s\"\n", agmt_get_long_name(ra));
  531. PR_Unlock(ra->lock);
  532. prot_free(&prot);
  533. return 0;
  534. }
  535. ra->protocol = prot;
  536. /* Start the protocol thread */
  537. prot_start(ra->protocol);
  538. PR_Unlock(ra->lock);
  539. return 0;
  540. }
  541. /*
  542. Cease replicating to this replica as soon as possible.
  543. */
  544. int
  545. agmt_stop(Repl_Agmt *ra)
  546. {
  547. int return_value = 0;
  548. Repl_Protocol *rp = NULL;
  549. PR_Lock(ra->lock);
  550. if (ra->stop_in_progress)
  551. {
  552. PR_Unlock(ra->lock);
  553. return return_value;
  554. }
  555. ra->stop_in_progress = PR_TRUE;
  556. rp = ra->protocol;
  557. PR_Unlock(ra->lock);
  558. if (NULL != rp) /* we use this pointer outside the lock - dangerous? */
  559. {
  560. prot_stop(rp);
  561. }
  562. PR_Lock(ra->lock);
  563. ra->stop_in_progress = PR_FALSE;
  564. /* we do not reuse the protocol object so free it */
  565. prot_free(&ra->protocol);
  566. PR_Unlock(ra->lock);
  567. return return_value;
  568. }
  569. /*
  570. Send any pending updates as soon as possible, ignoring any replication
  571. schedules.
  572. */
  573. int
  574. agmt_replicate_now(Repl_Agmt *ra)
  575. {
  576. int return_value = 0;
  577. return return_value;
  578. }
  579. /*
  580. * Return a copy of the remote replica's hostname.
  581. */
  582. char *
  583. agmt_get_hostname(const Repl_Agmt *ra)
  584. {
  585. char *return_value;
  586. PR_ASSERT(NULL != ra);
  587. PR_Lock(ra->lock);
  588. return_value = slapi_ch_strdup(ra->hostname);
  589. PR_Unlock(ra->lock);
  590. return return_value;
  591. }
  592. /*
  593. * Return the port number of the remote replica's instance.
  594. */
  595. int
  596. agmt_get_port(const Repl_Agmt *ra)
  597. {
  598. int return_value;
  599. PR_ASSERT(NULL != ra);
  600. PR_Lock(ra->lock);
  601. return_value = ra->port;
  602. PR_Unlock(ra->lock);
  603. return return_value;
  604. }
  605. /*
  606. * Return the transport flags for this agreement.
  607. */
  608. PRUint32
  609. agmt_get_transport_flags(const Repl_Agmt *ra)
  610. {
  611. unsigned int return_value;
  612. PR_ASSERT(NULL != ra);
  613. PR_Lock(ra->lock);
  614. return_value = ra->transport_flags;
  615. PR_Unlock(ra->lock);
  616. return return_value;
  617. }
  618. /*
  619. * Return a copy of the bind dn to be used with this
  620. * agreement (may return NULL if no binddn is required,
  621. * e.g. SSL client auth.
  622. */
  623. char *
  624. agmt_get_binddn(const Repl_Agmt *ra)
  625. {
  626. char *return_value;
  627. PR_ASSERT(NULL != ra);
  628. PR_Lock(ra->lock);
  629. return_value = ra->binddn == NULL ? NULL : slapi_ch_strdup(ra->binddn);
  630. PR_Unlock(ra->lock);
  631. return return_value;
  632. }
  633. /*
  634. * Return a copy of the credentials.
  635. */
  636. struct berval *
  637. agmt_get_credentials(const Repl_Agmt *ra)
  638. {
  639. struct berval *return_value;
  640. PR_ASSERT(NULL != ra);
  641. PR_Lock(ra->lock);
  642. return_value = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  643. return_value->bv_val = (char *)slapi_ch_malloc(ra->creds->bv_len + 1);
  644. return_value->bv_len = ra->creds->bv_len;
  645. memcpy(return_value->bv_val, ra->creds->bv_val, ra->creds->bv_len);
  646. return_value->bv_val[return_value->bv_len] = '\0'; /* just in case */
  647. PR_Unlock(ra->lock);
  648. return return_value;
  649. }
  650. int
  651. agmt_get_bindmethod(const Repl_Agmt *ra)
  652. {
  653. int return_value;
  654. PR_ASSERT(NULL != ra);
  655. PR_Lock(ra->lock);
  656. return_value = ra->bindmethod;
  657. PR_Unlock(ra->lock);
  658. return return_value;
  659. }
  660. /*
  661. * Return a copy of the dn at the top of the replicated area.
  662. */
  663. Slapi_DN *
  664. agmt_get_replarea(const Repl_Agmt *ra)
  665. {
  666. Slapi_DN *return_value;
  667. PR_ASSERT(NULL != ra);
  668. PR_Lock(ra->lock);
  669. return_value = slapi_sdn_new();
  670. slapi_sdn_copy(ra->replarea, return_value);
  671. PR_Unlock(ra->lock);
  672. return return_value;
  673. }
  674. int
  675. agmt_is_fractional(const Repl_Agmt *ra)
  676. {
  677. int return_value;
  678. PR_ASSERT(NULL != ra);
  679. PR_Lock(ra->lock);
  680. return_value = ra->frac_attrs != NULL;
  681. PR_Unlock(ra->lock);
  682. return return_value;
  683. }
  684. /* Returns a COPY of the attr list, remember to free it */
  685. char **
  686. agmt_get_fractional_attrs(const Repl_Agmt *ra)
  687. {
  688. char ** return_value = NULL;
  689. PR_ASSERT(NULL != ra);
  690. if (NULL == ra->frac_attrs)
  691. {
  692. return NULL;
  693. }
  694. PR_Lock(ra->lock);
  695. return_value = charray_dup(ra->frac_attrs);
  696. PR_Unlock(ra->lock);
  697. return return_value;
  698. }
  699. int
  700. agmt_is_fractional_attr(const Repl_Agmt *ra, const char *attrname)
  701. {
  702. int return_value;
  703. PR_ASSERT(NULL != ra);
  704. if (NULL == ra->frac_attrs)
  705. {
  706. return 0;
  707. }
  708. PR_Lock(ra->lock);
  709. /* Scan the list looking for a match */
  710. return_value = charray_inlist(ra->frac_attrs,(char*)attrname);
  711. PR_Unlock(ra->lock);
  712. return return_value;
  713. }
  714. int
  715. agmt_get_auto_initialize(const Repl_Agmt *ra)
  716. {
  717. int return_value;
  718. PR_ASSERT(NULL != ra);
  719. PR_Lock(ra->lock);
  720. return_value = ra->auto_initialize;
  721. PR_Unlock(ra->lock);
  722. return return_value;
  723. }
  724. long
  725. agmt_get_timeout(const Repl_Agmt *ra)
  726. {
  727. long return_value;
  728. PR_ASSERT(NULL != ra);
  729. PR_Lock(ra->lock);
  730. return_value = ra->timeout;
  731. PR_Unlock(ra->lock);
  732. return return_value;
  733. }
  734. long
  735. agmt_get_busywaittime(const Repl_Agmt *ra)
  736. {
  737. long return_value;
  738. PR_ASSERT(NULL != ra);
  739. PR_Lock(ra->lock);
  740. return_value = ra->busywaittime;
  741. PR_Unlock(ra->lock);
  742. return return_value;
  743. }
  744. long
  745. agmt_get_pausetime(const Repl_Agmt *ra)
  746. {
  747. long return_value;
  748. PR_ASSERT(NULL != ra);
  749. PR_Lock(ra->lock);
  750. return_value = ra->pausetime;
  751. PR_Unlock(ra->lock);
  752. return return_value;
  753. }
  754. /*
  755. * Warning - reference to the long name of the agreement is returned.
  756. * The long name of an agreement is the DN of the agreement entry,
  757. * followed by the host/port for the replica.
  758. */
  759. const char *
  760. agmt_get_long_name(const Repl_Agmt *ra)
  761. {
  762. char *return_value = NULL;
  763. return_value = ra ? ra->long_name : "";
  764. return return_value;
  765. }
  766. /*
  767. * Warning - reference to dn is returned. However, since the dn of
  768. * the replication agreement is its name, it won't change during the
  769. * lifetime of the replication agreement object.
  770. */
  771. const Slapi_DN *
  772. agmt_get_dn_byref(const Repl_Agmt *ra)
  773. {
  774. const Slapi_DN *return_value = NULL;
  775. PR_ASSERT(NULL != ra);
  776. if (NULL != ra)
  777. {
  778. return_value = ra->dn;
  779. }
  780. return return_value;
  781. }
  782. /* Return 1 if name matches the replication Dn, 0 otherwise */
  783. int
  784. agmt_matches_name(const Repl_Agmt *ra, const Slapi_DN *name)
  785. {
  786. int return_value = 0;
  787. PR_ASSERT(NULL != ra);
  788. if (NULL != ra)
  789. {
  790. PR_Lock(ra->lock);
  791. if (slapi_sdn_compare(name, ra->dn) == 0)
  792. {
  793. return_value = 1;
  794. }
  795. PR_Unlock(ra->lock);
  796. }
  797. return return_value;
  798. }
  799. /* Return 1 if name matches the replication area, 0 otherwise */
  800. int
  801. agmt_replarea_matches(const Repl_Agmt *ra, const Slapi_DN *name)
  802. {
  803. int return_value = 0;
  804. PR_ASSERT(NULL != ra);
  805. if (NULL != ra)
  806. {
  807. PR_Lock(ra->lock);
  808. if (slapi_sdn_compare(name, ra->replarea) == 0)
  809. {
  810. return_value = 1;
  811. }
  812. PR_Unlock(ra->lock);
  813. }
  814. return return_value;
  815. }
  816. int
  817. agmt_schedule_in_window_now(const Repl_Agmt *ra)
  818. {
  819. int return_value;
  820. PR_ASSERT(NULL != ra);
  821. PR_Lock(ra->lock);
  822. if (NULL != ra->schedule && schedule_in_window_now(ra->schedule))
  823. {
  824. return_value = 1;
  825. }
  826. else
  827. {
  828. return_value = 0;
  829. }
  830. PR_Unlock(ra->lock);
  831. return return_value;
  832. }
  833. /*
  834. * Set or reset the credentials used to bind to the remote replica.
  835. *
  836. * Returns 0 if credentials set, or -1 if an error occurred.
  837. */
  838. int
  839. agmt_set_credentials_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  840. {
  841. Slapi_Attr *sattr = NULL;
  842. int return_value = 0;
  843. PR_ASSERT(NULL != ra);
  844. slapi_entry_attr_find(e, type_nsds5ReplicaCredentials, &sattr);
  845. PR_Lock(ra->lock);
  846. slapi_ch_free((void **)&ra->creds->bv_val);
  847. ra->creds->bv_len = 0;
  848. if (NULL != sattr)
  849. {
  850. Slapi_Value *sval = NULL;
  851. slapi_attr_first_value(sattr, &sval);
  852. if (NULL != sval)
  853. {
  854. const struct berval *bv = slapi_value_get_berval(sval);
  855. ra->creds->bv_val = slapi_ch_calloc(1, bv->bv_len + 1);
  856. memcpy(ra->creds->bv_val, bv->bv_val, bv->bv_len);
  857. ra->creds->bv_len = bv->bv_len;
  858. }
  859. }
  860. /* If no credentials set, set to zero-length string */
  861. ra->creds->bv_val = NULL == ra->creds->bv_val ? slapi_ch_strdup("") : ra->creds->bv_val;
  862. PR_Unlock(ra->lock);
  863. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  864. return return_value;
  865. }
  866. /*
  867. * Set or reset the DN used to bind to the remote replica.
  868. *
  869. * Returns 0 if DN set, or -1 if an error occurred.
  870. */
  871. int
  872. agmt_set_binddn_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  873. {
  874. Slapi_Attr *sattr = NULL;
  875. int return_value = 0;
  876. PR_ASSERT(NULL != ra);
  877. slapi_entry_attr_find(e, type_nsds5ReplicaBindDN, &sattr);
  878. PR_Lock(ra->lock);
  879. slapi_ch_free((void **)&ra->binddn);
  880. ra->binddn = NULL;
  881. if (NULL != sattr)
  882. {
  883. Slapi_Value *sval = NULL;
  884. slapi_attr_first_value(sattr, &sval);
  885. if (NULL != sval)
  886. {
  887. const char *val = slapi_value_get_string(sval);
  888. ra->binddn = slapi_ch_strdup(val);
  889. }
  890. }
  891. /* If no BindDN set, set to zero-length string */
  892. if (ra->binddn == NULL) {
  893. ra->binddn = slapi_ch_strdup("");
  894. }
  895. PR_Unlock(ra->lock);
  896. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  897. return return_value;
  898. }
  899. static int
  900. agmt_parse_excluded_attrs_filter(const char *attr_string, size_t *offset)
  901. {
  902. char *filterstring = "(objectclass=*) ";
  903. size_t filterstringlen = strlen(filterstring);
  904. int retval = 0;
  905. if (strncmp(attr_string + *offset,filterstring,filterstringlen) == 0)
  906. {
  907. (*offset) += filterstringlen;
  908. } else
  909. {
  910. retval = -1;
  911. }
  912. return retval;
  913. }
  914. static int
  915. agmt_parse_excluded_attrs_exclude(const char *attr_string, size_t *offset)
  916. {
  917. char *excludestring = "$ EXCLUDE ";
  918. size_t excludestringlen = strlen(excludestring);
  919. int retval = 0;
  920. if (strncmp(attr_string + *offset,excludestring,excludestringlen) == 0)
  921. {
  922. (*offset) += excludestringlen;
  923. } else
  924. {
  925. retval = -1;
  926. }
  927. return retval;
  928. }
  929. static int
  930. agmt_parse_excluded_attrs_next(const char *attr_string, size_t *offset, char*** attrs)
  931. {
  932. int retval = 0;
  933. char *beginstr = ((char*) attr_string) + *offset;
  934. char *tmpstr = NULL;
  935. size_t stringlen = 0;
  936. char c = 0;
  937. /* Find the end of the current attribute name, if one is present */
  938. while (1)
  939. {
  940. c = *(beginstr + stringlen);
  941. if ('\0' == c || ' ' == c)
  942. {
  943. break;
  944. }
  945. stringlen++;
  946. }
  947. if (0 != stringlen)
  948. {
  949. tmpstr = slapi_ch_malloc(stringlen + 1);
  950. strncpy(tmpstr,beginstr,stringlen);
  951. tmpstr[stringlen] = '\0';
  952. if (charray_inlist(*attrs, tmpstr)) /* tmpstr is already in attrs */
  953. {
  954. slapi_ch_free_string(&tmpstr);
  955. }
  956. else
  957. {
  958. charray_add(attrs,tmpstr);
  959. }
  960. (*offset) += stringlen;
  961. /* Skip a delimiting space */
  962. if (c == ' ')
  963. {
  964. (*offset)++;
  965. }
  966. } else
  967. {
  968. retval = -1;
  969. }
  970. return retval;
  971. }
  972. /* It looks like this:
  973. * nsDS5ReplicatedAttributeList: (objectclass=*) $ EXCLUDE jpegPhoto telephoneNumber
  974. * This function could be called multiple times: to set excluded attrs in the
  975. * plugin default config and to set the ones in the replica agreement.
  976. * The excluded attrs from replica agreement are added to the ones from
  977. * default config. (Therefore, *attrs should not be initialized in this
  978. * function.)
  979. */
  980. static int
  981. agmt_parse_excluded_attrs_config_attr(const char *attr_string, char ***attrs)
  982. {
  983. int retval = 0;
  984. size_t offset = 0;
  985. char **new_attrs = NULL;
  986. /* First parse and skip the filter */
  987. retval = agmt_parse_excluded_attrs_filter(attr_string, &offset);
  988. if (retval)
  989. {
  990. goto error;
  991. }
  992. /* Now look for the 'EXCLUDE' keyword */
  993. retval = agmt_parse_excluded_attrs_exclude(attr_string, &offset);
  994. if (retval)
  995. {
  996. goto error;
  997. }
  998. /* Finally walk the list of attrs, storing in our chararray */
  999. while (!retval)
  1000. {
  1001. retval = agmt_parse_excluded_attrs_next(attr_string, &offset, &new_attrs);
  1002. }
  1003. /* If we got to here, we can't have an error */
  1004. retval = 0;
  1005. if (new_attrs)
  1006. {
  1007. charray_merge_nodup(attrs, new_attrs, 1);
  1008. slapi_ch_array_free(new_attrs);
  1009. }
  1010. error:
  1011. return retval;
  1012. }
  1013. /*
  1014. * _agmt_set_default_fractional_attrs
  1015. * helper function to set nsds5ReplicatedAttributeList value (from cn=plugin
  1016. * default config,cn=config) to frac_attrs in Repl_Agmt.
  1017. * nsds5ReplicatedAttributeList set in each agreement is added to the
  1018. * default list set in this function.
  1019. */
  1020. static int
  1021. _agmt_set_default_fractional_attrs(Repl_Agmt *ra)
  1022. {
  1023. Slapi_PBlock *newpb = NULL;
  1024. Slapi_Entry **entries = NULL;
  1025. int rc = LDAP_SUCCESS;
  1026. char *attrs[2];
  1027. attrs[0] = (char *)type_nsds5ReplicatedAttributeList;
  1028. attrs[1] = NULL;
  1029. newpb = slapi_pblock_new();
  1030. slapi_search_internal_set_pb(newpb,
  1031. SLAPI_PLUGIN_DEFAULT_CONFIG, /* Base DN */
  1032. LDAP_SCOPE_BASE,
  1033. "(objectclass=*)",
  1034. attrs, /* Attrs */
  1035. 0, /* AttrOnly */
  1036. NULL, /* Controls */
  1037. NULL, /* UniqueID */
  1038. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  1039. 0);
  1040. slapi_search_internal_pb(newpb);
  1041. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1042. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1043. ra->frac_attrs = NULL;
  1044. if (LDAP_SUCCESS == rc && entries && *entries) /* default config entry exists */
  1045. {
  1046. Slapi_Attr *attr;
  1047. Slapi_Value *sval = NULL;
  1048. if (0 == slapi_entry_attr_find(*entries,
  1049. type_nsds5ReplicatedAttributeList, &attr))
  1050. {
  1051. int i;
  1052. const char *val = NULL;
  1053. for (i = slapi_attr_first_value(attr, &sval);
  1054. i >= 0; i = slapi_attr_next_value(attr, i, &sval)) {
  1055. val = slapi_value_get_string(sval);
  1056. rc = agmt_parse_excluded_attrs_config_attr(val,
  1057. &(ra->frac_attrs));
  1058. if (0 != rc) {
  1059. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1060. "_agmt_set_default_fractional_attrs: failed to "
  1061. "parse default config (%s) attribute %s value: %s\n",
  1062. SLAPI_PLUGIN_DEFAULT_CONFIG,
  1063. type_nsds5ReplicatedAttributeList, val);
  1064. }
  1065. }
  1066. }
  1067. }
  1068. slapi_free_search_results_internal(newpb);
  1069. slapi_pblock_destroy(newpb);
  1070. return rc;
  1071. }
  1072. /*
  1073. * Set or reset the set of replicated attributes.
  1074. *
  1075. * Returns 0 if DN set, or -1 if an error occurred.
  1076. */
  1077. int
  1078. agmt_set_replicated_attributes_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1079. {
  1080. Slapi_Attr *sattr = NULL;
  1081. int return_value = 0;
  1082. PR_ASSERT(NULL != ra);
  1083. slapi_entry_attr_find(e, type_nsds5ReplicatedAttributeList, &sattr);
  1084. PR_Lock(ra->lock);
  1085. if (ra->frac_attrs)
  1086. {
  1087. slapi_ch_array_free(ra->frac_attrs);
  1088. ra->frac_attrs = NULL;
  1089. }
  1090. _agmt_set_default_fractional_attrs(ra);
  1091. if (NULL != sattr)
  1092. {
  1093. Slapi_Value *sval = NULL;
  1094. slapi_attr_first_value(sattr, &sval);
  1095. if (NULL != sval)
  1096. {
  1097. const char *val = slapi_value_get_string(sval);
  1098. return_value = agmt_parse_excluded_attrs_config_attr(val,&(ra->frac_attrs));
  1099. }
  1100. }
  1101. PR_Unlock(ra->lock);
  1102. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1103. return return_value;
  1104. }
  1105. /*
  1106. * Set or reset the set of replicated attributes.
  1107. *
  1108. * Returns 0 if DN set, or -1 if an error occurred.
  1109. */
  1110. int
  1111. agmt_set_replicated_attributes_from_attr(Repl_Agmt *ra, Slapi_Attr *sattr)
  1112. {
  1113. int return_value = 0;
  1114. PR_ASSERT(NULL != ra);
  1115. PR_Lock(ra->lock);
  1116. if (ra->frac_attrs)
  1117. {
  1118. slapi_ch_array_free(ra->frac_attrs);
  1119. ra->frac_attrs = NULL;
  1120. }
  1121. _agmt_set_default_fractional_attrs(ra);
  1122. if (NULL != sattr)
  1123. {
  1124. Slapi_Value *sval = NULL;
  1125. slapi_attr_first_value(sattr, &sval);
  1126. if (NULL != sval)
  1127. {
  1128. const char *val = slapi_value_get_string(sval);
  1129. return_value = agmt_parse_excluded_attrs_config_attr(val,&(ra->frac_attrs));
  1130. }
  1131. }
  1132. PR_Unlock(ra->lock);
  1133. return return_value;
  1134. }
  1135. char **
  1136. agmt_validate_replicated_attributes(Repl_Agmt *ra)
  1137. {
  1138. static char* verbotten_attrs[] = {
  1139. "nsuniqueid",
  1140. "modifiersname",
  1141. "lastmodifiedtime",
  1142. "dc", "o", "ou", "cn", "objectclass",
  1143. NULL
  1144. };
  1145. char **retval = NULL;
  1146. char **frac_attrs = ra->frac_attrs;
  1147. /* Iterate over the frac attrs */
  1148. if (frac_attrs)
  1149. {
  1150. char *this_attr = NULL;
  1151. int i = 0;
  1152. for (i = 0; (this_attr = frac_attrs[i]); i++)
  1153. {
  1154. if (charray_inlist(verbotten_attrs,this_attr)) {
  1155. int k = 0;
  1156. charray_add(&retval,this_attr);
  1157. /* Remove this attr from the list */
  1158. for (k = i; frac_attrs[k] ; k++)
  1159. {
  1160. frac_attrs[k] = frac_attrs[k+1];
  1161. }
  1162. i--;
  1163. }
  1164. }
  1165. }
  1166. return retval;
  1167. }
  1168. /*
  1169. * Set or reset the bind method used to bind to the remote replica.
  1170. *
  1171. * Returns 0 if bind method set, or -1 if an error occurred.
  1172. */
  1173. static int
  1174. agmt_set_bind_method_no_lock(Repl_Agmt *ra, const Slapi_Entry *e)
  1175. {
  1176. char *tmpstr = NULL;
  1177. int return_value = 0;
  1178. PR_ASSERT(NULL != ra);
  1179. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds5ReplicaBindMethod);
  1180. if (NULL == tmpstr || strcasecmp(tmpstr, "SIMPLE") == 0)
  1181. {
  1182. ra->bindmethod = BINDMETHOD_SIMPLE_AUTH;
  1183. }
  1184. else if (strcasecmp(tmpstr, "SSLCLIENTAUTH") == 0)
  1185. {
  1186. ra->bindmethod = BINDMETHOD_SSL_CLIENTAUTH;
  1187. }
  1188. else if (strcasecmp(tmpstr, "SASL/GSSAPI") == 0)
  1189. {
  1190. ra->bindmethod = BINDMETHOD_SASL_GSSAPI;
  1191. }
  1192. else if (strcasecmp(tmpstr, "SASL/DIGEST-MD5") == 0)
  1193. {
  1194. ra->bindmethod = BINDMETHOD_SASL_DIGEST_MD5;
  1195. }
  1196. else
  1197. {
  1198. ra->bindmethod = BINDMETHOD_SIMPLE_AUTH;
  1199. }
  1200. slapi_ch_free((void **)&tmpstr);
  1201. return return_value;
  1202. }
  1203. int
  1204. agmt_set_bind_method_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1205. {
  1206. int return_value = 0;
  1207. PR_ASSERT(NULL != ra);
  1208. PR_Lock(ra->lock);
  1209. if (ra->stop_in_progress)
  1210. {
  1211. PR_Unlock(ra->lock);
  1212. return return_value;
  1213. }
  1214. return_value = agmt_set_bind_method_no_lock(ra, e);
  1215. PR_Unlock(ra->lock);
  1216. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1217. return return_value;
  1218. }
  1219. /*
  1220. * Set or reset the transport used to bind to the remote replica.
  1221. *
  1222. * Returns 0 if transport set, or -1 if an error occurred.
  1223. */
  1224. static int
  1225. agmt_set_transportinfo_no_lock(Repl_Agmt *ra, const Slapi_Entry *e)
  1226. {
  1227. char *tmpstr;
  1228. int rc = 0;
  1229. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds5TransportInfo);
  1230. if (!tmpstr || !strcasecmp(tmpstr, "LDAP")) {
  1231. ra->transport_flags = 0;
  1232. } else if (strcasecmp(tmpstr, "SSL") == 0) {
  1233. ra->transport_flags = TRANSPORT_FLAG_SSL;
  1234. } else if (strcasecmp(tmpstr, "TLS") == 0) {
  1235. ra->transport_flags = TRANSPORT_FLAG_TLS;
  1236. }
  1237. /* else do nothing - invalid value is a no-op */
  1238. slapi_ch_free_string(&tmpstr);
  1239. return (rc);
  1240. }
  1241. int
  1242. agmt_set_transportinfo_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1243. {
  1244. int return_value = 0;
  1245. PR_ASSERT(NULL != ra);
  1246. PR_Lock(ra->lock);
  1247. if (ra->stop_in_progress)
  1248. {
  1249. PR_Unlock(ra->lock);
  1250. return return_value;
  1251. }
  1252. return_value = agmt_set_transportinfo_no_lock(ra, e);
  1253. PR_Unlock(ra->lock);
  1254. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1255. return return_value;
  1256. }
  1257. /*
  1258. * Set or reset the replication schedule. Notify the protocol handler
  1259. * that a change has been made.
  1260. *
  1261. * Returns 0 if schedule was set or -1 if an error occurred.
  1262. */
  1263. int
  1264. agmt_set_schedule_from_entry( Repl_Agmt *ra, const Slapi_Entry *e )
  1265. {
  1266. Slapi_Attr *sattr;
  1267. int return_value = 0;
  1268. PR_ASSERT(NULL != ra);
  1269. PR_Lock(ra->lock);
  1270. if (ra->stop_in_progress)
  1271. {
  1272. PR_Unlock(ra->lock);
  1273. return return_value;
  1274. }
  1275. PR_Unlock(ra->lock);
  1276. if (slapi_entry_attr_find(e, type_nsds5ReplicaUpdateSchedule, &sattr) != 0)
  1277. {
  1278. sattr = NULL; /* no schedule ==> delete any existing one */
  1279. }
  1280. /* make it so */
  1281. return_value = schedule_set(ra->schedule, sattr);
  1282. if ( 0 == return_value ) {
  1283. /* schedule set OK -- spread the news */
  1284. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1285. }
  1286. return return_value;
  1287. }
  1288. /*
  1289. * Set or reset the timeout used to bind to the remote replica.
  1290. *
  1291. * Returns 0 if timeout set, or -1 if an error occurred.
  1292. */
  1293. int
  1294. agmt_set_timeout_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1295. {
  1296. Slapi_Attr *sattr = NULL;
  1297. int return_value = -1;
  1298. PR_ASSERT(NULL != ra);
  1299. PR_Lock(ra->lock);
  1300. if (ra->stop_in_progress)
  1301. {
  1302. PR_Unlock(ra->lock);
  1303. return return_value;
  1304. }
  1305. slapi_entry_attr_find(e, type_nsds5ReplicaTimeout, &sattr);
  1306. if (NULL != sattr)
  1307. {
  1308. Slapi_Value *sval = NULL;
  1309. slapi_attr_first_value(sattr, &sval);
  1310. if (NULL != sval)
  1311. {
  1312. long tmpval = slapi_value_get_long(sval);
  1313. if (tmpval >= 0) {
  1314. ra->timeout = tmpval;
  1315. return_value = 0; /* success! */
  1316. }
  1317. }
  1318. }
  1319. PR_Unlock(ra->lock);
  1320. if (return_value == 0)
  1321. {
  1322. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1323. }
  1324. return return_value;
  1325. }
  1326. /*
  1327. * Set or reset the busywaittime
  1328. *
  1329. * Returns 0 if busywaittime set, or -1 if an error occurred.
  1330. */
  1331. int
  1332. agmt_set_busywaittime_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1333. {
  1334. Slapi_Attr *sattr = NULL;
  1335. int return_value = -1;
  1336. PR_ASSERT(NULL != ra);
  1337. PR_Lock(ra->lock);
  1338. if (ra->stop_in_progress)
  1339. {
  1340. PR_Unlock(ra->lock);
  1341. return return_value;
  1342. }
  1343. slapi_entry_attr_find(e, type_nsds5ReplicaBusyWaitTime, &sattr);
  1344. if (NULL != sattr)
  1345. {
  1346. Slapi_Value *sval = NULL;
  1347. slapi_attr_first_value(sattr, &sval);
  1348. if (NULL != sval)
  1349. {
  1350. long tmpval = slapi_value_get_long(sval);
  1351. if (tmpval >= 0) {
  1352. ra->busywaittime = tmpval;
  1353. return_value = 0; /* success! */
  1354. }
  1355. }
  1356. }
  1357. PR_Unlock(ra->lock);
  1358. if (return_value == 0)
  1359. {
  1360. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1361. }
  1362. return return_value;
  1363. }
  1364. /*
  1365. * Set or reset the pausetime
  1366. *
  1367. * Returns 0 if pausetime set, or -1 if an error occurred.
  1368. */
  1369. int
  1370. agmt_set_pausetime_from_entry(Repl_Agmt *ra, const Slapi_Entry *e)
  1371. {
  1372. Slapi_Attr *sattr = NULL;
  1373. int return_value = -1;
  1374. PR_ASSERT(NULL != ra);
  1375. PR_Lock(ra->lock);
  1376. if (ra->stop_in_progress)
  1377. {
  1378. PR_Unlock(ra->lock);
  1379. return return_value;
  1380. }
  1381. slapi_entry_attr_find(e, type_nsds5ReplicaSessionPauseTime, &sattr);
  1382. if (NULL != sattr)
  1383. {
  1384. Slapi_Value *sval = NULL;
  1385. slapi_attr_first_value(sattr, &sval);
  1386. if (NULL != sval)
  1387. {
  1388. long tmpval = slapi_value_get_long(sval);
  1389. if (tmpval >= 0) {
  1390. ra->pausetime = tmpval;
  1391. return_value = 0; /* success! */
  1392. }
  1393. }
  1394. }
  1395. PR_Unlock(ra->lock);
  1396. if (return_value == 0)
  1397. {
  1398. prot_notify_agmt_changed(ra->protocol, ra->long_name);
  1399. }
  1400. return return_value;
  1401. }
  1402. /* XXXggood - also make this pass an arg that tells if there was
  1403. * an update to a priority attribute */
  1404. void
  1405. agmt_notify_change(Repl_Agmt *agmt, Slapi_PBlock *pb)
  1406. {
  1407. if (NULL != pb)
  1408. {
  1409. /* Is the entry within our replicated area? */
  1410. char *target_dn;
  1411. Slapi_DN *target_sdn;
  1412. int change_is_relevant = 0;
  1413. PR_ASSERT(NULL != agmt);
  1414. PR_Lock(agmt->lock);
  1415. if (agmt->stop_in_progress)
  1416. {
  1417. PR_Unlock(agmt->lock);
  1418. return;
  1419. }
  1420. slapi_pblock_get(pb, SLAPI_TARGET_DN, &target_dn);
  1421. target_sdn = slapi_sdn_new_dn_byref(target_dn); /* XXX see if you can avoid allocating this */
  1422. if (slapi_sdn_issuffix(target_sdn, agmt->replarea))
  1423. {
  1424. /*
  1425. * Yep, it's in our replicated area. Is this a fractional
  1426. * replication agreement?
  1427. */
  1428. if (NULL != agmt->frac_attrs)
  1429. {
  1430. /*
  1431. * Yep, it's fractional. See if the change should be
  1432. * tossed because it doesn't affect any of the replicated
  1433. * attributes.
  1434. */
  1435. int optype;
  1436. int affects_non_fractional_attribute = 0;
  1437. slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &optype);
  1438. if (SLAPI_OPERATION_MODIFY == optype)
  1439. {
  1440. LDAPMod **mods;
  1441. int i, j;
  1442. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1443. for (i = 0; !affects_non_fractional_attribute && NULL != agmt->frac_attrs[i]; i++)
  1444. {
  1445. for (j = 0; !affects_non_fractional_attribute && NULL != mods[j]; j++)
  1446. {
  1447. if (!slapi_attr_types_equivalent(agmt->frac_attrs[i],
  1448. mods[j]->mod_type))
  1449. {
  1450. affects_non_fractional_attribute = 1;
  1451. }
  1452. }
  1453. }
  1454. }
  1455. else
  1456. {
  1457. /*
  1458. * Add, delete, and modrdn always cause some sort of
  1459. * operation replay, even if agreement is fractional.
  1460. */
  1461. affects_non_fractional_attribute = 1;
  1462. }
  1463. if (affects_non_fractional_attribute)
  1464. {
  1465. change_is_relevant = 1;
  1466. }
  1467. }
  1468. else
  1469. {
  1470. /* Not a fractional agreement */
  1471. change_is_relevant = 1;
  1472. }
  1473. }
  1474. PR_Unlock(agmt->lock);
  1475. slapi_sdn_free(&target_sdn);
  1476. if (change_is_relevant)
  1477. {
  1478. /* Notify the protocol that a change has occurred */
  1479. prot_notify_update(agmt->protocol);
  1480. }
  1481. }
  1482. }
  1483. int
  1484. agmt_is_50_mm_protocol(const Repl_Agmt *agmt)
  1485. {
  1486. return 1; /* XXXggood could support > 1 protocol */
  1487. }
  1488. int
  1489. agmt_initialize_replica(const Repl_Agmt *agmt)
  1490. {
  1491. PR_ASSERT(NULL != agmt);
  1492. PR_Lock(agmt->lock);
  1493. if (agmt->stop_in_progress)
  1494. {
  1495. PR_Unlock(agmt->lock);
  1496. return 0;
  1497. }
  1498. PR_Unlock(agmt->lock);
  1499. /* Call prot_initialize_replica only if the suffix is enabled (agmt->protocol != NULL) */
  1500. if (NULL != agmt->protocol) {
  1501. prot_initialize_replica(agmt->protocol);
  1502. }
  1503. else {
  1504. /* agmt->protocol == NULL --> Suffix is disabled */
  1505. return -1;
  1506. }
  1507. return 0;
  1508. }
  1509. /* delete nsds5BeginReplicaRefresh attribute to indicate to the clients
  1510. that replica initialization have completed */
  1511. void
  1512. agmt_replica_init_done (const Repl_Agmt *agmt)
  1513. {
  1514. int rc;
  1515. Slapi_PBlock *pb = slapi_pblock_new ();
  1516. LDAPMod *mods [2];
  1517. LDAPMod mod;
  1518. mods[0] = &mod;
  1519. mods[1] = NULL;
  1520. mod.mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
  1521. mod.mod_type = (char*)type_nsds5ReplicaInitialize;
  1522. mod.mod_bvalues = NULL;
  1523. slapi_modify_internal_set_pb(pb, slapi_sdn_get_dn (agmt->dn), mods, NULL/* controls */,
  1524. NULL/* uniqueid */, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0/* flags */);
  1525. slapi_modify_internal_pb (pb);
  1526. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1527. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE)
  1528. {
  1529. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmt_replica_init_done: "
  1530. "failed to remove (%s) attribute from (%s) entry; LDAP error - %d\n",
  1531. type_nsds5ReplicaInitialize, slapi_sdn_get_ndn (agmt->dn), rc);
  1532. }
  1533. slapi_pblock_destroy (pb);
  1534. }
  1535. /* Agreement object is acquired on behalf of the caller.
  1536. The caller is responsible for releasing the object
  1537. when it is no longer used */
  1538. Object*
  1539. agmt_get_consumer_ruv (Repl_Agmt *ra)
  1540. {
  1541. Object *rt = NULL;
  1542. PR_ASSERT(NULL != ra);
  1543. PR_Lock(ra->lock);
  1544. if (ra->consumerRUV)
  1545. {
  1546. object_acquire (ra->consumerRUV);
  1547. rt = ra->consumerRUV;
  1548. }
  1549. PR_Unlock(ra->lock);
  1550. return rt;
  1551. }
  1552. int
  1553. agmt_set_consumer_ruv (Repl_Agmt *ra, RUV *ruv)
  1554. {
  1555. if (ra == NULL || ruv == NULL)
  1556. {
  1557. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmt_set_consumer_ruv: invalid argument"
  1558. " agmt - %p, ruv - %p\n", ra, ruv);
  1559. return -1;
  1560. }
  1561. PR_Lock(ra->lock);
  1562. if (ra->consumerRUV)
  1563. {
  1564. object_release (ra->consumerRUV);
  1565. }
  1566. ra->consumerRUV = object_new (ruv_dup (ruv), (FNFree)ruv_destroy);
  1567. PR_Unlock(ra->lock);
  1568. return 0;
  1569. }
  1570. void
  1571. agmt_update_consumer_ruv (Repl_Agmt *ra)
  1572. {
  1573. int rc;
  1574. RUV *ruv;
  1575. Slapi_Mod smod;
  1576. Slapi_Mod smod_last_modified;
  1577. Slapi_PBlock *pb;
  1578. LDAPMod *mods[3];
  1579. PR_ASSERT (ra);
  1580. PR_Lock(ra->lock);
  1581. if (ra->consumerRUV)
  1582. {
  1583. ruv = (RUV*) object_get_data (ra->consumerRUV);
  1584. PR_ASSERT (ruv);
  1585. ruv_to_smod(ruv, &smod);
  1586. ruv_last_modified_to_smod(ruv, &smod_last_modified);
  1587. /* it is ok to release the lock here because we are done with the agreement data.
  1588. we have to do it before issuing the modify operation because it causes
  1589. agmtlist_notify_all to be called which uses the same lock - hence the deadlock */
  1590. PR_Unlock(ra->lock);
  1591. pb = slapi_pblock_new ();
  1592. mods[0] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod);
  1593. mods[1] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod_last_modified);
  1594. mods[2] = NULL;
  1595. slapi_modify_internal_set_pb (pb, (char*)slapi_sdn_get_dn(ra->dn), mods, NULL, NULL,
  1596. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  1597. slapi_modify_internal_pb (pb);
  1598. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1599. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE)
  1600. {
  1601. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: agmt_update_consumer_ruv: "
  1602. "failed to update consumer's RUV; LDAP error - %d\n",
  1603. ra->long_name, rc);
  1604. }
  1605. slapi_mod_done (&smod);
  1606. slapi_mod_done (&smod_last_modified);
  1607. slapi_pblock_destroy (pb);
  1608. }
  1609. else
  1610. PR_Unlock(ra->lock);
  1611. }
  1612. CSN*
  1613. agmt_get_consumer_schema_csn (Repl_Agmt *ra)
  1614. {
  1615. CSN *rt;
  1616. PR_ASSERT(NULL != ra);
  1617. PR_Lock(ra->lock);
  1618. rt = ra->consumerSchemaCSN;
  1619. PR_Unlock(ra->lock);
  1620. return rt;
  1621. }
  1622. void
  1623. agmt_set_consumer_schema_csn (Repl_Agmt *ra, CSN *csn)
  1624. {
  1625. PR_ASSERT(NULL != ra);
  1626. PR_Lock(ra->lock);
  1627. csn_free(&ra->consumerSchemaCSN);
  1628. ra->consumerSchemaCSN = csn;
  1629. PR_Unlock(ra->lock);
  1630. }
  1631. void
  1632. agmt_set_last_update_start (Repl_Agmt *ra, time_t start_time)
  1633. {
  1634. PR_ASSERT(NULL != ra);
  1635. if (NULL != ra)
  1636. {
  1637. ra->last_update_start_time = start_time;
  1638. ra->last_update_end_time = 0UL;
  1639. }
  1640. }
  1641. void
  1642. agmt_set_last_update_end (Repl_Agmt *ra, time_t end_time)
  1643. {
  1644. PR_ASSERT(NULL != ra);
  1645. if (NULL != ra)
  1646. {
  1647. ra->last_update_end_time = end_time;
  1648. }
  1649. }
  1650. void
  1651. agmt_set_last_init_start (Repl_Agmt *ra, time_t start_time)
  1652. {
  1653. PR_ASSERT(NULL != ra);
  1654. if (NULL != ra)
  1655. {
  1656. ra->last_init_start_time = start_time;
  1657. ra->last_init_end_time = 0UL;
  1658. }
  1659. }
  1660. void
  1661. agmt_set_last_init_end (Repl_Agmt *ra, time_t end_time)
  1662. {
  1663. PR_ASSERT(NULL != ra);
  1664. if (NULL != ra)
  1665. {
  1666. ra->last_init_end_time = end_time;
  1667. }
  1668. }
  1669. void
  1670. agmt_set_last_update_status (Repl_Agmt *ra, int ldaprc, int replrc, const char *message)
  1671. {
  1672. PR_ASSERT(NULL != ra);
  1673. if (NULL != ra)
  1674. {
  1675. if (replrc == NSDS50_REPL_UPTODATE)
  1676. {
  1677. /* no session started, no status update */
  1678. }
  1679. else if (ldaprc != LDAP_SUCCESS)
  1680. {
  1681. char *replmsg = NULL;
  1682. if ( replrc ) {
  1683. replmsg = protocol_response2string(replrc);
  1684. /* Do not mix the unknown replication error with the known ldap one */
  1685. if ( strcasecmp(replmsg, "unknown error") == 0 ) {
  1686. replmsg = NULL;
  1687. }
  1688. }
  1689. if (ldaprc > 0) {
  1690. PR_snprintf(ra->last_update_status, STATUS_LEN,
  1691. "%d %s%sLDAP error: %s%s%s",
  1692. ldaprc,
  1693. message?message:"",message?"":" - ",
  1694. ldap_err2string(ldaprc),
  1695. replmsg ? " - " : "", replmsg ? replmsg : "");
  1696. } else { /* ldaprc is < 0 */
  1697. PR_snprintf(ra->last_update_status, STATUS_LEN,
  1698. "%d %s%sSystem error%s%s",
  1699. ldaprc,message?message:"",message?"":" - ",
  1700. replmsg ? " - " : "", replmsg ? replmsg : "");
  1701. }
  1702. }
  1703. /* ldaprc == LDAP_SUCCESS */
  1704. else if (replrc != 0)
  1705. {
  1706. if (replrc == NSDS50_REPL_REPLICA_BUSY)
  1707. {
  1708. PR_snprintf(ra->last_update_status, STATUS_LEN,
  1709. "%d Can't acquire busy replica", replrc );
  1710. }
  1711. else if (replrc == NSDS50_REPL_REPLICA_RELEASE_SUCCEEDED)
  1712. {
  1713. PR_snprintf(ra->last_update_status, STATUS_LEN, "%d %s",
  1714. ldaprc, "Replication session successful");
  1715. }
  1716. else if (replrc == NSDS50_REPL_DISABLED)
  1717. {
  1718. PR_snprintf(ra->last_update_status, STATUS_LEN, "%d Incremental update aborted: "
  1719. "Replication agreement for %s\n can not be updated while the replica is disabled.\n"
  1720. "(If the suffix is disabled you must enable it then restart the server for replication to take place).",
  1721. replrc, ra->long_name ? ra->long_name : "a replica");
  1722. /* Log into the errors log, as "ra->long_name" is not accessible from the caller */
  1723. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1724. "Incremental update aborted: Replication agreement for \"%s\" "
  1725. "can not be updated while the replica is disabled\n", ra->long_name ? ra->long_name : "a replica");
  1726. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1727. "(If the suffix is disabled you must enable it then restart the server for replication to take place).\n");
  1728. }
  1729. else
  1730. {
  1731. PR_snprintf(ra->last_update_status, STATUS_LEN,
  1732. "%d Replication error acquiring replica: %s%s%s",
  1733. replrc, protocol_response2string(replrc),
  1734. message?" - ":"",message?message:"");
  1735. }
  1736. }
  1737. else if (message != NULL) /* replrc == NSDS50_REPL_REPLICA_READY == 0 */
  1738. {
  1739. PR_snprintf(ra->last_update_status, STATUS_LEN,
  1740. "%d Replica acquired successfully: %s",
  1741. ldaprc, message);
  1742. }
  1743. else
  1744. { /* agmt_set_last_update_status(0,0,NULL) to reset agmt */
  1745. PR_snprintf(ra->last_update_status, STATUS_LEN, "%d", ldaprc);
  1746. }
  1747. }
  1748. }
  1749. void
  1750. agmt_set_last_init_status (Repl_Agmt *ra, int ldaprc, int replrc, const char *message)
  1751. {
  1752. PR_ASSERT(NULL != ra);
  1753. if (NULL != ra)
  1754. {
  1755. if (ldaprc != LDAP_SUCCESS)
  1756. {
  1757. char *replmsg = NULL;
  1758. if ( replrc ) {
  1759. replmsg = protocol_response2string(replrc);
  1760. /* Do not mix the unknown replication error with the known ldap one */
  1761. if ( strcasecmp(replmsg, "unknown error") == 0 ) {
  1762. replmsg = NULL;
  1763. }
  1764. }
  1765. if (ldaprc > 0) {
  1766. PR_snprintf(ra->last_init_status, STATUS_LEN,
  1767. "%d %s%sLDAP error: %s%s%s",
  1768. ldaprc,
  1769. message?message:"",message?"":" - ",
  1770. ldap_err2string(ldaprc),
  1771. replmsg ? " - " : "", replmsg ? replmsg : "");
  1772. } else { /* ldaprc is < 0 */
  1773. PR_snprintf(ra->last_init_status, STATUS_LEN,
  1774. "%d %s%sSystem error%s%s",
  1775. ldaprc,message?message:"",message?"":" - ",
  1776. replmsg ? " - " : "", replmsg ? replmsg : "");
  1777. }
  1778. }
  1779. /* ldaprc == LDAP_SUCCESS */
  1780. else if (replrc != 0)
  1781. {
  1782. if (replrc == NSDS50_REPL_REPLICA_RELEASE_SUCCEEDED)
  1783. {
  1784. PR_snprintf(ra->last_init_status, STATUS_LEN, "%d %s",
  1785. ldaprc, "Replication session successful");
  1786. }
  1787. else if (replrc == NSDS50_REPL_DISABLED)
  1788. {
  1789. PR_snprintf(ra->last_init_status, STATUS_LEN, "%d Total update aborted: "
  1790. "Replication agreement for %s\n can not be updated while the replica is disabled.\n"
  1791. "(If the suffix is disabled you must enable it then restart the server for replication to take place).",
  1792. replrc, ra->long_name ? ra->long_name : "a replica");
  1793. /* Log into the errors log, as "ra->long_name" is not accessible from the caller */
  1794. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1795. "Total update aborted: Replication agreement for \"%s\" "
  1796. "can not be updated while the replica is disabled\n", ra->long_name ? ra->long_name : "a replica");
  1797. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1798. "(If the suffix is disabled you must enable it then restart the server for replication to take place).\n");
  1799. }
  1800. else
  1801. {
  1802. PR_snprintf(ra->last_init_status, STATUS_LEN,
  1803. "%d Replication error acquiring replica: %s%s%s",
  1804. replrc, protocol_response2string(replrc),
  1805. message?" - ":"",message?message:"");
  1806. }
  1807. }
  1808. else if (message != NULL) /* replrc == NSDS50_REPL_REPLICA_READY == 0 */
  1809. {
  1810. PR_snprintf(ra->last_init_status, STATUS_LEN,
  1811. "%d %s",
  1812. ldaprc, message);
  1813. }
  1814. else
  1815. { /* agmt_set_last_init_status(0,0,NULL) to reset agmt */
  1816. PR_snprintf(ra->last_init_status, STATUS_LEN, "%d", ldaprc);
  1817. }
  1818. }
  1819. }
  1820. void
  1821. agmt_set_update_in_progress (Repl_Agmt *ra, PRBool in_progress)
  1822. {
  1823. PR_ASSERT(NULL != ra);
  1824. if (NULL != ra)
  1825. {
  1826. ra->update_in_progress = in_progress;
  1827. }
  1828. }
  1829. void
  1830. agmt_inc_last_update_changecount (Repl_Agmt *ra, ReplicaId rid, int skipped)
  1831. {
  1832. PR_ASSERT(NULL != ra);
  1833. if (NULL != ra)
  1834. {
  1835. int i;
  1836. for ( i = 0; i < ra->num_changecounters; i++ )
  1837. {
  1838. if ( ra->changecounters[i]->rid == rid )
  1839. break;
  1840. }
  1841. if ( i < ra->num_changecounters )
  1842. {
  1843. if ( skipped )
  1844. ra->changecounters[i]->num_skipped ++;
  1845. else
  1846. ra->changecounters[i]->num_replayed ++;
  1847. }
  1848. else
  1849. {
  1850. ra->num_changecounters ++;
  1851. ra->changecounters[i] = (struct changecounter*) slapi_ch_calloc(1, sizeof(struct changecounter));
  1852. ra->changecounters[i]->rid = rid;
  1853. if ( skipped )
  1854. ra->changecounters[i]->num_skipped = 1;
  1855. else
  1856. ra->changecounters[i]->num_replayed = 1;
  1857. }
  1858. }
  1859. }
  1860. void
  1861. agmt_get_changecount_string (Repl_Agmt *ra, char *buf, int bufsize)
  1862. {
  1863. char tmp_buf[32]; /* 5 digit RID, 10 digit each replayed and skipped */
  1864. int i;
  1865. int buflen = 0;
  1866. *buf = '\0';
  1867. if (NULL != ra)
  1868. {
  1869. for ( i = 0; i < ra->num_changecounters; i++ )
  1870. {
  1871. PR_snprintf (tmp_buf, sizeof(tmp_buf), "%u:%u/%u ",
  1872. ra->changecounters[i]->rid,
  1873. ra->changecounters[i]->num_replayed,
  1874. ra->changecounters[i]->num_skipped);
  1875. PR_snprintf (buf+buflen, bufsize-buflen, "%s", tmp_buf);
  1876. buflen += strlen (tmp_buf);
  1877. }
  1878. }
  1879. }
  1880. static int
  1881. get_agmt_status(Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  1882. int *returncode, char *returntext, void *arg)
  1883. {
  1884. char *time_tmp = NULL;
  1885. char changecount_string[BUFSIZ];
  1886. Repl_Agmt *ra = (Repl_Agmt *)arg;
  1887. PR_ASSERT(NULL != ra);
  1888. if (NULL != ra)
  1889. {
  1890. PRBool reapActive = PR_FALSE;
  1891. Slapi_DN *replarea_sdn = NULL;
  1892. Object *repl_obj = NULL;
  1893. replarea_sdn = agmt_get_replarea(ra);
  1894. repl_obj = replica_get_replica_from_dn(replarea_sdn);
  1895. slapi_sdn_free(&replarea_sdn);
  1896. if (repl_obj) {
  1897. Replica *replica = (Replica*)object_get_data (repl_obj);
  1898. reapActive = replica_get_tombstone_reap_active(replica);
  1899. object_release(repl_obj);
  1900. }
  1901. slapi_entry_attr_set_int(e, "nsds5replicaReapActive", (int)reapActive);
  1902. /* these values persist in the dse.ldif file, so we delete them
  1903. here to avoid multi valued attributes */
  1904. slapi_entry_attr_delete(e, "nsds5replicaLastUpdateStart");
  1905. slapi_entry_attr_delete(e, "nsds5replicaLastUpdateEnd");
  1906. slapi_entry_attr_delete(e, "nsds5replicaChangesSentSinceStartup");
  1907. slapi_entry_attr_delete(e, "nsds5replicaLastUpdateStatus");
  1908. slapi_entry_attr_delete(e, "nsds5replicaUpdateInProgress");
  1909. slapi_entry_attr_delete(e, "nsds5replicaLastInitStart");
  1910. slapi_entry_attr_delete(e, "nsds5replicaLastInitStatus");
  1911. slapi_entry_attr_delete(e, "nsds5replicaLastInitEnd");
  1912. /* now, add the real values (singly) */
  1913. if (ra->last_update_start_time == 0)
  1914. {
  1915. slapi_entry_add_string(e, "nsds5replicaLastUpdateStart", "0");
  1916. }
  1917. else
  1918. {
  1919. time_tmp = format_genTime(ra->last_update_start_time);
  1920. slapi_entry_add_string(e, "nsds5replicaLastUpdateStart", time_tmp);
  1921. slapi_ch_free((void **)&time_tmp);
  1922. }
  1923. if (ra->last_update_end_time == 0)
  1924. {
  1925. slapi_entry_add_string(e, "nsds5replicaLastUpdateEnd", "0");
  1926. }
  1927. else
  1928. {
  1929. time_tmp = format_genTime(ra->last_update_end_time);
  1930. slapi_entry_add_string(e, "nsds5replicaLastUpdateEnd", time_tmp);
  1931. slapi_ch_free((void **)&time_tmp);
  1932. }
  1933. agmt_get_changecount_string (ra, changecount_string, sizeof (changecount_string) );
  1934. slapi_entry_add_string(e, "nsds5replicaChangesSentSinceStartup", changecount_string);
  1935. if (ra->last_update_status[0] == '\0')
  1936. {
  1937. slapi_entry_add_string(e, "nsds5replicaLastUpdateStatus", "0 No replication sessions started since server startup");
  1938. }
  1939. else
  1940. {
  1941. slapi_entry_add_string(e, "nsds5replicaLastUpdateStatus", ra->last_update_status);
  1942. }
  1943. slapi_entry_add_string(e, "nsds5replicaUpdateInProgress", ra->update_in_progress ? "TRUE" : "FALSE");
  1944. if (ra->last_init_start_time == 0)
  1945. {
  1946. slapi_entry_add_string(e, "nsds5replicaLastInitStart", "0");
  1947. }
  1948. else
  1949. {
  1950. time_tmp = format_genTime(ra->last_init_start_time);
  1951. slapi_entry_add_string(e, "nsds5replicaLastInitStart", time_tmp);
  1952. slapi_ch_free((void **)&time_tmp);
  1953. }
  1954. if (ra->last_init_end_time == 0)
  1955. {
  1956. slapi_entry_add_string(e, "nsds5replicaLastInitEnd", "0");
  1957. }
  1958. else
  1959. {
  1960. time_tmp = format_genTime(ra->last_init_end_time);
  1961. slapi_entry_add_string(e, "nsds5replicaLastInitEnd", time_tmp);
  1962. slapi_ch_free((void **)&time_tmp);
  1963. }
  1964. if (ra->last_init_status[0] != '\0')
  1965. {
  1966. slapi_entry_add_string(e, "nsds5replicaLastInitStatus", ra->last_init_status);
  1967. }
  1968. }
  1969. return SLAPI_DSE_CALLBACK_OK;
  1970. }
  1971. static void
  1972. update_window_state_change_callback (void *arg, PRBool opened)
  1973. {
  1974. Repl_Agmt *agmt = (Repl_Agmt*)arg;
  1975. PR_ASSERT (agmt);
  1976. if (opened)
  1977. {
  1978. prot_notify_window_opened (agmt->protocol);
  1979. }
  1980. else
  1981. {
  1982. prot_notify_window_closed (agmt->protocol);
  1983. }
  1984. }
  1985. ReplicaId
  1986. agmt_get_consumer_rid ( Repl_Agmt *agmt, void *conn )
  1987. {
  1988. if ( agmt->consumerRID <= 0 ) {
  1989. char *mapping_tree_node = NULL;
  1990. struct berval **bvals = NULL;
  1991. /* This function converts the old style DN to the new one. */
  1992. mapping_tree_node =
  1993. slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config",
  1994. slapi_sdn_get_dn (agmt->replarea) );
  1995. if (NULL == mapping_tree_node) {
  1996. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1997. "agmt_get_consumer_rid: failed to normalize "
  1998. "replica dn for %s\n",
  1999. slapi_sdn_get_dn (agmt->replarea));
  2000. agmt->consumerRID = 0;
  2001. }
  2002. conn_read_entry_attribute ( conn, mapping_tree_node, "nsDS5ReplicaID", &bvals );
  2003. if ( NULL != bvals && NULL != bvals[0] ) {
  2004. char *ridstr = slapi_ch_malloc( bvals[0]->bv_len + 1 );
  2005. memcpy ( ridstr, bvals[0]->bv_val, bvals[0]->bv_len );
  2006. ridstr[bvals[0]->bv_len] = '\0';
  2007. agmt->consumerRID = atoi (ridstr);
  2008. slapi_ch_free ( (void**) &ridstr );
  2009. ber_bvecfree ( bvals );
  2010. }
  2011. slapi_ch_free_string(&mapping_tree_node);
  2012. }
  2013. return agmt->consumerRID;
  2014. }
  2015. int
  2016. get_agmt_agreement_type( Repl_Agmt *agmt)
  2017. {
  2018. PR_ASSERT (agmt);
  2019. return agmt->agreement_type;
  2020. }
  2021. void* agmt_get_priv (const Repl_Agmt *agmt)
  2022. {
  2023. PR_ASSERT (agmt);
  2024. return agmt->priv;
  2025. }
  2026. void agmt_set_priv (Repl_Agmt *agmt, void* priv)
  2027. {
  2028. PR_ASSERT (agmt);
  2029. agmt->priv = priv;
  2030. }
  2031. ReplicaId agmt_get_consumerRID(Repl_Agmt *ra)
  2032. {
  2033. return ra->consumerRID;
  2034. }
  2035. int
  2036. agmt_has_protocol(Repl_Agmt *agmt)
  2037. {
  2038. if (agmt) {
  2039. return NULL != agmt->protocol;
  2040. }
  2041. return 0;
  2042. }