1
0

windows_protocol_util.c 123 KB

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