Quoting Scott Ullrich <[EMAIL PROTECTED]>:

> On 7/24/05, Dmitry Sorokin <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > I'd like to take a chance first and say big thank you to all developers and
> > supporters of this great project! Grate job! I wish I can help the project
> and
> > contribute some code or support one of the packages, but unfortunately I'm
> so
> > busy at work :-(
> >
> > There is one feature I'm missing from m0n0, which was implemented in 1.2b6
> > (03/01/2005):
> >  - static mappings can now be added by clicking a button on the DHCP leases
> page
> > Is it hard to port from m0n0? Can you include it in next release?
> 
> File a feature request ticket at
> http://cvstrac.pfsense.com/captcha?nxp=/tktnew
> 
This feature doesn't work properly. After clicking "+" on DHCP Leases page, 
instead of taking you to services_dhcp_edit.php it takes you to 
services_dhcp.php.
I looked into diag_dhcp_leases.php and it seems that $data['if'] is not 
available there, so on the diag_dhcp_leases.php the link for the "+" is not 
complete ("if" variable is missing): <a href="services_dhcp_edit.php?
if=&mac=xx:xx:xx:xx:xx:xx">
Is it possible to fix that?

Also I'd like to add &ipaddr={$data['ip']}&descr={$data['hostname']} to the 
services_dhcp_edit.php?if={$data['if']}&mac={$data['mac']} link so I don't have 
to enter hostname manually and can just slightly edit the ip address.
I'm attaching patched  diag_dhcp_leases.php and services_dhcp_edit.php to allow 
that. (but the bug with $data['if'] is not fixed still)
I'm using psSense-0.73.4

Hope you can include that in next release.

Best regards,
Dmitry


#!/usr/local/bin/php
<?php
/* $Id: diag_dhcp_leases.php,v 1.6 2005/07/25 17:39:49 sullrich Exp $ */
/*
        diag_dhcp_leases.php
        Copyright (C) 2004 Scott Ullrich
        All rights reserved.

        originially part of m0n0wall (http://m0n0.ch/wall)
        Copyright (C) 2003-2004 Manuel Kasper <[EMAIL PROTECTED]>.
        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.
*/

require("guiconfig.inc");

$pgtitle = "Diagnostics: DHCP leases";
include("head.inc");

?>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<p class="pgtitle"><?=$pgtitle?></p>
<?php

flush();

function leasecmp($a, $b) {
        return strcmp($a[$_GET['order']], $b[$_GET['order']]);
}

function adjust_gmt($dt) {
        $ts = strtotime($dt . " GMT");
        return strftime("%Y/%m/%d %H:%M:%S", $ts);
}

$fp = @fopen("{$g['vardb_path']}/dhcpd.leases","r");

if ($fp):

$return = array();

while ($line = fgets($fp)) {
        $matches = "";

        // Sort out comments
        // C-style comments not supported!
        if (preg_match("/^\s*[\r|\n]/", $line, $matches[0]) ||
                                preg_match("/^([^\"#]*)#.*$/", $line, 
$matches[1]) ||
                                preg_match("/^([^\"]*)\/\/.*$/", $line, 
$matches[2]) ||
                                preg_match("/\s*#(.*)/", $line, $matches[3]) ||
                                preg_match("/\\\"\176/", $line, $matches[4])
                ) {
                $line = "";
                continue;
        }

        if (preg_match("/(.*)#(.*)/", $line, $matches))
                $line = $matches[0];

        // Tokenize lines
        do {
                if (preg_match("/^\s*\"([^\"]*)\"(.*)$/", $line, $matches)) {
                        $line = $matches[2];
                        $return[] = array($matches[1], 0);
                } else if (preg_match("/^\s*([{};])(.*)$/", $line, $matches)) {
                        $line = $matches[2];
                        $return[] = array($matches[0], 1);
                } else if (preg_match("/^\s*([^{}; \t]+)(.*)$/", $line, 
$matches)) {
                        $line = $matches[2];
                        $return[] = array($matches[1], 0);
                } else
                        break;

        } while($line);

        $lines++;
}

fclose($fp);

$leases = array();
$i = 0;

// Put everything together again
while ($data = array_shift($return)) {
        if ($data[0] == "next") {
                $d = array_shift($return);
        }
        if ($data[0] == "lease") {
                $d = array_shift($return);
                $leases[$i]['ip'] = $d[0];
        }
        if ($data[0] == "client-hostname") {
                $d = array_shift($return);
                $leases[$i]['hostname'] = $d[0];
        }
        if ($data[0] == "hardware") {
                $d = array_shift($return);
                if ($d[0] == "ethernet") {
                        $d = array_shift($return);
                        $leases[$i]['mac'] = $d[0];
                }
        } else if ($data[0] == "starts") {
                $d = array_shift($return);
                $d = array_shift($return);
                $leases[$i]['start'] = $d[0];
                $d = array_shift($return);
                $leases[$i]['start'] .= " " . $d[0];
        } else if ($data[0] == "ends") {
                $d = array_shift($return);
                $d = array_shift($return);
                $leases[$i]['end'] = $d[0];
                $d = array_shift($return);
                $leases[$i]['end'] .= " " . $d[0];
        } else if ($data[0] == "binding") {
                $d = array_shift($return);
                if ($d[0] == "state") {
                        $d = array_shift($return);
                        $leases[$i]['act'] = $d[0];
                }
        } else if (($data[0] == "}") && ($data[1] == 1))                // End 
of group
                $i++;
}

if ($_GET['order'])
        usort($leases, "leasecmp");
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=ip">IP 
address</a></td>
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=mac">MAC 
address</a></td>
    <td class="listhdrr"><a 
href="?all=<?=$_GET['all'];?>&order=hostname">Hostname</a></td>
    <td class="listhdrr"><a 
href="?all=<?=$_GET['all'];?>&order=start">Start</a></td>
    <td class="listhdr"><a href="?all=<?=$_GET['all'];?>&order=end">End</a></td>
        </tr>
<?php
foreach ($leases as $data) {
        if (($data['act'] == "active") || ($_GET['all'] == 1)) {
                if ($data['act'] != "active") {
                        $fspans = "<span class=\"gray\">";
                        $fspane = "</span>";
                } else {
                        $fspans = $fspane = "";
                }
                echo "<tr>\n";
                echo "<td 
class=\"listlr\">{$fspans}{$data['ip']}{$fspane}&nbsp;</td>\n";
                echo "<td 
class=\"listr\">{$fspans}{$data['mac']}{$fspane}&nbsp;</td>\n";
                echo "<td 
class=\"listr\">{$fspans}{$data['hostname']}{$fspane}&nbsp;</td>\n";
                echo "<td class=\"listr\">{$fspans}" . 
adjust_gmt($data['start']) . "{$fspane}&nbsp;</td>\n";
                echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['end']) 
. "{$fspane}&nbsp;</td>\n";
                echo "<td class=\"list\" valign=\"middle\"><a 
href=\"services_dhcp_edit.php?if={$data['if']}&mac={$data['mac']}&ipaddr={$data['ip']}&descr={$data['hostname']}\"><img
 src=\"plus.gif\" width=\"17\" height=\"17\" border=\"0\" title=\"add a static 
mapping for this MAC address\"></a></td>\n";
                echo "</tr>\n";
        }
}
?>
</table>
<p>
<form action="diag_dhcp_leases.php" method="GET">
<input type="hidden" name="order" value="<?=$_GET['order'];?>">
<?php if ($_GET['all']): ?>
<input type="hidden" name="all" value="0">
<input type="submit" class="formbtn" value="Show active leases only">
<?php else: ?>
<input type="hidden" name="all" value="1">
<input type="submit" class="formbtn" value="Show active and expired leases">
<?php endif; ?>
</form>
<?php else: ?>
<p><strong>No leases file found. Is the DHCP server active?</strong></p>
<?php endif; ?>
<?php include("fend.inc"); ?>
</body>
</html>
#!/usr/local/bin/php
<?php 
/* $Id: services_dhcp_edit.php,v 1.11 2005/08/01 22:48:47 sullrich Exp $ */
/*
        services_dhcp_edit.php
        part of m0n0wall (http://m0n0.ch/wall)
        
        Copyright (C) 2003-2004 Manuel Kasper <[EMAIL PROTECTED]>.
        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.
*/

