1 /*-
2 * SPDX-License-Identifier: (BSD-3-Clause AND Beerware)
3 *
4 * Copyright (c) 1998-2011 Dag-Erling Smørgrav
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $FreeBSD: head/lib/libfetch/ftp.c 341013 2018-11-27 10:45:14Z des $
31 */
32
33 /*
34 * Portions of this code were taken from or based on ftpio.c:
35 *
36 * ----------------------------------------------------------------------------
37 * "THE BEER-WARE LICENSE" (Revision 42):
38 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
39 * can do whatever you want with this stuff. If we meet some day, and you think
40 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
41 * ----------------------------------------------------------------------------
42 *
43 * Major Changelog:
44 *
45 * Dag-Erling Smørgrav
46 * 9 Jun 1998
47 *
48 * Incorporated into libfetch
49 *
50 * Jordan K. Hubbard
51 * 17 Jan 1996
52 *
53 * Turned inside out. Now returns xfers as new file ids, not as a special
54 * `state' of FTP_t
55 *
56 * $ftpioId: ftpio.c,v 1.30 1998/04/11 07:28:53 phk Exp $
57 *
58 */
59
60 #include <sys/param.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
63
64 #include <ctype.h>
65 #include <err.h>
66 #include <errno.h>
67 #include <fcntl.h>
68 #include <netdb.h>
69 #include <stdarg.h>
70 #include <stdint.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <time.h>
75 #include <unistd.h>
76
77 #include "fetch.h"
78 #include "common.h"
79 #include "ftperr.h"
80
81 #define FTP_ANONYMOUS_USER "anonymous"
82
83 #define FTP_CONNECTION_ALREADY_OPEN 125
84 #define FTP_OPEN_DATA_CONNECTION 150
85 #define FTP_OK 200
86 #define FTP_FILE_STATUS 213
87 #define FTP_SERVICE_READY 220
88 #define FTP_TRANSFER_COMPLETE 226
89 #define FTP_PASSIVE_MODE 227
90 #define FTP_LPASSIVE_MODE 228
91 #define FTP_EPASSIVE_MODE 229
92 #define FTP_LOGGED_IN 230
93 #define FTP_FILE_ACTION_OK 250
94 #define FTP_DIRECTORY_CREATED 257 /* multiple meanings */
95 #define FTP_FILE_CREATED 257 /* multiple meanings */
96 #define FTP_WORKING_DIRECTORY 257 /* multiple meanings */
97 #define FTP_NEED_PASSWORD 331
98 #define FTP_NEED_ACCOUNT 332
99 #define FTP_FILE_OK 350
100 #define FTP_SYNTAX_ERROR 500
101 #define FTP_PROTOCOL_ERROR 999
102
103 static int ftp_cmd(conn_t *, const char *, ...) __printflike(2, 3);
104 static struct url cached_host;
105 static conn_t *cached_connection;
106
107 #define isftpreply(foo) \
108 (isdigit((unsigned char)foo[0]) && \
109 isdigit((unsigned char)foo[1]) && \
110 isdigit((unsigned char)foo[2]) && \
111 (foo[3] == ' ' || foo[3] == '\0'))
112 #define isftpinfo(foo) \
113 (isdigit((unsigned char)foo[0]) && \
114 isdigit((unsigned char)foo[1]) && \
115 isdigit((unsigned char)foo[2]) && \
116 foo[3] == '-')
117
118 /*
119 * Translate IPv4 mapped IPv6 address to IPv4 address
120 */
121 static void
unmappedaddr(struct sockaddr_in6 * sin6)122 unmappedaddr(struct sockaddr_in6 *sin6)
123 {
124 struct sockaddr_in *sin4;
125 void *addr;
126 int port;
127
128 if (sin6->sin6_family != AF_INET6 ||
129 !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
130 return;
131 sin4 = (struct sockaddr_in *)sin6;
132 addr = &sin6->sin6_addr.s6_addr[12];
133 port = sin6->sin6_port;
134 memset(sin4, 0, sizeof(struct sockaddr_in));
135 sin4->sin_addr.s_addr = *(uint32_t *)addr;
136 sin4->sin_port = port;
137 sin4->sin_family = AF_INET;
138 sin4->sin_len = sizeof(struct sockaddr_in);
139 }
140
141 /*
142 * Get server response
143 */
144 static int
ftp_chkerr(conn_t * conn)145 ftp_chkerr(conn_t *conn)
146 {
147 if (fetch_getln(conn) == -1) {
148 fetch_syserr();
149 return (-1);
150 }
151 if (isftpinfo(conn->buf)) {
152 while (conn->buflen && !isftpreply(conn->buf)) {
153 if (fetch_getln(conn) == -1) {
154 fetch_syserr();
155 return (-1);
156 }
157 }
158 }
159
160 while (conn->buflen &&
161 isspace((unsigned char)conn->buf[conn->buflen - 1]))
162 conn->buflen--;
163 conn->buf[conn->buflen] = '\0';
164
165 if (!isftpreply(conn->buf)) {
166 ftp_seterr(FTP_PROTOCOL_ERROR);
167 return (-1);
168 }
169
170 conn->err = (conn->buf[0] - '0') * 100
171 + (conn->buf[1] - '0') * 10
172 + (conn->buf[2] - '0');
173
174 return (conn->err);
175 }
176
177 /*
178 * Send a command and check reply
179 */
180 static int
ftp_cmd(conn_t * conn,const char * fmt,...)181 ftp_cmd(conn_t *conn, const char *fmt, ...)
182 {
183 va_list ap;
184 size_t len;
185 char *msg;
186 int r;
187
188 va_start(ap, fmt);
189 len = vasprintf(&msg, fmt, ap);
190 va_end(ap);
191
192 if (msg == NULL) {
193 errno = ENOMEM;
194 fetch_syserr();
195 return (-1);
196 }
197
198 r = fetch_putln(conn, msg, len);
199 free(msg);
200
201 if (r == -1) {
202 fetch_syserr();
203 return (-1);
204 }
205
206 return (ftp_chkerr(conn));
207 }
208
209 /*
210 * Return a pointer to the filename part of a path
211 */
212 static const char *
ftp_filename(const char * file,int * len,int * type)213 ftp_filename(const char *file, int *len, int *type)
214 {
215 const char *s;
216
217 if ((s = strrchr(file, '/')) == NULL)
218 s = file;
219 else
220 s = s + 1;
221 *len = strlen(s);
222 if (*len > 7 && strncmp(s + *len - 7, ";type=", 6) == 0) {
223 *type = s[*len - 1];
224 *len -= 7;
225 } else {
226 *type = '\0';
227 }
228 return (s);
229 }
230
231 /*
232 * Get current working directory from the reply to a CWD, PWD or CDUP
233 * command.
234 */
235 static int
ftp_pwd(conn_t * conn,char * pwd,size_t pwdlen)236 ftp_pwd(conn_t *conn, char *pwd, size_t pwdlen)
237 {
238 char *src, *dst, *end;
239 int q;
240
241 if (conn->err != FTP_WORKING_DIRECTORY &&
242 conn->err != FTP_FILE_ACTION_OK)
243 return (FTP_PROTOCOL_ERROR);
244 end = conn->buf + conn->buflen;
245 src = conn->buf + 4;
246 if (src >= end || *src++ != '"')
247 return (FTP_PROTOCOL_ERROR);
248 for (q = 0, dst = pwd; src < end && pwdlen--; ++src) {
249 if (!q && *src == '"')
250 q = 1;
251 else if (q && *src != '"')
252 break;
253 else if (q)
254 *dst++ = '"', q = 0;
255 else
256 *dst++ = *src;
257 }
258 if (!pwdlen)
259 return (FTP_PROTOCOL_ERROR);
260 *dst = '\0';
261 #if 0
262 DEBUGF("pwd: [%s]\n", pwd);
263 #endif
264 return (FTP_OK);
265 }
266
267 /*
268 * Change working directory to the directory that contains the specified
269 * file.
270 */
271 static int
ftp_cwd(conn_t * conn,const char * file)272 ftp_cwd(conn_t *conn, const char *file)
273 {
274 const char *beg, *end;
275 char pwd[PATH_MAX];
276 int e, i, len;
277
278 /* If no slashes in name, no need to change dirs. */
279 if ((end = strrchr(file, '/')) == NULL)
280 return (0);
281 if ((e = ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||
282 (e = ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) {
283 ftp_seterr(e);
284 return (-1);
285 }
286 for (;;) {
287 len = strlen(pwd);
288
289 /* Look for a common prefix between PWD and dir to fetch. */
290 for (i = 0; i <= len && i <= end - file; ++i)
291 if (pwd[i] != file[i])
292 break;
293 #if 0
294 DEBUGF("have: [%.*s|%s]\n", i, pwd, pwd + i);
295 DEBUGF("want: [%.*s|%s]\n", i, file, file + i);
296 #endif
297 /* Keep going up a dir until we have a matching prefix. */
298 if (pwd[i] == '\0' && (file[i - 1] == '/' || file[i] == '/'))
299 break;
300 if ((e = ftp_cmd(conn, "CDUP")) != FTP_FILE_ACTION_OK ||
301 (e = ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||
302 (e = ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) {
303 ftp_seterr(e);
304 return (-1);
305 }
306 }
307
308 #ifdef FTP_COMBINE_CWDS
309 /* Skip leading slashes, even "////". */
310 for (beg = file + i; beg < end && *beg == '/'; ++beg, ++i)
311 /* nothing */ ;
312
313 /* If there is no trailing dir, we're already there. */
314 if (beg >= end)
315 return (0);
316
317 /* Change to the directory all in one chunk (e.g., foo/bar/baz). */
318 e = ftp_cmd(conn, "CWD %.*s", (int)(end - beg), beg);
319 if (e == FTP_FILE_ACTION_OK)
320 return (0);
321 #endif /* FTP_COMBINE_CWDS */
322
323 /* That didn't work so go back to legacy behavior (multiple CWDs). */
324 for (beg = file + i; beg < end; beg = file + i + 1) {
325 while (*beg == '/')
326 ++beg, ++i;
327 for (++i; file + i < end && file[i] != '/'; ++i)
328 /* nothing */ ;
329 e = ftp_cmd(conn, "CWD %.*s", (int)(file + i - beg), beg);
330 if (e != FTP_FILE_ACTION_OK) {
331 ftp_seterr(e);
332 return (-1);
333 }
334 }
335 return (0);
336 }
337
338 /*
339 * Set transfer mode and data type
340 */
341 static int
ftp_mode_type(conn_t * conn,int mode,int type)342 ftp_mode_type(conn_t *conn, int mode, int type)
343 {
344 int e;
345
346 switch (mode) {
347 case 0:
348 case 's':
349 mode = 'S';
350 case 'S':
351 break;
352 default:
353 return (FTP_PROTOCOL_ERROR);
354 }
355 if ((e = ftp_cmd(conn, "MODE %c", mode)) != FTP_OK) {
356 if (mode == 'S') {
357 /*
358 * Stream mode is supposed to be the default - so
359 * much so that some servers not only do not
360 * support any other mode, but do not support the
361 * MODE command at all.
362 *
363 * If "MODE S" fails, it is unlikely that we
364 * previously succeeded in setting a different
365 * mode. Therefore, we simply hope that the
366 * server is already in the correct mode, and
367 * silently ignore the failure.
368 */
369 } else {
370 return (e);
371 }
372 }
373
374 switch (type) {
375 case 0:
376 case 'i':
377 type = 'I';
378 case 'I':
379 break;
380 case 'a':
381 type = 'A';
382 case 'A':
383 break;
384 case 'd':
385 type = 'D';
386 /* FALLTHROUGH */
387 case 'D':
388 /* can't handle yet */
389 default:
390 return (FTP_PROTOCOL_ERROR);
391 }
392 if ((e = ftp_cmd(conn, "TYPE %c", type)) != FTP_OK)
393 return (e);
394
395 return (FTP_OK);
396 }
397
398 /*
399 * Request and parse file stats
400 */
401 static int
ftp_stat(conn_t * conn,const char * file,struct url_stat * us)402 ftp_stat(conn_t *conn, const char *file, struct url_stat *us)
403 {
404 char *ln;
405 const char *filename;
406 int filenamelen, type;
407 struct tm tm;
408 time_t t;
409 int e;
410
411 us->size = -1;
412 us->atime = us->mtime = 0;
413
414 filename = ftp_filename(file, &filenamelen, &type);
415
416 if ((e = ftp_mode_type(conn, 0, type)) != FTP_OK) {
417 ftp_seterr(e);
418 return (-1);
419 }
420
421 e = ftp_cmd(conn, "SIZE %.*s", filenamelen, filename);
422 if (e != FTP_FILE_STATUS) {
423 ftp_seterr(e);
424 return (-1);
425 }
426 for (ln = conn->buf + 4; *ln && isspace((unsigned char)*ln); ln++)
427 /* nothing */ ;
428 for (us->size = 0; *ln && isdigit((unsigned char)*ln); ln++)
429 us->size = us->size * 10 + *ln - '0';
430 if (*ln && !isspace((unsigned char)*ln)) {
431 ftp_seterr(FTP_PROTOCOL_ERROR);
432 us->size = -1;
433 return (-1);
434 }
435 if (us->size == 0)
436 us->size = -1;
437 DEBUGF("size: [%lld]\n", (long long)us->size);
438
439 e = ftp_cmd(conn, "MDTM %.*s", filenamelen, filename);
440 if (e != FTP_FILE_STATUS) {
441 ftp_seterr(e);
442 return (-1);
443 }
444 for (ln = conn->buf + 4; *ln && isspace((unsigned char)*ln); ln++)
445 /* nothing */ ;
446 switch (strspn(ln, "0123456789")) {
447 case 14:
448 break;
449 case 15:
450 ln++;
451 ln[0] = '2';
452 ln[1] = '0';
453 break;
454 default:
455 ftp_seterr(FTP_PROTOCOL_ERROR);
456 return (-1);
457 }
458 if (sscanf(ln, "%04d%02d%02d%02d%02d%02d",
459 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
460 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
461 ftp_seterr(FTP_PROTOCOL_ERROR);
462 return (-1);
463 }
464 tm.tm_mon--;
465 tm.tm_year -= 1900;
466 tm.tm_isdst = -1;
467 t = timegm(&tm);
468 if (t == (time_t)-1)
469 t = time(NULL);
470 us->mtime = t;
471 us->atime = t;
472 DEBUGF("last modified: [%04d-%02d-%02d %02d:%02d:%02d]\n",
473 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
474 tm.tm_hour, tm.tm_min, tm.tm_sec);
475 return (0);
476 }
477
478 /*
479 * I/O functions for FTP
480 */
481 struct ftpio {
482 conn_t *cconn; /* Control connection */
483 conn_t *dconn; /* Data connection */
484 int dir; /* Direction */
485 int eof; /* EOF reached */
486 int err; /* Error code */
487 };
488
489 static int ftp_readfn(void *, char *, int);
490 static int ftp_writefn(void *, const char *, int);
491 static fpos_t ftp_seekfn(void *, fpos_t, int);
492 static int ftp_closefn(void *);
493
494 static int
ftp_readfn(void * v,char * buf,int len)495 ftp_readfn(void *v, char *buf, int len)
496 {
497 struct ftpio *io;
498 int r;
499
500 io = (struct ftpio *)v;
501 if (io == NULL) {
502 errno = EBADF;
503 return (-1);
504 }
505 if (io->cconn == NULL || io->dconn == NULL || io->dir == O_WRONLY) {
506 errno = EBADF;
507 return (-1);
508 }
509 if (io->err) {
510 errno = io->err;
511 return (-1);
512 }
513 if (io->eof)
514 return (0);
515 r = fetch_read(io->dconn, buf, len);
516 if (r > 0)
517 return (r);
518 if (r == 0) {
519 io->eof = 1;
520 return (0);
521 }
522 if (errno != EINTR)
523 io->err = errno;
524 return (-1);
525 }
526
527 static int
ftp_writefn(void * v,const char * buf,int len)528 ftp_writefn(void *v, const char *buf, int len)
529 {
530 struct ftpio *io;
531 int w;
532
533 io = (struct ftpio *)v;
534 if (io == NULL) {
535 errno = EBADF;
536 return (-1);
537 }
538 if (io->cconn == NULL || io->dconn == NULL || io->dir == O_RDONLY) {
539 errno = EBADF;
540 return (-1);
541 }
542 if (io->err) {
543 errno = io->err;
544 return (-1);
545 }
546 w = fetch_write(io->dconn, buf, len);
547 if (w >= 0)
548 return (w);
549 if (errno != EINTR)
550 io->err = errno;
551 return (-1);
552 }
553
554 static fpos_t
ftp_seekfn(void * v,fpos_t pos __unused,int whence __unused)555 ftp_seekfn(void *v, fpos_t pos __unused, int whence __unused)
556 {
557 struct ftpio *io;
558
559 io = (struct ftpio *)v;
560 if (io == NULL) {
561 errno = EBADF;
562 return (-1);
563 }
564 errno = ESPIPE;
565 return (-1);
566 }
567
568 static int
ftp_closefn(void * v)569 ftp_closefn(void *v)
570 {
571 struct ftpio *io;
572 int r;
573
574 io = (struct ftpio *)v;
575 if (io == NULL) {
576 errno = EBADF;
577 return (-1);
578 }
579 if (io->dir == -1)
580 return (0);
581 if (io->cconn == NULL || io->dconn == NULL) {
582 errno = EBADF;
583 return (-1);
584 }
585 fetch_close(io->dconn);
586 io->dir = -1;
587 io->dconn = NULL;
588 DEBUGF("Waiting for final status\n");
589 r = ftp_chkerr(io->cconn);
590 if (io->cconn == cached_connection && io->cconn->ref == 1)
591 cached_connection = NULL;
592 fetch_close(io->cconn);
593 free(io);
594 return (r == FTP_TRANSFER_COMPLETE) ? 0 : -1;
595 }
596
597 static FILE *
ftp_setup(conn_t * cconn,conn_t * dconn,int mode)598 ftp_setup(conn_t *cconn, conn_t *dconn, int mode)
599 {
600 struct ftpio *io;
601 FILE *f;
602
603 if (cconn == NULL || dconn == NULL)
604 return (NULL);
605 if ((io = malloc(sizeof(*io))) == NULL)
606 return (NULL);
607 io->cconn = cconn;
608 io->dconn = dconn;
609 io->dir = mode;
610 io->eof = io->err = 0;
611 f = funopen(io, ftp_readfn, ftp_writefn, ftp_seekfn, ftp_closefn);
612 if (f == NULL)
613 free(io);
614 return (f);
615 }
616
617 /*
618 * Transfer file
619 */
620 static FILE *
ftp_transfer(conn_t * conn,const char * oper,const char * file,int mode,off_t offset,const char * flags)621 ftp_transfer(conn_t *conn, const char *oper, const char *file,
622 int mode, off_t offset, const char *flags)
623 {
624 struct sockaddr_storage sa;
625 struct sockaddr_in6 *sin6;
626 struct sockaddr_in *sin4;
627 const char *bindaddr;
628 const char *filename;
629 int filenamelen, type;
630 int low, pasv, verbose;
631 int e, sd = -1;
632 socklen_t l;
633 char *s;
634 FILE *df;
635
636 /* check flags */
637 low = CHECK_FLAG('l');
638 pasv = CHECK_FLAG('p') || !CHECK_FLAG('P');
639 verbose = CHECK_FLAG('v');
640
641 /* passive mode */
642 if ((s = getenv("FTP_PASSIVE_MODE")) != NULL)
643 pasv = (strncasecmp(s, "no", 2) != 0);
644
645 /* isolate filename */
646 filename = ftp_filename(file, &filenamelen, &type);
647
648 /* set transfer mode and data type */
649 if ((e = ftp_mode_type(conn, 0, type)) != FTP_OK)
650 goto ouch;
651
652 /* find our own address, bind, and listen */
653 l = sizeof(sa);
654 if (getsockname(conn->sd, (struct sockaddr *)&sa, &l) == -1)
655 goto sysouch;
656 if (sa.ss_family == AF_INET6)
657 unmappedaddr((struct sockaddr_in6 *)&sa);
658
659 /* open data socket */
660 if ((sd = socket(sa.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
661 fetch_syserr();
662 return (NULL);
663 }
664
665 if (pasv) {
666 u_char addr[64];
667 char *ln, *p;
668 unsigned int i;
669 int port;
670
671 /* send PASV command */
672 if (verbose)
673 fetch_info("setting passive mode");
674 switch (sa.ss_family) {
675 case AF_INET:
676 if ((e = ftp_cmd(conn, "PASV")) != FTP_PASSIVE_MODE)
677 goto ouch;
678 break;
679 case AF_INET6:
680 if ((e = ftp_cmd(conn, "EPSV")) != FTP_EPASSIVE_MODE) {
681 if (e == -1)
682 goto ouch;
683 if ((e = ftp_cmd(conn, "LPSV")) !=
684 FTP_LPASSIVE_MODE)
685 goto ouch;
686 }
687 break;
688 default:
689 e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
690 goto ouch;
691 }
692
693 /*
694 * Find address and port number. The reply to the PASV command
695 * is IMHO the one and only weak point in the FTP protocol.
696 */
697 ln = conn->buf;
698 switch (e) {
699 case FTP_PASSIVE_MODE:
700 case FTP_LPASSIVE_MODE:
701 for (p = ln + 3; *p && !isdigit((unsigned char)*p); p++)
702 /* nothing */ ;
703 if (!*p) {
704 e = FTP_PROTOCOL_ERROR;
705 goto ouch;
706 }
707 l = (e == FTP_PASSIVE_MODE ? 6 : 21);
708 for (i = 0; *p && i < l; i++, p++)
709 addr[i] = strtol(p, &p, 10);
710 if (i < l) {
711 e = FTP_PROTOCOL_ERROR;
712 goto ouch;
713 }
714 break;
715 case FTP_EPASSIVE_MODE:
716 for (p = ln + 3; *p && *p != '('; p++)
717 /* nothing */ ;
718 if (!*p) {
719 e = FTP_PROTOCOL_ERROR;
720 goto ouch;
721 }
722 ++p;
723 if (sscanf(p, "%c%c%c%d%c", &addr[0], &addr[1], &addr[2],
724 &port, &addr[3]) != 5 ||
725 addr[0] != addr[1] ||
726 addr[0] != addr[2] || addr[0] != addr[3]) {
727 e = FTP_PROTOCOL_ERROR;
728 goto ouch;
729 }
730 break;
731 }
732
733 /* seek to required offset */
734 if (offset)
735 if (ftp_cmd(conn, "REST %lu", (u_long)offset) != FTP_FILE_OK)
736 goto sysouch;
737
738 /* construct sockaddr for data socket */
739 l = sizeof(sa);
740 if (getpeername(conn->sd, (struct sockaddr *)&sa, &l) == -1)
741 goto sysouch;
742 if (sa.ss_family == AF_INET6)
743 unmappedaddr((struct sockaddr_in6 *)&sa);
744 switch (sa.ss_family) {
745 case AF_INET6:
746 sin6 = (struct sockaddr_in6 *)&sa;
747 if (e == FTP_EPASSIVE_MODE)
748 sin6->sin6_port = htons(port);
749 else {
750 memcpy(&sin6->sin6_addr, addr + 2, 16);
751 memcpy(&sin6->sin6_port, addr + 19, 2);
752 }
753 break;
754 case AF_INET:
755 sin4 = (struct sockaddr_in *)&sa;
756 if (e == FTP_EPASSIVE_MODE)
757 sin4->sin_port = htons(port);
758 else {
759 memcpy(&sin4->sin_addr, addr, 4);
760 memcpy(&sin4->sin_port, addr + 4, 2);
761 }
762 break;
763 default:
764 e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
765 break;
766 }
767
768 /* connect to data port */
769 if (verbose)
770 fetch_info("opening data connection");
771 bindaddr = getenv("FETCH_BIND_ADDRESS");
772 if (bindaddr != NULL && *bindaddr != '\0' &&
773 (e = fetch_bind(sd, sa.ss_family, bindaddr)) != 0)
774 goto ouch;
775 if (connect(sd, (struct sockaddr *)&sa, sa.ss_len) == -1)
776 goto sysouch;
777
778 /* make the server initiate the transfer */
779 if (verbose)
780 fetch_info("initiating transfer");
781 e = ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
782 if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
783 goto ouch;
784
785 } else {
786 u_int32_t a;
787 u_short p;
788 int arg, d;
789 char *ap;
790 char hname[INET6_ADDRSTRLEN];
791
792 switch (sa.ss_family) {
793 case AF_INET6:
794 ((struct sockaddr_in6 *)&sa)->sin6_port = 0;
795 #ifdef IPV6_PORTRANGE
796 arg = low ? IPV6_PORTRANGE_DEFAULT : IPV6_PORTRANGE_HIGH;
797 if (setsockopt(sd, IPPROTO_IPV6, IPV6_PORTRANGE,
798 (char *)&arg, sizeof(arg)) == -1)
799 goto sysouch;
800 #endif
801 break;
802 case AF_INET:
803 ((struct sockaddr_in *)&sa)->sin_port = 0;
804 arg = low ? IP_PORTRANGE_DEFAULT : IP_PORTRANGE_HIGH;
805 if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE,
806 (char *)&arg, sizeof(arg)) == -1)
807 goto sysouch;
808 break;
809 }
810 if (verbose)
811 fetch_info("binding data socket");
812 if (bind(sd, (struct sockaddr *)&sa, sa.ss_len) == -1)
813 goto sysouch;
814 if (listen(sd, 1) == -1)
815 goto sysouch;
816
817 /* find what port we're on and tell the server */
818 if (getsockname(sd, (struct sockaddr *)&sa, &l) == -1)
819 goto sysouch;
820 switch (sa.ss_family) {
821 case AF_INET:
822 sin4 = (struct sockaddr_in *)&sa;
823 a = ntohl(sin4->sin_addr.s_addr);
824 p = ntohs(sin4->sin_port);
825 e = ftp_cmd(conn, "PORT %d,%d,%d,%d,%d,%d",
826 (a >> 24) & 0xff, (a >> 16) & 0xff,
827 (a >> 8) & 0xff, a & 0xff,
828 (p >> 8) & 0xff, p & 0xff);
829 break;
830 case AF_INET6:
831 #define UC(b) (((int)b)&0xff)
832 e = -1;
833 sin6 = (struct sockaddr_in6 *)&sa;
834 sin6->sin6_scope_id = 0;
835 if (getnameinfo((struct sockaddr *)&sa, sa.ss_len,
836 hname, sizeof(hname),
837 NULL, 0, NI_NUMERICHOST) == 0) {
838 e = ftp_cmd(conn, "EPRT |%d|%s|%d|", 2, hname,
839 htons(sin6->sin6_port));
840 if (e == -1)
841 goto ouch;
842 }
843 if (e != FTP_OK) {
844 ap = (char *)&sin6->sin6_addr;
845 e = ftp_cmd(conn,
846 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
847 6, 16,
848 UC(ap[0]), UC(ap[1]), UC(ap[2]), UC(ap[3]),
849 UC(ap[4]), UC(ap[5]), UC(ap[6]), UC(ap[7]),
850 UC(ap[8]), UC(ap[9]), UC(ap[10]), UC(ap[11]),
851 UC(ap[12]), UC(ap[13]), UC(ap[14]), UC(ap[15]),
852 2,
853 (ntohs(sin6->sin6_port) >> 8) & 0xff,
854 ntohs(sin6->sin6_port) & 0xff);
855 }
856 break;
857 default:
858 e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
859 goto ouch;
860 }
861 if (e != FTP_OK)
862 goto ouch;
863
864 /* seek to required offset */
865 if (offset)
866 if (ftp_cmd(conn, "REST %ju", (uintmax_t)offset) != FTP_FILE_OK)
867 goto sysouch;
868
869 /* make the server initiate the transfer */
870 if (verbose)
871 fetch_info("initiating transfer");
872 e = ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
873 if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
874 goto ouch;
875
876 /* accept the incoming connection and go to town */
877 if ((d = accept(sd, NULL, NULL)) == -1)
878 goto sysouch;
879 close(sd);
880 sd = d;
881 }
882
883 if ((df = ftp_setup(conn, fetch_reopen(sd), mode)) == NULL)
884 goto sysouch;
885 return (df);
886
887 sysouch:
888 fetch_syserr();
889 if (sd >= 0)
890 close(sd);
891 return (NULL);
892
893 ouch:
894 if (e != -1)
895 ftp_seterr(e);
896 if (sd >= 0)
897 close(sd);
898 return (NULL);
899 }
900
901 /*
902 * Authenticate
903 */
904 static int
ftp_authenticate(conn_t * conn,struct url * url,struct url * purl)905 ftp_authenticate(conn_t *conn, struct url *url, struct url *purl)
906 {
907 const char *user, *pwd, *logname;
908 char pbuf[MAXHOSTNAMELEN + MAXLOGNAME + 1];
909 int e, len;
910
911 /* XXX FTP_AUTH, and maybe .netrc */
912
913 /* send user name and password */
914 if (url->user[0] == '\0')
915 fetch_netrc_auth(url);
916 user = url->user;
917 if (*user == '\0')
918 if ((user = getenv("FTP_LOGIN")) != NULL)
919 DEBUGF("FTP_LOGIN=%s\n", user);
920 if (user == NULL || *user == '\0')
921 user = FTP_ANONYMOUS_USER;
922 if (purl && url->port == fetch_default_port(url->scheme))
923 e = ftp_cmd(conn, "USER %s@%s", user, url->host);
924 else if (purl)
925 e = ftp_cmd(conn, "USER %s@%s@%d", user, url->host, url->port);
926 else
927 e = ftp_cmd(conn, "USER %s", user);
928
929 /* did the server request a password? */
930 if (e == FTP_NEED_PASSWORD) {
931 pwd = url->pwd;
932 if (*pwd == '\0')
933 if ((pwd = getenv("FTP_PASSWORD")) != NULL)
934 DEBUGF("FTP_PASSWORD=%s\n", pwd);
935 if (pwd == NULL || *pwd == '\0') {
936 if ((logname = getlogin()) == NULL)
937 logname = FTP_ANONYMOUS_USER;
938 if ((len = snprintf(pbuf, MAXLOGNAME + 1, "%s@", logname)) < 0)
939 len = 0;
940 else if (len > MAXLOGNAME)
941 len = MAXLOGNAME;
942 gethostname(pbuf + len, sizeof(pbuf) - len);
943 pwd = pbuf;
944 }
945 e = ftp_cmd(conn, "PASS %s", pwd);
946 }
947
948 return (e);
949 }
950
951 /*
952 * Log on to FTP server
953 */
954 static conn_t *
ftp_connect(struct url * url,struct url * purl,const char * flags)955 ftp_connect(struct url *url, struct url *purl, const char *flags)
956 {
957 conn_t *conn;
958 int e, direct, verbose;
959 #ifdef INET6
960 int af = AF_UNSPEC;
961 #else
962 int af = AF_INET;
963 #endif
964
965 direct = CHECK_FLAG('d');
966 verbose = CHECK_FLAG('v');
967 if (CHECK_FLAG('4'))
968 af = AF_INET;
969 else if (CHECK_FLAG('6'))
970 af = AF_INET6;
971
972 if (direct)
973 purl = NULL;
974
975 /* check for proxy */
976 if (purl) {
977 /* XXX proxy authentication! */
978 conn = fetch_connect(purl->host, purl->port, af, verbose);
979 } else {
980 /* no proxy, go straight to target */
981 conn = fetch_connect(url->host, url->port, af, verbose);
982 purl = NULL;
983 }
984
985 /* check connection */
986 if (conn == NULL)
987 /* fetch_connect() has already set an error code */
988 return (NULL);
989
990 /* expect welcome message */
991 if ((e = ftp_chkerr(conn)) != FTP_SERVICE_READY)
992 goto fouch;
993
994 /* authenticate */
995 if ((e = ftp_authenticate(conn, url, purl)) != FTP_LOGGED_IN)
996 goto fouch;
997
998 /* TODO: Request extended features supported, if any (RFC 3659). */
999
1000 /* done */
1001 return (conn);
1002
1003 fouch:
1004 if (e != -1)
1005 ftp_seterr(e);
1006 fetch_close(conn);
1007 return (NULL);
1008 }
1009
1010 /*
1011 * Disconnect from server
1012 */
1013 static void
ftp_disconnect(conn_t * conn)1014 ftp_disconnect(conn_t *conn)
1015 {
1016 (void)ftp_cmd(conn, "QUIT");
1017 if (conn == cached_connection && conn->ref == 1)
1018 cached_connection = NULL;
1019 fetch_close(conn);
1020 }
1021
1022 /*
1023 * Check if we're already connected
1024 */
1025 static int
ftp_isconnected(struct url * url)1026 ftp_isconnected(struct url *url)
1027 {
1028 return (cached_connection
1029 && (strcmp(url->host, cached_host.host) == 0)
1030 && (strcmp(url->user, cached_host.user) == 0)
1031 && (strcmp(url->pwd, cached_host.pwd) == 0)
1032 && (url->port == cached_host.port));
1033 }
1034
1035 /*
1036 * Check the cache, reconnect if no luck
1037 */
1038 static conn_t *
ftp_cached_connect(struct url * url,struct url * purl,const char * flags)1039 ftp_cached_connect(struct url *url, struct url *purl, const char *flags)
1040 {
1041 conn_t *conn;
1042 int e;
1043
1044 /* set default port */
1045 if (!url->port)
1046 url->port = fetch_default_port(url->scheme);
1047
1048 /* try to use previously cached connection */
1049 if (ftp_isconnected(url)) {
1050 e = ftp_cmd(cached_connection, "NOOP");
1051 if (e == FTP_OK || e == FTP_SYNTAX_ERROR)
1052 return (fetch_ref(cached_connection));
1053 }
1054
1055 /* connect to server */
1056 if ((conn = ftp_connect(url, purl, flags)) == NULL)
1057 return (NULL);
1058 if (cached_connection)
1059 ftp_disconnect(cached_connection);
1060 cached_connection = fetch_ref(conn);
1061 memcpy(&cached_host, url, sizeof(*url));
1062 return (conn);
1063 }
1064
1065 /*
1066 * Check the proxy settings
1067 */
1068 static struct url *
ftp_get_proxy(struct url * url,const char * flags)1069 ftp_get_proxy(struct url * url, const char *flags)
1070 {
1071 struct url *purl;
1072 char *p;
1073
1074 if (flags != NULL && strchr(flags, 'd') != NULL)
1075 return (NULL);
1076 if (fetch_no_proxy_match(url->host))
1077 return (NULL);
1078 if (((p = getenv("FTP_PROXY")) || (p = getenv("ftp_proxy")) ||
1079 (p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
1080 *p && (purl = fetchParseURL(p)) != NULL) {
1081 if (!*purl->scheme) {
1082 if (getenv("FTP_PROXY") || getenv("ftp_proxy"))
1083 strcpy(purl->scheme, SCHEME_FTP);
1084 else
1085 strcpy(purl->scheme, SCHEME_HTTP);
1086 }
1087 if (!purl->port)
1088 purl->port = fetch_default_proxy_port(purl->scheme);
1089 if (strcmp(purl->scheme, SCHEME_FTP) == 0 ||
1090 strcmp(purl->scheme, SCHEME_HTTP) == 0)
1091 return (purl);
1092 fetchFreeURL(purl);
1093 }
1094 return (NULL);
1095 }
1096
1097 /*
1098 * Process an FTP request
1099 */
1100 FILE *
ftp_request(struct url * url,const char * op,struct url_stat * us,struct url * purl,const char * flags)1101 ftp_request(struct url *url, const char *op, struct url_stat *us,
1102 struct url *purl, const char *flags)
1103 {
1104 conn_t *conn;
1105 int oflag;
1106
1107 /* check if we should use HTTP instead */
1108 if (purl && (strcmp(purl->scheme, SCHEME_HTTP) == 0 ||
1109 strcmp(purl->scheme, SCHEME_HTTPS) == 0)) {
1110 if (strcmp(op, "STAT") == 0)
1111 return (http_request(url, "HEAD", us, purl, flags));
1112 else if (strcmp(op, "RETR") == 0)
1113 return (http_request(url, "GET", us, purl, flags));
1114 /*
1115 * Our HTTP code doesn't support PUT requests yet, so try
1116 * a direct connection.
1117 */
1118 }
1119
1120 /* connect to server */
1121 conn = ftp_cached_connect(url, purl, flags);
1122 if (purl)
1123 fetchFreeURL(purl);
1124 if (conn == NULL)
1125 return (NULL);
1126
1127 /* change directory */
1128 if (ftp_cwd(conn, url->doc) == -1)
1129 goto errsock;
1130
1131 /* stat file */
1132 if (us && ftp_stat(conn, url->doc, us) == -1
1133 && fetchLastErrCode != FETCH_PROTO
1134 && fetchLastErrCode != FETCH_UNAVAIL)
1135 goto errsock;
1136
1137 /* just a stat */
1138 if (strcmp(op, "STAT") == 0) {
1139 --conn->ref;
1140 ftp_disconnect(conn);
1141 return (FILE *)1; /* bogus return value */
1142 }
1143 if (strcmp(op, "STOR") == 0 || strcmp(op, "APPE") == 0)
1144 oflag = O_WRONLY;
1145 else
1146 oflag = O_RDONLY;
1147
1148 /* initiate the transfer */
1149 return (ftp_transfer(conn, op, url->doc, oflag, url->offset, flags));
1150
1151 errsock:
1152 ftp_disconnect(conn);
1153 return (NULL);
1154 }
1155
1156 /*
1157 * Get and stat file
1158 */
1159 FILE *
fetchXGetFTP(struct url * url,struct url_stat * us,const char * flags)1160 fetchXGetFTP(struct url *url, struct url_stat *us, const char *flags)
1161 {
1162 return (ftp_request(url, "RETR", us, ftp_get_proxy(url, flags), flags));
1163 }
1164
1165 /*
1166 * Get file
1167 */
1168 FILE *
fetchGetFTP(struct url * url,const char * flags)1169 fetchGetFTP(struct url *url, const char *flags)
1170 {
1171 return (fetchXGetFTP(url, NULL, flags));
1172 }
1173
1174 /*
1175 * Put file
1176 */
1177 FILE *
fetchPutFTP(struct url * url,const char * flags)1178 fetchPutFTP(struct url *url, const char *flags)
1179 {
1180 return (ftp_request(url, CHECK_FLAG('a') ? "APPE" : "STOR", NULL,
1181 ftp_get_proxy(url, flags), flags));
1182 }
1183
1184 /*
1185 * Get file stats
1186 */
1187 int
fetchStatFTP(struct url * url,struct url_stat * us,const char * flags)1188 fetchStatFTP(struct url *url, struct url_stat *us, const char *flags)
1189 {
1190 FILE *f;
1191
1192 f = ftp_request(url, "STAT", us, ftp_get_proxy(url, flags), flags);
1193 if (f == NULL)
1194 return (-1);
1195 /*
1196 * When op is "STAT", ftp_request() will return either NULL or
1197 * (FILE *)1, never a valid FILE *, so we mustn't fclose(f) before
1198 * returning, as it would cause a segfault.
1199 */
1200 return (0);
1201 }
1202
1203 /*
1204 * List a directory
1205 */
1206 struct url_ent *
fetchListFTP(struct url * url __unused,const char * flags __unused)1207 fetchListFTP(struct url *url __unused, const char *flags __unused)
1208 {
1209 warnx("fetchListFTP(): not implemented");
1210 return (NULL);
1211 }
1212