While attempting to get a libssh server running in linux, I am getting caught at the Error Listening to socket: Failed to import private RSA host key. I was using two examples as references. https://github.com/substack/libssh/blob/master/examples/samplesshd.c and https://github.com/PeteMo/sshpot/blob/master/main.c. But the latter reference mentioned using the public key, not private in the readme which has me confused.
I am still a tenderfoot C practitioner so I am very sure that I am doing something incorrectly. Maybe even asm himself(creator I believe) will grace me with a quick tip or two. Here is my code:
#include <libssh/libssh.h>
#include <libssh/server.h>
#include <libssh/callbacks.h>
#include <libssh/legacy.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define SSHD_USER "user"
#define SSHD_PASSWORD "password"
#define KEYS_FILE "./ssh_host_rsa_key"
/*#ifndef KEYS_FILE
#define KEYS_FILE
#else
#endif*/
static int auth_password(char *user, char *password)
{
if(strcmp(user, SSHD_USER)) {
return 0;
} else {
return 1;
}
if(strcmp(password, SSHD_PASSWORD)) {
return 0;
} else {
return 1;
}
return 0;
}
int main()
{
ssh_bind sshbind = ssh_bind_new();
ssh_session my_session = ssh_new();
int port = 900;
char *address = "127.0.0.1";
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, address);
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT_STR, &port);
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, "ssh-rsa");
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FILE);
if (ssh_bind_listen(sshbind) < 0) {
printf("Error listening to socket: %s\n", ssh_get_error(sshbind));
return 1;
}
/* Loop, waiting for and handling connection attempts. */
while(1) {
if (ssh_bind_accept(sshbind, my_session) == SSH_ERROR) {
fprintf(stderr, "Error accepting a connection: `%s'.\n",ssh_get_error(sshbind));
return -1;
} else {
printf("Accepted a connection.\n");
}
switch (fork()) {
case -1:
fprintf(stderr,"Fork returned error: `%d'.\n",-1);
exit(-1);
case 0:
exit(auth_password(SSHD_USER, SSHD_PASSWORD));
default:
break;
}
}
return 0;
}
Error Listening to socket: Failed to import private RSA host key
I also tried using the content of ssh_host_rsa_key directly as a variable in several ways. The official reference doc has this line
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key")
I used this too along with the #define KEYS_FILE if else block that I commented out when I tried a different approach. Right now my private key is in the same directory as my server(out of frustration).
Any tips or hints greatly appreciated!