-
Notifications
You must be signed in to change notification settings - Fork 2
/
lunchd.go
174 lines (151 loc) · 3.17 KB
/
lunchd.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"rpc"
"log"
"net"
"os"
"crypto/hmac"
"crypto/sha512"
"crypto/subtle"
"io/ioutil"
"sync"
"json"
"flag"
"strconv"
"gob"
)
var port = flag.Uint("p", 1234, "Specifies the port to listen on.")
var configFile = flag.String("c", "config.json", "Specify a config file.")
var dataFile = flag.String("d", "", "Specify a data file.")
var displayHelp = flag.Bool("help", false, "Displays this help message.")
var logFile = flag.String("log", "stderr", "Print errors to logfile.")
type ServerConfig struct {
Sekritz map[string]string
}
var userMap map[string]*Auth
var config *ServerConfig
var cMutex sync.Mutex
func loadUsersFromFile() (err os.Error) {
tempConfig := &ServerConfig{}
read, err := ioutil.ReadFile(*configFile)
if err != nil {
return err
}
err = json.Unmarshal(read, tempConfig)
if err != nil {
return err
}
cMutex.Lock()
config = tempConfig
cMutex.Unlock()
return nil
}
func checkUser(name string) bool {
loadUsersFromFile()
cMutex.Lock()
_, valid := config.Sekritz[name]
cMutex.Unlock()
if valid {
bin := make(Bin, 512)
userMap[name] = &Auth{Name: name, SChallenge: &bin}
}
return valid
}
func verify(a *Auth, d Byter) (bool, os.Error) {
cMutex.Lock()
key, ok := config.Sekritz[(*a).Name]
cMutex.Unlock()
if !ok {
return false, os.NewError("Unknown User")
}
mac := hmac.New(sha512.New, []byte(key))
mac.Write([]byte((*a).Name))
mac.Write(*(*a).CChallenge)
mac.Write(d.Byte())
mac.Write(*userMap[(*a).Name].SChallenge)
if subtle.ConstantTimeCompare(mac.Sum(), *(*a).Mac) == 1 {
return true, nil
}
return false, os.NewError("Authentication Failed")
}
func main() {
flag.Parse()
if *logFile == "stderr" {
log.SetOutput(os.Stderr)
} else {
logger, err := os.Open(*logFile, os.O_WRONLY|os.O_CREATE, 0640)
if err != nil {
log.SetOutput(os.Stderr)
log.Exit(err)
}
log.SetOutput(logger)
}
if *displayHelp {
flag.PrintDefaults()
return
}
userMap = make(map[string]*Auth)
err := loadUsersFromFile()
if err != nil {
log.Exit("Error reading config file. Have you created it?\nCaused By: ", err)
}
var t *LunchTracker
if *dataFile != "" {
stat, err := os.Stat(*dataFile)
if err != nil {
panic(err)
}
if stat.IsRegular() {
file, ferr := os.Open(*dataFile, os.O_RDONLY, 0600)
if ferr != nil {
panic(ferr)
}
t = oldPollChan(file)
}
} else {
t = newPollChan()
}
*dataFile = "poll.gob"
rpc.Register(t)
l, e := net.Listen("tcp", ":"+strconv.Uitoa(*port))
if e != nil {
log.Exit("listen error:", e)
}
for {
conn, err := l.Accept()
if err != nil {
log.Println("Failed to accept client", err)
} else {
go func() {
defer func() {
if x := recover(); x != nil {
log.Println("Fatal Exception", x)
}
}()
rpc.ServeConn(conn)
}()
}
}
}
func newPollChan() *LunchTracker {
return pollChan(NewPoll())
}
func oldPollChan(file *os.File) *LunchTracker {
decode := gob.NewDecoder(file)
poll := &LunchPoll{}
err := decode.Decode(poll)
if err != nil {
panic(err)
}
return pollChan(*poll)
}
func pollChan(poll LunchPoll) *LunchTracker {
ch := LunchTracker(make(chan LunchPoll))
go func() {
for {
ch <- poll
poll = <-ch
}
}()
return &ch
}