0

I have the following in my bashrc:

# Set up ssh-agent
SSH_ENV="$HOME/.ssh/environment"

function start_agent {
    echo "Initializing new SSH agent..."
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo succeeded
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
    /usr/bin/ssh-add;
}

# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" > /dev/null
    #ps ${SSH_AGENT_PID} doesn't work under cywgin
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

I know that start_agent is defined because I can type start_agent on the command-line and it "does the right thing". The problem is that I'm expecting start_agent to be executed on login and it is not executing. But I know that the control flow is passing through it because the function is defined.

Son of the Wai-Pan
  • 12,371
  • 16
  • 46
  • 55
  • 2
    Try just: `if [ condition ]; then echo "then branch "; start_agent; else echo "else branch"; start_agent; fi` if this works then it's your `ps -ef ....` line that is not doing what you expect. – RedX Mar 20 '14 at 08:43
  • agreed. O.P. can also use `set -vx` to turn on shell debug/trace. First it shows line or block of code that will be evaluated (as is, with`$var` words still in place`. then it displays (and often not in the order code is written) lines with the $var(s) expanded to their current values. Good luck to all. – shellter Mar 20 '14 at 11:39
  • @RedX Thanks for the debugging tip. I should've been more patient and did a line by line "echo". I think I spent too much time in front of my screen and just gave up (i.e. went to SO). If you're looking for some SO points, put your comment in an answer and I'll send you the points. – Son of the Wai-Pan Mar 20 '14 at 14:24
  • Your processing with `chmod` has a race condition -- an attacker could modify the file between its creation and the `chmod`. This isn't just theoretical; there are well-known demonstrations of this in practice. IMHO the best fix would be to get rid of the file altogether -- would something like `eval $(ssh-agent | sed 's/^echo/#&/')` work for you? – tripleee Mar 20 '14 at 15:46

1 Answers1

1

Try just:

if [ condition ]; then
  echo "then branch "
  start_agent
else
  echo "else branch"
  start_agent
fi

if this works then it's your ps -ef .... line that is not doing what you expect.

Additionally you can try what shellter suggests set -vx for an "echo style" debug.

RedX
  • 14,749
  • 1
  • 53
  • 76