1 /*
2  * Sample showing how to do a simple SCP transfer.
3  */
4 
5 #include "libssh2_config.h"
6 #include <libssh2.h>
7 
8 #ifdef HAVE_WINSOCK2_H
9 # include <winsock2.h>
10 #endif
11 #ifdef HAVE_SYS_SOCKET_H
12 # include <sys/socket.h>
13 #endif
14 #ifdef HAVE_NETINET_IN_H
15 # include <netinet/in.h>
16 #endif
17 # ifdef HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
20 #ifdef HAVE_ARPA_INET_H
21 # include <arpa/inet.h>
22 #endif
23 #ifdef HAVE_SYS_TIME_H
24 # include <sys/time.h>
25 #endif
26 
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 
main(int argc,char * argv[])33 int main(int argc, char *argv[])
34 {
35     unsigned long hostaddr;
36     int sock, i, auth_pw = 1;
37     struct sockaddr_in sin;
38     const char *fingerprint;
39     LIBSSH2_SESSION *session;
40     LIBSSH2_CHANNEL *channel;
41     const char *username = "username";
42     const char *password = "password";
43     const char *scppath = "/tmp/TEST";
44     libssh2_struct_stat fileinfo;
45     int rc;
46     libssh2_struct_stat_size got = 0;
47 
48 #ifdef WIN32
49     WSADATA wsadata;
50     int err;
51 
52     err = WSAStartup(MAKEWORD(2, 0), &wsadata);
53     if(err != 0) {
54         fprintf(stderr, "WSAStartup failed with error: %d\n", err);
55         return 1;
56     }
57 #endif
58 
59     if(argc > 1) {
60         hostaddr = inet_addr(argv[1]);
61     }
62     else {
63         hostaddr = htonl(0x7F000001);
64     }
65     if(argc > 2) {
66         username = argv[2];
67     }
68     if(argc > 3) {
69         password = argv[3];
70     }
71     if(argc > 4) {
72         scppath = argv[4];
73     }
74 
75     rc = libssh2_init(0);
76     if(rc) {
77         fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
78         return 1;
79     }
80 
81     /* Ultra basic "connect to port 22 on localhost"
82      * Your code is responsible for creating the socket establishing the
83      * connection
84      */
85     sock = socket(AF_INET, SOCK_STREAM, 0);
86 
87     sin.sin_family = AF_INET;
88     sin.sin_port = htons(22);
89     sin.sin_addr.s_addr = hostaddr;
90     if(connect(sock, (struct sockaddr*)(&sin),
91                sizeof(struct sockaddr_in)) != 0) {
92         fprintf(stderr, "failed to connect!\n");
93         return -1;
94     }
95 
96     /* Create a session instance
97      */
98     session = libssh2_session_init();
99     if(!session)
100         return -1;
101 
102     /* ... start it up. This will trade welcome banners, exchange keys,
103      * and setup crypto, compression, and MAC layers
104      */
105     rc = libssh2_session_handshake(session, sock);
106     if(rc) {
107         fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
108         return -1;
109     }
110 
111     /* At this point we havn't yet authenticated.  The first thing to do
112      * is check the hostkey's fingerprint against our known hosts Your app
113      * may have it hard coded, may go to a file, may present it to the
114      * user, that's your call
115      */
116     fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
117     fprintf(stderr, "Fingerprint: ");
118     for(i = 0; i < 20; i++) {
119         fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
120     }
121     fprintf(stderr, "\n");
122 
123     if(auth_pw) {
124         /* We could authenticate via password */
125         if(libssh2_userauth_password(session, username, password)) {
126             fprintf(stderr, "Authentication by password failed.\n");
127             goto shutdown;
128         }
129     }
130     else {
131         /* Or by public key */
132 #define HOME_DIR "/home/username/"
133         if(libssh2_userauth_publickey_fromfile(session, username,
134                                                HOME_DIR ".ssh/id_rsa.pub",
135                                                HOME_DIR ".ssh/id_rsa",
136                                                password)) {
137             fprintf(stderr, "\tAuthentication by public key failed\n");
138             goto shutdown;
139         }
140     }
141 
142     /* Request a file via SCP */
143     channel = libssh2_scp_recv2(session, scppath, &fileinfo);
144 
145     if(!channel) {
146         fprintf(stderr, "Unable to open a session: %d\n",
147                 libssh2_session_last_errno(session));
148         goto shutdown;
149     }
150 
151 
152     while(got < fileinfo.st_size) {
153         char mem[1024];
154         int amount = sizeof(mem);
155 
156         if((fileinfo.st_size -got) < amount) {
157             amount = (int)(fileinfo.st_size -got);
158         }
159 
160         rc = libssh2_channel_read(channel, mem, amount);
161         if(rc > 0) {
162             write(1, mem, rc);
163         }
164         else if(rc < 0) {
165             fprintf(stderr, "libssh2_channel_read() failed: %d\n", rc);
166             break;
167         }
168         got += rc;
169     }
170 
171     libssh2_channel_free(channel);
172     channel = NULL;
173 
174  shutdown:
175 
176     libssh2_session_disconnect(session,
177                                "Normal Shutdown, Thank you for playing");
178     libssh2_session_free(session);
179 
180 #ifdef WIN32
181     closesocket(sock);
182 #else
183     close(sock);
184 #endif
185     fprintf(stderr, "all done\n");
186 
187     libssh2_exit();
188 
189     return 0;
190 }
191