-
Notifications
You must be signed in to change notification settings - Fork 0
/
GALMV.cs
359 lines (295 loc) · 11.2 KB
/
GALMV.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
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
//=================================================================
// Copyright (c) Microsoft Corporation. All rights reserved.
//=================================================================
using Microsoft.MetadirectoryServices;
//using Microsoft.MetadirectoryServices.LoggingCs;
namespace MIM.Sync.GALSyncCs
{
public class MVSynchronizer : GALSyncCs.Synchronizer, IMVSynchronization
{
// dkegg - 17 Jan 2016 - declaring all customizer variables as global
bool useHubAndSpoke;
string HubDomainName;
string HubMAName;
bool UseServiceRoutingAddressAsTarget;
bool RetainExchangeLabsX500;
//
// AD_RDN_MAX_SIZE only counts for rdn value, not type
//
private const int AD_RDN_MAX_SIZE = 64;
private const int RETRY_NUM_LIMIT = 1000;
private const string RDN_TYPE = "CN=";
public void Initialize()
{
//
// Fetch configuration data before any other operation
//
GetConfigurationData();
}
public void Terminate()
{
//
// We don't need to release any resources
//
}
public void Provision(MVEntry mventry)
{
int i = 0;
CSEntry MasterConnector = null;
ConnectedMA MA = default(ConnectedMA);
//LoggingCs.Log("Entering provisioning for " + mventry.ToString());
foreach ( ConnectedMA ConnMA in mventry.ConnectedMAs) {
//CSEntry csentry = default(CSEntry);
foreach ( CSEntry cseentry in ConnMA.Connectors) {
if (cseentry.ConnectionRule == RuleType.Projection) {
MasterConnector = cseentry;
}
}
}
//
// For every MA, try to add a csentry, if there is not already one
//
for (i = 0; i <= galMAs.Length - 1; i++) {
MA = mventry.ConnectedMAs[galMAs[i].MAName];
if (0 == MA.Connectors.Count) {
//
// If there were no connectors, then we are going to add one
AddOrRenameConnector(ref MA, ref galMAs[i], mventry);
} else if (1 == MA.Connectors.Count) {
//
// If there is one connector,
// - if it is the master object connector then it is ok.
// - if it is a replica object, then check for rename.
// - if it is a join object outside the Synchronization
// OU, then it is a problem, log it
//
CSEntry csentry = MA.Connectors.ByIndex[0];
if (IsInSynchronizationOU(csentry)) {
AddOrRenameConnector(ref MA, ref galMAs[i], mventry, csentry);
} else {
if ((!object.ReferenceEquals(csentry, MasterConnector))) {
//
// This object has joined.
//
string LogString = "A contact for this object " + MasterConnector.ToString() + " called contact " + csentry.ToString() + " already exists in forest represented by MA " + MA.Name + ". If you would like to preserve this " + "contact and have us manage it, please " + "move the contact into Synchronization OU. " + "If you would like us to create " + "a new contact and manage it, " + "please delete this one.";
//LoggingCs.Log(LogString);
}
}
} else {
//
// We have more than one connectors undert the same MA,
// print an error message.
//
CSEntry csentry = default(CSEntry);
int index = 0;
int countCsRemaining = 0;
bool contactOutsideSyncOU = false;
string LogString = "Multiple or outside-synchronizaiton-OU " + "connector(s) for the MV object " + MasterConnector.ToString() + "exist, they are: ";
for (index = MA.Connectors.Count - 1; index >= 0; index += -1) {
csentry = MA.Connectors.ByIndex[index];
if (csentry.ConnectionRule == RuleType.Provisioning) {
//LoggingCs.Log("Disconnecting provisioned " + csentry.ToString() );
csentry.Deprovision();
} else {
countCsRemaining = countCsRemaining + 1;
LogString = LogString + csentry.ToString() + " lives in forest connected by " + MA.Name + " ";
if (!IsInSynchronizationOU(csentry) && csentry.ObjectType == CONTACT) {
contactOutsideSyncOU = true;
}
}
}
//
// If we end up with more than one connector, or
// any contact outside synchronization OU,
// we want to log a warning message
//
if ((countCsRemaining > 1) || (true == contactOutsideSyncOU)) {
LogString = LogString + ". Please refer to documentation " + "to resolve the conflict.";
//LoggingCs.Log(LogString);
}
}
}
}
public bool DetermineMVDeletion(CSEntry csentry, MVEntry mventry)
{
bool ShouldDeleteFromMV = false;
if (IsUnknownObjectType(csentry)) {
LogAndThrowUnexpectedDataException("Unhandled object type in DetermineMVDeletion " + "called with csentry " + csentry.ToString() + " and mventry " + mventry.ToString());
}
//
// Delete the mv object iff the deleted object is the master object
//
// replaced Switch Statement with IFELSE tree for C# runtime variable checking
if (csentry.ObjectType == USER || csentry.ObjectType == GROUP)
{
ShouldDeleteFromMV = true;
}
else if (csentry.ObjectType == CONTACT)
{
if ((GetContactType(csentry) == null))
{
ShouldDeleteFromMV = false;
}
else
{
ShouldDeleteFromMV = mventry.ObjectType.Equals(GetContactType(csentry));
}
}
else
{
LogAndThrowUnexpectedDataException("Unhandled object type in DetermineMVDeletion " + "called with csentry " + csentry.ToString() + " and mventry " + mventry.ToString());
}
return ShouldDeleteFromMV;
}
bool IMVSynchronization.ShouldDeleteFromMV(CSEntry csentry, MVEntry mventry)
{
return DetermineMVDeletion(csentry, mventry);
}
private void AddOrRenameConnector(ref ConnectedMA MA, ref GALMA MAConfig, MVEntry mventry, CSEntry csentry = null)
{
//
// All objects are provisioned as contacts
//
string cn = null;
int numberToAppend = 1;
bool successful = false;
bool extendedNameTried = false;
string extendedName = null;
string adminGroup = null;
bool provisioningAdd = false;
int cnLengthMax = 0;
string validatedName = null;
//
// Add or Rename if only SynchronizationOU is defined
//
if ((MAConfig.SynchronizationOU == null) || MAConfig.SynchronizationOU.Equals("")) {
return;
}
if (!mventry[COMMON_NAME].IsPresent || !mventry[MAIL_NICK_NAME].IsPresent || !mventry[TARGET_ADDRESS].IsPresent) {
LogAndThrowUnexpectedDataException("Provisioning without cn, mailNickName or targetAddress");
}
if (null == csentry) {
provisioningAdd = true;
}
cn = mventry[COMMON_NAME].Value.ToString();
//
// Active Directory does not distinguish CNs that differ only in use of diacritical marks (accents) etc.
// whereas the sync service does. So force uniqueness by appending mailnickname to all CNs with extended
// chars if doing so does not exceed CN max length.
//
IEnumerator cnEnum = cn.GetEnumerator();
while (cnEnum.MoveNext()) {
if (Strings.AscW(cnEnum.Current.ToString()) > 127 && cn.Length + mventry[MAIL_NICK_NAME].Value.ToString().Length + 2 + RDN_TYPE.Length < AD_RDN_MAX_SIZE) {
cn = cn + "(" + mventry[MAIL_NICK_NAME].Value.ToString() + ")";
break; // TODO: might not be correct. Was : Exit While
}
}
do {
try {
//
// Create a DN for the new object, need UPPER case "CN=..."
//
string rdn = RDN_TYPE + cn;
ReferenceValue dn = MA.EscapeDNComponent(rdn).Concat(MAConfig.SynchronizationOU);
if (rdn.Length > AD_RDN_MAX_SIZE + RDN_TYPE.Length) {
LogAndThrowUnexpectedDataException("RDN too long: " + rdn);
}
if (csentry == null) {
//
// Try to add the object
//
//LoggingCs.Log("Adding " + dn.ToString());
csentry = ExchangeUtils.CreateMailEnabledContact(MA, dn, mventry[MAIL_NICK_NAME].Value.ToString(), mventry[TARGET_ADDRESS].Value.ToString());
adminGroup = GetAdminGroup(csentry);
if ((adminGroup != null)) {
//
// LegacyExhangeDN = adminGroup/cn=mailnickname-guid
//
validatedName = ValidateLegacyExhangeDN(mventry[MAIL_NICK_NAME].Value.ToCharArray());
if ((validatedName == null)) {
csentry[LEGACY_EXCHANGE_DN].Value = adminGroup + "/cn=" + System.Guid.NewGuid().ToString();
} else {
csentry[LEGACY_EXCHANGE_DN].Value = adminGroup + "/cn=" + validatedName + "-" + System.Guid.NewGuid().ToString();
}
}
} else {
//
// Try to rename the object
//
if (!csentry.DN.Equals(dn)) {
//LoggingCs.Log("Renaming " + dn.ToString());
csentry.DN = dn;
}
}
successful = true;
} catch (MissingParentObjectException ex) {
//
// Typically the admin has to perform a full/delta import
// on the target CD, or disable provisioning until all
// forests are imported.
//
//LoggingCs.Log("Target MA " + MA.Name + " is not imported yet. " + "Please disable provisioning until all forests " + "are imported.");
throw ex;
} catch (ObjectAlreadyExistsException ex) {
//
// If adding connector, throw away the instance to start over
//
if (provisioningAdd) {
csentry = null;
}
//
// There is a duplicate object in the target AD,
// change the cn accordingly to avoid conflict.
//
if (!extendedNameTried) {
extendedNameTried = true;
try {
if (mventry[DEPARTMENT].IsPresent) {
extendedName = mventry[DEPARTMENT].Value;
}
} catch (NoSuchAttributeInObjectTypeException ex2) {
}
}
cn = null;
if (extendedName != null) {
cn = mventry[COMMON_NAME].Value + " (" + extendedName + ")";
extendedName = null;
if (cn.Length > AD_RDN_MAX_SIZE) {
//
// If too long, we'll try without it
//
cn = null;
}
}
if (null == cn) {
cn = mventry[COMMON_NAME].Value;
//
// To make sure that the number appended
// will not be truncated.
// The 2 spaces reserved is for "()"
//
cnLengthMax = AD_RDN_MAX_SIZE - (numberToAppend.ToString().Length + 2);
//
// If it's too long, we are going to truncate the
// name and preserve the number appended.
//
if (cn.Length > cnLengthMax) {
cn = cn.Substring(0, cnLengthMax);
}
cn = cn + "(" + numberToAppend.ToString() + ")";
numberToAppend = numberToAppend + 1;
if (numberToAppend > RETRY_NUM_LIMIT) {
LogAndThrowUnexpectedDataException("Retry for " + mventry[COMMON_NAME].Value + " exceeds limit " + numberToAppend.ToString());
}
}
}
} while (!successful);
}
}
}