ftp.c revision 1.5 1 1.5 itojun /* $NetBSD: ftp.c,v 1.5 2000/05/31 03:18:02 itojun Exp $ */
2 1.5 itojun /* $KAME: ftp.c,v 1.7 2000/05/31 03:06:07 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.1 itojun /* expecting "227 Entering Passive Mode (x,x,x,x,x,x,x)" */
452 1.1 itojun if (code != 227) {
453 1.1 itojun passivefail0:
454 1.1 itojun close(wport6);
455 1.1 itojun wport6 = -1;
456 1.1 itojun write(dst, rbuf, n);
457 1.1 itojun return n;
458 1.1 itojun }
459 1.1 itojun
460 1.1 itojun {
461 1.1 itojun unsigned int ho[4], po[2];
462 1.1 itojun struct sockaddr_in *sin;
463 1.1 itojun struct sockaddr_in6 *sin6;
464 1.1 itojun u_short port;
465 1.1 itojun char *p;
466 1.1 itojun
467 1.1 itojun /*
468 1.1 itojun * PASV result -> LPSV/EPSV result
469 1.1 itojun */
470 1.1 itojun p = param;
471 1.1 itojun while (*p && *p != '(')
472 1.1 itojun p++;
473 1.1 itojun if (!*p)
474 1.1 itojun goto passivefail0; /*XXX*/
475 1.1 itojun p++;
476 1.1 itojun n = sscanf(p, "%u,%u,%u,%u,%u,%u",
477 1.1 itojun &ho[0], &ho[1], &ho[2], &ho[3], &po[0], &po[1]);
478 1.1 itojun if (n != 6)
479 1.1 itojun goto passivefail0; /*XXX*/
480 1.1 itojun
481 1.1 itojun /* keep PORT parameter */
482 1.1 itojun memset(&data4, 0, sizeof(data4));
483 1.1 itojun sin = (struct sockaddr_in *)&data4;
484 1.1 itojun sin->sin_len = sizeof(*sin);
485 1.1 itojun sin->sin_family = AF_INET;
486 1.1 itojun sin->sin_addr.s_addr = 0;
487 1.1 itojun for (n = 0; n < 4; n++) {
488 1.1 itojun sin->sin_addr.s_addr |=
489 1.1 itojun htonl((ho[n] & 0xff) << ((3 - n) * 8));
490 1.1 itojun }
491 1.1 itojun sin->sin_port = htons(((po[0] & 0xff) << 8) | (po[1] & 0xff));
492 1.1 itojun
493 1.1 itojun /* get ready for passive data connection */
494 1.1 itojun memset(&data6, 0, sizeof(data6));
495 1.1 itojun sin6 = (struct sockaddr_in6 *)&data6;
496 1.1 itojun sin6->sin6_len = sizeof(*sin6);
497 1.1 itojun sin6->sin6_family = AF_INET6;
498 1.1 itojun wport6 = socket(sin6->sin6_family, SOCK_STREAM, 0);
499 1.1 itojun if (wport6 == -1) {
500 1.1 itojun passivefail:
501 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
502 1.1 itojun "500 could not translate from PASV\r\n");
503 1.1 itojun write(src, sbuf, n);
504 1.1 itojun return n;
505 1.1 itojun }
506 1.1 itojun #ifdef IPV6_FAITH
507 1.1 itojun {
508 1.1 itojun int on = 1;
509 1.1 itojun error = setsockopt(wport6, IPPROTO_IPV6, IPV6_FAITH,
510 1.1 itojun &on, sizeof(on));
511 1.1 itojun if (error == -1)
512 1.3 itojun exit_error("setsockopt(IPV6_FAITH): %s", ERRSTR);
513 1.1 itojun }
514 1.1 itojun #endif
515 1.1 itojun error = bind(wport6, (struct sockaddr *)sin6, sin6->sin6_len);
516 1.1 itojun if (error == -1) {
517 1.1 itojun close(wport6);
518 1.1 itojun wport6 = -1;
519 1.1 itojun goto passivefail;
520 1.1 itojun }
521 1.1 itojun error = listen(wport6, 1);
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
528 1.1 itojun /* transmit LPSV or EPSV */
529 1.1 itojun /*
530 1.1 itojun * addr from dst, port from wport6
531 1.1 itojun */
532 1.1 itojun n = sizeof(data6);
533 1.1 itojun error = getsockname(wport6, (struct sockaddr *)&data6, &n);
534 1.1 itojun if (error == -1) {
535 1.1 itojun close(wport6);
536 1.1 itojun wport6 = -1;
537 1.1 itojun goto passivefail;
538 1.1 itojun }
539 1.1 itojun sin6 = (struct sockaddr_in6 *)&data6;
540 1.1 itojun port = sin6->sin6_port;
541 1.1 itojun
542 1.1 itojun n = sizeof(data6);
543 1.1 itojun error = getsockname(dst, (struct sockaddr *)&data6, &n);
544 1.1 itojun if (error == -1) {
545 1.1 itojun close(wport6);
546 1.1 itojun wport6 = -1;
547 1.1 itojun goto passivefail;
548 1.1 itojun }
549 1.1 itojun sin6 = (struct sockaddr_in6 *)&data6;
550 1.1 itojun sin6->sin6_port = port;
551 1.1 itojun
552 1.1 itojun if (state == LPSV) {
553 1.1 itojun char *a, *p;
554 1.1 itojun
555 1.1 itojun a = (char *)&sin6->sin6_addr;
556 1.1 itojun p = (char *)&sin6->sin6_port;
557 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
558 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",
559 1.5 itojun 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
560 1.5 itojun UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
561 1.5 itojun UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
562 1.5 itojun UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
563 1.1 itojun 2, UC(p[0]), UC(p[1]));
564 1.1 itojun write(dst, sbuf, n);
565 1.1 itojun passivemode = 1;
566 1.1 itojun return n;
567 1.1 itojun } else {
568 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
569 1.1 itojun "229 Entering Extended Passive Mode (|||%d|)\r\n",
570 1.1 itojun ntohs(sin6->sin6_port));
571 1.1 itojun write(dst, sbuf, n);
572 1.1 itojun passivemode = 1;
573 1.1 itojun return n;
574 1.1 itojun }
575 1.1 itojun }
576 1.1 itojun #ifdef FAITH4
577 1.1 itojun case PASV:
578 1.1 itojun /* expecting "229 Entering Extended Passive Mode (|||x|)" */
579 1.1 itojun if (code != 229) {
580 1.1 itojun passivefail1:
581 1.1 itojun close(wport6);
582 1.1 itojun wport6 = -1;
583 1.1 itojun write(dst, rbuf, n);
584 1.1 itojun return n;
585 1.1 itojun }
586 1.1 itojun
587 1.1 itojun {
588 1.1 itojun u_short port;
589 1.1 itojun char *p;
590 1.1 itojun struct sockaddr_in *sin;
591 1.1 itojun struct sockaddr_in6 *sin6;
592 1.1 itojun
593 1.1 itojun /*
594 1.1 itojun * EPSV result -> PORT result
595 1.1 itojun */
596 1.1 itojun p = param;
597 1.1 itojun while (*p && *p != '(')
598 1.1 itojun p++;
599 1.1 itojun if (!*p)
600 1.1 itojun goto passivefail1; /*XXX*/
601 1.1 itojun p++;
602 1.1 itojun n = sscanf(p, "|||%hu|", &port);
603 1.1 itojun if (n != 1)
604 1.1 itojun goto passivefail1; /*XXX*/
605 1.1 itojun
606 1.1 itojun /* keep EPRT parameter */
607 1.1 itojun n = sizeof(data4);
608 1.1 itojun error = getpeername(src, (struct sockaddr *)&data4, &n);
609 1.1 itojun if (error == -1)
610 1.1 itojun goto passivefail1; /*XXX*/
611 1.1 itojun sin6 = (struct sockaddr_in6 *)&data4;
612 1.1 itojun sin6->sin6_port = htons(port);
613 1.1 itojun
614 1.1 itojun /* get ready for passive data connection */
615 1.1 itojun memset(&data6, 0, sizeof(data6));
616 1.1 itojun sin = (struct sockaddr_in *)&data6;
617 1.1 itojun sin->sin_len = sizeof(*sin);
618 1.1 itojun sin->sin_family = AF_INET;
619 1.1 itojun wport6 = socket(sin->sin_family, SOCK_STREAM, 0);
620 1.1 itojun if (wport6 == -1) {
621 1.1 itojun passivefail2:
622 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
623 1.1 itojun "500 could not translate from EPSV\r\n");
624 1.1 itojun write(src, sbuf, n);
625 1.1 itojun return n;
626 1.1 itojun }
627 1.1 itojun #ifdef IP_FAITH
628 1.1 itojun {
629 1.1 itojun int on = 1;
630 1.1 itojun error = setsockopt(wport6, IPPROTO_IP, IP_FAITH,
631 1.1 itojun &on, sizeof(on));
632 1.1 itojun if (error == -1)
633 1.3 itojun exit_error("setsockopt(IP_FAITH): %s", ERRSTR);
634 1.1 itojun }
635 1.1 itojun #endif
636 1.1 itojun error = bind(wport6, (struct sockaddr *)sin, sin->sin_len);
637 1.1 itojun if (error == -1) {
638 1.1 itojun close(wport6);
639 1.1 itojun wport6 = -1;
640 1.1 itojun goto passivefail2;
641 1.1 itojun }
642 1.1 itojun error = listen(wport6, 1);
643 1.1 itojun if (error == -1) {
644 1.1 itojun close(wport6);
645 1.1 itojun wport6 = -1;
646 1.1 itojun goto passivefail2;
647 1.1 itojun }
648 1.1 itojun
649 1.1 itojun /* transmit PORT */
650 1.1 itojun /*
651 1.1 itojun * addr from dst, port from wport6
652 1.1 itojun */
653 1.1 itojun n = sizeof(data6);
654 1.1 itojun error = getsockname(wport6, (struct sockaddr *)&data6, &n);
655 1.1 itojun if (error == -1) {
656 1.1 itojun close(wport6);
657 1.1 itojun wport6 = -1;
658 1.1 itojun goto passivefail2;
659 1.1 itojun }
660 1.1 itojun sin = (struct sockaddr_in *)&data6;
661 1.1 itojun port = sin->sin_port;
662 1.1 itojun
663 1.1 itojun n = sizeof(data6);
664 1.1 itojun error = getsockname(dst, (struct sockaddr *)&data6, &n);
665 1.1 itojun if (error == -1) {
666 1.1 itojun close(wport6);
667 1.1 itojun wport6 = -1;
668 1.1 itojun goto passivefail2;
669 1.1 itojun }
670 1.1 itojun sin = (struct sockaddr_in *)&data6;
671 1.1 itojun sin->sin_port = port;
672 1.1 itojun
673 1.1 itojun {
674 1.1 itojun char *a, *p;
675 1.1 itojun
676 1.1 itojun a = (char *)&sin->sin_addr;
677 1.1 itojun p = (char *)&sin->sin_port;
678 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
679 1.1 itojun "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n",
680 1.5 itojun UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
681 1.1 itojun UC(p[0]), UC(p[1]));
682 1.1 itojun write(dst, sbuf, n);
683 1.1 itojun passivemode = 1;
684 1.1 itojun return n;
685 1.1 itojun }
686 1.1 itojun }
687 1.1 itojun #endif /* FAITH4 */
688 1.1 itojun }
689 1.1 itojun
690 1.1 itojun bad:
691 1.1 itojun exit_failure(ERRSTR);
692 1.1 itojun /*NOTREACHED*/
693 1.1 itojun return 0; /* to make gcc happy */
694 1.1 itojun }
695 1.1 itojun
696 1.1 itojun static int
697 1.1 itojun ftp_copycommand(int src, int dst, enum state *state)
698 1.1 itojun {
699 1.1 itojun int error, atmark;
700 1.1 itojun int n;
701 1.1 itojun unsigned int af, hal, ho[16], pal, po[2];
702 1.1 itojun char *a, *p;
703 1.1 itojun char cmd[5], *param;
704 1.1 itojun struct sockaddr_in *sin;
705 1.1 itojun struct sockaddr_in6 *sin6;
706 1.1 itojun enum state nstate;
707 1.1 itojun char ch;
708 1.1 itojun
709 1.1 itojun /* OOB data handling */
710 1.1 itojun error = ioctl(src, SIOCATMARK, &atmark);
711 1.1 itojun if (error != -1 && atmark == 1) {
712 1.1 itojun n = read(src, rbuf, 1);
713 1.1 itojun if (n == -1)
714 1.1 itojun goto bad;
715 1.1 itojun send(dst, rbuf, n, MSG_OOB);
716 1.1 itojun #if 0
717 1.1 itojun n = read(src, rbuf, sizeof(rbuf));
718 1.1 itojun if (n == -1)
719 1.1 itojun goto bad;
720 1.1 itojun write(dst, rbuf, n);
721 1.1 itojun return n;
722 1.1 itojun #endif
723 1.1 itojun }
724 1.1 itojun
725 1.1 itojun n = read(src, rbuf, sizeof(rbuf));
726 1.1 itojun if (n <= 0)
727 1.1 itojun return n;
728 1.1 itojun rbuf[n] = '\0';
729 1.1 itojun
730 1.1 itojun if (n < 4) {
731 1.1 itojun write(dst, rbuf, n);
732 1.1 itojun return n;
733 1.1 itojun }
734 1.1 itojun
735 1.1 itojun /*
736 1.1 itojun * parse argument
737 1.1 itojun */
738 1.1 itojun {
739 1.1 itojun char *p, *q;
740 1.1 itojun int i;
741 1.1 itojun
742 1.1 itojun p = rbuf;
743 1.1 itojun q = cmd;
744 1.1 itojun for (i = 0; i < 4; i++) {
745 1.1 itojun if (!isalpha(*p)) {
746 1.1 itojun /* invalid command */
747 1.1 itojun write(dst, rbuf, n);
748 1.1 itojun return n;
749 1.1 itojun }
750 1.1 itojun *q++ = islower(*p) ? toupper(*p) : *p;
751 1.1 itojun p++;
752 1.1 itojun }
753 1.1 itojun if (!isspace(*p)) {
754 1.1 itojun /* invalid command */
755 1.1 itojun write(dst, rbuf, n);
756 1.1 itojun return n;
757 1.1 itojun }
758 1.1 itojun *q = '\0';
759 1.1 itojun param = p;
760 1.1 itojun /* param points to first non-command token, if any */
761 1.1 itojun while (*param && isspace(*param))
762 1.1 itojun param++;
763 1.1 itojun if (!*param)
764 1.1 itojun param = NULL;
765 1.1 itojun }
766 1.1 itojun
767 1.1 itojun *state = NONE;
768 1.1 itojun
769 1.1 itojun if (strcmp(cmd, "LPRT") == 0 && param) {
770 1.1 itojun /*
771 1.1 itojun * LPRT -> PORT
772 1.1 itojun */
773 1.1 itojun nstate = LPRT;
774 1.1 itojun
775 1.1 itojun close(wport4);
776 1.1 itojun close(wport6);
777 1.1 itojun close(port4);
778 1.1 itojun close(port6);
779 1.1 itojun wport4 = wport6 = port4 = port6 = -1;
780 1.1 itojun
781 1.1 itojun if (epsvall) {
782 1.4 itojun n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
783 1.1 itojun cmd);
784 1.1 itojun write(src, sbuf, n);
785 1.1 itojun return n;
786 1.1 itojun }
787 1.1 itojun
788 1.1 itojun n = sscanf(param,
789 1.1 itojun "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
790 1.1 itojun &af, &hal, &ho[0], &ho[1], &ho[2], &ho[3],
791 1.1 itojun &ho[4], &ho[5], &ho[6], &ho[7],
792 1.1 itojun &ho[8], &ho[9], &ho[10], &ho[11],
793 1.1 itojun &ho[12], &ho[13], &ho[14], &ho[15],
794 1.1 itojun &pal, &po[0], &po[1]);
795 1.1 itojun if (n != 21 || af != 6 || hal != 16|| pal != 2) {
796 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
797 1.1 itojun "501 illegal parameter to LPRT\r\n");
798 1.1 itojun write(src, sbuf, n);
799 1.1 itojun return n;
800 1.1 itojun }
801 1.1 itojun
802 1.1 itojun /* keep LPRT parameter */
803 1.1 itojun memset(&data6, 0, sizeof(data6));
804 1.1 itojun sin6 = (struct sockaddr_in6 *)&data6;
805 1.1 itojun sin6->sin6_len = sizeof(*sin6);
806 1.1 itojun sin6->sin6_family = AF_INET6;
807 1.1 itojun for (n = 0; n < 16; n++)
808 1.2 itojun sin6->sin6_addr.s6_addr[n] = ho[n];
809 1.1 itojun sin6->sin6_port = htons(((po[0] & 0xff) << 8) | (po[1] & 0xff));
810 1.1 itojun
811 1.1 itojun sendport:
812 1.1 itojun /* get ready for active data connection */
813 1.1 itojun n = sizeof(data4);
814 1.1 itojun error = getsockname(dst, (struct sockaddr *)&data4, &n);
815 1.1 itojun if (error == -1) {
816 1.1 itojun lprtfail:
817 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
818 1.1 itojun "500 could not translate to PORT\r\n");
819 1.1 itojun write(src, sbuf, n);
820 1.1 itojun return n;
821 1.1 itojun }
822 1.2 itojun if (((struct sockaddr *)&data4)->sa_family != AF_INET)
823 1.1 itojun goto lprtfail;
824 1.1 itojun sin = (struct sockaddr_in *)&data4;
825 1.1 itojun sin->sin_port = 0;
826 1.1 itojun wport4 = socket(sin->sin_family, SOCK_STREAM, 0);
827 1.1 itojun if (wport4 == -1)
828 1.1 itojun goto lprtfail;
829 1.1 itojun error = bind(wport4, (struct sockaddr *)sin, sin->sin_len);
830 1.1 itojun if (error == -1) {
831 1.1 itojun close(wport4);
832 1.1 itojun wport4 = -1;
833 1.1 itojun goto lprtfail;
834 1.1 itojun }
835 1.1 itojun error = listen(wport4, 1);
836 1.1 itojun if (error == -1) {
837 1.1 itojun close(wport4);
838 1.1 itojun wport4 = -1;
839 1.1 itojun goto lprtfail;
840 1.1 itojun }
841 1.1 itojun
842 1.1 itojun /* transmit PORT */
843 1.1 itojun n = sizeof(data4);
844 1.1 itojun error = getsockname(wport4, (struct sockaddr *)&data4, &n);
845 1.1 itojun if (error == -1) {
846 1.1 itojun close(wport4);
847 1.1 itojun wport4 = -1;
848 1.1 itojun goto lprtfail;
849 1.1 itojun }
850 1.2 itojun if (((struct sockaddr *)&data4)->sa_family != AF_INET) {
851 1.1 itojun close(wport4);
852 1.1 itojun wport4 = -1;
853 1.1 itojun goto lprtfail;
854 1.1 itojun }
855 1.1 itojun sin = (struct sockaddr_in *)&data4;
856 1.1 itojun a = (char *)&sin->sin_addr;
857 1.1 itojun p = (char *)&sin->sin_port;
858 1.4 itojun n = snprintf(sbuf, sizeof(sbuf), "PORT %d,%d,%d,%d,%d,%d\r\n",
859 1.1 itojun UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
860 1.1 itojun UC(p[0]), UC(p[1]));
861 1.1 itojun write(dst, sbuf, n);
862 1.1 itojun *state = nstate;
863 1.1 itojun passivemode = 0;
864 1.1 itojun return n;
865 1.1 itojun } else if (strcmp(cmd, "EPRT") == 0 && param) {
866 1.1 itojun /*
867 1.1 itojun * EPRT -> PORT
868 1.1 itojun */
869 1.1 itojun char *afp, *hostp, *portp;
870 1.1 itojun struct addrinfo hints, *res;
871 1.1 itojun
872 1.1 itojun nstate = EPRT;
873 1.1 itojun
874 1.1 itojun close(wport4);
875 1.1 itojun close(wport6);
876 1.1 itojun close(port4);
877 1.1 itojun close(port6);
878 1.1 itojun wport4 = wport6 = port4 = port6 = -1;
879 1.1 itojun
880 1.1 itojun if (epsvall) {
881 1.4 itojun n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
882 1.1 itojun cmd);
883 1.1 itojun write(src, sbuf, n);
884 1.1 itojun return n;
885 1.1 itojun }
886 1.1 itojun
887 1.1 itojun p = param;
888 1.1 itojun ch = *p++; /* boundary character */
889 1.1 itojun afp = p;
890 1.1 itojun while (*p && *p != ch)
891 1.1 itojun p++;
892 1.1 itojun if (!*p) {
893 1.1 itojun eprtparamfail:
894 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
895 1.1 itojun "501 illegal parameter to EPRT\r\n");
896 1.1 itojun write(src, sbuf, n);
897 1.1 itojun return n;
898 1.1 itojun }
899 1.1 itojun *p++ = '\0';
900 1.1 itojun hostp = p;
901 1.1 itojun while (*p && *p != ch)
902 1.1 itojun p++;
903 1.1 itojun if (!*p)
904 1.1 itojun goto eprtparamfail;
905 1.1 itojun *p++ = '\0';
906 1.1 itojun portp = p;
907 1.1 itojun while (*p && *p != ch)
908 1.1 itojun p++;
909 1.1 itojun if (!*p)
910 1.1 itojun goto eprtparamfail;
911 1.1 itojun *p++ = '\0';
912 1.1 itojun
913 1.1 itojun n = sscanf(afp, "%d", &af);
914 1.1 itojun if (n != 1 || af != 2) {
915 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
916 1.1 itojun "501 unsupported address family to EPRT\r\n");
917 1.1 itojun write(src, sbuf, n);
918 1.1 itojun return n;
919 1.1 itojun }
920 1.1 itojun memset(&hints, 0, sizeof(hints));
921 1.1 itojun hints.ai_family = AF_UNSPEC;
922 1.1 itojun error = getaddrinfo(hostp, portp, &hints, &res);
923 1.1 itojun if (error) {
924 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
925 1.1 itojun "501 EPRT: %s\r\n", gai_strerror(error));
926 1.1 itojun write(src, sbuf, n);
927 1.1 itojun return n;
928 1.1 itojun }
929 1.1 itojun if (res->ai_next) {
930 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
931 1.1 itojun "501 EPRT: %s resolved to multiple addresses\r\n", hostp);
932 1.1 itojun write(src, sbuf, n);
933 1.1 itojun return n;
934 1.1 itojun }
935 1.1 itojun
936 1.1 itojun memcpy(&data6, res->ai_addr, res->ai_addrlen);
937 1.1 itojun
938 1.1 itojun goto sendport;
939 1.1 itojun } else if (strcmp(cmd, "LPSV") == 0 && !param) {
940 1.1 itojun /*
941 1.1 itojun * LPSV -> PASV
942 1.1 itojun */
943 1.1 itojun nstate = LPSV;
944 1.1 itojun
945 1.1 itojun close(wport4);
946 1.1 itojun close(wport6);
947 1.1 itojun close(port4);
948 1.1 itojun close(port6);
949 1.1 itojun wport4 = wport6 = port4 = port6 = -1;
950 1.1 itojun
951 1.1 itojun if (epsvall) {
952 1.4 itojun n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
953 1.1 itojun cmd);
954 1.1 itojun write(src, sbuf, n);
955 1.1 itojun return n;
956 1.1 itojun }
957 1.1 itojun
958 1.1 itojun /* transmit PASV */
959 1.4 itojun n = snprintf(sbuf, sizeof(sbuf), "PASV\r\n");
960 1.1 itojun write(dst, sbuf, n);
961 1.1 itojun *state = LPSV;
962 1.1 itojun passivemode = 0; /* to be set to 1 later */
963 1.1 itojun return n;
964 1.1 itojun } else if (strcmp(cmd, "EPSV") == 0 && !param) {
965 1.1 itojun /*
966 1.1 itojun * EPSV -> PASV
967 1.1 itojun */
968 1.1 itojun close(wport4);
969 1.1 itojun close(wport6);
970 1.1 itojun close(port4);
971 1.1 itojun close(port6);
972 1.1 itojun wport4 = wport6 = port4 = port6 = -1;
973 1.1 itojun
974 1.4 itojun n = snprintf(sbuf, sizeof(sbuf), "PASV\r\n");
975 1.1 itojun write(dst, sbuf, n);
976 1.1 itojun *state = EPSV;
977 1.1 itojun passivemode = 0; /* to be set to 1 later */
978 1.1 itojun return n;
979 1.1 itojun } else if (strcmp(cmd, "EPSV") == 0 && param
980 1.1 itojun && strncasecmp(param, "ALL", 3) == 0 && isspace(param[3])) {
981 1.1 itojun /*
982 1.1 itojun * EPSV ALL
983 1.1 itojun */
984 1.1 itojun epsvall = 1;
985 1.4 itojun n = snprintf(sbuf, sizeof(sbuf), "200 EPSV ALL command successful.\r\n");
986 1.1 itojun write(src, sbuf, n);
987 1.1 itojun return n;
988 1.1 itojun #ifdef FAITH4
989 1.1 itojun } else if (strcmp(cmd, "PORT") == 0 && param) {
990 1.1 itojun /*
991 1.1 itojun * PORT -> EPRT
992 1.1 itojun */
993 1.1 itojun char host[NI_MAXHOST], serv[NI_MAXSERV];
994 1.1 itojun
995 1.1 itojun nstate = PORT;
996 1.1 itojun
997 1.1 itojun close(wport4);
998 1.1 itojun close(wport6);
999 1.1 itojun close(port4);
1000 1.1 itojun close(port6);
1001 1.1 itojun wport4 = wport6 = port4 = port6 = -1;
1002 1.1 itojun
1003 1.1 itojun p = param;
1004 1.1 itojun n = sscanf(p, "%u,%u,%u,%u,%u,%u",
1005 1.1 itojun &ho[0], &ho[1], &ho[2], &ho[3], &po[0], &po[1]);
1006 1.1 itojun if (n != 6) {
1007 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
1008 1.1 itojun "501 illegal parameter to PORT\r\n");
1009 1.1 itojun write(src, sbuf, n);
1010 1.1 itojun return n;
1011 1.1 itojun }
1012 1.1 itojun
1013 1.1 itojun memset(&data6, 0, sizeof(data6));
1014 1.1 itojun sin = (struct sockaddr_in *)&data6;
1015 1.1 itojun sin->sin_len = sizeof(*sin);
1016 1.1 itojun sin->sin_family = AF_INET;
1017 1.1 itojun sin->sin_addr.s_addr = htonl(
1018 1.1 itojun ((ho[0] & 0xff) << 24) | ((ho[1] & 0xff) << 16) |
1019 1.1 itojun ((ho[2] & 0xff) << 8) | (ho[3] & 0xff));
1020 1.1 itojun sin->sin_port = htons(((po[0] & 0xff) << 8) | (po[1] & 0xff));
1021 1.1 itojun
1022 1.1 itojun /* get ready for active data connection */
1023 1.1 itojun n = sizeof(data4);
1024 1.1 itojun error = getsockname(dst, (struct sockaddr *)&data4, &n);
1025 1.1 itojun if (error == -1) {
1026 1.1 itojun portfail:
1027 1.4 itojun n = snprintf(sbuf, sizeof(sbuf),
1028 1.1 itojun "500 could not translate to EPRT\r\n");
1029 1.1 itojun write(src, sbuf, n);
1030 1.1 itojun return n;
1031 1.1 itojun }
1032 1.2 itojun if (((struct sockaddr *)&data4)->sa_family != AF_INET6)
1033 1.1 itojun goto portfail;
1034 1.1 itojun
1035 1.1 itojun ((struct sockaddr_in6 *)&data4)->sin6_port = 0;
1036 1.2 itojun sa = (struct sockaddr *)&data4;
1037 1.2 itojun wport4 = socket(sa->sa_family, SOCK_STREAM, 0);
1038 1.1 itojun if (wport4 == -1)
1039 1.1 itojun goto portfail;
1040 1.2 itojun error = bind(wport4, sa, sa->sa_len);
1041 1.1 itojun if (error == -1) {
1042 1.1 itojun close(wport4);
1043 1.1 itojun wport4 = -1;
1044 1.1 itojun goto portfail;
1045 1.1 itojun }
1046 1.1 itojun error = listen(wport4, 1);
1047 1.1 itojun if (error == -1) {
1048 1.1 itojun close(wport4);
1049 1.1 itojun wport4 = -1;
1050 1.1 itojun goto portfail;
1051 1.1 itojun }
1052 1.1 itojun
1053 1.1 itojun /* transmit EPRT */
1054 1.1 itojun n = sizeof(data4);
1055 1.1 itojun error = getsockname(wport4, (struct sockaddr *)&data4, &n);
1056 1.1 itojun if (error == -1) {
1057 1.1 itojun close(wport4);
1058 1.1 itojun wport4 = -1;
1059 1.1 itojun goto portfail;
1060 1.1 itojun }
1061 1.1 itojun af = 2;
1062 1.2 itojun sa = (struct sockaddr *)&data4;
1063 1.2 itojun if (getnameinfo(sa, sa->sa_len, host, sizeof(host),
1064 1.2 itojun serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV)) {
1065 1.1 itojun close(wport4);
1066 1.1 itojun wport4 = -1;
1067 1.1 itojun goto portfail;
1068 1.1 itojun }
1069 1.4 itojun n = snprintf(sbuf, sizeof(sbuf), "EPRT |%d|%s|%s|\r\n", af, host, serv);
1070 1.1 itojun write(dst, sbuf, n);
1071 1.1 itojun *state = nstate;
1072 1.1 itojun passivemode = 0;
1073 1.1 itojun return n;
1074 1.1 itojun } else if (strcmp(cmd, "PASV") == 0 && !param) {
1075 1.1 itojun /*
1076 1.1 itojun * PASV -> EPSV
1077 1.1 itojun */
1078 1.1 itojun
1079 1.1 itojun nstate = PASV;
1080 1.1 itojun
1081 1.1 itojun close(wport4);
1082 1.1 itojun close(wport6);
1083 1.1 itojun close(port4);
1084 1.1 itojun close(port6);
1085 1.1 itojun wport4 = wport6 = port4 = port6 = -1;
1086 1.1 itojun
1087 1.1 itojun /* transmit EPSV */
1088 1.4 itojun n = snprintf(sbuf, sizeof(sbuf), "EPSV\r\n");
1089 1.1 itojun write(dst, sbuf, n);
1090 1.1 itojun *state = PASV;
1091 1.1 itojun passivemode = 0; /* to be set to 1 later */
1092 1.1 itojun return n;
1093 1.1 itojun #else /* FAITH4 */
1094 1.1 itojun } else if (strcmp(cmd, "PORT") == 0 || strcmp(cmd, "PASV") == 0) {
1095 1.1 itojun /*
1096 1.1 itojun * reject PORT/PASV
1097 1.1 itojun */
1098 1.4 itojun n = snprintf(sbuf, sizeof(sbuf), "502 %s not implemented.\r\n", cmd);
1099 1.1 itojun write(src, sbuf, n);
1100 1.1 itojun return n;
1101 1.1 itojun #endif /* FAITH4 */
1102 1.1 itojun } else if (passivemode
1103 1.1 itojun && (strcmp(cmd, "STOR") == 0
1104 1.1 itojun || strcmp(cmd, "STOU") == 0
1105 1.1 itojun || strcmp(cmd, "RETR") == 0
1106 1.1 itojun || strcmp(cmd, "LIST") == 0
1107 1.1 itojun || strcmp(cmd, "NLST") == 0
1108 1.1 itojun || strcmp(cmd, "APPE") == 0)) {
1109 1.1 itojun /*
1110 1.1 itojun * commands with data transfer. need to care about passive
1111 1.1 itojun * mode data connection.
1112 1.1 itojun */
1113 1.1 itojun
1114 1.1 itojun if (ftp_passiveconn() < 0) {
1115 1.4 itojun n = snprintf(sbuf, sizeof(sbuf), "425 Cannot open data connetion\r\n");
1116 1.1 itojun write(src, sbuf, n);
1117 1.1 itojun } else {
1118 1.1 itojun /* simply relay the command */
1119 1.1 itojun write(dst, rbuf, n);
1120 1.1 itojun }
1121 1.1 itojun
1122 1.1 itojun *state = NONE;
1123 1.1 itojun return n;
1124 1.1 itojun } else {
1125 1.1 itojun /* simply relay it */
1126 1.1 itojun *state = NONE;
1127 1.1 itojun write(dst, rbuf, n);
1128 1.1 itojun return n;
1129 1.1 itojun }
1130 1.1 itojun
1131 1.1 itojun bad:
1132 1.1 itojun exit_failure(ERRSTR);
1133 1.1 itojun /*NOTREACHED*/
1134 1.1 itojun return 0; /* to make gcc happy */
1135 1.1 itojun }
1136