Subversion Repositories Kolibri OS

Rev

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