client->server and server->client file/dir copies minimally working

This commit is contained in:
Russ Magee 2018-08-24 23:22:07 -07:00
parent 7867f84b87
commit ca2b6efd9b
2 changed files with 41 additions and 17 deletions

View File

@ -105,7 +105,7 @@ func parseNonSwitchArgs(a []string, dp string) (user, host, port, path string, i
func doCopyMode(conn *hkexnet.Conn, remoteDest bool, files string, recurs bool, rec *cmdSpec) (err error, exitStatus int) {
if remoteDest {
fmt.Println("local files:", files, "remote filepath:", string(rec.cmd))
fmt.Fprintf(conn, "copyMode remoteDest ...\n")
//fmt.Fprintf(conn, "copyMode remoteDest ...\n")
var c *exec.Cmd
@ -114,15 +114,18 @@ func doCopyMode(conn *hkexnet.Conn, remoteDest bool, files string, recurs bool,
//os.Setenv("TERM", "vt102") // TODO: server or client option?
cmdName := "/bin/tar"
cmdArgs := []string{"-cz", "-f", "/dev/stdout", files}
cmdArgs := []string{"-c", "-f", "/dev/stdout", strings.TrimSpace(files)}
fmt.Printf("[%v %v]\n", cmdName, cmdArgs)
// 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)
//cmdArgs := []string{"-xvz", "-C", files, `--xform=s#.*/\(.*\)#\1#`}
c = exec.Command(cmdName, cmdArgs...)
c.Dir, _ = os.Getwd()
fmt.Println("[wd:", c.Dir, "]")
c.Stdout = conn
c.Stderr = os.Stderr
// Start the command (no pty)
err = c.Start() // returns immediately
if err != nil {
@ -147,7 +150,7 @@ func doCopyMode(conn *hkexnet.Conn, remoteDest bool, files string, recurs bool,
}
} else {
fmt.Println("remote filepath:", string(rec.cmd), "local files:", files)
fmt.Fprintf(conn, "copyMode localDest ...\n")
//fmt.Fprintf(conn, "copyMode localDest ...\n")
var c *exec.Cmd
//os.Clearenv()
@ -162,7 +165,7 @@ func doCopyMode(conn *hkexnet.Conn, remoteDest bool, files string, recurs bool,
// destPath := strings.Join({os.Getenv("PWD"),files}, os.PathSeparator)
//}
cmdArgs := []string{"-xvz", "-C", destPath}
cmdArgs := []string{"-x", "-C", destPath}
fmt.Printf("[%v %v]\n", cmdName, cmdArgs)
// NOTE the lack of quotes around --xform option's sed expression.
// When args are passed in exec() format, no quoting is required
@ -172,7 +175,7 @@ func doCopyMode(conn *hkexnet.Conn, remoteDest bool, files string, recurs bool,
c.Stdin = conn
c.Stdout = os.Stdout
c.Stderr = os.Stderr
// Start the command (no pty)
err = c.Start() // returns immediately
if err != nil {

View File

@ -16,6 +16,7 @@ import (
"os"
"os/exec"
"os/user"
"path"
"runtime"
"strings"
"sync"
@ -38,7 +39,7 @@ type cmdSpec struct {
/* -------------------------------------------------------------- */
// Perform a client->server copy
func runClientToServerCopyAs(who string, conn hkexnet.Conn, destPath string, chaffing bool) (err error, exitStatus int) {
func runClientToServerCopyAs(who string, conn hkexnet.Conn, fpath string, chaffing bool) (err error, exitStatus int) {
u, _ := user.Lookup(who)
var uid, gid uint32
fmt.Sscanf(u.Uid, "%d", &uid)
@ -58,28 +59,48 @@ func runClientToServerCopyAs(who string, conn hkexnet.Conn, destPath string, cha
var c *exec.Cmd
cmdName := "/bin/tar"
var destDir string
if path.IsAbs(fpath) {
destDir = fpath
} else {
destDir = path.Join(u.HomeDir, fpath)
}
//stat, pe := os.Stat(destDir)
//_ = stat
//if pe != nil {
// log.Fatal(pe)
// return pe, 252 // ?!
//}
//if stat.IsDir(destBase) {
cmdArgs := []string{"-x", "-C", destDir}
//} else {
// cmdArgs := []string{"-x",
// 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)
cmdArgs := []string{"-xvz", "-C", destPath}
//cmdArgs := []string{"-xvz", "-C", destPath, `--xform=s#.*/\(.*\)#\1#`}
c = exec.Command(cmdName, cmdArgs...)
c.Dir = destDir
//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.Dir = u.HomeDir
c.SysProcAttr = &syscall.SysProcAttr{}
c.SysProcAttr.Credential = &syscall.Credential{Uid: uid, Gid: gid}
c.Stdin = conn
//c.Stdout = conn
//c.Stderr = conn
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if chaffing {
conn.EnableChaff()
}
defer conn.DisableChaff()
defer conn.ShutdownChaff()
if chaffing {
conn.EnableChaff()
}
defer conn.DisableChaff()
defer conn.ShutdownChaff()
// Start the command (no pty)
log.Printf("[%v %v]\n", cmdName, cmdArgs)
@ -129,7 +150,7 @@ func runServerToClientCopyAs(who string, conn hkexnet.Conn, srcPath string, chaf
var c *exec.Cmd
cmdName := "/bin/tar"
cmdArgs := []string{"-cz", "-f", "-", srcPath}
cmdArgs := []string{"-c", "-f", "-", srcPath}
c = exec.Command(cmdName, cmdArgs...)
//If os.Clearenv() isn't called by server above these will be seen in the