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

tidy-up: example, tests

- drop unnecessary `WIN32`-specific branches.

- add `static`.

- sync header inclusion order.

- sync some common code between examples/tests.

- fix formatting/indentation.

- fix some `checksrc` errors not caught by `checksrc`.

Closes #936
This commit is contained in:
Viktor Szakats
2023-04-08 22:26:10 +00:00
parent 7e4855926e
commit fb9f888308
28 changed files with 343 additions and 409 deletions

View File

@ -10,6 +10,12 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_NETINET_IN_H #ifdef HAVE_NETINET_IN_H
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
@ -20,34 +26,28 @@
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#include <sys/types.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifndef INADDR_NONE #ifndef INADDR_NONE
#define INADDR_NONE (in_addr_t)-1 #define INADDR_NONE (in_addr_t)~0
#endif #endif
const char *keyfile1 = "/home/username/.ssh/id_rsa.pub"; static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *keyfile2 = "/home/username/.ssh/id_rsa"; static const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; static const char *username = "username";
const char *password = ""; static const char *password = "";
const char *server_ip = "127.0.0.1"; static const char *server_ip = "127.0.0.1";
const char *local_listenip = "127.0.0.1"; static const char *local_listenip = "127.0.0.1";
unsigned int local_listenport = 2222; static unsigned int local_listenport = 2222;
const char *remote_desthost = "localhost"; /* resolved by the server */ static const char *remote_desthost = "localhost"; /* resolved by the server */
unsigned int remote_destport = 22; static unsigned int remote_destport = 22;
enum { enum {
AUTH_NONE = 0, AUTH_NONE = 0,
@ -112,24 +112,19 @@ int main(int argc, char *argv[])
/* Connect to SSH server */ /* Connect to SSH server */
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == LIBSSH2_INVALID_SOCKET) { if(sock == LIBSSH2_INVALID_SOCKET) {
#ifdef WIN32
fprintf(stderr, "failed to open socket!\n"); fprintf(stderr, "failed to open socket!\n");
#else
perror("socket");
#endif
return -1; return -1;
} }
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(server_ip); sin.sin_addr.s_addr = inet_addr(server_ip);
if(INADDR_NONE == sin.sin_addr.s_addr) { if(INADDR_NONE == sin.sin_addr.s_addr) {
perror("inet_addr"); fprintf(stderr, "inet_addr: Invalid IP address \"%s\"\n", server_ip);
return -1; return -1;
} }
sin.sin_port = htons(22); sin.sin_port = htons(22);
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr));
fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -184,8 +179,9 @@ int main(int argc, char *argv[])
} }
} }
else if(auth & AUTH_PUBLICKEY) { else if(auth & AUTH_PUBLICKEY) {
if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, if(libssh2_userauth_publickey_fromfile(session, username,
keyfile2, password)) { pubkey, privkey,
password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} }
@ -198,11 +194,7 @@ int main(int argc, char *argv[])
listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(listensock == LIBSSH2_INVALID_SOCKET) { if(listensock == LIBSSH2_INVALID_SOCKET) {
#ifdef WIN32
fprintf(stderr, "failed to open listen socket!\n"); fprintf(stderr, "failed to open listen socket!\n");
#else
perror("socket");
#endif
return -1; return -1;
} }
@ -231,11 +223,7 @@ int main(int argc, char *argv[])
forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen); forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen);
if(forwardsock == LIBSSH2_INVALID_SOCKET) { if(forwardsock == LIBSSH2_INVALID_SOCKET) {
#ifdef WIN32
fprintf(stderr, "failed to accept forward socket!\n"); fprintf(stderr, "failed to accept forward socket!\n");
#else
perror("accept");
#endif
goto shutdown; goto shutdown;
} }

View File

