Added RETR command to FTP daemon (net branch)

Also fixed some bugs and added CDUP (untested)

git-svn-id: svn://kolibrios.org@2571 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
hidnplayr 2012-04-05 13:00:39 +00:00
parent 729f5e776a
commit 843b584cd7
2 changed files with 124 additions and 36 deletions

View File

@ -11,7 +11,6 @@ parse_cmd: ; esi must point to command
mov eax, [esi] mov eax, [esi]
and eax, not 0x20202020 ; convert to upper case and eax, not 0x20202020 ; convert to upper case
; (also convert spaces to null)
mov edi, commands ; list of commands to scan mov edi, commands ; list of commands to scan
.scanloop: .scanloop:
cmp eax, [edi] cmp eax, [edi]
@ -35,6 +34,8 @@ commands: ; all commands must be in uppercase
db 'ABOR' db 'ABOR'
dd cmdABOR dd cmdABOR
db 'CDUP'
dd cmdCDUP
db 'CWD', 0 db 'CWD', 0
dd cmdCWD dd cmdCWD
db 'DELE' db 'DELE'
@ -75,12 +76,37 @@ cmdABOR:
ret ret
align 4
cmdCDUP:
cmp byte [work_dir+1], 0
je .done
mov ecx, 1024
xor al, al
mov edi, work_dir+1024
repne scasb
std
dec edi
mov al,'/'
scasb
cld
mov byte[edi], 0
.done:
mcall send, [socketnum2], str250, str250.length, 0 ; command successful
ret
align 4 align 4
cmdCWD: ; Change Working Directory cmdCWD: ; Change Working Directory
sub ecx, 4 sub ecx, 4
jb .err jb .err
add esi, 4 add esi, 4
.scan:
mov edi, work_dir + 1 mov edi, work_dir + 1
cmp byte [esi], '/' cmp byte [esi], '/'
@ -94,6 +120,9 @@ cmdCWD: ; Change Working Directory
lodsb lodsb
cmp al, 0x20 cmp al, 0x20
jb .done jb .done
cmp al, '.'
je .up
.continue:
stosb stosb
loop .loop loop .loop
.done: .done:
@ -108,6 +137,14 @@ cmdCWD: ; Change Working Directory
ret ret
.up:
lodsb
cmp al, '.'
jne .continue
call cmdCDUP
jmp .scan
.err: .err:
ret ret
@ -125,17 +162,20 @@ cmdLIST:
jne @f jne @f
mcall connect, [datasocketnum], datasock, datasock.length mcall connect, [datasocketnum], datasock, datasock.length
cmp eax, -1 cmp eax, -1
je .err je socketerror
mov [datasocketnum], eax
@@: @@:
; Warn the client we're about to send the data
mcall send, [socketnum2], str150, str150.length, 0 ; here it comes..
; Create fpath from home_dir and work_dir ; Create fpath from home_dir and work_dir
call create_path call create_path
push fpath
call [con_write_asciiz]
push str_newline
call [con_write_asciiz]
; Start the search ; Start the search
push FA_READONLY + FA_FOLDER push FA_ANY
push str_mask push str_mask
push fpath push fpath
call [file.find.first] call [file.find.first]
@ -220,7 +260,7 @@ cmdLIST:
; insert a cr lf ; insert a cr lf
.namedone: .namedone:
mov ax, 0x0d0a mov ax, 0x0a0d
stosw stosw
; check next file ; check next file
@ -237,9 +277,8 @@ cmdLIST:
xor al, al xor al, al
stosb stosb
; print everything on the console ; Warn the client we're about to send the data
push buffer mcall send, [socketnum2], str150, str150.length, 0 ; here it comes..
call [con_write_asciiz]
; and send it to the client ; and send it to the client
lea esi, [edi - buffer] lea esi, [edi - buffer]
@ -258,17 +297,6 @@ cmdLIST:
ret ret
.err:
pushd 0x0c
call [con_set_flags]
push str_err1
call [con_write_asciiz]
pushd 0x07
call [con_set_flags]
ret
align 4 align 4
cmdNLST: cmdNLST:
@ -327,7 +355,7 @@ cmdPASV:
stosd stosd
mov al, ')' mov al, ')'
stosb stosb
mov ax, 0x0d0a mov ax, 0x0a0d
stosw stosw
xor al, al xor al, al
stosb stosb
@ -426,30 +454,57 @@ cmdQUIT:
align 4 align 4
cmdRETR: cmdRETR:
sub ecx, 5
jb .cannot_open
cmp [mode], MODE_ACTIVE cmp [mode], MODE_ACTIVE
jne @f jne @f
push esi
mcall connect, [datasocketnum], datasock, datasock.length mcall connect, [datasocketnum], datasock, datasock.length
; cmp eax, -1 pop esi
; je .err cmp eax, -1
je socketerror
mov [datasocketnum], eax
@@: @@:
mcall send, [socketnum2], str150, str150.length, 0 ; here it comes.. push esi
call create_path
pop esi
dec edi
add esi, 5
mov ecx, 1024
.loop:
lodsb
cmp al, 0x20
jl .done
stosb
loop .loop
.done:
xor al, al
stosb
push fpath
call [con_write_asciiz]
push str_newline
call [con_write_asciiz]
push O_READ push O_READ
push home_dir push fpath
call [file.open] call [file.open]
; test eax, eax test eax, eax
; jz .cannot_open jz .cannot_open
mov ebx, eax push eax
mcall send, [socketnum2], str150, str150.length, 0 ; here it comes..
pop ebx
.read_more: .read_more:
push BUFFERSIZE push BUFFERSIZE
push buffer push buffer
push ebx push ebx
call [file.read] call [file.read]
; cmp eax, -1 cmp eax, -1
; je .cannot_open je .cannot_open ; fixme: this is not the correct error
push eax push eax
push ebx push ebx
@ -457,8 +512,8 @@ cmdRETR:
mcall send, [datasocketnum], buffer, , 0 mcall send, [datasocketnum], buffer, , 0
pop ebx pop ebx
pop ecx pop ecx
; cmp eax, -1 cmp eax, -1
; je .socketerr je socketerror
cmp ecx, BUFFERSIZE cmp ecx, BUFFERSIZE
je .read_more je .read_more
@ -474,6 +529,21 @@ cmdRETR:
ret ret
.cannot_open:
pushd 0x0c
call [con_set_flags]
push str_notfound
call [con_write_asciiz]
pushd 0x07
call [con_set_flags]
mcall send, [socketnum2], str550, str550.length, 0 ; file not found
ret
align 4 align 4
cmdSTOR: cmdSTOR:
@ -649,6 +719,21 @@ create_path: ; combine home_dir and work_dir strings into fpath
ret ret
align 4
socketerror:
pushd 0x0c
call [con_set_flags]
push str_sockerr
call [con_write_asciiz]
pushd 0x07
call [con_set_flags]
mcall send, [socketnum2], str425, str425.length, 0 ; data connection error
ret

