2005-07-05 05:21:44 +04:00
|
|
|
/* client.c */
|
|
|
|
/*
|
2009-07-13 02:23:42 +04:00
|
|
|
Copyright 2003-2009 Aris Adamantiadis
|
2005-07-05 05:21:44 +04:00
|
|
|
|
|
|
|
This file is part of the SSH Library
|
|
|
|
|
|
|
|
You are free to copy this file, modify it in any way, consider it being public
|
2009-10-09 23:44:05 +04:00
|
|
|
domain. This does not apply to the rest of the library though, but it is
|
2005-07-05 05:21:44 +04:00
|
|
|
allowed to cut-and-paste working code from this file to any license of
|
|
|
|
program.
|
|
|
|
The goal is to show the API in action. It's not a reference on how terminal
|
|
|
|
clients must be made or how a client should react.
|
|
|
|
*/
|
|
|
|
|
2005-08-19 15:12:29 +04:00
|
|
|
#include "config.h"
|
2005-07-05 05:21:44 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/time.h>
|
2013-07-22 12:41:58 +04:00
|
|
|
|
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
|
#include <termios.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2005-08-19 15:12:29 +04:00
|
|
|
#ifdef HAVE_PTY_H
|
2005-07-05 05:21:44 +04:00
|
|
|
#include <pty.h>
|
2005-08-19 15:12:29 +04:00
|
|
|
#endif
|
2013-07-22 12:41:58 +04:00
|
|
|
|
2005-08-31 14:40:52 +04:00
|
|
|
#include <sys/ioctl.h>
|
2005-07-05 05:21:44 +04:00
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
2013-07-22 12:41:58 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2009-10-10 13:55:42 +04:00
|
|
|
#include <libssh/callbacks.h>
|
2005-07-05 05:21:44 +04:00
|
|
|
#include <libssh/libssh.h>
|
|
|
|
#include <libssh/sftp.h>
|
|
|
|
|
|
|
|
|
2009-11-07 05:45:27 +03:00
|
|
|
#include "examples_common.h"
|
2005-07-05 05:21:44 +04:00
|
|
|
#define MAXCMD 10
|
2009-10-10 18:25:40 +04:00
|
|
|
|
2012-02-18 15:29:19 +04:00
|
|
|
static char *host;
|
|
|
|
static char *user;
|
|
|
|
static char *cmds[MAXCMD];
|
|
|
|
static struct termios terminal;
|
2009-11-17 00:36:31 +03:00
|
|
|
|
2012-02-18 15:29:19 +04:00
|
|
|
static char *pcap_file=NULL;
|
|
|
|
|
|
|
|
static char *proxycommand;
|
2010-05-11 00:44:06 +04:00
|
|
|
|
2009-10-10 13:55:42 +04:00
|
|
|
static int auth_callback(const char *prompt, char *buf, size_t len,
|
|
|
|
int echo, int verify, void *userdata) {
|
2012-10-08 22:07:09 +04:00
|
|
|
(void) verify;
|
|
|
|
(void) userdata;
|
2009-10-10 13:55:42 +04:00
|
|
|
|
2012-10-08 22:07:09 +04:00
|
|
|
return ssh_getpass(prompt, buf, len, echo, verify);
|
2009-10-10 13:55:42 +04:00
|
|
|
}
|
|
|
|
|
2009-10-10 18:25:40 +04:00
|
|
|
struct ssh_callbacks_struct cb = {
|
|
|
|
.auth_function=auth_callback,
|
|
|
|
.userdata=NULL
|
|
|
|
};
|
|
|
|
|
2009-02-06 12:58:44 +03:00
|
|
|
static void add_cmd(char *cmd){
|
2005-07-05 05:21:44 +04:00
|
|
|
int n;
|
2013-06-17 15:27:40 +04:00
|
|
|
|
|
|
|
for (n = 0; (n < MAXCMD) && cmds[n] != NULL; n++);
|
|
|
|
|
|
|
|
if (n == MAXCMD) {
|
2005-07-05 05:21:44 +04:00
|
|
|
return;
|
2013-06-17 15:27:40 +04:00
|
|
|
}
|
2005-07-05 05:21:44 +04:00
|
|
|
cmds[n]=strdup(cmd);
|
|
|
|
}
|
|
|
|
|
2009-02-06 12:58:44 +03:00
|
|
|
static void usage(){
|
2005-07-05 05:21:44 +04:00
|
|
|
fprintf(stderr,"Usage : ssh [options] [login@]hostname\n"
|
2009-03-21 12:29:31 +03:00
|
|
|
"sample client - libssh-%s\n"
|
2005-07-05 05:21:44 +04:00
|
|
|
"Options :\n"
|
|
|
|
" -l user : log in as user\n"
|
|
|
|
" -p port : connect to port\n"
|
|
|
|
" -d : use DSS to verify host public key\n"
|
2009-11-17 00:36:31 +03:00
|
|
|
" -r : use RSA to verify host public key\n"
|
|
|
|
#ifdef WITH_PCAP
|
|
|
|
" -P file : create a pcap debugging file\n"
|
2010-05-11 00:44:06 +04:00
|
|
|
#endif
|
|
|
|
#ifndef _WIN32
|
|
|
|
" -T proxycommand : command to execute as a socket proxy\n"
|
2009-11-17 00:36:31 +03:00
|
|
|
#endif
|
|
|
|
,
|
2009-03-21 12:29:31 +03:00
|
|
|
ssh_version(0));
|
2005-07-05 05:21:44 +04:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2009-02-06 12:58:44 +03:00
|
|
|
static int opts(int argc, char **argv){
|
2005-07-05 05:21:44 +04:00
|
|
|
int i;
|
|
|
|
// for(i=0;i<argc;i++)
|
|
|
|
// printf("%d : %s\n",i,argv[i]);
|
|
|
|
/* insert your own arguments here */
|
2010-05-11 00:44:06 +04:00
|
|
|
while((i=getopt(argc,argv,"T:P:"))!=-1){
|
2005-07-05 05:21:44 +04:00
|
|
|
switch(i){
|
2009-11-17 00:36:31 +03:00
|
|
|
case 'P':
|
|
|
|
pcap_file=optarg;
|
|
|
|
break;
|
2010-05-11 00:44:06 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
case 'T':
|
|
|
|
proxycommand=optarg;
|
|
|
|
break;
|
|
|
|
#endif
|
2009-11-27 20:08:53 +03:00
|
|
|
default:
|
2005-07-05 05:21:44 +04:00
|
|
|
fprintf(stderr,"unknown option %c\n",optopt);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(optind < argc)
|
|
|
|
host=argv[optind++];
|
|
|
|
while(optind < argc)
|
|
|
|
add_cmd(argv[optind++]);
|
|
|
|
if(host==NULL)
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-31 05:29:22 +04:00
|
|
|
#ifndef HAVE_CFMAKERAW
|
2005-08-19 15:12:29 +04:00
|
|
|
static void cfmakeraw(struct termios *termios_p){
|
|
|
|
termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
|
|
|
|
termios_p->c_oflag &= ~OPOST;
|
|
|
|
termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
|
|
|
|
termios_p->c_cflag &= ~(CSIZE|PARENB);
|
|
|
|
termios_p->c_cflag |= CS8;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-02-06 12:58:44 +03:00
|
|
|
static void do_cleanup(int i) {
|
|
|
|
/* unused variable */
|
|
|
|
(void) i;
|
|
|
|
|
|
|
|
tcsetattr(0,TCSANOW,&terminal);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-02-06 12:58:44 +03:00
|
|
|
|
|
|
|
static void do_exit(int i) {
|
|
|
|
/* unused variable */
|
|
|
|
(void) i;
|
|
|
|
|
|
|
|
do_cleanup(0);
|
|
|
|
exit(0);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-02-06 12:58:44 +03:00
|
|
|
|
2009-07-25 00:15:33 +04:00
|
|
|
ssh_channel chan;
|
2005-07-05 05:21:44 +04:00
|
|
|
int signal_delayed=0;
|
2009-02-06 12:58:44 +03:00
|
|
|
|
|
|
|
static void sigwindowchanged(int i){
|
|
|
|
(void) i;
|
|
|
|
signal_delayed=1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-02-06 12:58:44 +03:00
|
|
|
|
|
|
|
static void setsignal(void){
|
|
|
|
signal(SIGWINCH, sigwindowchanged);
|
|
|
|
signal_delayed=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sizechanged(void){
|
2005-07-05 05:21:44 +04:00
|
|
|
struct winsize win = { 0, 0, 0, 0 };
|
|
|
|
ioctl(1, TIOCGWINSZ, &win);
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_channel_change_pty_size(chan,win.ws_col, win.ws_row);
|
2005-07-05 05:21:44 +04:00
|
|
|
// printf("Changed pty size\n");
|
|
|
|
setsignal();
|
|
|
|
}
|
2009-11-08 22:00:23 +03:00
|
|
|
|
|
|
|
/* There are two flavors of select loop: the one based on
|
|
|
|
* ssh_select and the one based on channel_select.
|
|
|
|
* The ssh_select one permits you to give your own file descriptors to
|
|
|
|
* follow. It is thus a complete select loop.
|
|
|
|
* The second one only selects on channels. It is simplier to use
|
|
|
|
* but doesn't permit you to fill in your own file descriptor. It is
|
|
|
|
* more adapted if you can't use ssh_select as a main loop (because
|
|
|
|
* you already have another main loop system).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_CHANNEL_SELECT
|
|
|
|
|
|
|
|
/* channel_select base main loop, with a standard select(2)
|
|
|
|
*/
|
2009-09-23 23:55:54 +04:00
|
|
|
static void select_loop(ssh_session session,ssh_channel channel){
|
2005-07-05 05:21:44 +04:00
|
|
|
fd_set fds;
|
|
|
|
struct timeval timeout;
|
2009-11-07 19:35:24 +03:00
|
|
|
char buffer[4096];
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer readbuf=ssh_buffer_new();
|
2009-07-25 00:15:33 +04:00
|
|
|
ssh_channel channels[2];
|
2005-07-05 05:21:44 +04:00
|
|
|
int lus;
|
|
|
|
int eof=0;
|
2005-10-26 03:02:25 +04:00
|
|
|
int maxfd;
|
2005-07-05 05:21:44 +04:00
|
|
|
int ret;
|
|
|
|
while(channel){
|
|
|
|
/* when a signal is caught, ssh_select will return
|
2009-10-09 23:44:05 +04:00
|
|
|
* with SSH_EINTR, which means it should be started
|
2005-07-05 05:21:44 +04:00
|
|
|
* again. It lets you handle the signal the faster you
|
|
|
|
* can, like in this window changed example. Of course, if
|
|
|
|
* your signal handler doesn't call libssh at all, you're
|
|
|
|
* free to handle signals directly in sighandler.
|
|
|
|
*/
|
|
|
|
do{
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
if(!eof)
|
|
|
|
FD_SET(0,&fds);
|
|
|
|
timeout.tv_sec=30;
|
|
|
|
timeout.tv_usec=0;
|
2005-10-26 03:02:25 +04:00
|
|
|
FD_SET(ssh_get_fd(session),&fds);
|
|
|
|
maxfd=ssh_get_fd(session)+1;
|
|
|
|
ret=select(maxfd,&fds,NULL,NULL,&timeout);
|
|
|
|
if(ret==EINTR)
|
|
|
|
continue;
|
|
|
|
if(FD_ISSET(0,&fds)){
|
2009-11-07 19:35:24 +03:00
|
|
|
lus=read(0,buffer,sizeof(buffer));
|
2005-10-26 03:02:25 +04:00
|
|
|
if(lus)
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_channel_write(channel,buffer,lus);
|
2005-10-26 03:02:25 +04:00
|
|
|
else {
|
|
|
|
eof=1;
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_channel_send_eof(channel);
|
2005-10-26 03:02:25 +04:00
|
|
|
}
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2005-10-26 03:02:25 +04:00
|
|
|
if(FD_ISSET(ssh_get_fd(session),&fds)){
|
|
|
|
ssh_set_fd_toread(session);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2005-10-26 03:02:25 +04:00
|
|
|
channels[0]=channel; // set the first channel we want to read from
|
|
|
|
channels[1]=NULL;
|
2010-05-14 02:51:08 +04:00
|
|
|
ret=ssh_channel_select(channels,NULL,NULL,NULL); // no specific timeout - just poll
|
2005-10-26 03:02:25 +04:00
|
|
|
if(signal_delayed)
|
|
|
|
sizechanged();
|
|
|
|
} while (ret==EINTR || ret==SSH_EINTR);
|
|
|
|
|
|
|
|
// we already looked for input from stdin. Now, we are looking for input from the channel
|
2009-10-09 23:44:05 +04:00
|
|
|
|
2010-05-14 02:51:08 +04:00
|
|
|
if(channel && ssh_channel_is_closed(channel)){
|
|
|
|
ssh_channel_free(channel);
|
2005-10-26 03:02:25 +04:00
|
|
|
channel=NULL;
|
|
|
|
channels[0]=NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2005-10-26 03:02:25 +04:00
|
|
|
if(channels[0]){
|
2010-05-14 02:51:08 +04:00
|
|
|
while(channel && ssh_channel_is_open(channel) && ssh_channel_poll(channel,0)>0){
|
2009-05-04 14:06:49 +04:00
|
|
|
lus=channel_read_buffer(channel,readbuf,0,0);
|
2005-07-05 05:21:44 +04:00
|
|
|
if(lus==-1){
|
2009-03-12 18:52:17 +03:00
|
|
|
fprintf(stderr, "Error reading channel: %s\n",
|
|
|
|
ssh_get_error(session));
|
2005-07-05 05:21:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(lus==0){
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_channel_free(channel);
|
2005-10-26 03:02:25 +04:00
|
|
|
channel=channels[0]=NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
} else
|
2010-05-14 02:51:08 +04:00
|
|
|
if (write(1,ssh_buffer_get_begin(readbuf),lus) < 0) {
|
2010-03-30 00:44:20 +04:00
|
|
|
fprintf(stderr, "Error writing to buffer\n");
|
|
|
|
return;
|
|
|
|
}
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
while(channel && ssh_channel_is_open(channel) && ssh_channel_poll(channel,1)>0){ /* stderr */
|
2009-05-04 14:06:49 +04:00
|
|
|
lus=channel_read_buffer(channel,readbuf,0,1);
|
2005-07-05 05:21:44 +04:00
|
|
|
if(lus==-1){
|
2009-03-12 18:52:17 +03:00
|
|
|
fprintf(stderr, "Error reading channel: %s\n",
|
|
|
|
ssh_get_error(session));
|
2005-07-05 05:21:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(lus==0){
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_channel_free(channel);
|
2005-10-26 03:02:25 +04:00
|
|
|
channel=channels[0]=NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
} else
|
2010-05-14 02:51:08 +04:00
|
|
|
if (write(2,ssh_buffer_get_begin(readbuf),lus) < 0) {
|
2010-03-30 00:44:20 +04:00
|
|
|
fprintf(stderr, "Error writing to buffer\n");
|
|
|
|
return;
|
|
|
|
}
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
if(channel && ssh_channel_is_closed(channel)){
|
|
|
|
ssh_channel_free(channel);
|
2005-07-05 05:21:44 +04:00
|
|
|
channel=NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_buffer_free(readbuf);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2009-11-08 22:00:23 +03:00
|
|
|
#else /* CHANNEL_SELECT */
|
2005-07-05 05:21:44 +04:00
|
|
|
|
2009-11-08 22:00:23 +03:00
|
|
|
static void select_loop(ssh_session session,ssh_channel channel){
|
|
|
|
fd_set fds;
|
|
|
|
struct timeval timeout;
|
|
|
|
char buffer[4096];
|
|
|
|
/* channels will be set to the channels to poll.
|
|
|
|
* outchannels will contain the result of the poll
|
|
|
|
*/
|
|
|
|
ssh_channel channels[2], outchannels[2];
|
|
|
|
int lus;
|
|
|
|
int eof=0;
|
|
|
|
int maxfd;
|
2011-08-30 23:14:30 +04:00
|
|
|
unsigned int r;
|
2009-11-08 22:00:23 +03:00
|
|
|
int ret;
|
|
|
|
while(channel){
|
|
|
|
do{
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
if(!eof)
|
|
|
|
FD_SET(0,&fds);
|
|
|
|
timeout.tv_sec=30;
|
|
|
|
timeout.tv_usec=0;
|
|
|
|
FD_SET(ssh_get_fd(session),&fds);
|
|
|
|
maxfd=ssh_get_fd(session)+1;
|
|
|
|
channels[0]=channel; // set the first channel we want to read from
|
|
|
|
channels[1]=NULL;
|
|
|
|
ret=ssh_select(channels,outchannels,maxfd,&fds,&timeout);
|
|
|
|
if(signal_delayed)
|
|
|
|
sizechanged();
|
|
|
|
if(ret==EINTR)
|
|
|
|
continue;
|
|
|
|
if(FD_ISSET(0,&fds)){
|
|
|
|
lus=read(0,buffer,sizeof(buffer));
|
|
|
|
if(lus)
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_channel_write(channel,buffer,lus);
|
2009-11-08 22:00:23 +03:00
|
|
|
else {
|
|
|
|
eof=1;
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_channel_send_eof(channel);
|
2009-11-08 22:00:23 +03:00
|
|
|
}
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
if(channel && ssh_channel_is_closed(channel)){
|
|
|
|
ssh_channel_free(channel);
|
2009-11-08 22:00:23 +03:00
|
|
|
channel=NULL;
|
|
|
|
channels[0]=NULL;
|
|
|
|
}
|
|
|
|
if(outchannels[0]){
|
2011-08-30 23:14:30 +04:00
|
|
|
while(channel && ssh_channel_is_open(channel) && (r = ssh_channel_poll(channel,0))!=0){
|
|
|
|
lus=ssh_channel_read(channel,buffer,sizeof(buffer) > r ? r : sizeof(buffer),0);
|
2009-11-08 22:00:23 +03:00
|
|
|
if(lus==-1){
|
|
|
|
fprintf(stderr, "Error reading channel: %s\n",
|
|
|
|
ssh_get_error(session));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(lus==0){
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_channel_free(channel);
|
2009-11-08 22:00:23 +03:00
|
|
|
channel=channels[0]=NULL;
|
|
|
|
} else
|
2010-04-25 01:22:00 +04:00
|
|
|
if (write(1,buffer,lus) < 0) {
|
|
|
|
fprintf(stderr, "Error writing to buffer\n");
|
|
|
|
return;
|
|
|
|
}
|
2009-11-08 22:00:23 +03:00
|
|
|
}
|
2011-08-30 23:14:30 +04:00
|
|
|
while(channel && ssh_channel_is_open(channel) && (r = ssh_channel_poll(channel,1))!=0){ /* stderr */
|
|
|
|
lus=ssh_channel_read(channel,buffer,sizeof(buffer) > r ? r : sizeof(buffer),1);
|
2009-11-08 22:00:23 +03:00
|
|
|
if(lus==-1){
|
|
|
|
fprintf(stderr, "Error reading channel: %s\n",
|
|
|
|
ssh_get_error(session));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(lus==0){
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_channel_free(channel);
|
2009-11-08 22:00:23 +03:00
|
|
|
channel=channels[0]=NULL;
|
|
|
|
} else
|
2010-04-25 01:22:00 +04:00
|
|
|
if (write(2,buffer,lus) < 0) {
|
|
|
|
fprintf(stderr, "Error writing to buffer\n");
|
|
|
|
return;
|
|
|
|
}
|
2009-11-08 22:00:23 +03:00
|
|
|
}
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
if(channel && ssh_channel_is_closed(channel)){
|
|
|
|
ssh_channel_free(channel);
|
2009-11-08 22:00:23 +03:00
|
|
|
channel=NULL;
|
|
|
|
}
|
|
|
|
} while (ret==EINTR || ret==SSH_EINTR);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2005-10-26 03:02:25 +04:00
|
|
|
|
2009-09-23 23:55:54 +04:00
|
|
|
static void shell(ssh_session session){
|
2009-07-25 00:15:33 +04:00
|
|
|
ssh_channel channel;
|
2005-07-05 05:21:44 +04:00
|
|
|
struct termios terminal_local;
|
|
|
|
int interactive=isatty(0);
|
2010-05-14 02:51:08 +04:00
|
|
|
channel = ssh_channel_new(session);
|
2005-07-05 05:21:44 +04:00
|
|
|
if(interactive){
|
|
|
|
tcgetattr(0,&terminal_local);
|
|
|
|
memcpy(&terminal,&terminal_local,sizeof(struct termios));
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
if(ssh_channel_open_session(channel)){
|
2005-08-10 17:22:52 +04:00
|
|
|
printf("error opening channel : %s\n",ssh_get_error(session));
|
|
|
|
return;
|
|
|
|
}
|
2005-07-05 05:21:44 +04:00
|
|
|
chan=channel;
|
|
|
|
if(interactive){
|
2010-05-14 02:51:08 +04:00
|
|
|
ssh_channel_request_pty(channel);
|
2005-07-05 05:21:44 +04:00
|
|
|
sizechanged();
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
if(ssh_channel_request_shell(channel)){
|
2005-08-10 17:22:52 +04:00
|
|
|
printf("Requesting shell : %s\n",ssh_get_error(session));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(interactive){
|
|
|
|
cfmakeraw(&terminal_local);
|
|
|
|
tcsetattr(0,TCSANOW,&terminal_local);
|
|
|
|
setsignal();
|
|
|
|
}
|
|
|
|
signal(SIGTERM,do_cleanup);
|
2005-07-05 05:21:44 +04:00
|
|
|
select_loop(session,channel);
|
2009-11-06 21:21:18 +03:00
|
|
|
if(interactive)
|
|
|
|
do_cleanup(0);
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2009-09-23 23:55:54 +04:00
|
|
|
static void batch_shell(ssh_session session){
|
2009-07-25 00:15:33 +04:00
|
|
|
ssh_channel channel;
|
2005-07-05 05:21:44 +04:00
|
|
|
char buffer[1024];
|
|
|
|
int i,s=0;
|
2011-04-27 02:56:00 +04:00
|
|
|
for(i=0;i<MAXCMD && cmds[i];++i) {
|
2005-07-05 05:21:44 +04:00
|
|
|
s+=snprintf(buffer+s,sizeof(buffer)-s,"%s ",cmds[i]);
|
2011-04-27 02:56:00 +04:00
|
|
|
free(cmds[i]);
|
|
|
|
cmds[i] = NULL;
|
|
|
|
}
|
2010-05-14 02:51:08 +04:00
|
|
|
channel=ssh_channel_new(session);
|
|
|
|
ssh_channel_open_session(channel);
|
|
|
|
if(ssh_channel_request_exec(channel,buffer)){
|
2005-07-05 05:21:44 +04:00
|
|
|
printf("error executing \"%s\" : %s\n",buffer,ssh_get_error(session));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
select_loop(session,channel);
|
|
|
|
}
|
2009-04-27 14:58:29 +04:00
|
|
|
|
2009-11-07 19:23:06 +03:00
|
|
|
static int client(ssh_session session){
|
|
|
|
int auth=0;
|
|
|
|
char *banner;
|
|
|
|
int state;
|
|
|
|
if (user)
|
|
|
|
if (ssh_options_set(session, SSH_OPTIONS_USER, user) < 0)
|
|
|
|
return -1;
|
|
|
|
if (ssh_options_set(session, SSH_OPTIONS_HOST ,host) < 0)
|
|
|
|
return -1;
|
2010-05-11 00:44:06 +04:00
|
|
|
if (proxycommand != NULL){
|
|
|
|
if(ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, proxycommand))
|
|
|
|
return -1;
|
|
|
|
}
|
2009-11-07 19:23:06 +03:00
|
|
|
ssh_options_parse_config(session, NULL);
|
|
|
|
|
|
|
|
if(ssh_connect(session)){
|
|
|
|
fprintf(stderr,"Connection failed : %s\n",ssh_get_error(session));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
state=verify_knownhost(session);
|
|
|
|
if (state != 0)
|
|
|
|
return -1;
|
|
|
|
ssh_userauth_none(session, NULL);
|
|
|
|
banner=ssh_get_issue_banner(session);
|
|
|
|
if(banner){
|
|
|
|
printf("%s\n",banner);
|
|
|
|
free(banner);
|
|
|
|
}
|
|
|
|
auth=authenticate_console(session);
|
|
|
|
if(auth != SSH_AUTH_SUCCESS){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(!cmds[0])
|
|
|
|
shell(session);
|
|
|
|
else
|
|
|
|
batch_shell(session);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-17 00:36:31 +03:00
|
|
|
ssh_pcap_file pcap;
|
|
|
|
void set_pcap(ssh_session session);
|
|
|
|
void set_pcap(ssh_session session){
|
|
|
|
if(!pcap_file)
|
|
|
|
return;
|
|
|
|
pcap=ssh_pcap_file_new();
|
2009-11-27 20:08:53 +03:00
|
|
|
if(!pcap)
|
|
|
|
return;
|
2009-11-17 00:36:31 +03:00
|
|
|
if(ssh_pcap_file_open(pcap,pcap_file) == SSH_ERROR){
|
|
|
|
printf("Error opening pcap file\n");
|
|
|
|
ssh_pcap_file_free(pcap);
|
|
|
|
pcap=NULL;
|
|
|
|
return;
|
|
|
|
}
|
2009-11-17 01:20:16 +03:00
|
|
|
ssh_set_pcap_file(session,pcap);
|
2009-11-17 00:36:31 +03:00
|
|
|
}
|
2009-11-17 01:20:16 +03:00
|
|
|
|
2009-11-17 00:36:31 +03:00
|
|
|
void cleanup_pcap(void);
|
|
|
|
void cleanup_pcap(){
|
2009-11-27 20:08:53 +03:00
|
|
|
if(pcap)
|
|
|
|
ssh_pcap_file_free(pcap);
|
2009-11-17 00:36:31 +03:00
|
|
|
pcap=NULL;
|
|
|
|
}
|
|
|
|
|
2005-07-05 05:21:44 +04:00
|
|
|
int main(int argc, char **argv){
|
2009-09-23 23:55:54 +04:00
|
|
|
ssh_session session;
|
2009-11-07 19:23:06 +03:00
|
|
|
|
2009-10-02 16:06:41 +04:00
|
|
|
session = ssh_new();
|
|
|
|
|
2009-10-10 18:25:40 +04:00
|
|
|
ssh_callbacks_init(&cb);
|
|
|
|
ssh_set_callbacks(session,&cb);
|
2009-10-10 13:55:42 +04:00
|
|
|
|
2009-10-02 16:06:41 +04:00
|
|
|
if(ssh_options_getopt(session, &argc, argv)) {
|
|
|
|
fprintf(stderr, "error parsing command line :%s\n",
|
|
|
|
ssh_get_error(session));
|
|
|
|
usage();
|
2008-03-04 07:25:48 +03:00
|
|
|
}
|
2005-07-05 05:21:44 +04:00
|
|
|
opts(argc,argv);
|
2009-02-06 12:58:44 +03:00
|
|
|
signal(SIGTERM, do_exit);
|
2009-11-27 20:08:53 +03:00
|
|
|
|
2009-11-17 00:36:31 +03:00
|
|
|
set_pcap(session);
|
2009-11-07 19:23:06 +03:00
|
|
|
client(session);
|
2009-11-17 00:36:31 +03:00
|
|
|
|
2005-07-05 05:21:44 +04:00
|
|
|
ssh_disconnect(session);
|
2009-10-10 14:04:16 +04:00
|
|
|
ssh_free(session);
|
2009-11-17 00:36:31 +03:00
|
|
|
cleanup_pcap();
|
|
|
|
|
2006-07-09 14:36:44 +04:00
|
|
|
ssh_finalize();
|
2005-07-05 05:21:44 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|