2 * Copyright (c) 1998-2004 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
17 SM_RCSID("@(#)$Id: headers.c,v 8.287 2004/12/03 18:29:51 ca Exp $")
19 static HDR *allocheader __P((char *, char *, int, SM_RPOOL_T *));
20 static size_t fix_mime_header __P((HDR *, ENVELOPE *));
21 static int priencode __P((char *));
22 static void put_vanilla_header __P((HDR *, char *, MCI *));
25 ** SETUPHEADERS -- initialize headers in symbol table
40 for (hi = HdrInfo; hi->hi_field != NULL; hi++)
42 s = stab(hi->hi_field, ST_HEADER, ST_ENTER);
43 s->s_header.hi_flags = hi->hi_flags;
44 s->s_header.hi_ruleset = NULL;
48 ** CHOMPHEADER -- process and save a header line.
50 ** Called by collect, readcf, and readqf to deal with header lines.
53 ** line -- header as a text line.
54 ** pflag -- flags for chompheader() (from sendmail.h)
55 ** hdrp -- a pointer to the place to save the header.
56 ** e -- the envelope including this header.
59 ** flags for this header.
62 ** The header is saved on the header list.
63 ** Contents of 'line' are destroyed.
66 static struct hdrinfo NormalHeader = { NULL, 0, NULL };
69 chompheader(line, pflag, hdrp, e)
75 unsigned char mid = '\0';
86 bool nullheader = false;
91 sm_dprintf("chompheader: ");
92 xputs(sm_debug_file(), line);
96 headeronly = hdrp != NULL;
100 /* strip off options */
103 if (!bitset(pflag, CHHDR_USER) && *p == '?')
108 q = strchr(++p, '?');
115 /* possibly macro conditional */
116 if (c == MACROEXPAND)
125 mid = (unsigned char) *p++;
143 mid = (unsigned char) macid(p);
144 if (bitset(0200, mid))
146 p += strlen(macname(mid)) + 2;
169 setbitn(bitidx(*p), mopts);
177 /* find canonical name */
179 while (isascii(*p) && isgraph(*p) && *p != ':')
182 while (isascii(*p) && isspace(*p))
184 if (*p++ != ':' || fname == fvalue)
187 syserr("553 5.3.0 header syntax error, line \"%s\"", line);
192 /* strip field value on front */
197 /* if the field is null, go ahead and use the default */
198 while (isascii(*p) && isspace(*p))
203 /* security scan: long field names are end-of-header */
204 if (strlen(fname) > 100)
207 /* check to see if it represents a ruleset call */
208 if (bitset(pflag, CHHDR_DEF))
212 (void) expand(fvalue, hbuf, sizeof hbuf, e);
213 for (p = hbuf; isascii(*p) && isspace(*p); )
215 if ((*p++ & 0377) == CALLSUBR)
220 strc = *p == '+'; /* strip comments? */
223 if (strtorwset(p, &endp, ST_ENTER) > 0)
226 s = stab(fname, ST_HEADER, ST_ENTER);
228 s->s_header.hi_ruleset != NULL)
229 sm_syslog(LOG_WARNING, NOQID,
230 "Warning: redefined ruleset for header=%s, old=%s, new=%s",
232 s->s_header.hi_ruleset, p);
233 s->s_header.hi_ruleset = newstr(p);
235 s->s_header.hi_flags |= H_STRIPCOMM;
241 /* see if it is a known type */
242 s = stab(fname, ST_HEADER, ST_FIND);
251 sm_dprintf("no header flags match\n");
253 sm_dprintf("header match, flags=%lx, ruleset=%s\n",
255 hi->hi_ruleset == NULL ? "<NULL>"
259 /* see if this is a resent message */
260 if (!bitset(pflag, CHHDR_DEF) && !headeronly &&
261 bitset(H_RESENT, hi->hi_flags))
262 e->e_flags |= EF_RESENT;
264 /* if this is an Errors-To: header keep track of it now */
265 if (UseErrorsTo && !bitset(pflag, CHHDR_DEF) && !headeronly &&
266 bitset(H_ERRORSTO, hi->hi_flags))
267 (void) sendtolist(fvalue, NULLADDR, &e->e_errorqueue, 0, e);
269 /* if this means "end of header" quit now */
270 if (!headeronly && bitset(H_EOH, hi->hi_flags))
274 ** Horrible hack to work around problem with Lotus Notes SMTP
275 ** mail gateway, which generates From: headers with newlines in
276 ** them and the <address> on the second line. Although this is
277 ** legal RFC 822, many MUAs don't handle this properly and thus
278 ** never find the actual address.
281 if (bitset(H_FROM, hi->hi_flags) && SingleLineFromHeader)
283 while ((p = strchr(fvalue, '\n')) != NULL)
288 ** If there is a check ruleset, verify it against the header.
291 if (bitset(pflag, CHHDR_CHECK))
296 rscheckflags = RSF_COUNT;
297 if (!bitset(hi->hi_flags, H_FROM|H_RCPT))
298 rscheckflags |= RSF_UNSTRUCTURED;
300 /* no ruleset? look for default */
304 s = stab("*", ST_HEADER, ST_FIND);
307 rs = (&s->s_header)->hi_ruleset;
308 if (bitset((&s->s_header)->hi_flags,
310 rscheckflags |= RSF_RMCOMM;
313 else if (bitset(hi->hi_flags, H_STRIPCOMM))
314 rscheckflags |= RSF_RMCOMM;
323 /* - 3 to avoid problems with " at the end */
324 /* should be sizeof(qval), not MAXNAME */
325 for (k = 0; fvalue[k] != '\0' && l < MAXNAME - 3; k++)
329 /* XXX other control chars? */
330 case '\011': /* ht */
331 case '\012': /* nl */
332 case '\013': /* vt */
333 case '\014': /* np */
334 case '\015': /* cr */
341 qval[l++] = fvalue[k];
347 k += strlen(fvalue + k);
351 sm_syslog(LOG_WARNING, e->e_id,
352 "Warning: truncated header '%s' before check with '%s' len=%d max=%d",
353 fname, rs, k, MAXNAME - 1);
355 macdefine(&e->e_macro, A_TEMP,
356 macid("{currHeader}"), qval);
357 macdefine(&e->e_macro, A_TEMP,
358 macid("{hdr_name}"), fname);
360 (void) sm_snprintf(qval, sizeof qval, "%d", k);
361 macdefine(&e->e_macro, A_TEMP, macid("{hdrlen}"), qval);
363 if (bitset(H_FROM, hi->hi_flags))
364 macdefine(&e->e_macro, A_PERM,
365 macid("{addr_type}"), "h s");
366 else if (bitset(H_RCPT, hi->hi_flags))
367 macdefine(&e->e_macro, A_PERM,
368 macid("{addr_type}"), "h r");
370 #endif /* _FFR_HDR_TYPE */
371 macdefine(&e->e_macro, A_PERM,
372 macid("{addr_type}"), "h");
373 (void) rscheck(rs, fvalue, NULL, e, rscheckflags, 3,
379 ** Drop explicit From: if same as what we would generate.
380 ** This is to make MH (which doesn't always give a full name)
381 ** insert the full name information in all circumstances.
386 if (!bitset(EF_RESENT, e->e_flags))
388 if (!bitset(pflag, CHHDR_DEF) && !headeronly &&
389 !bitset(EF_QUEUERUN, e->e_flags) && sm_strcasecmp(fname, p) == 0)
393 sm_dprintf("comparing header from (%s) against default (%s or %s)\n",
394 fvalue, e->e_from.q_paddr, e->e_from.q_user);
396 if (e->e_from.q_paddr != NULL &&
397 e->e_from.q_mailer != NULL &&
398 bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) &&
399 (strcmp(fvalue, e->e_from.q_paddr) == 0 ||
400 strcmp(fvalue, e->e_from.q_user) == 0))
404 /* delete default value for this header */
405 for (hp = hdrp; (h = *hp) != NULL; hp = &h->h_link)
407 if (sm_strcasecmp(fname, h->h_field) == 0 &&
408 !bitset(H_USER, h->h_flags) &&
409 !bitset(H_FORCE, h->h_flags))
413 /* user-supplied value was null */
418 /* make this look like the user entered it */
419 h->h_flags |= H_USER;
425 /* copy conditions from default case */
426 memmove((char *) mopts, (char *) h->h_mflags,
433 /* create a new node */
434 h = (HDR *) sm_rpool_malloc_x(e->e_rpool, sizeof *h);
435 h->h_field = sm_rpool_strdup_x(e->e_rpool, fname);
436 h->h_value = sm_rpool_strdup_x(e->e_rpool, fvalue);
438 memmove((char *) h->h_mflags, (char *) mopts, sizeof mopts);
441 h->h_flags = hi->hi_flags;
442 if (bitset(pflag, CHHDR_USER) || bitset(pflag, CHHDR_QUEUE))
443 h->h_flags |= H_USER;
445 /* strip EOH flag if parsing MIME headers */
447 h->h_flags &= ~H_EOH;
448 if (bitset(pflag, CHHDR_DEF))
449 h->h_flags |= H_DEFAULT;
450 if (cond || mid != '\0')
451 h->h_flags |= H_CHECK;
453 /* hack to see if this is a new format message */
454 if (!bitset(pflag, CHHDR_DEF) && !headeronly &&
455 bitset(H_RCPT|H_FROM, h->h_flags) &&
456 (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
457 strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
459 e->e_flags &= ~EF_OLDSTYLE;
465 ** ALLOCHEADER -- allocate a header entry
468 ** field -- the name of the header field.
469 ** value -- the value of the field.
470 ** flags -- flags to add to h_flags.
471 ** rp -- resource pool for allocations
474 ** Pointer to a newly allocated and populated HDR.
478 allocheader(field, value, flags, rp)
487 /* find info struct */
488 s = stab(field, ST_HEADER, ST_FIND);
490 /* allocate space for new header */
491 h = (HDR *) sm_rpool_malloc_x(rp, sizeof *h);
493 h->h_value = sm_rpool_strdup_x(rp, value);
496 h->h_flags |= s->s_header.hi_flags;
497 clrbitmap(h->h_mflags);
503 ** ADDHEADER -- add a header entry to the end of the queue.
505 ** This bypasses the special checking of chompheader.
508 ** field -- the name of the header field.
509 ** value -- the value of the field.
510 ** flags -- flags to add to h_flags.
517 ** adds the field on the list of headers for this envelope.
521 addheader(field, value, flags, e)
529 HDR **hdrlist = &e->e_header;
531 /* find current place in list -- keep back pointer? */
532 for (hp = hdrlist; (h = *hp) != NULL; hp = &h->h_link)
534 if (sm_strcasecmp(field, h->h_field) == 0)
538 /* allocate space for new header */
539 h = allocheader(field, value, flags, e->e_rpool);
544 ** INSHEADER -- insert a header entry at the specified index
546 ** This bypasses the special checking of chompheader.
549 ** idx -- index into the header list at which to insert
550 ** field -- the name of the header field.
551 ** value -- the value of the field.
552 ** flags -- flags to add to h_flags.
559 ** inserts the field on the list of headers for this envelope.
563 insheader(idx, field, value, flags, e)
570 HDR *h, *srch, *last = NULL;
572 /* allocate space for new header */
573 h = allocheader(field, value, flags, e->e_rpool);
575 /* find insertion position */
576 for (srch = e->e_header; srch != NULL && idx > 0;
577 srch = srch->h_link, idx--)
580 if (e->e_header == NULL)
585 else if (srch == NULL)
587 SM_ASSERT(last != NULL);
593 h->h_link = srch->h_link;
598 ** HVALUE -- return value of a header.
600 ** Only "real" fields (i.e., ones that have not been supplied
601 ** as a default) are used.
604 ** field -- the field name.
605 ** header -- the header list.
608 ** pointer to the value part.
609 ** NULL if not found.
616 hvalue(field, header)
622 for (h = header; h != NULL; h = h->h_link)
624 if (!bitset(H_DEFAULT, h->h_flags) &&
625 sm_strcasecmp(h->h_field, field) == 0)
631 ** ISHEADER -- predicate telling if argument is a header.
633 ** A line is a header if it has a single word followed by
634 ** optional white space followed by a colon.
636 ** Header fields beginning with two dashes, although technically
637 ** permitted by RFC822, are automatically rejected in order
638 ** to make MIME work out. Without this we could have a technically
639 ** legal header such as ``--"foo:bar"'' that would also be a legal
643 ** h -- string to check for possible headerness.
646 ** true if h is a header.
657 register char *s = h;
659 if (s[0] == '-' && s[1] == '-')
662 while (*s > ' ' && *s != ':' && *s != '\0')
668 /* following technically violates RFC822 */
669 while (isascii(*s) && isspace(*s))
675 ** EATHEADER -- run through the stored header and extract info.
678 ** e -- the envelope to process.
679 ** full -- if set, do full processing (e.g., compute
680 ** message priority). This should not be set
681 ** when reading a queue file because some info
682 ** needed to compute the priority is wrong.
683 ** log -- call logsender()?
689 ** Sets a bunch of global variables from information
690 ** in the collected header.
694 eatheader(e, full, log)
695 register ENVELOPE *e;
705 ** Set up macros for possible expansion in headers.
708 macdefine(&e->e_macro, A_PERM, 'f', e->e_sender);
709 macdefine(&e->e_macro, A_PERM, 'g', e->e_sender);
710 if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0')
711 macdefine(&e->e_macro, A_PERM, 'u', e->e_origrcpt);
713 macdefine(&e->e_macro, A_PERM, 'u', NULL);
715 /* full name of from person */
716 p = hvalue("full-name", e->e_header);
719 if (!rfc822_string(p))
722 ** Quote a full name with special characters
723 ** as a comment so crackaddr() doesn't destroy
724 ** the name portion of the address.
727 p = addquotes(p, e->e_rpool);
729 macdefine(&e->e_macro, A_PERM, 'x', p);
733 sm_dprintf("----- collected header -----\n");
735 for (h = e->e_header; h != NULL; h = h->h_link)
738 sm_dprintf("%s: ", h->h_field);
739 if (h->h_value == NULL)
742 sm_dprintf("<NULL>\n");
746 /* do early binding */
747 if (bitset(H_DEFAULT, h->h_flags) &&
748 !bitset(H_BINDLATE, h->h_flags))
753 xputs(sm_debug_file(), h->h_value);
756 expand(h->h_value, buf, sizeof buf, e);
759 if (bitset(H_FROM, h->h_flags))
760 expand(crackaddr(buf, e),
762 h->h_value = sm_rpool_strdup_x(e->e_rpool, buf);
763 h->h_flags &= ~H_DEFAULT;
768 xputs(sm_debug_file(), h->h_value);
772 /* count the number of times it has been processed */
773 if (bitset(H_TRACE, h->h_flags))
776 /* send to this person if we so desire */
777 if (GrabTo && bitset(H_RCPT, h->h_flags) &&
778 !bitset(H_DEFAULT, h->h_flags) &&
779 (!bitset(EF_RESENT, e->e_flags) ||
780 bitset(H_RESENT, h->h_flags)))
783 int saveflags = e->e_flags;
786 (void) sendtolist(denlstring(h->h_value, true, false),
787 NULLADDR, &e->e_sendqueue, 0, e);
791 ** Change functionality so a fatal error on an
792 ** address doesn't affect the entire envelope.
795 /* delete fatal errors generated by this address */
796 if (!bitset(EF_FATALERRS, saveflags))
797 e->e_flags &= ~EF_FATALERRS;
801 /* save the message-id for logging */
802 p = "resent-message-id";
803 if (!bitset(EF_RESENT, e->e_flags))
805 if (sm_strcasecmp(h->h_field, p) == 0)
807 e->e_msgid = h->h_value;
808 while (isascii(*e->e_msgid) && isspace(*e->e_msgid))
810 macdefine(&e->e_macro, A_PERM, macid("{msg_id}"),
815 sm_dprintf("----------------------------\n");
817 /* if we are just verifying (that is, sendmail -t -bv), drop out now */
818 if (OpMode == MD_VERIFY)
821 /* store hop count */
822 if (hopcnt > e->e_hopcount)
824 e->e_hopcount = hopcnt;
825 (void) sm_snprintf(buf, sizeof buf, "%d", e->e_hopcount);
826 macdefine(&e->e_macro, A_TEMP, 'c', buf);
829 /* message priority */
830 p = hvalue("precedence", e->e_header);
832 e->e_class = priencode(p);
834 e->e_timeoutclass = TOC_NONURGENT;
835 else if (e->e_class > 0)
836 e->e_timeoutclass = TOC_URGENT;
839 e->e_msgpriority = e->e_msgsize
840 - e->e_class * WkClassFact
841 + e->e_nrcpts * WkRecipFact;
844 /* check for DSN to properly set e_timeoutclass */
845 p = hvalue("content-type", e->e_header);
850 char pvpbuf[MAXLINE];
851 extern unsigned char MimeTokenTab[256];
853 /* tokenize header */
856 pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
857 MimeTokenTab, false);
860 /* Check if multipart/report */
861 if (pvp != NULL && pvp[0] != NULL &&
862 pvp[1] != NULL && pvp[2] != NULL &&
863 sm_strcasecmp(*pvp++, "multipart") == 0 &&
864 strcmp(*pvp++, "/") == 0 &&
865 sm_strcasecmp(*pvp++, "report") == 0)
867 /* Look for report-type=delivery-status */
870 /* skip to semicolon separator */
871 while (*pvp != NULL && strcmp(*pvp, ";") != 0)
875 if (*pvp++ == NULL || *pvp == NULL)
878 /* look for report-type */
879 if (sm_strcasecmp(*pvp++, "report-type") != 0)
883 if (*pvp == NULL || strcmp(*pvp, "=") != 0)
887 if (*++pvp != NULL &&
889 "delivery-status") == 0)
890 e->e_timeoutclass = TOC_DSN;
892 /* found report-type, no need to continue */
898 /* message timeout priority */
899 p = hvalue("priority", e->e_header);
902 /* (this should be in the configuration file) */
903 if (sm_strcasecmp(p, "urgent") == 0)
904 e->e_timeoutclass = TOC_URGENT;
905 else if (sm_strcasecmp(p, "normal") == 0)
906 e->e_timeoutclass = TOC_NORMAL;
907 else if (sm_strcasecmp(p, "non-urgent") == 0)
908 e->e_timeoutclass = TOC_NONURGENT;
909 else if (bitset(EF_RESPONSE, e->e_flags))
910 e->e_timeoutclass = TOC_DSN;
912 else if (bitset(EF_RESPONSE, e->e_flags))
913 e->e_timeoutclass = TOC_DSN;
915 /* date message originated */
916 p = hvalue("posted-date", e->e_header);
918 p = hvalue("date", e->e_header);
920 macdefine(&e->e_macro, A_PERM, 'a', p);
922 /* check to see if this is a MIME message */
923 if ((e->e_bodytype != NULL &&
924 sm_strcasecmp(e->e_bodytype, "8BITMIME") == 0) ||
925 hvalue("MIME-Version", e->e_header) != NULL)
927 e->e_flags |= EF_IS_MIME;
929 e->e_bodytype = "8BITMIME";
931 else if ((p = hvalue("Content-Type", e->e_header)) != NULL)
933 /* this may be an RFC 1049 message */
934 p = strpbrk(p, ";/");
935 if (p == NULL || *p == ';')
938 e->e_flags |= EF_DONT_MIME;
943 ** From person in antiquated ARPANET mode
944 ** required by UK Grey Book e-mail gateways (sigh)
947 if (OpMode == MD_ARPAFTP)
949 register struct hdrinfo *hi;
951 for (hi = HdrInfo; hi->hi_field != NULL; hi++)
953 if (bitset(H_FROM, hi->hi_flags) &&
954 (!bitset(H_RESENT, hi->hi_flags) ||
955 bitset(EF_RESENT, e->e_flags)) &&
956 (p = hvalue(hi->hi_field, e->e_header)) != NULL)
959 if (hi->hi_field != NULL)
962 sm_dprintf("eatheader: setsender(*%s == %s)\n",
964 setsender(p, e, NULL, '\0', true);
969 ** Log collection information.
972 if (log && bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
974 logsender(e, e->e_msgid);
975 e->e_flags &= ~EF_LOGSENDER;
979 ** LOGSENDER -- log sender information
982 ** e -- the envelope to log
983 ** msgid -- the message id
991 register ENVELOPE *e;
998 char hbuf[MAXNAME + 1];
999 char sbuf[MAXLINE + 1];
1000 char mbuf[MAXNAME + 1];
1002 /* don't allow newlines in the message-id */
1003 /* XXX do we still need this? sm_syslog() replaces control chars */
1007 if (l > sizeof mbuf - 1)
1008 l = sizeof mbuf - 1;
1009 memmove(mbuf, msgid, l);
1012 while ((p = strchr(p, '\n')) != NULL)
1016 if (bitset(EF_RESPONSE, e->e_flags))
1017 name = "[RESPONSE]";
1018 else if ((name = macvalue('_', e)) != NULL)
1021 else if (RealHostName == NULL)
1023 else if (RealHostName[0] == '[')
1024 name = RealHostName;
1028 (void) sm_snprintf(hbuf, sizeof hbuf, "%.80s", RealHostName);
1029 if (RealHostAddr.sa.sa_family != 0)
1031 p = &hbuf[strlen(hbuf)];
1032 (void) sm_snprintf(p, SPACELEFT(hbuf, p),
1034 anynet_ntoa(&RealHostAddr));
1038 /* some versions of syslog only take 5 printf args */
1039 #if (SYSLOG_BUFSIZE) >= 256
1041 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1042 "from=%.200s, size=%ld, class=%d, nrcpts=%d",
1043 e->e_from.q_paddr == NULL ? "<NONE>" : e->e_from.q_paddr,
1044 e->e_msgsize, e->e_class, e->e_nrcpts);
1048 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1049 ", msgid=%.100s", mbuf);
1052 if (e->e_bodytype != NULL)
1054 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1055 ", bodytype=%.20s", e->e_bodytype);
1058 p = macvalue('r', e);
1061 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1062 ", proto=%.20s", p);
1065 p = macvalue(macid("{daemon_name}"), e);
1068 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1069 ", daemon=%.20s", p);
1072 sm_syslog(LOG_INFO, e->e_id, "%.850s, relay=%s", sbuf, name);
1074 #else /* (SYSLOG_BUFSIZE) >= 256 */
1076 sm_syslog(LOG_INFO, e->e_id,
1078 e->e_from.q_paddr == NULL ? "<NONE>"
1079 : shortenstring(e->e_from.q_paddr,
1081 sm_syslog(LOG_INFO, e->e_id,
1082 "size=%ld, class=%ld, nrcpts=%d",
1083 e->e_msgsize, e->e_class, e->e_nrcpts);
1085 sm_syslog(LOG_INFO, e->e_id,
1087 shortenstring(mbuf, 83));
1090 if (e->e_bodytype != NULL)
1092 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1093 "bodytype=%.20s, ", e->e_bodytype);
1096 p = macvalue('r', e);
1099 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1100 "proto=%.20s, ", p);
1103 sm_syslog(LOG_INFO, e->e_id,
1104 "%.400srelay=%s", sbuf, name);
1105 #endif /* (SYSLOG_BUFSIZE) >= 256 */
1108 ** PRIENCODE -- encode external priority names into internal values.
1111 ** p -- priority in ascii.
1114 ** priority as a numeric level.
1126 for (i = 0; i < NumPriorities; i++)
1128 if (sm_strcasecmp(p, Priorities[i].pri_name) == 0)
1129 return Priorities[i].pri_val;
1132 /* unknown priority */
1136 ** CRACKADDR -- parse an address and turn it into a macro
1138 ** This doesn't actually parse the address -- it just extracts
1139 ** it and replaces it with "$g". The parse is totally ad hoc
1140 ** and isn't even guaranteed to leave something syntactically
1141 ** identical to what it started with. However, it does leave
1142 ** something semantically identical if possible, else at least
1143 ** syntactically correct.
1145 ** For example, it changes "Real Name <real@example.com> (Comment)"
1146 ** to "Real Name <$g> (Comment)".
1148 ** This algorithm has been cleaned up to handle a wider range
1149 ** of cases -- notably quoted and backslash escaped strings.
1150 ** This modification makes it substantially better at preserving
1151 ** the original syntax.
1154 ** addr -- the address to be cracked.
1155 ** e -- the current envelope.
1158 ** a pointer to the new version.
1164 ** The return value is saved in local storage and should
1165 ** be copied if it is to be reused.
1168 #define SM_HAVE_ROOM ((bp < buflim) && (buflim <= bufend))
1171 ** Append a character to bp if we have room.
1172 ** If not, punt and return $g.
1175 #define SM_APPEND_CHAR(c) \
1185 ERROR MAXNAME must be at least 10
1186 #endif /* MAXNAME < 10 */
1190 register char *addr;
1195 int cmtlev; /* comment level in input string */
1196 int realcmtlev; /* comment level in output string */
1197 int anglelev; /* angle level in input string */
1198 int copylev; /* 0 == in address, >0 copying */
1199 int bracklev; /* bracket level for IPv6 addr check */
1200 bool addangle; /* put closing angle in output */
1201 bool qmode; /* quoting in original string? */
1202 bool realqmode; /* quoting in output string? */
1203 bool putgmac = false; /* already wrote $g */
1204 bool quoteit = false; /* need to quote next character */
1205 bool gotangle = false; /* found first '<' */
1206 bool gotcolon = false; /* found a ':' */
1212 static char buf[MAXNAME + 1];
1215 sm_dprintf("crackaddr(%s)\n", addr);
1217 /* strip leading spaces */
1218 while (*addr != '\0' && isascii(*addr) && isspace(*addr))
1222 ** Start by assuming we have no angle brackets. This will be
1223 ** adjusted later if we find them.
1226 buflim = bufend = &buf[sizeof(buf) - 1];
1228 p = addrhead = addr;
1229 copylev = anglelev = cmtlev = realcmtlev = 0;
1231 qmode = realqmode = addangle = false;
1233 while ((c = *p++) != '\0')
1236 ** Try to keep legal syntax using spare buffer space
1237 ** (maintained by buflim).
1243 /* check for backslash escapes */
1246 /* arrange to quote the address */
1247 if (cmtlev <= 0 && !qmode)
1250 if ((c = *p++) == '\0')
1261 /* check for quoted strings */
1262 if (c == '"' && cmtlev <= 0)
1265 if (copylev > 0 && SM_HAVE_ROOM)
1271 realqmode = !realqmode;
1278 /* check for comments */
1283 /* allow space for closing paren */
1291 SM_APPEND_CHAR(' ');
1312 /* syntax error: unmatched ) */
1313 if (copylev > 0 && SM_HAVE_ROOM && bp > bufhead)
1317 /* count nesting on [ ... ] (for IPv6 domain literals) */
1323 /* check for group: list; syntax */
1324 if (c == ':' && anglelev <= 0 && bracklev <= 0 &&
1325 !gotcolon && !ColonOkInAddr)
1330 ** Check for DECnet phase IV ``::'' (host::user)
1331 ** or DECnet phase V ``:.'' syntaxes. The latter
1332 ** covers ``user@DEC:.tay.myhost'' and
1333 ** ``DEC:.tay.myhost::user'' syntaxes (bletch).
1336 if (*p == ':' || *p == '.')
1338 if (cmtlev <= 0 && !qmode)
1354 SM_APPEND_CHAR('"');
1356 /* back up over the ':' and any spaces */
1359 isascii(*--p) && isspace(*p))
1363 for (q = addrhead; q < p; )
1366 if (quoteit && c == '"')
1367 SM_APPEND_CHAR('\\');
1372 if (bp == &bufhead[1])
1375 SM_APPEND_CHAR('"');
1376 while ((c = *p++) != ':')
1381 /* any trailing white space is part of group: */
1382 while (isascii(*p) && isspace(*p))
1388 putgmac = quoteit = false;
1394 if (c == ';' && copylev <= 0 && !ColonOkInAddr)
1397 /* check for characters that may have to be quoted */
1398 if (strchr(MustQuoteChars, c) != NULL)
1401 ** If these occur as the phrase part of a <>
1402 ** construct, but are not inside of () or already
1403 ** quoted, they will have to be quoted. Note that
1404 ** now (but don't actually do the quoting).
1407 if (cmtlev <= 0 && !qmode)
1411 /* check for angle brackets */
1416 /* assume first of two angles is bogus */
1421 /* oops -- have to change our mind */
1433 SM_APPEND_CHAR('"');
1435 /* back up over the '<' and any spaces */
1438 isascii(*--p) && isspace(*p))
1442 for (q = addrhead; q < p; )
1445 if (quoteit && c == '"')
1447 SM_APPEND_CHAR('\\');
1458 SM_APPEND_CHAR('"');
1459 while ((c = *p++) != '<')
1464 putgmac = quoteit = false;
1480 else if (SM_HAVE_ROOM)
1482 /* syntax error: unmatched > */
1483 if (copylev > 0 && bp > bufhead)
1493 /* must be a real address character */
1495 if (copylev <= 0 && !putgmac)
1497 if (bp > buf && bp[-1] == ')')
1498 SM_APPEND_CHAR(' ');
1499 SM_APPEND_CHAR(MACROEXPAND);
1500 SM_APPEND_CHAR('g');
1505 /* repair any syntactic damage */
1506 if (realqmode && bp < bufend)
1508 while (realcmtlev-- > 0 && bp < bufend)
1510 if (addangle && bp < bufend)
1517 /* String too long, punt */
1519 buf[1] = MACROEXPAND;
1523 sm_syslog(LOG_ALERT, e->e_id,
1524 "Dropped invalid comments from header address");
1529 sm_dprintf("crackaddr=>`");
1530 xputs(sm_debug_file(), buf);
1536 ** PUTHEADER -- put the header part of a message from the in-core copy
1539 ** mci -- the connection information.
1540 ** hdr -- the header to put.
1541 ** e -- envelope to use.
1542 ** flags -- MIME conversion flags.
1552 putheader(mci, hdr, e, flags)
1555 register ENVELOPE *e;
1559 char buf[SM_MAX(MAXLINE,BUFSIZ)];
1563 sm_dprintf("--- putheader, mailer = %s ---\n",
1564 mci->mci_mailer->m_name);
1567 ** If we're in MIME mode, we're not really in the header of the
1568 ** message, just the header of one of the parts of the body of
1569 ** the message. Therefore MCIF_INHEADER should not be turned on.
1572 if (!bitset(MCIF_INMIME, mci->mci_flags))
1573 mci->mci_flags |= MCIF_INHEADER;
1575 for (h = hdr; h != NULL; h = h->h_link)
1577 register char *p = h->h_value;
1582 sm_dprintf(" %s: ", h->h_field);
1583 xputs(sm_debug_file(), p);
1586 /* Skip empty headers */
1587 if (h->h_value == NULL)
1590 /* heuristic shortening of MIME fields to avoid MUA overflows */
1591 if (MaxMimeFieldLength > 0 &&
1592 wordinclass(h->h_field,
1593 macid("{checkMIMEFieldHeaders}")))
1597 len = fix_mime_header(h, e);
1600 sm_syslog(LOG_ALERT, e->e_id,
1601 "Truncated MIME %s header due to field size (length = %ld) (possible attack)",
1602 h->h_field, (unsigned long) len);
1604 sm_dprintf(" truncated MIME %s header due to field size (length = %ld) (possible attack)\n",
1606 (unsigned long) len);
1610 if (MaxMimeHeaderLength > 0 &&
1611 wordinclass(h->h_field,
1612 macid("{checkMIMETextHeaders}")))
1616 len = strlen(h->h_value);
1617 if (len > (size_t) MaxMimeHeaderLength)
1619 h->h_value[MaxMimeHeaderLength - 1] = '\0';
1620 sm_syslog(LOG_ALERT, e->e_id,
1621 "Truncated long MIME %s header (length = %ld) (possible attack)",
1622 h->h_field, (unsigned long) len);
1624 sm_dprintf(" truncated long MIME %s header (length = %ld) (possible attack)\n",
1626 (unsigned long) len);
1630 if (MaxMimeHeaderLength > 0 &&
1631 wordinclass(h->h_field,
1632 macid("{checkMIMEHeaders}")))
1636 len = strlen(h->h_value);
1637 if (shorten_rfc822_string(h->h_value,
1638 MaxMimeHeaderLength))
1640 if (len < MaxMimeHeaderLength)
1642 /* we only rebalanced a bogus header */
1643 sm_syslog(LOG_ALERT, e->e_id,
1644 "Fixed MIME %s header (possible attack)",
1647 sm_dprintf(" fixed MIME %s header (possible attack)\n",
1652 /* we actually shortened header */
1653 sm_syslog(LOG_ALERT, e->e_id,
1654 "Truncated long MIME %s header (length = %ld) (possible attack)",
1656 (unsigned long) len);
1658 sm_dprintf(" truncated long MIME %s header (length = %ld) (possible attack)\n",
1660 (unsigned long) len);
1666 ** Suppress Content-Transfer-Encoding: if we are MIMEing
1667 ** and we are potentially converting from 8 bit to 7 bit
1668 ** MIME. If converting, add a new CTE header in
1672 if (bitset(H_CTE, h->h_flags) &&
1673 bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME,
1675 !bitset(M87F_NO8TO7, flags))
1678 sm_dprintf(" (skipped (content-transfer-encoding))\n");
1682 if (bitset(MCIF_INMIME, mci->mci_flags))
1686 put_vanilla_header(h, p, mci);
1690 if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
1691 !bitintersect(h->h_mflags, mci->mci_mailer->m_flags) &&
1692 (h->h_macro == '\0' ||
1693 (q = macvalue(bitidx(h->h_macro), e)) == NULL ||
1697 sm_dprintf(" (skipped)\n");
1701 /* handle Resent-... headers specially */
1702 if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
1705 sm_dprintf(" (skipped (resent))\n");
1709 /* suppress return receipts if requested */
1710 if (bitset(H_RECEIPTTO, h->h_flags) &&
1711 (RrtImpliesDsn || bitset(EF_NORECEIPT, e->e_flags)))
1714 sm_dprintf(" (skipped (receipt))\n");
1718 /* macro expand value if generated internally */
1719 if (bitset(H_DEFAULT, h->h_flags) ||
1720 bitset(H_BINDLATE, h->h_flags))
1722 expand(p, buf, sizeof buf, e);
1727 sm_dprintf(" (skipped -- null value)\n");
1732 if (bitset(H_BCC, h->h_flags))
1734 /* Bcc: field -- either truncate or delete */
1735 if (bitset(EF_DELETE_BCC, e->e_flags))
1738 sm_dprintf(" (skipped -- bcc)\n");
1742 /* no other recipient headers: truncate value */
1743 (void) sm_strlcpyn(obuf, sizeof obuf, 2,
1753 if (bitset(H_FROM|H_RCPT, h->h_flags))
1756 bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
1758 if (bitset(H_FROM, h->h_flags))
1760 commaize(h, p, oldstyle, mci, e);
1764 put_vanilla_header(h, p, mci);
1769 ** If we are converting this to a MIME message, add the
1770 ** MIME headers (but not in MIME mode!).
1774 if (bitset(MM_MIME8BIT, MimeMode) &&
1775 bitset(EF_HAS8BIT, e->e_flags) &&
1776 !bitset(EF_DONT_MIME, e->e_flags) &&
1777 !bitnset(M_8BITS, mci->mci_mailer->m_flags) &&
1778 !bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME, mci->mci_flags) &&
1779 hvalue("MIME-Version", e->e_header) == NULL)
1781 putline("MIME-Version: 1.0", mci);
1782 if (hvalue("Content-Type", e->e_header) == NULL)
1784 (void) sm_snprintf(obuf, sizeof obuf,
1785 "Content-Type: text/plain; charset=%s",
1789 if (hvalue("Content-Transfer-Encoding", e->e_header) == NULL)
1790 putline("Content-Transfer-Encoding: 8bit", mci);
1792 #endif /* MIME8TO7 */
1795 ** PUT_VANILLA_HEADER -- output a fairly ordinary header
1798 ** h -- the structure describing this header
1799 ** v -- the value of this header
1800 ** mci -- the connection info for output
1807 put_vanilla_header(h, v, mci)
1815 char obuf[MAXLINE + 256]; /* additional length for h_field */
1817 putflags = PXLF_HEADER;
1818 if (bitnset(M_7BITHDRS, mci->mci_mailer->m_flags))
1819 putflags |= PXLF_STRIP8BIT;
1820 (void) sm_snprintf(obuf, sizeof obuf, "%.200s: ", h->h_field);
1821 obp = obuf + strlen(obuf);
1822 while ((nlp = strchr(v, '\n')) != NULL)
1829 ** XXX This is broken for SPACELEFT()==0
1830 ** However, SPACELEFT() is always > 0 unless MAXLINE==1.
1833 if (SPACELEFT(obuf, obp) - 1 < (size_t) l)
1834 l = SPACELEFT(obuf, obp) - 1;
1836 (void) sm_snprintf(obp, SPACELEFT(obuf, obp), "%.*s", l, v);
1837 putxline(obuf, strlen(obuf), mci, putflags);
1840 if (*v != ' ' && *v != '\t')
1844 /* XXX This is broken for SPACELEFT()==0 */
1845 (void) sm_snprintf(obp, SPACELEFT(obuf, obp), "%.*s",
1846 (int) (SPACELEFT(obuf, obp) - 1), v);
1847 putxline(obuf, strlen(obuf), mci, putflags);
1850 ** COMMAIZE -- output a header field, making a comma-translated list.
1853 ** h -- the header field to output.
1854 ** p -- the value to put in it.
1855 ** oldstyle -- true if this is an old style header.
1856 ** mci -- the connection information.
1857 ** e -- the envelope containing the message.
1863 ** outputs "p" to file "fp".
1867 commaize(h, p, oldstyle, mci, e)
1872 register ENVELOPE *e;
1877 bool firstone = true;
1878 int putflags = PXLF_HEADER;
1880 char obuf[MAXLINE + 3];
1883 ** Output the address list translated by the
1884 ** mailer and with commas.
1888 sm_dprintf("commaize(%s: %s)\n", h->h_field, p);
1890 if (bitnset(M_7BITHDRS, mci->mci_mailer->m_flags))
1891 putflags |= PXLF_STRIP8BIT;
1894 (void) sm_snprintf(obp, SPACELEFT(obuf, obp), "%.200s: ",
1897 /* opos = strlen(obp); */
1898 opos = strlen(h->h_field) + 2;
1902 omax = mci->mci_mailer->m_linelimit - 2;
1903 if (omax < 0 || omax > 78)
1907 ** Run through the list of values.
1912 register char *name;
1919 ** Find the end of the name. New style names
1920 ** end with a comma, old style names end with
1921 ** a space character. However, spaces do not
1922 ** necessarily delimit an old-style name -- at
1923 ** signs mean keep going.
1926 /* find end of name */
1927 while ((isascii(*p) && isspace(*p)) || *p == ',')
1934 char pvpbuf[PSBUFSIZE];
1936 res = prescan(p, oldstyle ? ' ' : ',', pvpbuf,
1937 sizeof pvpbuf, &oldp, NULL, false);
1939 #if _FFR_IGNORE_BOGUS_ADDR
1940 /* ignore addresses that can't be parsed */
1946 #endif /* _FFR_IGNORE_BOGUS_ADDR */
1948 /* look to see if we have an at sign */
1949 while (*p != '\0' && isascii(*p) && isspace(*p))
1958 while (*p != '\0' && isascii(*p) && isspace(*p))
1961 /* at the end of one complete name */
1963 /* strip off trailing white space */
1965 ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
1971 ** if prescan() failed go a bit backwards; this is a hack,
1972 ** there should be some better error recovery.
1975 if (res == NULL && p > name &&
1976 !((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
1981 /* translate the name to be relative */
1982 flags = RF_HEADERADDR|RF_ADDDOMAIN;
1983 if (bitset(H_FROM, h->h_flags))
1984 flags |= RF_SENDERADDR;
1986 else if (e->e_from.q_mailer != NULL &&
1987 bitnset(M_UDBRECIPIENT, e->e_from.q_mailer->m_flags))
1991 q = udbsender(name, e->e_rpool);
1997 name = remotename(name, mci->mci_mailer, flags, &status, e);
2003 name = denlstring(name, false, true);
2006 ** record data progress so DNS timeouts
2007 ** don't cause DATA timeouts
2010 DataProgress = true;
2012 /* output the name with nice formatting */
2013 opos += strlen(name);
2016 if (opos > omax && !firstone)
2018 (void) sm_strlcpy(obp, ",\n", SPACELEFT(obuf, obp));
2019 putxline(obuf, strlen(obuf), mci, putflags);
2021 (void) sm_strlcpy(obp, " ", sizeof obuf);
2024 opos += strlen(name);
2028 (void) sm_strlcpy(obp, ", ", SPACELEFT(obuf, obp));
2032 while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
2037 if (obp < &obuf[sizeof obuf])
2040 obuf[sizeof obuf - 1] = '\0';
2041 putxline(obuf, strlen(obuf), mci, putflags);
2044 ** COPYHEADER -- copy header list
2046 ** This routine is the equivalent of newstr for header lists
2049 ** header -- list of header structures to copy.
2050 ** rpool -- resource pool, or NULL
2053 ** a copy of 'header'.
2060 copyheader(header, rpool)
2061 register HDR *header;
2064 register HDR *newhdr;
2066 register HDR **tail = &ret;
2068 while (header != NULL)
2070 newhdr = (HDR *) sm_rpool_malloc_x(rpool, sizeof *newhdr);
2071 STRUCTCOPY(*header, *newhdr);
2073 tail = &newhdr->h_link;
2074 header = header->h_link;
2081 ** FIX_MIME_HEADER -- possibly truncate/rebalance parameters in a MIME header
2083 ** Run through all of the parameters of a MIME header and
2084 ** possibly truncate and rebalance the parameter according
2085 ** to MaxMimeFieldLength.
2088 ** h -- the header to truncate/rebalance
2089 ** e -- the current envelope
2092 ** length of last offending field, 0 if all ok.
2095 ** string modified in place
2099 fix_mime_header(h, e)
2103 char *begin = h->h_value;
2108 if (begin == NULL || *begin == '\0')
2111 /* Split on each ';' */
2112 /* find_character() never returns NULL */
2113 while ((end = find_character(begin, ';')) != NULL)
2120 len = strlen(begin);
2122 /* Shorten individual parameter */
2123 if (shorten_rfc822_string(begin, MaxMimeFieldLength))
2125 if (len < MaxMimeFieldLength)
2127 /* we only rebalanced a bogus field */
2128 sm_syslog(LOG_ALERT, e->e_id,
2129 "Fixed MIME %s header field (possible attack)",
2132 sm_dprintf(" fixed MIME %s header field (possible attack)\n",
2137 /* we actually shortened the header */
2142 /* Collapse the possibly shortened string with rest */
2143 bp = begin + strlen(begin);
2151 /* copy character by character due to overlap */