libssh2

Форк
0
/
sftp_mkdir_nonblock.c 
186 строк · 4.9 Кб
1
/* Copyright (C) The libssh2 project and its contributors.
2
 *
3
 * Sample showing how to do SFTP non-blocking mkdir.
4
 *
5
 * The sample code has default values for host name, user name, password
6
 * and path to copy, but you can specify them on the command line like:
7
 *
8
 * $ ./sftp_mkdir_nonblock 192.168.0.1 user password /tmp/sftp_write_nonblock.c
9
 *
10
 * SPDX-License-Identifier: BSD-3-Clause
11
 */
12

13
#include "libssh2_setup.h"
14
#include <libssh2.h>
15
#include <libssh2_sftp.h>
16

17
#ifdef HAVE_SYS_SOCKET_H
18
#include <sys/socket.h>
19
#endif
20
#ifdef HAVE_UNISTD_H
21
#include <unistd.h>
22
#endif
23
#ifdef HAVE_NETINET_IN_H
24
#include <netinet/in.h>
25
#endif
26
#ifdef HAVE_ARPA_INET_H
27
#include <arpa/inet.h>
28
#endif
29

30
#include <stdio.h>
31

32
static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
33
static const char *privkey = "/home/username/.ssh/id_rsa";
34
static const char *username = "username";
35
static const char *password = "password";
36
static const char *sftppath = "/tmp/sftp_mkdir_nonblock";
37

38
int main(int argc, char *argv[])
39
{
40
    uint32_t hostaddr;
41
    libssh2_socket_t sock;
42
    int i, auth_pw = 1;
43
    struct sockaddr_in sin;
44
    const char *fingerprint;
45
    int rc;
46
    LIBSSH2_SESSION *session = NULL;
47
    LIBSSH2_SFTP *sftp_session;
48

49
#ifdef _WIN32
50
    WSADATA wsadata;
51

52
    rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
53
    if(rc) {
54
        fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
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
        sftppath = 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
    /*
82
     * The application code is responsible for creating the socket
83
     * and establishing the connection
84
     */
85
    sock = socket(AF_INET, SOCK_STREAM, 0);
86
    if(sock == LIBSSH2_INVALID_SOCKET) {
87
        fprintf(stderr, "failed to create socket.\n");
88
        goto shutdown;
89
    }
90

91
    sin.sin_family = AF_INET;
92
    sin.sin_port = htons(22);
93
    sin.sin_addr.s_addr = hostaddr;
94
    if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
95
        fprintf(stderr, "failed to connect.\n");
96
        goto shutdown;
97
    }
98

99
    /* Create a session instance */
100
    session = libssh2_session_init();
101
    if(!session) {
102
        fprintf(stderr, "Could not initialize SSH session.\n");
103
        goto shutdown;
104
    }
105

106
    /* ... start it up. This will trade welcome banners, exchange keys,
107
     * and setup crypto, compression, and MAC layers
108
     */
109
    rc = libssh2_session_handshake(session, sock);
110
    if(rc) {
111
        fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
112
        goto shutdown;
113
    }
114

115
    /* At this point we have not yet authenticated.  The first thing to do
116
     * is check the hostkey's fingerprint against our known hosts Your app
117
     * may have it hard coded, may go to a file, may present it to the
118
     * user, that's your call
119
     */
120
    fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
121
    fprintf(stderr, "Fingerprint: ");
122
    for(i = 0; i < 20; i++) {
123
        fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
124
    }
125
    fprintf(stderr, "\n");
126

127
    if(auth_pw) {
128
        /* We could authenticate via password */
129
        if(libssh2_userauth_password(session, username, password)) {
130
            fprintf(stderr, "Authentication by password failed.\n");
131
            goto shutdown;
132
        }
133
    }
134
    else {
135
        /* Or by public key */
136
        if(libssh2_userauth_publickey_fromfile(session, username,
137
                                               pubkey, privkey,
138
                                               password)) {
139
            fprintf(stderr, "Authentication by public key failed.\n");
140
            goto shutdown;
141
        }
142
    }
143

144
    sftp_session = libssh2_sftp_init(session);
145

146
    if(!sftp_session) {
147
        fprintf(stderr, "Unable to init SFTP session\n");
148
        goto shutdown;
149
    }
150

151
    /* Since we have set non-blocking, tell libssh2 we are non-blocking */
152
    libssh2_session_set_blocking(session, 0);
153

154
    /* Make a directory via SFTP */
155
    while(libssh2_sftp_mkdir(sftp_session, sftppath,
156
                             LIBSSH2_SFTP_S_IRWXU |
157
                             LIBSSH2_SFTP_S_IRGRP |
158
                             LIBSSH2_SFTP_S_IXGRP |
159
                             LIBSSH2_SFTP_S_IROTH |
160
                             LIBSSH2_SFTP_S_IXOTH) ==
161
          LIBSSH2_ERROR_EAGAIN);
162

163
    libssh2_sftp_shutdown(sftp_session);
164

165
shutdown:
166

167
    if(session) {
168
        libssh2_session_disconnect(session, "Normal Shutdown");
169
        libssh2_session_free(session);
170
    }
171

172
    if(sock != LIBSSH2_INVALID_SOCKET) {
173
        shutdown(sock, 2);
174
        LIBSSH2_SOCKET_CLOSE(sock);
175
    }
176

177
    fprintf(stderr, "all done\n");
178

179
    libssh2_exit();
180

181
#ifdef _WIN32
182
    WSACleanup();
183
#endif
184

185
    return 0;
186
}
187

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.