Cover V03, I05
Article
Listing 1
Listing 2
Listing 3
Listing 4

sep94.tar


Listing 3: oldacct script

#!/bin/sh
#
#       oldacct
#
#       Identify accounts not used for over 90 days.
#
#       Copyright 1994, Lawrence S Reznick
#
#  94Apr19      LSR
#       Finger doesn't always show "On since" when the
#       user is still logged in.  If the user has been
#       idle, finger shows the minutes & seconds idle.
#       Added a test for "Idle" to catch that.  Also
#       found one acct that had a home phone field.
#       Finger outputs that on a separate line, pushing
#       the "Last logged in" line to the 5th line.  The
#       word "Directory:" appears on the 4th line when
#       that happens.  Added a test to handle that.
#       Finally, changed the "Never logged in" code to
#       collect the names.  After the loop finishes, the
#       names are output in a columnar list.  Looks
#       cleaner that way.

PW_FILE=/etc/passwd                     # Point to passwd file

LOWUID=200                              # Lowest non-admin UID

monthnum ()
{
Jan=1 Feb=2 Mar=3 Apr=4 May=5 Jun=6
Jul=7 Aug=8 Sep=9 Oct=10 Nov=11 Dec=12

echo `eval echo $"$1"`          # Show month's number
}

EXPMONTH=`date "+%m"`                   # Get current date
EXPDAY=`date "+%d"`
EXPYEAR=`date "+%Y"`

CURRMONTH=$EXPMONTH
CURRYEAR=$EXPYEAR

if [ $EXPMONTH -le 3 ]
then
EXPMONTH=`expr $EXPMONTH + 9`   # Wrap around year
EXPMONTH=`expr $EXPYEAR - 1`
else
EXPMONTH=`expr $EXPMONTH - 3`
fi

if [ $EXPMONTH -eq 2 -a $EXPDAY > 28 ]
then
EXPDAY=28                      # Force Feb 28
fi

#
# Turn month number into that month's name
#

#Months="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"

#set $MONTHS                            # Create associative array
#EXPMONTH=`eval echo $"$EXPMONTH"`      # Select month's name

#
# Collect all non-administrative users' names
#

USERS=`
awk '
BEGIN { FS = ":" }
$3 >= LOWUID { print $1 }
' LOWUID=$LOWUID $PW_FILE |
sort`

#
# Find last login time for each user
#

for u in $USERS
do
#       echo "$u\t\t\r\c"

LAST=`finger -m $u 2>/dev/null | sed -n '4p'`
set $LAST                       # Parse last login line

while [ $# -lt 5 ]              # Handle special message
do
if [ "$LAST" = "Never logged in." ]
then
#                       echo $u"\t"$LAST
NOLOGIN="$NOLOGIN $u"
break
fi

# Special case when home phone is in GECOS field

if [ "$1" = "Directory:" ]      # Phone pushed all 1 line down
then
LAST=`finger -m $u 2>/dev/null | sed -n '5p'`
set $LAST
continue
fi

# Special case when phone number isn't in GECOS field

LAST=`finger -m $u 2>/dev/null | sed -n '3p'`
set $LAST
done

if [ $# -lt 4 ]                         # Never logged in
then
continue
fi

if [ "$1" - "On" -a "$2" - "since" ]    # Still logged in
then
continue                        # Don't tell anyone
fi

if [ "$5" = "Idle" ]
# Still logged in
then
continue                        # Keep going
fi

OLDMONTH=$4
OLDDAY=$5
OLDYEAR=$6

# If last login was within 6 months,
# year will be an hh:mm time

if [ $OLDYEAR -gt 999 ] # It must be >= 6 months old
then
echo "$u"\t"$OLDMONTH $OLDDAY $OLDYEAR
continue
fi

if [ `monthnum $OLDMONTH` -lt $EXPMONTH -o \
`monthnum $OLDMONTH` -gt $CURRMONTH ]
then
echo "$u"\t"$OLDMONTH $OLDDAY $OLDYEAR
continue
fi

if [ `monthnum $OLDMONTH` -eq $EXPMONTH -a \
$OLDDAY -lt $EXPDAY ]
then
echo "$u"\t"$OLDMONTH $OLDDAY $OLDYEAR
continue
fi
done

if [ -n "$NOLOGIN" ]
then
echo "Never logged in:"
echo $NOLOGIN | tr ' ' '\012' | pr -t -6
fi