static int connect_to_ssh(BDRVSSHState *s, QDict *options,

                          int ssh_flags, int creat_mode, Error **errp)

{

    int r, ret;

    const char *host, *user, *path, *host_key_check;

    int port;



    if (!qdict_haskey(options, "host")) {

        ret = -EINVAL;

        error_setg(errp, "No hostname was specified");

        goto err;

    }

    host = qdict_get_str(options, "host");



    if (qdict_haskey(options, "port")) {

        port = qdict_get_int(options, "port");

    } else {

        port = 22;

    }



    if (!qdict_haskey(options, "path")) {

        ret = -EINVAL;

        error_setg(errp, "No path was specified");

        goto err;

    }

    path = qdict_get_str(options, "path");



    if (qdict_haskey(options, "user")) {

        user = qdict_get_str(options, "user");

    } else {

        user = g_get_user_name();

        if (!user) {

            error_setg_errno(errp, errno, "Can't get user name");

            ret = -errno;

            goto err;

        }

    }



    if (qdict_haskey(options, "host_key_check")) {

        host_key_check = qdict_get_str(options, "host_key_check");

    } else {

        host_key_check = "yes";

    }



    /* Construct the host:port name for inet_connect. */

    g_free(s->hostport);

    s->hostport = g_strdup_printf("%s:%d", host, port);



    /* Open the socket and connect. */

    s->sock = inet_connect(s->hostport, errp);

    if (s->sock < 0) {

        ret = -EIO;

        goto err;

    }



    /* Create SSH session. */

    s->session = libssh2_session_init();

    if (!s->session) {

        ret = -EINVAL;

        session_error_setg(errp, s, "failed to initialize libssh2 session");

        goto err;

    }



#if TRACE_LIBSSH2 != 0

    libssh2_trace(s->session, TRACE_LIBSSH2);

#endif



    r = libssh2_session_handshake(s->session, s->sock);

    if (r != 0) {

        ret = -EINVAL;

        session_error_setg(errp, s, "failed to establish SSH session");

        goto err;

    }



    /* Check the remote host's key against known_hosts. */

    ret = check_host_key(s, host, port, host_key_check, errp);

    if (ret < 0) {

        goto err;

    }



    /* Authenticate. */

    ret = authenticate(s, user, errp);

    if (ret < 0) {

        goto err;

    }



    /* Start SFTP. */

    s->sftp = libssh2_sftp_init(s->session);

    if (!s->sftp) {

        session_error_setg(errp, s, "failed to initialize sftp handle");

        ret = -EINVAL;

        goto err;

    }



    /* Open the remote file. */

    DPRINTF("opening file %s flags=0x%x creat_mode=0%o",

            path, ssh_flags, creat_mode);

    s->sftp_handle = libssh2_sftp_open(s->sftp, path, ssh_flags, creat_mode);

    if (!s->sftp_handle) {

        session_error_setg(errp, s, "failed to open remote file '%s'", path);

        ret = -EINVAL;

        goto err;

    }



    r = libssh2_sftp_fstat(s->sftp_handle, &s->attrs);

    if (r < 0) {

        sftp_error_setg(errp, s, "failed to read file attributes");

        return -EINVAL;

    }



    /* Delete the options we've used; any not deleted will cause the

     * block layer to give an error about unused options.

     */

    qdict_del(options, "host");

    qdict_del(options, "port");

    qdict_del(options, "user");

    qdict_del(options, "path");

    qdict_del(options, "host_key_check");



    return 0;



 err:

    if (s->sftp_handle) {

        libssh2_sftp_close(s->sftp_handle);

    }

    s->sftp_handle = NULL;

    if (s->sftp) {

        libssh2_sftp_shutdown(s->sftp);

    }

    s->sftp = NULL;

    if (s->session) {

        libssh2_session_disconnect(s->session,

                                   "from qemu ssh client: "

                                   "error opening connection");

        libssh2_session_free(s->session);

    }

    s->session = NULL;



    return ret;

}

static int connect_to_ssh(BDRVSSHState *s, QDict *options,

                          int ssh_flags, int creat_mode, Error **errp)

