1
|
<?php
|
2
|
/*
|
3
|
guiconfig.inc
|
4
|
by Scott Ullrich, Copyright 2004, All rights reserved.
|
5
|
originally based on 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
|
/* make sure nothing is cached */
|
33
|
if (!$omit_nocacheheaders) {
|
34
|
header("Expires: 0");
|
35
|
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
|
36
|
header("Cache-Control: no-store, no-cache, must-revalidate");
|
37
|
header("Cache-Control: post-check=0, pre-check=0", false);
|
38
|
header("Pragma: no-cache");
|
39
|
}
|
40
|
|
41
|
/* parse the configuration and include all configuration functions */
|
42
|
require_once("config.inc");
|
43
|
require_once("functions.inc");
|
44
|
|
45
|
$d_natconfdirty_path = $g['varrun_path'] . "/nat.conf.dirty";
|
46
|
$d_filterconfdirty_path = $g['varrun_path'] . "/filter.conf.dirty";
|
47
|
$d_ipsecconfdirty_path = $g['varrun_path'] . "/ipsec.conf.dirty";
|
48
|
$d_shaperconfdirty_path = $g['varrun_path'] . "/shaper.conf.dirty";
|
49
|
$d_pptpuserdirty_path = $g['varrun_path'] . "/pptpd.user.dirty";
|
50
|
$d_hostsdirty_path = $g['varrun_path'] . "/hosts.dirty";
|
51
|
$d_staticmapsdirty_path = $g['varrun_path'] . "/staticmaps.dirty";
|
52
|
$d_staticroutesdirty_path = $g['varrun_path'] . "/staticroutes.dirty";
|
53
|
$d_aliasesdirty_path = $g['varrun_path'] . "/aliases.dirty";
|
54
|
$d_proxyarpdirty_path = $g['varrun_path'] . "/proxyarp.dirty";
|
55
|
$d_fwupenabled_path = $g['varrun_path'] . "/fwup.enabled";
|
56
|
$d_firmwarelock_path = $g['varrun_path'] . "/firmware.lock";
|
57
|
$d_sysrebootreqd_path = $g['varrun_path'] . "/sysreboot.reqd";
|
58
|
$d_passthrumacsdirty_path = $g['varrun_path'] . "/passthrumacs.dirty";
|
59
|
$d_allowedipsdirty_path = $g['varrun_path'] . "/allowedips.dirty";
|
60
|
$d_ovpnclidirty_path = $g['varrun_path'] . "/ovpnclient.dirty";
|
61
|
|
62
|
if (file_exists($d_firmwarelock_path)) {
|
63
|
if (!$d_isfwfile) {
|
64
|
header("Location: system_firmware.php");
|
65
|
exit;
|
66
|
} else {
|
67
|
return;
|
68
|
}
|
69
|
}
|
70
|
|
71
|
/* some well knows ports */
|
72
|
$wkports = array(3389 => "MS RDP", 21 => "FTP", 22 => "SSH", 23 => "Telnet", 25 => "SMTP",
|
73
|
53 => "DNS", 80 => "HTTP", 110 => "POP3", 143 => "IMAP", 443 => "HTTPS");
|
74
|
|
75
|
$iptos = array("lowdelay", "throughput", "reliability", "mincost", "congestion");
|
76
|
/* TCP flags */
|
77
|
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg");
|
78
|
|
79
|
$specialnets = array("lan" => "LAN net", "pptp" => "PPTP clients");
|
80
|
|
81
|
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
|
82
|
$specialnets['opt' . $i] = $config['interfaces']['opt' . $i]['descr'] . " net";
|
83
|
}
|
84
|
|
85
|
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
|
86
|
"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
|
87
|
"10half" => "10BASE-T half-duplex");
|
88
|
|
89
|
/* platforms that support firmware updating */
|
90
|
$fwupplatforms = array('pfSense', 'net45xx', 'net48xx', 'generic-pc', 'wrap');
|
91
|
|
92
|
/* IPsec defines */
|
93
|
$my_identifier_list = array('myaddress' => 'My IP address',
|
94
|
'address' => 'IP address',
|
95
|
'fqdn' => 'Domain name',
|
96
|
'user_fqdn' => 'User FQDN');
|
97
|
|
98
|
$p1_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
|
99
|
'cast128' => 'CAST128');
|
100
|
$p2_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
|
101
|
'cast128' => 'CAST128', 'rijndael' => 'Rijndael (AES)');
|
102
|
$p1_halgos = array('sha1' => 'SHA1', 'md5' => 'MD5');
|
103
|
$p2_halgos = array('hmac_sha1' => 'SHA1', 'hmac_md5' => 'MD5');
|
104
|
$p2_protos = array('esp' => 'ESP', 'ah' => 'AH');
|
105
|
$p2_pfskeygroups = array('0' => 'off', '1' => '1', '2' => '2', '5' => '5');
|
106
|
|
107
|
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
|
108
|
|
109
|
/* check for bad control characters */
|
110
|
foreach ($postdata as $pn => $pd) {
|
111
|
if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
|
112
|
$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
|
113
|
}
|
114
|
}
|
115
|
|
116
|
for ($i = 0; $i < count($reqdfields); $i++) {
|
117
|
if (!$_POST[$reqdfields[$i]]) {
|
118
|
$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
|
119
|
}
|
120
|
}
|
121
|
}
|
122
|
|
123
|
function print_input_errors($input_errors) {
|
124
|
echo "<p><table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
|
125
|
echo "<tr><td bgcolor=\"#A12A2A\" width=\"36\" align=\"center\" valign=\"top\"><img src=\"/err.gif\" width=\"28\" height=\"32\"></td>\n";
|
126
|
echo "<td bgcolor=\"#FFD9D1\" style=\"padding-left: 8px; padding-top: 6px\">";
|
127
|
|
128
|
echo "<span class=\"errmsg\"><p>The following input errors were detected:<ul>\n";
|
129
|
foreach ($input_errors as $ierr) {
|
130
|
echo "<li>" . htmlspecialchars($ierr) . "</li>\n";
|
131
|
}
|
132
|
echo "</ul></span>";
|
133
|
|
134
|
echo "</td></tr></table></p>";
|
135
|
}
|
136
|
|
137
|
function exec_rc_script($scriptname) {
|
138
|
|
139
|
global $d_sysrebootreqd_path;
|
140
|
|
141
|
if (file_exists($d_sysrebootreqd_path))
|
142
|
return 0;
|
143
|
|
144
|
exec($scriptname . " >/dev/null 2>&1", $execoutput, $retval);
|
145
|
return $retval;
|
146
|
}
|
147
|
|
148
|
function exec_rc_script_async($scriptname) {
|
149
|
|
150
|
global $d_sysrebootreqd_path;
|
151
|
|
152
|
if (file_exists($d_sysrebootreqd_path))
|
153
|
return 0;
|
154
|
|
155
|
exec("nohup " . $scriptname . " >/dev/null 2>&1 &", $execoutput, $retval);
|
156
|
return $retval;
|
157
|
}
|
158
|
|
159
|
function verify_gzip_file($fname) {
|
160
|
|
161
|
$returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
|
162
|
if ($returnvar != 0)
|
163
|
return 0;
|
164
|
else
|
165
|
return 1;
|
166
|
}
|
167
|
|
168
|
function print_info_box_np($msg) {
|
169
|
echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
|
170
|
echo "<tr><td bgcolor=\"#990000\" align=\"center\" valign=\"top\" width=\"36\"><img src=\"/exclam.gif\" width=\"28\" height=\"32\"></td>\n";
|
171
|
echo "<td bgcolor=\"#00000\" style=\"padding-left: 8px\"><font color=\"#ffffff\">";
|
172
|
echo $msg;
|
173
|
echo "</td></tr></table><br>";
|
174
|
}
|
175
|
|
176
|
function print_info_box($msg) {
|
177
|
echo "<p>";
|
178
|
print_info_box_np($msg);
|
179
|
echo "</p>";
|
180
|
}
|
181
|
|
182
|
function format_bytes($bytes) {
|
183
|
if ($bytes >= 1073741824) {
|
184
|
return sprintf("%.2f GB", $bytes/1073741824);
|
185
|
} else if ($bytes >= 1048576) {
|
186
|
return sprintf("%.2f MB", $bytes/1048576);
|
187
|
} else if ($bytes >= 1024) {
|
188
|
return sprintf("%.0f KB", $bytes/1024);
|
189
|
} else {
|
190
|
return sprintf("%d bytes", $bytes);
|
191
|
}
|
192
|
}
|
193
|
|
194
|
function get_std_save_message($ok) {
|
195
|
global $d_sysrebootreqd_path;
|
196
|
|
197
|
if ($ok == 0) {
|
198
|
if (file_exists($d_sysrebootreqd_path))
|
199
|
return "The changes have been saved. You must <a class=\"navlnk\" href=\"/reboot.php\">reboot</a> your firewall for changes to take effect.";
|
200
|
else
|
201
|
return "The changes have been applied successfully.";
|
202
|
} else {
|
203
|
return "ERROR: the changes could not be applied (error code $ok).";
|
204
|
}
|
205
|
}
|
206
|
|
207
|
function pprint_address($adr) {
|
208
|
global $specialnets;
|
209
|
|
210
|
if (isset($adr['any'])) {
|
211
|
$padr = "*";
|
212
|
} else if ($adr['network']) {
|
213
|
$padr = $specialnets[$adr['network']];
|
214
|
} else {
|
215
|
$padr = $adr['address'];
|
216
|
}
|
217
|
|
218
|
if (isset($adr['not']))
|
219
|
$padr = "! " . $padr;
|
220
|
|
221
|
return $padr;
|
222
|
}
|
223
|
|
224
|
function pprint_port($port) {
|
225
|
global $wkports;
|
226
|
|
227
|
$pport = "";
|
228
|
|
229
|
if (!$port)
|
230
|
echo "*";
|
231
|
else {
|
232
|
$srcport = explode("-", $port);
|
233
|
if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
|
234
|
$pport = $srcport[0];
|
235
|
if ($wkports[$srcport[0]]) {
|
236
|
$pport .= " (" . $wkports[$srcport[0]] . ")";
|
237
|
}
|
238
|
} else
|
239
|
$pport .= $srcport[0] . " - " . $srcport[1];
|
240
|
}
|
241
|
|
242
|
return $pport;
|
243
|
}
|
244
|
|
245
|
/* sort by interface only, retain the original order of rules that apply to
|
246
|
the same interface */
|
247
|
function filter_rules_sort() {
|
248
|
global $g, $config;
|
249
|
|
250
|
/* mark each rule with the sequence number (to retain the order while sorting) */
|
251
|
for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
|
252
|
$config['filter']['rule'][$i]['seq'] = $i;
|
253
|
|
254
|
function filtercmp($a, $b) {
|
255
|
if ($a['interface'] == $b['interface'])
|
256
|
return $a['seq'] - $b['seq'];
|
257
|
else
|
258
|
return -strcmp($a['interface'], $b['interface']);
|
259
|
}
|
260
|
|
261
|
usort($config['filter']['rule'], "filtercmp");
|
262
|
|
263
|
/* strip the sequence numbers again */
|
264
|
for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
|
265
|
unset($config['filter']['rule'][$i]['seq']);
|
266
|
}
|
267
|
|
268
|
function nat_rules_sort() {
|
269
|
global $g, $config;
|
270
|
|
271
|
function natcmp($a, $b) {
|
272
|
if ($a['external-address'] == $b['external-address']) {
|
273
|
if ($a['protocol'] == $b['protocol']) {
|
274
|
if ($a['external-port'] == $b['external-port']) {
|
275
|
return 0;
|
276
|
} else {
|
277
|
return ($a['external-port'] - $b['external-port']);
|
278
|
}
|
279
|
} else {
|
280
|
return strcmp($a['protocol'], $b['protocol']);
|
281
|
}
|
282
|
} else if (!$a['external-address'])
|
283
|
return 1;
|
284
|
else if (!$b['external-address'])
|
285
|
return -1;
|
286
|
else
|
287
|
return ipcmp($a['external-address'], $b['external-address']);
|
288
|
}
|
289
|
|
290
|
usort($config['nat']['rule'], "natcmp");
|
291
|
}
|
292
|
|
293
|
function nat_1to1_rules_sort() {
|
294
|
global $g, $config;
|
295
|
|
296
|
function nat1to1cmp($a, $b) {
|
297
|
return ipcmp($a['external'], $b['external']);
|
298
|
}
|
299
|
|
300
|
usort($config['nat']['onetoone'], "nat1to1cmp");
|
301
|
}
|
302
|
|
303
|
function nat_server_rules_sort() {
|
304
|
global $g, $config;
|
305
|
|
306
|
function natservercmp($a, $b) {
|
307
|
return ipcmp($a['ipaddr'], $b['ipaddr']);
|
308
|
}
|
309
|
|
310
|
usort($config['nat']['servernat'], "natservercmp");
|
311
|
}
|
312
|
|
313
|
function nat_out_rules_sort() {
|
314
|
global $g, $config;
|
315
|
|
316
|
function natoutcmp($a, $b) {
|
317
|
return strcmp($a['source']['network'], $b['source']['network']);
|
318
|
}
|
319
|
|
320
|
usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
|
321
|
}
|
322
|
|
323
|
function pptpd_users_sort() {
|
324
|
global $g, $config;
|
325
|
|
326
|
function usercmp($a, $b) {
|
327
|
return strcasecmp($a['name'], $b['name']);
|
328
|
}
|
329
|
|
330
|
usort($config['pptpd']['user'], "usercmp");
|
331
|
}
|
332
|
|
333
|
function staticroutes_sort() {
|
334
|
global $g, $config;
|
335
|
|
336
|
function staticroutecmp($a, $b) {
|
337
|
return strcmp($a['network'], $b['network']);
|
338
|
}
|
339
|
|
340
|
usort($config['staticroutes']['route'], "staticroutecmp");
|
341
|
}
|
342
|
|
343
|
function hosts_sort() {
|
344
|
global $g, $config;
|
345
|
|
346
|
function hostcmp($a, $b) {
|
347
|
return strcasecmp($a['host'], $b['host']);
|
348
|
}
|
349
|
|
350
|
usort($config['dnsmasq']['hosts'], "hostcmp");
|
351
|
}
|
352
|
|
353
|
function staticmaps_sort($if) {
|
354
|
global $g, $config;
|
355
|
|
356
|
function staticmapcmp($a, $b) {
|
357
|
return ipcmp($a['ipaddr'], $b['ipaddr']);
|
358
|
}
|
359
|
|
360
|
usort($config['dhcpd'][$if]['staticmap'], "staticmapcmp");
|
361
|
}
|
362
|
|
363
|
function aliases_sort() {
|
364
|
global $g, $config;
|
365
|
|
366
|
function aliascmp($a, $b) {
|
367
|
return strcmp($a['name'], $b['name']);
|
368
|
}
|
369
|
|
370
|
usort($config['aliases']['alias'], "aliascmp");
|
371
|
}
|
372
|
|
373
|
function ipsec_mobilekey_sort() {
|
374
|
global $g, $config;
|
375
|
|
376
|
function mobilekeycmp($a, $b) {
|
377
|
return strcmp($a['ident'][0], $b['ident'][0]);
|
378
|
}
|
379
|
|
380
|
usort($config['ipsec']['mobilekey'], "mobilekeycmp");
|
381
|
}
|
382
|
|
383
|
function proxyarp_sort() {
|
384
|
global $g, $config;
|
385
|
|
386
|
function proxyarpcmp($a, $b) {
|
387
|
if (isset($a['network']))
|
388
|
list($ast,$asn) = explode("/", $a['network']);
|
389
|
else if (isset($a['range'])) {
|
390
|
$ast = $a['range']['from'];
|
391
|
$asn = 32;
|
392
|
}
|
393
|
if (isset($b['network']))
|
394
|
list($bst,$bsn) = explode("/", $b['network']);
|
395
|
else if (isset($b['range'])) {
|
396
|
$bst = $b['range']['from'];
|
397
|
$bsn = 32;
|
398
|
}
|
399
|
if (ipcmp($ast, $bst) == 0)
|
400
|
return ($asn - $bsn);
|
401
|
else
|
402
|
return ipcmp($ast, $bst);
|
403
|
}
|
404
|
|
405
|
usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
|
406
|
}
|
407
|
|
408
|
function passthrumacs_sort() {
|
409
|
global $g, $config;
|
410
|
|
411
|
function passthrumacscmp($a, $b) {
|
412
|
return strcmp($a['mac'], $b['mac']);
|
413
|
}
|
414
|
|
415
|
usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
|
416
|
}
|
417
|
|
418
|
function allowedips_sort() {
|
419
|
global $g, $config;
|
420
|
|
421
|
function allowedipscmp($a, $b) {
|
422
|
return strcmp($a['ip'], $b['ip']);
|
423
|
}
|
424
|
|
425
|
usort($config['captiveportal']['allowedip'],"allowedipscmp");
|
426
|
}
|
427
|
|
428
|
function wol_sort() {
|
429
|
global $g, $config;
|
430
|
|
431
|
function wolcmp($a, $b) {
|
432
|
return strcmp($a['descr'], $b['descr']);
|
433
|
}
|
434
|
|
435
|
usort($config['wol']['wolentry'], "wolcmp");
|
436
|
}
|
437
|
|
438
|
function gentitle($pgname) {
|
439
|
global $config;
|
440
|
return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
|
441
|
}
|
442
|
|
443
|
?>
|