windows_protocol_util.c 99 KB

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