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.0beta2, released 13 Apr 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);
161 my @Backups = $bpc->BackupInfoRead($host);
162 my $fullCnt = $incrCnt = 0;
163 my $fullAge = $incrAge = -1;
164 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
165 if ( $Backups[$i]{type} eq "full" ) {
167 if ( $fullAge < 0 || $Backups[$i]{startTime} > $fullAge ) {
168 $fullAge = $Backups[$i]{startTime};
169 $fullSize = $Backups[$i]{size} / (1024 * 1024);
170 $fullDur = $Backups[$i]{endTime} - $Backups[$i]{startTime};
172 $fullSizeTot += $Backups[$i]{size} / (1024 * 1024);
175 if ( $incrAge < 0 || $Backups[$i]{startTime} > $incrAge ) {
176 $incrAge = $Backups[$i]{startTime};
178 $incrSizeTot += $Backups[$i]{size} / (1024 * 1024);
181 if ( $fullAge < 0 ) {
185 $fullAge = sprintf("%.1f", (time - $fullAge) / (24 * 3600));
186 $fullRate = sprintf("%.2f",
187 $fullSize / ($fullDur <= 0 ? 1 : $fullDur));
189 if ( $incrAge < 0 ) {
192 $incrAge = sprintf("%.1f", (time - $incrAge) / (24 * 3600));
194 $fullTot += $fullCnt;
195 $incrTot += $incrCnt;
196 $fullSize = sprintf("%.2f", $fullSize / 1000);
197 $incrAge = " " if ( $incrAge eq "" );
198 $reasonHilite = $Conf{CgiStatusHilightColor}{$Status{$host}{reason}};
199 $reasonHilite = " bgcolor=\"$reasonHilite\"" if ( $reasonHilite ne "" );
200 if ( $Status{$host}{state} ne "Status_backup_in_progress"
201 && $Status{$host}{state} ne "Status_restore_in_progress"
202 && $Status{$host}{error} ne "" ) {
203 ($shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
204 $shortErr = " ($shortErr)";
208 <tr$reasonHilite><td> ${HostLink($host)} </td>
209 <td align="center"> ${UserLink(defined($Hosts->{$host})
210 ? $Hosts->{$host}{user} : "")} </td>
211 <td align="center"> $fullCnt </td>
212 <td align="center"> $fullAge </td>
213 <td align="center"> $fullSize </td>
214 <td align="center"> $fullRate </td>
215 <td align="center"> $incrCnt </td>
216 <td align="center"> $incrAge </td>
217 <td align="center"> $Lang->{$Status{$host}{state}} </td>
218 <td> $Lang->{$Status{$host}{reason}}$shortErr </td></tr>
220 if ( @Backups == 0 ) {
228 $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
229 $incrSizeTot = sprintf("%.2f", $incrSizeTot / 1000);
230 my $now = timeStamp2(time);
232 Header($Lang->{BackupPC__Server_Summary});
233 print eval ("qq{$Lang->{BackupPC_Summary}}");
238 sub Action_StartStopBackup
242 my $start = 1 if ( $In{action} eq $Lang->{Start_Incr_Backup}
243 || $In{action} eq $Lang->{Start_Full_Backup} );
244 my $doFull = $In{action} eq $Lang->{Start_Full_Backup} ? 1 : 0;
245 my $type = $doFull ? "full" : "incremental";
246 my $host = $In{host};
247 my $Privileged = CheckPermission($host);
249 if ( !$Privileged ) {
250 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_stop_or_start_backups}}"));
256 if ( $Hosts->{$host}{dhcp} ) {
257 $reply = $bpc->ServerMesg("backup $In{hostIP} ${EscURI($host)}"
259 $str = eval("qq{$Lang->{Backup_requested_on_DHCP__host}}");
261 $reply = $bpc->ServerMesg("backup ${EscURI($host)}"
262 . " ${EscURI($host)} $User $doFull");
263 $str = eval("qq{$Lang->{Backup_requested_on__host_by__User}}");
266 $reply = $bpc->ServerMesg("stop ${EscURI($host)} $User $In{backoff}");
267 $str = eval("qq{$Lang->{Backup_stopped_dequeued_on__host_by__User}}");
270 Header(eval ("qq{$Lang->{BackupPC__Backup_Requested_on__host}}") );
271 print (eval ("qq{$Lang->{REPLY_FROM_SERVER}}"));
276 my $ipAddr = ConfirmIPAddress($host);
278 Header(eval("qq{$Lang->{BackupPC__Start_Backup_Confirm_on__host}}"));
279 print (eval("qq{$Lang->{Are_you_sure_start}}"));
282 GetStatusInfo("host(${EscURI($host)})");
283 if ( $StatusHost{backoffTime} > time ) {
284 $backoff = sprintf("%.1f",
285 ($StatusHost{backoffTime} - time) / 3600);
287 Header($Lang->{BackupPC__Stop_Backup_Confirm_on__host});
288 print (eval ("qq{$Lang->{Are_you_sure_stop}}"));
296 my($strBg, $strUser, $strCmd);
298 GetStatusInfo("queues");
299 my $Privileged = CheckPermission();
301 if ( !$Privileged ) {
302 ErrorExit($Lang->{Only_privileged_users_can_view_queues_});
306 my $req = pop(@BgQueue);
307 my($reqTime) = timeStamp2($req->{reqTime});
309 <tr><td> ${HostLink($req->{host})} </td>
310 <td align="center"> $reqTime </td>
311 <td align="center"> $req->{user} </td></tr>
314 while ( @UserQueue ) {
315 my $req = pop(@UserQueue);
316 my $reqTime = timeStamp2($req->{reqTime});
318 <tr><td> ${HostLink($req->{host})} </td>
319 <td align="center"> $reqTime </td>
320 <td align="center"> $req->{user} </td></tr>
323 while ( @CmdQueue ) {
324 my $req = pop(@CmdQueue);
325 my $reqTime = timeStamp2($req->{reqTime});
326 (my $cmd = $req->{cmd}[0]) =~ s/$BinDir\///;
328 <tr><td> ${HostLink($req->{host})} </td>
329 <td align="center"> $reqTime </td>
330 <td align="center"> $req->{user} </td>
331 <td> $cmd $req->{cmd}[0] </td></tr>
334 Header($Lang->{BackupPC__Queue_Summary});
336 print ( eval ( "qq{$Lang->{Backup_Queue_Summary}}") );
343 my $Privileged = CheckPermission($In{host});
346 my $host = $In{host};
348 my $type = $In{type};
351 my $ext = $num ne "" ? ".$num" : "";
353 ErrorExit(eval("qq{$Lang->{Invalid_number__num}}")) if ( $num ne "" && $num !~ /^\d+$/ );
354 if ( $type eq "XferLOG" ) {
355 $file = "$TopDir/pc/$host/SmbLOG$ext";
356 $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
357 } elsif ( $type eq "XferLOGbad" ) {
358 $file = "$TopDir/pc/$host/SmbLOG.bad";
359 $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
360 } elsif ( $type eq "XferErrbad" ) {
361 $file = "$TopDir/pc/$host/SmbLOG.bad";
362 $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
363 $comment = $Lang->{Extracting_only_Errors};
364 } elsif ( $type eq "XferErr" ) {
365 $file = "$TopDir/pc/$host/SmbLOG$ext";
366 $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
367 $comment = $Lang->{Extracting_only_Errors};
368 } elsif ( $type eq "RestoreLOG" ) {
369 $file = "$TopDir/pc/$host/RestoreLOG$ext";
370 } elsif ( $type eq "RestoreErr" ) {
371 $file = "$TopDir/pc/$host/RestoreLOG$ext";
372 $comment = $Lang->{Extracting_only_Errors};
373 } elsif ( $host ne "" && $type eq "config" ) {
374 $file = "$TopDir/pc/$host/config.pl";
375 $file = "$TopDir/conf/$host.pl"
376 if ( $host ne "config" && -f "$TopDir/conf/$host.pl"
378 } elsif ( $type eq "docs" ) {
379 $file = "$BinDir/../doc/BackupPC.html";
380 if ( open(LOG, $file) ) {
381 Header($Lang->{BackupPC__Documentation});
382 print while ( <LOG> );
386 ErrorExit(eval("qq{$Lang->{Unable_to_open__file__configuration_problem}}"));
389 } elsif ( $type eq "config" ) {
390 $file = "$TopDir/conf/config.pl";
391 } elsif ( $type eq "hosts" ) {
392 $file = "$TopDir/conf/hosts";
393 } elsif ( $host ne "" ) {
394 $file = "$TopDir/pc/$host/LOG$ext";
396 $file = "$TopDir/log/LOG$ext";
399 if ( !$Privileged ) {
400 ErrorExit($Lang->{Only_privileged_users_can_view_log_or_config_files});
402 if ( !-f $file && -f "$file.z" ) {
406 Header(eval("qq{$Lang->{Backup_PC__Log_File__file}}") );
407 print( eval ("qq{$Lang->{Log_File__file__comment}}"));
408 if ( defined($fh = BackupPC::FileZIO->open($file, 0, $compress)) ) {
409 my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
411 print ( eval ("qq{$Lang->{Contents_of_log_file}}"));
414 if ( $type eq "XferErr" || $type eq "XferErrbad"
415 || $type eq "RestoreErr" ) {
418 $_ = $fh->readLine();
420 print(eval ("qq{$Lang->{skipped__skipped_lines}}"))
425 || /^\s*(\d+) \(\s*\d+\.\d kb\/s\) (.*)$/
426 || /^tar: dumped \d+ files/
427 || /^added interface/i
428 || /^restore tar file /i
429 || /^restore directory /i
430 || /^tarmode is now/i
431 || /^Total bytes written/i
433 || /^Getting files newer than/i
434 || /^Output is \/dev\/null/
435 || /^\([\d\.]* kb\/s\) \(average [\d\.]* kb\/s\)$/
436 || /^\s+directory \\/
444 print(eval("qq{$Lang->{skipped__skipped_lines}}"))
447 print ${EscHTML($_)};
449 } elsif ( $linkHosts ) {
451 $_ = $fh->readLine();
452 last if ( $_ eq "" );
453 my $s = ${EscHTML($_)};
454 $s =~ s/\b([\w-]+)\b/defined($Hosts->{$1})
455 ? ${HostLink($1)} : $1/eg;
458 } elsif ( $type eq "config" ) {
460 $_ = $fh->readLine();
461 last if ( $_ eq "" );
462 # remove any passwords and user names
463 s/(SmbSharePasswd.*=.*['"]).*(['"])/$1$2/ig;
464 s/(SmbShareUserName.*=.*['"]).*(['"])/$1$2/ig;
465 s/(ServerMesgSecret.*=.*['"]).*(['"])/$1$2/ig;
466 print ${EscHTML($_)};
470 $_ = $fh->readLine();
471 last if ( $_ eq "" );
472 print ${EscHTML($_)};
477 printf( eval("qq{$Lang->{_pre___Can_t_open_log_file__file}}"));
487 my $Privileged = CheckPermission($In{host});
489 if ( !$Privileged ) {
490 ErrorExit($Lang->{Only_privileged_users_can_view_log_files});
492 my $host = $In{host};
493 my($url0, $hdr, $root, $str);
495 $root = "$TopDir/pc/$host/LOG";
496 $url0 = "&host=${EscURI($host)}";
497 $hdr = "for host $host";
499 $root = "$TopDir/log/LOG";
503 for ( my $i = -1 ; ; $i++ ) {
510 $file .= ".z" if ( !-f $file && -f "$file.z" );
511 last if ( !-f $file );
512 my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
513 my $size = (stat($file))[7];
515 <tr><td> <a href="$MyURL?action=view&type=LOG$url0$url1"><tt>$file</tt></a> </td>
516 <td align="right"> $size </td>
517 <td> $mtimeStr </td></tr>
520 Header($Lang->{BackupPC__Log_File_History});
521 print (eval("qq{$Lang->{Log_File_History__hdr}}"));
525 sub Action_EmailSummary
527 my $Privileged = CheckPermission();
529 if ( !$Privileged ) {
530 ErrorExit($Lang->{Only_privileged_users_can_view_email_summaries});
532 GetStatusInfo("hosts");
535 foreach my $u ( keys(%UserEmailInfo) ) {
536 next if ( !defined($UserEmailInfo{$u}{lastTime}) );
537 my $emailTimeStr = timeStamp2($UserEmailInfo{$u}{lastTime});
538 $EmailStr{$UserEmailInfo{$u}{lastTime}} .= <<EOF;
539 <tr><td>${UserLink($u)} </td>
540 <td>${HostLink($UserEmailInfo{$u}{lastHost})} </td>
541 <td>$emailTimeStr </td>
542 <td>$UserEmailInfo{$u}{lastSubj} </td></tr>
545 foreach my $t ( sort({$b <=> $a} keys(%EmailStr)) ) {
546 $str .= $EmailStr{$t};
548 Header($Lang->{Email_Summary});
549 print (eval("qq{$Lang->{Recent_Email_Summary}}"));
555 my $Privileged = CheckPermission($In{host});
556 my($i, $dirStr, $fileStr, $attr);
559 if ( !$Privileged ) {
560 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_browse_backup_files}}"));
562 my $host = $In{host};
564 my $share = $In{share};
567 ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
569 # Find the requested backup and the previous filled backup
571 my @Backups = $bpc->BackupInfoRead($host);
572 for ( $i = 0 ; $i < @Backups ; $i++ ) {
573 last if ( $Backups[$i]{num} == $num );
575 if ( $i >= @Backups ) {
576 ErrorExit("Backup number $num for host ${EscHTML($host)} does"
579 my $backupTime = timeStamp2($Backups[$i]{startTime});
580 my $backupAge = sprintf("%.1f", (time - $Backups[$i]{startTime})
582 my $view = BackupPC::View->new($bpc, $host, \@Backups);
584 if ( $dir eq "" || $dir eq "." || $dir eq ".." ) {
585 $attr = $view->dirAttrib($num, "", "");
586 if ( keys(%$attr) > 0 ) {
587 $share = (sort(keys(%$attr)))[0];
590 ErrorExit(eval("qq{$Lang->{Directory___EscHTML}}"));
593 $dir = "/$dir" if ( $dir !~ /^\// );
598 # Loop up the directory tree until we hit the top.
602 my($fLast, $fLastum, @DirStr);
604 $attr = $view->dirAttrib($num, $share, $relDir);
605 if ( !defined($attr) ) {
606 ErrorExit(eval("qq{$Lang->{Can_t_browse_bad_directory_name2}}"));
609 my $fileCnt = 0; # file counter
610 $fLast = $dirStr = "";
613 # Loop over each of the files in this directory
615 foreach my $f ( sort(keys(%$attr)) ) {
616 my($dirOpen, $gotDir, $imgStr, $img, $path);
617 my $fURI = $f; # URI escaped $f
618 my $shareURI = $share; # URI escaped $share
619 if ( $relDir eq "" ) {
622 ($path = "$relDir/$f") =~ s{//+}{/}g;
624 if ( $shareURI eq "" ) {
629 $path =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
630 $fURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
631 $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
632 $dirOpen = 1 if ( defined($currDir) && $f eq $currDir );
633 if ( $attr->{$f}{type} == BPC_FTYPE_DIR ) {
635 # Display directory if it exists in current backup.
636 # First find out if there are subdirs
638 my($bold, $unbold, $BGcolor);
640 $img |= 1 << 5 if ( $attr->{$f}{nlink} > 2 );
645 $img |= 1 << 3 if ( $attr->{$f}{nlink} > 2 );
647 my $imgFileName = sprintf("%07b.gif", $img);
648 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
649 if ( "$relDir/$f" eq $dir ) {
650 $BGcolor = " bgcolor=\"$Conf{CgiHeaderBgColor}\"";
655 $dirName =~ s/ / /g;
656 push(@DirStr, {needTick => 1,
659 <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>
664 my($lastTick, $doneLastTick);
665 foreach my $d ( @DirStrPrev ) {
666 $lastTick = $d if ( $d->{needTick} );
668 $doneLastTick = 1 if ( !defined($lastTick) );
669 foreach my $d ( @DirStrPrev ) {
671 if ( $d->{needTick} ) {
674 if ( $d == $lastTick ) {
677 } elsif ( !$doneLastTick ) {
678 $img |= 1 << 3 | 1 << 4;
680 my $imgFileName = sprintf("%07b.gif", $img);
681 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
682 push(@DirStr, {needTick => 0,
683 tdArgs => $d->{tdArgs},
684 link => $imgStr . $d->{link}
689 if ( $relDir eq $dir ) {
691 # This is the selected directory, so display all the files
694 if ( defined($a = $attr->{$f}) ) {
695 my $mtimeStr = $bpc->timeStamp($a->{mtime});
697 my $typeStr = BackupPC::Attrib::fileType2Text(undef,
699 my $modeStr = sprintf("0%o", $a->{mode} & 07777);
701 <td align="center">$typeStr</td>
702 <td align="center">$modeStr</td>
703 <td align="center">$a->{backupNum}</td>
704 <td align="right">$a->{size}</td>
705 <td align="right">$mtimeStr</td>
709 $attrStr .= "<td colspan=\"5\" align=\"center\"> </td>\n";
711 (my $fDisp = "${EscHTML($f)}") =~ s/ / /g;
714 <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>
720 <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>
728 @DirStrPrev = @DirStr;
729 last if ( $relDir eq "" && $share eq "" );
731 # Prune the last directory off $relDir, or at the very end
732 # do the top-level directory.
734 if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) {
744 my $dirDisplay = "$share/$dir";
745 $dirDisplay =~ s{//+}{/}g;
746 $dirDisplay =~ s{/+$}{}g;
747 $dirDisplay = "/" if ( $dirDisplay eq "" );
750 if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) {
752 my $numF = join(", #", @mergeNums);
753 $filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}");
755 Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"));
757 foreach my $d ( @DirStrPrev ) {
758 $dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n";
761 ### hide checkall button if there are no files
762 my ($topCheckAll, $checkAll, $fileHeader);
764 $fileHeader = eval("qq{$Lang->{fileHeader}}");
766 $checkAll = $Lang->{checkAll};
768 # and put a checkall box on top if there are at least 20 files
769 if ( $checkBoxCnt >= 20 ) {
770 $topCheckAll = $checkAll;
771 $topCheckAll =~ s{allFiles}{allFilestop}g;
774 $fileStr = eval("qq{$Lang->{The_directory_is_empty}}");
777 foreach my $i ( $view->backupList($share, $dir) ) {
779 my $shareURI = $share;
780 $path =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
781 $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
782 push(@otherDirs, "<a href=\"$MyURL?action=browse&host=${EscURI($host)}&num=$i"
783 . "&share=$shareURI&dir=$path\">$i</a>");
787 my $otherDirs = join(",\n", @otherDirs);
788 $filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}");
790 print (eval("qq{$Lang->{Backup_browse_for__host}}"));
797 my $Privileged = CheckPermission($In{host});
798 if ( !$Privileged ) {
799 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files}}"));
801 my $host = $In{host};
803 my $share = $In{share};
804 my(@fileList, $fileListStr, $hiddenStr, $pathHdr, $badFileCnt);
805 my @Backups = $bpc->BackupInfoRead($host);
808 if ( !defined($Hosts->{$host}) ) {
809 ErrorExit(eval("qq{$Lang->{Bad_host_name}}"));
811 for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
812 next if ( !defined($In{"fcb$i"}) );
813 (my $name = $In{"fcb$i"}) =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
814 $badFileCnt++ if ( $name =~ m{(^|/)\.\.(/|$)} );
815 if ( @fileList == 0 ) {
818 while ( substr($name, 0, length($pathHdr)) ne $pathHdr ) {
819 $pathHdr = substr($pathHdr, 0, rindex($pathHdr, "/"));
822 push(@fileList, $name);
824 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
826 $fileListStr .= <<EOF;
827 <li> ${EscHTML($name)}
830 $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
831 $hiddenStr .= "<input type=\"hidden\" name=\"share\" value=\"${EscHTML($share)}\">\n";
832 $badFileCnt++ if ( $In{pathHdr} =~ m{(^|/)\.\.(/|$)} );
833 $badFileCnt++ if ( $In{num} =~ m{(^|/)\.\.(/|$)} );
834 if ( @fileList == 0 ) {
835 ErrorExit($Lang->{You_haven_t_selected_any_files__please_go_Back_to});
838 ErrorExit($Lang->{Nice_try__but_you_can_t_put});
840 if ( @fileList == 1 ) {
841 $pathHdr =~ s/(.*)\/.*/$1/;
843 $pathHdr = "/" if ( $pathHdr eq "" );
844 if ( $In{type} != 0 && @fileList == $In{fcbMax} ) {
846 # All the files in the list were selected, so just restore the
847 # entire parent directory
849 @fileList = ( $pathHdr );
851 if ( $In{type} == 0 ) {
853 # Tell the user what options they have
855 Header(eval("qq{$Lang->{Restore_Options_for__host}}"));
856 print(eval("qq{$Lang->{Restore_Options_for__host2}}"));
859 # Verify that Archive::Zip is available before showing the
862 if ( eval { require Archive::Zip } ) {
863 print (eval("qq{$Lang->{Option_2__Download_Zip_archive}}"));
865 print (eval("qq{$Lang->{Option_2__Download_Zip_archive2}}"));
867 print (eval("qq{$Lang->{Option_3__Download_Zip_archive}}"));
869 } elsif ( $In{type} == 1 ) {
871 # Provide the selected files via a tar archive.
873 my @fileListTrim = @fileList;
874 if ( @fileListTrim > 10 ) {
875 @fileListTrim = (@fileListTrim[0..9], '...');
877 $bpc->ServerMesg(eval("qq{$Lang->{log_User__User_downloaded_tar_archive_for__host}}"));
880 if ( $In{relative} ) {
881 @pathOpts = ("-r", $pathHdr, "-p", "");
884 Content-Type: application/x-gtar
885 Content-Transfer-Encoding: binary
886 Content-Disposition: attachment; filename=\"restore.tar\"
890 # Fork the child off and manually copy the output to our stdout.
891 # This is necessary to ensure the output gets to the correct place
894 $bpc->cmdSystemOrEval(["$BinDir/BackupPC_tarCreate",
903 } elsif ( $In{type} == 2 ) {
905 # Provide the selected files via a zip archive.
907 my @fileListTrim = @fileList;
908 if ( @fileListTrim > 10 ) {
909 @fileListTrim = (@fileListTrim[0..9], '...');
911 $bpc->ServerMesg(eval("qq{$Lang->{log_User__User_downloaded_zip_archive_for__host}}"));
914 if ( $In{relative} ) {
915 @pathOpts = ("-r", $pathHdr, "-p", "");
918 Content-Type: application/zip
919 Content-Transfer-Encoding: binary
920 Content-Disposition: attachment; filename=\"restore.zip\"
923 $In{compressLevel} = 5 if ( $In{compressLevel} !~ /^\d+$/ );
925 # Fork the child off and manually copy the output to our stdout.
926 # This is necessary to ensure the output gets to the correct place
929 $bpc->cmdSystemOrEval(["$BinDir/BackupPC_zipCreate",
932 "-c", $In{compressLevel},
939 } elsif ( $In{type} == 3 ) {
941 # Do restore directly onto host
943 if ( !defined($Hosts->{$In{hostDest}}) ) {
944 ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
946 if ( !CheckPermission($In{hostDest}) ) {
947 ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
950 foreach my $f ( @fileList ) {
952 (my $strippedShare = $share) =~ s/^\///;
953 (my $strippedShareDest = $In{shareDest}) =~ s/^\///;
954 substr($targetFile, 0, length($pathHdr)) = $In{pathHdr};
955 $fileListStr .= <<EOF;
956 <tr><td>$host:/$strippedShare$f</td><td>$In{hostDest}:/$strippedShareDest$targetFile</td></tr>
959 Header(eval("qq{$Lang->{Restore_Confirm_on__host}}"));
960 print(eval("qq{$Lang->{Are_you_sure}}"));
962 } elsif ( $In{type} == 4 ) {
963 if ( !defined($Hosts->{$In{hostDest}}) ) {
964 ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
966 if ( !CheckPermission($In{hostDest}) ) {
967 ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
969 my $hostDest = $1 if ( $In{hostDest} =~ /(.+)/ );
970 my $ipAddr = ConfirmIPAddress($hostDest);
972 # Prepare and send the restore request. We write the request
973 # information using Data::Dumper to a unique file,
974 # $TopDir/pc/$hostDest/restoreReq.$$.n. We use a file
975 # in case the list of files to restore is very long.
978 for ( my $i = 0 ; ; $i++ ) {
979 $reqFileName = "restoreReq.$$.$i";
980 last if ( !-f "$TopDir/pc/$hostDest/$reqFileName" );
983 # source of restore is hostSrc, #num, path shareSrc/pathHdrSrc
987 pathHdrSrc => $pathHdr,
989 # destination of restore is hostDest:shareDest/pathHdrDest
990 hostDest => $hostDest,
991 shareDest => $In{shareDest},
992 pathHdrDest => $In{pathHdr},
994 # list of files to restore
995 fileList => \@fileList,
1001 my($dump) = Data::Dumper->new(
1005 if ( open(REQ, ">$TopDir/pc/$hostDest/$reqFileName") ) {
1006 print(REQ $dump->Dump);
1009 ErrorExit(eval("qq{$Lang->{Can_t_open_create}}"));
1011 $reply = $bpc->ServerMesg("restore ${EscURI($ipAddr)}"
1012 . " ${EscURI($hostDest)} $User $reqFileName");
1013 $str = eval("qq{$Lang->{Restore_requested_to_host__hostDest__backup___num}}");
1014 Header(eval("qq{$Lang->{Restore_Requested_on__hostDest}}"));
1015 print (eval("qq{$Lang->{Reply_from_server_was___reply}}"));
1020 sub Action_RestoreFile
1022 restoreFile($In{host}, $In{num}, $In{share}, $In{dir});
1027 my($host, $num, $share, $dir, $skipHardLink, $origName) = @_;
1028 my($Privileged) = CheckPermission($host);
1031 # Some common content (media) types from www.iana.org (via MIME::Types).
1033 my $Ext2ContentType = {
1034 'asc' => 'text/plain',
1035 'avi' => 'video/x-msvideo',
1036 'bmp' => 'image/bmp',
1037 'book' => 'application/x-maker',
1038 'cc' => 'text/plain',
1039 'cpp' => 'text/plain',
1040 'csh' => 'application/x-csh',
1041 'csv' => 'text/comma-separated-values',
1042 'c' => 'text/plain',
1043 'deb' => 'application/x-debian-package',
1044 'doc' => 'application/msword',
1045 'dot' => 'application/msword',
1046 'dtd' => 'text/xml',
1047 'dvi' => 'application/x-dvi',
1048 'eps' => 'application/postscript',
1049 'fb' => 'application/x-maker',
1050 'fbdoc'=> 'application/x-maker',
1051 'fm' => 'application/x-maker',
1052 'frame'=> 'application/x-maker',
1053 'frm' => 'application/x-maker',
1054 'gif' => 'image/gif',
1055 'gtar' => 'application/x-gtar',
1056 'gz' => 'application/x-gzip',
1057 'hh' => 'text/plain',
1058 'hpp' => 'text/plain',
1059 'h' => 'text/plain',
1060 'html' => 'text/html',
1061 'htmlx'=> 'text/html',
1062 'htm' => 'text/html',
1063 'iges' => 'model/iges',
1064 'igs' => 'model/iges',
1065 'jpeg' => 'image/jpeg',
1066 'jpe' => 'image/jpeg',
1067 'jpg' => 'image/jpeg',
1068 'js' => 'application/x-javascript',
1069 'latex'=> 'application/x-latex',
1070 'maker'=> 'application/x-maker',
1071 'mid' => 'audio/midi',
1072 'midi' => 'audio/midi',
1073 'movie'=> 'video/x-sgi-movie',
1074 'mov' => 'video/quicktime',
1075 'mp2' => 'audio/mpeg',
1076 'mp3' => 'audio/mpeg',
1077 'mpeg' => 'video/mpeg',
1078 'mpg' => 'video/mpeg',
1079 'mpp' => 'application/vnd.ms-project',
1080 'pdf' => 'application/pdf',
1081 'pgp' => 'application/pgp-signature',
1082 'php' => 'application/x-httpd-php',
1083 'pht' => 'application/x-httpd-php',
1084 'phtml'=> 'application/x-httpd-php',
1085 'png' => 'image/png',
1086 'ppm' => 'image/x-portable-pixmap',
1087 'ppt' => 'application/powerpoint',
1088 'ppt' => 'application/vnd.ms-powerpoint',
1089 'ps' => 'application/postscript',
1090 'qt' => 'video/quicktime',
1091 'rgb' => 'image/x-rgb',
1092 'rtf' => 'application/rtf',
1093 'rtf' => 'text/rtf',
1094 'shar' => 'application/x-shar',
1095 'shtml'=> 'text/html',
1096 'swf' => 'application/x-shockwave-flash',
1097 'tex' => 'application/x-tex',
1098 'texi' => 'application/x-texinfo',
1099 'texinfo'=> 'application/x-texinfo',
1100 'tgz' => 'application/x-gtar',
1101 'tiff' => 'image/tiff',
1102 'tif' => 'image/tiff',
1103 'txt' => 'text/plain',
1104 'vcf' => 'text/x-vCard',
1105 'vrml' => 'model/vrml',
1106 'wav' => 'audio/x-wav',
1107 'wmls' => 'text/vnd.wap.wmlscript',
1108 'wml' => 'text/vnd.wap.wml',
1109 'wrl' => 'model/vrml',
1110 'xls' => 'application/vnd.ms-excel',
1111 'xml' => 'text/xml',
1112 'xwd' => 'image/x-xwindowdump',
1113 'z' => 'application/x-compress',
1114 'zip' => 'application/zip',
1115 %{$Conf{CgiExt2ContentType}}, # add site-specific values
1117 if ( !$Privileged ) {
1118 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files2}}"));
1121 ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
1123 $dir = "/" if ( $dir eq "" );
1124 my @Backups = $bpc->BackupInfoRead($host);
1125 my $view = BackupPC::View->new($bpc, $host, \@Backups);
1126 my $a = $view->fileAttrib($num, $share, $dir);
1127 if ( $dir =~ m{(^|/)\.\.(/|$)} || !defined($a) ) {
1128 ErrorExit("Can't restore bad file ${EscHTML($dir)}");
1130 my $f = BackupPC::FileZIO->open($a->{fullPath}, 0, $a->{compress});
1132 if ( !$skipHardLink && $a->{type} == BPC_FTYPE_HARDLINK ) {
1134 # hardlinks should look like the file they point to
1137 while ( $f->read(\$data, 65536) > 0 ) {
1141 $linkName =~ s/^\.\///;
1142 my $share = $1 if ( $dir =~ /^\/?(.*?)\// );
1143 restoreFile($host, $num, $share, $linkName, 1, $dir);
1146 $bpc->ServerMesg("log User $User recovered file $host/$num:$share/$dir ($a->{fullPath})");
1147 $dir = $origName if ( defined($origName) );
1148 my $ext = $1 if ( $dir =~ /\.([^\/\.]+)$/ );
1149 my $contentType = $Ext2ContentType->{lc($ext)}
1150 || "application/octet-stream";
1151 my $fileName = $1 if ( $dir =~ /.*\/(.*)/ );
1152 $fileName =~ s/"/\\"/g;
1153 print "Content-Type: $contentType\n";
1154 print "Content-Transfer-Encoding: binary\n";
1155 print "Content-Disposition: attachment; filename=\"$fileName\"\n\n";
1156 while ( $f->read(\$data, 1024 * 1024) > 0 ) {
1164 my $host = $1 if ( $In{host} =~ /(.*)/ );
1165 my($statusStr, $startIncrStr);
1169 return Action_GeneralInfo() if ( $host eq "" );
1171 if ( !-d "$TopDir/pc/$host" && -d "$TopDir/pc/" . lc($host) );
1172 if ( $host =~ /\.\./ || !-d "$TopDir/pc/$host" ) {
1174 # try to lookup by user name
1176 if ( !defined($Hosts->{$host}) ) {
1177 foreach my $h ( keys(%$Hosts) ) {
1178 if ( $Hosts->{$h}{user} eq $host
1179 || lc($Hosts->{$h}{user}) eq lc($host) ) {
1185 ErrorExit(eval("qq{$Lang->{Unknown_host_or_user}}"))
1186 if ( !defined($Hosts->{$host}) );
1190 GetStatusInfo("host(${EscURI($host)})");
1191 $bpc->ConfigRead($host);
1192 %Conf = $bpc->Conf();
1193 my $Privileged = CheckPermission($host);
1194 if ( !$Privileged ) {
1195 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_view_information_about}}"));
1197 ReadUserEmailInfo();
1199 my @Backups = $bpc->BackupInfoRead($host);
1200 my($str, $sizeStr, $compStr, $errStr, $warnStr);
1201 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
1202 my $startTime = timeStamp2($Backups[$i]{startTime});
1203 my $dur = $Backups[$i]{endTime} - $Backups[$i]{startTime};
1204 $dur = 1 if ( $dur <= 0 );
1205 my $duration = sprintf("%.1f", $dur / 60);
1206 my $MB = sprintf("%.1f", $Backups[$i]{size} / (1024*1024));
1207 my $MBperSec = sprintf("%.2f", $Backups[$i]{size} / (1024*1024*$dur));
1208 my $MBExist = sprintf("%.1f", $Backups[$i]{sizeExist} / (1024*1024));
1209 my $MBNew = sprintf("%.1f", $Backups[$i]{sizeNew} / (1024*1024));
1210 my($MBExistComp, $ExistComp, $MBNewComp, $NewComp);
1211 if ( $Backups[$i]{sizeExist} && $Backups[$i]{sizeExistComp} ) {
1212 $MBExistComp = sprintf("%.1f", $Backups[$i]{sizeExistComp}
1214 $ExistComp = sprintf("%.1f%%", 100 *
1215 (1 - $Backups[$i]{sizeExistComp} / $Backups[$i]{sizeExist}));
1217 if ( $Backups[$i]{sizeNew} && $Backups[$i]{sizeNewComp} ) {
1218 $MBNewComp = sprintf("%.1f", $Backups[$i]{sizeNewComp}
1220 $NewComp = sprintf("%.1f%%", 100 *
1221 (1 - $Backups[$i]{sizeNewComp} / $Backups[$i]{sizeNew}));
1223 my $age = sprintf("%.1f", (time - $Backups[$i]{startTime}) / (24*3600));
1224 my $browseURL = "$MyURL?action=browse&host=${EscURI($host)}&num=$Backups[$i]{num}";
1225 my $filled = $Backups[$i]{noFill} ? $Lang->{No} : $Lang->{Yes};
1226 $filled .= " ($Backups[$i]{fillFromNum}) "
1227 if ( $Backups[$i]{fillFromNum} ne "" );
1229 if ($Backups[$i]{type} eq "full") { $ltype = $Lang->{full}; }
1230 else { $ltype = $Lang->{incremental}; }
1232 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1233 <td align="center"> $ltype </td>
1234 <td align="center"> $filled </td>
1235 <td align="right"> $startTime </td>
1236 <td align="right"> $duration </td>
1237 <td align="right"> $age </td>
1238 <td align="left"> <tt>$TopDir/pc/$host/$Backups[$i]{num}</tt> </td></tr>
1241 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1242 <td align="center"> $ltype </td>
1243 <td align="right"> $Backups[$i]{nFiles} </td>
1244 <td align="right"> $MB </td>
1245 <td align="right"> $MBperSec </td>
1246 <td align="right"> $Backups[$i]{nFilesExist} </td>
1247 <td align="right"> $MBExist </td>
1248 <td align="right"> $Backups[$i]{nFilesNew} </td>
1249 <td align="right"> $MBNew </td>
1252 my $is_compress = $Backups[$i]{compress} || $Lang->{off};
1253 if (! $ExistComp) { $ExistComp = " "; }
1254 if (! $MBExistComp) { $MBExistComp = " "; }
1256 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1257 <td align="center"> $ltype </td>
1258 <td align="center"> $is_compress </td>
1259 <td align="right"> $MBExist </td>
1260 <td align="right"> $MBExistComp </td>
1261 <td align="right"> $ExistComp </td>
1262 <td align="right"> $MBNew </td>
1263 <td align="right"> $MBNewComp </td>
1264 <td align="right"> $NewComp </td>
1268 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1269 <td align="center"> $ltype </td>
1270 <td align="center"> <a href="$MyURL?action=view&type=XferLOG&num=$Backups[$i]{num}&host=${EscURI($host)}">$Lang->{XferLOG}</a>,
1271 <a href="$MyURL?action=view&type=XferErr&num=$Backups[$i]{num}&host=${EscURI($host)}">$Lang->{Errors}</a> </td>
1272 <td align="right"> $Backups[$i]{xferErrs} </td>
1273 <td align="right"> $Backups[$i]{xferBadFile} </td>
1274 <td align="right"> $Backups[$i]{xferBadShare} </td>
1275 <td align="right"> $Backups[$i]{tarErrs} </td></tr>
1279 my @Restores = $bpc->RestoreInfoRead($host);
1282 for ( my $i = 0 ; $i < @Restores ; $i++ ) {
1283 my $startTime = timeStamp2($Restores[$i]{startTime});
1284 my $dur = $Restores[$i]{endTime} - $Restores[$i]{startTime};
1285 $dur = 1 if ( $dur <= 0 );
1286 my $duration = sprintf("%.1f", $dur / 60);
1287 my $MB = sprintf("%.1f", $Restores[$i]{size} / (1024*1024));
1288 my $MBperSec = sprintf("%.2f", $Restores[$i]{size} / (1024*1024*$dur));
1289 my $Restores_Result = $Lang->{failed};
1290 if ($Restores[$i]{result} ne "failed") { $Restores_Result = $Lang->{success}; }
1291 $restoreStr .= <<EOF;
1292 <tr><td align="center"><a href="$MyURL?action=restoreInfo&num=$Restores[$i]{num}&host=${EscURI($host)}">$Restores[$i]{num}</a> </td>
1293 <td align="center"> $Restores_Result </td>
1294 <td align="right"> $startTime </td>
1295 <td align="right"> $duration </td>
1296 <td align="right"> $Restores[$i]{nFiles} </td>
1297 <td align="right"> $MB </td>
1298 <td align="right"> $Restores[$i]{tarCreateErrs} </td>
1299 <td align="right"> $Restores[$i]{xferErrs} </td>
1303 if ( $restoreStr ne "" ) {
1304 $restoreStr = eval("qq{$Lang->{Restore_Summary}}");
1306 if ( @Backups == 0 ) {
1307 $warnStr = $Lang->{This_PC_has_never_been_backed_up};
1309 if ( defined($Hosts->{$host}) ) {
1310 my $user = $Hosts->{$host}{user};
1311 my @moreUsers = sort(keys(%{$Hosts->{$host}{moreUsers}}));
1313 foreach my $u ( sort(keys(%{$Hosts->{$host}{moreUsers}})) ) {
1314 $moreUserStr .= ", " if ( $moreUserStr ne "" );
1315 $moreUserStr .= "${UserLink($u)}";
1317 if ( $moreUserStr ne "" ) {
1318 $moreUserStr = " ($Lang->{and} $moreUserStr).\n";
1320 $moreUserStr = ".\n";
1322 if ( $user ne "" ) {
1323 $statusStr .= eval("qq{$Lang->{This_PC_is_used_by}$moreUserStr}");
1325 if ( defined($UserEmailInfo{$user})
1326 && $UserEmailInfo{$user}{lastHost} eq $host ) {
1327 my $mailTime = timeStamp2($UserEmailInfo{$user}{lastTime});
1328 my $subj = $UserEmailInfo{$user}{lastSubj};
1329 $statusStr .= eval("qq{$Lang->{Last_email_sent_to__was_at___subject}}");
1332 if ( defined($Jobs{$host}) ) {
1333 my $startTime = timeStamp2($Jobs{$host}{startTime});
1334 (my $cmd = $Jobs{$host}{cmd}) =~ s/$BinDir\///g;
1335 $statusStr .= eval("qq{$Lang->{The_command_cmd_is_currently_running_for_started}}");
1337 if ( $StatusHost{BgQueueOn} ) {
1338 $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_background_queue_will_be_backed_up_soon}}");
1340 if ( $StatusHost{UserQueueOn} ) {
1341 $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_user_queue__will_be_backed_up_soon}}");
1343 if ( $StatusHost{CmdQueueOn} ) {
1344 $statusStr .= eval("qq{$Lang->{A_command_for_host_is_on_the_command_queue_will_run_soon}}");
1346 my $startTime = timeStamp2($StatusHost{endTime} == 0 ?
1347 $StatusHost{startTime} : $StatusHost{endTime});
1349 if ( $StatusHost{reason} ne "" ) {
1350 $reason = " ($Lang->{$StatusHost{reason}})";
1352 $statusStr .= eval("qq{$Lang->{Last_status_is_state_StatusHost_state_reason_as_of_startTime}}");
1354 if ( $StatusHost{state} ne "Status_backup_in_progress"
1355 && $StatusHost{state} ne "Status_restore_in_progress"
1356 && $StatusHost{error} ne "" ) {
1357 $statusStr .= eval("qq{$Lang->{Last_error_is____EscHTML_StatusHost_error}}");
1359 my $priorStr = "Pings";
1360 if ( $StatusHost{deadCnt} > 0 ) {
1361 $statusStr .= eval("qq{$Lang->{Pings_to_host_have_failed_StatusHost_deadCnt__consecutive_times}}");
1362 $priorStr = $Lang->{Prior_to_that__pings};
1364 if ( $StatusHost{aliveCnt} > 0 ) {
1365 $statusStr .= eval("qq{$Lang->{priorStr_to_host_have_succeeded_StatusHostaliveCnt_consecutive_times}}");
1367 if ( $StatusHost{aliveCnt} >= $Conf{BlackoutGoodCnt}
1368 && $Conf{BlackoutGoodCnt} >= 0 && $Conf{BlackoutHourBegin} >= 0
1369 && $Conf{BlackoutHourEnd} >= 0 ) {
1370 my(@days) = qw(Sun Mon Tue Wed Thu Fri Sat);
1371 my($days) = join(", ", @days[@{$Conf{BlackoutWeekDays}}]);
1372 my($t0) = sprintf("%d:%02d", $Conf{BlackoutHourBegin},
1373 60 * ($Conf{BlackoutHourBegin}
1374 - int($Conf{BlackoutHourBegin})));
1375 my($t1) = sprintf("%d:%02d", $Conf{BlackoutHourEnd},
1376 60 * ($Conf{BlackoutHourEnd}
1377 - int($Conf{BlackoutHourEnd})));
1378 $statusStr .= eval("qq{$Lang->{Because__host_has_been_on_the_network_at_least__Conf_BlackoutGoodCnt_consecutive_times___}}");
1381 if ( $StatusHost{backoffTime} > time ) {
1382 my $hours = sprintf("%.1f", ($StatusHost{backoffTime} - time) / 3600);
1383 $statusStr .= eval("qq{$Lang->{Backups_are_deferred_for_hours_hours_change_this_number}}");
1387 # only allow incremental if there are already some backups
1388 $startIncrStr = <<EOF;
1389 <input type="submit" value="\$Lang->{Start_Incr_Backup}" name="action">
1393 $startIncrStr = eval ("qq{$startIncrStr}");
1395 Header(eval("qq{$Lang->{Host__host_Backup_Summary}}"));
1396 print(eval("qq{$Lang->{Host__host_Backup_Summary2}}"));
1400 sub Action_GeneralInfo
1402 GetStatusInfo("info jobs hosts queueLen");
1403 my $Privileged = CheckPermission();
1405 my($jobStr, $statusStr);
1406 foreach my $host ( sort(keys(%Jobs)) ) {
1407 my $startTime = timeStamp2($Jobs{$host}{startTime});
1408 next if ( $host eq $bpc->trashJob
1409 && $Jobs{$host}{processState} ne "running" );
1410 $Jobs{$host}{type} = $Status{$host}{type}
1411 if ( $Jobs{$host}{type} eq "" && defined($Status{$host}));
1412 (my $cmd = $Jobs{$host}{cmd}) =~ s/$BinDir\///g;
1413 (my $xferPid = $Jobs{$host}{xferPid}) =~ s/,/, /g;
1415 <tr><td> ${HostLink($host)} </td>
1416 <td align="center"> $Jobs{$host}{type} </td>
1417 <td align="center"> ${UserLink(defined($Hosts->{$host})
1418 ? $Hosts->{$host}{user} : "")} </td>
1419 <td> $startTime </td>
1421 <td align="center"> $Jobs{$host}{pid} </td>
1422 <td align="center"> $xferPid </td>
1424 $jobStr .= "</tr>\n";
1426 foreach my $host ( sort(keys(%Status)) ) {
1427 next if ( $Status{$host}{reason} ne "Reason_backup_failed"
1428 && (!$Status{$host}{userReq}
1429 || $Status{$host}{reason} ne "Reason_no_ping") );
1430 my $startTime = timeStamp2($Status{$host}{startTime});
1431 my($errorTime, $XferViewStr);
1432 if ( $Status{$host}{errorTime} > 0 ) {
1433 $errorTime = timeStamp2($Status{$host}{errorTime});
1435 if ( -f "$TopDir/pc/$host/SmbLOG.bad"
1436 || -f "$TopDir/pc/$host/SmbLOG.bad.z"
1437 || -f "$TopDir/pc/$host/XferLOG.bad"
1438 || -f "$TopDir/pc/$host/XferLOG.bad.z"
1440 $XferViewStr = <<EOF;
1441 <a href="$MyURL?action=view&type=XferLOGbad&host=${EscURI($host)}">$Lang->{XferLOG}</a>,
1442 <a href="$MyURL?action=view&type=XferErrbad&host=${EscURI($host)}">$Lang->{Errors}</a>
1447 (my $shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
1448 $statusStr .= <<EOF;
1449 <tr><td> ${HostLink($host)} </td>
1450 <td align="center"> $Status{$host}{type} </td>
1451 <td align="center"> ${UserLink(defined($Hosts->{$host})
1452 ? $Hosts->{$host}{user} : "")} </td>
1453 <td align="right"> $startTime </td>
1454 <td> $XferViewStr </td>
1455 <td align="right"> $errorTime </td>
1456 <td> ${EscHTML($shortErr)} </td></tr>
1459 my $now = timeStamp2(time);
1460 my $nextWakeupTime = timeStamp2($Info{nextWakeup});
1461 my $DUlastTime = timeStamp2($Info{DUlastValueTime});
1462 my $DUmaxTime = timeStamp2($Info{DUDailyMaxTime});
1463 my $numBgQueue = $QueueLen{BgQueue};
1464 my $numUserQueue = $QueueLen{UserQueue};
1465 my $numCmdQueue = $QueueLen{CmdQueue};
1466 my $serverStartTime = timeStamp2($Info{startTime});
1467 my $poolInfo = genPoolInfo("pool", \%Info);
1468 my $cpoolInfo = genPoolInfo("cpool", \%Info);
1469 if ( $Info{poolFileCnt} > 0 && $Info{cpoolFileCnt} > 0 ) {
1471 <li>Uncompressed pool:
1475 <li>Compressed pool:
1480 } elsif ( $Info{cpoolFileCnt} > 0 ) {
1481 $poolInfo = $cpoolInfo;
1484 Header($Lang->{H_BackupPC_Server_Status});
1485 print (eval ("qq{$Lang->{BackupPC_Server_Status}}"));
1489 sub Action_RestoreInfo
1491 my $Privileged = CheckPermission($In{host});
1492 my $host = $1 if ( $In{host} =~ /(.*)/ );
1496 if ( !$Privileged ) {
1497 ErrorExit($Lang->{Only_privileged_users_can_view_restore_information});
1500 # Find the requested restore
1502 my @Restores = $bpc->RestoreInfoRead($host);
1503 for ( $i = 0 ; $i < @Restores ; $i++ ) {
1504 last if ( $Restores[$i]{num} == $num );
1506 if ( $i >= @Restores ) {
1507 ErrorExit(eval("qq{$Lang->{Restore_number__num_for_host__does_not_exist}}"));
1511 do "$TopDir/pc/$host/RestoreInfo.$Restores[$i]{num}"
1512 if ( -f "$TopDir/pc/$host/RestoreInfo.$Restores[$i]{num}" );
1514 my $startTime = timeStamp2($Restores[$i]{startTime});
1515 my $reqTime = timeStamp2($RestoreReq{reqTime});
1516 my $dur = $Restores[$i]{endTime} - $Restores[$i]{startTime};
1517 $dur = 1 if ( $dur <= 0 );
1518 my $duration = sprintf("%.1f", $dur / 60);
1519 my $MB = sprintf("%.1f", $Restores[$i]{size} / (1024*1024));
1520 my $MBperSec = sprintf("%.2f", $Restores[$i]{size} / (1024*1024*$dur));
1522 my $fileListStr = "";
1523 foreach my $f ( @{$RestoreReq{fileList}} ) {
1524 my $targetFile = $f;
1525 (my $strippedShareSrc = $RestoreReq{shareSrc}) =~ s/^\///;
1526 (my $strippedShareDest = $RestoreReq{shareDest}) =~ s/^\///;
1527 substr($targetFile, 0, length($RestoreReq{pathHdrSrc}))
1528 = $RestoreReq{pathHdrDest};
1529 $fileListStr .= <<EOF;
1530 <tr><td>$RestoreReq{hostSrc}:/$strippedShareSrc$f</td><td>$RestoreReq{hostDest}:/$strippedShareDest$targetFile</td></tr>
1534 Header(eval("qq{$Lang->{Restore___num_details_for__host}}"));
1535 print(eval("qq{$Lang->{Restore___num_details_for__host2 }}"));
1539 ###########################################################################
1540 # Miscellaneous subroutines
1541 ###########################################################################
1545 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
1546 = localtime($_[0] == 0 ? time : $_[0] );
1549 if ( $Conf{CgiDateFormatMMDD} ) {
1550 return sprintf("$mon/$mday %02d:%02d", $hour, $min);
1552 return sprintf("$mday/$mon %02d:%02d", $hour, $min);
1560 if ( defined($Hosts->{$host}) || defined($Status{$host}) ) {
1561 $s = "<a href=\"$MyURL?host=${EscURI($host)}\">$host</a>";
1573 return \$user if ( $user eq ""
1574 || $Conf{CgiUserUrlCreate} eq "" );
1575 if ( $Conf{CgiUserHomePageCheck} eq ""
1576 || -f sprintf($Conf{CgiUserHomePageCheck}, $user, $user, $user) ) {
1578 . sprintf($Conf{CgiUserUrlCreate}, $user, $user, $user)
1590 $s =~ s/\"/"/g;
1593 $s =~ s{([^[:print:]])}{sprintf("&\#x%02X;", ord($1));}eg;
1600 $s =~ s{([^\w.\/-])}{sprintf("%%%02X", ord($1));}eg;
1607 my($head) = shift(@mesg);
1608 my($mesg) = join("</p>\n<p>", @mesg);
1609 $Conf{CgiHeaderFontType} ||= "arial";
1610 $Conf{CgiHeaderFontSize} ||= "3";
1611 $Conf{CgiNavBarBgColor} ||= "#ddeeee";
1612 $Conf{CgiHeaderBgColor} ||= "#99cc33";
1614 $bpc->ServerMesg("log User $User (host=$In{host}) got CGI error: $head")
1615 if ( defined($bpc) );
1616 if ( !defined($Lang->{Error}) ) {
1617 Header("BackupPC: Error");
1618 $mesg = <<EOF if ( !defined($mesg) );
1619 There is some problem with the BackupPC installation.
1620 Please check the permissions on BackupPC_Admin.
1623 ${h1("Error: Unable to read config.pl or language strings!!")}
1628 Header(eval("qq{$Lang->{Error}}"));
1629 print (eval("qq{$Lang->{Error____head}}"));
1638 # Verify that the server connection is ok
1640 return if ( $bpc->ServerOK() );
1641 $bpc->ServerDisconnect();
1642 if ( my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}) ) {
1643 ErrorExit(eval("qq{$Lang->{Unable_to_connect_to_BackupPC_server}}"));
1651 my $reply = $bpc->ServerMesg("status $status");
1652 $reply = $1 if ( $reply =~ /(.*)/s );
1654 # ignore status related to admin and trashClean jobs
1655 if ( $status =~ /\bhosts\b/ ) {
1656 delete($Status{$bpc->adminJob});
1657 delete($Status{$bpc->trashJob});
1661 sub ReadUserEmailInfo
1663 if ( (stat("$TopDir/log/UserEmailInfo.pl"))[9] != $UserEmailInfoMTime ) {
1664 do "$TopDir/log/UserEmailInfo.pl";
1665 $UserEmailInfoMTime = (stat("$TopDir/log/UserEmailInfo.pl"))[9];
1670 # Check if the user is privileged. A privileged user can access
1671 # any information (backup files, logs, status pages etc).
1673 # A user is privileged if they belong to the group
1674 # $Conf{CgiAdminUserGroup}, or they are in $Conf{CgiAdminUsers}
1675 # or they are the user assigned to a host in the host file.
1682 return 0 if ( $User eq "" || ($host ne "" && !defined($Hosts->{$host})) );
1683 if ( $Conf{CgiAdminUserGroup} ne "" ) {
1684 my($n,$p,$gid,$mem) = getgrnam($Conf{CgiAdminUserGroup});
1685 $Privileged ||= ($mem =~ /\b$User\b/);
1687 if ( $Conf{CgiAdminUsers} ne "" ) {
1688 $Privileged ||= ($Conf{CgiAdminUsers} =~ /\b$User\b/);
1689 $Privileged ||= $Conf{CgiAdminUsers} eq "*";
1691 $PrivAdmin = $Privileged;
1692 $Privileged ||= $User eq $Hosts->{$host}{user};
1693 $Privileged ||= defined($Hosts->{$host}{moreUsers}{$User});
1699 # Returns the list of hosts that should appear in the navigation bar
1700 # for this user. If $Conf{CgiNavBarAdminAllHosts} is set, the admin
1701 # gets all the hosts. Otherwise, regular users get hosts for which
1702 # they are the user or are listed in the moreUsers column in the
1707 if ( $Conf{CgiNavBarAdminAllHosts} && CheckPermission() ) {
1708 return sort keys %$Hosts;
1711 return sort grep { $Hosts->{$_}{user} eq $User ||
1712 defined($Hosts->{$_}{moreUsers}{$User}) } keys(%$Hosts);
1716 # Given a host name tries to find the IP address. For non-dhcp hosts
1717 # we just return the host name. For dhcp hosts we check the address
1718 # the user is using ($ENV{REMOTE_ADDR}) and also the last-known IP
1719 # address for $host. (Later we should replace this with a broadcast
1722 sub ConfirmIPAddress
1727 if ( defined($Hosts->{$host}) && $Hosts->{$host}{dhcp}
1728 && $ENV{REMOTE_ADDR} =~ /^(\d+[\.\d]*)$/ ) {
1730 my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($ipAddr);
1731 if ( $netBiosHost ne $host ) {
1733 GetStatusInfo("host(${EscURI($host)})");
1734 if ( defined($StatusHost{dhcpHostIP})
1735 && $StatusHost{dhcpHostIP} ne $ipAddr ) {
1736 $tryIP = eval("qq{$Lang->{tryIP}}");
1737 ($netBiosHost, $netBiosUser)
1738 = $bpc->NetBiosInfoGet($StatusHost{dhcpHostIP});
1740 if ( $netBiosHost ne $host ) {
1741 ErrorExit(eval("qq{$Lang->{Can_t_find_IP_address_for}}"),
1742 eval("qq{$Lang->{host_is_a_DHCP_host}}"));
1744 $ipAddr = $StatusHost{dhcpHostIP};
1752 my($name, $info) = @_;
1753 my $poolSize = sprintf("%.2f", $info->{"${name}Kb"} / (1000 * 1024));
1754 my $poolRmSize = sprintf("%.2f", $info->{"${name}KbRm"} / (1000 * 1024));
1755 my $poolTime = timeStamp2($info->{"${name}Time"});
1756 $info->{"${name}FileCntRm"} = $info->{"${name}FileCntRm"} + 0;
1757 return eval("qq{$Lang->{Pool_Stat}}");
1760 ###########################################################################
1761 # HTML layout subroutines
1762 ###########################################################################
1768 { link => "", name => $Lang->{Status},
1770 { link => "?action=summary", name => $Lang->{PC_Summary} },
1771 { link => "?action=view&type=LOG", name => $Lang->{LOG_file} },
1772 { link => "?action=LOGlist", name => $Lang->{Old_LOGs} },
1773 { link => "?action=emailSummary", name => $Lang->{Email_summary} },
1774 { link => "?action=view&type=config", name => $Lang->{Config_file} },
1775 { link => "?action=view&type=hosts", name => $Lang->{Hosts_file} },
1776 { link => "?action=queue", name => $Lang->{Current_queues} },
1777 { link => "?action=view&type=docs", name => $Lang->{Documentation},
1779 { link => "http://backuppc.sourceforge.net/faq", name => "FAQ",
1781 { link => "http://backuppc.sourceforge.net", name => "SourceForge",
1784 print $Cgi->header();
1786 <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
1788 <title>$title</title>
1790 </head><body bgcolor="$Conf{CgiBodyBgColor}">
1791 <table cellpadding="0" cellspacing="0" border="0">
1792 <tr valign="top"><td valign="top" bgcolor="$Conf{CgiNavBarBgColor}" width="10%">
1794 NavSectionTitle("BackupPC");
1796 if ( defined($In{host}) && defined($Hosts->{$In{host}}) ) {
1797 my $host = $In{host};
1798 NavSectionTitle( eval("qq{$Lang->{Host_Inhost}}") );
1800 NavLink("?host=${EscURI($host)}", $Lang->{Home});
1801 NavLink("?action=view&type=LOG&host=${EscURI($host)}", $Lang->{LOG_file});
1802 NavLink("?action=LOGlist&host=${EscURI($host)}", $Lang->{Old_LOGs});
1803 if ( -f "$TopDir/pc/$host/SmbLOG.bad"
1804 || -f "$TopDir/pc/$host/SmbLOG.bad.z"
1805 || -f "$TopDir/pc/$host/XferLOG.bad"
1806 || -f "$TopDir/pc/$host/XferLOG.bad.z" ) {
1807 NavLink("?action=view&type=XferLOGbad&host=${EscURI($host)}",
1808 $Lang->{Last_bad_XferLOG});
1809 NavLink("?action=view&type=XferErrbad&host=${EscURI($host)}",
1810 $Lang->{Last_bad_XferLOG_errors_only});
1812 if ( -f "$TopDir/pc/$host/config.pl" ) {
1813 NavLink("?action=view&type=config&host=${EscURI($host)}", $Lang->{Config_file});
1817 NavSectionTitle($Lang->{Hosts});
1818 if ( defined($Hosts) && %$Hosts > 0 ) {
1820 foreach my $host ( GetUserHosts() ) {
1821 NavLink("?host=${EscURI($host)}", $host);
1826 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1827 <tr><td>$Lang->{Host_or_User_name}</td>
1828 <tr><td><form action="$MyURL" method="get"><small>
1829 <input type="text" name="host" size="10" maxlength="64">
1830 <input type="hidden" name="action" value="hostInfo"><input type="submit" value="$Lang->{Go}" name="ignore">
1831 </small></form></td></tr>
1834 NavSectionTitle($Lang->{NavSectionTitle_});
1836 foreach my $l ( @adminLinks ) {
1837 if ( $PrivAdmin || $l->{priv} ) {
1838 NavLink($l->{link}, $l->{name});
1840 NavLink(undef, $l->{name});
1845 </td><td valign="top" width="5"> </td>
1846 <td valign="top" width="90%">
1863 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1864 <tr><td bgcolor="$Conf{CgiHeaderBgColor}"><font face="$Conf{CgiHeaderFontType}"
1865 size="$Conf{CgiHeaderFontSize}"><b>$head</b>
1875 $padding = 2 if ( !defined($padding) );
1877 <table cellpadding="$padding" cellspacing="0" border="0" width="100%">
1888 my($link, $text) = @_;
1889 print "<tr><td width=\"2%\" valign=\"top\"><b>·</b></td>";
1890 if ( defined($link) ) {
1891 $link = "$MyURL$link" if ( $link eq "" || $link =~ /^\?/ );
1893 <td width="98%"><a href="$link"><small>$text</small></a></td></tr>
1897 <td width="98%"><small>$text</small></td></tr>
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>
1919 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1921 <td bgcolor="$Conf{CgiHeaderBgColor}"> <font face="$Conf{CgiHeaderFontType}"
1922 size="$Conf{CgiHeaderFontSize}"><b>$str</b></font>