From 0f5ee87a768c05cbb6ef426ca9148ae1a9af2920 Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 27 Jun 2022 15:10:17 -0700 Subject: [PATCH] allow mshell to execute local commands --- main-mshell.go | 3 --- pkg/shexec/shexec.go | 37 +++++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/main-mshell.go b/main-mshell.go index 2b3e4116..9a942492 100644 --- a/main-mshell.go +++ b/main-mshell.go @@ -369,9 +369,6 @@ func handleClient() (int, error) { if opts.Debug { packet.GlobalDebug = true } - if opts.SSHHost == "" { - return 1, fmt.Errorf("when running in client mode '--ssh' option must be present") - } if opts.Command == "" { return 1, fmt.Errorf("no [command] specified. [command] follows '--' option (see usage)") } diff --git a/pkg/shexec/shexec.go b/pkg/shexec/shexec.go index b81cae1e..09fcf167 100644 --- a/pkg/shexec/shexec.go +++ b/pkg/shexec/shexec.go @@ -30,7 +30,7 @@ const MaxCols = 1024 const MaxFdNum = 1023 const FirstExtraFilesFdNum = 3 -const SSHRemoteCommand = ` +const ClientCommand = ` PATH=$PATH:~/.mshell; which mshell > /dev/null; if [[ "$?" -ne 0 ]] @@ -229,20 +229,26 @@ type ClientOpts struct { CommandStdinFdNum int } -func (opts *ClientOpts) MakeSSHCommandString() string { - var moreSSHOpts []string - if opts.SSHIdentity != "" { - identityOpt := fmt.Sprintf("-i %s", shellescape.Quote(opts.SSHIdentity)) - moreSSHOpts = append(moreSSHOpts, identityOpt) +func (opts *ClientOpts) MakeExecCmd() *exec.Cmd { + if opts.SSHHost == "" { + ecmd := exec.Command("bash", "-c", strings.TrimSpace(ClientCommand)) + return ecmd + } else { + var moreSSHOpts []string + if opts.SSHIdentity != "" { + identityOpt := fmt.Sprintf("-i %s", shellescape.Quote(opts.SSHIdentity)) + moreSSHOpts = append(moreSSHOpts, identityOpt) + } + if opts.SSHUser != "" { + userOpt := fmt.Sprintf("-l %s", shellescape.Quote(opts.SSHUser)) + moreSSHOpts = append(moreSSHOpts, userOpt) + } + remoteCommand := strings.TrimSpace(ClientCommand) + // note that SSHOptsStr is *not* escaped + sshCmd := fmt.Sprintf("ssh %s %s %s %s", strings.Join(moreSSHOpts, " "), opts.SSHOptsStr, shellescape.Quote(opts.SSHHost), shellescape.Quote(remoteCommand)) + ecmd := exec.Command("bash", "-c", sshCmd) + return ecmd } - if opts.SSHUser != "" { - userOpt := fmt.Sprintf("-l %s", shellescape.Quote(opts.SSHUser)) - moreSSHOpts = append(moreSSHOpts, userOpt) - } - remoteCommand := strings.TrimSpace(SSHRemoteCommand) - // note that SSHOptsStr is *not* escaped - sshCmd := fmt.Sprintf("ssh %s %s %s %s", strings.Join(moreSSHOpts, " "), opts.SSHOptsStr, shellescape.Quote(opts.SSHHost), shellescape.Quote(remoteCommand)) - return sshCmd } func (opts *ClientOpts) MakeRunPacket() (*packet.RunPacketType, error) { @@ -351,8 +357,7 @@ func RunClientSSHCommandAndWait(opts *ClientOpts) (*packet.CmdDonePacketType, er return nil, err } cmd := MakeShExec("") - sshCmdStr := opts.MakeSSHCommandString() - ecmd := exec.Command("bash", "-c", sshCmdStr) + ecmd := opts.MakeExecCmd() cmd.Cmd = ecmd inputWriter, err := ecmd.StdinPipe() if err != nil {