-
Notifications
You must be signed in to change notification settings - Fork 25
/
session.go
587 lines (541 loc) · 18.9 KB
/
session.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
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>
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
type Session struct {
ptr C.ssh_session
}
func NewSession() (Session, error) {
session := Session{}
session.ptr = C.ssh_new()
if session.ptr == nil {
return session, errors.New("ssh_new() == nil")
}
return session, nil
}
// Blocking flush of the outgoing buffer.
//
// timeout:
// Set an upper limit on the time for which this function will block, in
// milliseconds. Specifying -1 means an infinite timeout. This parameter is
// passed to the poll( function.)
func (s Session) BlockingFlush(timeout int) error {
return s.apiError("ssh_blocking_flush", C.ssh_blocking_flush(s.ptr, C.int(timeout)))
}
// Connect to the ssh server
//
func (s Session) Connect() error {
return s.apiError("ssh_connect", C.ssh_connect(s.ptr))
}
// Disconnect from a session (client or server).
//
// The session can then be reused to open a new session.
func (s Session) Disconnect() {
C.ssh_disconnect(s.ptr)
}
// Deallocate a SSH session handle.
func (s Session) Free() {
C.ssh_free(s.ptr)
}
// get the name of the input cipher for the given session.
func (s Session) GetCipherIn() string {
ciphername := C.ssh_get_cipher_in(s.ptr)
if ciphername == nil {
return ""
}
return C.GoString(ciphername)
}
// get the name of the output cipher for the given session
func (s Session) GetCipherOut() string {
ciphername := C.ssh_get_cipher_out(s.ptr)
if ciphername == nil {
return ""
}
return C.GoString(ciphername)
}
// get the client banner
func (s Session) GetClientBanner() string {
banner := C.ssh_get_clientbanner(s.ptr)
if banner == nil {
return ""
}
return C.GoString(banner)
}
// Get the disconnect message from the server.
//
func (s Session) GetDisconnectMessage() string {
message := C.ssh_get_disconnect_message(s.ptr)
if message == nil {
return ""
}
return C.GoString(message)
}
// Get the fd of a connection
//
// In case you'd need the file descriptor of the connection to the
// server/client.
func (s Session) GetFd() int {
socketFd := C.ssh_get_fd(s.ptr)
return int(socketFd)
}
// get the name of the input HMAC algorithm for the given session.
//
// Returns HMAC algorithm name or "" if unknown.
func (s Session) GetHMacIn() string {
name := C.ssh_get_hmac_in(s.ptr)
if name == nil {
return ""
}
return C.GoString(name)
}
// get the name of the output HMAC algorithm for the given session.
//
// Returns HMAC algorithm name or "" if unknown.
func (s Session) GetHMacOut() string {
name := C.ssh_get_hmac_out(s.ptr)
if name == nil {
return ""
}
return C.GoString(name)
}
// GetIssueBanner()
//
// Get the issue banner from the server.
//
// This is the banner showing a disclaimer to users who log in, typically their right or the fact that they will be monitored.
//
// Return A newly allocated string with the banner, "" on error.
func (s Session) GetIssueBanner() string {
banner := C.ssh_get_issue_banner(s.ptr)
if banner == nil {
return ""
}
return C.GoString(banner)
}
// GetKeyExchangeAlgorithm()
//
// get the name of the current key exchange algorithm.
func (s Session) GetKeyExchangeAlgorithm() string {
algorithm := C.ssh_get_kex_algo(s.ptr)
return C.GoString(algorithm)
}
// Get the version of the OpenSSH server, if it is not an OpenSSH server then 0
// will be returned.
func (s Session) GetOpensshVersion() int {
return int(C.ssh_get_openssh_version(s.ptr))
}
// GetPollFlags()
//
// Get poll flags for an external mainloop.
// Return A bitmask including SSH_READ_PENDING or SSH_WRITE_PENDING.
// For SSH_READ_PENDING, your invocation of poll( should include POLLIN.
// For SSH_WRITE_PENDING, your invocation of poll( should include POLLOUT))
func (s Session) GetPollFlags() int {
return int(C.ssh_get_poll_flags(s.ptr))
}
// GetPubkey()
//
// Get the server public key from a session.
func (s Session) GetPublickey() (*Key, error) {
var key *C.ssh_key
err := s.apiError("ssh_get_publickey", C.ssh_get_publickey(s.ptr, key))
if err != nil {
return nil, err
}
return &Key{*key}, nil
}
// GetServerBanner()
//
// get the server banner
func (s Session) GetServerBanner() string {
banner := C.ssh_get_serverbanner(s.ptr)
if banner == nil {
return ""
}
return C.GoString(banner)
}
// GetStatus()
//
// Get session status.
//
// return A bitmask including SSH_CLOSED, SSH_READ_PENDING, SSH_WRITE_PENDING or
// SSH_CLOSED_ERROR which respectively means the session is closed, has data to
// read on the connection socket and session was closed due to an error.
func (s Session) GetStatus() int {
return int(C.ssh_get_status(s.ptr))
}
// GetVersion()
//
// Get the protocol version of the session.
//
// return 1 or 2, for ssh1 or ssh2, < 0 on error.
func (s Session) GetVersion() int {
return int(C.ssh_get_version(s.ptr))
}
// IsBlocking()
//
// Return the blocking mode of libssh.
func (s Session) IsBlocking() bool {
return C.ssh_is_blocking(s.ptr) == 1
}
// IsConnected()
//
// Check if we are connected.
func (s Session) IsConnected() bool {
return C.ssh_is_connected(s.ptr) == 1
}
// IsServerKnown()
//
// Check if the server is known.
// Checks the user's known host file for a previous connection to the current server.
//
// return:
// SSH_SERVER_KNOWN_OK: The server is known and has not changed.
// SSH_SERVER_KNOWN_CHANGED: The server key has changed. Either you are under attack or the administrator changed the key. You HAVE to warn the user about a possible attack.
// SSH_SERVER_FOUND_OTHER: The server gave use a key of a type while we had an other type recorded. It is a possible attack.
// SSH_SERVER_NOT_KNOWN: The server is unknown. User should confirm the MD5 is correct.
// SSH_SERVER_FILE_NOT_FOUND: The known host file does not exist. The host is thus unknown. File will be created if host key is accepted.
// SSH_SERVER_ERROR: Some error happened.
func (s Session) IsServerKnown() int {
return int(C.ssh_is_server_known(s.ptr))
}
// Duplicate()
//
// Duplicate the options of a session structure.
// If you make several sessions with the same options this is useful. You cannot
// use twice the same option structure in ssh_session_connect.
//
func (s Session) Duplicate() (Session, error) {
sess := Session{}
if rc := C.ssh_options_copy(s.ptr, &sess.ptr); rc == 0 {
return sess, nil
} else {
return sess, fmt.Errorf("ssh_channel_get_session() == %d", rc)
}
}
// GetOption()
//
// This function can get ssh options, it does not support all options provided
// for ssh options set, but mostly those which a user-space program may care
// about having trusted the ssh driver to infer these values from underlaying
// configuration files.
//
// optionType:
// The option type to get. This could be one of the following:
//
// * SSH_OPTIONS_HOST: The hostname or ip address to connect to (const char *).
// * SSH_OPTIONS_USER: The username for authentication (const char *).
// when not explicitly set this will be inferred from the ~/.ssh/config file.
//
// * SSH_OPTIONS_IDENTITY: Set the identity file name (const char *,format
// string).
// By default identity, id_dsa and id_rsa are checked.
// The identity file used authenticate with public key. It may include "%s"
// which will be replaced by the user home directory.
//
// * SSH_OPTIONS_ADD_IDENTITY: Add a new identity file (const char *,format
// string) to the identity list.
// By default identity, id_dsa and id_rsa are checked.
// The identity used authenticate with public key will be prepended to the list.
// It may include "%s" which will be replaced by the user home directory.
//
// * SSH_OPTIONS_PROXYCOMMAND: Get the proxycommand necessary to log into the
// remote host. When not explicitly set, it will be read from the ~/.ssh/config
// file
func (s Session) GetOption(optionType int) string {
var option **C.char
if C.ssh_options_get(s.ptr, C.enum_ssh_options_e(optionType), option) == SSH_OK {
defer C.ssh_string_free_char(*option)
return C.GoString(*option)
}
return ""
}
// GetPort()
//
// This function can get ssh the ssh port.
// It must only be used on a valid ssh session. This function is useful when the
// session options have been automatically inferred from the environment or
// configuration files and one
//
func (s Session) GetPort() int {
var port *C.uint
if C.ssh_options_get_port(s.ptr, port) == SSH_OK {
return int(*port)
}
return -1
}
// ParseConfig()
//
// Parse the ssh config file.
//
// This should be the last call of all options, it may overwrite options which
// are already set. It requires that the host name is already set with
// ssh_options_set_host().
//
// filename:
// The options file to use, if "" the default ~/.ssh/config will be used.
func (s Session) ParseConfig(filename string) error {
filename_cstr := CString(filename)
defer filename_cstr.Free()
if C.ssh_options_parse_config(s.ptr, filename_cstr.Ptr) == SSH_OK {
return nil
}
return errors.New("Unable to parse ssh config")
}
// SetOption()
//
// This function can set all possible ssh options.
//
// SSH_OPTIONS_HOST: The hostname or ip address to connect to (const char *).
// SSH_OPTIONS_PORT: The port to connect to (unsigned int).
// SSH_OPTIONS_PORT_STR: The port to connect to (const char *).
// SSH_OPTIONS_FD: The file descriptor to use (socket_t).
//
// If you wish to open the socket yourself for a reason or another, set the
// file descriptor. Don't forget to set the hostname as the hostname is used as
// a key in the known_host mechanism.
// SSH_OPTIONS_BINDADDR: The address to bind the client to (const char *).
// SSH_OPTIONS_USER: The username for authentication (const char *).
//
// If the value is NULL, the username is set to the default username.
// SSH_OPTIONS_SSH_DIR: Set the ssh directory (const char *,format string).
//
// If the value is NULL, the directory is set to the default ssh directory.
//
// The ssh directory is used for files like known_hosts and identity (private and public
// key). It may include "%s" which will be replaced by the user home directory.
// SSH_OPTIONS_KNOWNHOSTS: Set the known hosts file name (const char *,format string).
//
// If the value is NULL, the directory is set to the default known hosts file, normally ~/.ssh/known_hosts.
//
// The known hosts file is used to certify remote hosts are genuine. It may include "%s"
// which will be replaced by the user home directory.
// SSH_OPTIONS_IDENTITY: Set the identity file name (const char *,format string).
//
// By default identity, id_dsa and id_rsa are checked.
//
// The identity file used authenticate with public key. It may include "%s" which will be replaced by the user home directory.
// SSH_OPTIONS_TIMEOUT: Set a timeout for the connection in seconds (long).
// SSH_OPTIONS_TIMEOUT_USEC: Set a timeout for the connection in micro seconds (long).
// SSH_OPTIONS_SSH1: Allow or deny the connection to SSH1 servers (int, 0 is false).
// SSH_OPTIONS_SSH2: Allow or deny the connection to SSH2 servers (int, 0 is false).
// SSH_OPTIONS_LOG_VERBOSITY: Set the session logging verbosity (int).
//
// The verbosity of the messages. Every log smaller or equal to verbosity will be shown.
// SSH_LOG_NOLOG: No logging
// SSH_LOG_RARE: Rare conditions or warnings
// SSH_LOG_ENTRY: API-accessible entrypoints
// SSH_LOG_PACKET: Packet id and size
// SSH_LOG_FUNCTIONS: Function entering and leaving
// SSH_OPTIONS_LOG_VERBOSITY_STR: Set the session logging verbosity (const char *).
//
// The verbosity of the messages. Every log smaller or equal to verbosity will be shown.
// SSH_LOG_NOLOG: No logging
// SSH_LOG_RARE: Rare conditions or warnings
// SSH_LOG_ENTRY: API-accessible entrypoints
// SSH_LOG_PACKET: Packet id and size
// SSH_LOG_FUNCTIONS: Function entering and leaving
// See the corresponding numbers in libssh.h.
//
// During ssh_connect(), libssh will call the callback with status from 0.0 to 1.0.
// SSH_OPTIONS_STATUS_ARG: Set the status argument which should be passed to the status callback (generic pointer).
// SSH_OPTIONS_CIPHERS_C_S: Set the symmetric cipher client to server (const char *, comma-separated list).
// SSH_OPTIONS_CIPHERS_S_C: Set the symmetric cipher server to client (const char *, comma-separated list).
// SSH_OPTIONS_KEY_EXCHANGE: Set the key exchange method to be used (const char *, comma-separated list). ex: "ecdh-sha2-nistp256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1"
// SSH_OPTIONS_HOSTKEYS: Set the preferred server host key types (const char *, comma-separated list). ex: "ssh-rsa,ssh-dss,ecdh-sha2-nistp256"
// SSH_OPTIONS_COMPRESSION_C_S: Set the compression to use for client to server communication (const char *, "yes", "no" or a specific algorithm name if needed ("zlib","[email protected]","none").
// SSH_OPTIONS_COMPRESSION_S_C: Set the compression to use for server to client communication (const char *, "yes", "no" or a specific algorithm name if needed ("zlib","[email protected]","none").
// SSH_OPTIONS_COMPRESSION: Set the compression to use for both directions communication (const char *, "yes", "no" or a specific algorithm name if needed ("zlib","[email protected]","none").
// SSH_OPTIONS_COMPRESSION_LEVEL: Set the compression level to use for zlib functions. (int, value from 1 to 9, 9 being the most efficient but slower).
// SSH_OPTIONS_STRICTHOSTKEYCHECK: Set the parameter StrictHostKeyChecking to avoid asking about a fingerprint (int, 0 = false).
// SSH_OPTIONS_PROXYCOMMAND: Set the command to be executed in order to connect to server (const char *).
// SSH_OPTIONS_GSSAPI_SERVER_IDENTITY Set it to specify the GSSAPI server identity that libssh should expect when connecting to the server (const char *).
// SSH_OPTIONS_GSSAPI_CLIENT_IDENTITY Set it to specify the GSSAPI client identity that libssh should expect when connecting to the server (const char *).
// SSH_OPTIONS_GSSAPI_DELEGATE_CREDENTIALS Set it to specify that GSSAPI should delegate credentials to the server (int, 0 = false).
func (s Session) SetOption(optionType int, value interface{}) error {
var v unsafe.Pointer
switch optionType {
default:
val, ok := value.(string)
if !ok {
return errors.New("Illegal value for setting option, requires string")
}
_v := CString(val)
v = unsafe.Pointer(_v.Ptr)
defer _v.Free()
case SSH_OPTIONS_PORT:
val, ok := value.(int)
if !ok {
if _, ok = value.(string); ok {
optionType = SSH_OPTIONS_PORT_STR
return s.SetOption(SSH_OPTIONS_PORT_STR, value)
}
return errors.New("Illegal value for setting option, requires string(int)")
}
v = unsafe.Pointer(&val)
case SSH_OPTIONS_FD: // socket_t
val, ok := value.(int)
if !ok {
return errors.New("Illegal value for setting option, requires int")
}
_val := C.socket_t(val)
v = unsafe.Pointer(&_val)
case SSH_OPTIONS_TIMEOUT, SSH_OPTIONS_TIMEOUT_USEC: // long
if val_int, ok := value.(int); ok {
_v := C.long(val_int)
v = unsafe.Pointer(&_v)
} else if val_uint, ok := value.(uint); ok {
_v := C.long(val_uint)
v = unsafe.Pointer(&_v)
} else if val_i64, ok := value.(int64); ok {
_v := C.long(val_i64)
v = unsafe.Pointer(&_v)
} else if val_u64, ok := value.(uint64); ok {
_v := C.long(val_u64)
v = unsafe.Pointer(&_v)
} else {
return errors.New("Illegal value for setting option, requires int/uint/int64/uint64")
}
case SSH_OPTIONS_SSH1, SSH_OPTIONS_SSH2: // int or bool -> false
if val_int, ok := value.(int); ok {
v = unsafe.Pointer(&val_int)
} else if val_bool, ok := value.(bool); ok && !val_bool {
val := 0
v = unsafe.Pointer(&val)
} else {
return errors.New("Illegal value for setting option, requires int/false")
}
case SSH_OPTIONS_LOG_VERBOSITY,
SSH_OPTIONS_COMPRESSION_LEVEL,
SSH_OPTIONS_STRICTHOSTKEYCHECK,
SSH_OPTIONS_GSSAPI_DELEGATE_CREDENTIALS:
if val, ok := value.(int); !ok {
return errors.New("Illegal value for setting option, requires int")
} else {
v = unsafe.Pointer(&val)
}
}
if C.ssh_options_set(s.ptr, C.enum_ssh_options_e(optionType), v) != SSH_OK {
return errors.New("Unable to set option")
}
return nil
}
// SendDebugMessage()
//
// Send a debug message.
//
// message:
// Data to be sent
//
// alwaysDisplay:
// Message SHOULD be displayed by the server. It SHOULD NOT be displayed unless debugging information has been explicitly requested
func (s Session) SendDebugMessage(message string, alwaysDisplay bool) error {
msg := CString(message)
defer msg.Free()
var display C.int = 0
if alwaysDisplay {
display = 1
}
if C.ssh_send_debug(s.ptr, msg.Ptr, display) != SSH_OK {
return errors.New("send debug messaged failed")
}
return nil
}
// SendIgnoreMessage()
//
// Send a message that should be ignored.
func (s Session) SendIgnoreMessage(message string) error {
msg := CString(message)
defer msg.Free()
if C.ssh_send_ignore(s.ptr, msg.Ptr) != SSH_OK {
return errors.New("send ignored messaged failed")
}
return nil
}
// SetBlocking()
//
// Set the session in blocking/nonblocking mode
func (s Session) SetBlocking(blocking bool) {
var value C.int = 0
if blocking {
value = 1
}
C.ssh_set_blocking(s.ptr, value)
}
// Set the session data counters.
//
// This functions sets the counter structures to be used to calculate data which
// comes in and goes out through the session at different levels.
//
// scounter:
// Counter for byte data handled by the session sockets.
//
// rcounter:
// Counter for byte and packet data handled by the session, prior compression
// and SSH overhead.
func (s Session) SetCounter(scounter, rcounter Counter) {
C.ssh_set_counters(s.ptr, scounter.toCCounter(), rcounter.toCCounter())
}
// Tell the session it has an exception to catch on the file descriptor.
func (s Session) SetFdExcept() {
C.ssh_set_fd_except(s.ptr)
}
// Tell the session it has data to read on the file descriptor without blocking.
func (s Session) SetFdToRead() {
C.ssh_set_fd_toread(s.ptr)
}
// Tell the session it may write to the file descriptor without blocking.
func (s Session) SetFdToWrite() {
C.ssh_set_fd_towrite(s.ptr)
}
// Disconnect impolitely from a remote host by closing the socket.
//
// Suitable if you forked and want to destroy this session.
func (s Session) SilentDisconnect() {
C.ssh_silent_disconnect(s.ptr)
}
// Write the current server as known in the known hosts file.
//
// This will create the known hosts file if it does not exist. You generaly use
// it when ssh_is_server_known() answered SSH_SERVER_NOT_KNOWN.
func (s Session) WriteKnownHost() error {
if C.ssh_write_knownhost(s.ptr) != SSH_OK {
return errors.New("Unable to save current server as in known list")
}
return nil
}
func (s Session) GetErrorMsg() string {
err_cstr := C.ssh_get_error(unsafe.Pointer(s.ptr))
return C.GoString(err_cstr)
}
func (s Session) GetErrorCode() int {
return int(C.ssh_get_error_code(unsafe.Pointer(s.ptr)))
}
func (s Session) apiError(fn string, err C.int) error {
switch err {
case SSH_OK:
return nil
case SSH_AGAIN:
return apiError(fn, "SSH_AGAIN")
case SSH_ERROR:
return apiError(fn, fmt.Sprintf("%s (%d)", s.GetErrorMsg(), s.GetErrorCode()))
}
return apiError(fn, err)
}
func (s Session) SendKeepAlive() error {
return apiError("ssh_send_keepalive",
C.ssh_send_keepalive(s.ptr))
}