@@ -4,7 +4,7 @@
* The sample code has default values for host name, user name, password
* and path to copy, but you can specify them on the command line like:
*
* "sftp 192.168.0.1 user password sftp_write_nonblock.c /tmp/sftp_write_nonblock.c"
* "sftp 192.168.0.1 user password thisfile /tmp/storehere"
*/

#include "libssh2_config.h"
@@ -77,10 +77,10 @@ int main(int argc, char *argv[])
struct sockaddr_in sin;
const char *fingerprint;
LIBSSH2_SESSION *session;
const char *username="username";
const char *password="password";
const char *loclfile="sftp_write_nonblock.c";
const char *sftppath="/tmp/sftp_write_nonblock.c";
const char *username = "username";
const char *password = "password";
const char *loclfile = "sftp_write_nonblock.c";
const char *sftppath = "/tmp/sftp_write_nonblock.c";
int rc;
FILE *local;
LIBSSH2_SFTP *sftp_session;
@@ -96,40 +96,41 @@ int main(int argc, char *argv[])
WSADATA wsadata;
int err;

err = WSAStartup(MAKEWORD(2,0), &wsadata);
if (err != 0) {
err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1;
}
#endif

if (argc > 1) {
if(argc > 1) {
hostaddr = inet_addr(argv[1]);
} else {
}
else {
hostaddr = htonl(0x7F000001);
}

if (argc > 2) {
if(argc > 2) {
username = argv[2];
}
if (argc > 3) {
if(argc > 3) {
password = argv[3];
}
if (argc > 4) {
if(argc > 4) {
loclfile = argv[4];
}
if (argc > 5) {
if(argc > 5) {
sftppath = argv[5];
}

rc = libssh2_init (0);
if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
rc = libssh2_init(0);
if(rc != 0) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}

local = fopen(loclfile, "rb");
if (!local) {
if(!local) {
fprintf(stderr, "Can't open local file %s\n", loclfile);
return -1;
}
@@ -143,7 +144,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET;
sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin),
if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
return -1;
@@ -152,7 +153,7 @@ int main(int argc, char *argv[])
/* Create a session instance
*/
session = libssh2_session_init();
if (!session)
if(!session)
return -1;

/* Since we have set non-blocking, tell libssh2 we are non-blocking */
@@ -161,9 +162,9 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers
*/
while ((rc = libssh2_session_handshake(session, sock))
while((rc = libssh2_session_handshake(session, sock))
== LIBSSH2_ERROR_EAGAIN);
if (rc) {
if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1;
}
@@ -180,22 +181,24 @@ int main(int argc, char *argv[])
}
fprintf(stderr, "\n");

if (auth_pw) {
if(auth_pw) {
/* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) ==
while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
if(rc) {
fprintf(stderr, "Authentication by password failed.\n");
goto shutdown;
}
} else {
}
else {
/* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa",
password)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
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,
pubkey, privkey,
password)) ==
LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown;
}
@@ -205,36 +208,36 @@ int main(int argc, char *argv[])
do {
sftp_session = libssh2_sftp_init(session);

if (!sftp_session &&
if(!sftp_session &&
(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown;
}
} while (!sftp_session);
} while(!sftp_session);

fprintf(stderr, "libssh2_sftp_open()!\n");
/* Request a file via SFTP */
do {
sftp_handle =
libssh2_sftp_open(sftp_session, sftppath,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);

if (!sftp_handle &&
(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
libssh2_sftp_open(sftp_session, sftppath,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|
LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if(!sftp_handle &&
(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown;
}
} while (!sftp_handle);
} while(!sftp_handle);

fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n");

start = time(NULL);

do {
nread = fread(mem, 1, sizeof(mem), local);
if (nread <= 0) {
if(nread <= 0) {
/* end of file */
break;
}
@@ -244,7 +247,7 @@ int main(int argc, char *argv[])

do {
/* write data in a loop until we block */
while ((rc = libssh2_sftp_write(sftp_handle, ptr, nread)) ==
while((rc = libssh2_sftp_write(sftp_handle, ptr, nread)) ==
LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session);
}
@@ -253,8 +256,8 @@ int main(int argc, char *argv[])
ptr += rc;
nread -= rc;

} while (nread);
} while (rc > 0);
} while(nread);
} while(rc > 0);

duration = (int)(time(NULL)-start);

@@ -268,7 +271,7 @@ int main(int argc, char *argv[])

shutdown:

while (libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing")
while(libssh2_session_disconnect(session, "Normal Shutdown")
== LIBSSH2_ERROR_EAGAIN);
libssh2_session_free(session);

@@ -4,7 +4,7 @@
* The sample code has default values for host name, user name, password
* and path to copy, but you can specify them on the command line like:
*
* "sftp 192.168.0.1 user password sftp_write_nonblock.c /tmp/sftp_write_nonblock.c"
* "sftp 192.168.0.1 user password file /tmp/storehere"
*/

#include "libssh2_config.h"
@@ -77,10 +77,10 @@ int main(int argc, char *argv[])
struct sockaddr_in sin;
const char *fingerprint;
LIBSSH2_SESSION *session;
const char *username="username";
const char *password="password";
const char *loclfile="sftp_write_nonblock.c";
const char *sftppath="/tmp/sftp_write_nonblock.c";
const char *username = "username";
const char *password = "password";
const char *loclfile = "sftp_write_nonblock.c";
const char *sftppath = "/tmp/sftp_write_nonblock.c";
int rc;
FILE *local;
LIBSSH2_SFTP *sftp_session;
@@ -96,40 +96,41 @@ int main(int argc, char *argv[])
WSADATA wsadata;
int err;

err = WSAStartup(MAKEWORD(2,0), &wsadata);
if (err != 0) {
err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1;
}
#endif

if (argc > 1) {
if(argc > 1) {
hostaddr = inet_addr(argv[1]);
} else {
}
else {
hostaddr = htonl(0x7F000001);
}

if (argc > 2) {
if(argc > 2) {
username = argv[2];
}
if (argc > 3) {
if(argc > 3) {
password = argv[3];
}
if (argc > 4) {
if(argc > 4) {
loclfile = argv[4];
}
if (argc > 5) {
if(argc > 5) {
sftppath = argv[5];
}

rc = libssh2_init (0);
if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
rc = libssh2_init(0);
if(rc != 0) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}

local = fopen(loclfile, "rb");
if (!local) {
if(!local) {
fprintf(stderr, "Can't open local file %s\n", loclfile);
return -1;
}
@@ -143,7 +144,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET;
sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin),
if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
return -1;
@@ -152,7 +153,7 @@ int main(int argc, char *argv[])
/* Create a session instance
*/
session = libssh2_session_init();
if (!session)
if(!session)
return -1;

/* Since we have set non-blocking, tell libssh2 we are non-blocking */
@@ -161,9 +162,9 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers
*/
while ((rc = libssh2_session_handshake(session, sock))
while((rc = libssh2_session_handshake(session, sock))
== LIBSSH2_ERROR_EAGAIN);
if (rc) {
if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1;
}
@@ -180,22 +181,24 @@ int main(int argc, char *argv[])
}
fprintf(stderr, "\n");

if (auth_pw) {
if(auth_pw) {
/* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) ==
while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
if(rc) {
fprintf(stderr, "Authentication by password failed.\n");
goto shutdown;
}
} else {
}
else {
/* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa",
password)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
#define PUBKEY "/home/username/.ssh/id_rsa.pub"
#define PRIVKEY "/home/username/.ssh/id_rsa"
while((rc = libssh2_userauth_publickey_fromfile(session, username,
PUBKEY, PRIVKEY,
password)) ==
LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown;
}
@@ -205,28 +208,29 @@ int main(int argc, char *argv[])
do {
sftp_session = libssh2_sftp_init(session);

if (!sftp_session &&
if(!sftp_session &&
(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown;
}
} while (!sftp_session);
} while(!sftp_session);

fprintf(stderr, "libssh2_sftp_open()!\n");
/* Request a file via SFTP */
do {
sftp_handle =
libssh2_sftp_open(sftp_session, sftppath,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);

if (!sftp_handle &&
(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
libssh2_sftp_open(sftp_session, sftppath,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|
LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);

if(!sftp_handle &&
(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown;
}
} while (!sftp_handle);
} while(!sftp_handle);

fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n");

@@ -235,9 +239,9 @@ int main(int argc, char *argv[])
memuse = 0; /* it starts blank */
do {
nread = fread(&mem[memuse], 1, sizeof(mem)-memuse, local);
if (nread <= 0) {
if(nread <= 0) {
/* end of file */
if (memuse > 0)
if(memuse > 0)
/* the previous sending is not finished */
nread = 0;
else
@@ -247,7 +251,7 @@ int main(int argc, char *argv[])
total += nread;

/* write data in a loop until we block */
while ((rc = libssh2_sftp_write(sftp_handle, mem, memuse)) ==
while((rc = libssh2_sftp_write(sftp_handle, mem, memuse)) ==
LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session);
}
@@ -263,7 +267,7 @@ int main(int argc, char *argv[])
/* 'mem' was consumed fully */
memuse = 0;

} while (rc > 0);
} while(rc > 0);

duration = (int)(time(NULL)-start);

@@ -277,8 +281,8 @@ int main(int argc, char *argv[])

shutdown:

while (libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing")
== LIBSSH2_ERROR_EAGAIN);
while(libssh2_session_disconnect(session, "Normal Shutdown")
== LIBSSH2_ERROR_EAGAIN);
libssh2_session_free(session);

#ifdef WIN32
@@ -36,26 +36,16 @@
#include <stdio.h>
#include <ctype.h>

/* last resort for systems not defining PRIu64 in inttypes.h */
#ifndef __PRI64_PREFIX
#ifdef WIN32
#define __PRI64_PREFIX "I64"
#define __FILESIZE "I64"
#else
#if __WORDSIZE == 64
#define __PRI64_PREFIX "l"
#else
#define __PRI64_PREFIX "ll"
#endif /* __WORDSIZE */
#endif /* WIN32 */
#endif /* !__PRI64_PREFIX */
#ifndef PRIu64
#define PRIu64 __PRI64_PREFIX "u"
#endif /* PRIu64 */

const char *keyfile1="~/.ssh/id_rsa.pub";
const char *keyfile2="~/.ssh/id_rsa";
const char *username="username";
const char *password="password";
#define __FILESIZE "llu"
#endif

const char *keyfile1 = "~/.ssh/id_rsa.pub";
const char *keyfile2 = "~/.ssh/id_rsa";
const char *username = "username";
const char *password = "password";

static void kbd_callback(const char *name, int name_len,
const char *instruction, int instruction_len,
@@ -68,7 +58,7 @@ static void kbd_callback(const char *name, int name_len,
(void)name_len;
(void)instruction;
(void)instruction_len;
if (num_prompts == 1) {
if(num_prompts == 1) {
responses[0].text = strdup(password);
responses[0].length = strlen(password);
}
@@ -84,24 +74,25 @@ int main(int argc, char *argv[])
const char *fingerprint;
char *userauthlist;
LIBSSH2_SESSION *session;
const char *sftppath="/tmp/secretdir";
const char *sftppath = "/tmp/secretdir";
LIBSSH2_SFTP *sftp_session;
LIBSSH2_SFTP_HANDLE *sftp_handle;

#ifdef WIN32
WSADATA wsadata;
int err;

err = WSAStartup(MAKEWORD(2,0), &wsadata);
if (err != 0) {
err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1;
}
#endif

if (argc > 1) {
if(argc > 1) {
hostaddr = inet_addr(argv[1]);
} else {
}
else {
hostaddr = htonl(0x7F000001);
}

@@ -115,9 +106,9 @@ int main(int argc, char *argv[])
sftppath = argv[4];
}

rc = libssh2_init (0);
if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
rc = libssh2_init(0);
if(rc != 0) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}

@@ -130,8 +121,8 @@ int main(int argc, char *argv[])
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)) != 0) {
if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
return -1;
}
@@ -166,66 +157,72 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password") != NULL) {
if(strstr(userauthlist, "password") != NULL) {
auth_pw |= 1;
}
if (strstr(userauthlist, "keyboard-interactive") != NULL) {
if(strstr(userauthlist, "keyboard-interactive") != NULL) {
auth_pw |= 2;
}
if (strstr(userauthlist, "publickey") != NULL) {
if(strstr(userauthlist, "publickey") != NULL) {
auth_pw |= 4;
}

/* if we got an 5. argument we set this option if supported */
if(argc > 5) {
if ((auth_pw & 1) && !strcasecmp(argv[5], "-p")) {
if((auth_pw & 1) && !strcasecmp(argv[5], "-p")) {
auth_pw = 1;
}
if ((auth_pw & 2) && !strcasecmp(argv[5], "-i")) {
if((auth_pw & 2) && !strcasecmp(argv[5], "-i")) {
auth_pw = 2;
}
if ((auth_pw & 4) && !strcasecmp(argv[5], "-k")) {
if((auth_pw & 4) && !strcasecmp(argv[5], "-k")) {
auth_pw = 4;
}
}

if (auth_pw & 1) {
if(auth_pw & 1) {
/* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) {
if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "\tAuthentication by password failed!\n");
goto shutdown;
} else {
}
else {
fprintf(stderr, "\tAuthentication by password succeeded.\n");
}
} else if (auth_pw & 2) {
}
else if(auth_pw & 2) {
/* Or via keyboard-interactive */
if (libssh2_userauth_keyboard_interactive(session, username,
&kbd_callback) ) {
if(libssh2_userauth_keyboard_interactive(session, username,
&kbd_callback) ) {
fprintf(stderr,
"\tAuthentication by keyboard-interactive failed!\n");
"\tAuthentication by keyboard-interactive failed!\n");
goto shutdown;
} else {
}
else {
fprintf(stderr,
"\tAuthentication by keyboard-interactive succeeded.\n");
"\tAuthentication by keyboard-interactive succeeded.\n");
}
} else if (auth_pw & 4) {
}
else if(auth_pw & 4) {
/* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) {
if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown;
} else {
}
else {
fprintf(stderr, "\tAuthentication by public key succeeded.\n");
}
} else {
}
else {
fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown;
}

fprintf(stderr, "libssh2_sftp_init()!\n");
sftp_session = libssh2_sftp_init(session);

if (!sftp_session) {
if(!sftp_session) {
fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown;
}
@@ -237,7 +234,7 @@ int main(int argc, char *argv[])
/* Request a dir listing via SFTP */
sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);

if (!sftp_handle) {
if(!sftp_handle) {
fprintf(stderr, "Unable to open dir with SFTP\n");
goto shutdown;
}
@@ -254,9 +251,10 @@ int main(int argc, char *argv[])
/* rc is the length of the file name in the mem
buffer */

if (longentry[0] != '\0') {
if(longentry[0] != '\0') {
printf("%s\n", longentry);
} else {
}
else {
if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
/* this should check what permissions it
is and print the output accordingly */
@@ -267,14 +265,14 @@ int main(int argc, char *argv[])
}

if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
printf("%4ld %4ld ", attrs.uid, attrs.gid);
printf("%4d %4d ", (int) attrs.uid, (int) attrs.gid);
}
else {
printf(" - - ");
}

if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
printf("%8" PRIu64 " ", attrs.filesize);
printf("%8" __FILESIZE " ", attrs.filesize);
}

printf("%s\n", mem);
@@ -283,14 +281,14 @@ int main(int argc, char *argv[])
else
break;

} while (1);
} while(1);

libssh2_sftp_closedir(sftp_handle);
libssh2_sftp_shutdown(sftp_session);

shutdown:

libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
libssh2_session_disconnect(session, "Normal Shutdown");
libssh2_session_free(session);

#ifdef WIN32
@@ -36,21 +36,11 @@
#include <stdio.h>
#include <ctype.h>

/* last resort for systems not defining PRIu64 in inttypes.h */
#ifndef __PRI64_PREFIX
#ifdef WIN32
#define __PRI64_PREFIX "I64"
#define __FILESIZE "I64"
#else
#if __WORDSIZE == 64
#define __PRI64_PREFIX "l"
#else
#define __PRI64_PREFIX "ll"
#endif /* __WORDSIZE */
#endif /* WIN32 */
#endif /* !__PRI64_PREFIX */
#ifndef PRIu64
#define PRIu64 __PRI64_PREFIX "u"
#endif /* PRIu64 */
#define __FILESIZE "llu"
#endif

int main(int argc, char *argv[])
{
@@ -59,9 +49,11 @@ int main(int argc, char *argv[])
struct sockaddr_in sin;
const char *fingerprint;
LIBSSH2_SESSION *session;
const char *username="username";
const char *password="password";
const char *sftppath="/tmp/secretdir";
const char *username = "username";
const char *password = "password";
const char *sftppath = "/tmp/secretdir";
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
int rc;
LIBSSH2_SFTP *sftp_session;
LIBSSH2_SFTP_HANDLE *sftp_handle;
@@ -70,16 +62,17 @@ int main(int argc, char *argv[])
WSADATA wsadata;
int err;

err = WSAStartup(MAKEWORD(2,0), &wsadata);
if (err != 0) {
err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1;
}
#endif

if (argc > 1) {
if(argc > 1) {
hostaddr = inet_addr(argv[1]);
} else {
}
else {
hostaddr = htonl(0x7F000001);
}

@@ -93,9 +86,9 @@ int main(int argc, char *argv[])
sftppath = argv[4];
}

rc = libssh2_init (0);
if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
rc = libssh2_init(0);
if(rc != 0) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}

@@ -108,8 +101,8 @@ int main(int argc, char *argv[])
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)) != 0) {
if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
return -1;
}
@@ -126,8 +119,8 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers
*/
while ((rc = libssh2_session_handshake(session, sock)) ==
LIBSSH2_ERROR_EAGAIN);
while((rc = libssh2_session_handshake(session, sock)) ==
LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1;
@@ -145,21 +138,22 @@ int main(int argc, char *argv[])
}
fprintf(stderr, "\n");

if (auth_pw) {
if(auth_pw) {
/* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "Authentication by password failed.\n");
goto shutdown;
}
} else {
}
else {
/* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa",
password)) == LIBSSH2_ERROR_EAGAIN);
if (rc) {
while((rc = libssh2_userauth_publickey_fromfile(session, username,
pubkey, privkey,
password)) ==
LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown;
}
@@ -169,33 +163,33 @@ int main(int argc, char *argv[])
do {
sftp_session = libssh2_sftp_init(session);

if ((!sftp_session) && (libssh2_session_last_errno(session) !=
LIBSSH2_ERROR_EAGAIN)) {
if((!sftp_session) && (libssh2_session_last_errno(session) !=
LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown;
}
} while (!sftp_session);
} while(!sftp_session);

fprintf(stderr, "libssh2_sftp_opendir()!\n");
/* Request a dir listing via SFTP */
do {
sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);

if ((!sftp_handle) && (libssh2_session_last_errno(session) !=
LIBSSH2_ERROR_EAGAIN)) {
if((!sftp_handle) && (libssh2_session_last_errno(session) !=
LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to open dir with SFTP\n");
goto shutdown;
}
} while (!sftp_handle);
} while(!sftp_handle);

fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n");
do {
char mem[512];
LIBSSH2_SFTP_ATTRIBUTES attrs;

/* loop until we fail */
while ((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem),
&attrs)) == LIBSSH2_ERROR_EAGAIN) {
while((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem),
&attrs)) == LIBSSH2_ERROR_EAGAIN) {
;
}
if(rc > 0) {
@@ -206,37 +200,40 @@ int main(int argc, char *argv[])
/* this should check what permissions it
is and print the output accordingly */
printf("--fix----- ");
} else {
}
else {
printf("---------- ");
}

if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
printf("%4ld %4ld ", attrs.uid, attrs.gid);
} else {
printf("%4d %4d ", (int) attrs.uid, (int) attrs.gid);
}
else {
printf(" - - ");
}

if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
printf("%8" PRIu64 " ", attrs.filesize);
printf("%8" __FILESIZE " ", attrs.filesize);
}

printf("%s\n", mem);
}
else if (rc == LIBSSH2_ERROR_EAGAIN) {
else if(rc == LIBSSH2_ERROR_EAGAIN) {
/* blocking */
fprintf(stderr, "Blocking\n");
} else {
}
else {
break;
}

} while (1);
} while(1);

libssh2_sftp_closedir(sftp_handle);
libssh2_sftp_shutdown(sftp_session);

shutdown:

libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
libssh2_session_disconnect(session, "Normal Shutdown");
libssh2_session_free(session);

#ifdef WIN32
@@ -37,10 +37,10 @@
#include <ctype.h>


const char *keyfile1="~/.ssh/id_rsa.pub";
const char *keyfile2="~/.ssh/id_rsa";
const char *username="username";
const char *password="password";
const char *keyfile1 = "~/.ssh/id_rsa.pub";
const char *keyfile2 = "~/.ssh/id_rsa";
const char *username = "username";
const char *password = "password";


static void kbd_callback(const char *name, int name_len,
@@ -54,7 +54,7 @@ static void kbd_callback(const char *name, int name_len,
(void)name_len;
(void)instruction;
(void)instruction_len;
if (num_prompts == 1) {
if(num_prompts == 1) {
responses[0].text = strdup(password);
responses[0].length = strlen(password);
}
@@ -77,16 +77,17 @@ int main(int argc, char *argv[])
WSADATA wsadata;
int err;

err = WSAStartup(MAKEWORD(2,0), &wsadata);
if (err != 0) {
err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1;
}
#endif

if (argc > 1) {
if(argc > 1) {
hostaddr = inet_addr(argv[1]);
} else {
}
else {
hostaddr = htonl(0x7F000001);
}

@@ -97,9 +98,9 @@ int main(int argc, char *argv[])
password = argv[3];
}

rc = libssh2_init (0);
if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
rc = libssh2_init(0);
if(rc != 0) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}

@@ -111,7 +112,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET;
sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin),
if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
return -1;
@@ -121,7 +122,7 @@ int main(int argc, char *argv[])
* banners, exchange keys, and setup crypto, compression, and MAC layers
*/
session = libssh2_session_init();
if (libssh2_session_handshake(session, sock)) {
if(libssh2_session_handshake(session, sock)) {
fprintf(stderr, "Failure establishing SSH session\n");
return -1;
}
@@ -141,64 +142,71 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password") != NULL) {
if(strstr(userauthlist, "password") != NULL) {
auth_pw |= 1;
}
if (strstr(userauthlist, "keyboard-interactive") != NULL) {
if(strstr(userauthlist, "keyboard-interactive") != NULL) {
auth_pw |= 2;
}
if (strstr(userauthlist, "publickey") != NULL) {
if(strstr(userauthlist, "publickey") != NULL) {
auth_pw |= 4;
}

/* if we got an 4. argument we set this option if supported */
if(argc > 4) {
if ((auth_pw & 1) && !strcasecmp(argv[4], "-p")) {
if((auth_pw & 1) && !strcasecmp(argv[4], "-p")) {
auth_pw = 1;
}
if ((auth_pw & 2) && !strcasecmp(argv[4], "-i")) {
if((auth_pw & 2) && !strcasecmp(argv[4], "-i")) {
auth_pw = 2;
}
if ((auth_pw & 4) && !strcasecmp(argv[4], "-k")) {
if((auth_pw & 4) && !strcasecmp(argv[4], "-k")) {
auth_pw = 4;
}
}

if (auth_pw & 1) {
if(auth_pw & 1) {
/* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) {
if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "\tAuthentication by password failed!\n");
goto shutdown;
} else {
}
else {
fprintf(stderr, "\tAuthentication by password succeeded.\n");
}
} else if (auth_pw & 2) {
}
else if(auth_pw & 2) {
/* Or via keyboard-interactive */
if (libssh2_userauth_keyboard_interactive(session, username,
&kbd_callback) ) {
if(libssh2_userauth_keyboard_interactive(session, username,
&kbd_callback) ) {
fprintf(stderr,
"\tAuthentication by keyboard-interactive failed!\n");
"\tAuthentication by keyboard-interactive failed!\n");
goto shutdown;
} else {
}
else {
fprintf(stderr,
"\tAuthentication by keyboard-interactive succeeded.\n");
"\tAuthentication by keyboard-interactive succeeded.\n");
}
} else if (auth_pw & 4) {
}
else if(auth_pw & 4) {
/* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) {
if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown;
} else {
}
else {
fprintf(stderr, "\tAuthentication by public key succeeded.\n");
}
} else {
}
else {
fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown;
}

/* Request a shell */
if (!(channel = libssh2_channel_open_session(session))) {
channel = libssh2_channel_open_session(session);
if(!channel) {
fprintf(stderr, "Unable to open a session\n");
goto shutdown;
}
@@ -211,13 +219,13 @@ int main(int argc, char *argv[])
/* Request a terminal with 'vanilla' terminal emulation
* See /etc/termcap for more options
*/
if (libssh2_channel_request_pty(channel, "vanilla")) {
if(libssh2_channel_request_pty(channel, "vanilla")) {
fprintf(stderr, "Failed requesting pty\n");
goto skip_shell;
}

/* Open a SHELL on that pty */
if (libssh2_channel_shell(channel)) {
if(libssh2_channel_shell(channel)) {
fprintf(stderr, "Unable to request shell on allocated pty\n");
goto shutdown;
}
@@ -236,7 +244,7 @@ int main(int argc, char *argv[])
*/

skip_shell:
if (channel) {
if(channel) {
libssh2_channel_free(channel);
channel = NULL;
}
@@ -36,7 +36,7 @@
#include <ctype.h>
#include <stdlib.h>

const char *username="username";
const char *username = "username";

int main(int argc, char *argv[])
{
@@ -54,34 +54,35 @@ int main(int argc, char *argv[])
WSADATA wsadata;
int err;

err = WSAStartup(MAKEWORD(2,0), &wsadata);
if (err != 0) {
err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1;
}
#endif

if (argc > 1) {
if(argc > 1) {
hostaddr = inet_addr(argv[1]);
} else {
}
else {
hostaddr = htonl(0x7F000001);
}

if(argc > 2) {
username = argv[2];
}

rc = libssh2_init (0);
if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
rc = libssh2_init(0);
if(rc != 0) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}

/* 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 == -1) {
if(sock == -1) {
fprintf(stderr, "failed to create socket!\n");
rc = 1;
goto shutdown;
@@ -90,7 +91,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET;
sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin),
if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
goto shutdown;
@@ -100,7 +101,7 @@ int main(int argc, char *argv[])
* banners, exchange keys, and setup crypto, compression, and MAC layers
*/
session = libssh2_session_init();
if (libssh2_session_handshake(session, sock)) {
if(libssh2_session_handshake(session, sock)) {
fprintf(stderr, "Failure establishing SSH session\n");
return 1;
}
@@ -120,59 +121,61 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "publickey") == NULL) {
if(strstr(userauthlist, "publickey") == NULL) {
fprintf(stderr, "\"publickey\" authentication is not supported\n");
goto shutdown;
}

/* Connect to the ssh-agent */
agent = libssh2_agent_init(session);
if (!agent) {
if(!agent) {
fprintf(stderr, "Failure initializing ssh-agent support\n");
rc = 1;
goto shutdown;
}
if (libssh2_agent_connect(agent)) {
if(libssh2_agent_connect(agent)) {
fprintf(stderr, "Failure connecting to ssh-agent\n");
rc = 1;
goto shutdown;
}
if (libssh2_agent_list_identities(agent)) {
if(libssh2_agent_list_identities(agent)) {
fprintf(stderr, "Failure requesting identities to ssh-agent\n");
rc = 1;
goto shutdown;
}
while (1) {
while(1) {
rc = libssh2_agent_get_identity(agent, &identity, prev_identity);
if (rc == 1)
if(rc == 1)
break;
if (rc < 0) {
if(rc < 0) {
fprintf(stderr,
"Failure obtaining identity from ssh-agent support\n");
rc = 1;
goto shutdown;
}
if (libssh2_agent_userauth(agent, username, identity)) {
if(libssh2_agent_userauth(agent, username, identity)) {
fprintf(stderr, "\tAuthentication with username %s and "
"public key %s failed!\n",
username, identity->comment);
} else {
}
else {
fprintf(stderr, "\tAuthentication with username %s and "
"public key %s succeeded!\n",
username, identity->comment);
break;
}
prev_identity = identity;
}
if (rc) {
if(rc) {
fprintf(stderr, "Couldn't continue authentication\n");
goto shutdown;
}

/* We're authenticated now. */

/* Request a shell */
if (!(channel = libssh2_channel_open_session(session))) {
channel = libssh2_channel_open_session(session);
if(!channel) {
fprintf(stderr, "Unable to open a session\n");
goto shutdown;
}
@@ -185,13 +188,13 @@ int main(int argc, char *argv[])
/* Request a terminal with 'vanilla' terminal emulation
* See /etc/termcap for more options
*/
if (libssh2_channel_request_pty(channel, "vanilla")) {
if(libssh2_channel_request_pty(channel, "vanilla")) {
fprintf(stderr, "Failed requesting pty\n");
goto skip_shell;
}

/* Open a SHELL on that pty */
if (libssh2_channel_shell(channel)) {
if(libssh2_channel_shell(channel)) {
fprintf(stderr, "Unable to request shell on allocated pty\n");
goto shutdown;
}
@@ -210,7 +213,7 @@ int main(int argc, char *argv[])
*/

skip_shell:
if (channel) {
if(channel) {
libssh2_channel_free(channel);
channel = NULL;
}
@@ -223,16 +226,18 @@ int main(int argc, char *argv[])

shutdown:

libssh2_agent_disconnect(agent);
libssh2_agent_free(agent);
if(agent) {
libssh2_agent_disconnect(agent);
libssh2_agent_free(agent);
}

if(session) {
libssh2_session_disconnect(session,
"Normal Shutdown, Thank you for playing");
libssh2_session_free(session);
}

if (sock != -1) {
if(sock != -1) {
#ifdef WIN32
closesocket(sock);
#else
@@ -87,7 +87,7 @@ int main(int argc, char *argv[])
LIBSSH2_CHANNEL *channel;
int rc;
int exitcode = 0;
char *exitsignal=(char *)"none";
char *exitsignal = (char *)"none";
size_t len;
LIBSSH2_KNOWNHOSTS *nh;
int type;
@@ -96,27 +96,27 @@ int main(int argc, char *argv[])
WSADATA wsadata;
int err;

err = WSAStartup(MAKEWORD(2,0), &wsadata);
if (err != 0) {
err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1;
}
#endif

if (argc > 1)
if(argc > 1)
/* must be ip address only */
hostname = argv[1];

if (argc > 2) {
if(argc > 2) {
username = argv[2];
}
if (argc > 3) {
if(argc > 3) {
password = argv[3];
}

rc = libssh2_init (0);
if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
rc = libssh2_init(0);
if(rc != 0) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}

@@ -131,15 +131,15 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET;
sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin),
if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
return -1;
}

/* Create a session instance */
session = libssh2_session_init();
if (!session)
if(!session)
return -1;

/* tell libssh2 we want it all done non-blocking */
@@ -148,9 +148,9 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers
*/
while ((rc = libssh2_session_handshake(session, sock)) ==
while((rc = libssh2_session_handshake(session, sock)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1;
}
@@ -193,11 +193,11 @@ int main(int argc, char *argv[])
}
libssh2_knownhost_free(nh);

if ( strlen(password) != 0 ) {
if(strlen(password) != 0) {
/* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "Authentication by password failed.\n");
exit(1);
}
@@ -206,22 +206,22 @@ int main(int argc, char *argv[])
libssh2_trace(session, LIBSSH2_TRACE_SOCKET);

/* 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 ) {
while((channel = libssh2_channel_open_session(session)) == NULL &&
libssh2_session_last_error(session, NULL, NULL, 0) ==
LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session);
}
if( channel == NULL ) {
fprintf(stderr,"Error\n");
exit( 1 );
if(channel == NULL) {
fprintf(stderr, "Error\n");
exit(1);
}
while( (rc = libssh2_channel_exec(channel, commandline)) ==
LIBSSH2_ERROR_EAGAIN )
while((rc = libssh2_channel_exec(channel, commandline)) ==
LIBSSH2_ERROR_EAGAIN)
waitsocket(sock, session);

if( rc != 0 ) {
if(rc != 0) {
fprintf(stderr, "exec error\n");
exit( 1 );
exit(1);
}
else {
LIBSSH2_POLLFD *fds = NULL;
@@ -236,10 +236,11 @@ int main(int argc, char *argv[])
int rewrites = 0;
int i;

for (i = 0; i < BUFSIZE; i++)
for(i = 0; i < BUFSIZE; i++)
buffer[i] = 'A';

if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL) {
fds = malloc(sizeof (LIBSSH2_POLLFD));
if(!fds) {
fprintf(stderr, "malloc failed\n");
exit(1);
}
@@ -252,18 +253,18 @@ int main(int argc, char *argv[])
int rc = (libssh2_poll(fds, 1, 10));
int act = 0;

if (rc < 1)
if(rc < 1)
continue;

if (fds[0].revents & LIBSSH2_POLLFD_POLLIN) {
if(fds[0].revents & LIBSSH2_POLLFD_POLLIN) {
int n = libssh2_channel_read(channel, buffer, sizeof(buffer));
act++;

if (n == LIBSSH2_ERROR_EAGAIN) {
if(n == LIBSSH2_ERROR_EAGAIN) {
rereads++;
fprintf(stderr, "will read again\n");
}
else if (n < 0) {
else if(n < 0) {
fprintf(stderr, "read failed\n");
exit(1);
}
@@ -274,41 +275,42 @@ int main(int argc, char *argv[])
}
}

if (fds[0].revents & LIBSSH2_POLLFD_POLLOUT) {
if(fds[0].revents & LIBSSH2_POLLFD_POLLOUT) {
act++;

if (totwritten < totsize) {
if(totwritten < totsize) {
/* we have not written all data yet */
int left = totsize - totwritten;
int size = (left < bufsize) ? left : bufsize;
int n = libssh2_channel_write_ex(channel, 0, buffer, size);

if (n == LIBSSH2_ERROR_EAGAIN) {
if(n == LIBSSH2_ERROR_EAGAIN) {
rewrites++;
fprintf(stderr, "will write again\n");
}
else if (n < 0) {
else if(n < 0) {
fprintf(stderr, "write failed\n");
exit(1);
}
else {
totwritten += n;
fprintf(stderr, "wrote %d bytes (%d in total)",
n, totwritten);
if (left >= bufsize && n != bufsize) {
if(left >= bufsize && n != bufsize) {
partials++;
fprintf(stderr, " PARTIAL");
}
fprintf(stderr, "\n");
}
} else {
}
else {
/* all data written, send EOF */
rc = libssh2_channel_send_eof(channel);

if (rc == LIBSSH2_ERROR_EAGAIN) {
if(rc == LIBSSH2_ERROR_EAGAIN) {
fprintf(stderr, "will send eof again\n");
}
else if (rc < 0) {
else if(rc < 0) {
fprintf(stderr, "send eof failed\n");
exit(1);
}
@@ -320,23 +322,23 @@ int main(int argc, char *argv[])
}
}

if (fds[0].revents & LIBSSH2_POLLFD_CHANNEL_CLOSED) {
if (!act) /* don't leave loop until we have read all data */
if(fds[0].revents & LIBSSH2_POLLFD_CHANNEL_CLOSED) {
if(!act) /* don't leave loop until we have read all data */
running = 0;
}
} while(running);

exitcode = 127;
while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN )
while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN)
waitsocket(sock, session);

if( rc == 0 ) {
exitcode = libssh2_channel_get_exit_status( channel );
if(rc == 0) {
exitcode = libssh2_channel_get_exit_status(channel);
libssh2_channel_get_exit_signal(channel, &exitsignal,
NULL, NULL, NULL, NULL, NULL);
}

if (exitsignal)
if(exitsignal)
fprintf(stderr, "\nGot signal: %s\n", exitsignal);

libssh2_channel_free(channel);
@@ -345,7 +347,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "\nrereads: %d rewrites: %d totwritten %d\n",
rereads, rewrites, totwritten);

if (totwritten != totread) {
if(totwritten != totread) {
fprintf(stderr, "\n*** FAIL bytes written: %d bytes "
"read: %d ***\n", totwritten, totread);
exit(1);
@@ -87,7 +87,7 @@ int main(int argc, char *argv[])
LIBSSH2_CHANNEL *channel;
int rc;
int exitcode;
char *exitsignal=(char *)"none";
char *exitsignal = (char *)"none";
int bytecount = 0;
size_t len;
LIBSSH2_KNOWNHOSTS *nh;
@@ -97,30 +97,30 @@ int main(int argc, char *argv[])
WSADATA wsadata;
int err;

err = WSAStartup(MAKEWORD(2,0), &wsadata);
if (err != 0) {
err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1;
}
#endif

if (argc > 1)
if(argc > 1)
/* must be ip address only */
hostname = argv[1];

if (argc > 2) {
if(argc > 2) {
username = argv[2];
}
if (argc > 3) {
if(argc > 3) {
password = argv[3];
}
if (argc > 4) {
if(argc > 4) {
commandline = argv[4];
}

rc = libssh2_init (0);
if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
rc = libssh2_init(0);
if(rc != 0) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}

@@ -135,15 +135,15 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET;
sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin),
if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
return -1;
}

/* Create a session instance */
session = libssh2_session_init();
if (!session)
if(!session)
return -1;

/* tell libssh2 we want it all done non-blocking */
@@ -152,9 +152,9 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers
*/
while ((rc = libssh2_session_handshake(session, sock)) ==
while((rc = libssh2_session_handshake(session, sock)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1;
}
@@ -206,104 +206,95 @@ int main(int argc, char *argv[])
}
libssh2_knownhost_free(nh);

if ( strlen(password) != 0 ) {
if(strlen(password) != 0) {
/* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) ==
while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
if(rc) {
fprintf(stderr, "Authentication by password failed.\n");
goto shutdown;
}
}
else {
/* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session, username,
while((rc = libssh2_userauth_publickey_fromfile(session, username,
"/home/user/"
".ssh/id_rsa.pub",
"/home/user/"
".ssh/id_rsa",
password)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown;
}
}

#if 0
libssh2_trace(session, ~0 );
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 )
{
while((channel = libssh2_channel_open_session(session)) == NULL &&
libssh2_session_last_error(session, NULL, NULL, 0) ==
LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session);
}
if( channel == NULL )
{
fprintf(stderr,"Error\n");
exit( 1 );
if(channel == NULL) {
fprintf(stderr, "Error\n");
exit(1);
}
while( (rc = libssh2_channel_exec(channel, commandline)) ==
LIBSSH2_ERROR_EAGAIN )
{
while((rc = libssh2_channel_exec(channel, commandline)) ==
LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session);
}
if( rc != 0 )
{
fprintf(stderr,"Error\n");
exit( 1 );
if(rc != 0) {
fprintf(stderr, "Error\n");
exit(1);
}
for( ;; )
{
for(;;) {
/* loop until we block */
int rc;
do
{
do {
char buffer[0x4000];
rc = libssh2_channel_read( channel, buffer, sizeof(buffer) );
if( rc > 0 )
{
rc = libssh2_channel_read(channel, buffer, sizeof(buffer) );
if(rc > 0) {
int i;
bytecount += rc;
fprintf(stderr, "We read:\n");
for( i=0; i < rc; ++i )
fputc( buffer[i], stderr);
for(i = 0; i < rc; ++i)
fputc(buffer[i], stderr);
fprintf(stderr, "\n");
}
else {
if( rc != LIBSSH2_ERROR_EAGAIN )
if(rc != LIBSSH2_ERROR_EAGAIN)
/* no need to output this for the EAGAIN case */
fprintf(stderr, "libssh2_channel_read returned %d\n", rc);
}
}
while( rc > 0 );
while(rc > 0);

/* this is due to blocking that would occur otherwise so we loop on
this condition */
if( rc == LIBSSH2_ERROR_EAGAIN )
{
if(rc == LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session);
}
else
break;
}
exitcode = 127;
while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN )
while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN)
waitsocket(sock, session);

if( rc == 0 )
{
exitcode = libssh2_channel_get_exit_status( channel );
if(rc == 0) {
exitcode = libssh2_channel_get_exit_status(channel);
libssh2_channel_get_exit_signal(channel, &exitsignal,
NULL, NULL, NULL, NULL, NULL);
}

if (exitsignal)
if(exitsignal)
fprintf(stderr, "\nGot signal: %s\n", exitsignal);
else
else
fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount);

libssh2_channel_free(channel);
@@ -57,12 +57,12 @@ static int netconf_write(LIBSSH2_CHANNEL *channel, const char *buf, size_t len)

do {
i = libssh2_channel_write(channel, buf, len);
if (i < 0) {
if(i < 0) {
fprintf(stderr, "libssh2_channel_write: %d\n", i);
return -1;
}
wr += i;
} while (i > 0 && wr < (ssize_t)len);
} while(i > 0 && wr < (ssize_t)len);

return 0;
}
@@ -78,9 +78,9 @@ static int netconf_read_until(LIBSSH2_CHANNEL *channel, const char *endtag,

do {
len = libssh2_channel_read(channel, buf + rd, buflen - rd);
if (LIBSSH2_ERROR_EAGAIN == len)
if(LIBSSH2_ERROR_EAGAIN == len)
continue;
else if (len < 0) {
else if(len < 0) {
fprintf(stderr, "libssh2_channel_read: %d\n", (int)len);
return -1;
}
@@ -92,13 +92,14 @@ static int netconf_read_until(LIBSSH2_CHANNEL *channel, const char *endtag,
/* really, this MUST be replaced with proper XML parsing! */

endreply = strstr(buf, endtag);
if (endreply)
if(endreply)
specialsequence = strstr(endreply, "]]>]]>");

} while (!specialsequence && rd < buflen);
} while(!specialsequence && rd < buflen);

if (!specialsequence) {
fprintf(stderr, "%s: ]]>]]> not found! read buffer too small?\n", __func__);
if(!specialsequence) {
fprintf(stderr, "%s: ]]>]]> not found! read buffer too small?\n",
__func__);
return -1;
}

@@ -125,49 +126,50 @@ int main(int argc, char *argv[])
WSADATA wsadata;
int err;

err = WSAStartup(MAKEWORD(2,0), &wsadata);
if (err != 0) {
err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1;
}
#else
int sock = -1;
#endif

if (argc > 1)
if(argc > 1)
server_ip = argv[1];
if (argc > 2)
if(argc > 2)
username = argv[2];
if (argc > 3)
if(argc > 3)
password = argv[3];

rc = libssh2_init (0);
if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
rc = libssh2_init(0);
if(rc != 0) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}

/* Connect to SSH server */
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
#ifdef WIN32
if (sock == INVALID_SOCKET) {
if(sock == INVALID_SOCKET) {
fprintf(stderr, "failed to open socket!\n");
return -1;
}
#else
if (sock == -1) {
if(sock == -1) {
perror("socket");
return -1;
}
#endif

sin.sin_family = AF_INET;
if (INADDR_NONE == (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) {
fprintf(stderr, "inet_addr: Invalid IP address \"%s\"\n", server_ip);
return -1;
}
sin.sin_port = htons(830);
if (connect(sock, (struct sockaddr*)(&sin),
if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr));
return -1;
@@ -203,47 +205,49 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password"))
if(strstr(userauthlist, "password"))
auth |= AUTH_PASSWORD;
if (strstr(userauthlist, "publickey"))
if(strstr(userauthlist, "publickey"))
auth |= AUTH_PUBLICKEY;

/* check for options */
if(argc > 4) {
if ((auth & AUTH_PASSWORD) && !strcasecmp(argv[4], "-p"))
if((auth & AUTH_PASSWORD) && !strcasecmp(argv[4], "-p"))
auth = AUTH_PASSWORD;
if ((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[4], "-k"))
if((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[4], "-k"))
auth = AUTH_PUBLICKEY;
}

if (auth & AUTH_PASSWORD) {
if (libssh2_userauth_password(session, username, password)) {
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, keyfile1,
}
else if(auth & AUTH_PUBLICKEY) {
if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) {
fprintf(stderr, "Authentication by public key failed!\n");
goto shutdown;
}
fprintf(stderr, "Authentication by public key succeeded.\n");
} else {
}
else {
fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown;
}

/* open a channel */
channel = libssh2_channel_open_session(session);
if (!channel) {
if(!channel) {
fprintf(stderr, "Could not open the channel!\n"
"(Note that this can be a problem at the server!"
" Please review the server logs.)\n");
goto shutdown;
}

/* execute the subsystem on our channel */
if (libssh2_channel_subsystem(channel, "netconf")) {
if(libssh2_channel_subsystem(channel, "netconf")) {
fprintf(stderr, "Could not execute the \"netconf\" subsystem!\n"
"(Note that this can be a problem at the server!"
" Please review the server logs.)\n");
@@ -261,15 +265,16 @@ int main(int argc, char *argv[])
"</capabilities>"
"</hello>\n"
"]]>]]>\n%n", (int *)&len);
if (-1 == netconf_write(channel, buf, len))
if(-1 == netconf_write(channel, buf, len))
goto shutdown;

fprintf(stderr, "Reading NETCONF server <hello>\n");
len = netconf_read_until(channel, "</hello>", buf, sizeof(buf));
if (-1 == len)
if(-1 == len)
goto shutdown;

fprintf(stderr, "Got %d bytes:\n----------------------\n%s", (int)len, buf);
fprintf(stderr, "Got %d bytes:\n----------------------\n%s",
(int)len, buf);

fprintf(stderr, "Sending NETCONF <rpc>\n");
snprintf(buf, sizeof(buf),
@@ -278,18 +283,19 @@ int main(int argc, char *argv[])
"<get-interface-information><terse/></get-interface-information>"
"</rpc>\n"
"]]>]]>\n%n", (int *)&len);
if (-1 == netconf_write(channel, buf, len))
if(-1 == netconf_write(channel, buf, len))
goto shutdown;

fprintf(stderr, "Reading NETCONF <rpc-reply>\n");
len = netconf_read_until(channel, "</rpc-reply>", buf, sizeof(buf));
if (-1 == len)
if(-1 == len)
goto shutdown;

fprintf(stderr, "Got %d bytes:\n----------------------\n%s", (int)len, buf);
fprintf(stderr, "Got %d bytes:\n----------------------\n%s",
(int)len, buf);

shutdown:
if (channel)
if(channel)
libssh2_channel_free(channel);
libssh2_session_disconnect(session, "Client disconnecting normally");
libssh2_session_free(session);
@@ -70,57 +70,58 @@ int main(int argc, char *argv[])
WSADATA wsadata;
int err;

err = WSAStartup(MAKEWORD(2,0), &wsadata);
if (err != 0) {
err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1;
}
#else
int sock = -1, forwardsock = -1;
#endif

if (argc > 1)
if(argc > 1)
server_ip = argv[1];
if (argc > 2)
if(argc > 2)
username = argv[2];
if (argc > 3)
if(argc > 3)
password = argv[3];
if (argc > 4)
if(argc > 4)
remote_listenhost = argv[4];
if (argc > 5)
if(argc > 5)
remote_wantport = atoi(argv[5]);
if (argc > 6)
if(argc > 6)
local_destip = argv[6];
if (argc > 7)
if(argc > 7)
local_destport = atoi(argv[7]);

rc = libssh2_init (0);
if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
rc = libssh2_init(0);
if(rc != 0) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}

/* Connect to SSH server */
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
#ifdef WIN32
if (sock == INVALID_SOCKET) {
if(sock == INVALID_SOCKET) {
fprintf(stderr, "failed to open socket!\n");
return -1;
}
#else
if (sock == -1) {
if(sock == -1) {
perror("socket");
return -1;
}
#endif

sin.sin_family = AF_INET;
if (INADDR_NONE == (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) {
perror("inet_addr");
return -1;
}
sin.sin_port = htons(22);
if (connect(sock, (struct sockaddr*)(&sin),
if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
return -1;
@@ -156,32 +157,34 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password"))
if(strstr(userauthlist, "password"))
auth |= AUTH_PASSWORD;
if (strstr(userauthlist, "publickey"))
if(strstr(userauthlist, "publickey"))
auth |= AUTH_PUBLICKEY;

/* check for options */
if(argc > 8) {
if ((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p"))
if((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p"))
auth = AUTH_PASSWORD;
if ((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k"))
if((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k"))
auth = AUTH_PUBLICKEY;
}

if (auth & AUTH_PASSWORD) {
if (libssh2_userauth_password(session, username, password)) {
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, keyfile1,
keyfile2, password)) {
}
else if(auth & AUTH_PUBLICKEY) {
if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown;
}
fprintf(stderr, "\tAuthentication by public key succeeded.\n");
} else {
}
else {
fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown;
}
@@ -191,7 +194,7 @@ int main(int argc, char *argv[])

listener = libssh2_channel_forward_listen_ex(session, remote_listenhost,
remote_wantport, &remote_listenport, 1);
if (!listener) {
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");
@@ -203,7 +206,7 @@ int main(int argc, char *argv[])

fprintf(stderr, "Waiting for remote connection\n");
channel = libssh2_channel_forward_accept(listener);
if (!channel) {
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");
@@ -215,24 +218,25 @@ int main(int argc, char *argv[])
local_destip, local_destport);
forwardsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
#ifdef WIN32
if (forwardsock == INVALID_SOCKET) {
if(forwardsock == INVALID_SOCKET) {
fprintf(stderr, "failed to open forward socket!\n");
goto shutdown;
}
#else
if (forwardsock == -1) {
if(forwardsock == -1) {
perror("socket");
goto shutdown;
}
#endif

sin.sin_family = AF_INET;
sin.sin_port = htons(local_destport);
if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(local_destip))) {
sin.sin_addr.s_addr = inet_addr(local_destip);
if(INADDR_NONE == sin.sin_addr.s_addr) {
perror("inet_addr");
goto shutdown;
}
if (-1 == connect(forwardsock, (struct sockaddr *)&sin, sinlen)) {
if(-1 == connect(forwardsock, (struct sockaddr *)&sin, sinlen)) {
perror("connect");
goto shutdown;
}
@@ -243,54 +247,55 @@ int main(int argc, char *argv[])
/* Must use non-blocking IO hereafter due to the current libssh2 API */
libssh2_session_set_blocking(session, 0);

while (1) {
while(1) {
FD_ZERO(&fds);
FD_SET(forwardsock, &fds);
tv.tv_sec = 0;
tv.tv_usec = 100000;
rc = select(forwardsock + 1, &fds, NULL, NULL, &tv);
if (-1 == rc) {
if(-1 == rc) {
perror("select");
goto shutdown;
}
if (rc && FD_ISSET(forwardsock, &fds)) {
if(rc && FD_ISSET(forwardsock, &fds)) {
len = recv(forwardsock, buf, sizeof(buf), 0);
if (len < 0) {
if(len < 0) {
perror("read");
goto shutdown;
} else if (0 == len) {
}
else if(0 == len) {
fprintf(stderr, "The local server at %s:%d disconnected!\n",
local_destip, local_destport);
goto shutdown;
}
wr = 0;
do {
i = libssh2_channel_write(channel, buf, len);
if (i < 0) {
if(i < 0) {
fprintf(stderr, "libssh2_channel_write: %d\n", i);
goto shutdown;
}
wr += i;
} while(i > 0 && wr < len);
}
while (1) {
while(1) {
len = libssh2_channel_read(channel, buf, sizeof(buf));
if (LIBSSH2_ERROR_EAGAIN == len)
if(LIBSSH2_ERROR_EAGAIN == len)
break;
else if (len < 0) {
else if(len < 0) {
fprintf(stderr, "libssh2_channel_read: %d", (int)len);
goto shutdown;
}
wr = 0;
while (wr < len) {
while(wr < len) {
i = send(forwardsock, buf + wr, len - wr, 0);
if (i <= 0) {
if(i <= 0) {
perror("write");
goto shutdown;
}
wr += i;
}
if (libssh2_channel_eof(channel)) {
if(libssh2_channel_eof(channel)) {
fprintf(stderr, "The remote client at %s:%d disconnected!\n",
remote_listenhost, remote_listenport);
goto shutdown;
@@ -304,9 +309,9 @@ int main(int argc, char *argv[])
#else
close(forwardsock);
#endif
if (channel)
if(channel)
libssh2_channel_free(channel);
if (listener)
if(listener)
libssh2_channel_forward_cancel(listener);
libssh2_session_disconnect(session, "Client disconnecting normally");
libssh2_session_free(session);

Large diffs are not rendered by default.

Large diffs are not rendered by default.

@@ -81,16 +81,18 @@ extern "C" {
#endif

/* Publickey Subsystem */
LIBSSH2_API LIBSSH2_PUBLICKEY *libssh2_publickey_init(LIBSSH2_SESSION *session);

LIBSSH2_API int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey,
const unsigned char *name,
unsigned long name_len,
const unsigned char *blob,
unsigned long blob_len, char overwrite,
unsigned long num_attrs,
const libssh2_publickey_attribute attrs[]);
#define libssh2_publickey_add(pkey, name, blob, blob_len, overwrite, \
LIBSSH2_API LIBSSH2_PUBLICKEY *
libssh2_publickey_init(LIBSSH2_SESSION *session);

LIBSSH2_API int
libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey,
const unsigned char *name,
unsigned long name_len,
const unsigned char *blob,
unsigned long blob_len, char overwrite,
unsigned long num_attrs,
const libssh2_publickey_attribute attrs[]);
#define libssh2_publickey_add(pkey, name, blob, blob_len, overwrite, \
num_attrs, attrs) \
libssh2_publickey_add_ex((pkey), (name), strlen(name), (blob), (blob_len), \
(overwrite), (num_attrs), (attrs))
@@ -107,8 +109,9 @@ LIBSSH2_API int
libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey,
unsigned long *num_keys,
libssh2_publickey_list **pkey_list);
LIBSSH2_API void libssh2_publickey_list_free(LIBSSH2_PUBLICKEY *pkey,
libssh2_publickey_list *pkey_list);
LIBSSH2_API void
libssh2_publickey_list_free(LIBSSH2_PUBLICKEY *pkey,
libssh2_publickey_list *pkey_list);

LIBSSH2_API int libssh2_publickey_shutdown(LIBSSH2_PUBLICKEY *pkey);

@@ -79,6 +79,9 @@ typedef struct _LIBSSH2_SFTP_STATVFS LIBSSH2_SFTP_STATVFS;
#define LIBSSH2_SFTP_READLINK 1
#define LIBSSH2_SFTP_REALPATH 2

/* Flags for sftp_mkdir() */
#define LIBSSH2_SFTP_DEFAULT_MODE -1

/* SFTP attribute flag bits */
#define LIBSSH2_SFTP_ATTR_SIZE 0x00000001
#define LIBSSH2_SFTP_ATTR_UIDGID 0x00000002
@@ -221,12 +224,13 @@ LIBSSH2_API unsigned long libssh2_sftp_last_error(LIBSSH2_SFTP *sftp);
LIBSSH2_API LIBSSH2_CHANNEL *libssh2_sftp_get_channel(LIBSSH2_SFTP *sftp);

/* File / Directory Ops */
LIBSSH2_API LIBSSH2_SFTP_HANDLE *libssh2_sftp_open_ex(LIBSSH2_SFTP *sftp,
const char *filename,
unsigned int filename_len,
unsigned long flags,
long mode, int open_type);
#define libssh2_sftp_open(sftp, filename, flags, mode) \
LIBSSH2_API LIBSSH2_SFTP_HANDLE *
libssh2_sftp_open_ex(LIBSSH2_SFTP *sftp,
const char *filename,
unsigned int filename_len,
unsigned long flags,
long mode, int open_type);
#define libssh2_sftp_open(sftp, filename, flags, mode) \
libssh2_sftp_open_ex((sftp), (filename), strlen(filename), (flags), \
(mode), LIBSSH2_SFTP_OPENFILE)
#define libssh2_sftp_opendir(sftp, path) \
@@ -328,7 +332,8 @@ LIBSSH2_API int libssh2_sftp_symlink_ex(LIBSSH2_SFTP *sftp,
const char *path,
unsigned int path_len,
char *target,
unsigned int target_len, int link_type);
unsigned int target_len,
int link_type);
#define libssh2_sftp_symlink(sftp, orig, linkpath) \
libssh2_sftp_symlink_ex((sftp), (orig), strlen(orig), (linkpath), \
strlen(linkpath), LIBSSH2_SFTP_SYMLINK)
@@ -49,7 +49,7 @@ setenv TGTCCSID '500' # Target CCSID of objects.
setenv DEBUG '*ALL' # Debug level.
setenv OPTIMIZE '10' # Optimisation level
setenv OUTPUT '*NONE' # Compilation output option.
setenv TGTRLS 'V5R3M0' # Target OS release.
setenv TGTRLS 'V6R1M0' # Target OS release.
setenv IFSDIR '/libssh2' # Installation IFS directory.

# Define ZLIB availability and locations.
@@ -180,7 +180,7 @@ make_module()
CMD="CRTCMOD MODULE(${TARGETLIB}/${1}) SRCSTMF('__tmpsrcf.c')"
# CMD="${CMD} SYSIFCOPT(*IFS64IO) OPTION(*INCDIRFIRST *SHOWINC *SHOWSYS)"
CMD="${CMD} SYSIFCOPT(*IFS64IO) OPTION(*INCDIRFIRST)"
CMD="${CMD} LOCALETYPE(*LOCALE)"
CMD="${CMD} LOCALETYPE(*LOCALE) FLAG(10)"
CMD="${CMD} INCDIR('${TOPDIR}/os400/include'"
CMD="${CMD} '/QIBM/ProdData/qadrt/include' '${TOPDIR}/include'"
CMD="${CMD} '${TOPDIR}/os400' '${SRCDIR}'"
@@ -77,21 +77,21 @@ if(CRYPTO_BACKEND STREQUAL "OpenSSL" OR NOT CRYPTO_BACKEND)
list(APPEND PC_LIBS -lcrypt32)

find_file(DLL_LIBEAY32
NAMES libeay32.dll crypto.dll
NAMES libeay32.dll crypto.dll libcrypto-1_1.dll libcrypto-1_1-x64.dll
HINTS ${_OPENSSL_ROOT_HINTS} PATHS ${_OPENSSL_ROOT_PATHS}
PATH_SUFFIXES bin)
if (NOT DLL_LIBEAY32)
message(WARNING
"Unable to find OpenSSL libeay32 DLL, executables may not run")
"Unable to find OpenSSL crypto (aka libeay32) DLL, executables may not run")
endif()

find_file(DLL_SSLEAY32
NAMES ssleay32.dll ssl.dll
NAMES ssleay32.dll ssl.dll libssl-1_1.dll libssl-1_1-x64.dll
HINTS ${_OPENSSL_ROOT_HINTS} PATHS ${_OPENSSL_ROOT_PATHS}
PATH_SUFFIXES bin)
if (NOT DLL_SSLEAY32)
message(WARNING
"Unable to find OpenSSL ssleay32 DLL, executables may not run")
"Unable to find OpenSSL ssl (aka ssleay32) DLL, executables may not run")
endif()

if(DLL_LIBEAY32 AND DLL_SSLEAY32)
@@ -176,6 +176,9 @@ include(GNUInstallDirs)
set(SOURCES
${CRYPTO_SOURCES}
agent.c
blf.h
bcrypt_pbkdf.c
blowfish.c
channel.c
channel.h
comp.c
@@ -217,7 +220,7 @@ set_target_properties(libssh2 PROPERTIES PREFIX "")

target_compile_definitions(libssh2 PRIVATE ${PRIVATE_COMPILE_DEFINITIONS})
target_include_directories(libssh2
PRIVATE ${PRIVATE_INCLUDE_DIRECTORIES}
PRIVATE "${PROJECT_SOURCE_DIR}/include/" ${PRIVATE_INCLUDE_DIRECTORIES}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>)
@@ -312,6 +315,7 @@ if (NOT HAVE_STRTOLL)
check_symbol_exists(_strtoi64 stdlib.h HAVE_STRTOI64)
endif()
check_symbol_exists(snprintf stdio.h HAVE_SNPRINTF)
check_symbol_exists(memset_s string.h HAVE_MEMSET_S)

if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" OR
${CMAKE_SYSTEM_NAME} STREQUAL "Interix")
@@ -322,7 +326,7 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" OR
# filesystem here"
#
# Mac OS X's poll has funny behaviors, like:
# not being able to do poll on no fildescriptors (10.3?)
# not being able to do poll on no filedescriptors (10.3?)
# not being able to poll on some files (like anything in /dev)
# not having reliable timeout support
# inconsistent return of POLLHUP where other implementations give POLLIN
@@ -333,7 +337,7 @@ endif()

append_needed_socket_libraries(LIBRARIES)

# Non-blocking socket support tests. Must be after after library tests to
# Non-blocking socket support tests. Must be after library tests to
# link correctly
set(SAVE_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
set(CMAKE_REQUIRED_LIBRARIES ${LIBRARIES})
@@ -1,7 +1,7 @@
# $Id: Makefile.am,v 1.21 2009/05/07 17:21:56 bagder Exp $
AUTOMAKE_OPTIONS = foreign nostdinc

# Get the CRYPTO_CSOURCES and CRYPTO_HHEADERS defines
# Get the CRYPTO_CSOURCES, CRYPTO_HHEADERS and CRYPTO_LTLIBS defines
if OPENSSL
include ../Makefile.OpenSSL.inc
endif
@@ -11,9 +11,6 @@ endif
if WINCNG
include ../Makefile.WinCNG.inc
endif
if OS400QC3
include ../Makefile.os400qc3.inc
endif
if MBEDTLS
include ../Makefile.mbedTLS.inc
endif
@@ -65,4 +62,4 @@ VERSION=-version-info 1:1:0

libssh2_la_LDFLAGS = $(VERSION) -no-undefined \
-export-symbols-regex '^libssh2_.*' \
$(LTLIBGCRYPT) $(LTLIBSSL) $(LTLIBZ)
$(CRYPTO_LTLIBS) $(LTLIBZ)

Large diffs are not rendered by default.

@@ -0,0 +1,180 @@
/* $OpenBSD: bcrypt_pbkdf.c,v 1.4 2013/07/29 00:55:53 tedu Exp $ */
/*
* Copyright (c) 2013 Ted Unangst <tedu@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


#ifndef HAVE_BCRYPT_PBKDF

#include "libssh2_priv.h"
#include <stdlib.h>
#include <sys/types.h>
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

#include "blf.h"

#define MINIMUM(a,b) (((a) < (b)) ? (a) : (b))

/*
* pkcs #5 pbkdf2 implementation using the "bcrypt" hash
*
* The bcrypt hash function is derived from the bcrypt password hashing
* function with the following modifications:
* 1. The input password and salt are preprocessed with SHA512.
* 2. The output length is expanded to 256 bits.
* 3. Subsequently the magic string to be encrypted is lengthened and modifed
* to "OxychromaticBlowfishSwatDynamite"
* 4. The hash function is defined to perform 64 rounds of initial state
* expansion. (More rounds are performed by iterating the hash.)
*
* Note that this implementation pulls the SHA512 operations into the caller
* as a performance optimization.
*
* One modification from official pbkdf2. Instead of outputting key material
* linearly, we mix it. pbkdf2 has a known weakness where if one uses it to
* generate (i.e.) 512 bits of key material for use as two 256 bit keys, an
* attacker can merely run once through the outer loop below, but the user
* always runs it twice. Shuffling output bytes requires computing the
* entirety of the key material to assemble any subkey. This is something a
* wise caller could do; we just do it for you.
*/

#define BCRYPT_BLOCKS 8
#define BCRYPT_HASHSIZE (BCRYPT_BLOCKS * 4)

static void
bcrypt_hash(uint8_t *sha2pass, uint8_t *sha2salt, uint8_t *out)
{
blf_ctx state;
uint8_t ciphertext[BCRYPT_HASHSIZE] =
"OxychromaticBlowfishSwatDynamite";
uint32_t cdata[BCRYPT_BLOCKS];
int i;
uint16_t j;
size_t shalen = SHA512_DIGEST_LENGTH;

/* key expansion */
Blowfish_initstate(&state);
Blowfish_expandstate(&state, sha2salt, shalen, sha2pass, shalen);
for(i = 0; i < 64; i++) {
Blowfish_expand0state(&state, sha2salt, shalen);
Blowfish_expand0state(&state, sha2pass, shalen);
}

/* encryption */
j = 0;
for(i = 0; i < BCRYPT_BLOCKS; i++)
cdata[i] = Blowfish_stream2word(ciphertext, sizeof(ciphertext),
&j);
for(i = 0; i < 64; i++)
blf_enc(&state, cdata, sizeof(cdata) / sizeof(uint64_t));

/* copy out */
for(i = 0; i < BCRYPT_BLOCKS; i++) {
out[4 * i + 3] = (cdata[i] >> 24) & 0xff;
out[4 * i + 2] = (cdata[i] >> 16) & 0xff;
out[4 * i + 1] = (cdata[i] >> 8) & 0xff;
out[4 * i + 0] = cdata[i] & 0xff;
}

/* zap */
_libssh2_explicit_zero(ciphertext, sizeof(ciphertext));
_libssh2_explicit_zero(cdata, sizeof(cdata));
_libssh2_explicit_zero(&state, sizeof(state));
}

int
bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt,
size_t saltlen,
uint8_t *key, size_t keylen, unsigned int rounds)
{
uint8_t sha2pass[SHA512_DIGEST_LENGTH];
uint8_t sha2salt[SHA512_DIGEST_LENGTH];
uint8_t out[BCRYPT_HASHSIZE];
uint8_t tmpout[BCRYPT_HASHSIZE];
uint8_t *countsalt;
size_t i, j, amt, stride;
uint32_t count;
size_t origkeylen = keylen;
libssh2_sha512_ctx ctx;

/* nothing crazy */
if(rounds < 1)
return -1;
if(passlen == 0 || saltlen == 0 || keylen == 0 ||
keylen > sizeof(out) * sizeof(out) || saltlen > 1<<20)
return -1;
countsalt = calloc(1, saltlen + 4);
if(countsalt == NULL)
return -1;
stride = (keylen + sizeof(out) - 1) / sizeof(out);
amt = (keylen + stride - 1) / stride;

memcpy(countsalt, salt, saltlen);

/* collapse password */
libssh2_sha512_init(&ctx);
libssh2_sha512_update(ctx, pass, passlen);
libssh2_sha512_final(ctx, sha2pass);

/* generate key, sizeof(out) at a time */
for(count = 1; keylen > 0; count++) {
countsalt[saltlen + 0] = (count >> 24) & 0xff;
countsalt[saltlen + 1] = (count >> 16) & 0xff;
countsalt[saltlen + 2] = (count >> 8) & 0xff;
countsalt[saltlen + 3] = count & 0xff;

/* first round, salt is salt */
libssh2_sha512_init(&ctx);
libssh2_sha512_update(ctx, countsalt, saltlen + 4);
libssh2_sha512_final(ctx, sha2salt);

bcrypt_hash(sha2pass, sha2salt, tmpout);
memcpy(out, tmpout, sizeof(out));

for(i = 1; i < rounds; i++) {
/* subsequent rounds, salt is previous output */
libssh2_sha512_init(&ctx);
libssh2_sha512_update(ctx, tmpout, sizeof(tmpout));
libssh2_sha512_final(ctx, sha2salt);

bcrypt_hash(sha2pass, sha2salt, tmpout);
for(j = 0; j < sizeof(out); j++)
out[j] ^= tmpout[j];
}

/*
* pbkdf2 deviation: ouput the key material non-linearly.
*/
amt = MINIMUM(amt, keylen);
for(i = 0; i < amt; i++) {
size_t dest = i * stride + (count - 1);
if(dest >= origkeylen) {
break;
}
key[dest] = out[i];
}
keylen -= i;
}

/* zap */
_libssh2_explicit_zero(out, sizeof(out));
free(countsalt);

return 0;
}
#endif /* HAVE_BCRYPT_PBKDF */
@@ -0,0 +1,90 @@
/* $OpenBSD: blf.h,v 1.7 2007/03/14 17:59:41 grunk Exp $ */
/*
* Blowfish - a fast block cipher designed by Bruce Schneier
*
* Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Niels Provos.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _BLF_H_
#define _BLF_H_

#if !defined(HAVE_BCRYPT_PBKDF) && !defined(HAVE_BLH_H)

/* Schneier specifies a maximum key length of 56 bytes.
* This ensures that every key bit affects every cipher
* bit. However, the subkeys can hold up to 72 bytes.
* Warning: For normal blowfish encryption only 56 bytes
* of the key affect all cipherbits.
*/

#define BLF_N 16 /* Number of Subkeys */
#define BLF_MAXKEYLEN ((BLF_N-2)*4) /* 448 bits */
#define BLF_MAXUTILIZED ((BLF_N + 2)*4) /* 576 bits */

/* Blowfish context */
typedef struct BlowfishContext {
uint32_t S[4][256]; /* S-Boxes */
uint32_t P[BLF_N + 2]; /* Subkeys */
} blf_ctx;

/* Raw access to customized Blowfish
* blf_key is just:
* Blowfish_initstate( state )
* Blowfish_expand0state( state, key, keylen )
*/

void Blowfish_encipher(blf_ctx *, uint32_t *, uint32_t *);
void Blowfish_decipher(blf_ctx *, uint32_t *, uint32_t *);
void Blowfish_initstate(blf_ctx *);
void Blowfish_expand0state(blf_ctx *, const uint8_t *, uint16_t);
void Blowfish_expandstate
(blf_ctx *, const uint8_t *, uint16_t, const uint8_t *, uint16_t);

/* Standard Blowfish */

void blf_key(blf_ctx *, const uint8_t *, uint16_t);
void blf_enc(blf_ctx *, uint32_t *, uint16_t);
void blf_dec(blf_ctx *, uint32_t *, uint16_t);

void blf_ecb_encrypt(blf_ctx *, uint8_t *, uint32_t);
void blf_ecb_decrypt(blf_ctx *, uint8_t *, uint32_t);

void blf_cbc_encrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t);
void blf_cbc_decrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t);

/* Converts uint8_t to uint32_t */
uint32_t Blowfish_stream2word(const uint8_t *, uint16_t, uint16_t *);

/* bcrypt with pbkd */
int bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt,
size_t saltlen,
uint8_t *key, size_t keylen, unsigned int rounds);

#endif /* !defined(HAVE_BCRYPT_PBKDF) && !defined(HAVE_BLH_H) */
#endif /* _BLF_H */