protected override ADObjectId GetCookieContainerId()
 {
     if (MserveTargetConnection.configUnitsId == null)
     {
         MserveTargetConnection.configUnitsId = MserveTargetConnection.GetCookieContainerId(this.ConfigSession);
     }
     return(MserveTargetConnection.configUnitsId);
 }
        public void UpdateRecipientSyncStateValue(RecipientSyncOperation operation)
        {
            Dictionary <string, HashSet <string> > dictionary = new Dictionary <string, HashSet <string> >(MserveTargetConnection.ReplicationAddressAttributes.Length, StringComparer.OrdinalIgnoreCase);

            foreach (string text in MserveTargetConnection.ReplicationAddressAttributes)
            {
                string recipientSyncStateAttribute = MserveTargetConnection.GetRecipientSyncStateAttribute(operation.RecipientSyncState, text);
                dictionary[text] = RecipientSyncState.AddressHashSetFromConcatStringValue(recipientSyncStateAttribute);
            }
            foreach (OperationType key in operation.PendingSyncStateCommitEntries.Keys)
            {
                foreach (string text2 in operation.PendingSyncStateCommitEntries[key])
                {
                    string key2 = null;
                    if (!operation.AddressTypeTable.TryGetValue(text2, out key2))
                    {
                        throw new InvalidOperationException(text2 + " is not in AddressTypeTable");
                    }
                    switch (key)
                    {
                    case OperationType.Add:
                        if (!dictionary[key2].Contains(text2))
                        {
                            dictionary[key2].Add(text2);
                        }
                        break;

                    case OperationType.Delete:
                        if (dictionary[key2].Contains(text2))
                        {
                            dictionary[key2].Remove(text2);
                        }
                        break;
                    }
                }
            }
            foreach (string text3 in MserveTargetConnection.ReplicationAddressAttributes)
            {
                MserveTargetConnection.SetRecipientSyncStateAttribute(operation.RecipientSyncState, text3, RecipientSyncState.AddressHashSetToConcatStringValue(dictionary[text3]));
            }
        }
        public void FilterSmtpProxyAddressesBasedOnTenantSetting(ExSearchResultEntry entry, RecipientTypeDetails recipientTypeDetail)
        {
            DirectoryAttribute directoryAttribute = null;

            if (!entry.Attributes.TryGetValue("msExchCU", out directoryAttribute))
            {
                throw new ExDirectoryException("TenantCU is missing for the user", null);
            }
            if (directoryAttribute == null || directoryAttribute.Count <= 0)
            {
                throw new ExDirectoryException("TenantCU has empty value for the user", null);
            }
            string text = directoryAttribute[0] as string;

            if (string.IsNullOrEmpty(text))
            {
                throw new ExDirectoryException("TenantCU attribute is not string value", null);
            }
            ADObjectId tenantCUId = null;

            try
            {
                tenantCUId = new ADObjectId(text);
            }
            catch (FormatException e)
            {
                throw new ExDirectoryException("TenantCU DN is of invalid format as " + text, e);
            }
            bool flag = MserveTargetConnection.IsEntryMailEnabledUser(entry, recipientTypeDetail);

            MserveTargetConnection.TenantSyncControl tenantSyncControlAndUpdateCache = this.GetTenantSyncControlAndUpdateCache(tenantCUId);
            if ((flag && !tenantSyncControlAndUpdateCache.SyncMEUSMTPToMServ) || (!flag && !tenantSyncControlAndUpdateCache.SyncMailboxSMTPToMserv))
            {
                entry.Attributes["proxyAddresses"] = new DirectoryAttribute("proxyAddresses", MserveSynchronizationProvider.EmptyList);
            }
        }
        private static void OnAddressChange(DirectoryAttribute attribute, RecipientSyncOperation operation)
        {
            HashSet <string> hashSet = RecipientSyncState.AddressHashSetFromConcatStringValue(MserveTargetConnection.GetRecipientSyncStateAttribute(operation.RecipientSyncState, attribute.Name));

            foreach (object obj in attribute)
            {
                string text = (string)obj;
                string text2;
                if (text.StartsWith("smtp:", StringComparison.OrdinalIgnoreCase))
                {
                    text2 = text.Substring(5);
                }
                else if (text.StartsWith("meum:", StringComparison.OrdinalIgnoreCase))
                {
                    text2 = text.Substring(5);
                }
                else
                {
                    text2 = text;
                }
                if (!hashSet.Contains(text2))
                {
                    operation.AddedEntries.Add(text2);
                    operation.AddressTypeTable[text2] = attribute.Name;
                    ExTraceGlobals.TargetConnectionTracer.TraceDebug <string>(0L, "Add {0} to AddedEntries", text2);
                }
                else
                {
                    hashSet.Remove(text2);
                }
            }
            foreach (string text3 in hashSet)
            {
                operation.RemovedEntries.Add(text3);
                operation.AddressTypeTable[text3] = attribute.Name;
                ExTraceGlobals.TargetConnectionTracer.TraceDebug <string>(0L, "Add {0} to RemovedEntries", text3);
            }
        }
        protected List <RecipientSyncOperation> GetRecipientSyncOperation(ExSearchResultEntry entry)
        {
            List <RecipientSyncOperation> list = new List <RecipientSyncOperation>();

            ExTraceGlobals.TargetConnectionTracer.TraceDebug <string>(0L, "Try to GetRecipientSyncOperation for {0}", entry.DistinguishedName);
            RecipientSyncState recipientSyncState = null;

            if (entry.Attributes.ContainsKey("msExchExternalSyncState"))
            {
                byte[] bytes = Encoding.ASCII.GetBytes((string)entry.Attributes["msExchExternalSyncState"][0]);
                recipientSyncState = RecipientSyncState.DeserializeRecipientSyncState(bytes);
                ExTraceGlobals.TargetConnectionTracer.TraceDebug <string>(0L, "{0} has existing syncState", entry.DistinguishedName);
            }
            if (recipientSyncState == null)
            {
                if (entry.IsDeleted)
                {
                    ExTraceGlobals.TargetConnectionTracer.TraceDebug <string>(0L, "{0} is deleted entry without syncState. Ignore the entry", entry.DistinguishedName);
                    return(list);
                }
                ExTraceGlobals.TargetConnectionTracer.TraceDebug <string>(0L, "{0} is a normal entry without syncState. Creating one", entry.DistinguishedName);
                recipientSyncState = new RecipientSyncState();
            }
            if (!entry.IsDeleted)
            {
                int partnerId = MserveSynchronizationProvider.PartnerId;
                if (partnerId == -1)
                {
                    ExTraceGlobals.TargetConnectionTracer.TraceError <string>(0L, "Failed the sync because we could not get the partner Id for {0}", entry.DistinguishedName);
                    throw new ExDirectoryException(new ArgumentException("Failed the sync because we could not get the partner Id for " + entry.DistinguishedName));
                }
                int num = (recipientSyncState.PartnerId != 0) ? recipientSyncState.PartnerId : partnerId;
                recipientSyncState.PartnerId = partnerId;
                if (num != partnerId)
                {
                    ExTraceGlobals.TargetConnectionTracer.TraceDebug <string, int, int>(0L, "{0}'s partnerId changed from {1} to {2}", entry.DistinguishedName, num, partnerId);
                    base.LogSession.LogEvent(EdgeSyncLoggingLevel.Low, EdgeSyncEvent.TargetConnection, entry.DistinguishedName, "Warning: Changing partner ID from " + num.ToString() + " to " + partnerId.ToString());
                }
                else
                {
                    RecipientSyncOperation recipientSyncOperation = new RecipientSyncOperation(entry.DistinguishedName, partnerId, recipientSyncState, false);
                    ExTraceGlobals.TargetConnectionTracer.TraceDebug <int>(0L, "Create new operation with partner Id {0}", partnerId);
                    foreach (string key in MserveTargetConnection.ReplicationAddressAttributes)
                    {
                        if (entry.Attributes.ContainsKey(key))
                        {
                            MserveTargetConnection.OnAddressChange(entry.Attributes[key], recipientSyncOperation);
                        }
                    }
                    if (recipientSyncOperation.RemovedEntries.Count != 0 || recipientSyncOperation.AddedEntries.Count != 0)
                    {
                        list.Add(recipientSyncOperation);
                    }
                }
            }
            else
            {
                int partnerId2 = recipientSyncState.PartnerId;
                if (partnerId2 != 0)
                {
                    RecipientSyncOperation recipientSyncOperation2 = new RecipientSyncOperation(entry.DistinguishedName, partnerId2, null, true);
                    ExTraceGlobals.TargetConnectionTracer.TraceDebug <int>(0L, "Create remove operation with partner Id {0}", partnerId2);
                    foreach (string text in MserveTargetConnection.ReplicationAddressAttributes)
                    {
                        string recipientSyncStateAttribute = MserveTargetConnection.GetRecipientSyncStateAttribute(recipientSyncState, text);
                        if (recipientSyncStateAttribute != null)
                        {
                            List <string> list2 = RecipientSyncState.AddressToList(recipientSyncStateAttribute);
                            if (this.CanRemove(entry.DistinguishedName, text, list2))
                            {
                                foreach (string text2 in list2)
                                {
                                    recipientSyncOperation2.RemovedEntries.Add(text2);
                                    ExTraceGlobals.TargetConnectionTracer.TraceDebug <string>(0L, "Add {0} to RemovedEntries", text2);
                                }
                            }
                        }
                    }
                    if (recipientSyncOperation2.RemovedEntries.Count != 0)
                    {
                        list.Add(recipientSyncOperation2);
                    }
                }
                else
                {
                    ExTraceGlobals.TargetConnectionTracer.TraceDebug(0L, "No partner Id present on syncState. Skip the recipient");
                }
            }
            return(list);
        }
        private static FilterResult LoadAndFilter(ExSearchResultEntry entry, Connection sourceConnection, TargetConnection targetConnection)
        {
            if (entry.DistinguishedName.EndsWith(MserveSynchronizationProvider.rootDomainLostAndFoundContainerDN, StringComparison.OrdinalIgnoreCase))
            {
                return(FilterResult.Skip);
            }
            ExSearchResultEntry exSearchResultEntry = sourceConnection.ReadObjectEntry(entry.DistinguishedName, MserveSynchronizationProvider.RequiredAttributes);

            if (exSearchResultEntry == null)
            {
                return(FilterResult.Skip);
            }
            RecipientTypeDetails recipientTypeDetails = RecipientTypeDetails.None;
            DirectoryAttribute   directoryAttribute   = null;

            if (exSearchResultEntry.Attributes.TryGetValue("msExchRecipientTypeDetails", out directoryAttribute) && directoryAttribute != null && directoryAttribute.Count > 0)
            {
                try
                {
                    recipientTypeDetails = (RecipientTypeDetails)Enum.Parse(typeof(RecipientTypeDetails), directoryAttribute[0] as string, true);
                }
                catch (ArgumentException)
                {
                }
                catch (OverflowException)
                {
                }
            }
            if (recipientTypeDetails == RecipientTypeDetails.MailboxPlan || recipientTypeDetails == RecipientTypeDetails.RoleGroup)
            {
                return(FilterResult.Skip);
            }
            if (!entry.Attributes.ContainsKey("msExchExternalSyncState") && exSearchResultEntry.Attributes.ContainsKey("msExchExternalSyncState"))
            {
                entry.Attributes.Add("msExchExternalSyncState", exSearchResultEntry.Attributes["msExchExternalSyncState"]);
            }
            if (entry.Attributes.ContainsKey("msExchTransportRecipientSettingsFlags"))
            {
                if (!entry.Attributes.ContainsKey("proxyAddresses") && exSearchResultEntry.Attributes.ContainsKey("proxyAddresses"))
                {
                    entry.Attributes.Add("proxyAddresses", exSearchResultEntry.Attributes["proxyAddresses"]);
                }
                if (!entry.Attributes.ContainsKey("msExchSignupAddresses") && exSearchResultEntry.Attributes.ContainsKey("msExchSignupAddresses"))
                {
                    entry.Attributes.Add("msExchSignupAddresses", exSearchResultEntry.Attributes["msExchSignupAddresses"]);
                }
                if (!entry.Attributes.ContainsKey("msExchUMAddresses") && exSearchResultEntry.Attributes.ContainsKey("msExchUMAddresses"))
                {
                    entry.Attributes.Add("msExchUMAddresses", exSearchResultEntry.Attributes["msExchUMAddresses"]);
                }
                if (!entry.Attributes.ContainsKey("msExchArchiveGUID") && exSearchResultEntry.Attributes.ContainsKey("msExchArchiveGUID"))
                {
                    entry.Attributes.Add("msExchArchiveGUID", exSearchResultEntry.Attributes["msExchArchiveGUID"]);
                }
            }
            if (exSearchResultEntry.Attributes.ContainsKey("msExchTransportRecipientSettingsFlags"))
            {
                entry.Attributes["msExchTransportRecipientSettingsFlags"] = exSearchResultEntry.Attributes["msExchTransportRecipientSettingsFlags"];
            }
            if (exSearchResultEntry.Attributes.ContainsKey("msExchCU"))
            {
                entry.Attributes["msExchCU"] = exSearchResultEntry.Attributes["msExchCU"];
            }
            if (exSearchResultEntry.Attributes.ContainsKey("mailNickname"))
            {
                entry.Attributes["mailNickname"] = exSearchResultEntry.Attributes["mailNickname"];
            }
            if (exSearchResultEntry.Attributes.ContainsKey("msExchHomeServerName"))
            {
                entry.Attributes["msExchHomeServerName"] = exSearchResultEntry.Attributes["msExchHomeServerName"];
            }
            bool flag = false;
            int  num;

            if (entry.Attributes.ContainsKey("msExchTransportRecipientSettingsFlags") && entry.Attributes["msExchTransportRecipientSettingsFlags"].Count != 0 && int.TryParse((string)entry.Attributes["msExchTransportRecipientSettingsFlags"][0], NumberStyles.Number, CultureInfo.InvariantCulture, out num) && (num & 8) != 0)
            {
                bool flag2 = (num & 64) != 0;
                foreach (string text in MserveSynchronizationProvider.AddressAttributeNames)
                {
                    if (text.Equals("msExchArchiveGUID"))
                    {
                        if (!flag2)
                        {
                            entry.Attributes["msExchArchiveGUID"] = new DirectoryAttribute("msExchArchiveGUID", MserveSynchronizationProvider.EmptyList);
                        }
                    }
                    else if (entry.Attributes.ContainsKey(text))
                    {
                        DirectoryAttribute directoryAttribute2 = entry.Attributes[text];
                        entry.Attributes[text] = new DirectoryAttribute(directoryAttribute2.Name, MserveSynchronizationProvider.EmptyList);
                        flag = text.Equals("proxyAddresses", StringComparison.OrdinalIgnoreCase);
                    }
                }
            }
            if (!flag)
            {
                MserveTargetConnection mserveTargetConnection = targetConnection as MserveTargetConnection;
                if (targetConnection == null)
                {
                    throw new InvalidOperationException("targetConnection is not the type of MserveTargetConnection");
                }
                mserveTargetConnection.FilterSmtpProxyAddressesBasedOnTenantSetting(entry, recipientTypeDetails);
            }
            return(FilterResult.None);
        }