Subversion Repositories Kolibri OS

Rev

Rev 2627 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2554 hidnplayr 1
;
2
; Kolibrios FTP Daemon
3
;
4
; hidnplayr@gmail.com
5
;
6
; GPLv2
7
;
8
 
2635 hidnplayr 9
DEBUG                   = 0             ; if set to one, program will run in a single thread
10
 
2571 hidnplayr 11
BUFFERSIZE              = 8192
2554 hidnplayr 12
 
2598 hidnplayr 13
; using multiple's of 4
14
STATE_CONNECTED         = 4*0
15
STATE_LOGIN             = 4*1
16
STATE_LOGIN_FAIL        = 4*2           ; When an invalid username was given
17
STATE_ACTIVE            = 4*3
2554 hidnplayr 18
 
2560 hidnplayr 19
TYPE_UNDEF              = 0
2557 hidnplayr 20
 
2560 hidnplayr 21
TYPE_ASCII              = 00000100b
22
TYPE_EBDIC              = 00001000b
23
; subtypes for ascii & ebdic (np = default)
24
TYPE_NP                 = 00000001b     ; non printable
25
TYPE_TELNET             = 00000010b
26
TYPE_ASA                = 00000011b
27
 
28
TYPE_IMAGE              = 01000000b     ; binary data
29
TYPE_LOCAL              = 10000000b     ; bits per byte must be specified
30
                                        ; lower 4 bits will hold this value
31
MODE_NOTREADY           = 0
32
MODE_ACTIVE             = 1
2562 hidnplayr 33
MODE_PASSIVE_WAIT       = 2
34
MODE_PASSIVE_OK         = 3
2598 hidnplayr 35
MODE_PASSIVE_FAILED     = 4
2560 hidnplayr 36
 
2598 hidnplayr 37
PERMISSION_EXEC         = 1b            ; LIST
38
PERMISSION_READ         = 10b
39
PERMISSION_WRITE        = 100b
40
PERMISSION_DELETE       = 1000b
41
PERMISSION_CD           = 10000b        ; Change Directory
42
 
43
ABORT                   = 1 shl 31
44
 
2581 hidnplayr 45
format binary as ""
46
 
2554 hidnplayr 47
use32
48
        db      'MENUET01'      ; signature
49
        dd      1               ; header version
50
        dd      start           ; entry point
51
        dd      i_end           ; initialized size
52
        dd      mem+0x1000      ; required memory
53
        dd      mem+0x1000      ; stack pointer
2578 hidnplayr 54
        dd      params          ; parameters
2554 hidnplayr 55
        dd      path            ; path
56
 
57
include '../macros.inc'
58
purge mov,add,sub
59
include '../proc32.inc'
60
include '../dll.inc'
2562 hidnplayr 61
include '../struct.inc'
62
include '../libio.inc'
2554 hidnplayr 63
 
64
include '../network.inc'
2635 hidnplayr 65
 
66
macro sendFTP str {
67
local string, length
68
        xor     edi, edi
69
        mcall   send, [ebp + thread_data.socketnum], string, length
70
 
71
iglobal
72
string db str, 13, 10
73
length = $ - string
74
\}
75
}
76
 
2554 hidnplayr 77
include 'commands.inc'
78
 
79
start:
2609 hidnplayr 80
        mcall   68, 11                  ; init heap
81
        mcall   40, 1 shl 7             ; we only want network events
82
 
2554 hidnplayr 83
; load libraries
84
        stdcall dll.Load, @IMPORT
85
        test    eax, eax
86
        jnz     exit
87
 
2598 hidnplayr 88
; find path to main settings file (ftpd.ini)
2578 hidnplayr 89
        mov     edi, path               ; Calculate the length of zero-terminated string
2585 hidnplayr 90
        xor     al, al
2554 hidnplayr 91
        mov     ecx, 1024
92
        repne   scasb
93
        dec     edi
2598 hidnplayr 94
        mov     esi, str_ini            ; append it with '.ini', 0
2554 hidnplayr 95
        movsd
96
        movsb
97
 
2598 hidnplayr 98
; now create the second path (users.ini)
99
        std
100
        mov     al, '/'
101
        repne   scasb
102
        lea     ecx, [edi - path + 2]
103
        cld
104
        mov     esi, path
105
        mov     edi, path2
106
        rep     movsb
107
        mov     esi, str_users
