Project

General

Profile

Download (10.2 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
require("xmlparse_pkg.inc");
37

    
38
function spit_out_select_items($area) {
39
	$select = <<<EOD
40
	<select name="{$area}">
41
		<option VALUE="">ALL</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
				$command = "/sbin/sysctl -a | grep carp";
116
				$fd = fopen($_FILES['conffile']['tmp_name'], "r");
117
				if(!$fd) {
118
					log_error("Warning, could not open " . $_FILES['conffile']['tmp_name']);
119
					return 1;
120
				}
121
				while(!feof($fd)) {
122
					    $tmp .= fread($fd,49);
123
				}
124
				fclose($fd);
125
				if(stristr($tmp, "m0n0wall" != false)) {
126
					log_error("Upgrading m0n0wall configuration to pfsense.");
127
					/* m0n0wall was found in config.  convert it. */
128
					$upgradedconfig = str_replace("m0n0wall", "pfsense", $tmp);
129
					fopen($_FILES['conffile']['tmp_name'], "w");
130
					fwrite($fd, $upgradedconfig);
131
					fclose($fd);
132
				}
133
				if($_POST['restorearea'] <> "") {
134
					/* restore a specific area of the configuration */
135
					$rules = return_filename_as_string($_FILES['conffile']['tmp_name']);
136
					restore_config_section($_POST['restorearea'], $rules);
137
					filter_configure();
138
					$savemsg = "The configuration area has been restored.";
139
				} else {
140
					/* restore the entire configuration */
141
					if (config_install($_FILES['conffile']['tmp_name']) == 0) {
142
						system_reboot();
143
						$savemsg = "The configuration has been restored. The firewall is now rebooting.";
144
					} else {
145
						$input_errors[] = "The configuration could not be restored.";
146
					}
147
				}
148
			} else {
149
				$input_errors[] = "The configuration could not be restored (file upload error).";
150
			}
151
		} else if ($mode == "reinstallpackages") {
152
			header("Location: pkg_mgr_install.php?mode=reinstallall");
153
			exit;
154
                } else if ($mode == "restore_ver") {
155
			$input_errors[] = "XXX - this feature may hose your config (do NOT backrev configs!) - billm";
156
			if ($ver2restore <> "") {
157
				$conf_file = "{$g['cf_conf_path']}/bak/config-" . strtotime($ver2restore) . ".xml";
158
                                if (config_install($conf_file) == 0) {
159
                                        system_reboot();
160
                                        $savemsg = "The configuration has been restored. The firewall is now rebooting.";
161
                                } else {
162
                                        $input_errors[] = "The configuration could not be restored.";
163
                                }
164
                        } else {
165
                                $input_errors[] = "No version selected.";
166
                        }
167
		}
168
	}
169
}
170

    
171
$id = rand() . '.' . time();
172

    
173
$mth = ini_get('upload_progress_meter.store_method');
174
$dir = ini_get('upload_progress_meter.file.filename_template');
175

    
176
?>
177
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
178
<html>
179
<head>
180
<title><?=gentitle("Diagnostics: Backup/restore");?></title>
181
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
182
<link href="gui.css" rel="stylesheet" type="text/css">
183
</head>
184

    
185
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
186
<?php include("fbegin.inc"); ?>
187
      <p class="pgtitle">Diagnostics: Backup/restore</p>
188
            <form action="diag_backup.php" method="post" enctype="multipart/form-data" onSubmit="window.open('progress.php?conffile=<?=$id?>','UploadMeter','width=370,height=115', true); return true; ">
189
            <?php if ($input_errors) print_input_errors($input_errors); ?>
190
            <?php if ($savemsg) print_info_box($savemsg); ?>
191
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
192
		<tr><td><ul id="tabnav">
193
			<li class="tabact">Remote</a></li>
194
			<li class="tabinact"><a href="diag_confbak.php">Local</a></li>
195
		</ul></td></tr>
196
		<tr><td class="tabcont"><table align="center" width="100%" border="0" cellpadding="6" cellspacing="0">
197
                <tr>
198
                  <td colspan="2" class="listtopic">Backup configuration</td>
199
                </tr>
200
                <tr>
201
                  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
202
                  <td width="78%" class="vtable">
203
                    <p> Click this button to download the system configuration
204
                      in XML format.<br>
205
                      <br>
206
		      Backup area: <?php spit_out_select_items("backuparea"); ?>
207
		      <p>
208
		      <input name="nopackages" type="checkbox" class="formcheckbox" id="nopackages">Do not backup package information.<p>
209
                      <input name="Submit" type="submit" class="formbtn" id="download" value="Download configuration"></td>
210
                </tr>
211
                <tr>
212
                  <td colspan="2" class="list" height="12"></td>
213
                </tr>
214
                <tr>
215
                  <td colspan="2" class="listtopic">Restore configuration</td>
216
                </tr>
217
                <tr>
218
                  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
219
                  <td width="78%" class="vtable">
220
                    Open a pfSense configuration XML file and click the button
221
                      below to restore the configuration.<br>
222
                      <br>
223
		      Restore area: <?php spit_out_select_items("restorearea"); ?>
224
		      <p>
225
                      <input name="conffile" type="file" class="formfld" id="conffile" size="40">
226
                      <p>
227
                      <input name="Submit" type="submit" class="formbtn" id="restore" value="Restore configuration">
228
                      <p>
229
                      <strong><span class="red">Note:</span></strong><br>
230
                      The firewall will reboot after restoring the configuration.<br>
231
                  </td>
232
                </tr>
233
		<tr>
234
		 <td colspan="2">&nbsp;</td>
235
		</tr>
236
		<tr>
237
		 <td colspan="2" class="listtopic">Reinstall packages</td>
238
		</tr>
239
		<tr>
240
		  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
241
		  <td width="78%" class="vtable">
242
		  <p> Click this button to reinstall all system packages.  This may take a while.<br>
243
		  <br>
244
		  <input name="Submit" type="submit" class="formbtn" id="reinstallpackages" value="Reinstall packages"></td>
245
		</tr>
246
		</table>
247
		</td>
248
		</tr>
249
              </table>
250
            </form>
251
<?php include("fend.inc"); ?>
252
</body>
253
</html>
(3-3/113)