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

« back to all changes in this revision

Viewing changes to example/simple/sftpdir.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: sftpdir.c,v 1.11 2009/04/28 10:35:30 bagder Exp $
3
 
 *
4
 
 * Sample doing an SFTP directory listing.
5
 
 *
6
 
 * The sample code has default values for host name, user name, password and
7
 
 * path, but you can specify them on the command line like:
8
 
 *
9
 
 * "sftpdir 192.168.0.1 user password /tmp/secretdir"
10
 
 */
11
 
 
12
 
#include "libssh2_config.h"
13
 
#include <libssh2.h>
14
 
#include <libssh2_sftp.h>
15
 
 
16
 
#ifdef HAVE_WINSOCK2_H
17
 
# include <winsock2.h>
18
 
#endif
19
 
#ifdef HAVE_SYS_SOCKET_H
20
 
# include <sys/socket.h>
21
 
#endif
22
 
#ifdef HAVE_NETINET_IN_H
23
 
# include <netinet/in.h>
24
 
#endif
25
 
# ifdef HAVE_UNISTD_H
26
 
#include <unistd.h>
27
 
#endif
28
 
#ifdef HAVE_ARPA_INET_H
29
 
# include <arpa/inet.h>
30
 
#endif
31
 
 
32
 
#include <sys/types.h>
33
 
#include <fcntl.h>
34
 
#include <errno.h>
35
 
#include <stdio.h>
36
 
#include <ctype.h>
37
 
 
38
 
int main(int argc, char *argv[])
39
 
{
40
 
    unsigned long hostaddr;
41
 
    int sock, i, auth_pw = 1;
42
 
    struct sockaddr_in sin;
43
 
    const char *fingerprint;
44
 
    LIBSSH2_SESSION *session;
45
 
    const char *username="username";
46
 
    const char *password="password";
47
 
    const char *sftppath="/tmp/secretdir";
48
 
    int rc;
49
 
    LIBSSH2_SFTP *sftp_session;
50
 
    LIBSSH2_SFTP_HANDLE *sftp_handle;
51
 
 
52
 
#ifdef WIN32
53
 
    WSADATA wsadata;
54
 
 
55
 
    WSAStartup(MAKEWORD(2,0), &wsadata);
56
 
#endif
57
 
 
58
 
    if (argc > 1) {
59
 
        hostaddr = inet_addr(argv[1]);
60
 
    } else {
61
 
        hostaddr = htonl(0x7F000001);
62
 
    }
63
 
 
64
 
    if(argc > 2) {
65
 
        username = argv[2];
66
 
    }
67
 
    if(argc > 3) {
68
 
        password = argv[3];
69
 
    }
70
 
    if(argc > 4) {
71
 
        sftppath = argv[4];
72
 
    }
73
 
    /*
74
 
     * The application code is responsible for creating the socket
75
 
     * and establishing the connection
76
 
     */
77
 
    sock = socket(AF_INET, SOCK_STREAM, 0);
78
 
 
79
 
    sin.sin_family = AF_INET;
80
 
    sin.sin_port = htons(22);
81
 
    sin.sin_addr.s_addr = hostaddr;
82
 
    if (connect(sock, (struct sockaddr*)(&sin),
83
 
            sizeof(struct sockaddr_in)) != 0) {
84
 
        fprintf(stderr, "failed to connect!\n");
85
 
        return -1;
86
 
    }
87
 
 
88
 
    /* Create a session instance
89
 
     */
90
 
    session = libssh2_session_init();
91
 
    if(!session)
92
 
        return -1;
93
 
 
94
 
    /* ... start it up. This will trade welcome banners, exchange keys,
95
 
     * and setup crypto, compression, and MAC layers
96
 
     */
97
 
    rc = libssh2_session_startup(session, sock);
98
 
    if(rc) {
99
 
        fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
100
 
        return -1;
101
 
    }
102
 
 
103
 
    /* At this point we havn't yet authenticated.  The first thing to do
104
 
     * is check the hostkey's fingerprint against our known hosts Your app
105
 
     * may have it hard coded, may go to a file, may present it to the
106
 
     * user, that's your call
107
 
     */
108
 
    fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
109
 
    printf("Fingerprint: ");
110
 
    for(i = 0; i < 16; i++) {
111
 
        printf("%02X ", (unsigned char)fingerprint[i]);
112
 
    }
113
 
    printf("\n");
114
 
 
115
 
    if (auth_pw) {
116
 
        /* We could authenticate via password */
117
 
        if ((i = libssh2_userauth_password(session, username, password))) {
118
 
            printf("Authentication by password failed.\n");
119
 
            goto shutdown;
120
 
        }
121
 
    } else {
122
 
        /* Or by public key */
123
 
        if (libssh2_userauth_publickey_fromfile(session, username,
124
 
                            "/home/username/.ssh/id_rsa.pub",
125
 
                            "/home/username/.ssh/id_rsa",
126
 
                            password)) {
127
 
            printf("\tAuthentication by public key failed\n");
128
 
            goto shutdown;
129
 
        }
130
 
    }
131
 
 
132
 
    fprintf(stderr, "libssh2_sftp_init()!\n");
133
 
    sftp_session = libssh2_sftp_init(session);
134
 
 
135
 
    if (!sftp_session) {
136
 
        fprintf(stderr, "Unable to init SFTP session\n");
137
 
        goto shutdown;
138
 
    }
139
 
 
140
 
    /* Since we have not set non-blocking, tell libssh2 we are blocking */
141
 
    libssh2_session_set_blocking(session, 1);
142
 
 
143
 
    fprintf(stderr, "libssh2_sftp_opendir()!\n");
144
 
    /* Request a dir listing via SFTP */
145
 
    sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);
