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 = (
30 '/usr/local/bin' => 1,
31 '/usr/local/include' => 1,
32 '/usr/local/sbin' => 1,
33 '/usr/local/share' => 1,
40 my %protected_dirs = ();
42 sub read_config_file
{
46 if ( open my $fh, '<', $filename ) {
49 foreach my $file (glob) {
50 $protected_dirs{$file} = 1;
53 close $fh; # deliberatly ignore errors
56 print {*STDERR
} "Could not open configuration file: $filename\n";
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 = ();
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;
119 safe-rm - wrapper around the rm command to prevent accidental deletions
124 (same arguments as rm)
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
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
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
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):
157 If both of these are empty, a default list of important paths will be
160 =for stopword Wildcards
161 Wildcards are allowed in the configuration files, but be careful
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:
169 For a full protection, you should include both of these lines:
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
182 =head1 BUGS AND LIMITATIONS
184 Note that if you put the following in your protected paths list:
186 $ cat /etc/safe-rm.conf
189 Then safe-rm will prevent you from deleting the directory:
193 /bin/rm: missing operand
194 Try `/bin/rm --help' for more information.
196 However it cannot protect you from the following:
203 Francois Marier <francois@safe-rm.org.nz>
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/>.