forked from StanfordSNR/guardian-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sga-guard-bin.go
143 lines (122 loc) · 3.78 KB
/
sga-guard-bin.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net"
"os"
"runtime"
"strings"
guardianagent "github.com/StanfordSNR/guardian-agent"
flags "github.com/jessevdk/go-flags"
)
const debugClient = true
type SSHCommand struct {
UserHost string `required:"true" positional-arg-name:"[user@]hostname"`
}
type options struct {
guardianagent.CommonOptions
SSHProgram string `long:"ssh" description:"ssh program to run when setting up session" default:"ssh"`
PolicyConfig string `long:"policy" description:"Policy config file" default:"$HOME/.ssh/sga_policy"`
RemoteStubName string `long:"stub" description:"Remote stub executable path" default:"$SHELL -l -c \"exec sga-stub\""`
PromptType string `long:"prompt" description:"Type of prompt to use." choice:"DISPLAY" choice:"TERMINAL" default:"DISPLAY"`
SSHCommand SSHCommand `positional-args:"true" required:"true"`
}
func main() {
var opts options
var parser = flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
var sshOptions []string
parser.UnknownOptionHandler = func(option string, arg flags.SplitArgument, args []string) ([]string, error) {
val, isSet := arg.Value()
sshFlagsWithValues := "bcDEeFIiLmOopQRWw"
if isSet {
sshOptions = append(sshOptions, fmt.Sprintf("-%s", option), val)
} else if strings.Contains(sshFlagsWithValues, option) {
sshOptions = append(sshOptions, fmt.Sprintf("-%s", option), args[0])
args = args[1:]
} else {
sshOptions = append(sshOptions, fmt.Sprintf("-%s", option))
}
return args, nil
}
_, err := parser.Parse()
if opts.Version {
fmt.Println(guardianagent.Version)
os.Exit(0)
}
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok {
if flagsErr.Type == flags.ErrHelp {
fmt.Println(flagsErr.Message)
os.Exit(0)
}
fmt.Fprintln(os.Stderr, flagsErr.Message)
os.Exit(255)
}
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(255)
}
readableName := opts.SSHCommand.UserHost
if parser.FindOptionByShortName('l').IsSet() {
readableName = opts.Username + "@" + readableName
sshOptions = append(sshOptions, "-l", opts.Username)
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
if opts.Debug {
if opts.LogFile == "" {
log.SetOutput(os.Stderr)
} else {
f, err := os.OpenFile(opts.LogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open log file: %s", err)
os.Exit(255)
}
log.SetOutput(f)
}
} else {
log.SetOutput(ioutil.Discard)
}
opts.PolicyConfig = os.ExpandEnv(opts.PolicyConfig)
var ag *guardianagent.Agent
if opts.PromptType == "DISPLAY" {
if (runtime.GOOS == "linux") && (os.Getenv("DISPLAY") == "") {
fmt.Fprintln(os.Stderr, `DISPLAY environment variable is not set. Using terminal for user prompts.`)
opts.PromptType = "TERMINAL"
} else {
ag, err = guardianagent.NewGuardian(opts.PolicyConfig, guardianagent.Display)
}
}
if opts.PromptType == "TERMINAL" {
ag, err = guardianagent.NewGuardian(opts.PolicyConfig, guardianagent.Terminal)
}
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(255)
}
sshFwd := guardianagent.SSHFwd{
SSHProgram: opts.SSHProgram,
SSHArgs: sshOptions,
Host: opts.SSHCommand.UserHost,
RemoteReadableName: readableName,
RemoteStubName: opts.RemoteStubName,
}
fmt.Printf("Connecting to %s to set up forwarding...\n", readableName)
if err = sshFwd.SetupForwarding(); err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(255)
}
fmt.Printf("Forwarding to %s setup successfully. Waiting for incoming requests...\n", readableName)
var c net.Conn
for {
c, err = sshFwd.Accept()
if err != nil {
log.Printf("Error forwarding: %s", err)
os.Exit(255)
}
go func() {
if err = ag.HandleConnection(c); err != nil {
log.Printf("Error forwarding: %s", err)
}
}()
}
}