Link to home
Start Free TrialLog in
Avatar of Jim Klocksin
Jim KlocksinFlag for United States of America

asked on

Need to change from FTP to SFTP

I wrote two C-language programs almost 15 years ago, one that uploads files to an FTP server, one that downloads files from an FTP server.  The two programs are essentially identical in that I'm calling Windows functions as follows:

1) InternetOpen - to obtain a handle to an open Internet session.
2) InternetConnect - to obtain a handle to an open FTP session.
3) FtpDeleteFile - to delete the file being transferred before transferring the new version of the file.
4) FtpPutFile/FtpGetFile - to upload/download the file to/from the FTP server.
5) InternetCloseHandle - to close the handle to the FTP session.
6) InternetCloseHandle - to close the handle to the Internet session.

These programs have been working for the past 15 years and they both have the FTP server credentials "hard-coded" so they're not multi-functional, but were designed for my specific needs.  Now the corporation that I provide my software application services to are requesting that I use SFTP rather than FTP to adhere to their enhanced security guidelines.  I've searched the Internet for information on SFTP, but I haven't found anything that really provides any real insight into what I could do to change my existing programs to work with SFTP.  

Frankly, I don't really understand what I need to do to even set up an SFTP transfer, much less automate it, which is the primary advantage with using my own programs, since I can set up a command file to transfer ALL the files I need to transfer on a daily basis, actually "zipped-up" database backups, that I need to have copies of (in multiple locations) daily.

So I'm looking for some advice and/or education on what SFTP is all about and how I could switch my daily file transfers to SFTP without totally reconfiguring my existing solution.  As always, any help would be greatly appreciated!
Avatar of Daniel Pineault
Daniel Pineault

What library did you employ?  Have you looked at using something like https://www.libssh2.org/

I do not believe sftp is natively available in Windows.
Likely running the Windows FileZilla SFTP server will be the easiest to get working.

You're scripts will work as-is, with the only modification adding in user of either an SSH keyfile or using something like askpass to inject a user/pass into all your SFPT sessions.

Easiest way will be to create an empty passphrase SSH key.

Something like...

ssh-keygen -q -N "" -b 4096 -t rsa -f path-to-transfer-keyfile.rsa -C "File Transfer Key"

Open in new window

Avatar of Jim Klocksin

ASKER

David, Obviously you understand this stuff while I do NOT!  When you say running the "Windows FileZilla SFTP server", is that an "add-on" to a standard Windows Server or what is it exactly?  When you mention adding a user with an SSH keyfile, can you provide some more detail on that as well?  I don't have the ability to add "users" to the corporate domain that I'm working with.  That said, they may have user accounts that are already set up with an SSH keyfile.  Further, as you can probably tell, I, frankly, have no idea what I'm even talking about here or asking about, so please bear with me as all of this is foreign to me.  I'm a software application developer that, at times, has to broaden my knowledge by learning as much as possible to respond to my client's demands.
Sftp is a component of openssh that provide FTP like functionality over an ssh, port 22 connection.
The change needed is to instead of connecting to port 21 and then calculate the data port after issuing a PASV directive.
You need to incorporate the library Daniel referenced. You could as David suggested use an ssh key instead of username/password. The other side will have to ....

So the only change deals with the estab lush ent of the connection and handling of transfer over a single channel.
Daniel, I looked at the site you mentioned in your comment, but I couldn't figure out how to obtain the entire libssh2 library.  I can cut and paste some of the sample code, but I need to have the "include" files as well.  Can you tell me how I can obtain a copy of this library?
ASKER CERTIFIED SOLUTION
Avatar of Daniel Pineault
Daniel Pineault

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
While I'm not entirely sure that I can even do this with the "hosting company" that I use for my FTP transfers, I think the information in the libssh2 library should be what I would need to use in the case that I am successful in getting this to work.  Since I'm not currently under any time pressure to complete this (they are letting me continue to use FTP for at least a couple more months), I will probably not get around to working on this for a while (I asked the question to get some idea of what would be involved when I eventually will need to make this change!).  Thanks to all who responded.