Subversion Repositories Kolibri OS

Rev

Rev 2602 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;
  2. ; Kolibrios FTP Daemon
  3. ;
  4. ; hidnplayr@gmail.com
  5. ;
  6. ; GPLv2
  7. ;
  8.  
  9. BUFFERSIZE              = 8192
  10.  
  11. ; using multiple's of 4
  12. STATE_CONNECTED         = 4*0
  13. STATE_LOGIN             = 4*1
  14. STATE_LOGIN_FAIL        = 4*2           ; When an invalid username was given
  15. STATE_ACTIVE            = 4*3
  16.  
  17. TYPE_UNDEF              = 0
  18.  
  19. TYPE_ASCII              = 00000100b
  20. TYPE_EBDIC              = 00001000b
  21. ; subtypes for ascii & ebdic (np = default)
  22. TYPE_NP                 = 00000001b     ; non printable
  23. TYPE_TELNET             = 00000010b
  24. TYPE_ASA                = 00000011b
  25.  
  26. TYPE_IMAGE              = 01000000b     ; binary data
  27. TYPE_LOCAL              = 10000000b     ; bits per byte must be specified
  28.                                         ; lower 4 bits will hold this value
  29. MODE_NOTREADY           = 0
  30. MODE_ACTIVE             = 1
  31. MODE_PASSIVE_WAIT       = 2
  32. MODE_PASSIVE_OK         = 3
  33. MODE_PASSIVE_FAILED     = 4
  34.  
  35. PERMISSION_EXEC         = 1b            ; LIST
  36. PERMISSION_READ         = 10b
  37. PERMISSION_WRITE        = 100b
  38. PERMISSION_DELETE       = 1000b
  39. PERMISSION_CD           = 10000b        ; Change Directory
  40.  
  41. ABORT                   = 1 shl 31
  42.  
  43. format binary as ""
  44.  
  45. use32
  46.         db      'MENUET01'      ; signature
  47.         dd      1               ; header version
  48.         dd      start           ; entry point
  49.         dd      i_end           ; initialized size
  50.         dd      mem+0x1000      ; required memory
  51.         dd      mem+0x1000      ; stack pointer
  52.         dd      params          ; parameters
  53.         dd      path            ; path
  54.  
  55. include '../macros.inc'
  56. purge mov,add,sub
  57. include '../proc32.inc'
  58. include '../dll.inc'
  59. include '../struct.inc'
  60. include '../libio.inc'
  61.  
  62. include '../network.inc'
  63. include 'commands.inc'
  64.  
  65. align 4
  66. start:
  67.         mcall   68, 11                  ; init heap
  68.         mcall   40, 1 shl 7             ; we only want network events
  69.  
  70. ; load libraries
  71.         stdcall dll.Load, @IMPORT
  72.         test    eax, eax
  73.         jnz     exit
  74.  
  75. ; find path to main settings file (ftpd.ini)
  76.         mov     edi, path               ; Calculate the length of zero-terminated string
  77.         xor     al, al
  78.         mov     ecx, 1024
  79.         repne   scasb
  80.         dec     edi
  81.         mov     esi, str_ini            ; append it with '.ini', 0
  82.         movsd
  83.         movsb
  84.  
  85. ; now create the second path (users.ini)
  86.         std
  87.         mov     al, '/'
  88.         repne   scasb
  89.         lea     ecx, [edi - path + 2]
  90.         cld
  91.         mov     esi, path
  92.         mov     edi, path2
  93.         rep     movsb
  94.         mov     esi, str_users
  95.         movsd
  96.         movsd
  97.         movsw
  98.  
  99. ; initialize console
  100.         invoke  con_start, 1
  101.         invoke  con_init, -1, -1, -1, -1, title
  102.  
  103.         invoke  ini.get_str, path, str_ftpd, str_ip, ini_buf, 16, 0
  104.         mov     esi, ini_buf
  105.         call    ip_to_dword
  106.         mov     [serverip], ebx
  107.  
  108.         invoke  ini.get_int, path, str_ftpd, str_port, 21
  109.         mov     [sockaddr1.port], ax
  110.  
  111.         invoke  con_printf, str1, eax
  112.  
  113.         mcall   socket, AF_INET4, SOCK_STREAM, 0
  114.         cmp     eax, -1
  115.         je      sock_err
  116.         mov     [socketnum], eax
  117.  
  118.         invoke  con_write_asciiz, str2
  119.  
  120. ;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
  121. ;        cmp     eax, -1
  122. ;        je      opt_err
  123.  
  124.         mcall   bind, [socketnum], sockaddr1, sockaddr1.length
  125.         cmp     eax, -1
  126.         je      bind_err
  127.  
  128.         invoke  con_write_asciiz, str2
  129.  
  130.         invoke  ini.get_int, path, str_ftpd, str_conn, 1        ; Backlog (max connections)
  131.         mov     edx, eax
  132.  
  133.         invoke  con_write_asciiz, str2
  134.  
  135.         mcall   listen, [socketnum]
  136.         cmp     eax, -1
  137.         je      listen_err
  138.  
  139.         invoke  con_write_asciiz, str2b
  140.  
  141.         mov     [pasvport], 2000        ;;;;;; FIXME
  142.  
  143. mainloop:
  144.         mcall   10                              ; Wait here for incoming connections on the base socket (socketnum)
  145.  
  146.         mcall   51, 1, threadstart, 0           ; Start a new thread for every incoming connection
  147.                                                 ; NOTE: upon initialisation of the thread, stack will not be available!
  148.         jmp     mainloop
  149.  
  150.         diff16  "threadstart", 0, $
  151. threadstart:
  152. ;;;        mcall   68, 11                          ; init heap
  153.         mcall   68, 12, sizeof.thread_data      ; allocate the thread data struct
  154.         test    eax, eax
  155.         je      exit
  156.  
  157.         lea     esp, [eax + thread_data.stack]  ; init stack
  158.         mov     ebp, eax
  159.  
  160.         mcall   40, 1 shl 7                     ; we only want network events for this thread
  161.  
  162.         invoke  con_set_flags, 0x03
  163.         invoke  con_write_asciiz, str8          ; print on the console that we have created the new thread successfully
  164.         invoke  con_set_flags, 0x07
  165.  
  166.         mcall   accept, [socketnum], sockaddr1, sockaddr1.length                ; time to accept the awaiting connection..
  167.         cmp     eax, -1
  168.         je      thread_exit
  169.         mov     [ebp + thread_data.socketnum], eax
  170.  
  171.         mov     [ebp + thread_data.state], STATE_CONNECTED
  172.         mov     [ebp + thread_data.permissions], 0
  173.         mov     [ebp + thread_data.mode], MODE_NOTREADY
  174.         lea     eax, [ebp + thread_data.buffer]
  175.         mov     [ebp + thread_data.buffer_ptr], eax
  176.  
  177.         sendFTP "220 Welcome to KolibriOS FTP daemon"
  178.  
  179. threadloop:
  180.         mcall   10
  181.         mov     edx, [ebp]                                                      ; pointer to thread_data
  182.  
  183.         cmp     [ebp + thread_data.mode], MODE_PASSIVE_WAIT
  184.         jne     .not_passive
  185.         mov     [ebp + thread_data.mode], MODE_PASSIVE_FAILED                   ; assume that we will fail
  186.         mov     ecx, [ebp + thread_data.passivesocknum]
  187.         lea     edx, [ebp + thread_data.datasock]
  188.         mov     esi, sizeof.thread_data.datasock
  189.         mcall   accept
  190.         cmp     eax, -1
  191.         je      .not_passive
  192.         mov     [ebp + thread_data.datasocketnum], eax
  193.         mov     [ebp + thread_data.mode], MODE_PASSIVE_OK
  194.  
  195.         invoke  con_write_asciiz, str_datasock
  196.   .not_passive:
  197.  
  198.         mov     ecx, [ebp + thread_data.socketnum]
  199.         mov     edx, [ebp + thread_data.buffer_ptr]
  200.         mov     esi, sizeof.thread_data.buffer    ;;; FIXME
  201.         mcall   recv
  202.         inc     eax                                                             ; error? (-1)
  203.         jz      threadloop
  204.         dec     eax                                                             ; 0 bytes read?
  205.         jz      threadloop
  206.  
  207.         mov     edi, [ebp + thread_data.buffer_ptr]
  208.         add     [ebp + thread_data.buffer_ptr], eax
  209.  
  210. ; Check if we received a newline character, if not, wait for more data
  211.         mov     ecx, eax
  212.         mov     al, 13
  213.         repne   scasb
  214.         jne     threadloop
  215.  
  216. ; We got a command!
  217.         mov     byte [edi + 1], 0                                               ; append string with zero byte
  218.         lea     esi, [ebp + thread_data.buffer]
  219.         mov     ecx, [ebp + thread_data.buffer_ptr]
  220.         sub     ecx, esi
  221.         mov     [ebp + thread_data.buffer_ptr], esi                             ; reset buffer ptr
  222.  
  223.         invoke  con_set_flags, 0x02                                                            ; print received data to console (in green color)
  224.         invoke  con_write_asciiz, str_newline
  225.         invoke  con_write_asciiz, esi
  226.         invoke  con_set_flags, 0x07
  227.  
  228.         push    threadloop
  229.         jmp     parse_cmd
  230.  
  231. listen_err:
  232.         invoke  con_set_flags, 0x0c                                                            ; print received data to console (in green color)
  233.         invoke  con_write_asciiz, str3
  234.         jmp     done
  235.  
  236. bind_err:
  237.         invoke  con_set_flags, 0x0c                                                            ; print received data to console (in green color)
  238.         invoke  con_write_asciiz, str4
  239.         jmp     done
  240.  
  241. sock_err:
  242.         invoke  con_set_flags, 0x0c                                                            ; print received data to console (in green color)
  243.         invoke  con_write_asciiz, str6
  244.         jmp     done
  245.  
  246. done:
  247.         invoke  con_getch2
  248.         invoke  con_exit, 1
  249. exit:
  250.         mcall   -1
  251.  
  252.  
  253. thread_exit:
  254.         invoke  con_set_flags, 0x02                                                            ; print received data to console (in green color)
  255.         invoke  con_write_asciiz, str_bye
  256.         pop     ecx                     ; get the thread_data pointer from stack
  257.         mcall   68, 13                  ; free the memory
  258.         mcall   -1                      ; and kill the thread
  259.  
  260.  
  261.  
  262. ; initialized data
  263.  
  264. title           db 'KolibriOS FTP daemon 0.1', 0
  265. str1            db 'Starting FTP daemon on port %u', 0
  266. str2            db '.', 0
  267. str2b           db ' OK!',10,0
  268. str3            db 'Listen error',10,0
  269. str4            db 'Bind error',10,0
  270. ;str5            db 'Setsockopt error.',10,10,0
  271. str6            db 'Could not open socket',10,0
  272. str7            db 'Got data!',10,10,0
  273. str8            db 10,'New thread created!',10,0
  274. str_bye         db 10,'Closing thread!',10,0
  275.  
  276. str_logged_in   db 'Login ok',10,0
  277. str_pass_ok     db 'Password ok',10,0
  278. str_pwd         db 'Current directory is "%s"\n',0
  279. str_err2        db 'ERROR: cannot open directory',10,0
  280. str_datasock    db 'Passive data socket connected!',10,0
  281. str_notfound    db 'ERROR: file not found',10,0
  282. str_sockerr     db 'ERROR: socket error',10,0
  283.  
  284. str_login_invalid db 'Login invalid',10,0
  285.  
  286. str_test db 'test: %x ', 0
  287.  
  288. str_newline     db 10, 0
  289. str_mask        db '*', 0
  290. str_infinity    db 0xff, 0xff, 0xff, 0xff, 0
  291.  
  292. months          dd 'Jan '
  293.                 dd 'Feb '
  294.                 dd 'Mar '
  295.                 dd 'Apr '
  296.                 dd 'May '
  297.                 dd 'Jun '
  298.                 dd 'Jul '
  299.                 dd 'Aug '
  300.                 dd 'Sep '
  301.                 dd 'Oct '
  302.                 dd 'Nov '
  303.                 dd 'Dec '
  304.  
  305. str_users       db 'users'
  306. str_ini         db '.ini', 0
  307. str_port        db 'port', 0
  308. str_ftpd        db 'ftpd', 0
  309. str_conn        db 'conn', 0
  310. str_ip          db 'ip', 0
  311. str_pass        db 'pass', 0
  312. str_home        db 'home', 0
  313. str_mode        db 'mode', 0
  314.  
  315.  
  316. sockaddr1:
  317.                 dw AF_INET4
  318.   .port         dw 21
  319.   .ip           dd 0
  320.                 rb 10
  321.   .length       = $ - sockaddr1
  322.  
  323. ; import
  324.  
  325. align 4
  326. @IMPORT:
  327.  
  328. diff16 "import", 0, $
  329.  
  330. library console,                'console.obj',\
  331.         libini,                 'libini.obj', \
  332.         libio,                  'libio.obj'
  333.  
  334. import  console,\
  335.         con_start,              'START',\
  336.         con_init,               'con_init',\
  337.         con_write_asciiz,       'con_write_asciiz',\
  338.         con_exit,               'con_exit',\
  339.         con_gets,               'con_gets',\
  340.         con_cls,                'con_cls',\
  341.         con_printf,             'con_printf',\
  342.         con_getch2,             'con_getch2',\
  343.         con_set_cursor_pos,     'con_set_cursor_pos',\
  344.         con_set_flags,          'con_set_flags'
  345.  
  346. import  libini,\
  347.         ini.get_str,            'ini_get_str',\
  348.         ini.get_int,            'ini_get_int'
  349.  
  350. import  libio,\
  351.         libio.init,             'lib_init',\
  352.         file.size,              'file_size',\
  353.         file.open,              'file_open',\
  354.         file.read,              'file_read',\
  355.         file.close,             'file_close',\
  356.         file.find.first,        'file_find_first',\
  357.         file.find.next,         'file_find_next',\
  358.         file.find.close,        'file_find_close'
  359.  
  360.  
  361. i_end:
  362.  
  363. diff16 "i_end", 0, $
  364.  
  365. ; uninitialised data
  366.  
  367.         socketnum       dd ?
  368.         path            rb 1024
  369.         path2           rb 1024
  370.         params          rb 1024
  371.         serverip        dd ?
  372.         pasvport        dw ?
  373.  
  374.         ini_buf         rb 3*4+3+1
  375.  
  376. mem:
  377.  
  378.  
  379.