2019-07-18 21:29:16 +00:00
|
|
|
//+build !windows
|
|
|
|
|
|
|
|
package sshserver
|
|
|
|
|
|
|
|
import (
|
2019-08-22 16:36:21 +00:00
|
|
|
"errors"
|
2019-07-18 21:29:16 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/user"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
2019-08-28 15:48:30 +00:00
|
|
|
"time"
|
2019-07-18 21:29:16 +00:00
|
|
|
"unsafe"
|
|
|
|
|
2019-08-26 20:25:24 +00:00
|
|
|
"github.com/cloudflare/cloudflared/sshlog"
|
|
|
|
|
2019-07-18 21:29:16 +00:00
|
|
|
"github.com/creack/pty"
|
|
|
|
"github.com/gliderlabs/ssh"
|
2019-08-26 20:25:24 +00:00
|
|
|
"github.com/google/uuid"
|
2019-07-18 21:29:16 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SSHServer struct {
|
|
|
|
ssh.Server
|
2019-08-22 16:36:21 +00:00
|
|
|
logger *logrus.Logger
|
|
|
|
shutdownC chan struct{}
|
|
|
|
caCert ssh.PublicKey
|
|
|
|
getUserFunc func(string) (*User, error)
|
2019-08-26 20:25:24 +00:00
|
|
|
logManager sshlog.Manager
|
2019-07-18 21:29:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 20:25:24 +00:00
|
|
|
func New(logManager sshlog.Manager, logger *logrus.Logger, address string, shutdownC chan struct{}, idleTimeout, maxTimeout time.Duration) (*SSHServer, error) {
|
2019-07-18 21:29:16 +00:00
|
|
|
currentUser, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if currentUser.Uid != "0" {
|
2019-08-19 18:51:59 +00:00
|
|
|
return nil, errors.New("cloudflared SSH server needs to run as root")
|
2019-07-18 21:29:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 16:36:21 +00:00
|
|
|
sshServer := SSHServer{
|
2019-08-28 15:48:30 +00:00
|
|
|
Server: ssh.Server{Addr: address, MaxTimeout: maxTimeout, IdleTimeout: idleTimeout},
|
2019-08-22 16:36:21 +00:00
|
|
|
logger: logger,
|
|
|
|
shutdownC: shutdownC,
|
|
|
|
getUserFunc: lookupUser,
|
2019-08-26 20:25:24 +00:00
|
|
|
logManager: logManager,
|
2019-08-22 16:36:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 18:51:59 +00:00
|
|
|
if err := sshServer.configureHostKeys(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-29 20:36:45 +00:00
|
|
|
sshServer.configureAuthentication()
|
2019-07-18 21:29:16 +00:00
|
|
|
return &sshServer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSHServer) Start() error {
|
|
|
|
s.logger.Infof("Starting SSH server at %s", s.Addr)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-s.shutdownC
|
|
|
|
if err := s.Close(); err != nil {
|
|
|
|
s.logger.WithError(err).Error("Cannot close SSH server")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.Handle(s.connectionHandler)
|
|
|
|
return s.ListenAndServe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSHServer) connectionHandler(session ssh.Session) {
|
2019-08-26 20:25:24 +00:00
|
|
|
sessionID, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
if _, err := io.WriteString(session, "Failed to generate session ID\n"); err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to generate session ID: Failed to write to SSH session")
|
|
|
|
}
|
|
|
|
s.CloseSession(session)
|
|
|
|
}
|
|
|
|
|
2019-07-18 21:29:16 +00:00
|
|
|
// Get uid and gid of user attempting to login
|
2019-08-22 16:36:21 +00:00
|
|
|
sshUser, ok := session.Context().Value("sshUser").(*User)
|
|
|
|
if !ok || sshUser == nil {
|
|
|
|
s.logger.Error("Error retrieving credentials from session")
|
2019-08-20 17:48:47 +00:00
|
|
|
s.CloseSession(session)
|
2019-07-18 21:29:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-22 16:36:21 +00:00
|
|
|
uidInt, err := stringToUint32(sshUser.Uid)
|
|
|
|
if err != nil {
|
2019-08-20 17:48:47 +00:00
|
|
|
s.logger.WithError(err).Error("Invalid user")
|
|
|
|
s.CloseSession(session)
|
2019-07-18 21:29:16 +00:00
|
|
|
return
|
|
|
|
}
|
2019-08-22 16:36:21 +00:00
|
|
|
gidInt, err := stringToUint32(sshUser.Gid)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.WithError(err).Error("Invalid user group")
|
|
|
|
s.CloseSession(session)
|
|
|
|
return
|
|
|
|
}
|
2019-07-18 21:29:16 +00:00
|
|
|
|
2019-08-29 17:40:21 +00:00
|
|
|
// Spawn shell under user
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
if session.RawCommand() != "" {
|
|
|
|
cmd = exec.Command(sshUser.Shell, "-c", session.RawCommand())
|
|
|
|
} else {
|
|
|
|
cmd = exec.Command(sshUser.Shell)
|
|
|
|
}
|
2019-08-29 20:36:45 +00:00
|
|
|
// Supplementary groups are not explicitly specified. They seem to be inherited by default.
|
2019-08-29 17:40:21 +00:00
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Credential: &syscall.Credential{Uid: uidInt, Gid: gidInt}, Setsid: true}
|
|
|
|
cmd.Env = append(cmd.Env, session.Environ()...)
|
2019-08-22 16:36:21 +00:00
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("USER=%s", sshUser.Username))
|
2019-08-20 17:48:47 +00:00
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", sshUser.HomeDir))
|
|
|
|
cmd.Dir = sshUser.HomeDir
|
2019-07-18 21:29:16 +00:00
|
|
|
|
2019-08-29 17:40:21 +00:00
|
|
|
ptyReq, winCh, isPty := session.Pty()
|
|
|
|
var shellInput io.WriteCloser
|
|
|
|
var shellOutput io.ReadCloser
|
|
|
|
|
|
|
|
if isPty {
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptyReq.Term))
|
|
|
|
shellInput, shellOutput, err = s.startPtySession(cmd, winCh)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to start pty session")
|
|
|
|
close(s.shutdownC)
|
|
|
|
return
|
2019-07-18 21:29:16 +00:00
|
|
|
}
|
2019-08-29 17:40:21 +00:00
|
|
|
} else {
|
|
|
|
shellInput, shellOutput, err = s.startNonPtySession(cmd)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to start non-pty session")
|
|
|
|
close(s.shutdownC)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 21:29:16 +00:00
|
|
|
|
2019-08-29 17:40:21 +00:00
|
|
|
// Write incoming commands to shell
|
2019-07-18 21:29:16 +00:00
|
|
|
go func() {
|
2019-08-29 17:40:21 +00:00
|
|
|
if _, err := io.Copy(shellInput, session); err != nil {
|
2019-07-18 21:29:16 +00:00
|
|
|
s.logger.WithError(err).Error("Failed to write incoming command to pty")
|
|
|
|
}
|
|
|
|
}()
|
2019-08-29 17:40:21 +00:00
|
|
|
|
2019-07-18 21:29:16 +00:00
|
|
|
pr, pw := io.Pipe()
|
2019-08-29 17:40:21 +00:00
|
|
|
defer pr.Close()
|
|
|
|
defer pw.Close()
|
|
|
|
|
2019-08-26 20:25:24 +00:00
|
|
|
logger, err := s.logManager.NewLogger(fmt.Sprintf("%s-session.log", sessionID), s.logger)
|
|
|
|
if err != nil {
|
|
|
|
if _, err := io.WriteString(session, "Failed to create log\n"); err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to create log: Failed to write to SSH session")
|
2019-07-18 21:29:16 +00:00
|
|
|
}
|
2019-08-26 20:25:24 +00:00
|
|
|
s.CloseSession(session)
|
|
|
|
}
|
|
|
|
defer logger.Close()
|
|
|
|
go func() {
|
|
|
|
io.Copy(logger, pr)
|
2019-07-18 21:29:16 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Write outgoing command output to both the command recorder, and remote user
|
|
|
|
mw := io.MultiWriter(pw, session)
|
2019-08-29 17:40:21 +00:00
|
|
|
if _, err := io.Copy(mw, shellOutput); err != nil {
|
2019-07-18 21:29:16 +00:00
|
|
|
s.logger.WithError(err).Error("Failed to write command output to user")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all resources associated with cmd to be released
|
|
|
|
// Returns error if shell exited with a non-zero status or received a signal
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
s.logger.WithError(err).Debug("Shell did not close correctly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 17:48:47 +00:00
|
|
|
func (s *SSHServer) CloseSession(session ssh.Session) {
|
|
|
|
if err := session.Exit(1); err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to close SSH session")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 17:40:21 +00:00
|
|
|
func (s *SSHServer) startNonPtySession(cmd *exec.Cmd) (io.WriteCloser, io.ReadCloser, error) {
|
|
|
|
in, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
out, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
cmd.Stderr = cmd.Stdout
|
|
|
|
if err = cmd.Start(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return in, out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSHServer) startPtySession(cmd *exec.Cmd, winCh <-chan ssh.Window) (io.WriteCloser, io.ReadCloser, error) {
|
|
|
|
tty, err := pty.Start(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle terminal window size changes
|
|
|
|
go func() {
|
|
|
|
for win := range winCh {
|
|
|
|
if errNo := setWinsize(tty, win.Width, win.Height); errNo != 0 {
|
|
|
|
s.logger.WithError(err).Error("Failed to set pty window size")
|
|
|
|
close(s.shutdownC)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return tty, tty, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 21:29:16 +00:00
|
|
|
// Sets PTY window size for terminal
|
|
|
|
func setWinsize(f *os.File, w, h int) syscall.Errno {
|
|
|
|
_, _, errNo := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(syscall.TIOCSWINSZ),
|
|
|
|
uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
|
|
|
|
return errNo
|
|
|
|
}
|
|
|
|
|
2019-08-20 17:48:47 +00:00
|
|
|
func stringToUint32(str string) (uint32, error) {
|
|
|
|
uid, err := strconv.ParseUint(str, 10, 32)
|
|
|
|
return uint32(uid), err
|
|
|
|
|
2019-07-18 21:29:16 +00:00
|
|
|
}
|