1833704 Members
3197 Online
110062 Solutions
New Discussion

Script explanation

 
Ajin_1
Valued Contributor

Script explanation

What is the explanation of the below script.

 

#======= patrol user profile =======
LANG=C; export LANG
MAIL=/usr/mail/${LOGNAME:?}
tty -s
if [ $? -eq 0 ]
then
   if [ `logname` != "root" ]
   then
      exit 1
   fi
fi

Thanks & Regards
Ajin.S
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
4 REPLIES 4
Patrick Wallek
Honored Contributor

Re: Script explanation

If the return code of the 'tty -s' command is 0, which means you are logged in with a terminal session and NOT running via cron, and you are NOT logged in as root then the script will exit with a return code of 1.

 

 

Matti_Kurkela
Honored Contributor

Re: Script explanation

# Set the locale variable LANG to C and export it
# (locale C means "classic unix processing: no particular localization,
# all error messages in US English and character set US-ASCII only)
LANG=C; export LANG

# If the environment variable LOGNAME contains a value, set
# variable MAIL to /usr/mail/<value of LOGNAME>. 
# If LOGNAME is not set, exit.
MAIL=/usr/mail/${LOGNAME:?}

# Test silently if the script is associated with a terminal.
# If not (= the script is run non-interactively as a cron job or
# some other background process, or its standard input has been
# redirected), the result code $? will be set to
# a non-zero value.
tty -s

# Test the result code of the previous command. If it was zero,
# run the commands between the outermost "then" and the outermost "fi".
if [ $? -eq 0 ]
then

   # This will be executed only if the result code from the "tty -s" was
   # zero, i.e. the script is running with a terminal. 
   # Run the command "logname" (which returns the current username)
   # and examine it's output.
   # If the output is something other than "root", run the commands
   # between the inner "then" and inner "fi".
   if [ `logname` != "root" ]
   then

      # This will be executed only if the script is running with a
      # terminal and the current username was not "root".
      # This command will exit the script with result code 1.
      exit 1
   fi
fi

 

The first two lines are primarily for setting up some environment settings, and the rest seems to be trying to

stop the script if it is not run by the root user.

 

If this script has been named as ".profile" in the patrol user's home directory, this would prevent logging in as the "patrol" user, but would allow root to use the "su", "sudo" or similar commands to switch to "patrol" user. If someone tried to log in as user "patrol", the session would immediately end.

MK
Dennis Handly
Acclaimed Contributor

Re: Script explanation

>MAIL=/usr/mail/${LOGNAME:?}

 

Hmm, I would have thought it should be /var/mail/.

 

You can optimize the two "if"s to:

if [ $? -eq 0 -a $(logname) != "root" ];  then
   exit 1

fi

Patrick Wallek
Honored Contributor

Re: Script explanation

>>>MAIL=/usr/mail/${LOGNAME:?}

 

>>Hmm, I would have thought it should be /var/mail/.

 

/usr/mail is a symbolic link (transition link) to /var/mail.

 

root /usr # ls -ld mail
lrwxrwxrwt   1 root       sys              9 Jun 30  2011 mail -> /var/mail