2 #============================================================= -*-perl-*-w
4 # BackupPC_Admin: Apache/CGI interface for BackupPC.
7 # BackupPC_Admin provides a flexible web interface for BackupPC.
8 # It is a CGI script that runs under Apache.
10 # It requires that Apache pass in $ENV{SCRIPT_NAME} and
11 # $ENV{REMOTE_USER}. The latter requires .ht_access style
12 # authentication. Replace the code below if you are using some other
13 # type of authentication, and have a different way of getting the
16 # Also, this script needs to run as the BackupPC user. To accomplish
17 # this the script is typically installed as setuid to the BackupPC user,
18 # or it can run under mod_perl with httpd running as the BackupPC user.
21 # Craig Barratt <cbarratt@users.sourceforge.net>
24 # Copyright (C) 2001 Craig Barratt
26 # This program is free software; you can redistribute it and/or modify
27 # it under the terms of the GNU General Public License as published by
28 # the Free Software Foundation; either version 2 of the License, or
29 # (at your option) any later version.
31 # This program is distributed in the hope that it will be useful,
32 # but WITHOUT ANY WARRANTY; without even the implied warranty of
33 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 # GNU General Public License for more details.
36 # You should have received a copy of the GNU General Public License
37 # along with this program; if not, write to the Free Software
38 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #========================================================================
42 # Version 2.0.0, released 14 Jun 2003.
44 # See http://backuppc.sourceforge.net.
46 #========================================================================
51 use lib "/usr/local/BackupPC/lib";
53 use BackupPC::FileZIO;
54 use BackupPC::Attrib qw(:all);
58 use vars qw($Cgi %In $MyURL $User %Conf $TopDir $BinDir $bpc);
59 use vars qw(%Status %Info %Jobs @BgQueue @UserQueue @CmdQueue
60 %QueueLen %StatusHost);
61 use vars qw($Hosts $HostsMTime $ConfigMTime $PrivAdmin);
62 use vars qw(%UserEmailInfo $UserEmailInfoMTime %RestoreReq);
70 # We require that Apache pass in $ENV{SCRIPT_NAME} and $ENV{REMOTE_USER}.
71 # The latter requires .ht_access style authentication. Replace this
72 # code if you are using some other type of authentication, and have
73 # a different way of getting the user name.
75 $MyURL = $ENV{SCRIPT_NAME};
76 $User = $ENV{REMOTE_USER};
78 if ( !defined($bpc) ) {
79 ErrorExit($Lang->{BackupPC__Lib__new_failed__check_apache_error_log})
80 if ( !($bpc = BackupPC::Lib->new(undef, undef, 1)) );
81 $TopDir = $bpc->TopDir();
82 $BinDir = $bpc->BinDir();
85 $ConfigMTime = $bpc->ConfigMTime();
86 } elsif ( $bpc->ConfigMTime() != $ConfigMTime ) {
89 $ConfigMTime = $bpc->ConfigMTime();
94 # Clean up %ENV for taint checking
96 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
97 $ENV{PATH} = $Conf{MyPath};
100 # Verify we are running as the correct user
102 if ( $Conf{BackupPCUserVerify}
103 && $> != (my $uid = (getpwnam($Conf{BackupPCUser}))[2]) ) {
104 ErrorExit(eval("qq{$Lang->{Wrong_user__my_userid_is___}}"), <<EOF);
105 This script needs to run as the user specified in \$Conf{BackupPCUser},
106 which is set to $Conf{BackupPCUser}.
108 This is an installation problem. If you are using mod_perl then
109 it appears that Apache is not running as user $Conf{BackupPCUser}.
110 If you are not using mod_perl, then most like setuid is not working
111 properly on BackupPC_Admin. Check the permissions on
112 $Conf{CgiDir}/BackupPC_Admin and look at the documentation.
116 if ( !defined($Hosts) || $bpc->HostsMTime() != $HostsMTime ) {
117 $HostsMTime = $bpc->HostsMTime();
118 $Hosts = $bpc->HostInfoRead();
120 # turn moreUsers list into a hash for quick lookups
121 foreach my $host (keys %$Hosts) {
122 $Hosts->{$host}{moreUsers} =
123 {map {$_, 1} split(",", $Hosts->{$host}{moreUsers}) }
127 my %ActionDispatch = (
128 "summary" => \&Action_Summary,
129 $Lang->{Start_Incr_Backup} => \&Action_StartStopBackup,
130 $Lang->{Start_Full_Backup} => \&Action_StartStopBackup,
131 $Lang->{Stop_Dequeue_Backup} => \&Action_StartStopBackup,
132 "queue" => \&Action_Queue,
133 "view" => \&Action_View,
134 "LOGlist" => \&Action_LOGlist,
135 "emailSummary" => \&Action_EmailSummary,
136 "browse" => \&Action_Browse,
137 $Lang->{Restore} => \&Action_Restore,
138 "RestoreFile" => \&Action_RestoreFile,
139 "hostInfo" => \&Action_HostInfo,
140 "generalInfo" => \&Action_GeneralInfo,
141 "restoreInfo" => \&Action_RestoreInfo,
145 # Set default actions, then call sub handler
147 $In{action} ||= "hostInfo" if ( defined($In{host}) );
148 $In{action} = "generalInfo" if ( !defined($ActionDispatch{$In{action}}) );
149 $ActionDispatch{$In{action}}();
152 ###########################################################################
153 # Action handling subroutines
154 ###########################################################################
158 my($fullTot, $fullSizeTot, $incrTot, $incrSizeTot, $str,
159 $strNone, $strGood, $hostCntGood, $hostCntNone);
161 $hostCntGood = $hostCntNone = 0;
162 GetStatusInfo("hosts");
163 my $Privileged = CheckPermission();
165 if ( !$Privileged ) {
166 ErrorExit($Lang->{Only_privileged_users_can_view_PC_summaries} );
168 foreach my $host ( sort(keys(%Status)) ) {
169 my($fullDur, $incrCnt, $incrAge, $fullSize, $fullRate, $reasonHilite);
171 my @Backups = $bpc->BackupInfoRead($host);
172 my $fullCnt = $incrCnt = 0;
173 my $fullAge = $incrAge = -1;
174 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
175 if ( $Backups[$i]{type} eq "full" ) {
177 if ( $fullAge < 0 || $Backups[$i]{startTime} > $fullAge ) {
178 $fullAge = $Backups[$i]{startTime};
179 $fullSize = $Backups[$i]{size} / (1024 * 1024);
180 $fullDur = $Backups[$i]{endTime} - $Backups[$i]{startTime};
182 $fullSizeTot += $Backups[$i]{size} / (1024 * 1024);
185 if ( $incrAge < 0 || $Backups[$i]{startTime} > $incrAge ) {
186 $incrAge = $Backups[$i]{startTime};
188 $incrSizeTot += $Backups[$i]{size} / (1024 * 1024);
191 if ( $fullAge < 0 ) {
195 $fullAge = sprintf("%.1f", (time - $fullAge) / (24 * 3600));
196 $fullRate = sprintf("%.2f",
197 $fullSize / ($fullDur <= 0 ? 1 : $fullDur));
199 if ( $incrAge < 0 ) {
202 $incrAge = sprintf("%.1f", (time - $incrAge) / (24 * 3600));
204 $fullTot += $fullCnt;
205 $incrTot += $incrCnt;
206 $fullSize = sprintf("%.2f", $fullSize / 1000);
207 $incrAge = " " if ( $incrAge eq "" );
208 $reasonHilite = $Conf{CgiStatusHilightColor}{$Status{$host}{reason}}
209 || $Conf{CgiStatusHilightColor}{$Status{$host}{state}};
210 $reasonHilite = " bgcolor=\"$reasonHilite\"" if ( $reasonHilite ne "" );
211 if ( $Status{$host}{state} ne "Status_backup_in_progress"
212 && $Status{$host}{state} ne "Status_restore_in_progress"
213 && $Status{$host}{error} ne "" ) {
214 ($shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
215 $shortErr = " ($shortErr)";
219 <tr$reasonHilite><td> ${HostLink($host)} </td>
220 <td align="center"> ${UserLink(defined($Hosts->{$host})
221 ? $Hosts->{$host}{user} : "")} </td>
222 <td align="center"> $fullCnt </td>
223 <td align="center"> $fullAge </td>
224 <td align="center"> $fullSize </td>
225 <td align="center"> $fullRate </td>
226 <td align="center"> $incrCnt </td>
227 <td align="center"> $incrAge </td>
228 <td align="center"> $Lang->{$Status{$host}{state}} </td>
229 <td> $Lang->{$Status{$host}{reason}}$shortErr </td></tr>
231 if ( @Backups == 0 ) {
239 $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
240 $incrSizeTot = sprintf("%.2f", $incrSizeTot / 1000);
241 my $now = timeStamp2(time);
243 Header($Lang->{BackupPC__Server_Summary});
244 print eval ("qq{$Lang->{BackupPC_Summary}}");
249 sub Action_StartStopBackup
253 my $start = 1 if ( $In{action} eq $Lang->{Start_Incr_Backup}
254 || $In{action} eq $Lang->{Start_Full_Backup} );
255 my $doFull = $In{action} eq $Lang->{Start_Full_Backup} ? 1 : 0;
256 my $type = $doFull ? "full" : "incremental";
257 my $host = $In{host};
258 my $Privileged = CheckPermission($host);
260 if ( !$Privileged ) {
261 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_stop_or_start_backups}}"));
267 if ( $Hosts->{$host}{dhcp} ) {
268 $reply = $bpc->ServerMesg("backup $In{hostIP} ${EscURI($host)}"
270 $str = eval("qq{$Lang->{Backup_requested_on_DHCP__host}}");
272 $reply = $bpc->ServerMesg("backup ${EscURI($host)}"
273 . " ${EscURI($host)} $User $doFull");
274 $str = eval("qq{$Lang->{Backup_requested_on__host_by__User}}");
277 $reply = $bpc->ServerMesg("stop ${EscURI($host)} $User $In{backoff}");
278 $str = eval("qq{$Lang->{Backup_stopped_dequeued_on__host_by__User}}");
281 Header(eval ("qq{$Lang->{BackupPC__Backup_Requested_on__host}}") );
282 print (eval ("qq{$Lang->{REPLY_FROM_SERVER}}"));
287 my $ipAddr = ConfirmIPAddress($host);
289 Header(eval("qq{$Lang->{BackupPC__Start_Backup_Confirm_on__host}}"));
290 print (eval("qq{$Lang->{Are_you_sure_start}}"));
293 GetStatusInfo("host(${EscURI($host)})");
294 if ( $StatusHost{backoffTime} > time ) {
295 $backoff = sprintf("%.1f",
296 ($StatusHost{backoffTime} - time) / 3600);
298 Header($Lang->{BackupPC__Stop_Backup_Confirm_on__host});
299 print (eval ("qq{$Lang->{Are_you_sure_stop}}"));
307 my($strBg, $strUser, $strCmd);
309 GetStatusInfo("queues");
310 my $Privileged = CheckPermission();
312 if ( !$Privileged ) {
313 ErrorExit($Lang->{Only_privileged_users_can_view_queues_});
317 my $req = pop(@BgQueue);
318 my($reqTime) = timeStamp2($req->{reqTime});
320 <tr><td> ${HostLink($req->{host})} </td>
321 <td align="center"> $reqTime </td>
322 <td align="center"> $req->{user} </td></tr>
325 while ( @UserQueue ) {
326 my $req = pop(@UserQueue);
327 my $reqTime = timeStamp2($req->{reqTime});
329 <tr><td> ${HostLink($req->{host})} </td>
330 <td align="center"> $reqTime </td>
331 <td align="center"> $req->{user} </td></tr>
334 while ( @CmdQueue ) {
335 my $req = pop(@CmdQueue);
336 my $reqTime = timeStamp2($req->{reqTime});
337 (my $cmd = $req->{cmd}[0]) =~ s/$BinDir\///;
339 <tr><td> ${HostLink($req->{host})} </td>
340 <td align="center"> $reqTime </td>
341 <td align="center"> $req->{user} </td>
342 <td> $cmd $req->{cmd}[0] </td></tr>
345 Header($Lang->{BackupPC__Queue_Summary});
347 print ( eval ( "qq{$Lang->{Backup_Queue_Summary}}") );
354 my $Privileged = CheckPermission($In{host});
357 my $host = $In{host};
359 my $type = $In{type};
362 my $ext = $num ne "" ? ".$num" : "";
364 ErrorExit(eval("qq{$Lang->{Invalid_number__num}}")) if ( $num ne "" && $num !~ /^\d+$/ );
365 if ( $type eq "XferLOG" ) {
366 $file = "$TopDir/pc/$host/SmbLOG$ext";
367 $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
368 } elsif ( $type eq "XferLOGbad" ) {
369 $file = "$TopDir/pc/$host/SmbLOG.bad";
370 $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
371 } elsif ( $type eq "XferErrbad" ) {
372 $file = "$TopDir/pc/$host/SmbLOG.bad";
373 $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
374 $comment = $Lang->{Extracting_only_Errors};
375 } elsif ( $type eq "XferErr" ) {
376 $file = "$TopDir/pc/$host/SmbLOG$ext";
377 $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
378 $comment = $Lang->{Extracting_only_Errors};
379 } elsif ( $type eq "RestoreLOG" ) {
380 $file = "$TopDir/pc/$host/RestoreLOG$ext";
381 } elsif ( $type eq "RestoreErr" ) {
382 $file = "$TopDir/pc/$host/RestoreLOG$ext";
383 $comment = $Lang->{Extracting_only_Errors};
384 } elsif ( $host ne "" && $type eq "config" ) {
385 $file = "$TopDir/pc/$host/config.pl";
386 $file = "$TopDir/conf/$host.pl"
387 if ( $host ne "config" && -f "$TopDir/conf/$host.pl"
389 } elsif ( $type eq "docs" ) {
390 $file = "$BinDir/../doc/BackupPC.html";
391 if ( open(LOG, $file) ) {
393 Header($Lang->{BackupPC__Documentation});
394 print while ( <LOG> );
398 ErrorExit(eval("qq{$Lang->{Unable_to_open__file__configuration_problem}}"));
401 } elsif ( $type eq "config" ) {
402 $file = "$TopDir/conf/config.pl";
403 } elsif ( $type eq "hosts" ) {
404 $file = "$TopDir/conf/hosts";
405 } elsif ( $host ne "" ) {
406 $file = "$TopDir/pc/$host/LOG$ext";
408 $file = "$TopDir/log/LOG$ext";
411 if ( !$Privileged ) {
412 ErrorExit($Lang->{Only_privileged_users_can_view_log_or_config_files});
414 if ( !-f $file && -f "$file.z" ) {
418 Header(eval("qq{$Lang->{Backup_PC__Log_File__file}}") );
419 print( eval ("qq{$Lang->{Log_File__file__comment}}"));
420 if ( defined($fh = BackupPC::FileZIO->open($file, 0, $compress)) ) {
421 my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
423 print ( eval ("qq{$Lang->{Contents_of_log_file}}"));
426 if ( $type eq "XferErr" || $type eq "XferErrbad"
427 || $type eq "RestoreErr" ) {
430 $_ = $fh->readLine();
432 print(eval ("qq{$Lang->{skipped__skipped_lines}}"))
437 || /^\s*(\d+) \(\s*\d+\.\d kb\/s\) (.*)$/
438 || /^tar: dumped \d+ files/
439 || /^added interface/i
440 || /^restore tar file /i
441 || /^restore directory /i
442 || /^tarmode is now/i
443 || /^Total bytes written/i
445 || /^Getting files newer than/i
446 || /^Output is \/dev\/null/
447 || /^\([\d\.]* kb\/s\) \(average [\d\.]* kb\/s\)$/
448 || /^\s+directory \\/
456 print(eval("qq{$Lang->{skipped__skipped_lines}}"))
459 print ${EscHTML($_)};
461 } elsif ( $linkHosts ) {
463 $_ = $fh->readLine();
464 last if ( $_ eq "" );
465 my $s = ${EscHTML($_)};
466 $s =~ s/\b([\w-]+)\b/defined($Hosts->{$1})
467 ? ${HostLink($1)} : $1/eg;
470 } elsif ( $type eq "config" ) {
472 $_ = $fh->readLine();
473 last if ( $_ eq "" );
474 # remove any passwords and user names
475 s/(SmbSharePasswd.*=.*['"]).*(['"])/$1$2/ig;
476 s/(SmbShareUserName.*=.*['"]).*(['"])/$1$2/ig;
477 s/(RsyncdPasswd.*=.*['"]).*(['"])/$1$2/ig;
478 s/(ServerMesgSecret.*=.*['"]).*(['"])/$1$2/ig;
479 print ${EscHTML($_)};
483 $_ = $fh->readLine();
484 last if ( $_ eq "" );
485 print ${EscHTML($_)};
490 printf( eval("qq{$Lang->{_pre___Can_t_open_log_file__file}}"));
500 my $Privileged = CheckPermission($In{host});
502 if ( !$Privileged ) {
503 ErrorExit($Lang->{Only_privileged_users_can_view_log_files});
505 my $host = $In{host};
506 my($url0, $hdr, $root, $str);
508 $root = "$TopDir/pc/$host/LOG";
509 $url0 = "&host=${EscURI($host)}";
510 $hdr = "for host $host";
512 $root = "$TopDir/log/LOG";
516 for ( my $i = -1 ; ; $i++ ) {
523 $file .= ".z" if ( !-f $file && -f "$file.z" );
524 last if ( !-f $file );
525 my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
526 my $size = (stat($file))[7];
528 <tr><td> <a href="$MyURL?action=view&type=LOG$url0$url1"><tt>$file</tt></a> </td>
529 <td align="right"> $size </td>
530 <td> $mtimeStr </td></tr>
533 Header($Lang->{BackupPC__Log_File_History});
534 print (eval("qq{$Lang->{Log_File_History__hdr}}"));
538 sub Action_EmailSummary
540 my $Privileged = CheckPermission();
542 if ( !$Privileged ) {
543 ErrorExit($Lang->{Only_privileged_users_can_view_email_summaries});
545 GetStatusInfo("hosts");
548 foreach my $u ( keys(%UserEmailInfo) ) {
549 next if ( !defined($UserEmailInfo{$u}{lastTime}) );
550 my $emailTimeStr = timeStamp2($UserEmailInfo{$u}{lastTime});
551 $EmailStr{$UserEmailInfo{$u}{lastTime}} .= <<EOF;
552 <tr><td>${UserLink($u)} </td>
553 <td>${HostLink($UserEmailInfo{$u}{lastHost})} </td>
554 <td>$emailTimeStr </td>
555 <td>$UserEmailInfo{$u}{lastSubj} </td></tr>
558 foreach my $t ( sort({$b <=> $a} keys(%EmailStr)) ) {
559 $str .= $EmailStr{$t};
561 Header($Lang->{Email_Summary});
562 print (eval("qq{$Lang->{Recent_Email_Summary}}"));
568 my $Privileged = CheckPermission($In{host});
569 my($i, $dirStr, $fileStr, $attr);
572 if ( !$Privileged ) {
573 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_browse_backup_files}}"));
575 my $host = $In{host};
577 my $share = $In{share};
580 ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
582 # Find the requested backup and the previous filled backup
584 my @Backups = $bpc->BackupInfoRead($host);
585 for ( $i = 0 ; $i < @Backups ; $i++ ) {
586 last if ( $Backups[$i]{num} == $num );
588 if ( $i >= @Backups ) {
589 ErrorExit("Backup number $num for host ${EscHTML($host)} does"
592 my $backupTime = timeStamp2($Backups[$i]{startTime});
593 my $backupAge = sprintf("%.1f", (time - $Backups[$i]{startTime})
595 my $view = BackupPC::View->new($bpc, $host, \@Backups);
597 if ( $dir eq "" || $dir eq "." || $dir eq ".." ) {
598 $attr = $view->dirAttrib($num, "", "");
599 if ( keys(%$attr) > 0 ) {
600 $share = (sort(keys(%$attr)))[0];
603 ErrorExit(eval("qq{$Lang->{Directory___EscHTML}}"));
606 $dir = "/$dir" if ( $dir !~ /^\// );
611 # Loop up the directory tree until we hit the top.
615 my($fLast, $fLastum, @DirStr);
617 $attr = $view->dirAttrib($num, $share, $relDir);
618 if ( !defined($attr) ) {
619 ErrorExit(eval("qq{$Lang->{Can_t_browse_bad_directory_name2}}"));
622 my $fileCnt = 0; # file counter
623 $fLast = $dirStr = "";
626 # Loop over each of the files in this directory
628 foreach my $f ( sort(keys(%$attr)) ) {
629 my($dirOpen, $gotDir, $imgStr, $img, $path);
630 my $fURI = $f; # URI escaped $f
631 my $shareURI = $share; # URI escaped $share
632 if ( $relDir eq "" ) {
635 ($path = "$relDir/$f") =~ s{//+}{/}g;
637 if ( $shareURI eq "" ) {
642 $path =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
643 $fURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
644 $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
645 $dirOpen = 1 if ( defined($currDir) && $f eq $currDir );
646 if ( $attr->{$f}{type} == BPC_FTYPE_DIR ) {
648 # Display directory if it exists in current backup.
649 # First find out if there are subdirs
651 my($bold, $unbold, $BGcolor);
653 $img |= 1 << 5 if ( $attr->{$f}{nlink} > 2 );
658 $img |= 1 << 3 if ( $attr->{$f}{nlink} > 2 );
660 my $imgFileName = sprintf("%07b.gif", $img);
661 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
662 if ( "$relDir/$f" eq $dir ) {
663 $BGcolor = " bgcolor=\"$Conf{CgiHeaderBgColor}\"";
668 $dirName =~ s/ / /g;
669 push(@DirStr, {needTick => 1,
672 <a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$imgStr</a><a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path" style="font-size:13px;font-family:arial;text-decoration:none;line-height:15px"> $bold$dirName$unbold</a></td></tr>
677 my($lastTick, $doneLastTick);
678 foreach my $d ( @DirStrPrev ) {
679 $lastTick = $d if ( $d->{needTick} );
681 $doneLastTick = 1 if ( !defined($lastTick) );
682 foreach my $d ( @DirStrPrev ) {
684 if ( $d->{needTick} ) {
687 if ( $d == $lastTick ) {
690 } elsif ( !$doneLastTick ) {
691 $img |= 1 << 3 | 1 << 4;
693 my $imgFileName = sprintf("%07b.gif", $img);
694 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
695 push(@DirStr, {needTick => 0,
696 tdArgs => $d->{tdArgs},
697 link => $imgStr . $d->{link}
702 if ( $relDir eq $dir ) {
704 # This is the selected directory, so display all the files
707 if ( defined($a = $attr->{$f}) ) {
708 my $mtimeStr = $bpc->timeStamp($a->{mtime});
710 my $typeStr = BackupPC::Attrib::fileType2Text(undef,
712 my $modeStr = sprintf("0%o", $a->{mode} & 07777);
714 <td align="center">$typeStr</td>
715 <td align="center">$modeStr</td>
716 <td align="center">$a->{backupNum}</td>
717 <td align="right">$a->{size}</td>
718 <td align="right">$mtimeStr</td>
722 $attrStr .= "<td colspan=\"5\" align=\"center\"> </td>\n";
724 (my $fDisp = "${EscHTML($f)}") =~ s/ / /g;
727 <tr bgcolor="#ffffcc"><td><input type="checkbox" name="fcb$checkBoxCnt" value="$path"> <a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a></td>
733 <tr bgcolor="#ffffcc"><td><input type="checkbox" name="fcb$checkBoxCnt" value="$path"> <a href="$MyURL?action=RestoreFile&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a></td>
741 @DirStrPrev = @DirStr;
742 last if ( $relDir eq "" && $share eq "" );
744 # Prune the last directory off $relDir, or at the very end
745 # do the top-level directory.
747 if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) {
757 my $dirDisplay = "$share/$dir";
758 $dirDisplay =~ s{//+}{/}g;
759 $dirDisplay =~ s{/+$}{}g;
760 $dirDisplay = "/" if ( $dirDisplay eq "" );
763 if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) {
765 my $numF = join(", #", @mergeNums);
766 $filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}");
768 Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"));
770 foreach my $d ( @DirStrPrev ) {
771 $dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n";
774 ### hide checkall button if there are no files
775 my ($topCheckAll, $checkAll, $fileHeader);
777 $fileHeader = eval("qq{$Lang->{fileHeader}}");
779 $checkAll = $Lang->{checkAll};
781 # and put a checkall box on top if there are at least 20 files
782 if ( $checkBoxCnt >= 20 ) {
783 $topCheckAll = $checkAll;
784 $topCheckAll =~ s{allFiles}{allFilestop}g;
787 $fileStr = eval("qq{$Lang->{The_directory_is_empty}}");
790 foreach my $i ( $view->backupList($share, $dir) ) {
792 my $shareURI = $share;
793 $path =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
794 $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
795 push(@otherDirs, "<a href=\"$MyURL?action=browse&host=${EscURI($host)}&num=$i"
796 . "&share=$shareURI&dir=$path\">$i</a>");
800 my $otherDirs = join(",\n", @otherDirs);
801 $filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}");
803 print (eval("qq{$Lang->{Backup_browse_for__host}}"));
810 my $Privileged = CheckPermission($In{host});
811 if ( !$Privileged ) {
812 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files}}"));
814 my $host = $In{host};
816 my $share = $In{share};
817 my(@fileList, $fileListStr, $hiddenStr, $pathHdr, $badFileCnt);
818 my @Backups = $bpc->BackupInfoRead($host);
821 if ( !defined($Hosts->{$host}) ) {
822 ErrorExit(eval("qq{$Lang->{Bad_host_name}}"));
824 for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
825 next if ( !defined($In{"fcb$i"}) );
826 (my $name = $In{"fcb$i"}) =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
827 $badFileCnt++ if ( $name =~ m{(^|/)\.\.(/|$)} );
828 if ( @fileList == 0 ) {
831 while ( substr($name, 0, length($pathHdr)) ne $pathHdr ) {
832 $pathHdr = substr($pathHdr, 0, rindex($pathHdr, "/"));
835 push(@fileList, $name);
837 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
839 $fileListStr .= <<EOF;
840 <li> ${EscHTML($name)}
843 $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
844 $hiddenStr .= "<input type=\"hidden\" name=\"share\" value=\"${EscHTML($share)}\">\n";
845 $badFileCnt++ if ( $In{pathHdr} =~ m{(^|/)\.\.(/|$)} );
846 $badFileCnt++ if ( $In{num} =~ m{(^|/)\.\.(/|$)} );
847 if ( @fileList == 0 ) {
848 ErrorExit($Lang->{You_haven_t_selected_any_files__please_go_Back_to});
851 ErrorExit($Lang->{Nice_try__but_you_can_t_put});
853 if ( @fileList == 1 ) {
854 $pathHdr =~ s/(.*)\/.*/$1/;
856 $pathHdr = "/" if ( $pathHdr eq "" );
857 if ( $In{type} != 0 && @fileList == $In{fcbMax} ) {
859 # All the files in the list were selected, so just restore the
860 # entire parent directory
862 @fileList = ( $pathHdr );
864 if ( $In{type} == 0 ) {
866 # Tell the user what options they have
868 Header(eval("qq{$Lang->{Restore_Options_for__host}}"));
869 print(eval("qq{$Lang->{Restore_Options_for__host2}}"));
872 # Verify that Archive::Zip is available before showing the
875 if ( eval { require Archive::Zip } ) {
876 print (eval("qq{$Lang->{Option_2__Download_Zip_archive}}"));
878 print (eval("qq{$Lang->{Option_2__Download_Zip_archive2}}"));
880 print (eval("qq{$Lang->{Option_3__Download_Zip_archive}}"));
882 } elsif ( $In{type} == 1 ) {
884 # Provide the selected files via a tar archive.
886 my @fileListTrim = @fileList;
887 if ( @fileListTrim > 10 ) {
888 @fileListTrim = (@fileListTrim[0..9], '...');
890 $bpc->ServerMesg("log User $User downloaded tar archive for $host,"
891 . " backup $num; files were: "
892 . join(", ", @fileListTrim));
895 if ( $In{relative} ) {
896 @pathOpts = ("-r", $pathHdr, "-p", "");
899 Content-Type: application/x-gtar
900 Content-Transfer-Encoding: binary
901 Content-Disposition: attachment; filename=\"restore.tar\"
905 # Fork the child off and manually copy the output to our stdout.
906 # This is necessary to ensure the output gets to the correct place
909 $bpc->cmdSystemOrEval(["$BinDir/BackupPC_tarCreate",
918 } elsif ( $In{type} == 2 ) {
920 # Provide the selected files via a zip archive.
922 my @fileListTrim = @fileList;
923 if ( @fileListTrim > 10 ) {
924 @fileListTrim = (@fileListTrim[0..9], '...');
926 $bpc->ServerMesg("log User $User downloaded zip archive for $host,"
927 . " backup $num; files were: "
928 . join(", ", @fileListTrim));
931 if ( $In{relative} ) {
932 @pathOpts = ("-r", $pathHdr, "-p", "");
935 Content-Type: application/zip
936 Content-Transfer-Encoding: binary
937 Content-Disposition: attachment; filename=\"restore.zip\"
940 $In{compressLevel} = 5 if ( $In{compressLevel} !~ /^\d+$/ );
942 # Fork the child off and manually copy the output to our stdout.
943 # This is necessary to ensure the output gets to the correct place
946 $bpc->cmdSystemOrEval(["$BinDir/BackupPC_zipCreate",
949 "-c", $In{compressLevel},
956 } elsif ( $In{type} == 3 ) {
958 # Do restore directly onto host
960 if ( !defined($Hosts->{$In{hostDest}}) ) {
961 ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
963 if ( !CheckPermission($In{hostDest}) ) {
964 ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
967 foreach my $f ( @fileList ) {
969 (my $strippedShare = $share) =~ s/^\///;
970 (my $strippedShareDest = $In{shareDest}) =~ s/^\///;
971 substr($targetFile, 0, length($pathHdr)) = $In{pathHdr};
972 $fileListStr .= <<EOF;
973 <tr><td>$host:/$strippedShare$f</td><td>$In{hostDest}:/$strippedShareDest$targetFile</td></tr>
976 Header(eval("qq{$Lang->{Restore_Confirm_on__host}}"));
977 print(eval("qq{$Lang->{Are_you_sure}}"));
979 } elsif ( $In{type} == 4 ) {
980 if ( !defined($Hosts->{$In{hostDest}}) ) {
981 ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
983 if ( !CheckPermission($In{hostDest}) ) {
984 ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
986 my $hostDest = $1 if ( $In{hostDest} =~ /(.+)/ );
987 my $ipAddr = ConfirmIPAddress($hostDest);
989 # Prepare and send the restore request. We write the request
990 # information using Data::Dumper to a unique file,
991 # $TopDir/pc/$hostDest/restoreReq.$$.n. We use a file
992 # in case the list of files to restore is very long.
995 for ( my $i = 0 ; ; $i++ ) {
996 $reqFileName = "restoreReq.$$.$i";
997 last if ( !-f "$TopDir/pc/$hostDest/$reqFileName" );
1000 # source of restore is hostSrc, #num, path shareSrc/pathHdrSrc
1004 pathHdrSrc => $pathHdr,
1006 # destination of restore is hostDest:shareDest/pathHdrDest
1007 hostDest => $hostDest,
1008 shareDest => $In{shareDest},
1009 pathHdrDest => $In{pathHdr},
1011 # list of files to restore
1012 fileList => \@fileList,
1018 my($dump) = Data::Dumper->new(
1022 if ( open(REQ, ">$TopDir/pc/$hostDest/$reqFileName") ) {
1024 print(REQ $dump->Dump);
1027 ErrorExit(eval("qq{$Lang->{Can_t_open_create}}"));
1029 $reply = $bpc->ServerMesg("restore ${EscURI($ipAddr)}"
1030 . " ${EscURI($hostDest)} $User $reqFileName");
1031 $str = eval("qq{$Lang->{Restore_requested_to_host__hostDest__backup___num}}");
1032 Header(eval("qq{$Lang->{Restore_Requested_on__hostDest}}"));
1033 print (eval("qq{$Lang->{Reply_from_server_was___reply}}"));
1038 sub Action_RestoreFile
1040 restoreFile($In{host}, $In{num}, $In{share}, $In{dir});
1045 my($host, $num, $share, $dir, $skipHardLink, $origName) = @_;
1046 my($Privileged) = CheckPermission($host);
1049 # Some common content (media) types from www.iana.org (via MIME::Types).
1051 my $Ext2ContentType = {
1052 'asc' => 'text/plain',
1053 'avi' => 'video/x-msvideo',
1054 'bmp' => 'image/bmp',
1055 'book' => 'application/x-maker',
1056 'cc' => 'text/plain',
1057 'cpp' => 'text/plain',
1058 'csh' => 'application/x-csh',
1059 'csv' => 'text/comma-separated-values',
1060 'c' => 'text/plain',
1061 'deb' => 'application/x-debian-package',
1062 'doc' => 'application/msword',
1063 'dot' => 'application/msword',
1064 'dtd' => 'text/xml',
1065 'dvi' => 'application/x-dvi',
1066 'eps' => 'application/postscript',
1067 'fb' => 'application/x-maker',
1068 'fbdoc'=> 'application/x-maker',
1069 'fm' => 'application/x-maker',
1070 'frame'=> 'application/x-maker',
1071 'frm' => 'application/x-maker',
1072 'gif' => 'image/gif',
1073 'gtar' => 'application/x-gtar',
1074 'gz' => 'application/x-gzip',
1075 'hh' => 'text/plain',
1076 'hpp' => 'text/plain',
1077 'h' => 'text/plain',
1078 'html' => 'text/html',
1079 'htmlx'=> 'text/html',
1080 'htm' => 'text/html',
1081 'iges' => 'model/iges',
1082 'igs' => 'model/iges',
1083 'jpeg' => 'image/jpeg',
1084 'jpe' => 'image/jpeg',
1085 'jpg' => 'image/jpeg',
1086 'js' => 'application/x-javascript',
1087 'latex'=> 'application/x-latex',
1088 'maker'=> 'application/x-maker',
1089 'mid' => 'audio/midi',
1090 'midi' => 'audio/midi',
1091 'movie'=> 'video/x-sgi-movie',
1092 'mov' => 'video/quicktime',
1093 'mp2' => 'audio/mpeg',
1094 'mp3' => 'audio/mpeg',
1095 'mpeg' => 'video/mpeg',
1096 'mpg' => 'video/mpeg',
1097 'mpp' => 'application/vnd.ms-project',
1098 'pdf' => 'application/pdf',
1099 'pgp' => 'application/pgp-signature',
1100 'php' => 'application/x-httpd-php',
1101 'pht' => 'application/x-httpd-php',
1102 'phtml'=> 'application/x-httpd-php',
1103 'png' => 'image/png',
1104 'ppm' => 'image/x-portable-pixmap',
1105 'ppt' => 'application/powerpoint',
1106 'ppt' => 'application/vnd.ms-powerpoint',
1107 'ps' => 'application/postscript',
1108 'qt' => 'video/quicktime',
1109 'rgb' => 'image/x-rgb',
1110 'rtf' => 'application/rtf',
1111 'rtf' => 'text/rtf',
1112 'shar' => 'application/x-shar',
1113 'shtml'=> 'text/html',
1114 'swf' => 'application/x-shockwave-flash',
1115 'tex' => 'application/x-tex',
1116 'texi' => 'application/x-texinfo',
1117 'texinfo'=> 'application/x-texinfo',
1118 'tgz' => 'application/x-gtar',
1119 'tiff' => 'image/tiff',
1120 'tif' => 'image/tiff',
1121 'txt' => 'text/plain',
1122 'vcf' => 'text/x-vCard',
1123 'vrml' => 'model/vrml',
1124 'wav' => 'audio/x-wav',
1125 'wmls' => 'text/vnd.wap.wmlscript',
1126 'wml' => 'text/vnd.wap.wml',
1127 'wrl' => 'model/vrml',
1128 'xls' => 'application/vnd.ms-excel',
1129 'xml' => 'text/xml',
1130 'xwd' => 'image/x-xwindowdump',
1131 'z' => 'application/x-compress',
1132 'zip' => 'application/zip',
1133 %{$Conf{CgiExt2ContentType}}, # add site-specific values
1135 if ( !$Privileged ) {
1136 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files2}}"));
1139 ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
1141 $dir = "/" if ( $dir eq "" );
1142 my @Backups = $bpc->BackupInfoRead($host);
1143 my $view = BackupPC::View->new($bpc, $host, \@Backups);
1144 my $a = $view->fileAttrib($num, $share, $dir);
1145 if ( $dir =~ m{(^|/)\.\.(/|$)} || !defined($a) ) {
1146 ErrorExit("Can't restore bad file ${EscHTML($dir)}");
1148 my $f = BackupPC::FileZIO->open($a->{fullPath}, 0, $a->{compress});
1150 if ( !$skipHardLink && $a->{type} == BPC_FTYPE_HARDLINK ) {
1152 # hardlinks should look like the file they point to
1155 while ( $f->read(\$data, 65536) > 0 ) {
1159 $linkName =~ s/^\.\///;
1160 my $share = $1 if ( $dir =~ /^\/?(.*?)\// );
1161 restoreFile($host, $num, $share, $linkName, 1, $dir);
1164 $bpc->ServerMesg("log User $User recovered file $host/$num:$share/$dir ($a->{fullPath})");
1165 $dir = $origName if ( defined($origName) );
1166 my $ext = $1 if ( $dir =~ /\.([^\/\.]+)$/ );
1167 my $contentType = $Ext2ContentType->{lc($ext)}
1168 || "application/octet-stream";
1169 my $fileName = $1 if ( $dir =~ /.*\/(.*)/ );
1170 $fileName =~ s/"/\\"/g;
1171 print "Content-Type: $contentType\n";
1172 print "Content-Transfer-Encoding: binary\n";
1173 print "Content-Disposition: attachment; filename=\"$fileName\"\n\n";
1174 while ( $f->read(\$data, 1024 * 1024) > 0 ) {
1182 my $host = $1 if ( $In{host} =~ /(.*)/ );
1183 my($statusStr, $startIncrStr);
1187 return Action_GeneralInfo() if ( $host eq "" );
1189 if ( !-d "$TopDir/pc/$host" && -d "$TopDir/pc/" . lc($host) );
1190 if ( $host =~ /\.\./ || !-d "$TopDir/pc/$host" ) {
1192 # try to lookup by user name
1194 if ( !defined($Hosts->{$host}) ) {
1195 foreach my $h ( keys(%$Hosts) ) {
1196 if ( $Hosts->{$h}{user} eq $host
1197 || lc($Hosts->{$h}{user}) eq lc($host) ) {
1203 ErrorExit(eval("qq{$Lang->{Unknown_host_or_user}}"))
1204 if ( !defined($Hosts->{$host}) );
1208 GetStatusInfo("host(${EscURI($host)})");
1209 $bpc->ConfigRead($host);
1210 %Conf = $bpc->Conf();
1211 my $Privileged = CheckPermission($host);
1212 if ( !$Privileged ) {
1213 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_view_information_about}}"));
1215 ReadUserEmailInfo();
1217 my @Backups = $bpc->BackupInfoRead($host);
1218 my($str, $sizeStr, $compStr, $errStr, $warnStr);
1219 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
1220 my $startTime = timeStamp2($Backups[$i]{startTime});
1221 my $dur = $Backups[$i]{endTime} - $Backups[$i]{startTime};
1222 $dur = 1 if ( $dur <= 0 );
1223 my $duration = sprintf("%.1f", $dur / 60);
1224 my $MB = sprintf("%.1f", $Backups[$i]{size} / (1024*1024));
1225 my $MBperSec = sprintf("%.2f", $Backups[$i]{size} / (1024*1024*$dur));
1226 my $MBExist = sprintf("%.1f", $Backups[$i]{sizeExist} / (1024*1024));
1227 my $MBNew = sprintf("%.1f", $Backups[$i]{sizeNew} / (1024*1024));
1228 my($MBExistComp, $ExistComp, $MBNewComp, $NewComp);
1229 if ( $Backups[$i]{sizeExist} && $Backups[$i]{sizeExistComp} ) {
1230 $MBExistComp = sprintf("%.1f", $Backups[$i]{sizeExistComp}
1232 $ExistComp = sprintf("%.1f%%", 100 *
1233 (1 - $Backups[$i]{sizeExistComp} / $Backups[$i]{sizeExist}));
1235 if ( $Backups[$i]{sizeNew} && $Backups[$i]{sizeNewComp} ) {
1236 $MBNewComp = sprintf("%.1f", $Backups[$i]{sizeNewComp}
1238 $NewComp = sprintf("%.1f%%", 100 *
1239 (1 - $Backups[$i]{sizeNewComp} / $Backups[$i]{sizeNew}));
1241 my $age = sprintf("%.1f", (time - $Backups[$i]{startTime}) / (24*3600));
1242 my $browseURL = "$MyURL?action=browse&host=${EscURI($host)}&num=$Backups[$i]{num}";
1243 my $filled = $Backups[$i]{noFill} ? $Lang->{No} : $Lang->{Yes};
1244 $filled .= " ($Backups[$i]{fillFromNum}) "
1245 if ( $Backups[$i]{fillFromNum} ne "" );
1247 if ($Backups[$i]{type} eq "full") { $ltype = $Lang->{full}; }
1248 else { $ltype = $Lang->{incremental}; }
1250 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1251 <td align="center"> $ltype </td>
1252 <td align="center"> $filled </td>
1253 <td align="right"> $startTime </td>
1254 <td align="right"> $duration </td>
1255 <td align="right"> $age </td>
1256 <td align="left"> <tt>$TopDir/pc/$host/$Backups[$i]{num}</tt> </td></tr>
1259 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1260 <td align="center"> $ltype </td>
1261 <td align="right"> $Backups[$i]{nFiles} </td>
1262 <td align="right"> $MB </td>
1263 <td align="right"> $MBperSec </td>
1264 <td align="right"> $Backups[$i]{nFilesExist} </td>
1265 <td align="right"> $MBExist </td>
1266 <td align="right"> $Backups[$i]{nFilesNew} </td>
1267 <td align="right"> $MBNew </td>
1270 my $is_compress = $Backups[$i]{compress} || $Lang->{off};
1271 if (! $ExistComp) { $ExistComp = " "; }
1272 if (! $MBExistComp) { $MBExistComp = " "; }
1274 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1275 <td align="center"> $ltype </td>
1276 <td align="center"> $is_compress </td>
1277 <td align="right"> $MBExist </td>
1278 <td align="right"> $MBExistComp </td>
1279 <td align="right"> $ExistComp </td>
1280 <td align="right"> $MBNew </td>
1281 <td align="right"> $MBNewComp </td>
1282 <td align="right"> $NewComp </td>
1286 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1287 <td align="center"> $ltype </td>
1288 <td align="center"> <a href="$MyURL?action=view&type=XferLOG&num=$Backups[$i]{num}&host=${EscURI($host)}">$Lang->{XferLOG}</a>,
1289 <a href="$MyURL?action=view&type=XferErr&num=$Backups[$i]{num}&host=${EscURI($host)}">$Lang->{Errors}</a> </td>
1290 <td align="right"> $Backups[$i]{xferErrs} </td>
1291 <td align="right"> $Backups[$i]{xferBadFile} </td>
1292 <td align="right"> $Backups[$i]{xferBadShare} </td>
1293 <td align="right"> $Backups[$i]{tarErrs} </td></tr>
1297 my @Restores = $bpc->RestoreInfoRead($host);
1300 for ( my $i = 0 ; $i < @Restores ; $i++ ) {
1301 my $startTime = timeStamp2($Restores[$i]{startTime});
1302 my $dur = $Restores[$i]{endTime} - $Restores[$i]{startTime};
1303 $dur = 1 if ( $dur <= 0 );
1304 my $duration = sprintf("%.1f", $dur / 60);
1305 my $MB = sprintf("%.1f", $Restores[$i]{size} / (1024*1024));
1306 my $MBperSec = sprintf("%.2f", $Restores[$i]{size} / (1024*1024*$dur));
1307 my $Restores_Result = $Lang->{failed};
1308 if ($Restores[$i]{result} ne "failed") { $Restores_Result = $Lang->{success}; }
1309 $restoreStr .= <<EOF;
1310 <tr><td align="center"><a href="$MyURL?action=restoreInfo&num=$Restores[$i]{num}&host=${EscURI($host)}">$Restores[$i]{num}</a> </td>
1311 <td align="center"> $Restores_Result </td>
1312 <td align="right"> $startTime </td>
1313 <td align="right"> $duration </td>
1314 <td align="right"> $Restores[$i]{nFiles} </td>
1315 <td align="right"> $MB </td>
1316 <td align="right"> $Restores[$i]{tarCreateErrs} </td>
1317 <td align="right"> $Restores[$i]{xferErrs} </td>
1321 if ( $restoreStr ne "" ) {
1322 $restoreStr = eval("qq{$Lang->{Restore_Summary}}");
1324 if ( @Backups == 0 ) {
1325 $warnStr = $Lang->{This_PC_has_never_been_backed_up};
1327 if ( defined($Hosts->{$host}) ) {
1328 my $user = $Hosts->{$host}{user};
1329 my @moreUsers = sort(keys(%{$Hosts->{$host}{moreUsers}}));
1331 foreach my $u ( sort(keys(%{$Hosts->{$host}{moreUsers}})) ) {
1332 $moreUserStr .= ", " if ( $moreUserStr ne "" );
1333 $moreUserStr .= "${UserLink($u)}";
1335 if ( $moreUserStr ne "" ) {
1336 $moreUserStr = " ($Lang->{and} $moreUserStr).\n";
1338 $moreUserStr = ".\n";
1340 if ( $user ne "" ) {
1341 $statusStr .= eval("qq{$Lang->{This_PC_is_used_by}$moreUserStr}");
1343 if ( defined($UserEmailInfo{$user})
1344 && $UserEmailInfo{$user}{lastHost} eq $host ) {
1345 my $mailTime = timeStamp2($UserEmailInfo{$user}{lastTime});
1346 my $subj = $UserEmailInfo{$user}{lastSubj};
1347 $statusStr .= eval("qq{$Lang->{Last_email_sent_to__was_at___subject}}");
1350 if ( defined($Jobs{$host}) ) {
1351 my $startTime = timeStamp2($Jobs{$host}{startTime});
1352 (my $cmd = $Jobs{$host}{cmd}) =~ s/$BinDir\///g;
1353 $statusStr .= eval("qq{$Lang->{The_command_cmd_is_currently_running_for_started}}");
1355 if ( $StatusHost{BgQueueOn} ) {
1356 $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_background_queue_will_be_backed_up_soon}}");
1358 if ( $StatusHost{UserQueueOn} ) {
1359 $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_user_queue__will_be_backed_up_soon}}");
1361 if ( $StatusHost{CmdQueueOn} ) {
1362 $statusStr .= eval("qq{$Lang->{A_command_for_host_is_on_the_command_queue_will_run_soon}}");
1364 my $startTime = timeStamp2($StatusHost{endTime} == 0 ?
1365 $StatusHost{startTime} : $StatusHost{endTime});
1367 if ( $StatusHost{reason} ne "" ) {
1368 $reason = " ($Lang->{$StatusHost{reason}})";
1370 $statusStr .= eval("qq{$Lang->{Last_status_is_state_StatusHost_state_reason_as_of_startTime}}");
1372 if ( $StatusHost{state} ne "Status_backup_in_progress"
1373 && $StatusHost{state} ne "Status_restore_in_progress"
1374 && $StatusHost{error} ne "" ) {
1375 $statusStr .= eval("qq{$Lang->{Last_error_is____EscHTML_StatusHost_error}}");
1377 my $priorStr = "Pings";
1378 if ( $StatusHost{deadCnt} > 0 ) {
1379 $statusStr .= eval("qq{$Lang->{Pings_to_host_have_failed_StatusHost_deadCnt__consecutive_times}}");
1380 $priorStr = $Lang->{Prior_to_that__pings};
1382 if ( $StatusHost{aliveCnt} > 0 ) {
1383 $statusStr .= eval("qq{$Lang->{priorStr_to_host_have_succeeded_StatusHostaliveCnt_consecutive_times}}");
1385 if ( $StatusHost{aliveCnt} >= $Conf{BlackoutGoodCnt}
1386 && $Conf{BlackoutGoodCnt} >= 0 && $Conf{BlackoutHourBegin} >= 0
1387 && $Conf{BlackoutHourEnd} >= 0 ) {
1388 my(@days) = qw(Sun Mon Tue Wed Thu Fri Sat);
1389 my($days) = join(", ", @days[@{$Conf{BlackoutWeekDays}}]);
1390 my($t0) = sprintf("%d:%02d", $Conf{BlackoutHourBegin},
1391 60 * ($Conf{BlackoutHourBegin}
1392 - int($Conf{BlackoutHourBegin})));
1393 my($t1) = sprintf("%d:%02d", $Conf{BlackoutHourEnd},
1394 60 * ($Conf{BlackoutHourEnd}
1395 - int($Conf{BlackoutHourEnd})));
1396 $statusStr .= eval("qq{$Lang->{Because__host_has_been_on_the_network_at_least__Conf_BlackoutGoodCnt_consecutive_times___}}");
1399 if ( $StatusHost{backoffTime} > time ) {
1400 my $hours = sprintf("%.1f", ($StatusHost{backoffTime} - time) / 3600);
1401 $statusStr .= eval("qq{$Lang->{Backups_are_deferred_for_hours_hours_change_this_number}}");
1405 # only allow incremental if there are already some backups
1406 $startIncrStr = <<EOF;
1407 <input type="submit" value="\$Lang->{Start_Incr_Backup}" name="action">
1411 $startIncrStr = eval ("qq{$startIncrStr}");
1413 Header(eval("qq{$Lang->{Host__host_Backup_Summary}}"));
1414 print(eval("qq{$Lang->{Host__host_Backup_Summary2}}"));
1418 sub Action_GeneralInfo
1420 GetStatusInfo("info jobs hosts queueLen");
1421 my $Privileged = CheckPermission();
1423 my($jobStr, $statusStr);
1424 foreach my $host ( sort(keys(%Jobs)) ) {
1425 my $startTime = timeStamp2($Jobs{$host}{startTime});
1426 next if ( $host eq $bpc->trashJob
1427 && $Jobs{$host}{processState} ne "running" );
1428 $Jobs{$host}{type} = $Status{$host}{type}
1429 if ( $Jobs{$host}{type} eq "" && defined($Status{$host}));
1430 (my $cmd = $Jobs{$host}{cmd}) =~ s/$BinDir\///g;
1431 (my $xferPid = $Jobs{$host}{xferPid}) =~ s/,/, /g;
1433 <tr><td> ${HostLink($host)} </td>
1434 <td align="center"> $Jobs{$host}{type} </td>
1435 <td align="center"> ${UserLink(defined($Hosts->{$host})
1436 ? $Hosts->{$host}{user} : "")} </td>
1437 <td> $startTime </td>
1439 <td align="center"> $Jobs{$host}{pid} </td>
1440 <td align="center"> $xferPid </td>
1442 $jobStr .= "</tr>\n";
1444 foreach my $host ( sort(keys(%Status)) ) {
1445 next if ( $Status{$host}{reason} ne "Reason_backup_failed"
1446 && $Status{$host}{reason} ne "Reason_restore_failed"
1447 && (!$Status{$host}{userReq}
1448 || $Status{$host}{reason} ne "Reason_no_ping") );
1449 my $startTime = timeStamp2($Status{$host}{startTime});
1450 my($errorTime, $XferViewStr);
1451 if ( $Status{$host}{errorTime} > 0 ) {
1452 $errorTime = timeStamp2($Status{$host}{errorTime});
1454 if ( -f "$TopDir/pc/$host/SmbLOG.bad"
1455 || -f "$TopDir/pc/$host/SmbLOG.bad.z"
1456 || -f "$TopDir/pc/$host/XferLOG.bad"
1457 || -f "$TopDir/pc/$host/XferLOG.bad.z"
1459 $XferViewStr = <<EOF;
1460 <a href="$MyURL?action=view&type=XferLOGbad&host=${EscURI($host)}">$Lang->{XferLOG}</a>,
1461 <a href="$MyURL?action=view&type=XferErrbad&host=${EscURI($host)}">$Lang->{Errors}</a>
1466 (my $shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
1467 $statusStr .= <<EOF;
1468 <tr><td> ${HostLink($host)} </td>
1469 <td align="center"> $Status{$host}{type} </td>
1470 <td align="center"> ${UserLink(defined($Hosts->{$host})
1471 ? $Hosts->{$host}{user} : "")} </td>
1472 <td align="right"> $startTime </td>
1473 <td> $XferViewStr </td>
1474 <td align="right"> $errorTime </td>
1475 <td> ${EscHTML($shortErr)} </td></tr>
1478 my $now = timeStamp2(time);
1479 my $nextWakeupTime = timeStamp2($Info{nextWakeup});
1480 my $DUlastTime = timeStamp2($Info{DUlastValueTime});
1481 my $DUmaxTime = timeStamp2($Info{DUDailyMaxTime});
1482 my $numBgQueue = $QueueLen{BgQueue};
1483 my $numUserQueue = $QueueLen{UserQueue};
1484 my $numCmdQueue = $QueueLen{CmdQueue};
1485 my $serverStartTime = timeStamp2($Info{startTime});
1486 my $poolInfo = genPoolInfo("pool", \%Info);
1487 my $cpoolInfo = genPoolInfo("cpool", \%Info);
1488 if ( $Info{poolFileCnt} > 0 && $Info{cpoolFileCnt} > 0 ) {
1490 <li>Uncompressed pool:
1494 <li>Compressed pool:
1499 } elsif ( $Info{cpoolFileCnt} > 0 ) {
1500 $poolInfo = $cpoolInfo;
1503 Header($Lang->{H_BackupPC_Server_Status});
1504 print (eval ("qq{$Lang->{BackupPC_Server_Status}}"));
1508 sub Action_RestoreInfo
1510 my $Privileged = CheckPermission($In{host});
1511 my $host = $1 if ( $In{host} =~ /(.*)/ );
1515 if ( !$Privileged ) {
1516 ErrorExit($Lang->{Only_privileged_users_can_view_restore_information});
1519 # Find the requested restore
1521 my @Restores = $bpc->RestoreInfoRead($host);
1522 for ( $i = 0 ; $i < @Restores ; $i++ ) {
1523 last if ( $Restores[$i]{num} == $num );
1525 if ( $i >= @Restores ) {
1526 ErrorExit(eval("qq{$Lang->{Restore_number__num_for_host__does_not_exist}}"));
1530 do "$TopDir/pc/$host/RestoreInfo.$Restores[$i]{num}"
1531 if ( -f "$TopDir/pc/$host/RestoreInfo.$Restores[$i]{num}" );
1533 my $startTime = timeStamp2($Restores[$i]{startTime});
1534 my $reqTime = timeStamp2($RestoreReq{reqTime});
1535 my $dur = $Restores[$i]{endTime} - $Restores[$i]{startTime};
1536 $dur = 1 if ( $dur <= 0 );
1537 my $duration = sprintf("%.1f", $dur / 60);
1538 my $MB = sprintf("%.1f", $Restores[$i]{size} / (1024*1024));
1539 my $MBperSec = sprintf("%.2f", $Restores[$i]{size} / (1024*1024*$dur));
1541 my $fileListStr = "";
1542 foreach my $f ( @{$RestoreReq{fileList}} ) {
1543 my $targetFile = $f;
1544 (my $strippedShareSrc = $RestoreReq{shareSrc}) =~ s/^\///;
1545 (my $strippedShareDest = $RestoreReq{shareDest}) =~ s/^\///;
1546 substr($targetFile, 0, length($RestoreReq{pathHdrSrc}))
1547 = $RestoreReq{pathHdrDest};
1548 $fileListStr .= <<EOF;
1549 <tr><td>$RestoreReq{hostSrc}:/$strippedShareSrc$f</td><td>$RestoreReq{hostDest}:/$strippedShareDest$targetFile</td></tr>
1553 Header(eval("qq{$Lang->{Restore___num_details_for__host}}"));
1554 print(eval("qq{$Lang->{Restore___num_details_for__host2 }}"));
1558 ###########################################################################
1559 # Miscellaneous subroutines
1560 ###########################################################################
1564 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
1565 = localtime($_[0] == 0 ? time : $_[0] );
1568 if ( $Conf{CgiDateFormatMMDD} ) {
1569 return sprintf("$mon/$mday %02d:%02d", $hour, $min);
1571 return sprintf("$mday/$mon %02d:%02d", $hour, $min);
1579 if ( defined($Hosts->{$host}) || defined($Status{$host}) ) {
1580 $s = "<a href=\"$MyURL?host=${EscURI($host)}\">$host</a>";
1592 return \$user if ( $user eq ""
1593 || $Conf{CgiUserUrlCreate} eq "" );
1594 if ( $Conf{CgiUserHomePageCheck} eq ""
1595 || -f sprintf($Conf{CgiUserHomePageCheck}, $user, $user, $user) ) {
1597 . sprintf($Conf{CgiUserUrlCreate}, $user, $user, $user)
1609 $s =~ s/\"/"/g;
1612 $s =~ s{([^[:print:]])}{sprintf("&\#x%02X;", ord($1));}eg;
1619 $s =~ s{([^\w.\/-])}{sprintf("%%%02X", ord($1));}eg;
1626 my($head) = shift(@mesg);
1627 my($mesg) = join("</p>\n<p>", @mesg);
1628 $Conf{CgiHeaderFontType} ||= "arial";
1629 $Conf{CgiHeaderFontSize} ||= "3";
1630 $Conf{CgiNavBarBgColor} ||= "#ddeeee";
1631 $Conf{CgiHeaderBgColor} ||= "#99cc33";
1633 if ( !defined($ENV{REMOTE_USER}) ) {
1636 Note: \$ENV{REMOTE_USER} is not set, which could mean there is an
1637 installation problem. BackupPC_Admin expects Apache to authenticate
1638 the user and pass their user name into this script as the REMOTE_USER
1639 environment variable. See the documentation.
1643 $bpc->ServerMesg("log User $User (host=$In{host}) got CGI error: $head")
1644 if ( defined($bpc) );
1645 if ( !defined($Lang->{Error}) ) {
1646 Header("BackupPC: Error");
1647 $mesg = <<EOF if ( !defined($mesg) );
1648 There is some problem with the BackupPC installation.
1649 Please check the permissions on BackupPC_Admin.
1652 ${h1("Error: Unable to read config.pl or language strings!!")}
1657 Header(eval("qq{$Lang->{Error}}"));
1658 print (eval("qq{$Lang->{Error____head}}"));
1667 # Verify that the server connection is ok
1669 return if ( $bpc->ServerOK() );
1670 $bpc->ServerDisconnect();
1671 if ( my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}) ) {
1672 ErrorExit(eval("qq{$Lang->{Unable_to_connect_to_BackupPC_server}}"));
1680 my $reply = $bpc->ServerMesg("status $status");
1681 $reply = $1 if ( $reply =~ /(.*)/s );
1683 # ignore status related to admin and trashClean jobs
1684 if ( $status =~ /\bhosts\b/ ) {
1685 delete($Status{$bpc->adminJob});
1686 delete($Status{$bpc->trashJob});
1690 sub ReadUserEmailInfo
1692 if ( (stat("$TopDir/log/UserEmailInfo.pl"))[9] != $UserEmailInfoMTime ) {
1693 do "$TopDir/log/UserEmailInfo.pl";
1694 $UserEmailInfoMTime = (stat("$TopDir/log/UserEmailInfo.pl"))[9];
1699 # Check if the user is privileged. A privileged user can access
1700 # any information (backup files, logs, status pages etc).
1702 # A user is privileged if they belong to the group
1703 # $Conf{CgiAdminUserGroup}, or they are in $Conf{CgiAdminUsers}
1704 # or they are the user assigned to a host in the host file.
1711 return 0 if ( $User eq "" && $Conf{CgiAdminUsers} ne "*"
1712 || $host ne "" && !defined($Hosts->{$host}) );
1713 if ( $Conf{CgiAdminUserGroup} ne "" ) {
1714 my($n,$p,$gid,$mem) = getgrnam($Conf{CgiAdminUserGroup});
1715 $Privileged ||= ($mem =~ /\b$User\b/);
1717 if ( $Conf{CgiAdminUsers} ne "" ) {
1718 $Privileged ||= ($Conf{CgiAdminUsers} =~ /\b$User\b/);
1719 $Privileged ||= $Conf{CgiAdminUsers} eq "*";
1721 $PrivAdmin = $Privileged;
1722 $Privileged ||= $User eq $Hosts->{$host}{user};
1723 $Privileged ||= defined($Hosts->{$host}{moreUsers}{$User});
1729 # Returns the list of hosts that should appear in the navigation bar
1730 # for this user. If $Conf{CgiNavBarAdminAllHosts} is set, the admin
1731 # gets all the hosts. Otherwise, regular users get hosts for which
1732 # they are the user or are listed in the moreUsers column in the
1737 if ( $Conf{CgiNavBarAdminAllHosts} && CheckPermission() ) {
1738 return sort keys %$Hosts;
1741 return sort grep { $Hosts->{$_}{user} eq $User ||
1742 defined($Hosts->{$_}{moreUsers}{$User}) } keys(%$Hosts);
1746 # Given a host name tries to find the IP address. For non-dhcp hosts
1747 # we just return the host name. For dhcp hosts we check the address
1748 # the user is using ($ENV{REMOTE_ADDR}) and also the last-known IP
1749 # address for $host. (Later we should replace this with a broadcast
1752 sub ConfirmIPAddress
1757 if ( defined($Hosts->{$host}) && $Hosts->{$host}{dhcp}
1758 && $ENV{REMOTE_ADDR} =~ /^(\d+[\.\d]*)$/ ) {
1760 my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($ipAddr);
1761 if ( $netBiosHost ne $host ) {
1763 GetStatusInfo("host(${EscURI($host)})");
1764 if ( defined($StatusHost{dhcpHostIP})
1765 && $StatusHost{dhcpHostIP} ne $ipAddr ) {
1766 $tryIP = eval("qq{$Lang->{tryIP}}");
1767 ($netBiosHost, $netBiosUser)
1768 = $bpc->NetBiosInfoGet($StatusHost{dhcpHostIP});
1770 if ( $netBiosHost ne $host ) {
1771 ErrorExit(eval("qq{$Lang->{Can_t_find_IP_address_for}}"),
1772 eval("qq{$Lang->{host_is_a_DHCP_host}}"));
1774 $ipAddr = $StatusHost{dhcpHostIP};
1782 my($name, $info) = @_;
1783 my $poolSize = sprintf("%.2f", $info->{"${name}Kb"} / (1000 * 1024));
1784 my $poolRmSize = sprintf("%.2f", $info->{"${name}KbRm"} / (1000 * 1024));
1785 my $poolTime = timeStamp2($info->{"${name}Time"});
1786 $info->{"${name}FileCntRm"} = $info->{"${name}FileCntRm"} + 0;
1787 return eval("qq{$Lang->{Pool_Stat}}");
1790 ###########################################################################
1791 # HTML layout subroutines
1792 ###########################################################################
1798 { link => "", name => $Lang->{Status},
1800 { link => "?action=summary", name => $Lang->{PC_Summary} },
1801 { link => "?action=view&type=LOG", name => $Lang->{LOG_file} },
1802 { link => "?action=LOGlist", name => $Lang->{Old_LOGs} },
1803 { link => "?action=emailSummary", name => $Lang->{Email_summary} },
1804 { link => "?action=view&type=config", name => $Lang->{Config_file} },
1805 { link => "?action=view&type=hosts", name => $Lang->{Hosts_file} },
1806 { link => "?action=queue", name => $Lang->{Current_queues} },
1807 { link => "?action=view&type=docs", name => $Lang->{Documentation},
1809 { link => "http://backuppc.sourceforge.net/faq", name => "FAQ",
1811 { link => "http://backuppc.sourceforge.net", name => "SourceForge",
1814 print $Cgi->header();
1816 <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
1818 <title>$title</title>
1820 </head><body bgcolor="$Conf{CgiBodyBgColor}">
1821 <table cellpadding="0" cellspacing="0" border="0">
1822 <tr valign="top"><td valign="top" bgcolor="$Conf{CgiNavBarBgColor}" width="10%">
1824 NavSectionTitle("BackupPC");
1826 if ( defined($In{host}) && defined($Hosts->{$In{host}}) ) {
1827 my $host = $In{host};
1828 NavSectionTitle( eval("qq{$Lang->{Host_Inhost}}") );
1830 NavLink("?host=${EscURI($host)}", $Lang->{Home});
1831 NavLink("?action=view&type=LOG&host=${EscURI($host)}", $Lang->{LOG_file});
1832 NavLink("?action=LOGlist&host=${EscURI($host)}", $Lang->{Old_LOGs});
1833 if ( -f "$TopDir/pc/$host/SmbLOG.bad"
1834 || -f "$TopDir/pc/$host/SmbLOG.bad.z"
1835 || -f "$TopDir/pc/$host/XferLOG.bad"
1836 || -f "$TopDir/pc/$host/XferLOG.bad.z" ) {
1837 NavLink("?action=view&type=XferLOGbad&host=${EscURI($host)}",
1838 $Lang->{Last_bad_XferLOG});
1839 NavLink("?action=view&type=XferErrbad&host=${EscURI($host)}",
1840 $Lang->{Last_bad_XferLOG_errors_only});
1842 if ( -f "$TopDir/pc/$host/config.pl" ) {
1843 NavLink("?action=view&type=config&host=${EscURI($host)}", $Lang->{Config_file});
1847 NavSectionTitle($Lang->{Hosts});
1848 if ( defined($Hosts) && %$Hosts > 0 ) {
1850 foreach my $host ( GetUserHosts() ) {
1851 NavLink("?host=${EscURI($host)}", $host);
1856 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1857 <tr><td>$Lang->{Host_or_User_name}</td>
1858 <tr><td><form action="$MyURL" method="get"><small>
1859 <input type="text" name="host" size="10" maxlength="64">
1860 <input type="hidden" name="action" value="hostInfo"><input type="submit" value="$Lang->{Go}" name="ignore">
1861 </small></form></td></tr>
1864 NavSectionTitle($Lang->{NavSectionTitle_});
1866 foreach my $l ( @adminLinks ) {
1867 if ( $PrivAdmin || $l->{priv} ) {
1868 NavLink($l->{link}, $l->{name});
1870 NavLink(undef, $l->{name});
1875 </td><td valign="top" width="5"> </td>
1876 <td valign="top" width="90%">
1893 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1894 <tr><td bgcolor="$Conf{CgiHeaderBgColor}"><font face="$Conf{CgiHeaderFontType}"
1895 size="$Conf{CgiHeaderFontSize}"><b>$head</b>
1905 $padding = 1 if ( !defined($padding) );
1907 <table cellpadding="$padding" cellspacing="0" border="0" width="100%">
1918 my($link, $text) = @_;
1919 print "<tr><td width=\"2%\" valign=\"top\"><b>·</b></td>";
1920 if ( defined($link) ) {
1921 $link = "$MyURL$link" if ( $link eq "" || $link =~ /^\?/ );
1923 <td width="98%"><a href="$link"><small>$text</small></a></td></tr>
1927 <td width="98%"><small>$text</small></td></tr>
1936 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1938 <td bgcolor="$Conf{CgiHeaderBgColor}"> <font face="$Conf{CgiHeaderFontType}"
1939 size="$Conf{CgiHeaderFontSize}"><b>$str</b></font>
1949 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1951 <td bgcolor="$Conf{CgiHeaderBgColor}"> <font face="$Conf{CgiHeaderFontType}"
1952 size="$Conf{CgiHeaderFontSize}"><b>$str</b></font>