This file is indexed.

/usr/sbin/amreport is in amanda-server 1:3.3.6-4.1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#! /usr/bin/perl
# Copyright (c) 2010-2013 Zmanda, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com

use lib '/usr/lib/amanda/perl';
use strict;
use warnings;

use Getopt::Long;
use IPC::Open3;
use Cwd qw( abs_path );
use FileHandle;
use POSIX;

use Amanda::Config qw( :init :getconf config_dir_relative );
use Amanda::Util qw( :constants );
use Amanda::Tapelist;
use Amanda::Disklist;
use Amanda::Constants;
use Amanda::Debug qw( debug warning );
use Amanda::Report;
use Amanda::Logfile qw( find_latest_log);

# constants for dealing with outputs
use constant FORMAT  => 0;
use constant FMT_TYP => 0;
use constant FMT_TEMPLATE => 1;

use constant OUTPUT  => 1;
use constant OUT_TYP => 0;
use constant OUT_DST => 1;

# what mode is this running in? MODE_SCRIPT is when run from scripts like
# amdump, while MODE_CMDLINE is when run from the command line
use constant MODE_NONE    => 0;
use constant MODE_SCRIPT  => 1;
use constant MODE_CMDLINE => 2;

## Global Variables

my $opt_nomail = 0;
my ($opt_mailto, $opt_filename, $opt_logfname, $opt_psfname, $opt_xml);
my $from_amdump = 1;
my ($config_name, $report, $outfh);
my $mode = MODE_NONE;

# list of [ report-spec, output-spec ]
my (@outputs, @output_queue);

## Program subroutines

sub usage
{
    print <<EOF;
Usage: amreport [--version] [--help] [-o configoption] <conf>
  command-line mode options:
    [--log=logfile] [--ps=filename] [--text=filename] [--xml=filename]
    [--print=printer] [--mail-text=recipient]
  script-mode options:
    [-i] [-M address] [-f output-file] [-l logfile] [-p postscript-file]
    [--from-amdump]

Amreport uses short options for use from shell scripts (e.g., amreport), or
long options for use on the command line.

If the printer is omitted, the printer from the configuration is used.  If the
filename is omitted or is "-", output is to stdout.  If the recipient is
omitted, then the default mailto from the configuration is used.

If no options are given, a text report is printed to stdout.  The --from-amdump
option triggers script mode, and is used by amdump.
EOF
    exit 1;
}

sub error
{
    my ( $error_msg, $exit_code ) = @_;
    warning("error: $error_msg");
    print STDERR "$error_msg\n";
    exit $exit_code;
}

sub set_mode
{
    my ($new_mode) = @_;

    if ($mode != MODE_NONE && $mode != $new_mode) {
	error("cannot mix long options (command-line mode), and "
	    . "short options (script mode) with each other", 1);
    }

    $mode = $new_mode;
}

# Takes a string specifying an option name (e.g. "M") and a reference to a
# scalar variable. It's return values are suitable for use in the middle of
# option specification, e.g. GetOptions("foo" => \$foo, opt_set_var("bar", \$bar)
# It will only let the option be specified (at most) once, though, and will
# print an error message and exit otherwise.
sub opt_set_var
{
    my ($opt, $ref) = @_;
    error("must pass scalar ref to opt_set_var", 1)
      unless (ref($ref) eq "SCALAR");

    return (
        "$opt=s",
        sub {
            my ($op, $val) = @_;

	    # all short options are legacy options
	    set_mode(MODE_SCRIPT);

            if (defined($$ref)) {
                error("you may specify at most one -$op\n", 1);
            } else {
                $$ref = $val;
            }
        }
    );
}


sub opt_push_queue
{
    my ($output) = @_;

    unless ((ref $output eq "ARRAY")
        && (ref $output->[0] eq "ARRAY")
        && (ref $output->[1] eq "ARRAY")) {
        die "error: bad argument to opt_push_queue()";
    }

    # all queue-pushing options are command-line options
    set_mode(MODE_CMDLINE);

    push @output_queue, $output;
}

