Project

General

Profile

Download (12.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	guiconfig.inc
4
	part of m0n0wall (http://m0n0.ch/wall)
5
	
6
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
7
	All rights reserved.
8
	
9
	Redistribution and use in source and binary forms, with or without
10
	modification, are permitted provided that the following conditions are met:
11
	
12
	1. Redistributions of source code must retain the above copyright notice,
13
	   this list of conditions and the following disclaimer.
14
	
15
	2. Redistributions in binary form must reproduce the above copyright
16
	   notice, this list of conditions and the following disclaimer in the
17
	   documentation and/or other materials provided with the distribution.
18
	
19
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
	POSSIBILITY OF SUCH DAMAGE.
29
*/
30

    
31
/* make sure nothing is cached */
32
if (!$omit_nocacheheaders) {
33
	header("Expires: 0");
34
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
35
	header("Cache-Control: no-store, no-cache, must-revalidate");
36
	header("Cache-Control: post-check=0, pre-check=0", false);
37
	header("Pragma: no-cache");
38
}
39

    
40
/* parse the configuration and include all configuration functions */
41
require_once("config.inc");
42
require_once("functions.inc");
43

    
44
$d_natconfdirty_path = $g['varrun_path'] . "/nat.conf.dirty";
45
$d_filterconfdirty_path = $g['varrun_path'] . "/filter.conf.dirty";
46
$d_ipsecconfdirty_path = $g['varrun_path'] . "/ipsec.conf.dirty";
47
$d_shaperconfdirty_path = $g['varrun_path'] . "/shaper.conf.dirty";
48
$d_pptpuserdirty_path = $g['varrun_path'] . "/pptpd.user.dirty";
49
$d_hostsdirty_path = $g['varrun_path'] . "/hosts.dirty";
50
$d_staticmapsdirty_path = $g['varrun_path'] . "/staticmaps.dirty";
51
$d_staticroutesdirty_path = $g['varrun_path'] . "/staticroutes.dirty";
52
$d_aliasesdirty_path = $g['varrun_path'] . "/aliases.dirty";
53
$d_proxyarpdirty_path = $g['varrun_path'] . "/proxyarp.dirty";
54
$d_fwupenabled_path = $g['varrun_path'] . "/fwup.enabled";
55
$d_firmwarelock_path = $g['varrun_path'] . "/firmware.lock";
56
$d_sysrebootreqd_path = $g['varrun_path'] . "/sysreboot.reqd";
57
$d_passthrumacsdirty_path = $g['varrun_path'] . "/passthrumacs.dirty";
58
$d_allowedipsdirty_path = $g['varrun_path'] . "/allowedips.dirty";
59
$d_ovpnclidirty_path = $g['varrun_path'] . "/ovpnclient.dirty";
60

    
61
if (file_exists($d_firmwarelock_path)) {
62
	if (!$d_isfwfile) {
63
		header("Location: system_firmware.php");
64
		exit;
65
	} else {
66
		return;
67
	}
68
}
69

    
70
/* some well knows ports */
71
$wkports = array(21 => "FTP", 22 => "SSH", 23 => "Telnet", 25 => "SMTP", 53 => "DNS", 80 => "HTTP",
72
				 110 => "POP3", 143 => "IMAP", 443 => "HTTPS");
73

    
74
$iptos = array("lowdelay", "throughput", "reliability", "mincost", "congestion");
75
/* TCP flags */
76
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg");
77

    
78
$specialnets = array("lan" => "LAN net", "pptp" => "PPTP clients");
79

    
80
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
81
	$specialnets['opt' . $i] = $config['interfaces']['opt' . $i]['descr'] . " net";
82
}
83
	
84
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
85
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
86
	"10half" => "10BASE-T half-duplex");
87
	
88
/* platforms that support firmware updating */
89
$fwupplatforms = array('net45xx', 'net48xx', 'generic-pc', 'wrap');
90

    
91
/* IPsec defines */
92
$my_identifier_list = array('myaddress' => 'My IP address',
93
							'address' => 'IP address',
94
							'fqdn' => 'Domain name',
95
							'user_fqdn' => 'User FQDN');
96

    
97
$p1_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish', 
98
					'cast128' => 'CAST128');
99
$p2_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
100
					'cast128' => 'CAST128', 'rijndael' => 'Rijndael (AES)');
101
$p1_halgos = array('sha1' => 'SHA1', 'md5' => 'MD5');
102
$p2_halgos = array('hmac_sha1' => 'SHA1', 'hmac_md5' => 'MD5');
103
$p2_protos = array('esp' => 'ESP', 'ah' => 'AH');
104
$p2_pfskeygroups = array('0' => 'off', '1' => '1', '2' => '2', '5' => '5');
105

    
106
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
107
	
108
	/* check for bad control characters */	
109
	foreach ($postdata as $pn => $pd) {
110
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
111
			$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
112
		}
113
	}
114
	
115
	for ($i = 0; $i < count($reqdfields); $i++) {
116
		if (!$_POST[$reqdfields[$i]]) {
117
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
118
		}
119
	}	
