1 =head1 webminlog-lib.pl
3 This module contains functions for parsing the Webmin actions log file.
5 foreign_require("webminlog", "webminlog-lib.pl");
6 @actions = webminlog::list_webmin_log(undef, "useradmin", undef, undef);
7 foreach $a (@actions) {
8 print webminlog::get_action_description($a),"\n";
13 BEGIN { push(@INC, ".."); };
18 our %access = &get_module_acl();
19 our %access_mods = map { $_, 1 } split(/\s+/, $access{'mods'});
20 our %access_users = map { $_, 1 } split(/\s+/, $access{'users'});
22 our (%text, $module_config_directory, $root_directory, $webmin_logfile);
24 =head2 list_webmin_log([only-user], [only-module], [start-time, end-time])
26 Returns an array of matching Webmin log events, each of which is a hash ref
27 in the format returned by parse_logline (see below). By default all actions
28 will be returned, but you can limit it to a subset using by setting the
29 following parameters :
31 =item only-user - Only return actions by this Webmin user.
33 =item only-module - Only actions in this module.
35 =item start-time - Limit to actions at or after this Unix time.
37 =item end-time - Limit to actions at or before this Unix time.
42 my ($onlyuser, $onlymodule, $start, $end) = @_;
44 &build_log_index(\%index);
46 open(LOG, $webmin_logfile);
48 while(($id, $idx) = each %index) {
49 my ($pos, $time, $user, $module, $sid) = split(/\s+/, $idx);
50 next if (defined($onlyuser) && $user ne $onlyuser);
51 next if (defined($onlymodule) && $module ne $onlymodule);
52 next if (defined($start) && $time < $start);
53 next if (defined($end) && $time > $end);
56 my $act = &parse_logline($line);
65 =head2 parse_logline(line)
67 Converts a line of text in the format used in /var/webmin/webmin.log into
68 a hash ref containing the following keys :
70 =item time - Unix time the action happened.
72 =item id - A unique ID for the action.
74 =item user - The Webmin user who did it.
76 =item sid - The user's session ID.
78 =item ip - The IP address they were logged in from.
80 =item module - The Webmin module name in which the action was performed.
82 =item script - Relative filename of the script that performed the action.
84 =item action - A short action name, like 'create'.
86 =item type - The kind of object being operated on, like 'user'.
88 =item object - Name of the object being operated on, like 'joe'.
90 =item params - A hash ref of additional information about the action.
95 if ($_[0] =~ /^(\d+)\.(\S+)\s+\[.*\]\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+"([^"]+)"\s+"([^"]+)"\s+"([^"]+)"(.*)/ ||
96 $_[0] =~ /^(\d+)\.(\S+)\s+\[.*\]\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)(.*)/) {
97 my $rv = { 'time' => $1, 'id' => "$1.$2",
98 'user' => $3, 'sid' => $4,
99 'ip' => $5, 'module' => $6,
100 'script' => $7, 'action' => $8,
101 'type' => $9, 'object' => $10 };
104 while($p =~ /^\s*([^=\s]+)='([^']*)'(.*)$/) {
105 if (defined($param{$1})) {
106 $param{$1} .= "\0".$2;
113 foreach my $k (keys %param) {
114 $param{$k} =~ s/%(..)/pack("c",hex($1))/ge;
116 $rv->{'param'} = \%param;
117 if ($rv->{'script'} =~ /^(\S+):(\S+)$/) {
118 $rv->{'script'} = $2;
119 $rv->{'webmin'} = $1;
128 =head2 list_diffs(&action)
130 Returns details of file changes made by this action. Each of which is a
131 hash ref with the keys :
133 =item type - The change type, such as create, modify, delete, exec, sql or kill.
135 =item object - The file or database the change was made to.
137 =item diff - A diff of the file change made.
139 =item input - Input to the command run, if available.
147 my $idprefix = substr($act->{'id'}, 0, 5);
148 my $oldbase = "$ENV{'WEBMIN_VAR'}/diffs/$idprefix/$act->{'id'}";
149 my $base = "$ENV{'WEBMIN_VAR'}/diffs/$act->{'id'}";
150 return ( ) if (!-d $base && !-d $oldbase);
151 my @files = &expand_base_dir(-d $base ? $base : $oldbase);
153 # Read the diff files
154 foreach my $file (@files) {
155 my ($type, $object, $diff, $input);
158 while(<DIFF>) { $diff .= $_; }
160 if ($line =~ /^(\/.*)/) {
161 $type = 'modify'; $object = $1;
163 elsif ($line =~ /^(\S+)\s+(.*)/) {
164 $type = $1; $object = $2;
166 if ($type eq "exec") {
167 open(INPUT, $file.".input");
173 push(@rv, { 'type' => $type,
176 'input' => $input } );
182 =head2 list_files(&action)
184 Returns details of original files before this action was taken. Each is a hash
185 ref containing keys :
187 =item type - One of create, modify or delete.
189 =item file - Full path to the file.
191 =item data - Original file contents, if any.
199 my $idprefix = substr($act->{'id'}, 0, 5);
200 my $oldbase = "$ENV{'WEBMIN_VAR'}/files/$idprefix/$act->{'id'}";
201 my $base = "$ENV{'WEBMIN_VAR'}/files/$act->{'id'}";
202 return ( ) if (!-d $base && !-d $oldbase);
203 my @files = &expand_base_dir(-d $base ? $base : $oldbase);
205 foreach my $file (@files) {
206 my ($type, $object, $data);
210 while(<FILE>) { $data .= $_; }
212 if ($line =~ /^(\S+)\s+(.*)/) {
216 elsif ($line =~ /^\s+(.*)/) {
223 push(@rv, { 'type' => $type,
231 =head2 get_annotation(&action)
233 Returns the text of the log annotation for this action, or undef if none.
239 return &read_file_contents("$ENV{'WEBMIN_VAR'}/annotations/$act->{'id'}");
242 =head2 save_annotation(&action, text)
244 Updates the annotation for some action.
249 my ($act, $text) = @_;
250 my $dir = "$ENV{'WEBMIN_VAR'}/annotations";
251 my $file = "$dir/$act->{'id'}";
256 &make_dir($dir, 0700) if (!-d $dir);
258 &open_tempfile($fh, ">$file");
259 &print_tempfile($fh, $text);
260 &close_tempfile($fh);
264 =head2 get_action_output(&action)
266 Returns the text of the page that generated this action, or undef if none.
269 sub get_action_output
272 my $idprefix = substr($act->{'id'}, 0, 5);
273 return &read_file_contents("$ENV{'WEBMIN_VAR'}/output/$idprefix/$act->{'id'}")
275 &read_file_contents("$ENV{'WEBMIN_VAR'}/output/$act->{'id'}");
278 =head2 expand_base_dir(base)
280 Finds files either under some dir, or starting with some path in the same
289 # Find files in the dir
291 @files = map { "$base/$_" } sort { $a <=> $b }
292 grep { $_ =~ /^\d+$/ } readdir(DIR);
296 # Files are those that start with id
298 while(-r "$base.$i") {
299 push(@files, "$base.$i");
306 =head2 can_user(username)
308 Returns 1 if the current Webmin user can view log entries for the given user.
313 return $access_users{'*'} || $access_users{$_[0]};
316 =head2 can_mod(module)
318 Returns 1 if the current Webmin user can view log entries for the given module.
323 return $access_mods{'*'} || $access_mods{$_[0]};
326 =head2 get_action(id)
328 Returns the structure for some action identified by an ID, in the same format
329 as returned by parse_logline.
335 &build_log_index(\%index);
336 open(LOG, $webmin_logfile);
337 my @idx = split(/\s+/, $index{$_[0]});
338 seek(LOG, $idx[0], 0);
340 my $act = &parse_logline($line);
342 return $act->{'id'} eq $_[0] ? $act : undef;
345 =head2 build_log_index(&index)
347 Updates the given hash with mappings between action IDs and file positions.
348 For internal use only really.
354 my $ifile = "$module_config_directory/logindex";
355 dbmopen(%$index, $ifile, 0600);
356 my @st = stat($webmin_logfile);
357 if ($st[9] > $index->{'lastchange'}) {
358 # Log has changed .. perhaps need to rebuild
359 open(LOG, $webmin_logfile);
360 if ($index->{'lastsize'} && $st[7] >= $index->{'lastsize'}) {
361 # Gotten bigger .. just add new lines
362 seek(LOG, $index->{'lastpos'}, 0);
365 # Smaller! Need to rebuild from start
366 %$index = ( 'lastpos' => 0 );
370 if ($act = &parse_logline($_)) {
371 $index->{$act->{'id'}} = $index->{'lastpos'}." ".
374 $act->{'module'}." ".
377 $index->{'lastpos'} += length($_);
380 $index->{'lastsize'} = $st[7];
381 $index->{'lastchange'} = $st[9];
385 =head2 get_action_description(&action, [long])
387 Returns a human-readable description of some action. This is done by
388 calling the log_parser.pl file in the action's source module. If the long
389 parameter is set to 1 and the module provides a more detailed description
390 for the action, it will be returned.
393 sub get_action_description
395 my ($act, $long) = @_;
396 if (!defined($parser_cache{$act->{'module'}})) {
397 # Bring in module parser library for the first time
398 if (-r "$root_directory/$act->{'module'}/log_parser.pl") {
399 &foreign_require($act->{'module'}, "log_parser.pl");
400 $parser_cache{$act->{'module'}} = 1;
403 $parser_cache{$act->{'module'}} = 0;
407 if ($parser_cache{$act->{'module'}}) {
408 # Module can return string
409 $d = &foreign_call($act->{'module'}, "parse_webmin_log",
410 $act->{'user'}, $act->{'script'},
411 $act->{'action'}, $act->{'type'},
412 $act->{'object'}, $act->{'param'}, $long);
414 elsif ($act->{'module'} eq 'global') {
415 # This module converts global actions
416 $d = $text{'search_global_'.$act->{'action'}};
419 $act->{'action'} eq '_config_' ? $text{'search_config'} :
420 join(" ", $act->{'action'}, $act->{'type'}, $act->{'object'});