Project

General

Profile

Download (26.2 KB) Statistics
| Branch: | Tag: | Revision:
1 8ccc8f1a Scott Ullrich
<?php
2 5b237745 Scott Ullrich
/*
3 aaec5634 Renato Botelho
 * diag_backup.php
4 9da2cf1c Stephen Beaver
 *
5 aaec5634 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
7
 * All rights reserved.
8 fd9ebcd5 Stephen Beaver
 *
9 aaec5634 Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * All rights reserved.
12 fd9ebcd5 Stephen Beaver
 *
13 aaec5634 Renato Botelho
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15 fd9ebcd5 Stephen Beaver
 *
16 aaec5634 Renato Botelho
 * 1. Redistributions of source code must retain the above copyright notice,
17
 *    this list of conditions and the following disclaimer.
18 fd9ebcd5 Stephen Beaver
 *
19 aaec5634 Renato Botelho
 * 2. Redistributions in binary form must reproduce the above copyright
20
 *    notice, this list of conditions and the following disclaimer in
21
 *    the documentation and/or other materials provided with the
22
 *    distribution.
23 fd9ebcd5 Stephen Beaver
 *
24 aaec5634 Renato Botelho
 * 3. All advertising materials mentioning features or use of this software
25
 *    must display the following acknowledgment:
26
 *    "This product includes software developed by the pfSense Project
27
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
28 fd9ebcd5 Stephen Beaver
 *
29 aaec5634 Renato Botelho
 * 4. The names "pfSense" and "pfSense Project" must not be used to
30
 *    endorse or promote products derived from this software without
31
 *    prior written permission. For written permission, please contact
32
 *    coreteam@pfsense.org.
33 fd9ebcd5 Stephen Beaver
 *
34 aaec5634 Renato Botelho
 * 5. Products derived from this software may not be called "pfSense"
35
 *    nor may "pfSense" appear in their names without prior written
36
 *    permission of the Electric Sheep Fencing, LLC.
37 fd9ebcd5 Stephen Beaver
 *
38 aaec5634 Renato Botelho
 * 6. Redistributions of any form whatsoever must retain the following
39
 *    acknowledgment:
40 0da0d43e Phil Davis
 *
41 aaec5634 Renato Botelho
 * "This product includes software developed by the pfSense Project
42
 * for use in the pfSense software distribution (http://www.pfsense.org/).
43 fd9ebcd5 Stephen Beaver
 *
44 aaec5634 Renato Botelho
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
45
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
48
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55
 * OF THE POSSIBILITY OF SUCH DAMAGE.
56 fd9ebcd5 Stephen Beaver
 */
