* Failed dumps now cleanup correctly, deleting in-progress file
[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 8 Feb 2004.
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 use Config;
52
53 sub new
54 {
55     my $class = shift;
56     my($topDir, $installDir, $noUserCheck) = @_;
57
58     my $bpc = bless {
59         TopDir  => $topDir || '/data/BackupPC',
60         BinDir  => $installDir || '/usr/local/BackupPC',
61         LibDir  => $installDir || '/usr/local/BackupPC',
62         Version => '2.1.0_CVS',
63         BackupFields => [qw(
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
69                 )],
70         RestoreFields => [qw(
71                     num startTime endTime result errorMsg nFiles size
72                     tarCreateErrs xferErrs
73                 )],
74         ArchiveFields => [qw(
75                     num startTime endTime result errorMsg
76                 )],
77     }, $class;
78     $bpc->{BinDir} .= "/bin";
79     $bpc->{LibDir} .= "/lib";
80     #
81     # Clean up %ENV and setup other variables.
82     #
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");
88         return;
89     }
90     #
91     # Verify we are running as the correct user
92     #
93     if ( !$noUserCheck
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");
98         return;
99     }
100     return $bpc;
101 }
102
103 sub TopDir
104 {
105     my($bpc) = @_;
106     return $bpc->{TopDir};
107 }
108
109 sub BinDir
110 {
111     my($bpc) = @_;
112     return $bpc->{BinDir};
113 }
114
115 sub Version
116 {
117     my($bpc) = @_;
118     return $bpc->{Version};
119 }
120
121 sub Conf
122 {
123     my($bpc) = @_;
124     return %{$bpc->{Conf}};
125 }
126
127 sub Lang
128 {
129     my($bpc) = @_;
130     return $bpc->{Lang};
131 }
132
133 sub adminJob
134 {
135     return " admin ";
136 }
137
138 sub trashJob
139 {
140     return " trashClean ";
141 }
142
143 sub ConfValue
144 {
145     my($bpc, $param) = @_;
146
147     return $bpc->{Conf}{$param};
148 }
149
150 sub verbose
151 {
152     my($bpc, $param) = @_;
153
154     $bpc->{verbose} = $param if ( defined($param) );
155     return $bpc->{verbose};
156 }
157
158 sub sigName2num
159 {
160     my($bpc, $sig) = @_;
161
162     if ( !defined($bpc->{SigName2Num}) ) {
163         my $i = 0;
164         foreach my $name ( split(' ', $Config{sig_name}) ) {
165             $bpc->{SigName2Num}{$name} = $i;
166             $i++;
167         }
168     }
169     return $bpc->{SigName2Num}{$sig};
170 }
171
172 #
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
176 #
177 sub timeStamp
178 {
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 ? "" : " ");
185 }
186
187 sub BackupInfoRead
188 {
189     my($bpc, $host) = @_;
190     local(*BK_INFO, *LOCK);
191     my(@Backups);
192
193     flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
194     if ( open(BK_INFO, "$bpc->{TopDir}/pc/$host/backups") ) {
195         binmode(BK_INFO);
196         while ( <BK_INFO> ) {
197             s/[\n\r]+//;
198             next if ( !/^(\d+\t(incr|full|partial)[\d\t]*$)/ );
199             $_ = $1;
200             @{$Backups[@Backups]}{@{$bpc->{BackupFields}}} = split(/\t/);
201         }
202         close(BK_INFO);
203     }
204     close(LOCK);
205     return @Backups;
206 }
207
208 sub BackupInfoWrite
209 {
210     my($bpc, $host, @Backups) = @_;
211     local(*BK_INFO, *LOCK);
212     my($i);
213
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") ) {
221         binmode(BK_INFO);
222         for ( $i = 0 ; $i < @Backups ; $i++ ) {
223             my %b = %{$Backups[$i]};
224             printf(BK_INFO "%s\n", join("\t", @b{@{$bpc->{BackupFields}}}));
225         }
226         close(BK_INFO);
227     }
228     close(LOCK);
229 }
230
231 sub RestoreInfoRead
232 {
233     my($bpc, $host) = @_;
234     local(*RESTORE_INFO, *LOCK);
235     my(@Restores);
236
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> ) {
241             s/[\n\r]+//;
242             next if ( !/^(\d+.*)/ );
243             $_ = $1;
244             @{$Restores[@Restores]}{@{$bpc->{RestoreFields}}} = split(/\t/);
245         }
246         close(RESTORE_INFO);
247     }
248     close(LOCK);
249     return @Restores;
250 }
251
252 sub RestoreInfoWrite
253 {
254     my($bpc, $host, @Restores) = @_;
255     local(*RESTORE_INFO, *LOCK);
256     my($i);
257
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}}}));
270         }
271         close(RESTORE_INFO);
272     }
273     close(LOCK);
274 }
275
276 sub ArchiveInfoRead
277 {
278     my($bpc, $host) = @_;
279     local(*ARCHIVE_INFO, *LOCK);
280     my(@Archives);
281
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> ) {
286             s/[\n\r]+//;
287             next if ( !/^(\d+.*)/ );
288             $_ = $1;
289             @{$Archives[@Archives]}{@{$bpc->{ArchiveFields}}} = split(/\t/);
290         }
291         close(ARCHIVE_INFO);
292     }
293     close(LOCK);
294     return @Archives;
295 }
296
297 sub ArchiveInfoWrite
298 {
299     my($bpc, $host, @Archives) = @_;
300     local(*ARCHIVE_INFO, *LOCK);
301     my($i);
302
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}}}));
315         }
316         close(ARCHIVE_INFO);
317     }
318     close(LOCK);
319 }
320
321 sub ConfigRead
322 {
323     my($bpc, $host) = @_;
324     my($ret, $mesg, $config, @configs);
325
326     $bpc->{Conf} = ();
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 ) {
333         %Conf = ();
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]+//;
338             return $mesg;
339         }
340         %{$bpc->{Conf}} = ( %{$bpc->{Conf} || {}}, %Conf );
341     }
342     return if ( !defined($bpc->{Conf}{Language}) );
343     if ( defined($bpc->{Conf}{PerlModuleLoad}) ) {
344         #
345         # Load any user-specified perl modules.  This is for
346         # optional user-defined extensions.
347         #
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;");
352         }
353     }
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]+//;
359         return $mesg;
360     }
361     $bpc->{Lang} = \%Lang;
362     return;
363 }
364
365 #
366 # Return the mtime of the config file
367 #
368 sub ConfigMTime
369 {
370     my($bpc) = @_;
371     return (stat("$bpc->{TopDir}/conf/config.pl"))[9];
372 }
373
374 #
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.
380 #
381 sub HostInfoRead
382 {
383     my($bpc, $host) = @_;
384     my(%hosts, @hdr, @fld);
385     local(*HOST_INFO);
386
387     if ( !open(HOST_INFO, "$bpc->{TopDir}/conf/hosts") ) {
388         print(STDERR $bpc->timeStamp,
389                      "Can't open $bpc->{TopDir}/conf/hosts\n");
390         return {};
391     }
392     binmode(HOST_INFO);
393     while ( <HOST_INFO> ) {
394         s/[\n\r]+//;
395         s/#.*//;
396         s/\s+$//;
397         next if ( /^\s*$/ || !/^([\w\.\\-]+\s+.*)/ );
398         #
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).
402         #
403         @fld = split(/(?<!\\)\s+/, $1);
404         #
405         # Remove any \
406         #
407         foreach ( @fld ) {
408             s{\\(\s)}{$1}g;
409         }
410         if ( @hdr ) {
411             if ( defined($host) ) {
412                 next if ( lc($fld[0]) ne $host );
413                 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
414                 close(HOST_INFO);
415                 return \%hosts;
416             } else {
417                 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
418             }
419         } else {
420             @hdr = @fld;
421         }
422     }
423     close(HOST_INFO);
424     return \%hosts;
425 }
426
427 #
428 # Return the mtime of the hosts file
429 #
430 sub HostsMTime
431 {
432     my($bpc) = @_;
433     return (stat("$bpc->{TopDir}/conf/hosts"))[9];
434 }
435
436 #
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.
441 #
442 # Warning: this routine changes the cwd.
443 #
444 sub RmTreeQuiet
445 {
446     my($bpc, $pwd, $roots) = @_;
447     my(@files, $root);
448
449     if ( defined($roots) && length($roots) ) {
450       $roots = [$roots] unless ref $roots;
451     } else {
452       print(STDERR "RmTreeQuiet: No root path(s) specified\n");
453     }
454     chdir($pwd);
455     foreach $root (@{$roots}) {
456         $root = $1 if ( $root =~ m{(.*?)/*$} );
457         #
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
461         # then recurse.
462         #
463         if ( !unlink($root) ) {
464             if ( -d $root ) {
465                 my $d = DirHandle->new($root)
466                   or print(STDERR "Can't read $pwd/$root: $!");
467                 @files = $d->read;
468                 $d->close;
469                 @files = grep $_!~/^\.{1,2}$/, @files;
470                 $bpc->RmTreeQuiet("$pwd/$root", \@files);
471                 chdir($pwd);
472                 rmdir($root) || rmdir($root);
473             } else {
474                 unlink($root) || unlink($root);
475             }
476         }
477     }
478 }
479
480 #
481 # Move a directory or file away for later deletion
482 #
483 sub RmTreeDefer
484 {
485     my($bpc, $trashDir, $file) = @_;
486     my($i, $f);
487
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);
492         next if ( -e $f );
493         return if ( rename($file, $f) );
494     }
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 =~ /(.*)\/([^\/]*)/ ) {
498         my($d) = $1;
499         my($f) = $2;
500         my($cwd) = Cwd::fastcwd();
501         $cwd = $1 if ( $cwd =~ /(.*)/ );
502         $bpc->RmTreeQuiet($d, $f);
503         chdir($cwd) if ( $cwd );
504     }
505 }
506
507 #
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.
510 #
511 sub RmTreeTrashEmpty
512 {
513     my($bpc, $trashDir) = @_;
514     my(@files);
515     my($cwd) = Cwd::fastcwd();
516
517     $cwd = $1 if ( $cwd =~ /(.*)/ );
518     return if ( !-d $trashDir );
519     my $d = DirHandle->new($trashDir) or carp "Can't read $trashDir: $!";
520     @files = $d->read;
521     $d->close;
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 );
527     }
528     chdir($cwd) if ( $cwd );
529     return 1;
530 }
531
532 #
533 # Open a connection to the server.  Returns an error string on failure.
534 # Returns undef on success.
535 #
536 sub ServerConnect
537 {
538     my($bpc, $host, $port, $justConnect) = @_;
539     local(*FH);
540
541     return if ( defined($bpc->{ServerFD}) );
542     #
543     # First try the unix-domain socket
544     #
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: $!";
549         close(*FH);
550         if ( $port > 0 ) {
551             my $proto = getprotobyname('tcp');
552             my $iaddr = inet_aton($host)     || return "unknown host $host";
553             my $paddr = sockaddr_in($port, $iaddr);
554
555             socket(*FH, PF_INET, SOCK_STREAM, $proto)
556                                              || return "inet socket: $!";
557             connect(*FH, $paddr)             || return "inet connect: $!";
558         } else {
559             return $err;
560         }
561     }
562     my($oldFH) = select(*FH); $| = 1; select($oldFH);
563     $bpc->{ServerFD} = *FH;
564     return if ( $justConnect );
565     #
566     # Read the seed that we need for our MD5 message digest.  See
567     # ServerMesg below.
568     #
569     sysread($bpc->{ServerFD}, $bpc->{ServerSeed}, 1024);
570     $bpc->{ServerMesgCnt} = 0;
571     return;
572 }
573
574 #
575 # Check that the server connection is still ok
576 #
577 sub ServerOK
578 {
579     my($bpc) = @_;
580
581     return 0 if ( !defined($bpc->{ServerFD}) );
582     vec(my $FDread, fileno($bpc->{ServerFD}), 1) = 1;
583     my $ein = $FDread;
584     return 0 if ( select(my $rout = $FDread, undef, $ein, 0.0) < 0 );
585     return 1 if ( !vec($rout, fileno($bpc->{ServerFD}), 1) );
586 }
587
588 #
589 # Disconnect from the server
590 #
591 sub ServerDisconnect
592 {
593     my($bpc) = @_;
594     return if ( !defined($bpc->{ServerFD}) );
595     close($bpc->{ServerFD});
596     delete($bpc->{ServerFD});
597 }
598
599 #
600 # Sends a message to the server and returns with the reply.
601 #
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
604 # items:
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.
614 #
615 sub ServerMesg
616 {
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}++;
624     return <$fh>;
625 }
626
627 #
628 # Do initialization for child processes
629 #
630 sub ChildInit
631 {
632     my($bpc) = @_;
633     close(STDERR);
634     open(STDERR, ">&STDOUT");
635     select(STDERR); $| = 1;
636     select(STDOUT); $| = 1;
637     $ENV{PATH} = $bpc->{Conf}{MyPath};
638 }
639
640 #
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
645 #     the last 128K.
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.
650 #
651 # Returns the MD5 digest (a hex string) and the file size.
652 #
653 sub File2MD5
654 {
655     my($bpc, $md5, $name) = @_;
656     my($data, $fileSize);
657     local(*N);
658
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) );
664     binmode(N);
665     $md5->reset();
666     $md5->add($fileSize);
667     if ( $fileSize > 262144 ) {
668         #
669         # read the first and last 131072 bytes of the file,
670         # up to 1MB.
671         #
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) );
676     } else {
677         #
678         # read the whole file
679         #
680         $md5->add($data) if ( sysread(N, $data, $fileSize) );
681     }
682     close(N);
683     return ($md5->hexdigest, $fileSize);
684 }
685
686 #
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
691 #     the last 128K.
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.
696 #
697 # Returns the MD5 digest (a hex string).
698 #
699 sub Buffer2MD5
700 {
701     my($bpc, $md5, $fileSize, $dataRef) = @_;
702
703     $md5->reset();
704     $md5->add($fileSize);
705     if ( $fileSize > 262144 ) {
706         #
707         # add the first and last 131072 bytes of the string,
708         # up to 1MB.
709         #
710         my $seekPosn = ($fileSize > 1048576 ? 1048576 : $fileSize) - 131072;
711         $md5->add(substr($$dataRef, 0, 131072));
712         $md5->add(substr($$dataRef, $seekPosn, 131072));
713     } else {
714         #
715         # add the whole string
716         #
717         $md5->add($$dataRef);
718     }
719     return $md5->hexdigest;
720 }
721
722 #
723 # Given an MD5 digest $d and a compress flag, return the full
724 # path in the pool.
725 #
726 sub MD52Path
727 {
728     my($bpc, $d, $compress, $poolDir) = @_;
729
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";
734 }
735
736 #
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.
741 #
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.
747 #
748 sub MakeFileLink
749 {
750     my($bpc, $name, $d, $newFile, $compress) = @_;
751     my($i, $rawFile);
752
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 );
757         if ( -f $rawFile ) {
758             if ( (stat(_))[3] < $bpc->{Conf}{HardLinkMax}
759                     && !compare($name, $rawFile) ) {
760                 unlink($name);
761                 return -3 if ( !link($rawFile, $name) );
762                 return 1;
763             }
764         } elsif ( $newFile && -f $name && (stat($name))[3] == 1 ) {
765             my($newDir);
766             ($newDir = $rawFile) =~ s{(.*)/.*}{$1};
767             mkpath($newDir, 0, 0777) if ( !-d $newDir );
768             return -4 if ( !link($name, $rawFile) );
769             return 2;
770         } else {
771             return 0;
772         }
773     }
774 }
775
776 sub CheckHostAlive
777 {
778     my($bpc, $host) = @_;
779     my($s, $pingCmd, $ret);
780
781     #
782     # Return success if the ping cmd is undefined or empty.
783     #
784     if ( $bpc->{Conf}{PingCmd} eq "" ) {
785         print(STDERR "CheckHostAlive: return ok because \$Conf{PingCmd}"
786                    . " is empty\n") if ( $bpc->{verbose} );
787         return 0;
788     }
789
790     my $args = {
791         pingPath => $bpc->{Conf}{PingPath},
792         host     => $host,
793     };
794     $pingCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{PingCmd}, $args);
795
796     #
797     # Do a first ping in case the PC needs to wakeup
798     #
799     $s = $bpc->cmdSystemOrEval($pingCmd, undef, $args);
800     if ( $? ) {
801         print(STDERR "CheckHostAlive: first ping failed ($?, $!)\n")
802                         if ( $bpc->{verbose} );
803         return -1;
804     }
805
806     #
807     # Do a second ping and get the round-trip time in msec
808     #
809     $s = $bpc->cmdSystemOrEval($pingCmd, undef, $args);
810     if ( $? ) {
811         print(STDERR "CheckHostAlive: second ping failed ($?, $!)\n")
812                         if ( $bpc->{verbose} );
813         return -1;
814     }
815     if ( $s =~ /time=([\d\.]+)\s*ms/i ) {
816         $ret = $1;
817     } elsif ( $s =~ /time=([\d\.]+)\s*usec/i ) {
818         $ret =  $1/1000;
819     } else {
820         print(STDERR "CheckHostAlive: can't extract round-trip time"
821                    . " (not fatal)\n") if ( $bpc->{verbose} );
822         $ret = 0;
823     }
824     print(STDERR "CheckHostAlive: returning $ret\n") if ( $bpc->{verbose} );
825     return $ret;
826 }
827
828 sub CheckFileSystemUsage
829 {
830     my($bpc) = @_;
831     my($topDir) = $bpc->{TopDir};
832     my($s, $dfCmd);
833
834     return 0 if ( $bpc->{Conf}{DfCmd} eq "" );
835     my $args = {
836         dfPath   => $bpc->{Conf}{DfPath},
837         topDir   => $bpc->{TopDir},
838     };
839     $dfCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{DfCmd}, $args);
840     $s = $bpc->cmdSystemOrEval($dfCmd, undef, $args);
841     return 0 if ( $? || $s !~ /(\d+)%/s );
842     return $1;
843 }
844
845 #
846 # Given an IP address, return the host name and user name via
847 # NetBios.
848 #
849 sub NetBiosInfoGet
850 {
851     my($bpc, $host) = @_;
852     my($netBiosHostName, $netBiosUserName);
853     my($s, $nmbCmd);
854
855     #
856     # Skip NetBios check if NmbLookupCmd is emtpy
857     #
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);
862     }
863
864     my $args = {
865         nmbLookupPath => $bpc->{Conf}{NmbLookupPath},
866         host          => $host,
867     };
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
873     }
874     if ( !defined($netBiosHostName) ) {
875         print(STDERR "NetBiosInfoGet: failed: can't parse return string\n")
876                         if ( $bpc->{verbose} );
877         return;
878     }
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);
884 }
885
886 #
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.
892 #
893 sub NetBiosHostIPFind
894 {
895     my($bpc, $host) = @_;
896     my($netBiosHostName, $netBiosUserName);
897     my($s, $nmbCmd, $subnet, $ipAddr, $firstIpAddr);
898
899     #
900     # Skip NetBios lookup if NmbLookupFindHostCmd is emtpy
901     #
902     if ( $bpc->{Conf}{NmbLookupFindHostCmd} eq "" ) {
903         print(STDERR "NetBiosHostIPFind: return $host because"
904             . " \$Conf{NmbLookupFindHostCmd} is empty\n")
905                 if ( $bpc->{verbose} );
906         return $host;
907     }
908
909     my $args = {
910         nmbLookupPath => $bpc->{Conf}{NmbLookupPath},
911         host          => $host,
912     };
913     $nmbCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{NmbLookupFindHostCmd}, $args);
914     foreach my $resp ( split(/[\n\r]+/, $bpc->cmdSystemOrEval($nmbCmd, undef,
915                                                               $args) ) ) {
916         if ( $resp =~ /querying\s+\Q$host\E\s+on\s+(\d+\.\d+\.\d+\.\d+)/i ) {
917             $subnet = $1;
918             $subnet = $1 if ( $subnet =~ /^(.*?)(\.255)+$/ );
919         } elsif ( $resp =~ /^\s*(\d+\.\d+\.\d+\.\d+)\s+\Q$host/ ) {
920             my $ip = $1;
921             $firstIpAddr = $ip if ( !defined($firstIpAddr) );
922             $ipAddr      = $ip if ( !defined($ipAddr) && $ip =~ /^\Q$subnet/ );
923         }
924     }
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} );
929         return $ipAddr;
930     } else {
931         print(STDERR "NetBiosHostIPFind: couldn't find IP address for"
932                    . " host $host\n") if ( $bpc->{verbose} );
933         return;
934     }
935 }
936
937 sub fileNameEltMangle
938 {
939     my($bpc, $name) = @_;
940
941     return "" if ( $name eq "" );
942     $name =~ s{([%/\n\r])}{sprintf("%%%02x", ord($1))}eg;
943     return "f$name";
944 }
945
946 #
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.
952 #
953 sub fileNameMangle
954 {
955     my($bpc, $name) = @_;
956
957     $name =~ s{/([^/]+)}{"/" . $bpc->fileNameEltMangle($1)}eg;
958     $name =~ s{^([^/]+)}{$bpc->fileNameEltMangle($1)}eg;
959     return $name;
960 }
961
962 #
963 # This undoes FileNameMangle
964 #
965 sub fileNameUnmangle
966 {
967     my($bpc, $name) = @_;
968
969     $name =~ s{/f}{/}g;
970     $name =~ s{^f}{};
971     $name =~ s{%(..)}{chr(hex($1))}eg;
972     return $name;
973 }
974
975 #
976 # Escape shell meta-characters with backslashes.
977 # This should be applied to each argument seperately, not an
978 # entire shell command.
979 #
980 sub shellEscape
981 {
982     my($bpc, $cmd) = @_;
983
984     $cmd =~ s/([][;&()<>{}|^\n\r\t *\$\\'"`?])/\\$1/g;
985     return $cmd;
986 }
987
988 #
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.
992 #
993 sub execCmd2ShellCmd
994 {
995     my($bpc, @args) = @_;
996     my $str;
997
998     foreach my $a ( @args ) {
999         $str .= " " if ( $str ne "" );
1000         $str .= $bpc->shellEscape($a);
1001     }
1002     return $str;
1003 }
1004
1005 #
1006 # Do a URI-style escape to protect/encode special characters
1007 #
1008 sub uriEsc
1009 {
1010     my($bpc, $s) = @_;
1011     $s =~ s{([^\w.\/-])}{sprintf("%%%02X", ord($1));}eg;
1012     return $s;
1013 }
1014
1015 #
1016 # Do a URI-style unescape to restore special characters
1017 #
1018 sub uriUnesc
1019 {
1020     my($bpc, $s) = @_;
1021     $s =~ s{%(..)}{chr(hex($1))}eg;
1022     return $s;
1023 }
1024
1025 #
1026 # Do variable substitution prior to execution of a command.
1027 #
1028 sub cmdVarSubstitute
1029 {
1030     my($bpc, $template, $vars) = @_;
1031     my(@cmd);
1032
1033     #
1034     # Return without any substitution if the first entry starts with "&",
1035     # indicating this is perl code.
1036     #
1037     if ( (ref($template) eq "ARRAY" ? $template->[0] : $template) =~ /^\&/ ) {
1038         return $template;
1039     }
1040     if ( ref($template) ne "ARRAY" ) {
1041         #
1042         # Split at white space, except if escaped by \
1043         #
1044         $template = [split(/(?<!\\)\s+/, $template)];
1045         #
1046         # Remove the \ that escaped white space.
1047         #
1048         foreach ( @$template ) {
1049             s{\\(\s)}{$1}g;
1050         }
1051     }
1052     #
1053     # Merge variables into @tarClientCmd
1054     #
1055     foreach my $arg ( @$template ) {
1056         #
1057         # Replace scalar variables first
1058         #
1059         $arg =~ s{\$(\w+)(\+?)}{
1060             exists($vars->{$1}) && ref($vars->{$1}) ne "ARRAY"
1061                 ? ($2 eq "+" ? $bpc->shellEscape($vars->{$1}) : $vars->{$1})
1062                 : "\$$1$2"
1063         }eg;
1064         #
1065         # Now replicate any array arguments; this just works for just one
1066         # array var in each argument.
1067         #
1068         if ( $arg =~ m{(.*)\$(\w+)(\+?)(.*)} && ref($vars->{$2}) eq "ARRAY" ) {
1069             my $pre  = $1;
1070             my $var  = $2;
1071             my $esc  = $3;
1072             my $post = $4;
1073             foreach my $v ( @{$vars->{$var}} ) {
1074                 $v = $bpc->shellEscape($v) if ( $esc eq "+" );
1075                 push(@cmd, "$pre$v$post");
1076             }
1077         } else {
1078             push(@cmd, $arg);
1079         }
1080     }
1081     return \@cmd;
1082 }
1083
1084 #
1085 # Exec or eval a command.  $cmd is either a string on an array ref.
1086 #
1087 # @args are optional arguments for the eval() case; they are not used
1088 # for exec().
1089 #
1090 sub cmdExecOrEval
1091 {
1092     my($bpc, $cmd, @args) = @_;
1093     
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} );
1098         eval($cmd);
1099         print(STDERR "Perl code fragment for exec shouldn't return!!\n");
1100         exit(1);
1101     } else {
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} );
1106         alarm(0);
1107         $cmd = [map { m/(.*)/ } @$cmd];         # untaint
1108         #
1109         # force list-form of exec(), ie: no shell even for 1 arg
1110         #
1111         exec { $cmd->[0] } @$cmd;
1112         print(STDERR "Exec failed for @$cmd\n");
1113         exit(1);
1114     }
1115 }
1116
1117 #
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.
1123 #
1124 # @args are optional arguments for the eval() case; they are not used
1125 # for system().
1126 #
1127 # Also, $? should be set when the CHILD pipe is closed.
1128 #
1129 sub cmdSystemOrEval
1130 {
1131     my($bpc, $cmd, $stdoutCB, @args) = @_;
1132     my($pid, $out, $allOut);
1133     local(*CHILD);
1134     
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} );
1139         $out = eval($cmd);
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) );
1145         return;
1146     } else {
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";
1153             $? = 1;
1154             $$stdoutCB .= $err if ( ref($stdoutCB) eq 'SCALAR' );
1155             &$stdoutCB($err)   if ( ref($stdoutCB) eq 'CODE' );
1156             return $err        if ( !defined($stdoutCB) );
1157             return;
1158         }
1159         binmode(CHILD);
1160         if ( !$pid ) {
1161             #
1162             # This is the child
1163             #
1164             close(STDERR);
1165             open(STDERR, ">&STDOUT");
1166             alarm(0);
1167             $cmd = [map { m/(.*)/ } @$cmd];             # untaint
1168             #
1169             # force list-form of exec(), ie: no shell even for 1 arg
1170             #
1171             exec { $cmd->[0] } @$cmd;
1172             print(STDERR "Exec of @$cmd failed\n");
1173             exit(1);
1174         }
1175         #
1176         # The parent gathers the output from the child
1177         #
1178         while ( <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} );
1183         }
1184         $? = 0;
1185         close(CHILD);
1186     }
1187     print(STDERR "cmdSystemOrEval: finished: got output $allOut\n")
1188                         if ( $bpc->{verbose} );
1189     return $out;
1190 }
1191
1192 1;