@@ -50,6 +50,7 @@ import (
50
50
"github.com/evilsocket/opensnitch/daemon/rule"
51
51
"github.com/evilsocket/opensnitch/daemon/statistics"
52
52
"github.com/evilsocket/opensnitch/daemon/ui"
53
+ "github.com/evilsocket/opensnitch/daemon/ui/config"
53
54
"github.com/evilsocket/opensnitch/daemon/ui/protocol"
54
55
)
55
56
60
61
logFile = ""
61
62
logUTC = true
62
63
logMicro = false
63
- rulesPath = "rules"
64
+ rulesPath = "/etc/opensnitchd/rules/"
65
+ configFile = "/etc/opensnitchd/default-config.json"
64
66
noLiveReload = false
65
67
queueNum = 0
66
68
repeatQueueNum int //will be set later to queueNum + 1
@@ -102,6 +104,7 @@ func init() {
102
104
flag .IntVar (& workers , "workers" , workers , "Number of concurrent workers." )
103
105
flag .BoolVar (& noLiveReload , "no-live-reload" , debug , "Disable rules live reloading." )
104
106
107
+ flag .StringVar (& configFile , "config-file" , configFile , "Path to the daemon configuration file." )
105
108
flag .StringVar (& logFile , "log-file" , logFile , "Write logs to this file instead of the standard output." )
106
109
flag .BoolVar (& logUTC , "log-utc" , logUTC , "Write logs output with UTC timezone (enabled by default)." )
107
110
flag .BoolVar (& logMicro , "log-micro" , logMicro , "Write logs output with microsecond timestamp (disabled by default)." )
@@ -114,6 +117,27 @@ func init() {
114
117
flag .StringVar (& memProfile , "mem-profile" , memProfile , "Write memory profile to this file." )
115
118
}
116
119
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
+
117
141
func overwriteLogging () bool {
118
142
return debug || warning || important || errorlog || logFile != "" || logMicro
119
143
}
@@ -482,6 +506,17 @@ func main() {
482
506
483
507
log .Important ("Starting %s v%s" , core .Name , core .Version )
484
508
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
+
485
520
rulesPath , err := core .ExpandPath (rulesPath )
486
521
if err != nil {
487
522
log .Fatal ("Error accessing rules path (does it exist?): %s" , err )
@@ -490,14 +525,15 @@ func main() {
490
525
setupSignals ()
491
526
492
527
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 {
494
530
log .Fatal ("%s" , err )
495
531
} else if err = rules .Load (rulesPath ); err != nil {
496
532
log .Fatal ("%s" , err )
497
533
}
498
534
stats = statistics .New (rules )
499
535
loggerMgr = loggers .NewLoggerManager ()
500
- uiClient = ui .NewClient (uiSocket , stats , rules , loggerMgr )
536
+ uiClient = ui .NewClient (uiSocket , configFile , stats , rules , loggerMgr )
501
537
502
538
// prepare the queue
503
539
setupWorkers ()
0 commit comments