166 lines
4.5 KiB
Bash
Executable file
166 lines
4.5 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
FILELOGLEVEL=DEBUG
|
|
. $(dirname $0)/../logging
|
|
# loggerfactory
|
|
LANG=C
|
|
|
|
ssh_runinagent () {
|
|
# run ssh-* commands in wanted agent
|
|
|
|
if [ check_identity ]
|
|
then
|
|
local SSH_AUTH_SOCK
|
|
local SSH_AGENT_PID
|
|
local agentfile=$(get_agentfile)
|
|
shift
|
|
local sshcommand=${@}
|
|
|
|
logtrace "run command »$sshcommand« in agent $agentfile"
|
|
if [ -e "$agentfile" ]; then
|
|
. $agentfile
|
|
fi
|
|
logdebug "SSH_AUTH_SOCK: ${SSH_AUTH_SOCK:-x}"
|
|
if [ "${SSH_AUTH_SOCK:-x}" = "x" ]
|
|
then
|
|
start_or_restart_agent
|
|
. $agentfile
|
|
fi
|
|
$sshcommand
|
|
ret=$?
|
|
fi
|
|
}
|
|
|
|
check_identity () {
|
|
# is ssh-identity existing?
|
|
if [ -e ${SSH_IDENTITIES_DIR}/${ssh_identity} ]
|
|
then
|
|
return 0
|
|
else
|
|
logerr "Identity ${ssh_identity} is not definded. Create identity-directory ${SSH_IDENTITIES_DIR}/${ssh_identity}"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
get_agentfilename () {
|
|
printf "%s" "${SSH_AGENTS_DIR}/agent-${ssh_identity}"
|
|
}
|
|
get_agentsocketname () {
|
|
printf "%s" "${SSH_AGENT_SOCKETS_DIR}/socket-${ssh_identity}"
|
|
}
|
|
|
|
start_or_restart_agent () {
|
|
if check_identity
|
|
then
|
|
check_agent
|
|
ret=$?
|
|
logdebug "check_agent returncode $ret"
|
|
case $ret in
|
|
0)
|
|
logdebug "agent is running"
|
|
ret=0
|
|
;;
|
|
1)
|
|
logdebug "agent is running, but:"
|
|
ret=1
|
|
;;
|
|
2)
|
|
logdebug "former agent is not running -> start it"
|
|
logdebug "SSH_AGENT_OPTIONS: $SSH_AGENT_OPTIONS"
|
|
startagent=true
|
|
;;
|
|
3)
|
|
logwarning "Agent is not ok"
|
|
startagent=true
|
|
;;
|
|
*)
|
|
logwarning "Something else with agent"
|
|
ret=99
|
|
;;
|
|
esac
|
|
if ${startagent:-false}
|
|
then
|
|
ssh-agent -a $(get_agentsocketname) ${SSH_AGENT_OPTIONS} > $(get_agentfilename)
|
|
ret=$?
|
|
fi
|
|
ssh_runinagent ${ssh_identity} ssh-add -l >/dev/null 2>&1
|
|
if [ $? -eq 0 ]
|
|
then
|
|
printf "%s\n" "$(get_agentfilename)"
|
|
else
|
|
ret=92
|
|
fi
|
|
fi
|
|
}
|
|
|
|
check_agent () {
|
|
if [ -e $(get_agentsocketname) ]
|
|
then
|
|
ssh_runinagent ${ssh_identity} ssh-add -l >/dev/null 2>&1
|
|
return $?
|
|
fi
|
|
logdebug "agent socketname: $(get_agentsocketname)"
|
|
return 3
|
|
}
|
|
|
|
get_agentfile () {
|
|
if check_identity
|
|
then
|
|
printf "%s" "$(get_agentfilename)"
|
|
fi
|
|
}
|
|
|
|
get_agentsocket () {
|
|
if check_identity
|
|
then
|
|
printf "%s" "$(get_agentsocketname)"
|
|
fi
|
|
}
|
|
|
|
agent_load_identity_keys () {
|
|
|
|
# If DISPLAY is set, ssh-add calls ssh-askpass, and if its in remote-terminal, it wont work
|
|
# So remember and unset DISPLAY, and set it at the end again, if it was set before
|
|
[ $DISPLAY ] && local DISPLAY_ORIG=$DISPLAY
|
|
[ $DISPLAY ] && logtrace "unset DISPLAY: $DISPLAY"
|
|
[ $DISPLAY ] && unset DISPLAY
|
|
|
|
|
|
}
|
|
|
|
ssh_identity=${1:-default}
|
|
identitydir=${SSH_IDENTITIES_DIR}/${ssh_identity}
|
|
[ -z "${SSH_IDENTITIES_DIR+x}" ] && { export SSH_IDENTITIES_DIR="${SSH_IDENTITIES_DEFAULT_DIR-${HOME}/.ssh/identities}"; }
|
|
[ -z "${SSH_AGENTS_DIR+x}" ] && { export SSH_AGENTS_DIR=${SSH_AGENTS_DEFAULT_DIR-~/.ssh/agents}; }
|
|
[ -z "${SSH_AGENT_SOCKETS_DIR+x}" ] && { export SSH_AGENT_SOCKETS_DIR=${SSH_AGENT_SOCKETS_DEFAULT_DIR-~/.ssh/agents}; }
|
|
[ -z "${SSH_AGENT_OPTIONS+x}" ] && { export SSH_AGENT_OPTIONS=${SSH_AGENT_DEFAULT_OPTIONS--t 7200 }; }
|
|
|
|
export SSH_IDENTITIES_DIR SSH_AGENTS_DIR SSH_AGENT_SOCKETS_DIR SSH_AGENT_OPTIONS
|
|
|
|
#logdebug "<< EOF
|
|
# ssh identity: ${ssh_identity}
|
|
# identidies dir: $SSH_IDENTITIES_DIR
|
|
# agents dir: $SSH_AGENTS_DIR
|
|
# agents sockets dir: $SSH_AGENT_SOCKETS_DIR
|
|
# agents options: $SSH_AGENT_OPTIONS
|
|
# agentfile: $(get_agentfile ${ssh_identity})
|
|
# agentsocket: $(get_agentsocket ${ssh_identity})
|
|
#EOF
|
|
#"
|
|
#logtrace "Keys in your shells agent ${SSH_AUTH_SOCK}: $(ssh-add -l)"
|
|
logtrace "Keys in agent ${ssh_identity}: $(ssh_runinagent bmi ssh-add -l)"
|
|
|
|
|
|
createonly=false
|
|
|
|
if [[ $SSH_TTY || $X2GO_SESSION ]] ; then
|
|
logdebug "Shell running with forwarded ssh-agent. Please add local token manually"
|
|
check_token
|
|
res=1
|
|
else
|
|
logdebug "run with local ssh-agent"
|
|
start_or_restart_agent
|
|
! $createonly && agent_load_identity_keys $ssh_identity
|
|
res=0
|
|
fi
|
|
#exit $res
|