LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Blogs > camh
User Name
Password

Notices



Rate this Entry

Require Password Change script v2.

Posted 12-16-2008 at 07:11 PM by camh
Updated 12-16-2008 at 07:13 PM by camh (changed title to be more accurate)

Here is the second version of my required password change script. Added automated salt and password generation functionality, and removed the use of temp files.

If anyone knows how this can be optimized or improved, please comment. I'm still learning bash scripting and any help is appreciated.

Code:
#!/bin/bash

#Defines

SALT_CHARS="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
SALT_LENGTH="2"
PW_CHARS="1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()."
PW_LENGTH="8"

#Checks

if [ "$(whoami)" != 'root' ]; then
   echo "This program must be run as root"
   exit 1;
 elif [ -z $1 ]; then
    echo "Usage: pwexpire <username>"
    exit 1;
 elif [ ! `grep $1 /etc/passwd` ] ; then
    echo "$1 not found. Please ensure the user account exists."
    exit 1;
 elif [ ! -f /usr/bin/python ] ; then 
    echo "Python not found in /usr/bin!"
    exit 1;
fi

#Salt Generation
#Uses a numeric value if CHOICE returns 1; characters if 0
#since python's crypt function will only take one or the other (AFAIK)

CHOICE=`expr $RANDOM % 2`
if [ "$CHOICE" = 0 ]; then
   while [ "${n:=1}" -le "$SALT_LENGTH" ]; do
      NUMBER=$RANDOM
      let "NUMBER %= 26"
      if [ $NUMBER != 0 ]; then
         SALT=$SALT$(echo $SALT_CHARS | cut -c $NUMBER)
         let n+=1
      fi
   done
else
   NUMBER=$RANDOM
   let "NUMBER %= 99"
      if [[ $NUMBER < 10 ]]; then
         NUMBER=$NUMBER$(expr $NUMBER + $RANDOM % 10)
      fi
   SALT=$NUMBER
fi

#Password handling

echo -e "Generate random password? (Y/n) \c"
read YESNO

case "$YESNO" in
 "Y" | "y" | "")
 echo "Generating $PW_LENGTH character password..."
 n=1  #re-initialize
 while [ "$n" -le "$PW_LENGTH" ]; do
    NUMBER=$RANDOM
    let "NUMBER %= ${#PW_CHARS}"
    if [ $NUMBER != 0 ]; then
       PASS=$PASS$(echo $PW_CHARS | cut -c $NUMBER)
       let n+=1
    fi
 done
 ;;

 "N" | "n")
 echo -e "Please enter the temporary password: \c "
 read PASS
 ;;

 *) echo "Exiting."
 exit 0;
 ;;
esac

#Account Maintenenace

PWHASH=`/usr/bin/python -c "import crypt; print crypt.crypt(\"$PASS\",\"$SALT\")"` 

if [ $PWHASH ]; then
   usermod -L $1 && chage -d 0 $1 && usermod -p "$PWHASH" $1
   echo "Operation successful." 
   echo -e "User account \"$1\" will require a password change on next login\n"
   echo -e "Please record the following details:\n"
   echo "---"
   echo "Account name: $1"
   echo "Temporary password: $PASS"
   echo "---" 
   exit 0;
 else echo "An error has occured. Please re-run this program"
    exit 1;
fi
Posted in Uncategorized
Views 989 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 05:25 PM.

Main Menu
Advertisement

Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration