libssh2 - Agent Forwarding not working

2

1

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.

Niks

Posted 2015-08-18T09:58:23.603

Reputation: 741

do you have any error or verbose output. "not working" is quite broad definition. – Jakuje – 2015-08-18T10:13:22.703

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 – 2015-08-18T10:16:30.457

just for sure. Do you have agent running? – Jakuje – 2015-08-18T10:18:49.750

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 – 2015-08-18T10:22:19.170

@Jakuje any idea? – Niks – 2015-08-19T12:44:09.890

I see agent is running. Where do you add the keys to the agent? – Jakuje – 2015-08-19T13:00:38.480

I have added keys from my mac terminal. I am trying from my iOS app with libssh2 library. With libssh2 i am unable to connect with agent – Niks – 2015-08-19T13:53:28.230

yes, I understand, but how you add the keys in iOS app? – Jakuje – 2015-08-19T14:21:57.047

while connecting to server I am using key like const char *privatekey= [_privateKey cStringUsingEncoding:NSUTF8StringEncoding]; libssh2_userauth_publickey_fromfile(session, username, NULL,privatekey ,password); once connection does successfully I am trying to connect agent with that session and same user. – Niks – 2015-08-20T06:02:43.640

Answers

1

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.

Sandip Bhattacharya

Posted 2015-08-18T09:58:23.603

Reputation: 271

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 – 2015-08-20T06:18:05.200

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 – 2015-08-20T07:02:03.677

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 – 2015-08-20T07:10:23.213