-
Notifications
You must be signed in to change notification settings - Fork 2
/
ftpServer.py
545 lines (406 loc) · 15.2 KB
/
ftpServer.py
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
#***********************#
# Elias Sepuru #
# Boikanyo Radiokana #
# FTP Server #
# 07-May-2019 #
#***********************#
import socket
import os
import time
import threading
import math
class serverThread(threading.Thread):
def __init__(self, conn, addr,usersDB, currDir,IP, port):
threading.Thread.__init__(self)
self.conn = conn
self.addr = addr
self.serverIP = IP
self.serverPort = port
self.baseWD = currDir
self.cwd = self.baseWD
self.rest = False
self.PASVmode = False
self.isLoggedIn = False
self.users = usersDB
self.validUser = False
self.isConnected = True
self.islist = False
self.mode = 'I' #Default Mode
self.allowDelete = True
def run(self):
self.isConnected = True
# Welcome Message
resp = '220 Welcome!'
self.sendReply(resp)
# Await for connection from clients
while True:
cmd = self.conn.recv(256).decode()
if not cmd or not self.isConnected : break
else:
print('Recieved: ', cmd)
try:
func = getattr(self,cmd[:4].strip().upper())
func(cmd)
except Exception as err:
print('Error: ', err)
resp = '500 Syntax error, command unrecognized.'
self.sendReply(resp)
self.conn.close()
def sendReply(self,reply):
self.conn.send((reply + '\r\n').encode())
def notLoggedInMSG(self):
res = '530 Please login with USER and PASS.'
self.sendReply(res)
def paramError(self,cmd):
res = '501 \'' + cmd[:-2] + '\': parameter not understood.'
self.sendReply(res)
def resetState(self):
# RESET STATE of affairs
self.isLoggedIn = False
self.validUser = False
self.user = None
def SYST(self,cmd):
resp = '215 UNIX Type: L8.'
self.sendReply(resp)
def USER(self,cmd):
#RESET STATE, Incase someone logs in while the other is still logged in
self.resetState()
# Extract username in the command
self.user = cmd[5:-2]
# Read users file
users = open(self.users, 'r').read()
# Check if user exists on the database
for u in users.split('\n'):
if self.user == u.split(' ')[0] and len(u.split(' ')[0]) != 0:
self.validUser = True
resp = '331 User name okay, need password.'
self.sendReply(resp)
break
if not self.validUser:
resp = '530 Invalid User.'
self.sendReply(resp)
self.validUser = False
def PASS(self,cmd):
# Check if user name is entered
if self.validUser:
password = cmd[5:-2]
pws = open(self.users, 'r').read()
# Check if password matches user
for p in pws.split('\n'):
if len(p.split(' ')[0]) != 0:
if password == p.split(' ')[1] and self.user == p.split(' ')[0]:
self.isLoggedIn = True
resp = '230 User logged in, proceed.'
self.sendReply(resp)
break
if not self.isLoggedIn:
resp = '530 Invalid password for ' + self.user
self.sendReply(resp)
else:
self.notLoggedInMSG()
def QUIT(self,cmd):
# If the user is logged in, they are logged out
if self.isLoggedIn:
self.resetState()
resp = '221 Logged out'
self.sendReply(resp)
else:
resp = '221 Service closing control connection'
self.sendReply(resp)
self.isConnected = False
def STRU(self,cmd):
# Obsolete command
stru = cmd[5]
if stru == 'F':
resp = '200 F.'
else:
resp = '504 Command obsolete'
self.sendReply(resp)
def MODE(self,cmd):
# Obsolete command
mode = cmd[5]
if mode == 'S':
resp = '200 MODE set to stream.'
else:
resp = '504 Command obsolete'
self.sendReply(resp)
def NOOP(self,cmd):
# To check if the connection is alive
resp = '200 OK.'
self.sendReply(resp)
def TYPE(self,cmd):
# ASCII or Binary Mode
mode = cmd[5]
# Confirm I or A
if mode.upper() == 'I':
self.mode = mode
resp = '200 Binary mode.'
self.sendReply(resp)
elif mode.upper() == 'A':
self.mode = mode
resp = '200 ASCII mode.'
self.sendReply(resp)
else:
# Unknown parameter
self.paramError(cmd)
def PWD(self,cmd):
# Cant't print working directory if not looged in
if self.isLoggedIn:
# The path relative to the root
tempDir = '/' + self.cwd
cwd = os.path.relpath(tempDir,'/')
if cwd == '.':
cwd = '/'
else:
cwd = '/' + cwd
resp = '257' + ' "' + cwd + '" is the current dir.'
self.sendReply(resp)
else:
self.notLoggedInMSG()
def CWD(self,cmd):
if self.isLoggedIn:
# Get the directory
chwd = cmd[4:-2]
# Base directory?
if chwd == '.' or chwd == '/':
self.cwd = self.baseWD
resp = '250 OK.'
self.sendReply(resp)
else:
# Consider /dir or dir
if chwd[0] == '/':
chwd = chwd[1:]
tempCwd = os.path.join(self.cwd, chwd)
# Does the path exist?
if os.path.exists(tempCwd):
self.cwd = tempCwd
resp = '250 OK.'
self.sendReply(resp)
else:
resp = '550 The system cannot find the file specified.'
self.sendReply(resp)
else:
self.notLoggedInMSG()
def PASV(self,cmd):
# Cant't try to establish connection without logging in
if self.isLoggedIn:
self.PASVmode = True
self.serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.serverSocket.bind((self.serverIP,0))
self.serverSocket.listen(1)
ip, port = self.serverSocket.getsockname()
# Condition IP with the RFC959 standard
ip = ip.split('.')
ip = ','.join(ip)
# Condition the port with the RFC959 standard
p1 = math.floor(port/256)
p2 = port%256
print('open...\nIP: ' + str(ip) +'\nPORT: '+ str(port))
# Prepare the connection settings for take-off
resp = '227 Entering Passive Mode (' + str(ip) + ',' + str(p1) + ',' +str(p2) + ').'
self.sendReply(resp)
else:
self.notLoggedInMSG()
def PORT(self,cmd):
# Cant't try to establish connection without logging in
if self.isLoggedIn:
# check if Passive Mode
if self.PASVmode:
self.serverSocket.close()
self.PASVmode = False
# Split the connection settings
conSettings = cmd[5:].split(',')
# Generate the IP address from the connection settings
self.DTPaddr = '.'.join(conSettings[:4])
# Generate the PORT from the connection settings
# This is with respect to RFC959
self.DTPport = ((int(conSettings[4])<<8)) + int(conSettings[5])
print('Connected to :', self.DTPaddr, self.DTPport)
# Acknowledge
resp = '200 Got it.'
self.sendReply(resp)
self.DTPsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.DTPsocket.connect((self.DTPaddr,self.DTPport))
else:
self.notLoggedInMSG()
def startDTPsocket(self):
try:
if self.PASVmode:
self.DTPsocket, addr = self.serverSocket.accept()
print('connect: ', addr)
except socket.error:
resp = '425 Cannot open Data Connection'
self.sendReply(resp)
def stopDTPsocket(self):
self.DTPsocket.close()
if self.PASVmode:
self.serverSocket.close()
def sendData(self, data):
# Mode of sending?
if not self.islist and self.mode == 'I':
self.DTPsocket.send((data))
else:
self.DTPsocket.send((data+'\r\n').encode())
def LIST(self,cmd):
# Can't list if not logged in
if self.isLoggedIn:
resp = '150 File status okay; about to open data connection.'
self.sendReply(resp)
print('list: ', self.cwd)
# Ready the socket for data transfer
self.startDTPsocket()
# Get each file in the directory
for l in os.listdir(self.cwd):
ll = self.toList(os.path.join(self.cwd,l))
# Send as str/ASCII
self.islist = True
self.sendData(ll)
self.islist = False
# Done
self.stopDTPsocket()
resp = '200 Listing completed.'
self.sendReply(resp)
else:
self.notLoggedInMSG()
def toList(self,l):
st = os.stat(l)
fullmode ='rwxrwxrwx'
mode = ''
# Prep the directory listing with regards to RFC959
for i in range(9):
mode+=((st.st_mode>>(8-i))&1) and fullmode[i] or '-'
d = (os.path.isdir(l)) and 'd' or '-'
fhist = time.strftime(' %b %d %H:%M ',time.gmtime(st.st_mtime))
return d + mode+ '\t1 user'+'\t group \t\t' + str(st.st_size) + '\t' + fhist + '\t' + os.path.basename(l)
def MKD(self,cmd):
#Can't make new directory if not logged in
if self.isLoggedIn:
dirName = os.path.join(self.cwd,cmd[4:-2])
os.mkdir(dirName)
resp = '257 Directory created.'
self.sendReply(resp)
else:
self.notLoggedInMSG()
def RMD(self,cmd):
# Can't delete directory if not logged in
if self.isLoggedIn:
dirName = os.path.join(self.cwd,cmd[4:-2])
# Check if specified path exists
if os.path.exists(dirName):
# Allow deletion if only deletion is allowed
if self.allowDelete:
os.rmdir(dirName)
resp = '250 Directory deleted.'
self.sendReply(resp)
else:
resp = '450 Not allowed.'
self.sendReply(resp)
else:
resp = '550 The system cannot find the file specified.'
self.sendReply(resp)
else:
self.notLoggedInMSG()
def STOR(self,cmd):
# Cant store files if not logged in
if self.isLoggedIn:
# Create file path
fileName = os.path.join(self.cwd,cmd[5:-2])
print('Uploading: ', fileName)
# Upload mode?
if self.mode == 'I':
oFile = open(fileName,'wb')
else:
oFile = open(fileName, 'w')
resp = '150 Opening data connection.'
self.sendReply(resp)
# Ready the socket for upload
self.startDTPsocket()
# Get the file
while True:
data = self.DTPsocket.recv(8192)
#print(data)
if not data:
break
oFile.write(data)
# Done
self.stopDTPsocket()
resp = '226 Transfer complete.'
self.sendReply(resp)
print('Upload success')
oFile.close()
else:
self.notLoggedInMSG()
def RETR(self,cmd):
# Cant retrieve files if not logged in
if self.isLoggedIn:
fileName = os.path.join(self.cwd, cmd[5:-2])
# For Filezilla
if fileName[0] == '/':
fileName = fileName[1:]
# Check if file exist
if os.path.exists(fileName):
print('Downloading :', fileName)
# Mode?
if self.mode == 'I':
rFile = open(fileName, 'rb')
else:
rFile = open(fileName, 'r')
# Open data connection
resp = '150 Opening file data connection.'
self.sendReply(resp)
data = rFile.read(8192)
self.startDTPsocket()
# Send the file
while data:
self.sendData(data)
data = rFile.read(8192)
rFile.close()
self.stopDTPsocket()
resp = '226 Transfer complete.'
self.sendReply(resp)
else:
# File does not exist
resp = '550 The system cannot find the file specified.'
self.sendReply(resp)
else:
self.notLoggedInMSG()
class FTPserver(threading.Thread):
# The lookout class, waits for contact from client
def __init__(self,usersDB,homeDir,IP,Port):
self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.serverIP = IP
self.serverPort = Port
self.sock.bind((self.serverIP, self.serverPort))
self.usersDB = usersDB
self.homeDir = homeDir
threading.Thread.__init__(self)
def run(self):
self.sock.listen(5)
while True:
connectionSocket, addr = self.sock.accept()
thread = serverThread(connectionSocket, addr,self.usersDB, self.homeDir,self.serverIP,self.serverPort)
thread.daemon = True
thread.start()
def stop(self):
self.sock.close()
def Main():
serverPort = 21
#! For some reason this now gives localhost only
#! serverIP = socket.gethostbyname(socket.gethostname())
#* New method
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.connect(("8.8.8.8", 80))
serverIP = server.getsockname()[0]
# Database for users
users = './users.txt'
# Default directory
homeDir = '../'
# Make new thread for each new connection
cThread = FTPserver(users,homeDir,serverIP,serverPort)
cThread.daemon = True
cThread.start()
# Wait for contact
print('FTP-Application running on', serverIP, ':', serverPort)
input('Enter to end...\n')
cThread.stop()
Main()