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.0beta3, released 1 Jun 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(undef, undef, 1)) );
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___}}"), <<EOF);
104 This script needs to run as the user specified in \$Conf{BackupPCUser},
105 which is set to $Conf{BackupPCUser}.
107 This is an installation problem. If you are using mod_perl then
108 it appears that Apache is not running as user $Conf{BackupPCUser}.
109 If you are not using mod_perl, then most like setuid is not working
110 properly on BackupPC_Admin. Check the permissions on
111 $Conf{CgiDir}/BackupPC_Admin and look at the documentation.
115 if ( !defined($Hosts) || $bpc->HostsMTime() != $HostsMTime ) {
116 $HostsMTime = $bpc->HostsMTime();
117 $Hosts = $bpc->HostInfoRead();
119 # turn moreUsers list into a hash for quick lookups
120 foreach my $host (keys %$Hosts) {
121 $Hosts->{$host}{moreUsers} =
122 {map {$_, 1} split(",", $Hosts->{$host}{moreUsers}) }
126 my %ActionDispatch = (
127 "summary" => \&Action_Summary,
128 $Lang->{Start_Incr_Backup} => \&Action_StartStopBackup,
129 $Lang->{Start_Full_Backup} => \&Action_StartStopBackup,
130 $Lang->{Stop_Dequeue_Backup} => \&Action_StartStopBackup,
131 "queue" => \&Action_Queue,
132 "view" => \&Action_View,
133 "LOGlist" => \&Action_LOGlist,
134 "emailSummary" => \&Action_EmailSummary,
135 "browse" => \&Action_Browse,
136 $Lang->{Restore} => \&Action_Restore,
137 "RestoreFile" => \&Action_RestoreFile,
138 "hostInfo" => \&Action_HostInfo,
139 "generalInfo" => \&Action_GeneralInfo,
140 "restoreInfo" => \&Action_RestoreInfo,
144 # Set default actions, then call sub handler
146 $In{action} ||= "hostInfo" if ( defined($In{host}) );
147 $In{action} = "generalInfo" if ( !defined($ActionDispatch{$In{action}}) );
148 $ActionDispatch{$In{action}}();
151 ###########################################################################
152 # Action handling subroutines
153 ###########################################################################
157 my($fullTot, $fullSizeTot, $incrTot, $incrSizeTot, $str,
158 $strNone, $strGood, $hostCntGood, $hostCntNone);
160 $hostCntGood = $hostCntNone = 0;
161 GetStatusInfo("hosts");
162 my $Privileged = CheckPermission();
164 if ( !$Privileged ) {
165 ErrorExit($Lang->{Only_privileged_users_can_view_PC_summaries} );
167 foreach my $host ( sort(keys(%Status)) ) {
168 my($fullDur, $incrCnt, $incrAge, $fullSize, $fullRate, $reasonHilite);
170 my @Backups = $bpc->BackupInfoRead($host);
171 my $fullCnt = $incrCnt = 0;
172 my $fullAge = $incrAge = -1;
173 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
174 if ( $Backups[$i]{type} eq "full" ) {
176 if ( $fullAge < 0 || $Backups[$i]{startTime} > $fullAge ) {
177 $fullAge = $Backups[$i]{startTime};
178 $fullSize = $Backups[$i]{size} / (1024 * 1024);
179 $fullDur = $Backups[$i]{endTime} - $Backups[$i]{startTime};
181 $fullSizeTot += $Backups[$i]{size} / (1024 * 1024);
184 if ( $incrAge < 0 || $Backups[$i]{startTime} > $incrAge ) {
185 $incrAge = $Backups[$i]{startTime};
187 $incrSizeTot += $Backups[$i]{size} / (1024 * 1024);
190 if ( $fullAge < 0 ) {
194 $fullAge = sprintf("%.1f", (time - $fullAge) / (24 * 3600));
195 $fullRate = sprintf("%.2f",
196 $fullSize / ($fullDur <= 0 ? 1 : $fullDur));
198 if ( $incrAge < 0 ) {
201 $incrAge = sprintf("%.1f", (time - $incrAge) / (24 * 3600));
203 $fullTot += $fullCnt;
204 $incrTot += $incrCnt;
205 $fullSize = sprintf("%.2f", $fullSize / 1000);
206 $incrAge = " " if ( $incrAge eq "" );
207 $reasonHilite = $Conf{CgiStatusHilightColor}{$Status{$host}{reason}}
208 || $Conf{CgiStatusHilightColor}{$Status{$host}{state}};
209 $reasonHilite = " bgcolor=\"$reasonHilite\"" if ( $reasonHilite ne "" );
210 if ( $Status{$host}{state} ne "Status_backup_in_progress"
211 && $Status{$host}{state} ne "Status_restore_in_progress"
212 && $Status{$host}{error} ne "" ) {
213 ($shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
214 $shortErr = " ($shortErr)";
218 <tr$reasonHilite><td> ${HostLink($host)} </td>
219 <td align="center"> ${UserLink(defined($Hosts->{$host})
220 ? $Hosts->{$host}{user} : "")} </td>
221 <td align="center"> $fullCnt </td>
222 <td align="center"> $fullAge </td>
223 <td align="center"> $fullSize </td>
224 <td align="center"> $fullRate </td>
225 <td align="center"> $incrCnt </td>
226 <td align="center"> $incrAge </td>
227 <td align="center"> $Lang->{$Status{$host}{state}} </td>
228 <td> $Lang->{$Status{$host}{reason}}$shortErr </td></tr>
230 if ( @Backups == 0 ) {
238 $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
239 $incrSizeTot = sprintf("%.2f", $incrSizeTot / 1000);
240 my $now = timeStamp2(time);
242 Header($Lang->{BackupPC__Server_Summary});
243 print eval ("qq{$Lang->{BackupPC_Summary}}");
248 sub Action_StartStopBackup
252 my $start = 1 if ( $In{action} eq $Lang->{Start_Incr_Backup}
253 || $In{action} eq $Lang->{Start_Full_Backup} );
254 my $doFull = $In{action} eq $Lang->{Start_Full_Backup} ? 1 : 0;
255 my $type = $doFull ? "full" : "incremental";
256 my $host = $In{host};
257 my $Privileged = CheckPermission($host);
259 if ( !$Privileged ) {
260 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_stop_or_start_backups}}"));
266 if ( $Hosts->{$host}{dhcp} ) {
267 $reply = $bpc->ServerMesg("backup $In{hostIP} ${EscURI($host)}"
269 $str = eval("qq{$Lang->{Backup_requested_on_DHCP__host}}");
271 $reply = $bpc->ServerMesg("backup ${EscURI($host)}"
272 . " ${EscURI($host)} $User $doFull");
273 $str = eval("qq{$Lang->{Backup_requested_on__host_by__User}}");
276 $reply = $bpc->ServerMesg("stop ${EscURI($host)} $User $In{backoff}");
277 $str = eval("qq{$Lang->{Backup_stopped_dequeued_on__host_by__User}}");
280 Header(eval ("qq{$Lang->{BackupPC__Backup_Requested_on__host}}") );
281 print (eval ("qq{$Lang->{REPLY_FROM_SERVER}}"));
286 my $ipAddr = ConfirmIPAddress($host);
288 Header(eval("qq{$Lang->{BackupPC__Start_Backup_Confirm_on__host}}"));
289 print (eval("qq{$Lang->{Are_you_sure_start}}"));
292 GetStatusInfo("host(${EscURI($host)})");
293 if ( $StatusHost{backoffTime} > time ) {
294 $backoff = sprintf("%.1f",
295 ($StatusHost{backoffTime} - time) / 3600);
297 Header($Lang->{BackupPC__Stop_Backup_Confirm_on__host});
298 print (eval ("qq{$Lang->{Are_you_sure_stop}}"));
306 my($strBg, $strUser, $strCmd);
308 GetStatusInfo("queues");
309 my $Privileged = CheckPermission();
311 if ( !$Privileged ) {
312 ErrorExit($Lang->{Only_privileged_users_can_view_queues_});
316 my $req = pop(@BgQueue);
317 my($reqTime) = timeStamp2($req->{reqTime});
319 <tr><td> ${HostLink($req->{host})} </td>
320 <td align="center"> $reqTime </td>
321 <td align="center"> $req->{user} </td></tr>
324 while ( @UserQueue ) {
325 my $req = pop(@UserQueue);
326 my $reqTime = timeStamp2($req->{reqTime});
328 <tr><td> ${HostLink($req->{host})} </td>
329 <td align="center"> $reqTime </td>
330 <td align="center"> $req->{user} </td></tr>
333 while ( @CmdQueue ) {
334 my $req = pop(@CmdQueue);
335 my $reqTime = timeStamp2($req->{reqTime});
336 (my $cmd = $req->{cmd}[0]) =~ s/$BinDir\///;
338 <tr><td> ${HostLink($req->{host})} </td>
339 <td align="center"> $reqTime </td>
340 <td align="center"> $req->{user} </td>
341 <td> $cmd $req->{cmd}[0] </td></tr>
344 Header($Lang->{BackupPC__Queue_Summary});
346 print ( eval ( "qq{$Lang->{Backup_Queue_Summary}}") );
353 my $Privileged = CheckPermission($In{host});
356 my $host = $In{host};
358 my $type = $In{type};
361 my $ext = $num ne "" ? ".$num" : "";
363 ErrorExit(eval("qq{$Lang->{Invalid_number__num}}")) if ( $num ne "" && $num !~ /^\d+$/ );
364 if ( $type eq "XferLOG" ) {
365 $file = "$TopDir/pc/$host/SmbLOG$ext";
366 $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
367 } elsif ( $type eq "XferLOGbad" ) {
368 $file = "$TopDir/pc/$host/SmbLOG.bad";
369 $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
370 } elsif ( $type eq "XferErrbad" ) {
371 $file = "$TopDir/pc/$host/SmbLOG.bad";
372 $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
373 $comment = $Lang->{Extracting_only_Errors};
374 } elsif ( $type eq "XferErr" ) {
375 $file = "$TopDir/pc/$host/SmbLOG$ext";
376 $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
377 $comment = $Lang->{Extracting_only_Errors};
378 } elsif ( $type eq "RestoreLOG" ) {
379 $file = "$TopDir/pc/$host/RestoreLOG$ext";
380 } elsif ( $type eq "RestoreErr" ) {
381 $file = "$TopDir/pc/$host/RestoreLOG$ext";
382 $comment = $Lang->{Extracting_only_Errors};
383 } elsif ( $host ne "" && $type eq "config" ) {
384 $file = "$TopDir/pc/$host/config.pl";
385 $file = "$TopDir/conf/$host.pl"
386 if ( $host ne "config" && -f "$TopDir/conf/$host.pl"
388 } elsif ( $type eq "docs" ) {
389 $file = "$BinDir/../doc/BackupPC.html";
390 if ( open(LOG, $file) ) {
391 Header($Lang->{BackupPC__Documentation});
392 print while ( <LOG> );
396 ErrorExit(eval("qq{$Lang->{Unable_to_open__file__configuration_problem}}"));
399 } elsif ( $type eq "config" ) {
400 $file = "$TopDir/conf/config.pl";
401 } elsif ( $type eq "hosts" ) {
402 $file = "$TopDir/conf/hosts";
403 } elsif ( $host ne "" ) {
404 $file = "$TopDir/pc/$host/LOG$ext";
406 $file = "$TopDir/log/LOG$ext";
409 if ( !$Privileged ) {
410 ErrorExit($Lang->{Only_privileged_users_can_view_log_or_config_files});
412 if ( !-f $file && -f "$file.z" ) {
416 Header(eval("qq{$Lang->{Backup_PC__Log_File__file}}") );
417 print( eval ("qq{$Lang->{Log_File__file__comment}}"));
418 if ( defined($fh = BackupPC::FileZIO->open($file, 0, $compress)) ) {
419 my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
421 print ( eval ("qq{$Lang->{Contents_of_log_file}}"));
424 if ( $type eq "XferErr" || $type eq "XferErrbad"
425 || $type eq "RestoreErr" ) {
428 $_ = $fh->readLine();
430 print(eval ("qq{$Lang->{skipped__skipped_lines}}"))
435 || /^\s*(\d+) \(\s*\d+\.\d kb\/s\) (.*)$/
436 || /^tar: dumped \d+ files/
437 || /^added interface/i
438 || /^restore tar file /i
439 || /^restore directory /i
440 || /^tarmode is now/i
441 || /^Total bytes written/i
443 || /^Getting files newer than/i
444 || /^Output is \/dev\/null/
445 || /^\([\d\.]* kb\/s\) \(average [\d\.]* kb\/s\)$/
446 || /^\s+directory \\/
454 print(eval("qq{$Lang->{skipped__skipped_lines}}"))
457 print ${EscHTML($_)};
459 } elsif ( $linkHosts ) {
461 $_ = $fh->readLine();
462 last if ( $_ eq "" );
463 my $s = ${EscHTML($_)};
464 $s =~ s/\b([\w-]+)\b/defined($Hosts->{$1})
465 ? ${HostLink($1)} : $1/eg;
468 } elsif ( $type eq "config" ) {
470 $_ = $fh->readLine();
471 last if ( $_ eq "" );
472 # remove any passwords and user names
473 s/(SmbSharePasswd.*=.*['"]).*(['"])/$1$2/ig;
474 s/(SmbShareUserName.*=.*['"]).*(['"])/$1$2/ig;
475 s/(RsyncdPasswd.*=.*['"]).*(['"])/$1$2/ig;
476 s/(ServerMesgSecret.*=.*['"]).*(['"])/$1$2/ig;
477 print ${EscHTML($_)};
481 $_ = $fh->readLine();
482 last if ( $_ eq "" );
483 print ${EscHTML($_)};
488 printf( eval("qq{$Lang->{_pre___Can_t_open_log_file__file}}"));
498 my $Privileged = CheckPermission($In{host});
500 if ( !$Privileged ) {
501 ErrorExit($Lang->{Only_privileged_users_can_view_log_files});
503 my $host = $In{host};
504 my($url0, $hdr, $root, $str);
506 $root = "$TopDir/pc/$host/LOG";
507 $url0 = "&host=${EscURI($host)}";
508 $hdr = "for host $host";
510 $root = "$TopDir/log/LOG";
514 for ( my $i = -1 ; ; $i++ ) {
521 $file .= ".z" if ( !-f $file && -f "$file.z" );
522 last if ( !-f $file );
523 my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
524 my $size = (stat($file))[7];
526 <tr><td> <a href="$MyURL?action=view&type=LOG$url0$url1"><tt>$file</tt></a> </td>
527 <td align="right"> $size </td>
528 <td> $mtimeStr </td></tr>
531 Header($Lang->{BackupPC__Log_File_History});
532 print (eval("qq{$Lang->{Log_File_History__hdr}}"));
536 sub Action_EmailSummary
538 my $Privileged = CheckPermission();
540 if ( !$Privileged ) {
541 ErrorExit($Lang->{Only_privileged_users_can_view_email_summaries});
543 GetStatusInfo("hosts");
546 foreach my $u ( keys(%UserEmailInfo) ) {
547 next if ( !defined($UserEmailInfo{$u}{lastTime}) );
548 my $emailTimeStr = timeStamp2($UserEmailInfo{$u}{lastTime});
549 $EmailStr{$UserEmailInfo{$u}{lastTime}} .= <<EOF;
550 <tr><td>${UserLink($u)} </td>
551 <td>${HostLink($UserEmailInfo{$u}{lastHost})} </td>
552 <td>$emailTimeStr </td>
553 <td>$UserEmailInfo{$u}{lastSubj} </td></tr>
556 foreach my $t ( sort({$b <=> $a} keys(%EmailStr)) ) {
557 $str .= $EmailStr{$t};
559 Header($Lang->{Email_Summary});
560 print (eval("qq{$Lang->{Recent_Email_Summary}}"));
566 my $Privileged = CheckPermission($In{host});
567 my($i, $dirStr, $fileStr, $attr);
570 if ( !$Privileged ) {
571 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_browse_backup_files}}"));
573 my $host = $In{host};
575 my $share = $In{share};
578 ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
580 # Find the requested backup and the previous filled backup
582 my @Backups = $bpc->BackupInfoRead($host);
583 for ( $i = 0 ; $i < @Backups ; $i++ ) {
584 last if ( $Backups[$i]{num} == $num );
586 if ( $i >= @Backups ) {
587 ErrorExit("Backup number $num for host ${EscHTML($host)} does"
590 my $backupTime = timeStamp2($Backups[$i]{startTime});
591 my $backupAge = sprintf("%.1f", (time - $Backups[$i]{startTime})
593 my $view = BackupPC::View->new($bpc, $host, \@Backups);
595 if ( $dir eq "" || $dir eq "." || $dir eq ".." ) {
596 $attr = $view->dirAttrib($num, "", "");
597 if ( keys(%$attr) > 0 ) {
598 $share = (sort(keys(%$attr)))[0];
601 ErrorExit(eval("qq{$Lang->{Directory___EscHTML}}"));
604 $dir = "/$dir" if ( $dir !~ /^\// );
609 # Loop up the directory tree until we hit the top.
613 my($fLast, $fLastum, @DirStr);
615 $attr = $view->dirAttrib($num, $share, $relDir);
616 if ( !defined($attr) ) {
617 ErrorExit(eval("qq{$Lang->{Can_t_browse_bad_directory_name2}}"));
620 my $fileCnt = 0; # file counter
621 $fLast = $dirStr = "";
624 # Loop over each of the files in this directory
626 foreach my $f ( sort(keys(%$attr)) ) {
627 my($dirOpen, $gotDir, $imgStr, $img, $path);
628 my $fURI = $f; # URI escaped $f
629 my $shareURI = $share; # URI escaped $share
630 if ( $relDir eq "" ) {
633 ($path = "$relDir/$f") =~ s{//+}{/}g;
635 if ( $shareURI eq "" ) {
640 $path =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
641 $fURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
642 $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
643 $dirOpen = 1 if ( defined($currDir) && $f eq $currDir );
644 if ( $attr->{$f}{type} == BPC_FTYPE_DIR ) {
646 # Display directory if it exists in current backup.
647 # First find out if there are subdirs
649 my($bold, $unbold, $BGcolor);
651 $img |= 1 << 5 if ( $attr->{$f}{nlink} > 2 );
656 $img |= 1 << 3 if ( $attr->{$f}{nlink} > 2 );
658 my $imgFileName = sprintf("%07b.gif", $img);
659 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
660 if ( "$relDir/$f" eq $dir ) {
661 $BGcolor = " bgcolor=\"$Conf{CgiHeaderBgColor}\"";
666 $dirName =~ s/ / /g;
667 push(@DirStr, {needTick => 1,
670 <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>
675 my($lastTick, $doneLastTick);
676 foreach my $d ( @DirStrPrev ) {
677 $lastTick = $d if ( $d->{needTick} );
679 $doneLastTick = 1 if ( !defined($lastTick) );
680 foreach my $d ( @DirStrPrev ) {
682 if ( $d->{needTick} ) {
685 if ( $d == $lastTick ) {
688 } elsif ( !$doneLastTick ) {
689 $img |= 1 << 3 | 1 << 4;
691 my $imgFileName = sprintf("%07b.gif", $img);
692 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
693 push(@DirStr, {needTick => 0,
694 tdArgs => $d->{tdArgs},
695 link => $imgStr . $d->{link}
700 if ( $relDir eq $dir ) {
702 # This is the selected directory, so display all the files
705 if ( defined($a = $attr->{$f}) ) {
706 my $mtimeStr = $bpc->timeStamp($a->{mtime});
708 my $typeStr = BackupPC::Attrib::fileType2Text(undef,
710 my $modeStr = sprintf("0%o", $a->{mode} & 07777);
712 <td align="center">$typeStr</td>
713 <td align="center">$modeStr</td>
714 <td align="center">$a->{backupNum}</td>
715 <td align="right">$a->{size}</td>
716 <td align="right">$mtimeStr</td>
720 $attrStr .= "<td colspan=\"5\" align=\"center\"> </td>\n";
722 (my $fDisp = "${EscHTML($f)}") =~ s/ / /g;
725 <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>
731 <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>
739 @DirStrPrev = @DirStr;
740 last if ( $relDir eq "" && $share eq "" );
742 # Prune the last directory off $relDir, or at the very end
743 # do the top-level directory.
745 if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) {
755 my $dirDisplay = "$share/$dir";
756 $dirDisplay =~ s{//+}{/}g;
757 $dirDisplay =~ s{/+$}{}g;
758 $dirDisplay = "/" if ( $dirDisplay eq "" );
761 if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) {
763 my $numF = join(", #", @mergeNums);
764 $filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}");
766 Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"));
768 foreach my $d ( @DirStrPrev ) {
769 $dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n";
772 ### hide checkall button if there are no files
773 my ($topCheckAll, $checkAll, $fileHeader);
775 $fileHeader = eval("qq{$Lang->{fileHeader}}");
777 $checkAll = $Lang->{checkAll};
779 # and put a checkall box on top if there are at least 20 files
780 if ( $checkBoxCnt >= 20 ) {
781 $topCheckAll = $checkAll;
782 $topCheckAll =~ s{allFiles}{allFilestop}g;
785 $fileStr = eval("qq{$Lang->{The_directory_is_empty}}");
788 foreach my $i ( $view->backupList($share, $dir) ) {
790 my $shareURI = $share;
791 $path =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
792 $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
793 push(@otherDirs, "<a href=\"$MyURL?action=browse&host=${EscURI($host)}&num=$i"
794 . "&share=$shareURI&dir=$path\">$i</a>");
798 my $otherDirs = join(",\n", @otherDirs);
799 $filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}");
801 print (eval("qq{$Lang->{Backup_browse_for__host}}"));
808 my $Privileged = CheckPermission($In{host});
809 if ( !$Privileged ) {
810 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files}}"));
812 my $host = $In{host};
814 my $share = $In{share};
815 my(@fileList, $fileListStr, $hiddenStr, $pathHdr, $badFileCnt);
816 my @Backups = $bpc->BackupInfoRead($host);
819 if ( !defined($Hosts->{$host}) ) {
820 ErrorExit(eval("qq{$Lang->{Bad_host_name}}"));
822 for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
823 next if ( !defined($In{"fcb$i"}) );
824 (my $name = $In{"fcb$i"}) =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
825 $badFileCnt++ if ( $name =~ m{(^|/)\.\.(/|$)} );
826 if ( @fileList == 0 ) {
829 while ( substr($name, 0, length($pathHdr)) ne $pathHdr ) {
830 $pathHdr = substr($pathHdr, 0, rindex($pathHdr, "/"));
833 push(@fileList, $name);
835 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
837 $fileListStr .= <<EOF;
838 <li> ${EscHTML($name)}
841 $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
842 $hiddenStr .= "<input type=\"hidden\" name=\"share\" value=\"${EscHTML($share)}\">\n";
843 $badFileCnt++ if ( $In{pathHdr} =~ m{(^|/)\.\.(/|$)} );
844 $badFileCnt++ if ( $In{num} =~ m{(^|/)\.\.(/|$)} );
845 if ( @fileList == 0 ) {
846 ErrorExit($Lang->{You_haven_t_selected_any_files__please_go_Back_to});
849 ErrorExit($Lang->{Nice_try__but_you_can_t_put});
851 if ( @fileList == 1 ) {
852 $pathHdr =~ s/(.*)\/.*/$1/;
854 $pathHdr = "/" if ( $pathHdr eq "" );
855 if ( $In{type} != 0 && @fileList == $In{fcbMax} ) {
857 # All the files in the list were selected, so just restore the
858 # entire parent directory
860 @fileList = ( $pathHdr );
862 if ( $In{type} == 0 ) {
864 # Tell the user what options they have
866 Header(eval("qq{$Lang->{Restore_Options_for__host}}"));
867 print(eval("qq{$Lang->{Restore_Options_for__host2}}"));
870 # Verify that Archive::Zip is available before showing the
873 if ( eval { require Archive::Zip } ) {
874 print (eval("qq{$Lang->{Option_2__Download_Zip_archive}}"));
876 print (eval("qq{$Lang->{Option_2__Download_Zip_archive2}}"));
878 print (eval("qq{$Lang->{Option_3__Download_Zip_archive}}"));
880 } elsif ( $In{type} == 1 ) {
882 # Provide the selected files via a tar archive.
884 my @fileListTrim = @fileList;
885 if ( @fileListTrim > 10 ) {
886 @fileListTrim = (@fileListTrim[0..9], '...');
888 $bpc->ServerMesg("log User $User downloaded tar archive for $host,"
889 . " backup $num; files were: "
890 . join(", ", @fileListTrim));
893 if ( $In{relative} ) {
894 @pathOpts = ("-r", $pathHdr, "-p", "");
897 Content-Type: application/x-gtar
898 Content-Transfer-Encoding: binary
899 Content-Disposition: attachment; filename=\"restore.tar\"
903 # Fork the child off and manually copy the output to our stdout.
904 # This is necessary to ensure the output gets to the correct place
907 $bpc->cmdSystemOrEval(["$BinDir/BackupPC_tarCreate",
916 } elsif ( $In{type} == 2 ) {
918 # Provide the selected files via a zip archive.
920 my @fileListTrim = @fileList;
921 if ( @fileListTrim > 10 ) {
922 @fileListTrim = (@fileListTrim[0..9], '...');
924 $bpc->ServerMesg("log User $User downloaded zip archive for $host,"
925 . " backup $num; files were: "
926 . join(", ", @fileListTrim));
929 if ( $In{relative} ) {
930 @pathOpts = ("-r", $pathHdr, "-p", "");
933 Content-Type: application/zip
934 Content-Transfer-Encoding: binary
935 Content-Disposition: attachment; filename=\"restore.zip\"
938 $In{compressLevel} = 5 if ( $In{compressLevel} !~ /^\d+$/ );
940 # Fork the child off and manually copy the output to our stdout.
941 # This is necessary to ensure the output gets to the correct place
944 $bpc->cmdSystemOrEval(["$BinDir/BackupPC_zipCreate",
947 "-c", $In{compressLevel},
954 } elsif ( $In{type} == 3 ) {
956 # Do restore directly onto host
958 if ( !defined($Hosts->{$In{hostDest}}) ) {
959 ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
961 if ( !CheckPermission($In{hostDest}) ) {
962 ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
965 foreach my $f ( @fileList ) {
967 (my $strippedShare = $share) =~ s/^\///;
968 (my $strippedShareDest = $In{shareDest}) =~ s/^\///;
969 substr($targetFile, 0, length($pathHdr)) = $In{pathHdr};
970 $fileListStr .= <<EOF;
971 <tr><td>$host:/$strippedShare$f</td><td>$In{hostDest}:/$strippedShareDest$targetFile</td></tr>
974 Header(eval("qq{$Lang->{Restore_Confirm_on__host}}"));
975 print(eval("qq{$Lang->{Are_you_sure}}"));
977 } elsif ( $In{type} == 4 ) {
978 if ( !defined($Hosts->{$In{hostDest}}) ) {
979 ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
981 if ( !CheckPermission($In{hostDest}) ) {
982 ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
984 my $hostDest = $1 if ( $In{hostDest} =~ /(.+)/ );
985 my $ipAddr = ConfirmIPAddress($hostDest);
987 # Prepare and send the restore request. We write the request
988 # information using Data::Dumper to a unique file,
989 # $TopDir/pc/$hostDest/restoreReq.$$.n. We use a file
990 # in case the list of files to restore is very long.
993 for ( my $i = 0 ; ; $i++ ) {
994 $reqFileName = "restoreReq.$$.$i";
995 last if ( !-f "$TopDir/pc/$hostDest/$reqFileName" );
998 # source of restore is hostSrc, #num, path shareSrc/pathHdrSrc
1002 pathHdrSrc => $pathHdr,
1004 # destination of restore is hostDest:shareDest/pathHdrDest
1005 hostDest => $hostDest,
1006 shareDest => $In{shareDest},
1007 pathHdrDest => $In{pathHdr},
1009 # list of files to restore
1010 fileList => \@fileList,
1016 my($dump) = Data::Dumper->new(
1020 if ( open(REQ, ">$TopDir/pc/$hostDest/$reqFileName") ) {
1021 print(REQ $dump->Dump);
1024 ErrorExit(eval("qq{$Lang->{Can_t_open_create}}"));
1026 $reply = $bpc->ServerMesg("restore ${EscURI($ipAddr)}"
1027 . " ${EscURI($hostDest)} $User $reqFileName");
1028 $str = eval("qq{$Lang->{Restore_requested_to_host__hostDest__backup___num}}");
1029 Header(eval("qq{$Lang->{Restore_Requested_on__hostDest}}"));
1030 print (eval("qq{$Lang->{Reply_from_server_was___reply}}"));
1035 sub Action_RestoreFile
1037 restoreFile($In{host}, $In{num}, $In{share}, $In{dir});
1042 my($host, $num, $share, $dir, $skipHardLink, $origName) = @_;
1043 my($Privileged) = CheckPermission($host);
1046 # Some common content (media) types from www.iana.org (via MIME::Types).
1048 my $Ext2ContentType = {
1049 'asc' => 'text/plain',
1050 'avi' => 'video/x-msvideo',
1051 'bmp' => 'image/bmp',
1052 'book' => 'application/x-maker',
1053 'cc' => 'text/plain',
1054 'cpp' => 'text/plain',
1055 'csh' => 'application/x-csh',
1056 'csv' => 'text/comma-separated-values',
1057 'c' => 'text/plain',
1058 'deb' => 'application/x-debian-package',
1059 'doc' => 'application/msword',
1060 'dot' => 'application/msword',
1061 'dtd' => 'text/xml',
1062 'dvi' => 'application/x-dvi',
1063 'eps' => 'application/postscript',
1064 'fb' => 'application/x-maker',
1065 'fbdoc'=> 'application/x-maker',
1066 'fm' => 'application/x-maker',
1067 'frame'=> 'application/x-maker',
1068 'frm' => 'application/x-maker',
1069 'gif' => 'image/gif',
1070 'gtar' => 'application/x-gtar',
1071 'gz' => 'application/x-gzip',
1072 'hh' => 'text/plain',
1073 'hpp' => 'text/plain',
1074 'h' => 'text/plain',
1075 'html' => 'text/html',
1076 'htmlx'=> 'text/html',
1077 'htm' => 'text/html',
1078 'iges' => 'model/iges',
1079 'igs' => 'model/iges',
1080 'jpeg' => 'image/jpeg',
1081 'jpe' => 'image/jpeg',
1082 'jpg' => 'image/jpeg',
1083 'js' => 'application/x-javascript',
1084 'latex'=> 'application/x-latex',
1085 'maker'=> 'application/x-maker',
1086 'mid' => 'audio/midi',
1087 'midi' => 'audio/midi',
1088 'movie'=> 'video/x-sgi-movie',
1089 'mov' => 'video/quicktime',
1090 'mp2' => 'audio/mpeg',
1091 'mp3' => 'audio/mpeg',
1092 'mpeg' => 'video/mpeg',
1093 'mpg' => 'video/mpeg',
1094 'mpp' => 'application/vnd.ms-project',
1095 'pdf' => 'application/pdf',
1096 'pgp' => 'application/pgp-signature',
1097 'php' => 'application/x-httpd-php',
1098 'pht' => 'application/x-httpd-php',
1099 'phtml'=> 'application/x-httpd-php',
1100 'png' => 'image/png',
1101 'ppm' => 'image/x-portable-pixmap',
1102 'ppt' => 'application/powerpoint',
1103 'ppt' => 'application/vnd.ms-powerpoint',
1104 'ps' => 'application/postscript',
1105 'qt' => 'video/quicktime',
1106 'rgb' => 'image/x-rgb',
1107 'rtf' => 'application/rtf',
1108 'rtf' => 'text/rtf',
1109 'shar' => 'application/x-shar',
1110 'shtml'=> 'text/html',
1111 'swf' => 'application/x-shockwave-flash',
1112 'tex' => 'application/x-tex',
1113 'texi' => 'application/x-texinfo',
1114 'texinfo'=> 'application/x-texinfo',
1115 'tgz' => 'application/x-gtar',
1116 'tiff' => 'image/tiff',
1117 'tif' => 'image/tiff',
1118 'txt' => 'text/plain',
1119 'vcf' => 'text/x-vCard',
1120 'vrml' => 'model/vrml',
1121 'wav' => 'audio/x-wav',
1122 'wmls' => 'text/vnd.wap.wmlscript',
1123 'wml' => 'text/vnd.wap.wml',
1124 'wrl' => 'model/vrml',
1125 'xls' => 'application/vnd.ms-excel',
1126 'xml' => 'text/xml',
1127 'xwd' => 'image/x-xwindowdump',
1128 'z' => 'application/x-compress',
1129 'zip' => 'application/zip',
1130 %{$Conf{CgiExt2ContentType}}, # add site-specific values
1132 if ( !$Privileged ) {
1133 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files2}}"));
1136 ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
1138 $dir = "/" if ( $dir eq "" );
1139 my @Backups = $bpc->BackupInfoRead($host);
1140 my $view = BackupPC::View->new($bpc, $host, \@Backups);
1141 my $a = $view->fileAttrib($num, $share, $dir);
1142 if ( $dir =~ m{(^|/)\.\.(/|$)} || !defined($a) ) {
1143 ErrorExit("Can't restore bad file ${EscHTML($dir)}");
1145 my $f = BackupPC::FileZIO->open($a->{fullPath}, 0, $a->{compress});
1147 if ( !$skipHardLink && $a->{type} == BPC_FTYPE_HARDLINK ) {
1149 # hardlinks should look like the file they point to
1152 while ( $f->read(\$data, 65536) > 0 ) {
1156 $linkName =~ s/^\.\///;
1157 my $share = $1 if ( $dir =~ /^\/?(.*?)\// );
1158 restoreFile($host, $num, $share, $linkName, 1, $dir);
1161 $bpc->ServerMesg("log User $User recovered file $host/$num:$share/$dir ($a->{fullPath})");
1162 $dir = $origName if ( defined($origName) );
1163 my $ext = $1 if ( $dir =~ /\.([^\/\.]+)$/ );
1164 my $contentType = $Ext2ContentType->{lc($ext)}
1165 || "application/octet-stream";
1166 my $fileName = $1 if ( $dir =~ /.*\/(.*)/ );
1167 $fileName =~ s/"/\\"/g;
1168 print "Content-Type: $contentType\n";
1169 print "Content-Transfer-Encoding: binary\n";
1170 print "Content-Disposition: attachment; filename=\"$fileName\"\n\n";
1171 while ( $f->read(\$data, 1024 * 1024) > 0 ) {
1179 my $host = $1 if ( $In{host} =~ /(.*)/ );
1180 my($statusStr, $startIncrStr);
1184 return Action_GeneralInfo() if ( $host eq "" );
1186 if ( !-d "$TopDir/pc/$host" && -d "$TopDir/pc/" . lc($host) );
1187 if ( $host =~ /\.\./ || !-d "$TopDir/pc/$host" ) {
1189 # try to lookup by user name
1191 if ( !defined($Hosts->{$host}) ) {
1192 foreach my $h ( keys(%$Hosts) ) {
1193 if ( $Hosts->{$h}{user} eq $host
1194 || lc($Hosts->{$h}{user}) eq lc($host) ) {
1200 ErrorExit(eval("qq{$Lang->{Unknown_host_or_user}}"))
1201 if ( !defined($Hosts->{$host}) );
1205 GetStatusInfo("host(${EscURI($host)})");
1206 $bpc->ConfigRead($host);
1207 %Conf = $bpc->Conf();
1208 my $Privileged = CheckPermission($host);
1209 if ( !$Privileged ) {
1210 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_view_information_about}}"));
1212 ReadUserEmailInfo();
1214 my @Backups = $bpc->BackupInfoRead($host);
1215 my($str, $sizeStr, $compStr, $errStr, $warnStr);
1216 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
1217 my $startTime = timeStamp2($Backups[$i]{startTime});
1218 my $dur = $Backups[$i]{endTime} - $Backups[$i]{startTime};
1219 $dur = 1 if ( $dur <= 0 );
1220 my $duration = sprintf("%.1f", $dur / 60);
1221 my $MB = sprintf("%.1f", $Backups[$i]{size} / (1024*1024));
1222 my $MBperSec = sprintf("%.2f", $Backups[$i]{size} / (1024*1024*$dur));
1223 my $MBExist = sprintf("%.1f", $Backups[$i]{sizeExist} / (1024*1024));
1224 my $MBNew = sprintf("%.1f", $Backups[$i]{sizeNew} / (1024*1024));
1225 my($MBExistComp, $ExistComp, $MBNewComp, $NewComp);
1226 if ( $Backups[$i]{sizeExist} && $Backups[$i]{sizeExistComp} ) {
1227 $MBExistComp = sprintf("%.1f", $Backups[$i]{sizeExistComp}
1229 $ExistComp = sprintf("%.1f%%", 100 *
1230 (1 - $Backups[$i]{sizeExistComp} / $Backups[$i]{sizeExist}));
1232 if ( $Backups[$i]{sizeNew} && $Backups[$i]{sizeNewComp} ) {
1233 $MBNewComp = sprintf("%.1f", $Backups[$i]{sizeNewComp}
1235 $NewComp = sprintf("%.1f%%", 100 *
1236 (1 - $Backups[$i]{sizeNewComp} / $Backups[$i]{sizeNew}));
1238 my $age = sprintf("%.1f", (time - $Backups[$i]{startTime}) / (24*3600));
1239 my $browseURL = "$MyURL?action=browse&host=${EscURI($host)}&num=$Backups[$i]{num}";
1240 my $filled = $Backups[$i]{noFill} ? $Lang->{No} : $Lang->{Yes};
1241 $filled .= " ($Backups[$i]{fillFromNum}) "
1242 if ( $Backups[$i]{fillFromNum} ne "" );
1244 if ($Backups[$i]{type} eq "full") { $ltype = $Lang->{full}; }
1245 else { $ltype = $Lang->{incremental}; }
1247 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1248 <td align="center"> $ltype </td>
1249 <td align="center"> $filled </td>
1250 <td align="right"> $startTime </td>
1251 <td align="right"> $duration </td>
1252 <td align="right"> $age </td>
1253 <td align="left"> <tt>$TopDir/pc/$host/$Backups[$i]{num}</tt> </td></tr>
1256 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1257 <td align="center"> $ltype </td>
1258 <td align="right"> $Backups[$i]{nFiles} </td>
1259 <td align="right"> $MB </td>
1260 <td align="right"> $MBperSec </td>
1261 <td align="right"> $Backups[$i]{nFilesExist} </td>
1262 <td align="right"> $MBExist </td>
1263 <td align="right"> $Backups[$i]{nFilesNew} </td>
1264 <td align="right"> $MBNew </td>
1267 my $is_compress = $Backups[$i]{compress} || $Lang->{off};
1268 if (! $ExistComp) { $ExistComp = " "; }
1269 if (! $MBExistComp) { $MBExistComp = " "; }
1271 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1272 <td align="center"> $ltype </td>
1273 <td align="center"> $is_compress </td>
1274 <td align="right"> $MBExist </td>
1275 <td align="right"> $MBExistComp </td>
1276 <td align="right"> $ExistComp </td>
1277 <td align="right"> $MBNew </td>
1278 <td align="right"> $MBNewComp </td>
1279 <td align="right"> $NewComp </td>
1283 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1284 <td align="center"> $ltype </td>
1285 <td align="center"> <a href="$MyURL?action=view&type=XferLOG&num=$Backups[$i]{num}&host=${EscURI($host)}">$Lang->{XferLOG}</a>,
1286 <a href="$MyURL?action=view&type=XferErr&num=$Backups[$i]{num}&host=${EscURI($host)}">$Lang->{Errors}</a> </td>
1287 <td align="right"> $Backups[$i]{xferErrs} </td>
1288 <td align="right"> $Backups[$i]{xferBadFile} </td>
1289 <td align="right"> $Backups[$i]{xferBadShare} </td>
1290 <td align="right"> $Backups[$i]{tarErrs} </td></tr>
1294 my @Restores = $bpc->RestoreInfoRead($host);
1297 for ( my $i = 0 ; $i < @Restores ; $i++ ) {
1298 my $startTime = timeStamp2($Restores[$i]{startTime});
1299 my $dur = $Restores[$i]{endTime} - $Restores[$i]{startTime};
1300 $dur = 1 if ( $dur <= 0 );
1301 my $duration = sprintf("%.1f", $dur / 60);
1302 my $MB = sprintf("%.1f", $Restores[$i]{size} / (1024*1024));
1303 my $MBperSec = sprintf("%.2f", $Restores[$i]{size} / (1024*1024*$dur));
1304 my $Restores_Result = $Lang->{failed};
1305 if ($Restores[$i]{result} ne "failed") { $Restores_Result = $Lang->{success}; }
1306 $restoreStr .= <<EOF;
1307 <tr><td align="center"><a href="$MyURL?action=restoreInfo&num=$Restores[$i]{num}&host=${EscURI($host)}">$Restores[$i]{num}</a> </td>
1308 <td align="center"> $Restores_Result </td>
1309 <td align="right"> $startTime </td>
1310 <td align="right"> $duration </td>
1311 <td align="right"> $Restores[$i]{nFiles} </td>
1312 <td align="right"> $MB </td>
1313 <td align="right"> $Restores[$i]{tarCreateErrs} </td>
1314 <td align="right"> $Restores[$i]{xferErrs} </td>
1318 if ( $restoreStr ne "" ) {
1319 $restoreStr = eval("qq{$Lang->{Restore_Summary}}");
1321 if ( @Backups == 0 ) {
1322 $warnStr = $Lang->{This_PC_has_never_been_backed_up};
1324 if ( defined($Hosts->{$host}) ) {
1325 my $user = $Hosts->{$host}{user};
1326 my @moreUsers = sort(keys(%{$Hosts->{$host}{moreUsers}}));
1328 foreach my $u ( sort(keys(%{$Hosts->{$host}{moreUsers}})) ) {
1329 $moreUserStr .= ", " if ( $moreUserStr ne "" );
1330 $moreUserStr .= "${UserLink($u)}";
1332 if ( $moreUserStr ne "" ) {
1333 $moreUserStr = " ($Lang->{and} $moreUserStr).\n";
1335 $moreUserStr = ".\n";
1337 if ( $user ne "" ) {
1338 $statusStr .= eval("qq{$Lang->{This_PC_is_used_by}$moreUserStr}");
1340 if ( defined($UserEmailInfo{$user})
1341 && $UserEmailInfo{$user}{lastHost} eq $host ) {
1342 my $mailTime = timeStamp2($UserEmailInfo{$user}{lastTime});
1343 my $subj = $UserEmailInfo{$user}{lastSubj};
1344 $statusStr .= eval("qq{$Lang->{Last_email_sent_to__was_at___subject}}");
1347 if ( defined($Jobs{$host}) ) {
1348 my $startTime = timeStamp2($Jobs{$host}{startTime});
1349 (my $cmd = $Jobs{$host}{cmd}) =~ s/$BinDir\///g;
1350 $statusStr .= eval("qq{$Lang->{The_command_cmd_is_currently_running_for_started}}");
1352 if ( $StatusHost{BgQueueOn} ) {
1353 $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_background_queue_will_be_backed_up_soon}}");
1355 if ( $StatusHost{UserQueueOn} ) {
1356 $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_user_queue__will_be_backed_up_soon}}");
1358 if ( $StatusHost{CmdQueueOn} ) {
1359 $statusStr .= eval("qq{$Lang->{A_command_for_host_is_on_the_command_queue_will_run_soon}}");
1361 my $startTime = timeStamp2($StatusHost{endTime} == 0 ?
1362 $StatusHost{startTime} : $StatusHost{endTime});
1364 if ( $StatusHost{reason} ne "" ) {
1365 $reason = " ($Lang->{$StatusHost{reason}})";
1367 $statusStr .= eval("qq{$Lang->{Last_status_is_state_StatusHost_state_reason_as_of_startTime}}");
1369 if ( $StatusHost{state} ne "Status_backup_in_progress"
1370 && $StatusHost{state} ne "Status_restore_in_progress"
1371 && $StatusHost{error} ne "" ) {
1372 $statusStr .= eval("qq{$Lang->{Last_error_is____EscHTML_StatusHost_error}}");
1374 my $priorStr = "Pings";
1375 if ( $StatusHost{deadCnt} > 0 ) {
1376 $statusStr .= eval("qq{$Lang->{Pings_to_host_have_failed_StatusHost_deadCnt__consecutive_times}}");
1377 $priorStr = $Lang->{Prior_to_that__pings};
1379 if ( $StatusHost{aliveCnt} > 0 ) {
1380 $statusStr .= eval("qq{$Lang->{priorStr_to_host_have_succeeded_StatusHostaliveCnt_consecutive_times}}");
1382 if ( $StatusHost{aliveCnt} >= $Conf{BlackoutGoodCnt}
1383 && $Conf{BlackoutGoodCnt} >= 0 && $Conf{BlackoutHourBegin} >= 0
1384 && $Conf{BlackoutHourEnd} >= 0 ) {
1385 my(@days) = qw(Sun Mon Tue Wed Thu Fri Sat);
1386 my($days) = join(", ", @days[@{$Conf{BlackoutWeekDays}}]);
1387 my($t0) = sprintf("%d:%02d", $Conf{BlackoutHourBegin},
1388 60 * ($Conf{BlackoutHourBegin}
1389 - int($Conf{BlackoutHourBegin})));
1390 my($t1) = sprintf("%d:%02d", $Conf{BlackoutHourEnd},
1391 60 * ($Conf{BlackoutHourEnd}
1392 - int($Conf{BlackoutHourEnd})));
1393 $statusStr .= eval("qq{$Lang->{Because__host_has_been_on_the_network_at_least__Conf_BlackoutGoodCnt_consecutive_times___}}");
1396 if ( $StatusHost{backoffTime} > time ) {
1397 my $hours = sprintf("%.1f", ($StatusHost{backoffTime} - time) / 3600);
1398 $statusStr .= eval("qq{$Lang->{Backups_are_deferred_for_hours_hours_change_this_number}}");
1402 # only allow incremental if there are already some backups
1403 $startIncrStr = <<EOF;
1404 <input type="submit" value="\$Lang->{Start_Incr_Backup}" name="action">
1408 $startIncrStr = eval ("qq{$startIncrStr}");
1410 Header(eval("qq{$Lang->{Host__host_Backup_Summary}}"));
1411 print(eval("qq{$Lang->{Host__host_Backup_Summary2}}"));
1415 sub Action_GeneralInfo
1417 GetStatusInfo("info jobs hosts queueLen");
1418 my $Privileged = CheckPermission();
1420 my($jobStr, $statusStr);
1421 foreach my $host ( sort(keys(%Jobs)) ) {
1422 my $startTime = timeStamp2($Jobs{$host}{startTime});
1423 next if ( $host eq $bpc->trashJob
1424 && $Jobs{$host}{processState} ne "running" );
1425 $Jobs{$host}{type} = $Status{$host}{type}
1426 if ( $Jobs{$host}{type} eq "" && defined($Status{$host}));
1427 (my $cmd = $Jobs{$host}{cmd}) =~ s/$BinDir\///g;
1428 (my $xferPid = $Jobs{$host}{xferPid}) =~ s/,/, /g;
1430 <tr><td> ${HostLink($host)} </td>
1431 <td align="center"> $Jobs{$host}{type} </td>
1432 <td align="center"> ${UserLink(defined($Hosts->{$host})
1433 ? $Hosts->{$host}{user} : "")} </td>
1434 <td> $startTime </td>
1436 <td align="center"> $Jobs{$host}{pid} </td>
1437 <td align="center"> $xferPid </td>
1439 $jobStr .= "</tr>\n";
1441 foreach my $host ( sort(keys(%Status)) ) {
1442 next if ( $Status{$host}{reason} ne "Reason_backup_failed"
1443 && $Status{$host}{reason} ne "Reason_restore_failed"
1444 && (!$Status{$host}{userReq}
1445 || $Status{$host}{reason} ne "Reason_no_ping") );
1446 my $startTime = timeStamp2($Status{$host}{startTime});
1447 my($errorTime, $XferViewStr);
1448 if ( $Status{$host}{errorTime} > 0 ) {
1449 $errorTime = timeStamp2($Status{$host}{errorTime});
1451 if ( -f "$TopDir/pc/$host/SmbLOG.bad"
1452 || -f "$TopDir/pc/$host/SmbLOG.bad.z"
1453 || -f "$TopDir/pc/$host/XferLOG.bad"
1454 || -f "$TopDir/pc/$host/XferLOG.bad.z"
1456 $XferViewStr = <<EOF;
1457 <a href="$MyURL?action=view&type=XferLOGbad&host=${EscURI($host)}">$Lang->{XferLOG}</a>,
1458 <a href="$MyURL?action=view&type=XferErrbad&host=${EscURI($host)}">$Lang->{Errors}</a>
1463 (my $shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
1464 $statusStr .= <<EOF;
1465 <tr><td> ${HostLink($host)} </td>
1466 <td align="center"> $Status{$host}{type} </td>
1467 <td align="center"> ${UserLink(defined($Hosts->{$host})
1468 ? $Hosts->{$host}{user} : "")} </td>
1469 <td align="right"> $startTime </td>
1470 <td> $XferViewStr </td>
1471 <td align="right"> $errorTime </td>
1472 <td> ${EscHTML($shortErr)} </td></tr>
1475 my $now = timeStamp2(time);
1476 my $nextWakeupTime = timeStamp2($Info{nextWakeup});
1477 my $DUlastTime = timeStamp2($Info{DUlastValueTime});
1478 my $DUmaxTime = timeStamp2($Info{DUDailyMaxTime});
1479 my $numBgQueue = $QueueLen{BgQueue};
1480 my $numUserQueue = $QueueLen{UserQueue};
1481 my $numCmdQueue = $QueueLen{CmdQueue};
1482 my $serverStartTime = timeStamp2($Info{startTime});
1483 my $poolInfo = genPoolInfo("pool", \%Info);
1484 my $cpoolInfo = genPoolInfo("cpool", \%Info);
1485 if ( $Info{poolFileCnt} > 0 && $Info{cpoolFileCnt} > 0 ) {
1487 <li>Uncompressed pool:
1491 <li>Compressed pool:
1496 } elsif ( $Info{cpoolFileCnt} > 0 ) {
1497 $poolInfo = $cpoolInfo;
1500 Header($Lang->{H_BackupPC_Server_Status});
1501 print (eval ("qq{$Lang->{BackupPC_Server_Status}}"));
1505 sub Action_RestoreInfo
1507 my $Privileged = CheckPermission($In{host});
1508 my $host = $1 if ( $In{host} =~ /(.*)/ );
1512 if ( !$Privileged ) {
1513 ErrorExit($Lang->{Only_privileged_users_can_view_restore_information});
1516 # Find the requested restore
1518 my @Restores = $bpc->RestoreInfoRead($host);
1519 for ( $i = 0 ; $i < @Restores ; $i++ ) {
1520 last if ( $Restores[$i]{num} == $num );
1522 if ( $i >= @Restores ) {
1523 ErrorExit(eval("qq{$Lang->{Restore_number__num_for_host__does_not_exist}}"));
1527 do "$TopDir/pc/$host/RestoreInfo.$Restores[$i]{num}"
1528 if ( -f "$TopDir/pc/$host/RestoreInfo.$Restores[$i]{num}" );
1530 my $startTime = timeStamp2($Restores[$i]{startTime});
1531 my $reqTime = timeStamp2($RestoreReq{reqTime});
1532 my $dur = $Restores[$i]{endTime} - $Restores[$i]{startTime};
1533 $dur = 1 if ( $dur <= 0 );
1534 my $duration = sprintf("%.1f", $dur / 60);
1535 my $MB = sprintf("%.1f", $Restores[$i]{size} / (1024*1024));
1536 my $MBperSec = sprintf("%.2f", $Restores[$i]{size} / (1024*1024*$dur));
1538 my $fileListStr = "";
1539 foreach my $f ( @{$RestoreReq{fileList}} ) {
1540 my $targetFile = $f;
1541 (my $strippedShareSrc = $RestoreReq{shareSrc}) =~ s/^\///;
1542 (my $strippedShareDest = $RestoreReq{shareDest}) =~ s/^\///;
1543 substr($targetFile, 0, length($RestoreReq{pathHdrSrc}))
1544 = $RestoreReq{pathHdrDest};
1545 $fileListStr .= <<EOF;
1546 <tr><td>$RestoreReq{hostSrc}:/$strippedShareSrc$f</td><td>$RestoreReq{hostDest}:/$strippedShareDest$targetFile</td></tr>
1550 Header(eval("qq{$Lang->{Restore___num_details_for__host}}"));
1551 print(eval("qq{$Lang->{Restore___num_details_for__host2 }}"));
1555 ###########################################################################
1556 # Miscellaneous subroutines
1557 ###########################################################################
1561 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
1562 = localtime($_[0] == 0 ? time : $_[0] );
1565 if ( $Conf{CgiDateFormatMMDD} ) {
1566 return sprintf("$mon/$mday %02d:%02d", $hour, $min);
1568 return sprintf("$mday/$mon %02d:%02d", $hour, $min);
1576 if ( defined($Hosts->{$host}) || defined($Status{$host}) ) {
1577 $s = "<a href=\"$MyURL?host=${EscURI($host)}\">$host</a>";
1589 return \$user if ( $user eq ""
1590 || $Conf{CgiUserUrlCreate} eq "" );
1591 if ( $Conf{CgiUserHomePageCheck} eq ""
1592 || -f sprintf($Conf{CgiUserHomePageCheck}, $user, $user, $user) ) {
1594 . sprintf($Conf{CgiUserUrlCreate}, $user, $user, $user)
1606 $s =~ s/\"/"/g;
1609 $s =~ s{([^[:print:]])}{sprintf("&\#x%02X;", ord($1));}eg;
1616 $s =~ s{([^\w.\/-])}{sprintf("%%%02X", ord($1));}eg;
1623 my($head) = shift(@mesg);
1624 my($mesg) = join("</p>\n<p>", @mesg);
1625 $Conf{CgiHeaderFontType} ||= "arial";
1626 $Conf{CgiHeaderFontSize} ||= "3";
1627 $Conf{CgiNavBarBgColor} ||= "#ddeeee";
1628 $Conf{CgiHeaderBgColor} ||= "#99cc33";
1630 if ( !defined($ENV{REMOTE_USER}) ) {
1633 Note: \$ENV{REMOTE_USER} is not set, which could mean there is an
1634 installation problem. BackupPC_Admin expects Apache to authenticate
1635 the user and pass their user name into this script as the REMOTE_USER
1636 environment variable. See the documentation.
1640 $bpc->ServerMesg("log User $User (host=$In{host}) got CGI error: $head")
1641 if ( defined($bpc) );
1642 if ( !defined($Lang->{Error}) ) {
1643 Header("BackupPC: Error");
1644 $mesg = <<EOF if ( !defined($mesg) );
1645 There is some problem with the BackupPC installation.
1646 Please check the permissions on BackupPC_Admin.
1649 ${h1("Error: Unable to read config.pl or language strings!!")}
1654 Header(eval("qq{$Lang->{Error}}"));
1655 print (eval("qq{$Lang->{Error____head}}"));
1664 # Verify that the server connection is ok
1666 return if ( $bpc->ServerOK() );
1667 $bpc->ServerDisconnect();
1668 if ( my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}) ) {
1669 ErrorExit(eval("qq{$Lang->{Unable_to_connect_to_BackupPC_server}}"));
1677 my $reply = $bpc->ServerMesg("status $status");
1678 $reply = $1 if ( $reply =~ /(.*)/s );
1680 # ignore status related to admin and trashClean jobs
1681 if ( $status =~ /\bhosts\b/ ) {
1682 delete($Status{$bpc->adminJob});
1683 delete($Status{$bpc->trashJob});
1687 sub ReadUserEmailInfo
1689 if ( (stat("$TopDir/log/UserEmailInfo.pl"))[9] != $UserEmailInfoMTime ) {
1690 do "$TopDir/log/UserEmailInfo.pl";
1691 $UserEmailInfoMTime = (stat("$TopDir/log/UserEmailInfo.pl"))[9];
1696 # Check if the user is privileged. A privileged user can access
1697 # any information (backup files, logs, status pages etc).
1699 # A user is privileged if they belong to the group
1700 # $Conf{CgiAdminUserGroup}, or they are in $Conf{CgiAdminUsers}
1701 # or they are the user assigned to a host in the host file.
1708 return 0 if ( $User eq "" && $Conf{CgiAdminUsers} ne "*"
1709 || $host ne "" && !defined($Hosts->{$host}) );
1710 if ( $Conf{CgiAdminUserGroup} ne "" ) {
1711 my($n,$p,$gid,$mem) = getgrnam($Conf{CgiAdminUserGroup});
1712 $Privileged ||= ($mem =~ /\b$User\b/);
1714 if ( $Conf{CgiAdminUsers} ne "" ) {
1715 $Privileged ||= ($Conf{CgiAdminUsers} =~ /\b$User\b/);
1716 $Privileged ||= $Conf{CgiAdminUsers} eq "*";
1718 $PrivAdmin = $Privileged;
1719 $Privileged ||= $User eq $Hosts->{$host}{user};
1720 $Privileged ||= defined($Hosts->{$host}{moreUsers}{$User});
1726 # Returns the list of hosts that should appear in the navigation bar
1727 # for this user. If $Conf{CgiNavBarAdminAllHosts} is set, the admin
1728 # gets all the hosts. Otherwise, regular users get hosts for which
1729 # they are the user or are listed in the moreUsers column in the
1734 if ( $Conf{CgiNavBarAdminAllHosts} && CheckPermission() ) {
1735 return sort keys %$Hosts;
1738 return sort grep { $Hosts->{$_}{user} eq $User ||
1739 defined($Hosts->{$_}{moreUsers}{$User}) } keys(%$Hosts);
1743 # Given a host name tries to find the IP address. For non-dhcp hosts
1744 # we just return the host name. For dhcp hosts we check the address
1745 # the user is using ($ENV{REMOTE_ADDR}) and also the last-known IP
1746 # address for $host. (Later we should replace this with a broadcast
1749 sub ConfirmIPAddress
1754 if ( defined($Hosts->{$host}) && $Hosts->{$host}{dhcp}
1755 && $ENV{REMOTE_ADDR} =~ /^(\d+[\.\d]*)$/ ) {
1757 my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($ipAddr);
1758 if ( $netBiosHost ne $host ) {
1760 GetStatusInfo("host(${EscURI($host)})");
1761 if ( defined($StatusHost{dhcpHostIP})
1762 && $StatusHost{dhcpHostIP} ne $ipAddr ) {
1763 $tryIP = eval("qq{$Lang->{tryIP}}");
1764 ($netBiosHost, $netBiosUser)
1765 = $bpc->NetBiosInfoGet($StatusHost{dhcpHostIP});
1767 if ( $netBiosHost ne $host ) {
1768 ErrorExit(eval("qq{$Lang->{Can_t_find_IP_address_for}}"),
1769 eval("qq{$Lang->{host_is_a_DHCP_host}}"));
1771 $ipAddr = $StatusHost{dhcpHostIP};
1779 my($name, $info) = @_;
1780 my $poolSize = sprintf("%.2f", $info->{"${name}Kb"} / (1000 * 1024));
1781 my $poolRmSize = sprintf("%.2f", $info->{"${name}KbRm"} / (1000 * 1024));
1782 my $poolTime = timeStamp2($info->{"${name}Time"});
1783 $info->{"${name}FileCntRm"} = $info->{"${name}FileCntRm"} + 0;
1784 return eval("qq{$Lang->{Pool_Stat}}");
1787 ###########################################################################
1788 # HTML layout subroutines
1789 ###########################################################################
1795 { link => "", name => $Lang->{Status},
1797 { link => "?action=summary", name => $Lang->{PC_Summary} },
1798 { link => "?action=view&type=LOG", name => $Lang->{LOG_file} },
1799 { link => "?action=LOGlist", name => $Lang->{Old_LOGs} },
1800 { link => "?action=emailSummary", name => $Lang->{Email_summary} },
1801 { link => "?action=view&type=config", name => $Lang->{Config_file} },
1802 { link => "?action=view&type=hosts", name => $Lang->{Hosts_file} },
1803 { link => "?action=queue", name => $Lang->{Current_queues} },
1804 { link => "?action=view&type=docs", name => $Lang->{Documentation},
1806 { link => "http://backuppc.sourceforge.net/faq", name => "FAQ",
1808 { link => "http://backuppc.sourceforge.net", name => "SourceForge",
1811 print $Cgi->header();
1813 <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
1815 <title>$title</title>
1817 </head><body bgcolor="$Conf{CgiBodyBgColor}">
1818 <table cellpadding="0" cellspacing="0" border="0">
1819 <tr valign="top"><td valign="top" bgcolor="$Conf{CgiNavBarBgColor}" width="10%">
1821 NavSectionTitle("BackupPC");
1823 if ( defined($In{host}) && defined($Hosts->{$In{host}}) ) {
1824 my $host = $In{host};
1825 NavSectionTitle( eval("qq{$Lang->{Host_Inhost}}") );
1827 NavLink("?host=${EscURI($host)}", $Lang->{Home});
1828 NavLink("?action=view&type=LOG&host=${EscURI($host)}", $Lang->{LOG_file});
1829 NavLink("?action=LOGlist&host=${EscURI($host)}", $Lang->{Old_LOGs});
1830 if ( -f "$TopDir/pc/$host/SmbLOG.bad"
1831 || -f "$TopDir/pc/$host/SmbLOG.bad.z"
1832 || -f "$TopDir/pc/$host/XferLOG.bad"
1833 || -f "$TopDir/pc/$host/XferLOG.bad.z" ) {
1834 NavLink("?action=view&type=XferLOGbad&host=${EscURI($host)}",
1835 $Lang->{Last_bad_XferLOG});
1836 NavLink("?action=view&type=XferErrbad&host=${EscURI($host)}",
1837 $Lang->{Last_bad_XferLOG_errors_only});
1839 if ( -f "$TopDir/pc/$host/config.pl" ) {
1840 NavLink("?action=view&type=config&host=${EscURI($host)}", $Lang->{Config_file});
1844 NavSectionTitle($Lang->{Hosts});
1845 if ( defined($Hosts) && %$Hosts > 0 ) {
1847 foreach my $host ( GetUserHosts() ) {
1848 NavLink("?host=${EscURI($host)}", $host);
1853 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1854 <tr><td>$Lang->{Host_or_User_name}</td>
1855 <tr><td><form action="$MyURL" method="get"><small>
1856 <input type="text" name="host" size="10" maxlength="64">
1857 <input type="hidden" name="action" value="hostInfo"><input type="submit" value="$Lang->{Go}" name="ignore">
1858 </small></form></td></tr>
1861 NavSectionTitle($Lang->{NavSectionTitle_});
1863 foreach my $l ( @adminLinks ) {
1864 if ( $PrivAdmin || $l->{priv} ) {
1865 NavLink($l->{link}, $l->{name});
1867 NavLink(undef, $l->{name});
1872 </td><td valign="top" width="5"> </td>
1873 <td valign="top" width="90%">
1890 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1891 <tr><td bgcolor="$Conf{CgiHeaderBgColor}"><font face="$Conf{CgiHeaderFontType}"
1892 size="$Conf{CgiHeaderFontSize}"><b>$head</b>
1902 $padding = 1 if ( !defined($padding) );
1904 <table cellpadding="$padding" cellspacing="0" border="0" width="100%">
1915 my($link, $text) = @_;
1916 print "<tr><td width=\"2%\" valign=\"top\"><b>·</b></td>";
1917 if ( defined($link) ) {
1918 $link = "$MyURL$link" if ( $link eq "" || $link =~ /^\?/ );
1920 <td width="98%"><a href="$link"><small>$text</small></a></td></tr>
1924 <td width="98%"><small>$text</small></td></tr>
1933 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1935 <td bgcolor="$Conf{CgiHeaderBgColor}"> <font face="$Conf{CgiHeaderFontType}"
1936 size="$Conf{CgiHeaderFontSize}"><b>$str</b></font>
1946 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1948 <td bgcolor="$Conf{CgiHeaderBgColor}"> <font face="$Conf{CgiHeaderFontType}"
1949 size="$Conf{CgiHeaderFontSize}"><b>$str</b></font>