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