Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
2892 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2010-2012. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  ftpd.asm - FTP Daemon for KolibriOS                            ;;
7
;;                                                                 ;;
8
;;  Written by hidnplayr@kolibrios.org                             ;;
9
;;                                                                 ;;
10
;;          GNU GENERAL PUBLIC LICENSE                             ;;
11
;;             Version 2, June 1991                                ;;
12
;;                                                                 ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2554 hidnplayr 14
 
2635 hidnplayr 15
DEBUG                   = 0             ; if set to one, program will run in a single thread
16
 
2571 hidnplayr 17
BUFFERSIZE              = 8192
2554 hidnplayr 18
 
2598 hidnplayr 19
; using multiple's of 4
2892 hidnplayr 20
STATE_CONNECTED         = 0*4
21
STATE_LOGIN             = 1*4
22
STATE_LOGIN_FAIL        = 2*4           ; When an invalid username was given
23
STATE_ACTIVE            = 3*4
2554 hidnplayr 24
 
2560 hidnplayr 25
TYPE_UNDEF              = 0
2557 hidnplayr 26
 
2560 hidnplayr 27
TYPE_ASCII              = 00000100b
28
TYPE_EBDIC              = 00001000b
29
; subtypes for ascii & ebdic (np = default)
30
TYPE_NP                 = 00000001b     ; non printable
31
TYPE_TELNET             = 00000010b
32
TYPE_ASA                = 00000011b
33
 
34
TYPE_IMAGE              = 01000000b     ; binary data
35
TYPE_LOCAL              = 10000000b     ; bits per byte must be specified
36
                                        ; lower 4 bits will hold this value
37
MODE_NOTREADY           = 0
38
MODE_ACTIVE             = 1
2562 hidnplayr 39
MODE_PASSIVE_WAIT       = 2
40
MODE_PASSIVE_OK         = 3
2598 hidnplayr 41
MODE_PASSIVE_FAILED     = 4
2560 hidnplayr 42
 
2598 hidnplayr 43
PERMISSION_EXEC         = 1b            ; LIST
44
PERMISSION_READ         = 10b
45
PERMISSION_WRITE        = 100b
46
PERMISSION_DELETE       = 1000b
47
PERMISSION_CD           = 10000b        ; Change Directory
48
 
49
ABORT                   = 1 shl 31
50
 
2581 hidnplayr 51
format binary as ""
52
 
2554 hidnplayr 53
use32
54
        db      'MENUET01'      ; signature
55
        dd      1               ; header version
56
        dd      start           ; entry point
57
        dd      i_end           ; initialized size
58
        dd      mem+0x1000      ; required memory
59
        dd      mem+0x1000      ; stack pointer
2578 hidnplayr 60
        dd      params          ; parameters
2554 hidnplayr 61
        dd      path            ; path
62
 
63
include '../macros.inc'
64
purge mov,add,sub
65
include '../proc32.inc'
66
include '../dll.inc'
2562 hidnplayr 67
include '../struct.inc'
68
include '../libio.inc'
2554 hidnplayr 69
 
70
include '../network.inc'
2635 hidnplayr 71
 
72
macro sendFTP str {
73
local string, length
74
        xor     edi, edi
75
        mcall   send, [ebp + thread_data.socketnum], string, length
76
 
77
iglobal
78
string db str, 13, 10
79
length = $ - string
80
\}
81
}
82
 
2554 hidnplayr 83
include 'commands.inc'
84
 
85
start:
2609 hidnplayr 86
        mcall   68, 11                  ; init heap
87
        mcall   40, 1 shl 7             ; we only want network events
88
 
2554 hidnplayr 89
; load libraries
90
        stdcall dll.Load, @IMPORT
91
        test    eax, eax
92
        jnz     exit
93
 
2598 hidnplayr 94
; find path to main settings file (ftpd.ini)
2578 hidnplayr 95
        mov     edi, path               ; Calculate the length of zero-terminated string
2585 hidnplayr 96
        xor     al, al
2554 hidnplayr 97
        mov     ecx, 1024
98
        repne   scasb
99
        dec     edi
2598 hidnplayr 100
        mov     esi, str_ini            ; append it with '.ini', 0
2554 hidnplayr 101
        movsd
102
        movsb
103
 
2598 hidnplayr 104
; now create the second path (users.ini)
105
        std