108
        movsd
109
        movsd
110
        movsw
111
 
2554 hidnplayr 112
; initialize console
2602 hidnplayr 113
        invoke  con_start, 1
114
        invoke  con_init, -1, -1, -1, -1, title
2560 hidnplayr 115
 
2598 hidnplayr 116
        invoke  ini.get_str, path, str_ftpd, str_ip, ini_buf, 16, 0
117
        mov     esi, ini_buf
2610 hidnplayr 118
        mov     cl, '.'
2598 hidnplayr 119
        call    ip_to_dword
120
        mov     [serverip], ebx
121
 
2560 hidnplayr 122
        invoke  ini.get_int, path, str_ftpd, str_port, 21
123
        mov     [sockaddr1.port], ax
124
 
2602 hidnplayr 125
        invoke  con_printf, str1, eax
2624 hidnplayr 126
        add     esp, 8
2554 hidnplayr 127
 
128
        mcall   socket, AF_INET4, SOCK_STREAM, 0
129
        cmp     eax, -1
130
        je      sock_err
131
        mov     [socketnum], eax
132
 
2602 hidnplayr 133
        invoke  con_write_asciiz, str2
2560 hidnplayr 134
 
2585 hidnplayr 135
;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
136
;        cmp     eax, -1
137
;        je      opt_err
2554 hidnplayr 138
 
139
        mcall   bind, [socketnum], sockaddr1, sockaddr1.length
140
        cmp     eax, -1
141
        je      bind_err
142
 
2602 hidnplayr 143
        invoke  con_write_asciiz, str2
2560 hidnplayr 144
 
2554 hidnplayr 145
        invoke  ini.get_int, path, str_ftpd, str_conn, 1        ; Backlog (max connections)
146
        mov     edx, eax
2560 hidnplayr 147
 
2602 hidnplayr 148
        invoke  con_write_asciiz, str2
2560 hidnplayr 149
 
2554 hidnplayr 150
        mcall   listen, [socketnum]
151
        cmp     eax, -1
152
        je      listen_err
153
 
2602 hidnplayr 154
        invoke  con_write_asciiz, str2b
2554 hidnplayr 155
 
2610 hidnplayr 156
        invoke  ini.get_int, path, str_pasv, str_start, 2000
157
        mov     [pasv_start], ax
158
        invoke  ini.get_int, path, str_pasv, str_end, 5000
159
        mov     [pasv_end], ax
2609 hidnplayr 160
 
2627 hidnplayr 161
        mov     [alive], 1
162
 
2578 hidnplayr 163
mainloop:
2624 hidnplayr 164
        mcall   23, 100                         ; Wait here for incoming connections on the base socket (socketnum)
2627 hidnplayr 165
                                                ; One second timeout, we will use this to check if console is still working
2554 hidnplayr 166
 
2627 hidnplayr 167
        test    eax, eax                        ; network event?
2624 hidnplayr 168
        jz      .checkconsole
169
 
2635 hidnplayr 170
if DEBUG
171
        jmp     threadstart
172
else
2578 hidnplayr 173
        mcall   51, 1, threadstart, 0           ; Start a new thread for every incoming connection
174
                                                ; NOTE: upon initialisation of the thread, stack will not be available!
2635 hidnplayr 175
end if
2578 hidnplayr 176
        jmp     mainloop
177
 
2624 hidnplayr 178
  .checkconsole:
179
 
180
        invoke  con_get_flags                   ; Is console still running?
181
        test    eax, 0x0200
182
        jz      mainloop
183
        mcall   close, [socketnum]              ; kill the listening socket
2627 hidnplayr 184
        mov     [alive], 0
2624 hidnplayr 185
        mcall   -1                              ; and exit
186
 
2585 hidnplayr 187
        diff16  "threadstart", 0, $
2624 hidnplayr 188
 
2578 hidnplayr 189
threadstart:
2609 hidnplayr 190
;;;        mcall   68, 11                          ; init heap
2578 hidnplayr 191
        mcall   68, 12, sizeof.thread_data      ; allocate the thread data struct
2609 hidnplayr 192
        test    eax, eax
2578 hidnplayr 193
        je      exit
2554 hidnplayr 194
 
2578 hidnplayr 195
        lea     esp, [eax + thread_data.stack]  ; init stack
2609 hidnplayr 196
        mov     ebp, eax