2016-01-29 20:53:56 +01:00
|
|
|
package winrm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2020-06-25 14:41:09 +02:00
|
|
|
"sync"
|
2016-01-29 20:53:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type commandWriter struct {
|
|
|
|
*Command
|
2020-06-25 14:41:09 +02:00
|
|
|
mutex sync.Mutex
|
|
|
|
eof bool
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type commandReader struct {
|
|
|
|
*Command
|
|
|
|
write *io.PipeWriter
|
|
|
|
read *io.PipeReader
|
|
|
|
stream string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command represents a given command running on a Shell. This structure allows to get access
|
|
|
|
// to the various stdout, stderr and stdin pipes.
|
|
|
|
type Command struct {
|
2017-08-20 19:53:48 +02:00
|
|
|
client *Client
|
|
|
|
shell *Shell
|
|
|
|
id string
|
|
|
|
exitCode int
|
|
|
|
finished bool
|
|
|
|
err error
|
2016-01-29 20:53:56 +01:00
|
|
|
|
|
|
|
Stdin *commandWriter
|
|
|
|
Stdout *commandReader
|
|
|
|
Stderr *commandReader
|
|
|
|
|
|
|
|
done chan struct{}
|
|
|
|
cancel chan struct{}
|
|
|
|
}
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
func newCommand(shell *Shell, ids string) *Command {
|
|
|
|
command := &Command{
|
|
|
|
shell: shell,
|
|
|
|
client: shell.client,
|
|
|
|
id: ids,
|
|
|
|
exitCode: 0,
|
|
|
|
err: nil,
|
|
|
|
done: make(chan struct{}),
|
|
|
|
cancel: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
2016-01-29 20:53:56 +01:00
|
|
|
command.Stdout = newCommandReader("stdout", command)
|
2017-08-20 19:53:48 +02:00
|
|
|
command.Stdin = &commandWriter{
|
|
|
|
Command: command,
|
|
|
|
eof: false,
|
|
|
|
}
|
2016-01-29 20:53:56 +01:00
|
|
|
command.Stderr = newCommandReader("stderr", command)
|
|
|
|
|
|
|
|
go fetchOutput(command)
|
|
|
|
|
|
|
|
return command
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCommandReader(stream string, command *Command) *commandReader {
|
|
|
|
read, write := io.Pipe()
|
2017-08-20 19:53:48 +02:00
|
|
|
return &commandReader{
|
|
|
|
Command: command,
|
|
|
|
stream: stream,
|
|
|
|
write: write,
|
|
|
|
read: read,
|
|
|
|
}
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func fetchOutput(command *Command) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-command.cancel:
|
|
|
|
close(command.done)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
finished, err := command.slurpAllOutput()
|
|
|
|
if finished {
|
|
|
|
command.err = err
|
|
|
|
close(command.done)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
func (c *Command) check() error {
|
|
|
|
if c.id == "" {
|
2016-01-29 20:53:56 +01:00
|
|
|
return errors.New("Command has already been closed")
|
|
|
|
}
|
2017-08-20 19:53:48 +02:00
|
|
|
if c.shell == nil {
|
2016-01-29 20:53:56 +01:00
|
|
|
return errors.New("Command has no associated shell")
|
|
|
|
}
|
2017-08-20 19:53:48 +02:00
|
|
|
if c.client == nil {
|
2016-01-29 20:53:56 +01:00
|
|
|
return errors.New("Command has no associated client")
|
|
|
|
}
|
2017-08-20 19:53:48 +02:00
|
|
|
return nil
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close will terminate the running command
|
2017-08-20 19:53:48 +02:00
|
|
|
func (c *Command) Close() error {
|
|
|
|
if err := c.check(); err != nil {
|
2016-01-29 20:53:56 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
select { // close cancel channel if it's still open
|
2017-08-20 19:53:48 +02:00
|
|
|
case <-c.cancel:
|
2016-01-29 20:53:56 +01:00
|
|
|
default:
|
2017-08-20 19:53:48 +02:00
|
|
|
close(c.cancel)
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
request := NewSignalRequest(c.client.url, c.shell.id, c.id, &c.client.Parameters)
|
2016-01-29 20:53:56 +01:00
|
|
|
defer request.Free()
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
_, err := c.client.sendRequest(request)
|
2016-01-29 20:53:56 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
func (c *Command) slurpAllOutput() (bool, error) {
|
|
|
|
if err := c.check(); err != nil {
|
|
|
|
c.Stderr.write.CloseWithError(err)
|
|
|
|
c.Stdout.write.CloseWithError(err)
|
2016-01-29 20:53:56 +01:00
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
request := NewGetOutputRequest(c.client.url, c.shell.id, c.id, "stdout stderr", &c.client.Parameters)
|
2016-01-29 20:53:56 +01:00
|
|
|
defer request.Free()
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
response, err := c.client.sendRequest(request)
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "OperationTimeout") {
|
|
|
|
// Operation timeout because there was no command output
|
2017-08-20 19:53:48 +02:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if strings.Contains(err.Error(), "EOF") {
|
|
|
|
c.exitCode = 16001
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
c.Stderr.write.CloseWithError(err)
|
|
|
|
c.Stdout.write.CloseWithError(err)
|
2016-01-29 20:53:56 +01:00
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var exitCode int
|
|
|
|
var stdout, stderr bytes.Buffer
|
2017-08-20 19:53:48 +02:00
|
|
|
finished, exitCode, err := ParseSlurpOutputErrResponse(response, &stdout, &stderr)
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
2017-08-20 19:53:48 +02:00
|
|
|
c.Stderr.write.CloseWithError(err)
|
|
|
|
c.Stdout.write.CloseWithError(err)
|
2016-01-29 20:53:56 +01:00
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if stdout.Len() > 0 {
|
2017-08-20 19:53:48 +02:00
|
|
|
c.Stdout.write.Write(stdout.Bytes())
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
if stderr.Len() > 0 {
|
2017-08-20 19:53:48 +02:00
|
|
|
c.Stderr.write.Write(stderr.Bytes())
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
if finished {
|
2017-08-20 19:53:48 +02:00
|
|
|
c.exitCode = exitCode
|
|
|
|
c.Stderr.write.Close()
|
|
|
|
c.Stdout.write.Close()
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
return finished, nil
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2020-06-25 14:41:09 +02:00
|
|
|
func (c *Command) sendInput(data []byte, eof bool) error {
|
2017-08-20 19:53:48 +02:00
|
|
|
if err := c.check(); err != nil {
|
2016-01-29 20:53:56 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:41:09 +02:00
|
|
|
request := NewSendInputRequest(c.client.url, c.shell.id, c.id, data, eof, &c.client.Parameters)
|
2016-01-29 20:53:56 +01:00
|
|
|
defer request.Free()
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
_, err := c.client.sendRequest(request)
|
|
|
|
return err
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExitCode returns command exit code when it is finished. Before that the result is always 0.
|
2017-08-20 19:53:48 +02:00
|
|
|
func (c *Command) ExitCode() int {
|
|
|
|
return c.exitCode
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
// Wait function will block the current goroutine until the remote command terminates.
|
|
|
|
func (c *Command) Wait() {
|
2016-01-29 20:53:56 +01:00
|
|
|
// block until finished
|
2017-08-20 19:53:48 +02:00
|
|
|
<-c.done
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write data to this Pipe
|
2020-06-25 14:41:09 +02:00
|
|
|
// commandWriter implements io.Writer and io.Closer interface
|
2017-08-20 19:53:48 +02:00
|
|
|
func (w *commandWriter) Write(data []byte) (int, error) {
|
2020-06-25 14:41:09 +02:00
|
|
|
w.mutex.Lock()
|
|
|
|
defer w.mutex.Unlock()
|
|
|
|
|
|
|
|
if w.eof {
|
|
|
|
return 0, io.ErrClosedPipe
|
|
|
|
}
|
2017-08-20 19:53:48 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
written int
|
|
|
|
err error
|
|
|
|
)
|
2020-06-25 14:41:09 +02:00
|
|
|
origLen := len(data)
|
2016-01-29 20:53:56 +01:00
|
|
|
for len(data) > 0 {
|
|
|
|
// never send more data than our EnvelopeSize.
|
|
|
|
n := min(w.client.Parameters.EnvelopeSize-1000, len(data))
|
2020-06-25 14:41:09 +02:00
|
|
|
if err := w.sendInput(data[:n], false); err != nil {
|
2016-01-29 20:53:56 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
data = data[n:]
|
2017-08-20 19:53:48 +02:00
|
|
|
written += n
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
2017-08-20 19:53:48 +02:00
|
|
|
|
2020-06-25 14:41:09 +02:00
|
|
|
// signal that we couldn't write all data
|
|
|
|
if err == nil && written < origLen {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
}
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
return written, err
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2020-06-25 14:41:09 +02:00
|
|
|
// Write data to this Pipe and mark EOF
|
|
|
|
func (w *commandWriter) WriteClose(data []byte) (int, error) {
|
|
|
|
w.eof = true
|
|
|
|
return w.Write(data)
|
|
|
|
}
|
|
|
|
|
2016-01-29 20:53:56 +01:00
|
|
|
func min(a int, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
// Close method wrapper
|
|
|
|
// commandWriter implements io.Closer interface
|
2016-01-29 20:53:56 +01:00
|
|
|
func (w *commandWriter) Close() error {
|
2020-06-25 14:41:09 +02:00
|
|
|
w.mutex.Lock()
|
|
|
|
defer w.mutex.Unlock()
|
|
|
|
|
|
|
|
if w.eof {
|
|
|
|
return io.ErrClosedPipe
|
|
|
|
}
|
2016-01-29 20:53:56 +01:00
|
|
|
w.eof = true
|
2020-06-25 14:41:09 +02:00
|
|
|
return w.sendInput(nil, w.eof)
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read data from this Pipe
|
|
|
|
func (r *commandReader) Read(buf []byte) (int, error) {
|
|
|
|
n, err := r.read.Read(buf)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|