Project

General

Profile

Download (26 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	diag_backup.php
5
*/
6
/* ====================================================================
7
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
8
 *	Copyright (c)  2004, 2005 Scott Ullrich
9
 *
10
 *	Redistribution and use in source and binary forms, with or without modification,
11
 *	are permitted provided that the following conditions are met:
12
 *
13
 *	1. Redistributions of source code must retain the above copyright notice,
14
 *		this list of conditions and the following disclaimer.
15
 *
16
 *	2. Redistributions in binary form must reproduce the above copyright
17
 *		notice, this list of conditions and the following disclaimer in
18
 *		the documentation and/or other materials provided with the
19
 *		distribution.
20
 *
21
 *	3. All advertising materials mentioning features or use of this software
22
 *		must display the following acknowledgment:
23
 *		"This product includes software developed by the pfSense Project
24
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
25
 *
26
 *	4. The names "pfSense" and "pfSense Project" must not be used to
27
 *		 endorse or promote products derived from this software without
28
 *		 prior written permission. For written permission, please contact
29
 *		 coreteam@pfsense.org.
30
 *
31
 *	5. Products derived from this software may not be called "pfSense"
32
 *		nor may "pfSense" appear in their names without prior written
33
 *		permission of the Electric Sheep Fencing, LLC.
34
 *
35
 *	6. Redistributions of any form whatsoever must retain the following
36
 *		acknowledgment:
37
 *
38
 *	"This product includes software developed by the pfSense Project
39
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
40
  *
41
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
42
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
45
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
53
 *
54
 *	====================================================================
55
 *
56
 */
57

    
58
/*
59
	pfSense_BUILDER_BINARIES:	/sbin/shutdown
60
	pfSense_MODULE: backup
61
*/
62

    
63
##|+PRIV
64
##|*IDENT=page-diagnostics-backup/restore
65
##|*NAME=Diagnostics: Backup/restore page
66
##|*DESCR=Allow access to the 'Diagnostics: Backup/restore' page.
67
##|*MATCH=diag_backup.php*
68
##|-PRIV
69

    
70
/* Allow additional execution time 0 = no limit. */
71
ini_set('max_execution_time', '0');
72
ini_set('max_input_time', '0');
73

    
74
/* omit no-cache headers because it confuses IE with file downloads */
75
$omit_nocacheheaders = true;
76
$nocsrf = true;
77
require("guiconfig.inc");
78
require_once("functions.inc");
79
require_once("filter.inc");
80
require_once("shaper.inc");
81

    
82
$rrddbpath = "/var/db/rrd";
83
$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
84

    
85
function rrd_data_xml() {
86
	global $rrddbpath;
87
	global $rrdtool;
88

    
89
	$result = "\t<rrddata>\n";
90
	$rrd_files = glob("{$rrddbpath}/*.rrd");
91
	$xml_files = array();
92
	foreach ($rrd_files as $rrd_file) {
93
		$basename = basename($rrd_file);
94
		$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
95
		exec("$rrdtool dump '{$rrd_file}' '{$xml_file}'");
96
		$xml_data = file_get_contents($xml_file);
97
		unlink($xml_file);
98
		if ($xml_data !== false) {
99
			$result .= "\t\t<rrddatafile>\n";
100
			$result .= "\t\t\t<filename>{$basename}</filename>\n";
101
			$result .= "\t\t\t<xmldata>" . base64_encode(gzdeflate($xml_data)) . "</xmldata>\n";
102
			$result .= "\t\t</rrddatafile>\n";
103
		}
104
	}
105
	$result .= "\t</rrddata>\n";
106
	return $result;
107
}
108

    
109
function restore_rrddata() {
110
	global $config, $g, $rrdtool, $input_errors;
111
	foreach ($config['rrddata']['rrddatafile'] as $rrd) {
112
		if ($rrd['xmldata']) {
113
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
114
			$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
115
			if (file_put_contents($xml_file, gzinflate(base64_decode($rrd['xmldata']))) === false) {
116
				log_error("Cannot write $xml_file");
117
				continue;
118
			}
119
			$output = array();
120
			$status = null;
121
			exec("$rrdtool restore -f '{$xml_file}' '{$rrd_file}'", $output, $status);
122
			if ($status) {
123
				log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
124
				continue;
125
			}
126
			unlink($xml_file);
127
		} else if ($rrd['data']) {
128
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
129
			$rrd_fd = fopen($rrd_file, "w");
130
			if (!$rrd_fd) {
131
				log_error("Cannot write $rrd_file");
132
				continue;
133
			}
134
			$data = base64_decode($rrd['data']);
135
			/* Try to decompress the data. */
136
			$dcomp = @gzinflate($data);
137
			if ($dcomp) {
138
				/* If the decompression worked, write the decompressed data */
139
				if (fwrite($rrd_fd, $dcomp) === false) {
140
					log_error("fwrite $rrd_file failed");
141
					continue;
142
				}
143
			} else {
144
				/* If the decompression failed, it wasn't compressed, so write raw data */
145
				if (fwrite($rrd_fd, $data) === false) {
146
					log_error("fwrite $rrd_file failed");
147
					continue;
148
				}
149
			}
150
			if (fclose($rrd_fd) === false) {
151
				log_error("fclose $rrd_file failed");
152
				continue;
153
			}
154
		}
155
	}
156
}
157

    
158
function add_base_packages_menu_items() {
159
	global $g, $config;
160
	$base_packages = explode(",", $g['base_packages']);
161
	$modified_config = false;
162
	foreach ($base_packages as $bp) {
163
		$basepkg_path = "/usr/local/pkg/{$bp}";
164
		$tmpinfo = pathinfo($basepkg_path, PATHINFO_EXTENSION);
165
		if ($tmpinfo['extension'] == "xml" && file_exists($basepkg_path)) {
166
			$pkg_config = parse_xml_config_pkg($basepkg_path, "packagegui");
167
			if ($pkg_config['menu'] != "") {
168
				if (is_array($pkg_config['menu'])) {
169
					foreach ($pkg_config['menu'] as $menu) {
170
						if (is_array($config['installedpackages']['menu'])) {
171
							foreach ($config['installedpackages']['menu'] as $amenu) {
172
								if ($amenu['name'] == $menu['name']) {
173
									continue;
174
								}
175
							}
176
						}
177
						$config['installedpackages']['menu'][] = $menu;
178
						$modified_config = true;
179
					}
180
				}
181
			}
182
		}
183
	}
184
	if ($modified_config) {
185
		write_config(gettext("Restored base_package menus after configuration restore."));
186
		$config = parse_config(true);
187
	}
188
}
189

    
190
function remove_bad_chars($string) {
191
	return preg_replace('/[^a-z_0-9]/i', '', $string);
192
}
193

    
194
function check_and_returnif_section_exists($section) {
195
	global $config;
196
	if (is_array($config[$section])) {
197
		return true;
198
	}
199
	return false;
200
}
201

    
202
if ($_POST['apply']) {
203
	ob_flush();
204
	flush();
205
	conf_mount_rw();
206
	clear_subsystem_dirty("restore");
207
	conf_mount_ro();
208
	exit;
209
}
210

    
211
if ($_POST) {
212
	unset($input_errors);
213
	if (stristr($_POST['Submit'], gettext("Restore configuration"))) {
214
		$mode = "restore";
215
	} else if (stristr($_POST['Submit'], gettext("Reinstall"))) {
216
		$mode = "reinstallpackages";
217
	} else if (stristr($_POST['Submit'], gettext("Clear Package Lock"))) {
218
		$mode = "clearpackagelock";
219
	} else if (stristr($_POST['Submit'], gettext("Download"))) {
220
		$mode = "download";
221
	} else if (stristr($_POST['Submit'], gettext("Restore version"))) {
222
		$mode = "restore_ver";
223
	}
224
	if ($_POST["nopackages"] <> "") {
225
		$options = "nopackages";
226
	}
227
	if ($_POST["ver"] <> "") {
228
		$ver2restore = $_POST["ver"];
229
	}
230
	if ($mode) {
231
		if ($mode == "download") {
232
			if ($_POST['encrypt']) {
233
				if (!$_POST['encrypt_password'] || !$_POST['encrypt_passconf']) {
234
					$input_errors[] = gettext("You must supply and confirm the password for encryption.");
235
				}
236
				if ($_POST['encrypt_password'] != $_POST['encrypt_passconf']) {
237
					$input_errors[] = gettext("The supplied 'Password' and 'Confirm' field values must match.");
238
				}
239
			}
240

    
241
			if (!$input_errors) {
242

    
243
				//$lockbckp = lock('config');
244

    
245
				$host = "{$config['system']['hostname']}.{$config['system']['domain']}";
246
				$name = "config-{$host}-".date("YmdHis").".xml";
247
				$data = "";
248

    
249
				if ($options == "nopackages") {
250
					if (!$_POST['backuparea']) {
251
						/* backup entire configuration */
252
						$data = file_get_contents("{$g['conf_path']}/config.xml");
253
					} else {
254
						/* backup specific area of configuration */
255
						$data = backup_config_section($_POST['backuparea']);
256
						$name = "{$_POST['backuparea']}-{$name}";
257
					}
258
					$sfn = "{$g['tmp_path']}/config.xml.nopkg";
259
					file_put_contents($sfn, $data);
260
					exec("sed '/<installedpackages>/,/<\/installedpackages>/d' {$sfn} > {$sfn}-new");
261
					$data = file_get_contents($sfn . "-new");
262
				} else {
263
					if (!$_POST['backuparea']) {
264
						/* backup entire configuration */
265
						$data = file_get_contents("{$g['conf_path']}/config.xml");
266
					} else if ($_POST['backuparea'] === "rrddata") {
267
						$data = rrd_data_xml();
268
						$name = "{$_POST['backuparea']}-{$name}";
269
					} else {
270
						/* backup specific area of configuration */
271
						$data = backup_config_section($_POST['backuparea']);
272
						$name = "{$_POST['backuparea']}-{$name}";
273
					}
274
				}
275

    
276
				//unlock($lockbckp);
277

    
278
				/*
279
				 *	Backup RRD Data
280
				 */
281
				if ($_POST['backuparea'] !== "rrddata" && !$_POST['donotbackuprrd']) {
282
					$rrd_data_xml = rrd_data_xml();
283
					$closing_tag = "</" . $g['xml_rootobj'] . ">";
284
					$data = str_replace($closing_tag, $rrd_data_xml . $closing_tag, $data);
285
				}
286

    
287
				if ($_POST['encrypt']) {
288
					$data = encrypt_data($data, $_POST['encrypt_password']);
289
					tagfile_reformat($data, $data, "config.xml");
290
				}
291

    
292
				$size = strlen($data);
293
				header("Content-Type: application/octet-stream");
294
				header("Content-Disposition: attachment; filename={$name}");
295
				header("Content-Length: $size");
296
				if (isset($_SERVER['HTTPS'])) {
297
					header('Pragma: ');
298
					header('Cache-Control: ');
299
				} else {
300
					header("Pragma: private");
301
					header("Cache-Control: private, must-revalidate");
302
				}
303
				echo $data;
304

    
305
				exit;
306
			}
307
		}
308

    
309
		if ($mode == "restore") {
310
			if ($_POST['decrypt']) {
311
				if (!$_POST['decrypt_password'] || !$_POST['decrypt_passconf']) {
312
					$input_errors[] = gettext("You must supply and confirm the password for decryption.");
313
				}
314
				if ($_POST['decrypt_password'] != $_POST['decrypt_passconf']) {
315
					$input_errors[] = gettext("The supplied 'Password' and 'Confirm' field values must match.");
316
				}
317
			}
318

    
319
			if (!$input_errors) {
320
				if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
321

    
322
					/* read the file contents */
323
					$data = file_get_contents($_FILES['conffile']['tmp_name']);
324
					if (!$data) {
325
						log_error(sprintf(gettext("Warning, could not read file %s"), $_FILES['conffile']['tmp_name']));
326
						return 1;
327
					}
328

    
329
					if ($_POST['decrypt']) {
330
						if (!tagfile_deformat($data, $data, "config.xml")) {
331
							$input_errors[] = gettext("The uploaded file does not appear to contain an encrypted pfsense configuration.");
332
							return 1;
333
						}
334
						$data = decrypt_data($data, $_POST['decrypt_password']);
335
					}
336

    
337
					if (stristr($data, "<m0n0wall>")) {
338
						log_error(gettext("Upgrading m0n0wall configuration to pfsense."));
339
						/* m0n0wall was found in config.  convert it. */
340
						$data = str_replace("m0n0wall", "pfsense", $data);
341
						$m0n0wall_upgrade = true;
342
					}
343
					if ($_POST['restorearea']) {
344
						/* restore a specific area of the configuration */
345
						if (!stristr($data, "<" . $_POST['restorearea'] . ">")) {
346
							$input_errors[] = gettext("You have selected to restore an area but we could not locate the correct xml tag.");
347
						} else {
348
							if (!restore_config_section($_POST['restorearea'], $data)) {
349
								$input_errors[] = gettext("You have selected to restore an area but we could not locate the correct xml tag.");
350
							} else {
351
								if ($config['rrddata']) {
352
									restore_rrddata();
353
									unset($config['rrddata']);
354
									unlink_if_exists("{$g['tmp_path']}/config.cache");
355
									write_config();
356
									add_base_packages_menu_items();
357
									convert_config();
358
									conf_mount_ro();
359
								}
360
								filter_configure();
361
								$savemsg = gettext("The configuration area has been restored.  You may need to reboot the firewall.");
362
							}
363
						}
364
					} else {
365
						if (!stristr($data, "<" . $g['xml_rootobj'] . ">")) {
366
							$input_errors[] = sprintf(gettext("You have selected to restore the full configuration but we could not locate a %s tag."), $g['xml_rootobj']);
367
						} else {
368
							/* restore the entire configuration */
369
							file_put_contents($_FILES['conffile']['tmp_name'], $data);
370
							if (config_install($_FILES['conffile']['tmp_name']) == 0) {
371
								/* this will be picked up by /index.php */
372
								conf_mount_rw();
373
								mark_subsystem_dirty("restore");
374
								touch("/conf/needs_package_sync");
375
								/* remove cache, we will force a config reboot */
376
								if (file_exists("{$g['tmp_path']}/config.cache")) {
377
									unlink("{$g['tmp_path']}/config.cache");
378
								}
379
								$config = parse_config(true);
380
								if (file_exists("/boot/loader.conf")) {
381
									$loaderconf = file_get_contents("/boot/loader.conf");
382
									if (strpos($loaderconf, "console=\"comconsole")) {
383
										$config['system']['enableserial'] = true;
384
										write_config("Restore serial console enabling in configuration.");
385
									}
386
									unset($loaderconf);
387
								}
388
								/* extract out rrd items, unset from $config when done */
389
								if ($config['rrddata']) {
390
									restore_rrddata();
391
									unset($config['rrddata']);
392
									unlink_if_exists("{$g['tmp_path']}/config.cache");
393
									write_config();
394
									add_base_packages_menu_items();
395
									convert_config();
396
									conf_mount_ro();
397
								}
398
								if ($m0n0wall_upgrade == true) {
399
									if ($config['system']['gateway'] <> "") {
400
										$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
401
									}
402
									unset($config['shaper']);
403
									/* optional if list */
404
									$ifdescrs = get_configured_interface_list(true, true);
405
									/* remove special characters from interface descriptions */
406
									if (is_array($ifdescrs)) {
407
										foreach ($ifdescrs as $iface) {
408
											$config['interfaces'][$iface]['descr'] = remove_bad_chars($config['interfaces'][$iface]['descr']);
409
										}
410
									}
411
									/* check for interface names with an alias */
412
									if (is_array($ifdescrs)) {
413
										foreach ($ifdescrs as $iface) {
414
											if (is_alias($config['interfaces'][$iface]['descr'])) {
415
												// Firewall rules
416
												$origname = $config['interfaces'][$iface]['descr'];
417
												$newname = $config['interfaces'][$iface]['descr'] . "Alias";
418
												update_alias_names_upon_change(array('filter', 'rule'), array('source', 'address'), $newname, $origname);
419
												update_alias_names_upon_change(array('filter', 'rule'), array('destination', 'address'), $newname, $origname);
420
												// NAT Rules
421
												update_alias_names_upon_change(array('nat', 'rule'), array('source', 'address'), $newname, $origname);
422
												update_alias_names_upon_change(array('nat', 'rule'), array('destination', 'address'), $newname, $origname);
423
												update_alias_names_upon_change(array('nat', 'rule'), array('target'), $newname, $origname);
424
												// Alias in an alias
425
												update_alias_names_upon_change(array('aliases', 'alias'), array('address'), $newname, $origname);
426
											}
427
										}
428
									}
429
									unlink_if_exists("{$g['tmp_path']}/config.cache");
430
									// Reset configuration version to something low
431
									// in order to force the config upgrade code to
432
									// run through with all steps that are required.
433
									$config['system']['version'] = "1.0";
434
									// Deal with descriptions longer than 63 characters
435
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
436
										if (count($config['filter']['rule'][$i]['descr']) > 63) {
437
											$config['filter']['rule'][$i]['descr'] = substr($config['filter']['rule'][$i]['descr'], 0, 63);
438
										}
439
									}
440
									// Move interface from ipsec to enc0
441
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
442
										if ($config['filter']['rule'][$i]['interface'] == "ipsec") {
443
											$config['filter']['rule'][$i]['interface'] = "enc0";
444
										}
445
									}
446
									// Convert icmp types
447
									// http://www.openbsd.org/cgi-bin/man.cgi?query=icmp&sektion=4&arch=i386&apropos=0&manpath=OpenBSD+Current
448
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
449
										if ($config["filter"]["rule"][$i]['icmptype']) {
450
											switch ($config["filter"]["rule"][$i]['icmptype']) {
451
												case "echo":
452
													$config["filter"]["rule"][$i]['icmptype'] = "echoreq";
453
													break;
454
												case "unreach":
455
													$config["filter"]["rule"][$i]['icmptype'] = "unreach";
456
													break;
457
												case "echorep":
458
													$config["filter"]["rule"][$i]['icmptype'] = "echorep";
459
													break;
460
												case "squench":
461
													$config["filter"]["rule"][$i]['icmptype'] = "squench";
462
													break;
463
												case "redir":
464
													$config["filter"]["rule"][$i]['icmptype'] = "redir";
465
													break;
466
												case "timex":
467
													$config["filter"]["rule"][$i]['icmptype'] = "timex";
468
													break;
469
												case "paramprob":
470
													$config["filter"]["rule"][$i]['icmptype'] = "paramprob";
471
													break;
472
												case "timest":
473
													$config["filter"]["rule"][$i]['icmptype'] = "timereq";
474
													break;
475
												case "timestrep":
476
													$config["filter"]["rule"][$i]['icmptype'] = "timerep";
477
													break;
478
												case "inforeq":
479
													$config["filter"]["rule"][$i]['icmptype'] = "inforeq";
480
													break;
481
												case "inforep":
482
													$config["filter"]["rule"][$i]['icmptype'] = "inforep";
483
													break;
484
												case "maskreq":
485
													$config["filter"]["rule"][$i]['icmptype'] = "maskreq";
486
													break;
487
												case "maskrep":
488
													$config["filter"]["rule"][$i]['icmptype'] = "maskrep";
489
													break;
490
											}
491
										}
492
									}
493
									$config['diag']['ipv6nat'] = true;
494
									write_config();
495
									add_base_packages_menu_items();
496
									convert_config();
497
									conf_mount_ro();
498
									$savemsg = gettext("The m0n0wall configuration has been restored and upgraded to pfSense.");
499
									mark_subsystem_dirty("restore");
500
								}
501
								if (is_array($config['captiveportal'])) {
502
									foreach ($config['captiveportal'] as $cp) {
503
										if (isset($cp['enable'])) {
504
											/* for some reason ipfw doesn't init correctly except on bootup sequence */
505
											mark_subsystem_dirty("restore");
506
											break;
507
										}
508
									}
509
								}
510
								setup_serial_port();
511
								if (is_interface_mismatch() == true) {
512
									touch("/var/run/interface_mismatch_reboot_needed");
513
									clear_subsystem_dirty("restore");
514
									convert_config();
515
									header("Location: interfaces_assign.php");
516
									exit;
517
								}
518
								if (is_interface_vlan_mismatch() == true) {
519
									touch("/var/run/interface_mismatch_reboot_needed");
520
									clear_subsystem_dirty("restore");
521
									convert_config();
522
									header("Location: interfaces_assign.php");
523
									exit;
524
								}
525
							} else {
526
								$input_errors[] = gettext("The configuration could not be restored.");
527
							}
528
						}
529
					}
530
				} else {
531
					$input_errors[] = gettext("The configuration could not be restored (file upload error).");
532
				}
533
			}
