-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.c
1976 lines (1778 loc) · 57.7 KB
/
commands.c
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
#include "config.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
/* added for FreeBSD compatibility */
#include <limits.h>
#include <signal.h>
#define __USE_GNU
#include <unistd.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_ASM_SOCKET_H
#include <asm/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#include <fcntl.h>
#include <string.h>
#ifdef HAVE_WAIT_H
# include <wait.h>
#else
# ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
# endif
#endif
#include "mystring.h"
#include "login.h"
#include "logging.h"
#include "dirlist.h"
#include "options.h"
#include "main.h"
#include "targzip.h"
#include "cwd.h"
#include "bftpdutmp.h"
#include "md5.h"
#ifdef HAVE_ZLIB_H
#include <zlib.h>
#else
#undef WANT_GZIP
#endif
int state = STATE_CONNECTED;
char user[USERLEN + 1];
struct sockaddr_in sa;
char pasv = 0;
int sock;
int pasvsock;
char *philename = NULL;
unsigned long offset = 0;
short int xfertype = TYPE_BINARY;
int ratio_send = 1, ratio_recv = 1;
/* long unsigned bytes_sent = 0, bytes_recvd = 0; */
double bytes_sent = 0.0, bytes_recvd = 0.0;
int epsvall = 0;
int xfer_bufsize;
void control_printf(char success, char *format, ...)
{
char buffer[MAX_STRING_LENGTH];
va_list val;
va_start(val, format);
vsnprintf(buffer, sizeof(buffer), format, val);
va_end(val);
fprintf(stderr, "%s\r\n", buffer);
replace(buffer, "\r", "", MAX_STRING_LENGTH);
bftpd_statuslog(3, success, "%s", buffer);
}
void new_umask()
{
int um;
unsigned long get_um;
char *foo = config_getoption("UMASK");
if (!foo[0])
um = 022;
else
{
get_um = strtoul(foo, NULL, 8);
if (get_um <= INT_MAX)
um = get_um;
else
{
bftpd_log("Error with umask value. Setting to 022.\n", 0);
um = 022;
}
}
umask(um);
}
void prepare_sock(int sock)
{
int on = 1;
#ifdef TCP_NODELAY
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &on, sizeof(on));
#endif
#ifdef TCP_NOPUSH
setsockopt(sock, IPPROTO_TCP, TCP_NOPUSH, (void *) &on, sizeof(on));
#endif
#ifdef SO_REUSEADDR
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &on, sizeof(on));
#endif
#ifdef SO_REUSEPORT
setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void *) &on, sizeof(on));
#endif
#ifdef SO_SNDBUF
on = 65536;
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *) &on, sizeof(on));
#endif
}
int dataconn()
{
struct sockaddr foo;
struct sockaddr_in local;
socklen_t namelen = sizeof(foo);
int curuid = geteuid();
memset(&foo, 0, sizeof(foo));
memset(&local, 0, sizeof(local));
if (pasv) {
sock = accept(pasvsock, (struct sockaddr *) &foo, (socklen_t *) &namelen);
if (sock == -1) {
control_printf(SL_FAILURE, "425-Unable to accept data connection.\r\n425 %s.",
strerror(errno));
return 1;
}
close(pasvsock);
prepare_sock(sock);
} else {
sock = socket(AF_INET, SOCK_STREAM, 0);
prepare_sock(sock);
local.sin_addr.s_addr = name.sin_addr.s_addr;
local.sin_family = AF_INET;
if (!strcasecmp(config_getoption("DATAPORT20"), "yes")) {
if (seteuid(0) < 0) {
control_printf(SL_FAILURE, "425-Fail to seteuid.\r\n425 %s.",
strerror(errno));
return 1;
}
local.sin_port = htons(20);
}
if (bind(sock, (struct sockaddr *) &local, sizeof(local)) < 0) {
control_printf(SL_FAILURE, "425-Unable to bind data socket.\r\n425 %s.",
strerror(errno));
return 1;
}
if (!strcasecmp(config_getoption("DATAPORT20"), "yes"))
if (seteuid(curuid) < 0) {
control_printf(SL_FAILURE, "425-Fail to seteuid.\r\n425 %s.",
strerror(errno));
return 1;
}
sa.sin_family = AF_INET;
if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
control_printf(SL_FAILURE, "425-Unable to establish data connection.\r\n"
"425 %s.", strerror(errno));
return 1;
}
}
control_printf(SL_SUCCESS, "150 %s data connection established.",
xfertype == TYPE_BINARY ? "BINARY" : "ASCII");
return 0;
}
void init_userinfo()
{
#ifndef NO_GETPWNAM
struct passwd *temp = getpwnam(user);
if (temp) {
userinfo.pw_name = strdup(temp->pw_name);
userinfo.pw_passwd = strdup(temp->pw_passwd);
userinfo.pw_uid = temp->pw_uid;
userinfo.pw_gid = temp->pw_gid;
userinfo.pw_gecos = strdup(temp->pw_gecos);
userinfo.pw_dir = strdup(temp->pw_dir);
userinfo.pw_shell = strdup(temp->pw_shell);
userinfo_set = 1;
}
#endif
}
void command_user(char *username)
{
char *alias;
if (state) {
control_printf(SL_FAILURE, "503 Username already given.");
return;
}
mystrncpy(user, username, sizeof(user) - 1);
userinfo_set = 1; /* Dirty! */
alias = (char *) config_getoption("ALIAS");
userinfo_set = 0;
if (alias[0] != '\0')
mystrncpy(user, alias, sizeof(user) - 1);
init_userinfo();
userinfo_set = 1; /* just in case we missed it */
#ifdef DEBUG
bftpd_log("Trying to log in as %s.\n", user);
#endif
expand_groups();
if (!strcasecmp(config_getoption("ANONYMOUS_USER"), "yes"))
{
state = STATE_USER;
control_printf(SL_SUCCESS, "331 Password please.");
/* bftpd_login(""); */
}
else {
state = STATE_USER;
control_printf(SL_SUCCESS, "331 Password please.");
}
}
void command_pass(char *password)
{
if (state > STATE_USER) {
control_printf(SL_FAILURE, "503 Already logged in.");
return;
}
if (bftpd_login(password)) {
bftpd_log("Login as user '%s' failed.\n", user);
control_printf(SL_FAILURE, "530 Login incorrect.");
// exit(0);
state = STATE_CONNECTED;
return;
}
}
void command_pwd(char *params)
{
char *my_cwd = NULL;
my_cwd = bftpd_cwd_getcwd();
if (my_cwd)
{
control_printf(SL_SUCCESS, "257 \"%s\" is the current working directory.", my_cwd);
free(my_cwd);
}
}
void command_type(char *params)
{
if ((*params == 'A') || (*params == 'a')) {
control_printf(SL_SUCCESS, "200 Transfer type changed to ASCII");
xfertype = TYPE_ASCII;
} else if ((*params == 'I') || (*params == 'i')) {
control_printf(SL_SUCCESS, "200 Transfer type changed to BINARY");
xfertype = TYPE_BINARY;
} else
control_printf(SL_FAILURE, "500 Type '%c' not supported.", *params);
}
void command_port(char *params) {
unsigned long a0, a1, a2, a3, p0, p1, addr;
if (epsvall) {
control_printf(SL_FAILURE, "500 EPSV ALL has been called.");
return;
}
sscanf(params, "%lu,%lu,%lu,%lu,%lu,%lu", &a0, &a1, &a2, &a3, &p0, &p1);
addr = htonl((a0 << 24) + (a1 << 16) + (a2 << 8) + a3);
if((addr != remotename.sin_addr.s_addr) &&( strncasecmp(config_getoption("ALLOW_FXP"), "yes", 3))) {
control_printf(SL_FAILURE, "500 The given address is not yours.");
return;
}
sa.sin_addr.s_addr = addr;
sa.sin_port = htons((p0 << 8) + p1);
if (pasv) {
close(sock);
pasv = 0;
}
control_printf(SL_SUCCESS, "200 PORT %lu.%lu.%lu.%lu:%lu OK",
a0, a1, a2, a3, (p0 << 8) + p1);
}
void command_eprt(char *params) {
char delim;
int af;
char addr[51];
char foo[20];
int port;
if (epsvall) {
control_printf(SL_FAILURE, "500 EPSV ALL has been called.");
return;
}
if (strlen(params) < 5) {
control_printf(SL_FAILURE, "500 Syntax error.");
return;
}
delim = params[0];
sprintf(foo, "%c%%i%c%%50[^%c]%c%%i%c", delim, delim, delim, delim, delim);
if (sscanf(params, foo, &af, addr, &port) < 3) {
control_printf(SL_FAILURE, "500 Syntax error.");
return;
}
if (af != 1) {
control_printf(SL_FAILURE, "522 Protocol unsupported, use (1)");
return;
}
sa.sin_addr.s_addr = inet_addr(addr);
if ((sa.sin_addr.s_addr != remotename.sin_addr.s_addr) && (strncasecmp(config_getoption("ALLOW_FXP"), "yes", 3))) {
control_printf(SL_FAILURE, "500 The given address is not yours.");
return;
}
sa.sin_port = htons(port);
if (pasv) {
close(sock);
pasv = 0;
}
control_printf(SL_FAILURE, "200 EPRT %s:%i OK", addr, port);
}
void command_pasv(char *foo)
{
int a1, a2, a3, a4;
socklen_t namelen;
struct sockaddr_in localsock;
char *my_override_ip;
if (epsvall) {
control_printf(SL_FAILURE, "500 EPSV ALL has been called.");
return;
}
pasvsock = socket(AF_INET, SOCK_STREAM, 0);
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_family = AF_INET;
if (!config_getoption("PASSIVE_PORTS") || !strlen(config_getoption("PASSIVE_PORTS"))) {
/* bind to any port */
sa.sin_port = 0;
if (bind(pasvsock, (struct sockaddr *) &sa, sizeof(sa)) == -1)
{
control_printf(SL_FAILURE, "425-Error: Unable to bind data socket.\r\n425 %s", strerror(errno));
return;
}
}
else {
int i = 0, success = 0, port;
for (;;) {
port = int_from_list(config_getoption("PASSIVE_PORTS"), i++);
if (port < 0)
break;
sa.sin_port = htons(port);
if (bind(pasvsock, (struct sockaddr *) &sa, sizeof(sa)) == 0) {
success = 1;
#ifdef DEBUG
bftpd_log("Passive mode: Successfully bound port %d\n", port);
#endif
break;
}
} /* end of for loop */
if (!success) {
control_printf(SL_FAILURE, "425 Error: Unable to bind data socket.");
return;
}
prepare_sock(pasvsock);
} /* end of else using list of ports */
if (listen(pasvsock, 1)) {
control_printf(SL_FAILURE, "425-Error: Unable to make socket listen.\r\n425 %s",
strerror(errno));
return;
}
namelen = sizeof(localsock);
getsockname(pasvsock, (struct sockaddr *) &localsock, (socklen_t *) &namelen);
/* see if we should over-ride the IP address sent to the client */
my_override_ip = config_getoption("OVERRIDE_IP");
if (my_override_ip[0])
{
sscanf( my_override_ip, "%i.%i.%i.%i",
&a1, &a2, &a3, &a4);
}
else /* noraml, no over-ride */
{
sscanf((char *) inet_ntoa(name.sin_addr), "%i.%i.%i.%i",
&a1, &a2, &a3, &a4);
}
control_printf(SL_SUCCESS, "227 Entering Passive Mode (%i,%i,%i,%i,%i,%i)", a1, a2, a3, a4,
ntohs(localsock.sin_port) >> 8, ntohs(localsock.sin_port) & 0xFF);
pasv = 1;
}
void command_epsv(char *params)
{
struct sockaddr_in localsock;
socklen_t namelen;
int af;
if (params[0]) {
if (!strncasecmp(params, "ALL", 3))
epsvall = 1;
else {
if (sscanf(params, "%i", &af) < 1) {
control_printf(SL_FAILURE, "500 Syntax error.");
return;
} else {
if (af != 1) {
control_printf(SL_FAILURE, "522 Protocol unsupported, use (1)");
return;
}
}
}
}
pasvsock = socket(AF_INET, SOCK_STREAM, 0);
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = 0;
sa.sin_family = AF_INET;
if (bind(pasvsock, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
control_printf(SL_FAILURE, "500-Error: Unable to bind data socket.\r\n425 %s",
strerror(errno));
return;
}
if (listen(pasvsock, 1)) {
control_printf(SL_FAILURE, "500-Error: Unable to make socket listen.\r\n425 %s",
strerror(errno));
return;
}
namelen = sizeof(localsock);
getsockname(pasvsock, (struct sockaddr *) &localsock, (socklen_t *) &namelen);
control_printf(SL_SUCCESS, "229 Entering extended passive mode (|||%i|)",
ntohs(localsock.sin_port));
pasv = 1;
}
char test_abort(char selectbefore, int file, int sock)
{
char str[256];
fd_set rfds;
struct timeval tv;
char *result;
if (selectbefore) {
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(fileno(stdin), &rfds);
if (!select(fileno(stdin) + 1, &rfds, NULL, NULL, &tv))
return 0;
}
result = fgets(str, sizeof(str), stdin);
if ( (result) && (strstr(str, "ABOR")) ) {
control_printf(SL_SUCCESS, "426 Transfer aborted.");
close(file);
close(sock);
control_printf(SL_SUCCESS, "226 Aborted.");
bftpd_log("Client aborted file transmission.\n");
alarm(control_timeout);
return 1;
}
return 0;
}
void command_allo(char *foo)
{
command_noop(foo);
}
/* This function allows the storage of multiple files on the server. */
void command_mput(char *filenames)
{
char filename[MAXCMD]; /* single filename */
int from_index, to_index; /* position in "filenames" and "filename" */
from_index = 0; /* start at begining of filenames */
memset(filename, 0, MAXCMD); /* clear filename */
to_index = 0;
/* go until we find a NULL character */
while ( filenames[from_index] > 0)
{
/* copy filename until we hit a space */
if (filenames[from_index] == ' ')
{
/* got a full filename */
command_stor(filename);
/* clear filename and reset to_index */
to_index = 0;
memset(filename, 0, MAXCMD);
while (filenames[from_index] == ' ')
from_index++; /* goto next position */
}
/* if we haven't hit a space, then copy the letter */
else
{
filename[to_index] = filenames[from_index];
to_index++;
from_index++;
/* if the next character is a NULL, then this is the end of the filename */
if (! filenames[from_index])
{
command_stor(filename); /* get the file */
to_index = 0; /* reset filename index */
memset(filename, 0, MAXCMD); /* clear filename buffer */
from_index++; /* goto next character */
}
}
/* if the buffer is getting too big, then stop */
if (to_index > (MAXCMD - 2) )
{
bftpd_log("Error: Filename in '%s' too long.\n", filenames);
return;
}
} /* end of while */
}
void do_stor(char *filename, int flags)
{
char *buffer;
int fd, i, max;
fd_set rfds;
struct timeval tv;
char *p, *pp;
char *mapped = bftpd_cwd_mappath(filename);
int my_buffer_size; /* total transfer buffer size divided by number of clients */
int num_clients = 1; /* number of clients connected to the server */
int new_num_clients = 1;
int xfer_delay;
int attempt_gzip = FALSE;
unsigned long get_value;
int change_buffer_size = FALSE;
int stdin_fileno;
int write_result;
#ifdef HAVE_ZLIB_H
gzFile my_zip_file = NULL;
#endif
if (pre_write_script)
run_script(pre_write_script, mapped);
#ifdef HAVE_ZLIB_H
if (! strcmp( config_getoption("GZ_UPLOAD"), "yes") )
{
attempt_gzip = TRUE;
strcat(mapped, ".gz");
}
else
attempt_gzip = FALSE;
#endif
/* See if we should delay between data transfers */
get_value = strtoul( config_getoption("XFER_DELAY"), NULL, 0);
if (get_value <= INT_MAX)
xfer_delay = get_value;
else
{
bftpd_log("Error getting xfer_delay in do_stor().\n", 0);
xfer_delay = 0;
}
/* Check to see if the file exists and if we can over-write
it, if it does. -- Jesse */
fd = open(mapped, O_RDONLY);
if (fd >= 0) /* file exists */
{
/* close the file */
close(fd);
/* check if we can over-write it */
if ( !strcasecmp( config_getoption("ALLOWCOMMAND_DELE"), "no") )
{
bftpd_log("Not allowed to over-write '%s'.\n", filename);
control_printf(SL_FAILURE,
"553 Error: Remote file is write protected.");
free(mapped);
close(sock);
return;
}
}
if (! attempt_gzip)
{
fd = open(mapped, flags, 00666);
/*
do this below
if (mapped)
free(mapped);
*/
if (fd == -1) {
bftpd_log("Error: '%s' while trying to store file '%s'.\n",
strerror(errno), filename);
control_printf(SL_FAILURE, "553 Error: %s.", strerror(errno));
close(fd); /* make sure it is not open */
if (post_write_script)
run_script(post_write_script, mapped);
free(mapped);
return;
}
}
#ifdef HAVE_ZLIB_H
if ( attempt_gzip )
{
my_zip_file = gzopen(mapped, "wb+");
if (mapped)
{
free(mapped);
mapped = NULL;
}
if (! my_zip_file)
{
control_printf(SL_FAILURE, "553 Error: An error occured creating compressed file.");
close(sock);
close(fd);
return;
}
}
#endif
bftpd_log("Client is storing file '%s'.\n", filename);
if (dataconn())
{
close(fd);
if (post_write_script)
run_script(post_write_script, mapped);
if (mapped)
free(mapped);
return;
}
/* decide if the transfer buffer size should change. */
if (! strcasecmp( config_getoption("CHANGE_BUFSIZE"), "yes") )
change_buffer_size = TRUE;
/* Figure out how big the transfer buffer should be.
This will be the total size divided by the number of clients connected.
-- Jesse
*/
if (change_buffer_size)
{
num_clients = bftpdutmp_usercount("*");
my_buffer_size = get_buffer_size(num_clients);
}
else
my_buffer_size = xfer_bufsize;
alarm(0);
buffer = malloc(xfer_bufsize);
/* Check to see if we are out of memory. -- Jesse */
if (! buffer)
{
bftpd_log("Unable to create buffer to receive file.\n", 0);
control_printf(SL_FAILURE, "553 Error: An unknown error occured on the server.");
if (fd >= 0)
close(fd);
close(sock);
if (mapped)
free(mapped);
return;
}
lseek(fd, offset, SEEK_SET);
offset = 0;
/* Do not use the whole buffer, because a null byte has to be
* written after the string in ASCII mode. */
stdin_fileno = fileno(stdin);
max = (sock > stdin_fileno ? sock : stdin_fileno) + 1;
for (;;) /* start receiving loop */
{
FD_ZERO(&rfds);
FD_SET(sock, &rfds);
FD_SET( stdin_fileno, &rfds);
tv.tv_sec = data_timeout;
tv.tv_usec = 0;
if (!select(max, &rfds, NULL, NULL, &tv)) {
close(sock);
close(fd);
control_printf(SL_FAILURE, "426 Kicked due to data transmission timeout.");
bftpd_log("Kicked due to data transmission timeout.\n");
/* Before we exit, let's remove our entry in the log file. -- Jesse */
if (post_write_script)
run_script(post_write_script, mapped);
bftpdutmp_end();
// Update_Send_Recv(user, bytes_sent, bytes_recvd);
exit(0);
}
if (FD_ISSET(stdin_fileno, &rfds)) {
test_abort(0, fd, sock);
if (buffer)
free(buffer);
close(fd);
if (post_write_script)
run_script(post_write_script, mapped);
free(mapped);
return;
}
if (!((i = recv(sock, buffer, my_buffer_size - 1, 0))))
break;
bytes_recvd += i;
if (xfertype == TYPE_ASCII) {
buffer[i] = '\0';
/* on ASCII stransfer, strip character 13 */
p = pp = buffer;
while (*p) {
if ((unsigned char) *p == 13)
p++;
else
*pp++ = *p++;
}
*pp++ = 0;
i = strlen(buffer);
} // end of if ASCII type transfer
#ifdef HAVE_ZLIB_H
if (my_zip_file)
gzwrite( my_zip_file, buffer, i );
#endif
if(! attempt_gzip)
{
write_result = write(fd, buffer, i);
if (write_result == -1)
break;
}
/* Check to see if our bandwidth usage should change. -- Jesse */
if (change_buffer_size)
{
new_num_clients = bftpdutmp_usercount("*");
if (new_num_clients != num_clients)
{
num_clients = new_num_clients;
my_buffer_size = get_buffer_size(num_clients);
}
}
/* check for transfer delay */
if ( xfer_delay )
{
struct timeval wait_time;
wait_time.tv_sec = 0;
wait_time.tv_usec = xfer_delay;
select( 0, NULL, NULL, NULL, &wait_time);
}
} // end of for loop, reading
free(buffer);
#ifdef HAVE_ZLIB_H
gzclose(my_zip_file);
#else
close(fd);
#endif
close(sock);
alarm(control_timeout);
offset = 0;
control_printf(SL_SUCCESS, "226 File transmission successful.");
bftpd_log("File transmission successful.\n");
if (post_write_script)
run_script(post_write_script, mapped);
if (mapped)
free(mapped);
// Update_Send_Recv(user, bytes_sent, bytes_recvd);
}
void command_stor(char *filename)
{
do_stor(filename, O_CREAT | O_WRONLY | O_TRUNC);
}
void command_appe(char *filename)
{
do_stor(filename, O_CREAT | O_WRONLY | O_APPEND);
}
/* Send multpile files to the client. */
void command_mget(char *filenames)
{
char filename[MAXCMD]; /* single filename */
int from_index, to_index; /* position in "filenames" and "filename" */
from_index = 0; /* start at begining of filenames */
memset(filename, 0, MAXCMD); /* clear filename */
to_index = 0;
/* go until we find a NULL character */
while ( filenames[from_index] > 0)
{
/* copy filename until we hit a space */
if (filenames[from_index] == ' ')
{
/* got a full filename */
command_retr(filename);
/* clear filename and reset to_index */
to_index = 0;
memset(filename, 0, MAXCMD);
while (filenames[from_index] == ' ')
from_index++; /* goto next position */
}
/* if we haven't hit a space, then copy the letter */
else
{
filename[to_index] = filenames[from_index];
to_index++;
from_index++;
/* if the next character is a NULL, then this is the end of the filename */
if (! filenames[from_index])
{
command_retr(filename); /* send the file */
to_index = 0; /* reset filename index */
memset(filename, 0, MAXCMD); /* clear filename buffer */
from_index++; /* goto next character */
}
}
/* if the buffer is getting too big, then stop */
if (to_index > (MAXCMD - 2) )
{
bftpd_log("Error: Filename in '%s' too long.\n", filenames);
return;
}
} /* end of while */
}
void command_retr(char *filename)
{
int num_clients = 1;
int new_num_clients = 1; /* number of connectiosn to the server */
int my_buffer_size; /* size of the transfer buffer to use */
char *mapped = NULL;
char *buffer;
int xfer_delay;
struct timeval wait_time;
unsigned long get_value;
ssize_t send_status;
int change_buffer_size = FALSE;
#if (defined(WANT_GZIP) || defined(HAVE_ZLIB_H))
gzFile gzfile;
#endif
int phile;
int i, whattodo = DO_NORMAL;
struct stat statbuf;
#if (defined(WANT_TAR) && defined(WANT_GZIP))
int filedes[2];
#endif
#if (defined(WANT_TAR) || defined(WANT_GZIP))
char *foo;
#endif
#ifdef WANT_TAR
char *argv[4];
#endif
get_value = strtoul( config_getoption("XFER_DELAY"), NULL, 0);
if (get_value <= INT_MAX)
xfer_delay = get_value;
else
{
bftpd_log("Error getting XFER_DELAY in command_retr().\n", 0);
xfer_delay = 0;
}
mapped = bftpd_cwd_mappath(filename);
if (! mapped)
{
bftpd_log("Memory error in sending file.\n", 0);
control_printf(SL_FAILURE, "553 An unknown error occured on the server.", 9);
return;
}
if (! strcasecmp( config_getoption("CHANGE_BUFSIZE"), "yes") )
change_buffer_size = TRUE;
phile = open(mapped, O_RDONLY);
if (phile == -1) { // failed to open a file
#if (defined(WANT_TAR) && defined(WANT_GZIP))
if ((foo = strstr(filename, ".tar.gz")))
if (strlen(foo) == 7) {
whattodo = DO_TARGZ;
*foo = '\0';
}
#endif
#ifdef WANT_TAR
if ((foo = strstr(filename, ".tar")))
if (strlen(foo) == 4) {
whattodo = DO_TARONLY;
*foo = '\0';
}
#endif
#ifdef WANT_GZIP
if ((foo = strstr(filename, ".gz")))
if (strlen(foo) == 3) {
whattodo = DO_GZONLY;
*foo = '\0';
}
#endif
if (whattodo == DO_NORMAL) {
bftpd_log("Error: '%s' while trying to receive file '%s'.\n",
strerror(errno), filename);
control_printf(SL_FAILURE, "553 Error: %s.", strerror(errno));
if (mapped)
free(mapped);
return;
}
}
#ifdef HAVE_ZLIB_H
else // we did open a file
{
char *my_temp;
char *zip_option;
my_temp = strstr(filename, ".gz");
zip_option = config_getoption("GZ_DOWNLOAD");
if (my_temp)
{
if ( ( strlen(my_temp) == 3) && (! strcasecmp(zip_option, "yes") ) )
whattodo = DO_GZUNZIP;
}
}
#endif
stat(mapped, (struct stat *) &statbuf);
if (S_ISDIR(statbuf.st_mode)) {
control_printf(SL_FAILURE, "550 Error: Is a directory.");
if (mapped)
free(mapped);
return;
}
if ((((statbuf.st_size - offset) * ratio_send) / ratio_recv > bytes_recvd
- bytes_sent) && (strcmp((char *) config_getoption("RATIO"), "none"))) {
bftpd_log("Error: 'File too big (ratio)' while trying to receive file "
"'%s'.\n", filename);
control_printf(SL_FAILURE, "553 File too big. Send at least %lf bytes first.",
(double) (((statbuf.st_size - offset) * ratio_send) / ratio_recv)
- bytes_recvd);
if (mapped)
free(mapped);
return;
}
bftpd_log("Client is receiving file '%s'.\n", filename);
switch (whattodo) {
#if (defined(WANT_TAR) && defined(WANT_GZIP))
case DO_TARGZ:
close(phile);
if (dataconn()) {
if (mapped)
free(mapped);
return;
}
alarm(0);
pipe(filedes);
if (fork()) {
buffer = malloc(xfer_bufsize);
/* check to make sure alloc worked */
if (! buffer)
{
if (mapped)
free(mapped);
bftpd_log("Memory error in sending file.\n", 0);