Added parchive as a possible program name for par.
[BackupPC.git] / lib / BackupPC / Lib.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::Lib package
4 #
5 # DESCRIPTION
6 #
7 #   This library defines a BackupPC::Lib class and a variety of utility
8 #   functions used by BackupPC.
9 #
10 # AUTHOR
11 #   Craig Barratt  <cbarratt@users.sourceforge.net>
12 #
13 # COPYRIGHT
14 #   Copyright (C) 2001-2003  Craig Barratt
15 #
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.
20 #
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.
25 #
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
29 #
30 #========================================================================
31 #
32 # Version 2.1.0_CVS, released 3 Jul 2003.
33 #
34 # See http://backuppc.sourceforge.net.
35 #
36 #========================================================================
37
38 package BackupPC::Lib;
39
40 use strict;
41
42 use vars qw(%Conf %Lang);
43 use Fcntl qw/:flock/;
44 use Carp;
45 use DirHandle ();
46 use File::Path;
47 use File::Compare;
48 use Socket;
49 use Cwd;
50 use Digest::MD5;
51
52 sub new
53 {
54     my $class = shift;
55     my($topDir, $installDir, $noUserCheck) = @_;
56
57     my $bpc = bless {
58         TopDir  => $topDir || '/data/BackupPC',
59         BinDir  => $installDir || '/usr/local/BackupPC',
60         LibDir  => $installDir || '/usr/local/BackupPC',
61         Version => '2.1.0_CVS',
62         BackupFields => [qw(
63                     num type startTime endTime
64                     nFiles size nFilesExist sizeExist nFilesNew sizeNew
65                     xferErrs xferBadFile xferBadShare tarErrs
66                     compress sizeExistComp sizeNewComp
67                     noFill fillFromNum mangle xferMethod level
68                 )],
69         RestoreFields => [qw(
70                     num startTime endTime result errorMsg nFiles size
71                     tarCreateErrs xferErrs
72                 )],
73         ArchiveFields => [qw(
74                     num startTime endTime result errorMsg
75                 )],
76     }, $class;
77     $bpc->{BinDir} .= "/bin";
78     $bpc->{LibDir} .= "/lib";
79     #
80     # Clean up %ENV and setup other variables.
81     #
82     delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
83     $bpc->{PoolDir}  = "$bpc->{TopDir}/pool";
84     $bpc->{CPoolDir} = "$bpc->{TopDir}/cpool";
85     if ( defined(my $error = $bpc->ConfigRead()) ) {
86         print(STDERR $error, "\n");
87         return;
88     }
89     #
90     # Verify we are running as the correct user
91     #
92     if ( !$noUserCheck
93             && $bpc->{Conf}{BackupPCUserVerify}
94             && $> != (my $uid = (getpwnam($bpc->{Conf}{BackupPCUser}))[2]) ) {
95         print("Wrong user: my userid is $>, instead of $uid"
96             . " ($bpc->{Conf}{BackupPCUser})\n");
97         return;
98     }
99     return $bpc;
100 }
101
102 sub TopDir
103 {
104     my($bpc) = @_;
105     return $bpc->{TopDir};
106 }
107
108 sub BinDir
109 {
110     my($bpc) = @_;
111     return $bpc->{BinDir};
112 }
113
114 sub Version
115 {
116     my($bpc) = @_;
117     return $bpc->{Version};
118 }
119
120 sub Conf
121 {
122     my($bpc) = @_;
123     return %{$bpc->{Conf}};
124 }
125
126 sub Lang
127 {
128     my($bpc) = @_;
129     return $bpc->{Lang};
130 }
131
132 sub adminJob
133 {
134     return " admin ";
135 }
136
137 sub trashJob
138 {
139     return " trashClean ";
140 }
141
142 sub ConfValue
143 {
144     my($bpc, $param) = @_;
145
146     return $bpc->{Conf}{$param};
147 }
148
149 sub verbose
150 {
151     my($bpc, $param) = @_;
152
153     $bpc->{verbose} = $param if ( defined($param) );
154     return $bpc->{verbose};
155 }
156
157 #
158 # Generate an ISO 8601 format timeStamp (but without the "T").
159 # See http://www.w3.org/TR/NOTE-datetime and
160 # http://www.cl.cam.ac.uk/~mgk25/iso-time.html
161 #
162 sub timeStamp
163 {
164     my($bpc, $t, $noPad) = @_;
165     my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
166               = localtime($t || time);
167     return sprintf("%04d-%02d-%02d %02d:%02d:%02d",
168                     $year + 1900, $mon + 1, $mday, $hour, $min, $sec)
169              . ($noPad ? "" : " ");
170 }
171
172 sub BackupInfoRead
173 {
174     my($bpc, $host) = @_;
175     local(*BK_INFO, *LOCK);
176     my(@Backups);
177
178     flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
179     if ( open(BK_INFO, "$bpc->{TopDir}/pc/$host/backups") ) {
180         binmode(BK_INFO);
181         while ( <BK_INFO> ) {
182             s/[\n\r]+//;
183             next if ( !/^(\d+\t(incr|full|partial)[\d\t]*$)/ );
184             $_ = $1;
185             @{$Backups[@Backups]}{@{$bpc->{BackupFields}}} = split(/\t/);
186         }
187         close(BK_INFO);
188     }
189     close(LOCK);
190     return @Backups;
191 }
192
193 sub BackupInfoWrite
194 {
195     my($bpc, $host, @Backups) = @_;
196     local(*BK_INFO, *LOCK);
197     my($i);
198
199     flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
200     unlink("$bpc->{TopDir}/pc/$host/backups.old")
201                 if ( -f "$bpc->{TopDir}/pc/$host/backups.old" );
202     rename("$bpc->{TopDir}/pc/$host/backups",
203            "$bpc->{TopDir}/pc/$host/backups.old")
204                 if ( -f "$bpc->{TopDir}/pc/$host/backups" );
205     if ( open(BK_INFO, ">$bpc->{TopDir}/pc/$host/backups") ) {
206         binmode(BK_INFO);
207         for ( $i = 0 ; $i < @Backups ; $i++ ) {
208             my %b = %{$Backups[$i]};
209             printf(BK_INFO "%s\n", join("\t", @b{@{$bpc->{BackupFields}}}));
210         }
211         close(BK_INFO);
212     }
213     close(LOCK);
214 }
215
216 sub RestoreInfoRead
217 {
218     my($bpc, $host) = @_;
219     local(*RESTORE_INFO, *LOCK);
220     my(@Restores);
221
222     flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
223     if ( open(RESTORE_INFO, "$bpc->{TopDir}/pc/$host/restores") ) {
224         binmode(RESTORE_INFO);
225         while ( <RESTORE_INFO> ) {
226             s/[\n\r]+//;
227             next if ( !/^(\d+.*)/ );
228             $_ = $1;
229             @{$Restores[@Restores]}{@{$bpc->{RestoreFields}}} = split(/\t/);
230         }
231         close(RESTORE_INFO);
232     }
233     close(LOCK);
234     return @Restores;
235 }
236
237 sub RestoreInfoWrite
238 {
239     my($bpc, $host, @Restores) = @_;
240     local(*RESTORE_INFO, *LOCK);
241     my($i);
242
243     flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
244     unlink("$bpc->{TopDir}/pc/$host/restores.old")
245                 if ( -f "$bpc->{TopDir}/pc/$host/restores.old" );
246     rename("$bpc->{TopDir}/pc/$host/restores",
247            "$bpc->{TopDir}/pc/$host/restores.old")
248                 if ( -f "$bpc->{TopDir}/pc/$host/restores" );
249     if ( open(RESTORE_INFO, ">$bpc->{TopDir}/pc/$host/restores") ) {
250         binmode(RESTORE_INFO);
251         for ( $i = 0 ; $i < @Restores ; $i++ ) {
252             my %b = %{$Restores[$i]};
253             printf(RESTORE_INFO "%s\n",
254                         join("\t", @b{@{$bpc->{RestoreFields}}}));
255         }
256         close(RESTORE_INFO);
257     }
258     close(LOCK);
259 }
260
261 sub ArchiveInfoRead
262 {
263     my($bpc, $host) = @_;
264     local(*ARCHIVE_INFO, *LOCK);
265     my(@Archives);
266
267     flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
268     if ( open(ARCHIVE_INFO, "$bpc->{TopDir}/pc/$host/archives") ) {
269         binmode(ARCHIVE_INFO);
270         while ( <ARCHIVE_INFO> ) {
271             s/[\n\r]+//;
272             next if ( !/^(\d+.*)/ );
273             $_ = $1;
274             @{$Archives[@Archives]}{@{$bpc->{ArchiveFields}}} = split(/\t/);
275         }
276         close(ARCHIVE_INFO);
277     }
278     close(LOCK);
279     return @Archives;
280 }
281
282 sub ArchiveInfoWrite
283 {
284     my($bpc, $host, @Archives) = @_;
285     local(*ARCHIVE_INFO, *LOCK);
286     my($i);
287
288     flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
289     unlink("$bpc->{TopDir}/pc/$host/archives.old")
290                 if ( -f "$bpc->{TopDir}/pc/$host/archives.old" );
291     rename("$bpc->{TopDir}/pc/$host/archives",
292            "$bpc->{TopDir}/pc/$host/archives.old")
293                 if ( -f "$bpc->{TopDir}/pc/$host/archives" );
294     if ( open(ARCHIVE_INFO, ">$bpc->{TopDir}/pc/$host/archives") ) {
295         binmode(ARCHIVE_INFO);
296         for ( $i = 0 ; $i < @Archives ; $i++ ) {
297             my %b = %{$Archives[$i]};
298             printf(ARCHIVE_INFO "%s\n",
299                         join("\t", @b{@{$bpc->{ArchiveFields}}}));
300         }
301         close(ARCHIVE_INFO);
302     }
303     close(LOCK);
304 }
305
306 sub ConfigRead
307 {
308     my($bpc, $host) = @_;
309     my($ret, $mesg, $config, @configs);
310
311     $bpc->{Conf} = ();
312     push(@configs, "$bpc->{TopDir}/conf/config.pl");
313     push(@configs, "$bpc->{TopDir}/conf/$host.pl")
314             if ( $host ne "config" && -f "$bpc->{TopDir}/conf/$host.pl" );
315     push(@configs, "$bpc->{TopDir}/pc/$host/config.pl")
316             if ( defined($host) && -f "$bpc->{TopDir}/pc/$host/config.pl" );
317     foreach $config ( @configs ) {
318         %Conf = ();
319         if ( !defined($ret = do $config) && ($! || $@) ) {
320             $mesg = "Couldn't open $config: $!" if ( $! );
321             $mesg = "Couldn't execute $config: $@" if ( $@ );
322             $mesg =~ s/[\n\r]+//;
323             return $mesg;
324         }
325         %{$bpc->{Conf}} = ( %{$bpc->{Conf} || {}}, %Conf );
326     }
327     return if ( !defined($bpc->{Conf}{Language}) );
328     if ( defined($bpc->{Conf}{PerlModuleLoad}) ) {
329         #
330         # Load any user-specified perl modules.  This is for
331         # optional user-defined extensions.
332         #
333         $bpc->{Conf}{PerlModuleLoad} = [$bpc->{Conf}{PerlModuleLoad}]
334                     if ( ref($bpc->{Conf}{PerlModuleLoad}) ne "ARRAY" );
335         foreach my $module ( @{$bpc->{Conf}{PerlModuleLoad}} ) {
336             eval("use $module;");
337         }
338     }
339     my $langFile = "$bpc->{LibDir}/BackupPC/Lang/$bpc->{Conf}{Language}.pm";
340     if ( !defined($ret = do $langFile) && ($! || $@) ) {
341         $mesg = "Couldn't open language file $langFile: $!" if ( $! );
342         $mesg = "Couldn't execute language file $langFile: $@" if ( $@ );
343         $mesg =~ s/[\n\r]+//;
344         return $mesg;
345     }
346     $bpc->{Lang} = \%Lang;
347     return;
348 }
349
350 #
351 # Return the mtime of the config file
352 #
353 sub ConfigMTime
354 {
355     my($bpc) = @_;
356     return (stat("$bpc->{TopDir}/conf/config.pl"))[9];
357 }
358
359 #
360 # Returns information from the host file in $bpc->{TopDir}/conf/hosts.
361 # With no argument a ref to a hash of hosts is returned.  Each
362 # hash contains fields as specified in the hosts file.  With an
363 # argument a ref to a single hash is returned with information
364 # for just that host.
365 #
366 sub HostInfoRead
367 {
368     my($bpc, $host) = @_;
369     my(%hosts, @hdr, @fld);
370     local(*HOST_INFO);
371
372     if ( !open(HOST_INFO, "$bpc->{TopDir}/conf/hosts") ) {
373         print(STDERR $bpc->timeStamp,
374                      "Can't open $bpc->{TopDir}/conf/hosts\n");
375         return {};
376     }
377     binmode(HOST_INFO);
378     while ( <HOST_INFO> ) {
379         s/[\n\r]+//;
380         s/#.*//;
381         s/\s+$//;
382         next if ( /^\s*$/ || !/^([\w\.\\-]+\s+.*)/ );
383         #
384         # Split on white space, except if preceded by \
385         # using zero-width negative look-behind assertion
386         # (always wanted to use one of those).
387         #
388         @fld = split(/(?<!\\)\s+/, $1);
389         #
390         # Remove any \
391         #
392         foreach ( @fld ) {
393             s{\\(\s)}{$1}g;
394         }
395         if ( @hdr ) {
396             if ( defined($host) ) {
397                 next if ( lc($fld[0]) ne $host );
398                 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
399                 close(HOST_INFO);
400                 return \%hosts;
401             } else {
402                 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
403             }
404         } else {
405             @hdr = @fld;
406         }
407     }
408     close(HOST_INFO);
409     return \%hosts;
410 }
411
412 #
413 # Return the mtime of the hosts file
414 #
415 sub HostsMTime
416 {
417     my($bpc) = @_;
418     return (stat("$bpc->{TopDir}/conf/hosts"))[9];
419 }
420
421 #
422 # Stripped down from File::Path.  In particular we don't print
423 # many warnings and we try three times to delete each directory
424 # and file -- for some reason the original File::Path rmtree
425 # didn't always completely remove a directory tree on the NetApp.
426 #
427 # Warning: this routine changes the cwd.
428 #
429 sub RmTreeQuiet
430 {
431     my($bpc, $pwd, $roots) = @_;
432     my(@files, $root);
433
434     if ( defined($roots) && length($roots) ) {
435       $roots = [$roots] unless ref $roots;
436     } else {
437       print "RmTreeQuiet: No root path(s) specified\n";
438     }
439     chdir($pwd);
440     foreach $root (@{$roots}) {
441         $root = $1 if ( $root =~ m{(.*?)/*$} );
442         #
443         # Try first to simply unlink the file: this avoids an
444         # extra stat for every file.  If it fails (which it
445         # will for directories), check if it is a directory and
446         # then recurse.
447         #
448         if ( !unlink($root) ) {
449             if ( -d $root ) {
450                 my $d = DirHandle->new($root)
451                   or print "Can't read $pwd/$root: $!";
452                 @files = $d->read;
453                 $d->close;
454                 @files = grep $_!~/^\.{1,2}$/, @files;
455                 $bpc->RmTreeQuiet("$pwd/$root", \@files);
456                 chdir($pwd);
457                 rmdir($root) || rmdir($root);
458             } else {
459                 unlink($root) || unlink($root);
460             }
461         }
462     }
463 }
464
465 #
466 # Move a directory or file away for later deletion
467 #
468 sub RmTreeDefer
469 {
470     my($bpc, $trashDir, $file) = @_;
471     my($i, $f);
472
473     return if ( !-e $file );
474     mkpath($trashDir, 0, 0777) if ( !-d $trashDir );
475     for ( $i = 0 ; $i < 1000 ; $i++ ) {
476         $f = sprintf("%s/%d_%d_%d", $trashDir, time, $$, $i);
477         next if ( -e $f );
478         return if ( rename($file, $f) );
479     }
480     # shouldn't get here, but might if you tried to call this
481     # across file systems.... just remove the tree right now.
482     if ( $file =~ /(.*)\/([^\/]*)/ ) {
483         my($d) = $1;
484         my($f) = $2;
485         my($cwd) = Cwd::fastcwd();
486         $cwd = $1 if ( $cwd =~ /(.*)/ );
487         $bpc->RmTreeQuiet($d, $f);
488         chdir($cwd) if ( $cwd );
489     }
490 }
491
492 #
493 # Empty the trash directory.  Returns 0 if it did nothing, 1 if it
494 # did something, -1 if it failed to remove all the files.
495 #
496 sub RmTreeTrashEmpty
497 {
498     my($bpc, $trashDir) = @_;
499     my(@files);
500     my($cwd) = Cwd::fastcwd();
501
502     $cwd = $1 if ( $cwd =~ /(.*)/ );
503     return if ( !-d $trashDir );
504     my $d = DirHandle->new($trashDir) or carp "Can't read $trashDir: $!";
505     @files = $d->read;
506     $d->close;
507     @files = grep $_!~/^\.{1,2}$/, @files;
508     return 0 if ( !@files );
509     $bpc->RmTreeQuiet($trashDir, \@files);
510     foreach my $f ( @files ) {
511         return -1 if ( -e $f );
512     }
513     chdir($cwd) if ( $cwd );
514     return 1;
515 }
516
517 #
518 # Open a connection to the server.  Returns an error string on failure.
519 # Returns undef on success.
520 #
521 sub ServerConnect
522 {
523     my($bpc, $host, $port, $justConnect) = @_;
524     local(*FH);
525
526     return if ( defined($bpc->{ServerFD}) );
527     #
528     # First try the unix-domain socket
529     #
530     my $sockFile = "$bpc->{TopDir}/log/BackupPC.sock";
531     socket(*FH, PF_UNIX, SOCK_STREAM, 0)     || return "unix socket: $!";
532     if ( !connect(*FH, sockaddr_un($sockFile)) ) {
533         my $err = "unix connect: $!";
534         close(*FH);
535         if ( $port > 0 ) {
536             my $proto = getprotobyname('tcp');
537             my $iaddr = inet_aton($host)     || return "unknown host $host";
538             my $paddr = sockaddr_in($port, $iaddr);
539
540             socket(*FH, PF_INET, SOCK_STREAM, $proto)
541                                              || return "inet socket: $!";
542             connect(*FH, $paddr)             || return "inet connect: $!";
543         } else {
544             return $err;
545         }
546     }
547     my($oldFH) = select(*FH); $| = 1; select($oldFH);
548     $bpc->{ServerFD} = *FH;
549     return if ( $justConnect );
550     #
551     # Read the seed that we need for our MD5 message digest.  See
552     # ServerMesg below.
553     #
554     sysread($bpc->{ServerFD}, $bpc->{ServerSeed}, 1024);
555     $bpc->{ServerMesgCnt} = 0;
556     return;
557 }
558
559 #
560 # Check that the server connection is still ok
561 #
562 sub ServerOK
563 {
564     my($bpc) = @_;
565
566     return 0 if ( !defined($bpc->{ServerFD}) );
567     vec(my $FDread, fileno($bpc->{ServerFD}), 1) = 1;
568     my $ein = $FDread;
569     return 0 if ( select(my $rout = $FDread, undef, $ein, 0.0) < 0 );
570     return 1 if ( !vec($rout, fileno($bpc->{ServerFD}), 1) );
571 }
572
573 #
574 # Disconnect from the server
575 #
576 sub ServerDisconnect
577 {
578     my($bpc) = @_;
579     return if ( !defined($bpc->{ServerFD}) );
580     close($bpc->{ServerFD});
581     delete($bpc->{ServerFD});
582 }
583
584 #
585 # Sends a message to the server and returns with the reply.
586 #
587 # To avoid possible attacks via the TCP socket interface, every client
588 # message is protected by an MD5 digest. The MD5 digest includes four
589 # items:
590 #   - a seed that is sent to us when we first connect
591 #   - a sequence number that increments for each message
592 #   - a shared secret that is stored in $Conf{ServerMesgSecret}
593 #   - the message itself.
594 # The message is sent in plain text preceded by the MD5 digest. A
595 # snooper can see the plain-text seed sent by BackupPC and plain-text
596 # message, but cannot construct a valid MD5 digest since the secret in
597 # $Conf{ServerMesgSecret} is unknown. A replay attack is not possible
598 # since the seed changes on a per-connection and per-message basis.
599 #
600 sub ServerMesg
601 {
602     my($bpc, $mesg) = @_;
603     return if ( !defined(my $fh = $bpc->{ServerFD}) );
604     my $md5 = Digest::MD5->new;
605     $md5->add($bpc->{ServerSeed} . $bpc->{ServerMesgCnt}
606             . $bpc->{Conf}{ServerMesgSecret} . $mesg);
607     print($fh $md5->b64digest . " $mesg\n");
608     $bpc->{ServerMesgCnt}++;
609     return <$fh>;
610 }
611
612 #
613 # Do initialization for child processes
614 #
615 sub ChildInit
616 {
617     my($bpc) = @_;
618     close(STDERR);
619     open(STDERR, ">&STDOUT");
620     select(STDERR); $| = 1;
621     select(STDOUT); $| = 1;
622     $ENV{PATH} = $bpc->{Conf}{MyPath};
623 }
624
625 #
626 # Compute the MD5 digest of a file.  For efficiency we don't
627 # use the whole file for big files:
628 #   - for files <= 256K we use the file size and the whole file.
629 #   - for files <= 1M we use the file size, the first 128K and
630 #     the last 128K.
631 #   - for files > 1M, we use the file size, the first 128K and
632 #     the 8th 128K (ie: the 128K up to 1MB).
633 # See the documentation for a discussion of the tradeoffs in
634 # how much data we use and how many collisions we get.
635 #
636 # Returns the MD5 digest (a hex string) and the file size.
637 #
638 sub File2MD5
639 {
640     my($bpc, $md5, $name) = @_;
641     my($data, $fileSize);
642     local(*N);
643
644     $fileSize = (stat($name))[7];
645     return ("", -1) if ( !-f _ );
646     $name = $1 if ( $name =~ /(.*)/ );
647     return ("", 0) if ( $fileSize == 0 );
648     return ("", -1) if ( !open(N, $name) );
649     binmode(N);
650     $md5->reset();
651     $md5->add($fileSize);
652     if ( $fileSize > 262144 ) {
653         #
654         # read the first and last 131072 bytes of the file,
655         # up to 1MB.
656         #
657         my $seekPosn = ($fileSize > 1048576 ? 1048576 : $fileSize) - 131072;
658         $md5->add($data) if ( sysread(N, $data, 131072) );
659         $md5->add($data) if ( sysseek(N, $seekPosn, 0)
660                                 && sysread(N, $data, 131072) );
661     } else {
662         #
663         # read the whole file
664         #
665         $md5->add($data) if ( sysread(N, $data, $fileSize) );
666     }
667     close(N);
668     return ($md5->hexdigest, $fileSize);
669 }
670
671 #
672 # Compute the MD5 digest of a buffer (string).  For efficiency we don't
673 # use the whole string for big strings:
674 #   - for files <= 256K we use the file size and the whole file.
675 #   - for files <= 1M we use the file size, the first 128K and
676 #     the last 128K.
677 #   - for files > 1M, we use the file size, the first 128K and
678 #     the 8th 128K (ie: the 128K up to 1MB).
679 # See the documentation for a discussion of the tradeoffs in
680 # how much data we use and how many collisions we get.
681 #
682 # Returns the MD5 digest (a hex string).
683 #
684 sub Buffer2MD5
685 {
686     my($bpc, $md5, $fileSize, $dataRef) = @_;
687
688     $md5->reset();
689     $md5->add($fileSize);
690     if ( $fileSize > 262144 ) {
691         #
692         # add the first and last 131072 bytes of the string,
693         # up to 1MB.
694         #
695         my $seekPosn = ($fileSize > 1048576 ? 1048576 : $fileSize) - 131072;
696         $md5->add(substr($$dataRef, 0, 131072));
697         $md5->add(substr($$dataRef, $seekPosn, 131072));
698     } else {
699         #
700         # add the whole string
701         #
702         $md5->add($$dataRef);
703     }
704     return $md5->hexdigest;
705 }
706
707 #
708 # Given an MD5 digest $d and a compress flag, return the full
709 # path in the pool.
710 #
711 sub MD52Path
712 {
713     my($bpc, $d, $compress, $poolDir) = @_;
714
715     return if ( $d !~ m{(.)(.)(.)(.*)} );
716     $poolDir = ($compress ? $bpc->{CPoolDir} : $bpc->{PoolDir})
717                     if ( !defined($poolDir) );
718     return "$poolDir/$1/$2/$3/$1$2$3$4";
719 }
720
721 #
722 # For each file, check if the file exists in $bpc->{TopDir}/pool.
723 # If so, remove the file and make a hardlink to the file in
724 # the pool.  Otherwise, if the newFile flag is set, make a
725 # hardlink in the pool to the new file.
726 #
727 # Returns 0 if a link should be made to a new file (ie: when the file
728 #    is a new file but the newFile flag is 0).
729 # Returns 1 if a link to an existing file is made,
730 # Returns 2 if a link to a new file is made (only if $newFile is set)
731 # Returns negative on error.
732 #
733 sub MakeFileLink
734 {
735     my($bpc, $name, $d, $newFile, $compress) = @_;
736     my($i, $rawFile);
737
738     return -1 if ( !-f $name );
739     for ( $i = -1 ; ; $i++ ) {
740         return -2 if ( !defined($rawFile = $bpc->MD52Path($d, $compress)) );
741         $rawFile .= "_$i" if ( $i >= 0 );
742         if ( -f $rawFile ) {
743             if ( !compare($name, $rawFile) ) {
744                 unlink($name);
745                 return -3 if ( !link($rawFile, $name) );
746                 return 1;
747             }
748         } elsif ( $newFile && -f $name && (stat($name))[3] == 1 ) {
749             my($newDir);
750             ($newDir = $rawFile) =~ s{(.*)/.*}{$1};
751             mkpath($newDir, 0, 0777) if ( !-d $newDir );
752             return -4 if ( !link($name, $rawFile) );
753             return 2;
754         } else {
755             return 0;
756         }
757     }
758 }
759
760 sub CheckHostAlive
761 {
762     my($bpc, $host) = @_;
763     my($s, $pingCmd, $ret);
764
765     #
766     # Return success if the ping cmd is undefined or empty.
767     #
768     if ( $bpc->{Conf}{PingCmd} eq "" ) {
769         print(STDERR "CheckHostAlive: return ok because \$Conf{PingCmd}"
770                    . " is empty\n") if ( $bpc->{verbose} );
771         return 0;
772     }
773
774     my $args = {
775         pingPath => $bpc->{Conf}{PingPath},
776         host     => $host,
777     };
778     $pingCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{PingCmd}, $args);
779
780     #
781     # Do a first ping in case the PC needs to wakeup
782     #
783     $s = $bpc->cmdSystemOrEval($pingCmd, undef, $args);
784     if ( $? ) {
785         print(STDERR "CheckHostAlive: first ping failed ($?, $!)\n")
786                         if ( $bpc->{verbose} );
787         return -1;
788     }
789
790     #
791     # Do a second ping and get the round-trip time in msec
792     #
793     $s = $bpc->cmdSystemOrEval($pingCmd, undef, $args);
794     if ( $? ) {
795         print(STDERR "CheckHostAlive: second ping failed ($?, $!)\n")
796                         if ( $bpc->{verbose} );
797         return -1;
798     }
799     if ( $s =~ /time=([\d\.]+)\s*ms/i ) {
800         $ret = $1;
801     } elsif ( $s =~ /time=([\d\.]+)\s*usec/i ) {
802         $ret =  $1/1000;
803     } else {
804         print(STDERR "CheckHostAlive: can't extract round-trip time"
805                    . " (not fatal)\n") if ( $bpc->{verbose} );
806         $ret = 0;
807     }
808     print(STDERR "CheckHostAlive: returning $ret\n") if ( $bpc->{verbose} );
809     return $ret;
810 }
811
812 sub CheckFileSystemUsage
813 {
814     my($bpc) = @_;
815     my($topDir) = $bpc->{TopDir};
816     my($s, $dfCmd);
817
818     return 0 if ( $bpc->{Conf}{DfCmd} eq "" );
819     my $args = {
820         dfPath   => $bpc->{Conf}{DfPath},
821         topDir   => $bpc->{TopDir},
822     };
823     $dfCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{DfCmd}, $args);
824     $s = $bpc->cmdSystemOrEval($dfCmd, undef, $args);
825     return 0 if ( $? || $s !~ /(\d+)%/s );
826     return $1;
827 }
828
829 #
830 # Given an IP address, return the host name and user name via
831 # NetBios.
832 #
833 sub NetBiosInfoGet
834 {
835     my($bpc, $host) = @_;
836     my($netBiosHostName, $netBiosUserName);
837     my($s, $nmbCmd);
838
839     #
840     # Skip NetBios check if NmbLookupCmd is emtpy
841     #
842     if ( $bpc->{Conf}{NmbLookupCmd} eq "" ) {
843         print(STDERR "NetBiosInfoGet: return $host because \$Conf{NmbLookupCmd}"
844                    . " is empty\n") if ( $bpc->{verbose} );
845         return ($host, undef);
846     }
847
848     my $args = {
849         nmbLookupPath => $bpc->{Conf}{NmbLookupPath},
850         host          => $host,
851     };
852     $nmbCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{NmbLookupCmd}, $args);
853     foreach ( split(/[\n\r]+/, $bpc->cmdSystemOrEval($nmbCmd, undef, $args)) ) {
854         next if ( !/^\s*([\w\s-]+?)\s*<(\w{2})\> - .*<ACTIVE>/i );
855         $netBiosHostName ||= $1 if ( $2 eq "00" );  # host is first 00
856         $netBiosUserName   = $1 if ( $2 eq "03" );  # user is last 03
857     }
858     if ( !defined($netBiosHostName) ) {
859         print(STDERR "NetBiosInfoGet: failed: can't parse return string\n")
860                         if ( $bpc->{verbose} );
861         return;
862     }
863     $netBiosHostName = lc($netBiosHostName);
864     $netBiosUserName = lc($netBiosUserName);
865     print(STDERR "NetBiosInfoGet: success, returning host $netBiosHostName,"
866                . " user $netBiosUserName\n") if ( $bpc->{verbose} );
867     return ($netBiosHostName, $netBiosUserName);
868 }
869
870 #
871 # Given a NetBios name lookup the IP address via NetBios.
872 # In the case of a host returning multiple interfaces we
873 # return the first IP address that matches the subnet mask.
874 # If none match the subnet mask (or nmblookup doesn't print
875 # the subnet mask) then just the first IP address is returned.
876 #
877 sub NetBiosHostIPFind
878 {
879     my($bpc, $host) = @_;
880     my($netBiosHostName, $netBiosUserName);
881     my($s, $nmbCmd, $subnet, $ipAddr, $firstIpAddr);
882
883     #
884     # Skip NetBios lookup if NmbLookupFindHostCmd is emtpy
885     #
886     if ( $bpc->{Conf}{NmbLookupFindHostCmd} eq "" ) {
887         print(STDERR "NetBiosHostIPFind: return $host because"
888             . " \$Conf{NmbLookupFindHostCmd} is empty\n")
889                 if ( $bpc->{verbose} );
890         return $host;
891     }
892
893     my $args = {
894         nmbLookupPath => $bpc->{Conf}{NmbLookupPath},
895         host          => $host,
896     };
897     $nmbCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{NmbLookupFindHostCmd}, $args);
898     foreach my $resp ( split(/[\n\r]+/, $bpc->cmdSystemOrEval($nmbCmd, undef,
899                                                               $args) ) ) {
900         if ( $resp =~ /querying\s+\Q$host\E\s+on\s+(\d+\.\d+\.\d+\.\d+)/i ) {
901             $subnet = $1;
902             $subnet = $1 if ( $subnet =~ /^(.*?)(\.255)+$/ );
903         } elsif ( $resp =~ /^\s*(\d+\.\d+\.\d+\.\d+)\s+\Q$host/ ) {
904             my $ip = $1;
905             $firstIpAddr = $ip if ( !defined($firstIpAddr) );
906             $ipAddr      = $ip if ( !defined($ipAddr) && $ip =~ /^\Q$subnet/ );
907         }
908     }
909     $ipAddr = $firstIpAddr if ( !defined($ipAddr) );
910     if ( defined($ipAddr) ) {
911         print(STDERR "NetBiosHostIPFind: found IP address $ipAddr for"
912                    . " host $host\n") if ( $bpc->{verbose} );
913         return $ipAddr;
914     } else {
915         print(STDERR "NetBiosHostIPFind: couldn't find IP address for"
916                    . " host $host\n") if ( $bpc->{verbose} );
917         return;
918     }
919 }
920
921 sub fileNameEltMangle
922 {
923     my($bpc, $name) = @_;
924
925     return "" if ( $name eq "" );
926     $name =~ s{([%/\n\r])}{sprintf("%%%02x", ord($1))}eg;
927     return "f$name";
928 }
929
930 #
931 # We store files with every name preceded by "f".  This
932 # avoids possible name conflicts with other information
933 # we store in the same directories (eg: attribute info).
934 # The process of turning a normal path into one with each
935 # node prefixed with "f" is called mangling.
936 #
937 sub fileNameMangle
938 {
939     my($bpc, $name) = @_;
940
941     $name =~ s{/([^/]+)}{"/" . $bpc->fileNameEltMangle($1)}eg;
942     $name =~ s{^([^/]+)}{$bpc->fileNameEltMangle($1)}eg;
943     return $name;
944 }
945
946 #
947 # This undoes FileNameMangle
948 #
949 sub fileNameUnmangle
950 {
951     my($bpc, $name) = @_;
952
953     $name =~ s{/f}{/}g;
954     $name =~ s{^f}{};
955     $name =~ s{%(..)}{chr(hex($1))}eg;
956     return $name;
957 }
958
959 #
960 # Escape shell meta-characters with backslashes.
961 # This should be applied to each argument seperately, not an
962 # entire shell command.
963 #
964 sub shellEscape
965 {
966     my($bpc, $cmd) = @_;
967
968     $cmd =~ s/([][;&()<>{}|^\n\r\t *\$\\'"`?])/\\$1/g;
969     return $cmd;
970 }
971
972 #
973 # For printing exec commands (which don't use a shell) so they look like
974 # a valid shell command this function should be called with the exec
975 # args.  The shell command string is returned.
976 #
977 sub execCmd2ShellCmd
978 {
979     my($bpc, @args) = @_;
980     my $str;
981
982     foreach my $a ( @args ) {
983         $str .= " " if ( $str ne "" );
984         $str .= $bpc->shellEscape($a);
985     }
986     return $str;
987 }
988
989 #
990 # Do a URI-style escape to protect/encode special characters
991 #
992 sub uriEsc
993 {
994     my($bpc, $s) = @_;
995     $s =~ s{([^\w.\/-])}{sprintf("%%%02X", ord($1));}eg;
996     return $s;
997 }
998
999 #
1000 # Do a URI-style unescape to restore special characters
1001 #
1002 sub uriUnesc
1003 {
1004     my($bpc, $s) = @_;
1005     $s =~ s{%(..)}{chr(hex($1))}eg;
1006     return $s;
1007 }
1008
1009 #
1010 # Do variable substitution prior to execution of a command.
1011 #
1012 sub cmdVarSubstitute
1013 {
1014     my($bpc, $template, $vars) = @_;
1015     my(@cmd);
1016
1017     #
1018     # Return without any substitution if the first entry starts with "&",
1019     # indicating this is perl code.
1020     #
1021     if ( (ref($template) eq "ARRAY" ? $template->[0] : $template) =~ /^\&/ ) {
1022         return $template;
1023     }
1024     if ( ref($template) ne "ARRAY" ) {
1025         #
1026         # Split at white space, except if escaped by \
1027         #
1028         $template = [split(/(?<!\\)\s+/, $template)];
1029         #
1030         # Remove the \ that escaped white space.
1031         #
1032         foreach ( @$template ) {
1033             s{\\(\s)}{$1}g;
1034         }
1035     }
1036     #
1037     # Merge variables into @tarClientCmd
1038     #
1039     foreach my $arg ( @$template ) {
1040         #
1041         # Replace scalar variables first
1042         #
1043         $arg =~ s{\$(\w+)(\+?)}{
1044             exists($vars->{$1}) && ref($vars->{$1}) ne "ARRAY"
1045                 ? ($2 eq "+" ? $bpc->shellEscape($vars->{$1}) : $vars->{$1})
1046                 : "\$$1$2"
1047         }eg;
1048         #
1049         # Now replicate any array arguments; this just works for just one
1050         # array var in each argument.
1051         #
1052         if ( $arg =~ m{(.*)\$(\w+)(\+?)(.*)} && ref($vars->{$2}) eq "ARRAY" ) {
1053             my $pre  = $1;
1054             my $var  = $2;
1055             my $esc  = $3;
1056             my $post = $4;
1057             foreach my $v ( @{$vars->{$var}} ) {
1058                 $v = $bpc->shellEscape($v) if ( $esc eq "+" );
1059                 push(@cmd, "$pre$v$post");
1060             }
1061         } else {
1062             push(@cmd, $arg);
1063         }
1064     }
1065     return \@cmd;
1066 }
1067
1068 #
1069 # Exec or eval a command.  $cmd is either a string on an array ref.
1070 #
1071 # @args are optional arguments for the eval() case; they are not used
1072 # for exec().
1073 #
1074 sub cmdExecOrEval
1075 {
1076     my($bpc, $cmd, @args) = @_;
1077     
1078     if ( (ref($cmd) eq "ARRAY" ? $cmd->[0] : $cmd) =~ /^\&/ ) {
1079         $cmd = join(" ", $cmd) if ( ref($cmd) eq "ARRAY" );
1080         print(STDERR "cmdExecOrEval: about to eval perl code $cmd\n")
1081                         if ( $bpc->{verbose} );
1082         eval($cmd);
1083         print(STDERR "Perl code fragment for exec shouldn't return!!\n");
1084         exit(1);
1085     } else {
1086         $cmd = [split(/\s+/, $cmd)] if ( ref($cmd) ne "ARRAY" );
1087         print(STDERR "cmdExecOrEval: about to exec ",
1088               $bpc->execCmd2ShellCmd(@$cmd), "\n")
1089                         if ( $bpc->{verbose} );
1090         exec(map { m/(.*)/ } @$cmd);            # untaint
1091         print(STDERR "Exec failed for @$cmd\n");
1092         exit(1);
1093     }
1094 }
1095
1096 #
1097 # System or eval a command.  $cmd is either a string on an array ref.
1098 # $stdoutCB is a callback for output generated by the command.  If it
1099 # is undef then output is returned.  If it is a code ref then the function
1100 # is called with each piece of output as an argument.  If it is a scalar
1101 # ref the output is appended to this variable.
1102 #
1103 # @args are optional arguments for the eval() case; they are not used
1104 # for system().
1105 #
1106 # Also, $? should be set when the CHILD pipe is closed.
1107 #
1108 sub cmdSystemOrEval
1109 {
1110     my($bpc, $cmd, $stdoutCB, @args) = @_;
1111     my($pid, $out, $allOut);
1112     local(*CHILD);
1113     
1114     if ( (ref($cmd) eq "ARRAY" ? $cmd->[0] : $cmd) =~ /^\&/ ) {
1115         $cmd = join(" ", $cmd) if ( ref($cmd) eq "ARRAY" );
1116         print(STDERR "cmdSystemOrEval: about to eval perl code $cmd\n")
1117                         if ( $bpc->{verbose} );
1118         $out = eval($cmd);
1119         $$stdoutCB .= $out if ( ref($stdoutCB) eq 'SCALAR' );
1120         &$stdoutCB($out)   if ( ref($stdoutCB) eq 'CODE' );
1121         print(STDERR "cmdSystemOrEval: finished: got output $out\n")
1122                         if ( $bpc->{verbose} );
1123         return $out        if ( !defined($stdoutCB) );
1124         return;
1125     } else {
1126         $cmd = [split(/\s+/, $cmd)] if ( ref($cmd) ne "ARRAY" );
1127         print(STDERR "cmdSystemOrEval: about to system ",
1128               $bpc->execCmd2ShellCmd(@$cmd), "\n")
1129                         if ( $bpc->{verbose} );
1130         if ( !defined($pid = open(CHILD, "-|")) ) {
1131             my $err = "Can't fork to run @$cmd\n";
1132             $? = 1;
1133             $$stdoutCB .= $err if ( ref($stdoutCB) eq 'SCALAR' );
1134             &$stdoutCB($err)   if ( ref($stdoutCB) eq 'CODE' );
1135             return $err        if ( !defined($stdoutCB) );
1136             return;
1137         }
1138         binmode(CHILD);
1139         if ( !$pid ) {
1140             #
1141             # This is the child
1142             #
1143             close(STDERR);
1144             open(STDERR, ">&STDOUT");
1145             exec(map { m/(.*)/ } @$cmd);                # untaint
1146             print("Exec of @$cmd failed\n");
1147             exit(1);
1148         }
1149         #
1150         # The parent gathers the output from the child
1151         #
1152         while ( <CHILD> ) {
1153             $$stdoutCB .= $_ if ( ref($stdoutCB) eq 'SCALAR' );
1154             &$stdoutCB($_)   if ( ref($stdoutCB) eq 'CODE' );
1155             $out .= $_       if ( !defined($stdoutCB) );
1156             $allOut .= $_    if ( $bpc->{verbose} );
1157         }
1158         $? = 0;
1159         close(CHILD);
1160     }
1161     print(STDERR "cmdSystemOrEval: finished: got output $allOut\n")
1162                         if ( $bpc->{verbose} );
1163     return $out;
1164 }
1165
1166 1;