1
/* Copyright (C) The libssh2 project and its contributors.
3
* Sample showing how to do SFTP transfers in a non-blocking manner.
5
* It will first download a given source file, store it locally and then
6
* upload the file again to a given destination file.
8
* Using the SFTP server running on 127.0.0.1
10
* SPDX-License-Identifier: BSD-3-Clause
13
#include "libssh2_setup.h"
15
#include <libssh2_sftp.h>
18
#define write(f, b, c) write((f), (b), (unsigned int)(c))
21
#ifdef HAVE_SYS_SOCKET_H
22
#include <sys/socket.h>
27
#ifdef HAVE_NETINET_IN_H
28
#include <netinet/in.h>
36
static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
37
static const char *privkey = "/home/username/.ssh/id_rsa";
38
static const char *username = "username";
39
static const char *password = "password";
40
static const char *sftppath = "/tmp/TEST"; /* source path */
41
static const char *dest = "/tmp/TEST2"; /* destination path */
42
static const char *storage = "/tmp/sftp-storage"; /* local file name to store
43
the downloaded file in */
45
static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
47
struct timeval timeout;
50
fd_set *writefd = NULL;
51
fd_set *readfd = NULL;
60
#pragma GCC diagnostic push
61
#pragma GCC diagnostic ignored "-Wsign-conversion"
63
FD_SET(socket_fd, &fd);
65
#pragma GCC diagnostic pop
68
/* now make sure we wait in the correct direction */
69
dir = libssh2_session_block_directions(session);
71
if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
74
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
77
rc = select((int)(socket_fd + 1), readfd, writefd, NULL, &timeout);
82
int main(int argc, char *argv[])
84
libssh2_socket_t sock;
86
struct sockaddr_in sin;
87
const char *fingerprint;
89
LIBSSH2_SESSION *session = NULL;
90
LIBSSH2_SFTP *sftp_session;
91
LIBSSH2_SFTP_HANDLE *sftp_handle;
92
FILE *tempstorage = NULL;
94
struct timeval timeout;
101
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
103
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
121
rc = libssh2_init(0);
123
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
127
/* Ultra basic "connect to port 22 on localhost". Your code is
128
* responsible for creating the socket establishing the connection
130
sock = socket(AF_INET, SOCK_STREAM, 0);
131
if(sock == LIBSSH2_INVALID_SOCKET) {
132
fprintf(stderr, "failed to create socket.\n");
136
sin.sin_family = AF_INET;
137
sin.sin_port = htons(22);
138
sin.sin_addr.s_addr = htonl(0x7F000001);
139
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
140
fprintf(stderr, "failed to connect.\n");
144
/* Create a session instance */
145
session = libssh2_session_init();
147
fprintf(stderr, "Could not initialize SSH session.\n");
151
/* ... start it up. This will trade welcome banners, exchange keys,
152
* and setup crypto, compression, and MAC layers
154
rc = libssh2_session_handshake(session, sock);
156
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
160
libssh2_session_set_blocking(session, 0);
162
/* At this point we have not yet authenticated. The first thing to do
163
* is check the hostkey's fingerprint against our known hosts Your app
164
* may have it hard coded, may go to a file, may present it to the
165
* user, that's your call
167
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
168
fprintf(stderr, "Fingerprint: ");
169
for(i = 0; i < 20; i++) {
170
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
172
fprintf(stderr, "\n");
174
tempstorage = fopen(storage, "wb");
176
fprintf(stderr, "Cannot open temp storage file %s\n", storage);
181
/* We could authenticate via password */
182
while((rc = libssh2_userauth_password(session, username, password)) ==
183
LIBSSH2_ERROR_EAGAIN);
185
fprintf(stderr, "Authentication by password failed.\n");
190
/* Or by public key */
192
libssh2_userauth_publickey_fromfile(session, username,
195
LIBSSH2_ERROR_EAGAIN);
197
fprintf(stderr, "Authentication by public key failed.\n");
203
sftp_session = libssh2_sftp_init(session);
206
if(libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) {
207
fprintf(stderr, "non-blocking init\n");
208
waitsocket(sock, session); /* now we wait */
211
fprintf(stderr, "Unable to init SFTP session\n");
215
} while(!sftp_session);
217
/* Request a file via SFTP */
219
sftp_handle = libssh2_sftp_open(sftp_session, sftppath,
220
LIBSSH2_FXF_READ, 0);
222
if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
223
fprintf(stderr, "Unable to open file with SFTP: %ld\n",
224
libssh2_sftp_last_error(sftp_session));
228
fprintf(stderr, "non-blocking open\n");
229
waitsocket(sock, session); /* now we wait */
232
} while(!sftp_handle);
234
fprintf(stderr, "libssh2_sftp_open() is done, now receive data.\n");
238
/* read in a loop until we block */
239
nread = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
240
fprintf(stderr, "libssh2_sftp_read returned %ld\n",
244
/* write to stderr */
245
write(2, mem, (size_t)nread);
246
/* write to temporary storage area */
247
fwrite(mem, (size_t)nread, 1, tempstorage);
251
if(nread != LIBSSH2_ERROR_EAGAIN) {
252
/* error or end of file */
262
#pragma GCC diagnostic push
263
#pragma GCC diagnostic ignored "-Wsign-conversion"
268
#pragma GCC diagnostic pop
271
/* wait for readable or writeable */
272
rc = select((int)(sock + 1), &fd, &fd2, NULL, &timeout);
276
fprintf(stderr, "SFTP download timed out: %d\n", rc);
282
libssh2_sftp_close(sftp_handle);
285
tempstorage = fopen(storage, "rb");
287
/* weird, we cannot read the file we just wrote to... */
288
fprintf(stderr, "Cannot open %s for reading\n", storage);
292
/* we're done downloading, now reverse the process and upload the
293
temporarily stored data to the destination path */
294
sftp_handle = libssh2_sftp_open(sftp_session, dest,
297
LIBSSH2_SFTP_S_IRUSR |
298
LIBSSH2_SFTP_S_IWUSR |
299
LIBSSH2_SFTP_S_IRGRP |
300
LIBSSH2_SFTP_S_IROTH);
306
nread = fread(mem, 1, sizeof(mem), tempstorage);
314
/* write data in a loop until we block */
315
nwritten = libssh2_sftp_write(sftp_handle, ptr,
320
nread -= (size_t)nwritten;
321
} while(nwritten >= 0);
323
if(nwritten != LIBSSH2_ERROR_EAGAIN) {
324
/* error or end of file */
334
#pragma GCC diagnostic push
335
#pragma GCC diagnostic ignored "-Wsign-conversion"
340
#pragma GCC diagnostic pop
343
/* wait for readable or writeable */
344
rc = select((int)(sock + 1), &fd, &fd2, NULL, &timeout);
348
fprintf(stderr, "SFTP upload timed out: %d\n", rc);
352
fprintf(stderr, "SFTP upload done.\n");
355
fprintf(stderr, "SFTP failed to open destination path: %s\n",
359
libssh2_sftp_shutdown(sftp_session);
364
libssh2_session_disconnect(session, "Normal Shutdown");
365
libssh2_session_free(session);
368
if(sock != LIBSSH2_INVALID_SOCKET) {
370
LIBSSH2_SOCKET_CLOSE(sock);
376
fprintf(stderr, "all done\n");