Debian Codegraph @

lib/rfc/ftp.scm

raw
1 ;;; ftp.scm - FTP library
2 ;;;
3 ;;; Copyright (c) 2005 OOHASHI Daichi, All rights reserved.
4 ;;; Copyright (c) 2007-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; RFC 959 FILE TRANSFER PROTOCOL (FTP)
35 ;; RFC 1123 Requirements for Internet Hosts -- Application and Support
36 ;; RFC 2428 FTP Extensions for IPv6 and NATs
37
38 (define-module rfc.ftp
39 (use srfi.13)
40 (use srfi.19)
41 (use rfc.uri)
42 (use gauche.net)
43 (use gauche.logger)
44 (export <ftp-connection> <ftp-error> call-with-ftp-connection
45 ftp-chdir ftp-current-directory ftp-get ftp-size
46 ftp-help ftp-list ftp-login ftp-ls ftp-mdtm ftp-mtime
47 ftp-mkdir ftp-name-list ftp-noop ftp-passive? ftp-put
48 ftp-put-unique ftp-quit ftp-remove ftp-rename ftp-rmdir
49 ftp-site ftp-stat ftp-system ftp-transfer-type
50 ))
51 (select-module rfc.ftp)
52
53 (define-constant *default-ftp-port* 21)
54
55 (define-constant *anon-user* "anonymous")
56
57 (define-constant *anon-pass* "anonymous@")
58
59 (define-class <ftp-connection> ()
60 ((transfer-type :init-value 'binary
61 :init-keyword :transfer-type
62 :accessor ftp-transfer-type)
63 (passive :init-keyword :passive
64 :getter ftp-passive?)
65 (log-drain :init-keyword :log-drain
66 :init-value #f)
67 (socket :init-keyword :socket)))
68
69 (define (call-with-ftp-connection uri proc . keys)
70 (receive (user host port) (uri-decompose-authority uri)
71 (let1 conn (apply ftp-login host
72 (cond-list
73 [user @ (list :username user)]
74 [port @ (list :port port)]
75 [#t @ keys]))
76 (unwind-protect (proc conn)
77 (ftp-quit conn)))))
78
79 ;; rfc 959 6(5) type commands
80
81 ;; USER <SP> <username> <CRLF>
82 ;; PASS <SP> <password> <CRLF>
83 ;; ACCT <SP> <account-information> <CRLF>
84 (define (ftp-login host :key (account "") (passive #f)
85 (port *default-ftp-port*) (username *anon-user*)
86 (password (if (string=? username *anon-user*) *anon-pass* ""))
87 (log-drain #f))
88 (define (do-login conn)
89 (let retry ()
90 (if (string-prefix? "120" (get-response conn))
91 (begin (sys-sleep 1) (retry))
92 (let1 r1 (send-command conn "USER" username)
93 (if (not (string-prefix? "3" r1))
94 r1
95 (let1 r2 (send-command conn "PASS" password)
96 (if (not (string-prefix? "3" r2))
97 r2
98 (send-command conn "ACCT" account))))))))
99 (let* ((conn (make <ftp-connection>
100 :passive passive
101 :socket (make-client-socket 'inet host port)
102 :log-drain log-drain))
103 (res (do-login conn)))
104 (if (not (string-prefix? "2" res))
105 (ftp-error res)
106 conn)))
107
108 ;; rfc 959 6(1) type commands
109
110 ;; QUIT <CRLF>
111 (define (ftp-quit conn)
112 (unwind-protect
113 (simple-command conn "QUIT")
114 (if-let1 s (ref conn 'socket)
115 (begin (socket-shutdown s) (set! (ref conn 'socket) #f)))))
116
117 ;; *ABOR <CRLF>
118
119 ;; *ALLO <SP> <decimal-integer> [<SP> R <SP> <decimal-integer>] <CRLF>
120
121
122 ;; CWD <SP> <pathname> <CRLF>
123 ;; CDUP <CRLF>
124 (define (ftp-chdir conn dirname)
125 (if (string=? dirname "..")
126 (simple-command conn "CDUP")
127 (simple-command conn "CWD" dirname)))
128
129 ;; DELE <SP> <pathname> <CRLF>
130 (define (ftp-remove conn path) (simple-command conn "DELE" path))
131
132 ;; HELP [<SP> <string>] <CRLF>
133 (define (ftp-help conn . opt) (apply simple-command conn "HELP" opt))
134
135 ;; MKD <SP> <pathname> <CRLF>
136 ;; PWD <CRLF>
137 (define (ftp-mkdir conn dirname)(parse-257 (simple-command conn "MKD" dirname)))
138 (define (ftp-current-directory conn) (parse-257 (simple-command conn "PWD")))
139
140 (define (parse-257 res)
141 (rxmatch-if (#/^257 \"((?:[^\"]|\"\")+)\"/ res) (_ dirname)
142 (regexp-replace-all #/""/ dirname "\"")
143 (ftp-error res)))
144
145 ;; *MODE <SP> <mode-code> <CRLF>
146 ;; *SMNT <SP> <pathname> <CRLF>
147
148
149 ;; SITE <SP> <string> <CRLF>
150 (define (ftp-site conn arg) (simple-command conn "SITE" arg))
151
152
153 ;; *STRU <SP> <structure-code> <CRLF>
154
155
156 ;; RMD <SP> <pathname> <CRLF>
157 (define (ftp-rmdir conn dirname) (simple-command conn "RMD" dirname))
158
159 ;; STAT [<SP> <pathname>] <CRLF>
160 (define (ftp-stat conn . opt) (apply simple-command conn "STAT" opt))
161
162 ;; SYST <CRLF>
163 (define (ftp-system conn) (string-drop (simple-command conn "SYST") 4))
164
165 ;; SIZE
166 (define (ftp-size conn path)
167 (ftp-set-type conn) ; SIZE is affected by transfer type
168 (x->integer (string-drop (simple-command conn "SIZE" path) 4)))
169
170 ;; MDTM
171 (define (ftp-mdtm conn path) (simple-command conn "MDTM" path))
172
173 (define (ftp-mtime conn path :optional (local-time? #f))
174 (rxmatch-let (#/(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/ (ftp-mdtm conn path))
175 (#f year month day hour min sec)
176 (date->time-utc
177 (make-date 0 (x->integer sec) (x->integer min) (x->integer hour)
178 (x->integer day) (x->integer month) (x->integer year)
179 (if local-time? (date-zone-offset (current-date)) 0)))))
180
181 ;; NOOP <CRLF>
182 (define (ftp-noop conn) (simple-command conn "NOOP"))
183
184
185 ;;; rfc 959 6(2) type command
186
187 ;; *APPE <SP> <pathname> <CRLF>
188
189 ;; LIST [<SP> <pathname>] <CRLF>
190 ;; NLST [<SP> <pathname>] <CRLF>
191 (define (make-lister cmd)
192 (lambda (conn . opt)
193 (req&recv conn
194 (cut apply send-command conn cmd opt)
195 (cut port->string-list <>)
196 'ascii)))
197
198 (define ftp-list (make-lister "LIST"))
199 (define ftp-name-list (make-lister "NLST"))
200 (define ftp-ls ftp-name-list)
201
202 ;; *REIN <CRLF>
203
204 ;; *REST <SP> <marker> <CRLF>
205
206 ;; RETR <SP> <pathname> <CRLF>
207 (define (ftp-get conn path :key (sink (open-output-string))
208 (flusher get-output-string))
209 (req&recv conn
210 (cut send-command conn "RETR" path)
211 (lambda (in) (copy-port in sink) (flusher sink))))
212
213 ;; STOR <SP> <pathname> <CRLF>
214 (define (ftp-put conn from-file :optional (to-file (sys-basename from-file)))
215 (values-ref
216 (call-with-input-file from-file
217 (cute req&send conn (cut send-command conn "STOR" to-file) <>))
218 0))
219
220 ;; STOU <CRLF>
221 (define (ftp-put-unique conn from-file)
222 (call-with-input-file from-file
223 (cute req&send conn (cut send-command conn "STOU") <>)))
224
225 ;;; rfc 959 6(3) type commands
226
227 ;; RNFR <SP> <pathname> <CRLF>
228 ;; RNTO <SP> <pathname> <CRLF>
229 (define (ftp-rename conn from to)
230 (let1 res1 (send-command conn "RNFR" from)
231 (if (not (string-prefix? "3" res1))
232 (ftp-error res1)
233 (let1 res2 (send-command conn "RNTO" to)
234 (if (not (string-prefix? "2" res2))
235 (ftp-error res2)
236 res2)))))
237
238
239 ;; private utilities
240
241 ;; PORT <SP> <host-port> <CRLF>
242 ;; EPRT <SP> <d><net-prt><d><net-addr><d><tcp-port><d> <CRLF>
243 ;; PASV <CRLF>
244 ;; EPSV <SP> <net-prt> <CRLF>
245 ;; open data connection to server and pass it to proc.
246 (define (call-with-data-connection conn proc)
247 ;; returns a thunk to create data connection socket, and a thunk to
248 ;; clean up.
249 (define (dcsock)
250 (if (ftp-passive? conn)
251 (if (ipv4? conn)
252 (parse-227 (simple-command conn "PASV"))
253 (parse-229 (simple-command conn "EPSV")))
254 (make-asock)))
255 (define (ipv4? conn)
256 (is-a? (socket-address (ref conn 'socket)) <sockaddr-in>))
257 (define (parse-227 res)
258 (if (not (string-prefix? "227" res))
259 (ftp-error res)
260 (rxmatch-let (#/\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)/ res)
261 (#f h1 h2 h3 h4 p1 p2)
262 (let1 ds (make-client-socket 'inet
263 (string-join (list h1 h2 h3 h4) ".")
264 (+ (* 256 (x->integer p1))
265 (x->integer p2)))
266 (values (lambda () ds)
267 (lambda () (socket-shutdown ds 2)))))))
268 (define (parse-229 res)
269 (if (not (string-prefix? "229" res))
270 (ftp-error res)
271 (rxmatch-let (#/\((.+?)\)/ res) (#f s)
272 (let1 d (regexp-quote (substring s 0 1))
273 (rxmatch-let ((string->regexp #"~|d|~|d|~|d|([^~|d|]+)~|d|") s) (#f port)
274 (let1 ds (make-client-socket
275 'inet
276 (sockaddr-addr (socket-address (ref conn 'socket)))
277 (string->number port))
278 (values (lambda () ds)
279 (lambda () (socket-shutdown ds 2)))))))))
280 (define (make-asock)
281 (let* ((asock (make-server-socket (make (class-of
282 (socket-address (ref conn 'socket)))
283 :host "localhost")))
284 (psname (sockaddr-name (socket-address asock))))
285 (if (ipv4? conn)
286 (rxmatch-let (#/(\d+)\.(\d+)\.(\d+)\.(\d+):(\d+)/ psname)
287 (#f a b c d p)
288 (receive (q r) (quotient&remainder (string->number p) 256)
289 (simple-command conn
290 "PORT"
291 (format #f "~A,~A,~A,~A,~A,~A" a b c d q r))))
292 (rxmatch-let (#/\[(.+?)\]:(\d+)/ psname) (#f host port)
293 (simple-command conn
294 "EPRT"
295 (format #f "|2|~A|~A|" host port))))
296 (let ((ds #f))
297 (values (lambda () (set! ds (socket-accept asock)) ds)
298 (lambda ()
299 (when ds (socket-shutdown ds 2))
300 (socket-shutdown asock 2))))))
301 (receive (sock-thunk cleanup-thunk) (dcsock)
302 (unwind-protect (proc sock-thunk) (cleanup-thunk))))
303
304 ;; error reporting routine.
305 ;; it should be raise conditon with respect to the response code.
306 (define-condition-type <ftp-error> <error> #f)
307
308 (define (ftp-error res . args)
309 (apply error <ftp-error> res args))
310
311 ;; send ftp command
312 (define (send-command conn cmd . args)
313 (let ((out (socket-output-port (ref conn 'socket)))
314 (log (ref conn 'log-drain))
315 (msg (with-output-to-string
316 (lambda ()
317 (display cmd)
318 (for-each (lambda (arg) (display " ") (display arg)) args)))))
319 (display msg out) (display "\r\n" out) (flush out)
320 (when log
321 (if (equal? cmd "PASS")
322 (log-format log ">>> PASS *****") ;; do not log password
323 (log-format log ">>> ~a" msg)))
324 (get-response conn)))
325
326 ;; send rfc-959 6(2) type command
327 (define (simple-command conn cmd . args)
328 (let1 res (apply send-command conn cmd args)
329 (if (string-prefix? "2" res)
330 res
331 (ftp-error res))))
332
333 ;; receive response from server
334 (define (get-response conn)
335 (define (get-resp in)
336 (let1 l (read-line in)
337 (rxmatch-if (#/^(\d\d\d)-/ l) (#f code)
338 (let loop ((rs (list l))
339 (line (read-line in)))
340 (if (and (string-prefix? code line)
341 (char=? (string-ref line 3) #\space))
342 (string-concatenate-reverse rs)
343 (loop (cons* line "\n" rs) (read-line in))))
344 l)))
345 (let1 res (get-resp (socket-input-port (ref conn 'socket)))
346 (cond ((ref conn 'log-drain) => (cut log-format <> "<<< ~a" res)))
347 (if (#/^[45]/ res)
348 (ftp-error res)
349 res)))
350
351
352 ;; TYPE <SP> <type-code> <CRLF>
353 (define (ftp-set-type conn :optional (type (ftp-transfer-type conn)))
354 (simple-command conn "TYPE"
355 (case type
356 [(ascii) "A"]
357 [(binary image) "I"]
358 [else (error "Invalid transfer type:" type)])))
359
360 ;; requrest server to send data and receive it
361 (define (req&recv conn cmdproc reader . opt)
362 (apply ftp-set-type conn opt)
363 (call-with-data-connection conn
364 (lambda (get-data-socket)
365 (let1 res (cmdproc)
366 (if (not (string-prefix? "1" res))
367 (ftp-error res)
368 (begin0
369 (let1 s (get-data-socket)
370 (unwind-protect (reader (socket-input-port s))
371 (socket-close s)))
372 (let1 res2 (get-response conn)
373 (when (not (string-prefix? "2" res2))
374 (ftp-error res2)))))))))
375
376 ;; request server to receive data and send data
377 (define (req&send conn cmdproc port)
378
379 (define (copy-data get-data-socket)
380 (copy-port port (socket-output-port (get-data-socket))))
381 (define (send-data)
382 (call-with-data-connection conn
383 (lambda (get-data-socket)
384 (let1 res (cmdproc)
385 (rxmatch-case res
386 [#/^1\d\d FILE: (.+)$/ (#f dst-path)
387 ;; RFC 1123 - 4.1.2.9 STOU Command: RFC-959 Section 4.1.3
388 (copy-data get-data-socket) dst-path]
389 [#/^1/ ()
390 ;; in case if the server doesn't conform RFC1123
391 (copy-data get-data-socket) #f]
392 [else (ftp-error res)])))))
393 (define (retrieve-response res dst-path)
394 (rxmatch-case res
395 [#/^2/ () (values res dst-path)]
396 ; vsftpd duplicates the 1XX reply for STOU.
397 [#/^1/ () (retrieve-response (get-response conn) dst-path)]
398 [else (ftp-error res)]))
399
400 (ftp-set-type conn)
401 (let1 dst-path (send-data)
402 (retrieve-response (get-response conn) dst-path)))