/
auth.go
177 lines (148 loc) · 4.58 KB
/
auth.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
package yap
import (
"encoding/base64"
"errors"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
"bytes"
"crypto/sha1"
"crypto/subtle"
auth "github.com/abbot/go-http-auth"
"github.com/cloudflare/golibs/lrucache"
"github.com/phuslu/glog"
"golang.org/x/crypto/bcrypt"
)
type compareFunc func(hashedPassword, password []byte) error
var (
errMismatchedHashAndPassword = errors.New("mismatched hash and password")
errUserNameNotFound = errors.New("user name not found")
compareFuncs = []struct {
prefix string
compare compareFunc
}{
{"", compareMD5HashAndPassword}, // default compareFunc
{"{SHA}", compareShaHashAndPassword},
// Bcrypt is complicated. According to crypt(3) from
// crypt_blowfish version 1.3 (fetched from
// http://www.openwall.com/crypt/crypt_blowfish-1.3.tar.gz), there
// are three different has prefixes: "$2a$", used by versions up
// to 1.0.4, and "$2x$" and "$2y$", used in all later
// versions. "$2a$" has a known bug, "$2x$" was added as a
// migration path for systems with "$2a$" prefix and still has a
// bug, and only "$2y$" should be used by modern systems. The bug
// has something to do with handling of 8-bit characters. Since
// both "$2a$" and "$2x$" are deprecated, we are handling them the
// same way as "$2y$", which will yield correct results for 7-bit
// character passwords, but is wrong for 8-bit character
// passwords. You have to upgrade to "$2y$" if you want sant 8-bit
// character password support with bcrypt. To add to the mess,
// OpenBSD 5.5. introduced "$2b$" prefix, which behaves exactly
// like "$2y$" according to the same source.
{"$2a$", bcrypt.CompareHashAndPassword},
{"$2b$", bcrypt.CompareHashAndPassword},
{"$2x$", bcrypt.CompareHashAndPassword},
{"$2y$", bcrypt.CompareHashAndPassword},
}
)
func compareShaHashAndPassword(hashedPassword, password []byte) error {
d := sha1.New()
d.Write(password)
if subtle.ConstantTimeCompare(hashedPassword[5:], []byte(base64.StdEncoding.EncodeToString(d.Sum(nil)))) != 1 {
return errMismatchedHashAndPassword
}
return nil
}
func compareMD5HashAndPassword(hashedPassword, password []byte) error {
parts := bytes.SplitN(hashedPassword, []byte("$"), 4)
if len(parts) != 4 {
return errMismatchedHashAndPassword
}
magic := []byte("$" + string(parts[1]) + "$")
salt := parts[2]
if subtle.ConstantTimeCompare(hashedPassword, auth.MD5Crypt(password, salt, magic)) != 1 {
return errMismatchedHashAndPassword
}
return nil
}
type Authenticator interface {
Authenticate(username, password string) error
}
type HtpasswdFileAuth struct {
CacheSize uint
FilePath string
secrets auth.SecretProvider
cache lrucache.Cache
once sync.Once
}
func (h *HtpasswdFileAuth) init() {
h.cache = lrucache.NewLRUCache(h.CacheSize)
h.secrets = auth.HtpasswdFileProvider(h.FilePath)
}
func (h *HtpasswdFileAuth) Authenticate(username, password string) error {
h.once.Do(h.init)
credential := username + ":" + password
if _, ok := h.cache.GetNotStale(credential); ok {
return nil
}
secret := h.secrets(username, "")
if secret == "" {
return errUserNameNotFound
}
// default compare function: compareMD5HashAndPassword
compare := compareFuncs[0].compare
for _, cmp := range compareFuncs[1:] {
if strings.HasPrefix(secret, cmp.prefix) {
compare = cmp.compare
break
}
}
err := compare([]byte(secret), []byte(password))
if err != nil {
return err
}
h.cache.Set(credential, struct{}{}, time.Now().Add(2*time.Hour))
return nil
}
type SimplePAM struct {
CacheSize uint
path string
cache lrucache.Cache
once sync.Once
}
func (p *SimplePAM) init() {
p.cache = lrucache.NewLRUCache(p.CacheSize)
exe, err := os.Executable()
if err != nil {
glog.Fatalf("os.Executable() error: %+v", err)
}
p.path = filepath.Join(filepath.Dir(exe), "pwauth")
if _, err := os.Stat(p.path); err != nil {
glog.Fatalf("os.Stat(%#v) error: %+v", p.path, err)
}
if syscall.Geteuid() != 0 {
glog.Warningf("Please run as root if you want to use pam auth")
}
}
func (p *SimplePAM) Authenticate(username, password string) error {
p.once.Do(p.init)
credential := username + ":" + password
if _, ok := p.cache.GetNotStale(credential); ok {
return nil
}
cmd := exec.Command(p.path)
cmd.Stdin = strings.NewReader(username + "\n" + password + "\n")
err := cmd.Run()
if err != nil {
glog.Warningf("Authenticator: username=%v password=%v error: %+v", username, password, err)
time.Sleep(time.Duration(5+rand.Intn(6)) * time.Second)
return err
}
p.cache.Set(credential, struct{}{}, time.Now().Add(2*time.Hour))
return nil
}