Project

General

Profile

Download (12.2 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 47d11b79 Mark Crane
/* Allow additional execution time 0 = no limit. */
35
ini_set('max_execution_time', '3600');
36
ini_set('max_input_time', '3600');
37
38 5b237745 Scott Ullrich
/* omit no-cache headers because it confuses IE with file downloads */
39
$omit_nocacheheaders = true;
40 8ccc8f1a Scott Ullrich
require("guiconfig.inc");
41 5b237745 Scott Ullrich
42 645ec835 Scott Ullrich
function remove_bad_chars($string) {
43
	return preg_replace('/[^a-z|_|0-9]/i','',$string);
44
}
45
46 df157bb7 Scott Ullrich
function spit_out_select_items($area) {
47 8e35abee Scott Ullrich
	$select = <<<EOD
48 df157bb7 Scott Ullrich
	<select name="{$area}">
49 8e35abee Scott Ullrich
		<option VALUE="">ALL</option>
50 5161a6e6 Scott Ullrich
		<option VALUE="aliases">Aliases</option>
51 8e35abee Scott Ullrich
		<option VALUE="shaper">Traffic Shaper</option>
52
		<option VALUE="filter">Firewall Rules</option>
53
		<option VALUE="nat">NAT</option>
54
		<option VALUE="pptpd">PPTP Server</option>
55 0f266b2e Chris Buechler
		<option VALUE="ipsec">IPsec VPN</option>
56 8e35abee Scott Ullrich
		<option VALUE="captiveportal">Captive Portal</option>
57
		<option VALUE="installedpackages">Package Manager</option>
58
		<option VALUE="interfaces">Interfaces</option>
59
		<option VALUE="dhcpd">DHCP Server</option>
60
		<option VALUE="syslog">Syslog</option>
61
		<option VALUE="system">System</option>
62 6661d732 Scott Ullrich
		<option VALUE="staticroutes">Static routes</option>
63 8e35abee Scott Ullrich
	</select>
64
EOD;
65
	echo $select;
66
67
}
68
69 5b237745 Scott Ullrich
if ($_POST) {
70
	unset($input_errors);
71 7cf03119 Bill Marquette
	if (stristr($_POST['Submit'], "Restore configuration"))
72 5b237745 Scott Ullrich
		$mode = "restore";
73 8ccc8f1a Scott Ullrich
	else if (stristr($_POST['Submit'], "Reinstall"))
74
		$mode = "reinstallpackages";
75 5b237745 Scott Ullrich
	else if (stristr($_POST['Submit'], "Download"))
76
		$mode = "download";
77 7cf03119 Bill Marquette
	else if (stristr($_POST['Submit'], "Restore version"))
78
		$mode = "restore_ver";
79
80 aab57926 Scott Ullrich
	if ($_POST["nopackages"] <> "")
81 528cad39 Colin Smith
		$options = "nopackages";
82 8ccc8f1a Scott Ullrich
83 7cf03119 Bill Marquette
	if ($_POST["ver"] <> "")
84
		$ver2restore = $_POST["ver"];
85
86 5b237745 Scott Ullrich
	if ($mode) {
87
		if ($mode == "download") {
88
			config_lock();
89 8ccc8f1a Scott Ullrich
			$fn = "config-" . $config['system']['hostname'] . "." .
90 5b237745 Scott Ullrich
				$config['system']['domain'] . "-" . date("YmdHis") . ".xml";
91 528cad39 Colin Smith
			if($options == "nopackages") {
92
				exec("sed '/<installedpackages>/,/<\/installedpackages>/d' /conf/config.xml > /tmp/config.xml.nopkg");
93 f66683e6 Bill Marquette
				$fs = filesize("{$g['tmp_path']}/config.xml.nopkg");
94 528cad39 Colin Smith
				header("Content-Type: application/octet-stream");
95
                        	header("Content-Disposition: attachment; filename=$fn");
96
                        	header("Content-Length: $fs");
97 f66683e6 Bill Marquette
				readfile("{$g['tmp_path']}/config.xml.nopkg");
98 528cad39 Colin Smith
			} else {
99 181462b5 Scott Ullrich
				if($_POST['backuparea'] <> "") {
100
					/* user wishes to backup specific area of configuration */
101
					$current_trafficshaper_section = backup_config_section($_POST['backuparea']);
102
					/* generate aliases xml */
103
					$fout = fopen("{$g['tmp_path']}/backup_section.txt","w");
104
					fwrite($fout, $current_trafficshaper_section);
105
					fclose($fout);
106
					$fs = filesize($g['tmp_path'] . "/backup_section.txt");
107
					header("Content-Type: application/octet-stream");
108
					$fn = $_POST['backuparea'] . "-" . $fn;
109
					header("Content-Disposition: attachment; filename=$fn");
110
					header("Content-Length: $fs");
111 e55f5da6 Scott Ullrich
					readfile($g['tmp_path'] . "/backup_section.txt");
112 7e036443 Scott Ullrich
					unlink($g['tmp_path'] . "/backup_section.txt");
113 181462b5 Scott Ullrich
				} else {
114
					$fs = filesize($g['conf_path'] . "/config.xml");
115
					header("Content-Type: application/octet-stream");
116
					header("Content-Disposition: attachment; filename=$fn");
117
					header("Content-Length: $fs");
118
					readfile($g['conf_path'] . "/config.xml");
119
				}
120 528cad39 Colin Smith
			}
121 5b237745 Scott Ullrich
			config_unlock();
122
			exit;
123
		} else if ($mode == "restore") {
124
			if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
125 181462b5 Scott Ullrich
				$fd = fopen($_FILES['conffile']['tmp_name'], "r");
126
				if(!$fd) {
127
					log_error("Warning, could not open " . $_FILES['conffile']['tmp_name']);
128
					return 1;
129
				}
130
				while(!feof($fd)) {
131
					    $tmp .= fread($fd,49);
132
				}
133
				fclose($fd);
134 9d10caaa Scott Ullrich
				if(stristr($tmp, "m0n0wall") == true) {
135 181462b5 Scott Ullrich
					log_error("Upgrading m0n0wall configuration to pfsense.");
136
					/* m0n0wall was found in config.  convert it. */
137
					$upgradedconfig = str_replace("m0n0wall", "pfsense", $tmp);
138 6198d330 Scott Ullrich
					$fd = fopen($_FILES['conffile']['tmp_name'], "w");
139 181462b5 Scott Ullrich
					fwrite($fd, $upgradedconfig);
140 bfd3e487 Scott Ullrich
					fclose($fd);
141 db3996df Scott Ullrich
					$m0n0wall_upgrade = true;
142 181462b5 Scott Ullrich
				}
143 fdef6434 Bill Marquette
				if($_POST['restorearea'] <> "") {
144 181462b5 Scott Ullrich
					/* restore a specific area of the configuration */
145 7822d966 Colin Smith
					$rules = file_get_contents($_FILES['conffile']['tmp_name']);
146 afc4d0b7 Scott Ullrich
					if(stristr($rules, $_POST['restorearea']) == false) {
147 9fd18b1f Scott Ullrich
						$input_errors[] = "You have selected to restore a area but we could not locate the correct xml tag.";
148
					} else {
149
						restore_config_section($_POST['restorearea'], $rules);
150
						filter_configure();
151 3afdcfe8 Bill Marquette
						$savemsg = "The configuration area has been restored.  You may need to reboot the firewall.";
152 9fd18b1f Scott Ullrich
					}
153 5b237745 Scott Ullrich
				} else {
154 9c72b993 Scott Ullrich
					$rules = file_get_contents($_FILES['conffile']['tmp_name']);
155
					if(stristr($rules, "pfsense") == false) {
156
						$input_errors[] = "You have selected to restore the full configuration but we could not locate a pfsense tag.";
157 181462b5 Scott Ullrich
					} else {
158 db3996df Scott Ullrich
						/* restore the entire configuration */
159 9c72b993 Scott Ullrich
						if (config_install($_FILES['conffile']['tmp_name']) == 0) {
160
							/* this will be picked up by /index.php */
161
							conf_mount_rw();
162 447c8e03 Scott Ullrich
							if($g['platform'] <> "cdrom")
163
								touch("/needs_package_sync");
164 6f703733 Scott Ullrich
							$reboot_needed = true;
165
							$savemsg = "The configuration has been restored. The firewall is now rebooting.";
166 e57f259a Scott Ullrich
							/* remove cache, we will force a config reboot */
167 db3996df Scott Ullrich
							if(file_exists("/tmp/config.cache"))
168
								unlink("/tmp/config.cache");
169 ffd1f1aa Scott Ullrich
							$config = parse_config(true);
170 db3996df Scott Ullrich
							if($m0n0wall_upgrade == true) {
171 645ec835 Scott Ullrich
								if($config['system']['gateway'] <> "")
172 db3996df Scott Ullrich
									$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
173
								unset($config['shaper']);
174 645ec835 Scott Ullrich
								/* build an interface collection */
175
								for ($j = 1; isset ($config['interfaces']['opt' . $j]); $j++)
176
									$ifdescrs['opt' . $j] = "opt" . $j;
177
								/* remove special characters from interface descriptions */
178 981cf3ca Scott Ullrich
								if(is_array($ifdescrs))
179
									foreach($ifdescrs as $iface)
180
										$config['interfaces'][$iface]['descr'] = remove_bad_chars($config['interfaces'][$iface]['descr']);
181 645ec835 Scott Ullrich
								unlink_if_exists("/tmp/config.cache");
182 6b524b56 Scott Ullrich
								write_config();
183
								conf_mount_ro();
184 89d6fe70 Scott Ullrich
								$savemsg = "The m0n0wall configuration has been restored and upgraded to pfSense.<p>The firewall is now rebooting.";
185 56825c01 Scott Ullrich
								$reboot_needed = true;
186
							}
187
							if(isset($config['captiveportal']['enable'])) {
188 e3fa4e9a Scott Ullrich
								/* for some reason ipfw doesn't init correctly except on bootup sequence */
189 1ddc3e2a Scott Ullrich
								$savemsg = "The configuration has been restored.<p>The firewall is now rebooting.";
190 56825c01 Scott Ullrich
								$reboot_needed = true;
191 db3996df Scott Ullrich
							}
192 96af3ad5 Scott Ullrich
							setup_serial_port();
193 23be6f1b Scott Ullrich
							if(is_interface_mismatch() == true) {
194
								touch("/var/run/interface_mismatch_reboot_needed");
195
								$reboot_needed = false;
196
								header("Location: interfaces_assign.php");
197
							}
198 9c72b993 Scott Ullrich
						} else {
199
							$input_errors[] = "The configuration could not be restored.";
200
						}
201 181462b5 Scott Ullrich
					}
202 db3996df Scott Ullrich
				}
203 5b237745 Scott Ullrich
			} else {
204
				$input_errors[] = "The configuration could not be restored (file upload error).";
205
			}
206 8ccc8f1a Scott Ullrich
		} else if ($mode == "reinstallpackages") {
207
			header("Location: pkg_mgr_install.php?mode=reinstallall");
208
			exit;
209 7cf03119 Bill Marquette
                } else if ($mode == "restore_ver") {
210
			$input_errors[] = "XXX - this feature may hose your config (do NOT backrev configs!) - billm";
211
			if ($ver2restore <> "") {
212
				$conf_file = "{$g['cf_conf_path']}/bak/config-" . strtotime($ver2restore) . ".xml";
213
                                if (config_install($conf_file) == 0) {
214 e57f259a Scott Ullrich
									$reboot_needed = true;
215
                                    $savemsg = "The configuration has been restored. The firewall is now rebooting.";
216 7cf03119 Bill Marquette
                                } else {
217 e57f259a Scott Ullrich
                                	$input_errors[] = "The configuration could not be restored.";
218 7cf03119 Bill Marquette
                                }
219
                        } else {
220
                                $input_errors[] = "No version selected.";
221
                        }
222 5b237745 Scott Ullrich
		}
223
	}
224
}
225 6a1e6651 Bill Marquette
226 02e1170d Scott Ullrich
$id = rand() . '.' . time();
227
228
$mth = ini_get('upload_progress_meter.store_method');
229
$dir = ini_get('upload_progress_meter.file.filename_template');
230
231 d88c6a9f Scott Ullrich
$pgtitle = array("Diagnostics","Backup/restore");
232 b63695db Scott Ullrich
include("head.inc");
233
234 5b237745 Scott Ullrich
?>
235
236
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
237
<?php include("fbegin.inc"); ?>
238 02ecc674 Scott Ullrich
<form action="diag_backup.php" method="post" enctype="multipart/form-data">
239 9e2a4fce Erik Kristensen
<?php if ($input_errors) print_input_errors($input_errors); ?>
240
<?php if ($savemsg) print_info_box($savemsg); ?>
241
<table width="100%" border="0" cellspacing="0" cellpadding="0">
242
	<tr>
