Project

General

Profile

Download (22.3 KB) Statistics
| Branch: | Tag: | Revision:
1 8ccc8f1a Scott Ullrich
<?php
2 5b237745 Scott Ullrich
/*
3 c5d81585 Renato Botelho
 * diag_backup.php
4 9da2cf1c Stephen Beaver
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8 0284d79e jim-p
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9 c5d81585 Renato Botelho
 * All rights reserved.
10 fd9ebcd5 Stephen Beaver
 *
11 c5d81585 Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14 fd9ebcd5 Stephen Beaver
 *
15 b12ea3fb Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 *
19 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
20 fd9ebcd5 Stephen Beaver
 *
21 b12ea3fb Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 */
27 5b237745 Scott Ullrich
28 6b07c15a Matthew Grooms
##|+PRIV
29 5d916fc7 Stephen Beaver
##|*IDENT=page-diagnostics-backup-restore
30 0f298138 k-paulius
##|*NAME=Diagnostics: Backup & Restore
31
##|*DESCR=Allow access to the 'Diagnostics: Backup & Restore' page.
32 57188e47 Phil Davis
##|*WARN=standard-warning-root
33 6b07c15a Matthew Grooms
##|*MATCH=diag_backup.php*
34
##|-PRIV
35
36 47d11b79 Mark Crane
/* Allow additional execution time 0 = no limit. */
37 3420028c Scott Ullrich
ini_set('max_execution_time', '0');
38
ini_set('max_input_time', '0');
39 47d11b79 Mark Crane
40 5b237745 Scott Ullrich
/* omit no-cache headers because it confuses IE with file downloads */
41
$omit_nocacheheaders = true;
42 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
43 7a927e67 Scott Ullrich
require_once("functions.inc");
44
require_once("filter.inc");
45
require_once("shaper.inc");
46 4be5ed9f Renato Botelho
require_once("pkg-utils.inc");
47 5b237745 Scott Ullrich
48 8bdb6879 Darren Embry
$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 73ed069b Renato Botelho
55 8bdb6879 Darren Embry
	$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 754b75d0 Darren Embry
function restore_rrddata() {
76 7eead1fc Darren Embry
	global $config, $g, $rrdtool, $input_errors;
77 5f601060 Phil Davis
	foreach ($config['rrddata']['rrddatafile'] as $rrd) {
78 754b75d0 Darren Embry
		if ($rrd['xmldata']) {
79
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
80
			$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
81 08877746 Darren Embry
			if (file_put_contents($xml_file, gzinflate(base64_decode($rrd['xmldata']))) === false) {
82 ff30e319 bruno
				log_error(sprintf(gettext("Cannot write %s"), $xml_file));
83 08877746 Darren Embry
				continue;
84
			}
85
			$output = array();
86
			$status = null;
87
			exec("$rrdtool restore -f '{$xml_file}' '{$rrd_file}'", $output, $status);
88
			if ($status) {
89 f757431d Darren Embry
				log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
90 08877746 Darren Embry
				continue;
91
			}
92 754b75d0 Darren Embry
			unlink($xml_file);
93 5f601060 Phil Davis
		} else if ($rrd['data']) {
94 7eead1fc Darren Embry
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
95
			$rrd_fd = fopen($rrd_file, "w");
96 08877746 Darren Embry
			if (!$rrd_fd) {
97 ff30e319 bruno
				log_error(sprintf(gettext("Cannot write %s"), $rrd_file));
98 08877746 Darren Embry
				continue;
99
			}
100 754b75d0 Darren Embry
			$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 08877746 Darren Embry
				if (fwrite($rrd_fd, $dcomp) === false) {
106 ff30e319 bruno
					log_error(sprintf(gettext("fwrite %s failed"), $rrd_file));
107 08877746 Darren Embry
					continue;
108
				}
109 754b75d0 Darren Embry
			} else {
110
				/* If the decompression failed, it wasn't compressed, so write raw data */
111 08877746 Darren Embry
				if (fwrite($rrd_fd, $data) === false) {
112 ff30e319 bruno
					log_error(sprintf(gettext("fwrite %s failed"), $rrd_file));
113 08877746 Darren Embry
					continue;
114
				}
115
			}
116
			if (fclose($rrd_fd) === false) {
117 ff30e319 bruno
				log_error(sprintf(gettext("fclose %s failed"), $rrd_file));
118 08877746 Darren Embry
				continue;
119 754b75d0 Darren Embry
			}
120
		}
121
	}
