2015-03-10 00:11:57 -04:00
|
|
|
package ansible
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-08-24 13:53:39 -04:00
|
|
|
"log"
|
2015-03-10 00:11:57 -04:00
|
|
|
"net"
|
2016-08-29 01:22:48 -04:00
|
|
|
"strings"
|
2015-03-10 00:11:57 -04:00
|
|
|
|
2016-09-25 18:57:02 -04:00
|
|
|
"github.com/google/shlex"
|
2015-03-10 00:11:57 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
2016-08-29 01:22:48 -04:00
|
|
|
// An adapter satisfies SSH requests (from an Ansible client) by delegating SSH
|
|
|
|
// exec and subsystem commands to a packer.Communicator.
|
2015-03-10 00:11:57 -04:00
|
|
|
type adapter struct {
|
|
|
|
done <-chan struct{}
|
|
|
|
l net.Listener
|
|
|
|
config *ssh.ServerConfig
|
|
|
|
sftpCmd string
|
|
|
|
ui packer.Ui
|
|
|
|
comm packer.Communicator
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAdapter(done <-chan struct{}, l net.Listener, config *ssh.ServerConfig, sftpCmd string, ui packer.Ui, comm packer.Communicator) *adapter {
|
|
|
|
return &adapter{
|
|
|
|
done: done,
|
|
|
|
l: l,
|
|
|
|
config: config,
|
|
|
|
sftpCmd: sftpCmd,
|
|
|
|
ui: ui,
|
|
|
|
comm: comm,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *adapter) Serve() {
|
2016-11-05 09:25:45 -04:00
|
|
|
log.Printf("SSH proxy: serving on %s", c.l.Addr())
|
2015-03-10 00:11:57 -04:00
|
|
|
|
|
|
|
for {
|
|
|
|
// Accept will return if either the underlying connection is closed or if a connection is made.
|
|
|
|
// after returning, check to see if c.done can be received. If so, then Accept() returned because
|
|
|
|
// the connection has been closed.
|
|
|
|
conn, err := c.l.Accept()
|
|
|
|
select {
|
|
|
|
case <-c.done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if err != nil {
|
|
|
|
c.ui.Error(fmt.Sprintf("listen.Accept failed: %v", err))
|
2015-12-19 15:15:04 -05:00
|
|
|
continue
|
2015-03-10 00:11:57 -04:00
|
|
|
}
|
|
|
|
go func(conn net.Conn) {
|
2015-12-19 16:05:59 -05:00
|
|
|
if err := c.Handle(conn, c.ui); err != nil {
|
|
|
|
c.ui.Error(err.Error())
|
|
|
|
}
|
2015-03-10 00:11:57 -04:00
|
|
|
}(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-19 16:05:59 -05:00
|
|
|
func (c *adapter) Handle(conn net.Conn, ui packer.Ui) error {
|
2016-11-05 09:25:45 -04:00
|
|
|
log.Print("SSH proxy: accepted connection")
|
2015-03-10 00:11:57 -04:00
|
|
|
_, chans, reqs, err := ssh.NewServerConn(conn, c.config)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("failed to handshake")
|
|
|
|
}
|
|
|
|
|
|
|
|
// discard all global requests
|
|
|
|
go ssh.DiscardRequests(reqs)
|
|
|
|
|
|
|
|
// Service the incoming NewChannels
|
|
|
|
for newChannel := range chans {
|
|
|
|
if newChannel.ChannelType() != "session" {
|
|
|
|
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-12-19 16:05:59 -05:00
|
|
|
go func(ch ssh.NewChannel) {
|
|
|
|
if err := c.handleSession(ch); err != nil {
|
|
|
|
c.ui.Error(err.Error())
|
|
|
|
}
|
|
|
|
}(newChannel)
|
2015-03-10 00:11:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *adapter) handleSession(newChannel ssh.NewChannel) error {
|
|
|
|
channel, requests, err := newChannel.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer channel.Close()
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
// Sessions have requests such as "pty-req", "shell", "env", and "exec".
|
|
|
|
// see RFC 4254, section 6
|
|
|
|
go func(in <-chan *ssh.Request) {
|
|
|
|
env := make([]envRequestPayload, 4)
|
|
|
|
for req := range in {
|
|
|
|
switch req.Type {
|
|
|
|
case "pty-req":
|
2016-08-24 13:53:39 -04:00
|
|
|
log.Println("ansible provisioner pty-req request")
|
2015-03-10 00:11:57 -04:00
|
|
|
// accept pty-req requests, but don't actually do anything. Necessary for OpenSSH and sudo.
|
|
|
|
req.Reply(true, nil)
|
|
|
|
|
|
|
|
case "env":
|
|
|
|
req, err := newEnvRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
c.ui.Error(err.Error())
|
2016-08-24 13:53:39 -04:00
|
|
|
req.Reply(false, nil)
|
2015-03-10 00:11:57 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
env = append(env, req.Payload)
|
2016-08-24 13:53:39 -04:00
|
|
|
log.Printf("new env request: %s", req.Payload)
|
2015-03-10 00:11:57 -04:00
|
|
|
req.Reply(true, nil)
|
2016-08-24 13:53:39 -04:00
|
|
|
case "exec":
|
2015-03-10 00:11:57 -04:00
|
|
|
req, err := newExecRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
c.ui.Error(err.Error())
|
2016-08-24 13:53:39 -04:00
|
|
|
req.Reply(false, nil)
|
2015-03-10 00:11:57 -04:00
|
|
|
close(done)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-24 13:53:39 -04:00
|
|
|
log.Printf("new exec request: %s", req.Payload)
|
2015-03-10 00:11:57 -04:00
|
|
|
|
2016-08-24 13:53:39 -04:00
|
|
|
if len(req.Payload) == 0 {
|
|
|
|
req.Reply(false, nil)
|
|
|
|
close(done)
|
|
|
|
return
|
|
|
|
}
|
2015-03-10 00:11:57 -04:00
|
|
|
|
2016-08-29 01:22:48 -04:00
|
|
|
go func(channel ssh.Channel) {
|
|
|
|
exit := c.exec(string(req.Payload), channel, channel, channel.Stderr())
|
2016-08-24 13:53:39 -04:00
|
|
|
|
|
|
|
exitStatus := make([]byte, 4)
|
2016-08-29 01:22:48 -04:00
|
|
|
binary.BigEndian.PutUint32(exitStatus, uint32(exit))
|
2016-08-24 13:53:39 -04:00
|
|
|
channel.SendRequest("exit-status", false, exitStatus)
|
|
|
|
close(done)
|
2016-08-29 01:22:48 -04:00
|
|
|
}(channel)
|
2016-08-24 13:53:39 -04:00
|
|
|
req.Reply(true, nil)
|
2015-03-10 00:11:57 -04:00
|
|
|
case "subsystem":
|
|
|
|
req, err := newSubsystemRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
c.ui.Error(err.Error())
|
2016-08-24 13:53:39 -04:00
|
|
|
req.Reply(false, nil)
|
2015-03-10 00:11:57 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-24 13:53:39 -04:00
|
|
|
log.Printf("new subsystem request: %s", req.Payload)
|
2015-03-10 00:11:57 -04:00
|
|
|
switch req.Payload {
|
|
|
|
case "sftp":
|
|
|
|
sftpCmd := c.sftpCmd
|
|
|
|
if len(sftpCmd) == 0 {
|
|
|
|
sftpCmd = "/usr/lib/sftp-server -e"
|
|
|
|
}
|
|
|
|
|
2016-11-05 09:25:45 -04:00
|
|
|
log.Print("starting sftp subsystem")
|
2015-03-10 00:11:57 -04:00
|
|
|
go func() {
|
2016-09-09 02:07:29 -04:00
|
|
|
_ = c.remoteExec(sftpCmd, channel, channel, channel.Stderr())
|
2015-03-10 00:11:57 -04:00
|
|
|
close(done)
|
|
|
|
}()
|
2016-09-09 02:07:29 -04:00
|
|
|
req.Reply(true, nil)
|
2015-03-10 00:11:57 -04:00
|
|
|
default:
|
2016-08-24 13:53:39 -04:00
|
|
|
c.ui.Error(fmt.Sprintf("unsupported subsystem requested: %s", req.Payload))
|
2015-03-10 00:11:57 -04:00
|
|
|
req.Reply(false, nil)
|
|
|
|
}
|
|
|
|
default:
|
2016-11-05 09:25:45 -04:00
|
|
|
log.Printf("rejecting %s request", req.Type)
|
2015-03-10 00:11:57 -04:00
|
|
|
req.Reply(false, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(requests)
|
|
|
|
|
|
|
|
<-done
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *adapter) Shutdown() {
|
|
|
|
c.l.Close()
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:22:48 -04:00
|
|
|
func (c *adapter) exec(command string, in io.Reader, out io.Writer, err io.Writer) int {
|
|
|
|
var exitStatus int
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(command, "scp ") && serveSCP(command[4:]):
|
2016-09-25 18:57:02 -04:00
|
|
|
err := c.scpExec(command[4:], in, out)
|
2016-08-29 01:22:48 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
exitStatus = 1
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
exitStatus = c.remoteExec(command, in, out, err)
|
|
|
|
}
|
|
|
|
return exitStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveSCP(args string) bool {
|
|
|
|
opts, _ := scpOptions(args)
|
|
|
|
return bytes.IndexAny(opts, "tf") >= 0
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:57:02 -04:00
|
|
|
func (c *adapter) scpExec(args string, in io.Reader, out io.Writer) error {
|
2016-08-29 01:22:48 -04:00
|
|
|
opts, rest := scpOptions(args)
|
|
|
|
|
2016-09-25 18:57:02 -04:00
|
|
|
// remove the quoting that ansible added to rest for shell safety.
|
|
|
|
shargs, err := shlex.Split(rest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rest = strings.Join(shargs, "")
|
|
|
|
|
2016-08-29 01:22:48 -04:00
|
|
|
if i := bytes.IndexByte(opts, 't'); i >= 0 {
|
|
|
|
return scpUploadSession(opts, rest, in, out, c.comm)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i := bytes.IndexByte(opts, 'f'); i >= 0 {
|
|
|
|
return scpDownloadSession(opts, rest, in, out, c.comm)
|
|
|
|
}
|
|
|
|
return errors.New("no scp mode specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *adapter) remoteExec(command string, in io.Reader, out io.Writer, err io.Writer) int {
|
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Stdin: in,
|
|
|
|
Stdout: out,
|
|
|
|
Stderr: err,
|
|
|
|
Command: command,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.comm.Start(cmd); err != nil {
|
|
|
|
c.ui.Error(err.Error())
|
|
|
|
return cmd.ExitStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Wait()
|
|
|
|
|
|
|
|
return cmd.ExitStatus
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:11:57 -04:00
|
|
|
type envRequest struct {
|
|
|
|
*ssh.Request
|
|
|
|
Payload envRequestPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
type envRequestPayload struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2016-08-24 13:53:39 -04:00
|
|
|
func (p envRequestPayload) String() string {
|
|
|
|
return fmt.Sprintf("%s=%s", p.Name, p.Value)
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:11:57 -04:00
|
|
|
func newEnvRequest(raw *ssh.Request) (*envRequest, error) {
|
|
|
|
r := new(envRequest)
|
|
|
|
r.Request = raw
|
|
|
|
|
|
|
|
if err := ssh.Unmarshal(raw.Payload, &r.Payload); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshString(buf io.Reader) (string, error) {
|
|
|
|
var size uint32
|
|
|
|
err := binary.Read(buf, binary.BigEndian, &size)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := make([]byte, size)
|
|
|
|
err = binary.Read(buf, binary.BigEndian, b)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type execRequest struct {
|
|
|
|
*ssh.Request
|
|
|
|
Payload execRequestPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
type execRequestPayload string
|
|
|
|
|
2016-08-24 13:53:39 -04:00
|
|
|
func (p execRequestPayload) String() string {
|
|
|
|
return string(p)
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:11:57 -04:00
|
|
|
func newExecRequest(raw *ssh.Request) (*execRequest, error) {
|
|
|
|
r := new(execRequest)
|
|
|
|
r.Request = raw
|
|
|
|
buf := bytes.NewReader(r.Request.Payload)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var payload string
|
|
|
|
if payload, err = sshString(buf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Payload = execRequestPayload(payload)
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type subsystemRequest struct {
|
|
|
|
*ssh.Request
|
|
|
|
Payload subsystemRequestPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
type subsystemRequestPayload string
|
|
|
|
|
2016-08-24 13:53:39 -04:00
|
|
|
func (p subsystemRequestPayload) String() string {
|
|
|
|
return string(p)
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:11:57 -04:00
|
|
|
func newSubsystemRequest(raw *ssh.Request) (*subsystemRequest, error) {
|
|
|
|
r := new(subsystemRequest)
|
|
|
|
r.Request = raw
|
|
|
|
buf := bytes.NewReader(r.Request.Payload)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var payload string
|
|
|
|
if payload, err = sshString(buf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Payload = subsystemRequestPayload(payload)
|
|
|
|
return r, nil
|
|
|
|
}
|