forked from zTrix/simple-ftp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
595 lines (571 loc) · 23.6 KB
/
server.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
#include <stdio.h>
#define _XOPEN_SOURCE
#include <unistd.h>
#include <shadow.h>
#include <pwd.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include "vars.h"
#include "zlog.h"
#include "utils.h"
int server = -1, client = -1, running = 0;
pid_t forkpid = 1;
char buf[BUF_SIZE];
/**
* handle Ctrl-C
*
* @param {int} socket fd
*
* the parent and all its children will recieve kill signal and execute this
*/
void ouch(int n) {
running = 0;
puts("");
if (forkpid > 0) { // ftpd
if (server >= 0) {
int st = close(server);
info(0, "shutdown ftp ... %d", st);
}
} else { // session
if (client >= 0) {
int st = close(client);
info(1, "shutdown ftp session... %d", st);
}
}
exit(0);
}
enum FTP_CMD parse_cmd(char *buf, int len) {
int i,j;
for (i=0; i<FTP_CMD_COUNT; i++) {
for (j=0; FTP_CMD_LIST[i].name[j] != '\0' && j < len; j++) {
if (FTP_CMD_LIST[i].name[j] != buf[j] && FTP_CMD_LIST[i].name[j] != buf[j]-32) break;
}
if (FTP_CMD_LIST[i].name[j] == '\0')
return FTP_CMD_LIST[i].cmd;
}
return INVALID;
}
/**
* handle a newly accepted ftp session
*
*/
void handle_session(int client) {
send_str(client, FTP_RDY);
int i, n, retry;
char cwd[BUF_SIZE] = {0}, cmdbuf[BUF_SIZE] = {0};
enum DATA_TYPE datatype = TYPE_IMAGE;
srand(time(0));
uint32_t pasv_port;
enum TRSF_TYPE trsf_type = TRSF_PORT;
int pasv_server = -1;
struct sockaddr_in svr_addr;
int svr_addr_len = sizeof(svr_addr);
getsockname(client, (struct sockaddr*)&svr_addr, &svr_addr_len);
uint32_t svr_host_addr = ntohl(svr_addr.sin_addr.s_addr);
uint32_t port_address = 0;
uint16_t port_port = 0;
int data_client = -1;
struct sockaddr_in data_client_addr;
int data_client_len = sizeof(data_client_addr);
uint32_t restdata = 0;
char rnfr[BUF_SIZE];
char *p = NULL; // tmp file path
struct stat file_stat; // file stat for time and size
struct tm mdtime;
/* Updated Nov 8 2016 by @Holmes1st */
/* Variables for login process */
char *input_id; // inputed user name
char *input_pw; // inputed password data
char *input_hash; // hash generated with input id & pw
char shadow_salt[BUF_SIZE]; // parsed from shadow->pwdp
struct spwd *shadow; // shadow data // Check shadow.h
struct passwd *passwd_data; // passwd data for uid, gid //check pwd.h
int count;
int flag=2; // 0: not logined 1: id check 2: logined
while ((n=recv(client, buf, BUF_SIZE, MSG_PEEK)) > 0) {
if (!running) break;
buf[n] = '\0';
info(1, "recved %d bytes: %s", n, buf);
for (i=0; i<n; i++) {
if (buf[i] == '\n') break;
}
if (buf[i] != '\n') {
err(1, "no line break found");
break;
}
n = recv(client, buf, i+1, 0);
buf[n] = '\0';
enum FTP_CMD cmd = parse_cmd(buf, n);
if (cmd < 0) {
buf[n-2] = 0;
err(1, "unknown cmd: %s", buf);
continue;
}
info(1, "cmd: %s, %d", FTP_CMD_LIST[cmd].name, cmd);
if (flag == 0 && cmd != USER && cmd != QUIT || flag == 1 && cmd != PASS && cmd != QUIT){
send_str(client, FTP_NEED_LOGIN);
}
else {
switch(cmd) {
case NOOP:
send_str(client, FTP_OK);
break;
case QUIT:
send_str(client, FTP_QUIT);
running = 0;
flag = 0;
break;
case HELP:
send_str(client, FTP_HELP);
break;
case USER:
input_id = buf;
// cut id
while(*(input_id-1) != ' ') input_id++;
// remove <CRLF>
for (i = 0; i<strlen(input_id);i++){
if (input_id[i] == '\r' || input_id[i] == '\n')
input_id[i] = 0;
}
// get shadow data from /etc/shadow
shadow = getspnam(input_id);
if (shadow){
send_str(client, FTP_NAMEOK);
info(1,"user %s trying to login...",shadow->sp_namp);
flag = 1;
}
else {
send_str(client, FTP_ERR_NAME);
info(1, "%s login failed.", input_id);
}
// send_str(client, FTP_NAMEOK);
break;
case PASS:
input_pw = buf;
// cut pw
while (*(input_pw-1) != ' ') input_pw++;
// remove <CRLF>
for (i=0;i<strlen(input_pw);i++)
if (input_pw[i] == '\r' || input_pw[i] == '\n')
input_pw[i] = 0;
// get salt from shadow data
count = 0;
for(i = 0; i < 3;){
if (shadow->sp_pwdp[count++] == '$')
i++;
}
strncpy(shadow_salt, shadow->sp_pwdp, count);
input_hash = crypt(input_pw, shadow_salt);
if (strcmp(input_hash, shadow->sp_pwdp) == 0){
send_str(client, FTP_LOGIN);
info(1, "user %s logged in.", shadow->sp_namp);
// get data from /etc/passwd for uid and gid
passwd_data = getpwnam(shadow->sp_namp);
setgid(passwd_data->pw_gid);
setuid(passwd_data->pw_uid);
flag = 2;
}
else {
send_str(client, FTP_LOGIN_FAIL);
info(1, "user %s:%s logged in fail.", shadow->sp_namp,input_pw);
flag = 0;
}
break;
case PWD:
getcwd(cwd, sizeof(cwd));
send_str(client, FTP_PWD, cwd);
break;
case SYST:
send_str(client, FTP_SYST);
break;
case TYPE:
if (buf[5] == 'A') {
datatype = TYPE_ASCII;
send_str(client, FTP_CTYPE, buf[5]);
} else if (buf[5] == 'I') {
datatype = TYPE_IMAGE;
send_str(client, FTP_CTYPE, buf[5]);
} else {
send_str(client, FTP_ERR_DATATYPE, datatype == TYPE_ASCII ? 'A' : 'I');
}
break;
case PASV:
retry = 100;
while (retry--) { // in case of create server error, port used
pasv_port = (rand() % 64512 + 1024);
trsf_type = TRSF_PASV;
pasv_server = new_server(INADDR_ANY, pasv_port, 1);
if (pasv_server >= 0) break;
}
if (pasv_server < 0) {
err(1, "can not create pasv port for passive mode");
// TODO: send err msg here
} else {
info(1, "PASV server created, port : %hu", pasv_port);
uint32_t t = svr_addr.sin_addr.s_addr;
send_str(client, FTP_PASV, t&0xff, (t>>8)&0xff, (t>>16)&0xff, (t>>24)&0xff, pasv_port>>8, pasv_port & 0xff);
}
break;
case PORT:
trsf_type = TRSF_PORT;
int _st = parse_addr_port(buf, &port_address, &port_port);
if (!_st) {
err(1, "port cmd error parsing addr and port");
send_str(client, FTP_ERR_PORT);
} else {
info(1, "address is %s, port is %ld", n2a(port_address), port_port);
send_str(client, FTP_PORT);
}
break;
case LIST:
if (trsf_type == TRSF_PASV) {
if (pasv_port > 1024 && pasv_port <= 65535 && pasv_server >= 0) {
send_str(client, FTP_ASCII, "LIST");
data_client = accept(pasv_server, (struct sockaddr *)&data_client_addr, &data_client_len);
if (data_client < 0) {
err(1, "LIST, accept data client socket error");
}
} else {
err(1, "LIST, no pasv server created");
break;
}
} else if (trsf_type == TRSF_PORT) {
if (port_address == 0 || port_port == 0) {
err(1, "LIST, in PORT mode, address and port not set before");
break;
}
send_str(client, FTP_ASCII, "LIST");
info(1, "LIST, in PORT mode, try connecting %s %lu", n2a(port_address), port_port);
data_client = new_client(port_address, port_port);
if (data_client < 0) {
err(1, "port mode connect client data sock error");
break;
} else {
info(1, "LIST, in PORT mode, %s %lu connected", n2a(port_address), port_port);
}
} else {
err(1, "LIST: transfer type no specified");
}
if (data_client >= 0) {
getcwd(cwd, sizeof(cwd));
sprintf(cmdbuf, "ls -l %s", cwd);
FILE *p1 = popen(cmdbuf, "r");
send_file(data_client, p1, 0);
send_str(client, FTP_TRSF_OK);
pclose(p1);
info(1, "LIST , data client closed, status %d", close(data_client));
data_client = -1;
} else {
err(1, "LIST , no data client created");
}
if (pasv_server >= 0) {
info(1, "LIST, closing passive server ... %d", close(pasv_server));
pasv_server = -1;
}
break;
case REST:
if (parse_number(buf, &restdata) == 0) {
send_str(client, FTP_REST, restdata);
} else {
err(1, "REST, command error, wrong param");
send_str(client, FTP_ERR_PARAM, "REST");
}
break;
case RETR:
if (trsf_type == TRSF_PASV) {
if (pasv_port > 1024 && pasv_port <= 65535 && pasv_server >= 0) {
if (datatype == TYPE_ASCII) {
send_str(client, FTP_ASCII, "RETR");
} else {
send_str(client, FTP_BIN, "RETR");
}
data_client = accept(pasv_server, (struct sockaddr *)&data_client_addr, &data_client_len);
if (data_client < 0) {
err(1, "accept data client error");
break;
}
} else {
err(1, "RETR, pasv server not ready ");
}
} else if (trsf_type == TRSF_PORT) {
if (port_address == 0 || port_port == 0) {
err(1, "RETR, in PORT mode, address and port not set before");
break;
}
if (datatype == TYPE_ASCII) {
send_str(client, FTP_ASCII, "RETR");
} else {
send_str(client, FTP_BIN, "RETR");
}
info(1, "RETR , PORT mode, try connecting %s %lu", n2a(port_address), port_port);
data_client = new_client(port_address, port_port);
if (data_client < 0) {
err(1, "RETR: connect client error ");
}
} else {
err(1, "RETR: transfer type no specified");
break;
}
p = parse_path(buf);
if (!p) {
err(1, "RETR, wrong param");
send_str(1, FTP_ERR_PARAM, "RETR");
break;
} else {
int st = send_path(data_client, p, restdata);
if (st >= 0) {
send_str(client, FTP_TRSF_OK);
restdata = 0;
/* added controlling permission error */
} else {
send_str(client, FTP_ERROR, st == -1 ? (access(p, F_OK)==0? "access denyed. Check Permission" : "file not exist") : "unknow error");
}
}
if (data_client >= 0) {
info(1, "RETR, closing data client ... %d", close(data_client));
data_client = -1;
}
if (pasv_server >= 0) {
info(1, "RETR, closing passive server ... %d", close(pasv_server));
pasv_server = -1;
}
break;
case STOR:
if (trsf_type == TRSF_PASV) {
if (pasv_port > 1024 && pasv_port <= 65535 && pasv_server >= 0) {
if (datatype == TYPE_ASCII) {
send_str(client, FTP_ASCII, "STOR");
} else {
send_str(client, FTP_BIN, "STOR");
}
data_client = accept(pasv_server, (struct sockaddr *)&data_client_addr, &data_client_len);
if (data_client < 0) {
err(1, "STOR, accept data client error");
break;
}
} else {
err(1, "STOR, pasv server not ready ");
}
} else if (trsf_type == TRSF_PORT) {
if (port_address == 0 || port_port == 0) {
err(1, "STOR, PORT mode, address and port not set before");
break;
}
if (datatype == TYPE_ASCII) {
send_str(client, FTP_ASCII, "STOR");
} else {
send_str(client, FTP_BIN, "STOR");
}
info(1, "STOR, PORT mode, try connecting %s %lu", n2a(port_address), port_port);
data_client = new_client(port_address, port_port);
if (data_client < 0) {
err(1, "STOR: connect client error ");
}
} else {
err(1, "STOR: transfer type no specified");
break;
}
p = parse_path(buf);
if (!p) {
err(1, "STOR, wrong param");
send_str(1, FTP_ERR_PARAM, "RETR");
break;
} else {
int st = recv_path(data_client, p, restdata, 0);
if (st >= 0) {
send_str(client, FTP_TRSF_OK);
restdata = 0;
/* added controlling permission error */
} else {
send_str(client, FTP_ERROR, access(p, W_OK)!=0 ? "access denyed. Check permission" : "unknow error");
}
}
if (data_client >= 0) {
info(1, "STOR, closing data client ... %d", close(data_client));
data_client = -1;
}
if (pasv_server >= 0) {
info(1, "STOR, closing passive server ... %d", close(pasv_server));
pasv_server = -1;
}
break;
case CDUP:
if (!chdir("..")) {
send_str(client, FTP_CDUP);
} else {
send_str(client, FTP_ERROR, "change to parent dir failed");
}
break;
case CWD:
p = parse_path(buf);
if (!p) {
err(1, "CWD, wrong param");
send_str(1, FTP_ERR_PARAM, "CWD");
break;
}
info(1, "chdir \"%s\"", p);
if (!(chdir(p))) {
send_str(client, FTP_CWD);
} else {
err(1, "errno = %d, errstr is %s", errno, strerror(errno));
send_str(client, FTP_ERROR, "change dir failed");
}
break;
case MDTM:
case SIZE:
p = parse_path(buf);
if (!p) {
if (cmd == MDTM) {
err(1, "MDTM, wrong param");
send_str(client, FTP_ERR_PARAM, "MDTM");
} else {
err(1, "SIZE, wrong param");
send_str(client, FTP_ERR_PARAM, "SIZE");
}
break;
}
if (stat(p, &file_stat) == 0) {
if (cmd == MDTM) {
char _buf[BUF_SIZE];
gmtime_r(&(file_stat.st_mtime), &mdtime);
strftime(_buf, sizeof(_buf), "%Y%m%d%H%M%S", &mdtime);
send_str(client, FTP_MDTM, _buf);
} else {
send_str(client, FTP_SIZE, file_stat.st_size);
}
}
break;
case DELE:
p = parse_path(buf);
if (!p) {
err(1, "DELE, param error");
send_str(client, FTP_ERR_PARAM, "DELE");
} else {
if (remove(p) == 0) {
send_str(client, FTP_DELE);
} else {
send_str(client, FTP_ERROR, "delete failed, file not exist ?");
}
}
break;
case RMD:
p = parse_path(buf);
if (!p) {
err(1, "RMD, param error");
send_str(client, FTP_ERR_PARAM, "RMD");
} else {
if (rmdir(p) == 0) {
send_str(client, FTP_DELE);
} else {
send_str(client, FTP_ERROR, "rmdir failed, dir not exist ?");
}
}
break;
case MKD:
p = parse_path(buf);
if (!p) {
err(1, "MKD, param error");
send_str(client, FTP_ERR_PARAM, "MKD");
} else {
if (mkdir(p, 0777) == 0) {
send_str(client, FTP_MKDIR);
} else {
send_str(client, FTP_ERROR, "mkdir failed, dir already exist ?");
}
}
break;
case RNFR:
p = parse_path(buf);
if (!p) {
err(1, "RNFR param error");
send_str(client, FTP_ERR_PARAM, "RNFR");
} else {
strcpy(rnfr, p);
send_str(client, FTP_RNFR);
}
break;
case RNTO:
p = parse_path(buf);
if (!p) {
err(1, "RNTO param error");
send_str(client, FTP_ERR_PARAM, "RNTO");
} else {
if (rename(rnfr, p) == 0) {
send_str(client, FTP_RNTO);
} else {
send_str(client, FTP_ERROR, "rnto error, please check param");
}
}
break;
default:
send_str(client, FTP_CMDNOIM);
break;
}
}
if (p) {
free(p);
p = NULL;
}
if (!running) break;
}
info(1, "exiting session ...");
int st = close(client);
info(1, "clent closed , status %d", st);
client = -1;
if (data_client > 0) {
info(1, "data client closed, status %d", close(data_client));
}
if (pasv_server > 0) {
info(1, "pasv server closed, status %d", close(pasv_server));
}
}
int main(int argc, char *argv[]){
int port = LISTEN_PORT;
/* if(getuid() != 0){ */
/* printf("You need a root permission. Try \"sudo %s\" (or \"su\" as root).\nQuit program....\n",argv[0]); */
/* exit(0); */
/* } */
if (argc > 1) {
port = atoi(argv[1]);
}
signal(SIGCHLD, SIG_IGN); // ignore child termination signal
signal(SIGINT, ouch); // catch Ctrl-C
signal(SIGTERM, ouch);
server = new_server(LISTEN_ADDR, port, MAX_CONNECTIONS);
if (server < 0) {
err(0, "can not create server, return code is %d, socket already in use", server);
exit(1);
}
running = 1;
struct sockaddr_in client_addr;
while (running) {
uint32_t l = sizeof(client_addr);
client = accept(server, (struct sockaddr *)&client_addr, &l);
if (!running) break;
if (client < 0) {
err(0, "accept client error: %d", client);
exit(2);
}
info(0, "client connected: %s", inet_ntoa(client_addr.sin_addr));
forkpid = fork();
if (forkpid == -1) {
err(0, "fork server error");
} else if (forkpid == 0) { // child
server = -1; // avoid killing server on Ctrl-C
info(0, "new ftp session");
handle_session(client);
exit(0);
} else if (forkpid > 0) { // myself
client = -1;
}
}
info(0, "exit ftpd");
return 0;
}