xref: /freebsd/lib/libfetch/ftp.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 
31 #include <sys/cdefs.h>
32 /*
33  * Portions of this code were taken from or based on ftpio.c:
34  *
35  * ----------------------------------------------------------------------------
36  * "THE BEER-WARE LICENSE" (Revision 42):
37  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
38  * can do whatever you want with this stuff. If we meet some day, and you think
39  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
40  * ----------------------------------------------------------------------------
41  *
42  * Major Changelog:
43  *
44  * Dag-Erling Smørgrav
45  * 9 Jun 1998
46  *
47  * Incorporated into libfetch
48  *
49  * Jordan K. Hubbard
50  * 17 Jan 1996
51  *
52  * Turned inside out. Now returns xfers as new file ids, not as a special
53  * `state' of FTP_t
54  *
55  * $ftpioId: ftpio.c,v 1.30 1998/04/11 07:28:53 phk Exp $
56  *
57  */
58 
59 #include <sys/param.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 
63 #include <ctype.h>
64 #include <err.h>
65 #include <errno.h>
66 #include <fcntl.h>
67 #include <netdb.h>
68 #include <stdarg.h>
69 #include <stdint.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <time.h>
74 #include <unistd.h>
75 
76 #include "fetch.h"
77 #include "common.h"
78 #include "ftperr.h"
79 
80 #define FTP_ANONYMOUS_USER	"anonymous"
81 
82 #define FTP_CONNECTION_ALREADY_OPEN	125
83 #define FTP_OPEN_DATA_CONNECTION	150
84 #define FTP_OK				200
85 #define FTP_FILE_STATUS			213
86 #define FTP_SERVICE_READY		220
87 #define FTP_TRANSFER_COMPLETE		226
88 #define FTP_PASSIVE_MODE		227
89 #define FTP_LPASSIVE_MODE		228
90 #define FTP_EPASSIVE_MODE		229
91 #define FTP_LOGGED_IN			230
92 #define FTP_FILE_ACTION_OK		250
93 #define FTP_DIRECTORY_CREATED		257 /* multiple meanings */
94 #define FTP_FILE_CREATED		257 /* multiple meanings */
95 #define FTP_WORKING_DIRECTORY		257 /* multiple meanings */
96 #define FTP_NEED_PASSWORD		331
97 #define FTP_NEED_ACCOUNT		332
98 #define FTP_FILE_OK			350
99 #define FTP_SYNTAX_ERROR		500
100 #define FTP_PROTOCOL_ERROR		999
101 
102 static struct url cached_host;
103 static conn_t	*cached_connection;
104 
105 #define isftpreply(foo)				\
106 	(isdigit((unsigned char)foo[0]) &&	\
107 	    isdigit((unsigned char)foo[1]) &&	\
108 	    isdigit((unsigned char)foo[2]) &&	\
109 	    (foo[3] == ' ' || foo[3] == '\0'))
110 #define isftpinfo(foo) \
111 	(isdigit((unsigned char)foo[0]) &&	\
112 	    isdigit((unsigned char)foo[1]) &&	\
113 	    isdigit((unsigned char)foo[2]) &&	\
114 	    foo[3] == '-')
115 
116 /*
117  * Translate IPv4 mapped IPv6 address to IPv4 address
118  */
119 static void
120 unmappedaddr(struct sockaddr_in6 *sin6)
121 {
122 	struct sockaddr_in *sin4;
123 	u_int32_t addr;
124 	int port;
125 
126 	if (sin6->sin6_family != AF_INET6 ||
127 	    !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
128 		return;
129 	sin4 = (struct sockaddr_in *)sin6;
130 	addr = *(u_int32_t *)(uintptr_t)&sin6->sin6_addr.s6_addr[12];
131 	port = sin6->sin6_port;
132 	memset(sin4, 0, sizeof(struct sockaddr_in));
133 	sin4->sin_addr.s_addr = addr;
134 	sin4->sin_port = port;
135 	sin4->sin_family = AF_INET;
136 	sin4->sin_len = sizeof(struct sockaddr_in);
137 }
138 
139 /*
140  * Get server response
141  */
142 static int
143 ftp_chkerr(conn_t *conn)
144 {
145 	if (fetch_getln(conn) == -1) {
146 		fetch_syserr();
147 		return (-1);
148 	}
149 	if (isftpinfo(conn->buf)) {
150 		while (conn->buflen && !isftpreply(conn->buf)) {
151 			if (fetch_getln(conn) == -1) {
152 				fetch_syserr();
153 				return (-1);
154 			}
155 		}
156 	}
157 
158 	while (conn->buflen &&
159 	    isspace((unsigned char)conn->buf[conn->buflen - 1]))
160 		conn->buflen--;
161 	conn->buf[conn->buflen] = '\0';
162 
163 	if (!isftpreply(conn->buf)) {
164 		ftp_seterr(FTP_PROTOCOL_ERROR);
165 		return (-1);
166 	}
167 
168 	conn->err = (conn->buf[0] - '0') * 100
169 	    + (conn->buf[1] - '0') * 10
170 	    + (conn->buf[2] - '0');
171 
172 	return (conn->err);
173 }
174 
175 /*
176  * Send a command and check reply
177  */
178 static int
179 ftp_cmd(conn_t *conn, const char *fmt, ...)
180 {
181 	va_list ap;
182 	size_t len;
183 	char *msg;
184 	int r;
185 
186 	va_start(ap, fmt);
187 	len = vasprintf(&msg, fmt, ap);
188 	va_end(ap);
189 
190 	if (msg == NULL) {
191 		errno = ENOMEM;
192 		fetch_syserr();
193 		return (-1);
194 	}
195 
196 	r = fetch_putln(conn, msg, len);
197 	free(msg);
198 
199 	if (r == -1) {
200 		fetch_syserr();
201 		return (-1);
202 	}
203 
204 	return (ftp_chkerr(conn));
205 }
206 
207 /*
208  * Return a pointer to the filename part of a path
209  */
210 static const char *
211 ftp_filename(const char *file, int *len, int *type)
212 {
213 	const char *s;
214 
215 	if ((s = strrchr(file, '/')) == NULL)
216 		s = file;
217 	else
218 		s = s + 1;
219 	*len = strlen(s);
220 	if (*len > 7 && strncmp(s + *len - 7, ";type=", 6) == 0) {
221 		*type = s[*len - 1];
222 		*len -= 7;
223 	} else {
224 		*type = '\0';
225 	}
226 	return (s);
227 }
228 
229 /*
230  * Get current working directory from the reply to a CWD, PWD or CDUP
231  * command.
232  */
233 static int
234 ftp_pwd(conn_t *conn, char *pwd, size_t pwdlen)
235 {
236 	char *src, *dst, *end;
237 	int q;
238 
239 	if (conn->err != FTP_WORKING_DIRECTORY &&
240 	    conn->err != FTP_FILE_ACTION_OK)
241 		return (FTP_PROTOCOL_ERROR);
242 	end = conn->buf + conn->buflen;
243 	src = conn->buf + 4;
244 	if (src >= end || *src++ != '"')
245 		return (FTP_PROTOCOL_ERROR);
246 	for (q = 0, dst = pwd; src < end && pwdlen--; ++src) {
247 		if (!q && *src == '"')
248 			q = 1;
249 		else if (q && *src != '"')
250 			break;
251 		else if (q)
252 			*dst++ = '"', q = 0;
253 		else
254 			*dst++ = *src;
255 	}
256 	if (!pwdlen)
257 		return (FTP_PROTOCOL_ERROR);
258 	*dst = '\0';
259 #if 0
260 	DEBUGF("pwd: [%s]\n", pwd);
261 #endif
262 	return (FTP_OK);
263 }
264 
265 /*
266  * Change working directory to the directory that contains the specified
267  * file.
268  */
269 static int
270 ftp_cwd(conn_t *conn, const char *file)
271 {
272 	const char *beg, *end;
273 	char pwd[PATH_MAX];
274 	int e, i, len;
275 
276 	/* If no slashes in name, no need to change dirs. */
277 	if ((end = strrchr(file, '/')) == NULL)
278 		return (0);
279 	if ((e = ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||
280 	    (e = ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) {
281 		ftp_seterr(e);
282 		return (-1);
283 	}
284 	for (;;) {
285 		len = strlen(pwd);
286 
287 		/* Look for a common prefix between PWD and dir to fetch. */
288 		for (i = 0; i <= len && i <= end - file; ++i)
289 			if (pwd[i] != file[i])
290 				break;
291 #if 0
292 		DEBUGF("have: [%.*s|%s]\n", i, pwd, pwd + i);
293 		DEBUGF("want: [%.*s|%s]\n", i, file, file + i);
294 #endif
295 		/* Keep going up a dir until we have a matching prefix. */
296 		if (pwd[i] == '\0' && (file[i - 1] == '/' || file[i] == '/'))
297 			break;
298 		if ((e = ftp_cmd(conn, "CDUP")) != FTP_FILE_ACTION_OK ||
299 		    (e = ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||
300 		    (e = ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) {
301 			ftp_seterr(e);
302 			return (-1);
303 		}
304 	}
305 
306 #ifdef FTP_COMBINE_CWDS
307 	/* Skip leading slashes, even "////". */
308 	for (beg = file + i; beg < end && *beg == '/'; ++beg, ++i)
309 		/* nothing */ ;
310 
311 	/* If there is no trailing dir, we're already there. */
312 	if (beg >= end)
313 		return (0);
314 
315 	/* Change to the directory all in one chunk (e.g., foo/bar/baz). */
316 	e = ftp_cmd(conn, "CWD %.*s", (int)(end - beg), beg);
317 	if (e == FTP_FILE_ACTION_OK)
318 		return (0);
319 #endif /* FTP_COMBINE_CWDS */
320 
321 	/* That didn't work so go back to legacy behavior (multiple CWDs). */
322 	for (beg = file + i; beg < end; beg = file + i + 1) {
323 		while (*beg == '/')
324 			++beg, ++i;
325 		for (++i; file + i < end && file[i] != '/'; ++i)
326 			/* nothing */ ;
327 		e = ftp_cmd(conn, "CWD %.*s", file + i - beg, beg);
328 		if (e != FTP_FILE_ACTION_OK) {
329 			ftp_seterr(e);
330 			return (-1);
331 		}
332 	}
333 	return (0);
334 }
335 
336 /*
337  * Set transfer mode and data type
338  */
339 static int
340 ftp_mode_type(conn_t *conn, int mode, int type)
341 {
342 	int e;
343 
344 	switch (mode) {
345 	case 0:
346 	case 's':
347 		mode = 'S';
348 	case 'S':
349 		break;
350 	default:
351 		return (FTP_PROTOCOL_ERROR);
352 	}
353 	if ((e = ftp_cmd(conn, "MODE %c", mode)) != FTP_OK) {
354 		if (mode == 'S') {
355 			/*
356 			 * Stream mode is supposed to be the default - so
357 			 * much so that some servers not only do not
358 			 * support any other mode, but do not support the
359 			 * MODE command at all.
360 			 *
361 			 * If "MODE S" fails, it is unlikely that we
362 			 * previously succeeded in setting a different
363 			 * mode.  Therefore, we simply hope that the
364 			 * server is already in the correct mode, and
365 			 * silently ignore the failure.
366 			 */
367 		} else {
368 			return (e);
369 		}
370 	}
371 
372 	switch (type) {
373 	case 0:
374 	case 'i':
375 		type = 'I';
376 	case 'I':
377 		break;
378 	case 'a':
379 		type = 'A';
380 	case 'A':
381 		break;
382 	case 'd':
383 		type = 'D';
384 	case 'D':
385 		/* can't handle yet */
386 	default:
387 		return (FTP_PROTOCOL_ERROR);
388 	}
389 	if ((e = ftp_cmd(conn, "TYPE %c", type)) != FTP_OK)
390 		return (e);
391 
392 	return (FTP_OK);
393 }
394 
395 /*
396  * Request and parse file stats
397  */
398 static int
399 ftp_stat(conn_t *conn, const char *file, struct url_stat *us)
400 {
401 	char *ln;
402 	const char *filename;
403 	int filenamelen, type;
404 	struct tm tm;
405 	time_t t;
406 	int e;
407 
408 	us->size = -1;
409 	us->atime = us->mtime = 0;
410 
411 	filename = ftp_filename(file, &filenamelen, &type);
412 
413 	if ((e = ftp_mode_type(conn, 0, type)) != FTP_OK) {
414 		ftp_seterr(e);
415 		return (-1);
416 	}
417 
418 	e = ftp_cmd(conn, "SIZE %.*s", filenamelen, filename);
419 	if (e != FTP_FILE_STATUS) {
420 		ftp_seterr(e);
421 		return (-1);
422 	}
423 	for (ln = conn->buf + 4; *ln && isspace((unsigned char)*ln); ln++)
424 		/* nothing */ ;
425 	for (us->size = 0; *ln && isdigit((unsigned char)*ln); ln++)
426 		us->size = us->size * 10 + *ln - '0';
427 	if (*ln && !isspace((unsigned char)*ln)) {
428 		ftp_seterr(FTP_PROTOCOL_ERROR);
429 		us->size = -1;
430 		return (-1);
431 	}
432 	if (us->size == 0)
433 		us->size = -1;
434 	DEBUGF("size: [%lld]\n", (long long)us->size);
435 
436 	e = ftp_cmd(conn, "MDTM %.*s", filenamelen, filename);
437 	if (e != FTP_FILE_STATUS) {
438 		ftp_seterr(e);
439 		return (-1);
440 	}
441 	for (ln = conn->buf + 4; *ln && isspace((unsigned char)*ln); ln++)
442 		/* nothing */ ;
443 	switch (strspn(ln, "0123456789")) {
444 	case 14:
445 		break;
446 	case 15:
447 		ln++;
448 		ln[0] = '2';
449 		ln[1] = '0';
450 		break;
451 	default:
452 		ftp_seterr(FTP_PROTOCOL_ERROR);
453 		return (-1);
454 	}
455 	if (sscanf(ln, "%04d%02d%02d%02d%02d%02d",
456 	    &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
457 	    &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
458 		ftp_seterr(FTP_PROTOCOL_ERROR);
459 		return (-1);
460 	}
461 	tm.tm_mon--;
462 	tm.tm_year -= 1900;
463 	tm.tm_isdst = -1;
464 	t = timegm(&tm);
465 	if (t == (time_t)-1)
466 		t = time(NULL);
467 	us->mtime = t;
468 	us->atime = t;
469 	DEBUGF("last modified: [%04d-%02d-%02d %02d:%02d:%02d]\n",
470 	    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
471 	    tm.tm_hour, tm.tm_min, tm.tm_sec);
472 	return (0);
473 }
474 
475 /*
476  * I/O functions for FTP
477  */
478 struct ftpio {
479 	conn_t	*cconn;		/* Control connection */
480 	conn_t	*dconn;		/* Data connection */
481 	int	 dir;		/* Direction */
482 	int	 eof;		/* EOF reached */
483 	int	 err;		/* Error code */
484 };
485 
486 static int	 ftp_readfn(void *, char *, int);
487 static int	 ftp_writefn(void *, const char *, int);
488 static fpos_t	 ftp_seekfn(void *, fpos_t, int);
489 static int	 ftp_closefn(void *);
490 
491 static int
492 ftp_readfn(void *v, char *buf, int len)
493 {
494 	struct ftpio *io;
495 	int r;
496 
497 	io = (struct ftpio *)v;
498 	if (io == NULL) {
499 		errno = EBADF;
500 		return (-1);
501 	}
502 	if (io->cconn == NULL || io->dconn == NULL || io->dir == O_WRONLY) {
503 		errno = EBADF;
504 		return (-1);
505 	}
506 	if (io->err) {
507 		errno = io->err;
508 		return (-1);
509 	}
510 	if (io->eof)
511 		return (0);
512 	r = fetch_read(io->dconn, buf, len);
513 	if (r > 0)
514 		return (r);
515 	if (r == 0) {
516 		io->eof = 1;
517 		return (0);
518 	}
519 	if (errno != EINTR)
520 		io->err = errno;
521 	return (-1);
522 }
523 
524 static int
525 ftp_writefn(void *v, const char *buf, int len)
526 {
527 	struct ftpio *io;
528 	int w;
529 
530 	io = (struct ftpio *)v;
531 	if (io == NULL) {
532 		errno = EBADF;
533 		return (-1);
534 	}
535 	if (io->cconn == NULL || io->dconn == NULL || io->dir == O_RDONLY) {
536 		errno = EBADF;
537 		return (-1);
538 	}
539 	if (io->err) {
540 		errno = io->err;
541 		return (-1);
542 	}
543 	w = fetch_write(io->dconn, buf, len);
544 	if (w >= 0)
545 		return (w);
546 	if (errno != EINTR)
547 		io->err = errno;
548 	return (-1);
549 }
550 
551 static fpos_t
552 ftp_seekfn(void *v, fpos_t pos __unused, int whence __unused)
553 {
554 	struct ftpio *io;
555 
556 	io = (struct ftpio *)v;
557 	if (io == NULL) {
558 		errno = EBADF;
559 		return (-1);
560 	}
561 	errno = ESPIPE;
562 	return (-1);
563 }
564 
565 static int
566 ftp_closefn(void *v)
567 {
568 	struct ftpio *io;
569 	int r;
570 
571 	io = (struct ftpio *)v;
572 	if (io == NULL) {
573 		errno = EBADF;
574 		return (-1);
575 	}
576 	if (io->dir == -1)
577 		return (0);
578 	if (io->cconn == NULL || io->dconn == NULL) {
579 		errno = EBADF;
580 		return (-1);
581 	}
582 	fetch_close(io->dconn);
583 	io->dir = -1;
584 	io->dconn = NULL;
585 	DEBUGF("Waiting for final status\n");
586 	r = ftp_chkerr(io->cconn);
587 	if (io->cconn == cached_connection && io->cconn->ref == 1)
588 		cached_connection = NULL;
589 	fetch_close(io->cconn);
590 	free(io);
591 	return (r == FTP_TRANSFER_COMPLETE) ? 0 : -1;
592 }
593 
594 static FILE *
595 ftp_setup(conn_t *cconn, conn_t *dconn, int mode)
596 {
597 	struct ftpio *io;
598 	FILE *f;
599 
600 	if (cconn == NULL || dconn == NULL)
601 		return (NULL);
602 	if ((io = malloc(sizeof(*io))) == NULL)
603 		return (NULL);
604 	io->cconn = cconn;
605 	io->dconn = dconn;
606 	io->dir = mode;
607 	io->eof = io->err = 0;
608 	f = funopen(io, ftp_readfn, ftp_writefn, ftp_seekfn, ftp_closefn);
609 	if (f == NULL)
610 		free(io);
611 	return (f);
612 }
613 
614 /*
615  * Transfer file
616  */
617 static FILE *
618 ftp_transfer(conn_t *conn, const char *oper, const char *file,
619     int mode, off_t offset, const char *flags)
620 {
621 	struct sockaddr_storage sa;
622 	struct sockaddr_in6 *sin6;
623 	struct sockaddr_in *sin4;
624 	const char *bindaddr;
625 	const char *filename;
626 	int filenamelen, type;
627 	int low, pasv, verbose;
628 	int e, sd = -1;
629 	socklen_t l;
630 	char *s;
631 	FILE *df;
632 
633 	/* check flags */
634 	low = CHECK_FLAG('l');
635 	pasv = CHECK_FLAG('p') || !CHECK_FLAG('P');
636 	verbose = CHECK_FLAG('v');
637 
638 	/* passive mode */
639 	if ((s = getenv("FTP_PASSIVE_MODE")) != NULL)
640 		pasv = (strncasecmp(s, "no", 2) != 0);
641 
642 	/* isolate filename */
643 	filename = ftp_filename(file, &filenamelen, &type);
644 
645 	/* set transfer mode and data type */
646 	if ((e = ftp_mode_type(conn, 0, type)) != FTP_OK)
647 		goto ouch;
648 
649 	/* find our own address, bind, and listen */
650 	l = sizeof(sa);
651 	if (getsockname(conn->sd, (struct sockaddr *)&sa, &l) == -1)
652 		goto sysouch;
653 	if (sa.ss_family == AF_INET6)
654 		unmappedaddr((struct sockaddr_in6 *)&sa);
655 
656 	/* open data socket */
657 	if ((sd = socket(sa.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
658 		fetch_syserr();
659 		return (NULL);
660 	}
661 
662 	if (pasv) {
663 		u_char addr[64];
664 		char *ln, *p;
665 		unsigned int i;
666 		int port;
667 
668 		/* send PASV command */
669 		if (verbose)
670 			fetch_info("setting passive mode");
671 		switch (sa.ss_family) {
672 		case AF_INET:
673 			if ((e = ftp_cmd(conn, "PASV")) != FTP_PASSIVE_MODE)
674 				goto ouch;
675 			break;
676 		case AF_INET6:
677 			if ((e = ftp_cmd(conn, "EPSV")) != FTP_EPASSIVE_MODE) {
678 				if (e == -1)
679 					goto ouch;
680 				if ((e = ftp_cmd(conn, "LPSV")) !=
681 				    FTP_LPASSIVE_MODE)
682 					goto ouch;
683 			}
684 			break;
685 		default:
686 			e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
687 			goto ouch;
688 		}
689 
690 		/*
691 		 * Find address and port number. The reply to the PASV command
692 		 * is IMHO the one and only weak point in the FTP protocol.
693 		 */
694 		ln = conn->buf;
695 		switch (e) {
696 		case FTP_PASSIVE_MODE:
697 		case FTP_LPASSIVE_MODE:
698 			for (p = ln + 3; *p && !isdigit((unsigned char)*p); p++)
699 				/* nothing */ ;
700 			if (!*p) {
701 				e = FTP_PROTOCOL_ERROR;
702 				goto ouch;
703 			}
704 			l = (e == FTP_PASSIVE_MODE ? 6 : 21);
705 			for (i = 0; *p && i < l; i++, p++) {
706 				addr[i] = strtol(p, &p, 10);
707 				if (*p == '\0' && i < l - 1)
708 					break;
709 			}
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
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 *
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
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
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 *
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 *
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 *
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 *
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 *
1169 fetchGetFTP(struct url *url, const char *flags)
1170 {
1171 	return (fetchXGetFTP(url, NULL, flags));
1172 }
1173 
1174 /*
1175  * Put file
1176  */
1177 FILE *
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
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 *
1207 fetchListFTP(struct url *url __unused, const char *flags __unused)
1208 {
1209 	warnx("fetchListFTP(): not implemented");
1210 	return (NULL);
1211 }
1212