import vhost-audit

This commit is contained in:
2021-03-16 20:50:02 -07:00
commit 621e821cca
224 changed files with 44154 additions and 0 deletions

27
bin/cron.pl Normal file
View File

@ -0,0 +1,27 @@
#!/usr/bin/perl -w
use strict;
use vars qw( $BASE_PATH );
use Cwd 'abs_path';
use File::Basename;
$BASE_PATH = dirname(dirname(abs_path($0)));
# read in sites.txt
open(SITES, "$BASE_PATH/sites.txt")
|| die("Error: could not read sites.txt file $!\n");
while (<SITES>) {
# FIXME: handle comments, blank lines!
chomp(my $path = $_);
my @parts = split('/', $path);
my $site_name = $parts[-2];
my $cmd = "$BASE_PATH/bin/vhost-audit.pl $path > $BASE_PATH/json/$site_name.json";
# print("$cmd\n");
# FIXME: would be better if we wrote out these files ourselves.
open(AUDIT, "$cmd |")
|| die("Error: could sudit site ($site_name) $!\n");
close(AUDIT);
}
close(SITES);

103
bin/vhost-audit.pl Normal file
View File

@ -0,0 +1,103 @@
#!/usr/bin/perl -w
use strict;
use vars qw(
$DEBUG $BAK_BASE @BAK_VERSIONS %RESULTS @SCRIPT_PATTERNS
%CHANGES %SCRIPTS
);
use Cwd 'abs_path';
use JSON;
$DEBUG = 1;
$BAK_BASE = '/.zfs/snapshot/';
@BAK_VERSIONS = (
'daily.0',
'daily.1',
'daily.2',
'weekly.0'
);
@SCRIPT_PATTERNS = ('.php$', '^.htaccess$', '.js$');
%RESULTS = ();
if (@ARGV) {
chomp(my $site = $ARGV[0]);
my $real_site = abs_path($site);
foreach my $bak_vers ( @BAK_VERSIONS ) {
my $bak_site = $BAK_BASE . $bak_vers . $real_site;
if ( -e $BAK_BASE . $bak_vers ) {
my @bak_stat = stat($BAK_BASE . $bak_vers);
my $bak_date = $bak_stat[9];
$RESULTS{'data'}{$bak_date} = ();
$RESULTS{'data'}{$bak_date}{'files'} = ();
$RESULTS{'data'}{$bak_date}{'added'} = 0;
$RESULTS{'data'}{$bak_date}{'deleted'} = 0;
$RESULTS{'data'}{$bak_date}{'changed'} = 0;
$RESULTS{'data'}{$bak_date}{'scripts'} = 0;
open(DIFF, "diff -qr $real_site $bak_site |")
|| die("Could not diff, $!\n");
while (<DIFF>) {
if ( m/^Files (.+) and (.+) differ/ ) {
my $file = substr($1, length($real_site));
$RESULTS{'data'}{$bak_date}{'files'}{$file} = 'changed';
$RESULTS{'data'}{$bak_date}{'changed'}++;
foreach my $script (@SCRIPT_PATTERNS) {
if ( $file =~ m/$script/ ) {
$RESULTS{'data'}{$bak_date}{'scripts'}++;
$SCRIPTS{$file} = 1;
last;
}
}
$CHANGES{$file} = 1;
}
elsif ( m/^Only in (.+): (.+)/ ) {
if ( substr($1, 0, length($real_site)) eq $real_site) {
my $file = substr($1, length($real_site)) . '/' . $2;
$RESULTS{'data'}{$bak_date}{'files'}{$file} = 'added';
$RESULTS{'data'}{$bak_date}{'added'}++;
foreach my $script (@SCRIPT_PATTERNS) {
if ( $file =~ m/$script/ ) {
$RESULTS{'data'}{$bak_date}{'scripts'}++;
$SCRIPTS{$file} = 1;
last;
}
}
$CHANGES{$file} = 1;
}
else {
my $file = substr($1, length($bak_site)) . '/' . $2;
$RESULTS{'data'}{$bak_date}{'files'}{$file} = 'deleted';
$RESULTS{'data'}{$bak_date}{'deleted'}++;
foreach my $script (@SCRIPT_PATTERNS) {
if ( $file =~ m/$script/ ) {
$RESULTS{'data'}{$bak_date}{'scripts'}++;
$SCRIPTS{$file} = 1;
last;
}
}
$CHANGES{$file} = 1;
}
}
else {
print('# ' . $_);
}
}
close(DIFF);
}
else {
print STDERR ($BAK_BASE . $bak_vers . " does not exist\n");
}
}
}
else {
die "Usage: $0 DocumentRoot\n";
}
$RESULTS{'lastrun'} = time();
$RESULTS{'changes'} = scalar(keys %CHANGES);
$RESULTS{'scripts'} = scalar(keys %SCRIPTS);
print(to_json(\%RESULTS, {pretty => 1}) );