120
}
121

    
122
function print_input_errors($input_errors) {
123
	echo "<p><table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
124
	echo "<tr><td bgcolor=\"#A12A2A\" width=\"36\" align=\"center\" valign=\"top\"><img src=\"/err.gif\" width=\"28\" height=\"32\"></td>\n";
125
	echo "<td bgcolor=\"#FFD9D1\" style=\"padding-left: 8px; padding-top: 6px\">";
126
	
127
	echo "<span class=\"errmsg\"><p>The following input errors were detected:<ul>\n";
128
	foreach ($input_errors as $ierr) {
129
		echo "<li>" . htmlspecialchars($ierr) . "</li>\n";
130
	}
131
	echo "</ul></span>";
132
	
133
	echo "</td></tr></table></p>";
134
}
135

    
136
function exec_rc_script($scriptname) {
137
	
138
	global $d_sysrebootreqd_path;
139
	
140
	if (file_exists($d_sysrebootreqd_path))
141
		return 0;
142
	
143
	exec($scriptname . " >/dev/null 2>&1", $execoutput, $retval);
144
	return $retval; 
145
}
146

    
147
function exec_rc_script_async($scriptname) {
148
	
149
	global $d_sysrebootreqd_path;
150
	
151
	if (file_exists($d_sysrebootreqd_path))
152
		return 0;
153
		
154
	exec("nohup " . $scriptname . " >/dev/null 2>&1 &", $execoutput, $retval);
155
	return $retval; 
156
}
157

    
158
function verify_gzip_file($fname) {
159

    
160
	$returnvar = mwexec("/usr/bin/gunzip -S \"\" -t " . escapeshellarg($fname));
161
	if ($returnvar != 0)
162
		return 0;
163
	else
164
		return 1;
165
}
166

    
167
function print_info_box_np($msg) {
168
	echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
169
	echo "<tr><td bgcolor=\"#687BA4\" align=\"center\" valign=\"top\" width=\"36\"><img src=\"/exclam.gif\" width=\"28\" height=\"32\"></td>\n";
170
	echo "<td bgcolor=\"#D9DEE8\" style=\"padding-left: 8px\">";
171
	echo $msg;
172
	echo "</td></tr></table>";
173
}
174

    
175
function print_info_box($msg) {
176
	echo "<p>";
177
	print_info_box_np($msg);
178
	echo "</p>";
179
}
180

    
181
function format_bytes($bytes) {
182
	if ($bytes >= 1073741824) {
183
		return sprintf("%.2f GB", $bytes/1073741824);
184
	} else if ($bytes >= 1048576) {
185
		return sprintf("%.2f MB", $bytes/1048576);
186
	} else if ($bytes >= 1024) {
187
		return sprintf("%.0f KB", $bytes/1024);
188
	} else {
189
		return sprintf("%d bytes", $bytes);
190
	}
191
}
192

    
193
function get_std_save_message($ok) {
194
	global $d_sysrebootreqd_path;
195

    
196
	if ($ok == 0) {
197
		if (file_exists($d_sysrebootreqd_path))
198
			return "The changes have been saved. You must <a href=\"/reboot.php\">reboot</a> your firewall for changes to take effect.";
199
		else
200
			return "The changes have been applied successfully.";
201
	} else {
202
		return "ERROR: the changes could not be applied (error code $ok).";
203
	}
204
}
205

    
206
function pprint_address($adr) {
207
	global $specialnets;
208

    
209
	if (isset($adr['any'])) {
210
		$padr = "*";
211
	} else if ($adr['network']) {
212
		$padr = $specialnets[$adr['network']];
213
	} else {
214
		$padr = $adr['address'];
215
	}
216
	
217
	if (isset($adr['not']))
218
		$padr = "! " . $padr;
219
	
220
	return $padr;
221
}
222

    
223
function pprint_port($port) {
224
	global $wkports;
225

    
226
	$pport = "";
227
		
228
	if (!$port)
229
		echo "*";
230
	else {
231
		$srcport = explode("-", $port);
232
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
233
			$pport = $srcport[0];
234
			if ($wkports[$srcport[0]]) {
235
				$pport .= " (" . $wkports[$srcport[0]] . ")";
236
			}
237
		} else
238
			$pport .= $srcport[0] . " - " . $srcport[1];
239
	}
240
	
241
	return $pport;
242
}
243

    
244
/* sort by interface only, retain the original order of rules that apply to
245
   the same interface */
