-
Notifications
You must be signed in to change notification settings - Fork 1
/
repo.go
354 lines (292 loc) · 6.42 KB
/
repo.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/reconquest/hierr-go"
)
type ShadowdHost struct {
address string
resource *http.Client
alive bool
}
type ShadowdUpstream struct {
hosts []*ShadowdHost
}
type NotFoundError struct {
error
}
func NewShadowdHost(
address string, resource *http.Client,
) (*ShadowdHost, error) {
if strings.Contains(address, "://") {
return nil, errors.New(
"shadowd server address must be in host:port format",
)
}
shadowdHost := &ShadowdHost{
address: address,
resource: resource,
alive: true,
}
return shadowdHost, nil
}
func (shadowdHost *ShadowdHost) SetIsAlive(alive bool) {
shadowdHost.alive = alive
}
func (shadowdHost *ShadowdHost) IsAlive() bool {
return shadowdHost.alive
}
func (shadowdHost *ShadowdHost) GetAddr() string {
return shadowdHost.address
}
func (shadowdHost *ShadowdHost) GetShadow(
pool string, username string,
) (*Shadow, error) {
var token string
if pool != "" {
token = pool + "/" + username
} else {
token = username
}
hash, err := shadowdHost.getHash(token)
if err != nil {
if _, ok := err.(NotFoundError); ok {
return nil, err
}
return nil, hierr.Errorf(
err,
"can't retrieve shadow entry for %s",
user{username, pool},
)
}
proofHash, err := shadowdHost.getHash(token)
if err != nil {
if _, ok := err.(NotFoundError); ok {
return nil, err
}
return nil, hierr.Errorf(
err,
"can't retrieve proofing shadow entry for %s",
user{username, pool},
)
}
if hash == proofHash {
warningf(
"[!] hash for %s was recently requested; "+
"possible break-in attempt.",
user{username, pool},
)
}
shadow := &Shadow{
Username: username,
Hash: hash,
}
return shadow, nil
}
func (shadowdHost *ShadowdHost) GetSSHKeys(
pool string, username string,
) (SSHKeys, error) {
var token string
if pool != "" {
token = pool + "/" + username
} else {
token = username
}
body, err := request(
shadowdHost.resource,
"GET", "https://"+shadowdHost.address+"/ssh/"+token,
)
if err != nil {
return nil, err
}
sshKeys := SSHKeys{}
rawKeys := strings.Split(strings.TrimRight(body, "\n"), "\n")
for keyIndex, rawKey := range rawKeys {
key, err := ReadSSHKey(rawKey)
if err != nil {
return nil, hierr.Errorf(
err, "error while parsing #%d key", keyIndex+1,
)
}
sshKeys = append(sshKeys, key)
}
return sshKeys, nil
}
func (shadowdHost *ShadowdHost) getHash(token string) (string, error) {
body, err := request(
shadowdHost.resource,
"GET", "https://"+shadowdHost.address+"/t/"+token,
)
if err != nil {
return "", err
}
return strings.TrimRight(body, "\n"), nil
}
func (shadowdHost *ShadowdHost) GetTokens(base string) ([]string, error) {
body, err := request(
shadowdHost.resource,
"GET",
"https://"+shadowdHost.address+"/t/"+strings.TrimSuffix(base, "/")+"/",
)
if err != nil {
return nil, err
}
return strings.Split(body, "\n"), nil
}
func (shadowdHost *ShadowdHost) GetPasswordChangeSalts(
pool, username string,
) ([]string, error) {
var token string
if pool != "" {
token = pool + "/" + username
} else {
token = username
}
body, err := request(
shadowdHost.resource,
"PUT",
"https://"+shadowdHost.address+"/t/"+strings.TrimSuffix(token, "/"),
)
if err != nil {
return nil, err
}
return strings.Split(strings.TrimSuffix(body, "\n"), "\n"), nil
}
func (shadowdHost *ShadowdHost) ChangePassword(
pool, username string, shadows []string, password string,
) error {
var token string
if pool != "" {
token = pool + "/" + username
} else {
token = username
}
_, err := request(
shadowdHost.resource,
"PUT",
"https://"+shadowdHost.address+"/t/"+strings.TrimSuffix(token, "/"),
url.Values{
"shadow[]": shadows,
"password": []string{password},
},
)
if err != nil {
return err
}
return nil
}
func NewShadowdUpstream(
addresss []string, certificateFilepath string,
) (*ShadowdUpstream, error) {
pemData, err := ioutil.ReadFile(certificateFilepath)
if err != nil {
return nil, hierr.Errorf(
err, "can't read certificate file %s", certificateFilepath,
)
}
pemBlock, _ := pem.Decode(pemData)
if pemBlock == nil {
return nil, fmt.Errorf(
"%s is not valid certificate file because PEM data is not found",
certificateFilepath,
)
}
certificate, err := x509.ParseCertificate(pemBlock.Bytes)
if err != nil {
return nil, hierr.Errorf(
err, "can't parse certificate PEM block",
)
}
certsPool := x509.NewCertPool()
certsPool.AddCert(certificate)
resource := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certsPool,
},
},
}
upstream := ShadowdUpstream{}
for _, address := range addresss {
shadowdHost, err := NewShadowdHost(address, resource)
if err != nil {
return nil, hierr.Errorf(
err, "can't initialize shadowd client for %s", address,
)
}
upstream.hosts = append(upstream.hosts, shadowdHost)
}
return &upstream, nil
}
func (upstream *ShadowdUpstream) GetAliveShadowdHosts() (
[]*ShadowdHost, error,
) {
hosts := []*ShadowdHost{}
for _, host := range upstream.hosts {
if host.IsAlive() {
hosts = append(hosts, host)
}
}
if len(hosts) < 0 {
return nil, errors.New("all shadowd servers has gone away")
}
return hosts, nil
}
func readHTTPResponse(response *http.Response) (string, error) {
debugf("%s", response.Status)
if response.StatusCode != 200 {
if response.StatusCode == 404 {
return "", NotFoundError{
errors.New("404 Not Found"),
}
}
if response.StatusCode == 204 {
return "", NotFoundError{
errors.New("204 No Content"),
}
}
return "", fmt.Errorf("unexpected status %s", response.Status)
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", hierr.Errorf(
err, "can't read response body",
)
}
tracef("response: '%s'", string(body))
return string(body), nil
}
func request(
client *http.Client,
method string,
url string,
body ...url.Values,
) (string, error) {
var payload string
if len(body) > 0 {
payload = body[0].Encode()
}
debugf("%s %s %s", method, url, payload)
request, err := http.NewRequest(
method,
url,
bytes.NewBufferString(payload),
)
if err != nil {
return "", err
}
request.Header.Set("User-Agent", "shadowc/"+version)
response, err := client.Do(request)
if err != nil {
return "", err
}
return readHTTPResponse(response)
}