sub get_default_logfile
{
    my $logdir  = config_dir_relative(getconf($CNF_LOGDIR));
    my $logfile = "$logdir/log";

    if (-f $logfile) {
        return $logfile;

    } elsif ($mode == MODE_CMDLINE) {

        $logfile = "$logdir/" . find_latest_log($logdir);
        return $logfile if -f $logfile;
    }

    # otherwise, bail out
    error("nothing to report on!", 1);
}

sub apply_output_defaults
{
    my $cfg_printer = getconf($CNF_PRINTER);
    my $cfg_mailto = getconf_seen($CNF_MAILTO) ? getconf($CNF_MAILTO) : undef;

    foreach my $job (@output_queue) {

	# apply default destinations for each destination type
        if (!$job->[OUTPUT][OUT_DST]) {
            $job->[OUTPUT][OUT_DST] =
                ($job->[OUTPUT]->[OUT_TYP] eq 'printer') ? $cfg_printer
              : ($job->[OUTPUT]->[OUT_TYP] eq 'mail')    ? $cfg_mailto
              : ($job->[OUTPUT]->[OUT_TYP] eq 'file')    ? '-'
              :   undef;    # will result in error
        }

        push @outputs, $job;
    }
}


sub calculate_legacy_outputs {
    # Part of the "options" is the configuration.  Do we have a template?  And a
    # mailto? And mailer?

    my $ttyp = getconf($CNF_TAPETYPE);
    my $tt = lookup_tapetype($ttyp) if $ttyp;
    my $cfg_template = "" . tapetype_getconf($tt, $TAPETYPE_LBL_TEMPL) if $tt;

    my $cfg_mailer  = getconf($CNF_MAILER);
    my $cfg_printer = getconf($CNF_PRINTER);
    my $cfg_mailto  = getconf_seen($CNF_MAILTO) ? getconf($CNF_MAILTO) : undef;

    if (!defined $opt_mailto) {
	# ignore the default value for mailto
	$opt_mailto = getconf_seen($CNF_MAILTO)? getconf($CNF_MAILTO) : undef;
	# (note that we still may not send mail if CNF_MAILER is not set)
    } else {
	# check that mailer is defined if we got an explicit -M, but go on
	# processing (we will probably do nothing..)
	if (!$cfg_mailer) {
	    warning("a mailer is not defined; will not send mail");
	    print "Warning: a mailer is not defined";
	}
    }

    # should we send a mail?
    if ($cfg_mailer and $opt_mailto) {
        # -i and -f override this
	if (!$opt_nomail and !$opt_filename) {
	    push @outputs, [ [ 'human' ], [ 'mail', $opt_mailto ] ];
	}
    }

    # human/xml output to a file?
    if ($opt_filename) {
	if ($opt_xml) {
	    push @outputs, [ [ 'xml' ], [ 'file', $opt_filename ] ];
	} else {
	    push @outputs, [ [ 'human' ], [ 'file', $opt_filename ] ];
	}
    }

    # postscript output to a printer?
    # (this is just silly)
    if ($Amanda::Constants::LPR and $cfg_template) {
	# oddly, -i ($opt_nomail) will disable printing, but -i -f prints.
	if ((!$opt_nomail and !$opt_psfname) or ($opt_nomail and $opt_filename)) {
	    # but we don't print if the text report isn't going anywhere
	    unless ((!$cfg_mailer or !$opt_mailto) and !($opt_filename and !$opt_xml)) {
		push @outputs, [ [ 'postscript', $cfg_template ], [ 'printer', $cfg_printer ] ]
	    }
	}
    }

    # postscript output to a file?
    if ($opt_psfname and $cfg_template) {
	push @outputs, [ [ 'postscript', $cfg_template ], [ 'file', $opt_psfname ] ];
    }
}

