summaryrefslogtreecommitdiff
path: root/bin/rm/rm.c
diff options
context:
space:
mode:
authorDaniel Dickman <daniel@cvs.openbsd.org>2015-10-11 03:08:47 +0000
committerDaniel Dickman <daniel@cvs.openbsd.org>2015-10-11 03:08:47 +0000
commit20b5117dc17ca67292a67e1c2cd35b393135cad8 (patch)
tree3f1e13f3009104d98e09aa02fba639e9f55cfaa4 /bin/rm/rm.c
parent31a9155b0e47397dc9778dc10d4b29fed247cdf7 (diff)
Don't allow "rm -rf /"
Patch from Theo Buehler who was inspired by watching Bryan Cantrill in BSD Now 103. Minor tweak from me to turn the complained variables into flags instead of counters. "i think it's ok" tedu@ "this isn't 1980 anymore" deraadt@ ok millert@
Diffstat (limited to 'bin/rm/rm.c')
-rw-r--r--bin/rm/rm.c34
1 files changed, 22 insertions, 12 deletions
diff --git a/bin/rm/rm.c b/bin/rm/rm.c
index 49ed97e2345..e5e339e9dab 100644
--- a/bin/rm/rm.c
+++ b/bin/rm/rm.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rm.c,v 1.32 2015/10/09 01:37:06 deraadt Exp $ */
+/* $OpenBSD: rm.c,v 1.33 2015/10/11 03:08:46 daniel Exp $ */
/* $NetBSD: rm.c,v 1.19 1995/09/07 06:48:50 jtc Exp $ */
/*-
@@ -54,7 +54,7 @@ extern char *__progname;
int dflag, eval, fflag, iflag, Pflag, stdin_ok;
int check(char *, char *, struct stat *);
-void checkdot(char **);
+void checkdotorslash(char **);
void rm_file(char **);
int rm_overwrite(char *, struct stat *);
int pass(int, off_t, char *, size_t);
@@ -113,7 +113,7 @@ main(int argc, char *argv[])
if (argc < 1 && fflag == 0)
usage();
- checkdot(argv);
+ checkdotorslash(argv);
if (*argv) {
stdin_ok = isatty(STDIN_FILENO);
@@ -391,12 +391,12 @@ check(char *path, char *name, struct stat *sp)
*/
#define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
void
-checkdot(char **argv)
+checkdotorslash(char **argv)
{
char *p, **save, **t;
- int complained;
+ int dotcomplained, slashcomplained;
- complained = 0;
+ dotcomplained = slashcomplained = 0;
for (t = argv; *t;) {
/* strip trailing slashes */
p = strrchr (*t, '\0');
@@ -410,14 +410,24 @@ checkdot(char **argv)
p = *t;
if (ISDOT(p)) {
- if (!complained++)
+ if (!dotcomplained) {
+ dotcomplained = 1;
warnx("\".\" and \"..\" may not be removed");
- eval = 1;
- for (save = t; (t[0] = t[1]) != NULL; ++t)
- continue;
- t = save;
- } else
+ }
+ } else if (*p == '\0') {
+ if (!slashcomplained) {
+ slashcomplained = 1;
+ warnx("\"/\" may not be removed");
+ }
+ } else {
++t;
+ continue;
+ }
+
+ eval = 1;
+ for (save = t; (t[0] = t[1]) != NULL; ++t)
+ continue;
+ t = save;
}
}