2025-08-30 07:58:50 -05:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
2025-08-30 08:11:13 -05:00
|
|
|
# Variables
|
|
|
|
|
YUBIKEY_ID_DIR="$HOME/.config/yubikey"
|
|
|
|
|
SSH_ID_RSA="$HOME/.ssh/id_rsa"
|
2025-08-30 07:58:50 -05:00
|
|
|
|
2025-08-30 08:11:13 -05:00
|
|
|
# Function to display an error message and exit
|
|
|
|
|
error_exit() {
|
|
|
|
|
echo "Error: $1" >&2
|
2025-08-30 07:58:50 -05:00
|
|
|
exit 1
|
2025-08-30 08:11:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Find YubiKeys
|
|
|
|
|
yubikeys=$(ykman list)
|
|
|
|
|
if [[ -z "$yubikeys" ]]; then
|
|
|
|
|
error_exit "No YubiKeys found."
|
2025-08-30 07:58:50 -05:00
|
|
|
fi
|
|
|
|
|
|
2025-08-30 08:11:13 -05:00
|
|
|
# Select a YubiKey using wofi
|
2025-08-30 07:58:50 -05:00
|
|
|
selected=$(echo "$yubikeys" | wofi --dmenu -i)
|
|
|
|
|
if [[ -z "$selected" ]]; then
|
2025-08-30 08:11:13 -05:00
|
|
|
error_exit "No YubiKey selected."
|
2025-08-30 07:58:50 -05:00
|
|
|
fi
|
|
|
|
|
|
2025-08-30 08:11:13 -05:00
|
|
|
# Extract the YubiKey serial number
|
2025-08-30 07:58:50 -05:00
|
|
|
YUBIKEY_SERIAL=$(echo "$selected" | awk '{print $NF}')
|
2025-08-30 08:11:13 -05:00
|
|
|
if [[ -z "$YUBIKEY_SERIAL" ]]; then
|
|
|
|
|
error_exit "Failed to extract YubiKey serial number."
|
|
|
|
|
fi
|
2025-08-30 07:58:50 -05:00
|
|
|
|
2025-08-30 08:11:13 -05:00
|
|
|
# Remove existing symbolic link if it exists
|
|
|
|
|
if [ -L "$SSH_ID_RSA" ]; then
|
|
|
|
|
rm -f "$SSH_ID_RSA"
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
|
error_exit "Failed to remove existing symbolic link."
|
|
|
|
|
fi
|
2025-08-30 07:58:50 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Create the symbolic link
|
2025-08-30 08:11:13 -05:00
|
|
|
ln -s "$YUBIKEY_ID_DIR/id_$YUBIKEY_SERIAL" "$SSH_ID_RSA"
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
|
error_exit "Failed to create symbolic link."
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Set correct permissions
|
|
|
|
|
chmod 600 "$SSH_ID_RSA"
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
|
error_exit "Failed to set permissions on symbolic link."
|
|
|
|
|
fi
|
2025-08-30 07:58:50 -05:00
|
|
|
|
2025-08-30 08:11:13 -05:00
|
|
|
# Stop the current ssh-agent
|
2025-08-30 07:58:50 -05:00
|
|
|
if [ -n "$SSH_AGENT_PID" ]; then
|
2025-08-30 08:11:13 -05:00
|
|
|
ssh-agent -k
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
|
echo "Failed to stop existing ssh-agent. Continuing..."
|
|
|
|
|
fi
|
|
|
|
|
unset SSH_AGENT_PID SSH_AUTH_SOCK
|
2025-08-30 07:58:50 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Start a new ssh-agent
|
|
|
|
|
eval $(ssh-agent -s)
|
2025-08-30 08:11:13 -05:00
|
|
|
if [ -z "$SSH_AGENT_PID" ]; then
|
|
|
|
|
error_exit "Failed to start ssh-agent."
|
|
|
|
|
fi
|
2025-08-30 07:58:50 -05:00
|
|
|
|
2025-08-30 08:11:13 -05:00
|
|
|
# Update tmux sessions if tmux is running
|
|
|
|
|
if tmux ls 2>/dev/null; then
|
|
|
|
|
tmux ls 2>/dev/null | cut -d ':' -f1 | while read -r session_name; do
|
|
|
|
|
tmux setenv -t "$session_name" SSH_AUTH_SOCK "$SSH_AUTH_SOCK"
|
|
|
|
|
tmux setenv -t "$session_name" SSH_AGENT_PID "$SSH_AGENT_PID"
|
|
|
|
|
tmux refresh-client -t "$session_name"
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
|
echo "Failed to update tmux session: $session_name. Continuing..."
|
|
|
|
|
else
|
|
|
|
|
echo "Updated tmux session: $session_name"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Verify symbolic link creation
|
|
|
|
|
if [ -L "$SSH_ID_RSA" ]; then
|
|
|
|
|
echo "Symbolic link created successfully at $SSH_ID_RSA"
|
2025-08-30 07:58:50 -05:00
|
|
|
else
|
2025-08-30 08:11:13 -05:00
|
|
|
error_exit "Failed to create symbolic link at $SSH_ID_RSA"
|
2025-08-30 07:58:50 -05:00
|
|
|
fi
|
|
|
|
|
|
2025-08-30 08:11:13 -05:00
|
|
|
# Extract the YubiKey name for the notification
|
|
|
|
|
YUBIKEY_NAME=$(echo "$selected" | awk '{split($0,a,"("); print a[1]}')
|
|
|
|
|
notify-send "SSH Key has been updated for $YUBIKEY_NAME"
|
|
|
|
|
|
2025-08-30 07:58:50 -05:00
|
|
|
exit 0
|