add single-from-server option to mshell, send message packets with ck back to server, report unknown packets back to server

This commit is contained in:
sawka 2022-09-06 12:57:54 -07:00
parent 082fb7a8b4
commit 4f4e12c00a
4 changed files with 40 additions and 11 deletions

View File

@ -153,7 +153,7 @@ func readFullRunPacket(packetParser *packet.PacketParser) (*packet.RunPacketType
return nil, fmt.Errorf("no run packet received") return nil, fmt.Errorf("no run packet received")
} }
func handleSingle() { func handleSingle(fromServer bool) {
packetParser := packet.MakePacketParser(os.Stdin) packetParser := packet.MakePacketParser(os.Stdin)
sender := packet.MakePacketSender(os.Stdout) sender := packet.MakePacketSender(os.Stdout)
defer func() { defer func() {
@ -175,6 +175,12 @@ func handleSingle() {
sender.SendErrorResponse(runPacket.ReqId, err) sender.SendErrorResponse(runPacket.ReqId, err)
return return
} }
if fromServer {
err = runPacket.CK.Validate("run packet")
if err != nil {
sender.SendErrorResponse(runPacket.ReqId, fmt.Errorf("run packets from server must have a CK: %v", err))
}
}
if runPacket.Detached { if runPacket.Detached {
cmd, startPk, err := shexec.RunCommandDetached(runPacket, sender) cmd, startPk, err := shexec.RunCommandDetached(runPacket, sender)
if err != nil { if err != nil {
@ -187,7 +193,7 @@ func handleSingle() {
cmd.DetachedWait(startPk) cmd.DetachedWait(startPk)
return return
} else { } else {
cmd, err := shexec.RunCommandSimple(runPacket, sender) cmd, err := shexec.RunCommandSimple(runPacket, sender, true)
if err != nil { if err != nil {
sender.SendErrorResponse(runPacket.ReqId, fmt.Errorf("error running command: %w", err)) sender.SendErrorResponse(runPacket.ReqId, fmt.Errorf("error running command: %w", err))
return return
@ -504,7 +510,10 @@ func main() {
os.Exit(rtnCode) os.Exit(rtnCode)
} }
} else if firstArg == "--single" { } else if firstArg == "--single" {
handleSingle() handleSingle(false)
return
} else if firstArg == "--single-from-server" {
handleSingle(true)
return return
} else if firstArg == "--server" { } else if firstArg == "--server" {
rtnCode, err := server.RunServer() rtnCode, err := server.RunServer()

View File

@ -412,8 +412,9 @@ func MakeRawPacket(val string) *RawPacketType {
} }
type MessagePacketType struct { type MessagePacketType struct {
Type string `json:"type"` Type string `json:"type"`
Message string `json:"message"` CK base.CommandKey `json:"ck,omitempty"`
Message string `json:"message"`
} }
func (*MessagePacketType) GetType() string { func (*MessagePacketType) GetType() string {
@ -813,7 +814,17 @@ func (DefaultUPR) UnknownPacket(pk PacketType) {
} else { } else {
fmt.Fprintf(os.Stderr, "[error] invalid packet received '%s'", AsExtType(pk)) fmt.Fprintf(os.Stderr, "[error] invalid packet received '%s'", AsExtType(pk))
} }
}
type MessageUPR struct {
CK base.CommandKey
Sender *PacketSender
}
func (upr MessageUPR) UnknownPacket(pk PacketType) {
msg := FmtMessagePacket("[error] invalid packet received %s", AsString(pk))
msg.CK = upr.CK
upr.Sender.SendPacket(msg)
} }
// todo: clean hanging entries in RunMap when in server mode // todo: clean hanging entries in RunMap when in server mode

View File

@ -161,7 +161,7 @@ func (m *MServer) runCommand(runPacket *packet.RunPacketType) {
m.Sender.SendErrorResponse(runPacket.ReqId, fmt.Errorf("server run packets require valid ck: %s", err)) m.Sender.SendErrorResponse(runPacket.ReqId, fmt.Errorf("server run packets require valid ck: %s", err))
return return
} }
ecmd, err := shexec.SSHOpts{}.MakeMShellSingleCmd() ecmd, err := shexec.SSHOpts{}.MakeMShellSingleCmd(true)
if err != nil { if err != nil {
m.Sender.SendErrorResponse(runPacket.ReqId, fmt.Errorf("server run packets require valid ck: %s", err)) m.Sender.SendErrorResponse(runPacket.ReqId, fmt.Errorf("server run packets require valid ck: %s", err))
return return

View File

@ -375,13 +375,18 @@ func (opts SSHOpts) MakeMShellServerCmd() (*exec.Cmd, error) {
return ecmd, nil return ecmd, nil
} }
func (opts SSHOpts) MakeMShellSingleCmd() (*exec.Cmd, error) { func (opts SSHOpts) MakeMShellSingleCmd(fromServer bool) (*exec.Cmd, error) {
if opts.SSHHost == "" { if opts.SSHHost == "" {
execFile, err := os.Executable() execFile, err := os.Executable()
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot find local mshell executable: %w", err) return nil, fmt.Errorf("cannot find local mshell executable: %w", err)
} }
ecmd := exec.Command(execFile, "--single") var ecmd *exec.Cmd
if fromServer {
ecmd = exec.Command(execFile, "--single-from-server")
} else {
ecmd = exec.Command(execFile, "--single")
}
return ecmd, nil return ecmd, nil
} }
return opts.MakeSSHExecCmd(ClientCommand), nil return opts.MakeSSHExecCmd(ClientCommand), nil
@ -657,7 +662,7 @@ func HasDupStdin(fds []packet.RemoteFd) bool {
func RunClientSSHCommandAndWait(runPacket *packet.RunPacketType, fdContext FdContext, sshOpts SSHOpts, upr packet.UnknownPacketReporter, debug bool) (*packet.CmdDonePacketType, error) { func RunClientSSHCommandAndWait(runPacket *packet.RunPacketType, fdContext FdContext, sshOpts SSHOpts, upr packet.UnknownPacketReporter, debug bool) (*packet.CmdDonePacketType, error) {
cmd := MakeShExec(runPacket.CK, upr) cmd := MakeShExec(runPacket.CK, upr)
ecmd, err := sshOpts.MakeMShellSingleCmd() ecmd, err := sshOpts.MakeMShellSingleCmd(false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -829,8 +834,12 @@ func getTermType(pk *packet.RunPacketType) string {
return termType return termType
} }
func RunCommandSimple(pk *packet.RunPacketType, sender *packet.PacketSender) (*ShExecType, error) { func RunCommandSimple(pk *packet.RunPacketType, sender *packet.PacketSender, fromServer bool) (*ShExecType, error) {
cmd := MakeShExec(pk.CK, nil) var upr packet.UnknownPacketReporter
if fromServer {
upr = packet.MessageUPR{CK: pk.CK, Sender: sender}
}
cmd := MakeShExec(pk.CK, upr)
if pk.UsePty { if pk.UsePty {
cmd.Cmd = exec.Command("bash", "-i", "-c", pk.Command) cmd.Cmd = exec.Command("bash", "-i", "-c", pk.Command)
} else { } else {