Project

General

Profile

Download (12.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	diag_backup.php
5
	Copyright (C) 2004,2005,2006 Scott Ullrich
6
	All rights reserved.
7

    
8
	originally part of m0n0wall (http://m0n0.ch/wall)
9
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
	All rights reserved.
11

    
12
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14

    
15
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17

    
18
	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

    
22
	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
require("guiconfig.inc");
37

    
38
function remove_bad_chars($string) {
39
	return preg_replace('/[^a-z|_|0-9]/i','',$string);
40
}
41

    
42
function spit_out_select_items($area) {
43
	$select = <<<EOD
44
	<select name="{$area}">
45
		<option VALUE="">ALL</option>
46
		<option VALUE="aliases">Aliases</option>
47
		<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
		<option VALUE="ipsec">IPsec VPN</option>
52
		<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
if ($_POST) {
65
	unset($input_errors);
66
	if (stristr($_POST['Submit'], "Restore configuration"))
67
		$mode = "restore";
68
	else if (stristr($_POST['Submit'], "Reinstall"))
69
		$mode = "reinstallpackages";
70
	else if (stristr($_POST['Submit'], "Download"))
71
		$mode = "download";
72
	else if (stristr($_POST['Submit'], "Restore version"))
73
		$mode = "restore_ver";
74

    
75
	if ($_POST["nopackages"] <> "")
76
		$options = "nopackages";
77

    
78
	if ($_POST["ver"] <> "")
79
		$ver2restore = $_POST["ver"];
80

    
81
	if ($mode) {
82
		if ($mode == "download") {
83
			config_lock();
84
			$fn = "config-" . $config['system']['hostname'] . "." .
85
				$config['system']['domain'] . "-" . date("YmdHis") . ".xml";
86
			if($options == "nopackages") {
87
				exec("sed '/<installedpackages>/,/<\/installedpackages>/d' /conf/config.xml > /tmp/config.xml.nopkg");
88
				$fs = filesize("{$g['tmp_path']}/config.xml.nopkg");
89
				header("Content-Type: application/octet-stream");
90
                        	header("Content-Disposition: attachment; filename=$fn");
91
                        	header("Content-Length: $fs");
92
				readfile("{$g['tmp_path']}/config.xml.nopkg");
93
			} else {
94
				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
					readfile($g['tmp_path'] . "/backup_section.txt");
107
					unlink($g['tmp_path'] . "/backup_section.txt");
108
				} 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
			}
116
			config_unlock();
117
			exit;
118
		} else if ($mode == "restore") {
119
			if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
120
				$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
				if(stristr($tmp, "m0n0wall") == true) {
130
					log_error("Upgrading m0n0wall configuration to pfsense.");
131
					/* m0n0wall was found in config.  convert it. */
132
					$upgradedconfig = str_replace("m0n0wall", "pfsense", $tmp);
133
					$fd = fopen($_FILES['conffile']['tmp_name'], "w");
134
					fwrite($fd, $upgradedconfig);
135
					fclose($fd);
136
					$m0n0wall_upgrade = true;
137
				}
138
				if($_POST['restorearea'] <> "") {
139
					/* restore a specific area of the configuration */
140
					$rules = file_get_contents($_FILES['conffile']['tmp_name']);
141
					if(stristr($rules, $_POST['restorearea']) == false) {
142
						$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
						$savemsg = "The configuration area has been restored.  The firewall may need to be rebooted.";
147
					}
148
				} else {
149
					$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
					} else {
153
						/* restore the entire configuration */
154
						if (config_install($_FILES['conffile']['tmp_name']) == 0) {
155
							/* this will be picked up by /index.php */
156
							conf_mount_rw();
157
							if($g['platform'] <> "cdrom")
158
								touch("/needs_package_sync");
159
							$reboot_needed = true;
160
							$savemsg = "The configuration has been restored. The firewall is now rebooting.";
161
							/* remove cache, we will force a config reboot */
162
							if(file_exists("/tmp/config.cache"))
163
								unlink("/tmp/config.cache");
164
							$config = parse_config(true);
165
							if($m0n0wall_upgrade == true) {
166
								if($config['system']['gateway'] <> "")
167
									$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
168
								unset($config['shaper']);
169
								/* 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
								if(is_array($ifdescrs))
174
									foreach($ifdescrs as $iface)
175
										$config['interfaces'][$iface]['descr'] = remove_bad_chars($config['interfaces'][$iface]['descr']);
176
								unlink_if_exists("/tmp/config.cache");
177
								write_config();
178
								conf_mount_ro();
179
								$savemsg = "The m0n0wall configuration has been restored and upgraded to {$g['product_name']}.<p>The firewall is now rebooting.";
180
								$reboot_needed = true;
181
							}
182
							if(isset($config['captiveportal']['enable'])) {
183
								/* for some reason ipfw doesn't init correctly except on bootup sequence */
184
								$savemsg = "The configuration has been restored.<p>The firewall is now rebooting.";
185
								$reboot_needed = true;
186
							}
187
							setup_serial_port();
188
							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
						} else {
194
							$input_errors[] = "The configuration could not be restored.";
195
						}
196
					}
197
				}
198
			} else {
199
				$input_errors[] = "The configuration could not be restored (file upload error).";
200
			}
201
		} else if ($mode == "reinstallpackages") {
202
			header("Location: pkg_mgr_install.php?mode=reinstallall");
203
			exit;
204
                } 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
									$reboot_needed = true;
210
                                    $savemsg = "The configuration has been restored. The firewall is now rebooting.";
211
                                } else {
212
                                	$input_errors[] = "The configuration could not be restored.";
213
                                }
214
                        } else {
215
                                $input_errors[] = "No version selected.";
216
                        }
217
		}
218
	}
219
}
220

    
221
$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
$pgtitle = "Diagnostics: Backup/restore";
227
include("head.inc");
228

    
229
?>
230

    
231
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
232
<?php include("fbegin.inc"); ?>
233
<p class="pgtitle"><?=$pgtitle?></p>
234

    
235
<form action="diag_backup.php" method="post" enctype="multipart/form-data">
236
<?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
<?php
242
		$tab_array = array();
243
		$tab_array[0] = array("Config History", false, "diag_confbak.php");
244
		$tab_array[1] = array("Backup/Restore", true, "diag_backup.php");
245
		display_top_tabs($tab_array);
246
?>
247
		</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
                </tr>
267
                <tr>
268
					<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
                      	<p><strong><span class="red">Note:</span></strong><br />The firewall may need to be rebooted after restoring the configuration.<br /></p>
277
					</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
		</td>
297
	</tr>
298
</table>
299
</form>
300

    
301
<?php include("fend.inc"); ?>
302
</body>
303
</html>
304

    
305
<?php
306

    
307
if($reboot_needed == true) {
308
	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
	exit;
315
}
316

    
317
?>
(5-5/173)