repl5_agmt.c 54 KB

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