Project

General

Profile

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