SFTP Permission Denied w/ OpenFile Method

Question:

We are trying to upload a file to SFTP using private key. We are able to connect and retrieve folder listing. But when we try to upload to one of the folder on the SFTP we get the “Permission denied” error message.

The message is as below.

ChilkatLog:
  OpenFile:
    DllDate: Feb 15 2010
    UnlockPrefix: ****
    Username: joe
    Component: .NET 2.0
    SshVersion: SSH-1.99-OpenSSH_3.8p1
    SftpVersion: 3
    filename: .\Upload\myFiles\test.xml
    access: writeOnly
    createDisposition: createNew
    v3Flags: 0x2a
    Sent FXP_OPEN
    StatusResponse:
      Request: FXP_OPEN
      StatusCode: 3
      StatusMessage: Permission denied
    SshLog:
SFTP> Sending SSH_FXP_OPEN
TRAN> CHANNEL_DATA
TRAN* NumBytes: 44
TRAN< CHANNEL_DATA
SFTP< Received SSH_FXP_STATUS

Answer:

The content of the LastErrorText property (as shown above) provides good information that allows me to suggest some things to try:

  1. I can see by the “DllDate” that the version being used is older.  I would highly recommend updating to the latest version to ensure you’re getting the best information in LastErrorText.  The information in LastErrorText is always being refined and improvedd as Chilkat does user-support day-to-day.
  2. The createDisposition passed to OpenFile in this case is “createNew”.  If the file already exists, then the OpenFile will fail.  It’s possible that the server will indicate the failure as a “Permission denied” error.  Instead, use “createTruncate” for the createDisposition argument.
  3. I see that backslashes are used in the remote file path.  Try using forward slashes instead.
  4. To fully understand the absolute file path of the destination file on the SSH/SFTP server, call RealPath to ask the server to convert a relative path to an absolute path.  Maybe the SSH/SFTP user account’s home directory is something different than expected.

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.