libssh服务器无法导入专用RSA主机密钥

问题描述 投票:1回答:1

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

我仍然是一个温柔的C练习者,所以我很确定我做错了什么。也许即使是自己(我相信的创造者)也会给我一两个快速的提示。这是我的代码:

#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;
}

错误侦听套接字:无法导入专用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块,当我尝试不同的方法时,我注释掉了。现在我的私钥与我的服务器位于同一目录中(出于挫折)。

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

c ssh libssh
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.