c871eb379f1fb015d3cc42b297b66716721b7001
[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-2009  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 3.2.0beta0, released 5 April 2009.
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 BackupPC::Xfer;
42 use Data::Dumper;
43 use File::Path;
44 use Encode qw/decode_utf8/;
45
46 sub action
47 {
48     my($str, $reply, $content);
49     my $Privileged = CheckPermission($In{host});
50     if ( !$Privileged ) {
51         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files}}"));
52     }
53     my $host  = $In{host};
54     my $num   = $In{num};
55     my $share = $In{share};
56     my(@fileList, $fileListStr, $hiddenStr, $pathHdr, $badFileCnt);
57     my @Backups = $bpc->BackupInfoRead($host);
58
59     ServerConnect();
60     if ( !defined($Hosts->{$host}) ) {
61         ErrorExit(eval("qq{$Lang->{Bad_host_name}}"));
62     }
63     for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
64         next if ( !defined($In{"fcb$i"}) );
65         (my $name = $In{"fcb$i"}) =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
66         $badFileCnt++ if ( $name =~ m{(^|/)\.\.(/|$)} );
67         if ( @fileList == 0 ) {
68             $pathHdr = substr($name, 0, rindex($name, "/"));
69         } else {
70             while ( substr($name, 0, length($pathHdr)) ne $pathHdr ) {
71                 $pathHdr = substr($pathHdr, 0, rindex($pathHdr, "/"));
72             }
73         }
74         push(@fileList, $name);
75         $hiddenStr .= <<EOF;
76 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
77 EOF
78         $name = decode_utf8($name);
79         $fileListStr .= <<EOF;
80 <li> ${EscHTML($name)}
81 EOF
82     }
83     $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
84     $hiddenStr .= "<input type=\"hidden\" name=\"share\" value=\"${EscHTML(decode_utf8($share))}\">\n";
85     $badFileCnt++ if ( $In{pathHdr} =~ m{(^|/)\.\.(/|$)} );
86     $badFileCnt++ if ( $In{num} =~ m{(^|/)\.\.(/|$)} );
87     if ( @fileList == 0 ) {
88         ErrorExit($Lang->{You_haven_t_selected_any_files__please_go_Back_to});
89     }
90     if ( $badFileCnt ) {
91         ErrorExit($Lang->{Nice_try__but_you_can_t_put});
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, @hosts, $gotThisHost, $directHost);
106
107         #
108         # Check all the hosts this user has permissions for
109         # and make sure direct restore is enabled.
110         # Note: after this loop we have the config for the
111         # last host in @hosts, not the original $In{host}!!
112         #
113         $directHost = $host;
114         foreach my $h ( GetUserHosts(1) ) {
115             #
116             # Pick up the host's config file
117             #
118             $bpc->ConfigRead($h);
119             %Conf = $bpc->Conf();
120             if ( BackupPC::Xfer::restoreEnabled( \%Conf ) ) {
121                 #
122                 # Direct restore is enabled
123                 #
124                 push(@hosts, $h);
125                 $gotThisHost = 1 if ( $h eq $host );
126             }
127         }
128         $directHost = $hosts[0] if ( !$gotThisHost && @hosts );
129         foreach my $h ( @hosts ) {
130             my $sel = " selected" if ( $h eq $directHost );
131             $hostDestSel .= "<option value=\"$h\"$sel>${EscHTML($h)}</option>";
132         }
133
134         #
135         # Tell the user what options they have
136         #
137         $pathHdr = decode_utf8($pathHdr);
138         $share   = decode_utf8($share);
139         $content = eval("qq{$Lang->{Restore_Options_for__host2}}");
140
141         #
142         # Decide if option 1 (direct restore) is available based
143         # on whether the restore command is set.
144         #
145         if ( $hostDestSel ne "" ) {
146             $content .= eval(
147                 "qq{$Lang->{Restore_Options_for__host_Option1}}");
148         } else {
149             my $hostDest = $In{host};
150             $content .= eval(
151                 "qq{$Lang->{Restore_Options_for__host_Option1_disabled}}");
152         }
153
154         #
155         # Verify that Archive::Zip is available before showing the
156         # zip restore option
157         #
158         if ( eval { require Archive::Zip } ) {
159             $content .= eval("qq{$Lang->{Option_2__Download_Zip_archive}}");
160         } else {
161             $content .= eval("qq{$Lang->{Option_2__Download_Zip_archive2}}");
162         }
163         $content .= eval("qq{$Lang->{Option_3__Download_Zip_archive}}");
164         Header(eval("qq{$Lang->{Restore_Options_for__host}}"), $content);
165         Trailer();
166     } elsif ( $In{type} == 1 ) {
167         #
168         # Provide the selected files via a tar archive.
169         #
170         my @fileListTrim = @fileList;
171         if ( @fileListTrim > 10 ) {
172             @fileListTrim = (@fileListTrim[0..9], '...');
173         }
174         $bpc->ServerMesg("log User $User downloaded tar archive for $host,"
175                        . " backup $num; files were: "
176                        . join(", ", @fileListTrim));
177
178         my @pathOpts;
179         if ( $In{relative} ) {
180             @pathOpts = ("-r", $pathHdr, "-p", "");
181         }
182         print(STDOUT <<EOF);
183 Content-Type: application/x-gtar
184 Content-Transfer-Encoding: binary
185 Content-Disposition: attachment; filename=\"restore.tar\"
186
187 EOF
188         #
189         # Fork the child off and manually copy the output to our stdout.
190         # This is necessary to ensure the output gets to the correct place
191         # under mod_perl.
192         #
193         $bpc->cmdSystemOrEvalLong(["$BinDir/BackupPC_tarCreate",
194                  "-h", $host,
195                  "-n", $num,
196                  "-s", $share,
197                  @pathOpts,
198                  @fileList
199             ],
200             sub { print(@_); },
201             1,                  # ignore stderr
202         );
203     } elsif ( $In{type} == 2 ) {
204         #
205         # Provide the selected files via a zip archive.
206         #
207         my @fileListTrim = @fileList;
208         if ( @fileListTrim > 10 ) {
209             @fileListTrim = (@fileListTrim[0..9], '...');
210         }
211         $bpc->ServerMesg("log User $User downloaded zip archive for $host,"
212                        . " backup $num; files were: "
213                        . join(", ", @fileListTrim));
214
215         my @pathOpts;
216         if ( $In{relative} ) {
217             @pathOpts = ("-r", $pathHdr, "-p", "");
218         }
219         print(STDOUT <<EOF);
220 Content-Type: application/zip
221 Content-Transfer-Encoding: binary
222 Content-Disposition: attachment; filename=\"restore.zip\"
223
224 EOF
225         $In{compressLevel} = 5 if ( $In{compressLevel} !~ /^\d+$/ );
226         #
227         # Fork the child off and manually copy the output to our stdout.
228         # This is necessary to ensure the output gets to the correct place
229         # under mod_perl.
230         #
231         $bpc->cmdSystemOrEvalLong(["$BinDir/BackupPC_zipCreate",
232                  "-h", $host,
233                  "-n", $num,
234                  "-c", $In{compressLevel},
235                  "-s", $share,
236                  @pathOpts,
237                  @fileList
238             ],
239             sub { print(@_); },
240             1,                  # ignore stderr
241         );
242     } elsif ( $In{type} == 3 ) {
243         #
244         # Do restore directly onto host
245         #
246         if ( !defined($Hosts->{$In{hostDest}}) ) {
247             ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
248         }
249         if ( !CheckPermission($In{hostDest}) ) {
250             ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
251         }
252         #
253         # Pick up the destination host's config file
254         #
255         my $hostDest = $1 if ( $In{hostDest} =~ /(.*)/ );
256         $bpc->ConfigRead($hostDest);
257         %Conf = $bpc->Conf();
258
259         #
260         # Decide if option 1 (direct restore) is available based
261         # on whether the restore command is set.
262         #
263         unless ( BackupPC::Xfer::restoreEnabled( \%Conf ) ) {
264             ErrorExit(eval("qq{$Lang->{Restore_Options_for__host_Option1_disabled}}"));
265         }
266
267         $fileListStr = "";
268         foreach my $f ( @fileList ) {
269             my $targetFile = $f;
270             (my $strippedShare = $share) =~ s/^\///;
271             (my $strippedShareDest = $In{shareDest}) =~ s/^\///;
272             substr($targetFile, 0, length($pathHdr)) = "/$In{pathHdr}/";
273             $targetFile =~ s{//+}{/}g;
274             $strippedShareDest = decode_utf8($strippedShareDest);
275             $targetFile = decode_utf8($targetFile);
276             $strippedShare = decode_utf8($strippedShare);
277             $f = decode_utf8($f);
278             $fileListStr .= <<EOF;
279 <tr><td>$host:/$strippedShare$f</td><td>$In{hostDest}:/$strippedShareDest$targetFile</td></tr>
280 EOF
281         }
282         $In{shareDest} = decode_utf8($In{shareDest});
283         $In{pathHdr}   = decode_utf8($In{pathHdr});
284         my $content = eval("qq{$Lang->{Are_you_sure}}");
285         Header(eval("qq{$Lang->{Restore_Confirm_on__host}}"), $content);
286         Trailer();
287     } elsif ( $In{type} == 4 ) {
288         if ( !defined($Hosts->{$In{hostDest}}) ) {
289             ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
290         }
291         if ( !CheckPermission($In{hostDest}) ) {
292             ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
293         }
294         my $hostDest = $1 if ( $In{hostDest} =~ /(.+)/ );
295         my $ipAddr = ConfirmIPAddress($hostDest);
296         #
297         # Prepare and send the restore request.  We write the request
298         # information using Data::Dumper to a unique file,
299         # $TopDir/pc/$hostDest/restoreReq.$$.n.  We use a file
300         # in case the list of files to restore is very long.
301         #
302         my $reqFileName;
303         for ( my $i = 0 ; ; $i++ ) {
304             $reqFileName = "restoreReq.$$.$i";
305             last if ( !-f "$TopDir/pc/$hostDest/$reqFileName" );
306         }
307         my $inPathHdr = $In{pathHdr};
308         $inPathHdr = "/$inPathHdr" if ( $inPathHdr !~ m{^/} );
309         $inPathHdr = "$inPathHdr/" if ( $inPathHdr !~ m{/$} );
310         my %restoreReq = (
311             # source of restore is hostSrc, #num, path shareSrc/pathHdrSrc
312             num         => $In{num},
313             hostSrc     => $host,
314             shareSrc    => $share,
315             pathHdrSrc  => $pathHdr,
316
317             # destination of restore is hostDest:shareDest/pathHdrDest
318             hostDest    => $hostDest,
319             shareDest   => $In{shareDest},
320             pathHdrDest => $inPathHdr,
321
322             # list of files to restore
323             fileList    => \@fileList,
324
325             # other info
326             user        => $User,
327             reqTime     => time,
328         );
329         my($dump) = Data::Dumper->new(
330                          [  \%restoreReq],
331                          [qw(*RestoreReq)]);
332         $dump->Indent(1);
333         eval { mkpath("$TopDir/pc/$hostDest", 0, 0777) }
334                                     if ( !-d "$TopDir/pc/$hostDest" );
335         my $openPath = "$TopDir/pc/$hostDest/$reqFileName";
336         if ( open(REQ, ">", $openPath) ) {
337             binmode(REQ);
338             print(REQ $dump->Dump);
339             close(REQ);
340         } else {
341             ErrorExit(eval("qq{$Lang->{Can_t_open_create__openPath}}"));
342         }
343         $reply = $bpc->ServerMesg("restore ${EscURI($ipAddr)}"
344                         . " ${EscURI($hostDest)} $User $reqFileName");
345         $str = eval("qq{$Lang->{Restore_requested_to_host__hostDest__backup___num}}");
346         my $content = eval("qq{$Lang->{Reply_from_server_was___reply}}");
347         Header(eval("qq{$Lang->{Restore_Requested_on__hostDest}}"), $content);
348         Trailer();
349     }
350 }
351
352 1;