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