@ -8,15 +8,16 @@
#ifdef WIN32 #ifdef WIN32
#define write(f, b, c) write((f), (b), (unsigned int)(c)) #define write(f, b, c) write((f), (b), (unsigned int)(c))
#endif #endif
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -39,6 +40,8 @@ int main(int argc, char *argv[])
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *channel; LIBSSH2_CHANNEL *channel;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *scppath = "/tmp/TEST"; const char *scppath = "/tmp/TEST";
@ -88,8 +91,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -130,10 +132,8 @@ int main(int argc, char *argv[])
} }
else { else {
/* Or by public key */ /* Or by public key */
#define HOME_DIR "/home/username/"
if(libssh2_userauth_publickey_fromfile(session, username, if(libssh2_userauth_publickey_fromfile(session, username,
HOME_DIR ".ssh/id_rsa.pub", pubkey, privkey,
HOME_DIR ".ssh/id_rsa",
password)) { password)) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
@ -176,8 +176,7 @@ int main(int argc, char *argv[])
shutdown: shutdown:
libssh2_session_disconnect(session, libssh2_session_disconnect(session, "Normal Shutdown");
"Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

View File

@ -13,18 +13,19 @@
#ifdef WIN32 #ifdef WIN32
#define write(f, b, c) write((f), (b), (unsigned int)(c)) #define write(f, b, c) write((f), (b), (unsigned int)(c))
#endif #endif
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -86,6 +87,8 @@ int main(int argc, char *argv[])
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *channel; LIBSSH2_CHANNEL *channel;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *scppath = "/tmp/TEST"; const char *scppath = "/tmp/TEST";
@ -193,10 +196,7 @@ int main(int argc, char *argv[])
else { else {
/* Or by public key */ /* Or by public key */
while((rc = libssh2_userauth_publickey_fromfile(session, username, while((rc = libssh2_userauth_publickey_fromfile(session, username,
"/home/username/" pubkey, privkey,
".ssh/id_rsa.pub",
"/home/username/"
".ssh/id_rsa",
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if(rc) { if(rc) {
@ -277,8 +277,7 @@ int main(int argc, char *argv[])
shutdown: shutdown:
libssh2_session_disconnect(session, libssh2_session_disconnect(session, "Normal Shutdown");
"Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

View File

@ -8,12 +8,12 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -36,6 +36,8 @@ int main(int argc, char *argv[])
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session = NULL; LIBSSH2_SESSION *session = NULL;
LIBSSH2_CHANNEL *channel; LIBSSH2_CHANNEL *channel;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *loclfile = "scp_write.c"; const char *loclfile = "scp_write.c";
@ -104,8 +106,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -146,10 +147,8 @@ int main(int argc, char *argv[])
} }
else { else {
/* Or by public key */ /* Or by public key */
#define HOME "/home/username/"
if(libssh2_userauth_publickey_fromfile(session, username, if(libssh2_userauth_publickey_fromfile(session, username,
HOME ".ssh/id_rsa.pub", pubkey, privkey,
HOME ".ssh/id_rsa",
password)) { password)) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;

View File

@ -8,15 +8,15 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -70,6 +70,8 @@ int main(int argc, char *argv[])
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session = NULL; LIBSSH2_SESSION *session = NULL;
LIBSSH2_CHANNEL *channel; LIBSSH2_CHANNEL *channel;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *loclfile = "scp_write.c"; const char *loclfile = "scp_write.c";
@ -138,8 +140,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -186,10 +187,8 @@ int main(int argc, char *argv[])
} }
else { else {
/* Or by public key */ /* Or by public key */
#define HOME "/home/username/"
while((rc = libssh2_userauth_publickey_fromfile(session, username, while((rc = libssh2_userauth_publickey_fromfile(session, username,
HOME ".ssh/id_rsa.pub", pubkey, privkey,
HOME ".ssh/id_rsa",
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if(rc) { if(rc) {
@ -267,9 +266,8 @@ int main(int argc, char *argv[])
shutdown: shutdown:
while(libssh2_session_disconnect(session, while(libssh2_session_disconnect(session, "Normal Shutdown")
"Normal Shutdown,") == == LIBSSH2_ERROR_EAGAIN);
LIBSSH2_ERROR_EAGAIN);
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

View File

@ -14,15 +14,16 @@
#ifdef WIN32 #ifdef WIN32
#define write(f, b, c) write((f), (b), (unsigned int)(c)) #define write(f, b, c) write((f), (b), (unsigned int)(c))
#endif #endif
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -36,12 +37,11 @@
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
const char *keyfile1 = "~/.ssh/id_rsa.pub"; static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *keyfile2 = "~/.ssh/id_rsa"; static const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; static const char *username = "username";
const char *password = "password"; static const char *password = "password";
const char *sftppath = "/tmp/TEST"; static const char *sftppath = "/tmp/TEST";
static void kbd_callback(const char *name, int name_len, static void kbd_callback(const char *name, int name_len,
const char *instruction, int instruction_len, const char *instruction, int instruction_len,
@ -91,7 +91,6 @@ static void kbd_callback(const char *name, int name_len,
"Done. Sending keyboard-interactive responses to server now.\n"); "Done. Sending keyboard-interactive responses to server now.\n");
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
uint32_t hostaddr; uint32_t hostaddr;
@ -122,7 +121,6 @@ int main(int argc, char *argv[])
else { else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -148,8 +146,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -233,8 +230,9 @@ int main(int argc, char *argv[])
} }
else if(auth_pw & 4) { else if(auth_pw & 4) {
/* Or by public key */ /* Or by public key */
if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, if(libssh2_userauth_publickey_fromfile(session, username,
keyfile2, password)) { pubkey, privkey,
password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} }

View File

@ -14,18 +14,19 @@
#ifdef WIN32 #ifdef WIN32
#define write(f, b, c) write((f), (b), (unsigned int)(c)) #define write(f, b, c) write((f), (b), (unsigned int)(c))
#endif #endif
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -80,6 +81,8 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *sftppath = "/tmp/TEST"; /* source path */ const char *sftppath = "/tmp/TEST"; /* source path */
@ -119,8 +122,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = htonl(0x7F000001); sin.sin_addr.s_addr = htonl(0x7F000001);
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -186,10 +188,7 @@ int main(int argc, char *argv[])
/* Or by public key */ /* Or by public key */
while((rc = while((rc =
libssh2_userauth_publickey_fromfile(session, username, libssh2_userauth_publickey_fromfile(session, username,
"/home/username/" pubkey, privkey,
".ssh/id_rsa.pub",
"/home/username/"
".ssh/id_rsa",
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if(rc) { if(rc) {

View File

@ -14,12 +14,12 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -38,6 +38,8 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *loclfile = "sftp_write.c"; const char *loclfile = "sftp_write.c";
@ -69,7 +71,6 @@ int main(int argc, char *argv[])
else { else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -104,8 +105,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -149,10 +149,8 @@ int main(int argc, char *argv[])
} }
else { else {
/* Or by public key */ /* Or by public key */
#define HOME "/home/username/"
if(libssh2_userauth_publickey_fromfile(session, username, if(libssh2_userauth_publickey_fromfile(session, username,
HOME ".ssh/id_rsa.pub", pubkey, privkey,
HOME ".ssh/id_rsa",
password)) { password)) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
@ -218,8 +216,7 @@ int main(int argc, char *argv[])
libssh2_sftp_shutdown(sftp_session); libssh2_sftp_shutdown(sftp_session);
shutdown: shutdown:
libssh2_session_disconnect(session, libssh2_session_disconnect(session, "Normal Shutdown");
"Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

View File

@ -14,12 +14,12 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -38,6 +38,8 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *sftppath = "/tmp/sftp_mkdir"; const char *sftppath = "/tmp/sftp_mkdir";
@ -61,7 +63,6 @@ int main(int argc, char *argv[])
else { else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -87,8 +88,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -130,8 +130,7 @@ int main(int argc, char *argv[])
else { else {
/* Or by public key */ /* Or by public key */
if(libssh2_userauth_publickey_fromfile(session, username, if(libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub", pubkey, privkey,
"/home/username/.ssh/id_rsa",
password)) { password)) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;

View File

@ -14,12 +14,12 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -38,6 +38,8 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *sftppath = "/tmp/sftp_mkdir_nonblock"; const char *sftppath = "/tmp/sftp_mkdir_nonblock";
@ -61,7 +63,6 @@ int main(int argc, char *argv[])
else { else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -87,8 +88,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -130,8 +130,7 @@ int main(int argc, char *argv[])
else { else {
/* Or by public key */ /* Or by public key */
if(libssh2_userauth_publickey_fromfile(session, username, if(libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub", pubkey, privkey,
"/home/username/.ssh/id_rsa",
password)) { password)) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;

View File

@ -14,18 +14,19 @@
#ifdef WIN32 #ifdef WIN32
#define write(f, b, c) write((f), (b), (unsigned int)(c)) #define write(f, b, c) write((f), (b), (unsigned int)(c))
#endif #endif
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -86,6 +87,8 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *sftppath = "/tmp/TEST"; const char *sftppath = "/tmp/TEST";
@ -117,7 +120,6 @@ int main(int argc, char *argv[])
else { else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -143,8 +145,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -196,10 +197,7 @@ int main(int argc, char *argv[])
/* Or by public key */ /* Or by public key */
while((rc = while((rc =
libssh2_userauth_publickey_fromfile(session, username, libssh2_userauth_publickey_fromfile(session, username,
"/home/username/" pubkey, privkey,
".ssh/id_rsa.pub",
"/home/username/"
".ssh/id_rsa",
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if(rc) { if(rc) {
@ -281,9 +279,8 @@ int main(int argc, char *argv[])
shutdown: shutdown:
fprintf(stderr, "libssh2_session_disconnect\n"); fprintf(stderr, "libssh2_session_disconnect\n");
while(libssh2_session_disconnect(session, while(libssh2_session_disconnect(session, "Normal Shutdown")
"Normal Shutdown, Thank you") == == LIBSSH2_ERROR_EAGAIN);
LIBSSH2_ERROR_EAGAIN);
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

View File

@ -14,12 +14,12 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -38,6 +38,8 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *loclfile = "sftp_write.c"; const char *loclfile = "sftp_write.c";
@ -68,7 +70,6 @@ int main(int argc, char *argv[])
else { else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -103,8 +104,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -148,8 +148,6 @@ int main(int argc, char *argv[])
} }
else { else {
/* Or by public key */ /* Or by public key */
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa.pub";
if(libssh2_userauth_publickey_fromfile(session, username, if(libssh2_userauth_publickey_fromfile(session, username,
pubkey, privkey, pubkey, privkey,
password)) { password)) {
@ -202,8 +200,7 @@ int main(int argc, char *argv[])
libssh2_sftp_shutdown(sftp_session); libssh2_sftp_shutdown(sftp_session);
shutdown: shutdown:
libssh2_session_disconnect(session, libssh2_session_disconnect(session, "Normal Shutdown");
"Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

View File

@ -14,15 +14,15 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -75,6 +75,8 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *loclfile = "sftp_write_nonblock.c"; const char *loclfile = "sftp_write_nonblock.c";
@ -108,7 +110,6 @@ int main(int argc, char *argv[])
else { else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -143,8 +144,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -191,8 +191,6 @@ int main(int argc, char *argv[])
} }
else { else {
/* Or by public key */ /* Or by public key */
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
while((rc = libssh2_userauth_publickey_fromfile(session, username, while((rc = libssh2_userauth_publickey_fromfile(session, username,
pubkey, privkey, pubkey, privkey,
password)) == password)) ==

View File

@ -14,15 +14,15 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -75,6 +75,8 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *loclfile = "sftp_write_nonblock.c"; const char *loclfile = "sftp_write_nonblock.c";
@ -108,7 +110,6 @@ int main(int argc, char *argv[])
else { else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -143,8 +144,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -168,10 +168,10 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
/* At this point we have not yet authenticated. The first thing to do is /* At this point we have not yet authenticated. The first thing to do
* check the hostkey's fingerprint against our known hosts Your app may * is check the hostkey's fingerprint against our known hosts Your app
* have it hard coded, may go to a file, may present it to the user, * may have it hard coded, may go to a file, may present it to the
* that's your call * user, that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
fprintf(stderr, "Fingerprint: "); fprintf(stderr, "Fingerprint: ");
@ -191,10 +191,8 @@ int main(int argc, char *argv[])
} }
else { else {
/* Or by public key */ /* Or by public key */
#define PUBKEY "/home/username/.ssh/id_rsa.pub"
#define PRIVKEY "/home/username/.ssh/id_rsa"
while((rc = libssh2_userauth_publickey_fromfile(session, username, while((rc = libssh2_userauth_publickey_fromfile(session, username,
PUBKEY, PRIVKEY, pubkey, privkey,
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if(rc) { if(rc) {
@ -223,7 +221,6 @@ int main(int argc, char *argv[])
LIBSSH2_FXF_TRUNC, LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if(!sftp_handle && if(!sftp_handle &&
(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to open file with SFTP\n"); fprintf(stderr, "Unable to open file with SFTP\n");

View File

@ -14,12 +14,12 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -39,10 +39,10 @@
#define __FILESIZE "llu" #define __FILESIZE "llu"
#endif #endif
const char *keyfile1 = "~/.ssh/id_rsa.pub"; static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *keyfile2 = "~/.ssh/id_rsa"; static const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; static const char *username = "username";
const char *password = "password"; static const char *password = "password";
static void kbd_callback(const char *name, int name_len, static void kbd_callback(const char *name, int name_len,
const char *instruction, int instruction_len, const char *instruction, int instruction_len,
@ -93,7 +93,6 @@ int main(int argc, char *argv[])
else { else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -119,8 +118,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -204,8 +202,9 @@ int main(int argc, char *argv[])
} }
else if(auth_pw & 4) { else if(auth_pw & 4) {
/* Or by public key */ /* Or by public key */
if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, if(libssh2_userauth_publickey_fromfile(session, username,
keyfile2, password)) { pubkey, privkey,
password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} }
@ -277,8 +276,9 @@ int main(int argc, char *argv[])
printf("%s\n", mem); printf("%s\n", mem);
} }
} }
else else {
break; break;
}
} while(1); } while(1);

View File

@ -14,12 +14,12 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -47,11 +47,11 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
const char *sftppath = "/tmp/secretdir"; const char *sftppath = "/tmp/secretdir";
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
int rc; int rc;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
LIBSSH2_SFTP_HANDLE *sftp_handle; LIBSSH2_SFTP_HANDLE *sftp_handle;
@ -73,7 +73,6 @@ int main(int argc, char *argv[])
else { else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -99,8 +98,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }

View File

@ -18,32 +18,31 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
#include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#if defined(_MSC_VER) && _MSC_VER < 1900 #if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf _snprintf #define snprintf _snprintf
#endif #endif
const char *keyfile1 = ".ssh/id_rsa.pub"; static const char *pubkey = ".ssh/id_rsa.pub";
const char *keyfile2 = ".ssh/id_rsa"; static const char *privkey = ".ssh/id_rsa";
const char *username = "username"; static const char *username = "username";
const char *password = "password"; static const char *password = "password";
static void kbd_callback(const char *name, int name_len, static void kbd_callback(const char *name, int name_len,
const char *instruction, int instruction_len, const char *instruction, int instruction_len,
@ -62,8 +61,7 @@ static void kbd_callback(const char *name, int name_len,
} }
(void)prompts; (void)prompts;
(void)abstract; (void)abstract;
} /* kbd_callback */ }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -92,7 +90,6 @@ int main(int argc, char *argv[])
else { else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -118,8 +115,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "Connecting to %s:%d as user %s\n", fprintf(stderr, "Connecting to %s:%d as user %s\n",
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), username); inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), username);
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -146,10 +142,10 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
/* At this point we have not authenticated. The first thing to do is check /* At this point we have not yet authenticated. The first thing to do
* the hostkey's fingerprint against our known hosts Your app may have it * is check the hostkey's fingerprint against our known hosts Your app
* hard coded, may go to a file, may present it to the user, that's your * may have it hard coded, may go to a file, may present it to the
* call * user, that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
fprintf(stderr, "Fingerprint: "); fprintf(stderr, "Fingerprint: ");
@ -215,8 +211,8 @@ int main(int argc, char *argv[])
char const *h = getenv("HOME"); char const *h = getenv("HOME");
if(!h || !*h) if(!h || !*h)
h = "."; h = ".";
fn1sz = strlen(h) + strlen(keyfile1) + 2; fn1sz = strlen(h) + strlen(pubkey) + 2;
fn2sz = strlen(h) + strlen(keyfile2) + 2; fn2sz = strlen(h) + strlen(privkey) + 2;
fn1 = malloc(fn1sz); fn1 = malloc(fn1sz);
fn2 = malloc(fn2sz); fn2 = malloc(fn2sz);
if(!fn1 || !fn2) { if(!fn1 || !fn2) {
@ -226,11 +222,12 @@ int main(int argc, char *argv[])
goto shutdown; goto shutdown;
} }
/* Using asprintf() here would be much cleaner, but less portable */ /* Using asprintf() here would be much cleaner, but less portable */
snprintf(fn1, fn1sz, "%s/%s", h, keyfile1); snprintf(fn1, fn1sz, "%s/%s", h, pubkey);
snprintf(fn2, fn2sz, "%s/%s", h, keyfile2); snprintf(fn2, fn2sz, "%s/%s", h, privkey);
if(libssh2_userauth_publickey_fromfile(session, username, fn1, if(libssh2_userauth_publickey_fromfile(session, username,
fn2, password)) { fn1, fn2,
password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
free(fn2); free(fn2);
free(fn1); free(fn1);
@ -290,7 +287,8 @@ int main(int argc, char *argv[])
* libssh2_channel_write() * libssh2_channel_write()
* libssh2_channel_write_stderr() * libssh2_channel_write_stderr()
* *
* Blocking mode may be (en|dis)abled with: libssh2_channel_set_blocking() * Blocking mode may be (en|dis)abled with:
* libssh2_channel_set_blocking()
* If the server send EOF, libssh2_channel_eof() will return non-0 * If the server send EOF, libssh2_channel_eof() will return non-0
* To send EOF to the server use: libssh2_channel_send_eof() * To send EOF to the server use: libssh2_channel_send_eof()
* A channel can be closed with: libssh2_channel_close() * A channel can be closed with: libssh2_channel_close()
@ -333,8 +331,7 @@ int main(int argc, char *argv[])
shutdown: shutdown:
libssh2_session_disconnect(session, libssh2_session_disconnect(session, "Normal Shutdown");
"Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32
@ -342,7 +339,7 @@ int main(int argc, char *argv[])
#else #else
close(sock); close(sock);
#endif #endif
fprintf(stderr, "all done!\n"); fprintf(stderr, "all done\n");
libssh2_exit(); libssh2_exit();

View File

@ -13,12 +13,12 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -27,10 +27,10 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
const char *username = "username"; static const char *username = "username";
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -86,8 +86,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
goto shutdown; goto shutdown;
} }
@ -101,10 +100,10 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
/* At this point we have not authenticated. The first thing to do is check /* At this point we have not yet authenticated. The first thing to do
* the hostkey's fingerprint against our known hosts Your app may have it * is check the hostkey's fingerprint against our known hosts Your app
* hard coded, may go to a file, may present it to the user, that's your * may have it hard coded, may go to a file, may present it to the
* call * user, that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
fprintf(stderr, "Fingerprint: "); fprintf(stderr, "Fingerprint: ");
@ -228,8 +227,7 @@ int main(int argc, char *argv[])
} }
if(session) { if(session) {
libssh2_session_disconnect(session, libssh2_session_disconnect(session, "Normal Shutdown");
"Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
} }
@ -241,7 +239,7 @@ int main(int argc, char *argv[])
#endif #endif
} }
fprintf(stderr, "all done!\n"); fprintf(stderr, "all done\n");
libssh2_exit(); libssh2_exit();

View File

@ -19,27 +19,27 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
#ifdef HAVE_SYS_TIME_H #ifdef HAVE_SYS_TIME_H
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <ctype.h> #include <ctype.h>
static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session) static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
@ -122,8 +122,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -273,8 +272,7 @@ int main(int argc, char *argv[])
shutdown: shutdown:
libssh2_session_disconnect(session, libssh2_session_disconnect(session, "Normal Shutdown");
"Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

View File

@ -14,26 +14,27 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
#ifdef HAVE_SYS_TIME_H #ifdef HAVE_SYS_TIME_H
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <ctype.h> #include <ctype.h>
static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session) static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
@ -98,10 +99,9 @@ int main(int argc, char *argv[])
} }
#endif #endif
if(argc > 1) if(argc > 1) {
/* must be ip address only */ hostname = argv[1]; /* must be ip address only */
hostname = argv[1]; }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -126,8 +126,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -349,8 +348,7 @@ int main(int argc, char *argv[])
} }
} }
libssh2_session_disconnect(session, libssh2_session_disconnect(session, "Normal Shutdown");
"Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

View File

@ -16,26 +16,27 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
#ifdef HAVE_SYS_TIME_H #ifdef HAVE_SYS_TIME_H
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <ctype.h> #include <ctype.h>
static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session) static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
@ -72,6 +73,8 @@ int main(int argc, char *argv[])
{ {
const char *hostname = "127.0.0.1"; const char *hostname = "127.0.0.1";
const char *commandline = "uptime"; 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 *username = "user";
const char *password = "password"; const char *password = "password";
uint32_t hostaddr; uint32_t hostaddr;
@ -99,10 +102,9 @@ int main(int argc, char *argv[])
} }
#endif #endif
if(argc > 1) if(argc > 1) {
/* must be ip address only */ hostname = argv[1]; /* must be ip address only */
hostname = argv[1]; }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -130,8 +132,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -213,10 +214,7 @@ int main(int argc, char *argv[])
else { else {
/* Or by public key */ /* Or by public key */
while((rc = libssh2_userauth_publickey_fromfile(session, username, while((rc = libssh2_userauth_publickey_fromfile(session, username,
"/home/user/" pubkey, privkey,
".ssh/id_rsa.pub",
"/home/user/"
".ssh/id_rsa",
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if(rc) { if(rc) {
@ -244,7 +242,7 @@ int main(int argc, char *argv[])
waitsocket(sock, session); waitsocket(sock, session);
} }
if(rc != 0) { if(rc != 0) {
fprintf(stderr, "Error\n"); fprintf(stderr, "exec error\n");
exit(1); exit(1);
} }
for(;;) { for(;;) {
@ -299,8 +297,7 @@ int main(int argc, char *argv[])
shutdown: shutdown:
libssh2_session_disconnect(session, libssh2_session_disconnect(session, "Normal Shutdown");
"Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

View File

@ -4,6 +4,12 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_NETINET_IN_H #ifdef HAVE_NETINET_IN_H
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
@ -14,18 +20,12 @@
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#include <sys/types.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef HAVE_UNISTD_H #include <string.h>
#include <unistd.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifndef INADDR_NONE #ifndef INADDR_NONE
#define INADDR_NONE (in_addr_t)~0 #define INADDR_NONE (in_addr_t)~0
@ -35,12 +35,12 @@
#define snprintf _snprintf #define snprintf _snprintf
#endif #endif
const char *keyfile1 = "/home/username/.ssh/id_rsa.pub"; static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *keyfile2 = "/home/username/.ssh/id_rsa"; static const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; static const char *username = "username";
const char *password = ""; static const char *password = "";
const char *server_ip = "127.0.0.1"; static const char *server_ip = "127.0.0.1";
enum { enum {
AUTH_NONE = 0, AUTH_NONE = 0,
@ -147,11 +147,7 @@ int main(int argc, char *argv[])
/* Connect to SSH server */ /* Connect to SSH server */
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == LIBSSH2_INVALID_SOCKET) { if(sock == LIBSSH2_INVALID_SOCKET) {
#ifdef WIN32
fprintf(stderr, "failed to open socket!\n"); fprintf(stderr, "failed to open socket!\n");
#else
perror("socket");
#endif
return -1; return -1;
} }
@ -162,8 +158,7 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
sin.sin_port = htons(830); sin.sin_port = htons(830);
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr)); fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr));
return -1; return -1;
} }
@ -219,12 +214,13 @@ int main(int argc, char *argv[])
} }
} }
else if(auth & AUTH_PUBLICKEY) { else if(auth & AUTH_PUBLICKEY) {
if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, if(libssh2_userauth_publickey_fromfile(session, username,
keyfile2, password)) { pubkey, privkey,
fprintf(stderr, "Authentication by public key failed!\n"); password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} }
fprintf(stderr, "Authentication by public key succeeded.\n"); fprintf(stderr, "\tAuthentication by public key succeeded.\n");
} }
else { else {
fprintf(stderr, "No supported authentication methods found!\n"); fprintf(stderr, "No supported authentication methods found!\n");

View File

@ -10,6 +10,12 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_NETINET_IN_H #ifdef HAVE_NETINET_IN_H
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
@ -20,34 +26,30 @@
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#include <sys/types.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifndef INADDR_NONE #ifndef INADDR_NONE
#define INADDR_NONE (in_addr_t)-1 #define INADDR_NONE (in_addr_t)~0
#endif #endif
const char *keyfile1 = "/home/username/.ssh/id_rsa.pub"; static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *keyfile2 = "/home/username/.ssh/id_rsa"; static const char *privkey = "/home/username/.ssh/id_rsa";
const char *username = "username"; static const char *username = "username";
const char *password = ""; static const char *password = "";
const char *server_ip = "127.0.0.1"; static const char *server_ip = "127.0.0.1";
/* resolved by the server */
static const char *remote_listenhost = "localhost";
const char *remote_listenhost = "localhost"; /* resolved by the server */
int remote_wantport = 2222; int remote_wantport = 2222;
int remote_listenport; int remote_listenport;
const char *local_destip = "127.0.0.1"; static const char *local_destip = "127.0.0.1";
int local_destport = 22; int local_destport = 22;
enum { enum {
@ -108,24 +110,19 @@ int main(int argc, char *argv[])
/* Connect to SSH server */ /* Connect to SSH server */
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == LIBSSH2_INVALID_SOCKET) { if(sock == LIBSSH2_INVALID_SOCKET) {
#ifdef WIN32
fprintf(stderr, "failed to open socket!\n"); fprintf(stderr, "failed to open socket!\n");
#else
perror("socket");
#endif
return -1; return -1;
} }
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(server_ip); sin.sin_addr.s_addr = inet_addr(server_ip);
if(INADDR_NONE == sin.sin_addr.s_addr) { if(INADDR_NONE == sin.sin_addr.s_addr) {
perror("inet_addr"); fprintf(stderr, "inet_addr: Invalid IP address \"%s\"\n", server_ip);
return -1; return -1;
} }
sin.sin_port = htons(22); sin.sin_port = htons(22);
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr));
fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -180,8 +177,9 @@ int main(int argc, char *argv[])
} }
} }
else if(auth & AUTH_PUBLICKEY) { else if(auth & AUTH_PUBLICKEY) {
if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, if(libssh2_userauth_publickey_fromfile(session, username,
keyfile2, password)) { pubkey, privkey,
password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} }
@ -221,11 +219,7 @@ int main(int argc, char *argv[])
local_destip, local_destport); local_destip, local_destport);
forwardsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); forwardsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(forwardsock == LIBSSH2_INVALID_SOCKET) { if(forwardsock == LIBSSH2_INVALID_SOCKET) {
#ifdef WIN32
fprintf(stderr, "failed to open forward socket!\n"); fprintf(stderr, "failed to open forward socket!\n");
#else
perror("socket");
#endif
goto shutdown; goto shutdown;
} }

View File

@ -12,7 +12,6 @@
#ifdef HAVE_SYS_UN_H #ifdef HAVE_SYS_UN_H
#include <string.h>
#ifdef HAVE_SYS_IOCTL_H #ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h> #include <sys/ioctl.h>
#endif #endif
@ -25,20 +24,22 @@
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <sys/types.h> #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_SYS_UN_H #ifdef HAVE_SYS_UN_H
#include <sys/un.h> #include <sys/un.h>
#endif #endif
#include <sys/types.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <termios.h> #include <termios.h>
@ -85,8 +86,7 @@ static void remove_node(struct chan_X11_list *elem)
static void session_shutdown(LIBSSH2_SESSION *session) static void session_shutdown(LIBSSH2_SESSION *session)
{ {
libssh2_session_disconnect(session, libssh2_session_disconnect(session, "Session Shutdown");
"Session Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
} }
@ -211,7 +211,6 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock)
timeval_out.tv_sec = 0; timeval_out.tv_sec = 0;
timeval_out.tv_usec = 0; timeval_out.tv_usec = 0;
FD_ZERO(&set); FD_ZERO(&set);
FD_SET(sock, &set); FD_SET(sock, &set);
@ -295,7 +294,6 @@ main (int argc, char *argv[])
timeval_out.tv_sec = 0; timeval_out.tv_sec = 0;
timeval_out.tv_usec = 10; timeval_out.tv_usec = 10;
if(argc > 3) { if(argc > 3) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
username = argv[2]; username = argv[2];
@ -328,9 +326,7 @@ main (int argc, char *argv[])
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
rc = connect(sock, (struct sockaddr *) &sin, if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in));
if(rc != 0) {
fprintf(stderr, "Failed to established connection!\n"); fprintf(stderr, "Failed to established connection!\n");
return -1; return -1;
} }
@ -371,7 +367,6 @@ main (int argc, char *argv[])
return -1; return -1;
} }
/* Request a PTY */ /* Request a PTY */
rc = libssh2_channel_request_pty(channel, "xterm"); rc = libssh2_channel_request_pty(channel, "xterm");
if(rc != 0) { if(rc != 0) {
@ -468,7 +463,6 @@ main (int argc, char *argv[])
current_node = next_node; current_node = next_node;
} }
rc = select((int)(fileno(stdin) + 1), &set, NULL, NULL, &timeval_out); rc = select((int)(fileno(stdin) + 1), &set, NULL, NULL, &timeval_out);
if(rc > 0) { if(rc > 0) {
ssize_t nread; ssize_t nread;

View File

@ -390,7 +390,7 @@ static libssh2_socket_t open_socket_to_container(char *container_id)
for(counter = 0; counter < 3; ++counter) { for(counter = 0; counter < 3; ++counter) {
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in))) {
fprintf(stderr, fprintf(stderr,
"Connection to %s:%s attempt #%d failed: retrying...\n", "Connection to %s:%s attempt #%d failed: retrying...\n",
ip_address, port_string, counter); ip_address, port_string, counter);

View File

@ -36,11 +36,11 @@
* OF SUCH DAMAGE. * OF SUCH DAMAGE.
*/ */
#include <libssh2.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "libssh2.h"
static int test_libssh2_base64_decode(LIBSSH2_SESSION *session) static int test_libssh2_base64_decode(LIBSSH2_SESSION *session)
{ {
char *data; char *data;

View File

@ -1,4 +1,4 @@
/* Self test, based on examples/ssh2.c. */ /* Self test, based on example/ssh2.c. */
#include "libssh2_setup.h" #include "libssh2_setup.h"
#include <libssh2.h> #include <libssh2.h>
@ -7,12 +7,12 @@
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -34,8 +34,8 @@ int main(int argc, char *argv[])
char *userauthlist; char *userauthlist;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *channel; LIBSSH2_CHANNEL *channel;
const char *pubkeyfile = "etc/user.pub"; const char *pubkey = "etc/user.pub";
const char *privkeyfile = "etc/user"; const char *privkey = "etc/user";
const char *username = "username"; const char *username = "username";
const char *password = "password"; const char *password = "password";
int ec = 1; int ec = 1;
@ -58,10 +58,10 @@ int main(int argc, char *argv[])
username = getenv("USER"); username = getenv("USER");
if(getenv("PRIVKEY")) if(getenv("PRIVKEY"))
privkeyfile = getenv("PRIVKEY"); privkey = getenv("PRIVKEY");
if(getenv("PUBKEY")) if(getenv("PUBKEY"))
pubkeyfile = getenv("PUBKEY"); pubkey = getenv("PUBKEY");
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
@ -72,8 +72,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(4711); sin.sin_port = htons(4711);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return 1; return 1;
} }
@ -117,8 +116,9 @@ int main(int argc, char *argv[])
if(auth_pw & 4) { if(auth_pw & 4) {
/* Authenticate by public key */ /* Authenticate by public key */
if(libssh2_userauth_publickey_fromfile(session, username, pubkeyfile, if(libssh2_userauth_publickey_fromfile(session, username,
privkeyfile, password)) { pubkey, privkey,
password)) {
printf("\tAuthentication by public key failed!\n"); printf("\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} }