Handle hostnames with upper-case letters
[webmin.git] / web-lib-funcs.pl
index 7596cbd..16a2a52 100755 (executable)
@@ -1869,7 +1869,8 @@ my ($nowait) = @_;
 return undef if (&is_readonly_mode());
 my %miniserv;
 &get_miniserv_config(\%miniserv) || return;
-if ($main::webmin_script_type eq 'web' && !$ENV{"MINISERV_CONFIG"}) {
+if ($main::webmin_script_type eq 'web' && !$ENV{"MINISERV_CONFIG"} &&
+    !$ENV{'MINISERV_PID'}) {
        # Running under some web server other than miniserv, so do nothing
        return;
        }
@@ -1881,7 +1882,7 @@ if ($gconfig{'os_type'} ne 'windows') {
        $miniserv{'inetd'} && return;
        my @oldst = stat($miniserv{'pidfile'});
        $pid = $ENV{'MINISERV_PID'};
-       if (!$pid) {
+       if (!$pid || !kill(0, $pid)) {
                if (!open(PID, $miniserv{'pidfile'})) {
                        print STDERR "PID file $miniserv{'pidfile'} does ",
                                     "not exist\n";
@@ -1949,7 +1950,8 @@ sub reload_miniserv
 return undef if (&is_readonly_mode());
 my %miniserv;
 &get_miniserv_config(\%miniserv) || return;
-if ($main::webmin_script_type eq 'web' && !$ENV{"MINISERV_CONFIG"}) {
+if ($main::webmin_script_type eq 'web' && !$ENV{"MINISERV_CONFIG"} &&
+    !$ENV{'MINISERV_PID'}) {
        # Running under some web server other than miniserv, so do nothing
        return;
        }
@@ -1959,7 +1961,7 @@ if ($gconfig{'os_type'} ne 'windows') {
        my ($pid, $addr, $i);
        $miniserv{'inetd'} && return;
        $pid = $ENV{'MINISERV_PID'};
-       if (!$pid) {
+       if (!$pid || !kill(0, $pid)) {
                if (!open(PID, $miniserv{'pidfile'})) {
                        print STDERR "PID file $miniserv{'pidfile'} does ",
                                     "not exist\n";
@@ -4152,7 +4154,8 @@ if ($main::webmin_script_type eq 'cron') {
        }
 
 # If debugging is enabled, open the debug log
-if ($gconfig{'debug_enabled'} && !$main::opened_debug_log++) {
+if (($ENV{'WEBMIN_DEBUG'} || $gconfig{'debug_enabled'}) &&
+    !$main::opened_debug_log++) {
        my $dlog = $gconfig{'debug_file'} || $main::default_debug_log_file;
        if ($gconfig{'debug_size'}) {
                my @st = stat($dlog);
@@ -4167,7 +4170,6 @@ if ($gconfig{'debug_enabled'} && !$main::opened_debug_log++) {
                my $script_name = $0 =~ /([^\/]+)$/ ? $1 : '-';
                $main::debug_log_start_time = time();
                &webmin_debug_log("START", "script=$script_name");
-               $main::debug_log_start_module = $module_name;
                }
        }
 
@@ -4401,6 +4403,7 @@ my @langs = &list_languages();
 my $accepted_lang;
 if ($gconfig{'acceptlang'}) {
        foreach my $a (split(/,/, $ENV{'HTTP_ACCEPT_LANGUAGE'})) {
+               $a =~ s/;.*//;  # Remove ;q=0.5 or similar
                my ($al) = grep { $_->{'lang'} eq $a } @langs;
                if ($al) {
                        $accepted_lang = $al->{'lang'};
@@ -5610,8 +5613,6 @@ command is safe for read-only mode users to run.
 sub backquote_with_timeout
 {
 my $realcmd = &translate_command($_[0]);
-&webmin_debug_log('CMD', "cmd=$realcmd timeout=$_[1]")
-       if ($gconfig{'debug_what_cmd'});
 my $out;
 my $pid = &open_execute_command(OUT, "($realcmd) <$null_file", 1, $_[2]);
 my $start = time();
@@ -7079,6 +7080,22 @@ if (%UNCLEAN_ENV) {
        }
 }
 
+=head2 clean_language
+
+Sets all language and locale-related environment variables to US english, to
+ensure that commands run output in the expected language. Can be reverted by
+reset_environment.
+
+=cut
+sub clean_language
+{
+%UNCLEAN_ENV = %ENV;
+$ENV{'LANG'} = '';
+$ENV{'LANGUAGE'} = '';
+$ENV{'LC_ALL'} = '';
+$ENV{'LOCALE'} = '';
+}
+
 =head2 progress_callback
 
 Never called directly, but useful for passing to &http_download to print
@@ -8411,6 +8428,11 @@ $fh = &callers_package($fh);
 my $lockfile = $file;
 $lockfile =~ s/^[^\/]*//;
 if ($lockfile =~ /^\//) {
+       while(-l $lockfile) {
+               # If the file is a link, follow it so that locking is done on
+               # the same file that gets unlocked later
+               $lockfile = &resolve_links($lockfile);
+               }
        $main::open_templocks{$lockfile} = &lock_file($lockfile);
        }
 return &open_tempfile($fh, $file, $noerror, $notemp, $safe);
@@ -8422,14 +8444,12 @@ $main::end_exit_status ||= $?;
 if ($$ == $main::initial_process_id) {
        # Exiting from initial process
        &cleanup_tempnames();
-       if ($gconfig{'debug_what_start'} && $main::debug_log_start_time &&
-           $main::debug_log_start_module eq &get_module_name()) {
+       if ($gconfig{'debug_what_start'} && $main::debug_log_start_time) {
                my $len = time() - $main::debug_log_start_time;
                &webmin_debug_log("STOP", "runtime=$len");
                $main::debug_log_start_time = 0;
                }
-       if (!$ENV{'SCRIPT_NAME'} &&
-           $main::initial_module_name eq &get_module_name()) {
+       if (!$ENV{'SCRIPT_NAME'}) {
                # In a command-line script - call the real exit, so that the
                # exit status gets properly propogated. In some cases this
                # was not happening.