#!/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

mem_total=$(free -g | grep 'Mem:' | awk '{print $2}')
if [ $mem_total -lt 32 ]; then
    echo "Systems with less than 32GB of memory should be safe"
    exit $NAGIOS_OK
fi

stack_size=$(ulimit -Hs)
if [ -z "$stack_size" -o "$stack_size" = "unlimited" ]; then
    echo "No stack size limits are specified"
    exit $NAGIOS_ERROR
fi

if [ "$stack_size" -gt 16000000 ]; then
    echo "The stack size isn't sufficently limited"
    exit $NAGIOS_ERROR
fi

echo "The stack size is limited to prevent exploiting"
exit $NAGIOS_OK
