This repository has been 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 0
/
handler.go
158 lines (141 loc) · 4.72 KB
/
handler.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
package authintegration
import (
"context"
"net"
"github.com/containerssh/auth"
"github.com/containerssh/log"
"github.com/containerssh/sshserver"
)
// Behavior dictactes how when the authentication requests are passed to the backends.
type Behavior int
const (
// BehaviorNoPassthrough means that the authentication integration will never call the backend for authentication.
BehaviorNoPassthrough Behavior = iota
// BehaviorPassthroughOnFailure will call the backend if the authentication server returned a failure.
BehaviorPassthroughOnFailure Behavior = iota
// BehaviorPassthroughOnSuccess will call the backend if the authentication server returned a success.
BehaviorPassthroughOnSuccess Behavior = iota
// BehaviorPassthroughOnUnavailable will call the backend if the authentication server is not available.
BehaviorPassthroughOnUnavailable Behavior = iota
)
func (behavior Behavior) validate() bool {
switch behavior {
case BehaviorNoPassthrough:
return true
case BehaviorPassthroughOnFailure:
return true
case BehaviorPassthroughOnSuccess:
return true
case BehaviorPassthroughOnUnavailable:
return true
default:
return false
}
}
type handler struct {
backend sshserver.Handler
authClient auth.Client
behavior Behavior
}
func (h *handler) OnReady() error {
if h.backend != nil {
return h.backend.OnReady()
}
return nil
}
func (h *handler) OnShutdown(shutdownContext context.Context) {
if h.backend != nil {
h.backend.OnShutdown(shutdownContext)
}
}
func (h *handler) OnNetworkConnection(client net.TCPAddr, connectionID string) (sshserver.NetworkConnectionHandler, error) {
var backend sshserver.NetworkConnectionHandler = nil
var err error
if h.backend != nil {
backend, err = h.backend.OnNetworkConnection(client, connectionID)
if err != nil {
return nil, err
}
}
return &networkConnectionHandler{
connectionID: connectionID,
ip: client.IP,
authClient: h.authClient,
backend: backend,
behavior: h.behavior,
}, nil
}
type networkConnectionHandler struct {
backend sshserver.NetworkConnectionHandler
authClient auth.Client
ip net.IP
connectionID string
behavior Behavior
}
func (h *networkConnectionHandler) OnShutdown(shutdownContext context.Context) {
h.backend.OnShutdown(shutdownContext)
}
func (h *networkConnectionHandler) OnAuthPassword(username string, password []byte) (response sshserver.AuthResponse, reason error) {
success, err := h.authClient.Password(username, password, h.connectionID, h.ip)
if !success {
if err != nil {
if h.behavior == BehaviorPassthroughOnUnavailable {
return h.backend.OnAuthPassword(username, password)
}
return sshserver.AuthResponseUnavailable, err
}
if h.behavior == BehaviorPassthroughOnFailure {
return h.backend.OnAuthPassword(username, password)
}
return sshserver.AuthResponseFailure, nil
}
if h.behavior == BehaviorPassthroughOnSuccess {
return h.backend.OnAuthPassword(username, password)
} else {
return sshserver.AuthResponseSuccess, nil
}
}
func (h *networkConnectionHandler) OnAuthPubKey(username string, pubKey string) (response sshserver.AuthResponse, reason error) {
success, err := h.authClient.PubKey(username, pubKey, h.connectionID, h.ip)
if !success {
if err != nil {
if h.behavior == BehaviorPassthroughOnUnavailable {
return h.backend.OnAuthPubKey(username, pubKey)
}
return sshserver.AuthResponseUnavailable, err
}
if h.behavior == BehaviorPassthroughOnFailure {
return h.backend.OnAuthPubKey(username, pubKey)
}
return sshserver.AuthResponseFailure, nil
}
if h.behavior == BehaviorPassthroughOnSuccess {
return h.backend.OnAuthPubKey(username, pubKey)
}
return sshserver.AuthResponseSuccess, nil
}
func (h *networkConnectionHandler) OnAuthKeyboardInteractive(
username string,
challenge func(
instruction string,
questions sshserver.KeyboardInteractiveQuestions,
) (answers sshserver.KeyboardInteractiveAnswers, err error),
) (response sshserver.AuthResponse, reason error) {
if h.behavior == BehaviorPassthroughOnUnavailable {
return h.backend.OnAuthKeyboardInteractive(username, challenge)
}
return sshserver.AuthResponseUnavailable, log.UserMessage(
EUnsupported,
"keyboard-interactive authentication not available",
"Keyboard-interactive authentication is not supported by ContainerSSH.",
).Label("connectionId", h.connectionID).Label("username", username)
}
func (h *networkConnectionHandler) OnHandshakeFailed(reason error) {
h.backend.OnHandshakeFailed(reason)
}
func (h *networkConnectionHandler) OnHandshakeSuccess(username string) (connection sshserver.SSHConnectionHandler, failureReason error) {
return h.backend.OnHandshakeSuccess(username)
}
func (h *networkConnectionHandler) OnDisconnect() {
h.backend.OnDisconnect()
}