【发布时间】:2014-06-18 02:58:43
【问题描述】:
我有一个脚本,它可以扫描每个本地文件系统以查找全球可写文件。任何找到的文件都将写入输出文件。它还使用另一个文件,该文件提供要忽略的文件列表。
我们安装了 Tivoli 监视代理程序,出于某种奇怪的原因,它被设计为在其安装路径中创建具有全局可写权限的每个文件。众所周知,我们对此无能为力,我们想简单地忽略整个目录。
我想我可以使用诸如 /opt/IBM/ITM/* 这样的 glob,但我对如何做到这一点一无所知。
目前我已将目录硬编码到脚本中。这不太理想,但很实用。我更愿意将它放在排除列表中。
在Code Review,有人建议我使用File::Find::prune。不幸的是,这没有奏效。根据我对File::Find::prune 的收集和理解,如果它在/opt/IBM/ITM/.../.../file.txt 找到一个应该被排除的文件,它将跳过整个/opt/IBM/ITM/.../.../ 目录。这很好,但这意味着我需要为/opt/IBM/ITM/ 的每个子目录设置一个排除项。考虑到有多少子目录和子子目录,这将是一项繁琐的工作。
我确实尝试在/opt/IBM/ITM/ 下放置一个全球可写文件并将其添加到排除列表中,但它不起作用。我猜是因为没有先找到它。
脚本:
#!/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 (<MT>) {
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 = <<HEADER;
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! !!
!! /usr/local/etc/world_writable_excludes.txt is !!
!! is missing or empty. This report includes !!
!! every world-writable file including those which !!
!! are expected and should be excluded. !!
!! !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
HEADER
} else {
open XCLD, "<${exclude}" or die "Cannot open ${exclude}, $!\n";
while (<XCLD>) {
chomp;
$excludes{$_} = 1;
}
}
sub wanted {
my @dirStats = stat($File::Find::name);
# Is it excluded from the report...
return if exists $excludes{$File::Find::name};
# ...is the Tivoli installation directory...
return if ($File::Find::name =~ /\b\/ITM\b/);
# ...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);
finddepth(\&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}, $!";
}
其他地方也有人建议我使用 File::Find::Rule。我宁愿避免这样做,因为我不想完全重写脚本。
正如我所说,上面的脚本有效。不过,我不希望对排除项进行硬编码。弄清楚如何做到这一点也可以让我删除与“特殊”目录的匹配。
【问题讨论】:
标签: perl filesystems