Project

General

Profile

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

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

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

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

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

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

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

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

    
57
}
58

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

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

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

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

    
196
$id = rand() . '.' . time();
197

    
198
$mth = ini_get('upload_progress_meter.store_method');
199
$dir = ini_get('upload_progress_meter.file.filename_template');
200

    
201
$pgtitle = "Diagnostics: Backup/restore";
202
include("head.inc");
203

    
204
?>
205

    
206
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
207
<?php include("fbegin.inc"); ?>
208
<p class="pgtitle"><?=$pgtitle?></p>
209

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

    
276
<?php include("fend.inc"); ?>
277
</body>
278
</html>
279

    
280
<?php
281

    
282
if($reloadall == true) {
283
	
284
	if(file_exists("{$g['tmp_path']}/config.cache"))
285
		unlink("{$g['tmp_path']}/config.cache");
286
		
287
	parse_config();
288
	
289
	reload_all();
290
	
291
}
292

    
293
?>
(5-5/153)