57 5b237745 Scott Ullrich
58 6b07c15a Matthew Grooms
##|+PRIV
59 5d916fc7 Stephen Beaver
##|*IDENT=page-diagnostics-backup-restore
60 0f298138 k-paulius
##|*NAME=Diagnostics: Backup & Restore
61
##|*DESCR=Allow access to the 'Diagnostics: Backup & Restore' page.
62 6b07c15a Matthew Grooms
##|*MATCH=diag_backup.php*
63
##|-PRIV
64
65 47d11b79 Mark Crane
/* Allow additional execution time 0 = no limit. */
66 3420028c Scott Ullrich
ini_set('max_execution_time', '0');
67
ini_set('max_input_time', '0');
68 47d11b79 Mark Crane
69 5b237745 Scott Ullrich
/* omit no-cache headers because it confuses IE with file downloads */
70
$omit_nocacheheaders = true;
71 aceaf18c Phil Davis
require_once("guiconfig.inc");
72 7a927e67 Scott Ullrich
require_once("functions.inc");
73
require_once("filter.inc");
74
require_once("shaper.inc");
75 5b237745 Scott Ullrich
76 8bdb6879 Darren Embry
$rrddbpath = "/var/db/rrd";
77
$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
78
79
function rrd_data_xml() {
80
	global $rrddbpath;
81
	global $rrdtool;
82 73ed069b Renato Botelho
83 8bdb6879 Darren Embry
	$result = "\t<rrddata>\n";
84
	$rrd_files = glob("{$rrddbpath}/*.rrd");
85
	$xml_files = array();
86
	foreach ($rrd_files as $rrd_file) {
87
		$basename = basename($rrd_file);
88
		$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
89
		exec("$rrdtool dump '{$rrd_file}' '{$xml_file}'");
90
		$xml_data = file_get_contents($xml_file);
91
		unlink($xml_file);
92
		if ($xml_data !== false) {
93
			$result .= "\t\t<rrddatafile>\n";
94
			$result .= "\t\t\t<filename>{$basename}</filename>\n";
95
			$result .= "\t\t\t<xmldata>" . base64_encode(gzdeflate($xml_data)) . "</xmldata>\n";
96
			$result .= "\t\t</rrddatafile>\n";
97
		}
98
	}
99
	$result .= "\t</rrddata>\n";
100
	return $result;
101
}
102
103 754b75d0 Darren Embry
function restore_rrddata() {
104 7eead1fc Darren Embry
	global $config, $g, $rrdtool, $input_errors;
105 5f601060 Phil Davis
	foreach ($config['rrddata']['rrddatafile'] as $rrd) {
106 754b75d0 Darren Embry
		if ($rrd['xmldata']) {
107
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
108
			$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
109 08877746 Darren Embry
			if (file_put_contents($xml_file, gzinflate(base64_decode($rrd['xmldata']))) === false) {
110 ff30e319 bruno
				log_error(sprintf(gettext("Cannot write %s"), $xml_file));
111 08877746 Darren Embry
				continue;
112
			}
113
			$output = array();
114
			$status = null;
115
			exec("$rrdtool restore -f '{$xml_file}' '{$rrd_file}'", $output, $status);
116
			if ($status) {
117 f757431d Darren Embry
				log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
118 08877746 Darren Embry
				continue;
119
			}
120 754b75d0 Darren Embry
			unlink($xml_file);
121 5f601060 Phil Davis
		} else if ($rrd['data']) {
122 7eead1fc Darren Embry
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
123
			$rrd_fd = fopen($rrd_file, "w");
124 08877746 Darren Embry
			if (!$rrd_fd) {
125 ff30e319 bruno
				log_error(sprintf(gettext("Cannot write %s"), $rrd_file));
126 08877746 Darren Embry
				continue;
127
			}
128 754b75d0 Darren Embry
			$data = base64_decode($rrd['data']);
129
			/* Try to decompress the data. */
130
			$dcomp = @gzinflate($data);
131
			if ($dcomp) {
132
				/* If the decompression worked, write the decompressed data */
133 08877746 Darren Embry
				if (fwrite($rrd_fd, $dcomp) === false) {
134 ff30e319 bruno
					log_error(sprintf(gettext("fwrite %s failed"), $rrd_file));
135 08877746 Darren Embry
					continue;
136
				}
137 754b75d0 Darren Embry
			} else {
138
				/* If the decompression failed, it wasn't compressed, so write raw data */
139 08877746 Darren Embry
				if (fwrite($rrd_fd, $data) === false) {
140 ff30e319 bruno
					log_error(sprintf(gettext("fwrite %s failed"), $rrd_file));
141 08877746 Darren Embry
					continue;
142
				}
143
			}
144
			if (fclose($rrd_fd) === false) {
145 ff30e319 bruno
				log_error(sprintf(gettext("fclose %s failed"), $rrd_file));
146 08877746 Darren Embry
				continue;
147 754b75d0 Darren Embry
			}
148
		}
149
	}
150
}
151
152 3420028c Scott Ullrich
function add_base_packages_menu_items() {
153
	global $g, $config;
154 52c7f0d4 jim-p
	$base_packages = explode(",", $g['base_packages']);
155 3420028c Scott Ullrich
	$modified_config = false;
156 5f601060 Phil Davis
	foreach ($base_packages as $bp) {
157 da17d77e Ermal Lu?i
		$basepkg_path = "/usr/local/pkg/{$bp}";
158 a250f179 Renato Botelho
		$tmpinfo = pathinfo($basepkg_path, PATHINFO_EXTENSION);
159 5f601060 Phil Davis
		if ($tmpinfo['extension'] == "xml" && file_exists($basepkg_path)) {
160 da17d77e Ermal Lu?i
			$pkg_config = parse_xml_config_pkg($basepkg_path, "packagegui");
161 5f601060 Phil Davis
			if ($pkg_config['menu'] != "") {
162
				if (is_array($pkg_config['menu'])) {
163
					foreach ($pkg_config['menu'] as $menu) {
164
						if (is_array($config['installedpackages']['menu'])) {
165
							foreach ($config['installedpackages']['menu'] as $amenu) {
166
								if ($amenu['name'] == $menu['name']) {
167 3420028c Scott Ullrich
									continue;
168 5f601060 Phil Davis
								}
169
							}
170
						}
171 3420028c Scott Ullrich
						$config['installedpackages']['menu'][] = $menu;
172
						$modified_config = true;
173
					}
174
				}
175
			}
176
		}
177
	}
178 5f601060 Phil Davis
	if ($modified_config) {
179 f3ceee6e Vinicius Coque
		write_config(gettext("Restored base_package menus after configuration restore."));
180 3420028c Scott Ullrich
		$config = parse_config(true);
181
	}
182
}
183
184 645ec835 Scott Ullrich
function remove_bad_chars($string) {
185 699737d9 Phil Davis
	return preg_replace('/[^a-z_0-9]/i', '', $string);
186 645ec835 Scott Ullrich
}
187
188 b3277798 Scott Ullrich
function check_and_returnif_section_exists($section) {
189
	global $config;
190 5f601060 Phil Davis
	if (is_array($config[$section])) {
191 b3277798 Scott Ullrich
		return true;
192 5f601060 Phil Davis
	}
193 b3277798 Scott Ullrich
	return false;
194
}
195
196 da17d77e Ermal Lu?i
if ($_POST['apply']) {
197 73ed069b Renato Botelho
	ob_flush();
198
	flush();
199 c9a19238 Darren Embry
	conf_mount_rw();
200
	clear_subsystem_dirty("restore");
201
	conf_mount_ro();
202 73ed069b Renato Botelho
	exit;
203 da17d77e Ermal Lu?i
}
204
205 5b237745 Scott Ullrich
if ($_POST) {
206
	unset($input_errors);
207 5f601060 Phil Davis
	if (stristr($_POST['Submit'], gettext("Restore configuration"))) {
208 5b237745 Scott Ullrich
		$mode = "restore";
209 5f601060 Phil Davis
	} else if (stristr($_POST['Submit'], gettext("Reinstall"))) {
210 8ccc8f1a Scott Ullrich
		$mode = "reinstallpackages";
211 5f601060 Phil Davis
	} else if (stristr($_POST['Submit'], gettext("Clear Package Lock"))) {
212 039cb920 jim-p
		$mode = "clearpackagelock";
213 5f601060 Phil Davis
	} else if (stristr($_POST['Submit'], gettext("Download"))) {
214 5b237745 Scott Ullrich
		$mode = "download";
215 5f601060 Phil Davis
	} else if (stristr($_POST['Submit'], gettext("Restore version"))) {
216 7cf03119 Bill Marquette
		$mode = "restore_ver";
217 5f601060 Phil Davis
	}
218
	if ($_POST["nopackages"] <> "") {
219 528cad39 Colin Smith
		$options = "nopackages";
220 5f601060 Phil Davis
	}
221
	if ($_POST["ver"] <> "") {
222 7cf03119 Bill Marquette
		$ver2restore = $_POST["ver"];
223 5f601060 Phil Davis
	}
224 5b237745 Scott Ullrich
	if ($mode) {
225
		if ($mode == "download") {
226 8ff5ffcc Matthew Grooms
			if ($_POST['encrypt']) {
227 76d6d925 Stephen Beaver
				if (!$_POST['encrypt_password']) {
228 9b6ea44e Stephen Beaver
					$input_errors[] = gettext("A password for encryption must be supplied and confirmed.");
229 5f601060 Phil Davis
				}
230 528cad39 Colin Smith
			}
231 73ed069b Renato Botelho
232 8ff5ffcc Matthew Grooms
			if (!$input_errors) {
233 73ed069b Renato Botelho
234 07b1797b sullrich
				//$lockbckp = lock('config');
235 73ed069b Renato Botelho
236 8ff5ffcc Matthew Grooms
				$host = "{$config['system']['hostname']}.{$config['system']['domain']}";
237
				$name = "config-{$host}-".date("YmdHis").".xml";
238
				$data = "";
239 73ed069b Renato Botelho
240 5f601060 Phil Davis
				if ($options == "nopackages") {
241
					if (!$_POST['backuparea']) {
242 4195c967 sullrich
						/* backup entire configuration */
243
						$data = file_get_contents("{$g['conf_path']}/config.xml");
244
					} else {
245
						/* backup specific area of configuration */
246
						$data = backup_config_section($_POST['backuparea']);
247
						$name = "{$_POST['backuparea']}-{$name}";
248
					}
249 da17d77e Ermal Lu?i
					$sfn = "{$g['tmp_path']}/config.xml.nopkg";
250 4195c967 sullrich
					file_put_contents($sfn, $data);
251
					exec("sed '/<installedpackages>/,/<\/installedpackages>/d' {$sfn} > {$sfn}-new");
252 a250f179 Renato Botelho
					$data = file_get_contents($sfn . "-new");
253 8ff5ffcc Matthew Grooms
				} else {
254 5f601060 Phil Davis
					if (!$_POST['backuparea']) {
255 8ff5ffcc Matthew Grooms
						/* backup entire configuration */
256
						$data = file_get_contents("{$g['conf_path']}/config.xml");
257 7eead1fc Darren Embry
					} else if ($_POST['backuparea'] === "rrddata") {
258
						$data = rrd_data_xml();
259
						$name = "{$_POST['backuparea']}-{$name}";
260 8ff5ffcc Matthew Grooms
					} else {
261
						/* backup specific area of configuration */
262
						$data = backup_config_section($_POST['backuparea']);
263
						$name = "{$_POST['backuparea']}-{$name}";
264
					}
265 181462b5 Scott Ullrich
				}
266 73ed069b Renato Botelho
267 07b1797b sullrich
				//unlock($lockbckp);
268 73ed069b Renato Botelho
269 a250f179 Renato Botelho
				/*
270 0e834fc5 Stephen Beaver
				 *	Backup RRD Data
271 1390b049 Scott Ullrich
				 */
272 7eead1fc Darren Embry
				if ($_POST['backuparea'] !== "rrddata" && !$_POST['donotbackuprrd']) {
273 8bdb6879 Darren Embry
					$rrd_data_xml = rrd_data_xml();
274
					$closing_tag = "</" . $g['xml_rootobj'] . ">";
275
					$data = str_replace($closing_tag, $rrd_data_xml . $closing_tag, $data);
276 1390b049 Scott Ullrich
				}
277 73ed069b Renato Botelho
278 4cfd2390 Renato Botelho
				if ($_POST['encrypt']) {
279
					$data = encrypt_data($data, $_POST['encrypt_password']);
280
					tagfile_reformat($data, $data, "config.xml");
281
				}
282
283 8ff5ffcc Matthew Grooms
				$size = strlen($data);
284
				header("Content-Type: application/octet-stream");
285
				header("Content-Disposition: attachment; filename={$name}");
286 a8c35980 Scott Ullrich
				header("Content-Length: $size");
287 e77ea573 jim-p
				if (isset($_SERVER['HTTPS'])) {
288
					header('Pragma: ');
289
					header('Cache-Control: ');
290
				} else {
291
					header("Pragma: private");
292
					header("Cache-Control: private, must-revalidate");
293
				}
294 8ff5ffcc Matthew Grooms
				echo $data;
295 73ed069b Renato Botelho
296 8ff5ffcc Matthew Grooms
				exit;
297
			}
298
		}
299 73ed069b Renato Botelho
300 8ff5ffcc Matthew Grooms
		if ($mode == "restore") {
301
			if ($_POST['decrypt']) {
302 76d6d925 Stephen Beaver
				if (!$_POST['decrypt_password']) {
303 9b6ea44e Stephen Beaver
					$input_errors[] = gettext("A password for decryption must be supplied and confirmed.");
304 5f601060 Phil Davis
				}
305 8ff5ffcc Matthew Grooms
			}
306 73ed069b Renato Botelho
307 8ff5ffcc Matthew Grooms
			if (!$input_errors) {
308
				if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
309 73ed069b Renato Botelho
310 8ff5ffcc Matthew Grooms
					/* read the file contents */
311
					$data = file_get_contents($_FILES['conffile']['tmp_name']);
312 5f601060 Phil Davis
					if (!$data) {
313 dae4c487 Carlos Eduardo Ramos
						log_error(sprintf(gettext("Warning, could not read file %s"), $_FILES['conffile']['tmp_name']));
314 8ff5ffcc Matthew Grooms
						return 1;
315 9fd18b1f Scott Ullrich
					}
316 73ed069b Renato Botelho
317 8ff5ffcc Matthew Grooms
					if ($_POST['decrypt']) {
318
						if (!tagfile_deformat($data, $data, "config.xml")) {
319 205da5a0 Neriberto C.Prado
							$input_errors[] = gettext("The uploaded file does not appear to contain an encrypted pfsense configuration.");
320 8ff5ffcc Matthew Grooms
							return 1;
321
						}
322
						$data = decrypt_data($data, $_POST['decrypt_password']);
323
					}
324 73ed069b Renato Botelho
325 5f601060 Phil Davis
					if (stristr($data, "<m0n0wall>")) {
326 205da5a0 Neriberto C.Prado
						log_error(gettext("Upgrading m0n0wall configuration to pfsense."));
327 8ff5ffcc Matthew Grooms
						/* m0n0wall was found in config.  convert it. */
328
						$data = str_replace("m0n0wall", "pfsense", $data);
329
						$m0n0wall_upgrade = true;
330
					}
331 5f601060 Phil Davis
					if ($_POST['restorearea']) {
332 8ff5ffcc Matthew Grooms
						/* restore a specific area of the configuration */
333 5f601060 Phil Davis
						if (!stristr($data, "<" . $_POST['restorearea'] . ">")) {
334 ce871619 Stephen Beaver
							$input_errors[] = gettext("An area to restore was selected but the correct xml tag could not be located.");
335 8ff5ffcc Matthew Grooms
						} else {
336 8dcca9b5 Darren Embry
							if (!restore_config_section($_POST['restorearea'], $data)) {
337 ce871619 Stephen Beaver
								$input_errors[] = gettext("An area to restore was selected but the correct xml tag could not be located.");
338 8dcca9b5 Darren Embry
							} else {
339
								if ($config['rrddata']) {
340
									restore_rrddata();
341
									unset($config['rrddata']);
342
									unlink_if_exists("{$g['tmp_path']}/config.cache");
343
									write_config();
344
									add_base_packages_menu_items();
345
									convert_config();
346
									conf_mount_ro();
347
								}
348
								filter_configure();
349 ce871619 Stephen Beaver
								$savemsg = gettext("The configuration area has been restored. The firewall may need to be rebooted.");
350 7eead1fc Darren Embry
							}
351 8ff5ffcc Matthew Grooms
						}
352 181462b5 Scott Ullrich
					} else {
353 5f601060 Phil Davis
						if (!stristr($data, "<" . $g['xml_rootobj'] . ">")) {
354 ce871619 Stephen Beaver
							$input_errors[] = sprintf(gettext("A full configuration restore was selected but a %s tag could not be located."), $g['xml_rootobj']);
355 8ff5ffcc Matthew Grooms
						} else {
356
							/* restore the entire configuration */
357
							file_put_contents($_FILES['conffile']['tmp_name'], $data);
358
							if (config_install($_FILES['conffile']['tmp_name']) == 0) {
359
								/* this will be picked up by /index.php */
360
								conf_mount_rw();
361 da17d77e Ermal Lu?i
								mark_subsystem_dirty("restore");
362 e6068346 Phil Davis
								touch("/conf/needs_package_sync_after_reboot");
363 8ff5ffcc Matthew Grooms
								/* remove cache, we will force a config reboot */
364 5f601060 Phil Davis
								if (file_exists("{$g['tmp_path']}/config.cache")) {
365 da17d77e Ermal Lu?i
									unlink("{$g['tmp_path']}/config.cache");
366 5f601060 Phil Davis
								}
367 8ff5ffcc Matthew Grooms
								$config = parse_config(true);
368 0174c480 Ermal LUÇI
								if (file_exists("/boot/loader.conf")) {
369
									$loaderconf = file_get_contents("/boot/loader.conf");
370 08af94cb Chris Buechler
									if (strpos($loaderconf, "console=\"comconsole")) {
371 0174c480 Ermal LUÇI
										$config['system']['enableserial'] = true;
372 ff30e319 bruno
										write_config(gettext("Restore serial console enabling in configuration."));
373 0174c480 Ermal LUÇI
									}
374 5d4b8830 Ermal LUÇI
									unset($loaderconf);
375 0174c480 Ermal LUÇI
								}
376 67bc955d Chris Buechler
								/* extract out rrd items, unset from $config when done */
377 5f601060 Phil Davis
								if ($config['rrddata']) {
378 754b75d0 Darren Embry
									restore_rrddata();
379 a2b8f7b2 Scott Ullrich
									unset($config['rrddata']);
380 da17d77e Ermal Lu?i
									unlink_if_exists("{$g['tmp_path']}/config.cache");
381 a2b8f7b2 Scott Ullrich
									write_config();
382 3420028c Scott Ullrich
									add_base_packages_menu_items();
383 d442e4e2 Scott Ullrich
									convert_config();
384 a2b8f7b2 Scott Ullrich
									conf_mount_ro();
385 1390b049 Scott Ullrich
								}
386 5f601060 Phil Davis
								if ($m0n0wall_upgrade == true) {
387
									if ($config['system']['gateway'] <> "") {
388 8ff5ffcc Matthew Grooms
										$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
389 5f601060 Phil Davis
									}
390 8ff5ffcc Matthew Grooms
									unset($config['shaper']);
391
									/* optional if list */
392
									$ifdescrs = get_configured_interface_list(true, true);
393
									/* remove special characters from interface descriptions */
394 5f601060 Phil Davis
									if (is_array($ifdescrs)) {
395
										foreach ($ifdescrs as $iface) {
396 8ff5ffcc Matthew Grooms
											$config['interfaces'][$iface]['descr'] = remove_bad_chars($config['interfaces'][$iface]['descr']);
397 5f601060 Phil Davis
										}
398
									}
399 b6db8ea3 sullrich
									/* check for interface names with an alias */
400 5f601060 Phil Davis
									if (is_array($ifdescrs)) {
401
										foreach ($ifdescrs as $iface) {
402
											if (is_alias($config['interfaces'][$iface]['descr'])) {
403 b6db8ea3 sullrich
												// Firewall rules
404
												$origname = $config['interfaces'][$iface]['descr'];
405 6c07db48 Phil Davis
												$newname = $config['interfaces'][$iface]['descr'] . "Alias";
406 f1ac1733 Erik Fonnesbeck
												update_alias_names_upon_change(array('filter', 'rule'), array('source', 'address'), $newname, $origname);
407
												update_alias_names_upon_change(array('filter', 'rule'), array('destination', 'address'), $newname, $origname);
408 b6db8ea3 sullrich
												// NAT Rules
409 f1ac1733 Erik Fonnesbeck
												update_alias_names_upon_change(array('nat', 'rule'), array('source', 'address'), $newname, $origname);
410
												update_alias_names_upon_change(array('nat', 'rule'), array('destination', 'address'), $newname, $origname);
411
												update_alias_names_upon_change(array('nat', 'rule'), array('target'), $newname, $origname);
412 b6db8ea3 sullrich
												// Alias in an alias
413 f1ac1733 Erik Fonnesbeck
												update_alias_names_upon_change(array('aliases', 'alias'), array('address'), $newname, $origname);
414 b6db8ea3 sullrich
											}
415
										}
416
									}
417 da17d77e Ermal Lu?i
									unlink_if_exists("{$g['tmp_path']}/config.cache");
418 888e7a27 Scott Ullrich
									// Reset configuration version to something low
419 a250f179 Renato Botelho
									// in order to force the config upgrade code to
420 888e7a27 Scott Ullrich
									// run through with all steps that are required.
421
									$config['system']['version'] = "1.0";
422 798cb31a Scott Ullrich
									// Deal with descriptions longer than 63 characters
423
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
424 5f601060 Phil Davis
										if (count($config['filter']['rule'][$i]['descr']) > 63) {
425 798cb31a Scott Ullrich
											$config['filter']['rule'][$i]['descr'] = substr($config['filter']['rule'][$i]['descr'], 0, 63);
426 5f601060 Phil Davis
										}
427 798cb31a Scott Ullrich
									}
428
									// Move interface from ipsec to enc0
429
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
430 5f601060 Phil Davis
										if ($config['filter']['rule'][$i]['interface'] == "ipsec") {
431 798cb31a Scott Ullrich
											$config['filter']['rule'][$i]['interface'] = "enc0";
432 5f601060 Phil Davis
										}
433 798cb31a Scott Ullrich
									}
434
									// Convert icmp types
435
									// http://www.openbsd.org/cgi-bin/man.cgi?query=icmp&sektion=4&arch=i386&apropos=0&manpath=OpenBSD+Current
436
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
437 5f601060 Phil Davis
										if ($config["filter"]["rule"][$i]['icmptype']) {
438
											switch ($config["filter"]["rule"][$i]['icmptype']) {
439
												case "echo":
440
													$config["filter"]["rule"][$i]['icmptype'] = "echoreq";
441
													break;
442
												case "unreach":
443
													$config["filter"]["rule"][$i]['icmptype'] = "unreach";
444
													break;
445
												case "echorep":
446
													$config["filter"]["rule"][$i]['icmptype'] = "echorep";
447
													break;
448
												case "squench":
449
													$config["filter"]["rule"][$i]['icmptype'] = "squench";
450
													break;
451
												case "redir":
452
													$config["filter"]["rule"][$i]['icmptype'] = "redir";
453
													break;
454
												case "timex":
455
													$config["filter"]["rule"][$i]['icmptype'] = "timex";
456
													break;
457
												case "paramprob":
458
													$config["filter"]["rule"][$i]['icmptype'] = "paramprob";
459
													break;
460
												case "timest":
461
													$config["filter"]["rule"][$i]['icmptype'] = "timereq";
462
													break;
463
												case "timestrep":
464
													$config["filter"]["rule"][$i]['icmptype'] = "timerep";
465
													break;
466
												case "inforeq":
467
													$config["filter"]["rule"][$i]['icmptype'] = "inforeq";
468
													break;
469
												case "inforep":
470
													$config["filter"]["rule"][$i]['icmptype'] = "inforep";
471
													break;
472
												case "maskreq":
473
													$config["filter"]["rule"][$i]['icmptype'] = "maskreq";
474
													break;
475
												case "maskrep":
476
													$config["filter"]["rule"][$i]['icmptype'] = "maskrep";
477
													break;
478 798cb31a Scott Ullrich
											}
479
										}
480 dec5cb85 Scott Ullrich
									}
481
									$config['diag']['ipv6nat'] = true;
482 8ff5ffcc Matthew Grooms
									write_config();
483 a250f179 Renato Botelho
									add_base_packages_menu_items();
484 d442e4e2 Scott Ullrich
									convert_config();
485 8ff5ffcc Matthew Grooms
									conf_mount_ro();
486 205da5a0 Neriberto C.Prado
									$savemsg = gettext("The m0n0wall configuration has been restored and upgraded to pfSense.");
487 da17d77e Ermal Lu?i
									mark_subsystem_dirty("restore");
488 8ff5ffcc Matthew Grooms
								}
489 5f601060 Phil Davis
								if (is_array($config['captiveportal'])) {
490
									foreach ($config['captiveportal'] as $cp) {
491 2e19a683 Renato Botelho
										if (isset($cp['enable'])) {
492
											/* for some reason ipfw doesn't init correctly except on bootup sequence */
493
											mark_subsystem_dirty("restore");
494
											break;
495
										}
496
									}
497 8ff5ffcc Matthew Grooms
								}
498
								setup_serial_port();
499 5f601060 Phil Davis
								if (is_interface_mismatch() == true) {
500 8ff5ffcc Matthew Grooms
									touch("/var/run/interface_mismatch_reboot_needed");
501 da17d77e Ermal Lu?i
									clear_subsystem_dirty("restore");
502 f5a57bb0 Scott Ullrich
									convert_config();
503 8ff5ffcc Matthew Grooms
									header("Location: interfaces_assign.php");
504 bafaf123 Scott Ullrich
									exit;
505 8ff5ffcc Matthew Grooms
								}
506 66bcba1b Ermal
								if (is_interface_vlan_mismatch() == true) {
507
									touch("/var/run/interface_mismatch_reboot_needed");
508
									clear_subsystem_dirty("restore");
509
									convert_config();
510
									header("Location: interfaces_assign.php");
511
									exit;
512
								}
513 8ff5ffcc Matthew Grooms
							} else {
514 205da5a0 Neriberto C.Prado
								$input_errors[] = gettext("The configuration could not be restored.");
515 db3996df Scott Ullrich
							}
516 9c72b993 Scott Ullrich
						}
517 181462b5 Scott Ullrich
					}
518 8ff5ffcc Matthew Grooms
				} else {
519 205da5a0 Neriberto C.Prado
					$input_errors[] = gettext("The configuration could not be restored (file upload error).");
520 db3996df Scott Ullrich
				}
