-
Notifications
You must be signed in to change notification settings - Fork 54
/
lftpd.c
677 lines (564 loc) · 16.3 KB
/
lftpd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <kos/net.h>
#include "lftpd.h"
#include "private/lftpd_status.h"
#include "private/lftpd_inet.h"
#include "private/lftpd_log.h"
#include "private/lftpd_string.h"
#include "private/lftpd_io.h"
// https://tools.ietf.org/html/rfc959
// https://tools.ietf.org/html/rfc2389#section-2.2
// https://tools.ietf.org/html/rfc3659
// https://tools.ietf.org/html/rfc5797
// https://tools.ietf.org/html/rfc2428#section-3 EPSV
// https://en.wikipedia.org/wiki/List_of_FTP_commands
#define FILE_BUFFER_SIZE ((64 * 1024) - 512)
typedef struct {
char *command;
int (*handler) (lftpd_client_t* client, const char* arg);
} command_t;
static int cmd_cwd();
static int cmd_dele();
static int cmd_epsv();
static int cmd_feat();
static int cmd_list();
static int cmd_mkd();
static int cmd_nlst();
static int cmd_noop();
static int cmd_pass();
static int cmd_pasv();
static int cmd_pwd();
static int cmd_quit();
static int cmd_rmd();
static int cmd_retr();
static int cmd_size();
static int cmd_stor();
static int cmd_syst();
static int cmd_type();
static int cmd_user();
static command_t commands[] = {
{ "CWD", cmd_cwd },
{ "DELE", cmd_dele },
{ "EPSV", cmd_epsv },
{ "FEAT", cmd_feat },
{ "LIST", cmd_list },
{ "MKD", cmd_mkd },
{ "NLST", cmd_nlst },
{ "NOOP", cmd_noop },
{ "PASS", cmd_pass },
{ "PASV", cmd_pasv },
{ "PWD", cmd_pwd },
{ "QUIT", cmd_quit },
{ "RMD", cmd_rmd },
{ "RETR", cmd_retr },
{ "SIZE", cmd_size },
{ "STOR", cmd_stor },
{ "SYST", cmd_syst },
{ "TYPE", cmd_type },
{ "USER", cmd_user },
{ NULL, NULL },
};
static int send_response(int socket, int code, bool include_code,
bool multiline_start, const char* format, ...) {
va_list args;
va_start(args, format);
char* message = NULL;
int err = vasprintf(&message, format, args);
va_end(args);
if (err < 0) {
return -1;
}
char* response = NULL;
if (include_code) {
if (multiline_start) {
err = asprintf(&response, "%d-%s%s", code, message, CRLF);
}
else {
err = asprintf(&response, "%d %s%s", code, message, CRLF);
}
}
else {
err = asprintf(&response, "%s%s", message, CRLF);
}
free(message);
if (err < 0) {
return -1;
}
err = lftpd_inet_write_string(socket, response);
free(response);
return err;
}
#define send_simple_response(socket, code, format, ...) send_response(socket, code, true, false, format, ##__VA_ARGS__)
#define send_multiline_response_begin(socket, code, format, ...) send_response(socket, code, true, true, format, ##__VA_ARGS__)
#define send_multiline_response_line(socket, format, ...) send_response(socket, 0, false, false, format, ##__VA_ARGS__)
#define send_multiline_response_end(socket, code, format, ...) send_response(socket, code, true, false, format, ##__VA_ARGS__)
static int send_list(int socket, const char* path) {
// https://files.stairways.com/other/ftp-list-specs-info.txt
// http://cr.yp.to/ftp/list/binls.html
static const char* directory_format = "drw-rw-rw- 1 dream shell %lu Nov 27 1998 %s";
static const char* file_format = "-rw-rw-rw- 1 dream shell %lu Nov 27 1998 %s";
dirent_t *entry;
file_t fd = fs_open(path, O_RDONLY | O_DIR);
if (fd < 0) {
return -1;
}
while ((entry = fs_readdir(fd))) {
if (entry->attr == O_DIR) {
send_multiline_response_line(socket, directory_format, 0, entry->name);
}
else {
send_multiline_response_line(socket, file_format, entry->size, entry->name);
}
}
fs_close(fd);
return 0;
}
static int send_nlst(int socket, const char* path) {
file_t fd = fs_open(path, O_RDONLY | O_DIR);
if (fd < 0) {
return -1;
}
dirent_t *entry;
while ((entry = fs_readdir(fd))) {
send_multiline_response_line(socket, entry->name);
}
fs_close(fd);
return 0;
}
static int send_file(int socket, const char* path) {
uint8_t *p;
uint8_t *buffer;
int read_len, write_len, rv = -1;
file_t fd = fs_open(path, O_RDONLY);
if (fd < 0) {
lftpd_log_error("failed to open file for read");
return rv;
}
buffer = (uint8_t *)memalign(32, FILE_BUFFER_SIZE);
if (!buffer) {
lftpd_log_error("no free memory");
return rv;
}
rv = 0;
while ((read_len = fs_read(fd, buffer, FILE_BUFFER_SIZE)) > 0) {
p = buffer;
while (read_len > 0) {
write_len = write(socket, p, read_len);
if (write_len < 0) {
lftpd_log_error("write error");
rv = -1;
break;
}
p += write_len;
read_len -= write_len;
}
}
fs_close(fd);
free(buffer);
return rv;
}
static int receive_file(int socket, const char* path) {
uint8_t *buffer;
int read_len, rv = -1;
file_t fd = fs_open(path, O_WRONLY | O_TRUNC | O_CREAT);
if (fd < 0) {
lftpd_log_error("failed to open file for read");
return rv;
}
buffer = (uint8_t *)memalign(32, FILE_BUFFER_SIZE);
if (!buffer) {
lftpd_log_error("no free memory");
return rv;
}
rv = 0;
while ((read_len = read(socket, buffer, FILE_BUFFER_SIZE)) > 0) {
if (fs_write(fd, buffer, read_len) < 0) {
lftpd_log_error("write error");
rv = -1;
break;
}
}
fs_close(fd);
free(buffer);
return rv;
}
static int cmd_cwd(lftpd_client_t* client, const char* arg) {
if (arg == NULL || strlen(arg) == 0) {
send_simple_response(client->socket, 550, STATUS_550);
}
char* path = lftpd_io_canonicalize_path(client->directory, arg);
file_t fd = fs_open(path, O_RDONLY | O_DIR);
if (fd < 0) {
send_simple_response(client->socket, 550, STATUS_550);
free(path);
return 0;
}
fs_close(fd);
free(client->directory);
client->directory = path;
send_simple_response(client->socket, 250, STATUS_250);
return 0;
}
static int cmd_dele(lftpd_client_t* client, const char* arg) {
if (arg == NULL || strlen(arg) == 0) {
send_simple_response(client->socket, 550, STATUS_550);
}
char* path = lftpd_io_canonicalize_path(client->directory, arg);
if (fs_unlink(path) < 0) {
send_simple_response(client->socket, 550, STATUS_550);
}
else {
send_simple_response(client->socket, 250, STATUS_250);
}
free(path);
return 0;
}
static int cmd_epsv(lftpd_client_t* client, const char* arg) {
// open a data port
int listener_socket = lftpd_inet_listen(0);
if (listener_socket < 0) {
send_simple_response(client->socket, 425, STATUS_425);
return -1;
}
// get the port from the new socket, which is random
int port = lftpd_inet_get_socket_port(listener_socket);
// format the response
send_simple_response(client->socket, 229, STATUS_229, port);
// wait for the connection to the data port
lftpd_log_debug("waiting for data port connection on port %d...", port);
int client_socket = accept(listener_socket, NULL, NULL);
if (client_socket < 0) {
lftpd_log_error("error accepting client socket");
close(listener_socket);
return -1;
}
lftpd_log_debug("data port connection received...");
// close the listener
close(listener_socket);
uint32_t new_buf_sz = FILE_BUFFER_SIZE;
setsockopt(client_socket, SOL_SOCKET, SO_SNDBUF, &new_buf_sz, sizeof(new_buf_sz));
setsockopt(client_socket, SOL_SOCKET, SO_RCVBUF, &new_buf_sz, sizeof(new_buf_sz));
client->data_socket = client_socket;
return 0;
}
static int cmd_feat(lftpd_client_t* client, const char* arg) {
send_multiline_response_begin(client->socket, 211, STATUS_211);
send_multiline_response_line(client->socket, "EPSV");
send_multiline_response_line(client->socket, "PASV");
send_multiline_response_line(client->socket, "SIZE");
send_multiline_response_line(client->socket, "NLST");
send_multiline_response_end(client->socket, 211, STATUS_211);
return 0;
}
static int cmd_list(lftpd_client_t* client, const char* arg) {
if (client->data_socket == -1) {
send_simple_response(client->socket, 425, STATUS_425);
return -1;
}
send_simple_response(client->socket, 150, STATUS_150);
int err = send_list(client->data_socket, client->directory);
close(client->data_socket);
client->data_socket = -1;
if (err == 0) {
send_simple_response(client->socket, 226, STATUS_226);
}
else {
send_simple_response(client->socket, 550, STATUS_550);
}
return 0;
}
static int cmd_mkd(lftpd_client_t* client, const char* arg) {
if (arg == NULL || strlen(arg) == 0) {
send_simple_response(client->socket, 550, STATUS_550);
}
char* path = lftpd_io_canonicalize_path(client->directory, arg);
if (fs_mkdir(path) < 0) {
send_simple_response(client->socket, 550, STATUS_550);
}
else {
send_simple_response(client->socket, 250, STATUS_250);
}
free(path);
return 0;
}
static int cmd_nlst(lftpd_client_t* client, const char* arg) {
if (client->data_socket == -1) {
send_simple_response(client->socket, 425, STATUS_425);
return -1;
}
send_simple_response(client->socket, 150, STATUS_150);
int err = send_nlst(client->data_socket, client->directory);
close(client->data_socket);
client->data_socket = -1;
if (err == 0) {
send_simple_response(client->socket, 226, STATUS_226);
}
else {
send_simple_response(client->socket, 550, STATUS_550);
}
return 0;
}
static int cmd_noop(lftpd_client_t* client, const char* arg) {
send_simple_response(client->socket, 200, STATUS_200);
return 0;
}
static int cmd_pass(lftpd_client_t* client, const char* arg) {
send_simple_response(client->socket, 230, STATUS_230);
return 0;
}
static int cmd_pasv(lftpd_client_t* client, const char* arg) {
// open a data port
int listener_socket = lftpd_inet_listen(0);
if (listener_socket < 0) {
send_simple_response(client->socket, 425, STATUS_425);
return -1;
}
// get the port from the new socket, which is random
int port = lftpd_inet_get_socket_port(listener_socket);
send_simple_response(client->socket, 227, STATUS_227,
net_default_dev->ip_addr[0],
net_default_dev->ip_addr[1],
net_default_dev->ip_addr[2],
net_default_dev->ip_addr[3],
(port >> 8) & 0xff,
(port & 0xff));
// wait for the connection to the data port
lftpd_log_debug("waiting for data port connection on port %d...", port);
int client_socket = accept(listener_socket, NULL, NULL);
if (client_socket < 0) {
lftpd_log_error("error accepting client socket");
close(listener_socket);
return -1;
}
lftpd_log_debug("data port connection received...");
// close the listener
close(listener_socket);
uint32_t new_buf_sz = FILE_BUFFER_SIZE;
setsockopt(client_socket, SOL_SOCKET, SO_SNDBUF, &new_buf_sz, sizeof(new_buf_sz));
setsockopt(client_socket, SOL_SOCKET, SO_RCVBUF, &new_buf_sz, sizeof(new_buf_sz));
client->data_socket = client_socket;
return 0;
}
static int cmd_pwd(lftpd_client_t* client, const char* arg) {
send_simple_response(client->socket, 257, "\"%s\"", client->directory);
return 0;
}
static int cmd_quit(lftpd_client_t* client, const char* arg) {
send_simple_response(client->socket, 221, STATUS_221);
return -1;
}
static int cmd_retr(lftpd_client_t* client, const char* arg) {
if (client->data_socket == -1) {
send_simple_response(client->socket, 425, STATUS_425);
return -1;
}
send_simple_response(client->socket, 150, STATUS_150);
char* path = lftpd_io_canonicalize_path(client->directory, arg);
lftpd_log_debug("send '%s'", path);
int err = send_file(client->data_socket, path);
free(path);
close(client->data_socket);
client->data_socket = -1;
if (err == 0) {
send_simple_response(client->socket, 226, STATUS_226);
}
else {
send_simple_response(client->socket, 450, STATUS_450);
}
return 0;
}
static int cmd_rmd(lftpd_client_t* client, const char* arg) {
if (arg == NULL || strlen(arg) == 0) {
send_simple_response(client->socket, 550, STATUS_550);
}
char* path = lftpd_io_canonicalize_path(client->directory, arg);
if (fs_rmdir(path) < 0) {
send_simple_response(client->socket, 550, STATUS_550);
}
else {
send_simple_response(client->socket, 250, STATUS_250);
}
free(path);
return 0;
}
static int cmd_size(lftpd_client_t* client, const char* arg) {
if (!arg) {
send_simple_response(client->socket, 550, STATUS_550);
return 0;
}
char* path = lftpd_io_canonicalize_path(client->directory, arg);
lftpd_log_debug("size %s", path);
int fd = fs_open(path, O_RDONLY);
if (fd < 0) {
send_simple_response(client->socket, 550, STATUS_550);
}
else {
send_simple_response(client->socket, 213, "%lu", fs_total(fd));
fs_close(fd);
}
free(path);
return 0;
}
static int cmd_stor(lftpd_client_t* client, const char* arg) {
if (client->data_socket == -1) {
send_simple_response(client->socket, 425, STATUS_425);
return -1;
}
send_simple_response(client->socket, 150, STATUS_150);
char* path = lftpd_io_canonicalize_path(client->directory, arg);
lftpd_log_debug("receive '%s'", path);
int err = receive_file(client->data_socket, path);
free(path);
close(client->data_socket);
client->data_socket = -1;
if (err == 0) {
send_simple_response(client->socket, 226, STATUS_226);
}
else {
send_simple_response(client->socket, 450, STATUS_450);
}
return 0;
}
static int cmd_syst(lftpd_client_t* client, const char* arg) {
send_simple_response(client->socket, 215, "UNIX Type: L8");
return 0;
}
static int cmd_type(lftpd_client_t* client, const char* arg) {
send_simple_response(client->socket, 200, STATUS_200);
return 0;
}
static int cmd_user(lftpd_client_t* client, const char* arg) {
send_simple_response(client->socket, 230, STATUS_230);
return 0;
}
static int handle_control_channel(lftpd_client_t* client) {
size_t read_buffer_len = 512;
char _read_buffer[read_buffer_len];
char *read_buffer = _read_buffer;
char command_tmp[4 + 1];
int err = send_simple_response(client->socket, 220, STATUS_220);
if (err != 0) {
lftpd_log_error("error sending welcome message");
goto cleanup;
}
while (err == 0) {
memset(read_buffer, 0, read_buffer_len);
int line_len = lftpd_inet_read_line(client->socket, read_buffer, read_buffer_len);
if (line_len != 0) {
lftpd_log_error("error reading next command");
goto cleanup;
}
// find the index of the first space
int index;
char* p = strchr(read_buffer, ' ');
if (p != NULL) {
index = p - read_buffer;
}
// if no space, use the whole string
else {
index = strlen(read_buffer);
}
// if the index is 5 or greater the command is too long
if (index >= 5) {
err = send_simple_response(client->socket, 500, STATUS_500);
continue;
}
// copy the command into a temporary buffer
memset(command_tmp, 0, sizeof(command_tmp));
memcpy(command_tmp, read_buffer, index);
// upper case the command
for (int i = 0; command_tmp[i]; i++) {
command_tmp[i] = (char) toupper((int) command_tmp[i]);
}
// see if we have a matching function for the command, and if
// so, dispatch it
bool matched = false;
for (int i = 0; commands[i].command; i++) {
if (strcmp(commands[i].command, command_tmp) == 0) {
char* arg = NULL;
if (index < strlen(read_buffer)) {
arg = strdup(read_buffer + index + 1);
arg = lftpd_string_trim(arg);
}
err = commands[i].handler(client, arg);
if (arg) {
free(arg);
}
matched = true;
break;
}
}
if (!matched) {
send_simple_response(client->socket, 502, STATUS_502);
}
}
cleanup:
close(client->socket);
return 0;
}
int lftpd_start(const char* directory, int port, lftpd_t* lftpd) {
memset(lftpd, 0, sizeof(lftpd_t));
lftpd->directory = directory;
lftpd->port = port;
lftpd->server_socket = lftpd_inet_listen(port);
if (lftpd->server_socket < 0) {
lftpd_log_error("error creating listener");
return -1;
}
lftpd_log_info("listening on %d.%d.%d.%d:%d...",
net_default_dev->ip_addr[0],
net_default_dev->ip_addr[1],
net_default_dev->ip_addr[2],
net_default_dev->ip_addr[3],
port
);
while (true) {
lftpd_log_info("waiting for connection...");
int client_socket = accept(lftpd->server_socket, NULL, NULL);
if (client_socket < 0) {
lftpd_log_error("error accepting client socket");
close(lftpd->server_socket);
lftpd->server_socket = -1;
break;
}
lftpd_log_info("connection received.");
lftpd_client_t client = {
.directory = strdup(directory),
.socket = client_socket,
.data_socket = -1,
};
lftpd->client = &client;
handle_control_channel(&client);
free(client.directory);
lftpd->client = NULL;
}
return 0;
}
int lftpd_stop(lftpd_t* lftpd) {
if (lftpd->server_socket < 0) {
return -1;
}
shutdown(lftpd->server_socket, SHUT_RDWR);
close(lftpd->server_socket);
lftpd->server_socket = -1;
if (lftpd->client) {
shutdown(lftpd->client->socket, SHUT_RDWR);
close(lftpd->client->socket);
}
return 0;
}