-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
134 lines (106 loc) · 2.89 KB
/
info.go
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
package windurs
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"log"
"os"
"strings"
"github.com/masterzen/winrm/winrm"
)
type Info struct {
WinRM WinrmConfig
PowerShell PsSettings
}
type WinrmConfig struct {
MaxEnvelopeSizeKB int `xml:"MaxEnvelopeSizekb"`
MaxTimeoutMS int `xml:"MaxTimeoutms"`
Service WinrmServiceConfig
Winrs WinrmWinrsConfig
}
type WinrmServiceConfig struct {
MaxConnections int
MaxConcurrentOperations int
MaxConcurrentOperationsPerUser int
}
type WinrmWinrsConfig struct {
MaxMemoryPerShellMB int
MaxShellsPerUser int
MaxConcurrentUsers int
MaxProcessesPerShell int
}
type PsSettings struct {
Version string
ExecutionPolicy string
}
func fetchInfo(client *winrm.Client, user, pass string) (*Info, error) {
var err error
info := &Info{
WinRM: WinrmConfig{},
PowerShell: PsSettings{},
}
err = runPsVersion(client, &info.PowerShell)
if err != nil {
return info, err
}
err = runPsExecutionPolicy(client, &info.PowerShell)
if err != nil {
return info, err
}
err = runWinrmConfig(client, user, pass, &info.WinRM)
if err != nil {
return info, err
}
return info, nil
}
func runWinrmConfig(client *winrm.Client, user, pass string, config *WinrmConfig) error {
command := "winrm get winrm/config -format:xml"
stdout := bytes.NewBuffer(make([]byte, 0))
stderr := bytes.NewBuffer(make([]byte, 0))
_, err := runElevatedCmd(client, user, pass, stdout, stderr, command)
if os.Getenv("WINDURS_DEBUG") != "" && stderr.Len() > 0 {
log.Printf("STDERR returned: %s\n", stderr.String())
}
if err == nil && stdout.Len() > 0 {
err = xml.Unmarshal(stdout.Bytes(), config)
}
return err
}
func runPsVersion(client *winrm.Client, settings *PsSettings) error {
script := "$PSVersionTable.PSVersion | ConvertTo-Xml -NoTypeInformation -As String"
stdout, stderr, _, err := client.RunWithString("powershell -Command \""+script+"\"", "")
if err != nil {
return errors.New(fmt.Sprintf("Couldn't execute script %s: %v", script, err))
}
if stderr != "" {
if os.Getenv("WWNDURS_DEBUG") != "" {
log.Printf("STDERR returned: %s\n", stderr)
}
}
if stdout != "" {
doc := pslist{}
err := xml.Unmarshal([]byte(stdout), &doc)
if err != nil {
return errors.New(fmt.Sprintf("Couldn't parse results: %v", err))
}
settings.Version = doc.Objects[0].Value
}
return nil
}
func runPsExecutionPolicy(client *winrm.Client, settings *PsSettings) error {
script := "Get-ExecutionPolicy | Select-Object"
stdout, stderr, _, err := client.RunWithString("powershell -Command \""+script+"\"", "")
if err != nil {
return errors.New(fmt.Sprintf("Couldn't execute script %s: %v", script, err))
}
if stderr != "" {
if os.Getenv("WINRMCP_DEBUG") != "" {
log.Printf("STDERR returned: %s\n", stderr)
}
}
if stdout != "" {
settings.ExecutionPolicy = strings.Trim(stdout, "\n")
}
return nil
}