246
function filter_rules_sort() {
247
	global $g, $config;
248
	
249
	/* mark each rule with the sequence number (to retain the order while sorting) */
250
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
251
		$config['filter']['rule'][$i]['seq'] = $i;
252
	
253
	function filtercmp($a, $b) {
254
		if ($a['interface'] == $b['interface'])
255
			return $a['seq'] - $b['seq'];
256
		else
257
			return -strcmp($a['interface'], $b['interface']);
258
	}
259
	
260
	usort($config['filter']['rule'], "filtercmp");
261
	
262
	/* strip the sequence numbers again */
263
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
264
		unset($config['filter']['rule'][$i]['seq']);
265
}
266

    
267
function nat_rules_sort() {
268
	global $g, $config;
269
	
270
	function natcmp($a, $b) {
271
		if ($a['external-address'] == $b['external-address']) {
272
			if ($a['protocol'] == $b['protocol']) {
273
				if ($a['external-port'] == $b['external-port']) {
274
					return 0;
275
				} else {
276
					return ($a['external-port'] - $b['external-port']);
277
				}
278
			} else {
279
				return strcmp($a['protocol'], $b['protocol']);
280
			}
281
		} else if (!$a['external-address'])
282
			return 1;
283
		else if (!$b['external-address'])
284
			return -1;
285
		else
286
			return ipcmp($a['external-address'], $b['external-address']);
287
	}
288
	
289
	usort($config['nat']['rule'], "natcmp");
290
}
291

    
292
function nat_1to1_rules_sort() {
293
	global $g, $config;
294
	
295
	function nat1to1cmp($a, $b) {
296
		return ipcmp($a['external'], $b['external']);
297
	}
298
	
299
	usort($config['nat']['onetoone'], "nat1to1cmp");
300
}
301

    
302
function nat_server_rules_sort() {
303
	global $g, $config;
304
	
305
	function natservercmp($a, $b) {
306
		return ipcmp($a['ipaddr'], $b['ipaddr']);
307
	}
308
	
309
	usort($config['nat']['servernat'], "natservercmp");
310
}
311

    
312
function nat_out_rules_sort() {
313
	global $g, $config;
314
	
315
	function natoutcmp($a, $b) {
316
		return strcmp($a['source']['network'], $b['source']['network']);
317
	}
318
	
319
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
320
}
321

    
322
function pptpd_users_sort() {
323
	global $g, $config;
324
	
325
	function usercmp($a, $b) {
326
		return strcasecmp($a['name'], $b['name']);
327
	}
328
	
329
	usort($config['pptpd']['user'], "usercmp");
330
}
331

    
332
function staticroutes_sort() {
333
	global $g, $config;
334

    
335
	function staticroutecmp($a, $b) {
336
		return strcmp($a['network'], $b['network']);
337
	}
338

    
339
	usort($config['staticroutes']['route'], "staticroutecmp");
340
}
341

    
342
function hosts_sort() {
343
	global $g, $config;
344

    
345
	function hostcmp($a, $b) {
346
		return strcasecmp($a['host'], $b['host']);
347
	}
348

    
349
	usort($config['dnsmasq']['hosts'], "hostcmp");
350
}
351

    
352
function staticmaps_sort($if) {
353
	global $g, $config;
354

    
355
	function staticmapcmp($a, $b) {
356
		return ipcmp($a['ipaddr'], $b['ipaddr']);
357
	}
358

    
359
	usort($config['dhcpd'][$if]['staticmap'], "staticmapcmp");
360
}
361

    
362
function aliases_sort() {
363
	global $g, $config;
364

    
365
	function aliascmp($a, $b) {
366
		return strcmp($a['name'], $b['name']);
367
	}
368

    
369
	usort($config['aliases']['alias'], "aliascmp");
370
}
371

    
372
function ipsec_mobilekey_sort() {
373
	global $g, $config;
374

    
375
	function mobilekeycmp($a, $b) {
376
		return strcmp($a['ident'][0], $b['ident'][0]);
377
	}
378

    
379
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
380
}
381

    
382
function proxyarp_sort() {
383
	global $g, $config;
384

    
385
	function proxyarpcmp($a, $b) {
386
		if (isset($a['network']))
387
			list($ast,$asn) = explode("/", $a['network']);
388
		else if (isset($a['range'])) {
389
			$ast = $a['range']['from'];
390
			$asn = 32;
391
		}
392
		if (isset($b['network']))
393
			list($bst,$bsn) = explode("/", $b['network']);
394
		else if (isset($b['range'])) {
395
			$bst = $b['range']['from'];
396
			$bsn = 32;
397
		}
398
		if (ipcmp($ast, $bst) == 0)
399
			return ($asn - $bsn);
400
		else
401
			return ipcmp($ast, $bst);
402
	}
403

    
404
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
405
}
406

    
407
function passthrumacs_sort() {
408
	global $g, $config;
409

    
410
	function passthrumacscmp($a, $b) {
411
		return strcmp($a['mac'], $b['mac']);
412
	}
413
	
414
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
415
}
416

    
417
function allowedips_sort() {
418
	global $g, $config;
419

    
420
	function allowedipscmp($a, $b) {
421
		return strcmp($a['ip'], $b['ip']);
422
	}
423
	
424
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
425
}
426

    
427
function wol_sort() {
428
	global $g, $config;
429

    
430
	function wolcmp($a, $b) {
431
		return strcmp($a['descr'], $b['descr']);
432
	}
433
	
434
	usort($config['wol']['wolentry'], "wolcmp");
435
}
436

    
437
function gentitle($pgname) {
438
	global $config;
439
	return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
440
}
441

    
442
?>
(36-36/86)