windows_protocol_util.c 87 KB

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