-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSMain.cpp
More file actions
571 lines (464 loc) · 23.2 KB
/
Copy pathTSMain.cpp
File metadata and controls
571 lines (464 loc) · 23.2 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
//Course: 4050-212-02
//Authors: Alex Buie, Luke Matarazzo, Jackson Sadowski, Steven Tucker
//IDE/editor: Sublime Text 2
//Compilers: mingw32-g++
//Final SMTP Project
//Filename: TSMain.cpp server main
//Purpose:
#include "ThreadSock.h"
#include "ClientSocket.h"
#include "Status.h"
#include <algorithm>
string sSrvrIP; //global variable which holds the IP address of the server
bool isWritten = false; //global boolean which will indicate whether or not we have written to the fifo file
//Name: upCase()
//Parameters: string to be manipulated
//Purpose: makes the entire string uppercase
//Returns: the new string
string upCase(string str)
{
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
return str;
}
//our thread for FIFO reading and message relaying - doesn't need a parameter passed in at this point
DWORD WINAPI relayMail(LPVOID lpParam)
{
cout << "FIFO Thread Created\n";
while(true) //endless loop to open the fifo file and send the email in it
{
string recMessage = ""; //will hold the command the client sent
string sendMessage = ""; //will hold the reply we send
vector<string> message;
string line = ""; //will hold each line we read in from the file
int serverFlop = 0; //will hold value given by recv function and will be -1 if the server flops and shuts down
ifstream fin; //file input object to read stuff in from the message fifo queue
bool isSent = false; //bool that will hold if the message sent to the server successfully
bool isFTS = false;
int timeoutCount = 0; //timeout counter
ClientSocket fifoClient; //create an instance of clientsocket called fifoClient
timeoutCount = 0; //reset the timeout counter
if(isWritten) //if we have ownership of the mutex
{
fin.open("email.fifo"); //open the email.fifo file
if(!fin.is_open())
{
Sleep(100); //wait a little while before trying to open the file again
}
else
{
getline(fin, line); //get the first line from the file
while(line != ".") //while we don't read in a period, keep going. period denotes the end of a message
{
message.push_back(line); //add the lines to the vector that will hold the message
getline(fin, line); //get the next line
}
fin.close(); //close file; done reading stuff in
remove("email.fifo"); //remove the file after we're done with it
isWritten = false; //set isWritten equal to false because now we read in and deleted the file
while(!isSent)
{
timeoutCount++; //adding one to the timeout count.
//connect to the server where the message should be going
if(!fifoClient.connectToServer(message[1].substr(message[1].find("@")+1).c_str(), 31000))
{
ofstream fout;
fout.open("fts.flop", ios::app); //opening the new flop
for (int i =0; i < message.size(); i++) //writing the contents of the vector to the error log
fout << message[i] << endl; //writing the message
fout.close();
isSent = true;
isFTS = true;
}
else
{
//receive the first 220 message
serverFlop = fifoClient.recvData(recMessage);
if(serverFlop == -1) //check if the server flopped
{
fifoClient.closeConnection();
continue;
}
if(recMessage.substr(0,3) == "220")
{
fifoClient.sendData("HELO 127.0.0.1");
}
else
{
fifoClient.closeConnection();
continue;
}
serverFlop = fifoClient.recvData(recMessage); //receive next status message from server
if(serverFlop == -1) //check if the server flopped
{
fifoClient.closeConnection();
continue;
}
if(recMessage.substr(0,3) == "250") //send the login information as long as we got a 250 from the server first
{
//sendMessage = "VRFY " + message[2].substr(0, message[2].find("@"));
sendMessage = "VRFY guest"; //login using guest account
fifoClient.sendData(sendMessage);
}
else //if we can't login, close connection
{
fifoClient.closeConnection();
continue;
}
serverFlop = fifoClient.recvData(recMessage); //receive next status message from server
if(serverFlop == -1) //check if the server flopped
{
fifoClient.closeConnection();
continue;
}
if(recMessage == "550" || recMessage == "500") //if login fails, print error and close connection
{
cout << "Invalid user when trying to login as guest...\n";
fifoClient.closeConnection(); //close connection
continue;
}
//send the message in the fifo queue
sendMessage = "MAIL FROM:<" + message[2] + ">";
fifoClient.sendData(sendMessage); //send the mail from command to the server
serverFlop = fifoClient.recvData(recMessage); //receive next status message from server
if(serverFlop == -1) //check if the server flopped
{
fifoClient.closeConnection();
continue;
}
//check for an error
if(!fifoClient.checkError(recMessage, Status::SMTP_ACTION_COMPLETE))
{
cout << recMessage << endl;
fifoClient.closeConnection();
continue;
}
sendMessage = "RCPT TO:<" + message[1] + ">"; //set what we're sending
fifoClient.sendData(sendMessage); //send data
serverFlop = fifoClient.recvData(recMessage); //get response
if(serverFlop == -1) //check if the server flopped
{
fifoClient.closeConnection();
continue;
}
//check for an error
if(!fifoClient.checkError(recMessage, Status::SMTP_ACTION_COMPLETE))
{
cout << recMessage << endl;
fifoClient.closeConnection();
continue;
}
//send data command to the server and get a response
fifoClient.sendData("DATA"); //send that we're ready to send data
serverFlop = fifoClient.recvData(recMessage); //get the response
if(serverFlop == -1) //check if the server flopped
{
fifoClient.closeConnection();
continue;
}
//check for an error
if(!fifoClient.checkError(recMessage, Status::SMTP_BEGIN_MSG))
{
cout << recMessage << endl;
fifoClient.closeConnection();
continue;
}
for(int i = 3; i < message.size(); i++)
{
sendMessage = message[i];
if(sendMessage == "") //check if it's an empty string, if so add a newline because a getline drops that
sendMessage = "\n";
Sleep(250);
fifoClient.sendData(sendMessage); //send the data, it's already encrypted
}
fifoClient.sendData(".");
serverFlop = fifoClient.recvData(recMessage); //get data from server
if(serverFlop == -1) //check if the server flopped
{
fifoClient.closeConnection();
continue;
}
//check for an error, if there was an error we need to try this entire loop again
if(fifoClient.checkError(recMessage, Status::SMTP_ACTION_COMPLETE))
{
cout << "Message sent successfully! :)\n\n";
isSent = true;
}
else
{
cerr << "Error sending message. Please retry in a few minutes. :(\n\n";
isSent = false;
}
}
} //end of the sending while
if(!isFTS)//close connection to the server because we're done
{
fifoClient.closeConnection();
}
} //end of the else
isWritten = false; ///adding this so it doesn't shit the bed
} //end of the if checking the mutex result
Sleep(5000); //wait a little while before trying to open the file again
} //end of the entire while loop
}
//our thread for client connections - takes in a socket as a parameter
DWORD WINAPI handleMail(LPVOID lpParam)
{
cout << "Email Thread Created\n";
//set our socket to the socket passed in as a parameter
ThreadSock current_client;
current_client.setSock((SOCKET)lpParam);
string recMessage = ""; //will hold the command the client sent
string sendMessage = ""; //will hold the reply we send
bool isGuest = false; //checks to see if the user is a guest
int clientFlop = 0; //will hold the value that the recv function returns
//send the welcome message and receive input back from the client
current_client.sendResponse(Status::SMTP_SRV_RDY, "Welcome to the SMTP Server!"); //send initiation hello
clientFlop = current_client.recvData(recMessage); //get data from the client
//checking out the string to see if it's helo
bool bHeloSent = FALSE;
do{
if(clientFlop == -1) //check if the client flops and disconnects and break if they do
break;
if (upCase(recMessage.substr(0,4)) == "HELO") //if the first word is helo
{
current_client.sendResponse(Status::SMTP_ACTION_COMPLETE, "Welcome to MAST-Stimpy@" + sSrvrIP); //send 250 and welcome message
cout << "Connection Successful. We received a HELO from the client.\n";
bHeloSent = TRUE;
}
else //if it's not HELO, return error code
{
current_client.sendData(Status::SMTP_CMD_SNTX_ERR); //sending the error code
cout << "Connection Failed. We did not recieive a HELO from client...\n";
clientFlop = current_client.recvData(recMessage); //get data from the client
}
} while (!bHeloSent);
current_client.recvData(recMessage); //receiving the verify and a username
string username; //will hold the name of the user
//checking to see if it's a verify
if (upCase(recMessage.substr(0,4)) == "VRFY")
{
username = recMessage.substr(5); //get the username from the command the user sent
//if it is, validate the username and continue
if (current_client.validateUser(username))
{
current_client.sendData(Status::SMTP_ACTION_COMPLETE); //if the username was valid, send back 250
}
else //make any other username that tries to login, a guest account
{
isGuest = true; //changing the value of the bool to true
current_client.sendData(Status::SMTP_ACTION_COMPLETE);
}
//get data from the client only if they sent VRFY
clientFlop = current_client.recvData(recMessage);
}
else
{
cout << "We didn't get a VRFY.\n";
current_client.sendResponse(Status::SMTP_CMD_SNTX_ERR, "Expecting 'VRFY <user>' but was never received."); //send error
recMessage = "QUIT"; //set to quit so it just skips the while loop and ends the thread
}
//our send/recv loop
while(upCase(recMessage) != "QUIT")
{
bool bRecipientSent = FALSE;
bool canWrite = false; //will be true if this thread can write to the file
string sRecipient = "";
if(upCase(recMessage.substr(0,9)) == "MAIL FROM")
{
string sender = recMessage.substr(11, recMessage.length()-12); //parsing recMessage to find the sender
cout << "Client Sent: " << recMessage << endl; //for debugging
current_client.sendData(Status::SMTP_ACTION_COMPLETE);
//get more data from client
clientFlop = current_client.recvData(recMessage);
//rcpt to section
if (upCase(recMessage.substr(0,7)) != "RCPT TO") //checking to see if it's RCPT TO
{
cout << "RCPT TO wasn't sent...\n"; //should probably sent syntax error back
current_client.sendData(Status::SMTP_CMD_SNTX_ERR); //send error
}
else
{
cout << "Client Sent: " << recMessage << endl; //for debugging
//sRecipient = recMessage.substr(9, recMessage.find("@")-9);
sRecipient = recMessage.substr(9, recMessage.length()-10); //the entire string including IP Address
//checking to see if the user is valid
bool bLocalDelivery = FALSE;
if (sRecipient.find("@") == -1)
{
cout << "Assuming this user is local.\n";
sRecipient += "@" + sSrvrIP;
bLocalDelivery = TRUE;
}
string sSrvrT = recMessage.substr(recMessage.find("@")+1); //@ to end, including the >
string sSrvr = sSrvrT.substr(0, sSrvrT.length()-1); //@ to end, minus the bracet
if ((sSrvr == "127.0.0.1" || sSrvr == sSrvrIP || sSrvr == ""))
{
bLocalDelivery = TRUE;
}
if(isGuest && !bLocalDelivery) //if the guest account tries to send an email to a user not on our server, send bad error code
current_client.sendResponse(Status::SMTP_CMD_SNTX_ERR, "Guest doesn't have permission to send emails to outside servers.");
else if(bLocalDelivery && !current_client.validateUser(sRecipient.substr(0,sRecipient.find("@"))))
current_client.sendResponse(Status::SMTP_CMD_SNTX_ERR, "Malformed Recipient."); //sending back a bad error code
else
{
current_client.sendResponse(Status::SMTP_ACTION_COMPLETE, "OK"); //if the username was valid, send back 250
bRecipientSent = TRUE;
//getting data and writing to file part
clientFlop = current_client.recvData(recMessage); //getting more data from client
//checking to see if the string is DATA
if (upCase(recMessage.substr(0,6)) != "DATA")
{
cout << "DATA wasn't received.\n";
current_client.sendData(Status::SMTP_CMD_SNTX_ERR); //send error
}
else
{
//get the data of the message part
ofstream fout;//create file output object and open it in append mode
ofstream flog;
flog.open("mail.log",ios::app);
if(bLocalDelivery) //if local
fout.open ((string(sRecipient.substr(0,sRecipient.find("@")) + ".txt")).c_str(), ios::app);
else //if on a different server and needs to be relayed
{
while(!canWrite)
{
if(!isWritten)
{
fout.open("email.fifo", ios::app);
canWrite = true;
}
}
}
//write the initial part of the email
string dt = current_client.getDateTime();
fout << dt << endl << sRecipient << endl << sender << endl;
flog << dt << endl << sRecipient << endl << sender << endl;
//tell client to send data, then get data and write to file
current_client.sendResponse(Status::SMTP_BEGIN_MSG,"OK -- Send Data");
clientFlop = current_client.recvData(recMessage); //getting a line from the client
while (recMessage != ".") //while line !=. we want to keep getting message data from the client
{
if(clientFlop == -1)
break;
fout << recMessage; //write line to file
flog << recMessage;
if(recMessage != "\n")
{
flog << endl;
fout << endl;
}
cout << "Encrypted data sent: " << recMessage << endl;
clientFlop = current_client.recvData(recMessage); //getting next line from the user
}
//write a . to denote the end of the message
fout << "." << endl;
flog << "." << endl;
current_client.sendData(Status::SMTP_ACTION_COMPLETE);//send status code that action is complete
fout.close(); //closing the file
flog.close();
isWritten = true; //set this to true because the file is now written
}
}
}
}
else if(upCase(recMessage.substr(0,5)) == "INBOX") //if they send an inbox and want to check their inbox
{
cout << "The Client Sent: " << recMessage << endl;
if (isGuest == true) //if isGuest is true, send an error and send error
current_client.sendResponse(Status::SMTP_MBOX_UNAV, "No mailbox on a guest account."); //sending an error code
else
{
//open file for the user's mailbox that is logged in
ifstream fin(string(username + ".txt").c_str()); //file input object
if(!fin.is_open()) //check if the file opens or not
current_client.sendResponse(Status::SMTP_MBOX_UNAV, "No messages in your inbox."); //send back that there aren't any messages
else
{
current_client.sendResponse(Status::SMTP_ACTION_COMPLETE, "OK"); //send 250 OK so they know we got the command okay then send mail
getline(fin, sendMessage); //get first line from file
while(!fin.eof()) //until we get to the end of the file
{
if(sendMessage == "") //if it doesn't have anything, then make it a newline because getline skips over it
sendMessage = "\n";
//send the line from the file and get an ok from the client
current_client.sendData(sendMessage);
clientFlop = current_client.recvData(recMessage);
if(clientFlop == -1) //check to see if they actually sent a message and break if they didn't
break;
//get another line from the file
getline(fin, sendMessage);
}
current_client.sendData("EOF"); //send to the client that we're at the eof
}
}
}
else
{
cout << "Client didn't send 'MAIL FROM' or 'INBOX' in the beginning\n";
current_client.sendData(Status::SMTP_CMD_SNTX_ERR); //sending back an error code
}
//get data from the client before starting loop again
clientFlop = current_client.recvData(recMessage);
if (clientFlop == -1) //if they sent quit, break from while loop and the thread will end after exiting this
break;
if(upCase(recMessage) == "QUIT")
{
current_client.sendResponse(Status::SMTP_SRV_CLOSE, "OK -- Goodbye...");
break;
}
} //end of while
current_client.closeConnection(); //close socket connection with the client
}
int main(int argc, char * argv[])
{
if(argc != 2)
{
cout << "Usage: " << argv[0] << " <listening IP>" << endl;
return -69;
}
cout << "Starting up multi-threaded SMTP server\n";
//our masterSocket(socket that listens for connections)
SOCKET sock;
WSADATA wsaData;
sockaddr_in server;
//start winsock
int ret = WSAStartup(0x101,&wsaData); //use highest version of winsock avalible
if(ret != 0)
return 0;
sSrvrIP = string(argv[1]);
//fill in winsock struct ...
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(31000); //listen on telnet port 31000
//create our socket
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock == INVALID_SOCKET)
return 0;
//bind our socket to a port(port 123)
if(bind(sock,(sockaddr*)&server,sizeof(server)) !=0)
return 0;
//listen for a connection
if(listen(sock,5) != 0)
return 0;
//creating 2 seperate instances
DWORD thread;
DWORD thread2;
//socket that we sendrecv data on
SOCKET client;
sockaddr_in from;
int fromlen = sizeof(from);
//create our fifo thread only once because it will loop continuously
CreateThread(NULL, 0,relayMail,(LPVOID)NULL, 0, &thread);
//loop forever
while(true)
{
//accept connections
client = accept(sock,(struct sockaddr*)&from,&fromlen);
cout << "Client connected.\n";
//create our handleMail thread and pass client socket as a parameter
CreateThread(NULL, 0,handleMail,(LPVOID)client, 0, &thread2);
}
WSACleanup(); //windows cleanup
return 0; //ends program
}