#!/bin/bash
# /usr/lib/gdm/gdm-ssh-session
# A secure X connection
#
# *      Copyright 2010 Francesco Cappelli
# *      
# *      This program is free software; you can redistribute it and/or modify
# *      it under the terms of the GNU General Public License as published by
# *      the Free Software Foundation; either version 2 of the License, or
# *      (at your option) any later version.
# *      
# *      This program is distributed in the hope that it will be useful,
# *      but WITHOUT ANY WARRANTY; without even the implied warranty of
# *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# *      GNU General Public License for more details.
# *      
# *      You should have received a copy of the GNU General Public License
# *      along with this program; if not, write to the Free Software
# *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# *      MA 02110-1301, USA.
# *
#
ZENITY=`which zenity`
#
# Variables. The file /home/$USER/.ssh/ssh-par (or /etc/ssh/ssh-par) has to exist, to be readable and to be in the form
# ---------------------------------------
# HOSTSSH1=hostname1
# HOSTSSH2=hostname2
# HOSTSSH3=hostname3
# HOSTSSH4=
# HOSTSSH5=
#
# PORTSSH1=22
# PORTSSH2=port2
# PORTSSH3=
#
# USERSSH1=$USER
# USERSSH2=user2
# USERSSH3=
#
# COMMAND1=command1
# COMMAND2=command2
# COMMAND3=
# ---------------------------------------
# Commands can be shell commands or scripts in the form COMMAND=`cat /path/scriptsh`
#
#
if [ -r /home/$USER/.ssh/ssh-par ] ; then
. /home/$USER/.ssh/ssh-par
elif [ ! -r /home/$USER/.ssh/ssh-par ] && [ -r /etc/ssh/ssh-par ] ; then
. /etc/ssh/ssh-par
fi
# If a command is a script its name is replaced
if [ "${COMMAND1%!*}" == "#" ] ; then
COMM1="$COMMAND1"
COMMAND1="script1"
fi
if [ "${COMMAND2%!*}" == "#" ] ; then
COMM2="$COMMAND2"
COMMAND2="script2"
fi
if [ "${COMMAND3%!*}" == "#" ] ; then
COMM3="$COMMAND3"
COMMAND3="script3"
fi
#
#################### Procedure
#
zenity --question --title "GDM SSH Session" --text "GDM SSH Session - This procedure let you graphically connect to a host via SSH. \n \n If you want to insert data manually choose \"No\". \n If you want to connect via default parameters choose \"Yes\". \n \n \n To modify default parameters edit \n \n \t /home/$USER/.ssh/ssh-variable \n \n (or \t /etc/ssh/ssh-variable )"
DIR="$?" # stout
if ( [ -r /home/$USER/.ssh/ssh-par ] || [ -r /etc/ssh/ssh-par ] ) && [ "$DIR" == "0" ] ; then
# Host port and user
TARGETHOST=`$ZENITY --title "Server" --text "Choose host to connect to via SSH \n or choose \"Abort\" for manual management." --list --radiolist --column "" --column "server" TRUE "$HOSTSSH1" FALSE "$HOSTSSH2" FALSE "$HOSTSSH3" FALSE "$HOSTSSH4" FALSE "$HOSTSSH5" --height=290`
# if abort go directly to final request
if [ -n "$TARGETHOST" ]  ; then
TARGETPORT=`$ZENITY --title "Port" --text "Choose port." --list --radiolist --column "" --column "port" TRUE "$PORTSSH1" FALSE "$PORTSSH2" FALSE "$PORTSSH3" --height=220`
fi
#
if [ -n "$TARGETHOST" ] && [ -n "$TARGETPORT" ] ; then
TARGETUSER=`$ZENITY --title "User" --text "Choose username." --list --radiolist --column "" --column "user" TRUE "$USERSSH1" FALSE "$USERSSH2" FALSE "$USERSSH3" --height=220`
fi
if [ -n "$TARGETHOST" ] && [ -n "$TARGETPORT" ] && [ -n "$TARGETUSER" ] ; then
TARGETCOMMAND=`$ZENITY --title "Command" --text "Choose command." --list --radiolist --column "" --column "command" TRUE "$COMMAND1" FALSE "$COMMAND2" FALSE "$COMMAND3" --height=220 --width=280`
# If command is a script it is rereplaced
if [ "$TARGETCOMMAND" == "script1" ] ; then
TARGETCOMMAND="$COMM1"
fi
if [ "$TARGETCOMMAND" == "script2" ] ; then
TARGETCOMMAND="$COMM2"
fi
if [ "$TARGETCOMMAND" == "script3" ] ; then
TARGETCOMMAND="$COMM3"
fi
fi
fi
# If you choose default parameters but the file ssh-par doesn't exist an error message appear
if ( [ ! -r /home/$USER/.ssh/ssh-par ] && [ ! -r /etc/ssh/ssh-par ] ) && [ "$DIR" == "0" ] ; then
zenity --error --text="File \n \n \t /home/$USER/.ssh/ssh-variable \n \n (or \t /etc/ssh/ssh-variable ) \n \n not found."
fi
# If ssh-par doesn't exist or at least one variable isn't specified it asks for manual management
if ( [ ! -r /home/$USER/.ssh/ssh-par ] && [ ! -r /etc/ssh/ssh-par ] ) || [ -z "$TARGETHOST" ] || [ -z "$TARGETPORT" ] || [ -z "$TARGETUSER" ] || [ -z "$TARGETCOMMAND" ] || [ "$DIR" == "1" ] ; then
TARGETUSERHOSTPORTCOMMAND=`$ZENITY --title "Username, server, port and command to send via SSH" --entry --text "Username, server, port and command to send via SSH in the form  username@server:port-command ."`
# It cut the string to extract informations
TARGETUSER=${TARGETUSERHOSTPORTCOMMAND%@*}
TARGETHOSTPORTCOMMAND=${TARGETUSERHOSTPORTCOMMAND#*@}
TARGETHOST=${TARGETHOSTPORTCOMMAND%:*}
TARGETPORTCOMMAND=${TARGETHOSTPORTCOMMAND#*:}
TARGETPORT=${TARGETPORTCOMMAND%-*}
TARGETCOMMAND=${TARGETPORTCOMMAND#*-}
fi
#
################### End of procedure
#
# Until procedure isn't success replay
until ERROR=$(ssh -X -T -f -l "$TARGETUSER" -p "$TARGETPORT" "$TARGETHOST" "$TARGETCOMMAND" 2>&1 >/dev/null);
#
do
# Error message and request
INT=`$ZENITY --title "Error" --text "SSH error message: \n \n $ERROR \n \n \n What do you want to do?" --list --radiolist --column "" --column "" TRUE "Restart procedure" FALSE "Logout (Abort)" FALSE "Open a terminal" --height=350`
if [ "$INT" == "Logout (Abort)" ] || [ -z "$INT" ] ; then
# Logout
break
elif [ "$INT" == "Open a terminal" ] ; then
# Open a terminal and then logout
cd /home/$USER ; x-terminal-emulator
break
else
# 
##################### Restart procedure
#
if [ -r /home/$USER/.ssh/ssh-par ] ; then
. /home/$USER/.ssh/ssh-par
elif [ ! -r /home/$USER/.ssh/ssh-par ] && [ -r /etc/ssh/ssh-par ] ; then
. /etc/ssh/ssh-par
fi
# If a command is a script its name is replaced
if [ "${COMMAND1%!*}" == "#" ] ; then
COMM1="$COMMAND1"
COMMAND1="script1"
fi
if [ "${COMMAND2%!*}" == "#" ] ; then
COMM2="$COMMAND2"
COMMAND2="script2"
fi
if [ "${COMMAND3%!*}" == "#" ] ; then
COMM3="$COMMAND3"
COMMAND3="script3"
fi
#
#################### Procedure
#
zenity --question --title "GDM SSH Session" --text "GDM SSH Session - This procedure let you graphically connect to a host via SSH. \n \n If you want to insert data manually choose \"No\". \n If you want to connect via default parameters choose \"Yes\". \n \n \n To modify default parameters edit \n \n \t /home/$USER/.ssh/ssh-variable \n \n (or \t /etc/ssh/ssh-variable )"
DIR="$?" # stout
if ( [ -r /home/$USER/.ssh/ssh-par ] || [ -r /etc/ssh/ssh-par ] ) && [ "$DIR" == "0" ] ; then
# Host port and user
TARGETHOST=`$ZENITY --title "Server" --text "Choose host to connect to via SSH \n or choose \"Abort\" for manual management." --list --radiolist --column "" --column "server" TRUE "$HOSTSSH1" FALSE "$HOSTSSH2" FALSE "$HOSTSSH3" FALSE "$HOSTSSH4" FALSE "$HOSTSSH5" --height=290`
# if abort go directly to final request
if [ -n "$TARGETHOST" ]  ; then
TARGETPORT=`$ZENITY --title "Port" --text "Choose port." --list --radiolist --column "" --column "port" TRUE "$PORTSSH1" FALSE "$PORTSSH2" FALSE "$PORTSSH3" --height=220`
fi
#
if [ -n "$TARGETHOST" ] && [ -n "$TARGETPORT" ] ; then
TARGETUSER=`$ZENITY --title "User" --text "Choose username." --list --radiolist --column "" --column "user" TRUE "$USERSSH1" FALSE "$USERSSH2" FALSE "$USERSSH3" --height=220`
fi
if [ -n "$TARGETHOST" ] && [ -n "$TARGETPORT" ] && [ -n "$TARGETUSER" ] ; then
TARGETCOMMAND=`$ZENITY --title "Command" --text "Choose command." --list --radiolist --column "" --column "command" TRUE "$COMMAND1" FALSE "$COMMAND2" FALSE "$COMMAND3" --height=220 --width=280`
# If command is a script it is rereplaced
if [ "$TARGETCOMMAND" == "script1" ] ; then
TARGETCOMMAND="$COMM1"
fi
if [ "$TARGETCOMMAND" == "script2" ] ; then
TARGETCOMMAND="$COMM2"
fi
if [ "$TARGETCOMMAND" == "script3" ] ; then
TARGETCOMMAND="$COMM3"
fi
fi
fi
# If you choose default parameters but the file ssh-par doesn't exist an error message appear
if ( [ ! -r /home/$USER/.ssh/ssh-par ] && [ ! -r /etc/ssh/ssh-par ] ) && [ "$DIR" == "0" ] ; then
zenity --error --text="File \n \n \t /home/$USER/.ssh/ssh-variable \n \n (or \t /etc/ssh/ssh-variable ) \n \n not found."
fi
# If ssh-par doesn't exist or at least one variable isn't specified it asks for manual management
if ( [ ! -r /home/$USER/.ssh/ssh-par ] && [ ! -r /etc/ssh/ssh-par ] ) || [ -z "$TARGETHOST" ] || [ -z "$TARGETPORT" ] || [ -z "$TARGETUSER" ] || [ -z "$TARGETCOMMAND" ] || [ "$DIR" == "1" ] ; then
TARGETUSERHOSTPORTCOMMAND=`$ZENITY --title "Username, server, port and command to send via SSH" --entry --text "Username, server, port and command to send via SSH in the form  username@server:port-command ."`
# It cut the string to extract informations
TARGETUSER=${TARGETUSERHOSTPORTCOMMAND%@*}
TARGETHOSTPORTCOMMAND=${TARGETUSERHOSTPORTCOMMAND#*@}
TARGETHOST=${TARGETHOSTPORTCOMMAND%:*}
TARGETPORTCOMMAND=${TARGETHOSTPORTCOMMAND#*:}
TARGETPORT=${TARGETPORTCOMMAND%-*}
TARGETCOMMAND=${TARGETPORTCOMMAND#*-}
fi
#
################### End procedure
#
fi
#
done
# Exit
exit 0
#
#
