#!/bin/sh
#
#     tiger - A UN*X security checking system
#     Copyright (C) 1993 Douglas Lee Schales, David K. Hess, David R. Safford
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 1, or (at your option)
#    any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#     Please see the file `COPYING' for the complete copyright notice.
#
# tiger - 06/14/93
#
# The UN*X security check system. This is the main program that will run
# all the checks configured for your system in the tigerrc configuration 
# file.
#
# 05/14/2005 jfs Patch from Nicolas Franois which fixes aide output handling
# 01/25/2005 cslater Added separate config variable for 
#		 check_passwdformat.
# 01/15/2003 jfs Check should use INETDCONF (and not INETDFILE).
#                (Temporary fix: defined it if undefined)
# 09/03/2003 jfs Included check_ssh to the checks
# 08/19/2003 jfs check_cron is now check_crontabs
# 08/15/2003 jfs Integrated ARSC log to syslog mechanism but modified
#                the check for it (better to use the same mechanism as
#                used by the sendtologger function in order to avoid
#                some issues and prepended it to the use of -E since
#                otherwise a wrong LOGDIR would be used (with \@) for
#                the logfile and expfile)
# 08/14/2003 jfs Commands now run through run_script (except for check_cron
#                since many systems hold a check_cron script)
# 05/09/2003 jfs Fixed missing bracket (syntax error)
# 05/01/2003 jfs Added missing checks
# 04/27/93 dls check_passwd is now check_accounts
# 04/15/2003 jfs Added new year
# 05/26/2002 jfs Added check_tcpd test
# 04/29/93 dls Added support for 'tigerrc' file.
#
#-----------------------------------------------------------------------------
#
echo "Tiger UN*X security checking system"
echo "   Developed by Texas A&M University, 1994"
echo "   Updated by the Advanced Research Corporation, 1999-2002"
echo "   Further updated by Javier Fernandez-Sanguino, 2001-2015"
echo "   Contributions by Francisco Manuel Garcia Claramonte, 2009-2010"
echo "   Covered by the GNU General Public License (GPL)"
echo
TigerInstallDir="/usr/lib/tiger"
#
# Set default base directory.
# Order or preference:
#      -B option
#      TIGERHOMEDIR environment variable
#      TigerInstallDir installed location
#
basedir=${TIGERHOMEDIR:=$TigerInstallDir}

for parm
do
   case $parm in
   -B) basedir=$2; break;;
   esac
done

#
# Verify that a config file exists there, and if it does
# source it.
#
[ ! -r $basedir/config ] && {
  echo "--ERROR-- [init002e] No 'config' file in \`$basedir'."
  exit 1
}

. $basedir/config

. $BASEDIR/initdefs

sendtologger()
{
  file=$1
  if [ -r "$file" ]; then
  	echo "Cannot read report in $file to send to syslog"
	exit 1
  fi
  logger_port="5353"
  logger_name=`echo $LOGDIR | $AWK -F\@ '{print $2}'`
  safe_temp $WORKDIR/logger.$$
  $TigerCleanup="$TigerCleanup $WORKDIR/logger.$$"
  echo "sleep 5; cat $logfile; sleep 5" >$WORKDIR/logger.$$
  sh $WORKDIR/logger.$$ | telnet $logger_name $logger_port
  if [ "$?" -ne 0 ]; then
   echo "TIGER logger server [$logger_name] is not available"
   delete $WORKDIR/logger.$$
   exit 1
  fi
  delete $WORKDIR/logger.$$
  # Do not proceed since the LOGDIR value would be useless for us
  # TODO: Consider use a -s switch to define logging to syslog
  # so that logs are always kept locally too
  exit 0
}

tigercleanup()
{
  [ -n "$fspid" ] && kill -1 $fspid 2>/dev/null
  [ -f "$Tiger_PasswdFiles" ] && {
    while read __file
    do
      delete $__file $__file.src
    done < $Tiger_PasswdFiles
  }
  [ -n "$TigerCheckEmbedded" ] && delete $TigerCheckEmbedded
  delete $WORKDIR/fsscan.log$$ $Tiger_PasswdFiles

  [ -n "$TigerCleanup" ] && {
    delete $TigerCleanup
  }  
}

trap 'tigercleanup; exit 1' 1 2 3 15

#
# If run in test mode (-t) this will verify that all required
# elements are set.
#
[ "$Tiger_TESTMODE" = 'Y' ] && {
  haveallcmds CAT DATE DATECMD GEN_PASSWD_SETS GREP MV RM TIMECMD || exit 1
  haveallfiles BASEDIR SCRIPTDIR WORKDIR CONFIG_DIR LOGDIR || exit 1
  haveallvars HOSTNAME OS REL REV ARCH || exit 1
  
  echo "--CONFIG-- [init003c] $0: Configuration ok..."
  exit 0
}

#---------------------------------------                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         