windows_protocol_util.c 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944
  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. csn_free(&current_csn);
  623. }
  624. }
  625. if (ACQUIRE_SUCCESS != return_value)
  626. {
  627. /* could not acquire the replica, so reinstate the linger timer, since this
  628. means we won't call release_replica, which also reinstates the timer */
  629. windows_conn_start_linger(conn);
  630. }
  631. else
  632. {
  633. /* replica successfully acquired */
  634. prp->replica_acquired = PR_TRUE;
  635. }
  636. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_acquire_replica\n", 0, 0, 0 );
  637. return return_value;
  638. }
  639. void
  640. windows_release_replica(Private_Repl_Protocol *prp)
  641. {
  642. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_release_replica\n", 0, 0, 0 );
  643. PR_ASSERT(NULL != prp);
  644. PR_ASSERT(NULL != prp->conn);
  645. if (!prp->replica_acquired)
  646. return;
  647. windows_conn_start_linger(prp->conn);
  648. prp->replica_acquired = PR_FALSE;
  649. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_release_replica\n", 0, 0, 0 );
  650. }
  651. static void
  652. to_little_endian_double_bytes(UChar *unicode_password, int32_t unicode_password_length)
  653. {
  654. int32_t i = 0;
  655. for (i = 0 ; i < unicode_password_length; i++)
  656. {
  657. UChar c = unicode_password[i];
  658. char *byte_ptr = (char*)&(unicode_password[i]);
  659. byte_ptr[0] = (char)(c & 0xff);
  660. byte_ptr[1] = (char)(c >> 8);
  661. }
  662. }
  663. /* this entry had a password, handle it seperately */
  664. /* http://support.microsoft.com/?kbid=269190 */
  665. static int
  666. send_password_modify(Slapi_DN *sdn, char *password, Private_Repl_Protocol *prp)
  667. {
  668. ConnResult pw_return = 0;
  669. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  670. if (is_nt4)
  671. {
  672. /* NT4 just wants a plaintext password */
  673. Slapi_Mods smods = {0};
  674. slapi_mods_init (&smods, 0);
  675. slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "UnicodePwd", password);
  676. pw_return = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(sdn), slapi_mods_get_ldapmods_byref(&smods), NULL, NULL );
  677. slapi_mods_done(&smods);
  678. } else
  679. {
  680. /* We will attempt to bind to AD with the new password first. We do
  681. * this to avoid playing a password change that originated from AD
  682. * back to AD. If we just played the password change back, then
  683. * both sides would be in sync, but AD would contain the new password
  684. * twice in it's password history, which undermines the password
  685. * history policies in AD. */
  686. if (windows_check_user_password(prp->conn, sdn, password)) {
  687. char *quoted_password = NULL;
  688. /* AD wants the password in quotes ! */
  689. quoted_password = PR_smprintf("\"%s\"",password);
  690. if (quoted_password)
  691. {
  692. LDAPMod *pw_mods[2];
  693. LDAPMod pw_mod;
  694. struct berval bv = {0};
  695. UChar *unicode_password = NULL;
  696. int32_t unicode_password_length = 0; /* Length in _characters_ */
  697. int32_t buffer_size = 0; /* Size in _characters_ */
  698. UErrorCode error = U_ZERO_ERROR;
  699. struct berval *bvals[2];
  700. /* Need to UNICODE encode the password here */
  701. /* It's one of those 'ask me first and I will tell you the buffer size' functions */
  702. u_strFromUTF8(NULL, 0, &unicode_password_length, quoted_password, strlen(quoted_password), &error);
  703. buffer_size = unicode_password_length;
  704. unicode_password = (UChar *)slapi_ch_malloc(unicode_password_length * sizeof(UChar));
  705. if (unicode_password) {
  706. error = U_ZERO_ERROR;
  707. u_strFromUTF8(unicode_password, buffer_size, &unicode_password_length, quoted_password, strlen(quoted_password), &error);
  708. /* As an extra special twist, we need to send the unicode in little-endian order for AD to be happy */
  709. to_little_endian_double_bytes(unicode_password, unicode_password_length);
  710. bv.bv_len = unicode_password_length * sizeof(UChar);
  711. bv.bv_val = (char*)unicode_password;
  712. bvals[0] = &bv;
  713. bvals[1] = NULL;
  714. pw_mod.mod_type = "UnicodePwd";
  715. pw_mod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  716. pw_mod.mod_bvalues = bvals;
  717. pw_mods[0] = &pw_mod;
  718. pw_mods[1] = NULL;
  719. pw_return = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(sdn), pw_mods, NULL, NULL );
  720. slapi_ch_free((void**)&unicode_password);
  721. }
  722. PR_smprintf_free(quoted_password);
  723. }
  724. } else {
  725. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  726. "%s: AD already has the current password for %s. "
  727. "Not sending password modify to AD.\n",
  728. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(sdn));
  729. }
  730. }
  731. return pw_return;
  732. }
  733. static int
  734. send_accountcontrol_modify(Slapi_DN *sdn, Private_Repl_Protocol *prp, int missing_entry)
  735. {
  736. ConnResult mod_return = 0;
  737. Slapi_Mods smods = {0};
  738. Slapi_Entry *remote_entry = NULL;
  739. int retval;
  740. unsigned long acctval = 0;
  741. char acctvalstr[32];
  742. /* have to first retrieve the existing entry - userAccountControl is
  743. a bit array, and we must preserve the existing values if any */
  744. /* Get the remote entry */
  745. retval = windows_get_remote_entry(prp, sdn, &remote_entry);
  746. if (0 == retval && remote_entry) {
  747. acctval = slapi_entry_attr_get_ulong(remote_entry, "userAccountControl");
  748. }
  749. slapi_entry_free(remote_entry);
  750. /* if we are adding a new entry, we need to set the entry to be
  751. enabled to allow AD login */
  752. if (missing_entry) {
  753. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  754. "%s: New Windows entry %s will be enabled.\n",
  755. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(sdn));
  756. acctval &= ~0x2; /* unset the disabled bit, if set */
  757. }
  758. /* set the account to be a normal account */
  759. acctval |= 0x0200; /* normal account == 512 */
  760. slapi_mods_init (&smods, 0);
  761. PR_snprintf(acctvalstr, sizeof(acctvalstr), "%lu", acctval);
  762. slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "userAccountControl", acctvalstr);
  763. mod_return = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(sdn), slapi_mods_get_ldapmods_byref(&smods), NULL, NULL );
  764. slapi_mods_done(&smods);
  765. return mod_return;
  766. }
  767. static int
  768. windows_entry_has_attr_and_value(Slapi_Entry *e, const char *attrname, char *value)
  769. {
  770. int retval = 0;
  771. Slapi_Attr *attr = NULL;
  772. if (NULL == e || NULL == attrname)
  773. {
  774. return retval;
  775. }
  776. /* see if the entry has the specified attribute name */
  777. if (0 == slapi_entry_attr_find(e, attrname, &attr) && attr)
  778. {
  779. /* if value is not null, see if the attribute has that
  780. value */
  781. if (value)
  782. {
  783. Slapi_Value *v = NULL;
  784. int index = 0;
  785. for (index = slapi_attr_first_value(attr, &v);
  786. v && (index != -1);
  787. index = slapi_attr_next_value(attr, index, &v))
  788. {
  789. const char *s = slapi_value_get_string(v);
  790. if (NULL == s)
  791. {
  792. continue;
  793. }
  794. if (0 == strcasecmp(s, value))
  795. {
  796. retval = 1;
  797. break;
  798. }
  799. }
  800. }
  801. }
  802. return retval;
  803. }
  804. static void
  805. windows_is_local_entry_user_or_group(Slapi_Entry *e, int *is_user, int *is_group)
  806. {
  807. *is_user = windows_entry_has_attr_and_value(e,"objectclass","ntuser");
  808. *is_group = windows_entry_has_attr_and_value(e,"objectclass","ntgroup");
  809. }
  810. static void
  811. windows_is_remote_entry_user_or_group(Slapi_Entry *e, int *is_user, int *is_group)
  812. {
  813. *is_user = windows_entry_has_attr_and_value(e,"objectclass","person");
  814. *is_group = windows_entry_has_attr_and_value(e,"objectclass","group");
  815. }
  816. static int
  817. add_remote_entry_allowed(Slapi_Entry *e)
  818. {
  819. /* We say yes if the entry has the ntUserCreateNewAccount attribute set in the case of a user, or the ntGroupDeleteGroup
  820. * attribute set in the case of a group
  821. */
  822. /* Is this a user or a group ? */
  823. int is_user = 0;
  824. int is_group = 0;
  825. char *delete_attr = NULL;
  826. windows_is_local_entry_user_or_group(e,&is_user,&is_group);
  827. if (!is_user && !is_group)
  828. {
  829. /* Neither fish nor foul.. */
  830. return -1;
  831. }
  832. if (is_user && is_group)
  833. {
  834. /* Now that's just really strange... */
  835. return -1;
  836. }
  837. if (is_user)
  838. {
  839. delete_attr = "ntUserCreateNewAccount";
  840. } else
  841. {
  842. delete_attr = "ntGroupCreateNewGroup";
  843. }
  844. /* Now test if the attribute value is set */
  845. return windows_entry_has_attr_and_value(e,delete_attr,"true");
  846. }
  847. /* Tells us if we're allowed to add this (remote) entry locally */
  848. static int
  849. add_local_entry_allowed(Private_Repl_Protocol *prp, Slapi_Entry *e)
  850. {
  851. int is_user = 0;
  852. int is_group = 0;
  853. windows_is_remote_entry_user_or_group(e,&is_user,&is_group);
  854. if (is_user)
  855. {
  856. return windows_private_create_users(prp->agmt);
  857. }
  858. if (is_group)
  859. {
  860. return windows_private_create_groups(prp->agmt);
  861. }
  862. /* Default to 'no' */
  863. return 0;
  864. }
  865. static int
  866. delete_remote_entry_allowed(Slapi_Entry *e)
  867. {
  868. /* We say yes if the entry has the ntUserDeleteAccount attribute set in the case of a user, or the ntGroupDeleteGroup
  869. * attribute set in the case of a group
  870. */
  871. /* Is this a user or a group ? */
  872. int is_user = 0;
  873. int is_group = 0;
  874. char *delete_attr = NULL;
  875. windows_is_local_entry_user_or_group(e,&is_user,&is_group);
  876. if (!is_user && !is_group)
  877. {
  878. /* Neither fish nor foul.. */
  879. return 0;
  880. }
  881. if (is_user && is_group)
  882. {
  883. /* Now that's just really strange... */
  884. return 0;
  885. }
  886. if (is_user)
  887. {
  888. delete_attr = "ntUserDeleteAccount";
  889. } else
  890. {
  891. delete_attr = "ntGroupDeleteGroup";
  892. }
  893. /* Now test if the attribute value is set */
  894. return windows_entry_has_attr_and_value(e,delete_attr,"true");
  895. }
  896. static void
  897. windows_log_add_entry_remote(const Slapi_DN *local_dn,const Slapi_DN *remote_dn)
  898. {
  899. const char* local_dn_string = slapi_sdn_get_dn(local_dn);
  900. const char* remote_dn_string = slapi_sdn_get_dn(remote_dn);
  901. 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);
  902. }
  903. static ConnResult
  904. 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)
  905. {
  906. int remote_add_allowed = add_remote_entry_allowed(local_entry);
  907. ConnResult return_value = 0;
  908. int rc = 0;
  909. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  910. "%s: process_replay_add: dn=\"%s\" (%s,%s)\n", agmt_get_long_name(prp->agmt),
  911. slapi_sdn_get_dn(remote_dn), missing_entry ? "not present" : "already present",
  912. remote_add_allowed ? "add allowed" : "add not allowed");
  913. if (missing_entry)
  914. {
  915. /* If DN is a GUID, we need to attempt to reanimate the tombstone */
  916. if (is_guid_dn(remote_dn)) {
  917. int tstone_exists = 0;
  918. int reanimate_rc = -1;
  919. char *new_dn_string = NULL;
  920. char *cn_string = NULL;
  921. Slapi_DN *tombstone_dn = NULL;
  922. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  923. "%s: process_replay_add: dn=\"%s\" appears to have been"
  924. " deleted on remote side. Searching for tombstone.\n",
  925. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(remote_dn));
  926. /* Map local entry to tombstone DN and verify that it exists on
  927. * AD side */
  928. map_windows_tombstone_dn(local_entry, &tombstone_dn, prp, &tstone_exists);
  929. /* We can't use a GUID DN, so rewrite to the new mapped DN. */
  930. cn_string = slapi_entry_attr_get_charptr(local_entry,"cn");
  931. if (!cn_string) {
  932. cn_string = slapi_entry_attr_get_charptr(local_entry,"ntuserdomainid");
  933. }
  934. if (cn_string) {
  935. char *container_str = NULL;
  936. const char *suffix = slapi_sdn_get_dn(windows_private_get_windows_subtree(prp->agmt));
  937. container_str = extract_container(slapi_entry_get_sdn_const(local_entry),
  938. windows_private_get_directory_subtree(prp->agmt));
  939. new_dn_string = PR_smprintf("cn=%s,%s%s", cn_string, container_str, suffix);
  940. if (new_dn_string) {
  941. /* If the tombstone exists, reanimate it. If the tombstone
  942. * does not exist, we'll create a new entry in AD, which
  943. * will end up getting a new GUID generated by AD. */
  944. if (tstone_exists) {
  945. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  946. "%s: process_replay_add: Reanimating tombstone (dn=\"%s\") to"
  947. " normal entry (dn=\"%s\").\n", agmt_get_long_name(prp->agmt),
  948. slapi_sdn_get_dn(tombstone_dn), new_dn_string);
  949. reanimate_rc = windows_reanimate_tombstone(prp, tombstone_dn, (const char *)new_dn_string);
  950. if (reanimate_rc != 0) {
  951. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  952. "%s: process_replay_add: Reanimation of tombstone"
  953. " (dn=\"%s\") failed. A new entry (dn=\"%s\")"
  954. " will be added instead.\n", agmt_get_long_name(prp->agmt),
  955. slapi_sdn_get_dn(tombstone_dn), new_dn_string);
  956. }
  957. }
  958. /* Clear out the old GUID DN and use the new one. We hand off the memory
  959. * for new_dn_string into the remote_dn. */
  960. slapi_sdn_done(remote_dn);
  961. slapi_sdn_set_dn_passin(remote_dn, new_dn_string);
  962. }
  963. slapi_ch_free_string(&cn_string);
  964. slapi_ch_free_string(&container_str);
  965. }
  966. if (tombstone_dn) {
  967. slapi_sdn_free(&tombstone_dn);
  968. }
  969. if (reanimate_rc == 0) {
  970. /* We reanimated a tombstone, so an add won't work. We
  971. * fallback to doing a modify of the newly reanimated
  972. * entry. */
  973. goto modify_fallback;
  974. }
  975. }
  976. if (remote_add_allowed) {
  977. LDAPMod **entryattrs = NULL;
  978. Slapi_Entry *mapped_entry = NULL;
  979. /* First map the entry */
  980. rc = windows_create_remote_entry(prp,op->p.p_add.target_entry, remote_dn, &mapped_entry, password);
  981. /* Convert entry to mods */
  982. if (0 == rc && mapped_entry)
  983. {
  984. (void)slapi_entry2mods (mapped_entry , NULL /* &entrydn : We don't need it */, &entryattrs);
  985. slapi_entry_free(mapped_entry);
  986. mapped_entry = NULL;
  987. if (NULL == entryattrs)
  988. {
  989. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  990. "%s: windows_replay_update: Cannot convert entry to LDAPMods.\n",
  991. agmt_get_long_name(prp->agmt));
  992. return_value = CONN_LOCAL_ERROR;
  993. }
  994. else
  995. {
  996. windows_log_add_entry_remote(local_dn, remote_dn);
  997. return_value = windows_conn_send_add(prp->conn, slapi_sdn_get_dn(remote_dn),
  998. entryattrs, NULL, NULL);
  999. /* It's possible that the entry already exists in AD, in which
  1000. * case we fall back to modify it */
  1001. /* NGK - This fallback doesn't seem to happen, at least not at this point
  1002. * in the code. The only chance to fallback to doing a modify is if
  1003. * missing_entry is set to 0 at the top of this function. */
  1004. if (return_value)
  1005. {
  1006. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1007. "%s: windows_replay_update: Cannot replay add operation.\n",
  1008. agmt_get_long_name(prp->agmt));
  1009. }
  1010. ldap_mods_free(entryattrs, 1);
  1011. entryattrs = NULL;
  1012. }
  1013. } else
  1014. {
  1015. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1016. "%s: process_replay_add: failed to create mapped entry dn=\"%s\"\n",
  1017. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(remote_dn));
  1018. }
  1019. }
  1020. } else
  1021. {
  1022. Slapi_Entry *remote_entry;
  1023. modify_fallback:
  1024. remote_entry = NULL;
  1025. /* Fetch the remote entry */
  1026. rc = windows_get_remote_entry(prp, remote_dn,&remote_entry);
  1027. if (0 == rc && remote_entry) {
  1028. return_value = windows_update_remote_entry(prp,remote_entry,local_entry);
  1029. }
  1030. if (remote_entry)
  1031. {
  1032. slapi_entry_free(remote_entry);
  1033. }
  1034. }
  1035. return return_value;
  1036. }
  1037. /*
  1038. * Given a changelog entry, construct the appropriate LDAP operations to sync
  1039. * the operation to AD.
  1040. */
  1041. ConnResult
  1042. windows_replay_update(Private_Repl_Protocol *prp, slapi_operation_parameters *op)
  1043. {
  1044. ConnResult return_value = 0;
  1045. int rc = 0;
  1046. char *password = NULL;
  1047. int is_ours = 0;
  1048. int is_user = 0;
  1049. int is_group = 0;
  1050. Slapi_DN *remote_dn = NULL;
  1051. Slapi_DN *local_dn = NULL;
  1052. Slapi_Entry *local_entry = NULL;
  1053. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_replay_update\n", 0, 0, 0 );
  1054. local_dn = slapi_sdn_new_dn_byref( op->target_address.dn );
  1055. /* Since we have the target uniqueid in the op structure, let's
  1056. * fetch the local entry here using it. We do not want to search
  1057. * across tombstone entries unless we are dealing with a delete
  1058. * operation here since searching across tombstones can be very
  1059. * inefficient as the tombstones build up.
  1060. */
  1061. if (op->operation_type != SLAPI_OPERATION_DELETE) {
  1062. rc = windows_get_local_entry_by_uniqueid(prp, op->target_address.uniqueid, &local_entry);
  1063. } else {
  1064. rc = windows_get_local_tombstone_by_uniqueid(prp, op->target_address.uniqueid, &local_entry);
  1065. }
  1066. if (rc)
  1067. {
  1068. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1069. "%s: windows_replay_update: failed to fetch local entry for %s operation dn=\"%s\"\n",
  1070. agmt_get_long_name(prp->agmt),
  1071. op2string(op->operation_type), op->target_address.dn);
  1072. goto error;
  1073. }
  1074. is_ours = is_subject_of_agreement_local(local_entry, prp->agmt);
  1075. windows_is_local_entry_user_or_group(local_entry,&is_user,&is_group);
  1076. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1077. "%s: windows_replay_update: Looking at %s operation local dn=\"%s\" (%s,%s,%s)\n",
  1078. agmt_get_long_name(prp->agmt),
  1079. op2string(op->operation_type), op->target_address.dn, is_ours ? "ours" : "not ours",
  1080. is_user ? "user" : "not user", is_group ? "group" : "not group");
  1081. if (is_ours && (is_user || is_group) ) {
  1082. int missing_entry = 0;
  1083. /* Make the entry's DN */
  1084. rc = map_entry_dn_outbound(local_entry,&remote_dn,prp,&missing_entry, 1);
  1085. if (rc || NULL == remote_dn)
  1086. {
  1087. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1088. "%s: windows_replay_update: failed map dn for %s operation dn=\"%s\""
  1089. "rc=%d remote_dn = [%s]\n",
  1090. agmt_get_long_name(prp->agmt),
  1091. op2string(op->operation_type), op->target_address.dn,
  1092. rc, remote_dn ? slapi_sdn_get_dn(remote_dn) : "(null)");
  1093. goto error;
  1094. }
  1095. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1096. "%s: windows_replay_update: Processing %s operation local dn=\"%s\" remote dn=\"%s\"\n",
  1097. agmt_get_long_name(prp->agmt),
  1098. op2string(op->operation_type), op->target_address.dn, slapi_sdn_get_dn(remote_dn));
  1099. switch (op->operation_type) {
  1100. /*
  1101. we should check the modify case first and check the list of mods -
  1102. if the magic objectclass (ntuser) and attributes (ntUserCreateNewAccount
  1103. or ntGroupCreateNewAccount) then we should fall through to the ADD case
  1104. since the user wants to add the user to AD - could maybe just change
  1105. process_replay_add slightly, to add the mods list from the modify
  1106. operation - process_replay_add already turns the entry into a mods list
  1107. to pass to the ldap add operation, so it should not be too much more
  1108. trouble to apply the additional mods from the modify operation - we'll
  1109. have to pass in local entry, or perhaps just change the operation from
  1110. modify to an add, and set the op->p.p_add.target_entry to the local_entry
  1111. which gets retrieved above
  1112. */
  1113. case SLAPI_OPERATION_ADD:
  1114. return_value = process_replay_add(prp,op,local_entry,local_dn,remote_dn,is_user,missing_entry,&password);
  1115. break;
  1116. case SLAPI_OPERATION_MODIFY:
  1117. {
  1118. LDAPMod **mapped_mods = NULL;
  1119. windows_map_mods_for_replay(prp,op->p.p_modify.modify_mods, &mapped_mods, is_user, &password);
  1120. if (is_user) {
  1121. winsync_plugin_call_pre_ad_mod_user_mods_cb(prp->agmt,
  1122. windows_private_get_raw_entry(prp->agmt),
  1123. local_dn,
  1124. local_entry,
  1125. op->p.p_modify.modify_mods,
  1126. remote_dn,
  1127. &mapped_mods);
  1128. } else if (is_group) {
  1129. winsync_plugin_call_pre_ad_mod_group_mods_cb(prp->agmt,
  1130. windows_private_get_raw_entry(prp->agmt),
  1131. local_dn,
  1132. local_entry,
  1133. op->p.p_modify.modify_mods,
  1134. remote_dn,
  1135. &mapped_mods);
  1136. }
  1137. /* It's possible that the mapping process results in an empty mod list, in which case we don't bother with the replay */
  1138. if ( mapped_mods == NULL || *(mapped_mods)== NULL )
  1139. {
  1140. return_value = CONN_OPERATION_SUCCESS;
  1141. } else
  1142. {
  1143. if (slapi_is_loglevel_set(SLAPI_LOG_REPL))
  1144. {
  1145. int i = 0;
  1146. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,"dump mods for replay update:");
  1147. for(i=0;mapped_mods[i];i++)
  1148. {
  1149. slapi_mod_dump(mapped_mods[i],i);
  1150. }
  1151. }
  1152. return_value = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(remote_dn), mapped_mods, NULL, NULL /* returned controls */);
  1153. }
  1154. if (mapped_mods)
  1155. {
  1156. ldap_mods_free(mapped_mods,1);
  1157. mapped_mods = NULL;
  1158. }
  1159. }
  1160. break;
  1161. case SLAPI_OPERATION_DELETE:
  1162. if (delete_remote_entry_allowed(local_entry))
  1163. {
  1164. return_value = windows_conn_send_delete(prp->conn, slapi_sdn_get_dn(remote_dn), NULL, NULL /* returned controls */);
  1165. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1166. "%s: windows_replay_update: deleted remote entry, dn=\"%s\", result=%d\n",
  1167. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(remote_dn), return_value);
  1168. } else
  1169. {
  1170. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1171. "%s: windows_replay_update: delete not allowed on remote entry, dn=\"%s\"\n",
  1172. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(remote_dn));
  1173. }
  1174. break;
  1175. case SLAPI_OPERATION_MODRDN:
  1176. return_value = windows_conn_send_rename(prp->conn, op->target_address.dn,
  1177. op->p.p_modrdn.modrdn_newrdn,
  1178. op->p.p_modrdn.modrdn_newsuperior_address.dn,
  1179. op->p.p_modrdn.modrdn_deloldrdn,
  1180. NULL, NULL /* returned controls */);
  1181. break;
  1182. default:
  1183. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name, "%s: replay_update: Unknown "
  1184. "operation type %lu found in changelog - skipping change.\n",
  1185. agmt_get_long_name(prp->agmt), op->operation_type);
  1186. }
  1187. if (password)
  1188. {
  1189. /* We need to have a non-GUID dn in send_password_modify in order to
  1190. * bind as the user to check if we need to send the password change.
  1191. * You are supposed to be able to bind using a GUID dn, but it doesn't
  1192. * seem to work over plain LDAP. */
  1193. if (is_guid_dn(remote_dn)) {
  1194. Slapi_DN *remote_dn_norm = NULL;
  1195. int norm_missing = 0;
  1196. map_entry_dn_outbound(local_entry,&remote_dn_norm,prp,&norm_missing, 0);
  1197. return_value = send_password_modify(remote_dn_norm, password, prp);
  1198. slapi_sdn_free(&remote_dn_norm);
  1199. } else {
  1200. return_value = send_password_modify(remote_dn, password, prp);
  1201. }
  1202. if (return_value)
  1203. {
  1204. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1205. "%s: windows_replay_update: update password returned %d\n",
  1206. agmt_get_long_name(prp->agmt), return_value );
  1207. } else {
  1208. /* If we successfully added an entry, and then subsequently changed
  1209. * its password, THEN we need to change its status in AD in order
  1210. * that it can be used (otherwise the user is marked as disabled).
  1211. * To do this we set this attribute and value:
  1212. * userAccountControl: 512 */
  1213. if (op->operation_type == SLAPI_OPERATION_ADD && missing_entry)
  1214. {
  1215. return_value = send_accountcontrol_modify(remote_dn, prp, missing_entry);
  1216. }
  1217. }
  1218. }
  1219. } else {
  1220. /* We ignore operations that target entries outside of our sync'ed subtree, or which are not Windows users or groups */
  1221. }
  1222. error:
  1223. if (local_entry)
  1224. {
  1225. slapi_entry_free(local_entry);
  1226. }
  1227. if (local_dn)
  1228. {
  1229. slapi_sdn_free (&local_dn);
  1230. }
  1231. if (remote_dn)
  1232. {
  1233. slapi_sdn_free(&remote_dn);
  1234. }
  1235. slapi_ch_free_string(&password);
  1236. return return_value;
  1237. }
  1238. static int
  1239. is_straight_mapped_attr(const char *type, int is_user /* or group */, int is_nt4)
  1240. {
  1241. int found = 0;
  1242. size_t offset = 0;
  1243. char *this_attr = NULL;
  1244. char **list = is_user ? (is_nt4 ? nt4_user_matching_attributes : windows_user_matching_attributes) : (is_nt4 ? nt4_group_matching_attributes : windows_group_matching_attributes);
  1245. /* Look for the type in the list of straight mapped attrs for the appropriate object type */
  1246. while ((this_attr = list[offset]))
  1247. {
  1248. if (0 == slapi_attr_type_cmp(this_attr, type, SLAPI_TYPE_CMP_SUBTYPE))
  1249. {
  1250. found = 1;
  1251. break;
  1252. }
  1253. offset++;
  1254. }
  1255. return found;
  1256. }
  1257. static void
  1258. windows_map_attr_name(const char *original_type , int to_windows, int is_user, int is_create, char **mapped_type, int *map_dn)
  1259. {
  1260. char *new_type = NULL;
  1261. windows_attribute_map *our_map = is_user ? user_attribute_map : group_attribute_map;
  1262. windows_attribute_map *this_map = NULL;
  1263. size_t offset = 0;
  1264. *mapped_type = NULL;
  1265. /* Iterate over the map entries looking for the type we have */
  1266. while((this_map = &(our_map[offset])))
  1267. {
  1268. char *their_name = to_windows ? this_map->windows_attribute_name : this_map->ldap_attribute_name;
  1269. char *our_name = to_windows ? this_map->ldap_attribute_name : this_map->windows_attribute_name;
  1270. if (NULL == their_name)
  1271. {
  1272. /* End of the list */
  1273. break;
  1274. }
  1275. if (0 == slapi_attr_type_cmp(original_type, our_name, SLAPI_TYPE_CMP_SUBTYPE))
  1276. {
  1277. if (!is_create && (this_map->create_type == createonly))
  1278. {
  1279. /* Skip create-only entries if we're not creating */
  1280. } else
  1281. {
  1282. if ( (this_map->map_type == towindowsonly && to_windows) || (this_map->map_type == fromwindowsonly && !to_windows)
  1283. || (this_map->map_type == bidirectional) )
  1284. {
  1285. new_type = slapi_ch_strdup(their_name);
  1286. *map_dn = (this_map->attr_type == dnmap);
  1287. break;
  1288. }
  1289. }
  1290. }
  1291. offset++;
  1292. }
  1293. if (new_type)
  1294. {
  1295. *mapped_type = new_type;
  1296. }
  1297. }
  1298. /*
  1299. * Make a new entry suitable for the sync destination (indicated by the to_windows argument).
  1300. * Returns the new entry ready to be passed to an LDAP ADD operation, either remote or local.
  1301. * Also returns the plaintext value of any password contained in the original entry (only for the
  1302. * to_windows direction). This is because passwords must be added to entries after they are added in AD.
  1303. * Caller must free the new entry and any password returned.
  1304. */
  1305. static int
  1306. windows_create_remote_entry(Private_Repl_Protocol *prp,Slapi_Entry *original_entry, Slapi_DN *remote_sdn, Slapi_Entry **remote_entry, char** password)
  1307. {
  1308. int retval = 0;
  1309. char *entry_string = NULL;
  1310. Slapi_Entry *new_entry = NULL;
  1311. int rc = 0;
  1312. int is_user = 0;
  1313. int is_group = 0;
  1314. Slapi_Attr *attr = NULL;
  1315. char *username = NULL;
  1316. const char *dn_string = NULL;
  1317. char *fqusername = NULL;
  1318. const char *domain_name = windows_private_get_windows_domain(prp->agmt);
  1319. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  1320. char *remote_user_entry_template =
  1321. "dn: %s\n"
  1322. "objectclass:top\n"
  1323. "objectclass:person\n"
  1324. "objectclass:organizationalperson\n"
  1325. "objectclass:user\n"
  1326. "userPrincipalName:%s\n";
  1327. char *remote_group_entry_template =
  1328. "dn: %s\n"
  1329. "objectclass:top\n"
  1330. "objectclass:group\n";
  1331. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_create_remote_entry\n", 0, 0, 0 );
  1332. windows_is_local_entry_user_or_group(original_entry,&is_user,&is_group);
  1333. /* Create a new entry */
  1334. /* Give it its DN and samaccountname */
  1335. username = extract_ntuserdomainid_from_entry(original_entry);
  1336. if (NULL == username)
  1337. {
  1338. goto error;
  1339. }
  1340. fqusername = PR_smprintf("%s@%s",username,domain_name);
  1341. dn_string = slapi_sdn_get_dn(remote_sdn);
  1342. if (is_user)
  1343. {
  1344. entry_string = slapi_ch_smprintf(remote_user_entry_template, dn_string, fqusername);
  1345. } else
  1346. {
  1347. entry_string = slapi_ch_smprintf(remote_group_entry_template, dn_string);
  1348. }
  1349. PR_smprintf_free(fqusername);
  1350. if (NULL == entry_string)
  1351. {
  1352. goto error;
  1353. }
  1354. new_entry = slapi_str2entry(entry_string, 0);
  1355. slapi_ch_free_string(&entry_string);
  1356. if (NULL == new_entry)
  1357. {
  1358. goto error;
  1359. }
  1360. /* Map the appropriate attributes sourced from the remote entry */
  1361. /* Iterate over the local entry's attributes */
  1362. for (rc = slapi_entry_first_attr(original_entry, &attr); rc == 0;
  1363. rc = slapi_entry_next_attr(original_entry, attr, &attr))
  1364. {
  1365. char *type = NULL;
  1366. Slapi_ValueSet *vs = NULL;
  1367. int mapdn = 0;
  1368. slapi_attr_get_type( attr, &type );
  1369. slapi_attr_get_valueset(attr,&vs);
  1370. if ( is_straight_mapped_attr(type,is_user,is_nt4) )
  1371. {
  1372. /* The initials attribute is a special case. AD has a constraint
  1373. * that limits the value length. If we're sending a change to
  1374. * the initials attribute to AD, we trim if neccessary.
  1375. */
  1376. if (0 == slapi_attr_type_cmp(type, "initials", SLAPI_TYPE_CMP_SUBTYPE)) {
  1377. int i = 0;
  1378. const char *initials_value = NULL;
  1379. Slapi_Value *value = NULL;
  1380. i = slapi_valueset_first_value(vs,&value);
  1381. while (i >= 0) {
  1382. initials_value = slapi_value_get_string(value);
  1383. /* If > AD_INITIALS_LENGTH, trim the value */
  1384. if (strlen(initials_value) > AD_INITIALS_LENGTH) {
  1385. char *new_initials = PL_strndup(initials_value, AD_INITIALS_LENGTH);
  1386. /* the below hands off memory */
  1387. slapi_value_set_string_passin(value, new_initials);
  1388. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1389. "%s: windows_create_remote_entry: "
  1390. "Trimming initials attribute to %d characters.\n",
  1391. agmt_get_long_name(prp->agmt), AD_INITIALS_LENGTH);
  1392. }
  1393. i = slapi_valueset_next_value(vs, i, &value);
  1394. }
  1395. }
  1396. /* copy over the attr values */
  1397. slapi_entry_add_valueset(new_entry,type,vs);
  1398. } else
  1399. {
  1400. char *new_type = NULL;
  1401. windows_map_attr_name(type , 1 /* to windows */, is_user, 1 /* create */, &new_type, &mapdn);
  1402. if (new_type)
  1403. {
  1404. if (mapdn)
  1405. {
  1406. Slapi_ValueSet *mapped_values = NULL;
  1407. map_dn_values(prp,vs,&mapped_values, 1 /* to windows */,0);
  1408. if (mapped_values)
  1409. {
  1410. slapi_entry_add_valueset(new_entry,new_type,mapped_values);
  1411. slapi_valueset_free(mapped_values);
  1412. mapped_values = NULL;
  1413. }
  1414. } else
  1415. {
  1416. Slapi_Attr *new_attr = NULL;
  1417. /* AD treats streetAddress as a single-valued attribute, while we define it
  1418. * as a multi-valued attribute as it's defined in rfc 4519. We only
  1419. * sync the first value to AD to avoid a constraint violation.
  1420. */
  1421. if (0 == slapi_attr_type_cmp(new_type, "streetAddress", SLAPI_TYPE_CMP_SUBTYPE)) {
  1422. if (slapi_valueset_count(vs) > 1) {
  1423. int i = 0;
  1424. Slapi_Value *value = NULL;
  1425. Slapi_Value *new_value = NULL;
  1426. i = slapi_valueset_first_value(vs,&value);
  1427. if (i >= 0) {
  1428. /* Dup the first value, trash the valueset, then copy
  1429. * in the dup'd value. */
  1430. new_value = slapi_value_dup(value);
  1431. slapi_valueset_done(vs);
  1432. /* The below hands off the memory to the valueset */
  1433. slapi_valueset_add_value_ext(vs, new_value, SLAPI_VALUE_FLAG_PASSIN);
  1434. }
  1435. }
  1436. }
  1437. slapi_entry_add_valueset(new_entry,type,vs);
  1438. /* Reset the type to new_type here. This is needed since
  1439. * slapi_entry_add_valueset will create the Slapi_Attrs using
  1440. * the schema definition, which can reset the type to something
  1441. * other than the type you pass into it. To be safe, we just
  1442. * create the attributes with the old type, then reset them. */
  1443. if (slapi_entry_attr_find(new_entry, type, &new_attr) == 0) {
  1444. slapi_attr_set_type(new_attr, new_type);
  1445. }
  1446. }
  1447. slapi_ch_free_string(&new_type);
  1448. }
  1449. /* password mods are treated specially */
  1450. if (0 == slapi_attr_type_cmp(type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD, SLAPI_TYPE_CMP_SUBTYPE) )
  1451. {
  1452. const char *password_value = NULL;
  1453. Slapi_Value *value = NULL;
  1454. slapi_valueset_first_value(vs,&value);
  1455. password_value = slapi_value_get_string(value);
  1456. /* We need to check if the first character of password_value is an
  1457. * opening brace since strstr will simply return it's first argument
  1458. * if it is an empty string. */
  1459. if (password_value && (*password_value == '{')) {
  1460. if (strchr( password_value, '}' )) {
  1461. /* A storage scheme is present. Check if it's the
  1462. * clear storage scheme. */
  1463. if ((strlen(password_value) >= PASSWD_CLEAR_PREFIX_LEN + 1) &&
  1464. (strncasecmp(password_value, PASSWD_CLEAR_PREFIX, PASSWD_CLEAR_PREFIX_LEN) == 0)) {
  1465. /* This password is in clear text. Strip off the clear prefix
  1466. * and sync it. */
  1467. *password = slapi_ch_strdup(password_value + PASSWD_CLEAR_PREFIX_LEN);
  1468. } else {
  1469. /* This password is stored in a non-cleartext format.
  1470. * We can only sync cleartext passwords. */
  1471. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1472. "%s: windows_create_remote_entry: "
  1473. "Password is already hashed. Not syncing.\n",
  1474. agmt_get_long_name(prp->agmt));
  1475. }
  1476. } else {
  1477. /* This password doesn't have a storage prefix but
  1478. * just happens to start with the '{' character. We'll
  1479. * assume that it's just a cleartext password without
  1480. * the proper storage prefix. */
  1481. *password = slapi_ch_strdup(password_value);
  1482. }
  1483. } else {
  1484. /* This password has no storage prefix, or the password is empty */
  1485. *password = slapi_ch_strdup(password_value);
  1486. }
  1487. }
  1488. }
  1489. if (vs)
  1490. {
  1491. slapi_valueset_free(vs);
  1492. vs = NULL;
  1493. }
  1494. }
  1495. /* NT4 must have the groupType attribute set for groups. If it is not present, we will
  1496. * add it here with a value of 2 (global group).
  1497. */
  1498. if (is_nt4 && is_group)
  1499. {
  1500. Slapi_Attr *ap = NULL;
  1501. if(slapi_entry_attr_find(new_entry, "groupType", &ap))
  1502. {
  1503. /* groupType attribute wasn't found, so we'll add it */
  1504. slapi_entry_attr_set_int(new_entry, "groupType", 2 /* global group */);
  1505. }
  1506. }
  1507. if (remote_entry)
  1508. {
  1509. *remote_entry = new_entry;
  1510. }
  1511. error:
  1512. if (username)
  1513. {
  1514. slapi_ch_free_string(&username);
  1515. }
  1516. if (new_entry)
  1517. {
  1518. windows_dump_entry("Created new remote entry:\n",new_entry);
  1519. }
  1520. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_create_remote_entry: %d\n", retval, 0, 0 );
  1521. return retval;
  1522. }
  1523. #ifdef FOR_DEBUGGING
  1524. /* the entry has already been translated, so be sure to search for ntuserid
  1525. and not samaccountname or anything else. */
  1526. static Slapi_Entry*
  1527. windows_entry_already_exists(Slapi_Entry *e){
  1528. int rc = 0;
  1529. Slapi_DN *sdn = NULL;
  1530. Slapi_Entry *entry = NULL;
  1531. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_entry_already_exists\n", 0, 0, 0 );
  1532. sdn = slapi_entry_get_sdn(e);
  1533. rc = slapi_search_internal_get_entry( sdn, NULL, &entry, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  1534. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_entry_already_exists\n", 0, 0, 0 );
  1535. if (rc == LDAP_SUCCESS)
  1536. {
  1537. return entry;
  1538. }
  1539. else
  1540. {
  1541. return NULL;
  1542. }
  1543. }
  1544. #endif
  1545. static int
  1546. windows_delete_local_entry(Slapi_DN *sdn){
  1547. Slapi_PBlock *pb = NULL;
  1548. int return_value = 0;
  1549. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_delete_local_entry\n", 0, 0, 0 );
  1550. pb = slapi_pblock_new();
  1551. slapi_delete_internal_set_pb(pb, slapi_sdn_get_dn(sdn), NULL, NULL, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  1552. slapi_delete_internal_pb(pb);
  1553. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  1554. slapi_pblock_destroy(pb);
  1555. if (return_value) {
  1556. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1557. "delete operation on local entry %s returned: %d\n", slapi_sdn_get_dn(sdn), return_value);
  1558. }
  1559. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_delete_local_entry: %d\n", return_value, 0, 0 );
  1560. return return_value;
  1561. }
  1562. static void
  1563. windows_map_mods_for_replay(Private_Repl_Protocol *prp,LDAPMod **original_mods, LDAPMod ***returned_mods, int is_user, char** password)
  1564. {
  1565. Slapi_Mods smods = {0};
  1566. Slapi_Mods mapped_smods = {0};
  1567. LDAPMod *mod = NULL;
  1568. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  1569. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_map_mods_for_replay\n", 0, 0, 0 );
  1570. /* Iterate through the original mods, looking each attribute type up in the maps for either user or group */
  1571. slapi_mods_init_byref(&smods, original_mods);
  1572. slapi_mods_init(&mapped_smods,10);
  1573. mod = slapi_mods_get_first_mod(&smods);
  1574. while(mod)
  1575. {
  1576. char *attr_type = mod->mod_type;
  1577. int mapdn = 0;
  1578. /* Check to see if this attribute is passed through */
  1579. if (is_straight_mapped_attr(attr_type,is_user,is_nt4)) {
  1580. /* The initials attribute is a special case. AD has a constraint
  1581. * that limits the value length. If we're sending a change to
  1582. * the initials attribute to AD, we trim if neccessary.
  1583. */
  1584. if (0 == slapi_attr_type_cmp(attr_type, "initials", SLAPI_TYPE_CMP_SUBTYPE)) {
  1585. int i;
  1586. for (i = 0; mod->mod_bvalues[i] != NULL; i++) {
  1587. /* If > AD_INITIALS_LENGTH, trim the value */
  1588. if (mod->mod_bvalues[i]->bv_len > AD_INITIALS_LENGTH) {
  1589. mod->mod_bvalues[i]->bv_val[AD_INITIALS_LENGTH] = '\0';
  1590. mod->mod_bvalues[i]->bv_len = AD_INITIALS_LENGTH;
  1591. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1592. "%s: windows_map_mods_for_replay: "
  1593. "Trimming initials attribute to %d characters.\n",
  1594. agmt_get_long_name(prp->agmt), AD_INITIALS_LENGTH);
  1595. }
  1596. }
  1597. }
  1598. /* copy over the mod */
  1599. slapi_mods_add_modbvps(&mapped_smods,mod->mod_op,attr_type,mod->mod_bvalues);
  1600. } else
  1601. {
  1602. char *mapped_type = NULL;
  1603. /* Check if this mod has its attribute type mapped */
  1604. windows_map_attr_name(attr_type,1,is_user,0,&mapped_type, &mapdn);
  1605. if (mapped_type)
  1606. {
  1607. /* If so copy over the mod with new type name */
  1608. if (mapdn)
  1609. {
  1610. Slapi_ValueSet *mapped_values = NULL;
  1611. Slapi_ValueSet *vs = NULL;
  1612. Slapi_Mod smod;
  1613. vs = slapi_valueset_new();
  1614. slapi_mod_init_byref(&smod,mod);
  1615. slapi_valueset_set_from_smod(vs, &smod);
  1616. map_dn_values(prp,vs,&mapped_values, 1 /* to windows */,0);
  1617. if (mapped_values)
  1618. {
  1619. slapi_mods_add_mod_values(&mapped_smods,mod->mod_op,mapped_type,valueset_get_valuearray(mapped_values));
  1620. slapi_valueset_free(mapped_values);
  1621. mapped_values = NULL;
  1622. } else
  1623. {
  1624. /* this might be a del: mod, in which case there are no values */
  1625. if (mod->mod_op & LDAP_MOD_DELETE)
  1626. {
  1627. slapi_mods_add_mod_values(&mapped_smods, LDAP_MOD_DELETE, mapped_type, NULL);
  1628. }
  1629. }
  1630. slapi_mod_done(&smod);
  1631. slapi_valueset_free(vs);
  1632. } else
  1633. {
  1634. /* AD treats streetAddress as a single-valued attribute, while we define it
  1635. * as a multi-valued attribute as it's defined in rfc 4519. We only
  1636. * sync the first value to AD to avoid a constraint violation.
  1637. */
  1638. if (0 == slapi_attr_type_cmp(mapped_type, "streetAddress", SLAPI_TYPE_CMP_SUBTYPE)) {
  1639. Slapi_Mod smod;
  1640. slapi_mod_init_byref(&smod,mod);
  1641. /* Check if there is more than one value */
  1642. if (slapi_mod_get_num_values(&smod) > 1) {
  1643. slapi_mod_get_first_value(&smod);
  1644. /* Remove all values except for the first */
  1645. while (slapi_mod_get_next_value(&smod)) {
  1646. /* This modifies the bvalues in the mod itself */
  1647. slapi_mod_remove_value(&smod);
  1648. }
  1649. }
  1650. slapi_mod_done(&smod);
  1651. }
  1652. slapi_mods_add_modbvps(&mapped_smods,mod->mod_op,mapped_type,mod->mod_bvalues);
  1653. }
  1654. slapi_ch_free_string(&mapped_type);
  1655. } else
  1656. {
  1657. /* password mods are treated specially */
  1658. if ((0 == slapi_attr_type_cmp(attr_type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD, SLAPI_TYPE_CMP_SUBTYPE)) &&
  1659. mod && mod->mod_bvalues && mod->mod_bvalues[0] && mod->mod_bvalues[0]->bv_val)
  1660. {
  1661. char *password_value = NULL;
  1662. password_value = mod->mod_bvalues[0]->bv_val;
  1663. /* We need to check if the first character of password_value is an
  1664. * opening brace since strstr will simply return it's first argument
  1665. * if it is an empty string. */
  1666. if (password_value && (*password_value == '{')) {
  1667. if (strchr( password_value, '}' )) {
  1668. /* A storage scheme is present. Check if it's the
  1669. * clear storage scheme. */
  1670. if ((strlen(password_value) >= PASSWD_CLEAR_PREFIX_LEN + 1) &&
  1671. (strncasecmp(password_value, PASSWD_CLEAR_PREFIX, PASSWD_CLEAR_PREFIX_LEN) == 0)) {
  1672. /* This password is in clear text. Strip off the clear prefix
  1673. * and sync it. */
  1674. *password = slapi_ch_strdup(password_value + PASSWD_CLEAR_PREFIX_LEN);
  1675. } else {
  1676. /* This password is stored in a non-cleartext format.
  1677. * We can only sync cleartext passwords. */
  1678. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1679. "%s: windows_create_remote_entry: "
  1680. "Password is already hashed. Not syncing.\n",
  1681. agmt_get_long_name(prp->agmt));
  1682. }
  1683. } else {
  1684. /* This password doesn't have a storage prefix but
  1685. * just happens to start with the '{' character. We'll
  1686. * assume that it's just a cleartext password without
  1687. * the proper storage prefix. */
  1688. *password = slapi_ch_strdup(password_value);
  1689. }
  1690. } else {
  1691. /* This password has no storage prefix, or the password is empty */
  1692. *password = slapi_ch_strdup(password_value);
  1693. }
  1694. }
  1695. }
  1696. }
  1697. /* Otherwise we do not copy this mod at all */
  1698. mod = slapi_mods_get_next_mod(&smods);
  1699. }
  1700. slapi_mods_done (&smods);
  1701. /* Extract the mods for the caller */
  1702. *returned_mods = slapi_mods_get_ldapmods_passout(&mapped_smods);
  1703. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_map_mods_for_replay\n", 0, 0, 0 );
  1704. }
  1705. /* Returns non-zero if the attribute value sets are identical. If you want to
  1706. * compare the entire attribute value, set n to 0. You can compare only the
  1707. * first n characters of the values by passing in the legth as n. */
  1708. static int
  1709. attr_compare_equal(Slapi_Attr *a, Slapi_Attr *b, int n)
  1710. {
  1711. /* For now only handle single values */
  1712. Slapi_Value *va = NULL;
  1713. Slapi_Value *vb = NULL;
  1714. int num_a = 0;
  1715. int num_b = 0;
  1716. int match = 1;
  1717. slapi_attr_get_numvalues(a,&num_a);
  1718. slapi_attr_get_numvalues(b,&num_b);
  1719. if (num_a == num_b) {
  1720. slapi_attr_first_value(a, &va);
  1721. slapi_attr_first_value(b, &vb);
  1722. /* If either val is less than n, then check if the length, then values are
  1723. * equal. If both are n or greater, then only compare the first n chars.
  1724. * If n is 0, then just compare the entire attribute. */
  1725. if ((va->bv.bv_len < n) || (vb->bv.bv_len < n) || (n == 0)) {
  1726. if (va->bv.bv_len == vb->bv.bv_len) {
  1727. if (0 != memcmp(va->bv.bv_val, vb->bv.bv_val, va->bv.bv_len)) {
  1728. match = 0;
  1729. }
  1730. } else {
  1731. match = 0;
  1732. }
  1733. } else if (0 != memcmp(va->bv.bv_val, vb->bv.bv_val, n)) {
  1734. match = 0;
  1735. }
  1736. } else {
  1737. match = 0;
  1738. }
  1739. return match;
  1740. }
  1741. /* Returns non-zero if all of the values of attribute a are contained in attribute b. */
  1742. static int
  1743. attr_compare_present(Slapi_Attr *a, Slapi_Attr *b)
  1744. {
  1745. int match = 1;
  1746. int i = 0;
  1747. Slapi_Value *va = NULL;
  1748. /* Iterate through values in attr a and search for each in attr b */
  1749. for (i = slapi_attr_first_value(a, &va); va && (i != -1);
  1750. i = slapi_attr_next_value(a, i, &va)) {
  1751. if (slapi_attr_value_find(b, slapi_value_get_berval(va)) != 0) {
  1752. /* This value wasn't found, so stop checking for values */
  1753. match = 0;
  1754. break;
  1755. }
  1756. }
  1757. return match;
  1758. }
  1759. /* Helper functions for dirsync result processing */
  1760. /* Is this entry a tombstone ? */
  1761. static int
  1762. is_tombstone(Private_Repl_Protocol *prp, Slapi_Entry *e)
  1763. {
  1764. int retval = 0;
  1765. if ( (slapi_filter_test_simple( e, (Slapi_Filter*)windows_private_get_deleted_filter(prp->agmt) ) == 0) )
  1766. {
  1767. retval = 1;
  1768. }
  1769. return retval;
  1770. }
  1771. #define ENTRY_NOTFOUND -1
  1772. #define ENTRY_NOT_UNIQUE -2
  1773. /* Search for an entry in AD */
  1774. static int
  1775. find_entry_by_attr_value_remote(const char *attribute, const char *value, Slapi_Entry **e, Private_Repl_Protocol *prp)
  1776. {
  1777. int retval = 0;
  1778. ConnResult cres = 0;
  1779. char *filter = NULL;
  1780. const char *searchbase = NULL;
  1781. Slapi_Entry *found_entry = NULL;
  1782. filter = PR_smprintf("(%s=%s)",attribute,value);
  1783. searchbase = slapi_sdn_get_dn(windows_private_get_windows_subtree(prp->agmt));
  1784. cres = windows_search_entry(prp->conn, (char*)searchbase, filter, &found_entry);
  1785. if (cres)
  1786. {
  1787. retval = -1;
  1788. } else
  1789. {
  1790. if (found_entry)
  1791. {
  1792. *e = found_entry;
  1793. }
  1794. }
  1795. if (filter)
  1796. {
  1797. PR_smprintf_free(filter);
  1798. filter = NULL;
  1799. }
  1800. return retval;
  1801. }
  1802. /* Search for an entry in AD by DN */
  1803. static int
  1804. windows_get_remote_entry (Private_Repl_Protocol *prp, const Slapi_DN* remote_dn,Slapi_Entry **remote_entry)
  1805. {
  1806. int retval = 0;
  1807. ConnResult cres = 0;
  1808. char *filter = "(objectclass=*)";
  1809. const char *searchbase = NULL;
  1810. Slapi_Entry *found_entry = NULL;
  1811. searchbase = slapi_sdn_get_dn(remote_dn);
  1812. cres = windows_search_entry(prp->conn, (char*)searchbase, filter, &found_entry);
  1813. if (cres)
  1814. {
  1815. retval = -1;
  1816. } else
  1817. {
  1818. if (found_entry)
  1819. {
  1820. *remote_entry = found_entry;
  1821. }
  1822. }
  1823. return retval;
  1824. }
  1825. /* Search for a tombstone entry in AD by DN */
  1826. static int
  1827. windows_get_remote_tombstone (Private_Repl_Protocol *prp, const Slapi_DN* remote_dn,Slapi_Entry **remote_entry)
  1828. {
  1829. int retval = 0;
  1830. ConnResult cres = 0;
  1831. char *filter = "(objectclass=*)";
  1832. const char *searchbase = NULL;
  1833. Slapi_Entry *found_entry = NULL;
  1834. LDAPControl *server_controls[2];
  1835. /* We need to send the "Return Deleted Objects" control to search
  1836. * for tombstones. */
  1837. slapi_build_control(REPL_RETURN_DELETED_OBJS_CONTROL_OID, NULL, PR_TRUE,
  1838. &server_controls[0]);
  1839. server_controls[1] = NULL;
  1840. searchbase = slapi_sdn_get_dn(remote_dn);
  1841. cres = windows_search_entry_ext(prp->conn, (char*)searchbase, filter,
  1842. &found_entry, server_controls);
  1843. if (cres) {
  1844. retval = -1;
  1845. } else {
  1846. if (found_entry) {
  1847. *remote_entry = found_entry;
  1848. }
  1849. }
  1850. ldap_control_free(server_controls[0]);
  1851. return retval;
  1852. }
  1853. /* Reanimate a tombstone in AD. Returns 0 on success, otherwise you get the
  1854. * LDAP return code from the modify operation. */
  1855. static int
  1856. windows_reanimate_tombstone(Private_Repl_Protocol *prp, const Slapi_DN* tombstone_dn, const char* new_dn)
  1857. {
  1858. int retval = 0;
  1859. LDAPControl *server_controls[2];
  1860. Slapi_Mods smods = {0};
  1861. /* We need to send the "Return Deleted Objects" control to modify
  1862. * tombstone entries. */
  1863. slapi_build_control(REPL_RETURN_DELETED_OBJS_CONTROL_OID, NULL, PR_TRUE,
  1864. &server_controls[0]);
  1865. server_controls[1] = NULL;
  1866. /* To reanimate a tombstone in AD, you need to send a modify
  1867. * operation that does two things. It must remove the isDeleted
  1868. * attribute from the entry and it must modify the DN. This DN
  1869. * does not have to be the same place in the tree that the entry
  1870. * previously existed. */
  1871. slapi_mods_init (&smods, 0);
  1872. slapi_mods_add_mod_values(&smods, LDAP_MOD_DELETE, "isDeleted", NULL);
  1873. slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "distinguishedName", new_dn);
  1874. retval = windows_conn_send_modify(prp->conn, slapi_sdn_get_dn(tombstone_dn),
  1875. slapi_mods_get_ldapmods_byref(&smods), server_controls, NULL );
  1876. slapi_mods_done(&smods);
  1877. ldap_control_free(server_controls[0]);
  1878. return retval;
  1879. }
  1880. static int
  1881. find_entry_by_attr_value(const char *attribute, const char *value, Slapi_Entry **e, const Repl_Agmt *ra)
  1882. {
  1883. Slapi_PBlock *pb = slapi_pblock_new();
  1884. Slapi_Entry **entries = NULL, **ep = NULL;
  1885. Slapi_Entry *entry_found = NULL;
  1886. char *query = NULL;
  1887. int found_or_not = ENTRY_NOTFOUND;
  1888. int rval = 0;
  1889. const char *subtree_dn = NULL;
  1890. int not_unique = 0;
  1891. char *subtree_dn_copy = NULL;
  1892. int scope = LDAP_SCOPE_SUBTREE;
  1893. char **attrs = NULL;
  1894. LDAPControl **server_controls = NULL;
  1895. if (pb == NULL)
  1896. goto done;
  1897. query = slapi_ch_smprintf("(%s=%s)", attribute, value);
  1898. if (query == NULL)
  1899. goto done;
  1900. subtree_dn = slapi_sdn_get_dn(windows_private_get_directory_subtree(ra));
  1901. subtree_dn_copy = slapi_ch_strdup(subtree_dn);
  1902. winsync_plugin_call_pre_ds_search_entry_cb(ra, NULL, &subtree_dn_copy, &scope, &query,
  1903. &attrs, &server_controls);
  1904. slapi_search_internal_set_pb(pb, subtree_dn_copy,
  1905. scope, query, attrs, 0, server_controls, NULL,
  1906. (void *)plugin_get_default_component_id(), 0);
  1907. slapi_search_internal_pb(pb);
  1908. slapi_ch_free_string(&subtree_dn_copy);
  1909. slapi_ch_free_string(&query);
  1910. slapi_ch_array_free(attrs);
  1911. attrs = NULL;
  1912. ldap_controls_free(server_controls);
  1913. server_controls = NULL;
  1914. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rval);
  1915. if (rval != LDAP_SUCCESS)
  1916. {
  1917. goto done;
  1918. }
  1919. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1920. if ((entries == NULL) || (entries[0] == NULL))
  1921. {
  1922. goto done;
  1923. }
  1924. entry_found = entries[0];
  1925. for (ep = entries; *ep; ep++) {
  1926. if (not_unique)
  1927. {
  1928. found_or_not = ENTRY_NOT_UNIQUE;
  1929. }
  1930. not_unique = 1;
  1931. }
  1932. done:
  1933. if (entry_found && (found_or_not != ENTRY_NOT_UNIQUE))
  1934. {
  1935. found_or_not = 0;
  1936. *e = slapi_entry_dup(entry_found);
  1937. }
  1938. if (pb)
  1939. {
  1940. slapi_free_search_results_internal(pb);
  1941. slapi_pblock_destroy(pb);
  1942. }
  1943. return found_or_not;
  1944. }
  1945. static int
  1946. find_entry_by_username(const char *username, Slapi_Entry **e, const Repl_Agmt *ra)
  1947. {
  1948. return find_entry_by_attr_value("ntUserDomainId",username,e,ra);
  1949. }
  1950. /* Find an entry in the local server given its GUID, or return ENTRY_NOTFOUND */
  1951. static int
  1952. find_entry_by_guid(const char *guid, Slapi_Entry **e, const Repl_Agmt *ra)
  1953. {
  1954. return find_entry_by_attr_value("ntUniqueId",guid,e,ra);
  1955. }
  1956. /* Remove dashes from a GUID string. */
  1957. static void
  1958. dedash_guid(char *str)
  1959. {
  1960. char *p = str;
  1961. char c = '\0';
  1962. while ((c = *p))
  1963. {
  1964. if ('-' == c)
  1965. {
  1966. /* Move on down please */
  1967. char *q = p;
  1968. char *r = q + 1;
  1969. while (*r)
  1970. {
  1971. *q++ = *r++;
  1972. }
  1973. *q = '\0';
  1974. }
  1975. p++;
  1976. }
  1977. }
  1978. /* Add dashes into a GUID string. If the guid is not formatted properly,
  1979. * we will free it and set the pointer to NULL. */
  1980. static void
  1981. dash_guid(char **str)
  1982. {
  1983. if (strlen(*str) == NTUNIQUEID_LENGTH) {
  1984. char *p = NULL;
  1985. /* Add extra room for the dashes */
  1986. *str = slapi_ch_realloc(*str, AD_GUID_LENGTH + 1);
  1987. /* GUID needs to be in 8-4-4-4-12 format */
  1988. p = *str + 23;
  1989. memmove(p + 1, *str + 20, 12);
  1990. *p = '-';
  1991. p = *str + 18;
  1992. memmove(p + 1, *str + 16, 4);
  1993. *p = '-';
  1994. p = *str + 13;
  1995. memmove(p + 1, *str + 12, 4);
  1996. *p = '-';
  1997. p = *str + 8;
  1998. memmove(p + 1, *str + 8, 4);
  1999. *p = '-';
  2000. p = *str + 36;
  2001. *p = '\0';
  2002. } else {
  2003. /* This GUID does not appear to be valid */
  2004. slapi_ch_free_string(str);
  2005. }
  2006. }
  2007. /* For reasons not clear, the GUID returned in the tombstone DN is all
  2008. * messed up, like the guy in the movie 'the fly' after he want in the tranporter device */
  2009. static void
  2010. decrypt_guid(char *guid)
  2011. {
  2012. static int decrypt_offsets[] = {6,7,4,5,2,3,0,1,10,11,8,9,14,15,12,13,16,17,18,19,
  2013. 20,21,22,23,24,25,26,27,28,29,30,31};
  2014. char *p = guid;
  2015. int i = 0;
  2016. char *cpy = slapi_ch_strdup(guid);
  2017. while (*p && i < (sizeof(decrypt_offsets)/sizeof(int)))
  2018. {
  2019. *p = cpy[decrypt_offsets[i]];
  2020. p++;
  2021. i++;
  2022. }
  2023. slapi_ch_free_string(&cpy);
  2024. }
  2025. static char*
  2026. extract_guid_from_tombstone_dn(const char *dn)
  2027. {
  2028. char *guid = NULL;
  2029. char *colon_offset = NULL;
  2030. char *comma_offset = NULL;
  2031. /* example DN of tombstone:
  2032. "CN=WDel Userdb1\\\nDEL:551706bc-ecf2-4b38-9284-9a8554171d69,CN=Deleted Objects,DC=magpie,DC=com" */
  2033. /* First find the 'DEL:' */
  2034. colon_offset = strchr(dn,':');
  2035. /* Then scan forward to the next ',' */
  2036. comma_offset = strchr(dn,',');
  2037. /* The characters inbetween are the GUID, copy them to a new string and return to the caller */
  2038. if (comma_offset && colon_offset && comma_offset > colon_offset) {
  2039. guid = slapi_ch_malloc(comma_offset - colon_offset);
  2040. strncpy(guid,colon_offset+1,(comma_offset-colon_offset)-1);
  2041. guid[comma_offset-colon_offset-1] = '\0';
  2042. /* Finally remove the dashes since we don't store them on our side */
  2043. dedash_guid(guid);
  2044. decrypt_guid(guid);
  2045. }
  2046. return guid;
  2047. }
  2048. static char *
  2049. convert_to_hex(Slapi_Value *val)
  2050. {
  2051. int offset = 0;
  2052. const struct berval *bvp = NULL;
  2053. int length = 0;
  2054. char *result = NULL;
  2055. bvp = slapi_value_get_berval(val);
  2056. if (bvp)
  2057. {
  2058. char *new_buffer = NULL;
  2059. length = bvp->bv_len;
  2060. for (offset = 0; offset < length; offset++)
  2061. {
  2062. unsigned char byte = ((unsigned char*)(bvp->bv_val))[offset];
  2063. new_buffer = PR_sprintf_append(new_buffer, "%02x", byte );
  2064. }
  2065. if (new_buffer)
  2066. {
  2067. result = new_buffer;
  2068. }
  2069. }
  2070. return result;
  2071. }
  2072. /* Given a local entry, map it to it's AD tombstone DN. An AD
  2073. * tombstone DN is formatted like:
  2074. *
  2075. * cn=<cn>\0ADEL:<guid>,cn=Deleted Objects,<suffix>
  2076. *
  2077. * This function will allocate a new Slapi_DN. It is up to the
  2078. * caller to free it when they are finished with it. */
  2079. static int
  2080. map_windows_tombstone_dn(Slapi_Entry *e, Slapi_DN **dn, Private_Repl_Protocol *prp, int *exists)
  2081. {
  2082. int rc = 0;
  2083. char *cn = NULL;
  2084. char *guid = NULL;
  2085. const char *suffix = NULL;
  2086. char *tombstone_dn = NULL;
  2087. Slapi_Entry *tombstone = NULL;
  2088. /* Initialize the output values */
  2089. *dn = NULL;
  2090. *exists = 0;
  2091. cn = slapi_entry_attr_get_charptr(e,"cn");
  2092. if (!cn) {
  2093. cn = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  2094. }
  2095. guid = slapi_entry_attr_get_charptr(e,"ntUniqueId");
  2096. if (guid) {
  2097. /* the GUID is in a different form in the tombstone DN, so
  2098. * we need to transform it from the way we store it. */
  2099. decrypt_guid(guid);
  2100. dash_guid(&guid);
  2101. }
  2102. /* The tombstone suffix discards any containers, so we need
  2103. * to trim the DN to only dc components. */
  2104. if ((suffix = slapi_sdn_get_dn(windows_private_get_windows_subtree(prp->agmt)))) {
  2105. /* If this isn't found, it is treated as an error below. */
  2106. suffix = (const char *) PL_strcasestr(suffix,"dc=");
  2107. }
  2108. if (cn && guid && suffix) {
  2109. tombstone_dn = PR_smprintf("cn=%s\\0ADEL:%s,cn=Deleted Objects,%s",
  2110. cn, guid, suffix);
  2111. /* Hand off the memory to the Slapi_DN */
  2112. *dn = slapi_sdn_new_dn_passin(tombstone_dn);
  2113. windows_get_remote_tombstone(prp, *dn, &tombstone);
  2114. if (tombstone) {
  2115. *exists = 1;
  2116. slapi_entry_free(tombstone);
  2117. }
  2118. } else {
  2119. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2120. "%s: map_windows_tombstone_dn: Failed to map dn=\"%s\" "
  2121. "to windows tombstone dn.\n", agmt_get_long_name(prp->agmt),
  2122. slapi_entry_get_dn(e));
  2123. rc = 1;
  2124. }
  2125. slapi_ch_free_string(&cn);
  2126. slapi_ch_free_string(&guid);
  2127. return rc;
  2128. }
  2129. static int is_guid_dn(Slapi_DN *remote_dn)
  2130. {
  2131. if ((remote_dn != NULL) && (strncasecmp("<GUID=",
  2132. slapi_sdn_get_dn(remote_dn), 6) == 0)) {
  2133. return 1;
  2134. } else {
  2135. return 0;
  2136. }
  2137. }
  2138. static char*
  2139. extract_guid_from_entry(Slapi_Entry *e, int is_nt4)
  2140. {
  2141. char *guid = NULL;
  2142. Slapi_Value *val = NULL;
  2143. Slapi_Attr *attr = NULL;
  2144. slapi_entry_attr_find(e, "objectGUID", &attr);
  2145. if (attr)
  2146. {
  2147. slapi_attr_first_value(attr, &val);
  2148. if (val) {
  2149. if (is_nt4)
  2150. {
  2151. guid = slapi_ch_strdup(slapi_value_get_string(val));
  2152. } else
  2153. {
  2154. guid = convert_to_hex(val);
  2155. }
  2156. }
  2157. }
  2158. return guid;
  2159. }
  2160. #ifdef FOR_DEBUGGING
  2161. static void
  2162. extract_guid_from_entry_bv(Slapi_Entry *e, const struct berval **bv)
  2163. {
  2164. Slapi_Value *val = NULL;
  2165. Slapi_Attr *attr = NULL;
  2166. slapi_entry_attr_find(e, "objectGUID", &attr);
  2167. if (attr)
  2168. {
  2169. slapi_attr_first_value(attr, &val);
  2170. if (val) {
  2171. *bv = slapi_value_get_berval(val);
  2172. }
  2173. }
  2174. }
  2175. #endif
  2176. static char*
  2177. extract_username_from_entry(Slapi_Entry *e)
  2178. {
  2179. char *uid = NULL;
  2180. uid = slapi_entry_attr_get_charptr(e,"samAccountName");
  2181. return uid;
  2182. }
  2183. static char*
  2184. extract_ntuserdomainid_from_entry(Slapi_Entry *e)
  2185. {
  2186. char *uid = NULL;
  2187. uid = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  2188. return uid;
  2189. }
  2190. static Slapi_DN *make_dn_from_guid(char *guid, int is_nt4, const char* suffix)
  2191. {
  2192. Slapi_DN *new_dn = NULL;
  2193. char *dn_string = NULL;
  2194. if (guid)
  2195. {
  2196. if (is_nt4)
  2197. {
  2198. dn_string = PR_smprintf("GUID=%s,%s",guid,suffix);
  2199. } else
  2200. {
  2201. dn_string = PR_smprintf("<GUID=%s>",guid);
  2202. }
  2203. new_dn = slapi_sdn_new_dn_byval(dn_string);
  2204. PR_smprintf_free(dn_string);
  2205. }
  2206. /* dn string is now inside the Slapi_DN, and will be freed by its owner */
  2207. return new_dn;
  2208. }
  2209. static char*
  2210. extract_container(const Slapi_DN *entry_dn, const Slapi_DN *suffix_dn)
  2211. {
  2212. char *result = NULL;
  2213. /* First do a scope test to make sure that we weren't passed bogus arguments */
  2214. if (slapi_sdn_scope_test(entry_dn,suffix_dn,LDAP_SCOPE_SUBTREE))
  2215. {
  2216. Slapi_DN parent;
  2217. slapi_sdn_init(&parent);
  2218. /* Find the portion of the entry_dn between the RDN and the suffix */
  2219. /* Start with the parent of the entry DN */
  2220. slapi_sdn_get_parent(entry_dn, &parent);
  2221. /* Iterate finding the parent again until we have the suffix */
  2222. while (0 != slapi_sdn_compare(&parent,suffix_dn))
  2223. {
  2224. Slapi_DN child;
  2225. Slapi_RDN *rdn = slapi_rdn_new();
  2226. char *rdn_type = NULL;
  2227. char *rdn_str = NULL;
  2228. /* Append the current RDN to the new container string */
  2229. slapi_sdn_get_rdn(&parent,rdn);
  2230. slapi_rdn_get_first(rdn, &rdn_type, &rdn_str);
  2231. if (rdn_str)
  2232. {
  2233. result = PR_sprintf_append(result, "%s=%s,", rdn_type,rdn_str );
  2234. }
  2235. /* Don't free this until _after_ we've used the rdn_str */
  2236. slapi_rdn_free(&rdn);
  2237. /* Move to the next successive parent */
  2238. slapi_sdn_init(&child);
  2239. slapi_sdn_copy(&parent,&child);
  2240. slapi_sdn_done(&parent);
  2241. slapi_sdn_get_parent(&child, &parent);
  2242. slapi_sdn_done(&child);
  2243. }
  2244. slapi_sdn_done(&parent);
  2245. }
  2246. /* Always return something */
  2247. if (NULL == result)
  2248. {
  2249. result = slapi_ch_strdup("");
  2250. }
  2251. return result;
  2252. }
  2253. /* Given a non-tombstone entry, return the DN of its peer in AD (whether present or not) */
  2254. static int
  2255. map_entry_dn_outbound(Slapi_Entry *e, Slapi_DN **dn, Private_Repl_Protocol *prp, int *missing_entry, int guid_form)
  2256. {
  2257. int retval = 0;
  2258. char *guid = NULL;
  2259. Slapi_DN *new_dn = NULL;
  2260. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  2261. const char *suffix = slapi_sdn_get_dn(windows_private_get_windows_subtree(prp->agmt));
  2262. /* To find the DN of the peer entry we first look for an ntUniqueId attribute
  2263. * on the local entry. If that's present, we generate a GUID-form DN.
  2264. * If there's no GUID, then we look for an ntUserDomainId attribute
  2265. * on the entry. If that's present we attempt to search for an entry with
  2266. * that samaccountName attribute value in AD. If we don't find any matching
  2267. * entry we generate a new DN using the entry's cn. If later, we find that
  2268. * this entry already exists, we handle that problem at the time. We don't
  2269. * check here. Note: for NT4 we always use ntUserDomainId for the samaccountname rdn, never cn.
  2270. */
  2271. *missing_entry = 0;
  2272. guid = slapi_entry_attr_get_charptr(e,"ntUniqueId");
  2273. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2274. "%s: map_entry_dn_outbound: looking for AD entry for DS "
  2275. "dn=\"%s\" guid=\"%s\"\n",
  2276. agmt_get_long_name(prp->agmt),
  2277. slapi_entry_get_dn_const(e),
  2278. guid ? guid : "(null)");
  2279. if (guid && guid_form)
  2280. {
  2281. int rc = 0;
  2282. Slapi_Entry *remote_entry = NULL;
  2283. new_dn = make_dn_from_guid(guid, is_nt4, suffix);
  2284. slapi_ch_free_string(&guid);
  2285. /* There are certain cases where we will have a GUID, but the entry does not exist in
  2286. * AD. This happens when you delete an entry, then add it back elsewhere in the tree
  2287. * without removing the ntUniqueID attribute. We should verify that the entry really
  2288. * exists in AD. */
  2289. rc = windows_get_remote_entry(prp, new_dn, &remote_entry);
  2290. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2291. "%s: map_entry_dn_outbound: return code %d from search "
  2292. "for AD entry dn=\"%s\" or dn=\"%s\"\n",
  2293. agmt_get_long_name(prp->agmt), rc,
  2294. slapi_sdn_get_dn(new_dn),
  2295. remote_entry ? slapi_entry_get_dn_const(remote_entry) : "(null)");
  2296. if (0 == rc && remote_entry) {
  2297. slapi_entry_free(remote_entry);
  2298. } else {
  2299. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2300. "%s: map_entry_dn_outbound: entry not found - rc %d\n",
  2301. agmt_get_long_name(prp->agmt), rc);
  2302. /* We need to re-write the DN to a non-GUID DN if we're syncing to a
  2303. * Windows 2000 Server since tombstone reanimation is not supported.
  2304. * If we're syncing with Windows 2003 Server, we'll just use the GUID
  2305. * to reanimate the tombstone when processing the add operation. */
  2306. *missing_entry = 1;
  2307. if (!windows_private_get_iswin2k3(prp->agmt)) {
  2308. char *new_dn_string = NULL;
  2309. char *cn_string = NULL;
  2310. /* We can't use a GUID DN, so rewrite to the mapped DN. */
  2311. cn_string = slapi_entry_attr_get_charptr(e,"cn");
  2312. if (!cn_string) {
  2313. cn_string = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  2314. }
  2315. if (cn_string) {
  2316. char *container_str = NULL;
  2317. container_str = extract_container(slapi_entry_get_sdn_const(e),
  2318. windows_private_get_directory_subtree(prp->agmt));
  2319. new_dn_string = PR_smprintf("cn=%s,%s%s", cn_string, container_str, suffix);
  2320. if (new_dn_string) {
  2321. if (new_dn) {
  2322. slapi_sdn_free(&new_dn);
  2323. }
  2324. new_dn = slapi_sdn_new_dn_byval(new_dn_string);
  2325. PR_smprintf_free(new_dn_string);
  2326. }
  2327. slapi_ch_free_string(&cn_string);
  2328. slapi_ch_free_string(&container_str);
  2329. }
  2330. }
  2331. }
  2332. } else
  2333. {
  2334. /* No GUID found, try ntUserDomainId */
  2335. Slapi_Entry *remote_entry = NULL;
  2336. char *username = slapi_entry_attr_get_charptr(e,"ntUserDomainId");
  2337. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2338. "%s: map_entry_dn_outbound: looking for AD entry for DS "
  2339. "dn=\"%s\" username=\"%s\"\n",
  2340. agmt_get_long_name(prp->agmt),
  2341. slapi_entry_get_dn_const(e),
  2342. username ? username : "(null)");
  2343. if (username) {
  2344. retval = find_entry_by_attr_value_remote("samAccountName",username,&remote_entry,prp);
  2345. if (0 == retval && remote_entry)
  2346. {
  2347. /* Get the entry's DN */
  2348. new_dn = slapi_sdn_new();
  2349. slapi_sdn_copy(slapi_entry_get_sdn_const(remote_entry), new_dn);
  2350. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2351. "%s: map_entry_dn_outbound: found AD entry dn=\"%s\"\n",
  2352. agmt_get_long_name(prp->agmt),
  2353. slapi_sdn_get_dn(new_dn));
  2354. } else {
  2355. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2356. "%s: map_entry_dn_outbound: entry not found - rc %d\n",
  2357. agmt_get_long_name(prp->agmt), retval);
  2358. if (0 == retval)
  2359. {
  2360. char *new_dn_string = NULL;
  2361. char *cn_string = NULL;
  2362. *missing_entry = 1;
  2363. /* This means that we failed to find a peer entry */
  2364. /* In that case we need to generate the DN that we want to use */
  2365. /* Generated DN's take the form :
  2366. cn=<cn from local entry>, ... in the case that the local entry has a cn, OR
  2367. cn=<ntuserdomainid attribute value>, ... in the case that the local entry doesn't have a CN
  2368. */
  2369. if (is_nt4)
  2370. {
  2371. cn_string = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  2372. } else
  2373. {
  2374. cn_string = slapi_entry_attr_get_charptr(e,"cn");
  2375. if (!cn_string)
  2376. {
  2377. cn_string = slapi_entry_attr_get_charptr(e,"ntuserdomainid");
  2378. }
  2379. }
  2380. if (cn_string)
  2381. {
  2382. char *rdnstr = NULL;
  2383. char *container_str = NULL;
  2384. container_str = extract_container(slapi_entry_get_sdn_const(e), windows_private_get_directory_subtree(prp->agmt));
  2385. rdnstr = is_nt4 ? "samaccountname=%s,%s%s" : "cn=%s,%s%s";
  2386. new_dn_string = PR_smprintf(rdnstr,cn_string,container_str,suffix);
  2387. if (new_dn_string)
  2388. {
  2389. new_dn = slapi_sdn_new_dn_byval(new_dn_string);
  2390. PR_smprintf_free(new_dn_string);
  2391. }
  2392. slapi_ch_free_string(&cn_string);
  2393. slapi_ch_free_string(&container_str);
  2394. }
  2395. } else
  2396. {
  2397. /* This means that we failed to talk to the AD for some reason, the operation should be re-tried */
  2398. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2399. "%s: map_entry_dn_outbound: failed to fetch entry from AD: dn=\"%s\", err=%d\n",
  2400. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)), retval);
  2401. retval = -1;
  2402. }
  2403. }
  2404. slapi_ch_free_string(&username);
  2405. }
  2406. if (remote_entry)
  2407. {
  2408. slapi_entry_free(remote_entry);
  2409. }
  2410. }
  2411. if (new_dn)
  2412. {
  2413. *dn = new_dn;
  2414. }
  2415. slapi_ch_free_string(&guid);
  2416. return retval;
  2417. }
  2418. /* Given a tombstone entry, return the DN of its peer in this server (if present) */
  2419. static int
  2420. map_tombstone_dn_inbound(Slapi_Entry *e, Slapi_DN **dn, const Repl_Agmt *ra)
  2421. {
  2422. int retval = 0;
  2423. Slapi_DN *new_dn = NULL;
  2424. char *guid = NULL;
  2425. const char *dn_string = NULL;
  2426. Slapi_Entry *matching_entry = NULL;
  2427. /* To map a tombstone's DN we need to first extract the entry's objectGUID from the DN
  2428. * CN=vpdxtAD_07\
  2429. DEL:d4ca4e16-e35b-400d-834a-f02db600f3fa,CN=Deleted Objects,DC=magpie,DC=com
  2430. */
  2431. *dn = NULL;
  2432. dn_string = slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)); /* This is a pointer from inside the sdn, no need to free */
  2433. guid = extract_guid_from_tombstone_dn(dn_string);
  2434. if (guid)
  2435. {
  2436. retval = find_entry_by_guid(guid,&matching_entry,ra);
  2437. if (retval)
  2438. {
  2439. if (ENTRY_NOTFOUND == retval)
  2440. {
  2441. } else
  2442. {
  2443. if (ENTRY_NOT_UNIQUE == retval)
  2444. {
  2445. } else
  2446. {
  2447. /* A real error */
  2448. }
  2449. }
  2450. } else
  2451. {
  2452. /* We found the matching entry : get its DN */
  2453. new_dn = slapi_sdn_dup(slapi_entry_get_sdn_const(matching_entry));
  2454. }
  2455. }
  2456. if (new_dn)
  2457. {
  2458. *dn = new_dn;
  2459. }
  2460. if (guid)
  2461. {
  2462. slapi_ch_free_string(&guid);
  2463. }
  2464. if (matching_entry)
  2465. {
  2466. slapi_entry_free(matching_entry);
  2467. }
  2468. return retval;
  2469. }
  2470. /* Given a non-tombstone entry, return the DN of its peer in this server (whether present or not) */
  2471. static int
  2472. map_entry_dn_inbound(Slapi_Entry *e, Slapi_DN **dn, const Repl_Agmt *ra)
  2473. {
  2474. int retval = 0;
  2475. Slapi_DN *new_dn = NULL;
  2476. char *guid = NULL;
  2477. char *username = NULL;
  2478. Slapi_Entry *matching_entry = NULL;
  2479. int is_user = 0;
  2480. int is_group = 0;
  2481. int is_nt4 = windows_private_get_isnt4(ra);
  2482. /* To map a non-tombstone's DN we need to first try to look it up by GUID.
  2483. * If we do not find it, then we need to generate the DN that it would have if added as a new entry.
  2484. */
  2485. *dn = NULL;
  2486. windows_is_remote_entry_user_or_group(e,&is_user,&is_group);
  2487. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2488. "%s: map_entry_dn_inbound: looking for local entry "
  2489. "matching AD entry [%s]\n",
  2490. agmt_get_long_name(ra),
  2491. slapi_entry_get_dn_const(e));
  2492. guid = extract_guid_from_entry(e, is_nt4);
  2493. if (guid)
  2494. {
  2495. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2496. "%s: map_entry_dn_inbound: looking for local entry "
  2497. "by guid [%s]\n",
  2498. agmt_get_long_name(ra),
  2499. guid);
  2500. retval = find_entry_by_guid(guid,&matching_entry,ra);
  2501. if (retval)
  2502. {
  2503. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2504. "%s: map_entry_dn_inbound: problem looking for guid: %d\n",
  2505. agmt_get_long_name(ra), retval);
  2506. if (ENTRY_NOTFOUND == retval)
  2507. {
  2508. } else
  2509. {
  2510. if (ENTRY_NOT_UNIQUE == retval)
  2511. {
  2512. } else
  2513. {
  2514. /* A real error */
  2515. goto error;
  2516. }
  2517. }
  2518. } else
  2519. {
  2520. /* We found the matching entry : get its DN */
  2521. new_dn = slapi_sdn_dup(slapi_entry_get_sdn_const(matching_entry));
  2522. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2523. "%s: map_entry_dn_inbound: found local entry [%s]\n",
  2524. agmt_get_long_name(ra),
  2525. slapi_sdn_get_dn(new_dn));
  2526. }
  2527. }
  2528. else
  2529. {
  2530. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2531. "%s: map_entry_dn_inbound: AD entry has no guid!\n",
  2532. agmt_get_long_name(ra));
  2533. }
  2534. /* If we failed to lookup by guid, try samaccountname */
  2535. if (NULL == new_dn)
  2536. {
  2537. username = extract_username_from_entry(e);
  2538. if (username) {
  2539. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2540. "%s: map_entry_dn_inbound: looking for local entry "
  2541. "by uid [%s]\n",
  2542. agmt_get_long_name(ra),
  2543. username);
  2544. retval = find_entry_by_username(username,&matching_entry,ra);
  2545. if (retval)
  2546. {
  2547. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2548. "%s: map_entry_dn_inbound: problem looking for username: %d\n",
  2549. agmt_get_long_name(ra), retval);
  2550. if (ENTRY_NOTFOUND == retval)
  2551. {
  2552. } else
  2553. {
  2554. if (ENTRY_NOT_UNIQUE == retval)
  2555. {
  2556. } else
  2557. {
  2558. /* A real error */
  2559. goto error;
  2560. }
  2561. }
  2562. } else
  2563. {
  2564. /* We found the matching entry : get its DN */
  2565. new_dn = slapi_sdn_dup(slapi_entry_get_sdn_const(matching_entry));
  2566. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2567. "%s: map_entry_dn_inbound: found local entry by name [%s]\n",
  2568. agmt_get_long_name(ra),
  2569. slapi_sdn_get_dn(new_dn));
  2570. }
  2571. }
  2572. else
  2573. {
  2574. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2575. "%s: map_entry_dn_inbound: AD entry has no username!\n",
  2576. agmt_get_long_name(ra));
  2577. }
  2578. }
  2579. /* If we couldn't find a matching entry by either method, then we need to invent a new DN */
  2580. if (NULL == new_dn)
  2581. {
  2582. /* The new DN has the form: uid=<samaccountname>,<sync'ed subtree> */
  2583. /* If an entry with this DN already exists, we fail and return no DN
  2584. * this is because we don't want to second-guess what the admin wants here:
  2585. * they may want to associate this existing entry with the peer AD entry,
  2586. * but if they intend that we say they must add the ntDomainUserId attribute to
  2587. * that entry.
  2588. */
  2589. char *new_dn_string = NULL;
  2590. if (username)
  2591. {
  2592. const char *suffix = slapi_sdn_get_dn(windows_private_get_directory_subtree(ra));
  2593. char *container_str = NULL;
  2594. container_str = extract_container(slapi_entry_get_sdn_const(e), windows_private_get_windows_subtree(ra));
  2595. /* Local DNs for users and groups are different */
  2596. if (is_user)
  2597. {
  2598. new_dn_string = PR_smprintf("uid=%s,%s%s",username,container_str,suffix);
  2599. winsync_plugin_call_get_new_ds_user_dn_cb(ra,
  2600. windows_private_get_raw_entry(ra),
  2601. e,
  2602. &new_dn_string,
  2603. windows_private_get_directory_subtree(ra),
  2604. windows_private_get_windows_subtree(ra));
  2605. } else
  2606. {
  2607. new_dn_string = PR_smprintf("cn=%s,%s%s",username,container_str,suffix);
  2608. if (is_group) {
  2609. winsync_plugin_call_get_new_ds_group_dn_cb(ra,
  2610. windows_private_get_raw_entry(ra),
  2611. e,
  2612. &new_dn_string,
  2613. windows_private_get_directory_subtree(ra),
  2614. windows_private_get_windows_subtree(ra));
  2615. }
  2616. }
  2617. new_dn = slapi_sdn_new_dn_byval(new_dn_string);
  2618. PR_smprintf_free(new_dn_string);
  2619. slapi_ch_free_string(&container_str);
  2620. /* Clear any earlier error */
  2621. retval = 0;
  2622. } else
  2623. {
  2624. /* Error, no username */
  2625. }
  2626. }
  2627. if (new_dn)
  2628. {
  2629. *dn = new_dn;
  2630. }
  2631. error:
  2632. if (guid)
  2633. {
  2634. PR_smprintf_free(guid);
  2635. }
  2636. if (matching_entry)
  2637. {
  2638. slapi_entry_free(matching_entry);
  2639. }
  2640. if (username)
  2641. {
  2642. slapi_ch_free_string(&username);
  2643. }
  2644. return retval;
  2645. }
  2646. /* 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
  2647. * and does it have the right attribute values for sync ?)
  2648. */
  2649. static int
  2650. is_subject_of_agreement_local(const Slapi_Entry *local_entry, const Repl_Agmt *ra)
  2651. {
  2652. int retval = 0;
  2653. int is_in_subtree = 0;
  2654. const Slapi_DN *agreement_subtree = NULL;
  2655. /* First test for the sync'ed subtree */
  2656. agreement_subtree = windows_private_get_directory_subtree(ra);
  2657. if (NULL == agreement_subtree)
  2658. {
  2659. goto error;
  2660. }
  2661. is_in_subtree = slapi_sdn_scope_test(slapi_entry_get_sdn_const(local_entry), agreement_subtree, LDAP_SCOPE_SUBTREE);
  2662. if (is_in_subtree)
  2663. {
  2664. /* Next test for the correct kind of entry */
  2665. if (local_entry) {
  2666. if (slapi_filter_test_simple( (Slapi_Entry*)local_entry,
  2667. (Slapi_Filter*)windows_private_get_directory_filter(ra)) == 0)
  2668. {
  2669. retval = 1;
  2670. }
  2671. } else
  2672. {
  2673. /* Error: couldn't find the entry */
  2674. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  2675. "failed to find entry in is_subject_of_agreement_local: %d\n", retval);
  2676. retval = 0;
  2677. }
  2678. }
  2679. error:
  2680. return retval;
  2681. }
  2682. /* 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 ?) */
  2683. static int
  2684. is_subject_of_agreement_remote(Slapi_Entry *e, const Repl_Agmt *ra)
  2685. {
  2686. int retval = 0;
  2687. int is_in_subtree = 0;
  2688. const Slapi_DN *agreement_subtree = NULL;
  2689. /* First test for the sync'ed subtree */
  2690. agreement_subtree = windows_private_get_windows_subtree(ra);
  2691. if (NULL == agreement_subtree)
  2692. {
  2693. goto error;
  2694. }
  2695. is_in_subtree = slapi_sdn_scope_test(slapi_entry_get_sdn_const(e), agreement_subtree, LDAP_SCOPE_SUBTREE);
  2696. if (is_in_subtree)
  2697. {
  2698. retval = 1;
  2699. }
  2700. error:
  2701. return retval;
  2702. }
  2703. static int
  2704. windows_create_local_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,const Slapi_DN* local_sdn)
  2705. {
  2706. int retval = 0;
  2707. char *entry_string = NULL;
  2708. Slapi_Entry *local_entry = NULL;
  2709. Slapi_PBlock* pb = NULL;
  2710. int is_user = 0;
  2711. int is_group = 0;
  2712. char *local_entry_template = NULL;
  2713. char *user_entry_template = NULL;
  2714. char *username = extract_username_from_entry(remote_entry);
  2715. Slapi_Attr *attr = NULL;
  2716. int rc = 0;
  2717. char *guid_str = NULL;
  2718. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  2719. char *local_user_entry_template =
  2720. "dn: %s\n"
  2721. "objectclass:top\n"
  2722. "objectclass:person\n"
  2723. "objectclass:organizationalperson\n"
  2724. "objectclass:inetOrgPerson\n"
  2725. "objectclass:ntUser\n"
  2726. "ntUserDeleteAccount:true\n"
  2727. "uid:%s\n";
  2728. char *local_nt4_user_entry_template =
  2729. "dn: %s\n"
  2730. "objectclass:top\n"
  2731. "objectclass:person\n"
  2732. "objectclass:organizationalperson\n"
  2733. "objectclass:inetOrgPerson\n"
  2734. "objectclass:ntUser\n"
  2735. "ntUserDeleteAccount:true\n"
  2736. "uid:%s\n";
  2737. char *local_group_entry_template =
  2738. "dn: %s\n"
  2739. "objectclass:top\n"
  2740. "objectclass:groupofuniquenames\n"
  2741. "objectclass:ntGroup\n"
  2742. "ntGroupDeleteGroup:true\n"
  2743. "cn:%s\n";
  2744. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_create_local_entry\n", 0, 0, 0 );
  2745. windows_is_remote_entry_user_or_group(remote_entry,&is_user,&is_group);
  2746. user_entry_template = is_nt4 ? local_nt4_user_entry_template : local_user_entry_template;
  2747. local_entry_template = is_user ? user_entry_template : local_group_entry_template;
  2748. /* Create a new entry */
  2749. /* Give it its DN and username */
  2750. entry_string = slapi_ch_smprintf(local_entry_template,slapi_sdn_get_dn(local_sdn),username, username);
  2751. if (NULL == entry_string)
  2752. {
  2753. goto error;
  2754. }
  2755. local_entry = slapi_str2entry(entry_string, 0);
  2756. slapi_ch_free_string(&entry_string);
  2757. if (NULL == local_entry)
  2758. {
  2759. goto error;
  2760. }
  2761. /* Map the appropriate attributes sourced from the remote entry */
  2762. for (rc = slapi_entry_first_attr(remote_entry, &attr); rc == 0;
  2763. rc = slapi_entry_next_attr(remote_entry, attr, &attr))
  2764. {
  2765. char *type = NULL;
  2766. Slapi_ValueSet *vs = NULL;
  2767. int mapdn = 0;
  2768. slapi_attr_get_type( attr, &type );
  2769. slapi_attr_get_valueset(attr,&vs);
  2770. if ( is_straight_mapped_attr(type,is_user,is_nt4) )
  2771. {
  2772. /* copy over the attr values */
  2773. slapi_entry_add_valueset(local_entry,type,vs);
  2774. } else
  2775. {
  2776. char *new_type = NULL;
  2777. windows_map_attr_name(type , 0 /* from windows */, is_user, 1 /* create */, &new_type, &mapdn);
  2778. if (new_type)
  2779. {
  2780. if (mapdn)
  2781. {
  2782. Slapi_ValueSet *mapped_values = NULL;
  2783. map_dn_values(prp,vs,&mapped_values, 0 /* from windows */,0);
  2784. if (mapped_values)
  2785. {
  2786. slapi_entry_add_valueset(local_entry,new_type,mapped_values);
  2787. slapi_valueset_free(mapped_values);
  2788. mapped_values = NULL;
  2789. }
  2790. } else
  2791. {
  2792. slapi_entry_add_valueset(local_entry,new_type,vs);
  2793. }
  2794. slapi_ch_free_string(&new_type);
  2795. }
  2796. }
  2797. if (vs)
  2798. {
  2799. slapi_valueset_free(vs);
  2800. vs = NULL;
  2801. }
  2802. }
  2803. /* Copy over the GUID */
  2804. guid_str = extract_guid_from_entry(remote_entry, is_nt4);
  2805. if (guid_str)
  2806. {
  2807. slapi_entry_add_string(local_entry,"ntUniqueId",guid_str);
  2808. } else
  2809. {
  2810. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2811. "extract_guid_from_entry entry %s failed to extract the guid\n", slapi_sdn_get_dn(local_sdn));
  2812. /* Fatal error : need the guid */
  2813. goto error;
  2814. }
  2815. /* Hack for NT4, which has no surname */
  2816. if (is_nt4 && is_user)
  2817. {
  2818. slapi_entry_add_string(local_entry,"sn",username);
  2819. }
  2820. if (is_user) {
  2821. winsync_plugin_call_pre_ds_add_user_cb(prp->agmt,
  2822. windows_private_get_raw_entry(prp->agmt),
  2823. remote_entry,
  2824. local_entry);
  2825. } else if (is_group) {
  2826. winsync_plugin_call_pre_ds_add_group_cb(prp->agmt,
  2827. windows_private_get_raw_entry(prp->agmt),
  2828. remote_entry,
  2829. local_entry);
  2830. }
  2831. /* Store it */
  2832. windows_dump_entry("Adding new local entry",local_entry);
  2833. pb = slapi_pblock_new();
  2834. slapi_add_entry_internal_set_pb(pb, local_entry, NULL,repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),0);
  2835. slapi_add_internal_pb(pb);
  2836. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &retval);
  2837. if (retval) {
  2838. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2839. "add operation of entry %s returned: %d\n", slapi_sdn_get_dn(local_sdn), retval);
  2840. }
  2841. error:
  2842. slapi_ch_free_string(&guid_str);
  2843. if (pb)
  2844. {
  2845. slapi_pblock_destroy(pb);
  2846. }
  2847. if (username)
  2848. {
  2849. slapi_ch_free_string(&username);
  2850. }
  2851. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_create_local_entry\n", 0, 0, 0 );
  2852. return retval;
  2853. }
  2854. /* Function to generate the correct mods to bring 'local' attribute values into consistency with given 'remote' values */
  2855. /* 'local' and 'remote' in quotes because we actually use this function in both directions */
  2856. /* This function expects the value sets to have been already pruned to exclude values that are not
  2857. * subject to the agreement and present in the peer. */
  2858. static int
  2859. 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)
  2860. {
  2861. int ret = 0;
  2862. int i = 0;
  2863. Slapi_Value *rv = NULL;
  2864. Slapi_Value *lv = NULL;
  2865. /* We need to generate an ADD mod for each entry that is in the remote values but not in the local values */
  2866. /* Iterate over the remote values */
  2867. i = slapi_valueset_first_value(remote_values,&rv);
  2868. while (NULL != rv)
  2869. {
  2870. const char *remote_dn = slapi_value_get_string(rv);
  2871. int value_present_in_local_values = (NULL != slapi_valueset_find(attr, local_values, rv));
  2872. if (!value_present_in_local_values)
  2873. {
  2874. slapi_mods_add_string(smods,LDAP_MOD_ADD,local_type,remote_dn);
  2875. *do_modify = 1;
  2876. }
  2877. i = slapi_valueset_next_value(remote_values,i,&rv);
  2878. }
  2879. /* We need to generate a DEL mod for each entry that is in the local values but not in the remote values */
  2880. /* Iterate over the local values */
  2881. i = slapi_valueset_first_value(local_values,&lv);
  2882. while (NULL != lv)
  2883. {
  2884. const char *local_dn = slapi_value_get_string(lv);
  2885. int value_present_in_remote_values = (NULL != slapi_valueset_find(attr, remote_values, lv));
  2886. if (!value_present_in_remote_values)
  2887. {
  2888. slapi_mods_add_string(smods,LDAP_MOD_DELETE,local_type,local_dn);
  2889. *do_modify = 1;
  2890. }
  2891. i = slapi_valueset_next_value(local_values,i,&lv);
  2892. }
  2893. return ret;
  2894. }
  2895. /* 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. */
  2896. static int
  2897. 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)
  2898. {
  2899. int retval = 0;
  2900. Slapi_Attr *attr = NULL;
  2901. Slapi_Attr *del_attr = NULL;
  2902. int is_user = 0;
  2903. int is_group = 0;
  2904. int rc = 0;
  2905. int is_nt4 = windows_private_get_isnt4(prp->agmt);
  2906. /* Iterate over the attributes on the remote entry, updating the local entry where appropriate */
  2907. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_generate_update_mods\n", 0, 0, 0 );
  2908. *do_modify = 0;
  2909. if (to_windows)
  2910. {
  2911. windows_is_local_entry_user_or_group(remote_entry,&is_user,&is_group);
  2912. } else
  2913. {
  2914. windows_is_remote_entry_user_or_group(remote_entry,&is_user,&is_group);
  2915. }
  2916. for (rc = slapi_entry_first_attr(remote_entry, &attr); rc == 0;
  2917. rc = slapi_entry_next_attr(remote_entry, attr, &attr))
  2918. {
  2919. int is_present_local = 0;
  2920. char *type = NULL;
  2921. Slapi_ValueSet *vs = NULL;
  2922. char *local_type = NULL;
  2923. Slapi_Attr *local_attr = NULL;
  2924. int is_guid = 0;
  2925. int mapdn = 0;
  2926. slapi_attr_get_type( attr, &type );
  2927. slapi_attr_get_valueset(attr,&vs);
  2928. /* First determine what we will do with this attr */
  2929. /* If it's a GUID, we need to take special action */
  2930. if (0 == slapi_attr_type_cmp(type,"objectGuid",SLAPI_TYPE_CMP_SUBTYPE) && !to_windows)
  2931. {
  2932. is_guid = 1;
  2933. local_type = slapi_ch_strdup("ntUniqueId");
  2934. } else
  2935. {
  2936. if ( is_straight_mapped_attr(type,is_user,is_nt4) ) {
  2937. local_type = slapi_ch_strdup(type);
  2938. } else {
  2939. windows_map_attr_name(type , to_windows, is_user, 0 /* not create */, &local_type, &mapdn);
  2940. }
  2941. is_guid = 0;
  2942. }
  2943. if (NULL == local_type)
  2944. {
  2945. /* Means we do not map this attribute */
  2946. if (vs)
  2947. {
  2948. slapi_valueset_free(vs);
  2949. vs = NULL;
  2950. }
  2951. continue;
  2952. }
  2953. if (to_windows && (0 == slapi_attr_type_cmp(local_type, "streetAddress", SLAPI_TYPE_CMP_SUBTYPE))) {
  2954. slapi_entry_attr_find(local_entry,FAKE_STREET_ATTR_NAME,&local_attr);
  2955. } else {
  2956. slapi_entry_attr_find(local_entry,local_type,&local_attr);
  2957. }
  2958. is_present_local = (NULL == local_attr) ? 0 : 1;
  2959. /* Is the attribute present on the local entry ? */
  2960. if (is_present_local && !is_guid)
  2961. {
  2962. if (!mapdn)
  2963. {
  2964. int values_equal = 0;
  2965. /* AD has a legth contraint on the initials attribute,
  2966. * so treat is as a special case. */
  2967. if (0 == slapi_attr_type_cmp(type, "initials", SLAPI_TYPE_CMP_SUBTYPE)) {
  2968. values_equal = attr_compare_equal(attr, local_attr, AD_INITIALS_LENGTH);
  2969. /* If we're getting a streetAddress (a fake attr name is used) from AD, then
  2970. * we just check if the value in AD is present in our entry in DS. In this
  2971. * case, attr is from the AD entry, and local_attr is from the DS entry. */
  2972. } else if (0 == slapi_attr_type_cmp(type, FAKE_STREET_ATTR_NAME, SLAPI_TYPE_CMP_SUBTYPE) && !to_windows) {
  2973. values_equal = attr_compare_present(attr, local_attr);
  2974. /* If we are checking if we should send a street attribute to AD, then
  2975. * we want to first see if the AD entry already contains any street value
  2976. * that is present in the DS entry. In this case, attr is from the DS
  2977. * entry, and local_attr is from the AD entry. */
  2978. } else if ((0 == slapi_attr_type_cmp(type, "street", SLAPI_TYPE_CMP_SUBTYPE) && to_windows)) {
  2979. values_equal = attr_compare_present(local_attr, attr);
  2980. } else {
  2981. /* Compare the entire attribute values */
  2982. values_equal = attr_compare_equal(attr, local_attr, 0);
  2983. }
  2984. /* If it is then we need to replace the local values with the remote values if they are different */
  2985. if (!values_equal)
  2986. {
  2987. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  2988. "windows_generate_update_mods: %s, %s : values are different\n",
  2989. slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry)), local_type);
  2990. if ((0 == slapi_attr_type_cmp(local_type, "streetAddress",
  2991. SLAPI_TYPE_CMP_SUBTYPE) && to_windows)) {
  2992. /* streetAddress is single-valued in AD, so make
  2993. * sure we don't try to send more than one value. */
  2994. if (slapi_valueset_count(vs) > 1) {
  2995. int i = 0;
  2996. Slapi_Value *value = NULL;
  2997. Slapi_Value *new_value = NULL;
  2998. i = slapi_valueset_first_value(vs,&value);
  2999. if (i >= 0) {
  3000. /* Dup the first value, trash the valueset, then copy
  3001. * in the dup'd value. */
  3002. new_value = slapi_value_dup(value);
  3003. slapi_valueset_done(vs);
  3004. /* The below hands off the memory to the valueset */
  3005. slapi_valueset_add_value_ext(vs, new_value, SLAPI_VALUE_FLAG_PASSIN);
  3006. }
  3007. }
  3008. } else if ((0 == slapi_attr_type_cmp(local_type, "initials",
  3009. SLAPI_TYPE_CMP_SUBTYPE) && to_windows)) {
  3010. /* initials is constratined to a max length of
  3011. * 6 characters in AD, so trim the value if
  3012. * needed before sending. */
  3013. int i = 0;
  3014. const char *initials_value = NULL;
  3015. Slapi_Value *value = NULL;
  3016. i = slapi_valueset_first_value(vs,&value);
  3017. while (i >= 0) {
  3018. initials_value = slapi_value_get_string(value);
  3019. /* If > AD_INITIALS_LENGTH, trim the value */
  3020. if (strlen(initials_value) > AD_INITIALS_LENGTH) {
  3021. char *new_initials = PL_strndup(initials_value, AD_INITIALS_LENGTH);
  3022. /* the below hands off memory */
  3023. slapi_value_set_string_passin(value, new_initials);
  3024. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  3025. "%s: windows_generate_update_mods: "
  3026. "Trimming initials attribute to %d characters.\n",
  3027. agmt_get_long_name(prp->agmt), AD_INITIALS_LENGTH);
  3028. }
  3029. i = slapi_valueset_next_value(vs, i, &value);
  3030. }
  3031. }
  3032. slapi_mods_add_mod_values(smods,LDAP_MOD_REPLACE,
  3033. local_type,valueset_get_valuearray(vs));
  3034. *do_modify = 1;
  3035. } else
  3036. {
  3037. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  3038. "windows_generate_update_mods: %s, %s : values are equal\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry)), local_type);
  3039. }
  3040. } else {
  3041. /* A dn-valued attribute : need to take special steps */
  3042. Slapi_ValueSet *mapped_remote_values = NULL;
  3043. /* First map all the DNs to that they're in a consistent domain */
  3044. map_dn_values(prp,vs,&mapped_remote_values, to_windows,0);
  3045. if (mapped_remote_values)
  3046. {
  3047. Slapi_ValueSet *local_values = NULL;
  3048. Slapi_ValueSet *restricted_local_values = NULL;
  3049. /* Now do a compare on the values, generating mods to bring them into consistency (if any) */
  3050. /* We ignore any DNs that are outside the scope of the agreement (on both sides) */
  3051. slapi_attr_get_valueset(local_attr,&local_values);
  3052. map_dn_values(prp,local_values,&restricted_local_values,!to_windows,1);
  3053. if (restricted_local_values)
  3054. {
  3055. windows_generate_dn_value_mods(local_type,local_attr,smods,mapped_remote_values,restricted_local_values,do_modify);
  3056. slapi_valueset_free(restricted_local_values);
  3057. restricted_local_values = NULL;
  3058. }
  3059. slapi_valueset_free(mapped_remote_values);
  3060. mapped_remote_values = NULL;
  3061. if (local_values)
  3062. {
  3063. slapi_valueset_free(local_values);
  3064. local_values = NULL;
  3065. }
  3066. }
  3067. }
  3068. } else
  3069. {
  3070. if (!is_present_local)
  3071. {
  3072. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  3073. "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);
  3074. /* If it is currently absent, then we add the value from the remote entry */
  3075. if (is_guid)
  3076. {
  3077. /* Translate the guid value */
  3078. char *guid = extract_guid_from_entry(remote_entry, is_nt4);
  3079. if (guid)
  3080. {
  3081. slapi_mods_add_string(smods,LDAP_MOD_ADD,local_type,guid);
  3082. slapi_ch_free_string(&guid);
  3083. }
  3084. } else
  3085. {
  3086. /* Handle DN valued attributes here */
  3087. if (mapdn)
  3088. {
  3089. Slapi_ValueSet *mapped_values = NULL;
  3090. map_dn_values(prp,vs,&mapped_values, to_windows,0);
  3091. if (mapped_values)
  3092. {
  3093. slapi_mods_add_mod_values(smods,LDAP_MOD_ADD,local_type,valueset_get_valuearray(mapped_values));
  3094. slapi_valueset_free(mapped_values);
  3095. mapped_values = NULL;
  3096. }
  3097. } else
  3098. {
  3099. if ((0 == slapi_attr_type_cmp(local_type, "streetAddress",
  3100. SLAPI_TYPE_CMP_SUBTYPE) && to_windows)) {
  3101. /* streetAddress is single-valued in AD, so make
  3102. * sure we don't try to send more than one value. */
  3103. if (slapi_valueset_count(vs) > 1) {
  3104. int i = 0;
  3105. Slapi_Value *value = NULL;
  3106. Slapi_Value *new_value = NULL;
  3107. i = slapi_valueset_first_value(vs,&value);
  3108. if (i >= 0) {
  3109. /* Dup the first value, trash the valueset, then copy
  3110. * in the dup'd value. */
  3111. new_value = slapi_value_dup(value);
  3112. slapi_valueset_done(vs);
  3113. /* The below hands off the memory to the valueset */
  3114. slapi_valueset_add_value_ext(vs, new_value, SLAPI_VALUE_FLAG_PASSIN);
  3115. }
  3116. }
  3117. } else if ((0 == slapi_attr_type_cmp(local_type, "initials",
  3118. SLAPI_TYPE_CMP_SUBTYPE) && to_windows)) {
  3119. /* initials is constratined to a max length of
  3120. * 6 characters in AD, so trim the value if
  3121. * needed before sending. */
  3122. int i = 0;
  3123. const char *initials_value = NULL;
  3124. Slapi_Value *value = NULL;
  3125. i = slapi_valueset_first_value(vs,&value);
  3126. while (i >= 0) {
  3127. initials_value = slapi_value_get_string(value);
  3128. /* If > AD_INITIALS_LENGTH, trim the value */
  3129. if (strlen(initials_value) > AD_INITIALS_LENGTH) {
  3130. char *new_initials = PL_strndup(initials_value, AD_INITIALS_LENGTH);
  3131. /* the below hands off memory */
  3132. slapi_value_set_string_passin(value, new_initials);
  3133. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  3134. "%s: windows_generate_update_mods: "
  3135. "Trimming initials attribute to %d characters.\n",
  3136. agmt_get_long_name(prp->agmt), AD_INITIALS_LENGTH);
  3137. }
  3138. i = slapi_valueset_next_value(vs, i, &value);
  3139. }
  3140. }
  3141. slapi_mods_add_mod_values(smods,LDAP_MOD_ADD,local_type,valueset_get_valuearray(vs));
  3142. }
  3143. }
  3144. *do_modify = 1;
  3145. }
  3146. }
  3147. if (vs)
  3148. {
  3149. slapi_valueset_free(vs);
  3150. vs = NULL;
  3151. }
  3152. slapi_ch_free_string(&local_type);
  3153. }
  3154. /* Check if any attributes were deleted from the remote entry */
  3155. entry_first_deleted_attribute(remote_entry, &del_attr);
  3156. while (del_attr != NULL) {
  3157. Slapi_Attr *local_attr = NULL;
  3158. char *type = NULL;
  3159. char *local_type = NULL;
  3160. int mapdn = 0;
  3161. /* Map remote type to local type */
  3162. slapi_attr_get_type(del_attr, &type);
  3163. if ( is_straight_mapped_attr(type,is_user,is_nt4) ) {
  3164. local_type = slapi_ch_strdup(type);
  3165. } else {
  3166. windows_map_attr_name(type , to_windows, is_user, 0 /* not create */, &local_type, &mapdn);
  3167. }
  3168. /* Check if this attr exists in the local entry */
  3169. if (local_type) {
  3170. slapi_entry_attr_find(local_entry, local_type, &local_attr);
  3171. if (local_attr) {
  3172. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  3173. "windows_generate_update_mods: deleting %s attribute from local entry\n", local_type);
  3174. /* Delete this attr from the local entry */
  3175. slapi_mods_add_mod_values(smods, LDAP_MOD_DELETE, local_type, NULL);
  3176. *do_modify = 1;
  3177. }
  3178. }
  3179. entry_next_deleted_attribute(remote_entry, &del_attr);
  3180. slapi_ch_free_string(&local_type);
  3181. }
  3182. if (to_windows) {
  3183. if (is_user) {
  3184. winsync_plugin_call_pre_ad_mod_user_cb(prp->agmt,
  3185. windows_private_get_raw_entry(prp->agmt),
  3186. local_entry, /* the cooked ad entry */
  3187. remote_entry, /* the ds entry */
  3188. smods,
  3189. do_modify);
  3190. } else if (is_group) {
  3191. winsync_plugin_call_pre_ad_mod_group_cb(prp->agmt,
  3192. windows_private_get_raw_entry(prp->agmt),
  3193. local_entry, /* the cooked ad entry */
  3194. remote_entry, /* the ds entry */
  3195. smods,
  3196. do_modify);
  3197. }
  3198. } else {
  3199. if (is_user) {
  3200. winsync_plugin_call_pre_ds_mod_user_cb(prp->agmt,
  3201. windows_private_get_raw_entry(prp->agmt),
  3202. remote_entry, /* the cooked ad entry */
  3203. local_entry, /* the ds entry */
  3204. smods,
  3205. do_modify);
  3206. } else if (is_group) {
  3207. winsync_plugin_call_pre_ds_mod_group_cb(prp->agmt,
  3208. windows_private_get_raw_entry(prp->agmt),
  3209. remote_entry, /* the cooked ad entry */
  3210. local_entry, /* the ds entry */
  3211. smods,
  3212. do_modify);
  3213. }
  3214. }
  3215. if (slapi_is_loglevel_set(SLAPI_LOG_REPL) && *do_modify)
  3216. {
  3217. slapi_mods_dump(smods,"windows sync");
  3218. }
  3219. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_generate_update_mods: %d\n", retval, 0, 0 );
  3220. return retval;
  3221. }
  3222. static int
  3223. windows_update_remote_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,Slapi_Entry *local_entry)
  3224. {
  3225. Slapi_Mods smods = {0};
  3226. int retval = 0;
  3227. int do_modify = 0;
  3228. slapi_mods_init (&smods, 0);
  3229. retval = windows_generate_update_mods(prp,local_entry,remote_entry,1,&smods,&do_modify);
  3230. /* Now perform the modify if we need to */
  3231. if (0 == retval && do_modify)
  3232. {
  3233. char dnbuf[BUFSIZ];
  3234. const char *dn = slapi_sdn_get_dn(slapi_entry_get_sdn_const(remote_entry));
  3235. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  3236. "windows_update_remote_entry: modifying entry %s\n", escape_string(dn, dnbuf));
  3237. 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);
  3238. } else
  3239. {
  3240. char dnbuf[BUFSIZ];
  3241. const char *dn = slapi_sdn_get_dn(slapi_entry_get_sdn_const(remote_entry));
  3242. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  3243. "no mods generated for remote entry: %s\n", escape_string(dn, dnbuf));
  3244. }
  3245. slapi_mods_done(&smods);
  3246. return retval;
  3247. }
  3248. static int
  3249. windows_update_local_entry(Private_Repl_Protocol *prp,Slapi_Entry *remote_entry,Slapi_Entry *local_entry)
  3250. {
  3251. Slapi_Mods smods = {0};
  3252. int retval = 0;
  3253. Slapi_PBlock *pb = NULL;
  3254. int do_modify = 0;
  3255. slapi_mods_init (&smods, 0);
  3256. retval = windows_generate_update_mods(prp,remote_entry,local_entry,0,&smods,&do_modify);
  3257. /* Now perform the modify if we need to */
  3258. if (0 == retval && do_modify)
  3259. {
  3260. int rc = 0;
  3261. pb = slapi_pblock_new();
  3262. if (pb)
  3263. {
  3264. char dnbuf[BUFSIZ];
  3265. const char *dn = slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry));
  3266. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  3267. "modifying entry: %s\n", escape_string(dn, dnbuf));
  3268. slapi_modify_internal_set_pb (pb, slapi_entry_get_ndn(local_entry), slapi_mods_get_ldapmods_byref(&smods), NULL, NULL,
  3269. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  3270. slapi_modify_internal_pb (pb);
  3271. slapi_pblock_get (pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3272. if (rc)
  3273. {
  3274. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  3275. "windows_update_local_entry: failed to modify entry %s\n", escape_string(dn, dnbuf));
  3276. }
  3277. slapi_pblock_destroy(pb);
  3278. } else
  3279. {
  3280. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  3281. "failed to make pb in windows_update_local_entry\n");
  3282. }
  3283. } else
  3284. {
  3285. char dnbuf[BUFSIZ];
  3286. const char *dn = slapi_sdn_get_dn(slapi_entry_get_sdn_const(local_entry));
  3287. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  3288. "no mods generated for local entry: %s\n", escape_string(dn, dnbuf));
  3289. }
  3290. slapi_mods_done(&smods);
  3291. return retval;
  3292. }
  3293. static int
  3294. windows_process_total_add(Private_Repl_Protocol *prp,Slapi_Entry *e, Slapi_DN* remote_dn, int missing_entry)
  3295. {
  3296. int retval = 0;
  3297. LDAPMod **entryattrs = NULL;
  3298. Slapi_Entry *mapped_entry = NULL;
  3299. char *password = NULL;
  3300. const Slapi_DN* local_dn = NULL;
  3301. int can_add = winsync_plugin_call_can_add_entry_to_ad_cb(prp->agmt, e, remote_dn);
  3302. /* First map the entry */
  3303. local_dn = slapi_entry_get_sdn_const(e);
  3304. if (missing_entry) {
  3305. if (can_add) {
  3306. retval = windows_create_remote_entry(prp, e, remote_dn, &mapped_entry, &password);
  3307. } else {
  3308. return retval; /* cannot add and no entry to modify */
  3309. }
  3310. }
  3311. /* Convert entry to mods */
  3312. if (0 == retval && mapped_entry)
  3313. {
  3314. (void)slapi_entry2mods (mapped_entry , NULL /* &entrydn : We don't need it */, &entryattrs);
  3315. slapi_entry_free(mapped_entry);
  3316. mapped_entry = NULL;
  3317. if (NULL == entryattrs)
  3318. {
  3319. 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));
  3320. retval = CONN_LOCAL_ERROR;
  3321. }
  3322. else
  3323. {
  3324. windows_log_add_entry_remote(local_dn, remote_dn);
  3325. retval = windows_conn_send_add(prp->conn, slapi_sdn_get_dn(remote_dn), entryattrs, NULL, NULL /* returned controls */);
  3326. /* It's possible that the entry already exists in AD, in which case we fall back to modify it */
  3327. if (retval)
  3328. {
  3329. 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));
  3330. }
  3331. ldap_mods_free(entryattrs, 1);
  3332. entryattrs = NULL;
  3333. if (retval == 0) { /* set the account control bits */
  3334. retval = send_accountcontrol_modify(remote_dn, prp, missing_entry);
  3335. }
  3336. }
  3337. } else
  3338. {
  3339. /* Entry already exists, need to mod it instead */
  3340. Slapi_Entry *remote_entry = NULL;
  3341. /* Get the remote entry */
  3342. retval = windows_get_remote_entry(prp, remote_dn,&remote_entry);
  3343. if (0 == retval && remote_entry)
  3344. {
  3345. retval = windows_update_remote_entry(prp,remote_entry,e);
  3346. /* Detect the case where the error is benign */
  3347. if (retval)
  3348. {
  3349. int operation = 0;
  3350. int error = 0;
  3351. conn_get_error(prp->conn, &operation, &error);
  3352. if (windows_ignore_error_and_keep_going(error))
  3353. {
  3354. retval = CONN_OPERATION_SUCCESS;
  3355. }
  3356. }
  3357. }
  3358. if (remote_entry)
  3359. {
  3360. slapi_entry_free(remote_entry);
  3361. }
  3362. }
  3363. slapi_ch_free_string(&password);
  3364. return retval;
  3365. }
  3366. /* Entry point for the total protocol */
  3367. int windows_process_total_entry(Private_Repl_Protocol *prp,Slapi_Entry *e)
  3368. {
  3369. int retval = 0;
  3370. int is_ours = 0;
  3371. Slapi_DN *remote_dn = NULL;
  3372. int missing_entry = 0;
  3373. const Slapi_DN *local_dn = slapi_entry_get_sdn_const(e);
  3374. /* First check if the entry is for us */
  3375. is_ours = is_subject_of_agreement_local(e, prp->agmt);
  3376. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  3377. "%s: windows_process_total_entry: Looking dn=\"%s\" (%s)\n",
  3378. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)), is_ours ? "ours" : "not ours");
  3379. if (is_ours)
  3380. {
  3381. retval = map_entry_dn_outbound(e,&remote_dn,prp,&missing_entry,0 /* we don't want the GUID */);
  3382. if (retval || NULL == remote_dn)
  3383. {
  3384. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  3385. "%s: windows_replay_update: failed map dn for total update dn=\"%s\"\n",
  3386. agmt_get_long_name(prp->agmt), slapi_sdn_get_dn(local_dn));
  3387. goto error;
  3388. }
  3389. retval = windows_process_total_add(prp,e,remote_dn,missing_entry);
  3390. }
  3391. if (remote_dn)
  3392. {
  3393. slapi_sdn_free(&remote_dn);
  3394. }
  3395. error:
  3396. return retval;
  3397. }
  3398. static int
  3399. windows_search_local_entry_by_uniqueid(Private_Repl_Protocol *prp, const char *uniqueid, char ** attrs, Slapi_Entry **ret_entry, int tombstone, void * component_identity)
  3400. {
  3401. Slapi_Entry **entries = NULL;
  3402. Slapi_PBlock *int_search_pb = NULL;
  3403. int rc = 0;
  3404. char *filter_string = NULL;
  3405. const Slapi_DN *local_subtree = NULL;
  3406. *ret_entry = NULL;
  3407. local_subtree = windows_private_get_directory_subtree(prp->agmt);
  3408. /* Searching for tombstones can be expensive, so the caller needs to specify if
  3409. * we should search for a tombstone entry, or a normal entry. */
  3410. if (tombstone) {
  3411. filter_string = PR_smprintf("(&(objectclass=nsTombstone)(nsUniqueid=%s))", uniqueid);
  3412. } else {
  3413. filter_string = PR_smprintf("(&(|(objectclass=*)(objectclass=ldapsubentry))(nsUniqueid=%s))",uniqueid);
  3414. }
  3415. int_search_pb = slapi_pblock_new ();
  3416. slapi_search_internal_set_pb ( int_search_pb, slapi_sdn_get_dn(local_subtree), LDAP_SCOPE_SUBTREE, filter_string,
  3417. attrs ,
  3418. 0 /* attrsonly */, NULL /* controls */,
  3419. NULL /* uniqueid */,
  3420. component_identity, 0 /* actions */ );
  3421. slapi_search_internal_pb ( int_search_pb );
  3422. slapi_pblock_get( int_search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc );
  3423. if ( LDAP_SUCCESS == rc ) {
  3424. slapi_pblock_get( int_search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
  3425. if ( NULL != entries && NULL != entries[ 0 ]) {
  3426. Slapi_Entry *temp_entry = NULL;
  3427. temp_entry = entries[ 0 ];
  3428. *ret_entry = slapi_entry_dup(temp_entry);
  3429. } else {
  3430. /* No entry there */
  3431. rc = LDAP_NO_SUCH_OBJECT;
  3432. }
  3433. }
  3434. slapi_free_search_results_internal(int_search_pb);
  3435. slapi_pblock_destroy(int_search_pb);
  3436. int_search_pb = NULL;
  3437. if (filter_string)
  3438. {
  3439. PR_smprintf_free(filter_string);
  3440. }
  3441. return rc;
  3442. }
  3443. static int
  3444. windows_get_local_entry_by_uniqueid(Private_Repl_Protocol *prp,const char* uniqueid,Slapi_Entry **local_entry)
  3445. {
  3446. int retval = ENTRY_NOTFOUND;
  3447. Slapi_Entry *new_entry = NULL;
  3448. windows_search_local_entry_by_uniqueid( prp, uniqueid, NULL, &new_entry, 0, /* Don't search tombstones */
  3449. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  3450. if (new_entry)
  3451. {
  3452. *local_entry = new_entry;
  3453. retval = 0;
  3454. }
  3455. return retval;
  3456. }
  3457. static int
  3458. windows_get_local_tombstone_by_uniqueid(Private_Repl_Protocol *prp,const char* uniqueid,Slapi_Entry **local_entry)
  3459. {
  3460. int retval = ENTRY_NOTFOUND;
  3461. Slapi_Entry *new_entry = NULL;
  3462. windows_search_local_entry_by_uniqueid( prp, uniqueid, NULL, &new_entry, 1, /* Search for tombstones */
  3463. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  3464. if (new_entry)
  3465. {
  3466. *local_entry = new_entry;
  3467. retval = 0;
  3468. }
  3469. return retval;
  3470. }
  3471. static int
  3472. windows_get_local_entry(const Slapi_DN* local_dn,Slapi_Entry **local_entry)
  3473. {
  3474. int retval = ENTRY_NOTFOUND;
  3475. Slapi_Entry *new_entry = NULL;
  3476. slapi_search_internal_get_entry( (Slapi_DN*)local_dn, NULL, &new_entry,
  3477. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  3478. if (new_entry)
  3479. {
  3480. *local_entry = new_entry;
  3481. retval = 0;
  3482. }
  3483. return retval;
  3484. }
  3485. static int
  3486. windows_process_dirsync_entry(Private_Repl_Protocol *prp,Slapi_Entry *e, int is_total)
  3487. {
  3488. Slapi_DN* local_sdn = NULL;
  3489. int rc = 0;
  3490. /* deleted users are outside the 'correct container'.
  3491. They live in cn=deleted objects, windows_private_get_directory_subtree( prp->agmt) */
  3492. if (is_tombstone(prp, e))
  3493. {
  3494. rc = map_tombstone_dn_inbound(e, &local_sdn, prp->agmt);
  3495. if ((0 == rc) && local_sdn)
  3496. {
  3497. /* Go ahead and delte the local peer */
  3498. rc = windows_delete_local_entry(local_sdn);
  3499. slapi_sdn_free(&local_sdn);
  3500. } else
  3501. {
  3502. 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));
  3503. }
  3504. } else
  3505. {
  3506. /* Is this entry one we should be interested in ? */
  3507. if (is_subject_of_agreement_remote(e,prp->agmt))
  3508. {
  3509. /* First make its local DN */
  3510. rc = map_entry_dn_inbound(e, &local_sdn, prp->agmt);
  3511. if ((0 == rc) && local_sdn)
  3512. {
  3513. Slapi_Entry *local_entry = NULL;
  3514. /* Get the local entry if it exists */
  3515. rc = windows_get_local_entry(local_sdn,&local_entry);
  3516. if ((0 == rc) && local_entry)
  3517. {
  3518. /* Since the entry exists we should now make it match the entry we received from AD */
  3519. /* Actually we are better off simply fetching the entire entry from AD and using that
  3520. * because otherwise we don't get all the attributes we need to make sense of it such as
  3521. * objectclass */
  3522. Slapi_Entry *remote_entry = NULL;
  3523. windows_get_remote_entry(prp,slapi_entry_get_sdn_const(e),&remote_entry);
  3524. if (remote_entry)
  3525. {
  3526. /* We need to check for any deleted attrs from the dirsync entry
  3527. * and pass them into the newly fetched remote entry. */
  3528. Slapi_Attr *attr = NULL;
  3529. Slapi_Attr *rem_attr = NULL;
  3530. entry_first_deleted_attribute(e, &attr);
  3531. while (attr != NULL) {
  3532. /* We need to dup the attr and add it to the remote entry.
  3533. * rem_attr is now owned by remote_entry, so don't free it */
  3534. rem_attr = slapi_attr_dup(attr);
  3535. if (rem_attr) {
  3536. entry_add_deleted_attribute_wsi(remote_entry, rem_attr);
  3537. rem_attr = NULL;
  3538. }
  3539. entry_next_deleted_attribute(e, &attr);
  3540. }
  3541. rc = windows_update_local_entry(prp, remote_entry, local_entry);
  3542. slapi_entry_free(remote_entry);
  3543. remote_entry = NULL;
  3544. } else
  3545. {
  3546. 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));
  3547. }
  3548. slapi_entry_free(local_entry);
  3549. if (rc) {
  3550. /* Something bad happened */
  3551. 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),
  3552. slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)));
  3553. }
  3554. } else
  3555. {
  3556. /* If it doesn't exist, try to make it */
  3557. if (add_local_entry_allowed(prp,e))
  3558. {
  3559. windows_create_local_entry(prp,e,local_sdn);
  3560. } else
  3561. {
  3562. 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)
  3563. , slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)));
  3564. }
  3565. }
  3566. slapi_sdn_free(&local_sdn);
  3567. } else
  3568. {
  3569. /* We should have been able to map the DN, so this is an error */
  3570. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  3571. "%s: windows_process_dirsync_entry: failed to map "
  3572. "inbound entry %s - rc is %d dn is [%s].\n",
  3573. agmt_get_long_name(prp->agmt),
  3574. slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)),
  3575. rc,
  3576. local_sdn ? slapi_sdn_get_dn(local_sdn) : "null");
  3577. }
  3578. } /* subject of agreement */
  3579. } /* is tombstone */
  3580. return rc;
  3581. }
  3582. void
  3583. windows_dirsync_inc_run(Private_Repl_Protocol *prp)
  3584. {
  3585. int rc = 0;
  3586. int done = 0;
  3587. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_dirsync_inc_run\n", 0, 0, 0 );
  3588. while (!done) {
  3589. Slapi_Entry *e = NULL;
  3590. rc = send_dirsync_search(prp->conn);
  3591. if (rc != CONN_OPERATION_SUCCESS)
  3592. {
  3593. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  3594. "failed to send dirsync search request: %d\n", rc);
  3595. goto error;
  3596. }
  3597. while ( (e = windows_conn_get_search_result(prp->conn) ) != NULL)
  3598. {
  3599. rc = windows_process_dirsync_entry(prp,e,0);
  3600. if (e)
  3601. {
  3602. slapi_entry_free(e);
  3603. }
  3604. } /* While entry != NULL */
  3605. if (!windows_private_dirsync_has_more(prp->agmt))
  3606. {
  3607. done = 1;
  3608. }
  3609. } /* While !done */
  3610. error:
  3611. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_dirsync_inc_run\n", 0, 0, 0 );
  3612. }