forked from billywhizz/nodeftpd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ftpd.js
542 lines (520 loc) · 15.3 KB
/
ftpd.js
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
var net = require("net");
var sys = require("sys");
var posix = require("fs");
//var file = require("file");
var dummyfs = require("./dummyfs");
/*
TODO:
- Implement Full RFC 959
- Implement RFC 2428
- Implement RFC 2228
- Implement RFC 3659
- Implement TLS - http://en.wikipedia.org/wiki/FTPS
*/
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
// For some reason, the FTP server
function dotrace(traceline)
{
// sys.puts(traceline);
}
function createServer(host)
{
var server = net.createServer(function (socket) {
socket.setTimeout(0);
socket.setEncoding("ascii"); // force data String not Buffer
socket.setNoDelay();
socket.passive = false;
socket.pasvport = 0;
socket.pasvaddress = "";
socket.mode = "ascii";
socket.filefrom = "";
socket.username = "";
socket.datatransfer = null;
socket.totsize = 0;
socket.filename = "";
socket.fs = new dummyfs.dummyfs();
dotrace("CWD = "+socket.fs.cwd());
socket.addListener("connect", function () {
dotrace("CMD connect");
//socket.send("220 NodeFTPd Server version 0.0.10\r\n");
//socket.write("220 written by Andrew Johnston ([email protected])\r\n");
//socket.write("220 Please visit http://github.com/billywhizz/NodeFTPd\r\n");
socket.write("220 FTP server (nodeftpd) ready\r\n");
});
socket.addListener("data", function (data) {
dotrace("CMD data");
dotrace("CMD " + data.trim());
var command = data.split(" ");
switch(command[0].trim().toUpperCase())
{
case "ABOR":
// Abort an active file transfer.
socket.write("202 Not supported\r\n");
break;
case "ACCT":
// Account information
socket.write("202 Not supported\r\n");
break;
case "ADAT":
// Authentication/Security Data (RFC 2228)
socket.write("202 Not supported\r\n");
break;
case "ALLO":
// Allocate sufficient disk space to receive a file.
socket.write("202 Not supported\r\n");
break;
case "APPE":
// Append.
socket.write("202 Not supported\r\n");
break;
case "AUTH":
// Authentication/Security Mechanism (RFC 2228)
socket.write("202 Not supported\r\n");
break;
case "CCC":
// Clear Command Channel (RFC 2228)
socket.write("202 Not supported\r\n");
break;
case "CDUP":
// Change to Parent Directory.
socket.write("250 Directory changed to " + socket.fs.chdir("..") + "\r\n");
break;
case "CONF":
// Confidentiality Protection Command (RFC 697)
socket.write("202 Not supported\r\n");
break;
case "CWD":
// Change working directory.
socket.write("250 CWD successful. \"" + socket.fs.chdir(command[1].trim()) + "\" is current directory\r\n");
break;
case "DELE":
// Delete file.
// same problem again with size, repeating paths
var filename = command[1].trim();
rn = sys.exec("rm -f " + filename, function(err, stdout, stderr) {
if(err) {
dotrace("Error delting file: "+filename+", "+err);
}
socket.write("250 file deleted\r\n");
});
break;
case "ENC":
// Privacy Protected Channel (RFC 2228)
socket.write("202 Not supported\r\n");
break;
case "EPRT":
// Specifies an extended address and port to which the server should connect. (RFC 2428)
socket.write("202 Not supported\r\n");
break;
case "EPSV":
// Enter extended passive mode. (RFC 2428)
socket.write("202 Not supported\r\n");
break;
case "FEAT":
// Get the feature list implemented by the server. (RFC 2389)
socket.write("211-Features\r\n");
socket.write(" SIZE\r\n");
socket.write("211 end\r\n");
break;
case "HELP":
// Returns usage documentation on a command if specified, else a general help document is returned.
/*
214-The following commands are recognized:
USER PASS QUIT CWD PDD PORT PASV TYPE
LIST REST CDUP RETR STOR SIZE DELE RMD
MKD RNFR RNTO ABOR SYST NOOP APPE NLST
MDTM XPWD XCUP XMKD XRMD NOP EPSV EPRT
AUTH ADAT PBSZ PROT FEAT MODE OPTS HELP
ALLO MLST MLSD SITE P@SW STRU CLNT MFMT
214 Have a nice day.
*/
socket.write("202 Not supported\r\n");
break;
case "LANG":
// Language Negotiation (RFC 2640)
socket.write("202 Not supported\r\n");
break;
case "LIST":
// Returns information of a file or directory if specified, else information of the current working directory is returned.
socket.datatransfer = function(pasvconn) {
pasvconn.addListener("connect", function () {
socket.write("150 Connection Accepted\r\n");
dotrace("DATA connect");
ls_cmd = "ls -l " + socket.fs.cwd();
dotrace(ls_cmd);
ls = sys.exec(
ls_cmd,
function (err, stdout, stderr) {
if(err) {
pasvconn.write("");
}
else {
pasvconn.write(stdout);
}
pasvconn.close();
socket.write("226 Transfer OK\r\n");
});
});
};
if(!socket.passive){
socket.datatransfer(net.createConnection(socket.pasvport, socket.pasvhost));
}
break;
case "LPRT":
// Specifies a long address and port to which the server should connect. (RFC 1639)
socket.write("202 Not supported\r\n");
break;
case "LPSV":
// Enter long passive mode. (RFC 1639)
socket.write("202 Not supported\r\n");
break;
case "MDTM":
// Return the last-modified time of a specified file. (RFC 3659)
socket.write("202 Not supported\r\n");
break;
case "MIC":
// Integrity Protected Command (RFC 2228)
socket.write("202 Not supported\r\n");
break;
case "MKD":
// Make directory.
socket.write("202 Not supported\r\n");
break;
case "MLSD":
// Lists the contents of a directory if a directory is named. (RFC 3659)
socket.write("202 Not supported\r\n");
break;
case "MLST":
// Provides data about exactly the object named on its command line, and no others. (RFC 3659)
socket.write("202 Not supported\r\n");
break;
case "MODE":
// Sets the transfer mode (Stream, Block, or Compressed).
socket.write("202 Not supported\r\n");
break;
case "NLST":
// Returns a list of file names in a specified directory.
socket.datatransfer = function(pasvconn) {
pasvconn.addListener("connect", function () {
socket.write("150 Connection Accepted\r\n");
dotrace("DATA connect");
ls_cmd = "ls -l " + socket.fs.cwd();
dotrace(ls_cmd);
ls = sys.exec(
ls_cmd,
function (err, stdout, stderr) {
if(err) {
pasvconn.write("");
}
else {
pasvconn.write(stdout);
}
pasvconn.close();
socket.write("226 Transfer OK\r\n");
});
});
};
if(!socket.passive){
socket.datatransfer(net.createConnection(socket.pasvport, socket.pasvhost));
}
break;
case "NOOP":
// No operation (dummy packet; used mostly on keepalives).
socket.write("202 Not supported\r\n");
break;
case "OPTS":
// Select options for a feature. (RFC 2389)
socket.write("202 Not supported\r\n");
break;
case "PASS":
// Authentication password.
socket.write("230 Logged on\r\n");
break;
case "PASV":
// Enter passive mode.
socket.passive = true;
socket.pasvhost = host;
socket.pasvport = 0;
var pasv = net.createServer(function (psocket) {
psocket.addListener("connect", function () {
socket.datatransfer(psocket);
});
psocket.addListener("close", function () {
dotrace("DATA close");
});
psocket.addListener("eof", function () {
dotrace("DATA eof");
});
psocket.addListener("error", function(had_error) {
dotrace("DATA error: " + had_error);
});
});
pasv.addListener("listening", function(port) {
socket.pasvport = port;
var i1 = parseInt(port / 256);
var i2 = parseInt(port % 256);
socket.write("227 Entering Passive Mode (" + host.split(".").join(",") + "," + i1 + "," + i2 + ")\r\n");
});
pasv.listen(0, host);
break;
case "PBSZ":
// Protection Buffer Size (RFC 2228)
socket.write("202 Not supported\r\n");
break;
case "PORT":
// Specifies an address and port to which the server should connect.
socket.passive = false;
var addr = command[1].split(",");
socket.pasvhost = addr[0]+"."+addr[1]+"."+addr[2]+"."+addr[3];
socket.pasvport = (parseInt(addr[4]) * 256) + parseInt(addr[5]);
socket.write("200 PORT command successful.\r\n");
break;
case "PWD":
// Print working directory. Returns the current directory of the host.
socket.write("257 \"" + socket.fs.cwd() + "\" is current directory\r\n");
break;
case "QUIT":
// Disconnect.
socket.write("221 Goodbye\r\n");
socket.close();
break;
case "REIN":
// Re initializes the connection.
socket.write("202 Not supported\r\n");
break;
case "REST":
// Restart transfer from the specified point.
socket.totsize = parseInt(command[1].trim());
socket.write("350 Rest supported. Restarting at " + socket.totsize + "\r\n");
break;
case "RETR":
// Retrieve (download) a remote file.
socket.datatransfer = function(pasvconn) {
pasvconn.setEncoding(socket.mode);
pasvconn.addListener("connect", function () {
socket.write("150 Connection Accepted\r\n");
dotrace("DATA connect");
// again, same issue with size regarding repeating path, eg:
// /path/to/file//path/to/file/name.txt
filename = command[1].trim();
if(command[1].trim() != socket.filename) socket.totsize = 0;
socket.filename = command[1].trim();
posix.open(socket.filename,process.O_RDONLY, 0666, function (err, fd) {
dotrace("DATA file " + socket.filename + " opened");
function readChunk() {
posix.read(fd, 4096, socket.totsize, socket.mode, function(err, chunk, bytes_read) {
if(err) {
dotrace("Erro reading chunk");
throw err;
return;
}
if(chunk) {
socket.totsize += bytes_read;
if(pasvconn.readyState == "open") pasvconn.write(chunk, socket.mode);
readChunk();
}
else {
dotrace("DATA file " + socket.filename + " closed");
pasvconn.close();
socket.write("226 Closing data connection, sent " + socket.totsize + " bytes\r\n");
posix.close(fd);
socket.totsize = 0;
}
});
}
if(err) {
dotrace("Error at read");
throw err;
}
else {
readChunk();
}
});
});
pasvconn.addListener("close", function () {
dotrace("DATA close");
});
pasvconn.addListener("end", function () {
dotrace("DATA eof");
});
pasvconn.addListener("error", function(had_error) {
dotrace("DATA error: " + had_error);
});
};
if(!socket.passive){
socket.datatransfer(net.createConnection(socket.pasvport, socket.pasvhost));
}
break;
case "RMD":
// Remove a directory.
socket.write("202 Not supported\r\n");
break;
case "RNFR":
// Rename from.
socket.filefrom = socket.fs.cwd() + command[1].trim();
dotrace("Rename from "+socket.filefrom);
socket.write("350 File exists, ready for destination name.\r\n");
break;
case "RNTO":
// Rename to.
// as ever, problem with repeat paths
var fileto = socket.fs.cwd() + command[1].trim();
var mv_cmd = "mv " + socket.filefrom + " " + fileto
rn = sys.exec(mv_cmd, function(err, stdout, stderr) {
if(err) {
dotrace("Error renaming file from "+socket.filefrom+" to "+fileto);
}
socket.write("250 file renamed successfully\r\n");
});
break;
case "SITE":
// Sends site specific commands to remote server.
socket.write("202 Not supported\r\n");
break;
case "SIZE":
// Return the size of a file. (RFC 3659)
// for some reason, I am getting a repeated file path within
// cwd(0 and the command, resulting in an error
//var filename = socket.fs.cwd() + command[1].trim();
var filename = command[1].trim();
posix.stat(filename, function (err, s) {
if(err) {
dotrace("Error getting size of file: "+filename);
throw err;
}
socket.write("213 " + s.size + "\r\n");
});
break;
case "SMNT":
// Mount file structure.
socket.write("202 Not supported\r\n");
break;
case "STAT":
// Returns the current status.
/* from FileZilla
Connected to 192.168.2.100.
No proxy connection.
Mode: stream; Type: ascii; Form: non-print; Structure: file
Verbose: on; Bell: off; Prompting: on; Globbing: on
Store unique: off; Receive unique: off
Case: off; CR stripping: on
Ntrans: off
Nmap: off
Hash mark printing: off; Use of PORT cmds: on
Tick counter printing: off
*/
socket.write("202 Not supported\r\n");
break;
case "STOR":
// Store (upload) a file.
socket.datatransfer = function(pasvconn) {
dotrace(pasvconn);
pasvconn.setEncoding(socket.mode);
filename = command[1].trim();
posix.open(filename, 'w', 0644, function(err, fd) {
if(err) {
dotrace('Error opening file: '+filename);
throw err;
}
var size = 0;
var paused = false;
var npauses = 0;
pasvconn.addListener("data", function(data) {
size += data.length;
posix.write(fd, data, null, socket.mode, function(err, bytes_written) {
if(err) {
dotrace("Error writing file");
throw err;
}
else {
dotrace("Bytes written: "+bytes_written);
}
if (!paused) {
pasvconn.pause();
npauses += 1;
paused = true;
setTimeout(function () {
pasvconn.resume();
paused = false;
}, 1);
}
});
});
pasvconn.addListener("connect", function () {
dotrace("DATA connect");
socket.write("150 Connection Accepted\r\n");
});
pasvconn.addListener("close", function () {
dotrace("DATA close");
});
pasvconn.addListener("end", function () {
posix.close(fd, function(err) {
dotrace("Error closing file: "+fd);
//throw err;
// always seems to throw an error??
});
dotrace("DATA eof");
socket.write("226 Closing data connection, recv " + size + " bytes\r\n");
});
pasvconn.addListener("error", function(had_error) {
dotrace("DATA error: " + had_error);
});
});
}
if(!socket.passive){
socket.datatransfer(net.createConnection(socket.pasvport, socket.pasvhost));
}
break;
case "STOU":
// Store file uniquely.
socket.write("202 Not supported\r\n");
break;
case "STRU":
// Set file transfer structure.
socket.write("202 Not supported\r\n");
break;
case "SYST":
// Return system type.
socket.write("215 UNIX emulated by NodeFTPd\r\n");
break;
case "TYPE":
// Sets the transfer mode (ASCII/Binary).
if(command[1].trim() == "A"){
socket.mode = "ascii";
socket.write("200 Type set to A\r\n");
}
else{
socket.mode = "binary";
socket.write("200 Type set to I\r\n");
}
break;
case "USER":
// Authentication username.
socket.username = command[1].trim();
socket.write("331 password required for " + socket.username + "\r\n");
break;
case "XPWD":
//
socket.write("257 " + socket.fs.cwd() + " is the current directory\r\n");
break;
default:
socket.write("202 Not supported\r\n");
break;
}
});
socket.addListener("end", function () {
dotrace("CMD end");
socket.close();
});
socket.addListener("close", function () {
dotrace("CMD close");
});
});
return server;
}
sys.inherits(createServer, process.EventEmitter);
// for testing
//createServer("localhost").listen(7001);
exports.createServer = createServer;