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