Project

General

Profile

Download (22.5 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
 * Copyright (c) 2014-2019 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 8ff5ffcc Matthew Grooms
				$size = strlen($data);
220
				header("Content-Type: application/octet-stream");
221
				header("Content-Disposition: attachment; filename={$name}");
222 a8c35980 Scott Ullrich
				header("Content-Length: $size");
223 e77ea573 jim-p
				if (isset($_SERVER['HTTPS'])) {
224
					header('Pragma: ');
225
					header('Cache-Control: ');
226
				} else {
227
					header("Pragma: private");
228
					header("Cache-Control: private, must-revalidate");
229
				}
230 73ed069b Renato Botelho
231 4015b03d jim-p
				while (ob_get_level()) {
232
					@ob_end_clean();
233
				}
234
				echo $data;
235
				@ob_end_flush();
236 8ff5ffcc Matthew Grooms
				exit;
237
			}
238
		}
239 73ed069b Renato Botelho
240 8ff5ffcc Matthew Grooms
		if ($mode == "restore") {
241
			if ($_POST['decrypt']) {
242 76d6d925 Stephen Beaver
				if (!$_POST['decrypt_password']) {
243 89854d71 NewEraCracker
					$input_errors[] = gettext("A password for decryption must be supplied and confirmed.");
244 5f601060 Phil Davis
				}
245 8ff5ffcc Matthew Grooms
			}
246 73ed069b Renato Botelho
247 8ff5ffcc Matthew Grooms
			if (!$input_errors) {
248
				if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
249 73ed069b Renato Botelho
250 8ff5ffcc Matthew Grooms
					/* read the file contents */
251
					$data = file_get_contents($_FILES['conffile']['tmp_name']);
252 5f601060 Phil Davis
					if (!$data) {
253 dae4c487 Carlos Eduardo Ramos
						log_error(sprintf(gettext("Warning, could not read file %s"), $_FILES['conffile']['tmp_name']));
254 8ff5ffcc Matthew Grooms
						return 1;
255 9fd18b1f Scott Ullrich
					}
256 73ed069b Renato Botelho
257 8ff5ffcc Matthew Grooms
					if ($_POST['decrypt']) {
258
						if (!tagfile_deformat($data, $data, "config.xml")) {
259 205da5a0 Neriberto C.Prado
							$input_errors[] = gettext("The uploaded file does not appear to contain an encrypted pfsense configuration.");
260 8ff5ffcc Matthew Grooms
							return 1;
261
						}
262
						$data = decrypt_data($data, $_POST['decrypt_password']);
263
					}
264 73ed069b Renato Botelho
265 5f601060 Phil Davis
					if (stristr($data, "<m0n0wall>")) {
266 205da5a0 Neriberto C.Prado
						log_error(gettext("Upgrading m0n0wall configuration to pfsense."));
267 8ff5ffcc Matthew Grooms
						/* m0n0wall was found in config.  convert it. */
268
						$data = str_replace("m0n0wall", "pfsense", $data);
269
						$m0n0wall_upgrade = true;
270
					}
271 93867844 jim-p
272
					/* If the config on disk had empty rrddata tags, remove them to
273
					 * avoid an XML parsing error.
274
					 * See https://redmine.pfsense.org/issues/8994 */
275
					$data = preg_replace("/<rrddata><\\/rrddata>/", "", $data);
276
					$data = preg_replace("/<rrddata\\/>/", "", $data);
277
278 5f601060 Phil Davis
					if ($_POST['restorearea']) {
279 8ff5ffcc Matthew Grooms
						/* restore a specific area of the configuration */
280 5f601060 Phil Davis
						if (!stristr($data, "<" . $_POST['restorearea'] . ">")) {
281 b6078bed NOYB
							$input_errors[] = gettext("An area to restore was selected but the correct xml tag could not be located.");
282 8ff5ffcc Matthew Grooms
						} else {
283 8dcca9b5 Darren Embry
							if (!restore_config_section($_POST['restorearea'], $data)) {
284 b6078bed NOYB
								$input_errors[] = gettext("An area to restore was selected but the correct xml tag could not be located.");
285 8dcca9b5 Darren Embry
							} else {
286
								if ($config['rrddata']) {
287
									restore_rrddata();
288
									unset($config['rrddata']);
289
									unlink_if_exists("{$g['tmp_path']}/config.cache");
290 919a43a7 doktornotor
									write_config(sprintf(gettext("Unset RRD data from configuration after restoring %s configuration area"), $_POST['restorearea']));
291 8dcca9b5 Darren Embry
									convert_config();
292
								}
293
								filter_configure();
294 b6078bed NOYB
								$savemsg = gettext("The configuration area has been restored. The firewall may need to be rebooted.");
295 7eead1fc Darren Embry
							}
296 8ff5ffcc Matthew Grooms
						}
297 181462b5 Scott Ullrich
					} else {
298 5f601060 Phil Davis
						if (!stristr($data, "<" . $g['xml_rootobj'] . ">")) {
299 b6078bed NOYB
							$input_errors[] = sprintf(gettext("A full configuration restore was selected but a %s tag could not be located."), $g['xml_rootobj']);
300 8ff5ffcc Matthew Grooms
						} else {
301
							/* restore the entire configuration */
302
							file_put_contents($_FILES['conffile']['tmp_name'], $data);
303
							if (config_install($_FILES['conffile']['tmp_name']) == 0) {
304 0430b1b4 Renato Botelho
								/* Save current pkg repo to re-add on new config */
305
								unset($pkg_repo_conf_path);
306
								if (isset($config['system']['pkg_repo_conf_path'])) {
307
									$pkg_repo_conf_path = $config['system']['pkg_repo_conf_path'];
308
								}
309
310 8ff5ffcc Matthew Grooms
								/* this will be picked up by /index.php */
311 da17d77e Ermal Lu?i
								mark_subsystem_dirty("restore");
312 10511c3b Renato Botelho
								touch("/conf/needs_package_sync");
313 8ff5ffcc Matthew Grooms
								/* remove cache, we will force a config reboot */
314 5f601060 Phil Davis
								if (file_exists("{$g['tmp_path']}/config.cache")) {
315 da17d77e Ermal Lu?i
									unlink("{$g['tmp_path']}/config.cache");
316 5f601060 Phil Davis
								}
317 8ff5ffcc Matthew Grooms
								$config = parse_config(true);
318 0430b1b4 Renato Botelho
319
								/* Restore previously pkg repo configured */
320
								$pkg_repo_restored = false;
321
								if (isset($pkg_repo_conf_path)) {
322
									$config['system']['pkg_repo_conf_path'] =
323
									    $pkg_repo_conf_path;
324
									$pkg_repo_restored = true;
325
								} elseif (isset($config['system']['pkg_repo_conf_path'])) {
326
									unset($config['system']['pkg_repo_conf_path']);
327
									$pkg_repo_restored = true;
328
								}
329
330
								if ($pkg_repo_restored) {
331
									write_config(gettext("Removing pkg repository set after restoring full configuration"));
332 4be5ed9f Renato Botelho
									pkg_update(true);
333 0430b1b4 Renato Botelho
								}
334
335 0174c480 Ermal LUÇI
								if (file_exists("/boot/loader.conf")) {
336
									$loaderconf = file_get_contents("/boot/loader.conf");
337 c91af4ac Luiz Souza
									if (strpos($loaderconf, "console=\"comconsole") ||
338 e0b32eb9 jim-p
									    strpos($loaderconf, "boot_serial=\"YES")) {
339 4ff3adec Luiz Souza
										$config['system']['enableserial'] = true;
340
										write_config(gettext("Restore serial console enabling in configuration."));
341
									}
342
									unset($loaderconf);
343
								}
344
								if (file_exists("/boot/loader.conf.local")) {
345
									$loaderconf = file_get_contents("/boot/loader.conf.local");
346 c91af4ac Luiz Souza
									if (strpos($loaderconf, "console=\"comconsole") ||
347 e0b32eb9 jim-p
									    strpos($loaderconf, "boot_serial=\"YES")) {
348 0174c480 Ermal LUÇI
										$config['system']['enableserial'] = true;
349 ff30e319 bruno
										write_config(gettext("Restore serial console enabling in configuration."));
350 0174c480 Ermal LUÇI
									}
351 5d4b8830 Ermal LUÇI
									unset($loaderconf);
352 0174c480 Ermal LUÇI
								}
353 67bc955d Chris Buechler
								/* extract out rrd items, unset from $config when done */
354 5f601060 Phil Davis
								if ($config['rrddata']) {
355 754b75d0 Darren Embry
									restore_rrddata();
356 a2b8f7b2 Scott Ullrich
									unset($config['rrddata']);
357 da17d77e Ermal Lu?i
									unlink_if_exists("{$g['tmp_path']}/config.cache");
358 919a43a7 doktornotor
									write_config(gettext("Unset RRD data from configuration after restoring full configuration"));
359 d442e4e2 Scott Ullrich
									convert_config();
360 1390b049 Scott Ullrich
								}
361 5f601060 Phil Davis
								if ($m0n0wall_upgrade == true) {
362
									if ($config['system']['gateway'] <> "") {
363 8ff5ffcc Matthew Grooms
										$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
364 5f601060 Phil Davis
									}
365 8ff5ffcc Matthew Grooms
									unset($config['shaper']);
366
									/* optional if list */
367 80fe8369 Phil Davis
									$ifdescrs = get_configured_interface_list(true);
368 8ff5ffcc Matthew Grooms
									/* remove special characters from interface descriptions */
369 5f601060 Phil Davis
									if (is_array($ifdescrs)) {
370
										foreach ($ifdescrs as $iface) {
371 8ff5ffcc Matthew Grooms
											$config['interfaces'][$iface]['descr'] = remove_bad_chars($config['interfaces'][$iface]['descr']);
372 5f601060 Phil Davis
										}
373
									}
374 b6db8ea3 sullrich
									/* check for interface names with an alias */
375 5f601060 Phil Davis
									if (is_array($ifdescrs)) {
376
										foreach ($ifdescrs as $iface) {
377
											if (is_alias($config['interfaces'][$iface]['descr'])) {
378 b6db8ea3 sullrich
												$origname = $config['interfaces'][$iface]['descr'];
379 24807bfe Phil Davis
												update_alias_name($origname . "Alias", $origname);
380 b6db8ea3 sullrich
											}
381
										}
382
									}
383 da17d77e Ermal Lu?i
									unlink_if_exists("{$g['tmp_path']}/config.cache");
384 888e7a27 Scott Ullrich
									// Reset configuration version to something low
385 a250f179 Renato Botelho
									// in order to force the config upgrade code to
386 888e7a27 Scott Ullrich
									// run through with all steps that are required.
387
									$config['system']['version'] = "1.0";
388 798cb31a Scott Ullrich
									// Deal with descriptions longer than 63 characters
389
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
390 5f601060 Phil Davis
										if (count($config['filter']['rule'][$i]['descr']) > 63) {
391 798cb31a Scott Ullrich
											$config['filter']['rule'][$i]['descr'] = substr($config['filter']['rule'][$i]['descr'], 0, 63);
392 5f601060 Phil Davis
										}
393 798cb31a Scott Ullrich
									}
394
									// Move interface from ipsec to enc0
395
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
396 5f601060 Phil Davis
										if ($config['filter']['rule'][$i]['interface'] == "ipsec") {
397 798cb31a Scott Ullrich
											$config['filter']['rule'][$i]['interface'] = "enc0";
398 5f601060 Phil Davis
										}
399 798cb31a Scott Ullrich
									}
400
									// Convert icmp types
401
									// http://www.openbsd.org/cgi-bin/man.cgi?query=icmp&sektion=4&arch=i386&apropos=0&manpath=OpenBSD+Current
402 0ce1667b stilez
									$convert = array('echo' => 'echoreq', 'timest' => 'timereq', 'timestrep' => 'timerep');
403
									foreach ($config["filter"]["rule"] as $ruleid => &$ruledata) {
404
										if ($convert[$ruledata['icmptype']]) {
405
											$ruledata['icmptype'] = $convert[$ruledata['icmptype']];
406 798cb31a Scott Ullrich
										}
407 dec5cb85 Scott Ullrich
									}
408
									$config['diag']['ipv6nat'] = true;
409 919a43a7 doktornotor
									write_config(gettext("Imported m0n0wall configuration"));
410 d442e4e2 Scott Ullrich
									convert_config();
411 205da5a0 Neriberto C.Prado
									$savemsg = gettext("The m0n0wall configuration has been restored and upgraded to pfSense.");
412 da17d77e Ermal Lu?i
									mark_subsystem_dirty("restore");
413 8ff5ffcc Matthew Grooms
								}
414 5f601060 Phil Davis
								if (is_array($config['captiveportal'])) {
415
									foreach ($config['captiveportal'] as $cp) {
416 2e19a683 Renato Botelho
										if (isset($cp['enable'])) {
417
											/* for some reason ipfw doesn't init correctly except on bootup sequence */
418
											mark_subsystem_dirty("restore");
419
											break;
420
										}
421
									}
422 8ff5ffcc Matthew Grooms
								}
423 749dfdb7 Luiz Souza
								console_configure();
424 5f601060 Phil Davis
								if (is_interface_mismatch() == true) {
425 8ff5ffcc Matthew Grooms
									touch("/var/run/interface_mismatch_reboot_needed");
426 da17d77e Ermal Lu?i
									clear_subsystem_dirty("restore");
427 f5a57bb0 Scott Ullrich
									convert_config();
428 8ff5ffcc Matthew Grooms
									header("Location: interfaces_assign.php");
429 bafaf123 Scott Ullrich
									exit;
430 8ff5ffcc Matthew Grooms
								}
431 66bcba1b Ermal
								if (is_interface_vlan_mismatch() == true) {
432
									touch("/var/run/interface_mismatch_reboot_needed");
433
									clear_subsystem_dirty("restore");
434
									convert_config();
435
									header("Location: interfaces_assign.php");
436
									exit;
437
								}
438 8ff5ffcc Matthew Grooms
							} else {
439 205da5a0 Neriberto C.Prado
								$input_errors[] = gettext("The configuration could not be restored.");
440 db3996df Scott Ullrich
							}
441 9c72b993 Scott Ullrich
						}
442 181462b5 Scott Ullrich
					}
