Project

General

Profile

Download (24.6 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_backup.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
##|+PRIV
29
##|*IDENT=page-diagnostics-backup-restore
30
##|*NAME=Diagnostics: Backup & Restore
31
##|*DESCR=Allow access to the 'Diagnostics: Backup & Restore' page.
32
##|*WARN=standard-warning-root
33
##|*MATCH=diag_backup.php*
34
##|-PRIV
35

    
36
/* Allow additional execution time 0 = no limit. */
37
ini_set('max_execution_time', '0');
38
ini_set('max_input_time', '0');
39

    
40
/* omit no-cache headers because it confuses IE with file downloads */
41
$omit_nocacheheaders = true;
42
require_once("guiconfig.inc");
43
require_once("functions.inc");
44
require_once("filter.inc");
45
require_once("shaper.inc");
46
require_once("pkg-utils.inc");
47

    
48
$rrddbpath = "/var/db/rrd";
49
$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
50

    
51
function rrd_data_xml() {
52
	global $rrddbpath;
53
	global $rrdtool;
54

    
55
	$result = "\t<rrddata>\n";
56
	$rrd_files = glob("{$rrddbpath}/*.rrd");
57
	$xml_files = array();
58
	foreach ($rrd_files as $rrd_file) {
59
		$basename = basename($rrd_file);
60
		$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
61
		exec("$rrdtool dump '{$rrd_file}' '{$xml_file}'");
62
		$xml_data = file_get_contents($xml_file);
63
		unlink($xml_file);
64
		if ($xml_data !== false) {
65
			$result .= "\t\t<rrddatafile>\n";
66
			$result .= "\t\t\t<filename>{$basename}</filename>\n";
67
			$result .= "\t\t\t<xmldata>" . base64_encode(gzdeflate($xml_data)) . "</xmldata>\n";
68
			$result .= "\t\t</rrddatafile>\n";
69
		}
70
	}
71
	$result .= "\t</rrddata>\n";
72
	return $result;
73
}
74

    
75
function restore_rrddata() {
76
	global $config, $g, $rrdtool, $input_errors;
77
	foreach ($config['rrddata']['rrddatafile'] as $rrd) {
78
		if ($rrd['xmldata']) {
79
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
80
			$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
81
			if (file_put_contents($xml_file, gzinflate(base64_decode($rrd['xmldata']))) === false) {
82
				log_error(sprintf(gettext("Cannot write %s"), $xml_file));
83
				continue;
84
			}
85
			$output = array();
86
			$status = null;
87
			exec("$rrdtool restore -f '{$xml_file}' '{$rrd_file}'", $output, $status);
88
			if ($status) {
89
				log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
90
				continue;
91
			}
92
			unlink($xml_file);
93
		} else if ($rrd['data']) {
94
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
95
			$rrd_fd = fopen($rrd_file, "w");
96
			if (!$rrd_fd) {
97
				log_error(sprintf(gettext("Cannot write %s"), $rrd_file));
98
				continue;
99
			}
100
			$data = base64_decode($rrd['data']);
101
			/* Try to decompress the data. */
102
			$dcomp = @gzinflate($data);
103
			if ($dcomp) {
104
				/* If the decompression worked, write the decompressed data */
105
				if (fwrite($rrd_fd, $dcomp) === false) {
106
					log_error(sprintf(gettext("fwrite %s failed"), $rrd_file));
107
					continue;
108
				}
109
			} else {
110
				/* If the decompression failed, it wasn't compressed, so write raw data */
111
				if (fwrite($rrd_fd, $data) === false) {
112
					log_error(sprintf(gettext("fwrite %s failed"), $rrd_file));
113
					continue;
114
				}
115
			}
116
			if (fclose($rrd_fd) === false) {
117
				log_error(sprintf(gettext("fclose %s failed"), $rrd_file));
118
				continue;
119
			}
120
		}
121
	}
122
}
123

    
124
function voucher_data_xml($tab=false) {
125
	global $g;
126

    
127
	$voucher_files = glob("{$g['vardb_path']}/voucher_*.db");
128
	if (empty($voucher_files)) {
129
		return;
130
	}
131
	$t = ($tab) ? "\t" : "";
132
	$result = "\t<voucherdata>\n";
133
	foreach ($voucher_files as $voucher_file) {
134
		$basename = basename($voucher_file);
135
		$db_data = file_get_contents($voucher_file);
136
		if ($db_data !== false) {
137
			$result .= "{$t}\t\t<voucherdbfile>\n";
138
			$result .= "{$t}\t\t\t<filename>{$basename}</filename>\n";
139
			$result .= "{$t}\t\t\t<dbdata>" . base64_encode(gzdeflate($db_data)) . "</dbdata>\n";
140
			$result .= "{$t}\t\t</voucherdbfile>\n";
141
		}
142
	}
143
	$result .= "{$t}\t</voucherdata>\n";
144

    
145
	return $result;
146
}
147

    
148
function restore_voucher_data() {
149
	global $config, $g;
150
	foreach ($config['voucher']['voucherdata']['voucherdbfile'] as $db) {
151
		$voucherdb_file = "{$g['vardb_path']}/{$db['filename']}";
152
		if (file_put_contents($voucherdb_file, gzinflate(base64_decode($db['dbdata']))) === false) {
153
			log_error(sprintf(gettext("Cannot write %s"), $voucherdb_file));
154
			continue;
155
		}
156
	}
157
}
158

    
159
function remove_bad_chars($string) {
160
	return preg_replace('/[^a-z_0-9]/i', '', $string);
161
}
162

    
163
function check_and_returnif_section_exists($section) {
164
	global $config;
165
	if (is_array($config[$section])) {
166
		return true;
167
	}
168
	return false;
169
}
170

    
171
if ($_POST['apply']) {
172
	ob_flush();
173
	flush();
174
	clear_subsystem_dirty("restore");
175
	exit;
176
}
177

    
178
if ($_POST) {
179
	unset($input_errors);
180
	if ($_POST['restore']) {
181
		$mode = "restore";
182
	} else if ($_POST['reinstallpackages']) {
183
		$mode = "reinstallpackages";
184
	} else if ($_POST['clearpackagelock']) {
185
		$mode = "clearpackagelock";
186
	} else if ($_POST['download']) {
187
		$mode = "download";
188
	}
189
	if ($_POST["nopackages"] <> "") {
190
		$options = "nopackages";
191
	}
192
	if ($mode) {
193
		if ($mode == "download") {
194
			if ($_POST['encrypt']) {
195
				if (!$_POST['encrypt_password'] || ($_POST['encrypt_password'] != $_POST['encrypt_password_confirm'])) {
196
					$input_errors[] = gettext("Supplied password and confirmation do not match.");
197
				}
198
			}
199

    
200
			if (!$input_errors) {
201

    
202
				//$lockbckp = lock('config');
203

    
204
				$host = "{$config['system']['hostname']}.{$config['system']['domain']}";
205
				$name = "config-{$host}-".date("YmdHis").".xml";
206
				$data = "";
207

    
208
				if ($options == "nopackages") {
209
					if (!$_POST['backuparea']) {
210
						/* backup entire configuration */
211
						$data = file_get_contents("{$g['conf_path']}/config.xml");
212
					} else {
213
						/* backup specific area of configuration */
214
						$data = backup_config_section($_POST['backuparea']);
215
						$name = "{$_POST['backuparea']}-{$name}";
216
					}
217
					$data = preg_replace('/\t*<installedpackages>.*<\/installedpackages>\n/sm', '', $data);
218
				} else {
219
					if (!$_POST['backuparea']) {
220
						/* backup entire configuration */
221
						$data = file_get_contents("{$g['conf_path']}/config.xml");
222
					} else if ($_POST['backuparea'] === "rrddata") {
223
						$data = rrd_data_xml();
224
						$name = "{$_POST['backuparea']}-{$name}";
225
					} else if ($_POST['backuparea'] === "voucher") {
226
						$data = backup_config_section($_POST['backuparea']);
227
						$voucher_data_xml = voucher_data_xml();
228
						$closing_tag = "</voucher>";
229
						$data = str_replace($closing_tag, $voucher_data_xml . $closing_tag, $data);
230
						$name = "{$_POST['backuparea']}-{$name}";
231
					} else {
232
						/* backup specific area of configuration */
233
						$data = backup_config_section($_POST['backuparea']);
234
						$name = "{$_POST['backuparea']}-{$name}";
235
					}
236
				}
237

    
238
				//unlock($lockbckp);
239

    
240
				/*
241
				 *	Backup RRD Data
242
				 */
243

    
244
				/* If the config on disk had rrddata tags already, remove that section first.
245
				 * See https://redmine.pfsense.org/issues/8994 and
246
				 *     https://redmine.pfsense.org/issues/10508 */
247
				$data = preg_replace("/[[:blank:]]*<rrddata>.*<\\/rrddata>[[:blank:]]*\n*/s", "", $data);
248
				$data = preg_replace("/[[:blank:]]*<rrddata\\/>[[:blank:]]*\n*/", "", $data);
249

    
250
				if (!$_POST['backuparea'] && !empty($config['voucher'])) {
251
					$voucher_data_xml = voucher_data_xml(true);
252
					$closing_tag = "</voucher>";
253
					$data = str_replace($closing_tag, $voucher_data_xml . $closing_tag, $data);
254
				}
255

    
256
				if ($_POST['backuparea'] !== "rrddata" && !$_POST['donotbackuprrd']) {
257
					$rrd_data_xml = rrd_data_xml();
258
					$closing_tag = "</" . $g['xml_rootobj'] . ">";
259
					$data = str_replace($closing_tag, $rrd_data_xml . $closing_tag, $data);
260
				}
261

    
262
				if ($_POST['encrypt']) {
263
					$data = encrypt_data($data, $_POST['encrypt_password']);
264
					tagfile_reformat($data, $data, "config.xml");
265
				}
266

    
267
				send_user_download('data', $data, $name);
268
			}
269
		}
270

    
271
		if ($mode == "restore") {
272
			if ($_POST['decrypt']) {
273
				if (!$_POST['decrypt_password']) {
274
					$input_errors[] = gettext("A password for decryption must be supplied and confirmed.");
275
				}
276
			}
277

    
278
			if (!$input_errors) {
279
				if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
280

    
281
					/* read the file contents */
282
					$data = file_get_contents($_FILES['conffile']['tmp_name']);
283
					if (!$data) {
284
						$input_errors[] = gettext("Warning, could not read file {$_FILES['conffile']['tmp_name']}");
285
					} elseif ($_POST['decrypt']) {
286
						if (!tagfile_deformat($data, $data, "config.xml")) {
287
							$input_errors[] = gettext("The uploaded file does not appear to contain an encrypted pfsense configuration.");
288
						} else {
289
							$data = decrypt_data($data, $_POST['decrypt_password']);
290
							if (empty($data)) {
291
								$input_errors[] = gettext("File decryption failed. Incorrect password or file is invalid.");
292
							}
293
						}
294
					}
295
					if (stristr($data, "<m0n0wall>")) {
296
						log_error(gettext("Upgrading m0n0wall configuration to pfsense."));
297
						/* m0n0wall was found in config.  convert it. */
298
						$data = str_replace("m0n0wall", "pfsense", $data);
299
						$m0n0wall_upgrade = true;
300
					}
301

    
302
					/* If the config on disk had empty rrddata tags, remove them to
303
					 * avoid an XML parsing error.
304
					 * See https://redmine.pfsense.org/issues/8994 */
305
					$data = preg_replace("/<rrddata><\\/rrddata>/", "", $data);
306
					$data = preg_replace("/<rrddata\\/>/", "", $data);
307

    
308
					if ($_POST['restorearea'] && !$input_errors) {
309
						/* restore a specific area of the configuration */
310
						if (!stristr($data, "<" . $_POST['restorearea'] . ">")) {
311
							$input_errors[] = gettext("An area to restore was selected but the correct xml tag could not be located.");
312
						} else {
313
							if (!restore_config_section($_POST['restorearea'], $data)) {
314
								$input_errors[] = gettext("An area to restore was selected but the correct xml tag could not be located.");
315
							} else {
316
								if ($config['rrddata'] || $config['voucher']['voucherdata']) {
317
									if ($config['rrddata']) {
318
										restore_rrddata();
319
										unset($config['rrddata']);
320
										write_config(sprintf(gettext("Unset RRD data from configuration after restoring %s configuration area"), $_POST['restorearea']));
321
									}
322
									if (($_POST['restorearea'] == 'voucher') && $config['voucher']['voucherdata']) {
323
										restore_voucher_data();
324
										unset($config['voucher']['voucherdata']);
325
										write_config(sprintf(gettext("Unset Voucher data from configuration after restoring %s configuration area"), $_POST['restorearea']));
326
									}
327
									unlink_if_exists("{$g['tmp_path']}/config.cache");
328
									convert_config();
329
								}
330
								filter_configure();
331
								$savemsg = gettext("The configuration area has been restored. The firewall may need to be rebooted.");
332
							}
333
						}
334
					} elseif (!$input_errors) {
335
						if (!stristr($data, "<" . $g['xml_rootobj'] . ">")) {
336
							$input_errors[] = sprintf(gettext("A full configuration restore was selected but a %s tag could not be located."), $g['xml_rootobj']);
337
						} else {
338
							/* restore the entire configuration */
339
							file_put_contents($_FILES['conffile']['tmp_name'], $data);
340
							if (config_install($_FILES['conffile']['tmp_name']) == 0) {
341
								/* Save current pkg repo to re-add on new config */
342
								unset($pkg_repo_conf_path);
343
								if (isset($config['system']['pkg_repo_conf_path'])) {
344
									$pkg_repo_conf_path = $config['system']['pkg_repo_conf_path'];
345
								}
346

    
347
								/* this will be picked up by /index.php */
348
								mark_subsystem_dirty("restore");
349
								touch("/conf/needs_package_sync");
350
								/* remove cache, we will force a config reboot */
351
								if (file_exists("{$g['tmp_path']}/config.cache")) {
352
									unlink("{$g['tmp_path']}/config.cache");
353
								}
354
								$config = parse_config(true);
355

    
356
								/* Restore previously pkg repo configured */
357
								$pkg_repo_restored = false;
358
								if (isset($pkg_repo_conf_path)) {
359
									$config['system']['pkg_repo_conf_path'] =
360
									    $pkg_repo_conf_path;
361
									$pkg_repo_restored = true;
362
								} elseif (isset($config['system']['pkg_repo_conf_path'])) {
363
									unset($config['system']['pkg_repo_conf_path']);
364
									$pkg_repo_restored = true;
365
								}
366

    
367
								if ($pkg_repo_restored) {
368
									write_config(gettext("Removing pkg repository set after restoring full configuration"));
369
									pkg_update(true);
370
								}
371

    
372
								if (file_exists("/boot/loader.conf")) {
373
									$loaderconf = file_get_contents("/boot/loader.conf");
374
									if (strpos($loaderconf, "console=\"comconsole") ||
375
									    strpos($loaderconf, "boot_serial=\"YES")) {
376
										$config['system']['enableserial'] = true;
377
										write_config(gettext("Restore serial console enabling in configuration."));
378
									}
379
									unset($loaderconf);
380
								}
381
								if (file_exists("/boot/loader.conf.local")) {
382
									$loaderconf = file_get_contents("/boot/loader.conf.local");
383
									if (strpos($loaderconf, "console=\"comconsole") ||
384
									    strpos($loaderconf, "boot_serial=\"YES")) {
385
										$config['system']['enableserial'] = true;
386
										write_config(gettext("Restore serial console enabling in configuration."));
387
									}
388
									unset($loaderconf);
389
								}
390
								/* extract out rrd items, unset from $config when done */
391
								if ($config['rrddata'] || $config['voucher']['voucherdata']) {
392
									if ($config['rrddata']) {
393
										restore_rrddata();
394
										unset($config['rrddata']);
395
									}
396
									if ($config['voucher']['voucherdata']) {
397
										restore_voucher_data();
398
										unset($config['voucher']['voucherdata']);
399
									}
400
									write_config(gettext("Unset RRD and Voucher data from configuration after full restore."));
401
									unlink_if_exists("{$g['tmp_path']}/config.cache");
402
									convert_config();
403
								}
404
								if ($m0n0wall_upgrade == true) {
405
									if ($config['system']['gateway'] <> "") {
406
										$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
407
									}
408
									unset($config['shaper']);
409
									/* optional if list */
410
									$ifdescrs = get_configured_interface_list(true);
411
									/* remove special characters from interface descriptions */
412
									if (is_array($ifdescrs)) {
413
										foreach ($ifdescrs as $iface) {
414
											$config['interfaces'][$iface]['descr'] = remove_bad_chars($config['interfaces'][$iface]['descr']);
415
										}
416
									}
417
									/* check for interface names with an alias */
418
									if (is_array($ifdescrs)) {
419
										foreach ($ifdescrs as $iface) {
420
											if (is_alias($config['interfaces'][$iface]['descr'])) {
421
												$origname = $config['interfaces'][$iface]['descr'];
422
												update_alias_name($origname . "Alias", $origname);
423
											}
424
										}
425
									}
426
									unlink_if_exists("{$g['tmp_path']}/config.cache");
427
									// Reset configuration version to something low
428
									// in order to force the config upgrade code to
429
									// run through with all steps that are required.
430
									$config['system']['version'] = "1.0";
431
									// Deal with descriptions longer than 63 characters
432
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
433
										if (count($config['filter']['rule'][$i]['descr']) > 63) {
434
											$config['filter']['rule'][$i]['descr'] = substr($config['filter']['rule'][$i]['descr'], 0, 63);
435
										}
436
									}
437
									// Move interface from ipsec to enc0
438
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
439
										if ($config['filter']['rule'][$i]['interface'] == "ipsec") {
440
											$config['filter']['rule'][$i]['interface'] = "enc0";
441
										}
442
									}
443
									// Convert icmp types
444
									// http://www.openbsd.org/cgi-bin/man.cgi?query=icmp&sektion=4&arch=i386&apropos=0&manpath=OpenBSD+Current
445
									$convert = array('echo' => 'echoreq', 'timest' => 'timereq', 'timestrep' => 'timerep');
446
									foreach ($config["filter"]["rule"] as $ruleid => &$ruledata) {
447
										if ($convert[$ruledata['icmptype']]) {
448
											$ruledata['icmptype'] = $convert[$ruledata['icmptype']];
449
										}
450
									}
451
									$config['diag']['ipv6nat'] = true;
452
									write_config(gettext("Imported m0n0wall configuration"));
453
									convert_config();
454
									$savemsg = gettext("The m0n0wall configuration has been restored and upgraded to pfSense.");
455
									mark_subsystem_dirty("restore");
456
								}
457
								if (is_array($config['captiveportal'])) {
458
									foreach ($config['captiveportal'] as $cp) {
459
										if (isset($cp['enable'])) {
460
											/* for some reason ipfw doesn't init correctly except on bootup sequence */
461
											mark_subsystem_dirty("restore");
462
											break;
463
										}
464
									}
465
								}
466
								console_configure();
467
								if (is_interface_mismatch() == true) {
468
									touch("/var/run/interface_mismatch_reboot_needed");
469
									clear_subsystem_dirty("restore");
470
									convert_config();
471
									header("Location: interfaces_assign.php");
472
									exit;
473
								}
474
								if (is_interface_vlan_mismatch() == true) {
475
									touch("/var/run/interface_mismatch_reboot_needed");
476
									clear_subsystem_dirty("restore");
477
									convert_config();
478
									header("Location: interfaces_assign.php");
479
									exit;
480
								}
481
							} else {
482
								$input_errors[] = gettext("The configuration could not be restored.");
483
							}
484
						}
485
					}
486
				} else {
487
					$input_errors[] = gettext("The configuration could not be restored (file upload error).");
488
				}
489
			}
490
		}
