ftp.c revision 159566
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: head/lib/libfetch/ftp.c 159566 2006-06-13 10:21:03Z des $");
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
102static struct url cached_host;
103static 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 */
114static void
115unmappedaddr(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 */
137static 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 */
172static 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 */
204static 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 */
227static 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 */
263static 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		while (*beg == '/')
298			++beg, ++i;
299		for (++i; file + i < end && file[i] != '/'; ++i)
300			/* nothing */ ;
301		e = _ftp_cmd(conn, "CWD %.*s", file + i - beg, beg);
302		if (e != FTP_FILE_ACTION_OK) {
303			_ftp_seterr(e);
304			return (-1);
305		}
306	}
307	return (0);
308}
309
310/*
311 * Set transfer mode and data type
312 */
313static int
314_ftp_mode_type(conn_t *conn, int mode, int type)
315{
316	int e;
317
318	switch (mode) {
319	case 0:
320	case 's':
321		mode = 'S';
322	case 'S':
323		break;
324	default:
325		return (FTP_PROTOCOL_ERROR);
326	}
327	if ((e = _ftp_cmd(conn, "MODE %c", mode)) != FTP_OK) {
328		if (mode == 'S') {
329			/*
330			 * Stream mode is supposed to be the default - so
331			 * much so that some servers not only do not
332			 * support any other mode, but do not support the
333			 * MODE command at all.
334			 *
335			 * If "MODE S" fails, it is unlikely that we
336			 * previously succeeded in setting a different
337			 * mode.  Therefore, we simply hope that the
338			 * server is already in the correct mode, and
339			 * silently ignore the failure.
340			 */
341		} else {
342			return (e);
343		}
344	}
345
346	switch (type) {
347	case 0:
348	case 'i':
349		type = 'I';
350	case 'I':
351		break;
352	case 'a':
353		type = 'A';
354	case 'A':
355		break;
356	case 'd':
357		type = 'D';
358	case 'D':
359		/* can't handle yet */
360	default:
361		return (FTP_PROTOCOL_ERROR);
362	}
363	if ((e = _ftp_cmd(conn, "TYPE %c", type)) != FTP_OK)
364		return (e);
365
366	return (FTP_OK);
367}
368
369/*
370 * Request and parse file stats
371 */
372static int
373_ftp_stat(conn_t *conn, const char *file, struct url_stat *us)
374{
375	char *ln;
376	const char *filename;
377	int filenamelen, type;
378	struct tm tm;
379	time_t t;
380	int e;
381
382	us->size = -1;
383	us->atime = us->mtime = 0;
384
385	filename = _ftp_filename(file, &filenamelen, &type);
386
387	if ((e = _ftp_mode_type(conn, 0, type)) != FTP_OK) {
388		_ftp_seterr(e);
389		return (-1);
390	}
391
392	e = _ftp_cmd(conn, "SIZE %.*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	for (us->size = 0; *ln && isdigit(*ln); ln++)
400		us->size = us->size * 10 + *ln - '0';
401	if (*ln && !isspace(*ln)) {
402		_ftp_seterr(FTP_PROTOCOL_ERROR);
403		us->size = -1;
404		return (-1);
405	}
406	if (us->size == 0)
407		us->size = -1;
408	DEBUG(fprintf(stderr, "size: [%lld]\n", (long long)us->size));
409
410	e = _ftp_cmd(conn, "MDTM %.*s", filenamelen, filename);
411	if (e != FTP_FILE_STATUS) {
412		_ftp_seterr(e);
413		return (-1);
414	}
415	for (ln = conn->buf + 4; *ln && isspace(*ln); ln++)
416		/* nothing */ ;
417	switch (strspn(ln, "0123456789")) {
418	case 14:
419		break;
420	case 15:
421		ln++;
422		ln[0] = '2';
423		ln[1] = '0';
424		break;
425	default:
426		_ftp_seterr(FTP_PROTOCOL_ERROR);
427		return (-1);
428	}
429	if (sscanf(ln, "%04d%02d%02d%02d%02d%02d",
430	    &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
431	    &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
432		_ftp_seterr(FTP_PROTOCOL_ERROR);
433		return (-1);
434	}
435	tm.tm_mon--;
436	tm.tm_year -= 1900;
437	tm.tm_isdst = -1;
438	t = timegm(&tm);
439	if (t == (time_t)-1)
440		t = time(NULL);
441	us->mtime = t;
442	us->atime = t;
443	DEBUG(fprintf(stderr,
444	    "last modified: [%04d-%02d-%02d %02d:%02d:%02d]\n",
445	    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
446	    tm.tm_hour, tm.tm_min, tm.tm_sec));
447	return (0);
448}
449
450/*
451 * I/O functions for FTP
452 */
453struct ftpio {
454	conn_t	*cconn;		/* Control connection */
455	conn_t	*dconn;		/* Data connection */
456	int	 dir;		/* Direction */
457	int	 eof;		/* EOF reached */
458	int	 err;		/* Error code */
459};
460
461static int	 _ftp_readfn(void *, char *, int);
462static int	 _ftp_writefn(void *, const char *, int);
463static fpos_t	 _ftp_seekfn(void *, fpos_t, int);
464static int	 _ftp_closefn(void *);
465
466static int
467_ftp_readfn(void *v, char *buf, int len)
468{
469	struct ftpio *io;
470	int r;
471
472	io = (struct ftpio *)v;
473	if (io == NULL) {
474		errno = EBADF;
475		return (-1);
476	}
477	if (io->cconn == NULL || io->dconn == NULL || io->dir == O_WRONLY) {
478		errno = EBADF;
479		return (-1);
480	}
481	if (io->err) {
482		errno = io->err;
483		return (-1);
484	}
485	if (io->eof)
486		return (0);
487	r = _fetch_read(io->dconn, buf, len);
488	if (r > 0)
489		return (r);
490	if (r == 0) {
491		io->eof = 1;
492		return (0);
493	}
494	if (errno != EINTR)
495		io->err = errno;
496	return (-1);
497}
498
499static int
500_ftp_writefn(void *v, const char *buf, int len)
501{
502	struct ftpio *io;
503	int w;
504
505	io = (struct ftpio *)v;
506	if (io == NULL) {
507		errno = EBADF;
508		return (-1);
509	}
510	if (io->cconn == NULL || io->dconn == NULL || io->dir == O_RDONLY) {
511		errno = EBADF;
512		return (-1);
513	}
514	if (io->err) {
515		errno = io->err;
516		return (-1);
517	}
518	w = _fetch_write(io->dconn, buf, len);
519	if (w >= 0)
520		return (w);
521	if (errno != EINTR)
522		io->err = errno;
523	return (-1);
524}
525
526static fpos_t
527_ftp_seekfn(void *v, fpos_t pos __unused, int whence __unused)
528{
529	struct ftpio *io;
530
531	io = (struct ftpio *)v;
532	if (io == NULL) {
533		errno = EBADF;
534		return (-1);
535	}
536	errno = ESPIPE;
537	return (-1);
538}
539
540static int
541_ftp_closefn(void *v)
542{
543	struct ftpio *io;
544	int r;
545
546	io = (struct ftpio *)v;
547	if (io == NULL) {
548		errno = EBADF;
549		return (-1);
550	}
551	if (io->dir == -1)
552		return (0);
553	if (io->cconn == NULL || io->dconn == NULL) {
554		errno = EBADF;
555		return (-1);
556	}
557	_fetch_close(io->dconn);
558	io->dir = -1;
559	io->dconn = NULL;
560	DEBUG(fprintf(stderr, "Waiting for final status\n"));
561	r = _ftp_chkerr(io->cconn);
562	if (io->cconn == cached_connection && io->cconn->ref == 1)
563		cached_connection = NULL;
564	_fetch_close(io->cconn);
565	free(io);
566	return (r == FTP_TRANSFER_COMPLETE) ? 0 : -1;
567}
568
569static FILE *
570_ftp_setup(conn_t *cconn, conn_t *dconn, int mode)
571{
572	struct ftpio *io;
573	FILE *f;
574
575	if (cconn == NULL || dconn == NULL)
576		return (NULL);
577	if ((io = malloc(sizeof(*io))) == NULL)
578		return (NULL);
579	io->cconn = cconn;
580	io->dconn = dconn;
581	io->dir = mode;
582	io->eof = io->err = 0;
583	f = funopen(io, _ftp_readfn, _ftp_writefn, _ftp_seekfn, _ftp_closefn);
584	if (f == NULL)
585		free(io);
586	return (f);
587}
588
589/*
590 * Transfer file
591 */
592static FILE *
593_ftp_transfer(conn_t *conn, const char *oper, const char *file,
594    int mode, off_t offset, const char *flags)
595{
596	struct sockaddr_storage sa;
597	struct sockaddr_in6 *sin6;
598	struct sockaddr_in *sin4;
599	const char *bindaddr;
600	const char *filename;
601	int filenamelen, type;
602	int low, pasv, verbose;
603	int e, sd = -1;
604	socklen_t l;
605	char *s;
606	FILE *df;
607
608	/* check flags */
609	low = CHECK_FLAG('l');
610	pasv = CHECK_FLAG('p');
611	verbose = CHECK_FLAG('v');
612
613	/* passive mode */
614	if (!pasv)
615		pasv = ((s = getenv("FTP_PASSIVE_MODE")) != NULL &&
616		    strncasecmp(s, "no", 2) != 0);
617
618	/* isolate filename */
619	filename = _ftp_filename(file, &filenamelen, &type);
620
621	/* set transfer mode and data type */
622	if ((e = _ftp_mode_type(conn, 0, type)) != FTP_OK)
623		goto ouch;
624
625	/* find our own address, bind, and listen */
626	l = sizeof(sa);
627	if (getsockname(conn->sd, (struct sockaddr *)&sa, &l) == -1)
628		goto sysouch;
629	if (sa.ss_family == AF_INET6)
630		unmappedaddr((struct sockaddr_in6 *)&sa);
631
632	/* open data socket */
633	if ((sd = socket(sa.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
634		_fetch_syserr();
635		return (NULL);
636	}
637
638	if (pasv) {
639		u_char addr[64];
640		char *ln, *p;
641		unsigned int i;
642		int port;
643
644		/* send PASV command */
645		if (verbose)
646			_fetch_info("setting passive mode");
647		switch (sa.ss_family) {
648		case AF_INET:
649			if ((e = _ftp_cmd(conn, "PASV")) != FTP_PASSIVE_MODE)
650				goto ouch;
651			break;
652		case AF_INET6:
653			if ((e = _ftp_cmd(conn, "EPSV")) != FTP_EPASSIVE_MODE) {
654				if (e == -1)
655					goto ouch;
656				if ((e = _ftp_cmd(conn, "LPSV")) !=
657				    FTP_LPASSIVE_MODE)
658					goto ouch;
659			}
660			break;
661		default:
662			e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
663			goto ouch;
664		}
665
666		/*
667		 * Find address and port number. The reply to the PASV command
668		 * is IMHO the one and only weak point in the FTP protocol.
669		 */
670		ln = conn->buf;
671		switch (e) {
672		case FTP_PASSIVE_MODE:
673		case FTP_LPASSIVE_MODE:
674			for (p = ln + 3; *p && !isdigit(*p); p++)
675				/* nothing */ ;
676			if (!*p) {
677				e = FTP_PROTOCOL_ERROR;
678				goto ouch;
679			}
680			l = (e == FTP_PASSIVE_MODE ? 6 : 21);
681			for (i = 0; *p && i < l; i++, p++)
682				addr[i] = strtol(p, &p, 10);
683			if (i < l) {
684				e = FTP_PROTOCOL_ERROR;
685				goto ouch;
686			}
687			break;
688		case FTP_EPASSIVE_MODE:
689			for (p = ln + 3; *p && *p != '('; p++)
690				/* nothing */ ;
691			if (!*p) {
692				e = FTP_PROTOCOL_ERROR;
693				goto ouch;
694			}
695			++p;
696			if (sscanf(p, "%c%c%c%d%c", &addr[0], &addr[1], &addr[2],
697				&port, &addr[3]) != 5 ||
698			    addr[0] != addr[1] ||
699			    addr[0] != addr[2] || addr[0] != addr[3]) {
700				e = FTP_PROTOCOL_ERROR;
701				goto ouch;
702			}
703			break;
704		}
705
706		/* seek to required offset */
707		if (offset)
708			if (_ftp_cmd(conn, "REST %lu", (u_long)offset) != FTP_FILE_OK)
709				goto sysouch;
710
711		/* construct sockaddr for data socket */
712		l = sizeof(sa);
713		if (getpeername(conn->sd, (struct sockaddr *)&sa, &l) == -1)
714			goto sysouch;
715		if (sa.ss_family == AF_INET6)
716			unmappedaddr((struct sockaddr_in6 *)&sa);
717		switch (sa.ss_family) {
718		case AF_INET6:
719			sin6 = (struct sockaddr_in6 *)&sa;
720			if (e == FTP_EPASSIVE_MODE)
721				sin6->sin6_port = htons(port);
722			else {
723				bcopy(addr + 2, (char *)&sin6->sin6_addr, 16);
724				bcopy(addr + 19, (char *)&sin6->sin6_port, 2);
725			}
726			break;
727		case AF_INET:
728			sin4 = (struct sockaddr_in *)&sa;
729			if (e == FTP_EPASSIVE_MODE)
730				sin4->sin_port = htons(port);
731			else {
732				bcopy(addr, (char *)&sin4->sin_addr, 4);
733				bcopy(addr + 4, (char *)&sin4->sin_port, 2);
734			}
735			break;
736		default:
737			e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
738			break;
739		}
740
741		/* connect to data port */
742		if (verbose)
743			_fetch_info("opening data connection");
744		bindaddr = getenv("FETCH_BIND_ADDRESS");
745		if (bindaddr != NULL && *bindaddr != '\0' &&
746		    _fetch_bind(sd, sa.ss_family, bindaddr) != 0)
747			goto sysouch;
748		if (connect(sd, (struct sockaddr *)&sa, sa.ss_len) == -1)
749			goto sysouch;
750
751		/* make the server initiate the transfer */
752		if (verbose)
753			_fetch_info("initiating transfer");
754		e = _ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
755		if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
756			goto ouch;
757
758	} else {
759		u_int32_t a;
760		u_short p;
761		int arg, d;
762		char *ap;
763		char hname[INET6_ADDRSTRLEN];
764
765		switch (sa.ss_family) {
766		case AF_INET6:
767			((struct sockaddr_in6 *)&sa)->sin6_port = 0;
768#ifdef IPV6_PORTRANGE
769			arg = low ? IPV6_PORTRANGE_DEFAULT : IPV6_PORTRANGE_HIGH;
770			if (setsockopt(sd, IPPROTO_IPV6, IPV6_PORTRANGE,
771				(char *)&arg, sizeof(arg)) == -1)
772				goto sysouch;
773#endif
774			break;
775		case AF_INET:
776			((struct sockaddr_in *)&sa)->sin_port = 0;
777			arg = low ? IP_PORTRANGE_DEFAULT : IP_PORTRANGE_HIGH;
778			if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE,
779				(char *)&arg, sizeof(arg)) == -1)
780				goto sysouch;
781			break;
782		}
783		if (verbose)
784			_fetch_info("binding data socket");
785		if (bind(sd, (struct sockaddr *)&sa, sa.ss_len) == -1)
786			goto sysouch;
787		if (listen(sd, 1) == -1)
788			goto sysouch;
789
790		/* find what port we're on and tell the server */
791		if (getsockname(sd, (struct sockaddr *)&sa, &l) == -1)
792			goto sysouch;
793		switch (sa.ss_family) {
794		case AF_INET:
795			sin4 = (struct sockaddr_in *)&sa;
796			a = ntohl(sin4->sin_addr.s_addr);
797			p = ntohs(sin4->sin_port);
798			e = _ftp_cmd(conn, "PORT %d,%d,%d,%d,%d,%d",
799			    (a >> 24) & 0xff, (a >> 16) & 0xff,
800			    (a >> 8) & 0xff, a & 0xff,
801			    (p >> 8) & 0xff, p & 0xff);
802			break;
803		case AF_INET6:
804#define UC(b)	(((int)b)&0xff)
805			e = -1;
806			sin6 = (struct sockaddr_in6 *)&sa;
807			sin6->sin6_scope_id = 0;
808			if (getnameinfo((struct sockaddr *)&sa, sa.ss_len,
809				hname, sizeof(hname),
810				NULL, 0, NI_NUMERICHOST) == 0) {
811				e = _ftp_cmd(conn, "EPRT |%d|%s|%d|", 2, hname,
812				    htons(sin6->sin6_port));
813				if (e == -1)
814					goto ouch;
815			}
816			if (e != FTP_OK) {
817				ap = (char *)&sin6->sin6_addr;
818				e = _ftp_cmd(conn,
819				    "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
820				    6, 16,
821				    UC(ap[0]), UC(ap[1]), UC(ap[2]), UC(ap[3]),
822				    UC(ap[4]), UC(ap[5]), UC(ap[6]), UC(ap[7]),
823				    UC(ap[8]), UC(ap[9]), UC(ap[10]), UC(ap[11]),
824				    UC(ap[12]), UC(ap[13]), UC(ap[14]), UC(ap[15]),
825				    2,
826				    (ntohs(sin6->sin6_port) >> 8) & 0xff,
827				    ntohs(sin6->sin6_port)        & 0xff);
828			}
829			break;
830		default:
831			e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
832			goto ouch;
833		}
834		if (e != FTP_OK)
835			goto ouch;
836
837		/* seek to required offset */
838		if (offset)
839			if (_ftp_cmd(conn, "REST %ju", (uintmax_t)offset) != FTP_FILE_OK)
840				goto sysouch;
841
842		/* make the server initiate the transfer */
843		if (verbose)
844			_fetch_info("initiating transfer");
845		e = _ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
846		if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
847			goto ouch;
848
849		/* accept the incoming connection and go to town */
850		if ((d = accept(sd, NULL, NULL)) == -1)
851			goto sysouch;
852		close(sd);
853		sd = d;
854	}
855
856	if ((df = _ftp_setup(conn, _fetch_reopen(sd), mode)) == NULL)
857		goto sysouch;
858	return (df);
859
860sysouch:
861	_fetch_syserr();
862	if (sd >= 0)
863		close(sd);
864	return (NULL);
865
866ouch:
867	if (e != -1)
868		_ftp_seterr(e);
869	if (sd >= 0)
870		close(sd);
871	return (NULL);
872}
873
874/*
875 * Authenticate
876 */
877static int
878_ftp_authenticate(conn_t *conn, struct url *url, struct url *purl)
879{
880	const char *user, *pwd, *logname;
881	char pbuf[MAXHOSTNAMELEN + MAXLOGNAME + 1];
882	int e, len;
883
884	/* XXX FTP_AUTH, and maybe .netrc */
885
886	/* send user name and password */
887	if (url->user[0] == '\0')
888		_fetch_netrc_auth(url);
889	user = url->user;
890	if (*user == '\0')
891		user = getenv("FTP_LOGIN");
892	if (user == NULL || *user == '\0')
893		user = FTP_ANONYMOUS_USER;
894	if (purl && url->port == _fetch_default_port(url->scheme))
895		e = _ftp_cmd(conn, "USER %s@%s", user, url->host);
896	else if (purl)
897		e = _ftp_cmd(conn, "USER %s@%s@%d", user, url->host, url->port);
898	else
899		e = _ftp_cmd(conn, "USER %s", user);
900
901	/* did the server request a password? */
902	if (e == FTP_NEED_PASSWORD) {
903		pwd = url->pwd;
904		if (*pwd == '\0')
905			pwd = getenv("FTP_PASSWORD");
906		if (pwd == NULL || *pwd == '\0') {
907			if ((logname = getlogin()) == 0)
908				logname = FTP_ANONYMOUS_USER;
909			if ((len = snprintf(pbuf, MAXLOGNAME + 1, "%s@", logname)) < 0)
910				len = 0;
911			else if (len > MAXLOGNAME)
912				len = MAXLOGNAME;
913			gethostname(pbuf + len, sizeof(pbuf) - len);
914			pwd = pbuf;
915		}
916		e = _ftp_cmd(conn, "PASS %s", pwd);
917	}
918
919	return (e);
920}
921
922/*
923 * Log on to FTP server
924 */
925static conn_t *
926_ftp_connect(struct url *url, struct url *purl, const char *flags)
927{
928	conn_t *conn;
929	int e, direct, verbose;
930#ifdef INET6
931	int af = AF_UNSPEC;
932#else
933	int af = AF_INET;
934#endif
935
936	direct = CHECK_FLAG('d');
937	verbose = CHECK_FLAG('v');
938	if (CHECK_FLAG('4'))
939		af = AF_INET;
940	else if (CHECK_FLAG('6'))
941		af = AF_INET6;
942
943	if (direct)
944		purl = NULL;
945
946	/* check for proxy */
947	if (purl) {
948		/* XXX proxy authentication! */
949		conn = _fetch_connect(purl->host, purl->port, af, verbose);
950	} else {
951		/* no proxy, go straight to target */
952		conn = _fetch_connect(url->host, url->port, af, verbose);
953		purl = NULL;
954	}
955
956	/* check connection */
957	if (conn == NULL)
958		/* _fetch_connect() has already set an error code */
959		return (NULL);
960
961	/* expect welcome message */
962	if ((e = _ftp_chkerr(conn)) != FTP_SERVICE_READY)
963		goto fouch;
964
965	/* authenticate */
966	if ((e = _ftp_authenticate(conn, url, purl)) != FTP_LOGGED_IN)
967		goto fouch;
968
969	/* done */
970	return (conn);
971
972fouch:
973	if (e != -1)
974		_ftp_seterr(e);
975	_fetch_close(conn);
976	return (NULL);
977}
978
979/*
980 * Disconnect from server
981 */
982static void
983_ftp_disconnect(conn_t *conn)
984{
985	(void)_ftp_cmd(conn, "QUIT");
986	if (conn == cached_connection && conn->ref == 1)
987		cached_connection = NULL;
988	_fetch_close(conn);
989}
990
991/*
992 * Check if we're already connected
993 */
994static int
995_ftp_isconnected(struct url *url)
996{
997	return (cached_connection
998	    && (strcmp(url->host, cached_host.host) == 0)
999	    && (strcmp(url->user, cached_host.user) == 0)
1000	    && (strcmp(url->pwd, cached_host.pwd) == 0)
1001	    && (url->port == cached_host.port));
1002}
1003
1004/*
1005 * Check the cache, reconnect if no luck
1006 */
1007static conn_t *
1008_ftp_cached_connect(struct url *url, struct url *purl, const char *flags)
1009{
1010	conn_t *conn;
1011	int e;
1012
1013	/* set default port */
1014	if (!url->port)
1015		url->port = _fetch_default_port(url->scheme);
1016
1017	/* try to use previously cached connection */
1018	if (_ftp_isconnected(url)) {
1019		e = _ftp_cmd(cached_connection, "NOOP");
1020		if (e == FTP_OK || e == FTP_SYNTAX_ERROR)
1021			return (_fetch_ref(cached_connection));
1022	}
1023
1024	/* connect to server */
1025	if ((conn = _ftp_connect(url, purl, flags)) == NULL)
1026		return (NULL);
1027	if (cached_connection)
1028		_ftp_disconnect(cached_connection);
1029	cached_connection = _fetch_ref(conn);
1030	memcpy(&cached_host, url, sizeof(*url));
1031	return (conn);
1032}
1033
1034/*
1035 * Check the proxy settings
1036 */
1037static struct url *
1038_ftp_get_proxy(const char *flags)
1039{
1040	struct url *purl;
1041	char *p;
1042
1043	if (flags != NULL && strchr(flags, 'd') != NULL)
1044		return (NULL);
1045	if (((p = getenv("FTP_PROXY")) || (p = getenv("ftp_proxy")) ||
1046		(p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
1047	    *p && (purl = fetchParseURL(p)) != NULL) {
1048		if (!*purl->scheme) {
1049			if (getenv("FTP_PROXY") || getenv("ftp_proxy"))
1050				strcpy(purl->scheme, SCHEME_FTP);
1051			else
1052				strcpy(purl->scheme, SCHEME_HTTP);
1053		}
1054		if (!purl->port)
1055			purl->port = _fetch_default_proxy_port(purl->scheme);
1056		if (strcasecmp(purl->scheme, SCHEME_FTP) == 0 ||
1057		    strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
1058			return (purl);
1059		fetchFreeURL(purl);
1060	}
1061	return (NULL);
1062}
1063
1064/*
1065 * Process an FTP request
1066 */
1067FILE *
1068_ftp_request(struct url *url, const char *op, struct url_stat *us,
1069    struct url *purl, const char *flags)
1070{
1071	conn_t *conn;
1072	int oflag;
1073
1074	/* check if we should use HTTP instead */
1075	if (purl && strcasecmp(purl->scheme, SCHEME_HTTP) == 0) {
1076		if (strcmp(op, "STAT") == 0)
1077			return (_http_request(url, "HEAD", us, purl, flags));
1078		else if (strcmp(op, "RETR") == 0)
1079			return (_http_request(url, "GET", us, purl, flags));
1080		/*
1081		 * Our HTTP code doesn't support PUT requests yet, so try
1082		 * a direct connection.
1083		 */
1084	}
1085
1086	/* connect to server */
1087	conn = _ftp_cached_connect(url, purl, flags);
1088	if (purl)
1089		fetchFreeURL(purl);
1090	if (conn == NULL)
1091		return (NULL);
1092
1093	/* change directory */
1094	if (_ftp_cwd(conn, url->doc) == -1)
1095		return (NULL);
1096
1097	/* stat file */
1098	if (us && _ftp_stat(conn, url->doc, us) == -1
1099	    && fetchLastErrCode != FETCH_PROTO
1100	    && fetchLastErrCode != FETCH_UNAVAIL)
1101		return (NULL);
1102
1103	/* just a stat */
1104	if (strcmp(op, "STAT") == 0)
1105		return (FILE *)1; /* bogus return value */
1106	if (strcmp(op, "STOR") == 0 || strcmp(op, "APPE") == 0)
1107		oflag = O_WRONLY;
1108	else
1109		oflag = O_RDONLY;
1110
1111	/* initiate the transfer */
1112	return (_ftp_transfer(conn, op, url->doc, oflag, url->offset, flags));
1113}
1114
1115/*
1116 * Get and stat file
1117 */
1118FILE *
1119fetchXGetFTP(struct url *url, struct url_stat *us, const char *flags)
1120{
1121	return (_ftp_request(url, "RETR", us, _ftp_get_proxy(flags), flags));
1122}
1123
1124/*
1125 * Get file
1126 */
1127FILE *
1128fetchGetFTP(struct url *url, const char *flags)
1129{
1130	return (fetchXGetFTP(url, NULL, flags));
1131}
1132
1133/*
1134 * Put file
1135 */
1136FILE *
1137fetchPutFTP(struct url *url, const char *flags)
1138{
1139
1140	return (_ftp_request(url, CHECK_FLAG('a') ? "APPE" : "STOR", NULL,
1141	    _ftp_get_proxy(flags), flags));
1142}
1143
1144/*
1145 * Get file stats
1146 */
1147int
1148fetchStatFTP(struct url *url, struct url_stat *us, const char *flags)
1149{
1150	FILE *f;
1151
1152	f = _ftp_request(url, "STAT", us, _ftp_get_proxy(flags), flags);
1153	if (f == NULL)
1154		return (-1);
1155	return (0);
1156}
1157
1158/*
1159 * List a directory
1160 */
1161struct url_ent *
1162fetchListFTP(struct url *url __unused, const char *flags __unused)
1163{
1164	warnx("fetchListFTP(): not implemented");
1165	return (NULL);
1166}
1167