122
}
123
124 645ec835 Scott Ullrich
function remove_bad_chars($string) {
125 699737d9 Phil Davis
	return preg_replace('/[^a-z_0-9]/i', '', $string);
126 645ec835 Scott Ullrich
}
127
128 b3277798 Scott Ullrich
function check_and_returnif_section_exists($section) {
129
	global $config;
130 5f601060 Phil Davis
	if (is_array($config[$section])) {
131 b3277798 Scott Ullrich
		return true;
132 5f601060 Phil Davis
	}
133 b3277798 Scott Ullrich
	return false;
134
}
135
136 da17d77e Ermal Lu?i
if ($_POST['apply']) {
137 73ed069b Renato Botelho
	ob_flush();
138
	flush();
139 c9a19238 Darren Embry
	clear_subsystem_dirty("restore");
140 73ed069b Renato Botelho
	exit;
141 da17d77e Ermal Lu?i
}
142
143 5b237745 Scott Ullrich
if ($_POST) {
144
	unset($input_errors);
145 9a7e1c95 Phil Davis
	if ($_POST['restore']) {
146 5b237745 Scott Ullrich
		$mode = "restore";
147 9a7e1c95 Phil Davis
	} else if ($_POST['reinstallpackages']) {
148 8ccc8f1a Scott Ullrich
		$mode = "reinstallpackages";
149 9a7e1c95 Phil Davis
	} else if ($_POST['clearpackagelock']) {
150 039cb920 jim-p
		$mode = "clearpackagelock";
151 9a7e1c95 Phil Davis
	} else if ($_POST['download']) {
152 5b237745 Scott Ullrich
		$mode = "download";
153 5f601060 Phil Davis
	}
154
	if ($_POST["nopackages"] <> "") {
155 528cad39 Colin Smith
		$options = "nopackages";
156 5f601060 Phil Davis
	}
157 5b237745 Scott Ullrich
	if ($mode) {
158
		if ($mode == "download") {
159 8ff5ffcc Matthew Grooms
			if ($_POST['encrypt']) {
160 76d6d925 Stephen Beaver
				if (!$_POST['encrypt_password']) {
161 89854d71 NewEraCracker
					$input_errors[] = gettext("A password for encryption must be supplied and confirmed.");
162 5f601060 Phil Davis
				}
163 528cad39 Colin Smith
			}
164 73ed069b Renato Botelho
165 8ff5ffcc Matthew Grooms
			if (!$input_errors) {
166 73ed069b Renato Botelho
167 07b1797b sullrich
				//$lockbckp = lock('config');
168 73ed069b Renato Botelho
169 8ff5ffcc Matthew Grooms
				$host = "{$config['system']['hostname']}.{$config['system']['domain']}";
170
				$name = "config-{$host}-".date("YmdHis").".xml";
171
				$data = "";
172 73ed069b Renato Botelho
173 5f601060 Phil Davis
				if ($options == "nopackages") {
174
					if (!$_POST['backuparea']) {
175 4195c967 sullrich
						/* backup entire configuration */
176
						$data = file_get_contents("{$g['conf_path']}/config.xml");
177
					} else {
178
						/* backup specific area of configuration */
179
						$data = backup_config_section($_POST['backuparea']);
180
						$name = "{$_POST['backuparea']}-{$name}";
181
					}
182 79955411 Renato Botelho
					$data = preg_replace('/\t*<installedpackages>.*<\/installedpackages>\n/sm', '', $data);
183 8ff5ffcc Matthew Grooms
				} else {
184 5f601060 Phil Davis
					if (!$_POST['backuparea']) {
185 8ff5ffcc Matthew Grooms
						/* backup entire configuration */
186
						$data = file_get_contents("{$g['conf_path']}/config.xml");
187 7eead1fc Darren Embry
					} else if ($_POST['backuparea'] === "rrddata") {
188
						$data = rrd_data_xml();
189
						$name = "{$_POST['backuparea']}-{$name}";
190 8ff5ffcc Matthew Grooms
					} else {
191
						/* backup specific area of configuration */
192
						$data = backup_config_section($_POST['backuparea']);
193
						$name = "{$_POST['backuparea']}-{$name}";
194
					}
195 181462b5 Scott Ullrich
				}
196 73ed069b Renato Botelho
197 07b1797b sullrich
				//unlock($lockbckp);
198 73ed069b Renato Botelho
199 a250f179 Renato Botelho
				/*
200 0e834fc5 Stephen Beaver
				 *	Backup RRD Data
201 1390b049 Scott Ullrich
				 */
202 7eead1fc Darren Embry
				if ($_POST['backuparea'] !== "rrddata" && !$_POST['donotbackuprrd']) {
203 8bdb6879 Darren Embry
					$rrd_data_xml = rrd_data_xml();
204
					$closing_tag = "</" . $g['xml_rootobj'] . ">";
205 93867844 jim-p
206
					/* If the config on disk had rrddata tags already, remove that section first.
207
					 * See https://redmine.pfsense.org/issues/8994 */
208
					$data = preg_replace("/<rrddata>.*<\\/rrddata>/", "", $data);
209
					$data = preg_replace("/<rrddata\\/>/", "", $data);
210
211 8bdb6879 Darren Embry
					$data = str_replace($closing_tag, $rrd_data_xml . $closing_tag, $data);
212 1390b049 Scott Ullrich
				}
213 73ed069b Renato Botelho
214 4cfd2390 Renato Botelho
				if ($_POST['encrypt']) {
215
					$data = encrypt_data($data, $_POST['encrypt_password']);
216
					tagfile_reformat($data, $data, "config.xml");
217
				}
218
219 ecb594d0 jim-p
				send_user_download('data', $data, $name);
220 8ff5ffcc Matthew Grooms
			}
221
		}
222 73ed069b Renato Botelho
223 8ff5ffcc Matthew Grooms
		if ($mode == "restore") {
224
			if ($_POST['decrypt']) {
225 76d6d925 Stephen Beaver
				if (!$_POST['decrypt_password']) {
226 89854d71 NewEraCracker
					$input_errors[] = gettext("A password for decryption must be supplied and confirmed.");
227 5f601060 Phil Davis
				}
228 8ff5ffcc Matthew Grooms
			}
229 73ed069b Renato Botelho
230 8ff5ffcc Matthew Grooms
			if (!$input_errors) {
231
				if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
232 73ed069b Renato Botelho
233 8ff5ffcc Matthew Grooms
					/* read the file contents */
234
					$data = file_get_contents($_FILES['conffile']['tmp_name']);
235 5f601060 Phil Davis
					if (!$data) {
236 153f78af Viktor Gurov
						$input_errors[] = gettext("Warning, could not read file {$_FILES['conffile']['tmp_name']}");
237
					} elseif ($_POST['decrypt']) {
238 8ff5ffcc Matthew Grooms
						if (!tagfile_deformat($data, $data, "config.xml")) {
239 205da5a0 Neriberto C.Prado
							$input_errors[] = gettext("The uploaded file does not appear to contain an encrypted pfsense configuration.");
240 153f78af Viktor Gurov
						} else {
241
							$data = decrypt_data($data, $_POST['decrypt_password']);
242
							if (empty($data)) {
243
								$input_errors[] = gettext("File decryption failed. Incorrect password or file is invalid.");
244
							}
245 8ff5ffcc Matthew Grooms
						}
246
					}
247 5f601060 Phil Davis
					if (stristr($data, "<m0n0wall>")) {
248 205da5a0 Neriberto C.Prado
						log_error(gettext("Upgrading m0n0wall configuration to pfsense."));
249 8ff5ffcc Matthew Grooms
						/* m0n0wall was found in config.  convert it. */
250
						$data = str_replace("m0n0wall", "pfsense", $data);
251
						$m0n0wall_upgrade = true;
252
					}
253 93867844 jim-p
254
					/* If the config on disk had empty rrddata tags, remove them to
255
					 * avoid an XML parsing error.
256
					 * See https://redmine.pfsense.org/issues/8994 */
257
					$data = preg_replace("/<rrddata><\\/rrddata>/", "", $data);
258
					$data = preg_replace("/<rrddata\\/>/", "", $data);
259
260 153f78af Viktor Gurov
					if ($_POST['restorearea'] && !$input_errors) {
261 8ff5ffcc Matthew Grooms
						/* restore a specific area of the configuration */
262 5f601060 Phil Davis
						if (!stristr($data, "<" . $_POST['restorearea'] . ">")) {
263 b6078bed NOYB
							$input_errors[] = gettext("An area to restore was selected but the correct xml tag could not be located.");
264 8ff5ffcc Matthew Grooms
						} else {
265 8dcca9b5 Darren Embry
							if (!restore_config_section($_POST['restorearea'], $data)) {
266 b6078bed NOYB
								$input_errors[] = gettext("An area to restore was selected but the correct xml tag could not be located.");
267 8dcca9b5 Darren Embry
							} else {
268
								if ($config['rrddata']) {
269
									restore_rrddata();
270
									unset($config['rrddata']);
271
									unlink_if_exists("{$g['tmp_path']}/config.cache");
272 919a43a7 doktornotor
									write_config(sprintf(gettext("Unset RRD data from configuration after restoring %s configuration area"), $_POST['restorearea']));
273 8dcca9b5 Darren Embry
									convert_config();
274
								}
275
								filter_configure();
276 b6078bed NOYB
								$savemsg = gettext("The configuration area has been restored. The firewall may need to be rebooted.");
277 7eead1fc Darren Embry
							}
278 8ff5ffcc Matthew Grooms
						}
279 153f78af Viktor Gurov
					} elseif (!$input_errors) {
280 5f601060 Phil Davis
						if (!stristr($data, "<" . $g['xml_rootobj'] . ">")) {
281 b6078bed NOYB
							$input_errors[] = sprintf(gettext("A full configuration restore was selected but a %s tag could not be located."), $g['xml_rootobj']);
282 8ff5ffcc Matthew Grooms
						} else {
283
							/* restore the entire configuration */
284
							file_put_contents($_FILES['conffile']['tmp_name'], $data);
285
							if (config_install($_FILES['conffile']['tmp_name']) == 0) {
286 0430b1b4 Renato Botelho
								/* Save current pkg repo to re-add on new config */
287
								unset($pkg_repo_conf_path);
288
								if (isset($config['system']['pkg_repo_conf_path'])) {
289
									$pkg_repo_conf_path = $config['system']['pkg_repo_conf_path'];
290
								}
291
292 8ff5ffcc Matthew Grooms
								/* this will be picked up by /index.php */
293 da17d77e Ermal Lu?i
								mark_subsystem_dirty("restore");
294 10511c3b Renato Botelho
								touch("/conf/needs_package_sync");
295 8ff5ffcc Matthew Grooms
								/* remove cache, we will force a config reboot */
296 5f601060 Phil Davis
								if (file_exists("{$g['tmp_path']}/config.cache")) {
297 da17d77e Ermal Lu?i
									unlink("{$g['tmp_path']}/config.cache");
298 5f601060 Phil Davis
								}
299 8ff5ffcc Matthew Grooms
								$config = parse_config(true);
300 0430b1b4 Renato Botelho
301
								/* Restore previously pkg repo configured */
302
								$pkg_repo_restored = false;
303
								if (isset($pkg_repo_conf_path)) {
304
									$config['system']['pkg_repo_conf_path'] =
305
									    $pkg_repo_conf_path;
306
									$pkg_repo_restored = true;
307
								} elseif (isset($config['system']['pkg_repo_conf_path'])) {
308
									unset($config['system']['pkg_repo_conf_path']);
309
									$pkg_repo_restored = true;
310
								}
311
312
								if ($pkg_repo_restored) {
313
									write_config(gettext("Removing pkg repository set after restoring full configuration"));
314 4be5ed9f Renato Botelho
									pkg_update(true);
315 0430b1b4 Renato Botelho
								}
316
317 0174c480 Ermal LUÇI
								if (file_exists("/boot/loader.conf")) {
318
									$loaderconf = file_get_contents("/boot/loader.conf");
319 c91af4ac Luiz Souza
									if (strpos($loaderconf, "console=\"comconsole") ||
320 e0b32eb9 jim-p
									    strpos($loaderconf, "boot_serial=\"YES")) {
321 4ff3adec Luiz Souza
										$config['system']['enableserial'] = true;
322
										write_config(gettext("Restore serial console enabling in configuration."));
323
									}
324
									unset($loaderconf);
325
								}
326
								if (file_exists("/boot/loader.conf.local")) {
327
									$loaderconf = file_get_contents("/boot/loader.conf.local");
328 c91af4ac Luiz Souza
									if (strpos($loaderconf, "console=\"comconsole") ||
329 e0b32eb9 jim-p
									    strpos($loaderconf, "boot_serial=\"YES")) {
330 0174c480 Ermal LUÇI
										$config['system']['enableserial'] = true;
331 ff30e319 bruno
										write_config(gettext("Restore serial console enabling in configuration."));
332 0174c480 Ermal LUÇI
									}
333 5d4b8830 Ermal LUÇI
									unset($loaderconf);
334 0174c480 Ermal LUÇI
								}
335 67bc955d Chris Buechler
								/* extract out rrd items, unset from $config when done */
336 5f601060 Phil Davis
								if ($config['rrddata']) {
337 754b75d0 Darren Embry
									restore_rrddata();
338 a2b8f7b2 Scott Ullrich
									unset($config['rrddata']);
339 da17d77e Ermal Lu?i
									unlink_if_exists("{$g['tmp_path']}/config.cache");
340 919a43a7 doktornotor
									write_config(gettext("Unset RRD data from configuration after restoring full configuration"));
341 d442e4e2 Scott Ullrich
									convert_config();
342 1390b049 Scott Ullrich
								}
343 5f601060 Phil Davis
								if ($m0n0wall_upgrade == true) {
344
									if ($config['system']['gateway'] <> "") {
345 8ff5ffcc Matthew Grooms
										$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
346 5f601060 Phil Davis
									}
347 8ff5ffcc Matthew Grooms
									unset($config['shaper']);
348
									/* optional if list */
349 80fe8369 Phil Davis
									$ifdescrs = get_configured_interface_list(true);
350 8ff5ffcc Matthew Grooms
									/* remove special characters from interface descriptions */
351 5f601060 Phil Davis
									if (is_array($ifdescrs)) {
352
										foreach ($ifdescrs as $iface) {
353 8ff5ffcc Matthew Grooms
											$config['interfaces'][$iface]['descr'] = remove_bad_chars($config['interfaces'][$iface]['descr']);
354 5f601060 Phil Davis
										}
355
									}
356 b6db8ea3 sullrich
									/* check for interface names with an alias */
357 5f601060 Phil Davis
									if (is_array($ifdescrs)) {
358
										foreach ($ifdescrs as $iface) {
359
											if (is_alias($config['interfaces'][$iface]['descr'])) {
360 b6db8ea3 sullrich
												$origname = $config['interfaces'][$iface]['descr'];
361 24807bfe Phil Davis
												update_alias_name($origname . "Alias", $origname);
362 b6db8ea3 sullrich
											}
363
										}
364
									}
365 da17d77e Ermal Lu?i
									unlink_if_exists("{$g['tmp_path']}/config.cache");
366 888e7a27 Scott Ullrich
									// Reset configuration version to something low
367 a250f179 Renato Botelho
									// in order to force the config upgrade code to
368 888e7a27 Scott Ullrich
									// run through with all steps that are required.
369
									$config['system']['version'] = "1.0";
370 798cb31a Scott Ullrich
									// Deal with descriptions longer than 63 characters
371
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
372 5f601060 Phil Davis
										if (count($config['filter']['rule'][$i]['descr']) > 63) {
373 798cb31a Scott Ullrich
											$config['filter']['rule'][$i]['descr'] = substr($config['filter']['rule'][$i]['descr'], 0, 63);
374 5f601060 Phil Davis
										}
375 798cb31a Scott Ullrich
									}
376
									// Move interface from ipsec to enc0
377
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
378 5f601060 Phil Davis
										if ($config['filter']['rule'][$i]['interface'] == "ipsec") {
379 798cb31a Scott Ullrich
											$config['filter']['rule'][$i]['interface'] = "enc0";
380 5f601060 Phil Davis
										}
381 798cb31a Scott Ullrich
									}
382
									// Convert icmp types
383
									// http://www.openbsd.org/cgi-bin/man.cgi?query=icmp&sektion=4&arch=i386&apropos=0&manpath=OpenBSD+Current
384 0ce1667b stilez
									$convert = array('echo' => 'echoreq', 'timest' => 'timereq', 'timestrep' => 'timerep');
385
									foreach ($config["filter"]["rule"] as $ruleid => &$ruledata) {
386
										if ($convert[$ruledata['icmptype']]) {
387
											$ruledata['icmptype'] = $convert[$ruledata['icmptype']];
388 798cb31a Scott Ullrich
										}
389 dec5cb85 Scott Ullrich
									}
390
									$config['diag']['ipv6nat'] = true;
391 919a43a7 doktornotor
									write_config(gettext("Imported m0n0wall configuration"));
392 d442e4e2 Scott Ullrich
									convert_config();
393 205da5a0 Neriberto C.Prado
									$savemsg = gettext("The m0n0wall configuration has been restored and upgraded to pfSense.");
394 da17d77e Ermal Lu?i
									mark_subsystem_dirty("restore");
395 8ff5ffcc Matthew Grooms
								}
396 5f601060 Phil Davis
								if (is_array($config['captiveportal'])) {
397
									foreach ($config['captiveportal'] as $cp) {
398 2e19a683 Renato Botelho
										if (isset($cp['enable'])) {
399
											/* for some reason ipfw doesn't init correctly except on bootup sequence */
400
											mark_subsystem_dirty("restore");
401
											break;
402
										}
403
									}
404 8ff5ffcc Matthew Grooms
								}
405 749dfdb7 Luiz Souza
								console_configure();
406 5f601060 Phil Davis
								if (is_interface_mismatch() == true) {
407 8ff5ffcc Matthew Grooms
									touch("/var/run/interface_mismatch_reboot_needed");
408 da17d77e Ermal Lu?i
									clear_subsystem_dirty("restore");
409 f5a57bb0 Scott Ullrich
									convert_config();
410 8ff5ffcc Matthew Grooms
									header("Location: interfaces_assign.php");
411 bafaf123 Scott Ullrich
									exit;
412 8ff5ffcc Matthew Grooms
								}
413 66bcba1b Ermal
								if (is_interface_vlan_mismatch() == true) {
414
									touch("/var/run/interface_mismatch_reboot_needed");
415
									clear_subsystem_dirty("restore");
416
									convert_config();
417
									header("Location: interfaces_assign.php");
418
									exit;
419
								}
420 8ff5ffcc Matthew Grooms
							} else {
421 205da5a0 Neriberto C.Prado
								$input_errors[] = gettext("The configuration could not be restored.");
422 db3996df Scott Ullrich
							}
423 9c72b993 Scott Ullrich
						}
424 181462b5 Scott Ullrich
					}
425 8ff5ffcc Matthew Grooms
				} else {
426 205da5a0 Neriberto C.Prado
					$input_errors[] = gettext("The configuration could not be restored (file upload error).");
427 db3996df Scott Ullrich
				}
428 5b237745 Scott Ullrich
			}
