1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-07-29 13:01:14 +03:00

tidy-up: example, tests continued

- fix skip auth if `userauthlist` is NULL.
  Closes #836 (Reported-by: @sudipm-mukherjee on github)
- fix most silenced `checksrc` warnings.
- sync examples/tests code between each other.
  (output messages, error handling, declaration order, comments)
- stop including unnecessary headers.
- always deinitialize in case of error.
- drop some redundant variables.
- add error handling where missing.
- show more error codes.
- switch `perror()` to `fprintf()`.
- fix some `printf()`s to be `fprintf()`.
- formatting.

Closes #960
This commit is contained in:
Viktor Szakats
2023-04-14 11:05:21 +00:00
parent 0162d1649c
commit 2efdb6747a
51 changed files with 1550 additions and 1289 deletions

View File

@ -54,34 +54,34 @@ static int local_destport = 22;
enum {
AUTH_NONE = 0,
AUTH_PASSWORD,
AUTH_PUBLICKEY
AUTH_PASSWORD = 1,
AUTH_PUBLICKEY = 2
};
int main(int argc, char *argv[])
{
int rc, i, auth = AUTH_NONE;
int i, auth = AUTH_NONE;
struct sockaddr_in sin;
socklen_t sinlen = sizeof(sin);
const char *fingerprint;
char *userauthlist;
LIBSSH2_SESSION *session;
int rc;
LIBSSH2_SESSION *session = NULL;
LIBSSH2_LISTENER *listener = NULL;
LIBSSH2_CHANNEL *channel = NULL;
fd_set fds;
struct timeval tv;
ssize_t len, wr;
char buf[16384];
libssh2_socket_t sock = LIBSSH2_INVALID_SOCKET;
libssh2_socket_t sock;
libssh2_socket_t forwardsock = LIBSSH2_INVALID_SOCKET;
#ifdef WIN32
WSADATA wsadata;
int err;
err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err);
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(rc) {
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
return 1;
}
#endif
@ -102,7 +102,7 @@ int main(int argc, char *argv[])
local_destport = atoi(argv[7]);
rc = libssh2_init(0);
if(rc != 0) {
if(rc) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}
@ -111,26 +111,26 @@ int main(int argc, char *argv[])
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == LIBSSH2_INVALID_SOCKET) {
fprintf(stderr, "failed to open socket!\n");
return -1;
goto shutdown;
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(server_ip);
if(INADDR_NONE == sin.sin_addr.s_addr) {
fprintf(stderr, "inet_addr: Invalid IP address \"%s\"\n", server_ip);
return -1;
goto shutdown;
}
sin.sin_port = htons(22);
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr));
return -1;
goto shutdown;
}
/* Create a session instance */
session = libssh2_session_init();
if(!session) {
fprintf(stderr, "Could not initialize SSH session!\n");
return -1;
goto shutdown;
}
/* ... start it up. This will trade welcome banners, exchange keys,
@ -139,7 +139,7 @@ int main(int argc, char *argv[])
rc = libssh2_session_handshake(session, sock);
if(rc) {
fprintf(stderr, "Error when starting up SSH session: %d\n", rc);
return -1;
goto shutdown;
}
/* At this point we have not yet authenticated. The first thing to do
@ -156,39 +156,43 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username,
(unsigned int)strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if(strstr(userauthlist, "password"))
auth |= AUTH_PASSWORD;
if(strstr(userauthlist, "publickey"))
auth |= AUTH_PUBLICKEY;
if(userauthlist) {
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if(strstr(userauthlist, "password"))
auth |= AUTH_PASSWORD;
if(strstr(userauthlist, "publickey"))
auth |= AUTH_PUBLICKEY;
/* check for options */
if(argc > 8) {
if((auth & AUTH_PASSWORD) && !strcmp(argv[8], "-p"))
auth = AUTH_PASSWORD;
if((auth & AUTH_PUBLICKEY) && !strcmp(argv[8], "-k"))
auth = AUTH_PUBLICKEY;
}
/* check for options */
if(argc > 8) {
if((auth & AUTH_PASSWORD) && !strcmp(argv[8], "-p"))
auth = AUTH_PASSWORD;
if((auth & AUTH_PUBLICKEY) && !strcmp(argv[8], "-k"))
auth = AUTH_PUBLICKEY;
}
if(auth & AUTH_PASSWORD) {
if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed.\n");
if(auth & AUTH_PASSWORD) {
if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed!\n");
goto shutdown;
}
}
else if(auth & AUTH_PUBLICKEY) {
if(libssh2_userauth_publickey_fromfile(session, username,
pubkey, privkey,
password)) {
fprintf(stderr, "Authentication by public key failed!\n");
goto shutdown;
}
else {
fprintf(stderr, "Authentication by public key succeeded.\n");
}
}
else {
fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown;
}
}
else if(auth & AUTH_PUBLICKEY) {
if(libssh2_userauth_publickey_fromfile(session, username,
pubkey, privkey,
password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown;
}
fprintf(stderr, "\tAuthentication by public key succeeded.\n");
}
else {
fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown;
}
fprintf(stderr, "Asking server to listen on remote %s:%d\n",
remote_listenhost, remote_wantport);
@ -197,8 +201,8 @@ int main(int argc, char *argv[])
remote_wantport, &remote_listenport, 1);
if(!listener) {
fprintf(stderr, "Could not start the tcpip-forward listener!\n"
"(Note that this can be a problem at the server!"
" Please review the server logs.)\n");
"(Note that this can be a problem at the server!"
" Please review the server logs.)\n");
goto shutdown;
}
@ -209,8 +213,8 @@ int main(int argc, char *argv[])
channel = libssh2_channel_forward_accept(listener);
if(!channel) {
fprintf(stderr, "Could not accept connection!\n"
"(Note that this can be a problem at the server!"
" Please review the server logs.)\n");
"(Note that this can be a problem at the server!"
" Please review the server logs.)\n");
goto shutdown;
}
@ -227,11 +231,11 @@ int main(int argc, char *argv[])
sin.sin_port = htons((unsigned short)local_destport);
sin.sin_addr.s_addr = inet_addr(local_destip);
if(INADDR_NONE == sin.sin_addr.s_addr) {
perror("inet_addr");
fprintf(stderr, "failed in inet_addr()!\n");
goto shutdown;
}
if(-1 == connect(forwardsock, (struct sockaddr *)&sin, sinlen)) {
perror("connect");
fprintf(stderr, "failed to connect()!\n");
goto shutdown;
}
@ -248,14 +252,14 @@ int main(int argc, char *argv[])
tv.tv_usec = 100000;
rc = select((int)(forwardsock + 1), &fds, NULL, NULL, &tv);
if(-1 == rc) {
perror("select");
fprintf(stderr, "failed to select()!\n");
goto shutdown;
}
if(rc && FD_ISSET(forwardsock, &fds)) {
ssize_t nwritten;
len = recv(forwardsock, buf, sizeof(buf), 0);
if(len < 0) {
perror("read");
fprintf(stderr, "failed to recv()!\n");
goto shutdown;
}
else if(0 == len) {
@ -288,7 +292,7 @@ int main(int argc, char *argv[])
while(wr < len) {
nsent = send(forwardsock, buf + wr, len - wr, 0);
if(nsent <= 0) {
perror("write");
fprintf(stderr, "failed to send()!\n");
goto shutdown;
}
wr += nsent;
@ -302,23 +306,33 @@ int main(int argc, char *argv[])
}
shutdown:
if(forwardsock != LIBSSH2_INVALID_SOCKET) {
#ifdef WIN32
closesocket(forwardsock);
closesocket(forwardsock);
#else
close(forwardsock);
close(forwardsock);
#endif
}
if(channel)
libssh2_channel_free(channel);
if(listener)
libssh2_channel_forward_cancel(listener);
libssh2_session_disconnect(session, "Client disconnecting normally");
libssh2_session_free(session);
if(session) {
libssh2_session_disconnect(session, "Normal Shutdown");
libssh2_session_free(session);
}
if(sock != LIBSSH2_INVALID_SOCKET) {
#ifdef WIN32
closesocket(sock);
closesocket(sock);
#else
close(sock);
close(sock);
#endif
}
libssh2_exit();