forked from slackhq/go-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauditconfig.go
100 lines (82 loc) · 2.01 KB
/
auditconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// +build linux
package audit
import (
"fmt"
"strings"
)
// AuditConfig is the audit configuration
type AuditConfig struct {
nl *NetlinkClient
m *AuditMarshaller
w AuditWriter
f []AuditFilter
SocketBufferSize int
EventMin uint16
EventMax uint16
MaxOutOfOrder int
stop chan bool
TrackMessages bool
LogOutOfOrder bool
}
func NewAuditConfig(w AuditWriter, f []AuditFilter) (*AuditConfig, error) {
a := AuditConfig{
w: w,
f: f,
SocketBufferSize: 16384,
EventMin: 1300,
EventMax: 1399,
MaxOutOfOrder: 500,
TrackMessages: true,
LogOutOfOrder: false,
stop: make(chan bool),
}
var err error
a.nl, err = NewNetlinkClient(a.SocketBufferSize)
if err != nil {
return nil, fmt.Errorf("Unable to initialize netlink %s", err)
}
a.m = NewAuditMarshaller(a.w, a.EventMin, a.EventMax, a.TrackMessages, a.LogOutOfOrder, a.MaxOutOfOrder, a.f)
return &a, nil
}
// Start starts the audit process
func (a *AuditConfig) Start() {
for {
select {
case <-a.stop:
return
default:
msg, err := a.nl.Receive()
if err != nil {
continue
}
if msg == nil {
continue
}
a.m.Consume(msg)
}
}
}
// Stop stops the audit process
func (a *AuditConfig) Stop() {
a.stop <- true
}
// UpdateRules updates the rules in the system. The rules are provided
// as a list of audictl command strings
func (a *AuditConfig) UpdateRules(rules []string) error {
if err := lExec("auditctl", "-D"); err != nil {
return fmt.Errorf("Failed to flush existing audit rules. Error: %s", err)
}
if err := lExec("auditctl", "-e", "1"); err != nil {
return fmt.Errorf("Failed to initialize. Error: %s", err)
}
for _, v := range rules {
// Skip rules with no content
if v == "" {
continue
}
if err := lExec("auditctl", strings.Fields(v)...); err != nil {
return fmt.Errorf("Failed to add rule %+v. Error: %s", v, err)
}
}
return nil
}