#!/bin/bash

NAGIOS_OK=0
NAGIOS_ERROR=2

PAKITI_RESULT="pakiti_results"

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

lsmod | grep -q cve_2016_5195
if [ $? -eq 0 ]; then
	echo "CERN module detected (supposedly with systemtap mitigations)"
	exit $NAGIOS_OK
fi

lsmod | grep -q nomadvise
if [ $? -eq 0 ]; then
	echo "CESNET module detected (supposedly blocking madvise(2))"
	exit $NAGIOS_OK
fi

lsmod | grep -q '^m '
if [ $? -eq 0 ]; then
	echo "CESNET module detected (supposedly blocking madvise(2))"
	exit $NAGIOS_OK
fi

lsmod | grep -q '^stap_'
if [ $? -eq 0 ]; then
	echo "A stap module detected (presumably implementing systemtap mitigations)"
	exit $NAGIOS_OK
fi

echo "No known mitigation of CVE-2016-5195 detected"
exit $NAGIOS_ERROR
