#!/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.
#
# sub/check_suid - 06/14/93
#
#-----------------------------------------------------------------------------
# TODO
# - Consider fixing the Tiger_Admin_Accounts check so that it takes into
#   account Tiger_Accounts_Trust too
#-----------------------------------------------------------------------------
# This script is not runnable directly.
# 
inputfile="$1"

[ "$CONFIGURED_ALREADY" != "YES" ] && {
  echo "--ERROR-- [init008e] This script can not be run directly."
  exit 1
}

. $BASEDIR/initdefs

#
# If run in test mode (-t) this will verify that all required
# elements are set.
#
[ "$Tiger_TESTMODE" = 'Y' ] && {
  haveallcmds RM FILECMD SGREP LS STRINGS SORT COMM CAT AWK || exit 1
  haveallfiles BASEDIR WORKDIR SUID_LIST || exit 1
  
  echo "--CONFIG-- [init003c] $0: Configuration ok..."
  exit 0
}

#------------------------------------------------------------------------
echo
echo "# Checking setuid executables..."

haveallfiles BASEDIR WORKDIR || exit 1

[ -n "$TigerCheckEmbedded" -a "x$Tiger_Embed_Check_SUID" = 'xY' ] && {
  while read file
  do
    $LS -lLd "$file" 2>/dev/null | {
      read p l owner rest
      [ "$owner" = root ] && echo "$file" >> $TigerCheckEmbedded
    }
  done
}

haveallcmds FILECMD LS SGREP STRINGS && {
  while read file
  do
    $FILECMD "$file" 2>/dev/null | $SGREP script 2>/dev/null && {
      message FAIL fsys001f "" "File $file is a setuid script:"
      $LS $LSGROUP -ld "$file"
    }

    getpermit "$file" 2>/dev/null |
    while read _file owner group ur uw ux gr gw gx or ow ox suid sgid stk
    do
      eval "case \"$owner\" in
        $Tiger_Admin_Accounts|root)
          ;;
        *)
          message FAIL fsys0012w \"\" \"File $file is not owned by an administrative user.\"
          $LS $LSGROUP -ld \"$file\"
      esac"
    done
 
    case "$file" in
    *xterm) {
      message WARN misc013w "" "$file: see CERT Advisory CA-93:17 about a security hole in xterm (does not apply to HP-UX)."
    }
    ;;
    esac
  
    $STRINGS - "$file" 2>/dev/null |
    $SGREP '\.\./' 2>/dev/null && {
      $SGREP "$file" $REL_FILE_EXCP 2>/dev/null || {
	case "$file" in
	  */xload)
	  message WARN misc015w "" "$file appears to contain relative pathnames.  There is a known security vulnerability with xload in this configuration."
	  ;;
	  # The following is contributed by Bob Hall for HP-UX (it is not active by default)
	  # TODO: find a way to read this from a file and check them instead of the case
# /sbin/lvchange) ;;
# /sbin/lvcreate) ;;
# /sbin/lvdisplay) ;;
# /sbin/lvextend) ;;
# /sbin/lvlnboot) ;;
# /sbin/lvmerge) ;;
# /sbin/lvsplit) ;;
# /sbin/lvsync) ;;
# /sbin/lvreduce) ;;
# /sbin/lvremove) ;;
# /sbin/lvrmboot) ;;
# /sbin/passwd) ;;
# /sbin/pvchange) ;;
# /sbin/pvck) ;;
# /sbin/pvcreate) ;;
# /sbin/pvdisplay) ;;
# /sbin/pvmove) ;;
# /sbin/pvremove) ;;
# /sbin/sdstolvm) ;;
# /sbin/shut                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           