FTPS with curl

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
mlnr
Posts: 74
Joined: Fri Aug 28, 2015 1:52 pm
DBs Used: DBF
Location: Hungary
Has thanked: 25 times
Been thanked: 9 times

FTPS with curl

Post by mlnr » Tue May 29, 2018 8:39 am

Hi,
I need to upload and download files an FTPS server (not SFTP). I use CURL.
I modified Edward demo program which is communicate with SFTP protocol, but i get a message: "SSL connect error"

Code: Select all

Function SendFTPS(cFile)
Local cRet, UserLogin, UserPass, cUrl, cUploadFolder, cSftpURL, nPort, nFSize 

IF EMPTY(cFile)
	RETURN "No File"
ENDIF

UserLogin:='demo-user'
UserPass:='demo-user'
cUrl:="demo.wftpserver.com"
cUploadFolder:="UPLOAD"
nPort:=990

cSftpURL:="ftps://"+UserLogin+":"+UserPass+"@"+cUrl+"/"+cUploadFolder+cFile
nFSize:=FILESIZE(cFile)

info_('Init cURL')

//Init
curlHandle:=SFTP_api_init()
IF EMPTY(curlHandle)
	Info_("Error while init cURL lib.",.T.)
	WAIT CLEAR
	RETURN "Error while init cURL lib."
ENDIF

info_('Uploading ...')

curl_easy_reset( curlHandle )

curl_easy_setopt(curlHandle, HB_CURLOPT_URL, cSftpURL)
curl_easy_setopt(curlHandle, HB_CURLOPT_PORT, nPort)
curl_easy_setopt(curlHandle, HB_CURLOPT_UPLOAD, 1)
curl_easy_setopt(curlHandle, HB_CURLOPT_PROTOCOLS, HB_CURLPROTO_FTPS)
curl_easy_setopt(curlHandle, HB_CURLOPT_SSL_VERIFYPEER, .F. )
curl_easy_setopt(curlHandle, HB_CURLOPT_SSL_VERIFYHOST, .F. )
curl_easy_setopt(curlHandle, HB_CURLOPT_UL_FILE_SETUP, cFile)
curl_easy_setopt(curlHandle, HB_CURLOPT_INFILESIZE_LARGE, nFSize)

/* Do everything */
curlErr := curl_easy_perform(curlHandle)
	
/* Report any errors */
IF !EMPTY( curlErr )
	cRet:=curl_easy_strerror(curlErr)
ELSE
	cRet:='Sucessfully'
ENDIF

//Close
SFTP_api_close( curlHandle )
WAIT CLEAR

RETURN cRet
*****************************************************************************************
Function SFTP_api_init()
Local curlHandle := curl_easy_init()
RETURN curlHandle
******************************************************************************************
Function SFTP_api_close( curlHandle )
IF !EMPTY(curlHandle)
	/* Clean-up libcurl */
	curl_global_cleanup( curlHandle )
ENDIF
RETURN
******************************************************************************************
Function info_(cTresc, lCzekaj)
Local nTimeIni := hb_MilliSeconds()
Default lCzekaj:=.F.
 
Wait Window cTresc NoWait
IF lCzekaj
	WHILE( ( hb_MilliSeconds() - nTimeIni ) < 3 * 1000 )
		DO EVENTS
		hb_releaseCPU()
	ENDDO
ENDIF
RETURN Nil

Any help would be appreciated.
Best regards,
Gabor

edk
Posts: 307
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 85 times
Been thanked: 234 times

Post by edk » Tue May 29, 2018 11:35 am

UserLogin:='demo-user'
UserPass:='demo-user'
cUrl:="demo.wftpserver.com"
cUploadFolder:="UPLOAD"
Is this the actual authentication data?

I have set up a local FTPS server (FileZilla Server) and I have no problems with uploading files to this server.

Code: Select all

Function SendFTPS(cFile)
Local cRet, UserLogin, UserPass, cUrl, cUploadFolder, cSftpURL, nPort, nFSize 

IF EMPTY(cFile)
	RETURN "No File"
ENDIF

#pragma TEXTHIDDEN(1) 
UserLogin:='testftps'
UserPass:='1qazxsw2'
#pragma TEXTHIDDEN(0)
cUrl:="localhost"
cUploadFolder:=""

cftpsURL:="ftps://"+UserLogin+":"+UserPass+"@"+cUrl+"/"+cUploadFolder+cFile
nPort:=990
nFSize:=hb_FSize(cFile)

info_('Init cURL')

//Init
curlHandle:=FTPS_api_init()
IF EMPTY(curlHandle)
	Info_("Error while init cURL lib.",.T.)
	WAIT CLEAR
	RETURN "Error while init cURL lib."
