1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
|
/*
* ProFTPD - mod_tar
* Copyright (c) 2009-2025 TJ Saunders
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*
* $Libraries: -larchive -lz -lbz2$
*/
#include "conf.h"
#include "privs.h"
#include <archive.h>
#include <archive_entry.h>
#define MOD_TAR_VERSION "mod_tar/0.4"
/* Make sure the version of proftpd is as necessary. */
#if PROFTPD_VERSION_NUMBER < 0x0001030101
# error "ProFTPD 1.3.1rc1 or later required"
#endif
module tar_module;
/* Necessary prototype. */
static void tar_exit_ev(const void *, void *);
static int tar_engine = FALSE;
static int tar_logfd = -1;
static unsigned long tar_opts = 0UL;
#define TAR_OPT_DEREF_SYMLINKS 0x001
#define TAR_ARCHIVE_FL_USE_GZIP 0x001
#define TAR_ARCHIVE_FL_USE_BZIP2 0x002
#define TAR_ARCHIVE_FL_USE_USTAR 0x004
#define TAR_ARCHIVE_FL_USE_PAX 0x008
#define TAR_ARCHIVE_FL_USE_ZIP 0x010
static const char *tar_tmp_path = "./";
static char *tar_tmp_file = NULL;
struct archive_data {
const char *path;
pr_fh_t *fh;
};
static const char *trace_channel = "tar";
static int append_data(pool *p, struct archive *tar,
struct archive_entry *entry, char *path, struct stat *st) {
pool *tmp_pool;
pr_fh_t *fh;
void *buf;
size_t buflen;
int res;
struct stat pst;
fh = pr_fsio_open(path, O_RDONLY);
if (fh == NULL) {
int xerrno = errno;
pr_trace_msg(trace_channel, 3, "unable to read '%s': %s", path,
strerror(xerrno));
errno = xerrno;
return -1;
}
res = pr_fsio_fstat(fh, &pst);
if (res < 0) {
int xerrno = errno;
pr_trace_msg(trace_channel, 3, "unable to stat '%s': %s", path,
strerror(xerrno));
pr_fsio_close(fh);
errno = xerrno;
return -1;
}
if (S_ISDIR(pst.st_mode)) {
int xerrno = EISDIR;
pr_trace_msg(trace_channel, 3, "unable to use '%s': %s", path,
strerror(xerrno));
pr_fsio_close(fh);
errno = xerrno;
return -1;
}
tmp_pool = make_sub_pool(p);
/* Use a buffer size based on the filesystem blocksize, for better IO. */
buflen = st->st_blksize;
buf = palloc(tmp_pool, buflen);
res = pr_fsio_read(fh, buf, buflen);
while (res > 0) {
pr_signals_handle();
if (archive_write_data(tar, buf, res) < 0) {
int xerrno;
xerrno = archive_errno(tar);
pr_trace_msg(trace_channel, 3, "error adding data to archive: %s",
archive_error_string(tar));
destroy_pool(tmp_pool);
pr_fsio_close(fh);
errno = xerrno;
return -1;
}
res = pr_fsio_read(fh, buf, buflen);
}
destroy_pool(tmp_pool);
pr_fsio_close(fh);
return 0;
}
static int append_file(pool *p, struct archive *tar,
struct archive_entry *entry, char *real_name, char *save_name) {
struct stat st;
int res;
if (!(tar_opts & TAR_OPT_DEREF_SYMLINKS)) {
res = pr_fsio_lstat(real_name, &st);
} else {
res = pr_fsio_stat(real_name, &st);
}
if (res < 0) {
int xerrno = errno;
pr_trace_msg(trace_channel, 9, "error stat'ing '%s': %s", real_name,
strerror(xerrno));
errno = xerrno;
return -1;
}
archive_entry_clear(entry);
archive_entry_copy_stat(entry, &st);
archive_entry_copy_pathname(entry, save_name);
if (S_ISLNK(st.st_mode)) {
int i;
char path[PR_TUNABLE_PATH_MAX+1];
i = readlink(real_name, path, sizeof(path)-1);
if (i == -1)
return -1;
if (i >= PR_TUNABLE_PATH_MAX) {
i = PR_TUNABLE_PATH_MAX - 1;
}
path[i] = '\0';
pr_trace_msg(trace_channel, 15,
"setting destination path '%s' for symlink '%s'", path, real_name);
archive_entry_set_symlink(entry, path);
}
res = archive_write_header(tar, entry);
if (res != ARCHIVE_OK) {
int xerrno;
xerrno = archive_errno(tar);
pr_trace_msg(trace_channel, 3, "error writing archive entry header: %s",
archive_error_string(tar));
errno = xerrno;
return -1;
}
/* If it's a regular file, write the contents as well */
if (S_ISREG(st.st_mode)) {
if (append_data(p, tar, entry, real_name, &st) < 0) {
return -1;
}
}
res = archive_write_finish_entry(tar);
if (res != ARCHIVE_OK) {
int xerrno;
xerrno = archive_errno(tar);
pr_trace_msg(trace_channel, 3, "error finishing archive entry: %s",
archive_error_string(tar));
errno = xerrno;
return -1;
}
return 0;
}
static int append_tree(pool *p, struct archive *tar,
struct archive_entry *entry, char *real_dir, char *save_dir) {
char real_path[PR_TUNABLE_PATH_MAX+1];
char save_path[PR_TUNABLE_PATH_MAX+1];
struct dirent *dent;
DIR *dirh;
struct stat st;
int res;
res = append_file(p, tar, entry, real_dir, save_dir);
if (res < 0) {
return -1;
}
dirh = opendir(real_dir);
if (dirh == NULL) {
if (errno == ENOTDIR) {
return 0;
}
return -1;
}
while ((dent = readdir(dirh)) != NULL) {
pr_signals_handle();
if (strncmp(dent->d_name, ".", 2) == 0 ||
strncmp(dent->d_name, "..", 3) == 0) {
continue;
}
memset(real_path, '\0', sizeof(real_path));
snprintf(real_path, sizeof(real_path)-1, "%s/%s", real_dir, dent->d_name);
if (save_dir) {
memset(save_path, '\0', sizeof(save_path));
snprintf(save_path, sizeof(save_path)-1, "%s/%s", save_dir, dent->d_name);
}
if (!(tar_opts & TAR_OPT_DEREF_SYMLINKS)) {
res = pr_fsio_lstat(real_path, &st);
} else {
res = pr_fsio_stat(real_path, &st);
}
if (res < 0) {
int xerrno = errno;
(void) closedir(dirh);
errno = xerrno;
return -1;
}
if (S_ISDIR(st.st_mode)) {
res = append_tree(p, tar, entry, real_path,
(save_dir ? save_path : NULL));
if (res < 0) {
int xerrno = errno;
(void) closedir(dirh);
errno = xerrno;
return -1;
}
continue;
}
res = append_file(p, tar, entry, real_path, (save_dir ? save_path : NULL));
if (res < 0) {
int xerrno = errno;
(void) closedir(dirh);
errno = xerrno;
return -1;
}
}
closedir(dirh);
return 0;
}
static int tar_archive_open_cb(struct archive *tar, void *user_data) {
struct archive_data *tar_data;
pr_fh_t *fh;
tar_data = user_data;
fh = pr_fsio_open(tar_data->path, O_WRONLY|O_CREAT);
if (fh == NULL) {
return ARCHIVE_FATAL;
}
/* Override the default 0666 mode that pr_fsio_open() uses. */
if (pr_fsio_fchmod(fh, 0644) < 0) {
pr_trace_msg(trace_channel, 3, "error setting mode on '%s' to 0644: %s",
tar_data->path, strerror(errno));
}
tar_data->fh = fh;
return ARCHIVE_OK;
}
static ssize_t tar_archive_write_cb(struct archive *tar, void *user_data,
const void *buf, size_t buflen) {
struct archive_data *tar_data;
tar_data = user_data;
return pr_fsio_write(tar_data->fh, buf, buflen);
}
static int tar_archive_close_cb(struct archive *tar, void *user_data) {
struct archive_data *tar_data;
int res;
tar_data = user_data;
res = pr_fsio_close(tar_data->fh);
if (res < 0) {
return ARCHIVE_FATAL;
}
tar_data->fh = NULL;
return ARCHIVE_OK;
}
static int tar_create_archive(pool *p, char *dst_file, unsigned long blksize,
char *src_path, char *src_dir, unsigned long flags) {
struct archive_data *tar_data;
struct archive *tar;
struct archive_entry *entry;
int res;
tar = archive_write_new();
if (tar == NULL) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error allocating new archive handle: %s", archive_error_string(tar));
errno = archive_errno(tar);
return -1;
}
/* Call archive_write_set_bytes_per_block() there, so that the optimal
* block size for writing data out to the archive file is used.
*
* Sadly, the libarchive API uses an int for the block size, not an
* unsigned long, size_t, or off_t. Why even allow a signed data type
* for that parameter?
*
* NOTE: The `tar' program provided by libarchive defaults to a value
* of (20 * 512) for the bytes_per_block value; perhaps we should
* use that, too?
*/
archive_write_set_bytes_per_block(tar, blksize);
if (flags & TAR_ARCHIVE_FL_USE_USTAR) {
res = archive_write_set_format_ustar(tar);
if (res != ARCHIVE_OK) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error configuring archive handle for ustar format: %s",
archive_error_string(tar));
errno = archive_errno(tar);
return -1;
}
} else if (flags & TAR_ARCHIVE_FL_USE_ZIP) {
res = archive_write_set_format_zip(tar);
if (res != ARCHIVE_OK) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error configuring archive handle for zip format: %s",
archive_error_string(tar));
errno = archive_errno(tar);
return -1;
}
}
if (flags & TAR_ARCHIVE_FL_USE_GZIP) {
res = archive_write_add_filter_gzip(tar);
if (res != ARCHIVE_OK) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error configuring archive handle for gzip compression: %s",
archive_error_string(tar));
errno = archive_errno(tar);
return -1;
}
pr_trace_msg(trace_channel, 9, "using gzip compression for '%s'", src_path);
} else if (flags & TAR_ARCHIVE_FL_USE_BZIP2) {
res = archive_write_add_filter_bzip2(tar);
if (res != ARCHIVE_OK) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error configuring archive handle for bzip2 compression: %s",
archive_error_string(tar));
errno = archive_errno(tar);
return -1;
}
pr_trace_msg(trace_channel, 9, "using bzip2 compression for '%s'",
src_path);
} else {
res = archive_write_add_filter_none(tar);
if (res != ARCHIVE_OK) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error configuring archive handle for no compression: %s",
archive_error_string(tar));
errno = archive_errno(tar);
return -1;
}
}
tar_data = palloc(p, sizeof(struct archive_data));
tar_data->path = dst_file;
/* Allocate a new archive_entry to use for adding all entries to the
* archive. This avoid creating/destroying an archive_entry object per
* file.
*/
entry = archive_entry_new();
if (tar == NULL) {
int xerrno;
xerrno = archive_errno(tar);
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error allocating new archive entry handle: %s",
archive_error_string(tar));
archive_write_free(tar);
errno = xerrno;
return -1;
}
res = archive_write_open(tar, tar_data, tar_archive_open_cb,
tar_archive_write_cb, tar_archive_close_cb);
if (res != ARCHIVE_OK) {
int xerrno;
xerrno = archive_errno(tar);
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error opening archive handle for file '%s': %s", dst_file,
archive_error_string(tar));
archive_entry_free(entry);
archive_write_free(tar);
(void) unlink(dst_file);
errno = xerrno;
return -1;
}
if (append_tree(p, tar, entry, src_path, src_dir) < 0) {
int xerrno = errno;
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error appending '%s' to tar file: %s", src_path, strerror(xerrno));
archive_entry_free(entry);
(void) archive_write_close(tar);
archive_write_free(tar);
(void) unlink(dst_file);
errno = xerrno;
return -1;
}
archive_entry_free(entry);
res = archive_write_close(tar);
if (res < 0) {
int xerrno;
xerrno = archive_errno(tar);
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error writing tar file: %s", archive_error_string(tar));
archive_write_free(tar);
(void) unlink(dst_file);
errno = xerrno;
return -1;
}
archive_write_free(tar);
return 0;
}
static char *tar_get_ext_tar(char *path, size_t path_len) {
if (path_len < 4) {
return NULL;
}
if (path[path_len-4] == '.') {
if ((path[path_len-3] == 'T' || path[path_len-3] == 't') &&
(path[path_len-2] == 'A' || path[path_len-2] == 'a') &&
(path[path_len-1] == 'R' || path[path_len-1] == 'r')) {
return &path[path_len-4];
}
}
return NULL;
}
static char *tar_get_ext_tgz(char *path, size_t path_len) {
if (path_len < 4) {
return NULL;
}
if (path[path_len-4] == '.') {
if ((path[path_len-3] == 'T' || path[path_len-3] == 't') &&
(path[path_len-2] == 'G' || path[path_len-2] == 'g') &&
(path[path_len-1] == 'Z' || path[path_len-1] == 'z')) {
return &path[path_len-4];
}
}
return NULL;
}
static char *tar_get_ext_targz(char *path, size_t path_len) {
if (path_len < 7) {
return NULL;
}
if (path[path_len-7] == '.') {
if ((path[path_len-6] == 'T' || path[path_len-6] == 't') &&
(path[path_len-5] == 'A' || path[path_len-5] == 'a') &&
(path[path_len-4] == 'R' || path[path_len-4] == 'r') &&
path[path_len-3] == '.' &&
(path[path_len-2] == 'G' || path[path_len-2] == 'g') &&
(path[path_len-1] == 'z' || path[path_len-1] == 'z')) {
return &path[path_len-7];
}
return NULL;
}
return NULL;
}
static char *tar_get_ext_tbz2(char *path, size_t path_len) {
if (path_len < 5) {
return NULL;
}
if (path[path_len-5] == '.') {
if ((path[path_len-4] == 'T' || path[path_len-4] == 't') &&
(path[path_len-3] == 'B' || path[path_len-3] == 'b') &&
(path[path_len-2] == 'Z' || path[path_len-2] == 'z') &&
path[path_len-1] == '2') {
return &path[path_len-5];
}
}
return NULL;
}
static char *tar_get_ext_tarbz2(char *path, size_t path_len) {
if (path_len < 8) {
return NULL;
}
if (path[path_len-8] == '.') {
if ((path[path_len-7] == 'T' || path[path_len-7] == 't') &&
(path[path_len-6] == 'A' || path[path_len-6] == 'a') &&
(path[path_len-5] == 'R' || path[path_len-5] == 'r') &&
path[path_len-4] == '.' &&
(path[path_len-3] == 'B' || path[path_len-3] == 'b') &&
(path[path_len-2] == 'z' || path[path_len-2] == 'z') &&
path[path_len-1] == '2') {
return &path[path_len-8];
}
}
return NULL;
}
static char *tar_get_ext_zip(char *path, size_t path_len) {
if (path_len < 4) {
return NULL;
}
if (path[path_len-4] == '.') {
if ((path[path_len-3] == 'Z' || path[path_len-3] == 'z') &&
(path[path_len-2] == 'I' || path[path_len-2] == 'i') &&
(path[path_len-1] == 'P' || path[path_len-1] == 'p')) {
return &path[path_len-4];
}
}
return NULL;
}
static char *tar_get_flags(char *path, size_t path_len, unsigned long *flags) {
char *ptr;
ptr = tar_get_ext_tar(path, path_len);
if (ptr != NULL) {
*flags |= TAR_ARCHIVE_FL_USE_USTAR;
} else {
ptr = tar_get_ext_tgz(path, path_len);
if (ptr != NULL) {
*flags |= TAR_ARCHIVE_FL_USE_USTAR;
*flags |= TAR_ARCHIVE_FL_USE_GZIP;
} else {
ptr = tar_get_ext_targz(path, path_len);
if (ptr != NULL) {
*flags |= TAR_ARCHIVE_FL_USE_USTAR;
*flags |= TAR_ARCHIVE_FL_USE_GZIP;
} else {
ptr = tar_get_ext_tbz2(path, path_len);
if (ptr != NULL) {
*flags |= TAR_ARCHIVE_FL_USE_USTAR;
*flags |= TAR_ARCHIVE_FL_USE_BZIP2;
} else {
ptr = tar_get_ext_tarbz2(path, path_len);
if (ptr != NULL) {
*flags |= TAR_ARCHIVE_FL_USE_USTAR;
*flags |= TAR_ARCHIVE_FL_USE_BZIP2;
} else {
ptr = tar_get_ext_zip(path, path_len);
if (ptr != NULL) {
*flags |= TAR_ARCHIVE_FL_USE_ZIP;
}
}
}
}
}
}
if (*flags == 0) {
return NULL;
}
return ptr;
}
/* Configuration handlers
*/
/* usage: TarEnable on|off */
MODRET set_tarenable(cmd_rec *cmd) {
int enabled = -1;
config_rec *c;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_DIR|CONF_DYNDIR);
enabled = get_boolean(cmd, 1);
if (enabled == -1) {
CONF_ERROR(cmd, "expected Boolean parameter");
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = palloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = enabled;
return PR_HANDLED(cmd);
}
/* usage: TarEngine on|off */
MODRET set_tarengine(cmd_rec *cmd) {
int engine = -1;
config_rec *c;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
engine = get_boolean(cmd, 1);
if (engine == -1) {
CONF_ERROR(cmd, "expected Boolean parameter");
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = palloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = engine;
c->flags |= CF_MERGEDOWN;
return PR_HANDLED(cmd);
}
/* usage: TarLog path|"none" */
MODRET set_tarlog(cmd_rec *cmd) {
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
return PR_HANDLED(cmd);
}
/* usage: TarOptions opt1 opt2 ... */
MODRET set_taroptions(cmd_rec *cmd) {
config_rec *c = NULL;
register unsigned int i = 0;
unsigned long opts = 0UL;
if (cmd->argc-1 == 0)
CONF_ERROR(cmd, "wrong number of parameters");
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
c = add_config_param(cmd->argv[0], 1, NULL);
for (i = 1; i < cmd->argc; i++) {
if (strcmp(cmd->argv[i], "FollowSymlinks") == 0 ||
strcmp(cmd->argv[i], "dereference") == 0) {
opts |= TAR_OPT_DEREF_SYMLINKS;
} else {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown TarOption '",
cmd->argv[i], "'", NULL));
}
}
c->argv[0] = pcalloc(c->pool, sizeof(unsigned long));
*((unsigned long *) c->argv[0]) = opts;
c->flags |= CF_MERGEDOWN;
return PR_HANDLED(cmd);
}
/* usage: TarTempPath path */
MODRET set_tartemppath(cmd_rec *cmd) {
config_rec *c;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
c = add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
c->flags |= CF_MERGEDOWN;
return PR_HANDLED(cmd);
}
/* Command handlers
*/
MODRET tar_post_pass(cmd_rec *cmd) {
config_rec *c;
c = find_config(TOPLEVEL_CONF, CONF_PARAM, "TarEngine", FALSE);
if (c) {
int enable_tar = *((int *) c->argv[0]);
if (enable_tar) {
tar_engine = TRUE;
}
}
if (tar_engine) {
pr_event_register(&tar_module, "core.exit", tar_exit_ev, NULL);
c = find_config(TOPLEVEL_CONF, CONF_PARAM, "TarOptions", FALSE);
while (c != NULL) {
unsigned long opts;
pr_signals_handle();
opts = *((unsigned long *) c->argv[0]);
tar_opts |= opts;
c = find_config_next(c, c->next, CONF_PARAM, "TarOptions", FALSE);
}
c = find_config(TOPLEVEL_CONF, CONF_PARAM, "TarTempPath", FALSE);
if (c) {
tar_tmp_path = dir_canonical_path(session.pool, c->argv[0]);
if (session.chroot_path) {
size_t chroot_len;
chroot_len = strlen(session.chroot_path);
if (strncmp(tar_tmp_path, session.chroot_path, chroot_len) == 0) {
tar_tmp_path += chroot_len;
}
}
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"using '%s' as the staging directory for temporary .tar files",
tar_tmp_path);
}
}
return PR_DECLINED(cmd);
}
MODRET tar_pre_retr(cmd_rec *cmd) {
char *path, *tmp;
size_t path_len;
if (tar_engine == FALSE) {
return PR_DECLINED(cmd);
}
if (cmd->argc < 2) {
return PR_DECLINED(cmd);
}
path = pr_fs_decode_path(cmd->tmp_pool, cmd->arg);
/* If dir_realpath() returns non-NULL here, then the requested path
* exists; we should not try to handle it in that case.
*/
tmp = dir_realpath(cmd->tmp_pool, path);
if (tmp != NULL) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"path '%s' already exists, skipping", tmp);
return PR_DECLINED(cmd);
}
/* Check if the requested path ends in ".tar", ".tar.gz", ".tgz",
* or ".tar.bz2". If it does, check if the name leading up to that
* extension is a directory. If both of these conditions are met, we can
* proceed.
*/
path_len = strlen(path);
if (path_len > 4) {
char *dir, *notar_file, *ptr, *tar_file;
int fd, res;
struct stat st;
config_rec *d;
unsigned long flags = 0UL;
ptr = tar_get_flags(path, path_len, &flags);
if (ptr == NULL) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"no .tar file extension found in '%s'", path);
return PR_DECLINED(cmd);
}
*ptr = '\0';
path = dir_realpath(cmd->tmp_pool, path);
res = dir_exists(path);
if (res == 0) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"'%s' is not a directory, ignoring", path);
*ptr = '.';
return PR_DECLINED(cmd);
}
/* Check for a "$dir/.notar" file, for backward compatibility with
* wu-ftpd.
*/
notar_file = pdircat(cmd->tmp_pool, path, ".notar", NULL);
if (file_exists(notar_file)) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"'%s' present, skipping tar file of '%s' directory", notar_file,
path);
*ptr = '.';
return PR_DECLINED(cmd);
}
/* Check for "TarEnable off" for this directory. Make sure we check
* for any possible .ftpaccess files in the target directory which
* may contain a TarEnable configuration.
*/
if (pr_fsio_lstat(path, &st) == 0) {
build_dyn_config(cmd->pool, path, &st, TRUE);
}
d = dir_match_path(cmd->tmp_pool, path);
if (d) {
config_rec *c;
c = find_config(d->subset, CONF_PARAM, "TarEnable", FALSE);
if (c) {
int tar_enable;
tar_enable = *((int *) c->argv[0]);
if (tar_enable == FALSE) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"'TarEnable off' found, skipping tar file of '%s' directory", path);
*ptr = '.';
return PR_DECLINED(cmd);
}
}
} else {
pr_trace_msg(trace_channel, 9,
"no <Directory> match found for '%s'", path);
}
dir = strrchr(path, '/');
if (dir == NULL) {
dir = path;
} else {
dir++;
}
tar_file = pdircat(cmd->pool, tar_tmp_path, "XXXXXX", NULL);
fd = mkstemp(tar_file);
if (fd < 0) {
int xerrno = errno;
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error creating temporary filename using mkstemp: %s",
strerror(xerrno));
*ptr = '.';
return PR_DECLINED(cmd);
}
(void) fstat(fd, &st);
close(fd);
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"writing temporary .tar file to '%s'", tar_file);
/* Create the tar file. */
res = tar_create_archive(cmd->tmp_pool, tar_file,
(unsigned long) st.st_blksize, path, dir, flags);
if (res < 0) {
int xerrno = errno;
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error creating tar file '%s' from directory '%s': %s",
tar_file, path, strerror(xerrno));
*ptr = '.';
return PR_DECLINED(cmd);
}
/* Stash this temporary filename. */
if (pr_table_add(cmd->notes, pstrdup(cmd->pool, "mod_tar.tar-file"),
tar_file, 0) < 0) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error stashing tar file in notes: %s", strerror(errno));
*ptr = '.';
return PR_DECLINED(cmd);
}
/* We also make a copy of the temporary filename elsewhere, in case
* the client dies unexpectedly.
*/
tar_tmp_file = pstrdup(session.pool, tar_file);
/* And stash a copy of the originally requested directory. */
if (pr_table_add(cmd->notes, pstrdup(cmd->pool, "mod_tar.orig-path"),
pstrdup(cmd->pool, path), 0) < 0) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error stashing original path in notes: %s", strerror(errno));
*ptr = '.';
return PR_DECLINED(cmd);
}
*ptr = '.';
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"replaced 'RETR %s' with 'RETR %s'", cmd->arg, tar_file);
cmd->arg = tar_file;
}
return PR_DECLINED(cmd);
}
MODRET tar_log_retr(cmd_rec *cmd) {
const char *path;
if (tar_engine == FALSE) {
return PR_DECLINED(cmd);
}
path = pr_table_get(cmd->notes, "mod_tar.tar-file", NULL);
if (path != NULL) {
if (unlink(path) < 0) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error deleting '%s': %s", path, strerror(errno));
} else {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"deleted tar file '%s'", path);
tar_tmp_file = NULL;
}
}
path = pr_table_get(cmd->notes, "mod_tar.orig-path", NULL);
if (path != NULL) {
/* Replace session.xfer.path, so that the TransferLog/ExtendedLogs
* show the originally requested path, not the temporary filename
* generated by mod_tar.
*/
session.xfer.path = path;
}
return PR_DECLINED(cmd);
}
/* Event handlers
*/
static void tar_exit_ev(const void *event_data, void *user_data) {
/* Clean up any temporary tar files which we might have left around because
* the client exited uncleanly.
*/
if (tar_tmp_file != NULL) {
if (unlink(tar_tmp_file) < 0) {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"error deleting '%s': %s", tar_tmp_file, strerror(errno));
} else {
(void) pr_log_writefile(tar_logfd, MOD_TAR_VERSION,
"deleted tar file '%s'", tar_tmp_file);
tar_tmp_file = NULL;
}
}
}
/* Initialization functions
*/
static int tar_sess_init(void) {
config_rec *c;
c = find_config(main_server->conf, CONF_PARAM, "TarLog", FALSE);
if (c &&
strncasecmp((char *) c->argv[0], "none", 5) != 0) {
int res, xerrno;
char *path;
path = c->argv[0];
PRIVS_ROOT
res = pr_log_openfile(path, &tar_logfd, 0660);
xerrno = errno;
PRIVS_RELINQUISH
switch (res) {
case 0:
break;
case -1:
pr_log_debug(DEBUG1, MOD_TAR_VERSION ": unable to open TarLog '%s': %s",
path, strerror(xerrno));
break;
case PR_LOG_SYMLINK:
pr_log_debug(DEBUG1, MOD_TAR_VERSION ": unable to open TarLog '%s': %s",
path, "is a symlink");
break;
case PR_LOG_WRITABLE_DIR:
pr_log_debug(DEBUG1, MOD_TAR_VERSION ": unable to open TarLog '%s': %s",
path, "parent directory is world-writable");
break;
}
}
return 0;
}
static int tar_init(void) {
pr_log_debug(DEBUG0, MOD_TAR_VERSION ": using %s", archive_version_string());
return 0;
}
/* Module API tables
*/
static conftable tar_conftab[] = {
{ "TarEnable", set_tarenable, NULL },
{ "TarEngine", set_tarengine, NULL },
{ "TarLog", set_tarlog, NULL },
{ "TarOptions", set_taroptions, NULL },
{ "TarTempPath", set_tartemppath, NULL },
{ NULL }
};
static cmdtable tar_cmdtab[] = {
{ POST_CMD, C_PASS, G_NONE, tar_post_pass, FALSE, FALSE },
{ PRE_CMD, C_RETR, G_NONE, tar_pre_retr, FALSE, FALSE },
{ LOG_CMD, C_RETR, G_NONE, tar_log_retr, FALSE, FALSE },
{ LOG_CMD_ERR, C_RETR, G_NONE, tar_log_retr, FALSE, FALSE },
};
module tar_module = {
NULL, NULL,
/* Module API version 2.0 */
0x20,
/* Module name */
"tar",
/* Module config handler table */
tar_conftab,
/* Module command handler table */
tar_cmdtab,
/* Module auth handler table */
NULL,
/* Module init function */
tar_init,
/* Session init function */
tar_sess_init,
/* Module version */
MOD_TAR_VERSION
};
|