I am using libssh2 library to ssh connections in my mobile application. Here I want to use Agent Forwarding support.

I have followed same procedure as they have provided in example here LibSSH2 Agent Forwarding.

I am able to create agent successfully but when I try to connect it with libssh2_agent_connect(agent) It gives me error -39 LIBSSH2_ERROR_BAD_USE.

Well I am checking same thing using MAC OSX terminal and it's working fine. Please suggest if anything wrong,

This is how I am trying once do successful connection

struct libssh2_agent_publickey *identity, *prev_identity = NULL;
int rc;
agent = libssh2_agent_init(session);

if (!agent)
    fprintf(stderr, "Failure initializing ssh-agent support\n");
    rc = 1;

int temp=libssh2_agent_connect(agent);
if (temp)
    fprintf(stderr, "Failure connecting to ssh-agent\n");
    rc = 1;

Is there anything wrong here? please suggest.

  • do you have any error or verbose output. "not working" is quite broad definition. – Jakuje Aug 18 '15 at 10:13
  • ohh sorry I missed that part, edited now in question also mentioning here. when I try to connect it with libssh2_agent_connect(agent) It gives me error -39 LIBSSH2_ERROR_BAD_USE. – Niks Aug 18 '15 at 10:16
  • just for sure. Do you have agent running? – Jakuje Aug 18 '15 at 10:18
  • Yes, It's running. I am able to ssh without ask/enter paraphrase when I try to ssh with my other machine where I have copied public key which I added to agent. – Niks Aug 18 '15 at 10:22
  • @Jakuje any idea? – Niks Aug 19 '15 at 12:44

From the code at https://github.com/libssh2/libssh2/blob/master/src/agent.c

path = getenv("SSH_AUTH_SOCK");
if (!path)
    return _libssh2_error(agent->session, LIBSSH2_ERROR_BAD_USE,
                          "no auth sock variable");

So obviously, this error is happening because you don't have the environment variable pointing to the agent socket set correctly. Have your code print the value of getenv("SSH_AUTH_SOCK") in case of an error to confirm this.

  • Thanks for the feedback, Yes path is NULL of variable. My SSH client is Mac osx can there be any difference with unix? What I am doing is first connecting with my PC then I am starting to connect with agent with same user and session. I am able to create agent but not able to connect. As I am getting NULL value for path of environment variable, can you please suggest me what can be wrong OR I am going with wrong flow? Thanks – Niks Aug 20 '15 at 6:18
  • Just to be clear again, you have your keys on your mac client, you are adding the keys to the ssh agent running in your Mac Terminal, and then ssh-ing to the PC. And then you are finding your SSH_AUTH_SOCK null in your PC in the same shell? In that case, agent forwarding in probably not working well. Trying adding a "-A" parameter when you are ssh-ing to the PC. If it still doesn't work, agent forwarding is probably disabled in the ssh server in your PC. Change the config there and restart sshd. Try doing the ssh again and see. – Sandip Bhattacharya Aug 20 '15 at 7:02
  • I am sshing from my app on same machine (Mac client) where I have added key to ssh agent. YEs then SSH_AUTH_SOCK path is NULL. I have tried from my mac terminal and agent forwarding is working. I have copied publick key to my Other (Second) Mac PC and tried to ssh from My Mac client (First) and it's done without asking me passphrase of that key means agent forwarding is working. But some from app may be my flow is wrong or else I am not getting exact reason. – Niks Aug 20 '15 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.