1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2010, 2011, 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
42 ;;; Simple FTP client (RFC 959).
46 ;; TODO: Use SRFI-3{4,5} error conditions.
48 (define-record-type <ftp-connection>
49 (%make-ftp-connection socket addrinfo)
51 (socket ftp-connection-socket)
52 (addrinfo ftp-connection-addrinfo))
55 (make-regexp "^([0-9]{3}) (.+)$"))
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)
63 (values (string->number (match:substring match 1))
64 (match:substring match 2))))
66 (loop (read-line port))))))
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)
73 (throw 'ftp-error port command code message))))
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)))
82 ((331) (%ftp-command (string-append "PASS " pass) 230 port))
83 (else (throw 'ftp-error port command code message))))))
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.
93 (if (number? port) (number->string port) port)
94 (if (number? port) AI_NUMERICSERV 0)))
96 (let loop ((addresses addresses))
97 (let* ((ai (car addresses))
98 (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)
99 (addrinfo:protocol ai))))
103 (connect s (addrinfo:addr ai))
105 (let-values (((code message) (%ftp-listen s)))
108 ;;(%ftp-command "OPTS UTF8 ON" 200 s)
109 (%ftp-login "anonymous" "guix@example.com" s)
110 (%make-ftp-connection s ai))
113 (throw 'ftp-error s "log-in" code message)))))
116 ;; Connection failed, so try one of the other addresses.
118 (if (null? addresses)
120 (loop (cdr addresses))))))))
122 (define (ftp-close conn)
123 (close (ftp-connection-socket conn)))
125 (define %char-set:not-slash
126 (char-set-complement (char-set #\/)))
128 (define (ftp-chdir conn dir)
129 "Change to directory DIR."
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)))
138 (if (string-prefix? "/" dir)
139 (cons "/" components)
142 (define (ftp-size conn file)
143 "Return the size in bytes of FILE."
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))
149 (let ((message (%ftp-command (string-append "SIZE " file) 213
150 (ftp-connection-socket conn))))
151 (string->number (string-trim-both message))))
153 (define (ftp-pasv conn)
155 (make-regexp "([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+)"))
157 (let ((message (%ftp-command "PASV" 227 (ftp-connection-socket conn))))
158 (cond ((regexp-exec %pasv-rx message)
161 (+ (* (string->number (match:substring match 5)) 256)
162 (string->number (match:substring match 6)))))
164 (throw 'ftp-error conn "PASV" 227 message)))))
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))
173 (make-socket-address fam addr port
174 (sockaddr:flowinfo sa)
175 (sockaddr:scopeid sa)))
178 (define* (ftp-list conn #:optional directory)
180 (ftp-chdir conn directory))
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))
192 (%ftp-command "LIST" 150 (ftp-connection-socket conn))
194 (let loop ((line (read-line s))
196 (cond ((eof-object? line) (reverse result))
197 ((regexp-exec %ftp-ready-rx line)
200 (let ((code (string->number (match:substring match 1))))
203 (throw 'ftp-error conn "LIST" code)))))
206 (match (reverse (string-tokenize line))
207 ((file _ ... permissions)
208 (let ((type (case (string-ref permissions 0)
211 (cons (list file type) result)))
213 (cons (cons file 'file) result))))))))
216 (let-values (((code message) (%ftp-listen (ftp-connection-socket conn))))
218 (throw 'ftp-error conn "LIST" code message)))))))
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."
225 (ftp-chdir conn directory))
227 ;; Ask for "binary mode".
228 (%ftp-command "TYPE I" 200 (ftp-connection-socket conn))
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)))))
237 (let-values (((code message) (%ftp-listen (ftp-connection-socket conn))))
239 (throw 'ftp-error conn "LIST" code message))))
241 (connect s (address-with-port (addrinfo:addr ai) port))
244 (%ftp-command (string-append "RETR " file)
245 150 (ftp-connection-socket conn))
247 (make-custom-binary-input-port "FTP RETR port"
248 (rec (read! bv start count)
249 (match (get-bytevector-n! s bv
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))
260 #f #f ; no get/set position
263 ;;; ftp-client.scm ends here