C语言 libssh 服务器无法导入私有 RSA 主机密钥



在尝试让libssh服务器在Linux中运行时,我被捕获到侦听套接字时出错:无法导入私有RSA主机密钥。我用两个例子作为参考。https://github.com/substack/libssh/blob/master/examples/samplesshd.c 和 https://github.com/PeteMo/sshpot/blob/master/main.c。但是后一个参考提到了使用公钥,而不是自述文件中的私有密钥,这让我感到困惑。

我仍然是一个温柔的C从业者,所以我非常确定我做错了什么。也许甚至asm本人(我相信是创造者(也会给我一两个快速提示。这是我的代码:

#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: %sn", 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;
}

侦听套接字时出错:无法导入专用 RSA 主机密钥

我还尝试以多种方式将ssh_host_rsa_key的内容直接用作变量。官方参考文档有这一行

ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key")

我也将其与尝试不同方法时注释掉的 #define KEYS_FILE if else 块一起使用。现在我的私钥与我的服务器位于同一目录中(出于沮丧(。

任何提示或提示非常感谢!

万一有人遇到这个问题,我的解决方案是生成新的 ssh 密钥并将我的代码链接到这些密钥。无论出于何种原因,我第一次生成的密钥不起作用,但删除它们并使用一对新密钥起作用。 没有人会读到这些话。

相关内容

最新更新