* Fixed stupid last-minute change in octal size conversion in
[BackupPC.git] / cgi-bin / BackupPC_Admin
index 6e9d9f5..bc4f1e9 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/perl -T
+#!/bin/perl
 #============================================================= -*-perl-*-w
 #
 # BackupPC_Admin: Apache/CGI interface for BackupPC.
 #
 #========================================================================
 #
-# Version 2.0.0beta1, released 30 Mar 2003.
+# Version 2.0.2, released 6 Oct 2003.
 #
 # See http://backuppc.sourceforge.net.
 #
 #========================================================================
 
 use strict;
+no  utf8;
 use CGI;
-use lib "/usr/local/BackupPC/lib";
+use lib "/usr/local/BackupPC2.0.2/lib";
 use BackupPC::Lib;
 use BackupPC::FileZIO;
 use BackupPC::Attrib qw(:all);
@@ -65,18 +66,9 @@ use vars qw ($Lang);
 $Cgi = new CGI;
 %In = $Cgi->Vars;
 
-#
-# We require that Apache pass in $ENV{SCRIPT_NAME} and $ENV{REMOTE_USER}.
-# The latter requires .ht_access style authentication.  Replace this
-# code if you are using some other type of authentication, and have
-# a different way of getting the user name.
-#
-$MyURL  = $ENV{SCRIPT_NAME};
-$User   = $ENV{REMOTE_USER};
-
 if ( !defined($bpc) ) {
     ErrorExit($Lang->{BackupPC__Lib__new_failed__check_apache_error_log})
-       if ( !($bpc = BackupPC::Lib->new) );
+       if ( !($bpc = BackupPC::Lib->new(undef, undef, 1)) );
     $TopDir = $bpc->TopDir();
     $BinDir = $bpc->BinDir();
     %Conf   = $bpc->Conf();
@@ -89,6 +81,16 @@ if ( !defined($bpc) ) {
     $Lang   = $bpc->Lang();
 }
 
+#
+# We require that Apache pass in $ENV{SCRIPT_NAME} and $ENV{REMOTE_USER}.
+# The latter requires .ht_access style authentication.  Replace this
+# code if you are using some other type of authentication, and have
+# a different way of getting the user name.
+#
+$ENV{REMOTE_USER} = $Conf{BackupPCUser} if ( $ENV{REMOTE_USER} eq "" );
+$MyURL  = $ENV{SCRIPT_NAME};
+$User   = $ENV{REMOTE_USER};
+
 #
 # Clean up %ENV for taint checking
 #
@@ -100,7 +102,16 @@ $ENV{PATH} = $Conf{MyPath};
 #
 if ( $Conf{BackupPCUserVerify}
         && $> != (my $uid = (getpwnam($Conf{BackupPCUser}))[2]) ) {
-    ErrorExit(eval("qq{$Lang->{Wrong_user__my_userid_is___}}"));
+    ErrorExit(eval("qq{$Lang->{Wrong_user__my_userid_is___}}"), <<EOF);
+This script needs to run as the user specified in \$Conf{BackupPCUser},
+which is set to $Conf{BackupPCUser}.
+<p>
+This is an installation problem.  If you are using mod_perl then
+it appears that Apache is not running as user $Conf{BackupPCUser}.
+If you are not using mod_perl, then most like setuid is not working
+properly on BackupPC_Admin.  Check the permissions on
+$Conf{CgiDir}/BackupPC_Admin and look at the documentation.
+EOF
 }
 
 if ( !defined($Hosts) || $bpc->HostsMTime() != $HostsMTime ) {
@@ -157,6 +168,7 @@ sub Action_Summary
     }
     foreach my $host ( sort(keys(%Status)) ) {
         my($fullDur, $incrCnt, $incrAge, $fullSize, $fullRate, $reasonHilite);
+       my($shortErr);
         my @Backups = $bpc->BackupInfoRead($host);
         my $fullCnt = $incrCnt = 0;
         my $fullAge = $incrAge = -1;
@@ -194,12 +206,20 @@ sub Action_Summary
         $incrTot += $incrCnt;
         $fullSize = sprintf("%.2f", $fullSize / 1000);
        $incrAge = "&nbsp;" if ( $incrAge eq "" );
-       $reasonHilite = $Conf{CgiStatusHilightColor}{$Status{$host}{reason}};
+       $reasonHilite = $Conf{CgiStatusHilightColor}{$Status{$host}{reason}}
+                     || $Conf{CgiStatusHilightColor}{$Status{$host}{state}};
        $reasonHilite = " bgcolor=\"$reasonHilite\"" if ( $reasonHilite ne "" );
+        if ( $Status{$host}{state} ne "Status_backup_in_progress"
+               && $Status{$host}{state} ne "Status_restore_in_progress"
+               && $Status{$host}{error} ne "" ) {
+           ($shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
+           $shortErr = " ($shortErr)";
+       }
 
         $str = <<EOF;
 <tr$reasonHilite><td> ${HostLink($host)} </td>
-    <td align="center"> ${UserLink($Hosts->{$host}{user})} </td>
+    <td align="center"> ${UserLink(defined($Hosts->{$host})
+                                   ? $Hosts->{$host}{user} : "")} </td>
     <td align="center"> $fullCnt </td>
     <td align="center"> $fullAge </td>
     <td align="center"> $fullSize </td>
@@ -207,7 +227,7 @@ sub Action_Summary
     <td align="center"> $incrCnt </td>
     <td align="center"> $incrAge </td>
     <td align="center"> $Lang->{$Status{$host}{state}} </td>
-    <td> $Lang->{$Status{$host}{reason}} </td></tr>
+    <td> $Lang->{$Status{$host}{reason}}$shortErr </td></tr>
 EOF
         if ( @Backups == 0 ) {
             $hostCntNone++;
@@ -370,6 +390,7 @@ sub Action_View
     } elsif ( $type eq "docs" ) {
         $file = "$BinDir/../doc/BackupPC.html";
         if ( open(LOG, $file) ) {
+           binmode(LOG);
             Header($Lang->{BackupPC__Documentation});
             print while ( <LOG> );
             close(LOG);
@@ -416,19 +437,20 @@ sub Action_View
                 if ( /smb: \\>/
                         || /^\s*(\d+) \(\s*\d+\.\d kb\/s\) (.*)$/
                         || /^tar: dumped \d+ files/
-                        || /^added interface/i
-                        || /^restore tar file /i
-                        || /^restore directory /i
-                        || /^tarmode is now/i
-                        || /^Total bytes written/i
-                        || /^Domain=/i
-                        || /^Getting files newer than/i
-                        || /^Output is \/dev\/null/
+                        || /^\s*added interface/i
+                        || /^\s*restore tar file /i
+                        || /^\s*restore directory /i
+                        || /^\s*tarmode is now/i
+                        || /^\s*Total bytes written/i
+                        || /^\s*Domain=/i
+                        || /^\s*Getting files newer than/i
+                        || /^\s*Output is \/dev\/null/
                         || /^\([\d\.]* kb\/s\) \(average [\d\.]* kb\/s\)$/
                         || /^\s+directory \\/
-                        || /^Timezone is/
+                        || /^\s*Timezone is/
+                       || /^\s*creating lame (up|low)case table/i
                         || /^\.\//
-                        || /^  /
+                        || /^   /
                            ) {
                    $skipped++;
                    next;
@@ -454,6 +476,7 @@ sub Action_View
                 # remove any passwords and user names
                 s/(SmbSharePasswd.*=.*['"]).*(['"])/$1$2/ig;
                 s/(SmbShareUserName.*=.*['"]).*(['"])/$1$2/ig;
+                s/(RsyncdPasswd.*=.*['"]).*(['"])/$1$2/ig;
                 s/(ServerMesgSecret.*=.*['"]).*(['"])/$1$2/ig;
                 print ${EscHTML($_)};
             }
@@ -585,6 +608,9 @@ sub Action_Browse
     $dir = "/$dir" if ( $dir !~ /^\// );
     my $relDir  = $dir;
     my $currDir = undef;
+    if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
+        ErrorExit($Lang->{Nice_try__but_you_can_t_put});
+    }
 
     #
     # Loop up the directory tree until we hit the top.
@@ -866,7 +892,9 @@ EOF
        if ( @fileListTrim > 10 ) {
            @fileListTrim = (@fileListTrim[0..9], '...');
        }
-       $bpc->ServerMesg(eval("qq{$Lang->{log_User__User_downloaded_tar_archive_for__host}}"));
+       $bpc->ServerMesg("log User $User downloaded tar archive for $host,"
+                      . " backup $num; files were: "
+                      . join(", ", @fileListTrim));
 
         my @pathOpts;
         if ( $In{relative} ) {
@@ -900,7 +928,9 @@ EOF
        if ( @fileListTrim > 10 ) {
            @fileListTrim = (@fileListTrim[0..9], '...');
        }
-       $bpc->ServerMesg(eval("qq{$Lang->{log_User__User_downloaded_zip_archive_for__host}}"));
+       $bpc->ServerMesg("log User $User downloaded zip archive for $host,"
+                      . " backup $num; files were: "
+                      . join(", ", @fileListTrim));
 
         my @pathOpts;
         if ( $In{relative} ) {
@@ -995,6 +1025,7 @@ EOF
                          [qw(*RestoreReq)]);
         $dump->Indent(1);
         if ( open(REQ, ">$TopDir/pc/$hostDest/$reqFileName") ) {
+           binmode(REQ);
             print(REQ $dump->Dump);
             close(REQ);
         } else {
@@ -1343,7 +1374,9 @@ EOF
     }
     $statusStr .= eval("qq{$Lang->{Last_status_is_state_StatusHost_state_reason_as_of_startTime}}");
 
-    if ( $StatusHost{error} ne "" ) {
+    if ( $StatusHost{state} ne "Status_backup_in_progress"
+           && $StatusHost{state} ne "Status_restore_in_progress"
+           && $StatusHost{error} ne "" ) {
         $statusStr .= eval("qq{$Lang->{Last_error_is____EscHTML_StatusHost_error}}");
     }
     my $priorStr = "Pings";
@@ -1404,7 +1437,8 @@ sub Action_GeneralInfo
         $jobStr .= <<EOF;
 <tr><td> ${HostLink($host)} </td>
     <td align="center"> $Jobs{$host}{type} </td>
-    <td align="center"> ${UserLink($Hosts->{$host}{user})} </td>
+    <td align="center"> ${UserLink(defined($Hosts->{$host})
+                                       ? $Hosts->{$host}{user} : "")} </td>
     <td> $startTime </td>
     <td> $cmd </td>
     <td align="center"> $Jobs{$host}{pid} </td>
@@ -1414,7 +1448,9 @@ EOF
     }
     foreach my $host ( sort(keys(%Status)) ) {
         next if ( $Status{$host}{reason} ne "Reason_backup_failed"
-              || $Status{$host}{error} =~ /^Can't find host \Q$host/ );
+                   && $Status{$host}{reason} ne "Reason_restore_failed"
+                   && (!$Status{$host}{userReq}
+                       || $Status{$host}{reason} ne "Reason_no_ping") );
         my $startTime = timeStamp2($Status{$host}{startTime});
         my($errorTime, $XferViewStr);
         if ( $Status{$host}{errorTime} > 0 ) {
@@ -1436,7 +1472,8 @@ EOF
         $statusStr .= <<EOF;
 <tr><td> ${HostLink($host)} </td>
     <td align="center"> $Status{$host}{type} </td>
-    <td align="center"> ${UserLink($Hosts->{$host}{user})} </td>
+    <td align="center"> ${UserLink(defined($Hosts->{$host})
+                                       ? $Hosts->{$host}{user} : "")} </td>
     <td align="right"> $startTime </td>
     <td> $XferViewStr </td>
     <td align="right"> $errorTime </td>
@@ -1598,6 +1635,16 @@ sub ErrorExit
     $Conf{CgiNavBarBgColor}  ||= "#ddeeee";
     $Conf{CgiHeaderBgColor}  ||= "#99cc33";
 
+    if ( !defined($ENV{REMOTE_USER}) ) {
+       $mesg .= <<EOF;
+<p>
+Note: \$ENV{REMOTE_USER} is not set, which could mean there is an
+installation problem.  BackupPC_Admin expects Apache to authenticate
+the user and pass their user name into this script as the REMOTE_USER
+environment variable.  See the documentation.
+EOF
+    }
+
     $bpc->ServerMesg("log User $User (host=$In{host}) got CGI error: $head")
                             if ( defined($bpc) );
     if ( !defined($Lang->{Error}) ) {
@@ -1666,7 +1713,8 @@ sub CheckPermission
     my($host) = @_;
     my $Privileged = 0;
 
-    return 0 if ( $User eq "" || ($host ne "" && !defined($Hosts->{$host})) );
+    return 0 if ( $User eq "" && $Conf{CgiAdminUsers} ne "*"
+              || $host ne "" && !defined($Hosts->{$host}) );
     if ( $Conf{CgiAdminUserGroup} ne "" ) {
         my($n,$p,$gid,$mem) = getgrnam($Conf{CgiAdminUserGroup});
         $Privileged ||= ($mem =~ /\b$User\b/);
@@ -1711,7 +1759,7 @@ sub ConfirmIPAddress
     my($host) = @_;
     my $ipAddr = $host;
 
-    if ( $Hosts->{$host}{dhcp}
+    if ( defined($Hosts->{$host}) && $Hosts->{$host}{dhcp}
               && $ENV{REMOTE_ADDR} =~ /^(\d+[\.\d]*)$/ ) {
        $ipAddr = $1;
        my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($ipAddr);
@@ -1803,7 +1851,7 @@ EOF
     }
     NavSectionTitle($Lang->{Hosts});
     if ( defined($Hosts) && %$Hosts > 0 ) {
-        NavSectionStart(0);
+        NavSectionStart(1);
         foreach my $host ( GetUserHosts() ) {
             NavLink("?host=${EscURI($host)}", $host);
         }
@@ -1859,7 +1907,7 @@ sub NavSectionStart
 {
     my($padding) = @_;
 
-    $padding = 2 if ( !defined($padding) );
+    $padding = 1 if ( !defined($padding) );
     print <<EOF;
 <table cellpadding="$padding" cellspacing="0" border="0" width="100%">
 EOF