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