534
		}
535

    
536
		if ($mode == "reinstallpackages") {
537
			header("Location: pkg_mgr_install.php?mode=reinstallall");
538
			exit;
539
		} else if ($mode == "clearpackagelock") {
540
			clear_subsystem_dirty('packagelock');
541
			$savemsg = "Package Lock Cleared";
542
		} else if ($mode == "restore_ver") {
543
			$input_errors[] = gettext("XXX - this feature may hose your config (do NOT backrev configs!) - billm");
544
			if ($ver2restore <> "") {
545
				$conf_file = "{$g['cf_conf_path']}/bak/config-" . strtotime($ver2restore) . ".xml";
546
				if (config_install($conf_file) == 0) {
547
					mark_subsystem_dirty("restore");
548
				} else {
549
					$input_errors[] = gettext("The configuration could not be restored.");
550
				}
551
			} else {
552
				$input_errors[] = gettext("No version selected.");
553
			}
554
		}
555
	}
556
}
557

    
558
$id = rand() . '.' . time();
559

    
560
$mth = ini_get('upload_progress_meter.store_method');
561
$dir = ini_get('upload_progress_meter.file.filename_template');
562

    
563
function build_area_list($showall) {
564
	global $config;
565

    
566
	$areas = array(
567
		"aliases" => gettext("Aliases"),
568
		"captiveportal" => gettext("Captive Portal"),
569
		"voucher" => gettext("Captive Portal Vouchers"),
570
		"dnsmasq" => gettext("DNS Forwarder"),
571
		"unbound" => gettext("DNS Resolver"),
572
		"dhcpd" => gettext("DHCP Server"),
573
		"dhcpdv6" => gettext("DHCPv6 Server"),
574
		"filter" => gettext("Firewall Rules"),
575
		"interfaces" => gettext("Interfaces"),
576
		"ipsec" => gettext("IPSEC"),
577
		"nat" => gettext("NAT"),
578
		"openvpn" => gettext("OpenVPN"),
579
		"installedpackages" => gettext("Package Manager"),
580
		"rrddata" => gettext("RRD Data"),
581
		"cron" => gettext("Scheduled Tasks"),
582
		"syslog" => gettext("Syslog"),
583
		"system" => gettext("System"),
584
		"staticroutes" => gettext("Static routes"),
585
		"sysctl" => gettext("System tunables"),
586
		"snmpd" => gettext("SNMP Server"),
587
		"shaper" => gettext("Traffic Shaper"),
588
		"vlans" => gettext("VLANS"),
589
		"wol" => gettext("Wake on LAN")
590
		);
591

    
592
	$list = array("" => gettext("All"));
593

    
594
	if ($showall) {
595
		return($list + $areas);
596
	} else {
597
		foreach ($areas as $area => $areaname) {
598
			if ($area === "rrddata" || check_and_returnif_section_exists($area) == true) {
599
				$list[$area] = $areaname;
600
			}
601
		}
602

    
603
		return($list);
604
	}
605
}
606

    
607
$pgtitle = array(gettext("Diagnostics"), gettext("Backup/restore"));
608
include("head.inc");
609

    
610
if ($input_errors)
611
	print_input_errors($input_errors);
