windows_protocol_util.c 91 KB

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