Project

General

Profile

Download (12 KB) Statistics
| Branch: | Tag: | Revision:
1 8ccc8f1a Scott Ullrich
<?php
2 b46bfcf5 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	diag_backup.php
5 929db667 Scott Ullrich
	Copyright (C) 2004,2005,2006 Scott Ullrich
6
	All rights reserved.
7 8ccc8f1a Scott Ullrich
8 929db667 Scott Ullrich
	originally part of m0n0wall (http://m0n0.ch/wall)
9 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
	All rights reserved.
11 8ccc8f1a Scott Ullrich
12 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14 8ccc8f1a Scott Ullrich
15 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17 8ccc8f1a Scott Ullrich
18 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21 8ccc8f1a Scott Ullrich
22 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34
/* omit no-cache headers because it confuses IE with file downloads */
35
$omit_nocacheheaders = true;
36 8ccc8f1a Scott Ullrich
require("guiconfig.inc");
37 5b237745 Scott Ullrich
38 645ec835 Scott Ullrich
function remove_bad_chars($string) {
39
	return preg_replace('/[^a-z|_|0-9]/i','',$string);
40
}
41
42 df157bb7 Scott Ullrich
function spit_out_select_items($area) {
43 8e35abee Scott Ullrich
	$select = <<<EOD
44 df157bb7 Scott Ullrich
	<select name="{$area}">
45 8e35abee Scott Ullrich
		<option VALUE="">ALL</option>
46 5161a6e6 Scott Ullrich
		<option VALUE="aliases">Aliases</option>
47 8e35abee Scott Ullrich
		<option VALUE="shaper">Traffic Shaper</option>
48
		<option VALUE="filter">Firewall Rules</option>
49
		<option VALUE="nat">NAT</option>
50
		<option VALUE="pptpd">PPTP Server</option>
51 5c99e8ef Chris Buechler
		<option VALUE="ipsec">IPsec VPN</option>
52 8e35abee Scott Ullrich
		<option VALUE="captiveportal">Captive Portal</option>
53
		<option VALUE="installedpackages">Package Manager</option>
54
		<option VALUE="interfaces">Interfaces</option>
55
		<option VALUE="dhcpd">DHCP Server</option>
56
		<option VALUE="syslog">Syslog</option>
57
		<option VALUE="system">System</option>
58
	</select>
59
EOD;
60
	echo $select;
61
62
}
63
64 5b237745 Scott Ullrich
if ($_POST) {
65
	unset($input_errors);
66 7cf03119 Bill Marquette
	if (stristr($_POST['Submit'], "Restore configuration"))
67 5b237745 Scott Ullrich
		$mode = "restore";
68 8ccc8f1a Scott Ullrich
	else if (stristr($_POST['Submit'], "Reinstall"))
69
		$mode = "reinstallpackages";
70 5b237745 Scott Ullrich
	else if (stristr($_POST['Submit'], "Download"))
71
		$mode = "download";
72 7cf03119 Bill Marquette
	else if (stristr($_POST['Submit'], "Restore version"))
73
		$mode = "restore_ver";
74
75 aab57926 Scott Ullrich
	if ($_POST["nopackages"] <> "")
76 528cad39 Colin Smith
		$options = "nopackages";
77 8ccc8f1a Scott Ullrich
78 7cf03119 Bill Marquette
	if ($_POST["ver"] <> "")
79
		$ver2restore = $_POST["ver"];
80
81 5b237745 Scott Ullrich
	if ($mode) {
82
		if ($mode == "download") {
83
			config_lock();
84 8ccc8f1a Scott Ullrich
			$fn = "config-" . $config['system']['hostname'] . "." .
85 5b237745 Scott Ullrich
				$config['system']['domain'] . "-" . date("YmdHis") . ".xml";
86 528cad39 Colin Smith
			if($options == "nopackages") {
87
				exec("sed '/<installedpackages>/,/<\/installedpackages>/d' /conf/config.xml > /tmp/config.xml.nopkg");
88 f66683e6 Bill Marquette
				$fs = filesize("{$g['tmp_path']}/config.xml.nopkg");
89 528cad39 Colin Smith
				header("Content-Type: application/octet-stream");
90
                        	header("Content-Disposition: attachment; filename=$fn");
91
                        	header("Content-Length: $fs");
92 f66683e6 Bill Marquette
				readfile("{$g['tmp_path']}/config.xml.nopkg");
93 528cad39 Colin Smith
			} else {
94 181462b5 Scott Ullrich
				if($_POST['backuparea'] <> "") {
95
					/* user wishes to backup specific area of configuration */
96
					$current_trafficshaper_section = backup_config_section($_POST['backuparea']);
97
					/* generate aliases xml */
98
					$fout = fopen("{$g['tmp_path']}/backup_section.txt","w");
99
					fwrite($fout, $current_trafficshaper_section);
100
					fclose($fout);
101
					$fs = filesize($g['tmp_path'] . "/backup_section.txt");
102
					header("Content-Type: application/octet-stream");
103
					$fn = $_POST['backuparea'] . "-" . $fn;
104
					header("Content-Disposition: attachment; filename=$fn");
105
					header("Content-Length: $fs");
106 e55f5da6 Scott Ullrich
					readfile($g['tmp_path'] . "/backup_section.txt");
107 7e036443 Scott Ullrich
					unlink($g['tmp_path'] . "/backup_section.txt");
108 181462b5 Scott Ullrich
				} else {
109
					$fs = filesize($g['conf_path'] . "/config.xml");
110
					header("Content-Type: application/octet-stream");
111
					header("Content-Disposition: attachment; filename=$fn");
112
					header("Content-Length: $fs");
113
					readfile($g['conf_path'] . "/config.xml");
114
				}
115 528cad39 Colin Smith
			}
116 5b237745 Scott Ullrich
			config_unlock();
117
			exit;
118
		} else if ($mode == "restore") {
119
			if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
120 181462b5 Scott Ullrich
				$fd = fopen($_FILES['conffile']['tmp_name'], "r");
121
				if(!$fd) {
122
					log_error("Warning, could not open " . $_FILES['conffile']['tmp_name']);
123
					return 1;
124
				}
125
				while(!feof($fd)) {
126
					    $tmp .= fread($fd,49);
127
				}
128
				fclose($fd);
129 9d10caaa Scott Ullrich
				if(stristr($tmp, "m0n0wall") == true) {
130 181462b5 Scott Ullrich
					log_error("Upgrading m0n0wall configuration to pfsense.");
131
					/* m0n0wall was found in config.  convert it. */
132
					$upgradedconfig = str_replace("m0n0wall", "pfsense", $tmp);
133 6198d330 Scott Ullrich
					$fd = fopen($_FILES['conffile']['tmp_name'], "w");
134 181462b5 Scott Ullrich
					fwrite($fd, $upgradedconfig);
135 bfd3e487 Scott Ullrich
					fclose($fd);
136 db3996df Scott Ullrich
					$m0n0wall_upgrade = true;
137 181462b5 Scott Ullrich
				}
138 fdef6434 Bill Marquette
				if($_POST['restorearea'] <> "") {
139 181462b5 Scott Ullrich
					/* restore a specific area of the configuration */
140 7822d966 Colin Smith
					$rules = file_get_contents($_FILES['conffile']['tmp_name']);
141 afc4d0b7 Scott Ullrich
					if(stristr($rules, $_POST['restorearea']) == false) {
142 9fd18b1f Scott Ullrich
						$input_errors[] = "You have selected to restore a area but we could not locate the correct xml tag.";
143
					} else {
144
						restore_config_section($_POST['restorearea'], $rules);
145
						filter_configure();
146 2a1f7941 Bill Marquette
						$savemsg = "The configuration area has been restored.  The firewall may need to be rebooted.";
147 9fd18b1f Scott Ullrich
					}
148 5b237745 Scott Ullrich
				} else {
149 9c72b993 Scott Ullrich
					$rules = file_get_contents($_FILES['conffile']['tmp_name']);
150
					if(stristr($rules, "pfsense") == false) {
151
						$input_errors[] = "You have selected to restore the full configuration but we could not locate a pfsense tag.";
152 181462b5 Scott Ullrich
					} else {
153 db3996df Scott Ullrich
						/* restore the entire configuration */
154 9c72b993 Scott Ullrich
						if (config_install($_FILES['conffile']['tmp_name']) == 0) {
155
							/* this will be picked up by /index.php */
156
							conf_mount_rw();
157 407868d8 Scott Ullrich
							if($g['platform'] <> "cdrom")
158
								touch("/needs_package_sync");
159 6f703733 Scott Ullrich
							$reboot_needed = true;
160
							$savemsg = "The configuration has been restored. The firewall is now rebooting.";
161 e57f259a Scott Ullrich
							/* remove cache, we will force a config reboot */
162 db3996df Scott Ullrich
							if(file_exists("/tmp/config.cache"))
163
								unlink("/tmp/config.cache");
164 ffd1f1aa Scott Ullrich
							$config = parse_config(true);
165 db3996df Scott Ullrich
							if($m0n0wall_upgrade == true) {
166 645ec835 Scott Ullrich
								if($config['system']['gateway'] <> "")
167 db3996df Scott Ullrich
									$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
168
								unset($config['shaper']);
169 645ec835 Scott Ullrich
								/* build an interface collection */
170
								for ($j = 1; isset ($config['interfaces']['opt' . $j]); $j++)
171
									$ifdescrs['opt' . $j] = "opt" . $j;
172
								/* remove special characters from interface descriptions */
173 981cf3ca Scott Ullrich
								if(is_array($ifdescrs))
174
									foreach($ifdescrs as $iface)
175
										$config['interfaces'][$iface]['descr'] = remove_bad_chars($config['interfaces'][$iface]['descr']);
176 645ec835 Scott Ullrich
								unlink_if_exists("/tmp/config.cache");
177 6b524b56 Scott Ullrich
								write_config();
178
								conf_mount_ro();
179 89d6fe70 Scott Ullrich
								$savemsg = "The m0n0wall configuration has been restored and upgraded to pfSense.<p>The firewall is now rebooting.";
180 56825c01 Scott Ullrich
								$reboot_needed = true;
181
							}
182
							if(isset($config['captiveportal']['enable'])) {
183 e3fa4e9a Scott Ullrich
								/* for some reason ipfw doesn't init correctly except on bootup sequence */
184 1ddc3e2a Scott Ullrich
								$savemsg = "The configuration has been restored.<p>The firewall is now rebooting.";
185 56825c01 Scott Ullrich
								$reboot_needed = true;
186 db3996df Scott Ullrich
							}
187 96af3ad5 Scott Ullrich
							setup_serial_port();
188 914a762d Scott Ullrich
							if(is_interface_mismatch() == true) {
189
								touch("/var/run/interface_mismatch_reboot_needed");
190
								$reboot_needed = false;
191
								header("Location: interfaces_assign.php");
192
							}							
193 9c72b993 Scott Ullrich
						} else {
194
							$input_errors[] = "The configuration could not be restored.";
195
						}
196 181462b5 Scott Ullrich
					}
197 db3996df Scott Ullrich
				}
198 5b237745 Scott Ullrich
			} else {
199
				$input_errors[] = "The configuration could not be restored (file upload error).";
200
			}
201 8ccc8f1a Scott Ullrich
		} else if ($mode == "reinstallpackages") {
202
			header("Location: pkg_mgr_install.php?mode=reinstallall");
203
			exit;
204 7cf03119 Bill Marquette
                } else if ($mode == "restore_ver") {
205
			$input_errors[] = "XXX - this feature may hose your config (do NOT backrev configs!) - billm";
206
			if ($ver2restore <> "") {
207
				$conf_file = "{$g['cf_conf_path']}/bak/config-" . strtotime($ver2restore) . ".xml";
208
                                if (config_install($conf_file) == 0) {
209 e57f259a Scott Ullrich
									$reboot_needed = true;
210 069c6a92 Bill Marquette
                                    $savemsg = "The configuration has been restored. The firewall is now rebooting.";
211 7cf03119 Bill Marquette
                                } else {
212 e57f259a Scott Ullrich
                                	$input_errors[] = "The configuration could not be restored.";
213 7cf03119 Bill Marquette
                                }
214
                        } else {
215
                                $input_errors[] = "No version selected.";
216
                        }
217 5b237745 Scott Ullrich
		}
218
	}
219
}
220 6a1e6651 Bill Marquette
221 02e1170d Scott Ullrich
$id = rand() . '.' . time();
222
223
$mth = ini_get('upload_progress_meter.store_method');
224
$dir = ini_get('upload_progress_meter.file.filename_template');
225
226 b63695db Scott Ullrich
$pgtitle = "Diagnostics: Backup/restore";
227
include("head.inc");
228
229 5b237745 Scott Ullrich
?>
230
231
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
232
<?php include("fbegin.inc"); ?>
233 310b2c06 Bill Marquette
<p class="pgtitle"><?=$pgtitle?></p>
234 9e2a4fce Erik Kristensen
235 02ecc674 Scott Ullrich
<form action="diag_backup.php" method="post" enctype="multipart/form-data">
236 9e2a4fce Erik Kristensen
<?php if ($input_errors) print_input_errors($input_errors); ?>
237
<?php if ($savemsg) print_info_box($savemsg); ?>
238
<table width="100%" border="0" cellspacing="0" cellpadding="0">
239
	<tr>