429 8ff5ffcc Matthew Grooms
		}
430 73ed069b Renato Botelho
431 8ff5ffcc Matthew Grooms
		if ($mode == "reinstallpackages") {
432 8ccc8f1a Scott Ullrich
			header("Location: pkg_mgr_install.php?mode=reinstallall");
433
			exit;
434 039cb920 jim-p
		} else if ($mode == "clearpackagelock") {
435
			clear_subsystem_dirty('packagelock');
436 8545adde k-paulius
			$savemsg = "Package lock cleared.";
437 5b237745 Scott Ullrich
		}
438
	}
439
}
440 6a1e6651 Bill Marquette
441 02e1170d Scott Ullrich
$id = rand() . '.' . time();
442
443
$mth = ini_get('upload_progress_meter.store_method');
444
$dir = ini_get('upload_progress_meter.file.filename_template');
445
446 a4af095c Renato Botelho
function build_area_list($showall) {
447
	global $config;
448 b63695db Scott Ullrich
449 0e834fc5 Stephen Beaver
	$areas = array(
450
		"aliases" => gettext("Aliases"),
451 a4af095c Renato Botelho
		"captiveportal" => gettext("Captive Portal"),
452
		"voucher" => gettext("Captive Portal Vouchers"),
453
		"dnsmasq" => gettext("DNS Forwarder"),
454
		"unbound" => gettext("DNS Resolver"),
455
		"dhcpd" => gettext("DHCP Server"),
456
		"dhcpdv6" => gettext("DHCPv6 Server"),
457
		"filter" => gettext("Firewall Rules"),
458
		"interfaces" => gettext("Interfaces"),
459
		"ipsec" => gettext("IPSEC"),
460
		"nat" => gettext("NAT"),
461
		"openvpn" => gettext("OpenVPN"),
462
		"installedpackages" => gettext("Package Manager"),
463
		"rrddata" => gettext("RRD Data"),
464
		"cron" => gettext("Scheduled Tasks"),
465
		"syslog" => gettext("Syslog"),
466
		"system" => gettext("System"),
467
		"staticroutes" => gettext("Static routes"),
468
		"sysctl" => gettext("System tunables"),
469
		"snmpd" => gettext("SNMP Server"),
470
		"shaper" => gettext("Traffic Shaper"),
471
		"vlans" => gettext("VLANS"),
472 7ca42d47 k-paulius
		"wol" => gettext("Wake-on-LAN")
473 a4af095c Renato Botelho
		);
474 0e834fc5 Stephen Beaver
475
	$list = array("" => gettext("All"));
476
477 288a2a0f Phil Davis
	if ($showall) {
478 0e834fc5 Stephen Beaver
		return($list + $areas);
479 288a2a0f Phil Davis
	} else {
480 a4af095c Renato Botelho
		foreach ($areas as $area => $areaname) {
481
			if ($area === "rrddata" || check_and_returnif_section_exists($area) == true) {
482
				$list[$area] = $areaname;
483
			}
484
		}
485 8ff5ffcc Matthew Grooms
486 0e834fc5 Stephen Beaver
		return($list);
487 5f601060 Phil Davis
	}
488 8ff5ffcc Matthew Grooms
}
489
490 0f298138 k-paulius
$pgtitle = array(gettext("Diagnostics"), htmlspecialchars(gettext("Backup & Restore")), htmlspecialchars(gettext("Backup & Restore")));
491 edcd7535 Phil Davis
$pglinks = array("", "@self", "@self");
492 b63695db Scott Ullrich
include("head.inc");
493
494 947141fd Phil Davis
if ($input_errors) {
495 a4af095c Renato Botelho
	print_input_errors($input_errors);
496 947141fd Phil Davis
}
497 0e834fc5 Stephen Beaver
498 947141fd Phil Davis
if ($savemsg) {
499 a4af095c Renato Botelho
	print_info_box($savemsg, 'success');
500 947141fd Phil Davis
}
501 0e834fc5 Stephen Beaver
502 a4af095c Renato Botelho
if (is_subsystem_dirty('restore')):
503 5b237745 Scott Ullrich
?>
504 a4af095c Renato Botelho
	<br/>
