Project

General

Profile

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