491

    
492
		if ($mode == "reinstallpackages") {
493
			header("Location: pkg_mgr_install.php?mode=reinstallall");
494
			exit;
495
		} else if ($mode == "clearpackagelock") {
496
			clear_subsystem_dirty('packagelock');
497
			$savemsg = "Package lock cleared.";
498
		}
499
	}
500
}
501

    
502
$id = rand() . '.' . time();
503

    
504
$mth = ini_get('upload_progress_meter.store_method');
505
$dir = ini_get('upload_progress_meter.file.filename_template');
506

    
507
function build_area_list($showall) {
508
	global $config;
509

    
510
	$areas = array(
511
		"aliases" => gettext("Aliases"),
512
		"captiveportal" => gettext("Captive Portal"),
513
		"voucher" => gettext("Captive Portal Vouchers"),
514
		"dnsmasq" => gettext("DNS Forwarder"),
515
		"unbound" => gettext("DNS Resolver"),
516
		"dhcpd" => gettext("DHCP Server"),
517
		"dhcpdv6" => gettext("DHCPv6 Server"),
518
		"dyndnses" => gettext("Dynamic DNS"),
519
		"filter" => gettext("Firewall Rules"),
520
		"interfaces" => gettext("Interfaces"),
521
		"ipsec" => gettext("IPSEC"),
522
		"dnshaper" => gettext("Limiters"),
523
		"nat" => gettext("NAT"),
524
		"openvpn" => gettext("OpenVPN"),
525
		"installedpackages" => gettext("Package Manager"),
526
		"rrddata" => gettext("RRD Data"),
527
		"cron" => gettext("Scheduled Tasks"),
528
		"syslog" => gettext("Syslog"),
529
		"system" => gettext("System"),
530
		"staticroutes" => gettext("Static routes"),
531
		"sysctl" => gettext("System tunables"),
532
		"snmpd" => gettext("SNMP Server"),
533
		"shaper" => gettext("Traffic Shaper"),
534
		"vlans" => gettext("VLANS"),
535
		"wol" => gettext("Wake-on-LAN")
536
		);
537

    
538
	$list = array("" => gettext("All"));
539

    
540
	if ($showall) {
541
		return($list + $areas);
542
	} else {
543
		foreach ($areas as $area => $areaname) {
544
			if ($area === "rrddata" || check_and_returnif_section_exists($area) == true) {
545
				$list[$area] = $areaname;
546
			}
547
		}
548

    
549
		return($list);
550
	}
551
}
552

    
553
$pgtitle = array(gettext("Diagnostics"), htmlspecialchars(gettext("Backup & Restore")), htmlspecialchars(gettext("Backup & Restore")));
554
$pglinks = array("", "@self", "@self");
555
include("head.inc");
556

    
557
if ($input_errors) {
558
	print_input_errors($input_errors);
559
}
560

    
561
if ($savemsg) {
562
	print_info_box($savemsg, 'success');
563
}
564

    
565
if (is_subsystem_dirty('restore')):
566
?>
567
	<br/>
