windows_protocol_util.c 89 KB

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