SFTP OpenFile Succeeds, but ReadFileBytes Fails

SSH/SFTP servers are notoriously bad at providing any sort of useful information about the cause of a problem. This is one of those cases. It was discovered that for one particular server, if a remote file is opened (via the OpenFile method) using “readWrite” access, then the OpenFile succeeds, but a subsequent call to a method such as ReadFileBytes fails. The only information from the server is an error code “10”. Gee, thanks! What the #@!$%! is an error code “10”??? (Usually an SSH server will also provide a short and utterly useless 1-line error description, but this particular server was too lazy to even do that.)

In the end, after fiddling around with different access and disposition choices, it was found that using “readOnly” for the access and “openExisting” for the create disposition solved the problem.

SFTP OpenFile fails with “Folder not found” from SFTP Server

If the SSH/SFTP server responds with a “Folder not found” error in response to calling OpenFile with a remote filepath is just a filename with no path part, then try prepending “./” to the filepath.  For example:

success = sftp.OpenFile(“test.txt”, “writeOnly”, “createTruncate”);

If this fails with a “Folder not found”, then modify your code to this:

success = sftp.OpenFile(“./test.txt”, “writeOnly”, “createTruncate”);

SSH/SFTP Disrupted by Untangle Security Appliance

(With permission from a Chilkat customer, for the benefit of others..)

Problem:

We did some extensive testing and found that our client sees this on their end what the connection fails:

SERVER:   13.07.2010  15:21:43  (57220) session ended on error, Buffer missing (error 132).

Do you know what that could possibly mean in relation to Chilkat’s SFTP?

Solution:

“The issue ended up being a security appliance (Untangle) we had. We added a bypass rule in there for port 22 and it went from working 3/5 times to 5/5 times.”

SSH/SFTP Downloading Text file produces strange characters?

Question:
I receive strange characters when downloading a text file using ReadFileText. Why does this happen?