521 5b237745 Scott Ullrich
			}
522 8ff5ffcc Matthew Grooms
		}
523 73ed069b Renato Botelho
524 8ff5ffcc Matthew Grooms
		if ($mode == "reinstallpackages") {
525 8ccc8f1a Scott Ullrich
			header("Location: pkg_mgr_install.php?mode=reinstallall");
526
			exit;
527 039cb920 jim-p
		} else if ($mode == "clearpackagelock") {
528
			clear_subsystem_dirty('packagelock');
529 8545adde k-paulius
			$savemsg = "Package lock cleared.";
530 73ed069b Renato Botelho
		} else if ($mode == "restore_ver") {
531 ce871619 Stephen Beaver
			$input_errors[] = gettext("XXX - this feature may hose the config (do NOT backrev configs!) - billm");
532 7cf03119 Bill Marquette
			if ($ver2restore <> "") {
533
				$conf_file = "{$g['cf_conf_path']}/bak/config-" . strtotime($ver2restore) . ".xml";
534 343bb325 sullrich
				if (config_install($conf_file) == 0) {
535 c9a19238 Darren Embry
					mark_subsystem_dirty("restore");
536
				} else {
537
					$input_errors[] = gettext("The configuration could not be restored.");
538
				}
539
			} else {
540
				$input_errors[] = gettext("No version selected.");
541
			}
542 5b237745 Scott Ullrich
		}
