libssh 在远程机器上打开 fle - SFTP 服务器权限被拒绝错误调试

问题描述

我使用服务器作为跳转主机(主机 A)以通过 ssh 连接到内部网络(主机 B)上的机器。

我使用这个 ssh 命令,它工作正常。

ssh -J user@hostA_IP.com root@hostB_IP.com

同样,我可以通过以下方式将文件 scp 到主机 B:

scp -o 'ProxyCommand ssh user@hostA_IP.com -W %h:%p' file.txt root@hostB_IP.com:~/

我正在尝试使用 libssh 编写一个 c 程序来执行相同的操作。我可以在 libssh 文档中找到的最接近的示例是端口转发。我的理解可能与跳转主机相同。我正在使用 libssh 中的 sftp 函数来传输文件。

我有下面的 c 程序,它成功连接到主机 A。而且我相信它成功打开了到主机 B 的 sftp 连接 [下面的日志]。但是当它尝试打开主机 B 上的文件时,它会出现“权限被拒绝”的错误。

我有以下问题。

  1. 端口转发到主机 B 是否正确完成? [查看下面的日志 - 它有一个错误]

  2. 这是将端口转发到主机 B 的正确函数调用吗?参数 2 是 user@IP 地址吗?

    rc = ssh_channel_open_forward(channel,"root@hostB_IP.com",22,"localhost",5555);

  3. 我认为端口转发是正确完成的,因为从日志看来端口转发成功后打开了 sftp 会话。

  4. 假设端口转发和 sftp 会话创建正常,并且由于错误是“SFTP 服务器:权限被拒绝”,我是否应该在 sftp 会话中以不同的方式打开文件。

这是代码,完整的日志在代码下方:

谢谢

代码:

#include <libssh/sftp.h>
int main(int argc,char* argv[]) {

    // Create SSH session
    ssh_session session;
    ssh_channel channel;
    int rc,port = 22;
    char buffer[1024];
    unsigned int nbytes;
    int verbosity = SSH_LOG_PROTOCOL;

    printf("Session...\n");
    session = ssh_new();
    if (session == NULL)
        exit(-1);

    ssh_options_set(session,SSH_OPTIONS_HOST,hostA_IP.com);
    ssh_options_set(session,SSH_OPTIONS_LOG_VERBOSITY,&verbosity);
    ssh_options_set(session,SSH_OPTIONS_PORT,&port);
    ssh_options_set(session,SSH_OPTIONS_USER,"user");

    printf("Connecting...\n");
    rc = ssh_connect(session);
    if (rc != SSH_OK)
        error(session);

    printf("Password Autentication...\n");
    rc = ssh_userauth_password(session,"user","userpassword");
    if (rc != SSH_AUTH_SUCCESS)
        error(session);

    printf("Channel...\n");
    channel = ssh_channel_new(session);
    if (channel == NULL)
        exit(-1);

    printf("Port forwarding to Host B...\n");
    rc = ssh_channel_open_forward(channel,5555);
    if (rc != SSH_OK) {
        error(session);
        exit(1);
    }
    printf("Port forwarding done...\n");

    // Open SFTP session
    sftp_session sftp0;
    printf("Opening sftp session...\n");
    sftp0 = sftp_new(session);

    if (sftp0 == NULL) {
        fprintf(stderr,"Error allocating SFTP session: %s\n",ssh_get_error(session));
        return SSH_ERROR;
    }
    // Initialize the SFTP session
    rc = sftp_init(sftp0);
    if (rc != SSH_OK) {
        fprintf(stderr,"Error initializing SFTP session: %s.\n",sftp_get_error(sftp0));
        sftp_free(sftp0);
        return rc;
    }
    printf("SFTP session opened...\n");

    sftp_file file0;
    printf("Openign file on Host B...\n");
    file0 = sftp_open(sftp0,"/home/root/test.blob",O_WRONLY | O_CREAT | O_TRUNC,S_IRWXU);
    if (file0 == NULL) {
        fprintf(stderr,"Can't open test.blob for writing: %s\n",ssh_get_error(session));
        return SSH_ERROR;
        exit(1);
    }
}

