myshellconfig/bin/ssh-agent-start-or-restart

131 lines
4.8 KiB
Text
Raw Normal View History

2020-09-21 12:39:58 +02:00
#!/bin/bash
2020-09-21 16:47:59 +02:00
[ -z "${SSH_IDENTITIES_DIR+x}" ] && { SSH_IDENTITIES_DIR="${SSH_IDENTITIES_DEFAULT_DIR-${HOME}/.ssh/identities}"; export SSH_IDENTITIES_DIR; }
[ -z "${SSH_AGENTS_DIR+x}" ] && { SSH_AGENTS_DIR=${SSH_AGENTS_DEFAULT_DIR-~/.ssh/agents}; export SSH_AGENTS_DIR; }
[ -z "${SSH_AGENT_SOCKETS_DIR+x}" ] && { SSH_AGENT_SOCKETS_DIR=${SSH_AGENT_SOCKETS_DEFAULT_DIR-~/.ssh/agents}; export SSH_AGENT_SOCKETS_DIR; }
2020-09-21 21:37:38 +02:00
logdebug "SSH_AGENTS_DIR: $SSH_AGENTS_DIR" >&2
logdebug "SSH_AGENT_SOCKETS_DIR: $SSH_AGENT_SOCKETS_DIR" >&2
logdebug "SSH_IDENTITIES_DIR: $SSH_IDENTITIES_DIR" >&2
2020-09-21 17:58:23 +02:00
[ -z "${SSH_AGENTS_DIR-x}" ] || mkdir -vp "$SSH_AGENTS_DIR"
[ -z "${SSH_AGENT_SOCKETS_DIR-x}" ] || mkdir -vp "$SSH_AGENT_SOCKETS_DIR"
[ -z "${SSH_IDENTITIES_DIR-x}" ] || mkdir -vp "$SSH_IDENTITIES_DIR"
2020-09-21 12:39:58 +02:00
agent-start-or-restart () {
local ssh_identity
local agentfile
local agentsocket
if [ -n "${1+x}" ]; then
ssh_identity="$1"
2020-09-21 13:00:56 +02:00
agentfile="${SSH_AGENTS_DIR}/agent-${ssh_identity}-$(hostname)"
2020-09-21 16:47:59 +02:00
agentsocket="${SSH_AGENT_SOCKETS_DIR}/socket-${ssh_identity}-$(hostname)"
2020-09-21 21:24:47 +02:00
logdebug "agentfile: $agentfile" >&2
logdebug "agentsocket: $agentsocket" >&2
logdebug ssh-identität: $ssh_identity >&2
2020-09-21 16:47:59 +02:00
if [ -e $agentfile ]; then
2020-09-21 12:39:58 +02:00
2020-09-21 16:47:59 +02:00
# look if agent is reachable
# local sshsock sshpid
# sshsock=$SSH_AUTH_SOCK
# sshpid=$SSH_AGENT_PID
# unset SSH_AUTH_SOCK SSH_AGENT_PID
2020-09-21 21:37:38 +02:00
logdebug "$(/bin/sh -c "unset SSH_AUTH_SOCK SSH_AGENT_PID; . $agentfile >/dev/null 2>&1; ssh-add -l")"
2020-09-21 16:47:59 +02:00
case $? in
0|1)
2020-09-21 21:24:47 +02:00
logdebug "agent is running" >&2
2020-09-21 16:47:59 +02:00
;;
2)
2020-09-21 21:24:47 +02:00
logdebug "agent is not running 1" >&2
2020-09-21 16:47:59 +02:00
[ -e $agentsocket ] && rm "$agentsocket"
ssh-agent -a $agentsocket $SSH_AGENT_OPTIONS > $agentfile 2>&2
2020-09-21 21:24:47 +02:00
logdebug "agent started" >&2
2020-09-21 16:47:59 +02:00
;;
esac
else
2020-09-21 21:24:47 +02:00
logdebug "agent is not running 2" >&2
2020-09-21 16:47:59 +02:00
#rm "$agentsocket"
2020-09-21 21:24:47 +02:00
logdebug ssh-agent -a $agentsocket \> $agentfile
2020-09-21 16:47:59 +02:00
ssh-agent -a $agentsocket $SSH_AGENT_OPTIONS > $agentfile 2>&2
2020-09-21 21:24:47 +02:00
logdebug "agent started" >&2
2020-09-21 12:39:58 +02:00
fi
2020-09-21 16:47:59 +02:00
2020-09-21 21:40:47 +02:00
loginfo "agent for $ssh_identity: $agentfile"
echo -n "agent for $ssh_identity: " >&2
2020-09-21 12:39:58 +02:00
echo $agentfile
return 0
2020-09-21 16:47:59 +02:00
2020-09-21 12:39:58 +02:00
else
2020-09-21 21:24:47 +02:00
logwarn no identity given - exit >&2
2020-09-21 12:39:58 +02:00
return 1
fi
}
2020-09-21 16:47:59 +02:00
agent-load-identity-keys () {
local ssh_identity
local agentfile
local agentsocket
local fingerprints
declare -a fingerprints
local fingerprint
local tokenfingerprint
if [ -n "${1+x}" ]; then
ssh_identity="$1"
2020-09-21 18:47:37 +02:00
identitydir=${SSH_IDENTITIES_DIR}/${ssh_identity}
[ -e "${identitydir}/.config" ] && . "${identitydir}/.config"
2020-09-21 16:47:59 +02:00
agentfile="${SSH_AGENTS_DIR}/agent-${ssh_identity}-$(hostname)"
agentsocket="${SSH_AGENT_SOCKETS_DIR}/socket-${ssh_identity}-$(hostname)"
2020-09-21 21:24:47 +02:00
logdebug "agentfile: $agentfile" >&2
logdebug "agentsocket: $agentsocket" >&2
2020-09-21 21:35:26 +02:00
logdebug "ssh-identität: $ssh_identity" >&2
2020-09-21 16:47:59 +02:00
fingerprints=( $(ssh-runinagent $agentfile "ssh-add -l|awk '{print \$2}'") )
for key in $(ls ${SSH_IDENTITIES_DIR}/${ssh_identity}|grep -v "pub$\|so$"); do
2020-09-21 21:37:38 +02:00
logdebug "key: $key"
2020-09-21 16:47:59 +02:00
fingerprint=$(ssh-keygen -l -f ~/.ssh/identities/bmi/id_ed25519|awk '{print $2}')
2020-09-21 21:37:38 +02:00
logdebug "${fingerprints[*]} and $fingerprint"
2020-09-21 16:47:59 +02:00
if [[ ${fingerprints[*]} =~ "$fingerprint" ]]; then
2020-09-21 21:24:47 +02:00
logdebug "$key is loaded" >&2
2020-09-21 16:47:59 +02:00
else
2020-09-21 21:24:47 +02:00
logdebug "$key is not loaded" >&2
2020-09-21 16:47:59 +02:00
ssh-runinagent $agentfile ssh-add ${SSH_ADD_OPTIONS} ${identitydir}/${key}
fi
done
for token in $(ls ${SSH_IDENTITIES_DIR}/${ssh_identity}|grep "so$"); do
2020-09-21 21:37:38 +02:00
logdebug "token: $token"
2020-09-21 16:47:59 +02:00
tokenfingerprint="$(ssh-keygen -l -D $token|tr -s ' '|awk '{print $2}')"
if [[ ${fingerprints[*]} =~ "$tokenfingerprint" ]]; then
2020-09-21 21:24:47 +02:00
logdebug "$token is loaded" >&2
2020-09-21 16:47:59 +02:00
else
2020-09-21 21:24:47 +02:00
logdebug "$token is not loaded" >&2
2020-09-21 16:47:59 +02:00
ssh-runinagent $agentfile ssh-add ${SSH_ADD_OPTIONS} -s ${identitydir}/${token}
fi
done
fi
}
ssh-runinagent () {
local agentfile
local command
agentfile=${1}
shift
sshcommand=${@}
2020-09-21 21:24:47 +02:00
logdebug "run command »$sshcommand« in agent $agentfile" >&2
2020-09-21 16:47:59 +02:00
if [ -e "$agentfile" ]; then
/bin/sh -c "unset SSH_AUTH_SOCK SSH_AGENT_PID; . $agentfile >/dev/null 2>/dev/null; $sshcommand"
return $?
else
2020-09-21 21:24:47 +02:00
logwarn "agentfile not existent" >&2
2020-09-21 16:47:59 +02:00
return 1
fi
}
2020-09-21 12:39:58 +02:00
agent-start-or-restart $1
2020-09-21 16:47:59 +02:00
agent-load-identity-keys $1
2020-09-21 12:39:58 +02:00
exit $?