Project

General

Profile

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

    
59
##|+PRIV
60
##|*IDENT=page-diagnostics-backup/restore
61
##|*NAME=Diagnostics: Backup/restore
62
##|*DESCR=Allow access to the 'Diagnostics: Backup/restore' page.
63
##|*MATCH=diag_backup.php*
64
##|-PRIV
65

    
66
/* Allow additional execution time 0 = no limit. */
67
ini_set('max_execution_time', '0');
68
ini_set('max_input_time', '0');
69

    
70
/* omit no-cache headers because it confuses IE with file downloads */
71
$omit_nocacheheaders = true;
72
if (stristr($_POST['Submit'], gettext("Download"))) {
73
	$nocsrf = true;
74
}
75
require("guiconfig.inc");
76
require_once("functions.inc");
77
require_once("filter.inc");
78
require_once("shaper.inc");
79

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

    
83
function rrd_data_xml() {
84
	global $rrddbpath;
85
	global $rrdtool;
86

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

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

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

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

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

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

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

    
239
			if (!$input_errors) {
240

    
241
				//$lockbckp = lock('config');
242

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

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

    
274
				//unlock($lockbckp);
275

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

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

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

    
303
				exit;
304
			}
305
		}
306

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

    
317
			if (!$input_errors) {
318
				if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
319

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

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

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

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

    
556
$id = rand() . '.' . time();
557

    
558
$mth = ini_get('upload_progress_meter.store_method');
559
$dir = ini_get('upload_progress_meter.file.filename_template');
560

    
561
function build_area_list($showall) {
562
	global $config;
563

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

    
590
	$list = array("" => gettext("All"));
591

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

    
601
		return($list);
602
	}
603
}
604

    
605
$pgtitle = array(gettext("Diagnostics"), gettext("Backup/Restore"));
606
include("head.inc");
607

    
608
if ($input_errors) {
609
	print_input_errors($input_errors);
610
}
611

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

    
616
if (is_subsystem_dirty('restore')):
617
?>
618
	<br/>
619
	<form action="diag_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
$form = new Form(false);
633
$form->setMultipartEncoding();	// Allow file uploads
634

    
635
$section = new Form_Section('Backup configuration');
636

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

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

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

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

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

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

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

    
687
$section->add($group);
688
$form->add($section);
689

    
690
$section = new Form_Section('Restore backup');
691

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

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

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

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

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

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

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

    
740
$section->add($group);
741

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

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

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

    
754
		$section->add($group);
755
	}
756

    
757
	if (is_subsystem_dirty("packagelock")) {
758
		$group = new Form_Group('');
759
		$group->add(new Form_Button(
760
			'Submit',
761
			'Clear Package Lock'
762
		))->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');
763

    
764
		$section->add($group);
765
	}
766

    
767
	$form->add($section);
768
}
769

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

    
776
	// ------- Show/hide sections based on checkbox settings --------------------------------------
777

    
778
	function hideSections(hide) {
779
		hidePasswords();
780
	}
781

    
782
	function hidePasswords() {
783

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

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

    
793
	// ---------- Click handlers ------------------------------------------------------------------
794

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

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

    
803
	$('#conffile').change(function () {
804
		if (document.getElementById("conffile").value) {
805
			$('.restore').prop('disabled', false);
806
		} else {
807
			$('.restore').prop('disabled', true);
808
		}
809
    });
810
	// ---------- On initial page load ------------------------------------------------------------
811

    
812
	hideSections();
813
	$('.restore').prop('disabled', true);
814
});
815
//]]>
816
</script>
817

    
818
<?php
819
include("foot.inc");
820

    
821
if (is_subsystem_dirty('restore')) {
822
	system_reboot();
823
}
(7-7/228)