]>
Commit | Line | Data |
---|---|---|
cf29614f | 1 | #!/usr/bin/perl |
70df8302 MT |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
eb7fff99 | 5 | # Copyright (C) 2005-2013 IPFire Team <info@ipfire.org> # |
70df8302 MT |
6 | # # |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
cf29614f CS |
21 | |
22 | use strict; | |
23 | # enable only the following on debugging purpose | |
36dfbfcf CS |
24 | #use warnings; |
25 | #use CGI::Carp 'fatalsToBrowser'; | |
8e8bbd9d | 26 | use File::Copy; |
7f6257e0 | 27 | use File::Basename; |
cf29614f CS |
28 | |
29 | require '/var/ipfire/general-functions.pl'; | |
30 | require "${General::swroot}/lang.pl"; | |
31 | require "${General::swroot}/header.pl"; | |
32 | ||
33 | my %color = (); | |
34 | my %mainsettings = (); | |
8e8bbd9d | 35 | my %cgiparams=(); |
cf29614f CS |
36 | my %checked = (); |
37 | my $message = ""; | |
38 | my $errormessage = ""; | |
26906d98 CS |
39 | my @backups = ""; |
40 | my @backupisos = ""; | |
cf29614f | 41 | |
8e8bbd9d | 42 | $a = new CGI; |
cf29614f CS |
43 | |
44 | &General::readhash("${General::swroot}/main/settings", \%mainsettings); | |
8186b372 | 45 | &General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color); |
cf29614f | 46 | |
8e8bbd9d CS |
47 | $cgiparams{'ACTION'} = ''; |
48 | $cgiparams{'FILE'} = ''; | |
49 | $cgiparams{'UPLOAD'} = ''; | |
50 | $cgiparams{'BACKUPLOGS'} = ''; | |
69addbb8 | 51 | |
8e8bbd9d | 52 | &Header::getcgihash(\%cgiparams); |
cf29614f | 53 | |
a609bfb0 CS |
54 | ############################################################################################################################ |
55 | ################################################ Workaround for Directories ################################################ | |
56 | ||
a87366f5 | 57 | &General::system("/usr/local/bin/backupctrl", "makedirs") unless ( -e '/var/ipfire/backup/addons/backup') ; |
a609bfb0 | 58 | |
cf29614f | 59 | ############################################################################################################################ |
69addbb8 | 60 | ############################################## System calls ohne Http Header ############################################### |
cf29614f | 61 | |
7f6257e0 MT |
62 | if ($cgiparams{'ACTION'} eq "download") { |
63 | my $file = &sanitise_file($cgiparams{'FILE'}); | |
64 | exit(1) unless defined($file); | |
65 | ||
66 | &deliver_file($file); | |
67 | exit(0); | |
68 | } elsif ($cgiparams{'ACTION'} eq "downloadiso") { | |
69 | my $file = &sanitise_file($cgiparams{'FILE'}); | |
70 | exit(1) unless defined($file); | |
71 | ||
72 | &deliver_file($file); | |
73 | exit(0); | |
74 | } elsif ($cgiparams{'ACTION'} eq "downloadaddon") { | |
75 | my $file = &sanitise_file($cgiparams{'FILE'}); | |
76 | exit(1) unless defined($file); | |
77 | ||
78 | &deliver_file($file); | |
79 | exit(0); | |
80 | } elsif ( $cgiparams{'ACTION'} eq "restore") { | |
901a50cf CS |
81 | my $upload = $a->param("UPLOAD"); |
82 | open UPLOADFILE, ">/tmp/restore.ipf"; | |
83 | binmode $upload; | |
84 | while ( <$upload> ) { | |
85 | print UPLOADFILE; | |
86 | } | |
87 | close UPLOADFILE; | |
a87366f5 | 88 | &General::system("/usr/local/bin/backupctrl", "restore"); |
8e8bbd9d | 89 | } |
5ad5a6bc MT |
90 | elsif ( $cgiparams{'ACTION'} eq "restoreaddon" ) |
91 | { | |
d2b476e8 CS |
92 | chomp($cgiparams{'UPLOAD'}); |
93 | # we need to fix cause IE7 gives the full path and FF only the filename | |
94 | my @temp = split(/\\/,$cgiparams{'UPLOAD'}); | |
5ad5a6bc | 95 | my $upload = $a->param("UPLOAD"); |
d2b476e8 | 96 | open UPLOADFILE, ">/tmp/".$temp[$#temp]; |
5ad5a6bc MT |
97 | binmode $upload; |
98 | while ( <$upload> ) { | |
99 | print UPLOADFILE; | |
100 | } | |
101 | close UPLOADFILE; | |
a87366f5 | 102 | &General::system("/usr/local/bin/backupctrl", "restoreaddon", $temp[$#temp]); |
5ad5a6bc | 103 | } |
cf29614f | 104 | |
8e8bbd9d | 105 | &Header::showhttpheaders(); |
69addbb8 CS |
106 | |
107 | sub refreshpage{&Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";&Header::closebox();} | |
108 | ||
8e8bbd9d CS |
109 | &Header::openpage($Lang::tr{'backup'}, 1, ""); |
110 | &Header::openbigbox('100%', 'left', '', $errormessage); | |
111 | ||
69addbb8 CS |
112 | ############################################################################################################################ |
113 | ################################################### Default System calls ################################################### | |
114 | ||
115 | if ( $cgiparams{'ACTION'} eq "backup" ) | |
116 | { | |
f31d3c1f | 117 | if ( $cgiparams{'BACKUPLOGS'} eq "include" ) { |
a87366f5 | 118 | &General::system("/usr/local/bin/backupctrl", "include"); |
f31d3c1f | 119 | } elsif ( $cgiparams{'BACKUPLOGS'} eq "exclude" ) { |
a87366f5 | 120 | &General::system("/usr/local/bin/backupctrl", "exclude"); |
f31d3c1f | 121 | } elsif ( $cgiparams{'BACKUPLOGS'} eq "iso" ) { |
45a5df5a | 122 | &General::system_background("/usr/local/bin/backupctrl", "iso"); |
f31d3c1f | 123 | } |
901a50cf CS |
124 | } |
125 | if ( $cgiparams{'ACTION'} eq "addonbackup" ) | |
126 | { | |
614764e5 MT |
127 | # Exit if there is any dots or slashes in the addon name |
128 | exit(1) if ($cgiparams{'ADDON'} =~ /(\.|\/)/); | |
129 | ||
130 | # Check if the addon exists | |
131 | exit(1) unless (-e "/var/ipfire/backup/addons/includes/$cgiparams{'ADDON'}"); | |
132 | ||
a87366f5 | 133 | &General::system("/usr/local/bin/backupctrl", "addonbackup", "$cgiparams{'ADDON'}"); |
69addbb8 CS |
134 | } |
135 | elsif ( $cgiparams{'ACTION'} eq "delete" ) | |
136 | { | |
7f6257e0 MT |
137 | my $file = &sanitise_file($cgiparams{'FILE'}); |
138 | exit(1) unless defined($file); | |
139 | ||
a87366f5 | 140 | &General::system("/usr/local/bin/backupctrl", "$file"); |
69addbb8 CS |
141 | } |
142 | ||
143 | ############################################################################################################################ | |
901a50cf | 144 | ############################################ Backups des Systems erstellen ################################################# |
69addbb8 | 145 | |
8e8bbd9d CS |
146 | if ( $message ne "" ){ |
147 | &Header::openbox('100%','left',$Lang::tr{'error messages'}); | |
148 | print "<font color='red'>$message</font>\n"; | |
149 | &Header::closebox(); | |
150 | } | |
cf29614f | 151 | |
26906d98 CS |
152 | if ( -e "/var/ipfire/backup/" ){ |
153 | @backups = `cd /var/ipfire/backup/ && ls *.ipf 2>/dev/null`; | |
154 | } | |
155 | ||
156 | if ( -e "/var/tmp/backupiso/" ){ | |
157 | @backupisos = `cd /var/tmp/backupiso/ && ls *.iso 2>/dev/null`; | |
158 | } | |
901a50cf | 159 | |
6f5487d7 | 160 | &Header::openbox('100%', 'center', ); |
cf29614f CS |
161 | |
162 | print <<END | |
163 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
164 | <table width='95%' cellspacing='0'> | |
f31d3c1f DG |
165 | <tr> |
166 | <td align='left' width='40%'>$Lang::tr{'logs'}</td> | |
167 | <td align='left'> | |
168 | <input type='radio' name='BACKUPLOGS' value='include'/> $Lang::tr{'include logfiles'}<br/> | |
169 | <input type='radio' name='BACKUPLOGS' value='exclude' checked='checked'/> $Lang::tr{'exclude logfiles'}<br/> | |
eb7fff99 AF |
170 | END |
171 | ; | |
172 | my $MACHINE=`uname -m`; | |
173 | if ( ! ( $MACHINE =~ "arm" )) { | |
174 | print" <input type='radio' name='BACKUPLOGS' value='iso' /> $Lang::tr{'generate iso'}<br/>" | |
175 | } | |
176 | print <<END | |
f31d3c1f DG |
177 | </td> |
178 | </tr> | |
901a50cf CS |
179 | <tr><td align='center' colspan='2'> |
180 | <input type='hidden' name='ACTION' value='backup' /> | |
181 | <input type='image' alt='$Lang::tr{'backup'}' title='$Lang::tr{'backup'}' src='/images/document-save.png' /> | |
182 | </td></tr> | |
cf29614f CS |
183 | </table> |
184 | </form> | |
185 | END | |
186 | ; | |
187 | &Header::closebox(); | |
188 | ||
901a50cf CS |
189 | ############################################################################################################################ |
190 | ############################################ Backups des Systems downloaden ################################################ | |
191 | ||
cf29614f CS |
192 | &Header::openbox('100%', 'center', $Lang::tr{'backups'}); |
193 | ||
194 | print <<END | |
cf29614f CS |
195 | <table width='95%' cellspacing='0'> |
196 | END | |
197 | ; | |
198 | foreach (@backups){ | |
6b8be088 CS |
199 | if ( $_ !~ /ipf$/){next;} |
200 | chomp($_); | |
69addbb8 | 201 | my $Datei = "/var/ipfire/backup/".$_; |
8e8bbd9d | 202 | my @Info = stat($Datei); |
5504342a JPT |
203 | my $Size = $Info[7] / 1024 / 1024; |
204 | $Size = sprintf("%0.2f", $Size); | |
205 | print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size MB</td><td width='5'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='download' /><input type='hidden' name='FILE' value='$_' /><input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' /></form></td>"; | |
69addbb8 | 206 | print "<td width='5'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='delete' /><input type='hidden' name='FILE' value='$_' /><input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' /></form></td></tr>"; |
cf29614f | 207 | } |
b90a7e56 | 208 | foreach (@backupisos){ |
6b8be088 | 209 | if ( $_ !~ /iso$/){next;} |
b90a7e56 DG |
210 | chomp($_); |
211 | my $Datei = "/var/tmp/backupiso/".$_; | |
212 | my @Info = stat($Datei); | |
5504342a JPT |
213 | my $Size = $Info[7] / 1024 / 1024; |
214 | $Size = sprintf("%0.2f", $Size); | |
215 | print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size MB</td><td width='5'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='downloadiso' /><input type='hidden' name='FILE' value='$_' /><input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' /></form></td>"; | |
b90a7e56 DG |
216 | print "<td width='5'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='delete' /><input type='hidden' name='FILE' value='$_' /><input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' /></form></td></tr>"; |
217 | } | |
901a50cf | 218 | print <<END |
cf29614f | 219 | </table> |
8e8bbd9d CS |
220 | END |
221 | ; | |
222 | &Header::closebox(); | |
223 | ||
901a50cf CS |
224 | ############################################################################################################################ |
225 | ############################################# Backups von Addons erstellen ################################################# | |
226 | ||
4ede2956 | 227 | &Header::openbox('100%', 'center', $Lang::tr{'addons'}); |
901a50cf CS |
228 | |
229 | my @addonincluds = `ls /var/ipfire/backup/addons/includes/ 2>/dev/null`; | |
98ab7f50 CS |
230 | my @addons = `ls /var/ipfire/backup/addons/backup/ 2>/dev/null`; |
231 | my %addons; | |
232 | ||
233 | foreach (@addons){ | |
234 | my $addon=substr($_,0,length($_)-5); | |
235 | $addons{$addon}=''; | |
236 | } | |
901a50cf CS |
237 | |
238 | print "<table width='95%' cellspacing='0'>"; | |
239 | foreach (@addonincluds){ | |
240 | chomp($_); | |
98ab7f50 | 241 | delete $addons{$_}; |
901a50cf CS |
242 | my $Datei = "/var/ipfire/backup/addons/backup/".$_.".ipf"; |
243 | my @Info = stat($Datei); | |
244 | my $Size = $Info[7] / 1024; | |
c35ac552 | 245 | |
901a50cf | 246 | if ( -e $Datei ){ |
c35ac552 JPT |
247 | if ($Size < 1) { |
248 | $Size = sprintf("%.2f", $Size); | |
249 | print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>"; | |
250 | } else { | |
251 | $Size = sprintf("%2d", $Size); | |
252 | print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>"; | |
253 | ||
254 | } | |
255 | ||
901a50cf CS |
256 | print <<END |
257 | <td align='right' width='5'> | |
258 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
5ad5a6bc MT |
259 | <input type='hidden' name='ACTION' value='downloadaddon' /> |
260 | <input type='hidden' name='FILE' value='$_.ipf' /> | |
901a50cf CS |
261 | <input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' /> |
262 | </form> | |
263 | </td> | |
264 | <td align='right' width='5'> | |
265 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
266 | <input type='hidden' name='ACTION' value='delete' /> | |
a6c19081 | 267 | <input type='hidden' name='FILE' value='$_.ipf' /> |
901a50cf CS |
268 | <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' /> |
269 | </form> | |
270 | </td> | |
271 | END | |
272 | ; | |
273 | } | |
274 | else{ | |
b8e5a9c5 | 275 | print "<tr><td align='center'>$Lang::tr{'backup from'} $_ </td><td width='5' align='right'></td><td width='5' align='right'></td>"; |
901a50cf CS |
276 | } |
277 | print <<END | |
278 | <td align='right' width='5'> | |
279 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
280 | <input type='hidden' name='ACTION' value='addonbackup' /> | |
281 | <input type='hidden' name='ADDON' value='$_' /> | |
282 | <input type='image' alt='$Lang::tr{'backup'}' title='$Lang::tr{'backup'}' src='/images/document-save.png' /> | |
283 | </form> | |
284 | </td></tr> | |
285 | END | |
286 | ; | |
287 | } | |
b8e5a9c5 CS |
288 | foreach (keys(%addons)){ |
289 | chomp($_); | |
98ab7f50 | 290 | my $Datei = "/var/ipfire/backup/addons/backup/".$_.".ipf"; |
b8e5a9c5 CS |
291 | my @Info = stat($Datei); |
292 | my $Size = $Info[7] / 1024; | |
293 | $Size = sprintf("%2d", $Size); | |
98ab7f50 | 294 | print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>"; |
b8e5a9c5 CS |
295 | print <<END |
296 | <td align='right' width='5'> | |
297 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
298 | <input type='hidden' name='ACTION' value='downloadaddon' /> | |
98ab7f50 | 299 | <input type='hidden' name='FILE' value='$_.ipf' /> |
b8e5a9c5 CS |
300 | <input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' /> |
301 | </form> | |
302 | </td> | |
303 | <td align='right' width='5'> | |
304 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
305 | <input type='hidden' name='ACTION' value='delete' /> | |
a6c19081 | 306 | <input type='hidden' name='FILE' value='$_.ipf' /> |
b8e5a9c5 CS |
307 | <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' /> |
308 | </form> | |
309 | </td> | |
310 | <td align='right' width='5'></td></tr> | |
311 | END | |
312 | ; | |
313 | } | |
314 | ||
901a50cf CS |
315 | print "</table>"; |
316 | &Header::closebox(); | |
5ad5a6bc MT |
317 | |
318 | ############################################################################################################################ | |
319 | ####################################### Backups des Systems wiederherstellen ############################################### | |
320 | ||
321 | &Header::openbox('100%', 'center', $Lang::tr{'restore'}); | |
322 | ||
323 | print <<END | |
324 | <table width='95%' cellspacing='0'> | |
325 | <tr><td align='center' colspan='2'><font color='red'><br />$Lang::tr{'backupwarning'}</font><br /><br /></td></tr> | |
d2b476e8 CS |
326 | <tr><td align='left'>$Lang::tr{'backup'}</td><td align='left'><form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'><input type="file" size='50' name="UPLOAD" /><input type='hidden' name='ACTION' value='restore' /><input type='hidden' name='FILE' /><input type='image' alt='$Lang::tr{'restore'}' title='$Lang::tr{'restore'}' src='/images/media-floppy.png' /></form></td></tr> |
327 | <tr><td align='left'>$Lang::tr{'backupaddon'}</td><td align='left'><form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'><input type="file" size='50' name="UPLOAD" /><input type='hidden' name='ACTION' value='restoreaddon' /><input type='hidden' name='FILE' /><input type='image' alt='$Lang::tr{'restore'}' title='$Lang::tr{'restore'}' src='/images/media-floppy.png' /></form></td></tr> | |
5ad5a6bc MT |
328 | </table> |
329 | END | |
330 | ; | |
331 | &Header::closebox(); | |
cf29614f | 332 | &Header::closebigbox(); |
4ede2956 | 333 | &Header::closepage(); |
7f6257e0 MT |
334 | |
335 | sub sanitise_file() { | |
336 | my $file = shift; | |
337 | ||
338 | # Filenames cannot contain any slashes | |
339 | return undef if ($file =~ /\//); | |
340 | ||
341 | # File must end with .ipf or .iso | |
342 | return undef unless ($file =~ /\.(ipf|iso)$/); | |
343 | ||
344 | # Convert to absolute path | |
345 | if (-e "/var/ipfire/backup/$file") { | |
346 | return "/var/ipfire/backup/$file"; | |
347 | } elsif (-e "/var/ipfire/backup/addons/backup/$file") { | |
348 | return "/var/ipfire/backup/addons/backup/$file"; | |
349 | } elsif (-e "/var/tmp/backupiso/$file") { | |
350 | return "/var/tmp/backupiso/$file"; | |
351 | } | |
352 | ||
353 | # File does not seem to exist | |
354 | return undef; | |
355 | } | |
356 | ||
357 | sub deliver_file() { | |
358 | my $file = shift; | |
359 | my @stat = stat($file); | |
360 | ||
361 | # Print headers | |
362 | print "Content-Disposition: attachment; filename=" . &File::Basename::basename($file) . "\n"; | |
363 | print "Content-Type: application/octet-stream\n"; | |
364 | print "Content-Length: $stat[7]\n"; | |
365 | print "\n"; | |
366 | ||
367 | # Deliver content | |
368 | open(FILE, "<$file") or die "Unable to open $file: $!"; | |
369 | print <FILE>; | |
370 | close(FILE); | |
371 | } |