windows_protocol_util.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608
  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_protocol_util.c */
  7. /*
  8. Code common to both incremental and total protocols.
  9. */
  10. #include "repl5.h"
  11. #include "repl5_prot_private.h"
  12. #include "windowsrepl.h"
  13. #include "slap.h"
  14. #include <unicode/ustring.h> /* UTF8 conversion */
  15. int ruv_private_new( RUV **ruv, RUV *clone );
  16. static Slapi_Entry* windows_entry_already_exists(Slapi_Entry *e);
  17. static void windows_dirsync_now (Private_Repl_Protocol *prp);
  18. static Slapi_DN* map_dn_user(Slapi_DN *sdn, int map_to, const Slapi_DN *root);
  19. static Slapi_DN* map_dn_group(Slapi_DN *sdn, int map_to, const Slapi_DN *root);
  20. static void make_mods_from_entries(Slapi_Entry *new_entry, Slapi_Entry *existing_entry, LDAPMod ***attrs);
  21. static void windows_map_mods_for_replay(Private_Repl_Protocol *prp,LDAPMod **original_mods, LDAPMod ***returned_mods, int is_user, char** password);
  22. static int is_subject_of_agreemeent_local(const Slapi_Entry *local_entry,const Repl_Agmt *ra);
  23. static int windows_create_remote_entry(Private_Repl_Protocol *prp,Slapi_Entry *original_entry, Slapi_DN *remote_sdn, Slapi_Entry **remote_entry, char** password);
  24. static int windows_get_local_entry(const Slapi_DN* local_dn,Slapi_Entry **local_entry);
  25. static int windows_get_local_entry_by_uniqueid(Private_Repl_Protocol *prp,const char* uniqueid,Slapi_Entry **local_entry);
  26. static int map_entry_dn_outbound(Slapi_Entry *e, const Slapi_DN **dn, Private_Repl_Protocol *prp, int *missing_entry, int want_guid);
  27. static char* extract_ntuserdomainid_from_entry(Slapi_Entry *e);
  28. static int windows_get_remote_entry (Private_Repl_Protocol *prp, Slapi_DN* remote_dn,Slapi_Entry **remote_entry);
  29. static const char* op2string (int op);
  30. static int is_subject_of_agreemeent_remote(Slapi_Entry *e, const Repl_Agmt *ra);
  31. static int map_entry_dn_inbound(Slapi_Entry *e, const Slapi_DN **dn, const Repl_Agmt *ra);
  32. static int windows_update_remote_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,Slapi_Entry *local_entry);
  33. /* Controls the direction of flow for mapped attributes */
  34. typedef enum mapping_types {
  35. bidirectional,
  36. fromwindowsonly,
  37. towindowsonly,
  38. disabled
  39. } mapping_types;
  40. /* Controls if we sync the attibute always, or only when we're creating new entries */
  41. /* Used for attributes like samaccountname, where we want to fill it in on a new entry, but
  42. * we never want to change it on an existing entry */
  43. typedef enum creation_types {
  44. always,
  45. createonly
  46. } creation_types;
  47. typedef enum attr_types {
  48. normal,
  49. dnmap
  50. } attr_types;
  51. typedef struct _windows_attribute_map
  52. {
  53. char *windows_attribute_name;
  54. char *ldap_attribute_name;
  55. mapping_types map_type;
  56. creation_types create_type;
  57. attr_types attr_type;
  58. } windows_attribute_map;
  59. /* List of attributes that are common to AD and LDAP, so we simply copy them over in both directions */
  60. static char* windows_user_matching_attributes[] =
  61. {
  62. "description",
  63. "destinationIndicator",
  64. "facsimileTelephoneNumber",
  65. "givenName",
  66. "homePhone",
  67. "homePostalAddress",
  68. "initials",
  69. "l",
  70. "mail",
  71. "mobile",
  72. "o",
  73. "ou",
  74. "pager",
  75. "physicalDeliveryOfficeName",
  76. "postOfficeBox",
  77. "postalAddress",
  78. "postalCode",
  79. "registeredAddress",
  80. "seeAlso",
  81. "sn",
  82. "st",
  83. "street",
  84. "telephoneNumber",
  85. "teletexTerminalIdentifier",
  86. "telexNumber",
  87. "title",
  88. "userCertificate",
  89. "x121Address",
  90. NULL
  91. };
  92. static char* windows_group_matching_attributes[] =
  93. {
  94. "description",
  95. "destinationIndicator",
  96. "facsimileTelephoneNumber",
  97. "givenName",
  98. "homePhone",
  99. "homePostalAddress",
  100. "initials",
  101. "l",
  102. "mail",
  103. "manager",
  104. "mobile",
  105. "o",
  106. "ou",
  107. "pager",
  108. "physicalDeliveryOfficeName",
  109. "postOfficeBox",
  110. "postalAddress",
  111. "postalCode",
  112. "preferredDeliveryMethod",
  113. "registeredAddress",
  114. "seeAlso",
  115. "sn",
  116. "st",
  117. "street",
  118. "telephoneNumber",
  119. "teletexTerminalIdentifier",
  120. "telexNumber",
  121. "title",
  122. "userCertificate",
  123. "x121Address",
  124. NULL
  125. };
  126. static windows_attribute_map user_attribute_map[] =
  127. {
  128. { "homeDirectory", "ntUserHomeDir", bidirectional, always, normal},
  129. { "scriptPath", "ntUserScriptPath", bidirectional, always, normal},
  130. { "lastLogon", "ntUserLastLogon", fromwindowsonly, always, normal},
  131. { "lastLogoff", "ntUserLastLogoff", fromwindowsonly, always, normal},
  132. { "accountExpires", "ntUserAcctExpires", bidirectional, always, normal},
  133. { "codePage", "ntUserCodePage", bidirectional, always, normal},
  134. { "logonHours", "ntUserLogonHours", bidirectional, always, normal},
  135. { "maxStorage", "ntUserMaxStorage", bidirectional, always, normal},
  136. { "profilePath", "ntUserParms", bidirectional, always, normal},
  137. { "userParameters", "ntUserProfile", bidirectional, always, normal},
  138. { "userWorkstations", "ntUserWorkstations", bidirectional, always, normal},
  139. { "sAMAccountName", "ntUserDomainId", bidirectional, createonly, normal},
  140. /* cn is a naming attribute in AD, so we don't want to change it after entry creation */
  141. { "cn", "cn", towindowsonly, createonly, normal},
  142. /* However, it isn't a naming attribute in DS (we use uid) so it's safe to accept changes inbound */
  143. { "name", "cn", fromwindowsonly, always, normal},
  144. { "manager", "manager", bidirectional, always, dnmap},
  145. { "secretary", "secretary", bidirectional, always, dnmap},
  146. { "seealso", "seealso", bidirectional, always, dnmap},
  147. {NULL, NULL, -1}
  148. };
  149. static windows_attribute_map group_attribute_map[] =
  150. {
  151. { "groupType", "ntGroupType", bidirectional, createonly, normal},
  152. { "sAMAccountName", "ntUserDomainId", bidirectional, createonly, normal},
  153. { "member", "uniquemember", bidirectional, always, dnmap},
  154. {NULL, NULL, -1}
  155. };
  156. /*
  157. * Notes on differences for NT4:
  158. * 1. NT4 returns the SID value in the objectGUID attribute value.
  159. * The SID has variable length and does not match the length of a GUID.
  160. * 2. NT4 currently never generates tombstones. If it did, we'd need to parse the
  161. * different form of the GUID in the tombstone DNs.
  162. * 3. NT4 Does not implement the dirsync control. We always get all users and groups.
  163. * 4. NT4 generates and expects DNs with samaccountname as the RDN, not cn.
  164. * 5. NT4 handles the DN=<GUID> (remember that the '<' '>' characters are included!) DN form
  165. * for modifies and deletes, provided we use the value it gave us in the objectGUID attribute (which is actually the SID).
  166. * 6. NT4 has less and different schema from AD. For example users in NT4 have no firstname/lastname, only an optional 'description'.
  167. */
  168. static const char*
  169. op2string(int op)
  170. {
  171. LDAPDebug( LDAP_DEBUG_TRACE, "=> op2string\n", 0, 0, 0 );
  172. LDAPDebug( LDAP_DEBUG_TRACE, "<= op2string\n", 0, 0, 0 );
  173. switch (op) {
  174. case SLAPI_OPERATION_ADD:
  175. return "add";
  176. case SLAPI_OPERATION_MODIFY:
  177. return "modify";
  178. case SLAPI_OPERATION_DELETE:
  179. return "delete";
  180. case SLAPI_OPERATION_MODRDN:
  181. return "rename";
  182. case SLAPI_OPERATION_EXTENDED:
  183. return "extended";
  184. }
  185. return "unknown";
  186. }
  187. static void
  188. windows_dump_entry(const char *string, Slapi_Entry *e)
  189. {
  190. int length = 0;
  191. char *buffer = NULL;
  192. if (slapi_is_loglevel_set(SLAPI_LOG_REPL))
  193. {
  194. buffer = slapi_entry2str(e,&length);
  195. slapi_log_error(SLAPI_LOG_REPL, NULL, "Windows sync entry: %s %s\n", string, buffer);
  196. if (buffer)
  197. {
  198. slapi_ch_free((void**)&buffer);
  199. }
  200. }
  201. }
  202. static void
  203. map_dn_values(Private_Repl_Protocol *prp,Slapi_ValueSet *original_values, Slapi_ValueSet **mapped_values, int to_windows)
  204. {
  205. Slapi_ValueSet *new_vs = NULL;
  206. Slapi_Value *original_value = NULL;
  207. int retval = 0;
  208. int i = 0;
  209. /* For each value: */
  210. i= slapi_valueset_first_value(original_values,&original_value);
  211. while ( i != -1 ) {
  212. int is_ours = 0;
  213. char *new_dn_string = NULL;
  214. const char *original_dn_string = NULL;
  215. int original_dn_string_length = 0;
  216. Slapi_DN *original_dn = NULL;
  217. original_dn_string = slapi_value_get_string(original_value);
  218. /* Sanity check the data was a valid string */
  219. original_dn_string_length = slapi_value_get_length(original_value);
  220. /* Make a sdn from the string */
  221. original_dn = slapi_sdn_new_dn_byref(original_dn_string);
  222. if (to_windows)
  223. {
  224. Slapi_Entry *local_entry = NULL;
  225. /* Try to get the local entry */
  226. retval = windows_get_local_entry(original_dn,&local_entry);
  227. if (0 == retval && local_entry)
  228. {
  229. int missing_entry = 0;
  230. Slapi_DN *remote_dn = NULL;
  231. /* Now map the DN */
  232. is_ours = is_subject_of_agreemeent_local(local_entry,prp->agmt);
  233. if (is_ours)
  234. {
  235. map_entry_dn_outbound(local_entry,&remote_dn,prp,&missing_entry, 0 /* don't want GUID form here */);
  236. if (remote_dn)
  237. {
  238. if (!missing_entry)
  239. {
  240. /* Success */
  241. new_dn_string = slapi_ch_strdup(slapi_sdn_get_dn(remote_dn));
  242. }
  243. slapi_sdn_free(&remote_dn);
  244. }
  245. }
  246. } else {
  247. slapi_log_error(SLAPI_LOG_REPL, NULL, "map_dn_values: no local entry found for %s\n", original_dn_string);
  248. }
  249. if (local_entry)
  250. {
  251. slapi_entry_free(local_entry);
  252. local_entry = NULL;
  253. }
  254. } else
  255. {
  256. Slapi_Entry *remote_entry = NULL;
  257. Slapi_DN *local_dn = NULL;
  258. /* Try to get the remote entry */
  259. retval = windows_get_remote_entry(prp,original_dn,&remote_entry);
  260. is_ours = is_subject_of_agreemeent_remote(remote_entry,prp->agmt);
  261. if (is_ours)
  262. {
  263. retval = map_entry_dn_inbound(remote_entry,&local_dn,prp->agmt);
  264. if (0 == retval && local_dn)
  265. {
  266. new_dn_string = slapi_ch_strdup(slapi_sdn_get_dn(local_dn));
  267. slapi_sdn_free(&local_dn);
  268. } else
  269. {
  270. slapi_log_error(SLAPI_LOG_REPL, NULL, "map_dn_values: no remote entry found for %s\n", original_dn_string);
  271. }
  272. }
  273. if (remote_entry)
  274. {
  275. slapi_entry_free(remote_entry);
  276. remote_entry = NULL;
  277. }
  278. }
  279. /* Extract the dn string and store in the new value */
  280. if (new_dn_string)
  281. {
  282. Slapi_Value *new_value = NULL;
  283. if (NULL == new_vs)
  284. {
  285. new_vs = slapi_valueset_new();
  286. }
  287. new_value = slapi_value_new_string_passin(new_dn_string);
  288. slapi_valueset_add_value(new_vs,new_value);
  289. slapi_value_free(&new_value);
  290. }
  291. /* If not then we skip it */
  292. i = slapi_valueset_next_value(original_values,i,&original_value);
  293. if (original_dn)
  294. {
  295. slapi_sdn_free(&original_dn);
  296. }
  297. }/* while */
  298. if (new_vs)
  299. {
  300. *mapped_values = new_vs;
  301. }
  302. }
  303. /*
  304. * Acquire exclusive access to a replica. Send a start replication extended
  305. * operation to the replica. The response will contain a success code, and
  306. * optionally the replica's update vector if acquisition is successful.
  307. * This function returns one of the following:
  308. * ACQUIRE_SUCCESS - the replica was acquired, and we have exclusive update access
  309. * ACQUIRE_REPLICA_BUSY - another master was updating the replica
  310. * ACQUIRE_FATAL_ERROR - something bad happened, and it's not likely to improve
  311. * if we wait.
  312. * ACQUIRE_TRANSIENT_ERROR - something bad happened, but it's probably worth
  313. * another try after waiting a while.
  314. * If ACQUIRE_SUCCESS is returned, then ruv will point to the replica's update
  315. * vector. It's possible that the replica does something goofy and doesn't
  316. * return us an update vector, so be prepared for ruv to be NULL (but this is
  317. * an error).
  318. */
  319. int
  320. windows_acquire_replica(Private_Repl_Protocol *prp, RUV **ruv, int check_ruv)
  321. {
  322. int return_value = ACQUIRE_SUCCESS;
  323. ConnResult crc;
  324. Repl_Connection *conn;
  325. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_acquire_replica\n", 0, 0, 0 );
  326. PR_ASSERT(prp);
  327. if (prp->replica_acquired) /* we already acquire replica */
  328. {
  329. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  330. "%s: Remote replica already acquired\n",
  331. agmt_get_long_name(prp->agmt));
  332. return_value = ACQUIRE_FATAL_ERROR;
  333. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_acquire_replica\n", 0, 0, 0 );
  334. return ACQUIRE_SUCCESS;
  335. }
  336. {
  337. Replica *replica;
  338. Object *supl_ruv_obj, *cons_ruv_obj;
  339. PRBool is_newer = PR_FALSE;
  340. RUV *r;
  341. if (prp->agmt)
  342. {
  343. cons_ruv_obj = agmt_get_consumer_ruv(prp->agmt);
  344. }
  345. object_acquire(prp->replica_object);
  346. replica = object_get_data(prp->replica_object);
  347. supl_ruv_obj = replica_get_ruv ( replica );
  348. /* make a copy of the existing RUV as a starting point
  349. XXX this is probably a not-so-elegant hack */
  350. slapi_log_error(SLAPI_LOG_REPL, NULL, "acquire_replica, supplier RUV:\n");
  351. if (supl_ruv_obj) {
  352. object_acquire(supl_ruv_obj);
  353. ruv_dump ((RUV*) object_get_data ( supl_ruv_obj ), "supplier", NULL);
  354. object_release(supl_ruv_obj);
  355. }else
  356. slapi_log_error(SLAPI_LOG_REPL, NULL, "acquire_replica, supplier RUV = null\n");
  357. slapi_log_error(SLAPI_LOG_REPL, NULL, "acquire_replica, consumer RUV:\n");
  358. if (cons_ruv_obj)
  359. {
  360. RUV* con;
  361. object_acquire(cons_ruv_obj);
  362. con = (RUV*) object_get_data ( cons_ruv_obj );
  363. ruv_dump (con,"consumer", NULL);
  364. object_release( cons_ruv_obj );
  365. } else {
  366. slapi_log_error(SLAPI_LOG_REPL, NULL, "acquire_replica, consumer RUV = null\n");
  367. }
  368. is_newer = ruv_is_newer ( supl_ruv_obj, cons_ruv_obj );
  369. /* This follows ruv_is_newer, since it's always newer if it's null */
  370. if (cons_ruv_obj == NULL)
  371. {
  372. RUV *s;
  373. s = (RUV*) object_get_data ( replica_get_ruv ( replica ) );
  374. agmt_set_consumer_ruv(prp->agmt, s );
  375. object_release ( replica_get_ruv ( replica ) );
  376. cons_ruv_obj = agmt_get_consumer_ruv(prp->agmt);
  377. }
  378. r = (RUV*) object_get_data ( cons_ruv_obj);
  379. *ruv = r;
  380. if ( supl_ruv_obj ) object_release ( supl_ruv_obj );
  381. if ( cons_ruv_obj ) object_release ( cons_ruv_obj );
  382. object_release (prp->replica_object);
  383. replica = NULL;
  384. if (is_newer == PR_FALSE && check_ruv) {
  385. prp->last_acquire_response_code = NSDS50_REPL_UPTODATE;
  386. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_acquire_replica - ACQUIRE_CONSUMER_WAS_UPTODATE\n", 0, 0, 0 );
  387. return ACQUIRE_CONSUMER_WAS_UPTODATE;
  388. }
  389. }
  390. prp->last_acquire_response_code = NSDS50_REPL_REPLICA_NO_RESPONSE;
  391. /* Get the connection */
  392. conn = prp->conn;
  393. crc = windows_conn_connect(conn);
  394. if (CONN_OPERATION_FAILED == crc)
  395. {
  396. return_value = ACQUIRE_TRANSIENT_ERROR;
  397. }
  398. else if (CONN_SSL_NOT_ENABLED == crc)
  399. {
  400. return_value = ACQUIRE_FATAL_ERROR;
  401. }
  402. else
  403. {
  404. /* we don't want the timer to go off in the middle of an operation */
  405. windows_conn_cancel_linger(conn);
  406. /* Does the remote replica support the dirsync protocol?
  407. if it update the conn object */
  408. windows_conn_replica_supports_dirsync(conn);
  409. if (CONN_NOT_CONNECTED == crc || CONN_OPERATION_FAILED == crc)
  410. {
  411. /* We don't know anything about the remote replica. Try again later. */
  412. return_value = ACQUIRE_TRANSIENT_ERROR;
  413. }
  414. else
  415. {
  416. /* Good to go. Start the protocol. */
  417. CSN *current_csn = NULL;
  418. Slapi_DN *replarea_sdn;
  419. /* Obtain a current CSN */
  420. replarea_sdn = agmt_get_replarea(prp->agmt);
  421. current_csn = get_current_csn(replarea_sdn);
  422. if (NULL != current_csn)
  423. {
  424. return_value = ACQUIRE_SUCCESS;
  425. }
  426. else
  427. {
  428. /* Couldn't get a current CSN */
  429. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  430. "%s: Unable to obtain current CSN. "
  431. "Replication is aborting.\n",
  432. agmt_get_long_name(prp->agmt));
  433. return_value = ACQUIRE_FATAL_ERROR;
  434. }
  435. slapi_sdn_free(&replarea_sdn);
  436. }
  437. }
  438. if (ACQUIRE_SUCCESS != return_value)
  439. {
  440. /* could not acquire the replica, so reinstate the linger timer, since this
  441. means we won't call release_replica, which also reinstates the timer */
  442. windows_conn_start_linger(conn);
  443. }
  444. else
  445. {
  446. /* replica successfully acquired */
  447. prp->replica_acquired = PR_TRUE;
  448. }
  449. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_acquire_replica\n", 0, 0, 0 );
  450. return return_value;
  451. }
  452. void
  453. windows_release_replica(Private_Repl_Protocol *prp)
  454. {
  455. struct berval *retdata = NULL;
  456. char *retoid = NULL;
  457. struct berval *payload = NULL;
  458. Slapi_DN *replarea_sdn = NULL;
  459. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_release_replica\n", 0, 0, 0 );
  460. PR_ASSERT(NULL != prp);
  461. PR_ASSERT(NULL != prp->conn);
  462. if (!prp->replica_acquired)
  463. return;
  464. windows_conn_start_linger(prp->conn);
  465. prp->replica_acquired = PR_FALSE;
  466. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_release_replica\n", 0, 0, 0 );
  467. }
  468. /* this entry had a password, handle it seperately */
  469. /* http://support.microsoft.com/?kbid=269190 */
  470. static int
  471. send_password_modify(Slapi_DN *sdn, char *password, Private_Repl_Protocol *prp)
  472. {
  473. ConnResult pw_return = 0;
  474. LDAPMod *pw_mods[2];
  475. LDAPMod pw_mod;
  476. struct berval bv = {0};
  477. UChar *unicode_password = NULL;
  478. int32_t unicode_password_length = 0; /* Length in _characters_ */
  479. int32_t buffer_size = 0; /* Size in _characters_ */
  480. UErrorCode error = U_ZERO_ERROR;
  481. char *quoted_password = NULL;
  482. struct berval *bvals[2];
  483. /* AD wants the password in quotes ! */
  484. quoted_password = PR_smprintf("\"%s\"",password);
  485. if (quoted_password)
  486. {
  487. /* Need to UNICODE encode the password here */
  488. /* It's one of those 'ask me first and I will tell you the buffer size' functions */
  489. u_strFromUTF8(NULL, 0, &unicode_password_length, quoted_password, strlen(quoted_password), &error);
  490. buffer_size = unicode_password_length;
  491. unicode_password = (UChar *)slapi_ch_malloc(unicode_password_length * sizeof(UChar));
  492. if (unicode_password) {
  493. error = U_ZERO_ERROR;
  494. u_strFromUTF8(unicode_password, buffer_size, &unicode_password_length, quoted_password, strlen(quoted_password), &error);
  495. bv.bv_len = unicode_password_length * sizeof(UChar);
  496. bv.bv_val = (char*)unicode_password;
  497. bvals[0] = &bv;
  498. bvals[1] = NULL;
  499. pw_mod.mod_type = "UnicodePwd";
  500. pw_mod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  501. pw_mod.mod_bvalues = bvals;
  502. pw_mods[0] = &pw_mod;
  503. pw_mods[1] = NULL;
  504. pw_return = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(sdn), pw_mods, NULL, NULL );
  505. slapi_ch_free((void**)&unicode_password);
  506. }
  507. PR_smprintf_free(quoted_password);
  508. }
  509. return pw_return;
  510. }
  511. static int
  512. windows_entry_has_attr_and_value(Slapi_Entry *e, const char *attrname, char *value)
  513. {
  514. int retval = 0;
  515. Slapi_Attr *attr = 0;
  516. if (!e || !attrname)
  517. return retval;
  518. /* see if the entry has the specified attribute name */
  519. if (!slapi_entry_attr_find(e, attrname, &attr) && attr)
  520. {
  521. /* if value is not null, see if the attribute has that
  522. value */
  523. if (!value)
  524. {
  525. retval = 1;
  526. }
  527. else
  528. {
  529. Slapi_Value *v = 0;
  530. int index = 0;
  531. for (index = slapi_attr_first_value(attr, &v);
  532. v && (index != -1);
  533. index = slapi_attr_next_value(attr, index, &v))
  534. {
  535. const char *s = slapi_value_get_string(v);
  536. if (!s)
  537. continue;
  538. if (!strcasecmp(s, value))
  539. {
  540. retval = 1;
  541. break;
  542. }
  543. }
  544. }
  545. }
  546. return retval;
  547. }
  548. static void
  549. windows_is_local_entry_user_or_group(Slapi_Entry *e, int *is_user, int *is_group)
  550. {
  551. *is_user = windows_entry_has_attr_and_value(e,"objectclass","ntuser");
  552. *is_group = windows_entry_has_attr_and_value(e,"objectclass","ntgroup");
  553. }
  554. static void
  555. windows_is_remote_entry_user_or_group(Slapi_Entry *e, int *is_user, int *is_group)
  556. {
  557. *is_user = windows_entry_has_attr_and_value(e,"objectclass","person");
  558. *is_group = windows_entry_has_attr_and_value(e,"objectclass","group");
  559. }
  560. static int
  561. add_remote_entry_allowed(Slapi_Entry *e)
  562. {
  563. /* We say yes if the entry has the ntUserCreateNewAccount attribute set in the case of a user, or the ntGroupDeleteGroup
  564. * attribute set in the case of a group
  565. */
  566. /* Is this a user or a group ? */
  567. int is_user = 0;
  568. int is_group = 0;
  569. char *delete_attr = NULL;
  570. windows_is_local_entry_user_or_group(e,&is_user,&is_group);
  571. if (!is_user && !is_group)
  572. {
  573. /* Neither fish nor foul.. */
  574. return -1;
  575. }
  576. if (is_user && is_group)
  577. {
  578. /* Now that's just really strange... */
  579. return -1;
  580. }
  581. if (is_user)
  582. {
  583. delete_attr = "ntUserCreateNewAccount";
  584. } else
  585. {
  586. delete_attr = "ntGroupCreateNewGroup";
  587. }
  588. /* Now test if the attribute value is set */
  589. return windows_entry_has_attr_and_value(e,delete_attr,"true");
  590. }
  591. static int
  592. delete_remote_entry_allowed(Slapi_Entry *e)
  593. {
  594. /* We say yes if the entry has the ntUserDeleteAccount attribute set in the case of a user, or the ntGroupDeleteGroup
  595. * attribute set in the case of a group
  596. */
  597. /* Is this a user or a group ? */
  598. int is_user = 0;
  599. int is_group = 0;
  600. char *delete_attr = NULL;
  601. windows_is_local_entry_user_or_group(e,&is_user,&is_group);
  602. if (!is_user && !is_group)
  603. {
  604. /* Neither fish nor foul.. */
  605. return -1;
  606. }
  607. if (is_user && is_group)
  608. {
  609. /* Now that's just really strange... */
  610. return -1;
  611. }
  612. if (is_user)
  613. {
  614. delete_attr = "ntUserDeleteAccount";
  615. } else
  616. {
  617. delete_attr = "ntGroupDeleteGroup";
  618. }
  619. /* Now test if the attribute value is set */
  620. return windows_entry_has_attr_and_value(e,delete_attr,"true");
  621. }
  622. static void
  623. windows_log_add_entry_remote(const Slapi_DN *local_dn,const Slapi_DN *remote_dn)
  624. {
  625. const char* local_dn_string = slapi_sdn_get_dn(local_dn);
  626. const char* remote_dn_string = slapi_sdn_get_dn(remote_dn);
  627. slapi_log_error(SLAPI_LOG_REPL, NULL, "Attempting to add entry %s to AD for local entry %s\n",remote_dn_string,local_dn_string);
  628. }
  629. static ConnResult
  630. process_replay_add(Private_Repl_Protocol *prp, slapi_operation_parameters *op, Slapi_Entry *local_entry, Slapi_DN *local_dn, Slapi_DN *remote_dn, int is_user, int missing_entry, char **password)
  631. {
  632. int remote_add_allowed = add_remote_entry_allowed(local_entry);
  633. ConnResult return_value = 0;
  634. int rc = 0;
  635. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  636. "%s: process_replay_add: dn=\"%s\" (%s,%s)\n",
  637. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(remote_dn), missing_entry ? "not present" : "already present" , remote_add_allowed ? "add allowed" : "add not allowed");
  638. if (missing_entry)
  639. {
  640. if (remote_add_allowed) {
  641. LDAPMod **entryattrs = NULL;
  642. Slapi_Entry *mapped_entry = NULL;
  643. /* First map the entry */
  644. rc = windows_create_remote_entry(prp,op->p.p_add.target_entry, remote_dn, &mapped_entry, password);
  645. /* Convert entry to mods */
  646. if (0 == rc && mapped_entry)
  647. {
  648. (void)slapi_entry2mods (mapped_entry , NULL /* &entrydn : We don't need it */, &entryattrs);
  649. slapi_entry_free(mapped_entry);
  650. mapped_entry = NULL;
  651. if (NULL == entryattrs)
  652. {
  653. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,"%s: windows_replay_update: Cannot convert entry to LDAPMods.\n",agmt_get_long_name(prp->agmt));
  654. return_value = CONN_LOCAL_ERROR;
  655. }
  656. else
  657. {
  658. windows_log_add_entry_remote(local_dn, remote_dn);
  659. return_value = windows_conn_send_add(prp->conn, slapi_sdn_get_dn(remote_dn), entryattrs, NULL, NULL);
  660. /* It's possible that the entry already exists in AD, in which case we fall back to modify it */
  661. if (return_value)
  662. {
  663. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,"%s: windows_replay_update: Cannot replay add operation.\n",agmt_get_long_name(prp->agmt));
  664. }
  665. ldap_mods_free(entryattrs, 1);
  666. entryattrs = NULL;
  667. }
  668. } else
  669. {
  670. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  671. "%s: process_replay_add: failed to create mapped entry dn=\"%s\"\n",agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(remote_dn));
  672. }
  673. }
  674. } else
  675. {
  676. Slapi_Entry *remote_entry = NULL;
  677. /* Fetch the remote entry */
  678. rc = windows_get_remote_entry(prp, remote_dn,&remote_entry);
  679. if (0 == rc && remote_entry) {
  680. return_value = windows_update_remote_entry(prp,remote_entry,local_entry);
  681. }
  682. if (remote_entry)
  683. {
  684. slapi_entry_free(remote_entry);
  685. }
  686. }
  687. return return_value;
  688. }
  689. /*
  690. * Given a changelog entry, construct the appropriate LDAP operations to sync
  691. * the operation to AD.
  692. */
  693. ConnResult
  694. windows_replay_update(Private_Repl_Protocol *prp, slapi_operation_parameters *op)
  695. {
  696. ConnResult return_value = 0;
  697. LDAPControl *update_control = NULL; /* No controls used for AD */
  698. int rc = 0;
  699. char *password = NULL;
  700. int is_ours = 0;
  701. int is_user = 0;
  702. int is_group = 0;
  703. Slapi_DN *remote_dn = NULL;
  704. Slapi_DN *local_dn = NULL;
  705. Slapi_Entry *local_entry = NULL;
  706. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_replay_update\n", 0, 0, 0 );
  707. local_dn = slapi_sdn_new_dn_byref( op->target_address.dn );
  708. /* Since we have the target uniqueid in the op structure, let's
  709. * fetch the local entry here using it.
  710. */
  711. rc = windows_get_local_entry_by_uniqueid(prp, op->target_address.uniqueid, &local_entry);
  712. if (rc)
  713. {
  714. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  715. "%s: windows_replay_update: failed to fetch local entry for %s operation dn=\"%s\"\n",
  716. agmt_get_long_name(prp->agmt),
  717. op2string(op->operation_type), op->target_address.dn);
  718. goto error;
  719. }
  720. is_ours = is_subject_of_agreemeent_local(local_entry, prp->agmt);
  721. windows_is_local_entry_user_or_group(local_entry,&is_user,&is_group);
  722. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  723. "%s: windows_replay_update: Looking at %s operation local dn=\"%s\" (%s,%s,%s)\n",
  724. agmt_get_long_name(prp->agmt),
  725. op2string(op->operation_type), op->target_address.dn, is_ours ? "ours" : "not ours",
  726. is_user ? "user" : "not user", is_group ? "group" : "not group");
  727. if (is_ours && (is_user || is_group) ) {
  728. int missing_entry = 0;
  729. /* Make the entry's DN */
  730. rc = map_entry_dn_outbound(local_entry,&remote_dn,prp,&missing_entry, 1);
  731. if (rc || NULL == remote_dn)
  732. {
  733. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  734. "%s: windows_replay_update: failed map dn for %s operation dn=\"%s\"\n",
  735. agmt_get_long_name(prp->agmt),
  736. op2string(op->operation_type), op->target_address.dn);
  737. goto error;
  738. }
  739. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  740. "%s: windows_replay_update: Processing %s operation local dn=\"%s\" remote dn=\"%s\"\n",
  741. agmt_get_long_name(prp->agmt),
  742. op2string(op->operation_type), op->target_address.dn, slapi_sdn_get_dn(remote_dn));
  743. switch (op->operation_type) {
  744. /* For an ADD operation, we map the entry and then send the operation, which may fail if the peer entry already existed */
  745. case SLAPI_OPERATION_ADD:
  746. return_value = process_replay_add(prp,op,local_entry,local_dn,remote_dn,is_user,missing_entry,&password);
  747. break;
  748. case SLAPI_OPERATION_MODIFY:
  749. {
  750. LDAPMod **mapped_mods = NULL;
  751. windows_map_mods_for_replay(prp,op->p.p_modify.modify_mods, &mapped_mods, is_user, &password);
  752. /* It's possible that the mapping process results in an empty mod list, in which case we don't bother with the replay */
  753. if ( mapped_mods == NULL || *(mapped_mods)== NULL )
  754. {
  755. return_value = CONN_OPERATION_SUCCESS;
  756. } else
  757. {
  758. return_value = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(remote_dn), mapped_mods, update_control,NULL /* returned controls */);
  759. }
  760. if (mapped_mods)
  761. {
  762. ldap_mods_free(mapped_mods,1);
  763. mapped_mods = NULL;
  764. }
  765. }
  766. break;
  767. case SLAPI_OPERATION_DELETE:
  768. if (delete_remote_entry_allowed(local_entry))
  769. {
  770. return_value = windows_conn_send_delete(prp->conn, slapi_sdn_get_dn(remote_dn), update_control, NULL /* returned controls */);
  771. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  772. "%s: windows_replay_update: deleted remote entry, dn=\"%s\", result=%d\n",
  773. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(remote_dn), return_value);
  774. } else
  775. {
  776. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  777. "%s: windows_replay_update: delete not allowed on remote entry, dn=\"%s\"\n",
  778. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(remote_dn));
  779. }
  780. break;
  781. case SLAPI_OPERATION_MODRDN:
  782. return_value = windows_conn_send_rename(prp->conn, op->target_address.dn,
  783. op->p.p_modrdn.modrdn_newrdn,
  784. op->p.p_modrdn.modrdn_newsuperior_address.dn,
  785. op->p.p_modrdn.modrdn_deloldrdn,
  786. update_control, NULL /* returned controls */);
  787. break;
  788. default:
  789. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name, "%s: replay_update: Unknown "
  790. "operation type %d found in changelog - skipping change.\n",
  791. agmt_get_long_name(prp->agmt), op->operation_type);
  792. }
  793. if (password)
  794. {
  795. return_value = send_password_modify(remote_dn, password, prp);
  796. if (return_value)
  797. {
  798. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name, "%s: windows_replay_update: update password returned %d\n",
  799. agmt_get_long_name(prp->agmt), return_value );
  800. }
  801. }
  802. } else {
  803. /* We ignore operations that target entries outside of our sync'ed subtree, or which are not Windows users or groups */
  804. }
  805. error:
  806. if (local_entry)
  807. {
  808. slapi_entry_free(local_entry);
  809. }
  810. if (local_dn)
  811. {
  812. slapi_sdn_free (&local_dn);
  813. }
  814. if (remote_dn)
  815. {
  816. slapi_sdn_free(&remote_dn);
  817. }
  818. return return_value;
  819. }
  820. static int
  821. is_straight_mapped_attr(const char *type, int is_user /* or group */)
  822. {
  823. int found = 0;
  824. size_t offset = 0;
  825. char *this_attr = NULL;
  826. char **list = is_user ? windows_user_matching_attributes : windows_group_matching_attributes;
  827. /* Look for the type in the list of straight mapped attrs for the appropriate object type */
  828. while (this_attr = list[offset])
  829. {
  830. if (0 == slapi_attr_type_cmp(this_attr, type, SLAPI_TYPE_CMP_SUBTYPE))
  831. {
  832. found = 1;
  833. break;
  834. }
  835. offset++;
  836. }
  837. return found;
  838. }
  839. static void
  840. windows_map_attr_name(const char *original_type , int to_windows, int is_user, int is_create, char **mapped_type, int *map_dn)
  841. {
  842. char *new_type = NULL;
  843. windows_attribute_map *our_map = is_user ? user_attribute_map : group_attribute_map;
  844. windows_attribute_map *this_map = NULL;
  845. size_t offset = 0;
  846. *mapped_type = NULL;
  847. /* Iterate over the map entries looking for the type we have */
  848. while(this_map = &(our_map[offset]))
  849. {
  850. char *their_name = to_windows ? this_map->windows_attribute_name : this_map->ldap_attribute_name;
  851. char *our_name = to_windows ? this_map->ldap_attribute_name : this_map->windows_attribute_name;
  852. if (NULL == their_name)
  853. {
  854. /* End of the list */
  855. break;
  856. }
  857. if (0 == slapi_attr_type_cmp(original_type, our_name, SLAPI_TYPE_CMP_SUBTYPE))
  858. {
  859. if (!is_create && (this_map->create_type == createonly))
  860. {
  861. /* Skip create-only entries if we're not creating */
  862. } else
  863. {
  864. if ( (this_map->map_type == towindowsonly && to_windows) || (this_map->map_type == fromwindowsonly && !to_windows)
  865. || (this_map->map_type == bidirectional) )
  866. {
  867. new_type = slapi_ch_strdup(their_name);
  868. *map_dn = (this_map->attr_type == dnmap);
  869. break;
  870. }
  871. }
  872. }
  873. offset++;
  874. }
  875. if (new_type)
  876. {
  877. *mapped_type = new_type;
  878. }
  879. }
  880. /*
  881. * Make a new entry suitable for the sync destination (indicated by the to_windows argument).
  882. * Returns the new entry ready to be passed to an LDAP ADD operation, either remote or local.
  883. * Also returns the plaintext value of any password contained in the original entry (only for the
  884. * to_windows direction). This is because passwords must be added to entries after they are added in AD.
  885. * Caller must free the new entry and any password returned.
  886. */
  887. static int
  888. windows_create_remote_entry(Private_Repl_Protocol *prp,Slapi_Entry *original_entry, Slapi_DN *remote_sdn, Slapi_Entry **remote_entry, char** password)
  889. {
  890. int retval = 0;
  891. char *entry_string = NULL;
  892. Slapi_Entry *new_entry = NULL;
  893. Slapi_PBlock* pb = NULL;
  894. int rc = 0;
  895. int is_user = 0;
  896. int is_group = 0;
  897. Slapi_Attr *attr = NULL;
  898. char *username = NULL;
  899. const char *dn_string = NULL;
  900. char *remote_entry_template = NULL;
  901. char *fqusername = NULL;
  902. const char *domain_name = windows_private_get_windows_domain(prp->agmt);
  903. char *remote_user_entry_template =
  904. "dn: %s\n"
  905. "objectclass:top\n"
  906. "objectclass:person\n"
  907. "objectclass:organizationalperson\n"
  908. "objectclass:user\n"
  909. "userPrincipalName:%s\n"
  910. "userAccountControl:512\n";
  911. char *remote_group_entry_template =
  912. "dn: %s\n"
  913. "objectclass:top\n"
  914. "objectclass:group\n";
  915. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_create_remote_entry\n", 0, 0, 0 );
  916. windows_is_local_entry_user_or_group(original_entry,&is_user,&is_group);
  917. /* Create a new entry */
  918. /* Give it its DN and samaccountname */
  919. username = extract_ntuserdomainid_from_entry(original_entry);
  920. if (NULL == username)
  921. {
  922. goto error;
  923. }
  924. fqusername = PR_smprintf("%s@%s",username,domain_name);
  925. dn_string = slapi_sdn_get_dn(remote_sdn);
  926. if (is_user)
  927. {
  928. entry_string = slapi_ch_smprintf(remote_user_entry_template, dn_string, fqusername);
  929. } else
  930. {
  931. entry_string = slapi_ch_smprintf(remote_group_entry_template, dn_string);
  932. }
  933. PR_smprintf_free(fqusername);
  934. if (NULL == entry_string)
  935. {
  936. goto error;
  937. }
  938. new_entry = slapi_str2entry(entry_string, 0);
  939. slapi_ch_free((void**)&entry_string);
  940. if (NULL == new_entry)
  941. {
  942. goto error;
  943. }
  944. /* Map the appropriate attributes sourced from the remote entry */
  945. /* Iterate over the local entry's attributes */
  946. for (rc = slapi_entry_first_attr(original_entry, &attr); rc == 0;
  947. rc = slapi_entry_next_attr(original_entry, attr, &attr))
  948. {
  949. Slapi_Value *value = NULL;
  950. char *type = NULL;
  951. Slapi_ValueSet *vs = NULL;
  952. int mapdn = 0;
  953. slapi_attr_get_type( attr, &type );
  954. slapi_attr_get_valueset(attr,&vs);
  955. if ( is_straight_mapped_attr(type,is_user) )
  956. {
  957. /* copy over the attr values */
  958. slapi_entry_add_valueset(new_entry,type,vs);
  959. } else
  960. {
  961. char *new_type = NULL;
  962. windows_map_attr_name(type , 1 /* to windows */, is_user, 1 /* create */, &new_type, &mapdn);
  963. if (new_type)
  964. {
  965. if (mapdn)
  966. {
  967. Slapi_ValueSet *mapped_values = NULL;
  968. map_dn_values(prp,vs,&mapped_values, 1 /* to windows */);
  969. if (mapped_values)
  970. {
  971. slapi_entry_add_valueset(new_entry,new_type,mapped_values);
  972. slapi_valueset_free(mapped_values);
  973. mapped_values = NULL;
  974. }
  975. } else
  976. {
  977. slapi_entry_add_valueset(new_entry,new_type,vs);
  978. }
  979. slapi_ch_free((void**)&new_type);
  980. }
  981. /* password mods are treated specially */
  982. if (0 == slapi_attr_type_cmp(type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD, SLAPI_TYPE_CMP_SUBTYPE) )
  983. {
  984. const char *password_value = NULL;
  985. Slapi_Value *value = NULL;
  986. slapi_valueset_first_value(vs,&value);
  987. password_value = slapi_value_get_string(value);
  988. *password = slapi_ch_strdup(password_value);
  989. }
  990. }
  991. if (vs)
  992. {
  993. slapi_valueset_free(vs);
  994. vs = NULL;
  995. }
  996. }
  997. if (remote_entry)
  998. {
  999. *remote_entry = new_entry;
  1000. }
  1001. error:
  1002. if (username)
  1003. {
  1004. slapi_ch_free((void**)&username);
  1005. }
  1006. if (new_entry)
  1007. {
  1008. windows_dump_entry("Created new remote entry:\n",new_entry);
  1009. }
  1010. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_create_remote_entry: %d\n", retval, 0, 0 );
  1011. return retval;
  1012. }
  1013. /* the entry has already been translated, so be sure to search for ntuserid
  1014. and not samaccountname or anything else. */
  1015. static Slapi_Entry*
  1016. windows_entry_already_exists(Slapi_Entry *e){
  1017. int rc = 0;
  1018. Slapi_DN *sdn = NULL;
  1019. Slapi_Entry *entry = NULL;
  1020. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_entry_already_exists\n", 0, 0, 0 );
  1021. sdn = slapi_entry_get_sdn(e);
  1022. rc = slapi_search_internal_get_entry( sdn, NULL, &entry, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  1023. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_entry_already_exists\n", 0, 0, 0 );
  1024. if (rc == LDAP_SUCCESS)
  1025. {
  1026. return entry;
  1027. }
  1028. else
  1029. {
  1030. return NULL;
  1031. }
  1032. }
  1033. static int
  1034. windows_delete_local_entry(Slapi_DN *sdn){
  1035. Slapi_PBlock *pb = NULL;
  1036. int return_value = 0;
  1037. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_delete_local_entry\n", 0, 0, 0 );
  1038. pb = slapi_pblock_new();
  1039. slapi_delete_internal_set_pb(pb, slapi_sdn_get_dn(sdn), NULL, NULL, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  1040. slapi_delete_internal_pb(pb);
  1041. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  1042. slapi_pblock_destroy(pb);
  1043. if (return_value) {
  1044. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1045. "delete operation on local entry %s returned: %d\n", slapi_sdn_get_dn(sdn), return_value);
  1046. }
  1047. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_delete_local_entry: %d\n", return_value, 0, 0 );
  1048. return return_value;
  1049. }
  1050. static void
  1051. windows_map_mods_for_replay(Private_Repl_Protocol *prp,LDAPMod **original_mods, LDAPMod ***returned_mods, int is_user, char** password)
  1052. {
  1053. Slapi_Mods smods = {0};
  1054. Slapi_Mods mapped_smods = {0};
  1055. LDAPMod *mod = NULL;
  1056. int i=0;
  1057. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_map_mods_for_replay\n", 0, 0, 0 );
  1058. /* Iterate through the original mods, looking each attribute type up in the maps for either user or group */
  1059. slapi_mods_init_byref(&smods, original_mods);
  1060. slapi_mods_init(&mapped_smods,10);
  1061. mod = slapi_mods_get_first_mod(&smods);
  1062. while(mod)
  1063. {
  1064. char *attr_type = mod->mod_type;
  1065. int mapdn = 0;
  1066. /* Check to see if this attribute is passed through */
  1067. if (is_straight_mapped_attr(attr_type,is_user)) {
  1068. /* If so then just copy over the mod */
  1069. slapi_mods_add_modbvps(&mapped_smods,mod->mod_op,attr_type,mod->mod_bvalues);
  1070. } else
  1071. {
  1072. char *mapped_type = NULL;
  1073. /* Check if this mod has its attribute type mapped */
  1074. windows_map_attr_name(attr_type,1,is_user,0,&mapped_type, &mapdn);
  1075. if (mapped_type)
  1076. {
  1077. /* If so copy over the mod with new type name */
  1078. if (mapdn)
  1079. {
  1080. Slapi_ValueSet *mapped_values = NULL;
  1081. Slapi_ValueSet *vs = NULL;
  1082. Slapi_Mod smod;
  1083. vs = slapi_valueset_new();
  1084. slapi_mod_init_byref(&smod,mod);
  1085. slapi_valueset_set_from_smod(vs, &smod);
  1086. map_dn_values(prp,vs,&mapped_values, 1 /* to windows */);
  1087. if (mapped_values)
  1088. {
  1089. slapi_mods_add_mod_values(&mapped_smods,mod->mod_op,mapped_type,valueset_get_valuearray(mapped_values));
  1090. slapi_valueset_free(mapped_values);
  1091. mapped_values = NULL;
  1092. }
  1093. slapi_mod_done(&smod);
  1094. slapi_valueset_free(vs);
  1095. } else
  1096. {
  1097. slapi_mods_add_modbvps(&mapped_smods,mod->mod_op,mapped_type,mod->mod_bvalues);
  1098. }
  1099. slapi_ch_free((void**)&mapped_type);
  1100. } else
  1101. {
  1102. /* password mods are treated specially */
  1103. if (0 == slapi_attr_type_cmp(attr_type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD, SLAPI_TYPE_CMP_SUBTYPE) )
  1104. {
  1105. char *password_value = NULL;
  1106. password_value = mod->mod_bvalues[0]->bv_val;
  1107. *password = slapi_ch_strdup(password_value);
  1108. }
  1109. }
  1110. }
  1111. /* Otherwise we do not copy this mod at all */
  1112. mod = slapi_mods_get_next_mod(&smods);
  1113. }
  1114. slapi_mods_done (&smods);
  1115. /* Extract the mods for the caller */
  1116. *returned_mods = slapi_mods_get_ldapmods_passout(&mapped_smods);
  1117. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_map_mods_for_replay\n", 0, 0, 0 );
  1118. }
  1119. /* Returns non-zero if the attribute value sets are identical */
  1120. static int
  1121. attr_compare_equal(Slapi_Attr *a, Slapi_Attr *b)
  1122. {
  1123. /* For now only handle single values */
  1124. Slapi_Value *va = NULL;
  1125. Slapi_Value *vb = NULL;
  1126. int num_a = 0;
  1127. int num_b = 0;
  1128. int match = 1;
  1129. slapi_attr_get_numvalues(a,&num_a);
  1130. slapi_attr_get_numvalues(b,&num_b);
  1131. if (num_a == num_b)
  1132. {
  1133. slapi_attr_first_value(a, &va);
  1134. slapi_attr_first_value(b, &vb);
  1135. if (va->bv.bv_len == va->bv.bv_len)
  1136. {
  1137. if (0 != memcmp(va->bv.bv_val,vb->bv.bv_val,va->bv.bv_len))
  1138. {
  1139. match = 0;
  1140. }
  1141. } else
  1142. {
  1143. match = 0;
  1144. }
  1145. } else
  1146. {
  1147. match = 0;
  1148. }
  1149. return match;
  1150. }
  1151. /* Helper functions for dirsync result processing */
  1152. /* Is this entry a tombstone ? */
  1153. static int
  1154. is_tombstone(Slapi_Entry *e)
  1155. {
  1156. int retval = 0;
  1157. char *string_deleted = "(isdeleted=*)";
  1158. /* DBDB: we should allocate these filters once and keep them around for better performance */
  1159. Slapi_Filter *filter_deleted = slapi_str2filter( string_deleted );
  1160. /* DBDB: this should be one filter, the code originally tested separately and hasn't been fixed yet */
  1161. if ( (slapi_filter_test_simple( e, filter_deleted ) == 0) )
  1162. {
  1163. retval = 1;
  1164. }
  1165. slapi_filter_free(filter_deleted,1);
  1166. filter_deleted = NULL;
  1167. return retval;
  1168. }
  1169. #define ENTRY_NOTFOUND -1
  1170. #define ENTRY_NOT_UNIQUE -2
  1171. /* Search for an entry in AD */
  1172. static int
  1173. find_entry_by_attr_value_remote(const char *attribute, const char *value, Slapi_Entry **e, Private_Repl_Protocol *prp)
  1174. {
  1175. int retval = 0;
  1176. ConnResult cres = 0;
  1177. char *filter = NULL;
  1178. const char *searchbase = NULL;
  1179. Slapi_Entry *found_entry = NULL;
  1180. filter = PR_smprintf("(%s=%s)",attribute,value);
  1181. searchbase = slapi_sdn_get_dn(windows_private_get_windows_subtree(prp->agmt));
  1182. cres = windows_search_entry(prp->conn, (char*)searchbase, filter, &found_entry);
  1183. if (cres)
  1184. {
  1185. retval = -1;
  1186. } else
  1187. {
  1188. if (found_entry)
  1189. {
  1190. *e = found_entry;
  1191. }
  1192. }
  1193. if (filter)
  1194. {
  1195. PR_smprintf_free(filter);
  1196. filter = NULL;
  1197. }
  1198. return retval;
  1199. }
  1200. /* Search for an entry in AD by DN */
  1201. static int
  1202. windows_get_remote_entry (Private_Repl_Protocol *prp, Slapi_DN* remote_dn,Slapi_Entry **remote_entry)
  1203. {
  1204. int retval = 0;
  1205. ConnResult cres = 0;
  1206. char *filter = "(objectclass=*)";
  1207. const char *searchbase = NULL;
  1208. Slapi_Entry *found_entry = NULL;
  1209. searchbase = slapi_sdn_get_dn(remote_dn);
  1210. cres = windows_search_entry(prp->conn, (char*)searchbase, filter, &found_entry);
  1211. if (cres)
  1212. {
  1213. retval = -1;
  1214. } else
  1215. {
  1216. if (found_entry)
  1217. {
  1218. *remote_entry = found_entry;
  1219. }
  1220. }
  1221. return retval;
  1222. }
  1223. static int
  1224. find_entry_by_attr_value(const char *attribute, const char *value, Slapi_Entry **e, const Repl_Agmt *ra)
  1225. {
  1226. Slapi_PBlock *pb = slapi_pblock_new();
  1227. Slapi_Entry **entries = NULL, **ep = NULL;
  1228. Slapi_Entry *entry_found = NULL;
  1229. char *query = NULL;
  1230. int found_or_not = ENTRY_NOTFOUND;
  1231. int rval = 0;
  1232. const char *subtree_dn = NULL;
  1233. int not_unique = 0;
  1234. if (pb == NULL)
  1235. goto done;
  1236. query = slapi_ch_smprintf("(%s=%s)", attribute, value);
  1237. if (query == NULL)
  1238. goto done;
  1239. subtree_dn = slapi_sdn_get_dn(windows_private_get_directory_subtree(ra));
  1240. slapi_search_internal_set_pb(pb, subtree_dn,
  1241. LDAP_SCOPE_SUBTREE, query, NULL, 0, NULL, NULL,
  1242. (void *)plugin_get_default_component_id(), 0);
  1243. slapi_search_internal_pb(pb);
  1244. slapi_ch_free((void **)&query);
  1245. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rval);
  1246. if (rval != LDAP_SUCCESS)
  1247. {
  1248. goto done;
  1249. }
  1250. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1251. if ((entries == NULL) || (entries[0] == NULL))
  1252. {
  1253. goto done;
  1254. }
  1255. entry_found = entries[0];
  1256. for (ep = entries; *ep; ep++) {
  1257. if (not_unique)
  1258. {
  1259. found_or_not = ENTRY_NOT_UNIQUE;
  1260. }
  1261. not_unique = 1;
  1262. }
  1263. done:
  1264. if (entry_found && (found_or_not != ENTRY_NOT_UNIQUE))
  1265. {
  1266. found_or_not = 0;
  1267. *e = slapi_entry_dup(entry_found);
  1268. }
  1269. if (pb)
  1270. {
  1271. slapi_free_search_results_internal(pb);
  1272. slapi_pblock_destroy(pb);
  1273. }
  1274. return found_or_not;
  1275. }
  1276. static int
  1277. find_entry_by_username(const char *username, Slapi_Entry **e, const Repl_Agmt *ra)
  1278. {
  1279. return find_entry_by_attr_value("ntUserDomainId",username,e,ra);
  1280. }
  1281. /* Find an entry in the local server given its GUID, or return ENTRY_NOTFOUND */
  1282. static int
  1283. find_entry_by_guid(const char *guid, Slapi_Entry **e, const Repl_Agmt *ra)
  1284. {
  1285. return find_entry_by_attr_value("ntUniqueId",guid,e,ra);
  1286. }
  1287. /* Remove dashes from a GUID string */
  1288. static void
  1289. dedash(char *str)
  1290. {
  1291. char *p = str;
  1292. char c = '\0';
  1293. while (c = *p)
  1294. {
  1295. if ('-' == c)
  1296. {
  1297. /* Move on down please */
  1298. char *q = p;
  1299. char *r = q + 1;
  1300. while (*r)
  1301. {
  1302. *q++ = *r++;
  1303. }
  1304. *q = '\0';
  1305. }
  1306. p++;
  1307. }
  1308. }
  1309. /* For reasons not clear, the GUID returned in the tombstone DN is all
  1310. * messed up, like the guy in the movie 'the fly' after he want in the tranporter device */
  1311. static void
  1312. decrypt(char *guid)
  1313. {
  1314. static int decrypt_offsets[] = {6,7,4,5,2,3,0,1,10,11,8,9,14,15,12,13,16,17,18,19,
  1315. 20,21,22,23,24,25,26,27,28,29,30,31};
  1316. char *p = guid;
  1317. int i = 0;
  1318. char *cpy = slapi_ch_strdup(guid);
  1319. while (*p && i < (sizeof(decrypt_offsets)/sizeof(int)))
  1320. {
  1321. *p = cpy[decrypt_offsets[i]];
  1322. p++;
  1323. i++;
  1324. }
  1325. slapi_ch_free((void**)&cpy);
  1326. }
  1327. static char*
  1328. extract_guid_from_tombstone_dn(const char *dn)
  1329. {
  1330. char *guid = NULL;
  1331. char *colon_offset = NULL;
  1332. char *comma_offset = NULL;
  1333. /* example DN of tombstone:
  1334. "CN=WDel Userdb1\\\nDEL:551706bc-ecf2-4b38-9284-9a8554171d69,CN=Deleted Objects,DC=magpie,DC=com" */
  1335. /* First find the 'DEL:' */
  1336. colon_offset = strchr(dn,':');
  1337. /* Then scan forward to the next ',' */
  1338. comma_offset = strchr(dn,',');
  1339. /* The characters inbetween are the GUID, copy them to a new string and return to the caller */
  1340. if (comma_offset && colon_offset && comma_offset > colon_offset) {
  1341. guid = slapi_ch_malloc(comma_offset - colon_offset);
  1342. strncpy(guid,colon_offset+1,(comma_offset-colon_offset)-1);
  1343. guid[comma_offset-colon_offset-1] = '\0';
  1344. /* Finally remove the dashes since we don't store them on our side */
  1345. dedash(guid);
  1346. decrypt(guid);
  1347. }
  1348. return guid;
  1349. }
  1350. static char *
  1351. convert_to_hex(Slapi_Value *val)
  1352. {
  1353. int offset = 0;
  1354. const struct berval *bvp = NULL;
  1355. int length = 0;
  1356. char *result = NULL;
  1357. bvp = slapi_value_get_berval(val);
  1358. if (bvp)
  1359. {
  1360. char *new_buffer = NULL;
  1361. length = bvp->bv_len;
  1362. for (offset = 0; offset < length; offset++)
  1363. {
  1364. unsigned char byte = ((unsigned char*)(bvp->bv_val))[offset];
  1365. new_buffer = PR_sprintf_append(new_buffer, "%02x", byte );
  1366. }
  1367. if (new_buffer)
  1368. {
  1369. result = new_buffer;
  1370. }
  1371. }
  1372. return result;
  1373. }
  1374. static char*
  1375. extract_guid_from_entry(Slapi_Entry *e, int is_nt4)
  1376. {
  1377. char *guid = NULL;
  1378. Slapi_Value *val = NULL;
  1379. Slapi_Attr *attr = NULL;
  1380. slapi_entry_attr_find(e, "objectGUID", &attr);
  1381. if (attr)
  1382. {
  1383. slapi_attr_first_value(attr, &val);
  1384. if (val) {
  1385. if (is_nt4)
  1386. {
  1387. guid = slapi_ch_strdup(slapi_value_get_string(val));
  1388. } else
  1389. {
  1390. guid = convert_to_hex(val);
  1391. }
  1392. }
  1393. }
  1394. return guid;
  1395. }
  1396. static void
  1397. extract_guid_from_entry_bv(Slapi_Entry *e, const struct berval **bv)
  1398. {
  1399. Slapi_Value *val = NULL;
  1400. Slapi_Attr *attr = NULL;
  1401. slapi_entry_attr_find(e, "objectGUID", &attr);
  1402. if (attr)
  1403. {
  1404. slapi_attr_first_value(attr, &val);
  1405. if (val) {
  1406. *bv = slapi_value_get_berval(val);
  1407. }
  1408. }
  1409. }
  1410. static char*
  1411. extract_username_from_entry(Slapi_Entry *e)
  1412. {
  1413. char *uid = NULL;
  1414. uid = slapi_entry_attr_get_charptr(e,"samAccountName");
  1415. return uid;
  1416. }
  1417. static char*
  1418. extract_ntuserdomainid_from_entry(Slapi_Entry *e)
  1419. {
  1420. char *uid = NULL;
  1421. uid = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  1422. return uid;
  1423. }
  1424. static Slapi_DN *make_dn_from_guid(char *guid, int is_nt4, const char* suffix)
  1425. {
  1426. Slapi_DN *new_dn = NULL;
  1427. char *dn_string = NULL;
  1428. if (guid)
  1429. {
  1430. new_dn = slapi_sdn_new();
  1431. if (is_nt4)
  1432. {
  1433. dn_string = PR_smprintf("GUID=%s,%s",guid,suffix);
  1434. } else
  1435. {
  1436. dn_string = PR_smprintf("<GUID=%s>",guid);
  1437. }
  1438. slapi_sdn_init_dn_byval(new_dn,dn_string);
  1439. PR_smprintf_free(dn_string);
  1440. }
  1441. /* dn string is now inside the Slapi_DN, and will be freed by its owner */
  1442. return new_dn;
  1443. }
  1444. /* Given a non-tombstone entry, return the DN of its peer in AD (whether present or not) */
  1445. static int
  1446. map_entry_dn_outbound(Slapi_Entry *e, const Slapi_DN **dn, Private_Repl_Protocol *prp, int *missing_entry, int guid_form)
  1447. {
  1448. int retval = 0;
  1449. char *guid = NULL;
  1450. Slapi_DN *new_dn = NULL;
  1451. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  1452. const char *suffix = slapi_sdn_get_dn(windows_private_get_windows_subtree(prp->agmt));
  1453. /* To find the DN of the peer entry we first look for an ntUniqueId attribute
  1454. * on the local entry. If that's present, we generate a GUID-form DN.
  1455. * If there's no GUID, then we look for an ntUserDomainId attribute
  1456. * on the entry. If that's present we attempt to search for an entry with
  1457. * that samaccountName attribute value in AD. If we don't find any matching
  1458. * entry we generate a new DN using the entry's cn. If later, we find that
  1459. * this entry already exists, we handle that problem at the time. We don't
  1460. * check here.
  1461. */
  1462. *missing_entry = 0;
  1463. guid = slapi_entry_attr_get_charptr(e,"ntUniqueId");
  1464. if (guid && guid_form)
  1465. {
  1466. new_dn = make_dn_from_guid(guid, is_nt4, suffix);
  1467. slapi_ch_free((void**)&guid);
  1468. } else
  1469. {
  1470. /* No GUID found, try ntUserDomainId */
  1471. Slapi_Entry *remote_entry = NULL;
  1472. char *username = slapi_entry_attr_get_charptr(e,"ntUserDomainId");
  1473. if (username) {
  1474. retval = find_entry_by_attr_value_remote("samAccountName",username,&remote_entry,prp);
  1475. if (0 == retval && remote_entry)
  1476. {
  1477. /* Get the entry's DN */
  1478. new_dn = slapi_sdn_new();
  1479. slapi_sdn_copy(slapi_entry_get_sdn_const(remote_entry), new_dn);
  1480. } else {
  1481. if (0 == retval)
  1482. {
  1483. char *new_dn_string = NULL;
  1484. char *cn_string = NULL;
  1485. *missing_entry = 1;
  1486. /* This means that we failed to find a peer entry */
  1487. /* In that case we need to generate the DN that we want to use */
  1488. /* Generated DN's take the form :
  1489. cn=<cn from local entry>, ... in the case that the local entry has a cn, OR
  1490. cn=<ntuserdomainid attribute value>, ... in the case that the local entry doesn't have a CN
  1491. */
  1492. cn_string = slapi_entry_attr_get_charptr(e,"cn");
  1493. if (!cn_string)
  1494. {
  1495. cn_string = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  1496. }
  1497. if (cn_string)
  1498. {
  1499. char *rdnstr = NULL;
  1500. rdnstr = is_nt4 ? "samaccountname=%s,%s" : "cn=%s,%s";
  1501. new_dn_string = PR_smprintf(rdnstr,cn_string,suffix);
  1502. if (new_dn_string)
  1503. {
  1504. new_dn = slapi_sdn_new_dn_byval(new_dn_string);
  1505. PR_smprintf_free(new_dn_string);
  1506. }
  1507. slapi_ch_free((void**)&cn_string);
  1508. }
  1509. } else
  1510. {
  1511. /* This means that we failed to talk to the AD for some reason, the operation should be re-tried */
  1512. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1513. "%s: map_entry_dn_outbound: failed to fetch entry from AD: dn=\"%s\", err=%d\n",
  1514. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)), retval);
  1515. retval = -1;
  1516. }
  1517. }
  1518. slapi_ch_free((void**)&username);
  1519. }
  1520. if (remote_entry)
  1521. {
  1522. slapi_entry_free(remote_entry);
  1523. }
  1524. }
  1525. if (new_dn)
  1526. {
  1527. *dn = new_dn;
  1528. }
  1529. return retval;
  1530. }
  1531. /* Given a tombstone entry, return the DN of its peer in this server (if present) */
  1532. static int
  1533. map_tombstone_dn_inbound(Slapi_Entry *e, const Slapi_DN **dn, const Repl_Agmt *ra)
  1534. {
  1535. int retval = 0;
  1536. Slapi_DN *new_dn = NULL;
  1537. char *guid = NULL;
  1538. const char *dn_string = NULL;
  1539. Slapi_Entry *matching_entry = NULL;
  1540. /* To map a tombstone's DN we need to first extract the entry's objectGUID from the DN
  1541. * CN=vpdxtAD_07\
  1542. DEL:d4ca4e16-e35b-400d-834a-f02db600f3fa,CN=Deleted Objects,DC=magpie,DC=com
  1543. */
  1544. *dn = NULL;
  1545. dn_string = slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)); /* This is a pointer from inside the sdn, no need to free */
  1546. guid = extract_guid_from_tombstone_dn(dn_string);
  1547. if (guid)
  1548. {
  1549. retval = find_entry_by_guid(guid,&matching_entry,ra);
  1550. if (retval)
  1551. {
  1552. if (ENTRY_NOTFOUND == retval)
  1553. {
  1554. } else
  1555. {
  1556. if (ENTRY_NOT_UNIQUE == retval)
  1557. {
  1558. } else
  1559. {
  1560. /* A real error */
  1561. }
  1562. }
  1563. } else
  1564. {
  1565. /* We found the matching entry : get its DN */
  1566. new_dn = slapi_sdn_dup(slapi_entry_get_sdn_const(matching_entry));
  1567. }
  1568. }
  1569. if (new_dn)
  1570. {
  1571. *dn = new_dn;
  1572. }
  1573. if (guid)
  1574. {
  1575. slapi_ch_free((void**)&guid);
  1576. }
  1577. if (matching_entry)
  1578. {
  1579. slapi_entry_free(matching_entry);
  1580. }
  1581. return retval;
  1582. }
  1583. /* Given a non-tombstone entry, return the DN of its peer in this server (whether present or not) */
  1584. static int
  1585. map_entry_dn_inbound(Slapi_Entry *e, const Slapi_DN **dn, const Repl_Agmt *ra)
  1586. {
  1587. int retval = 0;
  1588. Slapi_DN *new_dn = NULL;
  1589. char *guid = NULL;
  1590. char *username = NULL;
  1591. Slapi_Entry *matching_entry = NULL;
  1592. int is_user = 0;
  1593. int is_group = 0;
  1594. int is_nt4 = windows_private_get_isnt4(ra);
  1595. /* To map a non-tombstone's DN we need to first try to look it up by GUID.
  1596. * If we do not find it, then we need to generate the DN that it would have if added as a new entry.
  1597. */
  1598. *dn = NULL;
  1599. windows_is_remote_entry_user_or_group(e,&is_user,&is_group);
  1600. guid = extract_guid_from_entry(e, is_nt4);
  1601. if (guid)
  1602. {
  1603. retval = find_entry_by_guid(guid,&matching_entry,ra);
  1604. if (retval)
  1605. {
  1606. if (ENTRY_NOTFOUND == retval)
  1607. {
  1608. } else
  1609. {
  1610. if (ENTRY_NOT_UNIQUE == retval)
  1611. {
  1612. } else
  1613. {
  1614. /* A real error */
  1615. goto error;
  1616. }
  1617. }
  1618. } else
  1619. {
  1620. /* We found the matching entry : get its DN */
  1621. new_dn = slapi_sdn_dup(slapi_entry_get_sdn_const(matching_entry));
  1622. }
  1623. }
  1624. /* If we failed to lookup by guid, try samaccountname */
  1625. if (NULL == new_dn)
  1626. {
  1627. username = extract_username_from_entry(e);
  1628. if (username) {
  1629. retval = find_entry_by_username(username,&matching_entry,ra);
  1630. if (retval)
  1631. {
  1632. if (ENTRY_NOTFOUND == retval)
  1633. {
  1634. } else
  1635. {
  1636. if (ENTRY_NOT_UNIQUE == retval)
  1637. {
  1638. } else
  1639. {
  1640. /* A real error */
  1641. goto error;
  1642. }
  1643. }
  1644. } else
  1645. {
  1646. /* We found the matching entry : get its DN */
  1647. new_dn = slapi_sdn_dup(slapi_entry_get_sdn_const(matching_entry));
  1648. }
  1649. }
  1650. }
  1651. /* If we couldn't find a matching entry by either method, then we need to invent a new DN */
  1652. if (NULL == new_dn)
  1653. {
  1654. /* The new DN has the form: uid=<samaccountname>,<sync'ed subtree> */
  1655. /* If an entry with this DN already exists, we fail and return no DN
  1656. * this is because we don't want to second-guess what the admin wants here:
  1657. * they may want to associate this existing entry with the peer AD entry,
  1658. * but if they intend that we say they must add the ntDomainUserId attribute to
  1659. * that entry.
  1660. */
  1661. char *new_dn_string = NULL;
  1662. if (username)
  1663. {
  1664. const char *suffix = slapi_sdn_get_dn(windows_private_get_directory_subtree(ra));
  1665. /* Local DNs for users and groups are different */
  1666. if (is_user)
  1667. {
  1668. new_dn_string = PR_smprintf("uid=%s,%s",username,suffix);
  1669. } else
  1670. {
  1671. new_dn_string = PR_smprintf("cn=%s,%s",username,suffix);
  1672. }
  1673. new_dn = slapi_sdn_new_dn_byval(new_dn_string);
  1674. PR_smprintf_free(new_dn_string);
  1675. /* Clear any earlier error */
  1676. retval = 0;
  1677. } else
  1678. {
  1679. /* Error, no username */
  1680. }
  1681. }
  1682. if (new_dn)
  1683. {
  1684. *dn = new_dn;
  1685. }
  1686. error:
  1687. if (guid)
  1688. {
  1689. PR_smprintf_free(guid);
  1690. }
  1691. if (matching_entry)
  1692. {
  1693. slapi_entry_free(matching_entry);
  1694. }
  1695. if (username)
  1696. {
  1697. slapi_ch_free((void **) &username);
  1698. }
  1699. return retval;
  1700. }
  1701. /* Tests if the entry is subject to our agreement (i.e. is it in the sync'ed subtree in this server, and is it the right objectclass
  1702. * and does it have the right attribute values for sync ?)
  1703. */
  1704. static int
  1705. is_subject_of_agreemeent_local(const Slapi_Entry *local_entry, const Repl_Agmt *ra)
  1706. {
  1707. int retval = 0;
  1708. int is_in_subtree = 0;
  1709. const Slapi_DN *agreement_subtree = NULL;
  1710. /* First test for the sync'ed subtree */
  1711. agreement_subtree = windows_private_get_directory_subtree(ra);
  1712. if (NULL == agreement_subtree)
  1713. {
  1714. goto error;
  1715. }
  1716. is_in_subtree = slapi_sdn_scope_test(slapi_entry_get_sdn_const(local_entry), agreement_subtree, LDAP_SCOPE_SUBTREE);
  1717. if (is_in_subtree)
  1718. {
  1719. /* Next test for the correct kind of entry */
  1720. if (local_entry) {
  1721. /* DBDB: we should allocate these filters once and keep them around for better performance */
  1722. char *string_filter = "(&(|(objectclass=ntuser)(objectclass=ntgroup))(ntUserDomainId=*))";
  1723. Slapi_Filter *filter = slapi_str2filter( string_filter );
  1724. if (slapi_filter_test_simple( (Slapi_Entry*)local_entry, filter ) == 0)
  1725. {
  1726. retval = 1;
  1727. }
  1728. slapi_filter_free(filter,1);
  1729. filter = NULL;
  1730. } else
  1731. {
  1732. /* Error: couldn't find the entry */
  1733. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1734. "failed to find entry in is_subject_of_agreemeent_local: %d\n", retval);
  1735. retval = 0;
  1736. }
  1737. }
  1738. error:
  1739. return retval;
  1740. }
  1741. /* Tests if the entry is subject to our agreement (i.e. is it in the sync'ed subtree in AD and either a user or a group ?) */
  1742. static int
  1743. is_subject_of_agreemeent_remote(Slapi_Entry *e, const Repl_Agmt *ra)
  1744. {
  1745. int retval = 0;
  1746. int is_in_subtree = 0;
  1747. const Slapi_DN *agreement_subtree = NULL;
  1748. /* First test for the sync'ed subtree */
  1749. agreement_subtree = windows_private_get_windows_subtree(ra);
  1750. if (NULL == agreement_subtree)
  1751. {
  1752. goto error;
  1753. }
  1754. is_in_subtree = slapi_sdn_scope_test(slapi_entry_get_sdn_const(e), agreement_subtree, LDAP_SCOPE_SUBTREE);
  1755. if (is_in_subtree)
  1756. {
  1757. retval = 1;
  1758. }
  1759. error:
  1760. return retval;
  1761. }
  1762. static int
  1763. windows_create_local_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,const Slapi_DN* local_sdn)
  1764. {
  1765. int retval = 0;
  1766. char *entry_string = NULL;
  1767. Slapi_Entry *local_entry = NULL;
  1768. Slapi_PBlock* pb = NULL;
  1769. int is_user = 0;
  1770. int is_group = 0;
  1771. char *local_entry_template = NULL;
  1772. char *user_entry_template = NULL;
  1773. char *username = extract_username_from_entry(remote_entry);
  1774. Slapi_Attr *attr = NULL;
  1775. int rc = 0;
  1776. char *guid_str = NULL;
  1777. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  1778. char *local_user_entry_template =
  1779. "dn: %s\n"
  1780. "objectclass:top\n"
  1781. "objectclass:person\n"
  1782. "objectclass:organizationalperson\n"
  1783. "objectclass:inetOrgPerson\n"
  1784. "objectclass:ntUser\n"
  1785. "ntUserDeleteAccount:true\n"
  1786. "uid:%s\n";
  1787. char *local_nt4_user_entry_template =
  1788. "dn: %s\n"
  1789. "objectclass:top\n"
  1790. "objectclass:person\n"
  1791. "objectclass:organizationalperson\n"
  1792. "objectclass:inetOrgPerson\n"
  1793. "objectclass:ntUser\n"
  1794. "ntUserDeleteAccount:true\n"
  1795. "uid:%s\n";
  1796. char *local_group_entry_template =
  1797. "dn: %s\n"
  1798. "objectclass:top\n"
  1799. "objectclass:groupofuniquenames\n"
  1800. "objectclass:ntGroup\n"
  1801. "objectclass:mailGroup\n"
  1802. "ntGroupDeleteGroup:true\n"
  1803. "cn:%s\n";
  1804. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_create_local_entry\n", 0, 0, 0 );
  1805. windows_is_remote_entry_user_or_group(remote_entry,&is_user,&is_group);
  1806. user_entry_template = is_nt4 ? local_nt4_user_entry_template : local_user_entry_template;
  1807. local_entry_template = is_user ? user_entry_template : local_group_entry_template;
  1808. /* Create a new entry */
  1809. /* Give it its DN and username */
  1810. entry_string = slapi_ch_smprintf(local_entry_template,slapi_sdn_get_dn(local_sdn),username, username);
  1811. if (NULL == entry_string)
  1812. {
  1813. goto error;
  1814. }
  1815. local_entry = slapi_str2entry(entry_string, 0);
  1816. slapi_ch_free((void**)&entry_string);
  1817. if (NULL == local_entry)
  1818. {
  1819. goto error;
  1820. }
  1821. /* Map the appropriate attributes sourced from the remote entry */
  1822. for (rc = slapi_entry_first_attr(remote_entry, &attr); rc == 0;
  1823. rc = slapi_entry_next_attr(remote_entry, attr, &attr))
  1824. {
  1825. Slapi_Value *value = NULL;
  1826. char *type = NULL;
  1827. Slapi_ValueSet *vs = NULL;
  1828. int mapdn = 0;
  1829. slapi_attr_get_type( attr, &type );
  1830. slapi_attr_get_valueset(attr,&vs);
  1831. if ( is_straight_mapped_attr(type,is_user) )
  1832. {
  1833. /* copy over the attr values */
  1834. slapi_entry_add_valueset(local_entry,type,vs);
  1835. } else
  1836. {
  1837. char *new_type = NULL;
  1838. windows_map_attr_name(type , 0 /* from windows */, is_user, 1 /* create */, &new_type, &mapdn);
  1839. if (new_type)
  1840. {
  1841. if (mapdn)
  1842. {
  1843. Slapi_ValueSet *mapped_values = NULL;
  1844. map_dn_values(prp,vs,&mapped_values, 0 /* from windows */);
  1845. if (mapped_values)
  1846. {
  1847. slapi_entry_add_valueset(local_entry,new_type,mapped_values);
  1848. slapi_valueset_free(mapped_values);
  1849. mapped_values = NULL;
  1850. }
  1851. } else
  1852. {
  1853. slapi_entry_add_valueset(local_entry,new_type,vs);
  1854. }
  1855. slapi_ch_free((void**)&new_type);
  1856. }
  1857. }
  1858. if (vs)
  1859. {
  1860. slapi_valueset_free(vs);
  1861. vs = NULL;
  1862. }
  1863. }
  1864. /* Copy over the GUID */
  1865. guid_str = extract_guid_from_entry(remote_entry, is_nt4);
  1866. if (guid_str)
  1867. {
  1868. slapi_entry_add_string(local_entry,"ntUniqueId",guid_str);
  1869. } else
  1870. {
  1871. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1872. "extract_guid_from_entry entry %s failed to extract the guid\n", slapi_sdn_get_dn(local_sdn));
  1873. /* Fatal error : need the guid */
  1874. goto error;
  1875. }
  1876. /* Hack for NT4, which has no surname */
  1877. if (is_nt4)
  1878. {
  1879. slapi_entry_add_string(local_entry,"sn",username);
  1880. }
  1881. /* Store it */
  1882. windows_dump_entry("Adding new local entry",local_entry);
  1883. pb = slapi_pblock_new();
  1884. slapi_add_entry_internal_set_pb(pb, local_entry, NULL,repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),0);
  1885. slapi_add_internal_pb(pb);
  1886. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &retval);
  1887. if (retval) {
  1888. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1889. "add operation of entry %s returned: %d\n", slapi_sdn_get_dn(local_sdn), retval);
  1890. }
  1891. error:
  1892. if (pb)
  1893. {
  1894. slapi_pblock_destroy(pb);
  1895. }
  1896. if (username)
  1897. {
  1898. slapi_ch_free((void**)&username);
  1899. }
  1900. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_create_local_entry\n", 0, 0, 0 );
  1901. return retval;
  1902. }
  1903. static int
  1904. windows_generate_update_mods(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,Slapi_Entry *local_entry, int to_windows, Slapi_Mods *smods, int *do_modify)
  1905. {
  1906. int retval = 0;
  1907. Slapi_Attr *attr = NULL;
  1908. int is_user = 0;
  1909. int is_group = 0;
  1910. int rc = 0;
  1911. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  1912. /* Iterate over the attributes on the remote entry, updating the local entry where appropriate */
  1913. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_update_local_entry\n", 0, 0, 0 );
  1914. *do_modify = 0;
  1915. if (to_windows)
  1916. {
  1917. windows_is_local_entry_user_or_group(remote_entry,&is_user,&is_group);
  1918. } else
  1919. {
  1920. windows_is_remote_entry_user_or_group(remote_entry,&is_user,&is_group);
  1921. }
  1922. for (rc = slapi_entry_first_attr(remote_entry, &attr); rc == 0;
  1923. rc = slapi_entry_next_attr(remote_entry, attr, &attr))
  1924. {
  1925. int is_present_local = 0;
  1926. Slapi_Value *value = NULL;
  1927. char *type = NULL;
  1928. Slapi_ValueSet *vs = NULL;
  1929. char *local_type = NULL;
  1930. Slapi_Attr *local_attr = NULL;
  1931. int is_guid = 0;
  1932. int mapdn = 0;
  1933. slapi_attr_get_type( attr, &type );
  1934. slapi_attr_get_valueset(attr,&vs);
  1935. /* First determine what we will do with this attr */
  1936. /* If it's a GUID, we need to take special action */
  1937. if (0 == slapi_attr_type_cmp(type,"objectGuid",SLAPI_TYPE_CMP_SUBTYPE) && !to_windows)
  1938. {
  1939. is_guid = 1;
  1940. local_type = slapi_ch_strdup("ntUniqueId");
  1941. } else
  1942. {
  1943. if ( is_straight_mapped_attr(type,is_user) ) {
  1944. local_type = slapi_ch_strdup(type);
  1945. } else {
  1946. windows_map_attr_name(type , to_windows, is_user, 0 /* not create */, &local_type, &mapdn);
  1947. }
  1948. is_guid = 0;
  1949. }
  1950. if (NULL == local_type)
  1951. {
  1952. /* Means we do not map this attribute */
  1953. if (vs)
  1954. {
  1955. slapi_valueset_free(vs);
  1956. vs = NULL;
  1957. }
  1958. continue;
  1959. }
  1960. slapi_entry_attr_find(local_entry,local_type,&local_attr);
  1961. is_present_local = (NULL == local_attr) ? 0 : 1;
  1962. /* Is the attribute present on the local entry ? */
  1963. if (is_present_local && !is_guid)
  1964. {
  1965. int values_equal = attr_compare_equal(attr,local_attr);
  1966. /* If it is then we need to replace the local values with the remote values if they are different */
  1967. if (!values_equal)
  1968. {
  1969. if (mapdn)
  1970. {
  1971. Slapi_ValueSet *mapped_values = NULL;
  1972. map_dn_values(prp,vs,&mapped_values, to_windows);
  1973. if (mapped_values)
  1974. {
  1975. slapi_mods_add_mod_values(smods,LDAP_MOD_REPLACE,local_type,valueset_get_valuearray(mapped_values));
  1976. slapi_valueset_free(mapped_values);
  1977. mapped_values = NULL;
  1978. }
  1979. } else
  1980. {
  1981. slapi_mods_add_mod_values(smods,LDAP_MOD_REPLACE,local_type,valueset_get_valuearray(vs));
  1982. }
  1983. *do_modify = 1;
  1984. } else
  1985. {
  1986. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1987. "windows_update_local_entry: %s, %s : values are equal\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry)), local_type);
  1988. }
  1989. } else
  1990. {
  1991. if (!is_present_local)
  1992. {
  1993. /* If it is currently absent, then we add the value from the remote entry */
  1994. if (is_guid)
  1995. {
  1996. /* Translate the guid value */
  1997. char *guid = extract_guid_from_entry(remote_entry, is_nt4);
  1998. if (guid)
  1999. {
  2000. slapi_mods_add_string(smods,LDAP_MOD_ADD,local_type,guid);
  2001. slapi_ch_free((void**)&guid);
  2002. }
  2003. } else
  2004. {
  2005. /* Handle DN valued attributes here */
  2006. if (mapdn)
  2007. {
  2008. Slapi_ValueSet *mapped_values = NULL;
  2009. map_dn_values(prp,vs,&mapped_values, to_windows);
  2010. if (mapped_values)
  2011. {
  2012. slapi_mods_add_mod_values(smods,LDAP_MOD_ADD,local_type,valueset_get_valuearray(mapped_values));
  2013. slapi_valueset_free(mapped_values);
  2014. mapped_values = NULL;
  2015. }
  2016. } else
  2017. {
  2018. slapi_mods_add_mod_values(smods,LDAP_MOD_ADD,local_type,valueset_get_valuearray(vs));
  2019. }
  2020. }
  2021. *do_modify = 1;
  2022. }
  2023. }
  2024. if (vs)
  2025. {
  2026. slapi_valueset_free(vs);
  2027. vs = NULL;
  2028. }
  2029. if (local_type)
  2030. {
  2031. slapi_ch_free((void**)&local_type);
  2032. local_type = NULL;
  2033. }
  2034. }
  2035. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_update_local_entry: %d\n", retval, 0, 0 );
  2036. return retval;
  2037. }
  2038. static int
  2039. windows_update_remote_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,Slapi_Entry *local_entry)
  2040. {
  2041. Slapi_Mods smods = {0};
  2042. int retval = 0;
  2043. int do_modify = 0;
  2044. slapi_mods_init (&smods, 0);
  2045. retval = windows_generate_update_mods(prp,local_entry,remote_entry,1,&smods,&do_modify);
  2046. /* Now perform the modify if we need to */
  2047. if (0 == retval && do_modify)
  2048. {
  2049. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2050. "windows_update_remote_entry: modifying entry %s\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(remote_entry)));
  2051. retval = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(slapi_entry_get_sdn_const(remote_entry)),slapi_mods_get_ldapmods_byref(&smods), NULL,NULL);
  2052. } else
  2053. {
  2054. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2055. "no mods generated for entry: %s\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(remote_entry)));
  2056. }
  2057. slapi_mods_done(&smods);
  2058. return retval;
  2059. }
  2060. static int
  2061. windows_update_local_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,Slapi_Entry *local_entry)
  2062. {
  2063. Slapi_Mods smods = {0};
  2064. int retval = 0;
  2065. int rc = 0;
  2066. Slapi_PBlock *pb = NULL;
  2067. int do_modify = 0;
  2068. slapi_mods_init (&smods, 0);
  2069. retval = windows_generate_update_mods(prp,remote_entry,local_entry,0,&smods,&do_modify);
  2070. /* Now perform the modify if we need to */
  2071. if (0 == retval && do_modify)
  2072. {
  2073. int rc = 0;
  2074. pb = slapi_pblock_new();
  2075. if (pb)
  2076. {
  2077. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2078. "modifying entry: %s\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry)));
  2079. slapi_modify_internal_set_pb (pb, slapi_entry_get_ndn(local_entry), slapi_mods_get_ldapmods_byref(&smods), NULL, NULL,
  2080. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2081. slapi_modify_internal_pb (pb);
  2082. slapi_pblock_get (pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2083. if (rc)
  2084. {
  2085. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  2086. "windows_update_local_entry: failed to modify entry %s\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry)));
  2087. }
  2088. slapi_pblock_destroy(pb);
  2089. } else
  2090. {
  2091. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  2092. "failed to make pb in windows_update_local_entry\n");
  2093. }
  2094. } else
  2095. {
  2096. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2097. "no mods generated for entry: %s\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(remote_entry)));
  2098. }
  2099. slapi_mods_done(&smods);
  2100. return retval;
  2101. }
  2102. static int
  2103. windows_process_total_add(Private_Repl_Protocol *prp,Slapi_Entry *e, Slapi_DN* remote_dn, int missing_entry)
  2104. {
  2105. int retval = 0;
  2106. LDAPMod **entryattrs = NULL;
  2107. Slapi_Entry *mapped_entry = NULL;
  2108. char *password = NULL;
  2109. const Slapi_DN* local_dn = NULL;
  2110. /* First map the entry */
  2111. local_dn = slapi_entry_get_sdn_const(e);
  2112. if (missing_entry)
  2113. retval = windows_create_remote_entry(prp, e, remote_dn, &mapped_entry, &password);
  2114. /* Convert entry to mods */
  2115. if (0 == retval && mapped_entry)
  2116. {
  2117. (void)slapi_entry2mods (mapped_entry , NULL /* &entrydn : We don't need it */, &entryattrs);
  2118. slapi_entry_free(mapped_entry);
  2119. mapped_entry = NULL;
  2120. if (NULL == entryattrs)
  2121. {
  2122. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,"%s: windows_replay_update: Cannot convert entry to LDAPMods.\n",agmt_get_long_name(prp->agmt));
  2123. retval = CONN_LOCAL_ERROR;
  2124. }
  2125. else
  2126. {
  2127. windows_log_add_entry_remote(local_dn, remote_dn);
  2128. retval = windows_conn_send_add(prp->conn, slapi_sdn_get_dn(remote_dn), entryattrs, NULL, NULL /* returned controls */);
  2129. /* It's possible that the entry already exists in AD, in which case we fall back to modify it */
  2130. if (retval)
  2131. {
  2132. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,"%s: windows_replay_update: Cannot replay add operation.\n",agmt_get_long_name(prp->agmt));
  2133. }
  2134. ldap_mods_free(entryattrs, 1);
  2135. entryattrs = NULL;
  2136. }
  2137. } else
  2138. {
  2139. /* Entry already exists, need to mod it instead */
  2140. Slapi_Entry *remote_entry = NULL;
  2141. /* Get the remote entry */
  2142. retval = windows_get_remote_entry(prp, remote_dn,&remote_entry);
  2143. if (0 == retval && remote_entry)
  2144. {
  2145. retval = windows_update_remote_entry(prp,remote_entry,e);
  2146. }
  2147. if (remote_entry)
  2148. {
  2149. slapi_entry_free(remote_entry);
  2150. }
  2151. }
  2152. return retval;
  2153. }
  2154. static int
  2155. windows_process_total_delete(Private_Repl_Protocol *prp,Slapi_Entry *e, Slapi_DN* remote_dn)
  2156. {
  2157. int retval = 0;
  2158. if (delete_remote_entry_allowed(e))
  2159. {
  2160. retval = windows_conn_send_delete(prp->conn, slapi_sdn_get_dn(remote_dn), NULL, NULL /* returned controls */);
  2161. }
  2162. return retval;
  2163. }
  2164. /* Entry point for the total protocol */
  2165. int windows_process_total_entry(Private_Repl_Protocol *prp,Slapi_Entry *e)
  2166. {
  2167. int retval = 0;
  2168. int is_ours = 0;
  2169. int is_tombstone = 0;
  2170. Slapi_DN *remote_dn = NULL;
  2171. int missing_entry = 0;
  2172. const Slapi_DN *local_dn = slapi_entry_get_sdn_const(e);
  2173. /* First check if the entry is for us */
  2174. is_ours = is_subject_of_agreemeent_local(e, prp->agmt);
  2175. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2176. "%s: windows_process_total_entry: Looking dn=\"%s\" (%s)\n",
  2177. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)), is_ours ? "ours" : "not ours");
  2178. if (is_ours)
  2179. {
  2180. retval = map_entry_dn_outbound(e,&remote_dn,prp,&missing_entry,1 /* want GUID */);
  2181. if (retval || NULL == remote_dn)
  2182. {
  2183. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2184. "%s: windows_replay_update: failed map dn for total update dn=\"%s\"\n",
  2185. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(local_dn));
  2186. goto error;
  2187. }
  2188. /* Either the entry is a tombstone, or not a tombstone */
  2189. if (is_tombstone)
  2190. {
  2191. retval = windows_process_total_delete(prp,e,remote_dn);
  2192. } else
  2193. {
  2194. retval = windows_process_total_add(prp,e,remote_dn,missing_entry);
  2195. }
  2196. }
  2197. if (remote_dn)
  2198. {
  2199. slapi_sdn_free(&remote_dn);
  2200. }
  2201. error:
  2202. return retval;
  2203. }
  2204. int
  2205. windows_search_local_entry_by_uniqueid(Private_Repl_Protocol *prp, const char *uniqueid, char ** attrs, Slapi_Entry **ret_entry , void * component_identity)
  2206. {
  2207. Slapi_Entry **entries = NULL;
  2208. Slapi_PBlock *int_search_pb = NULL;
  2209. int rc = 0;
  2210. char *filter_string = NULL;
  2211. const Slapi_DN *local_subtree = NULL;
  2212. *ret_entry = NULL;
  2213. local_subtree = windows_private_get_directory_subtree(prp->agmt);
  2214. filter_string = PR_smprintf("(&(|(objectclass=*)(objectclass=ldapsubentry)(objectclass=nsTombstone))(nsUniqueid=%s))",uniqueid);
  2215. int_search_pb = slapi_pblock_new ();
  2216. slapi_search_internal_set_pb ( int_search_pb, slapi_sdn_get_dn(local_subtree), LDAP_SCOPE_SUBTREE, filter_string,
  2217. attrs ,
  2218. 0 /* attrsonly */, NULL /* controls */,
  2219. NULL /* uniqueid */,
  2220. component_identity, 0 /* actions */ );
  2221. slapi_search_internal_pb ( int_search_pb );
  2222. slapi_pblock_get( int_search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc );
  2223. if ( LDAP_SUCCESS == rc ) {
  2224. slapi_pblock_get( int_search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
  2225. if ( NULL != entries && NULL != entries[ 0 ]) {
  2226. Slapi_Entry *temp_entry = NULL;
  2227. temp_entry = entries[ 0 ];
  2228. *ret_entry = slapi_entry_dup(temp_entry);
  2229. } else {
  2230. /* No entry there */
  2231. rc = LDAP_NO_SUCH_OBJECT;
  2232. }
  2233. }
  2234. slapi_free_search_results_internal(int_search_pb);
  2235. slapi_pblock_destroy(int_search_pb);
  2236. int_search_pb = NULL;
  2237. if (filter_string)
  2238. {
  2239. PR_smprintf_free(filter_string);
  2240. }
  2241. return rc;
  2242. }
  2243. static int
  2244. windows_get_local_entry_by_uniqueid(Private_Repl_Protocol *prp,const char* uniqueid,Slapi_Entry **local_entry)
  2245. {
  2246. int retval = ENTRY_NOTFOUND;
  2247. Slapi_Entry *new_entry = NULL;
  2248. windows_search_local_entry_by_uniqueid( prp, uniqueid, NULL, &new_entry,
  2249. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  2250. if (new_entry)
  2251. {
  2252. *local_entry = new_entry;
  2253. retval = 0;
  2254. }
  2255. return retval;
  2256. }
  2257. static int
  2258. windows_get_local_entry(const Slapi_DN* local_dn,Slapi_Entry **local_entry)
  2259. {
  2260. int retval = ENTRY_NOTFOUND;
  2261. Slapi_Entry *new_entry = NULL;
  2262. slapi_search_internal_get_entry( (Slapi_DN*)local_dn, NULL, &new_entry,
  2263. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  2264. if (new_entry)
  2265. {
  2266. *local_entry = new_entry;
  2267. retval = 0;
  2268. }
  2269. return retval;
  2270. }
  2271. static int
  2272. windows_process_dirsync_entry(Private_Repl_Protocol *prp,Slapi_Entry *e, int is_total)
  2273. {
  2274. Slapi_DN* local_sdn = NULL;
  2275. int rc = 0;
  2276. /* deleted users are outside the 'correct container'.
  2277. They live in cn=deleted objects, windows_private_get_directory_subtree( prp->agmt) */
  2278. if (is_tombstone(e))
  2279. {
  2280. rc = map_tombstone_dn_inbound(e, &local_sdn, prp->agmt);
  2281. if ((0 == rc) && local_sdn)
  2282. {
  2283. /* Go ahead and delte the local peer */
  2284. rc = windows_delete_local_entry(local_sdn);
  2285. slapi_sdn_free(&local_sdn);
  2286. } else
  2287. {
  2288. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"%s: windows_process_dirsync_entry: failed to map tombstone dn.\n",agmt_get_long_name(prp->agmt));
  2289. }
  2290. } else
  2291. {
  2292. /* Is this entry one we should be interested in ? */
  2293. if (is_subject_of_agreemeent_remote(e,prp->agmt))
  2294. {
  2295. /* First make its local DN */
  2296. rc = map_entry_dn_inbound(e, &local_sdn, prp->agmt);
  2297. if ((0 == rc) && local_sdn)
  2298. {
  2299. Slapi_Entry *local_entry = NULL;
  2300. /* Get the local entry if it exists */
  2301. rc = windows_get_local_entry(local_sdn,&local_entry);
  2302. if ((0 == rc) && local_entry)
  2303. {
  2304. /* Since the entry exists we should now make it match the entry we received from AD */
  2305. rc = windows_update_local_entry(prp, e, local_entry);
  2306. slapi_entry_free(local_entry);
  2307. if (rc) {
  2308. /* Something bad happened */
  2309. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"%s: windows_process_dirsync_entry: failed to update inbound entry.\n",agmt_get_long_name(prp->agmt));
  2310. }
  2311. } else
  2312. {
  2313. /* If it doesn't exist, try to make it */
  2314. windows_create_local_entry(prp,e,local_sdn);
  2315. }
  2316. slapi_sdn_free(&local_sdn);
  2317. } else
  2318. {
  2319. /* We should have been able to map the DN, so this is an error */
  2320. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"%s: windows_process_dirsync_entry: failed to map inbound entry.\n",agmt_get_long_name(prp->agmt));
  2321. }
  2322. } /* subject of agreement */
  2323. } /* is tombstone */
  2324. return rc;
  2325. }
  2326. void
  2327. windows_dirsync_inc_run(Private_Repl_Protocol *prp)
  2328. {
  2329. int rc = 0;
  2330. int msgid=0;
  2331. Slapi_PBlock *pb = NULL;
  2332. Slapi_Filter *filter_user = NULL;
  2333. Slapi_Filter *filter_user_deleted = NULL;
  2334. Slapi_Filter *filter_group = NULL;
  2335. Slapi_Filter *filter_group_deleted = NULL;
  2336. int done = 0;
  2337. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_dirsync_inc_run\n", 0, 0, 0 );
  2338. while (!done) {
  2339. Slapi_Entry *e = NULL;
  2340. int filter_ret = 0;
  2341. PRBool create_users_from_dirsync = windows_private_create_users(prp->agmt);
  2342. rc = send_dirsync_search(prp->conn);
  2343. if (rc != CONN_OPERATION_SUCCESS)
  2344. {
  2345. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  2346. "failed to send dirsync search request: %d\n", rc);
  2347. goto error;
  2348. }
  2349. while ( (e = windows_conn_get_search_result(prp->conn) ) != NULL)
  2350. {
  2351. rc = windows_process_dirsync_entry(prp,e,0);
  2352. if (e)
  2353. {
  2354. slapi_entry_free(e);
  2355. }
  2356. } /* While entry != NULL */
  2357. if (!windows_private_dirsync_has_more(prp->agmt))
  2358. {
  2359. done = 1;
  2360. }
  2361. } /* While !done */
  2362. error:
  2363. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_dirsync_inc_run\n", 0, 0, 0 );
  2364. }