-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_world_write_permissions.pl
147 lines (109 loc) · 3.06 KB
/
remove_world_write_permissions.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env perl
use v5.8.9;
use strict;
use warnings;
use Cwd qw(abs_path);
use File::Basename qw(dirname);
use Getopt::Long;
use Pod::Usage;
# custom libraries
use lib dirname(abs_path $0) . '/lib';
use Utils;
# initialize variables
my ($exit_code, $options, $utils) = ();
# run the program
run();
# requires: $results (hash ref)
# returns: success as (boolean)
sub parse_results {
my $results = shift;
# display successes
if ( $results->{'success'} ) {
printf "Successfully removed world write permissions from %d files.\n",
$results->{'success'};
}
# display failures
if ( scalar keys %{ $results->{'fail'} } ) {
printf "Failed to remove world write permissions from %d files.\n",
scalar keys %{ $results->{'fail'} };
# display errors
while (my ($file, $error) = each %{ $results->{'fail'} }) {
$utils->verbose("$file : $error", 1);
}
# set shell exit code
if ( $results->{'success'} ) {
$exit_code = 3;
} else {
$exit_code = 4;
}
}
return 1;
}
# requires: n/a
# returns: 1
sub setup {
my ($go) = ();
# initialize shell exit code and options hash
$exit_code = 0;
$options = {
'dirs' => []
};
# parse command line options with bundling options
$go = Getopt::Long::Parser->new(config => ['bundling']);
$go->getoptions($options,
'verbose|v',
'help|?'
) or pod2usage "Try '$0 --help' for more information.";
# display help and exit
if ( $options->{'help'} ) { pod2usage 2; }
# instantiate class objects
$utils = Utils->new({
'verbose' => $options->{'verbose'}
});
# no arguments passed, display help and exit
if ( ! @ARGV ) { pod2usage 'At least one directory is required.'; }
# test our directories
foreach ( @ARGV ) {
if ( ! -d $_ ) {
$utils->verbose( qq("$_" is not a valid directory.), 1 );
} else {
push @{ $options->{'dirs'} }, $_;
}
}
# no valid directories, display help and exit
if ( ! @{ $options->{'dirs'} } ) {
pod2usage 'No valid directories were found.';
}
return 1;
}
sub run {
my ($files, $results) = ();
# set up options, arguments and classes
setup;
# display status message (dirs with many files may take awhile)
$utils->verbose('Searching for world writable files...');
# find all world writable files in dirs and subdirs
$files = $utils->find_world_writable_files($options->{'dirs'});
# display findings
$utils->verbose(sprintf('Found: %d', scalar keys %$files), 1);
# exit immediately if no files were found
if ( ! scalar keys %$files ) { return $exit_code; }
# display status message
$utils->verbose('Modifying permissions...');
# remove offending permissions
$results = $utils->remove_world_write_perms();
# display findings
$utils->verbose('Done.', 1);
# display final results
parse_results $results;
return $exit_code;
}
# exit to shell
exit $exit_code;
__END__
=head1 SYNOPSIS
remove_world_write_permissions.pl <directory> [<directory>] [-v]
=head1 OPTIONS
-v, --verbose Display verbose output
-?, --help This help message
=cut