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

« back to all changes in this revision

Viewing changes to example/sftp_mkdir_nonblock.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: sftp_mkdir_nonblock.c,v 1.12 2009/04/28 10:35:30 bagder Exp $
 
3
 *
 
4
 * Sample showing how to do SFTP non-blocking mkdir.
 
5
 *
 
6
 * The sample code has default values for host name, user name, password
 
7
 * and path to copy, but you can specify them on the command line like:
 
8
 *
 
9
 * "sftp 192.168.0.1 user password /tmp/sftp_write_nonblock.c"
 
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/sftp_mkdir_nonblock";
 
48
    int rc;
 
49
#if defined(HAVE_IOCTLSOCKET)
 
50
    long flag = 1;
 
51
#endif
 
52
    LIBSSH2_SFTP *sftp_session;
 
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
 
 
66
    if(argc > 2) {
 
67
        username = argv[2];
 
68
    }
 
69
    if(argc > 3) {
 
70
        password = argv[3];
 
71
    }
 
72
    if(argc > 4) {
 
73
        sftppath = argv[4];
 
74
    }
 
75
 
 
76
    /*
 
77
     * The application code is responsible for creating the socket
 
78
     * and establishing the connection
 
79
     */
 
80
    sock = socket(AF_INET, SOCK_STREAM, 0);
 
81
 
 
82
    sin.sin_family = AF_INET;
 
83
    sin.sin_port = htons(22);
 
84
    sin.sin_addr.s_addr = hostaddr;
 
85
    if (connect(sock, (struct sockaddr*)(&sin),
 
86
            sizeof(struct sockaddr_in)) != 0) {
 
87
        fprintf(stderr, "failed to connect!\n");
 
88
        return -1;
 
89
    }
 
90
 
 
91
    /* Create a session instance
 
92
     */
 
93
    session = libssh2_session_init();
 
94
    if(!session)
 
95
        return -1;
 
96
 
 
97
    /* ... start it up. This will trade welcome banners, exchange keys,
 
98
     * and setup crypto, compression, and MAC layers
 
99
     */
 
100
    rc = libssh2_session_startup(session, sock);
 
101
    if(rc) {
 
102
        fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
 
103
        return -1;
 
104
    }
 
105
 
 
106
    /* At this point we havn't yet authenticated.  The first thing to do
 
107
     * is check the hostkey's fingerprint against our known hosts Your app
 
108
     * may have it hard coded, may go to a file, may present it to the
 
109
     * user, that's your call
 
110
     */
 
111
    fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
 
112
    printf("Fingerprint: ");
 
113
    for(i = 0; i < 20; i++) {
 
114
        printf("%02X ", (unsigned char)fingerprint[i]);
 
115
    }
 
116
    printf("\n");
 
117
 
 
118
    if (auth_pw) {
 
119
        /* We could authenticate via password */
 
120
        if (libssh2_userauth_password(session, username, password)) {
 
121
            printf("Authentication by password failed.\n");
 
122
            goto shutdown;
 
123
        }
 
124
    } else {
 
125
        /* Or by public key */
 
126
        if (libssh2_userauth_publickey_fromfile(session, username,
 
127
                            "/home/username/.ssh/id_rsa.pub",
 
128
                            "/home/username/.ssh/id_rsa",
 
129
                            password)) {
 
130
            printf("\tAuthentication by public key failed\n");
 
131
            goto shutdown;
 
132
        }
 
133
    }
 
134
 
 
135
    fprintf(stderr, "libssh2_sftp_init()!\n");
 
136
    sftp_session = libssh2_sftp_init(session);
 
137
 
 
138
    if (!sftp_session) {
 
139
        fprintf(stderr, "Unable to init SFTP session\n");
 
140
        goto shutdown;
 
141
    }
 
142
 
 
143
    /* Since we have set non-blocking, tell libssh2 we are non-blocking */
 
144
    libssh2_session_set_blocking(session, 0);
 
145
 
 
146
    fprintf(stderr, "libssh2_sftp_mkdirnb()!\n");
 
147
    /* Make a directory via SFTP */
 
148
    while ((rc = libssh2_sftp_mkdir(sftp_session, sftppath,
 
149
                                    LIBSSH2_SFTP_S_IRWXU|
 
150
                                    LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IXGRP|
 
151
                                    LIBSSH2_SFTP_S_IROTH|LIBSSH2_SFTP_S_IXOTH))
 
152
           == LIBSSH2_ERROR_EAGAIN) {
 
153
        ;
 
154
    }
 
155
 
 
156
    libssh2_sftp_shutdown(sftp_session);
 
157
 
 
158
 shutdown:
 
159
 
 
160
    libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
 
161
    libssh2_session_free(session);
 
162
 
 
163
#ifdef WIN32
 
164
    closesocket(sock);
 
165
#else
 
166
    close(sock);
 
167
#endif
 
168
printf("all done\n");
 
169
    return 0;
 
170
}