windows_protocol_util.c 116 KB

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