This file is indexed.

/usr/lib/amanda/amdumpd is in amanda-common 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
#! /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. Mathilda Ave., Suite 300
# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com

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

use Data::Dumper;

##
# ClientService class

package main::ClientService;
use base 'Amanda::ClientService';

use Symbol;
use IPC::Open3;

use Amanda::Debug qw( debug info warning );
use Amanda::Util qw( :constants );
use Amanda::Feature;
use Amanda::Config qw( :init :getconf config_dir_relative );
use Amanda::Cmdline;
use Amanda::Paths;
use Amanda::Disklist;
use Amanda::Util qw( match_disk match_host );

# Note that this class performs its control IO synchronously.  This is adequate
# for this service, as it never receives unsolicited input from the remote
# system.

sub run {
    my $self = shift;

    $self->{'my_features'} = Amanda::Feature::Set->mine();
    $self->{'their_features'} = Amanda::Feature::Set->old();

    $self->setup_streams();
}

sub setup_streams {
    my $self = shift;

    # always started from amandad.
    my $req = $self->get_req();

    # make some sanity checks
    my $errors = [];
    if (defined $req->{'options'}{'auth'} and defined $self->amandad_auth()
	    and $req->{'options'}{'auth'} ne $self->amandad_auth()) {
	my $reqauth = $req->{'options'}{'auth'};
	my $amauth = $self->amandad_auth();
	push @$errors, "recover program requested auth '$reqauth', " .
		       "but amandad is using auth '$amauth'";
	$main::exit_status = 1;
    }

    # and pull out the features, if given
    if (defined($req->{'features'})) {
	$self->{'their_features'} = $req->{'features'};
    }

    $self->send_rep(['CTL' => 'rw'], $errors);
    return $self->quit() if (@$errors);

    $self->{'ctl_stream'} = 'CTL';

    $self->read_command();
}

sub cmd_config {
    my $self = shift;

    if (defined $self->{'config'}) {
	$self->sendctlline("ERROR duplicate CONFIG command");
	$self->{'abort'} = 1;
	return;
    }
    my $config = $1;
    config_init($CONFIG_INIT_EXPLICIT_NAME, $config);
    my ($cfgerr_level, @cfgerr_errors) = config_errors();
    if ($cfgerr_level >= $CFGERR_ERRORS) {
	$self->sendctlline("ERROR configuration errors; aborting connection");
	$self->{'abort'} = 1;
	return;
    }
    Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER_PREFERRED);

    # and the disklist
    my $diskfile = Amanda::Config::config_dir_relative(getconf($CNF_DISKFILE));
    $cfgerr_level = Amanda::Disklist::read_disklist('filename' => $diskfile);
    if ($cfgerr_level >= $CFGERR_ERRORS) {
	$self->sendctlline("ERROR Errors processing disklist");
	$self->{'abort'} = 1;
	return;
    }
    $self->{'config'} = $config;
    $self->check_host();
}

sub cmd_features {
    my $self = shift;
    my $features;

    $self->{'their_features'} = Amanda::Feature::Set->from_string($features);
    my $featreply;
    my $featurestr = $self->{'my_features'}->as_string();
    $featreply = "FEATURES $featurestr";

    $self->sendctlline($featreply);
}

sub cmd_list {
    my $self = shift;

    if (!defined $self->{'config'}) {
	$self->sendctlline("CONFIG must be set before listing the disk");
	return;
    }

    for my $disk (@{$self->{'host'}->{'disks'}}) {
	$self->sendctlline(Amanda::Util::quote_string($disk));
    }
    $self->sendctlline("ENDLIST");
}

sub cmd_disk {
    my $self = shift;
    my $qdiskname = shift;
    my $diskname = Amanda::Util::unquote_string($qdiskname);
    if (!defined $self->{'config'}) {
	$self->sendctlline("CONFIG must be set before setting the disk");
	return;
    }

    for my $disk (@{$self->{'host'}->{'disks'}}) {
	if ($disk eq $diskname) {
	    push @{$self->{'disk'}}, $diskname;
	    $self->sendctlline("DISK $diskname added");
	    last;
	}
    }
}

sub cmd_dump {
    my $self = shift;

    if (!defined $self->{'config'}) {
	$self->sendctlline("CONFIG must be set before doing a backup");
	return;
    }

    my $logdir = config_dir_relative(getconf($CNF_LOGDIR));
    if (-f "$logdir/log" || -f "$logdir/amdump" || -f "$logdir/amflush") {
	$self->sendctlline("BUSY Amanda is busy, retry later");
	return;
    }

    $self->sendctlline("DUMPING");
    my @command = ("$sbindir/amdump", "--no-taper", "--from-client", $self->{'config'}, $self->{'host'}->{'hostname'});
    if (defined $self->{'disk'}) {
	@command = (@command, @{$self->{'disk'}});
    }

    debug("command: @command");
    my $amdump_out;
    my $amdump_in;
    my $pid = open3($amdump_in, $amdump_out, $amdump_out, @command);
    close($amdump_in);
    while (<$amdump_out>) {
	chomp;
	$self->sendctlline($_);
    }
    $self->sendctlline("ENDDUMP");
}

