File: sftpdriver.c

package info (click to toggle)
sitecopy 1:0.16.6-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 5,668 kB
  • ctags: 5,783
  • sloc: ansic: 48,856; sh: 3,820; makefile: 774; yacc: 291; awk: 58; sed: 27
file content (421 lines) | stat: -rw-r--r-- 10,790 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* 
   sitecopy sftp/ssh protocol driver module (forked from rshdriver.c)
   Copyright (C) 2000-2005, Joe Orton <joe@manyfish.co.uk>
   Copyright (C) 2004, Nobuyuki Tsuchimura <tutimura@nn.iij4u.or.jp>
                                                                     
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/* TODO: 
 * - handle ssh directly instead of handling sftp....(?)
 */

#include <config.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <errno.h>

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <ne_alloc.h>
#include <ne_string.h>
#include <ne_socket.h>

#include "protocol.h"
#include "lsparser.h"

#ifndef HAVE_SOCKETPAIR
#define USE_PIPES
#endif

typedef struct {
    struct site *site;
    const char *sftp_cmd, *ssh_cmd;
    char buf[BUFSIZ];
    int connected, fd_in, fd_out, sftp_pid;
    FILE *fp;  /* popen for sftp client */
} sftp_session;

static int run_sftp(sftp_session *sess, const char *template, ...) 
    ne_attribute((format (printf, 2, 3)));

static int read_sftp(sftp_session *sess) {
    size_t pos = 0;

    do {
        ssize_t ret = read(sess->fd_in, sess->buf+pos, BUFSIZ-pos);
        if (ret < 0) return SITE_FAILED;
        if (ret == 0) break;
        pos += ret;
        sess->buf[pos] = '\0';
    } while (strchr(sess->buf, '>') == NULL && pos < BUFSIZ);
    NE_DEBUG(DEBUG_SFTP, "(%s)", sess->buf);
    return SITE_OK;
}
    
static void exec_sftp(sftp_session *sess)
{
    size_t len;
    char *username;

    username = sess->site->server.username;
    len = ne_snprintf(sess->buf, BUFSIZ, "%s%s%s",
                      (username != NULL ? username : ""),
                      (username != NULL ? "@" : ""),
                      sess->site->server.hostname);
    if (len + 1 >= BUFSIZ) {
        NE_DEBUG(DEBUG_SFTP, "sftp: user/host name too long.\n");
        return;
    }

    execlp(sess->sftp_cmd, sess->sftp_cmd, sess->buf, NULL);
    NE_DEBUG(DEBUG_SFTP, "sftp exec: %s %s: %s\n",
             sess->sftp_cmd, sess->buf, strerror(errno));
}

static int sftp_connect(sftp_session *sess)
{
    int c_in, c_out;
#ifdef USE_PIPES
    int pin[2], pout[2];
    if ((pipe(pin) == -1) || (pipe(pout) == -1)) {
        NE_DEBUG(DEBUG_SFTP, "sftp pipe: %s", strerror(errno));
        return SITE_FAILED;
    }
    sess->fd_in  = pin[0];
    sess->fd_out = pout[1];
    c_in = pout[0];
    c_out = pin[1];
#else /* !USE_PIPES */
    int inout[2];
    if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1) {
        NE_DEBUG(DEBUG_SFTP, "sftp socketpair: %s", strerror(errno));
        return SITE_FAILED;
    }
    sess->fd_in = sess->fd_out = inout[0];
    c_in = c_out = inout[1];
#endif /* USE_PIPES */

    sess->connected = true;
    sess->sftp_pid = fork();
    switch (sess->sftp_pid) {
    case -1:
        NE_DEBUG(DEBUG_SFTP, "sftp: fork: %s", strerror(errno));
        return SITE_FAILED;
    case 0:
        if ((dup2(c_in, STDIN_FILENO) == -1) ||
            (dup2(c_out, STDOUT_FILENO) == -1)) {
            NE_DEBUG(DEBUG_SFTP, "sftp: dup2: %s\n", strerror(errno));
            return SITE_FAILED;
        }
        close(sess->fd_in); close(sess->fd_out);
        close(c_in);        close(c_out);
        exec_sftp(sess);
        return SITE_FAILED;
    }
    close(c_in);
    close(c_out);
    return SITE_OK;
}

