SSH Session

functions that manage a session More...


Functions

int ssh_connect (SSH_SESSION *session)
 connect to the ssh server
void ssh_disconnect (SSH_SESSION *session)
 Disconnect from a session (client or server).
int ssh_finalize (void)
 Finalize and cleanup all libssh and cryptographic data structures.
const char * ssh_get_disconnect_message (SSH_SESSION *session)
 get the disconnect message from the server
socket_t ssh_get_fd (SSH_SESSION *session)
 In case you'd need the file descriptor of the connection to the server/client.
char * ssh_get_issue_banner (SSH_SESSION *session)
 Get the issue banner from the server.
int ssh_get_openssh_version (SSH_SESSION *session)
 Get the version of the OpenSSH server, if it is not an OpenSSH server then 0 will be returned.
int ssh_get_pubkey_hash (SSH_SESSION *session, unsigned char **hash)
 Allocates a buffer with the MD5 hash of the server public key.
int ssh_get_status (SSH_SESSION *session)
 Get session status.
int ssh_get_version (SSH_SESSION *session)
 Get the protocol version of the session.
int ssh_handle_packets (SSH_SESSION *session)
int ssh_init (void)
 initialize global cryptographic data structures.
int ssh_is_server_known (SSH_SESSION *session)
 Check if the server is known.
SSH_SESSION * ssh_new (void)
 creates a new ssh session
int ssh_select (CHANNEL **channels, CHANNEL **outchannels, socket_t maxfd, fd_set *readfds, struct timeval *timeout)
 A wrapper for the select syscall.
void ssh_set_blocking (SSH_SESSION *session, int blocking)
 set the session in blocking/nonblocking mode
void ssh_set_fd_except (SSH_SESSION *session)
 say the session it has an exception to catch on the file descriptor
void ssh_set_fd_toread (SSH_SESSION *session)
 say to the session it has data to read on the file descriptor without blocking
void ssh_set_fd_towrite (SSH_SESSION *session)
 say the session it may write to the file descriptor without blocking
void ssh_set_options (SSH_SESSION *session, SSH_OPTIONS *options)
 set the options for the current session
void ssh_silent_disconnect (SSH_SESSION *session)
 disconnect impolitely from remote host
int ssh_write_knownhost (SSH_SESSION *session)
 You generaly use it when ssh_is_server_known() answered SSH_SERVER_NOT_KNOWN.


Detailed Description

functions that manage a session

Function Documentation

int ssh_connect ( SSH_SESSION *  session  ) 

connect to the ssh server

Parameters:
session ssh session
Returns:
SSH_OK on success, SSH_ERROR on error
See also:
ssh_new()

ssh_disconnect()

References ssh_init(), ssh_log(), and SSH_LOG_RARE.

void ssh_disconnect ( SSH_SESSION *  session  ) 

Disconnect from a session (client or server).

Parameters:
session The SSH session to disconnect.

References string_free(), and string_from_char().

Referenced by ssh_silent_disconnect().

int ssh_finalize ( void   ) 

Finalize and cleanup all libssh and cryptographic data structures.

This function should only be called once, at the end of the program!

Returns:
-1 in case of error

0 otherwise

const char* ssh_get_disconnect_message ( SSH_SESSION *  session  ) 

get the disconnect message from the server

Parameters:
session ssh session
Returns:
message sent by the server along with the disconnect, or NULL in which case the reason of the disconnect may be found with ssh_get_error.
See also:
ssh_get_error()

socket_t ssh_get_fd ( SSH_SESSION *  session  ) 

In case you'd need the file descriptor of the connection to the server/client.

recover the fd of connection

Parameters:
session ssh session
Returns:
file descriptor of the connection, or -1 if it is not connected

char* ssh_get_issue_banner ( SSH_SESSION *  session  ) 

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.

Parameters:
session The SSH session to use.
Returns:
A newly allocated string with the banner, NULL on error.

References string_to_char().

int ssh_get_openssh_version ( SSH_SESSION *  session  ) 

Get the version of the OpenSSH server, if it is not an OpenSSH server then 0 will be returned.

You can use the SSH_VERSION_INT macro to compare version numbers.

Parameters:
session The SSH session to use.
Returns:
The version number if available, 0 otherwise.

