2
* $Id: sftpdir.c,v 1.11 2009/04/28 10:35:30 bagder Exp $
4
* Sample doing an SFTP directory listing.
6
* The sample code has default values for host name, user name, password and
7
* path, but you can specify them on the command line like:
9
* "sftpdir 192.168.0.1 user password /tmp/secretdir"
12
#include "libssh2_config.h"
14
#include <libssh2_sftp.h>
16
#ifdef HAVE_WINSOCK2_H
17
# include <winsock2.h>
19
#ifdef HAVE_SYS_SOCKET_H
20
# include <sys/socket.h>
22
#ifdef HAVE_NETINET_IN_H
23
# include <netinet/in.h>
28
#ifdef HAVE_ARPA_INET_H
29
# include <arpa/inet.h>
32
#include <sys/types.h>
38
int main(int argc, char *argv[])
40
unsigned long hostaddr;
41
int sock, i, auth_pw = 1;
42
struct sockaddr_in sin;
43
const char *fingerprint;
44
LIBSSH2_SESSION *session;
45
const char *username="username";
46
const char *password="password";
47
const char *sftppath="/tmp/secretdir";
49
LIBSSH2_SFTP *sftp_session;
50
LIBSSH2_SFTP_HANDLE *sftp_handle;
55
WSAStartup(MAKEWORD(2,0), &wsadata);
59
hostaddr = inet_addr(argv[1]);
61
hostaddr = htonl(0x7F000001);
74
* The application code is responsible for creating the socket
75
* and establishing the connection
77
sock = socket(AF_INET, SOCK_STREAM, 0);
79
sin.sin_family = AF_INET;
80
sin.sin_port = htons(22);
81
sin.sin_addr.s_addr = hostaddr;
82
if (connect(sock, (struct sockaddr*)(&sin),
83
sizeof(struct sockaddr_in)) != 0) {
84
fprintf(stderr, "failed to connect!\n");
88
/* Create a session instance
90
session = libssh2_session_init();
94
/* ... start it up. This will trade welcome banners, exchange keys,
95
* and setup crypto, compression, and MAC layers
97
rc = libssh2_session_startup(session, sock);
99
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
103
/* At this point we havn't yet authenticated. The first thing to do
104
* is check the hostkey's fingerprint against our known hosts Your app
105
* may have it hard coded, may go to a file, may present it to the
106
* user, that's your call
108
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
109
printf("Fingerprint: ");
110
for(i = 0; i < 16; i++) {
111
printf("%02X ", (unsigned char)fingerprint[i]);
116
/* We could authenticate via password */
117
if ((i = libssh2_userauth_password(session, username, password))) {
118
printf("Authentication by password failed.\n");
122
/* Or by public key */
123
if (libssh2_userauth_publickey_fromfile(session, username,
124
"/home/username/.ssh/id_rsa.pub",
125
"/home/username/.ssh/id_rsa",
127
printf("\tAuthentication by public key failed\n");
132
fprintf(stderr, "libssh2_sftp_init()!\n");
133
sftp_session = libssh2_sftp_init(session);
136
fprintf(stderr, "Unable to init SFTP session\n");
140
/* Since we have not set non-blocking, tell libssh2 we are blocking */
141
libssh2_session_set_blocking(session, 1);
143
fprintf(stderr, "libssh2_sftp_opendir()!\n");
144
/* Request a dir listing via SFTP */
145
sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);
148
fprintf(stderr, "Unable to open dir with SFTP\n");
151
fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n");
155
LIBSSH2_SFTP_ATTRIBUTES attrs;
157
/* loop until we fail */
158
rc = libssh2_sftp_readdir_ex(sftp_handle, mem, sizeof(mem),
159
longentry, sizeof(longentry), &attrs);
161
/* rc is the length of the file name in the mem
164
if (longentry[0] != '\0') {
165
printf("%s\n", longentry);
167
if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
168
/* this should check what permissions it
169
is and print the output accordingly */
170
printf("--fix----- ");
173
printf("---------- ");
176
if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
177
printf("%4ld %4ld ", attrs.uid, attrs.gid);
183
if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
184
/* attrs.filesize is an uint64_t according to
185
the docs but there is no really good and
186
portable 64bit type for C before C99, and
187
correspondingly there was no good printf()
190
printf("%8lld ", attrs.filesize);
201
libssh2_sftp_closedir(sftp_handle);
202
libssh2_sftp_shutdown(sftp_session);
206
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
207
libssh2_session_free(session);
214
printf("all done\n");