ftp-client: Default port for 'ftp-open' is now "ftp".
[jackhill/guix/guix.git] / guix / ftp-client.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2010, 2011, 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (guix ftp-client)
20 #:use-module (srfi srfi-1)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-11)
23 #:use-module (srfi srfi-26)
24 #:use-module (srfi srfi-31)
25 #:use-module (rnrs io ports)
26 #:use-module (rnrs bytevectors)
27 #:use-module (ice-9 match)
28 #:use-module (ice-9 regex)
29 #:use-module (ice-9 rdelim)
30 #:export (ftp-connection?
31 ftp-connection-addrinfo
32
33 connect*
34 ftp-open
35 ftp-close
36 ftp-chdir
37 ftp-size
38 ftp-list
39 ftp-retr))
40
41 ;;; Commentary:
42 ;;;
43 ;;; Simple FTP client (RFC 959).
44 ;;;
45 ;;; Code:
46
47 ;; TODO: Use SRFI-3{4,5} error conditions.
48
49 (define-record-type <ftp-connection>
50 (%make-ftp-connection socket addrinfo)
51 ftp-connection?
52 (socket ftp-connection-socket)
53 (addrinfo ftp-connection-addrinfo))
54
55 (define %ftp-ready-rx
56 (make-regexp "^([0-9]{3}) (.+)$"))
57
58 (define (%ftp-listen port)
59 (let loop ((line (read-line port)))
60 (cond ((eof-object? line) (values line #f))
61 ((regexp-exec %ftp-ready-rx line)
62 =>
63 (lambda (match)
64 (values (string->number (match:substring match 1))
65 (match:substring match 2))))
66 (else
67 (loop (read-line port))))))
68
69 (define (%ftp-command command expected-code port)
70 (format port "~A~A~A" command (string #\return) (string #\newline))
71 (let-values (((code message) (%ftp-listen port)))
72 (if (eqv? code expected-code)
73 message
74 (throw 'ftp-error port command code message))))
75
76 (define (%ftp-login user pass port)
77 (let ((command (string-append "USER " user
78 (string #\return) (string #\newline))))
79 (display command port)
80 (let-values (((code message) (%ftp-listen port)))
81 (case code
82 ((230) #t)
83 ((331) (%ftp-command (string-append "PASS " pass) 230 port))
84 (else (throw 'ftp-error port command code message))))))
85
86 (define-syntax-rule (catch-EINPROGRESS body ...)
87 (catch 'system-error
88 (lambda ()
89 body ...)
90 (lambda args
91 (unless (= (system-error-errno args) EINPROGRESS)
92 (apply throw args)))))
93
94 ;; XXX: For lack of a better place.
95 (define* (connect* s sockaddr #:optional timeout)
96 "When TIMEOUT is omitted or #f, this procedure is equivalent to 'connect'.
97 When TIMEOUT is a number, it is the (possibly inexact) maximum number of
98 seconds to wait for the connection to succeed."
99 (define (raise-error errno)
100 (throw 'system-error 'connect* "~A"
101 (list (strerror errno))
102 (list errno)))
103
104 (if timeout
105 (let ((flags (fcntl s F_GETFL)))
106 (fcntl s F_SETFL (logior flags O_NONBLOCK))
107 (catch-EINPROGRESS (connect s sockaddr))
108 (match (select '() (list s) (list s) timeout)
109 ((() () ())
110 ;; Time is up!
111 (raise-error ETIMEDOUT))
112 ((() (write) ())
113 ;; Check for ECONNREFUSED and the likes.
114 (fcntl s F_SETFL flags)
115 (let ((errno (getsockopt s SOL_SOCKET SO_ERROR)))
116 (unless (zero? errno)
117 (raise-error errno))))
118 ((() () (except))
119 ;; Seems like this cannot really happen, but who knows.
120 (let ((errno (getsockopt s SOL_SOCKET SO_ERROR)))
121 (raise-error errno)))))
122 (connect s sockaddr)))
123
124 (define* (ftp-open host #:optional (port "ftp") #:key timeout)
125 "Open an FTP connection to HOST on PORT (a service-identifying string,
126 or a TCP port number), and return it.
127
128 When TIMEOUT is not #f, it must be a (possibly inexact) number denoting the
129 maximum duration in seconds to wait for the connection to complete; passed
130 TIMEOUT, an ETIMEDOUT error is raised."
131 ;; Using "ftp" for PORT instead of 21 allows 'getaddrinfo' to return only
132 ;; TCP/IP addresses (otherwise it would return SOCK_DGRAM and SOCK_RAW
133 ;; addresses as well.) With our bootstrap Guile, which includes a
134 ;; statically-linked NSS, resolving "ftp" works well, as long as
135 ;; /etc/services is available.
136
137 (define addresses
138 (getaddrinfo host
139 (if (number? port) (number->string port) port)
140 (if (number? port)
141 (logior AI_ADDRCONFIG AI_NUMERICSERV)
142 AI_ADDRCONFIG)))
143
144 (let loop ((addresses addresses))
145 (match addresses
146 ((ai rest ...)
147 (let ((s (socket (addrinfo:fam ai)
148 ;; TCP/IP only
149 SOCK_STREAM IPPROTO_IP)))
150
151 (catch 'system-error
152 (lambda ()
153 (connect* s (addrinfo:addr ai) timeout)
154 (setvbuf s _IOLBF)
155 (let-values (((code message) (%ftp-listen s)))
156 (if (eqv? code 220)
157 (begin
158 ;;(%ftp-command "OPTS UTF8 ON" 200 s)
159 (%ftp-login "anonymous" "guix@example.com" s)
160 (%make-ftp-connection s ai))
161 (begin
162 (close s)
163 (throw 'ftp-error s "log-in" code message)))))
164
165 (lambda args
166 ;; Connection failed, so try one of the other addresses.
167 (close s)
168 (if (null? rest)
169 (apply throw args)
170 (loop rest)))))))))
171
172 (define (ftp-close conn)
173 (close (ftp-connection-socket conn)))
174
175 (define %char-set:not-slash
176 (char-set-complement (char-set #\/)))
177
178 (define (ftp-chdir conn dir)
179 "Change to directory DIR."
180
181 ;; On ftp.gnupg.org, "PASV" right after "CWD /gcrypt/gnupg" hangs. Doing
182 ;; CWD in two steps works, so just do this.
183 (let ((components (string-tokenize dir %char-set:not-slash)))
184 (fold (lambda (dir result)
185 (%ftp-command (string-append "CWD " dir) 250
186 (ftp-connection-socket conn)))
187 #f
188 (if (string-prefix? "/" dir)
189 (cons "/" components)
190 components))))
191
192 (define (ftp-size conn file)
193 "Return the size in bytes of FILE."
194
195 ;; Ask for "binary mode", otherwise some servers, such as sourceware.org,
196 ;; fail with 550 ("SIZE not allowed in ASCII mode").
197 (%ftp-command "TYPE I" 200 (ftp-connection-socket conn))
198
199 (let ((message (%ftp-command (string-append "SIZE " file) 213
200 (ftp-connection-socket conn))))
201 (string->number (string-trim-both message))))
202
203 (define (ftp-pasv conn)
204 (define %pasv-rx
205 (make-regexp "([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+)"))
206
207 (let ((message (%ftp-command "PASV" 227 (ftp-connection-socket conn))))
208 (cond ((regexp-exec %pasv-rx message)
209 =>
210 (lambda (match)
211 (+ (* (string->number (match:substring match 5)) 256)
212 (string->number (match:substring match 6)))))
213 (else
214 (throw 'ftp-error conn "PASV" 227 message)))))
215
216 (define (address-with-port sa port)
217 "Return a socket-address object based on SA, but with PORT."
218 (let ((fam (sockaddr:fam sa))
219 (addr (sockaddr:addr sa)))
220 (cond ((= fam AF_INET)
221 (make-socket-address fam addr port))
222 ((= fam AF_INET6)
223 (make-socket-address fam addr port
224 (sockaddr:flowinfo sa)
225 (sockaddr:scopeid sa)))
226 (else #f))))
227
228 (define* (ftp-list conn #:optional directory)
229 (if directory
230 (ftp-chdir conn directory))
231
232 (let* ((port (ftp-pasv conn))
233 (ai (ftp-connection-addrinfo conn))
234 (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)
235 (addrinfo:protocol ai))))
236 (connect s (address-with-port (addrinfo:addr ai) port))
237 (setvbuf s _IOLBF)
238
239 (dynamic-wind
240 (lambda () #t)
241 (lambda ()
242 (%ftp-command "LIST" 150 (ftp-connection-socket conn))
243
244 (let loop ((line (read-line s))
245 (result '()))
246 (cond ((eof-object? line) (reverse result))
247 ((regexp-exec %ftp-ready-rx line)
248 =>
249 (lambda (match)
250 (let ((code (string->number (match:substring match 1))))
251 (if (= 126 code)
252 (reverse result)
253 (throw 'ftp-error conn "LIST" code)))))
254 (else
255 (loop (read-line s)
256 (match (reverse (string-tokenize line))
257 ((file _ ... permissions)
258 (let ((type (case (string-ref permissions 0)
259 ((#\d) 'directory)
260 (else 'file))))
261 (cons (list file type) result)))
262 ((file _ ...)
263 (cons (cons file 'file) result))))))))
264 (lambda ()
265 (close s)
266 (let-values (((code message) (%ftp-listen (ftp-connection-socket conn))))
267 (or (eqv? code 226)
268 (throw 'ftp-error conn "LIST" code message)))))))
269
270 (define* (ftp-retr conn file #:optional directory)
271 "Retrieve FILE from DIRECTORY (or, if omitted, the current directory) from
272 FTP connection CONN. Return a binary port to that file. The returned port
273 must be closed before CONN can be used for other purposes."
274 (if directory
275 (ftp-chdir conn directory))
276
277 ;; Ask for "binary mode".
278 (%ftp-command "TYPE I" 200 (ftp-connection-socket conn))
279
280 (let* ((port (ftp-pasv conn))
281 (ai (ftp-connection-addrinfo conn))
282 (s (with-fluids ((%default-port-encoding #f))
283 (socket (addrinfo:fam ai) (addrinfo:socktype ai)
284 (addrinfo:protocol ai)))))
285 (define (terminate)
286 (close s)
287 (let-values (((code message) (%ftp-listen (ftp-connection-socket conn))))
288 (or (eqv? code 226)
289 (throw 'ftp-error conn "LIST" code message))))
290
291 (connect s (address-with-port (addrinfo:addr ai) port))
292 (setvbuf s _IOLBF)
293
294 (%ftp-command (string-append "RETR " file)
295 150 (ftp-connection-socket conn))
296
297 (make-custom-binary-input-port "FTP RETR port"
298 (rec (read! bv start count)
299 (match (get-bytevector-n! s bv
300 start count)
301 ((? eof-object?) 0)
302 (0
303 ;; Nothing available yet, so try
304 ;; again. This is important because
305 ;; the return value of `read!' makes
306 ;; it impossible to distinguish
307 ;; between "not yet" and "EOF".
308 (read! bv start count))
309 (read read)))
310 #f #f ; no get/set position
311 terminate)))
312
313 ;;; ftp-client.scm ends here