From a43eb3b0ccaf84a6ab44a1f46f47cc4f8ce54844 Mon Sep 17 00:00:00 2001 From: Till Brehm Date: Tue, 29 Jul 2014 20:39:09 +0200 Subject: [PATCH] Implemented: FS#3568 - Configure server hostname trough ISPConfig --- .../web/admin/form/server_config.tform.php | 7 ++- .../web/admin/lib/lang/en_server_config.lng | 1 + .../network_settings_plugin.inc.php | 51 ++++++++++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/interface/web/admin/form/server_config.tform.php b/interface/web/admin/form/server_config.tform.php index 71de54e95..4fa71790c 100644 --- a/interface/web/admin/form/server_config.tform.php +++ b/interface/web/admin/form/server_config.tform.php @@ -126,8 +126,11 @@ $form["tabs"]['server'] = array( 2 => array( 'event' => 'SAVE', 'type' => 'TOLOWER') ), - 'validators' => array(0 => array('type' => 'NOTEMPTY', - 'errmsg' => 'hostname_error_empty'), + 'validators' => array( 0 => array('type' => 'NOTEMPTY', + 'errmsg' => 'hostname_error_empty'), + 1 => array ('type' => 'REGEX', + 'regex' => '/^[\w\.\-]{2,255}\.[a-zA-Z0-9\-]{2,30}$/', + 'errmsg'=> 'hostname_error_regex'), ), 'value' => '', 'width' => '40', diff --git a/interface/web/admin/lib/lang/en_server_config.lng b/interface/web/admin/lib/lang/en_server_config.lng index 63027c733..5ce704d9e 100644 --- a/interface/web/admin/lib/lang/en_server_config.lng +++ b/interface/web/admin/lib/lang/en_server_config.lng @@ -49,6 +49,7 @@ $wb["ip_address_error_wrong"] = 'Invalid IP address format.'; $wb["netmask_error_wrong"] = 'Invalid Netmask format.'; $wb["gateway_error_wrong"] = 'Invalid Gateway format.'; $wb["hostname_error_empty"] = 'Hostname is empty.'; +$wb["hostname_error_regex"] = 'Invalid Hostname.'; $wb["nameservers_error_empty"] = 'Nameserver is empty.'; $wb["config_dir_txt"] = 'Config directory'; $wb["init_script_txt"] = 'Cron init script name'; diff --git a/server/plugins-available/network_settings_plugin.inc.php b/server/plugins-available/network_settings_plugin.inc.php index 56e4980fe..46242d984 100644 --- a/server/plugins-available/network_settings_plugin.inc.php +++ b/server/plugins-available/network_settings_plugin.inc.php @@ -243,12 +243,61 @@ class network_settings_plugin { } else { if($data['mirrored'] == true) { - $app->log('Skipping network config request. IP addresses from amster are not configured on the mirror.', LOGLEVEL_DEBUG); + $app->log('Skipping network config request. IP addresses from master are not configured on the mirror.', LOGLEVEL_DEBUG); } if($server_config['auto_network_configuration'] == 'n') { $app->log('Network configuration disabled in server settings.', LOGLEVEL_DEBUG); } } + + //* Configure hostname + if($event_name == 'server_update' && $data['mirrored'] == false) { + + //* get old server config + $tmp = $app->ini_parser->parse_ini_string(stripslashes($data['old']['config'])); + $old_server_config = $tmp['server']; + unset($tmp); + + $new_hostname = trim($server_config['hostname']); + $old_hostname = trim($old_server_config['hostname']); + + if($new_hostname != '' && $old_hostname != $new_hostname) { + + if(is_file('/etc/hostname')) { + $app->system->file_put_contents('/etc/hostname',$new_hostname); + $app->log('Changed /etc/hostname to '.$new_hostname, LOGLEVEL_DEBUG); + } + + if(is_file('/etc/mailname')) { + $app->system->file_put_contents('/etc/mailname',$new_hostname); + $app->log('Changed /etc/mailname to '.$new_hostname, LOGLEVEL_DEBUG); + } + + $postconf_commands = array( + 'myhostname = '.$new_hostname, + 'mydestination = '.$new_hostname.', localhost, localhost.localdomain' + ); + + //* Executing the postconf commands + foreach($postconf_commands as $cmd) { + $command = "postconf -e '$cmd'"; + exec($command); + } + + $app->log('Changed changed myhostname and mydestination in postfix main.cf to '.$new_hostname, LOGLEVEL_DEBUG); + + //* change /etc/hosts + $hosts = file_get_contents('/etc/hosts'); + $hosts = str_replace($old_hostname,$new_hostname,$hosts); + $app->system->file_put_contents('/etc/hosts',$hosts); + + exec($app->system->getinitcommand('postfix', 'restart').' 2>&1'); + exec($app->system->getinitcommand('networking', 'restart').' 2>&1'); + + } + + } + } -- GitLab