-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandles.c
570 lines (516 loc) · 16.7 KB
/
handles.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
#include "server.h"
void ftp_user(Command *cmd, State *state)
{
const int total_usernames = sizeof(usernames)/sizeof(char *);
if(lookup(cmd->arg,usernames,total_usernames)>=0){
state->username = malloc(32);
memset(state->username,0,32);
strcpy(state->username,cmd->arg);
state->username_ok = 1;
state->message = "331 User name okay, need password\n";
}else{
state->message = "530 Invalid username\n";
}
write_state(state);
}
/* PASS */
void ftp_pass(Command *cmd, State *state)
{
if(state->username_ok==1){
state->logged_in = 1;
state->message = "230 Login successful\n";
}else{
state->message = "500 Invalid username or password\n";
}
write_state(state);
}
/* PORT */
void ftp_port(Command *cmd, State *state)
{
if(state->logged_in){
char ip_addr[32];
uint8_t tcp_addr[6] = {0, 0, 0, 0, 0, 0};
if(cmd->arg == NULL){
fprintf(stderr, "FTP: Get Arg error.\n");
pthread_exit(NULL);
}
char *pNext;
pNext = strtok(cmd->arg, ",");
int32_t count = 0;
while(pNext != NULL) {
tcp_addr[count] = atoi(pNext);
count++;
pNext = strtok(NULL,",");
}
sprintf(ip_addr, "%d.%d.%d.%d", tcp_addr[0], tcp_addr[1], tcp_addr[2], tcp_addr[3]);
/* Connect to client */
state->sock_port = conn_cli(ip_addr, 256 * tcp_addr[4] + tcp_addr[5]);
if(state->sock_port < 0) {
state->message = "501 Connect error.\n";
fprintf(stderr, "FTP: Connect error.\n");
pthread_exit(NULL);
}
state->message = "200 PORT SUCCESS!\n";
state->mode = CLIENT;
}else{
state->message = "530 Please login with USER and PASS.\n";
}
write_state(state);
}
/* PASV */
void ftp_pasv(Command *cmd, State *state)
{
if(state->logged_in){
int ip[4];
char buff[255];
char *response = "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\n";
Port *port = malloc(sizeof(Port));
gen_port(port);
getip(state->connection,ip);
/* Close previous passive socket? */
close(state->sock_pasv);
/* Start listening here, but don't accept the connection */
state->sock_pasv = create_socket((256*port->p1)+port->p2);
printf("port: %d\n",256*port->p1+port->p2);
sprintf(buff,response,ip[0],ip[1],ip[2],ip[3],port->p1,port->p2);
state->message = buff;
state->mode = SERVER;
puts(state->message);
}else{
state->message = "530 Please login with USER and PASS.\n";
printf("%s",state->message);
}
write_state(state);
}
/** LIST command */
void ftp_list(Command *cmd, State *state)
{
if(state->logged_in==1){
struct dirent *entry;
struct stat statbuf;
struct tm *time;
char timebuff[80], current_dir[BSIZE];
int connection;
time_t rawtime;
/* TODO: dynamic buffering maybe? */
char cwd[BSIZE], cwd_orig[BSIZE];
memset(cwd,0,BSIZE);
memset(cwd_orig,0,BSIZE);
/* Later we want to go to the original path */
getcwd(cwd_orig,BSIZE);
/* Just chdir to specified path */
if(strlen(cmd->arg)>0&&cmd->arg[0]!='-'){
chdir(cmd->arg);
}
getcwd(cwd,BSIZE);
DIR *dp = opendir(cwd);
if(!dp){
state->message = "551 Failed to open directory.\n";
}else{
if(state->mode == SERVER){
connection = accept_connection(state->sock_pasv);
state->message = "150 Here comes the directory listing.\n";
puts(state->message);
while(entry=readdir(dp)){
if(stat(entry->d_name,&statbuf)==-1){
fprintf(stderr, "FTP: Error reading file stats...\n");
}else{
char *perms = malloc(9);
memset(perms,0,9);
/* Convert time_t to tm struct */
rawtime = statbuf.st_mtime;
time = localtime(&rawtime);
strftime(timebuff,80,"%b %d %H:%M",time);
str_perm((statbuf.st_mode & ALLPERMS), perms);
dprintf(connection,
"%c%s %5ld %4d %4d %8ld %s %s\r\n",
(entry->d_type==DT_DIR)?'d':'-',
perms,
statbuf.st_nlink,
statbuf.st_uid,
statbuf.st_gid,
statbuf.st_size,
timebuff,
entry->d_name);
}
}
write_state(state);
state->message = "226 Directory send OK.\n";
state->mode = NORMAL;
close(connection);
close(state->sock_pasv);
}else if(state->mode == CLIENT){
state->message = "150 Here comes the directory listing.\n";
while(entry=readdir(dp)){
if(stat(entry->d_name,&statbuf)==-1){
fprintf(stderr, "FTP: Error reading file stats...\n");
}else{
char *perms = malloc(9);
memset(perms,0,9);
/* Convert time_t to tm struct */
rawtime = statbuf.st_mtime;
time = localtime(&rawtime);
strftime(timebuff,80,"%b %d %H:%M",time);
str_perm((statbuf.st_mode & ALLPERMS), perms);
dprintf(state->sock_port,
"%c%s %5ld %4d %4d %8ld %s %s\r\n",
(entry->d_type==DT_DIR)?'d':'-',
perms,
statbuf.st_nlink,
statbuf.st_uid,
statbuf.st_gid,
statbuf.st_size,
timebuff,
entry->d_name);
}
}
write_state(state);
state->message = "226 Directory send OK.\n";
state->mode = NORMAL;
close(state->sock_port);
}else{
state->message = "425 Use PASV or PORT first.\n";
}
}
closedir(dp);
chdir(cwd_orig);
}else{
state->message = "530 Please login with USER and PASS.\n";
}
state->mode = NORMAL;
write_state(state);
}
/* QUIT */
void ftp_quit(State *state)
{
state->message = "221 Goodbye!\n";
write_state(state);
close(state->connection);
pthread_exit(NULL);
}
/* PWD */
void ftp_pwd(Command *cmd, State *state)
{
if(state->logged_in){
char cwd[BSIZE];
char result[BSIZE];
memset(result, 0, BSIZE);
if(getcwd(cwd,BSIZE)!=NULL){
strcat(result,"257 \"");
strcat(result,cwd);
strcat(result,"\"\n");
state->message = result;
}else{
state->message = "550 Failed to get pwd.\n";
}
write_state(state);
}
}
/* CWD */
void ftp_cwd(Command *cmd, State *state)
{
if(state->logged_in){
if(chdir(cmd->arg)==0){
state->message = "250 Directory successfully changed.\n";
}else{
state->message = "550 Failed to change directory.\n";
}
}else{
state->message = "500 Login with USER and PASS.\n";
}
write_state(state);
}
/* MKD command */
void ftp_mkd(Command *cmd, State *state)
{
if(state->logged_in){
char cwd[BSIZE];
char res[BSIZE];
memset(cwd,0,BSIZE);
memset(res,0,BSIZE);
getcwd(cwd,BSIZE);
if(cmd->arg[0]=='/'){
if(mkdir(cmd->arg,S_IRWXU)==0){
strcat(res,"257 \"");
strcat(res,cmd->arg);
strcat(res,"\" new directory created.\n");
state->message = res;
}else{
state->message = "550 Failed to create directory. Check path or permissions.\n";
}
}
else{
if(mkdir(cmd->arg,S_IRWXU)==0){
sprintf(res,"257 \"%s/%s\" new directory created.\n", cwd, cmd->arg);
state->message = res;
}else{
state->message = "550 Failed to create directory.\n";
}
}
}else{
state->message = "500 Good news, everyone! There's a report on TV with some very bad news!\n";
}
write_state(state);
}
/* RETR */
void ftp_retr(Command *cmd, State *state)
{
int32_t fd;
struct stat stat_buf;
off_t offset = 0;
ssize_t sent_total = 0;
if(state->logged_in){
if(state->mode == SERVER) {
int32_t connection = -1;
if(access(cmd->arg,R_OK)==0 && (fd = open64(cmd->arg,O_RDONLY))){
fstat(fd,&stat_buf);
state->message = "150 Opening BINARY mode data connection.\n";
write_state(state);
connection = accept_connection(state->sock_pasv);
close(state->sock_pasv);
do {
ssize_t add_byte = sendfile(connection, fd, &offset, stat_buf.st_size);
sent_total += add_byte;
if(add_byte){
if(sent_total == stat_buf.st_size){
state->message = "226 File send OK.\n";
break;
}
}else{
state->message = "550 Failed to read file.\n";
}
} while (sent_total != stat_buf.st_size);
}else{
state->message = "550 Failed to get file\n";
}
close(connection);
close(state->sock_pasv);
} else if(state->mode == CLIENT) {
if(access(cmd->arg,R_OK)==0 && (fd = open64(cmd->arg,O_RDONLY))){
fstat(fd,&stat_buf);
state->message = "150 Opening BINARY mode data connection..\n";
write_state(state);
do {
ssize_t add_byte = sendfile(state->sock_port, fd, &offset, stat_buf.st_size);
sent_total += add_byte;
if(add_byte){
if(sent_total == stat_buf.st_size){
state->message = "226 File send OK.\n";
break;
}
}else{
state->message = "550 Failed to read file.\n";
}
} while (sent_total != stat_buf.st_size);
/*
if(sent_total = sendfile(state->sock_port, fd, &offset, stat_buf.st_size)){
if(sent_total != stat_buf.st_size){
perror("CLIENT ftp_retr:sendfile");
pthread_exit(NULL);
}
state->message = "226 File send OK.\n";
}else{
state->message = "550 Failed to read file.\n";
}
*/
}else{
state->message = "550 Failed to get file\n";
}
close(state->sock_port);
} else{
state->message = "550 Use PASV or PORT first.\n";
}
}else{
state->message = "530 Please login with USER and PASS.\n";
}
close(fd);
write_state(state);
state->mode = NORMAL;
}
/* STOR */
void ftp_stor(Command *cmd, State *state)
{
int32_t fd;
const int buff_size = 8192;
FILE *fp = fopen(cmd->arg,"w");
if(fp==NULL){
perror("ftp_stor:fopen");
}else if(state->logged_in){
if(state->mode == SERVER) {
int32_t res = 0;
int32_t pipefd[2];
fd = fileno(fp);
int32_t connection = accept_connection(state->sock_pasv);
close(state->sock_pasv);
if(pipe(pipefd)==-1)
perror("ftp_stor: pipe");
state->message = "125 Data connection already open; transfer starting.\n";
write_state(state);
while ((res = splice(connection, 0, pipefd[1], NULL, buff_size, SPLICE_F_MORE | SPLICE_F_MOVE))>0) {
splice(pipefd[0], NULL, fd, 0, buff_size, SPLICE_F_MORE | SPLICE_F_MOVE);
}
if(res==-1) {
perror("ftp_stor: splice");
pthread_exit(NULL);
}else{
state->message = "226 File send OK.\n";
}
close(connection);
close(state->sock_pasv);
} else if(state->mode == CLIENT) {
int32_t res = 0;
int32_t pipefd[2];
fd = fileno(fp);
if(pipe(pipefd)==-1)
perror("ftp_stor: pipe");
printf("DEBUG CLIENT socket=%d, fd=%d\n", state->sock_port, fd);
state->message = "125 Data connection already open; transfer starting.\n";
write_state(state);
while ((res = splice(state->sock_port, 0, pipefd[1], NULL, buff_size, SPLICE_F_MORE | SPLICE_F_MOVE))>0) {
printf("DEBUG res=%d", res);
splice(pipefd[0], NULL, fd, 0, buff_size, SPLICE_F_MORE | SPLICE_F_MOVE);
}
if(res==-1) {
perror("ftp_stor: splice");
pthread_exit(NULL);
}else{
state->message = "226 File send OK.\n";
}
close(state->sock_port);
} else {
state->message = "550 use PASV or PORT.\n";
}
}else{
state->message = "530 Please login with USER and PASS.\n";
}
write_state(state);
close(fd);
state->mode = NORMAL;
}
/* ABOR */
void ftp_abor(State *state)
{
if(state->logged_in){
state->message = "226 Closing data connection.\n";
state->message = "225 Data connection open; no transfer in progress.\n";
}else{
state->message = "530 Please login with USER and PASS.\n";
}
write_state(state);
}
/* TYPE */
void ftp_type(Command *cmd,State *state)
{
if(state->logged_in){
if(cmd->arg[0]=='A') {
state->message = "200 Switching to ASCII mode.\n";
state->type = 1;
} else {
state->message = "200 Switching to Binary mode.\n";
state->type = 0;
}
/*
if(cmd->arg[0]=='I'){
state->message = "200 Switching to Binary mode.\n";
state->type = 0;
}else if(cmd->arg[0]=='A'){
state->message = "200 Switching to ASCII mode.\n";
state->type = 1;
}else{
state->message = "504 Command not implemented for that parameter.\n";
}
*/
}else{
state->message = "530 Please login with USER and PASS.\n";
}
write_state(state);
}
/* CDUP */
void ftp_cdup(Command *cmd, State *state) {
if(state->logged_in){
if(chdir("..")==0){
state->message = "250 Directory successfully changed.\n";
}else{
state->message = "550 Failed to change directory.\n";
}
}else{
state->message = "500 Login with USER and PASS.\n";
}
write_state(state);
}
/* SYST */
void ftp_syst(State *state) {
struct utsname kernel_info;
int ret = uname(&kernel_info);
if (ret == 0) {
char kversion[512] = { 0 };
sprintf(kversion, "200 %s-%s%s\n", kernel_info.release, kernel_info.machine, kernel_info.version);
state->message = kversion;
} else {
state->message = "425 Get system info error.\n";
}
write_state(state);
}
/* DELE */
void ftp_dele(Command *cmd,State *state)
{
if(state->logged_in){
if(unlink(cmd->arg)==-1){
state->message = "550 File unavailable.\n";
}else{
state->message = "250 Requested file action okay, completed.\n";
}
}else{
state->message = "530 Please login with USER and PASS.\n";
}
write_state(state);
}
/* RMD */
void ftp_rmd(Command *cmd, State *state)
{
if(!state->logged_in){
state->message = "530 Please login first.\n";
}else{
if(rmdir(cmd->arg)==0){
state->message = "250 Requested file action okay, completed.\n";
}else{
state->message = "550 Cannot delete directory.\n";
}
}
write_state(state);
}
void ftp_size(Command *cmd, State *state)
{
if(state->logged_in){
struct stat statbuf;
char filesize[128];
memset(filesize,0,128);
if(stat(cmd->arg,&statbuf)==0){
sprintf(filesize, "213 %ld\n", statbuf.st_size);
state->message = filesize;
}else{
state->message = "550 Could not get file size.\n";
}
}else{
state->message = "530 Please login with USER and PASS.\n";
}
write_state(state);
}
void str_perm(int perm, char *str_perm)
{
int curperm = 0;
int flag = 0;
int read, write, exec;
char fbuff[3];
read = write = exec = 0;
int i;
for(i = 6; i>=0; i-=3){
curperm = ((perm & ALLPERMS) >> i ) & 0x7;
memset(fbuff,0,3);
read = (curperm >> 2) & 0x1;
write = (curperm >> 1) & 0x1;
exec = (curperm >> 0) & 0x1;
sprintf(fbuff,"%c%c%c",read?'r':'-' ,write?'w':'-', exec?'x':'-');
strcat(str_perm,fbuff);
}
}