sub cmd_check {
    my $self = shift;

    if (!defined $self->{'config'}) {
	$self->sendctlline("CONFIG must be set before doing a backup");
	return;
    }

    my $logdir = config_dir_relative(getconf($CNF_LOGDIR));
    if (-f "$logdir/log" || -f "$logdir/amdump" || -f "$logdir/amflush") {
	$self->sendctlline("BUSY Amanda is busy, retry later");
	return;
    }

    $self->sendctlline("CHECKING");
    my @command = ("$sbindir/amcheck", "-c", $self->{'config'}, $self->{'host'}->{'hostname'});
    if (defined $self->{'disk'}) {
	@command = (@command, @{$self->{'disk'}});
    }

    debug("command: @command");
    my $amcheck_out;
    my $amcheck_in;
    my $pid = open3($amcheck_in, $amcheck_out, $amcheck_out, @command);
    close($amcheck_in);
    while (<$amcheck_out>) {
	chomp;
	$self->sendctlline($_);
    }
    $self->sendctlline("ENDCHECK");
}

sub read_command {
    my $self = shift;
    my $ctl_stream = $self->{'ctl_stream'};
    my $command = $self->{'command'} = {};

    my @known_commands = qw(
	CONFIG DUMP FEATURES LIST DISK);
    while (!$self->{'abort'} and ($_ = $self->getline($ctl_stream))) {
	$_ =~ s/\r?\n$//g;

	last if /^END$/;
	last if /^[0-9]+$/;

	if (/^CONFIG (.*)$/) {
	    $self->cmd_config($1);
	} elsif (/^FEATURES (.*)$/) {
	    $self->cmd_features($1);
	} elsif (/^LIST$/) {
	    $self->cmd_list();
	} elsif (/^DISK (.*)$/) {
	    $self->cmd_disk($1);
	} elsif (/^CHECK$/) {
	    $self->cmd_check();
	} elsif (/^DUMP$/) {
	    $self->cmd_dump();
	} elsif (/^END$/) {
	    $self->{'abort'} = 1;
	} else {
	    $self->sendctlline("invalid command '$_'");
	}
    }
}

sub check_host {
    my $self = shift;

    my @hosts = Amanda::Disklist::all_hosts();
    my $peer = $ENV{'AMANDA_AUTHENTICATED_PEER'};

    if (!defined($peer)) {
	debug("no authenticated peer name is available; rejecting request.");
	$self->sendctlline("no authenticated peer name is available; rejecting request.");
	die();
    }

    # try to find the host that match the connection
    my $matched = 0;
    for my $host (@hosts) {
	if (lc($peer) eq lc($host->{'hostname'})) {
	    $matched = 1;
	    $self->{'host'} = $host;
	    last;
	}
    }

    if (!$matched) {
	debug("The peer host '$peer' doesn't match a host in the disklist.");
	$self->sendctlline("The peer host '$peer' doesn't match a host in the disklist.");
	$self->{'abort'} = 1;
    }
}

sub get_req {
    my $self = shift;

    my $req_str = '';
    while (1) {
	my $buf = Amanda::Util::full_read($self->rfd('main'), 1024);
	last unless $buf;
	$req_str .= $buf;
    }
    # we've read main to EOF, so close it
    $self->close('main', 'r');

    return $self->{'req'} = $self->parse_req($req_str);
}

sub send_rep {
    my $self = shift;
    my ($streams, $errors) = @_;
    my $rep = '';

    # first, if there were errors in the REQ, report them
    if (@$errors) {
	for my $err (@$errors) {
	    $rep .= "ERROR $err\n";
	}
    } else {
	my $connline = $self->connect_streams(@$streams);
	$rep .= "$connline\n";
    }
    # rep needs a empty-line terminator, I think
    $rep .= "\n";

    # write the whole rep packet, and close main to signal the end of the packet
    $self->senddata('main', $rep);
    $self->close('main', 'w');
}

# helper function to get a line, including the trailing '\n', from a stream.  This
# reads a character at a time to ensure that no extra characters are consumed.  This
# could certainly be more efficient! (TODO)
sub getline {
    my $self = shift;
    my ($stream) = @_;
    my $fd = $self->rfd($stream);
    my $line = undef;

    while (1) {
	my $c;
	my $a = POSIX::read($fd, $c, 1);
	last if $a != 1;
	$line .= $c;
	last if $c eq "\n";
    }

    if ($line) {
	my $chopped = $line;
	$chopped =~ s/[\r\n]*$//g;
	debug("CTL << $chopped");
    } else {
	debug("CTL << EOF");
    }

    return $line;
}

# helper function to write a data to a stream.  This does not add newline characters.
sub senddata {
    my $self = shift;
    my ($stream, $data) = @_;
    my $fd = $self->wfd($stream);

    Amanda::Util::full_write($fd, $data, length($data))
	    or die "writing to $stream: $!";
}

# send a line on the control stream, or just log it if the ctl stream is gone;
# async callback is just like for senddata
sub sendctlline {
    my $self = shift;
    my ($msg) = @_;

    if ($self->{'ctl_stream'}) {
	debug("CTL >> $msg");
	return $self->senddata($self->{'ctl_stream'}, $msg . "\n");
    } else {
	debug("not sending CTL message as CTL is closed >> $msg");
    }
}

##
# main driver

package main;
use Amanda::Debug qw( debug );
use Amanda::Util qw( :constants );
use Amanda::Config qw( :init );

our $exit_status = 0;

sub main {
    Amanda::Util::setup_application("amdumpd", "server", $CONTEXT_DAEMON);
    config_init(0, undef);
    Amanda::Debug::debug_dup_stderr_to_debug();

    my $cs = main::ClientService->new();
    $cs->run();

    debug("exiting with $exit_status");
    Amanda::Util::finish_application();
}

main();
exit($exit_status);