-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.c
executable file
·364 lines (333 loc) · 9.04 KB
/
mail.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
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "config.h"
#include "misc.h"
#define MIME "Mime-Version: 1.0"
#define CONTENT_CODE "Content-Transfer-Encoding: base64\r\n"
static const char mime_body[] =
"Content-Type: multipart/mixed;"
" boundary=__=_Part_Boundary_001_011991.029871\r\n\r\n"
"--__=_Part_Boundary_001_011991.029871\r\n"
"Content-Type: multipart/alternative;"
" boundary=__=_Part_Boundary_001_011991.029872\r\n\r\n"
"--__=_Part_Boundary_001_011991.029872\r\n"
"Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n"
"\r\n"
"--__=_Part_Boundary_001_011991.029872\r\n"
"Content-Type: text/html; charset=ISO-8859-1\r\n"
"\r\n\r\n\r\n"
"--__=_Part_Boundary_001_011991.029872--\r\n"
"--__=_Part_Boundary_001_011991.029871\r\n";
static int sockfd;
static int setup_socket(const char *server)
{
struct sockaddr_in server_addr;
struct hostent *host;
if ((host = gethostbyname(server)) == NULL) {
printf("Get host name error!\n");
close(sockfd);
return -1;
}
/* set up socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("Socket setup error!\n");
close(sockfd);
return -1;
}
bzero(&server_addr, sizeof (server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons((unsigned short)25);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
/* try to connect server */
if (connect(sockfd, (struct sockaddr *)&server_addr,
sizeof (struct sockaddr)) == -1) {
printf("Connect server error!\n");
close(sockfd);
return -1;
}
return 0;
}
static int command_HELO(const char *server)
{
char buffer[1024 * 5] = {0};
/* send HElO to server */
sprintf(buffer, "HELO %s\r\n", server);
if (send(sockfd, buffer, strlen(buffer), 0) == -1) {
printf("Send HELO error!\n");
close(sockfd);
return -1;
}
if (recv(sockfd, buffer, sizeof (buffer), 0) == -1) {
printf("Receive HELO error!\n");
close(sockfd);
return -1;
}
printf("\nData received HELO: %s\n", buffer);
return 0;
}
static int command_AUTH(void)
{
char buffer[1024 * 5] = {0};
/* send login message */
if (send(sockfd, "AUTH LOGIN\r\n", strlen("AUTH LOGIN\r\n"), 0) == -1) {
printf("Send AUTH LOGIN error!\n");
close(sockfd);
return -1;
}
if (recv(sockfd, buffer, sizeof (buffer), 0) == -1) {
printf("Receive AUTH LOGIN error!\n");
close(sockfd);
return -1;
}
printf("\nData received AUTH LOGIN: %s", buffer);
return 0;
}
static int command_NAME(void)
{
char buffer[1024 * 5] = {0};
int name_len = 0;
int base64_name_len = 0;
char *base64_name_buf = NULL;
/* send user name*/
name_len = strlen(USER_NAME);
base64_name_len = name_len * 4 / 3 + 50;
base64_name_buf = (char *)malloc(base64_name_len);
if (base64_name_buf == NULL) {
printf("Base64_name_buf memory allocate error!\nExit process...\n");
exit(EXIT_FAILURE);
}
if (base64_encode(USER_NAME, base64_name_buf, name_len) == -1) {
printf("Base64 encode user name error!\nExit process...\n");
exit(EXIT_FAILURE);
}
sprintf(buffer, "%s\n", base64_name_buf);
if (send(sockfd, buffer, strlen(buffer), 0) < 0) {
printf("Send USER NAME error!\n");
close(sockfd);
return -1;
}
if (base64_name_buf != NULL) {
free(base64_name_buf);
base64_name_buf = NULL;
}
else {
printf("Base64_name_buf is NULL!\nExit process...\n");
exit(EXIT_FAILURE);
}
if (recv(sockfd, buffer, sizeof (buffer), 0) == -1) {
printf("Receive USER NAME error!\n");
close(sockfd);
return -1;
}
printf("Data received NAME: %s\n", buffer);
return 0;
}
static int command_PASSWD(void)
{
char buffer[1024 * 5] = {0};
int passwd_len = 0;
int base64_passwd_len = 0;
char *base64_passwd_buf = NULL;
/* send user password */
passwd_len = strlen(USER_PASSWORD);
base64_passwd_len = passwd_len * 4 / 3 + 50;
base64_passwd_buf = (char *)malloc(base64_passwd_len);
if (base64_passwd_buf == NULL) {
printf("Base64_passed_buf is NULL!\nExit process...\n");
exit(EXIT_FAILURE);
}
if(base64_encode(USER_PASSWORD, base64_passwd_buf, passwd_len) == -1) {
printf("Base64 encode user password error!\nExit process...\n");
exit(EXIT_FAILURE);
}
sprintf(buffer, "%s\n", base64_passwd_buf);
if (send(sockfd, buffer, strlen(buffer), 0) == -1) {
printf("Send PASSWORD error!\n");
close(sockfd);
return -1;
}
if (base64_passwd_buf != NULL) {
free(base64_passwd_buf);
base64_passwd_buf = NULL;
}
else {
printf("Base64_passed_buf is NULL!\nExit process...\n");
exit(EXIT_FAILURE);
}
if (recv(sockfd, buffer, sizeof (buffer), 0) == -1) {
printf("Receive PASSWORD error!\n");
close(sockfd);
return -1;
}
printf("Data received PASSWD: %s", buffer);
return 0;
}
static int command_FROM(void)
{
char buffer[1024 * 5] = {0};
/* send sender mail */
sprintf(buffer, "MAIL FROM: <%s>\n", MAIL_SENDER);
if (send(sockfd, buffer, strlen(buffer), 0) == -1) {
printf("Send FROM error!\n");
close(sockfd);
return -1;
}
if (recv(sockfd, buffer, sizeof (buffer), 0) == -1) {
printf("Receive FROM error!\n");
close(sockfd);
return -1;
}
printf("Data received FROM: %s\n", buffer);
return 0;
}
static int command_TO(void)
{
char buffer[1024 * 5] = {0};
/* send recipient mail*/
sprintf(buffer, "RCPT TO: <%s>\n", MAIL_RECIPIENT);
if (send(sockfd, buffer, strlen(buffer), 0) == -1) {
printf("Send TO error!\n");
close(sockfd);
return -1;
}
if (recv(sockfd, buffer, sizeof (buffer), 0) == -1) {
printf("Receive TO error!\n");
close(sockfd);
return -1;
}
printf("\nData received TO: %s", buffer);
return 0;
}
static int command_DATA(const char * pmsg, const char* file_name)
{
char buffer[1024 * 5] = {0};
/* send data command */
if (send(sockfd, "DATA\r\n", strlen("DATA\r\n"), 0) == -1) {
printf("Send DATA error!\n");
close(sockfd);
return -1;
}
/* send subject */
sprintf(buffer, "From: %s\r\n", FROM);
if (send(sockfd, buffer, strlen(buffer), 0) == -1) {
printf("Send SUBJECT FROM error!\n");
close(sockfd);
return -1;
}
sprintf(buffer, "To: %s\r\n", TO);
if (send(sockfd, buffer, strlen(buffer), 0) == -1) {
printf("Send SUBJECT TO error!\n");
close(sockfd);
return -1;
}
sprintf(buffer, "%s\r\n", MIME);
if (send(sockfd, buffer, strlen(buffer), 0) == -1) {
printf("Send MIME HEAD error!\n");
close(sockfd);
return -1;
}
sprintf(buffer, "Subject: %s\r\n", SUBJECT);
if (send(sockfd, buffer, strlen(buffer), 0) == -1) {
printf("Send SUBJECT error!\n");
close(sockfd);
return -1;
}
if (send(sockfd, mime_body, strlen(mime_body), 0) == -1) {
printf("Send MIME BODY eror\n");
close(sockfd);
return -1;
}
sprintf(buffer, "Content-Type: application/x-bzip2;"
" name=\"%s.tar.bz2\"\r\n", file_name);
if (send(sockfd, buffer, strlen(buffer), 0) == -1) {
printf("Send Conten-Type error!\n");
close(sockfd);
return -1;
}
sprintf(buffer, "Content-Disposition: attachment;"
" filename=\"%s.tar.bz2\"\r\n", file_name);
if (send(sockfd, buffer, strlen(buffer), 0) == -1) {
printf("Send Content-Disposition error!\n");
close(sockfd);
return -1;
}
sprintf(buffer, "%s\r\n", CONTENT_CODE);
if (send(sockfd, buffer, strlen(buffer), 0) == -1) {
printf("Send ENCODE TYPE error!\n");
close(sockfd);
return -1;
}
/* send mail content*/
if (send(sockfd, pmsg, strlen(pmsg), 0) == -1) {
printf("Send MESSAGE error!\n");
close(sockfd);
return -1;
}
/*send finish message */
if (send(sockfd, "\r\n.\r\n", strlen("\r\n.\r\n"), 0) == -1) {
printf("Send FINISH error!\n");
close(sockfd);
return -1;
}
/* receive reply from server */
if (recv(sockfd, buffer, sizeof (buffer), 0) == -1) {
printf("Receive FINISH error!\n");
close(sockfd);
return -1;
}
printf("Data recevied FINISH: %s", buffer);
return 0;
}
static int command_QUIT(void)
{
char buffer[1024 * 5];
/* send quit message */
if (send(sockfd, "QUIT\r\n", strlen("QUIT\r\n"), 0) == -1) {
printf("Send QUIT erroe!\n");
close(sockfd);
return -1;
}
if (recv(sockfd, buffer, sizeof (buffer), 0) == -1) {
printf("Receive QUIT error!\n");
close(sockfd);
return -1;
}
printf("Data received QUIT: %s\n", buffer);
close(sockfd);
return 0;
}
int mail(const char *server, const char *pmsg, const char* file_name)
{
if (setup_socket(server) == -1)
return -1;
if (command_HELO(server) == -1)
return -1;
if (command_AUTH() == -1)
return -1;
if (command_NAME() == -1)
return -1;
if (command_PASSWD() == -1)
return -1;
if (command_FROM() == -1)
return -1;
if (command_TO() == -1)
return -1;
if (command_DATA(pmsg, file_name) == -1)
return -1;
if (command_QUIT() == -1)
return -1;
return 0;
}