static int sftp_disconnect(sftp_session *sess)
{
#ifndef USE_PIPES
#ifndef SHUT_RDWR
#define SHUT_RDWR 2
#endif
    shutdown(sess->fd_in,  SHUT_RDWR);
    shutdown(sess->fd_out, SHUT_RDWR);
#endif /* USE_PIPES */
    close(sess->fd_in);
    close(sess->fd_out);

    if (waitpid(sess->sftp_pid, NULL, 0) == -1) {
        NE_DEBUG(DEBUG_SFTP, "sftp: Couldn't wait for sftp process: %s",
                 strerror(errno));
        return SITE_FAILED;
    }
    return SITE_OK;
}


static int run_sftp(sftp_session *sess, const char *template, ...) 
{
    va_list params;
    if (!sess->connected) sftp_connect(sess);

    va_start(params, template);
    ne_vsnprintf(sess->buf, BUFSIZ, template, params);
    va_end(params);

    if (strlen(sess->buf) + 2 >= BUFSIZ) { /* will be added '\n' */
        NE_DEBUG(DEBUG_SFTP, "sftp: sftp: too long command '%s'.\n", sess->buf);
        return SITE_FAILED;  /* buff over flow */
    }
    NE_DEBUG(DEBUG_SFTP, "SFTP: '%s'\n", sess->buf);

    strcat(sess->buf, "\n");
    if (write(sess->fd_out, sess->buf, strlen(sess->buf)) == -1) {
	return SITE_FAILED;
    }
    read_sftp(sess);    /* wait for prompt */
    return SITE_OK;
}


static int run_ssh(sftp_session *sess, const char *template, ...) 
    ne_attribute((format (printf, 2, 3)));

static int run_ssh(sftp_session *sess, const char *template, ...) 
{
    va_list params;
    char *buf = sess->buf;
    size_t len;
    char *username = sess->site->server.username;

    len = ne_snprintf(buf, BUFSIZ, "%s%s%s %s \\\\", 
                      sess->ssh_cmd,
                      (username != NULL ? " -l " : ""),
                      (username != NULL ? username : ""),
                      sess->site->server.hostname);

    va_start(params, template);
    len += ne_vsnprintf(buf + len, BUFSIZ - len, template, params);
    va_end(params);

    len += ne_snprintf(buf + len, BUFSIZ - len, " 2> /dev/null" );
    if (len + 1 >= BUFSIZ) return SITE_FAILED;  /* buff over flow */
    NE_DEBUG(DEBUG_SFTP, "rcmd: %s\n", buf);

    sess->fp = popen(buf, "r");
    if (sess->fp != NULL) {
        return SITE_OK;
    } else {
        return SITE_FAILED;
    }
    /* why not free(cmd)? */
}

static int ssh_finish(sftp_session *sess)
{
    if (pclose(sess->fp) == 0) {
        sess->fp = NULL;
	return SITE_OK;
    } else {
	return SITE_FAILED;
    }
}

static int init(void **session, struct site *site)
{
    sftp_session *sess = ne_calloc(sizeof *sess);
    *session = sess;
    if (site->rcp_cmd != NULL) {
	sess->sftp_cmd = site->rcp_cmd;
    } else {
	sess->sftp_cmd = "sftp";
    }
    if (site->rsh_cmd != NULL) {
	sess->ssh_cmd = site->rsh_cmd;
    } else {
	sess->ssh_cmd = "ssh";
    }
    sess->site = site;
    sess->connected = false;
    return SITE_OK;
}    

static void finish(void *session)
{
    sftp_session *sess = session;

    if (sess->connected) sftp_disconnect(sess);
    if (sess->fp != NULL) ssh_finish(sess);
    free(sess);
}

