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 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
|
/*
NAME
mockmta - mock MTA server for use in test suites
SYNOPSIS
mockmta [-ad] [-c CERT] [-f CA] [-k KEY] [-p PORT] [-t SEC] [DUMPFILE]
DESCRIPTION
Starts a mock MTA, which behaves almost identically to the real one,
except that it does not actually inject messages to the mail transport
system. Instead, each accepted message is logged to the DUMPFILE.
Being a mailutils test tool, mockmta is written without relying on
the mailutils libraries. Only libc and, optionally, GnuTLS functions
are used.
No attempts are made to interpret the data supplied during the STMP
transaction, such as domain names, email addresses, etc, neither is
the material supplied in the DATA command verified to be a valid
email message. Except for being logged to DUMPFILE, these data are
ignored.
Incoming SMTP sessions are processed sequentially. Listening for
incoming requests is blocked while an SMTP session is active.
The utility listens for incoming requests on localhost. If the port
to listen on is not given explicitly via the -p option, mockmta
selects a random unused port and listens on it. The selected port
number is printed on the standard output.
By default, mockmta starts as a foreground process. If the DUMPFILE
argument is not supplied, messages will be logged to the standard
output.
If the -d option is given, mockmta detaches from the terminal and runs
in daemon mode. It prints the PID of the daemon process on the standard
output. The daemon will terminate after 60 seconds. This value can be
configured using the -t option. When running in daemon mode, the
DUMPFILE argument becomes mandatory.
To enable the STARTTLS ESMTP command, supply the names of the certificate
(-c CERT) and certificate key (-k KEY) files.
Output summary
Depending on the command line options given, mockmta can output port
number it listens on and the PID of the started daemon process. The
four possible variants are summarized in the table below:
1. Neither -d nor -p are given.
Prints the selected port number.
2. -p is given, but -d is not
Nothing is printed.
3. -d is given, but -p is not
Prints two numbers, each on a separate line:
Port number
PID
4. Both -d and -p are given.
Prints PID of the daemon process.
OPTIONS
-a Append to DUMPFILE instead of overwriting it.
-c CERT Name of the certificate file.
-d Daemon mode
-f CA Name of certificate authority file.
-k KEY Name of the certificate key file.
-p PORT Listen on this port.
-t SEC Terminate the daemon forcefully after this number of seconds.
Default is 60. Valid only in daemon mode (-d).
DUMP FORMAT
Each message is represented as a series of records:
MSGID: <numeric>
Four-digit sequential message identifier.
DOMAIN: <string>
EHLO (or HELO) domain.
SENDER: <string>
Sender email address as given by the MAIL command.
NRCPT: <numeric>
Number of recipients.
The list of recipients follows this line. Each record in the list is
RCPT[<I>]: <string>
where <I> is 0-based index of the recipient in recipient table.
LENGTH: <N>
Total length of the data section, including terminating dot and
newline. Notice, that line ending is changed from CRLF to LF
prior to length calculation, so that each line, including the
dot terminator, ends with one ASCII 10 character.
This line is followed by <N> bytes representing the material received
after the DATA SMTP keyword.
Message dump is terminated by a single LF character.
EXIT CODES
0 Success.
1 Failure (see stderr for details).
2 Command line usage error.
BUGS
At most 32 RCPT commands are allowed.
AUTHOR
Sergey Poznyakoff <gray@gnu.org>
LICENSE
This program is part of GNU Mailutils testsuite.
Copyright (C) 2020-2025 Free Software Foundation, Inc.
Mockmta 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, or (at your option)
any later version.
Mockmta 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
char *progname;
int daemon_opt;
int daemon_timeout = 60;
int port;
int msgid = 1;
FILE *logfile;
enum
{
EX_OK,
EX_FAILURE,
EX_USAGE
};
static void
terror (char const *fmt, ...)
{
va_list ap;
int m;
static char *fmtbuf = NULL;
static size_t fmtsize = 0;
int ec = errno;
char const *es = NULL;
size_t len;
for (m = 0; fmt[m += strcspn (fmt + m, "%")]; )
{
m++;
if (fmt[m] == 'm')
break;
}
len = strlen (fmt) + 1;
if (fmt[m])
{
es = strerror (ec);
len += strlen (es) - 2;
}
if (len > fmtsize)
{
fmtsize = len;
fmtbuf = realloc (fmtbuf, fmtsize);
if (!fmtbuf)
{
perror ("realloc");
exit (EX_FAILURE);
}
}
if (es)
{
memcpy (fmtbuf, fmt, m - 1);
memcpy (fmtbuf + m - 1, es, strlen (es) + 1);
strcat (fmtbuf, fmt + m + 1);
}
else
strcpy (fmtbuf, fmt);
fprintf (stderr, "%s: ", progname);
va_start (ap, fmt);
vfprintf (stderr, fmtbuf, ap);
va_end (ap);
fputc ('\n', stderr);
}
static void
nomemory (void)
{
terror ("out of memory");
exit (EX_FAILURE);
}
struct iodrv
{
int (*drv_read) (void *, char *, size_t, size_t *);
int (*drv_write) (void *, char *, size_t, size_t *);
void (*drv_close) (void *);
const char *(*drv_strerror) (void *, int);
};
#define IOBUFSIZE 1024
struct iobase
{
struct iodrv *iob_drv;
char iob_buf[IOBUFSIZE];
size_t iob_start;
size_t iob_level;
int iob_errno;
int iob_eof;
};
static struct iobase *
iobase_create (struct iodrv *drv, size_t size)
{
struct iobase *bp;
bp = calloc (1, size);
if (!bp)
nomemory ();
bp->iob_drv = drv;
bp->iob_start = 0;
bp->iob_level = 0;
bp->iob_errno = 0;
bp->iob_eof = 0;
return bp;
}
/* Number of data bytes in buffer */
static inline size_t
iobase_data_bytes (struct iobase *bp)
{
return bp->iob_level - bp->iob_start;
}
/* Pointer to the first data byte */
static inline char *
iobase_data_start (struct iobase *bp)
{
return bp->iob_buf + bp->iob_start;
}
static inline void
iobase_data_less (struct iobase *bp, size_t n)
{
bp->iob_start += n;
if (bp->iob_start == bp->iob_level)
{
bp->iob_start = 0;
bp->iob_level = 0;
}
}
static inline int
iobase_data_getc (struct iobase *bp)
{
char *p;
if (iobase_data_bytes (bp) == 0)
return -1;
p = iobase_data_start (bp);
iobase_data_less (bp, 1);
return *p;
}
static inline int
iobase_data_ungetc (struct iobase *bp, int c)
{
if (bp->iob_start > 0)
bp->iob_buf[--bp->iob_start] = c;
else if (bp->iob_level == 0)
bp->iob_buf[bp->iob_level++] = c;
else
return -1;
return 0;
}
/* Number of bytes available for writing in buffer */
static inline size_t
iobase_avail_bytes (struct iobase *bp)
{
return IOBUFSIZE - bp->iob_level;
}
/* Pointer to the first byte available for writing */
static inline char *
iobase_avail_start (struct iobase *bp)
{
return bp->iob_buf + bp->iob_level;
}
static inline void
iobase_avail_less (struct iobase *bp, size_t n)
{
bp->iob_level += n;
}
/* Fill the buffer */
static inline int
iobase_fill (struct iobase *bp)
{
int rc;
size_t n;
rc = bp->iob_drv->drv_read (bp, iobase_avail_start (bp),
iobase_avail_bytes (bp), &n);
if (rc == 0)
{
if (n == 0)
bp->iob_eof = 1;
else
iobase_avail_less (bp, n);
}
bp->iob_errno = rc;
return rc;
}
/* Flush the data available in buffer to external storage */
static inline int
iobase_flush (struct iobase *bp)
{
int rc;
size_t n;
rc = bp->iob_drv->drv_write (bp, iobase_data_start (bp),
iobase_data_bytes (bp), &n);
if (rc == 0)
iobase_data_less (bp, n);
bp->iob_errno = rc;
return rc;
}
static inline char const *
iobase_strerror (struct iobase *bp)
{
return bp->iob_drv->drv_strerror (bp, bp->iob_errno);
}
static inline int
iobase_eof (struct iobase *bp)
{
return bp->iob_eof && iobase_data_bytes (bp) == 0;
}
#if 0
/* Not actually used. Provided for completeness sake. */
static ssize_t
iobase_read (struct iobase *bp, char *buf, size_t size)
{
size_t len = 0;
while (size)
{
size_t n = iobase_data_bytes (bp);
if (n == 0)
{
if (bp->iob_eof)
break;
if (iobase_fill (bp))
break;
continue;
}
if (n > size)
n = size;
memcpy (buf, iobase_data_start (bp), n);
iobase_data_less (bp, n);
len += n;
buf += n;
size -= n;
}
if (len == 0 && bp->iob_errno)
return -1;
return len;
}
#endif
static ssize_t
iobase_readln (struct iobase *bp, char *buf, size_t size)
{
size_t len = 0;
int cr_seen = 0;
size--;
while (len < size)
{
int c = iobase_data_getc (bp);
if (c < 0)
{
if (bp->iob_eof)
break;
if (iobase_fill (bp))
break;
continue;
}
if (c == '\r')
{
cr_seen = 1;
continue;
}
if (c != '\n' && cr_seen)
{
buf[len++] = '\r';
if (len == size)
{
if (iobase_data_ungetc (bp, c))
abort ();
break;
}
}
cr_seen = 0;
buf[len++] = c;
if (c == '\n')
break;
}
if (len == 0 && bp->iob_errno)
return -1;
buf[len] = 0;
return len;
}
static ssize_t
iobase_write (struct iobase *bp, char *buf, size_t size)
{
size_t len = 0;
while (size)
{
size_t n = iobase_avail_bytes (bp);
if (n == 0)
{
if (iobase_flush (bp))
break;
continue;
}
if (n > size)
n = size;
memcpy (iobase_avail_start (bp), buf + len, n);
iobase_avail_less (bp, n);
len += n;
size -= n;
}
if (len == 0 && bp->iob_errno)
return -1;
return len;
}
static ssize_t
iobase_writeln (struct iobase *bp, char *buf, size_t size)
{
size_t len = 0;
while (size)
{
char *p = memchr (buf, '\n', size);
size_t n = p ? p - buf + 1 : size;
ssize_t rc = iobase_write (bp, buf, n);
if (rc <= 0)
break;
if (p && iobase_flush (bp))
break;
buf = p;
len += rc;
size -= rc;
}
if (len == 0 && bp->iob_errno)
return -1;
return len;
}
static void
iobase_close (struct iobase *bp)
{
bp->iob_drv->drv_close (bp);
free (bp);
}
/* File-descriptor I/O streams */
struct iofile
{
struct iobase base;
int fd;
};
static int
iofile_read (void *sd, char *data, size_t size, size_t *nbytes)
{
struct iofile *bp = sd;
ssize_t n = read (bp->fd, data, size);
if (n == -1)
return errno;
if (nbytes)
*nbytes = n;
return 0;
}
static int
iofile_write (void *sd, char *data, size_t size, size_t *nbytes)
{
struct iofile *bp = sd;
ssize_t n = write (bp->fd, data, size);
if (n == -1)
return errno;
if (nbytes)
*nbytes = n;
return 0;
}
static const char *
iofile_strerror (void *sd, int rc)
{
return strerror (rc);
}
static void
iofile_close (void *sd)
{
struct iofile *bp = sd;
close (bp->fd);
}
static struct iodrv iofile_drv = {
iofile_read,
iofile_write,
iofile_close,
iofile_strerror
};
struct iobase *
iofile_create (int fd)
{
struct iofile *bp = (struct iofile *) iobase_create (&iofile_drv, sizeof (*bp));
bp->fd = fd;
return (struct iobase*) bp;
}
enum
{
IO2_RD,
IO2_WR
};
static void disable_starttls (void);
#ifdef WITH_TLS
# include <gnutls/gnutls.h>
/* TLS support */
char *tls_cert; /* TLS certificate */
char *tls_key; /* TLS key */
char *tls_cafile;
static inline int
set_tls_opt (int c)
{
switch (c)
{
case 'c':
tls_cert = optarg;
break;
case 'k':
tls_key = optarg;
break;
case 'f':
tls_cafile = optarg;
break;
default:
return 1;
}
return 0;
}
static inline int
enable_tls (void)
{
return tls_cert != NULL && tls_key != NULL;
}
/* TLS streams */
struct iotls
{
struct iobase base;
gnutls_session_t sess;
int fd[2];
};
static int
iotls_read (void *sd, char *data, size_t size, size_t *nbytes)
{
struct iotls *iob = sd;
int rc;
do
rc = gnutls_record_recv (iob->sess, data, size);
while (rc == GNUTLS_E_AGAIN || rc == GNUTLS_E_INTERRUPTED);
if (rc >= 0)
{
if (nbytes)
*nbytes = rc;
return 0;
}
return rc;
}
static int
iotls_write (void *sd, char *data, size_t size, size_t *nbytes)
{
struct iotls *iob = sd;
int rc;
do
rc = gnutls_record_send (iob->sess, data, size);
while (rc == GNUTLS_E_INTERRUPTED || rc == GNUTLS_E_AGAIN);
if (rc >= 0)
{
if (nbytes)
*nbytes = rc;
return 0;
}
return rc;
}
static const char *
iotls_strerror (void *sd, int rc)
{
// struct iotls *iob = sd;
return gnutls_strerror (rc);
}
static void
iotls_close (void *sd)
{
struct iotls *iob = sd;
gnutls_bye (iob->sess, GNUTLS_SHUT_RDWR);
gnutls_deinit (iob->sess);
}
static struct iodrv iotls_drv = {
iotls_read,
iotls_write,
iotls_close,
iotls_strerror
};
static gnutls_dh_params_t dh_params;
static gnutls_certificate_server_credentials x509_cred;
static void
_tls_cleanup_x509 (void)
{
if (x509_cred)
gnutls_certificate_free_credentials (x509_cred);
}
#define DH_BITS 512
static void
generate_dh_params (void)
{
gnutls_dh_params_init (&dh_params);
gnutls_dh_params_generate2 (dh_params, DH_BITS);
}
static int
tls_init (void)
{
int rc;
if (!enable_tls())
return -1;
gnutls_global_init ();
atexit (gnutls_global_deinit);
gnutls_certificate_allocate_credentials (&x509_cred);
atexit (_tls_cleanup_x509);
if (tls_cafile)
{
rc = gnutls_certificate_set_x509_trust_file (x509_cred,
tls_cafile,
GNUTLS_X509_FMT_PEM);
if (rc < 0)
{
terror ("%s: %s", tls_cafile, gnutls_strerror (rc));
return -1;
}
}
rc = gnutls_certificate_set_x509_key_file (x509_cred,
tls_cert, tls_key,
GNUTLS_X509_FMT_PEM);
if (rc < 0)
{
terror ("error reading certificate files: %s", gnutls_strerror (rc));
return -1;
}
generate_dh_params ();
gnutls_certificate_set_dh_params (x509_cred, dh_params);
return 0;
}
static ssize_t
_tls_fd_pull (gnutls_transport_ptr_t fd, void *buf, size_t size)
{
struct iotls *bp = fd;
int rc;
do
{
rc = read (bp->fd[IO2_RD], buf, size);
}
while (rc == -1 && errno == EAGAIN);
return rc;
}
static ssize_t
_tls_fd_push (gnutls_transport_ptr_t fd, const void *buf, size_t size)
{
struct iotls *bp = fd;
int rc;
do
{
rc = write (bp->fd[IO2_WR], buf, size);
}
while (rc == -1 && errno == EAGAIN);
return rc;
}
struct iobase *
iotls_create (int in, int out)
{
struct iotls *bp = (struct iotls *) iobase_create (&iotls_drv, sizeof (*bp));
int rc;
bp->fd[IO2_RD] = in;
bp->fd[IO2_WR] = out;
gnutls_init (&bp->sess, GNUTLS_SERVER);
gnutls_set_default_priority (bp->sess);
gnutls_credentials_set (bp->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
gnutls_certificate_server_set_request (bp->sess, GNUTLS_CERT_REQUEST);
gnutls_dh_set_prime_bits (bp->sess, DH_BITS);
gnutls_transport_set_pull_function (bp->sess, _tls_fd_pull);
gnutls_transport_set_push_function (bp->sess, _tls_fd_push);
gnutls_transport_set_ptr2 (bp->sess,
(gnutls_transport_ptr_t) bp,
(gnutls_transport_ptr_t) bp);
rc = gnutls_handshake (bp->sess);
if (rc < 0)
{
gnutls_deinit (bp->sess);
gnutls_perror (rc);
free (bp);
return NULL;
}
return (struct iobase *)bp;
}
#else
static inline int set_tls_opt (int c) {
terror ("option -%c not supported: program compiled without support for TLS",
c);
return 1;
}
static inline int enable_tls(void) { return 0; }
static inline int tls_init (void) { return -1; }
static inline struct iobase *iotls_create (int in, int out) { return NULL; }
#endif
/* Two-way I/O */
struct io2
{
struct iobase base;
struct iobase *iob[2];
};
static int
io2_read (void *sd, char *data, size_t size, size_t *nbytes)
{
struct io2 *iob = sd;
ssize_t n = iobase_readln (iob->iob[IO2_RD], data, size);
if (n < 0)
return -(1 + IO2_RD);
*nbytes = n;
return 0;
}
static int
io2_write (void *sd, char *data, size_t size, size_t *nbytes)
{
struct io2 *iob = sd;
ssize_t n = iobase_writeln (iob->iob[IO2_WR], data, size);
if (n < 0)
return -(1 + IO2_WR);
*nbytes = n;
return 0;
}
static char const *
io2_strerror (void *sd, int rc)
{
struct io2 *iob = sd;
int n = -rc - 1;
switch (n)
{
case IO2_RD:
case IO2_WR:
return iobase_strerror (iob->iob[n]);
default:
return "undefined error";
}
}
static void
io2_close (void *sd)
{
struct io2 *iob = sd;
iobase_close (iob->iob[IO2_RD]);
iobase_close (iob->iob[IO2_WR]);
}
static struct iodrv io2_drv = {
io2_read,
io2_write,
io2_close,
io2_strerror
};
struct iobase *
io2_create (struct iobase *in, struct iobase *out)
{
struct io2 *bp = (struct io2 *) iobase_create (&io2_drv, sizeof (*bp));
bp->iob[IO2_RD] = in;
bp->iob[IO2_WR] = out;
return (struct iobase*) bp;
}
/* SMTP implementation */
enum smtp_state
{
STATE_ERR, // Reserved
STATE_INIT,
STATE_EHLO,
STATE_MAIL,
STATE_RCPT,
STATE_DATA,
STATE_QUIT,
MAX_STATE
};
#define MAX_RCPT 32
struct smtp
{
enum smtp_state state;
struct iobase *iob;
char buf[IOBUFSIZE];
char *arg;
int capa_mask;
char *helo;
char *sender;
char *rcpt[MAX_RCPT];
int nrcpt;
char *data_buf;
size_t data_len;
size_t data_size;
};
static void
smtp_io_send (struct iobase *iob, int code, char *fmt, ...)
{
va_list ap;
char buf[IOBUFSIZE];
int n;
snprintf (buf, sizeof buf, "%3d ", code);
va_start (ap, fmt);
n = vsnprintf (buf + 4, sizeof buf - 6, fmt, ap);
va_end (ap);
n += 4;
buf[n++] = '\r';
buf[n++] = '\n';
if (iobase_writeln (iob, buf, n) < 0)
{
terror ("iobase_writeln: %s", iobase_strerror (iob));
exit (EX_FAILURE);
}
}
static void
smtp_io_mlsend (struct iobase *iob, int code, char const **av)
{
char buf[IOBUFSIZE];
size_t n;
int i;
snprintf (buf, sizeof buf, "%3d", code);
for (i = 0; av[i]; i++)
{
n = snprintf (buf, sizeof(buf), "%3d%c%s\r\n",
code, av[i+1] ? '-' : ' ', av[i]);
if (iobase_writeln (iob, buf, n) < 0)
{
terror ("iobase_writeln: %s", iobase_strerror (iob));
exit (EX_FAILURE);
}
}
}
static void
smtp_reset (struct smtp *smtp, int state)
{
switch (state)
{
case STATE_INIT:
free (smtp->helo);
smtp->helo = NULL;
/* FALL THROUGH */
case STATE_MAIL:
free (smtp->sender);
smtp->sender = NULL;
/* FALL THROUGH */
case STATE_RCPT:
{
int i;
for (i = 0; i < smtp->nrcpt; i++)
free (smtp->rcpt[i]);
smtp->nrcpt = 0;
}
/* FALL THROUGH */
case STATE_DATA:
free (smtp->data_buf);
smtp->data_buf = NULL;
smtp->data_len = 0;
smtp->data_size = 0;
}
}
void
smtp_end (struct smtp *smtp)
{
smtp_io_send (smtp->iob, 221, "Bye");
iobase_close (smtp->iob);
smtp_reset (smtp, STATE_INIT);
smtp->iob = NULL;
}
enum smtp_keyword
{
KW_HELP,
KW_RSET,
KW_EHLO,
KW_HELO,
KW_MAIL,
KW_RCPT,
KW_DATA,
KW_STARTTLS,
KW_QUIT,
MAX_KW
};
static char *smtp_keyword_trans[MAX_KW] = {
[KW_HELP] = "HELP",
[KW_RSET] = "RSET",
[KW_EHLO] = "EHLO",
[KW_HELO] = "HELO",
[KW_MAIL] = "MAIL",
[KW_RCPT] = "RCPT",
[KW_DATA] = "DATA",
[KW_STARTTLS] = "STARTTLS",
[KW_QUIT] = "QUIT"
};
static int
smtp_keyword_find (char const *kw)
{
int i;
for (i = 0; i < MAX_KW; i++)
if (strcasecmp (kw, smtp_keyword_trans[i]) == 0)
return i;
return -1;
}
static int
smtp_help (struct smtp *smtp)
{
smtp_io_send (smtp->iob, 214, "http://www.ietf.org/rfc/rfc2821.txt");
return 0;
}
static int
smtp_rset (struct smtp *smtp)
{
if (smtp->arg)
{
smtp_io_send (smtp->iob, 501, "rset does not take arguments");
return -1;
}
smtp_io_send (smtp->iob, 250, "Reset state");
smtp_reset (smtp, STATE_INIT);
return 0;
}
enum
{
CAPA_PIPELINING,
CAPA_STARTTLS,
CAPA_HELP,
MAX_CAPA
};
static char const *capa_str[] = {
"PIPELINING",
"STARTTLS",
"HELP"
};
#define CAPA_MASK(n) (1<<(n))
static int
smtp_ehlo (struct smtp *smtp)
{
char const *capa[MAX_CAPA+2];
int i, j;
if (!smtp->arg)
{
smtp_io_send (smtp->iob, 501, "ehlo requires domain address");
return -1;
}
capa[0] = "localhost Mock MTA pleased to meet you";
for (i = 0, j = 1; i < MAX_CAPA; i++)
if (!(smtp->capa_mask & CAPA_MASK (i)))
capa[j++] = capa_str[i];
capa[j] = NULL;
smtp_io_mlsend (smtp->iob, 250, capa);
smtp_reset (smtp, STATE_INIT);
if ((smtp->helo = strdup (smtp->arg)) == NULL)
nomemory ();
return 0;
}
static int
smtp_starttls (struct smtp *smtp)
{
struct io2 *orig_iob = (struct io2 *) smtp->iob;
struct iofile *inb = (struct iofile *)orig_iob->iob[IO2_RD];
struct iofile *outb = (struct iofile *)orig_iob->iob[IO2_WR];
struct iobase *iob;
if (smtp->arg)
{
smtp_io_send (smtp->iob, 501, "Syntax error (no parameters allowed)");
return -1;
}
smtp_io_send (smtp->iob, 220, "Ready to start TLS");
iob = iotls_create (inb->fd, outb->fd);
if (iob)
{
free (inb);
free (outb);
free (orig_iob);
smtp->iob = iob;
disable_starttls ();
smtp->capa_mask |= CAPA_MASK (CAPA_STARTTLS);
}
else
{
free (iob);
exit (EX_FAILURE);
}
return 0;
}
static int
smtp_quit (struct smtp *smtp)
{
return 0;
}
static int
smtp_helo (struct smtp *smtp)
{
if (!smtp->arg)
{
smtp_io_send (smtp->iob, 501, "helo requires domain address");
return -1;
}
smtp_io_send (smtp->iob, 250, "localhost Mock MTA pleased to meet you");
smtp_reset (smtp, STATE_INIT);
if ((smtp->helo = strdup (smtp->arg)) == NULL)
nomemory ();
return 0;
}
static int
smtp_mail (struct smtp *smtp)
{
static char from_str[] = "FROM:";
static size_t from_len = sizeof(from_str) - 1;
char *p;
if (!smtp->arg)
{
smtp_io_send (smtp->iob, 501, "mail requires email address");
return -1;
}
if (strncasecmp (smtp->arg, from_str, from_len))
{
smtp_io_send (smtp->iob, 501, "syntax error");
return -1;
}
p = smtp->arg + from_len;
while (*p && (*p == ' ' || *p == '\t'))
p++;
if (!*p)
{
smtp_io_send (smtp->iob, 501, "mail requires email address");
return -1;
}
smtp_reset (smtp, STATE_MAIL);
if ((smtp->sender = strdup (p)) == NULL)
nomemory ();
smtp_io_send (smtp->iob, 250, "Sender ok");
return 0;
}
static int
smtp_rcpt (struct smtp *smtp)
{
static char to_str[] = "TO:";
static size_t to_len = sizeof(to_str) - 1;
char *p;
if (!smtp->arg)
{
smtp_io_send (smtp->iob, 501, "rcpt requires email address");
return -1;
}
if (strncasecmp (smtp->arg, to_str, to_len))
{
smtp_io_send (smtp->iob, 501, "syntax error");
return -1;
}
p = smtp->arg + to_len;
while (*p && (*p == ' ' || *p == '\t'))
p++;
if (!*p)
{
smtp_io_send (smtp->iob, 501, "to requires email address");
return -1;
}
if (smtp->nrcpt == MAX_RCPT)
{
smtp_io_send (smtp->iob, 501, "too many recipients");
return -1;
}
if ((smtp->rcpt[smtp->nrcpt] = strdup (p)) == NULL)
nomemory ();
smtp->nrcpt++;
smtp_io_send (smtp->iob, 250, "Recipient ok");
return 0;
}
static void
smtp_data_save (struct smtp *smtp)
{
size_t len = strlen (smtp->buf);
while (smtp->data_len + len > smtp->data_size)
{
char *p;
size_t n = smtp->data_size;
if (!smtp->data_buf)
{
n = smtp->data_len + len;
}
else
{
if ((size_t)-1 / 3 * 2 <= n)
nomemory ();
n += (n + 1) / 2;
}
p = realloc (smtp->data_buf, n);
if (!p)
nomemory ();
smtp->data_buf = p;
smtp->data_size = n;
}
memcpy (smtp->data_buf + smtp->data_len, smtp->buf, len);
smtp->data_len += len;
}
static int
smtp_data (struct smtp *smtp)
{
ssize_t n;
int i;
smtp_io_send (smtp->iob, 354,
"Enter mail, end with \".\" on a line by itself");
fprintf (logfile, "MSGID: %04d\n", msgid);
fprintf (logfile, "DOMAIN: %s\n", smtp->helo);
fprintf (logfile, "SENDER: %s\n", smtp->sender);
fprintf (logfile, "NRCPT: %d\n", smtp->nrcpt);
for (i = 0; i < smtp->nrcpt; i++)
fprintf (logfile, "RCPT[%d]: %s\n", i, smtp->rcpt[i]);
while (1)
{
char *p;
n = iobase_readln (smtp->iob, smtp->buf, sizeof (smtp->buf));
if (n <= 0)
{
smtp->state = STATE_QUIT;
return -1;
}
smtp_data_save (smtp);
if (smtp->buf[n-1] == '\n')
smtp->buf[--n] = 0;
else
{
//FIXME
terror ("line too long");
exit (EX_FAILURE);
}
p = smtp->buf;
if (*p == '.')
{
if (p[1] == 0)
break;
if (p[1] == '.')
p++;
}
}
fprintf (logfile, "LENGTH: %lu\n", (unsigned long)smtp->data_len);
fwrite (smtp->data_buf, smtp->data_len, 1, logfile);
fputc ('\n', logfile);
fflush (logfile);
smtp_io_send (smtp->iob, 250, "%04d Message accepted for delivery", msgid);
msgid++;
return 0;
}
struct smtp_transition
{
int new_state;
int (*handler) (struct smtp *);
};
static struct smtp_transition smtp_transition_table[MAX_STATE][MAX_KW] = {
[STATE_INIT] = {
[KW_HELP] = { STATE_INIT, smtp_help },
[KW_RSET] = { STATE_INIT, smtp_rset },
[KW_HELO] = { STATE_EHLO, smtp_helo },
[KW_EHLO] = { STATE_EHLO, smtp_ehlo },
[KW_QUIT] = { STATE_QUIT, smtp_quit }
},
[STATE_EHLO] = {
[KW_HELP] = { STATE_EHLO, smtp_help },
[KW_RSET] = { STATE_INIT, smtp_rset },
[KW_HELO] = { STATE_EHLO, smtp_helo },
[KW_EHLO] = { STATE_EHLO, smtp_ehlo },
[KW_MAIL] = { STATE_MAIL, smtp_mail },
[KW_STARTTLS] = { STATE_EHLO, smtp_starttls },
[KW_QUIT] = { STATE_QUIT, smtp_quit }
},
[STATE_MAIL] = {
[KW_HELP] = { STATE_MAIL, smtp_help },
[KW_RSET] = { STATE_INIT, smtp_rset },
[KW_RCPT] = { STATE_RCPT, smtp_rcpt },
[KW_HELO] = { STATE_EHLO, smtp_helo },
[KW_EHLO] = { STATE_EHLO, smtp_ehlo },
[KW_QUIT] = { STATE_QUIT, smtp_quit }
},
[STATE_RCPT] = {
[KW_HELP] = { STATE_RCPT, smtp_help },
[KW_RSET] = { STATE_INIT, smtp_rset },
[KW_RCPT] = { STATE_RCPT, smtp_rcpt },
[KW_HELO] = { STATE_EHLO, smtp_helo },
[KW_EHLO] = { STATE_EHLO, smtp_ehlo },
[KW_DATA] = { STATE_EHLO, smtp_data },
[KW_QUIT] = { STATE_QUIT, smtp_quit }
},
};
static void
disable_starttls (void)
{
#ifdef WITH_TLS
tls_cert = tls_key = tls_cafile = NULL;
#endif
smtp_transition_table[STATE_EHLO][KW_STARTTLS].new_state = STATE_ERR;
}
static void
do_smtp (int fd)
{
struct iobase *iob = io2_create (iofile_create (fd), iofile_create (fd));
struct smtp smtp;
struct smtp_transition *trans;
smtp.state = STATE_INIT;
smtp.iob = iob;
smtp.capa_mask = 0;
if (!enable_tls ())
smtp.capa_mask |= CAPA_MASK (CAPA_STARTTLS);
smtp.helo = NULL;
smtp.sender = NULL;
smtp.nrcpt = 0;
smtp.data_buf = NULL;
smtp.data_len = 0;
smtp.data_size = 0;
smtp_io_send (smtp.iob, 220, "Ready");
while (smtp.state != STATE_QUIT)
{
size_t i;
int kw;
int new_state;
ssize_t n = iobase_readln (smtp.iob, smtp.buf, sizeof (smtp.buf));
if (n <= 0)
break;
smtp.buf[--n] = 0;
i = strcspn (smtp.buf, " \t");
if (smtp.buf[i])
{
smtp.buf[i++] = 0;
while (i < n && (smtp.buf[i] == ' ' || smtp.buf[i] == '\t'))
i++;
if (smtp.buf[i])
smtp.arg = &smtp.buf[i];
else
smtp.arg = NULL;
}
else
smtp.arg = NULL;
kw = smtp_keyword_find (smtp.buf);
if (kw == -1)
{
smtp_io_send(smtp.iob, 500, "Command unrecognized");
continue;
}
trans = &smtp_transition_table[smtp.state][kw];
new_state = trans->new_state;
if (new_state == STATE_ERR)
{
smtp_io_send(smtp.iob, 500, "Command not valid");
continue;
}
if (trans->handler (&smtp))
continue;
smtp.state = new_state;
}
smtp_end (&smtp);
}
static int
mta_open (int port)
{
int on = 1;
struct sockaddr_in address;
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
terror ("socket: %m");
exit (EX_FAILURE);
}
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on));
memset (&address, 0, sizeof (address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
if (port)
address.sin_port = htons (port);
else
address.sin_port = 0;
if (bind (fd, (struct sockaddr *) &address, sizeof (address)) < 0)
{
close (fd);
terror ("bind: %m");
exit (EX_FAILURE);
}
listen (fd, 5);
if (!port)
{
socklen_t len = sizeof (address);
int rc = getsockname (fd, (struct sockaddr *) &address, &len);
if (rc)
{
close (fd);
terror ("getsockname: %m");
exit (EX_FAILURE);
}
port = ntohs (address.sin_port);
printf ("%d\n", port);
}
return fd;
}
void
mta_run (int fd)
{
while (1)
{
int sfd;
struct sockaddr_in remote_addr;
socklen_t len = sizeof (remote_addr);
if ((sfd = accept (fd, (struct sockaddr *) &remote_addr, &len)) < 0)
{
terror ("accept: %m");
exit (EX_FAILURE);
}
do_smtp (sfd);
close (sfd);
}
}
int
main (int argc, char **argv)
{
int c;
int fd;
int append_opt = 0;
progname = argv[0];
while ((c = getopt (argc, argv, "adc:f:k:p:t:")) != EOF)
{
switch (c)
{
case 'a':
append_opt = 1;
break;
case 'd':
daemon_opt = 1;
break;
case 'p':
port = atoi (optarg);
break;
case 't':
daemon_timeout = atoi (optarg);
if (daemon_timeout == 0)
{
terror ("wrong timeout value");
exit (EX_USAGE);
}
break;
default:
if (set_tls_opt (c))
exit (EX_USAGE);
}
}
argc -= optind;
argv += optind;
if (argc == 0)
{
if (daemon_opt)
{
terror ("DUMPFILE is required in daemon mode");
exit (EX_USAGE);
}
logfile = stdout;
}
else if (argc == 1)
{
logfile = fopen (argv[0], append_opt ? "a" : "w");
if (!logfile)
{
terror ("can't open %s for writing: %m", argv[0]);
exit (EX_FAILURE);
}
}
else
{
terror ("too many arguments");
exit (EX_USAGE);
}
if (tls_init ())
{
disable_starttls ();
}
fd = mta_open (port);
if (daemon_opt)
{
pid_t pid = fork ();
if (pid == -1)
{
terror ("fork: %m");
exit (EX_FAILURE);
}
if (pid)
{
/* master */
printf ("%lu\n", (unsigned long) pid);
return 0;
}
else
{
/* child */
/* Close unneeded descriptors */
int i;
for (i = 0; i < sysconf (_SC_OPEN_MAX); i++)
{
if (i != fileno (logfile) && i != fd)
close (i);
}
/* Provide replacements for the three standard streams */
if (open ("/dev/null", O_RDONLY) != 0 ||
dup2 (fileno (logfile), 1) == -1 ||
dup2 (fileno (logfile), 2) == -1)
abort ();
/* Impose the runtime timeout */
alarm (daemon_timeout);
}
}
mta_run (fd);
exit (EX_OK);
}
|