windows_protocol_util.c 81 KB

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