完整日志:

Session...
Connecting...
[2021/02/13 10:59:22.044331,2] ssh_connect:  libssh 0.7.0 (c) 2003-2014 Aris Adamantiadis,Andreas Schneider,and libssh contributors. Distributed under the LGPL,please refer to COPYING file for information about your rights,using threading threads_noop
[2021/02/13 10:59:22.063292,2] ssh_socket_connect:  Nonblocking connection socket: 3
[2021/02/13 10:59:22.063313,2] ssh_connect:  Socket connecting,now waiting for the callbacks to work
[2021/02/13 10:59:22.077165,1] socket_callback_connected:  Socket connection callback: 1 (0)
[2021/02/13 10:59:22.099055,1] ssh_client_connection_callback:  SSH server banner: SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
[2021/02/13 10:59:22.099081,1] ssh_analyze_banner:  Analyzing banner: SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
[2021/02/13 10:59:22.099095,1] ssh_analyze_banner:  We are talking to an OpenSSH client version: 7.6 (70600)
[2021/02/13 10:59:22.197188,2] ssh_packet_dh_reply:  Received SSH_KEXDH_REPLY
[2021/02/13 10:59:22.201442,2] ssh_client_curve25519_reply:  SSH_MSG_NEWKEYS sent
[2021/02/13 10:59:22.201466,2] ssh_packet_newkeys:  Received SSH_MSG_NEWKEYS
[2021/02/13 10:59:22.210742,2] ssh_packet_newkeys:  Signature verified and valid
Password Autentication...
Channel...
Port forwarding to Host B...
[2021/02/13 10:59:22.315060,2] channel_open:  Creating a channel 43 with 64000 window and 32768 max packet
[2021/02/13 10:59:22.465235,2] ssh_packet_global_request:  Received SSH_MSG_GLOBAL_REQUEST packet
[2021/02/13 10:59:22.465290,2] ssh_packet_global_request:  UNKNOWN SSH_MSG_GLOBAL_REQUEST [email protected] 0
[2021/02/13 10:59:22.465305,1] ssh_packet_process:  Couldn't do anything with packet type 80
[2021/02/13 10:59:22.709942,1] ssh_packet_channel_open_fail:  Channel opening failure: channel 43 error (2) Name or service not known
rc is 0
Port forwarding done...
Opening sftp session...
[2021/02/13 10:59:22.710034,2] channel_open:  Creating a channel 44 with 64000 window and 32768 max packet
[2021/02/13 10:59:22.725936,2] ssh_packet_channel_open_conf:  Received a CHANNEL_OPEN_CONFIRMATION for channel 44:0
[2021/02/13 10:59:22.725981,2] ssh_packet_channel_open_conf:  Remote window : 0,maxpacket : 32768
[2021/02/13 10:59:22.742666,2] channel_rcv_change_window:  Adding 2097152 bytes to channel (44:0) (from 0 bytes)
[2021/02/13 10:59:22.742715,2] channel_request:  Channel request subsystem success
[2021/02/13 10:59:22.760308,2] grow_window:  growing window (channel 44:0) to 1280000 bytes
[2021/02/13 10:59:22.760368,1] sftp_init:  SFTP server version 3
[2021/02/13 10:59:22.760390,1] sftp_init:  SFTP server extension: [email protected],version: 1
[2021/02/13 10:59:22.760405,1] sftp_init:  SFTP server extension: [email protected],version: 2
[2021/02/13 10:59:22.760419,1] sftp_init:  SFTP server extension: [email protected],version: 2
[2021/02/13 10:59:22.760433,1] sftp_init:  SFTP server extension: [email protected],version: 1
[2021/02/13 10:59:22.760447,1] sftp_init:  SFTP server extension: [email protected],version: 1
SFTP session opened...
Openign file on Host B...
[2021/02/13 10:59:22.842023,1] sftp_open:  SFTP server: Permission denied
Can't open test.blob for writing: SFTP server: Permission denied

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)