Fix typo in use line
[safe-rm.git] / safe-rm
blobe97b8ad2fe151452533e056c97c501bcec94fe73
1 #!/usr/bin/perl -t
3 use warnings;
4 use strict;
5 use autodie;
6 use Cwd 'realpath';
8 our $VERSION = '0.5';
10 my $homedir = $ENV{HOME} || q{};
11 my $USER_CONFIG_FILE = "$homedir/.safe-rm";
12 my $GLOBAL_CONFIG_FILE = '/etc/safe-rm.conf';
14 my %default_protected_dirs = (
15 '/bin' => 1,
16 '/boot' => 1,
17 '/dev' => 1,
18 '/etc' => 1,
19 '/home' => 1,
20 '/initrd' => 1,
21 '/lib' => 1,
22 '/proc' => 1,
23 '/root' => 1,
24 '/sbin' => 1,
25 '/sys' => 1,
26 '/usr' => 1,
27 '/usr/bin' => 1,
28 '/usr/include' => 1,
29 '/usr/lib' => 1,
30 '/usr/local' => 1,
31 '/usr/local/bin' => 1,
32 '/usr/local/include' => 1,
33 '/usr/local/sbin' => 1,
34 '/usr/local/share' => 1,
35 '/usr/sbin' => 1,
36 '/usr/share' => 1,
37 '/usr/src' => 1,
38 '/var' => 1,
41 my %protected_dirs = ();
43 # Read in system-wide configuration file
44 if ( -r $GLOBAL_CONFIG_FILE ) {
45 open my $systemwide_cfg, '<', $GLOBAL_CONFIG_FILE;
46 while (<$systemwide_cfg>) {
47 chomp;
48 $protected_dirs{$_} = 1;
50 close $systemwide_cfg;
53 # Read in user configuration file
54 if ( -r $USER_CONFIG_FILE ) {
55 open my $user_cfg, '<', $USER_CONFIG_FILE;
56 while (<$user_cfg>) {
57 chomp;
58 $protected_dirs{$_} = 1;
60 close $user_cfg;
63 if ( 0 == scalar keys %protected_dirs ) {
64 %protected_dirs = %default_protected_dirs;
67 my @allowed_args = ();
68 foreach (@ARGV) {
69 my $pathname = $_;
71 # Normalize the pathname
72 my $normalized_pathname = $pathname;
73 if ( $normalized_pathname =~ m{/}xms or -e "$normalized_pathname" ) {
75 # Convert to an absolute path (e.g. remove "..")
76 $normalized_pathname = realpath($normalized_pathname);
77 if ( !$normalized_pathname ) {
78 $normalized_pathname = $pathname;
81 if ( $normalized_pathname =~ m{^(.+?)/+$}xms ) {
83 # Trim trailing slashes
84 $normalized_pathname = $1;
87 # Check against the blacklist
88 if ( exists $protected_dirs{$normalized_pathname} ) {
89 print {*STDERR} "Skipping $pathname\n" || 0;
91 else {
92 push @allowed_args, $pathname;
96 # Prepare for actually deleting the file
97 local $ENV{PATH} = q{}; # Because of taintness
98 my $real_rm = '/bin/rm';
100 # Make sure we're not calling ourselves recursively
101 use English '-no_match_vars';
102 if ( realpath($real_rm) eq realpath($PROGRAM_NAME) ) {
103 die 'Cannot find the real "rm" binary' . "\n";
106 # Run the real rm command
107 my $status = system $real_rm, @allowed_args;
109 # Make our exit code the one returned by the real rm
110 use Readonly;
111 Readonly::Scalar my $ONE_BYTE => 8;
112 my $errcode = $status >> $ONE_BYTE;
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 =head1 EXIT STATUS
162 Same exit status as the real rm command.
164 Note that if all file arguments are skipped by safe-rm then the exit status
165 will be the same as the exit status of the real rm when no files arguments
166 are present.
168 =head1 DEPENDENCIES
170 =for stopword autodie
171 - Perl 5.8 or later
172 - autodie
174 =for stopword CPAN
175 autodie is included by default in Perl 5.10 but can also be found on CPAN:
177 http://search.cpan.org/dist/autodie/
179 =head1 BUGS AND LIMITATIONS
181 Note that if you put the following in your protected paths list:
183 $ cat /etc/safe-rm.conf
184 /usr/lib
186 Then safe-rm will prevent you from deleting the directory:
188 $ rm -rf /usr/lib
189 Skipping /usr/lib
190 /bin/rm: missing operand
191 Try `/bin/rm --help' for more information.
193 However it cannot protect you from the following:
195 $ cd /usr/lib
196 $ rm -f *
198 =head1 AUTHOR
200 =for stopword Marier
201 Francois Marier <francois@safe-rm.org.nz>
203 =head1 SEE ALSO
205 rm(1)
207 =head1 LICENSE AND COPYRIGHT
209 =for stopword Marier
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/>.