# 01-password
# functions:
# ChkPW: check password for quality
# GetPW: prompt user for password
# FinPW: loop to get good password (uses ChkPW & GetPW)
# SetPW: set password in system

#----------------------------------------------------------------------
# check quality of password
# Entry: P1 & P2 = password
# Returns: 0 if p/w is good, !0 otherwise
ChkPW() {
   echo "${P1}"  | cracklib-check >${tempfile} 2>&1
   Pass=$(grep -q ": OK\$" $tempfile && echo "OK" || echo $"fail")
   LEN=$(echo ${#P1})

   # PW too short?
   if [ $LEN -lt 8 ] ; then
      $DIALOG --clear --backtitle "$BT" --title "  Error  " --msgbox \
         "\n Password length $LEN too short.\n\n Minimum of 8 characters required." $H1 $W1
      log "Password length $LEN too short."

   # two PWs don't match?
   elif [ "x$P1" != "x$P2" ] ; then
      $DIALOG --clear --backtitle "$BT" --title "  Error  " --msgbox \
         "\n Two passwords do not match." $H1 $W1
      log "Two passwords do not match."

   # PW based on dictionary word?
   elif [ "x$Pass" != "xOK" ] ; then
      $DIALOG --clear --backtitle "$BT" --title "  Error  " --msgbox \
         "\n Password is based on dictionary word." $H1 $W1
      log "Password is based on dictionary word."

   # all tests pass, its good
   else
      return 0
   fi

   # failed a test, its bad
   return 1
} # ChkPW

#----------------------------------------------------------------------
# prompt user for password (two times)
# Entry: DefPW default p/w used if timeout, 
#        PWUsr username for this password
#        temfile used to capture input
# Exit: P1 & P2 set to password
GetPW() {
      P1=""
      P2=""
      MANUAL_BACK=0

      while true ; do
         : > "$tempfile"
         # enter password
         $DIALOG \
           --ok-label "Next" \
                --extra-button \
                --extra-label "Back" \
           --clear \
           --visit-items \
           --backtitle "$BT" \
           --title " Create $PWusr Password " \
           --insecure \
           --nocancel \
           --passwordform "\n\
 Password should be difficult to guess.\n\
 Password must be minimum 8 characters.\n\
    Use Arrows to switch fields. Press Tab to move to Next/Back.\n\n\
 Enter $PWusr password:" \
           $H1 $W1 2 \
           "Password:" 1 1 ""   1 20 16 15 \
           "Reenter :" 2 1 ""   2 20 16 15 \
           2>"$tempfile"
         k=$?

             # manual-firstboot back to previous screen
             if [ "$k" = "3" ] ; then
                  MANUAL_BACK=1
                  return 1
             fi

         # timeout -> use default password
         if [ "$k" = "255" ] || grep -iq timeout "$tempfile" ; then
            P1="$DefPW"
            P2="$DefPW"
            return 0
         fi

         # fallback non-zero -> use default password
         if [ "$k" -ne 0 ]; then
            P1="$DefPW"
            P2="$DefPW"
            return 0
         fi

         P1=$(head -1 "$tempfile")
         P2=$(tail -1 "$tempfile")
         return 0
      done
} # GetPW

#----------------------------------------------------------------------
# set password in system
# Entry: P1 is password to set
#        PWusr is user to be set with this password
# Returns: 0 if success
SetPW() {
   echo "$P1" | /bin/passwd --stdin $PWusr >/tmp/e1 2>/tmp/e2
   if [ $? -ne 0 ] ; then
      /bin/passwd --lock $PWusr
      show "   Failed to set the $PWusr password"
      show "   $(cat /tmp/e2)"
      sleep .1
      return 1
   else
      log "   set the $PWusr password successfully"
      log "   $(cat /tmp/e1)"
   fi
   rm -f /tmp/{e1,e2}
} # SetPW

#----------------------------------------------------------------------

