forked from urweb/email
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.c
More file actions
544 lines (434 loc) · 13 KB
/
Copy pathmail.c
File metadata and controls
544 lines (434 loc) · 13 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <urweb.h>
struct headers {
uw_Basis_string from, to, cc, bcc, subject;
};
typedef struct headers *uw_Mail_headers;
static uw_Basis_string copy_string(uw_Basis_string s) {
if (s == NULL)
return NULL;
else
return strdup(s);
}
static void free_string(uw_Basis_string s) {
if (s == NULL)
return;
else
free(s);
}
static uw_Mail_headers copy_headers(uw_Mail_headers h) {
uw_Mail_headers h2 = malloc(sizeof(struct headers));
h2->from = copy_string(h->from);
h2->to = copy_string(h->to);
h2->cc = copy_string(h->cc);
h2->bcc = copy_string(h->bcc);
h2->subject = copy_string(h->subject);
return h2;
}
static void free_headers(uw_Mail_headers h) {
free_string(h->from);
free_string(h->to);
free_string(h->cc);
free_string(h->bcc);
free_string(h->subject);
free(h);
}
uw_Mail_headers uw_Mail_empty = NULL;
static void header(uw_context ctx, uw_Basis_string s) {
if (strlen(s) > 100)
uw_error(ctx, FATAL, "Header value too long");
for (; *s; ++s)
if (*s == '\r' || *s == '\n')
uw_error(ctx, FATAL, "Header value contains newline");
}
static void address(uw_context ctx, uw_Basis_string s) {
header(ctx, s);
if (strchr(s, ','))
uw_error(ctx, FATAL, "E-mail address contains comma");
}
uw_Mail_headers uw_Mail_from(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
// char **allowed = uw_get_global(ctx, "mail_from");
// Might add this policy checking (or some expanded version of it) back later.
uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
if (h)
*h2 = *h;
else
memset(h2, 0, sizeof(*h2));
if (h2->from)
uw_error(ctx, FATAL, "Duplicate From header");
/*
if (!allowed)
uw_error(ctx, FATAL, "No From address whitelist has been set. Perhaps you are not authorized to send e-mail.");
if (!(allowed[0] && !strcmp(allowed[0], "*"))) {
for (; *allowed; ++allowed)
if (!strcmp(*allowed, s))
goto ok;
uw_error(ctx, FATAL, "From address is not in whitelist");
}
ok:
*/
address(ctx, s);
h2->from = uw_strdup(ctx, s);
return h2;
}
uw_Mail_headers uw_Mail_to(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
if (h)
*h2 = *h;
else
memset(h2, 0, sizeof(*h2));
address(ctx, s);
if (h2->to) {
uw_Basis_string all = uw_malloc(ctx, strlen(h2->to) + 2 + strlen(s));
sprintf(all, "%s,%s", h2->to, s);
h2->to = all;
} else
h2->to = uw_strdup(ctx, s);
fprintf(stderr, "TO: %s\n", h2->to);
return h2;
}
uw_Mail_headers uw_Mail_cc(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
if (h)
*h2 = *h;
else
memset(h2, 0, sizeof(*h2));
address(ctx, s);
if (h2->cc) {
uw_Basis_string all = uw_malloc(ctx, strlen(h2->cc) + 2 + strlen(s));
sprintf(all, "%s,%s", h2->cc, s);
h2->cc = all;
} else
h2->cc = uw_strdup(ctx, s);
return h2;
}
uw_Mail_headers uw_Mail_bcc(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
if (h)
*h2 = *h;
else
memset(h2, 0, sizeof(*h2));
address(ctx, s);
if (h2->bcc) {
uw_Basis_string all = uw_malloc(ctx, strlen(h2->bcc) + 2 + strlen(s));
sprintf(all, "%s,%s", h2->bcc, s);
h2->bcc = all;
} else
h2->bcc = uw_strdup(ctx, s);
return h2;
}
uw_Mail_headers uw_Mail_subject(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
if (h)
*h2 = *h;
else
memset(h2, 0, sizeof(*h2));
if (h2->subject)
uw_error(ctx, FATAL, "Duplicate Subject header");
header(ctx, s);
h2->subject = uw_strdup(ctx, s);
return h2;
}
typedef struct {
uw_context ctx;
uw_Mail_headers h;
uw_Basis_string body, xbody;
} job;
#define BUFLEN (1024*1024)
static int smtp_read(uw_context ctx, int sock, char *buf, ssize_t *pos) {
char *s;
while (1) {
ssize_t recvd;
buf[*pos] = 0;
if ((s = strchr(buf, '\n'))) {
int n;
*s = 0;
if (sscanf(buf, "%d ", &n) != 1) {
close(sock);
uw_set_error_message(ctx, "Mail server response does not begin with a code.");
return 0;
}
*pos -= s - buf + 1;
memmove(buf, s+1, *pos);
return n;
}
recvd = recv(sock, buf + *pos, BUFLEN - *pos - 1, 0);
if (recvd == 0) {
close(sock);
uw_set_error_message(ctx, "Mail server response ends unexpectedly.");
return 0;
} else if (recvd < 0) {
close(sock);
uw_set_error_message(ctx, "Error reading mail server response.");
return 0;
}
*pos += recvd;
}
}
static int really_string(int sock, const char *s) {
fprintf(stderr, "MAIL: %s\n", s);
return uw_really_send(sock, s, strlen(s));
}
static int sendAddrs(const char *kind, uw_context ctx, int sock, char *s, char *buf, ssize_t *pos) {
char *p;
char out[BUFLEN];
if (!s)
return 0;
for (p = strchr(s, ','); p; p = strchr(p+1, ',')) {
*p = 0;
snprintf(out, sizeof(out), "RCPT TO:%s\r\n", s);
out[sizeof(out)-1] = 0;
*p = ',';
if (really_string(sock, out) < 0) {
close(sock);
uw_set_error_message(ctx, "Error sending RCPT TO for %s", kind);
return 1;
}
if (smtp_read(ctx, sock, buf, pos) != 250) {
close(sock);
uw_set_error_message(ctx, "Mail server doesn't respond to %s RCPT TO with code 250.", kind);
return 1;
}
s = p+1;
}
if (*s) {
snprintf(out, sizeof(out), "RCPT TO:%s\r\n", s);
out[sizeof(out)-1] = 0;
if (really_string(sock, out) < 0) {
close(sock);
uw_set_error_message(ctx, "Error sending RCPT TO for %s", kind);
return 1;
}
if (smtp_read(ctx, sock, buf, pos) != 250) {
close(sock);
uw_set_error_message(ctx, "Mail server doesn't respond to %s RCPT TO with code 250.", kind);
return 1;
}
}
return 0;
}
static void commit(void *data) {
job *j = data;
int sock;
struct sockaddr_in my_addr;
char buf[BUFLEN], out[BUFLEN];
ssize_t pos = 0;
char *s;
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
uw_set_error_message(j->ctx, "Can't create socket for mail server connection");
return;
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(25);
my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(my_addr.sin_zero, 0, sizeof my_addr.sin_zero);
if (connect(sock, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error connecting to mail server");
return;
}
if (smtp_read(j->ctx, sock, buf, &pos) != 220) {
close(sock);
uw_set_error_message(j->ctx, "Mail server doesn't greet with code 220.");
return;
}
if (really_string(sock, "HELO localhost\r\n") < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending HELO");
return;
}
if (smtp_read(j->ctx, sock, buf, &pos) != 250) {
close(sock);
uw_set_error_message(j->ctx, "Mail server doesn't respond to HELO with code 250.");
return;
}
snprintf(out, sizeof(out), "MAIL FROM:%s\r\n", j->h->from);
out[sizeof(out)-1] = 0;
if (really_string(sock, out) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending MAIL FROM");
return;
}
if (smtp_read(j->ctx, sock, buf, &pos) != 250) {
close(sock);
uw_set_error_message(j->ctx, "Mail server doesn't respond to MAIL FROM with code 250.");
return;
}
if (sendAddrs("To", j->ctx, sock, j->h->to, buf, &pos)) return;
if (sendAddrs("Cc", j->ctx, sock, j->h->cc, buf, &pos)) return;
if (sendAddrs("Bcc", j->ctx, sock, j->h->bcc, buf, &pos)) return;
if (really_string(sock, "DATA\r\n") < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending DATA");
return;
}
if (smtp_read(j->ctx, sock, buf, &pos) != 354) {
close(sock);
uw_set_error_message(j->ctx, "Mail server doesn't respond to DATA with code 354.");
return;
}
snprintf(out, sizeof(out), "From: %s\r\n", j->h->from);
out[sizeof(out)-1] = 0;
if (really_string(sock, out) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending From");
return;
}
if (j->h->subject) {
snprintf(out, sizeof(out), "Subject: %s\r\n", j->h->subject);
out[sizeof(out)-1] = 0;
if (really_string(sock, out) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending Subject");
return;
}
}
if (j->h->to) {
snprintf(out, sizeof(out), "To: %s\r\n", j->h->to);
out[sizeof(out)-1] = 0;
if (really_string(sock, out) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending To");
return;
}
}
if (j->h->cc) {
snprintf(out, sizeof(out), "Cc: %s\r\n", j->h->cc);
out[sizeof(out)-1] = 0;
if (really_string(sock, out) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending Cc");
return;
}
}
if ((s = uw_get_global(j->ctx, "extra_mail_headers"))) {
if (really_string(sock, s) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending extra headers");
return;
}
}
if (j->xbody) {
char separator[11];
separator[sizeof(separator)-1] = 0;
do {
int i;
for (i = 0; i < sizeof(separator)-1; ++i)
separator[i] = 'A' + (rand() % 26);
} while (strstr(j->body, separator) || strstr(j->xbody, separator));
snprintf(out, sizeof(out), "MIME-Version: 1.0\r\n"
"Content-Type: multipart/alternative; boundary=\"%s\"\r\n"
"\r\n"
"--%s\r\n"
"Content-Type: text/plain; charset=utf-8\r\n"
"\r\n",
separator, separator);
out[sizeof(out)-1] = 0;
if (really_string(sock, out) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending multipart beginning");
return;
}
if (really_string(sock, j->body) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending message text body");
return;
}
snprintf(out, sizeof(out), "\r\n"
"--%s\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"\r\n",
separator);
out[sizeof(out)-1] = 0;
if (really_string(sock, out) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending multipart middle");
return;
}
if (really_string(sock, j->xbody) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending message HTML body");
return;
}
snprintf(out, sizeof(out), "\r\n"
"--%s--",
separator);
out[sizeof(out)-1] = 0;
if (really_string(sock, out) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending multipart end");
return;
}
} else {
if (really_string(sock, "Content-Type: text/plain; charset=utf-8\r\n\r\n") < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending text Content-Type");
return;
}
if (really_string(sock, j->body) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending message body");
return;
}
}
if (really_string(sock, "\r\n.\r\n") < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending message terminator");
return;
}
if (smtp_read(j->ctx, sock, buf, &pos) != 250) {
close(sock);
uw_set_error_message(j->ctx, "Mail server doesn't respond to end of message with code 250.");
return;
}
if (really_string(sock, "QUIT\r\n") < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending QUIT");
return;
}
if (smtp_read(j->ctx, sock, buf, &pos) != 221) {
close(sock);
uw_set_error_message(j->ctx, "Mail server doesn't respond to QUIT with code 221.");
return;
}
close(sock);
}
static void free_job(void *p, int will_retry) {
job *j = p;
free_headers(j->h);
free_string(j->body);
free_string(j->xbody);
free(j);
}
uw_unit uw_Mail_send(uw_context ctx, uw_Mail_headers h, uw_Basis_string body, uw_Basis_string xbody) {
job *j;
char *s;
if (!h || !h->from)
uw_error(ctx, FATAL, "No From address set for e-mail message");
if (!h->to && !h->cc && !h->bcc)
uw_error(ctx, FATAL, "No recipients specified for e-mail message");
for (s = strchr(body, '.'); s; s = strchr(s+1, '.'))
if ((s[1] == '\n' || s[1] == '\r')
&& (s <= body || s[-1] == '\n' || s[-1] == '\r'))
uw_error(ctx, FATAL, "Message body contains a line with just a period");
if (xbody) {
for (s = strchr(xbody, '.'); s; s = strchr(s+1, '.'))
if ((s[1] == '\n' || s[1] == '\r')
&& (s <= xbody || s[-1] == '\n' || s[-1] == '\r'))
uw_error(ctx, FATAL, "HTML message body contains a line with just a period");
}
j = malloc(sizeof(job));
j->ctx = ctx;
j->h = copy_headers(h);
j->body = copy_string(body);
j->xbody = copy_string(xbody);
uw_register_transactional(ctx, j, commit, NULL, free_job);
return uw_unit_v;
}