-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
696 lines (618 loc) · 21.5 KB
/
utils.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//
// Created by masteryoda on 18-10-26.
//
#include "utils.h"
#include "network.h"
int parseArgs(int argc, const char **argv) {
if(argc == 1) {
PORT = 21;
strcpy(ROOT, "/tmp");
return 0;
}
if(argc != 5){
printf("Arguments number unmatched\n");
return -1;
}
if (strcmp(argv[1], "-port") != 0 && strcmp(argv[3], "-root") != 0){
printf("Arguments unmatched\n");
return -1;
}
errno = 0;
char* ptr = NULL;
PORT = (int) strtol(argv[2], &ptr, 10);
if ((errno == ERANGE && (PORT == INT_MAX || PORT == INT_MIN)) || (errno != 0 && PORT == 0) || argv[2] == ptr) {
printf("Argument port invalid format\n");
return -1;
}
if(access(argv[4], F_OK) < 0) {
printf("Path %s does not exist\n", argv[4]);\
return -1;
}
memset(ROOT, 0, sizeof(ROOT));
strcpy(ROOT, argv[4]);
return 0;
}
void initClients() {
for (int i = 0; i < MAX_CON; ++i) {
clients[i].commandCon = -1;
clients[i].fileTransferCon = -1;
clients[i].listenCon = -1;
clients[i].loginState = 0;
clients[i].renameState = 0;
clients[i].port = 0;
clients[i].transferProcess = 0;
clients[i].dirPointer = NULL;
clients[i].mode = 0;
clients[i].port_port = -1;
memset(clients[i].port_ip, 0, sizeof(clients[i].port_ip));
memset(clients[i].fileName, 0, sizeof(clients[i].fileName));
memset(clients[i].renamePath, 0, sizeof(clients[i].renamePath));
memset(&clients[i].dirState, 0, sizeof(clients[i].dirState));
memset(clients[i].dirName, 0, sizeof(clients[i].dirName));
memset(clients[i].directory, 0, sizeof(clients[i].directory));
strcpy(clients[i].directory, ROOT);
}
}
char *processMsg(char *msg, int index) {
parseCommand(msg);
if (!command[0])
return "500 Please input correct command\r\n";
upper(command[0]);
chdir(clients[index].directory);
if (clients[index].transferState)
return NULL;
if (!strcmp(command[0], "USER"))
return user(command[1], index);
if (!strcmp(command[0], "PASS"))
return pass(command[1], index);
if (!strcmp(command[0], "QUIT") || !strcmp(command[0], "ABOR"))
return quit(command[1], index);
if (clients[index].loginState != 2)
return "332 Please login first\r\n";
if (!strcmp(command[0], "RETR"))
return retr(command[1], index);
if (!strcmp(command[0], "STOR"))
return stor(command[1], index);
if (!strcmp(command[0], "SYST"))
return syst(command[1], index);
if (!strcmp(command[0], "TYPE"))
return type(command[1], index);
if (!strcmp(command[0], "PORT"))
return port(command[1], index);
if (!strcmp(command[0], "PASV"))
return pasv(command[1], index);
if (!strcmp(command[0], "MKD"))
return mkd(command[1], index);
if (!strcmp(command[0], "CWD"))
return cwd(command[1], index);
if (!strcmp(command[0], "PWD"))
return pwd(command[1], index);
if (!strcmp(command[0], "LIST"))
return list(command[1], index);
if (!strcmp(command[0], "RMD"))
return rmd(command[1], index);
if (!strcmp(command[0], "RNFR"))
return rnfr(command[1], index);
if (!strcmp(command[0], "RNTO"))
return rnto(command[1], index);
else
return "500 Please input correct command\r\n";
}
int generateData(int state, int index) {
switch (state) {
case 1:
return sendFile(index);
case 2:
return sendList(index);
default:
disconnectFileTransfer(index);
return 0;
}
}
char *user(char *content, int index) {
if (!content)
return "501 Please input correct command argument\r\n";
if (!strcmp(content, "anonymous") && clients[index].loginState < 2) {
clients[index].loginState = 1;
return "331 Guest login ok, send your complete e-mail address as password.\r\n";
} else if (strcmp(content, "anonymous") != 0) {
return "530 User name incorrect\r\n";
} else {
return "550 Unknown error\r\n";
}
}
char *pass(char *content, int index) {
if (!content)
return "501 Please input correct command argument\r\n";
char *pattern = ".*@.*";
regex_t reg;
regmatch_t pmatch[1];
regcomp(®, pattern, REG_EXTENDED);
int result = regexec(®, content, 1, pmatch, 0);
if (!result && clients[index].loginState == 1) {
clients[index].loginState = 2;
return "230 Login ok\r\n";
} else if (!result && clients[index].loginState == 2) {
return "202 Already logged in\r\n";
} else if (!result && clients[index].loginState == 0) {
return "503 Please input USER first\r\n";
} else if (result) {
return "530 Password incorrect\r\n";
} else {
return "550 Unknown error\r\n";
}
}
char *retr(char *content, int index) {
if (!content)
return "501 Please input correct command argument\r\n";
else {
struct stat fileState;
if (access(content, F_OK | R_OK) || stat(content, &fileState))
return "551 File can't be read or does not exist\r\n";
else if (!S_ISREG(fileState.st_mode))
return "551 Path must be a readable regular file\r\n";
else {
if (strstr(content, "../"))
return "501 Path should not content \"..\"\r\n";
if (clients[index].mode == 0)
return "425 Data connection has not been established\r\n";
else if (clients[index].mode == 1) {
int result = connectToClient(index);
if (!result)
return "425 Cannot connect to client\r\n";
else {
clients[index].transferState = 1;
realpath(content, clients[index].fileName);
return "150 Data connection established, begin transfer\r\n";
}
} else {
if (clients[index].fileTransferCon == -1) {
return "425 Data connection has noe benn established";
}
clients[index].transferState = 1;
realpath(content, clients[index].fileName);
return "150 Data connection established, begin transfer\r\n";
}
}
}
}
char *stor(char *content, int index) {
if (!content)
return "501 Please input correct command argument\r\n";
else {
FILE *fp = fopen(content, "w");
if (!fp)
return "551 File can't be created\r\n";
else {
fclose(fp);
if (strstr(content, "../"))
return "501 Path should not content \"..\"\r\n";
if (clients[index].mode == 0)
return "425 Data connection has not been established\r\n";
if (clients[index].mode == 1) {
int result = connectToClient(index);
if (!result)
return "425 Cannot connect to client\r\n";
else {
clients[index].transferState = 3;
realpath(content, clients[index].fileName);
return "150 Data connection established, begin storing\r\n";
}
} else {
if (clients[index].fileTransferCon == -1) {
return "425 Data connection has noe benn established";
}
clients[index].transferState = 3;
realpath(content, clients[index].fileName);
return "150 Data connection established, begin storing\r\n";
}
}
}
}
char *quit(char *content, int index) {
char *msg = "221 Bye\r\n";
int num = (int) write(clients[index].commandCon, msg, strlen(msg));
if (num < 0)
perror("fail to write\r\n");
disconnect(index);
return NULL;
}
char *syst(char *content, int index) {
return "215 UNIX Type: L8\r\n";
}
char *type(char *content, int index) {
if (!content)
return "501 Please input correct command argument\r\n";
if (!strcmp(content, "I"))
return "200 Type set to I.\r\n";
else
return "501 Please input correct command argument\r\n";
}
char *port(char *content, int index) {
if (!content)
return "501 Please input correct command argument\r\n";
else {
memset(clients[index].port_ip, 0, sizeof(clients[index].port_ip));
int error = parseIP_PORT(content, clients[index].port_ip, &clients[index].port_port);
if (error < 0)
return "501 Please input correct IP-PORT format\r\n";
else {
clients[index].mode = 1;
disconnectListen(index);
disconnectFileTransfer(index);
return "200 Prepare to connect to client\r\n";
}
}
}
char *pasv(char *content, int index) {
if (content)
return "501 Arguments are prohibited\r\n";
else {
memset(message, 0, sizeof(message));
strcpy(message, "227 ");
strcat(message, IP);
srand((unsigned int) time(0));
int port = (int) (random() % (65535 - 20000 + 1) + 20000);
int i = 0;
while (i < MAX_CON) {
if (port == clients[i].port) {
port = (int) (random() % (65535 - 20000 + 1) + 20000);
i = 0;
continue;
} else
i++;
}
char p1[BUF_LEN] = {0};
char p2[BUF_LEN] = {0};
sprintf(p1, "%d", port / 256);
sprintf(p2, "%d", port % 256);
strcat(message, ",");
strcat(message, p1);
strcat(message, ",");
strcat(message, p2);
strcat(message, "\r\n");
int result = createListeningConn(port, index);
if (!result)
return "421 Unable tclients[index].transferState = 0;o listen for data connection\r\n";
else {
clients[index].mode = 2;
return message;
}
}
}
char *mkd(char *content, int index) {
if (!content)
return "501 Please input correct command argument\r\n";
else {
int error = mkdir(content, 0755);
if (error < 0)
return "550 Directory creation failed\r\n";
else {
char *directory = (char *) malloc(1024 * sizeof(char));
memset(message, 0, sizeof(message));
chdir(content);
getcwd(directory, 1024);
strcpy(message, "250 Directory");
strcat(message, directory);
strcat(message, " created");
strcat(message, "\r\n");
free(directory);
return message;
}
}
}
char *cwd(char *content, int index) {
if (!content)
return "501 Please input correct command argument\r\n";
else {
int error = chdir(content);
if (error < 0)
return "550 No such file or directory\r\n";
else {
memset(clients[index].directory, 0, sizeof(clients[index].directory));
getcwd(clients[index].directory, BUF_LEN);
return "250 Working directory has been changed\r\n";
}
}
}
char *pwd(char *content, int index) {
memset(message, 0, sizeof(message));
strcpy(message, "257 \"");
strcat(message, clients[index].directory);
strcat(message, "\"");
strcat(message, "\r\n");
return message;
}
char *list(char *content, int index) {
if (!content)
return "501 Please input correct command argument\r\n";
else {
if (access(content, F_OK)) {
disconnectFileTransfer(index);
return "450 No such file or directory\r\n";
} else {
clients[index].transferState = 2;
if (stat(content, &clients[index].dirState) < 0) {
disconnectFileTransfer(index);
return "450 Directory or file can't be accessed\r\n";
} else {
if (S_ISREG(clients[index].dirState.st_mode)) {
if (clients[index].mode == 0)
return "425 Data connection has not been established\r\n";
else if (clients[index].mode == 1) {
int result = connectToClient(index);
if (!result)
return "425 Cannot connect to client\r\n";
else {
clients[index].dirPointer = NULL;
realpath(content, clients[index].dirName);
return "150 File okay, begin transfer\r\n";
}
} else {
clients[index].dirPointer = NULL;
realpath(content, clients[index].dirName);
return "150 File okay, begin transfer\r\n";
}
} else if (S_ISDIR(clients[index].dirState.st_mode)) {
if (clients[index].mode == 0)
return "425 Data connection has not been established\r\n";
else if (clients[index].mode == 1) {
int result = connectToClient(index);
if (!result)
return "425 Cannot connect to client\r\n";
else {
clients[index].dirPointer = opendir(content);
realpath(content, clients[index].dirName);
return "150 Directory okay, begin transfer\r\n";
}
} else {
if (clients[index].fileTransferCon == -1) {
return "425 Data connection has noe benn established";
}
clients[index].dirPointer = opendir(content);
realpath(content, clients[index].dirName);
return "150 Directory okay, begin transfer\r\n";
}
} else {
disconnectFileTransfer(index);
return "450 Unknown file type\r\n";
}
}
}
}
}
char *rmd(char *content, int index) {
if (!content)
return "501 Please input correct command argument\r\n";
else {
int error = removeDir(content);
if (error < 0)
return "550 Fail to remove directory\r\n";
else
return "250 Successfully remove directory\r\n";
}
}
char *rnfr(char *content, int index) {
if (!content)
return "501 Please input correct command argument\r\n";
else {
if (access(content, F_OK))
return "550 File does not exist\r\n";
else if (access(content, R_OK | W_OK))
return "550 Permission denied\r\n";
else {
memset(clients[index].renamePath, 0, sizeof(clients[index].renamePath));
realpath(content, clients[index].renamePath);
clients[index].renameState = 1;
return "350 File is ready\r\n";
}
}
}
char *rnto(char *content, int index) {
if (!content)
return "501 Please input correct command argument\r\n";
else {
if (!clients[index].renameState)
return "503 Please send correct RNFR command first\r\n";
else {
char command[1024] = {0};
strcpy(command, "mv ");
strcat(command, clients[index].renamePath);
strcat(command, " ");
strcat(command, content);
int error = system(command);
if (error)
return "553 Renaming file failed\r\n";
else
return "250 Renaming file succeed\r\n";
}
}
}
int sendFile(int index) {
struct stat fileState;
stat(clients[index].fileName, &fileState);
if (clients[index].transferProcess >= fileState.st_size) {
clients[index].transferState = 4;
return 0;
} else {
memset(data, 0, sizeof(data));
FILE *file = fopen(clients[index].fileName, "r");
fseek(file, clients[index].transferProcess, SEEK_SET);
int size = (int) fread(data, 1, BUF_LEN, file);
fclose(file);
return size;
}
}
int sendList(int index) {
char curDir[] = ".";
char upDir[] = "..";
char size[BUF_LEN] = {0};
char fileName[BUF_LEN] = {0};
struct dirent *dp;
struct stat dirState;
memset(data, 0, sizeof(data));
if (S_ISREG(clients[index].dirState.st_mode)) {
sleep(0);
stat(clients[index].dirName, &dirState);
char *file = strrchr(clients[index].dirName, '/');
if (file)
++file;
else
file = clients[index].dirName;
strcpy(data, "-rw-r--r-- 1 owner group");
sprintf(size, "%13d", (int) dirState.st_size);
strcat(data, size);
strcat(data, " ");
strcat(data, ctime((const time_t *) &dirState.st_ctim));
strcat(data, " ");
strcat(data, file);
strcat(data, "\r\n");
clients[index].transferState = 4;
return (int) strlen(data);
} else if (S_ISDIR(clients[index].dirState.st_mode)) {
if (!clients[index].dirPointer) {
clients[index].transferState = 4;
return 0;
}
dp = readdir(clients[index].dirPointer);
if (!dp) {
printf("List transfer complete for client %d", index);
clients[index].transferState = 4;
return 0;
} else {
if ((0 == strcmp(curDir, dp->d_name)) || (0 == strcmp(upDir, dp->d_name)))
return 0;
strcpy(fileName, clients[index].dirName);
strcat(fileName, "/");
strcat(fileName, dp->d_name);
stat(fileName, &dirState);
if (dp->d_type == DT_DIR)
strcpy(data, "drwxr-xr-x 1 owner group");
else if (dp->d_type == DT_REG)
strcpy(data, "-rw-r--r-- 1 owner group");
sprintf(size, "%13d", (int) dirState.st_size);
strcat(data, size);
strcat(data, " ");
strcat(data, ctime((const time_t *) &dirState.st_ctim));
strcat(data, " ");
strcat(data, dp->d_name);
strcat(data, "\r\n");
return (int) strlen(data);
}
}
return 0;
}
void parseCommand(char *arg) {
memset(command, 0, sizeof(command));
command[0] = strtok(arg, " ");
command[1] = strtok(NULL, " ");
if (command[0]) {
int len = (int) strlen(command[0]);
if (len > 2) {
if (command[0][len - 1] == '\n')
command[0][len - 1] = 0;
if (command[0][len - 2] == '\r')
command[0][len - 2] = 0;
}
}
if (command[1]) {
int len = (int) strlen(command[1]);
if (len > 2) {
if (command[1][len - 1] == '\n')
command[1][len - 1] = 0;
if (command[1][len - 2] == '\r')
command[1][len - 2] = 0;
}
}
}
void upper(char *string) {
if (!string)
return;
for (int i = 0; i < strlen(string); ++i) {
if (string[i] >= 'a' && string[i] <= 'z')
string[i] += ('A' - 'a');
}
}
int removeDir(const char *dir) {
char cur_dir[] = ".";
char up_dir[] = "..";
char dir_name[BUF_LEN];
DIR *dirp;
struct dirent *dp;
struct stat dir_stat;
if (0 != access(dir, F_OK)) {
return -1;
}
if (0 > stat(dir, &dir_stat)) {
perror("get directory stat error");
return -1;
}
if (S_ISREG(dir_stat.st_mode)) {
remove(dir);
} else if (S_ISDIR(dir_stat.st_mode)) {
dirp = opendir(dir);
while ((dp = readdir(dirp)) != NULL) {
if ((0 == strcmp(cur_dir, dp->d_name)) || (0 == strcmp(up_dir, dp->d_name))) {
continue;
}
sprintf(dir_name, "%s/%s", dir, dp->d_name);
removeDir(dir_name);
}
closedir(dirp);
rmdir(dir);
} else {
perror("Unknown file type\n");
}
return 0;
}
int parseIP_PORT(char *ip_port, char *ip, int *port) {
char **ip_port_parse = (char **) malloc(6 * sizeof(char *));
memset(ip_port_parse, 0, 6 * sizeof(char *));
ip_port_parse[0] = strtok(ip_port, ",");
for (int i = 1; i < 6; ++i) {
ip_port_parse[i] = strtok(NULL, ",");
}
for (int i = 0; i < 6; ++i) {
if (ip_port_parse[i] == NULL)
return -1;
}
for (int i = 0; i < 4; ++i) {
strcat(ip, ip_port_parse[i]);
if (i != 3)
strcat(ip, ".");
}
char *ptr1 = NULL;
char *ptr2 = NULL;
errno = 0;
int p1 = (int) strtol(ip_port_parse[4], &ptr1, 10);
int p2 = (int) strtol(ip_port_parse[5], &ptr2, 10);
if ((errno == ERANGE && (p1 == INT_MAX || p1 == INT_MIN)) || (errno != 0 && p1 == 0) || ip_port_parse[4] == ptr1 ||
(errno == ERANGE && (p2 == INT_MAX || p2 == INT_MIN)) || (errno != 0 && p2 == 0) || ip_port_parse[5] == ptr2) {
perror("parse ip error\n");
return -1;
}
*port = p1 * 256 + p2;
return 0;
}
int getLocalIP(const char *eth_inf, char *ip) {
int sd;
struct sockaddr_in sin;
struct ifreq ifr;
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sd) {
printf("socket error: %s\n", strerror(errno));
return -1;
}
strncpy(ifr.ifr_name, eth_inf, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
// if error: No such device
if (ioctl(sd, SIOCGIFADDR, &ifr) < 0) {
printf("ioctl error: %s\n", strerror(errno));
close(sd);
return -1;
}
memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
snprintf(ip, 16, "%s", inet_ntoa(sin.sin_addr));
close(sd);
return 0;
}