Skip to content

Commit 43f1b74

Browse files
committed
Properly Close Connections & Single User Overide
1 parent 3251ed8 commit 43f1b74

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
ssh-bastion-*
12
ssh-log-server
23
data/*

config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type SSHConfigGlobal struct {
2727
type SSHConfigServer struct {
2828
HostPubKeyFiles []string `yaml:"host_pubkeys"`
2929
ConnectPath string `yaml:"connect_path"`
30+
LoginUser string `yaml:"login_user"`
3031
}
3132

3233
type SSHConfigACL struct {

forward.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (s *SSHServer) SessionForward(startTime time.Time, sshConn *ssh.ServerConn,
2626
sshConn.Close()
2727
return
2828
}
29+
defer sshConn.Close()
2930

3031
sesschan := NewLogChannel(startTime, rawsesschan, sshConn.User())
3132

@@ -53,6 +54,9 @@ func (s *SSHServer) SessionForward(startTime time.Time, sshConn *ssh.ServerConn,
5354
sesschan.LogRequest(req)
5455
if req.Type == "auth-agent-req@openssh.com" {
5556
agentForwarding = true
57+
if req.WantReply {
58+
req.Reply(true, []byte{})
59+
}
5660
continue
5761
} else if (req.Type == "pty-req") && (req.WantReply) {
5862
req.Reply(true, []byte{})
@@ -112,7 +116,8 @@ func (s *SSHServer) SessionForward(startTime time.Time, sshConn *ssh.ServerConn,
112116
WriteAuthLog("Connecting to remote for relay (%s) by %s from %s.", remote.ConnectPath, sshConn.User(), sshConn.RemoteAddr())
113117
fmt.Fprintf(sesschan, "Connecting to %s\r\n", remote_name)
114118

115-
clientConfig := &ssh.ClientConfig{
119+
var clientConfig *ssh.ClientConfig
120+
clientConfig = &ssh.ClientConfig{
116121
User: sshConn.User(),
117122
Auth: []ssh.AuthMethod{
118123
ssh.PasswordCallback(func() (secret string, err error) {
@@ -121,7 +126,7 @@ func (s *SSHServer) SessionForward(startTime time.Time, sshConn *ssh.ServerConn,
121126
} else {
122127
//log.Printf("Prompting for password for remote...")
123128
t := terminal.NewTerminal(sesschan, "")
124-
s, err := t.ReadPassword(fmt.Sprintf("%s@%s password: ", sshConn.User(), remote_name))
129+
s, err := t.ReadPassword(fmt.Sprintf("%s@%s password: ", clientConfig.User, remote_name))
125130
//log.Printf("Got password for remote auth, err: %s", err)
126131
return s, err
127132
}
@@ -151,6 +156,10 @@ func (s *SSHServer) SessionForward(startTime time.Time, sshConn *ssh.ServerConn,
151156
},
152157
}
153158

159+
if len(remote.LoginUser) > 0 {
160+
clientConfig.User = remote.LoginUser
161+
}
162+
154163
// Set up the agent
155164
if agentForwarding {
156165
agentChan, agentReqs, err := sshConn.OpenChannel("auth-agent@openssh.com", nil)
@@ -173,6 +182,7 @@ func (s *SSHServer) SessionForward(startTime time.Time, sshConn *ssh.ServerConn,
173182
sesschan.Close()
174183
return
175184
}
185+
defer client.Close()
176186
log.Printf("Dialled Remote SSH Successfully...")
177187

178188
// Forward the session channel

0 commit comments

Comments
 (0)