mirror of
https://github.com/warmbly/warmbly.git
synced 2026-10-27 08:02:27 +00:00
37 lines
1.0 KiB
Go
37 lines
1.0 KiB
Go
//go:build linux || darwin || freebsd || openbsd || netbsd || dragonfly
|
|
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"syscall"
|
|
)
|
|
|
|
func privateRemoteFile(info os.FileInfo, directory bool) bool {
|
|
stat, ok := info.Sys().(*syscall.Stat_t)
|
|
if !ok || int(stat.Uid) != os.Geteuid() || info.Mode()&(os.ModeSymlink|os.ModeSetuid|os.ModeSetgid|os.ModeSticky) != 0 {
|
|
return false
|
|
}
|
|
if directory {
|
|
return info.IsDir() && info.Mode().Perm() == 0700
|
|
}
|
|
return info.Mode().IsRegular() && info.Mode().Perm() == 0600 && stat.Nlink == 1
|
|
}
|
|
|
|
func trustedRemoteAncestor(info os.FileInfo) bool {
|
|
stat, ok := info.Sys().(*syscall.Stat_t)
|
|
return ok && info.IsDir() && (stat.Uid == 0 || stat.Uid == uint32(os.Geteuid())) &&
|
|
(info.Mode().Perm()&0022 == 0 || info.Mode()&os.ModeSticky != 0)
|
|
}
|
|
|
|
func remoteNoFollow() int { return syscall.O_NOFOLLOW }
|
|
|
|
func lockRemoteFile(f *os.File) (bool, error) {
|
|
err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
|
|
if errors.Is(err, syscall.EWOULDBLOCK) {
|
|
return false, nil
|
|
}
|
|
return err == nil, err
|
|
}
|