#!/usr/bin/sh
# Скрипт настройки /etc/sudoers для пользователя (Script for configuring /etc/sudoers for user)
# Автор - Логинов Алексей <loginov.alex.valer@gmail.com> (Author - Loginov Alexey <loginov.alex.valer@gmail.com>)
# Скрипт распространяется по лицензии GPL v.3 (Licence GPL v.3)

export TEXTDOMAIN="xsudo"
export TEXTDOMAINDIR="/usr/share/locale"
. gettext.sh

USER0=$1
SCRIPT=$2

MES0="$(eval_gettext 'Do you want to to deny the user $USER0 to run applications via sudo/xsudo without entering the administrator password?')"
MES1="$(eval_gettext 'Do you want to allow the user $USER0 to run applications via sudo/xsudo without entering the administrator password?')"
MES2="$(eval_gettext 'User $USER0 was added to /etc/sudoers, and will from now on be prompted for their password when using sudo or xsudo.')"
MES3="$(eval_gettext 'User $USER0 was removed from /etc/sudoers.')"
MES4="$(eval_gettext 'User $USER0 is already in /etc/sudoers, addition is not required.')"
MES5="$(eval_gettext 'User $USER0 is not in /etc/sudoers, removal is not required.')"
MES6="$(gettext 'It is not necessary to configure /etc/sudoers for root.')"
MES7="$(gettext 'You must be root.')"
MES8="$(gettext 'Requires a username in the first parameter.')"
MES9="$(eval_gettext 'User $USER0 does not exist.')"
MES10="$(gettext 'The second parameter is required to specify addition or removal: add2sudoers or rmfromsudoers.')"
MES11="$(gettext 'Choose whether to require password or not')"
MES12="$(gettext 'Choose either authentication with a password: PASSWD (recommended) or without: NOPASSWD (too dangerous):')"
MES13="$(gettext 'Your choice')"
MES14="$(gettext 'The choices')"
MES15="$(gettext 'Further configuration was interrupted')"
MES16="$(eval_gettext 'User $USER0 was added to /etc/sudoers, and will not from now on be prompted for their password when using sudo or xsudo.')"

if [ ! "$USER" = "root" ]
then
  zenity --info --text "$MES7"
  exit 0
fi

if [ -z "$USER0" ]
then
  zenity --info --text "$MES8"
  exit 0
fi

if [ ! -d /home/$USER0 ]
then
  zenity --info --text "$MES9"
  exit 0
fi

if [ "$USER0" = "root" ]
then
  zenity --info --text "$MES6"
  exit 0
fi

if [ ! "$SCRIPT" = "add2sudoers" ] && [ ! "$SCRIPT" = "rmfromsudoers" ]
then
  zenity --info --text "$MES10"
  exit 0
fi

TESTROOT=`cat /etc/sudoers|grep "$USER0 ALL=(ALL)"|grep "PASSWD:"`

if [ "$SCRIPT" = "add2sudoers" ]
then
    if [ ! "$SCRIPT" = "rmfromsudoers" ]
    then
	if [ -z "$TESTROOT" ]
	then
	  zenity --question --text "$MES1"
	  if [ "$?" = "1" ]
	  then
	    exit 0
	  else
	    A=`zenity --list --radiolist --title="$MES11" --text="$MES12" --column="$MES13" --column="$MES14" TRUE PASSWD FALSE NOPASSWD`
	    if [ ! "$A" = "PASSWD" ]
	    then
	      if [ ! "$A" = "NOPASSWD" ]
	      then
	        zenity --info --text "$MES15"
	        exit 0
	      fi
	    fi
	    if [ "$A" = "PASSWD" ]
	    then
	       echo "$USER0 ALL=(ALL) PASSWD: ALL" >> /etc/sudoers
	       zenity --info --text "$MES2"
	    else
	       echo "$USER0 ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
	       zenity --info --text "$MES16"
	    fi
	  fi
	else
	  zenity --info --text "$MES4"
	fi
    fi
fi
 
if [ "$SCRIPT" = "rmfromsudoers" ]
then
    if [ ! "$SCRIPT" = "add2sudoers" ]
    then
	if [ ! -z "$TESTROOT" ]
	then
	  zenity --question --text "$MES0"
	  if [ "$?" = "1" ]
	  then
	    exit 0
	  else
	    sed "/$USER0 ALL=(ALL) PASSWD: ALL/d" /etc/sudoers > /tmp/sudoers
	    mv -f /tmp/sudoers /etc/sudoers
	    sed "/$USER0 ALL=(ALL) NOPASSWD: ALL/d" /etc/sudoers > /tmp/sudoers
	    mv -f /tmp/sudoers /etc/sudoers
	    chmod 0440 /etc/sudoers
            zenity --info --text "$MES3"
	  fi
	else
	  zenity --info --text "$MES5"
	fi 
    fi
fi
