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