This repository was archived by the owner on Oct 2, 2022. It is now read-only.
generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler_session.go
More file actions
299 lines (283 loc) · 7.74 KB
/
handler_session.go
File metadata and controls
299 lines (283 loc) · 7.74 KB
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
package security
import (
"context"
"github.com/containerssh/log"
"github.com/containerssh/sshserver"
)
type sessionHandler struct {
config Config
backend sshserver.SessionChannelHandler
sshConnection *sshConnectionHandler
logger log.Logger
}
func (s *sessionHandler) OnClose() {
s.backend.OnClose()
}
func (s *sessionHandler) OnShutdown(shutdownContext context.Context) {
s.backend.OnShutdown(shutdownContext)
}
func (s *sessionHandler) OnUnsupportedChannelRequest(requestID uint64, requestType string, payload []byte) {
s.backend.OnUnsupportedChannelRequest(requestID, requestType, payload)
}
func (s *sessionHandler) OnFailedDecodeChannelRequest(
requestID uint64,
requestType string,
payload []byte,
reason error,
) {
s.backend.OnFailedDecodeChannelRequest(requestID, requestType, payload, reason)
}
func (s *sessionHandler) getPolicy(primary ExecutionPolicy) ExecutionPolicy {
if primary != ExecutionPolicyUnconfigured {
return primary
}
if s.config.DefaultMode != ExecutionPolicyUnconfigured {
return s.config.DefaultMode
}
return ExecutionPolicyEnable
}
func (s *sessionHandler) contains(items []string, item string) bool {
for _, searchItem := range items {
if searchItem == item {
return true
}
}
return false
}
func (s *sessionHandler) OnEnvRequest(requestID uint64, name string, value string) error {
mode := s.getPolicy(s.config.Env.Mode)
switch mode {
case ExecutionPolicyDisable:
err := log.UserMessage(
EEnvRejected,
"Environment variable setting rejected.",
"Setting an environment variable is rejected because it is disabled in the security settings.",
).Label("name", name)
s.logger.Debug(err)
return err
case ExecutionPolicyFilter:
if s.contains(s.config.Env.Allow, name) {
return s.backend.OnEnvRequest(requestID, name, value)
}
err := log.UserMessage(
EEnvRejected,
"Environment variable setting rejected.",
"Setting an environment variable is rejected because it does not match the allow list.",
).Label("name", name)
s.logger.Debug(err)
return err
case ExecutionPolicyEnable:
fallthrough
default:
if !s.contains(s.config.Env.Deny, name) {
return s.backend.OnEnvRequest(requestID, name, value)
}
err := log.UserMessage(
EEnvRejected,
"Environment variable setting rejected.",
"Setting an environment variable is rejected because it matches the deny list.",
).Label("name", name)
s.logger.Debug(err)
return err
}
}
func (s *sessionHandler) OnPtyRequest(
requestID uint64,
term string,
columns uint32,
rows uint32,
width uint32,
height uint32,
modeList []byte,
) error {
mode := s.getPolicy(s.config.TTY.Mode)
switch mode {
case ExecutionPolicyDisable:
fallthrough
case ExecutionPolicyFilter:
err := log.UserMessage(
ETTYRejected,
"TTY allocation disabled.",
"TTY allocation is disabled in the security settings.",
)
s.logger.Debug(err)
return err
case ExecutionPolicyEnable:
fallthrough
default:
return s.backend.OnPtyRequest(requestID, term, columns, rows, width, height, modeList)
}
}
func (s *sessionHandler) OnExecRequest(
requestID uint64,
program string,
) error {
mode := s.getPolicy(s.config.Command.Mode)
switch mode {
case ExecutionPolicyDisable:
err := log.UserMessage(
EExecRejected,
"Command execution disabled.",
"Command execution is disabled in the security settings.",
)
s.logger.Debug(err)
return err
case ExecutionPolicyFilter:
if !s.contains(s.config.Command.Allow, program) {
err := log.UserMessage(
EExecRejected,
"Command execution disabled.",
"The specified command passed from the client does not match the specified allow list.",
)
s.logger.Debug(err)
return err
}
case ExecutionPolicyEnable:
fallthrough
default:
}
if s.config.ForceCommand == "" {
return s.backend.OnExecRequest(requestID, program)
}
if err := s.backend.OnEnvRequest(requestID, "SSH_ORIGINAL_COMMAND", program); err != nil {
err := log.WrapUser(
err,
EFailedSetEnv,
"Could not execute program.",
"Command execution failed because the security layer could not set the SSH_ORIGINAL_COMMAND variable.",
)
s.logger.Error(err)
return err
}
s.logger.Debug(log.NewMessage(
MForcingCommand,
"Forcing command execution to %s",
s.config.ForceCommand,
))
return s.backend.OnExecRequest(requestID, s.config.ForceCommand)
}
func (s *sessionHandler) OnShell(
requestID uint64,
) error {
mode := s.getPolicy(s.config.Shell.Mode)
switch mode {
case ExecutionPolicyDisable:
fallthrough
case ExecutionPolicyFilter:
err := log.UserMessage(
EShellRejected,
"Shell execution disabled.",
"Shell execution is disabled in the security settings.",
)
s.logger.Debug(err)
return err
case ExecutionPolicyEnable:
fallthrough
default:
}
if s.config.ForceCommand == "" {
return s.backend.OnShell(requestID)
}
s.logger.Debug(log.NewMessage(
MForcingCommand,
"Forcing command execution to %s",
s.config.ForceCommand,
))
return s.backend.OnExecRequest(requestID, s.config.ForceCommand)
}
func (s *sessionHandler) OnSubsystem(
requestID uint64,
subsystem string,
) error {
mode := s.getPolicy(s.config.Subsystem.Mode)
switch mode {
case ExecutionPolicyDisable:
err := log.UserMessage(
ESubsystemRejected,
"Subsystem execution disabled.",
"Subsystem execution is disabled in the security settings.",
)
s.logger.Debug(err)
return err
case ExecutionPolicyFilter:
if !s.contains(s.config.Subsystem.Allow, subsystem) {
err := log.UserMessage(
ESubsystemRejected,
"Subsystem execution disabled.",
"The specified subsystem does not match the allowed subsystems list.",
)
s.logger.Debug(err)
return err
}
case ExecutionPolicyEnable:
if s.contains(s.config.Subsystem.Deny, subsystem) {
err := log.UserMessage(
ESubsystemRejected,
"Subsystem execution disabled.",
"The subsystem execution is rejected because the specified subsystem matches the deny list.",
)
s.logger.Debug(err)
return err
}
default:
}
if s.config.ForceCommand == "" {
return s.backend.OnSubsystem(requestID, subsystem)
}
if err := s.backend.OnEnvRequest(requestID, "SSH_ORIGINAL_COMMAND", subsystem); err != nil {
err := log.WrapUser(
err,
EFailedSetEnv,
"Could not execute program.",
"Command execution failed because the security layer could not set the SSH_ORIGINAL_COMMAND variable.",
)
s.logger.Error(err)
return err
}
s.logger.Debug(log.NewMessage(
MForcingCommand,
"Forcing command execution to %s",
s.config.ForceCommand,
))
return s.backend.OnExecRequest(requestID, s.config.ForceCommand)
}
func (s *sessionHandler) OnSignal(requestID uint64, signal string) error {
mode := s.getPolicy(s.config.Shell.Mode)
switch mode {
case ExecutionPolicyDisable:
err := log.UserMessage(
ESignalRejected,
"Sending signals is rejected.",
"Sending the signal is rejected because signal delivery is disabled.",
)
s.logger.Debug(err)
return err
case ExecutionPolicyFilter:
if s.contains(s.config.Signal.Allow, signal) {
return s.backend.OnSignal(requestID, signal)
}
err := log.UserMessage(
ESignalRejected,
"Sending signals is rejected.",
"Sending the signal is rejected because the specified signal does not match the allow list.",
)
s.logger.Debug(err)
return err
case ExecutionPolicyEnable:
fallthrough
default:
if !s.contains(s.config.Signal.Deny, signal) {
return s.backend.OnSignal(requestID, signal)
}
err := log.UserMessage(
ESignalRejected,
"Sending signals is rejected.",
"Sending the signal is rejected because the specified signal matches the deny list.",
)
s.logger.Debug(err)
return err
}
}
func (s *sessionHandler) OnWindow(requestID uint64, columns uint32, rows uint32, width uint32, height uint32) error {
return s.backend.OnWindow(requestID, columns, rows, width, height)
}