#!/bin/bash
####################################################################################################################
# Novell Inc.
# 1800 South Novell Place
# Provo, UT 84606-6194
# Script Name:		delete_loginExpirationTime.sh
# Description:		This will generate an ldif to remove the loginExpirationTime greater than current date from all users in the tree.  It will prompt for a user name.
#  			                              
# %Version:	        1.0
# %Creating Date:	Fri Aug 15 11:29:21 MDT 2014
# %Created by:		Rance Burker - Novell Technical Services

####################################################################################################################
defaultnamingcontext=`/usr/bin/ldapsearch -x -b "" -s base defaultnamingcontext | grep -i 'defaultnamingcontext: ' | awk '{print $2}'`
export LDAPCONF=/etc/opt/novell/xad/openldap/ldap.conf 

LOCATION=/tmp
TODAY=`date "+%Y%m%d%H%M%S" -u`Z

/usr/bin/ldapsearch -Y EXTERNAL -LLL -Q -b "$defaultnamingcontext" "(&(objectclass=user)(loginExpirationTime>=$TODAY)(!(|(objectClass=Computer)(cn:dn:=computers)(cn:dn:=users)(ou:dn:=oessystemobjects))))" dn | sed -e :a -e '$!N;s/\n //;ta' -e 'P;D'|sed -e'/^dn/ a\changetype: modify\ndelete: loginExpirationTime' |grep -v ^# >$LOCATION/delete_loginExpirationTime.ldif


cat $LOCATION/delete_loginExpirationTime.ldif
TIMELIMIT=60
echo -e "Returned is a list of users with a loginExpirationTime greater than $TODAY"
echo -ne "Do you wish to apply loginExpirationTime to the following objects? (y/n): " #yes not to continue
        read -t $TIMELIMIT REPLY  # set timelimit on REPLY
        echo
        if [ -z "$REPLY" ]; then   # if REPLY is null then
            echo Did not add loginExpirationTime to users
            echo -e  "$(basename $0) timed out"
			echo -e "ldif is located at $LOCATION/delete_loginExpirationTime.ldif"
            exit 0
        elif [[ ! $REPLY =~ ^[Yy]$ ]]; then
            echo Did not add loginExpirationTime to users
            echo -e  "$(basename $0) finished"
			echo -e "ldif is located at $LOCATION/delete_loginExpirationTime.ldif"
			echo -e "use ldapmodify to apply the ldif after editing"
            exit 0
        else
            echo "`date +"%b %d %Y %H:%m:%S"` Starting the setting of the loginExpirationTime attribute ..."
            echo "Executing $LOCATION/delete_loginExpirationTime.ldif ldif file ..." 
            ldapmodify -Y EXTERNAL -f $LOCATION/delete_loginExpirationTime.ldif 2>&1 
            echo
			rm $LOCATION/delete_loginExpirationTime.ldif
        fi

#unset $LDAPCONF

exit 0
