Project

General

Profile

Download (26.1 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
/*
60
	pfSense_BUILDER_BINARIES:	/sbin/shutdown
61
	pfSense_MODULE: backup
62
*/
63

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

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

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

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

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

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

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

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

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

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

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

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

    
242
			if (!$input_errors) {
243

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

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

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

    
277
				//unlock($lockbckp);
278

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

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

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

    
306
				exit;
307
			}
308
		}
309

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
779
	// ------- Show/hide sections based on checkbox settings --------------------------------------
780

    
781
	function hideSections(hide) {
782
		hidePasswords();
783
	}
784

    
785
	function hidePasswords() {
786

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

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

    
796
	// ---------- Click checkbox handlers ---------------------------------------------------------
797

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

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

    
806
	// ---------- On initial page load ------------------------------------------------------------
807

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

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

    
816
if (is_subsystem_dirty('restore')) {
817
	system_reboot();
818
}
(8-8/234)