2017-04-08 00:15:08 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
|
|
|
*/
|
|
|
|
//config:config SHRED
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "shred (5 kb)"
|
2017-04-08 00:15:08 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Overwrite a file to hide its contents, and optionally delete it
|
2017-04-08 00:15:08 +05:30
|
|
|
|
|
|
|
//applet:IF_SHRED(APPLET(shred, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_SHRED) += shred.o
|
|
|
|
|
|
|
|
//usage:#define shred_trivial_usage
|
|
|
|
//usage: "FILE..."
|
|
|
|
//usage:#define shred_full_usage "\n\n"
|
|
|
|
//usage: "Overwrite/delete FILEs\n"
|
|
|
|
//usage: "\n -f Chmod to ensure writability"
|
|
|
|
//usage: "\n -n N Overwrite N times (default 3)"
|
|
|
|
//usage: "\n -z Final overwrite with zeros"
|
|
|
|
//usage: "\n -u Remove file"
|
|
|
|
//-x and -v are accepted but have no effect
|
|
|
|
|
|
|
|
/* shred (GNU coreutils) 8.25:
|
|
|
|
-f, --force change permissions to allow writing if necessary
|
|
|
|
-u truncate and remove file after overwriting
|
|
|
|
-z, --zero add a final overwrite with zeros to hide shredding
|
|
|
|
-n, --iterations=N overwrite N times instead of the default (3)
|
|
|
|
-v, --verbose show progress
|
|
|
|
-x, --exact do not round file sizes up to the next full block; this is the default for non-regular files
|
|
|
|
--random-source=FILE get random bytes from FILE
|
|
|
|
-s, --size=N shred this many bytes (suffixes like K, M, G accepted)
|
|
|
|
--remove[=HOW] like -u but give control on HOW to delete; See below
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
|
|
|
int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int shred_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
int rand_fd = rand_fd; /* for compiler */
|
|
|
|
int zero_fd;
|
|
|
|
unsigned num_iter = 3;
|
|
|
|
unsigned opt;
|
|
|
|
enum {
|
|
|
|
OPT_f = (1 << 0),
|
|
|
|
OPT_u = (1 << 1),
|
|
|
|
OPT_z = (1 << 2),
|
|
|
|
OPT_n = (1 << 3),
|
|
|
|
OPT_v = (1 << 4),
|
|
|
|
OPT_x = (1 << 5),
|
|
|
|
};
|
|
|
|
|
|
|
|
opt = getopt32(argv, "fuzn:+vx", &num_iter);
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
zero_fd = xopen("/dev/zero", O_RDONLY);
|
|
|
|
if (num_iter != 0)
|
|
|
|
rand_fd = xopen("/dev/urandom", O_RDONLY);
|
|
|
|
|
|
|
|
if (!*argv)
|
|
|
|
bb_show_usage();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
struct stat sb;
|
2017-04-08 00:40:00 +05:30
|
|
|
const char *fname;
|
2017-04-08 00:15:08 +05:30
|
|
|
unsigned i;
|
2017-04-08 00:40:00 +05:30
|
|
|
int fd;
|
2017-04-08 00:15:08 +05:30
|
|
|
|
2017-04-08 00:40:00 +05:30
|
|
|
fname = *argv++;
|
|
|
|
if (!fname)
|
|
|
|
break;
|
|
|
|
fd = -1;
|
2017-04-08 00:15:08 +05:30
|
|
|
if (opt & OPT_f) {
|
2017-04-08 00:40:00 +05:30
|
|
|
fd = open(fname, O_WRONLY);
|
2017-04-08 00:15:08 +05:30
|
|
|
if (fd < 0)
|
2017-04-08 00:40:00 +05:30
|
|
|
chmod(fname, 0666);
|
2017-04-08 00:15:08 +05:30
|
|
|
}
|
|
|
|
if (fd < 0)
|
2017-04-08 00:40:00 +05:30
|
|
|
fd = xopen(fname, O_WRONLY);
|
2017-04-08 00:15:08 +05:30
|
|
|
|
|
|
|
if (fstat(fd, &sb) == 0 && sb.st_size > 0) {
|
|
|
|
off_t size = sb.st_size;
|
|
|
|
|
|
|
|
for (i = 0; i < num_iter; i++) {
|
|
|
|
bb_copyfd_size(rand_fd, fd, size);
|
|
|
|
fdatasync(fd);
|
|
|
|
xlseek(fd, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
if (opt & OPT_z) {
|
|
|
|
bb_copyfd_size(zero_fd, fd, size);
|
|
|
|
fdatasync(fd);
|
|
|
|
}
|
|
|
|
if (opt & OPT_u) {
|
|
|
|
ftruncate(fd, 0);
|
2017-04-08 00:40:00 +05:30
|
|
|
xunlink(fname);
|
2017-04-08 00:15:08 +05:30
|
|
|
}
|
|
|
|
xclose(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|