1 parent
48f4b24781
commit
a045a0cd17
27 files changed
+1209
-24
No files matched your search
@@ -86,8 +86,8 @@ func loadOrCreateKey(root string) ([]byte, error) {
|
||||
if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
directory := runtimepath.KeyDirectory(root)
|
||||
if err := os.MkdirAll(directory, 0700); err != nil {
|
||||
directory, err := runtimepath.SecureKeyDirectory(root, "credential.key")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateKeyDirectory(directory); err != nil {
|
||||
@@ -169,6 +169,11 @@ func readKey(root string) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
directory := runtimepath.KeyDirectory(root)
|
||||
if runtimepath.DeploymentLayout(root) {
|
||||
if err := validateKeyDirectory(filepath.Join(root, "ssl")); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err := validateKeyDirectory(directory); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
package runtimepath
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/atomicfile"
|
||||
)
|
||||
|
||||
// BusinessKeyNames 返回固定业务密钥清单,不能由外部输入扩展迁移范围。
|
||||
func BusinessKeyNames() []string {
|
||||
return []string{"credential.key", "external-email-encryption.key", "external-email-fingerprint.key"}
|
||||
}
|
||||
|
||||
func businessKeyFolder(name string) string {
|
||||
switch name {
|
||||
case "credential.key":
|
||||
return "credentials"
|
||||
case "external-email-encryption.key":
|
||||
return "external-email-encryption"
|
||||
case "external-email-fingerprint.key":
|
||||
return "external-email-fingerprint"
|
||||
default:
|
||||
panic("unknown business key")
|
||||
}
|
||||
}
|
||||
|
||||
func groupedKeyPath(root, name string) string {
|
||||
return filepath.Join(root, "ssl", businessKeyFolder(name), name)
|
||||
}
|
||||
|
||||
// KeyPath 兼容尚未升级的部署;正式启动完成归档后只使用业务子目录。
|
||||
func KeyPath(root, name string) string {
|
||||
folder := businessKeyFolder(name)
|
||||
if !DeploymentLayout(root) {
|
||||
return filepath.Join(root, "data", name)
|
||||
}
|
||||
if _, err := os.Lstat(filepath.Join(root, "ssl", name)); err == nil {
|
||||
return filepath.Join(root, "ssl", name)
|
||||
}
|
||||
if _, err := os.Lstat(filepath.Join(root, "ssl", folder)); !os.IsNotExist(err) {
|
||||
return groupedKeyPath(root, name)
|
||||
}
|
||||
return filepath.Join(root, "ssl", name)
|
||||
}
|
||||
|
||||
// SecureKeyDirectory 逐层验证目录,不能通过父目录链接写入外部位置。
|
||||
func SecureKeyDirectory(root, name string) (string, error) {
|
||||
if err := CheckKeyLayout(root); err != nil {
|
||||
return "", err
|
||||
}
|
||||
directory := filepath.Dir(KeyPath(root, name))
|
||||
base := "data"
|
||||
if DeploymentLayout(root) {
|
||||
base = "ssl"
|
||||
}
|
||||
parent, err := SecureDirectory(root, base)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if filepath.Clean(directory) == filepath.Clean(parent) {
|
||||
return parent, nil
|
||||
}
|
||||
return SecureDirectory(parent, businessKeyFolder(name))
|
||||
}
|
||||
|
||||
// PrepareDeploymentKeys 由主程序启动入口在独占状态锁内调用,不依赖安装脚本。
|
||||
// 每次重命名保留原字节;中断后的混合位置可在下次启动继续迁移。
|
||||
func PrepareDeploymentKeys(root string) error {
|
||||
if !DeploymentLayout(root) {
|
||||
return nil
|
||||
}
|
||||
if err := CheckReady(root); err != nil {
|
||||
return err
|
||||
}
|
||||
var moves []Move
|
||||
for _, name := range BusinessKeyNames() {
|
||||
source := KeyPath(root, name)
|
||||
info, err := os.Lstat(source)
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
if err != nil || !info.Mode().IsRegular() || info.Size() != 32 {
|
||||
return fmt.Errorf("业务密钥无效,拒绝迁移:%s", name)
|
||||
}
|
||||
if err := safeDirectory(filepath.Join(root, "ssl")); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := safeDirectory(filepath.Dir(source)); err != nil {
|
||||
return err
|
||||
}
|
||||
if runtime.GOOS != "windows" && info.Mode().Perm()&0077 != 0 {
|
||||
return errors.New("业务密钥权限过于宽松,拒绝迁移")
|
||||
}
|
||||
if source != groupedKeyPath(root, name) {
|
||||
moves = append(moves, Move{Source: source, Target: groupedKeyPath(root, name)})
|
||||
}
|
||||
}
|
||||
ssl, err := SecureDirectory(root, "ssl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 先检查所有目标,不能移动第一把密钥后才发现另一业务目录不安全。
|
||||
for _, folder := range []string{"credentials", "external-email-encryption", "external-email-fingerprint", "dkim", "managed"} {
|
||||
if err := safeDirectory(filepath.Join(ssl, folder)); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, folder := range []string{"config", "log"} {
|
||||
if _, err := SecureDirectory(root, folder); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, folder := range []string{"credentials", "external-email-encryption", "external-email-fingerprint", "dkim", "managed"} {
|
||||
if _, err := SecureDirectory(ssl, folder); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, move := range moves {
|
||||
if _, err := os.Lstat(move.Target); !os.IsNotExist(err) {
|
||||
return errors.New("密钥迁移目标已存在,拒绝覆盖")
|
||||
}
|
||||
if err := os.Rename(move.Source, move.Target); err != nil {
|
||||
return fmt.Errorf("业务密钥归档未完成,下次启动将继续:%w", err)
|
||||
}
|
||||
if err := atomicfile.SyncDirectory(filepath.Dir(move.Target)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := atomicfile.SyncDirectory(ssl); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return CheckKeyLayout(root)
|
||||
}
|
||||
@@ -4,17 +4,26 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CheckKeyLayout 拒绝同时存在新旧密钥,避免误用另一套加密材料。
|
||||
func CheckKeyLayout(root string) error {
|
||||
legacy, modern := false, false
|
||||
for _, name := range []string{"credential.key", "external-email-encryption.key", "external-email-fingerprint.key"} {
|
||||
for _, name := range BusinessKeyNames() {
|
||||
if _, err := os.Lstat(filepath.Join(root, "data", name)); err == nil {
|
||||
legacy = true
|
||||
} else if !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
if _, err := os.Lstat(groupedKeyPath(root, name)); err == nil {
|
||||
modern = true
|
||||
if _, err := os.Lstat(filepath.Join(root, "ssl", name)); !os.IsNotExist(err) {
|
||||
return errors.New("密钥同时存在于归档目录和旧目录,拒绝自动选择或覆盖")
|
||||
}
|
||||
} 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) {
|
||||
@@ -32,7 +41,7 @@ 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"} {
|
||||
for _, name := range []string{"data/smail-state.db", "ssl/credential.key", "ssl/credentials/credential.key"} {
|
||||
info, err := os.Lstat(filepath.Join(root, filepath.FromSlash(name)))
|
||||
if err == nil && info.Mode().IsRegular() {
|
||||
return true
|
||||
@@ -42,6 +51,12 @@ func DeploymentLayout(root string) bool {
|
||||
}
|
||||
|
||||
func SecureDirectory(root, name string) (string, error) {
|
||||
if !filepath.IsLocal(name) || strings.ContainsAny(name, `/\`) || name == "." {
|
||||
return "", errors.New("受保护目录名称无效")
|
||||
}
|
||||
if err := safeDirectory(root); err != nil {
|
||||
return "", err
|
||||
}
|
||||
directory := filepath.Join(root, name)
|
||||
if err := os.Mkdir(directory, 0700); err != nil && !os.IsExist(err) {
|
||||
return "", err
|
||||
@@ -53,10 +68,7 @@ func SecureDirectory(root, name string) (string, error) {
|
||||
}
|
||||
|
||||
func KeyDirectory(root string) string {
|
||||
if DeploymentLayout(root) {
|
||||
return filepath.Join(root, "ssl")
|
||||
}
|
||||
return filepath.Join(root, "data")
|
||||
return filepath.Dir(KeyPath(root, "credential.key"))
|
||||
}
|
||||
|
||||
func SSLReference(root, value string) string {
|
||||
|
||||
@@ -184,7 +184,7 @@ func CheckReady(root string) error {
|
||||
if config.SetupState != "ready" {
|
||||
return nil
|
||||
}
|
||||
required := []string{"credential.key", "external-email-encryption.key", "external-email-fingerprint.key"}
|
||||
required := BusinessKeyNames()
|
||||
if config.Database.Type == "sqlite" {
|
||||
if !filepath.IsLocal(config.Database.LocalRef) {
|
||||
return errors.New("SQLite 本地引用无效")
|
||||
@@ -194,7 +194,12 @@ func CheckReady(root string) error {
|
||||
for _, name := range required {
|
||||
base := filepath.Join(root, "data")
|
||||
if strings.HasSuffix(name, ".key") {
|
||||
base = KeyDirectory(root)
|
||||
base = filepath.Dir(KeyPath(root, name))
|
||||
if DeploymentLayout(root) {
|
||||
if err := safeDirectory(filepath.Join(root, "ssl")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := safeDirectory(base); err != nil {
|
||||
return err
|
||||
|
||||
Reference in new issue
Block a user