2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
struct thread_data
|
|
|
|
rb 1024
|
|
|
|
stack rb 0
|
|
|
|
|
|
|
|
home_dir rb 1024
|
|
|
|
work_dir rb 1024
|
|
|
|
fpath rb 1024*3
|
|
|
|
|
|
|
|
type db ? ; ASCII/EBDIC/IMAGE/..
|
|
|
|
mode db ? ; active/passive
|
|
|
|
socketnum dd ? ; Commands socket
|
|
|
|
state dd ? ; disconnected/logging in/logged in/..
|
|
|
|
passivesocknum dd ? ; when in passive mode, this is the listening socket
|
|
|
|
datasocketnum dd ? ; socket used for data transfers
|
|
|
|
|
|
|
|
datasock sockaddr_in
|
|
|
|
|
|
|
|
buffer rb BUFFERSIZE
|
|
|
|
ends
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
align 4
|
|
|
|
parse_cmd: ; esi must point to command
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
cmp byte [esi], 0x20 ; skip all leading characters
|
|
|
|
ja .ok
|
|
|
|
inc esi
|
|
|
|
dec ecx
|
|
|
|
cmp ecx, 3
|
|
|
|
ja parse_cmd
|
|
|
|
ret
|
|
|
|
.ok:
|
|
|
|
|
2012-04-03 22:28:26 +02:00
|
|
|
cmp byte [esi+3], 0x20
|
|
|
|
jae @f
|
|
|
|
mov byte [esi+3], 0
|
|
|
|
@@:
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
mov eax, [esi]
|
|
|
|
and eax, not 0x20202020 ; convert to upper case
|
|
|
|
mov edi, commands ; list of commands to scan
|
|
|
|
.scanloop:
|
|
|
|
cmp eax, [edi]
|
2012-04-03 22:28:26 +02:00
|
|
|
jne .try_next
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
jmp dword [edi+4]
|
|
|
|
|
|
|
|
.try_next:
|
|
|
|
add edi, 8
|
|
|
|
cmp byte [edi], 0
|
|
|
|
jne .scanloop
|
|
|
|
|
|
|
|
.error:
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall send, [edx + thread_data.socketnum], str500, str500.length, 0
|
2012-04-03 22:28:26 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
2012-04-04 15:08:07 +02:00
|
|
|
commands: ; all commands must be in uppercase
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
db 'ABOR'
|
|
|
|
dd cmdABOR
|
2012-04-05 15:00:39 +02:00
|
|
|
db 'CDUP'
|
|
|
|
dd cmdCDUP
|
2012-04-03 18:37:24 +02:00
|
|
|
db 'CWD', 0
|
|
|
|
dd cmdCWD
|
|
|
|
db 'DELE'
|
|
|
|
dd cmdDELE
|
|
|
|
db 'LIST'
|
|
|
|
dd cmdLIST
|
|
|
|
db 'NLST'
|
|
|
|
dd cmdNLST
|
|
|
|
db 'NOOP'
|
|
|
|
dd cmdNOOP
|
2012-04-03 22:28:26 +02:00
|
|
|
db 'PASS'
|
|
|
|
dd cmdPASS
|
2012-04-04 15:08:07 +02:00
|
|
|
db 'PASV'
|
|
|
|
dd cmdPASV
|
2012-04-03 18:37:24 +02:00
|
|
|
db 'PORT'
|
|
|
|
dd cmdPORT
|
2012-04-07 20:42:58 +02:00
|
|
|
db 'PWD', 0
|
|
|
|
dd cmdPWD
|
2012-04-03 18:37:24 +02:00
|
|
|
db 'QUIT'
|
|
|
|
dd cmdQUIT
|
|
|
|
db 'RETR'
|
|
|
|
dd cmdRETR
|
|
|
|
db 'STOR'
|
|
|
|
dd cmdSTOR
|
|
|
|
db 'SYST'
|
|
|
|
dd cmdSYST
|
|
|
|
db 'TYPE'
|
|
|
|
dd cmdTYPE
|
|
|
|
db 'USER'
|
|
|
|
dd cmdUSER
|
2012-04-04 15:08:07 +02:00
|
|
|
db 0 ; end marker
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
align 4
|
|
|
|
cmdABOR:
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; TODO: abort the current filetransfer
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
ret
|
|
|
|
|
2012-04-05 15:00:39 +02:00
|
|
|
align 4
|
|
|
|
cmdCDUP:
|
|
|
|
|
2012-04-07 20:42:58 +02:00
|
|
|
cmp byte [edx + thread_data.work_dir+1], 0 ; are we in "/" ?
|
2012-04-05 15:00:39 +02:00
|
|
|
je .done
|
|
|
|
|
|
|
|
mov ecx, 1024
|
|
|
|
xor al, al
|
2012-04-07 20:42:58 +02:00
|
|
|
lea edi, [edx + thread_data.work_dir]
|
2012-04-05 15:00:39 +02:00
|
|
|
repne scasb
|
|
|
|
std
|
|
|
|
dec edi
|
2012-04-07 20:42:58 +02:00
|
|
|
dec edi
|
|
|
|
dec edi
|
2012-04-05 15:00:39 +02:00
|
|
|
mov al,'/'
|
2012-04-07 20:42:58 +02:00
|
|
|
repne scasb
|
2012-04-05 15:00:39 +02:00
|
|
|
cld
|
2012-04-07 20:42:58 +02:00
|
|
|
mov byte[edi+1], 0
|
2012-04-05 15:00:39 +02:00
|
|
|
|
|
|
|
.done:
|
2012-04-07 20:42:58 +02:00
|
|
|
; Print the new working dir on the console
|
|
|
|
lea eax, [edx + thread_data.work_dir]
|
|
|
|
push eax
|
|
|
|
call [con_write_asciiz]
|
|
|
|
push str_newline
|
|
|
|
call [con_write_asciiz]
|
|
|
|
|
|
|
|
mcall send, [edx + thread_data.socketnum], str250, str250.length, 0 ; command successful
|
2012-04-05 15:00:39 +02:00
|
|
|
ret
|
|
|
|
|
2012-04-07 20:42:58 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
align 4
|
2012-04-04 19:19:00 +02:00
|
|
|
cmdCWD: ; Change Working Directory
|
|
|
|
|
|
|
|
sub ecx, 4
|
|
|
|
jb .err
|
|
|
|
add esi, 4
|
2012-04-05 15:00:39 +02:00
|
|
|
|
|
|
|
.scan:
|
2012-04-06 20:37:00 +02:00
|
|
|
lea edi, [edx + thread_data.work_dir + 1]
|
|
|
|
push ecx
|
|
|
|
mov ecx, 1024
|
|
|
|
.find_zero:
|
|
|
|
cmp byte [edi], 0
|
|
|
|
je .found_zero
|
|
|
|
inc edi
|
|
|
|
loop .find_zero
|
|
|
|
.found_zero:
|
|
|
|
pop ecx
|
2012-04-07 13:36:00 +02:00
|
|
|
.scan2:
|
2012-04-04 19:19:00 +02:00
|
|
|
|
|
|
|
cmp byte [esi], '/'
|
|
|
|
jne @f
|
|
|
|
inc esi
|
|
|
|
dec ecx
|
|
|
|
jz .done
|
|
|
|
@@:
|
|
|
|
|
|
|
|
.loop:
|
|
|
|
lodsb
|
|
|
|
cmp al, 0x20
|
|
|
|
jb .done
|
2012-04-05 15:00:39 +02:00
|
|
|
cmp al, '.'
|
|
|
|
je .up
|
|
|
|
.continue:
|
2012-04-04 19:19:00 +02:00
|
|
|
stosb
|
|
|
|
loop .loop
|
|
|
|
.done:
|
|
|
|
cmp byte [edi-1], '/'
|
|
|
|
je @f
|
|
|
|
mov byte [edi], '/'
|
|
|
|
inc edi
|
|
|
|
@@:
|
|
|
|
mov byte [edi], 0
|
|
|
|
|
2012-04-07 20:42:58 +02:00
|
|
|
; Print the new working dir on the console
|
|
|
|
lea eax, [edx + thread_data.work_dir]
|
|
|
|
push eax
|
|
|
|
call [con_write_asciiz]
|
|
|
|
push str_newline
|
|
|
|
call [con_write_asciiz]
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall send, [edx + thread_data.socketnum], str250, str250.length, 0
|
2012-04-04 19:19:00 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2012-04-05 15:00:39 +02:00
|
|
|
.up:
|
|
|
|
lodsb
|
|
|
|
cmp al, '.'
|
|
|
|
jne .continue
|
|
|
|
|
2012-04-07 13:36:00 +02:00
|
|
|
;;;; TODO: find second last '\' in work_dir and make next char zero
|
|
|
|
;;;; point edi to that 0
|
|
|
|
|
|
|
|
jmp .scan2
|
2012-04-05 15:00:39 +02:00
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
.err:
|
2012-04-06 20:37:00 +02:00
|
|
|
; TODO: print correct error message (550?)
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
cmdDELE:
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
2012-04-07 20:42:58 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
align 4
|
|
|
|
cmdLIST:
|
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
; If we are in active mode, it's time to open a data socket..
|
2012-04-06 20:37:00 +02:00
|
|
|
cmp [edx + thread_data.mode], MODE_ACTIVE
|
2012-04-04 15:08:07 +02:00
|
|
|
jne @f
|
2012-04-06 20:37:00 +02:00
|
|
|
mov ecx, [edx + thread_data.datasocketnum]
|
|
|
|
lea edx, [edx + thread_data.datasock]
|
|
|
|
mov esi, sizeof.thread_data.datasock
|
|
|
|
mcall connect
|
|
|
|
mov edx, [esp+4] ; thread_data pointer
|
2012-04-04 15:08:07 +02:00
|
|
|
cmp eax, -1
|
2012-04-05 15:00:39 +02:00
|
|
|
je socketerror
|
2012-04-04 15:08:07 +02:00
|
|
|
@@:
|
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
; Create fpath from home_dir and work_dir
|
|
|
|
call create_path
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
lea eax, [edx + thread_data.fpath]
|
|
|
|
push eax
|
2012-04-05 15:00:39 +02:00
|
|
|
call [con_write_asciiz]
|
|
|
|
push str_newline
|
|
|
|
call [con_write_asciiz]
|
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
; Start the search
|
2012-04-05 15:00:39 +02:00
|
|
|
push FA_ANY
|
2012-04-04 15:08:07 +02:00
|
|
|
push str_mask
|
2012-04-06 20:37:00 +02:00
|
|
|
lea eax, [edx + thread_data.fpath]
|
|
|
|
push eax
|
2012-04-04 15:08:07 +02:00
|
|
|
call [file.find.first]
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
test eax, eax
|
|
|
|
jz .nosuchdir
|
|
|
|
|
|
|
|
lea edi, [edx + thread_data.buffer]
|
2012-04-04 15:08:07 +02:00
|
|
|
.parse_file:
|
2012-04-07 13:36:00 +02:00
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
test eax, eax ; did we find a file?
|
2012-04-04 15:08:07 +02:00
|
|
|
jz .done
|
2012-04-06 20:37:00 +02:00
|
|
|
mov ebx, eax ; yes, save the descripter in ebx
|
2012-04-04 15:08:07 +02:00
|
|
|
|
|
|
|
; first, convert the attributes
|
2012-04-06 20:37:00 +02:00
|
|
|
test [ebx + FileInfoA.Attributes], FA_FOLDER
|
2012-04-04 15:08:07 +02:00
|
|
|
jnz .folder
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
test [ebx + FileInfoA.Attributes], FA_READONLY
|
2012-04-04 15:08:07 +02:00
|
|
|
jnz .readonly
|
|
|
|
|
|
|
|
mov eax, '-rw-'
|
|
|
|
stosd
|
|
|
|
jmp .attr
|
|
|
|
|
|
|
|
.folder:
|
|
|
|
mov eax, 'drwx'
|
2012-04-04 19:19:00 +02:00
|
|
|
stosd
|
2012-04-04 15:08:07 +02:00
|
|
|
jmp .attr
|
|
|
|
|
|
|
|
.readonly:
|
|
|
|
mov eax, '-r--'
|
|
|
|
stosd
|
|
|
|
|
|
|
|
.attr:
|
|
|
|
mov eax, 'rw-r'
|
|
|
|
stosd
|
|
|
|
mov ax, 'w-'
|
|
|
|
stosw
|
|
|
|
mov al, ' '
|
|
|
|
stosb
|
|
|
|
|
|
|
|
; now..
|
|
|
|
mov ax, '1 '
|
|
|
|
stosw
|
|
|
|
|
|
|
|
; now write owner, everything is owned by FTP, woohoo!
|
|
|
|
mov eax, 'FTP '
|
|
|
|
stosd
|
|
|
|
stosd
|
|
|
|
|
|
|
|
; now the filesize in ascii
|
2012-04-06 20:37:00 +02:00
|
|
|
mov eax, [ebx + FileInfoA.FileSizeLow]
|
2012-04-04 15:08:07 +02:00
|
|
|
call dword_to_ascii
|
|
|
|
|
|
|
|
mov al, ' '
|
|
|
|
stosb
|
|
|
|
|
|
|
|
; then date (month/day/year)
|
2012-04-06 20:37:00 +02:00
|
|
|
movzx eax, [ebx + FileInfoA.DateModify + FileDateTime.month]
|
|
|
|
mov eax, [months + 4*eax]
|
2012-04-04 15:08:07 +02:00
|
|
|
stosd
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
movzx eax, [ebx + FileInfoA.DateModify + FileDateTime.day]
|
2012-04-04 15:08:07 +02:00
|
|
|
call dword_to_ascii
|
|
|
|
|
|
|
|
mov al, ' '
|
|
|
|
stosb
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
movzx eax, [ebx + FileInfoA.DateModify + FileDateTime.year]
|
2012-04-04 15:08:07 +02:00
|
|
|
call dword_to_ascii
|
|
|
|
|
|
|
|
mov al, ' '
|
|
|
|
stosb
|
|
|
|
|
|
|
|
; and last but not least, filename
|
2012-04-06 20:37:00 +02:00
|
|
|
lea esi, [ebx + FileInfoA.FileName]
|
|
|
|
mov ecx, 264
|
2012-04-04 15:08:07 +02:00
|
|
|
.nameloop:
|
|
|
|
lodsb
|
|
|
|
test al, al
|
|
|
|
jz .namedone
|
|
|
|
stosb
|
|
|
|
loop .nameloop
|
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
; insert a cr lf
|
2012-04-04 15:08:07 +02:00
|
|
|
.namedone:
|
2012-04-05 15:00:39 +02:00
|
|
|
mov ax, 0x0a0d
|
2012-04-04 15:08:07 +02:00
|
|
|
stosw
|
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
; check next file
|
2012-04-06 20:37:00 +02:00
|
|
|
push ebx
|
2012-04-04 19:19:00 +02:00
|
|
|
call [file.find.next]
|
|
|
|
jmp .parse_file
|
|
|
|
|
|
|
|
; close file desc
|
2012-04-04 15:08:07 +02:00
|
|
|
.done:
|
2012-04-06 20:37:00 +02:00
|
|
|
push ebx
|
2012-04-04 15:08:07 +02:00
|
|
|
call [file.find.close]
|
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
; append the string with a 0
|
2012-04-04 15:08:07 +02:00
|
|
|
xor al, al
|
|
|
|
stosb
|
|
|
|
|
2012-04-05 15:00:39 +02:00
|
|
|
; Warn the client we're about to send the data
|
2012-04-06 20:37:00 +02:00
|
|
|
push edi edx
|
|
|
|
mcall send, [edx + thread_data.socketnum], str150, str150.length, 0 ; here it comes..
|
|
|
|
pop edx esi
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
; and send it to the client
|
2012-04-06 20:37:00 +02:00
|
|
|
mov ecx, [edx + thread_data.datasocketnum]
|
|
|
|
lea edx, [edx + thread_data.buffer]
|
|
|
|
sub esi, edx
|
|
|
|
xor edi, edi
|
|
|
|
mcall send
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
; close the data socket..
|
2012-04-06 20:37:00 +02:00
|
|
|
mov edx, [esp+4] ; thread_data pointer
|
|
|
|
mcall close, [edx + thread_data.datasocketnum]
|
|
|
|
mov [edx + thread_data.mode], MODE_NOTREADY
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
; And send "transfer ok" on the base connection
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall send, [edx + thread_data.socketnum], str226, str226.length, 0
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.nosuchdir:
|
|
|
|
mcall send, [edx + thread_data.socketnum], str550, str550.length, 0
|
2012-04-04 15:08:07 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
align 4
|
|
|
|
cmdNLST:
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; TODO: same as list but simpler output format
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
cmdNOOP:
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
2012-04-03 22:28:26 +02:00
|
|
|
align 4
|
|
|
|
cmdPASS:
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; TODO: verify password
|
|
|
|
|
|
|
|
mcall send, [edx + thread_data.socketnum], str230, str230.length, 0
|
2012-04-04 11:24:08 +02:00
|
|
|
|
|
|
|
push str_pass_ok
|
|
|
|
call [con_write_asciiz]
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mov edx, [esp+4] ; thread_data pointer
|
|
|
|
mov [edx + thread_data.state], STATE_ACTIVE
|
2012-04-03 22:28:26 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2012-04-04 11:24:08 +02:00
|
|
|
align 4
|
|
|
|
cmdPASV:
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; Open a new TCP socket
|
2012-04-04 15:08:07 +02:00
|
|
|
mcall socket, AF_INET4, SOCK_STREAM, 0
|
2012-04-06 20:37:00 +02:00
|
|
|
mov edx, [esp+4] ; thread_data pointer
|
2012-04-04 15:08:07 +02:00
|
|
|
cmp eax, -1
|
2012-04-06 20:37:00 +02:00
|
|
|
je socketerror
|
|
|
|
mov [edx + thread_data.passivesocknum], eax
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; Bind it to a known local port
|
|
|
|
mov [edx + thread_data.datasock.sin_family], AF_INET4
|
|
|
|
mov [edx + thread_data.datasock.sin_port], 2000
|
|
|
|
mov [edx + thread_data.datasock.sin_addr], 0
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mov ecx, eax ;[edx + thread_data.passivesocknum]
|
|
|
|
lea edx, [edx + thread_data.datasock]
|
2012-04-07 13:36:00 +02:00
|
|
|
mov esi, sizeof.thread_data.datasock
|
|
|
|
mcall bind
|
2012-04-06 20:37:00 +02:00
|
|
|
mov edx, [esp+4] ; thread_data pointer
|
2012-04-04 15:08:07 +02:00
|
|
|
cmp eax, -1
|
|
|
|
je bind_err
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; And set it to listen!
|
|
|
|
mcall listen, [edx + thread_data.passivesocknum], 10 ;;;;; FIXME
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; Tell our thread we are ready to accept incoming calls
|
|
|
|
mov edx, [esp+4] ; thread_data pointer
|
|
|
|
mov [edx + thread_data.mode], MODE_PASSIVE_WAIT
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; Now tell the client where to connect to in this format:
|
|
|
|
; 227 Entering Passive Mode (a1,a2,a3,a4,p1,p2)
|
|
|
|
; where a1.a2.a3.a4 is the IP address and p1*256+p2 is the port number.
|
|
|
|
lea edi, [edx + thread_data.buffer]
|
2012-04-04 15:08:07 +02:00
|
|
|
mov eax, '227 ' ; FIXME (now hardcoded to 127.0.0.1:2000)
|
|
|
|
stosd
|
|
|
|
mov eax, '(127'
|
|
|
|
stosd
|
|
|
|
mov eax, ',0,0'
|
|
|
|
stosd
|
|
|
|
mov eax, ',1,7'
|
|
|
|
stosd
|
|
|
|
mov eax, ',208'
|
|
|
|
stosd
|
|
|
|
mov al, ')'
|
|
|
|
stosb
|
2012-04-05 15:00:39 +02:00
|
|
|
mov ax, 0x0a0d
|
2012-04-04 15:08:07 +02:00
|
|
|
stosw
|
|
|
|
xor al, al
|
|
|
|
stosb
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
lea esi, [edi - thread_data.buffer]
|
|
|
|
sub esi, edx
|
|
|
|
mov ecx, [edx + thread_data.socketnum]
|
|
|
|
lea edx, [edx + thread_data.buffer]
|
2012-04-07 13:36:00 +02:00
|
|
|
xor esi, esi
|
|
|
|
mcall send
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-04 11:24:08 +02:00
|
|
|
ret
|
|
|
|
|
2012-04-07 20:42:58 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
align 4
|
2012-04-04 19:19:00 +02:00
|
|
|
cmdPWD: ; Print Working Directory
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mov dword [edx + thread_data.buffer], '257 '
|
|
|
|
mov byte [edx + thread_data.buffer+4], '"'
|
2012-04-04 11:24:08 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
lea edi, [edx + thread_data.buffer+5]
|
|
|
|
lea esi, [edx + thread_data.work_dir]
|
2012-04-04 11:24:08 +02:00
|
|
|
mov ecx, 1024
|
|
|
|
.loop:
|
|
|
|
lodsb
|
|
|
|
or al, al
|
|
|
|
jz .ok
|
|
|
|
stosb
|
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
|
|
|
|
|
|
|
.ok:
|
2012-04-06 20:37:00 +02:00
|
|
|
mov dword [edi], '"' + 0x000a0d00 ; '"',13,10,0
|
|
|
|
lea esi, [edi - thread_data.buffer + 4]
|
|
|
|
sub esi, edx
|
|
|
|
mov ecx, [edx + thread_data.socketnum]
|
|
|
|
lea edx, [edx + thread_data.buffer]
|
2012-04-07 20:42:58 +02:00
|
|
|
xor edi, edi
|
|
|
|
mcall send
|
2012-04-04 11:24:08 +02:00
|
|
|
|
2012-04-07 20:42:58 +02:00
|
|
|
mov edx, [esp+4]
|
|
|
|
; Print the new working dir on the console
|
|
|
|
lea eax, [edx + thread_data.work_dir]
|
|
|
|
push eax
|
|
|
|
call [con_write_asciiz]
|
|
|
|
push str_newline
|
|
|
|
call [con_write_asciiz]
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
ret
|
|
|
|
|
2012-04-07 20:42:58 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
align 4
|
|
|
|
cmdPORT:
|
|
|
|
|
2012-04-04 11:24:08 +02:00
|
|
|
; PORT a1,a2,a3,a4,p1,p2
|
|
|
|
; IP address a1.a2.a3.a4, port p1*256+p2
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mov [edx + thread_data.mode], MODE_ACTIVE
|
2012-04-04 11:24:08 +02:00
|
|
|
|
|
|
|
lea esi, [esi+5]
|
2012-04-06 20:37:00 +02:00
|
|
|
; Convert the IP
|
2012-04-04 11:24:08 +02:00
|
|
|
call ascii_to_byte
|
2012-04-07 13:36:00 +02:00
|
|
|
mov bl, al
|
2012-04-06 20:37:00 +02:00
|
|
|
inc esi ; skip past ','
|
2012-04-04 11:24:08 +02:00
|
|
|
call ascii_to_byte
|
2012-04-07 13:36:00 +02:00
|
|
|
mov bh, al
|
2012-04-06 20:37:00 +02:00
|
|
|
shl ebx, 16
|
2012-04-04 11:24:08 +02:00
|
|
|
inc esi
|
|
|
|
call ascii_to_byte
|
2012-04-07 13:36:00 +02:00
|
|
|
mov bl, al
|
2012-04-04 11:24:08 +02:00
|
|
|
inc esi
|
|
|
|
call ascii_to_byte
|
2012-04-07 13:36:00 +02:00
|
|
|
mov bh, al
|
2012-04-04 11:24:08 +02:00
|
|
|
inc esi
|
2012-04-07 13:36:00 +02:00
|
|
|
rol ebx, 16
|
2012-04-04 11:24:08 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; And put it in datasock
|
|
|
|
mov [edx + thread_data.datasock.sin_addr], ebx
|
2012-04-04 11:24:08 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; Now the same with portnumber
|
2012-04-04 11:24:08 +02:00
|
|
|
call ascii_to_byte
|
2012-04-06 20:37:00 +02:00
|
|
|
mov bh, al
|
2012-04-04 11:24:08 +02:00
|
|
|
inc esi
|
|
|
|
call ascii_to_byte
|
2012-04-06 20:37:00 +02:00
|
|
|
mov bl, al
|
2012-04-04 11:24:08 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; Save it in datasock too
|
|
|
|
mov [edx + thread_data.datasock.sin_port], bx
|
2012-04-04 11:24:08 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; We will open the socket, but do not connect yet!
|
|
|
|
mov [edx + thread_data.datasock.sin_family], AF_INET4
|
2012-04-04 15:08:07 +02:00
|
|
|
mcall socket, AF_INET4, SOCK_STREAM, 0
|
2012-04-06 20:37:00 +02:00
|
|
|
mov edx, [esp+4] ; thread_data pointer
|
2012-04-04 11:24:08 +02:00
|
|
|
cmp eax, -1
|
2012-04-06 20:37:00 +02:00
|
|
|
je socketerror
|
|
|
|
mov [edx + thread_data.datasocketnum], eax
|
2012-04-04 11:24:08 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; Tell the client we are ready
|
|
|
|
mov edx, [esp+4] ; thread_data pointer
|
|
|
|
mcall send, [edx + thread_data.socketnum], str225, str225.length, 0
|
2012-04-04 11:24:08 +02:00
|
|
|
ret
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
align 4
|
|
|
|
cmdQUIT:
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall close, [edx + thread_data.datasocketnum]
|
|
|
|
mcall send, [edx + thread_data.socketnum], str221, str221.length, 0 ; 221 - bye!
|
|
|
|
mcall close;, [edx + thread_data.socketnum]
|
2012-04-03 22:28:26 +02:00
|
|
|
|
2012-04-07 13:36:00 +02:00
|
|
|
add esp, 4 ; get rid of call return address
|
|
|
|
jmp thread_exit ; now close this thread
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
align 4
|
|
|
|
cmdRETR:
|
|
|
|
|
2012-04-05 15:00:39 +02:00
|
|
|
sub ecx, 5
|
|
|
|
jb .cannot_open
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
cmp [edx + thread_data.mode], MODE_ACTIVE
|
2012-04-04 19:19:00 +02:00
|
|
|
jne @f
|
2012-04-05 15:00:39 +02:00
|
|
|
push esi
|
2012-04-06 20:37:00 +02:00
|
|
|
mov ecx, [edx + thread_data.datasocketnum]
|
|
|
|
lea edx, [edx + thread_data.datasock]
|
2012-04-07 13:36:00 +02:00
|
|
|
mov esi, sizeof.thread_data.datasock
|
|
|
|
mcall connect
|
2012-04-05 15:00:39 +02:00
|
|
|
pop esi
|
2012-04-06 20:37:00 +02:00
|
|
|
mov edx, [esp+4] ; thread_data pointer
|
2012-04-05 15:00:39 +02:00
|
|
|
cmp eax, -1
|
|
|
|
je socketerror
|
2012-04-04 19:19:00 +02:00
|
|
|
@@:
|
|
|
|
|
2012-04-05 15:00:39 +02:00
|
|
|
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
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
lea eax, [edx + thread_data.fpath]
|
|
|
|
push eax
|
2012-04-05 15:00:39 +02:00
|
|
|
call [con_write_asciiz]
|
|
|
|
push str_newline
|
|
|
|
call [con_write_asciiz]
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
push O_READ
|
2012-04-06 20:37:00 +02:00
|
|
|
lea eax, [edx + thread_data.fpath]
|
|
|
|
push eax
|
2012-04-04 19:19:00 +02:00
|
|
|
call [file.open]
|
2012-04-05 15:00:39 +02:00
|
|
|
test eax, eax
|
|
|
|
jz .cannot_open
|
2012-04-04 19:19:00 +02:00
|
|
|
|
2012-04-05 15:00:39 +02:00
|
|
|
push eax
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall send, [edx + thread_data.socketnum], str150, str150.length, 0 ; here it comes..
|
2012-04-05 15:00:39 +02:00
|
|
|
pop ebx
|
2012-04-04 19:19:00 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mov edx, [esp+4] ; thread_data pointer
|
2012-04-04 19:19:00 +02:00
|
|
|
.read_more:
|
|
|
|
push BUFFERSIZE
|
2012-04-06 20:37:00 +02:00
|
|
|
lea eax, [edx + thread_data.buffer]
|
|
|
|
push eax
|
2012-04-04 19:19:00 +02:00
|
|
|
push ebx
|
|
|
|
call [file.read]
|
2012-04-05 15:00:39 +02:00
|
|
|
cmp eax, -1
|
|
|
|
je .cannot_open ; fixme: this is not the correct error
|
2012-04-04 19:19:00 +02:00
|
|
|
|
|
|
|
push eax
|
|
|
|
push ebx
|
|
|
|
mov esi, eax
|
2012-04-06 20:37:00 +02:00
|
|
|
mov ecx, [edx + thread_data.datasocketnum]
|
|
|
|
lea edx, [edx + thread_data.buffer]
|
2012-04-07 13:36:00 +02:00
|
|
|
xor esi, esi
|
|
|
|
mcall send
|
2012-04-04 19:19:00 +02:00
|
|
|
pop ebx
|
|
|
|
pop ecx
|
2012-04-06 20:37:00 +02:00
|
|
|
mov edx, [esp+4] ; thread_data pointer
|
2012-04-05 15:00:39 +02:00
|
|
|
cmp eax, -1
|
|
|
|
je socketerror
|
2012-04-04 19:19:00 +02:00
|
|
|
|
|
|
|
cmp ecx, BUFFERSIZE
|
|
|
|
je .read_more
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall close, [edx + thread_data.datasocketnum]
|
2012-04-07 20:42:58 +02:00
|
|
|
mov [edx + thread_data.mode], MODE_NOTREADY
|
2012-04-04 19:19:00 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall send, [edx + thread_data.socketnum], str226, str226.length, 0 ; transfer ok
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
ret
|
|
|
|
|
2012-04-05 15:00:39 +02:00
|
|
|
.cannot_open:
|
|
|
|
pushd 0x0c
|
|
|
|
call [con_set_flags]
|
|
|
|
push str_notfound
|
|
|
|
call [con_write_asciiz]
|
|
|
|
pushd 0x07
|
|
|
|
call [con_set_flags]
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall send, [edx + thread_data.socketnum], str550, str550.length, 0 ; file not found
|
2012-04-05 15:00:39 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
align 4
|
|
|
|
cmdSTOR:
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; TODO: check if user has write permission, and write file if so
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
cmdSYST:
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall send, [edx + thread_data.socketnum], str215, str215.length, 0
|
2012-04-03 22:28:26 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
cmdTYPE:
|
|
|
|
|
2012-04-04 11:24:08 +02:00
|
|
|
cmp ecx, 6
|
|
|
|
jb parse_cmd.error
|
|
|
|
|
|
|
|
mov al, byte[esi+5]
|
|
|
|
and al, not 0x20
|
|
|
|
|
|
|
|
cmp al, 'A'
|
|
|
|
je .ascii
|
|
|
|
cmp al, 'E'
|
|
|
|
je .ebdic
|
|
|
|
cmp al, 'I'
|
|
|
|
je .image
|
|
|
|
cmp al, 'L'
|
|
|
|
je .local
|
|
|
|
|
|
|
|
jmp parse_cmd.error
|
|
|
|
|
|
|
|
.ascii:
|
2012-04-06 20:37:00 +02:00
|
|
|
mov [edx + thread_data.type], TYPE_ASCII
|
2012-04-04 11:24:08 +02:00
|
|
|
jmp .subtype
|
|
|
|
|
|
|
|
.ebdic:
|
2012-04-06 20:37:00 +02:00
|
|
|
mov [edx + thread_data.type], TYPE_EBDIC
|
2012-04-04 11:24:08 +02:00
|
|
|
|
|
|
|
.subtype:
|
|
|
|
cmp ecx, 8
|
|
|
|
jb .non_print
|
|
|
|
|
|
|
|
mov al, byte[esi+7]
|
|
|
|
and al, not 0x20
|
|
|
|
|
|
|
|
cmp al, 'N'
|
|
|
|
je .non_print
|
|
|
|
cmp al, 'T'
|
|
|
|
je .telnet
|
|
|
|
cmp al, 'C'
|
|
|
|
je .asacc
|
|
|
|
|
|
|
|
jmp parse_cmd.error
|
|
|
|
|
|
|
|
.non_print:
|
2012-04-06 20:37:00 +02:00
|
|
|
or [edx + thread_data.type], TYPE_NP
|
2012-04-04 11:24:08 +02:00
|
|
|
jmp .ok
|
|
|
|
|
|
|
|
.telnet:
|
2012-04-06 20:37:00 +02:00
|
|
|
or [edx + thread_data.type], TYPE_TELNET
|
2012-04-04 11:24:08 +02:00
|
|
|
jmp .ok
|
|
|
|
|
|
|
|
.asacc:
|
2012-04-06 20:37:00 +02:00
|
|
|
or [edx + thread_data.type], TYPE_ASA
|
2012-04-04 11:24:08 +02:00
|
|
|
jmp .ok
|
|
|
|
|
|
|
|
.image:
|
2012-04-06 20:37:00 +02:00
|
|
|
mov [edx + thread_data.type], TYPE_IMAGE
|
2012-04-04 11:24:08 +02:00
|
|
|
jmp .ok
|
|
|
|
|
|
|
|
.local:
|
|
|
|
cmp ecx, 8
|
|
|
|
jb parse_cmd.error
|
|
|
|
|
|
|
|
mov al, byte[esi+7]
|
|
|
|
sub al, '0'
|
|
|
|
jb parse_cmd.error
|
|
|
|
cmp al, 9
|
|
|
|
ja parse_cmd.error
|
|
|
|
or al, TYPE_LOCAL
|
2012-04-06 20:37:00 +02:00
|
|
|
mov [edx + thread_data.type], al
|
2012-04-04 11:24:08 +02:00
|
|
|
|
|
|
|
.ok:
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall send, [edx + thread_data.socketnum], str200, str200.length, 0
|
2012-04-04 11:24:08 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
ret
|
|
|
|
|
2012-04-07 20:42:58 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
align 4
|
|
|
|
cmdUSER:
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; TODO: check user and set home directory (and permissions)
|
2012-04-03 22:28:26 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mov [edx + thread_data.state], STATE_LOGIN
|
2012-04-07 20:42:58 +02:00
|
|
|
mov word [edx + thread_data.home_dir], "/" ; "/", 0
|
|
|
|
mov word [edx + thread_data.work_dir], "/" ; "/", 0
|
2012-04-04 11:24:08 +02:00
|
|
|
|
|
|
|
push str_logged_in
|
|
|
|
call [con_write_asciiz]
|
|
|
|
|
2012-04-07 20:42:58 +02:00
|
|
|
mcall send, [edx + thread_data.socketnum], str331, str331.length, 0 ; Now send me the password!
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
align 4 ; esi = ptr to str, output in eax
|
2012-04-04 11:24:08 +02:00
|
|
|
ascii_to_byte:
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
xor eax, eax
|
|
|
|
push ebx
|
2012-04-04 11:24:08 +02:00
|
|
|
|
|
|
|
.loop:
|
2012-04-06 20:37:00 +02:00
|
|
|
movzx ebx, byte[esi]
|
|
|
|
sub bl, '0'
|
2012-04-04 11:24:08 +02:00
|
|
|
jb .done
|
2012-04-06 20:37:00 +02:00
|
|
|
cmp bl, 9
|
2012-04-04 11:24:08 +02:00
|
|
|
ja .done
|
2012-04-06 20:37:00 +02:00
|
|
|
lea eax, [eax*4 + eax] ;
|
|
|
|
shl eax, 1 ; eax = eax * 10
|
|
|
|
add eax, ebx
|
2012-04-04 11:24:08 +02:00
|
|
|
inc esi
|
|
|
|
|
|
|
|
jmp .loop
|
|
|
|
|
|
|
|
.done:
|
2012-04-06 20:37:00 +02:00
|
|
|
pop ebx
|
2012-04-04 11:24:08 +02:00
|
|
|
ret
|
|
|
|
|
2012-04-04 15:08:07 +02:00
|
|
|
align 4
|
2012-04-06 20:37:00 +02:00
|
|
|
dword_to_ascii: ; edi = ptr where to write, eax is number
|
2012-04-04 15:08:07 +02:00
|
|
|
|
|
|
|
mov eax, '1'
|
|
|
|
stosb
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
2012-04-04 19:19:00 +02:00
|
|
|
align 4
|
|
|
|
create_path: ; combine home_dir and work_dir strings into fpath
|
2012-04-07 13:36:00 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
lea edi, [edx + thread_data.fpath]
|
|
|
|
lea esi, [edx + thread_data.home_dir]
|
2012-04-04 19:19:00 +02:00
|
|
|
mov ecx, 1024
|
|
|
|
.loop1:
|
|
|
|
lodsb
|
|
|
|
or al, al
|
|
|
|
jz .next
|
|
|
|
stosb
|
|
|
|
loop .loop1
|
|
|
|
.next:
|
|
|
|
|
|
|
|
cmp byte[edi-1], '/'
|
|
|
|
jne @f
|
|
|
|
dec edi
|
|
|
|
@@:
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
lea esi, [edx + thread_data.work_dir]
|
2012-04-04 19:19:00 +02:00
|
|
|
mov ecx, 1024
|
|
|
|
.loop2:
|
|
|
|
lodsb
|
|
|
|
or al, al
|
|
|
|
jz .done
|
|
|
|
stosb
|
|
|
|
loop .loop2
|
|
|
|
|
|
|
|
.done:
|
|
|
|
stosb
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2012-04-05 15:00:39 +02:00
|
|
|
align 4
|
|
|
|
socketerror:
|
|
|
|
|
|
|
|
pushd 0x0c
|
|
|
|
call [con_set_flags]
|
|
|
|
push str_sockerr
|
|
|
|
call [con_write_asciiz]
|
|
|
|
pushd 0x07
|
|
|
|
call [con_set_flags]
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall send, [edx + thread_data.socketnum], str425, str425.length, 0 ; data connection error
|
2012-04-05 15:00:39 +02:00
|
|
|
|
|
|
|
ret
|
2012-04-04 11:24:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
str150 db '150 Here it comes...', 13, 10
|
2012-04-04 15:08:07 +02:00
|
|
|
.length = $ - str150
|
2012-04-03 18:37:24 +02:00
|
|
|
str200 db '200 Command OK.', 13, 10
|
2012-04-04 11:24:08 +02:00
|
|
|
.length = $ - str200
|
2012-04-03 18:37:24 +02:00
|
|
|
str215 db '215 UNIX type: L8', 13, 10
|
2012-04-03 22:28:26 +02:00
|
|
|
.length = $ - str215
|
2012-04-03 18:37:24 +02:00
|
|
|
str220 db '220 KolibriOS FTP Daemon 1.0', 13, 10
|
|
|
|
.length = $ - str220
|
|
|
|
str221 db '221 Bye!', 13, 10
|
2012-04-03 22:28:26 +02:00
|
|
|
.length = $ - str221
|
2012-04-04 11:24:08 +02:00
|
|
|
str225 db '225 Data connection open', 13, 10
|
|
|
|
.length = $ - str225
|
2012-04-03 18:37:24 +02:00
|
|
|
str226 db '226 Transfer OK, Closing connection', 13, 10
|
2012-04-04 15:08:07 +02:00
|
|
|
.length = $ - str226
|
2012-04-03 18:37:24 +02:00
|
|
|
str230 db '230 You are now logged in.', 13, 10
|
2012-04-03 22:28:26 +02:00
|
|
|
.length = $ - str230
|
2012-04-03 18:37:24 +02:00
|
|
|
str250 db '250 command successful', 13, 10
|
2012-04-04 19:19:00 +02:00
|
|
|
.length = $ - str250
|
2012-04-03 18:37:24 +02:00
|
|
|
str331 db '331 Please specify the password.', 13, 10
|
2012-04-03 22:28:26 +02:00
|
|
|
.length = $ - str331
|
2012-04-04 11:24:08 +02:00
|
|
|
str421 db '421 Timeout!', 13, 10
|
|
|
|
.length = $ - str421
|
|
|
|
str425 db '425 Cant open data connection.', 13, 10
|
|
|
|
.length = $ - str425
|
2012-04-03 18:37:24 +02:00
|
|
|
str500 db '500 Unsupported command', 13, 10
|
2012-04-03 22:28:26 +02:00
|
|
|
.length = $ - str500
|
2012-04-04 19:19:00 +02:00
|
|
|
str550 db '550 No such file', 13, 10
|
|
|
|
.length = $ - str550
|