windows_protocol_util.c 109 KB

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