-
Notifications
You must be signed in to change notification settings - Fork 50
/
postlogin.c
1971 lines (1921 loc) · 56.7 KB
/
postlogin.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
/*
* Part of Very Secure FTPd
* Licence: GPL v2
* Author: Chris Evans
* postlogin.c
*/
#include "postlogin.h"
#include "session.h"
#include "oneprocess.h"
#include "twoprocess.h"
#include "ftpcodes.h"
#include "ftpcmdio.h"
#include "ftpdataio.h"
#include "utility.h"
#include "tunables.h"
#include "defs.h"
#include "str.h"
#include "sysstr.h"
#include "banner.h"
#include "sysutil.h"
#include "logging.h"
#include "sysdeputil.h"
#include "ipaddrparse.h"
#include "access.h"
#include "features.h"
#include "ssl.h"
#include "vsftpver.h"
#include "opts.h"
/* Private local functions */
static void handle_pwd(struct vsf_session* p_sess);
static void handle_cwd(struct vsf_session* p_sess);
static void handle_pasv(struct vsf_session* p_sess, int is_epsv);
static void handle_retr(struct vsf_session* p_sess, int is_http);
static void handle_cdup(struct vsf_session* p_sess);
static void handle_list(struct vsf_session* p_sess);
static void handle_type(struct vsf_session* p_sess);
static void handle_port(struct vsf_session* p_sess);
static void handle_stor(struct vsf_session* p_sess);
static void handle_mkd(struct vsf_session* p_sess);
static void handle_rmd(struct vsf_session* p_sess);
static void handle_dele(struct vsf_session* p_sess);
static void handle_rest(struct vsf_session* p_sess);
static void handle_rnfr(struct vsf_session* p_sess);
static void handle_rnto(struct vsf_session* p_sess);
static void handle_nlst(struct vsf_session* p_sess);
static void handle_size(struct vsf_session* p_sess);
static void handle_site(struct vsf_session* p_sess);
static void handle_appe(struct vsf_session* p_sess);
static void handle_mdtm(struct vsf_session* p_sess);
static void handle_site_chmod(struct vsf_session* p_sess,
struct mystr* p_arg_str);
static void handle_site_umask(struct vsf_session* p_sess,
struct mystr* p_arg_str);
static void handle_eprt(struct vsf_session* p_sess);
static void handle_help(struct vsf_session* p_sess);
static void handle_stou(struct vsf_session* p_sess);
static void handle_stat(struct vsf_session* p_sess);
static void handle_stat_file(struct vsf_session* p_sess);
static void handle_logged_in_user(struct vsf_session* p_sess);
static void handle_logged_in_pass(struct vsf_session* p_sess);
static void handle_http(struct vsf_session* p_sess);
static int pasv_active(struct vsf_session* p_sess);
static int port_active(struct vsf_session* p_sess);
static void pasv_cleanup(struct vsf_session* p_sess);
static void port_cleanup(struct vsf_session* p_sess);
static void handle_dir_common(struct vsf_session* p_sess, int full_details,
int stat_cmd);
static void prepend_path_to_filename(struct mystr* p_str);
static int get_remote_transfer_fd(struct vsf_session* p_sess,
const char* p_status_msg);
static void check_abor(struct vsf_session* p_sess);
static void handle_sigurg(void* p_private);
static void handle_upload_common(struct vsf_session* p_sess, int is_append,
int is_unique);
static void get_unique_filename(struct mystr* p_outstr,
const struct mystr* p_base);
static int data_transfer_checks_ok(struct vsf_session* p_sess);
static void resolve_tilde(struct mystr* p_str, struct vsf_session* p_sess);
void
process_post_login(struct vsf_session* p_sess)
{
str_getcwd(&p_sess->home_str);
if (p_sess->is_anonymous)
{
vsf_sysutil_set_umask(tunable_anon_umask);
p_sess->bw_rate_max = tunable_anon_max_rate;
}
else
{
vsf_sysutil_set_umask(tunable_local_umask);
p_sess->bw_rate_max = tunable_local_max_rate;
}
if (p_sess->is_http)
{
handle_http(p_sess);
bug("should not be reached");
}
if (tunable_async_abor_enable)
{
vsf_sysutil_install_sighandler(kVSFSysUtilSigURG, handle_sigurg, p_sess, 0);
vsf_sysutil_activate_sigurg(VSFTP_COMMAND_FD);
}
/* Handle any login message */
vsf_banner_dir_changed(p_sess, FTP_LOGINOK);
vsf_cmdio_write(p_sess, FTP_LOGINOK, "Login successful.");
while(1)
{
int cmd_ok = 1;
if (tunable_setproctitle_enable)
{
vsf_sysutil_setproctitle("IDLE");
}
/* Blocks */
vsf_cmdio_get_cmd_and_arg(p_sess, &p_sess->ftp_cmd_str,
&p_sess->ftp_arg_str, 1);
if (tunable_setproctitle_enable)
{
struct mystr proctitle_str = INIT_MYSTR;
str_copy(&proctitle_str, &p_sess->ftp_cmd_str);
if (!str_isempty(&p_sess->ftp_arg_str))
{
str_append_char(&proctitle_str, ' ');
str_append_str(&proctitle_str, &p_sess->ftp_arg_str);
}
/* Suggestion from Solar */
str_replace_unprintable(&proctitle_str, '?');
vsf_sysutil_setproctitle_str(&proctitle_str);
str_free(&proctitle_str);
}
/* Test command against the allowed lists.. */
if (tunable_cmds_allowed)
{
static struct mystr s_src_str;
static struct mystr s_rhs_str;
str_alloc_text(&s_src_str, tunable_cmds_allowed);
while (1)
{
str_split_char(&s_src_str, &s_rhs_str, ',');
if (str_isempty(&s_src_str))
{
cmd_ok = 0;
break;
}
else if (str_equal(&s_src_str, &p_sess->ftp_cmd_str))
{
break;
}
str_copy(&s_src_str, &s_rhs_str);
}
}
if (tunable_cmds_denied)
{
static struct mystr s_src_str;
static struct mystr s_rhs_str;
str_alloc_text(&s_src_str, tunable_cmds_denied);
while (1)
{
str_split_char(&s_src_str, &s_rhs_str, ',');
if (str_isempty(&s_src_str))
{
break;
}
else if (str_equal(&s_src_str, &p_sess->ftp_cmd_str))
{
cmd_ok = 0;
break;
}
str_copy(&s_src_str, &s_rhs_str);
}
}
if (!cmd_ok)
{
vsf_cmdio_write(p_sess, FTP_NOPERM, "Permission denied.");
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "QUIT"))
{
vsf_cmdio_write_exit(p_sess, FTP_GOODBYE, "Goodbye.");
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "PWD") ||
str_equal_text(&p_sess->ftp_cmd_str, "XPWD"))
{
handle_pwd(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "CWD") ||
str_equal_text(&p_sess->ftp_cmd_str, "XCWD"))
{
handle_cwd(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "CDUP") ||
str_equal_text(&p_sess->ftp_cmd_str, "XCUP"))
{
handle_cdup(p_sess);
}
else if (tunable_pasv_enable &&
!p_sess->epsv_all &&
(str_equal_text(&p_sess->ftp_cmd_str, "PASV") ||
str_equal_text(&p_sess->ftp_cmd_str, "P@SW")))
{
handle_pasv(p_sess, 0);
}
else if (tunable_pasv_enable &&
str_equal_text(&p_sess->ftp_cmd_str, "EPSV"))
{
handle_pasv(p_sess, 1);
}
else if (tunable_download_enable &&
str_equal_text(&p_sess->ftp_cmd_str, "RETR"))
{
handle_retr(p_sess, 0);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "NOOP"))
{
vsf_cmdio_write(p_sess, FTP_NOOPOK, "NOOP ok.");
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "SYST"))
{
vsf_cmdio_write(p_sess, FTP_SYSTOK, "UNIX Type: L8");
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "HELP"))
{
handle_help(p_sess);
}
else if (tunable_dirlist_enable &&
str_equal_text(&p_sess->ftp_cmd_str, "LIST"))
{
handle_list(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "TYPE"))
{
handle_type(p_sess);
}
else if (tunable_port_enable &&
!p_sess->epsv_all &&
str_equal_text(&p_sess->ftp_cmd_str, "PORT"))
{
handle_port(p_sess);
}
else if (tunable_write_enable &&
(tunable_anon_upload_enable || !p_sess->is_anonymous) &&
str_equal_text(&p_sess->ftp_cmd_str, "STOR"))
{
handle_stor(p_sess);
}
else if (tunable_write_enable &&
(tunable_anon_mkdir_write_enable || !p_sess->is_anonymous) &&
(str_equal_text(&p_sess->ftp_cmd_str, "MKD") ||
str_equal_text(&p_sess->ftp_cmd_str, "XMKD")))
{
handle_mkd(p_sess);
}
else if (tunable_write_enable &&
(tunable_anon_other_write_enable || !p_sess->is_anonymous) &&
(str_equal_text(&p_sess->ftp_cmd_str, "RMD") ||
str_equal_text(&p_sess->ftp_cmd_str, "XRMD")))
{
handle_rmd(p_sess);
}
else if (tunable_write_enable &&
(tunable_anon_other_write_enable || !p_sess->is_anonymous) &&
str_equal_text(&p_sess->ftp_cmd_str, "DELE"))
{
handle_dele(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "REST"))
{
handle_rest(p_sess);
}
else if (tunable_write_enable &&
(tunable_anon_other_write_enable || !p_sess->is_anonymous) &&
str_equal_text(&p_sess->ftp_cmd_str, "RNFR"))
{
handle_rnfr(p_sess);
}
else if (tunable_write_enable &&
(tunable_anon_other_write_enable || !p_sess->is_anonymous) &&
str_equal_text(&p_sess->ftp_cmd_str, "RNTO"))
{
handle_rnto(p_sess);
}
else if (tunable_dirlist_enable &&
str_equal_text(&p_sess->ftp_cmd_str, "NLST"))
{
handle_nlst(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "SIZE"))
{
handle_size(p_sess);
}
else if (!p_sess->is_anonymous &&
str_equal_text(&p_sess->ftp_cmd_str, "SITE"))
{
handle_site(p_sess);
}
/* Note - the weird ABOR string is checking for an async ABOR arriving
* without a SIGURG condition.
*/
else if (str_equal_text(&p_sess->ftp_cmd_str, "ABOR") ||
str_equal_text(&p_sess->ftp_cmd_str, "\377\364\377\362ABOR"))
{
vsf_cmdio_write(p_sess, FTP_ABOR_NOCONN, "No transfer to ABOR.");
}
else if (tunable_write_enable &&
(tunable_anon_other_write_enable || !p_sess->is_anonymous) &&
str_equal_text(&p_sess->ftp_cmd_str, "APPE"))
{
handle_appe(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "MDTM"))
{
handle_mdtm(p_sess);
}
else if (tunable_port_enable &&
str_equal_text(&p_sess->ftp_cmd_str, "EPRT"))
{
handle_eprt(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "STRU"))
{
str_upper(&p_sess->ftp_arg_str);
if (str_equal_text(&p_sess->ftp_arg_str, "F"))
{
vsf_cmdio_write(p_sess, FTP_STRUOK, "Structure set to F.");
}
else
{
vsf_cmdio_write(p_sess, FTP_BADSTRU, "Bad STRU command.");
}
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "MODE"))
{
str_upper(&p_sess->ftp_arg_str);
if (str_equal_text(&p_sess->ftp_arg_str, "S"))
{
vsf_cmdio_write(p_sess, FTP_MODEOK, "Mode set to S.");
}
else
{
vsf_cmdio_write(p_sess, FTP_BADMODE, "Bad MODE command.");
}
}
else if (tunable_write_enable &&
(tunable_anon_upload_enable || !p_sess->is_anonymous) &&
str_equal_text(&p_sess->ftp_cmd_str, "STOU"))
{
handle_stou(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "ALLO"))
{
vsf_cmdio_write(p_sess, FTP_ALLOOK, "ALLO command ignored.");
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "REIN"))
{
vsf_cmdio_write(p_sess, FTP_COMMANDNOTIMPL, "REIN not implemented.");
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "ACCT"))
{
vsf_cmdio_write(p_sess, FTP_COMMANDNOTIMPL, "ACCT not implemented.");
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "SMNT"))
{
vsf_cmdio_write(p_sess, FTP_COMMANDNOTIMPL, "SMNT not implemented.");
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "FEAT"))
{
handle_feat(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "OPTS"))
{
handle_opts(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "STAT") &&
str_isempty(&p_sess->ftp_arg_str))
{
handle_stat(p_sess);
}
else if (tunable_dirlist_enable &&
str_equal_text(&p_sess->ftp_cmd_str, "STAT"))
{
handle_stat_file(p_sess);
}
else if (tunable_ssl_enable && str_equal_text(&p_sess->ftp_cmd_str, "PBSZ"))
{
handle_pbsz(p_sess);
}
else if (tunable_ssl_enable && str_equal_text(&p_sess->ftp_cmd_str, "PROT"))
{
handle_prot(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "USER"))
{
handle_logged_in_user(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "PASS"))
{
handle_logged_in_pass(p_sess);
}
else if (str_equal_text(&p_sess->ftp_cmd_str, "PASV") ||
str_equal_text(&p_sess->ftp_cmd_str, "PORT") ||
str_equal_text(&p_sess->ftp_cmd_str, "STOR") ||
str_equal_text(&p_sess->ftp_cmd_str, "MKD") ||
str_equal_text(&p_sess->ftp_cmd_str, "XMKD") ||
str_equal_text(&p_sess->ftp_cmd_str, "RMD") ||
str_equal_text(&p_sess->ftp_cmd_str, "XRMD") ||
str_equal_text(&p_sess->ftp_cmd_str, "DELE") ||
str_equal_text(&p_sess->ftp_cmd_str, "RNFR") ||
str_equal_text(&p_sess->ftp_cmd_str, "RNTO") ||
str_equal_text(&p_sess->ftp_cmd_str, "SITE") ||
str_equal_text(&p_sess->ftp_cmd_str, "APPE") ||
str_equal_text(&p_sess->ftp_cmd_str, "EPSV") ||
str_equal_text(&p_sess->ftp_cmd_str, "EPRT") ||
str_equal_text(&p_sess->ftp_cmd_str, "RETR") ||
str_equal_text(&p_sess->ftp_cmd_str, "LIST") ||
str_equal_text(&p_sess->ftp_cmd_str, "NLST") ||
str_equal_text(&p_sess->ftp_cmd_str, "STOU") ||
str_equal_text(&p_sess->ftp_cmd_str, "ALLO") ||
str_equal_text(&p_sess->ftp_cmd_str, "REIN") ||
str_equal_text(&p_sess->ftp_cmd_str, "ACCT") ||
str_equal_text(&p_sess->ftp_cmd_str, "SMNT") ||
str_equal_text(&p_sess->ftp_cmd_str, "FEAT") ||
str_equal_text(&p_sess->ftp_cmd_str, "OPTS") ||
str_equal_text(&p_sess->ftp_cmd_str, "STAT") ||
str_equal_text(&p_sess->ftp_cmd_str, "PBSZ") ||
str_equal_text(&p_sess->ftp_cmd_str, "PROT"))
{
vsf_cmdio_write(p_sess, FTP_NOPERM, "Permission denied.");
}
else if (str_isempty(&p_sess->ftp_cmd_str) &&
str_isempty(&p_sess->ftp_arg_str))
{
/* Deliberately ignore to avoid NAT device bugs. ProFTPd does the same. */
}
else
{
vsf_cmdio_write(p_sess, FTP_BADCMD, "Unknown command.");
}
if (vsf_log_entry_pending(p_sess))
{
vsf_log_do_log(p_sess, 0);
}
}
}
static void
handle_pwd(struct vsf_session* p_sess)
{
static struct mystr s_cwd_buf_mangle_str;
static struct mystr s_pwd_res_str;
str_getcwd(&s_cwd_buf_mangle_str);
/* Double up any double-quotes in the pathname! */
str_replace_text(&s_cwd_buf_mangle_str, "\"", "\"\"");
/* Enclose pathname in quotes */
str_alloc_text(&s_pwd_res_str, "\"");
str_append_str(&s_pwd_res_str, &s_cwd_buf_mangle_str);
str_append_text(&s_pwd_res_str, "\"");
vsf_cmdio_write_str(p_sess, FTP_PWDOK, &s_pwd_res_str);
}
static void
handle_cwd(struct vsf_session* p_sess)
{
int retval;
resolve_tilde(&p_sess->ftp_arg_str, p_sess);
if (!vsf_access_check_file(&p_sess->ftp_arg_str))
{
vsf_cmdio_write(p_sess, FTP_NOPERM, "Permission denied.");
return;
}
retval = str_chdir(&p_sess->ftp_arg_str);
if (retval == 0)
{
/* Handle any messages */
vsf_banner_dir_changed(p_sess, FTP_CWDOK);
vsf_cmdio_write(p_sess, FTP_CWDOK, "Directory successfully changed.");
}
else
{
vsf_cmdio_write(p_sess, FTP_FILEFAIL, "Failed to change directory.");
}
}
static void
handle_cdup(struct vsf_session* p_sess)
{
str_alloc_text(&p_sess->ftp_arg_str, "..");
handle_cwd(p_sess);
}
static int
port_active(struct vsf_session* p_sess)
{
int ret = 0;
if (p_sess->p_port_sockaddr != 0)
{
ret = 1;
if (pasv_active(p_sess))
{
bug("port and pasv both active");
}
}
return ret;
}
static int
pasv_active(struct vsf_session* p_sess)
{
int ret = 0;
if (tunable_one_process_model)
{
ret = vsf_one_process_pasv_active(p_sess);
}
else
{
ret = vsf_two_process_pasv_active(p_sess);
}
if (ret)
{
if (port_active(p_sess))
{
bug("pasv and port both active");
}
}
return ret;
}
static void
port_cleanup(struct vsf_session* p_sess)
{
vsf_sysutil_sockaddr_clear(&p_sess->p_port_sockaddr);
}
static void
pasv_cleanup(struct vsf_session* p_sess)
{
if (tunable_one_process_model)
{
vsf_one_process_pasv_cleanup(p_sess);
}
else
{
vsf_two_process_pasv_cleanup(p_sess);
}
}
static void
handle_pasv(struct vsf_session* p_sess, int is_epsv)
{
unsigned short the_port;
static struct mystr s_pasv_res_str;
static struct vsf_sysutil_sockaddr* s_p_sockaddr;
int is_ipv6 = vsf_sysutil_sockaddr_is_ipv6(p_sess->p_local_addr);
if (is_epsv && !str_isempty(&p_sess->ftp_arg_str))
{
int argval;
str_upper(&p_sess->ftp_arg_str);
if (str_equal_text(&p_sess->ftp_arg_str, "ALL"))
{
p_sess->epsv_all = 1;
vsf_cmdio_write(p_sess, FTP_EPSVALLOK, "EPSV ALL ok.");
return;
}
argval = vsf_sysutil_atoi(str_getbuf(&p_sess->ftp_arg_str));
if (argval < 1 || argval > 2 || (!is_ipv6 && argval == 2))
{
vsf_cmdio_write(p_sess, FTP_EPSVBAD, "Bad network protocol.");
return;
}
}
pasv_cleanup(p_sess);
port_cleanup(p_sess);
if (tunable_one_process_model)
{
the_port = vsf_one_process_listen(p_sess);
}
else
{
the_port = vsf_two_process_listen(p_sess);
}
if (is_epsv)
{
str_alloc_text(&s_pasv_res_str, "Entering Extended Passive Mode (|||");
str_append_ulong(&s_pasv_res_str, (unsigned long) the_port);
str_append_text(&s_pasv_res_str, "|).");
vsf_cmdio_write_str(p_sess, FTP_EPSVOK, &s_pasv_res_str);
return;
}
if (tunable_pasv_address != 0)
{
vsf_sysutil_sockaddr_alloc_ipv4(&s_p_sockaddr);
/* Report passive address as specified in configuration */
if (vsf_sysutil_inet_aton(tunable_pasv_address, s_p_sockaddr) == 0)
{
die("invalid pasv_address");
}
}
else
{
vsf_sysutil_sockaddr_clone(&s_p_sockaddr, p_sess->p_local_addr);
}
str_alloc_text(&s_pasv_res_str, "Entering Passive Mode (");
if (!is_ipv6)
{
str_append_text(&s_pasv_res_str, vsf_sysutil_inet_ntop(s_p_sockaddr));
}
else
{
const void* p_v4addr = vsf_sysutil_sockaddr_ipv6_v4(s_p_sockaddr);
if (p_v4addr)
{
str_append_text(&s_pasv_res_str, vsf_sysutil_inet_ntoa(p_v4addr));
}
else
{
str_append_text(&s_pasv_res_str, "0,0,0,0");
}
}
str_replace_char(&s_pasv_res_str, '.', ',');
str_append_text(&s_pasv_res_str, ",");
str_append_ulong(&s_pasv_res_str, the_port >> 8);
str_append_text(&s_pasv_res_str, ",");
str_append_ulong(&s_pasv_res_str, the_port & 255);
str_append_text(&s_pasv_res_str, ").");
vsf_cmdio_write_str(p_sess, FTP_PASVOK, &s_pasv_res_str);
}
static void
handle_retr(struct vsf_session* p_sess, int is_http)
{
static struct mystr s_mark_str;
static struct vsf_sysutil_statbuf* s_p_statbuf;
struct vsf_transfer_ret trans_ret;
int remote_fd;
int opened_file;
int is_ascii = 0;
filesize_t offset = p_sess->restart_pos;
p_sess->restart_pos = 0;
if (!is_http && !data_transfer_checks_ok(p_sess))
{
return;
}
if (p_sess->is_ascii && offset != 0)
{
vsf_cmdio_write(p_sess, FTP_FILEFAIL,
"No support for resume of ASCII transfer.");
return;
}
resolve_tilde(&p_sess->ftp_arg_str, p_sess);
vsf_log_start_entry(p_sess, kVSFLogEntryDownload);
str_copy(&p_sess->log_str, &p_sess->ftp_arg_str);
prepend_path_to_filename(&p_sess->log_str);
if (!vsf_access_check_file(&p_sess->ftp_arg_str))
{
vsf_cmdio_write(p_sess, FTP_NOPERM, "Permission denied.");
return;
}
opened_file = str_open(&p_sess->ftp_arg_str, kVSFSysStrOpenReadOnly);
if (vsf_sysutil_retval_is_error(opened_file))
{
vsf_cmdio_write(p_sess, FTP_FILEFAIL, "Failed to open file.");
return;
}
/* Lock file if required */
if (tunable_lock_upload_files)
{
vsf_sysutil_lock_file_read(opened_file);
}
vsf_sysutil_fstat(opened_file, &s_p_statbuf);
/* No games please */
if (!vsf_sysutil_statbuf_is_regfile(s_p_statbuf))
{
/* Note - pretend open failed */
vsf_cmdio_write(p_sess, FTP_FILEFAIL, "Failed to open file.");
/* Irritating FireFox does RETR on directories, so avoid logging this
* very common and noisy case.
*/
if (vsf_sysutil_statbuf_is_dir(s_p_statbuf))
{
vsf_log_clear_entry(p_sess);
}
goto file_close_out;
}
/* Now deactive O_NONBLOCK, otherwise we have a problem on DMAPI filesystems
* such as XFS DMAPI.
*/
vsf_sysutil_deactivate_noblock(opened_file);
/* Optionally, we'll be paranoid and only serve publicly readable stuff */
if (p_sess->is_anonymous && tunable_anon_world_readable_only &&
!vsf_sysutil_statbuf_is_readable_other(s_p_statbuf))
{
vsf_cmdio_write(p_sess, FTP_FILEFAIL, "Failed to open file.");
goto file_close_out;
}
/* Set the download offset (from REST) if any */
if (offset != 0)
{
vsf_sysutil_lseek_to(opened_file, offset);
}
str_alloc_text(&s_mark_str, "Opening ");
if (tunable_ascii_download_enable && p_sess->is_ascii)
{
str_append_text(&s_mark_str, "ASCII");
is_ascii = 1;
}
else
{
str_append_text(&s_mark_str, "BINARY");
}
str_append_text(&s_mark_str, " mode data connection for ");
str_append_str(&s_mark_str, &p_sess->ftp_arg_str);
str_append_text(&s_mark_str, " (");
str_append_filesize_t(&s_mark_str,
vsf_sysutil_statbuf_get_size(s_p_statbuf));
str_append_text(&s_mark_str, " bytes).");
if (is_http)
{
remote_fd = VSFTP_COMMAND_FD;
}
else
{
remote_fd = get_remote_transfer_fd(p_sess, str_getbuf(&s_mark_str));
if (vsf_sysutil_retval_is_error(remote_fd))
{
goto port_pasv_cleanup_out;
}
}
trans_ret = vsf_ftpdataio_transfer_file(p_sess, remote_fd,
opened_file, 0, is_ascii);
if (!is_http &&
vsf_ftpdataio_dispose_transfer_fd(p_sess) != 1 &&
trans_ret.retval == 0)
{
trans_ret.retval = -2;
}
p_sess->transfer_size = trans_ret.transferred;
/* Log _after_ the blocking dispose call, so we get transfer times right */
if (trans_ret.retval == 0)
{
vsf_log_do_log(p_sess, 1);
}
if (is_http)
{
goto file_close_out;
}
/* Emit status message _after_ blocking dispose call to avoid buggy FTP
* clients truncating the transfer.
*/
if (trans_ret.retval == -1)
{
vsf_cmdio_write(p_sess, FTP_BADSENDFILE, "Failure reading local file.");
}
else if (trans_ret.retval == -2)
{
vsf_cmdio_write(p_sess, FTP_BADSENDNET, "Failure writing network stream.");
}
else
{
vsf_cmdio_write(p_sess, FTP_TRANSFEROK, "Transfer complete.");
}
check_abor(p_sess);
port_pasv_cleanup_out:
port_cleanup(p_sess);
pasv_cleanup(p_sess);
file_close_out:
vsf_sysutil_close(opened_file);
}
static void
handle_list(struct vsf_session* p_sess)
{
handle_dir_common(p_sess, 1, 0);
}
static void
handle_dir_common(struct vsf_session* p_sess, int full_details, int stat_cmd)
{
static struct mystr s_option_str;
static struct mystr s_filter_str;
static struct mystr s_dir_name_str;
static struct vsf_sysutil_statbuf* s_p_dirstat;
int dir_allow_read = 1;
struct vsf_sysutil_dir* p_dir = 0;
int retval = 0;
int use_control = 0;
str_empty(&s_option_str);
str_empty(&s_filter_str);
/* By default open the current directory */
str_alloc_text(&s_dir_name_str, ".");
if (!stat_cmd && !data_transfer_checks_ok(p_sess))
{
return;
}
/* Do we have an option? Going to be strict here - the option must come
* first. e.g. "ls -a .." fine, "ls .. -a" not fine
*/
if (!str_isempty(&p_sess->ftp_arg_str) &&
str_get_char_at(&p_sess->ftp_arg_str, 0) == '-')
{
/* Chop off the '-' */
str_mid_to_end(&p_sess->ftp_arg_str, &s_option_str, 1);
/* A space will separate options from filter (if any) */
str_split_char(&s_option_str, &s_filter_str, ' ');
}
else
{
/* The argument, if any, is just a filter */
str_copy(&s_filter_str, &p_sess->ftp_arg_str);
}
if (!str_isempty(&s_filter_str))
{
resolve_tilde(&s_filter_str, p_sess);
if (!vsf_access_check_file(&s_filter_str))
{
vsf_cmdio_write(p_sess, FTP_NOPERM, "Permission denied.");
return;
}
/* First check - is it an outright directory, as in "ls /pub" */
p_dir = str_opendir(&s_filter_str);
if (p_dir != 0)
{
/* Listing a directory! */
str_copy(&s_dir_name_str, &s_filter_str);
str_free(&s_filter_str);
}
else
{
struct str_locate_result locate_result =
str_locate_char(&s_filter_str, '/');
if (locate_result.found)
{
/* Includes a path! Reverse scan for / in the arg, to get the
* base directory and filter (if any)
*/
str_copy(&s_dir_name_str, &s_filter_str);
str_split_char_reverse(&s_dir_name_str, &s_filter_str, '/');
/* If we have e.g. "ls /.message", we just ripped off the leading
* slash because it is the only one!
*/
if (str_isempty(&s_dir_name_str))
{
str_alloc_text(&s_dir_name_str, "/");
}
}
}
}
if (p_dir == 0)
{
/* NOTE - failure check done below, it's not forgotten */
p_dir = str_opendir(&s_dir_name_str);
}
/* Fine, do it */
if (stat_cmd)
{
use_control = 1;
str_append_char(&s_option_str, 'a');
vsf_cmdio_write_hyphen(p_sess, FTP_STATFILE_OK, "Status follows:");
}
else
{
int remote_fd = get_remote_transfer_fd(
p_sess, "Here comes the directory listing.");
if (vsf_sysutil_retval_is_error(remote_fd))
{
goto dir_close_out;
}
}
if (p_sess->is_anonymous && p_dir && tunable_anon_world_readable_only)
{
vsf_sysutil_dir_stat(p_dir, &s_p_dirstat);
if (!vsf_sysutil_statbuf_is_readable_other(s_p_dirstat))
{
dir_allow_read = 0;
}
}
if (p_dir != 0 && dir_allow_read)
{
retval = vsf_ftpdataio_transfer_dir(p_sess, use_control, p_dir,
&s_dir_name_str, &s_option_str,
&s_filter_str, full_details);
}
if (!stat_cmd)
{
if (vsf_ftpdataio_dispose_transfer_fd(p_sess) != 1 && retval == 0)
{
retval = -1;
}
}
if (stat_cmd)
{
vsf_cmdio_write(p_sess, FTP_STATFILE_OK, "End of status");
}
else if (p_dir == 0 || !dir_allow_read)
{
vsf_cmdio_write(p_sess, FTP_TRANSFEROK,
"Transfer done (but failed to open directory).");
}
else if (retval == 0)
{
vsf_cmdio_write(p_sess, FTP_TRANSFEROK, "Directory send OK.");
}
else
{
vsf_cmdio_write(p_sess, FTP_BADSENDNET, "Failure writing network stream.");
}
check_abor(p_sess);
dir_close_out:
if (p_dir)
{
vsf_sysutil_closedir(p_dir);
}
if (!stat_cmd)
{
port_cleanup(p_sess);
pasv_cleanup(p_sess);
}
}
static void
handle_type(struct vsf_session* p_sess)
{
str_upper(&p_sess->ftp_arg_str);
if (str_equal_text(&p_sess->ftp_arg_str, "I") ||
str_equal_text(&p_sess->ftp_arg_str, "L8") ||
str_equal_text(&p_sess->ftp_arg_str, "L 8"))
{
p_sess->is_ascii = 0;
vsf_cmdio_write(p_sess, FTP_TYPEOK, "Switching to Binary mode.");
}
else if (str_equal_text(&p_sess->ftp_arg_str, "A") ||
str_equal_text(&p_sess->ftp_arg_str, "A N"))
{
p_sess->is_ascii = 1;
vsf_cmdio_write(p_sess, FTP_TYPEOK, "Switching to ASCII mode.");
}
else
{
vsf_cmdio_write(p_sess, FTP_BADCMD, "Unrecognised TYPE command.");
}
}
static void
handle_port(struct vsf_session* p_sess)
{
unsigned short the_port;
unsigned char vals[6];
const unsigned char* p_raw;
pasv_cleanup(p_sess);
port_cleanup(p_sess);
p_raw = vsf_sysutil_parse_uchar_string_sep(&p_sess->ftp_arg_str, ',', vals,
sizeof(vals));
if (p_raw == 0)
{
vsf_cmdio_write(p_sess, FTP_BADCMD, "Illegal PORT command.");
return;
}
the_port = vals[4] << 8;
the_port |= vals[5];
vsf_sysutil_sockaddr_clone(&p_sess->p_port_sockaddr, p_sess->p_local_addr);
vsf_sysutil_sockaddr_set_ipv4addr(p_sess->p_port_sockaddr, vals);
vsf_sysutil_sockaddr_set_port(p_sess->p_port_sockaddr, the_port);
/* SECURITY:
* 1) Reject requests not connecting to the control socket IP
* 2) Reject connects to privileged ports
*/
if (!tunable_port_promiscuous)
{
if (!vsf_sysutil_sockaddr_addr_equal(p_sess->p_remote_addr,
p_sess->p_port_sockaddr) ||
vsf_sysutil_is_port_reserved(the_port))
{
vsf_cmdio_write(p_sess, FTP_BADCMD, "Illegal PORT command.");
port_cleanup(p_sess);
return;
}
}
vsf_cmdio_write(p_sess, FTP_PORTOK,
"PORT command successful. Consider using PASV.");
}
static void
handle_stor(struct vsf_session* p_sess)
{
handle_upload_common(p_sess, 0, 0);
}
static void
handle_upload_common(struct vsf_session* p_sess, int is_append, int is_unique)
{
static struct vsf_sysutil_statbuf* s_p_statbuf;
static struct mystr s_filename;
struct mystr* p_filename;
struct vsf_transfer_ret trans_ret;
int new_file_fd;
int remote_fd;
int success = 0;
int created = 0;