require("guiconfig.inc");

$if = $_GET['if'];
if ($_POST['if'])
        $if = $_POST['if'];
        
if (!$if) {
        header("Location: services_dhcp.php");
        exit;
}

if (!is_array($config['dhcpd'][$if]['staticmap'])) {
        $config['dhcpd'][$if]['staticmap'] = array();
}
staticmaps_sort($if);
$a_maps = &$config['dhcpd'][$if]['staticmap'];
$ifcfg = &$config['interfaces'][$if];

$id = $_GET['id'];
if (isset($_POST['id']))
        $id = $_POST['id'];

if (isset($id) && $a_maps[$id]) {
        $pconfig['mac'] = $a_maps[$id]['mac'];
        $pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
        $pconfig['descr'] = $a_maps[$id]['descr'];
} else {
        $pconfig['mac'] = $_GET['mac'];
        $pconfig['ipaddr'] = $_GET['ipaddr'];
        $pconfig['descr'] = $_GET['descr'];
}

if ($_POST) {

        unset($input_errors);
        $pconfig = $_POST;

        /* input validation */
        $reqdfields = explode(" ", "mac");
        $reqdfieldsn = explode(",", "MAC address");
        
        do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);

        /* normalize MAC addresses - lowercase and convert Windows-ized 
hyphenated MACs to colon delimited */
        $_POST['mac'] = strtolower(str_replace("-", ":", $_POST['mac']));
        
        if (($_POST['ipaddr'] && !is_ipaddr($_POST['ipaddr']))) {
                $input_errors[] = "A valid IP address must be specified.";
        }
        if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
                $input_errors[] = "A valid MAC address must be specified.";
        }

        /* check for overlaps */
        foreach ($a_maps as $mapent) {
                if (isset($id) && ($a_maps[$id]) && ($a_maps[$id] === $mapent))
                        continue;

                if (($mapent['mac'] == $_POST['mac']) || ($_POST['ipaddr'] && 
(ip2long($mapent['ipaddr']) == ip2long($_POST['ipaddr'])))) {
                        $input_errors[] = "This IP or MAC address already 
exists.";
                        break;
                }
        }
                
        /* make sure it's not within the dynamic subnet */
        if ($_POST['ipaddr']) {
                $dynsubnet_start = 
ip2long($config['dhcpd'][$if]['range']['from']);
                $dynsubnet_end = ip2long($config['dhcpd'][$if]['range']['to']);
                $lansubnet_start = (ip2long($ifcfg['ipaddr']) & 
gen_subnet_mask_long($ifcfg['subnet']));
                $lansubnet_end = (ip2long($ifcfg['ipaddr']) | 
(~gen_subnet_mask_long($ifcfg['subnet'])));
                
                if ((ip2long($_POST['ipaddr']) >= $dynsubnet_start) &&
                                (ip2long($_POST['ipaddr']) <= $dynsubnet_end)) {
                        $input_errors[] = "Static IP addresses may not lie 
within the dynamic client range.";
                }
                if ((ip2long($_POST['ipaddr']) < $lansubnet_start) ||
                        (ip2long($_POST['ipaddr']) > $lansubnet_end)) {
                        $input_errors[] = "The IP address must lie in the 
{$ifcfg['descr']} subnet.";
                }
        }

        if (!$input_errors) {
                $mapent = array();
                $mapent['mac'] = $_POST['mac'];
                $mapent['ipaddr'] = $_POST['ipaddr'];
                $mapent['descr'] = $_POST['descr'];

                if (isset($id) && $a_maps[$id])
                        $a_maps[$id] = $mapent;
                else
                        $a_maps[] = $mapent;
                
                touch($d_staticmapsdirty_path);
                
                write_config();
                
                header("Location: services_dhcp.php?if={$if}");
                exit;
        }
}