243
		<td>
244 12af52d9 Scott Ullrich
<?php
245 9e2a4fce Erik Kristensen
		$tab_array = array();
246 2dfdb028 Chris Buechler
		$tab_array[0] = array("Config History", false, "diag_confbak.php");
247
		$tab_array[1] = array("Backup/Restore", true, "diag_backup.php");
248 9e2a4fce Erik Kristensen
		display_top_tabs($tab_array);
249 645ec835 Scott Ullrich
?>
250 9e2a4fce Erik Kristensen
		</td>
251
	</tr>
252
	<tr>
253
		<td>
254
			<div id="mainarea">
255
			<table class="tabcont" align="center" width="100%" border="0" cellpadding="6" cellspacing="0">
256
				<tr>
257
					<td colspan="2" class="listtopic">Backup configuration</td>
258
				</tr>
259
				<tr>
260
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
261
					<td width="78%" class="vtable">
262
						<p>Click this button to download the system configuration in XML format.<br /><br /> Backup area: <?php spit_out_select_items("backuparea"); ?></p>
263
						<p><input name="nopackages" type="checkbox" class="formcheckbox" id="nopackages">Do not backup package information.</p>
264
						<p><input name="Submit" type="submit" class="formbtn" id="download" value="Download configuration"></p>
265
					</td>
