Compare commits

..

7 Commits

Author SHA1 Message Date
Ilia Ross
18f3e1273f Fix to use a new param to clear cache 2023-08-27 01:27:05 +03:00
Ilia Ross
8ced23d027 Fix to use a new way for clearing hostname cache 2023-08-26 21:26:03 +03:00
Ilia Ross
d834bb6da0 Fix to use state instead of globals in get_system_hostname sub 2023-08-26 21:17:57 +03:00
Jamie Cameron
cc560b00e5 Merge pull request #1990 from webmin/dev/hostname-detection-with-hostnamectl
Add hostname detection using `hostnamectl` command
2023-08-25 13:59:47 -07:00
Jamie Cameron
3e4668715a Merge branch 'master' of github.com:webmin/webmin 2023-08-25 11:25:51 -07:00
Jamie Cameron
053e8dcf22 Remove double HTML escape https://github.com/webmin/usermin/issues/105 2023-08-25 11:25:25 -07:00
Ilia Ross
2d7e57c7e1 Add hostname detection using hostnamectl command 2023-08-25 18:59:03 +03:00
19 changed files with 53 additions and 36 deletions

View File

@@ -120,7 +120,7 @@ if (@filters || &get_global_spamassassin()) {
# Work out nice condition and action descriptions
local $cond;
($cond, $lastalways) = &describe_condition($f);
$cond = &ui_link("edit.cgi?idx=$f->{'index'}", &html_escape($cond));
$cond = &ui_link("edit.cgi?idx=$f->{'index'}", $cond);
local $action = &describe_action($f, \@folders);
# Create mover links

View File

@@ -371,7 +371,7 @@ return &get_system_hostname();
sub save_hostname
{
&system_logged("hostname $_[0] >/dev/null 2>&1");
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
# get_domainname()

View File

@@ -583,7 +583,7 @@ if (&has_command("hostnamectl")) {
" >/dev/null 2>&1");
}
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
# get_domainname()

View File

@@ -583,7 +583,7 @@ my ($hostname) = @_;
&system_logged("hostname ".quotemeta($hostname)." >/dev/null 2>&1");
&save_rc_conf('hostname', $_[0]);
&unlock_file("/etc/rc.conf");
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
sub routing_config_files

View File

@@ -396,7 +396,7 @@ my ($hostname) = @_;
&lock_file($hostconfig);
&set_hostconfig("HOSTNAME", $hostname);
&unlock_file($hostconfig);
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
sub routing_config_files

View File

@@ -437,7 +437,7 @@ if (&has_command("hostnamectl")) {
" >/dev/null 2>&1");
}
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
# get_domainname()

View File

@@ -334,7 +334,7 @@ if (&has_command("hostnamectl")) {
" >/dev/null 2>&1");
}
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
# get_domainname()

View File

@@ -131,7 +131,7 @@ local %conf;
$conf{'HOSTNAME'} = $_[0];
&write_file($network_config, \%conf);
&unlock_file($network_config);
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
# get_domainname()

View File

@@ -417,7 +417,7 @@ my ($hostname) = @_;
&open_lock_tempfile(MYNAME, ">/etc/myname");
&print_tempfile(MYNAME, $hostname,"\n");
&close_tempfile(MYNAME);
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
sub set_line {

View File

@@ -506,7 +506,7 @@ if (&has_command("hostnamectl")) {
" >/dev/null 2>&1");
}
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
# get_domainname()

View File

@@ -274,7 +274,7 @@ sub save_hostname
&open_lock_tempfile(HOST, ">/etc/HOSTNAME");
&print_tempfile(HOST, $_[0],"\n");
&close_tempfile(HOST);
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
sub routing_config_files

View File

@@ -236,7 +236,7 @@ sub save_hostname
&open_lock_tempfile(HOST, ">/etc/HOSTNAME");
&print_tempfile(HOST, $_[0],"\n");
&close_tempfile(HOST);
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
sub routing_config_files

View File

@@ -442,7 +442,7 @@ if (-r "/etc/nodename") {
&print_tempfile(NODENAME, $_[0],"\n");
&close_tempfile(NODENAME);
}
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
# get_domainname()

View File

@@ -172,7 +172,7 @@ local %conf;
&open_lock_tempfile(HOST, ">/etc/HOSTNAME");
&print_tempfile(HOST, $_[0],"\n");
&close_tempfile(HOST);
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
sub routing_config_files

View File

@@ -258,7 +258,7 @@ local %conf;
&open_lock_tempfile(HOST, ">/etc/HOSTNAME");
&print_tempfile(HOST, $_[0],"\n");
&close_tempfile(HOST);
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
sub routing_config_files

