2007-01-30 12:07:14 +01:00
|
|
|
/*
|
2009-04-28 12:35:30 +02:00
|
|
|
* $Id: scp.c,v 1.12 2009/04/28 10:35:30 bagder Exp $
|
2007-01-30 12:07:14 +01:00
|
|
|
*
|
|
|
|
* Sample showing how to do a simple SCP transfer.
|
|
|
|
*/
|
|
|
|
|
2008-11-10 17:48:41 +01:00
|
|
|
#include "libssh2_config.h"
|
2007-01-30 12:07:14 +01:00
|
|
|
#include <libssh2.h>
|
|
|
|
|
2007-04-27 00:59:29 +02:00
|
|
|
#ifdef HAVE_WINSOCK2_H
|
|
|
|
# include <winsock2.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2007-01-30 12:07:14 +01:00
|
|
|
# include <sys/socket.h>
|
2007-04-27 00:59:29 +02:00
|
|
|
#endif
|
2009-09-15 23:08:48 +02:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
# include <netinet/in.h>
|
|
|
|
#endif
|
2007-04-27 00:59:29 +02:00
|
|
|
# ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2007-06-06 14:34:06 +02:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
# include <arpa/inet.h>
|
|
|
|
#endif
|
2007-04-27 00:59:29 +02:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
2007-01-30 12:07:14 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2007-06-06 14:34:06 +02:00
|
|
|
unsigned long hostaddr;
|
|
|
|
int sock, i, auth_pw = 1;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
const char *fingerprint;
|
|
|
|
LIBSSH2_SESSION *session;
|
|
|
|
LIBSSH2_CHANNEL *channel;
|
2007-08-09 03:10:11 +02:00
|
|
|
const char *username="username";
|
|
|
|
const char *password="password";
|
|
|
|
const char *scppath="/tmp/TEST";
|
2007-06-06 14:34:06 +02:00
|
|
|
struct stat fileinfo;
|
|
|
|
int rc;
|
|
|
|
off_t got=0;
|
2007-01-30 12:07:14 +01:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2007-06-06 14:34:06 +02:00
|
|
|
WSADATA wsadata;
|
2007-01-30 12:07:14 +01:00
|
|
|
|
2008-11-10 17:48:41 +01:00
|
|
|
WSAStartup(MAKEWORD(2,0), &wsadata);
|
2007-01-30 12:07:14 +01:00
|
|
|
#endif
|
|
|
|
|
2007-06-06 14:34:06 +02:00
|
|
|
if (argc > 1) {
|
|
|
|
hostaddr = inet_addr(argv[1]);
|
|
|
|
} else {
|
|
|
|
hostaddr = htonl(0x7F000001);
|
|
|
|
}
|
|
|
|
if (argc > 2) {
|
|
|
|
username = argv[2];
|
|
|
|
}
|
|
|
|
if (argc > 3) {
|
|
|
|
password = argv[3];
|
|
|
|
}
|
|
|
|
if (argc > 4) {
|
|
|
|
scppath = argv[4];
|
|
|
|
}
|
2007-07-14 23:24:38 +02:00
|
|
|
|
2007-06-06 14:34:06 +02:00
|
|
|
/* 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);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
fprintf(stderr, "failed to connect!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a session instance
|
|
|
|
*/
|
|
|
|
session = libssh2_session_init();
|
|
|
|
if(!session)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* ... start it up. This will trade welcome banners, exchange keys,
|
|
|
|
* and setup crypto, compression, and MAC layers
|
|
|
|
*/
|
|
|
|
rc = libssh2_session_startup(session, sock);
|
|
|
|
if(rc) {
|
|
|
|
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At this point we havn't yet authenticated. The first thing to do
|
|
|
|
* is check the hostkey's fingerprint against our known hosts Your app
|
|
|
|
* may have it hard coded, may go to a file, may present it to the
|
|
|
|
* user, that's your call
|
|
|
|
*/
|
2010-01-29 23:06:31 +01:00
|
|
|
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
2007-06-06 14:34:06 +02:00
|
|
|
fprintf(stderr, "Fingerprint: ");
|
2010-01-29 23:06:31 +01:00
|
|
|
for(i = 0; i < 20; i++) {
|
2007-06-06 14:34:06 +02:00
|
|
|
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
if (auth_pw) {
|
|
|
|
/* We could authenticate via password */
|
|
|
|
if (libssh2_userauth_password(session, username, password)) {
|
|
|
|
fprintf(stderr, "Authentication by password failed.\n");
|
|
|
|
goto shutdown;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Or by public key */
|
|
|
|
if (libssh2_userauth_publickey_fromfile(session, username,
|
|
|
|
"/home/username/.ssh/id_rsa.pub",
|
|
|
|
"/home/username/.ssh/id_rsa",
|
|
|
|
password)) {
|
|
|
|
fprintf(stderr, "\tAuthentication by public key failed\n");
|
|
|
|
goto shutdown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Request a file via SCP */
|
|
|
|
channel = libssh2_scp_recv(session, scppath, &fileinfo);
|
|
|
|
|
|
|
|
if (!channel) {
|
|
|
|
fprintf(stderr, "Unable to open a session\n");
|
|
|
|
goto shutdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while(got < fileinfo.st_size) {
|
|
|
|
char mem[1024];
|
|
|
|
int amount=sizeof(mem);
|
|
|
|
|
|
|
|
if((fileinfo.st_size -got) < amount) {
|
|
|
|
amount = fileinfo.st_size -got;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = libssh2_channel_read(channel, mem, amount);
|
|
|
|
if(rc == amount) {
|
|
|
|
write(1, mem, rc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "libssh2_channel_read() failed: %d\n", rc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
got += rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
libssh2_channel_free(channel);
|
|
|
|
channel = NULL;
|
2007-01-30 12:07:14 +01:00
|
|
|
|
|
|
|
shutdown:
|
|
|
|
|
2007-06-06 14:34:06 +02:00
|
|
|
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
|
|
|
|
libssh2_session_free(session);
|
2007-01-30 12:07:14 +01:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2007-06-06 14:34:06 +02:00
|
|
|
closesocket(sock);
|
2007-01-30 12:07:14 +01:00
|
|
|
#else
|
2007-06-06 14:34:06 +02:00
|
|
|
close(sock);
|
2007-01-30 12:07:14 +01:00
|
|
|
#endif
|
2007-06-06 14:34:06 +02:00
|
|
|
fprintf(stderr, "all done\n");
|
|
|
|
return 0;
|
2007-01-30 12:07:14 +01:00
|
|
|
}
|