This file is indexed.

/usr/share/gocode/src/github.com/dylanmei/winrmtest/wsman.go is in golang-github-dylanmei-winrmtest-dev 0.0~git20151226.0256178-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package winrmtest

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"net/http"
	"strconv"
	"strings"

	"github.com/masterzen/winrm/soap"
	"github.com/masterzen/xmlpath"
	"github.com/satori/go.uuid"
)

type wsman struct {
	commands     []*command
	identitySeed int
}

type command struct {
	id      string
	matcher MatcherFunc
	handler CommandFunc
}

func (w *wsman) HandleCommand(m MatcherFunc, f CommandFunc) string {
	id := uuid.NewV4().String()
	w.commands = append(w.commands, &command{
		id:      id,
		matcher: m,
		handler: f,
	})

	return id
}

func (w *wsman) CommandByText(cmd string) *command {
	for _, c := range w.commands {
		if c.matcher(cmd) {
			return c
		}
	}
	return nil
}

func (w *wsman) CommandByID(id string) *command {
	for _, c := range w.commands {
		if c.id == id {
			return c
		}
	}
	return nil
}

func (w *wsman) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	rw.Header().Add("Content-Type", "application/soap+xml")

	defer r.Body.Close()
	env, err := xmlpath.Parse(r.Body)

	if err != nil {
		return
	}

	action := readAction(env)
	switch {
	case strings.HasSuffix(action, "transfer/Create"):
		// create a new shell

		rw.Write([]byte(`
			<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
				<rsp:ShellId>123</rsp:ShellId>
			</env:Envelope>`))

	case strings.HasSuffix(action, "shell/Command"):
		// execute on behalf of the client
		text := readCommand(env)
		cmd := w.CommandByText(text)

		if cmd == nil {
			fmt.Printf("I don't know this command: Command=%s\n", text)
			rw.WriteHeader(http.StatusInternalServerError)
			return
		}

		rw.Write([]byte(fmt.Sprintf(`
			<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
				<rsp:CommandId>%s</rsp:CommandId>
			</env:Envelope>`, cmd.id)))

	case strings.HasSuffix(action, "shell/Receive"):
		// client ready to receive the results

		id := readCommandIDFromDesiredStream(env)
		cmd := w.CommandByID(id)

		if cmd == nil {
			fmt.Printf("I don't know this command: CommandId=%s\n", id)
			rw.WriteHeader(http.StatusInternalServerError)
			return
		}

		stdout := new(bytes.Buffer)
		stderr := new(bytes.Buffer)
		result := cmd.handler(stdout, stderr)
		content := base64.StdEncoding.EncodeToString(stdout.Bytes())

		rw.Write([]byte(fmt.Sprintf(`
			<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
				<rsp:ReceiveResponse>
					<rsp:Stream Name="stdout" CommandId="%s">%s</rsp:Stream>
					<rsp:Stream Name="stdout" CommandId="%s" End="true"></rsp:Stream>
					<rsp:Stream Name="stderr" CommandId="%s" End="true"></rsp:Stream>
					<rsp:CommandState State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done">
						<rsp:ExitCode>%d</rsp:ExitCode>
					</rsp:CommandState>
				</rsp:ReceiveResponse>
			</env:Envelope>`, id, content, id, id, result)))

	case strings.HasSuffix(action, "shell/Signal"):
		// end of the shell command
		rw.WriteHeader(http.StatusOK)
	case strings.HasSuffix(action, "transfer/Delete"):
		// end of the session
		rw.WriteHeader(http.StatusOK)
	default:
		fmt.Printf("I don't know this action: %s\n", action)
		rw.WriteHeader(http.StatusInternalServerError)
	}
}

func readAction(env *xmlpath.Node) string {
	xpath, err := xmlpath.CompileWithNamespace(
		"//a:Action", soap.GetAllNamespaces())

	if err != nil {
		return ""
	}

	action, _ := xpath.String(env)
	return action
}

func readCommand(env *xmlpath.Node) string {
	xpath, err := xmlpath.CompileWithNamespace(
		"//rsp:Command", soap.GetAllNamespaces())

	if err != nil {
		return ""
	}

	command, _ := xpath.String(env)
	if unquoted, err := strconv.Unquote(command); err == nil {
		return unquoted
	}
	return command
}

func readCommandIDFromDesiredStream(env *xmlpath.Node) string {
	xpath, err := xmlpath.CompileWithNamespace(
		"//rsp:DesiredStream/@CommandId", soap.GetAllNamespaces())

	if err != nil {
		return ""
	}

	id, _ := xpath.String(env)
	return id
}