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