-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscp.go
200 lines (166 loc) · 5.14 KB
/
scp.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
package cmd
import (
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"syscall"
"github.com/inpher/sb/internal/commands"
"github.com/inpher/sb/internal/config"
"github.com/inpher/sb/internal/helpers"
"github.com/inpher/sb/internal/models"
"golang.org/x/term"
)
// Scp describes the help command
type Scp struct{}
func init() {
commands.RegisterCommand("scp", func() (c commands.Command, r models.Right, helper helpers.Helper, args map[string]commands.Argument) {
return new(Scp), models.HasAccess, helpers.Helper{
Header: "transfer a file from or to a distant host through sb",
Usage: "proxy [--get-script | --access HOST --scp-cmd CMD]",
Description: fmt.Sprintf(`This command allows the transfer of a file from or to a distant host through sb.
This requires the execution of script in complement of your usual scp command.
To get this running, execute the following commands:
%s scp --get-script > ~/.%sscp && chmod +x ~/.%sscp
alias %sscp='scp -O -S ~/.%sscp '
And voila, you're all set: just run the command '%sscp' as you would run 'scp'!`,
config.GetSBName(), config.GetSBName(), config.GetSBName(), config.GetSBName(), config.GetSBName(), config.GetSBName()),
}, map[string]commands.Argument{
"access": {
Required: false,
Description: "The IP, host or alias of the distant host",
},
"scp-cmd": {
Required: false,
Description: "The actual SCP command",
},
"get-script": {
Required: false,
Description: "Get the SCP script",
Type: commands.BOOL,
},
}
})
}
// Checks checks whether or not the user can execute this method
func (c *Scp) Checks(ct *commands.Context) error {
scpValidRegexp := regexp.MustCompile(`^scp (-r )?(-f|-t) .*`)
// In case an access was provided and verified
if ct.AI != nil {
// And we check we have a scp-cmd
if ct.FormattedArguments["scp-cmd"] == "" {
return fmt.Errorf("argument scp-cmd should be provided")
}
if !scpValidRegexp.MatchString(ct.FormattedArguments["scp-cmd"]) {
return fmt.Errorf("argument scp-cmd should be a scp internal formated command")
}
}
return nil
}
// Execute executes the command
func (c *Scp) Execute(ct *commands.Context) (repl models.ReplicationData, cmdError error, err error) {
// We have three cases:
// - user calls scp with no arguments, we need to display the help
// - user calls scp --get-script, we will return the script that makes it work
// - user calls scp --host host --scp-cmd, we execute the scp cmd
_, ok := ct.FormattedArguments["get-script"]
// case with no arguments at all
if !ok && ct.AI == nil {
// Let's just print the help
_, _, commandHlprs, cas, errCmd := commands.GetCommand("scp")
if err != nil {
return repl, cmdError, errCmd
}
commands.DisplayHelpers(commandHlprs, cas)
return
}
// Case with --get-script
if ok {
// We set stdout in raw mode to avoid \r\n transformations by ssh -t on client side
_, err = term.MakeRaw(syscall.Stdout)
if err != nil {
return
}
fmt.Printf("%s", helpers.GetScpScript(ct.User.User.Username, config.GetSBHostname(), config.GetSSHPort()))
return
}
// We should have everything to scp something to somewhere
// If we're here, rights are already checked
access, err := c.getUniqueAccessFromAvailableAccesses(ct.AI.Accesses, ct.BA.Host)
if err != nil {
fmt.Printf("Error: %s", err)
return
}
ct.Log.SetTargetAccess(access)
// Get ssh command path on the system
sshPath, err := exec.LookPath("ssh")
if err != nil {
fmt.Printf("Unable to find ssh on system: %s\n", err)
return
}
command := []string{
sshPath,
"-x",
"-oForwardAgent=no",
"-oPermitLocalCommand=no",
"-oClearAllForwardings=yes",
"-p", strconv.Itoa(access.Port),
"-l", access.User,
}
for _, privateKeyFile := range ct.AI.KeyFilepathes {
command = append(command, "-i", privateKeyFile)
}
command = append(command,
"--",
access.Host,
ct.FormattedArguments["scp-cmd"],
)
cmd := exec.Command(command[0], command[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
return
}
err = cmd.Wait()
if err != nil {
var ok bool
cmdError, ok = err.(*exec.ExitError)
if !ok {
return
}
err = nil
}
return
}
func (c *Scp) PostExecute(repl models.ReplicationData) (err error) {
return
}
func (c *Scp) Replicate(repl models.ReplicationData) (err error) {
return
}
func (c *Scp) getUniqueAccessFromAvailableAccesses(accesses []*models.Access, host string) (a *models.Access, err error) {
// We initialize with the first access returned
uniqueAccesses := make([]*models.Access, 0)
for i := 0; i < len(accesses); i++ {
unique := true
// In case of a wide prefix stored access, we replace the Host by the user input
if accesses[i].Host == "" {
accesses[i].Host = host
}
for _, ua := range uniqueAccesses {
if accesses[i].Equals(ua) {
unique = false
}
}
if unique {
uniqueAccesses = append(uniqueAccesses, accesses[i])
}
}
if len(uniqueAccesses) == 1 {
return uniqueAccesses[0], nil
}
return a, fmt.Errorf("can't request fine access from user input in SCP context")
}