public static bool UpdateMx(this ServerDns dns, string domain)
        {
            var utcNow     = DateTime.UtcNow;
            var hasChanges = false;

            var dnsLookup = new DnsLookup();

            if (dns.MxDateChecked.HasValue && dns.MxDateChecked.Value.AddSeconds(dns.MxTtl) >= utcNow)
            {
                return(hasChanges);
            }

            var mxRecord =
                dnsLookup
                .GetDomainMxRecords(domain)
                .FirstOrDefault(mx => mx.ExchangeDomainName.ToString().TrimEnd('.').Equals(dns.Mx));

            dns.MxVerified    = mxRecord != null;
            dns.MxTtl         = mxRecord != null ? mxRecord.TimeToLive : Defines.ServerDnsDefaultTtl;
            dns.MxDateChecked = utcNow;

            hasChanges = true;

            return(hasChanges);
        }
        public MailOperationStatus CheckDomainDns(string domainName, ServerDns dns,
                                                  Func <DistributedTask, string> translateMailOperationStatus = null)
        {
            var tenant = CoreContext.TenantManager.GetCurrentTenant();
            var user   = SecurityContext.CurrentAccount;

            var operations = MailOperations.GetTasks()
                             .Where(o =>
            {
                var oTenant = o.GetProperty <int>(MailOperation.TENANT);
                var oUser   = o.GetProperty <string>(MailOperation.OWNER);
                var oType   = o.GetProperty <MailOperationType>(MailOperation.OPERATION_TYPE);
                var oSource = o.GetProperty <string>(MailOperation.SOURCE);
                return(oTenant == tenant.TenantId &&
                       oUser == user.ID.ToString() &&
                       oType == MailOperationType.CheckDomainDns &&
                       oSource == domainName);
            });

            var runningOperation = operations.FirstOrDefault(o => o.Status <= DistributedTaskStatus.Running);

            if (runningOperation != null)
            {
                return(GetMailOperationStatus(runningOperation.Id, translateMailOperationStatus));
            }

            var op = new MailCheckMailserverDomainsDnsOperation(tenant, user, domainName, dns);

            return(QueueTask(op, translateMailOperationStatus));
        }
示例#3
0
        public int Save(ServerDns dns)
        {
            var query = new SqlInsert(ServerDnsTable.TABLE_NAME, true)
                        .InColumnValue(ServerDnsTable.Columns.Id, dns.Id)
                        .InColumnValue(ServerDnsTable.Columns.Tenant, dns.Tenant)
                        .InColumnValue(ServerDnsTable.Columns.User, dns.User)
                        .InColumnValue(ServerDnsTable.Columns.DomainId, dns.DomainId)
                        .InColumnValue(ServerDnsTable.Columns.DomainCheck, dns.DomainCheck)
                        .InColumnValue(ServerDnsTable.Columns.DkimSelector, dns.DkimSelector)
                        .InColumnValue(ServerDnsTable.Columns.DkimPrivateKey, dns.DkimPrivateKey)
                        .InColumnValue(ServerDnsTable.Columns.DkimPublicKey, dns.DkimPublicKey)
                        .InColumnValue(ServerDnsTable.Columns.DkimTtl, dns.DkimTtl)
                        .InColumnValue(ServerDnsTable.Columns.DkimVerified, dns.DkimVerified)
                        .InColumnValue(ServerDnsTable.Columns.DkimDateChecked, dns.DkimDateChecked)
                        .InColumnValue(ServerDnsTable.Columns.Spf, dns.Spf)
                        .InColumnValue(ServerDnsTable.Columns.SpfTtl, dns.SpfTtl)
                        .InColumnValue(ServerDnsTable.Columns.SpfVerified, dns.SpfVerified)
                        .InColumnValue(ServerDnsTable.Columns.SpfDateChecked, dns.SpfDateChecked)
                        .InColumnValue(ServerDnsTable.Columns.Mx, dns.Mx)
                        .InColumnValue(ServerDnsTable.Columns.MxTtl, dns.MxTtl)
                        .InColumnValue(ServerDnsTable.Columns.MxVerified, dns.MxVerified)
                        .InColumnValue(ServerDnsTable.Columns.MxDateChecked, dns.MxDateChecked)
                        .InColumnValue(ServerDnsTable.Columns.TimeModified, dns.TimeModified)
                        .Identity(0, 0, true);

            var id = Db.ExecuteScalar <int>(query);

            return(id);
        }
