Skip to content

Commit a43eb3b

Browse files
author
Till Brehm
committed
Implemented: FS#3568 - Configure server hostname trough ISPConfig
1 parent 061d7c8 commit a43eb3b

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,11 @@
126126
2 => array( 'event' => 'SAVE',
127127
'type' => 'TOLOWER')
128128
),
129-
'validators' => array(0 => array('type' => 'NOTEMPTY',
130-
'errmsg' => 'hostname_error_empty'),
129+
'validators' => array( 0 => array('type' => 'NOTEMPTY',
130+
'errmsg' => 'hostname_error_empty'),
131+
1 => array ('type' => 'REGEX',
132+
'regex' => '/^[\w\.\-]{2,255}\.[a-zA-Z0-9\-]{2,30}$/',
133+
'errmsg'=> 'hostname_error_regex'),
131134
),
132135
'value' => '',
133136
'width' => '40',

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ $wb["ip_address_error_wrong"] = 'Invalid IP address format.';
4949
$wb["netmask_error_wrong"] = 'Invalid Netmask format.';
5050
$wb["gateway_error_wrong"] = 'Invalid Gateway format.';
5151
$wb["hostname_error_empty"] = 'Hostname is empty.';
52+
$wb["hostname_error_regex"] = 'Invalid Hostname.';
5253
$wb["nameservers_error_empty"] = 'Nameserver is empty.';
5354
$wb["config_dir_txt"] = 'Config directory';
5455
$wb["init_script_txt"] = 'Cron init script name';

server/plugins-available/network_settings_plugin.inc.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,61 @@ function update($event_name, $data) {
243243

244244
} else {
245245
if($data['mirrored'] == true) {
246-
$app->log('Skipping network config request. IP addresses from amster are not configured on the mirror.', LOGLEVEL_DEBUG);
246+
$app->log('Skipping network config request. IP addresses from master are not configured on the mirror.', LOGLEVEL_DEBUG);
247247
}
248248
if($server_config['auto_network_configuration'] == 'n') {
249249
$app->log('Network configuration disabled in server settings.', LOGLEVEL_DEBUG);
250250
}
251251
}
252+
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+
252301

253302
}
254303

0 commit comments

Comments
 (0)