Errorlogging verbessert, Geänderte ServerCerts können aktualisiert werden
This commit is contained in:
parent
6aa1cfda8e
commit
fd38c8b47d
146
bibVPN.sh
146
bibVPN.sh
@ -23,12 +23,13 @@
|
||||
###################
|
||||
#### Variables ####
|
||||
###################
|
||||
VERSION='1.4.6'
|
||||
VERSION='1.5.1'
|
||||
WORKDIR="/Users/$USER/.openconnect"
|
||||
mkdir -p $WORKDIR
|
||||
logfile="$WORKDIR/bibVPN.log"
|
||||
echo -e "\nStart: $(date)" >>$logfile
|
||||
echo "Run $0 $@" >> $logfile
|
||||
LOG="$WORKDIR/bibVPN.log"
|
||||
ERRORLOG="$WORKDIR/ERROR.log"
|
||||
echo -e "\nStart: $(date)" >>$LOG
|
||||
echo "Run $0 $@" >> $LOG
|
||||
PATH=$PATH:/usr/local/bin
|
||||
VPN_EXECUTABLE=$(which openconnect)
|
||||
OC_PIDFILE="$WORKDIR/vpn.bib.de.pid"
|
||||
@ -42,69 +43,33 @@ SHOW_SETTINGS='true'
|
||||
SHOW_ICON='true'
|
||||
SHOW_COLOR='true'
|
||||
DEBUG_OUTPUT='false'
|
||||
|
||||
function saveSettings(){
|
||||
echo "Speichere die Einstellungen" >> $logfile
|
||||
echo "SHOW_SETTINGS='$SHOW_SETTINGS'
|
||||
SHOW_ICON='$SHOW_ICON'
|
||||
SHOW_COLOR='$SHOW_COLOR'
|
||||
DEBUG_OUTPUT=$DEBUG_OUTPUT" |tee $SETTINGSFILE
|
||||
|
||||
echo "Lade die Einstellungsdatei" >> $logfile
|
||||
source $SETTINGSFILE
|
||||
open swiftbar://refreshplugin?name=$(basename $0)
|
||||
}
|
||||
|
||||
if [ -f $SETTINGSFILE ] ; then
|
||||
echo "Lade die Einstellungsdatei" >> $logfile
|
||||
source $SETTINGSFILE
|
||||
else
|
||||
saveSettings
|
||||
fi
|
||||
if [ $SHOW_COLOR == 'true' ] ; then
|
||||
COLOR_CONNECTED='green'
|
||||
COLOR_UNCONNECTED='crimson'
|
||||
else
|
||||
COLOR_CONNECTED='none'
|
||||
COLOR_UNCONNECTED='none'
|
||||
fi
|
||||
NET_FILTER='inet 172.[123][0-9].1[67][80].'
|
||||
|
||||
# A command that will result in your VPN password.
|
||||
GET_VPN_PASSWORD="security find-generic-password -g -a $VPN_USERNAME 2>&1 >/dev/null | cut -d'\"' -f2"
|
||||
# Command to determine if VPN is connected or disconnected
|
||||
VPN_CONNECTED="ifconfig | egrep -A1 '$NET_FILTER' |cut -d' ' -f2"
|
||||
# Command to run to disconnect VPN
|
||||
VPN_DISCONNECT_CMD="sudo killall -2 openconnect"
|
||||
|
||||
# Get IP of Current VPN Tunnel
|
||||
IP=$(ifconfig | egrep -A1 "$NET_FILTER" |cut -d' ' -f2)
|
||||
|
||||
###################
|
||||
#### Functions ####
|
||||
###################
|
||||
function saveSettings(){
|
||||
echo "Speichere die Einstellungen" >> $logfile
|
||||
echo "Speichere die Einstellungen" >> $LOG
|
||||
echo "SHOW_SETTINGS='$SHOW_SETTINGS'
|
||||
SHOW_ICON='$SHOW_ICON'
|
||||
SHOW_COLOR='$SHOW_COLOR'
|
||||
DEBUG_OUTPUT=$DEBUG_OUTPUT" |tee $SETTINGSFILE
|
||||
SHOW_ICON='$SHOW_ICON'
|
||||
SHOW_COLOR='$SHOW_COLOR'
|
||||
DEBUG_OUTPUT=$DEBUG_OUTPUT" | tee $SETTINGSFILE
|
||||
|
||||
echo "Lade die Einstellungsdatei" >> $logfile
|
||||
echo "Lade die Einstellungsdatei" >> $LOG
|
||||
source $SETTINGSFILE
|
||||
open swiftbar://refreshplugin?name=$(basename $0)
|
||||
}
|
||||
|
||||
function askFor(){
|
||||
osascript="$1 buttons {\"Cancel\",\"OK\"} default button {\"OK\"} cancel button \"Cancel\" with title \"Neuen User für das VPN anlegen\" with icon caution"
|
||||
#üecho $osascript >> $logfile
|
||||
#üecho $osascript >> $LOG
|
||||
results=$( /usr/bin/osascript -e "$osascript")
|
||||
if [[ $? != 0 ]]; then
|
||||
clmsg='display notification "" with title "OpenConnectVPN" subtitle "Beende mich" sound name "Submarine"'
|
||||
osascript -e "$clmsg"
|
||||
exit 2
|
||||
else
|
||||
#echo $results >> $logfile
|
||||
#echo $results >> $LOG
|
||||
theButton=$( echo "$results" | /usr/bin/awk -F "button returned:|," '{print $2}' )
|
||||
theText=$( echo "$results" | /usr/bin/awk -F "text returned:" '{print $2}' )
|
||||
|
||||
@ -134,7 +99,7 @@ function showSettings()
|
||||
echo "--Farbiges Icon/Schrift | shell='$0' terminal=false param1=toggleColor tooltip='Icon oder Schrift einfärben oder eintönig lassen' checked=$SHOW_COLOR"
|
||||
echo "---"
|
||||
echo "-- --- INFOS ---"
|
||||
echo "--**Logfile:** *${logfile}* | shell='$0' param1=openlog terminal=false refresh=true tooltip='Das Logfile öffnen' md=true"
|
||||
echo "--**Logfile:** *${LOG}* | shell='$0' param1=openlog terminal=false refresh=true tooltip='Das Logfile öffnen' md=true"
|
||||
echo "--${SETTINGSFILE} | shell='$0' param1=openDIR terminal=false refresh=true tooltip='Ordner öffnen'"
|
||||
echo "--Script: $(basename -- "$0") Version: **v$VERSION** | href="https://git.bib.de/itssct/OpenFortiVPN_macOS" terminal=false refresh=true tooltip='Die Projektwebsite im Browser öffnen' md=true"
|
||||
if [[ $DEBUG_OUTPUT == 'true' ]] ; then
|
||||
@ -146,36 +111,91 @@ function showSettings()
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
###################
|
||||
#### Prüfungen ####
|
||||
###################
|
||||
if [ -f $SETTINGSFILE ] ; then
|
||||
echo "Lade die Einstellungsdatei" >> $LOG
|
||||
source $SETTINGSFILE
|
||||
else
|
||||
saveSettings
|
||||
fi
|
||||
|
||||
if [ $SHOW_COLOR == 'true' ] ; then
|
||||
COLOR_CONNECTED='green'
|
||||
COLOR_UNCONNECTED='crimson'
|
||||
else
|
||||
COLOR_CONNECTED='none'
|
||||
COLOR_UNCONNECTED='none'
|
||||
fi
|
||||
|
||||
# A command that will result in your VPN password.
|
||||
GET_VPN_PASSWORD="security find-generic-password -g -a $VPN_USERNAME 2>&1 >/dev/null | cut -d'\"' -f2"
|
||||
# Command to determine if VPN is connected or disconnected
|
||||
VPN_CONNECTED="ifconfig | egrep -A1 '$NET_FILTER' |cut -d' ' -f2"
|
||||
# Command to run to disconnect VPN
|
||||
VPN_DISCONNECT_CMD="sudo killall -2 openconnect"
|
||||
|
||||
# Get IP of Current VPN Tunnel
|
||||
IP=$(ifconfig | egrep -A1 "$NET_FILTER" |cut -d' ' -f2)
|
||||
|
||||
###################
|
||||
###### MAIN #######
|
||||
###################
|
||||
case "$1" in
|
||||
connect)
|
||||
echo "$GET_VPN_PASSWORD" >> $logfile
|
||||
rm -f $ERRORLOG
|
||||
echo "$GET_VPN_PASSWORD" >> $LOG
|
||||
VPN_PASSWORD=$(eval "$GET_VPN_PASSWORD")
|
||||
# Passwort checken
|
||||
if [[ $VPN_PASSWORD =~ 'item could not be found in the keychain.' ]] ; then
|
||||
echo "Es konnte kein Passwort für $VPN_USERNAME gefunden werden" >> $logfile
|
||||
echo "Es konnte kein Passwort für $VPN_USERNAME gefunden werden" >> $LOG
|
||||
NEW_VPN_PASSWORD=$(askFor 'display dialog "Es ist kein Passwort im Schlüsselbund vorhanden. Wie lautet das Passwort für '$VPN_USERNAME'? " default answer "Mein Passwort..." with hidden answer')
|
||||
if [[ $? != 0 ]]; then exit 2 ; fi # Beim Abbruch der Maske, gesamte Script beenden.
|
||||
echo $VPN_PASSWORD
|
||||
#echo $VPN_PASSWORD
|
||||
VPN_PASSWORD=$NEW_VPN_PASSWORD
|
||||
#echo "security add-generic-password -a $VPN_USERNAME -s openconnect -w '$VPN_PASSWORD'" >> $logfile
|
||||
#echo "security add-generic-password -a $VPN_USERNAME -s openconnect -w '$VPN_PASSWORD'" >> $LOG
|
||||
SAVE_PASSWORD=$(askFor 'display dialog "Soll das Passwort, dauerhaft im Schlüsselbund gespeichert werden? "')
|
||||
if [[ $SAVE_PASSWORD == "OK" ]]; then
|
||||
security add-generic-password -a $VPN_USERNAME -s openconnect -T /usr/bin/security -w "$NEW_VPN_PASSWORD"
|
||||
fi
|
||||
#else
|
||||
#echo "VPN PASSWORT: $VPN_PASSWORD" >> $logfile
|
||||
#echo "VPN PASSWORT: $VPN_PASSWORD" >> $LOG
|
||||
fi
|
||||
VPN_EXECUTABLE_PARAMS="--passwd-on-stdin --servercert $4 --protocol=fortinet --pid-file=$OC_PIDFILE --background" # Optional
|
||||
|
||||
# Verbindung aufbauen
|
||||
echo "<pseudoPasswort>| sudo $VPN_EXECUTABLE $VPN_EXECUTABLE_PARAMS --user $VPN_USERNAME $VPN_HOST" >> $logfile 2>&1
|
||||
echo "$VPN_PASSWORD" | sudo $VPN_EXECUTABLE $VPN_EXECUTABLE_PARAMS --user $VPN_USERNAME $VPN_HOST>> $logfile 2>&1
|
||||
echo "<pseudoPasswort>| sudo $VPN_EXECUTABLE $VPN_EXECUTABLE_PARAMS --user $VPN_USERNAME $VPN_HOST" >> $LOG
|
||||
echo "$VPN_PASSWORD" | sudo $VPN_EXECUTABLE $VPN_EXECUTABLE_PARAMS --user $VPN_USERNAME $VPN_HOST 2> $ERRORLOG 1>> $LOG
|
||||
if [[ $? != 0 ]]; then
|
||||
osascript -e 'display altert "Verbindungsaufbau Fehlgeschlagen. Bitte im Log prüfen, was die Ursache ist."'
|
||||
fi
|
||||
if [ -f $ERRORLOG ] ; then
|
||||
cat $ERRORLOG >> $LOG
|
||||
if grep -q "None of the 1 fingerprint(s) specified via --servercert match server's certificate:" $ERRORLOG ; then
|
||||
newServerCert=$(grep -o 'pin-.*=$' $ERRORLOG)
|
||||
echo "Das Servercert ist nicht aktuell. Dieses muss aktualisiert werden. (ALT: $4 | NEU: $newServerCert)" >> $LOG
|
||||
dialogCert=$(osascript -e "display alert \"Das Servercert ist nicht aktuell. \nSoll dieses aktualisiert werden?\n\nAlt : \n$4\n\nNeu : \n$newServerCert\" buttons {\"Nein\", \"Ja\"}")
|
||||
theButton=$( echo "$dialogCert" | /usr/bin/awk -F "button returned:|," '{print $2}' )
|
||||
if [[ $theButton == "Ja" ]] ;then
|
||||
echo "Ändere das Servercert" >> $LOG
|
||||
# sed mit | statt / , da im String auch Slashes vorhanden sein können.
|
||||
echo "sed -i '' -e \"s|$4|$newServerCert|g\" $ACCOUNTFILE" >> $LOG
|
||||
sed -i '' -e "s|$4|$newServerCert|g" $ACCOUNTFILE
|
||||
if [[ $? = 0 ]]; then
|
||||
osascript -e 'display alert "Erfolgreich geändert. Bitte die Verbindung erneut aufbauen."'
|
||||
fi
|
||||
else
|
||||
echo "Wie du willst. Das Servercert wird nicht geändert." >> $LOG
|
||||
fi
|
||||
elif grep -q "nodename nor servname provided, or not known" $ERRORLOG; then
|
||||
echo "Verbindungsaufbau fehlgeschlagen. $VPN_HOST kann nicht aufgelöst werden." >> $LOG
|
||||
osascript -e "display notification \"Verbindungsaufbau fehlgeschlagen. $VPN_HOST kann nicht aufgelöst werden.\" with title \"OpenConnectVPN\" subtitle \"Schade\" sound name \"Brise\""
|
||||
fi
|
||||
else
|
||||
osascript -e 'display notification "Verbindungsaufbau fehlgeschlagen. Bitte im Log prüfen, was die Ursache ist." with title "OpenConnectVPN" subtitle "Schade" sound name "Brise"'
|
||||
fi
|
||||
exit
|
||||
fi
|
||||
IP=$(ifconfig | egrep -A1 "$NET_FILTER" |cut -d' ' -f2)
|
||||
ii=0
|
||||
while [ -z $IP ] && [[ $ii != 20 ]]; do echo "noch keine IP" ; sleep 0.5 ; IP=$(ifconfig | egrep -A1 "$NET_FILTER"|cut -d' ' -f2) ; ((ii++)) ; done
|
||||
@ -185,7 +205,7 @@ case "$1" in
|
||||
osascript -e "$msg" ;
|
||||
else
|
||||
osascript -e "$errmsg"
|
||||
echo "Vermutlich ist das Passwort falsch. Es konnte keine Verbindung hergestellt werden." >> $logfile
|
||||
echo "Vermutlich ist das Passwort falsch. Es konnte keine Verbindung hergestellt werden." >> $LOG
|
||||
exit 5 ;
|
||||
fi
|
||||
# Wait for connection so menu item refreshes instantly
|
||||
@ -208,7 +228,7 @@ case "$1" in
|
||||
if [[ -z $NEW_VPN_USERNAME ]] ; then echo -n "Wie lautet die E-Mail des Benutzers? " ; read NEW_VPN_USERNAME ; fi
|
||||
|
||||
NEW_VPN_PUBKEY=$(gnutls-cli --print-cert $NEW_VPN_HOST |grep -e 'pin-.*:'|awk '{$1=$1;print}')
|
||||
NEW_VPN_PASSWORD=$(security find-generic-password -g -a $NEW_VPN_USERNAME 2>&1 ) # >>$logfile) #>/dev/null)
|
||||
NEW_VPN_PASSWORD=$(security find-generic-password -g -a $NEW_VPN_USERNAME 2>&1 ) # >>$LOG) #>/dev/null)
|
||||
if [[ $NEW_VPN_PASSWORD =~ password ]] ; then
|
||||
echo "Es ist bereits ein Passwort im Schlüsselbund für $NEW_VPN_USERNAME hinterlegt."
|
||||
else
|
||||
@ -219,25 +239,25 @@ case "$1" in
|
||||
security add-generic-password -a $NEW_VPN_USERNAME -s openconnect -w $NEW_VPN_PASSWORD
|
||||
fi
|
||||
fi
|
||||
echo "$NEW_VPN_NAME,$NEW_VPN_USERNAME,$NEW_VPN_HOST,$NEW_VPN_PUBKEY" >> $logfile >> $ACCOUNTFILE
|
||||
echo "$NEW_VPN_NAME,$NEW_VPN_USERNAME,$NEW_VPN_HOST,$NEW_VPN_PUBKEY" >> $LOG >> $ACCOUNTFILE
|
||||
# Refreshen, damit das Profil direkt angezeigt wird.
|
||||
open swiftbar://refreshplugin?name=$(basename $0)
|
||||
osascript -e 'display notification "VPN Profil '$NEW_VPN_NAME' erfolgreich angelegt" with title "OpenVPN" subtitle "'$NEW_VPN_HOST' - '$NEW_VPN_USERNAME'" sound name "Brise"'
|
||||
;;
|
||||
openlog)
|
||||
open -a console $logfile
|
||||
open -a console $LOG
|
||||
;;
|
||||
openDIR)
|
||||
open ${WORKDIR}
|
||||
;;
|
||||
toggleColor)
|
||||
if [ $SHOW_COLOR == 'false' ];then SHOW_COLOR='true';else SHOW_COLOR='false' ; fi
|
||||
echo "Switche den Farbmodus ($SHOW_COLOR)" >> $logfile
|
||||
echo "Switche den Farbmodus ($SHOW_COLOR)" >> $LOG
|
||||
saveSettings
|
||||
;;
|
||||
toggleIcon)
|
||||
if [ $SHOW_ICON == 'false' ];then SHOW_ICON='true';else SHOW_ICON='false' ; fi
|
||||
echo "Switche den Iconmodus ($SHOW_ICON)" >> $logfile
|
||||
echo "Switche den Iconmodus ($SHOW_ICON)" >> $LOG
|
||||
saveSettings
|
||||
;;
|
||||
esac
|
||||
|
Loading…
Reference in New Issue
Block a user