From 07aba6c98f3a30a0f2d7f1799681ff7308554ad6 Mon Sep 17 00:00:00 2001 From: hidnplayr Date: Tue, 3 Apr 2012 16:37:24 +0000 Subject: [PATCH] added FTP daemon stub (net branch) git-svn-id: svn://kolibrios.org@2554 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../net/applications/ftpd/commands.inc | 163 ++++++++++++++ .../branches/net/applications/ftpd/ftpd.asm | 205 ++++++++++++++++++ .../branches/net/applications/ftpd/ftpd.ini | 3 + .../branches/net/applications/ftpd/users.ini | 14 ++ 4 files changed, 385 insertions(+) create mode 100644 kernel/branches/net/applications/ftpd/commands.inc create mode 100644 kernel/branches/net/applications/ftpd/ftpd.asm create mode 100644 kernel/branches/net/applications/ftpd/ftpd.ini create mode 100644 kernel/branches/net/applications/ftpd/users.ini diff --git a/kernel/branches/net/applications/ftpd/commands.inc b/kernel/branches/net/applications/ftpd/commands.inc new file mode 100644 index 0000000000..fd8da05d72 --- /dev/null +++ b/kernel/branches/net/applications/ftpd/commands.inc @@ -0,0 +1,163 @@ + + + +align 4 +parse_cmd: ; esi must point to command + + mov eax, [esi] + and eax, not 0x20202020 ; convert to upper case + ; (also convert spaces to null) + mov edi, commands ; list of commands to scan + .scanloop: + cmp eax, [edi] + jb .error + jl .try_next + + jmp dword [edi+4] + + .try_next: + add edi, 8 + cmp byte [edi], 0 + jne .scanloop + + .error: + ret + + +align 4 +commands: ; all commands must be in uppercase, and in alphabetical order. + + db 'ABOR' + dd cmdABOR + + db 'CWD', 0 + dd cmdCWD + + db 'DELE' + dd cmdDELE + + db 'LIST' + dd cmdLIST + + db 'NLST' + dd cmdNLST + + db 'NOOP' + dd cmdNOOP + + db 'PWD', 0 + dd cmdPWD + + db 'PORT' + dd cmdPORT + + db 'QUIT' + dd cmdQUIT + + db 'RETR' + dd cmdRETR + + db 'STOR' + dd cmdSTOR + + db 'SYST' + dd cmdSYST + + db 'TYPE' + dd cmdTYPE + + db 'USER' + dd cmdUSER + + db 'XPWD' + dd cmdPWD + + db 0 ; end marker + + +align 4 +cmdABOR: + + ret + +align 4 +cmdCWD: + + ret + +align 4 +cmdDELE: + + ret + +align 4 +cmdLIST: + + ret + +align 4 +cmdNLST: + + ret + +align 4 +cmdNOOP: + + ret + +align 4 +cmdPWD: + + ret + +align 4 +cmdPORT: + + ret + +align 4 +cmdQUIT: + + ret + +align 4 +cmdRETR: + + ret + +align 4 +cmdSTOR: + + ret + +align 4 +cmdSYST: + + ret + +align 4 +cmdTYPE: + + ret + +align 4 +cmdUSER: + + ret + + + +str150 db '150 Here it comes...', 13, 10 +str200 db '200 Command OK.', 13, 10 +str215 db '215 UNIX type: L8', 13, 10 +str220 db '220 KolibriOS FTP Daemon 1.0', 13, 10 +.length = $ - str220 +str221 db '221 Bye!', 13, 10 +str225 db '225 Abort successful', 13, 10 +str226 db '226 Transfer OK, Closing connection', 13, 10 +str230 db '230 You are now logged in.', 13, 10 +str250 db '250 command successful', 13, 10 +str257 db '257 ""', 13, 10 +str331 db '331 Please specify the password.', 13, 10 +str500 db '500 Unsupported command', 13, 10 +str550 db '550 No such file', 13, 10 diff --git a/kernel/branches/net/applications/ftpd/ftpd.asm b/kernel/branches/net/applications/ftpd/ftpd.asm new file mode 100644 index 0000000000..2ddda790ee --- /dev/null +++ b/kernel/branches/net/applications/ftpd/ftpd.asm @@ -0,0 +1,205 @@ +; +; Kolibrios FTP Daemon +; +; hidnplayr@gmail.com +; +; GPLv2 +; + +BUFFERSIZE equ 4096 + + +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 + dd 0 ; parameters + dd path ; path + +include '../macros.inc' +purge mov,add,sub +include '../proc32.inc' +include '../dll.inc' + +include '../network.inc' +include 'commands.inc' + +align 4 +start: +; load libraries + stdcall dll.Load, @IMPORT + test eax, eax + jnz exit + +; find path to main settings file + mov edi, path ; Calculate the length of zero-terminated string + xor al , al + mov ecx, 1024 + repne scasb + dec edi + mov esi, filename + movsd + movsb + +; initialize console + push 1 + call [con_start] + push title + push 25 + push 80 + push 25 + push 80 + call [con_init] + + mcall 40, 1 shl 7 ; we only want network events + + push str1 + call [con_write_asciiz] + + mcall socket, AF_INET4, SOCK_STREAM, 0 + cmp eax, -1 + je sock_err + + mov [socketnum], eax + +;; mcall setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes, +;; cmp eax, -1 +;; je opt_err + + invoke ini.get_int, path, str_ftpd, str_port, 21 + mov [sockaddr1.port], ax + + mcall bind, [socketnum], sockaddr1, sockaddr1.length + cmp eax, -1 + je bind_err + + invoke ini.get_int, path, str_ftpd, str_conn, 1 ; Backlog (max connections) + mov edx, eax + mcall listen, [socketnum] + cmp eax, -1 + je listen_err + + push str2 + call [con_write_asciiz] + + mcall 10 + + mcall accept, [socketnum], sockaddr1, sockaddr1.length + cmp eax, -1 + je acpt_err + + mov [socketnum2], eax + +;; mcall close, [socketnum] + + mcall send, [socketnum2], str220, str220.length ; send welcome string + + .loop: + mcall 10 + + mcall recv, [socketnum2], buffer, buffer.length + + push buffer + call [con_write_asciiz] + + mov esi, buffer + call parse_cmd + + jmp .loop + +acpt_err: + push str8 + call [con_write_asciiz] + jmp done + +listen_err: + push str3 + call [con_write_asciiz] + jmp done + +bind_err: + push str4 + call [con_write_asciiz] + jmp done + +sock_err: + push str6 + call [con_write_asciiz] + jmp done + +done: + call [con_getch2] + push 1 + call [con_exit] +exit: + mcall -1 + + + +; data +title db 'KolibriOS FTP daemon 1.0',0 +str1 db 'Opening socket',10, 0 +str2 db 'Listening for incoming connections...',10,0 +str3 db 'Listen error',10,10,0 +str4 db 'Bind error',10,10,0 +str5 db 'Setsockopt error.',10,10,0 +str6 db 'Could not open socket',10,10,0 +str7 db 'Got data!',10,10,0 +str8 db 'Error accepting connection',10,10,0 + +filename db '.ini', 0 +str_port db 'port', 0 +str_ftpd db 'ftpd', 0 +str_conn db 'conn', 0 + +sockaddr1: + dw AF_INET4 +.port dw 21 +.ip dd 0 + rb 10 +.length = $ - sockaddr1 + +; import +align 4 +@IMPORT: + +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' + +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' + + +i_end: + +socketnum dd ? +socketnum2 dd ? + +buffer rb BUFFERSIZE +.length = BUFFERSIZE + +path rb 1024 +mem: diff --git a/kernel/branches/net/applications/ftpd/ftpd.ini b/kernel/branches/net/applications/ftpd/ftpd.ini new file mode 100644 index 0000000000..4b701f5532 --- /dev/null +++ b/kernel/branches/net/applications/ftpd/ftpd.ini @@ -0,0 +1,3 @@ +[ftpd] +port=21 +conn=10 \ No newline at end of file diff --git a/kernel/branches/net/applications/ftpd/users.ini b/kernel/branches/net/applications/ftpd/users.ini new file mode 100644 index 0000000000..8c57b3e24b --- /dev/null +++ b/kernel/branches/net/applications/ftpd/users.ini @@ -0,0 +1,14 @@ +[anonymous] +pass= ; leavy empty for none +home=/rd/1/ +mode=5 + +; Access modes +; 7 full +; 6 read and write +; 5 read and execute +; 4 read only +; 3 write and execute +; 2 write only +; 1 execute only (open folder) +; 0 none \ No newline at end of file