Package: logcheck
Version: 1.3.7
Severity: normal

After installing logcheck on a system it posted an error to the cron
mail address hourly.  An untrapped script error was posting stderr
output to cron.  It originated here:

  # Hostname either fully qualified or not.
  if [ $FQDN -eq 1 ]; then
          HOSTNAME="$(hostname --fqdn)" > /dev/null 2>&1
  else
          HOSTNAME="$(hostname --short)" > /dev/null 2>&1
  fi

There has obviously been an attempt to silence errors in this area.
Unfortunately that isn't the correct shell syntax to do this.  That is
silencing the output of the assignment and not of the hostname command
itself.  Therefore if the hostname isn't registered in DNS the error
message is still emitted to stderr.

  $ hostname --short
  hostname: Unknown host

  $ hostname --long
  hostname: Unknown host

I believe what was intended was the following:

  # Hostname either fully qualified or not.
  if [ $FQDN -eq 1 ]; then
          HOSTNAME="$(hostname --fqdn 2>/dev/null)"
  else
          HOSTNAME="$(hostname --short 2>/dev/null)"
  fi

However this will still fail to produce a correct hostnames in the
face of an unresolvable hostname in DNS.  And I will guess that the
short hostname is the more typical case these days since it is the
default in Debian.  Therefore it would be better if for the short case
the hostname is received and then truncated at the first dot if one
exists.  This will avoid this error for the short case entirely.
Because the script is already a #!/bin/bash script it is safe to use a
POSIX shell parameter expansion construct.  Here is an improvement.

  # Hostname either fully qualified or not.
  if [ $FQDN -eq 1 ]; then
          HOSTNAME=$(hostname --fqdn 2>/dev/null)
          test -z "$HOSTNAME" && HOSTNAME=$(hostname)
  else
          HOSTNAME=$(hostname)
          HOSTNAME=${HOSTNAME%%.*}
  fi

I have tested this on my system and it produced the desired result.

The process of determining the FQDN using 'hostname --fqdn' is still
problematic since the hostname command doesn't do the Right Thing
there but that is a different topic.  Determining the one single
domain name of a host is fraught with subtle issues.

Note: Since shell word splitting isn't done on variable assignment the
extra quotes are not needed there to protect the right hand side value
from word splitting.  For example:

  $ foo=$(echo "one   two   three")
  $ echo "$foo"
  one   two   three

Thanks,
Bob



-- 
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to