Browse Source

overhaul ssh-keyman by addressing recently popped up issues

master
parent
commit
b569ce4f38
  1. 106
      openssh/ssh-keyman

106
openssh/ssh-keyman

@ -100,39 +100,49 @@ export SSH_AGENT_PID
# perform agent information sanity check # perform agent information sanity check
check_agent_info () { check_agent_info () {
location="$1" context="$1"
if [ ".$SSH_AUTH_SOCK" != . -o ".$SSH_AGENT_PID" != . ]; then invalid1=""
invalid="" invalid2=""
if [ ".$SSH_AUTH_SOCK" = . -o ".$SSH_AGENT_PID" = . ]; then if [ ".$SSH_AUTH_SOCK" != . ]; then
# just one of the two information is not enough # make sure the agent socket is (still) working
invalid="partial agent information missing" if [ ! -r $SSH_AUTH_SOCK ]; then
invalid1="agent socket $SSH_AUTH_SOCK no longer exists"
else else
ssh-add -l >/dev/null 2>&1
if [ $? -eq 2 ]; then
invalid1="agent socket $SSH_AUTH_SOCK no longer valid"
fi
fi
fi
if [ ".$SSH_AGENT_PID" != . ]; then
# make sure the agent process is (still) running # make sure the agent process is (still) running
kill -0 $SSH_AGENT_PID >/dev/null 2>&1 kill -0 $SSH_AGENT_PID >/dev/null 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
invalid="agent process $SSH_AGENT_PID is no longer valid" invalid2="agent process $SSH_AGENT_PID no longer exists"
else
if [ ! -r $SSH_AUTH_SOCK ]; then
invalid="agent socket $SSH_AUTH_SOCK is no longer existing"
fi fi
fi fi
if [ ".$invalid1" != . -o ".$invalid2" != . ]; then
if [ ".$opt_e" = .no ]; then
echo "$prog_name:WARNING: invalid agent setup found in $context." 1>&2
if [ ".$invalid1" != . ]; then
echo "$prog_name:WARNING: reason: $invalid1." 1>&2
fi fi
if [ ".$invalid" != . ]; then if [ ".$invalid2" != . ]; then
echo "$prog_name:WARNING: invalid agent information found in $location." 1>&2 echo "$prog_name:WARNING: reason: $invalid2." 1>&2
echo "$prog_name:WARNING: reason: $invalid." 1>&2 fi
if [ ".$location" = ".your shell environment" ]; then if [ ".$context" = ".your shell environment" ]; then
echo "$prog_name:HINT: run \"eval \`$prog_name -q -s -e\`\" to fix." 1>&2 echo "$prog_name:HINT: run \"eval \`$prog_name -q -s -e\`\" to fix." 1>&2
fi fi
fi
unset SSH_AUTH_SOCK unset SSH_AUTH_SOCK
unset SSH_AGENT_PID unset SSH_AGENT_PID
fi fi
fi
} }
check_agent_info "your shell environment" check_agent_info "your shell environment"
if [ -f $agentfile ]; then if [ -f $agentfile ]; then
. $agentfile . $agentfile
check_agent_info "in saved agent state" check_agent_info "in saved agent state"
if [ ".$SSH_AUTH_SOCK" = . -a ".$SSH_AGENT_PID" = . ]; then if [ ".$SSH_AUTH_SOCK" = . -o ".$SSH_AGENT_PID" = . ]; then
rm -f $agentfile rm -f $agentfile
fi fi
fi fi
@ -154,63 +164,73 @@ if [ ".$opt_k" = .yes ]; then
# stop the agent # stop the agent
kill=yes kill=yes
if [ ".$SSH_AUTH_SOCK" = . -a ".$SSH_AGENT_PID" = . ]; then if [ ".$SSH_AUTH_SOCK" != . -a ".$SSH_AGENT_PID" != . ]; then
if [ ".$opt_q" = .no ]; then
echo "$prog_name: agent not running"
fi
else
kill $SSH_AGENT_PID >/dev/null 2>&1 || true kill $SSH_AGENT_PID >/dev/null 2>&1 || true
if [ ".$opt_q" = .no ]; then if [ ".$opt_q" = .no ]; then
echo "$prog_name: stopped agent (pid $SSH_AGENT_PID)" echo "$prog_name: stopped agent (pid $SSH_AGENT_PID)" 1>&2
fi fi
rm -f $agentfile rm -f $agentfile
unset SSH_AUTH_SOCK unset SSH_AUTH_SOCK
unset SSH_AGENT_PID unset SSH_AGENT_PID
elif [ ".$SSH_AUTH_SOCK" != . -a ".$SSH_AGENT_PID" = . ]; then
if [ ".$opt_q" = .no ]; then
echo "$prog_name: agent running remotely - cannot kill locally" 1>&2
fi
else
if [ ".$opt_q" = .no ]; then
echo "$prog_name: agent not running" 1>&2
fi
fi fi
fi fi
# start the agent # start the agent
if [ ".$opt_s" = .yes ]; then if [ ".$opt_s" = .yes ]; then
if [ ".$SSH_AUTH_SOCK" != . -a ".$SSH_AGENT_PID" != . ]; then if [ ".$SSH_AUTH_SOCK" = . -a ".$SSH_AGENT_PID" = . ]; then
if [ ".$opt_q" = .no ]; then
echo "$prog_name: agent already running"
fi
else
eval `nohup $ssh_agent -s </dev/null 2>/dev/null | grep -v 'Agent pid'` eval `nohup $ssh_agent -s </dev/null 2>/dev/null | grep -v 'Agent pid'`
if [ ".$opt_q" != .yes ]; then if [ ".$opt_q" != .yes ]; then
echo "$prog_name: spawned agent (pid $SSH_AGENT_PID)" echo "$prog_name: spawned agent (pid $SSH_AGENT_PID)" 1>&2
fi fi
( echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK" ( echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK"
echo "SSH_AGENT_PID=$SSH_AGENT_PID" echo "SSH_AGENT_PID=$SSH_AGENT_PID"
) >$agentfile && chmod 600 $agentfile ) >$agentfile && chmod 600 $agentfile
elif [ ".$SSH_AUTH_SOCK" != . -a ".$SSH_AGENT_PID" = . ]; then
if [ ".$opt_q" = .no ]; then
echo "$prog_name: agent already running remotely - no need to start locally" 1>&2
fi
else
if [ ".$opt_q" = .no ]; then
echo "$prog_name: agent already running" 1>&2
fi
fi fi
fi fi
# setup environment # setup environment
if [ ".$opt_e" = .yes ]; then if [ ".$opt_e" = .yes ]; then
if [ ".$SSH_AUTH_SOCK" = . -a ".$SSH_AGENT_PID" = . ]; then if [ -r $agentfile ]; then
echo "$prog_name:WARNING: agent not running" 1>&2 sed -e 's/$/;/g' <$agentfile
echo "export SSH_AUTH_SOCK;"
echo "export SSH_AGENT_PID;"
else else
cat $agentfile echo "$prog_name:WARNING: agent not (or no longer) available" 1>&2
echo "export SSH_AUTH_SOCK" echo "unset SSH_AUTH_SOCK;"
echo "export SSH_AGENT_PID" echo "unset SSH_AGENT_PID;"
fi fi
fi fi
# delete key(s) from agent # delete key(s) from agent
if [ ".$opt_d" = .yes ]; then if [ ".$opt_d" = .yes ]; then
if [ ".$SSH_AUTH_SOCK" = . -a ".$SSH_AGENT_PID" = . ]; then if [ ".$SSH_AUTH_SOCK" = . ]; then
echo "$prog_name:WARNING: agent not running" 1>&2 echo "$prog_name:WARNING: agent not available" 1>&2
else else
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
if [ ".$opt_q" = .no ]; then if [ ".$opt_q" = .no ]; then
echo "$prog_name: deleting all keys" echo "$prog_name: deleting all keys" 1>&2
fi fi
$ssh_add -D $ssh_add -D
else else
if [ ".$opt_q" = .no ]; then if [ ".$opt_q" = .no ]; then
for key in "$@"; do for key in "$@"; do
echo "$prog_name: deleting key $key" echo "$prog_name: deleting key $key" 1>&2
done done
fi fi
$ssh_add -d "$@" $ssh_add -d "$@"
@ -220,8 +240,8 @@ fi
# add key(s) into agent # add key(s) into agent
if [ ".$opt_a" = .yes ]; then if [ ".$opt_a" = .yes ]; then
if [ ".$SSH_AUTH_SOCK" = . -a ".$SSH_AGENT_PID" = . ]; then if [ ".$SSH_AUTH_SOCK" = . ]; then
echo "$prog_name:WARNING: agent not running" 1>&2 echo "$prog_name:WARNING: agent not available" 1>&2
else else
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
echo "$prog_name:ERROR: no keys specified on command line" 1>&2 echo "$prog_name:ERROR: no keys specified on command line" 1>&2
@ -244,12 +264,12 @@ if [ ".$opt_a" = .yes ]; then
done done
if [ ".$load" = .yes ]; then if [ ".$load" = .yes ]; then
if [ ".$opt_q" = .no ]; then if [ ".$opt_q" = .no ]; then
echo "$prog_name: loading key $key_file" echo "$prog_name: loading key $key_file" 1>&2
fi fi
key_missing="$key_missing $key_file" key_missing="$key_missing $key_file"
else else
if [ ".$opt_q" = .no ]; then if [ ".$opt_q" = .no ]; then
echo "$prog_name: skipping key $key_file (already loaded)" echo "$prog_name: skipping key $key_file (already loaded)" 1>&2
fi fi
fi fi
done done
@ -261,8 +281,8 @@ fi
# list key(s) available in agent # list key(s) available in agent
if [ ".$opt_l" = .yes ]; then if [ ".$opt_l" = .yes ]; then
if [ ".$SSH_AUTH_SOCK" = . -a ".$SSH_AGENT_PID" = . ]; then if [ ".$SSH_AUTH_SOCK" = . ]; then
echo "$prog_name:WARNING: agent not running" 1>&2 echo "$prog_name:WARNING: agent not available" 1>&2
else else
$ssh_add -l $ssh_add -l
fi fi

Loading…
Cancel
Save