-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_SMTP.c
335 lines (301 loc) · 7.9 KB
/
client_SMTP.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
/**
* Description : Client SMTP implémenté sur la base d'un client TCP simple
* Using : ./client_smtp expéditeur sujet FichierCorps serveurSmtp destinataire
* Date : 3 juin 2017
* Context : Cours Réseaux INF1j
* Authors : Malik Fleury et Bastien Wermeille
* Version : 1.0
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#define SENDER argv[1]
#define SUBJECT argv[2]
#define BODY argv[3]
#define SERVER argv[4]
#define RECIPIENT argv[5]
#define PORT argv[6]
#define DEFAULTPORT "25"//587
#define MAXATTEMPT 5
typedef enum
{
CONNECTION, HELO, FROM, TO, DATA, DATA2, QUIT, ERROR
} EtatsSmtp;
/* Function definition */
static FILE *tcp_connect(const char *hostname, const char *port);
int smtp_send(char* sender,char* recipient,char* subject,char* body,char* server,char* nPort);
void tcp_close(FILE *f);
int tcp_send(FILE *f, char *cmd);
int errorManager(char first, char second, char third);
/* Public function implementation */
//expéditeur, sujet, corps, serveur smtp, destinataire
int main(int argc, char *argv[])
{
if(argc != 6 && argc !=7)
{
printf("%s: Bad arguments.\n\n", argv[0]);
printf("/**\n");
printf(" * Description : Client SMTP implémenté sur la base d'un client TCP simple\n");
printf(" * Using : ./client_smtp expéditeur sujet FichierCorps serveurSmtp destinataire (port)OPTIONNEL\n");
printf(" * Date : 3 juin 2017\n");
printf(" * Context : Cours Réseaux INF1j\n");
printf(" * Authors : Malik Fleury et Bastien Wermeille\n");
printf(" * Version : 1.0\n");
printf(" */)\n");
return 1;
}
else
{
//Choice of the port, by default 25
char* nPort = DEFAULTPORT;
if(argc == 7)
nPort = PORT;
smtp_send(SENDER,RECIPIENT,SUBJECT,BODY,SERVER,nPort);
return 0;
}
}
//Sending of an smtp request
int smtp_send(char* sender,char* recipient,char* subject,char* body,char* server,char* port)
{
//Initial state
EtatsSmtp smtpState=CONNECTION;
//Basic stuff
unsigned int attempt=0;
int codeErr;
//Check body message
FILE *f = NULL;
char buffer[1025];
FILE *message = fopen(body,"r");
if(message == NULL) return -1;
//log
//fprintf(stdout, "Send from %s to %s with message '%s' : %s on %s :: %s\n\n",sender,recipient,subject,body,server,port);
while(attempt < MAXATTEMPT)
{
//State machine
switch(smtpState)
{
case CONNECTION:
if((f = tcp_connect(server, port)))
{
printf("Connection ok\n");
codeErr = tcp_send(f,"");
(codeErr == 0) ? (smtpState = HELO) : (smtpState = ERROR);
}
else
{
perror("Error while conecting to the server\n");
codeErr = -1;
smtpState = ERROR;
}
break;
case HELO:
codeErr = tcp_send(f,"HELO client\r\n");
(codeErr == 0) ? (smtpState = FROM) : (smtpState = ERROR);
break;
case FROM:
sprintf(buffer,"MAIL FROM: <%s>\r\n",sender);
codeErr = tcp_send(f,buffer);
(codeErr == 0) ? (smtpState = TO) : (smtpState = ERROR);
break;
case TO:
sprintf(buffer,"RCPT TO: <%s>\r\n",recipient);
codeErr = tcp_send(f,buffer);
(codeErr == 0) ? (smtpState = DATA) : (smtpState = ERROR);
break;
case DATA:
codeErr = tcp_send(f,"DATA\r\n");
(codeErr == 2) ? (smtpState = DATA2) : (smtpState = ERROR);//3
break;
case DATA2:
//Header
sprintf(buffer,"From: %s\n",sender);
printf("From: %s\n",sender);
fprintf(f,buffer);
sprintf(buffer,"To: %s\n",recipient);
printf("To: %s\n",recipient);
fprintf(f,buffer);
sprintf(buffer,"Subject: %s\n",subject);
printf("Subject: %s\n",subject);
fprintf(f,buffer);
//Content
while(fgets(buffer,sizeof(buffer)-1,message))
{
if(buffer[0]=='.'){
strcpy(buffer+1,buffer);
}
printf(buffer);
fprintf(f,buffer);
}
codeErr = tcp_send(f,"\r\n.\r\n");
(codeErr == 0) ? (smtpState = QUIT) : (smtpState = ERROR);
break;
case QUIT:
tcp_send(f,"QUIT\r\n");
tcp_close(f);
fclose(message);
return 0;
break;
case ERROR:
if(codeErr == -1)
{
tcp_close(f);
fclose(message);
perror("Error 5xx\n");
return -1;
}
printf("New attempt in five minutes...\n");
sleep(300);
attempt++;
tcp_close(f);
smtpState = CONNECTION;
break;
default:
perror("Unexpected error\n");
tcp_close(f);
fclose(message);
return -1;
break;
}
sleep(1);
}
//Close opened file and socket
tcp_close(f);
fclose(message);
return 0;
}
//Send data to smtp server et get error code
int tcp_send(FILE *f, char *cmd)
{
printf("\n");
printf(cmd);
fprintf(f,cmd);
fflush(f);
char buffer[255];
do
{
fgets(buffer, sizeof(buffer), f);
printf(buffer);
//puts(buffer);
}while(buffer[0] < '2' || buffer[0] > '5');
return errorManager(buffer[0],buffer[1],buffer[2]);
}
//Mangement of error
// -1 -> KO
// 0 -> OK
// 1 -> Try again
// 2 -> Further informations required
int errorManager(char first, char second, char third)
{
//printf("%c%c%c ",first,second,third);
switch (first){
case '1':
printf("The server does not respond\n");
switch (second){
case '0':
printf("The server is unable to connect.\n");
case '1':
printf("Connection refused or inability to open an SMTP stream.\n");
default:
printf("Unexceptected error\n");
}
return -1;
case '2':
printf("The server has completed the task successfully.\n");
return 0;
case '3':
printf("The server has understood the request, but requires further information to complete it.\n\n");
return 2;
case '4':
printf("The server has encountered a temporary failure.\n");
return 1;
case '5':
printf("The server has encountered an error.\n");
return -1;
default:
printf("This error seems impossible \n");
return -1;
}
}
//Close tcp connection
void tcp_close(FILE *f)
{
shutdown(fileno(f), SHUT_WR);
if(fclose(f) == 0) f = NULL;
else
{
perror("fclose(): failed: ");
}
}
//Open tcp connection
static FILE *tcp_connect(const char *hostname, const char *port)
{
FILE *f = NULL;
int s;
struct addrinfo hints;
struct addrinfo *result, *rp;
hints.ai_family = AF_UNSPEC; /* IPv4 or v6 */
hints.ai_socktype = SOCK_STREAM; /* TCP */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* any protocol */
if((s = getaddrinfo(hostname, port, &hints, &result)))
{
fprintf(stderr, "getaddrinfo(): failed: %s.\n", gai_strerror(s));
}
else
{
/* getaddrinfo() returns a list of address structures.
* Try each address until we successfully connect(2).
*/
for(rp = result; rp != NULL; rp = rp->ai_next)
{
char ipname[INET_ADDRSTRLEN];
char servicename[6]; /* "65535\0" */
/* socket creation */
if((s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1)
{
perror("socket() failed: ");
continue; /* next loop */
}
if(!getnameinfo(rp->ai_addr, rp->ai_addrlen, ipname, sizeof(ipname), servicename, sizeof(servicename), NI_NUMERICHOST|NI_NUMERICSERV))
{
printf("Trying connection to host %s:%s ...\n", ipname, servicename);
}
if(connect(s, rp->ai_addr, rp->ai_addrlen) != -1)
{
/* from now on, read(2), write(2), shutdown(2) and close(2)
* can be called on that socket (file descriptor) s -- note that
* as for every character device, write(2) might not write
* all we want, and read(2) will return even if not all bytes
* are received: this is one of the reason we will promote
* the socket into a fully-equipped libc FILE *.
*/
break; /* end of loop */
}
else
{
perror("connect(): ");
}
close(s); /* err. ign. */
}
freeaddrinfo(result);
if(rp == NULL)
{
fprintf(stderr, "Could not connect.\n");
}
else
{
/* associate the file descriptor to a C library file, to make
* things easier for us
*/
if(!(f = fdopen(s, "r+")))
{
perror("fdopen() failed: ");
}
}
}
return f;
}