29cfcf8bc84f97516fbdc6865f9e5ee6ed806491
[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 3 Jul 2003.
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
43 sub action
44 {
45     my($str, $reply, $content);
46     my $Privileged = CheckPermission($In{host});
47     if ( !$Privileged ) {
48         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files}}"));
49     }
50     my $host  = $In{host};
51     my $num   = $In{num};
52     my $share = $In{share};
53     my(@fileList, $fileListStr, $hiddenStr, $pathHdr, $badFileCnt);
54     my @Backups = $bpc->BackupInfoRead($host);
55
56     ServerConnect();
57     if ( !defined($Hosts->{$host}) ) {
58         ErrorExit(eval("qq{$Lang->{Bad_host_name}}"));
59     }
60     for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
61         next if ( !defined($In{"fcb$i"}) );
62         (my $name = $In{"fcb$i"}) =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
63         $badFileCnt++ if ( $name =~ m{(^|/)\.\.(/|$)} );
64         if ( @fileList == 0 ) {
65             $pathHdr = $name;
66         } else {
67             while ( substr($name, 0, length($pathHdr)) ne $pathHdr ) {
68                 $pathHdr = substr($pathHdr, 0, rindex($pathHdr, "/"));
69             }
70         }
71         push(@fileList, $name);
72         $hiddenStr .= <<EOF;
73 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
74 EOF
75         $fileListStr .= <<EOF;
76 <li> ${EscHTML($name)}
77 EOF
78     }
79     $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
80     $hiddenStr .= "<input type=\"hidden\" name=\"share\" value=\"${EscHTML($share)}\">\n";
81     $badFileCnt++ if ( $In{pathHdr} =~ m{(^|/)\.\.(/|$)} );
82     $badFileCnt++ if ( $In{num} =~ m{(^|/)\.\.(/|$)} );
83     if ( @fileList == 0 ) {
84         ErrorExit($Lang->{You_haven_t_selected_any_files__please_go_Back_to});
85     }
86     if ( $badFileCnt ) {
87         ErrorExit($Lang->{Nice_try__but_you_can_t_put});
88     }
89     if ( @fileList == 1 ) {
90         $pathHdr =~ s/(.*)\/.*/$1/;
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;
105         foreach my $h ( GetUserHosts() ) {
106             my $sel = " selected" if ( $h eq $In{host} );
107             $hostDestSel .= "<option value=\"$h\"$sel>${EscHTML($h)}</option>";
108         }
109
110         #
111         # Tell the user what options they have
112         #
113         $content .= eval("qq{$Lang->{Restore_Options_for__host2}}");
114
115         #
116         # Verify that Archive::Zip is available before showing the
117         # zip restore option
118         #
119         if ( eval { require Archive::Zip } ) {
120             $content .= eval("qq{$Lang->{Option_2__Download_Zip_archive}}");
121         } else {
122             $content .= eval("qq{$Lang->{Option_2__Download_Zip_archive2}}");
123         }
124         $content .= eval("qq{$Lang->{Option_3__Download_Zip_archive}}");
125         Header(eval("qq{$Lang->{Restore_Options_for__host}}"), $content);
126         Trailer();
127     } elsif ( $In{type} == 1 ) {
128         #
129         # Provide the selected files via a tar archive.
130         #
131         my @fileListTrim = @fileList;
132         if ( @fileListTrim > 10 ) {
133             @fileListTrim = (@fileListTrim[0..9], '...');
134         }
135         $bpc->ServerMesg("log User $User downloaded tar archive for $host,"
136                        . " backup $num; files were: "
137                        . join(", ", @fileListTrim));
138
139         my @pathOpts;
140         if ( $In{relative} ) {
141             @pathOpts = ("-r", $pathHdr, "-p", "");
142         }
143         print(STDOUT <<EOF);
144 Content-Type: application/x-gtar
145 Content-Transfer-Encoding: binary
146 Content-Disposition: attachment; filename=\"restore.tar\"
147
148 EOF
149         #
150         # Fork the child off and manually copy the output to our stdout.
151         # This is necessary to ensure the output gets to the correct place
152         # under mod_perl.
153         #
154         $bpc->cmdSystemOrEval(["$BinDir/BackupPC_tarCreate",
155                  "-h", $host,
156                  "-n", $num,
157                  "-s", $share,
158                  @pathOpts,
159                  @fileList
160             ],
161             sub { print(@_); }
162         );
163     } elsif ( $In{type} == 2 ) {
164         #
165         # Provide the selected files via a zip archive.
166         #
167         my @fileListTrim = @fileList;
168         if ( @fileListTrim > 10 ) {
169             @fileListTrim = (@fileListTrim[0..9], '...');
170         }
171         $bpc->ServerMesg("log User $User downloaded zip archive for $host,"
172                        . " backup $num; files were: "
173                        . join(", ", @fileListTrim));
174
175         my @pathOpts;
176         if ( $In{relative} ) {
177             @pathOpts = ("-r", $pathHdr, "-p", "");
178         }
179         print(STDOUT <<EOF);
180 Content-Type: application/zip
181 Content-Transfer-Encoding: binary
182 Content-Disposition: attachment; filename=\"restore.zip\"
183
184 EOF
185         $In{compressLevel} = 5 if ( $In{compressLevel} !~ /^\d+$/ );
186         #
187         # Fork the child off and manually copy the output to our stdout.
188         # This is necessary to ensure the output gets to the correct place
189         # under mod_perl.
190         #
191         $bpc->cmdSystemOrEval(["$BinDir/BackupPC_zipCreate",
192                  "-h", $host,
193                  "-n", $num,
194                  "-c", $In{compressLevel},
195                  "-s", $share,
196                  @pathOpts,
197                  @fileList
198             ],
199             sub { print(@_); }
200         );
201     } elsif ( $In{type} == 3 ) {
202         #
203         # Do restore directly onto host
204         #
205         if ( !defined($Hosts->{$In{hostDest}}) ) {
206             ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
207         }
208         if ( !CheckPermission($In{hostDest}) ) {
209             ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
210         }
211         $fileListStr = "";
212         foreach my $f ( @fileList ) {
213             my $targetFile = $f;
214             (my $strippedShare = $share) =~ s/^\///;
215             (my $strippedShareDest = $In{shareDest}) =~ s/^\///;
216             substr($targetFile, 0, length($pathHdr)) = $In{pathHdr};
217             $fileListStr .= <<EOF;
218 <tr><td>$host:/$strippedShare$f</td><td>$In{hostDest}:/$strippedShareDest$targetFile</td></tr>
219 EOF
220         }
221         my $content = eval("qq{$Lang->{Are_you_sure}}");
222         Header(eval("qq{$Lang->{Restore_Confirm_on__host}}"), $content);
223         Trailer();
224     } elsif ( $In{type} == 4 ) {
225         if ( !defined($Hosts->{$In{hostDest}}) ) {
226             ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
227         }
228         if ( !CheckPermission($In{hostDest}) ) {
229             ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
230         }
231         my $hostDest = $1 if ( $In{hostDest} =~ /(.+)/ );
232         my $ipAddr = ConfirmIPAddress($hostDest);
233         #
234         # Prepare and send the restore request.  We write the request
235         # information using Data::Dumper to a unique file,
236         # $TopDir/pc/$hostDest/restoreReq.$$.n.  We use a file
237         # in case the list of files to restore is very long.
238         #
239         my $reqFileName;
240         for ( my $i = 0 ; ; $i++ ) {
241             $reqFileName = "restoreReq.$$.$i";
242             last if ( !-f "$TopDir/pc/$hostDest/$reqFileName" );
243         }
244         my %restoreReq = (
245             # source of restore is hostSrc, #num, path shareSrc/pathHdrSrc
246             num         => $In{num},
247             hostSrc     => $host,
248             shareSrc    => $share,
249             pathHdrSrc  => $pathHdr,
250
251             # destination of restore is hostDest:shareDest/pathHdrDest
252             hostDest    => $hostDest,
253             shareDest   => $In{shareDest},
254             pathHdrDest => $In{pathHdr},
255
256             # list of files to restore
257             fileList    => \@fileList,
258
259             # other info
260             user        => $User,
261             reqTime     => time,
262         );
263         my($dump) = Data::Dumper->new(
264                          [  \%restoreReq],
265                          [qw(*RestoreReq)]);
266         $dump->Indent(1);
267         if ( open(REQ, ">$TopDir/pc/$hostDest/$reqFileName") ) {
268             binmode(REQ);
269             print(REQ $dump->Dump);
270             close(REQ);
271         } else {
272             ErrorExit(eval("qq{$Lang->{Can_t_open_create}}"));
273         }
274         $reply = $bpc->ServerMesg("restore ${EscURI($ipAddr)}"
275                         . " ${EscURI($hostDest)} $User $reqFileName");
276         $str = eval("qq{$Lang->{Restore_requested_to_host__hostDest__backup___num}}");
277         my $content = eval("qq{$Lang->{Reply_from_server_was___reply}}");
278         Header(eval("qq{$Lang->{Restore_Requested_on__hostDest}}"), $content);
279         Trailer();
280     }
281 }
282
283 1;