mirror of https://github.com/subgraph/fw-daemon
parent
4b632fb6f2
commit
5d4b38c5b4
@ -0,0 +1,112 @@
|
||||
package sgfw
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
STR_REDACTED = "[redacted]"
|
||||
STR_UNKNOWN = "[uknown]"
|
||||
)
|
||||
|
||||
type RuleAction uint16
|
||||
|
||||
const (
|
||||
RULE_ACTION_DENY RuleAction = iota
|
||||
RULE_ACTION_ALLOW
|
||||
)
|
||||
|
||||
var RuleActionString = map[RuleAction]string {
|
||||
RULE_ACTION_DENY: "DENY",
|
||||
RULE_ACTION_ALLOW: "ALLOW",
|
||||
}
|
||||
|
||||
var RuleActionValue = map[string]RuleAction {
|
||||
"DENY": RULE_ACTION_DENY,
|
||||
"ALLOW": RULE_ACTION_ALLOW,
|
||||
}
|
||||
|
||||
type RuleMode uint16
|
||||
|
||||
const (
|
||||
RULE_MODE_SESSION RuleMode = iota
|
||||
RULE_MODE_PERMANENT
|
||||
RULE_MODE_SYSTEM
|
||||
)
|
||||
|
||||
var RuleModeString = map[RuleMode]string {
|
||||
RULE_MODE_SESSION: "SESSION",
|
||||
RULE_MODE_PERMANENT: "PERMANENT",
|
||||
RULE_MODE_SYSTEM: "SYSTEM",
|
||||
}
|
||||
|
||||
var RuleModeValue = map[string]RuleMode {
|
||||
"SESSION": RULE_MODE_SESSION,
|
||||
"PERMANENT": RULE_MODE_PERMANENT,
|
||||
"SYSTEM": RULE_MODE_SYSTEM,
|
||||
}
|
||||
|
||||
|
||||
type FilterScope uint16
|
||||
const (
|
||||
APPLY_ONCE FilterScope = iota
|
||||
APPLY_SESSION
|
||||
APPLY_FOREVER
|
||||
)
|
||||
|
||||
var FilterScopeString = map[FilterScope]string {
|
||||
APPLY_ONCE: "ONCE",
|
||||
APPLY_SESSION: "SESSION",
|
||||
APPLY_FOREVER: "FOREVER",
|
||||
}
|
||||
|
||||
var FilterScopeValue = map[string]FilterScope {
|
||||
"ONCE": APPLY_ONCE,
|
||||
"SESSION": APPLY_SESSION,
|
||||
"FOREVER": APPLY_FOREVER,
|
||||
}
|
||||
|
||||
|
||||
func GetFilterScopeString(scope FilterScope) string {
|
||||
if val, ok := FilterScopeString[scope]; ok {
|
||||
return val
|
||||
}
|
||||
return FilterScopeString[APPLY_SESSION]
|
||||
}
|
||||
|
||||
func GetFilterScopeValue(scope string) FilterScope {
|
||||
scope = strings.ToUpper(scope)
|
||||
if val, ok := FilterScopeValue[scope]; ok {
|
||||
return val
|
||||
}
|
||||
return APPLY_SESSION
|
||||
}
|
||||
|
||||
type FilterResult uint16
|
||||
|
||||
const (
|
||||
FILTER_DENY FilterResult = iota
|
||||
FILTER_ALLOW
|
||||
FILTER_PROMPT
|
||||
)
|
||||
|
||||
var FilterResultString = map[FilterResult]string{
|
||||
FILTER_DENY: "DENY",
|
||||
FILTER_ALLOW: "ALLOW",
|
||||
FILTER_PROMPT: "PROMPT",
|
||||
}
|
||||
|
||||
var FilterResultValue = map[string]FilterResult {
|
||||
"DENY": FILTER_DENY,
|
||||
"ALLOW": FILTER_ALLOW,
|
||||
"PROMPT": FILTER_PROMPT,
|
||||
}
|
||||
|
||||
type DbusRule struct {
|
||||
Id uint32
|
||||
App string
|
||||
Path string
|
||||
Verb uint16
|
||||
Target string
|
||||
Mode uint16
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package sgfw
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/op/go-logging"
|
||||
)
|
||||
|
||||
var LevelToId = map[int32]string{
|
||||
int32(logging.ERROR): "error",
|
||||
int32(logging.WARNING): "warning",
|
||||
int32(logging.NOTICE): "notice",
|
||||
int32(logging.INFO): "info",
|
||||
int32(logging.DEBUG): "debug",
|
||||
}
|
||||
|
||||
var IdToLevel = func() map[string]int32 {
|
||||
m := make(map[string]int32)
|
||||
for k, v := range LevelToId {
|
||||
m[v] = k
|
||||
}
|
||||
return m
|
||||
}()
|
||||
|
||||
var log = logging.MustGetLogger("sgfw")
|
||||
|
||||
var logFormat = logging.MustStringFormatter(
|
||||
"%{level:.4s} %{id:03x} %{message}",
|
||||
)
|
||||
var ttyFormat = logging.MustStringFormatter(
|
||||
"%{color}%{time:15:04:05} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}",
|
||||
)
|
||||
|
||||
const ioctlReadTermios = 0x5401
|
||||
|
||||
func isTerminal(fd int) bool {
|
||||
var termios syscall.Termios
|
||||
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
|
||||
return err == 0
|
||||
}
|
||||
|
||||
func setupLoggerBackend(lvl logging.Level) logging.LeveledBackend {
|
||||
format := logFormat
|
||||
if isTerminal(int(os.Stderr.Fd())) {
|
||||
format = ttyFormat
|
||||
}
|
||||
backend := logging.NewLogBackend(os.Stderr, "", 0)
|
||||
formatter := logging.NewBackendFormatter(backend, format)
|
||||
leveler := logging.AddModuleLevel(formatter)
|
||||
leveler.SetLevel(lvl, "sgfw")
|
||||
return leveler
|
||||
}
|
Loading…
Reference in new issue