#!/bin/bash

source /usr/local/lib/hostsharing/adr
source /usr/local/lib/hostsharing/pac
source /root/etc/config

aQualHost=`hostname -f`

##############################################################################
# Check open ports

TMPFILE=`mktemp /tmp/portlist-XXXXXX`
HIVEIP=`host -4 -t A $( hostname ) | cut -d' ' -f4`

netstat -a -n -p -A inet | awk '
$6 !~ /LISTEN/  { next; } # Match only Ports in LISTEN state
$4 ~  /127.0.0/ { next; } # Ignore localhost
$4 ~  /:21$/    { next; } #        FTP
$4 ~  /:22$/    { next; } #        ssh
$4 ~  /:25$/    { next; } #        SMTP
$4 ~  /:53$/    { next; } #        DNS
$4 ~  /:80$/    { next; } #        HTTP
$4 ~  /:110$/   { next; } #        POP3
$4 ~  /:111$/   { next; } #        Portmap
$4 ~  /:143$/   { next; } #        IMAP
$4 ~  /:222$/   { next; } #        ssh
$4 ~  /:225$/   { next; } #        SMTP
$4 ~  /:443$/   { next; } #        HTTPS
$4 ~  /:465$/   { next; } #        SMTP over SSL
$4 ~  /:587$/   { next; } #        Mail submission
$4 ~  /:993$/   { next; } #        IMAP over SSL
$4 ~  /:995$/   { next; } #        POP3 over SSL
$4 ~  /:1465$/  { next; } #        SMTP over SSL
$4 ~  /:2401$/  { next; } #        CVS
$4 ~  /:3306$/  { next; } #        MySQL
$4 ~  /:3307$/  { next; } #        MySQL
$4 ~  /:5432$/  { next; } #        PostgreSQL
$4 ~  /:5433$/  { next; } #        PostgreSQL
$4 ~  /:4025$/  { next; } #        SMTP
$4 ~  /:4190$/  { next; } #        Sieve
$4 ~  /:4465$/  { next; } #        SMTP over SSL
$4 ~  /:4587$/  { next; } #        Mail submission
$4 ~  /:4993$/  { next; } #        IMAP over SSL
$4 ~  /:4995$/  { next; } #        POP3 over SSL
{ gsub("/.*$", "", $7); print $7, " ", $4; }' | sort -n >${TMPFILE}

if [[ "$DRYRUN" == "true" ]]; then
  cat ${TMPFILE}
  rm -f ${TMPFILE}
  exit 0
fi

#
# Diffs between days
#
if [ -r /var/run/openports.yesterday ]; then
  DIFFILE=`mktemp /tmp/portlist-XXXXXX`
  NEWFILE=`mktemp /tmp/portlist-XXXXXX`
  awk '{print $2}' ${TMPFILE} |
    sort -n |
    comm -2 -3 - /var/run/openports.yesterday >${DIFFILE}

  grep --file ${DIFFILE} ${TMPFILE} >${NEWFILE}

  if [ -s ${NEWFILE} ]; then
    #
    # Send mail to hostmaster@
    #
    ( echo "To: $EMAIL_HOSTMASTER"
      echo "From: $EMAIL_HOSTMASTER"
      echo "Subject: `hostname --short`: Open Ports"
      echo "Reply-To: hostmaster@hostsharing.net"
      echo "MIME-Version: 1.0"
      echo "X-HS-Message: chk-security"
      echo ""
      echo "chk-security found the following ports newly allocated since the last test:"
      echo ""
      echo "ADDRESS:PORT            PID RUSER    EUSER    CMD"
      while read pid address; do
        address="$address           "
        echo -n "${address:0:22}"
        ps -o pid,ruser,euser,cmd --no-headers -p $pid | cut -c-57
      done <${NEWFILE}
      echo ""
      echo ""
      echo "Currently the following ports are open:"
      echo ""
      cat ${TMPFILE}
    ) | mail-encoded
  fi
  rm -f ${DIFFILE} ${NEWFILE}
fi

awk '{print $2}' ${TMPFILE} | sort -n >/var/run/openports.yesterday

# Skip mail to owner (ph, 15.05.2021)

rm -f ${TMPFILE}
exit 0

#
# Send mail to owner
#
TMPDIR=`mktemp -d /tmp/chk-security-XXXXXX`

while read pid address; do
  account=`ps -o ruser --no-headers -p $pid`
  pac=`echo ${account} | cut -d"-" -f1`

  case "${pac}" in
    root)
      ;;
    daemon)
      ;;
    *)
      ip=`echo ${address} | cut -d: -f1`

      pac_get ${pac}

      if [ "${PAC_IPNUMBER}" != "${ip}" ]; then
        address="$address           "
        echo -n "${address:0:22}" >>${TMPDIR}/${pac}
        ps -o pid,ruser,euser,cmd --no-headers -p $pid | cut -c-57 >>${TMPDIR}/${pac}
      fi
      ;;
  esac
done <${TMPFILE}


for file in ${TMPDIR}/*; do
  if [ -r ${file} ]; then
    pac=`basename ${file}`
    pac_get ${pac} || continue
    addr_get $PAC_MEMBER    

    IP_PAC=`host ${pac}.hostsharing.net | awk '{print $4}'`

    mail-encoded <<EOT
To: $EMAIL_EXTERNAL,$PAC@$THIS_HIVE.hostsharing.net
Cc: $EMAIL_HOSTMASTER 
From: $EMAIL_HOSTMASTER
Reply-To: service@hostsharing.net
Subject: [HS] IP-Adresse fuer Netzwerkdienste im Paket
MIME-Version: 1.0
X-HS-Message: Security
X-HS-Package: ${pac}

Hallo Paket-Admin,

im Paket ${pac} laufen Netzwerkdienste, die nicht die korrekte
IP-Adresse verwenden. In einem Paket laufende Netzwerkdienste müssen
auf die Adresse ${pac}.hostsharing.net bzw. die IP-Adresse ${IP_PAC}
konfiguriert werden.

Ferner bitten wir um Beachtung, dass der Betrieb von Daemons
grundsätzlich nur dann gestattet ist, wenn die entsprechenden
Paketoptionen gebucht und Ports zur Verwendung zugewiesen wurden.

Die Daten der laufenden Prozesse sind:

ADDRESS:PORT            PID RUSER    EUSER    CMD
$(cat ${TMPDIR}/${pac})

Danke für die freundliche Beachtung
	die Hostsharing Hostmaster
$(cat /etc/signature)
EOT
  fi
done


#
# Cleanup
#


rm -f ${TMPFILE}
rm -rf ${TMPDIR}

exit 0
