-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthenticate.go
212 lines (175 loc) · 4.58 KB
/
authenticate.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log/syslog"
"math/rand"
"os"
"strings"
"time"
"github.com/arcanericky/opiekey"
)
const (
defaultMaxSeq = 499
defaultRetries = 1
defaultSeedLen = 6
pamSuccess = 0
pamAuthErr = 1
pamCredUnavail = 2
)
type userConfig struct {
name string
maxSeq int
passphrase string
retries int
seedLen int
}
type pamFuncs interface {
GetChallengeResponse(string, string) string
GetUser() string
}
func opieSyslog(data string) {
if lw, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_AUTHPRIV, "opie"); err == nil {
fmt.Fprintf(lw, data)
lw.Close()
}
}
func getRandomSequence(max int) int {
return rand.Intn(max) + 1
}
func getRandomSeed(len int) string {
bytes := make([]byte, len)
for i := 0; i < len; i++ {
j := rand.Intn(36)
if j >= 0 && j <= 9 {
bytes[i] = byte(j + 48)
} else {
bytes[i] = byte(j - 10 + 97)
}
}
return string(bytes)
}
func getUserConfigFromReader(userName string, configReader io.Reader) userConfig {
var config userConfig
type DefaultValues struct {
MaxSeq int `json:"maxseq"`
Passphrase string `json:"passphrase"`
Retries int `json:"retries"`
SeedLen int `json:"seedlen"`
}
type User struct {
Name string `json:"name"`
MaxSeq int `json:"maxseq"`
Passphrase string `json:"passphrase"`
Retries int `json:"retries"`
SeedLen int `json:"seedlen"`
}
type ConfigData struct {
Defaults DefaultValues `json:"defaults"`
Users []User `json:"users"`
}
var configData ConfigData
if byteValue, err := ioutil.ReadAll(configReader); err == nil {
if json.Unmarshal(byteValue, &configData) == nil {
for _, user := range configData.Users {
if userName == user.Name {
config.name = user.Name
if user.MaxSeq > 0 {
config.maxSeq = user.MaxSeq
} else if configData.Defaults.MaxSeq > 0 {
config.maxSeq = configData.Defaults.MaxSeq
} else {
config.maxSeq = defaultMaxSeq
}
if len(user.Passphrase) > 0 {
config.passphrase = user.Passphrase
} else if len(configData.Defaults.Passphrase) > 0 {
config.passphrase = configData.Defaults.Passphrase
} else {
opieSyslog("No passphrase configured")
config.name = ""
}
if user.Retries > 0 {
config.retries = user.Retries
} else if configData.Defaults.Retries > 0 {
config.retries = configData.Defaults.Retries
} else {
config.retries = defaultRetries
}
if user.SeedLen > 0 {
config.seedLen = user.SeedLen
} else if configData.Defaults.SeedLen > 0 {
config.seedLen = configData.Defaults.SeedLen
} else {
config.seedLen = defaultSeedLen
}
}
}
} else {
opieSyslog("Config file could not be parsed")
}
} else {
opieSyslog("Config file could not be read")
}
return config
}
func getUserConfig(userName, configFile string) userConfig {
var config userConfig
info, errStat := os.Stat(configFile)
if errStat == nil && info.Mode().Perm() != 0600 {
opieSyslog(fmt.Sprintf("Attributes too permissible for config file %s", configFile))
} else {
reader, errOpen := os.Open(configFile)
if errOpen == nil {
defer reader.Close()
}
if errStat != nil || errOpen != nil {
opieSyslog(fmt.Sprintf("Could not access config file %s", configFile))
} else {
config = getUserConfigFromReader(userName, reader)
}
}
return config
}
func getOPIEConfigItem(item string, args []string) string {
var value string
for _, arg := range args {
fields := strings.Split(arg, "=")
if len(fields) == 2 {
if fields[0] == item {
value = fields[1]
}
}
}
return value
}
func authenticate(pamService pamFuncs, flags int, args []string) int {
retval := pamAuthErr
if user := pamService.GetUser(); len(user) > 0 {
const promptFormat = "otp-md5 %d %s ext\nPassword: "
opieConfigFile := getOPIEConfigItem("config", args)
if len(opieConfigFile) == 0 {
opieSyslog("Config file parameter not found")
} else {
config := getUserConfig(user, opieConfigFile)
if len(config.name) > 0 {
rand.Seed(time.Now().UnixNano())
seq := getRandomSequence(config.maxSeq)
seed := getRandomSeed(config.seedLen)
prompt := fmt.Sprintf(promptFormat, seq, seed)
expected := opiekey.ComputeWordResponse(seq, seed, config.passphrase, opiekey.MD5)
for i := 0; i < config.retries && retval != pamSuccess; i++ {
actual := pamService.GetChallengeResponse(user, prompt)
if actual == expected {
retval = pamSuccess
}
}
} else {
retval = pamCredUnavail
}
}
}
return retval
}