windows_protocol_util.c 120 KB

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