示例#4
0
        protected ServerDns ToServerDns(object[] r)
        {
            var s = new ServerDns
            {
                Id              = Convert.ToInt32(r[0]),
                Tenant          = Convert.ToInt32(r[1]),
                User            = Convert.ToString(r[2]),
                DomainId        = Convert.ToInt32(r[3]),
                DomainCheck     = Convert.ToString(r[4]),
                DkimSelector    = Convert.ToString(r[5]),
                DkimPrivateKey  = Convert.ToString(r[6]),
                DkimPublicKey   = Convert.ToString(r[7]),
                DkimTtl         = Convert.ToInt32(r[8]),
                DkimVerified    = Convert.ToBoolean(r[9]),
                DkimDateChecked = Convert.ToDateTime(r[10]),
                Spf             = Convert.ToString(r[11]),
                SpfTtl          = Convert.ToInt32(r[12]),
                SpfVerified     = Convert.ToBoolean(r[13]),
                SpfDateChecked  = Convert.ToDateTime(r[14]),
                Mx              = Convert.ToString(r[15]),
                MxTtl           = Convert.ToInt32(r[16]),
                MxVerified      = Convert.ToBoolean(r[17]),
                MxDateChecked   = Convert.ToDateTime(r[18]),
                TimeModified    = Convert.ToDateTime(r[19])
            };

            return(s);
        }
        public MailCheckMailserverDomainsDnsOperation(Tenant tenant, IAccount user, string domainName, ServerDns dns)
            : base(tenant, user)
        {
            _domainName = domainName;
            _dns        = dns;

            SetSource(_domainName);
        }
        public static bool UpdateRecords(this ServerDns dns, string domain, bool force = false)
        {
            if (string.IsNullOrEmpty(domain) || dns == null)
            {
                return(false);
            }

            var utcNow = DateTime.UtcNow;

            var hasChanges = false;

            if (force)
            {
                if (dns.UpdateMx(domain))
                {
                    hasChanges = true;
                }

                if (dns.UpdateSpf(domain))
                {
                    hasChanges = true;
                }

                if (dns.UpdateDkim(domain))
                {
                    hasChanges = true;
                }
            }
            else
            {
                if (dns.MxDateChecked.HasValue && dns.MxDateChecked.Value.AddSeconds(dns.MxTtl) >= utcNow &&
                    dns.SpfDateChecked.HasValue && dns.SpfDateChecked.Value.AddSeconds(dns.SpfTtl) >= utcNow &&
                    dns.DkimDateChecked.HasValue && dns.DkimDateChecked.Value.AddSeconds(dns.DkimTtl) >= utcNow)
                {
                    return(hasChanges);
                }

                var engineFactory = new EngineFactory(dns.Tenant, dns.User);
                engineFactory.OperationEngine.CheckDomainDns(domain, dns);
            }

            return(hasChanges);
        }
