# /usr/local/bin/chk-passwword-expire.sh
#!/bin/ksh
#--------------------------------------
# chk-psw-expire.sh shellscript/kornshell
# Description:
# @(#) check passwd expiration of user(s) USER in group GROUP
#      if exp.date < DIFFMIN send mail to 
#      1. USER if gecos contains email address x.y@domain 
#      2. MAILDEF if gecos of USER has no email address data 
# Version: 1.0.3
#-----------------------------------------------------------------------------+
# Changes:
# date     uid     reason
#-----------------------------------------------------------------------------+
# Naming conventions
# IOs:
# 1. Input
# 1.1 trace lvl: active if <> ""
# 2. Output
# 2.1 temp file /tmp/$$_<USER> which contain lines 1-4 of 'chage' cmd 
# 
TRACE=
#if [ "$1" = "" ];then set -x;TRACE=ON;fi
if [ "$1" = "ON" ];then TRACE=ON;else if [ ! "$1" = "" ];then set -x;fi;fi
# Defaults:
let DIFFMIN=7                               # password warning days
GROUP=group                              # password warning for group $GROUP
MAILDEF=your-email@server.com         # default mail addr
#MAILDEF=CQR@mail.awl.fr.atosorigin.com      # default mail addr
USERS=`grep $GROUP /etc/group|cut -d: -f4`  # extract USERS for GROUP
typeset -Z2 USEREXPM                        # numeric month
#typeset -Z8 TODAY=`date '+%Y%m%d'`          # current date 'yyyymmdd'
typeset -Z10 TODAY=`date '+%Y-%m-%d'`       # current date 'yyyy-mm-dd'
let I=1                                     # counter for USERS in GROUP
let N=0                                     # counter for USERS never expires  
USER=`echo $USERS|cut -d, -f$I`             # init USER (first user in USERS
# loop for all USER
until [ "$USER" = "" ]                      # 
do                                          #  extract Password expires date
 USEREXP=`/usr/bin/chage -l $USER 2>/dev/null|grep "Password expires"`
# => Password expires : May 01, 2015
 echo $USEREXP| grep never 1>/dev/null 2>&1 # check for USERS never expires
 if (( $? == 0 ))                           # YES = 'never expire'?
 then                                       #  incr counter 'never expire' 
  let N=$N+1
 else                                       # NO: create EXPIRE date yyyymmdd
  USEREXPY=`echo $USEREXP|cut -d" " -f6`    #  expir. year
  USEREXPD=`echo $USEREXP|cut -d" " -f5|cut -d, -f1` # expir. day
  USEREXPMC=`echo $USEREXP|cut -d" " -f4`   # expir. month (character) 
  case "$USEREXPMC" in                      #  repl month (char => num)
   Jan ) USEREXPM=01;;
   Feb ) USEREXPM=02;;
   Mar ) USEREXPM=03;;
   Apr ) USEREXPM=04;;
   May ) USEREXPM=05;;
   Jun ) USEREXPM=06;;
   Jul ) USEREXPM=07;;
   Aug ) USEREXPM=08;;
   Sep ) USEREXPM=09;;
   Oct ) USEREXPM=10;;
   Nov ) USEREXPM=11;;
   Dec ) USEREXPM=12;;
   *) USEREXPM=00;;
  esac
  EXPIRE=$USEREXPY"-"$USEREXPM"-"$USEREXPD # cre full expir.date yyyy-mm-dd
#                                          # calc difference in seconds 
  SECDIFF=$(($(date -d "$EXPIRE" +%s) - $(date -d "$TODAY" +%s) ))
  let DIFF=$SECDIFF/3600/24                # calc seconds to days
# echo USER=$USER EXPIRE=$EXPIRE DIFF=$DIFF
  if (( $DIFF <= $DIFFMIN && $DIFF > 0 ))  # chk expir. days < DIFFMIN + >0
  then                                     # YES: 
   /usr/bin/chage -l $USER| head -n4 > /tmp/$$-$USER    # cre chage temp file
   USERRECORD=`cat /etc/passwd| grep $USER|cut -d: -f5` # extract email addr
   echo $USERRECORD| grep @ 1>/dev/null 2>&1            # from gecos, chk for @
   if (( $? == 0 ))                                     # domain delim. found? 
    then                                                # YES:
    ADDRESS=`echo $USERRECORD|cut -d@ -f1`              # extr email addr
    LASTADDR=`echo $ADDRESS|wc -w`                      #  extr last word before
    ADDRESS=`echo $ADDRESS|cut -d" " -f$LASTADDR`       #  @ sign
    DOMAIN=`echo $USERRECORD|cut -d@ -f2|cut -d" " -f1` #  extr domain
    MAILTO=$ADDRESS"@"$DOMAIN                           #  set mail addr
    echo MAIL to USER $USER bec password expires in $DIFF days
    MAILSBJ="password of your user $USER expires in $DIFF days on $HOSTNAME"
    mail -s "$MAILSBJ" $MAILTO < /tmp/$$-$USER
   else
    echo MAIL to $MAILDEF bec. USER $USER expires in $DIFF days
    MAILSBJ="password of $USER expires in $DIFF days on $HOSTNAME" 
    mail -s "$MAILSBJ" $MAILDEF < /tmp/$$-$USER
   fi 
   rm -rf /tmp/$$-$USER
  fi
 fi
 let I=$I+1
 USER=`echo $USERS|cut -d, -f$I`
done
 let I=$I-1
 echo Users: $I - NEVER: $N
exit
