Debian Codegraph @

application-src/amsamba.pl

raw
1 #!@PERL@
2 # Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
3 # Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
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, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #
19 # Contact information: Carbonite Inc., 756 N Pastoria Ave
20 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
21
22 use lib '@amperldir@';
23 use strict;
24 use warnings;
25 use Getopt::Long;
26
27 package Amanda::Application::Amsamba;
28 use base qw(Amanda::Application);
29 use File::Copy;
30 use File::Path;
31 use IPC::Open2;
32 use IPC::Open3;
33 use Sys::Hostname;
34 use Symbol;
35 use IO::Handle;
36 use MIME::Base64 ();
37 use Amanda::Constants;
38 use Amanda::Config qw( :init :getconf config_dir_relative );
39 use Amanda::Debug qw( :logging );
40 use Amanda::Paths;
41 use Amanda::Util qw( :constants :quoting);
42 use Amanda::MainLoop qw( :GIOCondition );
43
44 sub new {
45 my $class = shift;
46 my ($config, $host, $disk, $device, $level, $index, $message, $collection, $record, $calcsize, $gnutar_path, $smbclient_path, $amandapass, $exclude_file, $exclude_list, $exclude_optional, $include_file, $include_list, $include_optional, $recover_mode, $allow_anonymous, $target, $regex_match) = @_;
47 my $self = $class->SUPER::new($config);
48
49 if (defined $gnutar_path) {
50 $self->{gnutar} = $gnutar_path;
51 } else {
52 $self->{gnutar} = $Amanda::Constants::GNUTAR;
53 }
54 if (defined $smbclient_path) {
55 $self->{smbclient} = $smbclient_path;
56 } else {
57 $self->{smbclient} = $Amanda::Constants::SAMBA_CLIENT;
58 }
59 if (defined $amandapass) {
60 $self->{amandapass} = config_dir_relative($amandapass);
61 } else {
62 $self->{amandapass} = "$Amanda::Paths::CONFIG_DIR/amandapass";
63 }
64
65 $self->{config} = $config;
66 $self->{host} = $host;
67 if (defined $disk) {
68 $self->{disk} = $disk;
69 } else {
70 $self->{disk} = $device;
71 }
72 if (defined $device) {
73 $self->{device} = $device;
74 } else {
75 $self->{device} = $disk;
76 }
77 $self->{level} = [ @{$level} ];
78 $self->{index} = $index;
79 $self->{message} = $message;
80 $self->{collection} = $collection;
81 $self->{record} = $record;
82 $self->{calcsize} = $calcsize;
83 $self->{exclude_file} = [ @{$exclude_file} ];
84 $self->{exclude_list} = [ @{$exclude_list} ];
85 $self->{exclude_optional} = $exclude_optional;
86 $self->{include_file} = [ @{$include_file} ];
87 $self->{include_list} = [ @{$include_list} ];
88 $self->{include_optional} = $include_optional;
89 $self->{recover_mode} = $recover_mode;
90 $self->{allow_anonymous} = $allow_anonymous;
91 $self->{target} = $target;
92 if ($regex_match =~ /^yes$/i) {
93 $self->{'regex_match'} = 1;
94 } else {
95 $self->{'regex_match'} = 0;
96 }
97
98 return $self;
99 }
100
101 # on entry:
102 # $self->{exclude_file}
103 # $self->{exclude_list}
104 # $self->{include_file}
105 # $self->{include_list}
106 #on exit:
107 # $self->{exclude}
108 # $self->{include}
109 # $self->{include_filename}
110 sub validate_inexclude {
111 my $self = shift;
112
113 if ($#{$self->{exclude_file}} + $#{$self->{exclude_list}} >= -1 &&
114 $#{$self->{include_file}} + $#{$self->{include_list}} >= -1) {
115 $self->print_to_server_and_die("Can't have both include and exclude",
116 $Amanda::Script_App::ERROR);
117 }
118
119 if ($#{$self->{exclude_file}} >= 0) {
120 $self->{exclude} = [ @{$self->{exclude_file}} ];
121 }
122 foreach my $file (@{$self->{exclude_list}}) {
123 if (!open(FF, $file)) {
124 if ($self->{action} eq 'check' && !$self->{exclude_optional}) {
125 $self->print_to_server("Open of '$file' failed: $!",
126 $Amanda::Script_App::ERROR);
127 }
128 next;
129 }
130 while (<FF>) {
131 chomp;
132 push @{$self->{exclude}}, $_;
133 }
134 close(FF);
135 }
136
137 if ($self->{action} eq "restore" and defined $self->{'include_list'}) {
138 # put all include in a single file $self->{'include_filename'}
139 $self->{'include_filename'} = "$AMANDA_TMPDIR/amsamba.$$.include";
140 open INC_FILE, ">$self->{'include_filename'}";
141 if ($#{$self->{include_file}} >= 0) {
142 print INC_FILE "$self->{include_file}\n";
143 }
144 foreach my $file (@{$self->{include_list}}) {
145 if (!open(FF, $file)) {
146 if ($self->{action} eq 'check' && !$self->{include_optional}) {
147 $self->print_to_server("Open of '$file' failed: $!",
148 $Amanda::Script_App::ERROR);
149 }
150 next;
151 }
152 while (<FF>) {
153 if (defined $self->{'subdir'}) {
154 $_ =~ s/^\./$self->{'subdir'}/;
155 }
156 print INC_FILE;
157 }
158 close(FF);
159 }
160
161 # add command line include for amrestore
162 for(my $i=1;defined $ARGV[$i]; $i++) {
163 my $param = $ARGV[$i];
164 $param =~ /^(.*)$/;
165 $_ = $1;
166 if (defined $self->{'subdir'}) {
167 $_ =~ s/^\./$self->{'subdir'}/;
168 }
169 $_ =~ s/\\([0-7]{3})/chr oct $1/eg;
170 print INC_FILE "$_\n";
171 }
172
173 close INC_FILE;
174 } else {
175 # put all include in $self->{'include'} they will be added on
176 # command line.
177 if ($#{$self->{include_file}} >= 0) {
178 $self->{include} = [ @{$self->{include_file}} ];
179 }
180
181 foreach my $file (@{$self->{include_list}}) {
182 if (!open(FF, $file)) {
183 if ($self->{action} eq 'check' && !$self->{include_optional}) {
184 $self->print_to_server("Open of '$file' failed: $!",
185 $Amanda::Script_App::ERROR);
186 }
187 next;
188 }
189 while (<FF>) {
190 chomp;
191 if ($self->{action} eq "restore" and
192 defined $self->{'subdir'}) {
193 $_ =~ s/^\./$self->{'subdir'}/;
194 }
195 push @{$self->{include}}, $_;
196 }
197 close(FF);
198 }
199
200 # add command line include for amrestore
201 if ($self->{action} eq "restore") {
202 for(my $i=1;defined $ARGV[$i]; $i++) {
203 my $param = $ARGV[$i];
204 $param =~ /^(.*)$/;
205 $_ = $1;
206 if (defined $self->{'subdir'}) {
207 $_ =~ s/^\./$self->{'subdir'}/;
208 }
209 push @{$self->{include}}, $1;
210 }
211 }
212 }
213 }
214
215 # on entry:
216 # $self->{target} == //host/share/subdir \\host\share\subdir
217 # or
218 # $self->{device} == //host/share/subdir \\host\share\subdir
219 # on exit:
220 # $self->{cifshost} = //host \\host
221 # $self->{share} = //host/share \\host\share
222 # $self->{sambashare} = \\host\share \\host\share
223 # $self->{subdir} = subdir subdir
224 sub parsesharename {
225 my $self = shift;
226 my $to_parse = $self->{target};
227 $to_parse = $self->{device} if !defined $to_parse;;
228
229 return if !defined $to_parse;
230 if ($to_parse =~ /^\\\\/) {
231 $self->{unc} = 1;
232 } else {
233 $self->{unc} = 0;
234 }
235
236 if ($self->{unc}) {
237 if ($to_parse =~ m,^(\\\\[^\\]+\\[^\\]+)\\(.*)$,) {
238 $self->{share} = $1;
239 $self->{subdir} = $2
240 } else {
241 $self->{share} = $to_parse
242 }
243 $self->{sambashare} = $self->{share};
244 $to_parse =~ m,^(\\\\[^\\]+)\\[^\\]+,;
245 $self->{cifshost} = $1;
246 } else {
247 if ($to_parse =~ m,^(//[^/]+/[^/]+)/(.*)$,) {
248 $self->{share} = $1;
249 $self->{subdir} = $2
250 } else {
251 $self->{share} = $to_parse
252 }
253 $self->{sambashare} = $self->{share};
254 $self->{sambashare} =~ s,/,\\,g;
255 $to_parse =~ m,^(//[^/]+)/[^/]+,;
256 $self->{cifshost} = $1;
257 }
258 }
259
260
261 # Read $self->{amandapass} file.
262 # on entry:
263 # $self->{cifshost} == //host/share
264 # $self->{share} == //host/share
265 # on exit:
266 # $self->{domain} = domain to connect to.
267 # $self->{username} = username (-U)
268 # $self->{password} = password
269 sub findpass {
270 my $self = shift;
271
272 my $amandapass;
273 my $line;
274
275 $self->{domain} = undef;
276 $self->{username} = undef;
277 $self->{password} = undef;
278
279 debug("amandapass: $self->{amandapass}");
280 if (!open($amandapass, $self->{amandapass})) {
281 if ($self->{allow_anonymous}) {
282 $self->{username} = $self->{allow_anonymous};
283 debug("cannot open password file '$self->{amandapass}': $!\n");
284 debug("Using anonymous user: $self->{username}");
285 return;
286 } else {
287 $self->print_to_server_and_die(
288 "cannot open password file '$self->{amandapass}': $!",
289 $Amanda::Script_App::ERROR);
290 }
291 }
292
293 while ($line = <$amandapass>) {
294 chomp $line;
295 next if $line =~ /^#/;
296 my ($diskname, $userpasswd, $domain, $extra) = Amanda::Util::split_quoted_string_friendly($line);
297 if ($extra) {
298 debug("Trailling characters ignored in amandapass line");
299 }
300 if (defined $diskname &&
301 ($diskname eq '*' ||
302 ($self->{unc}==0 && $diskname =~ m,^(//[^/]+)/\*$, && $1 eq $self->{cifshost}) ||
303 ($self->{unc}==1 && $diskname =~ m,^(\\\\[^\\]+)\\\*$, && $1 eq $self->{cifshost}) ||
304 $diskname eq $self->{share} ||
305 $diskname eq $self->{sambashare})) {
306 if (defined $userpasswd && $userpasswd ne "") {
307 $self->{domain} = $domain if defined $domain && $domain ne "";
308 my ($username, $password) = split('%', $userpasswd, 2);
309 $self->{username} = $username;
310 if ($password =~ /^6G\!dr(.*)/) {
311 my $base64 = $1;
312 $password = MIME::Base64::decode($base64);
313 chomp($password);
314 }
315 $self->{password} = $password;
316 $self->{password} = undef if (defined $password && $password eq "");
317 } else {
318 $self->{username} = "guest";
319 }
320 close($amandapass);
321 return;
322 }
323 }
324 close($amandapass);
325 if ($self->{allow_anonymous}) {
326 $self->{username} = $self->{allow_anonymous};
327 debug("Cannot find password for share $self->{share} in $self->{amandapass}");
328 debug("Using anonymous user: $self->{username}");
329 return;
330 }
331 $self->print_to_server_and_die(
332 "Cannot find password for share $self->{share} in $self->{amandapass}",
333 $Amanda::Script_App::ERROR);
334 }
335
336 sub command_support {
337 my $self = shift;
338
339 print "CONFIG YES\n";
340 print "HOST YES\n";
341 print "DISK YES\n";
342 print "MAX-LEVEL 1\n";
343 print "INDEX-LINE YES\n";
344 print "INDEX-XML NO\n";
345 print "MESSAGE-LINE YES\n";
346 print "MESSAGE-XML NO\n";
347 print "RECORD YES\n";
348 print "COLLECTION NO\n";
349 print "MULTI-ESTIMATE NO\n";
350 print "CALCSIZE NO\n";
351 print "CLIENT-ESTIMATE YES\n";
352 print "EXCLUDE-FILE YES\n";
353 print "EXCLUDE-LIST YES\n";
354 print "EXCLUDE-OPTIONAL YES\n";
355 print "INCLUDE-FILE YES\n";
356 print "INCLUDE-LIST YES\n";
357 print "INCLUDE-OPTIONAL YES\n";
358 print "RECOVER-MODE SMB\n";
359 }
360
361 sub command_selfcheck {
362 my $self = shift;
363
364 $self->print_to_server("disk " . quote_string($self->{disk}),
365 $Amanda::Script_App::GOOD);
366
367 $self->print_to_server("amsamba version " . $Amanda::Constants::VERSION,
368 $Amanda::Script_App::GOOD);
369 #check binary
370 if (!defined($self->{smbclient}) || $self->{smbclient} eq "") {
371 $self->print_to_server(
372 "smbclient not set; you must define the SMBCLIENT-PATH property",
373 $Amanda::Script_App::ERROR);
374 }
375 elsif (! -e $self->{smbclient}) {
376 $self->print_to_server("$self->{smbclient} doesn't exist",
377 $Amanda::Script_App::ERROR);
378 }
379 elsif (! -x $self->{smbclient}) {
380 $self->print_to_server("$self->{smbclient} is not executable",
381 $Amanda::Script_App::ERROR);
382 } else {
383 my @sv = `$self->{smbclient} --version`;
384 if ($? >> 8 == 0) {
385 $sv[0] =~ /^[^0-9]*(.*)$/;
386 my $sv = $1;
387 $self->print_to_server("amsamba smbclient-version $sv",
388 $Amanda::Script_App::GOOD);
389 } else {
390 $self->print_to_server(
391 "[Can't get " . $self->{smbclient} . " version]\n",
392 $Amanda::Script_App::ERROR);
393 }
394 }
395
396 $self->print_to_server("$self->{smbclient}",
397 $Amanda::Script_App::GOOD);
398 if (!defined $self->{disk} || !defined $self->{device}) {
399 return;
400 }
401 $self->parsesharename();
402 $self->findpass();
403 $self->validate_inexclude();
404
405 print "OK " . $self->{share} . "\n";
406 print "OK " . $self->{device} . "\n";
407 print "OK " . $self->{target} . "\n" if defined $self->{target};
408
409 my ($password_rdr, $password_wtr);
410 if (defined $self->{password}) {
411 # Don't set close-on-exec
412 $^F=10;
413 pipe($password_rdr, $password_wtr);
414 $^F=2;
415 $password_wtr->autoflush(1);
416 }
417 my($wtr, $rdr, $err);
418 $err = Symbol::gensym;
419 my $pid = open3($wtr, $rdr, $err, "-");
420 if ($pid == 0) {
421 #child
422 if (defined $self->{password}) {
423 my $ff = $password_rdr->fileno;
424 debug("password_rdr $ff");
425 $password_wtr->close();
426 $ENV{PASSWD_FD} = $password_rdr->fileno;
427 }
428 close(1);
429 close(2);
430 my @ARGV = ();
431 push @ARGV, $self->{smbclient}, $self->{share};
432 push @ARGV, "" if (!defined $self->{password});
433 push @ARGV, "-U", $self->{username},
434 "-E";
435 if (defined $self->{domain}) {
436 push @ARGV, "-W", $self->{domain},
437 }
438 if (defined $self->{subdir}) {
439 push @ARGV, "-D", $self->{subdir},
440 }
441 push @ARGV, "-c", "quit";
442 debug("execute: " . $self->{smbclient} . " " .
443 join(" ", @ARGV));
444 $ENV{'LC_CTYPE'} = 'en_US.UTF-8';
445 exec {$self->{smbclient}} @ARGV;
446 }
447 #parent
448 if (defined $self->{password}) {
449 my $ff = $password_wtr->fileno;
450 debug("password_wtr $ff");
451 $password_wtr->print("$self->{password}\n");
452 $password_wtr->print("$self->{password}\n");
453 $password_wtr->close();
454 $password_rdr->close();
455 } else {
456 debug("No password");
457 }
458 close($wtr);
459 close($rdr);
460 while (<$err>) {
461 chomp;
462 debug("stderr: " . $_);
463 next if /^Domain=/;
464 next if /^WARNING/g;
465 next if /^Unable to initialize messaging context/g;
466 # message if samba server is configured with 'security = share'
467 next if /Server not using user level security and no password supplied./;
468 $self->print_to_server("smbclient: $_",
469 $Amanda::Script_App::ERROR);
470 }
471 close($err);
472 waitpid($pid, 0);
473 #check statefile
474 #check amdevice
475 }
476
477 sub command_estimate {
478 my $self = shift;
479
480 $self->parsesharename();
481 $self->findpass();
482 $self->validate_inexclude();
483
484 my $level = $self->{level}[0];
485 my ($password_rdr, $password_wtr);
486 if (defined $self->{password}) {
487 # Don't set close-on-exec
488 $^F=10;
489 pipe($password_rdr, $password_wtr);
490 $^F=2;
491 $password_wtr->autoflush(1);
492 }
493 my($wtr, $rdr, $err);
494 $err = Symbol::gensym;
495 my $pid = open3($wtr, $rdr, $err, "-");
496 if ($pid == 0) {
497 #child
498 if (defined $self->{password}) {
499 my $ff = $password_rdr->fileno;
500 debug("password_rdr $ff");
501 $password_wtr->close();
502 $ENV{PASSWD_FD} = $password_rdr->fileno;
503 }
504 close(0);
505 close(1);
506 my @ARGV = ();
507 push @ARGV, $self->{smbclient}, $self->{share};
508 push @ARGV, "" if (!defined($self->{password}));
509 push @ARGV, "-d", "0",
510 "-U", $self->{username},
511 "-E";
512 if (defined $self->{domain}) {
513 push @ARGV, "-W", $self->{domain},
514 }
515 if (defined $self->{subdir}) {
516 push @ARGV, "-D", $self->{subdir},
517 }
518 if ($level == 0) {
519 push @ARGV, "-c", "archive 0;recurse;du *"; # needs '*' for current dir (from {subdir}) and below
520 } else {
521 push @ARGV, "-c", "archive 1;recurse;du *";
522 }
523 debug("execute: " . $self->{smbclient} . " " .
524 join(" ", @ARGV));
525 $ENV{'LC_CTYPE'} = 'en_US.UTF-8';
526 exec {$self->{smbclient}} @ARGV;
527 }
528 #parent
529 if (defined $self->{password}) {
530 my $ff = $password_wtr->fileno;
531 debug("password_wtr $ff");
532 $password_wtr->print("$self->{password}\n");
533 $password_wtr->print("$self->{password}\n");
534 $password_wtr->close();
535 $password_rdr->close();
536 }
537 close($wtr);
538 close($rdr);
539 my $size = $self->parse_estimate($err);
540 close($err);
541 output_size($level, $size);
542 waitpid($pid, 0);
543 }
544
545 sub parse_estimate {
546 my $self = shift;
547 my($fh) = shift;
548 my($size) = -1;
549 while(<$fh>) {
550 chomp;
551 next if /^\s*$/;
552 next if /blocks of size/;
553 next if /blocks available/;
554 next if /^\s*$/;
555 next if /^Domain=/;
556 next if /dumped \d+ files and directories/;
557 next if /^WARNING/g;
558 next if /^Unable to initialize messaging context/g;
559 # message if samba server is configured with 'security = share'
560 next if /Server not using user level security and no password supplied./;
561 debug("stderr: $_");
562 if ($_ =~ /^Total number of bytes: (\d*)/) {
563 $size = $1;
564 last;
565 } else {
566 $self->print_to_server("smbclient: $_",
567 $Amanda::Script_App::ERROR);
568 }
569 }
570 return $size;
571 }
572
573 sub output_size {
574 my($level) = shift;
575 my($size) = shift;
576 if($size == -1) {
577 print "$level -1 -1\n";
578 #exit 2;
579 }
580 else {
581 my($ksize) = int $size / (1024);
582 $ksize=32 if ($ksize<32);
583 print "$level $ksize 1\n";
584 }
585 }
586
587 sub send_empty_tar_file {
588 my $self = shift;
589 my ($out1, $out2) = @_;
590 my $out;
591 my $buf;
592 my $size;
593
594 Amanda::Debug::debug("Create empty archive with: tar --create --file=- --files-from=/dev/null");
595 open2($out, undef, "tar", "--create", "--file=-", "--files-from=/dev/null");
596
597 while(($size = sysread($out, $buf, 32768))) {
598 syswrite($out1, $buf, $size);
599 syswrite($out2, $buf, $size);
600 }
601 }
602
603 sub command_backup {
604 my $self = shift;
605
606 my $level = $self->{level}[0];
607
608 $self->parsesharename();
609 $self->findpass();
610 $self->validate_inexclude();
611
612 my ($password_rdr, $password_wtr);
613 if (defined $self->{password}) {
614 # Don't set close-on-exec
615 $^F=10;
616 pipe($password_rdr, $password_wtr);
617 $^F=2;
618 $password_wtr->autoflush(1);
619 }
620 my($smbclient_wtr, $smbclient_rdr, $smbclient_err);
621 $smbclient_err = Symbol::gensym;
622 my $pid = open3($smbclient_wtr, $smbclient_rdr, $smbclient_err, "-");
623 if ($pid == 0) {
624 #child
625 if (defined $self->{password}) {
626 my $ff = $password_rdr->fileno;
627 debug("password_rdr $ff");
628 $password_wtr->close();
629 $ENV{PASSWD_FD} = $password_rdr->fileno;
630 }
631 my @ARGV = ();
632 push @ARGV, $self->{smbclient}, $self->{share};
633 push @ARGV, "" if (!defined($self->{password}));
634 push @ARGV, "-d", "0",
635 "-U", $self->{username},
636 "-E";
637 if (defined $self->{domain}) {
638 push @ARGV, "-W", $self->{domain},
639 }
640 if (defined $self->{subdir}) {
641 push @ARGV, "-D", $self->{subdir},
642 }
643
644 my $comm ;
645 if ($level == 0) {
646 $comm = "tarmode full reset hidden system quiet;";
647 } else {
648 $comm = "tarmode inc noreset hidden system quiet;";
649 }
650 $comm .= " tar c";
651 if ($self->{'regex_match'}) {
652 $comm .= "r";
653 }
654 if ($#{$self->{exclude}} >= 0) {
655 $comm .= "X";
656 }
657 if ($#{$self->{include}} >= 0) {
658 $comm .= "I";
659 }
660 $comm .= " -";
661 if ($#{$self->{exclude}} >= 0) {
662 $comm .= " " . join(" ", @{$self->{exclude}});
663 }
664 if ($#{$self->{include}} >= 0) {
665 $comm .= " " . join(" ", @{$self->{include}});
666 }
667 push @ARGV, "-c", $comm;
668 debug("execute: " . $self->{smbclient} . " " .
669 join(" ", @ARGV));
670 $ENV{'LC_CTYPE'} = 'en_US.UTF-8';
671 exec {$self->{smbclient}} @ARGV;
672 }
673
674 if (defined $self->{password}) {
675 my $ff = $password_wtr->fileno;
676 debug("password_wtr $ff");
677 $password_wtr->print("$self->{password}\n");
678 $password_wtr->print("$self->{password}\n");
679 $password_wtr->close();
680 $password_rdr->close();
681 } else {
682 debug("No password");
683 }
684 close($smbclient_wtr);
685
686 #index process
687 my $index_wtr;
688 my $index_rdr;
689 my $index_err = Symbol::gensym;;
690 debug("$self->{gnutar} -tf -");
691 my $pid_index1 = open3($index_wtr, $index_rdr, $index_err, $self->{gnutar}, "-tf", "-");
692 my $size = -1;
693 my $index_fd = $index_rdr->fileno;
694 debug("index $index_fd");
695 my $indexout_fd;
696 if (defined($self->{index})) {
697 open($indexout_fd, '>&=4') ||
698 $self->print_to_server_and_die("Can't open indexout_fd: $!",
699 $Amanda::Script_App::ERROR);
700 }
701
702 my $file_to_close = 3;
703 my $smbclient_stdout_src = Amanda::MainLoop::fd_source($smbclient_rdr,
704 $G_IO_IN|$G_IO_HUP|$G_IO_ERR);
705 my $smbclient_stderr_src = Amanda::MainLoop::fd_source($smbclient_err,
706 $G_IO_IN|$G_IO_HUP|$G_IO_ERR);
707 my $index_tar_stdout_src = Amanda::MainLoop::fd_source($index_rdr,
708 $G_IO_IN|$G_IO_HUP|$G_IO_ERR);
709 my $index_tar_stderr_src = Amanda::MainLoop::fd_source($index_err,
710 $G_IO_IN|$G_IO_HUP|$G_IO_ERR);
711
712 my $smbclient_stdout_done = 0;
713 my $smbclient_stderr_done = 0;
714 my $data_size = 0;
715 my $nb_files = 0;
716 $smbclient_stdout_src->set_callback(sub {
717 my $buf;
718 my $blocksize = -1;
719 $blocksize = sysread($smbclient_rdr, $buf, 32768);
720 if (!$blocksize) {
721 $file_to_close--;
722 $smbclient_stdout_src->remove();
723 $smbclient_stdout_done = 1;
724 if ($smbclient_stderr_done) {
725 if ($data_size == 0 and $nb_files == 0 and $size == 0) {
726 $self->send_empty_tar_file(*STDOUT, $index_wtr);
727 }
728 close($index_wtr);
729 close(STDOUT);
730 }
731 close($smbclient_rdr);
732 Amanda::MainLoop::quit() if $file_to_close == 0;
733 return;
734 }
735 $data_size += $blocksize;
736 syswrite(STDOUT, $buf, $blocksize);
737 syswrite($index_wtr, $buf, $blocksize);
738 });
739
740 $smbclient_stderr_src->set_callback(sub {
741 my $line = <$smbclient_err>;
742 if (!defined $line) {
743 $file_to_close--;
744 $smbclient_stderr_src->remove();
745 $smbclient_stderr_done = 1;
746 if ($smbclient_stdout_done) {
747 if ($data_size == 0 and $nb_files == 0 and $size == 0) {
748 $self->send_empty_tar_file(*STDOUT, $index_wtr);
749 }
750 close($index_wtr);
751 close(STDOUT);
752 }
753 close ($smbclient_err);
754 Amanda::MainLoop::quit() if $file_to_close == 0;
755 return;
756 }
757 chomp $line;
758 debug("stderr: " . $line);
759 return if $line =~ /Domain=/;
760 return if $line =~ /tarmode is now /;
761 return if $line =~ /tar_re_search set/;
762 return if $line =~ /WARNING/g;
763 return if $line =~ /^Unable to initialize messaging context/;
764 if ($line =~ /dumped (\d+) files and directories/) {
765 $nb_files = $1;
766 return;
767 }
768 # message if samba server is configured with 'security = share'
769 return if $line =~ /Server not using user level security and no password supplied./;
770 if ($line =~ /Total bytes written: (\d*)/) {
771 $size = $1;
772 return;
773 } elsif ($line =~ /Total bytes received: (\d*)/) {
774 $size = $1;
775 return;
776 }
777 $self->print_to_server("smbclient: $line", $Amanda::Script_App::ERROR);
778 });
779
780 $index_tar_stdout_src->set_callback(sub {
781 my $line = <$index_rdr>;
782 if (!defined $line) {
783 $file_to_close--;
784 $index_tar_stdout_src->remove();
785 close($index_rdr);
786 close($indexout_fd);
787 Amanda::MainLoop::quit() if $file_to_close == 0;
788 return;
789 }
790 if ($line =~ /^\.\//) {
791 if(defined($indexout_fd)) {
792 if(defined($self->{index})) {
793 $line =~ s/^\.//;
794 print $indexout_fd $line;
795 }
796 }
797 } else {
798 chomp $line;
799 if ($line =~ /Ignoring unknown extended header keyword/) {
800 debug("tar stderr: $line");
801 } else {
802 $self->print_to_server($line, $Amanda::Script_App::ERROR);
803 }
804 }
805 });
806
807 $index_tar_stderr_src->set_callback(sub {
808 my $line = <$index_err>;
809 if (!defined $line) {
810 $file_to_close--;
811 $index_tar_stderr_src->remove();
812 close($index_err);
813 Amanda::MainLoop::quit() if $file_to_close == 0;
814 return;
815 }
816 chomp $line;
817 if ($line =~ /Ignoring unknown extended header keyword/) {
818 debug("tar stderr: $line");
819 } else {
820 $self->print_to_server($line, $Amanda::Script_App::ERROR);
821 }
822 });
823
824 Amanda::MainLoop::run();
825
826 if ($size >= 0) {
827 my $ksize = $size / 1024;
828 if ($ksize < 32) {
829 $ksize = 32;
830 }
831 print {$self->{mesgout}} "sendbackup: size $ksize\n";
832 }
833
834 waitpid $pid, 0;
835 if ($? != 0) {
836 $self->print_to_server_and_die("smbclient returned error",
837 $Amanda::Script_App::ERROR);
838 }
839 }
840
841 sub parse_backup {
842 my $self = shift;
843 my($fhin, $fhout, $indexout) = @_;
844 my $size = -1;
845 while(<$fhin>) {
846 if ( /^\.\//) {
847 if(defined($indexout)) {
848 if(defined($self->{index})) {
849 s/^\.//;
850 print $indexout $_;
851 }
852 }
853 }
854 else {
855 print $fhout "? $_";
856 }
857 }
858 }
859
860 sub command_index_from_output {
861 index_from_output(0, 1);
862 }
863
864 sub index_from_output {
865 my($fhin, $fhout) = @_;
866 my($size) = -1;
867 while(<$fhin>) {
868 next if /^Total bytes written:/;
869 next if !/^\.\//;
870 s/^\.//;
871 print $fhout $_;
872 }
873 }
874
875 sub command_index {
876 my $self = shift;
877 my $index_fd;
878 open2($index_fd, ">&0", $self->{gnutar}, "--list", "--file", "-") ||
879 $self->print_to_server_and_die("Can't run $self->{gnutar}: $!",
880 $Amanda::Script_App::ERROR);
881 index_from_output($index_fd, \*STDOUT);
882 }
883
884 sub command_restore {
885 my $self = shift;
886 my @cmd = ();
887
888 $self->parsesharename();
889 chdir(Amanda::Util::get_original_cwd());
890
891 if ($self->{recover_mode} eq "smb") {
892 $self->validate_inexclude();
893 $self->findpass();
894 push @cmd, $self->{smbclient}, $self->{share};
895 push @cmd, "-D", $self->{'subdir'} if defined $self->{'subdir'};
896 push @cmd, "" if (!defined $self->{password});
897 push @cmd, "-d", "0",
898 "-U", $self->{username};
899
900 if (defined $self->{domain}) {
901 push @cmd, "-W", $self->{domain};
902 }
903 if (defined $self->{'include_filename'}) {
904 push @cmd, "-TFx", "-", "$self->{'include_filename'}";
905 } else {
906 push @cmd, "-Tx", "-";
907 if ($#{$self->{include}} >= 0) {
908 push @cmd, @{$self->{include}};
909 }
910 for(my $i=1;defined $ARGV[$i]; $i++) {
911 my $param = $ARGV[$i];
912 $param =~ /^(.*)$/;
913 push @cmd, $1;
914 }
915 }
916 my ($parent_rdr, $child_wtr);
917 if (defined $self->{password}) {
918 # Don't set close-on-exec
919 $^F=10;
920 pipe($parent_rdr, $child_wtr);
921 $^F=2;
922 $child_wtr->autoflush(1);
923 }
924 my($wtr, $rdr, $err);
925 $err = Symbol::gensym;
926 my $pid = open3($wtr, $rdr, $err, "-");
927 if ($pid == 0) {
928 $child_wtr->print($self->{password});
929 $child_wtr->close();
930 exit 0;
931 }
932 if (defined $self->{password}) {
933 $child_wtr->close();
934 $ENV{PASSWD_FD} = $parent_rdr->fileno;
935 }
936 debug("cmd:" . join(" ", @cmd));
937 exec { $cmd[0] } @cmd;
938 die("Can't exec '", $cmd[0], "'");
939 } else {
940 push @cmd, $self->{gnutar}, "-xpvf", "-";
941 if (defined $self->{target}) {
942 if (!-d $self->{target}) {
943 $self->print_to_server_and_die(
944 "Directory $self->{target}: $!",
945 $Amanda::Script_App::ERROR);
946 }
947 if (!-w $self->{target}) {
948 $self->print_to_server_and_die(
949 "Directory $self->{target}: $!",
950 $Amanda::Script_App::ERROR);
951 }
952 push @cmd, "--directory", $self->{directory};
953 }
954 if ($#{$self->{include_list}} == 0) {
955 push @cmd, "--files-from", $self->{include_list}[0];
956 }
957 if ($#{$self->{exclude_list}} == 0) {
958 push @cmd, "--exclude-from", $self->{exclude_list}[0];
959 }
960 for(my $i=1;defined $ARGV[$i]; $i++) {
961 my $param = $ARGV[$i];
962 $param =~ /^(.*)$/;
963 push @cmd, $1;
964 }
965 debug("cmd:" . join(" ", @cmd));
966 exec { $cmd[0] } @cmd;
967 die("Can't exec '", $cmd[0], "'");
968 }
969 }
970
971 sub command_validate {
972 my $self = shift;
973
974 if (!defined($self->{gnutar}) || !-x $self->{gnutar}) {
975 return $self->default_validate();
976 }
977
978 my(@cmd) = ($self->{gnutar}, "-tf", "-");
979 debug("cmd:" . join(" ", @cmd));
980 my $pid = open3('>&STDIN', '>&STDOUT', '>&STDERR', @cmd) ||
981 $self->print_to_server_and_die("Unable to run @cmd: $!",
982 $Amanda::Script_App::ERROR);
983 waitpid $pid, 0;
984 if( $? != 0 ){
985 $self->print_to_server_and_die("$self->{gnutar} returned error",
986 $Amanda::Script_App::ERROR);
987 }
988 }
989
990 sub command_print_command {
991 }
992
993 package main;
994
995 sub usage {
996 print <<EOF;
997 Usage: amsamba <command> --config=<config> --host=<host> --disk=<disk> --device=<device> --level=<level> --index=<yes|no> --message=<text> --collection=<no> --record=<yes|no> --calcsize.
998 EOF
999 exit(1);
1000 }
1001
1002 my $opt_version;
1003 my $opt_config;
1004 my $opt_host;
1005 my $opt_disk;
1006 my $opt_device;
1007 my @opt_level;
1008 my $opt_index;
1009 my $opt_message;
1010 my $opt_collection;
1011 my $opt_record;
1012 my $opt_calcsize;
1013 my $opt_gnutar_path;
1014 my $opt_smbclient_path;
1015 my $opt_amandapass;
1016 my @opt_exclude_file;
1017 my @opt_exclude_list;
1018 my $opt_exclude_optional;
1019 my @opt_include_file;
1020 my @opt_include_list;
1021 my $opt_include_optional;
1022 my $opt_recover_mode;
1023 my $opt_allow_anonymous;
1024 my $opt_target;
1025 my $opt_regex_match;
1026
1027 my @orig_argv = @ARGV;
1028
1029 Getopt::Long::Configure(qw{bundling});
1030 GetOptions(
1031 'version' => \$opt_version,
1032 'config=s' => \$opt_config,
1033 'host=s' => \$opt_host,
1034 'disk=s' => \$opt_disk,
1035 'device=s' => \$opt_device,
1036 'level=s' => \@opt_level,
1037 'index=s' => \$opt_index,
1038 'message=s' => \$opt_message,
1039 'collection=s' => \$opt_collection,
1040 'record' => \$opt_record,
1041 'calcsize' => \$opt_calcsize,
1042 'gnutar-path=s' => \$opt_gnutar_path,
1043 'smbclient-path=s' => \$opt_smbclient_path,
1044 'amandapass=s' => \$opt_amandapass,
1045 'exclude-file=s' => \@opt_exclude_file,
1046 'exclude-list=s' => \@opt_exclude_list,
1047 'exclude-optional=s' => \$opt_exclude_optional,
1048 'include-file=s' => \@opt_include_file,
1049 'include-list=s' => \@opt_include_list,
1050 'include-optional=s' => \$opt_include_optional,
1051 'recover-mode=s' => \$opt_recover_mode,
1052 'allow-anonymous=s' => \$opt_allow_anonymous,
1053 'target|directory=s' => \$opt_target,
1054 'regex-match=s' => \$opt_regex_match,
1055 ) or usage();
1056
1057 if (defined $opt_version) {
1058 print "amsamba-" . $Amanda::Constants::VERSION , "\n";
1059 exit(0);
1060 }
1061
1062 my $application = Amanda::Application::Amsamba->new($opt_config, $opt_host, $opt_disk, $opt_device, \@opt_level, $opt_index, $opt_message, $opt_collection, $opt_record, $opt_calcsize, $opt_gnutar_path, $opt_smbclient_path, $opt_amandapass, \@opt_exclude_file, \@opt_exclude_list, $opt_exclude_optional, \@opt_include_file, \@opt_include_list, $opt_include_optional, $opt_recover_mode, $opt_allow_anonymous, $opt_target, $opt_regex_match);
1063
1064 Amanda::Debug::debug("Arguments: " . join(' ', @orig_argv));
1065
1066 $application->do($ARGV[0]);
1067 # NOTREACHED