1 #============================================================= -*-perl-*-
3 # BackupPC::Lib package
7 # This library defines a BackupPC::Lib class and a variety of utility
8 # functions used by BackupPC.
11 # Craig Barratt <cbarratt@users.sourceforge.net>
14 # Copyright (C) 2001-2003 Craig Barratt
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #========================================================================
32 # Version 2.1.0_CVS, released 8 Feb 2004.
34 # See http://backuppc.sourceforge.net.
36 #========================================================================
38 package BackupPC::Lib;
42 use vars qw(%Conf %Lang);
56 my($topDir, $installDir, $noUserCheck) = @_;
59 TopDir => $topDir || '/data/BackupPC',
60 BinDir => $installDir || '/usr/local/BackupPC',
61 LibDir => $installDir || '/usr/local/BackupPC',
62 Version => '2.1.0_CVS',
64 num type startTime endTime
65 nFiles size nFilesExist sizeExist nFilesNew sizeNew
66 xferErrs xferBadFile xferBadShare tarErrs
67 compress sizeExistComp sizeNewComp
68 noFill fillFromNum mangle xferMethod level
71 num startTime endTime result errorMsg nFiles size
72 tarCreateErrs xferErrs
75 num startTime endTime result errorMsg
78 $bpc->{BinDir} .= "/bin";
79 $bpc->{LibDir} .= "/lib";
81 # Clean up %ENV and setup other variables.
83 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
84 $bpc->{PoolDir} = "$bpc->{TopDir}/pool";
85 $bpc->{CPoolDir} = "$bpc->{TopDir}/cpool";
86 if ( defined(my $error = $bpc->ConfigRead()) ) {
87 print(STDERR $error, "\n");
91 # Verify we are running as the correct user
94 && $bpc->{Conf}{BackupPCUserVerify}
95 && $> != (my $uid = (getpwnam($bpc->{Conf}{BackupPCUser}))[2]) ) {
96 print(STDERR "Wrong user: my userid is $>, instead of $uid"
97 . " ($bpc->{Conf}{BackupPCUser})\n");
106 return $bpc->{TopDir};
112 return $bpc->{BinDir};
118 return $bpc->{Version};
124 return %{$bpc->{Conf}};
140 return " trashClean ";
145 my($bpc, $param) = @_;
147 return $bpc->{Conf}{$param};
152 my($bpc, $param) = @_;
154 $bpc->{verbose} = $param if ( defined($param) );
155 return $bpc->{verbose};
162 if ( !defined($bpc->{SigName2Num}) ) {
164 foreach my $name ( split(' ', $Config{sig_name}) ) {
165 $bpc->{SigName2Num}{$name} = $i;
169 return $bpc->{SigName2Num}{$sig};
173 # Generate an ISO 8601 format timeStamp (but without the "T").
174 # See http://www.w3.org/TR/NOTE-datetime and
175 # http://www.cl.cam.ac.uk/~mgk25/iso-time.html
179 my($bpc, $t, $noPad) = @_;
180 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
181 = localtime($t || time);
182 return sprintf("%04d-%02d-%02d %02d:%02d:%02d",
183 $year + 1900, $mon + 1, $mday, $hour, $min, $sec)
184 . ($noPad ? "" : " ");
189 my($bpc, $host) = @_;
190 local(*BK_INFO, *LOCK);
193 flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
194 if ( open(BK_INFO, "$bpc->{TopDir}/pc/$host/backups") ) {
196 while ( <BK_INFO> ) {
198 next if ( !/^(\d+\t(incr|full|partial)[\d\t]*$)/ );
200 @{$Backups[@Backups]}{@{$bpc->{BackupFields}}} = split(/\t/);
210 my($bpc, $host, @Backups) = @_;
211 local(*BK_INFO, *LOCK);
214 flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
215 unlink("$bpc->{TopDir}/pc/$host/backups.old")
216 if ( -f "$bpc->{TopDir}/pc/$host/backups.old" );
217 rename("$bpc->{TopDir}/pc/$host/backups",
218 "$bpc->{TopDir}/pc/$host/backups.old")
219 if ( -f "$bpc->{TopDir}/pc/$host/backups" );
220 if ( open(BK_INFO, ">$bpc->{TopDir}/pc/$host/backups") ) {
222 for ( $i = 0 ; $i < @Backups ; $i++ ) {
223 my %b = %{$Backups[$i]};
224 printf(BK_INFO "%s\n", join("\t", @b{@{$bpc->{BackupFields}}}));
233 my($bpc, $host) = @_;
234 local(*RESTORE_INFO, *LOCK);
237 flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
238 if ( open(RESTORE_INFO, "$bpc->{TopDir}/pc/$host/restores") ) {
239 binmode(RESTORE_INFO);
240 while ( <RESTORE_INFO> ) {
242 next if ( !/^(\d+.*)/ );
244 @{$Restores[@Restores]}{@{$bpc->{RestoreFields}}} = split(/\t/);
254 my($bpc, $host, @Restores) = @_;
255 local(*RESTORE_INFO, *LOCK);
258 flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
259 unlink("$bpc->{TopDir}/pc/$host/restores.old")
260 if ( -f "$bpc->{TopDir}/pc/$host/restores.old" );
261 rename("$bpc->{TopDir}/pc/$host/restores",
262 "$bpc->{TopDir}/pc/$host/restores.old")
263 if ( -f "$bpc->{TopDir}/pc/$host/restores" );
264 if ( open(RESTORE_INFO, ">$bpc->{TopDir}/pc/$host/restores") ) {
265 binmode(RESTORE_INFO);
266 for ( $i = 0 ; $i < @Restores ; $i++ ) {
267 my %b = %{$Restores[$i]};
268 printf(RESTORE_INFO "%s\n",
269 join("\t", @b{@{$bpc->{RestoreFields}}}));
278 my($bpc, $host) = @_;
279 local(*ARCHIVE_INFO, *LOCK);
282 flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
283 if ( open(ARCHIVE_INFO, "$bpc->{TopDir}/pc/$host/archives") ) {
284 binmode(ARCHIVE_INFO);
285 while ( <ARCHIVE_INFO> ) {
287 next if ( !/^(\d+.*)/ );
289 @{$Archives[@Archives]}{@{$bpc->{ArchiveFields}}} = split(/\t/);
299 my($bpc, $host, @Archives) = @_;
300 local(*ARCHIVE_INFO, *LOCK);
303 flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
304 unlink("$bpc->{TopDir}/pc/$host/archives.old")
305 if ( -f "$bpc->{TopDir}/pc/$host/archives.old" );
306 rename("$bpc->{TopDir}/pc/$host/archives",
307 "$bpc->{TopDir}/pc/$host/archives.old")
308 if ( -f "$bpc->{TopDir}/pc/$host/archives" );
309 if ( open(ARCHIVE_INFO, ">$bpc->{TopDir}/pc/$host/archives") ) {
310 binmode(ARCHIVE_INFO);
311 for ( $i = 0 ; $i < @Archives ; $i++ ) {
312 my %b = %{$Archives[$i]};
313 printf(ARCHIVE_INFO "%s\n",
314 join("\t", @b{@{$bpc->{ArchiveFields}}}));
323 my($bpc, $host) = @_;
324 my($ret, $mesg, $config, @configs);
327 push(@configs, "$bpc->{TopDir}/conf/config.pl");
328 push(@configs, "$bpc->{TopDir}/conf/$host.pl")
329 if ( $host ne "config" && -f "$bpc->{TopDir}/conf/$host.pl" );
330 push(@configs, "$bpc->{TopDir}/pc/$host/config.pl")
331 if ( defined($host) && -f "$bpc->{TopDir}/pc/$host/config.pl" );
332 foreach $config ( @configs ) {
334 if ( !defined($ret = do $config) && ($! || $@) ) {
335 $mesg = "Couldn't open $config: $!" if ( $! );
336 $mesg = "Couldn't execute $config: $@" if ( $@ );
337 $mesg =~ s/[\n\r]+//;
340 %{$bpc->{Conf}} = ( %{$bpc->{Conf} || {}}, %Conf );
342 return if ( !defined($bpc->{Conf}{Language}) );
343 if ( defined($bpc->{Conf}{PerlModuleLoad}) ) {
345 # Load any user-specified perl modules. This is for
346 # optional user-defined extensions.
348 $bpc->{Conf}{PerlModuleLoad} = [$bpc->{Conf}{PerlModuleLoad}]
349 if ( ref($bpc->{Conf}{PerlModuleLoad}) ne "ARRAY" );
350 foreach my $module ( @{$bpc->{Conf}{PerlModuleLoad}} ) {
351 eval("use $module;");
354 my $langFile = "$bpc->{LibDir}/BackupPC/Lang/$bpc->{Conf}{Language}.pm";
355 if ( !defined($ret = do $langFile) && ($! || $@) ) {
356 $mesg = "Couldn't open language file $langFile: $!" if ( $! );
357 $mesg = "Couldn't execute language file $langFile: $@" if ( $@ );
358 $mesg =~ s/[\n\r]+//;
361 $bpc->{Lang} = \%Lang;
366 # Return the mtime of the config file
371 return (stat("$bpc->{TopDir}/conf/config.pl"))[9];
375 # Returns information from the host file in $bpc->{TopDir}/conf/hosts.
376 # With no argument a ref to a hash of hosts is returned. Each
377 # hash contains fields as specified in the hosts file. With an
378 # argument a ref to a single hash is returned with information
379 # for just that host.
383 my($bpc, $host) = @_;
384 my(%hosts, @hdr, @fld);
387 if ( !open(HOST_INFO, "$bpc->{TopDir}/conf/hosts") ) {
388 print(STDERR $bpc->timeStamp,
389 "Can't open $bpc->{TopDir}/conf/hosts\n");
393 while ( <HOST_INFO> ) {
397 next if ( /^\s*$/ || !/^([\w\.\\-]+\s+.*)/ );
399 # Split on white space, except if preceded by \
400 # using zero-width negative look-behind assertion
401 # (always wanted to use one of those).
403 @fld = split(/(?<!\\)\s+/, $1);
411 if ( defined($host) ) {
412 next if ( lc($fld[0]) ne $host );
413 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
417 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
428 # Return the mtime of the hosts file
433 return (stat("$bpc->{TopDir}/conf/hosts"))[9];
437 # Stripped down from File::Path. In particular we don't print
438 # many warnings and we try three times to delete each directory
439 # and file -- for some reason the original File::Path rmtree
440 # didn't always completely remove a directory tree on the NetApp.
442 # Warning: this routine changes the cwd.
446 my($bpc, $pwd, $roots) = @_;
449 if ( defined($roots) && length($roots) ) {
450 $roots = [$roots] unless ref $roots;
452 print(STDERR "RmTreeQuiet: No root path(s) specified\n");
455 foreach $root (@{$roots}) {
456 $root = $1 if ( $root =~ m{(.*?)/*$} );
458 # Try first to simply unlink the file: this avoids an
459 # extra stat for every file. If it fails (which it
460 # will for directories), check if it is a directory and
463 if ( !unlink($root) ) {
465 my $d = DirHandle->new($root)
466 or print(STDERR "Can't read $pwd/$root: $!");
469 @files = grep $_!~/^\.{1,2}$/, @files;
470 $bpc->RmTreeQuiet("$pwd/$root", \@files);
472 rmdir($root) || rmdir($root);
474 unlink($root) || unlink($root);
481 # Move a directory or file away for later deletion
485 my($bpc, $trashDir, $file) = @_;
488 return if ( !-e $file );
489 mkpath($trashDir, 0, 0777) if ( !-d $trashDir );
490 for ( $i = 0 ; $i < 1000 ; $i++ ) {
491 $f = sprintf("%s/%d_%d_%d", $trashDir, time, $$, $i);
493 return if ( rename($file, $f) );
495 # shouldn't get here, but might if you tried to call this
496 # across file systems.... just remove the tree right now.
497 if ( $file =~ /(.*)\/([^\/]*)/ ) {
500 my($cwd) = Cwd::fastcwd();
501 $cwd = $1 if ( $cwd =~ /(.*)/ );
502 $bpc->RmTreeQuiet($d, $f);
503 chdir($cwd) if ( $cwd );
508 # Empty the trash directory. Returns 0 if it did nothing, 1 if it
509 # did something, -1 if it failed to remove all the files.
513 my($bpc, $trashDir) = @_;
515 my($cwd) = Cwd::fastcwd();
517 $cwd = $1 if ( $cwd =~ /(.*)/ );
518 return if ( !-d $trashDir );
519 my $d = DirHandle->new($trashDir) or carp "Can't read $trashDir: $!";
522 @files = grep $_!~/^\.{1,2}$/, @files;
523 return 0 if ( !@files );
524 $bpc->RmTreeQuiet($trashDir, \@files);
525 foreach my $f ( @files ) {
526 return -1 if ( -e $f );
528 chdir($cwd) if ( $cwd );
533 # Open a connection to the server. Returns an error string on failure.
534 # Returns undef on success.
538 my($bpc, $host, $port, $justConnect) = @_;
541 return if ( defined($bpc->{ServerFD}) );
543 # First try the unix-domain socket
545 my $sockFile = "$bpc->{TopDir}/log/BackupPC.sock";
546 socket(*FH, PF_UNIX, SOCK_STREAM, 0) || return "unix socket: $!";
547 if ( !connect(*FH, sockaddr_un($sockFile)) ) {
548 my $err = "unix connect: $!";
551 my $proto = getprotobyname('tcp');
552 my $iaddr = inet_aton($host) || return "unknown host $host";
553 my $paddr = sockaddr_in($port, $iaddr);
555 socket(*FH, PF_INET, SOCK_STREAM, $proto)
556 || return "inet socket: $!";
557 connect(*FH, $paddr) || return "inet connect: $!";
562 my($oldFH) = select(*FH); $| = 1; select($oldFH);
563 $bpc->{ServerFD} = *FH;
564 return if ( $justConnect );
566 # Read the seed that we need for our MD5 message digest. See
569 sysread($bpc->{ServerFD}, $bpc->{ServerSeed}, 1024);
570 $bpc->{ServerMesgCnt} = 0;
575 # Check that the server connection is still ok
581 return 0 if ( !defined($bpc->{ServerFD}) );
582 vec(my $FDread, fileno($bpc->{ServerFD}), 1) = 1;
584 return 0 if ( select(my $rout = $FDread, undef, $ein, 0.0) < 0 );
585 return 1 if ( !vec($rout, fileno($bpc->{ServerFD}), 1) );
589 # Disconnect from the server
594 return if ( !defined($bpc->{ServerFD}) );
595 close($bpc->{ServerFD});
596 delete($bpc->{ServerFD});
600 # Sends a message to the server and returns with the reply.
602 # To avoid possible attacks via the TCP socket interface, every client
603 # message is protected by an MD5 digest. The MD5 digest includes four
605 # - a seed that is sent to us when we first connect
606 # - a sequence number that increments for each message
607 # - a shared secret that is stored in $Conf{ServerMesgSecret}
608 # - the message itself.
609 # The message is sent in plain text preceded by the MD5 digest. A
610 # snooper can see the plain-text seed sent by BackupPC and plain-text
611 # message, but cannot construct a valid MD5 digest since the secret in
612 # $Conf{ServerMesgSecret} is unknown. A replay attack is not possible
613 # since the seed changes on a per-connection and per-message basis.
617 my($bpc, $mesg) = @_;
618 return if ( !defined(my $fh = $bpc->{ServerFD}) );
619 my $md5 = Digest::MD5->new;
620 $md5->add($bpc->{ServerSeed} . $bpc->{ServerMesgCnt}
621 . $bpc->{Conf}{ServerMesgSecret} . $mesg);
622 print($fh $md5->b64digest . " $mesg\n");
623 $bpc->{ServerMesgCnt}++;
628 # Do initialization for child processes
634 open(STDERR, ">&STDOUT");
635 select(STDERR); $| = 1;
636 select(STDOUT); $| = 1;
637 $ENV{PATH} = $bpc->{Conf}{MyPath};
641 # Compute the MD5 digest of a file. For efficiency we don't
642 # use the whole file for big files:
643 # - for files <= 256K we use the file size and the whole file.
644 # - for files <= 1M we use the file size, the first 128K and
646 # - for files > 1M, we use the file size, the first 128K and
647 # the 8th 128K (ie: the 128K up to 1MB).
648 # See the documentation for a discussion of the tradeoffs in
649 # how much data we use and how many collisions we get.
651 # Returns the MD5 digest (a hex string) and the file size.
655 my($bpc, $md5, $name) = @_;
656 my($data, $fileSize);
659 $fileSize = (stat($name))[7];
660 return ("", -1) if ( !-f _ );
661 $name = $1 if ( $name =~ /(.*)/ );
662 return ("", 0) if ( $fileSize == 0 );
663 return ("", -1) if ( !open(N, $name) );
666 $md5->add($fileSize);
667 if ( $fileSize > 262144 ) {
669 # read the first and last 131072 bytes of the file,
672 my $seekPosn = ($fileSize > 1048576 ? 1048576 : $fileSize) - 131072;
673 $md5->add($data) if ( sysread(N, $data, 131072) );
674 $md5->add($data) if ( sysseek(N, $seekPosn, 0)
675 && sysread(N, $data, 131072) );
678 # read the whole file
680 $md5->add($data) if ( sysread(N, $data, $fileSize) );
683 return ($md5->hexdigest, $fileSize);
687 # Compute the MD5 digest of a buffer (string). For efficiency we don't
688 # use the whole string for big strings:
689 # - for files <= 256K we use the file size and the whole file.
690 # - for files <= 1M we use the file size, the first 128K and
692 # - for files > 1M, we use the file size, the first 128K and
693 # the 8th 128K (ie: the 128K up to 1MB).
694 # See the documentation for a discussion of the tradeoffs in
695 # how much data we use and how many collisions we get.
697 # Returns the MD5 digest (a hex string).
701 my($bpc, $md5, $fileSize, $dataRef) = @_;
704 $md5->add($fileSize);
705 if ( $fileSize > 262144 ) {
707 # add the first and last 131072 bytes of the string,
710 my $seekPosn = ($fileSize > 1048576 ? 1048576 : $fileSize) - 131072;
711 $md5->add(substr($$dataRef, 0, 131072));
712 $md5->add(substr($$dataRef, $seekPosn, 131072));
715 # add the whole string
717 $md5->add($$dataRef);
719 return $md5->hexdigest;
723 # Given an MD5 digest $d and a compress flag, return the full
728 my($bpc, $d, $compress, $poolDir) = @_;
730 return if ( $d !~ m{(.)(.)(.)(.*)} );
731 $poolDir = ($compress ? $bpc->{CPoolDir} : $bpc->{PoolDir})
732 if ( !defined($poolDir) );
733 return "$poolDir/$1/$2/$3/$1$2$3$4";
737 # For each file, check if the file exists in $bpc->{TopDir}/pool.
738 # If so, remove the file and make a hardlink to the file in
739 # the pool. Otherwise, if the newFile flag is set, make a
740 # hardlink in the pool to the new file.
742 # Returns 0 if a link should be made to a new file (ie: when the file
743 # is a new file but the newFile flag is 0).
744 # Returns 1 if a link to an existing file is made,
745 # Returns 2 if a link to a new file is made (only if $newFile is set)
746 # Returns negative on error.
750 my($bpc, $name, $d, $newFile, $compress) = @_;
753 return -1 if ( !-f $name );
754 for ( $i = -1 ; ; $i++ ) {
755 return -2 if ( !defined($rawFile = $bpc->MD52Path($d, $compress)) );
756 $rawFile .= "_$i" if ( $i >= 0 );
758 if ( (stat(_))[3] < $bpc->{Conf}{HardLinkMax}
759 && !compare($name, $rawFile) ) {
761 return -3 if ( !link($rawFile, $name) );
764 } elsif ( $newFile && -f $name && (stat($name))[3] == 1 ) {
766 ($newDir = $rawFile) =~ s{(.*)/.*}{$1};
767 mkpath($newDir, 0, 0777) if ( !-d $newDir );
768 return -4 if ( !link($name, $rawFile) );
778 my($bpc, $host) = @_;
779 my($s, $pingCmd, $ret);
782 # Return success if the ping cmd is undefined or empty.
784 if ( $bpc->{Conf}{PingCmd} eq "" ) {
785 print(STDERR "CheckHostAlive: return ok because \$Conf{PingCmd}"
786 . " is empty\n") if ( $bpc->{verbose} );
791 pingPath => $bpc->{Conf}{PingPath},
794 $pingCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{PingCmd}, $args);
797 # Do a first ping in case the PC needs to wakeup
799 $s = $bpc->cmdSystemOrEval($pingCmd, undef, $args);
801 print(STDERR "CheckHostAlive: first ping failed ($?, $!)\n")
802 if ( $bpc->{verbose} );
807 # Do a second ping and get the round-trip time in msec
809 $s = $bpc->cmdSystemOrEval($pingCmd, undef, $args);
811 print(STDERR "CheckHostAlive: second ping failed ($?, $!)\n")
812 if ( $bpc->{verbose} );
815 if ( $s =~ /time=([\d\.]+)\s*ms/i ) {
817 } elsif ( $s =~ /time=([\d\.]+)\s*usec/i ) {
820 print(STDERR "CheckHostAlive: can't extract round-trip time"
821 . " (not fatal)\n") if ( $bpc->{verbose} );
824 print(STDERR "CheckHostAlive: returning $ret\n") if ( $bpc->{verbose} );
828 sub CheckFileSystemUsage
831 my($topDir) = $bpc->{TopDir};
834 return 0 if ( $bpc->{Conf}{DfCmd} eq "" );
836 dfPath => $bpc->{Conf}{DfPath},
837 topDir => $bpc->{TopDir},
839 $dfCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{DfCmd}, $args);
840 $s = $bpc->cmdSystemOrEval($dfCmd, undef, $args);
841 return 0 if ( $? || $s !~ /(\d+)%/s );
846 # Given an IP address, return the host name and user name via
851 my($bpc, $host) = @_;
852 my($netBiosHostName, $netBiosUserName);
856 # Skip NetBios check if NmbLookupCmd is emtpy
858 if ( $bpc->{Conf}{NmbLookupCmd} eq "" ) {
859 print(STDERR "NetBiosInfoGet: return $host because \$Conf{NmbLookupCmd}"
860 . " is empty\n") if ( $bpc->{verbose} );
861 return ($host, undef);
865 nmbLookupPath => $bpc->{Conf}{NmbLookupPath},
868 $nmbCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{NmbLookupCmd}, $args);
869 foreach ( split(/[\n\r]+/, $bpc->cmdSystemOrEval($nmbCmd, undef, $args)) ) {
870 next if ( !/^\s*([\w\s-]+?)\s*<(\w{2})\> - .*<ACTIVE>/i );
871 $netBiosHostName ||= $1 if ( $2 eq "00" ); # host is first 00
872 $netBiosUserName = $1 if ( $2 eq "03" ); # user is last 03
874 if ( !defined($netBiosHostName) ) {
875 print(STDERR "NetBiosInfoGet: failed: can't parse return string\n")
876 if ( $bpc->{verbose} );
879 $netBiosHostName = lc($netBiosHostName);
880 $netBiosUserName = lc($netBiosUserName);
881 print(STDERR "NetBiosInfoGet: success, returning host $netBiosHostName,"
882 . " user $netBiosUserName\n") if ( $bpc->{verbose} );
883 return ($netBiosHostName, $netBiosUserName);
887 # Given a NetBios name lookup the IP address via NetBios.
888 # In the case of a host returning multiple interfaces we
889 # return the first IP address that matches the subnet mask.
890 # If none match the subnet mask (or nmblookup doesn't print
891 # the subnet mask) then just the first IP address is returned.
893 sub NetBiosHostIPFind
895 my($bpc, $host) = @_;
896 my($netBiosHostName, $netBiosUserName);
897 my($s, $nmbCmd, $subnet, $ipAddr, $firstIpAddr);
900 # Skip NetBios lookup if NmbLookupFindHostCmd is emtpy
902 if ( $bpc->{Conf}{NmbLookupFindHostCmd} eq "" ) {
903 print(STDERR "NetBiosHostIPFind: return $host because"
904 . " \$Conf{NmbLookupFindHostCmd} is empty\n")
905 if ( $bpc->{verbose} );
910 nmbLookupPath => $bpc->{Conf}{NmbLookupPath},
913 $nmbCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{NmbLookupFindHostCmd}, $args);
914 foreach my $resp ( split(/[\n\r]+/, $bpc->cmdSystemOrEval($nmbCmd, undef,
916 if ( $resp =~ /querying\s+\Q$host\E\s+on\s+(\d+\.\d+\.\d+\.\d+)/i ) {
918 $subnet = $1 if ( $subnet =~ /^(.*?)(\.255)+$/ );
919 } elsif ( $resp =~ /^\s*(\d+\.\d+\.\d+\.\d+)\s+\Q$host/ ) {
921 $firstIpAddr = $ip if ( !defined($firstIpAddr) );
922 $ipAddr = $ip if ( !defined($ipAddr) && $ip =~ /^\Q$subnet/ );
925 $ipAddr = $firstIpAddr if ( !defined($ipAddr) );
926 if ( defined($ipAddr) ) {
927 print(STDERR "NetBiosHostIPFind: found IP address $ipAddr for"
928 . " host $host\n") if ( $bpc->{verbose} );
931 print(STDERR "NetBiosHostIPFind: couldn't find IP address for"
932 . " host $host\n") if ( $bpc->{verbose} );
937 sub fileNameEltMangle
939 my($bpc, $name) = @_;
941 return "" if ( $name eq "" );
942 $name =~ s{([%/\n\r])}{sprintf("%%%02x", ord($1))}eg;
947 # We store files with every name preceded by "f". This
948 # avoids possible name conflicts with other information
949 # we store in the same directories (eg: attribute info).
950 # The process of turning a normal path into one with each
951 # node prefixed with "f" is called mangling.
955 my($bpc, $name) = @_;
957 $name =~ s{/([^/]+)}{"/" . $bpc->fileNameEltMangle($1)}eg;
958 $name =~ s{^([^/]+)}{$bpc->fileNameEltMangle($1)}eg;
963 # This undoes FileNameMangle
967 my($bpc, $name) = @_;
971 $name =~ s{%(..)}{chr(hex($1))}eg;
976 # Escape shell meta-characters with backslashes.
977 # This should be applied to each argument seperately, not an
978 # entire shell command.
984 $cmd =~ s/([][;&()<>{}|^\n\r\t *\$\\'"`?])/\\$1/g;
989 # For printing exec commands (which don't use a shell) so they look like
990 # a valid shell command this function should be called with the exec
991 # args. The shell command string is returned.
995 my($bpc, @args) = @_;
998 foreach my $a ( @args ) {
999 $str .= " " if ( $str ne "" );
1000 $str .= $bpc->shellEscape($a);
1006 # Do a URI-style escape to protect/encode special characters
1011 $s =~ s{([^\w.\/-])}{sprintf("%%%02X", ord($1));}eg;
1016 # Do a URI-style unescape to restore special characters
1021 $s =~ s{%(..)}{chr(hex($1))}eg;
1026 # Do variable substitution prior to execution of a command.
1028 sub cmdVarSubstitute
1030 my($bpc, $template, $vars) = @_;
1034 # Return without any substitution if the first entry starts with "&",
1035 # indicating this is perl code.
1037 if ( (ref($template) eq "ARRAY" ? $template->[0] : $template) =~ /^\&/ ) {
1040 if ( ref($template) ne "ARRAY" ) {
1042 # Split at white space, except if escaped by \
1044 $template = [split(/(?<!\\)\s+/, $template)];
1046 # Remove the \ that escaped white space.
1048 foreach ( @$template ) {
1053 # Merge variables into @tarClientCmd
1055 foreach my $arg ( @$template ) {
1057 # Replace scalar variables first
1059 $arg =~ s{\$(\w+)(\+?)}{
1060 exists($vars->{$1}) && ref($vars->{$1}) ne "ARRAY"
1061 ? ($2 eq "+" ? $bpc->shellEscape($vars->{$1}) : $vars->{$1})
1065 # Now replicate any array arguments; this just works for just one
1066 # array var in each argument.
1068 if ( $arg =~ m{(.*)\$(\w+)(\+?)(.*)} && ref($vars->{$2}) eq "ARRAY" ) {
1073 foreach my $v ( @{$vars->{$var}} ) {
1074 $v = $bpc->shellEscape($v) if ( $esc eq "+" );
1075 push(@cmd, "$pre$v$post");
1085 # Exec or eval a command. $cmd is either a string on an array ref.
1087 # @args are optional arguments for the eval() case; they are not used
1092 my($bpc, $cmd, @args) = @_;
1094 if ( (ref($cmd) eq "ARRAY" ? $cmd->[0] : $cmd) =~ /^\&/ ) {
1095 $cmd = join(" ", $cmd) if ( ref($cmd) eq "ARRAY" );
1096 print(STDERR "cmdExecOrEval: about to eval perl code $cmd\n")
1097 if ( $bpc->{verbose} );
1099 print(STDERR "Perl code fragment for exec shouldn't return!!\n");
1102 $cmd = [split(/\s+/, $cmd)] if ( ref($cmd) ne "ARRAY" );
1103 print(STDERR "cmdExecOrEval: about to exec ",
1104 $bpc->execCmd2ShellCmd(@$cmd), "\n")
1105 if ( $bpc->{verbose} );
1107 $cmd = [map { m/(.*)/ } @$cmd]; # untaint
1109 # force list-form of exec(), ie: no shell even for 1 arg
1111 exec { $cmd->[0] } @$cmd;
1112 print(STDERR "Exec failed for @$cmd\n");
1118 # System or eval a command. $cmd is either a string on an array ref.
1119 # $stdoutCB is a callback for output generated by the command. If it
1120 # is undef then output is returned. If it is a code ref then the function
1121 # is called with each piece of output as an argument. If it is a scalar
1122 # ref the output is appended to this variable.
1124 # @args are optional arguments for the eval() case; they are not used
1127 # Also, $? should be set when the CHILD pipe is closed.
1131 my($bpc, $cmd, $stdoutCB, @args) = @_;
1132 my($pid, $out, $allOut);
1135 if ( (ref($cmd) eq "ARRAY" ? $cmd->[0] : $cmd) =~ /^\&/ ) {
1136 $cmd = join(" ", $cmd) if ( ref($cmd) eq "ARRAY" );
1137 print(STDERR "cmdSystemOrEval: about to eval perl code $cmd\n")
1138 if ( $bpc->{verbose} );
1140 $$stdoutCB .= $out if ( ref($stdoutCB) eq 'SCALAR' );
1141 &$stdoutCB($out) if ( ref($stdoutCB) eq 'CODE' );
1142 print(STDERR "cmdSystemOrEval: finished: got output $out\n")
1143 if ( $bpc->{verbose} );
1144 return $out if ( !defined($stdoutCB) );
1147 $cmd = [split(/\s+/, $cmd)] if ( ref($cmd) ne "ARRAY" );
1148 print(STDERR "cmdSystemOrEval: about to system ",
1149 $bpc->execCmd2ShellCmd(@$cmd), "\n")
1150 if ( $bpc->{verbose} );
1151 if ( !defined($pid = open(CHILD, "-|")) ) {
1152 my $err = "Can't fork to run @$cmd\n";
1154 $$stdoutCB .= $err if ( ref($stdoutCB) eq 'SCALAR' );
1155 &$stdoutCB($err) if ( ref($stdoutCB) eq 'CODE' );
1156 return $err if ( !defined($stdoutCB) );
1165 open(STDERR, ">&STDOUT");
1167 $cmd = [map { m/(.*)/ } @$cmd]; # untaint
1169 # force list-form of exec(), ie: no shell even for 1 arg
1171 exec { $cmd->[0] } @$cmd;
1172 print(STDERR "Exec of @$cmd failed\n");
1176 # The parent gathers the output from the child
1179 $$stdoutCB .= $_ if ( ref($stdoutCB) eq 'SCALAR' );
1180 &$stdoutCB($_) if ( ref($stdoutCB) eq 'CODE' );
1181 $out .= $_ if ( !defined($stdoutCB) );
1182 $allOut .= $_ if ( $bpc->{verbose} );
1187 print(STDERR "cmdSystemOrEval: finished: got output $allOut\n")
1188 if ( $bpc->{verbose} );