mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-22 14:54:25 +03:00
e77de2e67d
Closes #7314. Squashed commit of the following: commit f8b6ffeec2f0f96c947cf896c75d05efaca77caf Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Oct 29 14:14:41 2024 +0300 all: fix chlog commit9417b7dc51
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 19:41:30 2024 +0300 aghos: imp doc commitb91f0e72a7
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 19:26:15 2024 +0300 all: rm bin commit9008ee93b1
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 18:23:54 2024 +0300 all: revert permcheck commitbcc85d50f5
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 17:48:55 2024 +0300 all: use aghos more commit993e351712
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 16:24:56 2024 +0300 all: fix more bugs commita22b0d265e
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 18:30:52 2024 +0300 all: fix bugs commita2309f812a
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 17:05:08 2024 +0300 all: fix chlog, imp api commit42c3f8e91c
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 16:04:47 2024 +0300 scripts: fix docs commit9e781ff18d
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 16:03:19 2024 +0300 scripts: imp docs commit1dbc784982
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 15:55:16 2024 +0300 all: use new functions, add tests commitdcbabaf4e3
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 13:23:50 2024 +0300 aghos: add stat commit72d7c0f881
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 24 17:10:30 2024 +0300 aghos: add windows functions
86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
// Package permcheck contains code for simplifying permissions checks on files
|
|
// and directories.
|
|
//
|
|
// TODO(a.garipov): Improve the approach on Windows.
|
|
package permcheck
|
|
|
|
import (
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
)
|
|
|
|
// File type constants for logging.
|
|
const (
|
|
typeDir = "directory"
|
|
typeFile = "file"
|
|
)
|
|
|
|
// Check checks the permissions on important files. It logs the results at
|
|
// appropriate levels.
|
|
func Check(workDir, dataDir, statsDir, querylogDir, confFilePath string) {
|
|
checkDir(workDir)
|
|
|
|
checkFile(confFilePath)
|
|
|
|
// TODO(a.garipov): Put all paths in one place and remove this duplication.
|
|
checkDir(dataDir)
|
|
checkDir(filepath.Join(dataDir, "filters"))
|
|
checkFile(filepath.Join(dataDir, "sessions.db"))
|
|
checkFile(filepath.Join(dataDir, "leases.json"))
|
|
|
|
if dataDir != querylogDir {
|
|
checkDir(querylogDir)
|
|
}
|
|
checkFile(filepath.Join(querylogDir, "querylog.json"))
|
|
checkFile(filepath.Join(querylogDir, "querylog.json.1"))
|
|
|
|
if dataDir != statsDir {
|
|
checkDir(statsDir)
|
|
}
|
|
checkFile(filepath.Join(statsDir, "stats.db"))
|
|
}
|
|
|
|
// checkDir checks the permissions of a single directory. The results are
|
|
// logged at the appropriate level.
|
|
func checkDir(dirPath string) {
|
|
checkPath(dirPath, typeDir, aghos.DefaultPermDir)
|
|
}
|
|
|
|
// checkFile checks the permissions of a single file. The results are logged at
|
|
// the appropriate level.
|
|
func checkFile(filePath string) {
|
|
checkPath(filePath, typeFile, aghos.DefaultPermFile)
|
|
}
|
|
|
|
// checkPath checks the permissions of a single filesystem entity. The results
|
|
// are logged at the appropriate level.
|
|
func checkPath(entPath, fileType string, want fs.FileMode) {
|
|
s, err := aghos.Stat(entPath)
|
|
if err != nil {
|
|
logFunc := log.Error
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
logFunc = log.Debug
|
|
}
|
|
|
|
logFunc("permcheck: checking %s %q: %s", fileType, entPath, err)
|
|
|
|
return
|
|
}
|
|
|
|
// TODO(a.garipov): Add a more fine-grained check and result reporting.
|
|
perm := s.Mode().Perm()
|
|
if perm != want {
|
|
log.Info(
|
|
"permcheck: SECURITY WARNING: %s %q has unexpected permissions %#o; want %#o",
|
|
fileType,
|
|
entPath,
|
|
perm,
|
|
want,
|
|
)
|
|
}
|
|
}
|