Subject: Re: libssh2_channel_read() issue

Re: libssh2_channel_read() issue

From: Amirul Islam <blindcat_at_gmail.com>
Date: Sun, 21 Feb 2016 17:56:45 +0430

Hi Rene,

I have checked the sb.st_mode it returns 0x836B 68E3, I dont know if that
is a valid value either. Following is the function which downloads the
files. Before this is just establishing ssh connection and authentication.
What did I do wrong?

int scpGetFile(LIBSSH2_SESSION **session, int *sock, const struct instance
*inst, const char *path, const char *filename, const char *drop)
{
LIBSSH2_CHANNEL *channel;
struct stat fileinfo;
int got = 0, rc, retval = true;
char getfile[PATH_MAX], dropfile[PATH_MAX];
FILE *fp;
sprintf(getfile, "%s/%s", path, filename);
sprintf(dropfile, "%s/%s", drop, filename);

if (!(fp = fopen(dropfile, "wb")))
{
printlog("EROR", "%s - Cannot create file (%s) for writing.",
inst->shortdesc, dropfile);
return false;
}

if (!(channel = libssh2_scp_recv(*session, getfile, &fileinfo)))
{
printlog("EROR", "%s - Cannot create channel to download - %s",
inst->shortdesc, getfile);

fclose(fp);
unlink(dropfile);

return false;
}

//printlog("EROR", "%ld", fileinfo.st_mode);

while (got < fileinfo.st_size)
{
char buffer[MAXSCPBUF];
int amount = MAXSCPBUF;

if ((fileinfo.st_size - got) < amount)
amount = (int) (fileinfo.st_size - got);

rc = libssh2_channel_read(channel, buffer, amount);

if (rc > 0)
{
fwrite(buffer, rc, 1, fp);
}
else if (rc < 0)
{
printlog("EROR", "%s - Read error from channel for %s from %s",
inst->shortdesc, filename, inst->shortdesc);
retval = false;
break;
}
// else
// {
// if (libssh2_channel_eof(channel))
// {
// printlog("EROR", "%s - EOF sent from channel for %s", inst->shortdesc,
filename);
// break;
// }
// printlog("EROR", "%s - Zero?");
// }

got += rc;
}

printlog("INFO", "%s - Downloaded file %s, filesize: %d", inst->shortdesc,
filename, fileinfo.st_size);

fclose(fp);
libssh2_channel_free(channel);
channel = NULL;

if (!retval)
unlink(dropfile);

return retval;
}

On Fri, Feb 19, 2016 at 1:44 PM, Belau, Rene (ext) <
belau.rene.ext_at_siemens.com> wrote:

> Hi Amirul,
>
>
>
> sorry but this sounds weird to me.
>
>
>
> After call of libssh2_scp_recv() you got a handle and sb.mode =
> 0x0040(POSIX) or 0x0100(WIN) for read permissions?
>
>
>
> If it is true
>
>
>
> at the same usecase, as you described.
>
>
>
> Logged in user and directory have rights. ->
>
>
>
> Drectory >> me:me drwxrwxrwx
>
> File inside >> me:me –w-------
>
>
>
> My result: for libssh2(1.4.3 && 1.5.0) after 3 tries (test with blocking
> mode and tested again three different ssh servers)
>
> 1. libssh2_scp_recv() //handle = null, stat = {0},
> libssh2_session_last_errno() //error -28
>
> My result: for libssh2(1.4.3 && 1.5.0) after 3 tries (test with
> non-blocking mode and tested again three different ssh servers)
>
> 1. libssh2_scp_recv() //handle = null, stat = {0},
> libssh2_session_last_errno() //error -37
>
> 2. libssh2_scp_recv() //handle = null, stat = {0},
> libssh2_session_last_errno() //error -37
>
> 3. libssh2_scp_recv() //handle = null, stat = {0},
> libssh2_session_last_errno() //error -28
>
> Just fine, how it should be.
>
>
>
>
>
> Do you use an older version? you could dig into libssh2 -> scp.c ->
> scp_recv state 2 or 3
>
>
>
>
>
> Rene
>
>
>
> ---
>
>
>
> *Von:* libssh2-devel [mailto:libssh2-devel-bounces_at_cool.haxx.se
> <libssh2-devel-bounces_at_cool.haxx.se>] *Im Auftrag von *Amirul Islam
> *Gesendet:* Mittwoch, 17. Februar 2016 14:14
> *An:* libssh2 development
> *Betreff:* Re: libssh2_channel_read() issue
>
>
>
> Hi Rene,
>
>
>
> I have checked for last errno on both cases when libssh2_scp_recv() &
> libssh2_channel_read() is called, libssh2_session_last_errno() return 0.
> The issues is that my user has access to the remote folder, but does not
> have read permission on the target file which is contained in that folder.
> So the return value from libssh2_channel_read() is always 0 and sb.st_size
> is 1.
>
>
>
> you can simulate and check what you get
>
>
>
> On Wed, Feb 17, 2016 at 4:57 PM, Belau, Rene (ext) <
> belau.rene.ext_at_siemens.com> wrote:
>
> You need to do an operation and then check session error.
>
>
>
> libssh2_scp_recv() //handle null, return -37
>
> libssh2_session_last_errno() //error -28 scp protocol error (libssh2
> version 1.5.0) if you have no rights at target device
>
>
>
> libssh2_channel_read() //got we some bytes ?
>
> libssh2_session_last_errno() //without error?
>
>
>
> libssh2_channel_read() -> 0 bytes => no payload, it is not an error ->
> libssh2 tells us if it is error ! -> libssh2_session_last_errno()
>
>
>
>
>
> Rene
>
>
>
> *Von:* libssh2-devel [mailto:libssh2-devel-bounces_at_cool.haxx.se] *Im
> Auftrag von *Amirul Islam
> *Gesendet:* Mittwoch, 17. Februar 2016 11:24
> *An:* libssh2-devel_at_cool.haxx.se
> *Betreff:* libssh2_channel_read() issue
>
>
>
> Hi All,
>
>
>
> I am having an issue with the libssh2_channel_read() function. I am using
> it in blocking mode to download files from remote server. Now, I have an
> instance where the file I am trying to download, I do not have read
> permission. This is causing the read() function to return 0. I am not sure,
> if I should break my read loop when 0 is returned, since the documentation
> implies we should break when there is a negative value returned. I am not
> if breaking on 0 will have unpredictable behavior on other instances. need
> help
>
>
>
> Also, I can check the file mode when I call libssh2_scp_recv() function,
> what would be the best way to check if my user has read permission?
>
>
>
> Thank you in advance.
>
>
>
> --
>
>
>
> "Fame is a vapor; popularity an accident; the only earthly certainty is
> oblivion."
>
>
> _______________________________________________
> libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
>
>
>
>
>
> --
>
>
>
> "Fame is a vapor; popularity an accident; the only earthly certainty is
> oblivion."
>
> _______________________________________________
> libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
>
>

-- 
"Fame is a vapor; popularity an accident; the only earthly certainty is
oblivion."

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2016-02-22