/
Domain.cs
1484 lines (1259 loc) · 62 KB
/
Domain.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.Text;
using System.Collections;
using System.Globalization;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace System.DirectoryServices.ActiveDirectory
{
public enum DomainMode : int
{
Unknown = -1,
Windows2000MixedDomain = 0, // win2000, win2003, NT
Windows2000NativeDomain = 1, // win2000, win2003
Windows2003InterimDomain = 2, // win2003, NT
Windows2003Domain = 3, // win2003
Windows2008Domain = 4, // win2008
Windows2008R2Domain = 5, // win2008 R2
Windows8Domain = 6, //Windows Server 2012
Windows2012R2Domain = 7, //Windows Server 2012 R2
}
public class Domain : ActiveDirectoryPartition
{
/// Private Variables
private string? _crossRefDN;
private string? _trustParent;
// internal variables corresponding to public properties
private DomainControllerCollection? _cachedDomainControllers;
private DomainCollection? _cachedChildren;
private DomainMode _currentDomainMode = (DomainMode)(-1);
private int _domainModeLevel = -1;
private DomainController? _cachedPdcRoleOwner;
private DomainController? _cachedRidRoleOwner;
private DomainController? _cachedInfrastructureRoleOwner;
private Domain? _cachedParent;
private Forest? _cachedForest;
// this is needed because null value for parent is valid
private bool _isParentInitialized;
#region constructors
// internal constructors
internal Domain(DirectoryContext context, string domainName, DirectoryEntryManager directoryEntryMgr)
: base(context, domainName)
{
this.directoryEntryMgr = directoryEntryMgr;
}
internal Domain(DirectoryContext context, string domainName)
: this(context, domainName, new DirectoryEntryManager(context))
{
}
#endregion constructors
#region public methods
public static Domain GetDomain(DirectoryContext context)
{
// check that the argument is not null
if (context == null)
throw new ArgumentNullException(nameof(context));
// contexttype should be Domain or DirectoryServer
if ((context.ContextType != DirectoryContextType.Domain) &&
(context.ContextType != DirectoryContextType.DirectoryServer))
{
throw new ArgumentException(SR.TargetShouldBeServerORDomain, nameof(context));
}
if ((context.Name == null) && (!context.isDomain()))
{
throw new ActiveDirectoryObjectNotFoundException(SR.ContextNotAssociatedWithDomain, typeof(Domain), null);
}
if (context.Name != null)
{
// the target should be a valid domain name or a server
if (!((context.isDomain()) || (context.isServer())))
{
if (context.ContextType == DirectoryContextType.Domain)
{
throw new ActiveDirectoryObjectNotFoundException(SR.DomainNotFound, typeof(Domain), context.Name);
}
else
{
throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.DCNotFound, context.Name), typeof(Domain), null);
}
}
}
// work with copy of the context
context = new DirectoryContext(context);
// bind to the rootDSE of the domain specified in the context
// and get the dns name
DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
DirectoryEntry? rootDSE = null;
string? defaultDomainNC = null;
try
{
rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
if ((context.isServer()) && (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectory)))
{
throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.DCNotFound, context.Name), typeof(Domain), null);
}
defaultDomainNC = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.DefaultNamingContext)!;
}
catch (COMException e)
{
int errorCode = e.ErrorCode;
if (errorCode == unchecked((int)0x8007203a))
{
if (context.ContextType == DirectoryContextType.Domain)
{
throw new ActiveDirectoryObjectNotFoundException(SR.DomainNotFound, typeof(Domain), context.Name);
}
else
{
throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.DCNotFound, context.Name), typeof(Domain), null);
}
}
else
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
// return domain object
return new Domain(context, Utils.GetDnsNameFromDN(defaultDomainNC), directoryEntryMgr);
}
public static Domain GetComputerDomain()
{
string? computerDomainName = DirectoryContext.GetDnsDomainName(null);
if (computerDomainName == null)
{
throw new ActiveDirectoryObjectNotFoundException(SR.ComputerNotJoinedToDomain, typeof(Domain), null);
}
return Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, computerDomainName));
}
public void RaiseDomainFunctionalityLevel(int domainMode)
{
int existingDomainModeLevel;
CheckIfDisposed();
// check if domainMode is within the valid range
if (domainMode < 0)
{
throw new ArgumentException(SR.InvalidMode, nameof(domainMode));
}
// get the current domain mode
existingDomainModeLevel = DomainModeLevel;
if (existingDomainModeLevel >= domainMode)
{
throw new ArgumentException(SR.InvalidMode, nameof(domainMode));
}
DomainMode existingDomainMode = DomainMode;
// set the forest mode on AD
DirectoryEntry? domainEntry = null;
// CurrentDomain Valid domainMode Action
// -----------------
// Windows2000Mixed 0 ntMixedDomain = 0 msDS-Behavior-Version = 0
// Windows2000Mixed 1 msDS-Behavior-Version = 1
// Windows2000Mixed 2 ntMixedDomain = 0, msDS-Behavior-Version = 2
//
// Windows2003Interim 2 ntMixedDomain = 0, msDS-Behavior-Version = 2
//
// Rest 2 or above msDS-Behavior-Version = domainMode
try
{
domainEntry = DirectoryEntryManager.GetDirectoryEntry(context, directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.DefaultNamingContext));
// set the new functional level
domainEntry.Properties[PropertyManager.MsDSBehaviorVersion].Value = domainMode;
switch (existingDomainMode)
{
case DomainMode.Windows2000MixedDomain:
{
if (domainMode == 2 || domainMode == 0)
{
domainEntry.Properties[PropertyManager.NTMixedDomain].Value = 0;
}
else if (domainMode > 2) // new level should be less than or equal to Windows2003
{
throw new ArgumentException(SR.InvalidMode, nameof(domainMode));
}
break;
}
case DomainMode.Windows2003InterimDomain:
{
if (domainMode == 2) // only Windows2003 allowed
{
domainEntry.Properties[PropertyManager.NTMixedDomain].Value = 0;
}
else
{
throw new ArgumentException(SR.InvalidMode, nameof(domainMode));
}
break;
}
default:
break;
}
// NOTE:
// If the domain controller we are talking to is W2K
// (more specifically the schema is a W2K schema) then the
// msDS-Behavior-Version attribute will not be present.
// If that is the case, the domain functionality cannot be raised
// to Windows2003InterimDomain or Windows2003Domain (which is when we would set this attribute)
// since there are only W2K domain controllers
// So, we catch that exception and throw a more meaningful one.
domainEntry.CommitChanges();
}
catch (System.Runtime.InteropServices.COMException e)
{
if (e.ErrorCode == unchecked((int)0x8007200A))
{
// attribute does not exist which means this is not a W2K3 DC
// cannot raise domain functionality
throw new ArgumentException(SR.NoW2K3DCs, nameof(domainMode));
}
else
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
finally
{
domainEntry?.Dispose();
}
// at this point the raise domain function has succeeded
// invalidate the domain mode so that we get it from the server the next time
_currentDomainMode = (DomainMode)(-1);
_domainModeLevel = -1;
}
public void RaiseDomainFunctionality(DomainMode domainMode)
{
DomainMode existingDomainMode;
CheckIfDisposed();
// check if domain mode is within the valid range
if (domainMode < DomainMode.Windows2000MixedDomain || domainMode > DomainMode.Windows2012R2Domain)
{
throw new InvalidEnumArgumentException(nameof(domainMode), (int)domainMode, typeof(DomainMode));
}
// get the current domain mode
existingDomainMode = GetDomainMode();
// set the forest mode on AD
DirectoryEntry? domainEntry = null;
// CurrentDomain Valid RequestedDomain Action
// -----------------
// Windows2000Mixed Windows2000Native ntMixedDomain = 0
// Windows2000Mixed Windows2003Interim msDS-Behavior-Version = 1
// Windows2000Mixed Windows2003 ntMixedDomain = 0, msDS-Behavior-Version = 2
//
// Windows2003Interim Windows2003 ntMixedDomain = 0, msDS-Behavior-Version = 2
//
// Windows2000Native Windows2003 or above
// Windows2003 Windows2008 or above
// Windows2008 Windows2008R2 or above
// Windows2008R2 Windows2012 or above
// Windows2012 Windows2012R2 or above
// Windows2012R2 ERROR
try
{
domainEntry = DirectoryEntryManager.GetDirectoryEntry(context, directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.DefaultNamingContext));
switch (existingDomainMode)
{
case DomainMode.Windows2000MixedDomain:
{
if (domainMode == DomainMode.Windows2000NativeDomain)
{
domainEntry.Properties[PropertyManager.NTMixedDomain].Value = 0;
}
else if (domainMode == DomainMode.Windows2003InterimDomain)
{
domainEntry.Properties[PropertyManager.MsDSBehaviorVersion].Value = 1;
}
else if (domainMode == DomainMode.Windows2003Domain)
{
domainEntry.Properties[PropertyManager.NTMixedDomain].Value = 0;
domainEntry.Properties[PropertyManager.MsDSBehaviorVersion].Value = 2;
}
else
{
throw new ArgumentException(SR.InvalidMode, nameof(domainMode));
}
break;
}
case DomainMode.Windows2003InterimDomain:
{
if (domainMode == DomainMode.Windows2003Domain)
{
domainEntry.Properties[PropertyManager.NTMixedDomain].Value = 0;
domainEntry.Properties[PropertyManager.MsDSBehaviorVersion].Value = 2;
}
else
{
throw new ArgumentException(SR.InvalidMode, nameof(domainMode));
}
break;
}
case DomainMode.Windows2000NativeDomain:
case DomainMode.Windows2003Domain:
case DomainMode.Windows2008Domain:
case DomainMode.Windows2008R2Domain:
case DomainMode.Windows8Domain:
case DomainMode.Windows2012R2Domain:
{
if (existingDomainMode >= domainMode)
{
throw new ArgumentException(SR.InvalidMode, nameof(domainMode));
}
if (domainMode == DomainMode.Windows2003Domain)
{
domainEntry.Properties[PropertyManager.MsDSBehaviorVersion].Value = 2;
}
else if (domainMode == DomainMode.Windows2008Domain)
{
domainEntry.Properties[PropertyManager.MsDSBehaviorVersion].Value = 3;
}
else if (domainMode == DomainMode.Windows2008R2Domain)
{
domainEntry.Properties[PropertyManager.MsDSBehaviorVersion].Value = 4;
}
else if (domainMode == DomainMode.Windows8Domain)
{
domainEntry.Properties[PropertyManager.MsDSBehaviorVersion].Value = 5;
}
else if (domainMode == DomainMode.Windows2012R2Domain)
{
domainEntry.Properties[PropertyManager.MsDSBehaviorVersion].Value = 6;
}
else
{
throw new ArgumentException(SR.InvalidMode, nameof(domainMode));
}
}
break;
default:
{
// should not happen
throw new ActiveDirectoryOperationException();
}
}
// NOTE:
// If the domain controller we are talking to is W2K
// (more specifically the schema is a W2K schema) then the
// msDS-Behavior-Version attribute will not be present.
// If that is the case, the domain functionality cannot be raised
// to Windows2003InterimDomain or Windows2003Domain (which is when we would set this attribute)
// since there are only W2K domain controllers
// So, we catch that exception and throw a more meaningful one.
domainEntry.CommitChanges();
}
catch (System.Runtime.InteropServices.COMException e)
{
if (e.ErrorCode == unchecked((int)0x8007200A))
{
// attribute does not exist which means this is not a W2K3 DC
// cannot raise domain functionality
throw new ArgumentException(SR.NoW2K3DCs, nameof(domainMode));
}
else
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
}
finally
{
domainEntry?.Dispose();
}
// at this point the raise domain function has succeeded
// invalidate the domain mode so that we get it from the server the next time
_currentDomainMode = (DomainMode)(-1);
_domainModeLevel = -1;
}
public DomainController FindDomainController()
{
CheckIfDisposed();
return DomainController.FindOneInternal(context, Name, null, 0);
}
public DomainController FindDomainController(string siteName)
{
CheckIfDisposed();
if (siteName == null)
{
throw new ArgumentNullException(nameof(siteName));
}
return DomainController.FindOneInternal(context, Name, siteName, 0);
}
public DomainController FindDomainController(LocatorOptions flag)
{
CheckIfDisposed();
return DomainController.FindOneInternal(context, Name, null, flag);
}
public DomainController FindDomainController(string siteName, LocatorOptions flag)
{
CheckIfDisposed();
if (siteName == null)
{
throw new ArgumentNullException(nameof(siteName));
}
return DomainController.FindOneInternal(context, Name, siteName, flag);
}
public DomainControllerCollection FindAllDomainControllers()
{
CheckIfDisposed();
return DomainController.FindAllInternal(context, Name, true /*isDnsDomainName */, null);
}
public DomainControllerCollection FindAllDomainControllers(string siteName)
{
CheckIfDisposed();
if (siteName == null)
{
throw new ArgumentNullException(nameof(siteName));
}
return DomainController.FindAllInternal(context, Name, true /*isDnsDomainName */, siteName);
}
public DomainControllerCollection FindAllDiscoverableDomainControllers()
{
long flag = (long)PrivateLocatorFlags.DSWriteableRequired;
CheckIfDisposed();
return new DomainControllerCollection(Locator.EnumerateDomainControllers(context, Name, null, (long)flag));
}
public DomainControllerCollection FindAllDiscoverableDomainControllers(string siteName)
{
long flag = (long)PrivateLocatorFlags.DSWriteableRequired;
CheckIfDisposed();
if (siteName == null)
{
throw new ArgumentNullException(nameof(siteName));
}
if (siteName.Length == 0)
{
throw new ArgumentException(SR.EmptyStringParameter, nameof(siteName));
}
return new DomainControllerCollection(Locator.EnumerateDomainControllers(context, Name, siteName, (long)flag));
}
public override DirectoryEntry GetDirectoryEntry()
{
CheckIfDisposed();
return DirectoryEntryManager.GetDirectoryEntry(context, directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.DefaultNamingContext));
}
public TrustRelationshipInformationCollection GetAllTrustRelationships()
{
CheckIfDisposed();
ArrayList trusts = GetTrustsHelper(null);
TrustRelationshipInformationCollection collection = new TrustRelationshipInformationCollection(context, Name, trusts);
return collection;
}
public TrustRelationshipInformation GetTrustRelationship(string targetDomainName)
{
CheckIfDisposed();
if (targetDomainName == null)
throw new ArgumentNullException(nameof(targetDomainName));
if (targetDomainName.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(targetDomainName));
ArrayList trusts = GetTrustsHelper(targetDomainName);
TrustRelationshipInformationCollection collection = new TrustRelationshipInformationCollection(context, Name, trusts);
if (collection.Count == 0)
{
// trust relationship does not exist
throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.DomainTrustDoesNotExist, Name, targetDomainName), typeof(TrustRelationshipInformation), null);
}
else
{
Debug.Assert(collection.Count == 1);
return collection[0];
}
}
public bool GetSelectiveAuthenticationStatus(string targetDomainName)
{
CheckIfDisposed();
if (targetDomainName == null)
throw new ArgumentNullException(nameof(targetDomainName));
if (targetDomainName.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(targetDomainName));
return TrustHelper.GetTrustedDomainInfoStatus(context, Name, targetDomainName, TRUST_ATTRIBUTE.TRUST_ATTRIBUTE_CROSS_ORGANIZATION, false);
}
public void SetSelectiveAuthenticationStatus(string targetDomainName, bool enable)
{
CheckIfDisposed();
if (targetDomainName == null)
throw new ArgumentNullException(nameof(targetDomainName));
if (targetDomainName.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(targetDomainName));
TrustHelper.SetTrustedDomainInfoStatus(context, Name, targetDomainName, TRUST_ATTRIBUTE.TRUST_ATTRIBUTE_CROSS_ORGANIZATION, enable, false);
}
public bool GetSidFilteringStatus(string targetDomainName)
{
CheckIfDisposed();
if (targetDomainName == null)
throw new ArgumentNullException(nameof(targetDomainName));
if (targetDomainName.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(targetDomainName));
return TrustHelper.GetTrustedDomainInfoStatus(context, Name, targetDomainName, TRUST_ATTRIBUTE.TRUST_ATTRIBUTE_QUARANTINED_DOMAIN, false);
}
public void SetSidFilteringStatus(string targetDomainName, bool enable)
{
CheckIfDisposed();
if (targetDomainName == null)
throw new ArgumentNullException(nameof(targetDomainName));
if (targetDomainName.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(targetDomainName));
TrustHelper.SetTrustedDomainInfoStatus(context, Name, targetDomainName, TRUST_ATTRIBUTE.TRUST_ATTRIBUTE_QUARANTINED_DOMAIN, enable, false);
}
public void DeleteLocalSideOfTrustRelationship(string targetDomainName)
{
CheckIfDisposed();
if (targetDomainName == null)
throw new ArgumentNullException(nameof(targetDomainName));
if (targetDomainName.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(targetDomainName));
// delete local side of trust only
TrustHelper.DeleteTrust(context, Name, targetDomainName, false);
}
public void DeleteTrustRelationship(Domain targetDomain)
{
CheckIfDisposed();
if (targetDomain == null)
throw new ArgumentNullException(nameof(targetDomain));
// first delete the trust on the remote side
TrustHelper.DeleteTrust(targetDomain.GetDirectoryContext(), targetDomain.Name, Name, false);
// then delete the local side trust
TrustHelper.DeleteTrust(context, Name, targetDomain.Name, false);
}
public void VerifyOutboundTrustRelationship(string targetDomainName)
{
CheckIfDisposed();
if (targetDomainName == null)
throw new ArgumentNullException(nameof(targetDomainName));
if (targetDomainName.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(targetDomainName));
TrustHelper.VerifyTrust(context, Name, targetDomainName, false/*not forest*/, TrustDirection.Outbound, false/*just TC verification*/, null /* no need to go to specific server*/);
}
public void VerifyTrustRelationship(Domain targetDomain, TrustDirection direction)
{
CheckIfDisposed();
if (targetDomain == null)
throw new ArgumentNullException(nameof(targetDomain));
if (direction < TrustDirection.Inbound || direction > TrustDirection.Bidirectional)
throw new InvalidEnumArgumentException(nameof(direction), (int)direction, typeof(TrustDirection));
// verify outbound trust first
if ((direction & TrustDirection.Outbound) != 0)
{
try
{
TrustHelper.VerifyTrust(context, Name, targetDomain.Name, false/*not forest*/, TrustDirection.Outbound, false/*just TC verification*/, null /* no need to go to specific server*/);
}
catch (ActiveDirectoryObjectNotFoundException)
{
throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.WrongTrustDirection, Name, targetDomain.Name, direction), typeof(TrustRelationshipInformation), null);
}
}
// verify inbound trust
if ((direction & TrustDirection.Inbound) != 0)
{
try
{
TrustHelper.VerifyTrust(targetDomain.GetDirectoryContext(), targetDomain.Name, Name, false/*not forest*/, TrustDirection.Outbound, false/*just TC verification*/, null /* no need to go to specific server*/);
}
catch (ActiveDirectoryObjectNotFoundException)
{
throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.WrongTrustDirection, Name, targetDomain.Name, direction), typeof(TrustRelationshipInformation), null);
}
}
}
public void CreateLocalSideOfTrustRelationship(string targetDomainName, TrustDirection direction, string trustPassword)
{
CheckIfDisposed();
if (targetDomainName == null)
throw new ArgumentNullException(nameof(targetDomainName));
if (targetDomainName.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(targetDomainName));
if (direction < TrustDirection.Inbound || direction > TrustDirection.Bidirectional)
throw new InvalidEnumArgumentException(nameof(direction), (int)direction, typeof(TrustDirection));
if (trustPassword == null)
throw new ArgumentNullException(nameof(trustPassword));
if (trustPassword.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(trustPassword));
// verify first that the target domain name is valid
Locator.GetDomainControllerInfo(null, targetDomainName, null, (long)PrivateLocatorFlags.DirectoryServicesRequired);
DirectoryContext targetContext = Utils.GetNewDirectoryContext(targetDomainName, DirectoryContextType.Domain, context);
TrustHelper.CreateTrust(context, Name, targetContext, targetDomainName, false, direction, trustPassword);
}
public void CreateTrustRelationship(Domain targetDomain, TrustDirection direction)
{
CheckIfDisposed();
if (targetDomain == null)
throw new ArgumentNullException(nameof(targetDomain));
if (direction < TrustDirection.Inbound || direction > TrustDirection.Bidirectional)
throw new InvalidEnumArgumentException(nameof(direction), (int)direction, typeof(TrustDirection));
string password = TrustHelper.CreateTrustPassword();
// first create trust on local side
TrustHelper.CreateTrust(context, Name, targetDomain.GetDirectoryContext(), targetDomain.Name, false, direction, password);
// then create trust on remote side
int reverseDirection = 0;
if ((direction & TrustDirection.Inbound) != 0)
reverseDirection |= (int)TrustDirection.Outbound;
if ((direction & TrustDirection.Outbound) != 0)
reverseDirection |= (int)TrustDirection.Inbound;
TrustHelper.CreateTrust(targetDomain.GetDirectoryContext(), targetDomain.Name, context, Name, false, (TrustDirection)reverseDirection, password);
}
public void UpdateLocalSideOfTrustRelationship(string targetDomainName, string newTrustPassword)
{
CheckIfDisposed();
if (targetDomainName == null)
throw new ArgumentNullException(nameof(targetDomainName));
if (targetDomainName.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(targetDomainName));
if (newTrustPassword == null)
throw new ArgumentNullException(nameof(newTrustPassword));
if (newTrustPassword.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(newTrustPassword));
TrustHelper.UpdateTrust(context, Name, targetDomainName, newTrustPassword, false);
}
public void UpdateLocalSideOfTrustRelationship(string targetDomainName, TrustDirection newTrustDirection, string newTrustPassword)
{
CheckIfDisposed();
if (targetDomainName == null)
throw new ArgumentNullException(nameof(targetDomainName));
if (targetDomainName.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(targetDomainName));
if (newTrustDirection < TrustDirection.Inbound || newTrustDirection > TrustDirection.Bidirectional)
throw new InvalidEnumArgumentException(nameof(newTrustDirection), (int)newTrustDirection, typeof(TrustDirection));
if (newTrustPassword == null)
throw new ArgumentNullException(nameof(newTrustPassword));
if (newTrustPassword.Length == 0)
throw new ArgumentException(SR.EmptyStringParameter, nameof(newTrustPassword));
TrustHelper.UpdateTrustDirection(context, Name, targetDomainName, newTrustPassword, false /*not a forest*/, newTrustDirection);
}
public void UpdateTrustRelationship(Domain targetDomain, TrustDirection newTrustDirection)
{
CheckIfDisposed();
if (targetDomain == null)
throw new ArgumentNullException(nameof(targetDomain));
if (newTrustDirection < TrustDirection.Inbound || newTrustDirection > TrustDirection.Bidirectional)
throw new InvalidEnumArgumentException(nameof(newTrustDirection), (int)newTrustDirection, typeof(TrustDirection));
// no we generate trust password
string password = TrustHelper.CreateTrustPassword();
TrustHelper.UpdateTrustDirection(context, Name, targetDomain.Name, password, false /* not a forest */, newTrustDirection);
// then create trust on remote side
TrustDirection reverseDirection = 0;
if ((newTrustDirection & TrustDirection.Inbound) != 0)
reverseDirection |= TrustDirection.Outbound;
if ((newTrustDirection & TrustDirection.Outbound) != 0)
reverseDirection |= TrustDirection.Inbound;
TrustHelper.UpdateTrustDirection(targetDomain.GetDirectoryContext(), targetDomain.Name, Name, password, false /* not a forest */, reverseDirection);
}
public void RepairTrustRelationship(Domain targetDomain)
{
TrustDirection direction = TrustDirection.Bidirectional;
CheckIfDisposed();
if (targetDomain == null)
throw new ArgumentNullException(nameof(targetDomain));
// first try to reset the secure channel
try
{
direction = GetTrustRelationship(targetDomain.Name!).TrustDirection;
// verify outbound trust first
if ((direction & TrustDirection.Outbound) != 0)
{
TrustHelper.VerifyTrust(context, Name, targetDomain.Name, false /*not forest*/, TrustDirection.Outbound, true /*reset secure channel*/, null /* no need to go to specific server*/);
}
// verify inbound trust
if ((direction & TrustDirection.Inbound) != 0)
{
TrustHelper.VerifyTrust(targetDomain.GetDirectoryContext(), targetDomain.Name, Name, false /*not forest*/, TrustDirection.Outbound, true/*reset secure channel*/, null /* no need to go to specific server*/);
}
}
catch (ActiveDirectoryOperationException)
{
// secure channel setup fails
RepairTrustHelper(targetDomain, direction);
}
catch (UnauthorizedAccessException)
{
// trust password does not match
RepairTrustHelper(targetDomain, direction);
}
catch (ActiveDirectoryObjectNotFoundException)
{
throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.WrongTrustDirection, Name, targetDomain.Name, direction), typeof(TrustRelationshipInformation), null);
}
}
public static Domain GetCurrentDomain()
{
return Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain));
}
#endregion public methods
#region public properties
public Forest Forest
{
get
{
CheckIfDisposed();
if (_cachedForest == null)
{
// get the name of rootDomainNamingContext
DirectoryEntry rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
string rootDomainNC = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.RootDomainNamingContext)!;
string? forestName = Utils.GetDnsNameFromDN(rootDomainNC);
DirectoryContext forestContext = Utils.GetNewDirectoryContext(forestName, DirectoryContextType.Forest, context);
_cachedForest = new Forest(forestContext, forestName);
}
return _cachedForest;
}
}
public DomainControllerCollection DomainControllers
{
get
{
CheckIfDisposed();
return _cachedDomainControllers ??= FindAllDomainControllers();
}
}
public DomainCollection Children
{
get
{
CheckIfDisposed();
return _cachedChildren ??= new DomainCollection(GetChildDomains());
}
}
public DomainMode DomainMode
{
get
{
CheckIfDisposed();
if ((int)_currentDomainMode == -1)
{
_currentDomainMode = GetDomainMode();
}
return _currentDomainMode;
}
}
public int DomainModeLevel
{
get
{
CheckIfDisposed();
if (_domainModeLevel == -1)
{
_domainModeLevel = GetDomainModeLevel();
}
return _domainModeLevel;
}
}
public Domain? Parent
{
get
{
CheckIfDisposed();
if (!_isParentInitialized)
{
_cachedParent = GetParent();
_isParentInitialized = true;
}
return _cachedParent;
}
}
public DomainController PdcRoleOwner
{
get
{
CheckIfDisposed();
_cachedPdcRoleOwner ??= GetRoleOwner(ActiveDirectoryRole.PdcRole);
return _cachedPdcRoleOwner;
}
}
public DomainController RidRoleOwner
{
get
{
CheckIfDisposed();
return _cachedRidRoleOwner ??= GetRoleOwner(ActiveDirectoryRole.RidRole);
}
}
public DomainController InfrastructureRoleOwner
{
get
{
CheckIfDisposed();
return _cachedInfrastructureRoleOwner ??= GetRoleOwner(ActiveDirectoryRole.InfrastructureRole);
}
}
#endregion public properties
#region private methods
internal DirectoryContext GetDirectoryContext() => context;
private int GetDomainModeLevel()
{
DirectoryEntry? domainEntry = null;
DirectoryEntry rootDSE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
int domainFunctionality = 0;
try
{
if (rootDSE.Properties.Contains(PropertyManager.DomainFunctionality))
{
domainFunctionality = int.Parse((string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.DomainFunctionality)!, NumberFormatInfo.InvariantInfo);
}
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
finally
{
rootDSE.Dispose();
domainEntry?.Dispose();
}
return domainFunctionality;
}
private DomainMode GetDomainMode()
{
// logic to check the domain mode
// if domainFunctionality is 0,
// then check ntMixedDomain to differentiate between
// Windows2000Native and Windows2000Mixed
// if domainFunctionality is 1 ==> Windows2003Interim
// if domainFunctionality is 2 ==> Windows2003
// if domainFunctionality is 3 ==> Windows2008
// if domainFunctionality is 4 ==> Windows2008R2
// if domainFunctionality is 5 ==> Windows2012
// if domainFunctionality is 6 ==> Windows2012R2
DomainMode domainMode;
DirectoryEntry? domainEntry = null;
int domainFunctionality = DomainModeLevel;
try
{
// If the "domainFunctionality" attribute is not set on the rootdse, then
// this is a W2K domain (with W2K schema) so just check for mixed or native
switch (domainFunctionality)
{
case 0:
{
domainEntry = DirectoryEntryManager.GetDirectoryEntry(context, directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.DefaultNamingContext));
int ntMixedDomain = (int)PropertyManager.GetPropertyValue(context, domainEntry, PropertyManager.NTMixedDomain)!;
if (ntMixedDomain == 0)
{
domainMode = DomainMode.Windows2000NativeDomain;
}
else
{
domainMode = DomainMode.Windows2000MixedDomain;