122706Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 362525Sbostic * Copyright (c) 1988, 1993 462525Sbostic * The Regents of the University of California. All rights reserved. 533729Sbostic * 642826Sbostic * %sccs.include.redist.c% 733729Sbostic */ 822706Sdist 922706Sdist #ifndef lint 10*65059Seric static char sccsid[] = "@(#)headers.c 8.16 (Berkeley) 12/10/93"; 1133729Sbostic #endif /* not lint */ 1222706Sdist 134091Seric # include <errno.h> 144091Seric # include "sendmail.h" 154091Seric 164091Seric /* 174091Seric ** CHOMPHEADER -- process and save a header line. 184091Seric ** 194091Seric ** Called by collect and by readcf to deal with header lines. 204091Seric ** 214091Seric ** Parameters: 224091Seric ** line -- header as a text line. 234091Seric ** def -- if set, this is a default value. 2455012Seric ** e -- the envelope including this header. 254091Seric ** 264091Seric ** Returns: 274091Seric ** flags for this header. 284091Seric ** 294091Seric ** Side Effects: 304091Seric ** The header is saved on the header list. 314319Seric ** Contents of 'line' are destroyed. 324091Seric */ 334091Seric 3455012Seric chompheader(line, def, e) 354091Seric char *line; 364091Seric bool def; 3755012Seric register ENVELOPE *e; 384091Seric { 394091Seric register char *p; 404091Seric register HDR *h; 414091Seric HDR **hp; 424091Seric char *fname; 434091Seric char *fvalue; 444091Seric struct hdrinfo *hi; 459059Seric bool cond = FALSE; 4610689Seric BITMAP mopts; 4764351Seric char buf[MAXNAME]; 484091Seric 497677Seric if (tTd(31, 6)) 507677Seric printf("chompheader: %s\n", line); 517677Seric 524627Seric /* strip off options */ 5310689Seric clrbitmap(mopts); 544627Seric p = line; 5557405Seric if (*p == '?') 564627Seric { 574627Seric /* have some */ 5856795Seric register char *q = strchr(p + 1, *p); 594627Seric 604627Seric if (q != NULL) 614627Seric { 624627Seric *q++ = '\0'; 6310689Seric while (*++p != '\0') 6410689Seric setbitn(*p, mopts); 654627Seric p = q; 664627Seric } 674627Seric else 6858151Seric usrerr("553 header syntax error, line \"%s\"", line); 699059Seric cond = TRUE; 704627Seric } 714627Seric 724091Seric /* find canonical name */ 734627Seric fname = p; 7464149Seric while (isascii(*p) && isgraph(*p) && *p != ':') 7564149Seric p++; 7664149Seric fvalue = p; 7764149Seric while (isascii(*p) && isspace(*p)) 7864149Seric p++; 7964150Seric if (*p++ != ':' || fname == fvalue) 8010118Seric { 8158151Seric syserr("553 header syntax error, line \"%s\"", line); 8210118Seric return (0); 8310118Seric } 8464149Seric *fvalue = '\0'; 8564149Seric fvalue = p; 864091Seric 874091Seric /* strip field value on front */ 884091Seric if (*fvalue == ' ') 894091Seric fvalue++; 904091Seric 914091Seric /* see if it is a known type */ 924091Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 934091Seric { 9459579Seric if (strcasecmp(hi->hi_field, fname) == 0) 954091Seric break; 964091Seric } 974091Seric 9864283Seric if (tTd(31, 9)) 9964283Seric { 10064283Seric if (hi->hi_field == NULL) 10164283Seric printf("no header match\n"); 10264283Seric else 10364283Seric printf("header match, hi_flags=%o\n", hi->hi_flags); 10464283Seric } 10564283Seric 10611414Seric /* see if this is a resent message */ 10711930Seric if (!def && bitset(H_RESENT, hi->hi_flags)) 10855012Seric e->e_flags |= EF_RESENT; 10911414Seric 1104091Seric /* if this means "end of header" quit now */ 1114091Seric if (bitset(H_EOH, hi->hi_flags)) 1124091Seric return (hi->hi_flags); 1134091Seric 11464653Seric /* 11564653Seric ** Drop explicit From: if same as what we would generate. 11664653Seric ** This is to make MH (which doesn't always give a full name) 11764653Seric ** insert the full name information in all circumstances. 11864653Seric */ 11964653Seric 12014784Seric p = "resent-from"; 12155012Seric if (!bitset(EF_RESENT, e->e_flags)) 12214784Seric p += 7; 12359579Seric if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0) 12411414Seric { 12563753Seric if (tTd(31, 2)) 12663753Seric { 12763753Seric printf("comparing header from (%s) against default (%s or %s)\n", 12863753Seric fvalue, e->e_from.q_paddr, e->e_from.q_user); 12963753Seric } 13055012Seric if (e->e_from.q_paddr != NULL && 13163753Seric (strcmp(fvalue, e->e_from.q_paddr) == 0 || 13263753Seric strcmp(fvalue, e->e_from.q_user) == 0)) 13311414Seric return (hi->hi_flags); 13464351Seric #ifdef MAYBENEXTRELEASE /* XXX UNTESTED XXX UNTESTED XXX UNTESTED XXX */ 13564351Seric #ifdef USERDB 13664351Seric else 13764351Seric { 13864351Seric auto ADDRESS a; 13964351Seric char *fancy; 14064351Seric extern char *crackaddr(); 14164351Seric extern char *udbsender(); 14264351Seric 14364653Seric /* 14464653Seric ** Try doing USERDB rewriting even on fully commented 14564653Seric ** names; this saves the "comment" information (such 14664653Seric ** as full name) and rewrites the electronic part. 14764653Seric */ 14864653Seric 14964351Seric fancy = crackaddr(fvalue); 15064351Seric if (parseaddr(fvalue, &a, RF_COPYNONE, '\0', NULL, e) != NULL && 15164351Seric a.q_mailer == LocalMailer && 15264351Seric (p = udbsender(a.q_user)) != NULL) 15364351Seric { 15464351Seric char *oldg = macvalue('g', e); 15564351Seric 15664351Seric define('g', p, e); 15764351Seric expand(fancy, buf, &buf[sizeof buf], e); 15864351Seric define('g', oldg, e); 15964351Seric fvalue = buf; 16064351Seric } 16164351Seric } 16264351Seric #endif 16364351Seric #endif 16411414Seric } 16511414Seric 16614784Seric /* delete default value for this header */ 16755012Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 16814784Seric { 16959579Seric if (strcasecmp(fname, h->h_field) == 0 && 17014784Seric bitset(H_DEFAULT, h->h_flags) && 17114784Seric !bitset(H_FORCE, h->h_flags)) 17214784Seric h->h_value = NULL; 17314784Seric } 17414784Seric 17513012Seric /* create a new node */ 17613012Seric h = (HDR *) xalloc(sizeof *h); 17713012Seric h->h_field = newstr(fname); 17864134Seric h->h_value = newstr(fvalue); 17913012Seric h->h_link = NULL; 18023117Seric bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts); 18113012Seric *hp = h; 1828066Seric h->h_flags = hi->hi_flags; 1834091Seric if (def) 1844091Seric h->h_flags |= H_DEFAULT; 1859059Seric if (cond) 1869059Seric h->h_flags |= H_CHECK; 1874091Seric 1885937Seric /* hack to see if this is a new format message */ 1898095Seric if (!def && bitset(H_RCPT|H_FROM, h->h_flags) && 19056795Seric (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL || 19156795Seric strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL)) 1928089Seric { 19355012Seric e->e_flags &= ~EF_OLDSTYLE; 1948089Seric } 1955937Seric 1964091Seric return (h->h_flags); 1974091Seric } 1984091Seric /* 1996980Seric ** ADDHEADER -- add a header entry to the end of the queue. 2006980Seric ** 2016980Seric ** This bypasses the special checking of chompheader. 2026980Seric ** 2036980Seric ** Parameters: 20459579Seric ** field -- the name of the header field. 20558789Seric ** value -- the value of the field. 2066980Seric ** e -- the envelope to add them to. 2076980Seric ** 2086980Seric ** Returns: 2096980Seric ** none. 2106980Seric ** 2116980Seric ** Side Effects: 2126980Seric ** adds the field on the list of headers for this envelope. 2136980Seric */ 2146980Seric 2156980Seric addheader(field, value, e) 2166980Seric char *field; 2176980Seric char *value; 2186980Seric ENVELOPE *e; 2196980Seric { 2206980Seric register HDR *h; 2216980Seric register struct hdrinfo *hi; 2226980Seric HDR **hp; 2236980Seric 2246980Seric /* find info struct */ 2256980Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 2266980Seric { 22759579Seric if (strcasecmp(field, hi->hi_field) == 0) 2286980Seric break; 2296980Seric } 2306980Seric 2316980Seric /* find current place in list -- keep back pointer? */ 2326980Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 2336980Seric { 23459579Seric if (strcasecmp(field, h->h_field) == 0) 2356980Seric break; 2366980Seric } 2376980Seric 2386980Seric /* allocate space for new header */ 2396980Seric h = (HDR *) xalloc(sizeof *h); 2406980Seric h->h_field = field; 2416980Seric h->h_value = newstr(value); 2427368Seric h->h_link = *hp; 2436980Seric h->h_flags = hi->hi_flags | H_DEFAULT; 24410689Seric clrbitmap(h->h_mflags); 2456980Seric *hp = h; 2466980Seric } 2476980Seric /* 2484091Seric ** HVALUE -- return value of a header. 2494091Seric ** 2504091Seric ** Only "real" fields (i.e., ones that have not been supplied 2514091Seric ** as a default) are used. 2524091Seric ** 2534091Seric ** Parameters: 2544091Seric ** field -- the field name. 25555012Seric ** e -- the envelope containing the header. 2564091Seric ** 2574091Seric ** Returns: 2584091Seric ** pointer to the value part. 2594091Seric ** NULL if not found. 2604091Seric ** 2614091Seric ** Side Effects: 2629382Seric ** none. 2634091Seric */ 2644091Seric 2654091Seric char * 26655012Seric hvalue(field, e) 2674091Seric char *field; 26855012Seric register ENVELOPE *e; 2694091Seric { 2704091Seric register HDR *h; 2714091Seric 27255012Seric for (h = e->e_header; h != NULL; h = h->h_link) 2734091Seric { 27459579Seric if (!bitset(H_DEFAULT, h->h_flags) && 27559579Seric strcasecmp(h->h_field, field) == 0) 2764091Seric return (h->h_value); 2774091Seric } 2784091Seric return (NULL); 2794091Seric } 2804091Seric /* 2814091Seric ** ISHEADER -- predicate telling if argument is a header. 2824091Seric ** 2834319Seric ** A line is a header if it has a single word followed by 2844319Seric ** optional white space followed by a colon. 2854319Seric ** 2864091Seric ** Parameters: 2874091Seric ** s -- string to check for possible headerness. 2884091Seric ** 2894091Seric ** Returns: 2904091Seric ** TRUE if s is a header. 2914091Seric ** FALSE otherwise. 2924091Seric ** 2934091Seric ** Side Effects: 2944091Seric ** none. 2954091Seric */ 2964091Seric 2974091Seric bool 2984091Seric isheader(s) 2994091Seric register char *s; 3004091Seric { 3019382Seric while (*s > ' ' && *s != ':' && *s != '\0') 3024091Seric s++; 3039382Seric 3049382Seric /* following technically violates RFC822 */ 30558050Seric while (isascii(*s) && isspace(*s)) 3064091Seric s++; 3079382Seric 3084091Seric return (*s == ':'); 3094091Seric } 3105919Seric /* 3117783Seric ** EATHEADER -- run through the stored header and extract info. 3127783Seric ** 3137783Seric ** Parameters: 3149382Seric ** e -- the envelope to process. 31558929Seric ** full -- if set, do full processing (e.g., compute 31658929Seric ** message priority). 3177783Seric ** 3187783Seric ** Returns: 3197783Seric ** none. 3207783Seric ** 3217783Seric ** Side Effects: 3227783Seric ** Sets a bunch of global variables from information 3239382Seric ** in the collected header. 3249382Seric ** Aborts the message if the hop count is exceeded. 3257783Seric */ 3267783Seric 32758929Seric eatheader(e, full) 3289382Seric register ENVELOPE *e; 32958929Seric bool full; 3307783Seric { 3317783Seric register HDR *h; 3327783Seric register char *p; 3339382Seric int hopcnt = 0; 33457208Seric char *msgid; 33558688Seric char buf[MAXLINE]; 3367783Seric 33758688Seric /* 33858688Seric ** Set up macros for possible expansion in headers. 33958688Seric */ 34058688Seric 34158704Seric define('f', e->e_sender, e); 34258704Seric define('g', e->e_sender, e); 34364148Seric if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0') 34464148Seric define('u', e->e_origrcpt, e); 34564148Seric else 34664148Seric define('u', NULL, e); 34758688Seric 34865052Seric /* full name of from person */ 34965052Seric p = hvalue("full-name", e); 35065052Seric if (p != NULL) 35165052Seric define('x', p, e); 35265052Seric 3539382Seric if (tTd(32, 1)) 3549382Seric printf("----- collected header -----\n"); 35557208Seric msgid = "<none>"; 3569382Seric for (h = e->e_header; h != NULL; h = h->h_link) 3577783Seric { 35864156Seric if (h->h_value == NULL) 35964156Seric { 36064156Seric if (tTd(32, 1)) 36164156Seric printf("%s: <NULL>\n", h->h_field); 36264156Seric continue; 36364156Seric } 36464156Seric 36558688Seric /* do early binding */ 36664156Seric if (bitset(H_DEFAULT, h->h_flags)) 36758688Seric { 36858688Seric expand(h->h_value, buf, &buf[sizeof buf], e); 36958688Seric if (buf[0] != '\0') 37058688Seric { 37158688Seric h->h_value = newstr(buf); 37258688Seric h->h_flags &= ~H_DEFAULT; 37358688Seric } 37458688Seric } 37558688Seric 3769382Seric if (tTd(32, 1)) 37759579Seric printf("%s: %s\n", h->h_field, h->h_value); 37857359Seric 37911414Seric /* count the number of times it has been processed */ 3809382Seric if (bitset(H_TRACE, h->h_flags)) 3819382Seric hopcnt++; 38211414Seric 38311414Seric /* send to this person if we so desire */ 38411414Seric if (GrabTo && bitset(H_RCPT, h->h_flags) && 38511414Seric !bitset(H_DEFAULT, h->h_flags) && 38655012Seric (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags))) 38711414Seric { 38863839Seric int saveflags = e->e_flags; 38963839Seric 39064283Seric (void) sendtolist(h->h_value, NULLADDR, 39158082Seric &e->e_sendqueue, e); 39263839Seric 39363839Seric /* delete fatal errors generated by this address */ 39464148Seric if (!GrabTo && !bitset(EF_FATALERRS, saveflags)) 39563839Seric e->e_flags &= ~EF_FATALERRS; 39611414Seric } 39711414Seric 39857208Seric /* save the message-id for logging */ 39964156Seric if (full && strcasecmp(h->h_field, "message-id") == 0) 40011290Seric { 40157208Seric msgid = h->h_value; 40259859Seric while (isascii(*msgid) && isspace(*msgid)) 40359859Seric msgid++; 40411290Seric } 40557359Seric 40657359Seric /* see if this is a return-receipt header */ 40757359Seric if (bitset(H_RECEIPTTO, h->h_flags)) 40857359Seric e->e_receiptto = h->h_value; 40957359Seric 41057359Seric /* see if this is an errors-to header */ 41161104Seric if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags)) 41264283Seric (void) sendtolist(h->h_value, NULLADDR, 41358082Seric &e->e_errorqueue, e); 4149382Seric } 4159382Seric if (tTd(32, 1)) 4167783Seric printf("----------------------------\n"); 4177783Seric 41858145Seric /* if we are just verifying (that is, sendmail -t -bv), drop out now */ 41958145Seric if (OpMode == MD_VERIFY) 42058145Seric return; 42158145Seric 4229382Seric /* store hop count */ 4239382Seric if (hopcnt > e->e_hopcount) 4249382Seric e->e_hopcount = hopcnt; 4259382Seric 4267783Seric /* message priority */ 42755012Seric p = hvalue("precedence", e); 4289382Seric if (p != NULL) 4299382Seric e->e_class = priencode(p); 43058929Seric if (full) 43125013Seric e->e_msgpriority = e->e_msgsize 43224981Seric - e->e_class * WkClassFact 43324981Seric + e->e_nrcpts * WkRecipFact; 4347783Seric 4357783Seric /* date message originated */ 43655012Seric p = hvalue("posted-date", e); 4377783Seric if (p == NULL) 43855012Seric p = hvalue("date", e); 4397783Seric if (p != NULL) 4409382Seric define('a', p, e); 44111290Seric 44211290Seric /* 44311290Seric ** Log collection information. 44411290Seric */ 44511290Seric 44611290Seric # ifdef LOG 44758929Seric if (full && LogLevel > 4) 44811290Seric { 44957359Seric char *name; 45060575Seric register char *sbp; 45157232Seric char hbuf[MAXNAME]; 45257359Seric char sbuf[MAXLINE]; 45336230Skarels 45458667Seric if (bitset(EF_RESPONSE, e->e_flags)) 45558667Seric name = "[RESPONSE]"; 45658951Seric else if ((name = macvalue('_', e)) != NULL) 45758951Seric ; 45858667Seric else if (RealHostName[0] == '[') 45936230Skarels name = RealHostName; 46036230Skarels else 46157359Seric { 46257359Seric name = hbuf; 46358798Seric (void) sprintf(hbuf, "%.80s", RealHostName); 46458798Seric if (RealHostAddr.sa.sa_family != 0) 46558798Seric { 46658798Seric p = &hbuf[strlen(hbuf)]; 46758798Seric (void) sprintf(p, " (%s)", 46858798Seric anynet_ntoa(&RealHostAddr)); 46958798Seric } 47057359Seric } 47157359Seric 47257359Seric /* some versions of syslog only take 5 printf args */ 473*65059Seric # if (SYSLOG_BUFSIZE) >= 256 47460575Seric sbp = sbuf; 47560575Seric sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s", 47658558Seric e->e_from.q_paddr, e->e_msgsize, e->e_class, 47757589Seric e->e_msgpriority, e->e_nrcpts, msgid); 47860575Seric sbp += strlen(sbp); 47960575Seric if (e->e_bodytype != NULL) 48060575Seric { 48160575Seric (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype); 48260575Seric sbp += strlen(sbp); 48360575Seric } 48460575Seric p = macvalue('r', e); 48560575Seric if (p != NULL) 48660575Seric (void) sprintf(sbp, ", proto=%.20s", p); 48758686Seric syslog(LOG_INFO, "%s: %s, relay=%s", 48857359Seric e->e_id, sbuf, name); 489*65059Seric 490*65059Seric # else /* short syslog buffer */ 491*65059Seric 492*65059Seric syslog(LOG_INFO, "%s: from=%s", 493*65059Seric e->e_id, shortenstring(e->e_from.q_paddr, 83)); 494*65059Seric syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d", 495*65059Seric e->e_id, e->e_msgsize, e->e_class, 496*65059Seric e->e_msgpriority, e->e_nrcpts); 497*65059Seric syslog(LOG_INFO, "%s: msgid=%s", e->e_id, msgid); 498*65059Seric if (e->e_bodytype != NULL) 499*65059Seric syslog(LOG_INFO, "%s: bodytype=%s", 500*65059Seric e->e_id, e->e_bodytype); 501*65059Seric p = macvalue('r', e); 502*65059Seric if (p != NULL) 503*65059Seric syslog(LOG_INFO, "%s: proto=%s", e->e_id, p); 504*65059Seric syslog(LOG_INFO, "%s: relay=%s", e->e_id, name); 505*65059Seric # endif 50611290Seric } 50756795Seric # endif /* LOG */ 5087783Seric } 5097783Seric /* 5107783Seric ** PRIENCODE -- encode external priority names into internal values. 5117783Seric ** 5127783Seric ** Parameters: 5137783Seric ** p -- priority in ascii. 5147783Seric ** 5157783Seric ** Returns: 5167783Seric ** priority as a numeric level. 5177783Seric ** 5187783Seric ** Side Effects: 5197783Seric ** none. 5207783Seric */ 5217783Seric 5227783Seric priencode(p) 5237783Seric char *p; 5247783Seric { 5258253Seric register int i; 5267783Seric 5278253Seric for (i = 0; i < NumPriorities; i++) 5287783Seric { 52933725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 5308253Seric return (Priorities[i].pri_val); 5317783Seric } 5328253Seric 5338253Seric /* unknown priority */ 5348253Seric return (0); 5357783Seric } 5367783Seric /* 5377890Seric ** CRACKADDR -- parse an address and turn it into a macro 5387783Seric ** 5397783Seric ** This doesn't actually parse the address -- it just extracts 5407783Seric ** it and replaces it with "$g". The parse is totally ad hoc 5417783Seric ** and isn't even guaranteed to leave something syntactically 5427783Seric ** identical to what it started with. However, it does leave 5437783Seric ** something semantically identical. 5447783Seric ** 54551379Seric ** This algorithm has been cleaned up to handle a wider range 54651379Seric ** of cases -- notably quoted and backslash escaped strings. 54751379Seric ** This modification makes it substantially better at preserving 54851379Seric ** the original syntax. 5497783Seric ** 5507783Seric ** Parameters: 5517890Seric ** addr -- the address to be cracked. 5527783Seric ** 5537783Seric ** Returns: 5547783Seric ** a pointer to the new version. 5557783Seric ** 5567783Seric ** Side Effects: 5577890Seric ** none. 5587783Seric ** 5597783Seric ** Warning: 5607783Seric ** The return value is saved in local storage and should 5617783Seric ** be copied if it is to be reused. 5627783Seric */ 5637783Seric 5647783Seric char * 5657890Seric crackaddr(addr) 5667890Seric register char *addr; 5677783Seric { 5687783Seric register char *p; 56951379Seric register char c; 57051379Seric int cmtlev; 57156764Seric int realcmtlev; 57256764Seric int anglelev, realanglelev; 57351379Seric int copylev; 57451379Seric bool qmode; 57556764Seric bool realqmode; 57656764Seric bool skipping; 57751379Seric bool putgmac = FALSE; 57856735Seric bool quoteit = FALSE; 57964148Seric bool gotangle = FALSE; 58051379Seric register char *bp; 58156764Seric char *buflim; 5827783Seric static char buf[MAXNAME]; 5837783Seric 5847783Seric if (tTd(33, 1)) 5857890Seric printf("crackaddr(%s)\n", addr); 5867783Seric 5878082Seric /* strip leading spaces */ 58858050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 5898082Seric addr++; 5908082Seric 5917783Seric /* 59251379Seric ** Start by assuming we have no angle brackets. This will be 59351379Seric ** adjusted later if we find them. 5947783Seric */ 5957783Seric 59651379Seric bp = buf; 59756764Seric buflim = &buf[sizeof buf - 5]; 59851379Seric p = addr; 59956764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 60056764Seric qmode = realqmode = FALSE; 60151379Seric 60251379Seric while ((c = *p++) != '\0') 6037783Seric { 60456764Seric /* 60556764Seric ** If the buffer is overful, go into a special "skipping" 60656764Seric ** mode that tries to keep legal syntax but doesn't actually 60756764Seric ** output things. 60856764Seric */ 6097783Seric 61056764Seric skipping = bp >= buflim; 61156735Seric 61256764Seric if (copylev > 0 && !skipping) 61356764Seric *bp++ = c; 61456735Seric 61551379Seric /* check for backslash escapes */ 61651379Seric if (c == '\\') 6177783Seric { 61858890Seric /* arrange to quote the address */ 61958890Seric if (cmtlev <= 0 && !qmode) 62058890Seric quoteit = TRUE; 62158890Seric 62251379Seric if ((c = *p++) == '\0') 6237783Seric { 62451379Seric /* too far */ 62551379Seric p--; 62651379Seric goto putg; 6277783Seric } 62856764Seric if (copylev > 0 && !skipping) 62951379Seric *bp++ = c; 63051379Seric goto putg; 6317783Seric } 6327783Seric 63351379Seric /* check for quoted strings */ 63464967Seric if (c == '"' && cmtlev <= 0) 6357783Seric { 63651379Seric qmode = !qmode; 63756764Seric if (copylev > 0 && !skipping) 63856764Seric realqmode = !realqmode; 63951379Seric continue; 6407783Seric } 64151379Seric if (qmode) 64251379Seric goto putg; 6437783Seric 64451379Seric /* check for comments */ 64551379Seric if (c == '(') 6467783Seric { 64751379Seric cmtlev++; 64856764Seric 64956764Seric /* allow space for closing paren */ 65056764Seric if (!skipping) 65156764Seric { 65256764Seric buflim--; 65356764Seric realcmtlev++; 65456764Seric if (copylev++ <= 0) 65556764Seric { 65656764Seric *bp++ = ' '; 65756764Seric *bp++ = c; 65856764Seric } 65956764Seric } 66051379Seric } 66151379Seric if (cmtlev > 0) 66251379Seric { 66351379Seric if (c == ')') 6647783Seric { 66551379Seric cmtlev--; 66651379Seric copylev--; 66756764Seric if (!skipping) 66856764Seric { 66956764Seric realcmtlev--; 67056764Seric buflim++; 67156764Seric } 6727783Seric } 6737783Seric continue; 6747783Seric } 67556764Seric else if (c == ')') 67656764Seric { 67756764Seric /* syntax error: unmatched ) */ 67856764Seric if (!skipping) 67956764Seric bp--; 68056764Seric } 6817783Seric 68256764Seric /* check for characters that may have to be quoted */ 68364148Seric if (strchr(".'@,;:\\()[]", c) != NULL) 68456764Seric { 68556764Seric /* 68656764Seric ** If these occur as the phrase part of a <> 68756764Seric ** construct, but are not inside of () or already 68856764Seric ** quoted, they will have to be quoted. Note that 68956764Seric ** now (but don't actually do the quoting). 69056764Seric */ 69156764Seric 69256764Seric if (cmtlev <= 0 && !qmode) 69356764Seric quoteit = TRUE; 69456764Seric } 69556764Seric 69651379Seric /* check for angle brackets */ 69751379Seric if (c == '<') 69851379Seric { 69956735Seric register char *q; 70056735Seric 70164148Seric /* assume first of two angles is bogus */ 70264148Seric if (gotangle) 70364148Seric quoteit = TRUE; 70464148Seric gotangle = TRUE; 70564148Seric 70651379Seric /* oops -- have to change our mind */ 70764752Seric anglelev = 1; 70856764Seric if (!skipping) 70964752Seric realanglelev = 1; 71056764Seric 71156735Seric bp = buf; 71256735Seric if (quoteit) 71356735Seric { 71456735Seric *bp++ = '"'; 71556735Seric 71656735Seric /* back up over the '<' and any spaces */ 71756735Seric --p; 71858050Seric while (isascii(*--p) && isspace(*p)) 71956735Seric continue; 72056735Seric p++; 72156735Seric } 72256735Seric for (q = addr; q < p; ) 72356735Seric { 72456735Seric c = *q++; 72556764Seric if (bp < buflim) 72656764Seric { 72756764Seric if (quoteit && c == '"') 72856764Seric *bp++ = '\\'; 72956764Seric *bp++ = c; 73056764Seric } 73156735Seric } 73256735Seric if (quoteit) 73356735Seric { 73464148Seric if (bp == &buf[1]) 73564148Seric bp--; 73664148Seric else 73764148Seric *bp++ = '"'; 73856764Seric while ((c = *p++) != '<') 73956764Seric { 74056764Seric if (bp < buflim) 74156764Seric *bp++ = c; 74256764Seric } 74356764Seric *bp++ = c; 74456735Seric } 74551379Seric copylev = 0; 74656735Seric putgmac = quoteit = FALSE; 74751379Seric continue; 74851379Seric } 7497783Seric 75051379Seric if (c == '>') 7517783Seric { 75256764Seric if (anglelev > 0) 75356764Seric { 75456764Seric anglelev--; 75556764Seric if (!skipping) 75656764Seric { 75756764Seric realanglelev--; 75856764Seric buflim++; 75956764Seric } 76056764Seric } 76156764Seric else if (!skipping) 76256764Seric { 76356764Seric /* syntax error: unmatched > */ 76456764Seric if (copylev > 0) 76556764Seric bp--; 76664752Seric quoteit = TRUE; 76756764Seric continue; 76856764Seric } 76951379Seric if (copylev++ <= 0) 77051379Seric *bp++ = c; 77151379Seric continue; 7727783Seric } 77351379Seric 77451379Seric /* must be a real address character */ 77551379Seric putg: 77651379Seric if (copylev <= 0 && !putgmac) 77751379Seric { 77858050Seric *bp++ = MACROEXPAND; 77951379Seric *bp++ = 'g'; 78051379Seric putgmac = TRUE; 78151379Seric } 7827783Seric } 7837783Seric 78456764Seric /* repair any syntactic damage */ 78556764Seric if (realqmode) 78656764Seric *bp++ = '"'; 78756764Seric while (realcmtlev-- > 0) 78856764Seric *bp++ = ')'; 78956764Seric while (realanglelev-- > 0) 79056764Seric *bp++ = '>'; 79151379Seric *bp++ = '\0'; 7927783Seric 7937783Seric if (tTd(33, 1)) 7947944Seric printf("crackaddr=>`%s'\n", buf); 7957783Seric 7967783Seric return (buf); 7977783Seric } 7989382Seric /* 7999382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 8009382Seric ** 8019382Seric ** Parameters: 8029382Seric ** fp -- file to put it on. 8039382Seric ** m -- mailer to use. 8049382Seric ** e -- envelope to use. 8059382Seric ** 8069382Seric ** Returns: 8079382Seric ** none. 8089382Seric ** 8099382Seric ** Side Effects: 8109382Seric ** none. 8119382Seric */ 8129382Seric 81357589Seric /* 81457589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 81557589Seric */ 81657589Seric #ifndef MAX 81757589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 81857589Seric #endif 81957589Seric 82010176Seric putheader(fp, m, e) 8219382Seric register FILE *fp; 8229382Seric register MAILER *m; 8239382Seric register ENVELOPE *e; 8249382Seric { 82557135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 8269382Seric register HDR *h; 82757135Seric char obuf[MAXLINE]; 8289382Seric 82959882Seric if (tTd(34, 1)) 83059882Seric printf("--- putheader, mailer = %s ---\n", m->m_name); 83159882Seric 8329382Seric for (h = e->e_header; h != NULL; h = h->h_link) 8339382Seric { 8349382Seric register char *p; 83510689Seric extern bool bitintersect(); 8369382Seric 83759882Seric if (tTd(34, 11)) 83859882Seric { 83959882Seric printf(" %s: ", h->h_field); 84059882Seric xputs(h->h_value); 84159882Seric } 84259882Seric 8439382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 84410689Seric !bitintersect(h->h_mflags, m->m_flags)) 84559882Seric { 84659882Seric if (tTd(34, 11)) 84759882Seric printf(" (skipped)\n"); 8489382Seric continue; 84959882Seric } 8509382Seric 85111414Seric /* handle Resent-... headers specially */ 85211414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 85359882Seric { 85459882Seric if (tTd(34, 11)) 85559882Seric printf(" (skipped (resent))\n"); 85611414Seric continue; 85759882Seric } 85859882Seric if (tTd(34, 11)) 85959882Seric printf("\n"); 86011414Seric 8619382Seric p = h->h_value; 8629382Seric if (bitset(H_DEFAULT, h->h_flags)) 8639382Seric { 8649382Seric /* macro expand value if generated internally */ 8659382Seric expand(p, buf, &buf[sizeof buf], e); 8669382Seric p = buf; 8679382Seric if (p == NULL || *p == '\0') 8689382Seric continue; 8699382Seric } 8709382Seric 8719382Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 8729382Seric { 8739382Seric /* address field */ 8749382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 8759382Seric 8769382Seric if (bitset(H_FROM, h->h_flags)) 8779382Seric oldstyle = FALSE; 87855012Seric commaize(h, p, fp, oldstyle, m, e); 8799382Seric } 8809382Seric else 8819382Seric { 8829382Seric /* vanilla header line */ 88312159Seric register char *nlp; 88412159Seric 88559579Seric (void) sprintf(obuf, "%s: ", h->h_field); 88656795Seric while ((nlp = strchr(p, '\n')) != NULL) 88712159Seric { 88812159Seric *nlp = '\0'; 88912159Seric (void) strcat(obuf, p); 89012159Seric *nlp = '\n'; 89112159Seric putline(obuf, fp, m); 89212159Seric p = ++nlp; 89312161Seric obuf[0] = '\0'; 89412159Seric } 89512159Seric (void) strcat(obuf, p); 89610176Seric putline(obuf, fp, m); 8979382Seric } 8989382Seric } 8999382Seric } 9009382Seric /* 9019382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 9029382Seric ** 9039382Seric ** Parameters: 9049382Seric ** h -- the header field to output. 9059382Seric ** p -- the value to put in it. 9069382Seric ** fp -- file to put it to. 9079382Seric ** oldstyle -- TRUE if this is an old style header. 9089382Seric ** m -- a pointer to the mailer descriptor. If NULL, 9099382Seric ** don't transform the name at all. 91055012Seric ** e -- the envelope containing the message. 9119382Seric ** 9129382Seric ** Returns: 9139382Seric ** none. 9149382Seric ** 9159382Seric ** Side Effects: 9169382Seric ** outputs "p" to file "fp". 9179382Seric */ 9189382Seric 91955012Seric commaize(h, p, fp, oldstyle, m, e) 9209382Seric register HDR *h; 9219382Seric register char *p; 9229382Seric FILE *fp; 9239382Seric bool oldstyle; 9249382Seric register MAILER *m; 92555012Seric register ENVELOPE *e; 9269382Seric { 9279382Seric register char *obp; 9289382Seric int opos; 9299382Seric bool firstone = TRUE; 93011157Seric char obuf[MAXLINE + 3]; 9319382Seric 9329382Seric /* 9339382Seric ** Output the address list translated by the 9349382Seric ** mailer and with commas. 9359382Seric */ 9369382Seric 9379382Seric if (tTd(14, 2)) 9389382Seric printf("commaize(%s: %s)\n", h->h_field, p); 9399382Seric 9409382Seric obp = obuf; 94159579Seric (void) sprintf(obp, "%s: ", h->h_field); 9429382Seric opos = strlen(h->h_field) + 2; 9439382Seric obp += opos; 9449382Seric 9459382Seric /* 9469382Seric ** Run through the list of values. 9479382Seric */ 9489382Seric 9499382Seric while (*p != '\0') 9509382Seric { 9519382Seric register char *name; 95254983Seric register int c; 9539382Seric char savechar; 95459163Seric int flags; 95559163Seric auto int stat; 9569382Seric 9579382Seric /* 9589382Seric ** Find the end of the name. New style names 9599382Seric ** end with a comma, old style names end with 9609382Seric ** a space character. However, spaces do not 9619382Seric ** necessarily delimit an old-style name -- at 9629382Seric ** signs mean keep going. 9639382Seric */ 9649382Seric 9659382Seric /* find end of name */ 96658050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 9679382Seric p++; 9689382Seric name = p; 9699382Seric for (;;) 9709382Seric { 97158333Seric auto char *oldp; 97216909Seric char pvpbuf[PSBUFSIZE]; 9739382Seric 97458333Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp); 97558333Seric p = oldp; 9769382Seric 9779382Seric /* look to see if we have an at sign */ 97858050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 9799382Seric p++; 9809382Seric 98158170Seric if (*p != '@') 9829382Seric { 9839382Seric p = oldp; 9849382Seric break; 9859382Seric } 9869382Seric p += *p == '@' ? 1 : 2; 98758050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 9889382Seric p++; 9899382Seric } 9909382Seric /* at the end of one complete name */ 9919382Seric 9929382Seric /* strip off trailing white space */ 99358050Seric while (p >= name && 99458050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 9959382Seric p--; 9969382Seric if (++p == name) 9979382Seric continue; 9989382Seric savechar = *p; 9999382Seric *p = '\0'; 10009382Seric 10019382Seric /* translate the name to be relative */ 100259163Seric flags = RF_HEADERADDR|RF_ADDDOMAIN; 100359163Seric if (bitset(H_FROM, h->h_flags)) 100459163Seric flags |= RF_SENDERADDR; 100559163Seric stat = EX_OK; 100659163Seric name = remotename(name, m, flags, &stat, e); 10079382Seric if (*name == '\0') 10089382Seric { 10099382Seric *p = savechar; 10109382Seric continue; 10119382Seric } 10129382Seric 10139382Seric /* output the name with nice formatting */ 101454983Seric opos += strlen(name); 10159382Seric if (!firstone) 10169382Seric opos += 2; 10179382Seric if (opos > 78 && !firstone) 10189382Seric { 101910178Seric (void) strcpy(obp, ",\n"); 102010176Seric putline(obuf, fp, m); 10219382Seric obp = obuf; 10229382Seric (void) sprintf(obp, " "); 102310161Seric opos = strlen(obp); 102410161Seric obp += opos; 102554983Seric opos += strlen(name); 10269382Seric } 10279382Seric else if (!firstone) 10289382Seric { 10299382Seric (void) sprintf(obp, ", "); 10309382Seric obp += 2; 10319382Seric } 10329382Seric 103354983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 103454983Seric *obp++ = c; 10359382Seric firstone = FALSE; 10369382Seric *p = savechar; 10379382Seric } 10389382Seric (void) strcpy(obp, "\n"); 103910176Seric putline(obuf, fp, m); 10409382Seric } 10419382Seric /* 104258170Seric ** COPYHEADER -- copy header list 10439382Seric ** 104458170Seric ** This routine is the equivalent of newstr for header lists 104558170Seric ** 10469382Seric ** Parameters: 104758170Seric ** header -- list of header structures to copy. 10489382Seric ** 10499382Seric ** Returns: 105058170Seric ** a copy of 'header'. 10519382Seric ** 10529382Seric ** Side Effects: 10539382Seric ** none. 10549382Seric */ 10559382Seric 105658170Seric HDR * 105758170Seric copyheader(header) 105858170Seric register HDR *header; 10599382Seric { 106058170Seric register HDR *newhdr; 106158170Seric HDR *ret; 106258170Seric register HDR **tail = &ret; 10639382Seric 106458170Seric while (header != NULL) 106558170Seric { 106658170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 106758170Seric STRUCTCOPY(*header, *newhdr); 106858170Seric *tail = newhdr; 106958170Seric tail = &newhdr->h_link; 107058170Seric header = header->h_link; 107158170Seric } 107258170Seric *tail = NULL; 107358170Seric 107458170Seric return ret; 10759382Seric } 1076