266
				</tr>
267
				<tr>
268
					<td colspan="2" class="list" height="12">&nbsp;</td>
269 5b237745 Scott Ullrich
                </tr>
270 8ccc8f1a Scott Ullrich
                <tr>
271 9e2a4fce Erik Kristensen
					<td colspan="2" class="listtopic">Restore configuration</td>
272
				</tr>
273
				<tr>
274
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
275
					<td width="78%" class="vtable">
276 59605ba4 Scott Ullrich
						Open a <?=$g['[product_name']?> configuration XML file and click the button below to restore the configuration. <br /><br /> Restore area: <?php spit_out_select_items("restorearea"); ?>
277 b5c78501 Seth Mos
						<p><input name="conffile" type="file" class="formfld unknown" id="conffile" size="40"></p>
278 9e2a4fce Erik Kristensen
						<p><input name="Submit" type="submit" class="formbtn" id="restore" value="Restore configuration"></p>
279 aea4964f Bill Marquette
                      	<p><strong><span class="red">Note:</span></strong><br />The firewall may need a reboot after restoring the configuration.<br /></p>
280 9e2a4fce Erik Kristensen
					</td>
281
				</tr>
282
				<?php if($config['installedpackages']['package'] != "") { ?>
283
				<tr>
284
					<td colspan="2" class="list" height="12">&nbsp;</td>
285
				</tr>
286
				<tr>
287
					<td colspan="2" class="listtopic">Reinstall packages</td>
288
				</tr>
289
				<tr>
290
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
291
					<td width="78%" class="vtable">
292
						<p>Click this button to reinstall all system packages.  This may take a while. <br /><br />
293
		  				<input name="Submit" type="submit" class="formbtn" id="reinstallpackages" value="Reinstall packages">
294
					</td>
295
				</tr>
296
				<?php } ?>
297
			</table>
298
			</div>
299 b24bf37b Colin Smith
		</td>
300 9e2a4fce Erik Kristensen
	</tr>
301
</table>
302
</form>
303
304 5b237745 Scott Ullrich
<?php include("fend.inc"); ?>
305
</body>
306 38d48421 Colin Smith
</html>
307 8ed1f1a9 Scott Ullrich
308
<?php
309
310 56825c01 Scott Ullrich
if($reboot_needed == true) {
311 ff598ca2 Scott Ullrich
	ob_flush();
312
	flush();
313
	sleep(5);
314
	while(file_exists("{$g['varrun_path']}/config.lock"))
315
		sleep(3);
316
	mwexec("/sbin/shutdown -r now");
317 56825c01 Scott Ullrich
	exit;
318
}
319
320 b5c78501 Seth Mos
?>