sFTP session example lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- xinyiman · 2015-04-14 11:52 Hello guys, who explained to me how to make an ftp session with lazarus to send and receive a file? Possibly with synapse ---------------------------------------------------------------------- xinyiman · 2015-04-16 13:22 Guys, really no one knows how to do? I found this example, but it should work only with Windows. I need to succeed even with linux. Ideas? http://synapse.ararat.cz/files/contrib/sftp.zip ---------------------------------------------------------------------- ChrisF · 2015-04-16 13:46 Have you made a try with (BigChimp's answer) ? http://forum.lazarus.freepascal.org/index.php/topic,20061.msg115077.ht ml#msg115077 ---------------------------------------------------------------------- xinyiman · 2015-04-16 14:28 The BigChimp's answer is FTP solution, my problem is sFTP. Secure File Transfert Protocol ---------------------------------------------------------------------- ChrisF · 2015-04-16 14:50 Sorry, my mistake.  I've only read your  topic message, not the title... Quote from: xinyiman on April 14, 2015, 01:52:16 pm... how to make an ftp session with lazarus to send and receive a file... ---------------------------------------------------------------------- rvk · 2015-04-16 14:51 Quote from: xinyiman on April 16, 2015, 03:22:44 pmGuys, really no one knows how to do? I found this example, but it should work only with Windows. I need to succeed even with linux. Ideas? Why should that only work on Windows? CryptLib is also available on Linux. What doesn't work on Linux in SimpleSFTP.pas (after changing winsock to synsock, which you should do with all the synapse examples)? ---------------------------------------------------------------------- ChrisF · 2015-04-16 14:59 @rvk There are -at least- a few Windows API calls in SimpleSFTP.pas. Though I've not checked if there are a lots of (it's quite possible there are only very few of them). Code: [Select]CreateFile CloseHandle Windows.GetFileTime Windows.SetFileTime ... ---------------------------------------------------------------------- rvk · 2015-04-16 15:10 Quote from: ChrisF on April 16, 2015, 04:59:31 pmThere are -at least- a few Windows API calls in SimpleSFTP.pas. Woops, yeah. I see. That's gonna be a challenge to convert to Linux-compatibility  But it should be possible (although not for me as I'm not a Linux guy). I haven't found any examples for sFTP which are Linux-ready for Synapse. ---------------------------------------------------------------------- ChrisF · 2015-04-16 15:30 Quote from: rvk on April 16, 2015, 05:10:59 pmThat's gonna be a challenge to convert to Linux-compatibility  But it should be possible (although not for me as I'm not a Linux guy). I also guess it's quite possible to port it to Linux (but I'm not a Linux code neither). Apparently, after a quick test (Delphi 7 with TestSFTP demo), the Windows unit is needed only for: - Copy Memory. A simple "wrapper" for it is simple: Code: [Select]procedure CopyMemory(Destination:PVOID; Source:pointer; Length:DWORD); begin   Move(Source^, Destination^, Length); end; - TSimpleSFTP.GetLocalFileTimes and TSimpleSFTP.SetLocalFileTimes: those ones are apparently the only methods using directly the Windows API. So, I guess they could be rewritten from the scratch for Linux. *** EDIT *** I've used winsock for my quick test. So, it's not impossible there are a few additional issues with it. ---------------------------------------------------------------------- rvk · 2015-04-18 15:47 Quote from: ChrisF on April 16, 2015, 05:30:23 pmI also guess it's quite possible to port it to Linux (but I'm not a Linux code neither). Well... With my zero knowledge of Linux-GUI/desktop programming I just went for it (I only have experience with Linux servers at the command line  ) On the internet I only found commercial solutions for sFTP on Linux. So creating a working SimpleSFTP.pas could be interesting for more people. Adding a function CopyMemory, changing winsock to synsock and removing Windows I only had to IFDEF out the GetLocalFileTimes and SetLocalFileTimes functions. I think they are only used in (and after) the real getting and putting of files. For me it was only to test if I got this SimpleSFTP.pas running and connected under Linux. And it worked. I had a hell of a time installing CryptLib correctly (the beta 343 version didn't work) but finally cl342 did work. Here is the result connecting from Linux to a SFTP_2000.exe server (I got from here) running on a Windows 7 machine. The adjusted SimpleSFTP.pas is also attached. As you can see it connects correctly and I get the directory-result from the server. The factual transference of files doesn't work (yet). You'll get some errors. But it shows that using this SimpleSFTP.pas isn't as hard as we initially thought. @xinyiman, if you interested in help perfecting this SimpleSFTP.pas, let us know. ---------------------------------------------------------------------- xinyiman · 2015-05-14 18:47 Thank you for your advice. I intend to fix it. Only where can I download cryptlib for Ubuntu? Does anyone know? ---------------------------------------------------------------------- BitBangerUSA · 2015-05-14 18:57 https://www.cs.auckland.ac.nz/~pgut001/cryptlib/ ---------------------------------------------------------------------- xinyiman · 2015-05-20 08:40 Some progress has been made. but I still need help. As a prerequisite need to have cryptlib installed. Debian derived just type sudo apt-get install libcrypt * Then, download the attached file and try to compile it. You will see that will tell you that there are problems with the LCL. Ideas? PS: remember to give the path of the synapse source does not work otherwise. Attachments: http://www.lazaruspascal.it/index.php?action=dlattach;top ic=1539.0;attach=429 ---------------------------------------------------------------------- rvk · 2015-05-20 08:51 Quote from: xinyiman on May 20, 2015, 10:40:53 amThen, download the attached file and try to compile it. You will see that will tell you that there are problems with the LCL. Ideas? I (we) don't have access to the Italian Lazarus-forum, so you might want to post your attachment here. ---------------------------------------------------------------------- xinyiman · 2015-05-20 09:00 Sorry ---------------------------------------------------------------------- rvk · 2015-05-20 09:20 I guess you meant: sudo apt-get install libcrypt* (asterisk connected to libcrypt ) I had to remove Windows from the uses-clause in ssl_cryptlib. After that I got: Code: [Select]/usr/bin/ld: cannot find -lcl That's because Lazarus can't find the library libcl.so. (B.T.W. It's not LCL but Library "lc" which it can't find). From the source of cryptlib.pas: Quote  cryptlibname = 'libcl.so';  { library name for Unix/Linux  (Kylix) }                  { symbolic link should be used for libcl.so -> libcl.so.3.x.y }      I'm not sure where you need to get the libcl.so.3.x.y from. (you might need to compile it from source cl343_beta.zip) Edit: Yeah. There is probably no ready compiled cryptlib package for Debian (so go ahead and compile one yourself). http://comments.gmane.org/gmane.comp.encryption.cryptlib/2529 ---------------------------------------------------------------------- xinyiman · 2015-05-20 09:53 Thank you  Alternative idea for sftp connection? ---------------------------------------------------------------------- rvk · 2015-05-20 12:59 Quote from: xinyiman on May 20, 2015, 11:53:59 amAlternative idea for sftp connection? You could try converting the SimpleSFTP.pas to openssl but I'm not sure that's going to work. But what's the trouble with downloading CL343_Beta.zip from here and compiling it yourself. Make sure you use unzip -a (-a is for converting DOS-line endings to Linux-line endings) In Downloads Code: [Select]unzip -a -d cryptlib cl343_beta.zip cd cryptlib make shared sudo cp libcl.so.3.4.3 /usr/lib sudo ln -s /usr/lib/libcl.so.3.4.3 /usr/lib/libcl.so You now have a libcl.so (symbolic link to /usr/lib/libcl.so.3.4.3) and your program will compile perfectly. ---------------------------------------------------------------------- xinyiman · 2015-05-20 13:30 The problem is that I have to use it for a program that must be redistributed for fee ---------------------------------------------------------------------- rvk · 2015-05-21 09:11 Quote from: xinyiman on May 20, 2015, 03:30:20 pmThe problem is that I have to use it for a program that must be redistributed for fee Maybe you can find out if libssh2 is freely distributable. I'm not sure if it's going to work but you could try using TSSLLibSSH2 instead of TSSLCryptLib in SimpleSFTP. But before trying, you need to make sure you can use libssh2. libssh2 should be able to do sftp. See here. So maybe with some minor adjustments it could be made to work with SimpleSFTP (hopefully). From Synapse: Quote//requires LibSSH2 libraries! http://libssh2.org {:@abstract(SSH plugin for LibSSH2) Requires libssh2.dll or libssh2.so. You can download binaries as part of the CURL project from http://curl.haxx.se/download.html You need Pascal bindings for the library too! You can find one at:  http://www.lazarus.freepascal.org/index.php/topic,15935.msg86465.html #msg86465 (Edit: I can confirm libssh2 does sftp correctly. You need to check yourself if you may distribute it freely)