/
ActiveDirectorySite.cs
1487 lines (1334 loc) · 62.1 KB
/
ActiveDirectorySite.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.InteropServices;
using System.Collections;
using System.Diagnostics;
using System.Text;
using System.Diagnostics.CodeAnalysis;
namespace System.DirectoryServices.ActiveDirectory
{
[Flags]
public enum ActiveDirectorySiteOptions
{
None = 0,
AutoTopologyDisabled = 1,
TopologyCleanupDisabled = 2,
AutoMinimumHopDisabled = 4,
StaleServerDetectDisabled = 8,
AutoInterSiteTopologyDisabled = 16,
GroupMembershipCachingEnabled = 32,
ForceKccWindows2003Behavior = 64,
UseWindows2000IstgElection = 128,
RandomBridgeHeaderServerSelectionDisabled = 256,
UseHashingForReplicationSchedule = 512,
RedundantServerTopologyEnabled = 1024
}
public class ActiveDirectorySite : IDisposable
{
internal readonly DirectoryContext context;
private readonly string _name;
internal readonly DirectoryEntry cachedEntry;
private DirectoryEntry? _ntdsEntry;
private readonly ActiveDirectorySubnetCollection _subnets;
private DirectoryServer? _topologyGenerator;
private readonly ReadOnlySiteCollection _adjacentSites = new ReadOnlySiteCollection();
private bool _disposed;
private readonly DomainCollection _domains = new DomainCollection(null);
private readonly ReadOnlyDirectoryServerCollection _servers = new ReadOnlyDirectoryServerCollection();
private readonly ReadOnlySiteLinkCollection _links = new ReadOnlySiteLinkCollection();
private ActiveDirectorySiteOptions _siteOptions = ActiveDirectorySiteOptions.None;
private ReadOnlyDirectoryServerCollection _bridgeheadServers = new ReadOnlyDirectoryServerCollection();
private readonly DirectoryServerCollection _SMTPBridgeheadServers;
private readonly DirectoryServerCollection _RPCBridgeheadServers;
private byte[]? _replicationSchedule;
internal bool existing;
private bool _subnetRetrieved;
private bool _isADAMServer;
private bool _topologyTouched;
private bool _adjacentSitesRetrieved;
private readonly string _siteDN;
private bool _domainsRetrieved;
private bool _serversRetrieved;
private bool _belongLinksRetrieved;
private bool _bridgeheadServerRetrieved;
private bool _SMTPBridgeRetrieved;
private bool _RPCBridgeRetrieved;
private const int ERROR_NO_SITENAME = 1919;
public static ActiveDirectorySite FindByName(DirectoryContext context, string siteName)
{
// find an existing site
ValidateArgument(context, siteName);
// work with copy of the context
context = new DirectoryContext(context);
// bind to the rootdse to get the configurationnamingcontext
DirectoryEntry de;
string sitedn;
try
{
de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
sitedn = "CN=Sites," + (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ConfigurationNamingContext)!;
de = DirectoryEntryManager.GetDirectoryEntry(context, sitedn);
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
catch (ActiveDirectoryObjectNotFoundException)
{
// this is the case where the context is a config set and we could not find an ADAM instance in that config set
throw new ActiveDirectoryOperationException(SR.Format(SR.ADAMInstanceNotFoundInConfigSet, context.Name));
}
try
{
ADSearcher adSearcher = new ADSearcher(de,
"(&(objectClass=site)(objectCategory=site)(name=" + Utils.GetEscapedFilterValue(siteName) + "))",
new string[] { "distinguishedName" },
SearchScope.OneLevel,
false, /* don't need paged search */
false /* don't need to cache result */);
SearchResult? srchResult = adSearcher.FindOne();
if (srchResult == null)
{
// no such site object
throw new ActiveDirectoryObjectNotFoundException(SR.DSNotFound, typeof(ActiveDirectorySite), siteName);
}
// it is an existing site object
ActiveDirectorySite site = new ActiveDirectorySite(context, siteName, true);
return site;
}
catch (COMException e)
{
if (e.ErrorCode == unchecked((int)0x80072030))
{
// object is not found since we cannot even find the container in which to search
throw new ActiveDirectoryObjectNotFoundException(SR.DSNotFound, typeof(ActiveDirectorySite), siteName);
}
else
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
finally
{
de.Dispose();
}
}
public ActiveDirectorySite(DirectoryContext context, string siteName)
{
ValidateArgument(context, siteName);
// work with copy of the context
context = new DirectoryContext(context);
this.context = context;
_name = siteName;
// bind to the rootdse to get the configurationnamingcontext
DirectoryEntry? de = null;
try
{
de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
string config = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ConfigurationNamingContext)!;
_siteDN = "CN=Sites," + config;
// bind to the site container
de = DirectoryEntryManager.GetDirectoryEntry(context, _siteDN);
string rdn = "cn=" + _name;
rdn = Utils.GetEscapedPath(rdn);
cachedEntry = de.Children.Add(rdn, "site");
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
catch (ActiveDirectoryObjectNotFoundException)
{
// this is the case where the context is a config set and we could not find an ADAM instance in that config set
throw new ActiveDirectoryOperationException(SR.Format(SR.ADAMInstanceNotFoundInConfigSet, context.Name));
}
finally
{
de?.Dispose();
}
_subnets = new ActiveDirectorySubnetCollection(context, "CN=" + siteName + "," + _siteDN);
string transportDN = "CN=IP,CN=Inter-Site Transports," + _siteDN;
_RPCBridgeheadServers = new DirectoryServerCollection(context, "CN=" + siteName + "," + _siteDN, transportDN);
transportDN = "CN=SMTP,CN=Inter-Site Transports," + _siteDN;
_SMTPBridgeheadServers = new DirectoryServerCollection(context, "CN=" + siteName + "," + _siteDN, transportDN);
}
internal ActiveDirectorySite(DirectoryContext context, string siteName, bool existing)
{
Debug.Assert(existing);
this.context = context;
_name = siteName;
this.existing = existing;
DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
_siteDN = "CN=Sites," + (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ConfigurationNamingContext)!;
cachedEntry = DirectoryEntryManager.GetDirectoryEntry(context, "CN=" + siteName + "," + _siteDN);
_subnets = new ActiveDirectorySubnetCollection(context, "CN=" + siteName + "," + _siteDN);
string transportDN = "CN=IP,CN=Inter-Site Transports," + _siteDN;
_RPCBridgeheadServers = new DirectoryServerCollection(context, (string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.DistinguishedName)!, transportDN);
transportDN = "CN=SMTP,CN=Inter-Site Transports," + _siteDN;
_SMTPBridgeheadServers = new DirectoryServerCollection(context, (string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.DistinguishedName)!, transportDN);
}
public static ActiveDirectorySite GetComputerSite()
{
// make sure that this is the platform that we support
new DirectoryContext(DirectoryContextType.Forest);
IntPtr ptr = (IntPtr)0;
int result = UnsafeNativeMethods.DsGetSiteName(null, ref ptr);
if (result != 0)
{
// computer is not in a site
if (result == ERROR_NO_SITENAME)
throw new ActiveDirectoryObjectNotFoundException(SR.NoCurrentSite, typeof(ActiveDirectorySite), null);
else
throw ExceptionHelper.GetExceptionFromErrorCode(result);
}
else
{
try
{
string siteName = Marshal.PtrToStringUni(ptr)!;
Debug.Assert(siteName != null);
// find the forest this machine belongs to
string? forestName = Locator.GetDomainControllerInfo(null, null, null, (long)PrivateLocatorFlags.DirectoryServicesRequired).DnsForestName;
DirectoryContext currentContext = Utils.GetNewDirectoryContext(forestName, DirectoryContextType.Forest, null);
// existing site
ActiveDirectorySite site = ActiveDirectorySite.FindByName(currentContext, siteName);
return site;
}
finally
{
if (ptr != (IntPtr)0)
Marshal.FreeHGlobal(ptr);
}
}
}
public string Name
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
return _name;
}
}
public DomainCollection Domains
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (existing)
{
if (!_domainsRetrieved)
{
// clear it first to be safe in case GetDomains fail in the middle and leave partial results there
_domains.Clear();
GetDomains();
_domainsRetrieved = true;
}
}
return _domains;
}
}
public ActiveDirectorySubnetCollection Subnets
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (existing)
{
// if asked the first time, we need to properly construct the subnets collection
if (!_subnetRetrieved)
{
_subnets.initialized = false;
_subnets.Clear();
GetSubnets();
_subnetRetrieved = true;
}
}
_subnets.initialized = true;
return _subnets;
}
}
public ReadOnlyDirectoryServerCollection Servers
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (existing)
{
if (!_serversRetrieved)
{
_servers.Clear();
GetServers();
_serversRetrieved = true;
}
}
return _servers;
}
}
public ReadOnlySiteCollection AdjacentSites
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (existing)
{
if (!_adjacentSitesRetrieved)
{
_adjacentSites.Clear();
GetAdjacentSites();
_adjacentSitesRetrieved = true;
}
}
return _adjacentSites;
}
}
public ReadOnlySiteLinkCollection SiteLinks
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (existing)
{
if (!_belongLinksRetrieved)
{
_links.Clear();
GetLinks();
_belongLinksRetrieved = true;
}
}
return _links;
}
}
[DisallowNull]
public DirectoryServer? InterSiteTopologyGenerator
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (existing)
{
// have not load topology generator information from the directory and user has not set it yet
if (_topologyGenerator == null && !_topologyTouched)
{
bool ISTGExist;
try
{
ISTGExist = NTDSSiteEntry.Properties.Contains("interSiteTopologyGenerator");
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
if (ISTGExist)
{
string serverDN = (string)PropertyManager.GetPropertyValue(context, NTDSSiteEntry, PropertyManager.InterSiteTopologyGenerator)!;
string? hostname = null;
DirectoryEntry tmp = DirectoryEntryManager.GetDirectoryEntry(context, serverDN);
try
{
hostname = (string)PropertyManager.GetPropertyValue(context, tmp.Parent, PropertyManager.DnsHostName)!;
}
catch (COMException e)
{
if (e.ErrorCode == unchecked((int)0x80072030))
{
// indicates a demoted server
return null;
}
}
if (IsADAM)
{
int port = (int)PropertyManager.GetPropertyValue(context, tmp, PropertyManager.MsDSPortLDAP)!;
string fullHostName = hostname!;
if (port != 389)
{
fullHostName = hostname + ":" + port;
}
_topologyGenerator = new AdamInstance(Utils.GetNewDirectoryContext(fullHostName, DirectoryContextType.DirectoryServer, context), fullHostName);
}
else
{
_topologyGenerator = new DomainController(Utils.GetNewDirectoryContext(hostname, DirectoryContextType.DirectoryServer, context), hostname!);
}
}
}
}
return _topologyGenerator;
}
set
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (value == null)
throw new ArgumentNullException(nameof(value));
if (existing)
{
// for existing site, nTDSSiteSettings needs to exist
_ = NTDSSiteEntry;
}
_topologyTouched = true;
_topologyGenerator = value;
}
}
public ActiveDirectorySiteOptions Options
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (existing)
{
try
{
if (NTDSSiteEntry.Properties.Contains("options"))
{
return (ActiveDirectorySiteOptions)NTDSSiteEntry.Properties["options"][0]!;
}
else
return ActiveDirectorySiteOptions.None;
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
else
return _siteOptions;
}
set
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (existing)
{
try
{
NTDSSiteEntry.Properties["options"].Value = value;
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
else
_siteOptions = value;
}
}
public string? Location
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
try
{
if (cachedEntry.Properties.Contains("location"))
{
return (string)cachedEntry.Properties["location"][0]!;
}
else
return null;
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
set
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
try
{
if (value == null)
{
if (cachedEntry.Properties.Contains("location"))
cachedEntry.Properties["location"].Clear();
}
else
{
cachedEntry.Properties["location"].Value = value;
}
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
}
public ReadOnlyDirectoryServerCollection BridgeheadServers
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (!_bridgeheadServerRetrieved)
{
_bridgeheadServers = GetBridgeheadServers();
_bridgeheadServerRetrieved = true;
}
return _bridgeheadServers;
}
}
public DirectoryServerCollection PreferredSmtpBridgeheadServers
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (existing)
{
if (!_SMTPBridgeRetrieved)
{
_SMTPBridgeheadServers.initialized = false;
_SMTPBridgeheadServers.Clear();
GetPreferredBridgeheadServers(ActiveDirectoryTransportType.Smtp);
_SMTPBridgeRetrieved = true;
}
}
_SMTPBridgeheadServers.initialized = true;
return _SMTPBridgeheadServers;
}
}
public DirectoryServerCollection PreferredRpcBridgeheadServers
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (existing)
{
if (!_RPCBridgeRetrieved)
{
_RPCBridgeheadServers.initialized = false;
_RPCBridgeheadServers.Clear();
GetPreferredBridgeheadServers(ActiveDirectoryTransportType.Rpc);
_RPCBridgeRetrieved = true;
}
}
_RPCBridgeheadServers.initialized = true;
return _RPCBridgeheadServers;
}
}
public ActiveDirectorySchedule? IntraSiteReplicationSchedule
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
ActiveDirectorySchedule? schedule = null;
if (existing)
{
// if exists in the cache, return it, otherwise null is returned
try
{
if (NTDSSiteEntry.Properties.Contains("schedule"))
{
byte[] tmpSchedule = (byte[])NTDSSiteEntry.Properties["schedule"][0]!;
Debug.Assert(tmpSchedule != null && tmpSchedule.Length == 188);
schedule = new ActiveDirectorySchedule();
schedule.SetUnmanagedSchedule(tmpSchedule);
}
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
else
{
if (_replicationSchedule != null)
{
// newly created site, get the schedule if already has been set by the user
schedule = new ActiveDirectorySchedule();
schedule.SetUnmanagedSchedule(_replicationSchedule);
}
}
return schedule;
}
set
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (existing)
{
try
{
if (value == null)
{
// clear it out if existing before
if (NTDSSiteEntry.Properties.Contains("schedule"))
NTDSSiteEntry.Properties["schedule"].Clear();
}
else
// replace with the new value
NTDSSiteEntry.Properties["schedule"].Value = value.GetUnmanagedSchedule();
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
else
{
// clear out the schedule
if (value == null)
_replicationSchedule = null;
else
{
// replace with the new value
_replicationSchedule = value.GetUnmanagedSchedule();
}
}
}
}
private bool IsADAM
{
get
{
DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
PropertyValueCollection values;
try
{
values = de.Properties["supportedCapabilities"];
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
if (values.Contains(SupportedCapability.ADAMOid))
_isADAMServer = true;
return _isADAMServer;
}
}
private DirectoryEntry NTDSSiteEntry
{
get
{
if (_ntdsEntry == null)
{
DirectoryEntry tmp = DirectoryEntryManager.GetDirectoryEntry(context, "CN=NTDS Site Settings," + (string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.DistinguishedName)!);
try
{
tmp.RefreshCache();
}
catch (COMException e)
{
if (e.ErrorCode == unchecked((int)0x80072030))
{
string message = SR.Format(SR.NTDSSiteSetting, _name);
throw new ActiveDirectoryOperationException(message, e, 0x2030);
}
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
_ntdsEntry = tmp;
}
return _ntdsEntry;
}
}
public void Save()
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
try
{
// commit changes
cachedEntry.CommitChanges();
foreach (DictionaryEntry e in _subnets.changeList)
{
try
{
((DirectoryEntry)e.Value!).CommitChanges();
}
catch (COMException exception)
{
// there is a bug in ADSI that when targeting ADAM, permissive modify control is not used.
if (exception.ErrorCode != unchecked((int)0x8007200A))
throw ExceptionHelper.GetExceptionFromCOMException(exception);
}
}
// reset status variables
_subnets.changeList.Clear();
_subnetRetrieved = false;
// need to throw better exception for ADAM since its SMTP transport is not available
foreach (DictionaryEntry e in _SMTPBridgeheadServers.changeList!)
{
try
{
((DirectoryEntry)e.Value!).CommitChanges();
}
catch (COMException exception)
{
// SMTP transport is not supported on ADAM
if (IsADAM && (exception.ErrorCode == unchecked((int)0x8007202F)))
throw new NotSupportedException(SR.NotSupportTransportSMTP);
// there is a bug in ADSI that when targeting ADAM, permissive modify control is not used.
if (exception.ErrorCode != unchecked((int)0x8007200A))
throw ExceptionHelper.GetExceptionFromCOMException(exception);
}
}
_SMTPBridgeheadServers.changeList.Clear();
_SMTPBridgeRetrieved = false;
foreach (DictionaryEntry e in _RPCBridgeheadServers.changeList!)
{
try
{
((DirectoryEntry)e.Value!).CommitChanges();
}
catch (COMException exception)
{
// there is a bug in ADSI that when targeting ADAM, permissive modify control is not used.
if (exception.ErrorCode != unchecked((int)0x8007200A))
throw ExceptionHelper.GetExceptionFromCOMException(exception);
}
}
_RPCBridgeheadServers.changeList.Clear();
_RPCBridgeRetrieved = false;
if (existing)
{
// topology generator is changed
if (_topologyTouched)
{
try
{
DirectoryServer server = InterSiteTopologyGenerator!;
string ntdsaName = (server is DomainController) ? ((DomainController)server).NtdsaObjectName : ((AdamInstance)server).NtdsaObjectName;
NTDSSiteEntry.Properties["interSiteTopologyGenerator"].Value = ntdsaName;
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
NTDSSiteEntry.CommitChanges();
_topologyTouched = false;
}
else
{
try
{
// create nTDSSiteSettings object
DirectoryEntry tmpEntry = cachedEntry.Children.Add("CN=NTDS Site Settings", "nTDSSiteSettings");
//set properties on the Site NTDS settings object
DirectoryServer? replica = InterSiteTopologyGenerator;
if (replica != null)
{
string ntdsaName = (replica is DomainController) ? ((DomainController)replica).NtdsaObjectName : ((AdamInstance)replica).NtdsaObjectName;
tmpEntry.Properties["interSiteTopologyGenerator"].Value = ntdsaName;
}
tmpEntry.Properties["options"].Value = _siteOptions;
if (_replicationSchedule != null)
{
tmpEntry.Properties["schedule"].Value = _replicationSchedule;
}
tmpEntry.CommitChanges();
// cached the entry
_ntdsEntry = tmpEntry;
// create servers contain object
tmpEntry = cachedEntry.Children.Add("CN=Servers", "serversContainer");
tmpEntry.CommitChanges();
if (!IsADAM)
{
// create the licensingSiteSettings object
tmpEntry = cachedEntry.Children.Add("CN=Licensing Site Settings", "licensingSiteSettings");
tmpEntry.CommitChanges();
}
}
finally
{
// entry is created on the backend store successfully
existing = true;
}
}
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
public void Delete()
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (!existing)
{
throw new InvalidOperationException(SR.CannotDelete);
}
else
{
try
{
cachedEntry.DeleteTree();
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
}
public override string ToString()
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
return _name;
}
private ReadOnlyDirectoryServerCollection GetBridgeheadServers()
{
NativeComInterfaces.IAdsPathname pathCracker = (NativeComInterfaces.IAdsPathname)new NativeComInterfaces.Pathname();
// need to turn off the escaping for name
pathCracker.EscapedMode = NativeComInterfaces.ADS_ESCAPEDMODE_OFF_EX;
ReadOnlyDirectoryServerCollection collection = new ReadOnlyDirectoryServerCollection();
if (existing)
{
Hashtable bridgeHeadTable = new Hashtable();
Hashtable nonBridgHeadTable = new Hashtable();
Hashtable hostNameTable = new Hashtable();
const string ocValue = "CN=Server";
// get destination bridgehead servers
// first go to the servers container under the current site and then do a search to get the all server objects.
string serverContainer = "CN=Servers," + (string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.DistinguishedName)!;
DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(context, serverContainer);
try
{
// go through connection objects and find out its fromServer property.
ADSearcher adSearcher = new ADSearcher(de,
"(|(objectCategory=server)(objectCategory=NTDSConnection))",
new string[] { "fromServer", "distinguishedName", "dNSHostName", "objectCategory" },
SearchScope.Subtree,
true, /* need paged search */
true /* need cached result as we need to go back to the first record */);
SearchResultCollection? conResults = null;
try
{
conResults = adSearcher.FindAll();
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
try
{
// find out whether fromServer indicates replicating from a server in another site.
foreach (SearchResult r in conResults)
{
string objectCategoryValue = (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.ObjectCategory)!;
if (Utils.Compare(objectCategoryValue, 0, ocValue.Length, ocValue, 0, ocValue.Length) == 0)
{
hostNameTable.Add((string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.DistinguishedName)!, (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.DnsHostName)!);
}
}
foreach (SearchResult r in conResults)
{
string objectCategoryValue = (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.ObjectCategory)!;
if (Utils.Compare(objectCategoryValue, 0, ocValue.Length, ocValue, 0, ocValue.Length) != 0)
{
string fromServer = (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.FromServer)!;
// escaping manipulation
string fromSite = Utils.GetPartialDN(fromServer, 3);
pathCracker.Set(fromSite, NativeComInterfaces.ADS_SETTYPE_DN);
fromSite = pathCracker.Retrieve(NativeComInterfaces.ADS_FORMAT_LEAF);
Debug.Assert(fromSite != null && Utils.Compare(fromSite, 0, 3, "CN=", 0, 3) == 0);
fromSite = fromSite.Substring(3);
string serverObjectName = Utils.GetPartialDN((string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.DistinguishedName)!, 2);
// don't know whether it is a bridgehead server yet.
if (!bridgeHeadTable.Contains(serverObjectName))
{
string hostName = (string)hostNameTable[serverObjectName]!;
// add if not yet done
if (!nonBridgHeadTable.Contains(serverObjectName))
nonBridgHeadTable.Add(serverObjectName, hostName);
// check whether from different site
if (Utils.Compare((string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.Cn)!, fromSite) != 0)
{
// the server is a bridgehead server
bridgeHeadTable.Add(serverObjectName, hostName);
nonBridgHeadTable.Remove(serverObjectName);
}
}
}
}
}
finally
{
conResults.Dispose();
}
}
finally
{
de.Dispose();
}
// get source bridgehead server
if (nonBridgHeadTable.Count != 0)
{
// go to sites container to get all the connecdtion object that replicates from servers in the current sites that have
// not been determined whether it is a bridgehead server or not.
DirectoryEntry serverEntry = DirectoryEntryManager.GetDirectoryEntry(context, _siteDN);
// constructing the filter
StringBuilder str = new StringBuilder(100);
if (nonBridgHeadTable.Count > 1)
str.Append("(|");
foreach (DictionaryEntry val in nonBridgHeadTable)
{
str.Append("(fromServer=");
str.Append("CN=NTDS Settings,");
str.Append(Utils.GetEscapedFilterValue((string)val.Key));
str.Append(')');
}
if (nonBridgHeadTable.Count > 1)
str.Append(')');
ADSearcher adSearcher = new ADSearcher(serverEntry,
"(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)" + str.ToString() + ")",
new string[] { "fromServer", "distinguishedName" },
SearchScope.Subtree);
SearchResultCollection? conResults = null;
try
{
conResults = adSearcher.FindAll();
}
catch (COMException e)
{