Posted on 03-12-2018 06:12 PM
Hello Friends,
When I execute the script no dialog box appears. I am using jamfHelper & Applescript for this.
I think I am missing some important things. Please suggest .
My script is below.
LOGO_PNG=“/Users/navepand/Desktop/Shell_Script/AppIcon.png"
LOGO_ICNS="/Applications/Utilities/Keychain Access.app/Contents/Resources/AppIcon.icns"
PROMPT_TITLE="Keychain Repair"
PROMPT_MESSAGE="We will now repair your login Keychain on this Mac.Click the Next button below, then enter your Mac's password when prompted."
FORGOT_PW_MESSAGE="You made five incorrect password attempts.Please contact the Help Desk at it.amazon.com for help with your Mac password."
SUCCESS_MESSAGE="Thank you! Your Keychain has been repaired."
exec 2>/dev/null
BAIL=false
if [[ ! -f "$LOGO_ICNS" ]]; then
echo "[ERROR] Custom logo icon not present: $LOGO_ICNS"
BAIL=true
fi
if [[ ! -f "$LOGO_PNG" ]]; then
echo "[ERROR] Custom logo PNG not present: $LOGO_PNG"
BAIL=true
fi
LOGO_ICNS="$(osascript -e 'tell application "System Events" to return POSIX file "'"$LOGO_ICNS"'" as text')"
jamfHelper="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
if [[ ! -x "$jamfHelper" ]]; then
echo "[ERROR] jamfHelper not found."
BAIL=true
fi
OS_MAJOR=$(sw_vers -productVersion | awk -F . '{print $1}')
OS_MINOR=$(sw_vers -productVersion | awk -F . '{print $2}')
if [[ "$OS_MAJOR" -ne 10 || "$OS_MINOR" -lt 9 ]]; then
echo "[ERROR] OS version not 10.9+ or OS version unrecognized."
sw_vers -productVersion
BAIL=true
fi
CURRENT_USER="$(stat -f%Su /dev/console)"
USER_ID=$(id -u "$CURRENT_USER")
if [[ "$OS_MAJOR" -eq 10 && "$OS_MINOR" -le 9 ]]; then
L_ID=$(pgrep -x -u "$USER_ID" loginwindow)
L_METHOD="bsexec"
elif [[ "$OS_MAJOR" -eq 10 && "$OS_MINOR" -gt 9 ]]; then
L_ID=USER_ID
L_METHOD="asuser"
fi
echo "Alerting user $CURRENT_USER about incoming password prompt..."
launchctl "$L_METHOD" "$L_ID" "$jamfHelper" -windowType "hud" -icon "$LOGO_PNG" -title "$PROMPT_TITLE" -description "$PROMPT_MESSAGE" -button1 "Next" -defaultButton 1 -startlaunchd &>/dev/null
KEYCHAIN=$(su $CURRENT_USER -c "security list-keychains" | grep login | sed -e 's/"//g' | sed -e 's/// /g' | awk '{print $NF}')
su $CURRENT_USER -c "security delete-keychain $KEYCHAIN"
echo "Prompting $CURRENT_USER for their Mac password..."
USER_PASS="$(launchctl "$L_METHOD" "$L_ID" osascript -e 'display dialog "Please enter the password you use to log in to your Mac:" default answer "" with title "'"${PROMPT_TITLE//"/\"}"'" giving up after 86400 with text buttons {"OK"} default button 1 with hidden answer with icon file "'"${LOGO_ICNS//"/\"}"'"' -e 'return text returned of result')"
TRY=1
until dscl /Search -authonly "$CURRENT_USER" "$USER_PASS" &>/dev/null; do
(( TRY++ ))
echo "Prompting $CURRENT_USER for their Mac password (attempt $TRY)..."
USER_PASS="$(launchctl "$L_METHOD" "$L_ID" osascript -e 'display dialog "Sorry, that password was incorrect. Please try again:" default answer "" with title "'"${PROMPT_TITLE//"/\"}"'" giving up after 86400 with text buttons {"OK"} default button 1 with hidden answer with icon file "'"${LOGO_ICNS//"/\"}"'"' -e 'return text returned of result')"
if (( TRY >= 5 )); then
echo "[ERROR] Password prompt unsuccessful after 5 attempts. Displaying "forgot password" message..."
launchctl "$L_METHOD" "$L_ID" "$jamfHelper" -windowType "utility" -icon "$LOGO_PNG" -title "$PROMPT_TITLE" -description "$FORGOT_PW_MESSAGE" -button1 'OK' -defaultButton 1 -timeout 30 -startlaunchd &>/dev/null &
exit 1
fi
done
echo "Successfully prompted for Mac password."
USER_PASS_XML=$(echo "$USER_PASS" | sed -e 's~&~&~g' -e 's~<~<~g' -e 's~>~>~g' -e 's~"~"~g' -e "s~'~'~g" )
expect <<- DONE set timeout -1 spawn su $CURRENT_USER -c "security create-keychain login.keychain"
# Look for prompt
expect "?chain:"
# Send user-entered password from prompt
send "$USER_PASS_XML
"
expect "?chain:"
send "$USER_PASS_XML
"
expect EOF
DONE
su $CURRENT_USER -c "security default-keychain -s login.keychain"
echo "Displaying "success" message..."
launchctl "$L_METHOD" "$L_ID" "$jamfHelper" -windowType "utility" -icon "$LOGO_PNG" -title "$PROMPT_TITLE" -description "$SUCCESS_MESSAGE" -button1 'OK' -defaultButton 1 -timeout 30 -startlaunchd &>/dev/null &
exit 0