feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,187 @@
|
||||
package isolation
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var pureFTPTLSLine = regexp.MustCompile(`(?m)^[ \t]*TLS[ \t]+[0-9]+[ \t]*\r?$`)
|
||||
var pureFTPCertFileLine = regexp.MustCompile(`(?m)^[ \t]*CertFile[ \t]+[^\r\n]+\r?$`)
|
||||
|
||||
func (a Agent) configureFTPTLS(w http.ResponseWriter, r *http.Request) {
|
||||
var input struct {
|
||||
Domain string `json:"domain"`
|
||||
}
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&input); err != nil || decoder.Decode(&struct{}{}) != io.EOF {
|
||||
http.Error(w, "invalid request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
input.Domain = strings.ToLower(strings.TrimSpace(input.Domain))
|
||||
if !webDomainPattern.MatchString(input.Domain) {
|
||||
http.Error(w, "invalid FTP domain", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
policy, err := a.applyFTPTLSPolicy(r.Context(), input.Domain)
|
||||
if err != nil {
|
||||
http.Error(w, "cannot configure FTP TLS", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"policy": policy})
|
||||
}
|
||||
|
||||
func (a Agent) applyFTPTLSPolicy(ctx context.Context, domain string) (string, error) {
|
||||
certRoot := a.CertificateRoot
|
||||
if certRoot == "" {
|
||||
certRoot = "/www/server/panel/vhost/cert"
|
||||
}
|
||||
configPath := a.PureFTPConfig
|
||||
if configPath == "" {
|
||||
configPath = "/www/server/pure-ftpd/etc/pure-ftpd.conf"
|
||||
}
|
||||
pemPath := a.PureFTPPem
|
||||
if pemPath == "" {
|
||||
pemPath = "/www/server/pure-ftpd/etc/pure-ftpd.pem"
|
||||
}
|
||||
certPath := filepath.Join(certRoot, domain, "fullchain.pem")
|
||||
keyPath := filepath.Join(certRoot, domain, "privkey.pem")
|
||||
certInfo, certErr := os.Lstat(certPath)
|
||||
keyInfo, keyErr := os.Lstat(keyPath)
|
||||
hasCert := certErr == nil && keyErr == nil
|
||||
if (certErr == nil) != (keyErr == nil) || (certErr != nil && !errors.Is(certErr, os.ErrNotExist)) || (keyErr != nil && !errors.Is(keyErr, os.ErrNotExist)) {
|
||||
return "", errors.New("incomplete FTP certificate")
|
||||
}
|
||||
if hasCert && (!certInfo.Mode().IsRegular() || certInfo.Mode()&os.ModeSymlink != 0 || !keyInfo.Mode().IsRegular() || keyInfo.Mode()&os.ModeSymlink != 0) {
|
||||
return "", errors.New("unsafe FTP certificate")
|
||||
}
|
||||
configInfo, err := os.Lstat(configPath)
|
||||
if err != nil || !configInfo.Mode().IsRegular() || configInfo.Mode()&os.ModeSymlink != 0 {
|
||||
return "", errors.New("unsafe Pure-FTPd configuration")
|
||||
}
|
||||
config, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
wantedTLS := "TLS 0"
|
||||
policy := "plain_allowed_no_certificate"
|
||||
changed := false
|
||||
if hasCert {
|
||||
pair, pairErr := tls.LoadX509KeyPair(certPath, keyPath)
|
||||
if pairErr != nil || len(pair.Certificate) == 0 {
|
||||
return "", errors.New("invalid FTP certificate pair")
|
||||
}
|
||||
leaf, parseErr := x509.ParseCertificate(pair.Certificate[0])
|
||||
if parseErr != nil || time.Now().Before(leaf.NotBefore) || !time.Now().Before(leaf.NotAfter) {
|
||||
return "", errors.New("FTP certificate is not currently valid")
|
||||
}
|
||||
if err = leaf.VerifyHostname(domain); err != nil {
|
||||
return "", errors.New("FTP certificate does not match the configured domain")
|
||||
}
|
||||
certPEM, readCertErr := os.ReadFile(certPath)
|
||||
keyPEM, readKeyErr := os.ReadFile(keyPath)
|
||||
if readCertErr != nil || readKeyErr != nil {
|
||||
return "", errors.Join(readCertErr, readKeyErr)
|
||||
}
|
||||
combined := append(bytes.TrimSpace(certPEM), '\n')
|
||||
combined = append(combined, bytes.TrimSpace(keyPEM)...)
|
||||
combined = append(combined, '\n')
|
||||
existing, _ := os.ReadFile(pemPath)
|
||||
if !bytes.Equal(existing, combined) {
|
||||
if err = writeSecureFile(pemPath, combined, 0600); err != nil {
|
||||
return "", err
|
||||
}
|
||||
changed = true
|
||||
}
|
||||
wantedTLS = "TLS 2"
|
||||
policy = "explicit_tls_required"
|
||||
}
|
||||
directiveCount := 0
|
||||
updated := pureFTPTLSLine.ReplaceAllFunc(config, func([]byte) []byte {
|
||||
directiveCount++
|
||||
if directiveCount == 1 {
|
||||
return []byte(wantedTLS)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if directiveCount == 0 {
|
||||
updated = append(bytes.TrimRight(config, "\r\n"), []byte("\n"+wantedTLS+"\n")...)
|
||||
}
|
||||
if hasCert {
|
||||
wantedCertFile := []byte("CertFile " + pemPath)
|
||||
certFileCount := 0
|
||||
updated = pureFTPCertFileLine.ReplaceAllFunc(updated, func([]byte) []byte {
|
||||
certFileCount++
|
||||
if certFileCount == 1 {
|
||||
return wantedCertFile
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if certFileCount == 0 {
|
||||
updated = append(bytes.TrimRight(updated, "\r\n"), '\n')
|
||||
updated = append(updated, wantedCertFile...)
|
||||
updated = append(updated, '\n')
|
||||
}
|
||||
}
|
||||
if !bytes.Equal(updated, config) {
|
||||
if err = writeSecureFile(configPath, updated, configInfo.Mode().Perm()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
changed = true
|
||||
}
|
||||
if changed {
|
||||
// Pure-FTPd turns the configuration into daemon command-line flags at
|
||||
// process start. A SysV-generated unit may report a successful reload
|
||||
// without replacing those flags, so TLS policy changes require a full
|
||||
// restart rather than reload-or-restart.
|
||||
if err = a.restartPureFTP(ctx, configPath); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return policy, nil
|
||||
}
|
||||
|
||||
func (a Agent) restartPureFTP(ctx context.Context, configPath string) error {
|
||||
if err := a.command(ctx, "systemctl", "stop", "pure-ftpd.service"); err != nil {
|
||||
return err
|
||||
}
|
||||
binaryPath := filepath.Join(filepath.Dir(filepath.Dir(configPath)), "sbin", "pure-ftpd")
|
||||
if err := terminatePureFTPDaemons(ctx, binaryPath); err != nil {
|
||||
return err
|
||||
}
|
||||
return a.command(ctx, "systemctl", "start", "pure-ftpd.service")
|
||||
}
|
||||
|
||||
func writeSecureFile(path string, data []byte, mode os.FileMode) error {
|
||||
temporary, err := os.CreateTemp(filepath.Dir(path), ".sea-vhost-*.tmp")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
temporaryPath := temporary.Name()
|
||||
defer os.Remove(temporaryPath)
|
||||
if err = temporary.Chmod(mode); err == nil {
|
||||
_, err = temporary.Write(data)
|
||||
}
|
||||
if err == nil {
|
||||
err = temporary.Sync()
|
||||
}
|
||||
if closeErr := temporary.Close(); err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(temporaryPath, path)
|
||||
}
|
||||
Reference in new issue
Block a user