52#define LIBSSH_LEGACY_0_4
54#include <libssh/libssh.h>
55#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){
315 const char *getError(){
339 std::string ret =
"";
340 if (banner != NULL) {
341 ret = std::string(banner);
371 void log(
int priority,
const char *format, ...){
374 va_start(va, format);
375 ssh_vlog(priority,
"libsshpp", format, &va);
424 void_throwable cancelForward(
const char *address,
int port){
430 void_throwable listenForward(
const char *address,
int port,
437 ssh_session getCSession(){
442 ssh_session c_session;
446 Session(
const Session &);
447 Session& operator=(
const Session &);
459 this->session = &ssh_session;
476 ssh_throw_null(getCSession(),x11chan);
504 int getExitStatus() {
505 uint32_t exit_status = (uint32_t)-1;
509 void_throwable getExitState(uint32_t & pexit_code,
511 int & pcore_dumped) {
518 Session &getSession(){
539 int openForward(
const char *remotehost,
int remoteport,
540 const char *sourcehost,
int localport=0){
542 sourcehost, localport);
547 void_throwable openSession(){
552 int poll(
bool is_stderr=
false){
557 int read(
void *dest,
size_t count){
560 if(count > 0x7fffffff)
566 int read(
void *dest,
size_t count,
int timeout){
569 if(count > 0x7fffffff)
575 int read(
void *dest,
size_t count,
bool is_stderr=
false,
int timeout=-1){
578 if(count > 0x7fffffff)
584 int readNonblocking(
void *dest,
size_t count,
bool is_stderr=
false){
587 if(count > 0x7fffffff)
593 void_throwable requestEnv(
const char *name,
const char *value){
599 void_throwable requestExec(
const char *cmd){
604 void_throwable requestPty(
const char *term=NULL,
int cols=0,
int rows=0,
605 const unsigned char* modes=NULL,
size_t modes_len=0){
607 if(term != NULL && cols != 0 && rows != 0 && modes != NULL)
609 else if(term != NULL && cols != 0 && rows != 0)
610 err=ssh_channel_request_pty_size(channel,term,cols,rows);
617 void_throwable requestShell(){
622 void_throwable requestSendSignal(
const char *signum){
627 void_throwable requestSubsystem(
const char *subsystem){
632 int requestX11(
bool single_connection,
633 const char *protocol,
const char *cookie,
int screen_number){
635 protocol, cookie, screen_number);
639 void_throwable sendEof(){
653 int write(
const void *data,
size_t len,
bool is_stderr=
false){
664 ssh_session getCSession(){
665 return session->getCSession();
668 ssh_channel getCChannel() {
677 Channel (Session &ssh_session, ssh_channel c_channel){
678 this->channel=c_channel;
679 this->session = &ssh_session;
682 Channel(
const Channel &);
683 Channel &operator=(
const Channel &);
688 ssh_channel forward =
690 ssh_throw_null(c_session,forward);
the ssh::Channel class describes the state of an SSH channel.
Definition libsshpp.hpp:454
int write(const void *data, size_t len, bool is_stderr=false)
Writes on a channel.
Definition libsshpp.hpp:653
void_throwable close()
closes a channel
Definition libsshpp.hpp:496
bool isClosed()
returns true if channel is in closed state
Definition libsshpp.hpp:524
void_throwable changePtySize(int cols, int rows)
change the size of a pseudoterminal
Definition libsshpp.hpp:486
bool isEof()
returns true if channel is in EOF state
Definition libsshpp.hpp:530
bool isOpen()
returns true if channel is in open state
Definition libsshpp.hpp:536
Channel * acceptX11(int timeout_ms)
accept an incoming X11 connection
Definition libsshpp.hpp:474
Definition libsshpp.hpp:126
int getAuthList()
Returns the available authentication methods from the server.
Definition libsshpp.hpp:293
int isServerKnown()
verifies that the server is known
Definition libsshpp.hpp:366
int userauthKbdintGetNPrompts()
Get the number of prompts (questions) the server has given.
Definition libsshpp.hpp:224
int userauthKbdint(const char *username, const char *submethods)
Authenticate through the "keyboard-interactive" method.
Definition libsshpp.hpp:214
void silentDisconnect()
silently disconnect from remote host
Definition libsshpp.hpp:399
void_throwable setOption(enum ssh_options_e type, long int option)
sets an SSH session options
Definition libsshpp.hpp:152
socket_t getSocket()
returns the file descriptor used for the communication
Definition libsshpp.hpp:330
void_throwable setOption(enum ssh_options_e type, void *option)
sets an SSH session options
Definition libsshpp.hpp:162
void_throwable setOption(enum ssh_options_e type, const char *option)
sets an SSH session options
Definition libsshpp.hpp:142
int getVersion()
returns the version of the SSH protocol being used
Definition libsshpp.hpp:357
const char * getDisconnectMessage()
Returns the disconnect message from the server, if any.
Definition libsshpp.hpp:308
int userauthPublickeyAuto(void)
Authenticates automatically using public key.
Definition libsshpp.hpp:180
Channel * acceptForward(int timeout_ms)
accept an incoming forward connection
Definition libsshpp.hpp:687
void_throwable connect()
connects to the remote host
Definition libsshpp.hpp:170
int userauthPublickey(ssh_key privkey)
Authenticates using the publickey method.
Definition libsshpp.hpp:282
int userauthKbdintSetAnswer(unsigned int index, const char *answer)
Set the answer for a question from a message block.
Definition libsshpp.hpp:244
int userauthNone()
Authenticates using the "none" method. Prefer using autopubkey if possible.
Definition libsshpp.hpp:192
std::string getIssueBanner()
gets the Issue banner from the ssh server
Definition libsshpp.hpp:337
int getOpensshVersion()
returns the OpenSSH version (server) if possible
Definition libsshpp.hpp:350
void disconnect()
Disconnects from the SSH server and closes connection.
Definition libsshpp.hpp:301
int userauthPassword(const char *password)
Authenticates using the password method.
Definition libsshpp.hpp:259
void_throwable optionsCopy(const Session &source)
copies options from a session to another
Definition libsshpp.hpp:383
int writeKnownhost()
Writes the known host file with current host key.
Definition libsshpp.hpp:407
void_throwable optionsParseConfig(const char *file)
parses a configuration file for options
Definition libsshpp.hpp:392
int userauthTryPublickey(ssh_key pubkey)
Try to authenticate using the publickey method.
Definition libsshpp.hpp:271
if defined, disable C++ exceptions for libssh c++ wrapper
Definition libsshpp.hpp:74
int getCode()
returns the Error code
Definition libsshpp.hpp:89
std::string getError()
returns the error message of the last exception
Definition libsshpp.hpp:96
LIBSSH_API int ssh_userauth_list(ssh_session session, const char *username)
Get available authentication methods from the server.
Definition auth.c:379
LIBSSH_API int ssh_userauth_password(ssh_session session, const char *username, const char *password)
Try to authenticate by password.
Definition auth.c:1616
LIBSSH_API int ssh_userauth_publickey_auto(ssh_session session, const char *username, const char *passphrase)
Tries to automatically authenticate with public key and "none".
Definition auth.c:1272
LIBSSH_API int ssh_userauth_none(ssh_session session, const char *username)
Try to authenticate through the "none" method.
Definition auth.c:409
LIBSSH_API int ssh_userauth_try_publickey(ssh_session session, const char *username, const ssh_key pubkey)
Try to authenticate with the given public key.
Definition auth.c:493
LIBSSH_API int ssh_userauth_kbdint(ssh_session session, const char *user, const char *submethods)
Try to authenticate through the "keyboard-interactive" method.
Definition auth.c:2046
LIBSSH_API int ssh_userauth_kbdint_setanswer(ssh_session session, unsigned int i, const char *answer)
Set the answer for a question from a message block.
Definition auth.c:2253
LIBSSH_API int ssh_userauth_publickey(ssh_session session, const char *username, const ssh_key privkey)
Authenticate with public/private key or certificate.
Definition auth.c:623
LIBSSH_API int ssh_userauth_kbdint_getnprompts(ssh_session session)
Get the number of prompts (questions) the server has given.
Definition auth.c:2090
LIBSSH_API int ssh_channel_request_subsystem(ssh_channel channel, const char *subsystem)
Request a subsystem (for example "sftp").
Definition channels.c:2120
LIBSSH_API int ssh_channel_send_eof(ssh_channel channel)
Send an end of file on the channel.
Definition channels.c:1360
LIBSSH_API int ssh_channel_poll(ssh_channel channel, int is_stderr)
Polls a channel for data to read.
Definition channels.c:3252
LIBSSH_API int ssh_channel_get_exit_state(ssh_channel channel, uint32_t *pexit_code, char **pexit_signal, int *pcore_dumped)
Get the exit state of the channel (error code from the executed instruction or signal).
Definition channels.c:3416
LIBSSH_API int ssh_channel_close(ssh_channel channel)
Close a channel.
Definition channels.c:1421
LIBSSH_API int ssh_channel_request_pty_size_modes(ssh_channel channel, const char *term, int cols, int rows, const unsigned char *modes, size_t modes_len)
Request a pty with a specific type and size.
Definition channels.c:1956
LIBSSH_API int ssh_channel_read_timeout(ssh_channel channel, void *dest, uint32_t count, int is_stderr, int timeout_ms)
Reads data from a channel.
Definition channels.c:3080
LIBSSH_API int ssh_channel_request_pty(ssh_channel channel)
Request a PTY.
Definition channels.c:2037
LIBSSH_API int ssh_channel_cancel_forward(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:2666
LIBSSH_API ssh_channel ssh_channel_open_forward_port(ssh_session session, int timeout_ms, int *destination_port, char **originator, int *originator_port)
Accept an incoming TCP/IP forwarding channel and get information about incoming connection.
Definition channels.c:2647
LIBSSH_API ssh_channel ssh_channel_accept_x11(ssh_channel channel, int timeout_ms)
Accept an X11 forwarding channel.
Definition channels.c:2348
LIBSSH_API int ssh_channel_request_exec(ssh_channel channel, const char *cmd)
Run a shell command without an interactive shell.
Definition channels.c:2789
LIBSSH_API int ssh_channel_write(ssh_channel channel, const void *data, uint32_t len)
Blocking write on a channel.
Definition channels.c:1691
LIBSSH_API int ssh_channel_listen_forward(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:2557
LIBSSH_API int ssh_channel_request_env(ssh_channel channel, const char *name, const char *value)
Set environment variables.
Definition channels.c:2718
LIBSSH_API int ssh_channel_write_stderr(ssh_channel channel, const void *data, uint32_t len)
Blocking write on a channel stderr.
Definition channels.c:3751
LIBSSH_API 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:2855
LIBSSH_API int ssh_channel_is_open(ssh_channel channel)
Check if the channel is open or not.
Definition channels.c:1705
LIBSSH_API 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:3191
LIBSSH_API int ssh_channel_is_closed(ssh_channel channel)
Check if the channel is closed or not.
Definition channels.c:1722
LIBSSH_API int ssh_channel_is_eof(ssh_channel channel)
Check if remote has sent an EOF.
Definition channels.c:1737
LIBSSH_API void ssh_channel_free(ssh_channel channel)
Close and free a channel.
Definition channels.c:1258
LIBSSH_API ssh_channel ssh_channel_new(ssh_session session)
Allocate a new channel.
Definition channels.c:90
LIBSSH_API 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:2224
LIBSSH_API 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:1119
LIBSSH_API int ssh_channel_request_shell(ssh_channel channel)
Request a shell.
Definition channels.c:2097
LIBSSH_API int ssh_channel_open_session(ssh_channel channel)
Open a session channel (suited for a shell, not TCP forwarding).
Definition channels.c:1052
LIBSSH_API 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:2057
LIBSSH_API int ssh_get_error_code(void *error)
Retrieve the error code from the last error.
Definition error.c:148
LIBSSH_API const char * ssh_get_error(void *error)
Retrieve the error text message from the last error.
Definition error.c:128
LIBSSH_API int ssh_connect(ssh_session session)
Connect to the ssh server.
Definition client.c:528
LIBSSH_API const char * ssh_get_disconnect_message(ssh_session session)
Get the disconnect message from the server.
Definition session.c:900
LIBSSH_API void ssh_disconnect(ssh_session session)
Disconnect from a session (client or server).
Definition client.c:786
LIBSSH_API char * ssh_get_issue_banner(ssh_session session)
Get the issue banner from the server.
Definition client.c:684
LIBSSH_API 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:645
LIBSSH_API int ssh_options_parse_config(ssh_session session, const char *filename)
Parse the ssh config file.
Definition options.c:1815
LIBSSH_API int ssh_get_version(ssh_session session)
Get the protocol version of the session.
Definition session.c:925
LIBSSH_API int ssh_session_update_known_hosts(ssh_session session)
Adds the currently connected server to the user known_hosts file.
Definition knownhosts.c:993
LIBSSH_API ssh_session ssh_new(void)
Create a new ssh session.
Definition session.c:60
LIBSSH_API 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:711
LIBSSH_API enum ssh_known_hosts_e ssh_session_is_known_server(ssh_session session)
Check if the servers public key for the connected session is known.
Definition knownhosts.c:1288
LIBSSH_API void ssh_silent_disconnect(ssh_session session)
Disconnect impolitely from a remote host by closing the socket.
Definition session.c:544
LIBSSH_API void ssh_free(ssh_session session)
Deallocate a SSH session handle.
Definition session.c:209
LIBSSH_API int ssh_options_copy(ssh_session src, ssh_session *dest)
Duplicate the options of a session structure.
Definition options.c:68
LIBSSH_API socket_t ssh_get_fd(ssh_session session)
Get the fd of a connection.
Definition session.c:645