3

I currently try to extend an libssh2 Wrapper in Objective-C.

I'm trying to implement the libssh2_userauth_keyboard_interactive method. My problem is the response callback.

I found this implementation on the net that bypasses the "real" interactivity and uses the actual password to make the authentication possible:

int error = libssh2_userauth_keyboard_interactive(session, [username UTF8String], &kbdCallback);

static void kbdCallback (const char *name, int name_len, const char *instruction, int instruction_len, int num_prompts, const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, void **abstract)
{
    responses[0].text = (char *)[password UTF8String];   // resp. (char *)[@"test" UTF8String]
    responses[0].length = strlen([password UTF8String]); // resp. (char *)[@"test" UTF8String]
}

One of my problems is to access the instance variable password within the static void call and my other problem is that I get SIGABRT when I try to call the method (I used a fixed string to test if that works).

Is there any possibility to get that working ?!

Julian

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107

2 Answers2

0

kbdCallback is not actually a method, it's a function - you can tell a couple of ways - there's no - or + in front of it, no parentheses around the return type, and also methods cannot be static. So, due to it being a function and not a method, there is no object associated with it, and no self pointer; thus you cannot get to any instance variables directly. There's a couple of ways to solve this I suppose; you could have a static instance of your object that the function could get the password from, or if there's some way to pass a context pointer to be used in the callback you might be able to pass an object in that way.

Regarding your SIGABRT, can you say which line exactly that happens on, and what the values of the arguments that you're using are? It's not clear from your question.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
  • Actually I cannot pass any context pointer to the function. When I change the passed parameters, I get a warning in line one (corresponding to my code from above): `incompatible pointer types passing`. The `SIABRT` happens on line one too. – Julian F. Weinert Oct 08 '12 at 07:48
  • @Julian you'll probably have to do something unpleasant like store the username and password in `static` variables while awaiting the callback. Also, can you say what the values of the arguments are being passed in to `libssh2_userauth_keyboard_interactive` when it crashes? – Carl Veazey Oct 10 '12 at 03:25
0

http://comments.gmane.org/gmane.network.ssh.libssh2.devel/4163

Cause: malloc-in-EXE-free-in-DLL under Win32.
Fix: Use custom free/malloc/realloc functions. Add below

static void *my_alloc(size_t count, void **abstract)            {   return malloc(count);}
static void my_free(void *ptr, void **abstract)                 {   free(ptr);}
static void *my_realloc(void *ptr, size_t count, void **abstract){  return realloc(ptr, count);}

And replace

libssh2_session_init();

with

libssh2_session_init_ex(my_alloc, my_free, my_realloc, NULL);