$pgtitle = "Services: DHCP: Edit static mapping";
include("head.inc");
?>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<p class="pgtitle"><?=$pgtitle?></p>
<?php if ($input_errors) print_input_errors($input_errors); ?>
            <form action="services_dhcp_edit.php" method="post" name="iform" 
id="iform">
              <?display_topbar()?>
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
                <tr> 
                  <td width="22%" valign="top" class="vncellreq">MAC 
address</td>
                  <td width="78%" class="vtable"> 
                    <input name="mac" type="text" class="formfld" id="mac" 
size="30" value="<?=htmlspecialchars($pconfig['mac']);?>">
                    <?php
                        $ip = getenv('REMOTE_ADDR');
                        $mac = `/usr/sbin/arp -an | grep {$ip} | cut -d" " -f4`;
                        $mac = str_replace("\n","",$mac);
                    ?>
                    <a OnClick="document.forms[0].mac.value='<?=$mac?>';" 
href="#">Copy my MAC address</a>                  
                    <br>
                    <span class="vexpl">Enter a MAC address in the following 
format: 
                    xx:xx:xx:xx:xx:xx</span></td>
                </tr>
                <tr> 
                  <td width="22%" valign="top" class="vncell">IP address</td>
                  <td width="78%" class="vtable"> 
                    <input name="ipaddr" type="text" class="formfld" 
id="ipaddr" size="20" value="<?=htmlspecialchars($pconfig['ipaddr']);?>">
                    <br>
                    If no IP address is given, one will be dynamically 
allocated  from the pool.</td>
                </tr>
                <tr> 
                  <td width="22%" valign="top" class="vncell">Description</td>
                  <td width="78%" class="vtable"> 
                    <input name="descr" type="text" class="formfld" id="descr" 
size="40" value="<?=htmlspecialchars($pconfig['descr']);?>"> 
                    <br> <span class="vexpl">You may enter a description here 
                    for your reference (not parsed).</span></td>
                </tr>
                <tr> 
                  <td width="22%" valign="top">&nbsp;</td>
                  <td width="78%"> 
                    <input name="Submit" type="submit" class="formbtn" 
value="Save"> <input class="formbtn" type="button" value="Cancel" 
onclick="history.back()">
                    <?php if (isset($id) && $a_maps[$id]): ?>
                    <input name="id" type="hidden" value="<?=$id;?>">
                    <?php endif; ?>
                    <input name="if" type="hidden" value="<?=$if;?>"> 
                  </td>
                </tr>
              </table>
</form>
<?php include("fend.inc"); ?>
</body>
</html>

Reply via email to