Home | History | Annotate | Line # | Download | only in faithd
ftp.c revision 1.10
      1  1.10  itojun /*	$NetBSD: ftp.c,v 1.10 2002/06/24 06:03:13 itojun Exp $	*/
      2  1.10  itojun /*	$KAME: ftp.c,v 1.18 2002/06/23 14:41:47 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 enum state { NONE, LPRT, EPRT, LPSV, EPSV };
     65   1.1  itojun 
     66   1.1  itojun static int ftp_activeconn __P((void));
     67   1.1  itojun static int ftp_passiveconn __P((void));
     68   1.1  itojun static int ftp_copy __P((int, int));
     69   1.1  itojun static int ftp_copyresult __P((int, int, enum state));
     70   1.1  itojun static int ftp_copycommand __P((int, int, enum state *));
     71   1.1  itojun 
     72   1.1  itojun void
     73   1.1  itojun ftp_relay(int ctl6, int ctl4)
     74   1.1  itojun {
     75   1.1  itojun 	fd_set readfds;
     76   1.1  itojun 	int error;
     77   1.1  itojun 	enum state state = NONE;
     78   1.1  itojun 	struct timeval tv;
     79   1.1  itojun 
     80   1.1  itojun 	syslog(LOG_INFO, "starting ftp control connection");
     81   1.1  itojun 
     82   1.1  itojun 	for (;;) {
     83   1.7  itojun 		int maxfd = 0;
     84   1.7  itojun 
     85   1.1  itojun 		FD_ZERO(&readfds);
     86   1.1  itojun 		FD_SET(ctl4, &readfds);
     87  1.10  itojun 		maxfd = ctl4;
     88   1.1  itojun 		FD_SET(ctl6, &readfds);
     89  1.10  itojun 		maxfd = (ctl6 > maxfd) ? ctl6 : maxfd;
     90   1.7  itojun 		if (0 <= port4) {
     91   1.1  itojun 			FD_SET(port4, &readfds);
     92  1.10  itojun 			maxfd = (port4 > maxfd) ? port4 : maxfd;
     93   1.7  itojun 		}
     94   1.7  itojun 		if (0 <= port6) {
     95   1.1  itojun 			FD_SET(port6, &readfds);
     96  1.10  itojun 			maxfd = (port6 > maxfd) ? port6 : maxfd;
     97   1.7  itojun 		}
     98   1.1  itojun #if 0
     99   1.7  itojun 		if (0 <= wport4) {
    100   1.1  itojun 			FD_SET(wport4, &readfds);
    101  1.10  itojun 			maxfd = (wport4 > maxfd) ? wport4 : maxfd;
    102   1.7  itojun 		}
    103   1.7  itojun 		if (0 <= wport6) {
    104   1.1  itojun 			FD_SET(wport6, &readfds);
    105  1.10  itojun 			maxfd = (wport6 > maxfd) ? wport6 : maxfd;
    106   1.7  itojun 		}
    107   1.1  itojun #endif
    108   1.1  itojun 		tv.tv_sec = FAITH_TIMEOUT;
    109   1.1  itojun 		tv.tv_usec = 0;
    110   1.1  itojun 
    111   1.7  itojun 		error = select(maxfd + 1, &readfds, NULL, NULL, &tv);
    112   1.1  itojun 		if (error == -1)
    113   1.7  itojun 			exit_failure("select: %s", strerror(errno));
    114   1.1  itojun 		else if (error == 0)
    115   1.1  itojun 			exit_failure("connection timeout");
    116   1.1  itojun 
    117   1.1  itojun 		/*
    118   1.1  itojun 		 * The order of the following checks does (slightly) matter.
    119   1.1  itojun 		 * It is important to visit all checks (do not use "continue"),
    120   1.1  itojun 		 * otherwise some of the pipe may become full and we cannot
    121   1.1  itojun 		 * relay correctly.
    122   1.1  itojun 		 */
    123   1.1  itojun 		if (FD_ISSET(ctl6, &readfds)) {
    124   1.1  itojun 			/*
    125   1.1  itojun 			 * copy control connection from the client.
    126   1.1  itojun 			 * command translation is necessary.
    127   1.1  itojun 			 */
    128   1.1  itojun 			error = ftp_copycommand(ctl6, ctl4, &state);
    129   1.1  itojun 
    130  1.10  itojun 			if (error < 0)
    131   1.1  itojun 				goto bad;
    132  1.10  itojun 			else if (error == 0) {
    133   1.1  itojun 				close(ctl4);
    134   1.1  itojun 				close(ctl6);
    135   1.1  itojun 				exit_success("terminating ftp control connection");
    136   1.1  itojun 				/*NOTREACHED*/
    137   1.1  itojun 			}
    138   1.1  itojun 		}
    139   1.1  itojun 		if (FD_ISSET(ctl4, &readfds)) {
    140   1.1  itojun 			/*
    141   1.1  itojun 			 * copy control connection from the server
    142   1.1  itojun 			 * translation of result code is necessary.
    143   1.1  itojun 			 */
    144   1.1  itojun 			error = ftp_copyresult(ctl4, ctl6, state);
    145   1.1  itojun 
    146  1.10  itojun 			if (error < 0)
    147   1.1  itojun 				goto bad;
    148  1.10  itojun 			else if (error == 0) {
    149   1.1  itojun 				close(ctl4);
    150   1.1  itojun 				close(ctl6);
    151   1.1  itojun 				exit_success("terminating ftp control connection");
    152   1.1  itojun 				/*NOTREACHED*/
    153   1.1  itojun 			}
    154   1.1  itojun 		}
    155   1.1  itojun 		if (0 <= port4 && 0 <= port6 && FD_ISSET(port4, &readfds)) {
    156   1.1  itojun 			/*
    157   1.1  itojun 			 * copy data connection.
    158   1.1  itojun 			 * no special treatment necessary.
    159   1.1  itojun 			 */
    160   1.1  itojun 			if (FD_ISSET(port4, &readfds))
    161   1.1  itojun 				error = ftp_copy(port4, port6);
    162   1.1  itojun 			switch (error) {
    163   1.1  itojun 			case -1:
    164   1.1  itojun 				goto bad;
    165   1.1  itojun 			case 0:
    166   1.1  itojun 				close(port4);
    167   1.1  itojun 				close(port6);
    168   1.1  itojun 				port4 = port6 = -1;
    169   1.1  itojun 				syslog(LOG_INFO, "terminating data connection");
    170   1.1  itojun 				break;
    171   1.1  itojun 			default:
    172   1.1  itojun 				break;
    173   1.1  itojun 			}
    174   1.1  itojun 		}
    175   1.1  itojun 		if (0 <= port4 && 0 <= port6 && FD_ISSET(port6, &readfds)) {
    176   1.1  itojun 			/*
    177   1.1  itojun 			 * copy data connection.
    178   1.1  itojun 			 * no special treatment necessary.
    179   1.1  itojun 			 */
    180   1.1  itojun 			if (FD_ISSET(port6, &readfds))
    181   1.1  itojun 				error = ftp_copy(port6, port4);
    182   1.1  itojun 			switch (error) {
    183   1.1  itojun 			case -1:
    184   1.1  itojun 				goto bad;
    185   1.1  itojun 			case 0:
    186   1.1  itojun 				close(port4);
    187   1.1  itojun 				close(port6);
    188   1.1  itojun 				port4 = port6 = -1;
    189   1.1  itojun 				syslog(LOG_INFO, "terminating data connection");
    190   1.1  itojun 				break;
    191   1.1  itojun 			default:
    192   1.1  itojun 				break;
    193   1.1  itojun 			}
    194   1.1  itojun 		}
    195   1.1  itojun #if 0
    196   1.1  itojun 		if (wport4 && FD_ISSET(wport4, &readfds)) {
    197   1.1  itojun 			/*
    198   1.1  itojun 			 * establish active data connection from the server.
    199   1.1  itojun 			 */
    200   1.1  itojun 			ftp_activeconn();
    201   1.1  itojun 		}
    202   1.1  itojun 		if (wport6 && FD_ISSET(wport6, &readfds)) {
    203   1.1  itojun 			/*
    204   1.1  itojun 			 * establish passive data connection from the client.
    205   1.1  itojun 			 */
    206   1.1  itojun 			ftp_passiveconn();
    207   1.1  itojun 		}
    208   1.1  itojun #endif
    209   1.1  itojun 	}
    210   1.1  itojun 
    211   1.1  itojun  bad:
    212   1.7  itojun 	exit_failure("%s", strerror(errno));
    213   1.1  itojun }
    214   1.1  itojun 
    215   1.1  itojun static int
    216   1.1  itojun ftp_activeconn()
    217   1.1  itojun {
    218   1.1  itojun 	int n;
    219   1.1  itojun 	int error;
    220   1.1  itojun 	fd_set set;
    221   1.1  itojun 	struct timeval timeout;
    222   1.2  itojun 	struct sockaddr *sa;
    223   1.1  itojun 
    224   1.1  itojun 	/* get active connection from server */
    225   1.1  itojun 	FD_ZERO(&set);
    226   1.1  itojun 	FD_SET(wport4, &set);
    227   1.1  itojun 	timeout.tv_sec = 120;
    228   1.1  itojun 	timeout.tv_usec = -1;
    229   1.1  itojun 	n = sizeof(data4);
    230   1.1  itojun 	if (select(wport4 + 1, &set, NULL, NULL, &timeout) == 0
    231   1.1  itojun 	 || (port4 = accept(wport4, (struct sockaddr *)&data4, &n)) < 0) {
    232   1.1  itojun 		close(wport4);
    233   1.1  itojun 		wport4 = -1;
    234   1.1  itojun 		syslog(LOG_INFO, "active mode data connection failed");
    235   1.1  itojun 		return -1;
    236   1.1  itojun 	}
    237   1.1  itojun 
    238   1.1  itojun 	/* ask active connection to client */
    239   1.2  itojun 	sa = (struct sockaddr *)&data6;
    240   1.2  itojun 	port6 = socket(sa->sa_family, SOCK_STREAM, 0);
    241   1.1  itojun 	if (port6 == -1) {
    242   1.1  itojun 		close(port4);
    243   1.1  itojun 		close(wport4);
    244   1.1  itojun 		port4 = wport4 = -1;
    245   1.1  itojun 		syslog(LOG_INFO, "active mode data connection failed");
    246   1.1  itojun 		return -1;
    247   1.1  itojun 	}
    248   1.2  itojun 	error = connect(port6, sa, sa->sa_len);
    249   1.8  itojun 	if (error < 0) {
    250   1.1  itojun 		close(port6);
    251   1.1  itojun 		close(port4);
    252   1.1  itojun 		close(wport4);
    253   1.1  itojun 		port6 = port4 = wport4 = -1;
    254   1.1  itojun 		syslog(LOG_INFO, "active mode data connection failed");
    255   1.1  itojun 		return -1;
    256   1.1  itojun 	}
    257   1.1  itojun 
    258   1.1  itojun 	syslog(LOG_INFO, "active mode data connection established");
    259   1.1  itojun 	return 0;
    260   1.1  itojun }
    261   1.1  itojun 
    262   1.1  itojun static int
    263   1.1  itojun ftp_passiveconn()
    264   1.1  itojun {
    265   1.1  itojun 	int n;
    266   1.1  itojun 	int error;
    267   1.1  itojun 	fd_set set;
    268   1.1  itojun 	struct timeval timeout;
    269   1.2  itojun 	struct sockaddr *sa;
    270   1.1  itojun 
    271   1.1  itojun 	/* get passive connection from client */
    272   1.1  itojun 	FD_ZERO(&set);
    273   1.1  itojun 	FD_SET(wport6, &set);
    274   1.1  itojun 	timeout.tv_sec = 120;
    275   1.1  itojun 	timeout.tv_usec = 0;
    276   1.1  itojun 	n = sizeof(data6);
    277   1.1  itojun 	if (select(wport6 + 1, &set, NULL, NULL, &timeout) == 0
    278   1.1  itojun 	 || (port6 = accept(wport6, (struct sockaddr *)&data6, &n)) < 0) {
    279   1.1  itojun 		close(wport6);
    280   1.1  itojun 		wport6 = -1;
    281   1.1  itojun 		syslog(LOG_INFO, "passive mode data connection failed");
    282   1.1  itojun 		return -1;
    283   1.1  itojun 	}
    284   1.1  itojun 
    285   1.1  itojun 	/* ask passive connection to server */
    286   1.2  itojun 	sa = (struct sockaddr *)&data4;
    287   1.2  itojun 	port4 = socket(sa->sa_family, SOCK_STREAM, 0);
    288   1.1  itojun 	if (port4 == -1) {
    289   1.1  itojun 		close(wport6);
    290   1.1  itojun 		close(port6);
    291   1.1  itojun 		wport6 = port6 = -1;
    292   1.1  itojun 		syslog(LOG_INFO, "passive mode data connection failed");
    293   1.1  itojun 		return -1;
    294   1.1  itojun 	}
    295   1.2  itojun 	error = connect(port4, sa, sa->sa_len);
    296   1.8  itojun 	if (error < 0) {
    297   1.1  itojun 		close(wport6);
    298   1.1  itojun 		close(port4);
    299   1.1  itojun 		close(port6);
    300   1.1  itojun 		wport6 = port4 = port6 = -1;
    301   1.1  itojun 		syslog(LOG_INFO, "passive mode data connection failed");
    302   1.1  itojun 		return -1;
    303   1.1  itojun 	}
    304   1.1  itojun 
    305   1.1  itojun 	syslog(LOG_INFO, "passive mode data connection established");
    306   1.1  itojun 	return 0;
    307   1.1  itojun }
    308   1.1  itojun 
    309   1.1  itojun static int
    310   1.1  itojun ftp_copy(int src, int dst)
    311   1.1  itojun {
    312   1.1  itojun 	int error, atmark;
    313   1.1  itojun 	int n;
    314   1.1  itojun 
    315   1.1  itojun 	/* OOB data handling */
    316   1.1  itojun 	error = ioctl(src, SIOCATMARK, &atmark);
    317   1.1  itojun 	if (error != -1 && atmark == 1) {
    318   1.1  itojun 		n = read(src, rbuf, 1);
    319   1.1  itojun 		if (n == -1)
    320   1.1  itojun 			goto bad;
    321   1.1  itojun 		send(dst, rbuf, n, MSG_OOB);
    322   1.1  itojun #if 0
    323   1.1  itojun 		n = read(src, rbuf, sizeof(rbuf));
    324   1.1  itojun 		if (n == -1)
    325   1.1  itojun 			goto bad;
    326   1.1  itojun 		write(dst, rbuf, n);
    327   1.1  itojun 		return n;
    328   1.1  itojun #endif
    329   1.1  itojun 	}
    330   1.1  itojun 
    331   1.1  itojun 	n = read(src, rbuf, sizeof(rbuf));
    332   1.1  itojun 	switch (n) {
    333   1.1  itojun 	case -1:
    334   1.1  itojun 	case 0:
    335   1.1  itojun 		return n;
    336   1.1  itojun 	default:
    337   1.1  itojun 		write(dst, rbuf, n);
    338   1.1  itojun 		return n;
    339   1.1  itojun 	}
    340   1.1  itojun 
    341   1.1  itojun  bad:
    342   1.7  itojun 	exit_failure("%s", strerror(errno));
    343   1.1  itojun 	/*NOTREACHED*/
    344   1.1  itojun 	return 0;	/* to make gcc happy */
    345   1.1  itojun }
    346   1.1  itojun 
    347   1.1  itojun static int
    348   1.1  itojun ftp_copyresult(int src, int dst, enum state state)
    349   1.1  itojun {
    350   1.1  itojun 	int error, atmark;
    351   1.1  itojun 	int n;
    352   1.1  itojun 	char *param;
    353   1.1  itojun 	int code;
    354   1.7  itojun 	char *a, *p;
    355   1.7  itojun 	int i;
    356   1.1  itojun 
    357   1.1  itojun 	/* OOB data handling */
    358   1.1  itojun 	error = ioctl(src, SIOCATMARK, &atmark);
    359   1.1  itojun 	if (error != -1 && atmark == 1) {
    360   1.1  itojun 		n = read(src, rbuf, 1);
    361   1.1  itojun 		if (n == -1)
    362   1.1  itojun 			goto bad;
    363   1.1  itojun 		send(dst, rbuf, n, MSG_OOB);
    364   1.1  itojun #if 0
    365   1.1  itojun 		n = read(src, rbuf, sizeof(rbuf));
    366   1.1  itojun 		if (n == -1)
    367   1.1  itojun 			goto bad;
    368   1.1  itojun 		write(dst, rbuf, n);
    369   1.1  itojun 		return n;
    370   1.1  itojun #endif
    371   1.1  itojun 	}
    372   1.1  itojun 
    373   1.1  itojun 	n = read(src, rbuf, sizeof(rbuf));
    374   1.1  itojun 	if (n <= 0)
    375   1.1  itojun 		return n;
    376   1.1  itojun 	rbuf[n] = '\0';
    377   1.1  itojun 
    378   1.1  itojun 	/*
    379   1.1  itojun 	 * parse argument
    380   1.1  itojun 	 */
    381   1.1  itojun 	p = rbuf;
    382   1.1  itojun 	for (i = 0; i < 3; i++) {
    383   1.1  itojun 		if (!isdigit(*p)) {
    384   1.1  itojun 			/* invalid reply */
    385   1.1  itojun 			write(dst, rbuf, n);
    386   1.1  itojun 			return n;
    387   1.1  itojun 		}
    388   1.1  itojun 		p++;
    389   1.1  itojun 	}
    390   1.1  itojun 	if (!isspace(*p)) {
    391   1.1  itojun 		/* invalid reply */
    392   1.1  itojun 		write(dst, rbuf, n);
    393   1.1  itojun 		return n;
    394   1.1  itojun 	}
    395   1.1  itojun 	code = atoi(rbuf);
    396   1.1  itojun 	param = p;
    397   1.1  itojun 	/* param points to first non-command token, if any */
    398   1.1  itojun 	while (*param && isspace(*param))
    399   1.1  itojun 		param++;
    400   1.1  itojun 	if (!*param)
    401   1.1  itojun 		param = NULL;
    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.4  itojun 				n = snprintf(rbuf, sizeof(rbuf),
    408   1.1  itojun 					"425 Cannot open data connetion\r\n");
    409   1.7  itojun 				if (n < 0 || n >= sizeof(rbuf))
    410   1.7  itojun 					n = 0;
    411   1.1  itojun 			}
    412   1.1  itojun 		}
    413   1.7  itojun 		if (n)
    414   1.7  itojun 			write(dst, rbuf, n);
    415   1.1  itojun 		return n;
    416   1.1  itojun 	case LPRT:
    417   1.1  itojun 	case EPRT:
    418   1.1  itojun 		/* expecting "200 PORT command successful." */
    419   1.1  itojun 		if (code == 200) {
    420   1.1  itojun 			p = strstr(rbuf, "PORT");
    421   1.1  itojun 			if (p) {
    422   1.1  itojun 				p[0] = (state == LPRT) ? 'L' : 'E';
    423   1.1  itojun 				p[1] = 'P';
    424   1.1  itojun 			}
    425   1.1  itojun 		} else {
    426   1.1  itojun 			close(wport4);
    427   1.1  itojun 			wport4 = -1;
    428   1.1  itojun 		}
    429   1.1  itojun 		write(dst, rbuf, n);
    430   1.1  itojun 		return n;
    431   1.1  itojun 	case LPSV:
    432   1.1  itojun 	case EPSV:
    433   1.6  itojun 		/*
    434   1.6  itojun 		 * expecting "227 Entering Passive Mode (x,x,x,x,x,x,x)"
    435   1.6  itojun 		 * (in some cases result comes without paren)
    436   1.6  itojun 		 */
    437   1.1  itojun 		if (code != 227) {
    438   1.1  itojun passivefail0:
    439   1.1  itojun 			close(wport6);
    440   1.1  itojun 			wport6 = -1;
    441   1.1  itojun 			write(dst, rbuf, n);
    442   1.1  itojun 			return n;
    443   1.1  itojun 		}
    444   1.1  itojun 
    445   1.1  itojun 	    {
    446   1.1  itojun 		unsigned int ho[4], po[2];
    447   1.1  itojun 		struct sockaddr_in *sin;
    448   1.1  itojun 		struct sockaddr_in6 *sin6;
    449   1.1  itojun 		u_short port;
    450   1.1  itojun 
    451   1.1  itojun 		/*
    452   1.1  itojun 		 * PASV result -> LPSV/EPSV result
    453   1.1  itojun 		 */
    454   1.1  itojun 		p = param;
    455   1.6  itojun 		while (*p && *p != '(' && !isdigit(*p))	/*)*/
    456   1.1  itojun 			p++;
    457   1.1  itojun 		if (!*p)
    458   1.1  itojun 			goto passivefail0;	/*XXX*/
    459   1.6  itojun 		if (*p == '(')	/*)*/
    460   1.6  itojun 			p++;
    461   1.1  itojun 		n = sscanf(p, "%u,%u,%u,%u,%u,%u",
    462   1.1  itojun 			&ho[0], &ho[1], &ho[2], &ho[3], &po[0], &po[1]);
    463   1.1  itojun 		if (n != 6)
    464   1.1  itojun 			goto passivefail0;	/*XXX*/
    465   1.1  itojun 
    466   1.1  itojun 		/* keep PORT parameter */
    467   1.1  itojun 		memset(&data4, 0, sizeof(data4));
    468   1.1  itojun 		sin = (struct sockaddr_in *)&data4;
    469   1.1  itojun 		sin->sin_len = sizeof(*sin);
    470   1.1  itojun 		sin->sin_family = AF_INET;
    471   1.1  itojun 		sin->sin_addr.s_addr = 0;
    472   1.1  itojun 		for (n = 0; n < 4; n++) {
    473   1.1  itojun 			sin->sin_addr.s_addr |=
    474   1.1  itojun 				htonl((ho[n] & 0xff) << ((3 - n) * 8));
    475   1.1  itojun 		}
    476   1.1  itojun 		sin->sin_port = htons(((po[0] & 0xff) << 8) | (po[1] & 0xff));
    477   1.1  itojun 
    478   1.1  itojun 		/* get ready for passive data connection */
    479   1.1  itojun 		memset(&data6, 0, sizeof(data6));
    480   1.1  itojun 		sin6 = (struct sockaddr_in6 *)&data6;
    481   1.1  itojun 		sin6->sin6_len = sizeof(*sin6);
    482   1.1  itojun 		sin6->sin6_family = AF_INET6;
    483   1.1  itojun 		wport6 = socket(sin6->sin6_family, SOCK_STREAM, 0);
    484   1.1  itojun 		if (wport6 == -1) {
    485   1.1  itojun passivefail:
    486   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    487   1.1  itojun 				"500 could not translate from PASV\r\n");
    488   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    489  1.10  itojun 				n = 0;
    490   1.7  itojun 			if (n)
    491   1.7  itojun 				write(src, sbuf, n);
    492   1.1  itojun 			return n;
    493   1.1  itojun 		}
    494   1.1  itojun #ifdef IPV6_FAITH
    495   1.1  itojun 	    {
    496   1.1  itojun 		int on = 1;
    497   1.1  itojun 		error = setsockopt(wport6, IPPROTO_IPV6, IPV6_FAITH,
    498   1.1  itojun 			&on, sizeof(on));
    499   1.1  itojun 		if (error == -1)
    500   1.7  itojun 			exit_failure("setsockopt(IPV6_FAITH): %s", strerror(errno));
    501   1.1  itojun 	    }
    502   1.1  itojun #endif
    503   1.1  itojun 		error = bind(wport6, (struct sockaddr *)sin6, sin6->sin6_len);
    504   1.1  itojun 		if (error == -1) {
    505   1.1  itojun 			close(wport6);
    506   1.1  itojun 			wport6 = -1;
    507   1.1  itojun 			goto passivefail;
    508   1.1  itojun 		}
    509   1.1  itojun 		error = listen(wport6, 1);
    510   1.1  itojun 		if (error == -1) {
    511   1.1  itojun 			close(wport6);
    512   1.1  itojun 			wport6 = -1;
    513   1.1  itojun 			goto passivefail;
    514   1.1  itojun 		}
    515   1.1  itojun 
    516   1.1  itojun 		/* transmit LPSV or EPSV */
    517   1.1  itojun 		/*
    518   1.1  itojun 		 * addr from dst, port from wport6
    519   1.1  itojun 		 */
    520   1.1  itojun 		n = sizeof(data6);
    521   1.1  itojun 		error = getsockname(wport6, (struct sockaddr *)&data6, &n);
    522   1.1  itojun 		if (error == -1) {
    523   1.1  itojun 			close(wport6);
    524   1.1  itojun 			wport6 = -1;
    525   1.1  itojun 			goto passivefail;
    526   1.1  itojun 		}
    527   1.1  itojun 		sin6 = (struct sockaddr_in6 *)&data6;
    528   1.1  itojun 		port = sin6->sin6_port;
    529   1.1  itojun 
    530   1.1  itojun 		n = sizeof(data6);
    531   1.1  itojun 		error = getsockname(dst, (struct sockaddr *)&data6, &n);
    532   1.1  itojun 		if (error == -1) {
    533   1.1  itojun 			close(wport6);
    534   1.1  itojun 			wport6 = -1;
    535   1.1  itojun 			goto passivefail;
    536   1.1  itojun 		}
    537   1.1  itojun 		sin6 = (struct sockaddr_in6 *)&data6;
    538   1.1  itojun 		sin6->sin6_port = port;
    539   1.1  itojun 
    540   1.1  itojun 		if (state == LPSV) {
    541   1.1  itojun 			a = (char *)&sin6->sin6_addr;
    542   1.1  itojun 			p = (char *)&sin6->sin6_port;
    543   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    544   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",
    545   1.5  itojun 				6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
    546   1.5  itojun 				UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
    547   1.5  itojun 				UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
    548   1.5  itojun 				UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
    549   1.1  itojun 				2, UC(p[0]), UC(p[1]));
    550   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    551   1.7  itojun 				n = 0;
    552   1.7  itojun 			if (n)
    553   1.7  itojun 				write(dst, sbuf, n);
    554   1.1  itojun 			passivemode = 1;
    555   1.1  itojun 			return n;
    556   1.1  itojun 		} else {
    557   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    558   1.1  itojun "229 Entering Extended Passive Mode (|||%d|)\r\n",
    559   1.1  itojun 				ntohs(sin6->sin6_port));
    560   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    561   1.7  itojun 				n = 0;
    562   1.7  itojun 			if (n)
    563   1.7  itojun 				write(dst, sbuf, n);
    564   1.1  itojun 			passivemode = 1;
    565   1.1  itojun 			return n;
    566   1.1  itojun 		}
    567   1.1  itojun 	    }
    568   1.1  itojun 	}
    569   1.1  itojun 
    570   1.1  itojun  bad:
    571   1.7  itojun 	exit_failure("%s", strerror(errno));
    572   1.1  itojun 	/*NOTREACHED*/
    573   1.1  itojun 	return 0;	/* to make gcc happy */
    574   1.1  itojun }
    575   1.1  itojun 
    576   1.1  itojun static int
    577   1.1  itojun ftp_copycommand(int src, int dst, enum state *state)
    578   1.1  itojun {
    579   1.1  itojun 	int error, atmark;
    580   1.1  itojun 	int n;
    581   1.1  itojun 	unsigned int af, hal, ho[16], pal, po[2];
    582   1.7  itojun 	char *a, *p, *q;
    583   1.1  itojun 	char cmd[5], *param;
    584   1.1  itojun 	struct sockaddr_in *sin;
    585   1.1  itojun 	struct sockaddr_in6 *sin6;
    586   1.1  itojun 	enum state nstate;
    587   1.1  itojun 	char ch;
    588   1.7  itojun 	int i;
    589   1.1  itojun 
    590   1.1  itojun 	/* OOB data handling */
    591   1.1  itojun 	error = ioctl(src, SIOCATMARK, &atmark);
    592   1.1  itojun 	if (error != -1 && atmark == 1) {
    593   1.1  itojun 		n = read(src, rbuf, 1);
    594   1.1  itojun 		if (n == -1)
    595   1.1  itojun 			goto bad;
    596   1.1  itojun 		send(dst, rbuf, n, MSG_OOB);
    597   1.1  itojun #if 0
    598   1.1  itojun 		n = read(src, rbuf, sizeof(rbuf));
    599   1.1  itojun 		if (n == -1)
    600   1.1  itojun 			goto bad;
    601   1.1  itojun 		write(dst, rbuf, n);
    602   1.1  itojun 		return n;
    603   1.1  itojun #endif
    604   1.1  itojun 	}
    605   1.1  itojun 
    606   1.1  itojun 	n = read(src, rbuf, sizeof(rbuf));
    607   1.1  itojun 	if (n <= 0)
    608   1.1  itojun 		return n;
    609   1.1  itojun 	rbuf[n] = '\0';
    610   1.1  itojun 
    611   1.1  itojun 	if (n < 4) {
    612   1.1  itojun 		write(dst, rbuf, n);
    613   1.1  itojun 		return n;
    614   1.1  itojun 	}
    615   1.1  itojun 
    616   1.1  itojun 	/*
    617   1.1  itojun 	 * parse argument
    618   1.1  itojun 	 */
    619   1.1  itojun 	p = rbuf;
    620   1.1  itojun 	q = cmd;
    621   1.1  itojun 	for (i = 0; i < 4; i++) {
    622   1.1  itojun 		if (!isalpha(*p)) {
    623   1.1  itojun 			/* invalid command */
    624   1.1  itojun 			write(dst, rbuf, n);
    625   1.1  itojun 			return n;
    626   1.1  itojun 		}
    627   1.1  itojun 		*q++ = islower(*p) ? toupper(*p) : *p;
    628   1.1  itojun 		p++;
    629   1.1  itojun 	}
    630   1.1  itojun 	if (!isspace(*p)) {
    631   1.1  itojun 		/* invalid command */
    632   1.1  itojun 		write(dst, rbuf, n);
    633   1.1  itojun 		return n;
    634   1.1  itojun 	}
    635   1.1  itojun 	*q = '\0';
    636   1.1  itojun 	param = p;
    637   1.1  itojun 	/* param points to first non-command token, if any */
    638   1.1  itojun 	while (*param && isspace(*param))
    639   1.1  itojun 		param++;
    640   1.1  itojun 	if (!*param)
    641   1.1  itojun 		param = NULL;
    642   1.1  itojun 
    643   1.1  itojun 	*state = NONE;
    644   1.1  itojun 
    645   1.1  itojun 	if (strcmp(cmd, "LPRT") == 0 && param) {
    646   1.1  itojun 		/*
    647   1.1  itojun 		 * LPRT -> PORT
    648   1.1  itojun 		 */
    649   1.1  itojun 		nstate = LPRT;
    650   1.1  itojun 
    651   1.1  itojun 		close(wport4);
    652   1.1  itojun 		close(wport6);
    653   1.1  itojun 		close(port4);
    654   1.1  itojun 		close(port6);
    655   1.1  itojun 		wport4 = wport6 = port4 = port6 = -1;
    656   1.1  itojun 
    657   1.1  itojun 		if (epsvall) {
    658   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
    659   1.1  itojun 				cmd);
    660   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    661   1.7  itojun 				n = 0;
    662   1.7  itojun 			if (n)
    663   1.7  itojun 				write(src, sbuf, n);
    664   1.1  itojun 			return n;
    665   1.1  itojun 		}
    666   1.1  itojun 
    667   1.1  itojun 		n = sscanf(param,
    668   1.1  itojun "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
    669   1.1  itojun 			      &af, &hal, &ho[0], &ho[1], &ho[2], &ho[3],
    670   1.1  itojun 			      &ho[4], &ho[5], &ho[6], &ho[7],
    671   1.1  itojun 			      &ho[8], &ho[9], &ho[10], &ho[11],
    672   1.1  itojun 			      &ho[12], &ho[13], &ho[14], &ho[15],
    673   1.1  itojun 			      &pal, &po[0], &po[1]);
    674   1.1  itojun 		if (n != 21 || af != 6 || hal != 16|| pal != 2) {
    675   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    676   1.1  itojun 				"501 illegal parameter to LPRT\r\n");
    677   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    678   1.7  itojun 				n = 0;
    679   1.7  itojun 			if (n)
    680   1.7  itojun 				write(src, sbuf, n);
    681   1.1  itojun 			return n;
    682   1.1  itojun 		}
    683   1.1  itojun 
    684   1.1  itojun 		/* keep LPRT parameter */
    685   1.1  itojun 		memset(&data6, 0, sizeof(data6));
    686   1.1  itojun 		sin6 = (struct sockaddr_in6 *)&data6;
    687   1.1  itojun 		sin6->sin6_len = sizeof(*sin6);
    688   1.1  itojun 		sin6->sin6_family = AF_INET6;
    689   1.1  itojun 		for (n = 0; n < 16; n++)
    690   1.2  itojun 			sin6->sin6_addr.s6_addr[n] = ho[n];
    691   1.1  itojun 		sin6->sin6_port = htons(((po[0] & 0xff) << 8) | (po[1] & 0xff));
    692   1.1  itojun 
    693   1.1  itojun sendport:
    694   1.1  itojun 		/* get ready for active data connection */
    695   1.1  itojun 		n = sizeof(data4);
    696   1.1  itojun 		error = getsockname(dst, (struct sockaddr *)&data4, &n);
    697   1.1  itojun 		if (error == -1) {
    698   1.1  itojun lprtfail:
    699   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    700   1.1  itojun 				"500 could not translate to PORT\r\n");
    701   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    702   1.7  itojun 				n = 0;
    703   1.7  itojun 			if (n)
    704   1.7  itojun 				write(src, sbuf, n);
    705   1.1  itojun 			return n;
    706   1.1  itojun 		}
    707   1.2  itojun 		if (((struct sockaddr *)&data4)->sa_family != AF_INET)
    708   1.1  itojun 			goto lprtfail;
    709   1.1  itojun 		sin = (struct sockaddr_in *)&data4;
    710   1.1  itojun 		sin->sin_port = 0;
    711   1.1  itojun 		wport4 = socket(sin->sin_family, SOCK_STREAM, 0);
    712   1.1  itojun 		if (wport4 == -1)
    713   1.1  itojun 			goto lprtfail;
    714   1.1  itojun 		error = bind(wport4, (struct sockaddr *)sin, sin->sin_len);
    715   1.1  itojun 		if (error == -1) {
    716   1.1  itojun 			close(wport4);
    717   1.1  itojun 			wport4 = -1;
    718   1.1  itojun 			goto lprtfail;
    719   1.1  itojun 		}
    720   1.1  itojun 		error = listen(wport4, 1);
    721   1.1  itojun 		if (error == -1) {
    722   1.1  itojun 			close(wport4);
    723   1.1  itojun 			wport4 = -1;
    724   1.1  itojun 			goto lprtfail;
    725   1.1  itojun 		}
    726   1.1  itojun 
    727   1.1  itojun 		/* transmit PORT */
    728   1.1  itojun 		n = sizeof(data4);
    729   1.1  itojun 		error = getsockname(wport4, (struct sockaddr *)&data4, &n);
    730   1.1  itojun 		if (error == -1) {
    731   1.1  itojun 			close(wport4);
    732   1.1  itojun 			wport4 = -1;
    733   1.1  itojun 			goto lprtfail;
    734   1.1  itojun 		}
    735   1.2  itojun 		if (((struct sockaddr *)&data4)->sa_family != AF_INET) {
    736   1.1  itojun 			close(wport4);
    737   1.1  itojun 			wport4 = -1;
    738   1.1  itojun 			goto lprtfail;
    739   1.1  itojun 		}
    740   1.1  itojun 		sin = (struct sockaddr_in *)&data4;
    741   1.1  itojun 		a = (char *)&sin->sin_addr;
    742   1.1  itojun 		p = (char *)&sin->sin_port;
    743   1.4  itojun 		n = snprintf(sbuf, sizeof(sbuf), "PORT %d,%d,%d,%d,%d,%d\r\n",
    744   1.1  itojun 				  UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
    745   1.1  itojun 				  UC(p[0]), UC(p[1]));
    746   1.7  itojun 		if (n < 0 || n >= sizeof(sbuf))
    747   1.7  itojun 			n = 0;
    748   1.7  itojun 		if (n)
    749   1.7  itojun 			write(dst, sbuf, n);
    750   1.1  itojun 		*state = nstate;
    751   1.1  itojun 		passivemode = 0;
    752   1.1  itojun 		return n;
    753   1.1  itojun 	} else if (strcmp(cmd, "EPRT") == 0 && param) {
    754   1.1  itojun 		/*
    755   1.1  itojun 		 * EPRT -> PORT
    756   1.1  itojun 		 */
    757   1.1  itojun 		char *afp, *hostp, *portp;
    758   1.1  itojun 		struct addrinfo hints, *res;
    759   1.1  itojun 
    760   1.1  itojun 		nstate = EPRT;
    761   1.1  itojun 
    762   1.1  itojun 		close(wport4);
    763   1.1  itojun 		close(wport6);
    764   1.1  itojun 		close(port4);
    765   1.1  itojun 		close(port6);
    766   1.1  itojun 		wport4 = wport6 = port4 = port6 = -1;
    767   1.1  itojun 
    768   1.1  itojun 		if (epsvall) {
    769   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
    770   1.1  itojun 				cmd);
    771   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    772   1.7  itojun 				n = 0;
    773   1.7  itojun 			if (n)
    774   1.7  itojun 				write(src, sbuf, n);
    775   1.1  itojun 			return n;
    776   1.1  itojun 		}
    777   1.1  itojun 
    778   1.1  itojun 		p = param;
    779   1.1  itojun 		ch = *p++;	/* boundary character */
    780   1.1  itojun 		afp = p;
    781   1.1  itojun 		while (*p && *p != ch)
    782   1.1  itojun 			p++;
    783   1.1  itojun 		if (!*p) {
    784   1.1  itojun eprtparamfail:
    785   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    786   1.1  itojun 				"501 illegal parameter to EPRT\r\n");
    787   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    788   1.7  itojun 				n = 0;
    789   1.7  itojun 			if (n)
    790   1.7  itojun 				write(src, sbuf, n);
    791   1.1  itojun 			return n;
    792   1.1  itojun 		}
    793   1.1  itojun 		*p++ = '\0';
    794   1.1  itojun 		hostp = p;
    795   1.1  itojun 		while (*p && *p != ch)
    796   1.1  itojun 			p++;
    797   1.1  itojun 		if (!*p)
    798   1.1  itojun 			goto eprtparamfail;
    799   1.1  itojun 		*p++ = '\0';
    800   1.1  itojun 		portp = p;
    801   1.1  itojun 		while (*p && *p != ch)
    802   1.1  itojun 			p++;
    803   1.1  itojun 		if (!*p)
    804   1.1  itojun 			goto eprtparamfail;
    805   1.1  itojun 		*p++ = '\0';
    806   1.1  itojun 
    807   1.1  itojun 		n = sscanf(afp, "%d", &af);
    808   1.1  itojun 		if (n != 1 || af != 2) {
    809   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    810   1.1  itojun 				"501 unsupported address family to EPRT\r\n");
    811   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    812   1.7  itojun 				n = 0;
    813   1.7  itojun 			if (n)
    814   1.7  itojun 				write(src, sbuf, n);
    815   1.1  itojun 			return n;
    816   1.1  itojun 		}
    817   1.1  itojun 		memset(&hints, 0, sizeof(hints));
    818   1.1  itojun 		hints.ai_family = AF_UNSPEC;
    819   1.6  itojun 		hints.ai_socktype = SOCK_STREAM;
    820   1.1  itojun 		error = getaddrinfo(hostp, portp, &hints, &res);
    821   1.1  itojun 		if (error) {
    822   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    823   1.1  itojun 				"501 EPRT: %s\r\n", gai_strerror(error));
    824   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    825   1.7  itojun 				n = 0;
    826   1.7  itojun 			if (n)
    827   1.7  itojun 				write(src, sbuf, n);
    828   1.1  itojun 			return n;
    829   1.1  itojun 		}
    830   1.1  itojun 		if (res->ai_next) {
    831   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf),
    832   1.1  itojun 				"501 EPRT: %s resolved to multiple addresses\r\n", hostp);
    833   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    834   1.7  itojun 				n = 0;
    835   1.7  itojun 			if (n)
    836   1.7  itojun 				write(src, sbuf, n);
    837   1.1  itojun 			return n;
    838   1.1  itojun 		}
    839   1.1  itojun 
    840   1.1  itojun 		memcpy(&data6, res->ai_addr, res->ai_addrlen);
    841   1.1  itojun 
    842   1.1  itojun 		goto sendport;
    843   1.1  itojun 	} else if (strcmp(cmd, "LPSV") == 0 && !param) {
    844   1.1  itojun 		/*
    845   1.1  itojun 		 * LPSV -> PASV
    846   1.1  itojun 		 */
    847   1.1  itojun 		nstate = LPSV;
    848   1.1  itojun 
    849   1.1  itojun 		close(wport4);
    850   1.1  itojun 		close(wport6);
    851   1.1  itojun 		close(port4);
    852   1.1  itojun 		close(port6);
    853   1.1  itojun 		wport4 = wport6 = port4 = port6 = -1;
    854   1.1  itojun 
    855   1.1  itojun 		if (epsvall) {
    856   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
    857   1.1  itojun 				cmd);
    858   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    859   1.7  itojun 				n = 0;
    860   1.7  itojun 			if (n)
    861   1.7  itojun 				write(src, sbuf, n);
    862   1.1  itojun 			return n;
    863   1.1  itojun 		}
    864   1.1  itojun 
    865   1.1  itojun 		/* transmit PASV */
    866   1.4  itojun 		n = snprintf(sbuf, sizeof(sbuf), "PASV\r\n");
    867   1.7  itojun 		if (n < 0 || n >= sizeof(sbuf))
    868   1.7  itojun 			n = 0;
    869   1.7  itojun 		if (n)
    870   1.7  itojun 			write(dst, sbuf, n);
    871   1.1  itojun 		*state = LPSV;
    872   1.1  itojun 		passivemode = 0;	/* to be set to 1 later */
    873   1.1  itojun 		return n;
    874   1.1  itojun 	} else if (strcmp(cmd, "EPSV") == 0 && !param) {
    875   1.1  itojun 		/*
    876   1.1  itojun 		 * EPSV -> PASV
    877   1.1  itojun 		 */
    878   1.1  itojun 		close(wport4);
    879   1.1  itojun 		close(wport6);
    880   1.1  itojun 		close(port4);
    881   1.1  itojun 		close(port6);
    882   1.1  itojun 		wport4 = wport6 = port4 = port6 = -1;
    883   1.1  itojun 
    884   1.4  itojun 		n = snprintf(sbuf, sizeof(sbuf), "PASV\r\n");
    885   1.7  itojun 		if (n < 0 || n >= sizeof(sbuf))
    886   1.7  itojun 			n = 0;
    887   1.7  itojun 		if (n)
    888   1.7  itojun 			write(dst, sbuf, n);
    889   1.1  itojun 		*state = EPSV;
    890   1.1  itojun 		passivemode = 0;	/* to be set to 1 later */
    891   1.1  itojun 		return n;
    892   1.1  itojun 	} else if (strcmp(cmd, "EPSV") == 0 && param
    893   1.1  itojun 	 && strncasecmp(param, "ALL", 3) == 0 && isspace(param[3])) {
    894   1.1  itojun 		/*
    895   1.1  itojun 		 * EPSV ALL
    896   1.1  itojun 		 */
    897   1.1  itojun 		epsvall = 1;
    898   1.4  itojun 		n = snprintf(sbuf, sizeof(sbuf), "200 EPSV ALL command successful.\r\n");
    899   1.7  itojun 		if (n < 0 || n >= sizeof(sbuf))
    900   1.7  itojun 			n = 0;
    901   1.7  itojun 		if (n)
    902   1.7  itojun 			write(src, sbuf, n);
    903   1.1  itojun 		return n;
    904   1.1  itojun 	} else if (strcmp(cmd, "PORT") == 0 || strcmp(cmd, "PASV") == 0) {
    905   1.1  itojun 		/*
    906   1.1  itojun 		 * reject PORT/PASV
    907   1.1  itojun 		 */
    908   1.4  itojun 		n = snprintf(sbuf, sizeof(sbuf), "502 %s not implemented.\r\n", cmd);
    909   1.7  itojun 		if (n < 0 || n >= sizeof(sbuf))
    910   1.7  itojun 			n = 0;
    911   1.7  itojun 		if (n)
    912   1.7  itojun 			write(src, sbuf, n);
    913   1.1  itojun 		return n;
    914   1.1  itojun 	} else if (passivemode
    915   1.1  itojun 		&& (strcmp(cmd, "STOR") == 0
    916   1.1  itojun 		 || strcmp(cmd, "STOU") == 0
    917   1.1  itojun 		 || strcmp(cmd, "RETR") == 0
    918   1.1  itojun 		 || strcmp(cmd, "LIST") == 0
    919   1.1  itojun 		 || strcmp(cmd, "NLST") == 0
    920   1.1  itojun 		 || strcmp(cmd, "APPE") == 0)) {
    921   1.1  itojun 		/*
    922   1.1  itojun 		 * commands with data transfer.  need to care about passive
    923   1.1  itojun 		 * mode data connection.
    924   1.1  itojun 		 */
    925   1.1  itojun 
    926   1.1  itojun 		if (ftp_passiveconn() < 0) {
    927   1.4  itojun 			n = snprintf(sbuf, sizeof(sbuf), "425 Cannot open data connetion\r\n");
    928   1.7  itojun 			if (n < 0 || n >= sizeof(sbuf))
    929   1.7  itojun 				n = 0;
    930   1.7  itojun 			if (n)
    931   1.7  itojun 				write(src, sbuf, n);
    932   1.1  itojun 		} else {
    933   1.1  itojun 			/* simply relay the command */
    934   1.1  itojun 			write(dst, rbuf, n);
    935   1.1  itojun 		}
    936   1.1  itojun 
    937   1.1  itojun 		*state = NONE;
    938   1.1  itojun 		return n;
    939   1.1  itojun 	} else {
    940   1.1  itojun 		/* simply relay it */
    941   1.1  itojun 		*state = NONE;
    942   1.1  itojun 		write(dst, rbuf, n);
    943   1.1  itojun 		return n;
    944   1.1  itojun 	}
    945   1.1  itojun 
    946   1.1  itojun  bad:
    947   1.7  itojun 	exit_failure("%s", strerror(errno));
    948   1.1  itojun 	/*NOTREACHED*/
    949   1.1  itojun 	return 0;	/* to make gcc happy */
    950   1.1  itojun }
    951