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