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