240
		<td>
241 12af52d9 Scott Ullrich
<?php
242 9e2a4fce Erik Kristensen
		$tab_array = array();
243 17912fda Chris Buechler
		$tab_array[0] = array("Config History", false, "diag_confbak.php");
244
		$tab_array[1] = array("Backup/Restore", true, "diag_backup.php");
245 9e2a4fce Erik Kristensen
		display_top_tabs($tab_array);
246 645ec835 Scott Ullrich
?>
247 9e2a4fce Erik Kristensen
		</td>
248
	</tr>
249
	<tr>
250
		<td>
251
			<div id="mainarea">
252
			<table class="tabcont" align="center" width="100%" border="0" cellpadding="6" cellspacing="0">
253
				<tr>
254
					<td colspan="2" class="listtopic">Backup configuration</td>
255
				</tr>
256
				<tr>
257
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
258
					<td width="78%" class="vtable">
259
						<p>Click this button to download the system configuration in XML format.<br /><br /> Backup area: <?php spit_out_select_items("backuparea"); ?></p>
260
						<p><input name="nopackages" type="checkbox" class="formcheckbox" id="nopackages">Do not backup package information.</p>
261
						<p><input name="Submit" type="submit" class="formbtn" id="download" value="Download configuration"></p>
262
					</td>
