| 1 |
#! /usr/bin/perl -w |
| 2 |
# integrit - file integrity verification system |
| 3 |
# Copyright (C) 2006 Ed L. Cashin |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or |
| 6 |
# modify it under the terms of the GNU General Public License |
| 7 |
# as published by the Free Software Foundation; either version 2 |
| 8 |
# of the License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, write to the Free Software |
| 17 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 |
# |
| 19 |
# $Header: /cvsroot/integrit/integrit/examples/viewreport,v 1.14 2006/04/07 09:41:13 wavexx Exp $ |
| 20 |
|
| 21 |
# extensions based on a patch from |
| 22 |
# Volker Apelt <[email protected]> |
| 23 |
# added to version 1.6 of this file. |
| 24 |
|
| 25 |
use strict; |
| 26 |
use XML::Grove; |
| 27 |
use XML::Grove::Builder; |
| 28 |
use XML::Parser::PerlSAX; |
| 29 |
use Tk; |
| 30 |
|
| 31 |
if ("debug" eq "yup") { |
| 32 |
use Data::Dumper; |
| 33 |
} |
| 34 |
|
| 35 |
&go; |
| 36 |
|
| 37 |
sub go { |
| 38 |
&show_excuses; |
| 39 |
|
| 40 |
push @ARGV, "-" if ! @ARGV; # default to stdin |
| 41 |
|
| 42 |
foreach my $fname (@ARGV) { |
| 43 |
&show_report($fname); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
sub show_excuses { |
| 48 |
print STDERR <<"MY_BOLOGNA_HAS_A_FIRST_NAME"; |
| 49 |
viewreport - an example of a GUI viewer for the XML reports that integrit |
| 50 |
generates. |
| 51 |
|
| 52 |
I'm not too much of a GUI programmer, so I'm sure you can do better! |
| 53 |
|
| 54 |
MY_BOLOGNA_HAS_A_FIRST_NAME |
| 55 |
# ' # for emacs (in case single quote in here doc are odd) |
| 56 |
} |
| 57 |
|
| 58 |
sub show_report { |
| 59 |
die "Error: parameter missing" if @_ < 1; |
| 60 |
|
| 61 |
my ($fname) = @_; |
| 62 |
|
| 63 |
#--------------use perl for XML parsing |
| 64 |
my $xml = &slirp_file($fname); |
| 65 |
my $gbuilder = new XML::Grove::Builder; |
| 66 |
my $parser = new XML::Parser::PerlSAX('Handler' => $gbuilder); |
| 67 |
my $doc = $parser->parse('Source' => { |
| 68 |
'String' => $xml, |
| 69 |
}); |
| 70 |
die "Error: unable to parse XML" unless $doc; |
| 71 |
|
| 72 |
my $report = &get_report($doc); |
| 73 |
# print new Data::Dumper([ $report ])->Dump and die; # debug |
| 74 |
|
| 75 |
#--------------use Perl/Tk for the GUI |
| 76 |
my $mw = new MainWindow(-title => "demo integrit report viewer"); |
| 77 |
my $date = localtime($report->{'Attributes'}{'date'}); |
| 78 |
$mw->Label(-text => "report from $date")->pack; |
| 79 |
$mw->Button(-text => 'Quit', -command => [ $mw => 'destroy' ])->pack; |
| 80 |
my $box = $mw->Listbox(-relief => 'sunken', |
| 81 |
-width => -1, # Shrink to fit |
| 82 |
-height => 5, |
| 83 |
-setgrid => 1); |
| 84 |
$box->bind('<Double-1>' => sub { &show_info($_[0], $report); }); |
| 85 |
&insert_elements($box, $report); |
| 86 |
my $scroll = $mw->Scrollbar(-command => ['yview', $box]); |
| 87 |
$box->configure(-yscrollcommand => ['set', $scroll]); |
| 88 |
$box->pack(-side => 'left', -fill => 'both', -expand => 1); |
| 89 |
$scroll->pack(-side => 'right', -fill => 'y'); |
| 90 |
|
| 91 |
MainLoop(); |
| 92 |
} |
| 93 |
|
| 94 |
#--------------show more info for a given report line |
| 95 |
sub show_info { |
| 96 |
die "Error: missing parameter" if @_ < 2; |
| 97 |
my ($mw, $report) = @_; |
| 98 |
|
| 99 |
my $target = $mw->get('active'); |
| 100 |
|
| 101 |
foreach my $elem (@{ $report->{'Contents'} }) { |
| 102 |
my $name = $elem->{'Name'}; |
| 103 |
my $elem_data = &elem_data($elem); |
| 104 |
next unless $name; |
| 105 |
if (($name eq "change") |
| 106 |
&& ($target =~ m/^change/)) { |
| 107 |
my $type = $elem->{'Attributes'}{'type'}; |
| 108 |
my $filename = $elem->{'Attributes'}{'file'}; |
| 109 |
my $tag = "$name ($type): $filename"; |
| 110 |
if ($tag eq $target) { |
| 111 |
&display_change($mw, $elem); |
| 112 |
} |
| 113 |
} elsif (($name eq "options") |
| 114 |
&& ($target eq "options")) { |
| 115 |
&display_options($mw, $elem); |
| 116 |
} elsif ($target eq "$name: $elem_data"){ |
| 117 |
&display_misc($mw, $elem); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
#-----------------show changed data |
| 123 |
sub display_change { |
| 124 |
die "Error: missing parameter" if @_ < 2; |
| 125 |
my ($mw, $elem) = @_; |
| 126 |
|
| 127 |
my $type = $elem->{'Attributes'}{'type'}; # e.g. stat, SHA-1 |
| 128 |
my $filename = $elem->{'Attributes'}{'file'}; |
| 129 |
my ($old, $new) = ("(unknown)", "(unknown)"); |
| 130 |
|
| 131 |
my $change = $elem->{'Contents'}[0]; |
| 132 |
# $type .= ": " . $change->{'Name'}; |
| 133 |
|
| 134 |
my $changew = $mw->Toplevel(-title => "viewreport: change"); # new window |
| 135 |
$changew->Label(-text => "file: $filename")->pack; |
| 136 |
$changew->Label(-text => "type of change: $type")->pack; |
| 137 |
|
| 138 |
foreach my $change (@{ $elem->{'Contents'} }) { |
| 139 |
if ($type eq "stat") { |
| 140 |
my $statkind = $change->{'Name'}; # e.g., mtime, atime |
| 141 |
my $postprocess = &postprocessor_for_statchange($statkind); |
| 142 |
$changew->Label(-text => "stat change: $statkind")->pack; |
| 143 |
foreach my $stat (@{ $change->{'Contents'} }) { |
| 144 |
my $name = $stat->{'Name'}; |
| 145 |
if (($name eq "old") || ($name eq "new")) { |
| 146 |
my $val = $postprocess->(&elem_data($stat)); |
| 147 |
$changew->Label(-text => "$name: $val")->pack; |
| 148 |
} |
| 149 |
} |
| 150 |
} else { |
| 151 |
my $name = $change->{'Name'}; |
| 152 |
if (($name eq "old") || ($name eq "new")) { |
| 153 |
my $val = &elem_data($change); |
| 154 |
$changew->Label(-text => "$name: $val")->pack; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
$changew->Button(-text => "Dismiss", |
| 160 |
-command => [ $changew => 'destroy' ])->pack; |
| 161 |
} |
| 162 |
sub display_options { |
| 163 |
die "Error: missing parameter" if @_ < 2; |
| 164 |
my ($mw, $elem) = @_; |
| 165 |
|
| 166 |
#-----------new window |
| 167 |
my $optionsw = $mw->Toplevel(-title => "viewreport: options"); |
| 168 |
|
| 169 |
foreach my $thang (@{ $elem->{'Contents'} }) { |
| 170 |
my $name = $thang->{'Name'}; |
| 171 |
next unless $name; |
| 172 |
my $val = &elem_data($thang); |
| 173 |
$optionsw->Label(-text => "$name: $val")->pack; |
| 174 |
} |
| 175 |
$optionsw->Button(-text => "Dismiss", |
| 176 |
-command => [ $optionsw => 'destroy' ])->pack; |
| 177 |
} |
| 178 |
sub display_misc { |
| 179 |
die "Error: missing parameter" if @_ < 2; |
| 180 |
my ($mw, $elem) = @_; |
| 181 |
|
| 182 |
my $name = $elem->{'Name'}; |
| 183 |
return unless $name; |
| 184 |
|
| 185 |
#-----------new window |
| 186 |
my $miscw = $mw->Toplevel(-title => "viewreport: misc"); |
| 187 |
|
| 188 |
my $val = &elem_data($elem); |
| 189 |
$miscw->Label(-text => "$name: $val")->pack if $val =~ /\S/; |
| 190 |
$miscw->Button(-text => "Dismiss", |
| 191 |
-command => [ $miscw => 'destroy' ])->pack; |
| 192 |
} |
| 193 |
|
| 194 |
sub get_report { |
| 195 |
die "Error: missing parameter" if @_ < 1; |
| 196 |
my ($doc) = @_; |
| 197 |
|
| 198 |
my $report; |
| 199 |
foreach my $elem (@{ $doc->{'Contents'} }) { |
| 200 |
if ($elem->{'Name'} eq "report") { |
| 201 |
$report = $elem; |
| 202 |
} |
| 203 |
} |
| 204 |
die "no report found" unless $report; |
| 205 |
return $report; |
| 206 |
} |
| 207 |
|
| 208 |
sub insert_elements { |
| 209 |
die "Error: missing parameter" if @_ < 2; |
| 210 |
my ($box, $report) = @_; |
| 211 |
|
| 212 |
foreach my $elem (@{ $report->{'Contents'} }) { |
| 213 |
my $name = $elem->{'Name'}; |
| 214 |
next unless $name; |
| 215 |
if ($name eq "change") { |
| 216 |
my $type = $elem->{'Attributes'}{'type'}; |
| 217 |
my $filename = $elem->{'Attributes'}{'file'}; |
| 218 |
$box->insert('end', "$name ($type): $filename"); |
| 219 |
} elsif ($name eq "options") { |
| 220 |
$box->insert('end', $name); |
| 221 |
} else { |
| 222 |
$box->insert('end', "$name: " . &elem_data($elem)); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
sub elem_data { |
| 228 |
die "Error: missing parameter" if @_ < 1; |
| 229 |
my ($thang) = @_; |
| 230 |
|
| 231 |
my $contents = $thang->{'Contents'} or return "(no data)"; |
| 232 |
foreach my $elem (@$contents) { |
| 233 |
next unless $elem->{'Data'}; |
| 234 |
return $elem->{'Data'}; |
| 235 |
} |
| 236 |
return "(no data)"; |
| 237 |
} |
| 238 |
|
| 239 |
sub slirp_file { |
| 240 |
die "Error: missing parameter" if @_ < 1; |
| 241 |
my ($self, $filename) = @_; # disgard self param |
| 242 |
if (! ref $self) { |
| 243 |
#--------assume it's not an OO method if there's no |
| 244 |
# reference in the first param. |
| 245 |
$filename = $self; |
| 246 |
} |
| 247 |
open SLIRPFILE, $filename |
| 248 |
or die "Error: could not open $filename for reading"; |
| 249 |
local $/ = undef; |
| 250 |
my $file_contents = <SLIRPFILE>; |
| 251 |
close SLIRPFILE; |
| 252 |
|
| 253 |
return $file_contents; |
| 254 |
} |
| 255 |
|
| 256 |
####### element post processors |
| 257 |
# turn raw element data into readable text |
| 258 |
sub postprocessor_for_statchange { |
| 259 |
die "Error: missing parameter" if @_ < 1; |
| 260 |
my ($statchange) = @_; |
| 261 |
if ($statchange =~ m/_time$/) { |
| 262 |
return \&post_processor_time2localtime; |
| 263 |
} elsif ( $statchange eq 'gid' ){ |
| 264 |
return \&post_processor_gid; |
| 265 |
} elsif ( $statchange eq 'uid' ){ |
| 266 |
return \&post_processor_uid; |
| 267 |
} |
| 268 |
return \&post_processor_noop; |
| 269 |
} |
| 270 |
sub post_processor_noop { |
| 271 |
return $_[0]; |
| 272 |
} |
| 273 |
sub post_processor_time2localtime { |
| 274 |
my $time = shift; # time in unix seconds since .... |
| 275 |
return localtime($time); |
| 276 |
} |
| 277 |
sub post_processor_gid { |
| 278 |
my $gid = shift; # numeric gid |
| 279 |
if( $gid !~ /^\d+/ ){ |
| 280 |
return "$gid"; |
| 281 |
} |
| 282 |
my $name = getgrgid($gid); # must be scalar! |
| 283 |
if( ! defined $name ){ |
| 284 |
return "(unknown)$gid"; |
| 285 |
} |
| 286 |
return $name; |
| 287 |
} |
| 288 |
sub post_processor_uid { |
| 289 |
my $uid = shift; # numeric gid |
| 290 |
if( $uid !~ /^\d+/ ){ |
| 291 |
return "$uid"; |
| 292 |
} |
| 293 |
my $name = getpwuid($uid); # must be scalar! |
| 294 |
if( ! defined $name ){ |
| 295 |
return "(unknown)$uid"; |
| 296 |
} |
| 297 |
return $name; |
| 298 |
} |
| 299 |
####### end element post processors |
| 300 |
|