Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
commit | author | age
7abfae 1 <?php
J 2
3 /*
4 Copyright (c) 2007, 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 {
7fe908 32
7abfae 33     var $plugin_name = 'network_settings_plugin';
J 34     var $class_name = 'network_settings_plugin';
7fe908 35
MC 36
7abfae 37     //* This function is called during ispconfig installation to determine
J 38     //  if a symlink shall be created for this plugin.
39     function onInstall() {
40         global $conf;
7fe908 41
7abfae 42         return true;
7fe908 43
7abfae 44     }
7fe908 45
7abfae 46     /*
J 47          This function is called when the plugin is loaded
48     */
7fe908 49
7abfae 50     function onLoad() {
J 51         global $app;
7fe908 52
7abfae 53         /*
J 54         Register for the events
55         */
7fe908 56
MC 57         $app->plugins->registerEvent('server_insert', 'network_settings_plugin', 'insert');
58         $app->plugins->registerEvent('server_update', 'network_settings_plugin', 'update');
59
60         $app->plugins->registerEvent('server_ip_insert', 'network_settings_plugin', 'insert');
61         $app->plugins->registerEvent('server_ip_update', 'network_settings_plugin', 'update');
62
63
64
7abfae 65     }
7fe908 66
MC 67     function insert($event_name, $data) {
7abfae 68         global $app, $conf;
7fe908 69
MC 70         $this->update($event_name, $data);
71
7abfae 72     }
7fe908 73
7abfae 74     // The purpose of this plugin is to rewrite the main.cf file
7fe908 75     function update($event_name, $data) {
7abfae 76         global $app, $conf;
7fe908 77
7abfae 78         // get the config
663caf 79         $app->uses('getconf');
J 80         $server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
7fe908 81
1bf462 82         //* Configure the debian network card settings.
T 83         //* Dont configure
7fe908 84         if($server_config['auto_network_configuration'] == 'y' &&
MC 85             $data['mirrored'] == false &&
86             $server_config['ip_address'] != '0.0.0.0' &&
87             $server_config['gateway'] != '0.0.0.0') {
88
89             if (is_file('/etc/debian_version'))
7abfae 90             {
7fe908 91                 copy('/etc/network/interfaces', '/etc/network/interfaces~');
MC 92
7abfae 93                 $app->load('tpl');
7fe908 94
7abfae 95                 $network_tpl = new tpl();
663caf 96                 $network_tpl->newTemplate('debian_network_interfaces.master');
7fe908 97
MC 98                 $network_tpl->setVar('ip_address', $server_config['ip_address']);
99                 $network_tpl->setVar('netmask', $server_config['netmask']);
100                 $network_tpl->setVar('gateway', $server_config['gateway']);
101                 $network_tpl->setVar('broadcast', $this->broadcast($server_config['ip_address'], $server_config['netmask']));
102                 $network_tpl->setVar('network', $this->network($server_config['ip_address'], $server_config['netmask']));
103
cc7a82 104                 $records = $app->db->queryAllRecords("SELECT ip_address FROM server_ip WHERE server_id = ? ORDER BY server_ip_id ASC", $conf['server_id']);
7abfae 105                 $ip_records = array();
J 106                 $additionl_ip_records = 0;
107                 $n = 0;
108                 if(is_array($records)) {
109                     foreach($records as $rec) {
110                         /*
111                          * don't insert the main-ip again!
112                          */
99fcaa 113                         if ($rec['ip_address'] != $server_config['ip_address'] && filter_var($rec['ip_address'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
7abfae 114                         {
J 115                             $ip_records[$n] = array(
116                                 'id' => $n,
117                                 'ip_address' => $rec['ip_address'],
663caf 118                                 'netmask' => $server_config['netmask'],
J 119                                 'gateway' => $server_config['gateway'],
7fe908 120                                 'broadcast' => $this->broadcast($rec['ip_address'], $server_config['netmask']),
MC 121                                 'network' => $this->network($rec['ip_address'], $server_config['netmask'])
7abfae 122                             );
J 123                             $additionl_ip_records = 1;
124                             $n++;
125                         }
126                     }
127                 }
7fe908 128
7abfae 129                 /*
J 130                  * If we have more than 1 IP we have to add the main-ip at the end
131                  * of the network-ip-list. If we don't do so, there may be problems
132                  * in multi-server-settings (with the acces from other server to the
133                  * main-server) because the LAST IP in the list is the IP mysql uses
134                  * to determine the host, the user is logging in from.
135                  */
fb3a98 136                 /*
T 137                 // Disabled this part as it causes problems on multiserver setups
7abfae 138                 if ($additionl_ip_records != 0)
J 139                 {
663caf 140                     $swap['ip_address'] = $ip_records[$n-1]['ip_address'];
J 141                     $swap['netmask'] = $ip_records[$n-1]['netmask'];
142                     $swap['gateway'] = $ip_records[$n-1]['gateway'];
7fe908 143
7abfae 144                     $ip_records[$n-1] = array(
J 145                         'id' => $n-1,
146                         'ip_address' => $server_config['ip_address'],
663caf 147                         'netmask' => $server_config['netmask'],
J 148                         'gateway' => $server_config['gateway'],
149                         'broadcast' => $this->broadcast($server_config['ip_address'],$server_config['netmask']),
150                         'network' => $this->network($server_config['ip_address'],$server_config['netmask'])
7abfae 151                     );
663caf 152                     $network_tpl->setVar('ip_address',$swap['ip_address']);
J 153                     $network_tpl->setVar('netmask',$swap['netmask']);
154                     $network_tpl->setVar('gateway',$swap['gateway']);
155                     $network_tpl->setVar('broadcast',$this->broadcast($swap['ip_address'],$swap['netmask']));
156                     $network_tpl->setVar('network',$this->network($swap['ip_address'],$swap['netmask']));
7abfae 157                 }
fb3a98 158                 */
7fe908 159
MC 160                 $network_tpl->setVar('additionl_ip_records', $additionl_ip_records);
161                 $network_tpl->setLoop('interfaces', $ip_records);
162                 file_put_contents('/etc/network/interfaces', $network_tpl->grab());
7abfae 163                 unset($network_tpl);
7fe908 164
MC 165                 $app->log('Changed Network settings', LOGLEVEL_DEBUG);
7abfae 166                 exec($conf['init_scripts'] . '/' . 'networking force-reload');
7fe908 167             }
MC 168             elseif (is_file('/etc/gentoo-release'))
7abfae 169             {
7fe908 170                 copy('/etc/conf.d/net', '/etc/conf.d/net~');
MC 171
7abfae 172                 $app->load('tpl');
7fe908 173
7abfae 174                 $network_tpl = new tpl();
663caf 175                 $network_tpl->newTemplate('gentoo_network_interfaces.master');
7fe908 176
MC 177                 $network_tpl->setVar('ip_address', $server_config['ip_address']);
178                 $network_tpl->setVar('netmask', $server_config['netmask']);
179                 $network_tpl->setVar('gateway', $server_config['gateway']);
180                 $network_tpl->setVar('broadcast', $this->broadcast($server_config['ip_address'], $server_config['netmask']));
181
cc7a82 182                 $records = $app->db->queryAllRecords("SELECT ip_address FROM server_ip WHERE server_id = ? order by ip_address", $conf['server_id']);
7abfae 183                 $ip_records = array();
J 184                 $additionl_ip_records = 0;
185                 $n = 0;
186                 if(is_array($records)) {
187                     foreach($records as $rec) {
188                         /*
189                          * don't insert the main-ip again!
190                          */
99fcaa 191                         if ($rec['ip_address'] != $server_config['ip_address'] && filter_var($rec['ip_address'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
7abfae 192                         {
J 193                             $ip_records[$n] = array(
194                                 'id' => $n,
195                                 'ip_address' => $rec['ip_address'],
663caf 196                                 'netmask' => $server_config['netmask'],
J 197                                 'gateway' => $server_config['gateway'],
7fe908 198                                 'broadcast' => $this->broadcast($rec['ip_address'], $server_config['netmask'])
7abfae 199                             );
J 200                             $additionl_ip_records = 1;
201                             $n++;
202                         }
203                     }
204                 }
7fe908 205
7abfae 206                 /*
J 207                  * If we have more than 1 IP we have to add the main-ip at the end
208                  * of the network-ip-list. If we don't do so, there may be problems
209                  * in multi-server-settings (with the acces from other server to the
210                  * main-server) because the LAST IP in the list is the IP mysql uses
211                  * to determine the host, the user is logging in from.
212                  */
213                 if ($additionl_ip_records != 0)
214                 {
663caf 215                     $swap['ip_address'] = $ip_records[$n-1]['ip_address'];
J 216                     $swap['netmask'] = $ip_records[$n-1]['netmask'];
217                     $swap['gateway'] = $ip_records[$n-1]['gateway'];
7fe908 218
7abfae 219                     $ip_records[$n-1] = array(
J 220                         'id' => $n-1,
221                         'ip_address' => $server_config['ip_address'],
663caf 222                         'netmask' => $server_config['netmask'],
J 223                         'gateway' => $server_config['gateway'],
7fe908 224                         'broadcast' => $this->broadcast($server_config['ip_address'], $server_config['netmask'])
7abfae 225                     );
7fe908 226                     $network_tpl->setVar('ip_address', $swap['ip_address']);
MC 227                     $network_tpl->setVar('netmask', $swap['netmask']);
228                     $network_tpl->setVar('gateway', $swap['gateway']);
229                     $network_tpl->setVar('broadcast', $this->broadcast($swap['ip_address'], $swap['netmask']));
7abfae 230                 }
7fe908 231
MC 232                 $network_tpl->setVar('additionl_ip_records', $additionl_ip_records);
233                 $network_tpl->setLoop('interfaces', $ip_records);
234                 file_put_contents('/etc/conf.d/net', $network_tpl->grab());
7abfae 235                 unset($network_tpl);
7fe908 236
MC 237                 $app->log('Changed Network settings', LOGLEVEL_DEBUG);
7abfae 238                 exec($conf['init_scripts'] . '/' . 'net.eth0 restart');
J 239             }
7fe908 240             else {
MC 241                 $app->log('Network configuration not available for this Linux distribution.', LOGLEVEL_DEBUG);
242             }
243
7abfae 244         } else {
e63545 245             if($data['mirrored'] == true) {
a43eb3 246                 $app->log('Skipping network config request. IP addresses from master are not configured on the mirror.', LOGLEVEL_DEBUG);
7fe908 247             }
e63545 248             if($server_config['auto_network_configuration'] == 'n') {
7fe908 249                 $app->log('Network configuration disabled in server settings.', LOGLEVEL_DEBUG);
e63545 250             }
7abfae 251         }
a43eb3 252         
TB 253         //* Configure hostname
254         if($event_name == 'server_update' && $data['mirrored'] == false) {
255             
256             //* get old server config
257             $tmp = $app->ini_parser->parse_ini_string(stripslashes($data['old']['config']));
258             $old_server_config = $tmp['server'];
259             unset($tmp);
260             
261             $new_hostname = trim($server_config['hostname']);
262             $old_hostname = trim($old_server_config['hostname']);
263             
264             if($new_hostname != '' && $old_hostname != $new_hostname) {
265                 
266                 if(is_file('/etc/hostname')) {
267                     $app->system->file_put_contents('/etc/hostname',$new_hostname);
268                     $app->log('Changed /etc/hostname to '.$new_hostname, LOGLEVEL_DEBUG);
269                 }
270                 
271                 if(is_file('/etc/mailname')) {
272                     $app->system->file_put_contents('/etc/mailname',$new_hostname);
273                     $app->log('Changed /etc/mailname to '.$new_hostname, LOGLEVEL_DEBUG);
274                 }
275                 
276                 $postconf_commands = array(
277                     'myhostname = '.$new_hostname,
278                     'mydestination = '.$new_hostname.', localhost, localhost.localdomain'
279                 );
280                 
281                 //* Executing the postconf commands
282                 foreach($postconf_commands as $cmd) {
283                     $command = "postconf -e '$cmd'";
284                     exec($command);
285                 }
286                 
287                 $app->log('Changed changed myhostname and mydestination in postfix main.cf to '.$new_hostname, LOGLEVEL_DEBUG);
288                 
289                 //* change /etc/hosts
290                 $hosts = file_get_contents('/etc/hosts');
291                 $hosts = str_replace($old_hostname,$new_hostname,$hosts);
292                 $app->system->file_put_contents('/etc/hosts',$hosts);
293                 
294                 exec($app->system->getinitcommand('postfix', 'restart').' 2>&1');
295                 exec($app->system->getinitcommand('networking', 'restart').' 2>&1');
296                 
297             }
298             
299         }
300         
7fe908 301
7abfae 302     }
7fe908 303
7abfae 304     function network($ip, $netmask){
J 305         $netmask = $this->netmask($netmask);
7fe908 306         list($f1, $f2, $f3, $f4) = explode('.', $netmask);
MC 307         $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);
308         list($f1, $f2, $f3, $f4) = explode('.', $ip);
309         $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);
7abfae 310         for($i=0;$i<32;$i++){
7fe908 311             $network_bin .= substr($netmask_bin, $i, 1) * substr($ip_bin, $i, 1);
7abfae 312         }
663caf 313         $network_bin = wordwrap($network_bin, 8, '.', 1);
7fe908 314         list($f1, $f2, $f3, $f4) = explode('.', trim($network_bin));
663caf 315         return bindec($f1).'.'.bindec($f2).'.'.bindec($f3).'.'.bindec($f4);
7abfae 316     }
J 317
318     function broadcast($ip, $netmask){
319         $netmask = $this->netmask($netmask);
320         $binary_netmask = $this->binary_netmask($netmask);
7fe908 321         list($f1, $f2, $f3, $f4) = explode('.', $ip);
MC 322         $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);
323         $broadcast_bin = str_pad(substr($ip_bin, 0, $binary_netmask), 32, '1', STR_PAD_RIGHT);
663caf 324         $broadcast_bin = wordwrap($broadcast_bin, 8, '.', 1);
7fe908 325         list($f1, $f2, $f3, $f4) = explode('.', trim($broadcast_bin));
663caf 326         return bindec($f1).'.'.bindec($f2).'.'.bindec($f3).'.'.bindec($f4);
7abfae 327     }
7fe908 328
7abfae 329     function netmask($netmask){
7fe908 330         list($f1, $f2, $f3, $f4) = explode('.', trim($netmask));
MC 331         $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);
663caf 332         $parts = explode('0', $bin);
J 333         $bin = str_pad($parts[0], 32, '0', STR_PAD_RIGHT);
334         $bin = wordwrap($bin, 8, '.', 1);
7fe908 335         list($f1, $f2, $f3, $f4) = explode('.', trim($bin));
663caf 336         return bindec($f1).'.'.bindec($f2).'.'.bindec($f3).'.'.bindec($f4);
7abfae 337     }
J 338
339     function binary_netmask($netmask){
7fe908 340         list($f1, $f2, $f3, $f4) = explode('.', trim($netmask));
MC 341         $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);
663caf 342         $parts = explode('0', $bin);
J 343         return substr_count($parts[0], '1');
7abfae 344     }
J 345
346 } // end class
347
348
349
350 ?>