13
#include "libssh2_setup.h"
15
#include <libssh2_sftp.h>
17
#ifdef HAVE_SYS_SOCKET_H
18
#include <sys/socket.h>
23
#ifdef HAVE_NETINET_IN_H
24
#include <netinet/in.h>
26
#ifdef HAVE_ARPA_INET_H
33
#define LIBSSH2_FILESIZE_MASK "I64u"
35
#define LIBSSH2_FILESIZE_MASK "llu"
38
static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
39
static const char *privkey = "/home/username/.ssh/id_rsa";
40
static const char *username = "username";
41
static const char *password = "password";
42
static const char *sftppath = "/tmp/secretdir";
44
int main(int argc, char *argv[])
47
libssh2_socket_t sock;
49
struct sockaddr_in sin;
50
const char *fingerprint;
52
LIBSSH2_SESSION *session = NULL;
53
LIBSSH2_SFTP *sftp_session;
54
LIBSSH2_SFTP_HANDLE *sftp_handle;
59
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
61
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
67
hostaddr = inet_addr(argv[1]);
70
hostaddr = htonl(0x7F000001);
84
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
92
sock = socket(AF_INET, SOCK_STREAM, 0);
93
if(sock == LIBSSH2_INVALID_SOCKET) {
94
fprintf(stderr, "failed to create socket.\n");
98
sin.sin_family = AF_INET;
99
sin.sin_port = htons(22);
100
sin.sin_addr.s_addr = hostaddr;
101
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
102
fprintf(stderr, "failed to connect.\n");
107
session = libssh2_session_init();
109
fprintf(stderr, "Could not initialize SSH session.\n");
114
libssh2_session_set_blocking(session, 0);
119
while((rc = libssh2_session_handshake(session, sock)) ==
120
LIBSSH2_ERROR_EAGAIN);
122
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
131
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
132
fprintf(stderr, "Fingerprint: ");
133
for(i = 0; i < 20; i++) {
134
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
136
fprintf(stderr, "\n");
140
while((rc = libssh2_userauth_password(session, username, password)) ==
141
LIBSSH2_ERROR_EAGAIN);
143
fprintf(stderr, "Authentication by password failed.\n");
149
while((rc = libssh2_userauth_publickey_fromfile(session, username,
152
LIBSSH2_ERROR_EAGAIN);
154
fprintf(stderr, "Authentication by public key failed.\n");
159
fprintf(stderr, "libssh2_sftp_init().\n");
161
sftp_session = libssh2_sftp_init(session);
164
libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
165
fprintf(stderr, "Unable to init SFTP session\n");
168
} while(!sftp_session);
170
fprintf(stderr, "libssh2_sftp_opendir().\n");
173
sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);
176
libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
177
fprintf(stderr, "Unable to open dir with SFTP\n");
180
} while(!sftp_handle);
182
fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing.\n");
185
LIBSSH2_SFTP_ATTRIBUTES attrs;
188
while((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem),
189
&attrs)) == LIBSSH2_ERROR_EAGAIN);
194
if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
197
printf("--fix----- ");
200
printf("---------- ");
203
if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
204
printf("%4d %4d ", (int) attrs.uid, (int) attrs.gid);
210
if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
211
printf("%8" LIBSSH2_FILESIZE_MASK " ", attrs.filesize);
216
else if(rc == LIBSSH2_ERROR_EAGAIN) {
218
fprintf(stderr, "Blocking\n");
226
libssh2_sftp_closedir(sftp_handle);
227
libssh2_sftp_shutdown(sftp_session);
232
libssh2_session_disconnect(session, "Normal Shutdown");
233
libssh2_session_free(session);
236
if(sock != LIBSSH2_INVALID_SOCKET) {
238
LIBSSH2_SOCKET_CLOSE(sock);
241
fprintf(stderr, "all done\n");