You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fw-daemon/sgfw/config.go

103 lines
2.0 KiB

package sgfw
import (
"bufio"
"io/ioutil"
"os"
"path"
"github.com/naoina/toml"
"github.com/op/go-logging"
)
const (
configDefaultPath string = "/etc/sgfw/sgfw.conf"
)
type FirewallConfigs struct {
LogLevel string
LoggingLevel logging.Level `toml:"-"`
LogRedact bool
PromptExpanded bool
PromptExpert bool
DefaultAction string
8 years ago
DefaultActionID FilterScope `toml:"-"`
}
var FirewallConfig FirewallConfigs
func _readConfig(file string) []byte {
envFile := os.Getenv("SGFW_CONF")
7 years ago
if envFile != "" {
file = envFile
}
f, err := os.Open(file)
if err != nil {
log.Warning(err.Error())
return []byte{}
}
defer f.Close()
buf, err := ioutil.ReadAll(f)
if err != nil {
log.Warning(err.Error())
return []byte{}
}
return buf
}
func readConfig() {
buf := _readConfig(configDefaultPath)
FirewallConfig = FirewallConfigs{
LogLevel: "NOTICE",
LoggingLevel: logging.NOTICE,
LogRedact: false,
PromptExpanded: false,
PromptExpert: false,
DefaultAction: "SESSION",
8 years ago
DefaultActionID: 1,
}
if len(buf) > 0 {
if err := toml.Unmarshal(buf, &FirewallConfig); err != nil {
log.Error(err.Error())
os.Exit(1)
}
}
FirewallConfig.LoggingLevel, _ = logging.LogLevel(FirewallConfig.LogLevel)
8 years ago
FirewallConfig.DefaultActionID = GetFilterScopeValue(FirewallConfig.DefaultAction)
}
func writeConfig() {
FirewallConfig.LogLevel = FirewallConfig.LoggingLevel.String()
8 years ago
FirewallConfig.DefaultAction = GetFilterScopeString(FirewallConfig.DefaultActionID)
if _, err := os.Stat(path.Dir(configDefaultPath)); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(path.Dir(configDefaultPath), 0755); err != nil {
log.Error(err.Error())
//os.Exit(1)
return
}
}
f, err := os.Create(configDefaultPath)
if err != nil {
log.Error(err.Error())
//os.Exit(1)
return
}
defer f.Close()
w := bufio.NewWriter(f)
cw := toml.NewEncoder(w)
if err := cw.Encode(FirewallConfig); err != nil {
log.Error(err.Error())
//os.Exit(1)
return
}
w.Flush()
}