annotate mail.c @ 6:f96ca37be746

Fix sending to more than 2 recipients
author Adam Chlipala <adam@chlipala.net>
date Thu, 04 Feb 2016 09:23:10 -0500
parents d163044ae493
children 4a0de889bcec
rev   line source
adam@0 1 #include <stdio.h>
adam@0 2 #include <string.h>
adam@0 3 #include <stdlib.h>
adam@0 4 #include <ctype.h>
adam@0 5 #include <sys/types.h>
adam@0 6 #include <sys/socket.h>
adam@0 7 #include <netinet/in.h>
adam@0 8 #include <arpa/inet.h>
adam@0 9
adam@0 10 #include <urweb.h>
adam@0 11
adam@0 12 struct headers {
adam@0 13 uw_Basis_string from, to, cc, bcc, subject;
adam@0 14 };
adam@0 15
adam@0 16 typedef struct headers *uw_Mail_headers;
adam@0 17
adam@0 18 uw_Mail_headers uw_Mail_empty = NULL;
adam@0 19
adam@0 20 static void header(uw_context ctx, uw_Basis_string s) {
adam@0 21 if (strlen(s) > 100)
adam@0 22 uw_error(ctx, FATAL, "Header value too long");
adam@0 23
adam@0 24 for (; *s; ++s)
adam@0 25 if (*s == '\r' || *s == '\n')
adam@0 26 uw_error(ctx, FATAL, "Header value contains newline");
adam@0 27 }
adam@0 28
adam@0 29 static void address(uw_context ctx, uw_Basis_string s) {
adam@0 30 header(ctx, s);
adam@0 31
adam@0 32 if (strchr(s, ','))
adam@0 33 uw_error(ctx, FATAL, "E-mail address contains comma");
adam@0 34 }
adam@0 35
adam@0 36 uw_Mail_headers uw_Mail_from(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
adam@0 37 // char **allowed = uw_get_global(ctx, "mail_from");
adam@0 38 // Might add this policy checking (or some expanded version of it) back later.
adam@0 39 uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
adam@0 40
adam@0 41 if (h)
adam@0 42 *h2 = *h;
adam@0 43 else
adam@0 44 memset(h2, 0, sizeof(*h2));
adam@0 45
adam@0 46 if (h2->from)
adam@0 47 uw_error(ctx, FATAL, "Duplicate From header");
adam@0 48
adam@0 49 /*
adam@0 50 if (!allowed)
adam@0 51 uw_error(ctx, FATAL, "No From address whitelist has been set. Perhaps you are not authorized to send e-mail.");
adam@0 52
adam@0 53 if (!(allowed[0] && !strcmp(allowed[0], "*"))) {
adam@0 54 for (; *allowed; ++allowed)
adam@0 55 if (!strcmp(*allowed, s))
adam@0 56 goto ok;
adam@0 57
adam@0 58 uw_error(ctx, FATAL, "From address is not in whitelist");
adam@0 59 }
adam@0 60
adam@0 61 ok:
adam@0 62 */
adam@0 63 address(ctx, s);
adam@0 64 h2->from = s;
adam@0 65
adam@0 66 return h2;
adam@0 67 }
adam@0 68
adam@0 69 uw_Mail_headers uw_Mail_to(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
adam@0 70 uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
adam@0 71 if (h)
adam@0 72 *h2 = *h;
adam@0 73 else
adam@0 74 memset(h2, 0, sizeof(*h2));
adam@0 75
adam@0 76 address(ctx, s);
adam@0 77 if (h2->to) {
adam@4 78 uw_Basis_string all = uw_malloc(ctx, strlen(h2->to) + 2 + strlen(s));
adam@0 79 sprintf(all, "%s,%s", h2->to, s);
adam@0 80 h2->to = all;
adam@0 81 } else
adam@0 82 h2->to = s;
adam@0 83
adam@0 84 return h2;
adam@0 85 }
adam@0 86
adam@0 87 uw_Mail_headers uw_Mail_cc(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
adam@0 88 uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
adam@0 89 if (h)
adam@0 90 *h2 = *h;
adam@0 91 else
adam@0 92 memset(h2, 0, sizeof(*h2));
adam@0 93
adam@0 94 address(ctx, s);
adam@0 95 if (h2->cc) {
adam@5 96 uw_Basis_string all = uw_malloc(ctx, strlen(h2->cc) + 2 + strlen(s));
adam@0 97 sprintf(all, "%s,%s", h2->cc, s);
adam@0 98 h2->cc = all;
adam@0 99 } else
adam@0 100 h2->cc = s;
adam@0 101
adam@0 102 return h2;
adam@0 103 }
adam@0 104
adam@0 105 uw_Mail_headers uw_Mail_bcc(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
adam@0 106 uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
adam@0 107 if (h)
adam@0 108 *h2 = *h;
adam@0 109 else
adam@0 110 memset(h2, 0, sizeof(*h2));
adam@0 111
adam@0 112 address(ctx, s);
adam@0 113 if (h2->bcc) {
adam@5 114 uw_Basis_string all = uw_malloc(ctx, strlen(h2->bcc) + 2 + strlen(s));
adam@0 115 sprintf(all, "%s,%s", h2->bcc, s);
adam@0 116 h2->bcc = all;
adam@0 117 } else
adam@0 118 h2->bcc = s;
adam@0 119
adam@0 120 return h2;
adam@0 121 }
adam@0 122
adam@0 123 uw_Mail_headers uw_Mail_subject(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
adam@0 124 uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
adam@0 125
adam@0 126 if (h)
adam@0 127 *h2 = *h;
adam@0 128 else
adam@0 129 memset(h2, 0, sizeof(*h2));
adam@0 130
adam@0 131 if (h2->subject)
adam@0 132 uw_error(ctx, FATAL, "Duplicate Subject header");
adam@0 133
adam@0 134 header(ctx, s);
adam@0 135 h2->subject = s;
adam@0 136
adam@0 137 return h2;
adam@0 138 }
adam@0 139
adam@0 140 typedef struct {
adam@0 141 uw_context ctx;
adam@0 142 uw_Mail_headers h;
adam@0 143 uw_Basis_string body, xbody;
adam@0 144 } job;
adam@0 145
adam@0 146 #define BUFLEN 1024
adam@0 147
adam@0 148 static int smtp_read(uw_context ctx, int sock, char *buf, ssize_t *pos) {
adam@0 149 char *s;
adam@0 150
adam@0 151 while (1) {
adam@0 152 ssize_t recvd;
adam@0 153
adam@0 154 buf[*pos] = 0;
adam@0 155
adam@0 156 if ((s = strchr(buf, '\n'))) {
adam@0 157 int n;
adam@0 158
adam@0 159 *s = 0;
adam@0 160
adam@0 161 if (sscanf(buf, "%d ", &n) != 1) {
adam@0 162 close(sock);
adam@0 163 uw_set_error_message(ctx, "Mail server response does not begin with a code.");
adam@0 164 return 0;
adam@0 165 }
adam@0 166
adam@0 167 *pos -= s - buf + 1;
adam@0 168 memmove(buf, s+1, *pos);
adam@0 169
adam@0 170 return n;
adam@0 171 }
adam@0 172
adam@0 173 recvd = recv(sock, buf + *pos, BUFLEN - *pos - 1, 0);
adam@0 174
adam@0 175 if (recvd == 0) {
adam@0 176 close(sock);
adam@0 177 uw_set_error_message(ctx, "Mail server response ends unexpectedly.");
adam@0 178 return 0;
adam@0 179 } else if (recvd < 0) {
adam@0 180 close(sock);
adam@0 181 uw_set_error_message(ctx, "Error reading mail server response.");
adam@0 182 return 0;
adam@0 183 }
adam@0 184
adam@0 185 *pos += recvd;
adam@0 186 }
adam@0 187 }
adam@0 188
adam@0 189 static int really_string(int sock, const char *s) {
adam@0 190 return uw_really_send(sock, s, strlen(s));
adam@0 191 }
adam@0 192
adam@0 193 static int sendAddrs(const char *kind, uw_context ctx, int sock, char *s, char *buf, ssize_t *pos) {
adam@0 194 char *p;
adam@0 195 char out[BUFLEN];
adam@0 196
adam@0 197 if (!s)
adam@0 198 return 0;
adam@0 199
adam@0 200 for (p = strchr(s, ','); p; p = strchr(p+1, ',')) {
adam@0 201 *p = 0;
adam@0 202
adam@2 203 snprintf(out, sizeof(out), "RCPT TO:%s\r\n", s);
adam@0 204 out[sizeof(out)-1] = 0;
adam@0 205 *p = ',';
adam@0 206
adam@0 207 if (really_string(sock, out) < 0) {
adam@0 208 close(sock);
adam@0 209 uw_set_error_message(ctx, "Error sending RCPT TO for %s", kind);
adam@0 210 return 1;
adam@0 211 }
adam@0 212
adam@0 213 if (smtp_read(ctx, sock, buf, pos) != 250) {
adam@0 214 close(sock);
adam@0 215 uw_set_error_message(ctx, "Mail server doesn't respond to %s RCPT TO with code 250.", kind);
adam@0 216 return 1;
adam@0 217 }
adam@6 218
adam@6 219 s = p+1;
adam@0 220 }
adam@0 221
adam@0 222 if (*s) {
adam@2 223 snprintf(out, sizeof(out), "RCPT TO:%s\r\n", s);
adam@0 224 out[sizeof(out)-1] = 0;
adam@0 225
adam@0 226 if (really_string(sock, out) < 0) {
adam@0 227 close(sock);
adam@0 228 uw_set_error_message(ctx, "Error sending RCPT TO for %s", kind);
adam@0 229 return 1;
adam@0 230 }
adam@0 231
adam@0 232 if (smtp_read(ctx, sock, buf, pos) != 250) {
adam@0 233 close(sock);
adam@0 234 uw_set_error_message(ctx, "Mail server doesn't respond to %s RCPT TO with code 250.", kind);
adam@0 235 return 1;
adam@0 236 }
adam@0 237 }
adam@0 238
adam@0 239 return 0;
adam@0 240 }
adam@0 241
adam@0 242 static void commit(void *data) {
adam@0 243 job *j = data;
adam@0 244 int sock;
adam@0 245 struct sockaddr_in my_addr;
adam@0 246 char buf[BUFLEN], out[BUFLEN];
adam@0 247 ssize_t pos = 0;
adam@0 248 char *s;
adam@0 249
adam@0 250 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
adam@0 251 uw_set_error_message(j->ctx, "Can't create socket for mail server connection");
adam@0 252 return;
adam@0 253 }
adam@0 254
adam@0 255 my_addr.sin_family = AF_INET;
adam@0 256 my_addr.sin_port = htons(25);
adam@0 257 my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
adam@0 258 memset(my_addr.sin_zero, 0, sizeof my_addr.sin_zero);
adam@0 259
adam@0 260 if (connect(sock, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adam@0 261 close(sock);
adam@0 262 uw_set_error_message(j->ctx, "Error connecting to mail server");
adam@0 263 return;
adam@0 264 }
adam@0 265
adam@0 266 if (smtp_read(j->ctx, sock, buf, &pos) != 220) {
adam@0 267 close(sock);
adam@0 268 uw_set_error_message(j->ctx, "Mail server doesn't greet with code 220.");
adam@0 269 return;
adam@0 270 }
adam@0 271
adam@2 272 if (really_string(sock, "HELO localhost\r\n") < 0) {
adam@0 273 close(sock);
adam@0 274 uw_set_error_message(j->ctx, "Error sending HELO");
adam@0 275 return;
adam@0 276 }
adam@0 277
adam@0 278 if (smtp_read(j->ctx, sock, buf, &pos) != 250) {
adam@0 279 close(sock);
adam@0 280 uw_set_error_message(j->ctx, "Mail server doesn't respond to HELO with code 250.");
adam@0 281 return;
adam@0 282 }
adam@0 283
adam@2 284 snprintf(out, sizeof(out), "MAIL FROM:%s\r\n", j->h->from);
adam@0 285 out[sizeof(out)-1] = 0;
adam@0 286
adam@0 287 if (really_string(sock, out) < 0) {
adam@0 288 close(sock);
adam@0 289 uw_set_error_message(j->ctx, "Error sending MAIL FROM");
adam@0 290 return;
adam@0 291 }
adam@0 292
adam@0 293 if (smtp_read(j->ctx, sock, buf, &pos) != 250) {
adam@0 294 close(sock);
adam@0 295 uw_set_error_message(j->ctx, "Mail server doesn't respond to MAIL FROM with code 250.");
adam@0 296 return;
adam@0 297 }
adam@0 298
adam@0 299 if (sendAddrs("To", j->ctx, sock, j->h->to, buf, &pos)) return;
adam@0 300 if (sendAddrs("Cc", j->ctx, sock, j->h->cc, buf, &pos)) return;
adam@0 301 if (sendAddrs("Bcc", j->ctx, sock, j->h->bcc, buf, &pos)) return;
adam@0 302
adam@2 303 if (really_string(sock, "DATA\r\n") < 0) {
adam@0 304 close(sock);
adam@0 305 uw_set_error_message(j->ctx, "Error sending DATA");
adam@0 306 return;
adam@0 307 }
adam@0 308
adam@0 309 if (smtp_read(j->ctx, sock, buf, &pos) != 354) {
adam@0 310 close(sock);
adam@0 311 uw_set_error_message(j->ctx, "Mail server doesn't respond to DATA with code 354.");
adam@0 312 return;
adam@0 313 }
adam@0 314
adam@0 315 snprintf(out, sizeof(out), "From: %s\r\n", j->h->from);
adam@0 316 out[sizeof(out)-1] = 0;
adam@0 317
adam@0 318 if (really_string(sock, out) < 0) {
adam@0 319 close(sock);
adam@0 320 uw_set_error_message(j->ctx, "Error sending From");
adam@0 321 return;
adam@0 322 }
adam@0 323
adam@0 324 if (j->h->subject) {
adam@0 325 snprintf(out, sizeof(out), "Subject: %s\r\n", j->h->subject);
adam@0 326 out[sizeof(out)-1] = 0;
adam@0 327
adam@0 328 if (really_string(sock, out) < 0) {
adam@0 329 close(sock);
adam@0 330 uw_set_error_message(j->ctx, "Error sending Subject");
adam@0 331 return;
adam@0 332 }
adam@0 333 }
adam@0 334
adam@0 335 if (j->h->to) {
adam@0 336 snprintf(out, sizeof(out), "To: %s\r\n", j->h->to);
adam@0 337 out[sizeof(out)-1] = 0;
adam@0 338
adam@0 339 if (really_string(sock, out) < 0) {
adam@0 340 close(sock);
adam@0 341 uw_set_error_message(j->ctx, "Error sending To");
adam@0 342 return;
adam@0 343 }
adam@0 344 }
adam@0 345
adam@0 346 if (j->h->cc) {
adam@0 347 snprintf(out, sizeof(out), "Cc: %s\r\n", j->h->cc);
adam@0 348 out[sizeof(out)-1] = 0;
adam@0 349
adam@0 350 if (really_string(sock, out) < 0) {
adam@0 351 close(sock);
adam@0 352 uw_set_error_message(j->ctx, "Error sending Cc");
adam@0 353 return;
adam@0 354 }
adam@0 355 }
adam@0 356
adam@0 357 if (j->h->bcc) {
adam@0 358 snprintf(out, sizeof(out), "Bcc: %s\r\n", j->h->bcc);
adam@0 359 out[sizeof(out)-1] = 0;
adam@0 360
adam@0 361 if (really_string(sock, out) < 0) {
adam@0 362 close(sock);
adam@0 363 uw_set_error_message(j->ctx, "Error sending Bcc");
adam@0 364 return;
adam@0 365 }
adam@0 366 }
adam@0 367
adam@0 368 if ((s = uw_get_global(j->ctx, "extra_mail_headers"))) {
adam@0 369 if (really_string(sock, s) < 0) {
adam@0 370 close(sock);
adam@0 371 uw_set_error_message(j->ctx, "Error sending extra headers");
adam@0 372 return;
adam@0 373 }
adam@0 374 }
adam@0 375
adam@0 376 if (j->xbody) {
adam@0 377 char separator[11];
adam@0 378 separator[sizeof(separator)-1] = 0;
adam@0 379
adam@0 380 do {
adam@0 381 int i;
adam@0 382
adam@0 383 for (i = 0; i < sizeof(separator)-1; ++i)
adam@0 384 separator[i] = 'A' + (rand() % 26);
adam@0 385 } while (strstr(j->body, separator) || strstr(j->xbody, separator));
adam@0 386
adam@0 387 snprintf(out, sizeof(out), "MIME-Version: 1.0\r\n"
adam@0 388 "Content-Type: multipart/alternative; boundary=\"%s\"\r\n"
adam@0 389 "\r\n"
adam@0 390 "--%s\r\n"
adam@0 391 "Content-Type: text/plain\r\n"
adam@0 392 "\r\n",
adam@0 393 separator, separator);
adam@0 394 out[sizeof(out)-1] = 0;
adam@0 395
adam@0 396 if (really_string(sock, out) < 0) {
adam@0 397 close(sock);
adam@0 398 uw_set_error_message(j->ctx, "Error sending multipart beginning");
adam@0 399 return;
adam@0 400 }
adam@0 401
adam@0 402 if (really_string(sock, j->body) < 0) {
adam@0 403 close(sock);
adam@0 404 uw_set_error_message(j->ctx, "Error sending message text body");
adam@0 405 return;
adam@0 406 }
adam@0 407
adam@0 408 snprintf(out, sizeof(out), "\r\n"
adam@0 409 "--%s\r\n"
adam@0 410 "Content-Type: text/html\r\n"
adam@0 411 "\r\n",
adam@0 412 separator);
adam@0 413 out[sizeof(out)-1] = 0;
adam@0 414
adam@0 415 if (really_string(sock, out) < 0) {
adam@0 416 close(sock);
adam@0 417 uw_set_error_message(j->ctx, "Error sending multipart middle");
adam@0 418 return;
adam@0 419 }
adam@0 420
adam@0 421 if (really_string(sock, j->xbody) < 0) {
adam@0 422 close(sock);
adam@0 423 uw_set_error_message(j->ctx, "Error sending message HTML body");
adam@0 424 return;
adam@0 425 }
adam@0 426
adam@0 427 snprintf(out, sizeof(out), "\r\n"
adam@0 428 "--%s--",
adam@0 429 separator);
adam@0 430 out[sizeof(out)-1] = 0;
adam@0 431
adam@0 432 if (really_string(sock, out) < 0) {
adam@0 433 close(sock);
adam@0 434 uw_set_error_message(j->ctx, "Error sending multipart end");
adam@0 435 return;
adam@0 436 }
adam@0 437 } else {
adam@0 438 if (really_string(sock, "Content-Type: text/plain\r\n\r\n") < 0) {
adam@0 439 close(sock);
adam@0 440 uw_set_error_message(j->ctx, "Error sending text Content-Type");
adam@0 441 return;
adam@0 442 }
adam@0 443
adam@0 444 if (really_string(sock, j->body) < 0) {
adam@0 445 close(sock);
adam@0 446 uw_set_error_message(j->ctx, "Error sending message body");
adam@0 447 return;
adam@0 448 }
adam@0 449 }
adam@0 450
adam@0 451 if (really_string(sock, "\r\n.\r\n") < 0) {
adam@0 452 close(sock);
adam@0 453 uw_set_error_message(j->ctx, "Error sending message terminator");
adam@0 454 return;
adam@0 455 }
adam@0 456
adam@0 457 if (smtp_read(j->ctx, sock, buf, &pos) != 250) {
adam@0 458 close(sock);
adam@0 459 uw_set_error_message(j->ctx, "Mail server doesn't respond to end of message with code 250.");
adam@0 460 return;
adam@0 461 }
adam@0 462
adam@2 463 if (really_string(sock, "QUIT\r\n") < 0) {
adam@0 464 close(sock);
adam@0 465 uw_set_error_message(j->ctx, "Error sending QUIT");
adam@0 466 return;
adam@0 467 }
adam@0 468
adam@0 469 if (smtp_read(j->ctx, sock, buf, &pos) != 221) {
adam@0 470 close(sock);
adam@0 471 uw_set_error_message(j->ctx, "Mail server doesn't respond to QUIT with code 221.");
adam@0 472 return;
adam@0 473 }
adam@0 474
adam@0 475 close(sock);
adam@0 476 }
adam@0 477
adam@0 478 uw_unit uw_Mail_send(uw_context ctx, uw_Mail_headers h, uw_Basis_string body, uw_Basis_string xbody) {
adam@0 479 job *j;
adam@0 480 char *s;
adam@0 481
adam@0 482 if (!h || !h->from)
adam@0 483 uw_error(ctx, FATAL, "No From address set for e-mail message");
adam@0 484
adam@0 485 if (!h->to && !h->cc && !h->bcc)
adam@0 486 uw_error(ctx, FATAL, "No recipients specified for e-mail message");
adam@0 487
adam@0 488 for (s = strchr(body, '.'); s; s = strchr(s+1, '.'))
adam@0 489 if ((s[1] == '\n' || s[1] == '\r')
adam@0 490 && (s <= body || s[-1] == '\n' || s[-1] == '\r'))
adam@0 491 uw_error(ctx, FATAL, "Message body contains a line with just a period");
adam@0 492
adam@0 493 if (xbody) {
adam@0 494 for (s = strchr(xbody, '.'); s; s = strchr(s+1, '.'))
adam@0 495 if ((s[1] == '\n' || s[1] == '\r')
adam@0 496 && (s <= xbody || s[-1] == '\n' || s[-1] == '\r'))
adam@0 497 uw_error(ctx, FATAL, "HTML message body contains a line with just a period");
adam@0 498 }
adam@0 499
adam@0 500 j = uw_malloc(ctx, sizeof(job));
adam@0 501
adam@0 502 j->ctx = ctx;
adam@0 503 j->h = h;
adam@0 504 j->body = body;
adam@0 505 j->xbody = xbody;
adam@0 506
adam@0 507 uw_register_transactional(ctx, j, commit, NULL, NULL);
adam@0 508
adam@0 509 return uw_unit_v;
adam@0 510 }