windows_protocol_util.c 116 KB

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