static int file_move(void *session, const char *from, const char *to)
{
    sftp_session *sess = session;
    return run_sftp(sess, "rename %s %s", from, to);
}

static int file_upload(void *session, const char *local, const char *remote, 
                       int ascii)
{
    sftp_session *sess = session;
    return run_sftp(sess, "put %s %s", local, remote);
}

static int file_upload_cond(void *session,
			    const char *local, const char *remote,
			    int ascii, time_t t)
{
    return SITE_UNSUPPORTED;
}

static int file_get_modtime(void *sess, const char *remote, time_t *modtime)
{
    return SITE_UNSUPPORTED;
}
    
static int file_download(void *session, const char *local, const char *remote,
			 int ascii)
{
    sftp_session *sess = session;
    return run_sftp(sess, "get %s %s", remote, local);
}


static int file_read(void *session, const char *remote,
                     ne_block_reader reader, void *userdata)
{
    sftp_session *sess = session;
    size_t len;

    if (run_ssh(sess, "cat '%s'", remote) != SITE_OK)
        return SITE_FAILED;
    
    while ((len = fread(sess->buf, 1, BUFSIZ, sess->fp)) > 0) {
        reader(userdata, sess->buf, len);
    }

    return ssh_finish(sess);
}

static int file_delete(void *session, const char *filename)
{
    sftp_session *sess = session;
    return run_sftp(sess, "rm %s", filename);
}

static int file_chmod(void *session, const char *fname, const mode_t mode)
{
    sftp_session *sess = session;
    return run_sftp(sess, "chmod %03o %s", mode, fname);
}

static int dir_create(void *session, const char *dirname)
{
    sftp_session *sess = session;
    return run_sftp(sess, "mkdir %s", dirname);
}

static int dir_remove(void *session, const char *dirname)
{
    sftp_session *sess = session;
    return run_sftp(sess, "rmdir %s", dirname);
}

static const char *error(void *session)
{
    return "An error occurred.";
}

static int get_dummy_port(struct site *site)
{
    return 0;
}

static int ssh_fetch(sftp_session *sess, const char *startdir, 
                     struct proto_file **list)
{
    struct proto_file *tail = NULL;
    int success = SITE_OK;
    ls_context_t *lsctx = ls_init(startdir);

    memset(sess->buf, 0, BUFSIZ);
    
    while (fgets(sess->buf, BUFSIZ, sess->fp) != NULL) {
        enum ls_result res;
        struct ls_file lfile;
        
        res = ls_parse(lsctx, sess->buf, &lfile);

        switch (res) {
        case ls_error:
            NE_DEBUG(DEBUG_SFTP, "Could not parse line.\n");
            success = SITE_ERRORS;
            break;

        default:
            ls_pflist_add(list, &tail, &lfile, res);
            break;
	}
    }

    ls_destroy(lsctx);

    NE_DEBUG(DEBUG_SFTP, "Fetch finished successfully.\n");
    return success;
}

static int fetch_list(void *session, const char *dirname, int need_modtimes,
                     struct proto_file **files)
{
    sftp_session *sess = session;
    int ret;

    ret = run_ssh(sess, "ls -la '%s'", dirname);
    if (ret == SITE_OK) {
        ssh_fetch(sess, dirname, files);
        ret = ssh_finish(sess);
    }

    /*
    if (ret == SITE_OK && need_modtimes) {
	ret = sftp_fetch_modtimes(sess, dirname, *files);
    }
    */

    return ret;
}

/* The protocol driver */
const struct proto_driver sftp_driver = {
    init,
    finish,
    file_move,
    file_upload,
    file_upload_cond,
    file_get_modtime,
    file_download,
    file_read,
    file_delete,
    file_chmod,
    dir_create,
    dir_remove,
    NULL, /* create link */
    NULL, /* change link target */
    NULL, /* delete link */
    fetch_list, /* fetch list. */
    error,
    get_dummy_port,
    get_dummy_port,
    "sftp/ssh"
};