* Added Italian translation, it.pm, from Lorenzo Cappelletti
[BackupPC.git] / lib / BackupPC / CGI / Restore.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::CGI::Restore package
4 #
5 # DESCRIPTION
6 #
7 #   This module implements the Restore action for the CGI interface.
8 #
9 # AUTHOR
10 #   Craig Barratt  <cbarratt@users.sourceforge.net>
11 #
12 # COPYRIGHT
13 #   Copyright (C) 2003  Craig Barratt
14 #
15 #   This program is free software; you can redistribute it and/or modify
16 #   it under the terms of the GNU General Public License as published by
17 #   the Free Software Foundation; either version 2 of the License, or
18 #   (at your option) any later version.
19 #
20 #   This program is distributed in the hope that it will be useful,
21 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
22 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 #   GNU General Public License for more details.
24 #
25 #   You should have received a copy of the GNU General Public License
26 #   along with this program; if not, write to the Free Software
27 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 #
29 #========================================================================
30 #
31 # Version 2.1.0_CVS, released 8 Feb 2004.
32 #
33 # See http://backuppc.sourceforge.net.
34 #
35 #========================================================================
36
37 package BackupPC::CGI::Restore;
38
39 use strict;
40 use BackupPC::CGI::Lib qw(:all);
41 use Data::Dumper;
42 use File::Path;
43
44 sub action
45 {
46     my($str, $reply, $content);
47     my $Privileged = CheckPermission($In{host});
48     if ( !$Privileged ) {
49         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files}}"));
50     }
51     my $host  = $In{host};
52     my $num   = $In{num};
53     my $share = $In{share};
54     my(@fileList, $fileListStr, $hiddenStr, $pathHdr, $badFileCnt);
55     my @Backups = $bpc->BackupInfoRead($host);
56
57     ServerConnect();
58     if ( !defined($Hosts->{$host}) ) {
59         ErrorExit(eval("qq{$Lang->{Bad_host_name}}"));
60     }
61     for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
62         next if ( !defined($In{"fcb$i"}) );
63         (my $name = $In{"fcb$i"}) =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
64         $badFileCnt++ if ( $name =~ m{(^|/)\.\.(/|$)} );
65         if ( @fileList == 0 ) {
66             $pathHdr = substr($name, 0, rindex($name, "/"));
67         } else {
68             while ( substr($name, 0, length($pathHdr)) ne $pathHdr ) {
69                 $pathHdr = substr($pathHdr, 0, rindex($pathHdr, "/"));
70             }
71         }
72         push(@fileList, $name);
73         $hiddenStr .= <<EOF;
74 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
75 EOF
76         $fileListStr .= <<EOF;
77 <li> ${EscHTML($name)}
78 EOF
79     }
80     $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
81     $hiddenStr .= "<input type=\"hidden\" name=\"share\" value=\"${EscHTML($share)}\">\n";
82     $badFileCnt++ if ( $In{pathHdr} =~ m{(^|/)\.\.(/|$)} );
83     $badFileCnt++ if ( $In{num} =~ m{(^|/)\.\.(/|$)} );
84     if ( @fileList == 0 ) {
85         ErrorExit($Lang->{You_haven_t_selected_any_files__please_go_Back_to});
86     }
87     if ( $badFileCnt ) {
88         ErrorExit($Lang->{Nice_try__but_you_can_t_put});
89     }
90     if ( @fileList == 1 ) {
91         $pathHdr =~ s/(.*)\/.*/$1/;
92     }
93     $pathHdr = "/" if ( $pathHdr eq "" );
94     if ( $In{type} != 0 && @fileList == $In{fcbMax} ) {
95         #
96         # All the files in the list were selected, so just restore the
97         # entire parent directory
98         #
99         @fileList = ( $pathHdr );
100     }
101     if ( $In{type} == 0 ) {
102         #
103         # Build list of hosts
104         #
105         my $hostDestSel;
106         my @hosts;
107         foreach my $h ( GetUserHosts() ) {
108             my $sel = " selected" if ( $h eq $In{host} );
109             $hostDestSel .= "<option value=\"$h\"$sel>${EscHTML($h)}</option>";
110             push(@hosts, $h);
111         }
112
113         #
114         # Tell the user what options they have
115         #
116         $content .= eval("qq{$Lang->{Restore_Options_for__host2}}");
117
118         #
119         # If there is a single host, make sure direct restore is enabled
120         #
121         if ( @hosts == 1 ) {
122             #
123             # Pick up the host's config file
124             #
125             $bpc->ConfigRead($hosts[0]);
126             %Conf = $bpc->Conf();
127
128             #
129             # Decide if option 1 (direct restore) is available based
130             # on whether the restore command is set.
131             #
132             my $cmd = $Conf{XferMethod} eq "smb" ? $Conf{SmbClientRestoreCmd}
133                     : $Conf{XferMethod} eq "tar" ? $Conf{TarClientRestoreCmd}
134                     : $Conf{XferMethod} eq "archive" ? undef
135                     : $Conf{RsyncRestoreArgs};
136             if ( defined($cmd) ) {
137                 $content .= eval(
138                     "qq{$Lang->{Restore_Options_for__host_Option1}}");
139             } else {
140                 my $hostDest = $hosts[0];
141                 $content .= eval(
142                     "qq{$Lang->{Restore_Options_for__host_Option1_disabled}}");
143             }
144         } else {
145             $content .= eval("qq{$Lang->{Restore_Options_for__host_Option1}}");
146         }
147
148         #
149         # Verify that Archive::Zip is available before showing the
150         # zip restore option
151         #
152         if ( eval { require Archive::Zip } ) {
153             $content .= eval("qq{$Lang->{Option_2__Download_Zip_archive}}");
154         } else {
155             $content .= eval("qq{$Lang->{Option_2__Download_Zip_archive2}}");
156         }
157         $content .= eval("qq{$Lang->{Option_3__Download_Zip_archive}}");
158         Header(eval("qq{$Lang->{Restore_Options_for__host}}"), $content);
159         Trailer();
160     } elsif ( $In{type} == 1 ) {
161         #
162         # Provide the selected files via a tar archive.
163         #
164         my @fileListTrim = @fileList;
165         if ( @fileListTrim > 10 ) {
166             @fileListTrim = (@fileListTrim[0..9], '...');
167         }
168         $bpc->ServerMesg("log User $User downloaded tar archive for $host,"
169                        . " backup $num; files were: "
170                        . join(", ", @fileListTrim));
171
172         my @pathOpts;
173         if ( $In{relative} ) {
174             @pathOpts = ("-r", $pathHdr, "-p", "");
175         }
176         print(STDOUT <<EOF);
177 Content-Type: application/x-gtar
178 Content-Transfer-Encoding: binary
179 Content-Disposition: attachment; filename=\"restore.tar\"
180
181 EOF
182         #
183         # Fork the child off and manually copy the output to our stdout.
184         # This is necessary to ensure the output gets to the correct place
185         # under mod_perl.
186         #
187         $bpc->cmdSystemOrEval(["$BinDir/BackupPC_tarCreate",
188                  "-h", $host,
189                  "-n", $num,
190                  "-s", $share,
191                  @pathOpts,
192                  @fileList
193             ],
194             sub { print(@_); }
195         );
196     } elsif ( $In{type} == 2 ) {
197         #
198         # Provide the selected files via a zip archive.
199         #
200         my @fileListTrim = @fileList;
201         if ( @fileListTrim > 10 ) {
202             @fileListTrim = (@fileListTrim[0..9], '...');
203         }
204         $bpc->ServerMesg("log User $User downloaded zip archive for $host,"
205                        . " backup $num; files were: "
206                        . join(", ", @fileListTrim));
207
208         my @pathOpts;
209         if ( $In{relative} ) {
210             @pathOpts = ("-r", $pathHdr, "-p", "");
211         }
212         print(STDOUT <<EOF);
213 Content-Type: application/zip
214 Content-Transfer-Encoding: binary
215 Content-Disposition: attachment; filename=\"restore.zip\"
216
217 EOF
218         $In{compressLevel} = 5 if ( $In{compressLevel} !~ /^\d+$/ );
219         #
220         # Fork the child off and manually copy the output to our stdout.
221         # This is necessary to ensure the output gets to the correct place
222         # under mod_perl.
223         #
224         $bpc->cmdSystemOrEval(["$BinDir/BackupPC_zipCreate",
225                  "-h", $host,
226                  "-n", $num,
227                  "-c", $In{compressLevel},
228                  "-s", $share,
229                  @pathOpts,
230                  @fileList
231             ],
232             sub { print(@_); }
233         );
234     } elsif ( $In{type} == 3 ) {
235         #
236         # Do restore directly onto host
237         #
238         if ( !defined($Hosts->{$In{hostDest}}) ) {
239             ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
240         }
241         if ( !CheckPermission($In{hostDest}) ) {
242             ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
243         }
244         #
245         # Pick up the destination host's config file
246         #
247         my $hostDest = $1 if ( $In{hostDest} =~ /(.*)/ );
248         $bpc->ConfigRead($hostDest);
249         %Conf = $bpc->Conf();
250
251         #
252         # Decide if option 1 (direct restore) is available based
253         # on whether the restore command is set.
254         #
255         my $cmd = $Conf{XferMethod} eq "smb" ? $Conf{SmbClientRestoreCmd}
256                 : $Conf{XferMethod} eq "tar" ? $Conf{TarClientRestoreCmd}
257                 : $Conf{XferMethod} eq "archive" ? undef
258                 : $Conf{RsyncRestoreArgs};
259         if ( !defined($cmd) ) {
260             ErrorExit(eval("qq{$Lang->{Restore_Options_for__host_Option1_disabled}}"));
261         }
262
263         $fileListStr = "";
264         foreach my $f ( @fileList ) {
265             my $targetFile = $f;
266             (my $strippedShare = $share) =~ s/^\///;
267             (my $strippedShareDest = $In{shareDest}) =~ s/^\///;
268             substr($targetFile, 0, length($pathHdr)) = $In{pathHdr};
269             $fileListStr .= <<EOF;
270 <tr><td>$host:/$strippedShare$f</td><td>$In{hostDest}:/$strippedShareDest$targetFile</td></tr>
271 EOF
272         }
273         my $content = eval("qq{$Lang->{Are_you_sure}}");
274         Header(eval("qq{$Lang->{Restore_Confirm_on__host}}"), $content);
275         Trailer();
276     } elsif ( $In{type} == 4 ) {
277         if ( !defined($Hosts->{$In{hostDest}}) ) {
278             ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
279         }
280         if ( !CheckPermission($In{hostDest}) ) {
281             ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
282         }
283         my $hostDest = $1 if ( $In{hostDest} =~ /(.+)/ );
284         my $ipAddr = ConfirmIPAddress($hostDest);
285         #
286         # Prepare and send the restore request.  We write the request
287         # information using Data::Dumper to a unique file,
288         # $TopDir/pc/$hostDest/restoreReq.$$.n.  We use a file
289         # in case the list of files to restore is very long.
290         #
291         my $reqFileName;
292         for ( my $i = 0 ; ; $i++ ) {
293             $reqFileName = "restoreReq.$$.$i";
294             last if ( !-f "$TopDir/pc/$hostDest/$reqFileName" );
295         }
296         my %restoreReq = (
297             # source of restore is hostSrc, #num, path shareSrc/pathHdrSrc
298             num         => $In{num},
299             hostSrc     => $host,
300             shareSrc    => $share,
301             pathHdrSrc  => $pathHdr,
302
303             # destination of restore is hostDest:shareDest/pathHdrDest
304             hostDest    => $hostDest,
305             shareDest   => $In{shareDest},
306             pathHdrDest => $In{pathHdr},
307
308             # list of files to restore
309             fileList    => \@fileList,
310
311             # other info
312             user        => $User,
313             reqTime     => time,
314         );
315         my($dump) = Data::Dumper->new(
316                          [  \%restoreReq],
317                          [qw(*RestoreReq)]);
318         $dump->Indent(1);
319         mkpath("$TopDir/pc/$hostDest", 0, 0777)
320                                     if ( !-d "$TopDir/pc/$hostDest" );
321         if ( open(REQ, ">$TopDir/pc/$hostDest/$reqFileName") ) {
322             binmode(REQ);
323             print(REQ $dump->Dump);
324             close(REQ);
325         } else {
326             ErrorExit(eval("qq{$Lang->{Can_t_open_create}}"));
327         }
328         $reply = $bpc->ServerMesg("restore ${EscURI($ipAddr)}"
329                         . " ${EscURI($hostDest)} $User $reqFileName");
330         $str = eval("qq{$Lang->{Restore_requested_to_host__hostDest__backup___num}}");
331         my $content = eval("qq{$Lang->{Reply_from_server_was___reply}}");
332         Header(eval("qq{$Lang->{Restore_Requested_on__hostDest}}"), $content);
333         Trailer();
334     }
335 }
336
337 1;