private MapiStore ConnectToTargetMailbox(bool mailboxMustExist, string serverLegDN, string serverFqdn, MailboxConnectFlags mailboxConnectFlags)
        {
            MrsTracer.Provider.Function("MapiDestinationMailbox.ConnectToTargetMailbox", new object[0]);
            base.CheckDisposed();
            MapiStore     mapiStore     = null;
            Guid          guidMdb       = Guid.Empty;
            string        text          = null;
            bool          flag          = false;
            OpenStoreFlag openStoreFlag = OpenStoreFlag.UseAdminPrivilege | OpenStoreFlag.TakeOwnership | OpenStoreFlag.OverrideHomeMdb | OpenStoreFlag.FailIfNoMailbox | OpenStoreFlag.NoLocalization | OpenStoreFlag.MailboxGuid | OpenStoreFlag.DisconnectedMailbox;

            if (base.MbxType == MailboxType.DestMailboxCrossOrg)
            {
                openStoreFlag |= OpenStoreFlag.XForestMove;
            }
            ConnectFlag connectFlag = ConnectFlag.UseAdminPrivilege;

            if (base.Credential == null)
            {
                connectFlag |= ConnectFlag.UseRpcContextPool;
            }
            if (!mailboxMustExist)
            {
                bool flag2;
                for (;;)
                {
                    flag2 = false;
                    MrsTracer.Provider.Debug("Checking if destination mailbox exists...", new object[0]);
                    using (ExRpcAdmin rpcAdmin = base.GetRpcAdmin())
                    {
                        try
                        {
                            using (base.RHTracker.Start())
                            {
                                flag2 = MapiUtils.IsMailboxInDatabase(rpcAdmin, base.MdbGuid, base.MailboxGuid);
                            }
                        }
                        catch (MapiExceptionMdbOffline)
                        {
                            if (!flag)
                            {
                                MrsTracer.Provider.Debug("GetMailboxTableInfo returned MdbOffline, forcing AM rediscovery", new object[0]);
                                base.ResolveMDB(true);
                                serverLegDN = base.ServerDN;
                                serverFqdn  = base.ServerFqdn;
                                flag        = true;
                                continue;
                            }
                            throw;
                        }
                    }
                    break;
                }
                if (!flag2)
                {
                    MrsTracer.Provider.Debug("Mailbox {0} does not exist in database {1}", new object[]
                    {
                        base.MailboxGuid,
                        base.MdbGuid
                    });
                    return(null);
                }
                MrsTracer.Provider.Debug("Mailbox {0} exists in database {1}", new object[]
                {
                    base.MailboxGuid,
                    base.MdbGuid
                });
            }
            string userName;
            string domainName;
            string password;

            base.GetCreds(out userName, out domainName, out password);
            if (base.IsTitanium)
            {
                if (base.MbxType == MailboxType.DestMailboxCrossOrg && mailboxMustExist)
                {
                    base.RunADRecipientOperation(false, delegate(IRecipientSession adSession)
                    {
                        ADUser aduser = adSession.Read(base.MailboxId) as ADUser;
                        if (aduser == null)
                        {
                            throw new RecipientNotFoundPermanentException(base.MailboxGuid);
                        }
                        if (aduser.Database == null || !aduser.Database.Equals(base.MdbId))
                        {
                            MrsTracer.Provider.Debug("Stamping homeMDB on the destination Ti user", new object[0]);
                            aduser.Database = base.MdbId;
                            adSession.Save(aduser);
                            using (ExRpcAdmin rpcAdmin2 = base.GetRpcAdmin())
                            {
                                rpcAdmin2.PurgeCachedMailboxObject(base.MailboxGuid);
                            }
                        }
                    });
                }
            }
            else
            {
                guidMdb = base.MdbGuid;
                text    = guidMdb.ToString();
                if (mailboxMustExist && this.ServerVersion < Server.E14MinVersion)
                {
                    MrsTracer.Provider.Debug("E12 stores do not support open-by-mdb-guid functionality. Will use random mdb guid to connect to a newly created mailbox.", new object[0]);
                    guidMdb = Guid.NewGuid();
                    text    = "(random)";
                }
            }
            TimeSpan timeSpan;
            TimeSpan callTimeout;

            if (base.Credential != null)
            {
                timeSpan    = base.TestIntegration.RemoteMailboxConnectionTimeout;
                callTimeout = base.TestIntegration.RemoteMailboxCallTimeout;
            }
            else
            {
                timeSpan    = base.TestIntegration.LocalMailboxConnectionTimeout;
                callTimeout = base.TestIntegration.LocalMailboxCallTimeout;
            }
            string applicationId = "Client=MSExchangeMigration";

            if ((mailboxConnectFlags & MailboxConnectFlags.PublicFolderHierarchyReplication) != MailboxConnectFlags.None)
            {
                applicationId = "Client=PublicFolderSystem;Action=PublicFolderHierarchyReplication";
            }
            for (;;)
            {
                string text2 = (!string.IsNullOrEmpty(serverFqdn)) ? serverFqdn : serverLegDN;
                try
                {
                    using (base.RHTracker.Start())
                    {
                        ConnectFlag connectFlag2 = connectFlag;
                        if (base.IsTitanium)
                        {
                            connectFlag2 |= ConnectFlag.AllowLegacyStore;
                            MrsTracer.Provider.Debug("Opening Ti MapiStore: serverDN='{0}', mailboxDN='{1}', connectFlags=[{2}], openStoreFlags=[{3}], timeout={4}", new object[]
                            {
                                text2,
                                base.MailboxDN,
                                connectFlag2,
                                openStoreFlag,
                                timeSpan
                            });
                            mapiStore = MapiStore.OpenMailbox(text2, Server.GetSystemAttendantLegacyDN(serverLegDN), base.MailboxDN, userName, domainName, password, null, connectFlag2, openStoreFlag, null, null, applicationId, timeSpan, callTimeout);
                        }
                        else
                        {
                            if (base.IsExchange2007)
                            {
                                connectFlag2 |= ConnectFlag.AllowLegacyStore;
                            }
                            MrsTracer.Provider.Debug("Opening MapiStore: serverDN='{0}', mailbox='{1}', mailboxGuid={2}, dbGuid={3}, connectFlags=[{4}], openStoreFlags=[{5}], timeout={6}", new object[]
                            {
                                text2,
                                base.TraceMailboxId,
                                base.MailboxGuid.ToString(),
                                text,
                                connectFlag2,
                                openStoreFlag,
                                timeSpan
                            });
                            mapiStore = MapiStore.OpenMailbox(text2, Server.GetSystemAttendantLegacyDN(serverLegDN), base.MailboxGuid, guidMdb, userName, domainName, password, connectFlag2, openStoreFlag, null, null, applicationId, timeSpan, callTimeout, null);
                        }
                        MapiUtils.StartMapiDeadSessionChecking(mapiStore, base.TraceMailboxId);
                    }
                }
                catch (MapiExceptionNotFound originalException)
                {
                    base.VerifyMdbIsOnline(originalException);
                    if (mailboxMustExist)
                    {
                        throw;
                    }
                }
                catch (MapiExceptionWrongServer originalException2)
                {
                    if (base.IsTitanium)
                    {
                        base.VerifyMdbIsOnline(originalException2);
                        if (mailboxMustExist)
                        {
                            throw;
                        }
                    }
                    else
                    {
                        if (!flag)
                        {
                            MrsTracer.Provider.Debug("OpenMailbox returned WrongServer, forcing AM rediscovery", new object[0]);
                            base.ResolveMDB(true);
                            serverLegDN = base.ServerDN;
                            serverFqdn  = base.ServerFqdn;
                            flag        = true;
                            continue;
                        }
                        throw;
                    }
                }
                catch (MapiExceptionLogonFailed originalException3)
                {
                    if (!base.IsTitanium && !flag)
                    {
                        MrsTracer.Provider.Debug("OpenMailbox returned LogonFailed, forcing AM rediscovery", new object[0]);
                        base.ResolveMDB(true);
                        serverLegDN = base.ServerDN;
                        serverFqdn  = base.ServerFqdn;
                        flag        = true;
                        continue;
                    }
                    base.VerifyMdbIsOnline(originalException3);
                    if (mailboxMustExist)
                    {
                        throw;
                    }
                }
                catch (MapiExceptionMailboxInTransit innerException)
                {
                    throw this.GetMailboxInTransitException(innerException);
                }
                break;
            }
            return(mapiStore);
        }