sub legacy_send_amreport
{
    my ($output) = @_;
    my $cfg_send = getconf($CNF_SEND_AMREPORT_ON);

    ## only check $cfg_send if we are in script mode and sending mail
    return 1 if ($mode != MODE_SCRIPT);
    return 1 if !($output->[OUTPUT]->[OUT_TYP] eq "mail");

    ## do not bother checking for errors or stranges if set to 'all' or 'never'
    return 1 if ($cfg_send == $SEND_AMREPORT_ALL);
    return 0 if ($cfg_send == $SEND_AMREPORT_NEVER);

    my $output_name = join(" ", @{ $output->[FORMAT] }, @{ $output->[OUTPUT] });
    my $send_amreport = 0;

    debug("testingamreport_send_on=$cfg_send, output:$output_name");

    if ($cfg_send == $SEND_AMREPORT_STRANGE) {

        if (   !$report->get_flag("got_finish")
            || ($report->get_flag("exit_status") != 0)
	    || ($report->get_flag("dump_failed") != 0)
	    || ($report->get_flag("results_missing") != 0)
	    || ($report->get_flag("dump_strange") != 0)) {

            debug("send-amreport-on=$cfg_send, condition filled for $output_name");
            $send_amreport = 1;

        } else {

            debug("send-amreport-on=$cfg_send, condition not filled for $output_name");
            $send_amreport = 0;
        }

    } elsif ($cfg_send = $SEND_AMREPORT_ERROR) {

        if (   !$report->get_flag("got_finish")
            || ($report->get_flag("exit_status") != 0 && $report->get_flag("exit_status") != 2)
            || ($report->get_flag("dump_failed") != 0)
            || ($report->get_flag("results_missing") != 0)) {

            debug("send-amreport-on=$cfg_send, condition filled for $output_name");
            $send_amreport = 1;

        } else {

            debug("send-amreport-on=$cfg_send, condition not filled for $output_name");
            $send_amreport = 0;
        }
    }

    return $send_amreport;
}

sub open_file_output {
    my ($report, $outputspec) = @_;

    my $filename = $outputspec->[1];
    $filename = Amanda::Util::get_original_cwd() . "/$filename"
      unless ($filename eq "-" || $filename =~ m{^/});

    if ($filename eq "-") {
	return \*STDOUT;
    } else {
	open my $fh, ">", $filename or die "Cannot open '$filename': $!";
	return $fh;
    }
}

sub open_printer_output
{
    my ($report, $outputspec) = @_;
    my $printer = $outputspec->[1];

    my @cmd;
    if ($printer and $Amanda::Constants::LPRFLAG) {
	@cmd = ( $Amanda::Constants::LPR, $Amanda::Constants::LPRFLAG, $printer );
    } else {
	@cmd = ( $Amanda::Constants::LPR );
    }

    debug("invoking printer: " . join(" ", @cmd));

    # redirect stdout/stderr to stderr, which is usually the amdump log
    my ($pid, $fh);
    if (!-f $Amanda::Constants::LPR || !-x $Amanda::Constants::LPR) {
	my $errstr = "error: the mailer '$Amanda::Constants::LPR' is not an executable program.";
	print STDERR "$errstr\n";
        if ($mode == MODE_SCRIPT) {
            debug($errstr);
        } else {
            error($errstr, 1);
        }
    } else {
	eval { $pid = open3($fh, ">&2", ">&2", @cmd); } or do {
            ($pid, $fh) = (0, undef);
            chomp $@;
            my $errstr = "error: $@: $!";

	    print STDERR "$errstr\n";
            if ($mode == MODE_SCRIPT) {
		debug($errstr);
            } else {
		error($errstr, 1);
            }
        };
    }
    return ($pid, $fh);
}

