Bugs migrated to Launchpad
[safe-rm.git] / safe-rm
blobe80d1a9adcd44a0540c8cd233c4528b221aca28f
1 #!/usr/bin/perl -t
3 use warnings;
4 use strict;
5 use Cwd 'realpath';
7 our $VERSION = '0.8';
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 if ( realpath($real_rm) eq realpath($0) ) {
106 die 'safe-rm 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;
112 exit $errcode;
114 __END__
116 =head1 NAME
118 safe-rm - wrapper around the rm command to prevent accidental deletions
120 =head1 USAGE
122 safe-rm [ ... ]
123 (same arguments as rm)
125 =head1 DESCRIPTION
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
130 removed.
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
134 message instead.
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
146 =head1 CONFIGURATION
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):
153 /etc/safe-rm.conf
154 ~/.safe-rm
156 If both of these are empty, a default list of important paths will be
157 used.
159 =for stopword Wildcards
160 Wildcards are allowed in the configuration files, but be careful
162 /usr/lib/*
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:
166 rm -rf /usr/lib
168 For a full protection, you should include both of these lines:
170 /usr/lib
171 /usr/lib/*
173 =head1 EXIT STATUS
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
179 are present.
181 =head1 BUGS AND LIMITATIONS
183 Note that if you put the following in your protected paths list:
185 $ cat /etc/safe-rm.conf
186 /usr/lib
188 Then safe-rm will prevent you from deleting the directory:
190 $ rm -rf /usr/lib
191 Skipping /usr/lib
192 /bin/rm: missing operand
193 Try `/bin/rm --help' for more information.
195 However it cannot protect you from the following:
197 $ cd /usr/lib
198 $ rm -f *
200 =head1 AUTHOR
202 Francois Marier <francois@safe-rm.org.nz>
204 =head1 SEE ALSO
206 rm(1)
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/>.