保存模拟部署迁移与最新构建校验阶段成果
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-08 18:33:21 +08:00
1 parent 519e2b4a5f
commit d7837de561
45 files changed
+1588 -109

No files matched your search

+14
View File
@@ -23,6 +23,7 @@ func run(args []string) error {
root := flags.String("root", "", "Smail 服务根目录,包含 config 子目录")
apply := flags.Bool("apply", false, "确认已停止后端,备份并执行迁移;默认只检查")
runtimeData := flags.Bool("runtime-data", false, "将运行数据迁到 data,保留当前五个配置文件")
historySource := flags.String("deployment-history", "", "停服复制完成后,从原部署导入配置历史并转换证书引用")
if err := flags.Parse(args); err != nil {
return err
}
@@ -34,6 +35,19 @@ func run(args []string) error {
return fmt.Errorf("服务根目录无效")
}
options := config.RuntimeValidationOptions{HasSuperAdmin: true, Production: true}
if *historySource != "" {
if !*apply || *runtimeData {
return fmt.Errorf("部署历史导入必须单独使用 --apply,且先停止源与目标后端")
}
if err := config.ImportDeploymentHistory(*historySource, absolute, options); err != nil {
return fmt.Errorf("配置历史导入失败:%w", err)
}
if err := config.NormalizeDeploymentReferences(absolute, options); err != nil {
return fmt.Errorf("部署引用转换失败:%w", err)
}
fmt.Println("配置历史已导入状态数据库,生效配置仍为 config 下的五个文件。")
return nil
}
if *runtimeData {
moves, err := runtimepath.Plan(absolute)
if err != nil {
+21
View File
@@ -0,0 +1,21 @@
//go:build bundled
package main
import (
"embed"
"io/fs"
"git.pcux.cn/xuans/Smail/hooks"
)
//go:embed bundled/*
var pluginFiles embed.FS
func init() {
files, err := fs.Sub(pluginFiles, "bundled")
if err != nil {
panic(err)
}
hooks.SetBundledPlugins(files)
}
+5 -1
View File
@@ -46,7 +46,11 @@ func NewFileAuditWriter(root string) (*FileAuditWriter, error) {
if strings.TrimSpace(root) == "" {
return nil, ErrAuditStorageUnavailable
}
directory, err := runtimepath.Directory(root)
name := "data"
if runtimepath.DeploymentLayout(root) {
name = "log"
}
directory, err := runtimepath.SecureDirectory(root, name)
if err != nil {
return nil, err
}
+13
View File
@@ -5,6 +5,8 @@ import (
"path/filepath"
"sort"
"strings"
"git.pcux.cn/xuans/Smail/utils/runtimepath"
)
// ListBackups 枚举合法备份的代次和时间,不返回内部路径。
@@ -12,6 +14,17 @@ func (s *TransactionStore) ListBackups() ([]ConfigBackupInfo, error) {
if s == nil || s.root == "" {
return nil, ErrBackupListingUnavailable
}
if runtimepath.DeploymentLayout(s.root) {
items, err := s.stateBackups()
if err != nil {
return nil, err
}
result := make([]ConfigBackupInfo, 0, len(items))
for _, item := range items {
result = append(result, ConfigBackupInfo{Generation: item.Backup.Generation, CreatedAt: item.Backup.CreatedAt})
}
return result, nil
}
configDirectory, err := secureManagerConfigDirectory(s.root)
if err != nil {
return nil, err
+5 -5
View File
@@ -276,7 +276,7 @@ func legacyInitReady() bool {
}
func ReadPrivateKey() (*ecdsa.PrivateKey, bool, error) {
privateKey, created, err := loadOrCreateACMEPrivateKey(filepath.Join(ROOT_PATH, "data", "ssl"), "account_private.pem")
privateKey, created, err := loadOrCreateACMEPrivateKey(runtimepath.SSLDirectory(ROOT_PATH), "account_private.pem")
if err != nil {
return nil, false, err
}
@@ -332,9 +332,9 @@ func ReadConfig() (*Config, error) {
func readConfig() (*Config, error) {
configData := Config{
DkimPrivateKeyPath: ROOT_PATH + "data/dkim/dkim.priv",
SSLPrivateKeyPath: ROOT_PATH + "data/ssl/private.key",
SSLPublicKeyPath: ROOT_PATH + "data/ssl/public.crt",
DkimPrivateKeyPath: filepath.Join(runtimepath.DKIMDirectory(ROOT_PATH), "dkim.priv"),
SSLPrivateKeyPath: filepath.Join(runtimepath.SSLDirectory(ROOT_PATH), "private.key"),
SSLPublicKeyPath: filepath.Join(runtimepath.SSLDirectory(ROOT_PATH), "public.crt"),
VerificationEnabled: true,
ExternalRecoveryEnabled: true,
LoginPolicy: "new_device",
@@ -446,7 +446,7 @@ func persistControlledSSLPath(value string) string {
}
func resolvePath(value string) string {
value = runtimepath.LegacyReference(strings.TrimSpace(value))
value = runtimepath.SSLReference(ROOT_PATH, strings.TrimSpace(value))
if value == "" {
return ""
}
+96
View File
@@ -0,0 +1,96 @@
package config
import (
"errors"
"os"
"path/filepath"
fileutil "git.pcux.cn/xuans/Smail/utils/file"
"git.pcux.cn/xuans/Smail/utils/runtimepath"
)
// InitializeDeployment 仅对全新安装启用新布局,不触碰已有部署或生成替代密钥。
func InitializeDeployment(root string) error {
for _, name := range []string{"config/config.json", "data/credential.key", "config/credential.key"} {
if _, err := os.Lstat(filepath.Join(root, filepath.FromSlash(name))); err == nil {
return nil
} else if !os.IsNotExist(err) {
return err
}
}
db, err := NewTransactionStore(root, RuntimeValidationOptions{}).openState()
if err != nil {
return err
}
return db.Close()
}
// ImportDeploymentHistory 在停服迁移期间导入旧历史原始字节,原目录保持不动。
func ImportDeploymentHistory(source, target string, validation RuntimeValidationOptions) error {
if !runtimepath.DeploymentLayout(target) {
return errors.New("目标部署密钥尚未迁移到 ssl")
}
if entries, err := os.ReadDir(filepath.Join(source, "data", "transaction")); err == nil && len(entries) != 0 {
return ErrConfigRecoveryRequired
} else if err != nil && !os.IsNotExist(err) {
return err
}
store := NewTransactionStore(target, validation)
unlock, err := store.lockState()
if err != nil {
return err
}
defer unlock()
entries, err := os.ReadDir(filepath.Join(source, "data", "backups-v2"))
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
for _, entry := range entries {
directory := filepath.Join(source, "data", "backups-v2", entry.Name())
_, manifest, err := loadConfigBackup(directory, validation)
if err != nil {
return err
}
files := make(map[string][]byte)
for _, name := range transactionFileNames() {
files[name], err = fileutil.ReadRegularFile(filepath.Join(directory, "files", name), maxConfigFileSize)
if err != nil {
return err
}
}
if err := validateStateFiles(files, manifest.Files); err != nil {
return err
}
if err := store.saveState("backup", manifest.ID, stateRecord{Backup: &manifest, Original: files}); err != nil {
return err
}
}
_, err = store.stateBackups()
return err
}
// NormalizeDeploymentReferences 通过正常事务更新路径及摘要,不能直接编辑配置 JSON。
func NormalizeDeploymentReferences(root string, validation RuntimeValidationOptions) error {
loader := NewConfigLoader(root, ConfigLoaderOptions{Validation: validation})
current, err := loader.Load()
if err != nil {
return err
}
if !runtimepath.DeploymentLayout(root) {
return ErrManagerStorage
}
candidate := cloneRuntimeConfig(current)
ssl := &candidate.SSL.TLS
ssl.DefaultDirectory = runtimepath.SSLReference(root, ssl.DefaultDirectory)
ssl.CustomDirectory = runtimepath.SSLReference(root, ssl.CustomDirectory)
ssl.CertificateRef = runtimepath.SSLReference(root, ssl.CertificateRef)
ssl.PrivateKeyRef = runtimepath.SSLReference(root, ssl.PrivateKeyRef)
if candidate.SSL.TLS.CertificateRef == current.SSL.TLS.CertificateRef && candidate.SSL.TLS.PrivateKeyRef == current.SSL.TLS.PrivateKeyRef && candidate.SSL.TLS.DefaultDirectory == current.SSL.TLS.DefaultDirectory && candidate.SSL.TLS.CustomDirectory == current.SSL.TLS.CustomDirectory {
return nil
}
setCandidateGeneration(&candidate, current.Generation+1)
return NewTransactionStore(root, validation).Commit(current, candidate)
}
+3 -2
View File
@@ -10,6 +10,7 @@ import (
"strings"
"git.pcux.cn/xuans/Smail/utils/credential"
"git.pcux.cn/xuans/Smail/utils/runtimepath"
"github.com/go-sql-driver/mysql"
)
@@ -58,7 +59,7 @@ func runtimeCompatibility(runtime RuntimeConfig) (*Config, error) {
HttpPort: root.HTTP.Port, HttpsPort: root.HTTP.HTTPSPort,
HttpsEnabled: 2, TLSMode: root.HTTP.TLSMode,
TrustedProxyCIDRs: append([]string(nil), root.HTTP.TrustedProxyCIDRs...),
DkimPrivateKeyPath: filepath.Join(ROOT_PATH, "data", "dkim", "dkim.priv"),
DkimPrivateKeyPath: filepath.Join(runtimepath.DKIMDirectory(ROOT_PATH), "dkim.priv"),
SSLType: SSLTypeUser,
SpamFilterLevel: runtime.Email.Policy.SpamFilterLevel,
LoginPolicy: runtime.SMTP.Relay.LoginPolicy,
@@ -86,7 +87,7 @@ func runtimeCompatibility(runtime RuntimeConfig) (*Config, error) {
}
}
// 迁移器保留的旧证书引用仍走严格的只读文件校验;新托管引用沿用托管解析器。
if strings.HasPrefix(tls.CertificateRef, managedSSLRelativeRoot+"/") {
if strings.HasPrefix(tls.CertificateRef, managedSSLRelativeRoot+"/") || strings.HasPrefix(tls.CertificateRef, "ssl/managed/") {
cfg.SSL = tls
} else {
cfg.SSLPublicKeyPath = resolvePath(tls.CertificateRef)
+8 -5
View File
@@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"strings"
"git.pcux.cn/xuans/Smail/utils/runtimepath"
)
var ErrSSLFileUnavailable = errors.New("SSL certificate file is missing or unreadable")
@@ -32,7 +34,8 @@ func prepareCustomSSLPaths(root string, candidate *RuntimeConfig) (*customSSLMat
return nil, ErrInvalidSSLReference
}
// 已保存的受管引用只复验,不重复导入;普通相对路径以项目运行根目录为基准。
if strings.HasPrefix(certificatePath, managedSSLRelativeRoot+"/") && strings.HasPrefix(privateKeyPath, managedSSLRelativeRoot+"/") {
managedRoot := runtimepath.SSLReference(root, managedSSLRelativeRoot)
if strings.HasPrefix(certificatePath, managedRoot+"/") && strings.HasPrefix(privateKeyPath, managedRoot+"/") {
return nil, validateCustomSSLReferences(root, *candidate)
}
var err error
@@ -57,11 +60,11 @@ func prepareCustomSSLPaths(root string, candidate *RuntimeConfig) (*customSSLMat
return nil, err
}
ssl.Mode = ManagedSSLModeUpload
ssl.DefaultDirectory = "data/ssl"
ssl.CustomDirectory = "data/ssl"
ssl.DefaultDirectory = runtimepath.SSLReference(root, "data/ssl")
ssl.CustomDirectory = runtimepath.SSLReference(root, "data/ssl")
// 仅在内存中使用合法占位引用完成跨分区校验,最终候选必须换成独立版本。
ssl.CertificateRef = managedSSLReference(ManagedSSLModeUpload, "public.crt")
ssl.PrivateKeyRef = managedSSLReference(ManagedSSLModeUpload, "private.key")
ssl.CertificateRef = managedSSLReferenceAtRoot(root, ManagedSSLModeUpload, "public.crt")
ssl.PrivateKeyRef = managedSSLReferenceAtRoot(root, ManagedSSLModeUpload, "private.key")
return material, nil
}
+9 -5
View File
@@ -195,8 +195,8 @@ func importManagedSSLAtRoot(root string, mode string, privateKeyPEM, certificate
}
return ManagedSSLReference{
Mode: mode,
CertificateRef: managedSSLReference(mode, "public.crt"),
PrivateKeyRef: managedSSLReference(mode, "private.key"),
CertificateRef: managedSSLReferenceAtRoot(root, mode, "public.crt"),
PrivateKeyRef: managedSSLReferenceAtRoot(root, mode, "private.key"),
CertificatePath: certificatePath,
PrivateKeyPath: privateKeyPath,
}, nil
@@ -388,15 +388,19 @@ func managedSSLRootPathAtRoot(root string, mode string) (string, error) {
if mode == ManagedSSLModeUpload {
folder = "upload"
}
return filepath.Join(rootPath, managedSSLRelativeRoot, folder), nil
return filepath.Join(rootPath, runtimepath.SSLReference(root, managedSSLRelativeRoot), folder), nil
}
func managedSSLReference(mode, name string) string {
return managedSSLReferenceAtRoot(ROOT_PATH, mode, name)
}
func managedSSLReferenceAtRoot(root, mode, name string) string {
folder := "default"
if mode == ManagedSSLModeUpload {
folder = "upload"
}
return filepath.ToSlash(filepath.Join(managedSSLRelativeRoot, folder, "current", name))
return filepath.ToSlash(filepath.Join(runtimepath.SSLReference(root, managedSSLRelativeRoot), folder, "current", name))
}
func resolveManagedReference(rootPath, reference, expectedName string) (string, error) {
@@ -404,7 +408,7 @@ func resolveManagedReference(rootPath, reference, expectedName string) (string,
}
func resolveManagedReferenceAtRoot(root, rootPath, reference, expectedName string) (string, error) {
rawReference := runtimepath.LegacyReference(strings.TrimSpace(reference))
rawReference := runtimepath.SSLReference(root, strings.TrimSpace(reference))
if rawReference == "" || strings.Contains(rawReference, `\`) || filepath.IsAbs(rawReference) || isWindowsAbsolute(rawReference) {
return "", ErrInvalidSSLReference
}
+14
View File
@@ -0,0 +1,14 @@
//go:build !windows
package config
import (
"os"
"golang.org/x/sys/unix"
)
func lockStateFile(file *os.File) (func(), error) {
err := unix.Flock(int(file.Fd()), unix.LOCK_EX|unix.LOCK_NB)
return func() { _ = unix.Flock(int(file.Fd()), unix.LOCK_UN) }, err
}
+15
View File
@@ -0,0 +1,15 @@
//go:build windows
package config
import (
"os"
"golang.org/x/sys/windows"
)
func lockStateFile(file *os.File) (func(), error) {
position := &windows.Overlapped{Offset: 0x60000000}
err := windows.LockFileEx(windows.Handle(file.Fd()), windows.LOCKFILE_EXCLUSIVE_LOCK|windows.LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, position)
return func() { _ = windows.UnlockFileEx(windows.Handle(file.Fd()), 0, 1, 0, position) }, err
}
+379
View File
@@ -0,0 +1,379 @@
package config
import (
"database/sql"
"encoding/json"
"errors"
"os"
"path/filepath"
"sort"
"time"
"git.pcux.cn/xuans/Smail/utils/atomicfile"
"git.pcux.cn/xuans/Smail/utils/runtimepath"
_ "modernc.org/sqlite"
)
// 状态数据库只保存配置历史和恢复事务,五个生效配置文件仍是配置读取来源。
type stateRecord struct {
Transaction *TransactionManifest `json:"transaction,omitempty"`
Backup *ConfigBackupManifest `json:"backup,omitempty"`
Original map[string][]byte `json:"original,omitempty"`
Candidate map[string][]byte `json:"candidate,omitempty"`
}
func (s *TransactionStore) lockState() (func(), error) {
db, err := s.openState()
if err != nil {
return nil, err
}
if err := db.Close(); err != nil {
return nil, err
}
file, err := os.OpenFile(filepath.Join(s.root, "data", "smail-state.db"), os.O_RDWR, 0600)
if err != nil {
return nil, err
}
unlock, err := lockStateFile(file)
if err != nil {
file.Close()
return nil, errors.New("配置状态正在被其他进程使用,请稍后重试")
}
return func() { unlock(); file.Close() }, nil
}
func (s *TransactionStore) openState() (*sql.DB, error) {
directory, err := runtimepath.Directory(s.root)
if err != nil {
return nil, err
}
filename := filepath.Join(directory, "smail-state.db")
info, err := os.Lstat(filename)
if err == nil && (!info.Mode().IsRegular() || info.Mode()&os.ModeSymlink != 0) {
return nil, ErrManagerStorage
}
if err != nil && !os.IsNotExist(err) {
return nil, err
}
db, err := sql.Open("sqlite", filepath.ToSlash(filename))
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)
for _, statement := range []string{
"PRAGMA busy_timeout=5000", "PRAGMA synchronous=FULL",
`CREATE TABLE IF NOT EXISTS config_records (kind TEXT NOT NULL, id TEXT NOT NULL, payload BLOB NOT NULL, digest TEXT NOT NULL, PRIMARY KEY(kind,id))`,
} {
if _, err = db.Exec(statement); err != nil {
db.Close()
return nil, err
}
}
if err = os.Chmod(filename, 0600); err != nil {
db.Close()
return nil, err
}
return db, nil
}
func (s *TransactionStore) saveState(kind, id string, record stateRecord) error {
if !transactionIDPattern.MatchString(id) {
return ErrTransactionInvalid
}
data, err := json.Marshal(record)
if err != nil {
return err
}
db, err := s.openState()
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec(`INSERT INTO config_records(kind,id,payload,digest) VALUES(?,?,?,?) ON CONFLICT(kind,id) DO UPDATE SET payload=excluded.payload,digest=excluded.digest`, kind, id, data, SectionDigest(data))
return err
}
func validateStateFiles(files map[string][]byte, digests map[string]string) error {
if !validFileDigestSet(digests) || len(files) != len(digests) {
return ErrTransactionInvalid
}
for name, digest := range digests {
if len(files[name]) == 0 || len(files[name]) > maxConfigFileSize || SectionDigest(files[name]) != digest {
return ErrConfigDigestMismatch
}
}
return nil
}
func (s *TransactionStore) stateRecords(kind string) ([]stateRecord, error) {
db, err := s.openState()
if err != nil {
return nil, err
}
defer db.Close()
var oversized int
if err := db.QueryRow("SELECT count(*) FROM config_records WHERE kind=? AND length(payload)>?", kind, 16*maxConfigFileSize).Scan(&oversized); err != nil || oversized != 0 {
return nil, errors.Join(ErrTransactionInvalid, err)
}
rows, err := db.Query("SELECT id,payload,digest FROM config_records WHERE kind=? ORDER BY id", kind)
if err != nil {
return nil, err
}
defer rows.Close()
var result []stateRecord
for rows.Next() {
var id, digest string
var data []byte
if err := rows.Scan(&id, &data, &digest); err != nil {
return nil, err
}
if !transactionIDPattern.MatchString(id) || len(data) > 16*maxConfigFileSize || SectionDigest(data) != digest {
return nil, ErrConfigDigestMismatch
}
var record stateRecord
if err := ParseStrict(data, &record); err != nil {
return nil, err
}
switch kind {
case "transaction":
if record.Transaction == nil || record.Transaction.ID != id || record.Backup != nil {
return nil, ErrTransactionInvalid
}
if err := validateTransactionManifest(*record.Transaction); err != nil {
return nil, err
}
if err := validateStateFiles(record.Original, record.Transaction.OriginalFiles); err != nil {
return nil, err
}
if err := validateStateFiles(record.Candidate, record.Transaction.CandidateFiles); err != nil {
return nil, err
}
for _, item := range []struct {
files map[string][]byte
generation uint64
}{
{record.Original, record.Transaction.FromGeneration},
{record.Candidate, record.Transaction.ToGeneration},
} {
config, err := parseRuntimeFiles(item.files, s.validation)
if err != nil || config.Generation != item.generation {
return nil, errors.Join(ErrTransactionInvalid, err)
}
}
case "backup":
if record.Backup == nil || record.Backup.ID != id || record.Transaction != nil || record.Backup.CreatedAt.IsZero() {
return nil, ErrTransactionInvalid
}
if err := validateStateFiles(record.Original, record.Backup.Files); err != nil {
return nil, err
}
config, err := parseRuntimeFiles(record.Original, s.validation)
if err != nil {
return nil, err
}
if config.Generation != record.Backup.Generation {
return nil, ErrConfigGenerationMismatch
}
default:
return nil, ErrTransactionInvalid
}
result = append(result, record)
}
return result, rows.Err()
}
func (s *TransactionStore) removeState(kind, id string) error {
db, err := s.openState()
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec("DELETE FROM config_records WHERE kind=? AND id=?", kind, id)
return err
}
func writeStateLive(directory string, files map[string][]byte, rootOnly bool) error {
for _, name := range transactionFileNames() {
if (name == "config.json") != rootOnly {
continue
}
target := filepath.Join(directory, name)
if info, err := os.Lstat(target); err == nil && !info.Mode().IsRegular() {
return ErrManagerStorage
} else if err != nil && !os.IsNotExist(err) {
return err
}
if err := atomicfile.Write(target, files[name], 0600); err != nil {
return err
}
}
return atomicfile.SyncDirectory(directory)
}
func (s *TransactionStore) commitState(current, candidate RuntimeConfig) error {
unlock, err := s.lockState()
if err != nil {
return err
}
defer unlock()
if current.Generation == 0 || candidate.Generation != current.Generation+1 {
return ErrGenerationConflict
}
if err := ValidateRuntimeConfig(candidate, s.validation); err != nil {
return err
}
if records, err := s.stateRecords("transaction"); err != nil || len(records) != 0 {
return errors.Join(ErrConfigRecoveryRequired, err)
}
live, err := NewConfigLoader(s.root, ConfigLoaderOptions{Validation: s.validation}).Load()
if err != nil || live.Generation != current.Generation {
return errors.Join(ErrGenerationConflict, err)
}
original, err := marshalRuntimeFiles(current)
if err != nil {
return err
}
files, err := marshalRuntimeFiles(candidate)
if err != nil {
return err
}
directory, err := secureManagerConfigDirectory(s.root)
if err != nil {
return err
}
id, err := newTransactionID()
if err != nil {
return err
}
manifest := TransactionManifest{
ID: id, FromGeneration: current.Generation, ToGeneration: candidate.Generation,
Sections: append([]Section(nil), requiredPartitionSections...), OriginalFiles: digestFiles(original),
CandidateFiles: digestFiles(files), Phase: TransactionPrepared, CreatedAt: time.Now().UTC(),
}
record := stateRecord{Transaction: &manifest, Original: original, Candidate: files}
persist := func(phase TransactionPhase) error {
manifest.Phase = phase
if err := s.saveState("transaction", id, record); err != nil {
return err
}
return s.beforePhase(phase)
}
if err := persist(TransactionPrepared); err != nil {
return err
}
if err := writeStateLive(directory, files, false); err != nil {
return err
}
if err := persist(TransactionSectionsCommitted); err != nil {
return err
}
if err := writeStateLive(directory, files, true); err != nil {
return err
}
if err := persist(TransactionRootCommitted); err != nil {
return err
}
loaded, err := NewConfigLoader(s.root, ConfigLoaderOptions{Validation: s.validation}).Load()
if err != nil || loaded.Generation != candidate.Generation {
return errors.Join(ErrConfigGenerationMismatch, err)
}
if err := persist(TransactionVerified); err != nil {
return err
}
return s.removeState("transaction", id)
}
func (s *TransactionStore) recoverState() error {
unlock, err := s.lockState()
if err != nil {
return err
}
defer unlock()
if entries, err := os.ReadDir(filepath.Join(s.root, "data", "transaction")); err == nil && len(entries) != 0 {
return ErrConfigRecoveryRequired
} else if err != nil && !os.IsNotExist(err) {
return err
}
records, err := s.stateRecords("transaction")
if err != nil {
return err
}
if len(records) > 1 {
return ErrTransactionInvalid
}
for _, record := range records {
manifest := record.Transaction
loaded, err := NewConfigLoader(s.root, ConfigLoaderOptions{Validation: s.validation}).Load()
if err == nil && loaded.Generation > manifest.ToGeneration {
return ErrConfigGenerationMismatch
}
if err != nil || loaded.Generation < manifest.ToGeneration {
if _, err := parseRuntimeFiles(record.Original, s.validation); err != nil {
return err
}
directory, err := secureManagerConfigDirectory(s.root)
if err != nil {
return err
}
if err := writeStateLive(directory, record.Original, false); err != nil {
return err
}
if err := writeStateLive(directory, record.Original, true); err != nil {
return err
}
if _, err := NewConfigLoader(s.root, ConfigLoaderOptions{Validation: s.validation}).Load(); err != nil {
return err
}
}
if err := s.removeState("transaction", manifest.ID); err != nil {
return err
}
}
return nil
}
func (s *TransactionStore) stateBackups() ([]stateRecord, error) {
items, err := s.stateRecords("backup")
sort.Slice(items, func(i, j int) bool {
if items[i].Backup.CreatedAt.Equal(items[j].Backup.CreatedAt) {
return items[i].Backup.ID > items[j].Backup.ID
}
return items[i].Backup.CreatedAt.After(items[j].Backup.CreatedAt)
})
return items, err
}
func (s *TransactionStore) createStateBackup(config RuntimeConfig, now time.Time) (string, error) {
unlock, err := s.lockState()
if err != nil {
return "", err
}
defer unlock()
if err := ValidateRuntimeConfig(config, s.validation); err != nil {
return "", err
}
files, err := marshalRuntimeFiles(config)
if err != nil {
return "", err
}
id, err := newTransactionID()
if err != nil {
return "", err
}
manifest := ConfigBackupManifest{ID: id, Generation: config.Generation, CreatedAt: now, Files: digestFiles(files)}
if err := s.saveState("backup", id, stateRecord{Backup: &manifest, Original: files}); err != nil {
return "", err
}
items, err := s.stateBackups()
if err != nil {
return "", err
}
for index, item := range items {
if index > 0 && (index >= configBackupLimit || now.Sub(item.Backup.CreatedAt) > configBackupMaxAge) {
if err := s.removeState("backup", item.Backup.ID); err != nil {
return "", err
}
}
}
return id, nil
}
+35
View File
@@ -77,6 +77,9 @@ func (s *TransactionStore) Commit(current, candidate RuntimeConfig) error {
if s == nil || s.root == "" {
return ErrManagerStorage
}
if runtimepath.DeploymentLayout(s.root) {
return s.commitState(current, candidate)
}
if current.Generation == 0 || candidate.Generation != current.Generation+1 {
return ErrGenerationConflict
}
@@ -197,6 +200,9 @@ func (s *TransactionStore) Recover() error {
if s == nil || s.root == "" {
return ErrManagerStorage
}
if runtimepath.DeploymentLayout(s.root) {
return s.recoverState()
}
directory, err := secureManagerConfigDirectory(s.root)
if err != nil {
return err
@@ -600,6 +606,9 @@ func (s *TransactionStore) CreateBackupIfDue(runtime RuntimeConfig, now time.Tim
return "", false, err
}
if found && now.Before(latest.CreatedAt.Add(configBackupInterval)) {
if runtimepath.DeploymentLayout(s.root) {
return latest.ID, false, nil
}
return filepath.Join(s.root, "data", "backups-v2", latest.ID), false, nil
}
path, err := s.createBackupAt(runtime, now)
@@ -618,6 +627,9 @@ func (s *TransactionStore) createBackupAt(runtime RuntimeConfig, createdAt time.
} else {
createdAt = createdAt.UTC()
}
if runtimepath.DeploymentLayout(s.root) {
return s.createStateBackup(runtime, createdAt)
}
files, err := marshalRuntimeFiles(runtime)
if err != nil {
return "", err
@@ -681,6 +693,13 @@ func (s *TransactionStore) latestBackup() (ConfigBackupManifest, bool, error) {
if s == nil || s.root == "" {
return latest, false, ErrManagerStorage
}
if runtimepath.DeploymentLayout(s.root) {
items, err := s.stateBackups()
if err != nil || len(items) == 0 {
return latest, false, err
}
return *items[0].Backup, true, nil
}
configDirectory, err := secureManagerConfigDirectory(s.root)
if err != nil {
return latest, false, err
@@ -717,6 +736,18 @@ func (s *TransactionStore) LoadBackup(generation uint64) (RuntimeConfig, error)
if s == nil || s.root == "" || generation == 0 {
return RuntimeConfig{}, ErrNoBackup
}
if runtimepath.DeploymentLayout(s.root) {
items, err := s.stateBackups()
if err != nil {
return RuntimeConfig{}, err
}
for _, item := range items {
if item.Backup.Generation == generation {
return parseRuntimeFiles(item.Original, s.validation)
}
}
return RuntimeConfig{}, ErrNoBackup
}
configDirectory, err := secureManagerConfigDirectory(s.root)
if err != nil {
return RuntimeConfig{}, err
@@ -864,6 +895,10 @@ func parseRuntimeFiles(files map[string][]byte, validation RuntimeValidationOpti
return RuntimeConfig{}, err
}
for _, section := range requiredPartitionSections {
name, _ := FixedSectionFile(section)
if SectionDigest(files[name]) != root.Sections[section].SHA256 {
return RuntimeConfig{}, ErrConfigDigestMismatch
}
if err := ValidateSectionMeta(section, sectionMeta(runtime, section), runtime.Generation); err != nil {
return RuntimeConfig{}, err
}
+2 -2
View File
@@ -10,11 +10,11 @@ import (
"git.pcux.cn/xuans/Smail/config"
"git.pcux.cn/xuans/Smail/utils/context"
securefile "git.pcux.cn/xuans/Smail/utils/file"
"git.pcux.cn/xuans/Smail/utils/runtimepath"
"github.com/emersion/go-msgauth/dkim"
log "github.com/sirupsen/logrus"
"io"
"os"
"path/filepath"
"strings"
)
@@ -41,7 +41,7 @@ func Init() error {
}
func loadPrivateKey(path string) (crypto.Signer, error) {
basePath := filepath.Join(config.ROOT_PATH, "data", "dkim")
basePath := runtimepath.DKIMDirectory(config.ROOT_PATH)
privateName, err := securefile.ControlledRelativePath(basePath, path)
if err != nil {
return nil, err
+5 -2
View File
@@ -275,7 +275,7 @@ func Init(serverVersion string) {
return
}
root, err := filepath.Abs("./plugins")
root, err := pluginRoot()
if err != nil {
log.Error("Plugin root resolve failed")
return
@@ -696,7 +696,10 @@ func PersistPluginConfig(pluginName, payload string) error {
if !ok {
return errors.New("plugin configuration persistence is unavailable")
}
root, err := filepath.Abs("./plugins")
if bundledPlugins != nil {
return errors.New("内置插件配置必须通过 plugins.json 配置服务保存")
}
root, err := pluginRoot()
if err != nil || !securePluginDirectory(root) || !securePluginDirectoryOwner(root) {
return errors.New("plugin directory is unsafe")
}
+52
View File
@@ -0,0 +1,52 @@
package hooks
import (
"errors"
"io/fs"
"os"
"path/filepath"
"sync"
)
var bundledPlugins fs.FS
var bundledRoot string
var bundleMu sync.Mutex
// SetBundledPlugins 注入编译时封装的插件;插件仍作为独立进程运行,不放宽生产隔离。
func SetBundledPlugins(files fs.FS) {
bundledPlugins = files
}
func pluginRoot() (string, error) {
bundleMu.Lock()
defer bundleMu.Unlock()
if bundledPlugins == nil {
return filepath.Abs("./plugins")
}
if bundledRoot != "" {
return bundledRoot, nil
}
entries, err := fs.ReadDir(bundledPlugins, ".")
if err != nil || len(entries) != 3 {
return "", errors.New("内置插件包不完整")
}
// 操作系统临时沙箱不属于部署文件,避免向 config 或 data 解包可执行程序。
root, err := os.MkdirTemp("", "smail-bundled-")
if err != nil {
return "", err
}
for _, entry := range entries {
if entry.IsDir() || !entry.Type().IsRegular() {
return "", errors.New("内置插件包含有无效文件")
}
data, err := fs.ReadFile(bundledPlugins, entry.Name())
if err != nil || len(data) == 0 {
return "", errors.New("内置插件内容不可用")
}
if err := os.WriteFile(filepath.Join(root, entry.Name()), data, 0700); err != nil {
return "", err
}
}
bundledRoot = root
return root, nil
}
+3
View File
@@ -14,6 +14,9 @@ import (
// Run 启动 Smail 主服务。
func Run() error {
if err := config.InitializeDeployment(config.ROOT_PATH); err != nil {
return err
}
if err := runtimepath.CheckReady(config.ROOT_PATH); err != nil {
return err
}
+2 -1
View File
@@ -32,6 +32,7 @@ import (
"git.pcux.cn/xuans/Smail/session"
"git.pcux.cn/xuans/Smail/signal"
"git.pcux.cn/xuans/Smail/utils/credential"
"git.pcux.cn/xuans/Smail/utils/runtimepath"
log "github.com/sirupsen/logrus"
// 导入 HTTP 客户端,用于等待服务启动。
"net/http"
@@ -439,7 +440,7 @@ func stopAdminResetControl() {
func dirInit() error {
// 初始化与配置读写使用同一个运行根目录,不依赖启动时的工作目录。
root := filepath.Join(config.ROOT_PATH, "data")
for _, directory := range []string{filepath.Join(config.ROOT_PATH, "config"), root, filepath.Join(root, "dkim"), filepath.Join(root, "ssl")} {
for _, directory := range []string{filepath.Join(config.ROOT_PATH, "config"), root, runtimepath.DKIMDirectory(config.ROOT_PATH), runtimepath.SSLDirectory(config.ROOT_PATH)} {
if err := os.MkdirAll(directory, 0700); err != nil {
return fmt.Errorf("创建运行配置目录失败: %w", err)
}
+2 -1
View File
@@ -12,6 +12,7 @@ import (
"git.pcux.cn/xuans/Smail/models"
"git.pcux.cn/xuans/Smail/utils/context"
"git.pcux.cn/xuans/Smail/utils/password"
"git.pcux.cn/xuans/Smail/utils/runtimepath"
"git.pcux.cn/xuans/Smail/utils/validation"
log "github.com/sirupsen/logrus"
)
@@ -117,7 +118,7 @@ func HasAuth(ctx *context.Context, email *models.Email) bool {
}
func DkimGen() (string, error) {
basePath := filepath.Join(config.ROOT_PATH, "data", "dkim")
basePath := runtimepath.DKIMDirectory(config.ROOT_PATH)
cfg := config.CurrentConfig()
if cfg == nil {
return "", fmt.Errorf("runtime configuration is unavailable")
+1 -1
View File
@@ -48,7 +48,7 @@ func NewVault(root string) (*Vault, error) {
return nil, ErrVaultUnavailable
}
}
configDir, err := runtimepath.Directory(root)
configDir, err := runtimepath.SecureDirectory(root, filepath.Base(runtimepath.KeyDirectory(root)))
if err != nil {
return nil, ErrVaultUnavailable
}
+4 -3
View File
@@ -12,6 +12,7 @@ import (
"git.pcux.cn/xuans/Smail/signal"
"git.pcux.cn/xuans/Smail/utils/async"
"git.pcux.cn/xuans/Smail/utils/errors"
"git.pcux.cn/xuans/Smail/utils/runtimepath"
"github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/challenge/dns01"
log "github.com/sirupsen/logrus"
@@ -116,7 +117,7 @@ func SetSSL(sslType, priKey, crtKey string) error {
cfg.SSL = config.SSLConfig{
Management: config.SSLManagementProject,
Mode: config.ManagedSSLModeDefault,
DefaultDirectory: "data/ssl",
DefaultDirectory: runtimepath.SSLReference(config.ROOT_PATH, "data/ssl"),
}
if cfg.SSLType == config.SSLTypeAutoDNS {
cfg.SSL.Challenge = config.SSLChallengeDNS
@@ -190,12 +191,12 @@ func applyManagedSSLReference(cfg *config.Config, reference config.ManagedSSLRef
cfg.SSL = config.SSLConfig{
Management: management,
Mode: reference.Mode,
DefaultDirectory: "data/ssl",
DefaultDirectory: runtimepath.SSLReference(config.ROOT_PATH, "data/ssl"),
CertificateRef: reference.CertificateRef,
PrivateKeyRef: reference.PrivateKeyRef,
}
if reference.Mode == config.ManagedSSLModeUpload {
cfg.SSL.CustomDirectory = "data/ssl/managed/upload"
cfg.SSL.CustomDirectory = runtimepath.SSLReference(config.ROOT_PATH, "data/ssl/managed/upload")
} else if cfg.SSLType == config.SSLTypeAutoDNS {
cfg.SSL.Challenge = config.SSLChallengeDNS
} else {
+3 -2
View File
@@ -10,6 +10,7 @@ import (
"git.pcux.cn/xuans/Smail/config"
"git.pcux.cn/xuans/Smail/utils/atomicfile"
securefile "git.pcux.cn/xuans/Smail/utils/file"
"git.pcux.cn/xuans/Smail/utils/runtimepath"
)
func writeCertificateBundle(cfg *config.Config, privateKey, certificate, issuer []byte) error {
@@ -31,7 +32,7 @@ func writeCertificateBundle(cfg *config.Config, privateKey, certificate, issuer
return err
}
defer workspaceRoot.Close()
baseRelative := filepath.Join("data", "ssl")
baseRelative := filepath.FromSlash(runtimepath.SSLReference(rootPath, "data/ssl"))
if err := ensureSecureRootDirectory(workspaceRoot, baseRelative); err != nil {
return err
}
@@ -57,7 +58,7 @@ func writeCertificateBundle(cfg *config.Config, privateKey, certificate, issuer
}
relativePath, err := securefile.ControlledRelativePath(basePath, item.path)
if err != nil {
return errors.New("SSL file path must stay inside data/ssl")
return errors.New("证书文件必须位于受保护的 SSL 目录中")
}
if _, exists := seen[relativePath]; exists {
return errors.New("SSL file paths must be unique")
+6 -2
View File
@@ -7,6 +7,7 @@ import (
"encoding/base64"
"errors"
"git.pcux.cn/xuans/Smail/utils/atomicfile"
"git.pcux.cn/xuans/Smail/utils/runtimepath"
"io"
"os"
"path/filepath"
@@ -85,7 +86,7 @@ func loadOrCreateKey(root string) ([]byte, error) {
if !os.IsNotExist(err) {
return nil, err
}
directory := filepath.Join(root, "data")
directory := runtimepath.KeyDirectory(root)
if err := os.MkdirAll(directory, 0700); err != nil {
return nil, err
}
@@ -158,13 +159,16 @@ func newCredentialKeyTemporaryName() (string, error) {
}
func readKey(root string) ([]byte, error) {
if err := runtimepath.CheckKeyLayout(root); err != nil {
return nil, err
}
// 旧密钥尚未迁移时拒绝生成新密钥,避免现有密文永久失去读取能力。
if _, err := os.Lstat(filepath.Join(root, "config", "credential.key")); err == nil {
return nil, errors.New("凭据密钥尚未迁移到 data")
} else if !os.IsNotExist(err) {
return nil, err
}
directory := filepath.Join(root, "data")
directory := runtimepath.KeyDirectory(root)
if err := validateKeyDirectory(directory); err != nil {
return nil, err
}
+84
View File
@@ -0,0 +1,84 @@
package runtimepath
import (
"errors"
"os"
"path/filepath"
)
// CheckKeyLayout 拒绝同时存在新旧密钥,避免误用另一套加密材料。
func CheckKeyLayout(root string) error {
legacy, modern := false, false
for _, name := range []string{"credential.key", "external-email-encryption.key", "external-email-fingerprint.key"} {
if _, err := os.Lstat(filepath.Join(root, "data", name)); err == nil {
legacy = true
} else if !os.IsNotExist(err) {
return err
}
if _, err := os.Lstat(filepath.Join(root, "ssl", name)); err == nil {
modern = true
} else if !os.IsNotExist(err) {
return err
}
}
if legacy && modern {
return errors.New("新旧目录同时存在密钥,必须先人工确认迁移,不能自动选择")
}
return nil
}
// DeploymentLayout 识别已迁移部署,旧部署继续只读原路径,不能静默生成替代密钥。
func DeploymentLayout(root string) bool {
if _, err := os.Lstat(filepath.Join(root, "data", "credential.key")); err == nil {
return false
}
for _, name := range []string{"data/smail-state.db", "ssl/credential.key"} {
info, err := os.Lstat(filepath.Join(root, filepath.FromSlash(name)))
if err == nil && info.Mode().IsRegular() {
return true
}
}
return false
}
func SecureDirectory(root, name string) (string, error) {
directory := filepath.Join(root, name)
if err := os.Mkdir(directory, 0700); err != nil && !os.IsExist(err) {
return "", err
}
if err := safeDirectory(directory); err != nil {
return "", err
}
return directory, os.Chmod(directory, 0700)
}
func KeyDirectory(root string) string {
if DeploymentLayout(root) {
return filepath.Join(root, "ssl")
}
return filepath.Join(root, "data")
}
func SSLReference(root, value string) string {
value = LegacyReference(value)
if !DeploymentLayout(root) {
return value
}
for _, prefix := range []string{"data/ssl", "data/dkim"} {
if value == prefix || len(value) > len(prefix) && value[:len(prefix)+1] == prefix+"/" {
if prefix == "data/dkim" {
return "ssl/dkim" + value[len(prefix):]
}
return "ssl" + value[len(prefix):]
}
}
return value
}
func SSLDirectory(root string) string {
return filepath.Join(root, filepath.FromSlash(SSLReference(root, "data/ssl")))
}
func DKIMDirectory(root string) string {
return filepath.Join(root, filepath.FromSlash(SSLReference(root, "data/dkim")))
}
+12 -2
View File
@@ -150,6 +150,9 @@ func safeDirectory(path string) error {
// CheckReady 防止遗漏迁移时创建空账号库或重新生成加密密钥。
func CheckReady(root string) error {
if err := CheckKeyLayout(root); err != nil {
return err
}
if _, err := os.Lstat(filepath.Join(root, "data", migrationLock)); err == nil || !os.IsNotExist(err) {
return errors.New("运行目录迁移尚未结束,请保留现场并核对迁移锁")
}
@@ -189,14 +192,21 @@ func CheckReady(root string) error {
required = append(required, config.Database.LocalRef)
}
for _, name := range required {
base := filepath.Join(root, "data")
if strings.HasSuffix(name, ".key") {
base = KeyDirectory(root)
}
if err := safeDirectory(base); err != nil {
return err
}
parent := filepath.Dir(name)
for parent != "." {
if err := safeDirectory(filepath.Join(root, "data", parent)); err != nil {
if err := safeDirectory(filepath.Join(base, parent)); err != nil {
return err
}
parent = filepath.Dir(parent)
}
info, err := os.Lstat(filepath.Join(root, "data", name))
info, err := os.Lstat(filepath.Join(base, name))
if err != nil || !info.Mode().IsRegular() || info.Size() == 0 {
return fmt.Errorf("已初始化部署缺少必要运行文件,拒绝重新创建:%s", name)
}