[ previous ] [ next ] [ threads ]
 
 From:  Jim McBeath <jimmc at macrovision dot com>
 To:  m0n0wall at lists dot m0n0 dot ch
 Subject:  PATCH: WebGUI for assigning interface ports
 Date:  Thu, 27 Nov 2003 15:27:15 -0800
This patch adds a web page to m0n0 to allow specifying which physical
interface ports are used for lan, wan, and opt.  This is the same
functionality as is currently available from the console menu, but
done as part of the web interface.  To implement the patch, put the
file interfaces_assign.php into the /usr/local/www directory in mfsroot,
and modify the file fbegin.inc in that directory to include a pointer
to that new file, as indicated in the short diff below.  This was done
against pb20r550.

--
Jim McBeath


diff -aur mfsroot.orig/usr/local/www/fbegin.inc mfsroot/usr/local/www/fbegin.inc
--- mfsroot.orig/usr/local/www/fbegin.inc	Sat Nov 22 08:34:47 2003
+++ mfsroot/usr/local/www/fbegin.inc	Wed Nov 26 19:25:23 2003
@@ -15,6 +15,7 @@
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="system_routes.php" class="navlnk">Static 
               routes</a><br>
               <strong>Interfaces</strong><br>
+              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="interfaces_assign.php" class="navlnk">Assign
Ports</a><br>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="interfaces_lan.php" class="navlnk">LAN</a><br>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="interfaces_wan.php" class="navlnk">WAN</a><br>
 			  <?php for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++): ?>


-----------  /usr/local/www/interfaces_assign.php  -------------------------
#!/usr/local/bin/php
<?php 
/*
	interfaces_assign.php
	part of m0n0wall (http://m0n0.ch/wall)
	
	Copyright (C) 2003 Manuel Kasper <mk at neon1 dot net>.
	All rights reserved.
	
	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:
	
	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.
	
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/
//Written by Jim McBeath based on existing m0n0wall files

require("guiconfig.inc");

//In this file, "port" refers to the physical port name,
//while "interface" refers to LAN, WAN, or OPTn.

$portlist = get_interface_list();

if ($_POST) {

    unset($input_errors);

    /* input validation */

    //Build a list of the port names so we can see how the interfaces map
    $portifmap = array();
    foreach ($portlist as $portname => $portaddr) {
	    $portifmap[] = array($portname => array());
    }

    //Go through the list of ports selected by the user,
    //build a list of port-to-interface mappings in portifmap
    foreach ($_POST as $ifname => $ifport) {
	if ($ifname=='lan' || $ifname=='wan' || substr($ifname,0,3)=='opt'){
	    $portifmap[$ifport][] = strtoupper($ifname);
	}
    }

    //Deliver error message for any port with more than one assignment
    foreach ($portifmap as $portname => $ifnames) {
	if (count($ifnames)>1) {
	    $errstr = "Port " . $portname .
			" was assigned to " . count($ifnames) .
			" interfaces:";
	    foreach ($portifmap[$portname] as $ifn) {
		$errstr .= "  " . $ifn;
	    }
	    $input_errors[] = $errstr;
	}
    }


    if (!$input_errors) {
	//No errors detected, so update the config
	foreach ($_POST as $ifname => $ifport) {
	  if ($ifname=='lan' || $ifname=='wan' || substr($ifname,0,3)=='opt'){
	   if (!is_array($ifport)) {
	    $config['interfaces'][$ifname]['if'] = $ifport;
	    //check for wireless interfaces, set or clear ['wireless']
	    if (preg_match("/^(wi|awi|an)/", $ifport)) {
		if (!is_array($config['interfaces'][$ifname]['wireless'])) {
		    $config['interfaces'][$ifname]['wireless'] = array();
		}
	    } else {
		unset($config['interfaces'][$ifname]['wireless']);
	    }
	    //make sure there is a name for OPTn
	    if (substr($ifname,0,3)=='opt') {
		if (!isset($config['interfaces'][$ifname]['descr'])) {
		    $config['interfaces'][$ifname]['descr'] = $ifname;
		}
	    }
	   }
	  }
	}

	write_config();
	touch($d_sysrebootreqd_path);
	//message is set up below based on existance of bootreqd file
    }
}

