-
Notifications
You must be signed in to change notification settings - Fork 190
/
mod_xfer.c
4552 lines (3610 loc) · 129 KB
/
mod_xfer.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
/*
* ProFTPD - FTP server daemon
* Copyright (c) 1997, 1998 Public Flood Software
* Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net>
* Copyright (c) 2001-2023 The ProFTPD Project team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
* and other respective copyright holders give permission to link this program
* with OpenSSL, and distribute the resulting executable, without including
* the source code for OpenSSL in the source distribution.
*/
/* Data transfer module for ProFTPD */
#include "conf.h"
#include "privs.h"
#include "error.h"
#ifdef HAVE_SYS_SENDFILE_H
# include <sys/sendfile.h>
#endif
/* Minimum priority a process can have. */
#ifndef PRIO_MIN
# define PRIO_MIN -20
#endif
/* Maximum priority a process can have. */
#ifndef PRIO_MAX
# define PRIO_MAX 20
#endif
extern module auth_module;
extern pid_t mpid;
/* Variables for this module */
static pr_fh_t *retr_fh = NULL;
static pr_fh_t *stor_fh = NULL;
static pr_fh_t *displayfilexfer_fh = NULL;
static unsigned char have_rfc2228_data = FALSE;
static unsigned char have_type = FALSE;
static unsigned char have_zmode = FALSE;
static unsigned char use_sendfile = TRUE;
static off_t use_sendfile_len = 0;
static float use_sendfile_pct = -1.0;
static int xfer_check_limit(cmd_rec *);
/* TransferOptions */
#define PR_XFER_OPT_HANDLE_ALLO 0x0001
#define PR_XFER_OPT_IGNORE_ASCII 0x0002
#define PR_XFER_OPT_ALLOW_SYMLINK_UPLOAD 0x0004
static unsigned long xfer_opts = PR_XFER_OPT_HANDLE_ALLO;
static void xfer_exit_ev(const void *, void *);
static void xfer_sigusr2_ev(const void *, void *);
static void xfer_timeout_session_ev(const void *, void *);
static void xfer_timeout_stalled_ev(const void *, void *);
static int xfer_sess_init(void);
/* Used for MaxTransfersPerHost and TransferRate */
static int xfer_parse_cmdlist(const char *, config_rec *, char *);
module xfer_module;
static int xfer_logged_sendfile_decline_msg = FALSE;
static const char *trace_channel = "xfer";
static off_t find_max_nbytes(char *directive) {
config_rec *c = NULL;
unsigned int ctxt_precedence = 0;
unsigned char have_user_limit, have_group_limit, have_class_limit,
have_all_limit;
off_t max_nbytes = 0UL;
have_user_limit = have_group_limit = have_class_limit =
have_all_limit = FALSE;
c = find_config(CURRENT_CONF, CONF_PARAM, directive, FALSE);
while (c != NULL) {
pr_signals_handle();
/* This check is for more than three arguments: one argument is the
* classifier (i.e. "user", "group", or "class"), one argument is
* the precedence, one is the number of bytes; the remaining arguments
* are the individual items in the configured expression.
*/
if (c->argc > 3) {
if (strcasecmp(c->argv[2], "user") == 0) {
if (pr_expr_eval_user_or((char **) &c->argv[3]) == TRUE) {
if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
/* Set the context precedence */
ctxt_precedence = *((unsigned int *) c->argv[1]);
max_nbytes = *((off_t *) c->argv[0]);
have_group_limit = have_class_limit = have_all_limit = FALSE;
have_user_limit = TRUE;
}
}
} else if (strcasecmp(c->argv[2], "group") == 0) {
if (pr_expr_eval_group_or((char **) &c->argv[3]) == TRUE) {
if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
/* Set the context precedence */
ctxt_precedence = *((unsigned int *) c->argv[1]);
max_nbytes = *((off_t *) c->argv[0]);
have_user_limit = have_class_limit = have_all_limit = FALSE;
have_group_limit = TRUE;
}
}
} else if (strcasecmp(c->argv[2], "class") == 0) {
if (pr_expr_eval_class_or((char **) &c->argv[3]) == TRUE) {
if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
/* Set the context precedence */
ctxt_precedence = *((unsigned int *) c->argv[1]);
max_nbytes = *((off_t *) c->argv[0]);
have_user_limit = have_group_limit = have_all_limit = FALSE;
have_class_limit = TRUE;
}
}
}
} else {
if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
/* Set the context precedence. */
ctxt_precedence = *((unsigned int *) c->argv[1]);
max_nbytes = *((off_t *) c->argv[0]);
have_user_limit = have_group_limit = have_class_limit = FALSE;
have_all_limit = TRUE;
}
}
c = find_config_next(c, c->next, CONF_PARAM, directive, FALSE);
}
/* Print out some nice debugging information. */
if (max_nbytes > 0 &&
(have_user_limit ||
have_group_limit ||
have_class_limit ||
have_all_limit)) {
pr_log_debug(DEBUG5, "%s (%" PR_LU " bytes) in effect for %s",
directive, (pr_off_t) max_nbytes,
have_user_limit ? "user " : have_group_limit ? "group " :
have_class_limit ? "class " : "all");
}
return max_nbytes;
}
static void _log_transfer(char direction, char abort_flag) {
struct timeval end_time;
char *fullpath = NULL;
memset(&end_time, '\0', sizeof(end_time));
if (session.xfer.start_time.tv_sec != 0) {
gettimeofday(&end_time, NULL);
end_time.tv_sec -= session.xfer.start_time.tv_sec;
if (end_time.tv_usec >= session.xfer.start_time.tv_usec) {
end_time.tv_usec -= session.xfer.start_time.tv_usec;
} else {
end_time.tv_usec = 1000000L - (session.xfer.start_time.tv_usec -
end_time.tv_usec);
end_time.tv_sec--;
}
}
fullpath = dir_abs_path(session.xfer.p, session.xfer.path, TRUE);
if ((session.sf_flags & SF_ANON) != 0) {
xferlog_write(end_time.tv_sec, pr_netaddr_get_sess_remote_name(),
session.xfer.total_bytes, fullpath,
(session.sf_flags & SF_ASCII ? 'a' : 'b'), direction,
'a', session.anon_user, abort_flag, "_");
} else {
xferlog_write(end_time.tv_sec, pr_netaddr_get_sess_remote_name(),
session.xfer.total_bytes, fullpath,
(session.sf_flags & SF_ASCII ? 'a' : 'b'), direction,
'r', session.user, abort_flag, "_");
}
pr_log_debug(DEBUG1, "Transfer %s %" PR_LU " bytes in %ld.%02lu seconds",
abort_flag == 'c' ? "completed:" : "aborted after",
(pr_off_t) session.xfer.total_bytes, (long) end_time.tv_sec,
(unsigned long)(end_time.tv_usec / 10000));
}
/* Code borrowed from src/dirtree.c's get_word() -- modified to separate
* words on commas as well as spaces.
*/
static char *get_cmd_from_list(char **list) {
char *res = NULL, *dst = NULL;
unsigned char quote_mode = FALSE;
while (**list && PR_ISSPACE(**list)) {
(*list)++;
}
if (!**list) {
return NULL;
}
res = dst = *list;
if (**list == '\"') {
quote_mode = TRUE;
(*list)++;
}
while (**list && **list != ',' &&
(quote_mode ? (**list != '\"') : (!PR_ISSPACE(**list)))) {
if (**list == '\\' && quote_mode) {
/* escaped char */
if (*((*list) + 1)) {
*dst = *(++(*list));
}
}
*dst++ = **list;
++(*list);
}
if (**list) {
(*list)++;
}
*dst = '\0';
return res;
}
static int xfer_check_limit(cmd_rec *cmd) {
config_rec *c = NULL;
const char *client_addr = pr_netaddr_get_ipstr(session.c->remote_addr);
char server_addr[128];
memset(server_addr, '\0', sizeof(server_addr));
pr_snprintf(server_addr, sizeof(server_addr)-1, "%s:%d",
pr_netaddr_get_ipstr(main_server->addr), main_server->ServerPort);
server_addr[sizeof(server_addr)-1] = '\0';
c = find_config(CURRENT_CONF, CONF_PARAM, "MaxTransfersPerHost", FALSE);
while (c != NULL) {
char *xfer_cmd = NULL, **cmdlist = (char **) c->argv[0];
unsigned char matched_cmd = FALSE;
unsigned int curr = 0, max = 0;
pr_scoreboard_entry_t *score = NULL;
pr_signals_handle();
/* Does this MaxTransfersPerHost apply to the current command? Note: this
* could be made more efficient by using bitmasks rather than string
* comparisons.
*/
for (xfer_cmd = *cmdlist; xfer_cmd; xfer_cmd = *(cmdlist++)) {
if (strcasecmp(xfer_cmd, cmd->argv[0]) == 0) {
matched_cmd = TRUE;
break;
}
}
if (matched_cmd == FALSE) {
c = find_config_next(c, c->next, CONF_PARAM, "MaxTransfersPerHost",
FALSE);
continue;
}
max = *((unsigned int *) c->argv[1]);
/* Count how many times the current IP address is logged in, AND how
* many of those other logins are currently using this command.
*/
(void) pr_rewind_scoreboard();
while ((score = pr_scoreboard_entry_read()) != NULL) {
pr_signals_handle();
/* Scoreboard entry must match local server address and remote client
* address to be counted.
*/
if (strcmp(score->sce_server_addr, server_addr) != 0) {
pr_trace_msg(trace_channel, 25,
"MaxTransfersPerHost: server address '%s' does not match '%s', "
"skipping", server_addr, score->sce_server_addr);
continue;
}
if (strcmp(score->sce_client_addr, client_addr) != 0) {
pr_trace_msg(trace_channel, 25,
"MaxTransfersPerHost: client address '%s' does not match '%s', "
"skipping", client_addr, score->sce_client_addr);
continue;
}
if (strcmp(score->sce_cmd, xfer_cmd) != 0) {
pr_trace_msg(trace_channel, 25,
"MaxTransfersPerHost: current command '%s' does not match '%s', "
"skipping", xfer_cmd, score->sce_cmd);
continue;
}
curr++;
}
pr_restore_scoreboard();
pr_trace_msg(trace_channel, 19,
"MaxTransfersPerHost: %s (current = %u, max = %u) for client '%s'",
xfer_cmd, curr, max, client_addr);
if (curr >= max) {
char maxn[20];
char *maxstr = "Sorry, the maximum number of data transfers (%m) from "
"your host are currently being used.";
if (c->argv[2] != NULL) {
maxstr = c->argv[2];
}
pr_event_generate("mod_xfer.max-transfers-per-host", session.c);
memset(maxn, '\0', sizeof(maxn));
pr_snprintf(maxn, sizeof(maxn)-1, "%u", max);
pr_response_send(R_451, "%s", sreplace(cmd->tmp_pool, maxstr, "%m",
maxn, NULL));
pr_log_debug(DEBUG4, "MaxTransfersPerHost %u exceeded for %s for "
"client '%s'", max, xfer_cmd, client_addr);
return -1;
}
c = find_config_next(c, c->next, CONF_PARAM, "MaxTransfersPerHost", FALSE);
}
c = find_config(CURRENT_CONF, CONF_PARAM, "MaxTransfersPerUser", FALSE);
while (c != NULL) {
char *xfer_cmd = NULL, **cmdlist = (char **) c->argv[0];
unsigned char matched_cmd = FALSE;
unsigned int curr = 0, max = 0;
pr_scoreboard_entry_t *score = NULL;
pr_signals_handle();
/* Does this MaxTransfersPerUser apply to the current command? Note: this
* could be made more efficient by using bitmasks rather than string
* comparisons.
*/
for (xfer_cmd = *cmdlist; xfer_cmd; xfer_cmd = *(cmdlist++)) {
if (strcasecmp(xfer_cmd, cmd->argv[0]) == 0) {
matched_cmd = TRUE;
break;
}
}
if (matched_cmd == FALSE) {
c = find_config_next(c, c->next, CONF_PARAM, "MaxTransfersPerUser",
FALSE);
continue;
}
max = *((unsigned int *) c->argv[1]);
/* Count how many times the current user is logged in, AND how many of
* those other logins are currently using this command.
*/
(void) pr_rewind_scoreboard();
while ((score = pr_scoreboard_entry_read()) != NULL) {
pr_signals_handle();
if (strcmp(score->sce_server_addr, server_addr) != 0) {
pr_trace_msg(trace_channel, 25,
"MaxTransfersPerUser: server address '%s' does not match '%s', "
"skipping", server_addr, score->sce_server_addr);
continue;
}
if (strcmp(score->sce_user, session.user) != 0) {
pr_trace_msg(trace_channel, 25,
"MaxTransfersPerUser: user '%s' does not match '%s', skipping",
session.user, score->sce_user);
continue;
}
if (strcmp(score->sce_cmd, xfer_cmd) == 0) {
pr_trace_msg(trace_channel, 25,
"MaxTransfersPerUser: command '%s' does not match '%s', skipping",
xfer_cmd, score->sce_cmd);
continue;
}
curr++;
}
pr_restore_scoreboard();
pr_trace_msg(trace_channel, 19,
"MaxTransfersPerUser: %s (current = %u, max = %u) for user '%s'",
xfer_cmd, curr, max, session.user);
if (curr >= max) {
char maxn[20];
char *maxstr = "Sorry, the maximum number of data transfers (%m) from "
"this user are currently being used.";
if (c->argv[2] != NULL) {
maxstr = c->argv[2];
}
pr_event_generate("mod_xfer.max-transfers-per-user", session.user);
memset(maxn, '\0', sizeof(maxn));
pr_snprintf(maxn, sizeof(maxn)-1, "%u", max);
pr_response_send(R_451, "%s", sreplace(cmd->tmp_pool, maxstr, "%m",
maxn, NULL));
pr_log_debug(DEBUG4, "MaxTransfersPerUser %u exceeded for %s for "
"user '%s'", max, xfer_cmd, session.user);
return -1;
}
c = find_config_next(c, c->next, CONF_PARAM, "MaxTransfersPerUser", FALSE);
}
return 0;
}
static void xfer_displayfile(void) {
if (displayfilexfer_fh != NULL) {
if (pr_display_fh(displayfilexfer_fh, session.vwd, R_226, 0) < 0) {
pr_log_debug(DEBUG6, "unable to display DisplayFileTransfer "
"file '%s': %s", displayfilexfer_fh->fh_path, strerror(errno));
}
/* Rewind the filehandle, so that it can be used again. */
if (pr_fsio_lseek(displayfilexfer_fh, 0, SEEK_SET) < 0) {
pr_log_debug(DEBUG6, "error rewinding DisplayFileTransfer "
"file '%s': %s", displayfilexfer_fh->fh_path, strerror(errno));
}
} else {
char *displayfilexfer;
displayfilexfer = get_param_ptr(main_server->conf, "DisplayFileTransfer",
FALSE);
if (displayfilexfer) {
if (pr_display_file(displayfilexfer, session.vwd, R_226, 0) < 0) {
pr_log_debug(DEBUG6, "unable to display DisplayFileTransfer "
"file '%s': %s", displayfilexfer, strerror(errno));
}
}
}
}
static int xfer_parse_cmdlist(const char *name, config_rec *c,
char *cmdlist) {
char *cmd = NULL;
array_header *cmds = NULL;
/* Allocate an array_header. */
cmds = make_array(c->pool, 0, sizeof(char *));
/* Add each command to the array, checking for invalid commands or
* duplicates.
*/
while ((cmd = get_cmd_from_list(&cmdlist)) != NULL) {
/* Is the given command a valid one for this directive? */
if (strcasecmp(cmd, C_APPE) != 0 &&
strcasecmp(cmd, C_RETR) != 0 &&
strcasecmp(cmd, C_STOR) != 0 &&
strcasecmp(cmd, C_STOU) != 0) {
pr_log_debug(DEBUG0, "invalid %s command: %s", name, cmd);
errno = EINVAL;
return -1;
}
*((char **) push_array(cmds)) = pstrdup(c->pool, cmd);
}
/* Terminate the array with a NULL. */
*((char **) push_array(cmds)) = NULL;
/* Store the array of commands in the config_rec. */
c->argv[0] = (void *) cmds->elts;
return 0;
}
static int transmit_normal(pool *p, char *buf, size_t bufsz) {
int xerrno;
long nread;
size_t read_len;
pr_error_t *err = NULL;
read_len = bufsz;
if (session.range_len > 0) {
if (((off_t) read_len) > session.range_len) {
read_len = session.range_len;
}
}
nread = pr_fsio_read_with_error(p, retr_fh, buf, read_len, &err);
xerrno = errno;
while (nread < 0) {
if (xerrno == EINTR) {
/* Interrupted by signal; handle it, and try again. */
errno = EINTR;
pr_signals_handle();
nread = pr_fsio_read_with_error(p, retr_fh, buf, read_len, &err);
xerrno = errno;
continue;
}
pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 4);
pr_error_set_why(err, pstrcat(p, "normal download of '", retr_fh->fh_path,
"'", NULL));
(void) pr_trace_msg("fileperms", 1, "RETR, user '%s' (UID %s, GID %s): "
"error reading from '%s': %s", session.user,
pr_uid2str(p, session.uid), pr_gid2str(p, session.gid),
retr_fh->fh_path, strerror(xerrno));
if (err != NULL) {
pr_log_debug(DEBUG9, "%s", pr_error_strerror(err, 0));
pr_error_destroy(err);
err = NULL;
}
errno = xerrno;
return -1;
}
if (nread == 0) {
return 0;
}
return pr_data_xfer(buf, nread);
}
#ifdef HAVE_SENDFILE
static int transmit_sendfile(off_t data_len, off_t *data_offset,
pr_sendfile_t *sent_len) {
off_t send_len;
/* We don't use sendfile() if:
* - We're using bandwidth throttling.
* - We're transmitting an ASCII file.
* - We're using RFC2228 data channel protection
* - We're using MODE Z compression
* - There's no data left to transmit.
* - UseSendfile is set to off.
*/
if (pr_throttle_have_rate() ||
!(session.xfer.file_size - data_len) ||
(session.sf_flags & (SF_ASCII|SF_ASCII_OVERRIDE)) ||
have_rfc2228_data || have_zmode ||
!use_sendfile) {
if (!xfer_logged_sendfile_decline_msg) {
if (!use_sendfile) {
pr_log_debug(DEBUG10, "declining use of sendfile due to UseSendfile "
"configuration setting");
} else if (pr_throttle_have_rate()) {
pr_log_debug(DEBUG10, "declining use of sendfile due to TransferRate "
"restrictions");
} else if (session.sf_flags & (SF_ASCII|SF_ASCII_OVERRIDE)) {
pr_log_debug(DEBUG10, "declining use of sendfile for ASCII data");
} else if (have_rfc2228_data) {
pr_log_debug(DEBUG10, "declining use of sendfile due to RFC2228 data "
"channel protections");
} else if (have_zmode) {
pr_log_debug(DEBUG10, "declining use of sendfile due to MODE Z "
"restrictions");
} else {
pr_log_debug(DEBUG10, "declining use of sendfile due to lack of data "
"to transmit");
}
xfer_logged_sendfile_decline_msg = TRUE;
}
return 0;
}
pr_log_debug(DEBUG10, "using sendfile capability for transmitting data");
/* Determine how many bytes to send using sendfile(2). By default,
* we want to send all of the remaining bytes.
*
* However, the admin may have configured either a length in bytes, or
* a percentage, using the UseSendfile directive. We will send the smaller
* of the remaining size, or the length/percentage.
*/
if (session.range_len > 0) {
send_len = session.range_len;
} else {
send_len = session.xfer.file_size - data_len;
}
if (use_sendfile_len > 0 &&
send_len > use_sendfile_len) {
pr_log_debug(DEBUG10, "using sendfile with configured UseSendfile length "
"(%" PR_LU " bytes)", (pr_off_t) use_sendfile_len);
send_len = use_sendfile_len;
} else if (use_sendfile_pct > 0.0) {
off_t pct_len;
pct_len = (off_t) (session.xfer.file_size * use_sendfile_pct);
if (send_len > pct_len) {
pr_log_debug(DEBUG10, "using sendfile with configured UseSendfile "
"percentage %0.0f%% (%" PR_LU " bytes)", use_sendfile_pct * 100.0,
(pr_off_t) pct_len);
send_len = pct_len;
}
}
retry:
*sent_len = pr_data_sendfile(PR_FH_FD(retr_fh), data_offset, send_len);
if (*sent_len == -1) {
int xerrno = errno;
switch (xerrno) {
case EAGAIN:
case EINTR:
if (XFER_ABORTED) {
pr_log_pri(PR_LOG_NOTICE, "sendfile transmission aborted: %s",
strerror(xerrno));
errno = xerrno;
return -1;
}
/* Interrupted call, or the other side wasn't ready yet. */
pr_signals_handle();
goto retry;
case EPIPE:
case ECONNRESET:
case ETIMEDOUT:
case EHOSTUNREACH:
/* Other side broke the connection. */
break;
#ifdef ENOSYS
case ENOSYS:
#endif /* ENOSYS */
#ifdef EOVERFLOW
case EOVERFLOW:
#endif /* EOVERFLOW */
case EINVAL:
/* No sendfile support, apparently. Try it the normal way. */
return 0;
break;
default:
pr_log_pri(PR_LOG_WARNING, "error using sendfile(): [%d] %s", xerrno,
strerror(xerrno));
errno = xerrno;
return -1;
}
}
return 1;
}
#endif /* HAVE_SENDFILE */
/* Note: the data_len and data_offset arguments are only for the benefit of
* transmit_sendfile(), if sendfile support is enabled. The transmit_normal()
* function only needs/uses buf and bufsz.
*/
static long transmit_data(pool *p, off_t data_len, off_t *data_offset,
char *buf, size_t bufsz) {
long res;
int xerrno = 0;
#ifdef HAVE_SENDFILE
pr_sendfile_t sent_len;
int ret;
#endif /* HAVE_SENDFILE */
if (pr_inet_set_proto_cork(PR_NETIO_FD(session.d->outstrm), 1) < 0) {
pr_log_pri(PR_LOG_NOTICE, "error corking socket fd %d: %s",
PR_NETIO_FD(session.d->outstrm), strerror(errno));
}
#ifdef HAVE_SENDFILE
ret = transmit_sendfile(data_len, data_offset, &sent_len);
if (ret > 0) {
/* sendfile() was used, so return the value of sent_len. */
res = (long) sent_len;
} else if (ret == 0) {
/* sendfile() should not be used for some reason, fallback to using
* normal data transmission methods.
*/
res = transmit_normal(p, buf, bufsz);
xerrno = errno;
} else {
/* There was an error with sendfile(); do NOT attempt to re-send the
* data using normal data transmission methods, unless the cause
* of the error is one of an accepted few cases.
*/
# ifdef EOVERFLOW
pr_log_debug(DEBUG10, "use of sendfile(2) failed due to %s (%d), "
"falling back to normal data transmission", strerror(errno),
errno);
res = transmit_normal(p, buf, bufsz);
xerrno = errno;
# else
if (session.d != NULL) {
(void) pr_inet_set_proto_cork(PR_NETIO_FD(session.d->outstrm), 0);
}
errno = EIO;
res = -1;
# endif
}
#else
res = transmit_normal(p, buf, bufsz);
xerrno = errno;
#endif /* HAVE_SENDFILE */
if (session.d != NULL) {
/* The session.d struct can become null after transmit_normal() if the
* client aborts the transfer, thus we need to check for this.
*/
if (pr_inet_set_proto_cork(PR_NETIO_FD(session.d->outstrm), 0) < 0) {
if (errno != EINVAL) {
pr_log_pri(PR_LOG_NOTICE, "error uncorking socket fd %d: %s",
PR_NETIO_FD(session.d->outstrm), strerror(errno));
}
}
}
errno = xerrno;
return res;
}
static void stor_chown(pool *p) {
struct stat st;
const char *xfer_path = NULL;
if (session.xfer.xfer_type == STOR_HIDDEN) {
xfer_path = session.xfer.path_hidden;
} else {
xfer_path = session.xfer.path;
}
/* session.fsgid defaults to -1, so chown(2) won't chgrp unless specifically
* requested via GroupOwner.
*/
if (session.fsuid != (uid_t) -1 &&
xfer_path != NULL) {
int res, xerrno = 0;
pr_error_t *err = NULL;
PRIVS_ROOT
res = pr_fsio_lchown_with_error(p, xfer_path, session.fsuid, session.fsgid,
&err);
xerrno = errno;
PRIVS_RELINQUISH
if (res < 0) {
pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 6);
pr_error_set_why(err, pstrcat(p, "set UserOwner of '", xfer_path,
"'", NULL));
if (err != NULL) {
pr_log_pri(PR_LOG_WARNING, "%s", pr_error_strerror(err, 0));
pr_error_destroy(err);
err = NULL;
} else {
pr_log_pri(PR_LOG_WARNING, "lchown(%s) as root failed: %s", xfer_path,
strerror(xerrno));
}
} else {
if (session.fsgid != (gid_t) -1) {
pr_log_debug(DEBUG2, "root lchown(%s) to UID %s, GID %s successful",
xfer_path, pr_uid2str(p, session.fsuid),
pr_gid2str(p, session.fsgid));
} else {
pr_log_debug(DEBUG2, "root lchown(%s) to UID %s successful", xfer_path,
pr_uid2str(p, session.fsuid));
}
pr_fs_clear_cache2(xfer_path);
if (pr_fsio_stat(xfer_path, &st) < 0) {
pr_log_debug(DEBUG0,
"'%s' stat(2) error during root chmod: %s", xfer_path,
strerror(errno));
}
/* The chmod happens after the chown because chown will remove
* the S{U,G}ID bits on some files (namely, directories); the subsequent
* chmod is used to restore those dropped bits. This makes it
* necessary to use root privs when doing the chmod as well (at least
* in the case of chown'ing the file via root privs) in order to ensure
* that the mode can be set (a file might be being "given away", and if
* root privs aren't used, the chmod() will fail because the old owner/
* session user doesn't have the necessary privileges to do so).
*/
xerrno = 0;
PRIVS_ROOT
res = pr_fsio_chmod_with_error(p, xfer_path, st.st_mode, &err);
xerrno = errno;
PRIVS_RELINQUISH
if (res < 0) {
pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 5);
pr_error_set_why(err, pstrcat(p, "restore SUID/SGID on '", xfer_path,
"'", NULL));
if (err != NULL) {
pr_log_debug(DEBUG0, "%s", pr_error_strerror(err, 0));
pr_error_destroy(err);
err = NULL;
} else {
pr_log_debug(DEBUG0, "root chmod(%s) to %04o failed: %s", xfer_path,
(unsigned int) st.st_mode, strerror(xerrno));
}
} else {
pr_log_debug(DEBUG2, "root chmod(%s) to %04o successful", xfer_path,
(unsigned int) st.st_mode);
}
}
} else if (session.fsgid != (gid_t) -1 &&
xfer_path != NULL) {
register unsigned int i;
int res, use_root_privs = TRUE, xerrno = 0;
pr_error_t *err = NULL;
/* Check if session.fsgid is in session.gids. If not, use root privs. */
for (i = 0; i < session.gids->nelts; i++) {
gid_t *group_ids = session.gids->elts;
if (group_ids[i] == session.fsgid) {
use_root_privs = FALSE;
break;
}
}
if (use_root_privs) {
PRIVS_ROOT
}
res = pr_fsio_lchown_with_error(p, xfer_path, (uid_t) -1, session.fsgid,
&err);
xerrno = errno;
if (use_root_privs) {
PRIVS_RELINQUISH
}
if (res < 0) {
pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 9);
pr_error_set_why(err, pstrcat(p, "set GroupOwner of '", xfer_path, "'",
NULL));
if (err != NULL) {
pr_log_pri(PR_LOG_WARNING, "%s", pr_error_strerror(err, 0));
pr_error_destroy(err);
err = NULL;
} else {
pr_log_pri(PR_LOG_WARNING, "%slchown(%s) failed: %s",
use_root_privs ? "root " : "", xfer_path, strerror(xerrno));
}
} else {
pr_log_debug(DEBUG2, "%slchown(%s) to GID %s successful",
use_root_privs ? "root " : "", xfer_path,
pr_gid2str(p, session.fsgid));
pr_fs_clear_cache2(xfer_path);
if (pr_fsio_stat(xfer_path, &st) < 0) {
pr_log_debug(DEBUG0,
"'%s' stat(2) error during %schmod: %s", xfer_path,
use_root_privs ? "root " : "", strerror(errno));
}
if (use_root_privs) {
PRIVS_ROOT
}
res = pr_fsio_chmod_with_error(p, xfer_path, st.st_mode, &err);
xerrno = errno;
if (use_root_privs) {
PRIVS_RELINQUISH
}
if (res < 0) {
pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 8);
pr_error_set_why(err, pstrcat(p, "restore SUID/SGID of '", xfer_path,
"'", NULL));
if (err != NULL) {
pr_log_debug(DEBUG0, "%s", pr_error_strerror(err, 0));
pr_error_destroy(err);
err = NULL;
} else {
pr_log_debug(DEBUG0, "%schmod(%s) to %04o failed: %s",
use_root_privs ? "root " : "", xfer_path, (unsigned int) st.st_mode,
strerror(xerrno));
}
}
}
}
}
static void retr_abort(pool *p) {
/* Isn't necessary to send anything here, just cleanup */
if (retr_fh) {
pr_fsio_close(retr_fh);
retr_fh = NULL;
}
_log_transfer('o', 'i');
}
static void retr_complete(pool *p) {
pr_fsio_close(retr_fh);
retr_fh = NULL;
}
static void stor_abort(pool *p) {
int res, xerrno = 0;
pool *tmp_pool;
pr_error_t *err = NULL;
unsigned char *delete_stores = NULL;
tmp_pool = make_sub_pool(p);
if (stor_fh != NULL) {
const char *fh_path;
/* Note that FSIO close() will destroy the fh pool, including the path.
* So make a copy, for logging.