2021-05-21 11:55:42 +00:00
|
|
|
//go:build linux
|
2021-06-15 16:42:41 +00:00
|
|
|
// +build linux
|
2021-05-21 11:55:42 +00:00
|
|
|
|
2021-03-16 16:42:15 +00:00
|
|
|
package aghos
|
2020-11-30 16:23:14 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-08 17:42:04 +00:00
|
|
|
"bytes"
|
2020-11-30 16:23:14 +00:00
|
|
|
"os"
|
2021-04-08 17:42:04 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-11-30 16:23:14 +00:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
func canBindPrivilegedPorts() (can bool, err error) {
|
|
|
|
cnbs, err := unix.PrctlRetInt(unix.PR_CAP_AMBIENT, unix.PR_CAP_AMBIENT_IS_SET, unix.CAP_NET_BIND_SERVICE, 0, 0)
|
2020-12-04 14:06:19 +00:00
|
|
|
// Don't check the error because it's always nil on Linux.
|
|
|
|
adm, _ := haveAdminRights()
|
|
|
|
|
|
|
|
return cnbs == 1 || adm, err
|
2020-11-30 16:23:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 18:04:13 +00:00
|
|
|
func setRlimit(val uint64) (err error) {
|
2020-11-30 16:23:14 +00:00
|
|
|
var rlim syscall.Rlimit
|
2021-06-03 18:04:13 +00:00
|
|
|
rlim.Max = val
|
|
|
|
rlim.Cur = val
|
|
|
|
|
|
|
|
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim)
|
2020-11-30 16:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func haveAdminRights() (bool, error) {
|
2020-12-04 14:06:19 +00:00
|
|
|
// The error is nil because the platform-independent function signature
|
|
|
|
// requires returning an error.
|
2020-11-30 16:23:14 +00:00
|
|
|
return os.Getuid() == 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendProcessSignal(pid int, sig syscall.Signal) error {
|
|
|
|
return syscall.Kill(pid, sig)
|
|
|
|
}
|
2021-04-08 17:42:04 +00:00
|
|
|
|
|
|
|
func isOpenWrt() (ok bool) {
|
|
|
|
const etcDir = "/etc"
|
|
|
|
|
2021-05-21 11:55:42 +00:00
|
|
|
dirEnts, err := os.ReadDir(etcDir)
|
2021-04-08 17:42:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// fNameSubstr is a part of a name of the desired file.
|
|
|
|
const fNameSubstr = "release"
|
|
|
|
osNameData := []byte("OpenWrt")
|
|
|
|
|
2021-05-21 11:55:42 +00:00
|
|
|
for _, dirEnt := range dirEnts {
|
|
|
|
if dirEnt.IsDir() {
|
2021-04-08 17:42:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-21 11:55:42 +00:00
|
|
|
fn := dirEnt.Name()
|
2021-04-08 17:42:04 +00:00
|
|
|
if !strings.Contains(fn, fNameSubstr) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var body []byte
|
2021-05-21 11:55:42 +00:00
|
|
|
body, err = os.ReadFile(filepath.Join(etcDir, fn))
|
2021-04-08 17:42:04 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Contains(body, osNameData) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|