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*64351Seric static char sccsid[] = "@(#)headers.c 8.11 (Berkeley) 08/25/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; 47*64351Seric 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 11411414Seric /* drop explicit From: if same as what we would generate -- for MH */ 11514784Seric p = "resent-from"; 11655012Seric if (!bitset(EF_RESENT, e->e_flags)) 11714784Seric p += 7; 11859579Seric if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0) 11911414Seric { 12063753Seric if (tTd(31, 2)) 12163753Seric { 12263753Seric printf("comparing header from (%s) against default (%s or %s)\n", 12363753Seric fvalue, e->e_from.q_paddr, e->e_from.q_user); 12463753Seric } 12555012Seric if (e->e_from.q_paddr != NULL && 12663753Seric (strcmp(fvalue, e->e_from.q_paddr) == 0 || 12763753Seric strcmp(fvalue, e->e_from.q_user) == 0)) 12811414Seric return (hi->hi_flags); 129*64351Seric #ifdef MAYBENEXTRELEASE /* XXX UNTESTED XXX UNTESTED XXX UNTESTED XXX */ 130*64351Seric #ifdef USERDB 131*64351Seric else 132*64351Seric { 133*64351Seric auto ADDRESS a; 134*64351Seric char *fancy; 135*64351Seric extern char *crackaddr(); 136*64351Seric extern char *udbsender(); 137*64351Seric 138*64351Seric fancy = crackaddr(fvalue); 139*64351Seric if (parseaddr(fvalue, &a, RF_COPYNONE, '\0', NULL, e) != NULL && 140*64351Seric a.q_mailer == LocalMailer && 141*64351Seric (p = udbsender(a.q_user)) != NULL) 142*64351Seric { 143*64351Seric char *oldg = macvalue('g', e); 144*64351Seric 145*64351Seric define('g', p, e); 146*64351Seric expand(fancy, buf, &buf[sizeof buf], e); 147*64351Seric define('g', oldg, e); 148*64351Seric fvalue = buf; 149*64351Seric } 150*64351Seric } 151*64351Seric #endif 152*64351Seric #endif 15311414Seric } 15411414Seric 15514784Seric /* delete default value for this header */ 15655012Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 15714784Seric { 15859579Seric if (strcasecmp(fname, h->h_field) == 0 && 15914784Seric bitset(H_DEFAULT, h->h_flags) && 16014784Seric !bitset(H_FORCE, h->h_flags)) 16114784Seric h->h_value = NULL; 16214784Seric } 16314784Seric 16413012Seric /* create a new node */ 16513012Seric h = (HDR *) xalloc(sizeof *h); 16613012Seric h->h_field = newstr(fname); 16764134Seric h->h_value = newstr(fvalue); 16813012Seric h->h_link = NULL; 16923117Seric bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts); 17013012Seric *hp = h; 1718066Seric h->h_flags = hi->hi_flags; 1724091Seric if (def) 1734091Seric h->h_flags |= H_DEFAULT; 1749059Seric if (cond) 1759059Seric h->h_flags |= H_CHECK; 1764091Seric 1775937Seric /* hack to see if this is a new format message */ 1788095Seric if (!def && bitset(H_RCPT|H_FROM, h->h_flags) && 17956795Seric (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL || 18056795Seric strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL)) 1818089Seric { 18255012Seric e->e_flags &= ~EF_OLDSTYLE; 1838089Seric } 1845937Seric 1854091Seric return (h->h_flags); 1864091Seric } 1874091Seric /* 1886980Seric ** ADDHEADER -- add a header entry to the end of the queue. 1896980Seric ** 1906980Seric ** This bypasses the special checking of chompheader. 1916980Seric ** 1926980Seric ** Parameters: 19359579Seric ** field -- the name of the header field. 19458789Seric ** value -- the value of the field. 1956980Seric ** e -- the envelope to add them to. 1966980Seric ** 1976980Seric ** Returns: 1986980Seric ** none. 1996980Seric ** 2006980Seric ** Side Effects: 2016980Seric ** adds the field on the list of headers for this envelope. 2026980Seric */ 2036980Seric 2046980Seric addheader(field, value, e) 2056980Seric char *field; 2066980Seric char *value; 2076980Seric ENVELOPE *e; 2086980Seric { 2096980Seric register HDR *h; 2106980Seric register struct hdrinfo *hi; 2116980Seric HDR **hp; 2126980Seric 2136980Seric /* find info struct */ 2146980Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 2156980Seric { 21659579Seric if (strcasecmp(field, hi->hi_field) == 0) 2176980Seric break; 2186980Seric } 2196980Seric 2206980Seric /* find current place in list -- keep back pointer? */ 2216980Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 2226980Seric { 22359579Seric if (strcasecmp(field, h->h_field) == 0) 2246980Seric break; 2256980Seric } 2266980Seric 2276980Seric /* allocate space for new header */ 2286980Seric h = (HDR *) xalloc(sizeof *h); 2296980Seric h->h_field = field; 2306980Seric h->h_value = newstr(value); 2317368Seric h->h_link = *hp; 2326980Seric h->h_flags = hi->hi_flags | H_DEFAULT; 23310689Seric clrbitmap(h->h_mflags); 2346980Seric *hp = h; 2356980Seric } 2366980Seric /* 2374091Seric ** HVALUE -- return value of a header. 2384091Seric ** 2394091Seric ** Only "real" fields (i.e., ones that have not been supplied 2404091Seric ** as a default) are used. 2414091Seric ** 2424091Seric ** Parameters: 2434091Seric ** field -- the field name. 24455012Seric ** e -- the envelope containing the header. 2454091Seric ** 2464091Seric ** Returns: 2474091Seric ** pointer to the value part. 2484091Seric ** NULL if not found. 2494091Seric ** 2504091Seric ** Side Effects: 2519382Seric ** none. 2524091Seric */ 2534091Seric 2544091Seric char * 25555012Seric hvalue(field, e) 2564091Seric char *field; 25755012Seric register ENVELOPE *e; 2584091Seric { 2594091Seric register HDR *h; 2604091Seric 26155012Seric for (h = e->e_header; h != NULL; h = h->h_link) 2624091Seric { 26359579Seric if (!bitset(H_DEFAULT, h->h_flags) && 26459579Seric strcasecmp(h->h_field, field) == 0) 2654091Seric return (h->h_value); 2664091Seric } 2674091Seric return (NULL); 2684091Seric } 2694091Seric /* 2704091Seric ** ISHEADER -- predicate telling if argument is a header. 2714091Seric ** 2724319Seric ** A line is a header if it has a single word followed by 2734319Seric ** optional white space followed by a colon. 2744319Seric ** 2754091Seric ** Parameters: 2764091Seric ** s -- string to check for possible headerness. 2774091Seric ** 2784091Seric ** Returns: 2794091Seric ** TRUE if s is a header. 2804091Seric ** FALSE otherwise. 2814091Seric ** 2824091Seric ** Side Effects: 2834091Seric ** none. 2844091Seric */ 2854091Seric 2864091Seric bool 2874091Seric isheader(s) 2884091Seric register char *s; 2894091Seric { 2909382Seric while (*s > ' ' && *s != ':' && *s != '\0') 2914091Seric s++; 2929382Seric 2939382Seric /* following technically violates RFC822 */ 29458050Seric while (isascii(*s) && isspace(*s)) 2954091Seric s++; 2969382Seric 2974091Seric return (*s == ':'); 2984091Seric } 2995919Seric /* 3007783Seric ** EATHEADER -- run through the stored header and extract info. 3017783Seric ** 3027783Seric ** Parameters: 3039382Seric ** e -- the envelope to process. 30458929Seric ** full -- if set, do full processing (e.g., compute 30558929Seric ** message priority). 3067783Seric ** 3077783Seric ** Returns: 3087783Seric ** none. 3097783Seric ** 3107783Seric ** Side Effects: 3117783Seric ** Sets a bunch of global variables from information 3129382Seric ** in the collected header. 3139382Seric ** Aborts the message if the hop count is exceeded. 3147783Seric */ 3157783Seric 31658929Seric eatheader(e, full) 3179382Seric register ENVELOPE *e; 31858929Seric bool full; 3197783Seric { 3207783Seric register HDR *h; 3217783Seric register char *p; 3229382Seric int hopcnt = 0; 32357208Seric char *msgid; 32458688Seric char buf[MAXLINE]; 3257783Seric 32658688Seric /* 32758688Seric ** Set up macros for possible expansion in headers. 32858688Seric */ 32958688Seric 33058704Seric define('f', e->e_sender, e); 33158704Seric define('g', e->e_sender, e); 33264148Seric if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0') 33364148Seric define('u', e->e_origrcpt, e); 33464148Seric else 33564148Seric define('u', NULL, e); 33658688Seric 3379382Seric if (tTd(32, 1)) 3389382Seric printf("----- collected header -----\n"); 33957208Seric msgid = "<none>"; 3409382Seric for (h = e->e_header; h != NULL; h = h->h_link) 3417783Seric { 34264156Seric if (h->h_value == NULL) 34364156Seric { 34464156Seric if (tTd(32, 1)) 34564156Seric printf("%s: <NULL>\n", h->h_field); 34664156Seric continue; 34764156Seric } 34864156Seric 34958688Seric /* do early binding */ 35064156Seric if (bitset(H_DEFAULT, h->h_flags)) 35158688Seric { 35258688Seric expand(h->h_value, buf, &buf[sizeof buf], e); 35358688Seric if (buf[0] != '\0') 35458688Seric { 35558688Seric h->h_value = newstr(buf); 35658688Seric h->h_flags &= ~H_DEFAULT; 35758688Seric } 35858688Seric } 35958688Seric 3609382Seric if (tTd(32, 1)) 36159579Seric printf("%s: %s\n", h->h_field, h->h_value); 36257359Seric 36311414Seric /* count the number of times it has been processed */ 3649382Seric if (bitset(H_TRACE, h->h_flags)) 3659382Seric hopcnt++; 36611414Seric 36711414Seric /* send to this person if we so desire */ 36811414Seric if (GrabTo && bitset(H_RCPT, h->h_flags) && 36911414Seric !bitset(H_DEFAULT, h->h_flags) && 37055012Seric (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags))) 37111414Seric { 37263839Seric int saveflags = e->e_flags; 37363839Seric 37464283Seric (void) sendtolist(h->h_value, NULLADDR, 37558082Seric &e->e_sendqueue, e); 37663839Seric 37763839Seric /* delete fatal errors generated by this address */ 37864148Seric if (!GrabTo && !bitset(EF_FATALERRS, saveflags)) 37963839Seric e->e_flags &= ~EF_FATALERRS; 38011414Seric } 38111414Seric 38257208Seric /* save the message-id for logging */ 38364156Seric if (full && strcasecmp(h->h_field, "message-id") == 0) 38411290Seric { 38557208Seric msgid = h->h_value; 38659859Seric while (isascii(*msgid) && isspace(*msgid)) 38759859Seric msgid++; 38811290Seric } 38957359Seric 39057359Seric /* see if this is a return-receipt header */ 39157359Seric if (bitset(H_RECEIPTTO, h->h_flags)) 39257359Seric e->e_receiptto = h->h_value; 39357359Seric 39457359Seric /* see if this is an errors-to header */ 39561104Seric if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags)) 39664283Seric (void) sendtolist(h->h_value, NULLADDR, 39758082Seric &e->e_errorqueue, e); 3989382Seric } 3999382Seric if (tTd(32, 1)) 4007783Seric printf("----------------------------\n"); 4017783Seric 40258145Seric /* if we are just verifying (that is, sendmail -t -bv), drop out now */ 40358145Seric if (OpMode == MD_VERIFY) 40458145Seric return; 40558145Seric 4069382Seric /* store hop count */ 4079382Seric if (hopcnt > e->e_hopcount) 4089382Seric e->e_hopcount = hopcnt; 4099382Seric 4107783Seric /* message priority */ 41155012Seric p = hvalue("precedence", e); 4129382Seric if (p != NULL) 4139382Seric e->e_class = priencode(p); 41458929Seric if (full) 41525013Seric e->e_msgpriority = e->e_msgsize 41624981Seric - e->e_class * WkClassFact 41724981Seric + e->e_nrcpts * WkRecipFact; 4187783Seric 4197783Seric /* full name of from person */ 42055012Seric p = hvalue("full-name", e); 4217783Seric if (p != NULL) 4229382Seric define('x', p, e); 4237783Seric 4247783Seric /* date message originated */ 42555012Seric p = hvalue("posted-date", e); 4267783Seric if (p == NULL) 42755012Seric p = hvalue("date", e); 4287783Seric if (p != NULL) 4299382Seric define('a', p, e); 43011290Seric 43111290Seric /* 43211290Seric ** Log collection information. 43311290Seric */ 43411290Seric 43511290Seric # ifdef LOG 43658929Seric if (full && LogLevel > 4) 43711290Seric { 43857359Seric char *name; 43960575Seric register char *sbp; 44057232Seric char hbuf[MAXNAME]; 44157359Seric char sbuf[MAXLINE]; 44236230Skarels 44358667Seric if (bitset(EF_RESPONSE, e->e_flags)) 44458667Seric name = "[RESPONSE]"; 44558951Seric else if ((name = macvalue('_', e)) != NULL) 44658951Seric ; 44758667Seric else if (RealHostName[0] == '[') 44836230Skarels name = RealHostName; 44936230Skarels else 45057359Seric { 45157359Seric name = hbuf; 45258798Seric (void) sprintf(hbuf, "%.80s", RealHostName); 45358798Seric if (RealHostAddr.sa.sa_family != 0) 45458798Seric { 45558798Seric p = &hbuf[strlen(hbuf)]; 45658798Seric (void) sprintf(p, " (%s)", 45758798Seric anynet_ntoa(&RealHostAddr)); 45858798Seric } 45957359Seric } 46057359Seric 46157359Seric /* some versions of syslog only take 5 printf args */ 46260575Seric sbp = sbuf; 46360575Seric sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s", 46458558Seric e->e_from.q_paddr, e->e_msgsize, e->e_class, 46557589Seric e->e_msgpriority, e->e_nrcpts, msgid); 46660575Seric sbp += strlen(sbp); 46760575Seric if (e->e_bodytype != NULL) 46860575Seric { 46960575Seric (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype); 47060575Seric sbp += strlen(sbp); 47160575Seric } 47260575Seric p = macvalue('r', e); 47360575Seric if (p != NULL) 47460575Seric (void) sprintf(sbp, ", proto=%.20s", p); 47558686Seric syslog(LOG_INFO, "%s: %s, relay=%s", 47657359Seric e->e_id, sbuf, name); 47711290Seric } 47856795Seric # endif /* LOG */ 4797783Seric } 4807783Seric /* 4817783Seric ** PRIENCODE -- encode external priority names into internal values. 4827783Seric ** 4837783Seric ** Parameters: 4847783Seric ** p -- priority in ascii. 4857783Seric ** 4867783Seric ** Returns: 4877783Seric ** priority as a numeric level. 4887783Seric ** 4897783Seric ** Side Effects: 4907783Seric ** none. 4917783Seric */ 4927783Seric 4937783Seric priencode(p) 4947783Seric char *p; 4957783Seric { 4968253Seric register int i; 4977783Seric 4988253Seric for (i = 0; i < NumPriorities; i++) 4997783Seric { 50033725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 5018253Seric return (Priorities[i].pri_val); 5027783Seric } 5038253Seric 5048253Seric /* unknown priority */ 5058253Seric return (0); 5067783Seric } 5077783Seric /* 5087890Seric ** CRACKADDR -- parse an address and turn it into a macro 5097783Seric ** 5107783Seric ** This doesn't actually parse the address -- it just extracts 5117783Seric ** it and replaces it with "$g". The parse is totally ad hoc 5127783Seric ** and isn't even guaranteed to leave something syntactically 5137783Seric ** identical to what it started with. However, it does leave 5147783Seric ** something semantically identical. 5157783Seric ** 51651379Seric ** This algorithm has been cleaned up to handle a wider range 51751379Seric ** of cases -- notably quoted and backslash escaped strings. 51851379Seric ** This modification makes it substantially better at preserving 51951379Seric ** the original syntax. 5207783Seric ** 5217783Seric ** Parameters: 5227890Seric ** addr -- the address to be cracked. 5237783Seric ** 5247783Seric ** Returns: 5257783Seric ** a pointer to the new version. 5267783Seric ** 5277783Seric ** Side Effects: 5287890Seric ** none. 5297783Seric ** 5307783Seric ** Warning: 5317783Seric ** The return value is saved in local storage and should 5327783Seric ** be copied if it is to be reused. 5337783Seric */ 5347783Seric 5357783Seric char * 5367890Seric crackaddr(addr) 5377890Seric register char *addr; 5387783Seric { 5397783Seric register char *p; 54051379Seric register char c; 54151379Seric int cmtlev; 54256764Seric int realcmtlev; 54356764Seric int anglelev, realanglelev; 54451379Seric int copylev; 54551379Seric bool qmode; 54656764Seric bool realqmode; 54756764Seric bool skipping; 54851379Seric bool putgmac = FALSE; 54956735Seric bool quoteit = FALSE; 55064148Seric bool gotangle = FALSE; 55151379Seric register char *bp; 55256764Seric char *buflim; 5537783Seric static char buf[MAXNAME]; 5547783Seric 5557783Seric if (tTd(33, 1)) 5567890Seric printf("crackaddr(%s)\n", addr); 5577783Seric 5588082Seric /* strip leading spaces */ 55958050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 5608082Seric addr++; 5618082Seric 5627783Seric /* 56351379Seric ** Start by assuming we have no angle brackets. This will be 56451379Seric ** adjusted later if we find them. 5657783Seric */ 5667783Seric 56751379Seric bp = buf; 56856764Seric buflim = &buf[sizeof buf - 5]; 56951379Seric p = addr; 57056764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 57156764Seric qmode = realqmode = FALSE; 57251379Seric 57351379Seric while ((c = *p++) != '\0') 5747783Seric { 57556764Seric /* 57656764Seric ** If the buffer is overful, go into a special "skipping" 57756764Seric ** mode that tries to keep legal syntax but doesn't actually 57856764Seric ** output things. 57956764Seric */ 5807783Seric 58156764Seric skipping = bp >= buflim; 58256735Seric 58356764Seric if (copylev > 0 && !skipping) 58456764Seric *bp++ = c; 58556735Seric 58651379Seric /* check for backslash escapes */ 58751379Seric if (c == '\\') 5887783Seric { 58958890Seric /* arrange to quote the address */ 59058890Seric if (cmtlev <= 0 && !qmode) 59158890Seric quoteit = TRUE; 59258890Seric 59351379Seric if ((c = *p++) == '\0') 5947783Seric { 59551379Seric /* too far */ 59651379Seric p--; 59751379Seric goto putg; 5987783Seric } 59956764Seric if (copylev > 0 && !skipping) 60051379Seric *bp++ = c; 60151379Seric goto putg; 6027783Seric } 6037783Seric 60451379Seric /* check for quoted strings */ 60551379Seric if (c == '"') 6067783Seric { 60751379Seric qmode = !qmode; 60856764Seric if (copylev > 0 && !skipping) 60956764Seric realqmode = !realqmode; 61051379Seric continue; 6117783Seric } 61251379Seric if (qmode) 61351379Seric goto putg; 6147783Seric 61551379Seric /* check for comments */ 61651379Seric if (c == '(') 6177783Seric { 61851379Seric cmtlev++; 61956764Seric 62056764Seric /* allow space for closing paren */ 62156764Seric if (!skipping) 62256764Seric { 62356764Seric buflim--; 62456764Seric realcmtlev++; 62556764Seric if (copylev++ <= 0) 62656764Seric { 62756764Seric *bp++ = ' '; 62856764Seric *bp++ = c; 62956764Seric } 63056764Seric } 63151379Seric } 63251379Seric if (cmtlev > 0) 63351379Seric { 63451379Seric if (c == ')') 6357783Seric { 63651379Seric cmtlev--; 63751379Seric copylev--; 63856764Seric if (!skipping) 63956764Seric { 64056764Seric realcmtlev--; 64156764Seric buflim++; 64256764Seric } 6437783Seric } 6447783Seric continue; 6457783Seric } 64656764Seric else if (c == ')') 64756764Seric { 64856764Seric /* syntax error: unmatched ) */ 64956764Seric if (!skipping) 65056764Seric bp--; 65156764Seric } 6527783Seric 65356764Seric 65456764Seric /* check for characters that may have to be quoted */ 65564148Seric if (strchr(".'@,;:\\()[]", c) != NULL) 65656764Seric { 65756764Seric /* 65856764Seric ** If these occur as the phrase part of a <> 65956764Seric ** construct, but are not inside of () or already 66056764Seric ** quoted, they will have to be quoted. Note that 66156764Seric ** now (but don't actually do the quoting). 66256764Seric */ 66356764Seric 66456764Seric if (cmtlev <= 0 && !qmode) 66556764Seric quoteit = TRUE; 66656764Seric } 66756764Seric 66851379Seric /* check for angle brackets */ 66951379Seric if (c == '<') 67051379Seric { 67156735Seric register char *q; 67256735Seric 67364148Seric /* assume first of two angles is bogus */ 67464148Seric if (gotangle) 67564148Seric quoteit = TRUE; 67664148Seric gotangle = TRUE; 67764148Seric 67851379Seric /* oops -- have to change our mind */ 67956764Seric anglelev++; 68056764Seric if (!skipping) 68156764Seric realanglelev++; 68256764Seric 68356735Seric bp = buf; 68456735Seric if (quoteit) 68556735Seric { 68656735Seric *bp++ = '"'; 68756735Seric 68856735Seric /* back up over the '<' and any spaces */ 68956735Seric --p; 69058050Seric while (isascii(*--p) && isspace(*p)) 69156735Seric continue; 69256735Seric p++; 69356735Seric } 69456735Seric for (q = addr; q < p; ) 69556735Seric { 69656735Seric c = *q++; 69756764Seric if (bp < buflim) 69856764Seric { 69956764Seric if (quoteit && c == '"') 70056764Seric *bp++ = '\\'; 70156764Seric *bp++ = c; 70256764Seric } 70356735Seric } 70456735Seric if (quoteit) 70556735Seric { 70664148Seric if (bp == &buf[1]) 70764148Seric bp--; 70864148Seric else 70964148Seric *bp++ = '"'; 71056764Seric while ((c = *p++) != '<') 71156764Seric { 71256764Seric if (bp < buflim) 71356764Seric *bp++ = c; 71456764Seric } 71556764Seric *bp++ = c; 71656735Seric } 71751379Seric copylev = 0; 71856735Seric putgmac = quoteit = FALSE; 71951379Seric continue; 72051379Seric } 7217783Seric 72251379Seric if (c == '>') 7237783Seric { 72456764Seric if (anglelev > 0) 72556764Seric { 72656764Seric anglelev--; 72756764Seric if (!skipping) 72856764Seric { 72956764Seric realanglelev--; 73056764Seric buflim++; 73156764Seric } 73256764Seric } 73356764Seric else if (!skipping) 73456764Seric { 73556764Seric /* syntax error: unmatched > */ 73656764Seric if (copylev > 0) 73756764Seric bp--; 73856764Seric continue; 73956764Seric } 74051379Seric if (copylev++ <= 0) 74151379Seric *bp++ = c; 74251379Seric continue; 7437783Seric } 74451379Seric 74551379Seric /* must be a real address character */ 74651379Seric putg: 74751379Seric if (copylev <= 0 && !putgmac) 74851379Seric { 74958050Seric *bp++ = MACROEXPAND; 75051379Seric *bp++ = 'g'; 75151379Seric putgmac = TRUE; 75251379Seric } 7537783Seric } 7547783Seric 75556764Seric /* repair any syntactic damage */ 75656764Seric if (realqmode) 75756764Seric *bp++ = '"'; 75856764Seric while (realcmtlev-- > 0) 75956764Seric *bp++ = ')'; 76056764Seric while (realanglelev-- > 0) 76156764Seric *bp++ = '>'; 76251379Seric *bp++ = '\0'; 7637783Seric 7647783Seric if (tTd(33, 1)) 7657944Seric printf("crackaddr=>`%s'\n", buf); 7667783Seric 7677783Seric return (buf); 7687783Seric } 7699382Seric /* 7709382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 7719382Seric ** 7729382Seric ** Parameters: 7739382Seric ** fp -- file to put it on. 7749382Seric ** m -- mailer to use. 7759382Seric ** e -- envelope to use. 7769382Seric ** 7779382Seric ** Returns: 7789382Seric ** none. 7799382Seric ** 7809382Seric ** Side Effects: 7819382Seric ** none. 7829382Seric */ 7839382Seric 78457589Seric /* 78557589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 78657589Seric */ 78757589Seric #ifndef MAX 78857589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 78957589Seric #endif 79057589Seric 79110176Seric putheader(fp, m, e) 7929382Seric register FILE *fp; 7939382Seric register MAILER *m; 7949382Seric register ENVELOPE *e; 7959382Seric { 79657135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 7979382Seric register HDR *h; 79857135Seric char obuf[MAXLINE]; 7999382Seric 80059882Seric if (tTd(34, 1)) 80159882Seric printf("--- putheader, mailer = %s ---\n", m->m_name); 80259882Seric 8039382Seric for (h = e->e_header; h != NULL; h = h->h_link) 8049382Seric { 8059382Seric register char *p; 80610689Seric extern bool bitintersect(); 8079382Seric 80859882Seric if (tTd(34, 11)) 80959882Seric { 81059882Seric printf(" %s: ", h->h_field); 81159882Seric xputs(h->h_value); 81259882Seric } 81359882Seric 8149382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 81510689Seric !bitintersect(h->h_mflags, m->m_flags)) 81659882Seric { 81759882Seric if (tTd(34, 11)) 81859882Seric printf(" (skipped)\n"); 8199382Seric continue; 82059882Seric } 8219382Seric 82211414Seric /* handle Resent-... headers specially */ 82311414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 82459882Seric { 82559882Seric if (tTd(34, 11)) 82659882Seric printf(" (skipped (resent))\n"); 82711414Seric continue; 82859882Seric } 82959882Seric if (tTd(34, 11)) 83059882Seric printf("\n"); 83111414Seric 8329382Seric p = h->h_value; 8339382Seric if (bitset(H_DEFAULT, h->h_flags)) 8349382Seric { 8359382Seric /* macro expand value if generated internally */ 8369382Seric expand(p, buf, &buf[sizeof buf], e); 8379382Seric p = buf; 8389382Seric if (p == NULL || *p == '\0') 8399382Seric continue; 8409382Seric } 8419382Seric 8429382Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 8439382Seric { 8449382Seric /* address field */ 8459382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 8469382Seric 8479382Seric if (bitset(H_FROM, h->h_flags)) 8489382Seric oldstyle = FALSE; 84955012Seric commaize(h, p, fp, oldstyle, m, e); 8509382Seric } 8519382Seric else 8529382Seric { 8539382Seric /* vanilla header line */ 85412159Seric register char *nlp; 85512159Seric 85659579Seric (void) sprintf(obuf, "%s: ", h->h_field); 85756795Seric while ((nlp = strchr(p, '\n')) != NULL) 85812159Seric { 85912159Seric *nlp = '\0'; 86012159Seric (void) strcat(obuf, p); 86112159Seric *nlp = '\n'; 86212159Seric putline(obuf, fp, m); 86312159Seric p = ++nlp; 86412161Seric obuf[0] = '\0'; 86512159Seric } 86612159Seric (void) strcat(obuf, p); 86710176Seric putline(obuf, fp, m); 8689382Seric } 8699382Seric } 8709382Seric } 8719382Seric /* 8729382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 8739382Seric ** 8749382Seric ** Parameters: 8759382Seric ** h -- the header field to output. 8769382Seric ** p -- the value to put in it. 8779382Seric ** fp -- file to put it to. 8789382Seric ** oldstyle -- TRUE if this is an old style header. 8799382Seric ** m -- a pointer to the mailer descriptor. If NULL, 8809382Seric ** don't transform the name at all. 88155012Seric ** e -- the envelope containing the message. 8829382Seric ** 8839382Seric ** Returns: 8849382Seric ** none. 8859382Seric ** 8869382Seric ** Side Effects: 8879382Seric ** outputs "p" to file "fp". 8889382Seric */ 8899382Seric 89055012Seric commaize(h, p, fp, oldstyle, m, e) 8919382Seric register HDR *h; 8929382Seric register char *p; 8939382Seric FILE *fp; 8949382Seric bool oldstyle; 8959382Seric register MAILER *m; 89655012Seric register ENVELOPE *e; 8979382Seric { 8989382Seric register char *obp; 8999382Seric int opos; 9009382Seric bool firstone = TRUE; 90111157Seric char obuf[MAXLINE + 3]; 9029382Seric 9039382Seric /* 9049382Seric ** Output the address list translated by the 9059382Seric ** mailer and with commas. 9069382Seric */ 9079382Seric 9089382Seric if (tTd(14, 2)) 9099382Seric printf("commaize(%s: %s)\n", h->h_field, p); 9109382Seric 9119382Seric obp = obuf; 91259579Seric (void) sprintf(obp, "%s: ", h->h_field); 9139382Seric opos = strlen(h->h_field) + 2; 9149382Seric obp += opos; 9159382Seric 9169382Seric /* 9179382Seric ** Run through the list of values. 9189382Seric */ 9199382Seric 9209382Seric while (*p != '\0') 9219382Seric { 9229382Seric register char *name; 92354983Seric register int c; 9249382Seric char savechar; 92559163Seric int flags; 92659163Seric auto int stat; 9279382Seric 9289382Seric /* 9299382Seric ** Find the end of the name. New style names 9309382Seric ** end with a comma, old style names end with 9319382Seric ** a space character. However, spaces do not 9329382Seric ** necessarily delimit an old-style name -- at 9339382Seric ** signs mean keep going. 9349382Seric */ 9359382Seric 9369382Seric /* find end of name */ 93758050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 9389382Seric p++; 9399382Seric name = p; 9409382Seric for (;;) 9419382Seric { 94258333Seric auto char *oldp; 94316909Seric char pvpbuf[PSBUFSIZE]; 9449382Seric 94558333Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp); 94658333Seric p = oldp; 9479382Seric 9489382Seric /* look to see if we have an at sign */ 94958050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 9509382Seric p++; 9519382Seric 95258170Seric if (*p != '@') 9539382Seric { 9549382Seric p = oldp; 9559382Seric break; 9569382Seric } 9579382Seric p += *p == '@' ? 1 : 2; 95858050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 9599382Seric p++; 9609382Seric } 9619382Seric /* at the end of one complete name */ 9629382Seric 9639382Seric /* strip off trailing white space */ 96458050Seric while (p >= name && 96558050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 9669382Seric p--; 9679382Seric if (++p == name) 9689382Seric continue; 9699382Seric savechar = *p; 9709382Seric *p = '\0'; 9719382Seric 9729382Seric /* translate the name to be relative */ 97359163Seric flags = RF_HEADERADDR|RF_ADDDOMAIN; 97459163Seric if (bitset(H_FROM, h->h_flags)) 97559163Seric flags |= RF_SENDERADDR; 97659163Seric stat = EX_OK; 97759163Seric name = remotename(name, m, flags, &stat, e); 9789382Seric if (*name == '\0') 9799382Seric { 9809382Seric *p = savechar; 9819382Seric continue; 9829382Seric } 9839382Seric 9849382Seric /* output the name with nice formatting */ 98554983Seric opos += strlen(name); 9869382Seric if (!firstone) 9879382Seric opos += 2; 9889382Seric if (opos > 78 && !firstone) 9899382Seric { 99010178Seric (void) strcpy(obp, ",\n"); 99110176Seric putline(obuf, fp, m); 9929382Seric obp = obuf; 9939382Seric (void) sprintf(obp, " "); 99410161Seric opos = strlen(obp); 99510161Seric obp += opos; 99654983Seric opos += strlen(name); 9979382Seric } 9989382Seric else if (!firstone) 9999382Seric { 10009382Seric (void) sprintf(obp, ", "); 10019382Seric obp += 2; 10029382Seric } 10039382Seric 100454983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 100554983Seric *obp++ = c; 10069382Seric firstone = FALSE; 10079382Seric *p = savechar; 10089382Seric } 10099382Seric (void) strcpy(obp, "\n"); 101010176Seric putline(obuf, fp, m); 10119382Seric } 10129382Seric /* 101358170Seric ** COPYHEADER -- copy header list 10149382Seric ** 101558170Seric ** This routine is the equivalent of newstr for header lists 101658170Seric ** 10179382Seric ** Parameters: 101858170Seric ** header -- list of header structures to copy. 10199382Seric ** 10209382Seric ** Returns: 102158170Seric ** a copy of 'header'. 10229382Seric ** 10239382Seric ** Side Effects: 10249382Seric ** none. 10259382Seric */ 10269382Seric 102758170Seric HDR * 102858170Seric copyheader(header) 102958170Seric register HDR *header; 10309382Seric { 103158170Seric register HDR *newhdr; 103258170Seric HDR *ret; 103358170Seric register HDR **tail = &ret; 10349382Seric 103558170Seric while (header != NULL) 103658170Seric { 103758170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 103858170Seric STRUCTCOPY(*header, *newhdr); 103958170Seric *tail = newhdr; 104058170Seric tail = &newhdr->h_link; 104158170Seric header = header->h_link; 104258170Seric } 104358170Seric *tail = NULL; 104458170Seric 104558170Seric return ret; 10469382Seric } 1047