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