/src/proftpd/modules/mod_xfer.c
Line | Count | Source |
1 | | /* |
2 | | * ProFTPD - FTP server daemon |
3 | | * Copyright (c) 1997, 1998 Public Flood Software |
4 | | * Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net> |
5 | | * Copyright (c) 2001-2026 The ProFTPD Project team |
6 | | * |
7 | | * This program is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * This program is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
19 | | * |
20 | | * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu |
21 | | * and other respective copyright holders give permission to link this program |
22 | | * with OpenSSL, and distribute the resulting executable, without including |
23 | | * the source code for OpenSSL in the source distribution. |
24 | | */ |
25 | | |
26 | | /* Data transfer module for ProFTPD */ |
27 | | |
28 | | #include "conf.h" |
29 | | #include "privs.h" |
30 | | #include "error.h" |
31 | | |
32 | | #ifdef HAVE_SYS_SENDFILE_H |
33 | | # include <sys/sendfile.h> |
34 | | #endif |
35 | | |
36 | | /* Minimum priority a process can have. */ |
37 | | #ifndef PRIO_MIN |
38 | | # define PRIO_MIN -20 |
39 | | #endif |
40 | | |
41 | | /* Maximum priority a process can have. */ |
42 | | #ifndef PRIO_MAX |
43 | | # define PRIO_MAX 20 |
44 | | #endif |
45 | | |
46 | | extern module auth_module; |
47 | | extern pid_t mpid; |
48 | | extern pr_response_t *resp_err_list; |
49 | | |
50 | | /* Variables for this module */ |
51 | | static pr_fh_t *retr_fh = NULL; |
52 | | static pr_fh_t *stor_fh = NULL; |
53 | | static pr_fh_t *displayfilexfer_fh = NULL; |
54 | | |
55 | | static unsigned char have_rfc2228_data = FALSE; |
56 | | static unsigned char have_type = FALSE; |
57 | | static unsigned char have_zmode = FALSE; |
58 | | static unsigned char use_sendfile = TRUE; |
59 | | static off_t use_sendfile_len = 0; |
60 | | static float use_sendfile_pct = -1.0; |
61 | | |
62 | | static int xfer_check_limit(cmd_rec *); |
63 | | |
64 | | /* TransferOptions */ |
65 | 0 | #define PR_XFER_OPT_HANDLE_ALLO 0x0001 |
66 | 0 | #define PR_XFER_OPT_IGNORE_ASCII 0x0002 |
67 | 0 | #define PR_XFER_OPT_ALLOW_SYMLINK_UPLOAD 0x0004 |
68 | | static unsigned long xfer_opts = PR_XFER_OPT_HANDLE_ALLO; |
69 | | |
70 | | static void xfer_exit_ev(const void *, void *); |
71 | | static void xfer_sigusr2_ev(const void *, void *); |
72 | | static void xfer_timeout_session_ev(const void *, void *); |
73 | | static void xfer_timeout_stalled_ev(const void *, void *); |
74 | | static int xfer_sess_init(void); |
75 | | |
76 | | /* Used for MaxTransfersPerHost and TransferRate */ |
77 | | static int xfer_parse_cmdlist(const char *, config_rec *, char *); |
78 | | |
79 | | module xfer_module; |
80 | | |
81 | | static int xfer_logged_sendfile_decline_msg = FALSE; |
82 | | |
83 | | static const char *trace_channel = "xfer"; |
84 | | |
85 | 0 | static off_t find_max_nbytes(char *directive) { |
86 | 0 | config_rec *c = NULL; |
87 | 0 | unsigned int ctxt_precedence = 0; |
88 | 0 | unsigned char have_user_limit, have_group_limit, have_class_limit, |
89 | 0 | have_all_limit; |
90 | 0 | off_t max_nbytes = 0UL; |
91 | |
|
92 | 0 | have_user_limit = have_group_limit = have_class_limit = |
93 | 0 | have_all_limit = FALSE; |
94 | |
|
95 | 0 | c = find_config(CURRENT_CONF, CONF_PARAM, directive, FALSE); |
96 | 0 | while (c != NULL) { |
97 | 0 | pr_signals_handle(); |
98 | | |
99 | | /* This check is for more than three arguments: one argument is the |
100 | | * classifier (i.e. "user", "group", or "class"), one argument is |
101 | | * the precedence, one is the number of bytes; the remaining arguments |
102 | | * are the individual items in the configured expression. |
103 | | */ |
104 | |
|
105 | 0 | if (c->argc > 3) { |
106 | 0 | if (strcasecmp(c->argv[2], "user") == 0) { |
107 | 0 | if (pr_expr_eval_user_or((char **) &c->argv[3]) == TRUE) { |
108 | 0 | if (*((unsigned int *) c->argv[1]) > ctxt_precedence) { |
109 | | |
110 | | /* Set the context precedence */ |
111 | 0 | ctxt_precedence = *((unsigned int *) c->argv[1]); |
112 | |
|
113 | 0 | max_nbytes = *((off_t *) c->argv[0]); |
114 | |
|
115 | 0 | have_group_limit = have_class_limit = have_all_limit = FALSE; |
116 | 0 | have_user_limit = TRUE; |
117 | 0 | } |
118 | 0 | } |
119 | |
|
120 | 0 | } else if (strcasecmp(c->argv[2], "group") == 0) { |
121 | 0 | if (pr_expr_eval_group_or((char **) &c->argv[3]) == TRUE) { |
122 | 0 | if (*((unsigned int *) c->argv[1]) > ctxt_precedence) { |
123 | | |
124 | | /* Set the context precedence */ |
125 | 0 | ctxt_precedence = *((unsigned int *) c->argv[1]); |
126 | |
|
127 | 0 | max_nbytes = *((off_t *) c->argv[0]); |
128 | |
|
129 | 0 | have_user_limit = have_class_limit = have_all_limit = FALSE; |
130 | 0 | have_group_limit = TRUE; |
131 | 0 | } |
132 | 0 | } |
133 | |
|
134 | 0 | } else if (strcasecmp(c->argv[2], "class") == 0) { |
135 | 0 | if (pr_expr_eval_class_or((char **) &c->argv[3]) == TRUE) { |
136 | 0 | if (*((unsigned int *) c->argv[1]) > ctxt_precedence) { |
137 | | |
138 | | /* Set the context precedence */ |
139 | 0 | ctxt_precedence = *((unsigned int *) c->argv[1]); |
140 | |
|
141 | 0 | max_nbytes = *((off_t *) c->argv[0]); |
142 | |
|
143 | 0 | have_user_limit = have_group_limit = have_all_limit = FALSE; |
144 | 0 | have_class_limit = TRUE; |
145 | 0 | } |
146 | 0 | } |
147 | 0 | } |
148 | |
|
149 | 0 | } else { |
150 | 0 | if (*((unsigned int *) c->argv[1]) > ctxt_precedence) { |
151 | | |
152 | | /* Set the context precedence. */ |
153 | 0 | ctxt_precedence = *((unsigned int *) c->argv[1]); |
154 | |
|
155 | 0 | max_nbytes = *((off_t *) c->argv[0]); |
156 | |
|
157 | 0 | have_user_limit = have_group_limit = have_class_limit = FALSE; |
158 | 0 | have_all_limit = TRUE; |
159 | 0 | } |
160 | 0 | } |
161 | |
|
162 | 0 | c = find_config_next(c, c->next, CONF_PARAM, directive, FALSE); |
163 | 0 | } |
164 | | |
165 | | /* Print out some nice debugging information. */ |
166 | 0 | if (max_nbytes > 0 && |
167 | 0 | (have_user_limit || |
168 | 0 | have_group_limit || |
169 | 0 | have_class_limit || |
170 | 0 | have_all_limit)) { |
171 | 0 | pr_log_debug(DEBUG5, "%s (%" PR_LU " bytes) in effect for %s", |
172 | 0 | directive, (pr_off_t) max_nbytes, |
173 | 0 | have_user_limit ? "user " : have_group_limit ? "group " : |
174 | 0 | have_class_limit ? "class " : "all"); |
175 | 0 | } |
176 | |
|
177 | 0 | return max_nbytes; |
178 | 0 | } |
179 | | |
180 | 0 | static void _log_transfer(char direction, char abort_flag) { |
181 | 0 | struct timeval end_time; |
182 | 0 | char *fullpath = NULL; |
183 | |
|
184 | 0 | memset(&end_time, '\0', sizeof(end_time)); |
185 | |
|
186 | 0 | if (session.xfer.start_time.tv_sec != 0) { |
187 | 0 | gettimeofday(&end_time, NULL); |
188 | 0 | end_time.tv_sec -= session.xfer.start_time.tv_sec; |
189 | |
|
190 | 0 | if (end_time.tv_usec >= session.xfer.start_time.tv_usec) { |
191 | 0 | end_time.tv_usec -= session.xfer.start_time.tv_usec; |
192 | |
|
193 | 0 | } else { |
194 | 0 | end_time.tv_usec = 1000000L - (session.xfer.start_time.tv_usec - |
195 | 0 | end_time.tv_usec); |
196 | 0 | end_time.tv_sec--; |
197 | 0 | } |
198 | 0 | } |
199 | |
|
200 | 0 | fullpath = dir_abs_path(session.xfer.p, session.xfer.path, TRUE); |
201 | |
|
202 | 0 | if ((session.sf_flags & SF_ANON) != 0) { |
203 | 0 | xferlog_write(end_time.tv_sec, pr_netaddr_get_sess_remote_name(), |
204 | 0 | session.xfer.total_bytes, fullpath, |
205 | 0 | (session.sf_flags & SF_ASCII ? 'a' : 'b'), direction, |
206 | 0 | 'a', session.anon_user, abort_flag, "_"); |
207 | |
|
208 | 0 | } else { |
209 | 0 | xferlog_write(end_time.tv_sec, pr_netaddr_get_sess_remote_name(), |
210 | 0 | session.xfer.total_bytes, fullpath, |
211 | 0 | (session.sf_flags & SF_ASCII ? 'a' : 'b'), direction, |
212 | 0 | 'r', session.user, abort_flag, "_"); |
213 | 0 | } |
214 | |
|
215 | 0 | pr_log_debug(DEBUG1, "Transfer %s %" PR_LU " bytes in %ld.%02lu seconds", |
216 | 0 | abort_flag == 'c' ? "completed:" : "aborted after", |
217 | 0 | (pr_off_t) session.xfer.total_bytes, (long) end_time.tv_sec, |
218 | 0 | (unsigned long)(end_time.tv_usec / 10000)); |
219 | 0 | } |
220 | | |
221 | | /* Code borrowed from src/dirtree.c's get_word() -- modified to separate |
222 | | * words on commas as well as spaces. |
223 | | */ |
224 | 0 | static char *get_cmd_from_list(char **list) { |
225 | 0 | char *res = NULL, *dst = NULL; |
226 | 0 | unsigned char quote_mode = FALSE; |
227 | |
|
228 | 0 | while (**list && PR_ISSPACE(**list)) { |
229 | 0 | (*list)++; |
230 | 0 | } |
231 | |
|
232 | 0 | if (!**list) { |
233 | 0 | return NULL; |
234 | 0 | } |
235 | | |
236 | 0 | res = dst = *list; |
237 | |
|
238 | 0 | if (**list == '\"') { |
239 | 0 | quote_mode = TRUE; |
240 | 0 | (*list)++; |
241 | 0 | } |
242 | |
|
243 | 0 | while (**list && **list != ',' && |
244 | 0 | (quote_mode ? (**list != '\"') : (!PR_ISSPACE(**list)))) { |
245 | |
|
246 | 0 | if (**list == '\\' && quote_mode) { |
247 | | |
248 | | /* escaped char */ |
249 | 0 | if (*((*list) + 1)) { |
250 | 0 | *dst = *(++(*list)); |
251 | 0 | } |
252 | 0 | } |
253 | |
|
254 | 0 | *dst++ = **list; |
255 | 0 | ++(*list); |
256 | 0 | } |
257 | |
|
258 | 0 | if (**list) { |
259 | 0 | (*list)++; |
260 | 0 | } |
261 | |
|
262 | 0 | *dst = '\0'; |
263 | 0 | return res; |
264 | 0 | } |
265 | | |
266 | 0 | static int xfer_check_limit(cmd_rec *cmd) { |
267 | 0 | config_rec *c = NULL; |
268 | 0 | const char *client_addr, *server_addr; |
269 | 0 | char server_addr_buf[128]; |
270 | |
|
271 | 0 | server_addr = pr_netaddr_get_ipstr(main_server->addr); |
272 | 0 | if (strcmp(server_addr, "0.0.0.0") == 0) { |
273 | 0 | server_addr = pr_netaddr_get_ipstr(session.c->local_addr); |
274 | 0 | } |
275 | |
|
276 | 0 | memset(server_addr_buf, '\0', sizeof(server_addr_buf)); |
277 | 0 | pr_snprintf(server_addr_buf, sizeof(server_addr_buf)-1, "%s:%d", server_addr, |
278 | 0 | main_server->ServerPort); |
279 | 0 | server_addr_buf[sizeof(server_addr_buf)-1] = '\0'; |
280 | |
|
281 | 0 | client_addr = pr_netaddr_get_ipstr(session.c->remote_addr); |
282 | |
|
283 | 0 | c = find_config(CURRENT_CONF, CONF_PARAM, "MaxTransfersPerHost", FALSE); |
284 | 0 | while (c != NULL) { |
285 | 0 | char *xfer_cmd = NULL, **cmdlist = (char **) c->argv[0]; |
286 | 0 | unsigned char matched_cmd = FALSE; |
287 | 0 | unsigned int curr = 0, max = 0; |
288 | 0 | pr_scoreboard_entry_t *score = NULL; |
289 | |
|
290 | 0 | pr_signals_handle(); |
291 | | |
292 | | /* Does this MaxTransfersPerHost apply to the current command? Note: this |
293 | | * could be made more efficient by using bitmasks rather than string |
294 | | * comparisons. |
295 | | */ |
296 | 0 | for (xfer_cmd = *cmdlist; xfer_cmd; xfer_cmd = *(cmdlist++)) { |
297 | 0 | if (strcasecmp(xfer_cmd, cmd->argv[0]) == 0) { |
298 | 0 | matched_cmd = TRUE; |
299 | 0 | break; |
300 | 0 | } |
301 | 0 | } |
302 | |
|
303 | 0 | if (matched_cmd == FALSE) { |
304 | 0 | c = find_config_next(c, c->next, CONF_PARAM, "MaxTransfersPerHost", |
305 | 0 | FALSE); |
306 | 0 | continue; |
307 | 0 | } |
308 | | |
309 | 0 | max = *((unsigned int *) c->argv[1]); |
310 | | |
311 | | /* Count how many times the current IP address is logged in, AND how |
312 | | * many of those other logins are currently using this command. |
313 | | */ |
314 | |
|
315 | 0 | (void) pr_rewind_scoreboard(); |
316 | 0 | while ((score = pr_scoreboard_entry_read()) != NULL) { |
317 | 0 | pr_signals_handle(); |
318 | | |
319 | | /* Scoreboard entry must match local server address and remote client |
320 | | * address to be counted. |
321 | | */ |
322 | 0 | if (strcmp(score->sce_server_addr, server_addr_buf) != 0) { |
323 | 0 | pr_trace_msg(trace_channel, 25, |
324 | 0 | "MaxTransfersPerHost: server address '%s' does not match '%s', " |
325 | 0 | "skipping", server_addr_buf, score->sce_server_addr); |
326 | 0 | continue; |
327 | 0 | } |
328 | | |
329 | 0 | if (strcmp(score->sce_client_addr, client_addr) != 0) { |
330 | 0 | pr_trace_msg(trace_channel, 25, |
331 | 0 | "MaxTransfersPerHost: client address '%s' does not match '%s', " |
332 | 0 | "skipping", client_addr, score->sce_client_addr); |
333 | 0 | continue; |
334 | 0 | } |
335 | | |
336 | 0 | if (strcmp(score->sce_cmd, xfer_cmd) != 0) { |
337 | 0 | pr_trace_msg(trace_channel, 25, |
338 | 0 | "MaxTransfersPerHost: current command '%s' does not match '%s', " |
339 | 0 | "skipping", xfer_cmd, score->sce_cmd); |
340 | 0 | continue; |
341 | 0 | } |
342 | | |
343 | 0 | curr++; |
344 | 0 | } |
345 | |
|
346 | 0 | pr_restore_scoreboard(); |
347 | |
|
348 | 0 | pr_trace_msg(trace_channel, 19, |
349 | 0 | "MaxTransfersPerHost: %s (current = %u, max = %u) for client '%s'", |
350 | 0 | xfer_cmd, curr, max, client_addr); |
351 | |
|
352 | 0 | if (curr >= max) { |
353 | 0 | char maxn[20]; |
354 | |
|
355 | 0 | char *maxstr = "Sorry, the maximum number of data transfers (%m) from " |
356 | 0 | "your host are currently being used."; |
357 | |
|
358 | 0 | if (c->argv[2] != NULL) { |
359 | 0 | maxstr = c->argv[2]; |
360 | 0 | } |
361 | |
|
362 | 0 | pr_event_generate("mod_xfer.max-transfers-per-host", session.c); |
363 | |
|
364 | 0 | memset(maxn, '\0', sizeof(maxn)); |
365 | 0 | pr_snprintf(maxn, sizeof(maxn)-1, "%u", max); |
366 | 0 | pr_response_add_err(R_451, "%s", sreplace(cmd->tmp_pool, maxstr, "%m", |
367 | 0 | maxn, NULL)); |
368 | 0 | pr_log_debug(DEBUG4, "MaxTransfersPerHost %u exceeded for %s for " |
369 | 0 | "client '%s'", max, xfer_cmd, client_addr); |
370 | |
|
371 | 0 | return -1; |
372 | 0 | } |
373 | | |
374 | 0 | c = find_config_next(c, c->next, CONF_PARAM, "MaxTransfersPerHost", FALSE); |
375 | 0 | } |
376 | | |
377 | 0 | c = find_config(CURRENT_CONF, CONF_PARAM, "MaxTransfersPerUser", FALSE); |
378 | 0 | while (c != NULL) { |
379 | 0 | char *xfer_cmd = NULL, **cmdlist = (char **) c->argv[0]; |
380 | 0 | unsigned char matched_cmd = FALSE; |
381 | 0 | unsigned int curr = 0, max = 0; |
382 | 0 | pr_scoreboard_entry_t *score = NULL; |
383 | |
|
384 | 0 | pr_signals_handle(); |
385 | | |
386 | | /* Does this MaxTransfersPerUser apply to the current command? Note: this |
387 | | * could be made more efficient by using bitmasks rather than string |
388 | | * comparisons. |
389 | | */ |
390 | 0 | for (xfer_cmd = *cmdlist; xfer_cmd; xfer_cmd = *(cmdlist++)) { |
391 | 0 | if (strcasecmp(xfer_cmd, cmd->argv[0]) == 0) { |
392 | 0 | matched_cmd = TRUE; |
393 | 0 | break; |
394 | 0 | } |
395 | 0 | } |
396 | |
|
397 | 0 | if (matched_cmd == FALSE) { |
398 | 0 | c = find_config_next(c, c->next, CONF_PARAM, "MaxTransfersPerUser", |
399 | 0 | FALSE); |
400 | 0 | continue; |
401 | 0 | } |
402 | | |
403 | 0 | max = *((unsigned int *) c->argv[1]); |
404 | | |
405 | | /* Count how many times the current user is logged in, AND how many of |
406 | | * those other logins are currently using this command. |
407 | | */ |
408 | |
|
409 | 0 | (void) pr_rewind_scoreboard(); |
410 | 0 | while ((score = pr_scoreboard_entry_read()) != NULL) { |
411 | 0 | pr_signals_handle(); |
412 | |
|
413 | 0 | if (strcmp(score->sce_server_addr, server_addr_buf) != 0) { |
414 | 0 | pr_trace_msg(trace_channel, 25, |
415 | 0 | "MaxTransfersPerUser: server address '%s' does not match '%s', " |
416 | 0 | "skipping", server_addr_buf, score->sce_server_addr); |
417 | 0 | continue; |
418 | 0 | } |
419 | | |
420 | 0 | if (strcmp(score->sce_user, session.user) != 0) { |
421 | 0 | pr_trace_msg(trace_channel, 25, |
422 | 0 | "MaxTransfersPerUser: user '%s' does not match '%s', skipping", |
423 | 0 | session.user, score->sce_user); |
424 | 0 | continue; |
425 | 0 | } |
426 | | |
427 | 0 | if (strcmp(score->sce_cmd, xfer_cmd) != 0) { |
428 | 0 | pr_trace_msg(trace_channel, 25, |
429 | 0 | "MaxTransfersPerUser: command '%s' does not match '%s', skipping", |
430 | 0 | xfer_cmd, score->sce_cmd); |
431 | 0 | continue; |
432 | 0 | } |
433 | | |
434 | 0 | curr++; |
435 | 0 | } |
436 | |
|
437 | 0 | pr_restore_scoreboard(); |
438 | |
|
439 | 0 | pr_trace_msg(trace_channel, 19, |
440 | 0 | "MaxTransfersPerUser: %s (current = %u, max = %u) for user '%s'", |
441 | 0 | xfer_cmd, curr, max, session.user); |
442 | |
|
443 | 0 | if (curr >= max) { |
444 | 0 | char maxn[20]; |
445 | |
|
446 | 0 | char *maxstr = "Sorry, the maximum number of data transfers (%m) from " |
447 | 0 | "this user are currently being used."; |
448 | |
|
449 | 0 | if (c->argv[2] != NULL) { |
450 | 0 | maxstr = c->argv[2]; |
451 | 0 | } |
452 | |
|
453 | 0 | pr_event_generate("mod_xfer.max-transfers-per-user", session.user); |
454 | |
|
455 | 0 | memset(maxn, '\0', sizeof(maxn)); |
456 | 0 | pr_snprintf(maxn, sizeof(maxn)-1, "%u", max); |
457 | 0 | pr_response_add_err(R_451, "%s", sreplace(cmd->tmp_pool, maxstr, "%m", |
458 | 0 | maxn, NULL)); |
459 | 0 | pr_log_debug(DEBUG4, "MaxTransfersPerUser %u exceeded for %s for " |
460 | 0 | "user '%s'", max, xfer_cmd, session.user); |
461 | |
|
462 | 0 | return -1; |
463 | 0 | } |
464 | | |
465 | 0 | c = find_config_next(c, c->next, CONF_PARAM, "MaxTransfersPerUser", FALSE); |
466 | 0 | } |
467 | | |
468 | 0 | return 0; |
469 | 0 | } |
470 | | |
471 | 0 | static void xfer_displayfile(void) { |
472 | |
|
473 | 0 | if (displayfilexfer_fh != NULL) { |
474 | 0 | if (pr_display_fh(displayfilexfer_fh, session.vwd, R_226, 0) < 0) { |
475 | 0 | pr_log_debug(DEBUG6, "unable to display DisplayFileTransfer " |
476 | 0 | "file '%s': %s", displayfilexfer_fh->fh_path, strerror(errno)); |
477 | 0 | } |
478 | | |
479 | | /* Rewind the filehandle, so that it can be used again. */ |
480 | 0 | if (pr_fsio_lseek(displayfilexfer_fh, 0, SEEK_SET) < 0) { |
481 | 0 | pr_log_debug(DEBUG6, "error rewinding DisplayFileTransfer " |
482 | 0 | "file '%s': %s", displayfilexfer_fh->fh_path, strerror(errno)); |
483 | 0 | } |
484 | |
|
485 | 0 | } else { |
486 | 0 | char *displayfilexfer; |
487 | |
|
488 | 0 | displayfilexfer = get_param_ptr(main_server->conf, "DisplayFileTransfer", |
489 | 0 | FALSE); |
490 | 0 | if (displayfilexfer) { |
491 | 0 | if (pr_display_file(displayfilexfer, session.vwd, R_226, 0) < 0) { |
492 | 0 | pr_log_debug(DEBUG6, "unable to display DisplayFileTransfer " |
493 | 0 | "file '%s': %s", displayfilexfer, strerror(errno)); |
494 | 0 | } |
495 | 0 | } |
496 | 0 | } |
497 | 0 | } |
498 | | |
499 | | static int xfer_parse_cmdlist(const char *name, config_rec *c, |
500 | 0 | char *cmdlist) { |
501 | 0 | char *cmd = NULL; |
502 | 0 | array_header *cmds = NULL; |
503 | | |
504 | | /* Allocate an array_header. */ |
505 | 0 | cmds = make_array(c->pool, 0, sizeof(char *)); |
506 | | |
507 | | /* Add each command to the array, checking for invalid commands or |
508 | | * duplicates. |
509 | | */ |
510 | 0 | while ((cmd = get_cmd_from_list(&cmdlist)) != NULL) { |
511 | | |
512 | | /* Is the given command a valid one for this directive? */ |
513 | 0 | if (strcasecmp(cmd, C_APPE) != 0 && |
514 | 0 | strcasecmp(cmd, C_RETR) != 0 && |
515 | 0 | strcasecmp(cmd, C_STOR) != 0 && |
516 | 0 | strcasecmp(cmd, C_STOU) != 0) { |
517 | 0 | pr_log_debug(DEBUG0, "invalid %s command: %s", name, cmd); |
518 | 0 | errno = EINVAL; |
519 | 0 | return -1; |
520 | 0 | } |
521 | | |
522 | 0 | *((char **) push_array(cmds)) = pstrdup(c->pool, cmd); |
523 | 0 | } |
524 | | |
525 | | /* Terminate the array with a NULL. */ |
526 | 0 | *((char **) push_array(cmds)) = NULL; |
527 | | |
528 | | /* Store the array of commands in the config_rec. */ |
529 | 0 | c->argv[0] = (void *) cmds->elts; |
530 | |
|
531 | 0 | return 0; |
532 | 0 | } |
533 | | |
534 | 0 | static int transmit_normal(pool *p, char *buf, size_t bufsz) { |
535 | 0 | int xerrno; |
536 | 0 | long nread; |
537 | 0 | size_t read_len; |
538 | 0 | pr_error_t *err = NULL; |
539 | |
|
540 | 0 | read_len = bufsz; |
541 | 0 | if (session.range_len > 0) { |
542 | 0 | if (((off_t) read_len) > session.range_len) { |
543 | 0 | read_len = session.range_len; |
544 | 0 | } |
545 | 0 | } |
546 | |
|
547 | 0 | nread = pr_fsio_read_with_error(p, retr_fh, buf, read_len, &err); |
548 | 0 | xerrno = errno; |
549 | |
|
550 | 0 | while (nread < 0) { |
551 | 0 | if (xerrno == EINTR) { |
552 | | /* Interrupted by signal; handle it, and try again. */ |
553 | 0 | errno = EINTR; |
554 | 0 | pr_signals_handle(); |
555 | |
|
556 | 0 | nread = pr_fsio_read_with_error(p, retr_fh, buf, read_len, &err); |
557 | 0 | xerrno = errno; |
558 | 0 | continue; |
559 | 0 | } |
560 | | |
561 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 4); |
562 | 0 | pr_error_set_why(err, pstrcat(p, "normal download of '", retr_fh->fh_path, |
563 | 0 | "'", NULL)); |
564 | |
|
565 | 0 | (void) pr_trace_msg("fileperms", 1, "RETR, user '%s' (UID %s, GID %s): " |
566 | 0 | "error reading from '%s': %s", session.user, |
567 | 0 | pr_uid2str(p, session.uid), pr_gid2str(p, session.gid), |
568 | 0 | retr_fh->fh_path, strerror(xerrno)); |
569 | |
|
570 | 0 | if (err != NULL) { |
571 | 0 | pr_log_debug(DEBUG9, "%s", pr_error_strerror(err, 0)); |
572 | 0 | pr_error_destroy(err); |
573 | 0 | err = NULL; |
574 | 0 | } |
575 | |
|
576 | 0 | errno = xerrno; |
577 | 0 | return -1; |
578 | 0 | } |
579 | | |
580 | 0 | if (nread == 0) { |
581 | 0 | return 0; |
582 | 0 | } |
583 | | |
584 | 0 | return pr_data_xfer(buf, nread); |
585 | 0 | } |
586 | | |
587 | | #ifdef HAVE_SENDFILE |
588 | | static int transmit_sendfile(off_t data_len, off_t *data_offset, |
589 | 0 | pr_sendfile_t *sent_len) { |
590 | 0 | off_t send_len; |
591 | | |
592 | | /* We don't use sendfile() if: |
593 | | * - We're using bandwidth throttling. |
594 | | * - We're transmitting an ASCII file. |
595 | | * - We're using RFC2228 data channel protection |
596 | | * - We're using MODE Z compression |
597 | | * - There's no data left to transmit. |
598 | | * - UseSendfile is set to off. |
599 | | */ |
600 | 0 | if (pr_throttle_have_rate() || |
601 | 0 | !(session.xfer.file_size - data_len) || |
602 | 0 | (session.sf_flags & (SF_ASCII|SF_ASCII_OVERRIDE)) || |
603 | 0 | have_rfc2228_data || have_zmode || |
604 | 0 | !use_sendfile) { |
605 | |
|
606 | 0 | if (!xfer_logged_sendfile_decline_msg) { |
607 | 0 | if (!use_sendfile) { |
608 | 0 | pr_log_debug(DEBUG10, "declining use of sendfile due to UseSendfile " |
609 | 0 | "configuration setting"); |
610 | |
|
611 | 0 | } else if (pr_throttle_have_rate()) { |
612 | 0 | pr_log_debug(DEBUG10, "declining use of sendfile due to TransferRate " |
613 | 0 | "restrictions"); |
614 | |
|
615 | 0 | } else if (session.sf_flags & (SF_ASCII|SF_ASCII_OVERRIDE)) { |
616 | 0 | pr_log_debug(DEBUG10, "declining use of sendfile for ASCII data"); |
617 | |
|
618 | 0 | } else if (have_rfc2228_data) { |
619 | 0 | pr_log_debug(DEBUG10, "declining use of sendfile due to RFC2228 data " |
620 | 0 | "channel protections"); |
621 | |
|
622 | 0 | } else if (have_zmode) { |
623 | 0 | pr_log_debug(DEBUG10, "declining use of sendfile due to MODE Z " |
624 | 0 | "restrictions"); |
625 | |
|
626 | 0 | } else { |
627 | 0 | pr_log_debug(DEBUG10, "declining use of sendfile due to lack of data " |
628 | 0 | "to transmit"); |
629 | 0 | } |
630 | |
|
631 | 0 | xfer_logged_sendfile_decline_msg = TRUE; |
632 | 0 | } |
633 | |
|
634 | 0 | return 0; |
635 | 0 | } |
636 | | |
637 | 0 | pr_log_debug(DEBUG10, "using sendfile capability for transmitting data"); |
638 | | |
639 | | /* Determine how many bytes to send using sendfile(2). By default, |
640 | | * we want to send all of the remaining bytes. |
641 | | * |
642 | | * However, the admin may have configured either a length in bytes, or |
643 | | * a percentage, using the UseSendfile directive. We will send the smaller |
644 | | * of the remaining size, or the length/percentage. |
645 | | */ |
646 | |
|
647 | 0 | if (session.range_len > 0) { |
648 | 0 | send_len = session.range_len; |
649 | |
|
650 | 0 | } else { |
651 | 0 | send_len = session.xfer.file_size - data_len; |
652 | 0 | } |
653 | |
|
654 | 0 | if (use_sendfile_len > 0 && |
655 | 0 | send_len > use_sendfile_len) { |
656 | 0 | pr_log_debug(DEBUG10, "using sendfile with configured UseSendfile length " |
657 | 0 | "(%" PR_LU " bytes)", (pr_off_t) use_sendfile_len); |
658 | 0 | send_len = use_sendfile_len; |
659 | |
|
660 | 0 | } else if (use_sendfile_pct > 0.0) { |
661 | 0 | off_t pct_len; |
662 | |
|
663 | 0 | pct_len = (off_t) (session.xfer.file_size * use_sendfile_pct); |
664 | 0 | if (send_len > pct_len) { |
665 | 0 | pr_log_debug(DEBUG10, "using sendfile with configured UseSendfile " |
666 | 0 | "percentage %0.0f%% (%" PR_LU " bytes)", use_sendfile_pct * 100.0, |
667 | 0 | (pr_off_t) pct_len); |
668 | 0 | send_len = pct_len; |
669 | 0 | } |
670 | 0 | } |
671 | |
|
672 | 0 | retry: |
673 | 0 | *sent_len = pr_data_sendfile(PR_FH_FD(retr_fh), data_offset, send_len); |
674 | |
|
675 | 0 | if (*sent_len == -1) { |
676 | 0 | int xerrno = errno; |
677 | |
|
678 | 0 | switch (xerrno) { |
679 | 0 | case EAGAIN: |
680 | 0 | case EINTR: |
681 | 0 | if (XFER_ABORTED) { |
682 | 0 | pr_log_pri(PR_LOG_NOTICE, "sendfile transmission aborted: %s", |
683 | 0 | strerror(xerrno)); |
684 | 0 | errno = xerrno; |
685 | 0 | return -1; |
686 | 0 | } |
687 | | |
688 | | /* Interrupted call, or the other side wasn't ready yet. */ |
689 | 0 | pr_signals_handle(); |
690 | 0 | goto retry; |
691 | | |
692 | 0 | case EPIPE: |
693 | 0 | case ECONNRESET: |
694 | 0 | case ETIMEDOUT: |
695 | 0 | case EHOSTUNREACH: |
696 | | /* Other side broke the connection. */ |
697 | 0 | break; |
698 | | |
699 | 0 | #ifdef ENOSYS |
700 | 0 | case ENOSYS: |
701 | 0 | #endif /* ENOSYS */ |
702 | |
|
703 | 0 | #ifdef EOVERFLOW |
704 | 0 | case EOVERFLOW: |
705 | 0 | #endif /* EOVERFLOW */ |
706 | |
|
707 | 0 | case EINVAL: |
708 | | /* No sendfile support, apparently. Try it the normal way. */ |
709 | 0 | return 0; |
710 | 0 | break; |
711 | | |
712 | 0 | default: |
713 | 0 | pr_log_pri(PR_LOG_WARNING, "error using sendfile(): [%d] %s", xerrno, |
714 | 0 | strerror(xerrno)); |
715 | 0 | errno = xerrno; |
716 | 0 | return -1; |
717 | 0 | } |
718 | 0 | } |
719 | | |
720 | 0 | return 1; |
721 | 0 | } |
722 | | #endif /* HAVE_SENDFILE */ |
723 | | |
724 | | /* Note: the data_len and data_offset arguments are only for the benefit of |
725 | | * transmit_sendfile(), if sendfile support is enabled. The transmit_normal() |
726 | | * function only needs/uses buf and bufsz. |
727 | | */ |
728 | | static long transmit_data(pool *p, off_t data_len, off_t *data_offset, |
729 | 0 | char *buf, size_t bufsz) { |
730 | 0 | long res; |
731 | 0 | int xerrno = 0; |
732 | |
|
733 | 0 | #ifdef HAVE_SENDFILE |
734 | 0 | pr_sendfile_t sent_len; |
735 | 0 | int ret; |
736 | 0 | #endif /* HAVE_SENDFILE */ |
737 | |
|
738 | 0 | if (pr_inet_set_proto_cork(PR_NETIO_FD(session.d->outstrm), 1) < 0) { |
739 | 0 | pr_log_pri(PR_LOG_NOTICE, "error corking socket fd %d: %s", |
740 | 0 | PR_NETIO_FD(session.d->outstrm), strerror(errno)); |
741 | 0 | } |
742 | |
|
743 | 0 | #ifdef HAVE_SENDFILE |
744 | 0 | ret = transmit_sendfile(data_len, data_offset, &sent_len); |
745 | 0 | if (ret > 0) { |
746 | | /* sendfile() was used, so return the value of sent_len. */ |
747 | 0 | res = (long) sent_len; |
748 | |
|
749 | 0 | } else if (ret == 0) { |
750 | | /* sendfile() should not be used for some reason, fallback to using |
751 | | * normal data transmission methods. |
752 | | */ |
753 | 0 | res = transmit_normal(p, buf, bufsz); |
754 | 0 | xerrno = errno; |
755 | |
|
756 | 0 | } else { |
757 | | /* There was an error with sendfile(); do NOT attempt to re-send the |
758 | | * data using normal data transmission methods, unless the cause |
759 | | * of the error is one of an accepted few cases. |
760 | | */ |
761 | 0 | # ifdef EOVERFLOW |
762 | 0 | pr_log_debug(DEBUG10, "use of sendfile(2) failed due to %s (%d), " |
763 | 0 | "falling back to normal data transmission", strerror(errno), |
764 | 0 | errno); |
765 | 0 | res = transmit_normal(p, buf, bufsz); |
766 | 0 | xerrno = errno; |
767 | |
|
768 | | # else |
769 | | if (session.d != NULL) { |
770 | | (void) pr_inet_set_proto_cork(PR_NETIO_FD(session.d->outstrm), 0); |
771 | | } |
772 | | |
773 | | errno = EIO; |
774 | | res = -1; |
775 | | # endif |
776 | 0 | } |
777 | |
|
778 | | #else |
779 | | res = transmit_normal(p, buf, bufsz); |
780 | | xerrno = errno; |
781 | | #endif /* HAVE_SENDFILE */ |
782 | |
|
783 | 0 | if (session.d != NULL) { |
784 | | /* The session.d struct can become null after transmit_normal() if the |
785 | | * client aborts the transfer, thus we need to check for this. |
786 | | */ |
787 | 0 | if (pr_inet_set_proto_cork(PR_NETIO_FD(session.d->outstrm), 0) < 0) { |
788 | 0 | if (errno != EINVAL) { |
789 | 0 | pr_log_pri(PR_LOG_NOTICE, "error uncorking socket fd %d: %s", |
790 | 0 | PR_NETIO_FD(session.d->outstrm), strerror(errno)); |
791 | 0 | } |
792 | 0 | } |
793 | 0 | } |
794 | |
|
795 | 0 | errno = xerrno; |
796 | 0 | return res; |
797 | 0 | } |
798 | | |
799 | 0 | static void stor_chown(pool *p) { |
800 | 0 | struct stat st; |
801 | 0 | const char *xfer_path = NULL; |
802 | |
|
803 | 0 | if (session.xfer.xfer_type == STOR_HIDDEN) { |
804 | 0 | xfer_path = session.xfer.path_hidden; |
805 | |
|
806 | 0 | } else { |
807 | 0 | xfer_path = session.xfer.path; |
808 | 0 | } |
809 | | |
810 | | /* session.fsgid defaults to -1, so chown(2) won't chgrp unless specifically |
811 | | * requested via GroupOwner. |
812 | | */ |
813 | 0 | if (session.fsuid != (uid_t) -1 && |
814 | 0 | xfer_path != NULL) { |
815 | 0 | int res, xerrno = 0; |
816 | 0 | pr_error_t *err = NULL; |
817 | |
|
818 | 0 | PRIVS_ROOT |
819 | 0 | res = pr_fsio_lchown_with_error(p, xfer_path, session.fsuid, session.fsgid, |
820 | 0 | &err); |
821 | 0 | xerrno = errno; |
822 | 0 | PRIVS_RELINQUISH |
823 | |
|
824 | 0 | if (res < 0) { |
825 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 6); |
826 | 0 | pr_error_set_why(err, pstrcat(p, "set UserOwner of '", xfer_path, |
827 | 0 | "'", NULL)); |
828 | |
|
829 | 0 | if (err != NULL) { |
830 | 0 | pr_log_pri(PR_LOG_WARNING, "%s", pr_error_strerror(err, 0)); |
831 | 0 | pr_error_destroy(err); |
832 | 0 | err = NULL; |
833 | |
|
834 | 0 | } else { |
835 | 0 | pr_log_pri(PR_LOG_WARNING, "lchown(%s) as root failed: %s", xfer_path, |
836 | 0 | strerror(xerrno)); |
837 | 0 | } |
838 | |
|
839 | 0 | } else { |
840 | 0 | if (session.fsgid != (gid_t) -1) { |
841 | 0 | pr_log_debug(DEBUG2, "root lchown(%s) to UID %s, GID %s successful", |
842 | 0 | xfer_path, pr_uid2str(p, session.fsuid), |
843 | 0 | pr_gid2str(p, session.fsgid)); |
844 | |
|
845 | 0 | } else { |
846 | 0 | pr_log_debug(DEBUG2, "root lchown(%s) to UID %s successful", xfer_path, |
847 | 0 | pr_uid2str(p, session.fsuid)); |
848 | 0 | } |
849 | |
|
850 | 0 | pr_fs_clear_cache2(xfer_path); |
851 | 0 | if (pr_fsio_stat(xfer_path, &st) < 0) { |
852 | 0 | pr_log_debug(DEBUG0, |
853 | 0 | "'%s' stat(2) error during root chmod: %s", xfer_path, |
854 | 0 | strerror(errno)); |
855 | |
|
856 | 0 | } else { |
857 | | /* The chmod happens after the chown because chown will remove |
858 | | * the S{U,G}ID bits on some files (namely, directories); the subsequent |
859 | | * chmod is used to restore those dropped bits. This makes it |
860 | | * necessary to use root privs when doing the chmod as well (at least |
861 | | * in the case of chown'ing the file via root privs) in order to ensure |
862 | | * that the mode can be set (a file might be being "given away", and if |
863 | | * root privs aren't used, the chmod() will fail because the old owner/ |
864 | | * session user doesn't have the necessary privileges to do so). |
865 | | */ |
866 | 0 | xerrno = 0; |
867 | |
|
868 | 0 | PRIVS_ROOT |
869 | 0 | res = pr_fsio_chmod_with_error(p, xfer_path, st.st_mode, &err); |
870 | 0 | xerrno = errno; |
871 | 0 | PRIVS_RELINQUISH |
872 | |
|
873 | 0 | if (res < 0) { |
874 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 5); |
875 | 0 | pr_error_set_why(err, pstrcat(p, "restore SUID/SGID on '", xfer_path, |
876 | 0 | "'", NULL)); |
877 | |
|
878 | 0 | if (err != NULL) { |
879 | 0 | pr_log_debug(DEBUG0, "%s", pr_error_strerror(err, 0)); |
880 | 0 | pr_error_destroy(err); |
881 | 0 | err = NULL; |
882 | |
|
883 | 0 | } else { |
884 | 0 | pr_log_debug(DEBUG0, "root chmod(%s) to %04o failed: %s", xfer_path, |
885 | 0 | (unsigned int) st.st_mode, strerror(xerrno)); |
886 | 0 | } |
887 | |
|
888 | 0 | } else { |
889 | 0 | pr_log_debug(DEBUG2, "root chmod(%s) to %04o successful", xfer_path, |
890 | 0 | (unsigned int) st.st_mode); |
891 | 0 | } |
892 | 0 | } |
893 | 0 | } |
894 | |
|
895 | 0 | } else if (session.fsgid != (gid_t) -1 && |
896 | 0 | xfer_path != NULL) { |
897 | 0 | register unsigned int i; |
898 | 0 | int res, use_root_privs = TRUE, xerrno = 0; |
899 | 0 | pr_error_t *err = NULL; |
900 | | |
901 | | /* Check if session.fsgid is in session.gids. If not, use root privs. */ |
902 | 0 | for (i = 0; i < session.gids->nelts; i++) { |
903 | 0 | gid_t *group_ids = session.gids->elts; |
904 | |
|
905 | 0 | if (group_ids[i] == session.fsgid) { |
906 | 0 | use_root_privs = FALSE; |
907 | 0 | break; |
908 | 0 | } |
909 | 0 | } |
910 | |
|
911 | 0 | if (use_root_privs) { |
912 | 0 | PRIVS_ROOT |
913 | 0 | } |
914 | |
|
915 | 0 | res = pr_fsio_lchown_with_error(p, xfer_path, (uid_t) -1, session.fsgid, |
916 | 0 | &err); |
917 | 0 | xerrno = errno; |
918 | |
|
919 | 0 | if (use_root_privs) { |
920 | 0 | PRIVS_RELINQUISH |
921 | 0 | } |
922 | |
|
923 | 0 | if (res < 0) { |
924 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 9); |
925 | 0 | pr_error_set_why(err, pstrcat(p, "set GroupOwner of '", xfer_path, "'", |
926 | 0 | NULL)); |
927 | |
|
928 | 0 | if (err != NULL) { |
929 | 0 | pr_log_pri(PR_LOG_WARNING, "%s", pr_error_strerror(err, 0)); |
930 | 0 | pr_error_destroy(err); |
931 | 0 | err = NULL; |
932 | |
|
933 | 0 | } else { |
934 | 0 | pr_log_pri(PR_LOG_WARNING, "%slchown(%s) failed: %s", |
935 | 0 | use_root_privs ? "root " : "", xfer_path, strerror(xerrno)); |
936 | 0 | } |
937 | |
|
938 | 0 | } else { |
939 | 0 | pr_log_debug(DEBUG2, "%slchown(%s) to GID %s successful", |
940 | 0 | use_root_privs ? "root " : "", xfer_path, |
941 | 0 | pr_gid2str(p, session.fsgid)); |
942 | |
|
943 | 0 | pr_fs_clear_cache2(xfer_path); |
944 | 0 | if (pr_fsio_stat(xfer_path, &st) < 0) { |
945 | 0 | pr_log_debug(DEBUG0, |
946 | 0 | "'%s' stat(2) error during %schmod: %s", xfer_path, |
947 | 0 | use_root_privs ? "root " : "", strerror(errno)); |
948 | |
|
949 | 0 | } else { |
950 | |
|
951 | 0 | if (use_root_privs) { |
952 | 0 | PRIVS_ROOT |
953 | 0 | } |
954 | |
|
955 | 0 | res = pr_fsio_chmod_with_error(p, xfer_path, st.st_mode, &err); |
956 | 0 | xerrno = errno; |
957 | |
|
958 | 0 | if (use_root_privs) { |
959 | 0 | PRIVS_RELINQUISH |
960 | 0 | } |
961 | |
|
962 | 0 | if (res < 0) { |
963 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 8); |
964 | 0 | pr_error_set_why(err, pstrcat(p, "restore SUID/SGID of '", xfer_path, |
965 | 0 | "'", NULL)); |
966 | |
|
967 | 0 | if (err != NULL) { |
968 | 0 | pr_log_debug(DEBUG0, "%s", pr_error_strerror(err, 0)); |
969 | 0 | pr_error_destroy(err); |
970 | 0 | err = NULL; |
971 | |
|
972 | 0 | } else { |
973 | 0 | pr_log_debug(DEBUG0, "%schmod(%s) to %04o failed: %s", |
974 | 0 | use_root_privs ? "root " : "", xfer_path, |
975 | 0 | (unsigned int) st.st_mode, strerror(xerrno)); |
976 | 0 | } |
977 | 0 | } |
978 | 0 | } |
979 | 0 | } |
980 | 0 | } |
981 | 0 | } |
982 | | |
983 | 0 | static void retr_abort(pool *p) { |
984 | | /* Isn't necessary to send anything here, just cleanup */ |
985 | |
|
986 | 0 | if (retr_fh) { |
987 | 0 | pr_fsio_close(retr_fh); |
988 | 0 | retr_fh = NULL; |
989 | 0 | } |
990 | |
|
991 | 0 | _log_transfer('o', 'i'); |
992 | 0 | } |
993 | | |
994 | 0 | static void retr_complete(pool *p) { |
995 | 0 | pr_fsio_close(retr_fh); |
996 | 0 | retr_fh = NULL; |
997 | 0 | } |
998 | | |
999 | 0 | static void stor_abort(pool *p) { |
1000 | 0 | int res, xerrno = 0; |
1001 | 0 | pool *tmp_pool; |
1002 | 0 | pr_error_t *err = NULL; |
1003 | 0 | unsigned char *delete_stores = NULL; |
1004 | |
|
1005 | 0 | tmp_pool = make_sub_pool(p); |
1006 | |
|
1007 | 0 | if (stor_fh != NULL) { |
1008 | 0 | const char *fh_path; |
1009 | | |
1010 | | /* Note that FSIO close() will destroy the fh pool, including the path. |
1011 | | * So make a copy, for logging. |
1012 | | */ |
1013 | 0 | fh_path = pstrdup(tmp_pool, stor_fh->fh_path); |
1014 | |
|
1015 | 0 | res = pr_fsio_close_with_error(tmp_pool, stor_fh, &err); |
1016 | 0 | xerrno = errno; |
1017 | |
|
1018 | 0 | if (res < 0) { |
1019 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 4); |
1020 | 0 | pr_error_set_why(err, pstrcat(tmp_pool, "close file '", fh_path, "'", |
1021 | 0 | NULL)); |
1022 | |
|
1023 | 0 | if (err != NULL) { |
1024 | 0 | pr_log_pri(PR_LOG_NOTICE, "%s", pr_error_strerror(err, 0)); |
1025 | 0 | pr_error_destroy(err); |
1026 | 0 | err = NULL; |
1027 | |
|
1028 | 0 | } else { |
1029 | 0 | pr_log_pri(PR_LOG_NOTICE, "notice: error closing '%s': %s", fh_path, |
1030 | 0 | strerror(xerrno)); |
1031 | 0 | } |
1032 | |
|
1033 | 0 | errno = xerrno; |
1034 | 0 | } |
1035 | |
|
1036 | 0 | stor_fh = NULL; |
1037 | 0 | } |
1038 | |
|
1039 | 0 | delete_stores = get_param_ptr(CURRENT_CONF, "DeleteAbortedStores", FALSE); |
1040 | |
|
1041 | 0 | if (session.xfer.xfer_type == STOR_HIDDEN) { |
1042 | 0 | if (delete_stores == NULL || |
1043 | 0 | *delete_stores == TRUE) { |
1044 | | /* If a hidden store was aborted, remove only hidden file, not real |
1045 | | * one. |
1046 | | */ |
1047 | 0 | if (session.xfer.path_hidden) { |
1048 | 0 | pr_log_debug(DEBUG5, "removing aborted HiddenStores file '%s'", |
1049 | 0 | session.xfer.path_hidden); |
1050 | |
|
1051 | 0 | res = pr_fsio_unlink_with_error(tmp_pool, session.xfer.path_hidden, |
1052 | 0 | &err); |
1053 | 0 | xerrno = errno; |
1054 | |
|
1055 | 0 | if (res < 0) { |
1056 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 5); |
1057 | 0 | pr_error_set_why(err, pstrcat(tmp_pool, "delete HiddenStores file '", |
1058 | 0 | session.xfer.path_hidden, "'", NULL)); |
1059 | |
|
1060 | 0 | if (xerrno != ENOENT) { |
1061 | 0 | if (err != NULL) { |
1062 | 0 | pr_log_debug(DEBUG0, "%s", pr_error_strerror(err, 0)); |
1063 | |
|
1064 | 0 | } else { |
1065 | 0 | pr_log_debug(DEBUG0, "error deleting HiddenStores file '%s': %s", |
1066 | 0 | session.xfer.path_hidden, strerror(xerrno)); |
1067 | 0 | } |
1068 | 0 | } |
1069 | |
|
1070 | 0 | pr_error_destroy(err); |
1071 | 0 | err = NULL; |
1072 | 0 | } |
1073 | 0 | } |
1074 | 0 | } |
1075 | |
|
1076 | 0 | } else if (session.xfer.path != NULL) { |
1077 | 0 | if (delete_stores != NULL && |
1078 | 0 | *delete_stores == TRUE) { |
1079 | 0 | pr_log_debug(DEBUG5, "removing aborted file '%s'", session.xfer.path); |
1080 | |
|
1081 | 0 | res = pr_fsio_unlink_with_error(tmp_pool, session.xfer.path, &err); |
1082 | 0 | xerrno = errno; |
1083 | |
|
1084 | 0 | if (res < 0) { |
1085 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 4); |
1086 | 0 | pr_error_set_why(err, pstrcat(tmp_pool, "delete aborted file '", |
1087 | 0 | session.xfer.path, "'", NULL)); |
1088 | |
|
1089 | 0 | if (xerrno != ENOENT) { |
1090 | 0 | if (err != NULL) { |
1091 | 0 | pr_log_debug(DEBUG0, "%s", pr_error_strerror(err, 0)); |
1092 | 0 | pr_error_destroy(err); |
1093 | 0 | err = NULL; |
1094 | |
|
1095 | 0 | } else { |
1096 | 0 | pr_log_debug(DEBUG0, "error deleting aborted file '%s': %s", |
1097 | 0 | session.xfer.path, strerror(xerrno)); |
1098 | 0 | } |
1099 | 0 | } |
1100 | 0 | } |
1101 | 0 | } |
1102 | 0 | } |
1103 | |
|
1104 | 0 | destroy_pool(tmp_pool); |
1105 | 0 | _log_transfer('i', 'i'); |
1106 | 0 | } |
1107 | | |
1108 | 0 | static int stor_complete(pool *p) { |
1109 | 0 | int res, xerrno = 0; |
1110 | 0 | pool *tmp_pool; |
1111 | 0 | pr_error_t *err = NULL; |
1112 | 0 | const char *fh_path; |
1113 | |
|
1114 | 0 | tmp_pool = make_sub_pool(p); |
1115 | | |
1116 | | /* Note that FSIO close() will destroy the fh pool, including the path. |
1117 | | * So make a copy, for logging. |
1118 | | */ |
1119 | 0 | fh_path = pstrdup(tmp_pool, stor_fh->fh_path); |
1120 | |
|
1121 | 0 | res = pr_fsio_close_with_error(tmp_pool, stor_fh, &err); |
1122 | 0 | xerrno = errno; |
1123 | |
|
1124 | 0 | if (res < 0) { |
1125 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 4); |
1126 | 0 | pr_error_set_why(err, pstrcat(tmp_pool, "close uploaded file '", fh_path, |
1127 | 0 | "'", NULL)); |
1128 | |
|
1129 | 0 | if (err != NULL) { |
1130 | 0 | pr_log_pri(PR_LOG_NOTICE, "%s", pr_error_strerror(err, 0)); |
1131 | 0 | pr_error_destroy(err); |
1132 | 0 | err = NULL; |
1133 | |
|
1134 | 0 | } else { |
1135 | 0 | pr_log_pri(PR_LOG_NOTICE, "notice: error closing '%s': %s", fh_path, |
1136 | 0 | strerror(xerrno)); |
1137 | 0 | } |
1138 | | |
1139 | | /* We will unlink failed writes, but only if it's a HiddenStores file. |
1140 | | * Other files will need to be explicitly deleted/removed by the client. |
1141 | | */ |
1142 | 0 | if (session.xfer.xfer_type == STOR_HIDDEN) { |
1143 | 0 | if (session.xfer.path_hidden) { |
1144 | 0 | pr_log_debug(DEBUG5, "failed to close HiddenStores file '%s', removing", |
1145 | 0 | session.xfer.path_hidden); |
1146 | |
|
1147 | 0 | res = pr_fsio_unlink_with_error(tmp_pool, session.xfer.path_hidden, |
1148 | 0 | &err); |
1149 | 0 | xerrno = errno; |
1150 | |
|
1151 | 0 | if (res < 0) { |
1152 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 4); |
1153 | 0 | pr_error_set_why(err, pstrcat(tmp_pool, "close HiddenStores file '", |
1154 | 0 | session.xfer.path_hidden, "'", NULL)); |
1155 | |
|
1156 | 0 | if (xerrno != ENOENT) { |
1157 | 0 | if (err != NULL) { |
1158 | 0 | pr_log_debug(DEBUG0, "%s", pr_error_strerror(err, 0)); |
1159 | |
|
1160 | 0 | } else { |
1161 | 0 | pr_log_debug(DEBUG0, "error deleting HiddenStores file '%s': %s", |
1162 | 0 | session.xfer.path_hidden, strerror(xerrno)); |
1163 | 0 | } |
1164 | 0 | } |
1165 | |
|
1166 | 0 | pr_error_destroy(err); |
1167 | 0 | err = NULL; |
1168 | 0 | } |
1169 | 0 | } |
1170 | 0 | } |
1171 | |
|
1172 | 0 | errno = xerrno; |
1173 | 0 | res = -1; |
1174 | 0 | } |
1175 | |
|
1176 | 0 | destroy_pool(tmp_pool); |
1177 | 0 | stor_fh = NULL; |
1178 | 0 | return res; |
1179 | 0 | } |
1180 | | |
1181 | | static int get_hidden_store_path(cmd_rec *cmd, const char *path, |
1182 | 0 | const char *prefix, const char *suffix) { |
1183 | 0 | const char *c = NULL; |
1184 | 0 | char *hidden_path, *parent_dir = NULL; |
1185 | 0 | int dotcount = 0, found_slash = FALSE, basenamestart = 0, maxlen; |
1186 | | |
1187 | | /* We have to also figure out the temporary hidden file name for receiving |
1188 | | * this transfer. Length is +(N+M) due to prepended prefix and suffix. |
1189 | | */ |
1190 | | |
1191 | | /* Figure out where the basename starts */ |
1192 | 0 | for (c = path; *c; ++c) { |
1193 | |
|
1194 | 0 | if (*c == '/') { |
1195 | 0 | found_slash = TRUE; |
1196 | 0 | basenamestart = dotcount = 0; |
1197 | |
|
1198 | 0 | } else if (*c == '.') { |
1199 | 0 | ++dotcount; |
1200 | | |
1201 | | /* Keep track of leading dots, ... is normal, . and .. are special. |
1202 | | * So if we exceed ".." it becomes a normal file. Retroactively consider |
1203 | | * this the possible start of the basename. |
1204 | | */ |
1205 | 0 | if ((dotcount > 2) && |
1206 | 0 | !basenamestart) { |
1207 | 0 | basenamestart = ((unsigned long) c - (unsigned long) path) - dotcount; |
1208 | 0 | } |
1209 | |
|
1210 | 0 | } else { |
1211 | | |
1212 | | /* We found a nonslash, nondot character; if this is the first time |
1213 | | * we found one since the last slash, remember this as the possible |
1214 | | * start of the basename. |
1215 | | */ |
1216 | 0 | if (!basenamestart) { |
1217 | 0 | basenamestart = ((unsigned long) c - (unsigned long) path) - dotcount; |
1218 | 0 | } |
1219 | 0 | } |
1220 | 0 | } |
1221 | |
|
1222 | 0 | if (!basenamestart) { |
1223 | 0 | session.xfer.xfer_type = STOR_DEFAULT; |
1224 | |
|
1225 | 0 | pr_log_debug(DEBUG6, "could not determine HiddenStores path for '%s'", |
1226 | 0 | path); |
1227 | | |
1228 | | /* This probably shouldn't happen */ |
1229 | 0 | pr_response_add_err(R_451, _("%s: Bad file name"), path); |
1230 | 0 | errno = EINVAL; |
1231 | 0 | return -1; |
1232 | 0 | } |
1233 | | |
1234 | | /* Add N+M for the prefix and suffix characters, plus one for a terminating |
1235 | | * NUL. |
1236 | | */ |
1237 | 0 | maxlen = strlen(prefix) + strlen(path) + strlen(suffix) + 1; |
1238 | |
|
1239 | 0 | if (maxlen > PR_TUNABLE_PATH_MAX) { |
1240 | 0 | session.xfer.xfer_type = STOR_DEFAULT; |
1241 | |
|
1242 | 0 | pr_log_pri(PR_LOG_NOTICE, "making path '%s' a hidden path exceeds max " |
1243 | 0 | "path length (%u)", path, PR_TUNABLE_PATH_MAX); |
1244 | | |
1245 | | /* This probably shouldn't happen */ |
1246 | 0 | pr_response_add_err(R_451, _("%s: File name too long"), path); |
1247 | 0 | errno = EPERM; |
1248 | 0 | return -1; |
1249 | 0 | } |
1250 | | |
1251 | 0 | if (pr_table_add(cmd->notes, "mod_xfer.store-hidden-path", NULL, 0) < 0) { |
1252 | 0 | if (errno != EEXIST) { |
1253 | 0 | pr_log_pri(PR_LOG_NOTICE, |
1254 | 0 | "notice: error adding 'mod_xfer.store-hidden-path': %s", |
1255 | 0 | strerror(errno)); |
1256 | 0 | } |
1257 | 0 | } |
1258 | |
|
1259 | 0 | if (found_slash == FALSE) { |
1260 | | |
1261 | | /* Simple local file name */ |
1262 | 0 | hidden_path = pstrcat(cmd->tmp_pool, prefix, path, suffix, NULL); |
1263 | |
|
1264 | 0 | pr_log_debug(DEBUG2, "HiddenStore: local path, will rename %s to %s", |
1265 | 0 | hidden_path, path); |
1266 | |
|
1267 | 0 | } else { |
1268 | | |
1269 | | /* Complex relative path or absolute path */ |
1270 | 0 | hidden_path = pstrndup(cmd->pool, path, maxlen); |
1271 | 0 | hidden_path[basenamestart] = '\0'; |
1272 | |
|
1273 | 0 | hidden_path = pstrcat(cmd->pool, hidden_path, prefix, |
1274 | 0 | path + basenamestart, suffix, NULL); |
1275 | |
|
1276 | 0 | pr_log_debug(DEBUG2, "HiddenStore: complex path, will rename %s to %s", |
1277 | 0 | hidden_path, path); |
1278 | 0 | } |
1279 | |
|
1280 | 0 | pr_fs_clear_cache2(hidden_path); |
1281 | 0 | if (file_mode2(cmd->tmp_pool, hidden_path)) { |
1282 | 0 | session.xfer.xfer_type = STOR_DEFAULT; |
1283 | |
|
1284 | 0 | pr_log_debug(DEBUG3, "HiddenStore path '%s' already exists", |
1285 | 0 | hidden_path); |
1286 | |
|
1287 | 0 | pr_response_add_err(R_550, _("%s: Temporary hidden file %s already exists"), |
1288 | 0 | cmd->arg, hidden_path); |
1289 | 0 | errno = EEXIST; |
1290 | 0 | return -1; |
1291 | 0 | } |
1292 | | |
1293 | 0 | if (pr_table_set(cmd->notes, "mod_xfer.store-hidden-path", |
1294 | 0 | hidden_path, 0) < 0) { |
1295 | 0 | pr_log_pri(PR_LOG_NOTICE, |
1296 | 0 | "notice: error setting 'mod_xfer.store-hidden-path': %s", |
1297 | 0 | strerror(errno)); |
1298 | 0 | } |
1299 | | |
1300 | | /* Only use the O_EXCL open(2) flag if the path is NOT on an NFS-mounted |
1301 | | * filesystem (see Bug#3874). |
1302 | | */ |
1303 | 0 | if (found_slash == FALSE) { |
1304 | 0 | parent_dir = "./"; |
1305 | |
|
1306 | 0 | } else { |
1307 | 0 | parent_dir = pstrndup(cmd->tmp_pool, path, basenamestart); |
1308 | 0 | } |
1309 | |
|
1310 | 0 | if (pr_fs_is_nfs(parent_dir) == TRUE) { |
1311 | 0 | if (pr_table_add(cmd->notes, "mod_xfer.store-hidden-nfs", |
1312 | 0 | pstrdup(cmd->pool, "1"), 0) < 0) { |
1313 | 0 | pr_log_pri(PR_LOG_NOTICE, |
1314 | 0 | "notice: error adding 'mod_xfer.store-hidden-nfs' note: %s", |
1315 | 0 | strerror(errno)); |
1316 | 0 | } |
1317 | 0 | } |
1318 | |
|
1319 | 0 | session.xfer.xfer_type = STOR_HIDDEN; |
1320 | 0 | return 0; |
1321 | 0 | } |
1322 | | |
1323 | 0 | MODRET xfer_opts_rest(cmd_rec *cmd) { |
1324 | 0 | register unsigned int i; |
1325 | 0 | char *method, *xfer_cmd; |
1326 | 0 | unsigned char *authenticated; |
1327 | |
|
1328 | 0 | authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE); |
1329 | 0 | if (authenticated == NULL || |
1330 | 0 | *authenticated == FALSE) { |
1331 | 0 | pr_response_add_err(R_501, _("Please login with USER and PASS")); |
1332 | |
|
1333 | 0 | pr_cmd_set_errno(cmd, EPERM); |
1334 | 0 | errno = EPERM; |
1335 | 0 | return PR_ERROR(cmd); |
1336 | 0 | } |
1337 | | |
1338 | 0 | method = pstrdup(cmd->tmp_pool, cmd->argv[0]); |
1339 | | |
1340 | | /* Convert underscores to spaces in the method name, for prettier logging. */ |
1341 | 0 | for (i = 0; method[i]; i++) { |
1342 | 0 | if (method[i] == '_') { |
1343 | 0 | method[i] = ' '; |
1344 | 0 | } |
1345 | 0 | } |
1346 | |
|
1347 | 0 | if (cmd->argc != 2) { |
1348 | 0 | pr_response_add_err(R_501, _("'%s' not understood"), method); |
1349 | |
|
1350 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
1351 | 0 | errno = EINVAL; |
1352 | 0 | return PR_ERROR(cmd); |
1353 | 0 | } |
1354 | | |
1355 | 0 | xfer_cmd = cmd->argv[1]; |
1356 | 0 | if (strcasecmp(xfer_cmd, C_RETR) == 0) { |
1357 | 0 | unsigned char *allow_restart = NULL; |
1358 | | |
1359 | | /* Do we allow resumed downloads? */ |
1360 | 0 | allow_restart = get_param_ptr(main_server->conf, "AllowRetrieveRestart", |
1361 | 0 | FALSE); |
1362 | 0 | if (allow_restart == NULL || |
1363 | 0 | *allow_restart == TRUE) { |
1364 | 0 | pr_response_add(R_200, "%s", _("REST RETR allowed")); |
1365 | 0 | return PR_HANDLED(cmd); |
1366 | 0 | } |
1367 | | |
1368 | 0 | pr_response_add_err(R_451, "%s", _("REST RETR not allowed")); |
1369 | 0 | pr_cmd_set_errno(cmd, EPERM); |
1370 | 0 | errno = EPERM; |
1371 | 0 | return PR_ERROR(cmd); |
1372 | 0 | } |
1373 | | |
1374 | 0 | if (strcasecmp(xfer_cmd, C_STOR) == 0) { |
1375 | 0 | unsigned char *allow_restart = NULL; |
1376 | | |
1377 | | /* Do we allow resumed uploads? */ |
1378 | 0 | allow_restart = get_param_ptr(main_server->conf, "AllowStoreRestart", |
1379 | 0 | FALSE); |
1380 | 0 | if (allow_restart != NULL && |
1381 | 0 | *allow_restart == TRUE) { |
1382 | 0 | pr_response_add(R_200, "%s", _("REST STOR allowed")); |
1383 | 0 | return PR_HANDLED(cmd); |
1384 | 0 | } |
1385 | | |
1386 | 0 | pr_response_add_err(R_451, "%s", _("REST STOR not allowed")); |
1387 | 0 | pr_cmd_set_errno(cmd, EPERM); |
1388 | 0 | errno = EPERM; |
1389 | 0 | return PR_ERROR(cmd); |
1390 | 0 | } |
1391 | | |
1392 | | /* Otherwise, it's an OPTS REST query we do not support. */ |
1393 | 0 | pr_response_add_err(R_501, _("'%s' not understood"), method); |
1394 | |
|
1395 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
1396 | 0 | errno = EINVAL; |
1397 | 0 | return PR_ERROR(cmd); |
1398 | 0 | } |
1399 | | |
1400 | 0 | MODRET xfer_post_prot(cmd_rec *cmd) { |
1401 | 0 | CHECK_CMD_ARGS(cmd, 2); |
1402 | |
|
1403 | 0 | if (strcmp(cmd->argv[1], "C") != 0) { |
1404 | 0 | have_rfc2228_data = TRUE; |
1405 | |
|
1406 | 0 | } else { |
1407 | 0 | have_rfc2228_data = FALSE; |
1408 | 0 | } |
1409 | |
|
1410 | 0 | return PR_DECLINED(cmd); |
1411 | 0 | } |
1412 | | |
1413 | 0 | MODRET xfer_post_mode(cmd_rec *cmd) { |
1414 | 0 | CHECK_CMD_ARGS(cmd, 2); |
1415 | |
|
1416 | 0 | if (strcmp(cmd->argv[1], "Z") == 0) { |
1417 | 0 | have_zmode = TRUE; |
1418 | |
|
1419 | 0 | } else { |
1420 | 0 | have_zmode = FALSE; |
1421 | 0 | } |
1422 | |
|
1423 | 0 | return PR_DECLINED(cmd); |
1424 | 0 | } |
1425 | | |
1426 | | /* This is a PRE_CMD handler that checks security, etc, and places the full |
1427 | | * filename to receive in cmd->notes, under the key 'mod_xfer.store-path'. |
1428 | | * Note that we CANNOT use cmd->tmp_pool for this, as tmp_pool only lasts for |
1429 | | * the duration of this function. |
1430 | | */ |
1431 | 0 | MODRET xfer_pre_stor(cmd_rec *cmd) { |
1432 | 0 | char *decoded_path, *path; |
1433 | 0 | mode_t fmode = (mode_t) 0; |
1434 | 0 | unsigned char *allow_overwrite = NULL, *allow_restart = NULL; |
1435 | 0 | config_rec *c; |
1436 | 0 | int res, is_file = FALSE; |
1437 | |
|
1438 | 0 | if (cmd->argc < 2) { |
1439 | 0 | pr_response_add_err(R_500, _("'%s' not understood"), |
1440 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
1441 | |
|
1442 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
1443 | 0 | errno = EINVAL; |
1444 | 0 | return PR_ERROR(cmd); |
1445 | 0 | } |
1446 | | |
1447 | 0 | decoded_path = pr_fs_decode_path2(cmd->tmp_pool, cmd->arg, |
1448 | 0 | FSIO_DECODE_FL_TELL_ERRORS); |
1449 | 0 | if (decoded_path == NULL) { |
1450 | 0 | int xerrno = errno; |
1451 | |
|
1452 | 0 | pr_log_debug(DEBUG8, "'%s' failed to decode properly: %s", cmd->arg, |
1453 | 0 | strerror(xerrno)); |
1454 | 0 | pr_response_add_err(R_550, _("%s: Illegal character sequence in filename"), |
1455 | 0 | cmd->arg); |
1456 | |
|
1457 | 0 | pr_cmd_set_errno(cmd, xerrno); |
1458 | 0 | errno = xerrno; |
1459 | 0 | return PR_ERROR(cmd); |
1460 | 0 | } |
1461 | | |
1462 | 0 | pr_fs_clear_cache2(decoded_path); |
1463 | 0 | path = dir_best_path(cmd->tmp_pool, decoded_path); |
1464 | |
|
1465 | 0 | if (path == NULL || |
1466 | 0 | !dir_check(cmd->tmp_pool, cmd, cmd->group, path, NULL)) { |
1467 | 0 | int xerrno = errno; |
1468 | |
|
1469 | 0 | pr_log_debug(DEBUG8, "%s %s denied by <Limit> configuration", |
1470 | 0 | (char *) cmd->argv[0], cmd->arg); |
1471 | 0 | pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
1472 | |
|
1473 | 0 | pr_cmd_set_errno(cmd, xerrno); |
1474 | 0 | errno = xerrno; |
1475 | 0 | return PR_ERROR(cmd); |
1476 | 0 | } |
1477 | | |
1478 | 0 | res = pr_filter_allow_path(CURRENT_CONF, path); |
1479 | 0 | switch (res) { |
1480 | 0 | case 0: |
1481 | 0 | break; |
1482 | | |
1483 | 0 | case PR_FILTER_ERR_FAILS_ALLOW_FILTER: |
1484 | 0 | pr_log_pri(PR_LOG_NOTICE, "'%s %s' denied by PathAllowFilter", |
1485 | 0 | (char *) cmd->argv[0], path); |
1486 | 0 | pr_response_add_err(R_550, _("%s: Forbidden filename"), cmd->arg); |
1487 | |
|
1488 | 0 | pr_cmd_set_errno(cmd, EPERM); |
1489 | 0 | errno = EPERM; |
1490 | 0 | return PR_ERROR(cmd); |
1491 | | |
1492 | 0 | case PR_FILTER_ERR_FAILS_DENY_FILTER: |
1493 | 0 | pr_log_pri(PR_LOG_NOTICE, "'%s %s' denied by PathDenyFilter", |
1494 | 0 | (char *) cmd->argv[0], path); |
1495 | 0 | pr_response_add_err(R_550, _("%s: Forbidden filename"), cmd->arg); |
1496 | |
|
1497 | 0 | pr_cmd_set_errno(cmd, EPERM); |
1498 | 0 | errno = EPERM; |
1499 | 0 | return PR_ERROR(cmd); |
1500 | 0 | } |
1501 | | |
1502 | 0 | if (xfer_check_limit(cmd) < 0) { |
1503 | 0 | pr_response_add_err(R_451, _("%s: Too many transfers"), cmd->arg); |
1504 | |
|
1505 | 0 | pr_cmd_set_errno(cmd, EPERM); |
1506 | 0 | errno = EPERM; |
1507 | 0 | return PR_ERROR(cmd); |
1508 | 0 | } |
1509 | | |
1510 | 0 | fmode = file_mode2(cmd->tmp_pool, path); |
1511 | |
|
1512 | 0 | allow_overwrite = get_param_ptr(CURRENT_CONF, "AllowOverwrite", FALSE); |
1513 | |
|
1514 | 0 | if (fmode != 0) { |
1515 | 0 | if (session.xfer.xfer_type != STOR_APPEND && |
1516 | 0 | (!allow_overwrite || *allow_overwrite == FALSE)) { |
1517 | 0 | pr_log_debug(DEBUG6, "AllowOverwrite denied permission for %s", cmd->arg); |
1518 | 0 | pr_response_add_err(R_550, _("%s: Overwrite permission denied"), cmd->arg); |
1519 | |
|
1520 | 0 | pr_cmd_set_errno(cmd, EACCES); |
1521 | 0 | errno = EACCES; |
1522 | 0 | return PR_ERROR(cmd); |
1523 | 0 | } |
1524 | | |
1525 | 0 | if (S_ISREG(fmode) || |
1526 | 0 | S_ISFIFO(fmode)) { |
1527 | 0 | is_file = TRUE; |
1528 | |
|
1529 | 0 | } else { |
1530 | | /* Make an exception for the non-regular /dev/null file. This will allow |
1531 | | * network link testing by uploading as much data as necessary directly |
1532 | | * to /dev/null. |
1533 | | * |
1534 | | * On Linux, allow another exception for /dev/full; this is useful for |
1535 | | * tests which want to simulate running out-of-space scenarios. |
1536 | | */ |
1537 | 0 | if (strcasecmp(path, "/dev/null") == 0 |
1538 | 0 | #ifdef LINUX |
1539 | 0 | || strcasecmp(path, "/dev/full") == 0 |
1540 | 0 | #endif |
1541 | 0 | ) { |
1542 | 0 | is_file = TRUE; |
1543 | 0 | } |
1544 | |
|
1545 | 0 | if (is_file == FALSE && |
1546 | 0 | S_ISLNK(fmode) && |
1547 | 0 | (xfer_opts & PR_XFER_OPT_ALLOW_SYMLINK_UPLOAD)) { |
1548 | | /* We are allowing uploading to symlinks. */ |
1549 | 0 | is_file = TRUE; |
1550 | 0 | } |
1551 | 0 | } |
1552 | |
|
1553 | 0 | } else { |
1554 | | /* If we cannot discover the mode, assume it is a file. */ |
1555 | 0 | is_file = TRUE; |
1556 | 0 | } |
1557 | | |
1558 | 0 | if (is_file == FALSE) { |
1559 | 0 | const char *proto; |
1560 | | |
1561 | | /* Check for upload requests for non-files -- but not if the client is |
1562 | | * using SFTP/SCP. |
1563 | | */ |
1564 | 0 | proto = pr_session_get_protocol(0); |
1565 | 0 | if (strcmp(proto, "sftp") != 0 && |
1566 | 0 | strcmp(proto, "scp") != 0) { |
1567 | 0 | pr_response_add_err(R_550, _("%s: Not a regular file"), cmd->arg); |
1568 | | |
1569 | | /* Deliberately use EISDIR for anything non-file (e.g. directories). */ |
1570 | 0 | pr_cmd_set_errno(cmd, EISDIR); |
1571 | 0 | errno = EISDIR; |
1572 | 0 | return PR_ERROR(cmd); |
1573 | 0 | } |
1574 | 0 | } |
1575 | | |
1576 | | /* If restarting, check permissions on this directory, if |
1577 | | * AllowStoreRestart is set, permit it |
1578 | | */ |
1579 | 0 | allow_restart = get_param_ptr(CURRENT_CONF, "AllowStoreRestart", FALSE); |
1580 | |
|
1581 | 0 | if (fmode && |
1582 | 0 | ((session.restart_pos > 0 || session.range_len > 0) || |
1583 | 0 | (session.xfer.xfer_type == STOR_APPEND)) && |
1584 | 0 | (!allow_restart || *allow_restart == FALSE)) { |
1585 | |
|
1586 | 0 | pr_log_debug(DEBUG6, "AllowStoreRestart denied permission for %s", |
1587 | 0 | cmd->arg); |
1588 | 0 | pr_response_add_err(R_451, _("%s: Append/Restart not permitted, try again"), |
1589 | 0 | cmd->arg); |
1590 | 0 | session.restart_pos = 0L; |
1591 | 0 | session.xfer.xfer_type = STOR_DEFAULT; |
1592 | |
|
1593 | 0 | pr_cmd_set_errno(cmd, EPERM); |
1594 | 0 | errno = EPERM; |
1595 | 0 | return PR_ERROR(cmd); |
1596 | 0 | } |
1597 | | |
1598 | | /* Reject APPE preceded by RANG. */ |
1599 | 0 | if (session.xfer.xfer_type == STOR_APPEND && |
1600 | 0 | session.range_len > 0) { |
1601 | 0 | pr_response_add_err(R_550, _("APPE incompatible with RANG")); |
1602 | 0 | pr_cmd_set_errno(cmd, EPERM); |
1603 | 0 | errno = EPERM; |
1604 | 0 | return PR_ERROR(cmd); |
1605 | 0 | } |
1606 | | |
1607 | | /* If the file exists, add a note indicating that it is being modified. */ |
1608 | 0 | if (fmode) { |
1609 | | /* Clear any existing key in the notes. */ |
1610 | 0 | (void) pr_table_remove(cmd->notes, "mod_xfer.file-modified", NULL); |
1611 | |
|
1612 | 0 | if (pr_table_add(cmd->notes, "mod_xfer.file-modified", |
1613 | 0 | pstrdup(cmd->pool, "true"), 0) < 0) { |
1614 | 0 | if (errno != EEXIST) { |
1615 | 0 | pr_log_pri(PR_LOG_NOTICE, |
1616 | 0 | "notice: error adding 'mod_xfer.file-modified' note: %s", |
1617 | 0 | strerror(errno)); |
1618 | 0 | } |
1619 | 0 | } |
1620 | 0 | } |
1621 | | |
1622 | | /* Otherwise everything is good */ |
1623 | 0 | if (pr_table_add(cmd->notes, "mod_xfer.store-path", |
1624 | 0 | pstrdup(cmd->pool, path), 0) < 0) { |
1625 | 0 | if (errno != EEXIST) { |
1626 | 0 | pr_log_pri(PR_LOG_NOTICE, |
1627 | 0 | "notice: error adding 'mod_xfer.store-path': %s", strerror(errno)); |
1628 | 0 | } |
1629 | 0 | } |
1630 | |
|
1631 | 0 | c = find_config(CURRENT_CONF, CONF_PARAM, "HiddenStores", FALSE); |
1632 | 0 | if (c != NULL && |
1633 | 0 | *((int *) c->argv[0]) == TRUE) { |
1634 | | |
1635 | | /* If we're using HiddenStores, then RANG/REST won't work. */ |
1636 | 0 | if (session.restart_pos > 0 || |
1637 | 0 | session.range_len > 0) { |
1638 | 0 | int used_rest = TRUE; |
1639 | |
|
1640 | 0 | if (session.range_len > 0) { |
1641 | 0 | used_rest = FALSE; |
1642 | 0 | } |
1643 | |
|
1644 | 0 | pr_log_debug(DEBUG9, "HiddenStore in effect, refusing %s upload", |
1645 | 0 | used_rest ? "restarted" : "range"); |
1646 | 0 | pr_response_add_err(R_501, |
1647 | 0 | _("%s not compatible with server configuration"), |
1648 | 0 | used_rest ? C_REST : C_RANG); |
1649 | |
|
1650 | 0 | pr_cmd_set_errno(cmd, EPERM); |
1651 | 0 | errno = EPERM; |
1652 | 0 | return PR_ERROR(cmd); |
1653 | 0 | } |
1654 | | |
1655 | | /* For Bug#3598, we rejected any APPE command when HiddenStores are in |
1656 | | * effect (for good reasons). |
1657 | | * |
1658 | | * However, for Bug#4144, we're relaxing that policy. Instead of rejecting |
1659 | | * the APPE command, we accept that command, but we disable the HiddenStores |
1660 | | * functionality. |
1661 | | */ |
1662 | 0 | if (session.xfer.xfer_type != STOR_APPEND) { |
1663 | 0 | const char *prefix, *suffix; |
1664 | |
|
1665 | 0 | prefix = c->argv[1]; |
1666 | 0 | suffix = c->argv[2]; |
1667 | | |
1668 | | /* Substitute the %P variable for the PID, if present. */ |
1669 | 0 | if (strstr(prefix, "%P") != NULL) { |
1670 | 0 | char pid_buf[32]; |
1671 | |
|
1672 | 0 | memset(pid_buf, '\0', sizeof(pid_buf)); |
1673 | 0 | pr_snprintf(pid_buf, sizeof(pid_buf)-1, "%lu", |
1674 | 0 | (unsigned long) session.pid); |
1675 | 0 | prefix = sreplace(cmd->pool, prefix, "%P", pid_buf, NULL); |
1676 | 0 | } |
1677 | |
|
1678 | 0 | if (strstr(suffix, "%P") != NULL) { |
1679 | 0 | char pid_buf[32]; |
1680 | |
|
1681 | 0 | memset(pid_buf, '\0', sizeof(pid_buf)); |
1682 | 0 | pr_snprintf(pid_buf, sizeof(pid_buf)-1, "%lu", |
1683 | 0 | (unsigned long) session.pid); |
1684 | 0 | suffix = sreplace(cmd->pool, suffix, "%P", pid_buf, NULL); |
1685 | 0 | } |
1686 | |
|
1687 | 0 | if (get_hidden_store_path(cmd, path, prefix, suffix) < 0) { |
1688 | 0 | int xerrno = errno; |
1689 | |
|
1690 | 0 | pr_cmd_set_errno(cmd, xerrno); |
1691 | 0 | errno = xerrno; |
1692 | 0 | return PR_ERROR(cmd); |
1693 | 0 | } |
1694 | |
|
1695 | 0 | } else { |
1696 | 0 | pr_log_debug(DEBUG9, |
1697 | 0 | "HiddenStores in effect for APPE, ignoring HiddenStores"); |
1698 | 0 | } |
1699 | 0 | } |
1700 | | |
1701 | 0 | return PR_HANDLED(cmd); |
1702 | 0 | } |
1703 | | |
1704 | | /* xfer_pre_stou() is a PRE_CMD handler that changes the uploaded filename |
1705 | | * to a unique one, after making the requisite security and authorization |
1706 | | * checks. |
1707 | | */ |
1708 | 0 | MODRET xfer_pre_stou(cmd_rec *cmd) { |
1709 | 0 | config_rec *c = NULL; |
1710 | 0 | char *prefix = "ftp", *filename = NULL; |
1711 | 0 | int stou_fd; |
1712 | 0 | mode_t mode; |
1713 | 0 | unsigned char *allow_overwrite = NULL; |
1714 | |
|
1715 | 0 | session.xfer.xfer_type = STOR_DEFAULT; |
1716 | | |
1717 | | /* Some FTP clients are "broken" in that they will send a filename |
1718 | | * along with STOU. Technically this violates RFC959, but for now, just |
1719 | | * ignore that filename. Stupid client implementers. |
1720 | | */ |
1721 | |
|
1722 | 0 | if (cmd->argc > 2) { |
1723 | 0 | pr_response_add_err(R_500, _("'%s' not understood"), |
1724 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
1725 | |
|
1726 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
1727 | 0 | errno = EINVAL; |
1728 | 0 | return PR_ERROR(cmd); |
1729 | 0 | } |
1730 | | |
1731 | 0 | if (xfer_check_limit(cmd) < 0) { |
1732 | 0 | pr_response_add_err(R_451, _("%s: Too many transfers"), cmd->arg); |
1733 | |
|
1734 | 0 | pr_cmd_set_errno(cmd, EPERM); |
1735 | 0 | errno = EPERM; |
1736 | 0 | return PR_ERROR(cmd); |
1737 | 0 | } |
1738 | | |
1739 | | /* Watch for STOU preceded by REST, which makes no sense. Similarly |
1740 | | * for STOU preceded by RANG. |
1741 | | */ |
1742 | 0 | if (session.restart_pos > 0 || |
1743 | 0 | session.range_len > 0) { |
1744 | |
|
1745 | 0 | if (session.restart_pos > 0) { |
1746 | 0 | pr_response_add_err(R_550, _("STOU incompatible with REST")); |
1747 | |
|
1748 | 0 | } else { |
1749 | 0 | pr_response_add_err(R_550, _("STOU incompatible with RANG")); |
1750 | 0 | } |
1751 | |
|
1752 | 0 | pr_cmd_set_errno(cmd, EPERM); |
1753 | 0 | errno = EPERM; |
1754 | 0 | return PR_ERROR(cmd); |
1755 | 0 | } |
1756 | | |
1757 | | /* Generate the filename to be stored, depending on the configured |
1758 | | * unique filename prefix. |
1759 | | */ |
1760 | 0 | c = find_config(CURRENT_CONF, CONF_PARAM, "StoreUniquePrefix", FALSE); |
1761 | 0 | if (c != NULL) { |
1762 | 0 | prefix = c->argv[0]; |
1763 | 0 | } |
1764 | | |
1765 | | /* Now, construct the unique filename using the cmd_rec's pool, the |
1766 | | * prefix, and mkstemp(). |
1767 | | */ |
1768 | 0 | filename = pstrcat(cmd->pool, prefix, "XXXXXX", NULL); |
1769 | |
|
1770 | 0 | stou_fd = mkstemp(filename); |
1771 | 0 | if (stou_fd < 0) { |
1772 | 0 | int xerrno = errno; |
1773 | |
|
1774 | 0 | pr_log_pri(PR_LOG_WARNING, "error: unable to use mkstemp(): %s", |
1775 | 0 | strerror(xerrno)); |
1776 | | |
1777 | | /* If we can't guarantee a unique filename, refuse the command. */ |
1778 | 0 | pr_response_add_err(R_450, _("%s: unable to generate unique filename"), |
1779 | 0 | (char *) cmd->argv[0]); |
1780 | |
|
1781 | 0 | pr_cmd_set_errno(cmd, xerrno); |
1782 | 0 | errno = xerrno; |
1783 | 0 | return PR_ERROR(cmd); |
1784 | 0 | } |
1785 | | |
1786 | 0 | cmd->arg = filename; |
1787 | | |
1788 | | /* Close the unique file. This introduces a small race condition |
1789 | | * between the time this function returns, and the STOU CMD handler |
1790 | | * opens the unique file, but this may have to do, as closing that |
1791 | | * race would involve some major restructuring. |
1792 | | */ |
1793 | 0 | (void) close(stou_fd); |
1794 | |
|
1795 | 0 | filename = dir_best_path(cmd->tmp_pool, cmd->arg); |
1796 | |
|
1797 | 0 | if (filename == NULL || |
1798 | 0 | !dir_check(cmd->tmp_pool, cmd, cmd->group, filename, NULL)) { |
1799 | 0 | int xerrno = errno; |
1800 | | |
1801 | | /* Do not forget to delete the file created by mkstemp(3) if there is |
1802 | | * an error. |
1803 | | */ |
1804 | 0 | (void) pr_fsio_unlink(cmd->arg); |
1805 | 0 | pr_log_debug(DEBUG8, "%s %s denied by <Limit> configuration", |
1806 | 0 | (char *) cmd->argv[0], cmd->arg); |
1807 | 0 | pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
1808 | |
|
1809 | 0 | pr_cmd_set_errno(cmd, xerrno); |
1810 | 0 | errno = xerrno; |
1811 | 0 | return PR_ERROR(cmd); |
1812 | 0 | } |
1813 | | |
1814 | 0 | mode = file_mode2(cmd->tmp_pool, filename); |
1815 | | |
1816 | | /* Note: this case should never happen: how one can be appending to |
1817 | | * a supposedly unique filename? Should probably be removed... |
1818 | | */ |
1819 | 0 | allow_overwrite = get_param_ptr(CURRENT_CONF, "AllowOverwrite", FALSE); |
1820 | |
|
1821 | 0 | if (mode && session.xfer.xfer_type != STOR_APPEND && |
1822 | 0 | (!allow_overwrite || *allow_overwrite == FALSE)) { |
1823 | 0 | pr_log_debug(DEBUG6, "AllowOverwrite denied permission for %s", cmd->arg); |
1824 | 0 | pr_response_add_err(R_550, _("%s: Overwrite permission denied"), cmd->arg); |
1825 | |
|
1826 | 0 | pr_cmd_set_errno(cmd, EACCES); |
1827 | 0 | errno = EACCES; |
1828 | 0 | return PR_ERROR(cmd); |
1829 | 0 | } |
1830 | | |
1831 | | /* Not likely to _not_ be a regular file, but just to be certain... */ |
1832 | 0 | if (mode && |
1833 | 0 | !S_ISREG(mode)) { |
1834 | 0 | (void) pr_fsio_unlink(cmd->arg); |
1835 | 0 | pr_response_add_err(R_550, _("%s: Not a regular file"), cmd->arg); |
1836 | | |
1837 | | /* Deliberately use EISDIR for anything non-file (e.g. directories). */ |
1838 | 0 | pr_cmd_set_errno(cmd, EISDIR); |
1839 | 0 | errno = EISDIR; |
1840 | 0 | return PR_ERROR(cmd); |
1841 | 0 | } |
1842 | | |
1843 | | /* Otherwise everything is good */ |
1844 | 0 | if (pr_table_add(cmd->notes, "mod_xfer.store-path", |
1845 | 0 | pstrdup(cmd->pool, filename), 0) < 0) { |
1846 | 0 | if (errno != EEXIST) { |
1847 | 0 | pr_log_pri(PR_LOG_NOTICE, |
1848 | 0 | "notice: error adding 'mod_xfer.store-path': %s", strerror(errno)); |
1849 | 0 | } |
1850 | 0 | } |
1851 | |
|
1852 | 0 | session.xfer.xfer_type = STOR_UNIQUE; |
1853 | 0 | return PR_HANDLED(cmd); |
1854 | 0 | } |
1855 | | |
1856 | 0 | MODRET xfer_post_stor(cmd_rec *cmd) { |
1857 | 0 | const char *path; |
1858 | |
|
1859 | 0 | path = pr_table_get(cmd->notes, "mod_xfer.store-path", NULL); |
1860 | 0 | if (path != NULL) { |
1861 | 0 | struct stat st; |
1862 | |
|
1863 | 0 | if (pr_fsio_stat(path, &st) == 0) { |
1864 | 0 | off_t *file_size; |
1865 | |
|
1866 | 0 | file_size = palloc(cmd->pool, sizeof(off_t)); |
1867 | 0 | *file_size = st.st_size; |
1868 | 0 | (void) pr_table_add(cmd->notes, "mod_xfer.file-size", file_size, |
1869 | 0 | sizeof(off_t)); |
1870 | 0 | } |
1871 | 0 | } |
1872 | |
|
1873 | 0 | return PR_DECLINED(cmd); |
1874 | 0 | } |
1875 | | |
1876 | | /* xfer_post_stou() is a POST_CMD handler that changes the mode of the |
1877 | | * STOU file from 0600, which is what mkstemp() makes it, to 0666 (modulo |
1878 | | * Umask), the default for files uploaded via STOR. This is to prevent users |
1879 | | * from being surprised. |
1880 | | */ |
1881 | 0 | MODRET xfer_post_stou(cmd_rec *cmd) { |
1882 | 0 | mode_t mask, perms, *umask_setting; |
1883 | 0 | struct stat st; |
1884 | | |
1885 | | /* mkstemp(3) creates a file with 0600 perms; we need to adjust this |
1886 | | * for the Umask (Bug#4223). |
1887 | | */ |
1888 | 0 | umask_setting = get_param_ptr(CURRENT_CONF, "Umask", FALSE); |
1889 | 0 | if (umask_setting != NULL) { |
1890 | 0 | mask = *umask_setting; |
1891 | |
|
1892 | 0 | } else { |
1893 | 0 | mask = (mode_t) 0022; |
1894 | 0 | } |
1895 | |
|
1896 | 0 | perms = (0666 & ~mask); |
1897 | |
|
1898 | 0 | if (pr_fsio_chmod(cmd->arg, perms) < 0) { |
1899 | | /* Not much to do but log the error. */ |
1900 | 0 | pr_log_pri(PR_LOG_NOTICE, "error: unable to chmod '%s' to %04o: %s", |
1901 | 0 | cmd->arg, perms, strerror(errno)); |
1902 | 0 | } |
1903 | |
|
1904 | 0 | if (pr_fsio_stat(cmd->arg, &st) == 0) { |
1905 | 0 | off_t *file_size; |
1906 | |
|
1907 | 0 | file_size = palloc(cmd->pool, sizeof(off_t)); |
1908 | 0 | *file_size = st.st_size; |
1909 | 0 | (void) pr_table_add(cmd->notes, "mod_xfer.file-size", file_size, |
1910 | 0 | sizeof(off_t)); |
1911 | 0 | } |
1912 | |
|
1913 | 0 | return PR_DECLINED(cmd); |
1914 | 0 | } |
1915 | | |
1916 | | /* xfer_pre_appe() is the PRE_CMD handler for the APPE command, which |
1917 | | * simply sets xfer_type to STOR_APPEND and calls xfer_pre_stor(). |
1918 | | */ |
1919 | 0 | MODRET xfer_pre_appe(cmd_rec *cmd) { |
1920 | 0 | session.xfer.xfer_type = STOR_DEFAULT; |
1921 | |
|
1922 | 0 | if (xfer_check_limit(cmd) < 0) { |
1923 | 0 | pr_response_add_err(R_451, _("%s: Too many transfers"), cmd->arg); |
1924 | |
|
1925 | 0 | pr_cmd_set_errno(cmd, EPERM); |
1926 | 0 | errno = EPERM; |
1927 | 0 | return PR_ERROR(cmd); |
1928 | 0 | } |
1929 | | |
1930 | 0 | session.xfer.xfer_type = STOR_APPEND; |
1931 | 0 | return xfer_pre_stor(cmd); |
1932 | 0 | } |
1933 | | |
1934 | 0 | MODRET xfer_stor(cmd_rec *cmd) { |
1935 | 0 | const char *path; |
1936 | 0 | char *lbuf; |
1937 | 0 | int bufsz, len, xerrno = 0; |
1938 | 0 | off_t nbytes_stored, nbytes_max_store = 0; |
1939 | 0 | unsigned char have_limit = FALSE; |
1940 | 0 | struct stat st; |
1941 | 0 | off_t start_offset = 0, upload_len = 0; |
1942 | 0 | off_t curr_offset, curr_pos = 0; |
1943 | 0 | pr_error_t *err = NULL; |
1944 | |
|
1945 | 0 | memset(&st, 0, sizeof(st)); |
1946 | | |
1947 | | /* Prepare for any potential throttling. */ |
1948 | 0 | pr_throttle_init(cmd); |
1949 | |
|
1950 | 0 | session.xfer.path = pr_table_get(cmd->notes, "mod_xfer.store-path", NULL); |
1951 | 0 | session.xfer.path_hidden = pr_table_get(cmd->notes, |
1952 | 0 | "mod_xfer.store-hidden-path", NULL); |
1953 | |
|
1954 | 0 | path = session.xfer.path; |
1955 | | |
1956 | | /* Make sure the proper current working directory is set in the FSIO |
1957 | | * layer, so that the proper FS can be used for the open(). |
1958 | | */ |
1959 | 0 | pr_fs_setcwd(pr_fs_getcwd()); |
1960 | |
|
1961 | 0 | if (session.xfer.xfer_type == STOR_HIDDEN) { |
1962 | 0 | const void *nfs; |
1963 | 0 | int oflags; |
1964 | |
|
1965 | 0 | oflags = O_WRONLY; |
1966 | |
|
1967 | 0 | if (session.restart_pos == 0) { |
1968 | 0 | oflags |= O_CREAT; |
1969 | 0 | } |
1970 | |
|
1971 | 0 | nfs = pr_table_get(cmd->notes, "mod_xfer.store-hidden-nfs", NULL); |
1972 | 0 | if (nfs == NULL) { |
1973 | 0 | pr_trace_msg("fsio", 9, |
1974 | 0 | "HiddenStores path '%s' is NOT on NFS, using O_EXCL open(2) flags", |
1975 | 0 | session.xfer.path_hidden); |
1976 | 0 | oflags |= O_EXCL; |
1977 | |
|
1978 | 0 | } else { |
1979 | 0 | pr_trace_msg("fsio", 9, |
1980 | 0 | "HiddenStores path '%s' is on NFS, NOT using O_EXCL open(2) flags", |
1981 | 0 | session.xfer.path_hidden); |
1982 | 0 | } |
1983 | |
|
1984 | 0 | stor_fh = pr_fsio_open_with_error(cmd->pool, session.xfer.path_hidden, |
1985 | 0 | oflags, &err); |
1986 | 0 | xerrno = errno; |
1987 | |
|
1988 | 0 | if (stor_fh == NULL) { |
1989 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 5); |
1990 | 0 | pr_error_set_why(err, pstrcat(cmd->pool, "open HiddenStores file '", |
1991 | 0 | session.xfer.path_hidden, "'", NULL)); |
1992 | |
|
1993 | 0 | (void) pr_trace_msg("fileperms", 1, "%s, user '%s' (UID %s, GID %s): " |
1994 | 0 | "error opening '%s': %s", (char *) cmd->argv[0], session.user, |
1995 | 0 | pr_uid2str(cmd->tmp_pool, session.uid), |
1996 | 0 | pr_gid2str(cmd->tmp_pool, session.gid), session.xfer.path_hidden, |
1997 | 0 | strerror(xerrno)); |
1998 | 0 | } |
1999 | |
|
2000 | 0 | } else if (session.xfer.xfer_type == STOR_APPEND) { |
2001 | 0 | stor_fh = pr_fsio_open_with_error(cmd->pool, session.xfer.path, |
2002 | 0 | O_CREAT|O_WRONLY, &err); |
2003 | 0 | xerrno = errno; |
2004 | |
|
2005 | 0 | if (stor_fh != NULL) { |
2006 | 0 | if (pr_fsio_lseek(stor_fh, 0, SEEK_END) == (off_t) -1) { |
2007 | 0 | pr_log_debug(DEBUG4, "unable to seek to end of '%s' for appending: %s", |
2008 | 0 | cmd->arg, strerror(errno)); |
2009 | 0 | (void) pr_fsio_close(stor_fh); |
2010 | 0 | stor_fh = NULL; |
2011 | 0 | } |
2012 | |
|
2013 | 0 | } else { |
2014 | 0 | xerrno = errno; |
2015 | |
|
2016 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 15); |
2017 | 0 | pr_error_set_why(err, pstrcat(cmd->pool, "append to file '", |
2018 | 0 | session.xfer.path, "'", NULL)); |
2019 | |
|
2020 | 0 | (void) pr_trace_msg("fileperms", 1, "%s, user '%s' (UID %s, GID %s): " |
2021 | 0 | "error opening '%s': %s", (char *) cmd->argv[0], session.user, |
2022 | 0 | pr_uid2str(cmd->tmp_pool, session.uid), |
2023 | 0 | pr_gid2str(cmd->tmp_pool, session.gid), session.xfer.path, |
2024 | 0 | strerror(xerrno)); |
2025 | 0 | } |
2026 | |
|
2027 | 0 | } else { |
2028 | 0 | int open_flags = O_WRONLY|O_CREAT; |
2029 | |
|
2030 | 0 | if (session.range_len == 0 && |
2031 | 0 | session.restart_pos == 0) { |
2032 | | /* If we are not resuming an upload or handling a byte range transfer, |
2033 | | * then we should truncate the file to receive the new data. |
2034 | | */ |
2035 | 0 | open_flags |= O_TRUNC; |
2036 | 0 | } |
2037 | | |
2038 | | /* Normal session */ |
2039 | 0 | stor_fh = pr_fsio_open_with_error(cmd->pool, path, open_flags, &err); |
2040 | 0 | xerrno = errno; |
2041 | |
|
2042 | 0 | if (stor_fh == NULL) { |
2043 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 4); |
2044 | 0 | pr_error_set_why(err, pstrcat(cmd->pool, "upload file '", path, "'", |
2045 | 0 | NULL)); |
2046 | |
|
2047 | 0 | (void) pr_trace_msg("fileperms", 1, "%s, user '%s' (UID %s, GID %s): " |
2048 | 0 | "error opening '%s': %s", (char *) cmd->argv[0], session.user, |
2049 | 0 | pr_uid2str(cmd->tmp_pool, session.uid), |
2050 | 0 | pr_gid2str(cmd->tmp_pool, session.gid), path, strerror(xerrno)); |
2051 | 0 | } |
2052 | 0 | } |
2053 | |
|
2054 | 0 | if (session.restart_pos > 0) { |
2055 | 0 | start_offset = session.restart_pos; |
2056 | |
|
2057 | 0 | } else if (session.range_start > 0) { |
2058 | 0 | start_offset = session.range_start; |
2059 | 0 | } |
2060 | |
|
2061 | 0 | if (stor_fh != NULL && |
2062 | 0 | start_offset > 0) { |
2063 | 0 | xerrno = 0; |
2064 | |
|
2065 | 0 | pr_fs_clear_cache2(path); |
2066 | 0 | if (pr_fsio_lseek(stor_fh, start_offset, SEEK_SET) == -1) { |
2067 | 0 | pr_log_debug(DEBUG4, "unable to seek to position %" PR_LU " of '%s': %s", |
2068 | 0 | (pr_off_t) start_offset, cmd->arg, strerror(errno)); |
2069 | 0 | xerrno = errno; |
2070 | |
|
2071 | 0 | } else if (pr_fsio_stat(path, &st) < 0) { |
2072 | 0 | pr_log_debug(DEBUG4, "error checking '%s': %s", cmd->arg, |
2073 | 0 | strerror(errno)); |
2074 | 0 | xerrno = errno; |
2075 | 0 | } |
2076 | |
|
2077 | 0 | if (xerrno) { |
2078 | 0 | (void) pr_fsio_close(stor_fh); |
2079 | 0 | errno = xerrno; |
2080 | 0 | stor_fh = NULL; |
2081 | 0 | } |
2082 | | |
2083 | | /* Make sure that the requested offset is valid (within the size of the |
2084 | | * file being resumed). |
2085 | | */ |
2086 | 0 | if (stor_fh != NULL && |
2087 | 0 | start_offset > st.st_size) { |
2088 | 0 | int used_rest = TRUE; |
2089 | |
|
2090 | 0 | if (session.range_start > 0) { |
2091 | 0 | used_rest = FALSE; |
2092 | 0 | } |
2093 | |
|
2094 | 0 | pr_response_add_err(R_554, _("%s: invalid %s argument"), |
2095 | 0 | used_rest ? C_REST : C_RANG, cmd->arg); |
2096 | 0 | (void) pr_fsio_close(stor_fh); |
2097 | 0 | stor_fh = NULL; |
2098 | |
|
2099 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
2100 | 0 | errno = EINVAL; |
2101 | 0 | return PR_ERROR(cmd); |
2102 | 0 | } |
2103 | | |
2104 | 0 | curr_pos = start_offset; |
2105 | |
|
2106 | 0 | if (session.restart_pos > 0) { |
2107 | 0 | session.restart_pos = 0L; |
2108 | |
|
2109 | 0 | } else if (session.range_start > 0) { |
2110 | 0 | session.range_start = 0; |
2111 | 0 | } |
2112 | 0 | } |
2113 | | |
2114 | 0 | if (stor_fh == NULL) { |
2115 | 0 | if (err != NULL) { |
2116 | 0 | pr_log_debug(DEBUG4, "%s", pr_error_strerror(err, 0)); |
2117 | 0 | pr_error_destroy(err); |
2118 | 0 | err = NULL; |
2119 | |
|
2120 | 0 | } else { |
2121 | 0 | pr_log_debug(DEBUG4, "unable to open '%s' for writing: %s", cmd->arg, |
2122 | 0 | strerror(xerrno)); |
2123 | 0 | } |
2124 | |
|
2125 | 0 | pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
2126 | |
|
2127 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2128 | 0 | errno = xerrno; |
2129 | 0 | return PR_ERROR(cmd); |
2130 | 0 | } |
2131 | | |
2132 | | /* Advise the platform that we will be only writing this file. Note that a |
2133 | | * preceding REST command does not mean we need to use a different offset |
2134 | | * value here; we can/should still tell the platform that the entire file |
2135 | | * should be treated this way. |
2136 | | */ |
2137 | 0 | pr_fs_fadvise(PR_FH_FD(stor_fh), 0, 0, PR_FS_FADVISE_DONTNEED); |
2138 | | |
2139 | | /* Stash the offset at which we're writing to this file. */ |
2140 | 0 | curr_offset = pr_fsio_lseek(stor_fh, (off_t) 0, SEEK_CUR); |
2141 | 0 | if (curr_offset != (off_t) -1) { |
2142 | 0 | off_t *file_offset; |
2143 | |
|
2144 | 0 | file_offset = palloc(cmd->pool, sizeof(off_t)); |
2145 | 0 | *file_offset = (off_t) curr_offset; |
2146 | 0 | (void) pr_table_add(cmd->notes, "mod_xfer.file-offset", file_offset, |
2147 | 0 | sizeof(off_t)); |
2148 | 0 | } |
2149 | | |
2150 | | /* Get the latest stats on the file. If the file already existed, we |
2151 | | * want to know its current size. |
2152 | | */ |
2153 | 0 | (void) pr_fsio_fstat(stor_fh, &st); |
2154 | | |
2155 | | /* Block any timers for this section, where we want to prepare the |
2156 | | * data connection, then need to reprovision the session.xfer struct, |
2157 | | * and do NOT want timers (which may want/need that session.xfer data) |
2158 | | * to fire until after the reprovisioning (Bug#4168). |
2159 | | */ |
2160 | 0 | pr_alarms_block(); |
2161 | | |
2162 | | /* Perform the actual transfer now */ |
2163 | 0 | pr_data_init(cmd->arg, PR_NETIO_IO_RD); |
2164 | | |
2165 | | /* Note that we have to re-populate the session.xfer variables here, |
2166 | | * AFTER the pr_data_init() call. pr_data_init() ensures that there is |
2167 | | * no leftover information in session.xfer, as from aborted transfers. |
2168 | | */ |
2169 | 0 | session.xfer.path = pr_table_get(cmd->notes, "mod_xfer.store-path", NULL); |
2170 | 0 | session.xfer.path_hidden = pr_table_get(cmd->notes, |
2171 | 0 | "mod_xfer.store-hidden-path", NULL); |
2172 | 0 | session.xfer.file_size = curr_pos; |
2173 | |
|
2174 | 0 | pr_alarms_unblock(); |
2175 | | |
2176 | | /* First, make sure the uploaded file has the requested ownership. */ |
2177 | 0 | stor_chown(cmd->tmp_pool); |
2178 | |
|
2179 | 0 | if (session.range_len > 0) { |
2180 | 0 | upload_len = session.range_len; |
2181 | 0 | } |
2182 | |
|
2183 | 0 | if (pr_data_open(cmd->arg, NULL, PR_NETIO_IO_RD, upload_len) < 0) { |
2184 | 0 | xerrno = errno; |
2185 | |
|
2186 | 0 | stor_abort(cmd->pool); |
2187 | 0 | pr_data_abort(0, TRUE); |
2188 | |
|
2189 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2190 | 0 | errno = xerrno; |
2191 | 0 | return PR_ERROR(cmd); |
2192 | 0 | } |
2193 | | |
2194 | | /* Initialize the number of bytes stored */ |
2195 | 0 | nbytes_stored = 0; |
2196 | | |
2197 | | /* Retrieve the number of bytes to store, maximum, if present. |
2198 | | * This check is needed during the pr_data_xfer() loop, below, because |
2199 | | * the size of the file being uploaded isn't known in advance |
2200 | | */ |
2201 | 0 | nbytes_max_store = find_max_nbytes("MaxStoreFileSize"); |
2202 | 0 | if (nbytes_max_store == 0UL) { |
2203 | 0 | have_limit = FALSE; |
2204 | |
|
2205 | 0 | } else { |
2206 | 0 | have_limit = TRUE; |
2207 | 0 | } |
2208 | |
|
2209 | 0 | bufsz = pr_config_get_server_xfer_bufsz(PR_NETIO_IO_RD); |
2210 | 0 | lbuf = (char *) palloc(cmd->tmp_pool, bufsz); |
2211 | 0 | pr_trace_msg("data", 8, "allocated upload buffer of %lu bytes", |
2212 | 0 | (unsigned long) bufsz); |
2213 | |
|
2214 | 0 | while ((len = pr_data_xfer(lbuf, bufsz)) > 0) { |
2215 | 0 | int res; |
2216 | |
|
2217 | 0 | pr_signals_handle(); |
2218 | |
|
2219 | 0 | if (XFER_ABORTED) { |
2220 | 0 | break; |
2221 | 0 | } |
2222 | | |
2223 | 0 | nbytes_stored += len; |
2224 | | |
2225 | | /* If MaxStoreFileSize is configured, double-check the number of bytes |
2226 | | * uploaded so far against the configured limit. Also make sure that |
2227 | | * we take into account the size of the file, i.e. if it already existed. |
2228 | | */ |
2229 | 0 | if (have_limit && |
2230 | 0 | (nbytes_stored + st.st_size > nbytes_max_store)) { |
2231 | 0 | pr_log_pri(PR_LOG_NOTICE, "MaxStoreFileSize (%" PR_LU " bytes) reached: " |
2232 | 0 | "aborting transfer of '%s'", (pr_off_t) nbytes_max_store, path); |
2233 | | |
2234 | | /* Abort the transfer. */ |
2235 | 0 | stor_abort(cmd->pool); |
2236 | | |
2237 | | /* Set errno to EFBIG (or the most appropriate alternative). */ |
2238 | 0 | #if defined(EFBIG) |
2239 | 0 | xerrno = EFBIG; |
2240 | | #elif defined(EDQUOT) |
2241 | | xerrno = EDQUOT; |
2242 | | #else |
2243 | | xerrno = EPERM; |
2244 | | #endif |
2245 | |
|
2246 | 0 | pr_data_abort(xerrno, FALSE); |
2247 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2248 | 0 | errno = xerrno; |
2249 | 0 | return PR_ERROR(cmd); |
2250 | 0 | } |
2251 | | |
2252 | | /* XXX Need to handle short writes better here. It is possible that |
2253 | | * the underlying filesystem (e.g. a network-mounted filesystem) could |
2254 | | * be doing short writes, and we ideally should be more resilient/graceful |
2255 | | * in the face of such things. |
2256 | | */ |
2257 | 0 | res = pr_fsio_write_with_error(cmd->pool, stor_fh, lbuf, len, &err); |
2258 | 0 | xerrno = errno; |
2259 | |
|
2260 | 0 | while (res < 0 && |
2261 | 0 | xerrno == EINTR) { |
2262 | | /* Interrupted by signal; handle it, and try again. */ |
2263 | 0 | errno = EINTR; |
2264 | 0 | pr_signals_handle(); |
2265 | |
|
2266 | 0 | res = pr_fsio_write_with_error(cmd->pool, stor_fh, lbuf, len, &err); |
2267 | 0 | xerrno = errno; |
2268 | 0 | } |
2269 | |
|
2270 | 0 | if (res != len) { |
2271 | 0 | xerrno = EIO; |
2272 | |
|
2273 | 0 | if (res < 0) { |
2274 | 0 | xerrno = errno; |
2275 | |
|
2276 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 9); |
2277 | 0 | pr_error_set_why(err, pstrcat(cmd->pool, "writing '", stor_fh->fh_path, |
2278 | 0 | "'", NULL)); |
2279 | 0 | } |
2280 | |
|
2281 | 0 | (void) pr_trace_msg("fileperms", 1, "%s, user '%s' (UID %s, GID %s): " |
2282 | 0 | "error writing to '%s': %s", (char *) cmd->argv[0], session.user, |
2283 | 0 | pr_uid2str(cmd->tmp_pool, session.uid), |
2284 | 0 | pr_gid2str(cmd->tmp_pool, session.gid), stor_fh->fh_path, |
2285 | 0 | strerror(xerrno)); |
2286 | |
|
2287 | 0 | if (err != NULL) { |
2288 | 0 | pr_log_debug(DEBUG9, "%s", pr_error_strerror(err, 0)); |
2289 | 0 | pr_error_destroy(err); |
2290 | 0 | err = NULL; |
2291 | 0 | } |
2292 | |
|
2293 | 0 | stor_abort(cmd->pool); |
2294 | 0 | pr_data_abort(xerrno, FALSE); |
2295 | |
|
2296 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2297 | 0 | errno = xerrno; |
2298 | 0 | return PR_ERROR(cmd); |
2299 | 0 | } |
2300 | | |
2301 | | /* If no throttling is configured, this does nothing. */ |
2302 | 0 | pr_throttle_pause(nbytes_stored, FALSE, nbytes_stored); |
2303 | |
|
2304 | 0 | if (session.range_len > 0) { |
2305 | 0 | if (nbytes_stored == upload_len) { |
2306 | 0 | break; |
2307 | 0 | } |
2308 | | |
2309 | 0 | if (nbytes_stored > upload_len) { |
2310 | 0 | xerrno = EPERM; |
2311 | |
|
2312 | 0 | pr_log_pri(PR_LOG_NOTICE, "Transfer range length (%" PR_LU |
2313 | 0 | " %s) exceeded; aborting transfer of '%s'", (pr_off_t) upload_len, |
2314 | 0 | upload_len != 1 ? "bytes" : "byte", path); |
2315 | | |
2316 | | /* Abort the transfer. */ |
2317 | 0 | stor_abort(cmd->pool); |
2318 | |
|
2319 | 0 | pr_data_abort(xerrno, FALSE); |
2320 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2321 | 0 | errno = xerrno; |
2322 | 0 | return PR_ERROR(cmd); |
2323 | 0 | } |
2324 | 0 | } |
2325 | 0 | } |
2326 | | |
2327 | 0 | if (XFER_ABORTED) { |
2328 | 0 | stor_abort(cmd->pool); |
2329 | 0 | pr_data_abort(0, FALSE); |
2330 | |
|
2331 | 0 | pr_cmd_set_errno(cmd, EIO); |
2332 | 0 | errno = EIO; |
2333 | 0 | return PR_ERROR(cmd); |
2334 | 0 | } |
2335 | | |
2336 | 0 | if (len < 0) { |
2337 | | /* Default abort errno, in case session.d et al has already gone away */ |
2338 | 0 | xerrno = ECONNABORTED; |
2339 | |
|
2340 | 0 | stor_abort(cmd->pool); |
2341 | |
|
2342 | 0 | if (session.d != NULL && |
2343 | 0 | session.d->instrm != NULL) { |
2344 | 0 | xerrno = PR_NETIO_ERRNO(session.d->instrm); |
2345 | 0 | } |
2346 | |
|
2347 | 0 | pr_data_abort(xerrno, FALSE); |
2348 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2349 | 0 | errno = xerrno; |
2350 | 0 | return PR_ERROR(cmd); |
2351 | 0 | } |
2352 | | |
2353 | | /* Did we receive all of the expected bytes in a range? */ |
2354 | 0 | if (session.range_len > 0 && |
2355 | 0 | nbytes_stored < upload_len) { |
2356 | 0 | xerrno = EPERM; |
2357 | |
|
2358 | 0 | pr_log_pri(PR_LOG_NOTICE, "Transfer range length (%" PR_LU |
2359 | 0 | " %s) not provided; aborting transfer of '%s'", (pr_off_t) upload_len, |
2360 | 0 | upload_len != 1 ? "bytes" : "byte", path); |
2361 | | |
2362 | | /* Abort the transfer. */ |
2363 | 0 | stor_abort(cmd->pool); |
2364 | |
|
2365 | 0 | pr_data_abort(xerrno, FALSE); |
2366 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2367 | 0 | errno = xerrno; |
2368 | 0 | return PR_ERROR(cmd); |
2369 | 0 | } |
2370 | | |
2371 | | /* If no throttling is configured, this does nothing. */ |
2372 | 0 | pr_throttle_pause(nbytes_stored, TRUE, nbytes_stored); |
2373 | |
|
2374 | 0 | if (stor_complete(cmd->pool) < 0) { |
2375 | 0 | xerrno = errno; |
2376 | |
|
2377 | 0 | _log_transfer('i', 'i'); |
2378 | | |
2379 | | /* Check errno for EDQOUT (or the most appropriate alternative). |
2380 | | * (I hate the fact that FTP has a special response code just for |
2381 | | * this, and that clients actually expect it. Special cases are |
2382 | | * stupid.) |
2383 | | */ |
2384 | 0 | #if defined(EDQUOT) |
2385 | 0 | if (xerrno == EDQUOT) { |
2386 | 0 | pr_response_add_err(R_552, "%s: %s", cmd->arg, strerror(xerrno)); |
2387 | |
|
2388 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2389 | 0 | errno = xerrno; |
2390 | 0 | return PR_ERROR(cmd); |
2391 | 0 | } |
2392 | | #elif defined(EFBIG) |
2393 | | if (xerrno == EFBIG) { |
2394 | | pr_response_add_err(R_552, "%s: %s", cmd->arg, strerror(xerrno)); |
2395 | | |
2396 | | pr_cmd_set_errno(cmd, xerrno); |
2397 | | errno = xerrno; |
2398 | | return PR_ERROR(cmd); |
2399 | | } |
2400 | | #endif |
2401 | | |
2402 | 0 | pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
2403 | |
|
2404 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2405 | 0 | errno = xerrno; |
2406 | 0 | return PR_ERROR(cmd); |
2407 | 0 | } |
2408 | | |
2409 | 0 | if (session.xfer.path && |
2410 | 0 | session.xfer.path_hidden) { |
2411 | 0 | if (pr_fsio_rename(session.xfer.path_hidden, session.xfer.path) < 0) { |
2412 | 0 | xerrno = errno; |
2413 | | |
2414 | | /* This should only fail on a race condition with a chmod/chown or if |
2415 | | * STOR_APPEND is on and the permissions are squirrely. The poor user |
2416 | | * will have to re-upload, but we've got more important problems to worry |
2417 | | * about and this failure should be fairly rare. |
2418 | | */ |
2419 | 0 | pr_log_pri(PR_LOG_WARNING, "Rename of %s to %s failed: %s.", |
2420 | 0 | session.xfer.path_hidden, session.xfer.path, strerror(xerrno)); |
2421 | |
|
2422 | 0 | pr_response_add_err(R_550, _("%s: Rename of hidden file %s failed: %s"), |
2423 | 0 | session.xfer.path, session.xfer.path_hidden, strerror(xerrno)); |
2424 | |
|
2425 | 0 | if (pr_fsio_unlink(session.xfer.path_hidden) < 0) { |
2426 | 0 | if (errno != ENOENT) { |
2427 | 0 | pr_log_debug(DEBUG0, "failed to delete HiddenStores file '%s': %s", |
2428 | 0 | session.xfer.path_hidden, strerror(errno)); |
2429 | 0 | } |
2430 | 0 | } |
2431 | |
|
2432 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2433 | 0 | errno = xerrno; |
2434 | 0 | return PR_ERROR(cmd); |
2435 | 0 | } |
2436 | | |
2437 | | /* One way or another, we've dealt with the HiddenStores file. */ |
2438 | 0 | session.xfer.path_hidden = NULL; |
2439 | 0 | } |
2440 | | |
2441 | 0 | xfer_displayfile(); |
2442 | 0 | pr_data_close2(); |
2443 | 0 | pr_response_add(R_226, _("Transfer complete")); |
2444 | |
|
2445 | 0 | return PR_HANDLED(cmd); |
2446 | 0 | } |
2447 | | |
2448 | | /* Should this become part of the String API? */ |
2449 | 0 | static int parse_offset(char *str, off_t *num) { |
2450 | 0 | int res = 0; |
2451 | 0 | char *ptr, *tmp = NULL; |
2452 | 0 | unsigned long n; |
2453 | 0 | off_t sz; |
2454 | | |
2455 | | /* Don't allow negative numbers. strtoul()/strtoull() will silently |
2456 | | * handle them. |
2457 | | */ |
2458 | 0 | ptr = str; |
2459 | 0 | if (*ptr == '-') { |
2460 | 0 | errno = EINVAL; |
2461 | 0 | return -1; |
2462 | 0 | } |
2463 | | |
2464 | 0 | errno = 0; |
2465 | |
|
2466 | 0 | #if defined(HAVE_STRTOULL) |
2467 | 0 | n = strtoull(ptr, &tmp, 10); |
2468 | 0 | if (n == ULLONG_MAX && |
2469 | 0 | errno == ERANGE) { |
2470 | 0 | res = -1; |
2471 | 0 | } |
2472 | | #else |
2473 | | n = strtoul(ptr, &tmp, 10); |
2474 | | if (n == ULONG_MAX && |
2475 | | errno == ERANGE) { |
2476 | | res = -1; |
2477 | | } |
2478 | | #endif /* HAVE_STRTOULL */ |
2479 | |
|
2480 | 0 | if (res < 0) { |
2481 | 0 | pr_trace_msg(trace_channel, 3, |
2482 | 0 | "client-sent offset '%s' conversion error: %s", ptr, strerror(errno)); |
2483 | 0 | errno = EINVAL; |
2484 | 0 | return -1; |
2485 | 0 | } |
2486 | | |
2487 | 0 | if (tmp && *tmp) { |
2488 | 0 | errno = EINVAL; |
2489 | 0 | return -1; |
2490 | 0 | } |
2491 | | |
2492 | | /* Watch for cases where the sent file offset might overflow our size |
2493 | | * type (Issue #2286). |
2494 | | */ |
2495 | 0 | sz = n; |
2496 | 0 | if (sz < 0) { |
2497 | 0 | pr_trace_msg(trace_channel, 3, |
2498 | 0 | "client-sent offset '%s' out of range, rejecting", ptr); |
2499 | 0 | errno = EINVAL; |
2500 | 0 | return -1; |
2501 | 0 | } |
2502 | | |
2503 | 0 | *num = sz; |
2504 | 0 | return 0; |
2505 | 0 | } |
2506 | | |
2507 | 0 | MODRET xfer_rest(cmd_rec *cmd) { |
2508 | 0 | int res; |
2509 | 0 | off_t pos = 0; |
2510 | |
|
2511 | 0 | if (cmd->argc != 2) { |
2512 | 0 | pr_response_add_err(R_500, _("'%s' not understood"), |
2513 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
2514 | |
|
2515 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
2516 | 0 | errno = EINVAL; |
2517 | 0 | return PR_ERROR(cmd); |
2518 | 0 | } |
2519 | | |
2520 | 0 | res = parse_offset(cmd->argv[1], &pos); |
2521 | 0 | if (res < 0) { |
2522 | 0 | int xerrno = errno; |
2523 | |
|
2524 | 0 | pr_response_add_err(R_501, |
2525 | 0 | _("REST requires a value greater than or equal to 0")); |
2526 | |
|
2527 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2528 | 0 | errno = xerrno; |
2529 | 0 | return PR_ERROR(cmd); |
2530 | 0 | } |
2531 | | |
2532 | | /* Refuse the command if we're in ASCII mode, and the restart position |
2533 | | * is anything other than zero. |
2534 | | * |
2535 | | * Ideally, we would refuse the REST command when in ASCII mode regardless |
2536 | | * of position. However, some (IMHO, stupid) clients "test" the FTP |
2537 | | * server by sending "REST 0" to see if the server supports REST, without |
2538 | | * regard to the transfer type. This, then, is a hack to handle such |
2539 | | * clients. |
2540 | | */ |
2541 | 0 | if ((session.sf_flags & SF_ASCII) && |
2542 | 0 | pos != 0 && |
2543 | 0 | !(xfer_opts & PR_XFER_OPT_IGNORE_ASCII)) { |
2544 | 0 | pr_log_debug(DEBUG5, "%s not allowed in ASCII mode", (char *) cmd->argv[0]); |
2545 | 0 | pr_response_add_err(R_501, |
2546 | 0 | _("%s: Resuming transfers not allowed in ASCII mode"), |
2547 | 0 | (char *) cmd->argv[0]); |
2548 | |
|
2549 | 0 | pr_cmd_set_errno(cmd, EPERM); |
2550 | 0 | errno = EPERM; |
2551 | 0 | return PR_ERROR(cmd); |
2552 | 0 | } |
2553 | | |
2554 | 0 | session.restart_pos = pos; |
2555 | | |
2556 | | /* We can honor REST, or RANG, but not both at the same time. */ |
2557 | 0 | session.range_start = session.range_len = 0; |
2558 | |
|
2559 | 0 | pr_response_add(R_350, _("Restarting at %" PR_LU |
2560 | 0 | ". Send STORE or RETRIEVE to initiate transfer"), (pr_off_t) pos); |
2561 | 0 | return PR_HANDLED(cmd); |
2562 | 0 | } |
2563 | | |
2564 | 0 | MODRET xfer_rang(cmd_rec *cmd) { |
2565 | 0 | int res; |
2566 | 0 | off_t range_start, range_end; |
2567 | |
|
2568 | 0 | if (cmd->argc != 3) { |
2569 | 0 | pr_response_add_err(R_500, _("'%s' not understood"), |
2570 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
2571 | |
|
2572 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
2573 | 0 | errno = EINVAL; |
2574 | 0 | return PR_ERROR(cmd); |
2575 | 0 | } |
2576 | | |
2577 | 0 | if (!dir_check(cmd->tmp_pool, cmd, cmd->group, session.cwd, NULL)) { |
2578 | 0 | int xerrno = EPERM; |
2579 | |
|
2580 | 0 | pr_log_debug(DEBUG8, "RANG denied by <Limit> configuration"); |
2581 | 0 | pr_response_add_err(R_552, "%s: %s", (char *) cmd->argv[0], |
2582 | 0 | strerror(xerrno)); |
2583 | |
|
2584 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2585 | 0 | errno = xerrno; |
2586 | 0 | return PR_ERROR(cmd); |
2587 | 0 | } |
2588 | | |
2589 | 0 | res = parse_offset(cmd->argv[1], &range_start); |
2590 | 0 | if (res < 0) { |
2591 | 0 | int xerrno = errno; |
2592 | |
|
2593 | 0 | pr_response_add_err(R_501, |
2594 | 0 | _("RANG requires a value greater than or equal to 0")); |
2595 | |
|
2596 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2597 | 0 | errno = xerrno; |
2598 | 0 | return PR_ERROR(cmd); |
2599 | 0 | } |
2600 | | |
2601 | 0 | res = parse_offset(cmd->argv[2], &range_end); |
2602 | 0 | if (res < 0) { |
2603 | 0 | int xerrno = errno; |
2604 | |
|
2605 | 0 | pr_response_add_err(R_501, |
2606 | 0 | _("RANG requires a value greater than or equal to 0")); |
2607 | |
|
2608 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2609 | 0 | errno = xerrno; |
2610 | 0 | return PR_ERROR(cmd); |
2611 | 0 | } |
2612 | | |
2613 | 0 | if (range_start > range_end) { |
2614 | | /* Per Draft, such ranges will automatically reset the range. */ |
2615 | 0 | session.range_start = session.range_len = 0; |
2616 | | |
2617 | | /* Iff start = 1 AND end = 0, then this is the acceptable way to reset |
2618 | | * the range. Otherwise, it is an error. |
2619 | | */ |
2620 | 0 | if (range_start == 1 && |
2621 | 0 | range_end == 0) { |
2622 | 0 | pr_response_add(R_350, _("Reset byte transfer range")); |
2623 | 0 | return PR_HANDLED(cmd); |
2624 | 0 | } |
2625 | | |
2626 | 0 | pr_log_debug(DEBUG9, "rejecting RANG: start %" PR_LU " > end %" PR_LU, |
2627 | 0 | (pr_off_t) range_start, (pr_off_t) range_end); |
2628 | 0 | pr_response_add_err(R_501, _("RANG start must be less than end")); |
2629 | |
|
2630 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
2631 | 0 | errno = EINVAL; |
2632 | 0 | return PR_ERROR(cmd); |
2633 | 0 | } |
2634 | | |
2635 | | /* Per Draft, refuse (with 551) if we are not in IMAGE type, STREAM mode. */ |
2636 | 0 | if (session.sf_flags & SF_ASCII) { |
2637 | 0 | pr_log_debug(DEBUG5, "%s not allowed in ASCII mode", (char *) cmd->argv[0]); |
2638 | 0 | pr_response_add_err(R_551, |
2639 | 0 | _("%s: Transfer ranges not allowed in ASCII mode"), |
2640 | 0 | (char *) cmd->argv[0]); |
2641 | |
|
2642 | 0 | pr_cmd_set_errno(cmd, EPERM); |
2643 | 0 | errno = EPERM; |
2644 | 0 | return PR_ERROR(cmd); |
2645 | 0 | } |
2646 | | |
2647 | | /* Per Draft, the values given are positions, inclusive. Thus |
2648 | | * "RANG 0 1" would be a transfer range of TWO bytes. |
2649 | | * |
2650 | | * For consistency, we store offset+len, rather than start/end positions. |
2651 | | */ |
2652 | 0 | session.range_start = range_start; |
2653 | 0 | session.range_len = (range_end - range_start + 1); |
2654 | | |
2655 | | /* We can honor RANG, or REST, but not both at the same time. */ |
2656 | 0 | session.restart_pos = 0; |
2657 | |
|
2658 | 0 | pr_response_add(R_350, _("Transferring byte range of %" PR_LU |
2659 | 0 | " %s starting from %" PR_LU), (pr_off_t) session.range_len, |
2660 | 0 | session.range_len != 1 ? "bytes" : "byte", (pr_off_t) range_start); |
2661 | 0 | return PR_HANDLED(cmd); |
2662 | 0 | } |
2663 | | |
2664 | | /* This is a PRE_CMD handler that checks security, etc, and places the full |
2665 | | * filename to send in cmd->notes (note that we CANNOT use cmd->tmp_pool |
2666 | | * for this, as tmp_pool only lasts for the duration of this function). |
2667 | | */ |
2668 | 0 | MODRET xfer_pre_retr(cmd_rec *cmd) { |
2669 | 0 | char *decoded_path, *dir = NULL; |
2670 | 0 | mode_t fmode; |
2671 | 0 | unsigned char *allow_restart = NULL; |
2672 | 0 | config_rec *c; |
2673 | |
|
2674 | 0 | xfer_logged_sendfile_decline_msg = FALSE; |
2675 | |
|
2676 | 0 | if (cmd->argc < 2) { |
2677 | 0 | pr_response_add_err(R_500, _("'%s' not understood"), |
2678 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
2679 | |
|
2680 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
2681 | 0 | errno = EINVAL; |
2682 | 0 | return PR_ERROR(cmd); |
2683 | 0 | } |
2684 | | |
2685 | 0 | decoded_path = pr_fs_decode_path2(cmd->tmp_pool, cmd->arg, |
2686 | 0 | FSIO_DECODE_FL_TELL_ERRORS); |
2687 | 0 | if (decoded_path == NULL) { |
2688 | 0 | int xerrno = errno; |
2689 | |
|
2690 | 0 | pr_log_debug(DEBUG8, "'%s' failed to decode properly: %s", cmd->arg, |
2691 | 0 | strerror(xerrno)); |
2692 | 0 | pr_response_add_err(R_550, _("%s: Illegal character sequence in filename"), |
2693 | 0 | cmd->arg); |
2694 | |
|
2695 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2696 | 0 | errno = xerrno; |
2697 | 0 | return PR_ERROR(cmd); |
2698 | 0 | } |
2699 | | |
2700 | 0 | pr_fs_clear_cache2(decoded_path); |
2701 | 0 | dir = dir_realpath(cmd->tmp_pool, decoded_path); |
2702 | 0 | if (dir == NULL) { |
2703 | | /* Try using dir_best_path(), as xfer_pre_stor() does. |
2704 | | * |
2705 | | * Without this fallback, certain use cases (such as SFTP downloads using |
2706 | | * mod_sftp + mod_vroot) fail unexpectedly, with misleading |
2707 | | * "denied by <Limit> configuration" errors. |
2708 | | */ |
2709 | 0 | dir = dir_best_path(cmd->tmp_pool, decoded_path); |
2710 | 0 | } |
2711 | |
|
2712 | 0 | if (dir == NULL || |
2713 | 0 | !dir_check(cmd->tmp_pool, cmd, cmd->group, dir, NULL)) { |
2714 | 0 | int xerrno = errno; |
2715 | |
|
2716 | 0 | pr_log_debug(DEBUG8, "%s %s denied by <Limit> configuration", |
2717 | 0 | (char *) cmd->argv[0], cmd->arg); |
2718 | 0 | pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
2719 | |
|
2720 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2721 | 0 | errno = xerrno; |
2722 | 0 | return PR_ERROR(cmd); |
2723 | 0 | } |
2724 | | |
2725 | | /* Check for UseSendfile. */ |
2726 | 0 | use_sendfile = TRUE; |
2727 | 0 | use_sendfile_len = 0; |
2728 | 0 | use_sendfile_pct = -1.0; |
2729 | |
|
2730 | 0 | c = find_config(CURRENT_CONF, CONF_PARAM, "UseSendfile", FALSE); |
2731 | 0 | if (c != NULL) { |
2732 | 0 | use_sendfile = *((unsigned char *) c->argv[0]); |
2733 | 0 | use_sendfile_len = *((off_t *) c->argv[1]); |
2734 | 0 | use_sendfile_pct = *((float *) c->argv[2]); |
2735 | 0 | } |
2736 | |
|
2737 | 0 | if (xfer_check_limit(cmd) < 0) { |
2738 | 0 | pr_response_add_err(R_451, _("%s: Too many transfers"), cmd->arg); |
2739 | |
|
2740 | 0 | pr_cmd_set_errno(cmd, EPERM); |
2741 | 0 | errno = EPERM; |
2742 | 0 | return PR_ERROR(cmd); |
2743 | 0 | } |
2744 | | |
2745 | 0 | fmode = file_mode2(cmd->tmp_pool, dir); |
2746 | 0 | if (fmode == 0) { |
2747 | 0 | int xerrno = errno; |
2748 | |
|
2749 | 0 | pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
2750 | |
|
2751 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2752 | 0 | errno = xerrno; |
2753 | 0 | return PR_ERROR(cmd); |
2754 | 0 | } |
2755 | | |
2756 | 0 | if (!S_ISREG(fmode) && |
2757 | 0 | !S_ISLNK(fmode) |
2758 | 0 | #ifdef S_ISFIFO |
2759 | 0 | && !S_ISFIFO(fmode) |
2760 | 0 | #endif |
2761 | 0 | ) { |
2762 | 0 | const char *proto; |
2763 | | |
2764 | | /* Check for download requests for non-files -- but not if the client is |
2765 | | * using SFTP/SCP. |
2766 | | */ |
2767 | 0 | proto = pr_session_get_protocol(0); |
2768 | 0 | if (strcmp(proto, "sftp") != 0 && |
2769 | 0 | strcmp(proto, "scp") != 0) { |
2770 | 0 | pr_response_add_err(R_550, _("%s: Not a regular file"), cmd->arg); |
2771 | | |
2772 | | /* Deliberately use EISDIR for anything non-file (e.g. directories). */ |
2773 | 0 | pr_cmd_set_errno(cmd, EISDIR); |
2774 | 0 | errno = EISDIR; |
2775 | 0 | return PR_ERROR(cmd); |
2776 | 0 | } |
2777 | 0 | } |
2778 | | |
2779 | | /* If restart is on, check to see if AllowRestartRetrieve is off, in |
2780 | | * which case we disallow the transfer and clear restart_pos. |
2781 | | */ |
2782 | 0 | allow_restart = get_param_ptr(CURRENT_CONF, "AllowRetrieveRestart", FALSE); |
2783 | |
|
2784 | 0 | if ((session.restart_pos > 0 || session.range_len > 0) && |
2785 | 0 | (allow_restart && *allow_restart == FALSE)) { |
2786 | 0 | pr_response_add_err(R_451, _("%s: Restart not permitted, try again"), |
2787 | 0 | cmd->arg); |
2788 | 0 | session.restart_pos = 0L; |
2789 | |
|
2790 | 0 | pr_cmd_set_errno(cmd, EPERM); |
2791 | 0 | errno = EPERM; |
2792 | 0 | return PR_ERROR(cmd); |
2793 | 0 | } |
2794 | | |
2795 | | /* Otherwise everything is good */ |
2796 | 0 | if (pr_table_add(cmd->notes, "mod_xfer.retr-path", |
2797 | 0 | pstrdup(cmd->pool, dir), 0) < 0) { |
2798 | 0 | if (errno != EEXIST) { |
2799 | 0 | pr_log_pri(PR_LOG_NOTICE, "notice: error adding 'mod_xfer.retr-path': %s", |
2800 | 0 | strerror(errno)); |
2801 | 0 | } |
2802 | 0 | } |
2803 | |
|
2804 | 0 | return PR_HANDLED(cmd); |
2805 | 0 | } |
2806 | | |
2807 | 0 | MODRET xfer_post_retr(cmd_rec *cmd) { |
2808 | 0 | const char *path; |
2809 | |
|
2810 | 0 | path = pr_table_get(cmd->notes, "mod_xfer.retr-path", NULL); |
2811 | 0 | if (path != NULL) { |
2812 | 0 | struct stat st; |
2813 | |
|
2814 | 0 | if (pr_fsio_stat(path, &st) == 0) { |
2815 | 0 | off_t *file_size; |
2816 | |
|
2817 | 0 | file_size = palloc(cmd->pool, sizeof(off_t)); |
2818 | 0 | *file_size = st.st_size; |
2819 | 0 | (void) pr_table_add(cmd->notes, "mod_xfer.file-size", file_size, |
2820 | 0 | sizeof(off_t)); |
2821 | 0 | } |
2822 | 0 | } |
2823 | |
|
2824 | 0 | return PR_DECLINED(cmd); |
2825 | 0 | } |
2826 | | |
2827 | 0 | MODRET xfer_retr(cmd_rec *cmd) { |
2828 | 0 | int xerrno = 0; |
2829 | 0 | const char *dir = NULL; |
2830 | 0 | char *lbuf; |
2831 | 0 | struct stat st; |
2832 | 0 | off_t nbytes_max_retrieve = 0; |
2833 | 0 | unsigned char have_limit = FALSE; |
2834 | 0 | long bufsz, len = 0; |
2835 | 0 | off_t start_offset = 0, download_len = 0; |
2836 | 0 | off_t curr_offset, curr_pos = 0, nbytes_sent = 0, cnt_steps = 0, cnt_next = 0; |
2837 | 0 | pr_error_t *err = NULL; |
2838 | | |
2839 | | /* Prepare for any potential throttling. */ |
2840 | 0 | pr_throttle_init(cmd); |
2841 | |
|
2842 | 0 | dir = pr_table_get(cmd->notes, "mod_xfer.retr-path", NULL); |
2843 | |
|
2844 | 0 | retr_fh = pr_fsio_open_with_error(cmd->pool, dir, O_RDONLY, &err); |
2845 | 0 | xerrno = errno; |
2846 | |
|
2847 | 0 | if (retr_fh == NULL) { |
2848 | 0 | pr_error_set_where(err, &xfer_module, __FILE__, __LINE__ - 4); |
2849 | 0 | pr_error_set_why(err, pstrcat(cmd->pool, "download file '", dir, "'", |
2850 | 0 | NULL)); |
2851 | |
|
2852 | 0 | (void) pr_trace_msg("fileperms", 1, "%s, user '%s' (UID %s, GID %s): " |
2853 | 0 | "error opening '%s': %s", (char *) cmd->argv[0], session.user, |
2854 | 0 | pr_uid2str(cmd->tmp_pool, session.uid), |
2855 | 0 | pr_gid2str(cmd->tmp_pool, session.gid), dir, strerror(xerrno)); |
2856 | |
|
2857 | 0 | if (err != NULL) { |
2858 | 0 | pr_log_debug(DEBUG9, "%s", pr_error_strerror(err, 0)); |
2859 | 0 | pr_error_destroy(err); |
2860 | 0 | err = NULL; |
2861 | 0 | } |
2862 | |
|
2863 | 0 | pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
2864 | |
|
2865 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2866 | 0 | errno = xerrno; |
2867 | 0 | return PR_ERROR(cmd); |
2868 | 0 | } |
2869 | | |
2870 | 0 | if (pr_fsio_fstat(retr_fh, &st) < 0) { |
2871 | | /* Error stat'ing the file. */ |
2872 | 0 | xerrno = errno; |
2873 | 0 | pr_log_debug(DEBUG6, "error checking path '%s': %s", |
2874 | 0 | dir, strerror(xerrno)); |
2875 | |
|
2876 | 0 | pr_fsio_close(retr_fh); |
2877 | 0 | retr_fh = NULL; |
2878 | 0 | pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
2879 | |
|
2880 | 0 | pr_cmd_set_errno(cmd, xerrno); |
2881 | 0 | errno = xerrno; |
2882 | 0 | return PR_ERROR(cmd); |
2883 | 0 | } |
2884 | | |
2885 | | /* Advise the platform that we will be only reading this file |
2886 | | * sequentially. Note that a preceding REST command does not mean we |
2887 | | * need to use a different offset value here; we can/should still |
2888 | | * tell the platform that the entire file should be treated this way. |
2889 | | */ |
2890 | 0 | pr_fs_fadvise(PR_FH_FD(retr_fh), 0, 0, PR_FS_FADVISE_SEQUENTIAL); |
2891 | |
|
2892 | 0 | if (session.restart_pos > 0) { |
2893 | 0 | start_offset = session.restart_pos; |
2894 | |
|
2895 | 0 | } else if (session.range_start > 0) { |
2896 | 0 | start_offset = session.range_start; |
2897 | 0 | } |
2898 | |
|
2899 | 0 | if (start_offset > 0) { |
2900 | 0 | char *offset_cmd; |
2901 | |
|
2902 | 0 | offset_cmd = C_REST; |
2903 | 0 | if (session.range_start > 0) { |
2904 | 0 | offset_cmd = C_RANG; |
2905 | 0 | } |
2906 | | |
2907 | | /* Make sure that the requested offset is valid (within the size of the |
2908 | | * file being resumed). |
2909 | | */ |
2910 | |
|
2911 | 0 | if (start_offset > st.st_size) { |
2912 | 0 | pr_trace_msg(trace_channel, 4, |
2913 | 0 | "%s offset %" PR_LU " exceeds file size (%" PR_LU " bytes)", |
2914 | 0 | offset_cmd, (pr_off_t) start_offset, (pr_off_t) st.st_size); |
2915 | 0 | pr_response_add_err(R_554, _("%s: invalid %s argument"), offset_cmd, |
2916 | 0 | cmd->arg); |
2917 | 0 | pr_fsio_close(retr_fh); |
2918 | 0 | retr_fh = NULL; |
2919 | |
|
2920 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
2921 | 0 | errno = EINVAL; |
2922 | 0 | return PR_ERROR(cmd); |
2923 | 0 | } |
2924 | | |
2925 | | /* The RANG Draft says, on this topic: |
2926 | | * |
2927 | | * The server-PI SHOULD transfer 0 octets with RETR if the specified |
2928 | | * start point or start point and end point are larger than the actual |
2929 | | * file size. |
2930 | | * |
2931 | | * However, I vehemently disagree. Sending zero bytes in such a case |
2932 | | * would be treated as a successful download by the client, not informing |
2933 | | * the user of the erroneous conditions. Thus, IMHO, violating the |
2934 | | * principle of least surprise; the user might end up asking "Why did |
2935 | | * my download succeed, but I have zero bytes?". That is a terrible user |
2936 | | * experience. So instead, we return an error in this case. |
2937 | | */ |
2938 | | |
2939 | 0 | if ((start_offset + session.range_len) > st.st_size) { |
2940 | 0 | pr_trace_msg(trace_channel, 4, |
2941 | 0 | "%s offset %" PR_LU " exceeds file size (%" PR_LU " bytes)", |
2942 | 0 | offset_cmd, (pr_off_t) (start_offset + session.range_len), |
2943 | 0 | (pr_off_t) st.st_size); |
2944 | 0 | pr_response_add_err(R_554, _("%s: invalid RANG argument"), cmd->arg); |
2945 | 0 | pr_fsio_close(retr_fh); |
2946 | 0 | retr_fh = NULL; |
2947 | |
|
2948 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
2949 | 0 | errno = EINVAL; |
2950 | 0 | return PR_ERROR(cmd); |
2951 | 0 | } |
2952 | | |
2953 | 0 | if (pr_fsio_lseek(retr_fh, start_offset, SEEK_SET) == (off_t) -1) { |
2954 | 0 | xerrno = errno; |
2955 | 0 | pr_fsio_close(retr_fh); |
2956 | 0 | errno = xerrno; |
2957 | 0 | retr_fh = NULL; |
2958 | |
|
2959 | 0 | (void) pr_trace_msg("fileperms", 1, "%s, user '%s' (UID %s, GID %s): " |
2960 | 0 | "error seeking to byte %" PR_LU " of '%s': %s", (char *) cmd->argv[0], |
2961 | 0 | session.user, pr_uid2str(cmd->tmp_pool, session.uid), |
2962 | 0 | pr_gid2str(cmd->tmp_pool, session.gid), (pr_off_t) start_offset, |
2963 | 0 | dir, strerror(xerrno)); |
2964 | |
|
2965 | 0 | pr_log_debug(DEBUG0, "error seeking to offset %" PR_LU |
2966 | 0 | " for file %s: %s", (pr_off_t) start_offset, dir, strerror(xerrno)); |
2967 | 0 | pr_response_add_err(R_554, _("%s: invalid %s argument"), offset_cmd, |
2968 | 0 | cmd->arg); |
2969 | |
|
2970 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
2971 | 0 | errno = EINVAL; |
2972 | 0 | return PR_ERROR(cmd); |
2973 | 0 | } |
2974 | | |
2975 | 0 | curr_pos = start_offset; |
2976 | |
|
2977 | 0 | if (session.restart_pos > 0) { |
2978 | 0 | session.restart_pos = 0L; |
2979 | |
|
2980 | 0 | } else if (session.range_start > 0) { |
2981 | 0 | session.range_start = 0; |
2982 | 0 | } |
2983 | 0 | } |
2984 | | |
2985 | | /* Stash the offset at which we're writing from this file. */ |
2986 | 0 | curr_offset = pr_fsio_lseek(retr_fh, (off_t) 0, SEEK_CUR); |
2987 | 0 | if (curr_offset != (off_t) -1) { |
2988 | 0 | off_t *file_offset; |
2989 | |
|
2990 | 0 | file_offset = palloc(cmd->pool, sizeof(off_t)); |
2991 | 0 | *file_offset = (off_t) curr_offset; |
2992 | 0 | (void) pr_table_add(cmd->notes, "mod_xfer.file-offset", file_offset, |
2993 | 0 | sizeof(off_t)); |
2994 | 0 | } |
2995 | | |
2996 | | /* Block any timers for this section, where we want to prepare the |
2997 | | * data connection, then need to reprovision the session.xfer struct, |
2998 | | * and do NOT want timers (which may want/need that session.xfer data) |
2999 | | * to fire until after the reprovisioning (Bug#4168). |
3000 | | */ |
3001 | 0 | pr_alarms_block(); |
3002 | | |
3003 | | /* Send the data */ |
3004 | 0 | pr_data_init(cmd->arg, PR_NETIO_IO_WR); |
3005 | |
|
3006 | 0 | session.xfer.path = dir; |
3007 | 0 | session.xfer.file_size = st.st_size; |
3008 | |
|
3009 | 0 | pr_alarms_unblock(); |
3010 | |
|
3011 | 0 | cnt_steps = session.xfer.file_size / 100; |
3012 | 0 | if (cnt_steps == 0) { |
3013 | 0 | cnt_steps = 1; |
3014 | 0 | } |
3015 | |
|
3016 | 0 | if (session.range_len > 0) { |
3017 | 0 | if (curr_pos + session.range_len > st.st_size) { |
3018 | | /* If the RANG end point is past the end of our file, ignore it and |
3019 | | * treat this as the remainder of the file, from the starting offset. |
3020 | | */ |
3021 | 0 | download_len = st.st_size - curr_pos; |
3022 | |
|
3023 | 0 | } else { |
3024 | 0 | download_len = session.range_len; |
3025 | 0 | } |
3026 | |
|
3027 | 0 | } else { |
3028 | 0 | download_len = st.st_size - curr_pos; |
3029 | 0 | } |
3030 | |
|
3031 | 0 | if (pr_data_open(cmd->arg, NULL, PR_NETIO_IO_WR, download_len) < 0) { |
3032 | 0 | xerrno = errno; |
3033 | |
|
3034 | 0 | retr_abort(cmd->pool); |
3035 | 0 | pr_data_abort(0, TRUE); |
3036 | |
|
3037 | 0 | pr_cmd_set_errno(cmd, xerrno); |
3038 | 0 | errno = xerrno; |
3039 | 0 | return PR_ERROR(cmd); |
3040 | 0 | } |
3041 | | |
3042 | | /* Retrieve the number of bytes to retrieve, maximum, if present */ |
3043 | 0 | nbytes_max_retrieve = find_max_nbytes("MaxRetrieveFileSize"); |
3044 | 0 | if (nbytes_max_retrieve == 0UL) { |
3045 | 0 | have_limit = FALSE; |
3046 | |
|
3047 | 0 | } else { |
3048 | 0 | have_limit = TRUE; |
3049 | 0 | } |
3050 | | |
3051 | | /* Check the MaxRetrieveFileSize. If it is zero, or if the size |
3052 | | * of the file being retrieved is greater than the MaxRetrieveFileSize, |
3053 | | * then signal an error and abort the transfer now. |
3054 | | */ |
3055 | 0 | if (have_limit && |
3056 | 0 | ((nbytes_max_retrieve == 0) || (st.st_size > nbytes_max_retrieve))) { |
3057 | |
|
3058 | 0 | pr_log_pri(PR_LOG_NOTICE, "MaxRetrieveFileSize (%" PR_LU " %s) reached: " |
3059 | 0 | "aborting transfer of '%s'", (pr_off_t) nbytes_max_retrieve, |
3060 | 0 | nbytes_max_retrieve != 1 ? "bytes" : "byte", dir); |
3061 | | |
3062 | | /* Abort the transfer. */ |
3063 | 0 | retr_abort(cmd->pool); |
3064 | | |
3065 | | /* Set errno to EPERM ("Operation not permitted") */ |
3066 | 0 | pr_data_abort(EPERM, FALSE); |
3067 | |
|
3068 | 0 | pr_cmd_set_errno(cmd, EPERM); |
3069 | 0 | errno = EPERM; |
3070 | 0 | return PR_ERROR(cmd); |
3071 | 0 | } |
3072 | | |
3073 | 0 | bufsz = pr_config_get_server_xfer_bufsz(PR_NETIO_IO_WR); |
3074 | 0 | lbuf = (char *) palloc(cmd->tmp_pool, bufsz); |
3075 | 0 | pr_trace_msg("data", 8, "allocated download buffer of %lu bytes", |
3076 | 0 | (unsigned long) bufsz); |
3077 | |
|
3078 | 0 | pr_scoreboard_entry_update(session.pid, |
3079 | 0 | PR_SCORE_XFER_SIZE, download_len, |
3080 | 0 | PR_SCORE_XFER_DONE, (off_t) 0, |
3081 | 0 | NULL); |
3082 | |
|
3083 | 0 | if (session.range_len > 0) { |
3084 | 0 | if (bufsz > session.range_len) { |
3085 | 0 | bufsz = session.range_len; |
3086 | 0 | } |
3087 | 0 | } |
3088 | |
|
3089 | 0 | while (nbytes_sent != download_len) { |
3090 | 0 | int update_scoreboard = FALSE; |
3091 | |
|
3092 | 0 | pr_signals_handle(); |
3093 | |
|
3094 | 0 | if (XFER_ABORTED) { |
3095 | 0 | break; |
3096 | 0 | } |
3097 | | |
3098 | 0 | len = transmit_data(cmd->pool, curr_offset, &curr_pos, lbuf, bufsz); |
3099 | 0 | if (len == 0) { |
3100 | 0 | break; |
3101 | 0 | } |
3102 | | |
3103 | 0 | if (len < 0) { |
3104 | | /* Make sure that the errno value, needed for the pr_data_abort() call, |
3105 | | * is preserved; errno itself might be overwritten in retr_abort(). |
3106 | | */ |
3107 | 0 | int already_aborted = FALSE; |
3108 | |
|
3109 | 0 | xerrno = errno; |
3110 | 0 | retr_abort(cmd->pool); |
3111 | | |
3112 | | /* Do we need to abort the data transfer here? It's possible that |
3113 | | * the transfer has already been aborted, e.g. via the TCP OOB marker |
3114 | | * and/or the ABOR command. And if that is the case, then calling |
3115 | | * pr_data_abort() here will only lead to a spurious response code |
3116 | | * (see Bug#4252). |
3117 | | * |
3118 | | * However, there are OTHER error conditions which would lead to this |
3119 | | * code path. So we need to resort to some heuristics to differentiate |
3120 | | * between these cases. The errno value checks match those in the |
3121 | | * pr_data_xfer() function, after the control channel has been polled |
3122 | | * for commands such as ABOR. |
3123 | | */ |
3124 | |
|
3125 | 0 | if (session.d == NULL && |
3126 | 0 | #if defined(ECONNABORTED) |
3127 | 0 | xerrno == ECONNABORTED && |
3128 | | #elif defined(ENOTCONN) |
3129 | | xerrno == ENOTCONN && |
3130 | | #else |
3131 | | xerrno == EIO && |
3132 | | #endif |
3133 | 0 | session.xfer.xfer_type == STOR_DEFAULT) { |
3134 | | |
3135 | | /* If the ABOR command has been sent, then pr_data_reset() and |
3136 | | * pr_data_cleanup() will have been called; the latter resets the |
3137 | | * xfer_type value to DEFAULT. |
3138 | | */ |
3139 | 0 | already_aborted = TRUE; |
3140 | 0 | } |
3141 | |
|
3142 | 0 | if (already_aborted == FALSE) { |
3143 | 0 | pr_data_abort(xerrno, FALSE); |
3144 | 0 | } |
3145 | |
|
3146 | 0 | pr_cmd_set_errno(cmd, xerrno); |
3147 | 0 | errno = xerrno; |
3148 | 0 | return PR_ERROR(cmd); |
3149 | 0 | } |
3150 | | |
3151 | 0 | nbytes_sent += len; |
3152 | 0 | curr_offset += len; |
3153 | |
|
3154 | 0 | if ((nbytes_sent / cnt_steps) != cnt_next) { |
3155 | 0 | cnt_next = nbytes_sent / cnt_steps; |
3156 | |
|
3157 | 0 | update_scoreboard = TRUE; |
3158 | 0 | } |
3159 | | |
3160 | | /* If no throttling is configured, this simply updates the scoreboard. |
3161 | | * In this case, we want to use session.xfer.total_bytes, rather than |
3162 | | * nbytes_sent, as the latter incorporates a REST position and the |
3163 | | * former does not. (When handling STOR, this is not an issue: different |
3164 | | * end-of-loop conditions). |
3165 | | */ |
3166 | 0 | pr_throttle_pause(session.xfer.total_bytes, update_scoreboard, nbytes_sent); |
3167 | 0 | } |
3168 | | |
3169 | 0 | if (XFER_ABORTED) { |
3170 | 0 | retr_abort(cmd->pool); |
3171 | 0 | pr_data_abort(0, FALSE); |
3172 | |
|
3173 | 0 | pr_cmd_set_errno(cmd, EIO); |
3174 | 0 | errno = EIO; |
3175 | 0 | return PR_ERROR(cmd); |
3176 | 0 | } |
3177 | | |
3178 | | /* If no throttling is configured, this simply updates the scoreboard. |
3179 | | * In this case, we want to use session.xfer.total_bytes, rather than |
3180 | | * nbytes_sent, as the latter incorporates a REST position and the |
3181 | | * former does not. (When handling STOR, this is not an issue: different |
3182 | | * end-of-loop conditions). |
3183 | | */ |
3184 | 0 | pr_throttle_pause(session.xfer.total_bytes, TRUE, nbytes_sent); |
3185 | |
|
3186 | 0 | retr_complete(cmd->pool); |
3187 | 0 | xfer_displayfile(); |
3188 | 0 | pr_data_close2(); |
3189 | 0 | pr_response_add(R_226, _("Transfer complete")); |
3190 | |
|
3191 | 0 | return PR_HANDLED(cmd); |
3192 | 0 | } |
3193 | | |
3194 | 0 | MODRET xfer_abor(cmd_rec *cmd) { |
3195 | 0 | if (cmd->argc != 1) { |
3196 | 0 | pr_response_add_err(R_500, _("'%s' not understood"), |
3197 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
3198 | |
|
3199 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
3200 | 0 | errno = EINVAL; |
3201 | 0 | return PR_ERROR(cmd); |
3202 | 0 | } |
3203 | | |
3204 | 0 | if (session.xfer.direction == PR_NETIO_IO_RD) { |
3205 | 0 | stor_abort(cmd->pool); |
3206 | |
|
3207 | 0 | } else if (session.xfer.direction == PR_NETIO_IO_WR) { |
3208 | 0 | retr_abort(cmd->pool); |
3209 | 0 | } |
3210 | |
|
3211 | 0 | pr_data_abort(0, FALSE); |
3212 | |
|
3213 | 0 | pr_response_add(R_226, _("Abort successful")); |
3214 | 0 | return PR_HANDLED(cmd); |
3215 | 0 | } |
3216 | | |
3217 | 0 | MODRET xfer_log_abor(cmd_rec *cmd) { |
3218 | | |
3219 | | /* Clean up the data connection info in the session structure. */ |
3220 | 0 | pr_data_reset(); |
3221 | 0 | pr_data_cleanup(); |
3222 | |
|
3223 | 0 | return PR_DECLINED(cmd); |
3224 | 0 | } |
3225 | | |
3226 | 0 | MODRET xfer_type(cmd_rec *cmd) { |
3227 | 0 | char *type; |
3228 | |
|
3229 | 0 | if (cmd->argc < 2 || |
3230 | 0 | cmd->argc > 3) { |
3231 | 0 | pr_response_add_err(R_500, _("'%s' not understood"), |
3232 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
3233 | |
|
3234 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
3235 | 0 | errno = EINVAL; |
3236 | 0 | return PR_ERROR(cmd); |
3237 | 0 | } |
3238 | | |
3239 | 0 | type = pstrdup(cmd->tmp_pool, cmd->argv[1]); |
3240 | 0 | if (PR_ISALPHA((int) type[0])) { |
3241 | 0 | type[0] = toupper((int) type[0]); |
3242 | 0 | } |
3243 | |
|
3244 | 0 | if (strcmp(type, "A") == 0) { |
3245 | 0 | if (cmd->argc == 3 && |
3246 | 0 | strcmp((char *) cmd->argv[2], "N") != 0) { |
3247 | 0 | pr_response_add_err(R_504, _("%s not implemented for '%s' parameter"), |
3248 | 0 | (char *) cmd->argv[0], (char *) cmd->argv[2]); |
3249 | |
|
3250 | 0 | pr_cmd_set_errno(cmd, ENOSYS); |
3251 | 0 | errno = ENOSYS; |
3252 | 0 | return PR_ERROR(cmd); |
3253 | 0 | } |
3254 | | |
3255 | | /* TYPE A(SCII). */ |
3256 | 0 | session.sf_flags |= SF_ASCII; |
3257 | |
|
3258 | 0 | } else if (strcmp(type, "I") == 0) { |
3259 | 0 | if (cmd->argc == 3) { |
3260 | 0 | pr_response_add_err(R_504, _("%s not implemented for '%s' parameter"), |
3261 | 0 | (char *) cmd->argv[0], (char *) cmd->argv[2]); |
3262 | |
|
3263 | 0 | pr_cmd_set_errno(cmd, ENOSYS); |
3264 | 0 | errno = ENOSYS; |
3265 | 0 | return PR_ERROR(cmd); |
3266 | 0 | } |
3267 | | |
3268 | | /* TYPE I(MAGE). */ |
3269 | 0 | session.sf_flags &= (SF_ALL^(SF_ASCII|SF_ASCII_OVERRIDE)); |
3270 | |
|
3271 | 0 | } else if (strcmp(type, "L") == 0) { |
3272 | 0 | if (cmd->argc == 2) { |
3273 | 0 | pr_response_add_err(R_504, _("%s not implemented for '%s' parameter"), |
3274 | 0 | (char *) cmd->argv[0], (char *) cmd->argv[1]); |
3275 | |
|
3276 | 0 | pr_cmd_set_errno(cmd, ENOSYS); |
3277 | 0 | errno = ENOSYS; |
3278 | 0 | return PR_ERROR(cmd); |
3279 | 0 | } |
3280 | | |
3281 | 0 | if (strcmp((char *) cmd->argv[2], "7") == 0) { |
3282 | | /* TYPE L 7. */ |
3283 | 0 | session.sf_flags |= SF_ASCII; |
3284 | |
|
3285 | 0 | } else if (strcmp((char *) cmd->argv[2], "8") == 0) { |
3286 | | /* TYPE L 8. */ |
3287 | 0 | session.sf_flags &= (SF_ALL^(SF_ASCII|SF_ASCII_OVERRIDE)); |
3288 | |
|
3289 | 0 | } else { |
3290 | 0 | pr_response_add_err(R_504, _("%s not implemented for '%s' parameter"), |
3291 | 0 | (char *) cmd->argv[0], (char *) cmd->argv[2]); |
3292 | |
|
3293 | 0 | pr_cmd_set_errno(cmd, ENOSYS); |
3294 | 0 | errno = ENOSYS; |
3295 | 0 | return PR_ERROR(cmd); |
3296 | 0 | } |
3297 | |
|
3298 | 0 | } else { |
3299 | 0 | pr_response_add_err(R_504, _("%s not implemented for '%s' parameter"), |
3300 | 0 | (char *) cmd->argv[0], (char *) cmd->argv[1]); |
3301 | |
|
3302 | 0 | pr_cmd_set_errno(cmd, ENOSYS); |
3303 | 0 | errno = ENOSYS; |
3304 | 0 | return PR_ERROR(cmd); |
3305 | 0 | } |
3306 | | |
3307 | | /* Note that the client may NOT be authenticated at this point in time. |
3308 | | * If that is the case, set a flag so that the POST_CMD PASS handler does |
3309 | | * not overwrite the TYPE command's setting. |
3310 | | * |
3311 | | * Alternatively, we COULD bar/reject any TYPE commands before authentication. |
3312 | | * However, I think that doing so would interfere with many existing clients |
3313 | | * which assume that they can send TYPE before authenticating. |
3314 | | */ |
3315 | 0 | if (session.auth_mech == NULL) { |
3316 | 0 | have_type = TRUE; |
3317 | 0 | } |
3318 | |
|
3319 | 0 | pr_response_add(R_200, _("Type set to %s"), (char *) cmd->argv[1]); |
3320 | 0 | return PR_HANDLED(cmd); |
3321 | 0 | } |
3322 | | |
3323 | 0 | MODRET xfer_stru(cmd_rec *cmd) { |
3324 | 0 | char *stru; |
3325 | 0 | size_t stru_len; |
3326 | |
|
3327 | 0 | if (cmd->argc != 2) { |
3328 | 0 | pr_response_add_err(R_501, _("'%s' not understood"), |
3329 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
3330 | |
|
3331 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
3332 | 0 | errno = EINVAL; |
3333 | 0 | return PR_ERROR(cmd); |
3334 | 0 | } |
3335 | | |
3336 | 0 | stru = cmd->argv[1]; |
3337 | 0 | stru_len = strlen(stru); |
3338 | 0 | if (stru_len > 1) { |
3339 | 0 | pr_response_add_err(R_501, _("'%s' unrecognized structure type"), |
3340 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
3341 | |
|
3342 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
3343 | 0 | errno = EINVAL; |
3344 | 0 | return PR_ERROR(cmd); |
3345 | 0 | } |
3346 | | |
3347 | 0 | if (PR_ISALPHA((int) stru[0])) { |
3348 | 0 | stru[0] = toupper((int) stru[0]); |
3349 | 0 | } |
3350 | |
|
3351 | 0 | switch ((int) stru[0]) { |
3352 | 0 | case 'F': |
3353 | | /* Should 202 be returned instead??? */ |
3354 | 0 | pr_response_add(R_200, _("Structure set to F")); |
3355 | 0 | return PR_HANDLED(cmd); |
3356 | 0 | break; |
3357 | | |
3358 | 0 | case 'R': |
3359 | | /* Accept R but with no operational difference from F??? |
3360 | | * R is required in minimum implementations by RFC-959, 5.1. |
3361 | | * RFC-1123, 4.1.2.13, amends this to only apply to servers whose file |
3362 | | * systems support record structures, but also suggests that such a |
3363 | | * server "may still accept files with STRU R, recording the byte stream |
3364 | | * literally." Another configurable choice, perhaps? |
3365 | | * |
3366 | | * NOTE: wu-ftpd does not so accept STRU R. |
3367 | | */ |
3368 | | |
3369 | | /* FALLTHROUGH */ |
3370 | |
|
3371 | 0 | case 'P': |
3372 | | /* RFC-1123 recommends against implementing P. */ |
3373 | 0 | pr_response_add_err(R_504, _("'%s' unsupported structure type"), |
3374 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
3375 | |
|
3376 | 0 | pr_cmd_set_errno(cmd, ENOSYS); |
3377 | 0 | errno = ENOSYS; |
3378 | 0 | return PR_ERROR(cmd); |
3379 | | |
3380 | 0 | default: |
3381 | 0 | pr_response_add_err(R_501, _("'%s' unrecognized structure type"), |
3382 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
3383 | |
|
3384 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
3385 | 0 | errno = EINVAL; |
3386 | 0 | return PR_ERROR(cmd); |
3387 | 0 | } |
3388 | 0 | } |
3389 | | |
3390 | 0 | MODRET xfer_mode(cmd_rec *cmd) { |
3391 | 0 | char *mode; |
3392 | 0 | size_t mode_len; |
3393 | |
|
3394 | 0 | if (cmd->argc != 2) { |
3395 | 0 | pr_response_add_err(R_501, _("'%s' not understood"), |
3396 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
3397 | |
|
3398 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
3399 | 0 | errno = EINVAL; |
3400 | 0 | return PR_ERROR(cmd); |
3401 | 0 | } |
3402 | | |
3403 | 0 | mode = cmd->argv[1]; |
3404 | 0 | mode_len = strlen(mode); |
3405 | 0 | if (mode_len > 1) { |
3406 | 0 | pr_response_add_err(R_504, _("'%s' unsupported transfer mode"), |
3407 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
3408 | |
|
3409 | 0 | pr_cmd_set_errno(cmd, ENOSYS); |
3410 | 0 | errno = ENOSYS; |
3411 | 0 | return PR_ERROR(cmd); |
3412 | 0 | } |
3413 | | |
3414 | 0 | if (PR_ISALPHA((int) mode[0])) { |
3415 | 0 | mode[0] = toupper((int) mode[0]); |
3416 | 0 | } |
3417 | |
|
3418 | 0 | switch ((int) mode[0]) { |
3419 | 0 | case 'S': |
3420 | | /* Should 202 be returned instead??? */ |
3421 | 0 | pr_response_add(R_200, _("Mode set to S")); |
3422 | 0 | return PR_HANDLED(cmd); |
3423 | | |
3424 | 0 | case 'B': |
3425 | | /* FALLTHROUGH */ |
3426 | |
|
3427 | 0 | case 'C': |
3428 | 0 | pr_response_add_err(R_504, _("'%s' unsupported transfer mode"), |
3429 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
3430 | |
|
3431 | 0 | pr_cmd_set_errno(cmd, ENOSYS); |
3432 | 0 | errno = ENOSYS; |
3433 | 0 | return PR_ERROR(cmd); |
3434 | 0 | } |
3435 | | |
3436 | 0 | pr_response_add_err(R_501, _("'%s' unrecognized transfer mode"), |
3437 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
3438 | |
|
3439 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
3440 | 0 | errno = EINVAL; |
3441 | 0 | return PR_ERROR(cmd); |
3442 | 0 | } |
3443 | | |
3444 | 0 | MODRET xfer_allo(cmd_rec *cmd) { |
3445 | 0 | off_t requested_sz; |
3446 | 0 | char *tmp = NULL; |
3447 | | |
3448 | | /* Even though we only handle the "ALLO <size>" command, we should not |
3449 | | * barf on the unlikely (but RFC-compliant) "ALLO <size> R <size>" commands. |
3450 | | * See RFC 959, Section 4.1.3. |
3451 | | */ |
3452 | 0 | if (cmd->argc != 2 && |
3453 | 0 | cmd->argc != 4) { |
3454 | 0 | pr_response_add_err(R_504, _("'%s' not understood"), |
3455 | 0 | pr_cmd_get_displayable_str(cmd, NULL)); |
3456 | |
|
3457 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
3458 | 0 | errno = EINVAL; |
3459 | 0 | return PR_ERROR(cmd); |
3460 | 0 | } |
3461 | | |
3462 | 0 | #ifdef HAVE_STRTOULL |
3463 | 0 | requested_sz = strtoull(cmd->argv[1], &tmp, 10); |
3464 | | #else |
3465 | | requested_sz = strtoul(cmd->argv[1], &tmp, 10); |
3466 | | #endif /* !HAVE_STRTOULL */ |
3467 | |
|
3468 | 0 | if (tmp && *tmp) { |
3469 | 0 | pr_response_add_err(R_504, _("%s: Invalid ALLO argument"), cmd->arg); |
3470 | |
|
3471 | 0 | pr_cmd_set_errno(cmd, EINVAL); |
3472 | 0 | errno = EINVAL; |
3473 | 0 | return PR_ERROR(cmd); |
3474 | 0 | } |
3475 | | |
3476 | 0 | if (xfer_opts & PR_XFER_OPT_HANDLE_ALLO) { |
3477 | 0 | const char *path; |
3478 | 0 | off_t avail_kb; |
3479 | 0 | int res; |
3480 | |
|
3481 | 0 | path = pr_fs_getcwd(); |
3482 | |
|
3483 | 0 | res = pr_fs_getsize2((char *) path, &avail_kb); |
3484 | 0 | if (res < 0) { |
3485 | | /* If we can't check the filesystem stats for any reason, let the request |
3486 | | * proceed anyway. |
3487 | | */ |
3488 | 0 | pr_log_debug(DEBUG7, |
3489 | 0 | "error getting available size for filesystem containing '%s': %s", |
3490 | 0 | path, strerror(errno)); |
3491 | 0 | pr_response_add(R_202, _("No storage allocation necessary")); |
3492 | |
|
3493 | 0 | } else { |
3494 | 0 | off_t requested_kb; |
3495 | | |
3496 | | /* The requested size is in bytes; the size returned from |
3497 | | * pr_fs_getsize2() is in KB. |
3498 | | */ |
3499 | 0 | requested_kb = requested_sz / 1024; |
3500 | |
|
3501 | 0 | if (requested_kb > avail_kb) { |
3502 | 0 | pr_log_debug(DEBUG5, "%s requested %" PR_LU " KB, only %" PR_LU |
3503 | 0 | " KB available on '%s'", (char *) cmd->argv[0], |
3504 | 0 | (pr_off_t) requested_kb, (pr_off_t) avail_kb, path); |
3505 | 0 | pr_response_add_err(R_552, "%s: %s", cmd->arg, strerror(ENOSPC)); |
3506 | |
|
3507 | 0 | pr_cmd_set_errno(cmd, ENOSPC); |
3508 | 0 | errno = ENOSPC; |
3509 | 0 | return PR_ERROR(cmd); |
3510 | 0 | } |
3511 | | |
3512 | 0 | pr_log_debug(DEBUG9, "%s requested %" PR_LU " KB, %" PR_LU |
3513 | 0 | " KB available on '%s'", (char *) cmd->argv[0], (pr_off_t) requested_kb, |
3514 | 0 | (pr_off_t) avail_kb, path); |
3515 | 0 | pr_response_add(R_200, _("%s command successful"), (char *) cmd->argv[0]); |
3516 | 0 | } |
3517 | |
|
3518 | 0 | } else { |
3519 | 0 | pr_response_add(R_202, _("No storage allocation necessary")); |
3520 | 0 | } |
3521 | | |
3522 | 0 | return PR_HANDLED(cmd); |
3523 | 0 | } |
3524 | | |
3525 | 0 | MODRET xfer_smnt(cmd_rec *cmd) { |
3526 | 0 | pr_response_add_err(R_502, _("SMNT command not implemented")); |
3527 | 0 | return PR_ERROR(cmd); |
3528 | 0 | } |
3529 | | |
3530 | 0 | MODRET xfer_err_cleanup(cmd_rec *cmd) { |
3531 | | |
3532 | | /* If a hidden store was aborted, remove it. */ |
3533 | 0 | if (session.xfer.xfer_type == STOR_HIDDEN) { |
3534 | 0 | unsigned char *delete_stores = NULL; |
3535 | |
|
3536 | 0 | delete_stores = get_param_ptr(CURRENT_CONF, "DeleteAbortedStores", FALSE); |
3537 | 0 | if (delete_stores == NULL || |
3538 | 0 | *delete_stores == TRUE) { |
3539 | 0 | if (session.xfer.path_hidden) { |
3540 | 0 | pr_log_debug(DEBUG5, "removing aborted HiddenStores file '%s'", |
3541 | 0 | session.xfer.path_hidden); |
3542 | 0 | if (pr_fsio_unlink(session.xfer.path_hidden) < 0) { |
3543 | 0 | if (errno != ENOENT) { |
3544 | 0 | pr_log_debug(DEBUG0, "error deleting HiddenStores file '%s': %s", |
3545 | 0 | session.xfer.path_hidden, strerror(errno)); |
3546 | 0 | } |
3547 | 0 | } |
3548 | 0 | } |
3549 | 0 | } |
3550 | 0 | } |
3551 | |
|
3552 | 0 | pr_data_reset(); |
3553 | 0 | pr_data_cleanup(); |
3554 | | |
3555 | | /* Don't forget to clear any possible RANG/REST parameters as well. */ |
3556 | 0 | session.range_start = session.range_len = 0; |
3557 | 0 | session.restart_pos = 0; |
3558 | |
|
3559 | 0 | return PR_DECLINED(cmd); |
3560 | 0 | } |
3561 | | |
3562 | 0 | MODRET xfer_log_stor(cmd_rec *cmd) { |
3563 | 0 | _log_transfer('i', 'c'); |
3564 | | |
3565 | | /* Increment the file counters. */ |
3566 | 0 | session.total_files_in++; |
3567 | 0 | session.total_files_xfer++; |
3568 | |
|
3569 | 0 | pr_data_cleanup(); |
3570 | | |
3571 | | /* Don't forget to clear any possible RANG/REST parameters as well. */ |
3572 | 0 | session.range_start = session.range_len = 0; |
3573 | 0 | session.restart_pos = 0; |
3574 | |
|
3575 | 0 | return PR_DECLINED(cmd); |
3576 | 0 | } |
3577 | | |
3578 | 0 | MODRET xfer_log_retr(cmd_rec *cmd) { |
3579 | 0 | _log_transfer('o', 'c'); |
3580 | | |
3581 | | /* Increment the file counters. */ |
3582 | 0 | session.total_files_out++; |
3583 | 0 | session.total_files_xfer++; |
3584 | |
|
3585 | 0 | pr_data_cleanup(); |
3586 | | |
3587 | | /* Don't forget to clear any possible RANG/REST parameters as well. */ |
3588 | 0 | session.range_start = session.range_len = 0; |
3589 | 0 | session.restart_pos = 0; |
3590 | |
|
3591 | 0 | return PR_DECLINED(cmd); |
3592 | 0 | } |
3593 | | |
3594 | 0 | static int noxfer_timeout_cb(CALLBACK_FRAME) { |
3595 | 0 | int timeout; |
3596 | 0 | const char *proto; |
3597 | |
|
3598 | 0 | timeout = pr_data_get_timeout(PR_DATA_TIMEOUT_NO_TRANSFER); |
3599 | |
|
3600 | 0 | if (session.sf_flags & SF_XFER) { |
3601 | 0 | pr_trace_msg("timer", 4, |
3602 | 0 | "TimeoutNoTransfer (%d %s) reached, but data transfer in progress, " |
3603 | 0 | "ignoring", timeout, timeout != 1 ? "seconds" : "second"); |
3604 | | |
3605 | | /* Transfer in progress, ignore this timeout */ |
3606 | 0 | return 1; |
3607 | 0 | } |
3608 | | |
3609 | 0 | pr_event_generate("core.timeout-no-transfer", NULL); |
3610 | 0 | pr_response_send_async(R_421, |
3611 | 0 | _("No transfer timeout (%d seconds): closing control connection"), timeout); |
3612 | |
|
3613 | 0 | pr_timer_remove(PR_TIMER_IDLE, ANY_MODULE); |
3614 | 0 | pr_timer_remove(PR_TIMER_LOGIN, ANY_MODULE); |
3615 | | |
3616 | | /* If this timeout is encountered and we are expecting a passive transfer, |
3617 | | * add some logging that suggests things to check and possibly fix |
3618 | | * (e.g. network/firewall rules). |
3619 | | */ |
3620 | 0 | if (session.sf_flags & SF_PASSIVE) { |
3621 | 0 | pr_log_pri(PR_LOG_NOTICE, |
3622 | 0 | "Passive data transfer failed, possibly due to network issues"); |
3623 | 0 | pr_log_pri(PR_LOG_NOTICE, |
3624 | 0 | "Check your PassivePorts and MasqueradeAddress settings,"); |
3625 | 0 | pr_log_pri(PR_LOG_NOTICE, |
3626 | 0 | "and any router, NAT, and firewall rules in the network path."); |
3627 | 0 | } |
3628 | |
|
3629 | 0 | proto = pr_session_get_protocol(PR_SESS_PROTO_FL_LOGOUT); |
3630 | |
|
3631 | 0 | pr_log_pri(PR_LOG_NOTICE, "%s no transfer timeout, disconnected", proto); |
3632 | 0 | pr_session_disconnect(&xfer_module, PR_SESS_DISCONNECT_TIMEOUT, |
3633 | 0 | "TimeoutNoTransfer"); |
3634 | |
|
3635 | 0 | return 0; |
3636 | 0 | } |
3637 | | |
3638 | 0 | MODRET xfer_post_pass(cmd_rec *cmd) { |
3639 | 0 | config_rec *c; |
3640 | | |
3641 | | /* Default transfer mode is ASCII, per RFC 959, Section 3.1.1.1. Unless |
3642 | | * the client has already sent a TYPE command. |
3643 | | */ |
3644 | 0 | if (have_type == FALSE) { |
3645 | 0 | session.sf_flags |= SF_ASCII; |
3646 | 0 | c = find_config(main_server->conf, CONF_PARAM, "DefaultTransferMode", |
3647 | 0 | FALSE); |
3648 | 0 | if (c != NULL) { |
3649 | 0 | char *default_transfer_mode; |
3650 | |
|
3651 | 0 | default_transfer_mode = c->argv[0]; |
3652 | 0 | if (strcasecmp(default_transfer_mode, "binary") == 0) { |
3653 | 0 | session.sf_flags &= (SF_ALL^SF_ASCII); |
3654 | 0 | } |
3655 | 0 | } |
3656 | 0 | } |
3657 | |
|
3658 | 0 | c = find_config(TOPLEVEL_CONF, CONF_PARAM, "TimeoutNoTransfer", FALSE); |
3659 | 0 | if (c != NULL) { |
3660 | 0 | int timeout = *((int *) c->argv[0]); |
3661 | 0 | pr_data_set_timeout(PR_DATA_TIMEOUT_NO_TRANSFER, timeout); |
3662 | | |
3663 | | /* Setup timer */ |
3664 | 0 | if (timeout > 0) { |
3665 | 0 | pr_timer_add(timeout, PR_TIMER_NOXFER, &xfer_module, noxfer_timeout_cb, |
3666 | 0 | "TimeoutNoTransfer"); |
3667 | 0 | } |
3668 | 0 | } |
3669 | |
|
3670 | 0 | c = find_config(TOPLEVEL_CONF, CONF_PARAM, "TimeoutStalled", FALSE); |
3671 | 0 | if (c != NULL) { |
3672 | 0 | int timeout = *((int *) c->argv[0]); |
3673 | 0 | pr_data_set_timeout(PR_DATA_TIMEOUT_STALLED, timeout); |
3674 | | |
3675 | | /* Note: timers for handling TimeoutStalled timeouts are handled in the |
3676 | | * data transfer routines, not here. |
3677 | | */ |
3678 | 0 | } |
3679 | |
|
3680 | 0 | c = find_config(main_server->conf, CONF_PARAM, "TransferOptions", FALSE); |
3681 | 0 | while (c != NULL) { |
3682 | 0 | unsigned long opts = 0; |
3683 | |
|
3684 | 0 | pr_signals_handle(); |
3685 | |
|
3686 | 0 | opts = *((unsigned long *) c->argv[0]); |
3687 | 0 | xfer_opts |= opts; |
3688 | |
|
3689 | 0 | c = find_config_next(c, c->next, CONF_PARAM, "TransferOptions", FALSE); |
3690 | 0 | } |
3691 | |
|
3692 | 0 | if (xfer_opts & PR_XFER_OPT_IGNORE_ASCII) { |
3693 | 0 | pr_log_debug(DEBUG8, "Ignoring ASCII translation for this session"); |
3694 | 0 | pr_data_ignore_ascii(TRUE); |
3695 | 0 | } |
3696 | | |
3697 | | /* If we are chrooted, then skip actually processing the ALLO command |
3698 | | * (Bug#3996). |
3699 | | */ |
3700 | 0 | if (session.chroot_path != NULL) { |
3701 | 0 | xfer_opts &= ~PR_XFER_OPT_HANDLE_ALLO; |
3702 | 0 | } |
3703 | |
|
3704 | 0 | return PR_DECLINED(cmd); |
3705 | 0 | } |
3706 | | |
3707 | | /* Configuration handlers |
3708 | | */ |
3709 | | |
3710 | 0 | MODRET set_allowoverwrite(cmd_rec *cmd) { |
3711 | 0 | int allow_overwrite = -1; |
3712 | 0 | config_rec *c = NULL; |
3713 | |
|
3714 | 0 | CHECK_ARGS(cmd, 1); |
3715 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON| |
3716 | 0 | CONF_DIR|CONF_DYNDIR); |
3717 | |
|
3718 | 0 | allow_overwrite = get_boolean(cmd, 1); |
3719 | 0 | if (allow_overwrite == -1) { |
3720 | 0 | CONF_ERROR(cmd, "expected Boolean parameter"); |
3721 | 0 | } |
3722 | | |
3723 | 0 | c = add_config_param(cmd->argv[0], 1, NULL); |
3724 | 0 | c->argv[0] = pcalloc(c->pool, sizeof(unsigned char)); |
3725 | 0 | *((unsigned char *) c->argv[0]) = (unsigned char) allow_overwrite; |
3726 | 0 | c->flags |= CF_MERGEDOWN; |
3727 | |
|
3728 | 0 | return PR_HANDLED(cmd); |
3729 | 0 | } |
3730 | | |
3731 | 0 | MODRET set_allowrestart(cmd_rec *cmd) { |
3732 | 0 | int allow_restart = -1; |
3733 | 0 | config_rec *c = NULL; |
3734 | |
|
3735 | 0 | CHECK_ARGS(cmd, 1); |
3736 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON| |
3737 | 0 | CONF_DIR|CONF_DYNDIR); |
3738 | |
|
3739 | 0 | allow_restart = get_boolean(cmd, 1); |
3740 | 0 | if (allow_restart == -1) { |
3741 | 0 | CONF_ERROR(cmd, "expected Boolean parameter"); |
3742 | 0 | } |
3743 | | |
3744 | 0 | c = add_config_param(cmd->argv[0], 1, NULL); |
3745 | 0 | c->argv[0] = pcalloc(c->pool, sizeof(unsigned char)); |
3746 | 0 | *((unsigned char *) c->argv[0]) = allow_restart; |
3747 | 0 | c->flags |= CF_MERGEDOWN; |
3748 | |
|
3749 | 0 | return PR_HANDLED(cmd); |
3750 | 0 | } |
3751 | | |
3752 | | /* usage: DefaultTransferMode ascii|binary */ |
3753 | 0 | MODRET set_defaulttransfermode(cmd_rec *cmd) { |
3754 | 0 | char *default_mode; |
3755 | |
|
3756 | 0 | CHECK_ARGS(cmd, 1); |
3757 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
3758 | |
|
3759 | 0 | default_mode = cmd->argv[1]; |
3760 | 0 | if (strcasecmp(default_mode, "ascii") != 0 && |
3761 | 0 | strcasecmp(default_mode, "binary") != 0) { |
3762 | 0 | CONF_ERROR(cmd, "parameter must be 'ascii' or 'binary'"); |
3763 | 0 | } |
3764 | | |
3765 | 0 | add_config_param_str(cmd->argv[0], 1, default_mode); |
3766 | 0 | return PR_HANDLED(cmd); |
3767 | 0 | } |
3768 | | |
3769 | 0 | MODRET set_deleteabortedstores(cmd_rec *cmd) { |
3770 | 0 | int delete_aborted_stores = -1; |
3771 | 0 | config_rec *c = NULL; |
3772 | |
|
3773 | 0 | CHECK_ARGS(cmd, 1); |
3774 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON| |
3775 | 0 | CONF_DIR|CONF_DYNDIR); |
3776 | |
|
3777 | 0 | delete_aborted_stores = get_boolean(cmd, 1); |
3778 | 0 | if (delete_aborted_stores == -1) { |
3779 | 0 | CONF_ERROR(cmd, "expected Boolean parameter"); |
3780 | 0 | } |
3781 | | |
3782 | 0 | c = add_config_param(cmd->argv[0], 1, NULL); |
3783 | 0 | c->argv[0] = pcalloc(c->pool, sizeof(unsigned char)); |
3784 | 0 | *((unsigned char *) c->argv[0]) = delete_aborted_stores; |
3785 | 0 | c->flags |= CF_MERGEDOWN; |
3786 | |
|
3787 | 0 | return PR_HANDLED(cmd); |
3788 | 0 | } |
3789 | | |
3790 | | /* usage: DisplayFileTransfer path */ |
3791 | 0 | MODRET set_displayfiletransfer(cmd_rec *cmd) { |
3792 | 0 | CHECK_ARGS(cmd, 1); |
3793 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
3794 | |
|
3795 | 0 | (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]); |
3796 | 0 | return PR_HANDLED(cmd); |
3797 | 0 | } |
3798 | | |
3799 | 0 | MODRET set_hiddenstores(cmd_rec *cmd) { |
3800 | 0 | int enabled = -1, add_periods = TRUE; |
3801 | 0 | config_rec *c = NULL; |
3802 | 0 | char *prefix = NULL; |
3803 | |
|
3804 | 0 | CHECK_ARGS(cmd, 1); |
3805 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON|CONF_DIR); |
3806 | |
|
3807 | 0 | c = add_config_param(cmd->argv[0], 3, NULL, NULL, NULL); |
3808 | | |
3809 | | /* Handle the case where the admin may, for some reason, want a custom |
3810 | | * prefix which could also be construed to be a Boolean value by |
3811 | | * get_boolean(): if the value begins AND ends with a period, then treat |
3812 | | * it as a custom prefix. |
3813 | | */ |
3814 | 0 | prefix = cmd->argv[1]; |
3815 | 0 | if (prefix[0] == '.' && |
3816 | 0 | prefix[strlen(prefix)-1] == '.') { |
3817 | 0 | add_periods = FALSE; |
3818 | 0 | enabled = -1; |
3819 | |
|
3820 | 0 | } else { |
3821 | 0 | enabled = get_boolean(cmd, 1); |
3822 | 0 | } |
3823 | | |
3824 | | /* If a suffix has been configured as well, assume that we do NOT |
3825 | | * automatically want periods. |
3826 | | */ |
3827 | 0 | if (cmd->argc == 3) { |
3828 | 0 | add_periods = FALSE; |
3829 | 0 | } |
3830 | |
|
3831 | 0 | if (enabled == -1) { |
3832 | | /* If the parameter is not a Boolean parameter, assume that the |
3833 | | * admin is configuring a specific prefix to use instead of the |
3834 | | * default ".in.". |
3835 | | */ |
3836 | |
|
3837 | 0 | c->argv[0] = pcalloc(c->pool, sizeof(int)); |
3838 | 0 | *((int *) c->argv[0]) = TRUE; |
3839 | |
|
3840 | 0 | if (add_periods) { |
3841 | | /* Automatically add the leading and trailing periods. */ |
3842 | 0 | c->argv[1] = pstrcat(c->pool, ".", cmd->argv[1], ".", NULL); |
3843 | |
|
3844 | 0 | } else { |
3845 | 0 | c->argv[1] = pstrdup(c->pool, cmd->argv[1]); |
3846 | 0 | } |
3847 | |
|
3848 | 0 | if (cmd->argc == 3) { |
3849 | 0 | c->argv[2] = pstrdup(c->pool, cmd->argv[2]); |
3850 | |
|
3851 | 0 | } else { |
3852 | 0 | c->argv[2] = pstrdup(c->pool, "."); |
3853 | 0 | } |
3854 | |
|
3855 | 0 | } else { |
3856 | 0 | c->argv[0] = pcalloc(c->pool, sizeof(int)); |
3857 | 0 | *((int *) c->argv[0]) = enabled; |
3858 | |
|
3859 | 0 | if (enabled) { |
3860 | | /* The default HiddenStore prefix */ |
3861 | 0 | c->argv[1] = pstrdup(c->pool, ".in."); |
3862 | | |
3863 | | /* The default HiddenStores suffix. */ |
3864 | 0 | c->argv[2] = pstrdup(c->pool, "."); |
3865 | 0 | } |
3866 | 0 | } |
3867 | |
|
3868 | 0 | c->flags |= CF_MERGEDOWN; |
3869 | 0 | return PR_HANDLED(cmd); |
3870 | 0 | } |
3871 | | |
3872 | 0 | MODRET set_maxfilesize(cmd_rec *cmd) { |
3873 | 0 | config_rec *c = NULL; |
3874 | 0 | off_t nbytes; |
3875 | 0 | unsigned int precedence = 0; |
3876 | |
|
3877 | 0 | int ctxt = (cmd->config && cmd->config->config_type != CONF_PARAM ? |
3878 | 0 | cmd->config->config_type : cmd->server->config_type ? |
3879 | 0 | cmd->server->config_type : CONF_ROOT); |
3880 | |
|
3881 | 0 | if (cmd->argc-1 == 1) { |
3882 | 0 | if (strcmp(cmd->argv[1], "*") != 0) { |
3883 | 0 | CONF_ERROR(cmd, "incorrect number of parameters"); |
3884 | 0 | } |
3885 | |
|
3886 | 0 | } else if (cmd->argc-1 != 2 && |
3887 | 0 | cmd->argc-1 != 4) { |
3888 | 0 | CONF_ERROR(cmd, "incorrect number of parameters"); |
3889 | 0 | } |
3890 | | |
3891 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_ANON|CONF_VIRTUAL|CONF_GLOBAL|CONF_DIR| |
3892 | 0 | CONF_DYNDIR); |
3893 | | |
3894 | | /* Set the precedence for this config_rec based on its configuration |
3895 | | * context. |
3896 | | */ |
3897 | 0 | if (ctxt & CONF_GLOBAL) { |
3898 | 0 | precedence = 1; |
3899 | | |
3900 | | /* These will never appear simultaneously */ |
3901 | 0 | } else if ((ctxt & CONF_ROOT) || |
3902 | 0 | (ctxt & CONF_VIRTUAL)) { |
3903 | 0 | precedence = 2; |
3904 | |
|
3905 | 0 | } else if (ctxt & CONF_ANON) { |
3906 | 0 | precedence = 3; |
3907 | |
|
3908 | 0 | } else if (ctxt & CONF_DIR) { |
3909 | 0 | precedence = 4; |
3910 | |
|
3911 | 0 | } else if (ctxt & CONF_DYNDIR) { |
3912 | 0 | precedence = 5; |
3913 | 0 | } |
3914 | | |
3915 | | /* If the directive was used with four arguments, it means the optional |
3916 | | * classifiers and expression were used. Make sure the classifier is a valid |
3917 | | * one. |
3918 | | */ |
3919 | 0 | if (cmd->argc-1 == 4) { |
3920 | 0 | if (strcasecmp(cmd->argv[3], "user") != 0 && |
3921 | 0 | strcasecmp(cmd->argv[3], "group") != 0 && |
3922 | 0 | strcasecmp(cmd->argv[3], "class") != 0) { |
3923 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown classifier used: '", |
3924 | 0 | cmd->argv[3], "'", NULL)); |
3925 | 0 | } |
3926 | 0 | } |
3927 | | |
3928 | 0 | if (cmd->argc-1 == 1) { |
3929 | | /* Do nothing here -- the "*" (the only parameter allowed if there is |
3930 | | * only a single parameter given) signifies an unlimited size, which is |
3931 | | * what the server provides by default. |
3932 | | */ |
3933 | 0 | nbytes = 0UL; |
3934 | |
|
3935 | 0 | } else { |
3936 | | /* Pass the cmd_rec off to see what number of bytes was |
3937 | | * requested/configured. |
3938 | | */ |
3939 | 0 | if (pr_str_get_nbytes(cmd->argv[1], cmd->argv[2], &nbytes) < 0) { |
3940 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to parse: ", |
3941 | 0 | cmd->argv[1], " ", cmd->argv[2], ": ", strerror(errno), NULL)); |
3942 | 0 | } |
3943 | | |
3944 | 0 | if (nbytes == 0) { |
3945 | 0 | CONF_ERROR(cmd, "size must be greater than zero"); |
3946 | 0 | } |
3947 | 0 | } |
3948 | | |
3949 | 0 | if (cmd->argc-1 == 1 || |
3950 | 0 | cmd->argc-1 == 2) { |
3951 | 0 | c = add_config_param(cmd->argv[0], 2, NULL, NULL); |
3952 | 0 | c->argv[0] = pcalloc(c->pool, sizeof(off_t)); |
3953 | 0 | *((off_t *) c->argv[0]) = nbytes; |
3954 | 0 | c->argv[1] = pcalloc(c->pool, sizeof(unsigned int)); |
3955 | 0 | *((unsigned int *) c->argv[1]) = precedence; |
3956 | |
|
3957 | 0 | } else { |
3958 | 0 | array_header *acl = NULL; |
3959 | 0 | unsigned int argc; |
3960 | 0 | void **argv; |
3961 | |
|
3962 | 0 | argc = cmd->argc - 4; |
3963 | 0 | argv = cmd->argv + 3; |
3964 | |
|
3965 | 0 | acl = pr_expr_create(cmd->tmp_pool, &argc, (char **) argv); |
3966 | |
|
3967 | 0 | c = add_config_param(cmd->argv[0], 0); |
3968 | 0 | c->argc = argc + 3; |
3969 | 0 | c->argv = pcalloc(c->pool, ((argc + 4) * sizeof(void *))); |
3970 | |
|
3971 | 0 | argv = c->argv; |
3972 | | |
3973 | | /* Copy in the configured bytes */ |
3974 | 0 | *argv = pcalloc(c->pool, sizeof(unsigned long)); |
3975 | 0 | *((unsigned long *) *argv++) = nbytes; |
3976 | | |
3977 | | /* Copy in the precedence */ |
3978 | 0 | *argv = pcalloc(c->pool, sizeof(unsigned int)); |
3979 | 0 | *((unsigned int *) *argv++) = precedence; |
3980 | | |
3981 | | /* Copy in the classifier. */ |
3982 | 0 | *argv++ = pstrdup(c->pool, cmd->argv[3]); |
3983 | |
|
3984 | 0 | if (argc && acl) { |
3985 | 0 | while (argc--) { |
3986 | 0 | *argv++ = pstrdup(c->pool, *((char **) acl->elts)); |
3987 | 0 | acl->elts = ((char **) acl->elts) + 1; |
3988 | 0 | } |
3989 | 0 | } |
3990 | | |
3991 | | /* Don't forget the terminating NULL */ |
3992 | 0 | *argv = NULL; |
3993 | 0 | } |
3994 | |
|
3995 | 0 | c->flags |= CF_MERGEDOWN_MULTI; |
3996 | |
|
3997 | 0 | return PR_HANDLED(cmd); |
3998 | 0 | } |
3999 | | |
4000 | | /* usage: MaxTransfersPerHost cmdlist count [msg] */ |
4001 | 0 | MODRET set_maxtransfersperhost(cmd_rec *cmd) { |
4002 | 0 | config_rec *c = NULL; |
4003 | 0 | int count = 0; |
4004 | |
|
4005 | 0 | if (cmd->argc-1 < 2 || |
4006 | 0 | cmd->argc-1 > 3) { |
4007 | 0 | CONF_ERROR(cmd, "bad number of parameters"); |
4008 | 0 | } |
4009 | | |
4010 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON| |
4011 | 0 | CONF_DIR|CONF_DYNDIR); |
4012 | |
|
4013 | 0 | count = atoi(cmd->argv[2]); |
4014 | 0 | if (count < 1) { |
4015 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "count must be greater than zero: '", |
4016 | 0 | cmd->argv[2], "'", NULL)); |
4017 | 0 | } |
4018 | | |
4019 | 0 | c = add_config_param(cmd->argv[0], 3, NULL, NULL, NULL); |
4020 | | |
4021 | | /* Parse the command list. */ |
4022 | 0 | if (xfer_parse_cmdlist(cmd->argv[0], c, cmd->argv[1]) < 0) { |
4023 | 0 | CONF_ERROR(cmd, "error with command list"); |
4024 | 0 | } |
4025 | | |
4026 | 0 | c->argv[1] = pcalloc(c->pool, sizeof(unsigned int)); |
4027 | 0 | *((unsigned int *) c->argv[1]) = count; |
4028 | |
|
4029 | 0 | if (cmd->argc-1 == 3) { |
4030 | 0 | if (get_context_type(cmd) != CONF_DYNDIR) { |
4031 | | /* Per Issue #2490, we ignore configuration of the optional message |
4032 | | * in .ftpaccess files to prevent possible information leaks. |
4033 | | */ |
4034 | 0 | c->argv[2] = pstrdup(c->pool, cmd->argv[3]); |
4035 | 0 | } |
4036 | 0 | } |
4037 | |
|
4038 | 0 | c->flags |= CF_MERGEDOWN_MULTI; |
4039 | |
|
4040 | 0 | return PR_HANDLED(cmd); |
4041 | 0 | } |
4042 | | |
4043 | | /* usage: MaxTransfersPerUser cmdlist count [msg] */ |
4044 | 0 | MODRET set_maxtransfersperuser(cmd_rec *cmd) { |
4045 | 0 | config_rec *c = NULL; |
4046 | 0 | int count = 0; |
4047 | |
|
4048 | 0 | if (cmd->argc-1 < 2 || |
4049 | 0 | cmd->argc-1 > 3) { |
4050 | 0 | CONF_ERROR(cmd, "bad number of parameters"); |
4051 | 0 | } |
4052 | | |
4053 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON| |
4054 | 0 | CONF_DIR|CONF_DYNDIR); |
4055 | |
|
4056 | 0 | count = atoi(cmd->argv[2]); |
4057 | 0 | if (count < 1) { |
4058 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "count must be greater than zero: '", |
4059 | 0 | cmd->argv[2], "'", NULL)); |
4060 | 0 | } |
4061 | | |
4062 | 0 | c = add_config_param(cmd->argv[0], 3, NULL, NULL, NULL); |
4063 | | |
4064 | | /* Parse the command list. */ |
4065 | 0 | if (xfer_parse_cmdlist(cmd->argv[0], c, cmd->argv[1]) < 0) { |
4066 | 0 | CONF_ERROR(cmd, "error with command list"); |
4067 | 0 | } |
4068 | | |
4069 | 0 | c->argv[1] = pcalloc(c->pool, sizeof(unsigned int)); |
4070 | 0 | *((unsigned int *) c->argv[1]) = count; |
4071 | |
|
4072 | 0 | if (cmd->argc-1 == 3) { |
4073 | 0 | if (get_context_type(cmd) != CONF_DYNDIR) { |
4074 | | /* Per Issue #2490, we ignore configuration of the optional message |
4075 | | * in .ftpaccess files to prevent possible information leaks. |
4076 | | */ |
4077 | 0 | c->argv[2] = pstrdup(c->pool, cmd->argv[3]); |
4078 | 0 | } |
4079 | 0 | } |
4080 | |
|
4081 | 0 | c->flags |= CF_MERGEDOWN_MULTI; |
4082 | |
|
4083 | 0 | return PR_HANDLED(cmd); |
4084 | 0 | } |
4085 | | |
4086 | 0 | MODRET set_storeuniqueprefix(cmd_rec *cmd) { |
4087 | 0 | config_rec *c = NULL; |
4088 | |
|
4089 | 0 | CHECK_ARGS(cmd, 1); |
4090 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON|CONF_DIR); |
4091 | | |
4092 | | /* make sure there are no slashes in the prefix */ |
4093 | 0 | if (strchr(cmd->argv[1], '/') != NULL) { |
4094 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "no slashes allowed in prefix: '", |
4095 | 0 | cmd->argv[1], "'", NULL)); |
4096 | 0 | } |
4097 | | |
4098 | 0 | c = add_config_param_str(cmd->argv[0], 1, (void *) cmd->argv[1]); |
4099 | 0 | c->flags |= CF_MERGEDOWN; |
4100 | |
|
4101 | 0 | return PR_HANDLED(cmd); |
4102 | 0 | } |
4103 | | |
4104 | 0 | MODRET set_timeoutnoxfer(cmd_rec *cmd) { |
4105 | 0 | int timeout = -1; |
4106 | 0 | config_rec *c = NULL; |
4107 | |
|
4108 | 0 | CHECK_ARGS(cmd, 1); |
4109 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON); |
4110 | |
|
4111 | 0 | if (pr_str_get_duration(cmd->argv[1], &timeout) < 0) { |
4112 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing timeout value '", |
4113 | 0 | cmd->argv[1], "': ", strerror(errno), NULL)); |
4114 | 0 | } |
4115 | | |
4116 | 0 | c = add_config_param(cmd->argv[0], 1, NULL); |
4117 | 0 | c->argv[0] = pcalloc(c->pool, sizeof(int)); |
4118 | 0 | *((int *) c->argv[0]) = timeout; |
4119 | 0 | c->flags |= CF_MERGEDOWN; |
4120 | |
|
4121 | 0 | return PR_HANDLED(cmd); |
4122 | 0 | } |
4123 | | |
4124 | 0 | MODRET set_timeoutstalled(cmd_rec *cmd) { |
4125 | 0 | int timeout = -1; |
4126 | 0 | config_rec *c = NULL; |
4127 | |
|
4128 | 0 | CHECK_ARGS(cmd, 1); |
4129 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON); |
4130 | |
|
4131 | 0 | if (pr_str_get_duration(cmd->argv[1], &timeout) < 0) { |
4132 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing timeout value '", |
4133 | 0 | cmd->argv[1], "': ", strerror(errno), NULL)); |
4134 | 0 | } |
4135 | | |
4136 | 0 | c = add_config_param(cmd->argv[0], 1, NULL); |
4137 | 0 | c->argv[0] = pcalloc(c->pool, sizeof(int)); |
4138 | 0 | *((int *) c->argv[0]) = timeout; |
4139 | 0 | c->flags |= CF_MERGEDOWN; |
4140 | |
|
4141 | 0 | return PR_HANDLED(cmd); |
4142 | 0 | } |
4143 | | |
4144 | | /* usage: TransferOptions opt1 opt2 ... */ |
4145 | 0 | MODRET set_transferoptions(cmd_rec *cmd) { |
4146 | 0 | config_rec *c = NULL; |
4147 | 0 | register unsigned int i = 0; |
4148 | 0 | unsigned long opts = 0UL; |
4149 | |
|
4150 | 0 | if (cmd->argc-1 == 0) { |
4151 | 0 | CONF_ERROR(cmd, "wrong number of parameters"); |
4152 | 0 | } |
4153 | | |
4154 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
4155 | |
|
4156 | 0 | c = add_config_param(cmd->argv[0], 1, NULL); |
4157 | |
|
4158 | 0 | for (i = 1; i < cmd->argc; i++) { |
4159 | 0 | if (strcasecmp(cmd->argv[i], "IgnoreASCII") == 0) { |
4160 | 0 | opts |= PR_XFER_OPT_IGNORE_ASCII; |
4161 | |
|
4162 | 0 | } else if (strcasecmp(cmd->argv[i], "AllowSymlinkUpload") == 0 || |
4163 | 0 | strcasecmp(cmd->argv[i], "AllowSymlinkUploads") == 0) { |
4164 | 0 | opts |= PR_XFER_OPT_ALLOW_SYMLINK_UPLOAD; |
4165 | |
|
4166 | 0 | } else { |
4167 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown TransferOption '", |
4168 | 0 | cmd->argv[i], "'", NULL)); |
4169 | 0 | } |
4170 | 0 | } |
4171 | | |
4172 | 0 | c->argv[0] = pcalloc(c->pool, sizeof(unsigned long)); |
4173 | 0 | *((unsigned long *) c->argv[0]) = opts; |
4174 | |
|
4175 | 0 | return PR_HANDLED(cmd); |
4176 | 0 | } |
4177 | | |
4178 | | /* usage: TransferRate cmds kbps[:free-bytes] ["user"|"group"|"class" |
4179 | | * expression] |
4180 | | */ |
4181 | 0 | MODRET set_transferrate(cmd_rec *cmd) { |
4182 | 0 | config_rec *c = NULL; |
4183 | 0 | char *tmp = NULL, *endp = NULL; |
4184 | 0 | long double rate = 0.0; |
4185 | 0 | off_t freebytes = 0; |
4186 | 0 | unsigned int precedence = 0; |
4187 | |
|
4188 | 0 | int ctxt = (cmd->config && cmd->config->config_type != CONF_PARAM ? |
4189 | 0 | cmd->config->config_type : cmd->server->config_type ? |
4190 | 0 | cmd->server->config_type : CONF_ROOT); |
4191 | | |
4192 | | /* Must have two or four parameters */ |
4193 | 0 | if (cmd->argc-1 != 2 && |
4194 | 0 | cmd->argc-1 != 4) { |
4195 | 0 | CONF_ERROR(cmd, "wrong number of parameters"); |
4196 | 0 | } |
4197 | | |
4198 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON| |
4199 | 0 | CONF_DIR|CONF_DYNDIR); |
4200 | | |
4201 | | /* Set the precedence for this config_rec based on its configuration |
4202 | | * context. |
4203 | | */ |
4204 | 0 | if (ctxt & CONF_GLOBAL) { |
4205 | 0 | precedence = 1; |
4206 | | |
4207 | | /* These will never appear simultaneously */ |
4208 | 0 | } else if (ctxt & CONF_ROOT || ctxt & CONF_VIRTUAL) { |
4209 | 0 | precedence = 2; |
4210 | |
|
4211 | 0 | } else if (ctxt & CONF_ANON) { |
4212 | 0 | precedence = 3; |
4213 | |
|
4214 | 0 | } else if (ctxt & CONF_DIR) { |
4215 | 0 | precedence = 4; |
4216 | | |
4217 | | /* Note: by tweaking this value to be lower than the precedence for |
4218 | | * <Directory> appearances of this directive, I can effectively cause |
4219 | | * any .ftpaccess appearances not to override... |
4220 | | */ |
4221 | 0 | } else if (ctxt & CONF_DYNDIR) { |
4222 | 0 | precedence = 5; |
4223 | 0 | } |
4224 | | |
4225 | | /* Check for a valid classifier. */ |
4226 | 0 | if (cmd->argc-1 > 2) { |
4227 | 0 | if (strcasecmp(cmd->argv[3], "user") != 0 && |
4228 | 0 | strcasecmp(cmd->argv[3], "group") != 0 && |
4229 | 0 | strcasecmp(cmd->argv[3], "class") != 0) { |
4230 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown classifier requested: '", |
4231 | 0 | cmd->argv[3], "'", NULL)); |
4232 | 0 | } |
4233 | 0 | } |
4234 | | |
4235 | 0 | tmp = strchr(cmd->argv[2], ':'); |
4236 | 0 | if (tmp != NULL) { |
4237 | 0 | *tmp = '\0'; |
4238 | 0 | } |
4239 | | |
4240 | | /* Parse the 'kbps' part. Ideally, we'd be using strtold(3) rather than |
4241 | | * strtod(3) here, but FreeBSD doesn't have strtold(3). Yay. Portability. |
4242 | | */ |
4243 | 0 | rate = (long double) strtod(cmd->argv[2], &endp); |
4244 | |
|
4245 | 0 | if (rate < 0.0) { |
4246 | 0 | CONF_ERROR(cmd, "rate must be greater than zero"); |
4247 | 0 | } |
4248 | | |
4249 | 0 | if (endp && *endp) { |
4250 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "invalid number: '", |
4251 | 0 | cmd->argv[2], "'", NULL)); |
4252 | 0 | } |
4253 | | |
4254 | | /* Parse any 'free-bytes' part */ |
4255 | 0 | if (tmp) { |
4256 | 0 | cmd->argv[2] = ++tmp; |
4257 | |
|
4258 | 0 | freebytes = strtoul(cmd->argv[2], &endp, 10); |
4259 | 0 | if (endp && *endp) { |
4260 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "invalid number: '", |
4261 | 0 | cmd->argv[2], "'", NULL)); |
4262 | 0 | } |
4263 | 0 | } |
4264 | | |
4265 | | /* Construct the config_rec */ |
4266 | 0 | if (cmd->argc-1 == 2) { |
4267 | 0 | c = add_config_param(cmd->argv[0], 4, NULL, NULL, NULL, NULL); |
4268 | | |
4269 | | /* Parse the command list. */ |
4270 | 0 | if (xfer_parse_cmdlist(cmd->argv[0], c, cmd->argv[1]) < 0) { |
4271 | 0 | CONF_ERROR(cmd, "error with command list"); |
4272 | 0 | } |
4273 | | |
4274 | 0 | c->argv[1] = pcalloc(c->pool, sizeof(long double)); |
4275 | 0 | *((long double *) c->argv[1]) = rate; |
4276 | 0 | c->argv[2] = pcalloc(c->pool, sizeof(off_t)); |
4277 | 0 | *((off_t *) c->argv[2]) = freebytes; |
4278 | 0 | c->argv[3] = pcalloc(c->pool, sizeof(unsigned int)); |
4279 | 0 | *((unsigned int *) c->argv[3]) = precedence; |
4280 | |
|
4281 | 0 | } else { |
4282 | 0 | array_header *acl = NULL; |
4283 | 0 | unsigned int argc; |
4284 | 0 | void **argv; |
4285 | |
|
4286 | 0 | argc = cmd->argc - 4; |
4287 | 0 | argv = cmd->argv + 3; |
4288 | |
|
4289 | 0 | acl = pr_expr_create(cmd->tmp_pool, &argc, (char **) argv); |
4290 | 0 | c = add_config_param(cmd->argv[0], 0); |
4291 | | |
4292 | | /* Parse the command list. |
4293 | | * |
4294 | | * The five additional slots are for: cmd-list, bps, free-bytes, |
4295 | | * precedence, user/group/class. |
4296 | | */ |
4297 | 0 | c->argc = argc + 5; |
4298 | |
|
4299 | 0 | c->argv = pcalloc(c->pool, ((c->argc + 1) * sizeof(void *))); |
4300 | 0 | argv = c->argv; |
4301 | |
|
4302 | 0 | if (xfer_parse_cmdlist(cmd->argv[0], c, cmd->argv[1]) < 0) { |
4303 | 0 | CONF_ERROR(cmd, "error with command list"); |
4304 | 0 | } |
4305 | | |
4306 | | /* Note: the command list is at index 0, hence this increment. */ |
4307 | 0 | argv++; |
4308 | |
|
4309 | 0 | *argv = pcalloc(c->pool, sizeof(long double)); |
4310 | 0 | *((long double *) *argv++) = rate; |
4311 | 0 | *argv = pcalloc(c->pool, sizeof(off_t)); |
4312 | 0 | *((unsigned long *) *argv++) = freebytes; |
4313 | 0 | *argv = pcalloc(c->pool, sizeof(unsigned int)); |
4314 | 0 | *((unsigned int *) *argv++) = precedence; |
4315 | |
|
4316 | 0 | *argv++ = pstrdup(c->pool, cmd->argv[3]); |
4317 | |
|
4318 | 0 | if (argc && acl) { |
4319 | 0 | while (argc--) { |
4320 | 0 | *argv++ = pstrdup(c->pool, *((char **) acl->elts)); |
4321 | 0 | acl->elts = ((char **) acl->elts) + 1; |
4322 | 0 | } |
4323 | 0 | } |
4324 | | |
4325 | | /* don't forget the terminating NULL */ |
4326 | 0 | *argv = NULL; |
4327 | 0 | } |
4328 | | |
4329 | 0 | c->flags |= CF_MERGEDOWN_MULTI; |
4330 | 0 | return PR_HANDLED(cmd); |
4331 | 0 | } |
4332 | | |
4333 | | /* usage: UseSendfile on|off|"len units"|percentage"%" */ |
4334 | 0 | MODRET set_usesendfile(cmd_rec *cmd) { |
4335 | 0 | int do_sendfile = -1; |
4336 | 0 | off_t sendfile_len = 0; |
4337 | 0 | float sendfile_pct = -1.0; |
4338 | 0 | config_rec *c; |
4339 | |
|
4340 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON|CONF_DIR|CONF_DYNDIR); |
4341 | |
|
4342 | 0 | if (cmd->argc-1 == 1) { |
4343 | | /* Is the given parameter a boolean, or a percentage? Try parsing it a |
4344 | | * boolean first. |
4345 | | */ |
4346 | 0 | do_sendfile = get_boolean(cmd, 1); |
4347 | 0 | if (do_sendfile == -1) { |
4348 | 0 | char *arg; |
4349 | 0 | size_t arglen; |
4350 | | |
4351 | | /* See if the given parameter is a percentage. */ |
4352 | 0 | arg = cmd->argv[1]; |
4353 | 0 | arglen = strlen(arg); |
4354 | 0 | if (arglen > 1 && |
4355 | 0 | arg[arglen-1] == '%') { |
4356 | 0 | char *ptr = NULL; |
4357 | |
|
4358 | 0 | arg[arglen-1] = '\0'; |
4359 | |
|
4360 | 0 | #if defined(HAVE_STRTOF) |
4361 | 0 | sendfile_pct = strtof(arg, &ptr); |
4362 | | #elif defined(HAVE_STRTOD) |
4363 | | sendfile_pct = strtod(arg, &ptr); |
4364 | | #else |
4365 | | sendfile_pct = atof(arg); |
4366 | | #endif /* !HAVE_STRTOF and !HAVE_STRTOD */ |
4367 | |
|
4368 | 0 | if (ptr && *ptr) { |
4369 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "bad percentage value '", |
4370 | 0 | arg, "%'", NULL)); |
4371 | 0 | } |
4372 | | |
4373 | 0 | sendfile_pct /= 100.0; |
4374 | 0 | do_sendfile = TRUE; |
4375 | |
|
4376 | 0 | } else { |
4377 | 0 | CONF_ERROR(cmd, "expected Boolean parameter"); |
4378 | 0 | } |
4379 | 0 | } |
4380 | |
|
4381 | 0 | } else if (cmd->argc-1 == 2) { |
4382 | 0 | off_t nbytes; |
4383 | |
|
4384 | 0 | if (pr_str_get_nbytes(cmd->argv[1], cmd->argv[2], &nbytes) < 0) { |
4385 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to parse: ", |
4386 | 0 | cmd->argv[1], " ", cmd->argv[2], ": ", strerror(errno), NULL)); |
4387 | 0 | } |
4388 | | |
4389 | 0 | sendfile_len = nbytes; |
4390 | 0 | do_sendfile = TRUE; |
4391 | |
|
4392 | 0 | } else { |
4393 | 0 | CONF_ERROR(cmd, "wrong number of parameters"); |
4394 | 0 | } |
4395 | | |
4396 | 0 | c = add_config_param(cmd->argv[0], 3, NULL, NULL, NULL); |
4397 | 0 | c->argv[0] = pcalloc(c->pool, sizeof(unsigned char)); |
4398 | 0 | *((unsigned char *) c->argv[0]) = do_sendfile; |
4399 | 0 | c->argv[1] = pcalloc(c->pool, sizeof(off_t)); |
4400 | 0 | *((off_t *) c->argv[1]) = sendfile_len; |
4401 | 0 | c->argv[2] = pcalloc(c->pool, sizeof(float)); |
4402 | 0 | *((float *) c->argv[2]) = sendfile_pct; |
4403 | |
|
4404 | 0 | c->flags |= CF_MERGEDOWN; |
4405 | 0 | return PR_HANDLED(cmd); |
4406 | 0 | } |
4407 | | |
4408 | | /* Event handlers |
4409 | | */ |
4410 | | |
4411 | 0 | static void xfer_exit_ev(const void *event_data, void *user_data) { |
4412 | |
|
4413 | 0 | if (stor_fh != NULL) { |
4414 | | /* An upload is occurring... */ |
4415 | 0 | pr_trace_msg(trace_channel, 6, "session exiting, aborting upload"); |
4416 | 0 | stor_abort(session.pool); |
4417 | |
|
4418 | 0 | } else if (retr_fh != NULL) { |
4419 | | /* A download is occurring... */ |
4420 | 0 | pr_trace_msg(trace_channel, 6, "session exiting, aborting download"); |
4421 | 0 | retr_abort(session.pool); |
4422 | 0 | } |
4423 | |
|
4424 | 0 | if (session.sf_flags & SF_XFER) { |
4425 | 0 | pr_data_abort(0, FALSE); |
4426 | | |
4427 | | /* Issue #2360 demonstrated that sometimes, due to different module |
4428 | | * load order, assumptions about session state may not always be |
4429 | | * valid. So we double-check for our fields of interest. |
4430 | | */ |
4431 | 0 | if (session.xfer.xfer_cmd != NULL && |
4432 | 0 | session.xfer.path != NULL) { |
4433 | 0 | cmd_rec *cmd = NULL; |
4434 | |
|
4435 | 0 | cmd = pr_cmd_alloc(session.pool, 2, session.xfer.xfer_cmd, |
4436 | 0 | session.xfer.path); |
4437 | |
|
4438 | 0 | if (pr_cmd_cmp(cmd, PR_CMD_RETR_ID) == 0) { |
4439 | 0 | (void) pr_table_add(cmd->notes, "mod_xfer.retr-path", |
4440 | 0 | pstrdup(cmd->pool, session.xfer.path), 0); |
4441 | |
|
4442 | 0 | } else if (pr_cmd_cmp(cmd, PR_CMD_APPE_ID) == 0 || |
4443 | 0 | pr_cmd_cmp(cmd, PR_CMD_STOR_ID) == 0 || |
4444 | 0 | pr_cmd_cmp(cmd, PR_CMD_STOU_ID) == 0) { |
4445 | 0 | (void) pr_table_add(cmd->notes, "mod_xfer.store-path", |
4446 | 0 | pstrdup(cmd->pool, session.xfer.path), 0); |
4447 | 0 | } |
4448 | |
|
4449 | 0 | (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
4450 | 0 | (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
4451 | 0 | destroy_pool(cmd->pool); |
4452 | 0 | } |
4453 | |
|
4454 | 0 | pr_response_flush(&resp_err_list); |
4455 | 0 | } |
4456 | 0 | } |
4457 | | |
4458 | 0 | static void xfer_sess_reinit_ev(const void *event_data, void *user_data) { |
4459 | 0 | int res; |
4460 | | |
4461 | | /* A HOST command changed the main_server pointer, reinitialize ourselves. */ |
4462 | |
|
4463 | 0 | pr_event_unregister(&xfer_module, "core.exit", xfer_exit_ev); |
4464 | 0 | pr_event_unregister(&xfer_module, "core.session-reinit", xfer_sess_reinit_ev); |
4465 | 0 | pr_event_unregister(&xfer_module, "core.signal.USR2", xfer_sigusr2_ev); |
4466 | 0 | pr_event_unregister(&xfer_module, "core.timeout-stalled", |
4467 | 0 | xfer_timeout_stalled_ev); |
4468 | |
|
4469 | 0 | if (displayfilexfer_fh != NULL) { |
4470 | 0 | (void) pr_fsio_close(displayfilexfer_fh); |
4471 | 0 | displayfilexfer_fh = NULL; |
4472 | 0 | } |
4473 | |
|
4474 | 0 | res = xfer_sess_init(); |
4475 | 0 | if (res < 0) { |
4476 | 0 | pr_session_disconnect(&xfer_module, |
4477 | 0 | PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL); |
4478 | 0 | } |
4479 | 0 | } |
4480 | | |
4481 | 0 | static void xfer_sigusr2_ev(const void *event_data, void *user_data) { |
4482 | |
|
4483 | 0 | if (pr_module_exists("mod_shaper.c")) { |
4484 | | /* Only do this if we're currently involved in a data transfer. |
4485 | | * This is a hack put in to support mod_shaper's antics. |
4486 | | */ |
4487 | 0 | if (session.curr_cmd_id == PR_CMD_APPE_ID || |
4488 | 0 | session.curr_cmd_id == PR_CMD_RETR_ID || |
4489 | 0 | session.curr_cmd_id == PR_CMD_STOR_ID || |
4490 | 0 | session.curr_cmd_id == PR_CMD_STOU_ID) { |
4491 | 0 | pool *tmp_pool; |
4492 | 0 | cmd_rec *cmd; |
4493 | |
|
4494 | 0 | tmp_pool = make_sub_pool(session.pool); |
4495 | 0 | pr_pool_tag(tmp_pool, "Data Transfer SIGUSR2 pool"); |
4496 | |
|
4497 | 0 | cmd = pr_cmd_alloc(tmp_pool, 1, session.curr_cmd); |
4498 | | |
4499 | | /* Rescan the config tree for TransferRates, picking up any possible |
4500 | | * changes. |
4501 | | */ |
4502 | 0 | pr_log_debug(DEBUG2, "rechecking TransferRates"); |
4503 | 0 | pr_throttle_init(cmd); |
4504 | |
|
4505 | 0 | destroy_pool(tmp_pool); |
4506 | 0 | } |
4507 | 0 | } |
4508 | 0 | } |
4509 | | |
4510 | 0 | static void xfer_timedout(const char *reason) { |
4511 | 0 | if (stor_fh != NULL) { |
4512 | 0 | pr_trace_msg(trace_channel, 6, "%s, aborting upload", reason); |
4513 | 0 | stor_abort(session.pool); |
4514 | |
|
4515 | 0 | } else if (retr_fh != NULL) { |
4516 | 0 | pr_trace_msg(trace_channel, 6, "%s, aborting download", reason); |
4517 | 0 | retr_abort(session.pool); |
4518 | 0 | } |
4519 | 0 | } |
4520 | | |
4521 | 0 | static void xfer_timeout_session_ev(const void *event_data, void *user_data) { |
4522 | 0 | xfer_timedout("session timeout"); |
4523 | 0 | } |
4524 | | |
4525 | 0 | static void xfer_timeout_stalled_ev(const void *event_data, void *user_data) { |
4526 | | /* In this event handler, the "else" case, for a stalled transfer, will |
4527 | | * be handled by the 'core.exit' event handler above. For in that |
4528 | | * scenario, a data transfer WILL have actually been in progress, |
4529 | | * whereas in the !SF_XFER case, the client requested a transfer, but |
4530 | | * never actually opened the data connection. |
4531 | | */ |
4532 | |
|
4533 | 0 | if (!(session.sf_flags & SF_XFER)) { |
4534 | 0 | xfer_timedout("transfer stalled"); |
4535 | 0 | } |
4536 | 0 | } |
4537 | | |
4538 | | /* Initialization routines |
4539 | | */ |
4540 | | |
4541 | 0 | static int xfer_init(void) { |
4542 | | |
4543 | | /* Add the commands handled by this module to the HELP list. */ |
4544 | 0 | pr_help_add(C_TYPE, _("<sp> type-code (A, I, L 7, L 8)"), TRUE); |
4545 | 0 | pr_help_add(C_STRU, _("is not implemented (always F)"), TRUE); |
4546 | 0 | pr_help_add(C_MODE, _("is not implemented (always S)"), TRUE); |
4547 | 0 | pr_help_add(C_RETR, _("<sp> pathname"), TRUE); |
4548 | 0 | pr_help_add(C_STOR, _("<sp> pathname"), TRUE); |
4549 | 0 | pr_help_add(C_STOU, _("(store unique filename)"), TRUE); |
4550 | 0 | pr_help_add(C_APPE, _("<sp> pathname"), TRUE); |
4551 | 0 | pr_help_add(C_REST, _("<sp> byte-count"), TRUE); |
4552 | 0 | pr_help_add(C_ABOR, _("(abort current operation)"), TRUE); |
4553 | 0 | pr_help_add(C_RANG, _("<sp> start-point <sp> end-point"), TRUE); |
4554 | | |
4555 | | /* Add the additional features implemented by this module into the |
4556 | | * list, to be displayed in response to a FEAT command. |
4557 | | */ |
4558 | 0 | pr_feat_add(C_RANG " STREAM"); |
4559 | |
|
4560 | 0 | return 0; |
4561 | 0 | } |
4562 | | |
4563 | 0 | static int xfer_sess_init(void) { |
4564 | 0 | char *displayfilexfer = NULL; |
4565 | | |
4566 | | /* Exit handlers for HiddenStores cleanup */ |
4567 | 0 | pr_event_register(&xfer_module, "core.exit", xfer_exit_ev, NULL); |
4568 | 0 | pr_event_register(&xfer_module, "core.session-reinit", xfer_sess_reinit_ev, |
4569 | 0 | NULL); |
4570 | 0 | pr_event_register(&xfer_module, "core.signal.USR2", xfer_sigusr2_ev, |
4571 | 0 | NULL); |
4572 | 0 | pr_event_register(&xfer_module, "core.timeout-session", |
4573 | 0 | xfer_timeout_session_ev, NULL); |
4574 | 0 | pr_event_register(&xfer_module, "core.timeout-stalled", |
4575 | 0 | xfer_timeout_stalled_ev, NULL); |
4576 | |
|
4577 | 0 | have_type = FALSE; |
4578 | | |
4579 | | /* Look for a DisplayFileTransfer file which has an absolute path. If we |
4580 | | * find one, open a filehandle, such that that file can be displayed |
4581 | | * even if the session is chrooted. DisplayFileTransfer files with |
4582 | | * relative paths will be handled after chroot, preserving the old |
4583 | | * behavior. |
4584 | | */ |
4585 | 0 | displayfilexfer = get_param_ptr(main_server->conf, "DisplayFileTransfer", |
4586 | 0 | FALSE); |
4587 | 0 | if (displayfilexfer && |
4588 | 0 | *displayfilexfer == '/') { |
4589 | 0 | struct stat st; |
4590 | |
|
4591 | 0 | displayfilexfer_fh = pr_fsio_open(displayfilexfer, O_RDONLY); |
4592 | 0 | if (displayfilexfer_fh == NULL) { |
4593 | 0 | pr_log_debug(DEBUG6, "unable to open DisplayFileTransfer file '%s': %s", |
4594 | 0 | displayfilexfer, strerror(errno)); |
4595 | |
|
4596 | 0 | } else { |
4597 | 0 | if (pr_fsio_fstat(displayfilexfer_fh, &st) < 0) { |
4598 | 0 | pr_log_debug(DEBUG6, "error checking DisplayFileTransfer file '%s': %s", |
4599 | 0 | displayfilexfer, strerror(errno)); |
4600 | 0 | pr_fsio_close(displayfilexfer_fh); |
4601 | 0 | displayfilexfer_fh = NULL; |
4602 | |
|
4603 | 0 | } else { |
4604 | 0 | if (S_ISDIR(st.st_mode)) { |
4605 | 0 | errno = EISDIR; |
4606 | |
|
4607 | 0 | pr_log_debug(DEBUG6, |
4608 | 0 | "unable to use DisplayFileTransfer file '%s': %s", |
4609 | 0 | displayfilexfer, strerror(errno)); |
4610 | 0 | pr_fsio_close(displayfilexfer_fh); |
4611 | 0 | displayfilexfer_fh = NULL; |
4612 | 0 | } |
4613 | 0 | } |
4614 | 0 | } |
4615 | 0 | } |
4616 | | |
4617 | | /* IF the RFC2228 mechanism is "TLS" at this point in time, then set the flag |
4618 | | * to disable use of sendfile; the client is probably an FTPS client using |
4619 | | * implicit SSL (Bug#4073). |
4620 | | */ |
4621 | 0 | if (session.rfc2228_mech != NULL && |
4622 | 0 | strcmp(session.rfc2228_mech, "TLS") == 0) { |
4623 | 0 | have_rfc2228_data = TRUE; |
4624 | 0 | } |
4625 | |
|
4626 | 0 | return 0; |
4627 | 0 | } |
4628 | | |
4629 | | /* Module API tables |
4630 | | */ |
4631 | | |
4632 | | static conftable xfer_conftab[] = { |
4633 | | { "AllowOverwrite", set_allowoverwrite, NULL }, |
4634 | | { "AllowRetrieveRestart", set_allowrestart, NULL }, |
4635 | | { "AllowStoreRestart", set_allowrestart, NULL }, |
4636 | | { "DefaultTransferMode", set_defaulttransfermode, NULL }, |
4637 | | { "DeleteAbortedStores", set_deleteabortedstores, NULL }, |
4638 | | { "DisplayFileTransfer", set_displayfiletransfer, NULL }, |
4639 | | { "HiddenStores", set_hiddenstores, NULL }, |
4640 | | { "MaxRetrieveFileSize", set_maxfilesize, NULL }, |
4641 | | { "MaxStoreFileSize", set_maxfilesize, NULL }, |
4642 | | { "MaxTransfersPerHost", set_maxtransfersperhost, NULL }, |
4643 | | { "MaxTransfersPerUser", set_maxtransfersperuser, NULL }, |
4644 | | { "StoreUniquePrefix", set_storeuniqueprefix, NULL }, |
4645 | | { "TimeoutNoTransfer", set_timeoutnoxfer, NULL }, |
4646 | | { "TimeoutStalled", set_timeoutstalled, NULL }, |
4647 | | { "TransferOptions", set_transferoptions, NULL }, |
4648 | | { "TransferRate", set_transferrate, NULL }, |
4649 | | { "UseSendfile", set_usesendfile, NULL }, |
4650 | | |
4651 | | { NULL } |
4652 | | }; |
4653 | | |
4654 | | static cmdtable xfer_cmdtab[] = { |
4655 | | { CMD, C_TYPE, G_NONE, xfer_type, FALSE, FALSE, CL_MISC }, |
4656 | | { CMD, C_STRU, G_NONE, xfer_stru, TRUE, FALSE, CL_MISC }, |
4657 | | { CMD, C_MODE, G_NONE, xfer_mode, TRUE, FALSE, CL_MISC }, |
4658 | | { POST_CMD,C_MODE, G_NONE, xfer_post_mode,FALSE, FALSE }, |
4659 | | { CMD, C_ALLO, G_NONE, xfer_allo, TRUE, FALSE, CL_MISC }, |
4660 | | { CMD, C_SMNT, G_NONE, xfer_smnt, TRUE, FALSE, CL_MISC }, |
4661 | | { PRE_CMD, C_RETR, G_READ, xfer_pre_retr, TRUE, FALSE }, |
4662 | | { CMD, C_RETR, G_READ, xfer_retr, TRUE, FALSE, CL_READ }, |
4663 | | { POST_CMD,C_RETR, G_NONE, xfer_post_retr,FALSE, FALSE }, |
4664 | | { LOG_CMD, C_RETR, G_NONE, xfer_log_retr, FALSE, FALSE }, |
4665 | | { LOG_CMD_ERR, C_RETR,G_NONE, xfer_err_cleanup, FALSE, FALSE }, |
4666 | | { PRE_CMD, C_STOR, G_WRITE, xfer_pre_stor, TRUE, FALSE }, |
4667 | | { CMD, C_STOR, G_WRITE, xfer_stor, TRUE, FALSE, CL_WRITE }, |
4668 | | { POST_CMD,C_STOR, G_NONE, xfer_post_stor,FALSE, FALSE }, |
4669 | | { LOG_CMD, C_STOR, G_NONE, xfer_log_stor, FALSE, FALSE }, |
4670 | | { LOG_CMD_ERR, C_STOR,G_NONE, xfer_err_cleanup, FALSE, FALSE }, |
4671 | | { PRE_CMD, C_STOU, G_WRITE, xfer_pre_stou, TRUE, FALSE }, |
4672 | | { CMD, C_STOU, G_WRITE, xfer_stor, TRUE, FALSE, CL_WRITE }, |
4673 | | { POST_CMD,C_STOU, G_WRITE, xfer_post_stou,FALSE, FALSE }, |
4674 | | { LOG_CMD, C_STOU, G_NONE, xfer_log_stor, FALSE, FALSE }, |
4675 | | { LOG_CMD_ERR, C_STOU,G_NONE, xfer_err_cleanup, FALSE, FALSE }, |
4676 | | { PRE_CMD, C_APPE, G_WRITE, xfer_pre_appe, TRUE, FALSE }, |
4677 | | { CMD, C_APPE, G_WRITE, xfer_stor, TRUE, FALSE, CL_WRITE }, |
4678 | | { POST_CMD,C_APPE, G_NONE, xfer_post_stor,FALSE, FALSE }, |
4679 | | { LOG_CMD, C_APPE, G_NONE, xfer_log_stor, FALSE, FALSE }, |
4680 | | { LOG_CMD_ERR, C_APPE,G_NONE, xfer_err_cleanup, FALSE, FALSE }, |
4681 | | { CMD, C_ABOR, G_NONE, xfer_abor, TRUE, TRUE, CL_MISC }, |
4682 | | { LOG_CMD, C_ABOR, G_NONE, xfer_log_abor, TRUE, TRUE, CL_MISC }, |
4683 | | { CMD, C_OPTS "_REST", G_NONE, xfer_opts_rest, FALSE, FALSE }, |
4684 | | { CMD, C_REST, G_NONE, xfer_rest, TRUE, FALSE, CL_MISC }, |
4685 | | { CMD, C_RANG, G_NONE, xfer_rang, TRUE, FALSE, CL_MISC }, |
4686 | | { POST_CMD,C_PROT, G_NONE, xfer_post_prot, FALSE, FALSE }, |
4687 | | { POST_CMD,C_PASS, G_NONE, xfer_post_pass, FALSE, FALSE }, |
4688 | | { 0, NULL } |
4689 | | }; |
4690 | | |
4691 | | module xfer_module = { |
4692 | | NULL, NULL, |
4693 | | |
4694 | | /* Module API version */ |
4695 | | 0x20, |
4696 | | |
4697 | | /* Module name */ |
4698 | | "xfer", |
4699 | | |
4700 | | /* Module configuration directive table */ |
4701 | | xfer_conftab, |
4702 | | |
4703 | | /* Module command handler table */ |
4704 | | xfer_cmdtab, |
4705 | | |
4706 | | /* Module authentication handler table */ |
4707 | | NULL, |
4708 | | |
4709 | | /* Module initialization function */ |
4710 | | xfer_init, |
4711 | | |
4712 | | /* Session initialization function */ |
4713 | | xfer_sess_init |
4714 | | }; |