factor/extra/ftp/server/server.factor

319 lines
7.9 KiB
Factor
Raw Normal View History

2008-05-14 08:54:13 -04:00
! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
2008-05-12 20:31:56 -04:00
USING: accessors combinators io io.encodings.8-bit
io.encodings io.encodings.binary io.encodings.utf8 io.files
io.server io.sockets kernel math.parser namespaces sequences
ftp io.unix.launcher.parser unicode.case splitting assocs
classes io.server destructors calendar io.timeouts
2008-05-19 22:52:16 -04:00
io.streams.duplex threads continuations math
concurrency.promises byte-arrays ;
2008-05-12 20:31:56 -04:00
IN: ftp.server
2008-05-13 17:26:11 -04:00
SYMBOL: client
2008-05-12 20:31:56 -04:00
2008-05-14 08:54:13 -04:00
TUPLE: ftp-command raw tokenized ;
2008-05-13 17:26:11 -04:00
2008-05-14 08:54:13 -04:00
: <ftp-command> ( -- obj )
ftp-command new ;
2008-05-12 20:31:56 -04:00
TUPLE: ftp-get path ;
: <ftp-get> ( path -- obj )
ftp-get new swap >>path ;
TUPLE: ftp-put path ;
: <ftp-put> ( path -- obj )
ftp-put new swap >>path ;
TUPLE: ftp-list ;
C: <ftp-list> ftp-list
2008-05-14 08:54:13 -04:00
: read-command ( -- ftp-command )
<ftp-command> readln
[ >>raw ] [ tokenize-command >>tokenized ] bi ;
: (send-response) ( n string separator -- )
rot number>string write write ftp-send ;
2008-05-12 20:31:56 -04:00
2008-05-13 17:26:11 -04:00
: send-response ( ftp-response -- )
[ n>> ] [ strings>> ] bi
2008-05-14 08:54:13 -04:00
[ but-last-slice [ "-" (send-response) ] with each ]
[ first " " (send-response) ] 2bi ;
2008-05-13 17:26:11 -04:00
: server-response ( n string -- )
<ftp-response>
swap add-response-line
swap >>n
send-response ;
: ftp-error ( string -- )
500 "Unrecognized command: " rot append server-response ;
2008-05-13 17:26:11 -04:00
: send-banner ( -- )
220 "Welcome to " host-name append server-response ;
2008-05-14 08:54:13 -04:00
: anonymous-only ( -- )
530 "This FTP server is anonymous only." server-response ;
: handle-QUIT ( obj -- )
2008-05-13 17:26:11 -04:00
drop 221 "Goodbye." server-response ;
: handle-USER ( ftp-command -- )
[
tokenized>> second client get swap >>user drop
331 "Please specify the password." server-response
] [
2drop "bad USER" ftp-error
] recover ;
: handle-PASS ( ftp-command -- )
[
tokenized>> second client get swap >>password drop
230 "Login successful" server-response
] [
2drop "PASS error" ftp-error
] recover ;
ERROR: type-error type ;
2008-05-19 22:52:16 -04:00
: parse-type ( string -- string' )
>upper {
{ "IMAGE" [ "Binary" ] }
{ "I" [ "Binary" ] }
[ type-error ]
} case ;
: handle-TYPE ( obj -- )
[
2008-05-19 22:52:16 -04:00
tokenized>> second parse-type
200 "Switching to " rot " mode" 3append server-response
] [
2drop "TYPE is binary only" ftp-error
] recover ;
2008-05-19 22:52:16 -04:00
: random-local-server ( -- server )
remote-address get class new 0 >>port binary <server> ;
: port>bytes ( port -- hi lo )
[ -8 shift ] keep [ HEX: ff bitand ] bi@ ;
: handle-PWD ( obj -- )
drop
2008-05-14 08:54:13 -04:00
257 current-directory get "\"" swap "\"" 3append server-response ;
2008-05-19 22:52:16 -04:00
: handle-SYST ( obj -- )
drop
215 "UNIX Type: L8" server-response ;
2008-05-14 08:54:13 -04:00
: handle-STOR ( obj -- )
[
drop
] [
2drop
] recover ;
2008-05-14 08:54:13 -04:00
! EPRT |2|::1|62138|
! : handle-EPRT ( obj -- )
! tokenized>> second "|" split harvest ;
: start-directory ( -- )
150 "Here comes the directory listing." server-response ;
: finish-directory ( -- )
226 "Directory send OK." server-response ;
GENERIC: service-command ( stream obj -- )
M: ftp-list service-command ( stream obj -- )
drop
start-directory
[
utf8 encode-output
directory-list [ ftp-send ] each
] with-output-stream
finish-directory ;
: start-file-transfer ( path -- )
150 "Opening BINARY mode data connection for "
rot
[ file-name ] [
" " swap file-info file-info-size number>string
"(" " bytes)." swapd 3append append
] bi 3append server-response ;
: finish-file-transfer ( -- )
226 "File send OK." server-response ;
M: ftp-get service-command ( stream obj -- )
[
path>>
[ start-file-transfer ]
[ binary <file-reader> swap stream-copy ] bi
finish-file-transfer
] [
3drop "File transfer failed" ftp-error
] recover ;
M: ftp-put service-command ( stream obj -- )
[
path>>
[ start-file-transfer ]
[ binary <file-reader> swap stream-copy ] bi
finish-file-transfer
] [
3drop "File transfer failed" ftp-error
] recover ;
2008-05-19 22:52:16 -04:00
: passive-loop ( server -- )
[
[
|dispose
30 seconds over set-timeout
accept drop &dispose
client get command-promise>>
30 seconds ?promise-timeout
service-command
]
[ client get f >>command-promise drop ]
[ ] cleanup
] with-destructors ;
: if-command-promise ( quot -- )
>r client get command-promise>> r>
[ "Establish an active or passive connection first" ftp-error ] if* ;
: handle-LIST ( obj -- )
drop
[ <ftp-list> swap fulfill ] if-command-promise ;
: handle-SIZE ( obj -- )
[
tokenized>> second file-info size>>
213 swap number>string server-response
] [
2drop
550 "Could not get file size" server-response
] recover ;
: handle-RETR ( obj -- )
[ tokenized>> second <ftp-get> swap fulfill ]
curry if-command-promise ;
2008-05-19 22:52:16 -04:00
: expect-connection ( -- port )
random-local-server
client get <promise> >>command-promise drop
[ [ passive-loop ] curry in-thread ]
[ addr>> port>> ] bi ;
: handle-PASV ( obj -- )
drop client get passive >>mode drop
expect-connection
[
"Entering Passive Mode (127,0,0,1," %
port>bytes [ number>string ] bi@ "," swap 3append %
")" %
] "" make 227 swap server-response ;
: handle-EPSV ( obj -- )
drop
client get command-promise>> [
"You already have a passive stream" ftp-error
] [
229 "Entering Extended Passive Mode (|||"
2008-05-19 22:52:16 -04:00
expect-connection number>string
"|)" 3append server-response
] if ;
! LPRT 6,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,242,186
! : handle-LPRT ( obj -- ) tokenized>> "," split ;
ERROR: not-a-directory ;
: handle-CWD ( obj -- )
[
tokenized>> second dup directory? [
set-current-directory
250 "Directory successully changed." server-response
] [
not-a-directory throw
] if
] [
2drop
550 "Failed to change directory." server-response
] recover ;
2008-05-14 08:54:13 -04:00
: unrecognized-command ( obj -- ) raw>> ftp-error ;
2008-05-13 17:26:11 -04:00
: handle-client-loop ( -- )
2008-05-14 08:54:13 -04:00
<ftp-command> readln
[ >>raw ]
2008-05-13 17:26:11 -04:00
[ tokenize-command >>tokenized ] bi
dup tokenized>> first >upper {
{ "USER" [ handle-USER t ] }
{ "PASS" [ handle-PASS t ] }
2008-05-14 08:54:13 -04:00
{ "ACCT" [ drop "ACCT unimplemented" ftp-error t ] }
{ "CWD" [ handle-CWD t ] }
! { "XCWD" [ ] }
2008-05-12 20:31:56 -04:00
! { "CDUP" [ ] }
! { "SMNT" [ ] }
2008-05-14 08:54:13 -04:00
! { "REIN" [ drop client get reset-ftp-client t ] }
{ "QUIT" [ handle-QUIT f ] }
2008-05-12 20:31:56 -04:00
! { "PORT" [ ] }
2008-05-19 22:52:16 -04:00
{ "PASV" [ handle-PASV t ] }
2008-05-12 20:31:56 -04:00
! { "MODE" [ ] }
{ "TYPE" [ handle-TYPE t ] }
2008-05-12 20:31:56 -04:00
! { "STRU" [ ] }
! { "ALLO" [ ] }
! { "REST" [ ] }
2008-05-14 08:54:13 -04:00
! { "STOR" [ handle-STOR t ] }
2008-05-12 20:31:56 -04:00
! { "STOU" [ ] }
{ "RETR" [ handle-RETR t ] }
{ "LIST" [ handle-LIST t ] }
{ "SIZE" [ handle-SIZE t ] }
2008-05-12 20:31:56 -04:00
! { "NLST" [ ] }
! { "APPE" [ ] }
! { "RNFR" [ ] }
! { "RNTO" [ ] }
! { "DELE" [ ] }
! { "RMD" [ ] }
! { "MKD" [ ] }
{ "PWD" [ handle-PWD t ] }
2008-05-12 20:31:56 -04:00
! { "ABOR" [ ] }
2008-05-19 22:52:16 -04:00
{ "SYST" [ handle-SYST t ] }
2008-05-12 20:31:56 -04:00
! { "STAT" [ ] }
! { "HELP" [ ] }
! { "SITE" [ ] }
! { "NOOP" [ ] }
! { "EPRT" [ handle-EPRT ] }
! { "LPRT" [ handle-LPRT ] }
{ "EPSV" [ handle-EPSV t ] }
! { "LPSV" [ drop handle-LPSV t ] }
2008-05-14 08:54:13 -04:00
[ drop unrecognized-command t ]
2008-05-13 17:26:11 -04:00
} case [ handle-client-loop ] when ;
: handle-client ( -- )
[
"" [
host-name <ftp-client> client set
send-banner handle-client-loop
] with-directory
] with-destructors ;
2008-05-12 20:31:56 -04:00
: ftpd ( port -- )
internet-server "ftp.server"
latin1 [ handle-client ] with-server ;
2008-05-13 17:26:11 -04:00
: ftpd-main ( -- ) 2100 ftpd ;
2008-05-12 20:31:56 -04:00
MAIN: ftpd-main
2008-05-13 17:26:11 -04:00
! sudo tcpdump -i en1 -A -s 10000 tcp port 21