#!/bin/bash
#
# test_aide
#
# Aide aufrufen, Ausgabe der AIDE-DB an ULS
#
##############################################
CONF=/etc/aide.conf
SECTION="Security"
TESTSTEP="Aide"
#
unset CONF LANG LC_CTYPE
export LANG=C
#
while getopts c:s:t:? op
 do
  case "$op" in
    c) CONF=$OPTARG;;
    s) SECTION=$OPTARG;;
    t) TESTSTEP=$OPTARG;;
    ?) echo "usage: `basename $0` [-c <aideconf>] [-s <ulssection>] [-t <ulsteststep>] [<date>] [<time>]"
       exit 0;;
    *) echo "usage: `basename $0` [-c <aideconf>] [-s <ulssection>] [-t <ulsteststep>] [<date>] [<time>]"
       exit 1;;
  esac
done
shift $(( $OPTIND -1))
#
if [[ $# -lt 2 ]]
 then
  DT="`date '+%F %T%:z'`"
else
  DT="$1 $2"
fi
#
if which xz >/dev/null 2>&1
 then
  COMPRESS=xz
  EXT=xz
else
  COMPRESS=bzip2
  EXT=bz2
fi
#
if [[ -z "$CONF" ]]
 then
  if [[ -f /etc/aide.conf ]]
   then
    CONF=/etc/aide.conf
  elif [[ -f /etc/aide/aide.conf ]]
   then
    CONF=/etc/aide/aide.conf
  else
    echo "Can't find aide.conf"
    exit 1
  fi
elif [[ ! -f "$CONF" ]]
 then
  echo "Config file $CONF not found"
  exit 1
fi
#
test -x /usr/bin/aide || exit 0
#
if /usr/bin/aide --config="$CONF" --config-check 2>/tmp/aidecfgerror.$$
 then
  #
  while pgrep -x aide >/dev/null
   do
    sleep ${RANDOM::2}
  done
  #
  BUFSIZE=`df -iP \`mount | awk '
  $1 ~ "^/dev" && $1 !~ "/dev/loop" && $3 !~ "media" { if( $1 ~ ":" )
        print $3
      else
        print $1}'\` | awk '$1 ~ "^/dev" {i += $3};END{print (i<50000?50000:i)*80}'`
  #
  # Aide Konfigurationsdatei ans ULS
  #
  CFGN=`basename "$CONF"`
  $COMPRESS -c "$CONF" | send_stdin_file -S "$SECTION" "$TESTSTEP" "$CFGN" $DT "$CFGN.$EXT"
  #
  aide --config="$CONF" -B "database_out=stdout" -i | grep -v '^ *#' | sed 's/@@db_spec/ @@db_spec/' | sort | $COMPRESS -c | send_stdin_file -S -a -b ${BUFSIZE:-10240000} "$SECTION" "$TESTSTEP" DB $DT "${CFGN%.*}.$EXT"
  #
else
  send_test_value "$SECTION" "$TESTSTEP" "Config-Error" $DT "$CONF

`cat /tmp/aidecfgerror.$$`" ""
fi
rm /tmp/aidecfgerror.$$
