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 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
|
/*
* bulk mailer.
* sorts a recipient list by reversed domain and splits it up into
* several envelopes so sendmail can deliver it in parallel.
*
* Copyright 1994-2000 by Keith Moore <moore@cs.utk.edu>.
* All rights reserved.
*
* License to copy and use this program is granted according to the terms
* of the current version of the GNU General Public License. However,
* there is one exception: this program may not be used to send unsolicited
* commercial messages.
*
* There's no warranty on this program.
* So don't blame me if something goes wrong.
*/
/*
* NOTES:
* - Recipients go one-recipient-per-line in the recipient list file.
* bulk_mailer knows how to ignore comments and prefix phrases.
*
* - Mail to programs doesn't work. This is a feature.
*
* - If message does not contain a To, Cc, or Bcc header, bulk_mailer
* adds one. Otherwise sendmail would add one.
* - adds a Received header,
* - unconditionally nukes content-length, errors-to, return-receipt-to
* - nukes return-path header (shouldn't be there anyway)
* - adds message-id if there wasn't one there already
* - nukes Precedence (but sysadmin can add it back)
* - sysadmin can optionally add a reply-to
*/
/*
* TODO (maybe):
*
* - support passing recipients to sendmail on the command-line
* (rather than via SMTP; sendmail may refuse SMTP connections
* if the load average is too high).
* - support writing queue files directly into sendmail's mailq
* directory for efficiency.
* - sort addresses by primary MX rather than by domain.
* - allow splitting up the load into several different mail queues;
* arrange things so that mail for domain 'x.y' always goes in
* the same queue. That way, the site can control the number
* of sendmail "worker bees".
* - allow options to be specified in a configuration file, rather
* than having to put them all on the command-line.
* - require approval for any messages not from a particular set of
* users
* - accept majordomo-style 'approved: password' header.
* - add some checks to prevent nondelivery reports from going to
* the list.
*/
/*
* change log:
#
* April 10, 1996
* - bounce any message for which the Sender field contains
* mailer-daemon@
*
* September 28, 1995 by Keith Moore
* - allow setting of max_domains from the command-line
* - allow setting default domain from the command-line
* - prefrobnicate addresses to remove comments, white space,
* 'phrase' before address in angle-brackets.
* - add support for various kinds of header munging
*
* April 14, 1995 by Keith Moore, moore@cs.utk.edu
* - if input doesn't contain a To/Cc/Bcc header, add a dummy To header.
* - new functions malloc_or_else and realloc_or_else simplify flow.
*
* May 27, 1994 by Paul DuBois, dubois@primate.wisc.edu
* - Don't uppercase anything in the original, just the reversed domain
* that's used for sorting. This leaves the original addresses alone.
* - Fixed bug in argument processing. (Specifying -v resulted in unknown
* flag message due to missing "else")
* - If DEFAULT_DOMAIN is defined, it's added to local address so they
* have a @thing part. Otherwise addresses with no @thing part are rejected
* as originally.
* - Check all malloc() return values for NULL.
* - Add exit(EX_OK) at end of main() so explicit status gets returned.
* - Added "void" to definitions of a few functions that were missing it.
*/
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sysexits.h>
#include <errno.h>
#include <time.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "patchlevel.h"
#ifndef PIPECOMMAND
#define PIPECOMMAND "/usr/lib/sendmail -bs %s"
#endif
#ifndef HAVE_STRERROR
extern int sys_nerr;
/* extern char *sys_errlist[]; */
#define strerror(e) ((e) < 0 || (e) >= sys_nerr ? "(unknown)" : sys_errlist[e])
#endif
#ifndef errno
extern int errno;
#endif
#ifndef HAVE_STRDUP
char *strdup();
#endif
#ifndef STDC_HEADERS
char *strchr();
char *strrchr();
#endif
char *malloc ();
char *realloc ();
char *mktemp ();
/*
* (default) max different domains per envelope.
* if the worst-case delay is 30 minutes per delivery attempt,
* then the worst-case time to try to deliver to everyone will
* be 30 minutes * MAX_DOMAINS. Usually it will be much better.
*/
#define MAX_DOMAINS 20
/*
* (default) max number of recipients per envelope
*/
#define MAX_RCPTS 100
int author_is_subscriber = 0; /* true iff author is a list subscriber */
char *comment = NULL; /* generate comment hdr with this value */
int debug_flag = 0; /* don't mail; spit SMTP to stdout */
char *envelope_from; /* outgoing SMTP from address */
char *header_from; /* From: address from message header */
int force_reply_to = 0; /* override sender's reply-to hdr */
char *list_owner = NULL; /* address of owner of mailing list */
char *local_domain = NULL; /* local domain name */
int max_domains = MAX_DOMAINS; /* max # of domains / envelope */
int max_rcpts = MAX_RCPTS;
int max_size = 0x7fffffff; /* max message size before bouncing */
char *precedence = NULL; /* generate precedence hdr with this value */
int private_flag = 0; /* true iff author must be a list subscriber */
char *reply_to = NULL; /* generate reply-to hdr with this value */
char *sendmail_flags = ""; /* call sendmail with these flags */
int subscribe_flag = 0; /* check for "[un]subscribe" in body */
int verbose_flag = 0; /* be verbose */
/*
* List-* headers. See RFC 2369 before setting these.
*/
char *list_archive_hdr = NULL;
char *list_help_hdr = NULL;
char *list_id_hdr = NULL;
char *list_owner_hdr = NULL;
char *list_post_hdr = NULL;
char *list_subscribe_hdr = NULL;
char *list_unsubscribe_hdr = NULL;
int del_list_hdrs = 0;
/*
* structure used to hold an address while sorting
*/
struct address {
char *address; /* entire address */
char *niamod; /* reversed domain (for sorting) */
};
static struct address *address_list = NULL;
static int num_addrs = 0;
static int num_addr_slots = 0;
/*
* discard an address (maybe print an error message)
*/
static void
discard (addr, msg)
char *addr;
char *msg;
{
if (verbose_flag)
fprintf (stderr, "%s discarded (%s)\n", addr, msg);
}
/*
* upper case a string in place
*/
static char *
strupper (s)
char *s;
{
char *result = s;
while (*s) {
if (*s >= 'a' && *s <= 'z')
*s += 'A' - 'a';
++s;
}
return result;
}
/*
* attempt to malloc memory. if it fails, exit with EX_TEMPFAIL
*
* XXX instead of exiting, should clean up.
*/
static char *
malloc_or_else (size)
int size;
{
char *result;
if ((result = malloc (size)) == NULL) {
fprintf (stderr, "out of memory\n");
exit (EX_TEMPFAIL);
}
return result;
}
/*
* attempt to realloc memory. if it fails, exit with EX_TEMPFAIL
*
* XXX instead of exiting, should clean up.
*/
static char *
realloc_or_else (oldbuf, size)
char *oldbuf;
int size;
{
char *result;
if ((result = realloc (oldbuf, size)) == NULL) {
fprintf (stderr, "out of memory\n");
exit (EX_TEMPFAIL);
}
return result;
}
/*
* reverse the characters in a domain and put the result in malloc'ed memory
*/
static char *
niamod (domain)
char *domain;
{
int length;
char *result;
int i;
length = strlen (domain);
result = malloc_or_else (length + 1);
for (i = 0; i < length; ++i)
result[i] = domain[length - 1 - i];
result[length] = '\0';
return strupper (result);
}
#ifndef HAVE_STRDUP
/*
* copy a string into malloc'ed memory
* (most systems have this built-in, but not ultrix 4.3.)
*/
static char *
strdup (str)
char *str;
{
int length;
char *result;
length = strlen (str) + 1;
result = malloc_or_else (length);
strcpy (result, str);
return result;
}
#endif
/*
* copy the first 'len' characters of a string into malloc'ed
* memory, appending a NUL to the copy.
*/
static char *
strndup (str, len)
char *str;
int len;
{
char *result = malloc_or_else (len + 1);
strncpy (result, str, len);
result[len] = '\0';
return result;
}
/*
* sort by case-folded reversed domain
*/
static int
compare (a1, a2)
struct address *a1, *a2;
{
return strcmp (a1->niamod, a2->niamod);
}
static void
sort_addrs ()
{
qsort (address_list, num_addrs, sizeof (struct address), compare);
}
/*
* save an address in the sort buffer
*
* buffer is automagically grown 1000 addresses at a time.
*/
static void
save (addr)
char *addr;
{
char *at;
char *ptr;
char *domain;
char tempbuf[1024];
char c;
/*
* make sure there's room in the buffer.
*/
if (num_addrs >= num_addr_slots) {
struct address *new;
num_addr_slots += 1000;
if (address_list == NULL)
address_list = (struct address *)
malloc_or_else (num_addr_slots * sizeof (struct address));
else
address_list = (struct address *)
realloc_or_else (address_list,
num_addr_slots * sizeof (struct address));
}
address_list[num_addrs].address = strdup (addr);
/*
* find the next-hop domain for sorting. this will always be the
* domain name following the leftmost '@' sign. It is terminated
* by one of { ':' ',' '\0'}.
*/
at = strchr (addr, '@');
if (at == NULL) {
discard (addr, "missing '@'");
return;
}
for (ptr = at; *ptr != '\0' && *ptr != ':' && *ptr != ','; ++ptr);
*ptr = '\0';
strcpy (tempbuf, at + 1);
/*
* uppercase the domain to make sorting easier
*/
strupper (tempbuf);
address_list[num_addrs].niamod = niamod (tempbuf);
++num_addrs;
}
/*
* take an address and remove any extraneous bits. e.g:
*
* Keith Moore <moore@cs.utk.edu> => moore@cs.utk.edu
* moore@cs.utk.edu (Keith Moore) => moore@cs.utk.edu
*/
static char *
prefrobnicate (buf)
char *buf;
{
static char result[1024];
char *s, *d;
int state = 0;
int level = 0;
s = buf;
d = result;
while (*s) {
if (d >= result+sizeof(result)-1) {
fprintf (stderr, "prefrobnicate: line too long \"%s\"\n", buf);
return "";
}
switch (state) {
case 0: /* outside */
if (*s == '<') {
d = result;
state = 3;
}
else if (*s == '(') {
level++;
state = 1;
}
else if (*s == '"') {
state = 2;
*d++ = *s;
}
else if (*s == '\\') {
if (s[1])
*d++ = *s++;
*d++ = *s;
}
else if (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
;
else
*d++ = *s;
break;
case 1: /* inside comment */
case 4: /* comment inside <> */
/* don't copy comments to output */
if (*s == '(')
++level;
else if (*s == ')') {
if (--level <= 0)
state = (state == 1) ? 0 : 3;
}
else if (*s == '\\') {
if (s[1])
++s;
}
break;
case 2: /* inside quotes */
case 5: /* quotes inside <> */
if (*s == '"') {
state = (state == 2) ? 0 : 3;
*d++ = *s;
}
else if (*s == '\\') {
if (s[1])
*d++ = *s++;
*d++ = *s;
}
else
*d++ = *s;
break;
case 3: /* inside <> */
if (*s == '>') {
*d = '\0';
return result;
}
else if (*s == '(')
state = 4;
else if (*s == '"') {
*d++ = *s;
state = 5;
}
else if (*s == '\\') {
if (s[1])
*d++ = *s++;
*d++ = *s;
}
else if (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
;
else
*d++ = *s;
break;
}
++s;
}
*d = '\0';
return result;
}
/*
* do some rough canonicalization of the address, then save for later
*/
static void
process_addr (buf)
char *buf;
{
int length = strlen (buf);
char *at;
char *ptr;
if (length == 0)
return;
/* delete newline at end of buf */
if (buf[length-1] == '\n')
buf[--length] = '\0';
/*
* strip out phrases, white space, comments
*/
if ((ptr = prefrobnicate (buf)) == NULL)
return;
strcpy (buf, ptr);
/*
* find last '@' in the address. if there isn't one, it's
* bogus.
*/
if ((at = strrchr (buf, '@')) == NULL) {
if (local_domain) {
at = buf + strlen (buf);
*at++ = '@';
(void) strcpy (at--, local_domain);
}
else {
discard (buf, "no '@' in address");
return;
}
}
if (at == buf) {
discard (buf, "no local-part");
return;
}
if (header_from && strcasecmp (header_from, buf) == 0)
author_is_subscriber = 1;
save (buf);
}
static void
process (fp)
FILE *fp;
{
char linebuf[1024];
while (fgets (linebuf, sizeof (linebuf), fp) != NULL) {
char *ptr = linebuf;
while (*ptr == ' ' || *ptr == '\t')
++ptr;
if (*ptr == '#')
continue;
process_addr (linebuf);
}
}
/*
* open a new envelope.
*
* this could be done in lots of ways. SMTP has the advantage
* that it doesn't have any hard limit on the number of recipients,
* unlike a command line. (note that the code to limit the number
* of domains per envelope does nothing to limit the number of
* recipients)
*
* on the other hand, this implementation assumes that the SMTP server
* doesn't limit the number of recipients, which is not true for all
* SMTP implementations. and there are bugs in some sendmail versions
* that crop up when you feed them SMTP from a pipe.
*/
static FILE *
open_envelope ()
{
FILE *fp;
char command_buf[32*1024];
if (debug_flag)
fp = stderr;
else {
sprintf (command_buf, PIPECOMMAND, sendmail_flags);
if ((fp = popen (command_buf, "w")) == NULL) {
fprintf (stderr, "can't open pipe to sendmail: %s\n",
strerror (errno));
exit (EX_OSERR);
}
}
fprintf (fp, "HELO localhost\n");
/*
* Some versions of sendmail fork when they see MAIL FROM.
* Sendmail also uses stdio to read the SMTP socket.
* If there are already commands in the stdio buffer following
* the MAIL FROM, both the parent and child sendmail will see them,
* and the parent complains about bogus SMTP commands.
* The ONEX command inhibits forking, preventing this.
*/
fprintf (fp, "ONEX\n"); /* grrr */
fprintf (fp, "MAIL FROM:<%s>\n", envelope_from);
return fp;
}
static void
close_envelope (fp)
FILE *fp;
{
fprintf (fp, "QUIT\n");
if (fp != stderr)
pclose (fp);
}
static void
add_recipient (fp, rcpt)
FILE *fp;
char *rcpt;
{
fprintf (fp, "RCPT TO:<%s>\n", rcpt);
}
/*
* copy a message to an SMTP stream, taking care to handle dot quoting
* also add the initial DATA and the terminating '.'
*/
static void
enclose_msg (fp, file)
FILE *fp;
char *file;
{
int c;
int bol = 1;
FILE *infp;
if ((infp = fopen (file, "r")) == NULL) {
fprintf (stderr, "can't open %s (%s)\n", file, strerror (errno));
exit (EX_NOINPUT);
}
fprintf (fp, "DATA\n");
while ((c = getc (infp)) != EOF) {
if (bol && c == '.')
putc ('.', fp);
putc (c, fp);
bol = (c == '\n');
}
if (!bol)
putc ('\n', fp);
fprintf (fp, ".\n");
fclose (infp);
}
/*
* forward a message .
*/
static void
forward_msg (fp, file, to, subject)
FILE *fp;
char *file;
char *to, *subject;
{
int c;
int bol = 1;
FILE *infp;
if ((infp = fopen (file, "r")) == NULL) {
fprintf (stderr, "can't open %s (%s)\n", file, strerror (errno));
exit (EX_NOINPUT);
}
fprintf (fp, "DATA\n");
fprintf (fp, "To: %s\n", to);
fprintf (fp, "Subject: %s\n", subject);
fprintf (fp, "Content-type: message/rfc822\n");
fprintf (fp, "MIME-Version: 1.0\n");
fprintf (fp, "\n");
while ((c = getc (infp)) != EOF) {
if (bol && c == '.')
putc ('.', fp);
putc (c, fp);
bol = (c == '\n');
}
if (!bol)
putc ('\n', fp);
fprintf (fp, ".\n");
fclose (infp);
}
/*
* split a message up across several envelopes
*/
static void
dump_addrs (msgfile)
char *msgfile;
{
int i;
int domains_this_envelope = 0;
int rcpts_this_envelope = 0;
FILE *fp;
if (num_addrs <= 0)
return;
fp = open_envelope ();
/*
* for each recipient, if we have "room" in the current
* envelope, add her. otherwise, start a new envelope.
*/
for (i = 0; i < num_addrs; ++i) {
/*
* if there are no recipients in this envelope,
* we always have room. so go ahead and add.
*/
if (domains_this_envelope == 0) {
add_recipient (fp, address_list[i].address);
rcpts_this_envelope = 1;
domains_this_envelope = 1;
}
/*
* if too many recipients this envelope, start
* another envelope.
*/
else if (rcpts_this_envelope >= max_rcpts) {
enclose_msg (fp, msgfile);
close_envelope (fp);
fp = open_envelope ();
add_recipient (fp, address_list[i].address);
domains_this_envelope = 1;
rcpts_this_envelope = 1;
}
/*
* else check for too many domains per envelope.
* but if the domain of this recipient is the same
* as the domain of the previous recipient,
* add this recipient, as it won't increase the number of domains.
*/
else if (strcmp (address_list[i].niamod,
address_list[i-1].niamod) == 0) {
add_recipient (fp, address_list[i].address);
++rcpts_this_envelope;
}
#ifdef BITNET
/*
* BITNET is special, since we want to send all BITNET
* mail to the same gateway even if the node is different.
*
* should no longer be needed, as BITNET is long dead. (RIP)
* but who knows, we might need something else like this someday.
*/
else if (strncmp (address_list[i].niamod, "TENTIB.", 7) == 0 &&
strncmp (address_list[i-1].niamod, "TENTIB.", 7) == 0) {
add_recipient (fp, address_list[i].address);
++rcpts_this_envelope;
}
#endif /* BITNET */
else {
/*
* new domain, so it might overflow the envelope
* if this envelope is full, seal it and open another one
*/
if (domains_this_envelope >= max_domains) {
enclose_msg (fp, msgfile);
close_envelope (fp);
fp = open_envelope ();
domains_this_envelope = 0;
rcpts_this_envelope = 0;
}
add_recipient (fp, address_list[i].address);
++domains_this_envelope;
++rcpts_this_envelope;
}
}
enclose_msg (fp, msgfile);
close_envelope(fp);
}
/*
* return 1 iff 's' points to a To, Cc, or Bcc header
*/
static int
istohdr (s)
char *s;
{
if (strncasecmp (s, "to", 2) == 0)
s += 2;
else if (strncasecmp (s, "cc", 2) == 0)
s += 2;
else if (strncasecmp (s, "bcc", 3) == 0)
s += 3;
while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
++s;
return (*s == ':');
}
static int
contains (buf, str)
char *buf, *str;
{
register char c1, c2;
register char *ptr;
int length = strlen (str);
c1 = *str;
c2 = isupper (c1) ? tolower (c1) : toupper (c1);
for (ptr = buf; *ptr; ++ptr) {
if (*ptr == c1 || *ptr == c2) {
if ((ptr == buf || ptr[-1] == ' ' || ptr[-1] == '\t') &&
strncasecmp (ptr, str, length) == 0 &&
(ptr[length] == ' ' || ptr[length] == '\t' ||
ptr[length] == '\n' || ptr[length] == '\0'))
return 1;
}
}
return 0;
}
static int
match_command (buf, str)
char *buf, *str;
{
int len = strlen (str);
while (*buf == ' ' || *buf == '\t')
++buf;
return (strncasecmp (buf, str, len) == 0 &&
(buf[len] == ' ' || buf[len] == '\t' || buf[len] == '\n'));
}
/*
* return 1 iff 's' is a header field with name 'name'
*/
static int
match_hdr (name, s)
char *name, *s;
{
int nl = strlen (name);
if (strncasecmp (s, name, nl) == 0) {
s += nl;
while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
++s;
return (*s == ':');
}
return 0;
}
static int
match_prefix (buf, str)
char *str, *buf;
{
return (strncasecmp (str, buf, strlen (str)) == 0);
}
/*
* return 1 if 's' points to a blank line
*/
static int
isblankline (s)
char *s;
{
while (*s == ' ' || *s == '\t')
++s;
return (*s == '\r' || *s == '\n' || *s == '\0');
}
/*
* read in a header field, including continuation lines
*/
char *
get_hdr_field (buf, size, fp)
char *buf;
int size;
FILE *fp;
{
char *d = buf;
int c;
int bol = 1;
while ((c = getc (fp)) != EOF) {
/*
* if we're at the beginning of a line, and the buffer
* is empty (we're not at the start of a header),
* and the first character of the line is NOT a SPACE
* or HTAB, this is a new field. So put the character
* back on the input stream and return.
*/
if (bol && (d - buf) > 0 && c != ' ' && c != '\t') {
ungetc (c, fp);
*d = '\0';
return buf;
}
/*
* if the buffer is empty and we immediately see a
* newline, we've hit the blank line at the end of
* the header.
*/
if ((d - buf) == 0 && c == '\n')
return NULL;
/* stuff the character in the buffer */
*d++ = c;
bol = (c == '\n');
/* size check */
if (d - buf >= size - 1) {
buf[size-1] = '\0';
return buf;
}
}
/* at EOF, either return what's already in the buffer, or NULL */
if (d == buf)
return NULL;
else {
*d = 0;
return buf;
}
}
static char *
arpadate (t)
time_t *t;
{
struct tm gmt;
struct tm *lt;
static char datebuf[100];
int gmtoff;
char sign;
static char *months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
};
static char *wdays[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
};
/*
* "I'm ashamed of this." - SEK
*
* there's no portable function to get the offset between local time
* and gmt. so we call both localtime() and gmtime() with the same
* time clock and calculate the difference. also, gmtime() and
* localtime() share the same static return structure, so we have to
* copy the result of one before we call the other. yeech.
*/
gmt = *gmtime (t);
lt = localtime (t);
gmtoff = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
if (lt->tm_year != gmt.tm_year)
gmtoff += (lt->tm_year - gmt.tm_year) * 24 * 60;
else
gmtoff += (lt->tm_yday - gmt.tm_yday) * 24 * 60;
sign = '+';
if (gmtoff < 0) {
sign = '-';
gmtoff = -gmtoff;
}
sprintf (datebuf, "%s, %d %s %04d %02d:%02d:%02d %c%02d%02d",
wdays[lt->tm_wday], lt->tm_mday, months[lt->tm_mon], lt->tm_year + 1900,
lt->tm_hour, lt->tm_min, lt->tm_sec,
sign,
gmtoff / 60,
gmtoff % 60);
return datebuf;
}
/*
* copy a mesasge from 'in' to 'out', doing minimal header munging
* on-the-fly. Return true if it appears that the message has
* a subscribe command.
*/
#define MESSAGE_OKAY 0
#define HAS_EMBEDDED_COMMAND 1
#define LOOKS_LIKE_BOUNCE 2
#define WRITE_ERROR 3
static int
copy_message (out, in)
FILE *out, *in;
{
int c;
char linebuf[32*1024];
int has_valid_approved_hdr = 0;
int has_resent_to_hdr = 0;
int has_to_hdr = 0;
int has_reply_to_hdr = 0;
int has_message_id_hdr = 0;
int bol = 1;
time_t t = time ((time_t *) NULL);
int lines = 0;
int saw_command = 0;
int looks_like_bounce = 0;
/*
* add any prefix headers we want.
*/
fprintf (out, "Received: by %s (bulk_mailer v1.%d); %s\n",
local_domain, PATCHLEVEL, arpadate (&t));
/*
* copy header, checking for various fields
*
* - content-length, errors-to, and return-receipt-to are bogus
* and nonstandard fields, and are deleted with extreme prejudice.
*
* - return-path shouldn't be in the input (it should only appear
* at final delivery), so we nuke it.
*
* - If a message-id field does not appear in the input, we
* add one. This way all of the redistributed messages
* have the same message-id.
*
* - the nonstandard precedence header in the input gets unconditionally
* deleted, but the sysadmin can add one.
*
* - the sysadmin can add a reply-to if he wants to. depending on
* which option was used ("-reply-to" or "+reply-to"), this might
* or might not cause an existing reply-to header to be overridden.
*/
while (get_hdr_field (linebuf, sizeof(linebuf), in) != NULL) {
if (bol) {
if (strncmp (linebuf, "From ", 5) == 0)
goto delete_header;
else if (istohdr (linebuf))
has_to_hdr = 1;
else if (match_hdr ("approved", linebuf)) {
/* XXX need to actually check password */
has_valid_approved_hdr = 1;
goto delete_header;
}
else if (match_hdr ("content-type", linebuf)) {
char *colon = strchr (linebuf, ':');
while (*colon == ' ' || *colon == '\t')
++colon;
if (strncmp (colon, "multipart/report", 16) == 0)
looks_like_bounce = 1;
}
else if (match_hdr ("content-length", linebuf))
goto delete_header;
else if (match_hdr ("disposition-notification-to", linebuf)) {
/* XXX should optionally issue an MDN */
goto delete_header;
}
else if (match_hdr ("errors-to", linebuf))
goto delete_header;
else if (match_hdr ("from", linebuf)) {
char *colon = strchr (linebuf, ':');
if (strlen (colon) < 1024) {
char *temp = prefrobnicate (colon+1);
header_from = strdup (temp);
}
else
header_from = NULL;
}
else if (del_list_hdrs && match_hdr ("list-archive", linebuf))
goto delete_header;
else if (del_list_hdrs && match_hdr ("list-help", linebuf))
goto delete_header;
else if (del_list_hdrs && match_hdr ("list-id", linebuf))
goto delete_header;
else if (del_list_hdrs && match_hdr ("list-owner", linebuf))
goto delete_header;
else if (del_list_hdrs && match_hdr ("list-post", linebuf))
goto delete_header;
else if (del_list_hdrs && match_hdr ("list-subscribe", linebuf))
goto delete_header;
else if (del_list_hdrs && match_hdr ("list-unsubscribe", linebuf))
goto delete_header;
else if (match_hdr ("mail-followup-to", linebuf))
goto delete_header;
else if (match_hdr ("message-id", linebuf))
has_message_id_hdr = 1;
else if (match_hdr ("precedence", linebuf))
goto delete_header;
else if (match_hdr ("reply-to", linebuf)) {
if (force_reply_to)
goto delete_header; /* override existing reply-to */
has_reply_to_hdr = 1;
}
else if (match_hdr ("resent-to", linebuf))
has_resent_to_hdr = 1;
else if (match_hdr ("return-path", linebuf)) {
char *colon = strchr (linebuf, ':');
char *return_path = prefrobnicate (colon+1);
if (*return_path == '\0' ||
match_prefix (return_path, "mailer-daemon") ||
match_prefix (return_path, "mmdf") ||
match_prefix (return_path, "uucp"))
looks_like_bounce = 1;
else
goto delete_header;
}
else if (match_hdr ("return-receipt-to", linebuf))
goto delete_header;
else if (match_hdr ("sender", linebuf)) {
char *colon = strchr (linebuf, ':');
if (contains (colon+1, "mailer-daemon@")) {
fprintf (stderr,
"bulk_mailer: refusing to forward bounced mail to the list\n");
exit (EX_DATAERR);
}
}
else if (match_hdr ("subject", linebuf)) {
char *colon = strchr (linebuf, ':');
if (match_command (colon+1, "subscribe") ||
match_command (colon+1, "unsubscribe") ||
match_command (colon+1, "help"))
saw_command = 1;
else if (contains (colon+1, "automatic log from mail gateway") ||
contains (colon+1, "delivery confirmation") ||
contains (colon+1, "delivery error") ||
contains (colon+1, "delivery failure") ||
contains (colon+1, "delivery problems") ||
contains (colon+1, "delivery report") ||
contains (colon+1, "delivery status") ||
contains (colon+1, "failed mail") ||
contains (colon+1, "mail failure") ||
contains (colon+1, "mail system problem") ||
contains (colon+1, "message delayed") ||
contains (colon+1, "non-delivery of:") ||
contains (colon+1, "non-delivery notification") ||
contains (colon+1, "non-delivery report") ||
contains (colon+1, "nondelivery notification") ||
contains (colon+1, "rcpt:") ||
contains (colon+1, "receipt confirmation") ||
contains (colon+1, "returned mail") ||
contains (colon+1, "service message") ||
contains (colon+1, "undeliverable mail") ||
contains (colon+1, "undeliverable message") ||
contains (colon+1, "undelivered mail") ||
contains (colon+1, "waiting mail") ||
contains (colon+1, "warning from uucp") ||
contains (colon+1, "warning message") ||
contains (colon+1, "warning: cannot send mail"))
looks_like_bounce = 1;
}
else if (match_hdr ("transport-options", linebuf))
goto delete_header;
else if (match_hdr ("x-confirm-reading-to", linebuf))
goto delete_header; /* pegasus mail brain damage */
else if (match_hdr ("x-pmrqc", linebuf))
goto delete_header; /* pegasus mail brain damage */
else if (match_hdr ("x-report-type", linebuf)) {
char *colon = strchr (linebuf, ':');
if (contains (colon+1, "nondelivery"))
looks_like_bounce = 1;
}
else if (isblankline (linebuf)) {
break;
}
}
fputs (linebuf, out);
delete_header:
bol = (linebuf[strlen(linebuf)-1] == '\n');
}
/*
* End of header in input. Add any headers that need to appear
* at the end.
*/
if (precedence != NULL)
fprintf (out, "Precedence: %s\n", precedence);
if (comment != NULL)
fprintf (out, "Comment: %s\n", comment);
/* If we haven't seen a To, Cc, or Bcc Field, add a dummy one. */
if (has_to_hdr == 0)
fprintf (out, "To: undisclosed-recipients:;\n");
/* add a reply-to header if required */
if (reply_to != NULL &&
(force_reply_to == 1 || has_reply_to_hdr == 0)) {
fprintf (out, "Reply-To: %s\n", reply_to);
}
/* add a message-id header if there's not one there already */
if (has_message_id_hdr == 0) {
struct tm *tm = localtime (&t);
fprintf (out, "Message-ID: <bulk.%d.%4d%02d%02d%02d%02d%02d@%s>\n",
getpid(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, local_domain);
}
if (list_archive_hdr)
fprintf (out, "List-Archive: %s\n", list_archive_hdr);
if (list_help_hdr)
fprintf (out, "List-Help: %s\n", list_help_hdr);
if (list_id_hdr)
fprintf (out, "List-ID: %s\n", list_id_hdr);
if (list_owner_hdr)
fprintf (out, "List-Owner: %s\n", list_owner_hdr);
if (list_post_hdr)
fprintf (out, "List-Post: %s\n", list_post_hdr);
if (list_subscribe_hdr)
fprintf (out, "List-Subscribe: %s\n", list_subscribe_hdr);
if (list_unsubscribe_hdr)
fprintf (out, "List-Unsubscribe: %s\n", list_unsubscribe_hdr);
fprintf (out, "\n");
/*
* copy message body, notice if we've got an unsubscribe command
*/
lines = 0;
while (fgets (linebuf, sizeof (linebuf), in) != NULL) {
if (lines < 5 &&
/*
* these often occur in English-text unsubscribe requests
*/
contains (linebuf, "delete me") ||
contains (linebuf, "remove me") ||
contains (linebuf, "subscribe") ||
contains (linebuf, "unsubscribe"))
saw_command = 1;
++lines;
fputs (linebuf, out);
}
/*
* if we've seen an administrative command, and the message is short,
* and we didn't see an approved or resent-to header, return
* a special code so that our caller will know to forward us to
* the human.
*/
fflush (out);
if (has_valid_approved_hdr == 0 && looks_like_bounce)
return LOOKS_LIKE_BOUNCE;
else if (has_valid_approved_hdr == 0 && saw_command > 0 &&
subscribe_flag && lines <= 7)
return HAS_EMBEDDED_COMMAND;
else if (ferror (out))
return WRITE_ERROR;
else
return MESSAGE_OKAY;
}
static int
size_of_file (fd)
int fd;
{
struct stat sbuf;
if (fstat (fd, &sbuf) < 0)
return 0x7fffffff;
return sbuf.st_size;
}
static int
digits_only (s)
char *s;
{
do {
if (!isdigit (*s))
return 0;
} while (*++s);
return 1;
}
static char *
get_local_domain_name ()
{
static char buf[1024];
int i;
if (gethostname (buf, sizeof (buf)) == 0) {
if (strchr (buf, '.'))
return buf;
else {
struct hostent *hp = gethostbyname (buf);
if (hp) {
if (strchr (hp->h_name, '.')) {
strcpy (buf, hp->h_name);
return buf;
}
else {
for (i = 0; hp->h_addr_list[i]; ++i) {
if (strchr (hp->h_addr_list[i], '.')) {
strcpy (buf, hp->h_addr_list[i]);
return buf;
}
}
}
}
}
}
return NULL;
}
/*
* majordomo 'resend' options we might want to support someday:
*
* -C <config-file> specify alternate config file (must be first!)
* -l <list-name> REQUIRED: specify list name
* -h <host-name> REQUIRED: specify host name
* -f <from-addr> specify "sender" (default <list-name>-request)
* -m <sendmail-flags> specify special sendmail flags
* -M <max-msg-length> specify max message length to forward
* -p <precedence> add "Precedence: <precedence>" header
* -r <reply-to> add "Reply-To: <reply-to>" header
* -I <file-list> Bounce messages from users not listed in file
* in colon-separated <file-list>
* -a <passwd> approval password
* -A moderate list (require "Approved:" for posting)
* -R delete "Received:" lines
* -s enable "administrivia" checks
* -d debug; say it, but don't do it
*/
int
main (argc, argv)
int argc;
char *argv[];
{
int i;
FILE *fp;
FILE *tmp;
static char template[] = "/tmp/blkXXXXXX";
char *tempname;
int c;
char buf[1024];
while (argc > 1 && (*argv[1] == '-' || *argv[1] == '+')) {
if (strcmp (argv[1], "-comment") == 0 && argc > 2) {
comment = argv[2];
--argc;
++argv;
}
else if (strcmp (argv[1], "-debug") == 0)
++debug_flag;
else if (strcmp (argv[1], "-delete-list-hdrs") == 0)
++del_list_hdrs;
else if (strcmp (argv[1], "-domain") == 0 && argc > 2) {
if (strchr (argv[2], '.') == NULL) {
fprintf (stderr,
"Illegal argument to -domain: %s is not a fully-qualified domain name\n",
argv[2]);
exit (EX_USAGE);
}
local_domain = argv[2]; /* done */
--argc;
++argv;
}
else if (strcmp (argv[1], "-list-archive") == 0 && argc > 2) {
list_archive_hdr = argv[2];
del_list_hdrs = 1;
--argc;
++argv;
}
else if (strcmp (argv[1], "-list-help") == 0 && argc > 2) {
list_help_hdr = argv[2];
del_list_hdrs = 1;
--argc;
++argv;
}
else if (strcmp (argv[1], "-list-id") == 0 && argc > 2) {
list_id_hdr = argv[2];
del_list_hdrs = 1;
--argc;
++argv;
}
else if (strcmp (argv[1], "-list-owner") == 0 && argc > 2) {
list_owner_hdr = argv[2];
del_list_hdrs = 1;
--argc;
++argv;
}
else if (strcmp (argv[1], "-list-post") == 0 && argc > 2) {
list_post_hdr = argv[2];
del_list_hdrs = 1;
--argc;
++argv;
}
else if (strcmp (argv[1], "-list-subscribe") == 0 && argc > 2) {
list_subscribe_hdr = argv[2];
del_list_hdrs = 1;
--argc;
++argv;
}
else if (strcmp (argv[1], "-list-unsubscribe") == 0 && argc > 2) {
list_unsubscribe_hdr = argv[2];
del_list_hdrs = 1;
--argc;
++argv;
}
else if (strcmp (argv[1], "-maxdomains") == 0 && argc > 2) {
max_domains = atoi (argv[2]);
if (max_domains <= 0 || !digits_only (argv[2])) {
fprintf (stderr, "illegal value for -maxdomains: %s\n",
argv[2]);
exit (EX_USAGE);
}
--argc;
++argv;
}
else if (strcmp (argv[1], "-maxrcpts") == 0 && argc > 2) {
max_rcpts = atoi (argv[2]);
if (max_rcpts <= 0 || !digits_only (argv[2])) {
fprintf (stderr, "illegal value for -maxrcpts: %s\n",
argv[2]);
exit (EX_USAGE);
}
--argc;
++argv;
}
else if (strcmp (argv[1], "-maxsize") == 0 && argc > 2) {
max_size = atoi (argv[2]);
if (max_size <= 0 || !digits_only (argv[2])) {
fprintf (stderr, "illegal value for -maxsize: %s\n",
argv[2]);
exit (EX_USAGE);
}
--argc;
++argv;
}
else if (strcmp (argv[1], "-owner") == 0 && argc > 2) {
list_owner = argv[2];
--argc;
++argv;
}
else if (strcmp (argv[1], "-precedence") == 0 && argc > 2) {
precedence = argv[2];
--argc;
++argv;
}
else if (strcmp (argv[1], "-private") == 0)
private_flag = 1;
else if (strcmp (argv[1], "-reply-to") == 0 && argc > 2) {
reply_to = argv[2];
--argc;
++argv;
}
else if (strcmp (argv[1], "+reply-to") == 0 && argc > 2) {
reply_to = argv[2];
force_reply_to = 1;
--argc;
++argv;
}
else if (strcmp (argv[1], "-s") == 0)
subscribe_flag = 1;
else if (strcmp (argv[1], "-sendmail") == 0 && argc > 2) {
sendmail_flags = argv[2];
--argc;
++argv;
}
else if (strcmp (argv[1], "-v") == 0)
++verbose_flag;
else {
fprintf (stderr, "unknown flag %s\n", argv[1]);
exit (EX_USAGE);
}
--argc;
++argv;
}
/*
* NB: List-ID is deliberately not documented until the RFC comes out
*/
if (argc != 3) {
fprintf (stderr,
"usage: bulk_mailer [options] envelope_from recipient_list_file\n");
fprintf (stderr, "(message appears on stdin)\n");
fprintf (stderr, "options are:\n");
fprintf (stderr, "\
-comment text add a header of the form \"Comment: text\"\n\
-debug display SMTP output on stderr; don't send msg\n\
-delete-list-hdrs delete any List-* headers in input\n\
-domain xx.yy.zz set domain name to 'xx.yy.zz' (overrides default)\n\
-list-archive xxx add a 'List-Archive: xxx' field to output (*)\n\
-list-help xxx add a 'List-Help: xxx' field to output (*)\n\
-list-owner xxx add a 'List-Owner: xxx' field to output (*)\n\
-list-post xxx add a 'List-Post: xxx' field to output (*)\n\
-list-unsubscribe xxx add a 'List-Unsubscribe: xxx' field to output (*)\n\
-maxdomains ### set max # of distinct domains per envelope\n\
-maxrcpts ### set max # of recipients per envelope\n\
-maxsize #### set maximum message size\n\
-owner user@domain set name of list owner\n\
-precedence keyword add a 'Precedence: keyword' header field (NR)\n\
-private bounce if From address is not a list subscriber\n\
-reply-to user@domain add a reply-to header if there's not one there\n\
+reply-to user@domain force reply-to header even if there already (NR)\n\
-s check for 'unsubscribe' & similar commands\n\
(if found: don't deliver, forward to list owner)\n\
-sendmail flags pass 'flags' to sendmail\n\
-v turn on verbose mode\n\
\n\
NOTES:\n\
(NR) = NOT RECOMMENDED\n\
(*) = read RFC 2369 before setting these options\n");
exit (EX_USAGE);
}
envelope_from = argv[1];
if (list_owner == NULL)
list_owner = envelope_from;
if (local_domain == NULL)
local_domain = get_local_domain_name ();
if (local_domain == NULL) {
fprintf (stderr, "bulk_mailer: can't find local domain\n");
exit (EX_OSFILE);
}
tempname = mktemp (template);
tmp = fopen (template, "w");
switch (copy_message (tmp, stdin)) {
case HAS_EMBEDDED_COMMAND:
fclose (tmp);
tmp = open_envelope ();
add_recipient (tmp, list_owner);
forward_msg (tmp, tempname, list_owner,
"message appears to have a list server command");
close_envelope (tmp);
unlink (tempname);
exit (EX_OK);
case LOOKS_LIKE_BOUNCE:
fclose (tmp);
tmp = open_envelope ();
add_recipient (tmp, list_owner);
forward_msg (tmp, tempname, list_owner,
"this looks like a bounced message");
close_envelope (tmp);
unlink (tempname);
exit (EX_OK);
case WRITE_ERROR:
fprintf (stderr, "bulk_mailer: error writing temp file:\n");
fprintf (stderr, "%s\n", strerror (errno));
fclose (tmp);
unlink (tempname);
exit (EX_TEMPFAIL);
case MESSAGE_OKAY:
if (size_of_file (fileno (tmp)) > max_size) {
fprintf (stderr,
"bulk_mailer: message size (%d bytes) exceeds administrative limit\n",
size_of_file (fileno (tmp)));
fclose (tmp);
unlink (tempname);
exit (EX_NOPERM);
}
fclose (tmp);
if ((fp = fopen (argv[2], "r")) == NULL) {
fprintf (stderr,
"bulk_mailer: can't open recipient list %s (%s)\n",
argv[2], strerror (errno));
unlink (tempname);
exit (EX_NOINPUT);
}
process (fp);
fclose (fp);
/*
* crude support for bouncing messages from non-subscribers.
* Ideally, this would:
* a) allow site-configurable bounce messages
* b) allow a choice of bouncing, forwarding to the moderator,
* or (perhaps) silently discarding
* c) support a separate "authorized posters who are not on
* the list" file.
* d) support fuzzy matching (disregarding subaddresses)
*/
if (private_flag && !author_is_subscriber) {
fprintf (stderr, "bulk_mailer: The author of this message\n");
if (header_from)
fprintf (stderr, "<%s>\n", header_from);
fprintf (stderr, "is not subscribed to this mailing list,\n");
fprintf (stderr, "and the mailing list administrator\n");
fprintf (stderr, "has configured this list to refuse\n");
fprintf (stderr, "contributions from non-subscribers.\n");
fprintf (stderr, "The message is therefore being returned.\n");
unlink (tempname);
exit (EX_NOPERM);
}
sort_addrs ();
dump_addrs (tempname);
unlink (tempname);
exit (EX_OK);
}
}
|