packer-cn/common/adapter/adapter.go

338 lines
7.3 KiB
Go
Raw Normal View History

2019-02-12 02:10:57 -05:00
package adapter
2015-03-10 00:11:57 -04:00
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
2015-03-10 00:11:57 -04:00
"net"
"strings"
2015-03-10 00:11:57 -04:00
"github.com/google/shlex"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
2015-03-10 00:11:57 -04:00
"golang.org/x/crypto/ssh"
)
// An adapter satisfies SSH requests (from an Ansible client) by delegating SSH
// exec and subsystem commands to a packer.Communicator.
2019-02-12 02:10:57 -05:00
type Adapter struct {
2015-03-10 00:11:57 -04:00
done <-chan struct{}
l net.Listener
config *ssh.ServerConfig
sftpCmd string
ui packer.Ui
comm packer.Communicator
}
2019-02-12 02:10:57 -05:00
func NewAdapter(done <-chan struct{}, l net.Listener, config *ssh.ServerConfig, sftpCmd string, ui packer.Ui, comm packer.Communicator) *Adapter {
return &Adapter{
2015-03-10 00:11:57 -04:00
done: done,
l: l,
config: config,
sftpCmd: sftpCmd,
ui: ui,
comm: comm,
}
}
2019-02-12 02:10:57 -05:00
func (c *Adapter) Serve() {
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))
continue
2015-03-10 00:11:57 -04:00
}
go func(conn net.Conn) {
if err := c.Handle(conn, c.ui); err != nil {
c.ui.Error(err.Error())
}
2015-03-10 00:11:57 -04:00
}(conn)
}
}
}
2019-02-12 02:10:57 -05:00
func (c *Adapter) Handle(conn net.Conn, ui packer.Ui) error {
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
}
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
}
2019-02-12 02:10:57 -05:00
func (c *Adapter) handleSession(newChannel ssh.NewChannel) error {
2015-03-10 00:11:57 -04:00
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":
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())
req.Reply(false, nil)
2015-03-10 00:11:57 -04:00
continue
}
env = append(env, req.Payload)
log.Printf("new env request: %s", req.Payload)
2015-03-10 00:11:57 -04:00
req.Reply(true, nil)
case "exec":
2015-03-10 00:11:57 -04:00
req, err := newExecRequest(req)
if err != nil {
c.ui.Error(err.Error())
req.Reply(false, nil)
2015-03-10 00:11:57 -04:00
close(done)
continue
}
log.Printf("new exec request: %s", req.Payload)
2015-03-10 00:11:57 -04:00
if len(req.Payload) == 0 {
req.Reply(false, nil)
close(done)
return
}
2015-03-10 00:11:57 -04:00
go func(channel ssh.Channel) {
exit := c.exec(string(req.Payload), channel, channel, channel.Stderr())
exitStatus := make([]byte, 4)
binary.BigEndian.PutUint32(exitStatus, uint32(exit))
channel.SendRequest("exit-status", false, exitStatus)
close(done)
}(channel)
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())
req.Reply(false, nil)
2015-03-10 00:11:57 -04:00
continue
}
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"
}
log.Print("starting sftp subsystem")
2015-03-10 00:11:57 -04:00
go func() {
_ = c.remoteExec(sftpCmd, channel, channel, channel.Stderr())
2015-03-10 00:11:57 -04:00
close(done)
}()
req.Reply(true, nil)
2015-03-10 00:11:57 -04:00
default:
c.ui.Error(fmt.Sprintf("unsupported subsystem requested: %s", req.Payload))
2015-03-10 00:11:57 -04:00
req.Reply(false, nil)
}
default:
log.Printf("rejecting %s request", req.Type)
2015-03-10 00:11:57 -04:00
req.Reply(false, nil)
}
}
}(requests)
<-done
return nil
}
2019-02-12 02:10:57 -05:00
func (c *Adapter) Shutdown() {
2015-03-10 00:11:57 -04:00
c.l.Close()
}
2019-02-12 02:10:57 -05: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:]):
err := c.scpExec(command[4:], in, out)
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
}
2019-02-12 02:10:57 -05:00
func (c *Adapter) scpExec(args string, in io.Reader, out io.Writer) error {
opts, rest := scpOptions(args)
// 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, "")
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")
}
2019-02-12 02:10:57 -05:00
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
}
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
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
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
}