| 1 |
package sshutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"log" |
| 6 |
"net" |
| 7 |
"os" |
| 8 |
"os/exec" |
| 9 |
"path/filepath" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"github.com/pkg/errors" |
| 13 |
"github.com/smallstep/cli/config" |
| 14 |
"golang.org/x/crypto/ssh" |
| 15 |
"golang.org/x/crypto/ssh/knownhosts" |
| 16 |
"golang.org/x/crypto/ssh/terminal" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
func ProxyCommand(cmd, user, host, port string) string { |
| 25 |
cmd = strings.Replace(cmd, "%%", "%", -1) |
| 26 |
cmd = strings.Replace(cmd, "%h", host, -1) |
| 27 |
cmd = strings.Replace(cmd, "%p", port, -1) |
| 28 |
return strings.Replace(cmd, "%r", user, -1) |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
type ShellOption func(s *Shell) error |
| 33 |
|
| 34 |
|
| 35 |
func WithAuthMethod(am ssh.AuthMethod) ShellOption { |
| 36 |
return func(s *Shell) error { |
| 37 |
s.authMethods = append(s.authMethods, am) |
| 38 |
return nil |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
func WithSigner(signer ssh.Signer) ShellOption { |
| 44 |
return func(s *Shell) error { |
| 45 |
s.authMethods = append(s.authMethods, ssh.PublicKeys(signer)) |
| 46 |
return nil |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
func WithCertificate(cert *ssh.Certificate, priv interface{}) ShellOption { |
| 53 |
return func(s *Shell) error { |
| 54 |
signer, err := NewCertSigner(cert, priv) |
| 55 |
if err != nil { |
| 56 |
return err |
| 57 |
} |
| 58 |
s.authMethods = append(s.authMethods, ssh.PublicKeys(signer)) |
| 59 |
return nil |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
func WithAddUser(user string, cert *ssh.Certificate, priv interface{}) ShellOption { |
| 66 |
return func(s *Shell) error { |
| 67 |
return errors.New("not yet implemented") |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
func WithBastion(user, address, command string) ShellOption { |
| 73 |
return func(s *Shell) error { |
| 74 |
s.dialer = func(callback ssh.HostKeyCallback) (*ssh.Client, error) { |
| 75 |
|
| 76 |
bastion, err := ssh.Dial("tcp", address, &ssh.ClientConfig{ |
| 77 |
User: user, |
| 78 |
Auth: s.authMethods, |
| 79 |
HostKeyCallback: callback, |
| 80 |
}) |
| 81 |
if err != nil { |
| 82 |
return nil, errors.Wrapf(err, "error connecting %s", address) |
| 83 |
} |
| 84 |
|
| 85 |
conn, err := bastion.Dial("tcp", s.address) |
| 86 |
if err != nil { |
| 87 |
return nil, errors.Wrapf(err, "error connecting %s", s.address) |
| 88 |
} |
| 89 |
c, chans, reqs, err := ssh.NewClientConn(conn, s.address, &ssh.ClientConfig{ |
| 90 |
User: s.user, |
| 91 |
Auth: s.authMethods, |
| 92 |
HostKeyCallback: callback, |
| 93 |
}) |
| 94 |
if err != nil { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
return ssh.NewClient(c, chans, reqs), nil |
| 98 |
} |
| 99 |
return nil |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
func WithProxyCommand(command string) ShellOption { |
| 105 |
return func(s *Shell) error { |
| 106 |
s.dialer = func(callback ssh.HostKeyCallback) (*ssh.Client, error) { |
| 107 |
host, port, err := net.SplitHostPort(s.address) |
| 108 |
if err != nil { |
| 109 |
return nil, errors.Wrap(err, "error parsing address") |
| 110 |
} |
| 111 |
pr, pw := net.Pipe() |
| 112 |
args := strings.Fields(ProxyCommand(command, s.user, host, port)) |
| 113 |
cmd := exec.Command(args[0], args[1:]...) |
| 114 |
cmd.Stdin = pw |
| 115 |
cmd.Stdout = pw |
| 116 |
cmd.Stderr = os.Stderr |
| 117 |
if err := cmd.Start(); err != nil { |
| 118 |
return nil, errors.Wrap(err, "error running proxy command") |
| 119 |
} |
| 120 |
c, chans, reqs, err := ssh.NewClientConn(pr, s.address, &ssh.ClientConfig{ |
| 121 |
User: s.user, |
| 122 |
Auth: s.authMethods, |
| 123 |
HostKeyCallback: callback, |
| 124 |
}) |
| 125 |
if err != nil { |
| 126 |
return nil, err |
| 127 |
} |
| 128 |
return ssh.NewClient(c, chans, reqs), nil |
| 129 |
} |
| 130 |
return nil |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
func withDefaultAuthMethod() ShellOption { |
| 136 |
return func(s *Shell) error { |
| 137 |
agent, err := DialAgent() |
| 138 |
if err != nil { |
| 139 |
return err |
| 140 |
} |
| 141 |
s.authMethods = append(s.authMethods, agent.AuthMethod()) |
| 142 |
return nil |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
func withDefaultDialer(user, address string) ShellOption { |
| 148 |
return func(s *Shell) error { |
| 149 |
s.dialer = func(callback ssh.HostKeyCallback) (*ssh.Client, error) { |
| 150 |
client, err := ssh.Dial("tcp", address, &ssh.ClientConfig{ |
| 151 |
User: user, |
| 152 |
Auth: s.authMethods, |
| 153 |
HostKeyCallback: callback, |
| 154 |
}) |
| 155 |
if err != nil { |
| 156 |
return nil, errors.Wrapf(err, "error connecting %s", address) |
| 157 |
} |
| 158 |
return client, nil |
| 159 |
} |
| 160 |
return nil |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func formatAddress(address string) string { |
| 165 |
if _, _, err := net.SplitHostPort(address); err != nil { |
| 166 |
address += ":22" |
| 167 |
} |
| 168 |
return address |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
type Shell struct { |
| 173 |
user string |
| 174 |
address string |
| 175 |
authMethods []ssh.AuthMethod |
| 176 |
client *ssh.Client |
| 177 |
dialer func(callback ssh.HostKeyCallback) (*ssh.Client, error) |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
func NewShell(user, address string, opts ...ShellOption) (*Shell, error) { |
| 182 |
address = formatAddress(address) |
| 183 |
|
| 184 |
|
| 185 |
knownHosts, err := knownhosts.New(filepath.Join(config.Home(), ".ssh", "known_hosts")) |
| 186 |
if err != nil { |
| 187 |
return nil, errors.Wrap(err, "error reading known_hosts") |
| 188 |
} |
| 189 |
|
| 190 |
shell := &Shell{ |
| 191 |
user: user, |
| 192 |
address: address, |
| 193 |
} |
| 194 |
if err := shell.apply(opts); err != nil { |
| 195 |
return nil, err |
| 196 |
} |
| 197 |
|
| 198 |
if len(shell.authMethods) == 0 { |
| 199 |
if err := withDefaultAuthMethod()(shell); err != nil { |
| 200 |
return nil, err |
| 201 |
} |
| 202 |
} |
| 203 |
if shell.dialer == nil { |
| 204 |
if err := withDefaultDialer(user, address)(shell); err != nil { |
| 205 |
return nil, err |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
if shell.client, err = shell.dialer(knownHosts); err != nil { |
| 210 |
return nil, err |
| 211 |
} |
| 212 |
|
| 213 |
return shell, nil |
| 214 |
} |
| 215 |
|
| 216 |
func (s *Shell) apply(opts []ShellOption) error { |
| 217 |
for _, fn := range opts { |
| 218 |
if err := fn(s); err != nil { |
| 219 |
return err |
| 220 |
} |
| 221 |
} |
| 222 |
return nil |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
func (s *Shell) Close() error { |
| 227 |
return s.client.Close() |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
func (s *Shell) Run(cmd string) error { |
| 232 |
|
| 233 |
session, err := s.client.NewSession() |
| 234 |
if err != nil { |
| 235 |
return errors.Wrap(err, "error creating a session") |
| 236 |
} |
| 237 |
defer session.Close() |
| 238 |
|
| 239 |
return session.Run(cmd) |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
func (s *Shell) RemoteShell() error { |
| 244 |
|
| 245 |
session, err := s.client.NewSession() |
| 246 |
if err != nil { |
| 247 |
return errors.Wrap(err, "error creating a session") |
| 248 |
} |
| 249 |
defer session.Close() |
| 250 |
|
| 251 |
var fallback bool |
| 252 |
if fd := int(os.Stdin.Fd()); terminal.IsTerminal(fd) { |
| 253 |
|
| 254 |
if originalState, err := terminal.MakeRaw(fd); err != nil { |
| 255 |
fallback = true |
| 256 |
} else { |
| 257 |
defer terminal.Restore(fd, originalState) |
| 258 |
|
| 259 |
|
| 260 |
w, h, err := terminal.GetSize(fd) |
| 261 |
if err != nil { |
| 262 |
w, h = 80, 40 |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
if err := requestPty(session, h, w, ssh.TerminalModes{ |
| 267 |
ssh.ECHO: 1, |
| 268 |
ssh.TTY_OP_ISPEED: 14400, |
| 269 |
ssh.TTY_OP_OSPEED: 14400, |
| 270 |
}); err != nil { |
| 271 |
return err |
| 272 |
} |
| 273 |
} |
| 274 |
} else { |
| 275 |
fallback = true |
| 276 |
} |
| 277 |
|
| 278 |
if fallback { |
| 279 |
if err := requestPty(session, 40, 80, ssh.TerminalModes{ |
| 280 |
ssh.ECHO: 0, |
| 281 |
ssh.TTY_OP_ISPEED: 14400, |
| 282 |
ssh.TTY_OP_OSPEED: 14400, |
| 283 |
}); err != nil { |
| 284 |
return errors.Wrap(err, "error getting pseudo terminal") |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
stdin, err := session.StdinPipe() |
| 289 |
if err != nil { |
| 290 |
return errors.Wrap(err, "cannot setup stdin for session") |
| 291 |
} |
| 292 |
go io.Copy(stdin, os.Stdin) |
| 293 |
|
| 294 |
stdout, err := session.StdoutPipe() |
| 295 |
if err != nil { |
| 296 |
return errors.Wrap(err, "cannot setup stdout for session") |
| 297 |
} |
| 298 |
go io.Copy(os.Stdout, stdout) |
| 299 |
|
| 300 |
stderr, err := session.StderrPipe() |
| 301 |
if err != nil { |
| 302 |
return errors.Wrap(err, "cannot setup stderr for session") |
| 303 |
} |
| 304 |
go io.Copy(os.Stderr, stderr) |
| 305 |
|
| 306 |
|
| 307 |
if err := session.Shell(); err != nil { |
| 308 |
return errors.Wrap(err, "error starting remote shell") |
| 309 |
} |
| 310 |
|
| 311 |
return session.Wait() |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
func (s *Shell) LocalForward(bindNetwork, bindAddress, hostNetwork, hostAddress string) error { |
| 317 |
l, err := net.Listen(bindNetwork, bindAddress) |
| 318 |
if err != nil { |
| 319 |
return errors.Wrapf(err, "error listening on %s", bindAddress) |
| 320 |
} |
| 321 |
defer l.Close() |
| 322 |
|
| 323 |
remote, err := s.client.Dial(hostNetwork, hostAddress) |
| 324 |
if err != nil { |
| 325 |
return errors.Wrapf(err, "error dialing %s", hostAddress) |
| 326 |
} |
| 327 |
defer remote.Close() |
| 328 |
|
| 329 |
for { |
| 330 |
local, err := l.Accept() |
| 331 |
if err != nil { |
| 332 |
return errors.Wrapf(err, "error connecting to %s", bindAddress) |
| 333 |
} |
| 334 |
handleConn(local, remote) |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
func (s *Shell) RemoteForward(bindNetwork, bindAddress, hostNetwork, hostAddress string) error { |
| 341 |
l, err := s.client.Listen(bindNetwork, bindAddress) |
| 342 |
if err != nil { |
| 343 |
return errors.Wrapf(err, "error listening on %s", bindAddress) |
| 344 |
} |
| 345 |
defer l.Close() |
| 346 |
|
| 347 |
for { |
| 348 |
local, err := net.Dial(hostNetwork, hostAddress) |
| 349 |
if err != nil { |
| 350 |
return errors.Wrapf(err, "error dialing %s", hostAddress) |
| 351 |
} |
| 352 |
remote, err := l.Accept() |
| 353 |
if err != nil { |
| 354 |
return errors.Wrapf(err, "error connection to %s", bindAddress) |
| 355 |
} |
| 356 |
handleConn(remote, local) |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
func requestPty(session *ssh.Session, h, w int, modes ssh.TerminalModes) (err error) { |
| 361 |
var terms []string |
| 362 |
switch t := os.Getenv("TERM"); t { |
| 363 |
case "", "xterm-256color": |
| 364 |
terms = append(terms, "xterm-256color", "xterm") |
| 365 |
case "xterm": |
| 366 |
terms = append(terms, "xterm") |
| 367 |
default: |
| 368 |
terms = append(terms, t, "xterm-256color", "xterm") |
| 369 |
} |
| 370 |
for _, t := range terms { |
| 371 |
if err = session.RequestPty(t, h, w, modes); err == nil { |
| 372 |
return |
| 373 |
} |
| 374 |
} |
| 375 |
return errors.Wrap(err, "error getting pseudo terminal") |
| 376 |
} |
| 377 |
|
| 378 |
func handleConn(local, remote net.Conn) { |
| 379 |
chDone := make(chan bool) |
| 380 |
|
| 381 |
|
| 382 |
go func() { |
| 383 |
_, err := io.Copy(local, remote) |
| 384 |
if err != nil { |
| 385 |
log.Println("error while copy remote->local:", err) |
| 386 |
} |
| 387 |
chDone <- true |
| 388 |
}() |
| 389 |
|
| 390 |
|
| 391 |
go func() { |
| 392 |
_, err := io.Copy(remote, local) |
| 393 |
if err != nil { |
| 394 |
log.Println(err) |
| 395 |
} |
| 396 |
chDone <- true |
| 397 |
}() |
| 398 |
|
| 399 |
<-chDone |
| 400 |
} |