543
	}
544
}
545 6a1e6651 Bill Marquette
546 02e1170d Scott Ullrich
$id = rand() . '.' . time();
547
548
$mth = ini_get('upload_progress_meter.store_method');
549
$dir = ini_get('upload_progress_meter.file.filename_template');
550
551 a4af095c Renato Botelho
function build_area_list($showall) {
552
	global $config;
553 b63695db Scott Ullrich
554 0e834fc5 Stephen Beaver
	$areas = array(
555
		"aliases" => gettext("Aliases"),
556 a4af095c Renato Botelho
		"captiveportal" => gettext("Captive Portal"),
557
		"voucher" => gettext("Captive Portal Vouchers"),
558
		"dnsmasq" => gettext("DNS Forwarder"),
559
		"unbound" => gettext("DNS Resolver"),
560
		"dhcpd" => gettext("DHCP Server"),
561
		"dhcpdv6" => gettext("DHCPv6 Server"),
562
		"filter" => gettext("Firewall Rules"),
563
		"interfaces" => gettext("Interfaces"),
564
		"ipsec" => gettext("IPSEC"),
565
		"nat" => gettext("NAT"),
566
		"openvpn" => gettext("OpenVPN"),
567
		"installedpackages" => gettext("Package Manager"),
568
		"rrddata" => gettext("RRD Data"),
569
		"cron" => gettext("Scheduled Tasks"),
570
		"syslog" => gettext("Syslog"),
571
		"system" => gettext("System"),
572
		"staticroutes" => gettext("Static routes"),
573
		"sysctl" => gettext("System tunables"),
574
		"snmpd" => gettext("SNMP Server"),
575
		"shaper" => gettext("Traffic Shaper"),
576
		"vlans" => gettext("VLANS"),
577 7ca42d47 k-paulius
		"wol" => gettext("Wake-on-LAN")
578 a4af095c Renato Botelho
		);
579 0e834fc5 Stephen Beaver
580
	$list = array("" => gettext("All"));
581
582 288a2a0f Phil Davis
	if ($showall) {
583 0e834fc5 Stephen Beaver
		return($list + $areas);
584 288a2a0f Phil Davis
	} else {
585 a4af095c Renato Botelho
		foreach ($areas as $area => $areaname) {
586
			if ($area === "rrddata" || check_and_returnif_section_exists($area) == true) {
587
				$list[$area] = $areaname;
588
			}
589
		}
590 8ff5ffcc Matthew Grooms
591 0e834fc5 Stephen Beaver
		return($list);
592 5f601060 Phil Davis
	}
593 8ff5ffcc Matthew Grooms
}
594
595 0f298138 k-paulius
$pgtitle = array(gettext("Diagnostics"), htmlspecialchars(gettext("Backup & Restore")), htmlspecialchars(gettext("Backup & Restore")));
596 b63695db Scott Ullrich
include("head.inc");
597
598 947141fd Phil Davis
if ($input_errors) {
599 a4af095c Renato Botelho
	print_input_errors($input_errors);
600 947141fd Phil Davis
}
601 0e834fc5 Stephen Beaver
602 947141fd Phil Davis
if ($savemsg) {
603 a4af095c Renato Botelho
	print_info_box($savemsg, 'success');
604 947141fd Phil Davis
}
605 0e834fc5 Stephen Beaver
606 a4af095c Renato Botelho
if (is_subsystem_dirty('restore')):
607 5b237745 Scott Ullrich
?>
608 a4af095c Renato Botelho
	<br/>
