1
/* Copyright (C) The libssh2 project and its contributors.
3
* Sample doing an SFTP directory listing.
5
* The sample code has default values for host name, user name, password and
6
* path, but you can specify them on the command line like:
8
* $ ./sftpdir 192.168.0.1 user password /tmp/secretdir
10
* SPDX-License-Identifier: BSD-3-Clause
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
34
#define LIBSSH2_FILESIZE_MASK "I64u"
36
#define LIBSSH2_FILESIZE_MASK "llu"
39
static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
40
static const char *privkey = "/home/username/.ssh/id_rsa";
41
static const char *username = "username";
42
static const char *password = "password";
43
static const char *sftppath = "/tmp/secretdir";
45
static void kbd_callback(const char *name, int name_len,
46
const char *instruction, int instruction_len,
48
const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
49
LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
55
(void)instruction_len;
56
if(num_prompts == 1) {
57
responses[0].text = strdup(password);
58
responses[0].length = (unsigned int)strlen(password);
64
int main(int argc, char *argv[])
67
libssh2_socket_t sock;
69
struct sockaddr_in sin;
70
const char *fingerprint;
73
LIBSSH2_SESSION *session = NULL;
74
LIBSSH2_SFTP *sftp_session;
75
LIBSSH2_SFTP_HANDLE *sftp_handle;
80
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
82
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
88
hostaddr = inet_addr(argv[1]);
91
hostaddr = htonl(0x7F000001);
103
rc = libssh2_init(0);
105
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
110
* The application code is responsible for creating the socket
111
* and establishing the connection
113
sock = socket(AF_INET, SOCK_STREAM, 0);
114
if(sock == LIBSSH2_INVALID_SOCKET) {
115
fprintf(stderr, "failed to create socket.\n");
119
sin.sin_family = AF_INET;
120
sin.sin_port = htons(22);
121
sin.sin_addr.s_addr = hostaddr;
122
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
123
fprintf(stderr, "failed to connect.\n");
127
/* Create a session instance */
128
session = libssh2_session_init();
130
fprintf(stderr, "Could not initialize SSH session.\n");
134
/* ... start it up. This will trade welcome banners, exchange keys,
135
* and setup crypto, compression, and MAC layers
137
rc = libssh2_session_handshake(session, sock);
139
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
143
/* At this point we have not yet authenticated. The first thing to do
144
* is check the hostkey's fingerprint against our known hosts Your app
145
* may have it hard coded, may go to a file, may present it to the
146
* user, that's your call
148
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
149
fprintf(stderr, "Fingerprint: ");
150
for(i = 0; i < 20; i++) {
151
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
153
fprintf(stderr, "\n");
155
/* check what authentication methods are available */
156
userauthlist = libssh2_userauth_list(session, username,
157
(unsigned int)strlen(username));
159
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
160
if(strstr(userauthlist, "password")) {
163
if(strstr(userauthlist, "keyboard-interactive")) {
166
if(strstr(userauthlist, "publickey")) {
170
/* check for options */
172
if((auth_pw & 1) && !strcmp(argv[5], "-p")) {
175
if((auth_pw & 2) && !strcmp(argv[5], "-i")) {
178
if((auth_pw & 4) && !strcmp(argv[5], "-k")) {
184
/* We could authenticate via password */
185
if(libssh2_userauth_password(session, username, password)) {
186
fprintf(stderr, "Authentication by password failed.\n");
190
else if(auth_pw & 2) {
191
/* Or via keyboard-interactive */
192
if(libssh2_userauth_keyboard_interactive(session, username,
195
"Authentication by keyboard-interactive failed.\n");
200
"Authentication by keyboard-interactive succeeded.\n");
203
else if(auth_pw & 4) {
204
/* Or by public key */
205
if(libssh2_userauth_publickey_fromfile(session, username,
208
fprintf(stderr, "Authentication by public key failed.\n");
212
fprintf(stderr, "Authentication by public key succeeded.\n");
216
fprintf(stderr, "No supported authentication methods found.\n");
221
fprintf(stderr, "libssh2_sftp_init().\n");
222
sftp_session = libssh2_sftp_init(session);
225
fprintf(stderr, "Unable to init SFTP session\n");
229
/* Since we have not set non-blocking, tell libssh2 we are blocking */
230
libssh2_session_set_blocking(session, 1);
232
fprintf(stderr, "libssh2_sftp_opendir().\n");
233
/* Request a dir listing via SFTP */
234
sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);
236
fprintf(stderr, "Unable to open dir with SFTP\n");
240
fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing.\n");
244
LIBSSH2_SFTP_ATTRIBUTES attrs;
246
/* loop until we fail */
247
rc = libssh2_sftp_readdir_ex(sftp_handle, mem, sizeof(mem),
248
longentry, sizeof(longentry), &attrs);
250
/* rc is the length of the file name in the mem
253
if(longentry[0] != '\0') {
254
printf("%s\n", longentry);
257
if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
258
/* this should check what permissions it
259
is and print the output accordingly */
260
printf("--fix----- ");
263
printf("---------- ");
266
if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
267
printf("%4d %4d ", (int) attrs.uid, (int) attrs.gid);
273
if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
274
printf("%8" LIBSSH2_FILESIZE_MASK " ", attrs.filesize);
286
libssh2_sftp_closedir(sftp_handle);
287
libssh2_sftp_shutdown(sftp_session);
292
libssh2_session_disconnect(session, "Normal Shutdown");
293
libssh2_session_free(session);
296
if(sock != LIBSSH2_INVALID_SOCKET) {
298
LIBSSH2_SOCKET_CLOSE(sock);
301
fprintf(stderr, "all done\n");