summaryrefslogtreecommitdiff
path: root/pkg/server/hook/auth.go
blob: 9c8ca35eadde396bd2a2eaed4d5e3a5eccd30704 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package hook

import (
	"bufio"
	"crypto/md5"
	"crypto/rand"
	"errors"
	"fmt"
	"io"
	"os"
	"strings"
	"time"

	"tunnel/pkg/server/env"
	"tunnel/pkg/server/queue"
)

const authTimeout = 5 * time.Second

const saltSize = 16
const hashSize = md5.Size

var hashNone = strings.Repeat("\x00", hashSize)

type authHook struct {
	Passive bool
}

type auth struct {
	file *os.File
	user string
	salt string
	pass string

	peer struct {
		user string
		salt string
	}

	hash string

	passive bool

	stageHello chan struct{}
	stageHash  chan struct{}

	ready  chan struct{}
	recver chan struct{}
	sender chan struct{}

	tmr *time.Timer
}

var errAuthFail = errors.New("mismatch auth hash")
var errAuthUnknown = errors.New("unknown auth peer")
var errTimeout = errors.New("timeout")

func hashsum(args ...string) string {
	h := md5.New()

	for _, s := range args {
		io.WriteString(h, s)
	}

	return string(h.Sum(nil))
}

func getpass(f *os.File, salt string, user string) string {
	f.Seek(0, 0)

	match := func(s, t string) bool {
		if salt == "" {
			return s == t
		}
		return hashsum(salt, s) == t
	}

	for scanner := bufio.NewScanner(f); scanner.Scan(); {
		splitted := strings.SplitN(scanner.Text(), "#", 2)
		tokens := strings.Fields(splitted[0])
		if len(tokens) > 1 && match(tokens[0], user) {
			return tokens[1]
		}
	}

	return ""
}

func (a *auth) Init(authfile string, user string) error {
	file, err := os.Open(authfile)
	if err != nil {
		return fmt.Errorf("authfile: %w", err)
	}

	a.file = file

	if user != "" {
		pass := getpass(file, "", user)
		if pass == "" {
			file.Close()
			return fmt.Errorf("authfile: no pass for user '%s'", user)
		}
		a.user = user
		a.pass = pass
	}

	b := make([]byte, saltSize)
	if _, err := rand.Read(b); err != nil {
		return err
	}
	a.salt = string(b)

	a.tmr = time.NewTimer(authTimeout)

	return nil
}

func (a *auth) sync(c chan struct{}) error {
	select {
	case <-a.tmr.C:
		return errTimeout
	case <-a.recver:
		return io.EOF
	case <-a.sender:
		return io.EOF
	case <-c:
		return nil
	}
}

func (a *auth) Send(rq, wq queue.Q) error {
	w := wq.Writer()

	io.WriteString(w, a.salt)
	if a.passive {
		io.WriteString(w, hashNone)
	} else {
		io.WriteString(w, hashsum(a.salt, a.user))
	}

	if err := a.sync(a.stageHello); err != nil {
		return err
	}

	if a.passive && a.peer.user == hashNone {
		close(a.sender)
		return fmt.Errorf("can not be passive together")
	}

	var pass string

	if a.peer.user == hashNone {
		pass = hashsum(a.pass)
	} else {
		pass = getpass(a.file, a.peer.salt, a.peer.user)
		if pass == "" {
			close(a.sender)
			return fmt.Errorf("no pass for peer")
		}

		if a.passive {
			a.pass = hashsum(pass)
		}
	}

	io.WriteString(w, hashsum(a.peer.salt, pass))

	if err := a.sync(a.stageHash); err != nil {
		return err
	}

	if a.hash != hashsum(a.salt, a.pass) {
		close(a.sender)
		return errAuthFail
	}

	close(a.ready)

	return queue.IoCopy(rq.Reader(), w)
}

func (a *auth) read(r io.Reader, n int, s *string) error {
	b := make([]byte, n)

	if _, err := io.ReadFull(r, b); err != nil {
		close(a.recver)
		return err
	}

	*s = string(b)
	return nil
}

func (a *auth) Recv(rq, wq queue.Q) error {
	r := rq.Reader()

	if err := a.read(r, saltSize, &a.peer.salt); err != nil {
		return err
	}

	if err := a.read(r, hashSize, &a.peer.user); err != nil {
		return err
	}

	close(a.stageHello)

	if err := a.read(r, hashSize, &a.hash); err != nil {
		return err
	}

	close(a.stageHash)

	if err := a.sync(a.ready); err != nil {
		return err
	}

	a.tmr.Stop()

	return queue.IoCopy(r, wq.Writer())
}

func (a *auth) Close() {
	a.file.Close()
}

func (h *authHook) New(env env.Env) (interface{}, error) {
	file := env.Value("authfile")
	if file == "" {
		return nil, errors.New("no authfile configured")
	}

	user := ""

	if !h.Passive {
		user = env.Value("authuser")

		if user == "" {
			return nil, errors.New("no authuser configured")
		}
	}

	a := &auth{
		passive: h.Passive,

		ready:  make(chan struct{}),
		recver: make(chan struct{}),
		sender: make(chan struct{}),

		stageHello: make(chan struct{}),
		stageHash:  make(chan struct{}),
	}

	if err := a.Init(file, user); err != nil {
		return nil, err
	}

	return a, nil
}

func init() {
	register("auth", "chap out/in", authHook{})
}