2018-04-07 20:04:10 +00:00
|
|
|
// hkexshd server
|
|
|
|
//
|
|
|
|
// Copyright (c) 2017-2018 Russell Magee
|
|
|
|
// Licensed under the terms of the MIT license (see LICENSE.mit in this
|
|
|
|
// distribution)
|
|
|
|
//
|
|
|
|
// golang implementation by Russ Magee (rmagee_at_gmail.com)
|
2018-01-06 15:30:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-09-06 04:58:55 +00:00
|
|
|
"bytes"
|
2018-09-06 20:50:56 +00:00
|
|
|
"encoding/binary"
|
2018-01-13 06:24:40 +00:00
|
|
|
"flag"
|
2018-01-06 15:30:56 +00:00
|
|
|
"fmt"
|
2018-01-19 02:57:37 +00:00
|
|
|
"io"
|
2018-01-21 23:46:40 +00:00
|
|
|
"io/ioutil"
|
2018-01-06 15:30:56 +00:00
|
|
|
"log"
|
2018-01-21 04:37:27 +00:00
|
|
|
"os"
|
2018-01-18 00:39:01 +00:00
|
|
|
"os/exec"
|
|
|
|
"os/user"
|
2018-08-25 06:22:07 +00:00
|
|
|
"path"
|
2018-05-05 06:25:26 +00:00
|
|
|
"runtime"
|
2018-07-14 20:15:58 +00:00
|
|
|
"strings"
|
2018-08-06 04:43:21 +00:00
|
|
|
"sync"
|
2018-01-18 00:39:01 +00:00
|
|
|
"syscall"
|
2018-01-09 02:27:01 +00:00
|
|
|
|
2018-06-27 21:58:57 +00:00
|
|
|
"blitter.com/go/goutmp"
|
2018-04-28 23:05:33 +00:00
|
|
|
hkexsh "blitter.com/go/hkexsh"
|
2018-07-05 05:06:07 +00:00
|
|
|
"blitter.com/go/hkexsh/hkexnet"
|
2018-01-19 02:57:37 +00:00
|
|
|
"github.com/kr/pty"
|
2018-01-06 15:30:56 +00:00
|
|
|
)
|
|
|
|
|
2018-01-21 04:37:27 +00:00
|
|
|
type cmdSpec struct {
|
|
|
|
op []byte
|
|
|
|
who []byte
|
|
|
|
cmd []byte
|
|
|
|
authCookie []byte
|
2018-01-18 04:36:53 +00:00
|
|
|
status int
|
2018-01-18 00:39:01 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 02:57:37 +00:00
|
|
|
/* -------------------------------------------------------------- */
|
2018-08-23 18:03:19 +00:00
|
|
|
// Perform a client->server copy
|
2018-09-06 20:50:56 +00:00
|
|
|
func runClientToServerCopyAs(who string, conn hkexnet.Conn, fpath string, chaffing bool) (err error, exitStatus uint32) {
|
2018-01-18 00:39:01 +00:00
|
|
|
u, _ := user.Lookup(who)
|
|
|
|
var uid, gid uint32
|
|
|
|
fmt.Sscanf(u.Uid, "%d", &uid)
|
|
|
|
fmt.Sscanf(u.Gid, "%d", &gid)
|
2018-08-23 18:03:19 +00:00
|
|
|
log.Println("uid:", uid, "gid:", gid)
|
|
|
|
|
|
|
|
// Need to clear server's env and set key vars of the
|
|
|
|
// target user. This isn't perfect (TERM doesn't seem to
|
|
|
|
// work 100%; ANSI/xterm colour isn't working even
|
|
|
|
// if we set "xterm" or "ansi" here; and line count
|
|
|
|
// reported by 'stty -a' defaults to 24 regardless
|
|
|
|
// of client shell window used to run client.
|
|
|
|
// Investigate -- rlm 2018-01-26)
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("HOME", u.HomeDir)
|
|
|
|
os.Setenv("TERM", "vt102") // TODO: server or client option?
|
2018-01-18 00:39:01 +00:00
|
|
|
|
2018-08-23 18:03:19 +00:00
|
|
|
var c *exec.Cmd
|
|
|
|
cmdName := "/bin/tar"
|
2018-08-25 06:22:07 +00:00
|
|
|
|
|
|
|
var destDir string
|
|
|
|
if path.IsAbs(fpath) {
|
|
|
|
destDir = fpath
|
|
|
|
} else {
|
|
|
|
destDir = path.Join(u.HomeDir, fpath)
|
|
|
|
}
|
2018-08-31 03:06:42 +00:00
|
|
|
|
2018-09-06 23:37:17 +00:00
|
|
|
cmdArgs := []string{"-xz", "-C", destDir}
|
2018-08-25 06:22:07 +00:00
|
|
|
|
2018-08-23 18:03:19 +00:00
|
|
|
// NOTE the lack of quotes around --xform option's sed expression.
|
|
|
|
// When args are passed in exec() format, no quoting is required
|
|
|
|
// (as this isn't input from a shell) (right? -rlm 20180823)
|
2018-08-31 03:06:42 +00:00
|
|
|
//cmdArgs := []string{"-x", "-C", destDir, `--xform=s#.*/\(.*\)#\1#`}
|
2018-08-23 18:03:19 +00:00
|
|
|
c = exec.Command(cmdName, cmdArgs...)
|
|
|
|
|
2018-08-25 06:22:07 +00:00
|
|
|
c.Dir = destDir
|
|
|
|
|
2018-08-23 18:03:19 +00:00
|
|
|
//If os.Clearenv() isn't called by server above these will be seen in the
|
|
|
|
//client's session env.
|
|
|
|
//c.Env = []string{"HOME=" + u.HomeDir, "SUDO_GID=", "SUDO_UID=", "SUDO_USER=", "SUDO_COMMAND=", "MAIL=", "LOGNAME="+who}
|
2018-08-25 06:22:07 +00:00
|
|
|
//c.Dir = u.HomeDir
|
2018-01-18 00:39:01 +00:00
|
|
|
c.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
c.SysProcAttr.Credential = &syscall.Credential{Uid: uid, Gid: gid}
|
2018-01-18 05:27:00 +00:00
|
|
|
c.Stdin = conn
|
2018-08-25 06:22:07 +00:00
|
|
|
c.Stdout = os.Stdout
|
|
|
|
c.Stderr = os.Stderr
|
2018-08-25 01:50:45 +00:00
|
|
|
|
2018-08-25 06:22:07 +00:00
|
|
|
if chaffing {
|
|
|
|
conn.EnableChaff()
|
|
|
|
}
|
|
|
|
defer conn.DisableChaff()
|
|
|
|
defer conn.ShutdownChaff()
|
2018-01-18 05:27:00 +00:00
|
|
|
|
2018-08-23 18:03:19 +00:00
|
|
|
// Start the command (no pty)
|
|
|
|
log.Printf("[%v %v]\n", cmdName, cmdArgs)
|
|
|
|
err = c.Start() // returns immediately
|
2018-01-19 02:57:37 +00:00
|
|
|
if err != nil {
|
2018-08-23 18:03:19 +00:00
|
|
|
log.Printf("Command finished with error: %v", err)
|
2018-09-06 20:50:56 +00:00
|
|
|
return err, hkexnet.CSEExecFail // !?
|
2018-08-23 18:03:19 +00:00
|
|
|
} else {
|
|
|
|
if err := c.Wait(); err != nil {
|
2018-09-06 03:36:32 +00:00
|
|
|
//fmt.Println("*** c.Wait() done ***")
|
2018-08-23 18:03:19 +00:00
|
|
|
if exiterr, ok := err.(*exec.ExitError); ok {
|
|
|
|
// The program has exited with an exit code != 0
|
|
|
|
|
|
|
|
// This works on both Unix and Windows. Although package
|
|
|
|
// syscall is generally platform dependent, WaitStatus is
|
|
|
|
// defined for both Unix and Windows and in both cases has
|
|
|
|
// an ExitStatus() method with the same signature.
|
|
|
|
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
|
2018-09-06 20:50:56 +00:00
|
|
|
exitStatus = uint32(status.ExitStatus())
|
2018-08-23 18:03:19 +00:00
|
|
|
log.Printf("Exit Status: %d", exitStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 05:24:16 +00:00
|
|
|
//fmt.Println("*** client->server cp finished ***")
|
2018-08-25 01:50:45 +00:00
|
|
|
return
|
2018-01-19 02:57:37 +00:00
|
|
|
}
|
2018-08-23 18:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform a server->client copy
|
2018-09-06 20:50:56 +00:00
|
|
|
func runServerToClientCopyAs(who string, conn hkexnet.Conn, srcPath string, chaffing bool) (err error, exitStatus uint32) {
|
2018-08-23 18:03:19 +00:00
|
|
|
u, _ := user.Lookup(who)
|
|
|
|
var uid, gid uint32
|
|
|
|
fmt.Sscanf(u.Uid, "%d", &uid)
|
|
|
|
fmt.Sscanf(u.Gid, "%d", &gid)
|
|
|
|
log.Println("uid:", uid, "gid:", gid)
|
|
|
|
|
|
|
|
// Need to clear server's env and set key vars of the
|
|
|
|
// target user. This isn't perfect (TERM doesn't seem to
|
|
|
|
// work 100%; ANSI/xterm colour isn't working even
|
|
|
|
// if we set "xterm" or "ansi" here; and line count
|
|
|
|
// reported by 'stty -a' defaults to 24 regardless
|
|
|
|
// of client shell window used to run client.
|
|
|
|
// Investigate -- rlm 2018-01-26)
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("HOME", u.HomeDir)
|
|
|
|
os.Setenv("TERM", "vt102") // TODO: server or client option?
|
2018-01-19 02:57:37 +00:00
|
|
|
|
2018-08-23 18:03:19 +00:00
|
|
|
var c *exec.Cmd
|
|
|
|
cmdName := "/bin/tar"
|
2018-08-31 03:16:55 +00:00
|
|
|
if !path.IsAbs(srcPath) {
|
|
|
|
srcPath = fmt.Sprintf("%s%c%s", u.HomeDir, os.PathSeparator, srcPath)
|
|
|
|
}
|
|
|
|
|
2018-08-26 06:38:58 +00:00
|
|
|
srcDir, srcBase := path.Split(srcPath)
|
2018-09-06 23:37:17 +00:00
|
|
|
cmdArgs := []string{"-cz", "-C", srcDir, "-f", "-", srcBase}
|
2018-08-26 06:38:58 +00:00
|
|
|
|
2018-08-23 18:03:19 +00:00
|
|
|
c = exec.Command(cmdName, cmdArgs...)
|
2018-01-19 02:57:37 +00:00
|
|
|
|
2018-08-23 18:03:19 +00:00
|
|
|
//If os.Clearenv() isn't called by server above these will be seen in the
|
|
|
|
//client's session env.
|
|
|
|
//c.Env = []string{"HOME=" + u.HomeDir, "SUDO_GID=", "SUDO_UID=", "SUDO_USER=", "SUDO_COMMAND=", "MAIL=", "LOGNAME="+who}
|
|
|
|
c.Dir = u.HomeDir
|
|
|
|
c.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
c.SysProcAttr.Credential = &syscall.Credential{Uid: uid, Gid: gid}
|
|
|
|
c.Stdout = conn
|
2018-09-06 04:58:55 +00:00
|
|
|
// Stderr sinkholing (or buffering to something other than stdout)
|
|
|
|
// is important. Any extraneous output to tarpipe messes up remote
|
|
|
|
// side as it's expecting pure tar data.
|
2018-08-26 06:38:58 +00:00
|
|
|
// (For example, if user specifies abs paths, tar outputs
|
|
|
|
// "Removing leading '/' from path names")
|
2018-09-06 04:58:55 +00:00
|
|
|
stdErrBuffer := new(bytes.Buffer)
|
|
|
|
c.Stderr = stdErrBuffer
|
|
|
|
//c.Stderr = nil
|
2018-08-23 18:03:19 +00:00
|
|
|
|
2018-08-25 01:50:45 +00:00
|
|
|
if chaffing {
|
|
|
|
conn.EnableChaff()
|
|
|
|
}
|
|
|
|
//defer conn.Close()
|
|
|
|
defer conn.DisableChaff()
|
|
|
|
defer conn.ShutdownChaff()
|
|
|
|
|
2018-08-23 18:03:19 +00:00
|
|
|
// Start the command (no pty)
|
|
|
|
log.Printf("[%v %v]\n", cmdName, cmdArgs)
|
|
|
|
err = c.Start() // returns immediately
|
2018-01-18 00:39:01 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Command finished with error: %v", err)
|
2018-09-06 20:50:56 +00:00
|
|
|
return err, hkexnet.CSEExecFail // !?
|
2018-08-23 18:03:19 +00:00
|
|
|
} else {
|
|
|
|
if err := c.Wait(); err != nil {
|
2018-09-06 03:36:32 +00:00
|
|
|
//fmt.Println("*** c.Wait() done ***")
|
2018-08-23 18:03:19 +00:00
|
|
|
if exiterr, ok := err.(*exec.ExitError); ok {
|
|
|
|
// The program has exited with an exit code != 0
|
|
|
|
|
|
|
|
// This works on both Unix and Windows. Although package
|
|
|
|
// syscall is generally platform dependent, WaitStatus is
|
|
|
|
// defined for both Unix and Windows and in both cases has
|
|
|
|
// an ExitStatus() method with the same signature.
|
|
|
|
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
|
2018-09-06 20:50:56 +00:00
|
|
|
exitStatus = uint32(status.ExitStatus())
|
2018-09-06 04:58:55 +00:00
|
|
|
if len(stdErrBuffer.Bytes()) > 0 {
|
2018-09-06 23:37:17 +00:00
|
|
|
log.Print(stdErrBuffer)
|
2018-09-06 04:58:55 +00:00
|
|
|
}
|
2018-09-06 23:37:17 +00:00
|
|
|
log.Printf("Exit Status: %d", exitStatus)
|
2018-08-23 18:03:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 05:24:16 +00:00
|
|
|
//fmt.Println("*** server->client cp finished ***")
|
2018-08-25 01:50:45 +00:00
|
|
|
return
|
2018-01-18 00:39:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 01:31:54 +00:00
|
|
|
// Run a command (via default shell) as a specific user
|
|
|
|
//
|
|
|
|
// Uses ptys to support commands which expect a terminal.
|
2018-09-06 20:50:56 +00:00
|
|
|
func runShellAs(who string, cmd string, interactive bool, conn hkexnet.Conn, chaffing bool) (err error, exitStatus uint32) {
|
2018-08-06 04:43:21 +00:00
|
|
|
var wg sync.WaitGroup
|
2018-01-22 01:31:54 +00:00
|
|
|
u, _ := user.Lookup(who)
|
|
|
|
var uid, gid uint32
|
|
|
|
fmt.Sscanf(u.Uid, "%d", &uid)
|
|
|
|
fmt.Sscanf(u.Gid, "%d", &gid)
|
2018-02-17 02:46:29 +00:00
|
|
|
log.Println("uid:", uid, "gid:", gid)
|
2018-01-22 01:31:54 +00:00
|
|
|
|
2018-01-27 00:15:39 +00:00
|
|
|
// Need to clear server's env and set key vars of the
|
|
|
|
// target user. This isn't perfect (TERM doesn't seem to
|
|
|
|
// work 100%; ANSI/xterm colour isn't working even
|
|
|
|
// if we set "xterm" or "ansi" here; and line count
|
|
|
|
// reported by 'stty -a' defaults to 24 regardless
|
|
|
|
// of client shell window used to run client.
|
|
|
|
// Investigate -- rlm 2018-01-26)
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("HOME", u.HomeDir)
|
2018-09-06 04:58:55 +00:00
|
|
|
//os.Setenv("SHELL", "/bin/bash")
|
2018-01-27 00:15:39 +00:00
|
|
|
os.Setenv("TERM", "vt102") // TODO: server or client option?
|
|
|
|
|
2018-01-22 01:31:54 +00:00
|
|
|
var c *exec.Cmd
|
|
|
|
if interactive {
|
2018-01-27 00:15:39 +00:00
|
|
|
c = exec.Command("/bin/bash", "-i", "-l")
|
2018-01-22 01:31:54 +00:00
|
|
|
} else {
|
|
|
|
c = exec.Command("/bin/bash", "-c", cmd)
|
|
|
|
}
|
2018-01-27 00:15:39 +00:00
|
|
|
//If os.Clearenv() isn't called by server above these will be seen in the
|
|
|
|
//client's session env.
|
|
|
|
//c.Env = []string{"HOME=" + u.HomeDir, "SUDO_GID=", "SUDO_UID=", "SUDO_USER=", "SUDO_COMMAND=", "MAIL=", "LOGNAME="+who}
|
|
|
|
c.Dir = u.HomeDir
|
2018-01-22 01:31:54 +00:00
|
|
|
c.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
c.SysProcAttr.Credential = &syscall.Credential{Uid: uid, Gid: gid}
|
|
|
|
c.Stdin = conn
|
|
|
|
c.Stdout = conn
|
|
|
|
c.Stderr = conn
|
|
|
|
|
|
|
|
// Start the command with a pty.
|
|
|
|
ptmx, err := pty.Start(c) // returns immediately with ptmx file
|
|
|
|
if err != nil {
|
2018-09-06 20:50:56 +00:00
|
|
|
return err, hkexnet.CSEPtyExecFail
|
2018-01-22 01:31:54 +00:00
|
|
|
}
|
|
|
|
// Make sure to close the pty at the end.
|
|
|
|
defer func() { _ = ptmx.Close() }() // Best effort.
|
2018-04-29 02:28:37 +00:00
|
|
|
|
2018-03-27 04:58:42 +00:00
|
|
|
log.Printf("[%s]\n", cmd)
|
2018-01-22 01:31:54 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Command finished with error: %v", err)
|
2018-06-29 23:54:20 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// Watch for term resizes
|
|
|
|
go func() {
|
|
|
|
for sz := range conn.WinCh {
|
|
|
|
log.Printf("[Setting term size to: %v %v]\n", sz.Rows, sz.Cols)
|
|
|
|
pty.Setsize(ptmx, &pty.Winsize{Rows: sz.Rows, Cols: sz.Cols})
|
|
|
|
}
|
2018-08-06 04:43:21 +00:00
|
|
|
fmt.Println("*** WinCh goroutine done ***")
|
2018-06-29 23:54:20 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Copy stdin to the pty.. (bgnd goroutine)
|
|
|
|
go func() {
|
|
|
|
_, e := io.Copy(ptmx, conn)
|
|
|
|
if e != nil {
|
2018-08-06 04:43:21 +00:00
|
|
|
log.Println("** stdin->pty ended **:", e.Error())
|
2018-09-06 04:58:55 +00:00
|
|
|
} else {
|
|
|
|
log.Println("*** stdin->pty goroutine done ***")
|
2018-06-29 23:54:20 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if chaffing {
|
|
|
|
conn.EnableChaff()
|
|
|
|
}
|
|
|
|
defer conn.DisableChaff()
|
|
|
|
defer conn.ShutdownChaff()
|
|
|
|
|
|
|
|
// ..and the pty to stdout.
|
2018-08-06 04:43:21 +00:00
|
|
|
// This may take some time exceeding that of the
|
|
|
|
// actual command's lifetime, so the c.Wait() below
|
|
|
|
// must synchronize with the completion of this goroutine
|
|
|
|
// to ensure all stdout data gets to the client before
|
|
|
|
// connection is closed.
|
|
|
|
wg.Add(1)
|
2018-06-29 23:54:20 +00:00
|
|
|
go func() {
|
2018-08-06 04:43:21 +00:00
|
|
|
defer wg.Done()
|
2018-06-29 23:54:20 +00:00
|
|
|
_, e := io.Copy(conn, ptmx)
|
|
|
|
if e != nil {
|
2018-08-06 04:43:21 +00:00
|
|
|
log.Println("** pty->stdout ended **:", e.Error())
|
2018-09-06 04:58:55 +00:00
|
|
|
} else {
|
|
|
|
// The above io.Copy() will exit when the command attached
|
|
|
|
// to the pty exits
|
|
|
|
log.Println("*** pty->stdout goroutine done ***")
|
2018-06-29 23:54:20 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := c.Wait(); err != nil {
|
2018-09-06 03:36:32 +00:00
|
|
|
//fmt.Println("*** c.Wait() done ***")
|
2018-06-29 23:54:20 +00:00
|
|
|
if exiterr, ok := err.(*exec.ExitError); ok {
|
|
|
|
// The program has exited with an exit code != 0
|
|
|
|
|
|
|
|
// This works on both Unix and Windows. Although package
|
|
|
|
// syscall is generally platform dependent, WaitStatus is
|
|
|
|
// defined for both Unix and Windows and in both cases has
|
|
|
|
// an ExitStatus() method with the same signature.
|
|
|
|
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
|
2018-09-06 20:50:56 +00:00
|
|
|
exitStatus = uint32(status.ExitStatus())
|
2018-06-29 23:54:20 +00:00
|
|
|
log.Printf("Exit Status: %d", exitStatus)
|
|
|
|
}
|
2018-09-06 18:40:13 +00:00
|
|
|
}
|
2018-09-06 20:50:56 +00:00
|
|
|
conn.SetStatus(exitStatus)
|
2018-06-29 23:54:20 +00:00
|
|
|
}
|
2018-08-06 04:43:21 +00:00
|
|
|
wg.Wait() // Wait on pty->stdout completion to client
|
2018-01-22 01:31:54 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-09 03:16:55 +00:00
|
|
|
// Demo of a simple server that listens and spawns goroutines for each
|
|
|
|
// connecting client. Note this code is identical to standard tcp
|
|
|
|
// server code, save for declaring 'hkex' rather than 'net'
|
|
|
|
// Listener and Conns. The KEx and encrypt/decrypt is done within the type.
|
|
|
|
// Compare to 'serverp.go' in this directory to see the equivalence.
|
2018-01-06 15:30:56 +00:00
|
|
|
func main() {
|
2018-05-13 01:41:39 +00:00
|
|
|
version := "0.1pre (NO WARRANTY)"
|
|
|
|
var vopt bool
|
2018-05-26 20:43:09 +00:00
|
|
|
var chaffEnabled bool
|
2018-05-07 00:41:09 +00:00
|
|
|
var chaffFreqMin uint
|
|
|
|
var chaffFreqMax uint
|
|
|
|
var chaffBytesMax uint
|
2018-01-21 23:46:40 +00:00
|
|
|
var dbg bool
|
2018-01-13 06:24:40 +00:00
|
|
|
var laddr string
|
|
|
|
|
2018-05-13 01:41:39 +00:00
|
|
|
flag.BoolVar(&vopt, "v", false, "show version")
|
2018-01-13 06:24:40 +00:00
|
|
|
flag.StringVar(&laddr, "l", ":2000", "interface[:port] to listen")
|
2018-07-19 05:32:49 +00:00
|
|
|
flag.BoolVar(&chaffEnabled, "e", true, "enabled chaff pkts")
|
|
|
|
flag.UintVar(&chaffFreqMin, "f", 100, "chaff pkt freq min (msecs)")
|
|
|
|
flag.UintVar(&chaffFreqMax, "F", 5000, "chaff pkt freq max (msecs)")
|
|
|
|
flag.UintVar(&chaffBytesMax, "B", 64, "chaff pkt size max (bytes)")
|
2018-01-21 23:46:40 +00:00
|
|
|
flag.BoolVar(&dbg, "d", false, "debug logging")
|
2018-01-13 06:24:40 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2018-05-13 01:41:39 +00:00
|
|
|
if vopt {
|
2018-05-26 20:43:09 +00:00
|
|
|
fmt.Printf("version v%s\n", version)
|
2018-05-13 01:41:39 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2018-06-28 02:28:03 +00:00
|
|
|
{
|
|
|
|
me, e := user.Current()
|
|
|
|
if e != nil || me.Uid != "0" {
|
|
|
|
log.Fatal("Must run as root.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-14 03:26:48 +00:00
|
|
|
if dbg {
|
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
} else {
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
2018-01-06 15:30:56 +00:00
|
|
|
// Listen on TCP port 2000 on all available unicast and
|
|
|
|
// anycast IP addresses of the local system.
|
2018-07-05 05:06:07 +00:00
|
|
|
l, err := hkexnet.Listen("tcp", laddr)
|
2018-01-06 15:30:56 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
2018-02-17 02:46:29 +00:00
|
|
|
log.Println("Serving on", laddr)
|
2018-01-06 15:30:56 +00:00
|
|
|
for {
|
|
|
|
// Wait for a connection.
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
2018-04-29 02:28:37 +00:00
|
|
|
log.Printf("Accept() got error(%v), hanging up.\n", err)
|
|
|
|
conn.Close()
|
2018-04-28 23:05:33 +00:00
|
|
|
//log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
log.Println("Accepted client")
|
2018-01-06 15:30:56 +00:00
|
|
|
|
2018-05-07 00:41:09 +00:00
|
|
|
// Set up chaffing to client
|
2018-05-26 20:43:09 +00:00
|
|
|
// Will only start when runShellAs() is called
|
|
|
|
// after stdin/stdout are hooked up
|
2018-06-27 03:14:43 +00:00
|
|
|
conn.SetupChaff(chaffFreqMin, chaffFreqMax, chaffBytesMax) // configure server->client chaffing
|
2018-05-07 00:41:09 +00:00
|
|
|
|
2018-04-28 23:05:33 +00:00
|
|
|
// Handle the connection in a new goroutine.
|
|
|
|
// The loop then returns to accepting, so that
|
|
|
|
// multiple connections may be served concurrently.
|
2018-07-05 05:06:07 +00:00
|
|
|
go func(hc hkexnet.Conn) (e error) {
|
2018-06-30 02:23:11 +00:00
|
|
|
defer hc.Close()
|
2018-01-21 04:37:27 +00:00
|
|
|
|
2018-04-28 23:05:33 +00:00
|
|
|
//We use io.ReadFull() here to guarantee we consume
|
|
|
|
//just the data we want for the cmdSpec, and no more.
|
|
|
|
//Otherwise data will be sitting in the channel that isn't
|
|
|
|
//passed down to the command handlers.
|
|
|
|
var rec cmdSpec
|
|
|
|
var len1, len2, len3, len4 uint32
|
2018-03-26 04:47:38 +00:00
|
|
|
|
2018-06-30 02:23:11 +00:00
|
|
|
n, err := fmt.Fscanf(hc, "%d %d %d %d\n", &len1, &len2, &len3, &len4)
|
2018-04-28 23:05:33 +00:00
|
|
|
log.Printf("cmdSpec read:%d %d %d %d\n", len1, len2, len3, len4)
|
2018-03-26 04:47:38 +00:00
|
|
|
|
2018-04-28 23:05:33 +00:00
|
|
|
if err != nil || n < 4 {
|
|
|
|
log.Println("[Bad cmdSpec fmt]")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//fmt.Printf(" lens:%d %d %d %d\n", len1, len2, len3, len4)
|
2018-01-21 04:37:27 +00:00
|
|
|
|
2018-04-28 23:05:33 +00:00
|
|
|
rec.op = make([]byte, len1, len1)
|
2018-06-30 02:23:11 +00:00
|
|
|
_, err = io.ReadFull(hc, rec.op)
|
2018-04-28 23:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("[Bad cmdSpec.op]")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rec.who = make([]byte, len2, len2)
|
2018-06-30 02:23:11 +00:00
|
|
|
_, err = io.ReadFull(hc, rec.who)
|
2018-04-28 23:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("[Bad cmdSpec.who]")
|
|
|
|
return err
|
|
|
|
}
|
2018-01-21 04:37:27 +00:00
|
|
|
|
2018-04-28 23:05:33 +00:00
|
|
|
rec.cmd = make([]byte, len3, len3)
|
2018-06-30 02:23:11 +00:00
|
|
|
_, err = io.ReadFull(hc, rec.cmd)
|
2018-04-28 23:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("[Bad cmdSpec.cmd]")
|
|
|
|
return err
|
|
|
|
}
|
2018-01-21 04:37:27 +00:00
|
|
|
|
2018-04-28 23:05:33 +00:00
|
|
|
rec.authCookie = make([]byte, len4, len4)
|
2018-06-30 02:23:11 +00:00
|
|
|
_, err = io.ReadFull(hc, rec.authCookie)
|
2018-04-28 23:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("[Bad cmdSpec.authCookie]")
|
|
|
|
return err
|
|
|
|
}
|
2018-03-26 02:58:04 +00:00
|
|
|
|
2018-04-28 23:05:33 +00:00
|
|
|
log.Printf("[cmdSpec: op:%c who:%s cmd:%s auth:****]\n",
|
|
|
|
rec.op[0], string(rec.who), string(rec.cmd))
|
2018-01-21 04:37:27 +00:00
|
|
|
|
2018-04-28 23:05:33 +00:00
|
|
|
valid, allowedCmds := hkexsh.AuthUser(string(rec.who), string(rec.authCookie), "/etc/hkexsh.passwd")
|
2018-09-06 20:50:56 +00:00
|
|
|
|
2018-05-05 06:25:26 +00:00
|
|
|
// Security scrub
|
|
|
|
for i := range rec.authCookie {
|
|
|
|
rec.authCookie[i] = 0
|
|
|
|
}
|
|
|
|
runtime.GC()
|
|
|
|
|
2018-09-06 23:23:57 +00:00
|
|
|
// Tell client if auth was valid
|
|
|
|
if valid {
|
|
|
|
hc.Write([]byte{1})
|
|
|
|
} else {
|
2018-04-28 23:05:33 +00:00
|
|
|
log.Println("Invalid user", string(rec.who))
|
2018-09-06 23:23:57 +00:00
|
|
|
hc.Write([]byte{0})
|
2018-04-28 23:05:33 +00:00
|
|
|
return
|
|
|
|
}
|
2018-09-06 23:23:57 +00:00
|
|
|
|
2018-04-28 23:05:33 +00:00
|
|
|
log.Printf("[allowedCmds:%s]\n", allowedCmds)
|
2018-01-22 06:02:08 +00:00
|
|
|
|
2018-04-28 23:05:33 +00:00
|
|
|
if rec.op[0] == 'c' {
|
|
|
|
// Non-interactive command
|
2018-06-30 02:23:11 +00:00
|
|
|
addr := hc.RemoteAddr()
|
2018-07-14 20:15:58 +00:00
|
|
|
//hname := goutmp.GetHost(addr.String())
|
|
|
|
hname := strings.Split(addr.String(), ":")[0]
|
2018-06-28 02:09:35 +00:00
|
|
|
|
2018-06-28 02:13:14 +00:00
|
|
|
log.Printf("[Running command for [%s@%s]]\n", rec.who, hname)
|
2018-06-30 02:23:11 +00:00
|
|
|
runErr, cmdStatus := runShellAs(string(rec.who), string(rec.cmd), false, hc, chaffEnabled)
|
2018-04-28 23:05:33 +00:00
|
|
|
// Returned hopefully via an EOF or exit/logout;
|
|
|
|
// Clear current op so user can enter next, or EOF
|
|
|
|
rec.op[0] = 0
|
2018-06-29 23:54:20 +00:00
|
|
|
if runErr != nil {
|
|
|
|
log.Printf("[Error spawning cmd for %s@%s]\n", rec.who, hname)
|
|
|
|
} else {
|
|
|
|
log.Printf("[Command completed for %s@%s, status %d]\n", rec.who, hname, cmdStatus)
|
2018-09-06 20:50:56 +00:00
|
|
|
hc.SetStatus(cmdStatus)
|
2018-06-29 23:54:20 +00:00
|
|
|
}
|
2018-04-28 23:05:33 +00:00
|
|
|
} else if rec.op[0] == 's' {
|
2018-06-28 02:09:35 +00:00
|
|
|
// Interactive session
|
2018-06-30 02:23:11 +00:00
|
|
|
addr := hc.RemoteAddr()
|
2018-07-14 20:15:58 +00:00
|
|
|
//hname := goutmp.GetHost(addr.String())
|
|
|
|
hname := strings.Split(addr.String(), ":")[0]
|
2018-06-28 02:13:14 +00:00
|
|
|
log.Printf("[Running shell for [%s@%s]]\n", rec.who, hname)
|
2018-06-28 00:32:26 +00:00
|
|
|
|
2018-06-28 02:09:35 +00:00
|
|
|
utmpx := goutmp.Put_utmp(string(rec.who), hname)
|
2018-06-27 21:58:57 +00:00
|
|
|
defer func() { goutmp.Unput_utmp(utmpx) }()
|
2018-06-28 02:09:35 +00:00
|
|
|
goutmp.Put_lastlog_entry("hkexsh", string(rec.who), hname)
|
2018-06-30 02:23:11 +00:00
|
|
|
runErr, cmdStatus := runShellAs(string(rec.who), string(rec.cmd), true, hc, chaffEnabled)
|
2018-04-28 23:05:33 +00:00
|
|
|
// Returned hopefully via an EOF or exit/logout;
|
|
|
|
// Clear current op so user can enter next, or EOF
|
|
|
|
rec.op[0] = 0
|
2018-06-29 23:54:20 +00:00
|
|
|
if runErr != nil {
|
|
|
|
log.Printf("[Error spawning shell for %s@%s]\n", rec.who, hname)
|
|
|
|
} else {
|
|
|
|
log.Printf("[Shell completed for %s@%s, status %d]\n", rec.who, hname, cmdStatus)
|
2018-09-06 20:50:56 +00:00
|
|
|
hc.SetStatus(cmdStatus)
|
2018-06-29 23:54:20 +00:00
|
|
|
}
|
2018-08-07 05:29:51 +00:00
|
|
|
} else if rec.op[0] == 'D' {
|
|
|
|
// File copy (destination) operation - client copy to server
|
|
|
|
log.Printf("[Client->Server copy]\n")
|
2018-08-23 18:03:19 +00:00
|
|
|
addr := hc.RemoteAddr()
|
|
|
|
hname := strings.Split(addr.String(), ":")[0]
|
|
|
|
log.Printf("[Running copy for [%s@%s]]\n", rec.who, hname)
|
|
|
|
runErr, cmdStatus := runClientToServerCopyAs(string(rec.who), hc, string(rec.cmd), chaffEnabled)
|
2018-08-25 01:50:45 +00:00
|
|
|
// Returned hopefully via an EOF or exit/logout;
|
|
|
|
// Clear current op so user can enter next, or EOF
|
|
|
|
rec.op[0] = 0
|
2018-08-23 18:03:19 +00:00
|
|
|
if runErr != nil {
|
2018-08-25 01:50:45 +00:00
|
|
|
log.Printf("[Error spawning cp for %s@%s]\n", rec.who, hname)
|
2018-08-23 18:03:19 +00:00
|
|
|
} else {
|
2018-08-25 01:50:45 +00:00
|
|
|
log.Printf("[Command completed for %s@%s, status %d]\n", rec.who, hname, cmdStatus)
|
2018-08-23 18:03:19 +00:00
|
|
|
}
|
2018-09-06 20:50:56 +00:00
|
|
|
hc.SetStatus(cmdStatus)
|
2018-08-07 05:29:51 +00:00
|
|
|
} else if rec.op[0] == 'S' {
|
|
|
|
// File copy (src) operation - server copy to client
|
|
|
|
log.Printf("[Server->Client copy]\n")
|
2018-08-23 18:03:19 +00:00
|
|
|
addr := hc.RemoteAddr()
|
|
|
|
hname := strings.Split(addr.String(), ":")[0]
|
|
|
|
log.Printf("[Running copy for [%s@%s]]\n", rec.who, hname)
|
|
|
|
runErr, cmdStatus := runServerToClientCopyAs(string(rec.who), hc, string(rec.cmd), chaffEnabled)
|
2018-09-06 07:16:44 +00:00
|
|
|
//fmt.Print("ServerToClient cmdStatus:", cmdStatus)
|
2018-08-25 01:50:45 +00:00
|
|
|
// Returned hopefully via an EOF or exit/logout;
|
|
|
|
// Clear current op so user can enter next, or EOF
|
|
|
|
rec.op[0] = 0
|
2018-08-23 18:03:19 +00:00
|
|
|
if runErr != nil {
|
2018-08-25 01:50:45 +00:00
|
|
|
log.Printf("[Error spawning cp for %s@%s]\n", rec.who, hname)
|
2018-08-23 18:03:19 +00:00
|
|
|
} else {
|
2018-08-25 01:50:45 +00:00
|
|
|
log.Printf("[Command completed for %s@%s, status %d]\n", rec.who, hname, cmdStatus)
|
2018-08-23 18:03:19 +00:00
|
|
|
}
|
2018-09-06 20:50:56 +00:00
|
|
|
hc.SetStatus(cmdStatus)
|
2018-09-05 05:24:16 +00:00
|
|
|
// Signal other end transfer is complete
|
2018-09-06 20:50:56 +00:00
|
|
|
s := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(s, cmdStatus)
|
|
|
|
hc.WritePacket(s, hkexnet.CSOExitStatus)
|
2018-09-05 05:24:16 +00:00
|
|
|
//fmt.Println("Waiting for EOF from other end.")
|
|
|
|
_, _ = hc.Read(nil /*ackByte*/)
|
|
|
|
//fmt.Println("Got remote end ack.")
|
2018-04-28 23:05:33 +00:00
|
|
|
} else {
|
|
|
|
log.Println("[Bad cmdSpec]")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}(conn)
|
|
|
|
} // Accept() success
|
2018-01-21 04:37:27 +00:00
|
|
|
} //endfor
|
2018-02-17 02:46:29 +00:00
|
|
|
log.Println("[Exiting]")
|
2018-01-06 15:30:56 +00:00
|
|
|
}
|