/
ActiveDirectoryInterSiteTransport.cs
363 lines (310 loc) · 13.6 KB
/
ActiveDirectoryInterSiteTransport.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
// 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.ComponentModel;
namespace System.DirectoryServices.ActiveDirectory
{
public class ActiveDirectoryInterSiteTransport : IDisposable
{
private readonly DirectoryContext _context;
private readonly DirectoryEntry _cachedEntry;
private readonly ActiveDirectoryTransportType _transport;
private bool _disposed;
private bool _linkRetrieved;
private bool _bridgeRetrieved;
private readonly ReadOnlySiteLinkCollection _siteLinkCollection = new ReadOnlySiteLinkCollection();
private readonly ReadOnlySiteLinkBridgeCollection _bridgeCollection = new ReadOnlySiteLinkBridgeCollection();
internal ActiveDirectoryInterSiteTransport(DirectoryContext context, ActiveDirectoryTransportType transport, DirectoryEntry entry)
{
_context = context;
_transport = transport;
_cachedEntry = entry;
}
public static ActiveDirectoryInterSiteTransport FindByTransportType(DirectoryContext context, ActiveDirectoryTransportType transport)
{
ArgumentNullException.ThrowIfNull(context);
// if target is not specified, then we determin the target from the logon credential, so if it is a local user context, it should fail
if ((context.Name == null) && (!context.isRootDomain()))
{
throw new ArgumentException(SR.ContextNotAssociatedWithDomain, nameof(context));
}
// more validation for the context, if the target is not null, then it should be either forest name or server name
if (context.Name != null)
{
if (!(context.isRootDomain() || context.isServer() || context.isADAMConfigSet()))
throw new ArgumentException(SR.NotADOrADAM, nameof(context));
}
if (transport < ActiveDirectoryTransportType.Rpc || transport > ActiveDirectoryTransportType.Smtp)
throw new InvalidEnumArgumentException("value", (int)transport, typeof(ActiveDirectoryTransportType));
// work with copy of the context
context = new DirectoryContext(context);
// bind to the rootdse to get the configurationnamingcontext
DirectoryEntry de;
try
{
de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
string config = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ConfigurationNamingContext)!;
string containerDN = "CN=Inter-Site Transports,CN=Sites," + config;
if (transport == ActiveDirectoryTransportType.Rpc)
containerDN = "CN=IP," + containerDN;
else
containerDN = "CN=SMTP," + containerDN;
de = DirectoryEntryManager.GetDirectoryEntry(context, containerDN);
}
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
{
de.RefreshCache(s_options);
}
catch (COMException e)
{
if (e.ErrorCode == unchecked((int)0x80072030))
{
// if it is ADAM and transport type is SMTP, throw NotSupportedException.
DirectoryEntry tmpDE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
if (Utils.CheckCapability(tmpDE, Capability.ActiveDirectoryApplicationMode) && transport == ActiveDirectoryTransportType.Smtp)
{
throw new NotSupportedException(SR.NotSupportTransportSMTP);
}
throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.TransportNotFound, transport.ToString()), typeof(ActiveDirectoryInterSiteTransport), transport.ToString());
}
else
throw ExceptionHelper.GetExceptionFromCOMException(context, e);
}
return new ActiveDirectoryInterSiteTransport(context, transport, de);
}
public ActiveDirectoryTransportType TransportType
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
return _transport;
}
}
public bool IgnoreReplicationSchedule
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
int option = 0;
try
{
if (_cachedEntry.Properties.Contains("options"))
option = (int)_cachedEntry.Properties["options"][0]!;
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(_context, e);
}
// NTDSTRANSPORT_OPT_IGNORE_SCHEDULES ( 1 << 0 ) Schedules disabled
if ((option & 0x1) != 0)
return true;
else
return false;
}
set
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
int option = 0;
try
{
if (_cachedEntry.Properties.Contains("options"))
option = (int)_cachedEntry.Properties["options"][0]!;
// NTDSTRANSPORT_OPT_IGNORE_SCHEDULES ( 1 << 0 ) Schedules disabled
if (value)
option |= 0x1;
else
option &= (~(0x1));
_cachedEntry.Properties["options"].Value = option;
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(_context, e);
}
}
}
public bool BridgeAllSiteLinks
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
int option = 0;
try
{
if (_cachedEntry.Properties.Contains("options"))
option = (int)_cachedEntry.Properties["options"][0]!;
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(_context, e);
}
// NTDSTRANSPORT_OPT_BRIDGES_REQUIRED (1 << 1 ) siteLink bridges are required
// That is to say, if this bit is set, it means that all site links are not bridged and user needs to create specific bridge
if ((option & 0x2) != 0)
return false;
else
return true;
}
set
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
int option = 0;
try
{
if (_cachedEntry.Properties.Contains("options"))
option = (int)_cachedEntry.Properties["options"][0]!;
// NTDSTRANSPORT_OPT_BRIDGES_REQUIRED (1 << 1 ) siteLink bridges are required, all site links are not bridged
// That is to say, if this bit is set, it means that all site links are not bridged and user needs to create specific bridge
// if this bit is not set, all the site links are bridged
if (value)
option &= (~(0x2));
else
option |= 0x2;
_cachedEntry.Properties["options"].Value = option;
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(_context, e);
}
}
}
public ReadOnlySiteLinkCollection SiteLinks
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (!_linkRetrieved)
{
_siteLinkCollection.Clear();
ADSearcher adSearcher = new ADSearcher(_cachedEntry,
"(&(objectClass=siteLink)(objectCategory=SiteLink))",
s_cn,
SearchScope.OneLevel);
SearchResultCollection? results = null;
try
{
results = adSearcher.FindAll();
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(_context, e);
}
try
{
foreach (SearchResult result in results)
{
DirectoryEntry connectionEntry = result.GetDirectoryEntry();
string cn = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.Cn)!;
ActiveDirectorySiteLink link = new ActiveDirectorySiteLink(_context, cn, _transport, true, connectionEntry);
_siteLinkCollection.Add(link);
}
}
finally
{
results.Dispose();
}
_linkRetrieved = true;
}
return _siteLinkCollection;
}
}
public ReadOnlySiteLinkBridgeCollection SiteLinkBridges
{
get
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
if (!_bridgeRetrieved)
{
_bridgeCollection.Clear();
ADSearcher adSearcher = new ADSearcher(_cachedEntry,
"(&(objectClass=siteLinkBridge)(objectCategory=SiteLinkBridge))",
s_cn,
SearchScope.OneLevel);
SearchResultCollection? results = null;
try
{
results = adSearcher.FindAll();
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(_context, e);
}
try
{
foreach (SearchResult result in results)
{
DirectoryEntry connectionEntry = result.GetDirectoryEntry();
string cn = (string)PropertyManager.GetSearchResultPropertyValue(result, PropertyManager.Cn)!;
ActiveDirectorySiteLinkBridge bridge = new ActiveDirectorySiteLinkBridge(_context, cn, _transport, true);
bridge.cachedEntry = connectionEntry;
_bridgeCollection.Add(bridge);
}
}
finally
{
results.Dispose();
}
_bridgeRetrieved = true;
}
return _bridgeCollection;
}
}
private static readonly string[] s_options = new string[] { "options" };
private static readonly string[] s_cn = new string[] { "cn" };
public void Save()
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
try
{
_cachedEntry.CommitChanges();
}
catch (COMException e)
{
throw ExceptionHelper.GetExceptionFromCOMException(_context, e);
}
}
public DirectoryEntry GetDirectoryEntry()
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
return DirectoryEntryManager.GetDirectoryEntryInternal(_context, _cachedEntry.Path);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public override string ToString()
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
return _transport.ToString();
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// free other state (managed objects)
_cachedEntry?.Dispose();
}
// free your own state (unmanaged objects)
_disposed = true;
}
}
}