Debian Codegraph @

contrib/mod_copy.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_copy -- a module supporting copying of files on the server
3 * without transferring the data to the client and back
4 * Copyright (c) 2009-2025 TJ Saunders
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders and other respective copyright holders
21 * give permission to link this program with OpenSSL, and distribute the
22 * resulting executable, without including the source code for OpenSSL in the
23 * source distribution.
24 *
25 * This is mod_copy, contrib software for proftpd 1.3.x and above.
26 * For more information contact TJ Saunders <[email protected]>.
27 */
28
29 #include "conf.h"
30
31 #define MOD_COPY_VERSION "mod_copy/0.6"
32
33 /* Make sure the version of proftpd is as necessary. */
34 #if PROFTPD_VERSION_NUMBER < 0x0001030401
35 # error "ProFTPD 1.3.4rc1 or later required"
36 #endif
37
38 extern pr_response_t *resp_list, *resp_err_list;
39
40 module copy_module;
41 static int copy_engine = TRUE;
42 static unsigned long copy_opts = 0UL;
43 #define COPY_OPT_NO_DELETE_ON_FAILURE 0x0001
44
45 static const char *trace_channel = "copy";
46
47 static int copy_sess_init(void);
48
49 /* These are copied largely from src/mkhome.c */
50
51 static int create_dir(const char *dir) {
52 struct stat st;
53 int res = -1;
54
55 pr_fs_clear_cache2(dir);
56 res = pr_fsio_stat(dir, &st);
57
58 if (res < 0 &&
59 errno != ENOENT) {
60 int xerrno = errno;
61
62 pr_log_pri(PR_LOG_WARNING, MOD_COPY_VERSION ": error checking '%s': %s",
63 dir, strerror(xerrno));
64
65 errno = xerrno;
66 return -1;
67 }
68
69 /* The directory already exists. */
70 if (res == 0) {
71 pr_trace_msg(trace_channel, 9, "path '%s' already exists", dir);
72 return 1;
73 }
74
75 if (pr_fsio_mkdir(dir, 0777) < 0) {
76 int xerrno = errno;
77
78 pr_log_pri(PR_LOG_WARNING, MOD_COPY_VERSION ": error creating '%s': %s",
79 dir, strerror(xerrno));
80
81 errno = xerrno;
82 return -1;
83 }
84
85 pr_log_debug(DEBUG6, MOD_COPY_VERSION ": directory '%s' created", dir);
86 return 0;
87 }
88
89 static int create_path(pool *p, const char *path) {
90 struct stat st;
91 char *curr_path, *dup_path;
92
93 pr_fs_clear_cache2(path);
94 if (pr_fsio_stat(path, &st) == 0) {
95 return 0;
96 }
97
98 dup_path = pstrdup(p, path);
99
100 curr_path = "/";
101 while (dup_path &&
102 *dup_path) {
103 char *curr_dir;
104 int res;
105 cmd_rec *cmd;
106 pool *sub_pool;
107
108 pr_signals_handle();
109
110 curr_dir = strsep(&dup_path, "/");
111 curr_path = pdircat(p, curr_path, curr_dir, NULL);
112
113 /* Dispatch fake C_MKD command, e.g. for mod_quotatab */
114 sub_pool = pr_pool_create_sz(p, 64);
115 cmd = pr_cmd_alloc(sub_pool, 2, pstrdup(sub_pool, C_MKD),
116 pstrdup(sub_pool, curr_path));
117 cmd->arg = pstrdup(cmd->pool, curr_path);
118 cmd->cmd_class = CL_DIRS|CL_WRITE;
119
120 pr_response_clear(&resp_list);
121 pr_response_clear(&resp_err_list);
122
123 res = pr_cmd_dispatch_phase(cmd, PRE_CMD, 0);
124 if (res < 0) {
125 int xerrno = errno;
126
127 pr_log_debug(DEBUG3, MOD_COPY_VERSION
128 ": creating directory '%s' blocked by MKD handler: %s", curr_path,
129 strerror(xerrno));
130
131 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
132 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
133 pr_response_clear(&resp_err_list);
134
135 destroy_pool(sub_pool);
136
137 errno = xerrno;
138 return -1;
139 }
140
141 res = create_dir(curr_path);
142 if (res < 0) {
143 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
144 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
145 pr_response_clear(&resp_err_list);
146
147 destroy_pool(sub_pool);
148 return -1;
149 }
150
151 pr_cmd_dispatch_phase(cmd, POST_CMD, 0);
152 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
153 pr_response_clear(&resp_list);
154 destroy_pool(sub_pool);
155 }
156
157 return 0;
158 }
159
160 static int copy_symlink(pool *p, const char *src_path, const char *dst_path,
161 int flags) {
162 char *link_path = pcalloc(p, PR_TUNABLE_BUFFER_SIZE);
163 int len;
164
165 len = pr_fsio_readlink(src_path, link_path, PR_TUNABLE_BUFFER_SIZE-1);
166 if (len < 0) {
167 int xerrno = errno;
168
169 pr_log_pri(PR_LOG_WARNING, MOD_COPY_VERSION ": error reading link '%s': %s",
170 src_path, strerror(xerrno));
171
172 errno = xerrno;
173 return -1;
174 }
175 link_path[len] = '\0';
176
177 if (pr_fsio_symlink(link_path, dst_path) < 0) {
178 int xerrno = errno;
179
180 pr_log_pri(PR_LOG_WARNING, MOD_COPY_VERSION
181 ": error symlinking '%s' to '%s': %s", link_path, dst_path,
182 strerror(xerrno));
183
184 errno = xerrno;
185 return -1;
186 }
187
188 return 0;
189 }
190
191 static int copy_dir(pool *p, const char *src_dir, const char *dst_dir,
192 int flags) {
193 DIR *dh = NULL;
194 struct dirent *dent = NULL;
195 int res = 0;
196 pool *iter_pool = NULL;
197
198 dh = opendir(src_dir);
199 if (dh == NULL) {
200 pr_log_pri(PR_LOG_WARNING, MOD_COPY_VERSION
201 ": error reading directory '%s': %s", src_dir, strerror(errno));
202 return -1;
203 }
204
205 while ((dent = readdir(dh)) != NULL) {
206 struct stat st;
207 char *src_path, *dst_path;
208
209 pr_signals_handle();
210
211 /* Skip "." and ".." */
212 if (strncmp(dent->d_name, ".", 2) == 0 ||
213 strncmp(dent->d_name, "..", 3) == 0) {
214 continue;
215 }
216
217 if (iter_pool != NULL) {
218 destroy_pool(iter_pool);
219 }
220
221 iter_pool = pr_pool_create_sz(p, 128);
222 src_path = pdircat(iter_pool, src_dir, dent->d_name, NULL);
223 dst_path = pdircat(iter_pool, dst_dir, dent->d_name, NULL);
224
225 if (pr_fsio_lstat(src_path, &st) < 0) {
226 pr_log_debug(DEBUG3, MOD_COPY_VERSION
227 ": unable to stat '%s' (%s), skipping", src_path, strerror(errno));
228 continue;
229 }
230
231 /* Is this path to a directory? */
232 if (S_ISDIR(st.st_mode)) {
233 if (create_path(iter_pool, dst_path) < 0) {
234 res = -1;
235 break;
236 }
237
238 if (copy_dir(iter_pool, src_path, dst_path, flags) < 0) {
239 res = -1;
240 break;
241 }
242 continue;
243
244 /* Is this path to a regular file? */
245 } else if (S_ISREG(st.st_mode)) {
246 cmd_rec *cmd;
247
248 /* Dispatch fake COPY command, e.g. for mod_quotatab */
249 cmd = pr_cmd_alloc(iter_pool, 4, pstrdup(iter_pool, "SITE"),
250 pstrdup(iter_pool, "COPY"), pstrdup(iter_pool, src_path),
251 pstrdup(iter_pool, dst_path));
252 cmd->arg = pstrcat(iter_pool, "COPY ", src_path, " ", dst_path, NULL);
253 cmd->cmd_class = CL_WRITE;
254
255 pr_response_clear(&resp_list);
256 pr_response_clear(&resp_err_list);
257
258 if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
259 int xerrno = errno;
260
261 pr_log_debug(DEBUG3, MOD_COPY_VERSION
262 ": COPY of '%s' to '%s' blocked by COPY handler: %s", src_path,
263 dst_path, strerror(xerrno));
264
265 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
266 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
267 pr_response_clear(&resp_err_list);
268
269 errno = xerrno;
270 res = -1;
271 break;
272
273 } else {
274 if (pr_fs_copy_file2(src_path, dst_path, flags, NULL) < 0) {
275 int xerrno = errno;
276
277 pr_log_debug(DEBUG7, MOD_COPY_VERSION
278 ": error copying file '%s' to '%s': %s", src_path, dst_path,
279 strerror(xerrno));
280
281 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
282 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
283 pr_response_clear(&resp_err_list);
284
285 errno = xerrno;
286 res = -1;
287 break;
288
289 } else {
290 char *abs_path;
291
292 pr_cmd_dispatch_phase(cmd, POST_CMD, 0);
293 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
294 pr_response_clear(&resp_list);
295
296 /* Write a TransferLog entry as well. */
297
298 pr_fs_clear_cache2(dst_path);
299 pr_fsio_stat(dst_path, &st);
300
301 abs_path = dir_abs_path(p, dst_path, TRUE);
302
303 if (session.sf_flags & SF_ANON) {
304 xferlog_write(0, session.c->remote_name, st.st_size, abs_path,
305 (session.sf_flags & SF_ASCII ? 'a' : 'b'), 'd', 'a',
306 session.anon_user, 'c', "_");
307
308 } else {
309 xferlog_write(0, session.c->remote_name, st.st_size, abs_path,
310 (session.sf_flags & SF_ASCII ? 'a' : 'b'), 'd', 'r',
311 session.user, 'c', "_");
312 }
313 }
314 }
315
316 continue;
317
318 /* Is this path a symlink? */
319 } else if (S_ISLNK(st.st_mode)) {
320 if (copy_symlink(iter_pool, src_path, dst_path, flags) < 0) {
321 res = -1;
322 break;
323 }
324 continue;
325
326 /* All other file types are skipped */
327 } else {
328 pr_log_debug(DEBUG3, MOD_COPY_VERSION ": skipping supported file '%s'",
329 src_path);
330 continue;
331 }
332 }
333
334 if (iter_pool != NULL) {
335 destroy_pool(iter_pool);
336 }
337
338 closedir(dh);
339 return res;
340 }
341
342 static int copy_paths(pool *p, const char *from, const char *to) {
343 struct stat st;
344 int res, flags = 0;
345 xaset_t *set;
346
347 set = get_dir_ctxt(p, (char *) to);
348 res = pr_filter_allow_path(set, to);
349 switch (res) {
350 case 0:
351 break;
352
353 case PR_FILTER_ERR_FAILS_ALLOW_FILTER:
354 pr_log_pri(PR_LOG_NOTICE, MOD_COPY_VERSION
355 ": path '%s' denied by PathAllowFilter", to);
356 errno = EPERM;
357 return -1;
358
359 case PR_FILTER_ERR_FAILS_DENY_FILTER:
360 pr_log_pri(PR_LOG_NOTICE, MOD_COPY_VERSION
361 ": path '%s' denied by PathDenyFilter", to);
362 errno = EPERM;
363 return -1;
364 }
365
366 /* Check whether from is a file, a directory, a symlink, or something
367 * unsupported.
368 */
369 res = pr_fsio_lstat(from, &st);
370 if (res < 0) {
371 int xerrno = errno;
372
373 pr_log_debug(DEBUG7, MOD_COPY_VERSION ": error checking '%s': %s", from,
374 strerror(xerrno));
375
376 errno = xerrno;
377 return -1;
378 }
379
380 if (copy_opts & COPY_OPT_NO_DELETE_ON_FAILURE) {
381 flags |= PR_FSIO_COPY_FILE_FL_NO_DELETE_ON_FAILURE;
382 }
383
384 if (S_ISREG(st.st_mode)) {
385 char *abs_path;
386
387 pr_fs_clear_cache2(to);
388 res = pr_fsio_stat(to, &st);
389 if (res == 0) {
390 unsigned char *allow_overwrite;
391
392 allow_overwrite = get_param_ptr(CURRENT_CONF, "AllowOverwrite", FALSE);
393 if (allow_overwrite == NULL ||
394 *allow_overwrite == FALSE) {
395 pr_log_debug(DEBUG6,
396 MOD_COPY_VERSION ": AllowOverwrite permission denied for '%s'", to);
397 errno = EACCES;
398 return -1;
399 }
400 }
401
402 res = pr_fs_copy_file2(from, to, flags, NULL);
403 if (res < 0) {
404 int xerrno = errno;
405
406 pr_log_debug(DEBUG7, MOD_COPY_VERSION
407 ": error copying file '%s' to '%s': %s", from, to, strerror(xerrno));
408
409 errno = xerrno;
410 return -1;
411 }
412
413 pr_fs_clear_cache2(to);
414 if (pr_fsio_stat(to, &st) < 0) {
415 pr_trace_msg(trace_channel, 3,
416 "error stat'ing '%s': %s", to, strerror(errno));
417 }
418
419 /* Write a TransferLog entry as well. */
420 abs_path = dir_abs_path(p, to, TRUE);
421
422 if (session.sf_flags & SF_ANON) {
423 xferlog_write(0, session.c->remote_name, st.st_size, abs_path,
424 (session.sf_flags & SF_ASCII ? 'a' : 'b'), 'd', 'a',
425 session.anon_user, 'c', "_");
426
427 } else {
428 xferlog_write(0, session.c->remote_name, st.st_size, abs_path,
429 (session.sf_flags & SF_ASCII ? 'a' : 'b'), 'd', 'r',
430 session.user, 'c', "_");
431 }
432
433 } else if (S_ISDIR(st.st_mode)) {
434 res = create_path(p, to);
435 if (res < 0) {
436 int xerrno = errno;
437
438 pr_log_debug(DEBUG7, MOD_COPY_VERSION
439 ": error creating path '%s': %s", to, strerror(xerrno));
440
441 errno = xerrno;
442 return -1;
443 }
444
445 res = copy_dir(p, from, to, flags);
446 if (res < 0) {
447 int xerrno = errno;
448
449 pr_log_debug(DEBUG7, MOD_COPY_VERSION
450 ": error copying directory '%s' to '%s': %s", from, to,
451 strerror(xerrno));
452
453 errno = xerrno;
454 return -1;
455 }
456
457 } else if (S_ISLNK(st.st_mode)) {
458 pr_fs_clear_cache2(to);
459 res = pr_fsio_stat(to, &st);
460 if (res == 0) {
461 unsigned char *allow_overwrite;
462
463 allow_overwrite = get_param_ptr(CURRENT_CONF, "AllowOverwrite", FALSE);
464 if (allow_overwrite == NULL ||
465 *allow_overwrite == FALSE) {
466 pr_log_debug(DEBUG6, MOD_COPY_VERSION
467 ": AllowOverwrite permission denied for '%s'", to);
468
469 errno = EACCES;
470 return -1;
471 }
472 }
473
474 res = copy_symlink(p, from, to, flags);
475 if (res < 0) {
476 int xerrno = errno;
477
478 pr_log_debug(DEBUG7, MOD_COPY_VERSION
479 ": error copying symlink '%s' to '%s': %s", from, to, strerror(xerrno));
480
481 errno = xerrno;
482 return -1;
483 }
484
485 } else {
486 pr_log_debug(DEBUG7, MOD_COPY_VERSION
487 ": unsupported file type for '%s'", from);
488
489 errno = EINVAL;
490 return -1;
491 }
492
493 return 0;
494 }
495
496 /* Configuration handlers
497 */
498
499 /* usage: CopyEngine on|off */
500 MODRET set_copyengine(cmd_rec *cmd) {
501 int engine = -1;
502 config_rec *c;
503
504 CHECK_ARGS(cmd, 1);
505 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
506
507 engine = get_boolean(cmd, 1);
508 if (engine == -1) {
509 CONF_ERROR(cmd, "expected Boolean parameter");
510 }
511
512 c = add_config_param(cmd->argv[0], 1, NULL);
513 c->argv[0] = palloc(c->pool, sizeof(int));
514 *((int *) c->argv[0]) = engine;
515
516 return PR_HANDLED(cmd);
517 }
518
519 /* usage: CopyOptions opt1 ... */
520 MODRET set_copyoptions(cmd_rec *cmd) {
521 config_rec *c = NULL;
522 register unsigned int i = 0;
523 unsigned long opts = 0UL;
524
525 if (cmd->argc-1 == 0) {
526 CONF_ERROR(cmd, "wrong number of parameters");
527 }
528
529 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
530
531 c = add_config_param(cmd->argv[0], 1, NULL);
532
533 for (i = 1; i < cmd->argc; i++) {
534 if (strcmp(cmd->argv[i], "NoDeleteOnFailure") == 0) {
535 opts |= COPY_OPT_NO_DELETE_ON_FAILURE;
536
537 } else {
538 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown CopyOption '",
539 cmd->argv[i], "'", NULL));
540 }
541 }
542
543 c->argv[0] = pcalloc(c->pool, sizeof(unsigned long));
544 *((unsigned long *) c->argv[0]) = opts;
545
546 if (pr_module_exists("mod_ifsession.c")) {
547 /* These are needed in case this directive is used with mod_ifsession
548 * configuration.
549 */
550 c->flags |= CF_MULTI;
551 }
552
553 return PR_HANDLED(cmd);
554 }
555
556 /* Command handlers
557 */
558
559 MODRET copy_copy(cmd_rec *cmd) {
560 if (copy_engine == FALSE) {
561 return PR_DECLINED(cmd);
562 }
563
564 if (cmd->argc < 2) {
565 return PR_DECLINED(cmd);
566 }
567
568 if (strncasecmp(cmd->argv[1], "COPY", 5) == 0) {
569 char *cmd_name, *decoded_path, *from, *to;
570 unsigned char *authenticated;
571
572 if (cmd->argc != 4) {
573 return PR_DECLINED(cmd);
574 }
575
576 authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE);
577 if (authenticated == NULL ||
578 *authenticated == FALSE) {
579 pr_response_add_err(R_530, _("Please login with USER and PASS"));
580
581 pr_cmd_set_errno(cmd, EPERM);
582 errno = EPERM;
583 return PR_ERROR(cmd);
584 }
585
586 /* XXX What about paths which contain spaces? */
587
588 decoded_path = pr_fs_decode_path2(cmd->tmp_pool, cmd->argv[2],
589 FSIO_DECODE_FL_TELL_ERRORS);
590 if (decoded_path == NULL) {
591 int xerrno = errno;
592
593 pr_log_debug(DEBUG8, "'%s' failed to decode properly: %s",
594 (char *) cmd->argv[2], strerror(xerrno));
595 pr_response_add_err(R_550,
596 _("%s: Illegal character sequence in filename"), (char *) cmd->argv[2]);
597
598 pr_cmd_set_errno(cmd, xerrno);
599 errno = xerrno;
600 return PR_ERROR(cmd);
601 }
602
603 from = dir_canonical_vpath(cmd->tmp_pool, decoded_path);
604
605 decoded_path = pr_fs_decode_path2(cmd->tmp_pool, cmd->argv[3],
606 FSIO_DECODE_FL_TELL_ERRORS);
607 if (decoded_path == NULL) {
608 int xerrno = errno;
609
610 pr_log_debug(DEBUG8, "'%s' failed to decode properly: %s",
611 (char *) cmd->argv[3], strerror(xerrno));
612 pr_response_add_err(R_550,
613 _("%s: Illegal character sequence in filename"), (char *) cmd->argv[3]);
614
615 pr_cmd_set_errno(cmd, xerrno);
616 errno = xerrno;
617 return PR_ERROR(cmd);
618 }
619
620 to = dir_canonical_vpath(cmd->tmp_pool, decoded_path);
621
622 cmd_name = cmd->argv[0];
623 pr_cmd_set_name(cmd, "SITE_COPY");
624 if (!dir_check(cmd->tmp_pool, cmd, G_WRITE, to, NULL)) {
625 int xerrno = EPERM;
626
627 pr_cmd_set_name(cmd, cmd_name);
628 pr_log_debug(DEBUG8, "%s denied by <Limit> configuration",
629 (char *) cmd->argv[0]);
630 pr_response_add_err(R_550, "%s: %s", (char *) cmd->argv[3],
631 strerror(xerrno));
632
633 pr_cmd_set_errno(cmd, xerrno);
634 errno = xerrno;
635 return PR_ERROR(cmd);
636 }
637 pr_cmd_set_name(cmd, cmd_name);
638
639 if (copy_paths(cmd->tmp_pool, from, to) < 0) {
640 int xerrno = errno;
641
642 pr_log_debug(DEBUG7, MOD_COPY_VERSION
643 ": error copying '%s' to '%s': %s", from, to, strerror(xerrno));
644
645 pr_response_add_err(R_550, "%s: %s", (char *) cmd->argv[1],
646 strerror(xerrno));
647
648 pr_cmd_set_errno(cmd, xerrno);
649 errno = xerrno;
650 return PR_ERROR(cmd);
651 }
652
653 pr_response_add(R_200, _("SITE %s command successful"),
654 (char *) cmd->argv[1]);
655 return PR_HANDLED(cmd);
656 }
657
658 if (strncasecmp(cmd->argv[1], "HELP", 5) == 0) {
659 pr_response_add(R_214, _("CPFR <sp> pathname"));
660 pr_response_add(R_214, _("CPTO <sp> pathname"));
661 }
662
663 return PR_DECLINED(cmd);
664 }
665
666 MODRET copy_cpfr(cmd_rec *cmd) {
667 register unsigned int i;
668 int res;
669 char *cmd_name, *path = "";
670 unsigned char *authenticated = NULL;
671
672 if (copy_engine == FALSE) {
673 return PR_DECLINED(cmd);
674 }
675
676 if (cmd->argc < 3 ||
677 strncasecmp(cmd->argv[1], "CPFR", 5) != 0) {
678 return PR_DECLINED(cmd);
679 }
680
681 authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE);
682 if (authenticated == NULL ||
683 *authenticated == FALSE) {
684 pr_response_add_err(R_530, _("Please login with USER and PASS"));
685
686 pr_cmd_set_errno(cmd, EPERM);
687 errno = EPERM;
688 return PR_ERROR(cmd);
689 }
690
691 CHECK_CMD_MIN_ARGS(cmd, 3);
692
693 /* Construct the target file name by concatenating all the parameters after
694 * the "SITE CPFR", separating them with spaces.
695 */
696 for (i = 2; i <= cmd->argc-1; i++) {
697 char *decoded_path;
698
699 decoded_path = pr_fs_decode_path2(cmd->tmp_pool, cmd->argv[i],
700 FSIO_DECODE_FL_TELL_ERRORS);
701 if (decoded_path == NULL) {
702 int xerrno = errno;
703
704 pr_log_debug(DEBUG8, "'%s' failed to decode properly: %s",
705 (char *) cmd->argv[i], strerror(xerrno));
706 pr_response_add_err(R_550,
707 _("%s: Illegal character sequence in filename"), cmd->arg);
708
709 pr_cmd_set_errno(cmd, xerrno);
710 errno = xerrno;
711 return PR_ERROR(cmd);
712 }
713
714 path = pstrcat(cmd->tmp_pool, path, *path ? " " : "", decoded_path, NULL);
715 }
716
717 cmd_name = cmd->argv[0];
718 pr_cmd_set_name(cmd, "SITE_CPFR");
719 if (!dir_check(cmd->tmp_pool, cmd, G_READ, path, NULL)) {
720 int xerrno = EPERM;
721
722 pr_cmd_set_name(cmd, cmd_name);
723 pr_log_debug(DEBUG8, "%s denied by <Limit> configuration",
724 (char *) cmd->argv[0]);
725 pr_response_add_err(R_550, "%s: %s", (char *) cmd->argv[3],
726 strerror(xerrno));
727
728 pr_cmd_set_errno(cmd, xerrno);
729 errno = xerrno;
730 return PR_ERROR(cmd);
731 }
732 pr_cmd_set_name(cmd, cmd_name);
733
734 res = pr_filter_allow_path(CURRENT_CONF, path);
735 switch (res) {
736 case 0:
737 break;
738
739 case PR_FILTER_ERR_FAILS_ALLOW_FILTER:
740 pr_log_pri(PR_LOG_NOTICE, MOD_COPY_VERSION
741 ": 'CPFR %s' denied by PathAllowFilter", path);
742 pr_response_add_err(R_550, _("%s: Forbidden filename"), path);
743
744 pr_cmd_set_errno(cmd, EPERM);
745 errno = EPERM;
746 return PR_ERROR(cmd);
747
748 case PR_FILTER_ERR_FAILS_DENY_FILTER:
749 pr_log_pri(PR_LOG_NOTICE, MOD_COPY_VERSION
750 ": 'CPFR %s' denied by PathDenyFilter", path);
751 pr_response_add_err(R_550, _("%s: Forbidden filename"), path);
752
753 pr_cmd_set_errno(cmd, EPERM);
754 errno = EPERM;
755 return PR_ERROR(cmd);
756 }
757
758 /* Allow renaming a symlink, even a dangling one. */
759 path = dir_canonical_vpath(cmd->tmp_pool, path);
760
761 if (!path ||
762 !dir_check_canon(cmd->tmp_pool, cmd, cmd->group, path, NULL) ||
763 !exists2(cmd->tmp_pool, path)) {
764 int xerrno = errno;
765
766 pr_response_add_err(R_550, "%s: %s", path, strerror(xerrno));
767
768 pr_cmd_set_errno(cmd, xerrno);
769 errno = xerrno;
770 return PR_ERROR(cmd);
771 }
772
773 if (pr_table_add(session.notes, "mod_copy.cpfr-path",
774 pstrdup(session.pool, path), 0) < 0) {
775 pr_trace_msg(trace_channel, 4,
776 "error adding 'mod_copy.cpfr-path' note: %s", strerror(errno));
777 }
778
779 pr_response_add(R_350,
780 _("File or directory exists, ready for destination name"));
781 return PR_HANDLED(cmd);
782 }
783
784 MODRET copy_cpto(cmd_rec *cmd) {
785 register unsigned int i;
786 const char *from, *to = "";
787 char *cmd_name;
788 unsigned char *authenticated = NULL;
789
790 if (copy_engine == FALSE) {
791 return PR_DECLINED(cmd);
792 }
793
794 if (cmd->argc < 3 ||
795 strncasecmp(cmd->argv[1], "CPTO", 5) != 0) {
796 return PR_DECLINED(cmd);
797 }
798
799 authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE);
800 if (authenticated == NULL ||
801 *authenticated == FALSE) {
802 pr_response_add_err(R_530, _("Please login with USER and PASS"));
803
804 pr_cmd_set_errno(cmd, EPERM);
805 errno = EPERM;
806 return PR_ERROR(cmd);
807 }
808
809 CHECK_CMD_MIN_ARGS(cmd, 3);
810
811 from = pr_table_get(session.notes, "mod_copy.cpfr-path", NULL);
812 if (from == NULL) {
813 pr_response_add_err(R_503, _("Bad sequence of commands"));
814
815 pr_cmd_set_errno(cmd, EPERM);
816 errno = EPERM;
817 return PR_ERROR(cmd);
818 }
819
820 /* Construct the target file name by concatenating all the parameters after
821 * the "SITE CPTO", separating them with spaces.
822 */
823 for (i = 2; i <= cmd->argc-1; i++) {
824 char *decoded_path;
825
826 decoded_path = pr_fs_decode_path2(cmd->tmp_pool, cmd->argv[i],
827 FSIO_DECODE_FL_TELL_ERRORS);
828 if (decoded_path == NULL) {
829 int xerrno = errno;
830
831 pr_log_debug(DEBUG8, "'%s' failed to decode properly: %s",
832 (char *) cmd->argv[i], strerror(xerrno));
833 pr_response_add_err(R_550,
834 _("%s: Illegal character sequence in filename"), cmd->arg);
835
836 pr_cmd_set_errno(cmd, xerrno);
837 errno = xerrno;
838 return PR_ERROR(cmd);
839 }
840
841 to = pstrcat(cmd->tmp_pool, to, *to ? " " : "", decoded_path, NULL);
842 }
843
844 to = dir_canonical_vpath(cmd->tmp_pool, to);
845
846 cmd_name = cmd->argv[0];
847 pr_cmd_set_name(cmd, "SITE_CPTO");
848 if (!dir_check(cmd->tmp_pool, cmd, G_WRITE, to, NULL)) {
849 int xerrno = EPERM;
850
851 pr_cmd_set_name(cmd, cmd_name);
852 pr_log_debug(DEBUG8, "%s denied by <Limit> configuration",
853 (char *) cmd->argv[0]);
854 pr_response_add_err(R_550, "%s: %s", to, strerror(xerrno));
855
856 pr_cmd_set_errno(cmd, xerrno);
857 errno = xerrno;
858 return PR_ERROR(cmd);
859 }
860 pr_cmd_set_name(cmd, cmd_name);
861
862 if (copy_paths(cmd->tmp_pool, from, to) < 0) {
863 int xerrno = errno;
864 const char *err_code = R_550;
865
866 pr_log_debug(DEBUG7, MOD_COPY_VERSION
867 ": error copying '%s' to '%s': %s", from, to, strerror(xerrno));
868
869 /* Check errno for EDQOUT (or the most appropriate alternative).
870 * (I hate the fact that FTP has a special response code just for
871 * this, and that clients actually expect it. Special cases are
872 * stupid.)
873 */
874 switch (xerrno) {
875 #if defined(EDQUOT)
876 case EDQUOT:
877 #endif /* EDQUOT */
878 #if defined(EFBIG)
879 case EFBIG:
880 #endif /* EFBIG */
881 #if defined(ENOSPC)
882 case ENOSPC:
883 #endif /* ENOSPC */
884 err_code = R_552;
885 break;
886
887 default:
888 err_code = R_550;
889 break;
890 }
891
892 pr_response_add_err(err_code, "%s: %s", (char *) cmd->argv[1],
893 strerror(xerrno));
894
895 pr_cmd_set_errno(cmd, xerrno);
896 errno = xerrno;
897 return PR_ERROR(cmd);
898 }
899
900 pr_response_add(R_250, "%s", _("Copy successful"));
901 return PR_HANDLED(cmd);
902 }
903
904 MODRET copy_log_site(cmd_rec *cmd) {
905 if (copy_engine == FALSE) {
906 return PR_DECLINED(cmd);
907 }
908
909 if (cmd->argc < 3 ||
910 strncasecmp(cmd->argv[1], "CPTO", 5) != 0) {
911 return PR_DECLINED(cmd);
912 }
913
914 /* Delete the stashed CPFR path from the session.notes table. */
915 (void) pr_table_remove(session.notes, "mod_copy.cpfr-path", NULL);
916
917 return PR_DECLINED(cmd);
918 }
919
920 MODRET copy_post_pass(cmd_rec *cmd) {
921 config_rec *c;
922
923 if (copy_engine == FALSE) {
924 return PR_DECLINED(cmd);
925 }
926
927 /* The CopyEngine directive may have been changed for this user by
928 * e.g. mod_ifsession, thus we check again.
929 */
930 c = find_config(main_server->conf, CONF_PARAM, "CopyEngine", FALSE);
931 if (c != NULL) {
932 copy_engine = *((int *) c->argv[0]);
933 }
934
935 if (copy_engine == FALSE) {
936 return PR_DECLINED(cmd);
937 }
938
939 c = find_config(main_server->conf, CONF_PARAM, "CopyOptions", FALSE);
940 while (c != NULL) {
941 unsigned long opts = 0;
942
943 pr_signals_handle();
944
945 opts = *((unsigned long *) c->argv[0]);
946 copy_opts |= opts;
947
948 c = find_config_next(c, c->next, CONF_PARAM, "CopyOptions", FALSE);
949 }
950
951 return PR_DECLINED(cmd);
952 }
953
954 /* Initialization functions
955 */
956
957 static int copy_sess_init(void) {
958 config_rec *c;
959
960 c = find_config(main_server->conf, CONF_PARAM, "CopyEngine", FALSE);
961 if (c != NULL) {
962 copy_engine = *((int *) c->argv[0]);
963 }
964
965 if (copy_engine == FALSE) {
966 return 0;
967 }
968
969 /* Advertise support for the SITE command */
970 pr_feat_add("SITE COPY");
971 return 0;
972 }
973
974 /* Module API tables
975 */
976
977 static conftable copy_conftab[] = {
978 { "CopyEngine", set_copyengine, NULL },
979 { "CopyOptions", set_copyoptions, NULL },
980
981 { NULL }
982 };
983
984 static cmdtable copy_cmdtab[] = {
985 { CMD, C_SITE, G_WRITE, copy_copy, FALSE, FALSE, CL_MISC },
986 { CMD, C_SITE, G_READ, copy_cpfr, FALSE, FALSE, CL_MISC },
987 { CMD, C_SITE, G_WRITE, copy_cpto, FALSE, FALSE, CL_MISC },
988 { POST_CMD, C_PASS, G_NONE, copy_post_pass, FALSE, FALSE },
989 { LOG_CMD, C_SITE, G_NONE, copy_log_site, FALSE, FALSE },
990 { LOG_CMD_ERR, C_SITE, G_NONE, copy_log_site, FALSE, FALSE },
991
992 { 0, NULL }
993 };
994
995 module copy_module = {
996 NULL, NULL,
997
998 /* Module API version 2.0 */
999 0x20,
1000
1001 /* Module name */
1002 "copy",
1003
1004 /* Module configuration handler table */
1005 copy_conftab,
1006
1007 /* Module command handler table */
1008 copy_cmdtab,
1009
1010 /* Module authentication handler table */
1011 NULL,
1012
1013 /* Module initialization function */
1014 NULL,
1015
1016 /* Session initialization function */
1017 copy_sess_init,
1018
1019 /* Module version */
1020 MOD_COPY_VERSION
1021 };