Repository navigation
Expand file tree
/
Copy pathnet.c
More file actions
431 lines (377 loc) · 9.48 KB
/
Copy pathnet.c
File metadata and controls
431 lines (377 loc) · 9.48 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* net.c Copyright 1992-2000 by Michael Temari All Rights Reserved
*
* This file is part of ftpd.
*
* This file handles:
*
* PASV PORT
*
*
* 01/25/1995 Initial Release Michael Temari, <Michael@TemWare.Com>
* 02/09/2005 Initial Release Michael Temari, <Michael@TemWare.Com>
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include <net/netlib.h>
#include <net/hton.h>
#include <net/gen/in.h>
#include <net/gen/inet.h>
#include <net/gen/tcp.h>
#include <net/gen/tcp_io.h>
#include <net/gen/socket.h>
#include <net/gen/netdb.h>
#include "ftpd.h"
#include "access.h"
#include "net.h"
_PROTOTYPE(static void timeout, (int sig));
static char *msg425 = "425-Could not open data connection.\r\n";
static char *msg501 = "501 Syntax error in parameters.\r\n";
static int gottimeout = 0;
static int lpid = -1;
static int didpassive = 0;
/* they must be behind a firewall or using a web browser */
int doPASV(buff)
char *buff;
{
nwio_tcpconf_t tcpconf;
nwio_tcpcl_t tcplopt;
char *tcp_device;
ipaddr_t ipaddr;
tcpport_t lport;
int s;
time_t starttime;
int retry;
if(ChkLoggedIn())
return(GOOD);
CleanUpPasv();
/* here we set up a connection to listen on */
if((tcp_device = getenv("TCP_DEVICE")) == NULL)
tcp_device = TCP_DEVICE;
if(ftpdata_fd >= 0) {
close(ftpdata_fd);
ftpdata_fd = -1;
}
if((ftpdata_fd = open(tcp_device, O_RDWR)) < 0) {
printf(msg425);
printf("425 Could not open tcp_device. Error %s\r\n", strerror(errno));
return(GOOD);
}
tcpconf.nwtc_flags = NWTC_LP_SEL | NWTC_SET_RA | NWTC_UNSET_RP;
tcpconf.nwtc_remaddr = rmtipaddr;
tcpconf.nwtc_remport = htons(0);
tcpconf.nwtc_locport = htons(0);
s = ioctl(ftpdata_fd, NWIOSTCPCONF, &tcpconf);
if(s < 0) {
printf(msg425);
printf("425 Could not ioctl NWIOSTCPCONF. Error %s\r\n", strerror(errno));
close(ftpdata_fd);
ftpdata_fd = -1;
return(GOOD);
}
s = ioctl(ftpdata_fd, NWIOGTCPCONF, &tcpconf);
if(s < 0) {
printf(msg425);
printf("425 Could not NWIOGTCPCONF. Error %s\r\n", strerror(errno));
close(ftpdata_fd);
ftpdata_fd = -1;
return(GOOD);
}
ipaddr = tcpconf.nwtc_locaddr;
lport = tcpconf.nwtc_locport;
/* Now lets fork a child to do the listening :-( */
tcplopt.nwtcl_flags = 0;
lpid = fork();
if(lpid < 0) {
printf(msg425);
printf("425 Could not fork listener. Error %s\r\n", strerror(errno));
close(ftpdata_fd);
ftpdata_fd = -1;
return(GOOD);
} else if(lpid == 0) {
retry = 0;
while(1) {
#ifdef DEBUG
fprintf(logfile, "ftpd: child %d parent %d listen try %d\n", getpid(), getppid(), retry);
fflush(logfile);
#endif
s = ioctl(ftpdata_fd, NWIOTCPLISTEN, &tcplopt);
if(!(s == -1 && errno == EAGAIN)) break;
if(retry++ > 10) break;
sleep(1);
}
#ifdef DEBUG
fprintf(logfile, "ftpd: child %d s %d errno %d\n", getpid(), s, errno);
fflush(logfile);
#endif
if(s < 0)
exit(errno); /* tells parent listen failed */
else
exit(0); /* tells parent listen okay */
}
#ifdef DEBUG
fprintf(logfile, "ftpd: parent %d wait for %d\n", getpid(), lpid);
fflush(logfile);
#endif
/* wait for child to be listening, no more than serveral seconds */
(void) time(&starttime);
while(1) {
if(time((time_t *)NULL) > (starttime + 15)) break;
signal(SIGALRM, timeout);
alarm(1);
s = ioctl(ftpdata_fd, NWIOGTCPCONF, &tcpconf);
#ifdef DEBUG
fprintf(logfile, "ftpd: parent %d child %d s %d errno %d start %ld now %ld\n",
getpid(), lpid, s, errno, starttime, time((time_t *)NULL));
fflush(logfile);
#endif
alarm(0);
if(s == -1) break;
sleep(1);
}
#define hiword(x) ((u16_t)((x) >> 16))
#define loword(x) ((u16_t)(x & 0xffff))
#define hibyte(x) (((x) >> 8) & 0xff)
#define lobyte(x) ((x) & 0xff)
printf("227 Entering Passive Mode (%u,%u,%u,%u,%u,%u).\r\n",
hibyte(hiword(htonl(ipaddr))), lobyte(hiword(htonl(ipaddr))),
hibyte(loword(htonl(ipaddr))), lobyte(loword(htonl(ipaddr))),
hibyte(htons(lport)), lobyte(htons(lport)));
#ifdef DEBUG
fprintf(logfile, "ftpd: parent %d child %d send 227\n", getpid(), lpid);
fflush(logfile);
#endif
didpassive = -1;
return(GOOD);
}
/* they want us to connect here */
int doPORT(buff)
char *buff;
{
u32_t ipaddr;
u16_t port;
int i;
if(ftpdata_fd >= 0) {
close(ftpdata_fd);
ftpdata_fd = -1;
}
ipaddr = (u32_t)0;
for(i = 0; i < 4; i++) {
ipaddr = (ipaddr << 8) + (u32_t)atoi(buff);
if((buff = strchr(buff, ',')) == (char *)0) {
printf(msg501);
return(GOOD);
}
buff++;
}
port = (u16_t)atoi(buff);
if((buff = strchr(buff, ',')) == (char *)0) {
printf(msg501);
return(0);
}
buff++;
port = (port << 8) + (u16_t)atoi(buff);
dataaddr = htonl(ipaddr);
dataport = htons(port);
if(dataaddr != rmtipaddr) {
printf(msg501);
return(GOOD);
}
printf("200 Port command okay.\r\n");
return(GOOD);
}
/* connect, huh? */
int DataConnect()
{
nwio_tcpconf_t tcpconf;
nwio_tcpcl_t tcpcopt;
nwio_tcpcl_t tcplopt;
char *tcp_device;
int s, cs;
int retry;
if(didpassive && ftpdata_fd >= 0) {
didpassive = 0;
gottimeout = 0;
signal(SIGALRM, timeout);
alarm(10);
while(!gottimeout) {
s = waitpid(lpid, &cs, 0);
if((s == lpid) || (s < 0 && errno == ECHILD)) break;
#ifdef DEBUG
fprintf(logfile, "ftpd: parent %d child %d waitpid s %d cs %04x errno %d\n", getpid(), lpid, s, cs, errno);
fflush(logfile);
#endif
}
alarm(0);
#ifdef DEBUG
fprintf(logfile, "ftpd: parent %d child %d waitpid s %d cs %04x errno %d\n", getpid(), lpid, s, cs, errno);
fflush(logfile);
#endif
if(gottimeout) {
#ifdef DEBUG
fprintf(logfile, "ftpd: parent %d child %d got timeout\n", getpid(), lpid);
fflush(logfile);
#endif
kill(lpid, SIGKILL);
s = waitpid(lpid, &cs, 0);
}
#ifdef DEBUG
fprintf(logfile, "ftpd: parent %d child %d continuing\n", getpid(), lpid);
fflush(logfile);
#endif
lpid = -1;
if(gottimeout) {
printf(msg425);
printf("425 Child listener timeout.\r\n");
close(ftpdata_fd);
ftpdata_fd = -1;
return(BAD);
}
if(s < 0) {
printf(msg425);
printf("425 Child listener vanished.\r\n");
close(ftpdata_fd);
ftpdata_fd = -1;
return(BAD);
}
if((cs & 0x00ff)) {
printf(msg425);
printf("425 Child listener failed %04x\r\n", cs);
close(ftpdata_fd);
ftpdata_fd = -1;
return(BAD);
}
cs = (cs >> 8) & 0x00ff;
if(cs) {
printf(msg425);
printf("425 Child listener error %s\r\n", strerror(cs));
close(ftpdata_fd);
ftpdata_fd = -1;
return(BAD);
}
#ifdef DEBUG
fprintf(logfile, "ftpd: parent %d child %d pasv done\n", getpid(), lpid);
fflush(logfile);
#endif
return(GOOD);
}
if(ftpdata_fd >= 0)
return(GOOD);
if((tcp_device = getenv("TCP_DEVICE")) == NULL)
tcp_device = TCP_DEVICE;
if((ftpdata_fd = open(tcp_device, O_RDWR)) < 0) {
printf(msg425);
printf("425 Could not open tcp_device. Error %s\r\n", strerror(errno));
return(BAD);
}
tcpconf.nwtc_flags = NWTC_LP_SET | NWTC_SET_RA | NWTC_SET_RP;
tcpconf.nwtc_remaddr = dataaddr;
tcpconf.nwtc_remport = dataport;
tcpconf.nwtc_locport = htons(20);
s = ioctl(ftpdata_fd, NWIOSTCPCONF, &tcpconf);
if(s < 0) {
printf(msg425);
printf("425 Could not ioctl NWIOSTCPCONF. Error %s\r\n", strerror(errno));
close(ftpdata_fd);
ftpdata_fd = -1;
return(BAD);
}
s = ioctl(ftpdata_fd, NWIOGTCPCONF, &tcpconf);
if(s < 0) {
printf(msg425);
printf("425 Could not ioctl NWIOGTCPCONF. Error %s\r\n", strerror(errno));
close(ftpdata_fd);
ftpdata_fd = -1;
return(BAD);
}
tcpcopt.nwtcl_flags = 0;
retry = 0;
do {
#ifdef DEBUG
fprintf(logfile, "try connect\n"); fflush(logfile);
fflush(logfile);
#endif
sleep(2);
s = ioctl(ftpdata_fd, NWIOTCPCONN, &tcpcopt);
#ifdef DEBUG
fprintf(logfile, "after connect %d %d\n", s, errno);
fflush(logfile);
#endif
if(!(s == -1 && errno == EAGAIN)) break;
if(retry++ > 10) break;
sleep(1);
} while(1);
if(s < 0) {
printf(msg425);
printf("425 Could not ioctl NWIOTCPCONN. Error %s\r\n", strerror(errno));
close(ftpdata_fd);
ftpdata_fd = -1;
return(BAD);
}
s = ioctl(ftpdata_fd, NWIOGTCPCONF, &tcpconf);
if(s < 0) {
printf(msg425);
printf("425 Could not ioctl NWIOGTCPCONF. Error %s\r\n", strerror(errno));
close(ftpdata_fd);
ftpdata_fd = -1;
return(BAD);
}
return(GOOD);
}
/* Clean up stuff we did to get a Pasv connection going */
int CleanUpPasv()
{
int s, cs;
if(lpid >= 0) {
kill(lpid, SIGKILL);
while(1) {
s = waitpid(lpid, &cs, 0);
if(s == lpid || (s == -1 && errno == ECHILD))
break;
}
}
lpid = -1;
didpassive = 0;
return(GOOD);
}
void GetNetInfo()
{
nwio_tcpconf_t tcpconf;
int s;
struct hostent *hostent;
/* Ask the system what our hostname is. */
if(gethostname(myhostname, sizeof(myhostname)) < 0)
strcpy(myhostname, "unknown");
/* lets get our ip address and the clients ip address */
s = ioctl(0, NWIOGTCPCONF, &tcpconf);
if(s < 0) {
printf("421 FTP service unable to get remote ip address. Closing.\r\n");
fflush(stdout);
exit(1);
}
myipaddr = tcpconf.nwtc_locaddr;
myport = tcpconf.nwtc_locport;
rmtipaddr = tcpconf.nwtc_remaddr;
rmtport = tcpconf.nwtc_remport;
/* Look up the host name of the remote host. */
hostent = gethostbyaddr((char *) &rmtipaddr, sizeof(rmtipaddr), AF_INET);
if(!hostent)
strcpy(rmthostname, inet_ntoa(rmtipaddr));
else {
strncpy(rmthostname, hostent->h_name, sizeof(rmthostname)-1);
rmthostname[sizeof(rmthostname)-1] = '\0';
}
}
static void timeout(sig)
int sig;
{
gottimeout = 1;
}