Home | History | Annotate | Line # | Download | only in rm
rm.c revision 1.48.4.1
      1  1.48.4.1  sborrill /* $NetBSD: rm.c,v 1.48.4.1 2012/06/15 09:08:03 sborrill Exp $ */
      2      1.18       cgd 
      3       1.1       cgd /*-
      4      1.32       jrf  * Copyright (c) 1990, 1993, 1994, 2003
      5      1.15   mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15      1.36       agc  * 3. Neither the name of the University nor the names of its contributors
     16       1.1       cgd  *    may be used to endorse or promote products derived from this software
     17       1.1       cgd  *    without specific prior written permission.
     18       1.1       cgd  *
     19       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1       cgd  * SUCH DAMAGE.
     30       1.1       cgd  */
     31       1.1       cgd 
     32      1.20  christos #include <sys/cdefs.h>
     33       1.1       cgd #ifndef lint
     34      1.48     lukem __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\
     35      1.48     lukem  The Regents of the University of California.  All rights reserved.");
     36       1.1       cgd #endif /* not lint */
     37       1.1       cgd 
     38       1.1       cgd #ifndef lint
     39      1.18       cgd #if 0
     40      1.19       jtc static char sccsid[] = "@(#)rm.c	8.8 (Berkeley) 4/27/95";
     41      1.18       cgd #else
     42  1.48.4.1  sborrill __RCSID("$NetBSD: rm.c,v 1.48.4.1 2012/06/15 09:08:03 sborrill Exp $");
     43      1.18       cgd #endif
     44       1.1       cgd #endif /* not lint */
     45       1.1       cgd 
     46      1.35  jschauma #include <sys/param.h>
     47      1.35  jschauma #include <sys/stat.h>
     48      1.15   mycroft #include <sys/types.h>
     49      1.15   mycroft 
     50      1.15   mycroft #include <err.h>
     51      1.15   mycroft #include <errno.h>
     52      1.15   mycroft #include <fcntl.h>
     53      1.15   mycroft #include <fts.h>
     54      1.27       wiz #include <grp.h>
     55      1.35  jschauma #include <locale.h>
     56      1.27       wiz #include <pwd.h>
     57       1.7       jtc #include <stdio.h>
     58      1.15   mycroft #include <stdlib.h>
     59       1.7       jtc #include <string.h>
     60       1.7       jtc #include <unistd.h>
     61       1.7       jtc 
     62      1.38  jschauma int dflag, eval, fflag, iflag, Pflag, stdin_ok, vflag, Wflag;
     63       1.1       cgd 
     64      1.27       wiz int	check(char *, char *, struct stat *);
     65      1.27       wiz void	checkdot(char **);
     66      1.27       wiz void	rm_file(char **);
     67      1.45  liamjfoy int	rm_overwrite(char *, struct stat *);
     68      1.27       wiz void	rm_tree(char **);
     69      1.27       wiz void	usage(void);
     70      1.27       wiz int	main(int, char *[]);
     71       1.1       cgd 
     72       1.1       cgd /*
     73      1.21    kleink  * For the sake of the `-f' flag, check whether an error number indicates the
     74      1.21    kleink  * failure of an operation due to an non-existent file, either per se (ENOENT)
     75      1.21    kleink  * or because its filename argument was illegal (ENAMETOOLONG, ENOTDIR).
     76      1.21    kleink  */
     77      1.21    kleink #define NONEXISTENT(x) \
     78      1.21    kleink     ((x) == ENOENT || (x) == ENAMETOOLONG || (x) == ENOTDIR)
     79      1.21    kleink 
     80      1.21    kleink /*
     81       1.1       cgd  * rm --
     82       1.1       cgd  *	This rm is different from historic rm's, but is expected to match
     83       1.1       cgd  *	POSIX 1003.2 behavior.  The most visible difference is that -f
     84       1.1       cgd  *	has two specific effects now, ignore non-existent files and force
     85       1.1       cgd  * 	file removal.
     86       1.1       cgd  */
     87       1.7       jtc int
     88      1.27       wiz main(int argc, char *argv[])
     89       1.1       cgd {
     90       1.1       cgd 	int ch, rflag;
     91       1.1       cgd 
     92      1.27       wiz 	setprogname(argv[0]);
     93      1.22   mycroft 	(void)setlocale(LC_ALL, "");
     94       1.7       jtc 
     95      1.15   mycroft 	Pflag = rflag = 0;
     96      1.32       jrf 	while ((ch = getopt(argc, argv, "dfiPRrvW")) != -1)
     97      1.30     enami 		switch (ch) {
     98       1.1       cgd 		case 'd':
     99       1.1       cgd 			dflag = 1;
    100       1.1       cgd 			break;
    101       1.1       cgd 		case 'f':
    102       1.1       cgd 			fflag = 1;
    103       1.1       cgd 			iflag = 0;
    104       1.1       cgd 			break;
    105       1.1       cgd 		case 'i':
    106       1.1       cgd 			fflag = 0;
    107       1.1       cgd 			iflag = 1;
    108       1.1       cgd 			break;
    109      1.15   mycroft 		case 'P':
    110      1.15   mycroft 			Pflag = 1;
    111      1.15   mycroft 			break;
    112       1.1       cgd 		case 'R':
    113      1.15   mycroft 		case 'r':			/* Compatibility. */
    114       1.1       cgd 			rflag = 1;
    115       1.1       cgd 			break;
    116      1.32       jrf 		case 'v':
    117      1.32       jrf 			vflag = 1;
    118      1.32       jrf 			break;
    119      1.17   mycroft 		case 'W':
    120      1.17   mycroft 			Wflag = 1;
    121      1.17   mycroft 			break;
    122       1.1       cgd 		case '?':
    123       1.1       cgd 		default:
    124       1.1       cgd 			usage();
    125       1.1       cgd 		}
    126       1.1       cgd 	argc -= optind;
    127       1.1       cgd 	argv += optind;
    128       1.1       cgd 
    129      1.47  christos 	if (argc < 1) {
    130      1.47  christos 		if (fflag)
    131      1.47  christos 			return 0;
    132       1.1       cgd 		usage();
    133      1.47  christos 	}
    134       1.1       cgd 
    135       1.1       cgd 	checkdot(argv);
    136       1.1       cgd 
    137      1.11       jtc 	if (*argv) {
    138      1.11       jtc 		stdin_ok = isatty(STDIN_FILENO);
    139       1.1       cgd 
    140      1.11       jtc 		if (rflag)
    141      1.15   mycroft 			rm_tree(argv);
    142      1.11       jtc 		else
    143      1.15   mycroft 			rm_file(argv);
    144      1.11       jtc 	}
    145       1.9       jtc 
    146      1.24   mycroft 	exit(eval);
    147      1.24   mycroft 	/* NOTREACHED */
    148       1.1       cgd }
    149       1.1       cgd 
    150       1.7       jtc void
    151      1.27       wiz rm_tree(char **argv)
    152       1.1       cgd {
    153      1.15   mycroft 	FTS *fts;
    154      1.15   mycroft 	FTSENT *p;
    155      1.32       jrf 	int flags, needstat, rval;
    156      1.35  jschauma 
    157       1.1       cgd 	/*
    158       1.1       cgd 	 * Remove a file hierarchy.  If forcing removal (-f), or interactive
    159       1.1       cgd 	 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
    160       1.1       cgd 	 */
    161       1.1       cgd 	needstat = !fflag && !iflag && stdin_ok;
    162       1.1       cgd 
    163       1.1       cgd 	/*
    164       1.1       cgd 	 * If the -i option is specified, the user can skip on the pre-order
    165       1.1       cgd 	 * visit.  The fts_number field flags skipped directories.
    166       1.1       cgd 	 */
    167       1.1       cgd #define	SKIPPED	1
    168       1.1       cgd 
    169      1.17   mycroft 	flags = FTS_PHYSICAL;
    170      1.17   mycroft 	if (!needstat)
    171      1.17   mycroft 		flags |= FTS_NOSTAT;
    172      1.17   mycroft 	if (Wflag)
    173      1.17   mycroft 		flags |= FTS_WHITEOUT;
    174      1.47  christos 	if ((fts = fts_open(argv, flags, NULL)) == NULL)
    175      1.47  christos 		err(1, "fts_open failed");
    176       1.7       jtc 	while ((p = fts_read(fts)) != NULL) {
    177      1.35  jschauma 
    178      1.15   mycroft 		switch (p->fts_info) {
    179       1.1       cgd 		case FTS_DNR:
    180      1.15   mycroft 			if (!fflag || p->fts_errno != ENOENT) {
    181      1.38  jschauma 				warnx("%s: %s", p->fts_path,
    182      1.38  jschauma 						strerror(p->fts_errno));
    183      1.15   mycroft 				eval = 1;
    184      1.15   mycroft 			}
    185      1.15   mycroft 			continue;
    186       1.1       cgd 		case FTS_ERR:
    187      1.38  jschauma 			errx(EXIT_FAILURE, "%s: %s", p->fts_path,
    188      1.35  jschauma 					strerror(p->fts_errno));
    189      1.24   mycroft 			/* NOTREACHED */
    190       1.1       cgd 		case FTS_NS:
    191      1.15   mycroft 			/*
    192      1.15   mycroft 			 * FTS_NS: assume that if can't stat the file, it
    193      1.15   mycroft 			 * can't be unlinked.
    194      1.15   mycroft 			 */
    195      1.28       jmc 			if (fflag && NONEXISTENT(p->fts_errno))
    196      1.28       jmc 				continue;
    197      1.28       jmc 			if (needstat) {
    198      1.38  jschauma 				warnx("%s: %s", p->fts_path,
    199      1.38  jschauma 						strerror(p->fts_errno));
    200      1.15   mycroft 				eval = 1;
    201      1.28       jmc 				continue;
    202      1.15   mycroft 			}
    203      1.28       jmc 			break;
    204       1.1       cgd 		case FTS_D:
    205      1.15   mycroft 			/* Pre-order: give user chance to skip. */
    206       1.8       jtc 			if (!fflag && !check(p->fts_path, p->fts_accpath,
    207       1.6   deraadt 			    p->fts_statp)) {
    208       1.1       cgd 				(void)fts_set(fts, p, FTS_SKIP);
    209       1.1       cgd 				p->fts_number = SKIPPED;
    210       1.1       cgd 			}
    211       1.1       cgd 			continue;
    212       1.1       cgd 		case FTS_DP:
    213      1.15   mycroft 			/* Post-order: see if user skipped. */
    214       1.1       cgd 			if (p->fts_number == SKIPPED)
    215       1.1       cgd 				continue;
    216       1.1       cgd 			break;
    217       1.9       jtc 		default:
    218      1.15   mycroft 			if (!fflag &&
    219      1.15   mycroft 			    !check(p->fts_path, p->fts_accpath, p->fts_statp))
    220       1.9       jtc 				continue;
    221       1.1       cgd 		}
    222       1.1       cgd 
    223      1.32       jrf 		rval = 0;
    224       1.1       cgd 		/*
    225       1.1       cgd 		 * If we can't read or search the directory, may still be
    226       1.1       cgd 		 * able to remove it.  Don't print out the un{read,search}able
    227       1.1       cgd 		 * message unless the remove fails.
    228       1.1       cgd 		 */
    229      1.17   mycroft 		switch (p->fts_info) {
    230      1.17   mycroft 		case FTS_DP:
    231      1.17   mycroft 		case FTS_DNR:
    232      1.32       jrf 			rval = rmdir(p->fts_accpath);
    233      1.32       jrf 			if (rval != 0 && fflag && errno == ENOENT)
    234       1.1       cgd 				continue;
    235      1.17   mycroft 			break;
    236      1.17   mycroft 
    237      1.17   mycroft 		case FTS_W:
    238      1.32       jrf 			rval = undelete(p->fts_accpath);
    239      1.34     enami 			if (rval != 0 && fflag && errno == ENOENT)
    240      1.17   mycroft 				continue;
    241      1.17   mycroft 			break;
    242      1.17   mycroft 
    243      1.17   mycroft 		default:
    244      1.45  liamjfoy 			if (Pflag) {
    245      1.45  liamjfoy 				if (rm_overwrite(p->fts_accpath, NULL))
    246      1.45  liamjfoy 					continue;
    247      1.45  liamjfoy 			}
    248      1.32       jrf 			rval = unlink(p->fts_accpath);
    249      1.32       jrf 			if (rval != 0 && fflag && NONEXISTENT(errno))
    250      1.14       jtc 				continue;
    251      1.32       jrf 			break;
    252      1.14       jtc 		}
    253      1.32       jrf 		if (rval != 0) {
    254      1.38  jschauma 			warn("%s", p->fts_path);
    255      1.32       jrf 			eval = 1;
    256      1.38  jschauma 		} else if (vflag)
    257      1.38  jschauma 			(void)printf("%s\n", p->fts_path);
    258       1.1       cgd 	}
    259      1.15   mycroft 	if (errno)
    260      1.15   mycroft 		err(1, "fts_read");
    261      1.43     peter 	fts_close(fts);
    262       1.1       cgd }
    263       1.1       cgd 
    264       1.7       jtc void
    265      1.27       wiz rm_file(char **argv)
    266       1.1       cgd {
    267       1.1       cgd 	struct stat sb;
    268      1.15   mycroft 	int rval;
    269      1.38  jschauma 	char *f;
    270       1.1       cgd 
    271       1.1       cgd 	/*
    272       1.1       cgd 	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
    273       1.1       cgd 	 * to remove a directory is an error, so must always stat the file.
    274       1.1       cgd 	 */
    275       1.7       jtc 	while ((f = *argv++) != NULL) {
    276      1.15   mycroft 		/* Assume if can't stat the file, can't unlink it. */
    277       1.1       cgd 		if (lstat(f, &sb)) {
    278      1.17   mycroft 			if (Wflag) {
    279      1.17   mycroft 				sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
    280      1.17   mycroft 			} else {
    281      1.21    kleink 				if (!fflag || !NONEXISTENT(errno)) {
    282      1.38  jschauma 					warn("%s", f);
    283      1.17   mycroft 					eval = 1;
    284      1.17   mycroft 				}
    285      1.17   mycroft 				continue;
    286       1.9       jtc 			}
    287      1.17   mycroft 		} else if (Wflag) {
    288      1.38  jschauma 			warnx("%s: %s", f, strerror(EEXIST));
    289      1.17   mycroft 			eval = 1;
    290       1.1       cgd 			continue;
    291       1.1       cgd 		}
    292      1.17   mycroft 
    293       1.7       jtc 		if (S_ISDIR(sb.st_mode) && !dflag) {
    294      1.38  jschauma 			warnx("%s: is a directory", f);
    295      1.15   mycroft 			eval = 1;
    296       1.1       cgd 			continue;
    297       1.1       cgd 		}
    298      1.17   mycroft 		if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
    299       1.1       cgd 			continue;
    300      1.17   mycroft 		if (S_ISWHT(sb.st_mode))
    301      1.17   mycroft 			rval = undelete(f);
    302      1.17   mycroft 		else if (S_ISDIR(sb.st_mode))
    303      1.15   mycroft 			rval = rmdir(f);
    304      1.15   mycroft 		else {
    305      1.45  liamjfoy 			if (Pflag) {
    306      1.45  liamjfoy 				if (rm_overwrite(f, &sb))
    307      1.45  liamjfoy 					continue;
    308      1.45  liamjfoy 			}
    309      1.15   mycroft 			rval = unlink(f);
    310      1.15   mycroft 		}
    311      1.21    kleink 		if (rval && (!fflag || !NONEXISTENT(errno))) {
    312      1.38  jschauma 			warn("%s", f);
    313      1.15   mycroft 			eval = 1;
    314       1.7       jtc 		}
    315      1.38  jschauma 		if (vflag && rval == 0)
    316      1.38  jschauma 			(void)printf("%s\n", f);
    317      1.15   mycroft 	}
    318      1.15   mycroft }
    319       1.7       jtc 
    320      1.15   mycroft /*
    321      1.15   mycroft  * rm_overwrite --
    322      1.15   mycroft  *	Overwrite the file 3 times with varying bit patterns.
    323      1.15   mycroft  *
    324      1.41       tls  * This is an expensive way to keep people from recovering files from your
    325      1.41       tls  * non-snapshotted FFS filesystems using fsdb(8).  Really.  No more.  Only
    326      1.41       tls  * regular files are deleted, directories (and therefore names) will remain.
    327      1.15   mycroft  * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
    328      1.15   mycroft  * System V file system).  In a logging file system, you'll have to have
    329      1.15   mycroft  * kernel support.
    330      1.40       tls  *
    331      1.40       tls  * A note on standards:  U.S. DoD 5220.22-M "National Industrial Security
    332      1.40       tls  * Program Operating Manual" ("NISPOM") is often cited as a reference
    333      1.40       tls  * for clearing and sanitizing magnetic media.  In fact, a matrix of
    334      1.40       tls  * "clearing" and "sanitization" methods for various media was given in
    335      1.40       tls  * Chapter 8 of the original 1995 version of NISPOM.  However, that
    336      1.40       tls  * matrix was *removed from the document* when Chapter 8 was rewritten
    337      1.40       tls  * in Change 2 to the document in 2001.  Recently, the Defense Security
    338      1.40       tls  * Service has made a revised clearing and sanitization matrix available
    339      1.40       tls  * in Microsoft Word format on the DSS web site.  The standardization
    340      1.40       tls  * status of this matrix is unclear.  Furthermore, one must be very
    341      1.40       tls  * careful when referring to this matrix: it is intended for the "clearing"
    342      1.40       tls  * prior to reuse or "sanitization" prior to disposal of *entire media*,
    343      1.40       tls  * not individual files and the only non-physically-destructive method of
    344      1.40       tls  * "sanitization" that is permitted for magnetic disks of any kind is
    345      1.40       tls  * specifically noted to be prohibited for media that have contained
    346      1.40       tls  * Top Secret data.
    347      1.40       tls  *
    348      1.40       tls  * It is impossible to actually conform to the exact procedure given in
    349      1.40       tls  * the matrix if one is overwriting a file, not an entire disk, because
    350      1.40       tls  * the procedure requires examination and comparison of the disk's defect
    351      1.40       tls  * lists.  Any program that claims to securely erase *files* while
    352      1.44       tls  * conforming to the standard, then, is not correct.  We do as much of
    353      1.44       tls  * what the standard requires as can actually be done when erasing a
    354      1.44       tls  * file, rather than an entire disk; but that does not make us conformant.
    355      1.40       tls  *
    356      1.40       tls  * Furthermore, the presence of track caches, disk and controller write
    357      1.40       tls  * caches, and so forth make it extremely difficult to ensure that data
    358      1.42       wiz  * have actually been written to the disk, particularly when one tries
    359      1.40       tls  * to repeatedly overwrite the same sectors in quick succession.  We call
    360      1.40       tls  * fsync(), but controllers with nonvolatile cache, as well as IDE disks
    361      1.40       tls  * that just plain lie about the stable storage of data, will defeat this.
    362      1.40       tls  *
    363      1.40       tls  * Finally, widely respected research suggests that the given procedure
    364      1.40       tls  * is nowhere near sufficient to prevent the recovery of data using special
    365      1.40       tls  * forensic equipment and techniques that are well-known.  This is
    366      1.40       tls  * presumably one reason that the matrix requires physical media destruction,
    367      1.40       tls  * rather than any technique of the sort attempted here, for secret data.
    368      1.40       tls  *
    369      1.40       tls  * Caveat Emptor.
    370      1.45  liamjfoy  *
    371      1.45  liamjfoy  * rm_overwrite will return 0 on success.
    372      1.15   mycroft  */
    373      1.40       tls 
    374      1.45  liamjfoy int
    375      1.27       wiz rm_overwrite(char *file, struct stat *sbp)
    376      1.15   mycroft {
    377  1.48.4.1  sborrill 	struct stat sb, sb2;
    378      1.40       tls 	int fd, randint;
    379      1.40       tls 	char randchar;
    380      1.15   mycroft 
    381      1.15   mycroft 	fd = -1;
    382      1.15   mycroft 	if (sbp == NULL) {
    383      1.15   mycroft 		if (lstat(file, &sb))
    384      1.15   mycroft 			goto err;
    385      1.15   mycroft 		sbp = &sb;
    386       1.1       cgd 	}
    387      1.15   mycroft 	if (!S_ISREG(sbp->st_mode))
    388      1.45  liamjfoy 		return 0;
    389      1.40       tls 
    390      1.40       tls 	/* flags to try to defeat hidden caching by forcing seeks */
    391  1.48.4.1  sborrill 	if ((fd = open(file, O_RDWR|O_SYNC|O_RSYNC|O_NOFOLLOW, 0)) == -1)
    392  1.48.4.1  sborrill 		goto err;
    393  1.48.4.1  sborrill 
    394  1.48.4.1  sborrill 	if (fstat(fd, &sb2)) {
    395      1.15   mycroft 		goto err;
    396  1.48.4.1  sborrill 	}
    397  1.48.4.1  sborrill 
    398  1.48.4.1  sborrill 	if (sb2.st_dev != sbp->st_dev || sb2.st_ino != sbp->st_ino ||
    399  1.48.4.1  sborrill 	    !S_ISREG(sb2.st_mode)) {
    400  1.48.4.1  sborrill 		errno = EPERM;
    401  1.48.4.1  sborrill 		goto err;
    402  1.48.4.1  sborrill 	}
    403      1.15   mycroft 
    404      1.40       tls #define RAND_BYTES	1
    405      1.40       tls #define THIS_BYTE	0
    406      1.40       tls 
    407      1.40       tls #define	WRITE_PASS(mode, byte) do {					\
    408      1.40       tls 	off_t len;							\
    409      1.40       tls 	int wlen, i;							\
    410      1.40       tls 	char buf[8 * 1024];						\
    411      1.40       tls 									\
    412      1.40       tls 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))			\
    413      1.40       tls 		goto err;						\
    414      1.40       tls 									\
    415      1.40       tls 	if (mode == THIS_BYTE)						\
    416      1.40       tls 		memset(buf, byte, sizeof(buf));				\
    417      1.15   mycroft 	for (len = sbp->st_size; len > 0; len -= wlen) {		\
    418      1.40       tls 		if (mode == RAND_BYTES) {				\
    419      1.40       tls 			for (i = 0; i < sizeof(buf); 			\
    420      1.40       tls 			    i+= sizeof(u_int32_t))			\
    421      1.40       tls 				*(int *)(buf + i) = arc4random();	\
    422      1.40       tls 		}							\
    423      1.15   mycroft 		wlen = len < sizeof(buf) ? len : sizeof(buf);		\
    424      1.15   mycroft 		if (write(fd, buf, wlen) != wlen)			\
    425      1.15   mycroft 			goto err;					\
    426      1.15   mycroft 	}								\
    427      1.40       tls 	sync();		/* another poke at hidden caches */		\
    428      1.30     enami } while (/* CONSTCOND */ 0)
    429      1.40       tls 
    430      1.40       tls #define READ_PASS(byte) do {						\
    431      1.40       tls 	off_t len;							\
    432      1.40       tls 	int rlen;							\
    433      1.40       tls 	char pattern[8 * 1024];						\
    434      1.40       tls 	char buf[8 * 1024];						\
    435      1.40       tls 									\
    436      1.40       tls 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))			\
    437      1.40       tls 		goto err;						\
    438      1.40       tls 									\
    439      1.40       tls 	memset(pattern, byte, sizeof(pattern));				\
    440      1.40       tls 	for(len = sbp->st_size; len > 0; len -= rlen) {			\
    441      1.40       tls 		rlen = len < sizeof(buf) ? len : sizeof(buf);		\
    442      1.40       tls 		if(read(fd, buf, rlen) != rlen)				\
    443      1.40       tls 			goto err;					\
    444      1.40       tls 		if(memcmp(buf, pattern, rlen))				\
    445      1.40       tls 			goto err;					\
    446      1.40       tls 	}								\
    447      1.40       tls 	sync();		/* another poke at hidden caches */		\
    448      1.40       tls } while (/* CONSTCOND */ 0)
    449      1.40       tls 
    450      1.40       tls 	/*
    451      1.40       tls 	 * DSS sanitization matrix "clear" for magnetic disks:
    452      1.40       tls 	 * option 'c' "Overwrite all addressable locations with a single
    453      1.40       tls 	 * character."
    454      1.40       tls 	 */
    455      1.40       tls 	randint = arc4random();
    456      1.40       tls 	randchar = *(char *)&randint;
    457      1.40       tls 	WRITE_PASS(THIS_BYTE, randchar);
    458      1.40       tls 
    459      1.40       tls 	/*
    460      1.40       tls 	 * DSS sanitization matrix "sanitize" for magnetic disks:
    461      1.40       tls 	 * option 'd', sub 2 "Overwrite all addressable locations with a
    462      1.40       tls 	 * character, then its complement.  Verify "complement" character
    463      1.40       tls 	 * was written successfully to all addressable locations, then
    464      1.40       tls 	 * overwrite all addressable locations with random characters; or
    465      1.40       tls 	 * verify third overwrite of random characters."  The rest of the
    466      1.40       tls 	 * text in d-sub-2 specifies requirements for overwriting spared
    467      1.40       tls 	 * sectors; we cannot conform to it when erasing only a file, thus
    468      1.40       tls 	 * we do not conform to the standard.
    469      1.40       tls 	 */
    470      1.40       tls 
    471      1.40       tls 	/* 1. "a character" */
    472      1.40       tls 	WRITE_PASS(THIS_BYTE, 0xff);
    473      1.40       tls 
    474      1.40       tls 	/* 2. "its complement" */
    475      1.40       tls 	WRITE_PASS(THIS_BYTE, 0x00);
    476      1.40       tls 
    477      1.40       tls 	/* 3. "Verify 'complement' character" */
    478      1.40       tls 	READ_PASS(0x00);
    479      1.40       tls 
    480      1.40       tls 	/* 4. "overwrite all addressable locations with random characters" */
    481      1.40       tls 
    482      1.40       tls 	WRITE_PASS(RAND_BYTES, 0x00);
    483      1.40       tls 
    484      1.40       tls 	/*
    485      1.40       tls 	 * As the file might be huge, and we note that this revision of
    486      1.40       tls 	 * the matrix says "random characters", not "a random character"
    487      1.40       tls 	 * as the original did, we do not verify the random-character
    488      1.40       tls 	 * write; the "or" in the standard allows this.
    489      1.40       tls 	 */
    490      1.40       tls 
    491      1.45  liamjfoy 	if (close(fd) == -1) {
    492      1.45  liamjfoy 		fd = -1;
    493      1.45  liamjfoy 		goto err;
    494      1.45  liamjfoy 	}
    495      1.45  liamjfoy 
    496      1.45  liamjfoy 	return 0;
    497      1.15   mycroft 
    498      1.15   mycroft err:	eval = 1;
    499      1.38  jschauma 	warn("%s", file);
    500      1.45  liamjfoy 	if (fd != -1)
    501      1.45  liamjfoy 		close(fd);
    502      1.45  liamjfoy 	return 1;
    503       1.1       cgd }
    504       1.1       cgd 
    505       1.7       jtc int
    506      1.27       wiz check(char *path, char *name, struct stat *sp)
    507       1.1       cgd {
    508      1.15   mycroft 	int ch, first;
    509      1.15   mycroft 	char modep[15];
    510       1.1       cgd 
    511       1.1       cgd 	/* Check -i first. */
    512      1.38  jschauma 	if (iflag)
    513      1.38  jschauma 		(void)fprintf(stderr, "remove '%s'? ", path);
    514      1.38  jschauma 	else {
    515       1.1       cgd 		/*
    516       1.1       cgd 		 * If it's not a symbolic link and it's unwritable and we're
    517       1.1       cgd 		 * talking to a terminal, ask.  Symbolic links are excluded
    518      1.15   mycroft 		 * because their permissions are meaningless.  Check stdin_ok
    519      1.15   mycroft 		 * first because we may not have stat'ed the file.
    520       1.1       cgd 		 */
    521      1.25        is 		if (!stdin_ok || S_ISLNK(sp->st_mode) ||
    522      1.30     enami 		    !(access(name, W_OK) && (errno != ETXTBSY)))
    523      1.15   mycroft 			return (1);
    524       1.1       cgd 		strmode(sp->st_mode, modep);
    525      1.45  liamjfoy 		if (Pflag) {
    526      1.45  liamjfoy 			warnx(
    527      1.45  liamjfoy 			    "%s: -P was specified but file could not"
    528      1.45  liamjfoy 			    " be overwritten", path);
    529      1.45  liamjfoy 			return 0;
    530      1.45  liamjfoy 		}
    531      1.46  christos 		(void)fprintf(stderr, "override %s%s%s:%s for '%s'? ",
    532       1.8       jtc 		    modep + 1, modep[9] == ' ' ? "" : " ",
    533       1.8       jtc 		    user_from_uid(sp->st_uid, 0),
    534      1.38  jschauma 		    group_from_gid(sp->st_gid, 0), path);
    535       1.1       cgd 	}
    536       1.1       cgd 	(void)fflush(stderr);
    537       1.1       cgd 
    538       1.1       cgd 	first = ch = getchar();
    539       1.1       cgd 	while (ch != '\n' && ch != EOF)
    540       1.1       cgd 		ch = getchar();
    541      1.15   mycroft 	return (first == 'y' || first == 'Y');
    542       1.1       cgd }
    543       1.1       cgd 
    544      1.16       jtc /*
    545      1.16       jtc  * POSIX.2 requires that if "." or ".." are specified as the basename
    546      1.16       jtc  * portion of an operand, a diagnostic message be written to standard
    547      1.16       jtc  * error and nothing more be done with such operands.
    548      1.16       jtc  *
    549      1.16       jtc  * Since POSIX.2 defines basename as the final portion of a path after
    550      1.16       jtc  * trailing slashes have been removed, we'll remove them here.
    551      1.16       jtc  */
    552      1.20  christos #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
    553       1.7       jtc void
    554      1.27       wiz checkdot(char **argv)
    555       1.1       cgd {
    556      1.15   mycroft 	char *p, **save, **t;
    557       1.1       cgd 	int complained;
    558       1.1       cgd 
    559       1.1       cgd 	complained = 0;
    560       1.1       cgd 	for (t = argv; *t;) {
    561      1.16       jtc 		/* strip trailing slashes */
    562      1.31     enami 		p = strrchr(*t, '\0');
    563      1.16       jtc 		while (--p > *t && *p == '/')
    564      1.16       jtc 			*p = '\0';
    565      1.16       jtc 
    566      1.16       jtc 		/* extract basename */
    567      1.15   mycroft 		if ((p = strrchr(*t, '/')) != NULL)
    568       1.1       cgd 			++p;
    569       1.1       cgd 		else
    570       1.1       cgd 			p = *t;
    571      1.16       jtc 
    572       1.1       cgd 		if (ISDOT(p)) {
    573       1.8       jtc 			if (!complained++)
    574      1.15   mycroft 				warnx("\".\" and \"..\" may not be removed");
    575      1.15   mycroft 			eval = 1;
    576      1.15   mycroft 			for (save = t; (t[0] = t[1]) != NULL; ++t)
    577      1.13       jtc 				continue;
    578       1.1       cgd 			t = save;
    579       1.1       cgd 		} else
    580       1.1       cgd 			++t;
    581       1.1       cgd 	}
    582       1.1       cgd }
    583       1.1       cgd 
    584       1.7       jtc void
    585      1.27       wiz usage(void)
    586       1.1       cgd {
    587      1.30     enami 
    588      1.32       jrf 	(void)fprintf(stderr, "usage: %s [-f|-i] [-dPRrvW] file ...\n",
    589      1.29     soren 	    getprogname());
    590       1.1       cgd 	exit(1);
    591      1.23   mycroft 	/* NOTREACHED */
    592       1.1       cgd }
    593