Fix to make sure the mail URL uses a well-known host name

This commit is contained in:
Ilia Ross
2025-09-21 21:47:12 +03:00
parent 5231b31ddd
commit e88a77d32a
4 changed files with 86 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ package miniserv;
use Socket;
use POSIX;
use Time::Local;
use Fcntl qw(LOCK_EX LOCK_UN);
eval "use Time::HiRes;";
@itoa64 = split(//, "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
@@ -401,6 +402,9 @@ unlink($config{'restartflag'}) if ($config{'restartflag'});
unlink($config{'reloadflag'}) if ($config{'reloadflag'});
unlink($config{'stopflag'}) if ($config{'stopflag'});
# Cleanup well known hosts
&cleanup_wellknown();
# Build list of sockets to listen on
@listening_on_ports = ();
$config{'bind'} = '' if ($config{'bind'} eq '*');
@@ -4823,6 +4827,7 @@ if (defined(&Net::SSLeay::get_servername)) {
$h =~ /^[^\.]+\.(.*)$/ && $ssl_contexts{"*.$1"};
if ($c) {
$ssl_ctx = $c;
&update_wellknown_file($h);
}
}
}
@@ -4989,6 +4994,7 @@ if (!$config{'twofactor_wrapper'}) {
$config{'restartflag'} ||= $var_dir."/restart-flag";
$config{'reloadflag'} ||= $var_dir."/reload-flag";
$config{'stopflag'} ||= $var_dir."/stop-flag";
$config{'wellknown'} ||= $var_dir."/well-known";
}
# read_users_file()
@@ -6186,6 +6192,59 @@ close(BLOCKED);
chmod(0700, $config{'blockedfile'});
}
# update_wellknown_file(hostname)
# Writes out a text file of well-known hosts
sub update_wellknown_file
{
my ($h) = @_;
return if !$h;
my $path = $config{'wellknown'};
my $lock = "$path.lock";
open(my $lk, ">>", $lock) or return;
flock($lk, 2);
# Read current set
my %set;
if (-r $path && open(my $in, "<", $path)) {
local $/ = undef;
my $raw = <$in>;
close $in;
$raw //= '';
$set{ $_ } = 1 for grep { length } split(/\s+/, $raw);
}
# If already known, nothing to do
if ($set{$h}) {
flock($lk, 8);
close($lk);
return;
}
# Add and write out
$set{$h} = 1;
my $tmp = "$path.$$." . int(rand(1_000_000)) . ".tmp";
if (open(my $out, ">", $tmp)) {
print $out join(" ", sort keys %set), "\n";
close $out;
chmod 0700, $tmp;
rename $tmp, $path or unlink $tmp;
}
flock($lk, 8);
close($lk);
}
sub cleanup_wellknown
{
my $path = $config{'wellknown'};
for my $f ($path, "$path.lock") {
next unless -e $f;
unlink $f;
}
}
sub write_pid_file
{
open(PIDFILE, ">$config{'pidfile'}");