repl5_agmt.c 56 KB

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