static int check_host_key_knownhosts(BDRVSSHState *s,

                                     const char *host, int port)

{

    const char *home;

    char *knh_file = NULL;

    LIBSSH2_KNOWNHOSTS *knh = NULL;

    struct libssh2_knownhost *found;

    int ret, r;

    const char *hostkey;

    size_t len;

    int type;



    hostkey = libssh2_session_hostkey(s->session, &len, &type);

    if (!hostkey) {

        ret = -EINVAL;

        session_error_report(s, "failed to read remote host key");

        goto out;

    }



    knh = libssh2_knownhost_init(s->session);

    if (!knh) {

        ret = -EINVAL;

        session_error_report(s, "failed to initialize known hosts support");

        goto out;

    }



    home = getenv("HOME");

    if (home) {

        knh_file = g_strdup_printf("%s/.ssh/known_hosts", home);

    } else {

        knh_file = g_strdup_printf("/root/.ssh/known_hosts");

    }



    /* Read all known hosts from OpenSSH-style known_hosts file. */

    libssh2_knownhost_readfile(knh, knh_file, LIBSSH2_KNOWNHOST_FILE_OPENSSH);



    r = libssh2_knownhost_checkp(knh, host, port, hostkey, len,

                                 LIBSSH2_KNOWNHOST_TYPE_PLAIN|

                                 LIBSSH2_KNOWNHOST_KEYENC_RAW,

                                 &found);

    switch (r) {

    case LIBSSH2_KNOWNHOST_CHECK_MATCH:

        /* OK */

        DPRINTF("host key OK: %s", found->key);

        break;

    case LIBSSH2_KNOWNHOST_CHECK_MISMATCH:

        ret = -EINVAL;

        session_error_report(s, "host key does not match the one in known_hosts (found key %s)",

                             found->key);

        goto out;

    case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND:

        ret = -EINVAL;

        session_error_report(s, "no host key was found in known_hosts");

        goto out;

    case LIBSSH2_KNOWNHOST_CHECK_FAILURE:

        ret = -EINVAL;

        session_error_report(s, "failure matching the host key with known_hosts");

        goto out;

    default:

        ret = -EINVAL;

        session_error_report(s, "unknown error matching the host key with known_hosts (%d)",

                             r);

        goto out;

    }



    /* known_hosts checking successful. */

    ret = 0;



 out:

    if (knh != NULL) {

        libssh2_knownhost_free(knh);

    }

    g_free(knh_file);

    return ret;

}

static int check_host_key_knownhosts(BDRVSSHState *s,

                                     const char *host, int port)

{

    const char *home;

    char *knh_file = NULL;

    LIBSSH2_KNOWNHOSTS *knh = NULL;

    struct libssh2_knownhost *found;

    int ret, r;

    const char *hostkey;

    size_t len;

    int type;



    hostkey = libssh2_session_hostkey(s->session, &len, &type);

    if (!hostkey) {

        ret = -EINVAL;

        session_error_report(s, "failed to read remote host key");

        goto out;

    }



    knh = libssh2_knownhost_init(s->session);

    if (!knh) {

        ret = -EINVAL;

        session_error_report(s, "failed to initialize known hosts support");

        goto out;

    }



    home = getenv("HOME");

    if (home) {

        knh_file = g_strdup_printf("%s/.ssh/known_hosts", home);

    } else {

        knh_file = g_strdup_printf("/root/.ssh/known_hosts");

    }



    

    libssh2_knownhost_readfile(knh, knh_file, LIBSSH2_KNOWNHOST_FILE_OPENSSH);



    r = libssh2_knownhost_checkp(knh, host, port, hostkey, len,

                                 LIBSSH2_KNOWNHOST_TYPE_PLAIN|

                                 LIBSSH2_KNOWNHOST_KEYENC_RAW,

                                 &found);

    switch (r) {

    case LIBSSH2_KNOWNHOST_CHECK_MATCH:

        

        DPRINTF("host key OK: %s", found->key);

        break;

    case LIBSSH2_KNOWNHOST_CHECK_MISMATCH:

        ret = -EINVAL;

        session_error_report(s, "host key does not match the one in known_hosts (found key %s)",

                             found->key);

        goto out;

    case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND:

        ret = -EINVAL;

        session_error_report(s, "no host key was found in known_hosts");

        goto out;

    case LIBSSH2_KNOWNHOST_CHECK_FAILURE:

        ret = -EINVAL;

        session_error_report(s, "failure matching the host key with known_hosts");

        goto out;

    default:

        ret = -EINVAL;

        session_error_report(s, "unknown error matching the host key with known_hosts (%d)",

                             r);

        goto out;

    }



    

    ret = 0;



 out:

    if (knh != NULL) {

        libssh2_knownhost_free(knh);

    }

    g_free(knh_file);

    return ret;

}

static int FUNC_0(BDRVSSHState *VAR_0,

                                     const char *VAR_1, int VAR_2)

