Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NTAccount.cs
Go to the documentation of this file.
5
7
8public sealed class NTAccount : IdentityReference
9{
10 private readonly string _name;
11
12 public override string Value => ToString();
13
14 public NTAccount(string domainName, string accountName)
15 {
16 if (accountName == null)
17 {
18 throw new ArgumentNullException("accountName");
19 }
20 if (accountName.Length == 0)
21 {
22 throw new ArgumentException(System.SR.Argument_StringZeroLength, "accountName");
23 }
24 if (accountName.Length > 256)
25 {
27 }
28 if (domainName != null && domainName.Length > 255)
29 {
31 }
32 if (domainName == null || domainName.Length == 0)
33 {
34 _name = accountName;
35 }
36 else
37 {
38 _name = domainName + "\\" + accountName;
39 }
40 }
41
42 public NTAccount(string name)
43 {
44 if (name == null)
45 {
46 throw new ArgumentNullException("name");
47 }
48 if (name.Length == 0)
49 {
51 }
52 if (name.Length > 512)
53 {
55 }
56 _name = name;
57 }
58
59 public override bool IsValidTargetType(Type targetType)
60 {
61 if (targetType == typeof(SecurityIdentifier))
62 {
63 return true;
64 }
65 if (targetType == typeof(NTAccount))
66 {
67 return true;
68 }
69 return false;
70 }
71
72 public override IdentityReference Translate(Type targetType)
73 {
74 if (targetType == null)
75 {
76 throw new ArgumentNullException("targetType");
77 }
78 if (targetType == typeof(NTAccount))
79 {
80 return this;
81 }
82 if (targetType == typeof(SecurityIdentifier))
83 {
84 IdentityReferenceCollection identityReferenceCollection = new IdentityReferenceCollection(1);
85 identityReferenceCollection.Add(this);
86 IdentityReferenceCollection identityReferenceCollection2 = Translate(identityReferenceCollection, targetType, forceSuccess: true);
87 return identityReferenceCollection2[0];
88 }
90 }
91
92 public override bool Equals([NotNullWhen(true)] object? o)
93 {
94 return this == o as NTAccount;
95 }
96
97 public override int GetHashCode()
98 {
99 return StringComparer.OrdinalIgnoreCase.GetHashCode(_name);
100 }
101
102 public override string ToString()
103 {
104 return _name;
105 }
106
107 internal static IdentityReferenceCollection Translate(IdentityReferenceCollection sourceAccounts, Type targetType, bool forceSuccess)
108 {
109 bool someFailed;
110 IdentityReferenceCollection identityReferenceCollection = Translate(sourceAccounts, targetType, out someFailed);
111 if (forceSuccess && someFailed)
112 {
113 IdentityReferenceCollection identityReferenceCollection2 = new IdentityReferenceCollection();
114 foreach (IdentityReference item in identityReferenceCollection)
115 {
116 if (item.GetType() != targetType)
117 {
118 identityReferenceCollection2.Add(item);
119 }
120 }
121 throw new IdentityNotMappedException(System.SR.IdentityReference_IdentityNotMapped, identityReferenceCollection2);
122 }
123 return identityReferenceCollection;
124 }
125
126 internal static IdentityReferenceCollection Translate(IdentityReferenceCollection sourceAccounts, Type targetType, out bool someFailed)
127 {
128 if (sourceAccounts == null)
129 {
130 throw new ArgumentNullException("sourceAccounts");
131 }
132 if (targetType == typeof(SecurityIdentifier))
133 {
134 return TranslateToSids(sourceAccounts, out someFailed);
135 }
137 }
138
139 public static bool operator ==(NTAccount? left, NTAccount? right)
140 {
141 if ((object)left == right)
142 {
143 return true;
144 }
145 if ((object)left == null || (object)right == null)
146 {
147 return false;
148 }
149 return left.ToString().Equals(right.ToString(), StringComparison.OrdinalIgnoreCase);
150 }
151
152 public static bool operator !=(NTAccount? left, NTAccount? right)
153 {
154 return !(left == right);
155 }
156
157 private static IdentityReferenceCollection TranslateToSids(IdentityReferenceCollection sourceAccounts, out bool someFailed)
158 {
159 if (sourceAccounts == null)
160 {
161 throw new ArgumentNullException("sourceAccounts");
162 }
163 if (sourceAccounts.Count == 0)
164 {
165 throw new ArgumentException(System.SR.Arg_EmptyCollection, "sourceAccounts");
166 }
167 SafeLsaPolicyHandle safeLsaPolicyHandle = null;
168 SafeLsaMemoryHandle referencedDomains = null;
169 SafeLsaMemoryHandle sids = null;
170 try
171 {
172 global::Interop.Advapi32.MARSHALLED_UNICODE_STRING[] array = new global::Interop.Advapi32.MARSHALLED_UNICODE_STRING[sourceAccounts.Count];
173 int num = 0;
174 foreach (IdentityReference sourceAccount in sourceAccounts)
175 {
176 if (!(sourceAccount is NTAccount nTAccount))
177 {
178 throw new ArgumentException(System.SR.Argument_ImproperType, "sourceAccounts");
179 }
180 array[num].Buffer = nTAccount.ToString();
181 if (array[num].Buffer.Length * 2 + 2 > 65535)
182 {
183 throw new InvalidOperationException();
184 }
185 array[num].Length = (ushort)(array[num].Buffer.Length * 2);
186 array[num].MaximumLength = (ushort)(array[num].Length + 2);
187 num++;
188 }
189 safeLsaPolicyHandle = Win32.LsaOpenPolicy(null, PolicyRights.POLICY_LOOKUP_NAMES);
190 someFailed = false;
191 uint num2 = global::Interop.Advapi32.LsaLookupNames2(safeLsaPolicyHandle, 0, sourceAccounts.Count, array, out referencedDomains, out sids);
192 switch (num2)
193 {
194 case 3221225495u:
195 case 3221225626u:
196 throw new OutOfMemoryException();
197 case 3221225506u:
198 throw new UnauthorizedAccessException();
199 case 3221225587u:
200 case 263u:
201 someFailed = true;
202 break;
203 default:
204 {
205 uint error = global::Interop.Advapi32.LsaNtStatusToWinError(num2);
206 _ = 1789;
207 throw new Win32Exception((int)error);
208 }
209 case 0u:
210 break;
211 }
212 IdentityReferenceCollection identityReferenceCollection = new IdentityReferenceCollection(sourceAccounts.Count);
213 if (num2 == 0 || num2 == 263)
214 {
215 sids.Initialize((uint)sourceAccounts.Count, (uint)Marshal.SizeOf<global::Interop.LSA_TRANSLATED_SID2>());
216 Win32.InitializeReferencedDomainsPointer(referencedDomains);
217 global::Interop.LSA_TRANSLATED_SID2[] array2 = new global::Interop.LSA_TRANSLATED_SID2[sourceAccounts.Count];
218 sids.ReadArray(0uL, array2, 0, array2.Length);
219 for (int i = 0; i < sourceAccounts.Count; i++)
220 {
221 global::Interop.LSA_TRANSLATED_SID2 lSA_TRANSLATED_SID = array2[i];
222 switch ((SidNameUse)lSA_TRANSLATED_SID.Use)
223 {
224 case SidNameUse.User:
225 case SidNameUse.Group:
226 case SidNameUse.Alias:
227 case SidNameUse.WellKnownGroup:
228 case SidNameUse.Computer:
229 identityReferenceCollection.Add(new SecurityIdentifier(lSA_TRANSLATED_SID.Sid));
230 break;
231 default:
232 someFailed = true;
233 identityReferenceCollection.Add(sourceAccounts[i]);
234 break;
235 }
236 }
237 }
238 else
239 {
240 for (int j = 0; j < sourceAccounts.Count; j++)
241 {
242 identityReferenceCollection.Add(sourceAccounts[j]);
243 }
244 }
245 return identityReferenceCollection;
246 }
247 finally
248 {
249 safeLsaPolicyHandle?.Dispose();
250 referencedDomains?.Dispose();
251 sids?.Dispose();
252 }
253 }
254}
static int SizeOf(object structure)
Definition Marshal.cs:697
static string IdentityReference_AccountNameTooLong
Definition SR.cs:34
static string IdentityReference_IdentityNotMapped
Definition SR.cs:44
static string Argument_StringZeroLength
Definition SR.cs:882
static string Argument_ImproperType
Definition SR.cs:20
static string IdentityReference_MustBeIdentityReference
Definition SR.cs:50
static string IdentityReference_DomainNameTooLong
Definition SR.cs:38
static string Arg_EmptyCollection
Definition SR.cs:14
Definition SR.cs:7
static bool operator!=(NTAccount? left, NTAccount? right)
Definition NTAccount.cs:152
static IdentityReferenceCollection Translate(IdentityReferenceCollection sourceAccounts, Type targetType, bool forceSuccess)
Definition NTAccount.cs:107
override bool IsValidTargetType(Type targetType)
Definition NTAccount.cs:59
override bool Equals([NotNullWhen(true)] object? o)
Definition NTAccount.cs:92
NTAccount(string domainName, string accountName)
Definition NTAccount.cs:14
static IdentityReferenceCollection Translate(IdentityReferenceCollection sourceAccounts, Type targetType, out bool someFailed)
Definition NTAccount.cs:126
static bool operator==(NTAccount? left, NTAccount? right)
Definition NTAccount.cs:139
static IdentityReferenceCollection TranslateToSids(IdentityReferenceCollection sourceAccounts, out bool someFailed)
Definition NTAccount.cs:157
override IdentityReference Translate(Type targetType)
Definition NTAccount.cs:72
static unsafe void InitializeReferencedDomainsPointer(SafeLsaMemoryHandle referencedDomains)
Definition Win32.cs:101
static SafeLsaPolicyHandle LsaOpenPolicy(string systemName, PolicyRights rights)
Definition Win32.cs:9
static StringComparer OrdinalIgnoreCase