1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors calendar calendar.format classes combinators
4 combinators.short-circuit concurrency.promises continuations
5 destructors ftp io io.directories io.encodings
6 io.encodings.binary io.encodings.latin1 io.encodings.utf8
7 io.files io.files.info io.pathnames io.servers io.sockets
8 io.streams.string io.timeouts kernel logging math math.bitwise
9 math.parser namespaces sequences simple-tokenizer splitting
10 strings threads tools.files unicode ;
16 TUPLE: ftp-server < threaded-server { serving-directory string } ;
18 TUPLE: ftp-client user password extra-connection ;
20 TUPLE: ftp-command raw tokenized ;
21 : <ftp-command> ( str -- obj )
22 dup \ <ftp-command> DEBUG log-message
25 swap tokenize >>tokenized ;
34 C: <ftp-list> ftp-list
36 TUPLE: ftp-disconnect ;
37 C: <ftp-disconnect> ftp-disconnect
39 : (send-response) ( n string separator -- )
40 [ number>string write ] 2dip write ftp-send ;
42 : send-response ( ftp-response -- )
43 [ n>> ] [ strings>> ] bi
44 [ but-last-slice [ "-" (send-response) ] with each ]
45 [ first " " (send-response) ] 2bi ;
47 : make-path-relative? ( path -- ? )
50 [ drop server get serving-directory>> ]
53 : fixup-relative-path ( string -- string' )
54 dup make-path-relative? [
55 [ server get serving-directory>> ] dip append-relative-path
58 : server-response ( string n -- )
59 2dup number>string swap ":" glue \ server-response DEBUG log-message
62 swap add-response-line
65 : serving? ( path -- ? )
66 resolve-symlinks server get serving-directory>> head? ;
68 : can-serve-directory? ( path -- ? )
69 { [ file-exists? ] [ file-info directory? ] [ serving? ] } 1&& ;
71 : can-serve-file? ( path -- ? )
74 [ file-info regular-file? ]
78 : ftp-error ( string -- ) 500 server-response ;
80 : ftp-unimplemented ( string -- ) 502 server-response ;
83 "Welcome to " host-name append 220 server-response ;
85 : anonymous-only ( -- )
86 "This FTP server is anonymous only." 530 server-response ;
88 : handle-QUIT ( obj -- )
89 drop "Goodbye." 221 server-response ;
91 : handle-USER ( ftp-command -- )
93 tokenized>> second client get user<<
94 "Please specify the password." 331 server-response
96 2drop "bad USER" ftp-error
99 : handle-PASS ( ftp-command -- )
101 tokenized>> second client get password<<
102 "Login successful" 230 server-response
104 2drop "PASS error" ftp-error
107 ERROR: type-error type ;
109 : parse-type ( string -- string' )
111 { "IMAGE" [ "Binary" ] }
116 : handle-TYPE ( obj -- )
118 tokenized>> second parse-type
119 "Switching to " " mode" surround 200 server-response
121 2drop "TYPE is binary only" ftp-error
124 : random-local-server ( -- server )
125 remote-address get class-of new binary <server> ;
127 : port>bytes ( port -- hi lo )
128 [ -8 shift ] keep [ 8 bits ] bi@ ;
130 : display-directory ( -- string )
131 current-directory get server get serving-directory>> swap ?head drop
134 : handle-PWD ( obj -- )
136 display-directory "\"" dup surround 257 server-response ;
138 : handle-SYST ( obj -- )
140 "UNIX Type: L8" 215 server-response ;
142 : start-directory ( -- )
143 "Here comes the directory listing." 150 server-response ;
145 : transfer-outgoing-file ( path -- )
146 [ "Opening BINARY mode data connection for " ] dip
148 file-info size>> number>string
149 "(" " bytes)." surround
150 ] bi " " glue append 150 server-response ;
152 : transfer-incoming-file ( path -- )
153 "Opening BINARY mode data connection for " prepend
154 150 server-response ;
156 : finish-file-transfer ( -- )
157 "File send OK." 226 server-response ;
159 GENERIC: handle-passive-command ( stream obj -- )
161 : passive-loop ( server -- )
165 30 seconds over set-timeout
167 client get extra-connection>>
168 30 seconds ?promise-timeout
169 handle-passive-command
171 [ client get f >>extra-connection drop ]
175 : finish-directory ( -- )
176 "Directory send OK." 226 server-response ;
178 M: ftp-list handle-passive-command
183 ] with-string-writer split-lines
184 harvest [ ftp-send ] each
185 ] with-output-stream finish-directory ;
187 M: ftp-get handle-passive-command
190 [ transfer-outgoing-file ]
191 [ binary <file-reader> swap stream-copy ] bi
194 3drop "File transfer failed" ftp-error
197 M: ftp-put handle-passive-command
200 [ transfer-incoming-file ]
201 [ binary <file-writer> stream-copy ] bi
204 3drop "File transfer failed" ftp-error
207 M: ftp-disconnect handle-passive-command
210 : fulfill-client ( obj -- )
211 client get extra-connection>> [
215 "Establish an active or passive connection first" ftp-error
218 : handle-STOR ( obj -- )
220 dup can-serve-file? [
221 <ftp-put> fulfill-client
224 <ftp-disconnect> fulfill-client
227 : handle-LIST ( obj -- )
228 drop current-directory get
229 can-serve-directory? [
230 <ftp-list> fulfill-client
232 <ftp-disconnect> fulfill-client
235 : not-a-plain-file ( path -- )
236 ": not a plain file." append ftp-error ;
238 : handle-RETR ( obj -- )
241 dup can-serve-file? [
242 <ftp-get> fulfill-client
245 <ftp-disconnect> fulfill-client
248 : handle-SIZE ( obj -- )
250 dup can-serve-file? [
251 file-info size>> number>string 213 server-response
256 : expect-connection ( -- port )
257 <promise> client get extra-connection<<
259 [ [ passive-loop ] curry in-thread ]
260 [ addr>> port>> ] bi ;
262 : handle-PASV ( obj -- )
264 expect-connection port>bytes [ number>string ] bi@ "," glue
265 "Entering Passive Mode (127,0,0,1," ")" surround
266 221 server-response ;
268 : handle-EPSV ( obj -- )
270 client get f >>extra-connection drop
271 expect-connection number>string
272 "Entering Extended Passive Mode (|||" "|)" surround
273 229 server-response ;
275 : handle-MDTM ( obj -- )
276 tokenized>> ?second [
278 dup file-info dup directory? [
279 drop not-a-plain-file
282 modified>> timestamp>mdtm
289 : directory-change-success ( -- )
290 "Directory successully changed." 250 server-response ;
292 : directory-change-failed ( -- )
293 "Failed to change directory." 553 server-response ;
295 : handle-CWD ( obj -- )
296 tokenized>> ?second [
298 dup can-serve-directory? [
299 set-current-directory
300 directory-change-success
303 directory-change-failed
306 directory-change-success
309 : unrecognized-command ( obj -- )
310 raw>> "Unrecognized command: " prepend ftp-error ;
312 : client-loop-dispatch ( str/f -- ? )
313 dup tokenized>> first >upper {
314 { "QUIT" [ handle-QUIT f ] }
315 { "USER" [ handle-USER t ] }
316 { "PASS" [ handle-PASS t ] }
317 { "SYST" [ handle-SYST t ] }
318 { "ACCT" [ drop "ACCT unimplemented" ftp-unimplemented t ] }
319 { "PWD" [ handle-PWD t ] }
320 { "TYPE" [ handle-TYPE t ] }
321 { "CWD" [ handle-CWD t ] }
322 { "PASV" [ handle-PASV t ] }
323 { "EPSV" [ handle-EPSV t ] }
324 { "LIST" [ handle-LIST t ] }
325 { "STOR" [ handle-STOR t ] }
326 { "RETR" [ handle-RETR t ] }
327 { "SIZE" [ handle-SIZE t ] }
328 { "MDTM" [ handle-MDTM t ] }
329 [ drop unrecognized-command t ]
332 : read-command ( -- ftp-command/f )
333 readln [ f ] [ <ftp-command> ] if-empty ;
335 : handle-client-loop ( -- )
338 [ handle-client-loop ] when
341 : serve-directory ( server -- )
342 serving-directory>> [
347 M: ftp-server handle-client*
349 "New client" \ handle-client* DEBUG log-message
350 ftp-client new client set
351 [ server set ] [ serve-directory ] bi
354 : <ftp-server> ( directory port -- server )
355 latin1 ftp-server new-threaded-server
357 swap resolve-symlinks >>serving-directory
359 5 minutes >>timeout ;
361 : ftpd ( directory port -- server )
362 <ftp-server> start-server ;
364 ! sudo tcpdump -i en1 -A -s 10000 tcp port 21
365 ! [2010-09-04T22:07:58-05:00] DEBUG server-response: 500:Unrecognized command: EPRT |2|0:0:0:0:0:0:0:1|59359|