1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-05 20:55:46 +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

@@ -52,8 +52,13 @@ 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;
@@ -62,10 +67,11 @@ 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,6 +79,7 @@ static void add_cmd(char *cmd){
if (n == MAXCMD) { if (n == MAXCMD) {
return; return;
} }
cmds[n] = strdup(cmd); cmds[n] = strdup(cmd);
} }
@@ -98,11 +105,10 @@ 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]);
/* insert your own arguments here */
while((i = getopt(argc,argv,"T:P:")) != -1) { while((i = getopt(argc,argv,"T:P:")) != -1) {
switch(i){ switch(i){
case 'P': case 'P':
@@ -114,21 +120,28 @@ static int opts(int argc, char **argv){
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,14 +151,16 @@ 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 */ /* unused variable */
(void) i; (void) i;
@@ -153,29 +168,36 @@ static void do_exit(int i) {
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; (void) i;
signal_delayed = 1; 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 */
@@ -197,8 +219,9 @@ static void select_loop(ssh_session session,ssh_channel channel){
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,17 +236,20 @@ 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) { if (interactive) {
tcgetattr(0, &terminal_local); tcgetattr(0, &terminal_local);
memcpy(&terminal, &terminal_local, sizeof(struct termios)); memcpy(&terminal, &terminal_local, sizeof(struct termios));
} }
if (ssh_channel_open_session(channel)) { if (ssh_channel_open_session(channel)) {
printf("error opening channel : %s\n",ssh_get_error(session)); printf("Error opening channel : %s\n", ssh_get_error(session));
return; return;
} }
chan = channel; chan = channel;
@@ -231,10 +257,12 @@ static void shell(ssh_session session){
ssh_channel_request_pty(channel); ssh_channel_request_pty(channel);
sizechanged(); sizechanged();
} }
if (ssh_channel_request_shell(channel)) { if (ssh_channel_request_shell(channel)) {
printf("Requesting shell : %s\n", ssh_get_error(session)); 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);
@@ -242,50 +270,64 @@ static void shell(ssh_session session){
} }
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;
int s = 0;
for (i = 0; i < MAXCMD && cmds[i]; ++i) { for (i = 0; i < MAXCMD && cmds[i]; ++i) {
s += snprintf(buffer + s, sizeof(buffer) - s, "%s ", cmds[i]); s += snprintf(buffer + s, sizeof(buffer) - s, "%s ", cmds[i]);
free(cmds[i]); free(cmds[i]);
cmds[i] = NULL; cmds[i] = NULL;
} }
channel = ssh_channel_new(session); 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; int auth = 0;
char *banner; char *banner;
int state; int state;
if (user)
if (ssh_options_set(session, SSH_OPTIONS_USER, user) < 0) if (user) {
if (ssh_options_set(session, SSH_OPTIONS_USER, user) < 0) {
return -1; return -1;
if (ssh_options_set(session, SSH_OPTIONS_HOST ,host) < 0) }
}
if (ssh_options_set(session, SSH_OPTIONS_HOST ,host) < 0) {
return -1; return -1;
}
if (proxycommand != NULL) { if (proxycommand != NULL) {
if(ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, proxycommand)) if (ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, proxycommand)) {
return -1; return -1;
} }
}
ssh_options_parse_config(session, NULL); ssh_options_parse_config(session, NULL);
if (ssh_connect(session)) { if (ssh_connect(session)) {
fprintf(stderr, "Connection failed : %s\n", ssh_get_error(session)); fprintf(stderr, "Connection failed : %s\n", ssh_get_error(session));
return -1; return -1;
} }
state = verify_knownhost(session); state = verify_knownhost(session);
if (state != 0) if (state != 0) {
return -1; return -1;
}
ssh_userauth_none(session, NULL); ssh_userauth_none(session, NULL);
banner = ssh_get_issue_banner(session); banner = ssh_get_issue_banner(session);
if (banner) { if (banner) {
@@ -296,21 +338,27 @@ static int client(ssh_session session){
if (auth != SSH_AUTH_SUCCESS) { if (auth != SSH_AUTH_SUCCESS) {
return -1; return -1;
} }
if(!cmds[0]) if (cmds[0] == NULL) {
shell(session); shell(session);
else } else {
batch_shell(session); batch_shell(session);
}
return 0; 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(); pcap = ssh_pcap_file_new();
if(!pcap) if (pcap == NULL) {
return; return;
}
if (ssh_pcap_file_open(pcap, pcap_file) == SSH_ERROR) { if (ssh_pcap_file_open(pcap, pcap_file) == SSH_ERROR) {
printf("Error opening pcap file\n"); printf("Error opening pcap file\n");
ssh_pcap_file_free(pcap); ssh_pcap_file_free(pcap);
@@ -320,14 +368,16 @@ void set_pcap(ssh_session session){
ssh_set_pcap_file(session, pcap); 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();
@@ -336,7 +386,8 @@ int main(int argc, char **argv){
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,
"Error parsing command line: %s\n",
ssh_get_error(session)); ssh_get_error(session));
usage(); usage();
} }