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 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
|
/* smtpclient.c -- Routines for sending a message via SMTP
*
* Copyright (c) 1994-2008 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any legal
* details, please contact
* Carnegie Mellon University
* Center for Technology Transfer and Enterprise Creation
* 4615 Forbes Avenue
* Suite 302
* Pittsburgh, PA 15213
* (412) 268-7393, fax: (412) 268-7395
* innovation@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <assert.h>
#include <config.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sysexits.h>
#include <syslog.h>
#include "backend.h"
#include "xmalloc.h"
#include "global.h"
#include "smtpclient.h"
#include "telemetry.h"
/* generated headers are not necessarily in current directory */
#include "imap/imap_err.h"
typedef struct {
char code[3];
struct buf text; /* Zero-terminated reply text, excluding CRLF. OK to overwrite. */
int is_last;
} smtp_resp_t;
struct smtpclient {
/* SMTP backend. Client implementations can store
* their context data in backend->context. */
struct backend *backend;
/* Client implementations can free their context
* stored in backend->context. */
int (*free_context)(void *ctx);
/* Telemetry log */
int logfd;
/* Internal state */
hash_table *have_exts;
struct buf buf;
char *authid;
char *notify;
char *ret;
char *by;
unsigned long msgsize;
smtp_resp_t resp;
};
enum {
SMTPCLIENT_CAPA_DSN = (1 << 3),
SMTPCLIENT_CAPA_DELIVERBY = (1 << 4),
SMTPCLIENT_CAPA_SIZE = (1 << 5),
SMTPCLIENT_CAPA_STATUS = (1 << 6),
SMTPCLIENT_CAPA_FUTURE = (1 << 7),
SMTPCLIENT_CAPA_PRIORITY = (1 << 8),
SMTPCLIENT_CAPA_SENDCHECK = (1 << 9)
};
static struct protocol_t smtp_protocol =
{ "smtp", "smtp", TYPE_STD,
{ { { 0, "220 " },
{ "EHLO", "localhost", "250 ", NULL,
CAPAF_ONE_PER_LINE|CAPAF_SKIP_FIRST_WORD|CAPAF_DASH_STUFFING,
{ { "AUTH", CAPA_AUTH },
{ "STARTTLS", CAPA_STARTTLS },
{ "DSN", SMTPCLIENT_CAPA_DSN },
{ "DELIVERYBY", SMTPCLIENT_CAPA_DELIVERBY },
{ "SIZE", SMTPCLIENT_CAPA_SIZE },
{ "ENHANCEDSTATUSCODES", SMTPCLIENT_CAPA_STATUS },
{ "FUTURERELEASE", SMTPCLIENT_CAPA_FUTURE },
{ "MT-PRIORITY", SMTPCLIENT_CAPA_PRIORITY },
{ "SENDCHECK", SMTPCLIENT_CAPA_SENDCHECK },
{ NULL, 0 } } },
{ "STARTTLS", "220", "454", 0 },
{ "AUTH", 512, 0, "235", "5", "334 ", "*", NULL, 0 },
{ NULL, NULL, NULL },
{ "NOOP", NULL, "250" },
{ "QUIT", NULL, "221" } } }
};
/* SMTP protocol implementation */
typedef int smtp_readcb_t(smtpclient_t *sm, void *rock);
static int smtpclient_read(smtpclient_t *sm, smtp_readcb_t *cb, void *rock);
static int smtpclient_writebuf(smtpclient_t *sm, struct buf *buf, int flush);
static int smtpclient_ehlo(smtpclient_t *sm);
static int smtpclient_rset(smtpclient_t *sm);
static int smtpclient_quit(smtpclient_t *sm);
static int smtpclient_from(smtpclient_t *sm, smtp_addr_t *addr);
static int smtpclient_rcpt_to(smtpclient_t *sm, ptrarray_t *rcpt);
static int smtpclient_data(smtpclient_t *sm, struct protstream *data);
static void smtpclient_logerror(smtpclient_t *sm, const char *cmd, int r);
EXPORTED int smtpclient_open(smtpclient_t **smp)
{
int r = 0;
const char *backend = config_getstring(IMAPOPT_SMTP_BACKEND);
if (!strcmp(backend, "sendmail")) {
r = smtpclient_open_sendmail(smp);
}
else if (!strcmp(backend, "host")) {
r = smtpclient_open_host(config_getstring(IMAPOPT_SMTP_HOST), smp);
}
else {
syslog(LOG_ERR, "smtpclient_open: unknown backend: %s", backend);
r = IMAP_INTERNAL;
}
return r;
}
EXPORTED int smtpclient_close(smtpclient_t **smp)
{
if (!smp || !*smp) {
return 0;
}
int r = 0;
smtpclient_t *sm = *smp;
/* Close backend */
backend_disconnect(sm->backend);
if (sm->free_context) {
r = sm->free_context(sm->backend->context);
}
free(sm->backend);
sm->backend = NULL;
/* Close log */
if (sm->logfd != -1) {
close(sm->logfd);
}
sm->logfd = -1;
/* Free internal state */
if (sm->have_exts) {
free_hash_table(sm->have_exts, free);
free(sm->have_exts);
sm->have_exts = NULL;
}
buf_free(&sm->buf);
free(sm->by);
free(sm->ret);
free(sm->notify);
free(sm->authid);
buf_free(&sm->resp.text);
free(sm);
*smp = NULL;
return r;
}
/* Match the response code to an expected return code defined in rock.
*
* rock->code must be a zero-terminated string up to length 3 (excluding the
* NUL byte). Matching is performed by string matching the SMTP return
* code to the expected code, stopping at the zero byte.
*
* E.g. return code "250", "251" both would match a "2" or "25" rock->code.
*
* Also see Daniel Bernstein's site https://cr.yp.to/smtp/request.html:
* "I recommend that clients avoid looking past the first digit of the
* code, either 2, 3, 4, or 5. The other two digits and the text are
* primarily for human consumption. (Exception: See EHLO.)"
*
* Return IMAP_PROTOCOL_ERROR on mismatch, 0 on success.
*
* rock->cmd should describe the command whose response you're checking.
* If set, it'll be included in the error log on mismatch.
*
*/
struct expect_code_rock {
const char *cmd;
const char *code;
};
static int expect_code_cb(smtpclient_t *sm, void *rock)
{
size_t i;
const struct expect_code_rock *ecrock = (const struct expect_code_rock *) rock;
smtp_resp_t *resp = &sm->resp;
for (i = 0; i < 3 && ecrock->code[i]; i++) {
if (ecrock->code[i] != resp->code[i]) {
const char *text = buf_cstring(&resp->text);
syslog(LOG_ERR, "smtpclient: unexpected response%s%s: code=%c%c%c text=%s",
ecrock->cmd ? " to " : "",
ecrock->cmd ? ecrock->cmd : "",
resp->code[0], resp->code[1], resp->code[2], text);
/* Try to glean specific error from response */
if (CAPA(sm->backend, SMTPCLIENT_CAPA_STATUS)) {
if (text[2] == '1' && text[4] >= '1' && text[4] <= '3')
return IMAP_MAILBOX_NONEXISTENT;
else if (text[2] == '3' && text[4] == '4')
return IMAP_MESSAGE_TOO_LARGE;
else if (text[2] == '5' && text[4] == '3')
return IMAP_MAILBOX_DISABLED;
else if (text[2] == '3' && text[4] == '0')
return IMAP_REMOTE_DENIED;
}
else {
switch (atoi(resp->code)) {
case 421:
case 451:
case 554:
return IMAP_REMOTE_DENIED;
case 450:
case 550:
return IMAP_MAILBOX_DISABLED;
case 452:
case 552:
return IMAP_MESSAGE_TOO_LARGE;
case 553:
return IMAP_MAILBOX_NONEXISTENT;
}
}
return IMAP_PROTOCOL_ERROR;
}
}
return 0;
}
static int smtpclient_read(smtpclient_t *sm, smtp_readcb_t *cb, void *rock)
{
char buf[513]; /* Maximum length of reply line, see RFC 5321, 4.5.3.1.5. */
int r = IMAP_IOERROR;
do {
/* Read next reply line. */
if (!prot_fgets(buf, 513, sm->backend->in)) {
r = IMAP_IOERROR;
return r;
}
buf[512] = '\0';
/* Parse reply line. */
if (!isdigit(buf[0]) || !isdigit(buf[1]) || !isdigit(buf[2])) {
return IMAP_PROTOCOL_ERROR;
}
if (buf[3] != '-' && !isspace(buf[3])) {
return IMAP_PROTOCOL_ERROR;
}
char *p = memchr(buf + 4, '\n', 508);
if (p == NULL) {
return IMAP_PROTOCOL_ERROR;
}
if (*(p-1) == '\r') {
--p;
}
*p = '\0';
/* Call callback. */
memcpy(sm->resp.code, buf, 3);
buf_setcstr(&sm->resp.text, buf + 4);
sm->resp.is_last = isspace(buf[3]);
r = cb(sm, rock);
} while (!r && !sm->resp.is_last);
return r;
}
static int ehlo_cb(smtpclient_t *sm, void *rock __attribute__((unused)))
{
smtp_resp_t *resp = &sm->resp;
/* Is this the first response line? */
if (sm->have_exts == NULL) {
sm->have_exts = xzmalloc(sizeof(struct hash_table));
construct_hash_table(sm->have_exts, 16, 0);
return 0;
}
/* Add the extension */
const char *p = buf_cstring(&resp->text);
while (*p && !isspace(*p)) {
p++;
}
const char *args = isspace(*p) ? p + 1 : "";
buf_truncate(&resp->text, p - buf_base(&resp->text));
hash_insert(buf_cstring(&resp->text), xstrdup(args), sm->have_exts);
return 0;
}
static int smtpclient_writebuf(smtpclient_t *sm, struct buf *buf, int flush)
{
if (prot_putbuf(sm->backend->out, buf)) {
return IMAP_IOERROR;
}
if (flush && prot_flush(sm->backend->out)) {
return IMAP_IOERROR;
}
return 0;
}
static void smtpclient_logerror(smtpclient_t *sm, const char *cmd, int r)
{
if (r == IMAP_IOERROR) {
/* try to dig a more specific error out of the prot streams */
const char *errstr = prot_error(sm->backend->out);
if (!errstr) errstr = prot_error(sm->backend->in);
if (!errstr) errstr = error_message(r);
syslog(LOG_ERR, "smtpclient: %s during %s", errstr, cmd);
}
else {
syslog(LOG_ERR, "smtpclient: %s during %s", error_message(r), cmd);
}
}
static int smtpclient_ehlo(smtpclient_t *sm)
{
int r = 0;
/* Say EHLO */
buf_setcstr(&sm->buf, "EHLO localhost\r\n");
r = smtpclient_writebuf(sm, &sm->buf, 1);
if (r) goto done;
buf_reset(&sm->buf);
/* Process response */
r = smtpclient_read(sm, ehlo_cb, NULL);
done:
if (r) smtpclient_logerror(sm, "EHLO", r);
return r;
}
static int smtpclient_rset(smtpclient_t *sm)
{
int r = 0;
/* Say RSET */
buf_setcstr(&sm->buf, "RSET\r\n");
r = smtpclient_writebuf(sm, &sm->buf, 1);
if (r) goto done;
buf_reset(&sm->buf);
/* Process response */
struct expect_code_rock rock = { "RSET", "2" };
r = smtpclient_read(sm, expect_code_cb, &rock);
done:
if (r) smtpclient_logerror(sm, "RSET", r);
return r;
}
static int smtpclient_schk(smtpclient_t *sm, strarray_t *fromaddr)
{
int r = 0;
/* Say SCHK */
buf_setcstr(&sm->buf, "SCHK");
if (fromaddr) {
/* Add FROMADDR= parameters */
int i;
for (i = 0; i < strarray_size(fromaddr); i++) {
buf_appendcstr(&sm->buf, " FROMADDR=");
smtp_encode_esmtp_value(strarray_nth(fromaddr, i), &sm->buf);
}
}
buf_appendcstr(&sm->buf, "\r\n");
r = smtpclient_writebuf(sm, &sm->buf, 1);
if (r) goto done;
buf_reset(&sm->buf);
/* Process response */
struct expect_code_rock rock = { "SCHK", "2" };
r = smtpclient_read(sm, expect_code_cb, &rock);
done:
if (r) smtpclient_logerror(sm, "SCHK", r);
return r;
}
__attribute__((unused)) static int smtpclient_quit(smtpclient_t *sm)
{
int r = 0;
/* Say QUIT */
buf_setcstr(&sm->buf, "QUIT\r\n");
r = smtpclient_writebuf(sm, &sm->buf, 1);
if (r) goto done;
/* Don't insist on an answer */
struct expect_code_rock rock = { "QUIT", "2" };
r = smtpclient_read(sm, expect_code_cb, &rock);
if (r) {
syslog(LOG_INFO, "smtpclient: QUIT without reply: %s", error_message(r));
return r;
}
done:
if (r) smtpclient_logerror(sm, "QUIT", r);
return r;
}
static int write_addr(smtpclient_t *sm,
const char *cmd,
const smtp_addr_t *addr,
const ptrarray_t *extra_params)
{
int i, r = 0;
buf_reset(&sm->buf);
buf_printf(&sm->buf, "%s:<%s>", cmd, addr->addr);
for (i = 0; i < addr->params.count; i++) {
smtp_param_t *param = ptrarray_nth(&addr->params, i);
buf_appendcstr(&sm->buf, " ");
buf_appendcstr(&sm->buf, param->key);
if (!param->val) {
continue;
}
buf_appendcstr(&sm->buf, "=");
buf_appendcstr(&sm->buf, param->val);
}
for (i = 0; i < extra_params->count; i++) {
smtp_param_t *param = ptrarray_nth(extra_params, i);
buf_appendcstr(&sm->buf, " ");
buf_appendcstr(&sm->buf, param->key);
if (!param->val) {
continue;
}
buf_appendcstr(&sm->buf, "=");
buf_appendcstr(&sm->buf, param->val);
}
buf_appendcstr(&sm->buf, "\r\n");
r = smtpclient_writebuf(sm, &sm->buf, 1);
if (r) goto done;
struct expect_code_rock rock = { cmd, "2" };
r = smtpclient_read(sm, expect_code_cb, &rock);
if (r) goto done;
done:
if (r) smtpclient_logerror(sm, cmd, r);
buf_reset(&sm->buf);
return r;
}
/* Add key/value to extra, if it isn't defined in params */
static void smtp_params_set_extra(ptrarray_t *params, ptrarray_t *extra,
const char *key, const char *val)
{
int i;
for (i = 0; i < params->count; i++) {
smtp_param_t *param = ptrarray_nth(params, i);
if (!strcasecmp(param->key, key)) {
break;
}
}
if (i == params->count) {
smtp_param_t *param = xzmalloc(sizeof(smtp_param_t));
param->key = xstrdup(key);
param->val = xstrdup(val);
ptrarray_add(extra, param);
}
}
static void smtp_params_fini(ptrarray_t *params)
{
int i;
for (i = 0; i < params->count; i++) {
smtp_param_t *param = ptrarray_nth(params, i);
free(param->key);
free(param->val);
free(param);
}
ptrarray_fini(params);
}
/* Write a MAIL FROM command for address addr. */
static int smtpclient_from(smtpclient_t *sm, smtp_addr_t *addr)
{
ptrarray_t extra_params = PTRARRAY_INITIALIZER;
if (sm->authid && CAPA(sm->backend, CAPA_AUTH)) {
smtp_params_set_extra(&addr->params, &extra_params, "AUTH", sm->authid);
}
if (sm->ret && CAPA(sm->backend, SMTPCLIENT_CAPA_DSN)) {
smtp_params_set_extra(&addr->params, &extra_params, "RET", sm->ret);
}
if (sm->by && CAPA(sm->backend, SMTPCLIENT_CAPA_DELIVERBY)) {
smtp_params_set_extra(&addr->params, &extra_params, "BY", sm->by);
}
if (sm->msgsize && CAPA(sm->backend, SMTPCLIENT_CAPA_SIZE)) {
char szbuf[21];
snprintf(szbuf, sizeof(szbuf), "%lu", sm->msgsize);
smtp_params_set_extra(&addr->params, &extra_params, "SIZE", szbuf);
}
int r = write_addr(sm, "MAIL FROM", addr, &extra_params);
if (r) smtpclient_logerror(sm, "MAIL FROM", r);
smtp_params_fini(&extra_params);
return r;
}
/* Write a RCPT TO command for all addresses in rcpt. */
static int smtpclient_rcpt_to(smtpclient_t *sm, ptrarray_t *rcpts)
{
int i, r = 0;
for (i = 0; i < rcpts->count; i++) {
smtp_addr_t *addr = ptrarray_nth(rcpts, i);
ptrarray_t extra_params = PTRARRAY_INITIALIZER;
if (sm->notify && CAPA(sm->backend, SMTPCLIENT_CAPA_DSN)) {
smtp_params_set_extra(&addr->params, &extra_params, "NOTIFY", sm->notify);
}
int r1 = write_addr(sm, "RCPT TO", addr, &extra_params);
smtp_params_fini(&extra_params);
if (!r1) addr->completed = 1;
else if (!r) r = r1;
}
if (r) smtpclient_logerror(sm, "RCPT TO", r);
return r;
}
/* Write a DATA command using data as input. Data is dot-escaped
* before it is written to the SMTP backend. */
static int smtpclient_data(smtpclient_t *sm, struct protstream *data)
{
int r = 0;
/* Write DATA */
buf_setcstr(&sm->buf, "DATA\r\n");
r = smtpclient_writebuf(sm, &sm->buf, 1);
if (r) goto done;
buf_reset(&sm->buf);
/* Expect Start Input */
struct expect_code_rock rock = { "DATA", "3" };
r = smtpclient_read(sm, expect_code_cb, &rock);
if (r) goto done;
/* Write message, escaping dot characters. */
buf_ensure(&sm->buf, 4096);
int c;
int prev1 = 256, prev2 = 256;
int at_start = 1;
const char *eot = "\r\n.\r\n";
while ((c = prot_getc(data)) != EOF) {
if (c == '.' && at_start) {
prot_ungetc(c, data);
c = '.';
}
at_start = 0;
sm->buf.s[sm->buf.len++] = (unsigned char) c;
if (sm->buf.len == 4096) {
r = smtpclient_writebuf(sm, &sm->buf, 0);
if (r) goto done;
buf_reset(&sm->buf);
}
if (c == '\n') {
at_start = 1;
}
prev2 = prev1;
prev1 = c;
}
r = smtpclient_writebuf(sm, &sm->buf, 1);
if (r) goto done;
buf_reset(&sm->buf);
/* If message ends with CRLF, omit pre-dot CRLF. */
if (prev2 == '\r' && prev1 == '\n')
eot = ".\r\n";
/* Write end-of-text. */
buf_setcstr(&sm->buf, eot);
r = smtpclient_writebuf(sm, &sm->buf, 1);
if (r) goto done;
buf_reset(&sm->buf);
/* Expect OK */
rock.cmd = "EOT";
rock.code = "2";
r = smtpclient_read(sm, expect_code_cb, &rock);
if (r) goto done;
done:
if (r) smtpclient_logerror(sm, "DATA", r);
return r;
}
static int validate_envelope_params(ptrarray_t *params)
{
if (!params) return 0;
int i;
for (i = 0; i < ptrarray_size(params); i++) {
smtp_param_t *param = ptrarray_nth(params, i);
if (!smtp_is_valid_esmtp_keyword(param->key)) {
syslog(LOG_ERR, "smtpclient: invalid estmp keyword: \"%s\"", param->key);
return IMAP_PROTOCOL_ERROR;
}
if (!strcasecmp(param->key, "AUTH")) {
syslog(LOG_ERR, "smptclient: rejecting AUTH parameter in envelope");
return IMAP_PERMISSION_DENIED;
}
if (param->val && !smtp_is_valid_esmtp_value(param->val)) {
syslog(LOG_ERR, "smtpclient: invalid estmp value: \"%s\"", param->val);
return IMAP_PROTOCOL_ERROR;
}
}
return 0;
}
static int validate_envelope(smtp_envelope_t *env)
{
int i, r;
if (!env->from.addr) {
syslog(LOG_ERR, "smtpclient: envelope missing sender");
return IMAP_PROTOCOL_ERROR;
}
r = validate_envelope_params(&env->from.params);
if (r) return r;
if (!env->rcpts.count) {
syslog(LOG_ERR, "smtpclient: envelope missing recipients");
return IMAP_PROTOCOL_ERROR;
}
for (i = 0; i < env->rcpts.count; i++) {
smtp_addr_t *addr = ptrarray_nth(&env->rcpts, i);
if (!addr->addr) {
syslog(LOG_ERR, "smtpclient: invalid recipient at position %d", i);
return IMAP_PROTOCOL_ERROR;
}
r = validate_envelope_params(&addr->params);
if (r) return r;
}
return 0;
}
static int smtpclient_sendenv(smtpclient_t *sm, smtp_envelope_t *env)
{
int r = 0;
if (sm->msgsize) {
unsigned long maxsize = smtpclient_get_maxsize(sm);
if (maxsize && maxsize < sm->msgsize) {
r = IMAP_MESSAGE_TOO_LARGE;
goto done;
}
}
r = validate_envelope(env);
if (r) goto done;
r = smtpclient_from(sm, &env->from);
if (r) goto done;
r = smtpclient_rcpt_to(sm, &env->rcpts);
if (r) goto done;
done:
return r;
}
EXPORTED int smtpclient_sendprot(smtpclient_t *sm,
smtp_envelope_t *env, struct protstream *data)
{
int r = smtpclient_sendenv(sm, env);
if (r) return r;
return smtpclient_data(sm, data);
}
EXPORTED int smtpclient_send(smtpclient_t *sm,
smtp_envelope_t *env, struct buf *data)
{
struct protstream *p = prot_readmap(buf_base(data), buf_len(data));
smtpclient_set_size(sm, buf_len(data));
int r = smtpclient_sendprot(sm, env, p);
prot_free(p);
return r;
}
EXPORTED int smtpclient_sendcheck(smtpclient_t *sm, smtp_envelope_t *env,
size_t size, strarray_t *fromaddr)
{
if (size > 0) smtpclient_set_size(sm, size);
int r = smtpclient_sendenv(sm, env);
if (r) return r;
/* simply pre-flighting the envelope */
if (CAPA(sm->backend, SMTPCLIENT_CAPA_SENDCHECK)) {
r = smtpclient_schk(sm, fromaddr);
}
else {
r = smtpclient_rset(sm);
}
return r;
}
EXPORTED void smtpclient_set_auth(smtpclient_t *sm, const char *authid)
{
free(sm->authid);
sm->authid = xstrdupnull(authid);
}
EXPORTED void smtpclient_set_notify(smtpclient_t *sm, const char *value)
{
free(sm->notify);
sm->notify = xstrdupnull(value);
}
EXPORTED void smtpclient_set_ret(smtpclient_t *sm, const char *value)
{
free(sm->ret);
sm->ret = xstrdupnull(value);
}
EXPORTED void smtpclient_set_by(smtpclient_t *sm, const char *value)
{
free(sm->by);
sm->by = xstrdupnull(value);
}
EXPORTED void smtpclient_set_size(smtpclient_t *sm, unsigned long value)
{
sm->msgsize = value;
}
EXPORTED unsigned long smtpclient_get_maxsize(smtpclient_t *sm)
{
unsigned long maxsize = 0;
if (CAPA(sm->backend, SMTPCLIENT_CAPA_SIZE)) {
const char *sizestr = smtpclient_has_ext(sm, "SIZE");
if (sizestr) maxsize = strtoul(sizestr, NULL, 10);
}
return maxsize;
}
EXPORTED const char *smtpclient_get_resp_text(smtpclient_t *sm)
{
return buf_cstring(&sm->resp.text);
}
/* SMTP backend implementations */
static int smtpclient_new(smtpclient_t **smp,
struct backend *backend,
int (*freectx)(void *), int logfd)
{
smtpclient_t *sm = xzmalloc(sizeof(smtpclient_t));
sm->backend = backend;
sm->free_context = freectx;
sm->logfd = logfd;
*smp = sm;
return 0;
}
EXPORTED const char *smtpclient_has_ext(smtpclient_t *sm, const char *name)
{
if (!sm->have_exts) {
int r = smtpclient_ehlo(sm);
if (r) {
syslog(LOG_ERR, "smtpclient: can't EHLO for extensions: %s",
error_message(r));
return NULL;
}
}
return hash_lookup(name, sm->have_exts);
}
/* TCP host backend */
typedef struct {
int sockfd;
} smtpclient_host_t;
EXPORTED int smtpclient_open_host(const char *addr, smtpclient_t **smp)
{
struct backend *bk = NULL;
int r = 0;
char *myaddr = NULL;
int logfd = -1;
/* Setup SASL for authentication, if any */
sasl_callback_t *sasl_cb = NULL;
if (config_getstring(IMAPOPT_SMTP_AUTH_AUTHNAME)) {
sasl_cb = mysasl_callbacks(NULL /*userid*/,
config_getstring(IMAPOPT_SMTP_AUTH_AUTHNAME),
config_getstring(IMAPOPT_SMTP_AUTH_REALM),
config_getstring(IMAPOPT_SMTP_AUTH_PASSWORD));
} else {
myaddr = strconcat(addr, "/noauth", NULL);
}
logfd = telemetry_log("smtpclient.host", NULL, NULL, 0);
/* Connect to backend */
const char *host = myaddr ? myaddr : addr;
syslog(LOG_DEBUG, "smtpclient_open: connecting to host: %s", host);
bk = backend_connect(NULL, host, &smtp_protocol, NULL, sasl_cb, NULL, logfd);
if (sasl_cb) free_callbacks(sasl_cb);
if (!bk) {
syslog(LOG_ERR, "smptclient_open: can't connect to host: %s", host);
if (logfd != -1) close(logfd);
r = IMAP_INTERNAL;
goto done;
}
r = smtpclient_new(smp, bk, /*freectx*/NULL, logfd);
done:
free(myaddr);
return r;
}
EXPORTED int smtp_is_valid_esmtp_keyword(const char *val)
{
if (!isascii(*val) || !isalnum(*val)) {
return 0;
}
for (val = val + 1; *val; val++) {
if (!isascii(*val) || (*val != '-' && !isalnum(*val))) {
return 0;
}
}
return 1;
}
EXPORTED int smtp_is_valid_esmtp_value(const char *val)
{
if (*val == '\0') return 0;
for ( ; *val; val++) {
if (*val == '=' || *val < '!' || *val > '~') {
return 0;
}
}
return 1;
}
/* Encodes into the current location of the struct buf.
The caller must buf_reset() before calling, if necessary. */
EXPORTED void smtp_encode_esmtp_value(const char *val, struct buf *xtext)
{
const char *p;
for (p = val; *p; p++) {
if (('!' <= *p && *p <= '~') && *p != '=' && *p != '+') {
buf_putc(xtext, *p);
}
else buf_printf(xtext, "+%02X", *p);
}
}
EXPORTED smtp_addr_t *smtp_envelope_set_from(smtp_envelope_t *env, const char *addr)
{
smtp_params_fini(&env->from.params);
free(env->from.addr);
env->from.addr = xstrdup(addr);
return &env->from;
}
EXPORTED smtp_addr_t *smtp_envelope_add_rcpt(smtp_envelope_t *env, const char *addr)
{
smtp_addr_t *rcpt = xzmalloc(sizeof(smtp_addr_t));
rcpt->addr = xstrdup(addr);
ptrarray_append(&env->rcpts, rcpt);
return rcpt;
}
EXPORTED void smtp_envelope_fini(smtp_envelope_t *env)
{
smtp_params_fini(&env->from.params);
free(env->from.addr);
smtp_addr_t *a;
while ((a = ptrarray_pop(&env->rcpts))) {
smtp_params_fini(&a->params);
free(a->addr);
free(a);
}
ptrarray_fini(&env->rcpts);
}
/* Sendmail process backend */
typedef struct {
int infd;
int outfd;
pid_t pid;
} smtpclient_sendmail_ctx_t;
static int smtpclient_sendmail_freectx(smtpclient_sendmail_ctx_t *ctx)
{
if (!ctx) return 0;
if (ctx->pid && waitpid(ctx->pid, NULL, 0) < 0) {
syslog(LOG_ERR, "waitpid(): %m");
}
if (ctx->infd >= 0)
close(ctx->infd);
if (ctx->outfd >= 0)
close(ctx->outfd);
free(ctx);
return 0;
}
EXPORTED int smtpclient_open_sendmail(smtpclient_t **smp)
{
struct backend *bk = NULL;
int r = 0;
int p_child[2];
int p_parent[2];
int logfd = -1;
/* Create the pipes and fork */
r = pipe(p_child);
if (!r) {
r = pipe(p_parent);
}
if (r < 0) {
syslog(LOG_ERR, "smtpclient_open: can't create pipe: %m");
r = IMAP_SYS_ERROR;
goto done;
}
pid_t pid = fork();
if (pid < 0) {
syslog(LOG_ERR, "smtpclient_open: can't fork: %m");
r = IMAP_SYS_ERROR;
goto done;
}
if (pid == 0) {
/* child process */
close(p_child[1]);
dup2(p_child[0], STDIN_FILENO);
close(p_child[0]);
close(p_parent[0]);
dup2(p_parent[1], STDOUT_FILENO);
close(p_parent[1]);
execl(config_getstring(IMAPOPT_SENDMAIL), "sendmail", "-bs", (char *)NULL);
syslog(LOG_ERR, "smtpclient_open: can't exec sendmail: %m");
exit(1);
}
/* parent process */
close(p_child[0]);
p_child[0] = -1;
close(p_parent[1]);
p_parent[1] = -1;
smtpclient_sendmail_ctx_t *ctx = xmalloc(sizeof(smtpclient_sendmail_ctx_t));
ctx->infd = p_parent[0]; /* reader */
ctx->outfd = p_child[1]; /* writer */
ctx->pid = pid;
logfd = telemetry_log("smtpclient.sendmail", NULL, NULL, 0);
/* Create backend and setup context */
bk = backend_connect_pipe(ctx->infd, ctx->outfd, &smtp_protocol, 0, logfd);
if (!bk) {
syslog(LOG_ERR, "smptclient_open: can't open sendmail backend");
r = IMAP_INTERNAL;
smtpclient_sendmail_freectx(ctx);
if (logfd != -1) close(logfd);
goto done;
}
bk->context = ctx;
r = smtpclient_new(smp, bk, (int (*)(void *)) smtpclient_sendmail_freectx, logfd);
done:
return r;
}
|