|
#!/bin/sh |
|
# |
|
# add-sshkey-and-rights.sh |
|
# Script by Tobias Lindberg |
|
# |
|
|
|
|
|
# Print what the script does.. |
|
scriptname=`basename "$0"` |
|
printf "Starting $scriptname\n" |
|
printf "Description: add ssh-keys defined below to specified users home-directory to .ssh/authorized_keys file\n\n" |
|
|
|
|
|
# Getting username, in case this is run with sudo |
|
if [ $SUDO_USER ] |
|
then |
|
username=$SUDO_USER |
|
sudo=true |
|
else |
|
username=`whoami` |
|
sudo=false |
|
fi |
|
|
|
# Ask user for username to add ssh-key too |
|
read -p "Enter name of user you want to add ssh-key to: [$username] " user |
|
|
|
# Filling $user with userif |
|
if [ -z "$user" ] |
|
then |
|
user=$username |
|
fi |
|
|
|
|
|
# Check if user is not sudo and ask if he wants to get sudo level |
|
if [ $sudo = false ] |
|
then |
|
# Check if user is not the same as the user that is target of change |
|
if [ $user != $username ] |
|
then |
|
printf "ERROR: You will not succeed adding your key to user $user, since you are not running with sudo and require more rights!\n" |
|
read -p "DO you wan to continue by getting sudo rights (yes/no)? [yes] " request_sudo |
|
|
|
# Set default value to request_sudo to yes |
|
if [ -z "$request_sudo" ] |
|
then |
|
request_sudo="yes" |
|
fi |
|
|
|
# Check if yes was selected and then execute a sudo command and create prefix |
|
if [ $request_sudo = "yes" ] || [ $request_sudo = "y" ] || [ $request_sudo = "Yes" ] || [ $request_sudo = "Y" ] |
|
then |
|
sudo printf "Now we can continue with sudo..\n" |
|
prefix_sudo="sudo" |
|
else |
|
printf "ERROR: Failed to continue, since we are missing rights.. stopping script!\n\n" |
|
exit 1 |
|
fi |
|
fi |
|
fi |
|
|
|
# Checking if it's a value user |
|
id -u $user > /dev/null 2>&1 |
|
if [ $? -ne 0 ] |
|
then |
|
printf "ERROR: User $user doesn\'t exists.. please validate and re-run the script!\n" |
|
exit 1 |
|
fi |
|
|
|
|
|
# Defining dirs to work in.. |
|
if [ $user != $username ] |
|
then |
|
homedir=$( getent passwd "$user" | cut -d: -f6 ) |
|
else |
|
homedir=$HOME |
|
fi |
|
sshdir=$homedir/.ssh |
|
authorized_keys_file=$sshdir/authorized_keys |
|
|
|
|
|
# Setting right umask |
|
umask 0077 |
|
|
|
# Creating .ssh folder, if it doesn't exists |
|
$prefix_sudo mkdir -p $sshdir |
|
|
|
# The [email protected] ssh key |
|
sshrsa_tobiasehlertgmailcom_key="ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAu48hZJJwQUjfQEXE637P6KdGi3cTs+B5B2V2ZLdVeCHS5dMQ8HDAbRuGvxuK9Ty3OMqylQJ66UDa4dzMOzvnxZnThcP6bJ5e5ZoLP0BVe2c25HYSmJYt+4KTaur/OJmVuMaWiY/vAQRpjREl8z6UPh8/QibBDSreuk06Ln+jcFho5pXdvq+nmHxC8pyJV4QNSbXk+dwkeBMgimXtO3Tn6Dg7WR2Rnxt7Run5r9GfplWFobweT/AupXFYFJ9FJrXQXXIgdxh9h2wX4cqemUtD0kcIrskGdfWHWAsyt1UdJo0rW3nsuja+emTnd1e1YIhJ16ZaxlSdmBVnqDuYn7+7nQ== [email protected]" |
|
|
|
|
|
# Check if authorized_Keys exists and adding key to file.. or creating the file and adding key to file |
|
if [[ -f $authorized_keys_file ]] |
|
then |
|
# Searching for the file and adding all the keys, that should be inserted there.. that don't exist yet |
|
printf "Found $authorized_keys_file file.\n" |
|
|
|
# sshrsa_tobiasehlertgmailcom_key |
|
printf "Looking for key sshrsa_tobiasehlertgmailcom_key in $authorized_keys_file.. adding if needed!\n" |
|
$prefix_sudo grep -q -F "$sshrsa_tobiasehlertgmailcom_key" $authorized_keys_file || $prefix_sudo echo $sshrsa_tobiasehlertgmailcom_key >> $authorized_keys_file |
|
|
|
else |
|
# Could not find file.. so adding the different keys now |
|
printf "Could not find $authorized_keys_file.\n" |
|
|
|
|
|
# sshrsa_tobiasehlertgmailcom_key |
|
printf "Adding sshrsa_tobiasehlertgmailcom_key to $authorized_keys_file\n." |
|
$prefix_sudo echo $sshrsa_tobiasehlertgmailcom_key >> $authorized_keys_file |
|
|
|
fi |
|
printf "Finished adding key(s) to $authorized_keys_file!\n" |
|
|
|
# Adjusting rights of the folder |
|
printf "Performing chmod and chown commands on: $sshdir\n" |
|
$prefix_sudo chmod -R go= $sshdir |
|
$prefix_sudo chown -R $user:$user $sshdir |
|
|
|
|
|
printf "Complete!\nScript done!\n" |
|
exit 0 |