~ubuntu-branches/ubuntu/vivid/libssh2/vivid-proposed

« back to all changes in this revision

Viewing changes to example/scp_write.c

  • Committer: Bazaar Package Importer
  • Author(s): Mikhail Gusarov
  • Date: 2010-02-28 13:11:14 UTC
  • mto: (1.1.6 upstream) (2.1.8 sid)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20100228131114-g8d2ps9p1u8i80s3
Tags: upstream-1.2.4
ImportĀ upstreamĀ versionĀ 1.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: scp_write.c,v 1.7 2009/04/28 10:35:30 bagder Exp $
 
3
 *
 
4
 * Sample showing how to do a simple SCP transfer.
 
5
 */
 
6
 
 
7
#include "libssh2_config.h"
 
8
#include <libssh2.h>
 
9
 
 
10
#ifdef HAVE_WINSOCK2_H
 
11
# include <winsock2.h>
 
12
#endif
 
13
#ifdef HAVE_SYS_SOCKET_H
 
14
# include <sys/socket.h>
 
15
#endif
 
16
#ifdef HAVE_NETINET_IN_H
 
17
# include <netinet/in.h>
 
18
#endif
 
19
# ifdef HAVE_UNISTD_H
 
20
#include <unistd.h>
 
21
#endif
 
22
#ifdef HAVE_ARPA_INET_H
 
23
# include <arpa/inet.h>
 
24
#endif
 
25
#ifdef HAVE_SYS_TIME_H
 
26
# include <sys/time.h>
 
27
#endif
 
28
 
 
29
#include <sys/types.h>
 
30
#include <fcntl.h>
 
31
#include <errno.h>
 
32
#include <stdio.h>
 
33
#include <ctype.h>
 
34
 
 
35
int main(int argc, char *argv[])
 
36
{
 
37
    unsigned long hostaddr;
 
38
    int sock, i, auth_pw = 1;
 
39
    struct sockaddr_in sin;
 
40
    const char *fingerprint;
 
41
    LIBSSH2_SESSION *session;
 
42
    LIBSSH2_CHANNEL *channel;
 
43
    const char *username="username";
 
44
    const char *password="password";
 
45
    const char *loclfile="scp_write.c";
 
46
    const char *scppath="/tmp/TEST";
 
47
    FILE *local;
 
48
    int rc;
 
49
    char mem[1024];
 
50
    size_t nread, sent;
 
51
    char *ptr;
 
52
    struct stat fileinfo;
 
53
 
 
54
#ifdef WIN32
 
55
    WSADATA wsadata;
 
56
 
 
57
    WSAStartup(MAKEWORD(2,0), &wsadata);
 
58
#endif
 
59
 
 
60
    if (argc > 1) {
 
61
        hostaddr = inet_addr(argv[1]);
 
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
        loclfile = argv[4];
 
73
    }
 
74
    if (argc > 5) {
 
75
        scppath = argv[5];
 
76
    }
 
77
 
 
78
    local = fopen(loclfile, "rb");
 
79
    if (!local) {
 
80
        fprintf(stderr, "Can't local file %s\n", loclfile);
 
81
        goto shutdown;
 
82
    }
 
83
 
 
84
    stat(loclfile, &fileinfo);
 
85
 
 
86
    /* Ultra basic "connect to port 22 on localhost"
 
87
     * Your code is responsible for creating the socket establishing the
 
88
     * connection
 
89
     */
 
90
    sock = socket(AF_INET, SOCK_STREAM, 0);
 
91
 
 
92
    sin.sin_family = AF_INET;
 
93
    sin.sin_port = htons(22);
 
94
    sin.sin_addr.s_addr = hostaddr;
 
95
    if (connect(sock, (struct sockaddr*)(&sin),
 
96
            sizeof(struct sockaddr_in)) != 0) {
 
97
        fprintf(stderr, "failed to connect!\n");
 
98
        return -1;
 
99
    }
 
100
 
 
101
    /* Create a session instance
 
102
     */
 
103
    session = libssh2_session_init();
 
104
    if(!session)
 
105
        return -1;
 
106
 
 
107
    /* ... start it up. This will trade welcome banners, exchange keys,
 
108
     * and setup crypto, compression, and MAC layers
 
109
     */
 
110
    rc = libssh2_session_startup(session, sock);
 
111
    if(rc) {
 
112
        fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
 
113
        return -1;
 
114
    }
 
115
 
 
116
    /* At this point we havn't yet authenticated.  The first thing to do
 
117
     * is check the hostkey's fingerprint against our known hosts Your app
 
118
     * may have it hard coded, may go to a file, may present it to the
 
119
     * user, that's your call
 
120
     */
 
121
    fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
 
122
    fprintf(stderr, "Fingerprint: ");
 
123
    for(i = 0; i < 20; i++) {
 
124
        fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
 
125
    }
 
126
    fprintf(stderr, "\n");
 
127
 
 
128
    if (auth_pw) {
 
129
        /* We could authenticate via password */
 
130
        if (libssh2_userauth_password(session, username, password)) {
 
131
            fprintf(stderr, "Authentication by password failed.\n");
 
132
            goto shutdown;
 
133
        }
 
134
    } else {
 
135
        /* Or by public key */
 
136
        if (libssh2_userauth_publickey_fromfile(session, username,
 
137
                            "/home/username/.ssh/id_rsa.pub",
 
138
                            "/home/username/.ssh/id_rsa",
 
139
                            password)) {
 
140
            fprintf(stderr, "\tAuthentication by public key failed\n");
 
141
            goto shutdown;
 
142
        }
 
143
    }
 
144
 
 
145
    /* Send a file via scp. The mode parameter must only have permissions! */
 
146
    channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777,
 
147
                               (unsigned long)fileinfo.st_size);
 
148
 
 
149
    if (!channel) {
 
150
        fprintf(stderr, "Unable to open a session\n");
 
151
        goto shutdown;
 
152
    }
 
153
 
 
154
    fprintf(stderr, "SCP session waiting to send file\n");
 
155
    do {
 
156
        nread = fread(mem, 1, sizeof(mem), local);
 
157
        if (nread <= 0) {
 
158
            /* end of file */
 
159
            break;
 
160
        }
 
161
        ptr = mem;
 
162
        sent = 0;
 
163
 
 
164
        do {
 
165
            /* write the same data over and over, until error or completion */
 
166
            rc = libssh2_channel_write(channel, ptr, nread);
 
167
            if (rc < 0) {
 
168
                fprintf(stderr, "ERROR %d\n", rc);
 
169
            } else {
 
170
                /* rc indicates how many bytes were written this time */
 
171
                sent += rc;
 
172
            }
 
173
        } while (rc > 0 && sent < nread);
 
174
        ptr += sent;
 
175
        nread -= sent;
 
176
    } while (1);
 
177
 
 
178
    fprintf(stderr, "Sending EOF\n");
 
179
    libssh2_channel_send_eof(channel);
 
180
 
 
181
    fprintf(stderr, "Waiting for EOF\n");
 
182
    libssh2_channel_wait_eof(channel);
 
183
 
 
184
    fprintf(stderr, "Waiting for channel to close\n");
 
185
    libssh2_channel_wait_closed(channel);
 
186
 
 
187
    libssh2_channel_free(channel);
 
188
    channel = NULL;
 
189
 
 
190
 shutdown:
 
191
 
 
192
    libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
 
193
    libssh2_session_free(session);
 
194
 
 
195
#ifdef WIN32
 
196
    closesocket(sock);
 
197
#else
 
198
    close(sock);
 
199
#endif
 
200
    fprintf(stderr, "all done\n");
 
201
    return 0;
 
202
}