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} ) {
92 print {*STDERR
} "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 my $real_rm = '/bin/rm';
103 # Make sure we're not calling ourselves recursively
104 use English
'-no_match_vars';
105 if ( realpath
($real_rm) eq realpath
($PROGRAM_NAME) ) {
106 die 'Cannot find the real "rm" binary' . "\n";
109 # Run the real rm command, returning with the same error code
110 my $status = system $real_rm, @allowed_args;
111 my $errcode = $status >> 8;
118 safe-rm - wrapper around the rm command to prevent accidental deletions
123 (same arguments as rm)
127 safe-rm prevents the accidental deletion of important files by
128 replacing rm with a wrapper which checks the given arguments against a
129 configurable blacklist of files and directories which should never be
132 Users who attempt to delete one of these protected files or
133 directories will not be able to do so and will be shown a warning
136 safe-rm is meant to replace the rm command so you can achieve this by
137 putting a symbolic link with the name "rm" in a directory which sits
138 at the front of your path. For example, given this path:
140 PATH=/usr/local/bin:/bin:/usr/bin
142 You could create the following symbolic link:
144 ln -s /usr/local/bin/safe-rm /usr/local/bin/rm
148 Protected paths can be set both at the site and user levels.
150 Both of these configuration files can contain a list of important files
151 or directories (one per line):
156 If both of these are empty, a default list of important paths will be
159 =for stopword Wildcards
160 Wildcards are allowed in the configuration files, but be careful
164 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:
168 For a full protection, you should include both of these lines:
175 Same exit status as the real rm command.
177 Note that if all file arguments are skipped by safe-rm then the exit status
178 will be the same as the exit status of the real rm when no files arguments
181 =head1 BUGS AND LIMITATIONS
183 Note that if you put the following in your protected paths list:
185 $ cat /etc/safe-rm.conf
188 Then safe-rm will prevent you from deleting the directory:
192 /bin/rm: missing operand
193 Try `/bin/rm --help' for more information.
195 However it cannot protect you from the following:
202 Francois Marier <francois@safe-rm.org.nz>
208 =head1 LICENSE AND COPYRIGHT
210 Copyright (C) 2008-2009 Francois Marier
212 This program is free software: you can redistribute it and/or modify
213 it under the terms of the GNU General Public License as published by
214 the Free Software Foundation, either version 3 of the License, or
215 (at your option) any later version.
217 This program is distributed in the hope that it will be useful,
218 but WITHOUT ANY WARRANTY; without even the implied warranty of
219 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
220 GNU General Public License for more details.
222 You should have received a copy of the GNU General Public License
223 along with this program. If not, see <http://www.gnu.org/licenses/>.