windows_protocol_util.c 81 KB

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