* Several improvements to restore: cancel now reports the correct
[BackupPC.git] / cgi-bin / BackupPC_Admin
1 #!/bin/perl -T
2 #============================================================= -*-perl-*-w
3 #
4 # BackupPC_Admin: Apache/CGI interface for BackupPC.
5 #
6 # DESCRIPTION
7 #   BackupPC_Admin provides a flexible web interface for BackupPC.
8 #   It is a CGI script that runs under Apache.
9 #
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
14 #   user name.
15 #
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.
19 #
20 # AUTHOR
21 #   Craig Barratt  <cbarratt@users.sourceforge.net>
22 #
23 # COPYRIGHT
24 #   Copyright (C) 2001  Craig Barratt
25 #
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.
30 #
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.
35 #
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
39 #
40 #========================================================================
41 #
42 # Version 2.0.0beta3, released 1 Jun 2003.
43 #
44 # See http://backuppc.sourceforge.net.
45 #
46 #========================================================================
47
48 use strict;
49 use CGI;
50 use lib "/usr/local/BackupPC/lib";
51 use BackupPC::Lib;
52 use BackupPC::FileZIO;
53 use BackupPC::Attrib qw(:all);
54 use BackupPC::View;
55 use Data::Dumper;
56
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);
62
63 use vars qw ($Lang);
64
65 $Cgi = new CGI;
66 %In = $Cgi->Vars;
67
68 #
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.
73 #
74 $MyURL  = $ENV{SCRIPT_NAME};
75 $User   = $ENV{REMOTE_USER};
76
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();
82     %Conf   = $bpc->Conf();
83     $Lang   = $bpc->Lang();
84     $ConfigMTime = $bpc->ConfigMTime();
85 } elsif ( $bpc->ConfigMTime() != $ConfigMTime ) {
86     $bpc->ConfigRead();
87     %Conf   = $bpc->Conf();
88     $ConfigMTime = $bpc->ConfigMTime();
89     $Lang   = $bpc->Lang();
90 }
91
92 #
93 # Clean up %ENV for taint checking
94 #
95 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
96 $ENV{PATH} = $Conf{MyPath};
97
98 #
99 # Verify we are running as the correct user
100 #
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}.
106 <p>
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.
112 EOF
113 }
114
115 if ( !defined($Hosts) || $bpc->HostsMTime() != $HostsMTime ) {
116     $HostsMTime = $bpc->HostsMTime();
117     $Hosts = $bpc->HostInfoRead();
118
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}) }
123     }
124 }
125
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,
141 );
142
143 #
144 # Set default actions, then call sub handler
145 #
146 $In{action} ||= "hostInfo"    if ( defined($In{host}) );
147 $In{action}   = "generalInfo" if ( !defined($ActionDispatch{$In{action}}) );
148 $ActionDispatch{$In{action}}();
149 exit(0);
150
151 ###########################################################################
152 # Action handling subroutines
153 ###########################################################################
154
155 sub Action_Summary
156 {
157     my($fullTot, $fullSizeTot, $incrTot, $incrSizeTot, $str,
158        $strNone, $strGood, $hostCntGood, $hostCntNone);
159
160     $hostCntGood = $hostCntNone = 0;
161     GetStatusInfo("hosts");
162     my $Privileged = CheckPermission();
163
164     if ( !$Privileged ) {
165         ErrorExit($Lang->{Only_privileged_users_can_view_PC_summaries} );
166     }
167     foreach my $host ( sort(keys(%Status)) ) {
168         my($fullDur, $incrCnt, $incrAge, $fullSize, $fullRate, $reasonHilite);
169         my($shortErr);
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" ) {
175                 $fullCnt++;
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};
180                 }
181                 $fullSizeTot += $Backups[$i]{size} / (1024 * 1024);
182             } else {
183                 $incrCnt++;
184                 if ( $incrAge < 0 || $Backups[$i]{startTime} > $incrAge ) {
185                     $incrAge = $Backups[$i]{startTime};
186                 }
187                 $incrSizeTot += $Backups[$i]{size} / (1024 * 1024);
188             }
189         }
190         if ( $fullAge < 0 ) {
191             $fullAge = "";
192             $fullRate = "";
193         } else {
194             $fullAge = sprintf("%.1f", (time - $fullAge) / (24 * 3600));
195             $fullRate = sprintf("%.2f",
196                                 $fullSize / ($fullDur <= 0 ? 1 : $fullDur));
197         }
198         if ( $incrAge < 0 ) {
199             $incrAge = "";
200         } else {
201             $incrAge = sprintf("%.1f", (time - $incrAge) / (24 * 3600));
202         }
203         $fullTot += $fullCnt;
204         $incrTot += $incrCnt;
205         $fullSize = sprintf("%.2f", $fullSize / 1000);
206         $incrAge = "&nbsp;" 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)";
215         }
216
217         $str = <<EOF;
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>
229 EOF
230         if ( @Backups == 0 ) {
231             $hostCntNone++;
232             $strNone .= $str;
233         } else {
234             $hostCntGood++;
235             $strGood .= $str;
236         }
237     }
238     $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
239     $incrSizeTot = sprintf("%.2f", $incrSizeTot / 1000);
240     my $now      = timeStamp2(time);
241
242     Header($Lang->{BackupPC__Server_Summary});
243     print eval ("qq{$Lang->{BackupPC_Summary}}");
244
245     Trailer();
246 }
247
248 sub Action_StartStopBackup
249 {
250     my($str, $reply);
251
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);
258
259     if ( !$Privileged ) {
260         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_stop_or_start_backups}}"));
261     }
262     ServerConnect();
263
264     if ( $In{doit} ) {
265         if ( $start ) {
266             if ( $Hosts->{$host}{dhcp} ) {
267                 $reply = $bpc->ServerMesg("backup $In{hostIP} ${EscURI($host)}"
268                                     . " $User $doFull");
269                 $str = eval("qq{$Lang->{Backup_requested_on_DHCP__host}}");
270             } else {
271                 $reply = $bpc->ServerMesg("backup ${EscURI($host)}"
272                                     . " ${EscURI($host)} $User $doFull");
273                 $str = eval("qq{$Lang->{Backup_requested_on__host_by__User}}");
274             }
275         } else {
276             $reply = $bpc->ServerMesg("stop ${EscURI($host)} $User $In{backoff}");
277             $str = eval("qq{$Lang->{Backup_stopped_dequeued_on__host_by__User}}");
278         }
279
280         Header(eval ("qq{$Lang->{BackupPC__Backup_Requested_on__host}}") );
281         print (eval ("qq{$Lang->{REPLY_FROM_SERVER}}"));
282
283         Trailer();
284     } else {
285         if ( $start ) {
286             my $ipAddr = ConfirmIPAddress($host);
287
288             Header(eval("qq{$Lang->{BackupPC__Start_Backup_Confirm_on__host}}"));
289             print (eval("qq{$Lang->{Are_you_sure_start}}"));
290         } else {
291             my $backoff = "";
292             GetStatusInfo("host(${EscURI($host)})");
293             if ( $StatusHost{backoffTime} > time ) {
294                 $backoff = sprintf("%.1f",
295                                   ($StatusHost{backoffTime} - time) / 3600);
296             }
297             Header($Lang->{BackupPC__Stop_Backup_Confirm_on__host});
298             print (eval ("qq{$Lang->{Are_you_sure_stop}}"));
299         }
300         Trailer();
301     }
302 }
303
304 sub Action_Queue
305 {
306     my($strBg, $strUser, $strCmd);
307
308     GetStatusInfo("queues");
309     my $Privileged = CheckPermission();
310
311     if ( !$Privileged ) {
312         ErrorExit($Lang->{Only_privileged_users_can_view_queues_});
313     }
314
315     while ( @BgQueue ) {
316         my $req = pop(@BgQueue);
317         my($reqTime) = timeStamp2($req->{reqTime});
318         $strBg .= <<EOF;
319 <tr><td> ${HostLink($req->{host})} </td>
320     <td align="center"> $reqTime </td>
321     <td align="center"> $req->{user} </td></tr>
322 EOF
323     }
324     while ( @UserQueue ) {
325         my $req = pop(@UserQueue);
326         my $reqTime = timeStamp2($req->{reqTime});
327         $strUser .= <<EOF;
328 <tr><td> ${HostLink($req->{host})} </td>
329     <td align="center"> $reqTime </td>
330     <td align="center"> $req->{user} </td></tr>
331 EOF
332     }
333     while ( @CmdQueue ) {
334         my $req = pop(@CmdQueue);
335         my $reqTime = timeStamp2($req->{reqTime});
336         (my $cmd = $req->{cmd}[0]) =~ s/$BinDir\///;
337         $strCmd .= <<EOF;
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>
342 EOF
343     }
344     Header($Lang->{BackupPC__Queue_Summary});
345
346     print ( eval ( "qq{$Lang->{Backup_Queue_Summary}}") );
347
348     Trailer();
349 }
350
351 sub Action_View
352 {
353     my $Privileged = CheckPermission($In{host});
354     my $compress = 0;
355     my $fh;
356     my $host = $In{host};
357     my $num  = $In{num};
358     my $type = $In{type};
359     my $linkHosts = 0;
360     my($file, $comment);
361     my $ext = $num ne "" ? ".$num" : "";
362
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"
387                                            && !-f $file );
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> );
393             close(LOG);
394             Trailer();
395         } else {
396             ErrorExit(eval("qq{$Lang->{Unable_to_open__file__configuration_problem}}"));
397         }
398         return;
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";
405     } else {
406         $file = "$TopDir/log/LOG$ext";
407         $linkHosts = 1;
408     }
409     if ( !$Privileged ) {
410         ErrorExit($Lang->{Only_privileged_users_can_view_log_or_config_files});
411     }
412     if ( !-f $file && -f "$file.z" ) {
413         $file .= ".z";
414         $compress = 1;
415     }
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);
420
421         print ( eval ("qq{$Lang->{Contents_of_log_file}}"));
422
423         print "<pre>";
424         if ( $type eq "XferErr" || $type eq "XferErrbad"
425                                 || $type eq "RestoreErr" ) {
426             my $skipped;
427             while ( 1 ) {
428                 $_ = $fh->readLine();
429                 if ( $_ eq "" ) {
430                     print(eval ("qq{$Lang->{skipped__skipped_lines}}"))
431                                                     if ( $skipped );
432                     last;
433                 }
434                 if ( /smb: \\>/
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
442                         || /^Domain=/i
443                         || /^Getting files newer than/i
444                         || /^Output is \/dev\/null/
445                         || /^\([\d\.]* kb\/s\) \(average [\d\.]* kb\/s\)$/
446                         || /^\s+directory \\/
447                         || /^Timezone is/
448                         || /^\.\//
449                         || /^  /
450                             ) {
451                     $skipped++;
452                     next;
453                 }
454                 print(eval("qq{$Lang->{skipped__skipped_lines}}"))
455                                                      if ( $skipped );
456                 $skipped = 0;
457                 print ${EscHTML($_)};
458             }
459         } elsif ( $linkHosts ) {
460             while ( 1 ) {
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;
466                 print $s;
467             }
468         } elsif ( $type eq "config" ) {
469             while ( 1 ) {
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($_)};
478             }
479         } else {
480             while ( 1 ) {
481                 $_ = $fh->readLine();
482                 last if ( $_ eq "" );
483                 print ${EscHTML($_)};
484             }
485         }
486         $fh->close();
487     } else {
488         printf( eval("qq{$Lang->{_pre___Can_t_open_log_file__file}}"));
489     }
490     print <<EOF;
491 </pre>
492 EOF
493     Trailer();
494 }
495
496 sub Action_LOGlist
497 {
498     my $Privileged = CheckPermission($In{host});
499
500     if ( !$Privileged ) {
501         ErrorExit($Lang->{Only_privileged_users_can_view_log_files});
502     }
503     my $host = $In{host};
504     my($url0, $hdr, $root, $str);
505     if ( $host ne "" ) {
506         $root = "$TopDir/pc/$host/LOG";
507         $url0 = "&host=${EscURI($host)}";
508         $hdr = "for host $host";
509     } else {
510         $root = "$TopDir/log/LOG";
511         $url0 = "";
512         $hdr = "";
513     }
514     for ( my $i = -1 ; ; $i++ ) {
515         my $url1 = "";
516         my $file = $root;
517         if ( $i >= 0 ) {
518             $file .= ".$i";
519             $url1 = "&num=$i";
520         }
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];
525         $str .= <<EOF;
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>
529 EOF
530     }
531     Header($Lang->{BackupPC__Log_File_History});
532     print (eval("qq{$Lang->{Log_File_History__hdr}}"));
533     Trailer();
534 }
535
536 sub Action_EmailSummary
537 {
538     my $Privileged = CheckPermission();
539
540     if ( !$Privileged ) {
541         ErrorExit($Lang->{Only_privileged_users_can_view_email_summaries});
542     }
543     GetStatusInfo("hosts");
544     ReadUserEmailInfo();
545     my(%EmailStr, $str);
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>
554 EOF
555     }
556     foreach my $t ( sort({$b <=> $a} keys(%EmailStr)) ) {
557         $str .= $EmailStr{$t};
558     }
559     Header($Lang->{Email_Summary});
560     print (eval("qq{$Lang->{Recent_Email_Summary}}"));
561     Trailer();
562 }
563
564 sub Action_Browse
565 {
566     my $Privileged = CheckPermission($In{host});
567     my($i, $dirStr, $fileStr, $attr);
568     my $checkBoxCnt = 0;
569
570     if ( !$Privileged ) {
571         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_browse_backup_files}}"));
572     }
573     my $host  = $In{host};
574     my $num   = $In{num};
575     my $share = $In{share};
576     my $dir   = $In{dir};
577
578     ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
579     #
580     # Find the requested backup and the previous filled backup
581     #
582     my @Backups = $bpc->BackupInfoRead($host);
583     for ( $i = 0 ; $i < @Backups ; $i++ ) {
584         last if ( $Backups[$i]{num} == $num );
585     }
586     if ( $i >= @Backups ) {
587         ErrorExit("Backup number $num for host ${EscHTML($host)} does"
588                 . " not exist.");
589     }
590     my $backupTime = timeStamp2($Backups[$i]{startTime});
591     my $backupAge = sprintf("%.1f", (time - $Backups[$i]{startTime})
592                                     / (24 * 3600));
593     my $view = BackupPC::View->new($bpc, $host, \@Backups);
594
595     if ( $dir eq "" || $dir eq "." || $dir eq ".." ) {
596         $attr = $view->dirAttrib($num, "", "");
597         if ( keys(%$attr) > 0 ) {
598             $share = (sort(keys(%$attr)))[0];
599             $dir   = '/';
600         } else {
601             ErrorExit(eval("qq{$Lang->{Directory___EscHTML}}"));
602         }
603     }
604     $dir = "/$dir" if ( $dir !~ /^\// );
605     my $relDir  = $dir;
606     my $currDir = undef;
607
608     #
609     # Loop up the directory tree until we hit the top.
610     #
611     my(@DirStrPrev);
612     while ( 1 ) {
613         my($fLast, $fLastum, @DirStr);
614
615         $attr = $view->dirAttrib($num, $share, $relDir);
616         if ( !defined($attr) ) {
617             ErrorExit(eval("qq{$Lang->{Can_t_browse_bad_directory_name2}}"));
618         }
619
620         my $fileCnt = 0;          # file counter
621         $fLast = $dirStr = "";
622
623         #
624         # Loop over each of the files in this directory
625         #
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 "" ) {
631                 $path = "/$f";
632             } else {
633                 ($path = "$relDir/$f") =~ s{//+}{/}g;
634             }
635             if ( $shareURI eq "" ) {
636                 $shareURI = $f;
637                 $path  = "/";
638             }
639             $path =~ s{^/+}{/};
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 ) {
645                 #
646                 # Display directory if it exists in current backup.
647                 # First find out if there are subdirs
648                 #
649                 my($bold, $unbold, $BGcolor);
650                 $img |= 1 << 6;
651                 $img |= 1 << 5 if ( $attr->{$f}{nlink} > 2 );
652                 if ( $dirOpen ) {
653                     $bold = "<b>";
654                     $unbold = "</b>";
655                     $img |= 1 << 2;
656                     $img |= 1 << 3 if ( $attr->{$f}{nlink} > 2 );
657                 }
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}\"";
662                 } else {
663                     $BGcolor = "";
664                 }
665                 my $dirName = $f;
666                 $dirName =~ s/ /&nbsp;/g;
667                 push(@DirStr, {needTick => 1,
668                                tdArgs   => $BGcolor,
669                                link     => <<EOF});
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">&nbsp;$bold$dirName$unbold</a></td></tr>
671 EOF
672                 $fileCnt++;
673                 $gotDir = 1;
674                 if ( $dirOpen ) {
675                     my($lastTick, $doneLastTick);
676                     foreach my $d ( @DirStrPrev ) {
677                         $lastTick = $d if ( $d->{needTick} );
678                     }
679                     $doneLastTick = 1 if ( !defined($lastTick) );
680                     foreach my $d ( @DirStrPrev ) {
681                         $img = 0;
682                         if  ( $d->{needTick} ) {
683                             $img |= 1 << 0;
684                         }
685                         if ( $d == $lastTick ) {
686                             $img |= 1 << 4;
687                             $doneLastTick = 1;
688                         } elsif ( !$doneLastTick ) {
689                             $img |= 1 << 3 | 1 << 4;
690                         }
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}
696                         });
697                     }
698                 }
699             }
700             if ( $relDir eq $dir ) {
701                 #
702                 # This is the selected directory, so display all the files
703                 #
704                 my $attrStr;
705                 if ( defined($a = $attr->{$f}) ) {
706                     my $mtimeStr = $bpc->timeStamp($a->{mtime});
707                     # UGH -> fix this
708                     my $typeStr  = BackupPC::Attrib::fileType2Text(undef,
709                                                                    $a->{type});
710                     my $modeStr  = sprintf("0%o", $a->{mode} & 07777);
711                     $attrStr .= <<EOF;
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>
717 </tr>
718 EOF
719                 } else {
720                     $attrStr .= "<td colspan=\"5\" align=\"center\"> </td>\n";
721                 }
722                 (my $fDisp = "${EscHTML($f)}") =~ s/ /&nbsp;/g;
723                 if ( $gotDir ) {
724                     $fileStr .= <<EOF;
725 <tr bgcolor="#ffffcc"><td><input type="checkbox" name="fcb$checkBoxCnt" value="$path">&nbsp;<a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a></td>
726 $attrStr
727 </tr>
728 EOF
729                 } else {
730                     $fileStr .= <<EOF;
731 <tr bgcolor="#ffffcc"><td><input type="checkbox" name="fcb$checkBoxCnt" value="$path">&nbsp;<a href="$MyURL?action=RestoreFile&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a></td>
732 $attrStr
733 </tr>
734 EOF
735                 }
736                 $checkBoxCnt++;
737             }
738         }
739         @DirStrPrev = @DirStr;
740         last if ( $relDir eq "" && $share eq "" );
741         # 
742         # Prune the last directory off $relDir, or at the very end
743         # do the top-level directory.
744         #
745         if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) {
746             $currDir = $share;
747             $share = "";
748             $relDir = "";
749         } else {
750             $relDir  = $1;
751             $currDir = $2;
752         }
753     }
754     $share = $currDir;
755     my $dirDisplay = "$share/$dir";
756     $dirDisplay =~ s{//+}{/}g;
757     $dirDisplay =~ s{/+$}{}g;
758     $dirDisplay = "/" if ( $dirDisplay eq "" );
759     my $filledBackup;
760
761     if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) {
762         shift(@mergeNums);
763         my $numF = join(", #", @mergeNums);
764         $filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}");
765     }
766     Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"));
767
768     foreach my $d ( @DirStrPrev ) {
769         $dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n";
770     }
771
772     ### hide checkall button if there are no files
773     my ($topCheckAll, $checkAll, $fileHeader);
774     if ( $fileStr ) {
775         $fileHeader = eval("qq{$Lang->{fileHeader}}");
776
777         $checkAll = $Lang->{checkAll};
778
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;
783         }
784     } else {
785         $fileStr = eval("qq{$Lang->{The_directory_is_empty}}");
786     }
787     my @otherDirs;
788     foreach my $i ( $view->backupList($share, $dir) ) {
789         my $path = $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>");
795
796     }
797     if ( @otherDirs ) {
798         my $otherDirs  = join(",\n", @otherDirs);
799         $filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}");
800     }
801     print (eval("qq{$Lang->{Backup_browse_for__host}}"));
802     Trailer();
803 }
804
805 sub Action_Restore
806 {
807     my($str, $reply);
808     my $Privileged = CheckPermission($In{host});
809     if ( !$Privileged ) {
810         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files}}"));
811     }
812     my $host  = $In{host};
813     my $num   = $In{num};
814     my $share = $In{share};
815     my(@fileList, $fileListStr, $hiddenStr, $pathHdr, $badFileCnt);
816     my @Backups = $bpc->BackupInfoRead($host);
817
818     ServerConnect();
819     if ( !defined($Hosts->{$host}) ) {
820         ErrorExit(eval("qq{$Lang->{Bad_host_name}}"));
821     }
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 ) {
827             $pathHdr = $name;
828         } else {
829             while ( substr($name, 0, length($pathHdr)) ne $pathHdr ) {
830                 $pathHdr = substr($pathHdr, 0, rindex($pathHdr, "/"));
831             }
832         }
833         push(@fileList, $name);
834         $hiddenStr .= <<EOF;
835 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
836 EOF
837         $fileListStr .= <<EOF;
838 <li> ${EscHTML($name)}
839 EOF
840     }
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});
847     }
848     if ( $badFileCnt ) {
849         ErrorExit($Lang->{Nice_try__but_you_can_t_put});
850     }
851     if ( @fileList == 1 ) {
852         $pathHdr =~ s/(.*)\/.*/$1/;
853     }
854     $pathHdr = "/" if ( $pathHdr eq "" );
855     if ( $In{type} != 0 && @fileList == $In{fcbMax} ) {
856         #
857         # All the files in the list were selected, so just restore the
858         # entire parent directory
859         #
860         @fileList = ( $pathHdr );
861     }
862     if ( $In{type} == 0 ) {
863         #
864         # Tell the user what options they have
865         #
866         Header(eval("qq{$Lang->{Restore_Options_for__host}}"));
867         print(eval("qq{$Lang->{Restore_Options_for__host2}}"));
868
869         #
870         # Verify that Archive::Zip is available before showing the
871         # zip restore option
872         #
873         if ( eval { require Archive::Zip } ) {
874             print (eval("qq{$Lang->{Option_2__Download_Zip_archive}}"));
875         } else {
876             print (eval("qq{$Lang->{Option_2__Download_Zip_archive2}}"));
877         }
878         print (eval("qq{$Lang->{Option_3__Download_Zip_archive}}"));
879         Trailer();
880     } elsif ( $In{type} == 1 ) {
881         #
882         # Provide the selected files via a tar archive.
883         #
884         my @fileListTrim = @fileList;
885         if ( @fileListTrim > 10 ) {
886             @fileListTrim = (@fileListTrim[0..9], '...');
887         }
888         $bpc->ServerMesg("log User $User downloaded tar archive for $host,"
889                        . " backup $num; files were: "
890                        . join(", ", @fileListTrim));
891
892         my @pathOpts;
893         if ( $In{relative} ) {
894             @pathOpts = ("-r", $pathHdr, "-p", "");
895         }
896         print(STDOUT <<EOF);
897 Content-Type: application/x-gtar
898 Content-Transfer-Encoding: binary
899 Content-Disposition: attachment; filename=\"restore.tar\"
900
901 EOF
902         #
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
905         # under mod_perl.
906         #
907         $bpc->cmdSystemOrEval(["$BinDir/BackupPC_tarCreate",
908                  "-h", $host,
909                  "-n", $num,
910                  "-s", $share,
911                  @pathOpts,
912                  @fileList
913             ],
914             sub { print(@_); }
915         );
916     } elsif ( $In{type} == 2 ) {
917         #
918         # Provide the selected files via a zip archive.
919         #
920         my @fileListTrim = @fileList;
921         if ( @fileListTrim > 10 ) {
922             @fileListTrim = (@fileListTrim[0..9], '...');
923         }
924         $bpc->ServerMesg("log User $User downloaded zip archive for $host,"
925                        . " backup $num; files were: "
926                        . join(", ", @fileListTrim));
927
928         my @pathOpts;
929         if ( $In{relative} ) {
930             @pathOpts = ("-r", $pathHdr, "-p", "");
931         }
932         print(STDOUT <<EOF);
933 Content-Type: application/zip
934 Content-Transfer-Encoding: binary
935 Content-Disposition: attachment; filename=\"restore.zip\"
936
937 EOF
938         $In{compressLevel} = 5 if ( $In{compressLevel} !~ /^\d+$/ );
939         #
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
942         # under mod_perl.
943         #
944         $bpc->cmdSystemOrEval(["$BinDir/BackupPC_zipCreate",
945                  "-h", $host,
946                  "-n", $num,
947                  "-c", $In{compressLevel},
948                  "-s", $share,
949                  @pathOpts,
950                  @fileList
951             ],
952             sub { print(@_); }
953         );
954     } elsif ( $In{type} == 3 ) {
955         #
956         # Do restore directly onto host
957         #
958         if ( !defined($Hosts->{$In{hostDest}}) ) {
959             ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
960         }
961         if ( !CheckPermission($In{hostDest}) ) {
962             ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
963         }
964         $fileListStr = "";
965         foreach my $f ( @fileList ) {
966             my $targetFile = $f;
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>
972 EOF
973         }
974         Header(eval("qq{$Lang->{Restore_Confirm_on__host}}"));
975         print(eval("qq{$Lang->{Are_you_sure}}"));
976         Trailer();
977     } elsif ( $In{type} == 4 ) {
978         if ( !defined($Hosts->{$In{hostDest}}) ) {
979             ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
980         }
981         if ( !CheckPermission($In{hostDest}) ) {
982             ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
983         }
984         my $hostDest = $1 if ( $In{hostDest} =~ /(.+)/ );
985         my $ipAddr = ConfirmIPAddress($hostDest);
986         #
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.
991         #
992         my $reqFileName;
993         for ( my $i = 0 ; ; $i++ ) {
994             $reqFileName = "restoreReq.$$.$i";
995             last if ( !-f "$TopDir/pc/$hostDest/$reqFileName" );
996         }
997         my %restoreReq = (
998             # source of restore is hostSrc, #num, path shareSrc/pathHdrSrc
999             num         => $In{num},
1000             hostSrc     => $host,
1001             shareSrc    => $share,
1002             pathHdrSrc  => $pathHdr,
1003
1004             # destination of restore is hostDest:shareDest/pathHdrDest
1005             hostDest    => $hostDest,
1006             shareDest   => $In{shareDest},
1007             pathHdrDest => $In{pathHdr},
1008
1009             # list of files to restore
1010             fileList    => \@fileList,
1011
1012             # other info
1013             user        => $User,
1014             reqTime     => time,
1015         );
1016         my($dump) = Data::Dumper->new(
1017                          [  \%restoreReq],
1018                          [qw(*RestoreReq)]);
1019         $dump->Indent(1);
1020         if ( open(REQ, ">$TopDir/pc/$hostDest/$reqFileName") ) {
1021             print(REQ $dump->Dump);
1022             close(REQ);
1023         } else {
1024             ErrorExit(eval("qq{$Lang->{Can_t_open_create}}"));
1025         }
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}}"));
1031         Trailer();
1032     }
1033 }
1034
1035 sub Action_RestoreFile
1036 {
1037     restoreFile($In{host}, $In{num}, $In{share}, $In{dir});
1038 }
1039
1040 sub restoreFile
1041 {
1042     my($host, $num, $share, $dir, $skipHardLink, $origName) = @_;
1043     my($Privileged) = CheckPermission($host);
1044
1045     #
1046     # Some common content (media) types from www.iana.org (via MIME::Types).
1047     #
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
1131     };
1132     if ( !$Privileged ) {
1133         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files2}}"));
1134     }
1135     ServerConnect();
1136     ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
1137
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)}");
1144     }
1145     my $f = BackupPC::FileZIO->open($a->{fullPath}, 0, $a->{compress});
1146     my $data;
1147     if ( !$skipHardLink && $a->{type} == BPC_FTYPE_HARDLINK ) {
1148         #
1149         # hardlinks should look like the file they point to
1150         #
1151         my $linkName;
1152         while ( $f->read(\$data, 65536) > 0 ) {
1153             $linkName .= $data;
1154         }
1155         $f->close;
1156         $linkName =~ s/^\.\///;
1157         my $share = $1 if ( $dir =~ /^\/?(.*?)\// );
1158         restoreFile($host, $num, $share, $linkName, 1, $dir);
1159         return;
1160     }
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 ) {
1172         print STDOUT $data;
1173     }
1174     $f->close;
1175 }
1176
1177 sub Action_HostInfo
1178 {
1179     my $host = $1 if ( $In{host} =~ /(.*)/ );
1180     my($statusStr, $startIncrStr);
1181
1182     $host =~ s/^\s+//;
1183     $host =~ s/\s+$//;
1184     return Action_GeneralInfo() if ( $host eq "" );
1185     $host = lc($host)
1186                 if ( !-d "$TopDir/pc/$host" && -d "$TopDir/pc/" . lc($host) );
1187     if ( $host =~ /\.\./ || !-d "$TopDir/pc/$host" ) {
1188         #
1189         # try to lookup by user name
1190         #
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) ) {
1195                     $host = $h;
1196                     last;
1197                 }
1198             }
1199             CheckPermission();
1200             ErrorExit(eval("qq{$Lang->{Unknown_host_or_user}}"))
1201                                 if ( !defined($Hosts->{$host}) );
1202         }
1203         $In{host} = $host;
1204     }
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}}"));
1211     }
1212     ReadUserEmailInfo();
1213
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}
1228                                                 / (1024 * 1024));
1229             $ExistComp = sprintf("%.1f%%", 100 *
1230                   (1 - $Backups[$i]{sizeExistComp} / $Backups[$i]{sizeExist}));
1231         }
1232         if ( $Backups[$i]{sizeNew} && $Backups[$i]{sizeNewComp} ) {
1233             $MBNewComp = sprintf("%.1f", $Backups[$i]{sizeNewComp}
1234                                                 / (1024 * 1024));
1235             $NewComp = sprintf("%.1f%%", 100 *
1236                   (1 - $Backups[$i]{sizeNewComp} / $Backups[$i]{sizeNew}));
1237         }
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 "" );
1243         my $ltype;
1244         if ($Backups[$i]{type} eq "full") { $ltype = $Lang->{full}; }
1245         else { $ltype = $Lang->{incremental}; }
1246         $str .= <<EOF;
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>
1254 EOF
1255         $sizeStr .= <<EOF;
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>
1265 </tr>
1266 EOF
1267         my $is_compress = $Backups[$i]{compress} || $Lang->{off};
1268         if (! $ExistComp) { $ExistComp = "&nbsp;"; }
1269         if (! $MBExistComp) { $MBExistComp = "&nbsp;"; }
1270         $compStr .= <<EOF;
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>
1280 </tr>
1281 EOF
1282         $errStr .= <<EOF;
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>
1291 EOF
1292     }
1293
1294     my @Restores = $bpc->RestoreInfoRead($host);
1295     my $restoreStr;
1296
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>
1315 </tr>
1316 EOF
1317     }
1318     if ( $restoreStr ne "" ) {
1319         $restoreStr = eval("qq{$Lang->{Restore_Summary}}");
1320     }
1321     if ( @Backups == 0 ) {
1322         $warnStr = $Lang->{This_PC_has_never_been_backed_up};
1323     }
1324     if ( defined($Hosts->{$host}) ) {
1325         my $user = $Hosts->{$host}{user};
1326         my @moreUsers = sort(keys(%{$Hosts->{$host}{moreUsers}}));
1327         my $moreUserStr;
1328         foreach my $u ( sort(keys(%{$Hosts->{$host}{moreUsers}})) ) {
1329             $moreUserStr .= ", " if ( $moreUserStr ne "" );
1330             $moreUserStr .= "${UserLink($u)}";
1331         }
1332         if ( $moreUserStr ne "" ) {
1333             $moreUserStr = " ($Lang->{and} $moreUserStr).\n";
1334         } else {
1335             $moreUserStr = ".\n";
1336         }
1337         if ( $user ne "" ) {
1338             $statusStr .= eval("qq{$Lang->{This_PC_is_used_by}$moreUserStr}");
1339         }
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}}");
1345         }
1346     }
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}}");
1351     }
1352     if ( $StatusHost{BgQueueOn} ) {
1353         $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_background_queue_will_be_backed_up_soon}}");
1354     }
1355     if ( $StatusHost{UserQueueOn} ) {
1356         $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_user_queue__will_be_backed_up_soon}}");
1357     }
1358     if ( $StatusHost{CmdQueueOn} ) {
1359         $statusStr .= eval("qq{$Lang->{A_command_for_host_is_on_the_command_queue_will_run_soon}}");
1360     }
1361     my $startTime = timeStamp2($StatusHost{endTime} == 0 ?
1362                 $StatusHost{startTime} : $StatusHost{endTime});
1363     my $reason = "";
1364     if ( $StatusHost{reason} ne "" ) {
1365         $reason = " ($Lang->{$StatusHost{reason}})";
1366     }
1367     $statusStr .= eval("qq{$Lang->{Last_status_is_state_StatusHost_state_reason_as_of_startTime}}");
1368
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}}");
1373     }
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};
1378     }
1379     if ( $StatusHost{aliveCnt} > 0 ) {
1380         $statusStr .= eval("qq{$Lang->{priorStr_to_host_have_succeeded_StatusHostaliveCnt_consecutive_times}}");
1381
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___}}");
1394         }
1395     }
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}}");
1399
1400     }
1401     if ( @Backups ) {
1402         # only allow incremental if there are already some backups
1403         $startIncrStr = <<EOF;
1404 <input type="submit" value="\$Lang->{Start_Incr_Backup}" name="action">
1405 EOF
1406     }
1407
1408     $startIncrStr = eval ("qq{$startIncrStr}");
1409
1410     Header(eval("qq{$Lang->{Host__host_Backup_Summary}}"));
1411     print(eval("qq{$Lang->{Host__host_Backup_Summary2}}"));
1412     Trailer();
1413 }
1414
1415 sub Action_GeneralInfo
1416 {
1417     GetStatusInfo("info jobs hosts queueLen");
1418     my $Privileged = CheckPermission();
1419
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;
1429         $jobStr .= <<EOF;
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>
1435     <td> $cmd </td>
1436     <td align="center"> $Jobs{$host}{pid} </td>
1437     <td align="center"> $xferPid </td>
1438 EOF
1439         $jobStr .= "</tr>\n";
1440     }
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});
1450         }
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"
1455                 ) {
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>
1459 EOF
1460         } else {
1461             $XferViewStr = "";
1462         }
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>
1473 EOF
1474     }
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 ) {
1486         $poolInfo = <<EOF;
1487 <li>Uncompressed pool:
1488 <ul>
1489 $poolInfo
1490 </ul>
1491 <li>Compressed pool:
1492 <ul>
1493 $cpoolInfo
1494 </ul>
1495 EOF
1496     } elsif ( $Info{cpoolFileCnt} > 0 ) {
1497         $poolInfo = $cpoolInfo;
1498     }
1499
1500     Header($Lang->{H_BackupPC_Server_Status});
1501     print (eval ("qq{$Lang->{BackupPC_Server_Status}}"));
1502     Trailer();
1503 }
1504
1505 sub Action_RestoreInfo
1506 {
1507     my $Privileged = CheckPermission($In{host});
1508     my $host = $1 if ( $In{host} =~ /(.*)/ );
1509     my $num  = $In{num};
1510     my $i;
1511
1512     if ( !$Privileged ) {
1513         ErrorExit($Lang->{Only_privileged_users_can_view_restore_information});
1514     }
1515     #
1516     # Find the requested restore
1517     #
1518     my @Restores = $bpc->RestoreInfoRead($host);
1519     for ( $i = 0 ; $i < @Restores ; $i++ ) {
1520         last if ( $Restores[$i]{num} == $num );
1521     }
1522     if ( $i >= @Restores ) {
1523         ErrorExit(eval("qq{$Lang->{Restore_number__num_for_host__does_not_exist}}"));
1524     }
1525
1526     %RestoreReq = ();
1527     do "$TopDir/pc/$host/RestoreInfo.$Restores[$i]{num}"
1528             if ( -f "$TopDir/pc/$host/RestoreInfo.$Restores[$i]{num}" );
1529
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));
1537
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>
1547 EOF
1548     }
1549
1550     Header(eval("qq{$Lang->{Restore___num_details_for__host}}"));
1551     print(eval("qq{$Lang->{Restore___num_details_for__host2 }}"));
1552     Trailer();
1553 }
1554     
1555 ###########################################################################
1556 # Miscellaneous subroutines
1557 ###########################################################################
1558
1559 sub timeStamp2
1560 {
1561     my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
1562               = localtime($_[0] == 0 ? time : $_[0] );
1563     $year += 1900;
1564     $mon++;
1565     if ( $Conf{CgiDateFormatMMDD} ) {
1566         return sprintf("$mon/$mday %02d:%02d", $hour, $min);
1567     } else {
1568         return sprintf("$mday/$mon %02d:%02d", $hour, $min);
1569     }
1570 }
1571
1572 sub HostLink
1573 {
1574     my($host) = @_;
1575     my($s);
1576     if ( defined($Hosts->{$host}) || defined($Status{$host}) ) {
1577         $s = "<a href=\"$MyURL?host=${EscURI($host)}\">$host</a>";
1578     } else {
1579         $s = $host;
1580     }
1581     return \$s;
1582 }
1583
1584 sub UserLink
1585 {
1586     my($user) = @_;
1587     my($s);
1588
1589     return \$user if ( $user eq ""
1590                     || $Conf{CgiUserUrlCreate} eq "" );
1591     if ( $Conf{CgiUserHomePageCheck} eq ""
1592             || -f sprintf($Conf{CgiUserHomePageCheck}, $user, $user, $user) ) {
1593         $s = "<a href=\""
1594              . sprintf($Conf{CgiUserUrlCreate}, $user, $user, $user)
1595              . "\">$user</a>";
1596     } else {
1597         $s = $user;
1598     }
1599     return \$s;
1600 }
1601
1602 sub EscHTML
1603 {
1604     my($s) = @_;
1605     $s =~ s/&/&amp;/g;
1606     $s =~ s/\"/&quot;/g;
1607     $s =~ s/>/&gt;/g;
1608     $s =~ s/</&lt;/g;
1609     $s =~ s{([^[:print:]])}{sprintf("&\#x%02X;", ord($1));}eg;
1610     return \$s;
1611 }
1612
1613 sub EscURI
1614 {
1615     my($s) = @_;
1616     $s =~ s{([^\w.\/-])}{sprintf("%%%02X", ord($1));}eg;
1617     return \$s;
1618 }
1619
1620 sub ErrorExit
1621 {
1622     my(@mesg) = @_;
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";
1629
1630     if ( !defined($ENV{REMOTE_USER}) ) {
1631         $mesg .= <<EOF;
1632 <p>
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.
1637 EOF
1638     }
1639
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.
1647 EOF
1648         print <<EOF;
1649 ${h1("Error: Unable to read config.pl or language strings!!")}
1650 <p>$mesg</p>
1651 EOF
1652         Trailer();
1653     } else {
1654         Header(eval("qq{$Lang->{Error}}"));
1655         print (eval("qq{$Lang->{Error____head}}"));
1656         Trailer();
1657     }
1658     exit(1);
1659 }
1660
1661 sub ServerConnect
1662 {
1663     #
1664     # Verify that the server connection is ok
1665     #
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}}"));
1670     }
1671 }
1672
1673 sub GetStatusInfo
1674 {
1675     my($status) = @_;
1676     ServerConnect();
1677     my $reply = $bpc->ServerMesg("status $status");
1678     $reply = $1 if ( $reply =~ /(.*)/s );
1679     eval($reply);
1680     # ignore status related to admin and trashClean jobs
1681     if ( $status =~ /\bhosts\b/ ) {
1682         delete($Status{$bpc->adminJob});
1683         delete($Status{$bpc->trashJob});
1684     }
1685 }
1686
1687 sub ReadUserEmailInfo
1688 {
1689     if ( (stat("$TopDir/log/UserEmailInfo.pl"))[9] != $UserEmailInfoMTime ) {
1690         do "$TopDir/log/UserEmailInfo.pl";
1691         $UserEmailInfoMTime = (stat("$TopDir/log/UserEmailInfo.pl"))[9];
1692     }
1693 }
1694
1695 #
1696 # Check if the user is privileged.  A privileged user can access
1697 # any information (backup files, logs, status pages etc).
1698 #
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.
1702 #
1703 sub CheckPermission
1704 {
1705     my($host) = @_;
1706     my $Privileged = 0;
1707
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/);
1713     }
1714     if ( $Conf{CgiAdminUsers} ne "" ) {
1715         $Privileged ||= ($Conf{CgiAdminUsers} =~ /\b$User\b/);
1716         $Privileged ||= $Conf{CgiAdminUsers} eq "*";
1717     }
1718     $PrivAdmin = $Privileged;
1719     $Privileged ||= $User eq $Hosts->{$host}{user};
1720     $Privileged ||= defined($Hosts->{$host}{moreUsers}{$User});
1721
1722     return $Privileged;
1723 }
1724
1725 #
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
1730 # hosts file.
1731 #
1732 sub GetUserHosts
1733 {
1734     if ( $Conf{CgiNavBarAdminAllHosts} && CheckPermission() ) {
1735        return sort keys %$Hosts;
1736     }
1737
1738     return sort grep { $Hosts->{$_}{user} eq $User ||
1739                        defined($Hosts->{$_}{moreUsers}{$User}) } keys(%$Hosts);
1740 }
1741
1742 #
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
1747 # nmblookup.)
1748 #
1749 sub ConfirmIPAddress
1750 {
1751     my($host) = @_;
1752     my $ipAddr = $host;
1753
1754     if ( defined($Hosts->{$host}) && $Hosts->{$host}{dhcp}
1755                && $ENV{REMOTE_ADDR} =~ /^(\d+[\.\d]*)$/ ) {
1756         $ipAddr = $1;
1757         my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($ipAddr);
1758         if ( $netBiosHost ne $host ) {
1759             my($tryIP);
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});
1766             }
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}}"));
1770             }
1771             $ipAddr = $StatusHost{dhcpHostIP};
1772         }
1773     }
1774     return $ipAddr;
1775 }
1776
1777 sub genPoolInfo
1778 {
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}}");
1785 }
1786
1787 ###########################################################################
1788 # HTML layout subroutines
1789 ###########################################################################
1790
1791 sub Header
1792 {
1793     my($title) = @_;
1794     my @adminLinks = (
1795         { link => "",                          name => $Lang->{Status},
1796                                                priv => 1},
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},
1805                                                priv => 1},
1806         { link => "http://backuppc.sourceforge.net/faq", name => "FAQ",
1807                                                priv => 1},
1808         { link => "http://backuppc.sourceforge.net", name => "SourceForge",
1809                                                priv => 1},
1810     );
1811     print $Cgi->header();
1812     print <<EOF;
1813 <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
1814 <html><head>
1815 <title>$title</title>
1816 $Conf{CgiHeaders}
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%">
1820 EOF
1821     NavSectionTitle("BackupPC");
1822     print "&nbsp;\n";
1823     if ( defined($In{host}) && defined($Hosts->{$In{host}}) ) {
1824         my $host = $In{host};
1825         NavSectionTitle( eval("qq{$Lang->{Host_Inhost}}") );
1826         NavSectionStart();
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});
1838         }
1839         if ( -f "$TopDir/pc/$host/config.pl" ) {
1840             NavLink("?action=view&type=config&host=${EscURI($host)}", $Lang->{Config_file});
1841         }
1842         NavSectionEnd();
1843     }
1844     NavSectionTitle($Lang->{Hosts});
1845     if ( defined($Hosts) && %$Hosts > 0 ) {
1846         NavSectionStart(1);
1847         foreach my $host ( GetUserHosts() ) {
1848             NavLink("?host=${EscURI($host)}", $host);
1849         }
1850         NavSectionEnd();
1851     }
1852     print <<EOF;
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>
1859 </table>
1860 EOF
1861     NavSectionTitle($Lang->{NavSectionTitle_});
1862     NavSectionStart();
1863     foreach my $l ( @adminLinks ) {
1864         if ( $PrivAdmin || $l->{priv} ) {
1865             NavLink($l->{link}, $l->{name});
1866         } else {
1867             NavLink(undef, $l->{name});
1868         }
1869     }
1870     NavSectionEnd();
1871     print <<EOF;
1872 </td><td valign="top" width="5">&nbsp;&nbsp;</td>
1873 <td valign="top" width="90%">
1874 EOF
1875 }
1876
1877 sub Trailer
1878 {
1879     print <<EOF;
1880 </td></table>
1881 </body></html>
1882 EOF
1883 }
1884
1885
1886 sub NavSectionTitle
1887 {
1888     my($head) = @_;
1889     print <<EOF;
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>
1893 </font></td></tr>
1894 </table>
1895 EOF
1896 }
1897
1898 sub NavSectionStart
1899 {
1900     my($padding) = @_;
1901
1902     $padding = 1 if ( !defined($padding) );
1903     print <<EOF;
1904 <table cellpadding="$padding" cellspacing="0" border="0" width="100%">
1905 EOF
1906 }
1907
1908 sub NavSectionEnd
1909 {
1910     print "</table>\n";
1911 }
1912
1913 sub NavLink
1914 {
1915     my($link, $text) = @_;
1916     print "<tr><td width=\"2%\" valign=\"top\"><b>&middot;</b></td>";
1917     if ( defined($link) ) {
1918         $link = "$MyURL$link" if ( $link eq "" || $link =~ /^\?/ );
1919         print <<EOF;
1920 <td width="98%"><a href="$link"><small>$text</small></a></td></tr>
1921 EOF
1922     } else {
1923         print <<EOF;
1924 <td width="98%"><small>$text</small></td></tr>
1925 EOF
1926     }
1927 }
1928
1929 sub h1
1930 {
1931     my($str) = @_;
1932     return \<<EOF;
1933 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1934 <tr>
1935 <td bgcolor="$Conf{CgiHeaderBgColor}">&nbsp;<font face="$Conf{CgiHeaderFontType}"
1936     size="$Conf{CgiHeaderFontSize}"><b>$str</b></font>
1937 </td></tr>
1938 </table>
1939 EOF
1940 }
1941
1942 sub h2
1943 {
1944     my($str) = @_;
1945     return \<<EOF;
1946 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1947 <tr>
1948 <td bgcolor="$Conf{CgiHeaderBgColor}">&nbsp;<font face="$Conf{CgiHeaderFontType}"
1949     size="$Conf{CgiHeaderFontSize}"><b>$str</b></font>
1950 </td></tr>
1951 </table>
1952 EOF
1953 }