-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp.c
977 lines (823 loc) · 24.6 KB
/
ftp.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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
#include "std.h"
#include "util.h"
#include "ftp.h"
#include "sutils.h"
#include "psp_cfg.h"
#include <pspiofilemgr.h>
#include <pspiofilemgr_fcntl.h>
#include <pspiofilemgr_stat.h>
#include <pspiofilemgr_dirent.h>
#include "psp_init.h"
static u8 loc_ms_buf[TRANSFER_MS_BUFFER_SIZE];
int
mftpCreateDirIfNeeded(MftpConnection *con, char *Filename)
{
SceIoStat fileStats;
char DirPath[MAX_PATH_LENGTH];
char *ScanDir;
char *EndDir;
strcpy(DirPath, Filename);
EndDir = strrchr(DirPath, '/');
if (EndDir == (char *)0) return 0;
EndDir[1] = '\0';
/* Skip first slash */
ScanDir = DirPath;
ScanDir = strchr(ScanDir,'/');
if (ScanDir == (char *)0) return 0;
ScanDir = ScanDir + 1;
while ( (EndDir = strchr(ScanDir,'/')) != (char *)0 ) {
*EndDir = '\0';
if (sceIoGetstat(DirPath, &fileStats) >= 0) {
if (!FIO_SO_ISDIR(fileStats.st_attr)) {
return -1;
}
} else {
sceIoMkdir(DirPath, 0);
sceKernelDelayThread(100000);
}
*EndDir = '/';
ScanDir = EndDir + 1;
}
return 0;
}
void
sendResponse(MftpConnection *con, char* s)
{
strcat(con->sockCommandBuffer, s);
if (endsWith(con->sockCommandBuffer, "\n")) {
sceNetInetSend(con->sockCommand, con->sockCommandBuffer, strlen(con->sockCommandBuffer) , 0);
strcpy(con->sockCommandBuffer, "");
}
}
void sendResponseLn(MftpConnection *con, char* s)
{
strcat(con->sockCommandBuffer, s);
strcat(con->sockCommandBuffer, "\r\n");
sceNetInetSend(con->sockCommand, con->sockCommandBuffer, strlen(con->sockCommandBuffer) , 0);
strcpy(con->sockCommandBuffer, "");
}
void sendData(MftpConnection *con, char* s)
{
strcat(con->sockDataBuffer, s);
if (endsWith(con->sockDataBuffer, "\n")) {
sceNetInetSend(con->sockData, con->sockDataBuffer, strlen(con->sockDataBuffer) , 0);
strcpy(con->sockDataBuffer, "");
}
}
void
sendDataLn(MftpConnection *con, char* s) {
strcat(con->sockDataBuffer, s);
strcat(con->sockDataBuffer, "\r\n");
sceNetInetSend(con->sockData, con->sockDataBuffer, strlen(con->sockDataBuffer) , 0);
strcpy(con->sockDataBuffer, "");
}
unsigned short pasvPort=59735;
int
openDataConnectionPASV(MftpConnection *con)
{
con->usePassiveMode=1;
int err;
struct sockaddr_in addrPort;
memset(&addrPort, 0, sizeof(struct sockaddr_in));
addrPort.sin_reserved = sizeof(struct sockaddr_in);
addrPort.sin_family = AF_INET;
addrPort.sin_port = htons(pasvPort);
addrPort.sin_addr[0] = 0;
addrPort.sin_addr[1] = 0;
addrPort.sin_addr[2] = 0;
addrPort.sin_addr[3] = 0;
con->sockPASV = sceNetInetSocket(AF_INET, SOCK_STREAM, 0);
if (con->sockPASV & 0x80000000) return 0;
err = sceNetInetBind(con->sockPASV, &addrPort, sizeof(addrPort));
if (err) return 0;
err = sceNetInetListen(con->sockPASV, 1);
if (err) return 0;
pasvPort++;
return 0;
}
int openDataConnection(MftpConnection *con) {
int err;
if (con->usePassiveMode) {
struct sockaddr_in addrAccept;
u32 cbAddrAccept;
cbAddrAccept = sizeof(addrAccept);
con->sockData = sceNetInetAccept(con->sockPASV, &addrAccept, &cbAddrAccept);
if (con->sockData & 0x80000000) return 0;
} else {
struct sockaddr_in addrPort;
memset(&addrPort, 0, sizeof(struct sockaddr_in));
addrPort.sin_reserved = sizeof(struct sockaddr_in);
addrPort.sin_family = AF_INET;
addrPort.sin_port = htons(con->port_port);
addrPort.sin_addr[0] = con->port_addr[0];
addrPort.sin_addr[1] = con->port_addr[1];
addrPort.sin_addr[2] = con->port_addr[2];
addrPort.sin_addr[3] = con->port_addr[3];
con->sockData = sceNetInetSocket(AF_INET, SOCK_STREAM, 0);
if (con->sockData & 0x80000000) return 0;
err = sceNetInetConnect(con->sockData, &addrPort, sizeof(struct sockaddr_in));
if (err) return 0;
}
return 1;
}
int
closeDataConnection(MftpConnection *con) {
int err = 0;
err |= sceNetInetClose(con->sockData);
if (con->usePassiveMode) {
err |= sceNetInetClose(con->sockPASV);
}
if (err) return 0; else return 1;
}
int mftpServerHello(MftpConnection *con) {
sendResponseLn(con, "220 FTP Server Ready");
return 0;
}
int mftpCommandPWD(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con,command)) {
sendResponse(con, "257 \"");
sendResponse(con, con->curDir);
sendResponseLn(con, "\" is current directory.");
}
return 0;
}
int mftpCommandCWD(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con,command)) {
char* newDir=skipWS(&command[3]);
trimEndingWS(newDir);
char parsedDir[MAX_PATH_LENGTH+1];
char* pParsedDir=parsedDir;
parsedDir[0]=0;
char* parser=newDir;
if ((*newDir)=='/') {
strcpy(con->curDir,"/");
parser++;
}
do {
if ((*parser)==0 || (*parser)=='/') {
*pParsedDir=0;
if (strcmp(parsedDir,".")==0) {
} else if (strcmp(parsedDir,"..")==0) {
char* pUp=con->curDir+strlen(con->curDir)-2;
while ( pUp>=con->curDir && (*pUp)!='/' ) {
pUp--;
}
if ((++pUp)>=con->curDir) {
*pUp=0;
}
if (con->curDir[0]==0) {
break;
}
} else {
strcat(con->curDir, parsedDir);
if (!endsWith(con->curDir,"/")) {
strcat(con->curDir, "/");
}
}
pParsedDir=parsedDir;
} else {
(*pParsedDir++)=(*parser);
}
} while (*(parser++)!=0);
if (!endsWith(con->curDir,"/")) {
strcat(con->curDir, "/");
}
sendResponseLn(con, "250 CWD command successful.");
}
return 0;
}
int mftpCommandLIST(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con,command)) {
if (openDataConnection(con)==0) {
sendResponseLn(con, "425 impossible to open data connection.");
} else {
sendResponseLn(con, "150 Opening ASCII mode data connection for file list");
char path[MAX_PATH_LENGTH+1];
strcpy(path, con->root);
strcat(path, con->curDir);
int ret,fd;
SceIoDirent curFile;
fd = sceIoDopen(path);
if (fd>0) {
do {
memset(&curFile, 0, sizeof(SceIoDirent));
ret = sceIoDread(fd, &curFile);
char sInt[16]; strcpy(sInt,"");
if (ret>0) {
if (FIO_S_ISDIR(curFile.d_stat.st_mode)) {
sendData(con, "drwxrwxrwx 2 root root ");
itoa(sInt, curFile.d_stat.st_size);
sendData(con, sInt);
sendData(con, " Jan 01 1970 ");
sendDataLn(con, curFile.d_name);
} else if (FIO_S_ISLNK(curFile.d_stat.st_mode)) {
sendData(con, "lrwxrwxrwx 1 root root ");
itoa(sInt, curFile.d_stat.st_size);
sendData(con, sInt);
sendData(con, " Jan 01 1970 ");
sendData(con, curFile.d_name);
sendData(con, " -> ");
sendDataLn(con, "???");
} else {
sendData(con, "-rwxrwxrwx 1 root root ");
itoa(sInt, curFile.d_stat.st_size);
sendData(con, sInt);
sendData(con, " Jan 01 1970 ");
sendDataLn(con, curFile.d_name);
}
}
} while (ret>0);
sceIoDclose(fd);
}
sendResponseLn(con, "226 Transfer complete.");
closeDataConnection(con);
}
}
return 0;
}
int mftpCommandNLST(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con, command)) {
if (openDataConnection(con)==0) {
sendResponseLn(con, "425 impossible to open data connection.");
} else {
sendResponseLn(con, "150 Opening ASCII mode data connection for file list");
char path[MAX_PATH_LENGTH+1];
strcpy(path, con->root);
strcat(path, con->curDir);
int ret,fd;
SceIoDirent curFile;
fd = sceIoDopen(path);
if (fd>0) {
do {
memset(&curFile, 0, sizeof(SceIoDirent));
ret = sceIoDread(fd, &curFile);
sendDataLn(con, curFile.d_name);
} while (ret>0);
sceIoDclose(fd);
}
sendResponseLn(con, "226 Transfer complete.");
closeDataConnection(con);
}
}
return 0;
}
int mftpCommandRETR(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con, command)) {
if (openDataConnection(con)==0) {
sendResponseLn(con, "425 impossible to open data connection.");
} else {
char* fileName=skipWS(&command[5]);
trimEndingWS(fileName);
if (strlen(fileName)>0) {
sendResponse(con, "150 Opening ASCII mode data connection for ");
//TODO.txt (1805 bytes).
sendResponse(con, fileName);
sendResponseLn(con, ".");
char filePath[MAX_PATH_LENGTH];
if (strStartsWith(fileName, "/")) {
strcpy(filePath, con->root);
strcat(filePath, fileName);
} else {
strcpy(filePath, con->root);
strcat(filePath, con->curDir);
strcat(filePath, fileName);
}
SceIoStat fileStats;
sceIoGetstat(filePath, &fileStats);
if ((! FIO_SO_ISDIR(fileStats.st_attr)) &&
(! FIO_SO_ISLNK(fileStats.st_attr))) {
int fdFile = sceIoOpen(filePath, PSP_O_RDONLY, 0777);
int read_len = 0;
while (( read_len = sceIoRead(fdFile, loc_ms_buf, TRANSFER_MS_BUFFER_SIZE))>0) {
u8* scan_buf = loc_ms_buf;
int send_len = 0;
while (read_len > 0) {
if (read_len > TRANSFER_SEND_BUFFER_SIZE) {
sceNetInetSend(con->sockData, scan_buf, TRANSFER_SEND_BUFFER_SIZE, 0);
scan_buf += TRANSFER_SEND_BUFFER_SIZE;
read_len -= TRANSFER_SEND_BUFFER_SIZE;
} else {
sceNetInetSend(con->sockData, scan_buf, read_len, 0);
read_len = 0;
}
}
}
sceIoClose(fdFile);
sendResponseLn(con, "226 Transfer complete.");
} else {
sendResponse(con, "550 ");
sendResponse(con, fileName);
sendResponseLn(con, ": not a regular file.");
}
} else {
sendResponseLn(con, "500 'RETR': command requires a parameter.");
}
closeDataConnection(con);
}
}
return 0;
}
int mftpCommandSTOR(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con, command)) {
if (openDataConnection(con)==0) {
sendResponseLn(con, "425 impossible to open data connection.");
} else {
char* fileName=skipWS(&command[5]);
trimEndingWS(fileName);
if (strlen(fileName)>0) {
sendResponse(con, "150 Opening ASCII mode data connection for ");
//TODO.txt (1805 bytes).
sendResponse(con, fileName);
sendResponseLn(con, ".");
char filePath[MAX_PATH_LENGTH];
if (strStartsWith(fileName, "/")) {
strcpy(filePath, con->root);
strcat(filePath, fileName);
} else {
strcpy(filePath, con->root);
strcat(filePath, con->curDir);
strcat(filePath, fileName);
}
if (mftpCreateDirIfNeeded(con, filePath) < 0) {
sendResponseLn(con, "552 'STOR': Requested file action aborted.");
} else {
int fdFile = sceIoOpen(filePath, PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777);
int rcv_max = TRANSFER_MS_BUFFER_SIZE - TRANSFER_RECV_BUFFER_SIZE;
int rcv_left= rcv_max;
int rcv_len = 0;
int rcv_sum = 0;
u8* scan_buf = loc_ms_buf;
while (( rcv_len = sceNetInetRecv(con->sockData, (u8*)scan_buf, TRANSFER_RECV_BUFFER_SIZE, 0))>0) {
rcv_sum += rcv_len;
rcv_left -= rcv_len;
scan_buf += rcv_len;
if (rcv_left <= 0) {
sceIoWrite(fdFile, loc_ms_buf, rcv_sum );
rcv_sum = 0;
rcv_left= rcv_max;
scan_buf= loc_ms_buf;
}
}
if (rcv_sum) {
sceIoWrite(fdFile, loc_ms_buf, rcv_sum);
}
sceIoClose(fdFile);
closeDataConnection(con);
sendResponseLn(con, "226 Transfer complete.");
}
} else {
sendResponseLn(con, "500 'STOR': command requires a parameter.");
closeDataConnection(con);
}
}
}
return 0;
}
int mftpCommandSIZE(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con, command)) {
char* fileName=skipWS(&command[5]);
trimEndingWS(fileName);
if (strlen(fileName)>0) {
char filePath[MAX_PATH_LENGTH];
if (strStartsWith(fileName, "/")) {
strcpy(filePath, con->root);
strcat(filePath, fileName);
} else {
strcpy(filePath, con->root);
strcat(filePath, con->curDir);
strcat(filePath, fileName);
}
SceIoStat fileStats;
sceIoGetstat(filePath, &fileStats);
if ((! FIO_SO_ISDIR(fileStats.st_attr)) &&
(! FIO_SO_ISLNK(fileStats.st_attr))) {
char tmp[32];
itoa(tmp, fileStats.st_size);
sendResponse(con, "213 ");
sendResponseLn(con, tmp);
} else {
sendResponse(con, "550 ");
sendResponse(con, fileName);
sendResponseLn(con, ": not a regular file.");
}
} else {
sendResponseLn(con, "500 'SIZE': command requires a parameter.");
}
}
return 0;
}
int mftpCommandDELE(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con, command)) {
char* fileName=skipWS(&command[5]);
trimEndingWS(fileName);
if (strlen(fileName)>0) {
char filePath[MAX_PATH_LENGTH];
if (strStartsWith(fileName, "/")) {
strcpy(filePath, con->root);
strcat(filePath, fileName);
} else {
strcpy(filePath, con->root);
strcat(filePath, con->curDir);
strcat(filePath, fileName);
}
sceIoRemove(filePath);
sendResponseLn(con, "250 DELE command successful.");
} else {
sendResponseLn(con, "500 'DELE': command requires a parameter.");
}
}
return 0;
}
int mftpCommandRMD(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con, command)) {
char* fileName=skipWS(&command[4]);
trimEndingWS(fileName);
if (strlen(fileName)>0) {
char filePath[MAX_PATH_LENGTH];
if (strStartsWith(fileName, "/")) {
strcpy(filePath, con->root);
strcat(filePath, fileName);
} else {
strcpy(filePath, con->root);
strcat(filePath, con->curDir);
strcat(filePath, fileName);
}
trimEndingChar(filePath, '/');
sceIoRmdir(filePath);
sceKernelDelayThread(100000);
sendResponseLn(con, "250 RMD command successful.");
} else {
sendResponseLn(con, "500 'RMD': command requires a parameter.");
}
}
return 0;
}
int mftpCommandMKD(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con, command)) {
char* fileName=skipWS(&command[4]);
trimEndingWS(fileName);
if (strlen(fileName)>0) {
char filePath[MAX_PATH_LENGTH];
if (strStartsWith(fileName, "/")) {
strcpy(filePath, con->root);
strcat(filePath, fileName);
} else {
strcpy(filePath, con->root);
strcat(filePath, con->curDir);
strcat(filePath, fileName);
}
trimEndingChar(filePath, '/');
if (mftpCreateDirIfNeeded(con, filePath) < 0) {
sendResponseLn(con, "552 'MKD': Requested file action aborted.");
} else {
sceIoMkdir(filePath, 0);
sceKernelDelayThread(100000);
sendResponse(con, "257 \"");
sendResponse(con, fileName);
sendResponseLn(con, "\" - Directory successfully created.");
}
} else {
sendResponseLn(con, "500 'MKD': command requires a parameter.");
}
}
return 0;
}
int mftpCommandHELP(MftpConnection *con, char* command)
{
sendResponseLn(con, "214-The following commands are recognized (* =>'s unimplemented).");
sendResponseLn(con, "214-USER PASS ACCT* CWD XCWD* CDUP XCUP* SMNT*");
sendResponseLn(con, "214-QUIT REIN* PORT PASV TYPE STRU* MODE* RETR");
sendResponseLn(con, "214-STOR STOU* APPE* ALLO* REST* RNFR RNTO ABOR*");
sendResponseLn(con, "214-DELE MDTM* RMD XRMD* MKD XMKD* PWD XPWD*");
sendResponseLn(con, "214-SIZE LIST NLST SITE SYST STAT* HELP NOOP");
sendResponseLn(con, "214 Direct comments to [email protected].");
return 0;
}
int mftpCommandSITE(MftpConnection *con, char * command)
{
char* param=skipWS(&command[5]);
trimEndingWS(param);
toUpperCase(param);
if (strcmp(param, "HELP")==0) {
sendResponseLn(con, "214-The following SITE commands are recognized (* =>'s unimplemented).");
sendResponseLn(con, "214-HELP");
sendResponseLn(con, "214 Direct comments to [email protected].");
} else if (strlen(param)==0) {
sendResponseLn(con, "500 'SITE' requires argument.");
} else {
sendResponse(con, "500 '");
sendResponse(con, command);
sendResponseLn(con, "' not understood.");
}
return 0;
}
int mftpCommandPORT(MftpConnection *con, char* command)
{
int params[6];
char decimByte[4];
char* pDecimByte=decimByte;
char* pParams=skipWS(&command[5]);
int state=0; int err=0; int nbParams=0;
do {
if (state==0 && *pParams>='0' && *pParams<='9') {
state=1;
pParams--;
} else if (state==1 && *pParams>='0' && *pParams<='9') {
if (pDecimByte-decimByte<=2) {
*(pDecimByte++)=*pParams;
} else {
err=1;
}
} else if (state==1 && (*pParams==',' || *pParams==0) && nbParams<6) {
*pDecimByte=0;
if (strlen(decimByte)==0) {
err=1;
} else {
int param=0;
char* tmp=decimByte+strlen(decimByte)-1;
int pow=1;
while (tmp>=decimByte && err==0) {
if (*tmp>='0' && *tmp<='9') {
param+= ((*tmp)-48)*pow;
pow=pow*10;
} else {
err=1;
}
tmp--;
}
if (err==0) {
params[nbParams++]=param;
pDecimByte=decimByte;
}
}
} else {
err=1;
}
} while (*(pParams++)!=0 && err==0);
if (err==0 && state==1 && nbParams==6) {
con->usePassiveMode=0;
con->port_addr[0]=(unsigned char) params[0];
con->port_addr[1]=(unsigned char) params[1];
con->port_addr[2]=(unsigned char) params[2];
con->port_addr[3]=(unsigned char) params[3];
con->port_port=((unsigned char) params[4]<<8) | ((unsigned char) params[5]);
sendResponseLn(con, "200 PORT command successful.");
} else {
con->port_addr[0]=0;
con->port_addr[1]=0;
con->port_addr[2]=0;
con->port_addr[3]=0;
con->port_port=0;
sendResponseLn(con, "500 illegal PORT command.");
}
return 0;
}
int mftpCommandUSER(MftpConnection *con, char* command)
{
if (con->userLoggedIn) {
sendResponseLn(con, "503 You are already logged in!");
} else {
con->user[0]=0;
con->pass[0]=0;
char* pUser=skipWS(&command[5]);
trimEndingWS(pUser);
if (strlen(pUser)==0) {
sendResponseLn(con, "500 'USER': command requires a parameter.");
} else {
strncpy(con->user, pUser, MAX_USER_LENGTH);
sendResponse(con, "331 Password required for ");
sendResponse(con, con->user);
sendResponseLn(con, ".");
}
}
return 0;
}
int mftpCommandPASS(MftpConnection *con, char* command)
{
if (con->userLoggedIn) {
sendResponseLn(con, "503 You are already logged in!");
} else {
if (strlen(con->user)==0) {
sendResponseLn(con, "503 Login with USER first.");
}
else
{
con->pass[0]=0;
char* pPass=skipWS(&command[5]);
trimEndingWS(pPass);
if (strlen(pPass)==0) {
sendResponseLn(con, "500 'PASS': command requires a parameter.");
} else {
strncpy(con->pass, pPass, MAX_PASS_LENGTH);
if (mftp_config.auth_required) {
mftpUser_t *CheckUser = cfg_get_user(con->user, con->pass);
if (CheckUser == (mftpUser_t *)0) {
sendResponseLn(con, "530 You're not allowed to log in.");
con->userLoggedIn=0;
con->user[0]=0; con->pass[0]=0;
return 0;
} else {
strcpy(con->root,CheckUser->root);
}
}
sendResponseLn(con, "230 You're logged in.");
con->userLoggedIn=1;
}
}
}
return 0;
}
int mftpCommandTYPE(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con, command)) {
char* pParam1=skipWS(&command[5]);
trimEndingWS(pParam1);
if (strlen(pParam1)==0) {
sendResponseLn(con, "500 'TYPE': command requires a parameter.");
} else if (strlen(pParam1)==1 && (*pParam1=='A' || *pParam1=='E' || *pParam1=='I' || *pParam1=='L')) {
con->transfertType=*pParam1;
sendResponse(con, "200 Type set to ");
sendResponse(con, pParam1);
sendResponseLn(con, ".");
} else {
sendResponseLn(con, "500 'TYPE': 2 parameters extended version not understood.");
}
}
return 0;
}
int mftpCommandSYST(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con, command)) {
sendResponseLn(con, "215 UNIX Type: L8");
}
return 0;
}
int mftpCommandPASV(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con, command)) {
char tmp[32];
strncpy(tmp, con->serverIp, 31);
strReplaceChar(tmp, '.', ',');
sendResponse(con, "227 Entering Passive Mode (");
sendResponse(con, tmp);
sendResponse(con, ",");
itoa(tmp, (pasvPort>>8) & 0xFF);
sendResponse(con, tmp);
sendResponse(con, ",");
itoa(tmp, pasvPort & 0xFF);
sendResponse(con, tmp);
sendResponseLn(con, ").");
openDataConnectionPASV(con);
}
return 0;
}
int mftpCommandNOOP(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con, command)) {
sendResponseLn(con, "200 NOOP command successful.");
}
return 0;
}
int mftpCommandQUIT(MftpConnection *con, char* command)
{
closeDataConnection(con);
return -1;
}
int mftpCommandRNFR(MftpConnection *con, char* command)
{
if (mftpRestrictedCommand(con, command)) {
char* fileName=skipWS(&command[4]);
trimEndingWS(fileName);
if (strlen(fileName)>0) {
char filePath[MAX_PATH_LENGTH];
if (strStartsWith(fileName, "/")) {
strcpy(filePath, con->root);
strcat(filePath, fileName);
} else {
strcpy(filePath, con->root);
strcat(filePath, con->curDir);
strcat(filePath, fileName);
}
strncpy(con->renameFromFileName,filePath,MAX_PATH_LENGTH);
sendResponseLn(con, "350 File or directory exists, ready for destination name.");
con->renameFrom = 1;
} else {
sendResponseLn(con, "500 'RNFR': command requires a parameter.");
con->renameFrom = 0;
}
}
return 0;
}
int mftpCommandRNTO(MftpConnection *con, char* command)
{
if (! con->renameFrom) {
sendResponseLn(con, "503 'RNTO': Bad sequence of commands.");
return 0;
}
con->renameFrom = 0;
if (mftpRestrictedCommand(con, command)) {
char* fileName=skipWS(&command[4]);
trimEndingWS(fileName);
if (strlen(fileName)>0) {
char filePath[MAX_PATH_LENGTH];
if (strStartsWith(fileName, "/")) {
strcpy(filePath, con->root);
strcat(filePath, fileName);
} else {
strcpy(filePath, con->root);
strcat(filePath, con->curDir);
strcat(filePath, fileName);
}
sceIoRename(con->renameFromFileName,filePath);
sendResponseLn(con, "250 Rename successfull.");
} else {
sendResponseLn(con, "500 'RNTO': command requires a parameter.");
}
}
return 0;
}
int mftpRestrictedCommand(MftpConnection *con, char* command)
{
if ((mftp_config.auth_required) && (!con->userLoggedIn)) {
sendResponseLn(con, "530 Please login with USER and PASS.");
return 0;
}
return 1;
}
int mftpDispatch(MftpConnection *con, char* command)
{
char uCommand[MAX_COMMAND_LENGTH+1];
strncpy(uCommand, command, MAX_COMMAND_LENGTH);
toUpperCase(uCommand);
int renameFrom = 0;
int ret=0;
if (strlen(uCommand)>0) {
if (strcmp(uCommand, "PWD")==0) {
ret=mftpCommandPWD(con, command);
} else if (strcmp(uCommand, "NLST")==0) {
ret=mftpCommandNLST(con, command);
} else if (strcmp(uCommand, "LIST")==0 || strStartsWith(uCommand, "LIST ")) {
ret=mftpCommandLIST(con, command);
} else if (strStartsWith(uCommand, "RETR ")) {
ret=mftpCommandRETR(con, command);
} else if (strStartsWith(uCommand, "STOR ")) {
ret=mftpCommandSTOR(con, command);
} else if (strStartsWith(uCommand, "SIZE ")) {
ret=mftpCommandSIZE(con, command);
} else if (strStartsWith(uCommand, "DELE ")) {
ret=mftpCommandDELE(con, command);
} else if (strStartsWith(uCommand, "RMD ")) {
ret=mftpCommandRMD(con, command);
} else if (strStartsWith(uCommand, "MKD ")) {
ret=mftpCommandMKD(con, command);
} else if (strStartsWith(uCommand, "RNFR ")) {
ret=mftpCommandRNFR(con, command);
renameFrom = con->renameFrom;
} else if (strStartsWith(uCommand, "RNTO ")) {
ret=mftpCommandRNTO(con, command);
} else if (strcmp(uCommand, "CDUP")==0) {
ret=mftpCommandCWD(con, "CWD ..");
} else if (strStartsWith(uCommand, "CWD ")) {
ret=mftpCommandCWD(con, command);
} else if (strcmp(uCommand, "HELP")==0 || strStartsWith(uCommand, "HELP ")) {
ret=mftpCommandHELP(con, command);
} else if (strcmp(uCommand, "SITE")==0 || strStartsWith(uCommand, "SITE ")) {
ret=mftpCommandSITE(con, command);
} else if (strStartsWith(uCommand, "PORT ")) {
ret=mftpCommandPORT(con, command);
} else if (strStartsWith(uCommand, "USER ")) {
ret=mftpCommandUSER(con, command);
} else if (strStartsWith(uCommand, "PASS ")) {
ret=mftpCommandPASS(con, command);
} else if (strStartsWith(uCommand, "TYPE ")) {
ret=mftpCommandTYPE(con, command);
} else if (strcmp(uCommand, "SYST")==0) {
ret=mftpCommandSYST(con, command);
} else if (strcmp(uCommand, "PASV")==0) {
ret=mftpCommandPASV(con, command);
} else if (strcmp(uCommand, "NOOP")==0) {
ret=mftpCommandNOOP(con, command);
} else if (strcmp(uCommand, "QUIT")==0) {
ret=mftpCommandQUIT(con, command);
} else {
sendResponse(con, "500 ");
sendResponse(con, command);
sendResponseLn(con, " not understood.");
# ifdef DEBUG
pspDebugPrintf("command not understood\n");
# endif
}
con->renameFrom = renameFrom;
}
return ret;
}