1*4cb87529Srillig /* $NetBSD: rm.c,v 1.54 2021/09/10 22:11:03 rillig Exp $ */
249f0ad86Scgd
361f28255Scgd /*-
4c271c159Sjrf * Copyright (c) 1990, 1993, 1994, 2003
59baa91f3Smycroft * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15b5b29542Sagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
3225b23032Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
342fe2731dSlukem __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\
352fe2731dSlukem The Regents of the University of California. All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd
3861f28255Scgd #ifndef lint
3949f0ad86Scgd #if 0
40d943cdadSjtc static char sccsid[] = "@(#)rm.c 8.8 (Berkeley) 4/27/95";
4149f0ad86Scgd #else
42*4cb87529Srillig __RCSID("$NetBSD: rm.c,v 1.54 2021/09/10 22:11:03 rillig Exp $");
4349f0ad86Scgd #endif
4461f28255Scgd #endif /* not lint */
4561f28255Scgd
46458ed234Sjschauma #include <sys/param.h>
473f0d8565Sjtc #include <sys/stat.h>
48458ed234Sjschauma #include <sys/types.h>
499baa91f3Smycroft
509baa91f3Smycroft #include <err.h>
519baa91f3Smycroft #include <errno.h>
529baa91f3Smycroft #include <fcntl.h>
533f0d8565Sjtc #include <fts.h>
540b11ad45Swiz #include <grp.h>
55458ed234Sjschauma #include <locale.h>
560b11ad45Swiz #include <pwd.h>
57df65f3e2Schristos #include <signal.h>
589baa91f3Smycroft #include <stdio.h>
599baa91f3Smycroft #include <stdlib.h>
609baa91f3Smycroft #include <string.h>
619baa91f3Smycroft #include <unistd.h>
629baa91f3Smycroft
63074c0c6eSjoerg static int dflag, eval, fflag, iflag, Pflag, stdin_ok, vflag, Wflag;
64aab67d52Schristos static int xflag;
65df65f3e2Schristos static sig_atomic_t pinfo;
663f0d8565Sjtc
67074c0c6eSjoerg static int check(char *, char *, struct stat *);
68074c0c6eSjoerg static void checkdot(char **);
69df65f3e2Schristos static void progress(int);
70074c0c6eSjoerg static void rm_file(char **);
71074c0c6eSjoerg static int rm_overwrite(char *, struct stat *);
72074c0c6eSjoerg static void rm_tree(char **);
73074c0c6eSjoerg __dead static void usage(void);
7461f28255Scgd
7561f28255Scgd /*
7688c27a6cSkleink * For the sake of the `-f' flag, check whether an error number indicates the
7788c27a6cSkleink * failure of an operation due to an non-existent file, either per se (ENOENT)
7888c27a6cSkleink * or because its filename argument was illegal (ENAMETOOLONG, ENOTDIR).
7988c27a6cSkleink */
8088c27a6cSkleink #define NONEXISTENT(x) \
8188c27a6cSkleink ((x) == ENOENT || (x) == ENAMETOOLONG || (x) == ENOTDIR)
8288c27a6cSkleink
8388c27a6cSkleink /*
8461f28255Scgd * rm --
8561f28255Scgd * This rm is different from historic rm's, but is expected to match
8661f28255Scgd * POSIX 1003.2 behavior. The most visible difference is that -f
8761f28255Scgd * has two specific effects now, ignore non-existent files and force
8861f28255Scgd * file removal.
8961f28255Scgd */
903f0d8565Sjtc int
main(int argc,char * argv[])910b11ad45Swiz main(int argc, char *argv[])
9261f28255Scgd {
9361f28255Scgd int ch, rflag;
9461f28255Scgd
950b11ad45Swiz setprogname(argv[0]);
9629bf463dSmycroft (void)setlocale(LC_ALL, "");
973f0d8565Sjtc
98aab67d52Schristos Pflag = rflag = xflag = 0;
99aab67d52Schristos while ((ch = getopt(argc, argv, "dfiPRrvWx")) != -1)
10061f28255Scgd switch (ch) {
10161f28255Scgd case 'd':
10261f28255Scgd dflag = 1;
10361f28255Scgd break;
10461f28255Scgd case 'f':
10561f28255Scgd fflag = 1;
10661f28255Scgd iflag = 0;
10761f28255Scgd break;
10861f28255Scgd case 'i':
10961f28255Scgd fflag = 0;
11061f28255Scgd iflag = 1;
11161f28255Scgd break;
1129baa91f3Smycroft case 'P':
1139baa91f3Smycroft Pflag = 1;
1149baa91f3Smycroft break;
11561f28255Scgd case 'R':
1169baa91f3Smycroft case 'r': /* Compatibility. */
11761f28255Scgd rflag = 1;
11861f28255Scgd break;
119c271c159Sjrf case 'v':
120c271c159Sjrf vflag = 1;
121c271c159Sjrf break;
122aab67d52Schristos case 'x':
123aab67d52Schristos xflag = 1;
124aab67d52Schristos break;
1250155aa3bSmycroft case 'W':
1260155aa3bSmycroft Wflag = 1;
1270155aa3bSmycroft break;
12861f28255Scgd case '?':
12961f28255Scgd default:
13061f28255Scgd usage();
13161f28255Scgd }
13261f28255Scgd argc -= optind;
13361f28255Scgd argv += optind;
13461f28255Scgd
13525ff9ac9Schristos if (argc < 1) {
13625ff9ac9Schristos if (fflag)
13725ff9ac9Schristos return 0;
13861f28255Scgd usage();
13925ff9ac9Schristos }
14061f28255Scgd
141df65f3e2Schristos (void)signal(SIGINFO, progress);
142df65f3e2Schristos
14361f28255Scgd checkdot(argv);
14461f28255Scgd
1455f8699a3Sjtc if (*argv) {
14661f28255Scgd stdin_ok = isatty(STDIN_FILENO);
14761f28255Scgd
14861f28255Scgd if (rflag)
1499baa91f3Smycroft rm_tree(argv);
15061f28255Scgd else
1519baa91f3Smycroft rm_file(argv);
1525f8699a3Sjtc }
15370947c85Sjtc
1549baa91f3Smycroft exit(eval);
155ee9e50eaSmycroft /* NOTREACHED */
15661f28255Scgd }
15761f28255Scgd
158074c0c6eSjoerg static void
rm_tree(char ** argv)1590b11ad45Swiz rm_tree(char **argv)
16061f28255Scgd {
1619baa91f3Smycroft FTS *fts;
1629baa91f3Smycroft FTSENT *p;
163c271c159Sjrf int flags, needstat, rval;
16461f28255Scgd
16561f28255Scgd /*
16661f28255Scgd * Remove a file hierarchy. If forcing removal (-f), or interactive
16761f28255Scgd * (-i) or can't ask anyway (stdin_ok), don't stat the file.
16861f28255Scgd */
16961f28255Scgd needstat = !fflag && !iflag && stdin_ok;
17061f28255Scgd
17161f28255Scgd /*
17261f28255Scgd * If the -i option is specified, the user can skip on the pre-order
17361f28255Scgd * visit. The fts_number field flags skipped directories.
17461f28255Scgd */
17561f28255Scgd #define SKIPPED 1
17661f28255Scgd
1770155aa3bSmycroft flags = FTS_PHYSICAL;
1780155aa3bSmycroft if (!needstat)
1790155aa3bSmycroft flags |= FTS_NOSTAT;
1800155aa3bSmycroft if (Wflag)
1810155aa3bSmycroft flags |= FTS_WHITEOUT;
182aab67d52Schristos if (xflag)
183aab67d52Schristos flags |= FTS_XDEV;
18425ff9ac9Schristos if ((fts = fts_open(argv, flags, NULL)) == NULL)
18525ff9ac9Schristos err(1, "fts_open failed");
1863f0d8565Sjtc while ((p = fts_read(fts)) != NULL) {
187458ed234Sjschauma
18861f28255Scgd switch (p->fts_info) {
18961f28255Scgd case FTS_DNR:
1909baa91f3Smycroft if (!fflag || p->fts_errno != ENOENT) {
1916a75fbb6Sjschauma warnx("%s: %s", p->fts_path,
1926a75fbb6Sjschauma strerror(p->fts_errno));
1939baa91f3Smycroft eval = 1;
1949baa91f3Smycroft }
1955f8699a3Sjtc continue;
1969baa91f3Smycroft case FTS_ERR:
1976a75fbb6Sjschauma errx(EXIT_FAILURE, "%s: %s", p->fts_path,
198458ed234Sjschauma strerror(p->fts_errno));
199ee9e50eaSmycroft /* NOTREACHED */
20061f28255Scgd case FTS_NS:
2019baa91f3Smycroft /*
2029baa91f3Smycroft * FTS_NS: assume that if can't stat the file, it
2039baa91f3Smycroft * can't be unlinked.
2049baa91f3Smycroft */
205440e119bSjmc if (fflag && NONEXISTENT(p->fts_errno))
206440e119bSjmc continue;
207440e119bSjmc if (needstat) {
2086a75fbb6Sjschauma warnx("%s: %s", p->fts_path,
2096a75fbb6Sjschauma strerror(p->fts_errno));
2109baa91f3Smycroft eval = 1;
21161f28255Scgd continue;
212440e119bSjmc }
213440e119bSjmc break;
21461f28255Scgd case FTS_D:
2159baa91f3Smycroft /* Pre-order: give user chance to skip. */
2168e3ded2bSjtc if (!fflag && !check(p->fts_path, p->fts_accpath,
217ec2040d7Sderaadt p->fts_statp)) {
21861f28255Scgd (void)fts_set(fts, p, FTS_SKIP);
21961f28255Scgd p->fts_number = SKIPPED;
22061f28255Scgd }
22161f28255Scgd continue;
22261f28255Scgd case FTS_DP:
2239baa91f3Smycroft /* Post-order: see if user skipped. */
22461f28255Scgd if (p->fts_number == SKIPPED)
22561f28255Scgd continue;
22661f28255Scgd break;
22770947c85Sjtc default:
2289baa91f3Smycroft if (!fflag &&
2299baa91f3Smycroft !check(p->fts_path, p->fts_accpath, p->fts_statp))
23061f28255Scgd continue;
23170947c85Sjtc }
23261f28255Scgd
233c271c159Sjrf rval = 0;
23461f28255Scgd /*
23561f28255Scgd * If we can't read or search the directory, may still be
23661f28255Scgd * able to remove it. Don't print out the un{read,search}able
23761f28255Scgd * message unless the remove fails.
23861f28255Scgd */
2390155aa3bSmycroft switch (p->fts_info) {
2400155aa3bSmycroft case FTS_DP:
2410155aa3bSmycroft case FTS_DNR:
242c271c159Sjrf rval = rmdir(p->fts_accpath);
243c271c159Sjrf if (rval != 0 && fflag && errno == ENOENT)
24461f28255Scgd continue;
2450155aa3bSmycroft break;
2460155aa3bSmycroft
2470155aa3bSmycroft case FTS_W:
248c271c159Sjrf rval = undelete(p->fts_accpath);
249c271c159Sjrf if (rval != 0 && fflag && errno == ENOENT)
2500155aa3bSmycroft continue;
2510155aa3bSmycroft break;
2520155aa3bSmycroft
2530155aa3bSmycroft default:
254c9432db8Sliamjfoy if (Pflag) {
255c9432db8Sliamjfoy if (rm_overwrite(p->fts_accpath, NULL))
256c9432db8Sliamjfoy continue;
257c9432db8Sliamjfoy }
258c271c159Sjrf rval = unlink(p->fts_accpath);
259c271c159Sjrf if (rval != 0 && fflag && NONEXISTENT(errno))
26061f28255Scgd continue;
261c271c159Sjrf break;
262a1899e4cSjtc }
263c271c159Sjrf if (rval != 0) {
2646a75fbb6Sjschauma warn("%s", p->fts_path);
2659baa91f3Smycroft eval = 1;
266df65f3e2Schristos } else if (vflag || pinfo) {
267df65f3e2Schristos pinfo = 0;
2686a75fbb6Sjschauma (void)printf("%s\n", p->fts_path);
26961f28255Scgd }
270df65f3e2Schristos }
2719baa91f3Smycroft if (errno)
2729baa91f3Smycroft err(1, "fts_read");
27396b1a913Speter fts_close(fts);
27461f28255Scgd }
27561f28255Scgd
276074c0c6eSjoerg static void
rm_file(char ** argv)2770b11ad45Swiz rm_file(char **argv)
27861f28255Scgd {
27961f28255Scgd struct stat sb;
2809baa91f3Smycroft int rval;
2816a75fbb6Sjschauma char *f;
28261f28255Scgd
28361f28255Scgd /*
28461f28255Scgd * Remove a file. POSIX 1003.2 states that, by default, attempting
28561f28255Scgd * to remove a directory is an error, so must always stat the file.
28661f28255Scgd */
2873f0d8565Sjtc while ((f = *argv++) != NULL) {
2889baa91f3Smycroft /* Assume if can't stat the file, can't unlink it. */
28961f28255Scgd if (lstat(f, &sb)) {
2900155aa3bSmycroft if (Wflag) {
2910155aa3bSmycroft sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
2920155aa3bSmycroft } else {
29388c27a6cSkleink if (!fflag || !NONEXISTENT(errno)) {
2946a75fbb6Sjschauma warn("%s", f);
2959baa91f3Smycroft eval = 1;
29670947c85Sjtc }
29761f28255Scgd continue;
29861f28255Scgd }
2990155aa3bSmycroft } else if (Wflag) {
3006a75fbb6Sjschauma warnx("%s: %s", f, strerror(EEXIST));
3010155aa3bSmycroft eval = 1;
3020155aa3bSmycroft continue;
3030155aa3bSmycroft }
3040155aa3bSmycroft
3053f0d8565Sjtc if (S_ISDIR(sb.st_mode) && !dflag) {
3066a75fbb6Sjschauma warnx("%s: is a directory", f);
3079baa91f3Smycroft eval = 1;
30861f28255Scgd continue;
30961f28255Scgd }
3100155aa3bSmycroft if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
31161f28255Scgd continue;
3120155aa3bSmycroft if (S_ISWHT(sb.st_mode))
3130155aa3bSmycroft rval = undelete(f);
3140155aa3bSmycroft else if (S_ISDIR(sb.st_mode))
3159baa91f3Smycroft rval = rmdir(f);
3169baa91f3Smycroft else {
317c9432db8Sliamjfoy if (Pflag) {
318c9432db8Sliamjfoy if (rm_overwrite(f, &sb))
319c9432db8Sliamjfoy continue;
320c9432db8Sliamjfoy }
3219baa91f3Smycroft rval = unlink(f);
3229baa91f3Smycroft }
32388c27a6cSkleink if (rval && (!fflag || !NONEXISTENT(errno))) {
3246a75fbb6Sjschauma warn("%s", f);
3259baa91f3Smycroft eval = 1;
3269baa91f3Smycroft }
3276a75fbb6Sjschauma if (vflag && rval == 0)
3286a75fbb6Sjschauma (void)printf("%s\n", f);
3299baa91f3Smycroft }
3303f0d8565Sjtc }
3313f0d8565Sjtc
3323f0d8565Sjtc /*
3339baa91f3Smycroft * rm_overwrite --
3349baa91f3Smycroft * Overwrite the file 3 times with varying bit patterns.
3359baa91f3Smycroft *
336f7166635Stls * This is an expensive way to keep people from recovering files from your
337f7166635Stls * non-snapshotted FFS filesystems using fsdb(8). Really. No more. Only
338f7166635Stls * regular files are deleted, directories (and therefore names) will remain.
3399baa91f3Smycroft * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
3409baa91f3Smycroft * System V file system). In a logging file system, you'll have to have
3419baa91f3Smycroft * kernel support.
342e9e0ca41Stls *
343e9e0ca41Stls * A note on standards: U.S. DoD 5220.22-M "National Industrial Security
344e9e0ca41Stls * Program Operating Manual" ("NISPOM") is often cited as a reference
345e9e0ca41Stls * for clearing and sanitizing magnetic media. In fact, a matrix of
346e9e0ca41Stls * "clearing" and "sanitization" methods for various media was given in
347e9e0ca41Stls * Chapter 8 of the original 1995 version of NISPOM. However, that
348e9e0ca41Stls * matrix was *removed from the document* when Chapter 8 was rewritten
349e9e0ca41Stls * in Change 2 to the document in 2001. Recently, the Defense Security
350e9e0ca41Stls * Service has made a revised clearing and sanitization matrix available
351e9e0ca41Stls * in Microsoft Word format on the DSS web site. The standardization
352e9e0ca41Stls * status of this matrix is unclear. Furthermore, one must be very
353e9e0ca41Stls * careful when referring to this matrix: it is intended for the "clearing"
354e9e0ca41Stls * prior to reuse or "sanitization" prior to disposal of *entire media*,
355e9e0ca41Stls * not individual files and the only non-physically-destructive method of
356e9e0ca41Stls * "sanitization" that is permitted for magnetic disks of any kind is
357e9e0ca41Stls * specifically noted to be prohibited for media that have contained
358e9e0ca41Stls * Top Secret data.
359e9e0ca41Stls *
360e9e0ca41Stls * It is impossible to actually conform to the exact procedure given in
361e9e0ca41Stls * the matrix if one is overwriting a file, not an entire disk, because
362e9e0ca41Stls * the procedure requires examination and comparison of the disk's defect
363e9e0ca41Stls * lists. Any program that claims to securely erase *files* while
3641775a629Stls * conforming to the standard, then, is not correct. We do as much of
3651775a629Stls * what the standard requires as can actually be done when erasing a
3661775a629Stls * file, rather than an entire disk; but that does not make us conformant.
367e9e0ca41Stls *
368e9e0ca41Stls * Furthermore, the presence of track caches, disk and controller write
369e9e0ca41Stls * caches, and so forth make it extremely difficult to ensure that data
3708d21cba1Swiz * have actually been written to the disk, particularly when one tries
371e9e0ca41Stls * to repeatedly overwrite the same sectors in quick succession. We call
372e9e0ca41Stls * fsync(), but controllers with nonvolatile cache, as well as IDE disks
373e9e0ca41Stls * that just plain lie about the stable storage of data, will defeat this.
374e9e0ca41Stls *
375e9e0ca41Stls * Finally, widely respected research suggests that the given procedure
376e9e0ca41Stls * is nowhere near sufficient to prevent the recovery of data using special
377e9e0ca41Stls * forensic equipment and techniques that are well-known. This is
378e9e0ca41Stls * presumably one reason that the matrix requires physical media destruction,
379e9e0ca41Stls * rather than any technique of the sort attempted here, for secret data.
380e9e0ca41Stls *
381e9e0ca41Stls * Caveat Emptor.
382c9432db8Sliamjfoy *
383c9432db8Sliamjfoy * rm_overwrite will return 0 on success.
3843f0d8565Sjtc */
385e9e0ca41Stls
386074c0c6eSjoerg static int
rm_overwrite(char * file,struct stat * sbp)3870b11ad45Swiz rm_overwrite(char *file, struct stat *sbp)
3889baa91f3Smycroft {
38961f93db2Sdholland struct stat sb, sb2;
390e9e0ca41Stls int fd, randint;
391e9e0ca41Stls char randchar;
3929baa91f3Smycroft
3939baa91f3Smycroft fd = -1;
3949baa91f3Smycroft if (sbp == NULL) {
3959baa91f3Smycroft if (lstat(file, &sb))
3969baa91f3Smycroft goto err;
3979baa91f3Smycroft sbp = &sb;
39861f28255Scgd }
3999baa91f3Smycroft if (!S_ISREG(sbp->st_mode))
400c9432db8Sliamjfoy return 0;
401e9e0ca41Stls
402e9e0ca41Stls /* flags to try to defeat hidden caching by forcing seeks */
40361f93db2Sdholland if ((fd = open(file, O_RDWR|O_SYNC|O_RSYNC|O_NOFOLLOW, 0)) == -1)
4049baa91f3Smycroft goto err;
4059baa91f3Smycroft
40661f93db2Sdholland if (fstat(fd, &sb2)) {
40761f93db2Sdholland goto err;
40861f93db2Sdholland }
40961f93db2Sdholland
41061f93db2Sdholland if (sb2.st_dev != sbp->st_dev || sb2.st_ino != sbp->st_ino ||
41161f93db2Sdholland !S_ISREG(sb2.st_mode)) {
41261f93db2Sdholland errno = EPERM;
41361f93db2Sdholland goto err;
41461f93db2Sdholland }
41561f93db2Sdholland
416e9e0ca41Stls #define RAND_BYTES 1
417e9e0ca41Stls #define THIS_BYTE 0
418e9e0ca41Stls
419e9e0ca41Stls #define WRITE_PASS(mode, byte) do { \
420e9e0ca41Stls off_t len; \
421990d25a9Slukem size_t wlen, i; \
422e9e0ca41Stls char buf[8 * 1024]; \
423e9e0ca41Stls \
424e9e0ca41Stls if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) \
425e9e0ca41Stls goto err; \
426e9e0ca41Stls \
427e9e0ca41Stls if (mode == THIS_BYTE) \
4289baa91f3Smycroft memset(buf, byte, sizeof(buf)); \
4299baa91f3Smycroft for (len = sbp->st_size; len > 0; len -= wlen) { \
430e9e0ca41Stls if (mode == RAND_BYTES) { \
431e9e0ca41Stls for (i = 0; i < sizeof(buf); \
432e9e0ca41Stls i+= sizeof(u_int32_t)) \
433e9e0ca41Stls *(int *)(buf + i) = arc4random(); \
434e9e0ca41Stls } \
435990d25a9Slukem wlen = len < (off_t)sizeof(buf) ? (size_t)len : sizeof(buf); \
436990d25a9Slukem if ((size_t)write(fd, buf, wlen) != wlen) \
4379baa91f3Smycroft goto err; \
4389baa91f3Smycroft } \
439e9e0ca41Stls sync(); /* another poke at hidden caches */ \
440*4cb87529Srillig } while (0)
441e9e0ca41Stls
442e9e0ca41Stls #define READ_PASS(byte) do { \
443e9e0ca41Stls off_t len; \
444990d25a9Slukem size_t rlen; \
445e9e0ca41Stls char pattern[8 * 1024]; \
446e9e0ca41Stls char buf[8 * 1024]; \
447e9e0ca41Stls \
448e9e0ca41Stls if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) \
449e9e0ca41Stls goto err; \
450e9e0ca41Stls \
451e9e0ca41Stls memset(pattern, byte, sizeof(pattern)); \
452e9e0ca41Stls for(len = sbp->st_size; len > 0; len -= rlen) { \
453990d25a9Slukem rlen = len < (off_t)sizeof(buf) ? (size_t)len : sizeof(buf); \
454990d25a9Slukem if((size_t)read(fd, buf, rlen) != rlen) \
455e9e0ca41Stls goto err; \
456e9e0ca41Stls if(memcmp(buf, pattern, rlen)) \
457e9e0ca41Stls goto err; \
458e9e0ca41Stls } \
459e9e0ca41Stls sync(); /* another poke at hidden caches */ \
460*4cb87529Srillig } while (0)
461e9e0ca41Stls
462e9e0ca41Stls /*
463e9e0ca41Stls * DSS sanitization matrix "clear" for magnetic disks:
464e9e0ca41Stls * option 'c' "Overwrite all addressable locations with a single
465e9e0ca41Stls * character."
466e9e0ca41Stls */
467e9e0ca41Stls randint = arc4random();
468e9e0ca41Stls randchar = *(char *)&randint;
469e9e0ca41Stls WRITE_PASS(THIS_BYTE, randchar);
470e9e0ca41Stls
471e9e0ca41Stls /*
472e9e0ca41Stls * DSS sanitization matrix "sanitize" for magnetic disks:
473e9e0ca41Stls * option 'd', sub 2 "Overwrite all addressable locations with a
474e9e0ca41Stls * character, then its complement. Verify "complement" character
475e9e0ca41Stls * was written successfully to all addressable locations, then
476e9e0ca41Stls * overwrite all addressable locations with random characters; or
477e9e0ca41Stls * verify third overwrite of random characters." The rest of the
478e9e0ca41Stls * text in d-sub-2 specifies requirements for overwriting spared
479e9e0ca41Stls * sectors; we cannot conform to it when erasing only a file, thus
480e9e0ca41Stls * we do not conform to the standard.
481e9e0ca41Stls */
482e9e0ca41Stls
483e9e0ca41Stls /* 1. "a character" */
484e9e0ca41Stls WRITE_PASS(THIS_BYTE, 0xff);
485e9e0ca41Stls
486e9e0ca41Stls /* 2. "its complement" */
487e9e0ca41Stls WRITE_PASS(THIS_BYTE, 0x00);
488e9e0ca41Stls
489e9e0ca41Stls /* 3. "Verify 'complement' character" */
490e9e0ca41Stls READ_PASS(0x00);
491e9e0ca41Stls
492e9e0ca41Stls /* 4. "overwrite all addressable locations with random characters" */
493e9e0ca41Stls
494e9e0ca41Stls WRITE_PASS(RAND_BYTES, 0x00);
495e9e0ca41Stls
496e9e0ca41Stls /*
497e9e0ca41Stls * As the file might be huge, and we note that this revision of
498e9e0ca41Stls * the matrix says "random characters", not "a random character"
499e9e0ca41Stls * as the original did, we do not verify the random-character
500e9e0ca41Stls * write; the "or" in the standard allows this.
501e9e0ca41Stls */
502e9e0ca41Stls
503c9432db8Sliamjfoy if (close(fd) == -1) {
504c9432db8Sliamjfoy fd = -1;
505c9432db8Sliamjfoy goto err;
506c9432db8Sliamjfoy }
507c9432db8Sliamjfoy
508c9432db8Sliamjfoy return 0;
5099baa91f3Smycroft
5109baa91f3Smycroft err: eval = 1;
5116a75fbb6Sjschauma warn("%s", file);
512c9432db8Sliamjfoy if (fd != -1)
513c9432db8Sliamjfoy close(fd);
514c9432db8Sliamjfoy return 1;
5153f0d8565Sjtc }
51661f28255Scgd
517074c0c6eSjoerg static int
check(char * path,char * name,struct stat * sp)5180b11ad45Swiz check(char *path, char *name, struct stat *sp)
51961f28255Scgd {
5209baa91f3Smycroft int ch, first;
5219baa91f3Smycroft char modep[15];
52261f28255Scgd
52361f28255Scgd /* Check -i first. */
5246a75fbb6Sjschauma if (iflag)
5256a75fbb6Sjschauma (void)fprintf(stderr, "remove '%s'? ", path);
5266a75fbb6Sjschauma else {
52761f28255Scgd /*
52861f28255Scgd * If it's not a symbolic link and it's unwritable and we're
52961f28255Scgd * talking to a terminal, ask. Symbolic links are excluded
5309baa91f3Smycroft * because their permissions are meaningless. Check stdin_ok
5319baa91f3Smycroft * first because we may not have stat'ed the file.
53261f28255Scgd */
533b4d9cefaSis if (!stdin_ok || S_ISLNK(sp->st_mode) ||
534b4d9cefaSis !(access(name, W_OK) && (errno != ETXTBSY)))
53561f28255Scgd return (1);
53661f28255Scgd strmode(sp->st_mode, modep);
537c9432db8Sliamjfoy if (Pflag) {
538c9432db8Sliamjfoy warnx(
539c9432db8Sliamjfoy "%s: -P was specified but file could not"
540c9432db8Sliamjfoy " be overwritten", path);
541c9432db8Sliamjfoy return 0;
542c9432db8Sliamjfoy }
543c1686632Schristos (void)fprintf(stderr, "override %s%s%s:%s for '%s'? ",
54461f28255Scgd modep + 1, modep[9] == ' ' ? "" : " ",
54561f28255Scgd user_from_uid(sp->st_uid, 0),
5466a75fbb6Sjschauma group_from_gid(sp->st_gid, 0), path);
54761f28255Scgd }
54861f28255Scgd (void)fflush(stderr);
54961f28255Scgd
55061f28255Scgd first = ch = getchar();
55161f28255Scgd while (ch != '\n' && ch != EOF)
55261f28255Scgd ch = getchar();
553c297d9d0Sjtc return (first == 'y' || first == 'Y');
55461f28255Scgd }
55561f28255Scgd
5561a6ccc3eSjtc /*
5571a6ccc3eSjtc * POSIX.2 requires that if "." or ".." are specified as the basename
5581a6ccc3eSjtc * portion of an operand, a diagnostic message be written to standard
5591a6ccc3eSjtc * error and nothing more be done with such operands.
5601a6ccc3eSjtc *
5611a6ccc3eSjtc * Since POSIX.2 defines basename as the final portion of a path after
5621a6ccc3eSjtc * trailing slashes have been removed, we'll remove them here.
5631a6ccc3eSjtc */
56425b23032Schristos #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
565074c0c6eSjoerg static void
checkdot(char ** argv)5660b11ad45Swiz checkdot(char **argv)
56761f28255Scgd {
5689baa91f3Smycroft char *p, **save, **t;
56961f28255Scgd int complained;
57061f28255Scgd
57161f28255Scgd complained = 0;
57261f28255Scgd for (t = argv; *t;) {
5731a6ccc3eSjtc /* strip trailing slashes */
5741a6ccc3eSjtc p = strrchr(*t, '\0');
5751a6ccc3eSjtc while (--p > *t && *p == '/')
5761a6ccc3eSjtc *p = '\0';
5771a6ccc3eSjtc
5781a6ccc3eSjtc /* extract basename */
5799baa91f3Smycroft if ((p = strrchr(*t, '/')) != NULL)
58061f28255Scgd ++p;
58161f28255Scgd else
58261f28255Scgd p = *t;
5831a6ccc3eSjtc
58461f28255Scgd if (ISDOT(p)) {
5858e3ded2bSjtc if (!complained++)
5869baa91f3Smycroft warnx("\".\" and \"..\" may not be removed");
5879baa91f3Smycroft eval = 1;
5889baa91f3Smycroft for (save = t; (t[0] = t[1]) != NULL; ++t)
589bb2df5e3Sjtc continue;
59061f28255Scgd t = save;
59161f28255Scgd } else
59261f28255Scgd ++t;
59361f28255Scgd }
59461f28255Scgd }
59561f28255Scgd
596074c0c6eSjoerg static void
usage(void)5970b11ad45Swiz usage(void)
59861f28255Scgd {
599b0d6c27eSenami
600aab67d52Schristos (void)fprintf(stderr, "usage: %s [-f|-i] [-dPRrvWx] file ...\n",
601dc53bf3cSsoren getprogname());
60261f28255Scgd exit(1);
6039dc385beSmycroft /* NOTREACHED */
60461f28255Scgd }
605df65f3e2Schristos
606df65f3e2Schristos static void
progress(int sig __unused)607df65f3e2Schristos progress(int sig __unused)
608df65f3e2Schristos {
609df65f3e2Schristos
610df65f3e2Schristos pinfo++;
611df65f3e2Schristos }
612