Subject: bug#26191: Allow custom FTP logins.

From: Roel Janssen <[email-masked]>
Date: Tue, 21 Mar 2017 09:51:57 +0100

>From c5e193ba69392efdb3ef9769ea4e3eb705dbb2f0 Mon Sep 17 00:00:00 2001
From: Roel Janssen <[email-masked]>
Date: Tue, 21 Mar 2017 09:41:49 +0100
Subject: [PATCH] download: Handle username and password properties for FTP
 URIs.

* guix/build/download.scm (ftp-fetch): Process username and password from a URI.
---
 guix/build/download.scm | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/guix/build/download.scm b/guix/build/download.scm
index 36c815c..c5dddf8 100644
--- a/guix/build/download.scm
+++ b/guix/build/download.scm
@@ -241,7 +241,18 @@ and 'guix publish', something like
 (define* (ftp-fetch uri file #:key timeout)
   "Fetch data from URI and write it to FILE.  Return FILE on success.  Bail
 out if the connection could not be established in less than TIMEOUT seconds."
-  (let* ((conn (ftp-open (uri-host uri) #:timeout timeout))
+  (let* ((userinfo (string-split (uri-userinfo uri) #\:))
+         (conn (match userinfo
+                 (("")
+                  (ftp-open (uri-host uri) #:timeout timeout))
+                 (((? string? user))
+                  (ftp-open (uri-host uri) #:timeout timeout
+                                           #:username user))
+                 (((? string? user) (? string? pass))
+                  (ftp-open (uri-host uri) #:timeout timeout
+                                           #:username user
+                                           #:password pass))
+                 (_ (ftp-open (uri-host uri) #:timeout timeout))))
          (size (false-if-exception (ftp-size conn (uri-path uri))))
          (in   (ftp-retr conn (basename (uri-path uri))
                          (dirname (uri-path uri)))))
-- 
2.7.4

Hello Danny and Ludo’,

Thanks for your quick responses and feedback!

Ludovic Courtès writes:

> Roel Janssen <[email-masked]> skribis:
>
>> From 8536ba69ec3a04930e8a78c8f2b824df3e8c0454 Mon Sep 17 00:00:00 2001
>> From: Roel Janssen <[email-masked]>
>> Date: Mon, 20 Mar 2017 12:57:25 +0100
>> Subject: [PATCH 1/2] ftp-client: Allow custom username and password for FTP
>>  servers.
>>
>> * guix/ftp-client.scm (ftp-open): Add username and password arguments.
>
> OK!
>
>> From e7263ce1d1a242f187c9801b14ea47043f59be8e Mon Sep 17 00:00:00 2001
>> From: Roel Janssen <[email-masked]>
>> Date: Mon, 20 Mar 2017 12:59:59 +0100
>> Subject: [PATCH 2/2] download: Handle username and password properties for FTP
>>  uris.
>>
>> guix/build/download.scm (ftp-fetch): Process username and password from a URI.
>
> [...]
>
>> +  (let* ((userinfo (string-split (uri-userinfo uri) #\:))
>> +         (username (if (and (> (length userinfo) 0)
>> +                            (not (string= (car userinfo) "")))
>> +                       (car userinfo) #f))
>> +         (password (if (> (length userinfo) 1) (cadr userinfo) #f))
>> +         (conn (ftp-open (uri-host uri) #:timeout timeout
>> +                                        #:username username
>> +                                        #:password password))
>
> Of course I have to agree with Danny here regarding ‘match’.  ;-)
>
>   (let ((user (match userinfo
>                 (() #f)
>                 (("") #f)
>                 (((? string? user)) user)))
>         (pass (match userinfo
>                  …)))
>     …)
>
> OK with a change along these lines.

I am unsure about this patch (see attached).  Is this what you had in mind?
I would probably have to add a copyright line as well, right?

Thank you!

Kind regards,
Roel Janssen