File: socket.c

package info (click to toggle)
twoftpd 1.14-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,556 kB
  • ctags: 901
  • sloc: ansic: 6,685; makefile: 321; sh: 175
file content (233 lines) | stat: -rw-r--r-- 5,884 bytes parent folder | download
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
/* 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 "socket/socket.h"
#include "iopoll.h"
#include "twoftpd.h"
#include "backend.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 accept_connection(void)
{
  int fd;
  iopoll_fd pf;
  
  pf.fd = socket_fd;
  pf.events = IOPOLL_READ;
  switch (iopoll(&pf, 1, timeout*1000)) {
  case 1: break;
  case 0: respond_timedoutconn(); return -1;
  default: respond_connfailed(); 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 start_connection(void)
{
  int fd;
  iopoll_fd p;
  
  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) ||
      !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;
  }
    
  p.fd = fd;
  p.events = IOPOLL_WRITE;
  switch (iopoll(&p, 1, timeout*1000)) {
  case 0:
    respond_timedoutconn();
    break;
  case 1:
    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;
}

static unsigned char strtoc(const char* str, const char** end)
{
  long tmp;
  tmp = strtol(str, (char**)end, 10);
  if (tmp < 0 || tmp > 0xff) *end = str;
  return tmp;
}

static int scan_ip(const char* str, char sep,
		   ipv4addr addr, const char** endptr)
{
  const char* end;
  addr[0] = strtoc(str, &end); if (*end != sep) return 0;
  addr[1] = strtoc(end+1, &end); if (*end != sep) return 0;
  addr[2] = strtoc(end+1, &end); if (*end != sep) return 0;
  addr[3] = strtoc(end+1, &end);
  *endptr = end;
  return 1;
}

int parse_localip(const char* str)
{
  const char* end;
  return scan_ip(str, '.', server_ip, &end) && *end == 0;
}

int parse_remoteip(const char* str)
{
  const char* end;
  return scan_ip(str, '.', client_ip, &end) && *end == 0;
}
  
static int parse_addr(const char* str)
{
  const char* end;
  if (!scan_ip(str, ',', 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_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_socket()) return respond_syserr(550, "Could not create socket");
  snprintf(buffer, sizeof buffer, "Entering Passive Mode (%u,%u,%u,%u,%u,%u).",
	   socket_ip[0], socket_ip[1], socket_ip[2], socket_ip[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();
}