Project

General

Profile

Download (26.2 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
require("guiconfig.inc");
73
require_once("functions.inc");
74
require_once("filter.inc");
75
require_once("shaper.inc");
76

    
77
$rrddbpath = "/var/db/rrd";
78
$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
79

    
80
function rrd_data_xml() {
81
	global $rrddbpath;
82
	global $rrdtool;
83

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

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

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

    
185
function remove_bad_chars($string) {
186
	return preg_replace('/[^a-z_0-9]/i', '', $string);
187
}
188

    
189
function check_and_returnif_section_exists($section) {
190
	global $config;
191
	if (is_array($config[$section])) {
192
		return true;
193
	}
194
	return false;
195
}
196

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

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

    
236
			if (!$input_errors) {
237

    
238
				//$lockbckp = lock('config');
239

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

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

    
271
				//unlock($lockbckp);
272

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

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

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

    
300
				exit;
301
			}
302
		}
303

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

    
314
			if (!$input_errors) {
315
				if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
316

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

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

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

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

    
553
$id = rand() . '.' . time();
554

    
555
$mth = ini_get('upload_progress_meter.store_method');
556
$dir = ini_get('upload_progress_meter.file.filename_template');
557

    
558
function build_area_list($showall) {
559
	global $config;
560

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

    
587
	$list = array("" => gettext("All"));
588

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

    
598
		return($list);
599
	}
600
}
601

    
602
$pgtitle = array(gettext("Diagnostics"), gettext("Backup/Restore"));
603
include("head.inc");
604

    
605
if ($input_errors) {
606
	print_input_errors($input_errors);
607
}
608

    
609
if ($savemsg) {
610
	print_info_box($savemsg, 'success');
611
}
612

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

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

    
629
$form = new Form(false);
630
$form->setMultipartEncoding();	// Allow file uploads
631

    
632
$section = new Form_Section('Backup configuration');
633

    
634
$section->addInput(new Form_Select(
635
	'backuparea',
636
	'Backup area',
637
	'',
638
	build_area_list(false)
639
));
640

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

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

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

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

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

    
678
$group = new Form_Group('');
679
$group->add(new Form_Button(
680
	'Submit',
681
	'Download configuration as XML'
682
));
683

    
684
$section->add($group);
685
$form->add($section);
686

    
687
$section = new Form_Section('Restore backup');
688

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

    
694
$section->addInput(new Form_Select(
695
	'restorearea',
696
	'Restore area',
697
	'',
698
	build_area_list(false)
699
));
700

    
701
$section->addInput(new Form_Input(
702
	'conffile',
703
	'Configuration file',
704
	'file',
705
	null
706
));
707

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

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

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

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

    
737
$section->add($group);
738

    
739
$form->add($section);
740

    
741
if (($config['installedpackages']['package'] != "") || (is_subsystem_dirty("packagelock"))) {
742
	$section = new Form_Section('Package functions');
743

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

    
751
		$section->add($group);
752
	}
753

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

    
761
		$section->add($group);
762
	}
763

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

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

    
773
	// ------- Show/hide sections based on checkbox settings --------------------------------------
774

    
775
	function hideSections(hide) {
776
		hidePasswords();
777
	}
778

    
779
	function hidePasswords() {
780

    
781
		encryptHide = !($('input[name="encrypt"]').is(':checked'));
782
		decryptHide = !($('input[name="decrypt"]').is(':checked'));
783

    
784
		hideInput('encrypt_password', encryptHide);
785
		hideInput('encrypt_passconf', encryptHide);
786
		hideInput('decrypt_password', decryptHide);
787
		hideInput('decrypt_passconf', decryptHide);
788
	}
789

    
790
	// ---------- Click handlers ------------------------------------------------------------------
791

    
792
	$('input[name="encrypt"]').on('change', function() {
793
		hidePasswords();
794
	});
795

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

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

    
809
	hideSections();
810
	$('.restore').prop('disabled', true);
811
});
812
//]]>
813
</script>
814

    
815
<?php
816
include("foot.inc");
817

    
818
if (is_subsystem_dirty('restore')) {
819
	system_reboot();
820
}
(7-7/228)