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.8-bit.latin1 io.encodings.binary io.encodings.utf8
7 io.files io.files.info io.files.types io.pathnames
8 io.servers io.sockets io.streams.string io.timeouts
9 kernel logging math math.bitwise math.parser namespaces
10 sequences simple-tokenizer splitting strings threads
11 tools.files unicode.case ;
17 TUPLE: ftp-server < threaded-server { serving-directory string } ;
19 TUPLE: ftp-client user password extra-connection ;
21 TUPLE: ftp-command raw tokenized ;
22 : <ftp-command> ( str -- obj )
23 dup \ <ftp-command> DEBUG log-message
26 swap tokenize >>tokenized ;
35 C: <ftp-list> ftp-list
37 TUPLE: ftp-disconnect ;
38 C: <ftp-disconnect> ftp-disconnect
40 : (send-response) ( n string separator -- )
41 [ number>string write ] 2dip write ftp-send ;
43 : send-response ( ftp-response -- )
44 [ n>> ] [ strings>> ] bi
45 [ but-last-slice [ "-" (send-response) ] with each ]
46 [ first " " (send-response) ] 2bi ;
48 : make-path-relative? ( path -- ? )
51 [ drop server get serving-directory>> ]
54 : fixup-relative-path ( string -- string' )
55 dup make-path-relative? [
56 [ server get serving-directory>> ] dip append-relative-path
59 : server-response ( string n -- )
60 2dup number>string swap ":" glue \ server-response DEBUG log-message
63 swap add-response-line
66 : serving? ( path -- ? )
67 resolve-symlinks server get serving-directory>> head? ;
69 : can-serve-directory? ( path -- ? )
70 { [ exists? ] [ file-info directory? ] [ serving? ] } 1&& ;
72 : can-serve-file? ( path -- ? )
75 [ file-info type>> +regular-file+ = ]
79 : ftp-error ( string -- ) 500 server-response ;
81 : ftp-unimplemented ( string -- ) 502 server-response ;
84 "Welcome to " host-name append 220 server-response ;
86 : anonymous-only ( -- )
87 "This FTP server is anonymous only." 530 server-response ;
89 : handle-QUIT ( obj -- )
90 drop "Goodbye." 221 server-response ;
92 : handle-USER ( ftp-command -- )
94 tokenized>> second client get user<<
95 "Please specify the password." 331 server-response
97 2drop "bad USER" ftp-error
100 : handle-PASS ( ftp-command -- )
102 tokenized>> second client get password<<
103 "Login successful" 230 server-response
105 2drop "PASS error" ftp-error
108 ERROR: type-error type ;
110 : parse-type ( string -- string' )
112 { "IMAGE" [ "Binary" ] }
117 : handle-TYPE ( obj -- )
119 tokenized>> second parse-type
120 "Switching to " " mode" surround 200 server-response
122 2drop "TYPE is binary only" ftp-error
125 : random-local-server ( -- server )
126 remote-address get class-of new binary <server> ;
128 : port>bytes ( port -- hi lo )
129 [ -8 shift ] keep [ 8 bits ] bi@ ;
131 : display-directory ( -- string )
132 current-directory get server get serving-directory>> swap ?head drop
135 : handle-PWD ( obj -- )
137 display-directory "\"" dup surround 257 server-response ;
139 : handle-SYST ( obj -- )
141 "UNIX Type: L8" 215 server-response ;
143 : start-directory ( -- )
144 "Here comes the directory listing." 150 server-response ;
146 : transfer-outgoing-file ( path -- )
147 [ "Opening BINARY mode data connection for " ] dip
149 file-info size>> number>string
150 "(" " bytes)." surround
151 ] bi " " glue append 150 server-response ;
153 : transfer-incoming-file ( path -- )
154 "Opening BINARY mode data connection for " prepend
155 150 server-response ;
157 : finish-file-transfer ( -- )
158 "File send OK." 226 server-response ;
160 GENERIC: handle-passive-command ( stream obj -- )
162 : passive-loop ( server -- )
166 30 seconds over set-timeout
168 client get extra-connection>>
169 30 seconds ?promise-timeout
170 handle-passive-command
172 [ client get f >>extra-connection drop ]
176 : finish-directory ( -- )
177 "Directory send OK." 226 server-response ;
179 M: ftp-list handle-passive-command ( stream obj -- )
183 current-directory get directory.
184 ] with-string-writer string-lines
185 harvest [ ftp-send ] each
186 ] with-output-stream finish-directory ;
188 M: ftp-get handle-passive-command ( stream obj -- )
191 [ transfer-outgoing-file ]
192 [ binary <file-reader> swap stream-copy ] bi
195 3drop "File transfer failed" ftp-error
198 M: ftp-put handle-passive-command ( stream obj -- )
201 [ transfer-incoming-file ]
202 [ binary <file-writer> stream-copy ] bi
205 3drop "File transfer failed" ftp-error
208 M: ftp-disconnect handle-passive-command ( stream obj -- )
211 : fulfill-client ( obj -- )
212 client get extra-connection>> [
216 "Establish an active or passive connection first" ftp-error
219 : handle-STOR ( obj -- )
221 dup can-serve-file? [
222 <ftp-put> fulfill-client
225 <ftp-disconnect> fulfill-client
228 : handle-LIST ( obj -- )
229 drop current-directory get
230 can-serve-directory? [
231 <ftp-list> fulfill-client
233 <ftp-disconnect> fulfill-client
236 : not-a-plain-file ( path -- )
237 ": not a plain file." append ftp-error ;
239 : handle-RETR ( obj -- )
242 dup can-serve-file? [
243 <ftp-get> fulfill-client
246 <ftp-disconnect> fulfill-client
249 : handle-SIZE ( obj -- )
251 dup can-serve-file? [
252 file-info size>> number>string 213 server-response
257 : expect-connection ( -- port )
258 <promise> client get extra-connection<<
260 [ [ passive-loop ] curry in-thread ]
261 [ addr>> port>> ] bi ;
263 : handle-PASV ( obj -- )
265 expect-connection port>bytes [ number>string ] bi@ "," glue
266 "Entering Passive Mode (127,0,0,1," ")" surround
267 221 server-response ;
269 : handle-EPSV ( obj -- )
271 client get f >>extra-connection drop
272 expect-connection number>string
273 "Entering Extended Passive Mode (|||" "|)" surround
274 229 server-response ;
276 : handle-MDTM ( obj -- )
277 tokenized>> ?second [
279 dup file-info dup directory? [
280 drop not-a-plain-file
283 modified>> timestamp>mdtm
290 ERROR: not-a-directory ;
291 ERROR: no-directory-permissions ;
293 : directory-change-success ( -- )
294 "Directory successully changed." 250 server-response ;
296 : directory-change-failed ( -- )
297 "Failed to change directory." 553 server-response ;
299 : handle-CWD ( obj -- )
300 tokenized>> ?second [
302 dup can-serve-directory? [
303 set-current-directory
304 directory-change-success
307 directory-change-failed
310 directory-change-success
313 : unrecognized-command ( obj -- )
314 raw>> "Unrecognized command: " prepend ftp-error ;
316 : client-loop-dispatch ( str/f -- ? )
317 dup tokenized>> first >upper {
318 { "QUIT" [ handle-QUIT f ] }
319 { "USER" [ handle-USER t ] }
320 { "PASS" [ handle-PASS t ] }
321 { "SYST" [ handle-SYST t ] }
322 { "ACCT" [ drop "ACCT unimplemented" ftp-unimplemented t ] }
323 { "PWD" [ handle-PWD t ] }
324 { "TYPE" [ handle-TYPE t ] }
325 { "CWD" [ handle-CWD t ] }
326 { "PASV" [ handle-PASV t ] }
327 { "EPSV" [ handle-EPSV t ] }
328 { "LIST" [ handle-LIST t ] }
329 { "STOR" [ handle-STOR t ] }
330 { "RETR" [ handle-RETR t ] }
331 { "SIZE" [ handle-SIZE t ] }
332 { "MDTM" [ handle-MDTM t ] }
333 [ drop unrecognized-command t ]
336 : read-command ( -- ftp-command/f )
337 readln [ f ] [ <ftp-command> ] if-empty ;
339 : handle-client-loop ( -- )
342 [ handle-client-loop ] when
345 : serve-directory ( server -- )
346 serving-directory>> [
351 M: ftp-server handle-client* ( server -- )
353 "New client" \ handle-client* DEBUG log-message
354 ftp-client new client set
355 [ server set ] [ serve-directory ] bi
358 : <ftp-server> ( directory port -- server )
359 latin1 ftp-server new-threaded-server
361 swap resolve-symlinks >>serving-directory
363 5 minutes >>timeout ;
365 : ftpd ( directory port -- server )
366 <ftp-server> start-server ;
368 ! sudo tcpdump -i en1 -A -s 10000 tcp port 21
369 ! [2010-09-04T22:07:58-05:00] DEBUG server-response: 500:Unrecognized command: EPRT |2|0:0:0:0:0:0:0:1|59359|