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*64653Seric static char sccsid[] = "@(#)headers.c 8.12 (Berkeley) 09/26/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 114*64653Seric /* 115*64653Seric ** Drop explicit From: if same as what we would generate. 116*64653Seric ** This is to make MH (which doesn't always give a full name) 117*64653Seric ** insert the full name information in all circumstances. 118*64653Seric */ 119*64653Seric 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 143*64653Seric /* 144*64653Seric ** Try doing USERDB rewriting even on fully commented 145*64653Seric ** names; this saves the "comment" information (such 146*64653Seric ** as full name) and rewrites the electronic part. 147*64653Seric */ 148*64653Seric 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 3489382Seric if (tTd(32, 1)) 3499382Seric printf("----- collected header -----\n"); 35057208Seric msgid = "<none>"; 3519382Seric for (h = e->e_header; h != NULL; h = h->h_link) 3527783Seric { 35364156Seric if (h->h_value == NULL) 35464156Seric { 35564156Seric if (tTd(32, 1)) 35664156Seric printf("%s: <NULL>\n", h->h_field); 35764156Seric continue; 35864156Seric } 35964156Seric 36058688Seric /* do early binding */ 36164156Seric if (bitset(H_DEFAULT, h->h_flags)) 36258688Seric { 36358688Seric expand(h->h_value, buf, &buf[sizeof buf], e); 36458688Seric if (buf[0] != '\0') 36558688Seric { 36658688Seric h->h_value = newstr(buf); 36758688Seric h->h_flags &= ~H_DEFAULT; 36858688Seric } 36958688Seric } 37058688Seric 3719382Seric if (tTd(32, 1)) 37259579Seric printf("%s: %s\n", h->h_field, h->h_value); 37357359Seric 37411414Seric /* count the number of times it has been processed */ 3759382Seric if (bitset(H_TRACE, h->h_flags)) 3769382Seric hopcnt++; 37711414Seric 37811414Seric /* send to this person if we so desire */ 37911414Seric if (GrabTo && bitset(H_RCPT, h->h_flags) && 38011414Seric !bitset(H_DEFAULT, h->h_flags) && 38155012Seric (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags))) 38211414Seric { 38363839Seric int saveflags = e->e_flags; 38463839Seric 38564283Seric (void) sendtolist(h->h_value, NULLADDR, 38658082Seric &e->e_sendqueue, e); 38763839Seric 38863839Seric /* delete fatal errors generated by this address */ 38964148Seric if (!GrabTo && !bitset(EF_FATALERRS, saveflags)) 39063839Seric e->e_flags &= ~EF_FATALERRS; 39111414Seric } 39211414Seric 39357208Seric /* save the message-id for logging */ 39464156Seric if (full && strcasecmp(h->h_field, "message-id") == 0) 39511290Seric { 39657208Seric msgid = h->h_value; 39759859Seric while (isascii(*msgid) && isspace(*msgid)) 39859859Seric msgid++; 39911290Seric } 40057359Seric 40157359Seric /* see if this is a return-receipt header */ 40257359Seric if (bitset(H_RECEIPTTO, h->h_flags)) 40357359Seric e->e_receiptto = h->h_value; 40457359Seric 40557359Seric /* see if this is an errors-to header */ 40661104Seric if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags)) 40764283Seric (void) sendtolist(h->h_value, NULLADDR, 40858082Seric &e->e_errorqueue, e); 4099382Seric } 4109382Seric if (tTd(32, 1)) 4117783Seric printf("----------------------------\n"); 4127783Seric 41358145Seric /* if we are just verifying (that is, sendmail -t -bv), drop out now */ 41458145Seric if (OpMode == MD_VERIFY) 41558145Seric return; 41658145Seric 4179382Seric /* store hop count */ 4189382Seric if (hopcnt > e->e_hopcount) 4199382Seric e->e_hopcount = hopcnt; 4209382Seric 4217783Seric /* message priority */ 42255012Seric p = hvalue("precedence", e); 4239382Seric if (p != NULL) 4249382Seric e->e_class = priencode(p); 42558929Seric if (full) 42625013Seric e->e_msgpriority = e->e_msgsize 42724981Seric - e->e_class * WkClassFact 42824981Seric + e->e_nrcpts * WkRecipFact; 4297783Seric 4307783Seric /* full name of from person */ 43155012Seric p = hvalue("full-name", e); 4327783Seric if (p != NULL) 4339382Seric define('x', p, e); 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 */ 47360575Seric sbp = sbuf; 47460575Seric sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s", 47558558Seric e->e_from.q_paddr, e->e_msgsize, e->e_class, 47657589Seric e->e_msgpriority, e->e_nrcpts, msgid); 47760575Seric sbp += strlen(sbp); 47860575Seric if (e->e_bodytype != NULL) 47960575Seric { 48060575Seric (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype); 48160575Seric sbp += strlen(sbp); 48260575Seric } 48360575Seric p = macvalue('r', e); 48460575Seric if (p != NULL) 48560575Seric (void) sprintf(sbp, ", proto=%.20s", p); 48658686Seric syslog(LOG_INFO, "%s: %s, relay=%s", 48757359Seric e->e_id, sbuf, name); 48811290Seric } 48956795Seric # endif /* LOG */ 4907783Seric } 4917783Seric /* 4927783Seric ** PRIENCODE -- encode external priority names into internal values. 4937783Seric ** 4947783Seric ** Parameters: 4957783Seric ** p -- priority in ascii. 4967783Seric ** 4977783Seric ** Returns: 4987783Seric ** priority as a numeric level. 4997783Seric ** 5007783Seric ** Side Effects: 5017783Seric ** none. 5027783Seric */ 5037783Seric 5047783Seric priencode(p) 5057783Seric char *p; 5067783Seric { 5078253Seric register int i; 5087783Seric 5098253Seric for (i = 0; i < NumPriorities; i++) 5107783Seric { 51133725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 5128253Seric return (Priorities[i].pri_val); 5137783Seric } 5148253Seric 5158253Seric /* unknown priority */ 5168253Seric return (0); 5177783Seric } 5187783Seric /* 5197890Seric ** CRACKADDR -- parse an address and turn it into a macro 5207783Seric ** 5217783Seric ** This doesn't actually parse the address -- it just extracts 5227783Seric ** it and replaces it with "$g". The parse is totally ad hoc 5237783Seric ** and isn't even guaranteed to leave something syntactically 5247783Seric ** identical to what it started with. However, it does leave 5257783Seric ** something semantically identical. 5267783Seric ** 52751379Seric ** This algorithm has been cleaned up to handle a wider range 52851379Seric ** of cases -- notably quoted and backslash escaped strings. 52951379Seric ** This modification makes it substantially better at preserving 53051379Seric ** the original syntax. 5317783Seric ** 5327783Seric ** Parameters: 5337890Seric ** addr -- the address to be cracked. 5347783Seric ** 5357783Seric ** Returns: 5367783Seric ** a pointer to the new version. 5377783Seric ** 5387783Seric ** Side Effects: 5397890Seric ** none. 5407783Seric ** 5417783Seric ** Warning: 5427783Seric ** The return value is saved in local storage and should 5437783Seric ** be copied if it is to be reused. 5447783Seric */ 5457783Seric 5467783Seric char * 5477890Seric crackaddr(addr) 5487890Seric register char *addr; 5497783Seric { 5507783Seric register char *p; 55151379Seric register char c; 55251379Seric int cmtlev; 55356764Seric int realcmtlev; 55456764Seric int anglelev, realanglelev; 55551379Seric int copylev; 55651379Seric bool qmode; 55756764Seric bool realqmode; 55856764Seric bool skipping; 55951379Seric bool putgmac = FALSE; 56056735Seric bool quoteit = FALSE; 56164148Seric bool gotangle = FALSE; 56251379Seric register char *bp; 56356764Seric char *buflim; 5647783Seric static char buf[MAXNAME]; 5657783Seric 5667783Seric if (tTd(33, 1)) 5677890Seric printf("crackaddr(%s)\n", addr); 5687783Seric 5698082Seric /* strip leading spaces */ 57058050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 5718082Seric addr++; 5728082Seric 5737783Seric /* 57451379Seric ** Start by assuming we have no angle brackets. This will be 57551379Seric ** adjusted later if we find them. 5767783Seric */ 5777783Seric 57851379Seric bp = buf; 57956764Seric buflim = &buf[sizeof buf - 5]; 58051379Seric p = addr; 58156764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 58256764Seric qmode = realqmode = FALSE; 58351379Seric 58451379Seric while ((c = *p++) != '\0') 5857783Seric { 58656764Seric /* 58756764Seric ** If the buffer is overful, go into a special "skipping" 58856764Seric ** mode that tries to keep legal syntax but doesn't actually 58956764Seric ** output things. 59056764Seric */ 5917783Seric 59256764Seric skipping = bp >= buflim; 59356735Seric 59456764Seric if (copylev > 0 && !skipping) 59556764Seric *bp++ = c; 59656735Seric 59751379Seric /* check for backslash escapes */ 59851379Seric if (c == '\\') 5997783Seric { 60058890Seric /* arrange to quote the address */ 60158890Seric if (cmtlev <= 0 && !qmode) 60258890Seric quoteit = TRUE; 60358890Seric 60451379Seric if ((c = *p++) == '\0') 6057783Seric { 60651379Seric /* too far */ 60751379Seric p--; 60851379Seric goto putg; 6097783Seric } 61056764Seric if (copylev > 0 && !skipping) 61151379Seric *bp++ = c; 61251379Seric goto putg; 6137783Seric } 6147783Seric 61551379Seric /* check for quoted strings */ 61651379Seric if (c == '"') 6177783Seric { 61851379Seric qmode = !qmode; 61956764Seric if (copylev > 0 && !skipping) 62056764Seric realqmode = !realqmode; 62151379Seric continue; 6227783Seric } 62351379Seric if (qmode) 62451379Seric goto putg; 6257783Seric 62651379Seric /* check for comments */ 62751379Seric if (c == '(') 6287783Seric { 62951379Seric cmtlev++; 63056764Seric 63156764Seric /* allow space for closing paren */ 63256764Seric if (!skipping) 63356764Seric { 63456764Seric buflim--; 63556764Seric realcmtlev++; 63656764Seric if (copylev++ <= 0) 63756764Seric { 63856764Seric *bp++ = ' '; 63956764Seric *bp++ = c; 64056764Seric } 64156764Seric } 64251379Seric } 64351379Seric if (cmtlev > 0) 64451379Seric { 64551379Seric if (c == ')') 6467783Seric { 64751379Seric cmtlev--; 64851379Seric copylev--; 64956764Seric if (!skipping) 65056764Seric { 65156764Seric realcmtlev--; 65256764Seric buflim++; 65356764Seric } 6547783Seric } 6557783Seric continue; 6567783Seric } 65756764Seric else if (c == ')') 65856764Seric { 65956764Seric /* syntax error: unmatched ) */ 66056764Seric if (!skipping) 66156764Seric bp--; 66256764Seric } 6637783Seric 66456764Seric 66556764Seric /* check for characters that may have to be quoted */ 66664148Seric if (strchr(".'@,;:\\()[]", c) != NULL) 66756764Seric { 66856764Seric /* 66956764Seric ** If these occur as the phrase part of a <> 67056764Seric ** construct, but are not inside of () or already 67156764Seric ** quoted, they will have to be quoted. Note that 67256764Seric ** now (but don't actually do the quoting). 67356764Seric */ 67456764Seric 67556764Seric if (cmtlev <= 0 && !qmode) 67656764Seric quoteit = TRUE; 67756764Seric } 67856764Seric 67951379Seric /* check for angle brackets */ 68051379Seric if (c == '<') 68151379Seric { 68256735Seric register char *q; 68356735Seric 68464148Seric /* assume first of two angles is bogus */ 68564148Seric if (gotangle) 68664148Seric quoteit = TRUE; 68764148Seric gotangle = TRUE; 68864148Seric 68951379Seric /* oops -- have to change our mind */ 69056764Seric anglelev++; 69156764Seric if (!skipping) 69256764Seric realanglelev++; 69356764Seric 69456735Seric bp = buf; 69556735Seric if (quoteit) 69656735Seric { 69756735Seric *bp++ = '"'; 69856735Seric 69956735Seric /* back up over the '<' and any spaces */ 70056735Seric --p; 70158050Seric while (isascii(*--p) && isspace(*p)) 70256735Seric continue; 70356735Seric p++; 70456735Seric } 70556735Seric for (q = addr; q < p; ) 70656735Seric { 70756735Seric c = *q++; 70856764Seric if (bp < buflim) 70956764Seric { 71056764Seric if (quoteit && c == '"') 71156764Seric *bp++ = '\\'; 71256764Seric *bp++ = c; 71356764Seric } 71456735Seric } 71556735Seric if (quoteit) 71656735Seric { 71764148Seric if (bp == &buf[1]) 71864148Seric bp--; 71964148Seric else 72064148Seric *bp++ = '"'; 72156764Seric while ((c = *p++) != '<') 72256764Seric { 72356764Seric if (bp < buflim) 72456764Seric *bp++ = c; 72556764Seric } 72656764Seric *bp++ = c; 72756735Seric } 72851379Seric copylev = 0; 72956735Seric putgmac = quoteit = FALSE; 73051379Seric continue; 73151379Seric } 7327783Seric 73351379Seric if (c == '>') 7347783Seric { 73556764Seric if (anglelev > 0) 73656764Seric { 73756764Seric anglelev--; 73856764Seric if (!skipping) 73956764Seric { 74056764Seric realanglelev--; 74156764Seric buflim++; 74256764Seric } 74356764Seric } 74456764Seric else if (!skipping) 74556764Seric { 74656764Seric /* syntax error: unmatched > */ 74756764Seric if (copylev > 0) 74856764Seric bp--; 74956764Seric continue; 75056764Seric } 75151379Seric if (copylev++ <= 0) 75251379Seric *bp++ = c; 75351379Seric continue; 7547783Seric } 75551379Seric 75651379Seric /* must be a real address character */ 75751379Seric putg: 75851379Seric if (copylev <= 0 && !putgmac) 75951379Seric { 76058050Seric *bp++ = MACROEXPAND; 76151379Seric *bp++ = 'g'; 76251379Seric putgmac = TRUE; 76351379Seric } 7647783Seric } 7657783Seric 76656764Seric /* repair any syntactic damage */ 76756764Seric if (realqmode) 76856764Seric *bp++ = '"'; 76956764Seric while (realcmtlev-- > 0) 77056764Seric *bp++ = ')'; 77156764Seric while (realanglelev-- > 0) 77256764Seric *bp++ = '>'; 77351379Seric *bp++ = '\0'; 7747783Seric 7757783Seric if (tTd(33, 1)) 7767944Seric printf("crackaddr=>`%s'\n", buf); 7777783Seric 7787783Seric return (buf); 7797783Seric } 7809382Seric /* 7819382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 7829382Seric ** 7839382Seric ** Parameters: 7849382Seric ** fp -- file to put it on. 7859382Seric ** m -- mailer to use. 7869382Seric ** e -- envelope to use. 7879382Seric ** 7889382Seric ** Returns: 7899382Seric ** none. 7909382Seric ** 7919382Seric ** Side Effects: 7929382Seric ** none. 7939382Seric */ 7949382Seric 79557589Seric /* 79657589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 79757589Seric */ 79857589Seric #ifndef MAX 79957589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 80057589Seric #endif 80157589Seric 80210176Seric putheader(fp, m, e) 8039382Seric register FILE *fp; 8049382Seric register MAILER *m; 8059382Seric register ENVELOPE *e; 8069382Seric { 80757135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 8089382Seric register HDR *h; 80957135Seric char obuf[MAXLINE]; 8109382Seric 81159882Seric if (tTd(34, 1)) 81259882Seric printf("--- putheader, mailer = %s ---\n", m->m_name); 81359882Seric 8149382Seric for (h = e->e_header; h != NULL; h = h->h_link) 8159382Seric { 8169382Seric register char *p; 81710689Seric extern bool bitintersect(); 8189382Seric 81959882Seric if (tTd(34, 11)) 82059882Seric { 82159882Seric printf(" %s: ", h->h_field); 82259882Seric xputs(h->h_value); 82359882Seric } 82459882Seric 8259382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 82610689Seric !bitintersect(h->h_mflags, m->m_flags)) 82759882Seric { 82859882Seric if (tTd(34, 11)) 82959882Seric printf(" (skipped)\n"); 8309382Seric continue; 83159882Seric } 8329382Seric 83311414Seric /* handle Resent-... headers specially */ 83411414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 83559882Seric { 83659882Seric if (tTd(34, 11)) 83759882Seric printf(" (skipped (resent))\n"); 83811414Seric continue; 83959882Seric } 84059882Seric if (tTd(34, 11)) 84159882Seric printf("\n"); 84211414Seric 8439382Seric p = h->h_value; 8449382Seric if (bitset(H_DEFAULT, h->h_flags)) 8459382Seric { 8469382Seric /* macro expand value if generated internally */ 8479382Seric expand(p, buf, &buf[sizeof buf], e); 8489382Seric p = buf; 8499382Seric if (p == NULL || *p == '\0') 8509382Seric continue; 8519382Seric } 8529382Seric 8539382Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 8549382Seric { 8559382Seric /* address field */ 8569382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 8579382Seric 8589382Seric if (bitset(H_FROM, h->h_flags)) 8599382Seric oldstyle = FALSE; 86055012Seric commaize(h, p, fp, oldstyle, m, e); 8619382Seric } 8629382Seric else 8639382Seric { 8649382Seric /* vanilla header line */ 86512159Seric register char *nlp; 86612159Seric 86759579Seric (void) sprintf(obuf, "%s: ", h->h_field); 86856795Seric while ((nlp = strchr(p, '\n')) != NULL) 86912159Seric { 87012159Seric *nlp = '\0'; 87112159Seric (void) strcat(obuf, p); 87212159Seric *nlp = '\n'; 87312159Seric putline(obuf, fp, m); 87412159Seric p = ++nlp; 87512161Seric obuf[0] = '\0'; 87612159Seric } 87712159Seric (void) strcat(obuf, p); 87810176Seric putline(obuf, fp, m); 8799382Seric } 8809382Seric } 8819382Seric } 8829382Seric /* 8839382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 8849382Seric ** 8859382Seric ** Parameters: 8869382Seric ** h -- the header field to output. 8879382Seric ** p -- the value to put in it. 8889382Seric ** fp -- file to put it to. 8899382Seric ** oldstyle -- TRUE if this is an old style header. 8909382Seric ** m -- a pointer to the mailer descriptor. If NULL, 8919382Seric ** don't transform the name at all. 89255012Seric ** e -- the envelope containing the message. 8939382Seric ** 8949382Seric ** Returns: 8959382Seric ** none. 8969382Seric ** 8979382Seric ** Side Effects: 8989382Seric ** outputs "p" to file "fp". 8999382Seric */ 9009382Seric 90155012Seric commaize(h, p, fp, oldstyle, m, e) 9029382Seric register HDR *h; 9039382Seric register char *p; 9049382Seric FILE *fp; 9059382Seric bool oldstyle; 9069382Seric register MAILER *m; 90755012Seric register ENVELOPE *e; 9089382Seric { 9099382Seric register char *obp; 9109382Seric int opos; 9119382Seric bool firstone = TRUE; 91211157Seric char obuf[MAXLINE + 3]; 9139382Seric 9149382Seric /* 9159382Seric ** Output the address list translated by the 9169382Seric ** mailer and with commas. 9179382Seric */ 9189382Seric 9199382Seric if (tTd(14, 2)) 9209382Seric printf("commaize(%s: %s)\n", h->h_field, p); 9219382Seric 9229382Seric obp = obuf; 92359579Seric (void) sprintf(obp, "%s: ", h->h_field); 9249382Seric opos = strlen(h->h_field) + 2; 9259382Seric obp += opos; 9269382Seric 9279382Seric /* 9289382Seric ** Run through the list of values. 9299382Seric */ 9309382Seric 9319382Seric while (*p != '\0') 9329382Seric { 9339382Seric register char *name; 93454983Seric register int c; 9359382Seric char savechar; 93659163Seric int flags; 93759163Seric auto int stat; 9389382Seric 9399382Seric /* 9409382Seric ** Find the end of the name. New style names 9419382Seric ** end with a comma, old style names end with 9429382Seric ** a space character. However, spaces do not 9439382Seric ** necessarily delimit an old-style name -- at 9449382Seric ** signs mean keep going. 9459382Seric */ 9469382Seric 9479382Seric /* find end of name */ 94858050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 9499382Seric p++; 9509382Seric name = p; 9519382Seric for (;;) 9529382Seric { 95358333Seric auto char *oldp; 95416909Seric char pvpbuf[PSBUFSIZE]; 9559382Seric 95658333Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp); 95758333Seric p = oldp; 9589382Seric 9599382Seric /* look to see if we have an at sign */ 96058050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 9619382Seric p++; 9629382Seric 96358170Seric if (*p != '@') 9649382Seric { 9659382Seric p = oldp; 9669382Seric break; 9679382Seric } 9689382Seric p += *p == '@' ? 1 : 2; 96958050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 9709382Seric p++; 9719382Seric } 9729382Seric /* at the end of one complete name */ 9739382Seric 9749382Seric /* strip off trailing white space */ 97558050Seric while (p >= name && 97658050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 9779382Seric p--; 9789382Seric if (++p == name) 9799382Seric continue; 9809382Seric savechar = *p; 9819382Seric *p = '\0'; 9829382Seric 9839382Seric /* translate the name to be relative */ 98459163Seric flags = RF_HEADERADDR|RF_ADDDOMAIN; 98559163Seric if (bitset(H_FROM, h->h_flags)) 98659163Seric flags |= RF_SENDERADDR; 98759163Seric stat = EX_OK; 98859163Seric name = remotename(name, m, flags, &stat, e); 9899382Seric if (*name == '\0') 9909382Seric { 9919382Seric *p = savechar; 9929382Seric continue; 9939382Seric } 9949382Seric 9959382Seric /* output the name with nice formatting */ 99654983Seric opos += strlen(name); 9979382Seric if (!firstone) 9989382Seric opos += 2; 9999382Seric if (opos > 78 && !firstone) 10009382Seric { 100110178Seric (void) strcpy(obp, ",\n"); 100210176Seric putline(obuf, fp, m); 10039382Seric obp = obuf; 10049382Seric (void) sprintf(obp, " "); 100510161Seric opos = strlen(obp); 100610161Seric obp += opos; 100754983Seric opos += strlen(name); 10089382Seric } 10099382Seric else if (!firstone) 10109382Seric { 10119382Seric (void) sprintf(obp, ", "); 10129382Seric obp += 2; 10139382Seric } 10149382Seric 101554983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 101654983Seric *obp++ = c; 10179382Seric firstone = FALSE; 10189382Seric *p = savechar; 10199382Seric } 10209382Seric (void) strcpy(obp, "\n"); 102110176Seric putline(obuf, fp, m); 10229382Seric } 10239382Seric /* 102458170Seric ** COPYHEADER -- copy header list 10259382Seric ** 102658170Seric ** This routine is the equivalent of newstr for header lists 102758170Seric ** 10289382Seric ** Parameters: 102958170Seric ** header -- list of header structures to copy. 10309382Seric ** 10319382Seric ** Returns: 103258170Seric ** a copy of 'header'. 10339382Seric ** 10349382Seric ** Side Effects: 10359382Seric ** none. 10369382Seric */ 10379382Seric 103858170Seric HDR * 103958170Seric copyheader(header) 104058170Seric register HDR *header; 10419382Seric { 104258170Seric register HDR *newhdr; 104358170Seric HDR *ret; 104458170Seric register HDR **tail = &ret; 10459382Seric 104658170Seric while (header != NULL) 104758170Seric { 104858170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 104958170Seric STRUCTCOPY(*header, *newhdr); 105058170Seric *tail = newhdr; 105158170Seric tail = &newhdr->h_link; 105258170Seric header = header->h_link; 105358170Seric } 105458170Seric *tail = NULL; 105558170Seric 105658170Seric return ret; 10579382Seric } 1058