-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
253 lines (220 loc) · 6.64 KB
/
main.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"net"
"os"
"os/user"
"path/filepath"
flags "github.com/jessevdk/go-flags"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
// Options is representing the command line argunments
type Options struct {
Hostname string `short:"H" long:"hostname" description:"The hostname to connect to" required:"true"`
Port int `short:"P" long:"port" description:"SSH Port" default:"22"`
User string `short:"u" long:"username" description:"The Username to use" required:"true"`
PassAuth bool `short:"a" long:"passwordauth" description:"use password authentication"`
Password string `short:"p" long:"password" description:"password to use"`
IdentityFile string `short:"i" long:"identityfile" description:"key to use. Will ignored if passwordauth is true" default:"~/.ssh/id_rsa"`
Check string `short:"c" long:"check" description:"the check to use" required:"true"`
CheckArgs string `short:"o" long:"options" description:"the check options"`
PluginFolder string `short:"f" long:"pluginfolder" description:"the folder for the plugins to use" default:"./local_plugins"`
RemotePluginFolder string `short:"r" long:"remotepluginfolder" description:"the remote plugin folder" default:"checks"`
Sudo bool `short:"s" long:"sudo" description:"let the plugin run with sudo"`
}
var (
opts Options
parser = flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
authMethod []ssh.AuthMethod
)
func getCheckFile(pluginFolder string, checkPlugin string) (checkFile string, err error) {
checkFile, err = filepath.Abs(pluginFolder + "/" + checkPlugin)
if _, err := os.Stat(checkFile); os.IsNotExist(err) {
return "", fmt.Errorf("Check %s does not exist", checkFile)
}
return
}
func checkOpts(opts *Options) error {
if opts.PassAuth {
if opts.Password != "" {
return nil
}
return fmt.Errorf("You need to set a password for password auth")
}
if opts.IdentityFile == "" {
return fmt.Errorf("You have to set an identity file")
}
if opts.IdentityFile == "~/.ssh/id_rsa" {
user, _ := user.Current()
dir := user.HomeDir
opts.IdentityFile, _ = filepath.Abs(dir + "/.ssh/id_rsa")
}
if _, err := os.Stat(opts.IdentityFile); os.IsNotExist(err) {
return fmt.Errorf("Identity file %s is not existing", opts.IdentityFile)
}
if _, err := getCheckFile(opts.PluginFolder, opts.Check); err != nil {
return fmt.Errorf("Error getting check file: %s", err)
}
return nil
}
func getPassauth(password string) (authMethod []ssh.AuthMethod) {
return []ssh.AuthMethod{
ssh.Password(opts.Password),
}
}
func getCertAuth(identityFile string) (authMethod []ssh.AuthMethod, err error) {
var signers []ssh.Signer
if authsock := os.Getenv("SSH_AUTH_SOCK"); authsock != "" {
sock, err := net.Dial("unix", authsock)
if err != nil {
return authMethod, err
}
agent := agent.NewClient(sock)
signers, err = agent.Signers()
if err != nil {
return authMethod, err
}
} else {
key, err := ioutil.ReadFile(identityFile)
if err != nil {
return authMethod, fmt.Errorf("Unable to read private key: %v", err)
}
signer, err := ssh.ParsePrivateKey(key)
signers = append(signers, signer)
if err != nil {
return authMethod, fmt.Errorf("Unable to parse private key: %v", err)
}
}
return []ssh.AuthMethod{
ssh.PublicKeys(signers...),
}, nil
}
func remoteCheckSum(conn *ssh.Client, targetFolder string, targetFile string) (checksum [16]byte, err error) {
session, err := conn.NewSession()
if err != nil {
return checksum, fmt.Errorf("Can't create session %s", err)
}
cmd := fmt.Sprintf("md5sum %s/%s", targetFolder, targetFile)
output, err := session.Output(cmd)
if err != nil {
switch err.(type) {
case *ssh.ExitError:
err = fmt.Errorf("%s", output)
return
default:
err = fmt.Errorf("Unable to run command: %s", err)
return
}
}
decode, err := hex.DecodeString(string(output[:32]))
if err != nil {
return checksum, fmt.Errorf("Hex decode error %s", err)
}
copy(checksum[:], decode[:16])
return checksum, err
}
func copyCheck(conn *ssh.Client, targetFolder string, targetFile string, localFile string) (err error) {
content, err := ioutil.ReadFile(localFile)
if err != nil {
return fmt.Errorf("Can't read file %s", localFile)
}
localChecksum := md5.Sum(content)
remoteChecksum, _ := remoteCheckSum(conn, targetFolder, targetFile)
if localChecksum == remoteChecksum {
return
}
session, err := conn.NewSession()
if err != nil {
return fmt.Errorf("Cant create session %s", localFile)
}
go func() {
w, _ := session.StdinPipe()
defer w.Close()
fmt.Fprintln(w, "D0750", 0, targetFolder)
fmt.Fprintln(w, "C0740", len(content), targetFile)
w.Write(content)
fmt.Fprint(w, "\x00")
}()
if err := session.Run("scp -tr ./"); err != nil {
return fmt.Errorf("Failed to run: %s", err)
}
session.Close()
return
}
func main() {
_, err := parser.Parse()
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
fmt.Printf("%s", err)
os.Exit(0)
} else {
fmt.Printf("UNKNWON - Error parsing arguments: %s\n", err)
os.Exit(3)
}
}
err = checkOpts(&opts)
if err != nil {
fmt.Printf("UNKNOWN - Error in options: %s\n", err)
os.Exit(3)
}
if opts.PassAuth {
authMethod = getPassauth(opts.Password)
} else {
authMethod, err = getCertAuth(opts.IdentityFile)
if err != nil {
fmt.Printf("UNKNOWN - Error creating auth %s", err)
os.Exit(3)
}
}
config := &ssh.ClientConfig{
User: opts.User,
Auth: authMethod,
}
conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", opts.Hostname, opts.Port), config)
if err != nil {
fmt.Printf("UNKNWON - Unable to connect: %s", err)
os.Exit(3)
}
localCheck, _ := getCheckFile(opts.PluginFolder, opts.Check)
err = copyCheck(conn, opts.RemotePluginFolder, opts.Check, localCheck)
if err != nil {
fmt.Printf("UNKNOWN - Copy check error %s", err)
os.Exit(3)
}
defer conn.Close()
session, err := conn.NewSession()
if err != nil {
fmt.Printf("UNKNOWN - Unable to create session: %s", err)
os.Exit(3)
}
if err != nil {
fmt.Printf("UNKNOWN - Copy error: %s", err)
os.Exit(3)
}
args := ""
if opts.CheckArgs != "" {
args = " " + opts.CheckArgs
}
sudo := ""
if opts.Sudo {
sudo = "sudo -H "
}
output, err := session.Output(sudo + opts.RemotePluginFolder + "/" + opts.Check + args)
if err != nil {
switch e := err.(type) {
case *ssh.ExitError:
fmt.Printf("%s", output)
os.Exit(e.ExitStatus())
default:
fmt.Printf("UNKNOWN - Unable to run command: %s", err)
os.Exit(3)
}
}
fmt.Printf("%s", output)
defer session.Close()
os.Exit(0)
}