Merge branch 'master' into core-updates
[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 ftp-open
34 ftp-close
35 ftp-chdir
36 ftp-size
37 ftp-list
38 ftp-retr))
39
40 ;;; Commentary:
41 ;;;
42 ;;; Simple FTP client (RFC 959).
43 ;;;
44 ;;; Code:
45
46 ;; TODO: Use SRFI-3{4,5} error conditions.
47
48 (define-record-type <ftp-connection>
49 (%make-ftp-connection socket addrinfo)
50 ftp-connection?
51 (socket ftp-connection-socket)
52 (addrinfo ftp-connection-addrinfo))
53
54 (define %ftp-ready-rx
55 (make-regexp "^([0-9]{3}) (.+)$"))
56
57 (define (%ftp-listen port)
58 (let loop ((line (read-line port)))
59 (cond ((eof-object? line) (values line #f))
60 ((regexp-exec %ftp-ready-rx line)
61 =>
62 (lambda (match)
63 (values (string->number (match:substring match 1))
64 (match:substring match 2))))
65 (else
66 (loop (read-line port))))))
67
68 (define (%ftp-command command expected-code port)
69 (format port "~A~A~A" command (string #\return) (string #\newline))
70 (let-values (((code message) (%ftp-listen port)))
71 (if (eqv? code expected-code)
72 message
73 (throw 'ftp-error port command code message))))
74
75 (define (%ftp-login user pass port)
76 (let ((command (string-append "USER " user
77 (string #\return) (string #\newline))))
78 (display command port)
79 (let-values (((code message) (%ftp-listen port)))
80 (case code
81 ((230) #t)
82 ((331) (%ftp-command (string-append "PASS " pass) 230 port))
83 (else (throw 'ftp-error port command code message))))))
84
85 (define* (ftp-open host #:optional (port 21))
86 "Open an FTP connection to HOST on PORT (a service-identifying string,
87 or a TCP port number), and return it."
88 ;; Use 21 as the default PORT instead of "ftp", to avoid depending on
89 ;; libc's NSS, which is not available during bootstrap.
90
91 (define addresses
92 (getaddrinfo host
93 (if (number? port) (number->string port) port)
94 (if (number? port) AI_NUMERICSERV 0)))
95
96 (let loop ((addresses addresses))
97 (let* ((ai (car addresses))
98 (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)
99 (addrinfo:protocol ai))))
100
101 (catch 'system-error
102 (lambda ()
103 (connect s (addrinfo:addr ai))
104 (setvbuf s _IOLBF)
105 (let-values (((code message) (%ftp-listen s)))
106 (if (eqv? code 220)
107 (begin
108 ;;(%ftp-command "OPTS UTF8 ON" 200 s)
109 (%ftp-login "anonymous" "guix@example.com" s)
110 (%make-ftp-connection s ai))
111 (begin
112 (close s)
113 (throw 'ftp-error s "log-in" code message)))))
114
115 (lambda args
116 ;; Connection failed, so try one of the other addresses.
117 (close s)
118 (if (null? addresses)
119 (apply throw args)
120 (loop (cdr addresses))))))))
121
122 (define (ftp-close conn)
123 (close (ftp-connection-socket conn)))
124
125 (define %char-set:not-slash
126 (char-set-complement (char-set #\/)))
127
128 (define (ftp-chdir conn dir)
129 "Change to directory DIR."
130
131 ;; On ftp.gnupg.org, "PASV" right after "CWD /gcrypt/gnupg" hangs. Doing
132 ;; CWD in two steps works, so just do this.
133 (let ((components (string-tokenize dir %char-set:not-slash)))
134 (fold (lambda (dir result)
135 (%ftp-command (string-append "CWD " dir) 250
136 (ftp-connection-socket conn)))
137 #f
138 (if (string-prefix? "/" dir)
139 (cons "/" components)
140 components))))
141
142 (define (ftp-size conn file)
143 "Return the size in bytes of FILE."
144
145 ;; Ask for "binary mode", otherwise some servers, such as sourceware.org,
146 ;; fail with 550 ("SIZE not allowed in ASCII mode").
147 (%ftp-command "TYPE I" 200 (ftp-connection-socket conn))
148
149 (let ((message (%ftp-command (string-append "SIZE " file) 213
150 (ftp-connection-socket conn))))
151 (string->number (string-trim-both message))))
152
153 (define (ftp-pasv conn)
154 (define %pasv-rx
155 (make-regexp "([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+)"))
156
157 (let ((message (%ftp-command "PASV" 227 (ftp-connection-socket conn))))
158 (cond ((regexp-exec %pasv-rx message)
159 =>
160 (lambda (match)
161 (+ (* (string->number (match:substring match 5)) 256)
162 (string->number (match:substring match 6)))))
163 (else
164 (throw 'ftp-error conn "PASV" 227 message)))))
165
166 (define (address-with-port sa port)
167 "Return a socket-address object based on SA, but with PORT."
168 (let ((fam (sockaddr:fam sa))
169 (addr (sockaddr:addr sa)))
170 (cond ((= fam AF_INET)
171 (make-socket-address fam addr port))
172 ((= fam AF_INET6)
173 (make-socket-address fam addr port
174 (sockaddr:flowinfo sa)
175 (sockaddr:scopeid sa)))
176 (else #f))))
177
178 (define* (ftp-list conn #:optional directory)
179 (if directory
180 (ftp-chdir conn directory))
181
182 (let* ((port (ftp-pasv conn))
183 (ai (ftp-connection-addrinfo conn))
184 (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)
185 (addrinfo:protocol ai))))
186 (connect s (address-with-port (addrinfo:addr ai) port))
187 (setvbuf s _IOLBF)
188
189 (dynamic-wind
190 (lambda () #t)
191 (lambda ()
192 (%ftp-command "LIST" 150 (ftp-connection-socket conn))
193
194 (let loop ((line (read-line s))
195 (result '()))
196 (cond ((eof-object? line) (reverse result))
197 ((regexp-exec %ftp-ready-rx line)
198 =>
199 (lambda (match)
200 (let ((code (string->number (match:substring match 1))))
201 (if (= 126 code)
202 (reverse result)
203 (throw 'ftp-error conn "LIST" code)))))
204 (else
205 (loop (read-line s)
206 (match (reverse (string-tokenize line))
207 ((file _ ... permissions)
208 (let ((type (case (string-ref permissions 0)
209 ((#\d) 'directory)
210 (else 'file))))
211 (cons (list file type) result)))
212 ((file _ ...)
213 (cons (cons file 'file) result))))))))
214 (lambda ()
215 (close s)
216 (let-values (((code message) (%ftp-listen (ftp-connection-socket conn))))
217 (or (eqv? code 226)
218 (throw 'ftp-error conn "LIST" code message)))))))
219
220 (define* (ftp-retr conn file #:optional directory)
221 "Retrieve FILE from DIRECTORY (or, if omitted, the current directory) from
222 FTP connection CONN. Return a binary port to that file. The returned port
223 must be closed before CONN can be used for other purposes."
224 (if directory
225 (ftp-chdir conn directory))
226
227 ;; Ask for "binary mode".
228 (%ftp-command "TYPE I" 200 (ftp-connection-socket conn))
229
230 (let* ((port (ftp-pasv conn))
231 (ai (ftp-connection-addrinfo conn))
232 (s (with-fluids ((%default-port-encoding #f))
233 (socket (addrinfo:fam ai) (addrinfo:socktype ai)
234 (addrinfo:protocol ai)))))
235 (define (terminate)
236 (close s)
237 (let-values (((code message) (%ftp-listen (ftp-connection-socket conn))))
238 (or (eqv? code 226)
239 (throw 'ftp-error conn "LIST" code message))))
240
241 (connect s (address-with-port (addrinfo:addr ai) port))
242 (setvbuf s _IOLBF)
243
244 (%ftp-command (string-append "RETR " file)
245 150 (ftp-connection-socket conn))
246
247 (make-custom-binary-input-port "FTP RETR port"
248 (rec (read! bv start count)
249 (match (get-bytevector-n! s bv
250 start count)
251 ((? eof-object?) 0)
252 (0
253 ;; Nothing available yet, so try
254 ;; again. This is important because
255 ;; the return value of `read!' makes
256 ;; it impossible to distinguish
257 ;; between "not yet" and "EOF".
258 (read! bv start count))
259 (read read)))
260 #f #f ; no get/set position
261 terminate)))
262
263 ;;; ftp-client.scm ends here