mirror of
https://github.com/libssh2/libssh2.git
synced 2025-08-07 08:02:56 +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:
@@ -4,7 +4,7 @@
|
||||
* The sample code has default values for host name, user name, password and
|
||||
* path, but you can specify them on the command line like:
|
||||
*
|
||||
* "sftpdir 192.168.0.1 user password /tmp/secretdir"
|
||||
* $ ./sftpdir 192.168.0.1 user password /tmp/secretdir
|
||||
*/
|
||||
|
||||
#include "libssh2_setup.h"
|
||||
@@ -23,9 +23,6 @@
|
||||
#ifdef HAVE_ARPA_INET_H
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
#ifdef HAVE_INTTYPES_H
|
||||
#include <inttypes.h>
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
@@ -43,6 +40,7 @@ static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
|
||||
static const char *privkey = "/home/username/.ssh/id_rsa";
|
||||
static const char *username = "username";
|
||||
static const char *password = "password";
|
||||
static const char *sftppath = "/tmp/secretdir";
|
||||
|
||||
static void kbd_callback(const char *name, int name_len,
|
||||
const char *instruction, int instruction_len,
|
||||
@@ -67,22 +65,21 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
uint32_t hostaddr;
|
||||
libssh2_socket_t sock;
|
||||
int rc, i, auth_pw = 0;
|
||||
int i, auth_pw = 0;
|
||||
struct sockaddr_in sin;
|
||||
const char *fingerprint;
|
||||
char *userauthlist;
|
||||
LIBSSH2_SESSION *session;
|
||||
const char *sftppath = "/tmp/secretdir";
|
||||
int rc;
|
||||
LIBSSH2_SESSION *session = NULL;
|
||||
LIBSSH2_SFTP *sftp_session;
|
||||
LIBSSH2_SFTP_HANDLE *sftp_handle;
|
||||
|
||||
#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
|
||||
@@ -104,7 +101,7 @@ 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;
|
||||
}
|
||||
@@ -114,20 +111,25 @@ int main(int argc, char *argv[])
|
||||
* and 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
|
||||
*/
|
||||
/* Create a session instance */
|
||||
session = libssh2_session_init();
|
||||
if(!session)
|
||||
return -1;
|
||||
if(!session) {
|
||||
fprintf(stderr, "Could not initialize SSH session!\n");
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
/* ... start it up. This will trade welcome banners, exchange keys,
|
||||
* and setup crypto, compression, and MAC layers
|
||||
@@ -135,7 +137,7 @@ int main(int argc, char *argv[])
|
||||
rc = libssh2_session_handshake(session, sock);
|
||||
if(rc) {
|
||||
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
|
||||
return -1;
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
/* At this point we have not yet authenticated. The first thing to do
|
||||
@@ -153,68 +155,67 @@ 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") != NULL) {
|
||||
auth_pw |= 1;
|
||||
}
|
||||
if(strstr(userauthlist, "keyboard-interactive") != NULL) {
|
||||
auth_pw |= 2;
|
||||
}
|
||||
if(strstr(userauthlist, "publickey") != NULL) {
|
||||
auth_pw |= 4;
|
||||
}
|
||||
if(userauthlist) {
|
||||
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
|
||||
if(strstr(userauthlist, "password")) {
|
||||
auth_pw |= 1;
|
||||
}
|
||||
if(strstr(userauthlist, "keyboard-interactive")) {
|
||||
auth_pw |= 2;
|
||||
}
|
||||
if(strstr(userauthlist, "publickey")) {
|
||||
auth_pw |= 4;
|
||||
}
|
||||
|
||||
/* if we got an 5. argument we set this option if supported */
|
||||
if(argc > 5) {
|
||||
if((auth_pw & 1) && !strcmp(argv[5], "-p")) {
|
||||
auth_pw = 1;
|
||||
/* check for options */
|
||||
if(argc > 5) {
|
||||
if((auth_pw & 1) && !strcmp(argv[5], "-p")) {
|
||||
auth_pw = 1;
|
||||
}
|
||||
if((auth_pw & 2) && !strcmp(argv[5], "-i")) {
|
||||
auth_pw = 2;
|
||||
}
|
||||
if((auth_pw & 4) && !strcmp(argv[5], "-k")) {
|
||||
auth_pw = 4;
|
||||
}
|
||||
}
|
||||
if((auth_pw & 2) && !strcmp(argv[5], "-i")) {
|
||||
auth_pw = 2;
|
||||
}
|
||||
if((auth_pw & 4) && !strcmp(argv[5], "-k")) {
|
||||
auth_pw = 4;
|
||||
}
|
||||
}
|
||||
|
||||
if(auth_pw & 1) {
|
||||
/* We could authenticate via password */
|
||||
if(libssh2_userauth_password(session, username, password)) {
|
||||
fprintf(stderr, "\tAuthentication by password failed!\n");
|
||||
goto shutdown;
|
||||
if(auth_pw & 1) {
|
||||
/* We could authenticate via password */
|
||||
if(libssh2_userauth_password(session, username, password)) {
|
||||
fprintf(stderr, "Authentication by password failed!\n");
|
||||
goto shutdown;
|
||||
}
|
||||
}
|
||||
else if(auth_pw & 2) {
|
||||
/* Or via keyboard-interactive */
|
||||
if(libssh2_userauth_keyboard_interactive(session, username,
|
||||
&kbd_callback) ) {
|
||||
fprintf(stderr,
|
||||
"Authentication by keyboard-interactive failed!\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr,
|
||||
"Authentication by keyboard-interactive succeeded.\n");
|
||||
}
|
||||
}
|
||||
else if(auth_pw & 4) {
|
||||
/* Or by public key */
|
||||
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, "\tAuthentication by password succeeded.\n");
|
||||
}
|
||||
}
|
||||
else if(auth_pw & 2) {
|
||||
/* Or via keyboard-interactive */
|
||||
if(libssh2_userauth_keyboard_interactive(session, username,
|
||||
&kbd_callback) ) {
|
||||
fprintf(stderr,
|
||||
"\tAuthentication by keyboard-interactive failed!\n");
|
||||
fprintf(stderr, "No supported authentication methods found!\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr,
|
||||
"\tAuthentication by keyboard-interactive succeeded.\n");
|
||||
}
|
||||
}
|
||||
else if(auth_pw & 4) {
|
||||
/* Or by public key */
|
||||
if(libssh2_userauth_publickey_fromfile(session, username,
|
||||
pubkey, privkey,
|
||||
password)) {
|
||||
fprintf(stderr, "\tAuthentication by public key failed!\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "\tAuthentication by public key succeeded.\n");
|
||||
}
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "No supported authentication methods found!\n");
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
fprintf(stderr, "libssh2_sftp_init()!\n");
|
||||
@@ -231,11 +232,11 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "libssh2_sftp_opendir()!\n");
|
||||
/* Request a dir listing via SFTP */
|
||||
sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);
|
||||
|
||||
if(!sftp_handle) {
|
||||
fprintf(stderr, "Unable to open dir with SFTP\n");
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n");
|
||||
do {
|
||||
char mem[512];
|
||||
@@ -287,14 +288,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();
|
||||
|
Reference in New Issue
Block a user