Hey guys, I am getting an annoying warning when running this piece of code on my FreeBSD machine with Perl 5.8.9. It doesn't have the errors under Linux with Perl 5.10
Anyhow, this is the code:
and the output when run:
Any ideas? The line in question is the return statement in generate_file_hash()..
Anyhow, this is the code:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use vars qw($reinit_sysintegrity);
# this value will be read from a config file placed on the monitoring server
$reinit_sysintegrity = 0;
my ($test, $error) = &get_sys_integrity;
print ("Any Changes?: $test\n$error\n");
#=- Check System File Integrity
sub get_sys_integrity {
# work on improving the place this file is stored
# $BASEDIRECTORY keeps the fileinit list of hashes
our $BASEDIRECTORY = "/tmp";
our $MASTERCOUNT = 0;
our @FILES = ("/etc/passwd", "/etc/shadow", "/etc/group", "/etc/gshadow");
our @DIRECTORIES = ("/etc/rc.d", "/bin", "/usr/bin", "/sbin", "/usr/sbin");
if($reinit_sysintegrity == 1){
my $findresults = `find $BASEDIRECTORY 2>/dev/null`;
my @results = split(/\n/,$findresults);
foreach my $result (@results){
$_ = $result;
if(/CHECK\.\d\d/){
unlink($result);
}
}
&init_files("fileinit");
&init_directories("fileinit");
print "Initialization Of $MASTERCOUNT Files Successful\n";
return (0, "none"); # 0 for changes, and "none" for diff string
}
else{
my $CHECKCOUNT = 0;
my $findresults = `find $BASEDIRECTORY 2>/dev/null`;
my @results = split(/\n/,$findresults);
foreach my $result (@results){
$_ = $result;
if(/CHECK\.\d\d/){
$CHECKCOUNT++; # maybe reinitialize the file after n checks
}
}
my $checkfilename = "CHECK." . time;
&init_files($checkfilename);
&init_directories($checkfilename);
my $diff_command = "diff $BASEDIRECTORY/fileinit $BASEDIRECTORY/$checkfilename";
my $diff_results = `$diff_command`;
if(length($diff_results) > 0){
my $difference = "diff fileinit $checkfilename\n" . $diff_results;
print($difference);
return (1, $difference); # 1 for changes to sys files, and the full diff string
}
else{
unlink("$BASEDIRECTORY/$checkfilename");
print "Checked $MASTERCOUNT Files, Found No Changes\n";
return (0, "none"); # 0 for changes, and "none" for diff string
}
}
sub init_files(){
my $INITNAME = shift;
open(FILEINIT,">$BASEDIRECTORY/$INITNAME") ||
die ("couldn't open $BASEDIRECTORY/$INITNAME");
foreach my $file (@FILES){
my $hash = &generate_file_hash($file);
print FILEINIT ($hash . "\n");
}
close(FILEINIT);
}
sub init_directories(){
my $INITNAME = shift;
open(FILEINIT,">>$BASEDIRECTORY/$INITNAME") ||
die ("couldn't open $BASEDIRECTORY/$INITNAME");
foreach my $directory (@DIRECTORIES){
my $findresults = `find $directory`;
my @results = split(/\n/,$findresults);
foreach my $result (@results){
my $hash = &generate_file_hash($result);
print FILEINIT ($hash . "\n");
}
}
close(FILEINIT);
}
sub generate_file_hash(){
my $filename = shift;
$MASTERCOUNT++;
my ($devs, $ino, $mode, $nlink, $uid, $gid, $rdev,
$size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($filename);
return($filename . ",PERMISSIONS=" . $devs . ",INODE=" . $ino .
",MODE=" . $mode . ",HARD LINKS=" .
$nlink . ",UID=" . $uid . ",GID=" . $gid .
",DEVICE=" . $rdev . ",SIZE=" .
$size . ",LAST MODIFIED=" . $mtime .
",INODE CREATED=" . $ctime . ",BLOCK SIZE=" .
$blksize . ",BLOCKS=" . $blocks);
}
}
and the output when run:
Code:
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
Use of uninitialized value in concatenation (.) or string at ./sys2.pl line 112.
diff fileinit CHECK.1269153366
1c1
< /etc/passwd,PERMISSIONS=79,INODE=49775,MODE=33188,HARD LINKS=1,UID=0,GID=0,DEVICE=196641,SIZE=1616,LAST MODIFIED=1269136234,INODE CREATED=1269136234,BLOCK SIZE=4096,BLOCKS=4
---
> /etc/passwd,PERMISSIONS=79,INODE=49775,MODE=33188,HARD LINKS=1,UID=0,GID=0,DEVICE=196641,SIZE=1614,LAST MODIFIED=1269139597,INODE CREATED=1269139597,BLOCK SIZE=4096,BLOCKS=4
Any Changes?: 1
diff fileinit CHECK.1269153366
1c1
< /etc/passwd,PERMISSIONS=79,INODE=49775,MODE=33188,HARD LINKS=1,UID=0,GID=0,DEVICE=196641,SIZE=1616,LAST MODIFIED=1269136234,INODE CREATED=1269136234,BLOCK SIZE=4096,BLOCKS=4
---
> /etc/passwd,PERMISSIONS=79,INODE=49775,MODE=33188,HARD LINKS=1,UID=0,GID=0,DEVICE=196641,SIZE=1614,LAST MODIFIED=1269139597,INODE CREATED=1269139597,BLOCK SIZE=4096,BLOCKS=4
Any ideas? The line in question is the return statement in generate_file_hash()..