xref: /freebsd/lib/libfetch/ftp.c (revision 3642298923e528d795e3a30ec165d2b469e28b40)
1 /*-
2  * Copyright (c) 1998-2004 Dag-Erling Co�dan Sm�rgrav
3  * All rights reserved.
4  *
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
16  *
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.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
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 Co�dan 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) (isdigit(foo[0]) && isdigit(foo[1]) \
106 			 && isdigit(foo[2]) \
107 			 && (foo[3] == ' ' || foo[3] == '\0'))
108 #define isftpinfo(foo) (isdigit(foo[0]) && isdigit(foo[1]) \
109 			&& isdigit(foo[2]) && foo[3] == '-')
110 
111 /*
112  * Translate IPv4 mapped IPv6 address to IPv4 address
113  */
114 static void
115 unmappedaddr(struct sockaddr_in6 *sin6)
116 {
117 	struct sockaddr_in *sin4;
118 	u_int32_t addr;
119 	int port;
120 
121 	if (sin6->sin6_family != AF_INET6 ||
122 	    !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
123 		return;
124 	sin4 = (struct sockaddr_in *)sin6;
125 	addr = *(u_int32_t *)&sin6->sin6_addr.s6_addr[12];
126 	port = sin6->sin6_port;
127 	memset(sin4, 0, sizeof(struct sockaddr_in));
128 	sin4->sin_addr.s_addr = addr;
129 	sin4->sin_port = port;
130 	sin4->sin_family = AF_INET;
131 	sin4->sin_len = sizeof(struct sockaddr_in);
132 }
133 
134 /*
135  * Get server response
136  */
137 static int
138 _ftp_chkerr(conn_t *conn)
139 {
140 	if (_fetch_getln(conn) == -1) {
141 		_fetch_syserr();
142 		return (-1);
143 	}
144 	if (isftpinfo(conn->buf)) {
145 		while (conn->buflen && !isftpreply(conn->buf)) {
146 			if (_fetch_getln(conn) == -1) {
147 				_fetch_syserr();
148 				return (-1);
149 			}
150 		}
151 	}
152 
153 	while (conn->buflen && isspace(conn->buf[conn->buflen - 1]))
154 		conn->buflen--;
155 	conn->buf[conn->buflen] = '\0';
156 
157 	if (!isftpreply(conn->buf)) {
158 		_ftp_seterr(FTP_PROTOCOL_ERROR);
159 		return (-1);
160 	}
161 
162 	conn->err = (conn->buf[0] - '0') * 100
163 	    + (conn->buf[1] - '0') * 10
164 	    + (conn->buf[2] - '0');
165 
166 	return (conn->err);
167 }
168 
169 /*
170  * Send a command and check reply
171  */
172 static int
173 _ftp_cmd(conn_t *conn, const char *fmt, ...)
174 {
175 	va_list ap;
176 	size_t len;
177 	char *msg;
178 	int r;
179 
180 	va_start(ap, fmt);
181 	len = vasprintf(&msg, fmt, ap);
182 	va_end(ap);
183 
184 	if (msg == NULL) {
185 		errno = ENOMEM;
186 		_fetch_syserr();
187 		return (-1);
188 	}
189 
190 	r = _fetch_putln(conn, msg, len);
191 	free(msg);
192 
193 	if (r == -1) {
194 		_fetch_syserr();
195 		return (-1);
196 	}
197 
198 	return (_ftp_chkerr(conn));
199 }
200 
201 /*
202  * Return a pointer to the filename part of a path
203  */
204 static const char *
205 _ftp_filename(const char *file, int *len, int *type)
206 {
207 	const char *s;
208 
209 	if ((s = strrchr(file, '/')) == NULL)
210 		s = file;
211 	else
212 		s = s + 1;
213 	*len = strlen(s);
214 	if (*len > 7 && strncmp(s + *len - 7, ";type=", 6) == 0) {
215 		*type = s[*len - 1];
216 		*len -= 7;
217 	} else {
218 		*type = '\0';
219 	}
220 	return (s);
221 }
222 
223 /*
224  * Get current working directory from the reply to a CWD, PWD or CDUP
225  * command.
226  */
227 static int
228 _ftp_pwd(conn_t *conn, char *pwd, size_t pwdlen)
229 {
230 	char *src, *dst, *end;
231 	int q;
232 
233 	if (conn->err != FTP_WORKING_DIRECTORY &&
234 	    conn->err != FTP_FILE_ACTION_OK)
235 		return (FTP_PROTOCOL_ERROR);
236 	end = conn->buf + conn->buflen;
237 	src = conn->buf + 4;
238 	if (src >= end || *src++ != '"')
239 		return (FTP_PROTOCOL_ERROR);
240 	for (q = 0, dst = pwd; src < end && pwdlen--; ++src) {
241 		if (!q && *src == '"')
242 			q = 1;
243 		else if (q && *src != '"')
244 			break;
245 		else if (q)
246 			*dst++ = '"', q = 0;
247 		else
248 			*dst++ = *src;
249 	}
250 	if (!pwdlen)
251 		return (FTP_PROTOCOL_ERROR);
252 	*dst = '\0';
253 #if 0
254 	DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd));
255 #endif
256 	return (FTP_OK);
257 }
258 
259 /*
260  * Change working directory to the directory that contains the specified
261  * file.
262  */
263 static int
264 _ftp_cwd(conn_t *conn, const char *file)
265 {
266 	const char *beg, *end;
267 	char pwd[PATH_MAX];
268 	int e, i, len;
269 
270 	if ((end = strrchr(file, '/')) == NULL)
271 		return (0);
272 	if ((e = _ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||
273 	    (e = _ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) {
274 		_ftp_seterr(e);
275 		return (-1);
276 	}
277 	for (;;) {
278 		len = strlen(pwd);
279 		/* look for a common prefix */
280 		for (i = 0; i <= len && i <= end - file; ++i)
281 			if (pwd[i] != file[i])
282 				break;
283 #if 0
284 		DEBUG(fprintf(stderr, "have: [%.*s|%s]\n", i, pwd, pwd + i));
285 		DEBUG(fprintf(stderr, "want: [%.*s|%s]\n", i, file, file + i));
286 #endif
287 		if (pwd[i] == '\0' && (file[i - 1] == '/' || file[i] == '/'))
288 			break;
289 		if ((e = _ftp_cmd(conn, "CDUP")) != FTP_FILE_ACTION_OK ||
290 		    (e = _ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||
291 		    (e = _ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) {
292 			_ftp_seterr(e);
293 			return (-1);
294 		}
295 	}
296 	for (beg = file + i; beg < end; beg = file + i + 1) {
297 		for (++i; file + i < end && file[i] != '/'; ++i)
298 			/* nothing */ ;
299 		e = _ftp_cmd(conn, "CWD %.*s", file + i - beg, beg);
300 		if (e != FTP_FILE_ACTION_OK) {
301 			_ftp_seterr(e);
302 			return (-1);
303 		}
304 	}
305 	return (0);
306 }
307 
308 /*
309  * Set transfer mode and data type
310  */
311 static int
312 _ftp_mode_type(conn_t *conn, int mode, int type)
313 {
314 	int e;
315 
316 	switch (mode) {
317 	case 0:
318 	case 's':
319 		mode = 'S';
320 	case 'S':
321 		break;
322 	default:
323 		return (FTP_PROTOCOL_ERROR);
324 	}
325 	if ((e = _ftp_cmd(conn, "MODE %c", mode)) != FTP_OK)
326 		return (e);
327 
328 	switch (type) {
329 	case 0:
330 	case 'i':
331 		type = 'I';
332 	case 'I':
333 		break;
334 	case 'a':
335 		type = 'A';
336 	case 'A':
337 		break;
338 	case 'd':
339 		type = 'D';
340 	case 'D':
341 		/* can't handle yet */
342 	default:
343 		return (FTP_PROTOCOL_ERROR);
344 	}
345 	if ((e = _ftp_cmd(conn, "TYPE %c", type)) != FTP_OK)
346 		return (e);
347 
348 	return (FTP_OK);
349 }
350 
351 /*
352  * Request and parse file stats
353  */
354 static int
355 _ftp_stat(conn_t *conn, const char *file, struct url_stat *us)
356 {
357 	char *ln;
358 	const char *filename;
359 	int filenamelen, type;
360 	struct tm tm;
361 	time_t t;
362 	int e;
363 
364 	us->size = -1;
365 	us->atime = us->mtime = 0;
366 
367 	filename = _ftp_filename(file, &filenamelen, &type);
368 
369 	if ((e = _ftp_mode_type(conn, 0, type)) != FTP_OK) {
370 		_ftp_seterr(e);
371 		return (-1);
372 	}
373 
374 	e = _ftp_cmd(conn, "SIZE %.*s", filenamelen, filename);
375 	if (e != FTP_FILE_STATUS) {
376 		_ftp_seterr(e);
377 		return (-1);
378 	}
379 	for (ln = conn->buf + 4; *ln && isspace(*ln); ln++)
380 		/* nothing */ ;
381 	for (us->size = 0; *ln && isdigit(*ln); ln++)
382 		us->size = us->size * 10 + *ln - '0';
383 	if (*ln && !isspace(*ln)) {
384 		_ftp_seterr(FTP_PROTOCOL_ERROR);
385 		us->size = -1;
386 		return (-1);
387 	}
388 	if (us->size == 0)
389 		us->size = -1;
390 	DEBUG(fprintf(stderr, "size: [%lld]\n", (long long)us->size));
391 
392 	e = _ftp_cmd(conn, "MDTM %.*s", filenamelen, filename);
393 	if (e != FTP_FILE_STATUS) {
394 		_ftp_seterr(e);
395 		return (-1);
396 	}
397 	for (ln = conn->buf + 4; *ln && isspace(*ln); ln++)
398 		/* nothing */ ;
399 	switch (strspn(ln, "0123456789")) {
400 	case 14:
401 		break;
402 	case 15:
403 		ln++;
404 		ln[0] = '2';
405 		ln[1] = '0';
406 		break;
407 	default:
408 		_ftp_seterr(FTP_PROTOCOL_ERROR);
409 		return (-1);
410 	}
411 	if (sscanf(ln, "%04d%02d%02d%02d%02d%02d",
412 	    &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
413 	    &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
414 		_ftp_seterr(FTP_PROTOCOL_ERROR);
415 		return (-1);
416 	}
417 	tm.tm_mon--;
418 	tm.tm_year -= 1900;
419 	tm.tm_isdst = -1;
420 	t = timegm(&tm);
421 	if (t == (time_t)-1)
422 		t = time(NULL);
423 	us->mtime = t;
424 	us->atime = t;
425 	DEBUG(fprintf(stderr,
426 	    "last modified: [%04d-%02d-%02d %02d:%02d:%02d]\n",
427 	    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
428 	    tm.tm_hour, tm.tm_min, tm.tm_sec));
429 	return (0);
430 }
431 
432 /*
433  * I/O functions for FTP
434  */
435 struct ftpio {
436 	conn_t	*cconn;		/* Control connection */
437 	conn_t	*dconn;		/* Data connection */
438 	int	 dir;		/* Direction */
439 	int	 eof;		/* EOF reached */
440 	int	 err;		/* Error code */
441 };
442 
443 static int	 _ftp_readfn(void *, char *, int);
444 static int	 _ftp_writefn(void *, const char *, int);
445 static fpos_t	 _ftp_seekfn(void *, fpos_t, int);
446 static int	 _ftp_closefn(void *);
447 
448 static int
449 _ftp_readfn(void *v, char *buf, int len)
450 {
451 	struct ftpio *io;
452 	int r;
453 
454 	io = (struct ftpio *)v;
455 	if (io == NULL) {
456 		errno = EBADF;
457 		return (-1);
458 	}
459 	if (io->cconn == NULL || io->dconn == NULL || io->dir == O_WRONLY) {
460 		errno = EBADF;
461 		return (-1);
462 	}
463 	if (io->err) {
464 		errno = io->err;
465 		return (-1);
466 	}
467 	if (io->eof)
468 		return (0);
469 	r = _fetch_read(io->dconn, buf, len);
470 	if (r > 0)
471 		return (r);
472 	if (r == 0) {
473 		io->eof = 1;
474 		return (0);
475 	}
476 	if (errno != EINTR)
477 		io->err = errno;
478 	return (-1);
479 }
480 
481 static int
482 _ftp_writefn(void *v, const char *buf, int len)
483 {
484 	struct ftpio *io;
485 	int w;
486 
487 	io = (struct ftpio *)v;
488 	if (io == NULL) {
489 		errno = EBADF;
490 		return (-1);
491 	}
492 	if (io->cconn == NULL || io->dconn == NULL || io->dir == O_RDONLY) {
493 		errno = EBADF;
494 		return (-1);
495 	}
496 	if (io->err) {
497 		errno = io->err;
498 		return (-1);
499 	}
500 	w = _fetch_write(io->dconn, buf, len);
501 	if (w >= 0)
502 		return (w);
503 	if (errno != EINTR)
504 		io->err = errno;
505 	return (-1);
506 }
507 
508 static fpos_t
509 _ftp_seekfn(void *v, fpos_t pos __unused, int whence __unused)
510 {
511 	struct ftpio *io;
512 
513 	io = (struct ftpio *)v;
514 	if (io == NULL) {
515 		errno = EBADF;
516 		return (-1);
517 	}
518 	errno = ESPIPE;
519 	return (-1);
520 }
521 
522 static int
523 _ftp_closefn(void *v)
524 {
525 	struct ftpio *io;
526 	int r;
527 
528 	io = (struct ftpio *)v;
529 	if (io == NULL) {
530 		errno = EBADF;
531 		return (-1);
532 	}
533 	if (io->dir == -1)
534 		return (0);
535 	if (io->cconn == NULL || io->dconn == NULL) {
536 		errno = EBADF;
537 		return (-1);
538 	}
539 	_fetch_close(io->dconn);
540 	io->dir = -1;
541 	io->dconn = NULL;
542 	DEBUG(fprintf(stderr, "Waiting for final status\n"));
543 	r = _ftp_chkerr(io->cconn);
544 	if (io->cconn == cached_connection && io->cconn->ref == 1)
545 		cached_connection = NULL;
546 	_fetch_close(io->cconn);
547 	free(io);
548 	return (r == FTP_TRANSFER_COMPLETE) ? 0 : -1;
549 }
550 
551 static FILE *
552 _ftp_setup(conn_t *cconn, conn_t *dconn, int mode)
553 {
554 	struct ftpio *io;
555 	FILE *f;
556 
557 	if (cconn == NULL || dconn == NULL)
558 		return (NULL);
559 	if ((io = malloc(sizeof(*io))) == NULL)
560 		return (NULL);
561 	io->cconn = cconn;
562 	io->dconn = dconn;
563 	io->dir = mode;
564 	io->eof = io->err = 0;
565 	f = funopen(io, _ftp_readfn, _ftp_writefn, _ftp_seekfn, _ftp_closefn);
566 	if (f == NULL)
567 		free(io);
568 	return (f);
569 }
570 
571 /*
572  * Transfer file
573  */
574 static FILE *
575 _ftp_transfer(conn_t *conn, const char *oper, const char *file,
576     int mode, off_t offset, const char *flags)
577 {
578 	struct sockaddr_storage sa;
579 	struct sockaddr_in6 *sin6;
580 	struct sockaddr_in *sin4;
581 	const char *filename;
582 	int filenamelen, type;
583 	int low, pasv, verbose;
584 	int e, sd = -1;
585 	socklen_t l;
586 	char *s;
587 	FILE *df;
588 
589 	/* check flags */
590 	low = CHECK_FLAG('l');
591 	pasv = CHECK_FLAG('p');
592 	verbose = CHECK_FLAG('v');
593 
594 	/* passive mode */
595 	if (!pasv)
596 		pasv = ((s = getenv("FTP_PASSIVE_MODE")) != NULL &&
597 		    strncasecmp(s, "no", 2) != 0);
598 
599 	/* isolate filename */
600 	filename = _ftp_filename(file, &filenamelen, &type);
601 
602 	/* set transfer mode and data type */
603 	if ((e = _ftp_mode_type(conn, 0, type)) != FTP_OK)
604 		goto ouch;
605 
606 	/* find our own address, bind, and listen */
607 	l = sizeof(sa);
608 	if (getsockname(conn->sd, (struct sockaddr *)&sa, &l) == -1)
609 		goto sysouch;
610 	if (sa.ss_family == AF_INET6)
611 		unmappedaddr((struct sockaddr_in6 *)&sa);
612 
613 	/* open data socket */
614 	if ((sd = socket(sa.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
615 		_fetch_syserr();
616 		return (NULL);
617 	}
618 
619 	if (pasv) {
620 		u_char addr[64];
621 		char *ln, *p;
622 		unsigned int i;
623 		int port;
624 
625 		/* send PASV command */
626 		if (verbose)
627 			_fetch_info("setting passive mode");
628 		switch (sa.ss_family) {
629 		case AF_INET:
630 			if ((e = _ftp_cmd(conn, "PASV")) != FTP_PASSIVE_MODE)
631 				goto ouch;
632 			break;
633 		case AF_INET6:
634 			if ((e = _ftp_cmd(conn, "EPSV")) != FTP_EPASSIVE_MODE) {
635 				if (e == -1)
636 					goto ouch;
637 				if ((e = _ftp_cmd(conn, "LPSV")) !=
638 				    FTP_LPASSIVE_MODE)
639 					goto ouch;
640 			}
641 			break;
642 		default:
643 			e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
644 			goto ouch;
645 		}
646 
647 		/*
648 		 * Find address and port number. The reply to the PASV command
649 		 * is IMHO the one and only weak point in the FTP protocol.
650 		 */
651 		ln = conn->buf;
652 		switch (e) {
653 		case FTP_PASSIVE_MODE:
654 		case FTP_LPASSIVE_MODE:
655 			for (p = ln + 3; *p && !isdigit(*p); p++)
656 				/* nothing */ ;
657 			if (!*p) {
658 				e = FTP_PROTOCOL_ERROR;
659 				goto ouch;
660 			}
661 			l = (e == FTP_PASSIVE_MODE ? 6 : 21);
662 			for (i = 0; *p && i < l; i++, p++)
663 				addr[i] = strtol(p, &p, 10);
664 			if (i < l) {
665 				e = FTP_PROTOCOL_ERROR;
666 				goto ouch;
667 			}
668 			break;
669 		case FTP_EPASSIVE_MODE:
670 			for (p = ln + 3; *p && *p != '('; p++)
671 				/* nothing */ ;
672 			if (!*p) {
673 				e = FTP_PROTOCOL_ERROR;
674 				goto ouch;
675 			}
676 			++p;
677 			if (sscanf(p, "%c%c%c%d%c", &addr[0], &addr[1], &addr[2],
678 				&port, &addr[3]) != 5 ||
679 			    addr[0] != addr[1] ||
680 			    addr[0] != addr[2] || addr[0] != addr[3]) {
681 				e = FTP_PROTOCOL_ERROR;
682 				goto ouch;
683 			}
684 			break;
685 		}
686 
687 		/* seek to required offset */
688 		if (offset)
689 			if (_ftp_cmd(conn, "REST %lu", (u_long)offset) != FTP_FILE_OK)
690 				goto sysouch;
691 
692 		/* construct sockaddr for data socket */
693 		l = sizeof(sa);
694 		if (getpeername(conn->sd, (struct sockaddr *)&sa, &l) == -1)
695 			goto sysouch;
696 		if (sa.ss_family == AF_INET6)
697 			unmappedaddr((struct sockaddr_in6 *)&sa);
698 		switch (sa.ss_family) {
699 		case AF_INET6:
700 			sin6 = (struct sockaddr_in6 *)&sa;
701 			if (e == FTP_EPASSIVE_MODE)
702 				sin6->sin6_port = htons(port);
703 			else {
704 				bcopy(addr + 2, (char *)&sin6->sin6_addr, 16);
705 				bcopy(addr + 19, (char *)&sin6->sin6_port, 2);
706 			}
707 			break;
708 		case AF_INET:
709 			sin4 = (struct sockaddr_in *)&sa;
710 			if (e == FTP_EPASSIVE_MODE)
711 				sin4->sin_port = htons(port);
712 			else {
713 				bcopy(addr, (char *)&sin4->sin_addr, 4);
714 				bcopy(addr + 4, (char *)&sin4->sin_port, 2);
715 			}
716 			break;
717 		default:
718 			e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
719 			break;
720 		}
721 
722 		/* connect to data port */
723 		if (verbose)
724 			_fetch_info("opening data connection");
725 		if (connect(sd, (struct sockaddr *)&sa, sa.ss_len) == -1)
726 			goto sysouch;
727 
728 		/* make the server initiate the transfer */
729 		if (verbose)
730 			_fetch_info("initiating transfer");
731 		e = _ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
732 		if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
733 			goto ouch;
734 
735 	} else {
736 		u_int32_t a;
737 		u_short p;
738 		int arg, d;
739 		char *ap;
740 		char hname[INET6_ADDRSTRLEN];
741 
742 		switch (sa.ss_family) {
743 		case AF_INET6:
744 			((struct sockaddr_in6 *)&sa)->sin6_port = 0;
745 #ifdef IPV6_PORTRANGE
746 			arg = low ? IPV6_PORTRANGE_DEFAULT : IPV6_PORTRANGE_HIGH;
747 			if (setsockopt(sd, IPPROTO_IPV6, IPV6_PORTRANGE,
748 				(char *)&arg, sizeof(arg)) == -1)
749 				goto sysouch;
750 #endif
751 			break;
752 		case AF_INET:
753 			((struct sockaddr_in *)&sa)->sin_port = 0;
754 			arg = low ? IP_PORTRANGE_DEFAULT : IP_PORTRANGE_HIGH;
755 			if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE,
756 				(char *)&arg, sizeof(arg)) == -1)
757 				goto sysouch;
758 			break;
759 		}
760 		if (verbose)
761 			_fetch_info("binding data socket");
762 		if (bind(sd, (struct sockaddr *)&sa, sa.ss_len) == -1)
763 			goto sysouch;
764 		if (listen(sd, 1) == -1)
765 			goto sysouch;
766 
767 		/* find what port we're on and tell the server */
768 		if (getsockname(sd, (struct sockaddr *)&sa, &l) == -1)
769 			goto sysouch;
770 		switch (sa.ss_family) {
771 		case AF_INET:
772 			sin4 = (struct sockaddr_in *)&sa;
773 			a = ntohl(sin4->sin_addr.s_addr);
774 			p = ntohs(sin4->sin_port);
775 			e = _ftp_cmd(conn, "PORT %d,%d,%d,%d,%d,%d",
776 			    (a >> 24) & 0xff, (a >> 16) & 0xff,
777 			    (a >> 8) & 0xff, a & 0xff,
778 			    (p >> 8) & 0xff, p & 0xff);
779 			break;
780 		case AF_INET6:
781 #define UC(b)	(((int)b)&0xff)
782 			e = -1;
783 			sin6 = (struct sockaddr_in6 *)&sa;
784 			sin6->sin6_scope_id = 0;
785 			if (getnameinfo((struct sockaddr *)&sa, sa.ss_len,
786 				hname, sizeof(hname),
787 				NULL, 0, NI_NUMERICHOST) == 0) {
788 				e = _ftp_cmd(conn, "EPRT |%d|%s|%d|", 2, hname,
789 				    htons(sin6->sin6_port));
790 				if (e == -1)
791 					goto ouch;
792 			}
793 			if (e != FTP_OK) {
794 				ap = (char *)&sin6->sin6_addr;
795 				e = _ftp_cmd(conn,
796 				    "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
797 				    6, 16,
798 				    UC(ap[0]), UC(ap[1]), UC(ap[2]), UC(ap[3]),
799 				    UC(ap[4]), UC(ap[5]), UC(ap[6]), UC(ap[7]),
800 				    UC(ap[8]), UC(ap[9]), UC(ap[10]), UC(ap[11]),
801 				    UC(ap[12]), UC(ap[13]), UC(ap[14]), UC(ap[15]),
802 				    2,
803 				    (ntohs(sin6->sin6_port) >> 8) & 0xff,
804 				    ntohs(sin6->sin6_port)        & 0xff);
805 			}
806 			break;
807 		default:
808 			e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
809 			goto ouch;
810 		}
811 		if (e != FTP_OK)
812 			goto ouch;
813 
814 		/* seek to required offset */
815 		if (offset)
816 			if (_ftp_cmd(conn, "REST %ju", (uintmax_t)offset) != FTP_FILE_OK)
817 				goto sysouch;
818 
819 		/* make the server initiate the transfer */
820 		if (verbose)
821 			_fetch_info("initiating transfer");
822 		e = _ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
823 		if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
824 			goto ouch;
825 
826 		/* accept the incoming connection and go to town */
827 		if ((d = accept(sd, NULL, NULL)) == -1)
828 			goto sysouch;
829 		close(sd);
830 		sd = d;
831 	}
832 
833 	if ((df = _ftp_setup(conn, _fetch_reopen(sd), mode)) == NULL)
834 		goto sysouch;
835 	return (df);
836 
837 sysouch:
838 	_fetch_syserr();
839 	if (sd >= 0)
840 		close(sd);
841 	return (NULL);
842 
843 ouch:
844 	if (e != -1)
845 		_ftp_seterr(e);
846 	if (sd >= 0)
847 		close(sd);
848 	return (NULL);
849 }
850 
851 /*
852  * Authenticate
853  */
854 static int
855 _ftp_authenticate(conn_t *conn, struct url *url, struct url *purl)
856 {
857 	const char *user, *pwd, *logname;
858 	char pbuf[MAXHOSTNAMELEN + MAXLOGNAME + 1];
859 	int e, len;
860 
861 	/* XXX FTP_AUTH, and maybe .netrc */
862 
863 	/* send user name and password */
864 	if (url->user[0] == '\0')
865 		_fetch_netrc_auth(url);
866 	user = url->user;
867 	if (*user == '\0')
868 		user = getenv("FTP_LOGIN");
869 	if (user == NULL || *user == '\0')
870 		user = FTP_ANONYMOUS_USER;
871 	if (purl && url->port == _fetch_default_port(url->scheme))
872 		e = _ftp_cmd(conn, "USER %s@%s", user, url->host);
873 	else if (purl)
874 		e = _ftp_cmd(conn, "USER %s@%s@%d", user, url->host, url->port);
875 	else
876 		e = _ftp_cmd(conn, "USER %s", user);
877 
878 	/* did the server request a password? */
879 	if (e == FTP_NEED_PASSWORD) {
880 		pwd = url->pwd;
881 		if (*pwd == '\0')
882 			pwd = getenv("FTP_PASSWORD");
883 		if (pwd == NULL || *pwd == '\0') {
884 			if ((logname = getlogin()) == 0)
885 				logname = FTP_ANONYMOUS_USER;
886 			if ((len = snprintf(pbuf, MAXLOGNAME + 1, "%s@", logname)) < 0)
887 				len = 0;
888 			else if (len > MAXLOGNAME)
889 				len = MAXLOGNAME;
890 			gethostname(pbuf + len, sizeof(pbuf) - len);
891 			pwd = pbuf;
892 		}
893 		e = _ftp_cmd(conn, "PASS %s", pwd);
894 	}
895 
896 	return (e);
897 }
898 
899 /*
900  * Log on to FTP server
901  */
902 static conn_t *
903 _ftp_connect(struct url *url, struct url *purl, const char *flags)
904 {
905 	conn_t *conn;
906 	int e, direct, verbose;
907 #ifdef INET6
908 	int af = AF_UNSPEC;
909 #else
910 	int af = AF_INET;
911 #endif
912 
913 	direct = CHECK_FLAG('d');
914 	verbose = CHECK_FLAG('v');
915 	if (CHECK_FLAG('4'))
916 		af = AF_INET;
917 	else if (CHECK_FLAG('6'))
918 		af = AF_INET6;
919 
920 	if (direct)
921 		purl = NULL;
922 
923 	/* check for proxy */
924 	if (purl) {
925 		/* XXX proxy authentication! */
926 		conn = _fetch_connect(purl->host, purl->port, af, verbose);
927 	} else {
928 		/* no proxy, go straight to target */
929 		conn = _fetch_connect(url->host, url->port, af, verbose);
930 		purl = NULL;
931 	}
932 
933 	/* check connection */
934 	if (conn == NULL)
935 		/* _fetch_connect() has already set an error code */
936 		return (NULL);
937 
938 	/* expect welcome message */
939 	if ((e = _ftp_chkerr(conn)) != FTP_SERVICE_READY)
940 		goto fouch;
941 
942 	/* authenticate */
943 	if ((e = _ftp_authenticate(conn, url, purl)) != FTP_LOGGED_IN)
944 		goto fouch;
945 
946 	/* done */
947 	return (conn);
948 
949 fouch:
950 	if (e != -1)
951 		_ftp_seterr(e);
952 	_fetch_close(conn);
953 	return (NULL);
954 }
955 
956 /*
957  * Disconnect from server
958  */
959 static void
960 _ftp_disconnect(conn_t *conn)
961 {
962 	(void)_ftp_cmd(conn, "QUIT");
963 	if (conn == cached_connection && conn->ref == 1)
964 		cached_connection = NULL;
965 	_fetch_close(conn);
966 }
967 
968 /*
969  * Check if we're already connected
970  */
971 static int
972 _ftp_isconnected(struct url *url)
973 {
974 	return (cached_connection
975 	    && (strcmp(url->host, cached_host.host) == 0)
976 	    && (strcmp(url->user, cached_host.user) == 0)
977 	    && (strcmp(url->pwd, cached_host.pwd) == 0)
978 	    && (url->port == cached_host.port));
979 }
980 
981 /*
982  * Check the cache, reconnect if no luck
983  */
984 static conn_t *
985 _ftp_cached_connect(struct url *url, struct url *purl, const char *flags)
986 {
987 	conn_t *conn;
988 	int e;
989 
990 	/* set default port */
991 	if (!url->port)
992 		url->port = _fetch_default_port(url->scheme);
993 
994 	/* try to use previously cached connection */
995 	if (_ftp_isconnected(url)) {
996 		e = _ftp_cmd(cached_connection, "NOOP");
997 		if (e == FTP_OK || e == FTP_SYNTAX_ERROR)
998 			return (_fetch_ref(cached_connection));
999 	}
1000 
1001 	/* connect to server */
1002 	if ((conn = _ftp_connect(url, purl, flags)) == NULL)
1003 		return (NULL);
1004 	if (cached_connection)
1005 		_ftp_disconnect(cached_connection);
1006 	cached_connection = _fetch_ref(conn);
1007 	memcpy(&cached_host, url, sizeof(*url));
1008 	return (conn);
1009 }
1010 
1011 /*
1012  * Check the proxy settings
1013  */
1014 static struct url *
1015 _ftp_get_proxy(const char *flags)
1016 {
1017 	struct url *purl;
1018 	char *p;
1019 
1020 	if (flags != NULL && strchr(flags, 'd') != NULL)
1021 		return (NULL);
1022 	if (((p = getenv("FTP_PROXY")) || (p = getenv("ftp_proxy")) ||
1023 		(p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
1024 	    *p && (purl = fetchParseURL(p)) != NULL) {
1025 		if (!*purl->scheme) {
1026 			if (getenv("FTP_PROXY") || getenv("ftp_proxy"))
1027 				strcpy(purl->scheme, SCHEME_FTP);
1028 			else
1029 				strcpy(purl->scheme, SCHEME_HTTP);
1030 		}
1031 		if (!purl->port)
1032 			purl->port = _fetch_default_proxy_port(purl->scheme);
1033 		if (strcasecmp(purl->scheme, SCHEME_FTP) == 0 ||
1034 		    strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
1035 			return (purl);
1036 		fetchFreeURL(purl);
1037 	}
1038 	return (NULL);
1039 }
1040 
1041 /*
1042  * Process an FTP request
1043  */
1044 FILE *
1045 _ftp_request(struct url *url, const char *op, struct url_stat *us,
1046     struct url *purl, const char *flags)
1047 {
1048 	conn_t *conn;
1049 	int oflag;
1050 
1051 	/* check if we should use HTTP instead */
1052 	if (purl && strcasecmp(purl->scheme, SCHEME_HTTP) == 0) {
1053 		if (strcmp(op, "STAT") == 0)
1054 			return (_http_request(url, "HEAD", us, purl, flags));
1055 		else if (strcmp(op, "RETR") == 0)
1056 			return (_http_request(url, "GET", us, purl, flags));
1057 		/*
1058 		 * Our HTTP code doesn't support PUT requests yet, so try
1059 		 * a direct connection.
1060 		 */
1061 	}
1062 
1063 	/* connect to server */
1064 	conn = _ftp_cached_connect(url, purl, flags);
1065 	if (purl)
1066 		fetchFreeURL(purl);
1067 	if (conn == NULL)
1068 		return (NULL);
1069 
1070 	/* change directory */
1071 	if (_ftp_cwd(conn, url->doc) == -1)
1072 		return (NULL);
1073 
1074 	/* stat file */
1075 	if (us && _ftp_stat(conn, url->doc, us) == -1
1076 	    && fetchLastErrCode != FETCH_PROTO
1077 	    && fetchLastErrCode != FETCH_UNAVAIL)
1078 		return (NULL);
1079 
1080 	/* just a stat */
1081 	if (strcmp(op, "STAT") == 0)
1082 		return (FILE *)1; /* bogus return value */
1083 	if (strcmp(op, "STOR") == 0 || strcmp(op, "APPE") == 0)
1084 		oflag = O_WRONLY;
1085 	else
1086 		oflag = O_RDONLY;
1087 
1088 	/* initiate the transfer */
1089 	return (_ftp_transfer(conn, op, url->doc, oflag, url->offset, flags));
1090 }
1091 
1092 /*
1093  * Get and stat file
1094  */
1095 FILE *
1096 fetchXGetFTP(struct url *url, struct url_stat *us, const char *flags)
1097 {
1098 	return (_ftp_request(url, "RETR", us, _ftp_get_proxy(flags), flags));
1099 }
1100 
1101 /*
1102  * Get file
1103  */
1104 FILE *
1105 fetchGetFTP(struct url *url, const char *flags)
1106 {
1107 	return (fetchXGetFTP(url, NULL, flags));
1108 }
1109 
1110 /*
1111  * Put file
1112  */
1113 FILE *
1114 fetchPutFTP(struct url *url, const char *flags)
1115 {
1116 
1117 	return (_ftp_request(url, CHECK_FLAG('a') ? "APPE" : "STOR", NULL,
1118 	    _ftp_get_proxy(flags), flags));
1119 }
1120 
1121 /*
1122  * Get file stats
1123  */
1124 int
1125 fetchStatFTP(struct url *url, struct url_stat *us, const char *flags)
1126 {
1127 	FILE *f;
1128 
1129 	f = _ftp_request(url, "STAT", us, _ftp_get_proxy(flags), flags);
1130 	if (f == NULL)
1131 		return (-1);
1132 	return (0);
1133 }
1134 
1135 /*
1136  * List a directory
1137  */
1138 struct url_ent *
1139 fetchListFTP(struct url *url __unused, const char *flags __unused)
1140 {
1141 	warnx("fetchListFTP(): not implemented");
1142 	return (NULL);
1143 }
1144