Add another environment variable to untaint
[safe-rm.git] / safe-rm
bloba5a4be66eac0391d9e956203b608f8b90391a698
1 #!/usr/bin/perl -t
3 use warnings;
4 use strict;
5 use Cwd 'realpath';
7 our $VERSION = '0.6';
9 my $homedir = $ENV{HOME} || q{};
10 my $USER_CONFIG_FILE = "$homedir/.safe-rm";
11 my $GLOBAL_CONFIG_FILE = '/etc/safe-rm.conf';
13 my %default_protected_dirs = (
14 '/bin' => 1,
15 '/boot' => 1,
16 '/dev' => 1,
17 '/etc' => 1,
18 '/home' => 1,
19 '/initrd' => 1,
20 '/lib' => 1,
21 '/proc' => 1,
22 '/root' => 1,
23 '/sbin' => 1,
24 '/sys' => 1,
25 '/usr' => 1,
26 '/usr/bin' => 1,
27 '/usr/include' => 1,
28 '/usr/lib' => 1,
29 '/usr/local' => 1,
30 '/usr/local/bin' => 1,
31 '/usr/local/include' => 1,
32 '/usr/local/sbin' => 1,
33 '/usr/local/share' => 1,
34 '/usr/sbin' => 1,
35 '/usr/share' => 1,
36 '/usr/src' => 1,
37 '/var' => 1,
40 my %protected_dirs = ();
42 sub read_config_file {
43 my $filename = shift;
45 if ( -e $filename ) {
46 if ( open my $fh, '<', $filename ) {
47 while (<$fh>) {
48 chomp;
49 foreach my $file (glob) {
50 $protected_dirs{$file} = 1;
53 close $fh; # deliberatly ignore errors
55 else {
56 print {*STDERR} "Could not open configuration file: $filename\n";
60 return;
63 read_config_file($GLOBAL_CONFIG_FILE);
64 read_config_file($USER_CONFIG_FILE);
66 if ( 0 == scalar keys %protected_dirs ) {
67 %protected_dirs = %default_protected_dirs;
70 my @allowed_args = ();
71 foreach (@ARGV) {
72 my $pathname = $_;
74 # Normalize the pathname
75 my $normalized_pathname = $pathname;
76 if ( $normalized_pathname =~ m{/}xms or -e "$normalized_pathname" ) {
78 # Convert to an absolute path (e.g. remove "..")
79 $normalized_pathname = realpath($normalized_pathname);
80 if ( !$normalized_pathname ) {
81 $normalized_pathname = $pathname;
84 if ( $normalized_pathname =~ m{^(.+?)/+$}xms ) {
86 # Trim trailing slashes
87 $normalized_pathname = $1;
90 # Check against the blacklist
91 if ( exists $protected_dirs{$normalized_pathname} and not -l $pathname ) {
92 print {*STDERR} "safe-rm: skipping $pathname\n" || 0;
94 elsif ( $pathname =~ /(.*)/xms ) { # pointless untainting
95 push @allowed_args, $1;
99 # Prepare for actually deleting the file
100 local $ENV{PATH} = q{}; # pointless untainting
101 local $ENV{CDPATH} = q{}; # pointless untainting
102 my $real_rm = '/bin/rm';
104 # Make sure we're not calling ourselves recursively
105 use English '-no_match_vars';
106 if ( realpath($real_rm) eq realpath($PROGRAM_NAME) ) {
107 die 'Cannot find the real "rm" binary' . "\n";
110 # Run the real rm command, returning with the same error code
111 my $status = system $real_rm, @allowed_args;
112 my $errcode = $status >> 8;
113 exit $errcode;
115 __END__
117 =head1 NAME
119 safe-rm - wrapper around the rm command to prevent accidental deletions
121 =head1 USAGE
123 safe-rm [ ... ]
124 (same arguments as rm)
126 =head1 DESCRIPTION
128 safe-rm prevents the accidental deletion of important files by
129 replacing rm with a wrapper which checks the given arguments against a
130 configurable blacklist of files and directories which should never be
131 removed.
133 Users who attempt to delete one of these protected files or
134 directories will not be able to do so and will be shown a warning
135 message instead.
137 safe-rm is meant to replace the rm command so you can achieve this by
138 putting a symbolic link with the name "rm" in a directory which sits
139 at the front of your path. For example, given this path:
141 PATH=/usr/local/bin:/bin:/usr/bin
143 You could create the following symbolic link:
145 ln -s /usr/local/bin/safe-rm /usr/local/bin/rm
147 =head1 CONFIGURATION
149 Protected paths can be set both at the site and user levels.
151 Both of these configuration files can contain a list of important files
152 or directories (one per line):
154 /etc/safe-rm.conf
155 ~/.safe-rm
157 If both of these are empty, a default list of important paths will be
158 used.
160 =for stopword Wildcards
161 Wildcards are allowed in the configuration files, but be careful
163 /usr/lib/*
165 will protect all of the files inside the /usr/lib directory if they are referred to directly, but it will not protect your system against:
167 rm -rf /usr/lib
169 For a full protection, you should include both of these lines:
171 /usr/lib
172 /usr/lib/*
174 =head1 EXIT STATUS
176 Same exit status as the real rm command.
178 Note that if all file arguments are skipped by safe-rm then the exit status
179 will be the same as the exit status of the real rm when no files arguments
180 are present.
182 =head1 BUGS AND LIMITATIONS
184 Note that if you put the following in your protected paths list:
186 $ cat /etc/safe-rm.conf
187 /usr/lib
189 Then safe-rm will prevent you from deleting the directory:
191 $ rm -rf /usr/lib
192 Skipping /usr/lib
193 /bin/rm: missing operand
194 Try `/bin/rm --help' for more information.
196 However it cannot protect you from the following:
198 $ cd /usr/lib
199 $ rm -f *
201 =head1 AUTHOR
203 Francois Marier <francois@safe-rm.org.nz>
205 =head1 SEE ALSO
207 rm(1)
209 =head1 LICENSE AND COPYRIGHT
211 Copyright (C) 2008-2009 Francois Marier
213 This program is free software: you can redistribute it and/or modify
214 it under the terms of the GNU General Public License as published by
215 the Free Software Foundation, either version 3 of the License, or
216 (at your option) any later version.
218 This program is distributed in the hope that it will be useful,
219 but WITHOUT ANY WARRANTY; without even the implied warranty of
220 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
221 GNU General Public License for more details.
223 You should have received a copy of the GNU General Public License
224 along with this program. If not, see <http://www.gnu.org/licenses/>.