443 8ff5ffcc Matthew Grooms
				} else {
444 205da5a0 Neriberto C.Prado
					$input_errors[] = gettext("The configuration could not be restored (file upload error).");
445 db3996df Scott Ullrich
				}
446 5b237745 Scott Ullrich
			}
447 8ff5ffcc Matthew Grooms
		}
448 73ed069b Renato Botelho
449 8ff5ffcc Matthew Grooms
		if ($mode == "reinstallpackages") {
450 8ccc8f1a Scott Ullrich
			header("Location: pkg_mgr_install.php?mode=reinstallall");
451
			exit;
452 039cb920 jim-p
		} else if ($mode == "clearpackagelock") {
453
			clear_subsystem_dirty('packagelock');
454 8545adde k-paulius
			$savemsg = "Package lock cleared.";
455 5b237745 Scott Ullrich
		}
456
	}
457
}
458 6a1e6651 Bill Marquette
459 02e1170d Scott Ullrich
$id = rand() . '.' . time();
460
461
$mth = ini_get('upload_progress_meter.store_method');
462
$dir = ini_get('upload_progress_meter.file.filename_template');
463
464 a4af095c Renato Botelho
function build_area_list($showall) {
465
	global $config;
466 b63695db Scott Ullrich
467 0e834fc5 Stephen Beaver
	$areas = array(
468
		"aliases" => gettext("Aliases"),
469 a4af095c Renato Botelho
		"captiveportal" => gettext("Captive Portal"),
470
		"voucher" => gettext("Captive Portal Vouchers"),
471
		"dnsmasq" => gettext("DNS Forwarder"),
472
		"unbound" => gettext("DNS Resolver"),
473
		"dhcpd" => gettext("DHCP Server"),
474
		"dhcpdv6" => gettext("DHCPv6 Server"),
475
		"filter" => gettext("Firewall Rules"),
476
		"interfaces" => gettext("Interfaces"),
477
		"ipsec" => gettext("IPSEC"),
478
		"nat" => gettext("NAT"),
479
		"openvpn" => gettext("OpenVPN"),
480
		"installedpackages" => gettext("Package Manager"),
481
		"rrddata" => gettext("RRD Data"),
482
		"cron" => gettext("Scheduled Tasks"),
483
		"syslog" => gettext("Syslog"),
484
		"system" => gettext("System"),
485
		"staticroutes" => gettext("Static routes"),
486
		"sysctl" => gettext("System tunables"),
487
		"snmpd" => gettext("SNMP Server"),
488
		"shaper" => gettext("Traffic Shaper"),
489
		"vlans" => gettext("VLANS"),
490 7ca42d47 k-paulius
		"wol" => gettext("Wake-on-LAN")
491 a4af095c Renato Botelho
		);
492 0e834fc5 Stephen Beaver
493
	$list = array("" => gettext("All"));
494
495 288a2a0f Phil Davis
	if ($showall) {
496 0e834fc5 Stephen Beaver
		return($list + $areas);
497 288a2a0f Phil Davis
	} else {
498 a4af095c Renato Botelho
		foreach ($areas as $area => $areaname) {
499
			if ($area === "rrddata" || check_and_returnif_section_exists($area) == true) {
500
				$list[$area] = $areaname;
501
			}
502
		}
503 8ff5ffcc Matthew Grooms
504 0e834fc5 Stephen Beaver
		return($list);
505 5f601060 Phil Davis
	}
506 8ff5ffcc Matthew Grooms
}
507
508 0f298138 k-paulius
$pgtitle = array(gettext("Diagnostics"), htmlspecialchars(gettext("Backup & Restore")), htmlspecialchars(gettext("Backup & Restore")));
509 edcd7535 Phil Davis
$pglinks = array("", "@self", "@self");
510 b63695db Scott Ullrich
include("head.inc");
511
512 947141fd Phil Davis
if ($input_errors) {
513 a4af095c Renato Botelho
	print_input_errors($input_errors);
514 947141fd Phil Davis
}
515 0e834fc5 Stephen Beaver
516 947141fd Phil Davis
if ($savemsg) {
517 a4af095c Renato Botelho
	print_info_box($savemsg, 'success');
518 947141fd Phil Davis
}
519 0e834fc5 Stephen Beaver
520 a4af095c Renato Botelho
if (is_subsystem_dirty('restore')):
521 5b237745 Scott Ullrich
?>
522 a4af095c Renato Botelho
	<br/>