ENDIF

info_('Uploading ...')

curl_easy_reset( curlHandle )

curl_easy_setopt(curlHandle, HB_CURLOPT_URL, cftpsURL)
curl_easy_setopt(curlHandle, HB_CURLOPT_PORT, nPort)

curl_easy_setopt(curlHandle, HB_CURLOPT_FOLLOWLOCATION, .T. )
curl_easy_setopt(curlHandle, HB_CURLOPT_SSL_VERIFYPEER, .F. )
curl_easy_setopt(curlHandle, HB_CURLOPT_SSL_VERIFYHOST, .F. )

//curl_easy_setopt(curlHandle, HB_CURLOPT_CAPATH, capath)

curl_easy_setopt(curlHandle, HB_CURLOPT_UPLOAD, 1)
curl_easy_setopt(curlHandle, HB_CURLOPT_PROTOCOLS, HB_CURLPROTO_FTPS)
curl_easy_setopt(curlHandle, HB_CURLOPT_UL_FILE_SETUP, cFile)
curl_easy_setopt(curlHandle, HB_CURLOPT_INFILESIZE_LARGE, nFSize)


/* Do everything */
curlErr := curl_easy_perform(curlHandle)
	
/* Report any errors */
*IF !EMPTY( curlErr )
	cRet:=curl_easy_strerror(curlErr)
*ELSE
*	cRet:='Sucessfully'
*ENDIF

//Close
FTPS_api_close( curlHandle )
WAIT CLEAR
RETURN cRet
*****************************************************************************************
Function FTPS_api_init()
Local curlHandle := curl_easy_init()
RETURN curlHandle
******************************************************************************************
Function FTPS_api_close( curlHandle )
IF !EMPTY(curlHandle)
	/* Clean-up libcurl */
	curl_global_cleanup( curlHandle )
ENDIF
RETURN
******************************************************************************************
Function info_(cMessage, lWait)
Local nTimeIni := hb_MilliSeconds()
Default lWait:=.F.
 
Wait Window cMessage NoWait
IF lWait
	WHILE( ( hb_MilliSeconds() - nTimeIni ) < 3 * 1000 )
		DO EVENTS
		hb_releaseCPU()
	ENDDO
ENDIF
RETURN Nil
********************************
Log from local FTPS server:

Code: Select all

(000012)2018.05.29 13:10:05 - (not logged in) (127.0.0.1)> Connected on port 990, sending welcome message...
(000012)2018.05.29 13:10:05 - (not logged in) (127.0.0.1)> 220-FileZilla Server 0.9.60 beta
(000012)2018.05.29 13:10:05 - (not logged in) (127.0.0.1)> 220-written by Tim Kosse (tim.kosse@filezilla-project.org)
(000012)2018.05.29 13:10:05 - (not logged in) (127.0.0.1)> 220 Please visit https://filezilla-project.org/
(000012)2018.05.29 13:10:06 - (not logged in) (127.0.0.1)> TLS connection established
(000012)2018.05.29 13:10:06 - (not logged in) (127.0.0.1)> USER testftps
(000012)2018.05.29 13:10:06 - (not logged in) (127.0.0.1)> 331 Password required for testftps
(000012)2018.05.29 13:10:06 - (not logged in) (127.0.0.1)> PASS ********
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> 230 Logged on
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> PBSZ 0
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> 200 PBSZ=0
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> PROT P
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> 200 Protection level set to P
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> PWD
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> 257 "/" is current directory.
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> EPSV
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> 229 Entering Extended Passive Mode (|||56190|)
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> TYPE I
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> 200 Type set to I
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> STOR test.txt
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> 150 Opening data channel for file upload to server of "/test.txt"
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> TLS connection for data connection established
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> 226 Successfully transferred "/test.txt"
(000012)2018.05.29 13:10:06 - testftps (127.0.0.1)> disconnected.
Server response:
Bez tytułu1.png
Bez tytułu1.png (2.27 KiB) Viewed 177 times
But for your credentials I get Server sesponse:
Bez tytułu2.png
Bez tytułu2.png (2.63 KiB) Viewed 177 times
It means that this user doesn't have full permissions to the home directory on the FTPS server.

mlnr
Posts: 74
Joined: Fri Aug 28, 2015 1:52 pm
DBs Used: DBF
Location: Hungary
Has thanked: 25 times
Been thanked: 9 times

Post by mlnr » Tue May 29, 2018 2:54 pm

Edward,

Thank you for your help. It does not work for me.
I will send a pm with real data, if you don't mind.
Please, check it.
Best regards,
Gabor

Post Reply