9 my $homedir = $ENV{HOME
} || q{};
10 my $LEGACY_CONFIG_FILE = "$homedir/.safe-rm";
11 my $USER_CONFIG_FILE = ($ENV{XDG_CONFIG_HOME
} || "$homedir/.config") . "/safe-rm";
12 my $GLOBAL_CONFIG_FILE = '/etc/safe-rm.conf';
13 my $GLOBAL_LOCAL_CONFIG_FILE = '/usr/local/etc/safe-rm.conf';
15 my %default_protected_dirs = (
34 '/usr/local/bin' => 1,
35 '/usr/local/include' => 1,
36 '/usr/local/sbin' => 1,
37 '/usr/local/share' => 1,
44 my %protected_dirs = ();
46 sub read_config_file
{
50 if ( open my $fh, '<', $filename ) {
53 foreach my $file (glob) {
54 $protected_dirs{$file} = 1;
57 close $fh; # deliberatly ignore errors
60 print {*STDERR
} "Could not open configuration file: $filename\n";
67 read_config_file
($GLOBAL_CONFIG_FILE);
68 read_config_file
($GLOBAL_LOCAL_CONFIG_FILE);
69 read_config_file
($LEGACY_CONFIG_FILE);
70 read_config_file
($USER_CONFIG_FILE);
72 if ( 0 == scalar keys %protected_dirs ) {
73 %protected_dirs = %default_protected_dirs;
76 my @allowed_args = ();
80 # Normalize the pathname
81 my $normalized_pathname = $pathname;
82 if ( $normalized_pathname =~ m{/}xms or -e
"$normalized_pathname" ) {
84 # Convert to an absolute path (e.g. remove "..")
85 $normalized_pathname = realpath
($normalized_pathname);
86 if ( !$normalized_pathname ) {
87 $normalized_pathname = $pathname;
90 if ( $normalized_pathname =~ m{^(.+?)/+$}xms ) {
92 # Trim trailing slashes
93 $normalized_pathname = $1;
96 # Check against the exclusions
97 if ( exists $protected_dirs{$normalized_pathname} and not -l
$pathname ) {
98 print {*STDERR
} "safe-rm: skipping $pathname\n" || 0;
100 elsif ( $pathname =~ /(.*)/xms ) { # pointless untainting
101 push @allowed_args, $1;
105 # Prepare for actually deleting the file
106 local $ENV{PATH
} = q{}; # pointless untainting
107 local $ENV{CDPATH
} = q{}; # pointless untainting
108 local $ENV{IFS
} = " \t\n"; # pointless untainting
109 my $real_rm = '/bin/rm';
111 # Make sure we're not calling ourselves recursively
112 if ( realpath
($real_rm) eq realpath
($0) ) {
113 die 'safe-rm cannot find the real "rm" binary' . "\n";
116 # Run the real rm command, returning with the same error code
117 my $status = system $real_rm, @allowed_args;
118 my $errcode = $status >> 8;
125 safe-rm - wrapper around the rm command to prevent accidental deletions
130 (same arguments as rm)
134 safe-rm prevents the accidental deletion of important files by
135 replacing rm with a wrapper which checks the given arguments against a
136 configurable list of exclusions for files and directories which should
139 Users who attempt to delete one of these protected files or
140 directories will not be able to do so and will be shown a warning
143 safe-rm is meant to replace the rm command so you can achieve this by
144 putting a symbolic link with the name "rm" in a directory which sits
145 at the front of your path. For example, given this path:
147 PATH=/usr/local/bin:/bin:/usr/bin
149 You could create the following symbolic link:
151 ln -s /usr/local/bin/safe-rm /usr/local/bin/rm
155 Protected paths can be set both at the site and user levels.
157 All of these configuration files can contain a list of important files
158 or directories (one per line):
161 /usr/local/etc/safe-rm.conf
164 If all of these are empty, a default list of important paths will be
167 =for stopword Wildcards
168 Wildcards are allowed in the configuration files, but be careful
172 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:
176 For a full protection, you should include both of these lines:
183 Same exit status as the real rm command.
185 Note that if all file arguments are skipped by safe-rm then the exit status
186 will be the same as the exit status of the real rm when no files arguments
189 =head1 BUGS AND LIMITATIONS
191 Note that if you put the following in your protected paths list:
193 $ cat /etc/safe-rm.conf
196 Then safe-rm will prevent you from deleting the directory:
200 /bin/rm: missing operand
201 Try `/bin/rm --help' for more information.
203 However it cannot protect you from the following:
210 Francois Marier <francois@fmarier.org>
216 =head1 LICENSE AND COPYRIGHT
218 Copyright (C) 2008-2020 Francois Marier
220 This program is free software: you can redistribute it and/or modify
221 it under the terms of the GNU General Public License as published by
222 the Free Software Foundation, either version 3 of the License, or
223 (at your option) any later version.
225 This program is distributed in the hope that it will be useful,
226 but WITHOUT ANY WARRANTY; without even the implied warranty of
227 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
228 GNU General Public License for more details.
230 You should have received a copy of the GNU General Public License
231 along with this program. If not, see <https://www.gnu.org/licenses/>.