windows_protocol_util.c 88 KB

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