173 lines
4.5 KiB
Go
173 lines
4.5 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
// adpanel:fs-toctou - copyDir pins both tree roots by directory fd and opens
|
|
// entries relative to them. The walk enumerates names only; each file open
|
|
// rechecks containment, rejects symlinks, and cannot block on a FIFO.
|
|
//
|
|
// adpanel:backup-copy-budget - one optional byte cap covers the whole tree,
|
|
// checked before each write so one large file cannot bypass it.
|
|
func copyDir(src, dst string, privateDestination bool, budget ...int64) error {
|
|
var budgetWriter *limitedWriter
|
|
if len(budget) > 0 {
|
|
budgetWriter = &limitedWriter{limit: budget[0]}
|
|
}
|
|
|
|
srcResolved, err := filepath.EvalSymlinks(src)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to resolve source path: %w", err)
|
|
}
|
|
|
|
srcRoot, err := safeOpenRoot(srcResolved)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to resolve source path: %w", err)
|
|
}
|
|
defer srcRoot.Close()
|
|
dstRoot, err := safeOpenRoot(dst)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer dstRoot.Close()
|
|
|
|
// adpanel:backup-directory-modes - a private destination directory must
|
|
// remain writable until all its children are copied. The walk is depth
|
|
// first, so only the ancestors of the current entry need pending modes.
|
|
type directoryMode struct {
|
|
path string
|
|
mode os.FileMode
|
|
}
|
|
var pendingModes []directoryMode
|
|
finishDirectories := func(nextPath string) error {
|
|
for len(pendingModes) > 0 {
|
|
last := len(pendingModes) - 1
|
|
pending := pendingModes[last]
|
|
if strings.HasPrefix(nextPath, pending.path+string(filepath.Separator)) {
|
|
break
|
|
}
|
|
directory, err := safeOpenDirAt(dstRoot, pending.path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = directory.Chmod(pending.mode)
|
|
directory.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pendingModes = pendingModes[:last]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
err = filepath.Walk(srcResolved, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
return nil
|
|
}
|
|
|
|
linfo, err := os.Lstat(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if linfo.Mode()&os.ModeSymlink != 0 {
|
|
return nil
|
|
}
|
|
|
|
relPath, err := filepath.Rel(srcResolved, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, verr := safeRelUnderRoot(relPath); verr != nil {
|
|
return fmt.Errorf("path traversal detected: %s", relPath)
|
|
}
|
|
if relPath == "." {
|
|
return nil
|
|
}
|
|
if err := finishDirectories(relPath); err != nil {
|
|
return err
|
|
}
|
|
|
|
if linfo.IsDir() {
|
|
mode := linfo.Mode()
|
|
if privateDestination {
|
|
mode |= 0o200
|
|
}
|
|
if err := ensureDirUnderRoot(dstRoot, relPath, mode); err != nil {
|
|
if errors.Is(err, errSafeScopeEscape) {
|
|
return fmt.Errorf("symlink escape blocked: %s", relPath)
|
|
}
|
|
return err
|
|
}
|
|
if privateDestination {
|
|
pendingModes = append(pendingModes, directoryMode{path: relPath, mode: linfo.Mode()})
|
|
}
|
|
return nil
|
|
}
|
|
if !linfo.Mode().IsRegular() {
|
|
return nil
|
|
}
|
|
|
|
srcFile, _, err := safeOpenRegularAt(srcRoot, relPath, os.O_RDONLY|openNoFollow, 0)
|
|
if err != nil {
|
|
if errors.Is(err, syscall.ELOOP) || errors.Is(err, errSafeNotRegular) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
defer srcFile.Close()
|
|
|
|
dstFile, err := safeOpenWriteAt(dstRoot, relPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|openNoFollow, linfo.Mode())
|
|
if err != nil {
|
|
if errors.Is(err, syscall.ELOOP) || errors.Is(err, errSafeNotRegular) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
defer dstFile.Close()
|
|
|
|
var destination io.Writer = dstFile
|
|
if budgetWriter != nil {
|
|
budgetWriter.w = dstFile
|
|
destination = budgetWriter
|
|
}
|
|
if _, err := io.Copy(destination, srcFile); err != nil {
|
|
if budgetWriter != nil && budgetWriter.exceeded {
|
|
return errCopyBudgetExceeded
|
|
}
|
|
return err
|
|
}
|
|
return dstFile.Chmod(linfo.Mode())
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return finishDirectories("")
|
|
}
|
|
|
|
// adpanel:backup-delete-integrity - detach under the server op lock before removing
|
|
// files so a crash cannot leave a partial backup available to restore. Trash stays
|
|
// charged to this server and the stale meta-less directory sweep reaps it.
|
|
func (a *Agent) deleteBackupTree(backupDir string) error {
|
|
backupsDir := filepath.Dir(backupDir)
|
|
detached, exists, err := a.detachPathForAsyncDelete(backupsDir, backupDir, "backup", filepath.Base(backupDir))
|
|
if err != nil || !exists {
|
|
return err
|
|
}
|
|
if err := removeAllWritable(detached); err != nil {
|
|
a.logf("Detached backup %s, but trash cleanup failed: %v", backupDir, err)
|
|
} else {
|
|
_ = os.Remove(deletionTrashDir(backupsDir))
|
|
}
|
|
return nil
|
|
}
|