1 /*- 2 * Copyright (c) 1990, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1990, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94"; 43 #else 44 static const char rcsid[] = 45 "$Id: rm.c,v 1.17 1997/08/07 15:37:47 steve Exp $"; 46 #endif 47 #endif /* not lint */ 48 49 #include <sys/types.h> 50 #include <sys/stat.h> 51 52 #include <err.h> 53 #include <errno.h> 54 #include <fcntl.h> 55 #include <fts.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <unistd.h> 60 61 extern char *flags_to_string __P((u_long, char *)); 62 63 int dflag, eval, fflag, iflag, Pflag, Wflag, stdin_ok; 64 uid_t uid; 65 66 int check __P((char *, char *, struct stat *)); 67 void checkdot __P((char **)); 68 void rm_file __P((char **)); 69 void rm_overwrite __P((char *, struct stat *)); 70 void rm_tree __P((char **)); 71 void usage __P((void)); 72 73 /* 74 * rm -- 75 * This rm is different from historic rm's, but is expected to match 76 * POSIX 1003.2 behavior. The most visible difference is that -f 77 * has two specific effects now, ignore non-existent files and force 78 * file removal. 79 */ 80 int 81 main(argc, argv) 82 int argc; 83 char *argv[]; 84 { 85 int ch, rflag; 86 87 Pflag = rflag = 0; 88 while ((ch = getopt(argc, argv, "dfiPRrW")) != -1) 89 switch(ch) { 90 case 'd': 91 dflag = 1; 92 break; 93 case 'f': 94 fflag = 1; 95 iflag = 0; 96 break; 97 case 'i': 98 fflag = 0; 99 iflag = 1; 100 break; 101 case 'P': 102 Pflag = 1; 103 break; 104 case 'R': 105 case 'r': /* Compatibility. */ 106 rflag = 1; 107 break; 108 case 'W': 109 Wflag = 1; 110 break; 111 default: 112 usage(); 113 } 114 argc -= optind; 115 argv += optind; 116 117 if (argc < 1) 118 usage(); 119 120 checkdot(argv); 121 uid = geteuid(); 122 123 if (*argv) { 124 stdin_ok = isatty(STDIN_FILENO); 125 126 if (rflag) 127 rm_tree(argv); 128 else 129 rm_file(argv); 130 } 131 132 exit (eval); 133 } 134 135 void 136 rm_tree(argv) 137 char **argv; 138 { 139 FTS *fts; 140 FTSENT *p; 141 int needstat; 142 int flags; 143 int rval; 144 145 /* 146 * Remove a file hierarchy. If forcing removal (-f), or interactive 147 * (-i) or can't ask anyway (stdin_ok), don't stat the file. 148 */ 149 needstat = !uid || (!fflag && !iflag && stdin_ok); 150 151 /* 152 * If the -i option is specified, the user can skip on the pre-order 153 * visit. The fts_number field flags skipped directories. 154 */ 155 #define SKIPPED 1 156 157 flags = FTS_PHYSICAL | FTS_NOCHDIR; 158 if (!needstat) 159 flags |= FTS_NOSTAT; 160 if (Wflag) 161 flags |= FTS_WHITEOUT; 162 if (!(fts = fts_open(argv, flags, (int (*)())NULL))) 163 err(1, NULL); 164 while ((p = fts_read(fts)) != NULL) { 165 switch (p->fts_info) { 166 case FTS_DNR: 167 if (!fflag || p->fts_errno != ENOENT) { 168 warnx("%s: %s", 169 p->fts_path, strerror(p->fts_errno)); 170 eval = 1; 171 } 172 continue; 173 case FTS_ERR: 174 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno)); 175 case FTS_NS: 176 /* 177 * FTS_NS: assume that if can't stat the file, it 178 * can't be unlinked. 179 */ 180 if (!needstat) 181 break; 182 if (!fflag || p->fts_errno != ENOENT) { 183 warnx("%s: %s", 184 p->fts_path, strerror(p->fts_errno)); 185 eval = 1; 186 } 187 continue; 188 case FTS_D: 189 /* Pre-order: give user chance to skip. */ 190 if (!fflag && !check(p->fts_path, p->fts_accpath, 191 p->fts_statp)) { 192 (void)fts_set(fts, p, FTS_SKIP); 193 p->fts_number = SKIPPED; 194 } 195 else if (!uid && 196 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 197 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 198 chflags(p->fts_accpath, 199 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0) 200 goto err; 201 continue; 202 case FTS_DP: 203 /* Post-order: see if user skipped. */ 204 if (p->fts_number == SKIPPED) 205 continue; 206 break; 207 default: 208 if (!fflag && 209 !check(p->fts_path, p->fts_accpath, p->fts_statp)) 210 continue; 211 } 212 213 rval = 0; 214 if (!uid && 215 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 216 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE))) 217 rval = chflags(p->fts_accpath, 218 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 219 if (!rval) { 220 /* 221 * If we can't read or search the directory, may still be 222 * able to remove it. Don't print out the un{read,search}able 223 * message unless the remove fails. 224 */ 225 switch (p->fts_info) { 226 case FTS_DP: 227 case FTS_DNR: 228 if (!rmdir(p->fts_accpath) || (fflag && errno == ENOENT)) 229 continue; 230 break; 231 232 case FTS_W: 233 if (!undelete(p->fts_accpath) || 234 (fflag && errno == ENOENT)) 235 continue; 236 break; 237 238 default: 239 if (Pflag) 240 rm_overwrite(p->fts_accpath, NULL); 241 if (!unlink(p->fts_accpath) || (fflag && errno == ENOENT)) 242 continue; 243 } 244 } 245 err: 246 warn("%s", p->fts_path); 247 eval = 1; 248 } 249 if (errno) 250 err(1, "fts_read"); 251 } 252 253 void 254 rm_file(argv) 255 char **argv; 256 { 257 struct stat sb; 258 int rval; 259 char *f; 260 261 /* 262 * Remove a file. POSIX 1003.2 states that, by default, attempting 263 * to remove a directory is an error, so must always stat the file. 264 */ 265 while ((f = *argv++) != NULL) { 266 /* Assume if can't stat the file, can't unlink it. */ 267 if (lstat(f, &sb)) { 268 if (Wflag) { 269 sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR; 270 } else { 271 if (!fflag || errno != ENOENT) { 272 warn("%s", f); 273 eval = 1; 274 } 275 continue; 276 } 277 } else if (Wflag) { 278 warnx("%s: %s", f, strerror(EEXIST)); 279 eval = 1; 280 continue; 281 } 282 283 if (S_ISDIR(sb.st_mode) && !dflag) { 284 warnx("%s: is a directory", f); 285 eval = 1; 286 continue; 287 } 288 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb)) 289 continue; 290 rval = 0; 291 if (!uid && 292 (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) && 293 !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE))) 294 rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE)); 295 if (!rval) { 296 if (S_ISWHT(sb.st_mode)) 297 rval = undelete(f); 298 else if (S_ISDIR(sb.st_mode)) 299 rval = rmdir(f); 300 else { 301 if (Pflag) 302 rm_overwrite(f, &sb); 303 rval = unlink(f); 304 } 305 } 306 if (rval && (!fflag || errno != ENOENT)) { 307 warn("%s", f); 308 eval = 1; 309 } 310 } 311 } 312 313 /* 314 * rm_overwrite -- 315 * Overwrite the file 3 times with varying bit patterns. 316 * 317 * XXX 318 * This is a cheap way to *really* delete files. Note that only regular 319 * files are deleted, directories (and therefore names) will remain. 320 * Also, this assumes a fixed-block file system (like FFS, or a V7 or a 321 * System V file system). In a logging file system, you'll have to have 322 * kernel support. 323 */ 324 void 325 rm_overwrite(file, sbp) 326 char *file; 327 struct stat *sbp; 328 { 329 struct stat sb; 330 off_t len; 331 int fd, wlen; 332 char buf[8 * 1024]; 333 334 fd = -1; 335 if (sbp == NULL) { 336 if (lstat(file, &sb)) 337 goto err; 338 sbp = &sb; 339 } 340 if (!S_ISREG(sbp->st_mode)) 341 return; 342 if ((fd = open(file, O_WRONLY, 0)) == -1) 343 goto err; 344 345 #define PASS(byte) { \ 346 memset(buf, byte, sizeof(buf)); \ 347 for (len = sbp->st_size; len > 0; len -= wlen) { \ 348 wlen = len < sizeof(buf) ? len : sizeof(buf); \ 349 if (write(fd, buf, wlen) != wlen) \ 350 goto err; \ 351 } \ 352 } 353 PASS(0xff); 354 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 355 goto err; 356 PASS(0x00); 357 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 358 goto err; 359 PASS(0xff); 360 if (!fsync(fd) && !close(fd)) 361 return; 362 363 err: eval = 1; 364 warn("%s", file); 365 } 366 367 368 int 369 check(path, name, sp) 370 char *path, *name; 371 struct stat *sp; 372 { 373 int ch, first; 374 char modep[15], flagsp[128]; 375 376 /* Check -i first. */ 377 if (iflag) 378 (void)fprintf(stderr, "remove %s? ", path); 379 else { 380 /* 381 * If it's not a symbolic link and it's unwritable and we're 382 * talking to a terminal, ask. Symbolic links are excluded 383 * because their permissions are meaningless. Check stdin_ok 384 * first because we may not have stat'ed the file. 385 */ 386 if (!stdin_ok || S_ISLNK(sp->st_mode) || 387 (!access(name, W_OK) && 388 !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 389 (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))) 390 return (1); 391 strmode(sp->st_mode, modep); 392 strcpy(flagsp, flags_to_string(sp->st_flags, NULL)); 393 if (*flagsp) 394 strcat(flagsp, " "); 395 (void)fprintf(stderr, "override %s%s%s/%s %sfor %s? ", 396 modep + 1, modep[9] == ' ' ? "" : " ", 397 user_from_uid(sp->st_uid, 0), 398 group_from_gid(sp->st_gid, 0), 399 *flagsp ? flagsp : "", 400 path); 401 } 402 (void)fflush(stderr); 403 404 first = ch = getchar(); 405 while (ch != '\n' && ch != EOF) 406 ch = getchar(); 407 return (first == 'y' || first == 'Y'); 408 } 409 410 #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2]))) 411 void 412 checkdot(argv) 413 char **argv; 414 { 415 char *p, **save, **t; 416 int complained; 417 418 complained = 0; 419 for (t = argv; *t;) { 420 if ((p = strrchr(*t, '/')) != NULL) 421 ++p; 422 else 423 p = *t; 424 if (ISDOT(p)) { 425 if (!complained++) 426 warnx("\".\" and \"..\" may not be removed"); 427 eval = 1; 428 for (save = t; (t[0] = t[1]) != NULL; ++t) 429 continue; 430 t = save; 431 } else 432 ++t; 433 } 434 } 435 436 void 437 usage() 438 { 439 440 (void)fprintf(stderr, "usage: rm [-f | -i] [-dPRrW] file ...\n"); 441 exit(1); 442 } 443