Project

General

Profile

Download (11.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	diag_backup.php
5
	part of m0n0wall (http://m0n0.ch/wall)
6

    
7
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9

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

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

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
/* omit no-cache headers because it confuses IE with file downloads */
33
$omit_nocacheheaders = true;
34
require("guiconfig.inc");
35

    
36
function spit_out_select_items($area) {
37
	$select = <<<EOD
38
	<select name="{$area}">
39
		<option VALUE="">ALL</option>
40
		<option VALUE="aliases">Aliases</option>
41
		<option VALUE="shaper">Traffic Shaper</option>
42
		<option VALUE="filter">Firewall Rules</option>
43
		<option VALUE="nat">NAT</option>
44
		<option VALUE="pptpd">PPTP Server</option>
45
		<option VALUE="ipsec">IPSEC VPN</option>
46
		<option VALUE="captiveportal">Captive Portal</option>
47
		<option VALUE="installedpackages">Package Manager</option>
48
		<option VALUE="interfaces">Interfaces</option>
49
		<option VALUE="dhcpd">DHCP Server</option>
50
		<option VALUE="syslog">Syslog</option>
51
		<option VALUE="system">System</option>
52
	</select>
53
EOD;
54
	echo $select;
55

    
56
}
57

    
58
if ($_POST) {
59
	unset($input_errors);
60
	if (stristr($_POST['Submit'], "Restore configuration"))
61
		$mode = "restore";
62
	else if (stristr($_POST['Submit'], "Reinstall"))
63
		$mode = "reinstallpackages";
64
	else if (stristr($_POST['Submit'], "Download"))
65
		$mode = "download";
66
	else if (stristr($_POST['Submit'], "Restore version"))
67
		$mode = "restore_ver";
68

    
69
	if ($_POST["nopackages"] <> "")
70
		$options = "nopackages";
71

    
72
	if ($_POST["ver"] <> "")
73
		$ver2restore = $_POST["ver"];
74

    
75
	if ($mode) {
76
		if ($mode == "download") {
77
			config_lock();
78
			$fn = "config-" . $config['system']['hostname'] . "." .
79
				$config['system']['domain'] . "-" . date("YmdHis") . ".xml";
80
			if($options == "nopackages") {
81
				exec("sed '/<installedpackages>/,/<\/installedpackages>/d' /conf/config.xml > /tmp/config.xml.nopkg");
82
				$fs = filesize("{$g['tmp_path']}/config.xml.nopkg");
83
				header("Content-Type: application/octet-stream");
84
                        	header("Content-Disposition: attachment; filename=$fn");
85
                        	header("Content-Length: $fs");
86
				readfile("{$g['tmp_path']}/config.xml.nopkg");
87
			} else {
88
				if($_POST['backuparea'] <> "") {
89
					/* user wishes to backup specific area of configuration */
90
					$current_trafficshaper_section = backup_config_section($_POST['backuparea']);
91
					/* generate aliases xml */
92
					$fout = fopen("{$g['tmp_path']}/backup_section.txt","w");
93
					fwrite($fout, $current_trafficshaper_section);
94
					fclose($fout);
95
					$fs = filesize($g['tmp_path'] . "/backup_section.txt");
96
					header("Content-Type: application/octet-stream");
97
					$fn = $_POST['backuparea'] . "-" . $fn;
98
					header("Content-Disposition: attachment; filename=$fn");
99
					header("Content-Length: $fs");
100
					readfile($g['tmp_path'] . "/backup_section.txt");
101
					unlink($g['tmp_path'] . "/backup_section.txt");
102
				} else {
103
					$fs = filesize($g['conf_path'] . "/config.xml");
104
					header("Content-Type: application/octet-stream");
105
					header("Content-Disposition: attachment; filename=$fn");
106
					header("Content-Length: $fs");
107
					readfile($g['conf_path'] . "/config.xml");
108
				}
109
			}
110
			config_unlock();
111
			exit;
112
		} else if ($mode == "restore") {
113
			if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
114
				$fd = fopen($_FILES['conffile']['tmp_name'], "r");
115
				if(!$fd) {
116
					log_error("Warning, could not open " . $_FILES['conffile']['tmp_name']);
117
					return 1;
118
				}
119
				while(!feof($fd)) {
120
					    $tmp .= fread($fd,49);
121
				}
122
				fclose($fd);
123
				if(stristr($tmp, "m0n0wall") == true) {
124
					log_error("Upgrading m0n0wall configuration to pfsense.");
125
					/* m0n0wall was found in config.  convert it. */
126
					$upgradedconfig = str_replace("m0n0wall", "pfsense", $tmp);
127
					$fd = fopen($_FILES['conffile']['tmp_name'], "w");
128
					fwrite($fd, $upgradedconfig);
129
					fclose($fd);
130
					$m0n0wall_upgrade = true;
131
				}
132
				if($_POST['restorearea'] <> "") {
133
					/* restore a specific area of the configuration */
134
					$rules = file_get_contents($_FILES['conffile']['tmp_name']);
135
					if(stristr($rules, $_POST['restorearea']) == false) {
136
						$input_errors[] = "You have selected to restore a area but we could not locate the correct xml tag.";
137
					} else {
138
						restore_config_section($_POST['restorearea'], $rules);
139
						filter_configure();
140
						$savemsg = "The configuration area has been restored.  The firewall is now rebooting.";
141
					}
142
				} else {
143
					$rules = file_get_contents($_FILES['conffile']['tmp_name']);
144
					if(stristr($rules, "pfsense") == false) {
145
						$input_errors[] = "You have selected to restore the full configuration but we could not locate a pfsense tag.";
146
					} else {
147
						/* restore the entire configuration */
148
						if (config_install($_FILES['conffile']['tmp_name']) == 0) {
149
							/* this will be picked up by /index.php */
150
							conf_mount_rw();
151
							touch("/needs_package_sync");
152
							$reboot_needed = true;
153
							$savemsg = "The configuration has been restored. The firewall is now rebooting.";
154
							/* remove cache, we will force a config reboot */
155
							if(file_exists("/tmp/config.cache"))
156
								unlink("/tmp/config.cache");
157
							$config = parse_config(true);
158
							if($m0n0wall_upgrade == true) {
159
								if($config['system']['gateway'] <> "") {
160
									$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
161
								}
162
								unset($config['shaper']);
163
								write_config();
164
								conf_mount_ro();
165
								$savemsg = "The m0n0wall configuration has been restored and upgraded to pfSense.<p>The firewall is now rebooting.";
166
								$reboot_needed = true;
167
							}
168
							if(isset($config['captiveportal']['enable'])) {
169
								/* for some reason ipfw doesn't init correctly except on bootup sequence */
170
								$savemsg = "The configuration has been restored.<p>The firewall is now rebooting.";
171
								$reboot_needed = true;
172
							}
173
							setup_serial_port();
174
						} else {
175
							$input_errors[] = "The configuration could not be restored.";
176
						}
177
					}
178
				}
179
			} else {
180
				$input_errors[] = "The configuration could not be restored (file upload error).";
181
			}
182
		} else if ($mode == "reinstallpackages") {
183
			header("Location: pkg_mgr_install.php?mode=reinstallall");
184
			exit;
185
                } else if ($mode == "restore_ver") {
186
			$input_errors[] = "XXX - this feature may hose your config (do NOT backrev configs!) - billm";
187
			if ($ver2restore <> "") {
188
				$conf_file = "{$g['cf_conf_path']}/bak/config-" . strtotime($ver2restore) . ".xml";
189
                                if (config_install($conf_file) == 0) {
190
									$reboot_needed = true;
191
                                    $savemsg = "The configuration has been restored. The firewall is now rebooting.";
192
                                } else {
193
                                	$input_errors[] = "The configuration could not be restored.";
194
                                }
195
                        } else {
196
                                $input_errors[] = "No version selected.";
197
                        }
198
		}
199
	}
200
}
201

    
202
$id = rand() . '.' . time();
203

    
204
$mth = ini_get('upload_progress_meter.store_method');
205
$dir = ini_get('upload_progress_meter.file.filename_template');
206

    
207
$pgtitle = "Diagnostics: Backup/restore";
208
include("head.inc");
209

    
210
?>
211

    
212
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
213
<?php include("fbegin.inc"); ?>
214
<p class="pgtitle"><?=$pgtitle?></p>
215

    
216
<form action="diag_backup.php" method="post" enctype="multipart/form-data">
217
<?php if ($input_errors) print_input_errors($input_errors); ?>
218
<?php if ($savemsg) print_info_box($savemsg); ?>
219
<table width="100%" border="0" cellspacing="0" cellpadding="0">
220
	<tr>
221
		<td>
222
<?php
223
		$tab_array = array();
224
		$tab_array[0] = array("Remote", false, "diag_confbak.php");
225
		$tab_array[1] = array("Local", true, "diag_backup.php");
226
		display_top_tabs($tab_array);
227
?>			
228
		</td>
229
	</tr>
230
	<tr>
231
		<td>
232
			<div id="mainarea">
233
			<table class="tabcont" align="center" width="100%" border="0" cellpadding="6" cellspacing="0">
234
				<tr>
235
					<td colspan="2" class="listtopic">Backup configuration</td>
236
				</tr>
237
				<tr>
238
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
239
					<td width="78%" class="vtable">
240
						<p>Click this button to download the system configuration in XML format.<br /><br /> Backup area: <?php spit_out_select_items("backuparea"); ?></p>
241
						<p><input name="nopackages" type="checkbox" class="formcheckbox" id="nopackages">Do not backup package information.</p>
242
						<p><input name="Submit" type="submit" class="formbtn" id="download" value="Download configuration"></p>
243
					</td>
244
				</tr>
245
				<tr>
246
					<td colspan="2" class="list" height="12">&nbsp;</td>
247
                </tr>
248
                <tr>
249
					<td colspan="2" class="listtopic">Restore configuration</td>
250
				</tr>
251
				<tr>
252
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
253
					<td width="78%" class="vtable">
254
						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"); ?>
255
						<p><input name="conffile" type="file" class="formfld" id="conffile" size="40"></p>
256
						<p><input name="Submit" type="submit" class="formbtn" id="restore" value="Restore configuration"></p>
257
                      	<p><strong><span class="red">Note:</span></strong><br />The firewall will reboot after restoring the configuration.<br /></p>
258
					</td>
259
				</tr>
260
				<?php if($config['installedpackages']['package'] != "") { ?>
261
				<tr>
262
					<td colspan="2" class="list" height="12">&nbsp;</td>
263
				</tr>
264
				<tr>
265
					<td colspan="2" class="listtopic">Reinstall packages</td>
266
				</tr>
267
				<tr>
268
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
269
					<td width="78%" class="vtable">
270
						<p>Click this button to reinstall all system packages.  This may take a while. <br /><br />
271
		  				<input name="Submit" type="submit" class="formbtn" id="reinstallpackages" value="Reinstall packages">
272
					</td>
273
				</tr>
274
				<?php } ?>
275
			</table>
276
			</div>
277
		</td>
278
	</tr>
279
</table>
280
</form>
281

    
282
<?php include("fend.inc"); ?>
283
</body>
284
</html>
285

    
286
<?php
287

    
288
if($reboot_needed == true) {
289
	ob_flush();
290
	flush();
291
	sleep(5);
292
	while(file_exists("{$g['varrun_path']}/config.lock"))
293
		sleep(3);
294
	mwexec("/sbin/shutdown -r now");
295
	exit;
296
}
297

    
298
?>
(5-5/163)