146
 
 
147
 
    if (!sftp_handle) {
148
 
        fprintf(stderr, "Unable to open dir with SFTP\n");
149
 
        goto shutdown;
150
 
    }
151
 
    fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n");
152
 
    do {
153
 
        char mem[512];
154
 
        char longentry[512];
155
 
        LIBSSH2_SFTP_ATTRIBUTES attrs;
156
 
 
157
 
        /* loop until we fail */
158
 
        rc = libssh2_sftp_readdir_ex(sftp_handle, mem, sizeof(mem),
159
 
                                     longentry, sizeof(longentry), &attrs);
160
 
        if(rc > 0) {
161
 
            /* rc is the length of the file name in the mem
162
 
               buffer */
163
 
 
164
 
            if (longentry[0] != '\0') {
165
 
                printf("%s\n", longentry);
166
 
            } else {
167
 
                if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
168
 
                    /* this should check what permissions it
169
 
                       is and print the output accordingly */
170
 
                    printf("--fix----- ");
171
 
                }
172
 
                else {
173
 
                    printf("---------- ");
174
 
                }
175
 
 
176
 
                if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
177
 
                    printf("%4ld %4ld ", attrs.uid, attrs.gid);
178
 
                }
179
 
                else {
180
 
                    printf("   -    - ");
181
 
                }
182
 
 
183
 
                if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
184
 
                    /* attrs.filesize is an uint64_t according to
185
 
                       the docs but there is no really good and
186
 
                       portable 64bit type for C before C99, and
187
 
                       correspondingly there was no good printf()
188
 
                       option for it... */
189
 
 
190
 
                    printf("%8lld ", attrs.filesize);
191
 
                }
192
 
 
193
 
                printf("%s\n", mem);
194
 
            }
195
 
        }
196
 
        else
197
 
            break;
198
 
 
199
 
    } while (1);
200
 
 
201
 
    libssh2_sftp_closedir(sftp_handle);
202
 
    libssh2_sftp_shutdown(sftp_session);
203
 
 
204
 
 shutdown:
205
 
 
206
 
    libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
207
 
    libssh2_session_free(session);
208
 
 
209
 
#ifdef WIN32
210
 
    closesocket(sock);
211
 
#else
212
 
    close(sock);
213
 
#endif
214
 
printf("all done\n");
215
 
    return 0;
216
 
}