repl5_agmt.c 56 KB

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