0

TLDR; Convert the bash line to download sftp files get Inbox/* to c++ or python. We do not have execute permissions on Inbox directory.

I am trying to read the files present in a directory in a remote server through SFTP. The catch is that I only had read and write permissions on the directory and not execute. This means any method that requires opening (cding) into the folder would fail. I need to read the file names since they are variable. From what I understand ls does not require execute privs. If I can get a list of files in the directory then reading then would be fine. Here is the directory structure:

Inbox
 --file-a.txt
 --file_b.txt
...

I have tried libssh but sftp_readdir required a handle of the open directory. I also looked at paramiko for python but that too requires to open the directory to read the file names.

I am able to do this in bash using send "get Inbox/* ${destination_dir}". Is there anyway I can use a similar pattern match but on c++ or python?

Also, I cannot execute bash commands through my binary. Does anyone know of any library in python or c++ (preferred) that would support this?

I have not posted here in a while so please excuse me if I am not following the formatting. I will learn from your suggestions. Thank you!

anon
  • 1,258
  • 10
  • 17
  • 1
    Without X permissions, you can only retrieve a list of file names. What would that be good for? What would you do with the names afterwards? – Martin Prikryl May 01 '20 at 06:12
  • If I have the full path of a file then do I still need execute permissions in its directory in order to be able to read that file? I was under the impression that if you have the path for the file you can read it as long as you have read permissions for the file. – anon May 01 '20 at 06:50
  • I do not think so. See [Execute vs Read bit. How do directory permissions in Linux work?](https://unix.stackexchange.com/q/21251/119732#317446) – *"With X unset, R and W are mostly useless."* – Though this is not a programming question anymore. – Martin Prikryl May 01 '20 at 07:04
  • Hmm interesting. I am able to download the files using this: send "get Inbox/* ${destination_dir}". This should not be possible without reading the files.. – anon May 01 '20 at 08:07
  • I do not know what `send "get Inbox/* ${destination_dir}"` is. – Martin Prikryl May 01 '20 at 08:24
  • I meant to say `get Inbox/*`. It is a linux command to download files over sftp. The get command is present in here https://linux.die.net/man/1/sftp. We can call the get command after establishing the sftp session to download files – anon May 01 '20 at 08:26
  • OpenSSH `get` does the same what Paramiko `sftp_readdir` to resolve the wildcard. – Martin Prikryl May 01 '20 at 09:02
  • Paramiko does not seem to have an `sftp_readdir`, it has `listdir` which does `cd` into the directory: https://github.com/paramiko/paramiko/blob/master/paramiko/sftp_client.py#L278. Libssh has `sftp_readdir` but that takes in the handle of an open directory as an argument as seen in the examples: https://api.libssh.org/stable/libssh_tutor_sftp.html – anon May 01 '20 at 18:08
  • I do not understand you. There's no `cd` in SFTP. SFTP does not have the concept of as working directory. It uses absolute paths. + What's wrong about "handle of an open directory"? There's no other way read a directory contents in SFTP. OpenSSH does the same. – Martin Prikryl May 01 '20 at 18:13
  • Sorry for the confusion, I meant opening the directory as `cd`. The problem is that to get a handle for the open directory, we would need execute privileges on that directory. Is that not the case? In SFTP, can you get the handle of the directory if you do not have execute privileges? – anon May 04 '20 at 19:40
  • `cd` in OpenSSH SFTP client is something very different to `cd` in remote server shell. – Again SFTP protocol does not have the concept of a working directory – The OpenSSH SFTP client only simulates that on the local side. + There's only one way to retrieve directory contents in SFTP – `SSH_FXP_OPENDIR` followed by `SSH_FXP_READDIR` – If one SFTP client can retrieve a list of files in folder, other clients can do the same. – Martin Prikryl May 04 '20 at 20:06

0 Answers0