612

    
613
if ($savemsg)
614
	print_info_box($savemsg, 'success');
615

    
616
if (is_subsystem_dirty('restore')):
617
?>
618
	<br/>
619
	<form action="reboot.php" method="post">
620
		<input name="Submit" type="hidden" value="Yes" />
621
		<?=print_info_box(gettext("The firewall configuration has been changed.") . "<br />" . gettext("The firewall is now rebooting."))?>
622
		<br />
623
	</form>
624
<?php
625
endif;
626

    
627
$tab_array = array();
628
$tab_array[] = array(gettext("Config History"), false, "diag_confbak.php");
629
$tab_array[] = array(gettext("Backup/Restore"), true, "diag_backup.php");
630
display_top_tabs($tab_array);
631

    
632
require_once('classes/Form.class.php');
633

    
634
$form = new Form(false);
635
$form->setMultipartEncoding();	// Allow file uploads
636

    
637
$section = new Form_Section('Backup configuration');
638

    
639
$section->addInput(new Form_Select(
640
	'backuparea',
641
	'Backup area',
642
	'',
643
	build_area_list(false)
644
));
645

    
646
$section->addInput(new Form_Checkbox(
647
	'nopackages',
648
	'Skip packages',
649
	'Do not backup package information.',
650
	false
651
));
652

    
653
$section->addInput(new Form_Checkbox(
654
	'donotbackuprrd',
655
	'Skip RRD data',
656
	'Do not backup RRD data (NOTE: RRD Data can consume 4+ megabytes of config.xml space!)',
657
	true
658
));
659

    
660
$section->addInput(new Form_Checkbox(
661
	'encrypt',
662
	'Encryption',
663
	'Encrypt this configuration file.',
664
	false
665
));
666

    
667
$section->addInput(new Form_Input(
668
	'encrypt_password',
669
	null,
670
	'password',
671
	null,
672
	['placeholder' => 'Password']
673
));
674

    
675
$section->addInput(new Form_Input(
676
	'encrypt_passconf',
677
	null,
678
	'password',
679
	null,
680
	['placeholder' => 'Confirm password']
681
));
682

    
683
$group = new Form_Group('');
684
$group->add(new Form_Button(
685
	'Submit',
686
	'Download configuration as XML'
687
));
688

    
689
$section->add($group);
690
$form->add($section);
691

    
692
$section = new Form_Section('Restore backup');
693

    
694
$section->addInput(new Form_StaticText(
695
	null,
696
	gettext("Open a ") . $g['[product_name'] . gettext(" configuration XML file and click the button below to restore the configuration.")
697
));
698

    
699
$section->addInput(new Form_Select(
700
	'restorearea',
701
	'Restore area',
702
	'',
703
	build_area_list(false)
704
));
705

    
706
$section->addInput(new Form_Input(
707
	'conffile',
708
	'Configuration file',
709
	'file',
710
	null
711
));
712

    
713
$section->addInput(new Form_Checkbox(
714
	'decrypt',
715
	'Encryption',
716
	'Configuration file is encrypted.',
717
	false
718
));
719

    
720
$section->addInput(new Form_Input(
721
	'decrypt_password',
722
	null,
723
	'password',
724
	null,
725
	['placeholder' => 'Password']
726
));
727

    
728
$section->addInput(new Form_Input(
729
	'decrypt_passconf',
730
	null,
731
	'password',
732
	null,
733
	['placeholder' => 'Confirm password']
734
));
735

    
736
$group = new Form_Group('');
737
$group->add(new Form_Button(
738
	'Submit',
739
	'Restore configuration'
740
))->setHelp('The firewall will reboot after restoring the configuration.')->removeClass('btn-primary')->addClass('btn-danger');
741

    
742
$section->add($group);
743

    
744
$form->add($section);
745

    
746
if (($config['installedpackages']['package'] != "") || (is_subsystem_dirty("packagelock"))) {
747
	$section = new Form_Section('Package functions');
748

    
749
	if ($config['installedpackages']['package'] != "") {
750
		$group = new Form_Group('');
751
		$group->add(new Form_Button(
752
			'Submit',
753
			'Reinstall packages'
754
		))->setHelp('Click this button to reinstall all system packages.  This may take a while.')->removeClass('btn-primary')->addClass('btn-warning');
755

    
756
		$section->add($group);
757
	}
758

    
759
	if (is_subsystem_dirty("packagelock")) {
760
		$group = new Form_Group('');
761
		$group->add(new Form_Button(
762
			'Submit',
763
			'Clear Package Lock'
764
		))->setHelp('Click this button to clear the package lock if a package fails to reinstall properly after an upgrade.')->removeClass('btn-primary')->addClass('btn-warning');
765

    
766
		$section->add($group);
767
	}
768

    
769
	$form->add($section);
770
}
771

    
772
print($form);
773
?>
774
<script type="text/javascript">
775
//<![CDATA[
776
events.push(function(){
777

    
778
	// ------- Show/hide sections based on checkbox settings --------------------------------------
779
	
780
	function hideSections(hide) {
781
		hidePasswords();
782
	}
783

    
784
	function hidePasswords() {
785

    
786
		encryptHide = !($('input[name="encrypt"]').is(':checked'));
787
		decryptHide = !($('input[name="decrypt"]').is(':checked'));
788

    
789
		hideInput('encrypt_password', encryptHide);
790
		hideInput('encrypt_passconf', encryptHide);
791
		hideInput('decrypt_password', decryptHide);
792
		hideInput('decrypt_passconf', decryptHide);
793
	}
794

    
795
	// ---------- Click checkbox handlers ---------------------------------------------------------
796

    
797
	$('input[name="encrypt"]').on('change', function() {
798
		hidePasswords();
799
	});
800

    
801
	$('input[name="decrypt"]').on('change', function() {
802
		hidePasswords();
803
	});
804

    
805
	// ---------- On initial page load ------------------------------------------------------------
806

    
807
	hideSections();
808
});
809
//]]>
810
</script>
811

    
812
<?php
813
include("foot.inc");
814

    
815
if (is_subsystem_dirty('restore')) {
816
	system_reboot();
817
}
(8-8/233)