106
        mov     al, '/'
107
        repne   scasb
108
        lea     ecx, [edi - path + 2]
109
        cld
110
        mov     esi, path
111
        mov     edi, path2
112
        rep     movsb
113
        mov     esi, str_users
114
        movsd
115
        movsd
116
        movsw
117
 
2554 hidnplayr 118
; initialize console
2602 hidnplayr 119
        invoke  con_start, 1
120
        invoke  con_init, -1, -1, -1, -1, title
2560 hidnplayr 121
 
2598 hidnplayr 122
        invoke  ini.get_str, path, str_ftpd, str_ip, ini_buf, 16, 0
123
        mov     esi, ini_buf
2610 hidnplayr 124
        mov     cl, '.'
2598 hidnplayr 125
        call    ip_to_dword
126
        mov     [serverip], ebx
127
 
2560 hidnplayr 128
        invoke  ini.get_int, path, str_ftpd, str_port, 21
129
        mov     [sockaddr1.port], ax
130
 
2602 hidnplayr 131
        invoke  con_printf, str1, eax
2624 hidnplayr 132
        add     esp, 8
2554 hidnplayr 133
 
134
        mcall   socket, AF_INET4, SOCK_STREAM, 0
135
        cmp     eax, -1
136
        je      sock_err
137
        mov     [socketnum], eax
138
 
2602 hidnplayr 139
        invoke  con_write_asciiz, str2
2560 hidnplayr 140
 
2585 hidnplayr 141
;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
142
;        cmp     eax, -1
143
;        je      opt_err
2554 hidnplayr 144
 
145
        mcall   bind, [socketnum], sockaddr1, sockaddr1.length
146
        cmp     eax, -1
147
        je      bind_err
148
 
2602 hidnplayr 149
        invoke  con_write_asciiz, str2
2560 hidnplayr 150
 
2554 hidnplayr 151
        invoke  ini.get_int, path, str_ftpd, str_conn, 1        ; Backlog (max connections)
152
        mov     edx, eax
2560 hidnplayr 153
 
2602 hidnplayr 154
        invoke  con_write_asciiz, str2
2560 hidnplayr 155
 
2554 hidnplayr 156
        mcall   listen, [socketnum]
157
        cmp     eax, -1
158
        je      listen_err
159
 
2602 hidnplayr 160
        invoke  con_write_asciiz, str2b
2554 hidnplayr 161
 
2610 hidnplayr 162
        invoke  ini.get_int, path, str_pasv, str_start, 2000
163
        mov     [pasv_start], ax
164
        invoke  ini.get_int, path, str_pasv, str_end, 5000
165
        mov     [pasv_end], ax
2609 hidnplayr 166
 
2627 hidnplayr 167
        mov     [alive], 1
168
 
2578 hidnplayr 169
mainloop:
2624 hidnplayr 170
        mcall   23, 100                         ; Wait here for incoming connections on the base socket (socketnum)
2627 hidnplayr 171
                                                ; One second timeout, we will use this to check if console is still working
2554 hidnplayr 172
 
2627 hidnplayr 173
        test    eax, eax                        ; network event?
2624 hidnplayr 174
        jz      .checkconsole
175
 
2635 hidnplayr 176
if DEBUG
177
        jmp     threadstart
178
else
2578 hidnplayr 179
        mcall   51, 1, threadstart, 0           ; Start a new thread for every incoming connection
180
                                                ; NOTE: upon initialisation of the thread, stack will not be available!
2635 hidnplayr 181
end if
2578 hidnplayr 182
        jmp     mainloop
183
 
2624 hidnplayr 184
  .checkconsole:
185
 
186
        invoke  con_get_flags                   ; Is console still running?
187
        test    eax, 0x0200
188
        jz      mainloop
189
        mcall   close, [socketnum]              ; kill the listening socket
2627 hidnplayr 190
        mov     [alive], 0
2624 hidnplayr 191
        mcall   -1                              ; and exit
192
 
2585 hidnplayr 193
        diff16  "threadstart", 0, $
2624 hidnplayr 194
 
2578 hidnplayr 195
threadstart:
2609 hidnplayr 196
;;;        mcall   68, 11                          ; init heap
2578 hidnplayr 197
        mcall   68, 12, sizeof.thread_data      ; allocate the thread data struct
