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