2 * Copyright (c) 1998-2004, 2006, 2007 Proofpoint, 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.
15 #include <sm/sendmail.h>
17 SM_RCSID("@(#)$Id: headers.c,v 8.320 2013-11-22 20:51:55 ca Exp $")
19 static HDR *allocheader __P((char *, char *, int, SM_RPOOL_T *, bool));
20 static size_t fix_mime_header __P((HDR *, ENVELOPE *));
21 static int priencode __P((char *));
22 static bool 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;
49 ** DOCHOMPHEADER -- process and save a header line.
51 ** Called by chompheader.
54 ** line -- header as a text line.
55 ** pflag -- flags for chompheader() (from sendmail.h)
56 ** hdrp -- a pointer to the place to save the header.
57 ** e -- the envelope including this header.
60 ** flags for this header.
63 ** The header is saved on the header list.
64 ** Contents of 'line' are destroyed.
67 static struct hdrinfo NormalHeader = { NULL, 0, NULL };
68 static unsigned long dochompheader __P((char *, int, HDR **, ENVELOPE *));
71 dochompheader(line, pflag, hdrp, e)
77 unsigned char mid = '\0';
88 bool nullheader = false;
91 headeronly = hdrp != NULL;
95 /* strip off options */
98 if (!bitset(pflag, CHHDR_USER) && *p == '?')
103 q = strchr(++p, '?');
110 /* possibly macro conditional */
111 if (c == MACROEXPAND)
120 mid = (unsigned char) *p++;
138 mid = (unsigned char) macid(p);
139 if (bitset(0200, mid))
141 p += strlen(macname(mid)) + 2;
164 setbitn(bitidx(*p), mopts);
172 /* find canonical name */
174 while (isascii(*p) && isgraph(*p) && *p != ':')
177 while (isascii(*p) && isspace(*p))
179 if (*p++ != ':' || fname == fvalue)
182 syserr("553 5.3.0 header syntax error, line \"%s\"", line);
188 /* if the field is null, go ahead and use the default */
189 while (isascii(*p) && isspace(*p))
194 /* security scan: long field names are end-of-header */
195 if (strlen(fname) > 100)
198 /* check to see if it represents a ruleset call */
199 if (bitset(pflag, CHHDR_DEF))
203 (void) expand(fvalue, hbuf, sizeof(hbuf), e);
204 for (p = hbuf; isascii(*p) && isspace(*p); )
206 if ((*p++ & 0377) == CALLSUBR)
211 strc = *p == '+'; /* strip comments? */
214 if (strtorwset(p, &endp, ST_ENTER) > 0)
217 s = stab(fname, ST_HEADER, ST_ENTER);
219 s->s_header.hi_ruleset != NULL)
220 sm_syslog(LOG_WARNING, NOQID,
221 "Warning: redefined ruleset for header=%s, old=%s, new=%s",
223 s->s_header.hi_ruleset, p);
224 s->s_header.hi_ruleset = newstr(p);
226 s->s_header.hi_flags |= H_STRIPCOMM;
232 /* see if it is a known type */
233 s = stab(fname, ST_HEADER, ST_FIND);
242 sm_dprintf("no header flags match\n");
244 sm_dprintf("header match, flags=%lx, ruleset=%s\n",
246 hi->hi_ruleset == NULL ? "<NULL>"
250 /* see if this is a resent message */
251 if (!bitset(pflag, CHHDR_DEF) && !headeronly &&
252 bitset(H_RESENT, hi->hi_flags))
253 e->e_flags |= EF_RESENT;
255 /* if this is an Errors-To: header keep track of it now */
256 if (UseErrorsTo && !bitset(pflag, CHHDR_DEF) && !headeronly &&
257 bitset(H_ERRORSTO, hi->hi_flags))
258 (void) sendtolist(fvalue, NULLADDR, &e->e_errorqueue, 0, e);
260 /* if this means "end of header" quit now */
261 if (!headeronly && bitset(H_EOH, hi->hi_flags))
265 ** Horrible hack to work around problem with Lotus Notes SMTP
266 ** mail gateway, which generates From: headers with newlines in
267 ** them and the <address> on the second line. Although this is
268 ** legal RFC 822, many MUAs don't handle this properly and thus
269 ** never find the actual address.
272 if (bitset(H_FROM, hi->hi_flags) && SingleLineFromHeader)
274 while ((p = strchr(fvalue, '\n')) != NULL)
279 ** If there is a check ruleset, verify it against the header.
282 if (bitset(pflag, CHHDR_CHECK))
287 rscheckflags = RSF_COUNT;
288 if (!bitset(hi->hi_flags, H_FROM|H_RCPT))
289 rscheckflags |= RSF_UNSTRUCTURED;
291 /* no ruleset? look for default */
295 s = stab("*", ST_HEADER, ST_FIND);
298 rs = (&s->s_header)->hi_ruleset;
299 if (bitset((&s->s_header)->hi_flags,
301 rscheckflags |= RSF_RMCOMM;
304 else if (bitset(hi->hi_flags, H_STRIPCOMM))
305 rscheckflags |= RSF_RMCOMM;
314 /* - 3 to avoid problems with " at the end */
315 /* should be sizeof(qval), not MAXNAME */
316 for (k = 0; fvalue[k] != '\0' && l < MAXNAME - 3; k++)
320 /* XXX other control chars? */
321 case '\011': /* ht */
322 case '\012': /* nl */
323 case '\013': /* vt */
324 case '\014': /* np */
325 case '\015': /* cr */
332 qval[l++] = fvalue[k];
338 k += strlen(fvalue + k);
342 sm_syslog(LOG_WARNING, e->e_id,
343 "Warning: truncated header '%s' before check with '%s' len=%d max=%d",
344 fname, rs, k, MAXNAME - 1);
346 macdefine(&e->e_macro, A_TEMP,
347 macid("{currHeader}"), qval);
348 macdefine(&e->e_macro, A_TEMP,
349 macid("{hdr_name}"), fname);
351 (void) sm_snprintf(qval, sizeof(qval), "%d", k);
352 macdefine(&e->e_macro, A_TEMP, macid("{hdrlen}"), qval);
353 if (bitset(H_FROM, hi->hi_flags))
354 macdefine(&e->e_macro, A_PERM,
355 macid("{addr_type}"), "h s");
356 else if (bitset(H_RCPT, hi->hi_flags))
357 macdefine(&e->e_macro, A_PERM,
358 macid("{addr_type}"), "h r");
360 macdefine(&e->e_macro, A_PERM,
361 macid("{addr_type}"), "h");
362 (void) rscheck(rs, fvalue, NULL, e, rscheckflags, 3,
363 NULL, e->e_id, NULL, NULL);
368 ** Drop explicit From: if same as what we would generate.
369 ** This is to make MH (which doesn't always give a full name)
370 ** insert the full name information in all circumstances.
375 if (!bitset(EF_RESENT, e->e_flags))
377 if (!bitset(pflag, CHHDR_DEF) && !headeronly &&
378 !bitset(EF_QUEUERUN, e->e_flags) && sm_strcasecmp(fname, p) == 0)
380 if (e->e_from.q_paddr != NULL &&
381 e->e_from.q_mailer != NULL &&
382 bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) &&
383 (strcmp(fvalue, e->e_from.q_paddr) == 0 ||
384 strcmp(fvalue, e->e_from.q_user) == 0))
388 sm_dprintf("comparing header from (%s) against default (%s or %s), drop=%d\n",
389 fvalue, e->e_from.q_paddr, e->e_from.q_user,
394 /* delete default value for this header */
395 for (hp = hdrp; (h = *hp) != NULL; hp = &h->h_link)
397 if (sm_strcasecmp(fname, h->h_field) == 0 &&
398 !bitset(H_USER, h->h_flags) &&
399 !bitset(H_FORCE, h->h_flags))
403 /* user-supplied value was null */
408 /* make this look like the user entered it */
409 h->h_flags |= H_USER;
412 ** If the MH hack is selected, allow to turn
413 ** it off via a mailer flag to avoid problems
414 ** with setups that remove the F flag from
418 if (bitnset(M_NOMHHACK,
419 e->e_from.q_mailer->m_flags))
421 h->h_flags &= ~H_CHECK;
428 /* copy conditions from default case */
429 memmove((char *) mopts, (char *) h->h_mflags,
436 /* create a new node */
437 h = (HDR *) sm_rpool_malloc_x(e->e_rpool, sizeof(*h));
438 h->h_field = sm_rpool_strdup_x(e->e_rpool, fname);
439 h->h_value = sm_rpool_strdup_x(e->e_rpool, fvalue);
441 memmove((char *) h->h_mflags, (char *) mopts, sizeof(mopts));
444 h->h_flags = hi->hi_flags;
445 if (bitset(pflag, CHHDR_USER) || bitset(pflag, CHHDR_QUEUE))
446 h->h_flags |= H_USER;
448 /* strip EOH flag if parsing MIME headers */
450 h->h_flags &= ~H_EOH;
451 if (bitset(pflag, CHHDR_DEF))
452 h->h_flags |= H_DEFAULT;
453 if (cond || mid != '\0')
454 h->h_flags |= H_CHECK;
456 /* hack to see if this is a new format message */
457 if (!bitset(pflag, CHHDR_DEF) && !headeronly &&
458 bitset(H_RCPT|H_FROM, h->h_flags) &&
459 (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
460 strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
462 e->e_flags &= ~EF_OLDSTYLE;
469 ** CHOMPHEADER -- process and save a header line.
471 ** Called by collect, readcf, and readqf to deal with header lines.
472 ** This is just a wrapper for dochompheader().
475 ** line -- header as a text line.
476 ** pflag -- flags for chompheader() (from sendmail.h)
477 ** hdrp -- a pointer to the place to save the header.
478 ** e -- the envelope including this header.
481 ** flags for this header.
484 ** The header is saved on the header list.
485 ** Contents of 'line' are destroyed.
490 chompheader(line, pflag, hdrp, e)
494 register ENVELOPE *e;
500 sm_dprintf("chompheader: ");
501 xputs(sm_debug_file(), line);
505 /* quote this if user (not config file) input */
506 if (bitset(pflag, CHHDR_USER))
512 xbufs = sizeof(xbuf);
513 xbp = quote_internal_chars(line, xbuf, &xbufs);
516 sm_dprintf("chompheader: quoted: ");
517 xputs(sm_debug_file(), xbp);
520 rval = dochompheader(xbp, pflag, hdrp, e);
525 rval = dochompheader(line, pflag, hdrp, e);
531 ** ALLOCHEADER -- allocate a header entry
534 ** field -- the name of the header field (will not be copied).
535 ** value -- the value of the field (will be copied).
536 ** flags -- flags to add to h_flags.
537 ** rp -- resource pool for allocations
538 ** space -- add leading space?
541 ** Pointer to a newly allocated and populated HDR.
544 ** o field and value must be in internal format, i.e.,
545 ** metacharacters must be "quoted", see quote_internal_chars().
546 ** o maybe add more flags to decide:
547 ** - what to copy (field/value)
548 ** - whether to convert value to an internal format
552 allocheader(field, value, flags, rp, space)
562 /* find info struct */
563 s = stab(field, ST_HEADER, ST_FIND);
565 /* allocate space for new header */
566 h = (HDR *) sm_rpool_malloc_x(rp, sizeof(*h));
574 SM_ASSERT(l + 2 > l);
575 n = sm_rpool_malloc_x(rp, l + 2);
578 sm_strlcpy(n + 1, value, l + 1);
582 h->h_value = sm_rpool_strdup_x(rp, value);
585 h->h_flags |= s->s_header.hi_flags;
586 clrbitmap(h->h_mflags);
593 ** ADDHEADER -- add a header entry to the end of the queue.
595 ** This bypasses the special checking of chompheader.
598 ** field -- the name of the header field (will not be copied).
599 ** value -- the value of the field (will be copied).
600 ** flags -- flags to add to h_flags.
602 ** space -- add leading space?
608 ** adds the field on the list of headers for this envelope.
610 ** Notes: field and value must be in internal format, i.e.,
611 ** metacharacters must be "quoted", see quote_internal_chars().
615 addheader(field, value, flags, e, space)
624 HDR **hdrlist = &e->e_header;
626 /* find current place in list -- keep back pointer? */
627 for (hp = hdrlist; (h = *hp) != NULL; hp = &h->h_link)
629 if (sm_strcasecmp(field, h->h_field) == 0)
633 /* allocate space for new header */
634 h = allocheader(field, value, flags, e->e_rpool, space);
640 ** INSHEADER -- insert a header entry at the specified index
641 ** This bypasses the special checking of chompheader.
644 ** idx -- index into the header list at which to insert
645 ** field -- the name of the header field (will be copied).
646 ** value -- the value of the field (will be copied).
647 ** flags -- flags to add to h_flags.
649 ** space -- add leading space?
655 ** inserts the field on the list of headers for this envelope.
658 ** - field and value must be in internal format, i.e.,
659 ** metacharacters must be "quoted", see quote_internal_chars().
660 ** - the header list contains headers that might not be
661 ** sent "out" (see putheader(): "skip"), hence there is no
662 ** reliable way to insert a header at an exact position
663 ** (except at the front or end).
667 insheader(idx, field, value, flags, e, space)
675 HDR *h, *srch, *last = NULL;
677 /* allocate space for new header */
678 h = allocheader(field, value, flags, e->e_rpool, space);
680 /* find insertion position */
681 for (srch = e->e_header; srch != NULL && idx > 0;
682 srch = srch->h_link, idx--)
685 if (e->e_header == NULL)
690 else if (srch == NULL)
692 SM_ASSERT(last != NULL);
698 h->h_link = srch->h_link;
704 ** HVALUE -- return value of a header.
706 ** Only "real" fields (i.e., ones that have not been supplied
707 ** as a default) are used.
710 ** field -- the field name.
711 ** header -- the header list.
714 ** pointer to the value part (internal format).
715 ** NULL if not found.
722 hvalue(field, header)
728 for (h = header; h != NULL; h = h->h_link)
730 if (!bitset(H_DEFAULT, h->h_flags) &&
731 sm_strcasecmp(h->h_field, field) == 0)
738 while (isascii(*s) && isspace(*s))
747 ** ISHEADER -- predicate telling if argument is a header.
749 ** A line is a header if it has a single word followed by
750 ** optional white space followed by a colon.
752 ** Header fields beginning with two dashes, although technically
753 ** permitted by RFC822, are automatically rejected in order
754 ** to make MIME work out. Without this we could have a technically
755 ** legal header such as ``--"foo:bar"'' that would also be a legal
759 ** h -- string to check for possible headerness.
762 ** true if h is a header.
776 if (s[0] == '-' && s[1] == '-')
779 while (*s > ' ' && *s != ':' && *s != '\0')
785 /* following technically violates RFC822 */
786 while (isascii(*s) && isspace(*s))
793 ** EATHEADER -- run through the stored header and extract info.
796 ** e -- the envelope to process.
797 ** full -- if set, do full processing (e.g., compute
798 ** message priority). This should not be set
799 ** when reading a queue file because some info
800 ** needed to compute the priority is wrong.
801 ** log -- call logsender()?
807 ** Sets a bunch of global variables from information
808 ** in the collected header.
812 eatheader(e, full, log)
813 register ENVELOPE *e;
823 ** Set up macros for possible expansion in headers.
826 macdefine(&e->e_macro, A_PERM, 'f', e->e_sender);
827 macdefine(&e->e_macro, A_PERM, 'g', e->e_sender);
828 if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0')
829 macdefine(&e->e_macro, A_PERM, 'u', e->e_origrcpt);
831 macdefine(&e->e_macro, A_PERM, 'u', NULL);
833 /* full name of from person */
834 p = hvalue("full-name", e->e_header);
837 if (!rfc822_string(p))
840 ** Quote a full name with special characters
841 ** as a comment so crackaddr() doesn't destroy
842 ** the name portion of the address.
845 p = addquotes(p, e->e_rpool);
847 macdefine(&e->e_macro, A_PERM, 'x', p);
851 sm_dprintf("----- collected header -----\n");
853 for (h = e->e_header; h != NULL; h = h->h_link)
856 sm_dprintf("%s:", h->h_field);
857 if (h->h_value == NULL)
860 sm_dprintf("<NULL>\n");
864 /* do early binding */
865 if (bitset(H_DEFAULT, h->h_flags) &&
866 !bitset(H_BINDLATE, h->h_flags))
871 xputs(sm_debug_file(), h->h_value);
874 expand(h->h_value, buf, sizeof(buf), e);
875 if (buf[0] != '\0' &&
876 (buf[0] != ' ' || buf[1] != '\0'))
878 if (bitset(H_FROM, h->h_flags))
879 expand(crackaddr(buf, e),
880 buf, sizeof(buf), e);
881 h->h_value = sm_rpool_strdup_x(e->e_rpool, buf);
882 h->h_flags &= ~H_DEFAULT;
887 xputs(sm_debug_file(), h->h_value);
891 /* count the number of times it has been processed */
892 if (bitset(H_TRACE, h->h_flags))
895 /* send to this person if we so desire */
896 if (GrabTo && bitset(H_RCPT, h->h_flags) &&
897 !bitset(H_DEFAULT, h->h_flags) &&
898 (!bitset(EF_RESENT, e->e_flags) ||
899 bitset(H_RESENT, h->h_flags)))
902 int saveflags = e->e_flags;
905 (void) sendtolist(denlstring(h->h_value, true, false),
906 NULLADDR, &e->e_sendqueue, 0, e);
910 ** Change functionality so a fatal error on an
911 ** address doesn't affect the entire envelope.
914 /* delete fatal errors generated by this address */
915 if (!bitset(EF_FATALERRS, saveflags))
916 e->e_flags &= ~EF_FATALERRS;
920 /* save the message-id for logging */
921 p = "resent-message-id";
922 if (!bitset(EF_RESENT, e->e_flags))
924 if (sm_strcasecmp(h->h_field, p) == 0)
926 e->e_msgid = h->h_value;
927 while (isascii(*e->e_msgid) && isspace(*e->e_msgid))
929 macdefine(&e->e_macro, A_PERM, macid("{msg_id}"),
934 sm_dprintf("----------------------------\n");
936 /* if we are just verifying (that is, sendmail -t -bv), drop out now */
937 if (OpMode == MD_VERIFY)
940 /* store hop count */
941 if (hopcnt > e->e_hopcount)
943 e->e_hopcount = hopcnt;
944 (void) sm_snprintf(buf, sizeof(buf), "%d", e->e_hopcount);
945 macdefine(&e->e_macro, A_TEMP, 'c', buf);
948 /* message priority */
949 p = hvalue("precedence", e->e_header);
951 e->e_class = priencode(p);
953 e->e_timeoutclass = TOC_NONURGENT;
954 else if (e->e_class > 0)
955 e->e_timeoutclass = TOC_URGENT;
958 e->e_msgpriority = e->e_msgsize
959 - e->e_class * WkClassFact
960 + e->e_nrcpts * WkRecipFact;
963 /* check for DSN to properly set e_timeoutclass */
964 p = hvalue("content-type", e->e_header);
969 char pvpbuf[MAXLINE];
970 extern unsigned char MimeTokenTab[256];
972 /* tokenize header */
975 pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
976 MimeTokenTab, false);
979 /* Check if multipart/report */
980 if (pvp != NULL && pvp[0] != NULL &&
981 pvp[1] != NULL && pvp[2] != NULL &&
982 sm_strcasecmp(*pvp++, "multipart") == 0 &&
983 strcmp(*pvp++, "/") == 0 &&
984 sm_strcasecmp(*pvp++, "report") == 0)
986 /* Look for report-type=delivery-status */
989 /* skip to semicolon separator */
990 while (*pvp != NULL && strcmp(*pvp, ";") != 0)
994 if (*pvp++ == NULL || *pvp == NULL)
997 /* look for report-type */
998 if (sm_strcasecmp(*pvp++, "report-type") != 0)
1002 if (*pvp == NULL || strcmp(*pvp, "=") != 0)
1006 if (*++pvp != NULL &&
1008 "delivery-status") == 0)
1009 e->e_timeoutclass = TOC_DSN;
1011 /* found report-type, no need to continue */
1017 /* message timeout priority */
1018 p = hvalue("priority", e->e_header);
1021 /* (this should be in the configuration file) */
1022 if (sm_strcasecmp(p, "urgent") == 0)
1023 e->e_timeoutclass = TOC_URGENT;
1024 else if (sm_strcasecmp(p, "normal") == 0)
1025 e->e_timeoutclass = TOC_NORMAL;
1026 else if (sm_strcasecmp(p, "non-urgent") == 0)
1027 e->e_timeoutclass = TOC_NONURGENT;
1028 else if (bitset(EF_RESPONSE, e->e_flags))
1029 e->e_timeoutclass = TOC_DSN;
1031 else if (bitset(EF_RESPONSE, e->e_flags))
1032 e->e_timeoutclass = TOC_DSN;
1034 /* date message originated */
1035 p = hvalue("posted-date", e->e_header);
1037 p = hvalue("date", e->e_header);
1039 macdefine(&e->e_macro, A_PERM, 'a', p);
1041 /* check to see if this is a MIME message */
1042 if ((e->e_bodytype != NULL &&
1043 sm_strcasecmp(e->e_bodytype, "8BITMIME") == 0) ||
1044 hvalue("MIME-Version", e->e_header) != NULL)
1046 e->e_flags |= EF_IS_MIME;
1048 e->e_bodytype = "8BITMIME";
1050 else if ((p = hvalue("Content-Type", e->e_header)) != NULL)
1052 /* this may be an RFC 1049 message */
1053 p = strpbrk(p, ";/");
1054 if (p == NULL || *p == ';')
1057 e->e_flags |= EF_DONT_MIME;
1062 ** From person in antiquated ARPANET mode
1063 ** required by UK Grey Book e-mail gateways (sigh)
1066 if (OpMode == MD_ARPAFTP)
1068 register struct hdrinfo *hi;
1070 for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1072 if (bitset(H_FROM, hi->hi_flags) &&
1073 (!bitset(H_RESENT, hi->hi_flags) ||
1074 bitset(EF_RESENT, e->e_flags)) &&
1075 (p = hvalue(hi->hi_field, e->e_header)) != NULL)
1078 if (hi->hi_field != NULL)
1081 sm_dprintf("eatheader: setsender(*%s == %s)\n",
1083 setsender(p, e, NULL, '\0', true);
1088 ** Log collection information.
1092 sm_dprintf("eatheader: e_id=%s, EF_LOGSENDER=%d, LogLevel=%d, log=%d\n",
1093 e->e_id, bitset(EF_LOGSENDER, e->e_flags), LogLevel,
1095 if (log && bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
1097 logsender(e, e->e_msgid);
1098 e->e_flags &= ~EF_LOGSENDER;
1103 ** LOGSENDER -- log sender information
1106 ** e -- the envelope to log
1107 ** msgid -- the message id
1115 register ENVELOPE *e;
1121 char hbuf[MAXNAME + 1];
1122 char sbuf[MAXLINE + 1];
1123 char mbuf[MAXNAME + 1];
1125 /* don't allow newlines in the message-id */
1126 /* XXX do we still need this? sm_syslog() replaces control chars */
1132 if (l > sizeof(mbuf) - 1)
1133 l = sizeof(mbuf) - 1;
1134 memmove(mbuf, msgid, l);
1137 while ((p = strchr(p, '\n')) != NULL)
1141 if (bitset(EF_RESPONSE, e->e_flags))
1142 name = "[RESPONSE]";
1143 else if ((name = macvalue('_', e)) != NULL)
1146 else if (RealHostName == NULL)
1148 else if (RealHostName[0] == '[')
1149 name = RealHostName;
1153 (void) sm_snprintf(hbuf, sizeof(hbuf), "%.80s", RealHostName);
1154 if (RealHostAddr.sa.sa_family != 0)
1156 p = &hbuf[strlen(hbuf)];
1157 (void) sm_snprintf(p, SPACELEFT(hbuf, p),
1159 anynet_ntoa(&RealHostAddr));
1163 /* some versions of syslog only take 5 printf args */
1164 #if (SYSLOG_BUFSIZE) >= 256
1166 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1167 "from=%.200s, size=%ld, class=%d, nrcpts=%d",
1168 e->e_from.q_paddr == NULL ? "<NONE>" : e->e_from.q_paddr,
1169 PRT_NONNEGL(e->e_msgsize), e->e_class, e->e_nrcpts);
1173 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1174 ", msgid=%.100s", mbuf);
1177 if (e->e_bodytype != NULL)
1179 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1180 ", bodytype=%.20s", e->e_bodytype);
1183 p = macvalue('r', e);
1186 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1187 ", proto=%.20s", p);
1190 p = macvalue(macid("{daemon_name}"), e);
1193 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1194 ", daemon=%.20s", p);
1199 p = macvalue(macid("{verify}"), e);
1200 if (p == NULL || *p == '\0')
1202 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp), ", tls_verify=%.20s", p);
1204 # endif /* STARTTLS */
1206 p = macvalue(macid("{auth_type}"), e);
1207 if (p == NULL || *p == '\0')
1209 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp), ", auth=%.20s", p);
1212 # endif /* _FFR_LOG_MORE1 */
1213 sm_syslog(LOG_INFO, e->e_id, "%.850s, relay=%s", sbuf, name);
1215 #else /* (SYSLOG_BUFSIZE) >= 256 */
1217 sm_syslog(LOG_INFO, e->e_id,
1219 e->e_from.q_paddr == NULL ? "<NONE>"
1220 : shortenstring(e->e_from.q_paddr,
1222 sm_syslog(LOG_INFO, e->e_id,
1223 "size=%ld, class=%ld, nrcpts=%d",
1224 PRT_NONNEGL(e->e_msgsize), e->e_class, e->e_nrcpts);
1226 sm_syslog(LOG_INFO, e->e_id,
1228 shortenstring(mbuf, 83));
1231 if (e->e_bodytype != NULL)
1233 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1234 "bodytype=%.20s, ", e->e_bodytype);
1237 p = macvalue('r', e);
1240 (void) sm_snprintf(sbp, SPACELEFT(sbuf, sbp),
1241 "proto=%.20s, ", p);
1244 sm_syslog(LOG_INFO, e->e_id,
1245 "%.400srelay=%s", sbuf, name);
1246 #endif /* (SYSLOG_BUFSIZE) >= 256 */
1250 ** PRIENCODE -- encode external priority names into internal values.
1253 ** p -- priority in ascii.
1256 ** priority as a numeric level.
1268 for (i = 0; i < NumPriorities; i++)
1270 if (sm_strcasecmp(p, Priorities[i].pri_name) == 0)
1271 return Priorities[i].pri_val;
1274 /* unknown priority */
1279 ** CRACKADDR -- parse an address and turn it into a macro
1281 ** This doesn't actually parse the address -- it just extracts
1282 ** it and replaces it with "$g". The parse is totally ad hoc
1283 ** and isn't even guaranteed to leave something syntactically
1284 ** identical to what it started with. However, it does leave
1285 ** something semantically identical if possible, else at least
1286 ** syntactically correct.
1288 ** For example, it changes "Real Name <real@example.com> (Comment)"
1289 ** to "Real Name <$g> (Comment)".
1291 ** This algorithm has been cleaned up to handle a wider range
1292 ** of cases -- notably quoted and backslash escaped strings.
1293 ** This modification makes it substantially better at preserving
1294 ** the original syntax.
1297 ** addr -- the address to be cracked.
1298 ** e -- the current envelope.
1301 ** a pointer to the new version.
1307 ** The return value is saved in local storage and should
1308 ** be copied if it is to be reused.
1311 #define SM_HAVE_ROOM ((bp < buflim) && (buflim <= bufend))
1314 ** Append a character to bp if we have room.
1315 ** If not, punt and return $g.
1318 #define SM_APPEND_CHAR(c) \
1328 ERROR MAXNAME must be at least 10
1329 #endif /* MAXNAME < 10 */
1333 register char *addr;
1338 int cmtlev; /* comment level in input string */
1339 int realcmtlev; /* comment level in output string */
1340 int anglelev; /* angle level in input string */
1341 int copylev; /* 0 == in address, >0 copying */
1342 int bracklev; /* bracket level for IPv6 addr check */
1343 bool addangle; /* put closing angle in output */
1344 bool qmode; /* quoting in original string? */
1345 bool realqmode; /* quoting in output string? */
1346 bool putgmac = false; /* already wrote $g */
1347 bool quoteit = false; /* need to quote next character */
1348 bool gotangle = false; /* found first '<' */
1349 bool gotcolon = false; /* found a ':' */
1355 static char buf[MAXNAME + 1];
1358 sm_dprintf("crackaddr(%s)\n", addr);
1360 buflim = bufend = &buf[sizeof(buf) - 1];
1363 /* skip over leading spaces but preserve them */
1364 while (*addr != '\0' && isascii(*addr) && isspace(*addr))
1366 SM_APPEND_CHAR(*addr);
1372 ** Start by assuming we have no angle brackets. This will be
1373 ** adjusted later if we find them.
1376 p = addrhead = addr;
1377 copylev = anglelev = cmtlev = realcmtlev = 0;
1379 qmode = realqmode = addangle = false;
1381 while ((c = *p++) != '\0')
1384 ** Try to keep legal syntax using spare buffer space
1385 ** (maintained by buflim).
1391 /* check for backslash escapes */
1394 /* arrange to quote the address */
1395 if (cmtlev <= 0 && !qmode)
1398 if ((c = *p++) == '\0')
1409 /* check for quoted strings */
1410 if (c == '"' && cmtlev <= 0)
1413 if (copylev > 0 && SM_HAVE_ROOM)
1419 realqmode = !realqmode;
1426 /* check for comments */
1431 /* allow space for closing paren */
1439 SM_APPEND_CHAR(' ');
1460 /* syntax error: unmatched ) */
1461 if (copylev > 0 && SM_HAVE_ROOM && bp > bufhead)
1465 /* count nesting on [ ... ] (for IPv6 domain literals) */
1471 /* check for group: list; syntax */
1472 if (c == ':' && anglelev <= 0 && bracklev <= 0 &&
1473 !gotcolon && !ColonOkInAddr)
1478 ** Check for DECnet phase IV ``::'' (host::user)
1479 ** or DECnet phase V ``:.'' syntaxes. The latter
1480 ** covers ``user@DEC:.tay.myhost'' and
1481 ** ``DEC:.tay.myhost::user'' syntaxes (bletch).
1484 if (*p == ':' || *p == '.')
1486 if (cmtlev <= 0 && !qmode)
1502 SM_APPEND_CHAR('"');
1504 /* back up over the ':' and any spaces */
1507 isascii(*--p) && isspace(*p))
1511 for (q = addrhead; q < p; )
1514 if (quoteit && c == '"')
1515 SM_APPEND_CHAR('\\');
1520 if (bp == &bufhead[1])
1523 SM_APPEND_CHAR('"');
1524 while ((c = *p++) != ':')
1529 /* any trailing white space is part of group: */
1530 while (isascii(*p) && isspace(*p))
1536 putgmac = quoteit = false;
1542 if (c == ';' && copylev <= 0 && !ColonOkInAddr)
1545 /* check for characters that may have to be quoted */
1546 if (strchr(MustQuoteChars, c) != NULL)
1549 ** If these occur as the phrase part of a <>
1550 ** construct, but are not inside of () or already
1551 ** quoted, they will have to be quoted. Note that
1552 ** now (but don't actually do the quoting).
1555 if (cmtlev <= 0 && !qmode)
1559 /* check for angle brackets */
1564 /* assume first of two angles is bogus */
1569 /* oops -- have to change our mind */
1581 SM_APPEND_CHAR('"');
1583 /* back up over the '<' and any spaces */
1586 isascii(*--p) && isspace(*p))
1590 for (q = addrhead; q < p; )
1593 if (quoteit && c == '"')
1595 SM_APPEND_CHAR('\\');
1606 SM_APPEND_CHAR('"');
1607 while ((c = *p++) != '<')
1612 putgmac = quoteit = false;
1628 else if (SM_HAVE_ROOM)
1630 /* syntax error: unmatched > */
1631 if (copylev > 0 && bp > bufhead)
1641 /* must be a real address character */
1643 if (copylev <= 0 && !putgmac)
1645 if (bp > buf && bp[-1] == ')')
1646 SM_APPEND_CHAR(' ');
1647 SM_APPEND_CHAR(MACROEXPAND);
1648 SM_APPEND_CHAR('g');
1653 /* repair any syntactic damage */
1654 if (realqmode && bp < bufend)
1656 while (realcmtlev-- > 0 && bp < bufend)
1658 if (addangle && bp < bufend)
1665 /* String too long, punt */
1667 buf[1] = MACROEXPAND;
1671 sm_syslog(LOG_ALERT, e->e_id,
1672 "Dropped invalid comments from header address");
1677 sm_dprintf("crackaddr=>`");
1678 xputs(sm_debug_file(), buf);
1685 ** PUTHEADER -- put the header part of a message from the in-core copy
1688 ** mci -- the connection information.
1689 ** hdr -- the header to put.
1690 ** e -- envelope to use.
1691 ** flags -- MIME conversion flags.
1694 ** true iff header part was written successfully
1701 putheader(mci, hdr, e, flags)
1704 register ENVELOPE *e;
1708 char buf[SM_MAX(MAXLINE,BUFSIZ)];
1712 sm_dprintf("--- putheader, mailer = %s ---\n",
1713 mci->mci_mailer->m_name);
1716 ** If we're in MIME mode, we're not really in the header of the
1717 ** message, just the header of one of the parts of the body of
1718 ** the message. Therefore MCIF_INHEADER should not be turned on.
1721 if (!bitset(MCIF_INMIME, mci->mci_flags))
1722 mci->mci_flags |= MCIF_INHEADER;
1724 for (h = hdr; h != NULL; h = h->h_link)
1726 register char *p = h->h_value;
1731 sm_dprintf(" %s:", h->h_field);
1732 xputs(sm_debug_file(), p);
1735 /* Skip empty headers */
1736 if (h->h_value == NULL)
1739 /* heuristic shortening of MIME fields to avoid MUA overflows */
1740 if (MaxMimeFieldLength > 0 &&
1741 wordinclass(h->h_field,
1742 macid("{checkMIMEFieldHeaders}")))
1746 len = fix_mime_header(h, e);
1749 sm_syslog(LOG_ALERT, e->e_id,
1750 "Truncated MIME %s header due to field size (length = %ld) (possible attack)",
1751 h->h_field, (unsigned long) len);
1753 sm_dprintf(" truncated MIME %s header due to field size (length = %ld) (possible attack)\n",
1755 (unsigned long) len);
1759 if (MaxMimeHeaderLength > 0 &&
1760 wordinclass(h->h_field,
1761 macid("{checkMIMETextHeaders}")))
1765 len = strlen(h->h_value);
1766 if (len > (size_t) MaxMimeHeaderLength)
1768 h->h_value[MaxMimeHeaderLength - 1] = '\0';
1769 sm_syslog(LOG_ALERT, e->e_id,
1770 "Truncated long MIME %s header (length = %ld) (possible attack)",
1771 h->h_field, (unsigned long) len);
1773 sm_dprintf(" truncated long MIME %s header (length = %ld) (possible attack)\n",
1775 (unsigned long) len);
1779 if (MaxMimeHeaderLength > 0 &&
1780 wordinclass(h->h_field,
1781 macid("{checkMIMEHeaders}")))
1785 len = strlen(h->h_value);
1786 if (shorten_rfc822_string(h->h_value,
1787 MaxMimeHeaderLength))
1789 if (len < MaxMimeHeaderLength)
1791 /* we only rebalanced a bogus header */
1792 sm_syslog(LOG_ALERT, e->e_id,
1793 "Fixed MIME %s header (possible attack)",
1796 sm_dprintf(" fixed MIME %s header (possible attack)\n",
1801 /* we actually shortened header */
1802 sm_syslog(LOG_ALERT, e->e_id,
1803 "Truncated long MIME %s header (length = %ld) (possible attack)",
1805 (unsigned long) len);
1807 sm_dprintf(" truncated long MIME %s header (length = %ld) (possible attack)\n",
1809 (unsigned long) len);
1815 ** Suppress Content-Transfer-Encoding: if we are MIMEing
1816 ** and we are potentially converting from 8 bit to 7 bit
1817 ** MIME. If converting, add a new CTE header in
1821 if (bitset(H_CTE, h->h_flags) &&
1822 bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME,
1824 !bitset(M87F_NO8TO7, flags))
1827 sm_dprintf(" (skipped (content-transfer-encoding))\n");
1831 if (bitset(MCIF_INMIME, mci->mci_flags))
1835 if (!put_vanilla_header(h, p, mci))
1840 if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
1841 !bitintersect(h->h_mflags, mci->mci_mailer->m_flags) &&
1842 (h->h_macro == '\0' ||
1843 (q = macvalue(bitidx(h->h_macro), e)) == NULL ||
1847 sm_dprintf(" (skipped)\n");
1851 /* handle Resent-... headers specially */
1852 if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
1855 sm_dprintf(" (skipped (resent))\n");
1859 /* suppress return receipts if requested */
1860 if (bitset(H_RECEIPTTO, h->h_flags) &&
1861 (RrtImpliesDsn || bitset(EF_NORECEIPT, e->e_flags)))
1864 sm_dprintf(" (skipped (receipt))\n");
1868 /* macro expand value if generated internally */
1869 if (bitset(H_DEFAULT, h->h_flags) ||
1870 bitset(H_BINDLATE, h->h_flags))
1872 expand(p, buf, sizeof(buf), e);
1877 sm_dprintf(" (skipped -- null value)\n");
1882 if (bitset(H_BCC, h->h_flags))
1884 /* Bcc: field -- either truncate or delete */
1885 if (bitset(EF_DELETE_BCC, e->e_flags))
1888 sm_dprintf(" (skipped -- bcc)\n");
1892 /* no other recipient headers: truncate value */
1893 (void) sm_strlcpyn(obuf, sizeof(obuf), 2,
1895 if (!putline(obuf, mci))
1904 if (bitset(H_FROM|H_RCPT, h->h_flags))
1907 bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
1909 if (bitset(H_FROM, h->h_flags))
1911 if (!commaize(h, p, oldstyle, mci, e,
1912 PXLF_HEADER | PXLF_STRIPMQUOTE)
1913 && bitnset(M_xSMTP, mci->mci_mailer->m_flags))
1918 if (!put_vanilla_header(h, p, mci))
1924 ** If we are converting this to a MIME message, add the
1925 ** MIME headers (but not in MIME mode!).
1929 if (bitset(MM_MIME8BIT, MimeMode) &&
1930 bitset(EF_HAS8BIT, e->e_flags) &&
1931 !bitset(EF_DONT_MIME, e->e_flags) &&
1932 !bitnset(M_8BITS, mci->mci_mailer->m_flags) &&
1933 !bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME, mci->mci_flags) &&
1934 hvalue("MIME-Version", e->e_header) == NULL)
1936 if (!putline("MIME-Version: 1.0", mci))
1938 if (hvalue("Content-Type", e->e_header) == NULL)
1940 (void) sm_snprintf(obuf, sizeof(obuf),
1941 "Content-Type: text/plain; charset=%s",
1943 if (!putline(obuf, mci))
1946 if (hvalue("Content-Transfer-Encoding", e->e_header) == NULL
1947 && !putline("Content-Transfer-Encoding: 8bit", mci))
1950 #endif /* MIME8TO7 */
1958 ** PUT_VANILLA_HEADER -- output a fairly ordinary header
1961 ** h -- the structure describing this header
1962 ** v -- the value of this header
1963 ** mci -- the connection info for output
1966 ** true iff header was written successfully
1970 put_vanilla_header(h, v, mci)
1978 char obuf[MAXLINE + 256]; /* additional length for h_field */
1980 putflags = PXLF_HEADER | PXLF_STRIPMQUOTE;
1981 if (bitnset(M_7BITHDRS, mci->mci_mailer->m_flags))
1982 putflags |= PXLF_STRIP8BIT;
1983 (void) sm_snprintf(obuf, sizeof(obuf), "%.200s:", h->h_field);
1984 obp = obuf + strlen(obuf);
1985 while ((nlp = strchr(v, '\n')) != NULL)
1992 ** XXX This is broken for SPACELEFT()==0
1993 ** However, SPACELEFT() is always > 0 unless MAXLINE==1.
1996 if (SPACELEFT(obuf, obp) - 1 < (size_t) l)
1997 l = SPACELEFT(obuf, obp) - 1;
1999 (void) sm_snprintf(obp, SPACELEFT(obuf, obp), "%.*s", l, v);
2000 if (!putxline(obuf, strlen(obuf), mci, putflags))
2004 if (*v != ' ' && *v != '\t')
2008 /* XXX This is broken for SPACELEFT()==0 */
2009 (void) sm_snprintf(obp, SPACELEFT(obuf, obp), "%.*s",
2010 (int) (SPACELEFT(obuf, obp) - 1), v);
2011 return putxline(obuf, strlen(obuf), mci, putflags);
2018 ** COMMAIZE -- output a header field, making a comma-translated list.
2021 ** h -- the header field to output.
2022 ** p -- the value to put in it.
2023 ** oldstyle -- true if this is an old style header.
2024 ** mci -- the connection information.
2025 ** e -- the envelope containing the message.
2026 ** putflags -- flags for putxline()
2029 ** true iff header field was written successfully
2032 ** outputs "p" to "mci".
2036 commaize(h, p, oldstyle, mci, e, putflags)
2041 register ENVELOPE *e;
2045 int opos, omax, spaces;
2046 bool firstone = true;
2048 char obuf[MAXLINE + 3];
2051 ** Output the address list translated by the
2052 ** mailer and with commas.
2056 sm_dprintf("commaize(%s:%s)\n", h->h_field, p);
2058 if (bitnset(M_7BITHDRS, mci->mci_mailer->m_flags))
2059 putflags |= PXLF_STRIP8BIT;
2062 (void) sm_snprintf(obp, SPACELEFT(obuf, obp), "%.200s:", h->h_field);
2063 /* opos = strlen(obp); instead of the next 3 lines? */
2064 opos = strlen(h->h_field) + 1;
2070 while (*p != '\0' && isascii(*p) && isspace(*p))
2077 SM_ASSERT(sizeof(obuf) > opos * 2);
2080 ** Restrict number of spaces to half the length of buffer
2081 ** so the header field body can be put in here too.
2082 ** Note: this is a hack...
2085 if (spaces > sizeof(obuf) / 2)
2086 spaces = sizeof(obuf) / 2;
2087 (void) sm_snprintf(obp, SPACELEFT(obuf, obp), "%*s", spaces,
2091 SM_ASSERT(obp < &obuf[MAXLINE]);
2094 omax = mci->mci_mailer->m_linelimit - 2;
2095 if (omax < 0 || omax > 78)
2099 ** Run through the list of values.
2104 register char *name;
2111 ** Find the end of the name. New style names
2112 ** end with a comma, old style names end with
2113 ** a space character. However, spaces do not
2114 ** necessarily delimit an old-style name -- at
2115 ** signs mean keep going.
2118 /* find end of name */
2119 while ((isascii(*p) && isspace(*p)) || *p == ',')
2126 char pvpbuf[PSBUFSIZE];
2128 res = prescan(p, oldstyle ? ' ' : ',', pvpbuf,
2129 sizeof(pvpbuf), &oldp, ExtTokenTab, false);
2131 #if _FFR_IGNORE_BOGUS_ADDR
2132 /* ignore addresses that can't be parsed */
2138 #endif /* _FFR_IGNORE_BOGUS_ADDR */
2140 /* look to see if we have an at sign */
2141 while (*p != '\0' && isascii(*p) && isspace(*p))
2150 while (*p != '\0' && isascii(*p) && isspace(*p))
2153 /* at the end of one complete name */
2155 /* strip off trailing white space */
2157 ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
2163 ** if prescan() failed go a bit backwards; this is a hack,
2164 ** there should be some better error recovery.
2167 if (res == NULL && p > name &&
2168 !((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
2173 /* translate the name to be relative */
2174 flags = RF_HEADERADDR|RF_ADDDOMAIN;
2175 if (bitset(H_FROM, h->h_flags))
2176 flags |= RF_SENDERADDR;
2178 else if (e->e_from.q_mailer != NULL &&
2179 bitnset(M_UDBRECIPIENT, e->e_from.q_mailer->m_flags))
2183 q = udbsender(name, e->e_rpool);
2189 name = remotename(name, mci->mci_mailer, flags, &status, e);
2190 if (status != EX_OK && bitnset(M_xSMTP, mci->mci_mailer->m_flags))
2192 if (status == EX_TEMPFAIL)
2193 mci->mci_flags |= MCIF_NOTSTICKY;
2201 name = denlstring(name, false, true);
2203 /* output the name with nice formatting */
2204 opos += strlen(name);
2207 if (opos > omax && !firstone)
2209 (void) sm_strlcpy(obp, ",\n", SPACELEFT(obuf, obp));
2210 if (!putxline(obuf, strlen(obuf), mci, putflags))
2213 (void) sm_strlcpy(obp, " ", sizeof(obuf));
2216 opos += strlen(name);
2220 (void) sm_strlcpy(obp, ", ", SPACELEFT(obuf, obp));
2224 while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
2229 if (obp < &obuf[sizeof(obuf)])
2232 obuf[sizeof(obuf) - 1] = '\0';
2233 return putxline(obuf, strlen(obuf), mci, putflags);
2240 ** COPYHEADER -- copy header list
2242 ** This routine is the equivalent of newstr for header lists
2245 ** header -- list of header structures to copy.
2246 ** rpool -- resource pool, or NULL
2249 ** a copy of 'header'.
2256 copyheader(header, rpool)
2257 register HDR *header;
2260 register HDR *newhdr;
2262 register HDR **tail = &ret;
2264 while (header != NULL)
2266 newhdr = (HDR *) sm_rpool_malloc_x(rpool, sizeof(*newhdr));
2267 STRUCTCOPY(*header, *newhdr);
2269 tail = &newhdr->h_link;
2270 header = header->h_link;
2278 ** FIX_MIME_HEADER -- possibly truncate/rebalance parameters in a MIME header
2280 ** Run through all of the parameters of a MIME header and
2281 ** possibly truncate and rebalance the parameter according
2282 ** to MaxMimeFieldLength.
2285 ** h -- the header to truncate/rebalance
2286 ** e -- the current envelope
2289 ** length of last offending field, 0 if all ok.
2292 ** string modified in place
2296 fix_mime_header(h, e)
2300 char *begin = h->h_value;
2305 if (begin == NULL || *begin == '\0')
2308 /* Split on each ';' */
2309 /* find_character() never returns NULL */
2310 while ((end = find_character(begin, ';')) != NULL)
2317 len = strlen(begin);
2319 /* Shorten individual parameter */
2320 if (shorten_rfc822_string(begin, MaxMimeFieldLength))
2322 if (len < MaxMimeFieldLength)
2324 /* we only rebalanced a bogus field */
2325 sm_syslog(LOG_ALERT, e->e_id,
2326 "Fixed MIME %s header field (possible attack)",
2329 sm_dprintf(" fixed MIME %s header field (possible attack)\n",
2334 /* we actually shortened the header */
2339 /* Collapse the possibly shortened string with rest */
2340 bp = begin + strlen(begin);
2348 /* copy character by character due to overlap */