repl5_agmt.c 69 KB

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