tildemerge/setup_gui.sh

207 lines
5.5 KiB
Bash
Raw Permalink Normal View History

2018-11-15 04:49:59 +00:00
#!/bin/sh
# Copyright (c) 2018-2019 TildeLinux Maintainers
#
# 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.
#
# Email: tildelinux@tildeverse.org
export SSH_ASKPASS="/usr/lib/ssh/x11-ssh-askpass"
2018-12-04 07:15:19 +00:00
set -e
2018-11-15 04:49:59 +00:00
eval "$(xdotool getmouselocation --shell)"
mouse_x=$X
mouse_y=$Y
2018-12-10 07:29:11 +00:00
server_address="tilde.town!tilde.team!yourtilde.com!cosmic.voyage!tilde.institute"
2018-11-22 07:33:16 +00:00
server_name="MyTilde"
2018-11-15 04:49:59 +00:00
login=""
ssh_port="22"
ssh_dir="$HOME/.ssh"
priv_key_path=" "
2019-01-20 05:05:30 +00:00
get_field() {
2018-11-16 06:26:43 +00:00
field_num="$1"
info="$2"
2018-11-15 04:49:59 +00:00
2019-01-20 05:05:30 +00:00
printf "%s" "$info" | cut -f "$field_num" -d '|'
2018-11-15 04:49:59 +00:00
}
2019-01-20 05:05:30 +00:00
error_box() {
2018-11-16 06:26:43 +00:00
error_text="$1"
yad \
--posx="$mouse_x" \
--posy="$mouse_y" \
2019-01-30 00:04:35 +00:00
--width=250 \
--image="dialog-error" \
--title="Error!" \
--text="$error_text" \
2018-11-16 06:26:43 +00:00
--button="OK":0
2018-11-15 04:49:59 +00:00
}
2019-01-20 05:05:30 +00:00
succ_box() {
2018-11-16 06:26:43 +00:00
succ_text="$1"
yad \
--posx="$mouse_x" \
--posy="$mouse_y" \
2019-01-30 00:04:35 +00:00
--width=250 \
--image="dialog-info" \
--title="Success!" \
--text="$succ_text" \
2018-11-16 06:26:43 +00:00
--button="OK":0
2018-11-15 04:49:59 +00:00
}
2019-01-20 05:05:30 +00:00
dec_box() {
2018-11-16 06:26:43 +00:00
error_text="$1"
yad \
--posx="$mouse_x" \
--posy="$mouse_y" \
2019-01-30 00:04:35 +00:00
--width=250 \
--image="dialog-error" \
--title="Error!" \
--text="$error_text" \
2018-11-16 06:26:43 +00:00
--button="No":1 \
--button="Yes":0
return $?
2018-11-16 05:32:30 +00:00
}
2019-01-20 05:05:30 +00:00
info_box() {
2018-11-16 06:26:43 +00:00
info_text="$1"
yad \
--posx="$mouse_x" \
--posy="$mouse_y" \
--no-buttons \
2019-01-26 21:22:00 +00:00
--width=250 \
--image="dialog-info" \
--title="Info" \
2019-01-30 00:04:35 +00:00
--text="$info_text"
2018-11-15 04:49:59 +00:00
}
get_info() {
2018-11-16 06:26:43 +00:00
error_num=0
error_msg=""
info=$(yad \
--posx="$mouse_x" \
--posy="$mouse_y" \
--title="New server setup" \
--text="Add a new server" \
--form \
--field="Server":CB \
--field="Short Name" \
--field="Login" \
--field="Port" \
--field="SSH Directory":DIR \
--field="Private key":FL \
--button="Cancel":1 \
--button="Continue":0 \
"$server_address" "$server_name" "$login" "$ssh_port" "$ssh_dir" "$priv_key_path")
2018-11-22 07:31:40 +00:00
[ $? -eq 1 ] && exit 1
2018-11-16 06:26:43 +00:00
server_address=$(get_field 1 "$info")
server_name=$(get_field 2 "$info")
login=$(get_field 3 "$info")
ssh_port=$(get_field 4 "$info")
ssh_dir=$(get_field 5 "$info")
priv_key_path=$(get_field 6 "$info")
2019-01-20 05:05:30 +00:00
2018-11-16 06:26:43 +00:00
if test -z "$login"
then
error_msg="No login provided!"
2019-01-20 05:05:30 +00:00
error_num=$((error_num + 1))
2018-11-16 06:26:43 +00:00
fi
2019-01-20 05:05:30 +00:00
if test -d "$(printf "%s" "$priv_key_path" | sed 's/\/ *$//')"
2018-11-16 06:26:43 +00:00
then
2019-01-20 05:05:30 +00:00
error_msg="${error_msg}\\nNo private key provided!"
error_num=$((error_num + 1))
2018-11-16 06:26:43 +00:00
fi
if [ $error_num -gt 0 ]
then
error_box "$error_msg" \
&& get_info
fi
configure_server
2018-11-15 04:49:59 +00:00
}
configure_server() {
2019-01-20 05:05:30 +00:00
ssh_current_config="${ssh_dir}/config"
ssh_temp_config="${ssh_dir}/config.tmp"
ssh_host_dir="${ssh_dir}/${server_address}"
key_type=$(head -n 1 "${priv_key_path}" \
2018-11-16 06:26:43 +00:00
| cut -f 2 -d ' ' \
| tr '[:upper:]' '[:lower:]')
test -e "$ssh_current_config" \
|| touch "$ssh_current_config"
cp "$ssh_current_config" "$ssh_temp_config"
2019-01-20 05:05:30 +00:00
if grep -qi "Host ${server_name}" "$ssh_current_config"
2018-11-16 06:26:43 +00:00
then
2019-01-20 05:05:30 +00:00
dec_box "Host named ${server_name} already exists, overwrite?" \
2018-11-16 06:26:43 +00:00
|| get_info
if ! dec_box "Keep old config in file? (ssh will use the new one)"
then
IFS=""
in_old_host_block=0
while read -r line
do
2019-01-20 05:05:30 +00:00
if printf "%s" "$line" | grep -q "Host ${server_name}"
2018-11-16 06:26:43 +00:00
then
in_old_host_block=0
2019-01-20 05:05:30 +00:00
elif printf "%s" "$line" | grep -q "Host "
2018-11-16 06:26:43 +00:00
then
in_old_host_block=1
fi
2019-01-20 05:05:30 +00:00
2018-11-16 06:26:43 +00:00
[ $in_old_host_block -eq 1 ] \
2019-01-20 05:05:30 +00:00
&& printf "%s" "$line"
2018-11-16 06:26:43 +00:00
done < "$ssh_current_config" > "$ssh_temp_config"
fi
fi
mkdir -p "$ssh_host_dir"
2019-01-20 05:05:30 +00:00
cp "$priv_key_path" "${ssh_host_dir}/id_${key_type}"
{
printf "Host %s\\n" "$server_name"
printf " HostName %s\\n" "$server_address"
printf " Port %s\\n" "$ssh_port"
printf " User %s\\n" "$login"
printf " IdentityFile %s/id_%s\\n" "$ssh_host_dir" "$key_type"
2018-11-16 06:26:43 +00:00
} >> "$ssh_temp_config"
2018-11-16 06:33:42 +00:00
info_box "Attempting to log in with the provided credentials..." &
2018-11-16 06:26:43 +00:00
info_pid=$(( $! + 2 )) # yikes, is this safe?
2019-01-20 05:05:30 +00:00
printf "%s\\n" "$ssh_temp_config"
printf "%s\\n" "$server_address"
if setsid -w ssh -qF "$ssh_temp_config" "$server_name" exit
2018-11-16 06:26:43 +00:00
then
kill $info_pid
2019-01-20 05:05:30 +00:00
succ_box "Login success!\\nAccount for ${server_address} created!"
2018-11-16 06:26:43 +00:00
cp "$ssh_temp_config" "$ssh_current_config"
rm "$ssh_temp_config"
exit # Without this the "Host already exists" box appears, investigate.
else
kill $info_pid
2018-11-16 06:33:42 +00:00
error_box "Login failed, please try again."
2018-11-16 06:26:43 +00:00
get_info
fi
2018-11-15 04:49:59 +00:00
}
get_info