tbrehm
2008-08-03 e08297876e554e2a1b48bca50b44c26e7d755d7e
Network Configuration through the ISPConfig interface for debian and compatible linux distributions.
5 files modified
2 files added
211 ■■■■■ changed files
install/tpl/server.ini.master 1 ●●●● patch | view | raw | blame | history
interface/web/admin/form/server.tform.php 2 ●●● patch | view | raw | blame | history
interface/web/admin/form/server_config.tform.php 6 ●●●●● patch | view | raw | blame | history
interface/web/admin/lib/lang/en_server_config.lng 1 ●●●● patch | view | raw | blame | history
interface/web/admin/templates/server_config_server_edit.htm 8 ●●●● patch | view | raw | blame | history
server/conf/debian_network_interfaces.master 27 ●●●●● patch | view | raw | blame | history
server/plugins-available/network_settings_plugin.inc.php 166 ●●●●● patch | view | raw | blame | history
install/tpl/server.ini.master
@@ -4,6 +4,7 @@
dnsserver=mydns
[server]
auto_network_configuration=n
ip_address=192.168.0.105
netmask=255.255.255.0
gateway=192.168.0.1
interface/web/admin/form/server.tform.php
@@ -39,7 +39,7 @@
$form["action"]            = "server_edit.php";
$form["db_table"]        = "server";
$form["db_table_idx"]    = "server_id";
$form["db_history"]        = "no";
$form["db_history"]        = "yes";
$form["tab_default"]    = "services";
$form["list_default"]    = "server_list.php";
$form["auth"]            = 'yes';
interface/web/admin/form/server_config.tform.php
@@ -58,6 +58,12 @@
    ##################################
    # Begin Datatable fields
    ##################################
        'auto_network_configuration' => array (
            'datatype'    => 'VARCHAR',
            'formtype'    => 'CHECKBOX',
            'default'    => 'n',
            'value'        => array(0 => 'n',1 => 'y')
        ),
        'ip_address' => array (
            'datatype'    => 'VARCHAR',
            'formtype'    => 'TEXT',
interface/web/admin/lib/lang/en_server_config.lng
@@ -33,4 +33,5 @@
$wb["gateway_txt"] = 'Gateway';
$wb["hostname_txt"] = 'Hostname';
$wb["nameservers_txt"] = 'Nameservers';
$wb["auto_network_configuration_txt"] = 'Network Configuration';
?>
interface/web/admin/templates/server_config_server_edit.htm
@@ -1,5 +1,9 @@
<table width="500" border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td width="126" class="frmText11">{tmpl_var name='auto_network_configuration_txt'}:</td>
    <td width="366" class="frmText11">{tmpl_var name='auto_network_configuration'}</td>
  </tr>
  <tr>
    <td class="frmText11">{tmpl_var name='ip_address_txt'}:</td>
    <td class="frmText11"><input name="ip_address" type="text" class="text" value="{tmpl_var name='ip_address'}" size="15" maxlength="255"></td>
  </tr>
@@ -27,6 +31,6 @@
    <td><input name="btn_save" type="button" class="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','admin/server_config_edit.php');"><div class="buttonEnding"></div>&nbsp;
      <input name="btn_cancel" type="button" class="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('admin/server_config_list.php');"><div class="buttonEnding"></div>
    </td>
  </tr>
</table>
  </tr>
</table>
<input type="hidden" name="id" value="{tmpl_var name='id'}">
server/conf/debian_network_interfaces.master
New file
@@ -0,0 +1,27 @@
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
        address <tmpl_var name="ip_address">
        netmask <tmpl_var name="netmask">
        network <tmpl_var name="network">
        broadcast <tmpl_var name="broadcast">
        gateway <tmpl_var name="gateway">
<tmpl_if name="additionl_ip_records">
<tmpl_loop name="interfaces">
auto eth0:<tmpl_var name="id">
iface eth0:<tmpl_var name="id"> inet static
        address <tmpl_var name="ip_address">
        netmask <tmpl_var name="netmask">
        network <tmpl_var name="network">
        broadcast <tmpl_var name="broadcast">
        gateway <tmpl_var name="gateway">
</tmpl_loop>
</tmpl_if>
server/plugins-available/network_settings_plugin.inc.php
New file
@@ -0,0 +1,166 @@
<?php
/*
Copyright (c) 2008, Till Brehm, projektfarm Gmbh
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * 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.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
*/
class network_settings_plugin {
    var $plugin_name = 'network_settings_plugin';
    var $class_name = 'network_settings_plugin';
    /*
         This function is called when the plugin is loaded
    */
    function onLoad() {
        global $app;
        /*
        Register for the events
        */
        $app->plugins->registerEvent('server_insert','network_settings_plugin','insert');
        $app->plugins->registerEvent('server_update','network_settings_plugin','update');
        $app->plugins->registerEvent('server_ip_insert','network_settings_plugin','insert');
        $app->plugins->registerEvent('server_ip_update','network_settings_plugin','update');
    }
    function insert($event_name,$data) {
        global $app, $conf;
        $this->update($event_name,$data);
    }
    // The purpose of this plugin is to rewrite the main.cf file
    function update($event_name,$data) {
        global $app, $conf;
        // get the config
        $app->uses("getconf");
        $server_config = $app->getconf->get_server_config($conf["server_id"], 'server');
        // Configure the debian network card settings
        if(is_file('/etc/debian_version') && $server_config['auto_network_configuration'] == 'y') {
            copy('/etc/network/interfaces','/etc/network/interfaces~');
            $app->load('tpl');
            $network_tpl = new tpl();
            $network_tpl->newTemplate("debian_network_interfaces.master");
            $network_tpl->setVar('ip_address',$server_config["ip_address"]);
            $network_tpl->setVar('netmask',$server_config["netmask"]);
            $network_tpl->setVar('gateway',$server_config["gateway"]);
            $network_tpl->setVar('broadcast',$this->broadcast($server_config["ip_address"],$server_config["netmask"]));
            $network_tpl->setVar('network',$this->network($server_config["ip_address"],$server_config["netmask"]));
            $records = $app->db->queryAllRecords("SELECT ip_address FROM server_ip WHERE server_id = ".intval($conf["server_id"]));
            $ip_records = array();
            $additionl_ip_records = 0;
            $n = 0;
            if(is_array($records)) {
                foreach($records as $rec) {
                    $ip_records[] = array(
                        'id' => $n,
                        'ip_address' => $rec['ip_address'],
                        'netmask' => $server_config["netmask"],
                        'gateway' => $server_config["gateway"],
                        'broadcast' => $this->broadcast($rec['ip_address'],$server_config["netmask"]),
                        'network' => $this->network($rec['ip_address'],$server_config["netmask"])
                    );
                    $additionl_ip_records = 1;
                    $n++;
                }
            }
            $network_tpl->setVar('additionl_ip_records',$additionl_ip_records);
            $network_tpl->setLoop('interfaces',$ip_records);
            file_put_contents('/etc/network/interfaces',$network_tpl->grab());
            unset($network_tpl);
            $app->log("Changed Network settings",LOGLEVEL_DEBUG);
            exec('/etc/init.d/networking force-reload');
        }
    }
    function network($ip, $netmask){
        $netmask = $this->netmask($netmask);
        list($f1,$f2,$f3,$f4) = explode(".", $netmask);
        $netmask_bin = str_pad(decbin($f1),8,"0",STR_PAD_LEFT).str_pad(decbin($f2),8,"0",STR_PAD_LEFT).str_pad(decbin($f3),8,"0",STR_PAD_LEFT).str_pad(decbin($f4),8,"0",STR_PAD_LEFT);
        list($f1,$f2,$f3,$f4) = explode(".", $ip);
        $ip_bin = str_pad(decbin($f1),8,"0",STR_PAD_LEFT).str_pad(decbin($f2),8,"0",STR_PAD_LEFT).str_pad(decbin($f3),8,"0",STR_PAD_LEFT).str_pad(decbin($f4),8,"0",STR_PAD_LEFT);
        for($i=0;$i<32;$i++){
            $network_bin .= substr($netmask_bin,$i,1) * substr($ip_bin,$i,1);
        }
        $network_bin = wordwrap($network_bin, 8, ".", 1);
        list($f1,$f2,$f3,$f4) = explode(".", trim($network_bin));
        return bindec($f1).".".bindec($f2).".".bindec($f3).".".bindec($f4);
    }
    function broadcast($ip, $netmask){
        $netmask = $this->netmask($netmask);
        $binary_netmask = $this->binary_netmask($netmask);
        list($f1,$f2,$f3,$f4) = explode(".", $ip);
        $ip_bin = str_pad(decbin($f1),8,"0",STR_PAD_LEFT).str_pad(decbin($f2),8,"0",STR_PAD_LEFT).str_pad(decbin($f3),8,"0",STR_PAD_LEFT).str_pad(decbin($f4),8,"0",STR_PAD_LEFT);
        $broadcast_bin = str_pad(substr($ip_bin, 0, $binary_netmask),32,"1",STR_PAD_RIGHT);
        $broadcast_bin = wordwrap($broadcast_bin, 8, ".", 1);
        list($f1,$f2,$f3,$f4) = explode(".", trim($broadcast_bin));
        return bindec($f1).".".bindec($f2).".".bindec($f3).".".bindec($f4);
    }
    function netmask($netmask){
        list($f1,$f2,$f3,$f4) = explode(".", trim($netmask));
        $bin = str_pad(decbin($f1),8,"0",STR_PAD_LEFT).str_pad(decbin($f2),8,"0",STR_PAD_LEFT).str_pad(decbin($f3),8,"0",STR_PAD_LEFT).str_pad(decbin($f4),8,"0",STR_PAD_LEFT);
        $parts = explode("0", $bin);
        $bin = str_pad($parts[0], 32, "0", STR_PAD_RIGHT);
        $bin = wordwrap($bin, 8, ".", 1);
        list($f1,$f2,$f3,$f4) = explode(".", trim($bin));
        return bindec($f1).".".bindec($f2).".".bindec($f3).".".bindec($f4);
    }
    function binary_netmask($netmask){
        list($f1,$f2,$f3,$f4) = explode(".", trim($netmask));
        $bin = str_pad(decbin($f1),8,"0",STR_PAD_LEFT).str_pad(decbin($f2),8,"0",STR_PAD_LEFT).str_pad(decbin($f3),8,"0",STR_PAD_LEFT).str_pad(decbin($f4),8,"0",STR_PAD_LEFT);
        $parts = explode("0", $bin);
        return substr_count($parts[0], "1");
    }
} // end class
?>