6e9d9f58f8680ceef4c27ccc9c599a057f612245
[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.0beta1, released 30 Mar 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) );
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___}}"));
104 }
105
106 if ( !defined($Hosts) || $bpc->HostsMTime() != $HostsMTime ) {
107     $HostsMTime = $bpc->HostsMTime();
108     $Hosts = $bpc->HostInfoRead();
109
110     # turn moreUsers list into a hash for quick lookups
111     foreach my $host (keys %$Hosts) {
112        $Hosts->{$host}{moreUsers} =
113            {map {$_, 1} split(",", $Hosts->{$host}{moreUsers}) }
114     }
115 }
116
117 my %ActionDispatch = (
118     "summary"                    => \&Action_Summary,
119     $Lang->{Start_Incr_Backup}   => \&Action_StartStopBackup,
120     $Lang->{Start_Full_Backup}   => \&Action_StartStopBackup,
121     $Lang->{Stop_Dequeue_Backup} => \&Action_StartStopBackup,
122     "queue"                      => \&Action_Queue,
123     "view"                       => \&Action_View,
124     "LOGlist"                    => \&Action_LOGlist,
125     "emailSummary"               => \&Action_EmailSummary,
126     "browse"                     => \&Action_Browse,
127     $Lang->{Restore}             => \&Action_Restore,
128     "RestoreFile"                => \&Action_RestoreFile,
129     "hostInfo"                   => \&Action_HostInfo,
130     "generalInfo"                => \&Action_GeneralInfo,
131     "restoreInfo"                => \&Action_RestoreInfo,
132 );
133
134 #
135 # Set default actions, then call sub handler
136 #
137 $In{action} ||= "hostInfo"    if ( defined($In{host}) );
138 $In{action}   = "generalInfo" if ( !defined($ActionDispatch{$In{action}}) );
139 $ActionDispatch{$In{action}}();
140 exit(0);
141
142 ###########################################################################
143 # Action handling subroutines
144 ###########################################################################
145
146 sub Action_Summary
147 {
148     my($fullTot, $fullSizeTot, $incrTot, $incrSizeTot, $str,
149        $strNone, $strGood, $hostCntGood, $hostCntNone);
150
151     $hostCntGood = $hostCntNone = 0;
152     GetStatusInfo("hosts");
153     my $Privileged = CheckPermission();
154
155     if ( !$Privileged ) {
156         ErrorExit($Lang->{Only_privileged_users_can_view_PC_summaries} );
157     }
158     foreach my $host ( sort(keys(%Status)) ) {
159         my($fullDur, $incrCnt, $incrAge, $fullSize, $fullRate, $reasonHilite);
160         my @Backups = $bpc->BackupInfoRead($host);
161         my $fullCnt = $incrCnt = 0;
162         my $fullAge = $incrAge = -1;
163         for ( my $i = 0 ; $i < @Backups ; $i++ ) {
164             if ( $Backups[$i]{type} eq "full" ) {
165                 $fullCnt++;
166                 if ( $fullAge < 0 || $Backups[$i]{startTime} > $fullAge ) {
167                     $fullAge  = $Backups[$i]{startTime};
168                     $fullSize = $Backups[$i]{size} / (1024 * 1024);
169                     $fullDur  = $Backups[$i]{endTime} - $Backups[$i]{startTime};
170                 }
171                 $fullSizeTot += $Backups[$i]{size} / (1024 * 1024);
172             } else {
173                 $incrCnt++;
174                 if ( $incrAge < 0 || $Backups[$i]{startTime} > $incrAge ) {
175                     $incrAge = $Backups[$i]{startTime};
176                 }
177                 $incrSizeTot += $Backups[$i]{size} / (1024 * 1024);
178             }
179         }
180         if ( $fullAge < 0 ) {
181             $fullAge = "";
182             $fullRate = "";
183         } else {
184             $fullAge = sprintf("%.1f", (time - $fullAge) / (24 * 3600));
185             $fullRate = sprintf("%.2f",
186                                 $fullSize / ($fullDur <= 0 ? 1 : $fullDur));
187         }
188         if ( $incrAge < 0 ) {
189             $incrAge = "";
190         } else {
191             $incrAge = sprintf("%.1f", (time - $incrAge) / (24 * 3600));
192         }
193         $fullTot += $fullCnt;
194         $incrTot += $incrCnt;
195         $fullSize = sprintf("%.2f", $fullSize / 1000);
196         $incrAge = "&nbsp;" if ( $incrAge eq "" );
197         $reasonHilite = $Conf{CgiStatusHilightColor}{$Status{$host}{reason}};
198         $reasonHilite = " bgcolor=\"$reasonHilite\"" if ( $reasonHilite ne "" );
199
200         $str = <<EOF;
201 <tr$reasonHilite><td> ${HostLink($host)} </td>
202     <td align="center"> ${UserLink($Hosts->{$host}{user})} </td>
203     <td align="center"> $fullCnt </td>
204     <td align="center"> $fullAge </td>
205     <td align="center"> $fullSize </td>
206     <td align="center"> $fullRate </td>
207     <td align="center"> $incrCnt </td>
208     <td align="center"> $incrAge </td>
209     <td align="center"> $Lang->{$Status{$host}{state}} </td>
210     <td> $Lang->{$Status{$host}{reason}} </td></tr>
211 EOF
212         if ( @Backups == 0 ) {
213             $hostCntNone++;
214             $strNone .= $str;
215         } else {
216             $hostCntGood++;
217             $strGood .= $str;
218         }
219     }
220     $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
221     $incrSizeTot = sprintf("%.2f", $incrSizeTot / 1000);
222     my $now      = timeStamp2(time);
223
224     Header($Lang->{BackupPC__Server_Summary});
225     print eval ("qq{$Lang->{BackupPC_Summary}}");
226
227     Trailer();
228 }
229
230 sub Action_StartStopBackup
231 {
232     my($str, $reply);
233
234     my $start = 1 if ( $In{action} eq $Lang->{Start_Incr_Backup}
235                        || $In{action} eq $Lang->{Start_Full_Backup} );
236     my $doFull = $In{action} eq $Lang->{Start_Full_Backup} ? 1 : 0;
237     my $type = $doFull ? "full" : "incremental";
238     my $host = $In{host};
239     my $Privileged = CheckPermission($host);
240
241     if ( !$Privileged ) {
242         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_stop_or_start_backups}}"));
243     }
244     ServerConnect();
245
246     if ( $In{doit} ) {
247         if ( $start ) {
248             if ( $Hosts->{$host}{dhcp} ) {
249                 $reply = $bpc->ServerMesg("backup $In{hostIP} ${EscURI($host)}"
250                                     . " $User $doFull");
251                 $str = eval("qq{$Lang->{Backup_requested_on_DHCP__host}}");
252             } else {
253                 $reply = $bpc->ServerMesg("backup ${EscURI($host)}"
254                                     . " ${EscURI($host)} $User $doFull");
255                 $str = eval("qq{$Lang->{Backup_requested_on__host_by__User}}");
256             }
257         } else {
258             $reply = $bpc->ServerMesg("stop ${EscURI($host)} $User $In{backoff}");
259             $str = eval("qq{$Lang->{Backup_stopped_dequeued_on__host_by__User}}");
260         }
261
262         Header(eval ("qq{$Lang->{BackupPC__Backup_Requested_on__host}}") );
263         print (eval ("qq{$Lang->{REPLY_FROM_SERVER}}"));
264
265         Trailer();
266     } else {
267         if ( $start ) {
268             my $ipAddr = ConfirmIPAddress($host);
269
270             Header(eval("qq{$Lang->{BackupPC__Start_Backup_Confirm_on__host}}"));
271             print (eval("qq{$Lang->{Are_you_sure_start}}"));
272         } else {
273             my $backoff = "";
274             GetStatusInfo("host(${EscURI($host)})");
275             if ( $StatusHost{backoffTime} > time ) {
276                 $backoff = sprintf("%.1f",
277                                   ($StatusHost{backoffTime} - time) / 3600);
278             }
279             Header($Lang->{BackupPC__Stop_Backup_Confirm_on__host});
280             print (eval ("qq{$Lang->{Are_you_sure_stop}}"));
281         }
282         Trailer();
283     }
284 }
285
286 sub Action_Queue
287 {
288     my($strBg, $strUser, $strCmd);
289
290     GetStatusInfo("queues");
291     my $Privileged = CheckPermission();
292
293     if ( !$Privileged ) {
294         ErrorExit($Lang->{Only_privileged_users_can_view_queues_});
295     }
296
297     while ( @BgQueue ) {
298         my $req = pop(@BgQueue);
299         my($reqTime) = timeStamp2($req->{reqTime});
300         $strBg .= <<EOF;
301 <tr><td> ${HostLink($req->{host})} </td>
302     <td align="center"> $reqTime </td>
303     <td align="center"> $req->{user} </td></tr>
304 EOF
305     }
306     while ( @UserQueue ) {
307         my $req = pop(@UserQueue);
308         my $reqTime = timeStamp2($req->{reqTime});
309         $strUser .= <<EOF;
310 <tr><td> ${HostLink($req->{host})} </td>
311     <td align="center"> $reqTime </td>
312     <td align="center"> $req->{user} </td></tr>
313 EOF
314     }
315     while ( @CmdQueue ) {
316         my $req = pop(@CmdQueue);
317         my $reqTime = timeStamp2($req->{reqTime});
318         (my $cmd = $req->{cmd}[0]) =~ s/$BinDir\///;
319         $strCmd .= <<EOF;
320 <tr><td> ${HostLink($req->{host})} </td>
321     <td align="center"> $reqTime </td>
322     <td align="center"> $req->{user} </td>
323     <td> $cmd $req->{cmd}[0] </td></tr>
324 EOF
325     }
326     Header($Lang->{BackupPC__Queue_Summary});
327
328     print ( eval ( "qq{$Lang->{Backup_Queue_Summary}}") );
329
330     Trailer();
331 }
332
333 sub Action_View
334 {
335     my $Privileged = CheckPermission($In{host});
336     my $compress = 0;
337     my $fh;
338     my $host = $In{host};
339     my $num  = $In{num};
340     my $type = $In{type};
341     my $linkHosts = 0;
342     my($file, $comment);
343     my $ext = $num ne "" ? ".$num" : "";
344
345     ErrorExit(eval("qq{$Lang->{Invalid_number__num}}")) if ( $num ne "" && $num !~ /^\d+$/ );
346     if ( $type eq "XferLOG" ) {
347         $file = "$TopDir/pc/$host/SmbLOG$ext";
348         $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
349     } elsif ( $type eq "XferLOGbad" ) {
350         $file = "$TopDir/pc/$host/SmbLOG.bad";
351         $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
352     } elsif ( $type eq "XferErrbad" ) {
353         $file = "$TopDir/pc/$host/SmbLOG.bad";
354         $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
355         $comment = $Lang->{Extracting_only_Errors};
356     } elsif ( $type eq "XferErr" ) {
357         $file = "$TopDir/pc/$host/SmbLOG$ext";
358         $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
359         $comment = $Lang->{Extracting_only_Errors};
360     } elsif ( $type eq "RestoreLOG" ) {
361         $file = "$TopDir/pc/$host/RestoreLOG$ext";
362     } elsif ( $type eq "RestoreErr" ) {
363         $file = "$TopDir/pc/$host/RestoreLOG$ext";
364         $comment = $Lang->{Extracting_only_Errors};
365     } elsif ( $host ne "" && $type eq "config" ) {
366         $file = "$TopDir/pc/$host/config.pl";
367         $file = "$TopDir/conf/$host.pl"
368                     if ( $host ne "config" && -f "$TopDir/conf/$host.pl"
369                                            && !-f $file );
370     } elsif ( $type eq "docs" ) {
371         $file = "$BinDir/../doc/BackupPC.html";
372         if ( open(LOG, $file) ) {
373             Header($Lang->{BackupPC__Documentation});
374             print while ( <LOG> );
375             close(LOG);
376             Trailer();
377         } else {
378             ErrorExit(eval("qq{$Lang->{Unable_to_open__file__configuration_problem}}"));
379         }
380         return;
381     } elsif ( $type eq "config" ) {
382         $file = "$TopDir/conf/config.pl";
383     } elsif ( $type eq "hosts" ) {
384         $file = "$TopDir/conf/hosts";
385     } elsif ( $host ne "" ) {
386         $file = "$TopDir/pc/$host/LOG$ext";
387     } else {
388         $file = "$TopDir/log/LOG$ext";
389         $linkHosts = 1;
390     }
391     if ( !$Privileged ) {
392         ErrorExit($Lang->{Only_privileged_users_can_view_log_or_config_files});
393     }
394     if ( !-f $file && -f "$file.z" ) {
395         $file .= ".z";
396         $compress = 1;
397     }
398     Header(eval("qq{$Lang->{Backup_PC__Log_File__file}}")  );
399     print( eval ("qq{$Lang->{Log_File__file__comment}}"));
400     if ( defined($fh = BackupPC::FileZIO->open($file, 0, $compress)) ) {
401         my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
402
403         print ( eval ("qq{$Lang->{Contents_of_log_file}}"));
404
405         print "<pre>";
406         if ( $type eq "XferErr" || $type eq "XferErrbad"
407                                 || $type eq "RestoreErr" ) {
408             my $skipped;
409             while ( 1 ) {
410                 $_ = $fh->readLine();
411                 if ( $_ eq "" ) {
412                     print(eval ("qq{$Lang->{skipped__skipped_lines}}"))
413                                                     if ( $skipped );
414                     last;
415                 }
416                 if ( /smb: \\>/
417                         || /^\s*(\d+) \(\s*\d+\.\d kb\/s\) (.*)$/
418                         || /^tar: dumped \d+ files/
419                         || /^added interface/i
420                         || /^restore tar file /i
421                         || /^restore directory /i
422                         || /^tarmode is now/i
423                         || /^Total bytes written/i
424                         || /^Domain=/i
425                         || /^Getting files newer than/i
426                         || /^Output is \/dev\/null/
427                         || /^\([\d\.]* kb\/s\) \(average [\d\.]* kb\/s\)$/
428                         || /^\s+directory \\/
429                         || /^Timezone is/
430                         || /^\.\//
431                         || /^  /
432                             ) {
433                     $skipped++;
434                     next;
435                 }
436                 print(eval("qq{$Lang->{skipped__skipped_lines}}"))
437                                                      if ( $skipped );
438                 $skipped = 0;
439                 print ${EscHTML($_)};
440             }
441         } elsif ( $linkHosts ) {
442             while ( 1 ) {
443                 $_ = $fh->readLine();
444                 last if ( $_ eq "" );
445                 my $s = ${EscHTML($_)};
446                 $s =~ s/\b([\w-]+)\b/defined($Hosts->{$1})
447                                         ? ${HostLink($1)} : $1/eg;
448                 print $s;
449             }
450         } elsif ( $type eq "config" ) {
451             while ( 1 ) {
452                 $_ = $fh->readLine();
453                 last if ( $_ eq "" );
454                 # remove any passwords and user names
455                 s/(SmbSharePasswd.*=.*['"]).*(['"])/$1$2/ig;
456                 s/(SmbShareUserName.*=.*['"]).*(['"])/$1$2/ig;
457                 s/(ServerMesgSecret.*=.*['"]).*(['"])/$1$2/ig;
458                 print ${EscHTML($_)};
459             }
460         } else {
461             while ( 1 ) {
462                 $_ = $fh->readLine();
463                 last if ( $_ eq "" );
464                 print ${EscHTML($_)};
465             }
466         }
467         $fh->close();
468     } else {
469         printf( eval("qq{$Lang->{_pre___Can_t_open_log_file__file}}"));
470     }
471     print <<EOF;
472 </pre>
473 EOF
474     Trailer();
475 }
476
477 sub Action_LOGlist
478 {
479     my $Privileged = CheckPermission($In{host});
480
481     if ( !$Privileged ) {
482         ErrorExit($Lang->{Only_privileged_users_can_view_log_files});
483     }
484     my $host = $In{host};
485     my($url0, $hdr, $root, $str);
486     if ( $host ne "" ) {
487         $root = "$TopDir/pc/$host/LOG";
488         $url0 = "&host=${EscURI($host)}";
489         $hdr = "for host $host";
490     } else {
491         $root = "$TopDir/log/LOG";
492         $url0 = "";
493         $hdr = "";
494     }
495     for ( my $i = -1 ; ; $i++ ) {
496         my $url1 = "";
497         my $file = $root;
498         if ( $i >= 0 ) {
499             $file .= ".$i";
500             $url1 = "&num=$i";
501         }
502         $file .= ".z" if ( !-f $file && -f "$file.z" );
503         last if ( !-f $file );
504         my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
505         my $size     = (stat($file))[7];
506         $str .= <<EOF;
507 <tr><td> <a href="$MyURL?action=view&type=LOG$url0$url1"><tt>$file</tt></a> </td>
508     <td align="right"> $size </td>
509     <td> $mtimeStr </td></tr>
510 EOF
511     }
512     Header($Lang->{BackupPC__Log_File_History});
513     print (eval("qq{$Lang->{Log_File_History__hdr}}"));
514     Trailer();
515 }
516
517 sub Action_EmailSummary
518 {
519     my $Privileged = CheckPermission();
520
521     if ( !$Privileged ) {
522         ErrorExit($Lang->{Only_privileged_users_can_view_email_summaries});
523     }
524     GetStatusInfo("hosts");
525     ReadUserEmailInfo();
526     my(%EmailStr, $str);
527     foreach my $u ( keys(%UserEmailInfo) ) {
528         next if ( !defined($UserEmailInfo{$u}{lastTime}) );
529         my $emailTimeStr = timeStamp2($UserEmailInfo{$u}{lastTime});
530         $EmailStr{$UserEmailInfo{$u}{lastTime}} .= <<EOF;
531 <tr><td>${UserLink($u)} </td>
532     <td>${HostLink($UserEmailInfo{$u}{lastHost})} </td>
533     <td>$emailTimeStr </td>
534     <td>$UserEmailInfo{$u}{lastSubj} </td></tr>
535 EOF
536     }
537     foreach my $t ( sort({$b <=> $a} keys(%EmailStr)) ) {
538         $str .= $EmailStr{$t};
539     }
540     Header($Lang->{Email_Summary});
541     print (eval("qq{$Lang->{Recent_Email_Summary}}"));
542     Trailer();
543 }
544
545 sub Action_Browse
546 {
547     my $Privileged = CheckPermission($In{host});
548     my($i, $dirStr, $fileStr, $attr);
549     my $checkBoxCnt = 0;
550
551     if ( !$Privileged ) {
552         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_browse_backup_files}}"));
553     }
554     my $host  = $In{host};
555     my $num   = $In{num};
556     my $share = $In{share};
557     my $dir   = $In{dir};
558
559     ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
560     #
561     # Find the requested backup and the previous filled backup
562     #
563     my @Backups = $bpc->BackupInfoRead($host);
564     for ( $i = 0 ; $i < @Backups ; $i++ ) {
565         last if ( $Backups[$i]{num} == $num );
566     }
567     if ( $i >= @Backups ) {
568         ErrorExit("Backup number $num for host ${EscHTML($host)} does"
569                 . " not exist.");
570     }
571     my $backupTime = timeStamp2($Backups[$i]{startTime});
572     my $backupAge = sprintf("%.1f", (time - $Backups[$i]{startTime})
573                                     / (24 * 3600));
574     my $view = BackupPC::View->new($bpc, $host, \@Backups);
575
576     if ( $dir eq "" || $dir eq "." || $dir eq ".." ) {
577         $attr = $view->dirAttrib($num, "", "");
578         if ( keys(%$attr) > 0 ) {
579             $share = (sort(keys(%$attr)))[0];
580             $dir   = '/';
581         } else {
582             ErrorExit(eval("qq{$Lang->{Directory___EscHTML}}"));
583         }
584     }
585     $dir = "/$dir" if ( $dir !~ /^\// );
586     my $relDir  = $dir;
587     my $currDir = undef;
588
589     #
590     # Loop up the directory tree until we hit the top.
591     #
592     my(@DirStrPrev);
593     while ( 1 ) {
594         my($fLast, $fLastum, @DirStr);
595
596         $attr = $view->dirAttrib($num, $share, $relDir);
597         if ( !defined($attr) ) {
598             ErrorExit(eval("qq{$Lang->{Can_t_browse_bad_directory_name2}}"));
599         }
600
601         my $fileCnt = 0;          # file counter
602         $fLast = $dirStr = "";
603
604         #
605         # Loop over each of the files in this directory
606         #
607         foreach my $f ( sort(keys(%$attr)) ) {
608             my($dirOpen, $gotDir, $imgStr, $img, $path);
609             my $fURI = $f;                             # URI escaped $f
610             my $shareURI = $share;                     # URI escaped $share
611             if ( $relDir eq "" ) {
612                 $path = "/$f";
613             } else {
614                 ($path = "$relDir/$f") =~ s{//+}{/}g;
615             }
616             if ( $shareURI eq "" ) {
617                 $shareURI = $f;
618                 $path  = "/";
619             }
620             $path =~ s{^/+}{/};
621             $path     =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
622             $fURI     =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
623             $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
624             $dirOpen  = 1 if ( defined($currDir) && $f eq $currDir );
625             if ( $attr->{$f}{type} == BPC_FTYPE_DIR ) {
626                 #
627                 # Display directory if it exists in current backup.
628                 # First find out if there are subdirs
629                 #
630                 my($bold, $unbold, $BGcolor);
631                 $img |= 1 << 6;
632                 $img |= 1 << 5 if ( $attr->{$f}{nlink} > 2 );
633                 if ( $dirOpen ) {
634                     $bold = "<b>";
635                     $unbold = "</b>";
636                     $img |= 1 << 2;
637                     $img |= 1 << 3 if ( $attr->{$f}{nlink} > 2 );
638                 }
639                 my $imgFileName = sprintf("%07b.gif", $img);
640                 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
641                 if ( "$relDir/$f" eq $dir ) {
642                     $BGcolor = " bgcolor=\"$Conf{CgiHeaderBgColor}\"";
643                 } else {
644                     $BGcolor = "";
645                 }
646                 my $dirName = $f;
647                 $dirName =~ s/ /&nbsp;/g;
648                 push(@DirStr, {needTick => 1,
649                                tdArgs   => $BGcolor,
650                                link     => <<EOF});
651 <a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$imgStr</a><a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path" style="font-size:13px;font-family:arial;text-decoration:none;line-height:15px">&nbsp;$bold$dirName$unbold</a></td></tr>
652 EOF
653                 $fileCnt++;
654                 $gotDir = 1;
655                 if ( $dirOpen ) {
656                     my($lastTick, $doneLastTick);
657                     foreach my $d ( @DirStrPrev ) {
658                         $lastTick = $d if ( $d->{needTick} );
659                     }
660                     $doneLastTick = 1 if ( !defined($lastTick) );
661                     foreach my $d ( @DirStrPrev ) {
662                         $img = 0;
663                         if  ( $d->{needTick} ) {
664                             $img |= 1 << 0;
665                         }
666                         if ( $d == $lastTick ) {
667                             $img |= 1 << 4;
668                             $doneLastTick = 1;
669                         } elsif ( !$doneLastTick ) {
670                             $img |= 1 << 3 | 1 << 4;
671                         }
672                         my $imgFileName = sprintf("%07b.gif", $img);
673                         $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
674                         push(@DirStr, {needTick => 0,
675                                        tdArgs   => $d->{tdArgs},
676                                        link     => $imgStr . $d->{link}
677                         });
678                     }
679                 }
680             }
681             if ( $relDir eq $dir ) {
682                 #
683                 # This is the selected directory, so display all the files
684                 #
685                 my $attrStr;
686                 if ( defined($a = $attr->{$f}) ) {
687                     my $mtimeStr = $bpc->timeStamp($a->{mtime});
688                     # UGH -> fix this
689                     my $typeStr  = BackupPC::Attrib::fileType2Text(undef,
690                                                                    $a->{type});
691                     my $modeStr  = sprintf("0%o", $a->{mode} & 07777);
692                     $attrStr .= <<EOF;
693     <td align="center">$typeStr</td>
694     <td align="center">$modeStr</td>
695     <td align="center">$a->{backupNum}</td>
696     <td align="right">$a->{size}</td>
697     <td align="right">$mtimeStr</td>
698 </tr>
699 EOF
700                 } else {
701                     $attrStr .= "<td colspan=\"5\" align=\"center\"> </td>\n";
702                 }
703                 (my $fDisp = "${EscHTML($f)}") =~ s/ /&nbsp;/g;
704                 if ( $gotDir ) {
705                     $fileStr .= <<EOF;
706 <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>
707 $attrStr
708 </tr>
709 EOF
710                 } else {
711                     $fileStr .= <<EOF;
712 <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>
713 $attrStr
714 </tr>
715 EOF
716                 }
717                 $checkBoxCnt++;
718             }
719         }
720         @DirStrPrev = @DirStr;
721         last if ( $relDir eq "" && $share eq "" );
722         # 
723         # Prune the last directory off $relDir, or at the very end
724         # do the top-level directory.
725         #
726         if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) {
727             $currDir = $share;
728             $share = "";
729             $relDir = "";
730         } else {
731             $relDir  = $1;
732             $currDir = $2;
733         }
734     }
735     $share = $currDir;
736     my $dirDisplay = "$share/$dir";
737     $dirDisplay =~ s{//+}{/}g;
738     $dirDisplay =~ s{/+$}{}g;
739     $dirDisplay = "/" if ( $dirDisplay eq "" );
740     my $filledBackup;
741
742     if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) {
743         shift(@mergeNums);
744         my $numF = join(", #", @mergeNums);
745         $filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}");
746     }
747     Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"));
748
749     foreach my $d ( @DirStrPrev ) {
750         $dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n";
751     }
752
753     ### hide checkall button if there are no files
754     my ($topCheckAll, $checkAll, $fileHeader);
755     if ( $fileStr ) {
756         $fileHeader = eval("qq{$Lang->{fileHeader}}");
757
758         $checkAll = $Lang->{checkAll};
759
760         # and put a checkall box on top if there are at least 20 files
761         if ( $checkBoxCnt >= 20 ) {
762             $topCheckAll = $checkAll;
763             $topCheckAll =~ s{allFiles}{allFilestop}g;
764         }
765     } else {
766         $fileStr = eval("qq{$Lang->{The_directory_is_empty}}");
767     }
768     my @otherDirs;
769     foreach my $i ( $view->backupList($share, $dir) ) {
770         my $path = $dir;
771         my $shareURI = $share;
772         $path =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
773         $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
774         push(@otherDirs, "<a href=\"$MyURL?action=browse&host=${EscURI($host)}&num=$i"
775                        . "&share=$shareURI&dir=$path\">$i</a>");
776
777     }
778     if ( @otherDirs ) {
779         my $otherDirs  = join(",\n", @otherDirs);
780         $filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}");
781     }
782     print (eval("qq{$Lang->{Backup_browse_for__host}}"));
783     Trailer();
784 }
785
786 sub Action_Restore
787 {
788     my($str, $reply);
789     my $Privileged = CheckPermission($In{host});
790     if ( !$Privileged ) {
791         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files}}"));
792     }
793     my $host  = $In{host};
794     my $num   = $In{num};
795     my $share = $In{share};
796     my(@fileList, $fileListStr, $hiddenStr, $pathHdr, $badFileCnt);
797     my @Backups = $bpc->BackupInfoRead($host);
798
799     ServerConnect();
800     if ( !defined($Hosts->{$host}) ) {
801         ErrorExit(eval("qq{$Lang->{Bad_host_name}}"));
802     }
803     for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
804         next if ( !defined($In{"fcb$i"}) );
805         (my $name = $In{"fcb$i"}) =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
806         $badFileCnt++ if ( $name =~ m{(^|/)\.\.(/|$)} );
807         if ( @fileList == 0 ) {
808             $pathHdr = $name;
809         } else {
810             while ( substr($name, 0, length($pathHdr)) ne $pathHdr ) {
811                 $pathHdr = substr($pathHdr, 0, rindex($pathHdr, "/"));
812             }
813         }
814         push(@fileList, $name);
815         $hiddenStr .= <<EOF;
816 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
817 EOF
818         $fileListStr .= <<EOF;
819 <li> ${EscHTML($name)}
820 EOF
821     }
822     $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
823     $hiddenStr .= "<input type=\"hidden\" name=\"share\" value=\"${EscHTML($share)}\">\n";
824     $badFileCnt++ if ( $In{pathHdr} =~ m{(^|/)\.\.(/|$)} );
825     $badFileCnt++ if ( $In{num} =~ m{(^|/)\.\.(/|$)} );
826     if ( @fileList == 0 ) {
827         ErrorExit($Lang->{You_haven_t_selected_any_files__please_go_Back_to});
828     }
829     if ( $badFileCnt ) {
830         ErrorExit($Lang->{Nice_try__but_you_can_t_put});
831     }
832     if ( @fileList == 1 ) {
833         $pathHdr =~ s/(.*)\/.*/$1/;
834     }
835     $pathHdr = "/" if ( $pathHdr eq "" );
836     if ( $In{type} != 0 && @fileList == $In{fcbMax} ) {
837         #
838         # All the files in the list were selected, so just restore the
839         # entire parent directory
840         #
841         @fileList = ( $pathHdr );
842     }
843     if ( $In{type} == 0 ) {
844         #
845         # Tell the user what options they have
846         #
847         Header(eval("qq{$Lang->{Restore_Options_for__host}}"));
848         print(eval("qq{$Lang->{Restore_Options_for__host2}}"));
849
850         #
851         # Verify that Archive::Zip is available before showing the
852         # zip restore option
853         #
854         if ( eval { require Archive::Zip } ) {
855             print (eval("qq{$Lang->{Option_2__Download_Zip_archive}}"));
856         } else {
857             print (eval("qq{$Lang->{Option_2__Download_Zip_archive2}}"));
858         }
859         print (eval("qq{$Lang->{Option_3__Download_Zip_archive}}"));
860         Trailer();
861     } elsif ( $In{type} == 1 ) {
862         #
863         # Provide the selected files via a tar archive.
864         #
865         my @fileListTrim = @fileList;
866         if ( @fileListTrim > 10 ) {
867             @fileListTrim = (@fileListTrim[0..9], '...');
868         }
869         $bpc->ServerMesg(eval("qq{$Lang->{log_User__User_downloaded_tar_archive_for__host}}"));
870
871         my @pathOpts;
872         if ( $In{relative} ) {
873             @pathOpts = ("-r", $pathHdr, "-p", "");
874         }
875         print(STDOUT <<EOF);
876 Content-Type: application/x-gtar
877 Content-Transfer-Encoding: binary
878 Content-Disposition: attachment; filename=\"restore.tar\"
879
880 EOF
881         #
882         # Fork the child off and manually copy the output to our stdout.
883         # This is necessary to ensure the output gets to the correct place
884         # under mod_perl.
885         #
886         $bpc->cmdSystemOrEval(["$BinDir/BackupPC_tarCreate",
887                  "-h", $host,
888                  "-n", $num,
889                  "-s", $share,
890                  @pathOpts,
891                  @fileList
892             ],
893             sub { print(@_); }
894         );
895     } elsif ( $In{type} == 2 ) {
896         #
897         # Provide the selected files via a zip archive.
898         #
899         my @fileListTrim = @fileList;
900         if ( @fileListTrim > 10 ) {
901             @fileListTrim = (@fileListTrim[0..9], '...');
902         }
903         $bpc->ServerMesg(eval("qq{$Lang->{log_User__User_downloaded_zip_archive_for__host}}"));
904
905         my @pathOpts;
906         if ( $In{relative} ) {
907             @pathOpts = ("-r", $pathHdr, "-p", "");
908         }
909         print(STDOUT <<EOF);
910 Content-Type: application/zip
911 Content-Transfer-Encoding: binary
912 Content-Disposition: attachment; filename=\"restore.zip\"
913
914 EOF
915         $In{compressLevel} = 5 if ( $In{compressLevel} !~ /^\d+$/ );
916         #
917         # Fork the child off and manually copy the output to our stdout.
918         # This is necessary to ensure the output gets to the correct place
919         # under mod_perl.
920         #
921         $bpc->cmdSystemOrEval(["$BinDir/BackupPC_zipCreate",
922                  "-h", $host,
923                  "-n", $num,
924                  "-c", $In{compressLevel},
925                  "-s", $share,
926                  @pathOpts,
927                  @fileList
928             ],
929             sub { print(@_); }
930         );
931     } elsif ( $In{type} == 3 ) {
932         #
933         # Do restore directly onto host
934         #
935         if ( !defined($Hosts->{$In{hostDest}}) ) {
936             ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
937         }
938         if ( !CheckPermission($In{hostDest}) ) {
939             ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
940         }
941         $fileListStr = "";
942         foreach my $f ( @fileList ) {
943             my $targetFile = $f;
944             (my $strippedShare = $share) =~ s/^\///;
945             (my $strippedShareDest = $In{shareDest}) =~ s/^\///;
946             substr($targetFile, 0, length($pathHdr)) = $In{pathHdr};
947             $fileListStr .= <<EOF;
948 <tr><td>$host:/$strippedShare$f</td><td>$In{hostDest}:/$strippedShareDest$targetFile</td></tr>
949 EOF
950         }
951         Header(eval("qq{$Lang->{Restore_Confirm_on__host}}"));
952         print(eval("qq{$Lang->{Are_you_sure}}"));
953         Trailer();
954     } elsif ( $In{type} == 4 ) {
955         if ( !defined($Hosts->{$In{hostDest}}) ) {
956             ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
957         }
958         if ( !CheckPermission($In{hostDest}) ) {
959             ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
960         }
961         my $hostDest = $1 if ( $In{hostDest} =~ /(.+)/ );
962         my $ipAddr = ConfirmIPAddress($hostDest);
963         #
964         # Prepare and send the restore request.  We write the request
965         # information using Data::Dumper to a unique file,
966         # $TopDir/pc/$hostDest/restoreReq.$$.n.  We use a file
967         # in case the list of files to restore is very long.
968         #
969         my $reqFileName;
970         for ( my $i = 0 ; ; $i++ ) {
971             $reqFileName = "restoreReq.$$.$i";
972             last if ( !-f "$TopDir/pc/$hostDest/$reqFileName" );
973         }
974         my %restoreReq = (
975             # source of restore is hostSrc, #num, path shareSrc/pathHdrSrc
976             num         => $In{num},
977             hostSrc     => $host,
978             shareSrc    => $share,
979             pathHdrSrc  => $pathHdr,
980
981             # destination of restore is hostDest:shareDest/pathHdrDest
982             hostDest    => $hostDest,
983             shareDest   => $In{shareDest},
984             pathHdrDest => $In{pathHdr},
985
986             # list of files to restore
987             fileList    => \@fileList,
988
989             # other info
990             user        => $User,
991             reqTime     => time,
992         );
993         my($dump) = Data::Dumper->new(
994                          [  \%restoreReq],
995                          [qw(*RestoreReq)]);
996         $dump->Indent(1);
997         if ( open(REQ, ">$TopDir/pc/$hostDest/$reqFileName") ) {
998             print(REQ $dump->Dump);
999             close(REQ);
1000         } else {
1001             ErrorExit(eval("qq{$Lang->{Can_t_open_create}}"));
1002         }
1003         $reply = $bpc->ServerMesg("restore ${EscURI($ipAddr)}"
1004                         . " ${EscURI($hostDest)} $User $reqFileName");
1005         $str = eval("qq{$Lang->{Restore_requested_to_host__hostDest__backup___num}}");
1006         Header(eval("qq{$Lang->{Restore_Requested_on__hostDest}}"));
1007         print (eval("qq{$Lang->{Reply_from_server_was___reply}}"));
1008         Trailer();
1009     }
1010 }
1011
1012 sub Action_RestoreFile
1013 {
1014     restoreFile($In{host}, $In{num}, $In{share}, $In{dir});
1015 }
1016
1017 sub restoreFile
1018 {
1019     my($host, $num, $share, $dir, $skipHardLink, $origName) = @_;
1020     my($Privileged) = CheckPermission($host);
1021
1022     #
1023     # Some common content (media) types from www.iana.org (via MIME::Types).
1024     #
1025     my $Ext2ContentType = {
1026         'asc'  => 'text/plain',
1027         'avi'  => 'video/x-msvideo',
1028         'bmp'  => 'image/bmp',
1029         'book' => 'application/x-maker',
1030         'cc'   => 'text/plain',
1031         'cpp'  => 'text/plain',
1032         'csh'  => 'application/x-csh',
1033         'csv'  => 'text/comma-separated-values',
1034         'c'    => 'text/plain',
1035         'deb'  => 'application/x-debian-package',
1036         'doc'  => 'application/msword',
1037         'dot'  => 'application/msword',
1038         'dtd'  => 'text/xml',
1039         'dvi'  => 'application/x-dvi',
1040         'eps'  => 'application/postscript',
1041         'fb'   => 'application/x-maker',
1042         'fbdoc'=> 'application/x-maker',
1043         'fm'   => 'application/x-maker',
1044         'frame'=> 'application/x-maker',
1045         'frm'  => 'application/x-maker',
1046         'gif'  => 'image/gif',
1047         'gtar' => 'application/x-gtar',
1048         'gz'   => 'application/x-gzip',
1049         'hh'   => 'text/plain',
1050         'hpp'  => 'text/plain',
1051         'h'    => 'text/plain',
1052         'html' => 'text/html',
1053         'htmlx'=> 'text/html',
1054         'htm'  => 'text/html',
1055         'iges' => 'model/iges',
1056         'igs'  => 'model/iges',
1057         'jpeg' => 'image/jpeg',
1058         'jpe'  => 'image/jpeg',
1059         'jpg'  => 'image/jpeg',
1060         'js'   => 'application/x-javascript',
1061         'latex'=> 'application/x-latex',
1062         'maker'=> 'application/x-maker',
1063         'mid'  => 'audio/midi',
1064         'midi' => 'audio/midi',
1065         'movie'=> 'video/x-sgi-movie',
1066         'mov'  => 'video/quicktime',
1067         'mp2'  => 'audio/mpeg',
1068         'mp3'  => 'audio/mpeg',
1069         'mpeg' => 'video/mpeg',
1070         'mpg'  => 'video/mpeg',
1071         'mpp'  => 'application/vnd.ms-project',
1072         'pdf'  => 'application/pdf',
1073         'pgp'  => 'application/pgp-signature',
1074         'php'  => 'application/x-httpd-php',
1075         'pht'  => 'application/x-httpd-php',
1076         'phtml'=> 'application/x-httpd-php',
1077         'png'  => 'image/png',
1078         'ppm'  => 'image/x-portable-pixmap',
1079         'ppt'  => 'application/powerpoint',
1080         'ppt'  => 'application/vnd.ms-powerpoint',
1081         'ps'   => 'application/postscript',
1082         'qt'   => 'video/quicktime',
1083         'rgb'  => 'image/x-rgb',
1084         'rtf'  => 'application/rtf',
1085         'rtf'  => 'text/rtf',
1086         'shar' => 'application/x-shar',
1087         'shtml'=> 'text/html',
1088         'swf'  => 'application/x-shockwave-flash',
1089         'tex'  => 'application/x-tex',
1090         'texi' => 'application/x-texinfo',
1091         'texinfo'=> 'application/x-texinfo',
1092         'tgz'  => 'application/x-gtar',
1093         'tiff' => 'image/tiff',
1094         'tif'  => 'image/tiff',
1095         'txt'  => 'text/plain',
1096         'vcf'  => 'text/x-vCard',
1097         'vrml' => 'model/vrml',
1098         'wav'  => 'audio/x-wav',
1099         'wmls' => 'text/vnd.wap.wmlscript',
1100         'wml'  => 'text/vnd.wap.wml',
1101         'wrl'  => 'model/vrml',
1102         'xls'  => 'application/vnd.ms-excel',
1103         'xml'  => 'text/xml',
1104         'xwd'  => 'image/x-xwindowdump',
1105         'z'    => 'application/x-compress',
1106         'zip'  => 'application/zip',
1107         %{$Conf{CgiExt2ContentType}},       # add site-specific values
1108     };
1109     if ( !$Privileged ) {
1110         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files2}}"));
1111     }
1112     ServerConnect();
1113     ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
1114
1115     $dir = "/" if ( $dir eq "" );
1116     my @Backups = $bpc->BackupInfoRead($host);
1117     my $view = BackupPC::View->new($bpc, $host, \@Backups);
1118     my $a = $view->fileAttrib($num, $share, $dir);
1119     if ( $dir =~ m{(^|/)\.\.(/|$)} || !defined($a) ) {
1120         ErrorExit("Can't restore bad file ${EscHTML($dir)}");
1121     }
1122     my $f = BackupPC::FileZIO->open($a->{fullPath}, 0, $a->{compress});
1123     my $data;
1124     if ( !$skipHardLink && $a->{type} == BPC_FTYPE_HARDLINK ) {
1125         #
1126         # hardlinks should look like the file they point to
1127         #
1128         my $linkName;
1129         while ( $f->read(\$data, 65536) > 0 ) {
1130             $linkName .= $data;
1131         }
1132         $f->close;
1133         $linkName =~ s/^\.\///;
1134         my $share = $1 if ( $dir =~ /^\/?(.*?)\// );
1135         restoreFile($host, $num, $share, $linkName, 1, $dir);
1136         return;
1137     }
1138     $bpc->ServerMesg("log User $User recovered file $host/$num:$share/$dir ($a->{fullPath})");
1139     $dir = $origName if ( defined($origName) );
1140     my $ext = $1 if ( $dir =~ /\.([^\/\.]+)$/ );
1141     my $contentType = $Ext2ContentType->{lc($ext)}
1142                                     || "application/octet-stream";
1143     my $fileName = $1 if ( $dir =~ /.*\/(.*)/ );
1144     $fileName =~ s/"/\\"/g;
1145     print "Content-Type: $contentType\n";
1146     print "Content-Transfer-Encoding: binary\n";
1147     print "Content-Disposition: attachment; filename=\"$fileName\"\n\n";
1148     while ( $f->read(\$data, 1024 * 1024) > 0 ) {
1149         print STDOUT $data;
1150     }
1151     $f->close;
1152 }
1153
1154 sub Action_HostInfo
1155 {
1156     my $host = $1 if ( $In{host} =~ /(.*)/ );
1157     my($statusStr, $startIncrStr);
1158
1159     $host =~ s/^\s+//;
1160     $host =~ s/\s+$//;
1161     return Action_GeneralInfo() if ( $host eq "" );
1162     $host = lc($host)
1163                 if ( !-d "$TopDir/pc/$host" && -d "$TopDir/pc/" . lc($host) );
1164     if ( $host =~ /\.\./ || !-d "$TopDir/pc/$host" ) {
1165         #
1166         # try to lookup by user name
1167         #
1168         if ( !defined($Hosts->{$host}) ) {
1169             foreach my $h ( keys(%$Hosts) ) {
1170                 if ( $Hosts->{$h}{user} eq $host
1171                         || lc($Hosts->{$h}{user}) eq lc($host) ) {
1172                     $host = $h;
1173                     last;
1174                 }
1175             }
1176             CheckPermission();
1177             ErrorExit(eval("qq{$Lang->{Unknown_host_or_user}}"))
1178                                 if ( !defined($Hosts->{$host}) );
1179         }
1180         $In{host} = $host;
1181     }
1182     GetStatusInfo("host(${EscURI($host)})");
1183     $bpc->ConfigRead($host);
1184     %Conf = $bpc->Conf();
1185     my $Privileged = CheckPermission($host);
1186     if ( !$Privileged ) {
1187         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_view_information_about}}"));
1188     }
1189     ReadUserEmailInfo();
1190
1191     my @Backups = $bpc->BackupInfoRead($host);
1192     my($str, $sizeStr, $compStr, $errStr, $warnStr);
1193     for ( my $i = 0 ; $i < @Backups ; $i++ ) {
1194         my $startTime = timeStamp2($Backups[$i]{startTime});
1195         my $dur       = $Backups[$i]{endTime} - $Backups[$i]{startTime};
1196         $dur          = 1 if ( $dur <= 0 );
1197         my $duration  = sprintf("%.1f", $dur / 60);
1198         my $MB        = sprintf("%.1f", $Backups[$i]{size} / (1024*1024));
1199         my $MBperSec  = sprintf("%.2f", $Backups[$i]{size} / (1024*1024*$dur));
1200         my $MBExist   = sprintf("%.1f", $Backups[$i]{sizeExist} / (1024*1024));
1201         my $MBNew     = sprintf("%.1f", $Backups[$i]{sizeNew} / (1024*1024));
1202         my($MBExistComp, $ExistComp, $MBNewComp, $NewComp);
1203         if ( $Backups[$i]{sizeExist} && $Backups[$i]{sizeExistComp} ) {
1204             $MBExistComp = sprintf("%.1f", $Backups[$i]{sizeExistComp}
1205                                                 / (1024 * 1024));
1206             $ExistComp = sprintf("%.1f%%", 100 *
1207                   (1 - $Backups[$i]{sizeExistComp} / $Backups[$i]{sizeExist}));
1208         }
1209         if ( $Backups[$i]{sizeNew} && $Backups[$i]{sizeNewComp} ) {
1210             $MBNewComp = sprintf("%.1f", $Backups[$i]{sizeNewComp}
1211                                                 / (1024 * 1024));
1212             $NewComp = sprintf("%.1f%%", 100 *
1213                   (1 - $Backups[$i]{sizeNewComp} / $Backups[$i]{sizeNew}));
1214         }
1215         my $age = sprintf("%.1f", (time - $Backups[$i]{startTime}) / (24*3600));
1216         my $browseURL = "$MyURL?action=browse&host=${EscURI($host)}&num=$Backups[$i]{num}";
1217         my $filled = $Backups[$i]{noFill} ? $Lang->{No} : $Lang->{Yes};
1218         $filled .= " ($Backups[$i]{fillFromNum}) "
1219                             if ( $Backups[$i]{fillFromNum} ne "" );
1220         my $ltype;
1221         if ($Backups[$i]{type} eq "full") { $ltype = $Lang->{full}; }
1222         else { $ltype = $Lang->{incremental}; }
1223         $str .= <<EOF;
1224 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1225     <td align="center"> $ltype </td>
1226     <td align="center"> $filled </td>
1227     <td align="right">  $startTime </td>
1228     <td align="right">  $duration </td>
1229     <td align="right">  $age </td>
1230     <td align="left">   <tt>$TopDir/pc/$host/$Backups[$i]{num}</tt> </td></tr>
1231 EOF
1232         $sizeStr .= <<EOF;
1233 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1234     <td align="center"> $ltype </td>
1235     <td align="right">  $Backups[$i]{nFiles} </td>
1236     <td align="right">  $MB </td>
1237     <td align="right">  $MBperSec </td>
1238     <td align="right">  $Backups[$i]{nFilesExist} </td>
1239     <td align="right">  $MBExist </td>
1240     <td align="right">  $Backups[$i]{nFilesNew} </td>
1241     <td align="right">  $MBNew </td>
1242 </tr>
1243 EOF
1244         my $is_compress = $Backups[$i]{compress} || $Lang->{off};
1245         if (! $ExistComp) { $ExistComp = "&nbsp;"; }
1246         if (! $MBExistComp) { $MBExistComp = "&nbsp;"; }
1247         $compStr .= <<EOF;
1248 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1249     <td align="center"> $ltype </td>
1250     <td align="center"> $is_compress </td> 
1251     <td align="right">  $MBExist </td>
1252     <td align="right">  $MBExistComp </td> 
1253     <td align="right">  $ExistComp </td>   
1254     <td align="right">  $MBNew </td>
1255     <td align="right">  $MBNewComp </td>
1256     <td align="right">  $NewComp </td>
1257 </tr>
1258 EOF
1259         $errStr .= <<EOF;
1260 <tr><td align="center"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
1261     <td align="center"> $ltype </td>
1262     <td align="center"> <a href="$MyURL?action=view&type=XferLOG&num=$Backups[$i]{num}&host=${EscURI($host)}">$Lang->{XferLOG}</a>,
1263                       <a href="$MyURL?action=view&type=XferErr&num=$Backups[$i]{num}&host=${EscURI($host)}">$Lang->{Errors}</a> </td>
1264     <td align="right">  $Backups[$i]{xferErrs} </td>
1265     <td align="right">  $Backups[$i]{xferBadFile} </td>
1266     <td align="right">  $Backups[$i]{xferBadShare} </td>
1267     <td align="right">  $Backups[$i]{tarErrs} </td></tr>
1268 EOF
1269     }
1270
1271     my @Restores = $bpc->RestoreInfoRead($host);
1272     my $restoreStr;
1273
1274     for ( my $i = 0 ; $i < @Restores ; $i++ ) {
1275         my $startTime = timeStamp2($Restores[$i]{startTime});
1276         my $dur       = $Restores[$i]{endTime} - $Restores[$i]{startTime};
1277         $dur          = 1 if ( $dur <= 0 );
1278         my $duration  = sprintf("%.1f", $dur / 60);
1279         my $MB        = sprintf("%.1f", $Restores[$i]{size} / (1024*1024));
1280         my $MBperSec  = sprintf("%.2f", $Restores[$i]{size} / (1024*1024*$dur));
1281         my $Restores_Result = $Lang->{failed};
1282         if ($Restores[$i]{result} ne "failed") { $Restores_Result = $Lang->{success}; }
1283         $restoreStr  .= <<EOF;
1284 <tr><td align="center"><a href="$MyURL?action=restoreInfo&num=$Restores[$i]{num}&host=${EscURI($host)}">$Restores[$i]{num}</a> </td>
1285     <td align="center"> $Restores_Result </td>
1286     <td align="right"> $startTime </td>
1287     <td align="right"> $duration </td>
1288     <td align="right"> $Restores[$i]{nFiles} </td>
1289     <td align="right"> $MB </td>
1290     <td align="right"> $Restores[$i]{tarCreateErrs} </td>
1291     <td align="right"> $Restores[$i]{xferErrs} </td>
1292 </tr>
1293 EOF
1294     }
1295     if ( $restoreStr ne "" ) {
1296         $restoreStr = eval("qq{$Lang->{Restore_Summary}}");
1297     }
1298     if ( @Backups == 0 ) {
1299         $warnStr = $Lang->{This_PC_has_never_been_backed_up};
1300     }
1301     if ( defined($Hosts->{$host}) ) {
1302         my $user = $Hosts->{$host}{user};
1303         my @moreUsers = sort(keys(%{$Hosts->{$host}{moreUsers}}));
1304         my $moreUserStr;
1305         foreach my $u ( sort(keys(%{$Hosts->{$host}{moreUsers}})) ) {
1306             $moreUserStr .= ", " if ( $moreUserStr ne "" );
1307             $moreUserStr .= "${UserLink($u)}";
1308         }
1309         if ( $moreUserStr ne "" ) {
1310             $moreUserStr = " ($Lang->{and} $moreUserStr).\n";
1311         } else {
1312             $moreUserStr = ".\n";
1313         }
1314         if ( $user ne "" ) {
1315             $statusStr .= eval("qq{$Lang->{This_PC_is_used_by}$moreUserStr}");
1316         }
1317         if ( defined($UserEmailInfo{$user})
1318                 && $UserEmailInfo{$user}{lastHost} eq $host ) {
1319             my $mailTime = timeStamp2($UserEmailInfo{$user}{lastTime});
1320             my $subj     = $UserEmailInfo{$user}{lastSubj};
1321             $statusStr  .= eval("qq{$Lang->{Last_email_sent_to__was_at___subject}}");
1322         }
1323     }
1324     if ( defined($Jobs{$host}) ) {
1325         my $startTime = timeStamp2($Jobs{$host}{startTime});
1326         (my $cmd = $Jobs{$host}{cmd}) =~ s/$BinDir\///g;
1327         $statusStr .= eval("qq{$Lang->{The_command_cmd_is_currently_running_for_started}}");
1328     }
1329     if ( $StatusHost{BgQueueOn} ) {
1330         $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_background_queue_will_be_backed_up_soon}}");
1331     }
1332     if ( $StatusHost{UserQueueOn} ) {
1333         $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_user_queue__will_be_backed_up_soon}}");
1334     }
1335     if ( $StatusHost{CmdQueueOn} ) {
1336         $statusStr .= eval("qq{$Lang->{A_command_for_host_is_on_the_command_queue_will_run_soon}}");
1337     }
1338     my $startTime = timeStamp2($StatusHost{endTime} == 0 ?
1339                 $StatusHost{startTime} : $StatusHost{endTime});
1340     my $reason = "";
1341     if ( $StatusHost{reason} ne "" ) {
1342         $reason = " ($Lang->{$StatusHost{reason}})";
1343     }
1344     $statusStr .= eval("qq{$Lang->{Last_status_is_state_StatusHost_state_reason_as_of_startTime}}");
1345
1346     if ( $StatusHost{error} ne "" ) {
1347         $statusStr .= eval("qq{$Lang->{Last_error_is____EscHTML_StatusHost_error}}");
1348     }
1349     my $priorStr = "Pings";
1350     if ( $StatusHost{deadCnt} > 0 ) {
1351         $statusStr .= eval("qq{$Lang->{Pings_to_host_have_failed_StatusHost_deadCnt__consecutive_times}}");
1352         $priorStr = $Lang->{Prior_to_that__pings};
1353     }
1354     if ( $StatusHost{aliveCnt} > 0 ) {
1355         $statusStr .= eval("qq{$Lang->{priorStr_to_host_have_succeeded_StatusHostaliveCnt_consecutive_times}}");
1356
1357         if ( $StatusHost{aliveCnt} >= $Conf{BlackoutGoodCnt}
1358                 && $Conf{BlackoutGoodCnt} >= 0 && $Conf{BlackoutHourBegin} >= 0
1359                 && $Conf{BlackoutHourEnd} >= 0 ) {
1360             my(@days) = qw(Sun Mon Tue Wed Thu Fri Sat);
1361             my($days) = join(", ", @days[@{$Conf{BlackoutWeekDays}}]);
1362             my($t0) = sprintf("%d:%02d", $Conf{BlackoutHourBegin},
1363                             60 * ($Conf{BlackoutHourBegin}
1364                                      - int($Conf{BlackoutHourBegin})));
1365             my($t1) = sprintf("%d:%02d", $Conf{BlackoutHourEnd},
1366                             60 * ($Conf{BlackoutHourEnd}
1367                                      - int($Conf{BlackoutHourEnd})));
1368             $statusStr .= eval("qq{$Lang->{Because__host_has_been_on_the_network_at_least__Conf_BlackoutGoodCnt_consecutive_times___}}");
1369         }
1370     }
1371     if ( $StatusHost{backoffTime} > time ) {
1372         my $hours = sprintf("%.1f", ($StatusHost{backoffTime} - time) / 3600);
1373         $statusStr .= eval("qq{$Lang->{Backups_are_deferred_for_hours_hours_change_this_number}}");
1374
1375     }
1376     if ( @Backups ) {
1377         # only allow incremental if there are already some backups
1378         $startIncrStr = <<EOF;
1379 <input type="submit" value="\$Lang->{Start_Incr_Backup}" name="action">
1380 EOF
1381     }
1382
1383     $startIncrStr = eval ("qq{$startIncrStr}");
1384
1385     Header(eval("qq{$Lang->{Host__host_Backup_Summary}}"));
1386     print(eval("qq{$Lang->{Host__host_Backup_Summary2}}"));
1387     Trailer();
1388 }
1389
1390 sub Action_GeneralInfo
1391 {
1392     GetStatusInfo("info jobs hosts queueLen");
1393     my $Privileged = CheckPermission();
1394
1395     my($jobStr, $statusStr);
1396     foreach my $host ( sort(keys(%Jobs)) ) {
1397         my $startTime = timeStamp2($Jobs{$host}{startTime});
1398         next if ( $host eq $bpc->trashJob
1399                     && $Jobs{$host}{processState} ne "running" );
1400         $Jobs{$host}{type} = $Status{$host}{type}
1401                     if ( $Jobs{$host}{type} eq "" && defined($Status{$host}));
1402         (my $cmd = $Jobs{$host}{cmd}) =~ s/$BinDir\///g;
1403         (my $xferPid = $Jobs{$host}{xferPid}) =~ s/,/, /g;
1404         $jobStr .= <<EOF;
1405 <tr><td> ${HostLink($host)} </td>
1406     <td align="center"> $Jobs{$host}{type} </td>
1407     <td align="center"> ${UserLink($Hosts->{$host}{user})} </td>
1408     <td> $startTime </td>
1409     <td> $cmd </td>
1410     <td align="center"> $Jobs{$host}{pid} </td>
1411     <td align="center"> $xferPid </td>
1412 EOF
1413         $jobStr .= "</tr>\n";
1414     }
1415     foreach my $host ( sort(keys(%Status)) ) {
1416         next if ( $Status{$host}{reason} ne "Reason_backup_failed"
1417                || $Status{$host}{error} =~ /^Can't find host \Q$host/ );
1418         my $startTime = timeStamp2($Status{$host}{startTime});
1419         my($errorTime, $XferViewStr);
1420         if ( $Status{$host}{errorTime} > 0 ) {
1421             $errorTime = timeStamp2($Status{$host}{errorTime});
1422         }
1423         if ( -f "$TopDir/pc/$host/SmbLOG.bad"
1424                 || -f "$TopDir/pc/$host/SmbLOG.bad.z"
1425                 || -f "$TopDir/pc/$host/XferLOG.bad"
1426                 || -f "$TopDir/pc/$host/XferLOG.bad.z"
1427                 ) {
1428             $XferViewStr = <<EOF;
1429 <a href="$MyURL?action=view&type=XferLOGbad&host=${EscURI($host)}">$Lang->{XferLOG}</a>,
1430 <a href="$MyURL?action=view&type=XferErrbad&host=${EscURI($host)}">$Lang->{Errors}</a>
1431 EOF
1432         } else {
1433             $XferViewStr = "";
1434         }
1435         (my $shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;   
1436         $statusStr .= <<EOF;
1437 <tr><td> ${HostLink($host)} </td>
1438     <td align="center"> $Status{$host}{type} </td>
1439     <td align="center"> ${UserLink($Hosts->{$host}{user})} </td>
1440     <td align="right"> $startTime </td>
1441     <td> $XferViewStr </td>
1442     <td align="right"> $errorTime </td>
1443     <td> ${EscHTML($shortErr)} </td></tr>
1444 EOF
1445     }
1446     my $now          = timeStamp2(time);
1447     my $nextWakeupTime = timeStamp2($Info{nextWakeup});
1448     my $DUlastTime   = timeStamp2($Info{DUlastValueTime});
1449     my $DUmaxTime    = timeStamp2($Info{DUDailyMaxTime});
1450     my $numBgQueue   = $QueueLen{BgQueue};
1451     my $numUserQueue = $QueueLen{UserQueue};
1452     my $numCmdQueue  = $QueueLen{CmdQueue};
1453     my $serverStartTime = timeStamp2($Info{startTime});
1454     my $poolInfo     = genPoolInfo("pool", \%Info);
1455     my $cpoolInfo    = genPoolInfo("cpool", \%Info);
1456     if ( $Info{poolFileCnt} > 0 && $Info{cpoolFileCnt} > 0 ) {
1457         $poolInfo = <<EOF;
1458 <li>Uncompressed pool:
1459 <ul>
1460 $poolInfo
1461 </ul>
1462 <li>Compressed pool:
1463 <ul>
1464 $cpoolInfo
1465 </ul>
1466 EOF
1467     } elsif ( $Info{cpoolFileCnt} > 0 ) {
1468         $poolInfo = $cpoolInfo;
1469     }
1470
1471     Header($Lang->{H_BackupPC_Server_Status});
1472     print (eval ("qq{$Lang->{BackupPC_Server_Status}}"));
1473     Trailer();
1474 }
1475
1476 sub Action_RestoreInfo
1477 {
1478     my $Privileged = CheckPermission($In{host});
1479     my $host = $1 if ( $In{host} =~ /(.*)/ );
1480     my $num  = $In{num};
1481     my $i;
1482
1483     if ( !$Privileged ) {
1484         ErrorExit($Lang->{Only_privileged_users_can_view_restore_information});
1485     }
1486     #
1487     # Find the requested restore
1488     #
1489     my @Restores = $bpc->RestoreInfoRead($host);
1490     for ( $i = 0 ; $i < @Restores ; $i++ ) {
1491         last if ( $Restores[$i]{num} == $num );
1492     }
1493     if ( $i >= @Restores ) {
1494         ErrorExit(eval("qq{$Lang->{Restore_number__num_for_host__does_not_exist}}"));
1495     }
1496
1497     %RestoreReq = ();
1498     do "$TopDir/pc/$host/RestoreInfo.$Restores[$i]{num}"
1499             if ( -f "$TopDir/pc/$host/RestoreInfo.$Restores[$i]{num}" );
1500
1501     my $startTime = timeStamp2($Restores[$i]{startTime});
1502     my $reqTime   = timeStamp2($RestoreReq{reqTime});
1503     my $dur       = $Restores[$i]{endTime} - $Restores[$i]{startTime};
1504     $dur          = 1 if ( $dur <= 0 );
1505     my $duration  = sprintf("%.1f", $dur / 60);
1506     my $MB        = sprintf("%.1f", $Restores[$i]{size} / (1024*1024));
1507     my $MBperSec  = sprintf("%.2f", $Restores[$i]{size} / (1024*1024*$dur));
1508
1509     my $fileListStr = "";
1510     foreach my $f ( @{$RestoreReq{fileList}} ) {
1511         my $targetFile = $f;
1512         (my $strippedShareSrc  = $RestoreReq{shareSrc}) =~ s/^\///;
1513         (my $strippedShareDest = $RestoreReq{shareDest}) =~ s/^\///;
1514         substr($targetFile, 0, length($RestoreReq{pathHdrSrc}))
1515                                         = $RestoreReq{pathHdrDest};
1516         $fileListStr .= <<EOF;
1517 <tr><td>$RestoreReq{hostSrc}:/$strippedShareSrc$f</td><td>$RestoreReq{hostDest}:/$strippedShareDest$targetFile</td></tr>
1518 EOF
1519     }
1520
1521     Header(eval("qq{$Lang->{Restore___num_details_for__host}}"));
1522     print(eval("qq{$Lang->{Restore___num_details_for__host2 }}"));
1523     Trailer();
1524 }
1525     
1526 ###########################################################################
1527 # Miscellaneous subroutines
1528 ###########################################################################
1529
1530 sub timeStamp2
1531 {
1532     my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
1533               = localtime($_[0] == 0 ? time : $_[0] );
1534     $year += 1900;
1535     $mon++;
1536     if ( $Conf{CgiDateFormatMMDD} ) {
1537         return sprintf("$mon/$mday %02d:%02d", $hour, $min);
1538     } else {
1539         return sprintf("$mday/$mon %02d:%02d", $hour, $min);
1540     }
1541 }
1542
1543 sub HostLink
1544 {
1545     my($host) = @_;
1546     my($s);
1547     if ( defined($Hosts->{$host}) || defined($Status{$host}) ) {
1548         $s = "<a href=\"$MyURL?host=${EscURI($host)}\">$host</a>";
1549     } else {
1550         $s = $host;
1551     }
1552     return \$s;
1553 }
1554
1555 sub UserLink
1556 {
1557     my($user) = @_;
1558     my($s);
1559
1560     return \$user if ( $user eq ""
1561                     || $Conf{CgiUserUrlCreate} eq "" );
1562     if ( $Conf{CgiUserHomePageCheck} eq ""
1563             || -f sprintf($Conf{CgiUserHomePageCheck}, $user, $user, $user) ) {
1564         $s = "<a href=\""
1565              . sprintf($Conf{CgiUserUrlCreate}, $user, $user, $user)
1566              . "\">$user</a>";
1567     } else {
1568         $s = $user;
1569     }
1570     return \$s;
1571 }
1572
1573 sub EscHTML
1574 {
1575     my($s) = @_;
1576     $s =~ s/&/&amp;/g;
1577     $s =~ s/\"/&quot;/g;
1578     $s =~ s/>/&gt;/g;
1579     $s =~ s/</&lt;/g;
1580     $s =~ s{([^[:print:]])}{sprintf("&\#x%02X;", ord($1));}eg;
1581     return \$s;
1582 }
1583
1584 sub EscURI
1585 {
1586     my($s) = @_;
1587     $s =~ s{([^\w.\/-])}{sprintf("%%%02X", ord($1));}eg;
1588     return \$s;
1589 }
1590
1591 sub ErrorExit
1592 {
1593     my(@mesg) = @_;
1594     my($head) = shift(@mesg);
1595     my($mesg) = join("</p>\n<p>", @mesg);
1596     $Conf{CgiHeaderFontType} ||= "arial"; 
1597     $Conf{CgiHeaderFontSize} ||= "3";  
1598     $Conf{CgiNavBarBgColor}  ||= "#ddeeee";
1599     $Conf{CgiHeaderBgColor}  ||= "#99cc33";
1600
1601     $bpc->ServerMesg("log User $User (host=$In{host}) got CGI error: $head")
1602                             if ( defined($bpc) );
1603     if ( !defined($Lang->{Error}) ) {
1604         Header("BackupPC: Error");
1605         $mesg = <<EOF if ( !defined($mesg) );
1606 There is some problem with the BackupPC installation.
1607 Please check the permissions on BackupPC_Admin.
1608 EOF
1609         print <<EOF;
1610 ${h1("Error: Unable to read config.pl or language strings!!")}
1611 <p>$mesg</p>
1612 EOF
1613         Trailer();
1614     } else {
1615         Header(eval("qq{$Lang->{Error}}"));
1616         print (eval("qq{$Lang->{Error____head}}"));
1617         Trailer();
1618     }
1619     exit(1);
1620 }
1621
1622 sub ServerConnect
1623 {
1624     #
1625     # Verify that the server connection is ok
1626     #
1627     return if ( $bpc->ServerOK() );
1628     $bpc->ServerDisconnect();
1629     if ( my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}) ) {
1630         ErrorExit(eval("qq{$Lang->{Unable_to_connect_to_BackupPC_server}}"));
1631     }
1632 }
1633
1634 sub GetStatusInfo
1635 {
1636     my($status) = @_;
1637     ServerConnect();
1638     my $reply = $bpc->ServerMesg("status $status");
1639     $reply = $1 if ( $reply =~ /(.*)/s );
1640     eval($reply);
1641     # ignore status related to admin and trashClean jobs
1642     if ( $status =~ /\bhosts\b/ ) {
1643         delete($Status{$bpc->adminJob});
1644         delete($Status{$bpc->trashJob});
1645     }
1646 }
1647
1648 sub ReadUserEmailInfo
1649 {
1650     if ( (stat("$TopDir/log/UserEmailInfo.pl"))[9] != $UserEmailInfoMTime ) {
1651         do "$TopDir/log/UserEmailInfo.pl";
1652         $UserEmailInfoMTime = (stat("$TopDir/log/UserEmailInfo.pl"))[9];
1653     }
1654 }
1655
1656 #
1657 # Check if the user is privileged.  A privileged user can access
1658 # any information (backup files, logs, status pages etc).
1659 #
1660 # A user is privileged if they belong to the group
1661 # $Conf{CgiAdminUserGroup}, or they are in $Conf{CgiAdminUsers}
1662 # or they are the user assigned to a host in the host file.
1663 #
1664 sub CheckPermission
1665 {
1666     my($host) = @_;
1667     my $Privileged = 0;
1668
1669     return 0 if ( $User eq "" || ($host ne "" && !defined($Hosts->{$host})) );
1670     if ( $Conf{CgiAdminUserGroup} ne "" ) {
1671         my($n,$p,$gid,$mem) = getgrnam($Conf{CgiAdminUserGroup});
1672         $Privileged ||= ($mem =~ /\b$User\b/);
1673     }
1674     if ( $Conf{CgiAdminUsers} ne "" ) {
1675         $Privileged ||= ($Conf{CgiAdminUsers} =~ /\b$User\b/);
1676         $Privileged ||= $Conf{CgiAdminUsers} eq "*";
1677     }
1678     $PrivAdmin = $Privileged;
1679     $Privileged ||= $User eq $Hosts->{$host}{user};
1680     $Privileged ||= defined($Hosts->{$host}{moreUsers}{$User});
1681
1682     return $Privileged;
1683 }
1684
1685 #
1686 # Returns the list of hosts that should appear in the navigation bar
1687 # for this user.  If $Conf{CgiNavBarAdminAllHosts} is set, the admin
1688 # gets all the hosts.  Otherwise, regular users get hosts for which
1689 # they are the user or are listed in the moreUsers column in the
1690 # hosts file.
1691 #
1692 sub GetUserHosts
1693 {
1694     if ( $Conf{CgiNavBarAdminAllHosts} && CheckPermission() ) {
1695        return sort keys %$Hosts;
1696     }
1697
1698     return sort grep { $Hosts->{$_}{user} eq $User ||
1699                        defined($Hosts->{$_}{moreUsers}{$User}) } keys(%$Hosts);
1700 }
1701
1702 #
1703 # Given a host name tries to find the IP address.  For non-dhcp hosts
1704 # we just return the host name.  For dhcp hosts we check the address
1705 # the user is using ($ENV{REMOTE_ADDR}) and also the last-known IP
1706 # address for $host.  (Later we should replace this with a broadcast
1707 # nmblookup.)
1708 #
1709 sub ConfirmIPAddress
1710 {
1711     my($host) = @_;
1712     my $ipAddr = $host;
1713
1714     if ( $Hosts->{$host}{dhcp}
1715                && $ENV{REMOTE_ADDR} =~ /^(\d+[\.\d]*)$/ ) {
1716         $ipAddr = $1;
1717         my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($ipAddr);
1718         if ( $netBiosHost ne $host ) {
1719             my($tryIP);
1720             GetStatusInfo("host(${EscURI($host)})");
1721             if ( defined($StatusHost{dhcpHostIP})
1722                         && $StatusHost{dhcpHostIP} ne $ipAddr ) {
1723                 $tryIP = eval("qq{$Lang->{tryIP}}");
1724                 ($netBiosHost, $netBiosUser)
1725                         = $bpc->NetBiosInfoGet($StatusHost{dhcpHostIP});
1726             }
1727             if ( $netBiosHost ne $host ) {
1728                 ErrorExit(eval("qq{$Lang->{Can_t_find_IP_address_for}}"),
1729                           eval("qq{$Lang->{host_is_a_DHCP_host}}"));
1730             }
1731             $ipAddr = $StatusHost{dhcpHostIP};
1732         }
1733     }
1734     return $ipAddr;
1735 }
1736
1737 sub genPoolInfo
1738 {
1739     my($name, $info) = @_;
1740     my $poolSize   = sprintf("%.2f", $info->{"${name}Kb"} / (1000 * 1024));
1741     my $poolRmSize = sprintf("%.2f", $info->{"${name}KbRm"} / (1000 * 1024));
1742     my $poolTime   = timeStamp2($info->{"${name}Time"});
1743     $info->{"${name}FileCntRm"} = $info->{"${name}FileCntRm"} + 0;
1744     return eval("qq{$Lang->{Pool_Stat}}");
1745 }
1746
1747 ###########################################################################
1748 # HTML layout subroutines
1749 ###########################################################################
1750
1751 sub Header
1752 {
1753     my($title) = @_;
1754     my @adminLinks = (
1755         { link => "",                          name => $Lang->{Status},
1756                                                priv => 1},
1757         { link => "?action=summary",           name => $Lang->{PC_Summary} },
1758         { link => "?action=view&type=LOG",     name => $Lang->{LOG_file} },
1759         { link => "?action=LOGlist",           name => $Lang->{Old_LOGs} },
1760         { link => "?action=emailSummary",      name => $Lang->{Email_summary} },
1761         { link => "?action=view&type=config",  name => $Lang->{Config_file} },
1762         { link => "?action=view&type=hosts",   name => $Lang->{Hosts_file} },
1763         { link => "?action=queue",             name => $Lang->{Current_queues} },
1764         { link => "?action=view&type=docs",    name => $Lang->{Documentation},
1765                                                priv => 1},
1766         { link => "http://backuppc.sourceforge.net/faq", name => "FAQ",
1767                                                priv => 1},
1768         { link => "http://backuppc.sourceforge.net", name => "SourceForge",
1769                                                priv => 1},
1770     );
1771     print $Cgi->header();
1772     print <<EOF;
1773 <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
1774 <html><head>
1775 <title>$title</title>
1776 $Conf{CgiHeaders}
1777 </head><body bgcolor="$Conf{CgiBodyBgColor}">
1778 <table cellpadding="0" cellspacing="0" border="0">
1779 <tr valign="top"><td valign="top" bgcolor="$Conf{CgiNavBarBgColor}" width="10%">
1780 EOF
1781     NavSectionTitle("BackupPC");
1782     print "&nbsp;\n";
1783     if ( defined($In{host}) && defined($Hosts->{$In{host}}) ) {
1784         my $host = $In{host};
1785         NavSectionTitle( eval("qq{$Lang->{Host_Inhost}}") );
1786         NavSectionStart();
1787         NavLink("?host=${EscURI($host)}", $Lang->{Home});
1788         NavLink("?action=view&type=LOG&host=${EscURI($host)}", $Lang->{LOG_file});
1789         NavLink("?action=LOGlist&host=${EscURI($host)}", $Lang->{Old_LOGs});
1790         if ( -f "$TopDir/pc/$host/SmbLOG.bad"
1791                     || -f "$TopDir/pc/$host/SmbLOG.bad.z"
1792                     || -f "$TopDir/pc/$host/XferLOG.bad"
1793                     || -f "$TopDir/pc/$host/XferLOG.bad.z" ) {
1794             NavLink("?action=view&type=XferLOGbad&host=${EscURI($host)}",
1795                                 $Lang->{Last_bad_XferLOG});
1796             NavLink("?action=view&type=XferErrbad&host=${EscURI($host)}",
1797                                 $Lang->{Last_bad_XferLOG_errors_only});
1798         }
1799         if ( -f "$TopDir/pc/$host/config.pl" ) {
1800             NavLink("?action=view&type=config&host=${EscURI($host)}", $Lang->{Config_file});
1801         }
1802         NavSectionEnd();
1803     }
1804     NavSectionTitle($Lang->{Hosts});
1805     if ( defined($Hosts) && %$Hosts > 0 ) {
1806         NavSectionStart(0);
1807         foreach my $host ( GetUserHosts() ) {
1808             NavLink("?host=${EscURI($host)}", $host);
1809         }
1810         NavSectionEnd();
1811     }
1812     print <<EOF;
1813 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1814     <tr><td>$Lang->{Host_or_User_name}</td>
1815     <tr><td><form action="$MyURL" method="get"><small>
1816     <input type="text" name="host" size="10" maxlength="64">
1817     <input type="hidden" name="action" value="hostInfo"><input type="submit" value="$Lang->{Go}" name="ignore">
1818     </small></form></td></tr>
1819 </table>
1820 EOF
1821     NavSectionTitle($Lang->{NavSectionTitle_});
1822     NavSectionStart();
1823     foreach my $l ( @adminLinks ) {
1824         if ( $PrivAdmin || $l->{priv} ) {
1825             NavLink($l->{link}, $l->{name});
1826         } else {
1827             NavLink(undef, $l->{name});
1828         }
1829     }
1830     NavSectionEnd();
1831     print <<EOF;
1832 </td><td valign="top" width="5">&nbsp;&nbsp;</td>
1833 <td valign="top" width="90%">
1834 EOF
1835 }
1836
1837 sub Trailer
1838 {
1839     print <<EOF;
1840 </td></table>
1841 </body></html>
1842 EOF
1843 }
1844
1845
1846 sub NavSectionTitle
1847 {
1848     my($head) = @_;
1849     print <<EOF;
1850 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1851 <tr><td bgcolor="$Conf{CgiHeaderBgColor}"><font face="$Conf{CgiHeaderFontType}"
1852 size="$Conf{CgiHeaderFontSize}"><b>$head</b>
1853 </font></td></tr>
1854 </table>
1855 EOF
1856 }
1857
1858 sub NavSectionStart
1859 {
1860     my($padding) = @_;
1861
1862     $padding = 2 if ( !defined($padding) );
1863     print <<EOF;
1864 <table cellpadding="$padding" cellspacing="0" border="0" width="100%">
1865 EOF
1866 }
1867
1868 sub NavSectionEnd
1869 {
1870     print "</table>\n";
1871 }
1872
1873 sub NavLink
1874 {
1875     my($link, $text) = @_;
1876     print "<tr><td width=\"2%\" valign=\"top\"><b>&middot;</b></td>";
1877     if ( defined($link) ) {
1878         $link = "$MyURL$link" if ( $link eq "" || $link =~ /^\?/ );
1879         print <<EOF;
1880 <td width="98%"><a href="$link"><small>$text</small></a></td></tr>
1881 EOF
1882     } else {
1883         print <<EOF;
1884 <td width="98%"><small>$text</small></td></tr>
1885 EOF
1886     }
1887 }
1888
1889 sub h1
1890 {
1891     my($str) = @_;
1892     return \<<EOF;
1893 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1894 <tr>
1895 <td bgcolor="$Conf{CgiHeaderBgColor}">&nbsp;<font face="$Conf{CgiHeaderFontType}"
1896     size="$Conf{CgiHeaderFontSize}"><b>$str</b></font>
1897 </td></tr>
1898 </table>
1899 EOF
1900 }
1901
1902 sub h2
1903 {
1904     my($str) = @_;
1905     return \<<EOF;
1906 <table cellpadding="2" cellspacing="0" border="0" width="100%">
1907 <tr>
1908 <td bgcolor="$Conf{CgiHeaderBgColor}">&nbsp;<font face="$Conf{CgiHeaderFontType}"
1909     size="$Conf{CgiHeaderFontSize}"><b>$str</b></font>
1910 </td></tr>
1911 </table>
1912 EOF
1913 }