repl5_agmt.c 69 KB

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