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