53 #define LIBSSH_LEGACY_0_4
55 #include <libssh/libssh.h>
56 #include <libssh/server.h>
68 #ifndef SSH_NO_CPP_EXCEPTIONS
82 description=e.description;
101 std::string description;
107 #define ssh_throw(x) if((x)==SSH_ERROR) throw SshException(getCSession())
108 #define ssh_throw_null(CSession,x) if((x)==NULL) throw SshException(CSession)
109 #define void_throwable void
110 #define return_throwable return
117 #define ssh_throw(x) if((x)==SSH_ERROR) return SSH_ERROR
118 #define ssh_throw_null(CSession,x) if((x)==NULL) return NULL
119 #define void_throwable int
120 #define return_throwable return SSH_OK
142 void_throwable
setOption(
enum ssh_options_e type,
const char *option){
152 void_throwable
setOption(
enum ssh_options_e type,
long int option){
162 void_throwable
setOption(
enum ssh_options_e type,
void *option){
238 int userauthPrivatekeyFile(
const char *filename,
239 const char *passphrase);
267 const char *getError(){
291 std::string ret= std::string(banner);
320 void log(
int priority,
const char *format, ...){
324 va_start(va, format);
325 vsnprintf(buffer,
sizeof(buffer), format, va);
327 ssh_log(c_session,priority,
"%s", buffer);
375 void_throwable cancelForward(
const char *address,
int port){
381 void_throwable listenForward(
const char *address,
int port,
389 ssh_session c_session;
390 ssh_session getCSession(){
394 Session(
const Session &);
395 Session& operator=(
const Session &);
407 this->session=&session;
424 ssh_throw_null(getCSession(),x11chan);
452 Session &getSession(){
473 int openForward(
const char *remotehost,
int remoteport,
474 const char *sourcehost=NULL,
int localport=0){
476 sourcehost, localport);
481 void_throwable openSession(){
486 int poll(
bool is_stderr=
false){
491 int read(
void *dest,
size_t count,
bool is_stderr=
false){
494 if(count > 0x7fffffff)
500 int readNonblocking(
void *dest,
size_t count,
bool is_stderr=
false){
503 if(count > 0x7fffffff)
509 void_throwable requestEnv(
const char *name,
const char *value){
515 void_throwable requestExec(
const char *cmd){
520 void_throwable requestPty(
const char *term=NULL,
int cols=0,
int rows=0){
522 if(term != NULL && cols != 0 && rows != 0)
530 void_throwable requestShell(){
535 void_throwable requestSendSignal(
const char *signum){
540 void_throwable requestSubsystem(
const char *subsystem){
545 int requestX11(
bool single_connection,
546 const char *protocol,
const char *cookie,
int screen_number){
548 protocol, cookie, screen_number);
552 void_throwable sendEof(){
566 int write(
const void *data,
size_t len,
bool is_stderr=
false){
569 ret=ssh_channel_write_stderr(channel,data,len);
577 ssh_session getCSession(){
578 return session->getCSession();
580 Channel (Session &session, ssh_channel c_channel){
581 this->channel=c_channel;
582 this->session=&session;
587 Channel(
const Channel &);
588 Channel &operator=(
const Channel &);
596 ssh_throw_null(c_session,forward);
int userauthNone()
Authenticates using the "none" method.
Definition: libsshpp.hpp:192
int ssh_userauth_offer_pubkey(ssh_session session, const char *username, int type, ssh_string publickey)
Try to authenticate through public key.
Definition: auth.c:494
int getAuthList()
Returns the available authentication methods from the server.
Definition: libsshpp.hpp:245
void connect()
connects to the remote host
Definition: libsshpp.hpp:170
int ssh_options_parse_config(ssh_session session, const char *filename)
Parse the ssh config file.
Definition: options.c:868
Channel * acceptX11(int timeout_ms)
accept an incoming X11 connection
Definition: libsshpp.hpp:422
int ssh_channel_request_x11(ssh_channel channel, int single_connection, const char *protocol, const char *cookie, int screen_number)
Sends the "x11-req" channel request over an existing session channel.
Definition: channels.c:1711
int ssh_channel_request_shell(ssh_channel channel)
Request a shell.
Definition: channels.c:1622
int ssh_channel_open_forward(ssh_channel channel, const char *remotehost, int remoteport, const char *sourcehost, int localport)
Open a TCP/IP forwarding channel.
Definition: channels.c:925
void ssh_log(ssh_session session, int prioriry, const char *format,...)
Log a SSH event.
Definition: log.c:76
int ssh_channel_is_eof(ssh_channel channel)
Check if remote has sent an EOF.
Definition: channels.c:1304
ssh_channel ssh_forward_accept(ssh_session session, int timeout_ms)
Accept an incoming TCP/IP forwarding channel.
Definition: channels.c:2037
int isServerKnown()
verifies that the server is known
Definition: libsshpp.hpp:315
int ssh_channel_send_eof(ssh_channel channel)
Send an end of file on the channel.
Definition: channels.c:1038
void close()
closes a channel
Definition: libsshpp.hpp:444
int ssh_channel_close(ssh_channel channel)
Close a channel.
Definition: channels.c:1082
int ssh_forward_cancel(ssh_session session, const char *address, int port)
Sends the "cancel-tcpip-forward" global request to ask the server to cancel the tcpip-forward request...
Definition: channels.c:2069
int ssh_userauth_pubkey(ssh_session session, const char *username, ssh_string publickey, ssh_private_key privatekey)
Try to authenticate through public key.
Definition: auth.c:619
int getVersion()
returns the version of the SSH protocol being used
Definition: libsshpp.hpp:306
The ssh::Session class contains the state of a SSH connection.
Definition: libsshpp.hpp:126
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...
Definition: client.c:743
int ssh_userauth_list(ssh_session session, const char *username)
retrieves available authentication methods for this session
Definition: auth.c:325
const char * ssh_get_error(void *error)
Retrieve the error text message from the last error.
Definition: error.c:97
int ssh_channel_write(ssh_channel channel, const void *data, uint32_t len)
Blocking write on a channel.
Definition: channels.c:1267
int ssh_channel_request_pty_size(ssh_channel channel, const char *term, int cols, int rows)
Request a pty with a specific type and size.
Definition: channels.c:1498
int ssh_channel_request_subsystem(ssh_channel channel, const char *subsystem)
Request a subsystem (for example "sftp").
Definition: channels.c:1642
the ssh::Channel class describes the state of an SSH channel.
Definition: libsshpp.hpp:402
int ssh_get_version(ssh_session session)
Get the protocol version of the session.
Definition: session.c:573
int ssh_channel_request_env(ssh_channel channel, const char *name, const char *value)
Set environment variables.
Definition: channels.c:2113
int userauthOfferPubkey(int type, ssh_string pubkey)
Try to authenticate using the publickey method.
Definition: libsshpp.hpp:216
char * ssh_get_issue_banner(ssh_session session)
Get the issue banner from the server.
Definition: client.c:725
int write(const void *data, size_t len, bool is_stderr=false)
Writes on a channel.
Definition: libsshpp.hpp:566
void changePtySize(int cols, int rows)
change the size of a pseudoterminal
Definition: libsshpp.hpp:434
std::string getIssueBanner()
gets the Issue banner from the ssh server
Definition: libsshpp.hpp:289
int ssh_options_copy(ssh_session src, ssh_session *dest)
Duplicate the options of a session structure.
Definition: options.c:62
void disconnect()
Disconnects from the SSH server and closes connection.
Definition: libsshpp.hpp:253
int getCode()
returns the Error code
Definition: libsshpp.hpp:89
int ssh_channel_open_session(ssh_channel channel)
Open a session channel (suited for a shell, not TCP forwarding).
Definition: channels.c:889
int ssh_userauth_autopubkey(ssh_session session, const char *passphrase)
Tries to automatically authenticate with public key and "none".
Definition: auth.c:1113
Some people do not like C++ exceptions.
Definition: libsshpp.hpp:74
int ssh_channel_is_open(ssh_channel channel)
Check if the channel is open or not.
Definition: channels.c:1280
int ssh_write_knownhost(ssh_session session)
Write the current server as known in the known hosts file.
Definition: known_hosts.c:530
int ssh_channel_is_closed(ssh_channel channel)
Check if the channel is closed or not.
Definition: channels.c:1293
socket_t getSocket()
returns the file descriptor used for the communication
Definition: libsshpp.hpp:282
void optionsParseConfig(const char *file)
parses a configuration file for options
Definition: libsshpp.hpp:343
void setOption(enum ssh_options_e type, void *option)
sets an SSH session options
Definition: libsshpp.hpp:162
int ssh_channel_poll(ssh_channel channel, int is_stderr)
Polls a channel for data to read.
Definition: channels.c:2523
int writeKnownhost()
Writes the known host file with current host key.
Definition: libsshpp.hpp:358
ssh_channel ssh_channel_new(ssh_session session)
Allocate a new channel.
Definition: channels.c:79
int ssh_options_set(ssh_session session, enum ssh_options_e type, const void *value)
This function can set all possible ssh options.
Definition: options.c:348
int ssh_userauth_password(ssh_session session, const char *username, const char *password)
Try to authenticate by password.
Definition: auth.c:974
socket_t ssh_get_fd(ssh_session session)
Get the fd of a connection.
Definition: session.c:359
int userauthPassword(const char *password)
Authenticates using the password method.
Definition: libsshpp.hpp:203
bool isEof()
returns true if channel is in EOF state
Definition: libsshpp.hpp:464
int ssh_channel_change_pty_size(ssh_channel channel, int cols, int rows)
Change the size of the terminal associated to a channel.
Definition: channels.c:1578
int ssh_connect(ssh_session session)
Connect to the ssh server.
Definition: client.c:621
void ssh_disconnect(ssh_session session)
Disconnect from a session (client or server).
Definition: client.c:757
int getOpensshVersion()
returns the OpenSSH version (server) if possible
Definition: libsshpp.hpp:299
int userauthPubkey(ssh_string pubkey, ssh_private_key privkey)
Authenticates using the publickey method.
Definition: libsshpp.hpp:228
bool isClosed()
returns true if channel is in closed state
Definition: libsshpp.hpp:458
int ssh_userauth_none(ssh_session session, const char *username)
Try to authenticate through the "none" method.
Definition: auth.c:358
ssh_channel ssh_channel_accept_x11(ssh_channel channel, int timeout_ms)
Accept an X11 forwarding channel.
Definition: channels.c:1819
int ssh_channel_request_send_signal(ssh_channel channel, const char *signum)
Send a signal to remote process (as described in RFC 4254, section 6.9).
Definition: channels.c:2250
std::string getError()
returns the error message of the last exception
Definition: libsshpp.hpp:96
void silentDisconnect()
silently disconnect from remote host
Definition: libsshpp.hpp:350
Channel * acceptForward(int timeout_ms)
accept an incoming forward connection
Definition: libsshpp.hpp:593
int ssh_channel_request_exec(ssh_channel channel, const char *cmd)
Run a shell command without an interactive shell.
Definition: channels.c:2182
bool isOpen()
returns true if channel is in open state
Definition: libsshpp.hpp:470
int ssh_channel_request_pty(ssh_channel channel)
Request a PTY.
Definition: channels.c:1559
void ssh_silent_disconnect(ssh_session session)
Disconnect impolitely from a remote host by closing the socket.
Definition: session.c:268
int ssh_forward_listen(ssh_session session, const char *address, int port, int *bound_port)
Sends the "tcpip-forward" global request to ask the server to begin listening for inbound connections...
Definition: channels.c:1990
void setOption(enum ssh_options_e type, long int option)
sets an SSH session options
Definition: libsshpp.hpp:152
const char * ssh_get_disconnect_message(ssh_session session)
Get the disconnect message from the server.
Definition: session.c:545
ssh_session ssh_new(void)
Create a new ssh session.
Definition: session.c:55
void ssh_channel_free(ssh_channel channel)
Close and free a channel.
Definition: channels.c:997
int ssh_is_server_known(ssh_session session)
Check if the server is known.
Definition: known_hosts.c:414
void setOption(enum ssh_options_e type, const char *option)
sets an SSH session options
Definition: libsshpp.hpp:142
int ssh_channel_get_exit_status(ssh_channel channel)
Get the exit status of the channel (error code from the executed instruction).
Definition: channels.c:2574
const char * getDisconnectMessage()
Returns the disconnect message from the server, if any.
Definition: libsshpp.hpp:260
int ssh_channel_read(ssh_channel channel, void *dest, uint32_t count, int is_stderr)
Reads data from a channel.
Definition: channels.c:2388
void optionsCopy(const Session &source)
copies options from a session to another
Definition: libsshpp.hpp:334
void ssh_free(ssh_session session)
Deallocate a SSH session handle.
Definition: session.c:159
int userauthAutopubkey(void)
Authenticates automatically using public key.
Definition: libsshpp.hpp:180
int ssh_channel_read_nonblocking(ssh_channel channel, void *dest, uint32_t count, int is_stderr)
Do a nonblocking read on the channel.
Definition: channels.c:2485
int ssh_get_error_code(void *error)
Retrieve the error code from the last error.
Definition: error.c:117