-
Notifications
You must be signed in to change notification settings - Fork 0
/
winssh_etc.go
185 lines (165 loc) · 3.53 KB
/
winssh_etc.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
//go:build !windows
// +build !windows
package winssh
import (
"fmt"
"io"
"net"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
gl "github.com/gliderlabs/ssh"
"github.com/magiconair/properties"
)
// get authorized keys paths
func GetUserKeysPaths(ssh string, fns ...string) []string {
return append(fns[:],
path.Join(ssh, authorizedKeys),
path.Join("~", ".ssh", authorizedKeys),
path.Join(EtcDirs("dropbear"), authorizedKeys),
)
}
// get one key
func GetHostKey(ssh string) (pri string) {
for _, dir := range []string{
EtcDirs("ssh"),
EtcDirs("dropbear"),
ssh,
} {
for _, key := range []string{
"ssh_host_ecdsa_key",
"ssh_host_ed25519_key",
sshHostKey,
} {
pri = path.Join(dir, key)
ltf.Println(pri)
_, err := os.Stat(pri)
if err == nil {
return
}
}
}
return
}
// unix sock
func pipe(_ *session) string {
const (
agentTempDir = "auth-agent"
agentListenFile = "listener.sock"
)
dir, err := os.MkdirTemp("", agentTempDir)
if err != nil {
dir = os.TempDir()
}
return path.Join(dir, agentListenFile)
}
// close sock then rm parent dir
func doner(l net.Listener, s gl.Session) {
<-s.Context().Done()
p := l.Addr().String()
ltf.Println(p, "done")
l.Close()
dir := path.Dir(p)
if dir == os.TempDir() {
// only rm dir case its parent is /tmp
os.Remove(dir)
}
}
// SubsystemHandlers for agent
func SubsystemHandlerAgent(s gl.Session) {
l, err := NewAgentListener(s)
if err != nil {
return
}
defer l.Close()
go doner(l, s)
gl.ForwardAgentConnections(l, s)
}
// NewAgentListener sets up a temporary Unix socket that can be communicated
// to the session environment and used for forwarding connections.
func NewAgentListener(s gl.Session) (net.Listener, error) {
p := getPipe(s)
if p == "" {
return nil, fmt.Errorf("not found %s", SSH_AUTH_SOCK)
}
l, err := net.Listen("unix", p)
if err != nil {
return nil, err
}
return l, nil
}
func Env(s gl.Session, shell string) (e []string) {
e = s.Environ()
ra, ok := s.RemoteAddr().(*net.TCPAddr)
if ok {
la, ok := s.LocalAddr().(*net.TCPAddr)
if ok {
e = append(e,
fmt.Sprintf("%s=%s %d %d", "SSH_CLIENT", ra.IP, ra.Port, la.Port),
fmt.Sprintf("%s=%s %d %s %d", "SSH_CONNECTION", ra.IP, ra.Port, la.IP, la.Port),
)
}
}
e = append(e,
"LOGNAME="+s.User(),
)
return
}
// `ssh -p 2222 [email protected] command`
// `ssh -p 2222 [email protected] -T`
func NoPTY(s gl.Session) {
args, cmdLine := ShArgs(s)
e := Env(s, args[0])
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = Home(s)
cmd.Env = append(os.Environ(), e...)
stdout, err := cmd.StdoutPipe()
if err != nil {
letf.Println("unable to open stdout pipe", err)
return
}
cmd.Stderr = cmd.Stdout
stdin, err := cmd.StdinPipe()
if err != nil {
letf.Println("unable to open stdin pipe", err)
return
}
err = cmd.Start()
if err != nil {
letf.Println("could not start", cmdLine, err)
return
}
ppid := cmd.Process.Pid
ltf.Println(cmdLine, ppid)
go func() {
<-s.Context().Done()
stdout.Close()
}()
go io.Copy(stdin, s)
go io.Copy(s, stdout)
ltf.Println(cmdLine, "done", cmd.Wait())
}
// etc
func EtcDirs(dirs ...string) (s string) {
dirs = append([]string{"/etc"}, dirs...)
s = filepath.Join(dirs...)
os.MkdirAll(s, 0755)
return
}
func UserName() string {
return os.Getenv("USER")
}
func Banner(ss ...string) string {
ss = append(ss, runtime.GOOS)
p, err := properties.LoadFile("/etc/os-release", properties.UTF8)
if err == nil {
id := p.GetString("ID", "")
if id != "" {
ss = append(ss, id)
}
}
return strings.Join(ss, "_")
}