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 * $Id: rm.c,v 1.9 1996/02/19 00:44:02 wosch Exp $ 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1990, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 static char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94"; 44 #endif /* not lint */ 45 46 #include <sys/types.h> 47 #include <sys/stat.h> 48 49 #include <err.h> 50 #include <errno.h> 51 #include <fcntl.h> 52 #include <fts.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 58 extern char *flags_to_string __P((u_long, char *)); 59 60 int dflag, eval, fflag, iflag, Pflag, stdin_ok; 61 uid_t uid; 62 63 int check __P((char *, char *, struct stat *)); 64 void checkdot __P((char **)); 65 void rm_file __P((char **)); 66 void rm_overwrite __P((char *, struct stat *)); 67 void rm_tree __P((char **)); 68 void usage __P((void)); 69 70 /* 71 * rm -- 72 * This rm is different from historic rm's, but is expected to match 73 * POSIX 1003.2 behavior. The most visible difference is that -f 74 * has two specific effects now, ignore non-existent files and force 75 * file removal. 76 */ 77 int 78 main(argc, argv) 79 int argc; 80 char *argv[]; 81 { 82 int ch, rflag; 83 84 rflag = 0; 85 while ((ch = getopt(argc, argv, "dfiPRr")) != EOF) 86 switch(ch) { 87 case 'd': 88 dflag = 1; 89 break; 90 case 'f': 91 fflag = 1; 92 iflag = 0; 93 break; 94 case 'i': 95 fflag = 0; 96 iflag = 1; 97 break; 98 case 'P': 99 Pflag = 1; 100 break; 101 case 'R': 102 case 'r': /* Compatibility. */ 103 rflag = 1; 104 break; 105 default: 106 usage(); 107 } 108 argc -= optind; 109 argv += optind; 110 111 if (argc < 1) 112 usage(); 113 114 checkdot(argv); 115 if (!*argv) 116 exit (eval); 117 118 stdin_ok = isatty(STDIN_FILENO); 119 uid = geteuid(); 120 121 if (rflag) 122 rm_tree(argv); 123 else 124 rm_file(argv); 125 exit (eval); 126 } 127 128 void 129 rm_tree(argv) 130 char **argv; 131 { 132 FTS *fts; 133 FTSENT *p; 134 int needstat; 135 int rval; 136 137 /* 138 * Remove a file hierarchy. If forcing removal (-f), or interactive 139 * (-i) or can't ask anyway (stdin_ok), don't stat the file. 140 */ 141 needstat = !uid || !fflag && !iflag && stdin_ok; 142 143 /* 144 * If the -i option is specified, the user can skip on the pre-order 145 * visit. The fts_number field flags skipped directories. 146 */ 147 #define SKIPPED 1 148 149 if (!(fts = fts_open(argv, 150 needstat ? FTS_PHYSICAL|FTS_NOCHDIR : 151 FTS_PHYSICAL|FTS_NOSTAT|FTS_NOCHDIR, (int (*)())NULL))) 152 err(1, NULL); 153 while ((p = fts_read(fts)) != NULL) { 154 switch (p->fts_info) { 155 case FTS_DNR: 156 if (!fflag || p->fts_errno != ENOENT) { 157 warnx("%s: %s", 158 p->fts_path, strerror(p->fts_errno)); 159 eval = 1; 160 } 161 continue; 162 case FTS_ERR: 163 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno)); 164 case FTS_NS: 165 /* 166 * FTS_NS: assume that if can't stat the file, it 167 * can't be unlinked. 168 */ 169 if (!needstat) 170 break; 171 if (!fflag || p->fts_errno != ENOENT) { 172 warnx("%s: %s", 173 p->fts_path, strerror(p->fts_errno)); 174 eval = 1; 175 } 176 continue; 177 case FTS_D: 178 /* Pre-order: give user chance to skip. */ 179 if (iflag && !check(p->fts_path, p->fts_accpath, 180 p->fts_statp)) { 181 (void)fts_set(fts, p, FTS_SKIP); 182 p->fts_number = SKIPPED; 183 } 184 else if (!uid && 185 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 186 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 187 chflags(p->fts_accpath, 188 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0) 189 goto err; 190 continue; 191 case FTS_DP: 192 /* Post-order: see if user skipped. */ 193 if (p->fts_number == SKIPPED) 194 continue; 195 break; 196 } 197 if (!fflag && 198 !check(p->fts_path, p->fts_accpath, p->fts_statp)) 199 continue; 200 201 rval = 0; 202 if (!uid && 203 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 204 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE))) 205 rval = chflags(p->fts_accpath, 206 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 207 if (!rval) { 208 /* 209 * If we can't read or search the directory, may still be 210 * able to remove it. Don't print out the un{read,search}able 211 * message unless the remove fails. 212 */ 213 if (p->fts_info == FTS_DP || p->fts_info == FTS_DNR) { 214 if (!rmdir(p->fts_accpath)) 215 continue; 216 if (errno == ENOENT) { 217 if (fflag) 218 continue; 219 } else if (p->fts_info != FTS_DP) 220 warnx("%s: unable to read", p->fts_path); 221 } else { 222 if (Pflag) 223 rm_overwrite(p->fts_accpath, NULL); 224 if (!unlink(p->fts_accpath) || (fflag && errno == ENOENT)) 225 continue; 226 } 227 } 228 err: 229 warn("%s", p->fts_path); 230 eval = 1; 231 } 232 if (errno) 233 err(1, "fts_read"); 234 } 235 236 void 237 rm_file(argv) 238 char **argv; 239 { 240 struct stat sb; 241 int df, rval; 242 char *f; 243 244 df = dflag; 245 /* 246 * Remove a file. POSIX 1003.2 states that, by default, attempting 247 * to remove a directory is an error, so must always stat the file. 248 */ 249 while ((f = *argv++) != NULL) { 250 /* Assume if can't stat the file, can't unlink it. */ 251 if (lstat(f, &sb)) { 252 if (!fflag || errno != ENOENT) { 253 warn("%s", f); 254 eval = 1; 255 } 256 continue; 257 } 258 if (S_ISDIR(sb.st_mode) && !df) { 259 warnx("%s: is a directory", f); 260 eval = 1; 261 continue; 262 } 263 if (!fflag && !check(f, f, &sb)) 264 continue; 265 rval = 0; 266 if (!uid && 267 (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) && 268 !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE))) 269 rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE)); 270 if (!rval) { 271 if (S_ISDIR(sb.st_mode)) 272 rval = rmdir(f); 273 else { 274 if (Pflag) 275 rm_overwrite(f, &sb); 276 rval = unlink(f); 277 } 278 } 279 if (rval && (!fflag || errno != ENOENT)) { 280 warn("%s", f); 281 eval = 1; 282 } 283 } 284 } 285 286 /* 287 * rm_overwrite -- 288 * Overwrite the file 3 times with varying bit patterns. 289 * 290 * XXX 291 * This is a cheap way to *really* delete files. Note that only regular 292 * files are deleted, directories (and therefore names) will remain. 293 * Also, this assumes a fixed-block file system (like FFS, or a V7 or a 294 * System V file system). In a logging file system, you'll have to have 295 * kernel support. 296 */ 297 void 298 rm_overwrite(file, sbp) 299 char *file; 300 struct stat *sbp; 301 { 302 struct stat sb; 303 off_t len; 304 int fd, wlen; 305 char buf[8 * 1024]; 306 307 fd = -1; 308 if (sbp == NULL) { 309 if (lstat(file, &sb)) 310 goto err; 311 sbp = &sb; 312 } 313 if (!S_ISREG(sbp->st_mode)) 314 return; 315 if ((fd = open(file, O_WRONLY, 0)) == -1) 316 goto err; 317 318 #define PASS(byte) { \ 319 memset(buf, byte, sizeof(buf)); \ 320 for (len = sbp->st_size; len > 0; len -= wlen) { \ 321 wlen = len < sizeof(buf) ? len : sizeof(buf); \ 322 if (write(fd, buf, wlen) != wlen) \ 323 goto err; \ 324 } \ 325 } 326 PASS(0xff); 327 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 328 goto err; 329 PASS(0x00); 330 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 331 goto err; 332 PASS(0xff); 333 if (!fsync(fd) && !close(fd)) 334 return; 335 336 err: eval = 1; 337 warn("%s", file); 338 } 339 340 341 int 342 check(path, name, sp) 343 char *path, *name; 344 struct stat *sp; 345 { 346 int ch, first; 347 char modep[15], flagsp[128]; 348 349 /* Check -i first. */ 350 if (iflag) 351 (void)fprintf(stderr, "remove %s? ", path); 352 else { 353 /* 354 * If it's not a symbolic link and it's unwritable and we're 355 * talking to a terminal, ask. Symbolic links are excluded 356 * because their permissions are meaningless. Check stdin_ok 357 * first because we may not have stat'ed the file. 358 */ 359 if (!stdin_ok || S_ISLNK(sp->st_mode) || 360 !access(name, W_OK) && 361 !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 362 (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)) 363 return (1); 364 strmode(sp->st_mode, modep); 365 strcpy(flagsp, flags_to_string(sp->st_flags, NULL)); 366 if (*flagsp) 367 strcat(flagsp, " "); 368 (void)fprintf(stderr, "override %s%s%s/%s %sfor %s? ", 369 modep + 1, modep[9] == ' ' ? "" : " ", 370 user_from_uid(sp->st_uid, 0), 371 group_from_gid(sp->st_gid, 0), 372 *flagsp ? flagsp : "", 373 path); 374 } 375 (void)fflush(stderr); 376 377 first = ch = getchar(); 378 while (ch != '\n' && ch != EOF) 379 ch = getchar(); 380 return (first == 'y'); 381 } 382 383 #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2]))) 384 void 385 checkdot(argv) 386 char **argv; 387 { 388 char *p, **save, **t; 389 int complained; 390 391 complained = 0; 392 for (t = argv; *t;) { 393 if ((p = strrchr(*t, '/')) != NULL) 394 ++p; 395 else 396 p = *t; 397 if (ISDOT(p)) { 398 if (!complained++) 399 warnx("\".\" and \"..\" may not be removed"); 400 eval = 1; 401 for (save = t; (t[0] = t[1]) != NULL; ++t); 402 t = save; 403 } else 404 ++t; 405 } 406 } 407 408 void 409 usage() 410 { 411 412 (void)fprintf(stderr, "usage: rm [-dfiPRr] file ...\n"); 413 exit(1); 414 } 415