windows_protocol_util.c 116 KB

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