sub open_mail_output
{
    my ($report, $outputspec) = @_;
    my $mailto = $outputspec->[1];

    if ($mailto =~ /[*<>()\[\];:\\\/"!$|]/) {
        error("mail addresses have invalid characters", 1);
    }

    my $datestamp =
      $report->get_program_info(
        $report->get_flag("amflush_run") ? "amflush" : 
	$report->get_flag("amvault_run") ? "amvault" : "planner", "start" );

    $datestamp /= 1000000 if $datestamp > 99999999;
    $datestamp = int($datestamp);
    my $year  = int( $datestamp / 10000 ) - 1900;
    my $month = int( ( $datestamp / 100 ) % 100 ) - 1;
    my $day   = int( $datestamp % 100 );
    my $date  = POSIX::strftime( '%B %e, %Y', 0, 0, 0, $day, $month, $year );
    $date =~ s/  / /g;

    my $done = "";
    if (  !$report->get_flag("got_finish")
	|| $report->get_flag("dump_failed") != 0) {
	$done = " FAIL:";
    } elsif ($report->get_flag("results_missing") != 0) {
	$done = " MISSING:";
    } elsif ($report->get_flag("dump_strange") != 0) {
	$done = " STRANGE:";
    }

    my $subj_str =
        getconf($CNF_ORG) . $done
      . ( $report->get_flag("amflush_run") ? " AMFLUSH" :
	  $report->get_flag("amvault_run") ? " AMVAULT" : " AMANDA" )
      . " MAIL REPORT FOR "
      . $date;

    my $cfg_mailer = getconf($CNF_MAILER);

    my @cmd = ("$cfg_mailer", "-s", $subj_str, split(/ +/, $mailto));
    debug("invoking mail app: " . join(" ", @cmd));


    my ($pid, $fh);
    if (!-f $cfg_mailer || !-x $cfg_mailer) {
	my $errstr = "error: the mailer '$cfg_mailer' is not an executable program.";
	print STDERR "$errstr\n";
        if ($mode == MODE_SCRIPT) {
            debug($errstr);
        } else {
            error($errstr, 1);
        }
	
    } else {
	eval { $pid = open3($fh, ">&2", ">&2", @cmd) } or do {
            ($pid, $fh) = (0, undef);
            chomp $@;
            my $errstr = "error: $@: $!";

	    print STDERR "$errstr\n";
            if ($mode == MODE_SCRIPT) {
		debug($errstr);
            } else {
		error($errstr, 1);
            }
	};
    }

    return ($pid, $fh);
}

sub run_output {
    my ($output) = @_;
    my ($reportspec, $outputspec) = @$output;

    # get the output
    my ($pid, $fh);
    if ($outputspec->[0] eq 'file') {
	$fh = open_file_output($report, $outputspec);
    } elsif ($outputspec->[0] eq 'printer') {
	($pid, $fh) = open_printer_output($report, $outputspec);
    } elsif ($outputspec->[0] eq 'mail') {
	($pid, $fh) = open_mail_output($report, $outputspec);
    }

    # TODO: add some generic error handling here.  must be compatible
    # with legacy behavior.

    if (defined $fh) {
	my $fail_to_load = 0;
	my $report_name = $reportspec->[0];
	my $pkgname = "Amanda::Report::$report_name";
	my $filename = $pkgname;
	$filename =~ s|::|/|g;
	$filename .= '.pm';

	if (!exists $INC{$filename}) {
	    eval "use $pkgname;";
	    if ($@) {
		my $err = $@;
		# determine whether the module doesn't exist at all, or if
		# there was an error loading it;
		if (exists $INC{$filename} or
		    $err =~ /did not return a true value/) {
		    print STDERR "$err";
		    $fail_to_load = 1;
		}
	    }
	}
	if (!$fail_to_load) {
	    my $rep = eval {$pkgname->new($report, $config_name,
						   $opt_logfname);};
	    if ($@) {
		print STDERR "$@\n";
	    } else {
		$rep->write_report($fh);
	    }
	}

	close $fh;
    }

    # clean up any subprocess
    if (defined $pid) {
	debug("waiting for child process to finish..");
	waitpid($pid, 0);
	if ($? != 0) {
	    warning("child exited with status $?");
	}
    }
}


## Application initialization

Amanda::Util::setup_application("amreport", "server", $CONTEXT_CMDLINE);

my $config_overrides = new_config_overrides( scalar(@ARGV) + 1 );

debug("Arguments: " . join(' ', @ARGV));
Getopt::Long::Configure(qw/bundling/);
GetOptions(

    ## old legacy configuration opts
    "i" => sub { set_mode(MODE_SCRIPT); $opt_nomail = 1; },
    opt_set_var("M", \$opt_mailto),
    opt_set_var("f", \$opt_filename),
    opt_set_var("l", \$opt_logfname),
    opt_set_var("p", \$opt_psfname),

    "o=s" => sub { add_config_override_opt($config_overrides, $_[1]); },

    ## trigger default amdump behavior
    "from-amdump" => sub { set_mode(MODE_SCRIPT); $from_amdump = 1 },

    ## new configuration opts
    "log=s" => sub { set_mode(MODE_CMDLINE); $opt_logfname = $_[1]; },
    "ps:s" => sub { opt_push_queue([ ['postscript'], [ 'file', $_[1] ] ]); },
    "mail-text:s" => sub { opt_push_queue([ ['human'], [ 'mail', $_[1] ] ]); },
    "text:s"      => sub { opt_push_queue([ ['human'], [ 'file', $_[1] ] ]); },
    "xml:s"       => sub { opt_push_queue([ ['xml'],   [ 'file', $_[1] ] ]); },
    "print:s"     => sub { opt_push_queue([ [ 'postscript' ], [ 'printer', $_[1] ] ]); },
    "format:s"    => sub {
			   my ($module, $destination) = split ':', $_[1];
			   my ($mod, $mod_arg) = split ',', $module;
			   my ($dest, $dest_arg) = split ',', $destination;
			   $dest = 'file' if !defined $dest;
			   opt_push_queue([ [$mod, $mod_arg], [$dest, $dest_arg ] ]);
			 },

    'version' => \&Amanda::Util::version_opt,
    'help'    => \&usage,
) or usage();

# set command line mode if no options were given
$mode = MODE_CMDLINE if ($mode == MODE_NONE);

if ($mode == MODE_CMDLINE) {
    (scalar @ARGV == 1) or usage();
} else {    # MODE_SCRIPT
    (scalar @ARGV > 0) or usage();
}

$config_name = shift @ARGV;    # only use first argument
$config_name ||= '.';          # default config is current dir

set_config_overrides($config_overrides);
config_init( $CONFIG_INIT_EXPLICIT_NAME, $config_name );

my ( $cfgerr_level, @cfgerr_errors ) = config_errors();
if ( $cfgerr_level >= $CFGERR_WARNINGS ) {
    config_print_errors();
    if ( $cfgerr_level >= $CFGERR_ERRORS ) {
        error( "errors processing config file", 1 );
    }
}

Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);

