Exemple #1
0
        public static MapiStore GetSystemMailbox(Guid mdbGuid, string dcName, NetworkCredential cred, bool allowCrossSiteLogon)
        {
            bool        flag        = false;
            ConnectFlag connectFlag = ConnectFlag.UseAdminPrivilege | ConnectFlag.AllowLegacyStore;
            string      userName;
            string      password;
            string      domainName;
            TimeSpan    connectionTimeout;
            TimeSpan    callTimeout;

            if (cred != null)
            {
                userName          = cred.UserName;
                password          = cred.Password;
                domainName        = cred.Domain;
                connectionTimeout = TestIntegration.Instance.RemoteMailboxConnectionTimeout;
                callTimeout       = TestIntegration.Instance.RemoteMailboxCallTimeout;
            }
            else
            {
                userName          = null;
                password          = null;
                domainName        = null;
                connectFlag      |= ConnectFlag.UseRpcContextPool;
                connectionTimeout = TestIntegration.Instance.LocalMailboxConnectionTimeout;
                callTimeout       = TestIntegration.Instance.LocalMailboxCallTimeout;
            }
            DatabaseInformation databaseInformation;

            for (;;)
            {
                FindServerFlags findServerFlags = FindServerFlags.FindSystemMailbox;
                if (flag)
                {
                    findServerFlags |= FindServerFlags.ForceRediscovery;
                }
                databaseInformation = MapiUtils.FindServerForMdb(mdbGuid, dcName, cred, findServerFlags);
                if (!allowCrossSiteLogon && !databaseInformation.IsInLocalSite)
                {
                    break;
                }
                MapiStore result;
                try
                {
                    MapiStore mapiStore = MapiStore.OpenMailbox(databaseInformation.ServerFqdn, Server.GetSystemAttendantLegacyDN(databaseInformation.ServerDN), databaseInformation.SystemMailboxGuid, mdbGuid, userName, domainName, password, connectFlag, OpenStoreFlag.UseAdminPrivilege | OpenStoreFlag.TakeOwnership | OpenStoreFlag.MailboxGuid, CultureInfo.InvariantCulture, null, "Client=MSExchangeMigration", connectionTimeout, callTimeout, null);
                    MapiUtils.StartMapiDeadSessionChecking(mapiStore, mdbGuid.ToString());
                    result = mapiStore;
                }
                catch (MapiExceptionWrongServer)
                {
                    if (!flag)
                    {
                        MrsTracer.Common.Debug("OpenMailbox returned WrongServer, forcing AM rediscovery", new object[0]);
                        flag = true;
                        continue;
                    }
                    throw;
                }
                catch (MapiExceptionLogonFailed)
                {
                    if (!flag)
                    {
                        MrsTracer.Common.Debug("OpenMailbox returned LogonFailed, forcing AM rediscovery", new object[0]);
                        flag = true;
                        continue;
                    }
                    throw;
                }
                catch (MapiExceptionUnknownUser)
                {
                    if (!flag)
                    {
                        MrsTracer.Common.Debug("OpenMailbox returned UnknownUser, forcing AM rediscovery", new object[0]);
                        flag = true;
                        continue;
                    }
                    throw;
                }
                return(result);
            }
            throw new CrossSiteLogonTransientException(mdbGuid, databaseInformation.ServerGuid, databaseInformation.ServerSite.ToString(), CommonUtils.LocalSiteId.ToString());
        }
        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);
        }
Exemple #3
0
        public static MapiStore GetUserMailbox(Guid mailboxGuid, Guid mdbGuid, UserMailboxFlags umFlags)
        {
            OpenStoreFlag openStoreFlag = OpenStoreFlag.UseAdminPrivilege | OpenStoreFlag.TakeOwnership | OpenStoreFlag.NoLocalization | OpenStoreFlag.MailboxGuid;
            ConnectFlag   connectFlags  = ConnectFlag.UseAdminPrivilege | ConnectFlag.UseRpcContextPool | ConnectFlag.AllowLegacyStore;

            if (umFlags.HasFlag(UserMailboxFlags.RecoveryMDB))
            {
                openStoreFlag |= OpenStoreFlag.RestoreDatabase;
            }
            if (umFlags.HasFlag(UserMailboxFlags.Disconnected) || umFlags.HasFlag(UserMailboxFlags.SoftDeleted) || umFlags.HasFlag(UserMailboxFlags.MoveDestination))
            {
                openStoreFlag |= (OpenStoreFlag.OverrideHomeMdb | OpenStoreFlag.DisconnectedMailbox);
            }
            bool      flag = false;
            TimeSpan  localMailboxConnectionTimeout = TestIntegration.Instance.LocalMailboxConnectionTimeout;
            TimeSpan  localMailboxCallTimeout       = TestIntegration.Instance.LocalMailboxCallTimeout;
            MapiStore result;

            for (;;)
            {
                FindServerFlags findServerFlags = FindServerFlags.None;
                if (flag)
                {
                    findServerFlags |= FindServerFlags.ForceRediscovery;
                }
                DatabaseInformation databaseInformation = MapiUtils.FindServerForMdb(mdbGuid, null, null, findServerFlags);
                try
                {
                    MrsTracer.Common.Debug("Opening MapiStore: serverFqdn=\"{0}\", mailboxGuid=\"{1}\", mdbGuid=\"{2}\", flags=[{3}].", new object[]
                    {
                        databaseInformation.ServerFqdn,
                        mailboxGuid,
                        mdbGuid,
                        openStoreFlag
                    });
                    MapiStore mapiStore = MapiStore.OpenMailbox(databaseInformation.ServerFqdn, Server.GetSystemAttendantLegacyDN(databaseInformation.ServerDN), mailboxGuid, mdbGuid, null, null, null, connectFlags, openStoreFlag, null, null, "Client=MSExchangeMigration", localMailboxConnectionTimeout, localMailboxCallTimeout, null);
                    MapiUtils.StartMapiDeadSessionChecking(mapiStore, mailboxGuid.ToString());
                    result = mapiStore;
                }
                catch (MapiExceptionWrongServer)
                {
                    if (!flag)
                    {
                        MrsTracer.Common.Debug("OpenMailbox returned WrongServer, forcing AM rediscovery", new object[0]);
                        flag = true;
                        continue;
                    }
                    throw;
                }
                catch (MapiExceptionLogonFailed)
                {
                    if (!flag)
                    {
                        MrsTracer.Common.Debug("OpenMailbox returned LogonFailed, forcing AM rediscovery", new object[0]);
                        flag = true;
                        continue;
                    }
                    throw;
                }
                break;
            }
            return(result);
        }