2609 hidnplayr 198
        test    eax, eax
2578 hidnplayr 199
        je      exit
2554 hidnplayr 200
 
2578 hidnplayr 201
        lea     esp, [eax + thread_data.stack]  ; init stack
2609 hidnplayr 202
        mov     ebp, eax
2554 hidnplayr 203
 
2578 hidnplayr 204
        mcall   40, 1 shl 7                     ; we only want network events for this thread
2554 hidnplayr 205
 
2624 hidnplayr 206
        lea     ebx, [ebp + thread_data.buffer] ; get information about the current process
207
        or      ecx, -1
208
        mcall   9
209
        mov     eax, dword [ebp + thread_data.buffer + 30]              ; PID is at offset 30
210
        mov     [ebp + thread_data.pid], eax
211
 
2602 hidnplayr 212
        invoke  con_set_flags, 0x03
2624 hidnplayr 213
        invoke  con_printf, str8, [ebp + thread_data.pid]               ; print on the console that we have created the new thread successfully
214
        add     esp, 8                                                  ; balance stack
2602 hidnplayr 215
        invoke  con_set_flags, 0x07
2578 hidnplayr 216
 
2610 hidnplayr 217
        mcall   accept, [socketnum], sockaddr1, sockaddr1.length        ; time to accept the awaiting connection..
2578 hidnplayr 218
        cmp     eax, -1
219
        je      thread_exit
2609 hidnplayr 220
        mov     [ebp + thread_data.socketnum], eax
2578 hidnplayr 221
 
2635 hidnplayr 222
if DEBUG
223
        mcall   close, [socketnum]                                      ; close the listening socket
224
end if
225
 
2609 hidnplayr 226
        mov     [ebp + thread_data.state], STATE_CONNECTED
227
        mov     [ebp + thread_data.permissions], 0
228
        mov     [ebp + thread_data.mode], MODE_NOTREADY
229
        lea     eax, [ebp + thread_data.buffer]
230
        mov     [ebp + thread_data.buffer_ptr], eax
2610 hidnplayr 231
        mov     [ebp + thread_data.passivesocknum], -1
2578 hidnplayr 232
 
2598 hidnplayr 233
        sendFTP "220 Welcome to KolibriOS FTP daemon"
234
 
2624 hidnplayr 235
        diff16  "threadloop", 0, $
2578 hidnplayr 236
threadloop:
2624 hidnplayr 237
; Check if our socket is still connected
238
        mcall   send, [ebp + thread_data.socketnum], 0, 0       ; Try to send zero bytes, if socket is closed, this will return -1
239
        cmp     eax, -1
240
        je      thread_exit
2578 hidnplayr 241
 
2627 hidnplayr 242
        cmp     [alive], 0                                      ; Did main thread take a run for it?
243
        je      thread_exit
244
 
2624 hidnplayr 245
        mcall   10                                              ; Wait for network event
246
 
2609 hidnplayr 247
        cmp     [ebp + thread_data.mode], MODE_PASSIVE_WAIT
2598 hidnplayr 248
        jne     .not_passive
2609 hidnplayr 249
        mov     ecx, [ebp + thread_data.passivesocknum]
250
        lea     edx, [ebp + thread_data.datasock]
2578 hidnplayr 251
        mov     esi, sizeof.thread_data.datasock
252
        mcall   accept
2562 hidnplayr 253
        cmp     eax, -1
2598 hidnplayr 254
        je      .not_passive
2609 hidnplayr 255
        mov     [ebp + thread_data.datasocketnum], eax
256
        mov     [ebp + thread_data.mode], MODE_PASSIVE_OK
2610 hidnplayr 257
        mcall   close   ; [ebp + thread_data.passivesocknum]
258
        mov     [ebp + thread_data.passivesocknum], -1
2562 hidnplayr 259
 
2602 hidnplayr 260
        invoke  con_write_asciiz, str_datasock
2598 hidnplayr 261
  .not_passive:
2562 hidnplayr 262
 
2609 hidnplayr 263
        mov     ecx, [ebp + thread_data.socketnum]
264
        mov     edx, [ebp + thread_data.buffer_ptr]
2598 hidnplayr 265
        mov     esi, sizeof.thread_data.buffer    ;;; FIXME
2578 hidnplayr 266
        mcall   recv
