-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjacksquat.go
111 lines (90 loc) · 2.13 KB
/
jacksquat.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log/syslog"
"os"
"os/user"
"text/template"
"time"
)
const (
unknown = "UNKNOWN"
)
type configValues struct {
LogTemplate string `json:"logtemplate"`
NoticeTemplate string `json:"noticetemplate"`
}
func thisUser() (string, string) {
userName := unknown
uID := unknown
if c, err := user.Current(); err == nil {
userName = c.Username
uID = c.Uid
}
return userName, uID
}
func thisTTYName() string {
tty := unknown
dest, _ := os.Readlink("/proc/self/fd/0")
if len(dest) > 0 {
tty = dest
}
return tty
}
func log(data string) {
if lw, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_AUTH, "jacksquat"); err == nil {
fmt.Fprintf(lw, data)
lw.Close()
}
}
func getConfigFromReader(configReader io.Reader) configValues {
var byteValue []byte
var err error
var config configValues
if byteValue, err = ioutil.ReadAll(configReader); err != nil {
log("config file could not be read")
} else if json.Unmarshal(byteValue, &config) != nil {
log("config file could not be parsed")
}
return config
}
func getConfig(configFile string) configValues {
var config configValues
if reader, err := os.Open(configFile); err == nil {
defer reader.Close()
config = getConfigFromReader(reader)
}
return config
}
func captureLoginWithConfig(config configValues, duration time.Duration, exitCheck func() bool) {
if len(config.LogTemplate) > 0 {
loginID, uID := thisUser()
loginInfo := struct {
UserName string
UserID string
TTYName string
}{loginID, uID, thisTTYName()}
if t, err := template.New("sysloginfo").Parse(config.LogTemplate); err == nil {
var buf bytes.Buffer
if t.Execute(&buf, loginInfo) == nil {
log(buf.String())
}
}
if t, err := template.New("noticeinfo").Parse(config.NoticeTemplate); err == nil {
var buf bytes.Buffer
if t.Execute(&buf, loginInfo) == nil {
fmt.Println(buf.String())
}
}
}
for exitCheck() {
time.Sleep(duration)
}
}
func captureLogin(configFile string, duration time.Duration, exitCheck func() bool) {
captureLoginWithConfig(getConfig(configFile), duration, exitCheck)
}