{

    const char *VAR_3;

    char *VAR_4 = NULL;

    LIBSSH2_KNOWNHOSTS *knh = NULL;

    struct libssh2_knownhost *VAR_5;

    int VAR_6, VAR_7;

    const char *VAR_8;

    size_t len;

    int VAR_9;



    VAR_8 = libssh2_session_hostkey(VAR_0->session, &len, &VAR_9);

    if (!VAR_8) {

        VAR_6 = -EINVAL;

        session_error_report(VAR_0, "failed to read remote VAR_1 key");

        goto out;

    }



    knh = libssh2_knownhost_init(VAR_0->session);

    if (!knh) {

        VAR_6 = -EINVAL;

        session_error_report(VAR_0, "failed to initialize known hosts support");

        goto out;

    }



    VAR_3 = getenv("HOME");

    if (VAR_3) {

        VAR_4 = g_strdup_printf("%VAR_0/.ssh/known_hosts", VAR_3);

    } else {

        VAR_4 = g_strdup_printf("/root/.ssh/known_hosts");

    }



    

    libssh2_knownhost_readfile(knh, VAR_4, LIBSSH2_KNOWNHOST_FILE_OPENSSH);



    VAR_7 = libssh2_knownhost_checkp(knh, VAR_1, VAR_2, VAR_8, len,

                                 LIBSSH2_KNOWNHOST_TYPE_PLAIN|

                                 LIBSSH2_KNOWNHOST_KEYENC_RAW,

                                 &VAR_5);

    switch (VAR_7) {

    case LIBSSH2_KNOWNHOST_CHECK_MATCH:

        

        DPRINTF("VAR_1 key OK: %VAR_0", VAR_5->key);

        break;

    case LIBSSH2_KNOWNHOST_CHECK_MISMATCH:

        VAR_6 = -EINVAL;

        session_error_report(VAR_0, "VAR_1 key does not match the one in known_hosts (VAR_5 key %VAR_0)",

                             VAR_5->key);

        goto out;

    case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND:

        VAR_6 = -EINVAL;

        session_error_report(VAR_0, "no VAR_1 key was VAR_5 in known_hosts");

        goto out;

    case LIBSSH2_KNOWNHOST_CHECK_FAILURE:

        VAR_6 = -EINVAL;

        session_error_report(VAR_0, "failure matching the VAR_1 key with known_hosts");

        goto out;

    default:

        VAR_6 = -EINVAL;

        session_error_report(VAR_0, "unknown error matching the VAR_1 key with known_hosts (%d)",

                             VAR_7);

        goto out;

    }



    

    VAR_6 = 0;



 out:

    if (knh != NULL) {

        libssh2_knownhost_free(knh);

    }

    g_free(VAR_4);

    return VAR_6;

}

static int FUNC_0(BDRVSSHState *VAR_0,
const char *VAR_1, int VAR_2)
{ const char *VAR_3; char *VAR_4 = NULL; LIBSSH2_KNOWNHOSTS *knh = NULL; struct libssh2_knownhost *VAR_5; int VAR_6, VAR_7; const char *VAR_8; size_t len; int VAR_9; VAR_8 = libssh2_session_hostkey(VAR_0->session, &len, &VAR_9); if (!VAR_8) { VAR_6 = -EINVAL; session_error_report(VAR_0, "failed to read remote VAR_1 key"); goto out; } knh = libssh2_knownhost_init(VAR_0->session); if (!knh) { VAR_6 = -EINVAL; session_error_report(VAR_0, "failed to initialize known hosts support"); goto out; } VAR_3 = getenv("HOME"); if (VAR_3) { VAR_4 = g_strdup_printf("%VAR_0/.ssh/known_hosts", VAR_3); } else { VAR_4 = g_strdup_printf("/root/.ssh/known_hosts"); } libssh2_knownhost_readfile(knh, VAR_4, LIBSSH2_KNOWNHOST_FILE_OPENSSH); VAR_7 = libssh2_knownhost_checkp(knh, VAR_1, VAR_2, VAR_8, len,
LIBSSH2_KNOWNHOST_TYPE_PLAIN|
LIBSSH2_KNOWNHOST_KEYENC_RAW,
&VAR_5); switch (VAR_7) { case LIBSSH2_KNOWNHOST_CHECK_MATCH:
DPRINTF("VAR_1 key OK: %VAR_0", VAR_5->key); break; case LIBSSH2_KNOWNHOST_CHECK_MISMATCH:
VAR_6 = -EINVAL; session_error_report(VAR_0, "VAR_1 key does not match the one in known_hosts (VAR_5 key %VAR_0)",
VAR_5->key); goto out; case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND:
VAR_6 = -EINVAL; session_error_report(VAR_0, "no VAR_1 key was VAR_5 in known_hosts"); goto out; case LIBSSH2_KNOWNHOST_CHECK_FAILURE:
VAR_6 = -EINVAL; session_error_report(VAR_0, "failure matching the VAR_1 key with known_hosts"); goto out; default:
VAR_6 = -EINVAL; session_error_report(VAR_0, "unknown error matching the VAR_1 key with known_hosts (%d)",
VAR_7); goto out; } VAR_6 = 0; out:
if (knh != NULL) { libssh2_knownhost_free(knh); } g_free(VAR_4); return VAR_6; }