2610 hidnplayr 267
        inc     eax                                     ; error? (-1)
2578 hidnplayr 268
        jz      threadloop
2610 hidnplayr 269
        dec     eax                                     ; 0 bytes read?
2598 hidnplayr 270
        jz      threadloop
2554 hidnplayr 271
 
2609 hidnplayr 272
        mov     edi, [ebp + thread_data.buffer_ptr]
273
        add     [ebp + thread_data.buffer_ptr], eax
2560 hidnplayr 274
 
2598 hidnplayr 275
; Check if we received a newline character, if not, wait for more data
276
        mov     ecx, eax
277
        mov     al, 13
278
        repne   scasb
279
        jne     threadloop
280
 
281
; We got a command!
2610 hidnplayr 282
        mov     byte [edi + 1], 0                       ; append string with zero byte
2609 hidnplayr 283
        lea     esi, [ebp + thread_data.buffer]
284
        mov     ecx, [ebp + thread_data.buffer_ptr]
2602 hidnplayr 285
        sub     ecx, esi
2610 hidnplayr 286
        mov     [ebp + thread_data.buffer_ptr], esi     ; reset buffer ptr
2598 hidnplayr 287
 
2610 hidnplayr 288
        invoke  con_set_flags, 0x02                     ; print received data to console (in green color)
2602 hidnplayr 289
        invoke  con_write_asciiz, str_newline
290
        invoke  con_write_asciiz, esi
291
        invoke  con_set_flags, 0x07
2560 hidnplayr 292
 
2602 hidnplayr 293
        push    threadloop
294
        jmp     parse_cmd
2554 hidnplayr 295
 
296
listen_err:
2610 hidnplayr 297
        invoke  con_set_flags, 0x0c                     ; print errors in red
2602 hidnplayr 298
        invoke  con_write_asciiz, str3
2554 hidnplayr 299
        jmp     done
300
 
301
bind_err:
2610 hidnplayr 302
        invoke  con_set_flags, 0x0c                     ; print errors in red
2602 hidnplayr 303
        invoke  con_write_asciiz, str4
2554 hidnplayr 304
        jmp     done
305
 
306
sock_err:
2610 hidnplayr 307
        invoke  con_set_flags, 0x0c                     ; print errors in red
2602 hidnplayr 308
        invoke  con_write_asciiz, str6
2554 hidnplayr 309
        jmp     done
310
 
311
done:
2624 hidnplayr 312
        invoke  con_exit, 0
2554 hidnplayr 313
exit:
314
        mcall   -1
315
 
316
 
2578 hidnplayr 317
thread_exit:
2624 hidnplayr 318
        invoke  con_set_flags, 0x03                             ; print thread info in blue
319
        invoke  con_printf, str_bye, [ebp + thread_data.pid]    ; print on the console that we are about to kill the thread
320
        add     esp, 8                                          ; balance stack
321
        mcall   68, 13, ebp                                     ; free the memory
322
        mcall   -1                                              ; and kill the thread
2554 hidnplayr 323
 
324
 
2578 hidnplayr 325
; initialized data
326
 
327
title           db 'KolibriOS FTP daemon 0.1', 0
2624 hidnplayr 328
str1            db 'Starting FTP daemon on port %u.', 0
2578 hidnplayr 329
str2            db '.', 0
2585 hidnplayr 330
str2b           db ' OK!',10,0
331
str3            db 'Listen error',10,0
2624 hidnplayr 332
str4            db 10,'ERROR: local port is already in use.',10,0
2578 hidnplayr 333
;str5            db 'Setsockopt error.',10,10,0
2624 hidnplayr 334
str6            db 'ERROR: Could not open socket.',10,0
2578 hidnplayr 335
str7            db 'Got data!',10,10,0
2624 hidnplayr 336
str8            db 10,'Thread %d created',10,0
337
str_bye         db 10,'Thread %d killed',10,0
2578 hidnplayr 338
 
2585 hidnplayr 339
str_logged_in   db 'Login ok',10,0
2602 hidnplayr 340
str_pass_ok     db 'Password ok',10,0
2610 hidnplayr 341
str_pass_err    db 'Password/Username incorrect',10,0
2562 hidnplayr 342
str_pwd         db 'Current directory is "%s"\n',0
2624 hidnplayr 343
str_err2        db 'ERROR: cannot open the directory.',10,0
344
str_datasock    db 'Passive data socket connected.',10,0
345
str_notfound    db 'ERROR: file not found.',10,0
346
str_sockerr     db 'ERROR: socket error.',10,0
2560 hidnplayr 347
 
