Erst kürzlich hatte ich das Problem, herausfinden zu müssen, ob ein AD-User am Mac kürzlich das Kennwort ändern musste.
Das Setup ist folgendes: Die Clients (10.8.2) sind mit einer ActiveDirectory Domain connected. Von Zeit zu Zeit müssen die User an einem Windows-PC das Kennwort ändern. Wenn sie dann an die Macs zurück kamen kam bisher immer folgende Abfrage:
Die richtige Wahl die die User treffen müssten wäre „Update Keychain Password“. Sie werden dann nach dem alten Kennwort gerfragt. Mit diesem wird dann die Keychain-Datei entschlüsselt und mit dem neuen Kennwort aktualisiert, leider bekommen das die User aber nie hin.
Deshalb die Idee dafür ein Shell-Script zu schrieben. Das Script sollte
- herausfinden, ob die Keychain-Datei ein anderes Kennwort als das aktuelle Userkennwort hat
- falls ja: Nach dem alten Kennwort fragen und die Keychaindatei aktualisieren
- Dies absoult Narrensicher gestalten, dem User keine falsche Wahlmöglichkeit bieten
- Noch ein paar Laufwerke mounten
Dafür muss man schrittweise vorgehen:
1.)Die alte Meldung (Bild siehe oben) deaktivieren
- Terminal öffnen
- „User/Library/Preferences/com.apple.keychainaccess SyncLoginPassword -bool false“
- Achtung, das ist eine Usereinstellung, muss also für jeden User auf dem System manuell gemacht werden
2.)Detektieren ob die Keychain gelockt ist
- Dafür verwenden wir das Kommandozeilenprogramm „security“, mit dem die Keychain in vielerlei hinsicht verwaltet werden kann
- mit „security unlock-keychain /Users/<User>/Library/Keychains/login.keychain„wird die login.keychain entschlüsselt, aslo „geöffnet
- mit dem Parameter -p kann noch das Kennwort angegeben werden, mit dem die Keychain entschlüsselt werden soll, also:
- security unlock-keychain -p „<Passwort>“ /Users/<User>/Library/Keychains/login.keychain“
- Der return-code von unlock-keychain sagt nicht aus, ob das Kennwort richtig war, sondern nur ob die Keychain entschlüsselt wurde oder schon war.
- Das bedeutet also, dass wenn wir uns einloggen und die Keychain entschlüsselt ist, dann wird „unlock-keychain“ einen return-code 0 ergeben auch wenn das Kennwort falsch war.
Hier folgender Shell-code schnippsel der das auswertet:
security unlock-keychain -p "t"
if [ $? -ne 0 ]
then
#Mache etwas hier falls die Keychain gelockt ist
else
#Die Keychain ist nicht gelockt, das Passwort also noch "in Sync"
fi
The next time he logs in into his (AD-bound) Mac, he will be confronted with the dialog posted above. The ugly truth is that most user will choose the wrong decision and not update the password and not even read the message so after that, you’re confronted with a messed up keychain.
I developped a script which will detect if the keychain-password and the user password are out of sync and if so, ask for the new password and update the keychain file. You will find the script at the end of this post.
But first, you have to prepare the system, we have to disable the „System was unable to unlock your login keychain“-dialog.
Via „defaults write“ in the Terminal disable it in the user library:
"User/Library/Preferences/com.apple.keychainaccess SyncLoginPassword -bool false"
Vollständgies Script / Full script
#!/bin/bash
#################################################################
# #
# Keychain pass sync script #
# .___. #
# {o,o} #
# /)__) #
# -"-"- #
# natahori.wordpress.com, 2013 #
# #
#################################################################
################Config-Part################
Config(){
HOME=`cd && pwd`
MYNAME=`whoami`
OLDPASS=''
NEWPASS=''
}
################Passwortabfrage################
get_new_Password(){
NEWPASS=$(osascript<<-EOT
tell application "Finder"
activate
set TMP to text returned of (display dialog "Bitte geben Sie ihr aktuelles Kennwort für den Benutzer $MYNAME ein" with title "Passwortwechsel" default answer "" with icon 0 with hidden answer buttons {"OK"} default button "OK" )
end tell
EOT)
if [ "$NEWPASS" == "" ]
then
echo "Password was empty"
get_new_Password
fi
###############################################
#If you are bound to an AD you can check here if the entered password is correct. For that you have to paste the name of the domain in the section below (without the "<>"!
###############################################
# PWTEST=`dscl /Active\ Directory//All\ Domains authonly $MYNAME $NEWPASS`
# if [ $? -ne 0 ]
# then
# osascript<<-EOT
# tell application "Finder"
# activate
# display dialog "Das Passwort war leider falsch. Bitte das neue Passwort eingeben." with title "Falsches Passwort" buttons {"OK"} default button "OK"
# end tell
# EOT
# echo "Password was wrong"
# get_new_Password
# fi
}
################Passwortabfrage################
get_old_Password(){
OLDPASS=$(osascript<<-EOT
tell application "Finder"
activate
set TMP to text returned of (display dialog "Bitte geben Sie ihr ALTES Kennwort für den Benutzer $MYNAME ein" with title "Passwortwechsel" default answer "" with icon 0 with hidden answer buttons {"OK"} default button "OK" )
end tell
EOT)
}
#####Display warning Dialog###
warning(){
osascript<<-EOT
tell application "Finder"
activate
display dialog "Achtung: Im nächsten Schritt bitte das Kennwort eingeben, welches Sie VOR dem Passwortwechsel benutzt haben" with title "Achtung" with icon 0 buttons {"OK"} default button "OK"
end tell
EOT
}
###check keychain lock###
keychainLock(){
security unlock-keychain -p "t"
echo $?
}
###set Keychain password###
setKeychainPass(){
security unlock-keychain -p $OLDPASS ~/Library/Keychains/login.keychain
if [ $? -ne 0 ]
then
osascript<<-EOT
tell application "Finder"
activate
display dialog "Sorry, das ALTE Kennwort welches Sie eingegeben haben stimmt leider nicht. Bitte geben Sie das ein, mit welchem Sie sich früher eingeloggt haben" with title "Achtung" with icon 0 buttons {"OK"} default button "OK"
end tell
EOT
get_old_Password
setKeychainPass
fi
security set-keychain-password -o $OLDPASS -p $NEWPASS ~/Library/Keychains/login.keychain
security unlock-keychain -p $NEWPASS ~/Library/Keychains/login.keychain
}
###r-part###
detectedR(){
get_new_Password
modify_Keychain
}
################################################################################################
if [ "$1" = "-r" ]
then
detectedR
echo "-r value detected"
fi
RESULT=$( keychainLock )
echo $RESULT
if [ $RESULT -ne 0 ]
then
echo "Was not able to unlock keychain. Get new password"
get_new_Password
warning
get_old_Password
setKeychainPass
else
echo "Password is in sync"
fi
NEWPASS=''
OLDPASS=''
killall Terminal
Ich habe das ganze auch als file angehängt, ist ein bisschen handlicher. Blöderweise gings nur als pdf, sorry
Bitte hinterlasse einen Kommentar wenn du Anregungen hast oder wenn dir mein Script geholfen hat.
Please feel free to leave a comment!
script