windows_inc_protocol.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /* repl5_inc_protocol.c */
  7. /*
  8. The Prot_Incremental object implements the DS 5.0 multi-master incremental
  9. replication protocol.
  10. Stuff to do:
  11. - Need to figure out how asynchronous events end up in here. They are:
  12. - entry updated in replicated area.
  13. - backoff timeout
  14. - enter/leave.
  15. Perhaps these events should be properties of the main protocol.
  16. */
  17. #include "repl.h"
  18. #include "repl5.h"
  19. #include "windowsrepl.h"
  20. #include "windows_prot_private.h"
  21. #include "slap.h" /* PSEUDO_ATTR_UNHASHED */
  22. #include "repl5_ruv.h"
  23. #include "cl5_api.h"
  24. #include "slapi-plugin.h"
  25. extern int slapi_log_urp;
  26. typedef struct attribute_map
  27. {
  28. char *old_name;
  29. char *new_name;
  30. int map_to;
  31. } attribute_map;
  32. static attribute_map mapping[] =
  33. { /* map the attribute from , to, which platform we going to? */
  34. { "ntUserDomainId", "sAMAccountName", REPLICA_TYPE_WINDOWS},
  35. { "ntUserHomeDir", "homeDirectory", REPLICA_TYPE_WINDOWS},
  36. { "ntUserScriptPath", "scriptPath", REPLICA_TYPE_WINDOWS},
  37. { "ntUserLastLogon", "lastLogon", REPLICA_TYPE_WINDOWS},
  38. { "ntUserLastLogoff", "lastLogoff", REPLICA_TYPE_WINDOWS},
  39. { "cn", "description", REPLICA_TYPE_WINDOWS}, /* temporary */
  40. { "uid", "cn", REPLICA_TYPE_WINDOWS},
  41. { "dn", "dn", REPLICA_TYPE_WINDOWS},
  42. { "sn", "sn", REPLICA_TYPE_WINDOWS},
  43. { "sAMAccountName", "ntUserDomainId", REPLICA_TYPE_MULTIMASTER},
  44. { "homeDirectory", "ntUserHomeDir", REPLICA_TYPE_MULTIMASTER},
  45. { "scriptPath", "ntUserScriptPath", REPLICA_TYPE_MULTIMASTER},
  46. { "lastLogon", "ntUserLastLogon", REPLICA_TYPE_MULTIMASTER},
  47. { "lastLogoff", "ntUserLastLogoff", REPLICA_TYPE_MULTIMASTER},
  48. { "sAMAccountName", "uid", REPLICA_TYPE_MULTIMASTER},
  49. { "dn", "dn", REPLICA_TYPE_MULTIMASTER},
  50. { "sAMAccountName", "sn", REPLICA_TYPE_MULTIMASTER},
  51. { "sAMAccountName", "cn", REPLICA_TYPE_MULTIMASTER},
  52. {NULL, NULL, -1}
  53. };
  54. static attribute_map group_map[] =
  55. {
  56. { "ntGroupDomainId ", "name", REPLICA_TYPE_WINDOWS},
  57. { "ntGroupDomainId ", "sAMAccountName", REPLICA_TYPE_WINDOWS},
  58. { "ntGroupType", "groupType", REPLICA_TYPE_WINDOWS},
  59. { "sAMAccountName", "ntGroupDomainId ", REPLICA_TYPE_MULTIMASTER},
  60. { "groupType", "ntGroupType", REPLICA_TYPE_MULTIMASTER},
  61. {NULL, NULL, -1}
  62. };
  63. void map_entry_user(Slapi_Entry **e, int map_to, char** password);
  64. static Slapi_Entry* windows_entry_already_exists(Slapi_Entry *e);
  65. static Slapi_DN* map_dn_user(Slapi_DN *sdn, int map_to, const Slapi_DN *root);
  66. static Slapi_DN* map_dn_group(Slapi_DN *sdn, int map_to, const Slapi_DN *root);
  67. static void make_mods_from_entries(Slapi_Entry *new_entry, Slapi_Entry *existing_entry, LDAPMod ***attrs);
  68. static void alter_mods(LDAPMod ***m, char** password);
  69. /*** from proto-slap.h ***/
  70. void ava_done(struct ava *ava);
  71. typedef struct windows_inc_private
  72. {
  73. char *ruv; /* RUV on remote replica (use diff type for this? - ggood */
  74. Backoff_Timer *backoff;
  75. Repl_Protocol *rp;
  76. PRLock *lock;
  77. PRUint32 eventbits;
  78. } windows_inc_private;
  79. /* Various states the incremental protocol can pass through */
  80. #define STATE_START 0 /* ONREPL - should we rename this - we don't use it just to start up? */
  81. #define STATE_WAIT_WINDOW_OPEN 1
  82. #define STATE_WAIT_CHANGES 2
  83. #define STATE_READY_TO_ACQUIRE 3
  84. #define STATE_BACKOFF_START 4 /* ONREPL - can we combine BACKOFF_START and BACKOFF states? */
  85. #define STATE_BACKOFF 5
  86. #define STATE_SENDING_UPDATES 6
  87. #define STATE_STOP_FATAL_ERROR 7
  88. #define STATE_STOP_FATAL_ERROR_PART2 8
  89. #define STATE_STOP_NORMAL_TERMINATION 9
  90. /* Events (synchronous and asynchronous; these are bits) */
  91. #define EVENT_WINDOW_OPENED 1
  92. #define EVENT_WINDOW_CLOSED 2
  93. #define EVENT_TRIGGERING_CRITERIA_MET 4 /* ONREPL - should we rename this to EVENT_CHANGE_AVAILABLE */
  94. #define EVENT_BACKOFF_EXPIRED 8
  95. #define EVENT_REPLICATE_NOW 16
  96. #define EVENT_PROTOCOL_SHUTDOWN 32
  97. #define EVENT_AGMT_CHANGED 64
  98. #define UPDATE_NO_MORE_UPDATES 201
  99. #define UPDATE_TRANSIENT_ERROR 202
  100. #define UPDATE_FATAL_ERROR 203
  101. #define UPDATE_SCHEDULE_WINDOW_CLOSED 204
  102. #define UPDATE_CONNECTION_LOST 205
  103. #define UPDATE_TIMEOUT 206
  104. #define UPDATE_YIELD 207
  105. /* Return codes from examine_update_vector */
  106. #define EXAMINE_RUV_PRISTINE_REPLICA 401
  107. #define EXAMINE_RUV_GENERATION_MISMATCH 402
  108. #define EXAMINE_RUV_REPLICA_TOO_OLD 403
  109. #define EXAMINE_RUV_OK 404
  110. #define EXAMINE_RUV_PARAM_ERROR 405
  111. #define MAX_CHANGES_PER_SESSION 10000
  112. /*
  113. * Maximum time to wait between replication sessions. If we
  114. * don't see any updates for a period equal to this interval,
  115. * we go ahead and start a replication session, just to be safe
  116. */
  117. #define MAX_WAIT_BETWEEN_SESSIONS PR_SecondsToInterval(60 * 5) /* 5 minutes */
  118. /*
  119. * tests if the protocol has been shutdown and we need to quit
  120. * event_occurred resets the bits in the bit flag, so whoever tests for shutdown
  121. * resets the flags, so the next one who tests for shutdown won't get it, so we
  122. * also look at the terminate flag
  123. */
  124. #define PROTOCOL_IS_SHUTDOWN(prp) (event_occurred(prp, EVENT_PROTOCOL_SHUTDOWN) || prp->terminate)
  125. /* Forward declarations */
  126. static PRUint32 event_occurred(Private_Repl_Protocol *prp, PRUint32 event);
  127. static void reset_events (Private_Repl_Protocol *prp);
  128. static void protocol_sleep(Private_Repl_Protocol *prp, PRIntervalTime duration);
  129. static int send_updates(Private_Repl_Protocol *prp, RUV *ruv, PRUint32 *num_changes_sent);
  130. static void windows_inc_backoff_expired(time_t timer_fire_time, void *arg);
  131. static int windows_examine_update_vector(Private_Repl_Protocol *prp, RUV *ruv);
  132. static PRBool ignore_error_and_keep_going(int error);
  133. static const char* state2name (int state);
  134. static const char* event2name (int event);
  135. static const char* op2string (int op);
  136. /*
  137. * It's specifically ok to delete a protocol instance that
  138. * is currently running. The instance will be shut down, and
  139. * then resources will be freed. Since a graceful shutdown is
  140. * attempted, this function may take some time to complete.
  141. */
  142. static void
  143. windows_inc_delete(Private_Repl_Protocol **prpp)
  144. {
  145. /* First, stop the protocol if it isn't already stopped */
  146. /* Then, delete all resources used by the protocol */
  147. }
  148. /* helper function */
  149. void
  150. w_set_pause_and_busy_time(long *pausetime, long *busywaittime)
  151. {
  152. /* If neither are set, set busy time to its default */
  153. if (!*pausetime && !*busywaittime)
  154. {
  155. *busywaittime = PROTOCOL_BUSY_BACKOFF_MINIMUM;
  156. }
  157. /* pause time must be at least 1 more than the busy backoff time */
  158. if (*pausetime && !*busywaittime)
  159. {
  160. /*
  161. * user specified a pause time but no busy wait time - must
  162. * set busy wait time to 1 less than pause time - if pause
  163. * time is 1, we must set it to 2
  164. */
  165. if (*pausetime < 2)
  166. {
  167. *pausetime = 2;
  168. }
  169. *busywaittime = *pausetime - 1;
  170. }
  171. else if (!*pausetime && *busywaittime)
  172. {
  173. /*
  174. * user specified a busy wait time but no pause time - must
  175. * set pause time to 1 more than busy wait time
  176. */
  177. *pausetime = *busywaittime + 1;
  178. }
  179. else if (*pausetime && *busywaittime && *pausetime <= *busywaittime)
  180. {
  181. /*
  182. * user specified both pause and busy wait times, but the pause
  183. * time was <= busy wait time - pause time must be at least
  184. * 1 more than the busy wait time
  185. */
  186. *pausetime = *busywaittime + 1;
  187. }
  188. }
  189. /*
  190. * Do the incremental protocol.
  191. *
  192. * What's going on here? This thing is a state machine. It has the
  193. * following states:
  194. *
  195. * State transition table:
  196. *
  197. * Curr State Condition/Event Next State
  198. * ---------- ------------ -----------
  199. * START schedule window is open ACQUIRE_REPLICA
  200. * schedule window is closed WAIT_WINDOW_OPEN
  201. * WAIT_WINDOW_OPEN schedule change START
  202. * replicate now ACQUIRE_REPLICA
  203. * schedule window opens ACQUIRE_REPLICA
  204. * ACQUIRE_REPLICA acquired replica SEND_CHANGES
  205. * failed to acquire - transient error START_BACKOFF
  206. * failed to acquire - fatal error STOP_FATAL_ERROR
  207. * SEND_CHANGES can't update CONSUMER_NEEDS_REINIT
  208. * no changes to send WAIT_CHANGES
  209. * can't send - thransient error START_BACKOF
  210. * can't send - window closed WAIT_WINDOW_OPEN
  211. * can'r send - fatal error STOP_FATAL_ERROR
  212. * START_BACKOF replicate now ACQUIRE_REPLICA
  213. * schedule changes START
  214. * schedule window closes WAIT_WINDOW_OPEN
  215. * backoff expires & can acquire SEND_CHANGES
  216. * backoff expires & can't acquire-trans BACKOFF
  217. * backoff expires & can't acquire-fatal STOP_FATAL_ERROR
  218. * BACKOF replicate now ACQUIRE_REPLICA
  219. * schedule changes START
  220. * schedule window closes WAIT_WINDOW_OPEN
  221. * backoff expires & can acquire SEND_CHANGES
  222. * backoff expires & can't acquire-trans BACKOFF
  223. * backoff expires & can't acquire-fatal STOP_FATAL_ERROR
  224. * WAIT_CHANGES schedule window closes WAIT_WINDOW_OPEN
  225. * replicate_now ACQUIRE_REPLICA
  226. * change available ACQUIRE_REPLICA
  227. * schedule_change START
  228. */
  229. /*
  230. * Main state machine for the incremental protocol. This routine will,
  231. * under normal circumstances, not return until the protocol is shut
  232. * down.
  233. */
  234. static void
  235. windows_inc_run(Private_Repl_Protocol *prp)
  236. {
  237. int current_state = STATE_START;
  238. int next_state = STATE_START;
  239. windows_inc_private *prp_priv = (windows_inc_private *)prp->private;
  240. int done;
  241. int e1;
  242. RUV *ruv = NULL;
  243. CSN *cons_schema_csn;
  244. Replica *replica;
  245. int wait_change_timer_set = 0;
  246. time_t last_start_time;
  247. PRUint32 num_changes_sent;
  248. char *hostname = NULL;
  249. int portnum = 0;
  250. /* use a different backoff timer strategy for ACQUIRE_REPLICA_BUSY errors */
  251. PRBool use_busy_backoff_timer = PR_FALSE;
  252. long pausetime = 0;
  253. long busywaittime = 0;
  254. prp->stopped = 0;
  255. prp->terminate = 0;
  256. hostname = agmt_get_hostname(prp->agmt);
  257. portnum = agmt_get_port(prp->agmt);
  258. /* establish_protocol_callbacks(prp); */
  259. done = 0;
  260. do {
  261. int rc;
  262. /* Take action, based on current state, and compute new state. */
  263. switch (current_state)
  264. {
  265. case STATE_START:
  266. dev_debug("windows_inc_run(STATE_START)");
  267. if (PROTOCOL_IS_SHUTDOWN(prp))
  268. {
  269. done = 1;
  270. break;
  271. }
  272. /*
  273. * Our initial state. See if we're in a schedule window. If
  274. * so, then we're ready to acquire the replica and see if it
  275. * needs any updates from us. If not, then wait for the window
  276. * to open.
  277. */
  278. if (agmt_schedule_in_window_now(prp->agmt))
  279. {
  280. next_state = STATE_READY_TO_ACQUIRE;
  281. }
  282. else
  283. {
  284. next_state = STATE_WAIT_WINDOW_OPEN;
  285. }
  286. /* we can get here from other states because some events happened and were
  287. not cleared. For instance when we wake up in STATE_WAIT_CHANGES state.
  288. Since this is a fresh start state, we should clear all events */
  289. /* ONREPL - this does not feel right - we should take another look
  290. at this state machine */
  291. reset_events (prp);
  292. /* Cancel any linger timer that might be in effect... */
  293. windows_conn_cancel_linger(prp->conn);
  294. /* ... and disconnect, if currently connected */
  295. windows_conn_disconnect(prp->conn);
  296. /* get the new pause time, if any */
  297. pausetime = agmt_get_pausetime(prp->agmt);
  298. /* get the new busy wait time, if any */
  299. busywaittime = agmt_get_busywaittime(prp->agmt);
  300. if (pausetime || busywaittime)
  301. {
  302. /* helper function to make sure they are set correctly */
  303. w_set_pause_and_busy_time(&pausetime, &busywaittime);
  304. }
  305. break;
  306. case STATE_WAIT_WINDOW_OPEN:
  307. /*
  308. * We're waiting for a schedule window to open. If one did,
  309. * or we receive a "replicate now" event, then start a protocol
  310. * session immediately. If the replication schedule changed, go
  311. * back to start. Otherwise, go back to sleep.
  312. */
  313. dev_debug("windows_inc_run(STATE_WAIT_WINDOW_OPEN)");
  314. if (PROTOCOL_IS_SHUTDOWN(prp))
  315. {
  316. done = 1;
  317. break;
  318. }
  319. else if (event_occurred(prp, EVENT_WINDOW_OPENED))
  320. {
  321. next_state = STATE_READY_TO_ACQUIRE;
  322. }
  323. else if (event_occurred(prp, EVENT_REPLICATE_NOW))
  324. {
  325. next_state = STATE_READY_TO_ACQUIRE;
  326. }
  327. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  328. {
  329. next_state = STATE_START;
  330. windows_conn_set_agmt_changed(prp->conn);
  331. }
  332. else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET)) /* change available */
  333. {
  334. /* just ignore it and go to sleep */
  335. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  336. }
  337. else if (e1 = event_occurred(prp, EVENT_WINDOW_CLOSED) ||
  338. event_occurred(prp, EVENT_BACKOFF_EXPIRED))
  339. {
  340. /* this events - should not occur - log a warning and go to sleep */
  341. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  342. "%s: Incremental protocol: "
  343. "event %s should not occur in state %s; going to sleep\n",
  344. agmt_get_long_name(prp->agmt),
  345. e1 ? event2name(EVENT_WINDOW_CLOSED) : event2name(EVENT_BACKOFF_EXPIRED),
  346. state2name(current_state));
  347. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  348. }
  349. else
  350. {
  351. /* wait until window opens or an event occurs */
  352. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  353. "%s: Incremental protocol: "
  354. "waiting for update window to open\n", agmt_get_long_name(prp->agmt));
  355. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  356. }
  357. break;
  358. case STATE_WAIT_CHANGES:
  359. /*
  360. * We're in a replication window, but we're waiting for more
  361. * changes to accumulate before we actually hook up and send
  362. * them.
  363. */
  364. dev_debug("windows_inc_run(STATE_WAIT_CHANGES)");
  365. if (PROTOCOL_IS_SHUTDOWN(prp))
  366. {
  367. dev_debug("windows_inc_run(STATE_WAIT_CHANGES): PROTOCOL_IS_SHUTING_DOWN -> end windows_inc_run\n");
  368. done = 1;
  369. break;
  370. }
  371. else if (event_occurred(prp, EVENT_REPLICATE_NOW))
  372. {
  373. dev_debug("windows_inc_run(STATE_WAIT_CHANGES): EVENT_REPLICATE_NOW received -> STATE_READY_TO_ACQUIRE\n");
  374. next_state = STATE_READY_TO_ACQUIRE;
  375. wait_change_timer_set = 0;
  376. }
  377. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  378. {
  379. dev_debug("windows_inc_run(STATE_WAIT_CHANGES): EVENT_AGMT_CHANGED received -> STATE_START\n");
  380. next_state = STATE_START;
  381. windows_conn_set_agmt_changed(prp->conn);
  382. wait_change_timer_set = 0;
  383. }
  384. else if (event_occurred(prp, EVENT_WINDOW_CLOSED))
  385. {
  386. dev_debug("windows_inc_run(STATE_WAIT_CHANGES): EVENT_WINDOW_CLOSED received -> STATE_WAIT_WINDOW_OPEN\n");
  387. next_state = STATE_WAIT_WINDOW_OPEN;
  388. wait_change_timer_set = 0;
  389. }
  390. else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET))
  391. {
  392. dev_debug("windows_inc_run(STATE_WAIT_CHANGES): EVENT_TRIGGERING_CRITERIA_MET received -> STATE_READY_TO_ACQUIRE\n");
  393. next_state = STATE_READY_TO_ACQUIRE;
  394. wait_change_timer_set = 0;
  395. }
  396. else if (e1 = event_occurred(prp, EVENT_WINDOW_OPENED) ||
  397. event_occurred(prp, EVENT_BACKOFF_EXPIRED))
  398. {
  399. /* this events - should not occur - log a warning and clear the event */
  400. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name, "%s: Incremental protocol: "
  401. "event %s should not occur in state %s\n",
  402. agmt_get_long_name(prp->agmt),
  403. e1 ? event2name(EVENT_WINDOW_OPENED) : event2name(EVENT_BACKOFF_EXPIRED),
  404. state2name(current_state));
  405. wait_change_timer_set = 0;
  406. }
  407. else
  408. {
  409. if (wait_change_timer_set)
  410. {
  411. /* We are here because our timer expired */
  412. dev_debug("windows_inc_run(STATE_WAIT_CHANGES): wait_change_timer_set expired -> STATE_START\n");
  413. next_state = STATE_START;
  414. wait_change_timer_set = 0;
  415. }
  416. else
  417. {
  418. /* We are here because the last replication session
  419. * finished or aborted.
  420. */
  421. wait_change_timer_set = 1;
  422. protocol_sleep(prp, MAX_WAIT_BETWEEN_SESSIONS);
  423. }
  424. }
  425. break;
  426. case STATE_READY_TO_ACQUIRE:
  427. dev_debug("windows_inc_run(STATE_READY_TO_ACQUIRE)");
  428. if (PROTOCOL_IS_SHUTDOWN(prp))
  429. {
  430. done = 1;
  431. break;
  432. }
  433. /* ONREPL - at this state we unconditionally acquire the replica
  434. ignoring all events. Not sure if this is good */
  435. object_acquire(prp->replica_object);
  436. replica = object_get_data(prp->replica_object);
  437. rc = windows_acquire_replica(prp, &ruv);
  438. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  439. "windows_acquire_replica returned %s (%d)\n",
  440. state2name(rc),
  441. rc);
  442. use_busy_backoff_timer = PR_FALSE; /* default */
  443. if (rc == ACQUIRE_SUCCESS)
  444. {
  445. next_state = STATE_SENDING_UPDATES;
  446. }
  447. else if (rc == ACQUIRE_REPLICA_BUSY)
  448. {
  449. next_state = STATE_BACKOFF_START;
  450. use_busy_backoff_timer = PR_TRUE;
  451. }
  452. else if (rc == ACQUIRE_CONSUMER_WAS_UPTODATE)
  453. {
  454. next_state = STATE_WAIT_CHANGES;
  455. }
  456. else if (rc == ACQUIRE_TRANSIENT_ERROR)
  457. {
  458. next_state = STATE_BACKOFF_START;
  459. }
  460. else if (rc == ACQUIRE_FATAL_ERROR)
  461. {
  462. next_state = STATE_STOP_FATAL_ERROR;
  463. }
  464. if (rc != ACQUIRE_SUCCESS)
  465. {
  466. int optype, ldaprc;
  467. windows_conn_get_error(prp->conn, &optype, &ldaprc);
  468. agmt_set_last_update_status(prp->agmt, ldaprc,
  469. prp->last_acquire_response_code, NULL);
  470. }
  471. object_release(prp->replica_object); replica = NULL;
  472. break;
  473. case STATE_BACKOFF_START:
  474. dev_debug("windows_inc_run(STATE_BACKOFF_START)");
  475. if (PROTOCOL_IS_SHUTDOWN(prp))
  476. {
  477. done = 1;
  478. break;
  479. }
  480. if (event_occurred(prp, EVENT_REPLICATE_NOW))
  481. {
  482. next_state = STATE_READY_TO_ACQUIRE;
  483. }
  484. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  485. {
  486. next_state = STATE_START;
  487. windows_conn_set_agmt_changed(prp->conn);
  488. }
  489. else if (event_occurred (prp, EVENT_WINDOW_CLOSED))
  490. {
  491. next_state = STATE_WAIT_WINDOW_OPEN;
  492. }
  493. else if (event_occurred (prp, EVENT_TRIGGERING_CRITERIA_MET))
  494. {
  495. /* consume and ignore */
  496. }
  497. else if (e1 = event_occurred (prp, EVENT_WINDOW_OPENED) ||
  498. event_occurred (prp, EVENT_BACKOFF_EXPIRED))
  499. {
  500. /* This should never happen */
  501. /* this events - should not occur - log a warning and go to sleep */
  502. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  503. "%s: Incremental protocol: event %s should not occur in state %s\n",
  504. agmt_get_long_name(prp->agmt),
  505. e1 ? event2name(EVENT_WINDOW_OPENED) : event2name(EVENT_BACKOFF_EXPIRED),
  506. state2name(current_state));
  507. }
  508. else
  509. {
  510. /* Set up the backoff timer to wake us up at the appropriate time */
  511. if (use_busy_backoff_timer)
  512. {
  513. /* we received a busy signal from the consumer, wait for a while */
  514. if (!busywaittime)
  515. {
  516. busywaittime = PROTOCOL_BUSY_BACKOFF_MINIMUM;
  517. }
  518. prp_priv->backoff = backoff_new(BACKOFF_FIXED, busywaittime,
  519. busywaittime);
  520. }
  521. else
  522. {
  523. prp_priv->backoff = backoff_new(BACKOFF_EXPONENTIAL, PROTOCOL_BACKOFF_MINIMUM,
  524. PROTOCOL_BACKOFF_MAXIMUM);
  525. }
  526. next_state = STATE_BACKOFF;
  527. backoff_reset(prp_priv->backoff, windows_inc_backoff_expired, (void *)prp);
  528. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  529. use_busy_backoff_timer = PR_FALSE;
  530. }
  531. break;
  532. case STATE_BACKOFF:
  533. /*
  534. * We're in a backoff state.
  535. */
  536. dev_debug("windows_inc_run(STATE_BACKOFF)");
  537. if (PROTOCOL_IS_SHUTDOWN(prp))
  538. {
  539. if (prp_priv->backoff)
  540. backoff_delete(&prp_priv->backoff);
  541. done = 1;
  542. break;
  543. }
  544. else if (event_occurred(prp, EVENT_REPLICATE_NOW))
  545. {
  546. next_state = STATE_READY_TO_ACQUIRE;
  547. }
  548. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  549. {
  550. next_state = STATE_START;
  551. windows_conn_set_agmt_changed(prp->conn);
  552. /* Destroy the backoff timer, since we won't need it anymore */
  553. if (prp_priv->backoff)
  554. backoff_delete(&prp_priv->backoff);
  555. }
  556. else if (event_occurred(prp, EVENT_WINDOW_CLOSED))
  557. {
  558. next_state = STATE_WAIT_WINDOW_OPEN;
  559. /* Destroy the backoff timer, since we won't need it anymore */
  560. if (prp_priv->backoff)
  561. backoff_delete(&prp_priv->backoff);
  562. }
  563. else if (event_occurred(prp, EVENT_BACKOFF_EXPIRED))
  564. {
  565. rc = windows_acquire_replica(prp, &ruv);
  566. use_busy_backoff_timer = PR_FALSE;
  567. if (rc == ACQUIRE_SUCCESS)
  568. {
  569. next_state = STATE_SENDING_UPDATES;
  570. }
  571. else if (rc == ACQUIRE_REPLICA_BUSY)
  572. {
  573. next_state = STATE_BACKOFF;
  574. use_busy_backoff_timer = PR_TRUE;
  575. }
  576. else if (rc == ACQUIRE_CONSUMER_WAS_UPTODATE)
  577. {
  578. next_state = STATE_WAIT_CHANGES;
  579. }
  580. else if (rc == ACQUIRE_TRANSIENT_ERROR)
  581. {
  582. next_state = STATE_BACKOFF;
  583. }
  584. else if (rc == ACQUIRE_FATAL_ERROR)
  585. {
  586. next_state = STATE_STOP_FATAL_ERROR;
  587. }
  588. if (rc != ACQUIRE_SUCCESS)
  589. {
  590. int optype, ldaprc;
  591. windows_conn_get_error(prp->conn, &optype, &ldaprc);
  592. agmt_set_last_update_status(prp->agmt, ldaprc,
  593. prp->last_acquire_response_code, NULL);
  594. }
  595. /*
  596. * We either need to step the backoff timer, or
  597. * destroy it if we don't need it anymore.
  598. */
  599. if (STATE_BACKOFF == next_state)
  600. {
  601. time_t next_fire_time;
  602. time_t now;
  603. /* Step the backoff timer */
  604. time(&now);
  605. next_fire_time = backoff_step(prp_priv->backoff);
  606. /* And go back to sleep */
  607. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  608. "%s: Replication session backing off for %d seconds\n",
  609. agmt_get_long_name(prp->agmt),
  610. next_fire_time - now);
  611. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  612. }
  613. else
  614. {
  615. /* Destroy the backoff timer, since we won't need it anymore */
  616. backoff_delete(&prp_priv->backoff);
  617. }
  618. use_busy_backoff_timer = PR_FALSE;
  619. }
  620. else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET))
  621. {
  622. /* changes are available */
  623. if ( prp_priv->backoff == NULL || backoff_expired (prp_priv->backoff, 60) )
  624. {
  625. /*
  626. * Have seen cases that the agmt stuck here forever since
  627. * somehow the backoff timer was not in event queue anymore.
  628. * If the backoff timer has expired more than 60 seconds,
  629. * destroy it.
  630. */
  631. if ( prp_priv->backoff )
  632. backoff_delete(&prp_priv->backoff);
  633. next_state = STATE_READY_TO_ACQUIRE;
  634. }
  635. else
  636. {
  637. /* ignore changes and go to sleep */
  638. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  639. }
  640. }
  641. else if (event_occurred(prp, EVENT_WINDOW_OPENED))
  642. {
  643. /* this should never happen - log an error and go to sleep */
  644. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name, "%s: Incremental protocol: "
  645. "event %s should not occur in state %s; going to sleep\n",
  646. agmt_get_long_name(prp->agmt),
  647. event2name(EVENT_WINDOW_OPENED), state2name(current_state));
  648. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  649. }
  650. break;
  651. case STATE_SENDING_UPDATES:
  652. dev_debug("windows_inc_run(STATE_SENDING_UPDATES)");
  653. agmt_set_update_in_progress(prp->agmt, PR_TRUE);
  654. num_changes_sent = 0;
  655. last_start_time = current_time();
  656. agmt_set_last_update_start(prp->agmt, last_start_time);
  657. /*
  658. * We've acquired the replica, and are ready to send any
  659. * needed updates.
  660. */
  661. if (PROTOCOL_IS_SHUTDOWN(prp))
  662. {
  663. windows_release_replica (prp);
  664. done = 1;
  665. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  666. agmt_set_last_update_end(prp->agmt, current_time());
  667. /* MAB: I don't find the following status correct. How do we know it has
  668. been stopped by an admin and not by a total update request, for instance?
  669. In any case, how is this protocol shutdown situation different from all the
  670. other ones that are present in this state machine? */
  671. /* richm: We at least need to let monitors know that the protocol has been
  672. shutdown - maybe they can figure out why */
  673. agmt_set_last_update_status(prp->agmt, 0, 0, "Protocol stopped");
  674. break;
  675. }
  676. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update started");
  677. /* ONREPL - in this state we send changes no matter what other events occur.
  678. This is because we can get because of the REPLICATE_NOW event which
  679. has high priority. Is this ok? */
  680. /* First, push new schema to the consumer if needed */
  681. /* ONREPL - should we push schema after we examine the RUV? */
  682. /*
  683. * GGOOREPL - I don't see why we should wait until we've
  684. * examined the RUV. The schema entry has its own CSN that is
  685. * used to decide if the remote schema needs to be updated.
  686. */
  687. cons_schema_csn = agmt_get_consumer_schema_csn ( prp->agmt );
  688. // rc = windows_conn_push_schema(prp->conn, &cons_schema_csn); XXX
  689. if ( cons_schema_csn != agmt_get_consumer_schema_csn ( prp->agmt ))
  690. {
  691. agmt_set_consumer_schema_csn ( prp->agmt, cons_schema_csn );
  692. }
  693. if (CONN_SCHEMA_UPDATED != rc && CONN_SCHEMA_NO_UPDATE_NEEDED != rc)
  694. {
  695. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  696. "%s: Warning: unable to replicate schema: rc=%d\n",
  697. agmt_get_long_name(prp->agmt), rc);
  698. /* But keep going */
  699. }
  700. dev_debug("windows_inc_run(STATE_SENDING_UPDATES) -> windows_examine_update_vector");
  701. rc = windows_examine_update_vector(prp, ruv);
  702. /*
  703. * Decide what to do next - proceed with incremental,
  704. * backoff, or total update
  705. */
  706. switch (rc)
  707. {
  708. case EXAMINE_RUV_PARAM_ERROR:
  709. /* this is really bad - we have NULL prp! */
  710. next_state = STATE_STOP_FATAL_ERROR;
  711. break;
  712. case EXAMINE_RUV_PRISTINE_REPLICA:
  713. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  714. "%s: Replica has no update vector. It has never been initialized.\n",
  715. agmt_get_long_name(prp->agmt));
  716. next_state = STATE_BACKOFF_START;
  717. break;
  718. case EXAMINE_RUV_GENERATION_MISMATCH:
  719. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  720. "%s: Replica has a different generation ID than the local data.\n",
  721. agmt_get_long_name(prp->agmt));
  722. next_state = STATE_BACKOFF_START;
  723. break;
  724. case EXAMINE_RUV_REPLICA_TOO_OLD:
  725. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  726. "%s: Replica update vector is too out of date to bring "
  727. "into sync using the incremental protocol. The replica "
  728. "must be reinitialized.\n", agmt_get_long_name(prp->agmt));
  729. next_state = STATE_BACKOFF_START;
  730. break;
  731. case EXAMINE_RUV_OK:
  732. /* update our csn generator state with the consumer's ruv data */
  733. dev_debug("windows_inc_run(STATE_SENDING_UPDATES) -> windows_examine_update_vector OK");
  734. object_acquire(prp->replica_object);
  735. replica = object_get_data(prp->replica_object);
  736. rc = replica_update_csngen_state (replica, ruv);
  737. object_release (prp->replica_object);
  738. replica = NULL;
  739. if (rc != 0) /* too much skew */
  740. {
  741. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  742. "%s: Incremental protocol: fatal error - too much time skew between replicas!\n",
  743. agmt_get_long_name(prp->agmt));
  744. next_state = STATE_STOP_FATAL_ERROR;
  745. }
  746. else
  747. {
  748. rc = send_updates(prp, ruv, &num_changes_sent);
  749. if (rc == UPDATE_NO_MORE_UPDATES)
  750. {
  751. dev_debug("windows_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_NO_MORE_UPDATES -> STATE_WAIT_CHANGES");
  752. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update succeeded");
  753. next_state = STATE_WAIT_CHANGES;
  754. }
  755. else if (rc == UPDATE_YIELD)
  756. {
  757. dev_debug("windows_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_YIELD -> STATE_BACKOFF_START");
  758. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update succeeded and yielded");
  759. next_state = STATE_BACKOFF_START;
  760. }
  761. else if (rc == UPDATE_TRANSIENT_ERROR)
  762. {
  763. dev_debug("windows_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_TRANSIENT_ERROR -> STATE_BACKOFF_START");
  764. next_state = STATE_BACKOFF_START;
  765. }
  766. else if (rc == UPDATE_FATAL_ERROR)
  767. {
  768. dev_debug("windows_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_FATAL_ERROR -> STATE_STOP_FATAL_ERROR");
  769. next_state = STATE_STOP_FATAL_ERROR;
  770. }
  771. else if (rc == UPDATE_SCHEDULE_WINDOW_CLOSED)
  772. {
  773. dev_debug("windows_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_SCHEDULE_WINDOW_CLOSED -> STATE_WAIT_WINDOW_OPEN");
  774. /* ONREPL - I don't think we should check this. We might be
  775. here because of replicate_now event - so we don't care
  776. about the schedule */
  777. next_state = STATE_WAIT_WINDOW_OPEN;
  778. /* ONREPL - do we need to release the replica here ? */
  779. windows_conn_disconnect (prp->conn);
  780. }
  781. else if (rc == UPDATE_CONNECTION_LOST)
  782. {
  783. dev_debug("windows_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_CONNECTION_LOST -> STATE_BACKOFF_START");
  784. next_state = STATE_BACKOFF_START;
  785. }
  786. else if (rc == UPDATE_TIMEOUT)
  787. {
  788. dev_debug("windows_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_TIMEOUT -> STATE_BACKOFF_START");
  789. next_state = STATE_BACKOFF_START;
  790. }
  791. }
  792. last_start_time = 0UL;
  793. break;
  794. }
  795. /* if (NULL != ruv)
  796. {
  797. ruv_destroy(&ruv); ruv = NULL;
  798. } XXX elliot commented this out */
  799. agmt_set_last_update_end(prp->agmt, current_time());
  800. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  801. /* If timed out, close the connection after released the replica */
  802. windows_release_replica(prp);
  803. if (rc == UPDATE_TIMEOUT) {
  804. windows_conn_disconnect(prp->conn);
  805. }
  806. if (rc == UPDATE_NO_MORE_UPDATES && num_changes_sent > 0)
  807. {
  808. if (pausetime > 0)
  809. {
  810. /* richm - 20020219 - If we have acquired the consumer, and another master has gone
  811. into backoff waiting for us to release it, we may acquire the replica sooner
  812. than the other master has a chance to, and the other master may not be able
  813. to acquire the consumer for a long time (hours, days?) if this server is
  814. under a heavy load (see reliab06 et. al. system tests)
  815. So, this sleep gives the other master(s) a chance to acquire the consumer
  816. replica */
  817. long loops = pausetime;
  818. /* the while loop is so that we don't just sleep and sleep if an
  819. event comes in that we should handle immediately (like shutdown) */
  820. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  821. "%s: Pausing updates for %ld seconds to allow other suppliers to update consumer\n",
  822. agmt_get_long_name(prp->agmt), pausetime);
  823. while (loops-- && !(PROTOCOL_IS_SHUTDOWN(prp)))
  824. {
  825. DS_Sleep(PR_SecondsToInterval(1));
  826. }
  827. }
  828. else if (num_changes_sent > 10)
  829. {
  830. /* wait for consumer to write its ruv if the replication was busy */
  831. /* When asked, consumer sends its ruv in cache to the supplier. */
  832. /* DS_Sleep ( PR_SecondsToInterval(1) ); */
  833. }
  834. }
  835. windows_dirsync_inc_run(prp);
  836. break;
  837. case STATE_STOP_FATAL_ERROR:
  838. /*
  839. * We encountered some sort of a fatal error. Suspend.
  840. */
  841. /* XXXggood update state in replica */
  842. agmt_set_last_update_status(prp->agmt, -1, 0, "Incremental update has failed and requires administrator action");
  843. dev_debug("windows_inc_run(STATE_STOP_FATAL_ERROR)");
  844. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  845. "%s: Incremental update failed and requires administrator action\n",
  846. agmt_get_long_name(prp->agmt));
  847. next_state = STATE_STOP_FATAL_ERROR_PART2;
  848. break;
  849. case STATE_STOP_FATAL_ERROR_PART2:
  850. if (PROTOCOL_IS_SHUTDOWN(prp))
  851. {
  852. done = 1;
  853. break;
  854. }
  855. /* MAB: This state is the FATAL state where we are supposed to get
  856. as a result of a FATAL error on send_updates. But, as bug
  857. states, send_updates was always returning TRANSIENT errors and never
  858. FATAL... In other words, this code has never been tested before...
  859. As of 01/16/01, this piece of code was in a very dangerous state. In particular,
  860. 1) it does not catch any events
  861. 2) it is a terminal state (once reached it never transitions to a different state)
  862. Both things combined make this state to become a consuming infinite loop
  863. that is useless after all (we are in a fatal place requiring manual admin jobs */
  864. /* MAB: The following lines fix problem number 1 above... When the code gets
  865. into this state, it should only get a chance to get out of it by an
  866. EVENT_AGMT_CHANGED event... All other events should be ignored */
  867. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  868. {
  869. dev_debug("windows_inc_run(STATE_STOP_FATAL_ERROR): EVENT_AGMT_CHANGED received\n");
  870. /* Chance to recover for the EVENT_AGMT_CHANGED event.
  871. This is not mandatory, but fixes problem 2 above */
  872. next_state = STATE_STOP_NORMAL_TERMINATION;
  873. }
  874. else
  875. {
  876. dev_debug("windows_inc_run(STATE_STOP_FATAL_ERROR): Event received. Clearing it\n");
  877. reset_events (prp);
  878. }
  879. protocol_sleep (prp, PR_INTERVAL_NO_TIMEOUT);
  880. break;
  881. case STATE_STOP_NORMAL_TERMINATION:
  882. /*
  883. * We encountered some sort of a fatal error. Return.
  884. */
  885. /* XXXggood update state in replica */
  886. dev_debug("windows_inc_run(STATE_STOP_NORMAL_TERMINATION)");
  887. done = 1;
  888. break;
  889. }
  890. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  891. "%s: State: %s -> %s\n",
  892. agmt_get_long_name(prp->agmt),
  893. state2name(current_state), state2name(next_state));
  894. current_state = next_state;
  895. } while (!done);
  896. slapi_ch_free((void**)&hostname);
  897. /* remove_protocol_callbacks(prp); */
  898. prp->stopped = 1;
  899. /* Cancel any linger timer that might be in effect... */
  900. conn_cancel_linger(prp->conn);
  901. /* ... and disconnect, if currently connected */
  902. conn_disconnect(prp->conn);
  903. }
  904. /*
  905. * Go to sleep until awakened.
  906. */
  907. static void
  908. protocol_sleep(Private_Repl_Protocol *prp, PRIntervalTime duration)
  909. {
  910. PR_ASSERT(NULL != prp);
  911. PR_Lock(prp->lock);
  912. /* we should not go to sleep if there are events available to be processed.
  913. Otherwise, we can miss the event that suppose to wake us up */
  914. if (prp->eventbits == 0)
  915. PR_WaitCondVar(prp->cvar, duration);
  916. else
  917. {
  918. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  919. "%s: Incremental protocol: can't go to sleep: event bits - %x\n",
  920. agmt_get_long_name(prp->agmt), prp->eventbits);
  921. }
  922. PR_Unlock(prp->lock);
  923. }
  924. /*
  925. * Notify the protocol about some event. Signal the condition
  926. * variable in case the protocol is sleeping. Multiple occurences
  927. * of a single event type are not remembered (e.g. no stack
  928. * of events is maintained).
  929. */
  930. static void
  931. event_notify(Private_Repl_Protocol *prp, PRUint32 event)
  932. {
  933. PR_ASSERT(NULL != prp);
  934. PR_Lock(prp->lock);
  935. prp->eventbits |= event;
  936. PR_NotifyCondVar(prp->cvar);
  937. PR_Unlock(prp->lock);
  938. }
  939. /*
  940. * Test to see if an event occurred. The event is cleared when
  941. * read.
  942. */
  943. static PRUint32
  944. event_occurred(Private_Repl_Protocol *prp, PRUint32 event)
  945. {
  946. PRUint32 return_value;
  947. PR_ASSERT(NULL != prp);
  948. PR_Lock(prp->lock);
  949. return_value = (prp->eventbits & event);
  950. prp->eventbits &= ~event; /* Clear event */
  951. PR_Unlock(prp->lock);
  952. return return_value;
  953. }
  954. static void
  955. reset_events (Private_Repl_Protocol *prp)
  956. {
  957. PR_ASSERT(NULL != prp);
  958. PR_Lock(prp->lock);
  959. prp->eventbits = 0;
  960. PR_Unlock(prp->lock);
  961. }
  962. /*
  963. * Replay the actual update to the consumer. Construct an appropriate LDAP
  964. * operation, attach the baggage LDAPv3 control that contains the CSN, etc.,
  965. * and send the operation to the consumer.
  966. */
  967. ConnResult
  968. w_replay_update(Private_Repl_Protocol *prp, slapi_operation_parameters *op)
  969. {
  970. ConnResult return_value;
  971. LDAPControl *update_control = NULL;
  972. char csn_str[CSN_STRSIZE]; /* For logging only */
  973. char *old_dn;
  974. char *password = NULL;
  975. old_dn = slapi_ch_strdup(op->target_address.dn);
  976. csn_as_string(op->csn, PR_FALSE, csn_str);
  977. if ( windows_private_get_windows_replarea(prp->agmt) && windows_private_get_directory_replarea(prp->agmt) )
  978. {
  979. Slapi_DN *sdn;
  980. sdn = slapi_sdn_new_dn_passin( op->target_address.dn );
  981. sdn = map_dn_user(sdn, REPLICA_TYPE_WINDOWS, windows_private_get_windows_replarea( prp->agmt) );
  982. op->target_address.dn = slapi_ch_strdup(slapi_sdn_get_ndn(sdn));
  983. }
  984. /* Construct the replication info control that accompanies the operation */
  985. if (SLAPI_OPERATION_ADD == op->operation_type)
  986. {
  987. map_entry_user(&(op->p.p_add.target_entry), REPLICA_TYPE_WINDOWS, &password);
  988. }
  989. else if (SLAPI_OPERATION_MODRDN == op->operation_type)
  990. {
  991. /* op->p.p_modrdn.modrdn_newrdn needs to change from
  992. uid=fbar to cn=fbar */
  993. }
  994. else if (SLAPI_OPERATION_MODIFY == op->operation_type)
  995. {
  996. alter_mods(& op->p.p_modify.modify_mods, &password) ;
  997. /* if the entry already exists, modify_mods will be null.
  998. attempting to modifiy null will return LDAP_PARAM_ERROR.
  999. The password still might be set. bail */
  1000. if ( op->p.p_modify.modify_mods == NULL)
  1001. {
  1002. return_value = CONN_OPERATION_SUCCESS;
  1003. goto skipchange;
  1004. }
  1005. }
  1006. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1007. "%s: replay_update: Sending %s operation (dn=\"%s\" csn=%s)\n",
  1008. agmt_get_long_name(prp->agmt),
  1009. op2string(op->operation_type), op->target_address.dn, csn_str);
  1010. /* What type of operation is it? */
  1011. switch (op->operation_type)
  1012. {
  1013. case SLAPI_OPERATION_ADD:
  1014. {
  1015. LDAPMod **entryattrs;
  1016. /* Convert entry to mods */
  1017. (void)slapi_entry2mods (op->p.p_add.target_entry,
  1018. NULL /* &entrydn : We don't need it */,
  1019. &entryattrs);
  1020. if (NULL == entryattrs)
  1021. {
  1022. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1023. "%s: replay_update: Cannot convert entry to LDAPMods.\n",
  1024. agmt_get_long_name(prp->agmt));
  1025. return_value = CONN_LOCAL_ERROR;
  1026. }
  1027. else
  1028. {
  1029. return_value = windows_conn_send_add(prp->conn, op->target_address.dn,
  1030. entryattrs, update_control, NULL /* returned controls */);
  1031. ldap_mods_free(entryattrs, 1);
  1032. }
  1033. break;
  1034. }
  1035. case SLAPI_OPERATION_MODIFY:
  1036. return_value = windows_conn_send_modify(prp->conn, op->target_address.dn,
  1037. op->p.p_modify.modify_mods, update_control,
  1038. NULL /* returned controls */);
  1039. break;
  1040. case SLAPI_OPERATION_DELETE:
  1041. return_value = windows_conn_send_delete(prp->conn, op->target_address.dn,
  1042. update_control, NULL /* returned controls */);
  1043. break;
  1044. case SLAPI_OPERATION_MODRDN:
  1045. /* XXXggood need to pass modrdn mods in update control! */
  1046. return_value = windows_conn_send_rename(prp->conn, op->target_address.dn,
  1047. op->p.p_modrdn.modrdn_newrdn,
  1048. op->p.p_modrdn.modrdn_newsuperior_address.dn,
  1049. op->p.p_modrdn.modrdn_deloldrdn,
  1050. update_control, NULL /* returned controls */);
  1051. break;
  1052. default:
  1053. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name, "%s: replay_update: Unknown "
  1054. "operation type %d found in changelog - skipping change.\n",
  1055. agmt_get_long_name(prp->agmt), op->operation_type);
  1056. }
  1057. skipchange:
  1058. if (NULL != password)
  1059. { /* this entry had a password, handle it seperately */
  1060. /* http://support.microsoft.com/?kbid=269190 */
  1061. int pw_return;
  1062. LDAPMod *pw_mods[2];
  1063. LDAPMod pw_mod;
  1064. char *pw[2];
  1065. pw[0] = password;
  1066. pw[1] = NULL;
  1067. pw_mod.mod_type = "UnicodePwd";
  1068. pw_mod.mod_op = LDAP_MOD_REPLACE;
  1069. pw_mod.mod_values = pw;
  1070. pw_mods[0] = &pw_mod;
  1071. pw_mods[1] = NULL;
  1072. pw_return = windows_conn_send_modify(prp->conn, op->target_address.dn,
  1073. pw_mods, NULL, NULL );
  1074. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1075. "%s: replay_update: update password returned %d\n",
  1076. agmt_get_long_name(prp->agmt), pw_return );
  1077. // slapi_ch_free((void**) &password);
  1078. }
  1079. /* XXX
  1080. what else should be 'ignored' here?. and in which cases?
  1081. Or should this go in a different part of the file? */
  1082. if ( CONN_OPERATION_SUCCESS == return_value )
  1083. {
  1084. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1085. "%s: replay_update: Consumer successfully replayed operation with csn %s\n",
  1086. agmt_get_long_name(prp->agmt), csn_str);
  1087. }
  1088. else
  1089. {
  1090. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1091. "%s: replay_update: Consumer could not replay operation with csn %s\n",
  1092. agmt_get_long_name(prp->agmt), csn_str);
  1093. }
  1094. return return_value;
  1095. }
  1096. static PRBool
  1097. is_dummy_operation (const slapi_operation_parameters *op)
  1098. {
  1099. return (strcmp (op->target_address.uniqueid, START_ITERATION_ENTRY_UNIQUEID) == 0);
  1100. }
  1101. void
  1102. w_cl5_operation_parameters_done (struct slapi_operation_parameters *sop)
  1103. {
  1104. if(sop!=NULL) {
  1105. switch(sop->operation_type)
  1106. {
  1107. case SLAPI_OPERATION_BIND:
  1108. slapi_ch_free((void **)&(sop->p.p_bind.bind_saslmechanism));
  1109. if (sop->p.p_bind.bind_creds)
  1110. ber_bvecfree((struct berval**)&(sop->p.p_bind.bind_creds));
  1111. if (sop->p.p_bind.bind_ret_saslcreds)
  1112. ber_bvecfree((struct berval**)&(sop->p.p_bind.bind_ret_saslcreds));
  1113. sop->p.p_bind.bind_creds = NULL;
  1114. sop->p.p_bind.bind_ret_saslcreds = NULL;
  1115. break;
  1116. case SLAPI_OPERATION_COMPARE:
  1117. ava_done((struct ava *)&(sop->p.p_compare.compare_ava));
  1118. break;
  1119. case SLAPI_OPERATION_SEARCH:
  1120. slapi_ch_free((void **)&(sop->p.p_search.search_strfilter));
  1121. charray_free(sop->p.p_search.search_attrs);
  1122. slapi_filter_free(sop->p.p_search.search_filter,1);
  1123. break;
  1124. case SLAPI_OPERATION_MODRDN:
  1125. sop->p.p_modrdn.modrdn_deloldrdn = 0;
  1126. break;
  1127. case SLAPI_OPERATION_EXTENDED:
  1128. slapi_ch_free((void **)&(sop->p.p_extended.exop_oid));
  1129. if (sop->p.p_extended.exop_value)
  1130. ber_bvecfree((struct berval**)&(sop->p.p_extended.exop_value));
  1131. sop->p.p_extended.exop_value = NULL;
  1132. break;
  1133. default:
  1134. break;
  1135. }
  1136. }
  1137. operation_parameters_done(sop);
  1138. }
  1139. /*
  1140. * Send a set of updates to the replica. Assumes that (1) the replica
  1141. * has already been acquired, (2) that the consumer's update vector has
  1142. * been checked and (3) that it's ok to send incremental updates.
  1143. * Returns:
  1144. * UPDATE_NO_MORE_UPDATES - all updates were sent succussfully
  1145. * UPDATE_TRANSIENT_ERROR - some non-permanent error occurred. Try again later.
  1146. * UPDATE_FATAL_ERROR - some bad, permanent error occurred.
  1147. * UPDATE_SCHEDULE_WINDOW_CLOSED - the schedule window closed on us.
  1148. */
  1149. static int
  1150. send_updates(Private_Repl_Protocol *prp, RUV *remote_update_vector, PRUint32 *num_changes_sent)
  1151. {
  1152. CL5Entry entry;
  1153. slapi_operation_parameters op;
  1154. int return_value;
  1155. int rc;
  1156. CL5ReplayIterator *changelog_iterator = NULL;
  1157. *num_changes_sent = 0;
  1158. /*
  1159. * Iterate over the changelog. Retrieve each update,
  1160. * construct an appropriate LDAP operation,
  1161. * attaching the CSN, and send the change.
  1162. */
  1163. //rc = cl5CreateReplayIterator( prp, remote_update_vector, &changelog_iterator); // XXX fixme
  1164. rc = cl5CreateReplayIteratorEx( prp, remote_update_vector, &changelog_iterator, agmt_get_consumerRID(prp->agmt)); // XXX fixme
  1165. if (CL5_SUCCESS != rc)
  1166. {
  1167. switch (rc)
  1168. {
  1169. case CL5_BAD_DATA: /* invalid parameter passed to the function */
  1170. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1171. "%s: Invalid parameter passed to cl5CreateReplayIterator\n",
  1172. agmt_get_long_name(prp->agmt));
  1173. return_value = UPDATE_FATAL_ERROR;
  1174. break;
  1175. case CL5_BAD_FORMAT: /* db data has unexpected format */
  1176. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1177. "%s: Unexpected format encountered in changelog database\n",
  1178. agmt_get_long_name(prp->agmt));
  1179. return_value = UPDATE_FATAL_ERROR;
  1180. break;
  1181. case CL5_BAD_STATE: /* changelog is in an incorrect state for attempted operation */
  1182. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1183. "%s: Changelog database was in an incorrect state\n",
  1184. agmt_get_long_name(prp->agmt));
  1185. return_value = UPDATE_FATAL_ERROR;
  1186. break;
  1187. case CL5_BAD_DBVERSION: /* changelog has invalid dbversion */
  1188. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1189. "%s: Incorrect dbversion found in changelog database\n",
  1190. agmt_get_long_name(prp->agmt));
  1191. return_value = UPDATE_FATAL_ERROR;
  1192. break;
  1193. case CL5_DB_ERROR: /* database error */
  1194. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1195. "%s: A changelog database error was encountered\n",
  1196. agmt_get_long_name(prp->agmt));
  1197. return_value = UPDATE_FATAL_ERROR;
  1198. break;
  1199. case CL5_NOTFOUND: /* we have no changes to send */
  1200. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1201. "%s: No changes to send\n",
  1202. agmt_get_long_name(prp->agmt));
  1203. return_value = UPDATE_NO_MORE_UPDATES;
  1204. break;
  1205. case CL5_MEMORY_ERROR: /* memory allocation failed */
  1206. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1207. "%s: Memory allocation error occurred\n",
  1208. agmt_get_long_name(prp->agmt));
  1209. return_value = UPDATE_FATAL_ERROR;
  1210. break;
  1211. case CL5_SYSTEM_ERROR: /* NSPR error occurred: use PR_GetError for furhter info */
  1212. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1213. "%s: An NSPR error (%d) occurred\n",
  1214. agmt_get_long_name(prp->agmt), PR_GetError());
  1215. return_value = UPDATE_TRANSIENT_ERROR;
  1216. break;
  1217. case CL5_CSN_ERROR: /* CSN API failed */
  1218. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1219. "%s: A CSN API failure was encountered\n",
  1220. agmt_get_long_name(prp->agmt));
  1221. return_value = UPDATE_TRANSIENT_ERROR;
  1222. break;
  1223. case CL5_RUV_ERROR: /* RUV API failed */
  1224. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1225. "%s: An RUV API failure occurred\n",
  1226. agmt_get_long_name(prp->agmt));
  1227. return_value = UPDATE_TRANSIENT_ERROR;
  1228. break;
  1229. case CL5_OBJSET_ERROR: /* namedobjset api failed */
  1230. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1231. "%s: A namedobject API failure occurred\n",
  1232. agmt_get_long_name(prp->agmt));
  1233. return_value = UPDATE_TRANSIENT_ERROR;
  1234. break;
  1235. case CL5_PURGED_DATA: /* requested data has been purged */
  1236. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1237. "%s: Data required to update replica has been purged. "
  1238. "The replica must be reinitialized.\n",
  1239. agmt_get_long_name(prp->agmt));
  1240. return_value = UPDATE_FATAL_ERROR;
  1241. break;
  1242. case CL5_MISSING_DATA: /* data should be in the changelog, but is missing */
  1243. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1244. "%s: Missing data encountered\n",
  1245. agmt_get_long_name(prp->agmt));
  1246. return_value = UPDATE_FATAL_ERROR;
  1247. break;
  1248. case CL5_UNKNOWN_ERROR: /* unclassified error */
  1249. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1250. "%s: An unknown error was ecountered\n",
  1251. agmt_get_long_name(prp->agmt));
  1252. return_value = UPDATE_TRANSIENT_ERROR;
  1253. break;
  1254. default:
  1255. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1256. "%s: An unknown error (%d) occurred "
  1257. "(cl5CreateReplayIterator)\n",
  1258. agmt_get_long_name(prp->agmt), rc);
  1259. return_value = UPDATE_TRANSIENT_ERROR;
  1260. }
  1261. }
  1262. else
  1263. {
  1264. int finished = 0;
  1265. ConnResult replay_crc;
  1266. char csn_str[CSN_STRSIZE];
  1267. memset ( (void*)&op, 0, sizeof (op) );
  1268. entry.op = &op;
  1269. do {
  1270. w_cl5_operation_parameters_done ( entry.op );
  1271. memset ( (void*)entry.op, 0, sizeof (op) );
  1272. rc = cl5GetNextOperationToReplay(changelog_iterator, &entry);
  1273. switch (rc)
  1274. {
  1275. case CL5_SUCCESS:
  1276. /* check that we don't return dummy entries */
  1277. if (is_dummy_operation (entry.op))
  1278. {
  1279. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1280. "%s: changelog iteration code returned a dummy entry with csn %s, "
  1281. "skipping ...\n",
  1282. agmt_get_long_name(prp->agmt), csn_as_string(entry.op->csn, PR_FALSE, csn_str));
  1283. continue;
  1284. }
  1285. replay_crc = w_replay_update(prp, entry.op);
  1286. if (CONN_OPERATION_SUCCESS != replay_crc)
  1287. {
  1288. int operation, error;
  1289. conn_get_error(prp->conn, &operation, &error);
  1290. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1291. /* Figure out what to do next */
  1292. if (CONN_OPERATION_FAILED == replay_crc)
  1293. {
  1294. /* Map ldap error code to return value */
  1295. if (!ignore_error_and_keep_going(error))
  1296. {
  1297. return_value = UPDATE_TRANSIENT_ERROR;
  1298. finished = 1;
  1299. }
  1300. else
  1301. {
  1302. agmt_inc_last_update_changecount (prp->agmt, csn_get_replicaid(entry.op->csn), 1 /*skipped*/);
  1303. }
  1304. slapi_log_error(finished ? SLAPI_LOG_FATAL : slapi_log_urp, windows_repl_plugin_name,
  1305. "%s: Consumer failed to replay change (uniqueid %s, CSN %s): %s. %s.\n",
  1306. agmt_get_long_name(prp->agmt),
  1307. entry.op->target_address.uniqueid, csn_str,
  1308. ldap_err2string(error),
  1309. finished ? "Will retry later" : "Skipping");
  1310. }
  1311. else if (CONN_NOT_CONNECTED == replay_crc)
  1312. {
  1313. /* We lost the connection - enter backoff state */
  1314. return_value = UPDATE_TRANSIENT_ERROR;
  1315. finished = 1;
  1316. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1317. "%s: Consumer failed to replay change (uniqueid %s, CSN %s): "
  1318. "%s. Will retry later.\n",
  1319. agmt_get_long_name(prp->agmt),
  1320. entry.op->target_address.uniqueid, csn_str,
  1321. error ? ldap_err2string(error) : "Connection lost");
  1322. }
  1323. else if (CONN_TIMEOUT == replay_crc)
  1324. {
  1325. return_value = UPDATE_TIMEOUT;
  1326. finished = 1;
  1327. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1328. "%s: Consumer timed out to replay change (uniqueid %s, CSN %s): "
  1329. "%s.\n",
  1330. agmt_get_long_name(prp->agmt),
  1331. entry.op->target_address.uniqueid, csn_str,
  1332. error ? ldap_err2string(error) : "Timeout");
  1333. }
  1334. else if (CONN_LOCAL_ERROR == replay_crc)
  1335. {
  1336. /*
  1337. * Something bad happened on the local server - enter
  1338. * backoff state.
  1339. */
  1340. return_value = UPDATE_TRANSIENT_ERROR;
  1341. finished = 1;
  1342. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1343. "%s: Failed to replay change (uniqueid %s, CSN %s): "
  1344. "Local error. Will retry later.\n",
  1345. agmt_get_long_name(prp->agmt),
  1346. entry.op->target_address.uniqueid, csn_str);
  1347. }
  1348. }
  1349. else
  1350. {
  1351. /* Positive response received */
  1352. (*num_changes_sent)++;
  1353. agmt_inc_last_update_changecount (prp->agmt, csn_get_replicaid(entry.op->csn), 0 /*replayed*/);
  1354. }
  1355. break;
  1356. case CL5_BAD_DATA:
  1357. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1358. "%s: Invalid parameter passed to cl5GetNextOperationToReplay\n",
  1359. agmt_get_long_name(prp->agmt));
  1360. return_value = UPDATE_FATAL_ERROR;
  1361. finished = 1;
  1362. break;
  1363. case CL5_NOTFOUND:
  1364. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1365. "%s: No more updates to send (cl5GetNextOperationToReplay)\n",
  1366. agmt_get_long_name(prp->agmt));
  1367. return_value = UPDATE_NO_MORE_UPDATES;
  1368. finished = 1;
  1369. break;
  1370. case CL5_DB_ERROR:
  1371. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1372. "%s: A database error occurred (cl5GetNextOperationToReplay)\n",
  1373. agmt_get_long_name(prp->agmt));
  1374. return_value = UPDATE_FATAL_ERROR;
  1375. finished = 1;
  1376. break;
  1377. case CL5_BAD_FORMAT:
  1378. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1379. "%s: A malformed changelog entry was encountered (cl5GetNextOperationToReplay)\n",
  1380. agmt_get_long_name(prp->agmt));
  1381. break;
  1382. case CL5_MEMORY_ERROR:
  1383. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1384. "%s: A memory allocation error occurred (cl5GetNextOperationToRepla)\n",
  1385. agmt_get_long_name(prp->agmt));
  1386. return_value = UPDATE_FATAL_ERROR;
  1387. break;
  1388. default:
  1389. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1390. "%s: Unknown error code (%d) returned from cl5GetNextOperationToReplay\n",
  1391. agmt_get_long_name(prp->agmt), rc);
  1392. return_value = UPDATE_TRANSIENT_ERROR;
  1393. break;
  1394. }
  1395. /* Check for protocol shutdown */
  1396. if (prp->terminate)
  1397. {
  1398. return_value = UPDATE_NO_MORE_UPDATES;
  1399. finished = 1;
  1400. }
  1401. if (*num_changes_sent >= MAX_CHANGES_PER_SESSION)
  1402. {
  1403. return_value = UPDATE_YIELD;
  1404. finished = 1;
  1405. }
  1406. } while (!finished);
  1407. w_cl5_operation_parameters_done ( entry.op );
  1408. cl5DestroyReplayIterator(&changelog_iterator);
  1409. }
  1410. return return_value;
  1411. }
  1412. /*
  1413. * XXXggood this should probably be in the superclass, since the full update
  1414. * protocol is going to need it too.
  1415. */
  1416. static int
  1417. windows_inc_stop(Private_Repl_Protocol *prp)
  1418. {
  1419. int return_value;
  1420. PRIntervalTime start, maxwait, now;
  1421. int seconds = 1200;
  1422. maxwait = PR_SecondsToInterval(seconds);
  1423. prp->terminate = 1;
  1424. event_notify(prp, EVENT_PROTOCOL_SHUTDOWN);
  1425. start = PR_IntervalNow();
  1426. now = start;
  1427. while (!prp->stopped && ((now - start) < maxwait))
  1428. {
  1429. DS_Sleep(PR_SecondsToInterval(1));
  1430. now = PR_IntervalNow();
  1431. }
  1432. if (!prp->stopped)
  1433. {
  1434. /* Isn't listening. Do something drastic. */
  1435. return_value = -1;
  1436. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1437. "%s: windows_inc_stop: protocol does not stop after %d seconds\n",
  1438. agmt_get_long_name(prp->agmt), seconds);
  1439. }
  1440. else
  1441. {
  1442. return_value = 0;
  1443. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1444. "%s: windows_inc_stop: protocol stopped after %d seconds\n",
  1445. agmt_get_long_name(prp->agmt),
  1446. PR_IntervalToSeconds(now-start));
  1447. }
  1448. return return_value;
  1449. }
  1450. static int
  1451. windows_inc_status(Private_Repl_Protocol *prp)
  1452. {
  1453. int return_value = 0;
  1454. return return_value;
  1455. }
  1456. static void
  1457. windows_inc_notify_update(Private_Repl_Protocol *prp)
  1458. {
  1459. event_notify(prp, EVENT_TRIGGERING_CRITERIA_MET);
  1460. }
  1461. static void
  1462. windows_inc_update_now(Private_Repl_Protocol *prp)
  1463. {
  1464. event_notify(prp, EVENT_REPLICATE_NOW);
  1465. }
  1466. static void
  1467. windows_inc_notify_agmt_changed(Private_Repl_Protocol *prp)
  1468. {
  1469. event_notify(prp, EVENT_AGMT_CHANGED);
  1470. }
  1471. static void
  1472. windows_inc_notify_window_opened (Private_Repl_Protocol *prp)
  1473. {
  1474. event_notify(prp, EVENT_WINDOW_OPENED);
  1475. }
  1476. static void
  1477. windows_inc_notify_window_closed (Private_Repl_Protocol *prp)
  1478. {
  1479. event_notify(prp, EVENT_WINDOW_CLOSED);
  1480. }
  1481. Private_Repl_Protocol *
  1482. Windows_Inc_Protocol_new(Repl_Protocol *rp)
  1483. {
  1484. windows_inc_private *rip = NULL;
  1485. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)slapi_ch_malloc(sizeof(Private_Repl_Protocol));
  1486. prp->delete = windows_inc_delete;
  1487. prp->run = windows_inc_run;
  1488. prp->stop = windows_inc_stop;
  1489. prp->status = windows_inc_status;
  1490. prp->notify_update = windows_inc_notify_update;
  1491. prp->notify_agmt_changed = windows_inc_notify_agmt_changed;
  1492. prp->notify_window_opened = windows_inc_notify_window_opened;
  1493. prp->notify_window_closed = windows_inc_notify_window_closed;
  1494. prp->update_now = windows_inc_update_now;
  1495. prp->replica_object = prot_get_replica_object(rp);
  1496. if ((prp->lock = PR_NewLock()) == NULL)
  1497. {
  1498. goto loser;
  1499. }
  1500. if ((prp->cvar = PR_NewCondVar(prp->lock)) == NULL)
  1501. {
  1502. goto loser;
  1503. }
  1504. prp->stopped = 0;
  1505. prp->terminate = 0;
  1506. prp->eventbits = 0;
  1507. prp->conn = prot_get_connection(rp);
  1508. prp->agmt = prot_get_agreement(rp);
  1509. prp->last_acquire_response_code = NSDS50_REPL_REPLICA_READY;
  1510. rip = (void *)slapi_ch_malloc(sizeof(windows_inc_private));
  1511. rip->ruv = NULL;
  1512. rip->backoff = NULL;
  1513. rip->rp = rp;
  1514. prp->private = (void *)rip;
  1515. prp->replica_acquired = PR_FALSE;
  1516. return prp;
  1517. loser:
  1518. windows_inc_delete(&prp);
  1519. return NULL;
  1520. }
  1521. static void
  1522. windows_inc_backoff_expired(time_t timer_fire_time, void *arg)
  1523. {
  1524. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)arg;
  1525. PR_ASSERT(NULL != prp);
  1526. event_notify(prp, EVENT_BACKOFF_EXPIRED);
  1527. }
  1528. /*
  1529. * Examine the update vector and determine our course of action.
  1530. * There are 3 different possibilities, plus a catch-all error:
  1531. * 1 - no update vector (ruv is NULL). The consumer's replica is
  1532. * pristine, so it needs to be initialized. Return
  1533. * EXAMINE_RUV_PRISTINE_REPLICA.
  1534. * 2 - ruv is present, but its database generation ID doesn't
  1535. * match the local generation ID. This means that either
  1536. * the local replica must be reinitialized from the remote
  1537. * replica or vice-versa. Return
  1538. * EXAMINE_RUV_GENERATION_MISMATCH.
  1539. * 3 - ruv is present, and we have all updates needed to bring
  1540. * the replica up to date using the incremental protocol.
  1541. * return EXAMINE_RUV_OK.
  1542. * 4 - parameter error. Return EXAMINE_RUV_PARAM_ERROR
  1543. */
  1544. static int
  1545. windows_examine_update_vector(Private_Repl_Protocol *prp, RUV *remote_ruv)
  1546. {
  1547. int return_value;
  1548. PR_ASSERT(NULL != prp);
  1549. if (NULL == prp)
  1550. {
  1551. return_value = EXAMINE_RUV_PARAM_ERROR;
  1552. }
  1553. else if (NULL == remote_ruv)
  1554. {
  1555. return_value = EXAMINE_RUV_PRISTINE_REPLICA;
  1556. }
  1557. else
  1558. {
  1559. char *local_gen = NULL;
  1560. char *remote_gen = ruv_get_replica_generation(remote_ruv);
  1561. Object *local_ruv_obj;
  1562. RUV *local_ruv;
  1563. Replica *replica;
  1564. PR_ASSERT(NULL != prp->replica_object);
  1565. replica = object_get_data(prp->replica_object);
  1566. PR_ASSERT(NULL != replica);
  1567. local_ruv_obj = replica_get_ruv (replica);
  1568. if (NULL != local_ruv_obj)
  1569. {
  1570. local_ruv = (RUV*) object_get_data (local_ruv_obj);
  1571. PR_ASSERT (local_ruv);
  1572. local_gen = ruv_get_replica_generation(local_ruv);
  1573. object_release (local_ruv_obj);
  1574. }
  1575. if (NULL == remote_gen || NULL == local_gen || strcmp(remote_gen, local_gen) != 0)
  1576. {
  1577. return_value = EXAMINE_RUV_GENERATION_MISMATCH;
  1578. }
  1579. else
  1580. {
  1581. return_value = EXAMINE_RUV_OK;
  1582. }
  1583. slapi_ch_free((void**)&remote_gen);
  1584. slapi_ch_free((void**)&local_gen);
  1585. }
  1586. return return_value;
  1587. }
  1588. /*
  1589. * When we get an error from an LDAP operation, we call this
  1590. * function to decide if we should just keep replaying
  1591. * updates, or if we should stop, back off, and try again
  1592. * later.
  1593. * Returns PR_TRUE if we shoould keep going, PR_FALSE if
  1594. * we should back off and try again later.
  1595. *
  1596. * In general, we keep going if the return code is consistent
  1597. * with some sort of bug in URP that causes the consumer to
  1598. * emit an error code that it shouldn't have, e.g. LDAP_ALREADY_EXISTS.
  1599. *
  1600. * We stop if there's some indication that the server just completely
  1601. * failed to process the operation, e.g. LDAP_OPERATIONS_ERROR.
  1602. */
  1603. static PRBool
  1604. ignore_error_and_keep_going(int error)
  1605. {
  1606. int return_value;
  1607. switch (error)
  1608. {
  1609. /* Cases where we keep going */
  1610. case LDAP_SUCCESS:
  1611. case LDAP_NO_SUCH_ATTRIBUTE:
  1612. case LDAP_UNDEFINED_TYPE:
  1613. case LDAP_CONSTRAINT_VIOLATION:
  1614. case LDAP_TYPE_OR_VALUE_EXISTS:
  1615. case LDAP_INVALID_SYNTAX:
  1616. case LDAP_NO_SUCH_OBJECT:
  1617. case LDAP_INVALID_DN_SYNTAX:
  1618. case LDAP_IS_LEAF:
  1619. case LDAP_INSUFFICIENT_ACCESS:
  1620. case LDAP_NAMING_VIOLATION:
  1621. case LDAP_OBJECT_CLASS_VIOLATION:
  1622. case LDAP_NOT_ALLOWED_ON_NONLEAF:
  1623. case LDAP_NOT_ALLOWED_ON_RDN:
  1624. case LDAP_ALREADY_EXISTS:
  1625. case LDAP_NO_OBJECT_CLASS_MODS:
  1626. return_value = PR_TRUE;
  1627. break;
  1628. /* Cases where we stop and retry */
  1629. case LDAP_OPERATIONS_ERROR:
  1630. case LDAP_PROTOCOL_ERROR:
  1631. case LDAP_TIMELIMIT_EXCEEDED:
  1632. case LDAP_SIZELIMIT_EXCEEDED:
  1633. case LDAP_STRONG_AUTH_NOT_SUPPORTED:
  1634. case LDAP_STRONG_AUTH_REQUIRED:
  1635. case LDAP_PARTIAL_RESULTS:
  1636. case LDAP_REFERRAL:
  1637. case LDAP_ADMINLIMIT_EXCEEDED:
  1638. case LDAP_UNAVAILABLE_CRITICAL_EXTENSION:
  1639. case LDAP_CONFIDENTIALITY_REQUIRED:
  1640. case LDAP_SASL_BIND_IN_PROGRESS:
  1641. case LDAP_INAPPROPRIATE_MATCHING:
  1642. case LDAP_ALIAS_PROBLEM:
  1643. case LDAP_ALIAS_DEREF_PROBLEM:
  1644. case LDAP_INAPPROPRIATE_AUTH:
  1645. case LDAP_INVALID_CREDENTIALS:
  1646. case LDAP_BUSY:
  1647. case LDAP_UNAVAILABLE:
  1648. case LDAP_UNWILLING_TO_PERFORM:
  1649. case LDAP_LOOP_DETECT:
  1650. case LDAP_SORT_CONTROL_MISSING:
  1651. case LDAP_INDEX_RANGE_ERROR:
  1652. case LDAP_RESULTS_TOO_LARGE:
  1653. case LDAP_AFFECTS_MULTIPLE_DSAS:
  1654. case LDAP_OTHER:
  1655. case LDAP_SERVER_DOWN:
  1656. case LDAP_LOCAL_ERROR:
  1657. case LDAP_ENCODING_ERROR:
  1658. case LDAP_DECODING_ERROR:
  1659. case LDAP_TIMEOUT:
  1660. case LDAP_AUTH_UNKNOWN:
  1661. case LDAP_FILTER_ERROR:
  1662. case LDAP_USER_CANCELLED:
  1663. case LDAP_PARAM_ERROR:
  1664. case LDAP_NO_MEMORY:
  1665. case LDAP_CONNECT_ERROR:
  1666. case LDAP_NOT_SUPPORTED:
  1667. case LDAP_CONTROL_NOT_FOUND:
  1668. case LDAP_NO_RESULTS_RETURNED:
  1669. case LDAP_MORE_RESULTS_TO_RETURN:
  1670. case LDAP_CLIENT_LOOP:
  1671. case LDAP_REFERRAL_LIMIT_EXCEEDED:
  1672. return_value = PR_FALSE;
  1673. break;
  1674. }
  1675. return return_value;
  1676. }
  1677. /* this function converts a state to its name - for debug output */
  1678. static const char*
  1679. state2name (int state)
  1680. {
  1681. switch (state)
  1682. {
  1683. case STATE_START: return "start";
  1684. case STATE_WAIT_WINDOW_OPEN: return "wait_for_window_to_open";
  1685. case STATE_WAIT_CHANGES: return "wait_for_changes";
  1686. case STATE_READY_TO_ACQUIRE: return "ready_to_acquire_replica";
  1687. case STATE_BACKOFF_START: return "start_backoff";
  1688. case STATE_BACKOFF: return "backoff";
  1689. case STATE_SENDING_UPDATES: return "sending_updates";
  1690. case STATE_STOP_FATAL_ERROR: return "stop_fatal_error";
  1691. case STATE_STOP_FATAL_ERROR_PART2: return "stop_fatal_error";
  1692. case STATE_STOP_NORMAL_TERMINATION: return "stop_normal_termination";
  1693. default: return "invalid_state";
  1694. }
  1695. }
  1696. /* this function convert s an event to its name - for debug output */
  1697. static const char*
  1698. event2name (int event)
  1699. {
  1700. switch (event)
  1701. {
  1702. case EVENT_WINDOW_OPENED: return "update_window_opened";
  1703. case EVENT_WINDOW_CLOSED: return "update_window_closed";
  1704. case EVENT_TRIGGERING_CRITERIA_MET: return "data_modified";
  1705. case EVENT_BACKOFF_EXPIRED: return "backoff_timer_expired";
  1706. case EVENT_REPLICATE_NOW: return "replicate_now";
  1707. case EVENT_PROTOCOL_SHUTDOWN: return "protocol_shutdown";
  1708. case EVENT_AGMT_CHANGED: return "agreement_changed";
  1709. default: return "invalid_event";
  1710. }
  1711. }
  1712. static const char*
  1713. op2string(int op)
  1714. {
  1715. switch (op) {
  1716. case SLAPI_OPERATION_ADD:
  1717. return "add";
  1718. case SLAPI_OPERATION_MODIFY:
  1719. return "modify";
  1720. case SLAPI_OPERATION_DELETE:
  1721. return "delete";
  1722. case SLAPI_OPERATION_MODRDN:
  1723. return "rename";
  1724. case SLAPI_OPERATION_EXTENDED:
  1725. return "extended";
  1726. }
  1727. return "unknown";
  1728. }
  1729. void map_entry_user(Slapi_Entry **e, int map_to, char** password) {
  1730. /* do schema changes */
  1731. Slapi_Entry *new_entry;
  1732. Slapi_Attr *a;
  1733. Slapi_Attr **attr = &a;
  1734. Slapi_Attr *prevattr = NULL;
  1735. int rc;
  1736. char* ldif = ""; /* for debugging only */
  1737. int ldif_len=0; /* for debugging only */
  1738. a = slapi_attr_new();
  1739. new_entry = slapi_entry_alloc();
  1740. rc =slapi_entry_first_attr( *e, attr );
  1741. while (rc == 0)
  1742. {
  1743. char* attr_type;
  1744. Slapi_Value *old_value;
  1745. int i=-1;
  1746. int ret = 0;
  1747. slapi_attr_get_type(*attr, &attr_type);
  1748. while ( mapping[++i].old_name != NULL)
  1749. {
  1750. if ((mapping[i].map_to == map_to) && strcasecmp(attr_type, mapping[i].old_name) == 0 )
  1751. {
  1752. slapi_attr_first_value( *attr, &old_value);
  1753. slapi_entry_add_value (new_entry, mapping[i].new_name, old_value);
  1754. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1755. "translate_attrib \n\t%s\n\t%s\n", mapping[i].old_name, mapping[i].new_name);
  1756. }
  1757. }
  1758. if ((map_to == REPLICA_TYPE_WINDOWS) && (
  1759. slapi_attr_type_cmp(attr_type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD , 1) == 0))
  1760. {
  1761. int b;
  1762. const char* pw;
  1763. pw = slapi_value_get_string(old_value);
  1764. *password = (char*) slapi_ch_malloc(strlen( pw ) + 3); /* "%s"\0 */
  1765. b = sprintf(*password, "\"%s\"", pw);
  1766. }
  1767. if (mapping[i].old_name != NULL) {
  1768. slapi_attr_first_value( *attr, &old_value);
  1769. ret = slapi_entry_add_value (new_entry, mapping[i].new_name, old_value);
  1770. }
  1771. prevattr = *attr;
  1772. rc = slapi_entry_next_attr( *e, prevattr, attr );
  1773. }
  1774. /* replace the existing entry with our duplicate */
  1775. {
  1776. char *ad_objectclass[] = {"top", "person", "organizationalperson", "user", NULL};
  1777. char *ds_objectclass[] = {"top", "person", "organizationalperson", "inetorgperson", "ntuser", NULL};
  1778. Slapi_ValueSet *vs = slapi_valueset_new();
  1779. Slapi_Value * addval;
  1780. int j = -1;
  1781. if (map_to == REPLICA_TYPE_WINDOWS) {
  1782. while (ad_objectclass[++j]!= NULL)
  1783. {
  1784. addval = slapi_value_new_string(ad_objectclass[j]);
  1785. slapi_valueset_add_value(vs,addval);
  1786. slapi_value_free(&addval);
  1787. }
  1788. }
  1789. else
  1790. {
  1791. while (ds_objectclass[++j]!= NULL)
  1792. {
  1793. addval = slapi_value_new_string(ds_objectclass[j]);
  1794. slapi_valueset_add_value(vs,addval);
  1795. slapi_value_free(&addval);
  1796. }
  1797. }
  1798. slapi_entry_add_valueset(new_entry,"objectclass", vs);
  1799. slapi_valueset_free(vs);
  1800. }
  1801. /*
  1802. ldif = slapi_entry2str( *e, &ldif_len );
  1803. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1804. "found entry %s from windows:\n%s\n", slapi_entry_get_ndn(*e), ldif);
  1805. ldif = slapi_entry2str( new_entry, &ldif_len );
  1806. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1807. "found entry %s from windows:\n%s\n", slapi_entry_get_ndn(new_entry), ldif);
  1808. slapi_ch_free_string(&ldif);
  1809. */
  1810. *e = slapi_entry_dup( new_entry );
  1811. }
  1812. void map_entry_group(Slapi_Entry **e, int map_to) {
  1813. /* do schema changes */
  1814. Slapi_Entry *new_entry;
  1815. Slapi_Attr *a;
  1816. Slapi_Attr **attr = &a;
  1817. Slapi_Attr *prevattr = NULL;
  1818. int rc;
  1819. char* ldif = ""; /* for debugging only */
  1820. int ldif_len=0; /* for debugging only */
  1821. a = slapi_attr_new();
  1822. new_entry = slapi_entry_alloc();
  1823. rc =slapi_entry_first_attr( *e, attr );
  1824. while (rc == 0)
  1825. {
  1826. char* attr_type;
  1827. Slapi_Value *old_value;
  1828. int i=-1;
  1829. int ret = 0;
  1830. slapi_attr_get_type(*attr, &attr_type);
  1831. while ( group_map[++i].old_name != NULL)
  1832. {
  1833. if ((group_map[i].map_to == map_to) && strcasecmp(attr_type, group_map[i].old_name) == 0 )
  1834. {
  1835. slapi_attr_first_value( *attr, &old_value);
  1836. slapi_entry_add_value (new_entry, group_map[i].new_name, old_value);
  1837. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1838. "translate_attrib \n\t%s\n\t%s\n", group_map[i].old_name, group_map[i].new_name);
  1839. }
  1840. }
  1841. if (group_map[i].old_name != NULL) {
  1842. slapi_attr_first_value( *attr, &old_value);
  1843. ret = slapi_entry_add_value (new_entry, group_map[i].new_name, old_value);
  1844. }
  1845. prevattr = *attr;
  1846. rc = slapi_entry_next_attr( *e, prevattr, attr );
  1847. }
  1848. /* replace the existing entry with our duplicate */
  1849. {
  1850. char *ad_objectclass[] = {"top", "group", NULL};
  1851. char *ds_objectclass[] = {"top", "ntGroup", NULL};
  1852. Slapi_ValueSet *vs = slapi_valueset_new();
  1853. Slapi_Value * addval;
  1854. int j = -1;
  1855. if (map_to == REPLICA_TYPE_WINDOWS) {
  1856. while (ad_objectclass[++j]!= NULL)
  1857. {
  1858. addval = slapi_value_new_string(ad_objectclass[j]);
  1859. slapi_valueset_add_value(vs,addval);
  1860. slapi_value_free(&addval);
  1861. }
  1862. }
  1863. else
  1864. {
  1865. while (ds_objectclass[++j]!= NULL)
  1866. {
  1867. addval = slapi_value_new_string(ds_objectclass[j]);
  1868. slapi_valueset_add_value(vs,addval);
  1869. slapi_value_free(&addval);
  1870. }
  1871. }
  1872. slapi_entry_add_valueset(new_entry,"objectclass", vs);
  1873. slapi_valueset_free(vs);
  1874. }
  1875. ldif = slapi_entry2str( *e, &ldif_len );
  1876. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1877. "found entry %s from windows:\n%s\n", slapi_entry_get_ndn(*e), ldif);
  1878. ldif = slapi_entry2str( new_entry, &ldif_len );
  1879. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1880. "found entry %s from windows:\n%s\n", slapi_entry_get_ndn(new_entry), ldif);
  1881. slapi_ch_free_string(&ldif);
  1882. *e = slapi_entry_dup( new_entry );
  1883. }
  1884. static Slapi_DN* map_dn_user(Slapi_DN *sdn, int map_to, const Slapi_DN *root)
  1885. {
  1886. /* function operation
  1887. convert to rdn
  1888. pop off naming attribute of sdn "{ cn | uid }"
  1889. replace with other naming attribute "{ uid | cn }"
  1890. append root
  1891. return sdn by reference
  1892. */
  1893. Slapi_RDN *old_rdn = NULL;
  1894. Slapi_RDN *new_rdn = NULL;
  1895. char* naming_type = "";
  1896. char* naming_value= "";
  1897. Slapi_DN *return_sdn = NULL;
  1898. int rc = 0;
  1899. char* tempdn;
  1900. PR_ASSERT(sdn);
  1901. PR_ASSERT(root);
  1902. old_rdn = slapi_rdn_new_sdn(sdn); /* cn=administrator */
  1903. new_rdn = slapi_rdn_new();
  1904. rc = slapi_rdn_get_first(old_rdn, &naming_type, &naming_value); /* cn administrator */
  1905. if (map_to == REPLICA_TYPE_MULTIMASTER)
  1906. naming_type = slapi_ch_strdup("uid"); /* uid administrator */
  1907. else if (map_to == REPLICA_TYPE_WINDOWS)
  1908. naming_type = slapi_ch_strdup("cn");
  1909. rc = slapi_rdn_add(new_rdn, naming_type, naming_value); /* uid=administrator */
  1910. /* XXX this should be done with SDN and not char* */
  1911. tempdn = (char*) slapi_ch_malloc( strlen (naming_type) + strlen(naming_value) +
  1912. slapi_sdn_get_ndn_len(root) + 4);
  1913. sprintf(tempdn, "%s=%s, %s", naming_type, naming_value, slapi_sdn_get_ndn(root) );
  1914. return_sdn = slapi_sdn_new_dn_byref(tempdn);
  1915. return return_sdn;
  1916. }
  1917. static Slapi_DN* map_dn_group(Slapi_DN *sdn, int map_to, const Slapi_DN *root)
  1918. {
  1919. /* function operation
  1920. convert to rdn
  1921. append root
  1922. return sdn by reference
  1923. */
  1924. Slapi_RDN *old_rdn = NULL;
  1925. Slapi_RDN *new_rdn = NULL;
  1926. char* naming_type = "";
  1927. char* naming_value= "";
  1928. Slapi_DN *return_sdn = NULL;
  1929. int rc = 0;
  1930. char* tempdn;
  1931. PR_ASSERT(sdn);
  1932. PR_ASSERT(root);
  1933. old_rdn = slapi_rdn_new_sdn(sdn); /* cn=administrator */
  1934. new_rdn = slapi_rdn_new();
  1935. rc = slapi_rdn_get_first(old_rdn, &naming_type, &naming_value); /* cn administrator */
  1936. rc = slapi_rdn_add(new_rdn, naming_type, naming_value);
  1937. /* XXX this should be done with SDN and not char* */
  1938. tempdn = (char*) slapi_ch_malloc( strlen (naming_type) + strlen(naming_value) +
  1939. slapi_sdn_get_ndn_len(root) + 4);
  1940. sprintf(tempdn, "%s=%s, %s", naming_type, naming_value, slapi_sdn_get_ndn(root) );
  1941. return_sdn = slapi_sdn_new_dn_passin(tempdn);
  1942. return return_sdn;
  1943. }
  1944. static void alter_mods(LDAPMod ***m, char** password)
  1945. {
  1946. LDAPMod **mods = *m;
  1947. int i=0;
  1948. while (NULL != mods && NULL != mods[i])
  1949. {
  1950. int j=-1; /* index of the attribute mapping array */
  1951. char* attr_type;
  1952. attr_type = slapi_ch_strdup(mods[i]->mod_type);
  1953. while ( mapping[++j].old_name != NULL)
  1954. {
  1955. if ( (mapping[j].map_to == REPLICA_TYPE_WINDOWS) &&
  1956. (slapi_attr_type_cmp(attr_type, mapping[j].old_name, 1) == 0 ) )
  1957. {
  1958. mods[i]->mod_type = slapi_ch_strdup(mapping[j].new_name);
  1959. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1960. "alter_mods \n\t%s\n\t%s\n", mapping[j].old_name, mapping[j].new_name);
  1961. break;
  1962. }
  1963. }
  1964. if (slapi_attr_type_cmp(attr_type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD , 1) == 0)
  1965. {
  1966. int b;
  1967. char *pw;
  1968. if (mods[i]->mod_op | LDAP_MOD_BVALUES)
  1969. {
  1970. *password = (char*) slapi_ch_malloc(strlen( mods[i]->mod_bvalues[0]->bv_val) + 3); /* "%s"\0 */
  1971. pw = mods[i]->mod_bvalues[0]->bv_val;
  1972. }
  1973. else
  1974. {
  1975. *password = (char*) slapi_ch_malloc(strlen( mods[i]->mod_values[0]) + 3); /* "%s"\0 */
  1976. pw = mods[i]->mod_values[0];
  1977. }
  1978. b = sprintf(*password, "\"%s\"", pw);
  1979. }
  1980. if (mapping[j].old_name == NULL)
  1981. {
  1982. LDAPMod *this_mod = mods[i];
  1983. /* Move down all subsequent mods */
  1984. int k = 0;
  1985. for (k = i; mods[k+1] ; k++)
  1986. {
  1987. mods[k] = mods[k+1];
  1988. }
  1989. /* Zero the end of the array */
  1990. mods[k] = NULL;
  1991. /* Adjust value of j, implicit in not incrementing it */
  1992. /* Free this mod */
  1993. ber_bvecfree(this_mod->mod_bvalues);
  1994. slapi_ch_free((void **)&(this_mod->mod_type));
  1995. slapi_ch_free((void **)&this_mod);
  1996. }
  1997. else
  1998. i++;
  1999. }
  2000. }
  2001. /* the entry has already been translated, so be sure to search for ntuserid
  2002. and not samaccountname or anything else. */
  2003. static Slapi_Entry* windows_entry_already_exists(Slapi_Entry *e){
  2004. int rc = 0;
  2005. Slapi_DN *sdn = NULL;
  2006. Slapi_Entry *entry = NULL;
  2007. sdn = slapi_entry_get_sdn(e);
  2008. rc = slapi_search_internal_get_entry( sdn, NULL, &entry, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  2009. if (rc == LDAP_SUCCESS)
  2010. {
  2011. return entry;
  2012. }
  2013. else
  2014. {
  2015. return NULL;
  2016. }
  2017. }
  2018. static PRBool entry_is_tombstone(Slapi_Entry *e){
  2019. return PR_FALSE;
  2020. }
  2021. static int delete_user(Slapi_Entry *e){
  2022. const char* dn;
  2023. const char* new_dn;
  2024. Slapi_PBlock *pb = NULL;
  2025. Slapi_DN *sdn = NULL;
  2026. int return_value = 0;
  2027. PR_ASSERT(e);
  2028. dn = slapi_ch_strdup( slapi_entry_get_ndn(e) );
  2029. new_dn = slapi_ch_strdup(dn);
  2030. /* XXX dn parsing in this if block could use some improvement */
  2031. if ( strstr(new_dn, "\ndel:") != NULL )
  2032. {
  2033. char* comma;
  2034. const char* c = ",";
  2035. pb = slapi_pblock_new();
  2036. comma = strstr(dn, c);
  2037. strcpy( strstr(new_dn, "\ndel:"), comma);
  2038. slapi_delete_internal_set_pb(pb, new_dn, NULL, NULL, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  2039. slapi_delete_internal_pb(pb);
  2040. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  2041. slapi_pblock_destroy(pb);
  2042. }
  2043. else
  2044. {
  2045. return_value = -1;
  2046. }
  2047. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2048. "delete operation of entry %s returned: %d\n", new_dn, return_value);
  2049. slapi_ch_free((void**) &dn);
  2050. slapi_ch_free((void**) &new_dn);
  2051. return return_value;
  2052. }
  2053. static void make_mods_from_entries(Slapi_Entry *new_entry, Slapi_Entry *existing_entry, LDAPMod ***attrs){
  2054. Slapi_Attr *attr;
  2055. int rc = 0;
  2056. Slapi_Mods smods;
  2057. PR_ASSERT (new_entry && attrs && existing_entry);
  2058. slapi_mods_init (&smods, 0);
  2059. rc =slapi_entry_first_attr( new_entry, &attr );
  2060. while (rc == 0)
  2061. {
  2062. char* attr_type;
  2063. Slapi_Attr *existing_attr;
  2064. int i=-1;
  2065. Slapi_Value *v1 = NULL;
  2066. Slapi_Value *v2 = NULL;
  2067. slapi_attr_get_type(attr, &attr_type);
  2068. slapi_attr_first_value( attr, &v1);
  2069. if (slapi_entry_attr_find(existing_entry, attr_type, &existing_attr) == 0)
  2070. {
  2071. /* found */
  2072. slapi_attr_first_value( existing_attr, &v2);
  2073. if (slapi_value_compare(existing_attr, v1, v2) == 0)
  2074. {
  2075. /* same, do nothing */
  2076. /* slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2077. "make_mods_from_entries: %s has same value (%s) on both sides\n", attr_type, slapi_value_get_string(v1)); */
  2078. }
  2079. else
  2080. {
  2081. /* attributes different -- create CHANGE */
  2082. /* slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2083. "make_mods_from_entries: %s has different value (%s, %s)\n",
  2084. attr_type, slapi_value_get_string(v1),slapi_value_get_string(v2)); */
  2085. slapi_mods_add_mod_values(&smods, LDAP_MOD_REPLACE, attr_type, &v1 );
  2086. }
  2087. }
  2088. else
  2089. {
  2090. /* not found -- create ADD */
  2091. /*slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2092. "make_mods_from_entries: %s has no value ( will be %s)\n",
  2093. attr_type, slapi_value_get_string(v1)); */
  2094. slapi_mods_add_mod_values(&smods, LDAP_MOD_ADD, attr_type, &v1 );
  2095. }
  2096. /* XXX TODO:
  2097. delete an attribute */
  2098. rc = slapi_entry_next_attr(new_entry, attr, &attr);
  2099. }
  2100. *attrs = slapi_mods_get_ldapmods_passout (&smods);
  2101. slapi_mods_done (&smods);
  2102. }
  2103. int add_or_modify_user(Slapi_Entry *e){
  2104. Slapi_PBlock *pb = NULL;
  2105. int return_value = 0;
  2106. char *dn;
  2107. Slapi_Entry *existing = NULL;
  2108. pb = slapi_pblock_new();
  2109. dn = slapi_ch_strdup( slapi_entry_get_ndn(e) );
  2110. existing = windows_entry_already_exists(e);
  2111. if ( existing == NULL)
  2112. {
  2113. slapi_add_entry_internal_set_pb(pb, e, NULL,
  2114. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  2115. 0);
  2116. slapi_add_internal_pb(pb);
  2117. }
  2118. else
  2119. {
  2120. LDAPMod **mods;
  2121. make_mods_from_entries(e, existing, &mods);
  2122. if (NULL == mods)
  2123. { /* also get null if there are no differences, so this is ok. */
  2124. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  2125. "add_or_modify_user: Cannot convert entry %s to LDAPMods.\n", dn );
  2126. return_value = 0;
  2127. goto bail;
  2128. }
  2129. slapi_modify_internal_set_pb (pb, slapi_entry_get_ndn(e), mods, NULL, NULL,
  2130. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2131. slapi_modify_internal_pb (pb);
  2132. ldap_mods_free(mods, 1);
  2133. }
  2134. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  2135. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2136. "operation of entry %s returned: %d\n", dn, return_value);
  2137. bail:
  2138. if (pb)
  2139. slapi_pblock_destroy(pb);
  2140. return return_value;
  2141. }
  2142. int add_or_modify_group(Slapi_Entry *e){
  2143. Slapi_PBlock *pb = NULL;
  2144. int return_value = 0;
  2145. char *dn;
  2146. Slapi_Entry *existing = NULL;
  2147. pb = slapi_pblock_new();
  2148. dn = slapi_ch_strdup( slapi_entry_get_ndn(e) );
  2149. existing = windows_entry_already_exists(e);
  2150. if ( existing == NULL)
  2151. {
  2152. slapi_add_entry_internal_set_pb(pb, e, NULL,
  2153. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  2154. 0);
  2155. slapi_add_internal_pb(pb);
  2156. }
  2157. else
  2158. {
  2159. LDAPMod **mods;
  2160. make_mods_from_entries(e, existing, &mods);
  2161. if (NULL == mods)
  2162. { /* also get null if there are no differences, so this is ok. */
  2163. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  2164. "add_or_modify_user: Cannot convert entry %s to LDAPMods.\n", dn );
  2165. return_value = 0;
  2166. goto bail;
  2167. }
  2168. slapi_modify_internal_set_pb (pb, slapi_entry_get_ndn(e), mods, NULL, NULL,
  2169. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2170. slapi_modify_internal_pb (pb);
  2171. ldap_mods_free(mods, 1);
  2172. }
  2173. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  2174. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2175. "operation of entry %s returned: %d\n", dn, return_value);
  2176. bail:
  2177. if (pb)
  2178. slapi_pblock_destroy(pb);
  2179. return return_value;
  2180. }
  2181. void windows_dirsync_inc_run(Private_Repl_Protocol *prp)
  2182. {
  2183. int rc;
  2184. int msgid=0;
  2185. Slapi_PBlock *pb = NULL;
  2186. Slapi_Filter *filter_user = NULL;
  2187. Slapi_Filter *filter_user_deleted = NULL;
  2188. Slapi_Filter *filter_group = NULL;
  2189. Slapi_Filter *filter_group_deleted = NULL;
  2190. rc = perform_search(prp->conn);
  2191. if (rc == CONN_OPERATION_SUCCESS)
  2192. {
  2193. Slapi_Entry *e;
  2194. int filter_ret = 0;
  2195. #define FILTER_USER "(objectclass=user)"
  2196. #define FILTER_GROUP "(objectclass=group)"
  2197. #define FILTER_USER_DELETED "(&(isdeleted=*)(objectclass=user))"
  2198. #define FILTER_GROUP_DELETED "(&(isdeleted=*)(objectclass=group))"
  2199. filter_user = slapi_str2filter( slapi_ch_strdup( FILTER_USER ) );
  2200. filter_user_deleted = slapi_str2filter( slapi_ch_strdup( FILTER_USER_DELETED ) );
  2201. filter_group = slapi_str2filter( slapi_ch_strdup( FILTER_GROUP ) );
  2202. filter_group_deleted = slapi_str2filter( slapi_ch_strdup( FILTER_GROUP_DELETED ) );
  2203. while ((e = windows_conn_get_search_result(prp->conn)) != NULL)
  2204. {
  2205. const Slapi_DN* sdn = NULL;
  2206. /* deleted users are outside the 'correct container'.
  2207. They live in cn=deleted objects, windows_private_get_directory_replarea( prp->agmt) */
  2208. if ( slapi_filter_test( pb, e, filter_user_deleted, 0 ) == 0 )
  2209. {
  2210. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2211. "entry %s marked for deletion\n", slapi_entry_get_ndn(e));
  2212. sdn = map_dn_user(slapi_entry_get_sdn(e),REPLICA_TYPE_MULTIMASTER, windows_private_get_directory_replarea( prp->agmt) );
  2213. slapi_entry_set_sdn(e, sdn);
  2214. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2215. "entry %s marked for deletion\n", slapi_entry_get_ndn(e));
  2216. delete_user(e); /* CN is "garbled" if the entry is deleted */
  2217. }
  2218. else if ( slapi_filter_test( pb, e, filter_group_deleted, 0 ) == 0 )
  2219. {
  2220. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2221. "group %s marked for deletion\n", slapi_entry_get_ndn(e));
  2222. sdn = map_dn_group(slapi_entry_get_sdn(e),REPLICA_TYPE_MULTIMASTER, windows_private_get_directory_replarea( prp->agmt) );
  2223. slapi_entry_set_sdn(e, sdn);
  2224. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2225. "entry %s marked for deletion\n", slapi_entry_get_ndn(e));
  2226. delete_user(e); /* CN is "garbled" if the entry is deleted */
  2227. }
  2228. /* must be in the previously specified container to be replicated, lest it is ignored */
  2229. else if (slapi_sdn_isparent(windows_private_get_windows_replarea( prp->agmt ), slapi_entry_get_sdn(e)) )
  2230. {
  2231. if (0 == slapi_filter_test( pb, e, filter_user, 0))
  2232. {
  2233. sdn = map_dn_user(slapi_entry_get_sdn(e),REPLICA_TYPE_MULTIMASTER, windows_private_get_directory_replarea( prp->agmt) );
  2234. map_entry_user(&e, REPLICA_TYPE_MULTIMASTER, NULL);
  2235. slapi_entry_set_sdn(e, sdn);
  2236. /* if (windows_private_create_users(prp->ra)) */
  2237. add_or_modify_user(e);
  2238. }
  2239. else if (0 == slapi_filter_test( pb, e, filter_group, 0))
  2240. {
  2241. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2242. "group %s marked for addition\n", slapi_entry_get_ndn(e));
  2243. sdn = map_dn_group(slapi_entry_get_sdn(e),REPLICA_TYPE_MULTIMASTER, windows_private_get_directory_replarea( prp->agmt) );
  2244. slapi_entry_set_sdn(e, sdn);
  2245. map_entry_group(&e, REPLICA_TYPE_MULTIMASTER);
  2246. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2247. "group %s marked for addition\n", slapi_entry_get_ndn(e));
  2248. add_or_modify_group(e);
  2249. }
  2250. else
  2251. {
  2252. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2253. "entry %s in %s ignored\n", slapi_entry_get_ndn(e), slapi_sdn_get_dn(windows_private_get_directory_replarea( prp->agmt)) );
  2254. }
  2255. /* inside container */
  2256. }
  2257. else
  2258. {
  2259. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2260. "entry %s ignored\n", slapi_entry_get_ndn(e));
  2261. }
  2262. }
  2263. }
  2264. }