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 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 (format (current-error-port)
113 "FTP to `~a' failed: ~A: ~A~%"
114 host code message)
115 (close s)
116 #f))))
117
118 (lambda args
119 ;; Connection failed, so try one of the other addresses.
120 (close s)
121 (if (null? addresses)
122 (apply throw args)
123 (loop (cdr addresses))))))))
124
125 (define (ftp-close conn)
126 (close (ftp-connection-socket conn)))
127
128 (define %char-set:not-slash
129 (char-set-complement (char-set #\/)))
130
131 (define (ftp-chdir conn dir)
132 "Change to directory DIR."
133
134 ;; On ftp.gnupg.org, "PASV" right after "CWD /gcrypt/gnupg" hangs. Doing
135 ;; CWD in two steps works, so just do this.
136 (let ((components (string-tokenize dir %char-set:not-slash)))
137 (fold (lambda (dir result)
138 (%ftp-command (string-append "CWD " dir) 250
139 (ftp-connection-socket conn)))
140 #f
141 (if (string-prefix? "/" dir)
142 (cons "/" components)
143 components))))
144
145 (define (ftp-size conn file)
146 "Return the size in bytes of FILE."
147 (let ((message (%ftp-command (string-append "SIZE " file) 213
148 (ftp-connection-socket conn))))
149 (string->number (string-trim-both message))))
150
151 (define (ftp-pasv conn)
152 (define %pasv-rx
153 (make-regexp "([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+)"))
154
155 (let ((message (%ftp-command "PASV" 227 (ftp-connection-socket conn))))
156 (cond ((regexp-exec %pasv-rx message)
157 =>
158 (lambda (match)
159 (+ (* (string->number (match:substring match 5)) 256)
160 (string->number (match:substring match 6)))))
161 (else
162 (throw 'ftp-error conn "PASV" 227 message)))))
163
164 (define (address-with-port sa port)
165 "Return a socket-address object based on SA, but with PORT."
166 (let ((fam (sockaddr:fam sa))
167 (addr (sockaddr:addr sa)))
168 (cond ((= fam AF_INET)
169 (make-socket-address fam addr port))
170 ((= fam AF_INET6)
171 (make-socket-address fam addr port
172 (sockaddr:flowinfo sa)
173 (sockaddr:scopeid sa)))
174 (else #f))))
175
176 (define* (ftp-list conn #:optional directory)
177 (if directory
178 (ftp-chdir conn directory))
179
180 (let* ((port (ftp-pasv conn))
181 (ai (ftp-connection-addrinfo conn))
182 (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)
183 (addrinfo:protocol ai))))
184 (connect s (address-with-port (addrinfo:addr ai) port))
185 (setvbuf s _IOLBF)
186
187 (dynamic-wind
188 (lambda () #t)
189 (lambda ()
190 (%ftp-command "LIST" 150 (ftp-connection-socket conn))
191
192 (let loop ((line (read-line s))
193 (result '()))
194 (cond ((eof-object? line) (reverse result))
195 ((regexp-exec %ftp-ready-rx line)
196 =>
197 (lambda (match)
198 (let ((code (string->number (match:substring match 1))))
199 (if (= 126 code)
200 (reverse result)
201 (throw 'ftp-error conn "LIST" code)))))
202 (else
203 (loop (read-line s)
204 (match (reverse (string-tokenize line))
205 ((file _ ... permissions)
206 (let ((type (case (string-ref permissions 0)
207 ((#\d) 'directory)
208 (else 'file))))
209 (cons (list file type) result)))
210 ((file _ ...)
211 (cons (cons file 'file) result))))))))
212 (lambda ()
213 (close s)
214 (let-values (((code message) (%ftp-listen (ftp-connection-socket conn))))
215 (or (eqv? code 226)
216 (throw 'ftp-error conn "LIST" code message)))))))
217
218 (define* (ftp-retr conn file #:optional directory)
219 "Retrieve FILE from DIRECTORY (or, if omitted, the current directory) from
220 FTP connection CONN. Return a binary port to that file. The returned port
221 must be closed before CONN can be used for other purposes."
222 (if directory
223 (ftp-chdir conn directory))
224
225 ;; Ask for "binary mode".
226 (%ftp-command "TYPE I" 200 (ftp-connection-socket conn))
227
228 (let* ((port (ftp-pasv conn))
229 (ai (ftp-connection-addrinfo conn))
230 (s (with-fluids ((%default-port-encoding #f))
231 (socket (addrinfo:fam ai) (addrinfo:socktype ai)
232 (addrinfo:protocol ai)))))
233 (define (terminate)
234 (close s)
235 (let-values (((code message) (%ftp-listen (ftp-connection-socket conn))))
236 (or (eqv? code 226)
237 (throw 'ftp-error conn "LIST" code message))))
238
239 (connect s (address-with-port (addrinfo:addr ai) port))
240 (setvbuf s _IOLBF)
241
242 (%ftp-command (string-append "RETR " file)
243 150 (ftp-connection-socket conn))
244
245 (make-custom-binary-input-port "FTP RETR port"
246 (rec (read! bv start count)
247 (match (get-bytevector-n! s bv
248 start count)
249 ((? eof-object?) 0)
250 (0
251 ;; Nothing available yet, so try
252 ;; again. This is important because
253 ;; the return value of `read!' makes
254 ;; it impossible to distinguish
255 ;; between "not yet" and "EOF".
256 (read! bv start count))
257 (read read)))
258 #f #f ; no get/set position
259 terminate)))
260
261 ;;; ftp-client.scm ends here