568
	<form action="diag_reboot.php" method="post">
569
		<input name="Submit" type="hidden" value="Yes" />
570
		<?php print_info_box(gettext("The firewall configuration has been changed.") . "<br />" . gettext("The firewall is now rebooting.")); ?>
571
		<br />
572
	</form>
573
<?php
574
endif;
575

    
576
$tab_array = array();
577
$tab_array[] = array(htmlspecialchars(gettext("Backup & Restore")), true, "diag_backup.php");
578
$tab_array[] = array(gettext("Config History"), false, "diag_confbak.php");
579
display_top_tabs($tab_array);
580

    
581
$form = new Form(false);
582
$form->setMultipartEncoding();	// Allow file uploads
583

    
584
$section = new Form_Section('Backup Configuration');
585

    
586
$section->addInput(new Form_Select(
587
	'backuparea',
588
	'Backup area',
589
	'',
590
	build_area_list(false)
591
));
592

    
593
$section->addInput(new Form_Checkbox(
594
	'nopackages',
595
	'Skip packages',
596
	'Do not backup package information.',
597
	false
598
));
599

    
600
$section->addInput(new Form_Checkbox(
601
	'donotbackuprrd',
602
	'Skip RRD data',
603
	'Do not backup RRD data (NOTE: RRD Data can consume 4+ megabytes of config.xml space!)',
604
	true
605
));
606

    
607
$section->addInput(new Form_Checkbox(
608
	'encrypt',
609
	'Encryption',
610
	'Encrypt this configuration file.',
611
	false
612
));
613

    
614
$section->addPassword(new Form_Input(
615
	'encrypt_password',
616
	'Password',
617
	'password',
618
	null
619
));
620

    
621
$group = new Form_Group('');
622
// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
623
$group->add(new Form_Button(
624
	'download',
625
	'Download configuration as XML',
626
	null,
627
	'fa-download'
628
))->setAttribute('id')->addClass('btn-primary');
629

    
630
$section->add($group);
631
$form->add($section);
632

    
633
$section = new Form_Section('Restore Backup');
634

    
635
$section->addInput(new Form_StaticText(
636
	null,
637
	sprintf(gettext("Open a %s configuration XML file and click the button below to restore the configuration."), $g['product_name'])
638
));
639

    
640
$section->addInput(new Form_Select(
641
	'restorearea',
642
	'Restore area',
643
	'',
644
	build_area_list(true)
645
));
646

    
647
$section->addInput(new Form_Input(
648
	'conffile',
649
	'Configuration file',
650
	'file',
651
	null
652
));
653

    
654
$section->addInput(new Form_Checkbox(
655
	'decrypt',
656
	'Encryption',
657
	'Configuration file is encrypted.',
658
	false
659
));
660

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

    
669
$group = new Form_Group('');
670
// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
671
$group->add(new Form_Button(
672
	'restore',
673
	'Restore Configuration',
674
	null,
675
	'fa-undo'
676
))->setHelp('The firewall will reboot after restoring the configuration.')->addClass('btn-danger restore')->setAttribute('id');
677

    
678
$section->add($group);
679

    
680
$form->add($section);
681

    
682
if (($config['installedpackages']['package'] != "") || (is_subsystem_dirty("packagelock"))) {
683
	$section = new Form_Section('Package Functions');
684

    
685
	if ($config['installedpackages']['package'] != "") {
686
		$group = new Form_Group('');
687
		// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
688
		$group->add(new Form_Button(
689
			'reinstallpackages',
690
			'Reinstall Packages',
691
			null,
692
			'fa-retweet'
693
		))->setHelp('Click this button to reinstall all system packages.  This may take a while.')->addClass('btn-success')->setAttribute('id');
694

    
695
		$section->add($group);
696
	}
697

    
698
	if (is_subsystem_dirty("packagelock")) {
699
		$group = new Form_Group('');
700
		// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
701
		$group->add(new Form_Button(
702
			'clearpackagelock',
703
			'Clear Package Lock',
704
			null,
705
			'fa-wrench'
706
		))->setHelp('Click this button to clear the package lock if a package fails to reinstall properly after an upgrade.')->addClass('btn-warning')->setAttribute('id');
707

    
708
		$section->add($group);
709
	}
710

    
711
	$form->add($section);
712
}
713

    
714
print($form);
715
?>
716
<script type="text/javascript">
717
//<![CDATA[
718
events.push(function() {
719

    
720
	// ------- Show/hide sections based on checkbox settings --------------------------------------
721

    
722
	function hideSections(hide) {
723
		hidePasswords();
724
	}
725

    
726
	function hidePasswords() {
727

    
728
		encryptHide = !($('input[name="encrypt"]').is(':checked'));
729
		decryptHide = !($('input[name="decrypt"]').is(':checked'));
730

    
731
		hideInput('encrypt_password', encryptHide);
732
		hideInput('decrypt_password', decryptHide);
733
	}
734

    
735
	// ---------- Click handlers ------------------------------------------------------------------
736

    
737
	$('input[name="encrypt"]').on('change', function() {
738
		hidePasswords();
739
	});
740

    
741
	$('input[name="decrypt"]').on('change', function() {
742
		hidePasswords();
743
	});
744

    
745
	$('#conffile').change(function () {
746
		if (document.getElementById("conffile").value) {
747
			$('.restore').prop('disabled', false);
748
		} else {
749
			$('.restore').prop('disabled', true);
750
		}
751
    });
752
	// ---------- On initial page load ------------------------------------------------------------
753

    
754
	hideSections();
755
	$('.restore').prop('disabled', true);
756
});
757
//]]>
758
</script>
759

    
760
<?php
761
include("foot.inc");
762

    
763
if (is_subsystem_dirty('restore')) {
764
	system_reboot();
765
}
(10-10/228)