View File

@@ -200,7 +200,7 @@ local $rc = &parse_rc_config();
# run SuSEconfig, as this function is called last
&system_logged("SuSEconfig -quick >/dev/null 2>&1");
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
sub routing_config_files

View File

@@ -136,7 +136,7 @@ local %conf;
$conf{'HOSTNAME'} = $_[0];
&write_env_file($network_config, \%conf);
&unlock_file($network_config);
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
# get_domainname()

View File

@@ -314,7 +314,7 @@ return &get_system_hostname();
sub save_hostname
{
&system_logged("hostname $_[0] >/dev/null 2>&1");
undef(@main::get_system_hostname); # clear cache
&get_system_hostname(undef, undef, 2); # clear cache
}
# get_domainname()

View File

@@ -4237,19 +4237,36 @@ my $func = "${pkg}::${sub}";
return defined(&$func);
}
=head2 get_system_hostname([short], [skip-file])
=head2 get_system_hostname([short], [skip-file], [nocache])
Returns the hostname of this system. If the short parameter is set to 1,
then the domain name is not prepended - otherwise, Webmin will attempt to get
the fully qualified hostname, like foo.example.com.
Returns the hostname of this system. If the short parameter is set to 1 then
the domain name is not prepended - otherwise, Webmin will attempt to get the
fully qualified hostname, like foo.example.com.
If the nocache parameter is set to 1 it will flush the cache and re-read the
hostname from the system. If the nocache parameter is set to 2 it will flush
the cache and exit.
=cut
sub get_system_hostname
{
my $m = int($_[0]);
my $skipfile = $_[1];
if (!$main::get_system_hostname[$m]) {
my ($m , $skipfile, $nocache) = @_;
$m = int($m);
state @system_hostname;
undef(@system_hostname) if ($nocache);
return if ($nocache == 2);
if (!$system_hostname[$m]) {
if ($gconfig{'os_type'} ne 'windows') {
# Try hostnamectl command on Linux
if (&has_command("hostnamectl")) {
my $hostname =
&backquote_command("hostnamectl --static");
chop($hostname);
if ($? == 0 && $hostname =~ /\./) {
$hostname =~ s/\..*$// if ($m);
$system_hostname[$m] = $hostname;
return $hostname;
}
}
# Try some common Linux hostname files first
my $fromfile;
if ($skipfile) {
@@ -4330,24 +4347,24 @@ if (!$main::get_system_hostname[$m]) {
if ($m) {
$fromfile =~ s/\..*$//;
}
$main::get_system_hostname[$m] = $fromfile;
$system_hostname[$m] = $fromfile;
return $fromfile;
}
# Can use hostname command on Unix
&execute_command("hostname", undef,
\$main::get_system_hostname[$m], undef, 0, 1);
chop($main::get_system_hostname[$m]);
\$system_hostname[$m], undef, 0, 1);
chop($system_hostname[$m]);
if ($?) {
eval "use Sys::Hostname";
if (!$@) {
$main::get_system_hostname[$m] = eval "hostname()";
$system_hostname[$m] = eval "hostname()";
}
if ($@ || !$main::get_system_hostname[$m]) {
$main::get_system_hostname[$m] = "UNKNOWN";
if ($@ || !$system_hostname[$m]) {
$system_hostname[$m] = "UNKNOWN";
}
}
elsif ($main::get_system_hostname[$m] !~ /\./ &&
elsif ($system_hostname[$m] !~ /\./ &&
$gconfig{'os_type'} =~ /linux$/ &&
!$gconfig{'no_hostname_f'} && !$_[0]) {
# Try with -f flag to get fully qualified name
@@ -4356,7 +4373,7 @@ if (!$main::get_system_hostname[$m]) {
undef, 0, 1);
chop($flag);
if (!$ex && $flag ne "") {
$main::get_system_hostname[$m] = $flag;
$system_hostname[$m] = $flag;
}
}
}
@@ -4368,14 +4385,14 @@ if (!$main::get_system_hostname[$m]) {
# Fall back to net name command
my $out = `net name 2>&1`;
if ($out =~ /\-+\r?\n(\S+)/) {
$main::get_system_hostname[$m] = $1;
$system_hostname[$m] = $1;
}
else {
$main::get_system_hostname[$m] = "windows";
$system_hostname[$m] = "windows";
}
}
}
return $main::get_system_hostname[$m];
return $system_hostname[$m];
}
=head2 get_webmin_version