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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
|
/* $KAME: ftp.c,v 1.24 2005/03/16 05:05:48 itojun Exp $ */
/*
* Copyright (C) 1997 and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#include <errno.h>
#include <ctype.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "faithd.h"
static char rbuf[MSS];
static char sbuf[MSS];
static int passivemode = 0;
static int wport4 = -1; /* listen() to active */
static int wport6 = -1; /* listen() to passive */
static int port4 = -1; /* active: inbound passive: outbound */
static int port6 = -1; /* active: outbound passive: inbound */
static struct sockaddr_storage data4; /* server data address */
static struct sockaddr_storage data6; /* client data address */
static int epsvall = 0;
enum state { NONE, LPRT, EPRT, LPSV, EPSV };
static int ftp_activeconn __P((void));
static int ftp_passiveconn __P((void));
static int ftp_copy __P((int, int));
static int ftp_copyresult __P((int, int, enum state));
static int ftp_copycommand __P((int, int, enum state *));
void
ftp_relay(int ctl6, int ctl4)
{
#ifdef HAVE_POLL_H
struct pollfd pfd[6];
#else
fd_set readfds;
#endif
int error;
enum state state = NONE;
struct timeval tv;
syslog(LOG_INFO, "starting ftp control connection");
for (;;) {
#ifdef HAVE_POLL_H
pfd[0].fd = ctl4;
pfd[0].events = POLLIN;
pfd[1].fd = ctl6;
pfd[1].events = POLLIN;
if (0 <= port4) {
pfd[2].fd = port4;
pfd[2].events = POLLIN;
} else
pfd[2].fd = -1;
if (0 <= port6) {
pfd[3].fd = port6;
pfd[3].events = POLLIN;
} else
pfd[3].fd = -1;
#if 0
if (0 <= wport4) {
pfd[4].fd = wport4;
pfd[4].events = POLLIN;
} else
pfd[4].fd = -1;
if (0 <= wport6) {
pfd[5].fd = wport4;
pfd[5].events = POLLIN;
} else
pfd[5].fd = -1;
#else
pfd[4].fd = pfd[5].fd = -1;
pfd[4].events = pfd[5].events = 0;
#endif
#else
int maxfd = 0;
FD_ZERO(&readfds);
if (ctl4 >= FD_SETSIZE)
exit_failure("descriptor too big");
FD_SET(ctl4, &readfds);
maxfd = ctl4;
if (ctl6 >= FD_SETSIZE)
exit_failure("descriptor too big");
FD_SET(ctl6, &readfds);
maxfd = (ctl6 > maxfd) ? ctl6 : maxfd;
if (0 <= port4) {
if (port4 >= FD_SETSIZE)
exit_failure("descriptor too big");
FD_SET(port4, &readfds);
maxfd = (port4 > maxfd) ? port4 : maxfd;
}
if (0 <= port6) {
if (port6 >= FD_SETSIZE)
exit_failure("descriptor too big");
FD_SET(port6, &readfds);
maxfd = (port6 > maxfd) ? port6 : maxfd;
}
#if 0
if (0 <= wport4) {
if (wport4 >= FD_SETSIZE)
exit_failure("descriptor too big");
FD_SET(wport4, &readfds);
maxfd = (wport4 > maxfd) ? wport4 : maxfd;
}
if (0 <= wport6) {
if (wport6 >= FD_SETSIZE)
exit_failure("descriptor too big");
FD_SET(wport6, &readfds);
maxfd = (wport6 > maxfd) ? wport6 : maxfd;
}
#endif
#endif
tv.tv_sec = FAITH_TIMEOUT;
tv.tv_usec = 0;
#ifdef HAVE_POLL_H
error = poll(pfd, sizeof(pfd)/sizeof(pfd[0]), tv.tv_sec * 1000);
#else
error = select(maxfd + 1, &readfds, NULL, NULL, &tv);
#endif
if (error == -1) {
#ifdef HAVE_POLL_H
exit_failure("poll: %s", strerror(errno));
#else
exit_failure("select: %s", strerror(errno));
#endif
}
else if (error == 0)
exit_failure("connection timeout");
/*
* The order of the following checks does (slightly) matter.
* It is important to visit all checks (do not use "continue"),
* otherwise some of the pipe may become full and we cannot
* relay correctly.
*/
#ifdef HAVE_POLL_H
if (pfd[1].revents & POLLIN)
#else
if (FD_ISSET(ctl6, &readfds))
#endif
{
/*
* copy control connection from the client.
* command translation is necessary.
*/
error = ftp_copycommand(ctl6, ctl4, &state);
if (error < 0)
goto bad;
else if (error == 0) {
close(ctl4);
close(ctl6);
exit_success("terminating ftp control connection");
/*NOTREACHED*/
}
}
#ifdef HAVE_POLL_H
if (pfd[0].revents & POLLIN)
#else
if (FD_ISSET(ctl4, &readfds))
#endif
{
/*
* copy control connection from the server
* translation of result code is necessary.
*/
error = ftp_copyresult(ctl4, ctl6, state);
if (error < 0)
goto bad;
else if (error == 0) {
close(ctl4);
close(ctl6);
exit_success("terminating ftp control connection");
/*NOTREACHED*/
}
}
#ifdef HAVE_POLL_H
if (0 <= port4 && 0 <= port6 && (pfd[2].revents & POLLIN))
#else
if (0 <= port4 && 0 <= port6 && FD_ISSET(port4, &readfds))
#endif
{
/*
* copy data connection.
* no special treatment necessary.
*/
#ifdef HAVE_POLL_H
if (pfd[2].revents & POLLIN)
#else
if (FD_ISSET(port4, &readfds))
#endif
error = ftp_copy(port4, port6);
switch (error) {
case -1:
goto bad;
case 0:
close(port4);
close(port6);
port4 = port6 = -1;
syslog(LOG_INFO, "terminating data connection");
break;
default:
break;
}
}
#ifdef HAVE_POLL_H
if (0 <= port4 && 0 <= port6 && (pfd[3].revents & POLLIN))
#else
if (0 <= port4 && 0 <= port6 && FD_ISSET(port6, &readfds))
#endif
{
/*
* copy data connection.
* no special treatment necessary.
*/
#ifdef HAVE_POLL_H
if (pfd[3].revents & POLLIN)
#else
if (FD_ISSET(port6, &readfds))
#endif
error = ftp_copy(port6, port4);
switch (error) {
case -1:
goto bad;
case 0:
close(port4);
close(port6);
port4 = port6 = -1;
syslog(LOG_INFO, "terminating data connection");
break;
default:
break;
}
}
#if 0
#ifdef HAVE_POLL_H
if (wport4 && (pfd[4].revents & POLLIN))
#else
if (wport4 && FD_ISSET(wport4, &readfds))
#endif
{
/*
* establish active data connection from the server.
*/
ftp_activeconn();
}
#ifdef HAVE_POLL_H
if (wport4 && (pfd[5].revents & POLLIN))
#else
if (wport6 && FD_ISSET(wport6, &readfds))
#endif
{
/*
* establish passive data connection from the client.
*/
ftp_passiveconn();
}
#endif
}
bad:
exit_failure("%s", strerror(errno));
}
static int
ftp_activeconn()
{
socklen_t n;
int error;
#ifdef HAVE_POLL_H
struct pollfd pfd[1];
#else
fd_set set;
#endif
struct timeval timeout;
struct sockaddr *sa;
/* get active connection from server */
#ifdef HAVE_POLL_H
pfd[0].fd = wport4;
pfd[0].events = POLLIN;
#else
FD_ZERO(&set);
if (wport4 >= FD_SETSIZE)
exit_failure("descriptor too big");
FD_SET(wport4, &set);
#endif
timeout.tv_sec = 120;
timeout.tv_usec = 0;
n = sizeof(data4);
#ifdef HAVE_POLL_H
if (poll(pfd, sizeof(pfd)/sizeof(pfd[0]), timeout.tv_sec * 1000) == 0 ||
(port4 = accept(wport4, (struct sockaddr *)&data4, &n)) < 0)
#else
if (select(wport4 + 1, &set, NULL, NULL, &timeout) == 0 ||
(port4 = accept(wport4, (struct sockaddr *)&data4, &n)) < 0)
#endif
{
close(wport4);
wport4 = -1;
syslog(LOG_INFO, "active mode data connection failed");
return -1;
}
/* ask active connection to client */
sa = (struct sockaddr *)&data6;
port6 = socket(sa->sa_family, SOCK_STREAM, 0);
if (port6 == -1) {
close(port4);
close(wport4);
port4 = wport4 = -1;
syslog(LOG_INFO, "active mode data connection failed");
return -1;
}
error = connect(port6, sa, sa->sa_len);
if (error < 0) {
close(port6);
close(port4);
close(wport4);
port6 = port4 = wport4 = -1;
syslog(LOG_INFO, "active mode data connection failed");
return -1;
}
syslog(LOG_INFO, "active mode data connection established");
return 0;
}
static int
ftp_passiveconn()
{
socklen_t len;
int error;
#ifdef HAVE_POLL_H
struct pollfd pfd[1];
#else
fd_set set;
#endif
struct timeval timeout;
struct sockaddr *sa;
/* get passive connection from client */
#ifdef HAVE_POLL_H
pfd[0].fd = wport6;
pfd[0].events = POLLIN;
#else
FD_ZERO(&set);
if (wport6 >= FD_SETSIZE)
exit_failure("descriptor too big");
FD_SET(wport6, &set);
#endif
timeout.tv_sec = 120;
timeout.tv_usec = 0;
len = sizeof(data6);
#ifdef HAVE_POLL_H
if (poll(pfd, sizeof(pfd)/sizeof(pfd[0]), timeout.tv_sec * 1000) == 0 ||
(port6 = accept(wport6, (struct sockaddr *)&data6, &len)) < 0)
#else
if (select(wport6 + 1, &set, NULL, NULL, &timeout) == 0 ||
(port6 = accept(wport6, (struct sockaddr *)&data6, &len)) < 0)
#endif
{
close(wport6);
wport6 = -1;
syslog(LOG_INFO, "passive mode data connection failed");
return -1;
}
/* ask passive connection to server */
sa = (struct sockaddr *)&data4;
port4 = socket(sa->sa_family, SOCK_STREAM, 0);
if (port4 == -1) {
close(wport6);
close(port6);
wport6 = port6 = -1;
syslog(LOG_INFO, "passive mode data connection failed");
return -1;
}
error = connect(port4, sa, sa->sa_len);
if (error < 0) {
close(wport6);
close(port4);
close(port6);
wport6 = port4 = port6 = -1;
syslog(LOG_INFO, "passive mode data connection failed");
return -1;
}
syslog(LOG_INFO, "passive mode data connection established");
return 0;
}
static int
ftp_copy(int src, int dst)
{
int error, atmark, n;
/* OOB data handling */
error = ioctl(src, SIOCATMARK, &atmark);
if (error != -1 && atmark == 1) {
n = read(src, rbuf, 1);
if (n == -1)
goto bad;
send(dst, rbuf, n, MSG_OOB);
#if 0
n = read(src, rbuf, sizeof(rbuf));
if (n == -1)
goto bad;
write(dst, rbuf, n);
return n;
#endif
}
n = read(src, rbuf, sizeof(rbuf));
switch (n) {
case -1:
case 0:
return n;
default:
write(dst, rbuf, n);
return n;
}
bad:
exit_failure("%s", strerror(errno));
/*NOTREACHED*/
return 0; /* to make gcc happy */
}
static int
ftp_copyresult(int src, int dst, enum state state)
{
int error, atmark, n;
socklen_t len;
char *param;
int code;
char *a, *p;
int i;
/* OOB data handling */
error = ioctl(src, SIOCATMARK, &atmark);
if (error != -1 && atmark == 1) {
n = read(src, rbuf, 1);
if (n == -1)
goto bad;
send(dst, rbuf, n, MSG_OOB);
#if 0
n = read(src, rbuf, sizeof(rbuf));
if (n == -1)
goto bad;
write(dst, rbuf, n);
return n;
#endif
}
n = read(src, rbuf, sizeof(rbuf));
if (n <= 0)
return n;
rbuf[n] = '\0';
/*
* parse argument
*/
p = rbuf;
for (i = 0; i < 3; i++) {
if (!isdigit(*p)) {
/* invalid reply */
write(dst, rbuf, n);
return n;
}
p++;
}
if (!isspace(*p)) {
/* invalid reply */
write(dst, rbuf, n);
return n;
}
code = atoi(rbuf);
param = p;
/* param points to first non-command token, if any */
while (*param && isspace(*param))
param++;
if (!*param)
param = NULL;
switch (state) {
case NONE:
if (!passivemode && rbuf[0] == '1') {
if (ftp_activeconn() < 0) {
n = snprintf(rbuf, sizeof(rbuf),
"425 Cannot open data connetion\r\n");
if (n < 0 || n >= sizeof(rbuf))
n = 0;
}
}
if (n)
write(dst, rbuf, n);
return n;
case LPRT:
case EPRT:
/* expecting "200 PORT command successful." */
if (code == 200) {
p = strstr(rbuf, "PORT");
if (p) {
p[0] = (state == LPRT) ? 'L' : 'E';
p[1] = 'P';
}
} else {
close(wport4);
wport4 = -1;
}
write(dst, rbuf, n);
return n;
case LPSV:
case EPSV:
/*
* expecting "227 Entering Passive Mode (x,x,x,x,x,x,x)"
* (in some cases result comes without paren)
*/
if (code != 227) {
passivefail0:
close(wport6);
wport6 = -1;
write(dst, rbuf, n);
return n;
}
{
unsigned int ho[4], po[2];
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
u_short port;
/*
* PASV result -> LPSV/EPSV result
*/
p = param;
while (*p && *p != '(' && !isdigit(*p)) /*)*/
p++;
if (!*p)
goto passivefail0; /*XXX*/
if (*p == '(') /*)*/
p++;
n = sscanf(p, "%u,%u,%u,%u,%u,%u",
&ho[0], &ho[1], &ho[2], &ho[3], &po[0], &po[1]);
if (n != 6)
goto passivefail0; /*XXX*/
/* keep PORT parameter */
memset(&data4, 0, sizeof(data4));
sin = (struct sockaddr_in *)&data4;
sin->sin_len = sizeof(*sin);
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = 0;
for (n = 0; n < 4; n++) {
sin->sin_addr.s_addr |=
htonl((ho[n] & 0xff) << ((3 - n) * 8));
}
sin->sin_port = htons(((po[0] & 0xff) << 8) | (po[1] & 0xff));
/* get ready for passive data connection */
memset(&data6, 0, sizeof(data6));
sin6 = (struct sockaddr_in6 *)&data6;
sin6->sin6_len = sizeof(*sin6);
sin6->sin6_family = AF_INET6;
wport6 = socket(sin6->sin6_family, SOCK_STREAM, 0);
if (wport6 == -1) {
passivefail:
n = snprintf(sbuf, sizeof(sbuf),
"500 could not translate from PASV\r\n");
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
return n;
}
#ifdef IPV6_FAITH
{
int on = 1;
error = setsockopt(wport6, IPPROTO_IPV6, IPV6_FAITH,
&on, sizeof(on));
if (error == -1)
exit_failure("setsockopt(IPV6_FAITH): %s", strerror(errno));
}
#endif
error = bind(wport6, (struct sockaddr *)sin6, sin6->sin6_len);
if (error == -1) {
close(wport6);
wport6 = -1;
goto passivefail;
}
error = listen(wport6, 1);
if (error == -1) {
close(wport6);
wport6 = -1;
goto passivefail;
}
/* transmit LPSV or EPSV */
/*
* addr from dst, port from wport6
*/
len = sizeof(data6);
error = getsockname(wport6, (struct sockaddr *)&data6, &len);
if (error == -1) {
close(wport6);
wport6 = -1;
goto passivefail;
}
sin6 = (struct sockaddr_in6 *)&data6;
port = sin6->sin6_port;
len = sizeof(data6);
error = getsockname(dst, (struct sockaddr *)&data6, &len);
if (error == -1) {
close(wport6);
wport6 = -1;
goto passivefail;
}
sin6 = (struct sockaddr_in6 *)&data6;
sin6->sin6_port = port;
if (state == LPSV) {
a = (char *)&sin6->sin6_addr;
p = (char *)&sin6->sin6_port;
n = snprintf(sbuf, sizeof(sbuf),
"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",
6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
2, UC(p[0]), UC(p[1]));
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(dst, sbuf, n);
passivemode = 1;
return n;
} else {
n = snprintf(sbuf, sizeof(sbuf),
"229 Entering Extended Passive Mode (|||%d|)\r\n",
ntohs(sin6->sin6_port));
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(dst, sbuf, n);
passivemode = 1;
return n;
}
}
}
bad:
exit_failure("%s", strerror(errno));
/*NOTREACHED*/
return 0; /* to make gcc happy */
}
static int
ftp_copycommand(int src, int dst, enum state *state)
{
int error, atmark, n;
socklen_t len;
unsigned int af, hal, ho[16], pal, po[2];
char *a, *p, *q;
char cmd[5], *param;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
enum state nstate;
char ch;
int i;
/* OOB data handling */
error = ioctl(src, SIOCATMARK, &atmark);
if (error != -1 && atmark == 1) {
n = read(src, rbuf, 1);
if (n == -1)
goto bad;
send(dst, rbuf, n, MSG_OOB);
#if 0
n = read(src, rbuf, sizeof(rbuf));
if (n == -1)
goto bad;
write(dst, rbuf, n);
return n;
#endif
}
n = read(src, rbuf, sizeof(rbuf));
if (n <= 0)
return n;
rbuf[n] = '\0';
if (n < 4) {
write(dst, rbuf, n);
return n;
}
/*
* parse argument
*/
p = rbuf;
q = cmd;
for (i = 0; i < 4; i++) {
if (!isalpha(*p)) {
/* invalid command */
write(dst, rbuf, n);
return n;
}
*q++ = islower(*p) ? toupper(*p) : *p;
p++;
}
if (!isspace(*p)) {
/* invalid command */
write(dst, rbuf, n);
return n;
}
*q = '\0';
param = p;
/* param points to first non-command token, if any */
while (*param && isspace(*param))
param++;
if (!*param)
param = NULL;
*state = NONE;
if (strcmp(cmd, "LPRT") == 0 && param) {
/*
* LPRT -> PORT
*/
nstate = LPRT;
close(wport4);
close(wport6);
close(port4);
close(port6);
wport4 = wport6 = port4 = port6 = -1;
if (epsvall) {
n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
cmd);
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
return n;
}
n = sscanf(param,
"%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
&af, &hal, &ho[0], &ho[1], &ho[2], &ho[3],
&ho[4], &ho[5], &ho[6], &ho[7],
&ho[8], &ho[9], &ho[10], &ho[11],
&ho[12], &ho[13], &ho[14], &ho[15],
&pal, &po[0], &po[1]);
if (n != 21 || af != 6 || hal != 16|| pal != 2) {
n = snprintf(sbuf, sizeof(sbuf),
"501 illegal parameter to LPRT\r\n");
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
return n;
}
/* keep LPRT parameter */
memset(&data6, 0, sizeof(data6));
sin6 = (struct sockaddr_in6 *)&data6;
sin6->sin6_len = sizeof(*sin6);
sin6->sin6_family = AF_INET6;
for (n = 0; n < 16; n++)
sin6->sin6_addr.s6_addr[n] = ho[n];
sin6->sin6_port = htons(((po[0] & 0xff) << 8) | (po[1] & 0xff));
sendport:
/* get ready for active data connection */
len = sizeof(data4);
error = getsockname(dst, (struct sockaddr *)&data4, &len);
if (error == -1) {
lprtfail:
n = snprintf(sbuf, sizeof(sbuf),
"500 could not translate to PORT\r\n");
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
return n;
}
if (((struct sockaddr *)&data4)->sa_family != AF_INET)
goto lprtfail;
sin = (struct sockaddr_in *)&data4;
sin->sin_port = 0;
wport4 = socket(sin->sin_family, SOCK_STREAM, 0);
if (wport4 == -1)
goto lprtfail;
error = bind(wport4, (struct sockaddr *)sin, sin->sin_len);
if (error == -1) {
close(wport4);
wport4 = -1;
goto lprtfail;
}
error = listen(wport4, 1);
if (error == -1) {
close(wport4);
wport4 = -1;
goto lprtfail;
}
/* transmit PORT */
len = sizeof(data4);
error = getsockname(wport4, (struct sockaddr *)&data4, &len);
if (error == -1) {
close(wport4);
wport4 = -1;
goto lprtfail;
}
if (((struct sockaddr *)&data4)->sa_family != AF_INET) {
close(wport4);
wport4 = -1;
goto lprtfail;
}
sin = (struct sockaddr_in *)&data4;
a = (char *)&sin->sin_addr;
p = (char *)&sin->sin_port;
n = snprintf(sbuf, sizeof(sbuf), "PORT %d,%d,%d,%d,%d,%d\r\n",
UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
UC(p[0]), UC(p[1]));
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(dst, sbuf, n);
*state = nstate;
passivemode = 0;
return n;
} else if (strcmp(cmd, "EPRT") == 0 && param) {
/*
* EPRT -> PORT
*/
char *afp, *hostp, *portp;
struct addrinfo hints, *res;
nstate = EPRT;
close(wport4);
close(wport6);
close(port4);
close(port6);
wport4 = wport6 = port4 = port6 = -1;
if (epsvall) {
n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
cmd);
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
return n;
}
p = param;
ch = *p++; /* boundary character */
afp = p;
while (*p && *p != ch)
p++;
if (!*p) {
eprtparamfail:
n = snprintf(sbuf, sizeof(sbuf),
"501 illegal parameter to EPRT\r\n");
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
return n;
}
*p++ = '\0';
hostp = p;
while (*p && *p != ch)
p++;
if (!*p)
goto eprtparamfail;
*p++ = '\0';
portp = p;
while (*p && *p != ch)
p++;
if (!*p)
goto eprtparamfail;
*p++ = '\0';
n = sscanf(afp, "%d", &af);
if (n != 1 || af != 2) {
n = snprintf(sbuf, sizeof(sbuf),
"501 unsupported address family to EPRT\r\n");
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
return n;
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
error = getaddrinfo(hostp, portp, &hints, &res);
if (error) {
n = snprintf(sbuf, sizeof(sbuf),
"501 EPRT: %s\r\n", gai_strerror(error));
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
return n;
}
if (res->ai_next) {
n = snprintf(sbuf, sizeof(sbuf),
"501 EPRT: %s resolved to multiple addresses\r\n", hostp);
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
freeaddrinfo(res);
return n;
}
freeaddrinfo(res);
memcpy(&data6, res->ai_addr, res->ai_addrlen);
goto sendport;
} else if (strcmp(cmd, "LPSV") == 0 && !param) {
/*
* LPSV -> PASV
*/
nstate = LPSV;
close(wport4);
close(wport6);
close(port4);
close(port6);
wport4 = wport6 = port4 = port6 = -1;
if (epsvall) {
n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
cmd);
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
return n;
}
/* transmit PASV */
n = snprintf(sbuf, sizeof(sbuf), "PASV\r\n");
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(dst, sbuf, n);
*state = LPSV;
passivemode = 0; /* to be set to 1 later */
return n;
} else if (strcmp(cmd, "EPSV") == 0 && !param) {
/*
* EPSV -> PASV
*/
close(wport4);
close(wport6);
close(port4);
close(port6);
wport4 = wport6 = port4 = port6 = -1;
n = snprintf(sbuf, sizeof(sbuf), "PASV\r\n");
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(dst, sbuf, n);
*state = EPSV;
passivemode = 0; /* to be set to 1 later */
return n;
} else if (strcmp(cmd, "EPSV") == 0 && param
&& strncasecmp(param, "ALL", 3) == 0 && isspace(param[3])) {
/*
* EPSV ALL
*/
epsvall = 1;
n = snprintf(sbuf, sizeof(sbuf), "200 EPSV ALL command successful.\r\n");
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
return n;
} else if (strcmp(cmd, "PORT") == 0 || strcmp(cmd, "PASV") == 0) {
/*
* reject PORT/PASV
*/
n = snprintf(sbuf, sizeof(sbuf), "502 %s not implemented.\r\n", cmd);
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
return n;
} else if (passivemode
&& (strcmp(cmd, "STOR") == 0
|| strcmp(cmd, "STOU") == 0
|| strcmp(cmd, "RETR") == 0
|| strcmp(cmd, "LIST") == 0
|| strcmp(cmd, "NLST") == 0
|| strcmp(cmd, "APPE") == 0)) {
/*
* commands with data transfer. need to care about passive
* mode data connection.
*/
if (ftp_passiveconn() < 0) {
n = snprintf(sbuf, sizeof(sbuf), "425 Cannot open data connetion\r\n");
if (n < 0 || n >= sizeof(sbuf))
n = 0;
if (n)
write(src, sbuf, n);
} else {
/* simply relay the command */
write(dst, rbuf, n);
}
*state = NONE;
return n;
} else {
/* simply relay it */
*state = NONE;
write(dst, rbuf, n);
return n;
}
bad:
exit_failure("%s", strerror(errno));
/*NOTREACHED*/
return 0; /* to make gcc happy */
}
|