Home | History | Annotate | Line # | Download | only in faithd
ftp.c revision 1.8
      1  1.8  itojun /*	$NetBSD: ftp.c,v 1.8 2002/04/24 12:14:42 itojun Exp $	*/
      2  1.8  itojun /*	$KAME: ftp.c,v 1.14 2002/04/24 08:17:23 itojun Exp $	*/
      3  1.1  itojun 
      4  1.1  itojun /*
      5  1.1  itojun  * Copyright (C) 1997 and 1998 WIDE Project.
      6  1.1  itojun  * All rights reserved.
      7  1.5  itojun  *
      8  1.1  itojun  * Redistribution and use in source and binary forms, with or without
      9  1.1  itojun  * modification, are permitted provided that the following conditions
     10  1.1  itojun  * are met:
     11  1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     12  1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     13  1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  itojun  *    documentation and/or other materials provided with the distribution.
     16  1.1  itojun  * 3. Neither the name of the project nor the names of its contributors
     17  1.1  itojun  *    may be used to endorse or promote products derived from this software
     18  1.1  itojun  *    without specific prior written permission.
     19  1.5  itojun  *
     20  1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  1.1  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  1.1  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.1  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  1.1  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.1  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  1.1  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.1  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  1.1  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  1.1  itojun  * SUCH DAMAGE.
     31  1.1  itojun  */
     32  1.1  itojun 
     33  1.1  itojun #include <sys/param.h>
     34  1.1  itojun #include <sys/types.h>
     35  1.1  itojun #include <sys/socket.h>
     36  1.1  itojun #include <sys/ioctl.h>
     37  1.1  itojun #include <sys/time.h>
     38  1.1  itojun 
     39  1.1  itojun #include <stdio.h>
     40  1.1  itojun #include <stdlib.h>
     41  1.1  itojun #include <string.h>
     42  1.1  itojun #include <syslog.h>
     43  1.1  itojun #include <unistd.h>
     44  1.1  itojun #include <errno.h>
     45  1.1  itojun #include <ctype.h>
     46  1.1  itojun 
     47  1.1  itojun #include <netinet/in.h>
     48  1.1  itojun #include <arpa/inet.h>
     49  1.1  itojun #include <netdb.h>
     50  1.1  itojun 
     51  1.1  itojun #include "faithd.h"
     52  1.1  itojun 
     53  1.1  itojun static char rbuf[MSS];
     54  1.1  itojun static char sbuf[MSS];
     55  1.1  itojun static int passivemode = 0;
     56  1.1  itojun static int wport4 = -1;			/* listen() to active */
     57  1.1  itojun static int wport6 = -1;			/* listen() to passive */
     58  1.1  itojun static int port4 = -1;			/* active: inbound  passive: outbound */
     59  1.1  itojun static int port6 = -1;			/* active: outbound  passive: inbound */
     60  1.1  itojun static struct sockaddr_storage data4;	/* server data address */
     61  1.1  itojun static struct sockaddr_storage data6;	/* client data address */
     62  1.1  itojun static int epsvall = 0;
     63  1.1  itojun 
     64  1.1  itojun #ifdef FAITH4
     65  1.1  itojun enum state { NONE, LPRT, EPRT, PORT, LPSV, EPSV, PASV };
     66  1.1  itojun #else
     67  1.1  itojun enum state { NONE, LPRT, EPRT, LPSV, EPSV };
     68  1.1  itojun #endif
     69  1.1  itojun 
     70  1.1  itojun static int ftp_activeconn __P((void));
     71  1.1  itojun static int ftp_passiveconn __P((void));
     72  1.1  itojun static int ftp_copy __P((int, int));
     73  1.1  itojun static int ftp_copyresult __P((int, int, enum state));
     74  1.1  itojun static int ftp_copycommand __P((int, int, enum state *));
     75  1.1  itojun 
     76  1.1  itojun void
     77  1.1  itojun ftp_relay(int ctl6, int ctl4)
     78  1.1  itojun {
     79  1.1  itojun 	fd_set readfds;
     80  1.1  itojun 	int error;
     81  1.1  itojun 	enum state state = NONE;
     82  1.1  itojun 	struct timeval tv;
     83  1.1  itojun 
     84  1.1  itojun 	syslog(LOG_INFO, "starting ftp control connection");
     85  1.1  itojun 
     86  1.1  itojun 	for (;;) {
     87  1.7  itojun 		int maxfd = 0;
     88  1.7  itojun 
     89  1.1  itojun 		FD_ZERO(&readfds);
     90  1.1  itojun 		FD_SET(ctl4, &readfds);
     91  1.1  itojun 		FD_SET(ctl6, &readfds);
     92  1.7  itojun 		if (0 <= port4) {
     93  1.1  itojun 			FD_SET(port4, &readfds);
     94  1.7  itojun 			if (port4 > maxfd)
     95  1.7  itojun 				maxfd = port4;
     96  1.7  itojun 		}
     97  1.7  itojun 		if (0 <= port6) {
     98  1.1  itojun 			FD_SET(port6, &readfds);
     99  1.7  itojun 			if (port6 > maxfd)
    100  1.7  itojun 				maxfd = port6;
    101  1.7  itojun 		}
    102  1.1  itojun #if 0
    103  1.7  itojun 		if (0 <= wport4) {
    104  1.1  itojun 			FD_SET(wport4, &readfds);
    105  1.7  itojun 			if (wport4 > maxfd)
    106  1.7  itojun 				maxfd = wport4;
    107  1.7  itojun 		}
    108  1.7  itojun 		if (0 <= wport6) {
    109  1.1  itojun 			FD_SET(wport6, &readfds);
    110  1.7  itojun 			if (wport6 > maxfd)
    111  1.7  itojun 				maxfd = wport6;
    112  1.7  itojun 		}
    113  1.1  itojun #endif
    114  1.1  itojun 		tv.tv_sec = FAITH_TIMEOUT;
    115  1.1  itojun 		tv.tv_usec = 0;
    116  1.1  itojun 
    117  1.7  itojun 		error = select(maxfd + 1, &readfds, NULL, NULL, &tv);
    118  1.1  itojun 		if (error == -1)
    119  1.7  itojun 			exit_failure("select: %s", strerror(errno));
    120  1.1  itojun 		else if (error == 0)
    121  1.1  itojun 			exit_failure("connection timeout");
    122  1.1  itojun 
    123  1.1  itojun 		/*
    124  1.1  itojun 		 * The order of the following checks does (slightly) matter.
    125  1.1  itojun 		 * It is important to visit all checks (do not use "continue"),
    126  1.1  itojun 		 * otherwise some of the pipe may become full and we cannot
    127  1.1  itojun 		 * relay correctly.
    128  1.1  itojun 		 */
    129  1.1  itojun 		if (FD_ISSET(ctl6, &readfds)) {
    130  1.1  itojun 			/*
    131  1.1  itojun 			 * copy control connection from the client.
    132  1.1  itojun 			 * command translation is necessary.
    133  1.1  itojun 			 */
    134  1.1  itojun 			error = ftp_copycommand(ctl6, ctl4, &state);
    135  1.1  itojun 
    136  1.1  itojun 			switch (error) {
    137  1.1  itojun 			case -1:
    138  1.1  itojun 				goto bad;
    139  1.1  itojun 			case 0:
    140  1.1  itojun 				close(ctl4);
    141  1.1  itojun 				close(ctl6);
    142  1.1  itojun 				exit_success("terminating ftp control connection");
    143  1.1  itojun 				/*NOTREACHED*/
    144  1.1  itojun 			default:
    145  1.1  itojun 				break;
    146  1.1  itojun 			}
    147  1.1  itojun 		}
    148  1.1  itojun 		if (FD_ISSET(ctl4, &readfds)) {
    149  1.1  itojun 			/*
    150  1.1  itojun 			 * copy control connection from the server
    151  1.1  itojun 			 * translation of result code is necessary.
    152  1.1  itojun 			 */
    153  1.1  itojun 			error = ftp_copyresult(ctl4, ctl6, state);
    154  1.1  itojun 
    155  1.1  itojun 			switch (error) {
    156  1.1  itojun 			case -1:
    157  1.1  itojun 				goto bad;
    158  1.1  itojun 			case 0:
    159  1.1  itojun 				close(ctl4);
    160  1.1  itojun 				close(ctl6);
    161  1.1  itojun 				exit_success("terminating ftp control connection");
    162  1.1  itojun 				/*NOTREACHED*/
    163  1.1  itojun 			default:
    164  1.1  itojun 				break;
    165  1.1  itojun 			}
    166  1.1  itojun 		}
    167  1.1  itojun 		if (0 <= port4 && 0 <= port6 && FD_ISSET(port4, &readfds)) {
    168  1.1  itojun 			/*
    169  1.1  itojun 			 * copy data connection.
    170  1.1  itojun 			 * no special treatment necessary.
    171  1.1  itojun 			 */
    172  1.1  itojun 			if (FD_ISSET(port4, &readfds))
    173  1.1  itojun 				error = ftp_copy(port4, port6);
    174  1.1  itojun 			switch (error) {
    175  1.1  itojun 			case -1:
    176  1.1  itojun 				goto bad;
    177  1.1  itojun 			case 0:
    178  1.1  itojun 				close(port4);
    179  1.1  itojun 				close(port6);
    180  1.1  itojun 				port4 = port6 = -1;
    181  1.1  itojun 				syslog(LOG_INFO, "terminating data connection");
    182  1.1  itojun 				break;
    183  1.1  itojun 			default:
    184  1.1  itojun 				break;
    185  1.1  itojun 			}
    186  1.1  itojun 		}
    187  1.1  itojun 		if (0 <= port4 && 0 <= port6 && FD_ISSET(port6, &readfds)) {
    188  1.1  itojun 			/*
    189  1.1  itojun 			 * copy data connection.
    190  1.1  itojun 			 * no special treatment necessary.
    191  1.1  itojun 			 */
    192  1.1  itojun 			if (FD_ISSET(port6, &readfds))
    193  1.1  itojun 				error = ftp_copy(port6, port4);
    194  1.1  itojun 			switch (error) {
    195  1.1  itojun 			case -1:
    196  1.1  itojun 				goto bad;
    197  1.1  itojun 			case 0:
    198  1.1  itojun 				close(port4);
    199  1.1  itojun 				close(port6);
    200  1.1  itojun 				port4 = port6 = -1;
    201  1.1  itojun 				syslog(LOG_INFO, "terminating data connection");
    202  1.1  itojun 				break;
    203  1.1  itojun 			default:
    204  1.1  itojun 				break;
    205  1.1  itojun 			}
    206  1.1  itojun 		}
    207  1.1  itojun #if 0
    208  1.1  itojun 		if (wport4 && FD_ISSET(wport4, &readfds)) {
    209  1.1  itojun 			/*
    210  1.1  itojun 			 * establish active data connection from the server.
    211  1.1  itojun 			 */
    212  1.1  itojun 			ftp_activeconn();
    213  1.1  itojun 		}
    214  1.1  itojun 		if (wport6 && FD_ISSET(wport6, &readfds)) {
    215  1.1  itojun 			/*
    216  1.1  itojun 			 * establish passive data connection from the client.
    217  1.1  itojun 			 */
    218  1.1  itojun 			ftp_passiveconn();
    219  1.1  itojun 		}
    220  1.1  itojun #endif
    221  1.1  itojun 	}
    222  1.1  itojun 
    223  1.1  itojun  bad:
    224  1.7  itojun 	exit_failure("%s", strerror(errno));
    225  1.1  itojun }
    226  1.1  itojun 
    227  1.1  itojun static int
    228  1.1  itojun ftp_activeconn()
    229  1.1  itojun {
    230  1.1  itojun 	int n;
    231  1.1  itojun 	int error;
    232  1.1  itojun 	fd_set set;
    233  1.1  itojun 	struct timeval timeout;
    234  1.2  itojun 	struct sockaddr *sa;
    235  1.1  itojun 
    236  1.1  itojun 	/* get active connection from server */
    237  1.1  itojun 	FD_ZERO(&set);
    238  1.1  itojun 	FD_SET(wport4, &set);
    239  1.1  itojun 	timeout.tv_sec = 120;
    240  1.1  itojun 	timeout.tv_usec = -1;
    241  1.1  itojun 	n = sizeof(data4);
    242  1.1  itojun 	if (select(wport4 + 1, &set, NULL, NULL, &timeout) == 0
    243  1.1  itojun 	 || (port4 = accept(wport4, (struct sockaddr *)&data4, &n)) < 0) {
    244  1.1  itojun 		close(wport4);
    245  1.1  itojun 		wport4 = -1;
    246  1.1  itojun 		syslog(LOG_INFO, "active mode data connection failed");
    247  1.1  itojun 		return -1;
    248  1.1  itojun 	}
    249  1.1  itojun 
    250  1.1  itojun 	/* ask active connection to client */
    251  1.2  itojun 	sa = (struct sockaddr *)&data6;
    252  1.2  itojun 	port6 = socket(sa->sa_family, SOCK_STREAM, 0);
    253  1.1  itojun 	if (port6 == -1) {
    254  1.1  itojun 		close(port4);
    255  1.1  itojun 		close(wport4);
    256  1.1  itojun 		port4 = wport4 = -1;
    257  1.1  itojun 		syslog(LOG_INFO, "active mode data connection failed");
    258  1.1  itojun 		return -1;
    259  1.1  itojun 	}
    260  1.2  itojun 	error = connect(port6, sa, sa->sa_len);
    261  1.8  itojun 	if (error < 0) {
    262  1.1  itojun 		close(port6);
    263  1.1  itojun 		close(port4);
    264  1.1  itojun 		close(wport4);
    265  1.1  itojun 		port6 = port4 = wport4 = -1;
    266  1.1  itojun 		syslog(LOG_INFO, "active mode data connection failed");
    267  1.1  itojun 		return -1;
    268  1.1  itojun 	}
    269  1.1  itojun 
    270  1.1  itojun 	syslog(LOG_INFO, "active mode data connection established");
    271  1.1  itojun 	return 0;
    272  1.1  itojun }
    273  1.1  itojun 
    274  1.1  itojun static int
    275  1.1  itojun ftp_passiveconn()
    276  1.1  itojun {
    277  1.1  itojun 	int n;
    278  1.1  itojun 	int error;
    279  1.1  itojun 	fd_set set;
    280  1.1  itojun 	struct timeval timeout;
    281  1.2  itojun 	struct sockaddr *sa;
    282  1.1  itojun 
    283  1.1  itojun 	/* get passive connection from client */
    284  1.1  itojun 	FD_ZERO(&set);
    285  1.1  itojun 	FD_SET(wport6, &set);
    286  1.1  itojun 	timeout.tv_sec = 120;
    287  1.1  itojun 	timeout.tv_usec = 0;
    288  1.1  itojun 	n = sizeof(data6);
    289  1.1  itojun 	if (select(wport6 + 1, &set, NULL, NULL, &timeout) == 0
    290  1.1  itojun 	 || (port6 = accept(wport6, (struct sockaddr *)&data6, &n)) < 0) {
    291  1.1  itojun 		close(wport6);
    292  1.1  itojun 		wport6 = -1;
    293  1.1  itojun 		syslog(LOG_INFO, "passive mode data connection failed");
    294  1.1  itojun 		return -1;
    295  1.1  itojun 	}
    296  1.1  itojun 
    297  1.1  itojun 	/* ask passive connection to server */
    298  1.2  itojun 	sa = (struct sockaddr *)&data4;
    299  1.2  itojun 	port4 = socket(sa->sa_family, SOCK_STREAM, 0);
    300  1.1  itojun 	if (port4 == -1) {
    301  1.1  itojun 		close(wport6);
    302  1.1  itojun 		close(port6);
    303  1.1  itojun 		wport6 = port6 = -1;
    304  1.1  itojun 		syslog(LOG_INFO, "passive mode data connection failed");
    305  1.1  itojun 		return -1;
    306  1.1  itojun 	}
    307  1.2  itojun 	error = connect(port4, sa, sa->sa_len);
    308  1.8  itojun 	if (error < 0) {
    309  1.1  itojun 		close(wport6);
    310  1.1  itojun 		close(port4);
    311  1.1  itojun 		close(port6);
    312  1.1  itojun 		wport6 = port4 = port6 = -1;
    313  1.1  itojun 		syslog(LOG_INFO, "passive mode data connection failed");
    314  1.1  itojun 		return -1;
    315  1.1  itojun 	}
    316  1.1  itojun 
    317  1.1  itojun 	syslog(LOG_INFO, "passive mode data connection established");
    318  1.1  itojun 	return 0;
    319  1.1  itojun }
    320  1.1  itojun 
    321  1.1  itojun static int
    322  1.1  itojun ftp_copy(int src, int dst)
    323  1.1  itojun {
    324  1.1  itojun 	int error, atmark;
    325  1.1  itojun 	int n;
    326  1.1  itojun 
    327  1.1  itojun 	/* OOB data handling */
    328  1.1  itojun 	error = ioctl(src, SIOCATMARK, &atmark);
    329  1.1  itojun 	if (error != -1 && atmark == 1) {
    330  1.1  itojun 		n = read(src, rbuf, 1);
    331  1.1  itojun 		if (n == -1)
    332  1.1  itojun 			goto bad;
    333  1.1  itojun 		send(dst, rbuf, n, MSG_OOB);
    334  1.1  itojun #if 0
    335  1.1  itojun 		n = read(src, rbuf, sizeof(rbuf));
    336  1.1  itojun 		if (n == -1)
    337  1.1  itojun 			goto bad;
    338  1.1  itojun 		write(dst, rbuf, n);
    339  1.1  itojun 		return n;
    340  1.1  itojun #endif
    341  1.1  itojun 	}
    342  1.1  itojun 
    343  1.1  itojun 	n = read(src, rbuf, sizeof(rbuf));
    344  1.1  itojun 	switch (n) {
    345  1.1  itojun 	case -1:
    346  1.1  itojun 	case 0:
    347  1.1  itojun 		return n;
    348  1.1  itojun 	default:
    349  1.1  itojun 		write(dst, rbuf, n);
    350  1.1  itojun 		return n;
    351  1.1  itojun 	}
    352  1.1  itojun 
    353  1.1  itojun  bad:
    354  1.7  itojun 	exit_failure("%s", strerror(errno));
    355  1.1  itojun 	/*NOTREACHED*/
    356  1.1  itojun 	return 0;	/* to make gcc happy */
    357  1.1  itojun }
    358  1.1  itojun 
    359  1.1  itojun static int
    360  1.1  itojun ftp_copyresult(int src, int dst, enum state state)
    361  1.1  itojun {
    362  1.1  itojun 	int error, atmark;
    363  1.1  itojun 	int n;
    364  1.1  itojun 	char *param;
    365  1.1  itojun 	int code;
    366  1.7  itojun 	char *a, *p;
    367  1.7  itojun 	int i;
    368  1.1  itojun 
    369  1.1  itojun 	/* OOB data handling */
    370  1.1  itojun 	error = ioctl(src, SIOCATMARK, &atmark);
    371  1.1  itojun 	if (error != -1 && atmark == 1) {
    372  1.1  itojun 		n = read(src, rbuf, 1);
    373  1.1  itojun 		if (n == -1)
    374  1.1  itojun 			goto bad;
    375  1.1  itojun 		send(dst, rbuf, n, MSG_OOB);
    376  1.1  itojun #if 0
    377  1.1  itojun 		n = read(src, rbuf, sizeof(rbuf));
    378  1.1  itojun 		if (n == -1)
    379  1.1  itojun 			goto bad;
    380  1.1  itojun 		write(dst, rbuf, n);
    381  1.1  itojun 		return n;
    382  1.1  itojun #endif
    383  1.1  itojun 	}
    384  1.1  itojun 
    385  1.1  itojun 	n = read(src, rbuf, sizeof(rbuf));
    386  1.1  itojun 	if (n <= 0)
    387  1.1  itojun 		return n;
    388  1.1  itojun 	rbuf[n] = '\0';
    389  1.1  itojun 
    390  1.1  itojun 	/*
    391  1.1  itojun 	 * parse argument
    392  1.1  itojun 	 */
    393  1.1  itojun 	p = rbuf;
    394  1.1  itojun 	for (i = 0; i < 3; i++) {
    395  1.1  itojun 		if (!isdigit(*p)) {
    396  1.1  itojun 			/* invalid reply */
    397  1.1  itojun 			write(dst, rbuf, n);
    398  1.1  itojun 			return n;
    399  1.1  itojun 		}
    400  1.1  itojun 		p++;
    401  1.1  itojun 	}
    402  1.1  itojun 	if (!isspace(*p)) {
    403  1.1  itojun 		/* invalid reply */
    404  1.1  itojun 		write(dst, rbuf, n);
    405  1.1  itojun 		return n;
    406  1.1  itojun 	}
    407  1.1  itojun 	code = atoi(rbuf);
    408  1.1  itojun 	param = p;
    409  1.1  itojun 	/* param points to first non-command token, if any */
    410  1.1  itojun 	while (*param && isspace(*param))
    411  1.1  itojun 		param++;
    412  1.1  itojun 	if (!*param)
    413  1.1  itojun 		param = NULL;
    414  1.1  itojun 
    415  1.1  itojun 	switch (state) {
    416  1.1  itojun 	case NONE:
    417  1.1  itojun 		if (!passivemode && rbuf[0] == '1') {
    418  1.1  itojun 			if (ftp_activeconn() < 0) {
    419  1.4  itojun 				n = snprintf(rbuf, sizeof(rbuf),
    420  1.1  itojun 					"425 Cannot open data connetion\r\n");
    421  1.7  itojun 				if (n < 0 || n >= sizeof(rbuf))
    422  1.7  itojun 					n = 0;
    423  1.1  itojun 			}
    424  1.1  itojun 		}
    425  1.7  itojun 		if (n)
    426  1.7  itojun 			write(dst, rbuf, n);
    427  1.1  itojun 		return n;
    428  1.1  itojun 	case LPRT:
    429  1.1  itojun 	case EPRT:
    430  1.1  itojun 		/* expecting "200 PORT command successful." */
    431  1.1  itojun 		if (code == 200) {
    432  1.1  itojun 			p = strstr(rbuf, "PORT");
    433  1.1  itojun 			if (p) {
    434  1.1  itojun 				p[0] = (state == LPRT) ? 'L' : 'E';
    435  1.1  itojun 				p[1] = 'P';
    436  1.1  itojun 			}
    437  1.1  itojun 		} else {
    438  1.1  itojun 			close(wport4);
    439  1.1  itojun 			wport4 = -1;
    440  1.1  itojun 		}
    441  1.1  itojun 		write(dst, rbuf, n);
    442  1.1  itojun 		return n;
    443  1.1  itojun #ifdef FAITH4
    444  1.1  itojun 	case PORT:
    445  1.1  itojun 		/* expecting "200 EPRT command successful." */
    446  1.1  itojun 		if (code == 200) {
    447  1.1  itojun 			p = strstr(rbuf, "EPRT");
    448  1.1  itojun 			if (p) {
    449  1.1  itojun 				p[0] = 'P';
    450  1.1  itojun 				p[1] = 'O';
    451  1.1  itojun 			}
    452  1.1  itojun 		} else {
    453  1.1  itojun 			close(wport4);
    454  1.1  itojun 			wport4 = -1;
    455  1.1  itojun 		}
    456  1.1  itojun 		write(dst, rbuf, n);
    457  1.1  itojun 		return n;
    458  1.1  itojun #endif
    459  1.1  itojun 	case LPSV:
    460  1.1  itojun 	case EPSV:
    461  1.6  itojun 		/*
    462  1.6  itojun 		 * expecting "227 Entering Passive Mode (x,x,x,x,x,x,x)"
    463  1.6  itojun 		 * (in some cases result comes without paren)
    464  1.6  itojun 		 */
    465  1.1  itojun 		if (code != 227) {
    466  1.1  itojun passivefail0:
    467  1.1  itojun 			close(wport6);
    468  1.1  itojun 			wport6 = -1;
    469  1.1  itojun 			write(dst, rbuf, n);
    470  1.1  itojun 			return n;
    471  1.1  itojun 		}
    472  1.1  itojun 
    473  1.1  itojun 	    {
    474  1.1  itojun 		unsigned int ho[4], po[2];
    475  1.1  itojun 		struct sockaddr_in *sin;
    476  1.1  itojun 		struct sockaddr_in6 *sin6;
    477  1.1  itojun 		u_short port;
    478  1.1  itojun 
    479  1.1  itojun 		/*
    480  1.1  itojun 		 * PASV result -> LPSV/EPSV result
    481  1.1  itojun 		 */
    482  1.1  itojun 		p = param;
    483  1.6  itojun 		while (*p && *p != '(' && !isdigit(*p))	/*)*/
    484  1.1  itojun 			p++;
    485  1.1  itojun 		if (!*p)
    486  1.1  itojun 			goto passivefail0;	/*XXX*/
    487  1.6  itojun 		if (*p == '(')	/*)*/
    488  1.6  itojun 			p++;
    489  1.1  itojun 		n = sscanf(p, "%u,%u,%u,%u,%u,%u",
    490  1.1  itojun 			&ho[0], &ho[1], &ho[2], &ho[3], &po[0], &po[1]);
    491  1.1  itojun 		if (n != 6)
    492  1.1  itojun 			goto passivefail0;	/*XXX*/
    493  1.1  itojun 
    494  1.1  itojun 		/* keep PORT parameter */
    495  1.1  itojun 		memset(&data4, 0, sizeof(data4));
    496  1.1  itojun 		sin = (struct sockaddr_in *)&data4;
    497  1.1  itojun 		sin->sin_len = sizeof(*sin);
    498  1.1  itojun 		sin->sin_family = AF_INET;
    499  1.1  itojun 		sin->sin_addr.s_addr = 0;
    500  1.1  itojun 		for (n = 0; n < 4; n++) {
    501  1.1  itojun 			sin->sin_addr.s_addr |=
    502  1.1  itojun 				htonl((ho[n] & 0xff) << ((3 - n) * 8));
    503  1.1  itojun 		}
    504  1.1  itojun 		sin->sin_port = htons(((po[0] & 0xff) << 8) | (po[1] & 0xff));
    505  1.1  itojun 
    506  1.1  itojun 		/* get ready for passive data connection */
    507  1.1  itojun 		memset(&data6, 0, sizeof(data6));
    508  1.1  itojun 		sin6 = (struct sockaddr_in6 *)&data6;
    509  1.1  itojun 		sin6->sin6_len = sizeof(*sin6);
    510  1.1  itojun 		sin6->sin6_family = AF_INET6;
    511  1.1  itojun 		wport6 = socket(sin6->sin6_family, SOCK_STREAM, 0);
    512  1.1  itojun 		if (wport6 == -1) {
    513  1.1  itojun passivefail:
    514  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    515  1.1  itojun 				"500 could not translate from PASV\r\n");
    516  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    517  1.7  itojun 			if (n)
    518  1.7  itojun 				write(src, sbuf, n);
    519  1.1  itojun 			return n;
    520  1.1  itojun 		}
    521  1.1  itojun #ifdef IPV6_FAITH
    522  1.1  itojun 	    {
    523  1.1  itojun 		int on = 1;
    524  1.1  itojun 		error = setsockopt(wport6, IPPROTO_IPV6, IPV6_FAITH,
    525  1.1  itojun 			&on, sizeof(on));
    526  1.1  itojun 		if (error == -1)
    527  1.7  itojun 			exit_failure("setsockopt(IPV6_FAITH): %s", strerror(errno));
    528  1.1  itojun 	    }
    529  1.1  itojun #endif
    530  1.1  itojun 		error = bind(wport6, (struct sockaddr *)sin6, sin6->sin6_len);
    531  1.1  itojun 		if (error == -1) {
    532  1.1  itojun 			close(wport6);
    533  1.1  itojun 			wport6 = -1;
    534  1.1  itojun 			goto passivefail;
    535  1.1  itojun 		}
    536  1.1  itojun 		error = listen(wport6, 1);
    537  1.1  itojun 		if (error == -1) {
    538  1.1  itojun 			close(wport6);
    539  1.1  itojun 			wport6 = -1;
    540  1.1  itojun 			goto passivefail;
    541  1.1  itojun 		}
    542  1.1  itojun 
    543  1.1  itojun 		/* transmit LPSV or EPSV */
    544  1.1  itojun 		/*
    545  1.1  itojun 		 * addr from dst, port from wport6
    546  1.1  itojun 		 */
    547  1.1  itojun 		n = sizeof(data6);
    548  1.1  itojun 		error = getsockname(wport6, (struct sockaddr *)&data6, &n);
    549  1.1  itojun 		if (error == -1) {
    550  1.1  itojun 			close(wport6);
    551  1.1  itojun 			wport6 = -1;
    552  1.1  itojun 			goto passivefail;
    553  1.1  itojun 		}
    554  1.1  itojun 		sin6 = (struct sockaddr_in6 *)&data6;
    555  1.1  itojun 		port = sin6->sin6_port;
    556  1.1  itojun 
    557  1.1  itojun 		n = sizeof(data6);
    558  1.1  itojun 		error = getsockname(dst, (struct sockaddr *)&data6, &n);
    559  1.1  itojun 		if (error == -1) {
    560  1.1  itojun 			close(wport6);
    561  1.1  itojun 			wport6 = -1;
    562  1.1  itojun 			goto passivefail;
    563  1.1  itojun 		}
    564  1.1  itojun 		sin6 = (struct sockaddr_in6 *)&data6;
    565  1.1  itojun 		sin6->sin6_port = port;
    566  1.1  itojun 
    567  1.1  itojun 		if (state == LPSV) {
    568  1.1  itojun 			a = (char *)&sin6->sin6_addr;
    569  1.1  itojun 			p = (char *)&sin6->sin6_port;
    570  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    571  1.1  itojun "228 Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)\r\n",
    572  1.5  itojun 				6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
    573  1.5  itojun 				UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
    574  1.5  itojun 				UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
    575  1.5  itojun 				UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
    576  1.1  itojun 				2, UC(p[0]), UC(p[1]));
    577  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    578  1.7  itojun 				n = 0;
    579  1.7  itojun 			if (n)
    580  1.7  itojun 				write(dst, sbuf, n);
    581  1.1  itojun 			passivemode = 1;
    582  1.1  itojun 			return n;
    583  1.1  itojun 		} else {
    584  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    585  1.1  itojun "229 Entering Extended Passive Mode (|||%d|)\r\n",
    586  1.1  itojun 				ntohs(sin6->sin6_port));
    587  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    588  1.7  itojun 				n = 0;
    589  1.7  itojun 			if (n)
    590  1.7  itojun 				write(dst, sbuf, n);
    591  1.1  itojun 			passivemode = 1;
    592  1.1  itojun 			return n;
    593  1.1  itojun 		}
    594  1.1  itojun 	    }
    595  1.1  itojun #ifdef FAITH4
    596  1.1  itojun 	case PASV:
    597  1.1  itojun 		/* expecting "229 Entering Extended Passive Mode (|||x|)" */
    598  1.1  itojun 		if (code != 229) {
    599  1.1  itojun passivefail1:
    600  1.1  itojun 			close(wport6);
    601  1.1  itojun 			wport6 = -1;
    602  1.1  itojun 			write(dst, rbuf, n);
    603  1.1  itojun 			return n;
    604  1.1  itojun 		}
    605  1.1  itojun 
    606  1.1  itojun 	    {
    607  1.1  itojun 		u_short port;
    608  1.1  itojun 		struct sockaddr_in *sin;
    609  1.1  itojun 		struct sockaddr_in6 *sin6;
    610  1.1  itojun 
    611  1.1  itojun 		/*
    612  1.1  itojun 		 * EPSV result -> PORT result
    613  1.1  itojun 		 */
    614  1.1  itojun 		p = param;
    615  1.6  itojun 		while (*p && *p != '(')	/*)*/
    616  1.1  itojun 			p++;
    617  1.1  itojun 		if (!*p)
    618  1.1  itojun 			goto passivefail1;	/*XXX*/
    619  1.1  itojun 		p++;
    620  1.1  itojun 		n = sscanf(p, "|||%hu|", &port);
    621  1.1  itojun 		if (n != 1)
    622  1.1  itojun 			goto passivefail1;	/*XXX*/
    623  1.1  itojun 
    624  1.1  itojun 		/* keep EPRT parameter */
    625  1.1  itojun 		n = sizeof(data4);
    626  1.1  itojun 		error = getpeername(src, (struct sockaddr *)&data4, &n);
    627  1.1  itojun 		if (error == -1)
    628  1.1  itojun 			goto passivefail1;	/*XXX*/
    629  1.1  itojun 		sin6 = (struct sockaddr_in6 *)&data4;
    630  1.1  itojun 		sin6->sin6_port = htons(port);
    631  1.1  itojun 
    632  1.1  itojun 		/* get ready for passive data connection */
    633  1.1  itojun 		memset(&data6, 0, sizeof(data6));
    634  1.1  itojun 		sin = (struct sockaddr_in *)&data6;
    635  1.1  itojun 		sin->sin_len = sizeof(*sin);
    636  1.1  itojun 		sin->sin_family = AF_INET;
    637  1.1  itojun 		wport6 = socket(sin->sin_family, SOCK_STREAM, 0);
    638  1.1  itojun 		if (wport6 == -1) {
    639  1.1  itojun passivefail2:
    640  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    641  1.1  itojun 				"500 could not translate from EPSV\r\n");
    642  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    643  1.7  itojun 				n = 0;
    644  1.7  itojun 			if (n)
    645  1.7  itojun 				write(src, sbuf, n);
    646  1.1  itojun 			return n;
    647  1.1  itojun 		}
    648  1.1  itojun #ifdef IP_FAITH
    649  1.1  itojun 	    {
    650  1.1  itojun 		int on = 1;
    651  1.1  itojun 		error = setsockopt(wport6, IPPROTO_IP, IP_FAITH,
    652  1.1  itojun 			&on, sizeof(on));
    653  1.1  itojun 		if (error == -1)
    654  1.7  itojun 			exit_error("setsockopt(IP_FAITH): %s", strerror(errno));
    655  1.1  itojun 	    }
    656  1.1  itojun #endif
    657  1.1  itojun 		error = bind(wport6, (struct sockaddr *)sin, sin->sin_len);
    658  1.1  itojun 		if (error == -1) {
    659  1.1  itojun 			close(wport6);
    660  1.1  itojun 			wport6 = -1;
    661  1.1  itojun 			goto passivefail2;
    662  1.1  itojun 		}
    663  1.1  itojun 		error = listen(wport6, 1);
    664  1.1  itojun 		if (error == -1) {
    665  1.1  itojun 			close(wport6);
    666  1.1  itojun 			wport6 = -1;
    667  1.1  itojun 			goto passivefail2;
    668  1.1  itojun 		}
    669  1.1  itojun 
    670  1.1  itojun 		/* transmit PORT */
    671  1.1  itojun 		/*
    672  1.1  itojun 		 * addr from dst, port from wport6
    673  1.1  itojun 		 */
    674  1.1  itojun 		n = sizeof(data6);
    675  1.1  itojun 		error = getsockname(wport6, (struct sockaddr *)&data6, &n);
    676  1.1  itojun 		if (error == -1) {
    677  1.1  itojun 			close(wport6);
    678  1.1  itojun 			wport6 = -1;
    679  1.1  itojun 			goto passivefail2;
    680  1.1  itojun 		}
    681  1.1  itojun 		sin = (struct sockaddr_in *)&data6;
    682  1.1  itojun 		port = sin->sin_port;
    683  1.1  itojun 
    684  1.1  itojun 		n = sizeof(data6);
    685  1.1  itojun 		error = getsockname(dst, (struct sockaddr *)&data6, &n);
    686  1.1  itojun 		if (error == -1) {
    687  1.1  itojun 			close(wport6);
    688  1.1  itojun 			wport6 = -1;
    689  1.1  itojun 			goto passivefail2;
    690  1.1  itojun 		}
    691  1.1  itojun 		sin = (struct sockaddr_in *)&data6;
    692  1.1  itojun 		sin->sin_port = port;
    693  1.1  itojun 
    694  1.1  itojun 		{
    695  1.1  itojun 			char *a, *p;
    696  1.1  itojun 
    697  1.1  itojun 			a = (char *)&sin->sin_addr;
    698  1.1  itojun 			p = (char *)&sin->sin_port;
    699  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    700  1.1  itojun "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n",
    701  1.5  itojun 				UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
    702  1.1  itojun 				UC(p[0]), UC(p[1]));
    703  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    704  1.7  itojun 				n = 0;
    705  1.7  itojun 			if (n)
    706  1.7  itojun 				write(dst, sbuf, n);
    707  1.1  itojun 			passivemode = 1;
    708  1.1  itojun 			return n;
    709  1.1  itojun 		}
    710  1.1  itojun 	    }
    711  1.1  itojun #endif /* FAITH4 */
    712  1.1  itojun 	}
    713  1.1  itojun 
    714  1.1  itojun  bad:
    715  1.7  itojun 	exit_failure("%s", strerror(errno));
    716  1.1  itojun 	/*NOTREACHED*/
    717  1.1  itojun 	return 0;	/* to make gcc happy */
    718  1.1  itojun }
    719  1.1  itojun 
    720  1.1  itojun static int
    721  1.1  itojun ftp_copycommand(int src, int dst, enum state *state)
    722  1.1  itojun {
    723  1.1  itojun 	int error, atmark;
    724  1.1  itojun 	int n;
    725  1.1  itojun 	unsigned int af, hal, ho[16], pal, po[2];
    726  1.7  itojun 	char *a, *p, *q;
    727  1.1  itojun 	char cmd[5], *param;
    728  1.1  itojun 	struct sockaddr_in *sin;
    729  1.1  itojun 	struct sockaddr_in6 *sin6;
    730  1.1  itojun 	enum state nstate;
    731  1.1  itojun 	char ch;
    732  1.7  itojun 	int i;
    733  1.1  itojun 
    734  1.1  itojun 	/* OOB data handling */
    735  1.1  itojun 	error = ioctl(src, SIOCATMARK, &atmark);
    736  1.1  itojun 	if (error != -1 && atmark == 1) {
    737  1.1  itojun 		n = read(src, rbuf, 1);
    738  1.1  itojun 		if (n == -1)
    739  1.1  itojun 			goto bad;
    740  1.1  itojun 		send(dst, rbuf, n, MSG_OOB);
    741  1.1  itojun #if 0
    742  1.1  itojun 		n = read(src, rbuf, sizeof(rbuf));
    743  1.1  itojun 		if (n == -1)
    744  1.1  itojun 			goto bad;
    745  1.1  itojun 		write(dst, rbuf, n);
    746  1.1  itojun 		return n;
    747  1.1  itojun #endif
    748  1.1  itojun 	}
    749  1.1  itojun 
    750  1.1  itojun 	n = read(src, rbuf, sizeof(rbuf));
    751  1.1  itojun 	if (n <= 0)
    752  1.1  itojun 		return n;
    753  1.1  itojun 	rbuf[n] = '\0';
    754  1.1  itojun 
    755  1.1  itojun 	if (n < 4) {
    756  1.1  itojun 		write(dst, rbuf, n);
    757  1.1  itojun 		return n;
    758  1.1  itojun 	}
    759  1.1  itojun 
    760  1.1  itojun 	/*
    761  1.1  itojun 	 * parse argument
    762  1.1  itojun 	 */
    763  1.1  itojun 	p = rbuf;
    764  1.1  itojun 	q = cmd;
    765  1.1  itojun 	for (i = 0; i < 4; i++) {
    766  1.1  itojun 		if (!isalpha(*p)) {
    767  1.1  itojun 			/* invalid command */
    768  1.1  itojun 			write(dst, rbuf, n);
    769  1.1  itojun 			return n;
    770  1.1  itojun 		}
    771  1.1  itojun 		*q++ = islower(*p) ? toupper(*p) : *p;
    772  1.1  itojun 		p++;
    773  1.1  itojun 	}
    774  1.1  itojun 	if (!isspace(*p)) {
    775  1.1  itojun 		/* invalid command */
    776  1.1  itojun 		write(dst, rbuf, n);
    777  1.1  itojun 		return n;
    778  1.1  itojun 	}
    779  1.1  itojun 	*q = '\0';
    780  1.1  itojun 	param = p;
    781  1.1  itojun 	/* param points to first non-command token, if any */
    782  1.1  itojun 	while (*param && isspace(*param))
    783  1.1  itojun 		param++;
    784  1.1  itojun 	if (!*param)
    785  1.1  itojun 		param = NULL;
    786  1.1  itojun 
    787  1.1  itojun 	*state = NONE;
    788  1.1  itojun 
    789  1.1  itojun 	if (strcmp(cmd, "LPRT") == 0 && param) {
    790  1.1  itojun 		/*
    791  1.1  itojun 		 * LPRT -> PORT
    792  1.1  itojun 		 */
    793  1.1  itojun 		nstate = LPRT;
    794  1.1  itojun 
    795  1.1  itojun 		close(wport4);
    796  1.1  itojun 		close(wport6);
    797  1.1  itojun 		close(port4);
    798  1.1  itojun 		close(port6);
    799  1.1  itojun 		wport4 = wport6 = port4 = port6 = -1;
    800  1.1  itojun 
    801  1.1  itojun 		if (epsvall) {
    802  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
    803  1.1  itojun 				cmd);
    804  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    805  1.7  itojun 				n = 0;
    806  1.7  itojun 			if (n)
    807  1.7  itojun 				write(src, sbuf, n);
    808  1.1  itojun 			return n;
    809  1.1  itojun 		}
    810  1.1  itojun 
    811  1.1  itojun 		n = sscanf(param,
    812  1.1  itojun "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
    813  1.1  itojun 			      &af, &hal, &ho[0], &ho[1], &ho[2], &ho[3],
    814  1.1  itojun 			      &ho[4], &ho[5], &ho[6], &ho[7],
    815  1.1  itojun 			      &ho[8], &ho[9], &ho[10], &ho[11],
    816  1.1  itojun 			      &ho[12], &ho[13], &ho[14], &ho[15],
    817  1.1  itojun 			      &pal, &po[0], &po[1]);
    818  1.1  itojun 		if (n != 21 || af != 6 || hal != 16|| pal != 2) {
    819  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    820  1.1  itojun 				"501 illegal parameter to LPRT\r\n");
    821  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    822  1.7  itojun 				n = 0;
    823  1.7  itojun 			if (n)
    824  1.7  itojun 				write(src, sbuf, n);
    825  1.1  itojun 			return n;
    826  1.1  itojun 		}
    827  1.1  itojun 
    828  1.1  itojun 		/* keep LPRT parameter */
    829  1.1  itojun 		memset(&data6, 0, sizeof(data6));
    830  1.1  itojun 		sin6 = (struct sockaddr_in6 *)&data6;
    831  1.1  itojun 		sin6->sin6_len = sizeof(*sin6);
    832  1.1  itojun 		sin6->sin6_family = AF_INET6;
    833  1.1  itojun 		for (n = 0; n < 16; n++)
    834  1.2  itojun 			sin6->sin6_addr.s6_addr[n] = ho[n];
    835  1.1  itojun 		sin6->sin6_port = htons(((po[0] & 0xff) << 8) | (po[1] & 0xff));
    836  1.1  itojun 
    837  1.1  itojun sendport:
    838  1.1  itojun 		/* get ready for active data connection */
    839  1.1  itojun 		n = sizeof(data4);
    840  1.1  itojun 		error = getsockname(dst, (struct sockaddr *)&data4, &n);
    841  1.1  itojun 		if (error == -1) {
    842  1.1  itojun lprtfail:
    843  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    844  1.1  itojun 				"500 could not translate to PORT\r\n");
    845  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    846  1.7  itojun 				n = 0;
    847  1.7  itojun 			if (n)
    848  1.7  itojun 				write(src, sbuf, n);
    849  1.1  itojun 			return n;
    850  1.1  itojun 		}
    851  1.2  itojun 		if (((struct sockaddr *)&data4)->sa_family != AF_INET)
    852  1.1  itojun 			goto lprtfail;
    853  1.1  itojun 		sin = (struct sockaddr_in *)&data4;
    854  1.1  itojun 		sin->sin_port = 0;
    855  1.1  itojun 		wport4 = socket(sin->sin_family, SOCK_STREAM, 0);
    856  1.1  itojun 		if (wport4 == -1)
    857  1.1  itojun 			goto lprtfail;
    858  1.1  itojun 		error = bind(wport4, (struct sockaddr *)sin, sin->sin_len);
    859  1.1  itojun 		if (error == -1) {
    860  1.1  itojun 			close(wport4);
    861  1.1  itojun 			wport4 = -1;
    862  1.1  itojun 			goto lprtfail;
    863  1.1  itojun 		}
    864  1.1  itojun 		error = listen(wport4, 1);
    865  1.1  itojun 		if (error == -1) {
    866  1.1  itojun 			close(wport4);
    867  1.1  itojun 			wport4 = -1;
    868  1.1  itojun 			goto lprtfail;
    869  1.1  itojun 		}
    870  1.1  itojun 
    871  1.1  itojun 		/* transmit PORT */
    872  1.1  itojun 		n = sizeof(data4);
    873  1.1  itojun 		error = getsockname(wport4, (struct sockaddr *)&data4, &n);
    874  1.1  itojun 		if (error == -1) {
    875  1.1  itojun 			close(wport4);
    876  1.1  itojun 			wport4 = -1;
    877  1.1  itojun 			goto lprtfail;
    878  1.1  itojun 		}
    879  1.2  itojun 		if (((struct sockaddr *)&data4)->sa_family != AF_INET) {
    880  1.1  itojun 			close(wport4);
    881  1.1  itojun 			wport4 = -1;
    882  1.1  itojun 			goto lprtfail;
    883  1.1  itojun 		}
    884  1.1  itojun 		sin = (struct sockaddr_in *)&data4;
    885  1.1  itojun 		a = (char *)&sin->sin_addr;
    886  1.1  itojun 		p = (char *)&sin->sin_port;
    887  1.4  itojun 		n = snprintf(sbuf, sizeof(sbuf), "PORT %d,%d,%d,%d,%d,%d\r\n",
    888  1.1  itojun 				  UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
    889  1.1  itojun 				  UC(p[0]), UC(p[1]));
    890  1.7  itojun 		if (n < 0 || n >= sizeof(sbuf))
    891  1.7  itojun 			n = 0;
    892  1.7  itojun 		if (n)
    893  1.7  itojun 			write(dst, sbuf, n);
    894  1.1  itojun 		*state = nstate;
    895  1.1  itojun 		passivemode = 0;
    896  1.1  itojun 		return n;
    897  1.1  itojun 	} else if (strcmp(cmd, "EPRT") == 0 && param) {
    898  1.1  itojun 		/*
    899  1.1  itojun 		 * EPRT -> PORT
    900  1.1  itojun 		 */
    901  1.1  itojun 		char *afp, *hostp, *portp;
    902  1.1  itojun 		struct addrinfo hints, *res;
    903  1.1  itojun 
    904  1.1  itojun 		nstate = EPRT;
    905  1.1  itojun 
    906  1.1  itojun 		close(wport4);
    907  1.1  itojun 		close(wport6);
    908  1.1  itojun 		close(port4);
    909  1.1  itojun 		close(port6);
    910  1.1  itojun 		wport4 = wport6 = port4 = port6 = -1;
    911  1.1  itojun 
    912  1.1  itojun 		if (epsvall) {
    913  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
    914  1.1  itojun 				cmd);
    915  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    916  1.7  itojun 				n = 0;
    917  1.7  itojun 			if (n)
    918  1.7  itojun 				write(src, sbuf, n);
    919  1.1  itojun 			return n;
    920  1.1  itojun 		}
    921  1.1  itojun 
    922  1.1  itojun 		p = param;
    923  1.1  itojun 		ch = *p++;	/* boundary character */
    924  1.1  itojun 		afp = p;
    925  1.1  itojun 		while (*p && *p != ch)
    926  1.1  itojun 			p++;
    927  1.1  itojun 		if (!*p) {
    928  1.1  itojun eprtparamfail:
    929  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    930  1.1  itojun 				"501 illegal parameter to EPRT\r\n");
    931  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    932  1.7  itojun 				n = 0;
    933  1.7  itojun 			if (n)
    934  1.7  itojun 				write(src, sbuf, n);
    935  1.1  itojun 			return n;
    936  1.1  itojun 		}
    937  1.1  itojun 		*p++ = '\0';
    938  1.1  itojun 		hostp = p;
    939  1.1  itojun 		while (*p && *p != ch)
    940  1.1  itojun 			p++;
    941  1.1  itojun 		if (!*p)
    942  1.1  itojun 			goto eprtparamfail;
    943  1.1  itojun 		*p++ = '\0';
    944  1.1  itojun 		portp = p;
    945  1.1  itojun 		while (*p && *p != ch)
    946  1.1  itojun 			p++;
    947  1.1  itojun 		if (!*p)
    948  1.1  itojun 			goto eprtparamfail;
    949  1.1  itojun 		*p++ = '\0';
    950  1.1  itojun 
    951  1.1  itojun 		n = sscanf(afp, "%d", &af);
    952  1.1  itojun 		if (n != 1 || af != 2) {
    953  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    954  1.1  itojun 				"501 unsupported address family to EPRT\r\n");
    955  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    956  1.7  itojun 				n = 0;
    957  1.7  itojun 			if (n)
    958  1.7  itojun 				write(src, sbuf, n);
    959  1.1  itojun 			return n;
    960  1.1  itojun 		}
    961  1.1  itojun 		memset(&hints, 0, sizeof(hints));
    962  1.1  itojun 		hints.ai_family = AF_UNSPEC;
    963  1.6  itojun 		hints.ai_socktype = SOCK_STREAM;
    964  1.1  itojun 		error = getaddrinfo(hostp, portp, &hints, &res);
    965  1.1  itojun 		if (error) {
    966  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    967  1.1  itojun 				"501 EPRT: %s\r\n", gai_strerror(error));
    968  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    969  1.7  itojun 				n = 0;
    970  1.7  itojun 			if (n)
    971  1.7  itojun 				write(src, sbuf, n);
    972  1.1  itojun 			return n;
    973  1.1  itojun 		}
    974  1.1  itojun 		if (res->ai_next) {
    975  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    976  1.1  itojun 				"501 EPRT: %s resolved to multiple addresses\r\n", hostp);
    977  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    978  1.7  itojun 				n = 0;
    979  1.7  itojun 			if (n)
    980  1.7  itojun 				write(src, sbuf, n);
    981  1.1  itojun 			return n;
    982  1.1  itojun 		}
    983  1.1  itojun 
    984  1.1  itojun 		memcpy(&data6, res->ai_addr, res->ai_addrlen);
    985  1.1  itojun 
    986  1.1  itojun 		goto sendport;
    987  1.1  itojun 	} else if (strcmp(cmd, "LPSV") == 0 && !param) {
    988  1.1  itojun 		/*
    989  1.1  itojun 		 * LPSV -> PASV
    990  1.1  itojun 		 */
    991  1.1  itojun 		nstate = LPSV;
    992  1.1  itojun 
    993  1.1  itojun 		close(wport4);
    994  1.1  itojun 		close(wport6);
    995  1.1  itojun 		close(port4);
    996  1.1  itojun 		close(port6);
    997  1.1  itojun 		wport4 = wport6 = port4 = port6 = -1;
    998  1.1  itojun 
    999  1.1  itojun 		if (epsvall) {
   1000  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
   1001  1.1  itojun 				cmd);
   1002  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
   1003  1.7  itojun 				n = 0;
   1004  1.7  itojun 			if (n)
   1005  1.7  itojun 				write(src, sbuf, n);
   1006  1.1  itojun 			return n;
   1007  1.1  itojun 		}
   1008  1.1  itojun 
   1009  1.1  itojun 		/* transmit PASV */
   1010  1.4  itojun 		n = snprintf(sbuf, sizeof(sbuf), "PASV\r\n");
   1011  1.7  itojun 		if (n < 0 || n >= sizeof(sbuf))
   1012  1.7  itojun 			n = 0;
   1013  1.7  itojun 		if (n)
   1014  1.7  itojun 			write(dst, sbuf, n);
   1015  1.1  itojun 		*state = LPSV;
   1016  1.1  itojun 		passivemode = 0;	/* to be set to 1 later */
   1017  1.1  itojun 		return n;
   1018  1.1  itojun 	} else if (strcmp(cmd, "EPSV") == 0 && !param) {
   1019  1.1  itojun 		/*
   1020  1.1  itojun 		 * EPSV -> PASV
   1021  1.1  itojun 		 */
   1022  1.1  itojun 		close(wport4);
   1023  1.1  itojun 		close(wport6);
   1024  1.1  itojun 		close(port4);
   1025  1.1  itojun 		close(port6);
   1026  1.1  itojun 		wport4 = wport6 = port4 = port6 = -1;
   1027  1.1  itojun 
   1028  1.4  itojun 		n = snprintf(sbuf, sizeof(sbuf), "PASV\r\n");
   1029  1.7  itojun 		if (n < 0 || n >= sizeof(sbuf))
   1030  1.7  itojun 			n = 0;
   1031  1.7  itojun 		if (n)
   1032  1.7  itojun 			write(dst, sbuf, n);
   1033  1.1  itojun 		*state = EPSV;
   1034  1.1  itojun 		passivemode = 0;	/* to be set to 1 later */
   1035  1.1  itojun 		return n;
   1036  1.1  itojun 	} else if (strcmp(cmd, "EPSV") == 0 && param
   1037  1.1  itojun 	 && strncasecmp(param, "ALL", 3) == 0 && isspace(param[3])) {
   1038  1.1  itojun 		/*
   1039  1.1  itojun 		 * EPSV ALL
   1040  1.1  itojun 		 */
   1041  1.1  itojun 		epsvall = 1;
   1042  1.4  itojun 		n = snprintf(sbuf, sizeof(sbuf), "200 EPSV ALL command successful.\r\n");
   1043  1.7  itojun 		if (n < 0 || n >= sizeof(sbuf))
   1044  1.7  itojun 			n = 0;
   1045  1.7  itojun 		if (n)
   1046  1.7  itojun 			write(src, sbuf, n);
   1047  1.1  itojun 		return n;
   1048  1.1  itojun #ifdef FAITH4
   1049  1.1  itojun 	} else if (strcmp(cmd, "PORT") == 0 && param) {
   1050  1.1  itojun 		/*
   1051  1.1  itojun 		 * PORT -> EPRT
   1052  1.1  itojun 		 */
   1053  1.1  itojun 		char host[NI_MAXHOST], serv[NI_MAXSERV];
   1054  1.1  itojun 
   1055  1.1  itojun 		nstate = PORT;
   1056  1.1  itojun 
   1057  1.1  itojun 		close(wport4);
   1058  1.1  itojun 		close(wport6);
   1059  1.1  itojun 		close(port4);
   1060  1.1  itojun 		close(port6);
   1061  1.1  itojun 		wport4 = wport6 = port4 = port6 = -1;
   1062  1.1  itojun 
   1063  1.1  itojun 		p = param;
   1064  1.1  itojun 		n = sscanf(p, "%u,%u,%u,%u,%u,%u",
   1065  1.1  itojun 			&ho[0], &ho[1], &ho[2], &ho[3], &po[0], &po[1]);
   1066  1.1  itojun 		if (n != 6) {
   1067  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
   1068  1.1  itojun 				"501 illegal parameter to PORT\r\n");
   1069  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
   1070  1.7  itojun 				n = 0;
   1071  1.7  itojun 			if (n)
   1072  1.7  itojun 				write(src, sbuf, n);
   1073  1.1  itojun 			return n;
   1074  1.1  itojun 		}
   1075  1.1  itojun 
   1076  1.1  itojun 		memset(&data6, 0, sizeof(data6));
   1077  1.1  itojun 		sin = (struct sockaddr_in *)&data6;
   1078  1.1  itojun 		sin->sin_len = sizeof(*sin);
   1079  1.1  itojun 		sin->sin_family = AF_INET;
   1080  1.1  itojun 		sin->sin_addr.s_addr = htonl(
   1081  1.1  itojun 			((ho[0] & 0xff) << 24) | ((ho[1] & 0xff) << 16) |
   1082  1.1  itojun 			((ho[2] & 0xff) << 8) | (ho[3] & 0xff));
   1083  1.1  itojun 		sin->sin_port = htons(((po[0] & 0xff) << 8) | (po[1] & 0xff));
   1084  1.1  itojun 
   1085  1.1  itojun 		/* get ready for active data connection */
   1086  1.1  itojun 		n = sizeof(data4);
   1087  1.1  itojun 		error = getsockname(dst, (struct sockaddr *)&data4, &n);
   1088  1.1  itojun 		if (error == -1) {
   1089  1.1  itojun portfail:
   1090  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
   1091  1.1  itojun 				"500 could not translate to EPRT\r\n");
   1092  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
   1093  1.7  itojun 				n = 0;
   1094  1.7  itojun 			if (n)
   1095  1.7  itojun 				write(src, sbuf, n);
   1096  1.1  itojun 			return n;
   1097  1.1  itojun 		}
   1098  1.2  itojun 		if (((struct sockaddr *)&data4)->sa_family != AF_INET6)
   1099  1.1  itojun 			goto portfail;
   1100  1.1  itojun 
   1101  1.1  itojun 		((struct sockaddr_in6 *)&data4)->sin6_port = 0;
   1102  1.2  itojun 		sa = (struct sockaddr *)&data4;
   1103  1.2  itojun 		wport4 = socket(sa->sa_family, SOCK_STREAM, 0);
   1104  1.1  itojun 		if (wport4 == -1)
   1105  1.1  itojun 			goto portfail;
   1106  1.2  itojun 		error = bind(wport4, sa, sa->sa_len);
   1107  1.1  itojun 		if (error == -1) {
   1108  1.1  itojun 			close(wport4);
   1109  1.1  itojun 			wport4 = -1;
   1110  1.1  itojun 			goto portfail;
   1111  1.1  itojun 		}
   1112  1.1  itojun 		error = listen(wport4, 1);
   1113  1.1  itojun 		if (error == -1) {
   1114  1.1  itojun 			close(wport4);
   1115  1.1  itojun 			wport4 = -1;
   1116  1.1  itojun 			goto portfail;
   1117  1.1  itojun 		}
   1118  1.1  itojun 
   1119  1.1  itojun 		/* transmit EPRT */
   1120  1.1  itojun 		n = sizeof(data4);
   1121  1.1  itojun 		error = getsockname(wport4, (struct sockaddr *)&data4, &n);
   1122  1.1  itojun 		if (error == -1) {
   1123  1.1  itojun 			close(wport4);
   1124  1.1  itojun 			wport4 = -1;
   1125  1.1  itojun 			goto portfail;
   1126  1.1  itojun 		}
   1127  1.1  itojun 		af = 2;
   1128  1.2  itojun 		sa = (struct sockaddr *)&data4;
   1129  1.2  itojun 		if (getnameinfo(sa, sa->sa_len, host, sizeof(host),
   1130  1.2  itojun 			serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV)) {
   1131  1.1  itojun 			close(wport4);
   1132  1.1  itojun 			wport4 = -1;
   1133  1.1  itojun 			goto portfail;
   1134  1.1  itojun 		}
   1135  1.4  itojun 		n = snprintf(sbuf, sizeof(sbuf), "EPRT |%d|%s|%s|\r\n", af, host, serv);
   1136  1.7  itojun 		if (n < 0 || n >= sizeof(sbuf))
   1137  1.7  itojun 			n = 0;
   1138  1.7  itojun 		if (n)
   1139  1.7  itojun 			write(dst, sbuf, n);
   1140  1.1  itojun 		*state = nstate;
   1141  1.1  itojun 		passivemode = 0;
   1142  1.1  itojun 		return n;
   1143  1.1  itojun 	} else if (strcmp(cmd, "PASV") == 0 && !param) {
   1144  1.1  itojun 		/*
   1145  1.1  itojun 		 * PASV -> EPSV
   1146  1.1  itojun 		 */
   1147  1.1  itojun 
   1148  1.1  itojun 		nstate = PASV;
   1149  1.1  itojun 
   1150  1.1  itojun 		close(wport4);
   1151  1.1  itojun 		close(wport6);
   1152  1.1  itojun 		close(port4);
   1153  1.1  itojun 		close(port6);
   1154  1.1  itojun 		wport4 = wport6 = port4 = port6 = -1;
   1155  1.1  itojun 
   1156  1.1  itojun 		/* transmit EPSV */
   1157  1.4  itojun 		n = snprintf(sbuf, sizeof(sbuf), "EPSV\r\n");
   1158  1.7  itojun 		if (n < 0 || n >= sizeof(sbuf))
   1159  1.7  itojun 			n = 0;
   1160  1.7  itojun 		if (n)
   1161  1.7  itojun 			write(dst, sbuf, n);
   1162  1.1  itojun 		*state = PASV;
   1163  1.1  itojun 		passivemode = 0;	/* to be set to 1 later */
   1164  1.1  itojun 		return n;
   1165  1.1  itojun #else /* FAITH4 */
   1166  1.1  itojun 	} else if (strcmp(cmd, "PORT") == 0 || strcmp(cmd, "PASV") == 0) {
   1167  1.1  itojun 		/*
   1168  1.1  itojun 		 * reject PORT/PASV
   1169  1.1  itojun 		 */
   1170  1.4  itojun 		n = snprintf(sbuf, sizeof(sbuf), "502 %s not implemented.\r\n", cmd);
   1171  1.7  itojun 		if (n < 0 || n >= sizeof(sbuf))
   1172  1.7  itojun 			n = 0;
   1173  1.7  itojun 		if (n)
   1174  1.7  itojun 			write(src, sbuf, n);
   1175  1.1  itojun 		return n;
   1176  1.1  itojun #endif /* FAITH4 */
   1177  1.1  itojun 	} else if (passivemode
   1178  1.1  itojun 		&& (strcmp(cmd, "STOR") == 0
   1179  1.1  itojun 		 || strcmp(cmd, "STOU") == 0
   1180  1.1  itojun 		 || strcmp(cmd, "RETR") == 0
   1181  1.1  itojun 		 || strcmp(cmd, "LIST") == 0
   1182  1.1  itojun 		 || strcmp(cmd, "NLST") == 0
   1183  1.1  itojun 		 || strcmp(cmd, "APPE") == 0)) {
   1184  1.1  itojun 		/*
   1185  1.1  itojun 		 * commands with data transfer.  need to care about passive
   1186  1.1  itojun 		 * mode data connection.
   1187  1.1  itojun 		 */
   1188  1.1  itojun 
   1189  1.1  itojun 		if (ftp_passiveconn() < 0) {
   1190  1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf), "425 Cannot open data connetion\r\n");
   1191  1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
   1192  1.7  itojun 				n = 0;
   1193  1.7  itojun 			if (n)
   1194  1.7  itojun 				write(src, sbuf, n);
   1195  1.1  itojun 		} else {
   1196  1.1  itojun 			/* simply relay the command */
   1197  1.1  itojun 			write(dst, rbuf, n);
   1198  1.1  itojun 		}
   1199  1.1  itojun 
   1200  1.1  itojun 		*state = NONE;
   1201  1.1  itojun 		return n;
   1202  1.1  itojun 	} else {
   1203  1.1  itojun 		/* simply relay it */
   1204  1.1  itojun 		*state = NONE;
   1205  1.1  itojun 		write(dst, rbuf, n);
   1206  1.1  itojun 		return n;
   1207  1.1  itojun 	}
   1208  1.1  itojun 
   1209  1.1  itojun  bad:
   1210  1.7  itojun 	exit_failure("%s", strerror(errno));
   1211  1.1  itojun 	/*NOTREACHED*/
   1212  1.1  itojun 	return 0;	/* to make gcc happy */
   1213  1.1  itojun }
   1214