-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathserver.go
193 lines (165 loc) · 5.24 KB
/
server.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
package libssh
/*
#cgo pkg-config: libssh
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <libssh/libssh.h>
#include <libssh/server.h>
#include <libssh/callbacks.h>
#include "callbacks.h"
extern void set_session_message_callback(ssh_session session, void *userdata);
*/
import "C"
import (
"fmt"
"unsafe"
)
const (
SSH_BIND_OPTIONS_BINDADDR = C.SSH_BIND_OPTIONS_BINDADDR
SSH_BIND_OPTIONS_BINDPORT = C.SSH_BIND_OPTIONS_BINDPORT
SSH_BIND_OPTIONS_BINDPORT_STR = C.SSH_BIND_OPTIONS_BINDPORT_STR
SSH_BIND_OPTIONS_HOSTKEY = C.SSH_BIND_OPTIONS_HOSTKEY
SSH_BIND_OPTIONS_DSAKEY = C.SSH_BIND_OPTIONS_DSAKEY
SSH_BIND_OPTIONS_RSAKEY = C.SSH_BIND_OPTIONS_RSAKEY
SSH_BIND_OPTIONS_BANNER = C.SSH_BIND_OPTIONS_BANNER
SSH_BIND_OPTIONS_LOG_VERBOSITY = C.SSH_BIND_OPTIONS_LOG_VERBOSITY
SSH_BIND_OPTIONS_LOG_VERBOSITY_STR = C.SSH_BIND_OPTIONS_LOG_VERBOSITY_STR
SSH_BIND_OPTIONS_ECDSAKEY = C.SSH_BIND_OPTIONS_ECDSAKEY
)
type Bind struct {
ptr C.ssh_bind
}
type BindIncomingConnectionCallback interface {
OnBindIncomingConnection(bind Bind)
}
func wrapBindIncomingConnectionCallback(cb BindIncomingConnectionCallback) C.ssh_bind_incoming_connection_callback {
ptr := NULL
if cb != nil {
wrapper := func(bind C.ssh_bind, userdata unsafe.Pointer) {
cb.OnBindIncomingConnection(Bind{bind})
}
ptr = unsafe.Pointer(&wrapper)
}
return C.ssh_bind_incoming_connection_callback(ptr)
}
func NewBind() (Bind, error) {
bind := C.ssh_bind_new()
if bind == nil {
return Bind{}, apiError("ssh_bind_new", "NULL")
}
return Bind{bind}, nil
}
func (b Bind) Free() {
C.ssh_bind_free(b.ptr)
}
func (b Bind) SetOption(optionType int, value interface{}) error {
var ptr unsafe.Pointer
switch optionType {
case SSH_BIND_OPTIONS_BINDADDR,
SSH_BIND_OPTIONS_BINDPORT,
SSH_BIND_OPTIONS_LOG_VERBOSITY:
val, ok := value.(int)
if ok {
ptr = unsafe.Pointer(&val)
}
case SSH_BIND_OPTIONS_BINDPORT_STR,
SSH_BIND_OPTIONS_HOSTKEY,
SSH_BIND_OPTIONS_DSAKEY,
SSH_BIND_OPTIONS_RSAKEY,
SSH_BIND_OPTIONS_BANNER,
SSH_BIND_OPTIONS_LOG_VERBOSITY_STR,
SSH_BIND_OPTIONS_ECDSAKEY:
val, ok := value.(string)
if ok {
v := CString(val)
defer v.Free()
ptr = unsafe.Pointer(v.Ptr)
}
}
if ptr == nil {
return fmt.Errorf("Illegal parameter type for ssh_bind_options_set()")
}
return apiError("ssh_bind_options_set",
C.ssh_bind_options_set(b.ptr, C.enum_ssh_bind_options_e(optionType), ptr))
}
func (b Bind) Listen() error {
return apiError("ssh_bind_listen", C.ssh_bind_listen(b.ptr))
}
type BindCallbacks struct {
cbsptr C.ssh_bind_callbacks_wrapper
OnIncomingConnection func(bind Bind)
}
func (callbacks *BindCallbacks) Free() {
if callbacks.cbsptr != nil {
C.free(unsafe.Pointer(callbacks.cbsptr))
callbacks.cbsptr = nil
}
}
//export bind_incoming_connection_callback
func bind_incoming_connection_callback(bind C.ssh_bind, userdata unsafe.Pointer) {
callbacks := (*BindCallbacks)(userdata)
callbacks.OnIncomingConnection(Bind{bind})
}
func (b Bind) SetCallback(callbacks *BindCallbacks) error {
callbacks.cbsptr = C.new_bind_callbacks()
callbacks.cbsptr.userdata = unsafe.Pointer(callbacks)
if callbacks.OnIncomingConnection != nil {
C.install_bind_incoming_connection_callback(&callbacks.cbsptr.callbacks)
}
return apiError("ssh_bind_set_callbacks", C.set_bind_callbacks(b.ptr, callbacks.cbsptr))
}
func (b Bind) SetBlocking(blocking bool) {
C.ssh_bind_set_blocking(b.ptr, CBool(blocking))
}
func (b Bind) GetFd() (int, error) {
fd := C.ssh_bind_get_fd(b.ptr)
return int(fd), apiError("ssh_bind_get_fd", fd)
}
func (b Bind) SetFd(socketFd int) {
C.ssh_bind_set_fd(b.ptr, C.socket_t(socketFd))
}
func (b Bind) SetFdToAccept() {
C.ssh_bind_fd_toaccept(b.ptr)
}
// Accept an incoming ssh connection and initialize the session.
//
// session:
// A preallocated ssh session
func (b Bind) Accept(session Session) error {
return apiError("ssh_bind_accept", C.ssh_bind_accept(b.ptr, session.ptr))
}
func (b Bind) AcceptFd(session Session, socketFd int) error {
return apiError("ssh_bind_accept_fd", C.ssh_bind_accept(b.ptr, session.ptr))
}
func (s Session) HandleKeyExchange() error {
return apiError("ssh_handle_key_exchange", C.ssh_handle_key_exchange(s.ptr))
}
// methods:
// SSH_AUTH_METHOD_UNKNOWN 0
// SSH_AUTH_METHOD_NONE 0x0001
// SSH_AUTH_METHOD_PASSWORD 0x0002
// SSH_AUTH_METHOD_PUBLICKEY 0x0004
// SSH_AUTH_METHOD_HOSTBASED 0x0008
// SSH_AUTH_METHOD_INTERACTIVE 0x0010
// SSH_AUTH_METHOD_GSSAPI_MIC 0x0020
func (s Session) SetAuthMethods(methods int) {
C.ssh_set_auth_methods(s.ptr, C.int(methods))
}
// for server
type BindMessageCallback struct {
OnBindMessage func(session Session, msg Message) int
}
//export bind_message_callback
func bind_message_callback(sess C.ssh_session, msg C.ssh_message, userdata unsafe.Pointer) C.int {
callback := (*BindMessageCallback)(userdata)
return C.int(callback.OnBindMessage(Session{sess}, Message{msg}))
}
func (s Session) SetMessageCallback(callback *BindMessageCallback) {
C.set_session_message_callback(s.ptr, unsafe.Pointer(callback))
}
func (s Session) ExecuteMessageCallbacks() error {
return apiError("ssh_execute_message_callbacks", C.ssh_execute_message_callbacks(s.ptr))
}