Skip to content

Commit e082978

Browse files
committed
Network Configuration through the ISPConfig interface for debian and compatible linux distributions.
1 parent 855c6a2 commit e082978

File tree

7 files changed

+208
-3
lines changed

7 files changed

+208
-3
lines changed

install/tpl/server.ini.master

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mailserver=postfix
44
dnsserver=mydns
55

66
[server]
7+
auto_network_configuration=n
78
ip_address=192.168.0.105
89
netmask=255.255.255.0
910
gateway=192.168.0.1

interface/web/admin/form/server.tform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
$form["action"] = "server_edit.php";
4040
$form["db_table"] = "server";
4141
$form["db_table_idx"] = "server_id";
42-
$form["db_history"] = "no";
42+
$form["db_history"] = "yes";
4343
$form["tab_default"] = "services";
4444
$form["list_default"] = "server_list.php";
4545
$form["auth"] = 'yes';

interface/web/admin/form/server_config.tform.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
##################################
5959
# Begin Datatable fields
6060
##################################
61+
'auto_network_configuration' => array (
62+
'datatype' => 'VARCHAR',
63+
'formtype' => 'CHECKBOX',
64+
'default' => 'n',
65+
'value' => array(0 => 'n',1 => 'y')
66+
),
6167
'ip_address' => array (
6268
'datatype' => 'VARCHAR',
6369
'formtype' => 'TEXT',

interface/web/admin/lib/lang/en_server_config.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ $wb["netmask_txt"] = 'Netmask';
3333
$wb["gateway_txt"] = 'Gateway';
3434
$wb["hostname_txt"] = 'Hostname';
3535
$wb["nameservers_txt"] = 'Nameservers';
36+
$wb["auto_network_configuration_txt"] = 'Network Configuration';
3637
?>

interface/web/admin/templates/server_config_server_edit.htm

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<table width="500" border="0" cellspacing="0" cellpadding="2">
2+
<tr>
3+
<td width="126" class="frmText11">{tmpl_var name='auto_network_configuration_txt'}:</td>
4+
<td width="366" class="frmText11">{tmpl_var name='auto_network_configuration'}</td>
5+
</tr>
26
<tr>
37
<td class="frmText11">{tmpl_var name='ip_address_txt'}:</td>
48
<td class="frmText11"><input name="ip_address" type="text" class="text" value="{tmpl_var name='ip_address'}" size="15" maxlength="255"></td>
@@ -27,6 +31,6 @@
2731
<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;
2832
<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>
2933
</td>
30-
</tr>
31-
</table>
34+
</tr>
35+
</table>
3236
<input type="hidden" name="id" value="{tmpl_var name='id'}">
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This file describes the network interfaces available on your system
2+
# and how to activate them. For more information, see interfaces(5).
3+
4+
# The loopback network interface
5+
auto lo
6+
iface lo inet loopback
7+
8+
# The primary network interface
9+
auto eth0
10+
iface eth0 inet static
11+
address <tmpl_var name="ip_address">
12+
netmask <tmpl_var name="netmask">
13+
network <tmpl_var name="network">
14+
broadcast <tmpl_var name="broadcast">
15+
gateway <tmpl_var name="gateway">
16+
17+
<tmpl_if name="additionl_ip_records">
18+
<tmpl_loop name="interfaces">
19+
auto eth0:<tmpl_var name="id">
20+
iface eth0:<tmpl_var name="id"> inet static
21+
address <tmpl_var name="ip_address">
22+
netmask <tmpl_var name="netmask">
23+
network <tmpl_var name="network">
24+
broadcast <tmpl_var name="broadcast">
25+
gateway <tmpl_var name="gateway">
26+
</tmpl_loop>
27+
</tmpl_if>
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2008, Till Brehm, projektfarm Gmbh
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
class network_settings_plugin {
32+
33+
var $plugin_name = 'network_settings_plugin';
34+
var $class_name = 'network_settings_plugin';
35+
36+
37+
/*
38+
This function is called when the plugin is loaded
39+
*/
40+
41+
function onLoad() {
42+
global $app;
43+
44+
/*
45+
Register for the events
46+
*/
47+
48+
$app->plugins->registerEvent('server_insert','network_settings_plugin','insert');
49+
$app->plugins->registerEvent('server_update','network_settings_plugin','update');
50+
51+
$app->plugins->registerEvent('server_ip_insert','network_settings_plugin','insert');
52+
$app->plugins->registerEvent('server_ip_update','network_settings_plugin','update');
53+
54+
55+
56+
}
57+
58+
function insert($event_name,$data) {
59+
global $app, $conf;
60+
61+
$this->update($event_name,$data);
62+
63+
}
64+
65+
// The purpose of this plugin is to rewrite the main.cf file
66+
function update($event_name,$data) {
67+
global $app, $conf;
68+
69+
// get the config
70+
$app->uses("getconf");
71+
$server_config = $app->getconf->get_server_config($conf["server_id"], 'server');
72+
73+
// Configure the debian network card settings
74+
if(is_file('/etc/debian_version') && $server_config['auto_network_configuration'] == 'y') {
75+
copy('/etc/network/interfaces','/etc/network/interfaces~');
76+
77+
$app->load('tpl');
78+
79+
$network_tpl = new tpl();
80+
$network_tpl->newTemplate("debian_network_interfaces.master");
81+
82+
$network_tpl->setVar('ip_address',$server_config["ip_address"]);
83+
$network_tpl->setVar('netmask',$server_config["netmask"]);
84+
$network_tpl->setVar('gateway',$server_config["gateway"]);
85+
$network_tpl->setVar('broadcast',$this->broadcast($server_config["ip_address"],$server_config["netmask"]));
86+
$network_tpl->setVar('network',$this->network($server_config["ip_address"],$server_config["netmask"]));
87+
88+
$records = $app->db->queryAllRecords("SELECT ip_address FROM server_ip WHERE server_id = ".intval($conf["server_id"]));
89+
$ip_records = array();
90+
$additionl_ip_records = 0;
91+
$n = 0;
92+
if(is_array($records)) {
93+
foreach($records as $rec) {
94+
$ip_records[] = array(
95+
'id' => $n,
96+
'ip_address' => $rec['ip_address'],
97+
'netmask' => $server_config["netmask"],
98+
'gateway' => $server_config["gateway"],
99+
'broadcast' => $this->broadcast($rec['ip_address'],$server_config["netmask"]),
100+
'network' => $this->network($rec['ip_address'],$server_config["netmask"])
101+
);
102+
$additionl_ip_records = 1;
103+
$n++;
104+
}
105+
}
106+
107+
$network_tpl->setVar('additionl_ip_records',$additionl_ip_records);
108+
$network_tpl->setLoop('interfaces',$ip_records);
109+
110+
file_put_contents('/etc/network/interfaces',$network_tpl->grab());
111+
unset($network_tpl);
112+
113+
$app->log("Changed Network settings",LOGLEVEL_DEBUG);
114+
115+
exec('/etc/init.d/networking force-reload');
116+
}
117+
118+
}
119+
120+
function network($ip, $netmask){
121+
$netmask = $this->netmask($netmask);
122+
list($f1,$f2,$f3,$f4) = explode(".", $netmask);
123+
$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);
124+
list($f1,$f2,$f3,$f4) = explode(".", $ip);
125+
$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);
126+
for($i=0;$i<32;$i++){
127+
$network_bin .= substr($netmask_bin,$i,1) * substr($ip_bin,$i,1);
128+
}
129+
$network_bin = wordwrap($network_bin, 8, ".", 1);
130+
list($f1,$f2,$f3,$f4) = explode(".", trim($network_bin));
131+
return bindec($f1).".".bindec($f2).".".bindec($f3).".".bindec($f4);
132+
}
133+
134+
function broadcast($ip, $netmask){
135+
$netmask = $this->netmask($netmask);
136+
$binary_netmask = $this->binary_netmask($netmask);
137+
list($f1,$f2,$f3,$f4) = explode(".", $ip);
138+
$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);
139+
$broadcast_bin = str_pad(substr($ip_bin, 0, $binary_netmask),32,"1",STR_PAD_RIGHT);
140+
$broadcast_bin = wordwrap($broadcast_bin, 8, ".", 1);
141+
list($f1,$f2,$f3,$f4) = explode(".", trim($broadcast_bin));
142+
return bindec($f1).".".bindec($f2).".".bindec($f3).".".bindec($f4);
143+
}
144+
145+
function netmask($netmask){
146+
list($f1,$f2,$f3,$f4) = explode(".", trim($netmask));
147+
$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);
148+
$parts = explode("0", $bin);
149+
$bin = str_pad($parts[0], 32, "0", STR_PAD_RIGHT);
150+
$bin = wordwrap($bin, 8, ".", 1);
151+
list($f1,$f2,$f3,$f4) = explode(".", trim($bin));
152+
return bindec($f1).".".bindec($f2).".".bindec($f3).".".bindec($f4);
153+
}
154+
155+
function binary_netmask($netmask){
156+
list($f1,$f2,$f3,$f4) = explode(".", trim($netmask));
157+
$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);
158+
$parts = explode("0", $bin);
159+
return substr_count($parts[0], "1");
160+
}
161+
162+
} // end class
163+
164+
165+
166+
?>

0 commit comments

Comments
 (0)