1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-07-29 13:01:13 +03:00

examples: Fix formatting

Signed-off-by: Norbert Pocs <npocs@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Norbert Pocs
2023-07-12 09:06:10 +02:00
parent 5726af1956
commit 393a9bf82c

View File

@ -119,7 +119,8 @@ pthread_mutex_t mutex;
*/ */
/* Linked nodes to manage channel/fd tuples */ /* Linked nodes to manage channel/fd tuples */
static int insert_item(ssh_channel channel, int fd_in, int fd_out, int protected); static int insert_item(ssh_channel channel, int fd_in, int fd_out,
int protected);
static void delete_item(ssh_channel channel); static void delete_item(ssh_channel channel);
static node_t * search_item(ssh_channel channel); static node_t * search_item(ssh_channel channel);
@ -135,13 +136,18 @@ static int x11_connect_display(void);
static int copy_fd_to_channel_callback(int fd, int revents, void *userdata); static int copy_fd_to_channel_callback(int fd, int revents, void *userdata);
/* Read data from channel */ /* Read data from channel */
static int copy_channel_to_fd_callback(ssh_session session, ssh_channel channel, void *data, uint32_t len, int is_stderr, void *userdata); static int copy_channel_to_fd_callback(ssh_session session, ssh_channel channel,
void *data, uint32_t len, int is_stderr,
void *userdata);
/* EOF&Close channel */ /* EOF&Close channel */
static void channel_close_callback(ssh_session session, ssh_channel channel, void *userdata); static void channel_close_callback(ssh_session session, ssh_channel channel,
void *userdata);
/* X11 Request */ /* X11 Request */
static ssh_channel x11_open_request_callback(ssh_session session, const char *shost, int sport, void *userdata); static ssh_channel x11_open_request_callback(ssh_session session,
const char *shost, int sport,
void *userdata);
/* Main loop */ /* Main loop */
static int main_loop(ssh_channel channel); static int main_loop(ssh_channel channel);
@ -193,8 +199,8 @@ struct termios _saved_tio;
* Internal functions * Internal functions
*/ */
int64_t int64_t _current_timestamp(void)
_current_timestamp(void) { {
struct timeval tv; struct timeval tv;
int64_t milliseconds; int64_t milliseconds;
@ -204,8 +210,8 @@ _current_timestamp(void) {
return milliseconds; return milliseconds;
} }
static void static void _logging_callback(int priority, const char *function,
_logging_callback(int priority, const char *function, const char *buffer, void *userdata) const char *buffer, void *userdata)
{ {
FILE *fp = NULL; FILE *fp = NULL;
char buf[100]; char buf[100];
@ -218,20 +224,19 @@ _logging_callback(int priority, const char *function, const char *buffer, void *
strftime(buf, 100, "%Y-%m-%d %H:%M:%S", localtime(&now)); strftime(buf, 100, "%Y-%m-%d %H:%M:%S", localtime(&now));
fp = fopen("debug.log","a"); fp = fopen("debug.log","a");
if(fp == NULL) if (fp == NULL) {
{
printf("Error!"); printf("Error!");
exit(-11); exit(-11);
} }
milliseconds = _current_timestamp(); milliseconds = _current_timestamp();
fprintf(fp, "[%s.%jd, %d] %s: %s\n", buf, milliseconds, priority, function, buffer); fprintf(fp, "[%s.%jd, %d] %s: %s\n", buf, milliseconds, priority,
function, buffer);
fclose(fp); fclose(fp);
} }
static int static int _enter_term_raw_mode(void)
_enter_term_raw_mode(void)
{ {
struct termios tio; struct termios tio;
int ret = tcgetattr(fileno(stdin), &tio); int ret = tcgetattr(fileno(stdin), &tio);
@ -251,11 +256,11 @@ _enter_term_raw_mode(void)
tio.c_cc[VTIME] = 0; tio.c_cc[VTIME] = 0;
ret = tcsetattr(fileno(stdin), TCSADRAIN, &tio); ret = tcsetattr(fileno(stdin), TCSADRAIN, &tio);
} }
return ret; return ret;
} }
static int static int _leave_term_raw_mode(void)
_leave_term_raw_mode(void)
{ {
int ret = tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio); int ret = tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio);
return ret; return ret;
@ -266,8 +271,8 @@ _leave_term_raw_mode(void)
* Functions * Functions
*/ */
static int static int insert_item(ssh_channel channel, int fd_in, int fd_out,
insert_item(ssh_channel channel, int fd_in, int fd_out, int protected) int protected)
{ {
node_t *node_iterator = NULL, *new = NULL; node_t *node_iterator = NULL, *new = NULL;
@ -286,8 +291,9 @@ insert_item(ssh_channel channel, int fd_in, int fd_out, int protected)
node->next = NULL; node->next = NULL;
} else { } else {
node_iterator = node; node_iterator = node;
while (node_iterator->next != NULL) while (node_iterator->next != NULL) {
node_iterator = node_iterator->next; node_iterator = node_iterator->next;
}
/* Create the new node */ /* Create the new node */
new = (node_t *) malloc(sizeof(node_t)); new = (node_t *) malloc(sizeof(node_t));
if (new == NULL) { if (new == NULL) {
@ -307,21 +313,22 @@ insert_item(ssh_channel channel, int fd_in, int fd_out, int protected)
} }
static void static void delete_item(ssh_channel channel)
delete_item(ssh_channel channel)
{ {
node_t *current = NULL, *previous = NULL; node_t *current = NULL, *previous = NULL;
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
for (current = node; current; previous = current, current = current->next) { for (current = node; current; previous = current, current = current->next) {
if (current->channel != channel) if (current->channel != channel) {
continue; continue;
}
if (previous == NULL) if (previous == NULL) {
node = current->next; node = current->next;
else } else {
previous->next = current->next; previous->next = current->next;
}
free(current); free(current);
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
@ -332,8 +339,7 @@ delete_item(ssh_channel channel)
} }
static node_t * static node_t *search_item(ssh_channel channel)
search_item(ssh_channel channel)
{ {
node_t *current = node; node_t *current = node;
@ -355,15 +361,17 @@ search_item(ssh_channel channel)
static void static void set_nodelay(int fd)
set_nodelay(int fd)
{ {
int opt; int opt, rc;
socklen_t optlen; socklen_t optlen;
optlen = sizeof(opt); optlen = sizeof(opt);
if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "getsockopt TCP_NODELAY: %.100s", strerror(errno)); rc = getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen);
if (rc == -1) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "getsockopt TCP_NODELAY: %.100s",
strerror(errno));
return; return;
} }
if (opt == 1) { if (opt == 1) {
@ -372,23 +380,26 @@ set_nodelay(int fd)
} }
opt = 1; opt = 1;
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "fd %d setting TCP_NODELAY", fd); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "fd %d setting TCP_NODELAY", fd);
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)) == -1)
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "setsockopt TCP_NODELAY: %.100s", strerror(errno)); rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
if (rc == -1) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "setsockopt TCP_NODELAY: %.100s",
strerror(errno));
}
} }
const char * const char *ssh_gai_strerror(int gaierr)
ssh_gai_strerror(int gaierr)
{ {
if (gaierr == EAI_SYSTEM && errno != 0) if (gaierr == EAI_SYSTEM && errno != 0) {
return strerror(errno); return strerror(errno);
}
return gai_strerror(gaierr); return gai_strerror(gaierr);
} }
static int static int x11_get_proto(const char *display, char **_proto, char **_cookie)
x11_get_proto(const char *display, char **_proto, char **_cookie)
{ {
char cmd[1024], line[512], xdisplay[512]; char cmd[1024], line[512], xdisplay[512];
static char proto[512], cookie[512]; static char proto[512], cookie[512];
@ -401,8 +412,10 @@ x11_get_proto(const char *display, char **_proto, char **_cookie)
proto[0] = cookie[0] = '\0'; proto[0] = cookie[0] = '\0';
if (strncmp(display, "localhost:", 10) == 0) { if (strncmp(display, "localhost:", 10) == 0) {
if ((ret = snprintf(xdisplay, sizeof(xdisplay), "unix:%s", display + 10)) < 0 || (size_t)ret >= sizeof(xdisplay)) { ret = snprintf(xdisplay, sizeof(xdisplay), "unix:%s", display + 10);
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "display name too long. display: %s", display); if (ret < 0 || (size_t)ret >= sizeof(xdisplay)) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__,
"display name too long. display: %s", display);
return -1; return -1;
} }
display = xdisplay; display = xdisplay;
@ -412,28 +425,32 @@ x11_get_proto(const char *display, char **_proto, char **_cookie)
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "xauth cmd: %s", cmd); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "xauth cmd: %s", cmd);
f = popen(cmd, "r"); f = popen(cmd, "r");
if (f && fgets(line, sizeof(line), f) && sscanf(line, "%*s %511s %511s", proto, cookie) == 2) { if (f && fgets(line, sizeof(line), f) &&
sscanf(line, "%*s %511s %511s", proto, cookie) == 2) {
ret = 0; ret = 0;
} else { } else {
ret = 1; ret = 1;
} }
if (f) pclose(f); if (f) {
pclose(f);
}
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "proto: %s - cookie: %s - ret: %d", proto, cookie, ret); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "proto: %s - cookie: %s - ret: %d",
proto, cookie, ret);
return ret; return ret;
} }
static int static int connect_local_xsocket_path(const char *pathname)
connect_local_xsocket_path(const char *pathname)
{ {
int sock; int sock, rc;
struct sockaddr_un addr; struct sockaddr_un addr;
sock = socket(AF_UNIX, SOCK_STREAM, 0); sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) { if (sock == -1) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "socket: %.100s", strerror(errno)); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "socket: %.100s",
strerror(errno));
return -1; return -1;
} }
@ -442,16 +459,20 @@ connect_local_xsocket_path(const char *pathname)
addr.sun_path[0] = '\0'; addr.sun_path[0] = '\0';
/* pathname is guaranteed to be initialized and larger than addr.sun_path[108] */ /* pathname is guaranteed to be initialized and larger than addr.sun_path[108] */
memcpy(addr.sun_path + 1, pathname, sizeof(addr.sun_path) - 1); memcpy(addr.sun_path + 1, pathname, sizeof(addr.sun_path) - 1);
if (connect(sock, (struct sockaddr *)&addr, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(pathname)) == 0) rc = connect(sock, (struct sockaddr *)&addr,
offsetof(struct sockaddr_un, sun_path) + 1 + strlen(pathname));
if (rc == 0) {
return sock; return sock;
}
close(sock); close(sock);
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "connect %.100s: %.100s", addr.sun_path, strerror(errno)); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "connect %.100s: %.100s",
addr.sun_path, strerror(errno));
return -1; return -1;
} }
static int static int connect_local_xsocket(int display_number)
connect_local_xsocket(int display_number)
{ {
char buf[1024] = {0}; char buf[1024] = {0};
snprintf(buf, sizeof(buf), _PATH_UNIX_X, display_number); snprintf(buf, sizeof(buf), _PATH_UNIX_X, display_number);
@ -459,8 +480,7 @@ connect_local_xsocket(int display_number)
} }
static int static int x11_connect_display(void)
x11_connect_display(void)
{ {
int display_number; int display_number;
const char *display = NULL; const char *display = NULL;
@ -474,7 +494,7 @@ x11_connect_display(void)
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "display: %s", display); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "display: %s", display);
if (!display) { if (display == 0) {
return -1; return -1;
} }
@ -482,19 +502,23 @@ x11_connect_display(void)
if (strncmp(display, "unix:", 5) == 0 || display[0] == ':') { if (strncmp(display, "unix:", 5) == 0 || display[0] == ':') {
/* Connect to the unix domain socket. */ /* Connect to the unix domain socket. */
if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) { if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "Could not parse display number from DISPLAY: %.100s", display); _ssh_log(SSH_LOG_FUNCTIONS, __func__,
"Could not parse display number from DISPLAY: %.100s",
display);
return -1; return -1;
} }
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "display_number: %d", display_number); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "display_number: %d",
display_number);
/* Create a socket. */ /* Create a socket. */
sock = connect_local_xsocket(display_number); sock = connect_local_xsocket(display_number);
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "socket: %d", sock); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "socket: %d", sock);
if (sock < 0) if (sock < 0) {
return -1; return -1;
}
/* OK, we now have a connection to the display. */ /* OK, we now have a connection to the display. */
return sock; return sock;
@ -503,13 +527,16 @@ x11_connect_display(void)
/* Connect to an inet socket. */ /* Connect to an inet socket. */
strncpy(buf, display, sizeof(buf) - 1); strncpy(buf, display, sizeof(buf) - 1);
cp = strchr(buf, ':'); cp = strchr(buf, ':');
if (!cp) { if (cp == 0) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "Could not find ':' in DISPLAY: %.100s", display); _ssh_log(SSH_LOG_FUNCTIONS, __func__,
"Could not find ':' in DISPLAY: %.100s", display);
return -1; return -1;
} }
*cp = 0; *cp = 0;
if (sscanf(cp + 1, "%d", &display_number) != 1) { if (sscanf(cp + 1, "%d", &display_number) != 1) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "Could not parse display number from DISPLAY: %.100s", display); _ssh_log(SSH_LOG_FUNCTIONS, __func__,
"Could not parse display number from DISPLAY: %.100s",
display);
return -1; return -1;
} }
@ -518,20 +545,25 @@ x11_connect_display(void)
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
snprintf(strport, sizeof(strport), "%u", 6000 + display_number); snprintf(strport, sizeof(strport), "%u", 6000 + display_number);
if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) { gaierr = getaddrinfo(buf, strport, &hints, &aitop);
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "%.100s: unknown host. (%s)", buf, ssh_gai_strerror(gaierr)); if (gaierr != 0) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "%.100s: unknown host. (%s)",
buf, ssh_gai_strerror(gaierr));
return -1; return -1;
} }
for (ai = aitop; ai; ai = ai->ai_next) { for (ai = aitop; ai; ai = ai->ai_next) {
/* Create a socket. */ /* Create a socket. */
sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (sock == -1) { if (sock == -1) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "socket: %.100s", strerror(errno)); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "socket: %.100s",
strerror(errno));
continue; continue;
} }
/* Connect it to the display. */ /* Connect it to the display. */
if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) { if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "connect %.100s port %u: %.100s", buf, 6000 + display_number, strerror(errno)); _ssh_log(SSH_LOG_FUNCTIONS, __func__,
"connect %.100s port %u: %.100s", buf,
6000 + display_number, strerror(errno));
close(sock); close(sock);
continue; continue;
} }
@ -539,18 +571,19 @@ x11_connect_display(void)
break; break;
} }
freeaddrinfo(aitop); freeaddrinfo(aitop);
if (!ai) { if (ai == 0) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "connect %.100s port %u: %.100s", buf, 6000 + display_number, strerror(errno)); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "connect %.100s port %u: %.100s",
buf, 6000 + display_number, strerror(errno));
return -1; return -1;
} }
set_nodelay(sock); set_nodelay(sock);
return sock; return sock;
} }
static int static int copy_fd_to_channel_callback(int fd, int revents, void *userdata)
copy_fd_to_channel_callback(int fd, int revents, void *userdata)
{ {
ssh_channel channel = (ssh_channel)userdata; ssh_channel channel = (ssh_channel)userdata;
char buf[2097152]; char buf[2097152];
@ -560,7 +593,7 @@ copy_fd_to_channel_callback(int fd, int revents, void *userdata)
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "event: %d - fd: %d", revents, fd); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "event: %d - fd: %d", revents, fd);
if (!channel) { if (channel == NULL) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "channel does not exist."); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "channel does not exist.");
if (temp_node->protected == 0) { if (temp_node->protected == 0) {
close(fd); close(fd);
@ -585,7 +618,8 @@ copy_fd_to_channel_callback(int fd, int revents, void *userdata)
return -1; return -1;
} else { } else {
/* sz = 0. Why the hell I'm here? */ /* sz = 0. Why the hell I'm here? */
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "Why the hell am I here?: sz: %d", sz); _ssh_log(SSH_LOG_FUNCTIONS, __func__,
"Why the hell am I here?: sz: %d", sz);
if (temp_node->protected == 0) { if (temp_node->protected == 0) {
close(fd); close(fd);
} }
@ -602,8 +636,9 @@ copy_fd_to_channel_callback(int fd, int revents, void *userdata)
} }
static int static int copy_channel_to_fd_callback(ssh_session session, ssh_channel channel,
copy_channel_to_fd_callback(ssh_session session, ssh_channel channel, void *data, uint32_t len, int is_stderr, void *userdata) void *data, uint32_t len, int is_stderr,
void *userdata)
{ {
node_t *temp_node = NULL; node_t *temp_node = NULL;
int fd, sz; int fd, sz;
@ -616,7 +651,8 @@ copy_channel_to_fd_callback(ssh_session session, ssh_channel channel, void *data
fd = temp_node->fd_out; fd = temp_node->fd_out;
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "len: %d - fd: %d - is_stderr: %d", len, fd, is_stderr); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "len: %d - fd: %d - is_stderr: %d",
len, fd, is_stderr);
sz = write(fd, data, len); sz = write(fd, data, len);
@ -624,8 +660,8 @@ copy_channel_to_fd_callback(ssh_session session, ssh_channel channel, void *data
} }
static void static void channel_close_callback(ssh_session session, ssh_channel channel,
channel_close_callback(ssh_session session, ssh_channel channel, void *userdata) void *userdata)
{ {
node_t *temp_node = NULL; node_t *temp_node = NULL;
@ -649,8 +685,9 @@ channel_close_callback(ssh_session session, ssh_channel channel, void *userdata)
} }
static ssh_channel static ssh_channel x11_open_request_callback(ssh_session session,
x11_open_request_callback(ssh_session session, const char *shost, int sport, void *userdata) const char *shost, int sport,
void *userdata)
{ {
ssh_channel channel = NULL; ssh_channel channel = NULL;
int sock, rv; int sock, rv;
@ -685,8 +722,7 @@ x11_open_request_callback(ssh_session session, const char *shost, int sport, voi
* MAIN LOOP * MAIN LOOP
*/ */
static int static int main_loop(ssh_channel channel)
main_loop(ssh_channel channel)
{ {
ssh_session session = ssh_channel_get_session(channel); ssh_session session = ssh_channel_get_session(channel);
int rv; int rv;
@ -705,12 +741,15 @@ main_loop(ssh_channel channel)
return -1; return -1;
} }
if (ssh_event_add_fd(event, fileno(stdin), events, copy_fd_to_channel_callback, channel) != SSH_OK) { rv = ssh_event_add_fd(event, fileno(stdin), events,
copy_fd_to_channel_callback, channel);
if (rv != SSH_OK) {
printf("Couldn't add an fd to the event\n"); printf("Couldn't add an fd to the event\n");
return -1; return -1;
} }
if(ssh_event_add_session(event, session) != SSH_OK) { rv = ssh_event_add_session(event, session);
if (rv != SSH_OK) {
printf("Couldn't add the session to the event\n"); printf("Couldn't add the session to the event\n");
return -1; return -1;
} }
@ -735,16 +774,17 @@ main_loop(ssh_channel channel)
* USAGE * USAGE
*/ */
static void static void usage(void)
usage(void)
{ {
fprintf(stderr, fprintf(stderr,
"Usage : ssh-X11-client [options] [login@]hostname\n" "Usage : ssh-X11-client [options] [login@]hostname\n"
"sample X11 client - libssh-%s\n" "sample X11 client - libssh-%s\n"
"Options :\n" "Options :\n"
" -l user : Specifies the user to log in as on the remote machine.\n" " -l user : Specifies the user to log in as on the remote "
"machine.\n"
" -p port : Port to connect to on the remote host.\n" " -p port : Port to connect to on the remote host.\n"
" -v : Verbose mode. Multiple -v options increase the verbosity. The maximum is 5.\n" " -v : Verbose mode. Multiple -v options increase the "
"verbosity. The maximum is 5.\n"
" -C : Requests compression of all data.\n" " -C : Requests compression of all data.\n"
" -x : Disables X11 forwarding.\n" " -x : Disables X11 forwarding.\n"
"\n", "\n",
@ -783,8 +823,7 @@ static int opts(int argc, char **argv)
* MAIN * MAIN
*/ */
int int main(int argc, char **argv)
main(int argc, char **argv)
{ {
char *password = NULL; char *password = NULL;
@ -798,13 +837,18 @@ main(int argc, char **argv)
ssh_set_log_callback(_logging_callback); ssh_set_log_callback(_logging_callback);
ret = ssh_init(); ret = ssh_init();
if (ret != SSH_OK) return ret; if (ret != SSH_OK) {
return ret;
}
session = ssh_new(); session = ssh_new();
if (session == NULL) exit(-1); if (session == NULL) {
exit(-1);
}
if (ssh_options_getopt(session, &argc, argv) || opts(argc, argv)) { if (ssh_options_getopt(session, &argc, argv) || opts(argc, argv)) {
fprintf(stderr, "Error parsing command line: %s\n", ssh_get_error(session)); fprintf(stderr, "Error parsing command line: %s\n",
ssh_get_error(session));
ssh_free(session); ssh_free(session);
ssh_finalize(); ssh_finalize();
usage(); usage();
@ -823,21 +867,30 @@ main(int argc, char **argv)
password = getpass("Password: "); password = getpass("Password: ");
ret = ssh_userauth_password(session, NULL, password); ret = ssh_userauth_password(session, NULL, password);
if (ret != SSH_AUTH_SUCCESS) { if (ret != SSH_AUTH_SUCCESS) {
fprintf(stderr, "Error authenticating with password: %s\n", ssh_get_error(session)); fprintf(stderr, "Error authenticating with password: %s\n",
ssh_get_error(session));
exit(-1); exit(-1);
} }
channel = ssh_channel_new(session); channel = ssh_channel_new(session);
if (channel == NULL) return SSH_ERROR; if (channel == NULL) {
return SSH_ERROR;
}
ret = ssh_channel_open_session(channel); ret = ssh_channel_open_session(channel);
if (ret != SSH_OK) return ret; if (ret != SSH_OK) {
return ret;
}
ret = ssh_channel_request_pty(channel); ret = ssh_channel_request_pty(channel);
if (ret != SSH_OK) return ret; if (ret != SSH_OK) {
return ret;
}
ret = ssh_channel_change_pty_size(channel, 80, 24); ret = ssh_channel_change_pty_size(channel, 80, 24);
if (ret != SSH_OK) return ret; if (ret != SSH_OK) {
return ret;
}
if (enableX11 == 1) { if (enableX11 == 1) {
display = getenv("DISPLAY"); display = getenv("DISPLAY");
@ -847,29 +900,42 @@ main(int argc, char **argv)
if (display) { if (display) {
ssh_callbacks_init(&cb); ssh_callbacks_init(&cb);
ret = ssh_set_callbacks(session, &cb); ret = ssh_set_callbacks(session, &cb);
if (ret != SSH_OK) return ret; if (ret != SSH_OK) {
return ret;
}
if (x11_get_proto(display, &proto, &cookie) != 0) { ret = x11_get_proto(display, &proto, &cookie);
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "Using fake authentication data for X11 forwarding"); if (ret != 0) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__,
"Using fake authentication data for X11 forwarding");
proto = NULL; proto = NULL;
cookie = NULL; cookie = NULL;
} }
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "proto: %s - cookie: %s", proto, cookie); _ssh_log(SSH_LOG_FUNCTIONS, __func__, "proto: %s - cookie: %s",
proto, cookie);
/* See https://gitlab.com/libssh/libssh-mirror/-/blob/master/src/channels.c#L2062 for details. */ /* See https://gitlab.com/libssh/libssh-mirror/-/blob/master/src/channels.c#L2062 for details. */
ret = ssh_channel_request_x11(channel, 0, proto, cookie, 0); ret = ssh_channel_request_x11(channel, 0, proto, cookie, 0);
if (ret != SSH_OK) return ret; if (ret != SSH_OK) {
return ret;
}
} }
} }
ret = _enter_term_raw_mode(); ret = _enter_term_raw_mode();
if (ret != 0) exit(-1); if (ret != 0) {
exit(-1);
}
ret = ssh_channel_request_shell(channel); ret = ssh_channel_request_shell(channel);
if (ret != SSH_OK) return ret; if (ret != SSH_OK) {
return ret;
}
ret = main_loop(channel); ret = main_loop(channel);
if (ret != SSH_OK) return ret; if (ret != SSH_OK) {
return ret;
}
_leave_term_raw_mode(); _leave_term_raw_mode();