if ($_GET['act'] == "del") {
    $id = $_GET['id'];
    unset($config['interfaces'][$id]);	//delete the specified OPTn

    //shift down other OPTn interfaces to get rid of holes
    $i = substr($id,3);	//the number of the OPTn port being deleted
    $i++;			//look at the following OPTn ports
    while (is_array($config['interfaces']['opt' . $i])) {
	$config['interfaces']['opt' . ($i - 1)] =
	    $config['interfaces']['opt' . $i];
	if ($config['interfaces']['opt' . ($i - 1)]['descr'] == "OPT" . $i) {
	    $config['interfaces']['opt' . ($i - 1)]['descr'] = "OPT" . ($i - 1);
	}
	unset($config['interfaces']['opt' . $i]);
	$i++;
    }

    write_config();
    touch($d_sysrebootreqd_path);
    header("Location: interfaces_assign.php");
    exit;
}

if ($_GET['act'] == "add") {
    $i = 0;
    while (1) {
	$newifname = 'opt' . ($i+1);
	if (!is_array($config['interfaces'][$newifname])) {
	    $config['interfaces'][$newifname] = array();
	    $config['interfaces'][$newifname]['descr'] = "OPT" . ($i+1);
	    //Find an unused port for this interface
	    foreach ($portlist as $portname => $portaddr) {
		$portused = false;
		foreach ($config['interfaces'] as $ifname => $ifdata) {
		    if ($ifdata['if']==$portname) {
			$portused = true;
			break;
		    }
		}
		if (!$portused) {
		    $config['interfaces'][$newifname]['if'] = $portname;
		    if (preg_match("/^(wi|awi|an)/", $portname)) {
			$config['interfaces'][$newifname]['wireless'] = array();
		    }
		    break;
		}
	    }
	    break;
	}
	$i++;
    }
    write_config();
    touch($d_sysrebootreqd_path);
    header("Location: interfaces_assign.php");
    exit;
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>m0n0wall webGUI - Interfaces: Assign Ports</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="gui.css" rel="stylesheet" type="text/css">
</head>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<p class="pgtitle">Interfaces: Assign Ports</p>
<?php if ($input_errors) print_input_errors($input_errors); ?>
<?php if ($savemsg) print_info_box($savemsg); ?>
<?php if (file_exists($d_sysrebootreqd_path)): ?><p>
<?php print_info_box_np("Changes will take effect when you
<a href=\"reboot.php\">reboot</a> the firewall.");?><br>
<?php endif; ?>
            <form action="interfaces_assign.php" method="post" name="iform" id="iform">
              <table border="0" cellpadding="0" cellspacing="0">
<?php foreach ($config['interfaces'] as $ifname => $iface): ?>
                <tr> 
                  <td valign="middle"><strong><?=strtoupper($ifname);?></strong></td>
                  <td> <select name="<?=$ifname;?>" class="formfld" id="<?=$ifname;?>">
		      <?php foreach ($portlist as $portname => $portaddr): ?>
                      <option value="<?=$portname;?>" <?php if ($portname == $iface['if']) echo
"selected";?>> 
                      <?=htmlspecialchars($portname . " = " . $portaddr);?>
                      </option>
                      <?php endforeach; ?>
                    </select></td>
                  <td>
                      <?php if ($ifname != 'lan' && $ifname != 'wan'): ?> 
		    <a href="interfaces_assign.php?act=del&id=<?=$ifname;?>"><img src="x.gif" alt="delete
interface" width="17" height="17" border"0"></a>
                      <?php endif; ?> 
		  </td>
                </tr>
<?php endforeach; ?>
		<tr>
		  <td colspan=2></td>
                  <td>
                      <?php if (count($config['interfaces']) < count($portlist)): ?> 
		    <a href="interfaces_assign.php?act=add"><img src="plus.gif" alt="add interface" width="17"
height="17" border"0"></a>
                      <?php endif; ?> 
		  </td>
		</tr>
                <tr> 
                  <td valign="top">&nbsp;</td>
                  <td> <input name="Submit" type="submit" class="formbtn" value="Save"> 
                  </td>
                </tr>
                <tr> 
                  <td valign="top">&nbsp;</td>
                  <td colspan=3>
		    <span class="vexpl"><span class="red"><strong>Warning:<br>
                    </strong></span>After you click &quot;Save&quot;, you must 
                    reboot the firewall to make the changes take effect.<br>
                    You may also have to do one or more of the following steps 
                    before you can<br>
                    access your firewall again: 
                    <ul>
                      <li>change the IP address of your computer</li>
                      <li>renew it's DHCP lease</li>
                      <li>access the webGUI with the new IP address</li>
                    </ul>
                    </span></td>
                </tr>
              </table>
</form>
<?php include("fend.inc"); ?>
</body>
</html>