tildemerge/setup.sh

174 lines
5.5 KiB
Bash
Raw Normal View History

2018-11-11 21:54:17 +00:00
#!/bin/sh
2019-01-20 05:05:30 +00:00
# Copyright (c) 2018-2019 TildeLinux Maintainers
2018-11-11 21:54:17 +00:00
#
# 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.
2019-01-20 05:05:30 +00:00
#
# Email: tildelinux@tildeverse.org
2018-11-11 21:54:17 +00:00
2019-01-20 05:05:30 +00:00
is_substring() {
2018-11-16 06:26:43 +00:00
case $2 in
2019-01-20 05:05:30 +00:00
*$1*)
return 0
2018-11-16 06:26:43 +00:00
;;
2019-01-20 05:05:30 +00:00
*)
return 1
2018-11-16 06:26:43 +00:00
;;
esac
2018-11-11 21:54:17 +00:00
}
configure_directories() {
2018-11-16 06:26:43 +00:00
server_name="$1"
local_dir=""
remote_dir=""
2019-01-20 05:05:30 +00:00
printf "What would you like to call the local directory? (~/Remote/...)?\\n"
2018-11-16 06:26:43 +00:00
read -r answer
2019-01-20 05:05:30 +00:00
local_dir="${HOME}/Remote/${answer}"
2018-11-16 06:26:43 +00:00
mkdir -p "$local_dir"
2019-01-20 05:05:30 +00:00
printf "Created directory %s.\\n" "$local_dir"
printf "What directory on \"%s\" would you like to link it to? (absolute path)\\n" "$server_name"
2018-11-16 06:26:43 +00:00
read -r answer
remote_dir="$answer"
2019-01-20 05:05:30 +00:00
printf "Linking directory %s to %s on \"%s\".\\n" "$local_dir" "$remote_dir" "$server_name"
sshfs "${server_name}:${remote_dir}" "$local_dir"
printf "Done, enjoy!\\n"
2018-11-11 21:54:17 +00:00
}
attempt_login() {
2018-11-16 06:26:43 +00:00
server_name="$1"
ssh_temp_config="$2"
ssh_config="$3"
2018-11-11 21:54:17 +00:00
2019-01-20 05:05:30 +00:00
printf "Attempting to log in with configuration for \"%s\".\\n" "$server_name"
if ssh -qF "$ssh_temp_config" "$server_name" exit ; then
printf "Login successful! Writing ssh config...\\n"
2018-11-16 06:26:43 +00:00
cp "$ssh_temp_config" "$ssh_config"
2019-01-20 05:05:30 +00:00
printf "Would you like to configure sshfs for this server? (y/n)\\n"
2018-11-16 06:26:43 +00:00
read -r answer
case "$answer" in
"y")
configure_directories "$server_name"
;;
"n")
2019-01-20 05:05:30 +00:00
printf "All right, you're all done!\\n"
printf "Exiting...\\n"
2018-11-16 06:26:43 +00:00
;;
"*")
2019-01-20 05:05:30 +00:00
printf "\"%s\" was not recognized as an answer, please enter 'y' or 'n'\\n" "$answer"
2018-11-16 06:26:43 +00:00
esac
else
2019-01-20 05:05:30 +00:00
printf "Login failed, what would you like to do?\\n"
printf " 1) Try logging in again.\\n"
printf " 2) Re-configure my credentials.\\n"
2018-11-16 06:26:43 +00:00
read -r answer
case "$answer" in
"1")
attempt_login "$server_name" "$ssh_temp_config" "$ssh_config"
;;
"2")
setup_keys "$server_address"
;;
esac
fi
2018-11-11 21:54:17 +00:00
}
setup_keys() {
2018-11-16 06:26:43 +00:00
server_address="$1"
server_name="$2"
login=""
key_path=""
ssh_dir=""
key_type=""
2018-11-11 21:54:17 +00:00
2019-01-20 05:05:30 +00:00
printf "Configuring \"%s\" as \"%s\".\\n" "$server_address" "$server_name"
printf "What is your login name for this server?\\n"
2018-11-16 06:26:43 +00:00
read -r login
2019-01-20 05:05:30 +00:00
printf "Where is the private key you use for this account? (absolute path)\\n"
2018-11-16 06:26:43 +00:00
read -r key_path
2019-01-20 05:05:30 +00:00
if test -r "$key_path" ; then
printf "File found, configuring ssh...\\n"
ssh_dir="${HOME}/.ssh"
ssh_host_dir="${HOME}/.ssh/${server_name}"
2018-11-16 06:26:43 +00:00
key_type=$(head -n 1 "$key_path" \
| cut -f 2 -d ' ' \
| tr '[:upper:]' '[:lower:]')
2019-01-26 00:35:32 +00:00
test -d "$ssh_dir" || mkdir -p "$ssh_dir"
test -d "$ssh_host_dir" || mkdir -p "$ssh_host_dir"
printf "Key appears to be of type \"%s\".\\n" "$key_type"
2019-01-20 05:05:30 +00:00
printf "Generating public key...\\n"
ssh-keygen -y -f "$key_path" > "${ssh_host_dir}/id_${key_type}.pub"
printf "Copying private key...\\n"
cp "$key_path" "${ssh_host_dir}/id_${key_type}"
printf "Saving configuration...\\n"
test -e "${ssh_dir}/config" || touch "${ssh_dir}/config"
2018-11-11 21:54:17 +00:00
2019-01-20 05:05:30 +00:00
cp "${ssh_dir}/config" "${ssh_dir}/config.tmp"
{
printf "Host %s\\n" "$server_name"
printf " HostName %s\\n" "$server_address"
printf " port 22\\n"
printf " User %s\\n" "$login"
printf " IdentityFile %s/id_%s\\n" "$ssh_host_dir" "$key_type"
} >> "${ssh_dir}/config.tmp"
printf "Right now your configuration looks like this:\\n"
cat "${ssh_dir}/config.tmp"
2018-11-11 21:54:17 +00:00
2019-01-20 05:05:30 +00:00
attempt_login "$server_name" "${ssh_dir}/config.tmp" "${ssh_dir}/config"
2018-11-16 06:26:43 +00:00
else
2019-01-20 05:05:30 +00:00
printf "%s could not be read.\\n" "$key_path"
printf "Either it doesn't exist, or you dont have the correct permissions, please try again.\\n"
2018-11-16 06:26:43 +00:00
setup_keys "$server_address" "$server_name"
fi
2018-11-11 21:54:17 +00:00
}
pick_account() {
2018-12-01 20:41:20 +00:00
answers="1 2 3 4"
2018-11-16 06:26:43 +00:00
answer=""
2019-01-20 05:05:30 +00:00
printf "What account would you like to configure?\\n"
printf " 1) tilde.town\\n"
printf " 2) tilde.team\\n"
printf " 3) yourtilde.com\\n"
printf " 4) cosmic.voyage\\n"
printf " 5) tilde.institute\\n"
2018-11-16 06:26:43 +00:00
read -r answer
2019-01-20 05:05:30 +00:00
2018-11-16 06:26:43 +00:00
if is_substring "$answer" "$answers"
then
case "$answer" in
"1")
setup_keys "tilde.town" "town"
;;
"2")
setup_keys "tilde.team" "team"
;;
2018-11-17 02:27:18 +00:00
"3")
setup_keys "yourtilde.com" "your"
;;
"4")
setup_keys "cosmic.voyage" "cosmic"
2018-11-28 07:25:29 +00:00
;;
2018-12-10 07:29:11 +00:00
"5")
setup_keys "tilde.institute" "institute"
;;
2018-11-16 06:26:43 +00:00
esac
else
2019-01-20 05:05:30 +00:00
printf "\"%s\" is not a valid option.\\n" "$answer"
2018-11-16 06:26:43 +00:00
pick_account
fi
2018-11-11 21:54:17 +00:00
}
pick_account