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