1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/* socket.c - twoftpd routines for handling sockets
* Copyright (C) 2001 Bruce Guenter <bruceg@em.ca>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/socket.h>
#include "twoftpd.h"
#include "backend.h"
#include <sysdeps.h>
#include <unix/nonblock.h>
/* State variables */
static int socket_fd = -1;
static ipv4addr socket_ip;
static unsigned short socket_port;
static ipv4addr remote_ip;
static unsigned short remote_port;
static ipv4addr client_ip;
static ipv4addr server_ip;
static enum { NONE, PASV, PORT } connect_mode = NONE;
static int respond_timedoutconn(void)
{
return respond(425, 1, "Timed out waiting for connection.");
}
static int respond_connfailed(void)
{
return respond_syserr(425, "Connection failed");
}
static int respond_connaborted(void)
{
return respond(425, 1, "Connection aborted by incoming command.");
}
static int accept_connection(void)
{
int fd;
iopoll_fd pf[2];
pf[0].fd = 0;
pf[0].events = IOPOLL_READ;
pf[0].revents = 0;
pf[1].fd = socket_fd;
pf[1].events = IOPOLL_READ;
switch (iopoll_restart(pf, 2, timeout*1000)) {
case 2: break;
case 1: break;
case 0: respond_timedoutconn(); return -1;
default: respond_connfailed(); return -1;
}
if (pf[0].revents) {
respond_connaborted();
return -1;
}
if ((fd = socket_accept4(socket_fd, &remote_ip, &remote_port)) == -1) {
respond_connfailed();
return -1;
}
close(socket_fd);
socket_fd = -1;
connect_mode = NONE;
if (!nonblock_on(fd)) {
respond_syserr(425, "Could not set flags on socket");
close(fd);
return -1;
}
return fd;
}
static int make_connect_socket(void)
{
int fd;
char buf;
fd = -1;
if (bind_port_fd != -1) {
if (write(bind_port_fd, &buf, 1) == 1 &&
read(bind_port_fd, &buf, 1) == 1 &&
buf == 0)
fd = socket_recvfd(bind_port_fd);
}
if (fd == -1) {
if ((fd = socket_tcp()) == -1) {
respond_syserr(425, "Could not allocate a socket");
return -1;
}
if (!socket_reuse(fd) ||
!socket_bind4(fd, &server_ip, 0)) {
respond_syserr(425, "Could not set flags on socket");
close(fd);
return -1;
}
}
return fd;
}
static int start_connection(void)
{
int fd;
iopoll_fd pf[2];
if ((fd = make_connect_socket()) == -1) return -1;
if (!nonblock_on(fd)) {
respond_syserr(425, "Could not set flags on socket");
close(fd);
return -1;
}
if (socket_connect4(fd, &remote_ip, remote_port)) return fd;
if (errno != EINPROGRESS && errno != EWOULDBLOCK) {
respond_connfailed();
return -1;
}
pf[0].fd = 0;
pf[0].events = IOPOLL_READ;
pf[0].revents = 0;
pf[1].fd = fd;
pf[1].events = IOPOLL_WRITE;
switch (iopoll_restart(pf, 2, timeout*1000)) {
case 0:
respond_timedoutconn();
break;
case 2:
case 1:
if (pf[0].revents) {
respond_connaborted();
break;
}
if (socket_connected(fd)) return fd;
/* No break, fall through */
default:
respond_connfailed();
}
close(fd);
return -1;
}
static int make_connection_fd(void)
{
int fd;
if (connect_mode == NONE) {
fd = -1;
respond(425, 1, "No PORT or PASV commands have been issued.");
}
else {
fd = (connect_mode == PASV) ? accept_connection() : start_connection();
if (fd != -1) respond(150, 1, "Opened data connection.");
}
return fd;
}
int make_in_connection(ibuf* in)
{
int fd;
if ((fd = make_connection_fd()) == -1) return 0;
if (!ibuf_init(in, fd, 0, IOBUF_NEEDSCLOSE, 0)) return 0;
in->io.timeout = timeout * 1000;
return 1;
}
int make_out_connection(obuf* out)
{
int fd;
if ((fd = make_connection_fd()) == -1) return 0;
socket_cork(fd);
if (!socket_linger(fd, 1, timeout)) {
respond_syserr(425, "Could not set flags on socket");
close(fd);
return 0;
}
if (!obuf_init(out, fd, 0, IOBUF_NEEDSCLOSE, 0)) return 0;
out->io.timeout = timeout * 1000;
return 1;
}
int close_out_connection(obuf* out)
{
if (!obuf_flush(out)) {
obuf_close(out);
return 0;
}
socket_uncork(out->io.fd);
return obuf_close(out);
}
static unsigned char strtoc(const char* s, const char** end)
{
long tmp;
tmp = strtol(s, (char**)end, 10);
if (tmp < 0 || tmp > 0xff) *end = s;
return tmp;
}
static int scan_ip(const char* s, char sep,
ipv4addr* addr, const char** endptr)
{
const char* end;
addr->addr[0] = strtoc(s, &end); if (*end != sep) return 0;
addr->addr[1] = strtoc(end+1, &end); if (*end != sep) return 0;
addr->addr[2] = strtoc(end+1, &end); if (*end != sep) return 0;
addr->addr[3] = strtoc(end+1, &end);
*endptr = end;
return 1;
}
int parse_localip(const char* s)
{
const char* end;
return scan_ip(s, '.', &server_ip, &end) && *end == 0;
}
int parse_remoteip(const char* s)
{
const char* end;
return scan_ip(s, '.', &client_ip, &end) && *end == 0;
}
static int parse_addr(const char* s)
{
const char* end;
if (!scan_ip(s, ',', &remote_ip, &end) || *end != ',') return 0;
remote_port = strtoc(end+1, &end) << 8; if (*end != ',') return 0;
remote_port |= strtoc(end+1, &end); if (*end != 0) return 0;
connect_mode = PORT;
return 1;
}
static int make_accept_socket(void)
{
if (socket_fd != -1) close(socket_fd);
if ((socket_fd = socket_tcp()) != -1) {
if (socket_bind4(socket_fd, &server_ip, 0) &&
socket_listen(socket_fd, 1) &&
socket_getaddr4(socket_fd, &socket_ip, &socket_port)) {
connect_mode = PASV;
return 1;
}
else {
close(socket_fd);
socket_fd = -1;
}
}
connect_mode = NONE;
return 0;
}
int handle_pasv(void)
{
char buffer[6*4+25];
if (!make_accept_socket())
return respond_syserr(550, "Could not create socket");
snprintf(buffer, sizeof buffer, "Entering Passive Mode (%u,%u,%u,%u,%u,%u).",
socket_ip.addr[0], socket_ip.addr[1],
socket_ip.addr[2], socket_ip.addr[3],
(socket_port>>8)&0xff, socket_port&0xff);
return respond(227, 1, buffer);
}
int handle_port(void)
{
if (!parse_addr(req_param))
return respond(501, 1, "Can't parse your PORT address.");
if (memcmp(&remote_ip, &client_ip, sizeof client_ip))
return respond(501, 1, "PORT IP does not match client address.");
return respond_ok();
}
|