windows_protocol_util.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204
  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_entry_add_valueset(new_entry,new_type,vs);
  1244. }
  1245. slapi_ch_free((void**)&new_type);
  1246. }
  1247. /* password mods are treated specially */
  1248. if (0 == slapi_attr_type_cmp(type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD, SLAPI_TYPE_CMP_SUBTYPE) )
  1249. {
  1250. const char *password_value = NULL;
  1251. Slapi_Value *value = NULL;
  1252. slapi_valueset_first_value(vs,&value);
  1253. password_value = slapi_value_get_string(value);
  1254. /* We need to check if the first character of password_value is an
  1255. * opening brace since strstr will simply return it's first argument
  1256. * if it is an empty string. */
  1257. if (password_value && (*password_value == '{')) {
  1258. if (strchr( password_value, '}' )) {
  1259. /* A storage scheme is present. Check if it's the
  1260. * clear storage scheme. */
  1261. if ((strlen(password_value) >= PASSWD_CLEAR_PREFIX_LEN + 1) &&
  1262. (strncasecmp(password_value, PASSWD_CLEAR_PREFIX, PASSWD_CLEAR_PREFIX_LEN) == 0)) {
  1263. /* This password is in clear text. Strip off the clear prefix
  1264. * and sync it. */
  1265. *password = slapi_ch_strdup(password_value + PASSWD_CLEAR_PREFIX_LEN);
  1266. } else {
  1267. /* This password is stored in a non-cleartext format.
  1268. * We can only sync cleartext passwords. */
  1269. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1270. "%s: windows_create_remote_entry: "
  1271. "Password is already hashed. Not syncing.\n",
  1272. agmt_get_long_name(prp->agmt));
  1273. }
  1274. } else {
  1275. /* This password doesn't have a storage prefix but
  1276. * just happens to start with the '{' character. We'll
  1277. * assume that it's just a cleartext password without
  1278. * the proper storage prefix. */
  1279. *password = slapi_ch_strdup(password_value);
  1280. }
  1281. } else {
  1282. /* This password has no storage prefix, or the password is empty */
  1283. *password = slapi_ch_strdup(password_value);
  1284. }
  1285. }
  1286. }
  1287. if (vs)
  1288. {
  1289. slapi_valueset_free(vs);
  1290. vs = NULL;
  1291. }
  1292. }
  1293. /* NT4 must have the groupType attribute set for groups. If it is not present, we will
  1294. * add it here with a value of 2 (global group).
  1295. */
  1296. if (is_nt4 && is_group)
  1297. {
  1298. Slapi_Attr *ap = NULL;
  1299. if(slapi_entry_attr_find(new_entry, "groupType", &ap))
  1300. {
  1301. /* groupType attribute wasn't found, so we'll add it */
  1302. slapi_entry_attr_set_int(new_entry, "groupType", 2 /* global group */);
  1303. }
  1304. }
  1305. if (remote_entry)
  1306. {
  1307. *remote_entry = new_entry;
  1308. }
  1309. error:
  1310. if (username)
  1311. {
  1312. slapi_ch_free((void**)&username);
  1313. }
  1314. if (new_entry)
  1315. {
  1316. windows_dump_entry("Created new remote entry:\n",new_entry);
  1317. }
  1318. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_create_remote_entry: %d\n", retval, 0, 0 );
  1319. return retval;
  1320. }
  1321. /* the entry has already been translated, so be sure to search for ntuserid
  1322. and not samaccountname or anything else. */
  1323. static Slapi_Entry*
  1324. windows_entry_already_exists(Slapi_Entry *e){
  1325. int rc = 0;
  1326. Slapi_DN *sdn = NULL;
  1327. Slapi_Entry *entry = NULL;
  1328. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_entry_already_exists\n", 0, 0, 0 );
  1329. sdn = slapi_entry_get_sdn(e);
  1330. rc = slapi_search_internal_get_entry( sdn, NULL, &entry, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  1331. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_entry_already_exists\n", 0, 0, 0 );
  1332. if (rc == LDAP_SUCCESS)
  1333. {
  1334. return entry;
  1335. }
  1336. else
  1337. {
  1338. return NULL;
  1339. }
  1340. }
  1341. static int
  1342. windows_delete_local_entry(Slapi_DN *sdn){
  1343. Slapi_PBlock *pb = NULL;
  1344. int return_value = 0;
  1345. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_delete_local_entry\n", 0, 0, 0 );
  1346. pb = slapi_pblock_new();
  1347. slapi_delete_internal_set_pb(pb, slapi_sdn_get_dn(sdn), NULL, NULL, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  1348. slapi_delete_internal_pb(pb);
  1349. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  1350. slapi_pblock_destroy(pb);
  1351. if (return_value) {
  1352. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1353. "delete operation on local entry %s returned: %d\n", slapi_sdn_get_dn(sdn), return_value);
  1354. }
  1355. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_delete_local_entry: %d\n", return_value, 0, 0 );
  1356. return return_value;
  1357. }
  1358. static void
  1359. windows_map_mods_for_replay(Private_Repl_Protocol *prp,LDAPMod **original_mods, LDAPMod ***returned_mods, int is_user, char** password)
  1360. {
  1361. Slapi_Mods smods = {0};
  1362. Slapi_Mods mapped_smods = {0};
  1363. LDAPMod *mod = NULL;
  1364. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  1365. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_map_mods_for_replay\n", 0, 0, 0 );
  1366. /* Iterate through the original mods, looking each attribute type up in the maps for either user or group */
  1367. slapi_mods_init_byref(&smods, original_mods);
  1368. slapi_mods_init(&mapped_smods,10);
  1369. mod = slapi_mods_get_first_mod(&smods);
  1370. while(mod)
  1371. {
  1372. char *attr_type = mod->mod_type;
  1373. int mapdn = 0;
  1374. /* Check to see if this attribute is passed through */
  1375. if (is_straight_mapped_attr(attr_type,is_user,is_nt4)) {
  1376. /* The initials attribute is a special case. AD has a constraint
  1377. * that limits the value length. If we're sending a change to
  1378. * the initials attribute to AD, we trim if neccessary.
  1379. */
  1380. if (0 == slapi_attr_type_cmp(attr_type, "initials", SLAPI_TYPE_CMP_SUBTYPE)) {
  1381. int i;
  1382. for (i = 0; mod->mod_bvalues[i] != NULL; i++) {
  1383. /* If > AD_INITIALS_LENGTH, trim the value */
  1384. if (mod->mod_bvalues[i]->bv_len > AD_INITIALS_LENGTH) {
  1385. mod->mod_bvalues[i]->bv_val[AD_INITIALS_LENGTH] = '\0';
  1386. mod->mod_bvalues[i]->bv_len = AD_INITIALS_LENGTH;
  1387. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1388. "%s: windows_map_mods_for_replay: "
  1389. "Trimming initials attribute to %d characters.\n",
  1390. agmt_get_long_name(prp->agmt), AD_INITIALS_LENGTH);
  1391. }
  1392. }
  1393. }
  1394. /* copy over the mod */
  1395. slapi_mods_add_modbvps(&mapped_smods,mod->mod_op,attr_type,mod->mod_bvalues);
  1396. } else
  1397. {
  1398. char *mapped_type = NULL;
  1399. /* Check if this mod has its attribute type mapped */
  1400. windows_map_attr_name(attr_type,1,is_user,0,&mapped_type, &mapdn);
  1401. if (mapped_type)
  1402. {
  1403. /* If so copy over the mod with new type name */
  1404. if (mapdn)
  1405. {
  1406. Slapi_ValueSet *mapped_values = NULL;
  1407. Slapi_ValueSet *vs = NULL;
  1408. Slapi_Mod smod;
  1409. vs = slapi_valueset_new();
  1410. slapi_mod_init_byref(&smod,mod);
  1411. slapi_valueset_set_from_smod(vs, &smod);
  1412. map_dn_values(prp,vs,&mapped_values, 1 /* to windows */,0);
  1413. if (mapped_values)
  1414. {
  1415. slapi_mods_add_mod_values(&mapped_smods,mod->mod_op,mapped_type,valueset_get_valuearray(mapped_values));
  1416. slapi_valueset_free(mapped_values);
  1417. mapped_values = NULL;
  1418. } else
  1419. {
  1420. /* this might be a del: mod, in which case there are no values */
  1421. if (mod->mod_op & LDAP_MOD_DELETE)
  1422. {
  1423. slapi_mods_add_mod_values(&mapped_smods, LDAP_MOD_DELETE, mapped_type, NULL);
  1424. }
  1425. }
  1426. slapi_mod_done(&smod);
  1427. slapi_valueset_free(vs);
  1428. } else
  1429. {
  1430. slapi_mods_add_modbvps(&mapped_smods,mod->mod_op,mapped_type,mod->mod_bvalues);
  1431. }
  1432. slapi_ch_free((void**)&mapped_type);
  1433. } else
  1434. {
  1435. /* password mods are treated specially */
  1436. if (0 == slapi_attr_type_cmp(attr_type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD, SLAPI_TYPE_CMP_SUBTYPE) )
  1437. {
  1438. char *password_value = NULL;
  1439. password_value = mod->mod_bvalues[0]->bv_val;
  1440. /* We need to check if the first character of password_value is an
  1441. * opening brace since strstr will simply return it's first argument
  1442. * if it is an empty string. */
  1443. if (password_value && (*password_value == '{')) {
  1444. if (strchr( password_value, '}' )) {
  1445. /* A storage scheme is present. Check if it's the
  1446. * clear storage scheme. */
  1447. if ((strlen(password_value) >= PASSWD_CLEAR_PREFIX_LEN + 1) &&
  1448. (strncasecmp(password_value, PASSWD_CLEAR_PREFIX, PASSWD_CLEAR_PREFIX_LEN) == 0)) {
  1449. /* This password is in clear text. Strip off the clear prefix
  1450. * and sync it. */
  1451. *password = slapi_ch_strdup(password_value + PASSWD_CLEAR_PREFIX_LEN);
  1452. } else {
  1453. /* This password is stored in a non-cleartext format.
  1454. * We can only sync cleartext passwords. */
  1455. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1456. "%s: windows_create_remote_entry: "
  1457. "Password is already hashed. Not syncing.\n",
  1458. agmt_get_long_name(prp->agmt));
  1459. }
  1460. } else {
  1461. /* This password doesn't have a storage prefix but
  1462. * just happens to start with the '{' character. We'll
  1463. * assume that it's just a cleartext password without
  1464. * the proper storage prefix. */
  1465. *password = slapi_ch_strdup(password_value);
  1466. }
  1467. } else {
  1468. /* This password has no storage prefix, or the password is empty */
  1469. *password = slapi_ch_strdup(password_value);
  1470. }
  1471. }
  1472. }
  1473. }
  1474. /* Otherwise we do not copy this mod at all */
  1475. mod = slapi_mods_get_next_mod(&smods);
  1476. }
  1477. slapi_mods_done (&smods);
  1478. /* Extract the mods for the caller */
  1479. *returned_mods = slapi_mods_get_ldapmods_passout(&mapped_smods);
  1480. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_map_mods_for_replay\n", 0, 0, 0 );
  1481. }
  1482. /* Returns non-zero if the attribute value sets are identical. If you want to
  1483. * compare the entire attribute value, set n to 0. You can compare only the
  1484. * first n characters of the values by passing in the legth as n. */
  1485. static int
  1486. attr_compare_equal(Slapi_Attr *a, Slapi_Attr *b, int n)
  1487. {
  1488. /* For now only handle single values */
  1489. Slapi_Value *va = NULL;
  1490. Slapi_Value *vb = NULL;
  1491. int num_a = 0;
  1492. int num_b = 0;
  1493. int match = 1;
  1494. slapi_attr_get_numvalues(a,&num_a);
  1495. slapi_attr_get_numvalues(b,&num_b);
  1496. if (num_a == num_b) {
  1497. slapi_attr_first_value(a, &va);
  1498. slapi_attr_first_value(b, &vb);
  1499. /* If either val is less than n, then check if the length, then values are
  1500. * equal. If both are n or greater, then only compare the first n chars.
  1501. * If n is 0, then just compare the entire attribute. */
  1502. if ((va->bv.bv_len < n) || (vb->bv.bv_len < n) || (n == 0)) {
  1503. if (va->bv.bv_len == vb->bv.bv_len) {
  1504. if (0 != memcmp(va->bv.bv_val, vb->bv.bv_val, va->bv.bv_len)) {
  1505. match = 0;
  1506. }
  1507. } else {
  1508. match = 0;
  1509. }
  1510. } else if (0 != memcmp(va->bv.bv_val, vb->bv.bv_val, n)) {
  1511. match = 0;
  1512. }
  1513. } else {
  1514. match = 0;
  1515. }
  1516. return match;
  1517. }
  1518. /* Helper functions for dirsync result processing */
  1519. /* Is this entry a tombstone ? */
  1520. static int
  1521. is_tombstone(Slapi_Entry *e)
  1522. {
  1523. int retval = 0;
  1524. char *string_deleted = slapi_ch_strdup("(isdeleted=*)");
  1525. /* DBDB: we should allocate these filters once and keep them around for better performance */
  1526. Slapi_Filter *filter_deleted = slapi_str2filter( string_deleted );
  1527. slapi_ch_free_string(&string_deleted);
  1528. /* DBDB: this should be one filter, the code originally tested separately and hasn't been fixed yet */
  1529. if ( (slapi_filter_test_simple( e, filter_deleted ) == 0) )
  1530. {
  1531. retval = 1;
  1532. }
  1533. slapi_filter_free(filter_deleted,1);
  1534. filter_deleted = NULL;
  1535. return retval;
  1536. }
  1537. #define ENTRY_NOTFOUND -1
  1538. #define ENTRY_NOT_UNIQUE -2
  1539. /* Search for an entry in AD */
  1540. static int
  1541. find_entry_by_attr_value_remote(const char *attribute, const char *value, Slapi_Entry **e, Private_Repl_Protocol *prp)
  1542. {
  1543. int retval = 0;
  1544. ConnResult cres = 0;
  1545. char *filter = NULL;
  1546. const char *searchbase = NULL;
  1547. Slapi_Entry *found_entry = NULL;
  1548. filter = PR_smprintf("(%s=%s)",attribute,value);
  1549. searchbase = slapi_sdn_get_dn(windows_private_get_windows_subtree(prp->agmt));
  1550. cres = windows_search_entry(prp->conn, (char*)searchbase, filter, &found_entry);
  1551. if (cres)
  1552. {
  1553. retval = -1;
  1554. } else
  1555. {
  1556. if (found_entry)
  1557. {
  1558. *e = found_entry;
  1559. }
  1560. }
  1561. if (filter)
  1562. {
  1563. PR_smprintf_free(filter);
  1564. filter = NULL;
  1565. }
  1566. return retval;
  1567. }
  1568. /* Search for an entry in AD by DN */
  1569. static int
  1570. windows_get_remote_entry (Private_Repl_Protocol *prp, const Slapi_DN* remote_dn,Slapi_Entry **remote_entry)
  1571. {
  1572. int retval = 0;
  1573. ConnResult cres = 0;
  1574. char *filter = "(objectclass=*)";
  1575. const char *searchbase = NULL;
  1576. Slapi_Entry *found_entry = NULL;
  1577. searchbase = slapi_sdn_get_dn(remote_dn);
  1578. cres = windows_search_entry(prp->conn, (char*)searchbase, filter, &found_entry);
  1579. if (cres)
  1580. {
  1581. retval = -1;
  1582. } else
  1583. {
  1584. if (found_entry)
  1585. {
  1586. *remote_entry = found_entry;
  1587. }
  1588. }
  1589. return retval;
  1590. }
  1591. static int
  1592. find_entry_by_attr_value(const char *attribute, const char *value, Slapi_Entry **e, const Repl_Agmt *ra)
  1593. {
  1594. Slapi_PBlock *pb = slapi_pblock_new();
  1595. Slapi_Entry **entries = NULL, **ep = NULL;
  1596. Slapi_Entry *entry_found = NULL;
  1597. char *query = NULL;
  1598. int found_or_not = ENTRY_NOTFOUND;
  1599. int rval = 0;
  1600. const char *subtree_dn = NULL;
  1601. int not_unique = 0;
  1602. if (pb == NULL)
  1603. goto done;
  1604. query = slapi_ch_smprintf("(%s=%s)", attribute, value);
  1605. if (query == NULL)
  1606. goto done;
  1607. subtree_dn = slapi_sdn_get_dn(windows_private_get_directory_subtree(ra));
  1608. slapi_search_internal_set_pb(pb, subtree_dn,
  1609. LDAP_SCOPE_SUBTREE, query, NULL, 0, NULL, NULL,
  1610. (void *)plugin_get_default_component_id(), 0);
  1611. slapi_search_internal_pb(pb);
  1612. slapi_ch_free((void **)&query);
  1613. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rval);
  1614. if (rval != LDAP_SUCCESS)
  1615. {
  1616. goto done;
  1617. }
  1618. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1619. if ((entries == NULL) || (entries[0] == NULL))
  1620. {
  1621. goto done;
  1622. }
  1623. entry_found = entries[0];
  1624. for (ep = entries; *ep; ep++) {
  1625. if (not_unique)
  1626. {
  1627. found_or_not = ENTRY_NOT_UNIQUE;
  1628. }
  1629. not_unique = 1;
  1630. }
  1631. done:
  1632. if (entry_found && (found_or_not != ENTRY_NOT_UNIQUE))
  1633. {
  1634. found_or_not = 0;
  1635. *e = slapi_entry_dup(entry_found);
  1636. }
  1637. if (pb)
  1638. {
  1639. slapi_free_search_results_internal(pb);
  1640. slapi_pblock_destroy(pb);
  1641. }
  1642. return found_or_not;
  1643. }
  1644. static int
  1645. find_entry_by_username(const char *username, Slapi_Entry **e, const Repl_Agmt *ra)
  1646. {
  1647. return find_entry_by_attr_value("ntUserDomainId",username,e,ra);
  1648. }
  1649. /* Find an entry in the local server given its GUID, or return ENTRY_NOTFOUND */
  1650. static int
  1651. find_entry_by_guid(const char *guid, Slapi_Entry **e, const Repl_Agmt *ra)
  1652. {
  1653. return find_entry_by_attr_value("ntUniqueId",guid,e,ra);
  1654. }
  1655. /* Remove dashes from a GUID string */
  1656. static void
  1657. dedash(char *str)
  1658. {
  1659. char *p = str;
  1660. char c = '\0';
  1661. while (c = *p)
  1662. {
  1663. if ('-' == c)
  1664. {
  1665. /* Move on down please */
  1666. char *q = p;
  1667. char *r = q + 1;
  1668. while (*r)
  1669. {
  1670. *q++ = *r++;
  1671. }
  1672. *q = '\0';
  1673. }
  1674. p++;
  1675. }
  1676. }
  1677. /* For reasons not clear, the GUID returned in the tombstone DN is all
  1678. * messed up, like the guy in the movie 'the fly' after he want in the tranporter device */
  1679. static void
  1680. decrypt(char *guid)
  1681. {
  1682. static int decrypt_offsets[] = {6,7,4,5,2,3,0,1,10,11,8,9,14,15,12,13,16,17,18,19,
  1683. 20,21,22,23,24,25,26,27,28,29,30,31};
  1684. char *p = guid;
  1685. int i = 0;
  1686. char *cpy = slapi_ch_strdup(guid);
  1687. while (*p && i < (sizeof(decrypt_offsets)/sizeof(int)))
  1688. {
  1689. *p = cpy[decrypt_offsets[i]];
  1690. p++;
  1691. i++;
  1692. }
  1693. slapi_ch_free((void**)&cpy);
  1694. }
  1695. static char*
  1696. extract_guid_from_tombstone_dn(const char *dn)
  1697. {
  1698. char *guid = NULL;
  1699. char *colon_offset = NULL;
  1700. char *comma_offset = NULL;
  1701. /* example DN of tombstone:
  1702. "CN=WDel Userdb1\\\nDEL:551706bc-ecf2-4b38-9284-9a8554171d69,CN=Deleted Objects,DC=magpie,DC=com" */
  1703. /* First find the 'DEL:' */
  1704. colon_offset = strchr(dn,':');
  1705. /* Then scan forward to the next ',' */
  1706. comma_offset = strchr(dn,',');
  1707. /* The characters inbetween are the GUID, copy them to a new string and return to the caller */
  1708. if (comma_offset && colon_offset && comma_offset > colon_offset) {
  1709. guid = slapi_ch_malloc(comma_offset - colon_offset);
  1710. strncpy(guid,colon_offset+1,(comma_offset-colon_offset)-1);
  1711. guid[comma_offset-colon_offset-1] = '\0';
  1712. /* Finally remove the dashes since we don't store them on our side */
  1713. dedash(guid);
  1714. decrypt(guid);
  1715. }
  1716. return guid;
  1717. }
  1718. static char *
  1719. convert_to_hex(Slapi_Value *val)
  1720. {
  1721. int offset = 0;
  1722. const struct berval *bvp = NULL;
  1723. int length = 0;
  1724. char *result = NULL;
  1725. bvp = slapi_value_get_berval(val);
  1726. if (bvp)
  1727. {
  1728. char *new_buffer = NULL;
  1729. length = bvp->bv_len;
  1730. for (offset = 0; offset < length; offset++)
  1731. {
  1732. unsigned char byte = ((unsigned char*)(bvp->bv_val))[offset];
  1733. new_buffer = PR_sprintf_append(new_buffer, "%02x", byte );
  1734. }
  1735. if (new_buffer)
  1736. {
  1737. result = new_buffer;
  1738. }
  1739. }
  1740. return result;
  1741. }
  1742. static char*
  1743. extract_guid_from_entry(Slapi_Entry *e, int is_nt4)
  1744. {
  1745. char *guid = NULL;
  1746. Slapi_Value *val = NULL;
  1747. Slapi_Attr *attr = NULL;
  1748. slapi_entry_attr_find(e, "objectGUID", &attr);
  1749. if (attr)
  1750. {
  1751. slapi_attr_first_value(attr, &val);
  1752. if (val) {
  1753. if (is_nt4)
  1754. {
  1755. guid = slapi_ch_strdup(slapi_value_get_string(val));
  1756. } else
  1757. {
  1758. guid = convert_to_hex(val);
  1759. }
  1760. }
  1761. }
  1762. return guid;
  1763. }
  1764. static void
  1765. extract_guid_from_entry_bv(Slapi_Entry *e, const struct berval **bv)
  1766. {
  1767. Slapi_Value *val = NULL;
  1768. Slapi_Attr *attr = NULL;
  1769. slapi_entry_attr_find(e, "objectGUID", &attr);
  1770. if (attr)
  1771. {
  1772. slapi_attr_first_value(attr, &val);
  1773. if (val) {
  1774. *bv = slapi_value_get_berval(val);
  1775. }
  1776. }
  1777. }
  1778. static char*
  1779. extract_username_from_entry(Slapi_Entry *e)
  1780. {
  1781. char *uid = NULL;
  1782. uid = slapi_entry_attr_get_charptr(e,"samAccountName");
  1783. return uid;
  1784. }
  1785. static char*
  1786. extract_ntuserdomainid_from_entry(Slapi_Entry *e)
  1787. {
  1788. char *uid = NULL;
  1789. uid = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  1790. return uid;
  1791. }
  1792. static Slapi_DN *make_dn_from_guid(char *guid, int is_nt4, const char* suffix)
  1793. {
  1794. Slapi_DN *new_dn = NULL;
  1795. char *dn_string = NULL;
  1796. if (guid)
  1797. {
  1798. new_dn = slapi_sdn_new();
  1799. if (is_nt4)
  1800. {
  1801. dn_string = PR_smprintf("GUID=%s,%s",guid,suffix);
  1802. } else
  1803. {
  1804. dn_string = PR_smprintf("<GUID=%s>",guid);
  1805. }
  1806. slapi_sdn_init_dn_byval(new_dn,dn_string);
  1807. PR_smprintf_free(dn_string);
  1808. }
  1809. /* dn string is now inside the Slapi_DN, and will be freed by its owner */
  1810. return new_dn;
  1811. }
  1812. static char*
  1813. extract_container(const Slapi_DN *entry_dn, const Slapi_DN *suffix_dn)
  1814. {
  1815. char *result = NULL;
  1816. /* First do a scope test to make sure that we weren't passed bogus arguments */
  1817. if (slapi_sdn_scope_test(entry_dn,suffix_dn,LDAP_SCOPE_SUBTREE))
  1818. {
  1819. Slapi_DN parent;
  1820. slapi_sdn_init(&parent);
  1821. /* Find the portion of the entry_dn between the RDN and the suffix */
  1822. /* Start with the parent of the entry DN */
  1823. slapi_sdn_get_parent(entry_dn, &parent);
  1824. /* Iterate finding the parent again until we have the suffix */
  1825. while (0 != slapi_sdn_compare(&parent,suffix_dn))
  1826. {
  1827. Slapi_DN child;
  1828. Slapi_RDN *rdn = slapi_rdn_new();
  1829. char *rdn_type = NULL;
  1830. char *rdn_str = NULL;
  1831. /* Append the current RDN to the new container string */
  1832. slapi_sdn_get_rdn(&parent,rdn);
  1833. slapi_rdn_get_first(rdn, &rdn_type, &rdn_str);
  1834. if (rdn_str)
  1835. {
  1836. result = PR_sprintf_append(result, "%s=%s,", rdn_type,rdn_str );
  1837. }
  1838. /* Don't free this until _after_ we've used the rdn_str */
  1839. slapi_rdn_free(&rdn);
  1840. /* Move to the next successive parent */
  1841. slapi_sdn_init(&child);
  1842. slapi_sdn_copy(&parent,&child);
  1843. slapi_sdn_done(&parent);
  1844. slapi_sdn_get_parent(&child, &parent);
  1845. slapi_sdn_done(&child);
  1846. }
  1847. slapi_sdn_done(&parent);
  1848. }
  1849. /* Always return something */
  1850. if (NULL == result)
  1851. {
  1852. result = slapi_ch_strdup("");
  1853. }
  1854. return result;
  1855. }
  1856. /* Given a non-tombstone entry, return the DN of its peer in AD (whether present or not) */
  1857. static int
  1858. map_entry_dn_outbound(Slapi_Entry *e, Slapi_DN **dn, Private_Repl_Protocol *prp, int *missing_entry, int guid_form)
  1859. {
  1860. int retval = 0;
  1861. char *guid = NULL;
  1862. Slapi_DN *new_dn = NULL;
  1863. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  1864. const char *suffix = slapi_sdn_get_dn(windows_private_get_windows_subtree(prp->agmt));
  1865. /* To find the DN of the peer entry we first look for an ntUniqueId attribute
  1866. * on the local entry. If that's present, we generate a GUID-form DN.
  1867. * If there's no GUID, then we look for an ntUserDomainId attribute
  1868. * on the entry. If that's present we attempt to search for an entry with
  1869. * that samaccountName attribute value in AD. If we don't find any matching
  1870. * entry we generate a new DN using the entry's cn. If later, we find that
  1871. * this entry already exists, we handle that problem at the time. We don't
  1872. * check here. Note: for NT4 we always use ntUserDomainId for the samaccountname rdn, never cn.
  1873. */
  1874. *missing_entry = 0;
  1875. guid = slapi_entry_attr_get_charptr(e,"ntUniqueId");
  1876. if (guid && guid_form)
  1877. {
  1878. new_dn = make_dn_from_guid(guid, is_nt4, suffix);
  1879. slapi_ch_free((void**)&guid);
  1880. } else
  1881. {
  1882. /* No GUID found, try ntUserDomainId */
  1883. Slapi_Entry *remote_entry = NULL;
  1884. char *username = slapi_entry_attr_get_charptr(e,"ntUserDomainId");
  1885. if (username) {
  1886. retval = find_entry_by_attr_value_remote("samAccountName",username,&remote_entry,prp);
  1887. if (0 == retval && remote_entry)
  1888. {
  1889. /* Get the entry's DN */
  1890. new_dn = slapi_sdn_new();
  1891. slapi_sdn_copy(slapi_entry_get_sdn_const(remote_entry), new_dn);
  1892. } else {
  1893. if (0 == retval)
  1894. {
  1895. char *new_dn_string = NULL;
  1896. char *cn_string = NULL;
  1897. *missing_entry = 1;
  1898. /* This means that we failed to find a peer entry */
  1899. /* In that case we need to generate the DN that we want to use */
  1900. /* Generated DN's take the form :
  1901. cn=<cn from local entry>, ... in the case that the local entry has a cn, OR
  1902. cn=<ntuserdomainid attribute value>, ... in the case that the local entry doesn't have a CN
  1903. */
  1904. if (is_nt4)
  1905. {
  1906. cn_string = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  1907. } else
  1908. {
  1909. cn_string = slapi_entry_attr_get_charptr(e,"cn");
  1910. if (!cn_string)
  1911. {
  1912. cn_string = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  1913. }
  1914. }
  1915. if (cn_string)
  1916. {
  1917. char *rdnstr = NULL;
  1918. char *container_str = NULL;
  1919. container_str = extract_container(slapi_entry_get_sdn_const(e), windows_private_get_directory_subtree(prp->agmt));
  1920. rdnstr = is_nt4 ? "samaccountname=%s,%s%s" : "cn=%s,%s%s";
  1921. new_dn_string = PR_smprintf(rdnstr,cn_string,container_str,suffix);
  1922. if (new_dn_string)
  1923. {
  1924. new_dn = slapi_sdn_new_dn_byval(new_dn_string);
  1925. PR_smprintf_free(new_dn_string);
  1926. }
  1927. slapi_ch_free((void**)&cn_string);
  1928. slapi_ch_free((void**)&container_str);
  1929. }
  1930. } else
  1931. {
  1932. /* This means that we failed to talk to the AD for some reason, the operation should be re-tried */
  1933. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1934. "%s: map_entry_dn_outbound: failed to fetch entry from AD: dn=\"%s\", err=%d\n",
  1935. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)), retval);
  1936. retval = -1;
  1937. }
  1938. }
  1939. slapi_ch_free((void**)&username);
  1940. }
  1941. if (remote_entry)
  1942. {
  1943. slapi_entry_free(remote_entry);
  1944. }
  1945. }
  1946. if (new_dn)
  1947. {
  1948. *dn = new_dn;
  1949. }
  1950. return retval;
  1951. }
  1952. /* Given a tombstone entry, return the DN of its peer in this server (if present) */
  1953. static int
  1954. map_tombstone_dn_inbound(Slapi_Entry *e, Slapi_DN **dn, const Repl_Agmt *ra)
  1955. {
  1956. int retval = 0;
  1957. Slapi_DN *new_dn = NULL;
  1958. char *guid = NULL;
  1959. const char *dn_string = NULL;
  1960. Slapi_Entry *matching_entry = NULL;
  1961. /* To map a tombstone's DN we need to first extract the entry's objectGUID from the DN
  1962. * CN=vpdxtAD_07\
  1963. DEL:d4ca4e16-e35b-400d-834a-f02db600f3fa,CN=Deleted Objects,DC=magpie,DC=com
  1964. */
  1965. *dn = NULL;
  1966. dn_string = slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)); /* This is a pointer from inside the sdn, no need to free */
  1967. guid = extract_guid_from_tombstone_dn(dn_string);
  1968. if (guid)
  1969. {
  1970. retval = find_entry_by_guid(guid,&matching_entry,ra);
  1971. if (retval)
  1972. {
  1973. if (ENTRY_NOTFOUND == retval)
  1974. {
  1975. } else
  1976. {
  1977. if (ENTRY_NOT_UNIQUE == retval)
  1978. {
  1979. } else
  1980. {
  1981. /* A real error */
  1982. }
  1983. }
  1984. } else
  1985. {
  1986. /* We found the matching entry : get its DN */
  1987. new_dn = slapi_sdn_dup(slapi_entry_get_sdn_const(matching_entry));
  1988. }
  1989. }
  1990. if (new_dn)
  1991. {
  1992. *dn = new_dn;
  1993. }
  1994. if (guid)
  1995. {
  1996. slapi_ch_free((void**)&guid);
  1997. }
  1998. if (matching_entry)
  1999. {
  2000. slapi_entry_free(matching_entry);
  2001. }
  2002. return retval;
  2003. }
  2004. /* Given a non-tombstone entry, return the DN of its peer in this server (whether present or not) */
  2005. static int
  2006. map_entry_dn_inbound(Slapi_Entry *e, Slapi_DN **dn, const Repl_Agmt *ra)
  2007. {
  2008. int retval = 0;
  2009. Slapi_DN *new_dn = NULL;
  2010. char *guid = NULL;
  2011. char *username = NULL;
  2012. Slapi_Entry *matching_entry = NULL;
  2013. int is_user = 0;
  2014. int is_group = 0;
  2015. int is_nt4 = windows_private_get_isnt4(ra);
  2016. /* To map a non-tombstone's DN we need to first try to look it up by GUID.
  2017. * If we do not find it, then we need to generate the DN that it would have if added as a new entry.
  2018. */
  2019. *dn = NULL;
  2020. windows_is_remote_entry_user_or_group(e,&is_user,&is_group);
  2021. guid = extract_guid_from_entry(e, is_nt4);
  2022. if (guid)
  2023. {
  2024. retval = find_entry_by_guid(guid,&matching_entry,ra);
  2025. if (retval)
  2026. {
  2027. if (ENTRY_NOTFOUND == retval)
  2028. {
  2029. } else
  2030. {
  2031. if (ENTRY_NOT_UNIQUE == retval)
  2032. {
  2033. } else
  2034. {
  2035. /* A real error */
  2036. goto error;
  2037. }
  2038. }
  2039. } else
  2040. {
  2041. /* We found the matching entry : get its DN */
  2042. new_dn = slapi_sdn_dup(slapi_entry_get_sdn_const(matching_entry));
  2043. }
  2044. }
  2045. /* If we failed to lookup by guid, try samaccountname */
  2046. if (NULL == new_dn)
  2047. {
  2048. username = extract_username_from_entry(e);
  2049. if (username) {
  2050. retval = find_entry_by_username(username,&matching_entry,ra);
  2051. if (retval)
  2052. {
  2053. if (ENTRY_NOTFOUND == retval)
  2054. {
  2055. } else
  2056. {
  2057. if (ENTRY_NOT_UNIQUE == retval)
  2058. {
  2059. } else
  2060. {
  2061. /* A real error */
  2062. goto error;
  2063. }
  2064. }
  2065. } else
  2066. {
  2067. /* We found the matching entry : get its DN */
  2068. new_dn = slapi_sdn_dup(slapi_entry_get_sdn_const(matching_entry));
  2069. }
  2070. }
  2071. }
  2072. /* If we couldn't find a matching entry by either method, then we need to invent a new DN */
  2073. if (NULL == new_dn)
  2074. {
  2075. /* The new DN has the form: uid=<samaccountname>,<sync'ed subtree> */
  2076. /* If an entry with this DN already exists, we fail and return no DN
  2077. * this is because we don't want to second-guess what the admin wants here:
  2078. * they may want to associate this existing entry with the peer AD entry,
  2079. * but if they intend that we say they must add the ntDomainUserId attribute to
  2080. * that entry.
  2081. */
  2082. char *new_dn_string = NULL;
  2083. if (username)
  2084. {
  2085. const char *suffix = slapi_sdn_get_dn(windows_private_get_directory_subtree(ra));
  2086. char *container_str = NULL;
  2087. container_str = extract_container(slapi_entry_get_sdn_const(e), windows_private_get_windows_subtree(ra));
  2088. /* Local DNs for users and groups are different */
  2089. if (is_user)
  2090. {
  2091. new_dn_string = PR_smprintf("uid=%s,%s%s",username,container_str,suffix);
  2092. } else
  2093. {
  2094. new_dn_string = PR_smprintf("cn=%s,%s%s",username,container_str,suffix);
  2095. }
  2096. new_dn = slapi_sdn_new_dn_byval(new_dn_string);
  2097. PR_smprintf_free(new_dn_string);
  2098. slapi_ch_free((void**)&container_str);
  2099. /* Clear any earlier error */
  2100. retval = 0;
  2101. } else
  2102. {
  2103. /* Error, no username */
  2104. }
  2105. }
  2106. if (new_dn)
  2107. {
  2108. *dn = new_dn;
  2109. }
  2110. error:
  2111. if (guid)
  2112. {
  2113. PR_smprintf_free(guid);
  2114. }
  2115. if (matching_entry)
  2116. {
  2117. slapi_entry_free(matching_entry);
  2118. }
  2119. if (username)
  2120. {
  2121. slapi_ch_free((void **) &username);
  2122. }
  2123. return retval;
  2124. }
  2125. /* 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
  2126. * and does it have the right attribute values for sync ?)
  2127. */
  2128. static int
  2129. is_subject_of_agreemeent_local(const Slapi_Entry *local_entry, const Repl_Agmt *ra)
  2130. {
  2131. int retval = 0;
  2132. int is_in_subtree = 0;
  2133. const Slapi_DN *agreement_subtree = NULL;
  2134. /* First test for the sync'ed subtree */
  2135. agreement_subtree = windows_private_get_directory_subtree(ra);
  2136. if (NULL == agreement_subtree)
  2137. {
  2138. goto error;
  2139. }
  2140. is_in_subtree = slapi_sdn_scope_test(slapi_entry_get_sdn_const(local_entry), agreement_subtree, LDAP_SCOPE_SUBTREE);
  2141. if (is_in_subtree)
  2142. {
  2143. /* Next test for the correct kind of entry */
  2144. if (local_entry) {
  2145. /* DBDB: we should allocate these filters once and keep them around for better performance */
  2146. char *string_filter = slapi_ch_strdup("(&(|(objectclass=ntuser)(objectclass=ntgroup))(ntUserDomainId=*))");
  2147. Slapi_Filter *filter = slapi_str2filter( string_filter );
  2148. slapi_ch_free_string(&string_filter);
  2149. if (slapi_filter_test_simple( (Slapi_Entry*)local_entry, filter ) == 0)
  2150. {
  2151. retval = 1;
  2152. }
  2153. slapi_filter_free(filter,1);
  2154. filter = NULL;
  2155. } else
  2156. {
  2157. /* Error: couldn't find the entry */
  2158. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  2159. "failed to find entry in is_subject_of_agreemeent_local: %d\n", retval);
  2160. retval = 0;
  2161. }
  2162. }
  2163. error:
  2164. return retval;
  2165. }
  2166. /* 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 ?) */
  2167. static int
  2168. is_subject_of_agreemeent_remote(Slapi_Entry *e, const Repl_Agmt *ra)
  2169. {
  2170. int retval = 0;
  2171. int is_in_subtree = 0;
  2172. const Slapi_DN *agreement_subtree = NULL;
  2173. /* First test for the sync'ed subtree */
  2174. agreement_subtree = windows_private_get_windows_subtree(ra);
  2175. if (NULL == agreement_subtree)
  2176. {
  2177. goto error;
  2178. }
  2179. is_in_subtree = slapi_sdn_scope_test(slapi_entry_get_sdn_const(e), agreement_subtree, LDAP_SCOPE_SUBTREE);
  2180. if (is_in_subtree)
  2181. {
  2182. retval = 1;
  2183. }
  2184. error:
  2185. return retval;
  2186. }
  2187. static int
  2188. windows_create_local_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,const Slapi_DN* local_sdn)
  2189. {
  2190. int retval = 0;
  2191. char *entry_string = NULL;
  2192. Slapi_Entry *local_entry = NULL;
  2193. Slapi_PBlock* pb = NULL;
  2194. int is_user = 0;
  2195. int is_group = 0;
  2196. char *local_entry_template = NULL;
  2197. char *user_entry_template = NULL;
  2198. char *username = extract_username_from_entry(remote_entry);
  2199. Slapi_Attr *attr = NULL;
  2200. int rc = 0;
  2201. char *guid_str = NULL;
  2202. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  2203. char *local_user_entry_template =
  2204. "dn: %s\n"
  2205. "objectclass:top\n"
  2206. "objectclass:person\n"
  2207. "objectclass:organizationalperson\n"
  2208. "objectclass:inetOrgPerson\n"
  2209. "objectclass:ntUser\n"
  2210. "ntUserDeleteAccount:true\n"
  2211. "uid:%s\n";
  2212. char *local_nt4_user_entry_template =
  2213. "dn: %s\n"
  2214. "objectclass:top\n"
  2215. "objectclass:person\n"
  2216. "objectclass:organizationalperson\n"
  2217. "objectclass:inetOrgPerson\n"
  2218. "objectclass:ntUser\n"
  2219. "ntUserDeleteAccount:true\n"
  2220. "uid:%s\n";
  2221. char *local_group_entry_template =
  2222. "dn: %s\n"
  2223. "objectclass:top\n"
  2224. "objectclass:groupofuniquenames\n"
  2225. "objectclass:ntGroup\n"
  2226. "objectclass:mailGroup\n"
  2227. "ntGroupDeleteGroup:true\n"
  2228. "cn:%s\n";
  2229. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_create_local_entry\n", 0, 0, 0 );
  2230. windows_is_remote_entry_user_or_group(remote_entry,&is_user,&is_group);
  2231. user_entry_template = is_nt4 ? local_nt4_user_entry_template : local_user_entry_template;
  2232. local_entry_template = is_user ? user_entry_template : local_group_entry_template;
  2233. /* Create a new entry */
  2234. /* Give it its DN and username */
  2235. entry_string = slapi_ch_smprintf(local_entry_template,slapi_sdn_get_dn(local_sdn),username, username);
  2236. if (NULL == entry_string)
  2237. {
  2238. goto error;
  2239. }
  2240. local_entry = slapi_str2entry(entry_string, 0);
  2241. slapi_ch_free((void**)&entry_string);
  2242. if (NULL == local_entry)
  2243. {
  2244. goto error;
  2245. }
  2246. /* Map the appropriate attributes sourced from the remote entry */
  2247. for (rc = slapi_entry_first_attr(remote_entry, &attr); rc == 0;
  2248. rc = slapi_entry_next_attr(remote_entry, attr, &attr))
  2249. {
  2250. char *type = NULL;
  2251. Slapi_ValueSet *vs = NULL;
  2252. int mapdn = 0;
  2253. slapi_attr_get_type( attr, &type );
  2254. slapi_attr_get_valueset(attr,&vs);
  2255. if ( is_straight_mapped_attr(type,is_user,is_nt4) )
  2256. {
  2257. /* copy over the attr values */
  2258. slapi_entry_add_valueset(local_entry,type,vs);
  2259. } else
  2260. {
  2261. char *new_type = NULL;
  2262. windows_map_attr_name(type , 0 /* from windows */, is_user, 1 /* create */, &new_type, &mapdn);
  2263. if (new_type)
  2264. {
  2265. if (mapdn)
  2266. {
  2267. Slapi_ValueSet *mapped_values = NULL;
  2268. map_dn_values(prp,vs,&mapped_values, 0 /* from windows */,0);
  2269. if (mapped_values)
  2270. {
  2271. slapi_entry_add_valueset(local_entry,new_type,mapped_values);
  2272. slapi_valueset_free(mapped_values);
  2273. mapped_values = NULL;
  2274. }
  2275. } else
  2276. {
  2277. slapi_entry_add_valueset(local_entry,new_type,vs);
  2278. }
  2279. slapi_ch_free((void**)&new_type);
  2280. }
  2281. }
  2282. if (vs)
  2283. {
  2284. slapi_valueset_free(vs);
  2285. vs = NULL;
  2286. }
  2287. }
  2288. /* Copy over the GUID */
  2289. guid_str = extract_guid_from_entry(remote_entry, is_nt4);
  2290. if (guid_str)
  2291. {
  2292. slapi_entry_add_string(local_entry,"ntUniqueId",guid_str);
  2293. } else
  2294. {
  2295. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2296. "extract_guid_from_entry entry %s failed to extract the guid\n", slapi_sdn_get_dn(local_sdn));
  2297. /* Fatal error : need the guid */
  2298. goto error;
  2299. }
  2300. /* Hack for NT4, which has no surname */
  2301. if (is_nt4 && is_user)
  2302. {
  2303. slapi_entry_add_string(local_entry,"sn",username);
  2304. }
  2305. /* Store it */
  2306. windows_dump_entry("Adding new local entry",local_entry);
  2307. pb = slapi_pblock_new();
  2308. slapi_add_entry_internal_set_pb(pb, local_entry, NULL,repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),0);
  2309. slapi_add_internal_pb(pb);
  2310. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &retval);
  2311. if (retval) {
  2312. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2313. "add operation of entry %s returned: %d\n", slapi_sdn_get_dn(local_sdn), retval);
  2314. }
  2315. error:
  2316. if (pb)
  2317. {
  2318. slapi_pblock_destroy(pb);
  2319. }
  2320. if (username)
  2321. {
  2322. slapi_ch_free((void**)&username);
  2323. }
  2324. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_create_local_entry\n", 0, 0, 0 );
  2325. return retval;
  2326. }
  2327. /* Function to generate the correct mods to bring 'local' attribute values into consistency with given 'remote' values */
  2328. /* 'local' and 'remote' in quotes because we actually use this function in both directions */
  2329. /* This function expects the value sets to have been already pruned to exclude values that are not
  2330. * subject to the agreement and present in the peer. */
  2331. static int
  2332. 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)
  2333. {
  2334. int ret = 0;
  2335. int i = 0;
  2336. Slapi_Value *rv = NULL;
  2337. Slapi_Value *lv = NULL;
  2338. /* We need to generate an ADD mod for each entry that is in the remote values but not in the local values */
  2339. /* Iterate over the remote values */
  2340. i = slapi_valueset_first_value(remote_values,&rv);
  2341. while (NULL != rv)
  2342. {
  2343. const char *remote_dn = slapi_value_get_string(rv);
  2344. int value_present_in_local_values = (NULL != slapi_valueset_find(attr, local_values, rv));
  2345. if (!value_present_in_local_values)
  2346. {
  2347. slapi_mods_add_string(smods,LDAP_MOD_ADD,local_type,remote_dn);
  2348. *do_modify = 1;
  2349. }
  2350. i = slapi_valueset_next_value(remote_values,i,&rv);
  2351. }
  2352. /* We need to generate a DEL mod for each entry that is in the local values but not in the remote values */
  2353. /* Iterate over the local values */
  2354. i = slapi_valueset_first_value(local_values,&lv);
  2355. while (NULL != lv)
  2356. {
  2357. const char *local_dn = slapi_value_get_string(lv);
  2358. int value_present_in_remote_values = (NULL != slapi_valueset_find(attr, remote_values, lv));
  2359. if (!value_present_in_remote_values)
  2360. {
  2361. slapi_mods_add_string(smods,LDAP_MOD_DELETE,local_type,local_dn);
  2362. *do_modify = 1;
  2363. }
  2364. i = slapi_valueset_next_value(local_values,i,&lv);
  2365. }
  2366. return ret;
  2367. }
  2368. static int
  2369. 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)
  2370. {
  2371. int retval = 0;
  2372. Slapi_Attr *attr = NULL;
  2373. Slapi_Attr *del_attr = NULL;
  2374. int is_user = 0;
  2375. int is_group = 0;
  2376. int rc = 0;
  2377. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  2378. /* Iterate over the attributes on the remote entry, updating the local entry where appropriate */
  2379. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_generate_update_mods\n", 0, 0, 0 );
  2380. *do_modify = 0;
  2381. if (to_windows)
  2382. {
  2383. windows_is_local_entry_user_or_group(remote_entry,&is_user,&is_group);
  2384. } else
  2385. {
  2386. windows_is_remote_entry_user_or_group(remote_entry,&is_user,&is_group);
  2387. }
  2388. for (rc = slapi_entry_first_attr(remote_entry, &attr); rc == 0;
  2389. rc = slapi_entry_next_attr(remote_entry, attr, &attr))
  2390. {
  2391. int is_present_local = 0;
  2392. char *type = NULL;
  2393. Slapi_ValueSet *vs = NULL;
  2394. char *local_type = NULL;
  2395. Slapi_Attr *local_attr = NULL;
  2396. int is_guid = 0;
  2397. int mapdn = 0;
  2398. slapi_attr_get_type( attr, &type );
  2399. slapi_attr_get_valueset(attr,&vs);
  2400. /* First determine what we will do with this attr */
  2401. /* If it's a GUID, we need to take special action */
  2402. if (0 == slapi_attr_type_cmp(type,"objectGuid",SLAPI_TYPE_CMP_SUBTYPE) && !to_windows)
  2403. {
  2404. is_guid = 1;
  2405. local_type = slapi_ch_strdup("ntUniqueId");
  2406. } else
  2407. {
  2408. if ( is_straight_mapped_attr(type,is_user,is_nt4) ) {
  2409. local_type = slapi_ch_strdup(type);
  2410. } else {
  2411. windows_map_attr_name(type , to_windows, is_user, 0 /* not create */, &local_type, &mapdn);
  2412. }
  2413. is_guid = 0;
  2414. }
  2415. if (NULL == local_type)
  2416. {
  2417. /* Means we do not map this attribute */
  2418. if (vs)
  2419. {
  2420. slapi_valueset_free(vs);
  2421. vs = NULL;
  2422. }
  2423. continue;
  2424. }
  2425. slapi_entry_attr_find(local_entry,local_type,&local_attr);
  2426. is_present_local = (NULL == local_attr) ? 0 : 1;
  2427. /* Is the attribute present on the local entry ? */
  2428. if (is_present_local && !is_guid)
  2429. {
  2430. if (!mapdn)
  2431. {
  2432. int values_equal = 0;
  2433. /* AD has a legth contraint on the initials attribute,
  2434. * so treat is as a special case. */
  2435. if (0 == slapi_attr_type_cmp(type,"initials",SLAPI_TYPE_CMP_SUBTYPE) && !to_windows) {
  2436. values_equal = attr_compare_equal(attr, local_attr, AD_INITIALS_LENGTH);
  2437. } else {
  2438. /* Compare the entire attribute values */
  2439. values_equal = attr_compare_equal(attr, local_attr, 0);
  2440. }
  2441. /* If it is then we need to replace the local values with the remote values if they are different */
  2442. if (!values_equal)
  2443. {
  2444. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2445. "windows_generate_update_mods: %s, %s : values are different\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry)), local_type);
  2446. slapi_mods_add_mod_values(smods,LDAP_MOD_REPLACE,local_type,valueset_get_valuearray(vs));
  2447. *do_modify = 1;
  2448. } else
  2449. {
  2450. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2451. "windows_generate_update_mods: %s, %s : values are equal\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry)), local_type);
  2452. }
  2453. } else {
  2454. /* A dn-valued attribute : need to take special steps */
  2455. Slapi_ValueSet *mapped_remote_values = NULL;
  2456. /* First map all the DNs to that they're in a consistent domain */
  2457. map_dn_values(prp,vs,&mapped_remote_values, to_windows,0);
  2458. if (mapped_remote_values)
  2459. {
  2460. Slapi_ValueSet *local_values = NULL;
  2461. Slapi_ValueSet *restricted_local_values = NULL;
  2462. /* Now do a compare on the values, generating mods to bring them into consistency (if any) */
  2463. /* We ignore any DNs that are outside the scope of the agreement (on both sides) */
  2464. slapi_attr_get_valueset(local_attr,&local_values);
  2465. map_dn_values(prp,local_values,&restricted_local_values,!to_windows,1);
  2466. if (restricted_local_values)
  2467. {
  2468. windows_generate_dn_value_mods(local_type,local_attr,smods,mapped_remote_values,restricted_local_values,do_modify);
  2469. slapi_valueset_free(restricted_local_values);
  2470. restricted_local_values = NULL;
  2471. }
  2472. slapi_valueset_free(mapped_remote_values);
  2473. mapped_remote_values = NULL;
  2474. if (local_values)
  2475. {
  2476. slapi_valueset_free(local_values);
  2477. local_values = NULL;
  2478. }
  2479. }
  2480. }
  2481. } else
  2482. {
  2483. if (!is_present_local)
  2484. {
  2485. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2486. "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);
  2487. /* If it is currently absent, then we add the value from the remote entry */
  2488. if (is_guid)
  2489. {
  2490. /* Translate the guid value */
  2491. char *guid = extract_guid_from_entry(remote_entry, is_nt4);
  2492. if (guid)
  2493. {
  2494. slapi_mods_add_string(smods,LDAP_MOD_ADD,local_type,guid);
  2495. slapi_ch_free((void**)&guid);
  2496. }
  2497. } else
  2498. {
  2499. /* Handle DN valued attributes here */
  2500. if (mapdn)
  2501. {
  2502. Slapi_ValueSet *mapped_values = NULL;
  2503. map_dn_values(prp,vs,&mapped_values, to_windows,0);
  2504. if (mapped_values)
  2505. {
  2506. slapi_mods_add_mod_values(smods,LDAP_MOD_ADD,local_type,valueset_get_valuearray(mapped_values));
  2507. slapi_valueset_free(mapped_values);
  2508. mapped_values = NULL;
  2509. }
  2510. } else
  2511. {
  2512. slapi_mods_add_mod_values(smods,LDAP_MOD_ADD,local_type,valueset_get_valuearray(vs));
  2513. }
  2514. }
  2515. *do_modify = 1;
  2516. }
  2517. }
  2518. if (vs)
  2519. {
  2520. slapi_valueset_free(vs);
  2521. vs = NULL;
  2522. }
  2523. slapi_ch_free_string(&local_type);
  2524. }
  2525. /* Check if any attributes were deleted from the remote entry */
  2526. entry_first_deleted_attribute(remote_entry, &del_attr);
  2527. while (del_attr != NULL) {
  2528. Slapi_Attr *local_attr = NULL;
  2529. char *type = NULL;
  2530. char *local_type = NULL;
  2531. int mapdn = 0;
  2532. /* Map remote type to local type */
  2533. slapi_attr_get_type(del_attr, &type);
  2534. if ( is_straight_mapped_attr(type,is_user,is_nt4) ) {
  2535. local_type = slapi_ch_strdup(type);
  2536. } else {
  2537. windows_map_attr_name(type , to_windows, is_user, 0 /* not create */, &local_type, &mapdn);
  2538. }
  2539. /* Check if this attr exists in the local entry */
  2540. if (local_type) {
  2541. slapi_entry_attr_find(local_entry, local_type, &local_attr);
  2542. if (local_attr) {
  2543. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2544. "windows_generate_update_mods: deleting %s attribute from local entry\n", local_type);
  2545. /* Delete this attr from the local entry */
  2546. slapi_mods_add_mod_values(smods, LDAP_MOD_DELETE, local_type, NULL);
  2547. *do_modify = 1;
  2548. }
  2549. }
  2550. entry_next_deleted_attribute(remote_entry, &del_attr);
  2551. slapi_ch_free_string(&local_type);
  2552. }
  2553. if (slapi_is_loglevel_set(SLAPI_LOG_REPL) && *do_modify)
  2554. {
  2555. slapi_mods_dump(smods,"windows sync");
  2556. }
  2557. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_generate_update_mods: %d\n", retval, 0, 0 );
  2558. return retval;
  2559. }
  2560. static int
  2561. windows_update_remote_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,Slapi_Entry *local_entry)
  2562. {
  2563. Slapi_Mods smods = {0};
  2564. int retval = 0;
  2565. int do_modify = 0;
  2566. slapi_mods_init (&smods, 0);
  2567. retval = windows_generate_update_mods(prp,local_entry,remote_entry,1,&smods,&do_modify);
  2568. /* Now perform the modify if we need to */
  2569. if (0 == retval && do_modify)
  2570. {
  2571. char dnbuf[BUFSIZ];
  2572. const char *dn = slapi_sdn_get_dn(slapi_entry_get_sdn_const(remote_entry));
  2573. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2574. "windows_update_remote_entry: modifying entry %s\n", escape_string(dn, dnbuf));
  2575. 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);
  2576. } else
  2577. {
  2578. char dnbuf[BUFSIZ];
  2579. const char *dn = slapi_sdn_get_dn(slapi_entry_get_sdn_const(remote_entry));
  2580. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2581. "no mods generated for remote entry: %s\n", escape_string(dn, dnbuf));
  2582. }
  2583. slapi_mods_done(&smods);
  2584. return retval;
  2585. }
  2586. static int
  2587. windows_update_local_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,Slapi_Entry *local_entry)
  2588. {
  2589. Slapi_Mods smods = {0};
  2590. int retval = 0;
  2591. Slapi_PBlock *pb = NULL;
  2592. int do_modify = 0;
  2593. slapi_mods_init (&smods, 0);
  2594. retval = windows_generate_update_mods(prp,remote_entry,local_entry,0,&smods,&do_modify);
  2595. /* Now perform the modify if we need to */
  2596. if (0 == retval && do_modify)
  2597. {
  2598. int rc = 0;
  2599. pb = slapi_pblock_new();
  2600. if (pb)
  2601. {
  2602. char dnbuf[BUFSIZ];
  2603. const char *dn = slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry));
  2604. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2605. "modifying entry: %s\n", escape_string(dn, dnbuf));
  2606. slapi_modify_internal_set_pb (pb, slapi_entry_get_ndn(local_entry), slapi_mods_get_ldapmods_byref(&smods), NULL, NULL,
  2607. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2608. slapi_modify_internal_pb (pb);
  2609. slapi_pblock_get (pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2610. if (rc)
  2611. {
  2612. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  2613. "windows_update_local_entry: failed to modify entry %s\n", escape_string(dn, dnbuf));
  2614. }
  2615. slapi_pblock_destroy(pb);
  2616. } else
  2617. {
  2618. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  2619. "failed to make pb in windows_update_local_entry\n");
  2620. }
  2621. } else
  2622. {
  2623. char dnbuf[BUFSIZ];
  2624. const char *dn = slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry));
  2625. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2626. "no mods generated for local entry: %s\n", escape_string(dn, dnbuf));
  2627. }
  2628. slapi_mods_done(&smods);
  2629. return retval;
  2630. }
  2631. static int
  2632. windows_process_total_add(Private_Repl_Protocol *prp,Slapi_Entry *e, Slapi_DN* remote_dn, int missing_entry)
  2633. {
  2634. int retval = 0;
  2635. LDAPMod **entryattrs = NULL;
  2636. Slapi_Entry *mapped_entry = NULL;
  2637. char *password = NULL;
  2638. const Slapi_DN* local_dn = NULL;
  2639. /* First map the entry */
  2640. local_dn = slapi_entry_get_sdn_const(e);
  2641. if (missing_entry)
  2642. retval = windows_create_remote_entry(prp, e, remote_dn, &mapped_entry, &password);
  2643. /* Convert entry to mods */
  2644. if (0 == retval && mapped_entry)
  2645. {
  2646. (void)slapi_entry2mods (mapped_entry , NULL /* &entrydn : We don't need it */, &entryattrs);
  2647. slapi_entry_free(mapped_entry);
  2648. mapped_entry = NULL;
  2649. if (NULL == entryattrs)
  2650. {
  2651. 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));
  2652. retval = CONN_LOCAL_ERROR;
  2653. }
  2654. else
  2655. {
  2656. windows_log_add_entry_remote(local_dn, remote_dn);
  2657. retval = windows_conn_send_add(prp->conn, slapi_sdn_get_dn(remote_dn), entryattrs, NULL, NULL /* returned controls */);
  2658. /* It's possible that the entry already exists in AD, in which case we fall back to modify it */
  2659. if (retval)
  2660. {
  2661. 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));
  2662. }
  2663. ldap_mods_free(entryattrs, 1);
  2664. entryattrs = NULL;
  2665. }
  2666. } else
  2667. {
  2668. /* Entry already exists, need to mod it instead */
  2669. Slapi_Entry *remote_entry = NULL;
  2670. /* Get the remote entry */
  2671. retval = windows_get_remote_entry(prp, remote_dn,&remote_entry);
  2672. if (0 == retval && remote_entry)
  2673. {
  2674. retval = windows_update_remote_entry(prp,remote_entry,e);
  2675. /* Detect the case where the error is benign */
  2676. if (retval)
  2677. {
  2678. int operation = 0;
  2679. int error = 0;
  2680. conn_get_error(prp->conn, &operation, &error);
  2681. if (windows_ignore_error_and_keep_going(error))
  2682. {
  2683. retval = CONN_OPERATION_SUCCESS;
  2684. }
  2685. }
  2686. }
  2687. if (remote_entry)
  2688. {
  2689. slapi_entry_free(remote_entry);
  2690. }
  2691. }
  2692. return retval;
  2693. }
  2694. static int
  2695. windows_process_total_delete(Private_Repl_Protocol *prp,Slapi_Entry *e, Slapi_DN* remote_dn)
  2696. {
  2697. int retval = 0;
  2698. if (delete_remote_entry_allowed(e))
  2699. {
  2700. retval = windows_conn_send_delete(prp->conn, slapi_sdn_get_dn(remote_dn), NULL, NULL /* returned controls */);
  2701. }
  2702. return retval;
  2703. }
  2704. /* Entry point for the total protocol */
  2705. int windows_process_total_entry(Private_Repl_Protocol *prp,Slapi_Entry *e)
  2706. {
  2707. int retval = 0;
  2708. int is_ours = 0;
  2709. int is_tombstone = 0;
  2710. Slapi_DN *remote_dn = NULL;
  2711. int missing_entry = 0;
  2712. const Slapi_DN *local_dn = slapi_entry_get_sdn_const(e);
  2713. /* First check if the entry is for us */
  2714. is_ours = is_subject_of_agreemeent_local(e, prp->agmt);
  2715. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2716. "%s: windows_process_total_entry: Looking dn=\"%s\" (%s)\n",
  2717. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)), is_ours ? "ours" : "not ours");
  2718. if (is_ours)
  2719. {
  2720. retval = map_entry_dn_outbound(e,&remote_dn,prp,&missing_entry,0 /* we don't want the GUID */);
  2721. if (retval || NULL == remote_dn)
  2722. {
  2723. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2724. "%s: windows_replay_update: failed map dn for total update dn=\"%s\"\n",
  2725. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(local_dn));
  2726. goto error;
  2727. }
  2728. /* Either the entry is a tombstone, or not a tombstone */
  2729. if (is_tombstone)
  2730. {
  2731. retval = windows_process_total_delete(prp,e,remote_dn);
  2732. } else
  2733. {
  2734. retval = windows_process_total_add(prp,e,remote_dn,missing_entry);
  2735. }
  2736. }
  2737. if (remote_dn)
  2738. {
  2739. slapi_sdn_free(&remote_dn);
  2740. }
  2741. error:
  2742. return retval;
  2743. }
  2744. int
  2745. windows_search_local_entry_by_uniqueid(Private_Repl_Protocol *prp, const char *uniqueid, char ** attrs, Slapi_Entry **ret_entry , void * component_identity)
  2746. {
  2747. Slapi_Entry **entries = NULL;
  2748. Slapi_PBlock *int_search_pb = NULL;
  2749. int rc = 0;
  2750. char *filter_string = NULL;
  2751. const Slapi_DN *local_subtree = NULL;
  2752. *ret_entry = NULL;
  2753. local_subtree = windows_private_get_directory_subtree(prp->agmt);
  2754. filter_string = PR_smprintf("(&(|(objectclass=*)(objectclass=ldapsubentry)(objectclass=nsTombstone))(nsUniqueid=%s))",uniqueid);
  2755. int_search_pb = slapi_pblock_new ();
  2756. slapi_search_internal_set_pb ( int_search_pb, slapi_sdn_get_dn(local_subtree), LDAP_SCOPE_SUBTREE, filter_string,
  2757. attrs ,
  2758. 0 /* attrsonly */, NULL /* controls */,
  2759. NULL /* uniqueid */,
  2760. component_identity, 0 /* actions */ );
  2761. slapi_search_internal_pb ( int_search_pb );
  2762. slapi_pblock_get( int_search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc );
  2763. if ( LDAP_SUCCESS == rc ) {
  2764. slapi_pblock_get( int_search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
  2765. if ( NULL != entries && NULL != entries[ 0 ]) {
  2766. Slapi_Entry *temp_entry = NULL;
  2767. temp_entry = entries[ 0 ];
  2768. *ret_entry = slapi_entry_dup(temp_entry);
  2769. } else {
  2770. /* No entry there */
  2771. rc = LDAP_NO_SUCH_OBJECT;
  2772. }
  2773. }
  2774. slapi_free_search_results_internal(int_search_pb);
  2775. slapi_pblock_destroy(int_search_pb);
  2776. int_search_pb = NULL;
  2777. if (filter_string)
  2778. {
  2779. PR_smprintf_free(filter_string);
  2780. }
  2781. return rc;
  2782. }
  2783. static int
  2784. windows_get_local_entry_by_uniqueid(Private_Repl_Protocol *prp,const char* uniqueid,Slapi_Entry **local_entry)
  2785. {
  2786. int retval = ENTRY_NOTFOUND;
  2787. Slapi_Entry *new_entry = NULL;
  2788. windows_search_local_entry_by_uniqueid( prp, uniqueid, NULL, &new_entry,
  2789. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  2790. if (new_entry)
  2791. {
  2792. *local_entry = new_entry;
  2793. retval = 0;
  2794. }
  2795. return retval;
  2796. }
  2797. static int
  2798. windows_get_local_entry(const Slapi_DN* local_dn,Slapi_Entry **local_entry)
  2799. {
  2800. int retval = ENTRY_NOTFOUND;
  2801. Slapi_Entry *new_entry = NULL;
  2802. slapi_search_internal_get_entry( (Slapi_DN*)local_dn, NULL, &new_entry,
  2803. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  2804. if (new_entry)
  2805. {
  2806. *local_entry = new_entry;
  2807. retval = 0;
  2808. }
  2809. return retval;
  2810. }
  2811. static int
  2812. windows_process_dirsync_entry(Private_Repl_Protocol *prp,Slapi_Entry *e, int is_total)
  2813. {
  2814. Slapi_DN* local_sdn = NULL;
  2815. int rc = 0;
  2816. /* deleted users are outside the 'correct container'.
  2817. They live in cn=deleted objects, windows_private_get_directory_subtree( prp->agmt) */
  2818. if (is_tombstone(e))
  2819. {
  2820. rc = map_tombstone_dn_inbound(e, &local_sdn, prp->agmt);
  2821. if ((0 == rc) && local_sdn)
  2822. {
  2823. /* Go ahead and delte the local peer */
  2824. rc = windows_delete_local_entry(local_sdn);
  2825. slapi_sdn_free(&local_sdn);
  2826. } else
  2827. {
  2828. 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));
  2829. }
  2830. } else
  2831. {
  2832. /* Is this entry one we should be interested in ? */
  2833. if (is_subject_of_agreemeent_remote(e,prp->agmt))
  2834. {
  2835. /* First make its local DN */
  2836. rc = map_entry_dn_inbound(e, &local_sdn, prp->agmt);
  2837. if ((0 == rc) && local_sdn)
  2838. {
  2839. Slapi_Entry *local_entry = NULL;
  2840. /* Get the local entry if it exists */
  2841. rc = windows_get_local_entry(local_sdn,&local_entry);
  2842. if ((0 == rc) && local_entry)
  2843. {
  2844. /* Since the entry exists we should now make it match the entry we received from AD */
  2845. /* Actually we are better off simply fetching the entire entry from AD and using that
  2846. * because otherwise we don't get all the attributes we need to make sense of it such as
  2847. * objectclass */
  2848. Slapi_Entry *remote_entry = NULL;
  2849. windows_get_remote_entry(prp,slapi_entry_get_sdn_const(e),&remote_entry);
  2850. if (remote_entry)
  2851. {
  2852. /* We need to check for any deleted attrs from the dirsync entry
  2853. * and pass them into the newly fetched remote entry. */
  2854. Slapi_Attr *attr = NULL;
  2855. Slapi_Attr *rem_attr = NULL;
  2856. entry_first_deleted_attribute(e, &attr);
  2857. while (attr != NULL) {
  2858. /* We need to dup the attr and add it to the remote entry.
  2859. * rem_attr is now owned by remote_entry, so don't free it */
  2860. rem_attr = slapi_attr_dup(attr);
  2861. if (rem_attr) {
  2862. entry_add_deleted_attribute_wsi(remote_entry, rem_attr);
  2863. rem_attr = NULL;
  2864. }
  2865. entry_next_deleted_attribute(e, &attr);
  2866. }
  2867. rc = windows_update_local_entry(prp, remote_entry, local_entry);
  2868. slapi_entry_free(remote_entry);
  2869. remote_entry = NULL;
  2870. } else
  2871. {
  2872. 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));
  2873. }
  2874. slapi_entry_free(local_entry);
  2875. if (rc) {
  2876. /* Something bad happened */
  2877. 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),
  2878. slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)));
  2879. }
  2880. } else
  2881. {
  2882. /* If it doesn't exist, try to make it */
  2883. if (add_local_entry_allowed(prp,e))
  2884. {
  2885. windows_create_local_entry(prp,e,local_sdn);
  2886. } else
  2887. {
  2888. 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)
  2889. , slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)));
  2890. }
  2891. }
  2892. slapi_sdn_free(&local_sdn);
  2893. } else
  2894. {
  2895. /* We should have been able to map the DN, so this is an error */
  2896. 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)
  2897. , slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)));
  2898. }
  2899. } /* subject of agreement */
  2900. } /* is tombstone */
  2901. return rc;
  2902. }
  2903. void
  2904. windows_dirsync_inc_run(Private_Repl_Protocol *prp)
  2905. {
  2906. int rc = 0;
  2907. int done = 0;
  2908. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_dirsync_inc_run\n", 0, 0, 0 );
  2909. while (!done) {
  2910. Slapi_Entry *e = NULL;
  2911. rc = send_dirsync_search(prp->conn);
  2912. if (rc != CONN_OPERATION_SUCCESS)
  2913. {
  2914. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  2915. "failed to send dirsync search request: %d\n", rc);
  2916. goto error;
  2917. }
  2918. while ( (e = windows_conn_get_search_result(prp->conn) ) != NULL)
  2919. {
  2920. rc = windows_process_dirsync_entry(prp,e,0);
  2921. if (e)
  2922. {
  2923. slapi_entry_free(e);
  2924. }
  2925. } /* While entry != NULL */
  2926. if (!windows_private_dirsync_has_more(prp->agmt))
  2927. {
  2928. done = 1;
  2929. }
  2930. } /* While !done */
  2931. error:
  2932. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_dirsync_inc_run\n", 0, 0, 0 );
  2933. }