2012-04-03 18:37:24 +02:00
|
|
|
;
|
|
|
|
; Kolibrios FTP Daemon
|
|
|
|
;
|
|
|
|
; hidnplayr@gmail.com
|
|
|
|
;
|
|
|
|
; GPLv2
|
|
|
|
;
|
|
|
|
|
2012-04-05 15:00:39 +02:00
|
|
|
BUFFERSIZE = 8192
|
2012-04-03 22:28:26 +02:00
|
|
|
|
2012-04-10 21:44:51 +02:00
|
|
|
; using multiple's of 4
|
|
|
|
STATE_CONNECTED = 4*0
|
|
|
|
STATE_LOGIN = 4*1
|
|
|
|
STATE_LOGIN_FAIL = 4*2 ; When an invalid username was given
|
|
|
|
STATE_ACTIVE = 4*3
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-04 11:24:08 +02:00
|
|
|
TYPE_UNDEF = 0
|
|
|
|
|
|
|
|
TYPE_ASCII = 00000100b
|
|
|
|
TYPE_EBDIC = 00001000b
|
|
|
|
; subtypes for ascii & ebdic (np = default)
|
|
|
|
TYPE_NP = 00000001b ; non printable
|
|
|
|
TYPE_TELNET = 00000010b
|
|
|
|
TYPE_ASA = 00000011b
|
|
|
|
|
|
|
|
TYPE_IMAGE = 01000000b ; binary data
|
|
|
|
TYPE_LOCAL = 10000000b ; bits per byte must be specified
|
|
|
|
; lower 4 bits will hold this value
|
|
|
|
MODE_NOTREADY = 0
|
|
|
|
MODE_ACTIVE = 1
|
2012-04-04 15:08:07 +02:00
|
|
|
MODE_PASSIVE_WAIT = 2
|
|
|
|
MODE_PASSIVE_OK = 3
|
2012-04-10 21:44:51 +02:00
|
|
|
MODE_PASSIVE_FAILED = 4
|
|
|
|
|
|
|
|
PERMISSION_EXEC = 1b ; LIST
|
|
|
|
PERMISSION_READ = 10b
|
|
|
|
PERMISSION_WRITE = 100b
|
|
|
|
PERMISSION_DELETE = 1000b
|
|
|
|
PERMISSION_CD = 10000b ; Change Directory
|
|
|
|
|
|
|
|
ABORT = 1 shl 31
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-07 13:36:00 +02:00
|
|
|
format binary as ""
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
use32
|
|
|
|
db 'MENUET01' ; signature
|
|
|
|
dd 1 ; header version
|
|
|
|
dd start ; entry point
|
|
|
|
dd i_end ; initialized size
|
|
|
|
dd mem+0x1000 ; required memory
|
|
|
|
dd mem+0x1000 ; stack pointer
|
2012-04-06 20:37:00 +02:00
|
|
|
dd params ; parameters
|
2012-04-03 18:37:24 +02:00
|
|
|
dd path ; path
|
|
|
|
|
|
|
|
include '../macros.inc'
|
|
|
|
purge mov,add,sub
|
|
|
|
include '../proc32.inc'
|
|
|
|
include '../dll.inc'
|
2012-04-04 15:08:07 +02:00
|
|
|
include '../struct.inc'
|
|
|
|
include '../libio.inc'
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
include '../network.inc'
|
|
|
|
include 'commands.inc'
|
|
|
|
|
|
|
|
align 4
|
|
|
|
start:
|
|
|
|
; load libraries
|
|
|
|
stdcall dll.Load, @IMPORT
|
|
|
|
test eax, eax
|
|
|
|
jnz exit
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall 68, 11 ; init heap
|
|
|
|
|
2012-04-10 21:44:51 +02:00
|
|
|
; find path to main settings file (ftpd.ini)
|
2012-04-06 20:37:00 +02:00
|
|
|
mov edi, path ; Calculate the length of zero-terminated string
|
2012-04-07 20:42:58 +02:00
|
|
|
xor al, al
|
2012-04-03 18:37:24 +02:00
|
|
|
mov ecx, 1024
|
|
|
|
repne scasb
|
|
|
|
dec edi
|
2012-04-10 21:44:51 +02:00
|
|
|
mov esi, str_ini ; append it with '.ini', 0
|
2012-04-03 18:37:24 +02:00
|
|
|
movsd
|
|
|
|
movsb
|
|
|
|
|
2012-04-10 21:44:51 +02:00
|
|
|
; now create the second path (users.ini)
|
|
|
|
std
|
|
|
|
mov al, '/'
|
|
|
|
repne scasb
|
|
|
|
lea ecx, [edi - path + 2]
|
|
|
|
cld
|
|
|
|
mov esi, path
|
|
|
|
mov edi, path2
|
|
|
|
rep movsb
|
|
|
|
mov esi, str_users
|
|
|
|
movsd
|
|
|
|
movsd
|
|
|
|
movsw
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
; initialize console
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_start, 1
|
|
|
|
invoke con_init, -1, -1, -1, -1, title
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall 40, 1 shl 7 ; we only want network events
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-10 21:44:51 +02:00
|
|
|
invoke ini.get_str, path, str_ftpd, str_ip, ini_buf, 16, 0
|
|
|
|
mov esi, ini_buf
|
|
|
|
call ip_to_dword
|
|
|
|
mov [serverip], ebx
|
|
|
|
|
2012-04-04 11:24:08 +02:00
|
|
|
invoke ini.get_int, path, str_ftpd, str_port, 21
|
|
|
|
mov [sockaddr1.port], ax
|
|
|
|
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_printf, str1, eax
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
mcall socket, AF_INET4, SOCK_STREAM, 0
|
|
|
|
cmp eax, -1
|
|
|
|
je sock_err
|
|
|
|
mov [socketnum], eax
|
|
|
|
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_write_asciiz, str2
|
2012-04-04 11:24:08 +02:00
|
|
|
|
2012-04-07 20:42:58 +02:00
|
|
|
; mcall setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
|
|
|
|
; cmp eax, -1
|
|
|
|
; je opt_err
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
mcall bind, [socketnum], sockaddr1, sockaddr1.length
|
|
|
|
cmp eax, -1
|
|
|
|
je bind_err
|
|
|
|
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_write_asciiz, str2
|
2012-04-04 11:24:08 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
invoke ini.get_int, path, str_ftpd, str_conn, 1 ; Backlog (max connections)
|
|
|
|
mov edx, eax
|
2012-04-04 11:24:08 +02:00
|
|
|
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_write_asciiz, str2
|
2012-04-04 11:24:08 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
mcall listen, [socketnum]
|
|
|
|
cmp eax, -1
|
|
|
|
je listen_err
|
|
|
|
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_write_asciiz, str2b
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mainloop:
|
|
|
|
mcall 10 ; Wait here for incoming connections on the base socket (socketnum)
|
|
|
|
|
|
|
|
mcall 51, 1, threadstart, 0 ; Start a new thread for every incoming connection
|
|
|
|
; NOTE: upon initialisation of the thread, stack will not be available!
|
|
|
|
jmp mainloop
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-07 20:42:58 +02:00
|
|
|
diff16 "threadstart", 0, $
|
2012-04-06 20:37:00 +02:00
|
|
|
threadstart:
|
|
|
|
mcall 68, 12, sizeof.thread_data ; allocate the thread data struct
|
2012-04-03 18:37:24 +02:00
|
|
|
cmp eax, -1
|
2012-04-06 20:37:00 +02:00
|
|
|
je exit
|
|
|
|
|
|
|
|
lea esp, [eax + thread_data.stack] ; init stack
|
|
|
|
push eax ; save pointer to thread_data on stack
|
2012-04-10 21:44:51 +02:00
|
|
|
mov ebp, esp
|
2012-04-06 20:37:00 +02:00
|
|
|
|
|
|
|
mcall 40, 1 shl 7 ; we only want network events for this thread
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_set_flags, 0x03
|
|
|
|
invoke con_write_asciiz, str8 ; print on the console that we have created the new thread successfully
|
|
|
|
invoke con_set_flags, 0x07
|
2012-04-06 20:37:00 +02:00
|
|
|
|
|
|
|
mcall accept, [socketnum], sockaddr1, sockaddr1.length ; time to accept the awaiting connection..
|
|
|
|
cmp eax, -1
|
|
|
|
je thread_exit
|
2012-04-10 21:44:51 +02:00
|
|
|
mov edx, [ebp] ; pointer to thread_data
|
2012-04-06 20:37:00 +02:00
|
|
|
mov [edx + thread_data.socketnum], eax
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-10 21:44:51 +02:00
|
|
|
mov [edx + thread_data.state], STATE_CONNECTED
|
|
|
|
mov [edx + thread_data.permissions], 0
|
|
|
|
mov [edx + thread_data.mode], MODE_NOTREADY
|
|
|
|
lea eax, [edx + thread_data.buffer]
|
|
|
|
mov [edx + thread_data.buffer_ptr], eax
|
|
|
|
|
|
|
|
sendFTP "220 Welcome to KolibriOS FTP daemon"
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
threadloop:
|
2012-04-03 18:37:24 +02:00
|
|
|
mcall 10
|
2012-04-10 21:44:51 +02:00
|
|
|
mov edx, [ebp] ; pointer to thread_data
|
2012-04-06 20:37:00 +02:00
|
|
|
|
|
|
|
cmp [edx + thread_data.mode], MODE_PASSIVE_WAIT
|
2012-04-10 21:44:51 +02:00
|
|
|
jne .not_passive
|
2012-04-12 11:58:58 +02:00
|
|
|
mov [edx + thread_data.mode], MODE_PASSIVE_FAILED ; assume that we will fail
|
2012-04-06 20:37:00 +02:00
|
|
|
mov ecx, [edx + thread_data.passivesocknum]
|
|
|
|
lea edx, [edx + thread_data.datasock]
|
|
|
|
mov esi, sizeof.thread_data.datasock
|
|
|
|
mcall accept
|
2012-04-10 21:44:51 +02:00
|
|
|
mov edx, [ebp] ; pointer to thread_data
|
2012-04-04 15:08:07 +02:00
|
|
|
cmp eax, -1
|
2012-04-10 21:44:51 +02:00
|
|
|
je .not_passive
|
2012-04-06 20:37:00 +02:00
|
|
|
mov [edx + thread_data.datasocketnum], eax
|
2012-04-12 11:58:58 +02:00
|
|
|
mov [edx + thread_data.mode], MODE_PASSIVE_OK
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_write_asciiz, str_datasock
|
2012-04-10 21:44:51 +02:00
|
|
|
.not_passive:
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mov ecx, [edx + thread_data.socketnum]
|
2012-04-10 21:44:51 +02:00
|
|
|
mov edx, [edx + thread_data.buffer_ptr]
|
|
|
|
mov esi, sizeof.thread_data.buffer ;;; FIXME
|
2012-04-06 20:37:00 +02:00
|
|
|
mcall recv
|
2012-04-10 21:44:51 +02:00
|
|
|
inc eax ; error? (-1)
|
2012-04-06 20:37:00 +02:00
|
|
|
jz threadloop
|
2012-04-10 21:44:51 +02:00
|
|
|
dec eax ; 0 bytes read?
|
|
|
|
jz threadloop
|
|
|
|
|
|
|
|
mov edx, [ebp] ; pointer to thread_data
|
|
|
|
mov edi, [edx + thread_data.buffer_ptr]
|
|
|
|
add [edx + thread_data.buffer_ptr], eax
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-10 21:44:51 +02:00
|
|
|
; Check if we received a newline character, if not, wait for more data
|
|
|
|
mov ecx, eax
|
|
|
|
mov al, 13
|
|
|
|
repne scasb
|
|
|
|
jne threadloop
|
|
|
|
|
|
|
|
; We got a command!
|
2012-04-12 11:58:58 +02:00
|
|
|
mov byte [edi + 1], 0 ; append string with zero byte
|
2012-04-06 20:37:00 +02:00
|
|
|
lea esi, [edx + thread_data.buffer]
|
2012-04-12 11:58:58 +02:00
|
|
|
mov ecx, [edx + thread_data.buffer_ptr]
|
|
|
|
sub ecx, esi
|
|
|
|
mov [edx + thread_data.buffer_ptr], esi ; reset buffer ptr
|
|
|
|
|
|
|
|
invoke con_set_flags, 0x02 ; print received data to console (in green color)
|
|
|
|
invoke con_write_asciiz, str_newline
|
|
|
|
invoke con_write_asciiz, esi
|
|
|
|
invoke con_set_flags, 0x07
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-12 11:58:58 +02:00
|
|
|
push threadloop
|
|
|
|
jmp parse_cmd
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
listen_err:
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_set_flags, 0x0c ; print received data to console (in green color)
|
|
|
|
invoke con_write_asciiz, str3
|
2012-04-03 18:37:24 +02:00
|
|
|
jmp done
|
|
|
|
|
|
|
|
bind_err:
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_set_flags, 0x0c ; print received data to console (in green color)
|
|
|
|
invoke con_write_asciiz, str4
|
2012-04-03 18:37:24 +02:00
|
|
|
jmp done
|
|
|
|
|
|
|
|
sock_err:
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_set_flags, 0x0c ; print received data to console (in green color)
|
|
|
|
invoke con_write_asciiz, str6
|
2012-04-03 18:37:24 +02:00
|
|
|
jmp done
|
|
|
|
|
|
|
|
done:
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_getch2
|
|
|
|
invoke con_exit, 1
|
2012-04-03 18:37:24 +02:00
|
|
|
exit:
|
|
|
|
mcall -1
|
|
|
|
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
thread_exit:
|
2012-04-12 11:58:58 +02:00
|
|
|
invoke con_set_flags, 0x02 ; print received data to console (in green color)
|
|
|
|
invoke con_write_asciiz, str_bye
|
2012-04-06 20:37:00 +02:00
|
|
|
pop ecx ; get the thread_data pointer from stack
|
|
|
|
mcall 68, 13 ; free the memory
|
|
|
|
mcall -1 ; and kill the thread
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; initialized data
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
title db 'KolibriOS FTP daemon 0.1', 0
|
|
|
|
str1 db 'Starting FTP daemon on port %u', 0
|
|
|
|
str2 db '.', 0
|
2012-04-07 20:42:58 +02:00
|
|
|
str2b db ' OK!',10,0
|
|
|
|
str3 db 'Listen error',10,0
|
|
|
|
str4 db 'Bind error',10,0
|
2012-04-06 20:37:00 +02:00
|
|
|
;str5 db 'Setsockopt error.',10,10,0
|
2012-04-07 20:42:58 +02:00
|
|
|
str6 db 'Could not open socket',10,0
|
2012-04-06 20:37:00 +02:00
|
|
|
str7 db 'Got data!',10,10,0
|
2012-04-07 20:42:58 +02:00
|
|
|
str8 db 10,'New thread created!',10,0
|
|
|
|
str_bye db 10,'Closing thread!',10,0
|
2012-04-03 18:37:24 +02:00
|
|
|
|
2012-04-07 20:42:58 +02:00
|
|
|
str_logged_in db 'Login ok',10,0
|
2012-04-12 11:58:58 +02:00
|
|
|
str_pass_ok db 'Password ok',10,0
|
2012-04-04 15:08:07 +02:00
|
|
|
str_pwd db 'Current directory is "%s"\n',0
|
2012-04-07 20:42:58 +02:00
|
|
|
str_err2 db 'ERROR: cannot open directory',10,0
|
|
|
|
str_datasock db 'Passive data socket connected!',10,0
|
|
|
|
str_notfound db 'ERROR: file not found',10,0
|
|
|
|
str_sockerr db 'ERROR: socket error',10,0
|
2012-04-05 15:00:39 +02:00
|
|
|
|
2012-04-10 21:44:51 +02:00
|
|
|
str_login_invalid db 'Login invalid',10,0
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
str_newline db 10, 0
|
2012-04-04 15:08:07 +02:00
|
|
|
str_mask db '*', 0
|
2012-04-12 11:58:58 +02:00
|
|
|
str_infinity db 0xff, 0xff, 0xff, 0xff, 0
|
2012-04-04 15:08:07 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
months dd 'Jan '
|
|
|
|
dd 'Feb '
|
|
|
|
dd 'Mar '
|
|
|
|
dd 'Apr '
|
|
|
|
dd 'May '
|
|
|
|
dd 'Jun '
|
|
|
|
dd 'Jul '
|
|
|
|
dd 'Aug '
|
|
|
|
dd 'Sep '
|
|
|
|
dd 'Oct '
|
|
|
|
dd 'Nov '
|
|
|
|
dd 'Dec '
|
|
|
|
|
2012-04-10 21:44:51 +02:00
|
|
|
str_users db 'users'
|
|
|
|
str_ini db '.ini', 0
|
2012-04-06 20:37:00 +02:00
|
|
|
str_port db 'port', 0
|
|
|
|
str_ftpd db 'ftpd', 0
|
|
|
|
str_conn db 'conn', 0
|
2012-04-10 21:44:51 +02:00
|
|
|
str_ip db 'ip', 0
|
|
|
|
str_pass db 'pass', 0
|
|
|
|
str_home db 'home', 0
|
|
|
|
str_mode db 'mode', 0
|
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
sockaddr1:
|
2012-04-06 20:37:00 +02:00
|
|
|
dw AF_INET4
|
|
|
|
.port dw 21
|
|
|
|
.ip dd 0
|
|
|
|
rb 10
|
|
|
|
.length = $ - sockaddr1
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
; import
|
2012-04-06 20:37:00 +02:00
|
|
|
|
2012-04-03 18:37:24 +02:00
|
|
|
align 4
|
|
|
|
@IMPORT:
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
library console, 'console.obj',\
|
|
|
|
libini, 'libini.obj', \
|
|
|
|
libio, 'libio.obj'
|
|
|
|
|
|
|
|
import console,\
|
|
|
|
con_start, 'START',\
|
|
|
|
con_init, 'con_init',\
|
|
|
|
con_write_asciiz, 'con_write_asciiz',\
|
|
|
|
con_exit, 'con_exit',\
|
|
|
|
con_gets, 'con_gets',\
|
|
|
|
con_cls, 'con_cls',\
|
|
|
|
con_printf, 'con_printf',\
|
|
|
|
con_getch2, 'con_getch2',\
|
|
|
|
con_set_cursor_pos, 'con_set_cursor_pos',\
|
|
|
|
con_set_flags, 'con_set_flags'
|
|
|
|
|
|
|
|
import libini,\
|
|
|
|
ini.get_str, 'ini_get_str',\
|
|
|
|
ini.get_int, 'ini_get_int'
|
|
|
|
|
|
|
|
import libio,\
|
|
|
|
libio.init, 'lib_init',\
|
|
|
|
file.size, 'file_size',\
|
|
|
|
file.open, 'file_open',\
|
|
|
|
file.read, 'file_read',\
|
|
|
|
file.close, 'file_close',\
|
|
|
|
file.find.first, 'file_find_first',\
|
|
|
|
file.find.next, 'file_find_next',\
|
|
|
|
file.find.close, 'file_find_close'
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
i_end:
|
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
; uninitialised data
|
2012-04-03 22:28:26 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
socketnum dd ?
|
|
|
|
path rb 1024
|
2012-04-10 21:44:51 +02:00
|
|
|
path2 rb 1024
|
2012-04-06 20:37:00 +02:00
|
|
|
params rb 1024
|
2012-04-10 21:44:51 +02:00
|
|
|
serverip dd ?
|
|
|
|
|
|
|
|
ini_buf rb 3*4+3+1
|
2012-04-03 22:28:26 +02:00
|
|
|
|
2012-04-06 20:37:00 +02:00
|
|
|
mem:
|
2012-04-03 18:37:24 +02:00
|
|
|
|
|
|
|
|