Answer:
Using SSH/SFTP, files are downloaded exactly as-is byte-for-byte except if you call the ReadFileText* methods. The ReadFileText* methods require a charset (i.e. character encoding) to be specified in the method arguments. This allows the method to interpret the bytes according to a specific character encoding (such as utf-8, windows-1252, Shift_JIS, etc.) and then return the correct string (because strings in programming environments using ActiveX, or languages such as C#, VB.NET, Java, etc are objects with an internal Unicode representation).

SSH / SFTP – Too much time between connect and authentication

The Solution:

Issue solved.   The problem was, that we stepped through the code and because of that too much time elapsed between connect and authentication.  As we ran the program without breakpoints it worked.

The Problem:

The AuthenticatePw method failed and the LastErrorText contained this information:

ChilkatLog:
   AuthenticatePw:
     DllDate: Jan 31 2010
     UnlockPrefix: ***
     Username: ***
     Component: .NET 2.0
     SshVersion: SSH-2.0-XFB.Gateway Windows
     SftpVersion: 0
     login: ***
     sendMessage:
       msgName: SERVICE_REQUEST
       unpaddedLength: 22
       remainder: 6
       paddingLen: 10
       totalSize: 32
     SentServiceReq: ssh-userauth
     numBytesRequested: 16
     Connection closed by server.
     Failed to read data on SSH connection.
     Failed to read packet from SSH server.
     Error reading service accept.
     Socket connection lost.
     Failed.

SFTP File not Found

Question:

I’m trying to open a file on the SFTP server, but I get a “File not Found” error?
Here is the LastErrorText:

ChilkatLog:
 OpenFile:
   DllDate: Dec  2 2009
   UnlockPrefix: Anything for 30-day trial
   Username: IUSR_ABC***
   Component: ActiveX
   SshVersion: SSH-2.0-1.36 sshlib: GlobalScape
   SftpVersion: 3
   hcCurDate: Tue, 08 Dec 2009 13:48:25 +0000
   hcExpire: 1/2010
   filename: test.txt
   access: writeOnly
   createDisposition: createTruncate
   v3Flags: 0x1a
   Sent FXP_OPEN
   StatusResponse:
     Request: FXP_OPEN
     StatusCode: 2
     StatusMessage: File not found
   SshLog: SFTP> Sending SSH_FXP_OPEN
TRAN> CHANNEL_DATA
TRAN* NumBytes: 45
TRAN< CHANNEL_DATA
SFTP< Received SSH_FXP_STATUS

   Failed.


Answer:

You passed “test.txt” to OpenFile, which means you’re trying to open that file in the HOME directory of the SSH/SFTP user account. To solve the problem, you should either pass the full absolute filepath of the file you wish to open, or a relative filepath from the HOME directory of the SSH user account.

SFTP Permission Denied trying to open existing file.


Question:

Why did I get a permission denied error from the server when trying to open an existing file on the SSH/SFTP server? Here’s the LastErrorText:

ChilkatLog:
   DownloadFileByName:
     DllDate: Dec  2 2009
     UnlockPrefix: ***
     Username: Administrator
     Component: ActiveX
     SshVersion: SSH-2.0-CoreFTP-0.1.2
     SftpVersion: 3
     PreserveDate: 0
     fromFilePath: /test.dat.gz
     toFilePath: c:\abc\test.dat.gz
     OpenRemoteFile:
       filename: /test.dat.gz
       access: readOnly
       createDisposition: openExisting
       v3Flags: 0x1
       Sent FXP_OPEN
       StatusResponse:
         Request: FXP_OPEN
         StatusCode: 3
         StatusMessage: Permission denied
       SshLog: 
 SFTP> Sending SSH_FXP_OPEN
 TRAN> CHANNEL_DATA
 TRAN* NumBytes: 59
 TRAN< CHANNEL_DATA
 SFTP< Received SSH_FXP_STATUS

       timeToOpenMs: Elapsed time: 31 millisec
     totalTimeMs: Elapsed time: 31 millisec
     Failed.

Answer:

You are trying to download “/test.dat.gz” from the SSH/SFTP server. Some servers interpret the leading “/” as the root directory of the SSH user account. However, some servers interpret it as the root directory of the default drive (i.e. default filesystem). Your SSH/SFTP server is doing the latter. Your SSH/SFTP user account apparently does not have permission to read the root directory of the default filesystem, and therefore you get a “permission denied”. The solution is to download “./test.dat.gz” to clearly indicate that test.dat.gz is located in the HOME directory of the SSH/SFTP user account.

SendBufferSize/MaxPacketSize can make progress events more frequent in FTP and SSH/SFTP

Question:

I’ve downloaded the trial version of your FTP2 ActiveX control. I’m
using it in Visual FoxPro 9, but need to have a progress bar to track
an upload. When using your asynchronous example code, the progress
bar jumps from 0 to 524288 and so on. I need much more precision than
that so the progress bar will update smoothly. How do I accomplish
this with your tools?

Solution:

Set the SendBufferSize property to a smaller value, such as 8192. The FTP2 component has the SendBufferSize property. The equivalent property in SSH/SFTP is MaxPacketSize.

InitializeSftp – Received FAILURE response to subsystem request.

If the SSH server is setup/configured to not allow SFTP, or your SSH user account is not allowed to use SFTP, the InitializeSftp method will return a failed status. Here is an example:

ChilkatLog:
   InitializeSftp:
     DllDate: Oct 30 2009
     UnlockPrefix: ABC123
     Username: chilkat
     Component: .NET 2.0
     SshVersion: SSH-2.0-OpenSSH_4.5
     SftpVersion: 0
     channelType: session
     clientChannel: 200
     initialWindowSize: 327680
     maxPacketSize: 32768
     Sent open channel request
     ClientChannelNum: 200
     ServerChannelNum: 0
     ServerInitialWindowSize: 0
     ServerMaxPacketSize: 32768
     Session channel successfully opened.
     ChannelNum: 200
     ServerChannelNum: 0
     Sent subsystem request
     Received FAILURE response to subsystem request.
     SSH read failed...
     Failed.

SFTP ReadFileBytes not returning all the data?

Question:

I’m just beginning to work with some of the Chilkat components and I’ve
run into an apparent issue with the ReadFileBytes method in the
ChilkatSFtp component.

I’m attempting to read an entire file using ReadFileBytes and only
getting part of the file. Here’s essentially what I’m doing:

* Use GetFileSize32 to get the size of the file (returns 168914)
* Use ReadFileBytes passing in that length (168914) as the number
of bytes

The result is that I get 50000 bytes.
Is there a 50000 byte limit in ReadFileBytes?

Answer:

The 2nd argument to ReadFileBytes is the maximum number of bytes you want to read.
The method may return with a lesser number of bytes. To read the entire file, you would call ReadFileBytes repeatedly until you reach the EOF.

(See the reference documentation at http://www.chilkatsoft.com/refdoc –> “To read an entire file, one may call ReadFileBytes repeatedly until Eof(handle) returns true.” )