-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
192 lines (175 loc) · 4.62 KB
/
main.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
package main
import (
"bytes"
"context"
"flag"
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)
var (
configFile string
bind string
debug bool
)
func init() {
flag.StringVar(&configFile, "config", "s3-sftp-proxy.toml", "configuration file")
flag.StringVar(&bind, "bind", "", "listen on addr:port")
flag.BoolVar(&debug, "debug", false, "turn on debugging output")
}
func buildSSHServerConfig(buckets *S3Buckets, cfg *S3SFTPProxyConfig) (*ssh.ServerConfig, error) {
pem, err := ioutil.ReadFile(cfg.HostKeyFile)
if err != nil {
return nil, errors.Wrapf(err, `failed to open "%s"`, cfg.HostKeyFile)
}
key, err := ssh.ParseRawPrivateKey(pem)
if err != nil {
return nil, errors.Wrapf(err, `failed to parse host key "%s"`, cfg.HostKeyFile)
}
c := &ssh.ServerConfig{
PasswordCallback: func(c ssh.ConnMetadata, passwd []byte) (*ssh.Permissions, error) {
bucket, ok := buckets.UserToBucketMap[c.User()]
if !ok {
return nil, fmt.Errorf("unknown user: %s", c.User())
}
u := bucket.Users.Lookup(c.User())
if u.Password != "" && u.Password == string(passwd) {
return nil, nil
}
return nil, fmt.Errorf("passwords do not match")
},
PublicKeyCallback: func(c ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
bucket, ok := buckets.UserToBucketMap[c.User()]
if !ok {
return nil, fmt.Errorf("unknown user: %s", c.User())
}
u := bucket.Users.Lookup(c.User())
if u.PublicKeys != nil {
keyMarshaled := key.Marshal()
for _, herKey := range u.PublicKeys {
if herKey.Type() == key.Type() && len(herKey.Marshal()) == len(keyMarshaled) && bytes.Compare(herKey.Marshal(), keyMarshaled) == 0 {
return &ssh.Permissions{
Extensions: map[string]string{
"pubkey-fp": ssh.FingerprintSHA256(key),
},
}, nil
}
}
}
return nil, fmt.Errorf("public keys do not match")
},
KeyboardInteractiveCallback: func(c ssh.ConnMetadata, client ssh.KeyboardInteractiveChallenge) (*ssh.Permissions, error) {
bucket, ok := buckets.UserToBucketMap[c.User()]
if !ok {
return nil, fmt.Errorf("unknown user: %s", c.User())
}
if !bucket.KeyboardInteractiveAuthEnabled {
return nil, fmt.Errorf("keyboard interactive authentication not enabled")
}
u := bucket.Users.Lookup(c.User())
if u.Password == "" {
return nil, fmt.Errorf("no credentials are present")
}
answers, err := client(u.Name, "", []string{"Password: "}, []bool{false})
if err != nil {
return nil, errors.Wrapf(err, "keyboard interactive conversation failed")
}
if answers[0] != u.Password {
return nil, fmt.Errorf("passwords do not match")
}
return nil, nil
},
BannerCallback: func(c ssh.ConnMetadata) string {
return cfg.Banner
},
}
sgn, err := ssh.NewSignerFromKey(key)
if err != nil {
return nil, err
}
c.AddHostKey(sgn)
return c, nil
}
func bail(msg string, status ...interface{}) {
os.Stderr.Write([]byte(msg + "\n"))
statusCode := 1
if len(status) > 0 {
var ok bool
statusCode, ok = status[0].(int)
if !ok {
panic("invalid argument for bail()")
}
}
os.Exit(statusCode)
}
func main() {
flag.Parse()
cfg, err := ReadConfigFromFile(configFile)
if err != nil {
bail(err.Error())
}
uStores, err := NewUserStoresFromConfig(cfg)
if err != nil {
bail(err.Error())
}
buckets, err := NewS3BucketFromConfig(uStores, cfg)
if err != nil {
bail(err.Error())
}
sCfg, err := buildSSHServerConfig(buckets, cfg)
if err != nil {
bail(err.Error())
}
_bind := bind
if _bind == "" {
_bind = cfg.Bind
if _bind == "" {
_bind = ":10022"
}
}
logger := logrus.New()
if debug {
logger.SetLevel(logrus.DebugLevel)
}
lsnr, err := net.Listen("tcp", _bind)
if err != nil {
bail(err.Error())
}
defer lsnr.Close()
logger.Info("Listen on ", _bind)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigChan := make(chan os.Signal)
signal.Notify(sigChan, os.Interrupt)
errChan := make(chan error)
go func() {
errChan <- (&Server{
S3Buckets: buckets,
ServerConfig: sCfg,
Log: logger,
ReaderLookbackBufferSize: *cfg.ReaderLookbackBufferSize,
ReaderMinChunkSize: *cfg.ReaderMinChunkSize,
ListerLookbackBufferSize: *cfg.ListerLookbackBufferSize,
PhantomObjectMap: NewPhantomObjectMap(),
Now: time.Now,
}).RunListenerEventLoop(ctx, lsnr.(*net.TCPListener))
}()
outer:
for {
select {
case err = <-errChan:
if err != nil {
bail(err.Error())
}
break outer
case <-sigChan:
cancel()
}
}
}