blob: 04bf87229434928f8230d7f717d1c63ce6d5fdd4 [file] [log] [blame]
Denys Vlasenko2f59bf32017-04-07 20:45:08 +02001/* 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 Vlasenkob097a842018-12-28 03:20:17 +01008//config: bool "shred (4.9 kb)"
Denys Vlasenko2f59bf32017-04-07 20:45:08 +02009//config: default y
10//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020011//config: Overwrite a file to hide its contents, and optionally delete it
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020012
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
Denys Vlasenkoec3170a2021-06-20 13:08:50 +020018//usage: "[-fuz] [-n N] [-s SIZE] FILE..."
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020019//usage:#define shred_full_usage "\n\n"
20//usage: "Overwrite/delete FILEs\n"
21//usage: "\n -f Chmod to ensure writability"
Denys Vlasenkoec3170a2021-06-20 13:08:50 +020022//usage: "\n -s SIZE Size to write"
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020023//usage: "\n -n N Overwrite N times (default 3)"
24//usage: "\n -z Final overwrite with zeros"
25//usage: "\n -u Remove file"
Denys Vlasenkoec3170a2021-06-20 13:08:50 +020026//-x (exact: don't round up to 4k) and -v (verbose) are accepted but have no effect
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020027
28/* shred (GNU coreutils) 8.25:
29-f, --force change permissions to allow writing if necessary
30-u truncate and remove file after overwriting
31-z, --zero add a final overwrite with zeros to hide shredding
32-n, --iterations=N overwrite N times instead of the default (3)
33-v, --verbose show progress
34-x, --exact do not round file sizes up to the next full block; this is the default for non-regular files
35--random-source=FILE get random bytes from FILE
36-s, --size=N shred this many bytes (suffixes like K, M, G accepted)
37--remove[=HOW] like -u but give control on HOW to delete; See below
38*/
39
40#include "libbb.h"
41
42int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
43int shred_main(int argc UNUSED_PARAM, char **argv)
44{
Denys Vlasenkoec3170a2021-06-20 13:08:50 +020045 char *opt_s;
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020046 int rand_fd = rand_fd; /* for compiler */
47 int zero_fd;
48 unsigned num_iter = 3;
49 unsigned opt;
50 enum {
51 OPT_f = (1 << 0),
52 OPT_u = (1 << 1),
53 OPT_z = (1 << 2),
54 OPT_n = (1 << 3),
55 OPT_v = (1 << 4),
56 OPT_x = (1 << 5),
Denys Vlasenkoec3170a2021-06-20 13:08:50 +020057 OPT_s = (1 << 6),
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020058 };
59
Denys Vlasenkoec3170a2021-06-20 13:08:50 +020060 opt = getopt32(argv, "^" "fuzn:+vxs:" "\0" "-1"/*min 1 arg*/, &num_iter, &opt_s);
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020061 argv += optind;
62
63 zero_fd = xopen("/dev/zero", O_RDONLY);
64 if (num_iter != 0)
65 rand_fd = xopen("/dev/urandom", O_RDONLY);
66
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020067 for (;;) {
68 struct stat sb;
Denys Vlasenko0f7f1ae2017-04-07 21:10:00 +020069 const char *fname;
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020070 unsigned i;
Denys Vlasenko0f7f1ae2017-04-07 21:10:00 +020071 int fd;
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020072
Denys Vlasenko0f7f1ae2017-04-07 21:10:00 +020073 fname = *argv++;
74 if (!fname)
75 break;
76 fd = -1;
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020077 if (opt & OPT_f) {
Denys Vlasenko0f7f1ae2017-04-07 21:10:00 +020078 fd = open(fname, O_WRONLY);
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020079 if (fd < 0)
Denys Vlasenko0f7f1ae2017-04-07 21:10:00 +020080 chmod(fname, 0666);
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020081 }
82 if (fd < 0)
Denys Vlasenko0f7f1ae2017-04-07 21:10:00 +020083 fd = xopen(fname, O_WRONLY);
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020084
85 if (fstat(fd, &sb) == 0 && sb.st_size > 0) {
86 off_t size = sb.st_size;
87
Denys Vlasenkoec3170a2021-06-20 13:08:50 +020088 if (opt & OPT_s) {
89 size = BB_STRTOOFF(opt_s, NULL, 0); /* accepts oct/hex */
90 if (errno || size < 0) bb_show_usage();
91 }
92
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020093 for (i = 0; i < num_iter; i++) {
94 bb_copyfd_size(rand_fd, fd, size);
95 fdatasync(fd);
96 xlseek(fd, 0, SEEK_SET);
97 }
98 if (opt & OPT_z) {
99 bb_copyfd_size(zero_fd, fd, size);
100 fdatasync(fd);
101 }
Denys Vlasenko2f59bf32017-04-07 20:45:08 +0200102 }
Denys Vlasenkod71c7892021-06-20 13:48:21 +0200103 if (opt & OPT_u) {
104 ftruncate(fd, 0);
105 xunlink(fname);
106 }
107 xclose(fd);
Denys Vlasenko2f59bf32017-04-07 20:45:08 +0200108 }
109
110 return EXIT_SUCCESS;
111}