tildemerge/setup.sh

174 lines
5.5 KiB
Bash
Executable File

#!/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
is_substring() {
case $2 in
*$1*)
return 0
;;
*)
return 1
;;
esac
}
configure_directories() {
server_name="$1"
local_dir=""
remote_dir=""
printf "What would you like to call the local directory? (~/Remote/...)?\\n"
read -r answer
local_dir="${HOME}/Remote/${answer}"
mkdir -p "$local_dir"
printf "Created directory %s.\\n" "$local_dir"
printf "What directory on \"%s\" would you like to link it to? (absolute path)\\n" "$server_name"
read -r answer
remote_dir="$answer"
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"
}
attempt_login() {
server_name="$1"
ssh_temp_config="$2"
ssh_config="$3"
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"
cp "$ssh_temp_config" "$ssh_config"
printf "Would you like to configure sshfs for this server? (y/n)\\n"
read -r answer
case "$answer" in
"y")
configure_directories "$server_name"
;;
"n")
printf "All right, you're all done!\\n"
printf "Exiting...\\n"
;;
"*")
printf "\"%s\" was not recognized as an answer, please enter 'y' or 'n'\\n" "$answer"
esac
else
printf "Login failed, what would you like to do?\\n"
printf " 1) Try logging in again.\\n"
printf " 2) Re-configure my credentials.\\n"
read -r answer
case "$answer" in
"1")
attempt_login "$server_name" "$ssh_temp_config" "$ssh_config"
;;
"2")
setup_keys "$server_address"
;;
esac
fi
}
setup_keys() {
server_address="$1"
server_name="$2"
login=""
key_path=""
ssh_dir=""
key_type=""
printf "Configuring \"%s\" as \"%s\".\\n" "$server_address" "$server_name"
printf "What is your login name for this server?\\n"
read -r login
printf "Where is the private key you use for this account? (absolute path)\\n"
read -r key_path
if test -r "$key_path" ; then
printf "File found, configuring ssh...\\n"
ssh_dir="${HOME}/.ssh"
ssh_host_dir="${HOME}/.ssh/${server_name}"
key_type=$(head -n 1 "$key_path" \
| cut -f 2 -d ' ' \
| tr '[:upper:]' '[:lower:]')
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"
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"
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"
attempt_login "$server_name" "${ssh_dir}/config.tmp" "${ssh_dir}/config"
else
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"
setup_keys "$server_address" "$server_name"
fi
}
pick_account() {
answers="1 2 3 4"
answer=""
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"
read -r answer
if is_substring "$answer" "$answers"
then
case "$answer" in
"1")
setup_keys "tilde.town" "town"
;;
"2")
setup_keys "tilde.team" "team"
;;
"3")
setup_keys "yourtilde.com" "your"
;;
"4")
setup_keys "cosmic.voyage" "cosmic"
;;
"5")
setup_keys "tilde.institute" "institute"
;;
esac
else
printf "\"%s\" is not a valid option.\\n" "$answer"
pick_account
fi
}
pick_account