#!/bin/bash

# -*- coding: utf-8 -*-


# This script is for grading the sudo portion of rh134 lab 2.

# It does the following:
# * Checks if the listed users can successfully run the specified
#   commands with sudo.


TEST_USERS=( morris borris horace )

TEST_COMMANDS[1]="service httpd status" 
TEST_COMMANDS[2]="service httpd start"
TEST_COMMANDS[3]="service httpd stop"
TEST_COMMANDS[4]="service httpd restart"

if [ "$(/usr/bin/id -u)" -gt "0" ] ; then
    echo 'This script must be run as root!'
    exit 2
fi

for user in ${TEST_USERS[@]} ; do

    for i in $(seq 1 ${#TEST_COMMANDS[@]}) ; do
        if /usr/bin/sudo -U $user -l ${TEST_COMMANDS[${i}]} &>/dev/null ; then
            continue
        else
            echo "The user $user is not able to run:"
            echo "sudo ${TEST_COMMANDS[${i}]}"
            echo
            echo "Please check your configuration and try again."
            exit 1
        fi
    done

done

echo 'Success: sudo is configured correctly!'

exit 0
