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 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.