int ssh_get_pubkey_hash ( SSH_SESSION *  session,
unsigned char **  hash 
)

Allocates a buffer with the MD5 hash of the server public key.

Parameters:
session The SSH session to use.
hash The buffer to allocate.
Returns:
The bytes allocated or < 0 on error.
Warning:
It is very important that you verify at some moment that the hash matches a known server. If you don't do it, cryptography wont help you at making things secure
See also:
ssh_is_server_known()

ssh_get_hexa()

ssh_print_hexa()

References string_len().

int ssh_get_status ( SSH_SESSION *  session  ) 

Get session status.

Parameters:
session The ssh session to use.
Returns:
A bitmask including SSH_CLOSED, SSH_READ_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.

int ssh_get_version ( SSH_SESSION *  session  ) 

Get the protocol version of the session.

Parameters:
session The ssh session to use.
Returns:
1 or 2, for ssh1 or ssh2, < 0 on error.

int ssh_handle_packets ( SSH_SESSION *  session  ) 

Warning:
I don't remember if this should be internal or not

Referenced by channel_poll().

int ssh_init ( void   ) 

initialize global cryptographic data structures.

This function should only be called once, at the begining of the program, in the main thread. It may be omitted if your program is not multithreaded.

Returns:
0

Referenced by ssh_bind_listen(), and ssh_connect().

int ssh_is_server_known ( SSH_SESSION *  session  ) 

Check if the server is known.

Checks the user's known host file for a previous connection to the current server.

Parameters:
session ssh session
Returns:
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
See also:
ssh_options_set_wanted_algo()

ssh_get_pubkey_hash()

Bug:
There is no current way to remove or modify an entry into the known host table.

SSH_SESSION* ssh_new ( void   ) 

creates a new ssh session

Returns:
new ssh_session pointer

References buffer_new().

Referenced by ssh_bind_accept().

int ssh_select ( CHANNEL **  channels,
CHANNEL **  outchannels,
socket_t  maxfd,
fd_set *  readfds,
struct timeval *  timeout 
)

A wrapper for the select syscall.

This functions acts more or less like the select(2) syscall.
There is no support for writing or exceptions.

Parameters:
channels Arrays of channels pointers terminated by a NULL. It is never rewritten.
outchannels Arrays of same size that "channels", there is no need to initialize it.
maxfd Maximum +1 file descriptor from readfds.
readfds A fd_set of file descriptors to be select'ed for reading.
timeout A timeout for the select.
Returns:
-1 if an error occured. E_INTR if it was interrupted. In that case, just restart it.
Warning:
libssh is not threadsafe here. That means that if a signal is caught during the processing of this function, you cannot call ssh functions on sessions that are busy with ssh_select().
See also:
select(2)

References channel_poll().

void ssh_set_blocking ( SSH_SESSION *  session,
int  blocking 
)

set the session in blocking/nonblocking mode

Parameters:
session ssh session
blocking zero for nonblocking mode
Bug:
nonblocking code is in development and won't work as expected

void ssh_set_fd_except ( SSH_SESSION *  session  ) 

say the session it has an exception to catch on the file descriptor

Parameters:
session ssh session

void ssh_set_fd_toread ( SSH_SESSION *  session  ) 

say to the session it has data to read on the file descriptor without blocking

Parameters:
session ssh session

void ssh_set_fd_towrite ( SSH_SESSION *  session  ) 

say the session it may write to the file descriptor without blocking

Parameters:
session ssh session

void ssh_set_options ( SSH_SESSION *  session,
SSH_OPTIONS *  options 
)

set the options for the current session

Parameters:
session ssh session
options options structure
See also:
ssh_new()

ssh_options_new()

void ssh_silent_disconnect ( SSH_SESSION *  session  ) 

disconnect impolitely from remote host

Parameters:
session current ssh session

References ssh_disconnect().

int ssh_write_knownhost ( SSH_SESSION *  session  ) 

You generaly use it when ssh_is_server_known() answered SSH_SERVER_NOT_KNOWN.

write the current server as known in the known hosts file. This will create the known hosts file if it does not exist.

Parameters:
session ssh session
Returns:
0 on success, -1 on error

References ssh_dirname(), ssh_mkdir(), and string_len().


doxygen