1 parent
bb2477b8b9
commit
56f4abc632
16 files changed
+171
-26
No files matched your search
@@ -30,6 +30,7 @@ type Config struct {
|
||||
Domain string `json:"domain"`
|
||||
Domains []string `json:"domains"` //多域名设置,把所有收信域名都填进去
|
||||
WebDomain string `json:"webDomain"`
|
||||
AdminRecoveryDomain string `json:"adminRecoveryDomain,omitempty"`
|
||||
AccessHosts []string `json:"accessHosts,omitempty"`
|
||||
DkimPrivateKeyPath string `json:"dkimPrivateKeyPath"`
|
||||
SSLType string `json:"sslType"` // 0表示自动生成证书,HTTP挑战模式,1表示用户上传证书,2表示自动-DNS挑战模式
|
||||
|
||||
@@ -49,6 +49,7 @@ type legacyMigrationConfig struct {
|
||||
Domain string `json:"domain"`
|
||||
Domains []string `json:"domains"`
|
||||
WebDomain string `json:"webDomain"`
|
||||
AdminRecoveryDomain string `json:"adminRecoveryDomain"`
|
||||
AccessHosts []string `json:"accessHosts"`
|
||||
DkimPrivateKeyPath string `json:"dkimPrivateKeyPath"`
|
||||
SSLType string `json:"sslType"`
|
||||
@@ -88,7 +89,7 @@ type legacyMigrationConfig struct {
|
||||
}
|
||||
|
||||
var legacyMigrationKnownFields = map[string]struct{}{
|
||||
"logLevel": {}, "domain": {}, "domains": {}, "webDomain": {}, "accessHosts": {},
|
||||
"logLevel": {}, "domain": {}, "domains": {}, "webDomain": {}, "adminRecoveryDomain": {}, "accessHosts": {},
|
||||
"dkimPrivateKeyPath": {}, "sslType": {}, "SSLPrivateKeyPath": {}, "SSLPublicKeyPath": {},
|
||||
"dbDSN": {}, "dbType": {}, "httpsEnabled": {}, "spamFilterLevel": {}, "httpPort": {}, "httpsPort": {}, "tlsMode": {}, "trustedProxyCidrs": {},
|
||||
"weChatPushAppId": {}, "weChatPushSecret": {}, "weChatPushTemplateId": {}, "weChatPushUserId": {},
|
||||
@@ -499,10 +500,11 @@ func legacyRuntimeConfig(root string, legacy legacyMigrationConfig) (RuntimeConf
|
||||
LogLevel: legacy.LogLevel,
|
||||
Database: database,
|
||||
Domains: DomainConfig{
|
||||
Primary: legacy.Domain,
|
||||
Accepted: accepted,
|
||||
Web: legacy.WebDomain,
|
||||
AccessHosts: append([]string(nil), legacy.AccessHosts...),
|
||||
Primary: legacy.Domain,
|
||||
Accepted: accepted,
|
||||
Web: legacy.WebDomain,
|
||||
AdminRecovery: legacy.AdminRecoveryDomain,
|
||||
AccessHosts: append([]string(nil), legacy.AccessHosts...),
|
||||
},
|
||||
HTTP: HTTPConfig{
|
||||
Port: legacy.HttpPort,
|
||||
|
||||
@@ -52,8 +52,9 @@ func runtimeCompatibility(runtime RuntimeConfig) (*Config, error) {
|
||||
cfg := &Config{
|
||||
LogLevel: root.LogLevel, Domain: root.Domains.Primary,
|
||||
Domains: append([]string(nil), root.Domains.Accepted...), WebDomain: root.Domains.Web,
|
||||
AccessHosts: append([]string(nil), root.Domains.AccessHosts...),
|
||||
DbType: driver, DbDSN: dsn, SetupState: runtime.SetupState,
|
||||
AdminRecoveryDomain: root.Domains.AdminRecovery,
|
||||
AccessHosts: append([]string(nil), root.Domains.AccessHosts...),
|
||||
DbType: driver, DbDSN: dsn, SetupState: runtime.SetupState,
|
||||
IsInit: runtime.SetupState == SetupStateReady,
|
||||
HttpPort: root.HTTP.Port, HttpsPort: root.HTTP.HTTPSPort,
|
||||
HttpsEnabled: 2, TLSMode: root.HTTP.TLSMode,
|
||||
|
||||
@@ -55,7 +55,7 @@ func TestPartitionedCompatibilityStartupAndWriteProtection(t *testing.T) {
|
||||
PublishConfigSnapshot(oldSnapshot)
|
||||
})
|
||||
writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"logLevel": "info", "domain": "mail.example.com", "webDomain": "mail.example.com",
|
||||
"logLevel": "info", "domain": "mail.example.com", "webDomain": "mail.example.com", "adminRecoveryDomain": "recovery.mail.example.com",
|
||||
"dbType": "sqlite", "dbDSN": "./config/existing.db", "isInit": true,
|
||||
"sslType": "1", "SSLPublicKeyPath": "./config/ssl/site.crt", "SSLPrivateKeyPath": "./config/ssl/site.key",
|
||||
"httpPort": 80, "httpsPort": 443, "httpsEnabled": 1,
|
||||
@@ -76,7 +76,7 @@ func TestPartitionedCompatibilityStartupAndWriteProtection(t *testing.T) {
|
||||
if !cfg.IsSetupReady() || cfg.DbType != DBTypeSQLite || cfg.DbDSN != filepath.Join(root, "data", "existing.db") {
|
||||
t.Fatal("partitioned startup lost initialization or database identity")
|
||||
}
|
||||
if cfg.WebDomain != "mail.example.com" || cfg.SSL.CertificateRef != "data/ssl/site.crt" {
|
||||
if cfg.WebDomain != "mail.example.com" || cfg.AdminRecoveryDomain != "recovery.mail.example.com" || cfg.SSL.CertificateRef != "data/ssl/site.crt" {
|
||||
t.Fatal("partitioned startup lost domain or certificate path")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,6 +133,28 @@ func CurrentWebDomain() string {
|
||||
return cfg.WebDomain
|
||||
}
|
||||
|
||||
// CurrentAdminRecoveryDomain 返回超级管理员本机恢复通道的部署标识域名。
|
||||
// 此值不用于提供公网重置路由;未配置时沿用主邮件域名。
|
||||
func CurrentAdminRecoveryDomain() string {
|
||||
if manager := CurrentManager(); manager != nil {
|
||||
runtime := manager.Snapshot()
|
||||
if runtime.Generation != 0 {
|
||||
if domain := runtime.Root.Domains.AdminRecovery; domain != "" {
|
||||
return domain
|
||||
}
|
||||
return runtime.Root.Domains.Primary
|
||||
}
|
||||
}
|
||||
cfg := CurrentConfig()
|
||||
if cfg == nil {
|
||||
return ""
|
||||
}
|
||||
if cfg.AdminRecoveryDomain != "" {
|
||||
return cfg.AdminRecoveryDomain
|
||||
}
|
||||
return cfg.Domain
|
||||
}
|
||||
|
||||
// SetRuntimeSetupPort stores the temporary setup listener port outside the persisted configuration.
|
||||
func SetRuntimeSetupPort(port int) {
|
||||
setupPort.Store(int64(port))
|
||||
|
||||
@@ -216,6 +216,42 @@ func TestCurrentMailDomainsFallsBackToLegacyConfig(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentAdminRecoveryDomainUsesConfiguredValueAndSafeFallback(t *testing.T) {
|
||||
previousManager := CurrentManager()
|
||||
previousInstance := Instance
|
||||
defer func() {
|
||||
SetRuntimeManager(previousManager)
|
||||
Instance = previousInstance
|
||||
PublishConfigSnapshot(previousInstance)
|
||||
}()
|
||||
|
||||
runtime := validRuntimeConfig()
|
||||
runtime.Root.Domains.AdminRecovery = "recovery.example.com"
|
||||
SetRuntimeManager(&ConfigManager{snapshots: runtimeAccessSnapshotStore{runtime: runtime}})
|
||||
if got := CurrentAdminRecoveryDomain(); got != "recovery.example.com" {
|
||||
t.Fatalf("configured recovery domain = %q", got)
|
||||
}
|
||||
|
||||
runtime.Root.Domains.AdminRecovery = ""
|
||||
runtime.Root.Domains.Primary = "example.com"
|
||||
SetRuntimeManager(&ConfigManager{snapshots: runtimeAccessSnapshotStore{runtime: runtime}})
|
||||
if got := CurrentAdminRecoveryDomain(); got != "example.com" {
|
||||
t.Fatalf("partition fallback recovery domain = %q", got)
|
||||
}
|
||||
|
||||
SetRuntimeManager(nil)
|
||||
Instance = &Config{Domain: "legacy.example.com", AdminRecoveryDomain: "recovery.legacy.example.com"}
|
||||
PublishConfigSnapshot(Instance)
|
||||
if got := CurrentAdminRecoveryDomain(); got != "recovery.legacy.example.com" {
|
||||
t.Fatalf("legacy configured recovery domain = %q", got)
|
||||
}
|
||||
Instance = &Config{Domain: "legacy.example.com"}
|
||||
PublishConfigSnapshot(Instance)
|
||||
if got := CurrentAdminRecoveryDomain(); got != "legacy.example.com" {
|
||||
t.Fatalf("legacy fallback recovery domain = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentSMTPRuntimeSettingsReadsPartitionSnapshot(t *testing.T) {
|
||||
previousManager := CurrentManager()
|
||||
defer SetRuntimeManager(previousManager)
|
||||
|
||||
@@ -83,7 +83,7 @@ func ValidateRuntimeConfig(runtime RuntimeConfig, options RuntimeValidationOptio
|
||||
}
|
||||
|
||||
func validateDomains(domains DomainConfig) error {
|
||||
if domains.Primary == "" && domains.Web == "" && len(domains.Accepted) == 0 && len(domains.AccessHosts) == 0 {
|
||||
if domains.Primary == "" && domains.Web == "" && domains.AdminRecovery == "" && len(domains.Accepted) == 0 && len(domains.AccessHosts) == 0 {
|
||||
return nil
|
||||
}
|
||||
primary, err := normalizeConfigDomain(domains.Primary)
|
||||
@@ -94,6 +94,12 @@ func validateDomains(domains DomainConfig) error {
|
||||
if err != nil || web != strings.ToLower(strings.TrimSuffix(domains.Web, ".")) {
|
||||
return ErrInvalidDomainConfig
|
||||
}
|
||||
if domains.AdminRecovery != "" {
|
||||
recovery, recoveryErr := normalizeConfigDomain(domains.AdminRecovery)
|
||||
if recoveryErr != nil || recovery != strings.ToLower(strings.TrimSuffix(domains.AdminRecovery, ".")) {
|
||||
return ErrInvalidDomainConfig
|
||||
}
|
||||
}
|
||||
seen := make(map[string]struct{}, len(domains.Accepted))
|
||||
for _, item := range domains.Accepted {
|
||||
domain, normalizeErr := normalizeConfigDomain(item)
|
||||
|
||||
@@ -65,6 +65,9 @@ func TestValidateRuntimeConfigRejectsCrossSectionViolations(t *testing.T) {
|
||||
{name: "primary_domain_missing", mutate: func(runtime *RuntimeConfig, _ *RuntimeValidationOptions) {
|
||||
runtime.Root.Domains.Accepted = []string{"example.com"}
|
||||
}, want: ErrInvalidDomainConfig},
|
||||
{name: "invalid_admin_recovery_domain", mutate: func(runtime *RuntimeConfig, _ *RuntimeValidationOptions) {
|
||||
runtime.Root.Domains.AdminRecovery = "https://recovery.example.com/reset"
|
||||
}, want: ErrInvalidDomainConfig},
|
||||
{name: "generation_mismatch", mutate: func(runtime *RuntimeConfig, _ *RuntimeValidationOptions) { runtime.Email.Meta.Generation++ }, want: ErrSectionGenerationMismatch},
|
||||
{name: "missing_super_admin", mutate: func(_ *RuntimeConfig, options *RuntimeValidationOptions) { options.HasSuperAdmin = false }, want: ErrInvalidRuntimeConfig},
|
||||
{name: "network_without_capability", mutate: func(runtime *RuntimeConfig, _ *RuntimeValidationOptions) {
|
||||
|
||||
@@ -35,10 +35,11 @@ type SectionMeta struct {
|
||||
}
|
||||
|
||||
type DomainConfig struct {
|
||||
Primary string `json:"primary,omitempty"`
|
||||
Accepted []string `json:"accepted,omitempty"`
|
||||
Web string `json:"web,omitempty"`
|
||||
AccessHosts []string `json:"accessHosts,omitempty"`
|
||||
Primary string `json:"primary,omitempty"`
|
||||
Accepted []string `json:"accepted,omitempty"`
|
||||
Web string `json:"web,omitempty"`
|
||||
AdminRecovery string `json:"adminRecovery,omitempty"`
|
||||
AccessHosts []string `json:"accessHosts,omitempty"`
|
||||
}
|
||||
|
||||
type HTTPConfig struct {
|
||||
|
||||
@@ -192,6 +192,9 @@ func validationCertificateDomains(candidate *RuntimeConfig) []string {
|
||||
if candidate.Root.Domains.Web != "" {
|
||||
domains = append(domains, candidate.Root.Domains.Web)
|
||||
}
|
||||
if candidate.Root.Domains.AdminRecovery != "" {
|
||||
domains = append(domains, candidate.Root.Domains.AdminRecovery)
|
||||
}
|
||||
for _, domain := range candidate.Root.Domains.Accepted {
|
||||
domains = append(domains, "smtp."+domain, "imap."+domain, "pop."+domain)
|
||||
}
|
||||
@@ -201,11 +204,11 @@ func validationCertificateDomains(candidate *RuntimeConfig) []string {
|
||||
}
|
||||
|
||||
func runtimeCertificateDomains(candidate *RuntimeConfig) []string {
|
||||
return SSLCertificateDomains(candidate.Root.Domains.Primary, candidate.Root.Domains.Web, candidate.Root.Domains.Accepted)
|
||||
return SSLCertificateDomains(candidate.Root.Domains.Primary, candidate.Root.Domains.Web, candidate.Root.Domains.Accepted, candidate.Root.Domains.AdminRecovery)
|
||||
}
|
||||
|
||||
// SSLCertificateDomains 统一初始化和配置管理所需的 Web、主域及邮件协议域名。
|
||||
func SSLCertificateDomains(primary, web string, accepted []string) []string {
|
||||
// SSLCertificateDomains 统一初始化和配置管理所需的 Web、主域、邮件协议及恢复通道域名。
|
||||
func SSLCertificateDomains(primary, web string, accepted []string, adminRecovery ...string) []string {
|
||||
seen := make(map[string]bool)
|
||||
var domains []string
|
||||
appendDomain := func(value string) {
|
||||
@@ -217,6 +220,9 @@ func SSLCertificateDomains(primary, web string, accepted []string) []string {
|
||||
}
|
||||
appendDomain(web)
|
||||
appendDomain(primary)
|
||||
for _, domain := range adminRecovery {
|
||||
appendDomain(domain)
|
||||
}
|
||||
for _, domain := range accepted {
|
||||
domain = strings.TrimSpace(domain)
|
||||
if domain == "" {
|
||||
|
||||
@@ -37,6 +37,7 @@ func CurrentCertificateConfig() *Config {
|
||||
cfg.SSL = runtime.SSL.TLS
|
||||
cfg.Domain, cfg.WebDomain = runtime.Root.Domains.Primary, runtime.Root.Domains.Web
|
||||
cfg.Domains = runtime.Root.Domains.Accepted
|
||||
cfg.AdminRecoveryDomain = runtime.Root.Domains.AdminRecovery
|
||||
cfg.TLSMode = runtime.Root.HTTP.TLSMode
|
||||
cfg.HttpsEnabled = 2
|
||||
if runtime.Root.HTTP.HTTPSEnabled {
|
||||
@@ -98,7 +99,12 @@ func loadValidatedRuntimeCertificate(cfg *Config) (tls.Certificate, error) {
|
||||
if err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
runtime := RuntimeConfig{Root: RootConfig{Domains: DomainConfig{Primary: cfg.Domain, Web: cfg.WebDomain, Accepted: cfg.Domains}}}
|
||||
runtime := RuntimeConfig{Root: RootConfig{Domains: DomainConfig{
|
||||
Primary: cfg.Domain,
|
||||
Web: cfg.WebDomain,
|
||||
Accepted: cfg.Domains,
|
||||
AdminRecovery: cfg.AdminRecoveryDomain,
|
||||
}}}
|
||||
runtime.SSL.TLS = cfg.SSL
|
||||
if cfg.SSLType == SSLTypeAutoDNS {
|
||||
runtime.SSL.TLS.Management = SSLManagementProject
|
||||
|
||||
@@ -191,6 +191,22 @@ func TestValidateSSLMaterialRejectsDomainExpiryAndWeakKeys(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSSLCertificateDomainsIncludesConfiguredAdminRecoveryDomain(t *testing.T) {
|
||||
domains := SSLCertificateDomains("example.com", "mail.example.com", []string{"example.com"}, "recovery.example.com")
|
||||
for _, want := range []string{"example.com", "mail.example.com", "recovery.example.com", "smtp.example.com", "imap.example.com", "pop.example.com"} {
|
||||
found := false
|
||||
for _, got := range domains {
|
||||
if got == want {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("certificate domains %v do not include %q", domains, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareCustomSSLPathsKeepsDirectFilesWithoutCopying(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
previousRoot := ROOT_PATH
|
||||
|
||||
@@ -306,7 +306,7 @@ func sslDomains(cfg *config.Config) []string {
|
||||
if cfg == nil {
|
||||
return nil
|
||||
}
|
||||
return config.SSLCertificateDomains(cfg.Domain, cfg.WebDomain, cfg.Domains)
|
||||
return config.SSLCertificateDomains(cfg.Domain, cfg.WebDomain, cfg.Domains, cfg.AdminRecoveryDomain)
|
||||
}
|
||||
|
||||
func certificateMatchesConfiguredDomains(cert *x509.Certificate, cfg *config.Config) bool {
|
||||
@@ -343,6 +343,9 @@ func validateWildcardConfiguration(cfg *config.Config) error {
|
||||
if cfg.WebDomain != "" && !covered(cfg.WebDomain) {
|
||||
return newUserCertificateError("Web 域名必须是主域名的一级子域名,泛域名证书不覆盖根域名或多级子域名")
|
||||
}
|
||||
if cfg.AdminRecoveryDomain != "" && !covered(cfg.AdminRecoveryDomain) {
|
||||
return newUserCertificateError("超级管理员恢复域名必须是主邮件域名的一级子域名,才能由 DNS 泛域名证书覆盖")
|
||||
}
|
||||
for _, domain := range cfg.Domains {
|
||||
if !covered("smtp."+domain) || !covered("imap."+domain) || !covered("pop."+domain) {
|
||||
return newUserCertificateError("附加邮箱域名的服务地址不在主域泛域名证书覆盖范围内")
|
||||
|
||||
@@ -52,17 +52,18 @@ func TestWildcardCertificateDomains(t *testing.T) {
|
||||
|
||||
func TestCertificateDomainsFollowChallengeMode(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Domain: "xuans.com",
|
||||
WebDomain: "web.xuans.com",
|
||||
Domains: []string{"xuans.com"},
|
||||
Domain: "xuans.com",
|
||||
WebDomain: "web.xuans.com",
|
||||
AdminRecoveryDomain: "recovery.xuans.com",
|
||||
Domains: []string{"xuans.com"},
|
||||
}
|
||||
|
||||
cfg.SSLType = config.SSLTypeAutoHTTP
|
||||
httpDomains := certificateDomains(cfg)
|
||||
if len(httpDomains) != 5 {
|
||||
t.Fatalf("got %d HTTP domains, want 5: %v", len(httpDomains), httpDomains)
|
||||
if len(httpDomains) != 6 {
|
||||
t.Fatalf("got %d HTTP domains, want 6: %v", len(httpDomains), httpDomains)
|
||||
}
|
||||
if httpDomains[0] != "web.xuans.com" || httpDomains[1] != "xuans.com" {
|
||||
if httpDomains[0] != "web.xuans.com" || httpDomains[1] != "xuans.com" || httpDomains[2] != "recovery.xuans.com" {
|
||||
t.Fatalf("unexpected HTTP domains: %v", httpDomains)
|
||||
}
|
||||
|
||||
@@ -73,6 +74,26 @@ func TestCertificateDomainsFollowChallengeMode(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNSWildcardRejectsUncoveredAdminRecoveryDomain(t *testing.T) {
|
||||
base := &config.Config{
|
||||
Domain: "example.com",
|
||||
WebDomain: "mail.example.com",
|
||||
Domains: []string{"example.com"},
|
||||
SSLType: config.SSLTypeAutoDNS,
|
||||
AdminRecoveryDomain: "recovery.example.com",
|
||||
}
|
||||
if err := validateWildcardConfiguration(base); err != nil {
|
||||
t.Fatalf("covered recovery domain was rejected: %v", err)
|
||||
}
|
||||
for _, value := range []string{"example.com", "a.recovery.example.com", "recovery.other.example"} {
|
||||
candidate := *base
|
||||
candidate.AdminRecoveryDomain = value
|
||||
if err := validateWildcardConfiguration(&candidate); err == nil {
|
||||
t.Fatalf("uncovered recovery domain %q was accepted", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNSChallengeWildcardProducesSingleRecord(t *testing.T) {
|
||||
challenge := &DNSChallenge{authInfo: map[string]*authInfo{}}
|
||||
if err := challenge.Present("*.xuans.com", "token", "key-auth"); err != nil {
|
||||
|
||||
Reference in new issue
Block a user