1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-06-10 22:21:40 +03:00

examples: Reformat ssh_client

The example should be clean code if possible.

Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Andreas Schneider
2018-08-31 10:20:25 +02:00
parent 8d8b64cc3f
commit 2b05e46b62

View File

@ -48,12 +48,17 @@ static char *user;
static char *cmds[MAXCMD]; static char *cmds[MAXCMD];
static struct termios terminal; static struct termios terminal;
static char *pcap_file=NULL; static char *pcap_file = NULL;
static char *proxycommand; static char *proxycommand;
static int auth_callback(const char *prompt, char *buf, size_t len, static int auth_callback(const char *prompt,
int echo, int verify, void *userdata) { char *buf,
size_t len,
int echo,
int verify,
void *userdata)
{
(void) verify; (void) verify;
(void) userdata; (void) userdata;
@ -61,11 +66,12 @@ static int auth_callback(const char *prompt, char *buf, size_t len,
} }
struct ssh_callbacks_struct cb = { struct ssh_callbacks_struct cb = {
.auth_function=auth_callback, .auth_function = auth_callback,
.userdata=NULL .userdata = NULL,
}; };
static void add_cmd(char *cmd){ static void add_cmd(char *cmd)
{
int n; int n;
for (n = 0; (n < MAXCMD) && cmds[n] != NULL; n++); for (n = 0; (n < MAXCMD) && cmds[n] != NULL; n++);
@ -73,7 +79,8 @@ static void add_cmd(char *cmd){
if (n == MAXCMD) { if (n == MAXCMD) {
return; return;
} }
cmds[n]=strdup(cmd);
cmds[n] = strdup(cmd);
} }
static void usage(void) static void usage(void)
@ -98,37 +105,43 @@ static void usage(void)
exit(0); exit(0);
} }
static int opts(int argc, char **argv){ static int opts(int argc, char **argv)
{
int i; int i;
// for(i=0;i<argc;i++)
// printf("%d : %s\n",i,argv[i]); while((i = getopt(argc,argv,"T:P:")) != -1) {
/* insert your own arguments here */
while((i=getopt(argc,argv,"T:P:"))!=-1){
switch(i){ switch(i){
case 'P': case 'P':
pcap_file=optarg; pcap_file = optarg;
break; break;
#ifndef _WIN32 #ifndef _WIN32
case 'T': case 'T':
proxycommand=optarg; proxycommand = optarg;
break; break;
#endif #endif
default: default:
fprintf(stderr,"unknown option %c\n",optopt); fprintf(stderr, "Unknown option %c\n", optopt);
usage(); usage();
} }
} }
if(optind < argc) if (optind < argc) {
host=argv[optind++]; host = argv[optind++];
while(optind < argc) }
while(optind < argc) {
add_cmd(argv[optind++]); add_cmd(argv[optind++]);
if(host==NULL) }
if (host == NULL) {
usage(); usage();
}
return 0; return 0;
} }
#ifndef HAVE_CFMAKERAW #ifndef HAVE_CFMAKERAW
static void cfmakeraw(struct termios *termios_p){ static void cfmakeraw(struct termios *termios_p)
{
termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
termios_p->c_oflag &= ~OPOST; termios_p->c_oflag &= ~OPOST;
termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
@ -138,44 +151,53 @@ static void cfmakeraw(struct termios *termios_p){
#endif #endif
static void do_cleanup(int i) { static void do_cleanup(int i)
{
/* unused variable */ /* unused variable */
(void) i; (void) i;
tcsetattr(0,TCSANOW,&terminal); tcsetattr(0, TCSANOW, &terminal);
} }
static void do_exit(int i) { static void do_exit(int i)
/* unused variable */ {
(void) i; /* unused variable */
(void) i;
do_cleanup(0); do_cleanup(0);
exit(0); exit(0);
} }
ssh_channel chan; static ssh_channel chan;
int signal_delayed=0; static int signal_delayed = 0;
static void sigwindowchanged(int i){ static void sigwindowchanged(int i)
(void) i; {
signal_delayed=1; (void) i;
signal_delayed = 1;
} }
static void setsignal(void){ static void setsignal(void)
{
signal(SIGWINCH, sigwindowchanged); signal(SIGWINCH, sigwindowchanged);
signal_delayed=0; signal_delayed = 0;
} }
static void sizechanged(void){ static void sizechanged(void)
struct winsize win = { 0, 0, 0, 0 }; {
struct winsize win = {
.ws_row = 0,
};
ioctl(1, TIOCGWINSZ, &win); ioctl(1, TIOCGWINSZ, &win);
ssh_channel_change_pty_size(chan,win.ws_col, win.ws_row); ssh_channel_change_pty_size(chan,win.ws_col, win.ws_row);
// printf("Changed pty size\n");
setsignal(); setsignal();
} }
static void select_loop(ssh_session session,ssh_channel channel){ static void select_loop(ssh_session session,ssh_channel channel)
{
ssh_connector connector_in, connector_out, connector_err; ssh_connector connector_in, connector_out, connector_err;
ssh_event event = ssh_event_new(); ssh_event event = ssh_event_new();
/* stdin */ /* stdin */
@ -196,9 +218,10 @@ static void select_loop(ssh_session session,ssh_channel channel){
ssh_connector_set_in_channel(connector_err, channel, SSH_CONNECTOR_STDERR); ssh_connector_set_in_channel(connector_err, channel, SSH_CONNECTOR_STDERR);
ssh_event_add_connector(event, connector_err); ssh_event_add_connector(event, connector_err);
while(ssh_channel_is_open(channel)){ while (ssh_channel_is_open(channel)) {
if(signal_delayed) if (signal_delayed) {
sizechanged(); sizechanged();
}
ssh_event_dopoll(event, 60000); ssh_event_dopoll(event, 60000);
} }
ssh_event_remove_connector(event, connector_in); ssh_event_remove_connector(event, connector_in);
@ -213,121 +236,148 @@ static void select_loop(ssh_session session,ssh_channel channel){
ssh_channel_free(channel); ssh_channel_free(channel);
} }
static void shell(ssh_session session){ static void shell(ssh_session session)
{
ssh_channel channel; ssh_channel channel;
struct termios terminal_local; struct termios terminal_local;
int interactive=isatty(0); int interactive=isatty(0);
channel = ssh_channel_new(session); channel = ssh_channel_new(session);
if(interactive){
tcgetattr(0,&terminal_local); if (interactive) {
memcpy(&terminal,&terminal_local,sizeof(struct termios)); tcgetattr(0, &terminal_local);
memcpy(&terminal, &terminal_local, sizeof(struct termios));
} }
if(ssh_channel_open_session(channel)){
printf("error opening channel : %s\n",ssh_get_error(session)); if (ssh_channel_open_session(channel)) {
printf("Error opening channel : %s\n", ssh_get_error(session));
return; return;
} }
chan=channel; chan = channel;
if(interactive){ if (interactive) {
ssh_channel_request_pty(channel); ssh_channel_request_pty(channel);
sizechanged(); sizechanged();
} }
if(ssh_channel_request_shell(channel)){
printf("Requesting shell : %s\n",ssh_get_error(session)); if (ssh_channel_request_shell(channel)) {
printf("Requesting shell : %s\n", ssh_get_error(session));
return; return;
} }
if(interactive){
if (interactive) {
cfmakeraw(&terminal_local); cfmakeraw(&terminal_local);
tcsetattr(0,TCSANOW,&terminal_local); tcsetattr(0, TCSANOW, &terminal_local);
setsignal(); setsignal();
} }
signal(SIGTERM,do_cleanup); signal(SIGTERM, do_cleanup);
select_loop(session,channel); select_loop(session, channel);
if(interactive) if (interactive) {
do_cleanup(0); do_cleanup(0);
}
} }
static void batch_shell(ssh_session session){ static void batch_shell(ssh_session session)
{
ssh_channel channel; ssh_channel channel;
char buffer[1024]; char buffer[1024];
int i,s=0; size_t i;
for(i=0;i<MAXCMD && cmds[i];++i) { int s = 0;
s+=snprintf(buffer+s,sizeof(buffer)-s,"%s ",cmds[i]);
free(cmds[i]); for (i = 0; i < MAXCMD && cmds[i]; ++i) {
cmds[i] = NULL; s += snprintf(buffer + s, sizeof(buffer) - s, "%s ", cmds[i]);
} free(cmds[i]);
channel=ssh_channel_new(session); cmds[i] = NULL;
}
channel = ssh_channel_new(session);
ssh_channel_open_session(channel); ssh_channel_open_session(channel);
if(ssh_channel_request_exec(channel,buffer)){ if (ssh_channel_request_exec(channel, buffer)) {
printf("error executing \"%s\" : %s\n",buffer,ssh_get_error(session)); printf("Error executing '%s' : %s\n", buffer, ssh_get_error(session));
return; return;
} }
select_loop(session,channel); select_loop(session, channel);
} }
static int client(ssh_session session){ static int client(ssh_session session)
int auth=0; {
char *banner; int auth = 0;
int state; char *banner;
if (user) int state;
if (ssh_options_set(session, SSH_OPTIONS_USER, user) < 0)
return -1;
if (ssh_options_set(session, SSH_OPTIONS_HOST ,host) < 0)
return -1;
if (proxycommand != NULL){
if(ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, proxycommand))
return -1;
}
ssh_options_parse_config(session, NULL);
if(ssh_connect(session)){ if (user) {
fprintf(stderr,"Connection failed : %s\n",ssh_get_error(session)); if (ssh_options_set(session, SSH_OPTIONS_USER, user) < 0) {
return -1; return -1;
} }
state=verify_knownhost(session); }
if (state != 0) if (ssh_options_set(session, SSH_OPTIONS_HOST ,host) < 0) {
return -1; return -1;
ssh_userauth_none(session, NULL); }
banner=ssh_get_issue_banner(session); if (proxycommand != NULL) {
if(banner){ if (ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, proxycommand)) {
printf("%s\n",banner); return -1;
free(banner); }
} }
auth=authenticate_console(session); ssh_options_parse_config(session, NULL);
if(auth != SSH_AUTH_SUCCESS){
return -1; if (ssh_connect(session)) {
} fprintf(stderr, "Connection failed : %s\n", ssh_get_error(session));
if(!cmds[0]) return -1;
shell(session); }
else
batch_shell(session); state = verify_knownhost(session);
return 0; 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] == NULL) {
shell(session);
} else {
batch_shell(session);
}
return 0;
} }
ssh_pcap_file pcap; static ssh_pcap_file pcap;
void set_pcap(ssh_session session); static void set_pcap(ssh_session session)
void set_pcap(ssh_session session){ {
if(!pcap_file) if (pcap_file == NULL) {
return; return;
pcap=ssh_pcap_file_new(); }
if(!pcap)
return; pcap = ssh_pcap_file_new();
if(ssh_pcap_file_open(pcap,pcap_file) == SSH_ERROR){ if (pcap == NULL) {
printf("Error opening pcap file\n"); return;
ssh_pcap_file_free(pcap); }
pcap=NULL;
return; if (ssh_pcap_file_open(pcap, pcap_file) == SSH_ERROR) {
} printf("Error opening pcap file\n");
ssh_set_pcap_file(session,pcap); ssh_pcap_file_free(pcap);
pcap = NULL;
return;
}
ssh_set_pcap_file(session, pcap);
} }
void cleanup_pcap(void); static void cleanup_pcap(void)
void cleanup_pcap(){ {
if(pcap) if (pcap != NULL) {
ssh_pcap_file_free(pcap); ssh_pcap_file_free(pcap);
pcap=NULL; }
pcap = NULL;
} }
int main(int argc, char **argv){ int main(int argc, char **argv)
{
ssh_session session; ssh_session session;
session = ssh_new(); session = ssh_new();
@ -335,12 +385,13 @@ int main(int argc, char **argv){
ssh_callbacks_init(&cb); ssh_callbacks_init(&cb);
ssh_set_callbacks(session,&cb); ssh_set_callbacks(session,&cb);
if(ssh_options_getopt(session, &argc, argv)) { if (ssh_options_getopt(session, &argc, argv)) {
fprintf(stderr, "error parsing command line :%s\n", fprintf(stderr,
ssh_get_error(session)); "Error parsing command line: %s\n",
usage(); ssh_get_error(session));
usage();
} }
opts(argc,argv); opts(argc, argv);
signal(SIGTERM, do_exit); signal(SIGTERM, do_exit);
set_pcap(session); set_pcap(session);