263
				</tr>
264
				<tr>
265
					<td colspan="2" class="list" height="12">&nbsp;</td>
266 5b237745 Scott Ullrich
                </tr>
267 8ccc8f1a Scott Ullrich
                <tr>
268 9e2a4fce Erik Kristensen
					<td colspan="2" class="listtopic">Restore configuration</td>
269
				</tr>
270
				<tr>
271
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
272
					<td width="78%" class="vtable">
273
						Open a pfSense configuration XML file and click the button below to restore the configuration. <br /><br /> Restore area: <?php spit_out_select_items("restorearea"); ?>
274
						<p><input name="conffile" type="file" class="formfld" id="conffile" size="40"></p>
275
						<p><input name="Submit" type="submit" class="formbtn" id="restore" value="Restore configuration"></p>
276 2a1f7941 Bill Marquette
                      	<p><strong><span class="red">Note:</span></strong><br />The firewall may need to be rebooted after restoring the configuration.<br /></p>
277 9e2a4fce Erik Kristensen
					</td>
278
				</tr>
279
				<?php if($config['installedpackages']['package'] != "") { ?>
280
				<tr>
281
					<td colspan="2" class="list" height="12">&nbsp;</td>
282
				</tr>
283
				<tr>
284
					<td colspan="2" class="listtopic">Reinstall packages</td>
285
				</tr>
286
				<tr>
287
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
288
					<td width="78%" class="vtable">
289
						<p>Click this button to reinstall all system packages.  This may take a while. <br /><br />
290
		  				<input name="Submit" type="submit" class="formbtn" id="reinstallpackages" value="Reinstall packages">
291
					</td>
292
				</tr>
293
				<?php } ?>
294
			</table>
295
			</div>
296 b24bf37b Colin Smith
		</td>
297 9e2a4fce Erik Kristensen
	</tr>
298
</table>
299
</form>
300
301 5b237745 Scott Ullrich
<?php include("fend.inc"); ?>
302
</body>
303 38d48421 Colin Smith
</html>
304 8ed1f1a9 Scott Ullrich
305
<?php
306
307 56825c01 Scott Ullrich
if($reboot_needed == true) {
308 ff598ca2 Scott Ullrich
	ob_flush();
309
	flush();
310
	sleep(5);
311
	while(file_exists("{$g['varrun_path']}/config.lock"))
312
		sleep(3);
313
	mwexec("/sbin/shutdown -r now");
314 56825c01 Scott Ullrich
	exit;
315
}
316
317 17912fda Chris Buechler
?>