FTP fails when there is an '@' in the user name or password

255 views
Skip to first unread message

Jayadev U

unread,
Jul 1, 2021, 5:53:24 AM7/1/21
to harbou...@googlegroups.com

Hi,

 

Please check out the following code:

 

If you take a look at the aMatch array in the url.prg shipped with Harbour, the user and the password are not correctly detected because of ‘@’ in the username and ‘:’ or ‘@’ within the password.  The username in the given context is assigned by the FTP Admins and cannot be changed.  So how to overcome the problem ? As of now, no FTP log is generated to analyze the problem.

 

Just for information, use of any other FTP Clients viz. CoreFTP,FileZilla works correctly.

 

/* code begins */

STATIC FUNCTION  ftpDB(aFiles)

   LOCAL cStartTime := Time(), cEndTime :=""

   LOCAL cServer   := "ftp.abc.com" , cUser     := "j...@ujayadev.com", cPassword := "Table@chair3"

   LOCAL lRetVal := .F.

   LOCAL cUrl      := "ftp://" + cUser + ":" + cPassword + "@" + cServer

   LOCAL oUrl              := TUrl():New( cUrl )

   LOCAL oFTP              := TIPClientFTP():New(oUrl , .t. ) //TIPClientFTP():New( oUrl, .F. )

   LOCAL i := 0

   oFTP:nConnTimeout := 20000

   oFTP:bUsePasv     := .T.

   IF oFTP:Open(cUrl)   /* fails here */

    if oFtp:Cwd( "/ugrani/" )

        logdebug("Directory changed to ugrani"+oURL:cServer+hb_eol())

     else

        ErrorLevel( iif( lRetVal, 0, 1 ) )

        logdebug("Directory could not be changed to ugrani,exiting"+oURL:cServer+hb_eol())

        RETURN .F.

     endif     

     /* fetch files to transfer */

      IF ! Empty( aFiles )

            FOR  i = 1 to Len(aFiles)

               logdebug("Filename: "+aFiles[ i ]+hb_eol())

               IF ! oFtp:UploadFile( aFiles[ i ] )

                  lRetVal := .F.

                  EXIT

               ENDIF

            NEXT

            oFTP:Close()

      endif

   ELSE

      logdebug("Could not connect to FTP server "+oURL:cServer+hb_eol())

      logdebug("Error: ",oFTP:LasterrorCode(),oFTP:Lasterrormessage())

     

      IF oFTP:SocketCon == NIL

         logdebug("Connection not initialized"+hb_eol())

      ELSEIF hb_inetErrorCode( oFTP:SocketCon ) == 0

         logdebug("Server response: "+hb_ntos(oFTP:cReply)+hb_eol())

      ELSE

         logdebug("Error in connection: "+hb_inetErrorDesc( oFTP:SocketCon )+hb_eol())

      ENDIF

      lRetVal := .F.

   ENDIF

   cEndTime := Time()

   logdebug("FTP "+DToC(Date()),cStartTime,cEndTime,ElapTime(cStartTime,cEndTime))

   ErrorLevel( iif( lRetVal, 0, 1 ) )

RETURN lRetVal

 

/* code ends */

Klas Engwall

unread,
Jul 1, 2021, 9:21:40 AM7/1/21
to harbou...@googlegroups.com
Hi Jayadev,

Using ...
LOCAL cUrl := "ftp://" + cUser + ":" + cPassword + "@" + cServer
... to set up the TUrl() class has a known problem. And it is commented
with a "May fail" in the code itself. The solution is to use
TUrl():New() without arguments and set each instance variable separately
after that call. That also, as an extra bonus :-), makes the code more
readable. See this thread:

https://groups.google.com/g/harbour-devel/c/1duN_CHot4E/m/3q5iz1x9AgAJ

