-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.go
154 lines (127 loc) · 3.85 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
153
154
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"path"
"path/filepath"
"strings"
"sync"
"syscall"
"github.com/proglottis/gpgme"
log "github.com/sirupsen/logrus"
"github.com/maximbaz/yubikey-touch-detector/detector"
"github.com/maximbaz/yubikey-touch-detector/notifier"
)
const appVersion = "1.12.2"
func main() {
truthyValues := map[string]bool{"true": true, "yes": true, "1": true}
envVerbose := truthyValues[strings.ToLower(os.Getenv("YUBIKEY_TOUCH_DETECTOR_VERBOSE"))]
envLibnotify := truthyValues[strings.ToLower(os.Getenv("YUBIKEY_TOUCH_DETECTOR_LIBNOTIFY"))]
envStdout := truthyValues[strings.ToLower(os.Getenv("YUBIKEY_TOUCH_DETECTOR_STDOUT"))]
envNosocket := truthyValues[strings.ToLower(os.Getenv("YUBIKEY_TOUCH_DETECTOR_NOSOCKET"))]
var version bool
var verbose bool
var libnotify bool
var stdout bool
var nosocket bool
flag.BoolVar(&version, "version", false, "print version and exit")
flag.BoolVar(&verbose, "v", envVerbose, "enable debug logging")
flag.BoolVar(&libnotify, "libnotify", envLibnotify, "show desktop notifications using libnotify")
flag.BoolVar(&stdout, "stdout", envStdout, "print notifications to stdout")
flag.BoolVar(&nosocket, "no-socket", envNosocket, "disable unix socket notifier")
flag.Parse()
if version {
fmt.Println("YubiKey touch detector version:", appVersion)
os.Exit(0)
}
if verbose {
log.SetLevel(log.DebugLevel)
}
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
log.Debug("Starting YubiKey touch detector")
exits := &sync.Map{}
go setupExitSignalWatch(exits)
notifiers := &sync.Map{}
if verbose {
go notifier.SetupDebugNotifier(notifiers)
}
if !nosocket {
go notifier.SetupUnixSocketNotifier(notifiers, exits)
}
if libnotify {
go notifier.SetupLibnotifyNotifier(notifiers)
}
if stdout {
go notifier.SetupStdoutNotifier(notifiers)
}
go detector.WatchU2F(notifiers)
go detector.WatchHMAC(notifiers)
initGPGBasedDetectors(notifiers, exits)
wait := make(chan bool)
<-wait
}
func initGPGBasedDetectors(notifiers, exits *sync.Map) {
ctx, err := gpgme.New()
if err != nil {
log.Debugf("Cannot initialize GPG context: %v. Disabling GPG and SSH watchers.", err)
return
}
if ctx.SetProtocol(gpgme.ProtocolAssuan) != nil {
log.Debugf("Cannot initialize Assuan IPC: %v. Disabling GPG and SSH watchers.", err)
return
}
var gpgPrivateKeysDirPath = path.Join(gpgme.GetDirInfo("homedir"), "private-keys-v1.d")
if _, err := os.Stat(gpgPrivateKeysDirPath); err != nil {
log.Debugf("Directory '%s' does not exist or cannot stat it\n", gpgPrivateKeysDirPath)
return
}
filesToWatch, err := findShadowedPrivateKeys(gpgPrivateKeysDirPath)
if err != nil {
log.Debugf("Error finding shadowed private keys: %v\n", err)
return
}
if len(filesToWatch) == 0 {
log.Debugf("No shadowed private keys found.\n")
return
}
requestGPGCheck := make(chan bool)
go detector.CheckGPGOnRequest(requestGPGCheck, notifiers, ctx)
go detector.WatchGPG(filesToWatch, requestGPGCheck)
go detector.WatchSSH(requestGPGCheck, exits)
}
func findShadowedPrivateKeys(folderPath string) ([]string, error) {
var result []string
err := filepath.WalkDir(folderPath, func(path string, info os.DirEntry, err error) error {
if err != nil || info.IsDir() {
return err
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
if strings.Contains(string(data), "shadowed-private-key") {
result = append(result, path)
}
return nil
})
if err != nil {
return nil, err
}
return result, nil
}
func setupExitSignalWatch(exits *sync.Map) {
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt, syscall.SIGTERM)
<-exitSignal
println()
exits.Range(func(_, v interface{}) bool {
exit := v.(chan bool)
exit <- true // Notify exit watcher
<-exit // Wait for confirmation
return true
})
log.Debug("Stopping YubiKey touch detector")
os.Exit(0)
}