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

@ -4,8 +4,6 @@
* The sample code has fixed values for host name, user name, password
* and command to run.
*
* Run it like this:
*
* $ ./ssh2_exec 127.0.0.1 user password "uptime"
*
*/
@ -39,6 +37,13 @@
#include <stdlib.h>
#include <ctype.h>
static const char *hostname = "127.0.0.1";
static const char *commandline = "uptime";
static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
static const char *privkey = "/home/username/.ssh/id_rsa";
static const char *username = "user";
static const char *password = "password";
static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
{
struct timeval timeout;
@ -71,19 +76,13 @@ static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
int main(int argc, char *argv[])
{
const char *hostname = "127.0.0.1";
const char *commandline = "uptime";
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "user";
const char *password = "password";
uint32_t hostaddr;
libssh2_socket_t sock;
struct sockaddr_in sin;
const char *fingerprint;
LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *channel;
int rc;
LIBSSH2_SESSION *session = NULL;
LIBSSH2_CHANNEL *channel;
int exitcode;
char *exitsignal = (char *)"none";
ssize_t bytecount = 0;
@ -93,11 +92,10 @@ int main(int argc, char *argv[])
#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
@ -116,31 +114,36 @@ int main(int argc, char *argv[])
}
rc = libssh2_init(0);
if(rc != 0) {
if(rc) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}
hostaddr = inet_addr(hostname);
/* Ultra basic "connect to port 22 on localhost"
* Your code is responsible for creating the socket establishing the
* connection
/* Ultra basic "connect to port 22 on localhost". Your code is
* responsible for creating the socket establishing the connection
*/
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == LIBSSH2_INVALID_SOCKET) {
fprintf(stderr, "failed to create socket!\n");
goto shutdown;
}
sin.sin_family = AF_INET;
sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
fprintf(stderr, "failed to connect!\n");
return -1;
goto shutdown;
}
/* Create a session instance */
session = libssh2_session_init();
if(!session)
return -1;
if(!session) {
fprintf(stderr, "Could not initialize SSH session!\n");
goto shutdown;
}
/* tell libssh2 we want it all done non-blocking */
libssh2_session_set_blocking(session, 0);
@ -152,7 +155,7 @@ int main(int argc, char *argv[])
LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1;
goto shutdown;
}
nh = libssh2_knownhost_init(session);
@ -177,9 +180,10 @@ int main(int argc, char *argv[])
LIBSSH2_KNOWNHOST_TYPE_PLAIN|
LIBSSH2_KNOWNHOST_KEYENC_RAW,
&host);
fprintf(stderr, "Host check: %d, key: %s\n", check,
(check <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH)?
host->key:"<none>");
(check <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH) ?
host->key : "<none>");
/*****
* At this point, we could verify that 'check' tells us the key is
@ -195,9 +199,9 @@ int main(int argc, char *argv[])
if(strlen(password) != 0) {
/* We could authenticate via password */
while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN);
LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "Authentication by password failed.\n");
fprintf(stderr, "Authentication by password failed!\n");
goto shutdown;
}
}
@ -206,9 +210,9 @@ int main(int argc, char *argv[])
while((rc = libssh2_userauth_publickey_fromfile(session, username,
pubkey, privkey,
password)) ==
LIBSSH2_ERROR_EAGAIN);
LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n");
fprintf(stderr, "Authentication by public key failed!\n");
goto shutdown;
}
}
@ -217,13 +221,16 @@ int main(int argc, char *argv[])
libssh2_trace(session, ~0);
#endif
/* Exec non-blocking on the remove host */
while((channel = libssh2_channel_open_session(session)) == NULL &&
libssh2_session_last_error(session, NULL, NULL, 0) ==
LIBSSH2_ERROR_EAGAIN) {
/* Exec non-blocking on the remote host */
do {
channel = libssh2_channel_open_session(session);
if(channel ||
libssh2_session_last_error(session, NULL, NULL, 0) !=
LIBSSH2_ERROR_EAGAIN)
break;
waitsocket(sock, session);
}
if(channel == NULL) {
} while(1);
if(!channel) {
fprintf(stderr, "Error\n");
exit(1);
}
@ -231,7 +238,7 @@ int main(int argc, char *argv[])
LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session);
}
if(rc != 0) {
if(rc) {
fprintf(stderr, "exec error\n");
exit(1);
}
@ -287,14 +294,19 @@ int main(int argc, char *argv[])
shutdown:
libssh2_session_disconnect(session, "Normal Shutdown");
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
}
fprintf(stderr, "all done\n");
libssh2_exit();