523 1af5edbf Stephen Beaver
	<form action="diag_reboot.php" method="post">
524 7d5b007c Sjon Hortensius
		<input name="Submit" type="hidden" value="Yes" />
525 f6aebbcc NewEraCracker
		<?php print_info_box(gettext("The firewall configuration has been changed.") . "<br />" . gettext("The firewall is now rebooting.")); ?>
526 a4af095c Renato Botelho
		<br />
527 7d5b007c Sjon Hortensius
	</form>
528 0e834fc5 Stephen Beaver
<?php
529 a4af095c Renato Botelho
endif;
530
531
$tab_array = array();
532 0f298138 k-paulius
$tab_array[] = array(htmlspecialchars(gettext("Backup & Restore")), true, "diag_backup.php");
533 a4af095c Renato Botelho
$tab_array[] = array(gettext("Config History"), false, "diag_confbak.php");
534
display_top_tabs($tab_array);
535
536
$form = new Form(false);
537 3beeb66a Stephen Beaver
$form->setMultipartEncoding();	// Allow file uploads
538 a4af095c Renato Botelho
539 5f88f964 k-paulius
$section = new Form_Section('Backup Configuration');
540 a4af095c Renato Botelho
541
$section->addInput(new Form_Select(
542
	'backuparea',
543
	'Backup area',
544
	'',
545
	build_area_list(false)
546
));
547
548
$section->addInput(new Form_Checkbox(
549
	'nopackages',
550
	'Skip packages',
551
	'Do not backup package information.',
552
	false
553
));
554
555
$section->addInput(new Form_Checkbox(
556
	'donotbackuprrd',
557
	'Skip RRD data',
558
	'Do not backup RRD data (NOTE: RRD Data can consume 4+ megabytes of config.xml space!)',
559
	true
560
));
561
562
$section->addInput(new Form_Checkbox(
563
	'encrypt',
564
	'Encryption',
565
	'Encrypt this configuration file.',
566
	false
567 7c0f116b Jared Dillard
));
568 a4af095c Renato Botelho
569 76d6d925 Stephen Beaver
$section->addInput(new Form_Input(
570 a4af095c Renato Botelho
	'encrypt_password',
571 c8b10b4c Stephen Beaver
	'Password',
572 a4af095c Renato Botelho
	'password',
573 c8b10b4c Stephen Beaver
	null
574 7c0f116b Jared Dillard
));
575 a4af095c Renato Botelho
576
$group = new Form_Group('');
577 1d80d714 NOYB
// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
578 a4af095c Renato Botelho
$group->add(new Form_Button(
579 9a7e1c95 Phil Davis
	'download',
580 faab522f Renato Botelho
	'Download configuration as XML',
581 37676f4e jim-p
	null,
582
	'fa-download'
583
))->setAttribute('id')->addClass('btn-primary');
584 a4af095c Renato Botelho
585
$section->add($group);
586
$form->add($section);
587
588 5f88f964 k-paulius
$section = new Form_Section('Restore Backup');
589 a4af095c Renato Botelho
590
$section->addInput(new Form_StaticText(
591
	null,
592 52e416cc Phil Davis
	sprintf(gettext("Open a %s configuration XML file and click the button below to restore the configuration."), $g['product_name'])
593 a4af095c Renato Botelho
));
594
595
$section->addInput(new Form_Select(
596
	'restorearea',
597
	'Restore area',
598
	'',
599 ce3eca6e Phil Davis
	build_area_list(true)
600 a4af095c Renato Botelho
));
601
602
$section->addInput(new Form_Input(
603
	'conffile',
604
	'Configuration file',
605
	'file',
606
	null
607
));
608
609
$section->addInput(new Form_Checkbox(
610 7c0f116b Jared Dillard
	'decrypt',
611 a4af095c Renato Botelho
	'Encryption',
612 7c0f116b Jared Dillard
	'Configuration file is encrypted.',
613 a4af095c Renato Botelho
	false
614 7c0f116b Jared Dillard
));
615 a4af095c Renato Botelho
616 76d6d925 Stephen Beaver
$section->addInput(new Form_Input(
617 a4af095c Renato Botelho
	'decrypt_password',
618 c8b10b4c Stephen Beaver
	'Password',
619 a4af095c Renato Botelho
	'password',
620
	null,
621
	['placeholder' => 'Password']
622 7c0f116b Jared Dillard
));
623 a4af095c Renato Botelho
624
$group = new Form_Group('');
625 1d80d714 NOYB
// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
626 a4af095c Renato Botelho
$group->add(new Form_Button(
627 9a7e1c95 Phil Davis
	'restore',
628 faab522f Renato Botelho
	'Restore Configuration',
629 37676f4e jim-p
	null,
630
	'fa-undo'
631
))->setHelp('The firewall will reboot after restoring the configuration.')->addClass('btn-danger restore')->setAttribute('id');
632 a4af095c Renato Botelho
633
$section->add($group);
634
635
$form->add($section);
636
637
if (($config['installedpackages']['package'] != "") || (is_subsystem_dirty("packagelock"))) {
638 5f88f964 k-paulius
	$section = new Form_Section('Package Functions');
639 0e834fc5 Stephen Beaver
640 a4af095c Renato Botelho
	if ($config['installedpackages']['package'] != "") {
641
		$group = new Form_Group('');
642 1d80d714 NOYB
		// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
643 a4af095c Renato Botelho
		$group->add(new Form_Button(
644 9a7e1c95 Phil Davis
			'reinstallpackages',
645 faab522f Renato Botelho
			'Reinstall Packages',
646 37676f4e jim-p
			null,
647
			'fa-retweet'
648
		))->setHelp('Click this button to reinstall all system packages.  This may take a while.')->addClass('btn-success')->setAttribute('id');
649 0e834fc5 Stephen Beaver
650 a4af095c Renato Botelho
		$section->add($group);
651 7eead1fc Darren Embry
	}
652 0e834fc5 Stephen Beaver
653 a4af095c Renato Botelho
	if (is_subsystem_dirty("packagelock")) {
654
		$group = new Form_Group('');
655 1d80d714 NOYB
		// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
656 a4af095c Renato Botelho
		$group->add(new Form_Button(
657 9a7e1c95 Phil Davis
			'clearpackagelock',
658 faab522f Renato Botelho
			'Clear Package Lock',
659 37676f4e jim-p
			null,
660
			'fa-wrench'
661
		))->setHelp('Click this button to clear the package lock if a package fails to reinstall properly after an upgrade.')->addClass('btn-warning')->setAttribute('id');
662 0e834fc5 Stephen Beaver
663 a4af095c Renato Botelho
		$section->add($group);
664
	}
665 0e834fc5 Stephen Beaver
666 a4af095c Renato Botelho
	$form->add($section);
667 a1003d57 Ermal Lu?i
}
668 a4af095c Renato Botelho
669
print($form);
670 7c0f116b Jared Dillard
?>
671
<script type="text/javascript">
672
//<![CDATA[
673 947141fd Phil Davis
events.push(function() {
674 7c0f116b Jared Dillard
675
	// ------- Show/hide sections based on checkbox settings --------------------------------------
676 0da0d43e Phil Davis
677 7c0f116b Jared Dillard
	function hideSections(hide) {
678
		hidePasswords();
679
	}
680
681
	function hidePasswords() {
682 3beeb66a Stephen Beaver
683 7c0f116b Jared Dillard
		encryptHide = !($('input[name="encrypt"]').is(':checked'));
684
		decryptHide = !($('input[name="decrypt"]').is(':checked'));
685
686
		hideInput('encrypt_password', encryptHide);
687 c8b10b4c Stephen Beaver
		hideInput('encrypt_password_confirm', encryptHide);
688 7c0f116b Jared Dillard
		hideInput('decrypt_password', decryptHide);
689 c8b10b4c Stephen Beaver
		hideInput('decrypt_password_confirm', decryptHide);
690 7c0f116b Jared Dillard
	}
691
692 ff59b884 Stephen Beaver
	// ---------- Click handlers ------------------------------------------------------------------
693 7c0f116b Jared Dillard
694
	$('input[name="encrypt"]').on('change', function() {
695
		hidePasswords();
696
	});
697
698
	$('input[name="decrypt"]').on('change', function() {
699
		hidePasswords();
700 3beeb66a Stephen Beaver
	});
701 7c0f116b Jared Dillard
702 947141fd Phil Davis
	$('#conffile').change(function () {
703 40f146c5 Phil Davis
		if (document.getElementById("conffile").value) {
704
			$('.restore').prop('disabled', false);
705
		} else {
706
			$('.restore').prop('disabled', true);
707
		}
708 ff59b884 Stephen Beaver
    });
709 eef93144 Jared Dillard
	// ---------- On initial page load ------------------------------------------------------------
710
711 7c0f116b Jared Dillard
	hideSections();
712 ff59b884 Stephen Beaver
	$('.restore').prop('disabled', true);
713 7c0f116b Jared Dillard
});
714
//]]>
715
</script>
716
717
<?php
718 a4af095c Renato Botelho
include("foot.inc");
719 7a7f3af1 sullrich
720 5f601060 Phil Davis
if (is_subsystem_dirty('restore')) {
721 cf9a4467 jim-p
	system_reboot();
722 c9d46a8e Renato Botelho
}