#!/bin/sh
#@(#) user_log.sh  v1.0 - Script to check status on users - Rikard Skjelsvik
#
# ********************************************************************
# 
# File:         	user_log.sh
# Author:       	Rikard Skjelsvik
# Created:      	12 mars 2001
# Modified:    
# Version:		1.0
#
# Acknowledgement:	Shellskeleton from the "do_backup" script
#			by Eric Nielsen found at BigAdmin
#			Applied a lot of tips &
#			tricks from Heiner's Shelldorado at 
#			http://www.oase-shareware.org/shell.
#			Thanks to all those at sunmanagers 
#			mailing list who helped. Too numerous
#			to mention. You know who you are!
#
# Description:  	This script checks the status of users on a 
#               	system. It will get the users from the passwd
#			file/passwd-map and check the users with the 
#			lastcommand and check the loginfiles in the  
#			home directories
#
# Bugs/known problems:	This script will probably not work if the 
#			home directories are remotly mounted
#
#
# Disclaimer:
#
# Please note that i am not a programmer and that the program may 
# not be as good or pretty as could be. So please do not bother 
# to complain , unless your critisim is
# constructive.(That is, do it better yourself) 
#
# This code worked for me, it may not work as intended for you.
# I will not and can not warrant this script. It may cause harm.
# It may wreak havoc. You, that
# download this script or use it, must test it beforehand, possibly fix it. 
#
# It is a work in progress. If you make changes that make it
#  safer or better and want to share your doings with me - i 
# would be happy to hear from you. Please note
# that my time is limited. I might not be able to respond as fast as you hope. 
#
#
# ********************************************************************
#
# ********************************************************************
#
#       Check to be sure user is root
#
# ********************************************************************
       
        RUN_USER=`/bin/id | grep root | wc -l`
        if [ $RUN_USER -eq 0 ]
        then
                echo ""
                echo "You must be root to run this script"
                echo ""
                exit 1
         fi


# ********************************************************************
#
#       Argument Processing
#
# ********************************************************************

PROG=$0
USAGE="Usage: $PROG [-n|-p]"
if [ $# -eq 0 ] ; then
	echo $USAGE;exit 1
fi
while [ $# -gt 0 ]
	do
	case $1 in
		-p)	OUT_CMD=/usr/bin/cat;PASS_DOMAIN=/etc/passwd;;
		-n)	OUT_CMD=/usr/bin/ypcat;PASS_DOMAIN=passwd;;
		-*)     echo $USAGE;exit 1;;
		/?)	echo $USAGE;exit 1;;
	
	esac
	shift
done
  
   
# ********************************************************************
#
#       Create header
#
# ********************************************************************
echo " "
echo Userlogins
echo " "
echo $PROG run on `uname -n` at `date`
echo " "
echo Using command : $OUT_CMD with $PASS_DOMAIN
echo " "
echo "User		Lastlog		Finger		Shell		~ Initialization"	
echo "								(.profile/.login)"
echo "								~ Startupfile"
echo "								(.[c|k|bas]hrc)"
echo "=========================================================================="       



# ********************************************************************
#
#       Start loop for each user 
#
# ********************************************************************

for USER in `$OUT_CMD $PASS_DOMAIN | grep -v "^#" |cut -d: -f1`
do

# ********************************************************************
#
#       Get information from the last command
#
# ********************************************************************


last -1 $USER |egrep -v "^$|wtmp"| grep $USER > /dev/null
if [ $? -eq 0 ] ; then
	L_MONTH=`last -1 $USER |egrep -v "^$|wtmp" | awk '{print $5}'`
	L_DAY=`last -1 $USER   |egrep -v "^$|wtmp" | awk '{print $6}'`
	L_TIME=`last -1 $USER  |egrep -v "^$|wtmp" | awk '{print $7}'`
	LAST_TIME="$L_DAY $L_MONTH $L_TIME"
else
	LAST_TIME="not in last"
fi

# ********************************************************************
#
#       Get information from the finger command
#
# ********************************************************************


F_WORD=`finger $USER | head -3 | grep -v "^Login name:" | grep -v "^Directory:" | awk '{print $1}'`
case $F_WORD in
	Last)	CHECK_L=yes;CHECK_O=no;;
	On)	CHECK_L=no;CHECK_O=yes;;
	*)	CHECK_L=no;CHECK_O=no;;
