当尝试在 Linux 中运行 libssh 服务器时,我遇到了错误侦听套接字:无法导入私有 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: %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;
}
Run Code Online (Sandbox Code Playgroud)
侦听套接字时出错:无法导入私有 RSA 主机密钥
我还尝试以多种方式直接使用 ssh_host_rsa_key 的内容作为变量。官方参考文档有这一行
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key")
Run Code Online (Sandbox Code Playgroud)
我也将其与 #define KEYS_FILE if else 块一起使用,当我尝试不同的方法时我将其注释掉。现在我的私钥与我的服务器位于同一目录中(出于沮丧)。
非常感谢任何提示或提示!
万一有人遇到这个问题,我的解决方案是生成新的 ssh 密钥并将我的代码链接到这些密钥。无论出于何种原因,我第一次生成的密钥不起作用,但删除它们并使用新的一对就可以了。没有人会读到这些文字。