1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators.short-circuit accessors combinators io
4 io.encodings.8-bit io.encodings io.encodings.binary
5 io.encodings.utf8 io.files io.sockets kernel math.parser
6 namespaces make sequences ftp io.unix.launcher.parser
7 unicode.case splitting assocs classes io.servers.connection
8 destructors calendar io.timeouts io.streams.duplex threads
9 continuations math concurrency.promises byte-arrays ;
14 TUPLE: ftp-command raw tokenized ;
16 : <ftp-command> ( -- obj )
21 : <ftp-get> ( path -- obj )
22 ftp-get new swap >>path ;
26 : <ftp-put> ( path -- obj )
27 ftp-put new swap >>path ;
31 C: <ftp-list> ftp-list
33 : read-command ( -- ftp-command )
35 [ >>raw ] [ tokenize-command >>tokenized ] bi ;
37 : (send-response) ( n string separator -- )
38 rot number>string write write ftp-send ;
40 : send-response ( ftp-response -- )
41 [ n>> ] [ strings>> ] bi
42 [ but-last-slice [ "-" (send-response) ] with each ]
43 [ first " " (send-response) ] 2bi ;
45 : server-response ( n string -- )
47 swap add-response-line
51 : ftp-error ( string -- )
52 500 "Unrecognized command: " rot append server-response ;
55 220 "Welcome to " host-name append server-response ;
57 : anonymous-only ( -- )
58 530 "This FTP server is anonymous only." server-response ;
60 : handle-QUIT ( obj -- )
61 drop 221 "Goodbye." server-response ;
63 : handle-USER ( ftp-command -- )
65 tokenized>> second client get swap >>user drop
66 331 "Please specify the password." server-response
68 2drop "bad USER" ftp-error
71 : handle-PASS ( ftp-command -- )
73 tokenized>> second client get swap >>password drop
74 230 "Login successful" server-response
76 2drop "PASS error" ftp-error
79 ERROR: type-error type ;
81 : parse-type ( string -- string' )
83 { "IMAGE" [ "Binary" ] }
88 : handle-TYPE ( obj -- )
90 tokenized>> second parse-type
91 200 "Switching to " rot " mode" 3append server-response
93 2drop "TYPE is binary only" ftp-error
96 : random-local-server ( -- server )
97 remote-address get class new 0 >>port binary <server> ;
99 : port>bytes ( port -- hi lo )
100 [ -8 shift ] keep [ HEX: ff bitand ] bi@ ;
102 : handle-PWD ( obj -- )
104 257 current-directory get "\"" swap "\"" 3append server-response ;
106 : handle-SYST ( obj -- )
108 215 "UNIX Type: L8" server-response ;
110 : if-command-promise ( quot -- )
111 >r client get command-promise>> r>
112 [ "Establish an active or passive connection first" ftp-error ] if* ;
114 : handle-STOR ( obj -- )
117 [ >r <ftp-put> r> fulfill ] if-command-promise
123 ! : handle-EPRT ( obj -- )
124 ! tokenized>> second "|" split harvest ;
126 : start-directory ( -- )
127 150 "Here comes the directory listing." server-response ;
129 : finish-directory ( -- )
130 226 "Opening " server-response ;
132 GENERIC: service-command ( stream obj -- )
134 M: ftp-list service-command ( stream obj -- )
139 directory-list [ ftp-send ] each
143 : transfer-outgoing-file ( path -- )
144 150 "Opening BINARY mode data connection for "
147 " " swap file-info size>> number>string
148 "(" " bytes)." swapd 3append append
149 ] bi 3append server-response ;
151 : transfer-incoming-file ( path -- )
152 150 "Opening BINARY mode data connection for " rot append
155 : finish-file-transfer ( -- )
156 226 "File send OK." server-response ;
158 M: ftp-get service-command ( stream obj -- )
161 [ transfer-outgoing-file ]
162 [ binary <file-reader> swap stream-copy ] bi
165 3drop "File transfer failed" ftp-error
168 M: ftp-put service-command ( stream obj -- )
171 [ transfer-incoming-file ]
172 [ binary <file-writer> stream-copy ] bi
175 3drop "File transfer failed" ftp-error
178 : passive-loop ( server -- )
182 30 seconds over set-timeout
184 client get command-promise>>
185 30 seconds ?promise-timeout
188 [ client get f >>command-promise drop ]
192 : handle-LIST ( obj -- )
194 [ >r <ftp-list> r> fulfill ] if-command-promise ;
196 : handle-SIZE ( obj -- )
198 tokenized>> second file-info size>>
199 213 swap number>string server-response
202 550 "Could not get file size" server-response
205 : handle-RETR ( obj -- )
206 [ tokenized>> second <ftp-get> swap fulfill ]
207 curry if-command-promise ;
209 : expect-connection ( -- port )
211 client get <promise> >>command-promise drop
212 [ [ passive-loop ] curry in-thread ]
213 [ addr>> port>> ] bi ;
215 : handle-PASV ( obj -- )
216 drop client get passive >>mode drop
219 "Entering Passive Mode (127,0,0,1," %
220 port>bytes [ number>string ] bi@ "," swap 3append %
222 ] "" make 227 swap server-response ;
224 : handle-EPSV ( obj -- )
226 client get command-promise>> [
227 "You already have a passive stream" ftp-error
229 229 "Entering Extended Passive Mode (|||"
230 expect-connection number>string
231 "|)" 3append server-response
234 ! LPRT 6,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,242,186
235 ! : handle-LPRT ( obj -- ) tokenized>> "," split ;
237 ERROR: not-a-directory ;
239 : handle-CWD ( obj -- )
241 tokenized>> second dup directory? [
242 set-current-directory
243 250 "Directory successully changed." server-response
245 not-a-directory throw
249 550 "Failed to change directory." server-response
252 : unrecognized-command ( obj -- ) raw>> ftp-error ;
254 : handle-client-loop ( -- )
257 [ tokenize-command >>tokenized ] bi
258 dup tokenized>> first >upper {
259 { "USER" [ handle-USER t ] }
260 { "PASS" [ handle-PASS t ] }
261 { "ACCT" [ drop "ACCT unimplemented" ftp-error t ] }
262 { "CWD" [ handle-CWD t ] }
267 ! { "REIN" [ drop client get reset-ftp-client t ] }
268 { "QUIT" [ handle-QUIT f ] }
270 ! { "PORT" [ ] } ! TODO
271 { "PASV" [ handle-PASV t ] }
273 { "TYPE" [ handle-TYPE t ] }
278 { "STOR" [ handle-STOR t ] }
280 { "RETR" [ handle-RETR t ] }
281 { "LIST" [ handle-LIST t ] }
282 { "SIZE" [ handle-SIZE t ] }
287 ! { "DELE" [ handle-DELE t ] }
288 ! { "RMD" [ handle-RMD t ] }
289 ! ! { "XRMD" [ handle-XRMD t ] }
290 ! { "MKD" [ handle-MKD t ] }
291 { "PWD" [ handle-PWD t ] }
294 { "SYST" [ handle-SYST t ] }
301 ! { "EPRT" [ handle-EPRT ] }
302 ! { "LPRT" [ handle-LPRT ] }
303 { "EPSV" [ handle-EPSV t ] }
304 ! { "LPSV" [ drop handle-LPSV t ] }
305 [ drop unrecognized-command t ]
306 } case [ handle-client-loop ] when ;
308 TUPLE: ftp-server < threaded-server ;
310 M: ftp-server handle-client* ( server -- )
314 host-name <ftp-client> client set
315 send-banner handle-client-loop
319 : <ftp-server> ( port -- server )
320 ftp-server new-threaded-server
326 <ftp-server> start-server ;
328 : ftpd-main ( -- ) 2100 ftpd ;
332 ! sudo tcpdump -i en1 -A -s 10000 tcp port 21