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