forked from wingedpig/loom
/
loom.go
568 lines (495 loc) · 13.3 KB
/
loom.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
Package loom implements a set of functions to interact with remote servers using SSH.
It is based on the Python fabric library.
*/
package loom
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
"sync"
"code.google.com/p/go.crypto/ssh"
)
// Config contains ssh and other configuration data needed for all the public functions in loom.
type Config struct {
// The user name used in SSH connections. If not specified, the current user is assumed.
User string
// Password for SSH connections. This is optional. If the user has an ~/.ssh/id_rsa keyfile,
// that will also be tried. In addition, other key files can be specified.
Password string
// The machine:port to connect to.
Host string
// The file names of additional key files to use for authentication (~/.ssh/id_rsa is defaulted).
// RSA (PKCS#1), DSA (OpenSSL), and ECDSA private keys are supported.
KeyFiles []string
// If true, send command output to stdout.
DisplayOutput bool
// If true, errors are fatal and will abort immediately.
AbortOnError bool
}
// parsekey is a private function that reads in a keyfile containing a private key and parses it.
func parsekey(file string) (ssh.Signer, error) {
var private ssh.Signer
privateBytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
private, err = ssh.ParsePrivateKey(privateBytes)
if err != nil {
return nil, err
}
return private, nil
}
// connect is a private function to set up the ssh connection. It is called at the beginning of every public
// function.
func (config *Config) connect() (*ssh.Session, error) {
sshconfig := &ssh.ClientConfig{
User: config.User,
}
if config.User == "" {
u, err := user.Current()
if err != nil {
return nil, err
}
sshconfig.User = u.Username
}
if config.Password != "" {
sshconfig.Auth = append(sshconfig.Auth, ssh.Password(config.Password))
}
var pkey ssh.Signer
var keyfile string
var err error
// Include additional key files
for _, keyfile = range config.KeyFiles {
pkey, err = parsekey(keyfile)
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return nil, err
}
sshconfig.Auth = append(sshconfig.Auth, ssh.PublicKeys(pkey))
}
// By default, we try to include ~/.ssh/id_rsa. It is not an error if this file
// doesn't exist.
keyfile = os.Getenv("HOME") + "/.ssh/id_rsa"
pkey, err = parsekey(keyfile)
if err == nil {
//fmt.Println("Adding default rsa key")
sshconfig.Auth = append(sshconfig.Auth, ssh.PublicKeys(pkey))
}
host := config.Host
if strings.Contains(host, ":") == false {
host = host + ":22"
}
client, err := ssh.Dial("tcp", host, sshconfig)
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return nil, err
}
session, err := client.NewSession()
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return nil, err
}
return session, err
}
// doRun is called by both Run() and Sudo() to execute a command.
func (config *Config) doRun(cmd string, sudo bool) (string, error) {
session, err := config.connect()
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return "", err
}
defer session.Close()
// Set up terminal modes
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
// Request pseudo terminal
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return "", err
}
if sudo == true {
cmd = fmt.Sprintf("/usr/bin/sudo bash <<CMD\nexport PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin\n%s\nCMD", cmd)
}
// TODO: use pipes instead of CombinedOutput so that we can show the output of commands more interactively, instead
// of now, which is after they're completely done executing.
output, err := config.executeCommand(session, cmd, sudo)
soutput := strings.Replace(string(output), "\r\n", "\n", -1)
if err != nil {
if config.DisplayOutput == true && len(soutput) > 0 {
fmt.Printf("%s", soutput)
}
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return "", err
}
session.SendRequest("close", false, nil)
if config.DisplayOutput == true {
fmt.Printf("%s", soutput)
}
return soutput, nil
}
type singleWriterReader struct {
b *bytes.Buffer
b2 *bytes.Buffer
teeReader io.Reader
mu sync.Mutex
usingRead bool
}
func newSingleWriterReader() singleWriterReader {
b := bytes.NewBuffer([]byte{})
b2 := bytes.NewBuffer([]byte{})
return singleWriterReader{b, b2, io.TeeReader(b, b2), sync.Mutex{}, false}
}
func (w *singleWriterReader) Write(p []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.b.Write(p)
}
func (w *singleWriterReader) Read(p []byte) (int, error) {
w.usingRead = true
w.mu.Lock()
defer w.mu.Unlock()
n, err := w.teeReader.Read(p)
return n, err
}
// Note: This should only be read once at the end, after everything has
// be written or read.
func (w *singleWriterReader) Bytes() []byte {
w.mu.Lock()
defer w.mu.Unlock()
return append(w.b2.Bytes(), w.b.Bytes()...)
}
func (config *Config) executeCommand(s *ssh.Session, cmd string, sudo bool) ([]byte, error) {
if s.Stdout != nil {
return nil, errors.New("ssh: Stdout already set")
}
if s.Stderr != nil {
return nil, errors.New("ssh: Stderr already set")
}
b := newSingleWriterReader()
s.Stdout = &b
s.Stderr = &b
done := make(chan bool)
if sudo {
stdInWriter, err := s.StdinPipe()
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return nil, err
}
go config.injectSudoPasswordIfNecessary(done, &b, stdInWriter)
}
err := s.Run(cmd)
close(done)
return b.Bytes(), err
}
func (config *Config) injectSudoPasswordIfNecessary(done <-chan bool, stdOutReader io.Reader, stdInWriter io.Writer) error {
matchFound := false
sudoMatcher := newSudoMatcher(config.User)
for matchFound == false {
select {
case <-done:
default:
bytesRead := make([]byte, sudoMatcher.totalMatchLength)
_, err := stdOutReader.Read(bytesRead)
if err == io.EOF {
continue
}
if err != nil {
return err
}
matchFound = sudoMatcher.Match(bytesRead)
if matchFound {
stdInWriter.Write([]byte(config.Password + "\n"))
break
}
}
}
return nil
}
type sudoMatcher struct {
currentIndexMatch int
currentPrompt string
stringToFind string
totalMatchLength int
}
func newSudoMatcher(user string) sudoMatcher {
stringToFind := fmt.Sprintf("[sudo] password for %s:", user)
totalMatchLength := len([]byte(stringToFind))
return sudoMatcher{0, "", stringToFind, totalMatchLength}
}
func (m *sudoMatcher) Match(additionalBytes []byte) bool {
readString := string(additionalBytes)
for _, runeVal := range readString {
if runeVal == rune(m.stringToFind[m.currentIndexMatch]) {
m.currentPrompt = m.currentPrompt + string(runeVal)
m.currentIndexMatch++
} else {
m.currentPrompt = ""
m.currentIndexMatch = 0
}
if len(m.currentPrompt) == m.totalMatchLength {
return true
}
}
return false
}
// Run takes a command and runs it on the remote host, using ssh.
func (config *Config) Run(cmd string) (string, error) {
if config.DisplayOutput == true {
fmt.Printf("run: %s\n", cmd)
}
return config.doRun(cmd, false)
}
// Sudo takes a command and runs it as root on the remote host, using sudo over ssh.
func (config *Config) Sudo(cmd string) (string, error) {
if config.DisplayOutput == true {
fmt.Printf("sudo: %s\n", cmd)
}
return config.doRun(cmd, true)
}
// Put copies one or more local files to the remote host, using scp. localfiles can
// contain wildcards, and remotefile can be either a directory or a file.
func (config *Config) Put(localfiles string, remotefile string) error {
files, err := filepath.Glob(localfiles)
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return err
}
if len(files) == 0 {
err = fmt.Errorf("No files match %s", localfiles)
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return err
}
for _, localfile := range files {
fmt.Printf("put: %s %s\n", localfile, remotefile)
contents, err := ioutil.ReadFile(localfile)
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return err
}
// get the local file mode bits
fi, err := os.Stat(localfile)
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return err
}
// the file mode bits are the 9 least significant bits of Mode()
mode := fi.Mode() & 1023
session, err := config.connect()
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return err
}
var stdoutBuf bytes.Buffer
var stderrBuf bytes.Buffer
session.Stdout = &stdoutBuf
session.Stderr = &stderrBuf
w, _ := session.StdinPipe()
_, lfile := filepath.Split(localfile)
err = session.Start("/usr/bin/scp -qrt " + remotefile)
if err != nil {
w.Close()
session.Close()
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return err
}
fmt.Fprintf(w, "C%04o %d %s\n", mode, len(contents), lfile /*remotefile*/)
w.Write(contents)
fmt.Fprint(w, "\x00")
w.Close()
err = session.Wait()
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
session.Close()
return err
}
if config.DisplayOutput == true {
stdout := stdoutBuf.String()
stderr := stderrBuf.String()
fmt.Printf("%s%s", stderr, stdout)
}
session.Close()
}
return nil
}
// PutString generates a new file on the remote host containing data. The file is created with mode 0644.
func (config *Config) PutString(data string, remotefile string) error {
if config.DisplayOutput == true {
fmt.Printf("putstring: %s\n", remotefile)
}
session, err := config.connect()
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return err
}
var stdoutBuf bytes.Buffer
var stderrBuf bytes.Buffer
session.Stdout = &stdoutBuf
session.Stderr = &stderrBuf
w, _ := session.StdinPipe()
_, rfile := filepath.Split(remotefile)
err = session.Start("/usr/bin/scp -qrt " + remotefile)
if err != nil {
w.Close()
session.Close()
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return err
}
fmt.Fprintf(w, "C0644 %d %s\n", len(data), rfile)
w.Write([]byte(data))
fmt.Fprint(w, "\x00")
w.Close()
err = session.Wait()
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
session.Close()
return err
}
if config.DisplayOutput == true {
stdout := stdoutBuf.String()
stderr := stderrBuf.String()
fmt.Printf("%s%s", stderr, stdout)
}
session.Close()
return nil
}
// Get copies the file from the remote host to the local host, using scp. Wildcards are not currently supported.
func (config *Config) Get(remotefile string, localfile string) error {
if config.DisplayOutput == true {
fmt.Printf("get: %s %s\n", remotefile, localfile)
}
session, err := config.connect()
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return err
}
defer session.Close()
// TODO: Handle wildcards in remotefile
var stdoutBuf bytes.Buffer
var stderrBuf bytes.Buffer
session.Stdout = &stdoutBuf
session.Stderr = &stderrBuf
w, _ := session.StdinPipe()
err = session.Start("/usr/bin/scp -qrf " + remotefile)
if err != nil {
w.Close()
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return err
}
// TODO: better error checking than just firing and forgetting these nulls.
fmt.Fprintf(w, "\x00")
fmt.Fprintf(w, "\x00")
fmt.Fprintf(w, "\x00")
fmt.Fprintf(w, "\x00")
fmt.Fprintf(w, "\x00")
fmt.Fprintf(w, "\x00")
err = session.Wait()
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return err
}
stdout := stdoutBuf.String()
//stderr := stderrBuf.String()
// first line of stdout contains file information
fields := strings.SplitN(stdout, "\n", 2)
mode, _ := strconv.ParseInt(fields[0][1:5], 8, 32)
// need to generate final local file name
// localfile could be a directory or a filename
// if it's a directory, we need to append the remotefile filename
// if it doesn't exist, we assume file
var lfile string
_, rfile := filepath.Split(remotefile)
l := len(localfile)
if localfile[l-1] == '/' {
localfile = localfile[:l-1]
}
fi, err := os.Stat(localfile)
if err != nil || fi.IsDir() == false {
lfile = localfile
} else if fi.IsDir() == true {
lfile = localfile + "/" + rfile
}
// there's a trailing 0 in the file that we need to nuke
l = len(fields[1])
err = ioutil.WriteFile(lfile, []byte(fields[1][:l-1]), os.FileMode(mode))
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return err
}
return nil
}
// Local executes a command on the local host.
func (config *Config) Local(cmd string) (string, error) {
if config.DisplayOutput == true {
fmt.Printf("local: %s\n", cmd)
}
var command *exec.Cmd
command = exec.Command("/bin/sh", "-c", cmd)
var out bytes.Buffer
command.Stdout = &out
err := command.Run()
if err != nil {
if config.AbortOnError == true {
log.Fatalf("%s", err)
}
return "", err
}
if config.DisplayOutput == true {
fmt.Printf("%s", out.String())
}
return out.String(), nil
}