示例#7
0
        protected ServerDomainDnsData ToServerDomainDnsData(ServerDns serverDns)
        {
            if (serverDns == null)
            {
                return(null);
            }

            var dnsData = new ServerDomainDnsData
            {
                Id       = serverDns.Id,
                MxRecord = new ServerDomainMxRecordData
                {
                    Host       = serverDns.Mx,
                    IsVerified = serverDns.MxVerified,
                    Priority   = Defines.ServerDnsMxRecordPriority
                },
                SpfRecord = new ServerDomainDnsRecordData
                {
                    Name       = Defines.DNS_DEFAULT_ORIGIN,
                    IsVerified = serverDns.SpfVerified,
                    Value      = serverDns.Spf
                },
                DkimRecord = new ServerDomainDkimRecordData
                {
                    Selector   = serverDns.DkimSelector,
                    IsVerified = serverDns.DkimVerified,
                    PublicKey  = serverDns.DkimPublicKey
                },
                DomainCheckRecord = new ServerDomainDnsRecordData
                {
                    Name       = Defines.DNS_DEFAULT_ORIGIN,
                    IsVerified = true,
                    Value      = serverDns.DomainCheck
                }
            };

            return(dnsData);
        }
        public static bool UpdateSpf(this ServerDns dns, string domain)
        {
            var utcNow = DateTime.UtcNow;

            var dnsLookup = new DnsLookup();

            if (dns.SpfDateChecked.HasValue && dns.SpfDateChecked.Value.AddSeconds(dns.SpfTtl) >= utcNow)
            {
                return(false);
            }

            var txtRecords = dnsLookup
                             .GetDomainTxtRecords(domain);

            var spfRecord = txtRecords.FirstOrDefault(
                txt => txt.TextData.Trim('\"')
                .Equals(dns.Spf, StringComparison.InvariantCultureIgnoreCase));

            dns.SpfVerified    = spfRecord != null;
            dns.SpfTtl         = spfRecord != null ? spfRecord.TimeToLive : Defines.ServerDnsDefaultTtl;
            dns.SpfDateChecked = utcNow;

            return(true);
        }
        public static bool UpdateDkim(this ServerDns dns, string domain)
        {
            var utcNow = DateTime.UtcNow;

            var dnsLookup = new DnsLookup();

            if (dns.DkimDateChecked.HasValue && dns.DkimDateChecked.Value.AddSeconds(dns.DkimTtl) >= utcNow)
            {
                return(false);
            }

            var dkimRecordName = string.Format("{0}._domainkey.{1}", dns.DkimSelector, domain);

            var dkimRecord = dnsLookup
                             .GetDomainTxtRecords(dkimRecordName).FirstOrDefault(
                txt => txt.TextData.Trim('\"')
                .Equals(dns.DkimPublicKey, StringComparison.InvariantCultureIgnoreCase));

            dns.DkimVerified    = dkimRecord != null;
            dns.DkimTtl         = dkimRecord != null ? dkimRecord.TimeToLive : Defines.ServerDnsDefaultTtl;
            dns.DkimDateChecked = utcNow;

            return(true);
        }
示例#10
0
        public ServerDomainDnsData GetOrCreateUnusedDnsData(IDaoFactory daoFactory, Entities.Server server)
        {
            var serverDnsDao = daoFactory.CreateServerDnsDao(Tenant, User);

            var dnsSettings = serverDnsDao.GetFree();

            if (dnsSettings == null)
            {
                string privateKey, publicKey;
                CryptoUtil.GenerateDkimKeys(out privateKey, out publicKey);

                var domainCheckValue = PasswordGenerator.GenerateNewPassword(16);
                var domainCheck      = Defines.ServerDnsDomainCheckPrefix + ": " + domainCheckValue;

                var serverDns = new ServerDns
                {
                    Id              = 0,
                    Tenant          = Tenant,
                    User            = User,
                    DomainId        = Defines.UNUSED_DNS_SETTING_DOMAIN_ID,
                    DomainCheck     = domainCheck,
                    DkimSelector    = Defines.ServerDnsDkimSelector,
                    DkimPrivateKey  = privateKey,
                    DkimPublicKey   = publicKey,
                    DkimTtl         = Defines.ServerDnsDefaultTtl,
                    DkimVerified    = false,
                    DkimDateChecked = null,
                    Spf             = Defines.ServerDnsSpfRecordValue,
                    SpfTtl          = Defines.ServerDnsDefaultTtl,
                    SpfVerified     = false,
                    SpfDateChecked  = null,
                    Mx              = server.MxRecord,
                    MxTtl           = Defines.ServerDnsDefaultTtl,
                    MxVerified      = false,
                    MxDateChecked   = null,
                    TimeModified    = DateTime.UtcNow
                };

                serverDns.Id = serverDnsDao.Save(serverDns);

                dnsSettings = serverDns;
            }

            var dnsData = new ServerDomainDnsData
            {
                Id       = dnsSettings.Id,
                MxRecord = new ServerDomainMxRecordData
                {
                    Host       = dnsSettings.Mx,
                    IsVerified = false,
                    Priority   = Defines.ServerDnsMxRecordPriority
                },
                DkimRecord = new ServerDomainDkimRecordData
                {
                    Selector   = dnsSettings.DkimSelector,
                    IsVerified = false,
                    PublicKey  = dnsSettings.DkimPublicKey
                },
                DomainCheckRecord = new ServerDomainDnsRecordData
                {
                    Name       = Defines.DNS_DEFAULT_ORIGIN,
                    IsVerified = false,
                    Value      = dnsSettings.DomainCheck
                },
                SpfRecord = new ServerDomainDnsRecordData
                {
                    Name       = Defines.DNS_DEFAULT_ORIGIN,
                    IsVerified = false,
                    Value      = dnsSettings.Spf
                }
            };

            return(dnsData);
        }