Issue
I have an SFTP server set up at home which uses FreeBSD 11.0, and I am writing a program that would also do simple permission changes. How would I change file permissions on the remote host?
I have tried looking for libraries all over that were both user-friendly and could support this, but to no avail do they match my criteria. I decided to try and write my own support, but I don't know how I would accomplish this.
Pseudo-code of what I want to do:
void setFlags (short flags) {
// Set remote file's permissions based on flags (example flag: 744)
FILE *rfile = fopen ("/remote/file/path.txt", 'w');
chmodRemote (rfile, flags);
fclose (rfile);
}
Edit: I don't want to use system ("foo");
for this.
Solution
I have come up with a solution after all this time.
Connect to the SFTP site using
sys/socket.h
under its set port (default 22, the SSH port). The encryption technique you choose (Symmetric Encryption, Asymmetric Encryption, and Hashing) will have to be set up by you however if you choose to do this process by hand, as this is how SSH makes decryption possible on the destination device, all of which (and packet format) is explained here. You may also want to find how you can retrieve data over the session.Send a packet with all the metadata needed since this is a shell session (a la "FTP over SSH"). This is also how other commands can be sent.
Do note that if you want backwards compatibility with FTP, you will need to refer to a raw FTP commands list since the old version does not use shell packets; you will need the SITE
command (followed by the server's command and arguments) for that, no surrounding fluff, just raw data.
Answered By - Jamesthe1 Answer Checked By - Senaida (WPSolving Volunteer)