Regards,
Klas
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Harbour Users" group.
> Unsubscribe: harbour-user...@googlegroups.com
> Web: http://groups.google.com/group/harbour-users
>
> ---
> You received this message because you are subscribed to the Google
> Groups "Harbour Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to harbour-user...@googlegroups.com
> <mailto:harbour-user...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/harbour-users/000701d76e5e%24ec60b160%24c5221420%24%40com
> <https://groups.google.com/d/msgid/harbour-users/000701d76e5e%24ec60b160%24c5221420%24%40com?utm_medium=email&utm_source=footer>.

CNavarro

unread,
Jul 1, 2021, 4:32:33 PM7/1/21
to Harbour Users
Using Curl is straightforward and works as expected, as well as being able to find a lot of information.

jgt

unread,
Jul 1, 2021, 5:29:53 PM7/1/21
to Harbour Users
Depending upon the security, you could use a .netrc file that contains the site, user and password.
machine abc.com user jayadev password abc123. 
This file is stored in the user's home directory, and has access set to owner only.
Whether on not the ftp server will accept an @ is up to its administrator.

Fausto Di Creddo Trautwein

unread,
Jul 1, 2021, 5:54:09 PM7/1/21
to harbou...@googlegroups.com
Hi,

I have the same problem with hb_sendmail.

Try this:


/* code begins */

STATIC FUNCTION  ftpDB(aFiles)

   LOCAL cStartTime := Time(), cEndTime :=""

   LOCAL cServer := "ftp.abc.com" , cUser := "j...@ujayadev.com", cPassword := "Table@chair3"

   LOCAL lRetVal := .F.

   LOCAL cUrl := "ftp://" + cUser + ":" + cPassword + "@" + cServer

   LOCAL oUrl := TUrl():New( cUrl )

   LOCAL oFTP := TIPClientFTP():New(oUrl , .t. ) //TIPClientFTP():New( oUrl, .F. )

   LOCAL i := 0

   oFTP:nConnTimeout := 20000

   oFTP:bUsePasv     := .T.


   oFTP:oUrl:= TUrl():New( cUrl )

   oFTP:oUrl:cPassword:= cPassword

   oFTP:oUrl:cServer:= cServer

   IF oFTP:Open( /*NOTHING HERE*/ )   /* fails here ? */

   ...


Regards,

Fausto
--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users

---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/000701d76e5e%24ec60b160%24c5221420%24%40com.

Jayadev U

unread,
Jul 2, 2021, 12:20:20 AM7/2/21
to harbou...@googlegroups.com
Hi Klas,

Thanks so much for the information. Helpful as always.

I will check out during the day.

Warm regards,

Jayadev
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/04b200ab-cfa0-0d6e-b2f9-048f98c5e952%40engwall.com.

Jayadev U

unread,
Jul 2, 2021, 12:23:04 AM7/2/21
to harbou...@googlegroups.com

Hi,

 

>>Using Curl is straightforward and works as expected, as well as being able to find a lot of information.

Looks very promising,  Do you have some sample on how we can access curl from within the program ? what hbc to link etc.

Thanks for your help.

Warm regards,

Jayadev

 

 

 

From: harbou...@googlegroups.com [mailto:harbou...@googlegroups.com] On Behalf Of CNavarro
Sent: 02 July 2021 02:03 AM
To: Harbour Users
Subject: Re: [harbour-users] FTP fails when there is an '@' in the user name or password

 

Using Curl is straightforward and works as expected, as well as being able to find a lot of information.

To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/40292478-4175-4257-b3b8-84f7b779729dn%40googlegroups.com.

Jayadev U

unread,
Jul 2, 2021, 12:24:04 AM7/2/21
to harbou...@googlegroups.com

Hi,

 

Interesting, could you please elaborate a little more on this.

 

Warm regards,

 

Jayadev

 

 

From: harbou...@googlegroups.com [mailto:harbou...@googlegroups.com] On Behalf Of jgt
Sent: 02 July 2021 03:00 AM
To: Harbour Users
Subject: Re: [harbour-users] FTP fails when there is an '@' in the user name or password

 

