1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-07-28 01:41:48 +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 */
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 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);
/* 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 */
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 */
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 */
static int main_loop(ssh_channel channel);
@ -193,8 +199,8 @@ struct termios _saved_tio;
* Internal functions
*/
int64_t
_current_timestamp(void) {
int64_t _current_timestamp(void)
{
struct timeval tv;
int64_t milliseconds;
@ -204,8 +210,8 @@ _current_timestamp(void) {
return milliseconds;
}
static void
_logging_callback(int priority, const char *function, const char *buffer, void *userdata)
static void _logging_callback(int priority, const char *function,
const char *buffer, void *userdata)
{
FILE *fp = NULL;
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));
fp = fopen("debug.log","a");
if(fp == NULL)
{
if (fp == NULL) {
printf("Error!");
exit(-11);
}
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);
}
static int
_enter_term_raw_mode(void)
static int _enter_term_raw_mode(void)
{
struct termios tio;
int ret = tcgetattr(fileno(stdin), &tio);
@ -251,11 +256,11 @@ _enter_term_raw_mode(void)
tio.c_cc[VTIME] = 0;
ret = tcsetattr(fileno(stdin), TCSADRAIN, &tio);
}
return ret;
}
static int
_leave_term_raw_mode(void)
static int _leave_term_raw_mode(void)
{
int ret = tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio);
return ret;
@ -266,8 +271,8 @@ _leave_term_raw_mode(void)
* Functions
*/
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)
{
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;
} else {
node_iterator = node;
while (node_iterator->next != NULL)
while (node_iterator->next != NULL) {
node_iterator = node_iterator->next;
}
/* Create the new node */
new = (node_t *) malloc(sizeof(node_t));
if (new == NULL) {
@ -307,21 +313,22 @@ 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)
{
node_t *current = NULL, *previous = NULL;
pthread_mutex_lock(&mutex);
for (current = node; current; previous = current, current = current->next) {
if (current->channel != channel)
if (current->channel != channel) {
continue;
}
if (previous == NULL)
if (previous == NULL) {
node = current->next;
else
} else {
previous->next = current->next;
}
free(current);
pthread_mutex_unlock(&mutex);
@ -332,8 +339,7 @@ delete_item(ssh_channel channel)
}
static node_t *
search_item(ssh_channel channel)
static node_t *search_item(ssh_channel channel)
{
node_t *current = node;
@ -355,15 +361,17 @@ search_item(ssh_channel channel)
static void
set_nodelay(int fd)
static void set_nodelay(int fd)
{
int opt;
int opt, rc;
socklen_t optlen;
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;
}
if (opt == 1) {
@ -372,23 +380,26 @@ set_nodelay(int fd)
}
opt = 1;
_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 *
ssh_gai_strerror(int gaierr)
const char *ssh_gai_strerror(int gaierr)
{
if (gaierr == EAI_SYSTEM && errno != 0)
if (gaierr == EAI_SYSTEM && errno != 0) {
return strerror(errno);
}
return gai_strerror(gaierr);
}
static int
x11_get_proto(const char *display, char **_proto, char **_cookie)
static int x11_get_proto(const char *display, char **_proto, char **_cookie)
{
char cmd[1024], line[512], xdisplay[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';
if (strncmp(display, "localhost:", 10) == 0) {
if ((ret = snprintf(xdisplay, sizeof(xdisplay), "unix:%s", display + 10)) < 0 || (size_t)ret >= sizeof(xdisplay)) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "display name too long. display: %s", display);
ret = snprintf(xdisplay, sizeof(xdisplay), "unix:%s", display + 10);
if (ret < 0 || (size_t)ret >= sizeof(xdisplay)) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__,
"display name too long. display: %s", display);
return -1;
}
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);
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;
} else {
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;
}
static int
connect_local_xsocket_path(const char *pathname)
static int connect_local_xsocket_path(const char *pathname)
{
int sock;
int sock, rc;
struct sockaddr_un addr;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
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;
}
@ -442,16 +459,20 @@ connect_local_xsocket_path(const char *pathname)
addr.sun_path[0] = '\0';
/* pathname is guaranteed to be initialized and larger than addr.sun_path[108] */
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;
}
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;
}
static int
connect_local_xsocket(int display_number)
static int connect_local_xsocket(int display_number)
{
char buf[1024] = {0};
snprintf(buf, sizeof(buf), _PATH_UNIX_X, display_number);
@ -459,8 +480,7 @@ connect_local_xsocket(int display_number)
}
static int
x11_connect_display(void)
static int x11_connect_display(void)
{
int display_number;
const char *display = NULL;
@ -474,7 +494,7 @@ x11_connect_display(void)
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "display: %s", display);
if (!display) {
if (display == 0) {
return -1;
}
@ -482,19 +502,23 @@ x11_connect_display(void)
if (strncmp(display, "unix:", 5) == 0 || display[0] == ':') {
/* Connect to the unix domain socket. */
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;
}
_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. */
sock = connect_local_xsocket(display_number);
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "socket: %d", sock);
if (sock < 0)
if (sock < 0) {
return -1;
}
/* OK, we now have a connection to the display. */
return sock;
@ -503,13 +527,16 @@ x11_connect_display(void)
/* Connect to an inet socket. */
strncpy(buf, display, sizeof(buf) - 1);
cp = strchr(buf, ':');
if (!cp) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "Could not find ':' in DISPLAY: %.100s", display);
if (cp == 0) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__,
"Could not find ':' in DISPLAY: %.100s", display);
return -1;
}
*cp = 0;
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;
}
@ -518,20 +545,25 @@ x11_connect_display(void)
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
snprintf(strport, sizeof(strport), "%u", 6000 + display_number);
if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "%.100s: unknown host. (%s)", buf, ssh_gai_strerror(gaierr));
gaierr = getaddrinfo(buf, strport, &hints, &aitop);
if (gaierr != 0) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "%.100s: unknown host. (%s)",
buf, ssh_gai_strerror(gaierr));
return -1;
}
for (ai = aitop; ai; ai = ai->ai_next) {
/* Create a socket. */
sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (sock == -1) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "socket: %.100s", strerror(errno));
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "socket: %.100s",
strerror(errno));
continue;
}
/* Connect it to the display. */
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);
continue;
}
@ -539,18 +571,19 @@ x11_connect_display(void)
break;
}
freeaddrinfo(aitop);
if (!ai) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "connect %.100s port %u: %.100s", buf, 6000 + display_number, strerror(errno));
if (ai == 0) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "connect %.100s port %u: %.100s",
buf, 6000 + display_number, strerror(errno));
return -1;
}
set_nodelay(sock);
return sock;
}
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)
{
ssh_channel channel = (ssh_channel)userdata;
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);
if (!channel) {
if (channel == NULL) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "channel does not exist.");
if (temp_node->protected == 0) {
close(fd);
@ -585,7 +618,8 @@ copy_fd_to_channel_callback(int fd, int revents, void *userdata)
return -1;
} else {
/* 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) {
close(fd);
}
@ -602,8 +636,9 @@ copy_fd_to_channel_callback(int fd, int revents, 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)
static int copy_channel_to_fd_callback(ssh_session session, ssh_channel channel,
void *data, uint32_t len, int is_stderr,
void *userdata)
{
node_t *temp_node = NULL;
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;
_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);
@ -624,8 +660,8 @@ copy_channel_to_fd_callback(ssh_session session, ssh_channel channel, void *data
}
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)
{
node_t *temp_node = NULL;
@ -649,8 +685,9 @@ channel_close_callback(ssh_session session, ssh_channel channel, void *userdata)
}
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)
{
ssh_channel channel = NULL;
int sock, rv;
@ -685,8 +722,7 @@ x11_open_request_callback(ssh_session session, const char *shost, int sport, voi
* MAIN LOOP
*/
static int
main_loop(ssh_channel channel)
static int main_loop(ssh_channel channel)
{
ssh_session session = ssh_channel_get_session(channel);
int rv;
@ -705,12 +741,15 @@ main_loop(ssh_channel channel)
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");
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");
return -1;
}
@ -735,16 +774,17 @@ main_loop(ssh_channel channel)
* USAGE
*/
static void
usage(void)
static void usage(void)
{
fprintf(stderr,
"Usage : ssh-X11-client [options] [login@]hostname\n"
"sample X11 client - libssh-%s\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"
" -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"
" -x : Disables X11 forwarding.\n"
"\n",
@ -783,8 +823,7 @@ static int opts(int argc, char **argv)
* MAIN
*/
int
main(int argc, char **argv)
int main(int argc, char **argv)
{
char *password = NULL;
@ -798,13 +837,18 @@ main(int argc, char **argv)
ssh_set_log_callback(_logging_callback);
ret = ssh_init();
if (ret != SSH_OK) return ret;
if (ret != SSH_OK) {
return ret;
}
session = ssh_new();
if (session == NULL) exit(-1);
if (session == NULL) {
exit(-1);
}
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_finalize();
usage();
@ -823,21 +867,30 @@ main(int argc, char **argv)
password = getpass("Password: ");
ret = ssh_userauth_password(session, NULL, password);
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);
}
channel = ssh_channel_new(session);
if (channel == NULL) return SSH_ERROR;
if (channel == NULL) {
return SSH_ERROR;
}
ret = ssh_channel_open_session(channel);
if (ret != SSH_OK) return ret;
if (ret != SSH_OK) {
return ret;
}
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);
if (ret != SSH_OK) return ret;
if (ret != SSH_OK) {
return ret;
}
if (enableX11 == 1) {
display = getenv("DISPLAY");
@ -847,29 +900,42 @@ main(int argc, char **argv)
if (display) {
ssh_callbacks_init(&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) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__, "Using fake authentication data for X11 forwarding");
ret = x11_get_proto(display, &proto, &cookie);
if (ret != 0) {
_ssh_log(SSH_LOG_FUNCTIONS, __func__,
"Using fake authentication data for X11 forwarding");
proto = 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. */
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();
if (ret != 0) exit(-1);
if (ret != 0) {
exit(-1);
}
ret = ssh_channel_request_shell(channel);
if (ret != SSH_OK) return ret;
if (ret != SSH_OK) {
return ret;
}
ret = main_loop(channel);
if (ret != SSH_OK) return ret;
if (ret != SSH_OK) {
return ret;
}
_leave_term_raw_mode();