View File

@ -6,7 +6,7 @@
; GPLv2 ; GPLv2
; ;
BUFFERSIZE = 4096 BUFFERSIZE = 8192
STATE_DISCONNECTED = 0 STATE_DISCONNECTED = 0
STATE_CONNECTED = 1 STATE_CONNECTED = 1
@ -228,9 +228,12 @@ str8 db 'Error accepting connection',10,10,0
str_logged_in db 'Login ok',10,10,0 str_logged_in db 'Login ok',10,10,0
str_pass_ok db 'Password ok - Logged in',10,10,0 str_pass_ok db 'Password ok - Logged in',10,10,0
str_pwd db 'Current directory is "%s"\n',0 str_pwd db 'Current directory is "%s"\n',0
str_err1 db 'ERROR: cannot connect to remote socket',10,10,0
str_err2 db 'ERROR: cannot open directory',10,10,0 str_err2 db 'ERROR: cannot open directory',10,10,0
str_datasock db 'Passive data socket connected!',10,10,0 str_datasock db 'Passive data socket connected!',10,10,0
str_notfound db 'ERROR: file not found',10,10,0
str_sockerr db 'ERROR: socket error',10,10,0
str_newline db 10,0
str_mask db '*', 0 str_mask db '*', 0
@ -298,7 +301,7 @@ state dd ?
home_dir db '/rd/1/', 0 home_dir db '/rd/1/', 0
rb 1024 rb 1024
work_dir rb 1024 work_dir rb 1024
fpath rb 2048 fpath rb 1024*3
type db ? type db ?
mode db ? ; active/passive mode db ? ; active/passive