annotate mail.c @ 9:8966edef462a

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