windows_protocol_util.c 131 KB

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