/opt/app/bin/.../* /opt/app/config/.../.../* /opt/app/log/.../.../.../* /opt/app/tmp/* #### #!/usr/bin/perl use warnings; use strict; use Fcntl ':mode'; use File::Find; no warnings 'File::Find'; no warnings 'uninitialized'; my $dir = "/var/log/tivoli/"; my $mtab = "/etc/mtab"; my $permFile = "world_writable_files.txt"; my $tmpFile = "world_writable_files.tmp"; my $exclude = "/usr/local/etc/world_writable_excludes.txt"; my $mask = S_IWUSR | S_IWGRP | S_IWOTH; my (%excludes, %devNums); my $errHeader; # Compile a list of mountpoints that need to be scanned my @mounts; open MT, "<${mtab}" or die "Cannot open ${mtab}, $!"; # We only want the local mountpoints while () { if ($_ =~ /ext[34]/) { chomp; my @line = split; push(@mounts, $line[1]); my @stats = stat($line[1]); $devNums{$stats[0]} = undef; } } close MT; # Build a hash from /usr/local/etc/world_writables_excludes.txt if ((! -e $exclude) || (-z $exclude)) { $errHeader = <) { chomp; $excludes{$_} = 1; } } sub wanted { my @dirStats = stat($File::Find::name); # Is it excluded from the report... return if exists $excludes{$File::Find::name}; # ...in a special directory, ... return if ($File::Find::name =~ /^\bsys\b|\bproc\b|\bdev\b$/); # ...a regular file, ... return unless -f; # ...local, ... return unless (exists $devNums{$dirStats[0]}); # ...and world writable? return unless ($dirStats[2] & $mask) == $mask; # If so, add the file to the list of world writable files print(WWFILE "$File::Find::name\n"); } # Create the output file path if it doesn't already exist. mkdir($dir or die "Cannot execute mkdir on ${dir}, $!") unless (-d $dir); # Create our filehandle for writing our findings open WWFILE, ">${dir}${tmpFile}" or die "Cannot open ${dir}${tmpFile}, $!"; print(WWFILE "${errHeader}") if ($errHeader); find(\&wanted, @mounts); close WWFILE; # If no world-writable files have been found ${tmpFile} should be zero-size; # Delete it so Tivoli won't alert if (-z "${dir}${tmpFile}") { unlink "${dir}${tmpFile}"; } else { rename("${dir}${tmpFile}","${dir}${permFile}") or die "Cannot rename file ${dir}${tmpFile}, $!"; } #### sub wanted { my @dirStats = stat($File::Find::name); # Is it excluded from the report... if (exists $excludes{$File::Find::name}) { $File::Find::prune=1 if (-d _); return; } # ...in a basic directory, ... if ($File::Find::name =~ /^\bsys\b|\bproc\b|\bdev\b$/) { $File::Find::prune=1 if (-d _); return; } # ... not a regular file, ... return unless -f _; # ...local, ... return if (exists $devNums{$dirStats[0]}); # ...and world writable? my $protection = $dirStats[2]; my $writemask = (S_IWUSR | S_IWGRP | S_IWOTH); return unless $writemask == $protection & $writemask; # If so, add the file to the list of world writable files print(WWFILE "$File::Find::name\n"); }