esac


if [ $CHECK_L = yes -a $CHECK_O = no ] ; then
	
	F_DAY=`finger $USER | grep "^Last" | head -1 | awk '{print $5}'`
	F_MONTH=`finger $USER | grep "^Last" | head -1 | awk '{print $4}'`
	F_TIME=`finger $USER | grep "^Last" | head -1 | awk '{print $6}'`
	F_STATUS="$F_DAY $F_MONTH $F_TIME"
	F_DESC="(Last login)"
fi

if [ $CHECK_L = no -a $CHECK_O = yes ] ; then
 
	F_DAY=`finger $USER | grep "^On" | head -1 | awk '{print $4}'`
	F_MONTH=`finger $USER | grep "^On" | head -1 | awk '{print $3}'`
	F_TIME_H=`finger $USER | grep "^On" | head -1 | awk '{print $5}'| awk -F: '{print $1}'`
	F_TIME_M=`finger $USER | grep "^On" | head -1 | awk '{print $5}'| awk -F: '{print $2}'`
	F_STATUS="$F_DAY $F_MONTH $F_TIME_H:$F_TIME_M"
	F_DESC="(On since)"
fi

if [ $CHECK_L = no -a $CHECK_O = no ] ; then
 	
	F_STATUS="Finger: no"
	F_DESC="(Never)"
fi


# ********************************************************************
#
#       Get atime information from the users login files
#
# ********************************************************************

CHECK_I=yes
CHECK_S=yes
HOME_DIR=`$OUT_CMD $PASS_DOMAIN |grep $USER  | cut -d: -f6`
USER_SHELL=`$OUT_CMD $PASS_DOMAIN |grep "^$USER" | cut -d: -f7|grep -v "^$"`
case $USER_SHELL in
	*/sh) 	 	I_FILE=.profile;S_FILE="file";CHECK_S=no;USER_SHELL=sh;;
	*/ksh)	 	I_FILE=.profile;S_FILE=.kshrc;USER_SHELL=ksh;;
	*/csh)	 	I_FILE=.login;S_FILE=.cshrc;USER_SHELL=csh;;
	*/tcsh)		I_FILE=.login;S_FILE=.cshrc;USER_SHELL=tcsh;;
	*/bash)		I_FILE=.bashprofile;S_FILE=.bashrc;USER_SHELL=bash;;
	*)		I_FILE="file";S_FILE="file";USER_SHELL="no sh";CHECK_I=no;CHECK_S=no;;
esac

if [ $CHECK_I = yes -a -f $HOME_DIR/$I_FILE ] ; then
	I_MONTH=`ls -lu $HOME_DIR/$I_FILE | awk '{print $6}'`
	I_DAY=`ls -lu $HOME_DIR/$I_FILE   | awk '{print $7}'`
	I_TIME=`ls -lu $HOME_DIR/$I_FILE  | awk '{print $8}'`
	I_STATUS="$I_DAY $I_MONTH $I_TIME"
else
	I_STATUS="atime N/A"
	I_FILE="no $I_FILE"
fi

if [ $CHECK_S = yes -a -f $HOME_DIR/$S_FILE ] ; then
	S_MONTH=`ls -lu $HOME_DIR/$S_FILE | awk '{print $6}'`
	S_DAY=`ls -lu $HOME_DIR/$S_FILE   | awk '{print $7}'`
	S_TIME=`ls -lu $HOME_DIR/$S_FILE  | awk '{print $8}'`
	S_STATUS="$S_DAY $S_MONTH $S_TIME"
else
	S_STATUS="atime N/A"
	S_FILE="no $S_FILE"
fi



# ********************************************************************
#
#       Output our findings
#
# ********************************************************************

echo "$USER       	$LAST_TIME	$F_STATUS	$USER_SHELL	   $I_FILE: $I_STATUS"
echo "				$F_DESC			$S_FILE: $S_STATUS"
echo "--------------------------------------------------------------------------"
done


# ********************************************************************
#
#       Create a footer
#
# ******************************************************************** 

NR_USERS=`$OUT_CMD $PASS_DOMAIN |grep -v "^#" |wc -l`
echo " "
echo "==========================================================================" 
echo "Number of users on `uname -n` in $PASS_DOMAIN is :		$NR_USERS"