609 1af5edbf Stephen Beaver
	<form action="diag_reboot.php" method="post">
610 7d5b007c Sjon Hortensius
		<input name="Submit" type="hidden" value="Yes" />
611 f6aebbcc NewEraCracker
		<?php print_info_box(gettext("The firewall configuration has been changed.") . "<br />" . gettext("The firewall is now rebooting.")); ?>
612 a4af095c Renato Botelho
		<br />
613 7d5b007c Sjon Hortensius
	</form>
614 0e834fc5 Stephen Beaver
<?php
615 a4af095c Renato Botelho
endif;
616
617
$tab_array = array();
618 0f298138 k-paulius
$tab_array[] = array(htmlspecialchars(gettext("Backup & Restore")), true, "diag_backup.php");
619 a4af095c Renato Botelho
$tab_array[] = array(gettext("Config History"), false, "diag_confbak.php");
620
display_top_tabs($tab_array);
621
622
$form = new Form(false);
623 3beeb66a Stephen Beaver
$form->setMultipartEncoding();	// Allow file uploads
624 a4af095c Renato Botelho
625 5f88f964 k-paulius
$section = new Form_Section('Backup Configuration');
626 a4af095c Renato Botelho
627
$section->addInput(new Form_Select(
628
	'backuparea',
629
	'Backup area',
630
	'',
631
	build_area_list(false)
632
));
633
634
$section->addInput(new Form_Checkbox(
635
	'nopackages',
636
	'Skip packages',
637
	'Do not backup package information.',
638
	false
639
));
640
641
$section->addInput(new Form_Checkbox(
642
	'donotbackuprrd',
643
	'Skip RRD data',
644
	'Do not backup RRD data (NOTE: RRD Data can consume 4+ megabytes of config.xml space!)',
645
	true
646
));
647
648
$section->addInput(new Form_Checkbox(
649
	'encrypt',
650
	'Encryption',
651
	'Encrypt this configuration file.',
652
	false
653 7c0f116b Jared Dillard
));
654 a4af095c Renato Botelho
655 76d6d925 Stephen Beaver
$section->addInput(new Form_Input(
656 a4af095c Renato Botelho
	'encrypt_password',
657 c8b10b4c Stephen Beaver
	'Password',
658 a4af095c Renato Botelho
	'password',
659 c8b10b4c Stephen Beaver
	null
660 7c0f116b Jared Dillard
));
661 a4af095c Renato Botelho
662
$group = new Form_Group('');
663 1d80d714 NOYB
// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
664 a4af095c Renato Botelho
$group->add(new Form_Button(
665
	'Submit',
666 faab522f Renato Botelho
	'Download configuration as XML',
667 37676f4e jim-p
	null,
668
	'fa-download'
669
))->setAttribute('id')->addClass('btn-primary');
670 a4af095c Renato Botelho
671
$section->add($group);
672
$form->add($section);
673
674 5f88f964 k-paulius
$section = new Form_Section('Restore Backup');
675 a4af095c Renato Botelho
676
$section->addInput(new Form_StaticText(
677
	null,
678 52e416cc Phil Davis
	sprintf(gettext("Open a %s configuration XML file and click the button below to restore the configuration."), $g['product_name'])
679 a4af095c Renato Botelho
));
680
681
$section->addInput(new Form_Select(
682
	'restorearea',
683
	'Restore area',
684
	'',
685 de407762 Phil Davis
	build_area_list(true)
686 a4af095c Renato Botelho
));
687
688
$section->addInput(new Form_Input(
689
	'conffile',
690
	'Configuration file',
691
	'file',
692
	null
693
));
694
695
$section->addInput(new Form_Checkbox(
696 7c0f116b Jared Dillard
	'decrypt',
697 a4af095c Renato Botelho
	'Encryption',
698 7c0f116b Jared Dillard
	'Configuration file is encrypted.',
699 a4af095c Renato Botelho
	false
700 7c0f116b Jared Dillard
));
701 a4af095c Renato Botelho
702 76d6d925 Stephen Beaver
$section->addInput(new Form_Input(
703 a4af095c Renato Botelho
	'decrypt_password',
704 c8b10b4c Stephen Beaver
	'Password',
705 a4af095c Renato Botelho
	'password',
706
	null,
707
	['placeholder' => 'Password']
708 7c0f116b Jared Dillard
));
709 a4af095c Renato Botelho
710
$group = new Form_Group('');
711 1d80d714 NOYB
// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
712 a4af095c Renato Botelho
$group->add(new Form_Button(
713
	'Submit',
714 faab522f Renato Botelho
	'Restore Configuration',
715 37676f4e jim-p
	null,
716
	'fa-undo'
717
))->setHelp('The firewall will reboot after restoring the configuration.')->addClass('btn-danger restore')->setAttribute('id');
718 a4af095c Renato Botelho
719
$section->add($group);
720
721
$form->add($section);
722
723
if (($config['installedpackages']['package'] != "") || (is_subsystem_dirty("packagelock"))) {
724 5f88f964 k-paulius
	$section = new Form_Section('Package Functions');
725 0e834fc5 Stephen Beaver
726 a4af095c Renato Botelho
	if ($config['installedpackages']['package'] != "") {
727
		$group = new Form_Group('');
728 1d80d714 NOYB
		// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
729 a4af095c Renato Botelho
		$group->add(new Form_Button(
730
			'Submit',
731 faab522f Renato Botelho
			'Reinstall Packages',
732 37676f4e jim-p
			null,
733
			'fa-retweet'
734
		))->setHelp('Click this button to reinstall all system packages.  This may take a while.')->addClass('btn-success')->setAttribute('id');
735 0e834fc5 Stephen Beaver
736 a4af095c Renato Botelho
		$section->add($group);
737 7eead1fc Darren Embry
	}
738 0e834fc5 Stephen Beaver
739 a4af095c Renato Botelho
	if (is_subsystem_dirty("packagelock")) {
740
		$group = new Form_Group('');
741 1d80d714 NOYB
		// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
742 a4af095c Renato Botelho
		$group->add(new Form_Button(
743
			'Submit',
744 faab522f Renato Botelho
			'Clear Package Lock',
745 37676f4e jim-p
			null,
746
			'fa-wrench'
747
		))->setHelp('Click this button to clear the package lock if a package fails to reinstall properly after an upgrade.')->addClass('btn-warning')->setAttribute('id');
748 0e834fc5 Stephen Beaver
749 a4af095c Renato Botelho
		$section->add($group);
750
	}
751 0e834fc5 Stephen Beaver
752 a4af095c Renato Botelho
	$form->add($section);
753 a1003d57 Ermal Lu?i
}
754 a4af095c Renato Botelho
755
print($form);
756 7c0f116b Jared Dillard
?>
757
<script type="text/javascript">
758
//<![CDATA[
759 947141fd Phil Davis
events.push(function() {
760 7c0f116b Jared Dillard
761
	// ------- Show/hide sections based on checkbox settings --------------------------------------
762 0da0d43e Phil Davis
763 7c0f116b Jared Dillard
	function hideSections(hide) {
764
		hidePasswords();
765
	}
766
767
	function hidePasswords() {
768 3beeb66a Stephen Beaver
769 7c0f116b Jared Dillard
		encryptHide = !($('input[name="encrypt"]').is(':checked'));
770
		decryptHide = !($('input[name="decrypt"]').is(':checked'));
771
772
		hideInput('encrypt_password', encryptHide);
773 c8b10b4c Stephen Beaver
		hideInput('encrypt_password_confirm', encryptHide);
774 7c0f116b Jared Dillard
		hideInput('decrypt_password', decryptHide);
775 c8b10b4c Stephen Beaver
		hideInput('decrypt_password_confirm', decryptHide);
776 7c0f116b Jared Dillard
	}
777
778 ff59b884 Stephen Beaver
	// ---------- Click handlers ------------------------------------------------------------------
779 7c0f116b Jared Dillard
780
	$('input[name="encrypt"]').on('change', function() {
781
		hidePasswords();
782
	});
783
784
	$('input[name="decrypt"]').on('change', function() {
785
		hidePasswords();
786 3beeb66a Stephen Beaver
	});
787 7c0f116b Jared Dillard
788 947141fd Phil Davis
	$('#conffile').change(function () {
789 40f146c5 Phil Davis
		if (document.getElementById("conffile").value) {
790
			$('.restore').prop('disabled', false);
791
		} else {
792
			$('.restore').prop('disabled', true);
793
		}
794 ff59b884 Stephen Beaver
    });
795 eef93144 Jared Dillard
	// ---------- On initial page load ------------------------------------------------------------
796
797 7c0f116b Jared Dillard
	hideSections();
798 ff59b884 Stephen Beaver
	$('.restore').prop('disabled', true);
799 7c0f116b Jared Dillard
});
800
//]]>
801
</script>
802
803
<?php
804 a4af095c Renato Botelho
include("foot.inc");
805 7a7f3af1 sullrich
806 5f601060 Phil Davis
if (is_subsystem_dirty('restore')) {
807 cf9a4467 jim-p
	system_reboot();
808 c9d46a8e Renato Botelho
}