-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftpConnection.cpp
623 lines (584 loc) · 17.7 KB
/
ftpConnection.cpp
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
#include <assert.h>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include "ftpServer.hpp"
#ifdef NXDK
#include <hal/xbox.h>
#include <lwip/arch.h>
#include <lwip/debug.h>
#include <lwip/dhcp.h>
#include <lwip/errno.h>
#include <lwip/init.h>
#include <lwip/netdb.h>
#include <lwip/netif.h>
#include <lwip/opt.h>
#include <lwip/sockets.h>
#include <lwip/sys.h>
#include <lwip/tcpip.h>
#include <lwip/timeouts.h>
#include <netif/etharp.h>
#include <nxdk/mount.h>
#include <pktdrv.h>
#include <windows.h> // Used for file I/O
#include <xboxkrnl/xboxkrnl.h>
#else
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstring>
bool nxIsDriveMounted(char) {
return true;
}
#endif
const std::string drives = "CDEFGXYZ";
const std::string types[] = { "ASCII", "EBCDIC", "IMAGE", "LOCAL" };
const std::string replies[] = {
"220 Please enter your login name now.\r\n",
"331 Password required.\r\n",
"230 User logged in, proceed.\r\n",
"257 \"%s\" is current directory\r\n",
"502 %s not implemented.\r\n",
"200 Type set to %s\r\n",
"250 \"%s\" is current directory.\r\n",
"215 UNIX type: L8\r\n",
"200 Port command ok.\r\n",
"150 Opening ASCII data connection for ls\r\n",
"226 Data transfer finished successfully. Data connection closed.\r\n",
"504 Command parameter not implemented.\r\n",
"250 Requested file action ok.\r\n",
"553 Requested action not taken.\r\n",
"530 Not logged in.\r\n"
};
void ftpConnection::sendStdString(std::string const& s, int flags = 0) {
sendStdString(_fd, s, flags);
}
void ftpConnection::sendStdString(int fd, std::string const& s, int flags = 0) {
send(fd, s.c_str(), s.length(), flags);
}
ftpConnection::ftpConnection(int fd, FtpServer* s) : _fd(fd), server(s) {
buf = (char*)malloc(FTP_CMD_BUFFER_SIZE);
pwd = "/";
logged_in = false;
sendStdString(replies[0]);
mode = 'I';
}
ftpConnection::~ftpConnection() {
free(buf);
server = nullptr;
}
bool ftpConnection::update() {
// Might as well kill the connection if buffer memory allocation failed.
if (buf == nullptr) {
return false;
}
// handle data from a client
int nbytes;
if ((nbytes = recv(_fd, buf, FTP_CMD_BUFFER_SIZE, 0)) <= 0) {
// got error or connection closed by client
if (nbytes == 0) {
// connection closed
} else {
//InfoLog::outputLine(InfoLog::ERROR, "Error: recv\n");
}
/* Report back to server that we're a dud! */
return false;
} else {
// We received a command!
/* Add a terminating zero to the received string as the
* client does not necessarily do so. */
buf[nbytes] = '\0';
/* Do what the command asks of you:
*
* ABOR - abort a file transfer
* / CWD - change working directory (Lacks sanity check)
* X CDUP - Move up to parent directory
* X DELE - delete a remote file
* X LIST - list remote files
* MDTM - return the modification time of a file
* X MKD - make a remote directory
* X NLST - name list of remote directory
* X PASS - send password
* PASV - enter passive mode
* X PORT - open a data port
* X EPRT - Extended data port (IPv6)
* X PWD - print working directory
* QUIT - terminate the connection
* X RETR - retrieve a remote file
* X RMD - remove a remote directory
* X RNFR - rename from
* X RNTO - rename to
* SITE - site-specific commands
* SIZE - return the size of a file
* X STOR - store a file on the remote host
* X SYST - Identify yourself
* / TYPE - set transfer type (Only accepts IMAGE and ASCII)
* X USER - send username
**/
std::string recvdata(buf);
size_t cmdDataSep = recvdata.find(' ', 0);
std::string arg = " ";
if (cmdDataSep == std::string::npos) {
cmdDataSep = recvdata.find('\r', 0);
} else {
arg = recvdata.substr(cmdDataSep + 1, recvdata.find('\r') - (cmdDataSep + 1));
}
std::string cmd = recvdata.substr(0, cmdDataSep);
if (!cmd.compare("USER")) {
cmdUser(arg);
} else if (!cmd.compare("PASS")) {
cmdPass(arg);
} else if (!cmd.compare("AUTH")) {
cmdUnimplemented(cmd);
} else if (logged_in) {
if (!cmd.compare("ABOR")) {
// cmdAbor();
cmdUnimplemented(cmd);
} else if (!cmd.compare("CWD")) {
cmdCwd(arg);
} else if (!cmd.compare("CDUP")) {
cmdCdup();
} else if (!cmd.compare("DELE")) {
cmdDele(arg);
} else if (!cmd.compare("LIST")) {
cmdList(arg);
} else if (!cmd.compare("MDTM")) {
// cmdMdtm(arg);
cmdUnimplemented(cmd);
} else if (!cmd.compare("MKD")) {
cmdMkd(arg);
} else if (!cmd.compare("NLST")) {
cmdNlst(arg);
} else if (!cmd.compare("PASV")) {
cmdUnimplemented(cmd);
} else if (!cmd.compare("PORT")) {
cmdPort(arg);
} else if (!cmd.compare("EPRT")) {
cmdEprt(arg);
} else if (!cmd.compare("PWD")) {
cmdPwd();
} else if (!cmd.compare("QUIT")) {
// cmdQuit();
cmdUnimplemented(cmd);
} else if (!cmd.compare("RETR")) {
cmdRetr(arg);
} else if (!cmd.compare("RMD")) {
cmdRmd(arg);
} else if (!cmd.compare("RNFR")) {
cmdRnfr(arg);
} else if (!cmd.compare("RNTO")) {
cmdRnto(arg);
} else if (!cmd.compare("SITE")) {
// cmdSite(arg);
cmdUnimplemented(cmd);
} else if (!cmd.compare("SIZE")) {
// cmdSize(arg);
cmdUnimplemented(cmd);
} else if (!cmd.compare("STOR")) {
cmdStor(arg);
} else if (!cmd.compare("SYST")) {
cmdSyst();
} else if (!cmd.compare("TYPE")) {
cmdType(arg);
} else if (!cmd.compare("execute")) {
cmdExecute(arg);
} else {
//InfoLog::outputLine(InfoLog::DEBUG,
// ("Received cmd " + cmd + ", arg " + arg + "\n").c_str());
cmdUnimplemented(cmd);
}
} else {
sendStdString(replies[14]);
}
}
/* Tell the server that we're still alive and kicking! */
return true;
}
void ftpConnection::cmdUser(std::string const& arg) {
if (!arg.compare(server->conf->getUser())) {
sendStdString(replies[1]);
} else {
sendStdString("530 login authentication failed.\r\n");
}
}
void ftpConnection::cmdPass(std::string const& arg) {
if (!arg.compare(server->conf->getPassword())) {
sendStdString(replies[2]);
logged_in = true;
} else {
sendStdString("530 login authentication failed.\r\n");
}
}
void ftpConnection::cmdPwd() {
sprintf(buf, replies[3].c_str(), pwd.c_str());
sendStdString(buf);
}
void ftpConnection::cmdType(std::string const& arg) {
if (arg[0] == 'I') {
sprintf(buf, replies[5].c_str(), "IMAGE");
sendStdString(buf);
mode = 'I';
} else if (arg[0] == 'A') {
sprintf(buf, replies[5].c_str(), "ASCII");
sendStdString(buf);
mode = 'A';
} else {
sendStdString(replies[11]);
}
}
void ftpConnection::cmdCwd(std::string const& arg) {
std::string tmpPwd = "";
if (arg[0] == '.' && arg[1] == '.') {
tmpPwd = pwd.substr(0, pwd.rfind('/', pwd.rfind('/') - 1) + 1);
} else if (arg[0] == '/') {
if (arg.length() > 1) {
tmpPwd = arg + "/";
} else {
tmpPwd = "/";
}
} else {
tmpPwd = pwd + arg + "/";
}
#ifdef NXDK
if (tmpPwd.length() > 1) {
std::string tmpDosPwd = unixToDosPath(tmpPwd);
HANDLE soughtFolder;
if ((soughtFolder = CreateFileA(tmpDosPwd.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
NULL))
!= INVALID_HANDLE_VALUE) {
pwd = tmpPwd;
sprintf(buf, replies[6].c_str(), pwd.c_str());
sendStdString(buf);
} else {
sendStdString(replies[14]);
}
CloseHandle(soughtFolder);
} else {
pwd = "/";
sprintf(buf, replies[6].c_str(), pwd.c_str());
sendStdString(buf);
}
#else
pwd = tmpPwd;
sprintf(buf, replies[6].c_str(), pwd.c_str());
sendStdString(buf);
#endif
}
void ftpConnection::cmdDele(std::string const& arg) {
std::string fileName = arg;
if (arg[0] != '/') {
fileName = pwd + arg;
}
#ifdef NXDK
fileName = unixToDosPath(fileName);
if (DeleteFile(fileName.c_str())) {
sendStdString(replies[12]);
} else {
sendStdString(replies[13]);
}
#else
cmdUnimplemented("DELE");
#endif
}
void ftpConnection::cmdCdup() {
cmdCwd("..");
}
void ftpConnection::cmdSyst() {
sendStdString(replies[7]);
}
void ftpConnection::cmdPort(std::string const& arg) {
int valueSep = arg.find(',', 0);
valueSep = arg.find(',', valueSep + 1);
valueSep = arg.find(',', valueSep + 1);
valueSep = arg.find(',', valueSep + 1);
std::string address = arg.substr(0, valueSep);
std::replace(address.begin(), address.end(), ',', '.');
int cmdDataSep = valueSep + 1;
valueSep = arg.find(',', cmdDataSep);
std::string p1 = arg.substr(cmdDataSep, valueSep - cmdDataSep);
cmdDataSep = valueSep + 1;
std::string p2 = arg.substr(cmdDataSep, std::string::npos);
std::string port = std::to_string(stoi(p1) * 256 + stoi(p2));
//InfoLog::outputLine(InfoLog::INFO,
// (address + " " + port + " " + std::to_string(_fd) + "\n").c_str());
dataFd = server->openConnection(address, port);
if (dataFd != -1) {
sendStdString(replies[8]);
} else {
sendStdString("425 Socket creation failed.");
}
}
void ftpConnection::cmdMkd(std::string const& arg) {
#ifdef NXDK
std::string fileName = arg;
if (arg[0] != '/') {
fileName = pwd + arg;
}
fileName = unixToDosPath(fileName);
if (CreateDirectoryA(fileName.c_str(), NULL)) {
sendStdString(replies[12]);
} else {
sendStdString(replies[13]);
}
#else
cmdUnimplemented("MKD");
#endif
}
void ftpConnection::cmdRmd(std::string const& arg) {
#ifdef NXDK
std::string fileName = arg;
if (arg[0] != '/') {
fileName = pwd + arg;
}
fileName = unixToDosPath(fileName);
if (RemoveDirectoryA(fileName.c_str())) {
sendStdString(replies[12]);
} else {
sendStdString(replies[13]);
}
#else
cmdUnimplemented("RMD");
#endif
}
void ftpConnection::cmdRnfr(std::string const& arg) {
#ifdef NXDK
if (arg[0] != '/') {
rnfr = pwd + arg;
} else {
rnfr = arg;
}
sendStdString("350 File action pending further information.\r\n");
#else
cmdUnimplemented("RNFR");
#endif
}
void ftpConnection::cmdRnto(std::string const& arg) {
std::string fileName = arg;
if (arg[0] != '/') {
fileName = pwd + arg;
}
#ifdef NXDK
if (MoveFileA(rnfr.c_str(), fileName.c_str())) {
sendStdString(replies[12]);
} else {
sendStdString(replies[13]);
}
rnfr = "";
#else
cmdUnimplemented("RNTO");
#endif
}
void ftpConnection::cmdList(std::string const&) {
if (dataFd != -1) {
sendStdString(replies[9]);
sendFolderContents(dataFd, pwd);
close(dataFd);
sendStdString(replies[10]);
}
}
void ftpConnection::cmdNlst(std::string const&) {
sendStdString(replies[9]);
sendFolderContents(_fd, pwd);
sendStdString(replies[10]);
}
void ftpConnection::cmdEprt(std::string const& arg) {
int family = std::stoi(arg.substr(1, 1));
if (family != 1 && family != 2) {
sendStdString("502 Unknown address family; use (1,2)\r\n");
return;
}
char delimiter = arg[0];
int portDelimiter = arg.find(delimiter, 3);
std::string address = arg.substr(3, arg.find(delimiter, portDelimiter) - 3);
++portDelimiter;
std::string port = arg.substr(portDelimiter,
arg.find(delimiter, portDelimiter) - portDelimiter);
dataFd = server->openConnection(address, port);
if (dataFd != -1) {
sendStdString(replies[8]);
} else {
sendStdString("425 Socket creation failed.");
}
}
void ftpConnection::cmdRetr(std::string const& arg) {
if (dataFd != -1) {
std::string fileName = arg;
if (arg[0] != '/') {
fileName = pwd + arg;
}
//InfoLog::outputLine(InfoLog::DEBUG, "Trying to send file %s!\n", fileName.c_str());
sendStdString("150 Sending file " + arg + "\r\n");
sendFile(fileName);
close(dataFd);
sendStdString(replies[10]);
}
}
void ftpConnection::cmdStor(std::string const& arg) {
if (dataFd != -1) {
std::string fileName = arg;
if (arg[0] != '/') {
fileName = pwd + arg;
}
//InfoLog::outputLine(InfoLog::DEBUG, "Trying to receive file %s!\n", fileName.c_str());
sendStdString("150 Receiving file " + arg + "\r\n");
recvFile(fileName);
close(dataFd);
sendStdString(replies[10]);
}
}
void ftpConnection::cmdExecute(std::string const& path) {
XLaunchXBE(const_cast<char*>(path.c_str()));
}
void ftpConnection::cmdUnimplemented(std::string const& arg) {
sprintf(buf, replies[4].c_str(), arg.c_str());
sendStdString(buf);
}
std::string ftpConnection::unixToDosPath(std::string const& path) {
std::string ret;
if (path[0] == '/' && path[1] == '/') {
ret = path.substr(1, std::string::npos);
} else {
ret = path;
}
ret = ret.substr(1, 1) + ":" + path.substr(2, std::string::npos);
std::replace(ret.begin(), ret.end(), '/', '\\');
if (ret[1] == 'D') {
ret.replace(1, 2, "Device\\CdRom0");
}
return ret;
}
void ftpConnection::sendFolderContents(int fd, std::string& path) {
if (path[0] == '/' && path[1] == '/') {
path = path.substr(1, std::string::npos);
}
if (!path.compare("/")) {
for (size_t i = 0; i < drives.length(); ++i) {
if (nxIsDriveMounted(drives[i])) {
sendStdString(fd, "drwxr-xr-x 1 XBOX XBOX 0 2020-03-02 10:41 " + drives.substr(i, 1)
+ "\r\n");
}
}
return;
}
#ifdef NXDK
WIN32_FIND_DATAA fData;
std::string searchmask = unixToDosPath(path + "*");
HANDLE fHandle = FindFirstFileA(searchmask.c_str(), &fData);
if (fHandle == INVALID_HANDLE_VALUE) {
return;
}
do {
std::string retstr = "";
if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
retstr = "d";
} else {
retstr = "-";
}
TIME_FIELDS fTime;
RtlTimeToTimeFields((PLARGE_INTEGER)&fData.ftLastWriteTime, &fTime);
retstr += "rwxr-xr-x 1 XBOX XBOX " + std::to_string(fData.nFileSizeLow) + " "
+ std::to_string(fTime.Year) + "-" + std::to_string(fTime.Month) + "-"
+ std::to_string(fTime.Day) + " " + std::to_string(fTime.Hour) + ":"
+ std::to_string(fTime.Minute) + " " + fData.cFileName + "\r\n";
sendStdString(fd, retstr);
} while (FindNextFile(fHandle, &fData) != 0);
FindClose(fHandle);
#else
for (int q = 0; q < 10; ++q) {
std::string retstr = "drwxr-xr-x 1 XBOX XBOX " + std::to_string(q) + " May 11 10:41 "
+ std::to_string(q) + "\r\n";
sendStdString(fd, retstr);
}
std::string retstr = "-rwxr-xr-x 1 XBOX XBOX 1024 May 11 10:41 X\r\n";
sendStdString(fd, retstr);
#endif
}
bool ftpConnection::sendFile(std::string const& fileName) {
#ifdef NXDK
std::string filePath = unixToDosPath(fileName);
HANDLE fHandle = CreateFile(filePath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//InfoLog::outputLine(InfoLog::DEBUG, ("\n" + filePath + "\n").c_str());
if (fHandle == INVALID_HANDLE_VALUE) {
//InfoLog::outputLine(InfoLog::ERROR, "Failed to open %s. %x.\n", filePath.c_str(),
// GetLastError());
return false;
}
unsigned char* sendBuffer = (unsigned char*)malloc(FTP_DATA_BUFFER_SIZE);
if (sendBuffer == nullptr) {
//InfoLog::outputLine(InfoLog::ERROR, "File sending buffer memory allocation failed.\n");
return false;
}
int bytesToRead = FTP_DATA_BUFFER_SIZE;
unsigned long bytesRead = 0;
if (mode == 'I') {
while (ReadFile(fHandle, sendBuffer, bytesToRead, &bytesRead, NULL) && (bytesRead > 0)) {
send(dataFd, sendBuffer, bytesRead, 0);
}
} else if (mode == 'A') {
std::string abuf;
while (ReadFile(fHandle, sendBuffer, bytesToRead, &bytesRead, NULL) && (bytesRead > 0)) {
abuf = reinterpret_cast<char*>(sendBuffer);
std::for_each(abuf.begin(), abuf.begin() + bytesRead, [](char& c) { c &= 0x7F; });
send(dataFd, abuf.c_str(), bytesRead, 0);
}
}
CloseHandle(fHandle);
free(sendBuffer);
return true;
#else
char trash[1024];
int bytesRead = 1024;
send(dataFd, trash, bytesRead, 0);
return true;
#endif
}
bool ftpConnection::recvFile(std::string const& fileName) {
bool retVal = true;
std::string filePath = unixToDosPath(fileName);
#ifdef NXDK
HANDLE fHandle = CreateFile(filePath.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if (fHandle == INVALID_HANDLE_VALUE) {
//InfoLog::outputLine(InfoLog::ERROR, "Failed to create file %s. %x\n", filePath.c_str(),
//GetLastError());
return false;
}
#endif
unsigned char* recvBuffer = (unsigned char*)malloc(FTP_DATA_BUFFER_SIZE);
if (!recvBuffer) {
//InfoLog::outputLine(InfoLog::ERROR, "Could not create buffer for file receiving! \n");
return false;
}
//InfoLog::outputLine(InfoLog::DEBUG, ("\r\n" + filePath + "\r\n").c_str());
unsigned long bytesWritten;
ssize_t bytesRead;
while ((bytesRead = recv(dataFd, recvBuffer, FTP_DATA_BUFFER_SIZE, 0))) {
if (bytesRead < 0) {
//InfoLog::outputLine(InfoLog::ERROR, "Error %d, aborting!\n", errno);
retVal = false;
break;
}
#ifdef NXDK
WriteFile(fHandle, recvBuffer, bytesRead, &bytesWritten, NULL);
if (bytesWritten != static_cast<unsigned long>(bytesRead)) {
//InfoLog::outputLine(InfoLog::ERROR, "ERROR: Bytes read != Bytes written (%d, %d)\n",
//bytesRead, bytesWritten);
retVal = false;
}
#endif
}
#ifdef NXDK
CloseHandle(fHandle);
#endif
free(recvBuffer);
return retVal;
}