Subversion Repositories Kolibri OS

Rev

Rev 2598 | 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. ; load libraries
  68.         stdcall dll.Load, @IMPORT
  69.         test    eax, eax
  70.         jnz     exit
  71.  
  72.         mcall   68, 11                  ; init heap
  73.  
  74. ; find path to main settings file (ftpd.ini)
  75.         mov     edi, path               ; Calculate the length of zero-terminated string
  76.         xor     al, al
  77.         mov     ecx, 1024
  78.         repne   scasb
  79.         dec     edi
  80.         mov     esi, str_ini            ; append it with '.ini', 0
  81.         movsd
  82.         movsb
  83.  
  84. ; now create the second path (users.ini)
  85.         std
  86.         mov     al, '/'
  87.         repne   scasb
  88.         lea     ecx, [edi - path + 2]
  89.         cld
  90.         mov     esi, path
  91.         mov     edi, path2
  92.         rep     movsb
  93.         mov     esi, str_users
  94.         movsd
  95.         movsd
  96.         movsw
  97.  
  98. ; initialize console
  99.         invoke  con_start, 1
  100.         invoke  con_init, -1, -1, -1, -1, title
  101.  
  102.         mcall   40, 1 shl 7             ; we only want network events
  103.  
  104.         invoke  ini.get_str, path, str_ftpd, str_ip, ini_buf, 16, 0
  105.         mov     esi, ini_buf
  106.         call    ip_to_dword
  107.         mov     [serverip], ebx
  108.  
  109.         invoke  ini.get_int, path, str_ftpd, str_port, 21
  110.         mov     [sockaddr1.port], ax
  111.  
  112.         invoke  con_printf, str1, eax
  113.  
  114.         mcall   socket, AF_INET4, SOCK_STREAM, 0
  115.         cmp     eax, -1
  116.         je      sock_err
  117.         mov     [socketnum], eax
  118.  
  119.         invoke  con_write_asciiz, str2
  120.  
  121. ;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
  122. ;        cmp     eax, -1
  123. ;        je      opt_err
  124.  
  125.         mcall   bind, [socketnum], sockaddr1, sockaddr1.length
  126.         cmp     eax, -1
  127.         je      bind_err
  128.  
  129.         invoke  con_write_asciiz, str2
  130.  
  131.         invoke  ini.get_int, path, str_ftpd, str_conn, 1        ; Backlog (max connections)
  132.         mov     edx, eax
  133.  
  134.         invoke  con_write_asciiz, str2
  135.  
  136.         mcall   listen, [socketnum]
  137.         cmp     eax, -1
  138.         je      listen_err
  139.  
  140.         invoke  con_write_asciiz, str2b
  141.  
  142. mainloop:
  143.         mcall   10                              ; Wait here for incoming connections on the base socket (socketnum)
  144.  
  145.         mcall   51, 1, threadstart, 0           ; Start a new thread for every incoming connection
  146.                                                 ; NOTE: upon initialisation of the thread, stack will not be available!
  147.         jmp     mainloop
  148.  
  149.         diff16  "threadstart", 0, $
  150. threadstart:
  151.         mcall   68, 12, sizeof.thread_data      ; allocate the thread data struct
  152.         cmp     eax, -1
  153.         je      exit
  154.  
  155.         lea     esp, [eax + thread_data.stack]  ; init stack
  156.         push    eax                             ; save pointer to thread_data on stack
  157.         mov     ebp, esp
  158.  
  159.         mcall   40, 1 shl 7                     ; we only want network events for this thread
  160.  
  161.         invoke  con_set_flags, 0x03
  162.         invoke  con_write_asciiz, str8          ; print on the console that we have created the new thread successfully
  163.         invoke  con_set_flags, 0x07
  164.  
  165.         mcall   accept, [socketnum], sockaddr1, sockaddr1.length                ; time to accept the awaiting connection..
  166.         cmp     eax, -1
  167.         je      thread_exit
  168.         mov     edx, [ebp]                                                      ; pointer to thread_data
  169.         mov     [edx + thread_data.socketnum], eax
  170.  
  171.         mov     [edx + thread_data.state], STATE_CONNECTED
  172.         mov     [edx + thread_data.permissions], 0
  173.         mov     [edx + thread_data.mode], MODE_NOTREADY
  174.         lea     eax, [edx + thread_data.buffer]
  175.         mov     [edx + 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     [edx + thread_data.mode], MODE_PASSIVE_WAIT
  184.         jne     .not_passive
  185.         mov     [edx + thread_data.mode], MODE_PASSIVE_FAILED                   ; assume that we will fail
  186.         mov     ecx, [edx + thread_data.passivesocknum]
  187.         lea     edx, [edx + thread_data.datasock]
  188.         mov     esi, sizeof.thread_data.datasock
  189.         mcall   accept
  190.         mov     edx, [ebp]                                                      ; pointer to thread_data
  191.         cmp     eax, -1
  192.         je      .not_passive
  193.         mov     [edx + thread_data.datasocketnum], eax
  194.         mov     [edx + thread_data.mode], MODE_PASSIVE_OK
  195.  
  196.         invoke  con_write_asciiz, str_datasock
  197.   .not_passive:
  198.  
  199.         mov     ecx, [edx + thread_data.socketnum]
  200.         mov     edx, [edx + thread_data.buffer_ptr]
  201.         mov     esi, sizeof.thread_data.buffer    ;;; FIXME
  202.         mcall   recv
  203.         inc     eax                                                             ; error? (-1)
  204.         jz      threadloop
  205.         dec     eax                                                             ; 0 bytes read?
  206.         jz      threadloop
  207.  
  208.         mov     edx, [ebp]                                                      ; pointer to thread_data
  209.         mov     edi, [edx + thread_data.buffer_ptr]
  210.         add     [edx + thread_data.buffer_ptr], eax
  211.  
  212. ; Check if we received a newline character, if not, wait for more data
  213.         mov     ecx, eax
  214.         mov     al, 13
  215.         repne   scasb
  216.         jne     threadloop
  217.  
  218. ; We got a command!
  219.         mov     byte [edi + 1], 0                                               ; append string with zero byte
  220.         lea     esi, [edx + thread_data.buffer]
  221.         mov     ecx, [edx + thread_data.buffer_ptr]
  222.         sub     ecx, esi
  223.         mov     [edx + thread_data.buffer_ptr], esi                             ; reset buffer ptr
  224.  
  225.         invoke  con_set_flags, 0x02                                                            ; print received data to console (in green color)
  226.         invoke  con_write_asciiz, str_newline
  227.         invoke  con_write_asciiz, esi
  228.         invoke  con_set_flags, 0x07
  229.  
  230.         push    threadloop
  231.         jmp     parse_cmd
  232.  
  233. listen_err:
  234.         invoke  con_set_flags, 0x0c                                                            ; print received data to console (in green color)
  235.         invoke  con_write_asciiz, str3
  236.         jmp     done
  237.  
  238. bind_err:
  239.         invoke  con_set_flags, 0x0c                                                            ; print received data to console (in green color)
  240.         invoke  con_write_asciiz, str4
  241.         jmp     done
  242.  
  243. sock_err:
  244.         invoke  con_set_flags, 0x0c                                                            ; print received data to console (in green color)
  245.         invoke  con_write_asciiz, str6
  246.         jmp     done
  247.  
  248. done:
  249.         invoke  con_getch2
  250.         invoke  con_exit, 1
  251. exit:
  252.         mcall   -1
  253.  
  254.  
  255. thread_exit:
  256.         invoke  con_set_flags, 0x02                                                            ; print received data to console (in green color)
  257.         invoke  con_write_asciiz, str_bye
  258.         pop     ecx                     ; get the thread_data pointer from stack
  259.         mcall   68, 13                  ; free the memory
  260.         mcall   -1                      ; and kill the thread
  261.  
  262.  
  263.  
  264. ; initialized data
  265.  
  266. title           db 'KolibriOS FTP daemon 0.1', 0
  267. str1            db 'Starting FTP daemon on port %u', 0
  268. str2            db '.', 0
  269. str2b           db ' OK!',10,0
  270. str3            db 'Listen error',10,0
  271. str4            db 'Bind error',10,0
  272. ;str5            db 'Setsockopt error.',10,10,0
  273. str6            db 'Could not open socket',10,0
  274. str7            db 'Got data!',10,10,0
  275. str8            db 10,'New thread created!',10,0
  276. str_bye         db 10,'Closing thread!',10,0
  277.  
  278. str_logged_in   db 'Login ok',10,0
  279. str_pass_ok     db 'Password ok',10,0
  280. str_pwd         db 'Current directory is "%s"\n',0
  281. str_err2        db 'ERROR: cannot open directory',10,0
  282. str_datasock    db 'Passive data socket connected!',10,0
  283. str_notfound    db 'ERROR: file not found',10,0
  284. str_sockerr     db 'ERROR: socket error',10,0
  285.  
  286. str_login_invalid db 'Login invalid',10,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. library console,                'console.obj',\
  329.         libini,                 'libini.obj', \
  330.         libio,                  'libio.obj'
  331.  
  332. import  console,\
  333.         con_start,              'START',\
  334.         con_init,               'con_init',\
  335.         con_write_asciiz,       'con_write_asciiz',\
  336.         con_exit,               'con_exit',\
  337.         con_gets,               'con_gets',\
  338.         con_cls,                'con_cls',\
  339.         con_printf,             'con_printf',\
  340.         con_getch2,             'con_getch2',\
  341.         con_set_cursor_pos,     'con_set_cursor_pos',\
  342.         con_set_flags,          'con_set_flags'
  343.  
  344. import  libini,\
  345.         ini.get_str,            'ini_get_str',\
  346.         ini.get_int,            'ini_get_int'
  347.  
  348. import  libio,\
  349.         libio.init,             'lib_init',\
  350.         file.size,              'file_size',\
  351.         file.open,              'file_open',\
  352.         file.read,              'file_read',\
  353.         file.close,             'file_close',\
  354.         file.find.first,        'file_find_first',\
  355.         file.find.next,         'file_find_next',\
  356.         file.find.close,        'file_find_close'
  357.  
  358.  
  359. i_end:
  360.  
  361. ; uninitialised data
  362.  
  363.         socketnum       dd ?
  364.         path            rb 1024
  365.         path2           rb 1024
  366.         params          rb 1024
  367.         serverip        dd ?
  368.  
  369.         ini_buf         rb 3*4+3+1
  370.  
  371. mem:
  372.  
  373.  
  374.