2578 hidnplayr 348
str_newline     db 10, 0
2562 hidnplayr 349
str_mask        db '*', 0
2602 hidnplayr 350
str_infinity    db 0xff, 0xff, 0xff, 0xff, 0
2562 hidnplayr 351
 
2578 hidnplayr 352
months          dd 'Jan '
353
                dd 'Feb '
354
                dd 'Mar '
355
                dd 'Apr '
356
                dd 'May '
357
                dd 'Jun '
358
                dd 'Jul '
359
                dd 'Aug '
360
                dd 'Sep '
361
                dd 'Oct '
362
                dd 'Nov '
363
                dd 'Dec '
2562 hidnplayr 364
 
2598 hidnplayr 365
str_users       db 'users'
366
str_ini         db '.ini', 0
2578 hidnplayr 367
str_port        db 'port', 0
368
str_ftpd        db 'ftpd', 0
369
str_conn        db 'conn', 0
2598 hidnplayr 370
str_ip          db 'ip', 0
371
str_pass        db 'pass', 0
372
str_home        db 'home', 0
373
str_mode        db 'mode', 0
2610 hidnplayr 374
str_pasv        db 'pasv', 0
375
str_start       db 'start', 0
376
str_end         db 'end', 0
2562 hidnplayr 377
 
2598 hidnplayr 378
 
2554 hidnplayr 379
sockaddr1:
2578 hidnplayr 380
                dw AF_INET4
381
  .port         dw 21
382
  .ip           dd 0
383
                rb 10
384
  .length       = $ - sockaddr1
2554 hidnplayr 385
 
386
; import
2578 hidnplayr 387
 
2554 hidnplayr 388
align 4
389
@IMPORT:
390
 
2609 hidnplayr 391
diff16 "import", 0, $
392
 
2578 hidnplayr 393
library console,                'console.obj',\
394
        libini,                 'libini.obj', \
395
        libio,                  'libio.obj'
2554 hidnplayr 396
 
2578 hidnplayr 397
import  console,\
398
        con_start,              'START',\
399
        con_init,               'con_init',\
400
        con_write_asciiz,       'con_write_asciiz',\
401
        con_exit,               'con_exit',\
402
        con_gets,               'con_gets',\
403
        con_cls,                'con_cls',\
404
        con_printf,             'con_printf',\
405
        con_getch2,             'con_getch2',\
406
        con_set_cursor_pos,     'con_set_cursor_pos',\
2624 hidnplayr 407
        con_set_flags,          'con_set_flags',\
408
        con_get_flags,          'con_get_flags'
2554 hidnplayr 409
 
2578 hidnplayr 410
import  libini,\
411
        ini.get_str,            'ini_get_str',\
412
        ini.get_int,            'ini_get_int'
2554 hidnplayr 413
 
2578 hidnplayr 414
import  libio,\
415
        file.size,              'file_size',\
416
        file.open,              'file_open',\
417
        file.read,              'file_read',\
418
        file.close,             'file_close',\
419
        file.find.first,        'file_find_first',\
420
        file.find.next,         'file_find_next',\
421
        file.find.close,        'file_find_close'
2554 hidnplayr 422
 
423
 
2635 hidnplayr 424
IncludeIGlobals
425
 
426
 
2554 hidnplayr 427
i_end:
428
 
2609 hidnplayr 429
diff16 "i_end", 0, $
430
 
2578 hidnplayr 431
; uninitialised data
2557 hidnplayr 432
 
2578 hidnplayr 433
        socketnum       dd ?
434
        path            rb 1024
2598 hidnplayr 435
        path2           rb 1024
2578 hidnplayr 436
        params          rb 1024
2598 hidnplayr 437
        serverip        dd ?
2610 hidnplayr 438
        pasv_start      dw ?
439
        pasv_end        dw ?
440
        pasv_port       dw ?
2557 hidnplayr 441
 
2598 hidnplayr 442
        ini_buf         rb 3*4+3+1
443
 
2627 hidnplayr 444
        alive           db ?
445
 
2578 hidnplayr 446
mem:
2563 hidnplayr 447