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