Project

General

Profile

Download (18.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	firewall_aliases_edit.php
5
	Copyright (C) 2004 Scott Ullrich
6
	All rights reserved.
7

    
8
	originially part of m0n0wall (http://m0n0.ch/wall)
9
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
	All rights reserved.
11

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

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

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

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

    
34
##|+PRIV
35
##|*IDENT=page-firewall-alias-edit
36
##|*NAME=Firewall: Alias: Edit page
37
##|*DESCR=Allow access to the 'Firewall: Alias: Edit' page.
38
##|*MATCH=firewall_aliases_edit.php*
39
##|-PRIV
40

    
41

    
42
$pgtitle = array("Firewall","Aliases","Edit");
43

    
44
require("guiconfig.inc");
45

    
46
if (!is_array($config['aliases']['alias']))
47
	$config['aliases']['alias'] = array();
48

    
49
aliases_sort();
50
$a_aliases = &$config['aliases']['alias'];
51

    
52
$id = $_GET['id'];
53
if (isset($_POST['id']))
54
	$id = $_POST['id'];
55

    
56
if (isset($id) && $a_aliases[$id]) {
57
	$pconfig['name'] = $a_aliases[$id]['name'];
58
	$pconfig['detail'] = $a_aliases[$id]['detail'];
59
	$pconfig['address'] = $a_aliases[$id]['address'];
60
	$pconfig['descr'] = html_entity_decode($a_aliases[$id]['descr']);
61

    
62
	/* optional if list */
63
	$iflist = get_configured_interface_with_descr(true, true);
64
	foreach ($iflist as $if => $ifdesc)
65
		if($ifdesc == $pconfig['descr']) 
66
			$input_errors[] = "Sorry, an interface is already named {$pconfig['descr']}.";
67

    
68
	$addresses = explode(' ', $pconfig['address']);
69
	$address = explode("/", $addresses[0]);
70
	if ($address[1])
71
		$addresssubnettest = true;
72
	else
73
		$addresssubnettest = false;	
74
	
75
	if ($addresssubnettest)
76
		$pconfig['type'] = "network";
77
	else
78
		if (is_ipaddr($address[0]))
79
			$pconfig['type'] = "host";
80
		else
81
			$pconfig['type'] = "port";
82

    
83
	if($a_aliases[$id]['aliasurl'] <> "") {
84
		$pconfig['type'] = "url";
85
		if(is_array($a_aliases[$id]['aliasurl'])) {
86
			$isfirst = 0;
87
			$pconfig['address'] = "";
88
			foreach($a_aliases[$id]['aliasurl'] as $aa) {
89
				if($isfirst == 1)
90
					$pconfig['address'] .= " ";
91
				$isfirst = 1;
92
				$pconfig['address'] .= $aa;
93
			}
94
		} else {
95
			$pconfig['address'] = $a_aliases[$id]['aliasurl'];
96
		}
97
	}
98
}
99

    
100
if ($_POST) {
101

    
102
	unset($input_errors);
103
	$pconfig = $_POST;
104

    
105
	/* input validation */
106
	if(strtolower($_POST['name']) == "pptp")
107
		$input_errors[] = gettext("Aliases may not be named PPTP.");
108

    
109
	$x = is_validaliasname($_POST['name']);
110
	if (!isset($x)) {
111
		$input_errors[] = "Reserved word used for alias name.";
112
	} else {
113
		if (is_validaliasname($_POST['name']) == false)
114
			$input_errors[] = "The alias name may only consist of the characters a-z, A-Z, 0-9, _.";
115
	}
116
	/* check for name conflicts */
117
	foreach ($a_aliases as $alias) {
118
		if (isset($id) && ($a_aliases[$id]) && ($a_aliases[$id] === $alias))
119
			continue;
120

    
121
		if ($alias['name'] == $_POST['name']) {
122
			$input_errors[] = "An alias with this name already exists.";
123
			break;
124
		}
125
	}
126

    
127
	/* check for name interface description conflicts */
128
	foreach($config['interfaces'] as $interface) {
129
		if($interface['descr'] == $_POST['name']) {
130
			$input_errors[] = "An interface description with this name already exists.";
131
			break;
132
		}
133
	}
134
	
135
	$alias = array();
136
	$alias['name'] = $_POST['name'];
137
	if($_POST['type'] == "url") {
138
		$address = "";
139
		$isfirst = 0;
140
		$address_count = 2;
141

    
142
		/* item is a url type */
143
		for($x=0; isset($_POST['address'. $x]); $x++) {
144
			if($_POST['address' . $x]) {
145
				/* fetch down and add in */
146
				$isfirst = 0;
147
				$temp_filename = tempnam("/tmp/", "alias_import");
148
				unlink($temp_filename);
149
				$fda = fopen("/tmp/tmpfetch","w");
150
				fwrite($fda, "/usr/bin/fetch -q -o \"{$temp_filename}/aliases\" \"" . $_POST['address' . $x] . "\"");
151
				fclose($fda);
152
				mwexec("mkdir -p {$temp_filename}");
153
				mwexec("/usr/bin/fetch -q -o \"{$temp_filename}/aliases\" \"" . $_POST['address' . $x] . "\"");
154
				/* if the item is tar gzipped then extract */
155
				if(stristr($_POST['address' . $x], ".tgz"))
156
					process_alias_tgz($temp_filename);
157
				if(file_exists("{$temp_filename}/aliases")) {
158
					$file_contents = file_get_contents("{$temp_filename}/aliases");
159
					$file_contents = str_replace("#", "\n#", $file_contents);
160
					$file_contents_split = split("\n", $file_contents);
161
					foreach($file_contents_split as $fc) {
162
						$tmp = trim($fc);
163
						if(stristr($fc, "#")) {
164
							$tmp_split = split("#", $tmp);
165
							$tmp = trim($tmp_split[0]);
166
						}
167
						if(trim($tmp) <> "") {
168
							if($isfirst == 1)
169
								$address .= " ";
170
							$address .= $tmp;
171
							$isfirst = 1;
172
						}
173
					}
174
					if($isfirst == 0) {
175
						/* nothing was found */
176
						$input_errors[] = "You must provide a valid URL. Could not fetch usable data.";
177
						$dont_update = true;
178
						break;
179
					}
180
					$alias['aliasurl'][] = $_POST['address' . $x];
181
					mwexec("/bin/rm -rf {$temp_filename}");
182
				} else {
183
					$input_errors[] = "You must provide a valid URL.";
184
					$dont_update = true;
185
					break;
186
				}
187
			}
188
		}
189
	} else {
190
		$address = "";
191
		$isfirst = 0;
192
		/* item is a normal alias type */
193
		for($x=0; $x<4999; $x++) {
194
			if($_POST["address{$x}"] <> "") {
195
				if ($isfirst > 0)
196
					$address .= " ";
197
				$address .= $_POST["address{$x}"];
198
				if($_POST["address_subnet{$x}"] <> "") 
199
					$address .= "/" . $_POST["address_subnet{$x}"];
200

    
201
	       			if($_POST["detail{$x}"] <> "") {
202
	       				$final_address_details .= $_POST["detail{$x}"];
203
	       			} else {
204
		       			$final_address_details .= "Entry added" . " ";
205
		       			$final_address_details .= date('r');
206
	       			}
207
	       			$final_address_details .= "||";
208
				$isfirst++;
209
			}
210
		}
211
	}
212

    
213
	if (!$input_errors) {
214
		$alias['address'] = $address;
215
		$alias['descr'] = mb_convert_encoding($_POST['descr'],"HTML-ENTITIES","auto");
216
		$alias['type'] = $_POST['type'];
217
		$alias['detail'] = $final_address_details;
218

    
219
		if (isset($id) && $a_aliases[$id])
220
			$a_aliases[$id] = $alias;
221
		else
222
			$a_aliases[] = $alias;
223

    
224
		touch($d_aliasesdirty_path);
225

    
226
		write_config();
227
		filter_configure();
228

    
229
		header("Location: firewall_aliases.php");
230
		exit;		
231
	}
232
	//we received input errors, copy data to prevent retype
233
	else
234
	{
235
		$pconfig['descr'] = mb_convert_encoding($_POST['descr'],"HTML-ENTITIES","auto");
236
		$pconfig['address'] = $address;
237
		$pconfig['type'] = $_POST['type'];
238
		$pconfig['detail'] = $final_address_details;
239
	}
240
}
241

    
242
include("head.inc");
243

    
244
$jscriptstr = <<<EOD
245

    
246
<script type="text/javascript">
247
function typesel_change() {
248
	switch (document.iform.type.selectedIndex) {
249
		case 0:	/* host */
250
			var cmd;
251

    
252
			newrows = totalrows;
253
			for(i=0; i<newrows; i++) {
254
				comd = 'document.iform.address_subnet' + i + '.disabled = 1;';
255
				eval(comd);
256
				comd = 'document.iform.address_subnet' + i + '.value = "";';
257
				eval(comd);
258
			}
259
			break;
260
		case 1:	/* network */
261
			var cmd;
262

    
263
			newrows = totalrows;
264
			for(i=0; i<newrows; i++) {
265
				comd = 'document.iform.address_subnet' + i + '.disabled = 0;';
266
				eval(comd);
267
			}
268
			break;
269
		case 2:	/* port */
270
			var cmd;
271

    
272
			newrows = totalrows;
273
			for(i=0; i<newrows; i++) {
274
				comd = 'document.iform.address_subnet' + i + '.disabled = 1;';
275
				eval(comd);
276
				comd = 'document.iform.address_subnet' + i + '.value = "32";';
277
				eval(comd);
278
			}
279
			break;
280
		case 3:	/* OpenVPN Users */
281
			var cmd;
282

    
283
			newrows = totalrows;
284
			for(i=0; i<newrows; i++) {
285
				comd = 'document.iform.address_subnet' + i + '.disabled = 1;';
286
				eval(comd);
287
				comd = 'document.iform.address_subnet' + i + '.value = "";';
288
				eval(comd);
289
			}
290
			break;
291

    
292
		case 4:	/* url */
293
			var cmd;
294
			newrows = totalrows;
295
			for(i=0; i<newrows; i++) {
296
				comd = 'document.iform.address_subnet' + i + '.disabled = 0;';
297
				eval(comd);
298
			}
299
			break;
300
	}
301
}
302

    
303
EOD;
304

    
305
$network_str = gettext("Network");
306
$networks_str = gettext("Network(s)");
307
$cidr_str = gettext("CIDR");
308
$description_str = gettext("Description");
309
$hosts_str = gettext("Host(s)");
310
$ip_str = gettext("IP");
311
$ports_str = gettext("Port(s)");
312
$port_str = gettext("Port");
313
$url_str = gettext("URL");
314
$update_freq_str = gettext("Update Freq.");
315

    
316
$networks_help = gettext("Networks are specified in CIDR format.  Select the CIDR mask that pertains to each entry. /32 specifies a single host, /24 specifies 255.255.255.0, etc. Hostnames (FQDNs) may also be specified, using a /32 mask.");
317
$hosts_help = gettext("Enter as many hosts as you would like.  Hosts must be specified by their IP address.");
318
$ports_help = gettext("Enter as many ports as you wish.  Port ranges can be expressed by seperating with a colon.");
319
$url_help = gettext("Enter as many urls as you wish.  Also set the time that you would like the url refreshed in days.  After saving {$g['product_name']} will download the URL and import the items into the alias.");
320

    
321
$openvpn_str = gettext("Username");
322
$openvpn_user_str = gettext("OpenVPN Users");
323
$openvpn_help = gettext("Enter as many usernames as you wish.");
324
$openvpn_freq = gettext("");
325

    
326
$jscriptstr .= <<<EOD
327

    
328
function update_box_type() {
329
	var indexNum = document.forms[0].type.selectedIndex;
330
	var selected = document.forms[0].type.options[indexNum].text;
331
	if(selected == '{$networks_str}') {
332
		document.getElementById ("addressnetworkport").firstChild.data = "{$networks_str}";
333
		document.getElementById ("onecolumn").firstChild.data = "{$network_str}";
334
		document.getElementById ("twocolumn").firstChild.data = "{$cidr_str}";
335
		document.getElementById ("threecolumn").firstChild.data = "{$description_str}";
336
		document.getElementById ("itemhelp").firstChild.data = "{$networks_help}";
337
	} else if(selected == '{$hosts_str}') {
338
		document.getElementById ("addressnetworkport").firstChild.data = "{$hosts_str}";
339
		document.getElementById ("onecolumn").firstChild.data = "{$ip_str}";
340
		document.getElementById ("twocolumn").firstChild.data = "";
341
		document.getElementById ("threecolumn").firstChild.data = "{$description_str}";
342
		document.getElementById ("itemhelp").firstChild.data = "{$hosts_help}";
343
	} else if(selected == '{$ports_str}') {
344
		document.getElementById ("addressnetworkport").firstChild.data = "{$ports_str}";
345
		document.getElementById ("onecolumn").firstChild.data = "{$port_str}";
346
		document.getElementById ("twocolumn").firstChild.data = "";
347
		document.getElementById ("threecolumn").firstChild.data = "{$description_str}";
348
		document.getElementById ("itemhelp").firstChild.data = "{$ports_help}";
349
	} else if(selected == '{$url_str}') {
350
		document.getElementById ("addressnetworkport").firstChild.data = "{$url_str}";
351
		document.getElementById ("onecolumn").firstChild.data = "{$url_str}";
352
		document.getElementById ("twocolumn").firstChild.data = "{$update_freq_str}";
353
		document.getElementById ("threecolumn").firstChild.data = "{$description_str}";
354
		document.getElementById ("itemhelp").firstChild.data = "{$url_help}";
355
	} else if(selected == '{$openvpn_user_str}') {
356
		document.getElementById ("addressnetworkport").firstChild.data = "{$openvpn_user_str}";
357
		document.getElementById ("onecolumn").firstChild.data = "{$openvpn_str}";
358
		document.getElementById ("twocolumn").firstChild.data = "{$openvpn_freq}";
359
		document.getElementById ("threecolumn").firstChild.data = "{$description_str}";
360
		document.getElementById ("itemhelp").firstChild.data = "{$openvpn_help}";
361
	}
362
}
363
</script>
364

    
365
EOD;
366

    
367
?>
368

    
369
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onload="<?= $jsevents["body"]["onload"] ?>">
370
<?php
371
	include("fbegin.inc");
372
	echo $jscriptstr;
373
?>
374

    
375
<script type="text/javascript" src="/javascript/row_helper.js">
376
</script>
377

    
378
<input type='hidden' name='address_type' value='textbox' />
379
<input type='hidden' name='address_subnet_type' value='select' />
380

    
381
<script type="text/javascript">
382
	rowname[0] = "address";
383
	rowtype[0] = "textbox";
384
	rowsize[0] = "30";
385

    
386
	rowname[1] = "address_subnet";
387
	rowtype[1] = "select";
388
	rowsize[1] = "1";
389

    
390
	rowname[2] = "detail";
391
	rowtype[2] = "textbox";
392
	rowsize[2] = "50";
393
</script>
394

    
395
<?php if ($input_errors) print_input_errors($input_errors); ?>
396
<div id="inputerrors"></div>
397

    
398
<form action="firewall_aliases_edit.php" method="post" name="iform" id="iform">
399
<table width="100%" border="0" cellpadding="6" cellspacing="0">
400
  <tr>
401
	<td colspan="2" valign="top" class="listtopic">Alias Edit</td>
402
  </tr>
403
<?php if(is_alias_inuse($pconfig['name']) == true): ?>
404
  <tr>
405
    <td valign="top" class="vncellreq">Name</td>
406
    <td class="vtable"> <input name="name" type="hidden" id="name" size="40" value="<?=htmlspecialchars($pconfig['name']);?>" />
407
		  <?php echo $pconfig['name']; ?>
408
      <p>
409
        <span class="vexpl">NOTE: This alias is in use so the name may not be modified!</span>
410
      </p>
411
    </td>
412
  </tr>
413
<?php else: ?>
414
  <tr>
415
    <td valign="top" class="vncellreq">Name</td>
416
    <td class="vtable">
417
      <input name="name" type="text" id="name" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['name']);?>" />
418
      <br />
419
      <span class="vexpl">
420
        The name of the alias may only consist of the characters a-z, A-Z and 0-9.
421
      </span>
422
    </td>
423
  </tr>
424
<?php endif; ?>
425
  <tr>
426
    <td width="22%" valign="top" class="vncell">Description</td>
427
    <td width="78%" class="vtable">
428
      <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=$pconfig['descr'];?>" />
429
      <br />
430
      <span class="vexpl">
431
        You may enter a description here for your reference (not parsed).
432
      </span>
433
    </td>
434
  </tr>
435
  <tr>
436
    <td valign="top" class="vncellreq">Type</td>
437
    <td class="vtable">
438
      <select name="type" class="formselect" id="type" onchange="update_box_type(); typesel_change();">
439
        <option value="host" <?php if ($pconfig['type'] == "host") echo "selected"; ?>>Host(s)</option>
440
        <option value="network" <?php if ($pconfig['type'] == "network") echo "selected"; ?>>Network(s)</option>
441
        <option value="port" <?php if ($pconfig['type'] == "port") echo "selected"; ?>>Port(s)</option>
442
        <option value="openvpn" <?php if ($pconfig['type'] == "openvpn") echo "selected"; ?>>OpenVPN Users</option>
443
      </select>
444
    </td>
445
  </tr>
446
  <tr>
447
    <td width="22%" valign="top" class="vncellreq"><div id="addressnetworkport">Host(s)</div></td>
448
    <td width="78%" class="vtable">
449
      <table id="maintable">
450
        <tbody>
451
          <tr>
452
            <td colspan="4">
453
      		    <div style="padding:5px; margin-top: 16px; margin-bottom: 16px; border:1px dashed #000066; background-color: #ffffff; color: #000000; font-size: 8pt;" id="itemhelp">Item information</div>
454
            </td>
455
          </tr>
456
          <tr>
457
            <td><div id="onecolumn">Network</div></td>
458
            <td><div id="twocolumn">CIDR</div></td>
459
           <td><div id="threecolumn">Description</div></td>
460
          </tr>
461

    
462
	<?php
463
	$counter = 0;
464
	$address = $pconfig['address'];
465
	if ($address <> "") {
466
		$item = explode(" ", $address);
467
		$item3 = explode("||", $pconfig['detail']);
468
		foreach($item as $ww) {
469
			$address = $item[$counter];
470
			$address_subnet = "";
471
			$item2 = explode("/", $address);
472
			foreach($item2 as $current) {
473
				if($item2[1] <> "") {
474
					$address = $item2[0];
475
					$address_subnet = $item2[1];
476
				}
477
			}
478
			$item4 = $item3[$counter];
479
			$tracker = $counter;
480
	?>
481
          <tr>
482
            <td>
483
              <input name="address<?php echo $tracker; ?>" type="text" class="formfld unknown" id="address<?php echo $tracker; ?>" size="30" value="<?=htmlspecialchars($address);?>" />
484
            </td>
485
            <td>
486
			        <select name="address_subnet<?php echo $tracker; ?>" class="formselect" id="address_subnet<?php echo $tracker; ?>">
487
			          <option></option>
488
			          <?php for ($i = 32; $i >= 1; $i--): ?>
489
			          <option value="<?=$i;?>" <?php if ($i == $address_subnet) echo "selected"; ?>><?=$i;?></option>
490
			          <?php endfor; ?>
491
			        </select>
492
			      </td>
493
            <td>
494
              <input name="detail<?php echo $tracker; ?>" type="text" class="formfld unknown" id="detail<?php echo $tracker; ?>" size="50" value="<?=$item4;?>" />
495
            </td>
496
            <td>
497
    		<input type="image" src="/themes/<?echo $g['theme'];?>/images/icons/icon_x.gif" onclick="removeRow(this); return false;" value="Delete" />
498
	      </td>
499
          </tr>
500
<?php
501
        	$counter++;
502

    
503
       		} // end foreach
504
	} // end if
505
?>
506
        </tbody>
507
        <tfoot>
508

    
509
        </tfoot>
510
		  </table>
511
			<a onclick="javascript:addRowTo('maintable'); typesel_change(); return false;" href="#">
512
        <img border="0" src="/themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" alt="" title="add another entry" />
513
      </a>
514
		</td>
515
  </tr>
516
  <tr>
517
    <td width="22%" valign="top">&nbsp;</td>
518
    <td width="78%">
519
      <input id="submit" name="submit" type="submit" class="formbtn" value="Save" />
520
      <a href="firewall_aliases.php"><input id="cancelbutton" name="cancelbutton" type="button" class="formbtn" value="Cancel" /></a>
521
      <?php if (isset($id) && $a_aliases[$id]): ?>
522
      <input name="id" type="hidden" value="<?=$id;?>" />
523
      <?php endif; ?>
524
    </td>
525
  </tr>
526
</table>
527
</form>
528

    
529
<script type="text/javascript">
530
	field_counter_js = 3;
531
	rows = 1;
532
	totalrows = <?php echo $counter; ?>;
533
	loaded = <?php echo $counter; ?>;
534
	typesel_change();
535
	update_box_type();
536
</script>
537

    
538
<?php include("fend.inc"); ?>
539
</body>
540
</html>
541

    
542
<?php
543
function process_alias_tgz($temp_filename) {
544
	mwexec("/bin/mv {$temp_filename}/aliases {$temp_filename}/aliases.tgz");
545
	mwexec("/usr/bin/tar xzf {$temp_filename}/aliases.tgz -C {$temp_filename}/aliases/");
546
	unlink("{$temp_filename}/aliases.tgz");
547
	$files_to_process = return_dir_as_array("{$temp_filename}/");
548
	/* foreach through all extracted files and build up aliases file */
549
	$fd = fopen("{$temp_filename}/aliases", "a");
550
	foreach($files_to_process as $f2p) {
551
		$file_contents = file_get_contents($f2p);
552
		fwrite($fd, $file_contents);
553
		unlink($f2p);
554
	}
555
	fclose($fd);
556
}
557
?>
(41-41/207)