Handle hostnames with upper-case letters
[webmin.git] / webminlog / webminlog-lib.pl
1 =head1 webminlog-lib.pl
2
3 This module contains functions for parsing the Webmin actions log file.
4
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";
9  }
10
11 =cut
12
13 BEGIN { push(@INC, ".."); };
14 use strict;
15 use warnings;
16 use WebminCore;
17 &init_config();
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'});
21 our %parser_cache;
22 our (%text, $module_config_directory, $root_directory, $webmin_logfile);
23
24 =head2 list_webmin_log([only-user], [only-module], [start-time, end-time])
25
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 :
30
31 =item only-user - Only return actions by this Webmin user.
32
33 =item only-module - Only actions in this module.
34
35 =item start-time - Limit to actions at or after this Unix time.
36
37 =item end-time - Limit to actions at or before this Unix time.
38
39 =cut
40 sub list_webmin_log
41 {
42 my ($onlyuser, $onlymodule, $start, $end) = @_;
43 my %index;
44 &build_log_index(\%index);
45 my @rv;
46 open(LOG, $webmin_logfile);
47 my ($id, $idx);
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);
54         seek(LOG, $pos, 0);
55         my $line = <LOG>;
56         my $act = &parse_logline($line);
57         if ($act) {
58                 push(@rv, $act);
59                 }
60         }
61 close(LOG);
62 return @rv;
63 }
64
65 =head2 parse_logline(line)
66
67 Converts a line of text in the format used in /var/webmin/webmin.log into
68 a hash ref containing the following keys :
69
70 =item time - Unix time the action happened.
71
72 =item id - A unique ID for the action.
73
74 =item user - The Webmin user who did it.
75
76 =item sid - The user's session ID.
77
78 =item ip - The IP address they were logged in from.
79
80 =item module - The Webmin module name in which the action was performed.
81
82 =item script - Relative filename of the script that performed the action.
83
84 =item action - A short action name, like 'create'.
85
86 =item type - The kind of object being operated on, like 'user'.
87
88 =item object - Name of the object being operated on, like 'joe'.
89
90 =item params - A hash ref of additional information about the action.
91
92 =cut
93 sub parse_logline
94 {
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 };
102         my %param;
103         my $p = $11;
104         while($p =~ /^\s*([^=\s]+)='([^']*)'(.*)$/) {
105                 if (defined($param{$1})) {
106                         $param{$1} .= "\0".$2;
107                         }
108                 else {
109                         $param{$1} = $2;
110                         }
111                 $p = $3;
112                 }
113         foreach my $k (keys %param) {
114                 $param{$k} =~ s/%(..)/pack("c",hex($1))/ge;
115                 }
116         $rv->{'param'} = \%param;
117         if ($rv->{'script'} =~ /^(\S+):(\S+)$/) {
118                 $rv->{'script'} = $2;
119                 $rv->{'webmin'} = $1;
120                 }
121         return $rv;
122         }
123 else {
124         return undef;
125         }
126 }
127
128 =head2 list_diffs(&action)
129
130 Returns details of file changes made by this action. Each of which is a
131 hash ref with the keys :
132
133 =item type - The change type, such as create, modify, delete, exec, sql or kill.
134
135 =item object - The file or database the change was made to.
136
137 =item diff - A diff of the file change made.
138
139 =item input - Input to the command run, if available.
140
141 =cut
142 sub list_diffs
143 {
144 my ($act) = @_;
145 my $i = 0;
146 my @rv;
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);
152
153 # Read the diff files
154 foreach my $file (@files) {
155         my ($type, $object, $diff, $input);
156         open(DIFF, $file);
157         my $line = <DIFF>;
158         while(<DIFF>) { $diff .= $_; }
159         close(DIFF);
160         if ($line =~ /^(\/.*)/) {
161                 $type = 'modify'; $object = $1;
162                 }
163         elsif ($line =~ /^(\S+)\s+(.*)/) {
164                 $type = $1; $object = $2;
165                 }
166         if ($type eq "exec") {
167                 open(INPUT, $file.".input");
168                 while(<INPUT>) {
169                         $input .= $_;
170                         }
171                 close(INPUT);
172                 }
173         push(@rv, { 'type' => $type,
174                     'object' => $object,
175                     'diff' => $diff,
176                     'input' => $input } );
177         $i++;
178         }
179 return @rv;
180 }
181
182 =head2 list_files(&action)
183
184 Returns details of original files before this action was taken. Each is a hash
185 ref containing keys :
186
187 =item type - One of create, modify or delete.
188
189 =item file - Full path to the file.
190
191 =item data - Original file contents, if any.
192
193 =cut
194 sub list_files
195 {
196 my ($act) = @_;
197 my $i = 0;
198 my @rv;
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);
204
205 foreach my $file (@files) {
206         my ($type, $object, $data);
207         open(FILE, $file);
208         my $line = <FILE>;
209         $line =~ s/\r|\n//g;
210         while(<FILE>) { $data .= $_; }
211         close(FILE);
212         if ($line =~ /^(\S+)\s+(.*)/) {
213                 $type = $1;
214                 $file = $2;
215                 }
216         elsif ($line =~ /^\s+(.*)/) {
217                 $type = -1;
218                 $file = $1;
219                 }
220         else {
221                 next;
222                 }
223         push(@rv, { 'type' => $type,
224                     'file' => $file,
225                     'data' => $data });
226         $i++;
227         }
228 return @rv;
229 }
230
231 =head2 get_annotation(&action)
232
233 Returns the text of the log annotation for this action, or undef if none.
234
235 =cut
236 sub get_annotation
237 {
238 my ($act) = @_;
239 return &read_file_contents("$ENV{'WEBMIN_VAR'}/annotations/$act->{'id'}");
240 }
241
242 =head2 save_annotation(&action, text)
243
244 Updates the annotation for some action.
245
246 =cut
247 sub save_annotation
248 {
249 my ($act, $text) = @_;
250 my $dir = "$ENV{'WEBMIN_VAR'}/annotations";
251 my $file = "$dir/$act->{'id'}";
252 if ($text eq '') {
253         unlink($file);
254         }
255 else {
256         &make_dir($dir, 0700) if (!-d $dir);
257         my $fh;
258         &open_tempfile($fh, ">$file");
259         &print_tempfile($fh, $text);
260         &close_tempfile($fh);
261         }
262 }
263
264 =head2 get_action_output(&action)
265
266 Returns the text of the page that generated this action, or undef if none.
267
268 =cut
269 sub get_action_output
270 {
271 my ($act) = @_;
272 my $idprefix = substr($act->{'id'}, 0, 5);
273 return &read_file_contents("$ENV{'WEBMIN_VAR'}/output/$idprefix/$act->{'id'}")
274        ||
275        &read_file_contents("$ENV{'WEBMIN_VAR'}/output/$act->{'id'}");
276 }
277
278 =head2 expand_base_dir(base)
279
280 Finds files either under some dir, or starting with some path in the same
281 directory.
282
283 =cut
284 sub expand_base_dir
285 {
286 my ($base) = @_;
287 my @files;
288 if (-d $base) {
289         # Find files in the dir
290         opendir(DIR, $base);
291         @files = map { "$base/$_" } sort { $a <=> $b }
292                         grep { $_ =~ /^\d+$/ } readdir(DIR);
293         closedir(DIR);
294         }
295 else {
296         # Files are those that start with id
297         my $i = 0;
298         while(-r "$base.$i") {
299                 push(@files, "$base.$i");
300                 $i++;
301                 }
302         }
303 return @files;
304 }
305
306 =head2 can_user(username)
307
308 Returns 1 if the current Webmin user can view log entries for the given user.
309
310 =cut
311 sub can_user
312 {
313 return $access_users{'*'} || $access_users{$_[0]};
314 }
315
316 =head2 can_mod(module)
317
318 Returns 1 if the current Webmin user can view log entries for the given module.
319
320 =cut
321 sub can_mod
322 {
323 return $access_mods{'*'} || $access_mods{$_[0]};
324 }
325
326 =head2 get_action(id)
327
328 Returns the structure for some action identified by an ID, in the same format 
329 as returned by parse_logline.
330
331 =cut
332 sub get_action
333 {
334 my %index;
335 &build_log_index(\%index);
336 open(LOG, $webmin_logfile);
337 my @idx = split(/\s+/, $index{$_[0]});
338 seek(LOG, $idx[0], 0);
339 my $line = <LOG>;
340 my $act = &parse_logline($line);
341 close(LOG);
342 return $act->{'id'} eq $_[0] ? $act : undef;
343 }
344
345 =head2 build_log_index(&index)
346
347 Updates the given hash with mappings between action IDs and file positions.
348 For internal use only really.
349
350 =cut
351 sub build_log_index
352 {
353 my ($index) = @_;
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);
363                 }
364         else {
365                 # Smaller! Need to rebuild from start
366                 %$index = ( 'lastpos' => 0 );
367                 }
368         while(<LOG>) {
369                 my $act;
370                 if ($act = &parse_logline($_)) {
371                         $index->{$act->{'id'}} = $index->{'lastpos'}." ".
372                                                  $act->{'time'}." ".
373                                                  $act->{'user'}." ".
374                                                  $act->{'module'}." ".
375                                                  $act->{'sid'};
376                         }
377                 $index->{'lastpos'} += length($_);
378                 }
379         close(LOG);
380         $index->{'lastsize'} = $st[7];
381         $index->{'lastchange'} = $st[9];
382         }
383 }
384
385 =head2 get_action_description(&action, [long])
386
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.
391
392 =cut
393 sub get_action_description
394 {
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;
401                 }
402         else {
403                 $parser_cache{$act->{'module'}} = 0;
404                 }
405         }
406 my $d;
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);
413         }
414 elsif ($act->{'module'} eq 'global') {
415         # This module converts global actions
416         $d = $text{'search_global_'.$act->{'action'}};
417         }
418 return $d ? $d :
419        $act->{'action'} eq '_config_' ? $text{'search_config'} :
420                 join(" ", $act->{'action'}, $act->{'type'}, $act->{'object'});
421 }
422
423 1;
424