505 1af5edbf Stephen Beaver
	<form action="diag_reboot.php" method="post">
506 7d5b007c Sjon Hortensius
		<input name="Submit" type="hidden" value="Yes" />
507 f6aebbcc NewEraCracker
		<?php print_info_box(gettext("The firewall configuration has been changed.") . "<br />" . gettext("The firewall is now rebooting.")); ?>
508 a4af095c Renato Botelho
		<br />
509 7d5b007c Sjon Hortensius
	</form>
510 0e834fc5 Stephen Beaver
<?php
511 a4af095c Renato Botelho
endif;
512
513
$tab_array = array();
514 0f298138 k-paulius
$tab_array[] = array(htmlspecialchars(gettext("Backup & Restore")), true, "diag_backup.php");
515 a4af095c Renato Botelho
$tab_array[] = array(gettext("Config History"), false, "diag_confbak.php");
516
display_top_tabs($tab_array);
517
518
$form = new Form(false);
519 3beeb66a Stephen Beaver
$form->setMultipartEncoding();	// Allow file uploads
520 a4af095c Renato Botelho
521 5f88f964 k-paulius
$section = new Form_Section('Backup Configuration');
522 a4af095c Renato Botelho
523
$section->addInput(new Form_Select(
524
	'backuparea',
525
	'Backup area',
526
	'',
527
	build_area_list(false)
528
));
529
530
$section->addInput(new Form_Checkbox(
531
	'nopackages',
532
	'Skip packages',
533
	'Do not backup package information.',
534
	false
535
));
536
537
$section->addInput(new Form_Checkbox(
538
	'donotbackuprrd',
539
	'Skip RRD data',
540
	'Do not backup RRD data (NOTE: RRD Data can consume 4+ megabytes of config.xml space!)',
541
	true
542
));
543
544
$section->addInput(new Form_Checkbox(
545
	'encrypt',
546
	'Encryption',
547
	'Encrypt this configuration file.',
548
	false
549 7c0f116b Jared Dillard
));
550 a4af095c Renato Botelho
551 76d6d925 Stephen Beaver
$section->addInput(new Form_Input(
552 a4af095c Renato Botelho
	'encrypt_password',
553 c8b10b4c Stephen Beaver
	'Password',
554 a4af095c Renato Botelho
	'password',
555 c8b10b4c Stephen Beaver
	null
556 7c0f116b Jared Dillard
));
557 a4af095c Renato Botelho
558
$group = new Form_Group('');
559 1d80d714 NOYB
// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
560 a4af095c Renato Botelho
$group->add(new Form_Button(
561 9a7e1c95 Phil Davis
	'download',
562 faab522f Renato Botelho
	'Download configuration as XML',
563 37676f4e jim-p
	null,
564
	'fa-download'
565
))->setAttribute('id')->addClass('btn-primary');
566 a4af095c Renato Botelho
567
$section->add($group);
568
$form->add($section);
569
570 5f88f964 k-paulius
$section = new Form_Section('Restore Backup');
571 a4af095c Renato Botelho
572
$section->addInput(new Form_StaticText(
573
	null,
574 52e416cc Phil Davis
	sprintf(gettext("Open a %s configuration XML file and click the button below to restore the configuration."), $g['product_name'])
575 a4af095c Renato Botelho
));
576
577
$section->addInput(new Form_Select(
578
	'restorearea',
579
	'Restore area',
580
	'',
581 ce3eca6e Phil Davis
	build_area_list(true)
582 a4af095c Renato Botelho
));
583
584
$section->addInput(new Form_Input(
585
	'conffile',
586
	'Configuration file',
587
	'file',
588
	null
589
));
590
591
$section->addInput(new Form_Checkbox(
592 7c0f116b Jared Dillard
	'decrypt',
593 a4af095c Renato Botelho
	'Encryption',
594 7c0f116b Jared Dillard
	'Configuration file is encrypted.',
595 a4af095c Renato Botelho
	false
596 7c0f116b Jared Dillard
));
597 a4af095c Renato Botelho
598 76d6d925 Stephen Beaver
$section->addInput(new Form_Input(
599 a4af095c Renato Botelho
	'decrypt_password',
600 c8b10b4c Stephen Beaver
	'Password',
601 a4af095c Renato Botelho
	'password',
602
	null,
603
	['placeholder' => 'Password']
604 7c0f116b Jared Dillard
));
605 a4af095c Renato Botelho
606
$group = new Form_Group('');
607 1d80d714 NOYB
// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
608 a4af095c Renato Botelho
$group->add(new Form_Button(
609 9a7e1c95 Phil Davis
	'restore',
610 faab522f Renato Botelho
	'Restore Configuration',
611 37676f4e jim-p
	null,
612
	'fa-undo'
613
))->setHelp('The firewall will reboot after restoring the configuration.')->addClass('btn-danger restore')->setAttribute('id');
614 a4af095c Renato Botelho
615
$section->add($group);
616
617
$form->add($section);
618
619
if (($config['installedpackages']['package'] != "") || (is_subsystem_dirty("packagelock"))) {
620 5f88f964 k-paulius
	$section = new Form_Section('Package Functions');
621 0e834fc5 Stephen Beaver
622 a4af095c Renato Botelho
	if ($config['installedpackages']['package'] != "") {
623
		$group = new Form_Group('');
624 1d80d714 NOYB
		// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
625 a4af095c Renato Botelho
		$group->add(new Form_Button(
626 9a7e1c95 Phil Davis
			'reinstallpackages',
627 faab522f Renato Botelho
			'Reinstall Packages',
628 37676f4e jim-p
			null,
629
			'fa-retweet'
630
		))->setHelp('Click this button to reinstall all system packages.  This may take a while.')->addClass('btn-success')->setAttribute('id');
631 0e834fc5 Stephen Beaver
632 a4af095c Renato Botelho
		$section->add($group);
633 7eead1fc Darren Embry
	}
634 0e834fc5 Stephen Beaver
635 a4af095c Renato Botelho
	if (is_subsystem_dirty("packagelock")) {
636
		$group = new Form_Group('');
637 1d80d714 NOYB
		// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
638 a4af095c Renato Botelho
		$group->add(new Form_Button(
639 9a7e1c95 Phil Davis
			'clearpackagelock',
640 faab522f Renato Botelho
			'Clear Package Lock',
641 37676f4e jim-p
			null,
642
			'fa-wrench'
643
		))->setHelp('Click this button to clear the package lock if a package fails to reinstall properly after an upgrade.')->addClass('btn-warning')->setAttribute('id');
644 0e834fc5 Stephen Beaver
645 a4af095c Renato Botelho
		$section->add($group);
646
	}
647 0e834fc5 Stephen Beaver
648 a4af095c Renato Botelho
	$form->add($section);
649 a1003d57 Ermal Lu?i
}
650 a4af095c Renato Botelho
651
print($form);
652 7c0f116b Jared Dillard
?>
653
<script type="text/javascript">
654
//<![CDATA[
655 947141fd Phil Davis
events.push(function() {
656 7c0f116b Jared Dillard
657
	// ------- Show/hide sections based on checkbox settings --------------------------------------
658 0da0d43e Phil Davis
659 7c0f116b Jared Dillard
	function hideSections(hide) {
660
		hidePasswords();
661
	}
662
663
	function hidePasswords() {
664 3beeb66a Stephen Beaver
665 7c0f116b Jared Dillard
		encryptHide = !($('input[name="encrypt"]').is(':checked'));
666
		decryptHide = !($('input[name="decrypt"]').is(':checked'));
667
668
		hideInput('encrypt_password', encryptHide);
669 c8b10b4c Stephen Beaver
		hideInput('encrypt_password_confirm', encryptHide);
670 7c0f116b Jared Dillard
		hideInput('decrypt_password', decryptHide);
671 c8b10b4c Stephen Beaver
		hideInput('decrypt_password_confirm', decryptHide);
672 7c0f116b Jared Dillard
	}
673
674 ff59b884 Stephen Beaver
	// ---------- Click handlers ------------------------------------------------------------------
675 7c0f116b Jared Dillard
676
	$('input[name="encrypt"]').on('change', function() {
677
		hidePasswords();
678
	});
679
680
	$('input[name="decrypt"]').on('change', function() {
681
		hidePasswords();
682 3beeb66a Stephen Beaver
	});
683 7c0f116b Jared Dillard
684 947141fd Phil Davis
	$('#conffile').change(function () {
685 40f146c5 Phil Davis
		if (document.getElementById("conffile").value) {
686
			$('.restore').prop('disabled', false);
687
		} else {
688
			$('.restore').prop('disabled', true);
689
		}
690 ff59b884 Stephen Beaver
    });
691 eef93144 Jared Dillard
	// ---------- On initial page load ------------------------------------------------------------
692
693 7c0f116b Jared Dillard
	hideSections();
694 ff59b884 Stephen Beaver
	$('.restore').prop('disabled', true);
695 7c0f116b Jared Dillard
});
696
//]]>
697
</script>
698
699
<?php
700 a4af095c Renato Botelho
include("foot.inc");
701 7a7f3af1 sullrich
702 5f601060 Phil Davis
if (is_subsystem_dirty('restore')) {
703 cf9a4467 jim-p
	system_reboot();
704 c9d46a8e Renato Botelho
}