-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
152 lines (130 loc) · 3.33 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/subgraph/usblockout/config"
mlog "github.com/subgraph/usblockout/logging"
"github.com/godbus/dbus"
"github.com/op/go-logging"
)
var log = logging.MustGetLogger(config.AppName)
const (
dbusObjectScreensaverActiveChange = "type='signal',path='/org/gnome/ScreenSaver',interface='org.gnome.ScreenSaver',member='ActiveChanged'"
)
var dbusObjectSetLocked = config.BusName + ".SetLocked"
var dbusObjectIsRunning = config.BusName + ".IsRunning"
type usbLockout struct {
dconnSys *dbus.Conn
dbusSys dbus.BusObject
dconnSess *dbus.Conn
logBackend logging.LeveledBackend
}
func (ul *usbLockout) lock() error {
if res := ul.dbusSys.Call(dbusObjectSetLocked, 0, true); res.Err != nil {
return res.Err
}
return nil
}
func (ul *usbLockout) unlock() error {
if res := ul.dbusSys.Call(dbusObjectSetLocked, 0, false); res.Err != nil {
return res.Err
}
return nil
}
func (ul *usbLockout) processSignals(c <-chan os.Signal) {
for {
sig := <-c
log.Debugf("Recieved signal (%v)\n", sig)
if err := ul.lock(); err != nil {
log.Fatal(err)
os.Exit(1)
}
os.Exit(0)
}
}
func (ul *usbLockout) runDaemon() {
for {
c := make(chan *dbus.Signal, 1)
ul.dconnSess.Signal(c)
for v := range c {
state := v.Body[0].(bool)
switch state {
case true:
log.Debug("Screen locked")
if err := ul.lock(); err != nil {
log.Fatal(err)
os.Exit(1)
}
case false:
log.Debug("Screen unlocked")
if err := ul.unlock(); err != nil {
log.Fatal(err)
os.Exit(1)
}
break
default:
log.Errorf("Unknown signal received: %+v\n", v.Body[0])
}
break
}
ul.dconnSess.RemoveSignal(c)
}
}
var flagdebug, enable, disable bool
func init() {
flag.BoolVar(&enable, "enable", false, "manually enable the usb deny feature")
flag.BoolVar(&disable, "disable", false, "manually disable the usb deny feature")
flag.BoolVar(&flagdebug, "debug", false, "enable debug logging")
flag.Parse()
}
func main() {
var err error
ul := &usbLockout{}
ul.logBackend = mlog.SetupLoggerBackend(logging.INFO, config.AppName)
log.SetBackend(ul.logBackend)
if flagdebug {
ul.logBackend.SetLevel(logging.DEBUG, config.AppName)
log.Debug("Debug logging enabled")
}
ul.dconnSys, err = dbus.SystemBus()
if err != nil {
log.Errorf("Failed to connect to system bus: %+v\n", err)
os.Exit(1)
}
ul.dconnSess, err = dbus.SessionBus()
if err != nil {
log.Errorf("Failed to connect to session bus: %+v\n", err)
os.Exit(1)
}
ul.dconnSess.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, dbusObjectScreensaverActiveChange)
ul.dbusSys = ul.dconnSys.Object(config.BusName, config.ObjectPath)
if res := ul.dbusSys.Call(dbusObjectIsRunning, 0); res.Err != nil || res.Body[0] == false {
log.Error("USB Lockout daemon is not running or unavailable")
os.Exit(1)
}
switch {
case enable == true:
fmt.Println("Enabling USB Deny")
ul.lock()
os.Exit(0)
break
case disable == true:
fmt.Println("Disabling USB Deny")
ul.unlock()
os.Exit(0)
break
default:
log.Notice("USB Lockout client enabled")
if res := ul.dbusSys.Call(dbusObjectSetLocked, 0, false); res.Err != nil {
log.Fatal(res.Err)
os.Exit(1)
}
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGTERM, os.Interrupt)
go ul.processSignals(sigs)
ul.runDaemon()
}
}