Depending upon the security, you could use a .netrc file that contains the site, user and password.

To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/afe88ade-18ea-4f3e-bcd8-fd8c2bdff42en%40googlegroups.com.

Jayadev U

unread,
Jul 2, 2021, 12:25:02 AM7/2/21
to harbou...@googlegroups.com

Hi Fausto,

 

Thanks so much for your reply.  I will try during the day.

 

Warm regards,

 

Jayadev

 

 

 

 

From: harbou...@googlegroups.com [mailto:harbou...@googlegroups.com] On Behalf Of Fausto Di Creddo Trautwein
Sent: 02 July 2021 03:24 AM
To: harbou...@googlegroups.com
Subject: Re: [harbour-users] FTP fails when there is an '@' in the user name or password

 

Hi,

CNavarro

unread,
Jul 2, 2021, 3:16:36 AM7/2/21
to Harbour Users


#ifdef __PLATFORM__WINDOWS
#include "c:\harbour\contrib\hbcurl\hbcurl.ch"
#else
#include "/usr/include/harbour/hbcurl.ch"
#endif

function Main()

curl_global_init()
? FtpUploadFile( "ftp://fivetechsoft.com/test.prg", hb_GetEnv( "PRGPATH" ) + "/test.prg" )
curl_global_cleanup()
return nil

function FtpUploadFile( cUrlFileName, cFileName )

local hCurl, uValue, nResult

if ! empty( hCurl := curl_easy_init() )
curl_easy_setopt( hCurl, HB_CURLOPT_URL, cUrlFileName )
curl_easy_setopt( hCurl, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( hCurl, HB_CURLOPT_UPLOAD )
curl_easy_setopt( hCurl, HB_CURLOPT_UL_FILE_SETUP, cFileName )
curl_easy_setopt( hCurl, HB_CURLOPT_INFILESIZE, hb_vfSize( cFileName ) )
curl_easy_setopt( hCurl, HB_CURLOPT_USERPWD, "username:password" )
curl_easy_setopt( hCurl, HB_CURLOPT_FAILONERROR, .T. )
curl_easy_setopt( hCurl, HB_CURLOPT_FILETIME, .T. )
curl_easy_setopt( hCurl, HB_CURLOPT_NOPROGRESS, 0 )
curl_easy_setopt( hCurl, HB_CURLOPT_VERBOSE, .F. )
if ( nResult := curl_easy_perform( hCurl ) ) == 0
curl_easy_dl_buff_get( hCurl )
else
? "error code:" + Str( nResult )
endif
endif
return nResult == HB_CURLE_OK

Add hbcurl.lib to script for compile and link
if you use SFTP, add hbcurls.lib
and, in your code, add
curl_easy_setopt( hCurl, HB_CURLOPT_PROTOCOLS, HB_CURLPROTO_SFTP )

Please, revise this

Jayadev U

unread,
Jul 2, 2021, 3:26:24 AM7/2/21
to harbou...@googlegroups.com
Hi,

Thanks to all who replied to the thread.

I confirm it works correctly now per information in the following thread.

I mention the thread again for the posterity:

https://groups.google.com/g/harbour-devel/c/1duN_CHot4E/m/3q5iz1x9AgAJ


Warm regards,

Jayadev


-----Original Message-----
From: harbou...@googlegroups.com [mailto:harbou...@googlegroups.com] On Behalf Of Klas Engwall
Sent: 01 July 2021 06:52 PM
To: harbou...@googlegroups.com
Subject: Re: [harbour-users] FTP fails when there is an '@' in the user name or password

To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/04b200ab-cfa0-0d6e-b2f9-048f98c5e952%40engwall.com.

Jayadev U

unread,
Jul 2, 2021, 3:27:58 AM7/2/21
to harbou...@googlegroups.com

Hi,

 

Thanks so much for the sample.

 

Warm regards,

 

Jayadev

Jack Tearle

unread,
Jul 2, 2021, 11:49:21 AM7/2/21
to harbou...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages