-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysmtpd.c
392 lines (333 loc) · 12.2 KB
/
mysmtpd.c
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
#include "netbuffer.h"
#include "mailuser.h"
#include "server.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <ctype.h>
#define MAX_LINE_LENGTH 1024
typedef enum state {
Undefined,
// TODO: Add additional states as necessary
HELOexist,
MAILexist,
RCPTexist,
} State;
typedef struct smtp_state {
int fd;
net_buffer_t nb;
char recvbuf[MAX_LINE_LENGTH + 1];
char *words[MAX_LINE_LENGTH];
int nwords;
State state;
struct utsname my_uname;
// TODO: Add additional fields as necessary
user_list_t userList;
} smtp_state;
static void handle_client(int fd);
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Invalid arguments. Expected: %s <port>\n", argv[0]);
return 1;
}
run_server(argv[1], handle_client);
return 0;
}
// syntax_error returns
// -1 if the server should exit
// 1 otherwise
int syntax_error(smtp_state *ms) {
if (send_formatted(ms->fd, "501 %s\r\n", "Syntax error in parameters or arguments") <= 0) return -1;
return 1;
// comment to TA: I tried to use this helper function but it leads to the prairie Learn test not pass,
// so I ended up with writing this two lines myself.
}
// checkstate returns
// -1 if the server should exit
// 0 if the server is in the appropriate state
// 1 if the server is not in the appropriate state
int checkstate(smtp_state *ms, State s) {
if (ms->state != s) {
if (send_formatted(ms->fd, "503 %s\r\n", "Bad sequence of commands") <= 0) return -1;
return 1;
}
return 0;
}
// isValidDomain returns
// 1 if it is a valid domain
// 0 if it isn't
int isValidDomain(char *str) {
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] < '0' && str[i] > '9' &&
str[i] < 'a' && str[i] > 'z' &&
str[i] < 'A' && str[i] > 'Z' &&
str[i] != '.') {
return 0;
}
if (str[i - 1] == '.' && str[i] == '.')
return 0;
}
return 1;
}
// isValidPath checks whether there is at least one
// character between <>, and returns
// 1 if it is a valid path
// 0 if it isn't
int isValidPath(char *str){
int len= strlen(str);
if(str[0] !='<' || str[len-1] !='>' || len < 3){
return 0;
}
return 1;
}
// All the functions that implement a single command return
// -1 if the server should exit
// 0 if the command was successful
// 1 if the command was unsuccessful
int do_quit(smtp_state *ms) {
dlog("Executing quit\n");
// TODO: Implement this function
if(send_formatted(ms->fd, "221 %s\r\n", "Service closing transmission channel") <= 0) return -1;
return -1;
}
int do_helo(smtp_state *ms) {
dlog("Executing helo\n");
// TODO: Implement this function
if(send_formatted(ms->fd,"250 %s\r\n", ms->my_uname.nodename)<= 0) return -1;
ms->state = HELOexist;
return 0;
}
int do_rset(smtp_state *ms) {
dlog("Executing rset\n");
// TODO: Implement this function
// Any stored sender, recipients, and mail data MUST be
// discarded, and all buffers and state tables cleared.
if(ms->nwords != 1){
if (send_formatted(ms->fd, "501 %s\r\n", "Syntax error in parameters or arguments") <= 0) return -1;
return 1;
}
ms->recvbuf[0] = '\0';
if(ms->state == Undefined) {
ms->state = Undefined;
} else {
ms->state = HELOexist;
}
if(ms->userList != NULL){
user_list_destroy(ms->userList);
ms->userList = NULL;
}
// mail_list_destroy(ms->mailItem);
if(send_formatted(ms->fd,"250 State reset\r\n") <= 0) return -1;
return 0;
}
int do_mail(smtp_state *ms) {
dlog("Executing mail\n");
// TODO: Implement this function
int state;
if(ms->nwords != 2 && ms->nwords != 3){
if (send_formatted(ms->fd, "501 %s\r\n", "Syntax error in parameters or arguments") <= 0) return -1;
return 1;
}
// Check whether the command starts with "MAIL FROM:<"
if(strncasecmp(ms->words[1], "FROM:<", 6)){
if(send_formatted(ms->fd, "501 %s\r\n", "Syntax error in parameters or arguments") <= 0) return -1;
return 1;
}
char *start = strchr(ms->words[1],'<'); // find the end of the email address
char *end = strchr(ms->words[1], '>'); // find the end of the email address
if(end == NULL){
if(send_formatted(ms->fd, "501 %s\r\n", "Syntax error in parameters or arguments") <= 0) return -1;
return 1;
}
if(start !=NULL && !isValidPath(start)){
if(send_formatted(ms->fd, "501 %s\r\n", "Syntax error in parameters or arguments") <= 0) return -1;
return 1;
}
// check whether the email address after vrfy command is valid
char *atExist = strchr(ms->words[1], '@'); // find a pointer pointing to '@'
// check whether '@' exist and whether the domain server is valid
if(atExist == NULL || !isValidDomain(atExist+1)) {
if(send_formatted(ms->fd, "501 %s\r\n", "Syntax error in parameters or arguments") <= 0) return -1;
return 1;
}
if ((state = checkstate(ms, HELOexist)) == 0) {
if (send_formatted(ms->fd, "250 %s \r\n", "Requested mail action okay, completed") <= 0) return -1;
ms->state = MAILexist;
return 0;
}
return state;
}
int do_rcpt(smtp_state *ms) {
dlog("Executing rcpt\n");
// TODO: Implement this function
char *emailAddr;
if (ms->state != MAILexist && ms->state !=RCPTexist) {
if (send_formatted(ms->fd, "503 %s\r\n", "Bad sequence of commands") <= 0) return -1;
return 1;
}
if(ms->nwords != 2){
if (send_formatted(ms->fd, "501 %s\r\n", "Syntax error in parameters or arguments") <= 0) return -1;
return 1;
}
//Check whether the command start with "RCPT TO:<"
if(strncasecmp(ms->words[1], "TO:<", 4)){
if(send_formatted(ms->fd, "500 Syntax error, command unrecognized\r\n") <= 0) return -1;
return 1;
}
char *start = strchr(ms->words[1], '<'); // find the start of the email address
char *end = strchr(ms->words[1], '>'); // find the end of the email address
//Check whether the command ends with '>' and make sure there are messages between '<' and '>'
if(end == NULL || end-1 == start){
if(send_formatted(ms->fd, "500 Syntax error, command unrecognized\r\n") <= 0) return -1;
return 1;
} else {
emailAddr = trim_angle_brackets(start);
user_list_add(&ms->userList,emailAddr);
}
// check whether the argument identifies a user
if(is_valid_user(emailAddr, NULL) == 0){
if(send_formatted(ms->fd,"550 %s - %s\r\n", "No such user", emailAddr) <= 0) return -1;
return 1;
} else { //if the username exists
if(send_formatted(ms->fd,"250 OK %s\r\n", "User Exists") <= 0) return -1;
ms->state = RCPTexist;
return 0;
}
}
int do_data(smtp_state *ms) {
dlog("Executing data\n");
// TODO: Implement this function
int len;
char template[] = "templateXXXXXX";
if(ms->state != RCPTexist){
if (send_formatted(ms->fd, "503 %s\r\n", "Bad sequence of commands") <= 0) return -1;
return 1;
}
if(ms->nwords != 1) {
if (send_formatted(ms->fd, "501 %s\r\n", "Syntax error in parameters or arguments") <= 0) return -1;
return 1;
}
if(send_formatted(ms->fd,"354 Waiting for data, finish with <CR><LF>.<CR><LF> \r\n") <= 0) return -1;
// Create the temporary file
int temp_fd = mkstemp(template);
if(temp_fd == -1){ // dealing with errors caused during creating the temporary file
perror("mkstemp");
if(send_formatted(ms->fd,"451 Requested action aborted: error in processing \r\n") <= 0) return -1;
return 1;
}
while ((len = nb_read_line(ms->nb, ms->recvbuf)) >= 0) {
if (ms->recvbuf[len - 1] != '\n') {
// command line is too long, stop immediately
send_formatted(ms->fd, "500 Syntax error, command unrecognized\r\n");
break;
}
if (strlen(ms->recvbuf) < len) {
// received null byte somewhere in the string, stop immediately.
send_formatted(ms->fd, "500 Syntax error, command unrecognized\r\n");
break;
}
// Remove CR, LF and other space characters from end of buffer
while (isspace(ms->recvbuf[len - 1])) ms->recvbuf[--len] = 0;
if(!strcasecmp(ms->recvbuf, ".")){
break;
}
//check if the first character is a period and there are other characters on the line
if(!strncasecmp(ms->recvbuf, ".", 1)){
write(temp_fd, ms->recvbuf+1, strlen(ms->recvbuf)-1); // delete the first character
write(temp_fd, "\r\n",2);
} else{
write(temp_fd, ms->recvbuf, strlen(ms->recvbuf));
write(temp_fd, "\r\n",2);
}
}
// Save the email messages for each recipient in userList of the message
save_user_mail(template, ms->userList);
user_list_destroy(ms->userList);
ms->userList = user_list_create();
// clear tmp file
unlink(template);
close(temp_fd);
ms->state = HELOexist;
if(send_formatted(ms->fd,"250 OK\r\n") <= 0) return -1;
return 0;
}
int do_noop(smtp_state *ms) {
dlog("Executing noop\n");
// TODO: Implement this function
if(send_formatted(ms->fd,"250 OK (noop)\r\n")<= 0) return -1;
return 0;
}
int do_vrfy(smtp_state *ms) {
dlog("Executing vrfy\n");
// TODO: Implement this function
if(ms->nwords != 2){
if (send_formatted(ms->fd, "501 %s\r\n", "Syntax error in parameters or arguments") <= 0) return -1;
return 1;
}
ms->words[1] = trim_angle_brackets(ms->words[1]);
// check whether the argument identifies a user
if(is_valid_user(ms->words[1], NULL) == 0){
if(send_formatted(ms->fd, "550 %s - %s\r\n", "No such user", ms->words[1]) <= 0) return -1;
return 1;
} else { //if the username exists
if(send_formatted(ms->fd,"250 OK %s\r\n", "User Exists") <= 0) return -1;
return 0;
}
}
void handle_client(int fd) {
size_t len;
smtp_state mstate, *ms = &mstate;
ms->fd = fd;
ms->nb = nb_create(fd, MAX_LINE_LENGTH);
ms->state = Undefined;
uname(&ms->my_uname);
ms->userList = user_list_create(); // initialize an empty userList
if (send_formatted(fd, "220 %s Service ready\r\n", ms->my_uname.nodename) <= 0) return;
while ((len = nb_read_line(ms->nb, ms->recvbuf)) >= 0) {
if (ms->recvbuf[len - 1] != '\n') {
// command line is too long, stop immediately
send_formatted(fd, "500 Syntax error, command unrecognized\r\n");
break;
}
if (strlen(ms->recvbuf) < len) {
// received null byte somewhere in the string, stop immediately.
send_formatted(fd, "500 Syntax error, command unrecognized\r\n");
break;
}
// Remove CR, LF and other space characters from end of buffer
while (isspace(ms->recvbuf[len - 1])) ms->recvbuf[--len] = 0;
dlog("Command is %s\n", ms->recvbuf);
// Split the command into its component "words"send_string
ms->nwords = split(ms->recvbuf, ms->words);
char *command = ms->words[0];
if (!strcasecmp(command, "QUIT")) {
if (do_quit(ms) == -1) break;
} else if (!strcasecmp(command, "HELO") || !strcasecmp(command, "EHLO")) {
if (do_helo(ms) == -1) break;
} else if (!strcasecmp(command, "MAIL")) {
if (do_mail(ms) == -1) break;
} else if (!strcasecmp(command, "RCPT")) {
if (do_rcpt(ms) == -1) break;
} else if (!strcasecmp(command, "DATA")) {
if (do_data(ms) == -1) break;
} else if (!strcasecmp(command, "RSET")) {
if (do_rset(ms) == -1) break;
} else if (!strcasecmp(command, "NOOP")) {
if (do_noop(ms) == -1) break;
} else if (!strcasecmp(command, "VRFY")) {
if (do_vrfy(ms) == -1) break;
} else if (!strcasecmp(command, "EXPN") ||
!strcasecmp(command, "HELP")) {
dlog("Command not implemented \"%s\"\n", command);
if (send_formatted(fd, "502 Command not implemented\r\n") <= 0) break;
} else {
// invalid command
dlog("Illegal command \"%s\"\n", command);
if (send_formatted(fd, "500 Syntax error, command unrecognized\r\n") <= 0) break;
}
}
nb_destroy(ms->nb);
}