Denys Vlasenko | 2f59bf3 | 2017-04-07 20:45:08 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com> |
| 4 | * |
| 5 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 6 | */ |
| 7 | //config:config SHRED |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 8 | //config: bool "shred (5 kb)" |
Denys Vlasenko | 2f59bf3 | 2017-04-07 20:45:08 +0200 | [diff] [blame] | 9 | //config: default y |
| 10 | //config: help |
| 11 | //config: Overwrite a file to hide its contents, and optionally delete it |
| 12 | |
| 13 | //applet:IF_SHRED(APPLET(shred, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 14 | |
| 15 | //kbuild:lib-$(CONFIG_SHRED) += shred.o |
| 16 | |
| 17 | //usage:#define shred_trivial_usage |
| 18 | //usage: "FILE..." |
| 19 | //usage:#define shred_full_usage "\n\n" |
| 20 | //usage: "Overwrite/delete FILEs\n" |
| 21 | //usage: "\n -f Chmod to ensure writability" |
| 22 | //usage: "\n -n N Overwrite N times (default 3)" |
| 23 | //usage: "\n -z Final overwrite with zeros" |
| 24 | //usage: "\n -u Remove file" |
| 25 | //-x and -v are accepted but have no effect |
| 26 | |
| 27 | /* shred (GNU coreutils) 8.25: |
| 28 | -f, --force change permissions to allow writing if necessary |
| 29 | -u truncate and remove file after overwriting |
| 30 | -z, --zero add a final overwrite with zeros to hide shredding |
| 31 | -n, --iterations=N overwrite N times instead of the default (3) |
| 32 | -v, --verbose show progress |
| 33 | -x, --exact do not round file sizes up to the next full block; this is the default for non-regular files |
| 34 | --random-source=FILE get random bytes from FILE |
| 35 | -s, --size=N shred this many bytes (suffixes like K, M, G accepted) |
| 36 | --remove[=HOW] like -u but give control on HOW to delete; See below |
| 37 | */ |
| 38 | |
| 39 | #include "libbb.h" |
| 40 | |
| 41 | int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 42 | int shred_main(int argc UNUSED_PARAM, char **argv) |
| 43 | { |
| 44 | int rand_fd = rand_fd; /* for compiler */ |
| 45 | int zero_fd; |
| 46 | unsigned num_iter = 3; |
| 47 | unsigned opt; |
| 48 | enum { |
| 49 | OPT_f = (1 << 0), |
| 50 | OPT_u = (1 << 1), |
| 51 | OPT_z = (1 << 2), |
| 52 | OPT_n = (1 << 3), |
| 53 | OPT_v = (1 << 4), |
| 54 | OPT_x = (1 << 5), |
| 55 | }; |
| 56 | |
| 57 | opt = getopt32(argv, "fuzn:+vx", &num_iter); |
| 58 | argv += optind; |
| 59 | |
| 60 | zero_fd = xopen("/dev/zero", O_RDONLY); |
| 61 | if (num_iter != 0) |
| 62 | rand_fd = xopen("/dev/urandom", O_RDONLY); |
| 63 | |
| 64 | if (!*argv) |
| 65 | bb_show_usage(); |
| 66 | |
| 67 | for (;;) { |
| 68 | struct stat sb; |
Denys Vlasenko | 0f7f1ae | 2017-04-07 21:10:00 +0200 | [diff] [blame] | 69 | const char *fname; |
Denys Vlasenko | 2f59bf3 | 2017-04-07 20:45:08 +0200 | [diff] [blame] | 70 | unsigned i; |
Denys Vlasenko | 0f7f1ae | 2017-04-07 21:10:00 +0200 | [diff] [blame] | 71 | int fd; |
Denys Vlasenko | 2f59bf3 | 2017-04-07 20:45:08 +0200 | [diff] [blame] | 72 | |
Denys Vlasenko | 0f7f1ae | 2017-04-07 21:10:00 +0200 | [diff] [blame] | 73 | fname = *argv++; |
| 74 | if (!fname) |
| 75 | break; |
| 76 | fd = -1; |
Denys Vlasenko | 2f59bf3 | 2017-04-07 20:45:08 +0200 | [diff] [blame] | 77 | if (opt & OPT_f) { |
Denys Vlasenko | 0f7f1ae | 2017-04-07 21:10:00 +0200 | [diff] [blame] | 78 | fd = open(fname, O_WRONLY); |
Denys Vlasenko | 2f59bf3 | 2017-04-07 20:45:08 +0200 | [diff] [blame] | 79 | if (fd < 0) |
Denys Vlasenko | 0f7f1ae | 2017-04-07 21:10:00 +0200 | [diff] [blame] | 80 | chmod(fname, 0666); |
Denys Vlasenko | 2f59bf3 | 2017-04-07 20:45:08 +0200 | [diff] [blame] | 81 | } |
| 82 | if (fd < 0) |
Denys Vlasenko | 0f7f1ae | 2017-04-07 21:10:00 +0200 | [diff] [blame] | 83 | fd = xopen(fname, O_WRONLY); |
Denys Vlasenko | 2f59bf3 | 2017-04-07 20:45:08 +0200 | [diff] [blame] | 84 | |
| 85 | if (fstat(fd, &sb) == 0 && sb.st_size > 0) { |
| 86 | off_t size = sb.st_size; |
| 87 | |
| 88 | for (i = 0; i < num_iter; i++) { |
| 89 | bb_copyfd_size(rand_fd, fd, size); |
| 90 | fdatasync(fd); |
| 91 | xlseek(fd, 0, SEEK_SET); |
| 92 | } |
| 93 | if (opt & OPT_z) { |
| 94 | bb_copyfd_size(zero_fd, fd, size); |
| 95 | fdatasync(fd); |
| 96 | } |
| 97 | if (opt & OPT_u) { |
| 98 | ftruncate(fd, 0); |
Denys Vlasenko | 0f7f1ae | 2017-04-07 21:10:00 +0200 | [diff] [blame] | 99 | xunlink(fname); |
Denys Vlasenko | 2f59bf3 | 2017-04-07 20:45:08 +0200 | [diff] [blame] | 100 | } |
| 101 | xclose(fd); |
| 102 | } |
Denys Vlasenko | 2f59bf3 | 2017-04-07 20:45:08 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return EXIT_SUCCESS; |
| 106 | } |