# read the tapelist
my $tl_file = config_dir_relative(getconf($CNF_TAPELIST));
my $tl = Amanda::Tapelist->new($tl_file);

# read the disklist
my $diskfile = config_dir_relative(getconf($CNF_DISKFILE));
$cfgerr_level += Amanda::Disklist::read_disklist('filename' => $diskfile);
($cfgerr_level < $CFGERR_ERRORS) || die "Errors processing disklist";

# shim for installchecks
$Amanda::Constants::LPR = $ENV{'INSTALLCHECK_MOCK_LPR'}
    if exists $ENV{'INSTALLCHECK_MOCK_LPR'};

# calculate the logfile to read from
$opt_logfname = Amanda::Util::get_original_cwd() . "/" . $opt_logfname
	if defined $opt_logfname and $opt_logfname !~ /^\//;
my $logfile = $opt_logfname || get_default_logfile();
my $historical = defined $opt_logfname;
debug("using logfile: $logfile" . ($historical? " (historical)" : ""));

if ($mode == MODE_CMDLINE) {
    debug("operating in cmdline mode");
    apply_output_defaults();
    push @outputs, [ ['human'], [ 'file', '-' ] ] if !@outputs;
} else {
    debug("operating in script mode");
    my $cnf_report_format = getconf($CNF_REPORT_FORMAT);
    if ($from_amdump && @{$cnf_report_format}) {
	for my $report_format (@{$cnf_report_format}) {
	    my ($module, $destination) = split ':', $report_format;
	    my ($mod, $mod_arg) = split ',', $module;
	    my ($dest, $dest_arg) = split ',', $destination;
	    $dest = 'file' if !defined $dest;
	    push @output_queue,[[$mod, $mod_arg], [$dest, $dest_arg ] ];
	}
	apply_output_defaults();
    } else {
	calculate_legacy_outputs();
    }
}

## Parse the report & set output

$report = Amanda::Report->new($logfile, $historical);
my $exit_status = $report->get_flag("exit_status");

## filter outputs by errors & stranges

@outputs = grep { legacy_send_amreport($_) } @outputs;

for my $output (@outputs) {
    debug("planned output: " . join(" ", @{ $output->[FORMAT] }, @{ $output->[OUTPUT] }));
}

## Output

for my $output (@outputs) {
    run_output($output);
}

Amanda::Util::finish_application();
exit $exit_status;