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.0beta1, released 30 Mar 2003.
44 # See http://backuppc.sourceforge.net.
46 #========================================================================
50 use lib "/usr/local/BackupPC/lib";
52 use BackupPC::FileZIO;
53 use BackupPC::Attrib qw(:all);
57 use vars qw($Cgi %In $MyURL $User %Conf $TopDir $BinDir $bpc);
58 use vars qw(%Status %Info %Jobs @BgQueue @UserQueue @CmdQueue
59 %QueueLen %StatusHost);
60 use vars qw($Hosts $HostsMTime $ConfigMTime $PrivAdmin);
61 use vars qw(%UserEmailInfo $UserEmailInfoMTime %RestoreReq);
69 # We require that Apache pass in $ENV{SCRIPT_NAME} and $ENV{REMOTE_USER}.
70 # The latter requires .ht_access style authentication. Replace this
71 # code if you are using some other type of authentication, and have
72 # a different way of getting the user name.
74 $MyURL = $ENV{SCRIPT_NAME};
75 $User = $ENV{REMOTE_USER};
77 if ( !defined($bpc) ) {
78 ErrorExit($Lang->{BackupPC__Lib__new_failed__check_apache_error_log})
79 if ( !($bpc = BackupPC::Lib->new) );
80 $TopDir = $bpc->TopDir();
81 $BinDir = $bpc->BinDir();
84 $ConfigMTime = $bpc->ConfigMTime();
85 } elsif ( $bpc->ConfigMTime() != $ConfigMTime ) {
88 $ConfigMTime = $bpc->ConfigMTime();
93 # Clean up %ENV for taint checking
95 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
96 $ENV{PATH} = $Conf{MyPath};
99 # Verify we are running as the correct user
101 if ( $Conf{BackupPCUserVerify}
102 && $> != (my $uid = (getpwnam($Conf{BackupPCUser}))[2]) ) {
103 ErrorExit(eval("qq{$Lang->{Wrong_user__my_userid_is___}}"));
106 if ( !defined($Hosts) || $bpc->HostsMTime() != $HostsMTime ) {
107 $HostsMTime = $bpc->HostsMTime();
108 $Hosts = $bpc->HostInfoRead();
110 # turn moreUsers list into a hash for quick lookups
111 foreach my $host (keys %$Hosts) {
112 $Hosts->{$host}{moreUsers} =
113 {map {$_, 1} split(",", $Hosts->{$host}{moreUsers}) }
117 my %ActionDispatch = (
118 "summary" => \&Action_Summary,
119 $Lang->{Start_Incr_Backup} => \&Action_StartStopBackup,
120 $Lang->{Start_Full_Backup} => \&Action_StartStopBackup,
121 $Lang->{Stop_Dequeue_Backup} => \&Action_StartStopBackup,
122 "queue" => \&Action_Queue,
123 "view" => \&Action_View,
124 "LOGlist" => \&Action_LOGlist,
125 "emailSummary" => \&Action_EmailSummary,
126 "browse" => \&Action_Browse,
127 $Lang->{Restore} => \&Action_Restore,
128 "RestoreFile" => \&Action_RestoreFile,
129 "hostInfo" => \&Action_HostInfo,
130 "generalInfo" => \&Action_GeneralInfo,
131 "restoreInfo" => \&Action_RestoreInfo,
135 # Set default actions, then call sub handler
137 $In{action} ||= "hostInfo" if ( defined($In{host}) );
138 $In{action} = "generalInfo" if ( !defined($ActionDispatch{$In{action}}) );
139 $ActionDispatch{$In{action}}();
142 ###########################################################################
143 # Action handling subroutines
144 ###########################################################################
148 my($fullTot, $fullSizeTot, $incrTot, $incrSizeTot, $str,
149 $strNone, $strGood, $hostCntGood, $hostCntNone);
151 $hostCntGood = $hostCntNone = 0;
152 GetStatusInfo("hosts");
153 my $Privileged = CheckPermission();
155 if ( !$Privileged ) {
156 ErrorExit($Lang->{Only_privileged_users_can_view_PC_summaries} );
158 foreach my $host ( sort(keys(%Status)) ) {
159 my($fullDur, $incrCnt, $incrAge, $fullSize, $fullRate, $reasonHilite);
160 my @Backups = $bpc->BackupInfoRead($host);
161 my $fullCnt = $incrCnt = 0;
162 my $fullAge = $incrAge = -1;
163 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
164 if ( $Backups[$i]{type} eq "full" ) {
166 if ( $fullAge < 0 || $Backups[$i]{startTime} > $fullAge ) {
167 $fullAge = $Backups[$i]{startTime};
168 $fullSize = $Backups[$i]{size} / (1024 * 1024);
169 $fullDur = $Backups[$i]{endTime} - $Backups[$i]{startTime};
171 $fullSizeTot += $Backups[$i]{size} / (1024 * 1024);
174 if ( $incrAge < 0 || $Backups[$i]{startTime} > $incrAge ) {
175 $incrAge = $Backups[$i]{startTime};
177 $incrSizeTot += $Backups[$i]{size} / (1024 * 1024);
180 if ( $fullAge < 0 ) {
184 $fullAge = sprintf("%.1f", (time - $fullAge) / (24 * 3600));
185 $fullRate = sprintf("%.2f",
186 $fullSize / ($fullDur <= 0 ? 1 : $fullDur));
188 if ( $incrAge < 0 ) {
191 $incrAge = sprintf("%.1f", (time - $incrAge) / (24 * 3600));
193 $fullTot += $fullCnt;
194 $incrTot += $incrCnt;
195 $fullSize = sprintf("%.2f", $fullSize / 1000);
196 $incrAge = " " if ( $incrAge eq "" );
197 $reasonHilite = $Conf{CgiStatusHilightColor}{$Status{$host}{reason}};
198 $reasonHilite = " bgcolor=\"$reasonHilite\"" if ( $reasonHilite ne "" );
201 <tr$reasonHilite><td> ${HostLink($host)} </td>
202 <td align="center"> ${UserLink($Hosts->{$host}{user})} </td>
203 <td align="center"> $fullCnt </td>
204 <td align="center"> $fullAge </td>
205 <td align="center"> $fullSize </td>
206 <td align="center"> $fullRate </td>
207 <td align="center"> $incrCnt </td>
208 <td align="center"> $incrAge </td>
209 <td align="center"> $Lang->{$Status{$host}{state}} </td>
210 <td> $Lang->{$Status{$host}{reason}} </td></tr>
212 if ( @Backups == 0 ) {
220 $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
221 $incrSizeTot = sprintf("%.2f", $incrSizeTot / 1000);
222 my $now = timeStamp2(time);
224 Header($Lang->{BackupPC__Server_Summary});
225 print eval ("qq{$Lang->{BackupPC_Summary}}");
230 sub Action_StartStopBackup
234 my $start = 1 if ( $In{action} eq $Lang->{Start_Incr_Backup}
235 || $In{action} eq $Lang->{Start_Full_Backup} );
236 my $doFull = $In{action} eq $Lang->{Start_Full_Backup} ? 1 : 0;
237 my $type = $doFull ? "full" : "incremental";
238 my $host = $In{host};
239 my $Privileged = CheckPermission($host);
241 if ( !$Privileged ) {
242 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_stop_or_start_backups}}"));
248 if ( $Hosts->{$host}{dhcp} ) {
249 $reply = $bpc->ServerMesg("backup $In{hostIP} ${EscURI($host)}"
251 $str = eval("qq{$Lang->{Backup_requested_on_DHCP__host}}");
253 $reply = $bpc->ServerMesg("backup ${EscURI($host)}"
254 . " ${EscURI($host)} $User $doFull");
255 $str = eval("qq{$Lang->{Backup_requested_on__host_by__User}}");
258 $reply = $bpc->ServerMesg("stop ${EscURI($host)} $User $In{backoff}");
259 $str = eval("qq{$Lang->{Backup_stopped_dequeued_on__host_by__User}}");
262 Header(eval ("qq{$Lang->{BackupPC__Backup_Requested_on__host}}") );
263 print (eval ("qq{$Lang->{REPLY_FROM_SERVER}}"));
268 my $ipAddr = ConfirmIPAddress($host);
270 Header(eval("qq{$Lang->{BackupPC__Start_Backup_Confirm_on__host}}"));
271 print (eval("qq{$Lang->{Are_you_sure_start}}"));
274 GetStatusInfo("host(${EscURI($host)})");
275 if ( $StatusHost{backoffTime} > time ) {
276 $backoff = sprintf("%.1f",
277 ($StatusHost{backoffTime} - time) / 3600);
279 Header($Lang->{BackupPC__Stop_Backup_Confirm_on__host});
280 print (eval ("qq{$Lang->{Are_you_sure_stop}}"));
288 my($strBg, $strUser, $strCmd);
290 GetStatusInfo("queues");
291 my $Privileged = CheckPermission();
293 if ( !$Privileged ) {
294 ErrorExit($Lang->{Only_privileged_users_can_view_queues_});
298 my $req = pop(@BgQueue);
299 my($reqTime) = timeStamp2($req->{reqTime});
301 <tr><td> ${HostLink($req->{host})} </td>
302 <td align="center"> $reqTime </td>
303 <td align="center"> $req->{user} </td></tr>
306 while ( @UserQueue ) {
307 my $req = pop(@UserQueue);
308 my $reqTime = timeStamp2($req->{reqTime});
310 <tr><td> ${HostLink($req->{host})} </td>
311 <td align="center"> $reqTime </td>
312 <td align="center"> $req->{user} </td></tr>
315 while ( @CmdQueue ) {
316 my $req = pop(@CmdQueue);
317 my $reqTime = timeStamp2($req->{reqTime});
318 (my $cmd = $req->{cmd}[0]) =~ s/$BinDir\///;
320 <tr><td> ${HostLink($req->{host})} </td>
321 <td align="center"> $reqTime </td>
322 <td align="center"> $req->{user} </td>
323 <td> $cmd $req->{cmd}[0] </td></tr>
326 Header($Lang->{BackupPC__Queue_Summary});
328 print ( eval ( "qq{$Lang->{Backup_Queue_Summary}}") );
335 my $Privileged = CheckPermission($In{host});
338 my $host = $In{host};
340 my $type = $In{type};
343 my $ext = $num ne "" ? ".$num" : "";
345 ErrorExit(eval("qq{$Lang->{Invalid_number__num}}")) if ( $num ne "" && $num !~ /^\d+$/ );
346 if ( $type eq "XferLOG" ) {
347 $file = "$TopDir/pc/$host/SmbLOG$ext";
348 $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
349 } elsif ( $type eq "XferLOGbad" ) {
350 $file = "$TopDir/pc/$host/SmbLOG.bad";
351 $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
352 } elsif ( $type eq "XferErrbad" ) {
353 $file = "$TopDir/pc/$host/SmbLOG.bad";
354 $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
355 $comment = $Lang->{Extracting_only_Errors};
356 } elsif ( $type eq "XferErr" ) {
357 $file = "$TopDir/pc/$host/SmbLOG$ext";
358 $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
359 $comment = $Lang->{Extracting_only_Errors};
360 } elsif ( $type eq "RestoreLOG" ) {
361 $file = "$TopDir/pc/$host/RestoreLOG$ext";
362 } elsif ( $type eq "RestoreErr" ) {
363 $file = "$TopDir/pc/$host/RestoreLOG$ext";
364 $comment = $Lang->{Extracting_only_Errors};
365 } elsif ( $host ne "" && $type eq "config" ) {
366 $file = "$TopDir/pc/$host/config.pl";
367 $file = "$TopDir/conf/$host.pl"
368 if ( $host ne "config" && -f "$TopDir/conf/$host.pl"
370 } elsif ( $type eq "docs" ) {
371 $file = "$BinDir/../doc/BackupPC.html";
372 if ( open(LOG, $file) ) {
373 Header($Lang->{BackupPC__Documentation});
374 print while ( <LOG> );
378 ErrorExit(eval("qq{$Lang->{Unable_to_open__file__configuration_problem}}"));
381 } elsif ( $type eq "config" ) {
382 $file = "$TopDir/conf/config.pl";
383 } elsif ( $type eq "hosts" ) {
384 $file = "$TopDir/conf/hosts";
385 } elsif ( $host ne "" ) {
386 $file = "$TopDir/pc/$host/LOG$ext";
388 $file = "$TopDir/log/LOG$ext";
391 if ( !$Privileged ) {
392 ErrorExit($Lang->{Only_privileged_users_can_view_log_or_config_files});
394 if ( !-f $file && -f "$file.z" ) {
398 Header(eval("qq{$Lang->{Backup_PC__Log_File__file}}") );
399 print( eval ("qq{$Lang->{Log_File__file__comment}}"));
400 if ( defined($fh = BackupPC::FileZIO->open($file, 0, $compress)) ) {
401 my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
403 print ( eval ("qq{$Lang->{Contents_of_log_file}}"));
406 if ( $type eq "XferErr" || $type eq "XferErrbad"
407 || $type eq "RestoreErr" ) {
410 $_ = $fh->readLine();
412 print(eval ("qq{$Lang->{skipped__skipped_lines}}"))
417 || /^\s*(\d+) \(\s*\d+\.\d kb\/s\) (.*)$/
418 || /^tar: dumped \d+ files/
419 || /^added interface/i
420 || /^restore tar file /i
421 || /^restore directory /i
422 || /^tarmode is now/i
423 || /^Total bytes written/i
425 || /^Getting files newer than/i
426 || /^Output is \/dev\/null/
427 || /^\([\d\.]* kb\/s\) \(average [\d\.]* kb\/s\)$/
428 || /^\s+directory \\/
436 print(eval("qq{$Lang->{skipped__skipped_lines}}"))
439 print ${EscHTML($_)};
441 } elsif ( $linkHosts ) {
443 $_ = $fh->readLine();
444 last if ( $_ eq "" );
445 my $s = ${EscHTML($_)};
446 $s =~ s/\b([\w-]+)\b/defined($Hosts->{$1})
447 ? ${HostLink($1)} : $1/eg;
450 } elsif ( $type eq "config" ) {
452 $_ = $fh->readLine();
453 last if ( $_ eq "" );
454 # remove any passwords and user names
455 s/(SmbSharePasswd.*=.*['"]).*(['"])/$1$2/ig;
456 s/(SmbShareUserName.*=.*['"]).*(['"])/$1$2/ig;
457 s/(ServerMesgSecret.*=.*['"]).*(['"])/$1$2/ig;
458 print ${EscHTML($_)};
462 $_ = $fh->readLine();
463 last if ( $_ eq "" );
464 print ${EscHTML($_)};
469 printf( eval("qq{$Lang->{_pre___Can_t_open_log_file__file}}"));
479 my $Privileged = CheckPermission($In{host});
481 if ( !$Privileged ) {
482 ErrorExit($Lang->{Only_privileged_users_can_view_log_files});
484 my $host = $In{host};
485 my($url0, $hdr, $root, $str);
487 $root = "$TopDir/pc/$host/LOG";
488 $url0 = "&host=${EscURI($host)}";
489 $hdr = "for host $host";
491 $root = "$TopDir/log/LOG";
495 for ( my $i = -1 ; ; $i++ ) {
502 $file .= ".z" if ( !-f $file && -f "$file.z" );
503 last if ( !-f $file );
504 my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
505 my $size = (stat($file))[7];
507 <tr><td> <a href="$MyURL?action=view&type=LOG$url0$url1"><tt>$file</tt></a> </td>
508 <td align="right"> $size </td>
509 <td> $mtimeStr </td></tr>
512 Header($Lang->{BackupPC__Log_File_History});
513 print (eval("qq{$Lang->{Log_File_History__hdr}}"));
517 sub Action_EmailSummary
519 my $Privileged = CheckPermission();
521 if ( !$Privileged ) {
522 ErrorExit($Lang->{Only_privileged_users_can_view_email_summaries});
524 GetStatusInfo("hosts");
527 foreach my $u ( keys(%UserEmailInfo) ) {
528 next if ( !defined($UserEmailInfo{$u}{lastTime}) );
529 my $emailTimeStr = timeStamp2($UserEmailInfo{$u}{lastTime});
530 $EmailStr{$UserEmailInfo{$u}{lastTime}} .= <<EOF;
531 <tr><td>${UserLink($u)} </td>
532 <td>${HostLink($UserEmailInfo{$u}{lastHost})} </td>
533 <td>$emailTimeStr </td>
534 <td>$UserEmailInfo{$u}{lastSubj} </td></tr>
537 foreach my $t ( sort({$b <=> $a} keys(%EmailStr)) ) {
538 $str .= $EmailStr{$t};
540 Header($Lang->{Email_Summary});
541 print (eval("qq{$Lang->{Recent_Email_Summary}}"));
547 my $Privileged = CheckPermission($In{host});
548 my($i, $dirStr, $fileStr, $attr);
551 if ( !$Privileged ) {
552 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_browse_backup_files}}"));
554 my $host = $In{host};
556 my $share = $In{share};
559 ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
561 # Find the requested backup and the previous filled backup
563 my @Backups = $bpc->BackupInfoRead($host);
564 for ( $i = 0 ; $i < @Backups ; $i++ ) {
565 last if ( $Backups[$i]{num} == $num );
567 if ( $i >= @Backups ) {
568 ErrorExit("Backup number $num for host ${EscHTML($host)} does"
571 my $backupTime = timeStamp2($Backups[$i]{startTime});
572 my $backupAge = sprintf("%.1f", (time - $Backups[$i]{startTime})
574 my $view = BackupPC::View->new($bpc, $host, \@Backups);
576 if ( $dir eq "" || $dir eq "." || $dir eq ".." ) {
577 $attr = $view->dirAttrib($num, "", "");
578 if ( keys(%$attr) > 0 ) {
579 $share = (sort(keys(%$attr)))[0];
582 ErrorExit(eval("qq{$Lang->{Directory___EscHTML}}"));
585 $dir = "/$dir" if ( $dir !~ /^\// );
590 # Loop up the directory tree until we hit the top.
594 my($fLast, $fLastum, @DirStr);
596 $attr = $view->dirAttrib($num, $share, $relDir);
597 if ( !defined($attr) ) {
598 ErrorExit(eval("qq{$Lang->{Can_t_browse_bad_directory_name2}}"));
601 my $fileCnt = 0; # file counter
602 $fLast = $dirStr = "";
605 # Loop over each of the files in this directory
607 foreach my $f ( sort(keys(%$attr)) ) {
608 my($dirOpen, $gotDir, $imgStr, $img, $path);
609 my $fURI = $f; # URI escaped $f
610 my $shareURI = $share; # URI escaped $share
611 if ( $relDir eq "" ) {
614 ($path = "$relDir/$f") =~ s{//+}{/}g;
616 if ( $shareURI eq "" ) {
621 $path =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
622 $fURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
623 $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
624 $dirOpen = 1 if ( defined($currDir) && $f eq $currDir );
625 if ( $attr->{$f}{type} == BPC_FTYPE_DIR ) {
627 # Display directory if it exists in current backup.
628 # First find out if there are subdirs
630 my($bold, $unbold, $BGcolor);
632 $img |= 1 << 5 if ( $attr->{$f}{nlink} > 2 );
637 $img |= 1 << 3 if ( $attr->{$f}{nlink} > 2 );
639 my $imgFileName = sprintf("%07b.gif", $img);
640 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
641 if ( "$relDir/$f" eq $dir ) {
642 $BGcolor = " bgcolor=\"$Conf{CgiHeaderBgColor}\"";
647 $dirName =~ s/ / /g;
648 push(@DirStr, {needTick => 1,
651 <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>
656 my($lastTick, $doneLastTick);
657 foreach my $d ( @DirStrPrev ) {
658 $lastTick = $d if ( $d->{needTick} );
660 $doneLastTick = 1 if ( !defined($lastTick) );
661 foreach my $d ( @DirStrPrev ) {
663 if ( $d->{needTick} ) {
666 if ( $d == $lastTick ) {
669 } elsif ( !$doneLastTick ) {
670 $img |= 1 << 3 | 1 << 4;
672 my $imgFileName = sprintf("%07b.gif", $img);
673 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
674 push(@DirStr, {needTick => 0,
675 tdArgs => $d->{tdArgs},
676 link => $imgStr . $d->{link}
681 if ( $relDir eq $dir ) {
683 # This is the selected directory, so display all the files
686 if ( defined($a = $attr->{$f}) ) {
687 my $mtimeStr = $bpc->timeStamp($a->{mtime});
689 my $typeStr = BackupPC::Attrib::fileType2Text(undef,
691 my $modeStr = sprintf("0%o", $a->{mode} & 07777);
693 <td align="center">$typeStr</td>
694 <td align="center">$modeStr</td>
695 <td align="center">$a->{backupNum}</td>
696 <td align="right">$a->{size}</td>
697 <td align="right">$mtimeStr</td>
701 $attrStr .= "<td colspan=\"5\" align=\"center\"> </td>\n";
703 (my $fDisp = "${EscHTML($f)}") =~ s/ / /g;
706 <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>
712 <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>
720 @DirStrPrev = @DirStr;
721 last if ( $relDir eq "" && $share eq "" );
723 # Prune the last directory off $relDir, or at the very end
724 # do the top-level directory.
726 if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) {
736 my $dirDisplay = "$share/$dir";
737 $dirDisplay =~ s{//+}{/}g;
738 $dirDisplay =~ s{/+$}{}g;
739 $dirDisplay = "/" if ( $dirDisplay eq "" );
742 if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) {
744 my $numF = join(", #", @mergeNums);
745 $filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}");
747 Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"));
749 foreach my $d ( @DirStrPrev ) {
750 $dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n";
753 ### hide checkall button if there are no files
754 my ($topCheckAll, $checkAll, $fileHeader);
756 $fileHeader = eval("qq{$Lang->{fileHeader}}");
758 $checkAll = $Lang->{checkAll};
760 # and put a checkall box on top if there are at least 20 files
761 if ( $checkBoxCnt >= 20 ) {
762 $topCheckAll = $checkAll;
763 $topCheckAll =~ s{allFiles}{allFilestop}g;
766 $fileStr = eval("qq{$Lang->{The_directory_is_empty}}");
769 foreach my $i ( $view->backupList($share, $dir) ) {
771 my $shareURI = $share;
772 $path =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
773 $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
774 push(@otherDirs, "<a href=\"$MyURL?action=browse&host=${EscURI($host)}&num=$i"
775 . "&share=$shareURI&dir=$path\">$i</a>");
779 my $otherDirs = join(",\n", @otherDirs);
780 $filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}");
782 print (eval("qq{$Lang->{Backup_browse_for__host}}"));
789 my $Privileged = CheckPermission($In{host});
790 if ( !$Privileged ) {
791 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files}}"));
793 my $host = $In{host};
795 my $share = $In{share};
796 my(@fileList, $fileListStr, $hiddenStr, $pathHdr, $badFileCnt);
797 my @Backups = $bpc->BackupInfoRead($host);
800 if ( !defined($Hosts->{$host}) ) {
801 ErrorExit(eval("qq{$Lang->{Bad_host_name}}"));
803 for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
804 next if ( !defined($In{"fcb$i"}) );
805 (my $name = $In{"fcb$i"}) =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
806 $badFileCnt++ if ( $name =~ m{(^|/)\.\.(/|$)} );
807 if ( @fileList == 0 ) {
810 while ( substr($name, 0, length($pathHdr)) ne $pathHdr ) {
811 $pathHdr = substr($pathHdr, 0, rindex($pathHdr, "/"));
814 push(@fileList, $name);
816 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
818 $fileListStr .= <<EOF;
819 <li> ${EscHTML($name)}
822 $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
823 $hiddenStr .= "<input type=\"hidden\" name=\"share\" value=\"${EscHTML($share)}\">\n";
824 $badFileCnt++ if ( $In{pathHdr} =~ m{(^|/)\.\.(/|$)} );
825 $badFileCnt++ if ( $In{num} =~ m{(^|/)\.\.(/|$)} );
826 if ( @fileList == 0 ) {
827 ErrorExit($Lang->{You_haven_t_selected_any_files__please_go_Back_to});
830 ErrorExit($Lang->{Nice_try__but_you_can_t_put});
832 if ( @fileList == 1 ) {
833 $pathHdr =~ s/(.*)\/.*/$1/;
835 $pathHdr = "/" if ( $pathHdr eq "" );
836 if ( $In{type} != 0 && @fileList == $In{fcbMax} ) {
838 # All the files in the list were selected, so just restore the
839 # entire parent directory
841 @fileList = ( $pathHdr );
843 if ( $In{type} == 0 ) {
845 # Tell the user what options they have
847 Header(eval("qq{$Lang->{Restore_Options_for__host}}"));
848 print(eval("qq{$Lang->{Restore_Options_for__host2}}"));
851 # Verify that Archive::Zip is available before showing the
854 if ( eval { require Archive::Zip } ) {
855 print (eval("qq{$Lang->{Option_2__Download_Zip_archive}}"));
857 print (eval("qq{$Lang->{Option_2__Download_Zip_archive2}}"));
859 print (eval("qq{$Lang->{Option_3__Download_Zip_archive}}"));
861 } elsif ( $In{type} == 1 ) {
863 # Provide the selected files via a tar archive.
865 my @fileListTrim = @fileList;
866 if ( @fileListTrim > 10 ) {
867 @fileListTrim = (@fileListTrim[0..9], '...');
869 $bpc->ServerMesg(eval("qq{$Lang->{log_User__User_downloaded_tar_archive_for__host}}"));
872 if ( $In{relative} ) {
873 @pathOpts = ("-r", $pathHdr, "-p", "");
876 Content-Type: application/x-gtar
877 Content-Transfer-Encoding: binary
878 Content-Disposition: attachment; filename=\"restore.tar\"
882 # Fork the child off and manually copy the output to our stdout.
883 # This is necessary to ensure the output gets to the correct place
886 $bpc->cmdSystemOrEval(["$BinDir/BackupPC_tarCreate",
895 } elsif ( $In{type} == 2 ) {
897 # Provide the selected files via a zip archive.
899 my @fileListTrim = @fileList;
900 if ( @fileListTrim > 10 ) {
901 @fileListTrim = (@fileListTrim[0..9], '...');
903 $bpc->ServerMesg(eval("qq{$Lang->{log_User__User_downloaded_zip_archive_for__host}}"));
906 if ( $In{relative} ) {
907 @pathOpts = ("-r", $pathHdr, "-p", "");
910 Content-Type: application/zip
911 Content-Transfer-Encoding: binary
912 Content-Disposition: attachment; filename=\"restore.zip\"
915 $In{compressLevel} = 5 if ( $In{compressLevel} !~ /^\d+$/ );
917 # Fork the child off and manually copy the output to our stdout.
918 # This is necessary to ensure the output gets to the correct place
921 $bpc->cmdSystemOrEval(["$BinDir/BackupPC_zipCreate",
924 "-c", $In{compressLevel},
931 } elsif ( $In{type} == 3 ) {
933 # Do restore directly onto host
935 if ( !defined($Hosts->{$In{hostDest}}) ) {
936 ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
938 if ( !CheckPermission($In{hostDest}) ) {
939 ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
942 foreach my $f ( @fileList ) {
944 (my $strippedShare = $share) =~ s/^\///;
945 (my $strippedShareDest = $In{shareDest}) =~ s/^\///;
946 substr($targetFile, 0, length($pathHdr)) = $In{pathHdr};
947 $fileListStr .= <<EOF;
948 <tr><td>$host:/$strippedShare$f</td><td>$In{hostDest}:/$strippedShareDest$targetFile</td></tr>
951 Header(eval("qq{$Lang->{Restore_Confirm_on__host}}"));
952 print(eval("qq{$Lang->{Are_you_sure}}"));
954 } elsif ( $In{type} == 4 ) {
955 if ( !defined($Hosts->{$In{hostDest}}) ) {
956 ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
958 if ( !CheckPermission($In{hostDest}) ) {
959 ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
961 my $hostDest = $1 if ( $In{hostDest} =~ /(.+)/ );
962 my $ipAddr = ConfirmIPAddress($hostDest);
964 # Prepare and send the restore request. We write the request
965 # information using Data::Dumper to a unique file,
966 # $TopDir/pc/$hostDest/restoreReq.$$.n. We use a file
967 # in case the list of files to restore is very long.
970 for ( my $i = 0 ; ; $i++ ) {
971 $reqFileName = "restoreReq.$$.$i";
972 last if ( !-f "$TopDir/pc/$hostDest/$reqFileName" );
975 # source of restore is hostSrc, #num, path shareSrc/pathHdrSrc
979 pathHdrSrc => $pathHdr,
981 # destination of restore is hostDest:shareDest/pathHdrDest
982 hostDest => $hostDest,
983 shareDest => $In{shareDest},
984 pathHdrDest => $In{pathHdr},
986 # list of files to restore
987 fileList => \@fileList,
993 my($dump) = Data::Dumper->new(
997 if ( open(REQ, ">$TopDir/pc/$hostDest/$reqFileName") ) {
998 print(REQ $dump->Dump);
1001 ErrorExit(eval("qq{$Lang->{Can_t_open_create}}"));
1003 $reply = $bpc->ServerMesg("restore ${EscURI($ipAddr)}"
1004 . " ${EscURI($hostDest)} $User $reqFileName");
1005 $str = eval("qq{$Lang->{Restore_requested_to_host__hostDest__backup___num}}");
1006 Header(eval("qq{$Lang->{Restore_Requested_on__hostDest}}"));
1007 print (eval("qq{$Lang->{Reply_from_server_was___reply}}"));
1012 sub Action_RestoreFile
1014 restoreFile($In{host}, $In{num}, $In{share}, $In{dir});
1019 my($host, $num, $share, $dir, $skipHardLink, $origName) = @_;
1020 my($Privileged) = CheckPermission($host);
1023 # Some common content (media) types from www.iana.org (via MIME::Types).
1025 my $Ext2ContentType = {
1026 'asc' => 'text/plain',
1027 'avi' => 'video/x-msvideo',
1028 'bmp' => 'image/bmp',
1029 'book' => 'application/x-maker',
1030 'cc' => 'text/plain',
1031 'cpp' => 'text/plain',
1032 'csh' => 'application/x-csh',
1033 'csv' => 'text/comma-separated-values',
1034 'c' => 'text/plain',
1035 'deb' => 'application/x-debian-package',
1036 'doc' => 'application/msword',
1037 'dot' => 'application/msword',
1038 'dtd' => 'text/xml',
1039 'dvi' => 'application/x-dvi',
1040 'eps' => 'application/postscript',
1041 'fb' => 'application/x-maker',
1042 'fbdoc'=> 'application/x-maker',
1043 'fm' => 'application/x-maker',
1044 'frame'=> 'application/x-maker',
1045 'frm' => 'application/x-maker',
1046 'gif' => 'image/gif',
1047 'gtar' => 'application/x-gtar',
1048 'gz' => 'application/x-gzip',
1049 'hh' => 'text/plain',
1050 'hpp' => 'text/plain',
1051 'h' => 'text/plain',
1052 'html' => 'text/html',
1053 'htmlx'=> 'text/html',
1054 'htm' => 'text/html',
1055 'iges' => 'model/iges',
1056 'igs' => 'model/iges',
1057 'jpeg' => 'image/jpeg',
1058 'jpe' => 'image/jpeg',
1059 'jpg' => 'image/jpeg',
1060 'js' => 'application/x-javascript',
1061 'latex'=> 'application/x-latex',
1062 'maker'=> 'application/x-maker',
1063 'mid' => 'audio/midi',
1064 'midi' => 'audio/midi',
1065 'movie'=> 'video/x-sgi-movie',
1066 'mov' => 'video/quicktime',
1067 'mp2' => 'audio/mpeg',
1068 'mp3' => 'audio/mpeg',
1069 'mpeg' => 'video/mpeg',
1070 'mpg' => 'video/mpeg',
1071 'mpp' => 'application/vnd.ms-project',
1072 'pdf' => 'application/pdf',
1073 'pgp' => 'application/pgp-signature',
1074 'php' => 'application/x-httpd-php',
1075 'pht' => 'application/x-httpd-php',
1076 'phtml'=> 'application/x-httpd-php',
1077 'png' => 'image/png',
1078 'ppm' => 'image/x-portable-pixmap',
1079 'ppt' => 'application/powerpoint',
1080 'ppt' => 'application/vnd.ms-powerpoint',
1081 'ps' => 'application/postscript',
1082 'qt' => 'video/quicktime',
1083 'rgb' => 'image/x-rgb',
1084 'rtf' => 'application/rtf',
1085 'rtf' => 'text/rtf',
1086 'shar' => 'application/x-shar',
1087 'shtml'=> 'text/html',
1088 'swf' => 'application/x-shockwave-flash',
1089 'tex' => 'application/x-tex',
1090 'texi' => 'application/x-texinfo',
1091 'texinfo'=> 'application/x-texinfo',
1092 'tgz' => 'application/x-gtar',
1093 'tiff' => 'image/tiff',
1094 'tif' => 'image/tiff',
1095 'txt' => 'text/plain',
1096 'vcf' => 'text/x-vCard',
1097 'vrml' => 'model/vrml',
1098 'wav' => 'audio/x-wav',
1099 'wmls' => 'text/vnd.wap.wmlscript',
1100 'wml' => 'text/vnd.wap.wml',
1101 'wrl' => 'model/vrml',
1102 'xls' => 'application/vnd.ms-excel',
1103 'xml' => 'text/xml',
1104 'xwd' => 'image/x-xwindowdump',
1105 'z' => 'application/x-compress',
1106 'zip' => 'application/zip',
1107 %{$Conf{CgiExt2ContentType}}, # add site-specific values
1109 if ( !$Privileged ) {
1110 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files2}}"));
1113 ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
1115 $dir = "/" if ( $dir eq "" );
1116 my @Backups = $bpc->BackupInfoRead($host);
1117 my $view = BackupPC::View->new($bpc, $host, \@Backups);
1118 my $a = $view->fileAttrib($num, $share, $dir);
1119 if ( $dir =~ m{(^|/)\.\.(/|$)} || !defined($a) ) {
1120 ErrorExit("Can't restore bad file ${EscHTML($dir)}");
1122 my $f = BackupPC::FileZIO->open($a->{fullPath}, 0, $a->{compress});
1124 if ( !$skipHardLink && $a->{type} == BPC_FTYPE_HARDLINK ) {
1126 # hardlinks should look like the file they point to
1129 while ( $f->read(\$data, 65536) > 0 ) {
1133 $linkName =~ s/^\.\///;
1134 my $share = $1 if ( $dir =~ /^\/?(.*?)\// );
1135 restoreFile($host, $num, $share, $linkName, 1, $dir);
1138 $bpc->ServerMesg("log User $User recovered file $host/$num:$share/$dir ($a->{fullPath})");
1139 $dir = $origName if ( defined($origName) );
1140 my $ext = $1 if ( $dir =~ /\.([^\/\.]+)$/ );
1141 my $contentType = $Ext2ContentType->{lc($ext)}
1142 || "application/octet-stream";
1143 my $fileName = $1 if ( $dir =~ /.*\/(.*)/ );
1144 $fileName =~ s/"/\\"/g;
1145 print "Content-Type: $contentType\n";
1146 print "Content-Transfer-Encoding: binary\n";
1147 print "Content-Disposition: attachment; filename=\"$fileName\"\n\n";
1148 while ( $f->read(\$data, 1024 * 1024) > 0 ) {
1156 my $host = $1 if ( $In{host} =~ /(.*)/ );
1157 my($statusStr, $startIncrStr);
1161 return Action_GeneralInfo() if ( $host eq "" );
1163 if ( !-d "$TopDir/pc/$host" && -d "$TopDir/pc/" . lc($host) );
1164 if ( $host =~ /\.\./ || !-d "$TopDir/pc/$host" ) {
1166 # try to lookup by user name
1168 if ( !defined($Hosts->{$host}) ) {
1169 foreach my $h ( keys(%$Hosts) ) {
1170 if ( $Hosts->{$h}{user} eq $host
1171 || lc($Hosts->{$h}{user}) eq lc($host) ) {
1177 ErrorExit(eval("qq{$Lang->{Unknown_host_or_user}}"))
1178 if ( !defined($Hosts->{$host}) );
1182 GetStatusInfo("host(${EscURI($host)})");
1183 $bpc->ConfigRead($host);
1184 %Conf = $bpc->Conf();
1185 my $Privileged = CheckPermission($host);
1186 if ( !$Privileged ) {
1187 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_view_information_about}}"));
1189 ReadUserEmailInfo();
1191 my @Backups = $bpc->BackupInfoRead($host);
1192 my($str, $sizeStr, $compStr, $errStr, $warnStr);
1193 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
1194 my $startTime = timeStamp2($Backups[$i]{startTime});
1195 my $dur = $Backups[$i]{endTime} - $Backups[$i]{startTime};
1196 $dur = 1 if ( $dur <= 0 );
1197 my $duration = sprintf("%.1f", $dur / 60);
1198 my $MB = sprintf("%.1f", $Backups[$i]{size} / (1024*1024));
1199 my $MBperSec = sprintf("%.2f", $Backups[$i]{size} / (1024*1024*$dur));
1200 my $MBExist = sprintf("%.1f", $Backups[$i]{sizeExist} / (1024*1024));
1201 my $MBNew = sprintf("%.1f", $Backups[$i]{sizeNew} / (1024*1024));
1202 my($MBExistComp, $ExistComp, $MBNewComp, $NewComp);
1203 if ( $Backups[$i]{sizeExist} && $Backups[$i]{sizeExistComp} ) {
1204 $MBExistComp = sprintf("%.1f", $Backups[$i]{sizeExistComp}
1206 $ExistComp = sprintf("%.1f%%", 100 *
1207 (1 - $Backups[$i]{sizeExistComp} / $Backups[$i]{sizeExist}));
1209 if ( $Backups[$i]{sizeNew} && $Backups[$i]{sizeNewComp} ) {
1210 $MBNewComp = sprintf("%.1f", $Backups[$i]{sizeNewComp}
1212 $NewComp = sprintf("%.1f%%", 100 *
1213 (1 - $Backups[$i]{sizeNewComp} / $Backups[$i]{sizeNew}));
1215 my $age = sprintf("%.1f", (time - $Backups[$i]{startTime}) / (24*3600));
1216 my $browseURL = "$MyURL?action=browse&host=${EscURI($host)}&num=$Backups[$i]{num}";
1217 my $filled = $Backups[$i]{noFill} ? $Lang->{No} : $Lang->{Yes};
1218 $filled .= " ($Backups[$i]{fillFromNum}) "
1219 if ( $Backups[$i]{fillFromNum} ne "" );
1221 if ($Backups[$i]{type} eq "full") { $ltype = $Lang->{full}; }
1222 else { $ltype = $Lang->{incremental}; }
1224 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1225 <td align="center"> $ltype </td>
1226 <td align="center"> $filled </td>
1227 <td align="right"> $startTime </td>
1228 <td align="right"> $duration </td>
1229 <td align="right"> $age </td>
1230 <td align="left"> <tt>$TopDir/pc/$host/$Backups[$i]{num}</tt> </td></tr>
1233 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1234 <td align="center"> $ltype </td>
1235 <td align="right"> $Backups[$i]{nFiles} </td>
1236 <td align="right"> $MB </td>
1237 <td align="right"> $MBperSec </td>
1238 <td align="right"> $Backups[$i]{nFilesExist} </td>
1239 <td align="right"> $MBExist </td>
1240 <td align="right"> $Backups[$i]{nFilesNew} </td>
1241 <td align="right"> $MBNew </td>
1244 my $is_compress = $Backups[$i]{compress} || $Lang->{off};
1245 if (! $ExistComp) { $ExistComp = " "; }
1246 if (! $MBExistComp) { $MBExistComp = " "; }
1248 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1249 <td align="center"> $ltype </td>
1250 <td align="center"> $is_compress </td>
1251 <td align="right"> $MBExist </td>
1252 <td align="right"> $MBExistComp </td>
1253 <td align="right"> $ExistComp </td>
1254 <td align="right"> $MBNew </td>
1255 <td align="right"> $MBNewComp </td>
1256 <td align="right"> $NewComp </td>
1260 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1261 <td align="center"> $ltype </td>
1262 <td align="center"> <a href="$MyURL?action=view&type=XferLOG&num=$Backups[$i]{num}&host=${EscURI($host)}">$Lang->{XferLOG}</a>,
1263 <a href="$MyURL?action=view&type=XferErr&num=$Backups[$i]{num}&host=${EscURI($host)}">$Lang->{Errors}</a> </td>
1264 <td align="right"> $Backups[$i]{xferErrs} </td>
1265 <td align="right"> $Backups[$i]{xferBadFile} </td>
1266 <td align="right"> $Backups[$i]{xferBadShare} </td>
1267 <td align="right"> $Backups[$i]{tarErrs} </td></tr>
1271 my @Restores = $bpc->RestoreInfoRead($host);
1274 for ( my $i = 0 ; $i < @Restores ; $i++ ) {
1275 my $startTime = timeStamp2($Restores[$i]{startTime});
1276 my $dur = $Restores[$i]{endTime} - $Restores[$i]{startTime};
1277 $dur = 1 if ( $dur <= 0 );
1278 my $duration = sprintf("%.1f", $dur / 60);
1279 my $MB = sprintf("%.1f", $Restores[$i]{size} / (1024*1024));
1280 my $MBperSec = sprintf("%.2f", $Restores[$i]{size} / (1024*1024*$dur));
1281 my $Restores_Result = $Lang->{failed};
1282 if ($Restores[$i]{result} ne "failed") { $Restores_Result = $Lang->{success}; }
1283 $restoreStr .= <<EOF;
1284 <tr><td align="center"><a href="$MyURL?action=restoreInfo&num=$Restores[$i]{num}&host=${EscURI($host)}">$Restores[$i]{num}</a> </td>
1285 <td align="center"> $Restores_Result </td>
1286 <td align="right"> $startTime </td>
1287 <td align="right"> $duration </td>
1288 <td align="right"> $Restores[$i]{nFiles} </td>
1289 <td align="right"> $MB </td>
1290 <td align="right"> $Restores[$i]{tarCreateErrs} </td>
1291 <td align="right"> $Restores[$i]{xferErrs} </td>
1295 if ( $restoreStr ne "" ) {
1296 $restoreStr = eval("qq{$Lang->{Restore_Summary}}");
1298 if ( @Backups == 0 ) {
1299 $warnStr = $Lang->{This_PC_has_never_been_backed_up};
1301 if ( defined($Hosts->{$host}) ) {
1302 my $user = $Hosts->{$host}{user};
1303 my @moreUsers = sort(keys(%{$Hosts->{$host}{moreUsers}}));
1305 foreach my $u ( sort(keys(%{$Hosts->{$host}{moreUsers}})) ) {
1306 $moreUserStr .= ", " if ( $moreUserStr ne "" );
1307 $moreUserStr .= "${UserLink($u)}";
1309 if ( $moreUserStr ne "" ) {
1310 $moreUserStr = " ($Lang->{and} $moreUserStr).\n";
1312 $moreUserStr = ".\n";
1314 if ( $user ne "" ) {
1315 $statusStr .= eval("qq{$Lang->{This_PC_is_used_by}$moreUserStr}");
1317 if ( defined($UserEmailInfo{$user})
1318 && $UserEmailInfo{$user}{lastHost} eq $host ) {
1319 my $mailTime = timeStamp2($UserEmailInfo{$user}{lastTime});
1320 my $subj = $UserEmailInfo{$user}{lastSubj};
1321 $statusStr .= eval("qq{$Lang->{Last_email_sent_to__was_at___subject}}");
1324 if ( defined($Jobs{$host}) ) {
1325 my $startTime = timeStamp2($Jobs{$host}{startTime});
1326 (my $cmd = $Jobs{$host}{cmd}) =~ s/$BinDir\///g;
1327 $statusStr .= eval("qq{$Lang->{The_command_cmd_is_currently_running_for_started}}");
1329 if ( $StatusHost{BgQueueOn} ) {
1330 $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_background_queue_will_be_backed_up_soon}}");
1332 if ( $StatusHost{UserQueueOn} ) {
1333 $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_user_queue__will_be_backed_up_soon}}");
1335 if ( $StatusHost{CmdQueueOn} ) {
1336 $statusStr .= eval("qq{$Lang->{A_command_for_host_is_on_the_command_queue_will_run_soon}}");
1338 my $startTime = timeStamp2($StatusHost{endTime} == 0 ?
1339 $StatusHost{startTime} : $StatusHost{endTime});
1341 if ( $StatusHost{reason} ne "" ) {
1342 $reason = " ($Lang->{$StatusHost{reason}})";
1344 $statusStr .= eval("qq{$Lang->{Last_status_is_state_StatusHost_state_reason_as_of_startTime}}");
1346 if ( $StatusHost{error} ne "" ) {
1347 $statusStr .= eval("qq{$Lang->{Last_error_is____EscHTML_StatusHost_error}}");
1349 my $priorStr = "Pings";
1350 if ( $StatusHost{deadCnt} > 0 ) {
1351 $statusStr .= eval("qq{$Lang->{Pings_to_host_have_failed_StatusHost_deadCnt__consecutive_times}}");
1352 $priorStr = $Lang->{Prior_to_that__pings};
1354 if ( $StatusHost{aliveCnt} > 0 ) {
1355 $statusStr .= eval("qq{$Lang->{priorStr_to_host_have_succeeded_StatusHostaliveCnt_consecutive_times}}");
1357 if ( $StatusHost{aliveCnt} >= $Conf{BlackoutGoodCnt}
1358 && $Conf{BlackoutGoodCnt} >= 0 && $Conf{BlackoutHourBegin} >= 0
1359 && $Conf{BlackoutHourEnd} >= 0 ) {
1360 my(@days) = qw(Sun Mon Tue Wed Thu Fri Sat);
1361 my($days) = join(", ", @days[@{$Conf{BlackoutWeekDays}}]);
1362 my($t0) = sprintf("%d:%02d", $Conf{BlackoutHourBegin},
1363 60 * ($Conf{BlackoutHourBegin}
1364 - int($Conf{BlackoutHourBegin})));
1365 my($t1) = sprintf("%d:%02d", $Conf{BlackoutHourEnd},
1366 60 * ($Conf{BlackoutHourEnd}
1367 - int($Conf{BlackoutHourEnd})));
1368 $statusStr .= eval("qq{$Lang->{Because__host_has_been_on_the_network_at_least__Conf_BlackoutGoodCnt_consecutive_times___}}");
1371 if ( $StatusHost{backoffTime} > time ) {
1372 my $hours = sprintf("%.1f", ($StatusHost{backoffTime} - time) / 3600);
1373 $statusStr .= eval("qq{$Lang->{Backups_are_deferred_for_hours_hours_change_this_number}}");
1377 # only allow incremental if there are already some backups
1378 $startIncrStr = <<EOF;
1379 <input type="submit" value="\$Lang->{Start_Incr_Backup}" name="action">
1383 $startIncrStr = eval ("qq{$startIncrStr}");
1385 Header(eval("qq{$Lang->{Host__host_Backup_Summary}}"));
1386 print(eval("qq{$Lang->{Host__host_Backup_Summary2}}"));
1390 sub Action_GeneralInfo
1392 GetStatusInfo("info jobs hosts queueLen");
1393 my $Privileged = CheckPermission();
1395 my($jobStr, $statusStr);
1396 foreach my $host ( sort(keys(%Jobs)) ) {
1397 my $startTime = timeStamp2($Jobs{$host}{startTime});
1398 next if ( $host eq $bpc->trashJob
1399 && $Jobs{$host}{processState} ne "running" );
1400 $Jobs{$host}{type} = $Status{$host}{type}
1401 if ( $Jobs{$host}{type} eq "" && defined($Status{$host}));
1402 (my $cmd = $Jobs{$host}{cmd}) =~ s/$BinDir\///g;
1403 (my $xferPid = $Jobs{$host}{xferPid}) =~ s/,/, /g;
1405 <tr><td> ${HostLink($host)} </td>
1406 <td align="center"> $Jobs{$host}{type} </td>
1407 <td align="center"> ${UserLink($Hosts->{$host}{user})} </td>
1408 <td> $startTime </td>
1410 <td align="center"> $Jobs{$host}{pid} </td>
1411 <td align="center"> $xferPid </td>
1413 $jobStr .= "</tr>\n";
1415 foreach my $host ( sort(keys(%Status)) ) {
1416 next if ( $Status{$host}{reason} ne "Reason_backup_failed"
1417 || $Status{$host}{error} =~ /^Can't find host \Q$host/ );
1418 my $startTime = timeStamp2($Status{$host}{startTime});
1419 my($errorTime, $XferViewStr);
1420 if ( $Status{$host}{errorTime} > 0 ) {
1421 $errorTime = timeStamp2($Status{$host}{errorTime});
1423 if ( -f "$TopDir/pc/$host/SmbLOG.bad"
1424 || -f "$TopDir/pc/$host/SmbLOG.bad.z"
1425 || -f "$TopDir/pc/$host/XferLOG.bad"
1426 || -f "$TopDir/pc/$host/XferLOG.bad.z"
1428 $XferViewStr = <<EOF;
1429 <a href="$MyURL?action=view&type=XferLOGbad&host=${EscURI($host)}">$Lang->{XferLOG}</a>,
1430 <a href="$MyURL?action=view&type=XferErrbad&host=${EscURI($host)}">$Lang->{Errors}</a>
1435 (my $shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
1436 $statusStr .= <<EOF;
1437 <tr><td> ${HostLink($host)} </td>
1438 <td align="center"> $Status{$host}{type} </td>
1439 <td align="center"> ${UserLink($Hosts->{$host}{user})} </td>
1440 <td align="right"> $startTime </td>
1441 <td> $XferViewStr </td>
1442 <td align="right"> $errorTime </td>
1443 <td> ${EscHTML($shortErr)} </td></tr>
1446 my $now = timeStamp2(time);
1447 my $nextWakeupTime = timeStamp2($Info{nextWakeup});
1448 my $DUlastTime = timeStamp2($Info{DUlastValueTime});
1449 my $DUmaxTime = timeStamp2($Info{DUDailyMaxTime});
1450 my $numBgQueue = $QueueLen{BgQueue};
1451 my $numUserQueue = $QueueLen{UserQueue};
1452 my $numCmdQueue = $QueueLen{CmdQueue};
1453 my $serverStartTime = timeStamp2($Info{startTime});
1454 my $poolInfo = genPoolInfo("pool", \%Info);
1455 my $cpoolInfo = genPoolInfo("cpool", \%Info);
1456 if ( $Info{poolFileCnt} > 0 && $Info{cpoolFileCnt} > 0 ) {
1458 <li>Uncompressed pool:
1462 <li>Compressed pool:
1467 } elsif ( $Info{cpoolFileCnt} > 0 ) {
1468 $poolInfo = $cpoolInfo;
1471 Header($Lang->{H_BackupPC_Server_Status});
1472 print (eval ("qq{$Lang->{BackupPC_Server_Status}}"));
1476 sub Action_RestoreInfo
1478 my $Privileged = CheckPermission($In{host});
1479 my $host = $1 if ( $In{host} =~ /(.*)/ );
1483 if ( !$Privileged ) {
1484 ErrorExit($Lang->{Only_privileged_users_can_view_restore_information});
1487 # Find the requested restore
1489 my @Restores = $bpc->RestoreInfoRead($host);
1490 for ( $i = 0 ; $i < @Restores ; $i++ ) {
1491 last if ( $Restores[$i]{num} == $num );
1493 if ( $i >= @Restores ) {
1494 ErrorExit(eval("qq{$Lang->{Restore_number__num_for_host__does_not_exist}}"));
1498 do "$TopDir/pc/$host/RestoreInfo.$Restores[$i]{num}"
1499 if ( -f "$TopDir/pc/$host/RestoreInfo.$Restores[$i]{num}" );
1501 my $startTime = timeStamp2($Restores[$i]{startTime});
1502 my $reqTime = timeStamp2($RestoreReq{reqTime});
1503 my $dur = $Restores[$i]{endTime} - $Restores[$i]{startTime};
1504 $dur = 1 if ( $dur <= 0 );
1505 my $duration = sprintf("%.1f", $dur / 60);
1506 my $MB = sprintf("%.1f", $Restores[$i]{size} / (1024*1024));
1507 my $MBperSec = sprintf("%.2f", $Restores[$i]{size} / (1024*1024*$dur));
1509 my $fileListStr = "";
1510 foreach my $f ( @{$RestoreReq{fileList}} ) {
1511 my $targetFile = $f;
1512 (my $strippedShareSrc = $RestoreReq{shareSrc}) =~ s/^\///;
1513 (my $strippedShareDest = $RestoreReq{shareDest}) =~ s/^\///;
1514 substr($targetFile, 0, length($RestoreReq{pathHdrSrc}))
1515 = $RestoreReq{pathHdrDest};
1516 $fileListStr .= <<EOF;
1517 <tr><td>$RestoreReq{hostSrc}:/$strippedShareSrc$f</td><td>$RestoreReq{hostDest}:/$strippedShareDest$targetFile</td></tr>
1521 Header(eval("qq{$Lang->{Restore___num_details_for__host}}"));
1522 print(eval("qq{$Lang->{Restore___num_details_for__host2 }}"));
1526 ###########################################################################
1527 # Miscellaneous subroutines
1528 ###########################################################################
1532 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
1533 = localtime($_[0] == 0 ? time : $_[0] );
1536 if ( $Conf{CgiDateFormatMMDD} ) {
1537 return sprintf("$mon/$mday %02d:%02d", $hour, $min);
1539 return sprintf("$mday/$mon %02d:%02d", $hour, $min);
1547 if ( defined($Hosts->{$host}) || defined($Status{$host}) ) {
1548 $s = "<a href=\"$MyURL?host=${EscURI($host)}\">$host</a>";
1560 return \$user if ( $user eq ""
1561 || $Conf{CgiUserUrlCreate} eq "" );
1562 if ( $Conf{CgiUserHomePageCheck} eq ""
1563 || -f sprintf($Conf{CgiUserHomePageCheck}, $user, $user, $user) ) {
1565 . sprintf($Conf{CgiUserUrlCreate}, $user, $user, $user)
1577 $s =~ s/\"/"/g;
1580 $s =~ s{([^[:print:]])}{sprintf("&\#x%02X;", ord($1));}eg;
1587 $s =~ s{([^\w.\/-])}{sprintf("%%%02X", ord($1));}eg;
1594 my($head) = shift(@mesg);
1595 my($mesg) = join("</p>\n<p>", @mesg);
1596 $Conf{CgiHeaderFontType} ||= "arial";
1597 $Conf{CgiHeaderFontSize} ||= "3";
1598 $Conf{CgiNavBarBgColor} ||= "#ddeeee";
1599 $Conf{CgiHeaderBgColor} ||= "#99cc33";
1601 $bpc->ServerMesg("log User $User (host=$In{host}) got CGI error: $head")
1602 if ( defined($bpc) );
1603 if ( !defined($Lang->{Error}) ) {
1604 Header("BackupPC: Error");
1605 $mesg = <<EOF if ( !defined($mesg) );
1606 There is some problem with the BackupPC installation.
1607 Please check the permissions on BackupPC_Admin.
1610 ${h1("Error: Unable to read config.pl or language strings!!")}
1615 Header(eval("qq{$Lang->{Error}}"));
1616 print (eval("qq{$Lang->{Error____head}}"));
1625 # Verify that the server connection is ok
1627 return if ( $bpc->ServerOK() );
1628 $bpc->ServerDisconnect();
1629 if ( my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}) ) {
1630 ErrorExit(eval("qq{$Lang->{Unable_to_connect_to_BackupPC_server}}"));
1638 my $reply = $bpc->ServerMesg("status $status");
1639 $reply = $1 if ( $reply =~ /(.*)/s );
1641 # ignore status related to admin and trashClean jobs
1642 if ( $status =~ /\bhosts\b/ ) {
1643 delete($Status{$bpc->adminJob});
1644 delete($Status{$bpc->trashJob});
1648 sub ReadUserEmailInfo
1650 if ( (stat("$TopDir/log/UserEmailInfo.pl"))[9] != $UserEmailInfoMTime ) {
1651 do "$TopDir/log/UserEmailInfo.pl";
1652 $UserEmailInfoMTime = (stat("$TopDir/log/UserEmailInfo.pl"))[9];
1657 # Check if the user is privileged. A privileged user can access
1658 # any information (backup files, logs, status pages etc).
1660 # A user is privileged if they belong to the group
1661 # $Conf{CgiAdminUserGroup}, or they are in $Conf{CgiAdminUsers}
1662 # or they are the user assigned to a host in the host file.
1669 return 0 if ( $User eq "" || ($host ne "" && !defined($Hosts->{$host})) );
1670 if ( $Conf{CgiAdminUserGroup} ne "" ) {
1671 my($n,$p,$gid,$mem) = getgrnam($Conf{CgiAdminUserGroup});
1672 $Privileged ||= ($mem =~ /\b$User\b/);
1674 if ( $Conf{CgiAdminUsers} ne "" ) {
1675 $Privileged ||= ($Conf{CgiAdminUsers} =~ /\b$User\b/);
1676 $Privileged ||= $Conf{CgiAdminUsers} eq "*";
1678 $PrivAdmin = $Privileged;
1679 $Privileged ||= $User eq $Hosts->{$host}{user};
1680 $Privileged ||= defined($Hosts->{$host}{moreUsers}{$User});
1686 # Returns the list of hosts that should appear in the navigation bar
1687 # for this user. If $Conf{CgiNavBarAdminAllHosts} is set, the admin
1688 # gets all the hosts. Otherwise, regular users get hosts for which
1689 # they are the user or are listed in the moreUsers column in the
1694 if ( $Conf{CgiNavBarAdminAllHosts} && CheckPermission() ) {
1695 return sort keys %$Hosts;
1698 return sort grep { $Hosts->{$_}{user} eq $User ||
1699 defined($Hosts->{$_}{moreUsers}{$User}) } keys(%$Hosts);
1703 # Given a host name tries to find the IP address. For non-dhcp hosts
1704 # we just return the host name. For dhcp hosts we check the address
1705 # the user is using ($ENV{REMOTE_ADDR}) and also the last-known IP
1706 # address for $host. (Later we should replace this with a broadcast
1709 sub ConfirmIPAddress
1714 if ( $Hosts->{$host}{dhcp}
1715 && $ENV{REMOTE_ADDR} =~ /^(\d+[\.\d]*)$/ ) {
1717 my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($ipAddr);
1718 if ( $netBiosHost ne $host ) {
1720 GetStatusInfo("host(${EscURI($host)})");
1721 if ( defined($StatusHost{dhcpHostIP})
1722 && $StatusHost{dhcpHostIP} ne $ipAddr ) {
1723 $tryIP = eval("qq{$Lang->{tryIP}}");
1724 ($netBiosHost, $netBiosUser)
1725 = $bpc->NetBiosInfoGet($StatusHost{dhcpHostIP});
1727 if ( $netBiosHost ne $host ) {
1728 ErrorExit(eval("qq{$Lang->{Can_t_find_IP_address_for}}"),
1729 eval("qq{$Lang->{host_is_a_DHCP_host}}"));
1731 $ipAddr = $StatusHost{dhcpHostIP};
1739 my($name, $info) = @_;
1740 my $poolSize = sprintf("%.2f", $info->{"${name}Kb"} / (1000 * 1024));
1741 my $poolRmSize = sprintf("%.2f", $info->{"${name}KbRm"} / (1000 * 1024));
1742 my $poolTime = timeStamp2($info->{"${name}Time"});
1743 $info->{"${name}FileCntRm"} = $info->{"${name}FileCntRm"} + 0;
1744 return eval("qq{$Lang->{Pool_Stat}}");
1747 ###########################################################################
1748 # HTML layout subroutines
1749 ###########################################################################
1755 { link => "", name => $Lang->{Status},
1757 { link => "?action=summary", name => $Lang->{PC_Summary} },
1758 { link => "?action=view&type=LOG", name => $Lang->{LOG_file} },
1759 { link => "?action=LOGlist", name => $Lang->{Old_LOGs} },
1760 { link => "?action=emailSummary", name => $Lang->{Email_summary} },
1761 { link => "?action=view&type=config", name => $Lang->{Config_file} },
1762 { link => "?action=view&type=hosts", name => $Lang->{Hosts_file} },
1763 { link => "?action=queue", name => $Lang->{Current_queues} },
1764 { link => "?action=view&type=docs", name => $Lang->{Documentation},
1766 { link => "http://backuppc.sourceforge.net/faq", name => "FAQ",
1768 { link => "http://backuppc.sourceforge.net", name => "SourceForge",
1771 print $Cgi->header();
1773 <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
1775 <title>$title</title>
1777 </head><body bgcolor="$Conf{CgiBodyBgColor}">
1778 <table cellpadding="0" cellspacing="0" border="0">
1779 <tr valign="top"><td valign="top" bgcolor="$Conf{CgiNavBarBgColor}" width="10%">
1781 NavSectionTitle("BackupPC");
1783 if ( defined($In{host}) && defined($Hosts->{$In{host}}) ) {
1784 my $host = $In{host};
1785 NavSectionTitle( eval("qq{$Lang->{Host_Inhost}}") );
1787 NavLink("?host=${EscURI($host)}", $Lang->{Home});
1788 NavLink("?action=view&type=LOG&host=${EscURI($host)}", $Lang->{LOG_file});
1789 NavLink("?action=LOGlist&host=${EscURI($host)}", $Lang->{Old_LOGs});
1790 if ( -f "$TopDir/pc/$host/SmbLOG.bad"
1791 || -f "$TopDir/pc/$host/SmbLOG.bad.z"
1792 || -f "$TopDir/pc/$host/XferLOG.bad"
1793 || -f "$TopDir/pc/$host/XferLOG.bad.z" ) {
1794 NavLink("?action=view&type=XferLOGbad&host=${EscURI($host)}",
1795 $Lang->{Last_bad_XferLOG});
1796 NavLink("?action=view&type=XferErrbad&host=${EscURI($host)}",
1797 $Lang->{Last_bad_XferLOG_errors_only});
1799 if ( -f "$TopDir/pc/$host/config.pl" ) {
1800 NavLink("?action=view&type=config&host=${EscURI($host)}", $Lang->{Config_file});
1804 NavSectionTitle($Lang->{Hosts});
1805 if ( defined($Hosts) && %$Hosts > 0 ) {
1807 foreach my $host ( GetUserHosts() ) {
1808 NavLink("?host=${EscURI($host)}", $host);
1813 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1814 <tr><td>$Lang->{Host_or_User_name}</td>
1815 <tr><td><form action="$MyURL" method="get"><small>
1816 <input type="text" name="host" size="10" maxlength="64">
1817 <input type="hidden" name="action" value="hostInfo"><input type="submit" value="$Lang->{Go}" name="ignore">
1818 </small></form></td></tr>
1821 NavSectionTitle($Lang->{NavSectionTitle_});
1823 foreach my $l ( @adminLinks ) {
1824 if ( $PrivAdmin || $l->{priv} ) {
1825 NavLink($l->{link}, $l->{name});
1827 NavLink(undef, $l->{name});
1832 </td><td valign="top" width="5"> </td>
1833 <td valign="top" width="90%">
1850 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1851 <tr><td bgcolor="$Conf{CgiHeaderBgColor}"><font face="$Conf{CgiHeaderFontType}"
1852 size="$Conf{CgiHeaderFontSize}"><b>$head</b>
1862 $padding = 2 if ( !defined($padding) );
1864 <table cellpadding="$padding" cellspacing="0" border="0" width="100%">
1875 my($link, $text) = @_;
1876 print "<tr><td width=\"2%\" valign=\"top\"><b>·</b></td>";
1877 if ( defined($link) ) {
1878 $link = "$MyURL$link" if ( $link eq "" || $link =~ /^\?/ );
1880 <td width="98%"><a href="$link"><small>$text</small></a></td></tr>
1884 <td width="98%"><small>$text</small></td></tr>
1893 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1895 <td bgcolor="$Conf{CgiHeaderBgColor}"> <font face="$Conf{CgiHeaderFontType}"
1896 size="$Conf{CgiHeaderFontSize}"><b>$str</b></font>
1906 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1908 <td bgcolor="$Conf{CgiHeaderBgColor}"> <font face="$Conf{CgiHeaderFontType}"
1909 size="$Conf{CgiHeaderFontSize}"><b>$str</b></font>