windows_protocol_util.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* repl5_protocol_util.c */
  42. /*
  43. Code common to both incremental and total protocols.
  44. */
  45. #include "repl5.h"
  46. #include "repl5_prot_private.h"
  47. #include "windowsrepl.h"
  48. #include "slap.h"
  49. #include <unicode/ustring.h> /* UTF8 conversion */
  50. int ruv_private_new( RUV **ruv, RUV *clone );
  51. static Slapi_Entry* windows_entry_already_exists(Slapi_Entry *e);
  52. static void windows_dirsync_now (Private_Repl_Protocol *prp);
  53. static Slapi_DN* map_dn_user(Slapi_DN *sdn, int map_to, const Slapi_DN *root);
  54. static Slapi_DN* map_dn_group(Slapi_DN *sdn, int map_to, const Slapi_DN *root);
  55. static void make_mods_from_entries(Slapi_Entry *new_entry, Slapi_Entry *existing_entry, LDAPMod ***attrs);
  56. static void windows_map_mods_for_replay(Private_Repl_Protocol *prp,LDAPMod **original_mods, LDAPMod ***returned_mods, int is_user, char** password);
  57. static int is_subject_of_agreement_local(const Slapi_Entry *local_entry,const Repl_Agmt *ra);
  58. static int windows_create_remote_entry(Private_Repl_Protocol *prp,Slapi_Entry *original_entry, Slapi_DN *remote_sdn, Slapi_Entry **remote_entry, char** password);
  59. static int windows_get_local_entry(const Slapi_DN* local_dn,Slapi_Entry **local_entry);
  60. static int windows_get_local_entry_by_uniqueid(Private_Repl_Protocol *prp,const char* uniqueid,Slapi_Entry **local_entry);
  61. static int windows_get_local_tombstone_by_uniqueid(Private_Repl_Protocol *prp,const char* uniqueid,Slapi_Entry **local_entry);
  62. static int windows_search_local_entry_by_uniqueid(Private_Repl_Protocol *prp, const char *uniqueid, char ** attrs, Slapi_Entry **ret_entry, int tombstone, void * component_identity);
  63. static int map_entry_dn_outbound(Slapi_Entry *e, Slapi_DN **dn, Private_Repl_Protocol *prp, int *missing_entry, int want_guid);
  64. static char* extract_ntuserdomainid_from_entry(Slapi_Entry *e);
  65. static char* extract_container(const Slapi_DN *entry_dn, const Slapi_DN *suffix_dn);
  66. static int windows_get_remote_entry (Private_Repl_Protocol *prp, const Slapi_DN* remote_dn,Slapi_Entry **remote_entry);
  67. static int windows_get_remote_tombstone(Private_Repl_Protocol *prp, const Slapi_DN* remote_dn,Slapi_Entry **remote_entry);
  68. static int windows_reanimate_tombstone(Private_Repl_Protocol *prp, const Slapi_DN* tombstone_dn, const char* new_dn);
  69. static const char* op2string (int op);
  70. static int is_subject_of_agreement_remote(Slapi_Entry *e, const Repl_Agmt *ra);
  71. static int map_entry_dn_inbound(Slapi_Entry *e, Slapi_DN **dn, const Repl_Agmt *ra);
  72. static int windows_update_remote_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,Slapi_Entry *local_entry);
  73. static int is_guid_dn(Slapi_DN *remote_dn);
  74. static int map_windows_tombstone_dn(Slapi_Entry *e, Slapi_DN **dn, Private_Repl_Protocol *prp, int *exists);
  75. /* Controls the direction of flow for mapped attributes */
  76. typedef enum mapping_types {
  77. bidirectional,
  78. fromwindowsonly,
  79. towindowsonly,
  80. disabled
  81. } mapping_types;
  82. /* Controls if we sync the attibute always, or only when we're creating new entries */
  83. /* Used for attributes like samaccountname, where we want to fill it in on a new entry, but
  84. * we never want to change it on an existing entry */
  85. typedef enum creation_types {
  86. always,
  87. createonly
  88. } creation_types;
  89. typedef enum attr_types {
  90. normal,
  91. dnmap
  92. } attr_types;
  93. typedef struct _windows_attribute_map
  94. {
  95. char *windows_attribute_name;
  96. char *ldap_attribute_name;
  97. mapping_types map_type;
  98. creation_types create_type;
  99. attr_types attr_type;
  100. } windows_attribute_map;
  101. /* List of attributes that are common to AD and LDAP, so we simply copy them over in both directions */
  102. static char* windows_user_matching_attributes[] =
  103. {
  104. "description",
  105. "destinationIndicator",
  106. "facsimileTelephoneNumber",
  107. "givenName",
  108. "homePhone",
  109. "homePostalAddress",
  110. "initials",
  111. "l",
  112. "mail",
  113. "mobile",
  114. "o",
  115. "ou",
  116. "pager",
  117. "physicalDeliveryOfficeName",
  118. "postOfficeBox",
  119. "postalAddress",
  120. "postalCode",
  121. "registeredAddress",
  122. "sn",
  123. "st",
  124. "telephoneNumber",
  125. "teletexTerminalIdentifier",
  126. "telexNumber",
  127. "title",
  128. "userCertificate",
  129. "x121Address",
  130. NULL
  131. };
  132. static char* windows_group_matching_attributes[] =
  133. {
  134. "description",
  135. "destinationIndicator",
  136. "facsimileTelephoneNumber",
  137. "givenName",
  138. "homePhone",
  139. "homePostalAddress",
  140. "initials",
  141. "l",
  142. "mail",
  143. "manager",
  144. "mobile",
  145. "o",
  146. "ou",
  147. "pager",
  148. "physicalDeliveryOfficeName",
  149. "postOfficeBox",
  150. "postalAddress",
  151. "postalCode",
  152. "preferredDeliveryMethod",
  153. "registeredAddress",
  154. "sn",
  155. "st",
  156. "telephoneNumber",
  157. "teletexTerminalIdentifier",
  158. "telexNumber",
  159. "title",
  160. "userCertificate",
  161. "x121Address",
  162. NULL
  163. };
  164. /* List of attributes that are common to AD and LDAP, so we simply copy them over in both directions */
  165. static char* nt4_user_matching_attributes[] =
  166. {
  167. "description",
  168. NULL
  169. };
  170. static char* nt4_group_matching_attributes[] =
  171. {
  172. "description",
  173. NULL
  174. };
  175. static windows_attribute_map user_attribute_map[] =
  176. {
  177. { "homeDirectory", "ntUserHomeDir", bidirectional, always, normal},
  178. { "scriptPath", "ntUserScriptPath", bidirectional, always, normal},
  179. { "lastLogon", "ntUserLastLogon", fromwindowsonly, always, normal},
  180. { "lastLogoff", "ntUserLastLogoff", fromwindowsonly, always, normal},
  181. { "accountExpires", "ntUserAcctExpires", bidirectional, always, normal},
  182. { "codePage", "ntUserCodePage", bidirectional, always, normal},
  183. { "logonHours", "ntUserLogonHours", bidirectional, always, normal},
  184. { "maxStorage", "ntUserMaxStorage", bidirectional, always, normal},
  185. { "profilePath", "ntUserProfile", bidirectional, always, normal},
  186. /* IETF schema has 'street' and 'streetaddress' as aliases, but Microsoft does not */
  187. { "streetAddress", "street", towindowsonly, always, normal},
  188. { FAKE_STREET_ATTR_NAME, "street", fromwindowsonly, always, normal},
  189. { "userParameters", "ntUserParms", bidirectional, always, normal},
  190. { "userWorkstations", "ntUserWorkstations", bidirectional, always, normal},
  191. { "sAMAccountName", "ntUserDomainId", bidirectional, always, normal},
  192. /* cn is a naming attribute in AD, so we don't want to change it after entry creation */
  193. { "cn", "cn", towindowsonly, createonly, normal},
  194. /* However, it isn't a naming attribute in DS (we use uid) so it's safe to accept changes inbound */
  195. { "name", "cn", fromwindowsonly, always, normal},
  196. { "manager", "manager", bidirectional, always, dnmap},
  197. { "seealso", "seealso", bidirectional, always, dnmap},
  198. {NULL, NULL, -1}
  199. };
  200. static windows_attribute_map group_attribute_map[] =
  201. {
  202. { "groupType", "ntGroupType", bidirectional, createonly, normal},
  203. { "sAMAccountName", "ntUserDomainId", bidirectional, always, normal},
  204. /* IETF schema has 'street' and 'streetaddress' as aliases, but Microsoft does not */
  205. { "streetAddress", "street", towindowsonly, always, normal},
  206. { FAKE_STREET_ATTR_NAME, "street", fromwindowsonly, always, normal},
  207. { "member", "uniquemember", bidirectional, always, dnmap},
  208. {NULL, NULL, -1}
  209. };
  210. /*
  211. * Notes on differences for NT4:
  212. * 1. NT4 returns the SID value in the objectGUID attribute value.
  213. * The SID has variable length and does not match the length of a GUID.
  214. * 2. NT4 currently never generates tombstones. If it did, we'd need to parse the
  215. * different form of the GUID in the tombstone DNs.
  216. * 3. NT4 Does not implement the dirsync control. We always get all users and groups.
  217. * 4. NT4 generates and expects DNs with samaccountname as the RDN, not cn.
  218. * 5. NT4 handles the DN=<GUID> (remember that the '<' '>' characters are included!) DN form
  219. * for modifies and deletes, provided we use the value it gave us in the objectGUID attribute (which is actually the SID).
  220. * 6. NT4 has less and different schema from AD. For example users in NT4 have no firstname/lastname, only an optional 'description'.
  221. */
  222. /*
  223. * When we get an error from an LDAP operation, we call this
  224. * function to decide if we should just keep replaying
  225. * updates, or if we should stop, back off, and try again
  226. * later.
  227. * Returns PR_TRUE if we shoould keep going, PR_FALSE if
  228. * we should back off and try again later.
  229. *
  230. * In general, we keep going if the return code is consistent
  231. * with some sort of bug in URP that causes the consumer to
  232. * emit an error code that it shouldn't have, e.g. LDAP_ALREADY_EXISTS.
  233. *
  234. * We stop if there's some indication that the server just completely
  235. * failed to process the operation, e.g. LDAP_OPERATIONS_ERROR.
  236. */
  237. PRBool
  238. windows_ignore_error_and_keep_going(int error)
  239. {
  240. int return_value;
  241. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_ignore_error_and_keep_going\n", 0, 0, 0 );
  242. switch (error)
  243. {
  244. /* Cases where we keep going */
  245. case LDAP_SUCCESS:
  246. case LDAP_NO_SUCH_ATTRIBUTE:
  247. case LDAP_UNDEFINED_TYPE:
  248. case LDAP_CONSTRAINT_VIOLATION:
  249. case LDAP_TYPE_OR_VALUE_EXISTS:
  250. case LDAP_INVALID_SYNTAX:
  251. case LDAP_NO_SUCH_OBJECT:
  252. case LDAP_INVALID_DN_SYNTAX:
  253. case LDAP_IS_LEAF:
  254. case LDAP_INSUFFICIENT_ACCESS:
  255. case LDAP_NAMING_VIOLATION:
  256. case LDAP_OBJECT_CLASS_VIOLATION:
  257. case LDAP_NOT_ALLOWED_ON_NONLEAF:
  258. case LDAP_NOT_ALLOWED_ON_RDN:
  259. case LDAP_ALREADY_EXISTS:
  260. case LDAP_NO_OBJECT_CLASS_MODS:
  261. return_value = PR_TRUE;
  262. break;
  263. /* Cases where we stop and retry */
  264. case LDAP_OPERATIONS_ERROR:
  265. case LDAP_PROTOCOL_ERROR:
  266. case LDAP_TIMELIMIT_EXCEEDED:
  267. case LDAP_SIZELIMIT_EXCEEDED:
  268. case LDAP_STRONG_AUTH_NOT_SUPPORTED:
  269. case LDAP_STRONG_AUTH_REQUIRED:
  270. case LDAP_PARTIAL_RESULTS:
  271. case LDAP_REFERRAL:
  272. case LDAP_ADMINLIMIT_EXCEEDED:
  273. case LDAP_UNAVAILABLE_CRITICAL_EXTENSION:
  274. case LDAP_CONFIDENTIALITY_REQUIRED:
  275. case LDAP_SASL_BIND_IN_PROGRESS:
  276. case LDAP_INAPPROPRIATE_MATCHING:
  277. case LDAP_ALIAS_PROBLEM:
  278. case LDAP_ALIAS_DEREF_PROBLEM:
  279. case LDAP_INAPPROPRIATE_AUTH:
  280. case LDAP_INVALID_CREDENTIALS:
  281. case LDAP_BUSY:
  282. case LDAP_UNAVAILABLE:
  283. case LDAP_UNWILLING_TO_PERFORM:
  284. case LDAP_LOOP_DETECT:
  285. case LDAP_SORT_CONTROL_MISSING:
  286. case LDAP_INDEX_RANGE_ERROR:
  287. case LDAP_RESULTS_TOO_LARGE:
  288. case LDAP_AFFECTS_MULTIPLE_DSAS:
  289. case LDAP_OTHER:
  290. case LDAP_SERVER_DOWN:
  291. case LDAP_LOCAL_ERROR:
  292. case LDAP_ENCODING_ERROR:
  293. case LDAP_DECODING_ERROR:
  294. case LDAP_TIMEOUT:
  295. case LDAP_AUTH_UNKNOWN:
  296. case LDAP_FILTER_ERROR:
  297. case LDAP_USER_CANCELLED:
  298. case LDAP_PARAM_ERROR:
  299. case LDAP_NO_MEMORY:
  300. case LDAP_CONNECT_ERROR:
  301. case LDAP_NOT_SUPPORTED:
  302. case LDAP_CONTROL_NOT_FOUND:
  303. case LDAP_NO_RESULTS_RETURNED:
  304. case LDAP_MORE_RESULTS_TO_RETURN:
  305. case LDAP_CLIENT_LOOP:
  306. case LDAP_REFERRAL_LIMIT_EXCEEDED:
  307. return_value = PR_FALSE;
  308. break;
  309. }
  310. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_ignore_error_and_keep_going\n", 0, 0, 0 );
  311. return return_value;
  312. }
  313. static const char*
  314. op2string(int op)
  315. {
  316. LDAPDebug( LDAP_DEBUG_TRACE, "=> op2string\n", 0, 0, 0 );
  317. LDAPDebug( LDAP_DEBUG_TRACE, "<= op2string\n", 0, 0, 0 );
  318. switch (op) {
  319. case SLAPI_OPERATION_ADD:
  320. return "add";
  321. case SLAPI_OPERATION_MODIFY:
  322. return "modify";
  323. case SLAPI_OPERATION_DELETE:
  324. return "delete";
  325. case SLAPI_OPERATION_MODRDN:
  326. return "rename";
  327. case SLAPI_OPERATION_EXTENDED:
  328. return "extended";
  329. }
  330. return "unknown";
  331. }
  332. static void
  333. windows_dump_entry(const char *string, Slapi_Entry *e)
  334. {
  335. int length = 0;
  336. char *buffer = NULL;
  337. if (slapi_is_loglevel_set(SLAPI_LOG_REPL))
  338. {
  339. buffer = slapi_entry2str(e,&length);
  340. slapi_log_error(SLAPI_LOG_REPL, NULL, "Windows sync entry: %s %s\n", string, buffer);
  341. if (buffer)
  342. {
  343. slapi_ch_free_string(&buffer);
  344. }
  345. }
  346. }
  347. static void
  348. map_dn_values(Private_Repl_Protocol *prp,Slapi_ValueSet *original_values, Slapi_ValueSet **mapped_values, int to_windows, int return_originals)
  349. {
  350. Slapi_ValueSet *new_vs = NULL;
  351. Slapi_Value *original_value = NULL;
  352. int retval = 0;
  353. int i = 0;
  354. /* For each value: */
  355. i= slapi_valueset_first_value(original_values,&original_value);
  356. while ( i != -1 ) {
  357. int is_ours = 0;
  358. char *new_dn_string = NULL;
  359. const char *original_dn_string = NULL;
  360. int original_dn_string_length = 0;
  361. Slapi_DN *original_dn = NULL;
  362. original_dn_string = slapi_value_get_string(original_value);
  363. /* Sanity check the data was a valid string */
  364. original_dn_string_length = slapi_value_get_length(original_value);
  365. /* Make a sdn from the string */
  366. original_dn = slapi_sdn_new_dn_byref(original_dn_string);
  367. if (to_windows)
  368. {
  369. Slapi_Entry *local_entry = NULL;
  370. /* Try to get the local entry */
  371. retval = windows_get_local_entry(original_dn,&local_entry);
  372. if (0 == retval && local_entry)
  373. {
  374. int missing_entry = 0;
  375. Slapi_DN *remote_dn = NULL;
  376. /* Now map the DN */
  377. is_ours = is_subject_of_agreement_local(local_entry,prp->agmt);
  378. if (is_ours)
  379. {
  380. map_entry_dn_outbound(local_entry,&remote_dn,prp,&missing_entry, 0 /* don't want GUID form here */);
  381. if (remote_dn)
  382. {
  383. if (!missing_entry)
  384. {
  385. /* Success */
  386. if (return_originals)
  387. {
  388. new_dn_string = slapi_ch_strdup(slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry)));
  389. } else
  390. {
  391. new_dn_string = slapi_ch_strdup(slapi_sdn_get_dn(remote_dn));
  392. }
  393. }
  394. slapi_sdn_free(&remote_dn);
  395. } else
  396. {
  397. slapi_log_error(SLAPI_LOG_REPL, NULL, "map_dn_values: no remote dn found for %s\n", original_dn_string);
  398. }
  399. } else
  400. {
  401. slapi_log_error(SLAPI_LOG_REPL, NULL, "map_dn_values: this entry is not ours %s\n", original_dn_string);
  402. }
  403. } else {
  404. slapi_log_error(SLAPI_LOG_REPL, NULL, "map_dn_values: no local entry found for %s\n", original_dn_string);
  405. }
  406. if (local_entry)
  407. {
  408. slapi_entry_free(local_entry);
  409. local_entry = NULL;
  410. }
  411. } else
  412. {
  413. Slapi_Entry *remote_entry = NULL;
  414. Slapi_DN *local_dn = NULL;
  415. /* Try to get the remote entry */
  416. retval = windows_get_remote_entry(prp,original_dn,&remote_entry);
  417. if (remote_entry && 0 == retval)
  418. {
  419. is_ours = is_subject_of_agreement_remote(remote_entry,prp->agmt);
  420. if (is_ours)
  421. {
  422. retval = map_entry_dn_inbound(remote_entry,&local_dn,prp->agmt);
  423. if (0 == retval && local_dn)
  424. {
  425. if (return_originals)
  426. {
  427. new_dn_string = slapi_ch_strdup(slapi_sdn_get_dn(slapi_entry_get_sdn_const(remote_entry)));
  428. } else
  429. {
  430. new_dn_string = slapi_ch_strdup(slapi_sdn_get_dn(local_dn));
  431. }
  432. slapi_sdn_free(&local_dn);
  433. } else
  434. {
  435. slapi_log_error(SLAPI_LOG_REPL, NULL, "map_dn_values: no local dn found for %s\n", original_dn_string);
  436. }
  437. } else
  438. {
  439. slapi_log_error(SLAPI_LOG_REPL, NULL, "map_dn_values: this entry is not ours %s\n", original_dn_string);
  440. }
  441. } else
  442. {
  443. slapi_log_error(SLAPI_LOG_REPL, NULL, "map_dn_values: no remote entry found for %s\n", original_dn_string);
  444. }
  445. if (remote_entry)
  446. {
  447. slapi_entry_free(remote_entry);
  448. remote_entry = NULL;
  449. }
  450. }
  451. /* Extract the dn string and store in the new value */
  452. if (new_dn_string)
  453. {
  454. Slapi_Value *new_value = NULL;
  455. if (NULL == new_vs)
  456. {
  457. new_vs = slapi_valueset_new();
  458. }
  459. new_value = slapi_value_new_string_passin(new_dn_string);
  460. slapi_valueset_add_value(new_vs,new_value);
  461. slapi_value_free(&new_value);
  462. }
  463. /* If not then we skip it */
  464. i = slapi_valueset_next_value(original_values,i,&original_value);
  465. if (original_dn)
  466. {
  467. slapi_sdn_free(&original_dn);
  468. }
  469. }/* while */
  470. if (new_vs)
  471. {
  472. *mapped_values = new_vs;
  473. }
  474. }
  475. static void
  476. windows_dump_ruvs(Object *supl_ruv_obj, Object *cons_ruv_obj)
  477. {
  478. if (slapi_is_loglevel_set(SLAPI_LOG_REPL))
  479. {
  480. slapi_log_error(SLAPI_LOG_REPL, NULL, "acquire_replica, supplier RUV:\n");
  481. if (supl_ruv_obj) {
  482. RUV* sup = NULL;
  483. object_acquire(supl_ruv_obj);
  484. sup = (RUV*) object_get_data ( supl_ruv_obj );
  485. ruv_dump (sup, "supplier", NULL);
  486. object_release(supl_ruv_obj);
  487. } else
  488. {
  489. slapi_log_error(SLAPI_LOG_REPL, NULL, "acquire_replica, supplier RUV = null\n");
  490. }
  491. slapi_log_error(SLAPI_LOG_REPL, NULL, "acquire_replica, consumer RUV:\n");
  492. if (cons_ruv_obj)
  493. {
  494. RUV* con = NULL;
  495. object_acquire(cons_ruv_obj);
  496. con = (RUV*) object_get_data ( cons_ruv_obj );
  497. ruv_dump (con,"consumer", NULL);
  498. object_release( cons_ruv_obj );
  499. } else {
  500. slapi_log_error(SLAPI_LOG_REPL, NULL, "acquire_replica, consumer RUV = null\n");
  501. }
  502. }
  503. }
  504. /*
  505. * Acquire exclusive access to a replica. Send a start replication extended
  506. * operation to the replica. The response will contain a success code, and
  507. * optionally the replica's update vector if acquisition is successful.
  508. * This function returns one of the following:
  509. * ACQUIRE_SUCCESS - the replica was acquired, and we have exclusive update access
  510. * ACQUIRE_REPLICA_BUSY - another master was updating the replica
  511. * ACQUIRE_FATAL_ERROR - something bad happened, and it's not likely to improve
  512. * if we wait.
  513. * ACQUIRE_TRANSIENT_ERROR - something bad happened, but it's probably worth
  514. * another try after waiting a while.
  515. * If ACQUIRE_SUCCESS is returned, then ruv will point to the replica's update
  516. * vector. It's possible that the replica does something goofy and doesn't
  517. * return us an update vector, so be prepared for ruv to be NULL (but this is
  518. * an error).
  519. */
  520. int
  521. windows_acquire_replica(Private_Repl_Protocol *prp, RUV **ruv, int check_ruv)
  522. {
  523. int return_value = ACQUIRE_SUCCESS;
  524. ConnResult crc = 0;
  525. Repl_Connection *conn = NULL;
  526. Replica *replica = NULL;
  527. Object *supl_ruv_obj, *cons_ruv_obj = NULL;
  528. PRBool is_newer = PR_FALSE;
  529. RUV *r = NULL;
  530. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_acquire_replica\n", 0, 0, 0 );
  531. PR_ASSERT(prp);
  532. if (prp->replica_acquired) /* we already acquire replica */
  533. {
  534. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  535. "%s: Remote replica already acquired\n",
  536. agmt_get_long_name(prp->agmt));
  537. return_value = ACQUIRE_FATAL_ERROR;
  538. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_acquire_replica\n", 0, 0, 0 );
  539. return ACQUIRE_SUCCESS;
  540. }
  541. if (NULL != ruv)
  542. {
  543. ruv_destroy ( ruv );
  544. }
  545. object_acquire(prp->replica_object);
  546. replica = object_get_data(prp->replica_object);
  547. supl_ruv_obj = replica_get_ruv ( replica );
  548. cons_ruv_obj = agmt_get_consumer_ruv(prp->agmt);
  549. windows_dump_ruvs(supl_ruv_obj,cons_ruv_obj);
  550. is_newer = ruv_is_newer ( supl_ruv_obj, cons_ruv_obj );
  551. if (is_newer)
  552. {
  553. slapi_log_error(SLAPI_LOG_REPL, NULL, "acquire_replica, supplier RUV is newer\n");
  554. }
  555. /* Handle the pristine case */
  556. if (cons_ruv_obj == NULL)
  557. {
  558. *ruv = NULL;
  559. } else
  560. {
  561. r = (RUV*) object_get_data(cons_ruv_obj);
  562. *ruv = ruv_dup(r);
  563. }
  564. if ( supl_ruv_obj ) object_release ( supl_ruv_obj );
  565. if ( cons_ruv_obj ) object_release ( cons_ruv_obj );
  566. object_release (prp->replica_object);
  567. replica = NULL;
  568. /* Once we get here we have a valid ruv */
  569. if (is_newer == PR_FALSE && check_ruv) {
  570. prp->last_acquire_response_code = NSDS50_REPL_UPTODATE;
  571. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_acquire_replica - ACQUIRE_CONSUMER_WAS_UPTODATE\n", 0, 0, 0 );
  572. return ACQUIRE_CONSUMER_WAS_UPTODATE;
  573. }
  574. prp->last_acquire_response_code = NSDS50_REPL_REPLICA_NO_RESPONSE;
  575. /* Get the connection */
  576. conn = prp->conn;
  577. crc = windows_conn_connect(conn);
  578. if (CONN_OPERATION_FAILED == crc)
  579. {
  580. return_value = ACQUIRE_TRANSIENT_ERROR;
  581. }
  582. else if (CONN_SSL_NOT_ENABLED == crc)
  583. {
  584. return_value = ACQUIRE_FATAL_ERROR;
  585. }
  586. else
  587. {
  588. /* we don't want the timer to go off in the middle of an operation */
  589. windows_conn_cancel_linger(conn);
  590. /* Does the remote replica support the dirsync protocol?
  591. if it update the conn object */
  592. windows_conn_replica_supports_dirsync(conn);
  593. if (CONN_NOT_CONNECTED == crc || CONN_OPERATION_FAILED == crc)
  594. {
  595. /* We don't know anything about the remote replica. Try again later. */
  596. return_value = ACQUIRE_TRANSIENT_ERROR;
  597. }
  598. else
  599. {
  600. /* Good to go. Start the protocol. */
  601. CSN *current_csn = NULL;
  602. Slapi_DN *replarea_sdn;
  603. /* Obtain a current CSN */
  604. replarea_sdn = agmt_get_replarea(prp->agmt);
  605. current_csn = get_current_csn(replarea_sdn);
  606. if (NULL != current_csn)
  607. {
  608. return_value = ACQUIRE_SUCCESS;
  609. }
  610. else
  611. {
  612. /* Couldn't get a current CSN */
  613. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  614. "%s: Unable to obtain current CSN. "
  615. "Replication is aborting.\n",
  616. agmt_get_long_name(prp->agmt));
  617. return_value = ACQUIRE_FATAL_ERROR;
  618. }
  619. slapi_sdn_free(&replarea_sdn);
  620. }
  621. }
  622. if (ACQUIRE_SUCCESS != return_value)
  623. {
  624. /* could not acquire the replica, so reinstate the linger timer, since this
  625. means we won't call release_replica, which also reinstates the timer */
  626. windows_conn_start_linger(conn);
  627. }
  628. else
  629. {
  630. /* replica successfully acquired */
  631. prp->replica_acquired = PR_TRUE;
  632. }
  633. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_acquire_replica\n", 0, 0, 0 );
  634. return return_value;
  635. }
  636. void
  637. windows_release_replica(Private_Repl_Protocol *prp)
  638. {
  639. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_release_replica\n", 0, 0, 0 );
  640. PR_ASSERT(NULL != prp);
  641. PR_ASSERT(NULL != prp->conn);
  642. if (!prp->replica_acquired)
  643. return;
  644. windows_conn_start_linger(prp->conn);
  645. prp->replica_acquired = PR_FALSE;
  646. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_release_replica\n", 0, 0, 0 );
  647. }
  648. static void
  649. to_little_endian_double_bytes(UChar *unicode_password, int32_t unicode_password_length)
  650. {
  651. int32_t i = 0;
  652. for (i = 0 ; i < unicode_password_length; i++)
  653. {
  654. UChar c = unicode_password[i];
  655. char *byte_ptr = (char*)&(unicode_password[i]);
  656. byte_ptr[0] = (char)(c & 0xff);
  657. byte_ptr[1] = (char)(c >> 8);
  658. }
  659. }
  660. /* this entry had a password, handle it seperately */
  661. /* http://support.microsoft.com/?kbid=269190 */
  662. static int
  663. send_password_modify(Slapi_DN *sdn, char *password, Private_Repl_Protocol *prp)
  664. {
  665. ConnResult pw_return = 0;
  666. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  667. if (is_nt4)
  668. {
  669. /* NT4 just wants a plaintext password */
  670. Slapi_Mods smods = {0};
  671. slapi_mods_init (&smods, 0);
  672. slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "UnicodePwd", password);
  673. pw_return = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(sdn), slapi_mods_get_ldapmods_byref(&smods), NULL, NULL );
  674. slapi_mods_done(&smods);
  675. } else
  676. {
  677. char *quoted_password = NULL;
  678. /* AD wants the password in quotes ! */
  679. quoted_password = PR_smprintf("\"%s\"",password);
  680. if (quoted_password)
  681. {
  682. LDAPMod *pw_mods[2];
  683. LDAPMod pw_mod;
  684. struct berval bv = {0};
  685. UChar *unicode_password = NULL;
  686. int32_t unicode_password_length = 0; /* Length in _characters_ */
  687. int32_t buffer_size = 0; /* Size in _characters_ */
  688. UErrorCode error = U_ZERO_ERROR;
  689. struct berval *bvals[2];
  690. /* Need to UNICODE encode the password here */
  691. /* It's one of those 'ask me first and I will tell you the buffer size' functions */
  692. u_strFromUTF8(NULL, 0, &unicode_password_length, quoted_password, strlen(quoted_password), &error);
  693. buffer_size = unicode_password_length;
  694. unicode_password = (UChar *)slapi_ch_malloc(unicode_password_length * sizeof(UChar));
  695. if (unicode_password) {
  696. error = U_ZERO_ERROR;
  697. u_strFromUTF8(unicode_password, buffer_size, &unicode_password_length, quoted_password, strlen(quoted_password), &error);
  698. /* As an extra special twist, we need to send the unicode in little-endian order for AD to be happy */
  699. to_little_endian_double_bytes(unicode_password, unicode_password_length);
  700. bv.bv_len = unicode_password_length * sizeof(UChar);
  701. bv.bv_val = (char*)unicode_password;
  702. bvals[0] = &bv;
  703. bvals[1] = NULL;
  704. pw_mod.mod_type = "UnicodePwd";
  705. pw_mod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  706. pw_mod.mod_bvalues = bvals;
  707. pw_mods[0] = &pw_mod;
  708. pw_mods[1] = NULL;
  709. pw_return = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(sdn), pw_mods, NULL, NULL );
  710. slapi_ch_free((void**)&unicode_password);
  711. }
  712. PR_smprintf_free(quoted_password);
  713. }
  714. }
  715. return pw_return;
  716. }
  717. static int
  718. send_accountcontrol_modify(Slapi_DN *sdn, Private_Repl_Protocol *prp)
  719. {
  720. ConnResult mod_return = 0;
  721. Slapi_Mods smods = {0};
  722. slapi_mods_init (&smods, 0);
  723. slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "userAccountControl", "512");
  724. mod_return = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(sdn), slapi_mods_get_ldapmods_byref(&smods), NULL, NULL );
  725. slapi_mods_done(&smods);
  726. return mod_return;
  727. }
  728. static int
  729. windows_entry_has_attr_and_value(Slapi_Entry *e, const char *attrname, char *value)
  730. {
  731. int retval = 0;
  732. Slapi_Attr *attr = NULL;
  733. if (NULL == e || NULL == attrname)
  734. {
  735. return retval;
  736. }
  737. /* see if the entry has the specified attribute name */
  738. if (0 == slapi_entry_attr_find(e, attrname, &attr) && attr)
  739. {
  740. /* if value is not null, see if the attribute has that
  741. value */
  742. if (value)
  743. {
  744. Slapi_Value *v = NULL;
  745. int index = 0;
  746. for (index = slapi_attr_first_value(attr, &v);
  747. v && (index != -1);
  748. index = slapi_attr_next_value(attr, index, &v))
  749. {
  750. const char *s = slapi_value_get_string(v);
  751. if (NULL == s)
  752. {
  753. continue;
  754. }
  755. if (0 == strcasecmp(s, value))
  756. {
  757. retval = 1;
  758. break;
  759. }
  760. }
  761. }
  762. }
  763. return retval;
  764. }
  765. static void
  766. windows_is_local_entry_user_or_group(Slapi_Entry *e, int *is_user, int *is_group)
  767. {
  768. *is_user = windows_entry_has_attr_and_value(e,"objectclass","ntuser");
  769. *is_group = windows_entry_has_attr_and_value(e,"objectclass","ntgroup");
  770. }
  771. static void
  772. windows_is_remote_entry_user_or_group(Slapi_Entry *e, int *is_user, int *is_group)
  773. {
  774. *is_user = windows_entry_has_attr_and_value(e,"objectclass","person");
  775. *is_group = windows_entry_has_attr_and_value(e,"objectclass","group");
  776. }
  777. static int
  778. add_remote_entry_allowed(Slapi_Entry *e)
  779. {
  780. /* We say yes if the entry has the ntUserCreateNewAccount attribute set in the case of a user, or the ntGroupDeleteGroup
  781. * attribute set in the case of a group
  782. */
  783. /* Is this a user or a group ? */
  784. int is_user = 0;
  785. int is_group = 0;
  786. char *delete_attr = NULL;
  787. windows_is_local_entry_user_or_group(e,&is_user,&is_group);
  788. if (!is_user && !is_group)
  789. {
  790. /* Neither fish nor foul.. */
  791. return -1;
  792. }
  793. if (is_user && is_group)
  794. {
  795. /* Now that's just really strange... */
  796. return -1;
  797. }
  798. if (is_user)
  799. {
  800. delete_attr = "ntUserCreateNewAccount";
  801. } else
  802. {
  803. delete_attr = "ntGroupCreateNewGroup";
  804. }
  805. /* Now test if the attribute value is set */
  806. return windows_entry_has_attr_and_value(e,delete_attr,"true");
  807. }
  808. /* Tells us if we're allowed to add this (remote) entry locally */
  809. static int
  810. add_local_entry_allowed(Private_Repl_Protocol *prp, Slapi_Entry *e)
  811. {
  812. int is_user = 0;
  813. int is_group = 0;
  814. windows_is_remote_entry_user_or_group(e,&is_user,&is_group);
  815. if (is_user)
  816. {
  817. return windows_private_create_users(prp->agmt);
  818. }
  819. if (is_group)
  820. {
  821. return windows_private_create_groups(prp->agmt);
  822. }
  823. /* Default to 'no' */
  824. return 0;
  825. }
  826. static int
  827. delete_remote_entry_allowed(Slapi_Entry *e)
  828. {
  829. /* We say yes if the entry has the ntUserDeleteAccount attribute set in the case of a user, or the ntGroupDeleteGroup
  830. * attribute set in the case of a group
  831. */
  832. /* Is this a user or a group ? */
  833. int is_user = 0;
  834. int is_group = 0;
  835. char *delete_attr = NULL;
  836. windows_is_local_entry_user_or_group(e,&is_user,&is_group);
  837. if (!is_user && !is_group)
  838. {
  839. /* Neither fish nor foul.. */
  840. return 0;
  841. }
  842. if (is_user && is_group)
  843. {
  844. /* Now that's just really strange... */
  845. return 0;
  846. }
  847. if (is_user)
  848. {
  849. delete_attr = "ntUserDeleteAccount";
  850. } else
  851. {
  852. delete_attr = "ntGroupDeleteGroup";
  853. }
  854. /* Now test if the attribute value is set */
  855. return windows_entry_has_attr_and_value(e,delete_attr,"true");
  856. }
  857. static void
  858. windows_log_add_entry_remote(const Slapi_DN *local_dn,const Slapi_DN *remote_dn)
  859. {
  860. const char* local_dn_string = slapi_sdn_get_dn(local_dn);
  861. const char* remote_dn_string = slapi_sdn_get_dn(remote_dn);
  862. 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);
  863. }
  864. static ConnResult
  865. 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)
  866. {
  867. int remote_add_allowed = add_remote_entry_allowed(local_entry);
  868. ConnResult return_value = 0;
  869. int rc = 0;
  870. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  871. "%s: process_replay_add: dn=\"%s\" (%s,%s)\n", agmt_get_long_name(prp->agmt),
  872. slapi_sdn_get_dn(remote_dn), missing_entry ? "not present" : "already present",
  873. remote_add_allowed ? "add allowed" : "add not allowed");
  874. if (missing_entry)
  875. {
  876. /* If DN is a GUID, we need to attempt to reanimate the tombstone */
  877. if (is_guid_dn(remote_dn)) {
  878. int tstone_exists = 0;
  879. int reanimate_rc = -1;
  880. char *new_dn_string = NULL;
  881. char *cn_string = NULL;
  882. Slapi_DN *tombstone_dn = NULL;
  883. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  884. "%s: process_replay_add: dn=\"%s\" appears to have been"
  885. " deleted on remote side. Searching for tombstone.\n",
  886. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(remote_dn));
  887. /* Map local entry to tombstone DN and verify that it exists on
  888. * AD side */
  889. map_windows_tombstone_dn(local_entry, &tombstone_dn, prp, &tstone_exists);
  890. /* We can't use a GUID DN, so rewrite to the new mapped DN. */
  891. cn_string = slapi_entry_attr_get_charptr(local_entry,"cn");
  892. if (!cn_string) {
  893. cn_string = slapi_entry_attr_get_charptr(local_entry,"ntuserdomainid");
  894. }
  895. if (cn_string) {
  896. char *rdnstr = NULL;
  897. char *container_str = NULL;
  898. const char *suffix = slapi_sdn_get_dn(windows_private_get_windows_subtree(prp->agmt));
  899. container_str = extract_container(slapi_entry_get_sdn_const(local_entry),
  900. windows_private_get_directory_subtree(prp->agmt));
  901. new_dn_string = PR_smprintf("cn=%s,%s%s", cn_string, container_str, suffix);
  902. if (new_dn_string) {
  903. /* If the tombstone exists, reanimate it. If the tombstone
  904. * does not exist, we'll create a new entry in AD, which
  905. * will end up getting a new GUID generated by AD. */
  906. if (tstone_exists) {
  907. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  908. "%s: process_replay_add: Reanimating tombstone (dn=\"%s\") to"
  909. " normal entry (dn=\"%s\").\n", agmt_get_long_name(prp->agmt),
  910. slapi_sdn_get_dn(tombstone_dn), new_dn_string);
  911. reanimate_rc = windows_reanimate_tombstone(prp, tombstone_dn, (const char *)new_dn_string);
  912. if (reanimate_rc != 0) {
  913. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  914. "%s: process_replay_add: Reanimation of tombstone"
  915. " (dn=\"%s\") failed. A new entry (dn=\"%s\")"
  916. " will be added instead.\n", agmt_get_long_name(prp->agmt),
  917. slapi_sdn_get_dn(tombstone_dn), new_dn_string);
  918. }
  919. }
  920. /* Clear out the old GUID DN and use the new one. We hand off the memory
  921. * for new_dn_string into the remote_dn. */
  922. slapi_sdn_done(remote_dn);
  923. slapi_sdn_set_dn_passin(remote_dn, new_dn_string);
  924. }
  925. slapi_ch_free_string(&cn_string);
  926. slapi_ch_free_string(&container_str);
  927. }
  928. if (tombstone_dn) {
  929. slapi_sdn_free(&tombstone_dn);
  930. }
  931. if (reanimate_rc == 0) {
  932. /* We reanimated a tombstone, so an add won't work. We
  933. * fallback to doing a modify of the newly reanimated
  934. * entry. */
  935. goto modify_fallback;
  936. }
  937. }
  938. if (remote_add_allowed) {
  939. LDAPMod **entryattrs = NULL;
  940. Slapi_Entry *mapped_entry = NULL;
  941. /* First map the entry */
  942. rc = windows_create_remote_entry(prp,op->p.p_add.target_entry, remote_dn, &mapped_entry, password);
  943. /* Convert entry to mods */
  944. if (0 == rc && mapped_entry)
  945. {
  946. (void)slapi_entry2mods (mapped_entry , NULL /* &entrydn : We don't need it */, &entryattrs);
  947. slapi_entry_free(mapped_entry);
  948. mapped_entry = NULL;
  949. if (NULL == entryattrs)
  950. {
  951. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  952. "%s: windows_replay_update: Cannot convert entry to LDAPMods.\n",
  953. agmt_get_long_name(prp->agmt));
  954. return_value = CONN_LOCAL_ERROR;
  955. }
  956. else
  957. {
  958. windows_log_add_entry_remote(local_dn, remote_dn);
  959. return_value = windows_conn_send_add(prp->conn, slapi_sdn_get_dn(remote_dn),
  960. entryattrs, NULL, NULL);
  961. /* It's possible that the entry already exists in AD, in which
  962. * case we fall back to modify it */
  963. /* NGK - This fallback doesn't seem to happen, at least not at this point
  964. * in the code. The only chance to fallback to doing a modify is if
  965. * missing_entry is set to 0 at the top of this function. */
  966. if (return_value)
  967. {
  968. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  969. "%s: windows_replay_update: Cannot replay add operation.\n",
  970. agmt_get_long_name(prp->agmt));
  971. }
  972. ldap_mods_free(entryattrs, 1);
  973. entryattrs = NULL;
  974. }
  975. } else
  976. {
  977. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  978. "%s: process_replay_add: failed to create mapped entry dn=\"%s\"\n",
  979. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(remote_dn));
  980. }
  981. }
  982. } else
  983. {
  984. Slapi_Entry *remote_entry = NULL;
  985. modify_fallback:
  986. /* Fetch the remote entry */
  987. rc = windows_get_remote_entry(prp, remote_dn,&remote_entry);
  988. if (0 == rc && remote_entry) {
  989. return_value = windows_update_remote_entry(prp,remote_entry,local_entry);
  990. }
  991. if (remote_entry)
  992. {
  993. slapi_entry_free(remote_entry);
  994. }
  995. }
  996. return return_value;
  997. }
  998. /*
  999. * Given a changelog entry, construct the appropriate LDAP operations to sync
  1000. * the operation to AD.
  1001. */
  1002. ConnResult
  1003. windows_replay_update(Private_Repl_Protocol *prp, slapi_operation_parameters *op)
  1004. {
  1005. ConnResult return_value = 0;
  1006. int rc = 0;
  1007. char *password = NULL;
  1008. int is_ours = 0;
  1009. int is_user = 0;
  1010. int is_group = 0;
  1011. Slapi_DN *remote_dn = NULL;
  1012. Slapi_DN *local_dn = NULL;
  1013. Slapi_Entry *local_entry = NULL;
  1014. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_replay_update\n", 0, 0, 0 );
  1015. local_dn = slapi_sdn_new_dn_byref( op->target_address.dn );
  1016. /* Since we have the target uniqueid in the op structure, let's
  1017. * fetch the local entry here using it. We do not want to search
  1018. * across tombstone entries unless we are dealing with a delete
  1019. * operation here since searching across tombstones can be very
  1020. * inefficient as the tombstones build up.
  1021. */
  1022. if (op->operation_type != SLAPI_OPERATION_DELETE) {
  1023. rc = windows_get_local_entry_by_uniqueid(prp, op->target_address.uniqueid, &local_entry);
  1024. } else {
  1025. rc = windows_get_local_tombstone_by_uniqueid(prp, op->target_address.uniqueid, &local_entry);
  1026. }
  1027. if (rc)
  1028. {
  1029. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1030. "%s: windows_replay_update: failed to fetch local entry for %s operation dn=\"%s\"\n",
  1031. agmt_get_long_name(prp->agmt),
  1032. op2string(op->operation_type), op->target_address.dn);
  1033. goto error;
  1034. }
  1035. is_ours = is_subject_of_agreement_local(local_entry, prp->agmt);
  1036. windows_is_local_entry_user_or_group(local_entry,&is_user,&is_group);
  1037. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1038. "%s: windows_replay_update: Looking at %s operation local dn=\"%s\" (%s,%s,%s)\n",
  1039. agmt_get_long_name(prp->agmt),
  1040. op2string(op->operation_type), op->target_address.dn, is_ours ? "ours" : "not ours",
  1041. is_user ? "user" : "not user", is_group ? "group" : "not group");
  1042. if (is_ours && (is_user || is_group) ) {
  1043. int missing_entry = 0;
  1044. /* Make the entry's DN */
  1045. rc = map_entry_dn_outbound(local_entry,&remote_dn,prp,&missing_entry, 1);
  1046. if (rc || NULL == remote_dn)
  1047. {
  1048. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1049. "%s: windows_replay_update: failed map dn for %s operation dn=\"%s\"\n",
  1050. agmt_get_long_name(prp->agmt),
  1051. op2string(op->operation_type), op->target_address.dn);
  1052. goto error;
  1053. }
  1054. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1055. "%s: windows_replay_update: Processing %s operation local dn=\"%s\" remote dn=\"%s\"\n",
  1056. agmt_get_long_name(prp->agmt),
  1057. op2string(op->operation_type), op->target_address.dn, slapi_sdn_get_dn(remote_dn));
  1058. switch (op->operation_type) {
  1059. /* For an ADD operation, we map the entry and then send the operation, which may fail if the peer entry already existed */
  1060. case SLAPI_OPERATION_ADD:
  1061. return_value = process_replay_add(prp,op,local_entry,local_dn,remote_dn,is_user,missing_entry,&password);
  1062. break;
  1063. case SLAPI_OPERATION_MODIFY:
  1064. {
  1065. LDAPMod **mapped_mods = NULL;
  1066. windows_map_mods_for_replay(prp,op->p.p_modify.modify_mods, &mapped_mods, is_user, &password);
  1067. /* It's possible that the mapping process results in an empty mod list, in which case we don't bother with the replay */
  1068. if ( mapped_mods == NULL || *(mapped_mods)== NULL )
  1069. {
  1070. return_value = CONN_OPERATION_SUCCESS;
  1071. } else
  1072. {
  1073. if (slapi_is_loglevel_set(SLAPI_LOG_REPL))
  1074. {
  1075. int i = 0;
  1076. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,"dump mods for replay update:");
  1077. for(i=0;mapped_mods[i];i++)
  1078. {
  1079. slapi_mod_dump(mapped_mods[i],i);
  1080. }
  1081. }
  1082. return_value = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(remote_dn), mapped_mods, NULL, NULL /* returned controls */);
  1083. }
  1084. if (mapped_mods)
  1085. {
  1086. ldap_mods_free(mapped_mods,1);
  1087. mapped_mods = NULL;
  1088. }
  1089. }
  1090. break;
  1091. case SLAPI_OPERATION_DELETE:
  1092. if (delete_remote_entry_allowed(local_entry))
  1093. {
  1094. return_value = windows_conn_send_delete(prp->conn, slapi_sdn_get_dn(remote_dn), NULL, NULL /* returned controls */);
  1095. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1096. "%s: windows_replay_update: deleted remote entry, dn=\"%s\", result=%d\n",
  1097. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(remote_dn), return_value);
  1098. } else
  1099. {
  1100. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1101. "%s: windows_replay_update: delete not allowed on remote entry, dn=\"%s\"\n",
  1102. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(remote_dn));
  1103. }
  1104. break;
  1105. case SLAPI_OPERATION_MODRDN:
  1106. return_value = windows_conn_send_rename(prp->conn, op->target_address.dn,
  1107. op->p.p_modrdn.modrdn_newrdn,
  1108. op->p.p_modrdn.modrdn_newsuperior_address.dn,
  1109. op->p.p_modrdn.modrdn_deloldrdn,
  1110. NULL, NULL /* returned controls */);
  1111. break;
  1112. default:
  1113. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name, "%s: replay_update: Unknown "
  1114. "operation type %d found in changelog - skipping change.\n",
  1115. agmt_get_long_name(prp->agmt), op->operation_type);
  1116. }
  1117. if (password)
  1118. {
  1119. return_value = send_password_modify(remote_dn, password, prp);
  1120. if (return_value)
  1121. {
  1122. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name, "%s: windows_replay_update: update password returned %d\n",
  1123. agmt_get_long_name(prp->agmt), return_value );
  1124. } else {
  1125. /* If we successfully added an entry, and then subsequently changed its password, THEN we need to change its status in AD
  1126. * in order that it can be used (otherwise the user is marked as disabled). To do this we set this attribute and value:
  1127. * userAccountControl: 512 */
  1128. if (op->operation_type == SLAPI_OPERATION_ADD && missing_entry)
  1129. {
  1130. return_value = send_accountcontrol_modify(remote_dn, prp);
  1131. }
  1132. }
  1133. }
  1134. } else {
  1135. /* We ignore operations that target entries outside of our sync'ed subtree, or which are not Windows users or groups */
  1136. }
  1137. error:
  1138. if (local_entry)
  1139. {
  1140. slapi_entry_free(local_entry);
  1141. }
  1142. if (local_dn)
  1143. {
  1144. slapi_sdn_free (&local_dn);
  1145. }
  1146. if (remote_dn)
  1147. {
  1148. slapi_sdn_free(&remote_dn);
  1149. }
  1150. return return_value;
  1151. }
  1152. static int
  1153. is_straight_mapped_attr(const char *type, int is_user /* or group */, int is_nt4)
  1154. {
  1155. int found = 0;
  1156. size_t offset = 0;
  1157. char *this_attr = NULL;
  1158. char **list = is_user ? (is_nt4 ? nt4_user_matching_attributes : windows_user_matching_attributes) : (is_nt4 ? nt4_group_matching_attributes : windows_group_matching_attributes);
  1159. /* Look for the type in the list of straight mapped attrs for the appropriate object type */
  1160. while (this_attr = list[offset])
  1161. {
  1162. if (0 == slapi_attr_type_cmp(this_attr, type, SLAPI_TYPE_CMP_SUBTYPE))
  1163. {
  1164. found = 1;
  1165. break;
  1166. }
  1167. offset++;
  1168. }
  1169. return found;
  1170. }
  1171. static void
  1172. windows_map_attr_name(const char *original_type , int to_windows, int is_user, int is_create, char **mapped_type, int *map_dn)
  1173. {
  1174. char *new_type = NULL;
  1175. windows_attribute_map *our_map = is_user ? user_attribute_map : group_attribute_map;
  1176. windows_attribute_map *this_map = NULL;
  1177. size_t offset = 0;
  1178. *mapped_type = NULL;
  1179. /* Iterate over the map entries looking for the type we have */
  1180. while(this_map = &(our_map[offset]))
  1181. {
  1182. char *their_name = to_windows ? this_map->windows_attribute_name : this_map->ldap_attribute_name;
  1183. char *our_name = to_windows ? this_map->ldap_attribute_name : this_map->windows_attribute_name;
  1184. if (NULL == their_name)
  1185. {
  1186. /* End of the list */
  1187. break;
  1188. }
  1189. if (0 == slapi_attr_type_cmp(original_type, our_name, SLAPI_TYPE_CMP_SUBTYPE))
  1190. {
  1191. if (!is_create && (this_map->create_type == createonly))
  1192. {
  1193. /* Skip create-only entries if we're not creating */
  1194. } else
  1195. {
  1196. if ( (this_map->map_type == towindowsonly && to_windows) || (this_map->map_type == fromwindowsonly && !to_windows)
  1197. || (this_map->map_type == bidirectional) )
  1198. {
  1199. new_type = slapi_ch_strdup(their_name);
  1200. *map_dn = (this_map->attr_type == dnmap);
  1201. break;
  1202. }
  1203. }
  1204. }
  1205. offset++;
  1206. }
  1207. if (new_type)
  1208. {
  1209. *mapped_type = new_type;
  1210. }
  1211. }
  1212. /*
  1213. * Make a new entry suitable for the sync destination (indicated by the to_windows argument).
  1214. * Returns the new entry ready to be passed to an LDAP ADD operation, either remote or local.
  1215. * Also returns the plaintext value of any password contained in the original entry (only for the
  1216. * to_windows direction). This is because passwords must be added to entries after they are added in AD.
  1217. * Caller must free the new entry and any password returned.
  1218. */
  1219. static int
  1220. windows_create_remote_entry(Private_Repl_Protocol *prp,Slapi_Entry *original_entry, Slapi_DN *remote_sdn, Slapi_Entry **remote_entry, char** password)
  1221. {
  1222. int retval = 0;
  1223. char *entry_string = NULL;
  1224. Slapi_Entry *new_entry = NULL;
  1225. int rc = 0;
  1226. int is_user = 0;
  1227. int is_group = 0;
  1228. Slapi_Attr *attr = NULL;
  1229. char *username = NULL;
  1230. const char *dn_string = NULL;
  1231. char *fqusername = NULL;
  1232. const char *domain_name = windows_private_get_windows_domain(prp->agmt);
  1233. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  1234. char *remote_user_entry_template =
  1235. "dn: %s\n"
  1236. "objectclass:top\n"
  1237. "objectclass:person\n"
  1238. "objectclass:organizationalperson\n"
  1239. "objectclass:user\n"
  1240. "userPrincipalName:%s\n";
  1241. char *remote_group_entry_template =
  1242. "dn: %s\n"
  1243. "objectclass:top\n"
  1244. "objectclass:group\n";
  1245. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_create_remote_entry\n", 0, 0, 0 );
  1246. windows_is_local_entry_user_or_group(original_entry,&is_user,&is_group);
  1247. /* Create a new entry */
  1248. /* Give it its DN and samaccountname */
  1249. username = extract_ntuserdomainid_from_entry(original_entry);
  1250. if (NULL == username)
  1251. {
  1252. goto error;
  1253. }
  1254. fqusername = PR_smprintf("%s@%s",username,domain_name);
  1255. dn_string = slapi_sdn_get_dn(remote_sdn);
  1256. if (is_user)
  1257. {
  1258. entry_string = slapi_ch_smprintf(remote_user_entry_template, dn_string, fqusername);
  1259. } else
  1260. {
  1261. entry_string = slapi_ch_smprintf(remote_group_entry_template, dn_string);
  1262. }
  1263. PR_smprintf_free(fqusername);
  1264. if (NULL == entry_string)
  1265. {
  1266. goto error;
  1267. }
  1268. new_entry = slapi_str2entry(entry_string, 0);
  1269. slapi_ch_free_string(&entry_string);
  1270. if (NULL == new_entry)
  1271. {
  1272. goto error;
  1273. }
  1274. /* Map the appropriate attributes sourced from the remote entry */
  1275. /* Iterate over the local entry's attributes */
  1276. for (rc = slapi_entry_first_attr(original_entry, &attr); rc == 0;
  1277. rc = slapi_entry_next_attr(original_entry, attr, &attr))
  1278. {
  1279. char *type = NULL;
  1280. Slapi_ValueSet *vs = NULL;
  1281. int mapdn = 0;
  1282. slapi_attr_get_type( attr, &type );
  1283. slapi_attr_get_valueset(attr,&vs);
  1284. if ( is_straight_mapped_attr(type,is_user,is_nt4) )
  1285. {
  1286. /* The initials attribute is a special case. AD has a constraint
  1287. * that limits the value length. If we're sending a change to
  1288. * the initials attribute to AD, we trim if neccessary.
  1289. */
  1290. if (0 == slapi_attr_type_cmp(type, "initials", SLAPI_TYPE_CMP_SUBTYPE)) {
  1291. int i = 0;
  1292. const char *initials_value = NULL;
  1293. Slapi_Value *value = NULL;
  1294. i = slapi_valueset_first_value(vs,&value);
  1295. while (i >= 0) {
  1296. initials_value = slapi_value_get_string(value);
  1297. /* If > AD_INITIALS_LENGTH, trim the value */
  1298. if (strlen(initials_value) > AD_INITIALS_LENGTH) {
  1299. char *new_initials = PL_strndup(initials_value, AD_INITIALS_LENGTH);
  1300. /* the below hands off memory */
  1301. slapi_value_set_string_passin(value, new_initials);
  1302. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1303. "%s: windows_create_remote_entry: "
  1304. "Trimming initials attribute to %d characters.\n",
  1305. agmt_get_long_name(prp->agmt), AD_INITIALS_LENGTH);
  1306. }
  1307. i = slapi_valueset_next_value(vs, i, &value);
  1308. }
  1309. }
  1310. /* copy over the attr values */
  1311. slapi_entry_add_valueset(new_entry,type,vs);
  1312. } else
  1313. {
  1314. char *new_type = NULL;
  1315. windows_map_attr_name(type , 1 /* to windows */, is_user, 1 /* create */, &new_type, &mapdn);
  1316. if (new_type)
  1317. {
  1318. if (mapdn)
  1319. {
  1320. Slapi_ValueSet *mapped_values = NULL;
  1321. map_dn_values(prp,vs,&mapped_values, 1 /* to windows */,0);
  1322. if (mapped_values)
  1323. {
  1324. slapi_entry_add_valueset(new_entry,new_type,mapped_values);
  1325. slapi_valueset_free(mapped_values);
  1326. mapped_values = NULL;
  1327. }
  1328. } else
  1329. {
  1330. Slapi_Attr *new_attr = NULL;
  1331. /* AD treats streetAddress as a single-valued attribute, while we define it
  1332. * as a multi-valued attribute as it's defined in rfc 4519. We only
  1333. * sync the first value to AD to avoid a constraint violation.
  1334. */
  1335. if (0 == slapi_attr_type_cmp(new_type, "streetAddress", SLAPI_TYPE_CMP_SUBTYPE)) {
  1336. if (slapi_valueset_count(vs) > 1) {
  1337. int i = 0;
  1338. const char *street_value = NULL;
  1339. Slapi_Value *value = NULL;
  1340. Slapi_Value *new_value = NULL;
  1341. i = slapi_valueset_first_value(vs,&value);
  1342. if (i >= 0) {
  1343. /* Dup the first value, trash the valueset, then copy
  1344. * in the dup'd value. */
  1345. new_value = slapi_value_dup(value);
  1346. slapi_valueset_done(vs);
  1347. /* The below hands off the memory to the valueset */
  1348. slapi_valueset_add_value_ext(vs, new_value, SLAPI_VALUE_FLAG_PASSIN);
  1349. }
  1350. }
  1351. }
  1352. slapi_entry_add_valueset(new_entry,type,vs);
  1353. /* Reset the type to new_type here. This is needed since
  1354. * slapi_entry_add_valueset will create the Slapi_Attrs using
  1355. * the schema definition, which can reset the type to something
  1356. * other than the type you pass into it. To be safe, we just
  1357. * create the attributes with the old type, then reset them. */
  1358. if (slapi_entry_attr_find(new_entry, type, &new_attr) == 0) {
  1359. slapi_attr_set_type(new_attr, new_type);
  1360. }
  1361. }
  1362. slapi_ch_free_string(&new_type);
  1363. }
  1364. /* password mods are treated specially */
  1365. if (0 == slapi_attr_type_cmp(type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD, SLAPI_TYPE_CMP_SUBTYPE) )
  1366. {
  1367. const char *password_value = NULL;
  1368. Slapi_Value *value = NULL;
  1369. slapi_valueset_first_value(vs,&value);
  1370. password_value = slapi_value_get_string(value);
  1371. /* We need to check if the first character of password_value is an
  1372. * opening brace since strstr will simply return it's first argument
  1373. * if it is an empty string. */
  1374. if (password_value && (*password_value == '{')) {
  1375. if (strchr( password_value, '}' )) {
  1376. /* A storage scheme is present. Check if it's the
  1377. * clear storage scheme. */
  1378. if ((strlen(password_value) >= PASSWD_CLEAR_PREFIX_LEN + 1) &&
  1379. (strncasecmp(password_value, PASSWD_CLEAR_PREFIX, PASSWD_CLEAR_PREFIX_LEN) == 0)) {
  1380. /* This password is in clear text. Strip off the clear prefix
  1381. * and sync it. */
  1382. *password = slapi_ch_strdup(password_value + PASSWD_CLEAR_PREFIX_LEN);
  1383. } else {
  1384. /* This password is stored in a non-cleartext format.
  1385. * We can only sync cleartext passwords. */
  1386. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1387. "%s: windows_create_remote_entry: "
  1388. "Password is already hashed. Not syncing.\n",
  1389. agmt_get_long_name(prp->agmt));
  1390. }
  1391. } else {
  1392. /* This password doesn't have a storage prefix but
  1393. * just happens to start with the '{' character. We'll
  1394. * assume that it's just a cleartext password without
  1395. * the proper storage prefix. */
  1396. *password = slapi_ch_strdup(password_value);
  1397. }
  1398. } else {
  1399. /* This password has no storage prefix, or the password is empty */
  1400. *password = slapi_ch_strdup(password_value);
  1401. }
  1402. }
  1403. }
  1404. if (vs)
  1405. {
  1406. slapi_valueset_free(vs);
  1407. vs = NULL;
  1408. }
  1409. }
  1410. /* NT4 must have the groupType attribute set for groups. If it is not present, we will
  1411. * add it here with a value of 2 (global group).
  1412. */
  1413. if (is_nt4 && is_group)
  1414. {
  1415. Slapi_Attr *ap = NULL;
  1416. if(slapi_entry_attr_find(new_entry, "groupType", &ap))
  1417. {
  1418. /* groupType attribute wasn't found, so we'll add it */
  1419. slapi_entry_attr_set_int(new_entry, "groupType", 2 /* global group */);
  1420. }
  1421. }
  1422. if (remote_entry)
  1423. {
  1424. *remote_entry = new_entry;
  1425. }
  1426. error:
  1427. if (username)
  1428. {
  1429. slapi_ch_free_string(&username);
  1430. }
  1431. if (new_entry)
  1432. {
  1433. windows_dump_entry("Created new remote entry:\n",new_entry);
  1434. }
  1435. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_create_remote_entry: %d\n", retval, 0, 0 );
  1436. return retval;
  1437. }
  1438. /* the entry has already been translated, so be sure to search for ntuserid
  1439. and not samaccountname or anything else. */
  1440. static Slapi_Entry*
  1441. windows_entry_already_exists(Slapi_Entry *e){
  1442. int rc = 0;
  1443. Slapi_DN *sdn = NULL;
  1444. Slapi_Entry *entry = NULL;
  1445. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_entry_already_exists\n", 0, 0, 0 );
  1446. sdn = slapi_entry_get_sdn(e);
  1447. rc = slapi_search_internal_get_entry( sdn, NULL, &entry, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  1448. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_entry_already_exists\n", 0, 0, 0 );
  1449. if (rc == LDAP_SUCCESS)
  1450. {
  1451. return entry;
  1452. }
  1453. else
  1454. {
  1455. return NULL;
  1456. }
  1457. }
  1458. static int
  1459. windows_delete_local_entry(Slapi_DN *sdn){
  1460. Slapi_PBlock *pb = NULL;
  1461. int return_value = 0;
  1462. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_delete_local_entry\n", 0, 0, 0 );
  1463. pb = slapi_pblock_new();
  1464. slapi_delete_internal_set_pb(pb, slapi_sdn_get_dn(sdn), NULL, NULL, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  1465. slapi_delete_internal_pb(pb);
  1466. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  1467. slapi_pblock_destroy(pb);
  1468. if (return_value) {
  1469. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1470. "delete operation on local entry %s returned: %d\n", slapi_sdn_get_dn(sdn), return_value);
  1471. }
  1472. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_delete_local_entry: %d\n", return_value, 0, 0 );
  1473. return return_value;
  1474. }
  1475. static void
  1476. windows_map_mods_for_replay(Private_Repl_Protocol *prp,LDAPMod **original_mods, LDAPMod ***returned_mods, int is_user, char** password)
  1477. {
  1478. Slapi_Mods smods = {0};
  1479. Slapi_Mods mapped_smods = {0};
  1480. LDAPMod *mod = NULL;
  1481. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  1482. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_map_mods_for_replay\n", 0, 0, 0 );
  1483. /* Iterate through the original mods, looking each attribute type up in the maps for either user or group */
  1484. slapi_mods_init_byref(&smods, original_mods);
  1485. slapi_mods_init(&mapped_smods,10);
  1486. mod = slapi_mods_get_first_mod(&smods);
  1487. while(mod)
  1488. {
  1489. char *attr_type = mod->mod_type;
  1490. int mapdn = 0;
  1491. /* Check to see if this attribute is passed through */
  1492. if (is_straight_mapped_attr(attr_type,is_user,is_nt4)) {
  1493. /* The initials attribute is a special case. AD has a constraint
  1494. * that limits the value length. If we're sending a change to
  1495. * the initials attribute to AD, we trim if neccessary.
  1496. */
  1497. if (0 == slapi_attr_type_cmp(attr_type, "initials", SLAPI_TYPE_CMP_SUBTYPE)) {
  1498. int i;
  1499. for (i = 0; mod->mod_bvalues[i] != NULL; i++) {
  1500. /* If > AD_INITIALS_LENGTH, trim the value */
  1501. if (mod->mod_bvalues[i]->bv_len > AD_INITIALS_LENGTH) {
  1502. mod->mod_bvalues[i]->bv_val[AD_INITIALS_LENGTH] = '\0';
  1503. mod->mod_bvalues[i]->bv_len = AD_INITIALS_LENGTH;
  1504. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1505. "%s: windows_map_mods_for_replay: "
  1506. "Trimming initials attribute to %d characters.\n",
  1507. agmt_get_long_name(prp->agmt), AD_INITIALS_LENGTH);
  1508. }
  1509. }
  1510. }
  1511. /* copy over the mod */
  1512. slapi_mods_add_modbvps(&mapped_smods,mod->mod_op,attr_type,mod->mod_bvalues);
  1513. } else
  1514. {
  1515. char *mapped_type = NULL;
  1516. /* Check if this mod has its attribute type mapped */
  1517. windows_map_attr_name(attr_type,1,is_user,0,&mapped_type, &mapdn);
  1518. if (mapped_type)
  1519. {
  1520. /* If so copy over the mod with new type name */
  1521. if (mapdn)
  1522. {
  1523. Slapi_ValueSet *mapped_values = NULL;
  1524. Slapi_ValueSet *vs = NULL;
  1525. Slapi_Mod smod;
  1526. vs = slapi_valueset_new();
  1527. slapi_mod_init_byref(&smod,mod);
  1528. slapi_valueset_set_from_smod(vs, &smod);
  1529. map_dn_values(prp,vs,&mapped_values, 1 /* to windows */,0);
  1530. if (mapped_values)
  1531. {
  1532. slapi_mods_add_mod_values(&mapped_smods,mod->mod_op,mapped_type,valueset_get_valuearray(mapped_values));
  1533. slapi_valueset_free(mapped_values);
  1534. mapped_values = NULL;
  1535. } else
  1536. {
  1537. /* this might be a del: mod, in which case there are no values */
  1538. if (mod->mod_op & LDAP_MOD_DELETE)
  1539. {
  1540. slapi_mods_add_mod_values(&mapped_smods, LDAP_MOD_DELETE, mapped_type, NULL);
  1541. }
  1542. }
  1543. slapi_mod_done(&smod);
  1544. slapi_valueset_free(vs);
  1545. } else
  1546. {
  1547. /* AD treats streetAddress as a single-valued attribute, while we define it
  1548. * as a multi-valued attribute as it's defined in rfc 4519. We only
  1549. * sync the first value to AD to avoid a constraint violation.
  1550. */
  1551. if (0 == slapi_attr_type_cmp(mapped_type, "streetAddress", SLAPI_TYPE_CMP_SUBTYPE)) {
  1552. Slapi_Mod smod;
  1553. struct berval *new_bval = NULL;
  1554. slapi_mod_init_byref(&smod,mod);
  1555. /* Check if there is more than one value */
  1556. if (slapi_mod_get_num_values(&smod) > 1) {
  1557. new_bval = slapi_mod_get_first_value(&smod);
  1558. /* Remove all values except for the first */
  1559. while (slapi_mod_get_next_value(&smod)) {
  1560. /* This modifies the bvalues in the mod itself */
  1561. slapi_mod_remove_value(&smod);
  1562. }
  1563. }
  1564. slapi_mod_done(&smod);
  1565. }
  1566. slapi_mods_add_modbvps(&mapped_smods,mod->mod_op,mapped_type,mod->mod_bvalues);
  1567. }
  1568. slapi_ch_free_string(&mapped_type);
  1569. } else
  1570. {
  1571. /* password mods are treated specially */
  1572. if (0 == slapi_attr_type_cmp(attr_type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD, SLAPI_TYPE_CMP_SUBTYPE) )
  1573. {
  1574. char *password_value = NULL;
  1575. password_value = mod->mod_bvalues[0]->bv_val;
  1576. /* We need to check if the first character of password_value is an
  1577. * opening brace since strstr will simply return it's first argument
  1578. * if it is an empty string. */
  1579. if (password_value && (*password_value == '{')) {
  1580. if (strchr( password_value, '}' )) {
  1581. /* A storage scheme is present. Check if it's the
  1582. * clear storage scheme. */
  1583. if ((strlen(password_value) >= PASSWD_CLEAR_PREFIX_LEN + 1) &&
  1584. (strncasecmp(password_value, PASSWD_CLEAR_PREFIX, PASSWD_CLEAR_PREFIX_LEN) == 0)) {
  1585. /* This password is in clear text. Strip off the clear prefix
  1586. * and sync it. */
  1587. *password = slapi_ch_strdup(password_value + PASSWD_CLEAR_PREFIX_LEN);
  1588. } else {
  1589. /* This password is stored in a non-cleartext format.
  1590. * We can only sync cleartext passwords. */
  1591. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1592. "%s: windows_create_remote_entry: "
  1593. "Password is already hashed. Not syncing.\n",
  1594. agmt_get_long_name(prp->agmt));
  1595. }
  1596. } else {
  1597. /* This password doesn't have a storage prefix but
  1598. * just happens to start with the '{' character. We'll
  1599. * assume that it's just a cleartext password without
  1600. * the proper storage prefix. */
  1601. *password = slapi_ch_strdup(password_value);
  1602. }
  1603. } else {
  1604. /* This password has no storage prefix, or the password is empty */
  1605. *password = slapi_ch_strdup(password_value);
  1606. }
  1607. }
  1608. }
  1609. }
  1610. /* Otherwise we do not copy this mod at all */
  1611. mod = slapi_mods_get_next_mod(&smods);
  1612. }
  1613. slapi_mods_done (&smods);
  1614. /* Extract the mods for the caller */
  1615. *returned_mods = slapi_mods_get_ldapmods_passout(&mapped_smods);
  1616. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_map_mods_for_replay\n", 0, 0, 0 );
  1617. }
  1618. /* Returns non-zero if the attribute value sets are identical. If you want to
  1619. * compare the entire attribute value, set n to 0. You can compare only the
  1620. * first n characters of the values by passing in the legth as n. */
  1621. static int
  1622. attr_compare_equal(Slapi_Attr *a, Slapi_Attr *b, int n)
  1623. {
  1624. /* For now only handle single values */
  1625. Slapi_Value *va = NULL;
  1626. Slapi_Value *vb = NULL;
  1627. int num_a = 0;
  1628. int num_b = 0;
  1629. int match = 1;
  1630. slapi_attr_get_numvalues(a,&num_a);
  1631. slapi_attr_get_numvalues(b,&num_b);
  1632. if (num_a == num_b) {
  1633. slapi_attr_first_value(a, &va);
  1634. slapi_attr_first_value(b, &vb);
  1635. /* If either val is less than n, then check if the length, then values are
  1636. * equal. If both are n or greater, then only compare the first n chars.
  1637. * If n is 0, then just compare the entire attribute. */
  1638. if ((va->bv.bv_len < n) || (vb->bv.bv_len < n) || (n == 0)) {
  1639. if (va->bv.bv_len == vb->bv.bv_len) {
  1640. if (0 != memcmp(va->bv.bv_val, vb->bv.bv_val, va->bv.bv_len)) {
  1641. match = 0;
  1642. }
  1643. } else {
  1644. match = 0;
  1645. }
  1646. } else if (0 != memcmp(va->bv.bv_val, vb->bv.bv_val, n)) {
  1647. match = 0;
  1648. }
  1649. } else {
  1650. match = 0;
  1651. }
  1652. return match;
  1653. }
  1654. /* Returns non-zero if all of the values of attribute a are contained in attribute b. */
  1655. static int
  1656. attr_compare_present(Slapi_Attr *a, Slapi_Attr *b)
  1657. {
  1658. int match = 1;
  1659. int i = 0;
  1660. Slapi_Value *va = NULL;
  1661. /* Iterate through values in attr a and search for each in attr b */
  1662. for (i = slapi_attr_first_value(a, &va); va && (i != -1);
  1663. i = slapi_attr_next_value(a, i, &va)) {
  1664. if (slapi_attr_value_find(b, slapi_value_get_berval(va)) != 0) {
  1665. /* This value wasn't found, so stop checking for values */
  1666. match = 0;
  1667. break;
  1668. }
  1669. }
  1670. return match;
  1671. }
  1672. /* Helper functions for dirsync result processing */
  1673. /* Is this entry a tombstone ? */
  1674. static int
  1675. is_tombstone(Private_Repl_Protocol *prp, Slapi_Entry *e)
  1676. {
  1677. int retval = 0;
  1678. if ( (slapi_filter_test_simple( e, (Slapi_Filter*)windows_private_get_deleted_filter(prp->agmt) ) == 0) )
  1679. {
  1680. retval = 1;
  1681. }
  1682. return retval;
  1683. }
  1684. #define ENTRY_NOTFOUND -1
  1685. #define ENTRY_NOT_UNIQUE -2
  1686. /* Search for an entry in AD */
  1687. static int
  1688. find_entry_by_attr_value_remote(const char *attribute, const char *value, Slapi_Entry **e, Private_Repl_Protocol *prp)
  1689. {
  1690. int retval = 0;
  1691. ConnResult cres = 0;
  1692. char *filter = NULL;
  1693. const char *searchbase = NULL;
  1694. Slapi_Entry *found_entry = NULL;
  1695. filter = PR_smprintf("(%s=%s)",attribute,value);
  1696. searchbase = slapi_sdn_get_dn(windows_private_get_windows_subtree(prp->agmt));
  1697. cres = windows_search_entry(prp->conn, (char*)searchbase, filter, &found_entry);
  1698. if (cres)
  1699. {
  1700. retval = -1;
  1701. } else
  1702. {
  1703. if (found_entry)
  1704. {
  1705. *e = found_entry;
  1706. }
  1707. }
  1708. if (filter)
  1709. {
  1710. PR_smprintf_free(filter);
  1711. filter = NULL;
  1712. }
  1713. return retval;
  1714. }
  1715. /* Search for an entry in AD by DN */
  1716. static int
  1717. windows_get_remote_entry (Private_Repl_Protocol *prp, const Slapi_DN* remote_dn,Slapi_Entry **remote_entry)
  1718. {
  1719. int retval = 0;
  1720. ConnResult cres = 0;
  1721. char *filter = "(objectclass=*)";
  1722. const char *searchbase = NULL;
  1723. Slapi_Entry *found_entry = NULL;
  1724. searchbase = slapi_sdn_get_dn(remote_dn);
  1725. cres = windows_search_entry(prp->conn, (char*)searchbase, filter, &found_entry);
  1726. if (cres)
  1727. {
  1728. retval = -1;
  1729. } else
  1730. {
  1731. if (found_entry)
  1732. {
  1733. *remote_entry = found_entry;
  1734. }
  1735. }
  1736. return retval;
  1737. }
  1738. /* Search for a tombstone entry in AD by DN */
  1739. static int
  1740. windows_get_remote_tombstone (Private_Repl_Protocol *prp, const Slapi_DN* remote_dn,Slapi_Entry **remote_entry)
  1741. {
  1742. int retval = 0;
  1743. ConnResult cres = 0;
  1744. char *filter = "(objectclass=*)";
  1745. const char *searchbase = NULL;
  1746. Slapi_Entry *found_entry = NULL;
  1747. LDAPControl *server_controls[2];
  1748. /* We need to send the "Return Deleted Objects" control to search
  1749. * for tombstones. */
  1750. slapi_build_control(REPL_RETURN_DELETED_OBJS_CONTROL_OID, NULL, PR_TRUE,
  1751. &server_controls[0]);
  1752. server_controls[1] = NULL;
  1753. searchbase = slapi_sdn_get_dn(remote_dn);
  1754. cres = windows_search_entry_ext(prp->conn, (char*)searchbase, filter,
  1755. &found_entry, server_controls);
  1756. if (cres) {
  1757. retval = -1;
  1758. } else {
  1759. if (found_entry) {
  1760. *remote_entry = found_entry;
  1761. }
  1762. }
  1763. ldap_control_free(server_controls[0]);
  1764. return retval;
  1765. }
  1766. /* Reanimate a tombstone in AD. Returns 0 on success, otherwise you get the
  1767. * LDAP return code from the modify operation. */
  1768. static int
  1769. windows_reanimate_tombstone(Private_Repl_Protocol *prp, const Slapi_DN* tombstone_dn, const char* new_dn)
  1770. {
  1771. int retval = 0;
  1772. LDAPControl *server_controls[2];
  1773. Slapi_Mods smods = {0};
  1774. /* We need to send the "Return Deleted Objects" control to modify
  1775. * tombstone entries. */
  1776. slapi_build_control(REPL_RETURN_DELETED_OBJS_CONTROL_OID, NULL, PR_TRUE,
  1777. &server_controls[0]);
  1778. server_controls[1] = NULL;
  1779. /* To reanimate a tombstone in AD, you need to send a modify
  1780. * operation that does two things. It must remove the isDeleted
  1781. * attribute from the entry and it must modify the DN. This DN
  1782. * does not have to be the same place in the tree that the entry
  1783. * previously existed. */
  1784. slapi_mods_init (&smods, 0);
  1785. slapi_mods_add_mod_values(&smods, LDAP_MOD_DELETE, "isDeleted", NULL);
  1786. slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "distinguishedName", new_dn);
  1787. retval = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(tombstone_dn),
  1788. slapi_mods_get_ldapmods_byref(&smods), server_controls, NULL );
  1789. slapi_mods_done(&smods);
  1790. ldap_control_free(server_controls[0]);
  1791. return retval;
  1792. }
  1793. static int
  1794. find_entry_by_attr_value(const char *attribute, const char *value, Slapi_Entry **e, const Repl_Agmt *ra)
  1795. {
  1796. Slapi_PBlock *pb = slapi_pblock_new();
  1797. Slapi_Entry **entries = NULL, **ep = NULL;
  1798. Slapi_Entry *entry_found = NULL;
  1799. char *query = NULL;
  1800. int found_or_not = ENTRY_NOTFOUND;
  1801. int rval = 0;
  1802. const char *subtree_dn = NULL;
  1803. int not_unique = 0;
  1804. if (pb == NULL)
  1805. goto done;
  1806. query = slapi_ch_smprintf("(%s=%s)", attribute, value);
  1807. if (query == NULL)
  1808. goto done;
  1809. subtree_dn = slapi_sdn_get_dn(windows_private_get_directory_subtree(ra));
  1810. slapi_search_internal_set_pb(pb, subtree_dn,
  1811. LDAP_SCOPE_SUBTREE, query, NULL, 0, NULL, NULL,
  1812. (void *)plugin_get_default_component_id(), 0);
  1813. slapi_search_internal_pb(pb);
  1814. slapi_ch_free_string(&query);
  1815. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rval);
  1816. if (rval != LDAP_SUCCESS)
  1817. {
  1818. goto done;
  1819. }
  1820. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1821. if ((entries == NULL) || (entries[0] == NULL))
  1822. {
  1823. goto done;
  1824. }
  1825. entry_found = entries[0];
  1826. for (ep = entries; *ep; ep++) {
  1827. if (not_unique)
  1828. {
  1829. found_or_not = ENTRY_NOT_UNIQUE;
  1830. }
  1831. not_unique = 1;
  1832. }
  1833. done:
  1834. if (entry_found && (found_or_not != ENTRY_NOT_UNIQUE))
  1835. {
  1836. found_or_not = 0;
  1837. *e = slapi_entry_dup(entry_found);
  1838. }
  1839. if (pb)
  1840. {
  1841. slapi_free_search_results_internal(pb);
  1842. slapi_pblock_destroy(pb);
  1843. }
  1844. return found_or_not;
  1845. }
  1846. static int
  1847. find_entry_by_username(const char *username, Slapi_Entry **e, const Repl_Agmt *ra)
  1848. {
  1849. return find_entry_by_attr_value("ntUserDomainId",username,e,ra);
  1850. }
  1851. /* Find an entry in the local server given its GUID, or return ENTRY_NOTFOUND */
  1852. static int
  1853. find_entry_by_guid(const char *guid, Slapi_Entry **e, const Repl_Agmt *ra)
  1854. {
  1855. return find_entry_by_attr_value("ntUniqueId",guid,e,ra);
  1856. }
  1857. /* Remove dashes from a GUID string. */
  1858. static void
  1859. dedash_guid(char *str)
  1860. {
  1861. char *p = str;
  1862. char c = '\0';
  1863. while (c = *p)
  1864. {
  1865. if ('-' == c)
  1866. {
  1867. /* Move on down please */
  1868. char *q = p;
  1869. char *r = q + 1;
  1870. while (*r)
  1871. {
  1872. *q++ = *r++;
  1873. }
  1874. *q = '\0';
  1875. }
  1876. p++;
  1877. }
  1878. }
  1879. /* Add dashes into a GUID string. If the guid is not formatted properly,
  1880. * we will free it and set the pointer to NULL. */
  1881. static void
  1882. dash_guid(char **str)
  1883. {
  1884. if (strlen(*str) == NTUNIQUEID_LENGTH) {
  1885. char *p = NULL;
  1886. /* Add extra room for the dashes */
  1887. *str = slapi_ch_realloc(*str, AD_GUID_LENGTH + 1);
  1888. /* GUID needs to be in 8-4-4-4-12 format */
  1889. p = *str + 23;
  1890. memmove(p + 1, *str + 20, 12);
  1891. *p = '-';
  1892. p = *str + 18;
  1893. memmove(p + 1, *str + 16, 4);
  1894. *p = '-';
  1895. p = *str + 13;
  1896. memmove(p + 1, *str + 12, 4);
  1897. *p = '-';
  1898. p = *str + 8;
  1899. memmove(p + 1, *str + 8, 4);
  1900. *p = '-';
  1901. p = *str + 36;
  1902. *p = '\0';
  1903. } else {
  1904. /* This GUID does not appear to be valid */
  1905. slapi_ch_free_string(str);
  1906. }
  1907. }
  1908. /* For reasons not clear, the GUID returned in the tombstone DN is all
  1909. * messed up, like the guy in the movie 'the fly' after he want in the tranporter device */
  1910. static void
  1911. decrypt_guid(char *guid)
  1912. {
  1913. static int decrypt_offsets[] = {6,7,4,5,2,3,0,1,10,11,8,9,14,15,12,13,16,17,18,19,
  1914. 20,21,22,23,24,25,26,27,28,29,30,31};
  1915. char *p = guid;
  1916. int i = 0;
  1917. char *cpy = slapi_ch_strdup(guid);
  1918. while (*p && i < (sizeof(decrypt_offsets)/sizeof(int)))
  1919. {
  1920. *p = cpy[decrypt_offsets[i]];
  1921. p++;
  1922. i++;
  1923. }
  1924. slapi_ch_free_string(&cpy);
  1925. }
  1926. static char*
  1927. extract_guid_from_tombstone_dn(const char *dn)
  1928. {
  1929. char *guid = NULL;
  1930. char *colon_offset = NULL;
  1931. char *comma_offset = NULL;
  1932. /* example DN of tombstone:
  1933. "CN=WDel Userdb1\\\nDEL:551706bc-ecf2-4b38-9284-9a8554171d69,CN=Deleted Objects,DC=magpie,DC=com" */
  1934. /* First find the 'DEL:' */
  1935. colon_offset = strchr(dn,':');
  1936. /* Then scan forward to the next ',' */
  1937. comma_offset = strchr(dn,',');
  1938. /* The characters inbetween are the GUID, copy them to a new string and return to the caller */
  1939. if (comma_offset && colon_offset && comma_offset > colon_offset) {
  1940. guid = slapi_ch_malloc(comma_offset - colon_offset);
  1941. strncpy(guid,colon_offset+1,(comma_offset-colon_offset)-1);
  1942. guid[comma_offset-colon_offset-1] = '\0';
  1943. /* Finally remove the dashes since we don't store them on our side */
  1944. dedash_guid(guid);
  1945. decrypt_guid(guid);
  1946. }
  1947. return guid;
  1948. }
  1949. static char *
  1950. convert_to_hex(Slapi_Value *val)
  1951. {
  1952. int offset = 0;
  1953. const struct berval *bvp = NULL;
  1954. int length = 0;
  1955. char *result = NULL;
  1956. bvp = slapi_value_get_berval(val);
  1957. if (bvp)
  1958. {
  1959. char *new_buffer = NULL;
  1960. length = bvp->bv_len;
  1961. for (offset = 0; offset < length; offset++)
  1962. {
  1963. unsigned char byte = ((unsigned char*)(bvp->bv_val))[offset];
  1964. new_buffer = PR_sprintf_append(new_buffer, "%02x", byte );
  1965. }
  1966. if (new_buffer)
  1967. {
  1968. result = new_buffer;
  1969. }
  1970. }
  1971. return result;
  1972. }
  1973. /* Given a local entry, map it to it's AD tombstone DN. An AD
  1974. * tombstone DN is formatted like:
  1975. *
  1976. * cn=<cn>\0ADEL:<guid>,cn=Deleted Objects,<suffix>
  1977. *
  1978. * This function will allocate a new Slapi_DN. It is up to the
  1979. * caller to free it when they are finished with it. */
  1980. static int
  1981. map_windows_tombstone_dn(Slapi_Entry *e, Slapi_DN **dn, Private_Repl_Protocol *prp, int *exists)
  1982. {
  1983. int rc = 0;
  1984. char *cn = NULL;
  1985. char *guid = NULL;
  1986. const char *suffix = NULL;
  1987. char *tombstone_dn = NULL;
  1988. Slapi_Entry *tombstone = NULL;
  1989. /* Initialize the output values */
  1990. *dn = NULL;
  1991. *exists = 0;
  1992. cn = slapi_entry_attr_get_charptr(e,"cn");
  1993. if (!cn) {
  1994. cn = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  1995. }
  1996. guid = slapi_entry_attr_get_charptr(e,"ntUniqueId");
  1997. if (guid) {
  1998. /* the GUID is in a different form in the tombstone DN, so
  1999. * we need to transform it from the way we store it. */
  2000. decrypt_guid(guid);
  2001. dash_guid(&guid);
  2002. }
  2003. /* The tombstone suffix discards any containers, so we need
  2004. * to trim the DN to only dc components. */
  2005. if (suffix = slapi_sdn_get_dn(windows_private_get_windows_subtree(prp->agmt))) {
  2006. /* If this isn't found, it is treated as an error below. */
  2007. suffix = (const char *) strcasestr(suffix,"dc=");
  2008. }
  2009. if (cn && guid && suffix) {
  2010. tombstone_dn = PR_smprintf("cn=%s\\0ADEL:%s,cn=Deleted Objects,%s",
  2011. cn, guid, suffix);
  2012. /* Hand off the memory to the Slapi_DN */
  2013. *dn = slapi_sdn_new_dn_passin(tombstone_dn);
  2014. windows_get_remote_tombstone(prp, *dn, &tombstone);
  2015. if (tombstone) {
  2016. *exists = 1;
  2017. slapi_entry_free(tombstone);
  2018. }
  2019. } else {
  2020. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2021. "%s: map_windows_tombstone_dn: Failed to map dn=\"%s\" "
  2022. "to windows tombstone dn.\n", agmt_get_long_name(prp->agmt),
  2023. slapi_entry_get_dn(e));
  2024. rc = 1;
  2025. }
  2026. slapi_ch_free_string(&cn);
  2027. slapi_ch_free_string(&guid);
  2028. return rc;
  2029. }
  2030. static int is_guid_dn(Slapi_DN *remote_dn)
  2031. {
  2032. if ((remote_dn != NULL) && (strncasecmp("<GUID=",
  2033. slapi_sdn_get_dn(remote_dn), 6) == 0)) {
  2034. return 1;
  2035. } else {
  2036. return 0;
  2037. }
  2038. }
  2039. static char*
  2040. extract_guid_from_entry(Slapi_Entry *e, int is_nt4)
  2041. {
  2042. char *guid = NULL;
  2043. Slapi_Value *val = NULL;
  2044. Slapi_Attr *attr = NULL;
  2045. slapi_entry_attr_find(e, "objectGUID", &attr);
  2046. if (attr)
  2047. {
  2048. slapi_attr_first_value(attr, &val);
  2049. if (val) {
  2050. if (is_nt4)
  2051. {
  2052. guid = slapi_ch_strdup(slapi_value_get_string(val));
  2053. } else
  2054. {
  2055. guid = convert_to_hex(val);
  2056. }
  2057. }
  2058. }
  2059. return guid;
  2060. }
  2061. static void
  2062. extract_guid_from_entry_bv(Slapi_Entry *e, const struct berval **bv)
  2063. {
  2064. Slapi_Value *val = NULL;
  2065. Slapi_Attr *attr = NULL;
  2066. slapi_entry_attr_find(e, "objectGUID", &attr);
  2067. if (attr)
  2068. {
  2069. slapi_attr_first_value(attr, &val);
  2070. if (val) {
  2071. *bv = slapi_value_get_berval(val);
  2072. }
  2073. }
  2074. }
  2075. static char*
  2076. extract_username_from_entry(Slapi_Entry *e)
  2077. {
  2078. char *uid = NULL;
  2079. uid = slapi_entry_attr_get_charptr(e,"samAccountName");
  2080. return uid;
  2081. }
  2082. static char*
  2083. extract_ntuserdomainid_from_entry(Slapi_Entry *e)
  2084. {
  2085. char *uid = NULL;
  2086. uid = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  2087. return uid;
  2088. }
  2089. static Slapi_DN *make_dn_from_guid(char *guid, int is_nt4, const char* suffix)
  2090. {
  2091. Slapi_DN *new_dn = NULL;
  2092. char *dn_string = NULL;
  2093. if (guid)
  2094. {
  2095. new_dn = slapi_sdn_new();
  2096. if (is_nt4)
  2097. {
  2098. dn_string = PR_smprintf("GUID=%s,%s",guid,suffix);
  2099. } else
  2100. {
  2101. dn_string = PR_smprintf("<GUID=%s>",guid);
  2102. }
  2103. slapi_sdn_init_dn_byval(new_dn,dn_string);
  2104. PR_smprintf_free(dn_string);
  2105. }
  2106. /* dn string is now inside the Slapi_DN, and will be freed by its owner */
  2107. return new_dn;
  2108. }
  2109. static char*
  2110. extract_container(const Slapi_DN *entry_dn, const Slapi_DN *suffix_dn)
  2111. {
  2112. char *result = NULL;
  2113. /* First do a scope test to make sure that we weren't passed bogus arguments */
  2114. if (slapi_sdn_scope_test(entry_dn,suffix_dn,LDAP_SCOPE_SUBTREE))
  2115. {
  2116. Slapi_DN parent;
  2117. slapi_sdn_init(&parent);
  2118. /* Find the portion of the entry_dn between the RDN and the suffix */
  2119. /* Start with the parent of the entry DN */
  2120. slapi_sdn_get_parent(entry_dn, &parent);
  2121. /* Iterate finding the parent again until we have the suffix */
  2122. while (0 != slapi_sdn_compare(&parent,suffix_dn))
  2123. {
  2124. Slapi_DN child;
  2125. Slapi_RDN *rdn = slapi_rdn_new();
  2126. char *rdn_type = NULL;
  2127. char *rdn_str = NULL;
  2128. /* Append the current RDN to the new container string */
  2129. slapi_sdn_get_rdn(&parent,rdn);
  2130. slapi_rdn_get_first(rdn, &rdn_type, &rdn_str);
  2131. if (rdn_str)
  2132. {
  2133. result = PR_sprintf_append(result, "%s=%s,", rdn_type,rdn_str );
  2134. }
  2135. /* Don't free this until _after_ we've used the rdn_str */
  2136. slapi_rdn_free(&rdn);
  2137. /* Move to the next successive parent */
  2138. slapi_sdn_init(&child);
  2139. slapi_sdn_copy(&parent,&child);
  2140. slapi_sdn_done(&parent);
  2141. slapi_sdn_get_parent(&child, &parent);
  2142. slapi_sdn_done(&child);
  2143. }
  2144. slapi_sdn_done(&parent);
  2145. }
  2146. /* Always return something */
  2147. if (NULL == result)
  2148. {
  2149. result = slapi_ch_strdup("");
  2150. }
  2151. return result;
  2152. }
  2153. /* Given a non-tombstone entry, return the DN of its peer in AD (whether present or not) */
  2154. static int
  2155. map_entry_dn_outbound(Slapi_Entry *e, Slapi_DN **dn, Private_Repl_Protocol *prp, int *missing_entry, int guid_form)
  2156. {
  2157. int retval = 0;
  2158. char *guid = NULL;
  2159. Slapi_DN *new_dn = NULL;
  2160. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  2161. const char *suffix = slapi_sdn_get_dn(windows_private_get_windows_subtree(prp->agmt));
  2162. /* To find the DN of the peer entry we first look for an ntUniqueId attribute
  2163. * on the local entry. If that's present, we generate a GUID-form DN.
  2164. * If there's no GUID, then we look for an ntUserDomainId attribute
  2165. * on the entry. If that's present we attempt to search for an entry with
  2166. * that samaccountName attribute value in AD. If we don't find any matching
  2167. * entry we generate a new DN using the entry's cn. If later, we find that
  2168. * this entry already exists, we handle that problem at the time. We don't
  2169. * check here. Note: for NT4 we always use ntUserDomainId for the samaccountname rdn, never cn.
  2170. */
  2171. *missing_entry = 0;
  2172. guid = slapi_entry_attr_get_charptr(e,"ntUniqueId");
  2173. if (guid && guid_form)
  2174. {
  2175. int rc = 0;
  2176. Slapi_Entry *remote_entry = NULL;
  2177. new_dn = make_dn_from_guid(guid, is_nt4, suffix);
  2178. slapi_ch_free_string(&guid);
  2179. /* There are certain cases where we will have a GUID, but the entry does not exist in
  2180. * AD. This happens when you delete an entry, then add it back elsewhere in the tree
  2181. * without removing the ntUniqueID attribute. We should verify that the entry really
  2182. * exists in AD. */
  2183. rc = windows_get_remote_entry(prp, new_dn, &remote_entry);
  2184. if (0 == rc && remote_entry) {
  2185. slapi_entry_free(remote_entry);
  2186. } else {
  2187. /* We need to re-write the DN to a non-GUID DN if we're syncing to a
  2188. * Windows 2000 Server since tombstone reanimation is not supported.
  2189. * If we're syncing with Windows 2003 Server, we'll just use the GUID
  2190. * to reanimate the tombstone when processing the add operation. */
  2191. *missing_entry = 1;
  2192. if (!windows_private_get_iswin2k3(prp->agmt)) {
  2193. char *new_dn_string = NULL;
  2194. char *cn_string = NULL;
  2195. /* We can't use a GUID DN, so rewrite to the mapped DN. */
  2196. cn_string = slapi_entry_attr_get_charptr(e,"cn");
  2197. if (!cn_string) {
  2198. cn_string = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  2199. }
  2200. if (cn_string) {
  2201. char *rdnstr = NULL;
  2202. char *container_str = NULL;
  2203. container_str = extract_container(slapi_entry_get_sdn_const(e),
  2204. windows_private_get_directory_subtree(prp->agmt));
  2205. new_dn_string = PR_smprintf("cn=%s,%s%s", cn_string, container_str, suffix);
  2206. if (new_dn_string) {
  2207. if (new_dn) {
  2208. slapi_sdn_free(&new_dn);
  2209. }
  2210. new_dn = slapi_sdn_new_dn_byval(new_dn_string);
  2211. PR_smprintf_free(new_dn_string);
  2212. }
  2213. slapi_ch_free_string(&cn_string);
  2214. slapi_ch_free_string(&container_str);
  2215. }
  2216. }
  2217. }
  2218. } else
  2219. {
  2220. /* No GUID found, try ntUserDomainId */
  2221. Slapi_Entry *remote_entry = NULL;
  2222. char *username = slapi_entry_attr_get_charptr(e,"ntUserDomainId");
  2223. if (username) {
  2224. retval = find_entry_by_attr_value_remote("samAccountName",username,&remote_entry,prp);
  2225. if (0 == retval && remote_entry)
  2226. {
  2227. /* Get the entry's DN */
  2228. new_dn = slapi_sdn_new();
  2229. slapi_sdn_copy(slapi_entry_get_sdn_const(remote_entry), new_dn);
  2230. } else {
  2231. if (0 == retval)
  2232. {
  2233. char *new_dn_string = NULL;
  2234. char *cn_string = NULL;
  2235. *missing_entry = 1;
  2236. /* This means that we failed to find a peer entry */
  2237. /* In that case we need to generate the DN that we want to use */
  2238. /* Generated DN's take the form :
  2239. cn=<cn from local entry>, ... in the case that the local entry has a cn, OR
  2240. cn=<ntuserdomainid attribute value>, ... in the case that the local entry doesn't have a CN
  2241. */
  2242. if (is_nt4)
  2243. {
  2244. cn_string = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  2245. } else
  2246. {
  2247. cn_string = slapi_entry_attr_get_charptr(e,"cn");
  2248. if (!cn_string)
  2249. {
  2250. cn_string = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  2251. }
  2252. }
  2253. if (cn_string)
  2254. {
  2255. char *rdnstr = NULL;
  2256. char *container_str = NULL;
  2257. container_str = extract_container(slapi_entry_get_sdn_const(e), windows_private_get_directory_subtree(prp->agmt));
  2258. rdnstr = is_nt4 ? "samaccountname=%s,%s%s" : "cn=%s,%s%s";
  2259. new_dn_string = PR_smprintf(rdnstr,cn_string,container_str,suffix);
  2260. if (new_dn_string)
  2261. {
  2262. new_dn = slapi_sdn_new_dn_byval(new_dn_string);
  2263. PR_smprintf_free(new_dn_string);
  2264. }
  2265. slapi_ch_free_string(&cn_string);
  2266. slapi_ch_free_string(&container_str);
  2267. }
  2268. } else
  2269. {
  2270. /* This means that we failed to talk to the AD for some reason, the operation should be re-tried */
  2271. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2272. "%s: map_entry_dn_outbound: failed to fetch entry from AD: dn=\"%s\", err=%d\n",
  2273. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)), retval);
  2274. retval = -1;
  2275. }
  2276. }
  2277. slapi_ch_free_string(&username);
  2278. }
  2279. if (remote_entry)
  2280. {
  2281. slapi_entry_free(remote_entry);
  2282. }
  2283. }
  2284. if (new_dn)
  2285. {
  2286. *dn = new_dn;
  2287. }
  2288. return retval;
  2289. }
  2290. /* Given a tombstone entry, return the DN of its peer in this server (if present) */
  2291. static int
  2292. map_tombstone_dn_inbound(Slapi_Entry *e, Slapi_DN **dn, const Repl_Agmt *ra)
  2293. {
  2294. int retval = 0;
  2295. Slapi_DN *new_dn = NULL;
  2296. char *guid = NULL;
  2297. const char *dn_string = NULL;
  2298. Slapi_Entry *matching_entry = NULL;
  2299. /* To map a tombstone's DN we need to first extract the entry's objectGUID from the DN
  2300. * CN=vpdxtAD_07\
  2301. DEL:d4ca4e16-e35b-400d-834a-f02db600f3fa,CN=Deleted Objects,DC=magpie,DC=com
  2302. */
  2303. *dn = NULL;
  2304. dn_string = slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)); /* This is a pointer from inside the sdn, no need to free */
  2305. guid = extract_guid_from_tombstone_dn(dn_string);
  2306. if (guid)
  2307. {
  2308. retval = find_entry_by_guid(guid,&matching_entry,ra);
  2309. if (retval)
  2310. {
  2311. if (ENTRY_NOTFOUND == retval)
  2312. {
  2313. } else
  2314. {
  2315. if (ENTRY_NOT_UNIQUE == retval)
  2316. {
  2317. } else
  2318. {
  2319. /* A real error */
  2320. }
  2321. }
  2322. } else
  2323. {
  2324. /* We found the matching entry : get its DN */
  2325. new_dn = slapi_sdn_dup(slapi_entry_get_sdn_const(matching_entry));
  2326. }
  2327. }
  2328. if (new_dn)
  2329. {
  2330. *dn = new_dn;
  2331. }
  2332. if (guid)
  2333. {
  2334. slapi_ch_free_string(&guid);
  2335. }
  2336. if (matching_entry)
  2337. {
  2338. slapi_entry_free(matching_entry);
  2339. }
  2340. return retval;
  2341. }
  2342. /* Given a non-tombstone entry, return the DN of its peer in this server (whether present or not) */
  2343. static int
  2344. map_entry_dn_inbound(Slapi_Entry *e, Slapi_DN **dn, const Repl_Agmt *ra)
  2345. {
  2346. int retval = 0;
  2347. Slapi_DN *new_dn = NULL;
  2348. char *guid = NULL;
  2349. char *username = NULL;
  2350. Slapi_Entry *matching_entry = NULL;
  2351. int is_user = 0;
  2352. int is_group = 0;
  2353. int is_nt4 = windows_private_get_isnt4(ra);
  2354. /* To map a non-tombstone's DN we need to first try to look it up by GUID.
  2355. * If we do not find it, then we need to generate the DN that it would have if added as a new entry.
  2356. */
  2357. *dn = NULL;
  2358. windows_is_remote_entry_user_or_group(e,&is_user,&is_group);
  2359. guid = extract_guid_from_entry(e, is_nt4);
  2360. if (guid)
  2361. {
  2362. retval = find_entry_by_guid(guid,&matching_entry,ra);
  2363. if (retval)
  2364. {
  2365. if (ENTRY_NOTFOUND == retval)
  2366. {
  2367. } else
  2368. {
  2369. if (ENTRY_NOT_UNIQUE == retval)
  2370. {
  2371. } else
  2372. {
  2373. /* A real error */
  2374. goto error;
  2375. }
  2376. }
  2377. } else
  2378. {
  2379. /* We found the matching entry : get its DN */
  2380. new_dn = slapi_sdn_dup(slapi_entry_get_sdn_const(matching_entry));
  2381. }
  2382. }
  2383. /* If we failed to lookup by guid, try samaccountname */
  2384. if (NULL == new_dn)
  2385. {
  2386. username = extract_username_from_entry(e);
  2387. if (username) {
  2388. retval = find_entry_by_username(username,&matching_entry,ra);
  2389. if (retval)
  2390. {
  2391. if (ENTRY_NOTFOUND == retval)
  2392. {
  2393. } else
  2394. {
  2395. if (ENTRY_NOT_UNIQUE == retval)
  2396. {
  2397. } else
  2398. {
  2399. /* A real error */
  2400. goto error;
  2401. }
  2402. }
  2403. } else
  2404. {
  2405. /* We found the matching entry : get its DN */
  2406. new_dn = slapi_sdn_dup(slapi_entry_get_sdn_const(matching_entry));
  2407. }
  2408. }
  2409. }
  2410. /* If we couldn't find a matching entry by either method, then we need to invent a new DN */
  2411. if (NULL == new_dn)
  2412. {
  2413. /* The new DN has the form: uid=<samaccountname>,<sync'ed subtree> */
  2414. /* If an entry with this DN already exists, we fail and return no DN
  2415. * this is because we don't want to second-guess what the admin wants here:
  2416. * they may want to associate this existing entry with the peer AD entry,
  2417. * but if they intend that we say they must add the ntDomainUserId attribute to
  2418. * that entry.
  2419. */
  2420. char *new_dn_string = NULL;
  2421. if (username)
  2422. {
  2423. const char *suffix = slapi_sdn_get_dn(windows_private_get_directory_subtree(ra));
  2424. char *container_str = NULL;
  2425. container_str = extract_container(slapi_entry_get_sdn_const(e), windows_private_get_windows_subtree(ra));
  2426. /* Local DNs for users and groups are different */
  2427. if (is_user)
  2428. {
  2429. new_dn_string = PR_smprintf("uid=%s,%s%s",username,container_str,suffix);
  2430. } else
  2431. {
  2432. new_dn_string = PR_smprintf("cn=%s,%s%s",username,container_str,suffix);
  2433. }
  2434. new_dn = slapi_sdn_new_dn_byval(new_dn_string);
  2435. PR_smprintf_free(new_dn_string);
  2436. slapi_ch_free_string(&container_str);
  2437. /* Clear any earlier error */
  2438. retval = 0;
  2439. } else
  2440. {
  2441. /* Error, no username */
  2442. }
  2443. }
  2444. if (new_dn)
  2445. {
  2446. *dn = new_dn;
  2447. }
  2448. error:
  2449. if (guid)
  2450. {
  2451. PR_smprintf_free(guid);
  2452. }
  2453. if (matching_entry)
  2454. {
  2455. slapi_entry_free(matching_entry);
  2456. }
  2457. if (username)
  2458. {
  2459. slapi_ch_free_string(&username);
  2460. }
  2461. return retval;
  2462. }
  2463. /* 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
  2464. * and does it have the right attribute values for sync ?)
  2465. */
  2466. static int
  2467. is_subject_of_agreement_local(const Slapi_Entry *local_entry, const Repl_Agmt *ra)
  2468. {
  2469. int retval = 0;
  2470. int is_in_subtree = 0;
  2471. const Slapi_DN *agreement_subtree = NULL;
  2472. /* First test for the sync'ed subtree */
  2473. agreement_subtree = windows_private_get_directory_subtree(ra);
  2474. if (NULL == agreement_subtree)
  2475. {
  2476. goto error;
  2477. }
  2478. is_in_subtree = slapi_sdn_scope_test(slapi_entry_get_sdn_const(local_entry), agreement_subtree, LDAP_SCOPE_SUBTREE);
  2479. if (is_in_subtree)
  2480. {
  2481. /* Next test for the correct kind of entry */
  2482. if (local_entry) {
  2483. if (slapi_filter_test_simple( (Slapi_Entry*)local_entry,
  2484. (Slapi_Filter*)windows_private_get_directory_filter(ra)) == 0)
  2485. {
  2486. retval = 1;
  2487. }
  2488. } else
  2489. {
  2490. /* Error: couldn't find the entry */
  2491. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  2492. "failed to find entry in is_subject_of_agreement_local: %d\n", retval);
  2493. retval = 0;
  2494. }
  2495. }
  2496. error:
  2497. return retval;
  2498. }
  2499. /* 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 ?) */
  2500. static int
  2501. is_subject_of_agreement_remote(Slapi_Entry *e, const Repl_Agmt *ra)
  2502. {
  2503. int retval = 0;
  2504. int is_in_subtree = 0;
  2505. const Slapi_DN *agreement_subtree = NULL;
  2506. /* First test for the sync'ed subtree */
  2507. agreement_subtree = windows_private_get_windows_subtree(ra);
  2508. if (NULL == agreement_subtree)
  2509. {
  2510. goto error;
  2511. }
  2512. is_in_subtree = slapi_sdn_scope_test(slapi_entry_get_sdn_const(e), agreement_subtree, LDAP_SCOPE_SUBTREE);
  2513. if (is_in_subtree)
  2514. {
  2515. retval = 1;
  2516. }
  2517. error:
  2518. return retval;
  2519. }
  2520. static int
  2521. windows_create_local_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,const Slapi_DN* local_sdn)
  2522. {
  2523. int retval = 0;
  2524. char *entry_string = NULL;
  2525. Slapi_Entry *local_entry = NULL;
  2526. Slapi_PBlock* pb = NULL;
  2527. int is_user = 0;
  2528. int is_group = 0;
  2529. char *local_entry_template = NULL;
  2530. char *user_entry_template = NULL;
  2531. char *username = extract_username_from_entry(remote_entry);
  2532. Slapi_Attr *attr = NULL;
  2533. int rc = 0;
  2534. char *guid_str = NULL;
  2535. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  2536. char *local_user_entry_template =
  2537. "dn: %s\n"
  2538. "objectclass:top\n"
  2539. "objectclass:person\n"
  2540. "objectclass:organizationalperson\n"
  2541. "objectclass:inetOrgPerson\n"
  2542. "objectclass:ntUser\n"
  2543. "ntUserDeleteAccount:true\n"
  2544. "uid:%s\n";
  2545. char *local_nt4_user_entry_template =
  2546. "dn: %s\n"
  2547. "objectclass:top\n"
  2548. "objectclass:person\n"
  2549. "objectclass:organizationalperson\n"
  2550. "objectclass:inetOrgPerson\n"
  2551. "objectclass:ntUser\n"
  2552. "ntUserDeleteAccount:true\n"
  2553. "uid:%s\n";
  2554. char *local_group_entry_template =
  2555. "dn: %s\n"
  2556. "objectclass:top\n"
  2557. "objectclass:groupofuniquenames\n"
  2558. "objectclass:ntGroup\n"
  2559. "objectclass:mailGroup\n"
  2560. "ntGroupDeleteGroup:true\n"
  2561. "cn:%s\n";
  2562. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_create_local_entry\n", 0, 0, 0 );
  2563. windows_is_remote_entry_user_or_group(remote_entry,&is_user,&is_group);
  2564. user_entry_template = is_nt4 ? local_nt4_user_entry_template : local_user_entry_template;
  2565. local_entry_template = is_user ? user_entry_template : local_group_entry_template;
  2566. /* Create a new entry */
  2567. /* Give it its DN and username */
  2568. entry_string = slapi_ch_smprintf(local_entry_template,slapi_sdn_get_dn(local_sdn),username, username);
  2569. if (NULL == entry_string)
  2570. {
  2571. goto error;
  2572. }
  2573. local_entry = slapi_str2entry(entry_string, 0);
  2574. slapi_ch_free_string(&entry_string);
  2575. if (NULL == local_entry)
  2576. {
  2577. goto error;
  2578. }
  2579. /* Map the appropriate attributes sourced from the remote entry */
  2580. for (rc = slapi_entry_first_attr(remote_entry, &attr); rc == 0;
  2581. rc = slapi_entry_next_attr(remote_entry, attr, &attr))
  2582. {
  2583. char *type = NULL;
  2584. Slapi_ValueSet *vs = NULL;
  2585. int mapdn = 0;
  2586. slapi_attr_get_type( attr, &type );
  2587. slapi_attr_get_valueset(attr,&vs);
  2588. if ( is_straight_mapped_attr(type,is_user,is_nt4) )
  2589. {
  2590. /* copy over the attr values */
  2591. slapi_entry_add_valueset(local_entry,type,vs);
  2592. } else
  2593. {
  2594. char *new_type = NULL;
  2595. windows_map_attr_name(type , 0 /* from windows */, is_user, 1 /* create */, &new_type, &mapdn);
  2596. if (new_type)
  2597. {
  2598. if (mapdn)
  2599. {
  2600. Slapi_ValueSet *mapped_values = NULL;
  2601. map_dn_values(prp,vs,&mapped_values, 0 /* from windows */,0);
  2602. if (mapped_values)
  2603. {
  2604. slapi_entry_add_valueset(local_entry,new_type,mapped_values);
  2605. slapi_valueset_free(mapped_values);
  2606. mapped_values = NULL;
  2607. }
  2608. } else
  2609. {
  2610. slapi_entry_add_valueset(local_entry,new_type,vs);
  2611. }
  2612. slapi_ch_free_string(&new_type);
  2613. }
  2614. }
  2615. if (vs)
  2616. {
  2617. slapi_valueset_free(vs);
  2618. vs = NULL;
  2619. }
  2620. }
  2621. /* Copy over the GUID */
  2622. guid_str = extract_guid_from_entry(remote_entry, is_nt4);
  2623. if (guid_str)
  2624. {
  2625. slapi_entry_add_string(local_entry,"ntUniqueId",guid_str);
  2626. } else
  2627. {
  2628. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2629. "extract_guid_from_entry entry %s failed to extract the guid\n", slapi_sdn_get_dn(local_sdn));
  2630. /* Fatal error : need the guid */
  2631. goto error;
  2632. }
  2633. /* Hack for NT4, which has no surname */
  2634. if (is_nt4 && is_user)
  2635. {
  2636. slapi_entry_add_string(local_entry,"sn",username);
  2637. }
  2638. /* Store it */
  2639. windows_dump_entry("Adding new local entry",local_entry);
  2640. pb = slapi_pblock_new();
  2641. slapi_add_entry_internal_set_pb(pb, local_entry, NULL,repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),0);
  2642. slapi_add_internal_pb(pb);
  2643. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &retval);
  2644. if (retval) {
  2645. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2646. "add operation of entry %s returned: %d\n", slapi_sdn_get_dn(local_sdn), retval);
  2647. }
  2648. error:
  2649. if (pb)
  2650. {
  2651. slapi_pblock_destroy(pb);
  2652. }
  2653. if (username)
  2654. {
  2655. slapi_ch_free_string(&username);
  2656. }
  2657. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_create_local_entry\n", 0, 0, 0 );
  2658. return retval;
  2659. }
  2660. /* Function to generate the correct mods to bring 'local' attribute values into consistency with given 'remote' values */
  2661. /* 'local' and 'remote' in quotes because we actually use this function in both directions */
  2662. /* This function expects the value sets to have been already pruned to exclude values that are not
  2663. * subject to the agreement and present in the peer. */
  2664. static int
  2665. 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)
  2666. {
  2667. int ret = 0;
  2668. int i = 0;
  2669. Slapi_Value *rv = NULL;
  2670. Slapi_Value *lv = NULL;
  2671. /* We need to generate an ADD mod for each entry that is in the remote values but not in the local values */
  2672. /* Iterate over the remote values */
  2673. i = slapi_valueset_first_value(remote_values,&rv);
  2674. while (NULL != rv)
  2675. {
  2676. const char *remote_dn = slapi_value_get_string(rv);
  2677. int value_present_in_local_values = (NULL != slapi_valueset_find(attr, local_values, rv));
  2678. if (!value_present_in_local_values)
  2679. {
  2680. slapi_mods_add_string(smods,LDAP_MOD_ADD,local_type,remote_dn);
  2681. *do_modify = 1;
  2682. }
  2683. i = slapi_valueset_next_value(remote_values,i,&rv);
  2684. }
  2685. /* We need to generate a DEL mod for each entry that is in the local values but not in the remote values */
  2686. /* Iterate over the local values */
  2687. i = slapi_valueset_first_value(local_values,&lv);
  2688. while (NULL != lv)
  2689. {
  2690. const char *local_dn = slapi_value_get_string(lv);
  2691. int value_present_in_remote_values = (NULL != slapi_valueset_find(attr, remote_values, lv));
  2692. if (!value_present_in_remote_values)
  2693. {
  2694. slapi_mods_add_string(smods,LDAP_MOD_DELETE,local_type,local_dn);
  2695. *do_modify = 1;
  2696. }
  2697. i = slapi_valueset_next_value(local_values,i,&lv);
  2698. }
  2699. return ret;
  2700. }
  2701. /* Generate the mods for an update in either direction. Be careful... the "remote" entry is the DS entry in the to_windows case, but the AD entry in the other case. */
  2702. static int
  2703. 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)
  2704. {
  2705. int retval = 0;
  2706. Slapi_Attr *attr = NULL;
  2707. Slapi_Attr *del_attr = NULL;
  2708. int is_user = 0;
  2709. int is_group = 0;
  2710. int rc = 0;
  2711. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  2712. /* Iterate over the attributes on the remote entry, updating the local entry where appropriate */
  2713. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_generate_update_mods\n", 0, 0, 0 );
  2714. *do_modify = 0;
  2715. if (to_windows)
  2716. {
  2717. windows_is_local_entry_user_or_group(remote_entry,&is_user,&is_group);
  2718. } else
  2719. {
  2720. windows_is_remote_entry_user_or_group(remote_entry,&is_user,&is_group);
  2721. }
  2722. for (rc = slapi_entry_first_attr(remote_entry, &attr); rc == 0;
  2723. rc = slapi_entry_next_attr(remote_entry, attr, &attr))
  2724. {
  2725. int is_present_local = 0;
  2726. char *type = NULL;
  2727. Slapi_ValueSet *vs = NULL;
  2728. char *local_type = NULL;
  2729. Slapi_Attr *local_attr = NULL;
  2730. int is_guid = 0;
  2731. int mapdn = 0;
  2732. slapi_attr_get_type( attr, &type );
  2733. slapi_attr_get_valueset(attr,&vs);
  2734. /* First determine what we will do with this attr */
  2735. /* If it's a GUID, we need to take special action */
  2736. if (0 == slapi_attr_type_cmp(type,"objectGuid",SLAPI_TYPE_CMP_SUBTYPE) && !to_windows)
  2737. {
  2738. is_guid = 1;
  2739. local_type = slapi_ch_strdup("ntUniqueId");
  2740. } else
  2741. {
  2742. if ( is_straight_mapped_attr(type,is_user,is_nt4) ) {
  2743. local_type = slapi_ch_strdup(type);
  2744. } else {
  2745. windows_map_attr_name(type , to_windows, is_user, 0 /* not create */, &local_type, &mapdn);
  2746. }
  2747. is_guid = 0;
  2748. }
  2749. if (NULL == local_type)
  2750. {
  2751. /* Means we do not map this attribute */
  2752. if (vs)
  2753. {
  2754. slapi_valueset_free(vs);
  2755. vs = NULL;
  2756. }
  2757. continue;
  2758. }
  2759. if (to_windows && (0 == slapi_attr_type_cmp(local_type, "streetAddress", SLAPI_TYPE_CMP_SUBTYPE))) {
  2760. slapi_entry_attr_find(local_entry,FAKE_STREET_ATTR_NAME,&local_attr);
  2761. } else {
  2762. slapi_entry_attr_find(local_entry,local_type,&local_attr);
  2763. }
  2764. is_present_local = (NULL == local_attr) ? 0 : 1;
  2765. /* Is the attribute present on the local entry ? */
  2766. if (is_present_local && !is_guid)
  2767. {
  2768. if (!mapdn)
  2769. {
  2770. int values_equal = 0;
  2771. /* AD has a legth contraint on the initials attribute,
  2772. * so treat is as a special case. */
  2773. if (0 == slapi_attr_type_cmp(type, "initials", SLAPI_TYPE_CMP_SUBTYPE)) {
  2774. values_equal = attr_compare_equal(attr, local_attr, AD_INITIALS_LENGTH);
  2775. /* If we're getting a streetAddress (a fake attr name is used) from AD, then
  2776. * we just check if the value in AD is present in our entry in DS. In this
  2777. * case, attr is from the AD entry, and local_attr is from the DS entry. */
  2778. } else if (0 == slapi_attr_type_cmp(type, FAKE_STREET_ATTR_NAME, SLAPI_TYPE_CMP_SUBTYPE) && !to_windows) {
  2779. values_equal = attr_compare_present(attr, local_attr);
  2780. /* If we are checking if we should send a street attribute to AD, then
  2781. * we want to first see if the AD entry already contains any street value
  2782. * that is present in the DS entry. In this case, attr is from the DS
  2783. * entry, and local_attr is from the AD entry. */
  2784. } else if ((0 == slapi_attr_type_cmp(type, "street", SLAPI_TYPE_CMP_SUBTYPE) && to_windows)) {
  2785. values_equal = attr_compare_present(local_attr, attr);
  2786. } else {
  2787. /* Compare the entire attribute values */
  2788. values_equal = attr_compare_equal(attr, local_attr, 0);
  2789. }
  2790. /* If it is then we need to replace the local values with the remote values if they are different */
  2791. if (!values_equal)
  2792. {
  2793. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2794. "windows_generate_update_mods: %s, %s : values are different\n",
  2795. slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry)), local_type);
  2796. if ((0 == slapi_attr_type_cmp(local_type, "streetAddress",
  2797. SLAPI_TYPE_CMP_SUBTYPE) && to_windows)) {
  2798. /* streetAddress is single-valued in AD, so make
  2799. * sure we don't try to send more than one value. */
  2800. if (slapi_valueset_count(vs) > 1) {
  2801. int i = 0;
  2802. const char *street_value = NULL;
  2803. Slapi_Value *value = NULL;
  2804. Slapi_Value *new_value = NULL;
  2805. i = slapi_valueset_first_value(vs,&value);
  2806. if (i >= 0) {
  2807. /* Dup the first value, trash the valueset, then copy
  2808. * in the dup'd value. */
  2809. new_value = slapi_value_dup(value);
  2810. slapi_valueset_done(vs);
  2811. /* The below hands off the memory to the valueset */
  2812. slapi_valueset_add_value_ext(vs, new_value, SLAPI_VALUE_FLAG_PASSIN);
  2813. }
  2814. }
  2815. } else if ((0 == slapi_attr_type_cmp(local_type, "initials",
  2816. SLAPI_TYPE_CMP_SUBTYPE) && to_windows)) {
  2817. /* initials is constratined to a max length of
  2818. * 6 characters in AD, so trim the value if
  2819. * needed before sending. */
  2820. int i = 0;
  2821. const char *initials_value = NULL;
  2822. Slapi_Value *value = NULL;
  2823. i = slapi_valueset_first_value(vs,&value);
  2824. while (i >= 0) {
  2825. initials_value = slapi_value_get_string(value);
  2826. /* If > AD_INITIALS_LENGTH, trim the value */
  2827. if (strlen(initials_value) > AD_INITIALS_LENGTH) {
  2828. char *new_initials = PL_strndup(initials_value, AD_INITIALS_LENGTH);
  2829. /* the below hands off memory */
  2830. slapi_value_set_string_passin(value, new_initials);
  2831. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2832. "%s: windows_generate_update_mods: "
  2833. "Trimming initials attribute to %d characters.\n",
  2834. agmt_get_long_name(prp->agmt), AD_INITIALS_LENGTH);
  2835. }
  2836. i = slapi_valueset_next_value(vs, i, &value);
  2837. }
  2838. }
  2839. slapi_mods_add_mod_values(smods,LDAP_MOD_REPLACE,
  2840. local_type,valueset_get_valuearray(vs));
  2841. *do_modify = 1;
  2842. } else
  2843. {
  2844. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2845. "windows_generate_update_mods: %s, %s : values are equal\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry)), local_type);
  2846. }
  2847. } else {
  2848. /* A dn-valued attribute : need to take special steps */
  2849. Slapi_ValueSet *mapped_remote_values = NULL;
  2850. /* First map all the DNs to that they're in a consistent domain */
  2851. map_dn_values(prp,vs,&mapped_remote_values, to_windows,0);
  2852. if (mapped_remote_values)
  2853. {
  2854. Slapi_ValueSet *local_values = NULL;
  2855. Slapi_ValueSet *restricted_local_values = NULL;
  2856. /* Now do a compare on the values, generating mods to bring them into consistency (if any) */
  2857. /* We ignore any DNs that are outside the scope of the agreement (on both sides) */
  2858. slapi_attr_get_valueset(local_attr,&local_values);
  2859. map_dn_values(prp,local_values,&restricted_local_values,!to_windows,1);
  2860. if (restricted_local_values)
  2861. {
  2862. windows_generate_dn_value_mods(local_type,local_attr,smods,mapped_remote_values,restricted_local_values,do_modify);
  2863. slapi_valueset_free(restricted_local_values);
  2864. restricted_local_values = NULL;
  2865. }
  2866. slapi_valueset_free(mapped_remote_values);
  2867. mapped_remote_values = NULL;
  2868. if (local_values)
  2869. {
  2870. slapi_valueset_free(local_values);
  2871. local_values = NULL;
  2872. }
  2873. }
  2874. }
  2875. } else
  2876. {
  2877. if (!is_present_local)
  2878. {
  2879. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2880. "windows_generate_update_mods: %s, %s : values not present on peer entry\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry)), local_type);
  2881. /* If it is currently absent, then we add the value from the remote entry */
  2882. if (is_guid)
  2883. {
  2884. /* Translate the guid value */
  2885. char *guid = extract_guid_from_entry(remote_entry, is_nt4);
  2886. if (guid)
  2887. {
  2888. slapi_mods_add_string(smods,LDAP_MOD_ADD,local_type,guid);
  2889. slapi_ch_free_string(&guid);
  2890. }
  2891. } else
  2892. {
  2893. /* Handle DN valued attributes here */
  2894. if (mapdn)
  2895. {
  2896. Slapi_ValueSet *mapped_values = NULL;
  2897. map_dn_values(prp,vs,&mapped_values, to_windows,0);
  2898. if (mapped_values)
  2899. {
  2900. slapi_mods_add_mod_values(smods,LDAP_MOD_ADD,local_type,valueset_get_valuearray(mapped_values));
  2901. slapi_valueset_free(mapped_values);
  2902. mapped_values = NULL;
  2903. }
  2904. } else
  2905. {
  2906. if ((0 == slapi_attr_type_cmp(local_type, "streetAddress",
  2907. SLAPI_TYPE_CMP_SUBTYPE) && to_windows)) {
  2908. /* streetAddress is single-valued in AD, so make
  2909. * sure we don't try to send more than one value. */
  2910. if (slapi_valueset_count(vs) > 1) {
  2911. int i = 0;
  2912. const char *street_value = NULL;
  2913. Slapi_Value *value = NULL;
  2914. Slapi_Value *new_value = NULL;
  2915. i = slapi_valueset_first_value(vs,&value);
  2916. if (i >= 0) {
  2917. /* Dup the first value, trash the valueset, then copy
  2918. * in the dup'd value. */
  2919. new_value = slapi_value_dup(value);
  2920. slapi_valueset_done(vs);
  2921. /* The below hands off the memory to the valueset */
  2922. slapi_valueset_add_value_ext(vs, new_value, SLAPI_VALUE_FLAG_PASSIN);
  2923. }
  2924. }
  2925. } else if ((0 == slapi_attr_type_cmp(local_type, "initials",
  2926. SLAPI_TYPE_CMP_SUBTYPE) && to_windows)) {
  2927. /* initials is constratined to a max length of
  2928. * 6 characters in AD, so trim the value if
  2929. * needed before sending. */
  2930. int i = 0;
  2931. const char *initials_value = NULL;
  2932. Slapi_Value *value = NULL;
  2933. i = slapi_valueset_first_value(vs,&value);
  2934. while (i >= 0) {
  2935. initials_value = slapi_value_get_string(value);
  2936. /* If > AD_INITIALS_LENGTH, trim the value */
  2937. if (strlen(initials_value) > AD_INITIALS_LENGTH) {
  2938. char *new_initials = PL_strndup(initials_value, AD_INITIALS_LENGTH);
  2939. /* the below hands off memory */
  2940. slapi_value_set_string_passin(value, new_initials);
  2941. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2942. "%s: windows_generate_update_mods: "
  2943. "Trimming initials attribute to %d characters.\n",
  2944. agmt_get_long_name(prp->agmt), AD_INITIALS_LENGTH);
  2945. }
  2946. i = slapi_valueset_next_value(vs, i, &value);
  2947. }
  2948. }
  2949. slapi_mods_add_mod_values(smods,LDAP_MOD_ADD,local_type,valueset_get_valuearray(vs));
  2950. }
  2951. }
  2952. *do_modify = 1;
  2953. }
  2954. }
  2955. if (vs)
  2956. {
  2957. slapi_valueset_free(vs);
  2958. vs = NULL;
  2959. }
  2960. slapi_ch_free_string(&local_type);
  2961. }
  2962. /* Check if any attributes were deleted from the remote entry */
  2963. entry_first_deleted_attribute(remote_entry, &del_attr);
  2964. while (del_attr != NULL) {
  2965. Slapi_Attr *local_attr = NULL;
  2966. char *type = NULL;
  2967. char *local_type = NULL;
  2968. int mapdn = 0;
  2969. /* Map remote type to local type */
  2970. slapi_attr_get_type(del_attr, &type);
  2971. if ( is_straight_mapped_attr(type,is_user,is_nt4) ) {
  2972. local_type = slapi_ch_strdup(type);
  2973. } else {
  2974. windows_map_attr_name(type , to_windows, is_user, 0 /* not create */, &local_type, &mapdn);
  2975. }
  2976. /* Check if this attr exists in the local entry */
  2977. if (local_type) {
  2978. slapi_entry_attr_find(local_entry, local_type, &local_attr);
  2979. if (local_attr) {
  2980. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2981. "windows_generate_update_mods: deleting %s attribute from local entry\n", local_type);
  2982. /* Delete this attr from the local entry */
  2983. slapi_mods_add_mod_values(smods, LDAP_MOD_DELETE, local_type, NULL);
  2984. *do_modify = 1;
  2985. }
  2986. }
  2987. entry_next_deleted_attribute(remote_entry, &del_attr);
  2988. slapi_ch_free_string(&local_type);
  2989. }
  2990. if (slapi_is_loglevel_set(SLAPI_LOG_REPL) && *do_modify)
  2991. {
  2992. slapi_mods_dump(smods,"windows sync");
  2993. }
  2994. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_generate_update_mods: %d\n", retval, 0, 0 );
  2995. return retval;
  2996. }
  2997. static int
  2998. windows_update_remote_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,Slapi_Entry *local_entry)
  2999. {
  3000. Slapi_Mods smods = {0};
  3001. int retval = 0;
  3002. int do_modify = 0;
  3003. slapi_mods_init (&smods, 0);
  3004. retval = windows_generate_update_mods(prp,local_entry,remote_entry,1,&smods,&do_modify);
  3005. /* Now perform the modify if we need to */
  3006. if (0 == retval && do_modify)
  3007. {
  3008. char dnbuf[BUFSIZ];
  3009. const char *dn = slapi_sdn_get_dn(slapi_entry_get_sdn_const(remote_entry));
  3010. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  3011. "windows_update_remote_entry: modifying entry %s\n", escape_string(dn, dnbuf));
  3012. 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);
  3013. } else
  3014. {
  3015. char dnbuf[BUFSIZ];
  3016. const char *dn = slapi_sdn_get_dn(slapi_entry_get_sdn_const(remote_entry));
  3017. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  3018. "no mods generated for remote entry: %s\n", escape_string(dn, dnbuf));
  3019. }
  3020. slapi_mods_done(&smods);
  3021. return retval;
  3022. }
  3023. static int
  3024. windows_update_local_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,Slapi_Entry *local_entry)
  3025. {
  3026. Slapi_Mods smods = {0};
  3027. int retval = 0;
  3028. Slapi_PBlock *pb = NULL;
  3029. int do_modify = 0;
  3030. slapi_mods_init (&smods, 0);
  3031. retval = windows_generate_update_mods(prp,remote_entry,local_entry,0,&smods,&do_modify);
  3032. /* Now perform the modify if we need to */
  3033. if (0 == retval && do_modify)
  3034. {
  3035. int rc = 0;
  3036. pb = slapi_pblock_new();
  3037. if (pb)
  3038. {
  3039. char dnbuf[BUFSIZ];
  3040. const char *dn = slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry));
  3041. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  3042. "modifying entry: %s\n", escape_string(dn, dnbuf));
  3043. slapi_modify_internal_set_pb (pb, slapi_entry_get_ndn(local_entry), slapi_mods_get_ldapmods_byref(&smods), NULL, NULL,
  3044. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  3045. slapi_modify_internal_pb (pb);
  3046. slapi_pblock_get (pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3047. if (rc)
  3048. {
  3049. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  3050. "windows_update_local_entry: failed to modify entry %s\n", escape_string(dn, dnbuf));
  3051. }
  3052. slapi_pblock_destroy(pb);
  3053. } else
  3054. {
  3055. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  3056. "failed to make pb in windows_update_local_entry\n");
  3057. }
  3058. } else
  3059. {
  3060. char dnbuf[BUFSIZ];
  3061. const char *dn = slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry));
  3062. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  3063. "no mods generated for local entry: %s\n", escape_string(dn, dnbuf));
  3064. }
  3065. slapi_mods_done(&smods);
  3066. return retval;
  3067. }
  3068. static int
  3069. windows_process_total_add(Private_Repl_Protocol *prp,Slapi_Entry *e, Slapi_DN* remote_dn, int missing_entry)
  3070. {
  3071. int retval = 0;
  3072. LDAPMod **entryattrs = NULL;
  3073. Slapi_Entry *mapped_entry = NULL;
  3074. char *password = NULL;
  3075. const Slapi_DN* local_dn = NULL;
  3076. /* First map the entry */
  3077. local_dn = slapi_entry_get_sdn_const(e);
  3078. if (missing_entry)
  3079. retval = windows_create_remote_entry(prp, e, remote_dn, &mapped_entry, &password);
  3080. /* Convert entry to mods */
  3081. if (0 == retval && mapped_entry)
  3082. {
  3083. (void)slapi_entry2mods (mapped_entry , NULL /* &entrydn : We don't need it */, &entryattrs);
  3084. slapi_entry_free(mapped_entry);
  3085. mapped_entry = NULL;
  3086. if (NULL == entryattrs)
  3087. {
  3088. 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));
  3089. retval = CONN_LOCAL_ERROR;
  3090. }
  3091. else
  3092. {
  3093. windows_log_add_entry_remote(local_dn, remote_dn);
  3094. retval = windows_conn_send_add(prp->conn, slapi_sdn_get_dn(remote_dn), entryattrs, NULL, NULL /* returned controls */);
  3095. /* It's possible that the entry already exists in AD, in which case we fall back to modify it */
  3096. if (retval)
  3097. {
  3098. 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));
  3099. }
  3100. ldap_mods_free(entryattrs, 1);
  3101. entryattrs = NULL;
  3102. }
  3103. } else
  3104. {
  3105. /* Entry already exists, need to mod it instead */
  3106. Slapi_Entry *remote_entry = NULL;
  3107. /* Get the remote entry */
  3108. retval = windows_get_remote_entry(prp, remote_dn,&remote_entry);
  3109. if (0 == retval && remote_entry)
  3110. {
  3111. retval = windows_update_remote_entry(prp,remote_entry,e);
  3112. /* Detect the case where the error is benign */
  3113. if (retval)
  3114. {
  3115. int operation = 0;
  3116. int error = 0;
  3117. conn_get_error(prp->conn, &operation, &error);
  3118. if (windows_ignore_error_and_keep_going(error))
  3119. {
  3120. retval = CONN_OPERATION_SUCCESS;
  3121. }
  3122. }
  3123. }
  3124. if (remote_entry)
  3125. {
  3126. slapi_entry_free(remote_entry);
  3127. }
  3128. }
  3129. return retval;
  3130. }
  3131. /* Entry point for the total protocol */
  3132. int windows_process_total_entry(Private_Repl_Protocol *prp,Slapi_Entry *e)
  3133. {
  3134. int retval = 0;
  3135. int is_ours = 0;
  3136. Slapi_DN *remote_dn = NULL;
  3137. int missing_entry = 0;
  3138. const Slapi_DN *local_dn = slapi_entry_get_sdn_const(e);
  3139. /* First check if the entry is for us */
  3140. is_ours = is_subject_of_agreement_local(e, prp->agmt);
  3141. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  3142. "%s: windows_process_total_entry: Looking dn=\"%s\" (%s)\n",
  3143. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)), is_ours ? "ours" : "not ours");
  3144. if (is_ours)
  3145. {
  3146. retval = map_entry_dn_outbound(e,&remote_dn,prp,&missing_entry,0 /* we don't want the GUID */);
  3147. if (retval || NULL == remote_dn)
  3148. {
  3149. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  3150. "%s: windows_replay_update: failed map dn for total update dn=\"%s\"\n",
  3151. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(local_dn));
  3152. goto error;
  3153. }
  3154. retval = windows_process_total_add(prp,e,remote_dn,missing_entry);
  3155. }
  3156. if (remote_dn)
  3157. {
  3158. slapi_sdn_free(&remote_dn);
  3159. }
  3160. error:
  3161. return retval;
  3162. }
  3163. static int
  3164. windows_search_local_entry_by_uniqueid(Private_Repl_Protocol *prp, const char *uniqueid, char ** attrs, Slapi_Entry **ret_entry, int tombstone, void * component_identity)
  3165. {
  3166. Slapi_Entry **entries = NULL;
  3167. Slapi_PBlock *int_search_pb = NULL;
  3168. int rc = 0;
  3169. char *filter_string = NULL;
  3170. const Slapi_DN *local_subtree = NULL;
  3171. *ret_entry = NULL;
  3172. local_subtree = windows_private_get_directory_subtree(prp->agmt);
  3173. /* Searching for tombstones can be expensive, so the caller needs to specify if
  3174. * we should search for a tombstone entry, or a normal entry. */
  3175. if (tombstone) {
  3176. filter_string = PR_smprintf("(&(objectclass=nsTombstone)(nsUniqueid=%s))", uniqueid);
  3177. } else {
  3178. filter_string = PR_smprintf("(&(|(objectclass=*)(objectclass=ldapsubentry))(nsUniqueid=%s))",uniqueid);
  3179. }
  3180. int_search_pb = slapi_pblock_new ();
  3181. slapi_search_internal_set_pb ( int_search_pb, slapi_sdn_get_dn(local_subtree), LDAP_SCOPE_SUBTREE, filter_string,
  3182. attrs ,
  3183. 0 /* attrsonly */, NULL /* controls */,
  3184. NULL /* uniqueid */,
  3185. component_identity, 0 /* actions */ );
  3186. slapi_search_internal_pb ( int_search_pb );
  3187. slapi_pblock_get( int_search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc );
  3188. if ( LDAP_SUCCESS == rc ) {
  3189. slapi_pblock_get( int_search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
  3190. if ( NULL != entries && NULL != entries[ 0 ]) {
  3191. Slapi_Entry *temp_entry = NULL;
  3192. temp_entry = entries[ 0 ];
  3193. *ret_entry = slapi_entry_dup(temp_entry);
  3194. } else {
  3195. /* No entry there */
  3196. rc = LDAP_NO_SUCH_OBJECT;
  3197. }
  3198. }
  3199. slapi_free_search_results_internal(int_search_pb);
  3200. slapi_pblock_destroy(int_search_pb);
  3201. int_search_pb = NULL;
  3202. if (filter_string)
  3203. {
  3204. PR_smprintf_free(filter_string);
  3205. }
  3206. return rc;
  3207. }
  3208. static int
  3209. windows_get_local_entry_by_uniqueid(Private_Repl_Protocol *prp,const char* uniqueid,Slapi_Entry **local_entry)
  3210. {
  3211. int retval = ENTRY_NOTFOUND;
  3212. Slapi_Entry *new_entry = NULL;
  3213. windows_search_local_entry_by_uniqueid( prp, uniqueid, NULL, &new_entry, 0, /* Don't search tombstones */
  3214. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  3215. if (new_entry)
  3216. {
  3217. *local_entry = new_entry;
  3218. retval = 0;
  3219. }
  3220. return retval;
  3221. }
  3222. static int
  3223. windows_get_local_tombstone_by_uniqueid(Private_Repl_Protocol *prp,const char* uniqueid,Slapi_Entry **local_entry)
  3224. {
  3225. int retval = ENTRY_NOTFOUND;
  3226. Slapi_Entry *new_entry = NULL;
  3227. windows_search_local_entry_by_uniqueid( prp, uniqueid, NULL, &new_entry, 1, /* Search for tombstones */
  3228. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  3229. if (new_entry)
  3230. {
  3231. *local_entry = new_entry;
  3232. retval = 0;
  3233. }
  3234. return retval;
  3235. }
  3236. static int
  3237. windows_get_local_entry(const Slapi_DN* local_dn,Slapi_Entry **local_entry)
  3238. {
  3239. int retval = ENTRY_NOTFOUND;
  3240. Slapi_Entry *new_entry = NULL;
  3241. slapi_search_internal_get_entry( (Slapi_DN*)local_dn, NULL, &new_entry,
  3242. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  3243. if (new_entry)
  3244. {
  3245. *local_entry = new_entry;
  3246. retval = 0;
  3247. }
  3248. return retval;
  3249. }
  3250. static int
  3251. windows_process_dirsync_entry(Private_Repl_Protocol *prp,Slapi_Entry *e, int is_total)
  3252. {
  3253. Slapi_DN* local_sdn = NULL;
  3254. int rc = 0;
  3255. /* deleted users are outside the 'correct container'.
  3256. They live in cn=deleted objects, windows_private_get_directory_subtree( prp->agmt) */
  3257. if (is_tombstone(prp, e))
  3258. {
  3259. rc = map_tombstone_dn_inbound(e, &local_sdn, prp->agmt);
  3260. if ((0 == rc) && local_sdn)
  3261. {
  3262. /* Go ahead and delte the local peer */
  3263. rc = windows_delete_local_entry(local_sdn);
  3264. slapi_sdn_free(&local_sdn);
  3265. } else
  3266. {
  3267. 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));
  3268. }
  3269. } else
  3270. {
  3271. /* Is this entry one we should be interested in ? */
  3272. if (is_subject_of_agreement_remote(e,prp->agmt))
  3273. {
  3274. /* First make its local DN */
  3275. rc = map_entry_dn_inbound(e, &local_sdn, prp->agmt);
  3276. if ((0 == rc) && local_sdn)
  3277. {
  3278. Slapi_Entry *local_entry = NULL;
  3279. /* Get the local entry if it exists */
  3280. rc = windows_get_local_entry(local_sdn,&local_entry);
  3281. if ((0 == rc) && local_entry)
  3282. {
  3283. /* Since the entry exists we should now make it match the entry we received from AD */
  3284. /* Actually we are better off simply fetching the entire entry from AD and using that
  3285. * because otherwise we don't get all the attributes we need to make sense of it such as
  3286. * objectclass */
  3287. Slapi_Entry *remote_entry = NULL;
  3288. windows_get_remote_entry(prp,slapi_entry_get_sdn_const(e),&remote_entry);
  3289. if (remote_entry)
  3290. {
  3291. /* We need to check for any deleted attrs from the dirsync entry
  3292. * and pass them into the newly fetched remote entry. */
  3293. Slapi_Attr *attr = NULL;
  3294. Slapi_Attr *rem_attr = NULL;
  3295. entry_first_deleted_attribute(e, &attr);
  3296. while (attr != NULL) {
  3297. /* We need to dup the attr and add it to the remote entry.
  3298. * rem_attr is now owned by remote_entry, so don't free it */
  3299. rem_attr = slapi_attr_dup(attr);
  3300. if (rem_attr) {
  3301. entry_add_deleted_attribute_wsi(remote_entry, rem_attr);
  3302. rem_attr = NULL;
  3303. }
  3304. entry_next_deleted_attribute(e, &attr);
  3305. }
  3306. rc = windows_update_local_entry(prp, remote_entry, local_entry);
  3307. slapi_entry_free(remote_entry);
  3308. remote_entry = NULL;
  3309. } else
  3310. {
  3311. 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));
  3312. }
  3313. slapi_entry_free(local_entry);
  3314. if (rc) {
  3315. /* Something bad happened */
  3316. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"%s: windows_process_dirsync_entry: failed to update inbound entry for %s.\n",agmt_get_long_name(prp->agmt),
  3317. slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)));
  3318. }
  3319. } else
  3320. {
  3321. /* If it doesn't exist, try to make it */
  3322. if (add_local_entry_allowed(prp,e))
  3323. {
  3324. windows_create_local_entry(prp,e,local_sdn);
  3325. } else
  3326. {
  3327. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"%s: windows_process_dirsync_entry: not allowed to add entry %s.\n",agmt_get_long_name(prp->agmt)
  3328. , slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)));
  3329. }
  3330. }
  3331. slapi_sdn_free(&local_sdn);
  3332. } else
  3333. {
  3334. /* We should have been able to map the DN, so this is an error */
  3335. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"%s: windows_process_dirsync_entry: failed to map inbound entry %s.\n",agmt_get_long_name(prp->agmt)
  3336. , slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)));
  3337. }
  3338. } /* subject of agreement */
  3339. } /* is tombstone */
  3340. return rc;
  3341. }
  3342. void
  3343. windows_dirsync_inc_run(Private_Repl_Protocol *prp)
  3344. {
  3345. int rc = 0;
  3346. int done = 0;
  3347. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_dirsync_inc_run\n", 0, 0, 0 );
  3348. while (!done) {
  3349. Slapi_Entry *e = NULL;
  3350. rc = send_dirsync_search(prp->conn);
  3351. if (rc != CONN_OPERATION_SUCCESS)
  3352. {
  3353. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  3354. "failed to send dirsync search request: %d\n", rc);
  3355. goto error;
  3356. }
  3357. while ( (e = windows_conn_get_search_result(prp->conn) ) != NULL)
  3358. {
  3359. rc = windows_process_dirsync_entry(prp,e,0);
  3360. if (e)
  3361. {
  3362. slapi_entry_free(e);
  3363. }
  3364. } /* While entry != NULL */
  3365. if (!windows_private_dirsync_has_more(prp->agmt))
  3366. {
  3367. done = 1;
  3368. }
  3369. } /* While !done */
  3370. error:
  3371. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_dirsync_inc_run\n", 0, 0, 0 );
  3372. }