-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
executable file
·587 lines (474 loc) · 15.1 KB
/
Server.py
File metadata and controls
executable file
·587 lines (474 loc) · 15.1 KB
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
# Author: Christian Nell
# Onyen: cnell
# PID: 7302-29326
# Date: 8/11/20
# Purpose: Takes one command line argument which is the port number to listen for connections on.
# Once client connections to the servers socket, a three way handshake is made between the
# server and client. Then server parses the clients messages and makes sure they follow protocol.
# Once a correctly formated message is accepted by this server, if there is a foward directory in the same
# directory as the server file, the server will create a file in the foward directly with the From: domain.
# It will be the same format as the client's CLI input.
#
# UNC Honor Pledge: I certify that no unauthorized assistance has been received or
# given in the completion of this work
# Signature: _Christian Nell__
import sys
import shutil
from socket import *
rcpts = []
mailboxs = []
datas = []
#############################################################
############## MAIL FROM ##############
#############################################################
def mail_from_cmd(string):
# checks MAIL FROM: command
string = check_mail_from(string)
# <nullspace>
string = nullspace(string)
# <reverse-path>
string = reverse_path(string)
if(not(string)):
return False
# <nullspace>
string = nullspace(string)
if(not(string)):
return False
# <CLRF>
return CRLF(string)
def check_mail_from(string):
mailString = "MAIL"
fromString = "FROM:"
# MAIL
mail = string[0:4]
if(not(mailString == mail)):
return False
string = string[4:]
# <whitespace>
if(SP(string) == False):
return False
string = whitespace(string)
# "FROM:"
if(not(fromString == string[0:5])):
return False
string = string[5:]
return string
def whitespace(string):
# <SP> | <SP> <whitespace>
if(SP(string) == False):
return string[0:]
string = SP(string)
return whitespace(string)
def SP(string):
# the space or tab char
if(string[0] == ' ' or string[0] == '\t'):
return string[1:]
elif(string[0] == '\\'):
if(string[1] == 't'):
return string[2:]
return False
def nullspace(string):
# <null> | <whitespace>
if(null(string[0])):
return string
return whitespace(string)
def null(c):
# no character
if((c != ' ') or (c != '\t')):
return False
return True
def reverse_path(string):
# <path>
return path(string)
def path(string):
# "<"
if(string[0] != '<'):
return False
# <mailbox>
string = mailbox(string[1:])
if(string == False):
return False
# ">"
if(string[0] != '>'):
return False
return string[1:]
def mailbox(string):
# <local-part>
copy = string
string = local_part(string)
if(string == False or (copy[0] == string[0])):
return False
# "@"
if(string[0] != '@'):
return False
# <domain>
string = domain(string[1:])
if(string == False):
return False
return string
def local_part(string):
# <string>
string = string_(string)
return string
def string_(string):
# <char> | <char> <string>
if(char(string) == False):
return string
return string_(string[1:])
def char(string):
# any one of the printable ASCII characters, but not any
# of <special> or <SP>
if(special(string[0]) or SP(string) or not(ord(string[0]) < 128) or CRLF(string[0])):
return False
return True
def domain(string):
# <element> | <element> "." <domain>
string = element(string)
if(string == False):
return False
if(string[0] == '.'):
string = domain(string[1:])
return string
def element(string):
# <letter> | <name>
if(not(letter(string[0]))):
return False
if(name(string) != False):
string = name(string)
elif():
string = string[1:]
return string
def name(string):
# <letter> <let-dig-str>
if(not(letter(string[0]))):
return False
return let_dig_str(string)
def letter(c):
# any one of the 52 alphabetic characters A through Z
# in upper case and a through z in lower case
if c.isalpha():
return True
return False
def let_dig_str(string):
# <let-dig> | <let-dig> <let-dig-str>
if(not(let_dig(string[0]))):
return string
return let_dig_str(string[1:])
def let_dig(c):
# <letter> | <digit>
if(letter(c) or digit(c)):
return True
return False
def digit(c):
# any one of the ten digits 0 through 9
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
if c in digits:
return True
return False
def CRLF(string):
# the newline character
if(string[0] == '\n'):
string = string[1:]
if(len(string) == 0):
return True
return string[1:]
if(string[0] == '\\'):
if(string[1] == 'n'):
return string[2:]
return False
def special(c):
# special list ... shouldn't be in input
special_list = ['<', '>', '(', ')', '[', ']',
'\\', '.', ',', ';', ':', '@', '"']
if c in special_list:
return True
return False
#############################################################
############## RCPT TO ##############
#############################################################
# Makes sure the RCPT TO: syntax is all corect
def rcpt_to(string):
# checks RCPT TO: command
string = check_rcpt_to(string)
# <nullspace>
string = nullspace(string)
# <forward-path>
string = forward_path(string)
if(not(string)):
return False
# <nullspace>
string = nullspace(string)
# <CRLF>
return CRLF(string)
# Makes sure the 'RCPT TO:' command has been typed correctly
def check_rcpt_to(string):
rcptString = "RCPT"
toString = "TO:"
# "RCPT"
if(not(rcptString == string[0:4])):
return False
string = string[4:]
# <whitespace>
if(SP(string) == False):
return False
string = whitespace(string)
# "TO:"
if(not(toString == string[0:3])):
return False
string = string[3:]
return string
def forward_path(string):
return path(string)
#############################################################
############## DATA ##############
#############################################################
# Takes line of input and appends to list of RCPT TO: files
def data(string):
if(string == ".\n"):
return -2
datas.append(string)
return -1
# Makes sure the 'DATA' command has been typed correctly
def check_data(string):
if(string[0:4] != "DATA"):
return False
string = string[4:]
# <nullspace>
string = nullspace(string)
# <CRLF>
return CRLF(string)
# Gets the mailbox of the RCPT TO: to make a file name
def getMailbox(string):
count = 0
while(string[count] != '<'):
count += 1
count += 1
string = string[count:]
count = 0
while(string[count] != '>'):
count += 1
return string[:count]
# Makes a "From: " string ending in the mailbox
def from_(string):
return "From: <" + getMailbox(string) + ">"
# Makes a "To: " string ending in the mailbox
def to(string):
return "To: <" + getMailbox(string) + ">"
# Gets the domain of an address so that it can be used to create a file in the forward directory (called by writeData)
def getDomain(string):
position = 0
while string[position] != '@':
position += 1
return string[position+1:]
# After full correct input, we will write all From, To and Data messages
# to all RCPT TO: files
def writeData(connectionSocket):
already = []
for entry in rcpts:
fileName = getDomain(entry)
if not(fileName in already):
already.append(fileName)
for r in already:
f = open("forward/"+r, "a+")
for d in datas:
f.write(d)
f.close()
ok250(0, connectionSocket)
return 0
# 500 Syntax error
def error500(string, connectionSocket):
message = "500 Syntax error: command unrecognized"
connectionSocket.send(message.encode())
return False
# 501 Syntax error
def error501(string, connectionSocket):
message = "501 Syntax error in parameters or arguments"
connectionSocket.send(message.encode())
return False
# 503 Bad sequence
def error503(string, connectionSocket):
message = "503 Bad sequence of commands"
connectionSocket.send(message.encode())
return False
# 250 OK
def ok250(count, connectionSocket):
message = "250 OK"
try:
connectionSocket.send(message.encode())
except OSError:
print("ERROR sending OK message")
return False
count += 1
return count
# HELO message parse
def heloParse(string, connectionSocket):
# HELO <whitespace> <domain> <nullspace> <CRLF>
iString = string
if(not(string[:4] == 'HELO')):
return error500(iString, connectionSocket)
string = string[4:]
copy = string
string = whitespace(string)
if(copy[0] == string[0]):
return error501(iString, connectionSocket)
findDomain = string
string = domain(string)
if(not(string)):
return error501(iString, connectionSocket)
findDomain = findDomain[:len(findDomain)-len(string)]
string = nullspace(string)
string = CRLF(string)
if(not(string)):
return error501(iString, connectionSocket)
return findDomain
def quitParse(string, connectionSocket):
# <QUIT> ::= “QUIT” <nullspace> <CRLF>
if(not(string[0:4] == 'QUIT')):
return error500(string, connectionSocket)
string = string[4:]
string = nullspace(string)
string = CRLF(string)
if(not(string)):
return error501(string, connectionSocket)
return True
# Selects the correct command to call and throws error if needed
def call_command(string, count, connectionSocket):
passCommand = False
# DATA (store input, then write)
if(count == -1):
copy = data(string)
if(copy == -2):
acceptedString = "250 Message accepted for delivery"
try:
connectionSocket.send(acceptedString.encode())
except OSError:
print("ERROR sending 250 message")
return False
try:
quit_ = connectionSocket.recv(1024).decode()
except OSError:
print("ERROR recv quit message")
return False
if(check_mail_from(quit_) != False):
writeData(connectionSocket)
return call_command(quit_)
quit_ = quitParse(quit_, connectionSocket)
if not(quit_):
return error500("ERROR QUIT", connectionSocket)
writeData(connectionSocket)
return "Done"
return copy
# MAIL FROM:
elif(check_mail_from(string) != False):
if(count != 0):
return error503(string, connectionSocket)
passCommand = mail_from_cmd(string)
if(passCommand != False):
mailboxs.append(from_(string))
if(passCommand != True):
count = ok250(count, connectionSocket)
return call_command(passCommand, count, connectionSocket)
return ok250(count, connectionSocket)
return error501(string, connectionSocket)
# RCPT TO:
elif(check_rcpt_to(string) != False):
if(count < 1):
return error503(string, connectionSocket)
passCommand = rcpt_to(string)
if(passCommand != False):
rcpts.append(getMailbox(string))
mailboxs.append(to(string))
if(passCommand != True):
count = ok250(count, connectionSocket)
return call_command(passCommand, count, connectionSocket)
return ok250(count, connectionSocket)
return error501(string, connectionSocket)
# DATA
elif(check_data(string) != False):
passCommand = check_data(string)
if(count < 2):
return error503(string, connectionSocket)
mailStart = "354 Start mail input; end with <CRLF>.<CRLF>"
try:
connectionSocket.send(mailStart.encode())
except OSError:
print("ERROR sending 354 message")
return False
count = -1
if(passCommand != True):
return call_command(passCommand, count, connectionSocket)
return count
# Unrecognized command
else:
return error500(string, connectionSocket)
def acceptingMessages(connectionSocket):
count = 0
takingMessages = True
datas.clear()
mailboxs.clear()
rcpts.clear()
greeting = "220 comp431fa20.cs.unc.edu\n"
try:
connectionSocket.send(greeting.encode())
except OSError:
print("ERROR sending 220 message")
return False
try:
heloMessage = connectionSocket.recv(1024).decode()
except OSError:
print("ERROR recv HELO message")
return False
test = heloParse(heloMessage, connectionSocket)
if not(test):
return error500("HELO message incorrect", connectionSocket)
heloResponse = "250 Hello" + \
heloMessage[4:len(heloMessage)-1]+" pleased to meet you\n"
try:
connectionSocket.send(heloResponse.encode())
except OSError:
print("ERROR sending 250 hello message")
return False
while takingMessages:
try:
line = connectionSocket.recv(1024).decode()
except OSError:
print("ERROR recv state machine message")
return False
count = call_command(line, count, connectionSocket)
if(count == "Done"):
takingMessages = False
elif(count == False): # False = start over from MAIL FROM command
return False
if(count != "Done"):
return error501("Incomplete data input", connectionSocket)
closeMessage = "221 comp431fa20.cs.unc.edu closing connection\n"
try:
connectionSocket.send(closeMessage.encode())
except OSError:
print("ERROR sending 221 message")
return False
return True
def main():
# Make welcome socket and set port number from command line
try:
serverPort = int(sys.argv[1])
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
# Server begins listening for incoming TCP requests
serverSocket.listen(1)
except OSError:
print("ERROR creating socket")
return False
while True:
try:
connectionSocket, addr = serverSocket.accept()
except OSError:
print("ERROR accepting socket")
return False
acceptingMessages(connectionSocket)
try:
connectionSocket.close() # Close connection to this client
except OSError:
print("ERROR accepting socket")
return False
main()