Esempio n. 2
0
        private StoreSession ConnectToTargetMailbox(bool mailboxMustExist, MailboxConnectFlags mailboxConnectFlags)
        {
            MrsTracer.Provider.Function("StorageDestinationMailbox.ConnectToTargetMailbox", new object[0]);
            base.CheckDisposed();
            StoreSession result      = null;
            ConnectFlag  connectFlag = ConnectFlag.UseAdminPrivilege;

            connectFlag |= ConnectFlag.UseRpcContextPool;
            if (!mailboxMustExist)
            {
                bool flag = false;
                MrsTracer.Provider.Debug("Checking if destination mailbox exists...", new object[0]);
                using (ExRpcAdmin rpcAdmin = base.GetRpcAdmin())
                {
                    using (base.RHTracker.Start())
                    {
                        flag = MapiUtils.IsMailboxInDatabase(rpcAdmin, base.MdbGuid, base.MailboxGuid);
                    }
                }
                if (!flag)
                {
                    MrsTracer.Provider.Debug("Mailbox {0} does not exist in database {1}", new object[]
                    {
                        base.MailboxGuid,
                        base.MdbGuid
                    });
                    return(null);
                }
                MrsTracer.Provider.Debug("Mailbox {0} exists in database {1}", new object[]
                {
                    base.MailboxGuid,
                    base.MdbGuid
                });
            }
            StoreSession storeSession = null;

            try
            {
                using (base.RHTracker.Start())
                {
                    DefaultFolderType[] foldersToInit = (DefaultFolderType[])Enum.GetValues(typeof(DefaultFolderType));
                    MailboxSession.InitializationFlags initializationFlags = MailboxSession.InitializationFlags.DefaultFolders | MailboxSession.InitializationFlags.UserConfigurationManager | MailboxSession.InitializationFlags.DeadSessionChecking | MailboxSession.InitializationFlags.RequestLocalRpc | MailboxSession.InitializationFlags.OverrideHomeMdb | MailboxSession.InitializationFlags.SuppressFolderIdPrefetch | MailboxSession.InitializationFlags.UseNamedProperties | MailboxSession.InitializationFlags.DisconnectedMailbox | MailboxSession.InitializationFlags.MoveUser;
                    if (base.MbxType == MailboxType.DestMailboxCrossOrg)
                    {
                        initializationFlags |= MailboxSession.InitializationFlags.XForestMove;
                    }
                    MrsTracer.Provider.Debug("Opening Mailbox Session: mailbox='{0}', mailboxGuid={1}, dbGuid={2}, connectFlags=[{3}], initFlags=[{4}]", new object[]
                    {
                        base.TraceMailboxId,
                        base.MailboxGuid.ToString(),
                        base.MdbGuid.ToString(),
                        connectFlag,
                        initializationFlags
                    });
                    OrganizationId organizationId;
                    if (base.PartitionHint != null)
                    {
                        ADSessionSettings adsessionSettings = ADSessionSettings.FromTenantPartitionHint(base.PartitionHint);
                        organizationId = adsessionSettings.CurrentOrganizationId;
                    }
                    else
                    {
                        organizationId = OrganizationId.ForestWideOrgId;
                    }
                    ExchangePrincipal exchangePrincipal = ExchangePrincipal.FromMailboxData(base.MailboxGuid, base.MdbGuid, organizationId, StorageMailbox.CultureInfos);
                    string            clientInfoString  = "Client=MSExchangeMigration";
                    if ((mailboxConnectFlags & MailboxConnectFlags.PublicFolderHierarchyReplication) != MailboxConnectFlags.None)
                    {
                        clientInfoString = "Client=PublicFolderSystem;Action=PublicFolderHierarchyReplication";
                    }
                    if (base.IsPublicFolderMailbox)
                    {
                        storeSession = PublicFolderSession.OpenAsMRS(exchangePrincipal, clientInfoString, OpenMailboxSessionFlags.None);
                    }
                    else
                    {
                        MailboxAccessInfo accessInfo = new MailboxAccessInfo(new WindowsPrincipal(WindowsIdentity.GetCurrent()));
                        storeSession = MailboxSession.ConfigurableOpen(exchangePrincipal, accessInfo, CultureInfo.InvariantCulture, clientInfoString, LogonType.SystemService, null, initializationFlags, foldersToInit);
                    }
                    if (!base.TestIntegration.DisableFolderCreationBlockFeature)
                    {
                        storeSession.BlockFolderCreation = true;
                    }
                    if (storeSession != null && base.Flags.HasFlag(LocalMailboxFlags.WordBreak))
                    {
                        bool invalidateAnnotations = base.Flags.HasFlag(LocalMailboxFlags.InvalidateContentIndexAnnotations);
                        storeSession.ContentIndexingSession = ContentIndexingSession.CreateSession(storeSession, invalidateAnnotations);
                    }
                    result       = storeSession;
                    storeSession = null;
                }
            }
            catch (Exception ex)
            {
                if (CommonUtils.ExceptionIs(ex, new WellKnownException[]
                {
                    WellKnownException.MapiNotFound
                }))
                {
                    base.VerifyMdbIsOnline(ex);
                    if (mailboxMustExist)
                    {
                        throw;
                    }
                }
                else
                {
                    if (CommonUtils.ExceptionIs(ex, new WellKnownException[]
                    {
                        WellKnownException.MapiMailboxInTransit
                    }))
                    {
                        throw this.GetMailboxInTransitException(ex);
                    }
                    throw;
                }
            }
            finally
            {
                if (storeSession != null)
                {
                    storeSession.Dispose();
                }
            }
            return(result);
        }