#!/bin/bash

# nagios check for CVE-2013-2094
# version 0.8

NAGIOS_OK=0
NAGIOS_WARNING=1
NAGIOS_ERROR=2
NAGIOS_UNKNOWN=3

PAKITI_RESULT="pakiti_results"

OK_KERNEL="2.6.32-358.6.2.el6"
RUNNING_KERNEL=$(uname -r | sed -e "s/.$(uname -m)//")

verlte() {
    [ "$1" = "$(echo -e "$1\n$2" | sort -V | head -n1)" ]
}


if [ -f "$PAKITI_RESULT" ]; then
    cve=$(echo $0 | sed 's/.*check_//')
    grep -q "$cve" "$PAKITI_RESULT" 2>/dev/null
    if [ $? -eq 1 ]; then
        echo "No $cve vulnerability found, skipping the mitigation check"
        exit $NAGIOS_OK
    fi
fi

# check for affected distribution
if [[ $RUNNING_KERNEL =~ ".el5" ]]; then
  MSG="OS (RHEL 5 or derived) is not affected"
fi

if [ -z "$MSG" ]; then
  MITIGATION="cve_2013_2094-0.2-1.el6"
  if rpm --quiet --query $MITIGATION; then
    MSG="mitigation $MITIGATION is installed"
  fi
fi

if [ -z "$MSG" ]; then
  MITIGATION="mitigation_2_6_32_279_22_1"
  if grep -w -q $MITIGATION /proc/modules; then
    MSG="mitigation $MITIGATION is installed"
  fi
fi

if verlte $OK_KERNEL $RUNNING_KERNEL; then
    MSG="kernel $RUNNING_KERNEL is installed"
fi

if [ -z "$MSG" ]; then
  echo "CRITICAL: vulnerable kernel $RUNNING_KERNEL found without mitigation installed"
  exit $NAGIOS_ERROR
else
  echo "OK: $MSG"
  exit $NAGIOS_OK
fi
