Skip to content

Commit 6bd1fe8

Browse files
allow to configure rules and config file paths
- Added cli option -config-file to specify an alternate path to the config file. - Allow to configure rules path from the configuration file (cli option takes precedence). - Default options are now /etc/opensnitchd/rules and /etc/opensnitchd/default-config.json. Previously the default rules directory was "rules" (relative path). Closes #449 (cherry picked from commit 211c864)
1 parent 1608ab7 commit 6bd1fe8

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

daemon/main.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"github.com/evilsocket/opensnitch/daemon/rule"
5151
"github.com/evilsocket/opensnitch/daemon/statistics"
5252
"github.com/evilsocket/opensnitch/daemon/ui"
53+
"github.com/evilsocket/opensnitch/daemon/ui/config"
5354
"github.com/evilsocket/opensnitch/daemon/ui/protocol"
5455
)
5556

@@ -60,7 +61,8 @@ var (
6061
logFile = ""
6162
logUTC = true
6263
logMicro = false
63-
rulesPath = "rules"
64+
rulesPath = "/etc/opensnitchd/rules/"
65+
configFile = "/etc/opensnitchd/default-config.json"
6466
noLiveReload = false
6567
queueNum = 0
6668
repeatQueueNum int //will be set later to queueNum + 1
@@ -102,6 +104,7 @@ func init() {
102104
flag.IntVar(&workers, "workers", workers, "Number of concurrent workers.")
103105
flag.BoolVar(&noLiveReload, "no-live-reload", debug, "Disable rules live reloading.")
104106

107+
flag.StringVar(&configFile, "config-file", configFile, "Path to the daemon configuration file.")
105108
flag.StringVar(&logFile, "log-file", logFile, "Write logs to this file instead of the standard output.")
106109
flag.BoolVar(&logUTC, "log-utc", logUTC, "Write logs output with UTC timezone (enabled by default).")
107110
flag.BoolVar(&logMicro, "log-micro", logMicro, "Write logs output with microsecond timestamp (disabled by default).")
@@ -114,6 +117,27 @@ func init() {
114117
flag.StringVar(&memProfile, "mem-profile", memProfile, "Write memory profile to this file.")
115118
}
116119

120+
// Load configuration file from disk, by default from /etc/opensnitchd/default-config.json,
121+
// or from the path specified by configFile.
122+
// This configuration will be loaded again by uiClient(), in order to monitor it for changes.
123+
func loadDiskConfiguration() (*config.Config, error) {
124+
if configFile == "" {
125+
return nil, fmt.Errorf("Configuration file cannot be empty")
126+
}
127+
128+
raw, err := config.Load(configFile)
129+
if err != nil || len(raw) == 0 {
130+
return nil, fmt.Errorf("Error loading configuration %s: %s", configFile, err)
131+
}
132+
clientConfig, err := config.Parse(raw)
133+
if err != nil {
134+
return nil, fmt.Errorf("Error parsing configuration %s: %s", configFile, err)
135+
}
136+
137+
log.Info("Loading configuration file %s ...", configFile)
138+
return &clientConfig, nil
139+
}
140+
117141
func overwriteLogging() bool {
118142
return debug || warning || important || errorlog || logFile != "" || logMicro
119143
}
@@ -482,6 +506,17 @@ func main() {
482506

483507
log.Important("Starting %s v%s", core.Name, core.Version)
484508

509+
cfg, err := loadDiskConfiguration()
510+
if err != nil {
511+
log.Fatal("%s", err)
512+
}
513+
if err == nil && cfg.Rules.Path != "" {
514+
rulesPath = cfg.Rules.Path
515+
}
516+
if rulesPath == "" {
517+
log.Fatal("rules path cannot be empty")
518+
}
519+
485520
rulesPath, err := core.ExpandPath(rulesPath)
486521
if err != nil {
487522
log.Fatal("Error accessing rules path (does it exist?): %s", err)
@@ -490,14 +525,15 @@ func main() {
490525
setupSignals()
491526

492527
log.Info("Loading rules from %s ...", rulesPath)
493-
if rules, err = rule.NewLoader(!noLiveReload); err != nil {
528+
rules, err = rule.NewLoader(!noLiveReload)
529+
if err != nil {
494530
log.Fatal("%s", err)
495531
} else if err = rules.Load(rulesPath); err != nil {
496532
log.Fatal("%s", err)
497533
}
498534
stats = statistics.New(rules)
499535
loggerMgr = loggers.NewLoggerManager()
500-
uiClient = ui.NewClient(uiSocket, stats, rules, loggerMgr)
536+
uiClient = ui.NewClient(uiSocket, configFile, stats, rules, loggerMgr)
501537

502538
// prepare the queue
503539
setupWorkers()

daemon/rule/loader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ func (l *Loader) GetAll() map[string]*Rule {
6464
// Load loads rules files from disk.
6565
func (l *Loader) Load(path string) error {
6666
if core.Exists(path) == false {
67-
return fmt.Errorf("Path '%s' does not exist\nCreate it in if you want to save rules to disk", path)
67+
return fmt.Errorf("Path '%s' does not exist\nCreate it if you want to save rules to disk", path)
6868
}
6969
path, err := core.ExpandPath(path)
7070
if err != nil {
71-
return fmt.Errorf("Error accessing rules path: %s.\nCreate it in if you want to save rules to disk", err)
71+
return fmt.Errorf("Error accessing rules path: %s.\nCreate it if you want to save rules to disk", err)
7272
}
7373

7474
expr := filepath.Join(path, "*.json")

daemon/ui/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ type Client struct {
5959
}
6060

6161
// NewClient creates and configures a new client.
62-
func NewClient(socketPath string, stats *statistics.Statistics, rules *rule.Loader, loggers *loggers.LoggerManager) *Client {
62+
func NewClient(socketPath, localConfigFile string, stats *statistics.Statistics, rules *rule.Loader, loggers *loggers.LoggerManager) *Client {
63+
if localConfigFile != "" {
64+
configFile = localConfigFile
65+
}
6366
c := &Client{
6467
stats: stats,
6568
rules: rules,

daemon/ui/config/config.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,25 @@ type serverConfig struct {
4444
Loggers []loggers.LoggerConfig `json:"Loggers"`
4545
}
4646

47+
type rulesOptions struct {
48+
Path string `json:"Path"`
49+
EnableChecksums bool `json:"EnableChecksums"`
50+
}
51+
4752
// Config holds the values loaded from configFile
4853
type Config struct {
4954
sync.RWMutex
5055
Server serverConfig `json:"Server"`
56+
Stats statistics.StatsConfig `json:"Stats"`
57+
Rules rulesOptions `json:"Rules"`
5158
DefaultAction string `json:"DefaultAction"`
5259
DefaultDuration string `json:"DefaultDuration"`
53-
InterceptUnknown bool `json:"InterceptUnknown"`
5460
ProcMonitorMethod string `json:"ProcMonitorMethod"`
61+
Firewall string `json:"Firewall"`
5562
LogLevel *uint32 `json:"LogLevel"`
63+
InterceptUnknown bool `json:"InterceptUnknown"`
5664
LogUTC bool `json:"LogUTC"`
5765
LogMicro bool `json:"LogMicro"`
58-
Firewall string `json:"Firewall"`
59-
Stats statistics.StatsConfig `json:"Stats"`
6066
}
6167

6268
// Parse determines if the given configuration is ok.

0 commit comments

Comments
 (0)