{

    int r, ret;

    const char *host, *user, *path, *host_key_check;

    int port;



    if (!qdict_haskey(options, "host")) {

        ret = -EINVAL;

        error_setg(errp, "No hostname was specified");

        goto err;

    }

    host = qdict_get_str(options, "host");



    if (qdict_haskey(options, "port")) {

        port = qdict_get_int(options, "port");

    } else {

        port = 22;

    }



    if (!qdict_haskey(options, "path")) {

        ret = -EINVAL;

        error_setg(errp, "No path was specified");

        goto err;

    }

    path = qdict_get_str(options, "path");



    if (qdict_haskey(options, "user")) {

        user = qdict_get_str(options, "user");

    } else {

        user = g_get_user_name();

        if (!user) {

            error_setg_errno(errp, errno, "Can't get user name");

            ret = -errno;

            goto err;

        }

    }



    if (qdict_haskey(options, "host_key_check")) {

        host_key_check = qdict_get_str(options, "host_key_check");

    } else {

        host_key_check = "yes";

    }



    

    g_free(s->hostport);

    s->hostport = g_strdup_printf("%s:%d", host, port);



    

    s->sock = inet_connect(s->hostport, errp);

    if (s->sock < 0) {

        ret = -EIO;

        goto err;

    }



    

    s->session = libssh2_session_init();

    if (!s->session) {

        ret = -EINVAL;

        session_error_setg(errp, s, "failed to initialize libssh2 session");

        goto err;

    }



#if TRACE_LIBSSH2 != 0

    libssh2_trace(s->session, TRACE_LIBSSH2);

#endif



    r = libssh2_session_handshake(s->session, s->sock);

    if (r != 0) {

        ret = -EINVAL;

        session_error_setg(errp, s, "failed to establish SSH session");

        goto err;

    }



    

    ret = check_host_key(s, host, port, host_key_check, errp);

    if (ret < 0) {

        goto err;

    }



    

    ret = authenticate(s, user, errp);

    if (ret < 0) {

        goto err;

    }



    

    s->sftp = libssh2_sftp_init(s->session);

    if (!s->sftp) {

        session_error_setg(errp, s, "failed to initialize sftp handle");

        ret = -EINVAL;

        goto err;

    }



    

    DPRINTF("opening file %s flags=0x%x creat_mode=0%o",

            path, ssh_flags, creat_mode);

    s->sftp_handle = libssh2_sftp_open(s->sftp, path, ssh_flags, creat_mode);

    if (!s->sftp_handle) {

        session_error_setg(errp, s, "failed to open remote file '%s'", path);

        ret = -EINVAL;

        goto err;

    }



    r = libssh2_sftp_fstat(s->sftp_handle, &s->attrs);

    if (r < 0) {

        sftp_error_setg(errp, s, "failed to read file attributes");

        return -EINVAL;

    }



    





    qdict_del(options, "host");

    qdict_del(options, "port");

    qdict_del(options, "user");

    qdict_del(options, "path");

    qdict_del(options, "host_key_check");



    return 0;



 err:

    if (s->sftp_handle) {

        libssh2_sftp_close(s->sftp_handle);

    }

    s->sftp_handle = NULL;

    if (s->sftp) {

        libssh2_sftp_shutdown(s->sftp);

    }

    s->sftp = NULL;

    if (s->session) {

        libssh2_session_disconnect(s->session,

                                   "from qemu ssh client: "

                                   "error opening connection");

        libssh2_session_free(s->session);

    }

    s->session = NULL;



    return ret;

}

    if (!qdict_haskey(options, "host")) {         error_setg(errp, "No hostname was specified");     host = qdict_get_str(options, "host");     if (qdict_haskey(options, "port")) {         port = qdict_get_int(options, "port");     } else {         port = 22;     if (!qdict_haskey(options, "path")) {     path = qdict_get_str(options, "path");     if (qdict_haskey(options, "user")) {         user = qdict_get_str(options, "user");     } else {     if (qdict_haskey(options, "host_key_check")) {         host_key_check = qdict_get_str(options, "host_key_check");     } else {     qdict_del(options, "host");     qdict_del(options, "port");     qdict_del(options, "user");     qdict_del(options, "path");     qdict_del(options, "host_key_check");
static int FUNC_0(BDRVSSHState *VAR_0, QDict *VAR_1,

                          int VAR_2, int VAR_3, Error **VAR_4)

{

    int VAR_5, VAR_6;

    const char *VAR_7, *VAR_8, *VAR_9, *VAR_10;

    int VAR_11;



    if (!qdict_haskey(VAR_1, "VAR_7")) {

        VAR_6 = -EINVAL;

        error_setg(VAR_4, "No hostname was specified");

        goto err;

    }

    VAR_7 = qdict_get_str(VAR_1, "VAR_7");



    if (qdict_haskey(VAR_1, "VAR_11")) {

        VAR_11 = qdict_get_int(VAR_1, "VAR_11");

    } else {

        VAR_11 = 22;

    }



    if (!qdict_haskey(VAR_1, "VAR_9")) {

        VAR_6 = -EINVAL;

        error_setg(VAR_4, "No VAR_9 was specified");

        goto err;

    }

    VAR_9 = qdict_get_str(VAR_1, "VAR_9");



    if (qdict_haskey(VAR_1, "VAR_8")) {

        VAR_8 = qdict_get_str(VAR_1, "VAR_8");

    } else {

        VAR_8 = g_get_user_name();

        if (!VAR_8) {

            error_setg_errno(VAR_4, errno, "Can't get VAR_8 name");

            VAR_6 = -errno;

            goto err;

        }

    }



    if (qdict_haskey(VAR_1, "VAR_10")) {

        VAR_10 = qdict_get_str(VAR_1, "VAR_10");

    } else {

        VAR_10 = "yes";

    }



    

    g_free(VAR_0->hostport);

    VAR_0->hostport = g_strdup_printf("%VAR_0:%d", VAR_7, VAR_11);



    

    VAR_0->sock = inet_connect(VAR_0->hostport, VAR_4);

    if (VAR_0->sock < 0) {

        VAR_6 = -EIO;

        goto err;

    }



    

    VAR_0->session = libssh2_session_init();

    if (!VAR_0->session) {

        VAR_6 = -EINVAL;

        session_error_setg(VAR_4, VAR_0, "failed to initialize libssh2 session");

        goto err;

    }



#if TRACE_LIBSSH2 != 0

    libssh2_trace(VAR_0->session, TRACE_LIBSSH2);

#endif



    VAR_5 = libssh2_session_handshake(VAR_0->session, VAR_0->sock);

    if (VAR_5 != 0) {

        VAR_6 = -EINVAL;

        session_error_setg(VAR_4, VAR_0, "failed to establish SSH session");

        goto err;

    }



    

    VAR_6 = check_host_key(VAR_0, VAR_7, VAR_11, VAR_10, VAR_4);

    if (VAR_6 < 0) {

        goto err;

    }



    

    VAR_6 = authenticate(VAR_0, VAR_8, VAR_4);

    if (VAR_6 < 0) {

        goto err;

    }



    

    VAR_0->sftp = libssh2_sftp_init(VAR_0->session);

    if (!VAR_0->sftp) {

        session_error_setg(VAR_4, VAR_0, "failed to initialize sftp handle");

        VAR_6 = -EINVAL;

        goto err;

    }



    

    DPRINTF("opening file %VAR_0 flags=0x%x VAR_3=0%o",

            VAR_9, VAR_2, VAR_3);

    VAR_0->sftp_handle = libssh2_sftp_open(VAR_0->sftp, VAR_9, VAR_2, VAR_3);

    if (!VAR_0->sftp_handle) {

        session_error_setg(VAR_4, VAR_0, "failed to open remote file '%VAR_0'", VAR_9);

        VAR_6 = -EINVAL;

        goto err;

    }



    VAR_5 = libssh2_sftp_fstat(VAR_0->sftp_handle, &VAR_0->attrs);

    if (VAR_5 < 0) {

        sftp_error_setg(VAR_4, VAR_0, "failed to read file attributes");

        return -EINVAL;

    }



    





    qdict_del(VAR_1, "VAR_7");

    qdict_del(VAR_1, "VAR_11");

    qdict_del(VAR_1, "VAR_8");

    qdict_del(VAR_1, "VAR_9");

    qdict_del(VAR_1, "VAR_10");



    return 0;



 err:

    if (VAR_0->sftp_handle) {

        libssh2_sftp_close(VAR_0->sftp_handle);

    }

    VAR_0->sftp_handle = NULL;

    if (VAR_0->sftp) {

        libssh2_sftp_shutdown(VAR_0->sftp);

    }

    VAR_0->sftp = NULL;

    if (VAR_0->session) {

        libssh2_session_disconnect(VAR_0->session,

                                   "from qemu ssh client: "

                                   "error opening connection");

        libssh2_session_free(VAR_0->session);

    }

    VAR_0->session = NULL;



    return VAR_6;

}

static int FUNC_0(BDRVSSHState *VAR_0, QDict *VAR_1,
int VAR_2, int VAR_3, Error **VAR_4)
{ int VAR_5, VAR_6; const char *VAR_7, *VAR_8, *VAR_9, *VAR_10; int VAR_11; if (!qdict_haskey(VAR_1, "VAR_7")) { VAR_6 = -EINVAL; error_setg(VAR_4, "No hostname was specified"); goto err; } VAR_7 = qdict_get_str(VAR_1, "VAR_7"); if (qdict_haskey(VAR_1, "VAR_11")) { VAR_11 = qdict_get_int(VAR_1, "VAR_11"); } else { VAR_11 = 22; } if (!qdict_haskey(VAR_1, "VAR_9")) { VAR_6 = -EINVAL; error_setg(VAR_4, "No VAR_9 was specified"); goto err; } VAR_9 = qdict_get_str(VAR_1, "VAR_9"); if (qdict_haskey(VAR_1, "VAR_8")) { VAR_8 = qdict_get_str(VAR_1, "VAR_8"); } else { VAR_8 = g_get_user_name(); if (!VAR_8) { error_setg_errno(VAR_4, errno, "Can't get VAR_8 name"); VAR_6 = -errno; goto err; } } if (qdict_haskey(VAR_1, "VAR_10")) { VAR_10 = qdict_get_str(VAR_1, "VAR_10"); } else { VAR_10 = "yes"; } g_free(VAR_0->hostport); VAR_0->hostport = g_strdup_printf("%VAR_0:%d", VAR_7, VAR_11); VAR_0->sock = inet_connect(VAR_0->hostport, VAR_4); if (VAR_0->sock < 0) { VAR_6 = -EIO; goto err; } VAR_0->session = libssh2_session_init(); if (!VAR_0->session) { VAR_6 = -EINVAL; session_error_setg(VAR_4, VAR_0, "failed to initialize libssh2 session"); goto err; } #if TRACE_LIBSSH2 != 0
libssh2_trace(VAR_0->session, TRACE_LIBSSH2); #endif
VAR_5 = libssh2_session_handshake(VAR_0->session, VAR_0->sock); if (VAR_5 != 0) { VAR_6 = -EINVAL; session_error_setg(VAR_4, VAR_0, "failed to establish SSH session"); goto err; } VAR_6 = check_host_key(VAR_0, VAR_7, VAR_11, VAR_10, VAR_4); if (VAR_6 < 0) { goto err; } VAR_6 = authenticate(VAR_0, VAR_8, VAR_4); if (VAR_6 < 0) { goto err; } VAR_0->sftp = libssh2_sftp_init(VAR_0->session); if (!VAR_0->sftp) { session_error_setg(VAR_4, VAR_0, "failed to initialize sftp handle"); VAR_6 = -EINVAL; goto err; } DPRINTF("opening file %VAR_0 flags=0x%x VAR_3=0%o",
VAR_9, VAR_2, VAR_3); VAR_0->sftp_handle = libssh2_sftp_open(VAR_0->sftp, VAR_9, VAR_2, VAR_3); if (!VAR_0->sftp_handle) { session_error_setg(VAR_4, VAR_0, "failed to open remote file '%VAR_0'", VAR_9); VAR_6 = -EINVAL; goto err; } VAR_5 = libssh2_sftp_fstat(VAR_0->sftp_handle, &VAR_0->attrs); if (VAR_5 < 0) { sftp_error_setg(VAR_4, VAR_0, "failed to read file attributes"); return -EINVAL; } qdict_del(VAR_1, "VAR_7"); qdict_del(VAR_1, "VAR_11"); qdict_del(VAR_1, "VAR_8"); qdict_del(VAR_1, "VAR_9"); qdict_del(VAR_1, "VAR_10"); return 0; err:
if (VAR_0->sftp_handle) { libssh2_sftp_close(VAR_0->sftp_handle); } VAR_0->sftp_handle = NULL; if (VAR_0->sftp) { libssh2_sftp_shutdown(VAR_0->sftp); } VAR_0->sftp = NULL; if (VAR_0->session) { libssh2_session_disconnect(VAR_0->session,
"from qemu ssh client: "
"error opening connection"); libssh2_session_free(VAR_0->session); } VAR_0->session = NULL; return VAR_6; }