can now authenticate, send exec, read response, close connection

This commit is contained in:
cy384 2020-06-18 21:21:24 -04:00
parent e6c8d28a09
commit 45c62a4b54
1 changed files with 49 additions and 34 deletions

View File

@ -150,9 +150,9 @@ static pascal void yield_notifier(void* contextPtr, OTEventCode code, OTResult r
void do_ssh_connection(void) void do_ssh_connection(void)
{ {
char* hostname = "10.0.2.2:22"; char* hostname = "10.0.2.2:22";
char* command = "uname";
char* username = "ssheven"; char* username = "ssheven";
char* password = "password"; char* password = "password";
char* command = "uname -a";
LIBSSH2_CHANNEL* channel; LIBSSH2_CHANNEL* channel;
LIBSSH2_SESSION* session; LIBSSH2_SESSION* session;
@ -175,7 +175,7 @@ void do_ssh_connection(void)
} }
else else
{ {
printf("got mem\n"); printf("got OT buffer\n");
} }
// open TCP endpoint // open TCP endpoint
@ -234,44 +234,38 @@ void do_ssh_connection(void)
return; return;
} }
// I think we stubbed this function out, lol
//libssh2_session_set_blocking(session, endpoint);
// replace 0 with the OT connection
rc = libssh2_session_handshake(session, endpoint); rc = libssh2_session_handshake(session, endpoint);
printf("handshake rc %s\n", libssh2_error_string(rc)); printf("handshake rc %s\n", libssh2_error_string(rc));
if (rc != LIBSSH2_ERROR_NONE) return; if (rc != LIBSSH2_ERROR_NONE) return;
//while((rc = libssh2_session_handshake(session, endpoint)) == LIBSSH2_ERROR_EAGAIN);
rc = libssh2_userauth_password(session, username, password); rc = libssh2_userauth_password(session, username, password);
printf("authenticate rc %s\n", libssh2_error_string(rc)); printf("authenticate rc %s\n", libssh2_error_string(rc));
if (rc != LIBSSH2_ERROR_NONE) return; if (rc != LIBSSH2_ERROR_NONE) return;
/* channel = libssh2_channel_open_session(session);
// are we required to look at the known hosts? see if we can skip for now printf("channel open: %d\n", channel);
while((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); printf("sending command \"%s\"\n", command);
assertp("pw login failed", !rc); rc = libssh2_channel_exec(channel, command);
printf("libssh2_channel_exec rc %s\n", libssh2_error_string(rc));
while((channel = libssh2_channel_open_session(session)) == NULL && libssh2_session_last_error(session, NULL, NULL, 0) == LIBSSH2_ERROR_EAGAIN) // read from the channel
rc = libssh2_channel_read(channel, buffer, buffer_size);
if (rc > 0)
{ {
;//waitsocket(sock, session); // need this? printf("got %d bytes:\n", rc);
for(int i = 0; i < rc; ++i) printf("%c", buffer[i]);
printf("\n");
}
else
{
printf("channel read error: %s\n", libssh2_error_string(rc));
} }
assertp("channel open failed", channel != 0);
while((rc = libssh2_channel_exec(channel, command)) == LIBSSH2_ERROR_EAGAIN); rc = libssh2_channel_close(channel);
assertp("exec channel open failed", rc == 0); printf("libssh2_channel_close rc %s\n", libssh2_error_string(rc));
libssh2_channel_free(channel);
// TODO: do the actual read here!
while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN);
assertp("channel close failed", rc == 0);
// skipping some other stuff here
*/
//libssh2_channel_free(channel);
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
@ -279,16 +273,37 @@ void do_ssh_connection(void)
libssh2_exit(); libssh2_exit();
// OT cleanup // request to close the TCP connection
rc = OTSndOrderlyDisconnect(endpoint);
assertp("OTSndOrderlyDisconnect failed", rc == noErr);
// get any remaining data so we can finish closing the connection
OTFlags ot_flags;
rc = 1;
while (rc != kOTLookErr)
{
rc = OTRcv(endpoint, buffer, 1, &ot_flags);
}
// finish closing the TCP connection
OTResult look_result = OTLook(endpoint);
switch (look_result)
{
case T_DISCONNECT:
err = OTRcvDisconnect(endpoint, nil);
break;
default:
printf("other connection error: %d\n", look_result);
break;
}
// release endpoint
result = OTUnbind(endpoint); result = OTUnbind(endpoint);
assertp("OTUnbind failed", result == noErr); assertp("OTUnbind failed", result == noErr);
// if we set up an endpoint, close it result = OTCloseProvider(endpoint);
if (endpoint != kOTInvalidEndpointRef) assertp("OTCloseProvider failed", result == noErr);
{
OSStatus result = OTCloseProvider(endpoint);
assertp("OTCloseProvider failed", result == noErr);
}
// if we got a buffer, release it // if we got a buffer, release it
if (buffer != nil) OTFreeMem(buffer); if (buffer != nil) OTFreeMem(buffer);
@ -298,7 +313,7 @@ void do_ssh_connection(void)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
printf("hello, world\n"); printf("starting up\n");
if (InitOpenTransport() != noErr) if (InitOpenTransport() != noErr)
{ {