No empty .Rs/.Re
[netbsd-mini2440.git] / share / examples / refuse / ian / libfetch / ftp.c
bloba49203a61336ae27666483e7b657a656df29ce95
1 /*-
2 * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "free2net.h"
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: src/lib/libfetch/ftp.c,v 1.91.2.2 2007/05/16 07:04:02 njl Exp $");
35 * Portions of this code were taken from or based on ftpio.c:
37 * ----------------------------------------------------------------------------
38 * "THE BEER-WARE LICENSE" (Revision 42):
39 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
40 * can do whatever you want with this stuff. If we meet some day, and you think
41 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
42 * ----------------------------------------------------------------------------
44 * Major Changelog:
46 * Dag-Erling Coïdan Smørgrav
47 * 9 Jun 1998
49 * Incorporated into libfetch
51 * Jordan K. Hubbard
52 * 17 Jan 1996
54 * Turned inside out. Now returns xfers as new file ids, not as a special
55 * `state' of FTP_t
57 * $ftpioId: ftpio.c,v 1.30 1998/04/11 07:28:53 phk Exp $
61 #include <sys/param.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
65 #include <ctype.h>
66 #include <err.h>
67 #include <errno.h>
68 #include <fcntl.h>
69 #include <netdb.h>
70 #include <stdarg.h>
71 #include <stdint.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <time.h>
76 #include <unistd.h>
78 #include "fetch.h"
79 #include "common.h"
80 #include "ftperr.h"
82 #define FTP_ANONYMOUS_USER "anonymous"
84 #define FTP_CONNECTION_ALREADY_OPEN 125
85 #define FTP_OPEN_DATA_CONNECTION 150
86 #define FTP_OK 200
87 #define FTP_FILE_STATUS 213
88 #define FTP_SERVICE_READY 220
89 #define FTP_TRANSFER_COMPLETE 226
90 #define FTP_PASSIVE_MODE 227
91 #define FTP_LPASSIVE_MODE 228
92 #define FTP_EPASSIVE_MODE 229
93 #define FTP_LOGGED_IN 230
94 #define FTP_FILE_ACTION_OK 250
95 #define FTP_DIRECTORY_CREATED 257 /* multiple meanings */
96 #define FTP_FILE_CREATED 257 /* multiple meanings */
97 #define FTP_WORKING_DIRECTORY 257 /* multiple meanings */
98 #define FTP_NEED_PASSWORD 331
99 #define FTP_NEED_ACCOUNT 332
100 #define FTP_FILE_OK 350
101 #define FTP_SYNTAX_ERROR 500
102 #define FTP_PROTOCOL_ERROR 999
104 static struct url cached_host;
105 static conn_t *cached_connection;
107 #define isftpreply(foo) (isdigit((unsigned)foo[0]) && \
108 isdigit((unsigned)foo[1]) && \
109 isdigit((unsigned)foo[2]) && \
110 (foo[3] == ' ' || foo[3] == '\0'))
111 #define isftpinfo(foo) (isdigit((unsigned)foo[0]) && \
112 isdigit((unsigned)foo[1]) && \
113 isdigit((unsigned)foo[2]) && foo[3] == '-')
116 * Translate IPv4 mapped IPv6 address to IPv4 address
118 static void
119 unmappedaddr(struct sockaddr_in6 *sin6)
121 struct sockaddr_in *sin4;
122 u_int32_t addr;
123 int port;
125 if (sin6->sin6_family != AF_INET6 ||
126 !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
127 return;
128 sin4 = (struct sockaddr_in *)(void *)sin6;
129 addr = *(u_int32_t *)(void *)&sin6->sin6_addr.s6_addr[12];
130 port = sin6->sin6_port;
131 memset(sin4, 0, sizeof(struct sockaddr_in));
132 sin4->sin_addr.s_addr = addr;
133 sin4->sin_port = port;
134 sin4->sin_family = AF_INET;
135 sin4->sin_len = sizeof(struct sockaddr_in);
139 * Get server response
141 static int
142 _ftp_chkerr(conn_t *conn)
144 if (_fetch_getln(conn) == -1) {
145 _fetch_syserr();
146 return (-1);
148 if (isftpinfo(conn->buf)) {
149 while (conn->buflen && !isftpreply(conn->buf)) {
150 if (_fetch_getln(conn) == -1) {
151 _fetch_syserr();
152 return (-1);
157 while (conn->buflen && isspace((unsigned)conn->buf[conn->buflen - 1]))
158 conn->buflen--;
159 conn->buf[conn->buflen] = '\0';
161 if (!isftpreply(conn->buf)) {
162 _ftp_seterr(FTP_PROTOCOL_ERROR);
163 return (-1);
166 conn->err = (conn->buf[0] - '0') * 100
167 + (conn->buf[1] - '0') * 10
168 + (conn->buf[2] - '0');
170 return (conn->err);
174 * Send a command and check reply
176 static int
177 _ftp_cmd(conn_t *conn, const char *fmt, ...)
179 va_list ap;
180 size_t len;
181 char *msg;
182 int r;
184 va_start(ap, fmt);
185 len = vasprintf(&msg, fmt, ap);
186 va_end(ap);
188 if (msg == NULL) {
189 errno = ENOMEM;
190 _fetch_syserr();
191 return (-1);
194 r = _fetch_putln(conn, msg, len);
195 free(msg);
197 if (r == -1) {
198 _fetch_syserr();
199 return (-1);
202 return (_ftp_chkerr(conn));
206 * Return a pointer to the filename part of a path
208 static const char *
209 _ftp_filename(const char *file, int *len, int *type)
211 const char *s;
213 if ((s = strrchr(file, '/')) == NULL)
214 s = file;
215 else
216 s = s + 1;
217 *len = strlen(s);
218 if (*len > 7 && strncmp(s + *len - 7, ";type=", 6) == 0) {
219 *type = s[*len - 1];
220 *len -= 7;
221 } else {
222 *type = '\0';
224 return (s);
228 * Get current working directory from the reply to a CWD, PWD or CDUP
229 * command.
231 static int
232 _ftp_pwd(conn_t *conn, char *pwd, size_t pwdlen)
234 char *src, *dst, *end;
235 int q;
237 if (conn->err != FTP_WORKING_DIRECTORY &&
238 conn->err != FTP_FILE_ACTION_OK)
239 return (FTP_PROTOCOL_ERROR);
240 end = conn->buf + conn->buflen;
241 src = conn->buf + 4;
242 if (src >= end || *src++ != '"')
243 return (FTP_PROTOCOL_ERROR);
244 for (q = 0, dst = pwd; src < end && pwdlen--; ++src) {
245 if (!q && *src == '"')
246 q = 1;
247 else if (q && *src != '"')
248 break;
249 else if (q)
250 *dst++ = '"', q = 0;
251 else
252 *dst++ = *src;
254 if (!pwdlen)
255 return (FTP_PROTOCOL_ERROR);
256 *dst = '\0';
257 #if 0
258 DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd));
259 #endif
260 return (FTP_OK);
264 * Change working directory to the directory that contains the specified
265 * file.
267 static int
268 _ftp_cwd(conn_t *conn, const char *file)
270 const char *beg, *end;
271 char pwd[PATH_MAX];
272 int e, i, len;
274 /* If no slashes in name, no need to change dirs. */
275 if ((end = strrchr(file, '/')) == NULL)
276 return (0);
277 if ((e = _ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||
278 (e = _ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) {
279 _ftp_seterr(e);
280 return (-1);
282 for (;;) {
283 len = strlen(pwd);
285 /* Look for a common prefix between PWD and dir to fetch. */
286 for (i = 0; i <= len && i <= end - file; ++i)
287 if (pwd[i] != file[i])
288 break;
289 #if 0
290 DEBUG(fprintf(stderr, "have: [%.*s|%s]\n", i, pwd, pwd + i));
291 DEBUG(fprintf(stderr, "want: [%.*s|%s]\n", i, file, file + i));
292 #endif
293 /* Keep going up a dir until we have a matching prefix. */
294 if (pwd[i] == '\0' && (file[i - 1] == '/' || file[i] == '/'))
295 break;
296 if ((e = _ftp_cmd(conn, "CDUP")) != FTP_FILE_ACTION_OK ||
297 (e = _ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||
298 (e = _ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) {
299 _ftp_seterr(e);
300 return (-1);
304 #ifdef FTP_COMBINE_CWDS
305 /* Skip leading slashes, even "////". */
306 for (beg = file + i; beg < end && *beg == '/'; ++beg, ++i)
307 /* nothing */ ;
309 /* If there is no trailing dir, we're already there. */
310 if (beg >= end)
311 return (0);
313 /* Change to the directory all in one chunk (e.g., foo/bar/baz). */
314 e = _ftp_cmd(conn, "CWD %.*s", (int)(end - beg), beg);
315 if (e == FTP_FILE_ACTION_OK)
316 return (0);
317 #endif /* FTP_COMBINE_CWDS */
319 /* That didn't work so go back to legacy behavior (multiple CWDs). */
320 for (beg = file + i; beg < end; beg = file + i + 1) {
321 while (*beg == '/')
322 ++beg, ++i;
323 for (++i; file + i < end && file[i] != '/'; ++i)
324 /* nothing */ ;
325 e = _ftp_cmd(conn, "CWD %.*s", file + i - beg, beg);
326 if (e != FTP_FILE_ACTION_OK) {
327 _ftp_seterr(e);
328 return (-1);
331 return (0);
335 * Set transfer mode and data type
337 static int
338 _ftp_mode_type(conn_t *conn, int mode, int type)
340 int e;
342 switch (mode) {
343 case 0:
344 case 's':
345 mode = 'S';
346 /* FALLTHROUGH */
347 case 'S':
348 break;
349 default:
350 return (FTP_PROTOCOL_ERROR);
352 if ((e = _ftp_cmd(conn, "MODE %c", mode)) != FTP_OK) {
353 if (mode == 'S') {
355 * Stream mode is supposed to be the default - so
356 * much so that some servers not only do not
357 * support any other mode, but do not support the
358 * MODE command at all.
360 * If "MODE S" fails, it is unlikely that we
361 * previously succeeded in setting a different
362 * mode. Therefore, we simply hope that the
363 * server is already in the correct mode, and
364 * silently ignore the failure.
366 } else {
367 return (e);
371 switch (type) {
372 case 0:
373 case 'i':
374 type = 'I';
375 /* FALLTHROUGH */
376 case 'I':
377 break;
378 case 'a':
379 type = 'A';
380 /* FALLTHROUGH */
381 case 'A':
382 break;
383 case 'd':
384 type = 'D';
385 /* FALLTHROUGH */
386 case 'D':
387 /* can't handle yet */
388 default:
389 return (FTP_PROTOCOL_ERROR);
391 if ((e = _ftp_cmd(conn, "TYPE %c", type)) != FTP_OK)
392 return (e);
394 return (FTP_OK);
398 * Request and parse file stats
400 static int
401 _ftp_stat(conn_t *conn, const char *file, struct url_stat *us)
403 char *ln;
404 const char *filename;
405 int filenamelen, type;
406 struct tm tm;
407 time_t t;
408 int e;
410 us->size = -1;
411 us->atime = us->mtime = 0;
413 filename = _ftp_filename(file, &filenamelen, &type);
415 if ((e = _ftp_mode_type(conn, 0, type)) != FTP_OK) {
416 _ftp_seterr(e);
417 return (-1);
420 e = _ftp_cmd(conn, "SIZE %.*s", filenamelen, filename);
421 if (e != FTP_FILE_STATUS) {
422 _ftp_seterr(e);
423 return (-1);
425 for (ln = conn->buf + 4; *ln && isspace((unsigned)*ln); ln++)
426 /* nothing */ ;
427 for (us->size = 0; *ln && isdigit((unsigned)*ln); ln++)
428 us->size = us->size * 10 + *ln - '0';
429 if (*ln && !isspace((unsigned)*ln)) {
430 _ftp_seterr(FTP_PROTOCOL_ERROR);
431 us->size = -1;
432 return (-1);
434 if (us->size == 0)
435 us->size = -1;
436 DEBUG(fprintf(stderr, "size: [%lld]\n", (long long)us->size));
438 e = _ftp_cmd(conn, "MDTM %.*s", filenamelen, filename);
439 if (e != FTP_FILE_STATUS) {
440 _ftp_seterr(e);
441 return (-1);
443 for (ln = conn->buf + 4; *ln && isspace((unsigned)*ln); ln++)
444 /* nothing */ ;
445 switch (strspn(ln, "0123456789")) {
446 case 14:
447 break;
448 case 15:
449 ln++;
450 ln[0] = '2';
451 ln[1] = '0';
452 break;
453 default:
454 _ftp_seterr(FTP_PROTOCOL_ERROR);
455 return (-1);
457 if (sscanf(ln, "%04d%02d%02d%02d%02d%02d",
458 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
459 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
460 _ftp_seterr(FTP_PROTOCOL_ERROR);
461 return (-1);
463 tm.tm_mon--;
464 tm.tm_year -= 1900;
465 tm.tm_isdst = -1;
466 t = timegm(&tm);
467 if (t == (time_t)-1)
468 t = time(NULL);
469 us->mtime = t;
470 us->atime = t;
471 DEBUG(fprintf(stderr,
472 "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);
479 * I/O functions for FTP
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 */
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 *);
494 static int
495 _ftp_readfn(void *v, char *buf, int len)
497 struct ftpio *io;
498 int r;
500 io = (struct ftpio *)v;
501 if (io == NULL) {
502 errno = EBADF;
503 return (-1);
505 if (io->cconn == NULL || io->dconn == NULL || io->dir == O_WRONLY) {
506 errno = EBADF;
507 return (-1);
509 if (io->err) {
510 errno = io->err;
511 return (-1);
513 if (io->eof)
514 return (0);
515 r = _fetch_read(io->dconn, buf, (unsigned)len);
516 if (r > 0)
517 return (r);
518 if (r == 0) {
519 io->eof = 1;
520 return (0);
522 if (errno != EINTR)
523 io->err = errno;
524 return (-1);
527 static int
528 _ftp_writefn(void *v, const char *buf, int len)
530 struct ftpio *io;
531 int w;
533 io = (struct ftpio *)v;
534 if (io == NULL) {
535 errno = EBADF;
536 return (-1);
538 if (io->cconn == NULL || io->dconn == NULL || io->dir == O_RDONLY) {
539 errno = EBADF;
540 return (-1);
542 if (io->err) {
543 errno = io->err;
544 return (-1);
546 w = _fetch_write(io->dconn, buf, (unsigned)len);
547 if (w >= 0)
548 return (w);
549 if (errno != EINTR)
550 io->err = errno;
551 return (-1);
554 /* ARGSUSED1 */
555 static fpos_t
556 _ftp_seekfn(void *v, fpos_t pos __unused, int whence __unused)
558 struct ftpio *io;
560 io = (struct ftpio *)v;
561 if (io == NULL) {
562 errno = EBADF;
563 return (-1);
565 errno = ESPIPE;
566 return (-1);
569 static int
570 _ftp_closefn(void *v)
572 struct ftpio *io;
573 int r;
575 io = (struct ftpio *)v;
576 if (io == NULL) {
577 errno = EBADF;
578 return (-1);
580 if (io->dir == -1)
581 return (0);
582 if (io->cconn == NULL || io->dconn == NULL) {
583 errno = EBADF;
584 return (-1);
586 _fetch_close(io->dconn);
587 io->dir = -1;
588 io->dconn = NULL;
589 DEBUG(fprintf(stderr, "Waiting for final status\n"));
590 r = _ftp_chkerr(io->cconn);
591 if (io->cconn == cached_connection && io->cconn->ref == 1)
592 cached_connection = NULL;
593 _fetch_close(io->cconn);
594 free(io);
595 return (r == FTP_TRANSFER_COMPLETE) ? 0 : -1;
598 static FILE *
599 _ftp_setup(conn_t *cconn, conn_t *dconn, int mode)
601 struct ftpio *io;
602 FILE *f;
604 if (cconn == NULL || dconn == NULL)
605 return (NULL);
606 if ((io = malloc(sizeof(*io))) == NULL)
607 return (NULL);
608 io->cconn = cconn;
609 io->dconn = dconn;
610 io->dir = mode;
611 io->eof = io->err = 0;
612 f = funopen(io, _ftp_readfn, _ftp_writefn, _ftp_seekfn, _ftp_closefn);
613 if (f == NULL)
614 free(io);
615 return (f);
619 * Transfer file
621 static FILE *
622 _ftp_transfer(conn_t *conn, const char *oper, const char *file,
623 int mode, off_t offset, const char *flags)
625 struct sockaddr_storage sa;
626 struct sockaddr_in6 *sin6;
627 struct sockaddr_in *sin4;
628 const char *bindaddr;
629 const char *filename;
630 int filenamelen, type;
631 int low, pasv, verbose;
632 int e, sd = -1;
633 socklen_t l;
634 char *s;
635 FILE *df;
637 /* check flags */
638 low = CHECK_FLAG('l');
639 pasv = CHECK_FLAG('p');
640 verbose = CHECK_FLAG('v');
642 /* passive mode */
643 if (!pasv)
644 pasv = ((s = getenv("FTP_PASSIVE_MODE")) != NULL &&
645 strncasecmp(s, "no", 2) != 0);
647 /* isolate filename */
648 filename = _ftp_filename(file, &filenamelen, &type);
650 /* set transfer mode and data type */
651 if ((e = _ftp_mode_type(conn, 0, type)) != FTP_OK)
652 goto ouch;
654 /* find our own address, bind, and listen */
655 l = sizeof(sa);
656 if (getsockname(conn->sd, (struct sockaddr *)(void *)&sa, &l) == -1)
657 goto sysouch;
658 if (sa.ss_family == AF_INET6)
659 unmappedaddr((struct sockaddr_in6 *)(void *)&sa);
661 /* open data socket */
662 if ((sd = socket(sa.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
663 _fetch_syserr();
664 return (NULL);
667 if (pasv) {
668 u_char addr[64];
669 char *ln, *p;
670 unsigned int i;
671 int port;
673 /* send PASV command */
674 if (verbose)
675 _fetch_info("setting passive mode");
676 switch (sa.ss_family) {
677 case AF_INET:
678 if ((e = _ftp_cmd(conn, "PASV")) != FTP_PASSIVE_MODE)
679 goto ouch;
680 break;
681 case AF_INET6:
682 if ((e = _ftp_cmd(conn, "EPSV")) != FTP_EPASSIVE_MODE) {
683 if (e == -1)
684 goto ouch;
685 if ((e = _ftp_cmd(conn, "LPSV")) !=
686 FTP_LPASSIVE_MODE)
687 goto ouch;
689 break;
690 default:
691 e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
692 goto ouch;
696 * Find address and port number. The reply to the PASV command
697 * is IMHO the one and only weak point in the FTP protocol.
699 ln = conn->buf;
700 switch (e) {
701 case FTP_PASSIVE_MODE:
702 case FTP_LPASSIVE_MODE:
703 for (p = ln + 3; *p && !isdigit((unsigned)*p); p++)
704 /* nothing */ ;
705 if (!*p) {
706 e = FTP_PROTOCOL_ERROR;
707 goto ouch;
709 l = (e == FTP_PASSIVE_MODE ? 6 : 21);
710 for (i = 0; *p && i < l; i++, p++)
711 addr[i] = (unsigned char) strtol(p, &p, 10);
712 if (i < l) {
713 e = FTP_PROTOCOL_ERROR;
714 goto ouch;
716 break;
717 case FTP_EPASSIVE_MODE:
718 for (p = ln + 3; *p && *p != '('; p++)
719 /* nothing */ ;
720 if (!*p) {
721 e = FTP_PROTOCOL_ERROR;
722 goto ouch;
724 ++p;
725 if (sscanf(p, "%c%c%c%d%c", &addr[0], &addr[1], &addr[2],
726 &port, &addr[3]) != 5 ||
727 addr[0] != addr[1] ||
728 addr[0] != addr[2] || addr[0] != addr[3]) {
729 e = FTP_PROTOCOL_ERROR;
730 goto ouch;
732 break;
735 /* seek to required offset */
736 if (offset)
737 if (_ftp_cmd(conn, "REST %lu", (u_long)offset) != FTP_FILE_OK)
738 goto sysouch;
740 /* construct sockaddr for data socket */
741 l = sizeof(sa);
742 if (getpeername(conn->sd, (struct sockaddr *)(void *)&sa, &l) == -1)
743 goto sysouch;
744 if (sa.ss_family == AF_INET6)
745 unmappedaddr((struct sockaddr_in6 *)(void *)&sa);
746 switch (sa.ss_family) {
747 case AF_INET6:
748 sin6 = (struct sockaddr_in6 *)(void *)&sa;
749 if (e == FTP_EPASSIVE_MODE)
750 sin6->sin6_port = htons(port);
751 else {
752 bcopy(addr + 2, (char *)(void *)&sin6->sin6_addr, 16);
753 bcopy(addr + 19, (char *)(void *)&sin6->sin6_port, 2);
755 break;
756 case AF_INET:
757 sin4 = (struct sockaddr_in *)(void *)&sa;
758 if (e == FTP_EPASSIVE_MODE)
759 sin4->sin_port = htons(port);
760 else {
761 bcopy(addr, (char *)(void *)&sin4->sin_addr, 4);
762 bcopy(addr + 4, (char *)(void *)&sin4->sin_port, 2);
764 break;
765 default:
766 e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
767 break;
770 /* connect to data port */
771 if (verbose)
772 _fetch_info("opening data connection");
773 bindaddr = getenv("FETCH_BIND_ADDRESS");
774 if (bindaddr != NULL && *bindaddr != '\0' &&
775 _fetch_bind(sd, sa.ss_family, bindaddr) != 0)
776 goto sysouch;
777 if (connect(sd, (struct sockaddr *)(void *)&sa, (unsigned)sa.ss_len) == -1)
778 goto sysouch;
780 /* make the server initiate the transfer */
781 if (verbose)
782 _fetch_info("initiating transfer");
783 e = _ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
784 if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
785 goto ouch;
787 } else {
788 u_int32_t a;
789 u_short p;
790 int arg, d;
791 char *ap;
792 char hname[INET6_ADDRSTRLEN];
794 switch (sa.ss_family) {
795 case AF_INET6:
796 ((struct sockaddr_in6 *)(void *)&sa)->sin6_port = 0;
797 #ifdef IPV6_PORTRANGE
798 arg = low ? IPV6_PORTRANGE_DEFAULT : IPV6_PORTRANGE_HIGH;
799 if (setsockopt(sd, IPPROTO_IPV6, IPV6_PORTRANGE,
800 (char *)(void *)&arg, sizeof(arg)) == -1)
801 goto sysouch;
802 #endif
803 break;
804 case AF_INET:
805 ((struct sockaddr_in *)(void *)&sa)->sin_port = 0;
806 arg = low ? IP_PORTRANGE_DEFAULT : IP_PORTRANGE_HIGH;
807 if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE,
808 (char *)(void *)&arg, sizeof(arg)) == -1)
809 goto sysouch;
810 break;
812 if (verbose)
813 _fetch_info("binding data socket");
814 if (bind(sd, (struct sockaddr *)(void *)&sa, (unsigned) sa.ss_len) == -1)
815 goto sysouch;
816 if (listen(sd, 1) == -1)
817 goto sysouch;
819 /* find what port we're on and tell the server */
820 if (getsockname(sd, (struct sockaddr *)(void *)&sa, &l) == -1)
821 goto sysouch;
822 switch (sa.ss_family) {
823 case AF_INET:
824 sin4 = (struct sockaddr_in *)(void *)&sa;
825 a = ntohl(sin4->sin_addr.s_addr);
826 p = ntohs(sin4->sin_port);
827 e = _ftp_cmd(conn, "PORT %d,%d,%d,%d,%d,%d",
828 (a >> 24) & 0xff, (a >> 16) & 0xff,
829 (a >> 8) & 0xff, a & 0xff,
830 ((unsigned)p >> 8) & 0xff, p & 0xff);
831 break;
832 case AF_INET6:
833 #define UC(b) (((int)b)&0xff)
834 e = -1;
835 sin6 = (struct sockaddr_in6 *)(void *)&sa;
836 sin6->sin6_scope_id = 0;
837 if (getnameinfo((struct sockaddr *)(void *)&sa,
838 (unsigned)sa.ss_len,
839 hname, sizeof(hname),
840 NULL, 0, NI_NUMERICHOST) == 0) {
841 e = _ftp_cmd(conn, "EPRT |%d|%s|%d|", 2, hname,
842 htons(sin6->sin6_port));
843 if (e == -1)
844 goto ouch;
846 if (e != FTP_OK) {
847 ap = (char *)(void *)&sin6->sin6_addr;
848 e = _ftp_cmd(conn,
849 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
850 6, 16,
851 UC(ap[0]), UC(ap[1]), UC(ap[2]), UC(ap[3]),
852 UC(ap[4]), UC(ap[5]), UC(ap[6]), UC(ap[7]),
853 UC(ap[8]), UC(ap[9]), UC(ap[10]), UC(ap[11]),
854 UC(ap[12]), UC(ap[13]), UC(ap[14]), UC(ap[15]),
856 ((unsigned)ntohs(sin6->sin6_port) >> 8) & 0xff,
857 ntohs(sin6->sin6_port) & 0xff);
859 break;
860 default:
861 e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
862 goto ouch;
864 if (e != FTP_OK)
865 goto ouch;
867 /* seek to required offset */
868 if (offset)
869 if (_ftp_cmd(conn, "REST %ju", (uintmax_t)offset) != FTP_FILE_OK)
870 goto sysouch;
872 /* make the server initiate the transfer */
873 if (verbose)
874 _fetch_info("initiating transfer");
875 e = _ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
876 if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
877 goto ouch;
879 /* accept the incoming connection and go to town */
880 if ((d = accept(sd, NULL, NULL)) == -1)
881 goto sysouch;
882 close(sd);
883 sd = d;
886 if ((df = _ftp_setup(conn, _fetch_reopen(sd), mode)) == NULL)
887 goto sysouch;
888 return (df);
890 sysouch:
891 _fetch_syserr();
892 if (sd >= 0)
893 close(sd);
894 return (NULL);
896 ouch:
897 if (e != -1)
898 _ftp_seterr(e);
899 if (sd >= 0)
900 close(sd);
901 return (NULL);
905 * Authenticate
907 static int
908 _ftp_authenticate(conn_t *conn, struct url *url, struct url *purl)
910 const char *user, *pwd, *logname;
911 char pbuf[MAXHOSTNAMELEN + MAXLOGNAME + 1];
912 int e, len;
914 /* XXX FTP_AUTH, and maybe .netrc */
916 /* send user name and password */
917 if (url->user[0] == '\0')
918 _fetch_netrc_auth(url);
919 user = url->user;
920 if (*user == '\0')
921 user = getenv("FTP_LOGIN");
922 if (user == NULL || *user == '\0')
923 user = FTP_ANONYMOUS_USER;
924 if (purl && url->port == _fetch_default_port(url->scheme))
925 e = _ftp_cmd(conn, "USER %s@%s", user, url->host);
926 else if (purl)
927 e = _ftp_cmd(conn, "USER %s@%s@%d", user, url->host, url->port);
928 else
929 e = _ftp_cmd(conn, "USER %s", user);
931 /* did the server request a password? */
932 if (e == FTP_NEED_PASSWORD) {
933 pwd = url->pwd;
934 if (*pwd == '\0')
935 pwd = getenv("FTP_PASSWORD");
936 if (pwd == NULL || *pwd == '\0') {
937 if ((logname = getlogin()) == 0)
938 logname = FTP_ANONYMOUS_USER;
939 if ((len = snprintf(pbuf, MAXLOGNAME + 1, "%s@", logname)) < 0)
940 len = 0;
941 else if (len > MAXLOGNAME)
942 len = MAXLOGNAME;
943 gethostname(pbuf + len, sizeof(pbuf) - len);
944 pwd = pbuf;
946 e = _ftp_cmd(conn, "PASS %s", pwd);
949 return (e);
953 * Log on to FTP server
955 static conn_t *
956 _ftp_connect(struct url *url, struct url *purl, const char *flags)
958 conn_t *conn;
959 int e, direct, verbose;
960 #ifdef INET6
961 int af = AF_UNSPEC;
962 #else
963 int af = AF_INET;
964 #endif
966 direct = CHECK_FLAG('d');
967 verbose = CHECK_FLAG('v');
968 if (CHECK_FLAG('4'))
969 af = AF_INET;
970 else if (CHECK_FLAG('6'))
971 af = AF_INET6;
973 if (direct)
974 purl = NULL;
976 /* check for proxy */
977 if (purl) {
978 /* XXX proxy authentication! */
979 conn = _fetch_connect(purl->host, purl->port, af, verbose);
980 } else {
981 /* no proxy, go straight to target */
982 conn = _fetch_connect(url->host, url->port, af, verbose);
983 purl = NULL;
986 /* check connection */
987 if (conn == NULL)
988 /* _fetch_connect() has already set an error code */
989 return (NULL);
991 /* expect welcome message */
992 if ((e = _ftp_chkerr(conn)) != FTP_SERVICE_READY)
993 goto fouch;
995 /* authenticate */
996 if ((e = _ftp_authenticate(conn, url, purl)) != FTP_LOGGED_IN)
997 goto fouch;
999 /* TODO: Request extended features supported, if any (RFC 3659). */
1001 /* done */
1002 return (conn);
1004 fouch:
1005 if (e != -1)
1006 _ftp_seterr(e);
1007 _fetch_close(conn);
1008 return (NULL);
1012 * Disconnect from server
1014 static void
1015 _ftp_disconnect(conn_t *conn)
1017 (void)_ftp_cmd(conn, "QUIT");
1018 if (conn == cached_connection && conn->ref == 1)
1019 cached_connection = NULL;
1020 _fetch_close(conn);
1024 * Check if we're already connected
1026 static int
1027 _ftp_isconnected(struct url *url)
1029 return (cached_connection
1030 && (strcmp(url->host, cached_host.host) == 0)
1031 && (strcmp(url->user, cached_host.user) == 0)
1032 && (strcmp(url->pwd, cached_host.pwd) == 0)
1033 && (url->port == cached_host.port));
1037 * Check the cache, reconnect if no luck
1039 static conn_t *
1040 _ftp_cached_connect(struct url *url, struct url *purl, const char *flags)
1042 conn_t *conn;
1043 int e;
1045 /* set default port */
1046 if (!url->port)
1047 url->port = _fetch_default_port(url->scheme);
1049 /* try to use previously cached connection */
1050 if (_ftp_isconnected(url)) {
1051 e = _ftp_cmd(cached_connection, "NOOP");
1052 if (e == FTP_OK || e == FTP_SYNTAX_ERROR)
1053 return (_fetch_ref(cached_connection));
1056 /* connect to server */
1057 if ((conn = _ftp_connect(url, purl, flags)) == NULL)
1058 return (NULL);
1059 if (cached_connection)
1060 _ftp_disconnect(cached_connection);
1061 cached_connection = _fetch_ref(conn);
1062 memcpy(&cached_host, url, sizeof(*url));
1063 return (conn);
1067 * Check the proxy settings
1069 static struct url *
1070 _ftp_get_proxy(const char *flags)
1072 struct url *purl;
1073 char *p;
1075 if (flags != NULL && strchr(flags, 'd') != NULL)
1076 return (NULL);
1077 if (((p = getenv("FTP_PROXY")) || (p = getenv("ftp_proxy")) ||
1078 (p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
1079 *p && (purl = fetchParseURL(p)) != NULL) {
1080 if (!*purl->scheme) {
1081 if (getenv("FTP_PROXY") || getenv("ftp_proxy"))
1082 strcpy(purl->scheme, SCHEME_FTP);
1083 else
1084 strcpy(purl->scheme, SCHEME_HTTP);
1086 if (!purl->port)
1087 purl->port = _fetch_default_proxy_port(purl->scheme);
1088 if (strcasecmp(purl->scheme, SCHEME_FTP) == 0 ||
1089 strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
1090 return (purl);
1091 fetchFreeURL(purl);
1093 return (NULL);
1097 * Process an FTP request
1099 FILE *
1100 _ftp_request(struct url *url, const char *op, struct url_stat *us,
1101 struct url *purl, const char *flags)
1103 conn_t *conn;
1104 int oflag;
1106 /* check if we should use HTTP instead */
1107 if (purl && strcasecmp(purl->scheme, SCHEME_HTTP) == 0) {
1108 if (strcmp(op, "STAT") == 0)
1109 return (_http_request(url, "HEAD", us, purl, flags));
1110 else if (strcmp(op, "RETR") == 0)
1111 return (_http_request(url, "GET", us, purl, flags));
1113 * Our HTTP code doesn't support PUT requests yet, so try
1114 * a direct connection.
1118 /* connect to server */
1119 conn = _ftp_cached_connect(url, purl, flags);
1120 if (purl)
1121 fetchFreeURL(purl);
1122 if (conn == NULL)
1123 return (NULL);
1125 /* change directory */
1126 if (_ftp_cwd(conn, url->doc) == -1)
1127 return (NULL);
1129 /* stat file */
1130 if (us && _ftp_stat(conn, url->doc, us) == -1
1131 && fetchLastErrCode != FETCH_PROTO
1132 && fetchLastErrCode != FETCH_UNAVAIL)
1133 return (NULL);
1135 /* just a stat */
1136 if (strcmp(op, "STAT") == 0)
1137 return (FILE *)1; /* bogus return value */
1138 if (strcmp(op, "STOR") == 0 || strcmp(op, "APPE") == 0)
1139 oflag = O_WRONLY;
1140 else
1141 oflag = O_RDONLY;
1143 /* initiate the transfer */
1144 return (_ftp_transfer(conn, op, url->doc, oflag, url->offset, flags));
1148 * Get and stat file
1150 FILE *
1151 fetchXGetFTP(struct url *url, struct url_stat *us, const char *flags)
1153 return (_ftp_request(url, "RETR", us, _ftp_get_proxy(flags), flags));
1157 * Get file
1159 FILE *
1160 fetchGetFTP(struct url *url, const char *flags)
1162 return (fetchXGetFTP(url, NULL, flags));
1166 * Put file
1168 FILE *
1169 fetchPutFTP(struct url *url, const char *flags)
1172 return (_ftp_request(url, CHECK_FLAG('a') ? "APPE" : "STOR", NULL,
1173 _ftp_get_proxy(flags), flags));
1177 * Get file stats
1180 fetchStatFTP(struct url *url, struct url_stat *us, const char *flags)
1182 FILE *f;
1184 f = _ftp_request(url, "STAT", us, _ftp_get_proxy(flags), flags);
1185 if (f == NULL)
1186 return (-1);
1187 return (0);
1191 * List a directory
1193 /* ARGSUSED0 */
1194 struct url_ent *
1195 fetchListFTP(struct url *url __unused, const char *flags __unused)
1197 warnx("fetchListFTP(): not implemented");
1198 return (NULL);