1
0

windows_protocol_util.c 76 KB

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