122706Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333729Sbostic * Copyright (c) 1988 Regents of the University of California. 433729Sbostic * All rights reserved. 533729Sbostic * 642826Sbostic * %sccs.include.redist.c% 733729Sbostic */ 822706Sdist 922706Sdist #ifndef lint 10*58798Seric static char sccsid[] = "@(#)headers.c 6.26 (Berkeley) 03/24/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; 474091Seric 487677Seric if (tTd(31, 6)) 497677Seric printf("chompheader: %s\n", line); 507677Seric 514627Seric /* strip off options */ 5210689Seric clrbitmap(mopts); 534627Seric p = line; 5457405Seric if (*p == '?') 554627Seric { 564627Seric /* have some */ 5756795Seric register char *q = strchr(p + 1, *p); 584627Seric 594627Seric if (q != NULL) 604627Seric { 614627Seric *q++ = '\0'; 6210689Seric while (*++p != '\0') 6310689Seric setbitn(*p, mopts); 644627Seric p = q; 654627Seric } 664627Seric else 6758151Seric usrerr("553 header syntax error, line \"%s\"", line); 689059Seric cond = TRUE; 694627Seric } 704627Seric 714091Seric /* find canonical name */ 724627Seric fname = p; 7356795Seric p = strchr(p, ':'); 7410118Seric if (p == NULL) 7510118Seric { 7658151Seric syserr("553 header syntax error, line \"%s\"", line); 7710118Seric return (0); 7810118Seric } 794091Seric fvalue = &p[1]; 8058050Seric while (isascii(*--p) && isspace(*p)) 814091Seric continue; 824091Seric *++p = '\0'; 834091Seric makelower(fname); 844091Seric 854091Seric /* strip field value on front */ 864091Seric if (*fvalue == ' ') 874091Seric fvalue++; 884091Seric 894091Seric /* see if it is a known type */ 904091Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 914091Seric { 924091Seric if (strcmp(hi->hi_field, fname) == 0) 934091Seric break; 944091Seric } 954091Seric 9611414Seric /* see if this is a resent message */ 9711930Seric if (!def && bitset(H_RESENT, hi->hi_flags)) 9855012Seric e->e_flags |= EF_RESENT; 9911414Seric 1004091Seric /* if this means "end of header" quit now */ 1014091Seric if (bitset(H_EOH, hi->hi_flags)) 1024091Seric return (hi->hi_flags); 1034091Seric 10411414Seric /* drop explicit From: if same as what we would generate -- for MH */ 10514784Seric p = "resent-from"; 10655012Seric if (!bitset(EF_RESENT, e->e_flags)) 10714784Seric p += 7; 10858737Seric if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcmp(fname, p) == 0) 10911414Seric { 11055012Seric if (e->e_from.q_paddr != NULL && 11155012Seric strcmp(fvalue, e->e_from.q_paddr) == 0) 11211414Seric return (hi->hi_flags); 11311414Seric } 11411414Seric 11514784Seric /* delete default value for this header */ 11655012Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 11714784Seric { 11814784Seric if (strcmp(fname, h->h_field) == 0 && 11914784Seric bitset(H_DEFAULT, h->h_flags) && 12014784Seric !bitset(H_FORCE, h->h_flags)) 12114784Seric h->h_value = NULL; 12214784Seric } 12314784Seric 12413012Seric /* create a new node */ 12513012Seric h = (HDR *) xalloc(sizeof *h); 12613012Seric h->h_field = newstr(fname); 12713012Seric h->h_value = NULL; 12813012Seric h->h_link = NULL; 12923117Seric bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts); 13013012Seric *hp = h; 1318066Seric h->h_flags = hi->hi_flags; 1324091Seric if (def) 1334091Seric h->h_flags |= H_DEFAULT; 1349059Seric if (cond) 1359059Seric h->h_flags |= H_CHECK; 1364091Seric if (h->h_value != NULL) 1379351Seric free((char *) h->h_value); 1388066Seric h->h_value = newstr(fvalue); 1394091Seric 1405937Seric /* hack to see if this is a new format message */ 1418095Seric if (!def && bitset(H_RCPT|H_FROM, h->h_flags) && 14256795Seric (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL || 14356795Seric strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL)) 1448089Seric { 14555012Seric e->e_flags &= ~EF_OLDSTYLE; 1468089Seric } 1475937Seric 1484091Seric return (h->h_flags); 1494091Seric } 1504091Seric /* 1516980Seric ** ADDHEADER -- add a header entry to the end of the queue. 1526980Seric ** 1536980Seric ** This bypasses the special checking of chompheader. 1546980Seric ** 1556980Seric ** Parameters: 15658789Seric ** field -- the name of the header field. It must be 15758789Seric ** lower-cased. 15858789Seric ** value -- the value of the field. 1596980Seric ** e -- the envelope to add them to. 1606980Seric ** 1616980Seric ** Returns: 1626980Seric ** none. 1636980Seric ** 1646980Seric ** Side Effects: 1656980Seric ** adds the field on the list of headers for this envelope. 1666980Seric */ 1676980Seric 1686980Seric addheader(field, value, e) 1696980Seric char *field; 1706980Seric char *value; 1716980Seric ENVELOPE *e; 1726980Seric { 1736980Seric register HDR *h; 1746980Seric register struct hdrinfo *hi; 1756980Seric HDR **hp; 1766980Seric 1776980Seric /* find info struct */ 1786980Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 1796980Seric { 1806980Seric if (strcmp(field, hi->hi_field) == 0) 1816980Seric break; 1826980Seric } 1836980Seric 1846980Seric /* find current place in list -- keep back pointer? */ 1856980Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 1866980Seric { 1876980Seric if (strcmp(field, h->h_field) == 0) 1886980Seric break; 1896980Seric } 1906980Seric 1916980Seric /* allocate space for new header */ 1926980Seric h = (HDR *) xalloc(sizeof *h); 1936980Seric h->h_field = field; 1946980Seric h->h_value = newstr(value); 1957368Seric h->h_link = *hp; 1966980Seric h->h_flags = hi->hi_flags | H_DEFAULT; 19710689Seric clrbitmap(h->h_mflags); 1986980Seric *hp = h; 1996980Seric } 2006980Seric /* 2014091Seric ** HVALUE -- return value of a header. 2024091Seric ** 2034091Seric ** Only "real" fields (i.e., ones that have not been supplied 2044091Seric ** as a default) are used. 2054091Seric ** 2064091Seric ** Parameters: 2074091Seric ** field -- the field name. 20855012Seric ** e -- the envelope containing the header. 2094091Seric ** 2104091Seric ** Returns: 2114091Seric ** pointer to the value part. 2124091Seric ** NULL if not found. 2134091Seric ** 2144091Seric ** Side Effects: 2159382Seric ** none. 2164091Seric */ 2174091Seric 2184091Seric char * 21955012Seric hvalue(field, e) 2204091Seric char *field; 22155012Seric register ENVELOPE *e; 2224091Seric { 2234091Seric register HDR *h; 2244091Seric 22555012Seric for (h = e->e_header; h != NULL; h = h->h_link) 2264091Seric { 2274091Seric if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0) 2284091Seric return (h->h_value); 2294091Seric } 2304091Seric return (NULL); 2314091Seric } 2324091Seric /* 2334091Seric ** ISHEADER -- predicate telling if argument is a header. 2344091Seric ** 2354319Seric ** A line is a header if it has a single word followed by 2364319Seric ** optional white space followed by a colon. 2374319Seric ** 2384091Seric ** Parameters: 2394091Seric ** s -- string to check for possible headerness. 2404091Seric ** 2414091Seric ** Returns: 2424091Seric ** TRUE if s is a header. 2434091Seric ** FALSE otherwise. 2444091Seric ** 2454091Seric ** Side Effects: 2464091Seric ** none. 2474091Seric */ 2484091Seric 2494091Seric bool 2504091Seric isheader(s) 2514091Seric register char *s; 2524091Seric { 2539382Seric while (*s > ' ' && *s != ':' && *s != '\0') 2544091Seric s++; 2559382Seric 2569382Seric /* following technically violates RFC822 */ 25758050Seric while (isascii(*s) && isspace(*s)) 2584091Seric s++; 2599382Seric 2604091Seric return (*s == ':'); 2614091Seric } 2625919Seric /* 2637783Seric ** EATHEADER -- run through the stored header and extract info. 2647783Seric ** 2657783Seric ** Parameters: 2669382Seric ** e -- the envelope to process. 2677783Seric ** 2687783Seric ** Returns: 2697783Seric ** none. 2707783Seric ** 2717783Seric ** Side Effects: 2727783Seric ** Sets a bunch of global variables from information 2739382Seric ** in the collected header. 2749382Seric ** Aborts the message if the hop count is exceeded. 2757783Seric */ 2767783Seric 27758737Seric eatheader(e) 2789382Seric register ENVELOPE *e; 2797783Seric { 2807783Seric register HDR *h; 2817783Seric register char *p; 2829382Seric int hopcnt = 0; 28357208Seric char *msgid; 28458688Seric char buf[MAXLINE]; 2857783Seric 28658688Seric /* 28758688Seric ** Set up macros for possible expansion in headers. 28858688Seric */ 28958688Seric 29058704Seric define('f', e->e_sender, e); 29158704Seric define('g', e->e_sender, e); 29258688Seric 2939382Seric if (tTd(32, 1)) 2949382Seric printf("----- collected header -----\n"); 29557208Seric msgid = "<none>"; 2969382Seric for (h = e->e_header; h != NULL; h = h->h_link) 2977783Seric { 2987783Seric extern char *capitalize(); 2997783Seric 30058688Seric /* do early binding */ 30158688Seric if (bitset(H_DEFAULT, h->h_flags) && h->h_value != NULL) 30258688Seric { 30358688Seric expand(h->h_value, buf, &buf[sizeof buf], e); 30458688Seric if (buf[0] != '\0') 30558688Seric { 30658688Seric h->h_value = newstr(buf); 30758688Seric h->h_flags &= ~H_DEFAULT; 30858688Seric } 30958688Seric } 31058688Seric 3119382Seric if (tTd(32, 1)) 3127783Seric printf("%s: %s\n", capitalize(h->h_field), h->h_value); 31357359Seric 31411414Seric /* count the number of times it has been processed */ 3159382Seric if (bitset(H_TRACE, h->h_flags)) 3169382Seric hopcnt++; 31711414Seric 31811414Seric /* send to this person if we so desire */ 31911414Seric if (GrabTo && bitset(H_RCPT, h->h_flags) && 32011414Seric !bitset(H_DEFAULT, h->h_flags) && 32155012Seric (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags))) 32211414Seric { 32358082Seric (void) sendtolist(h->h_value, (ADDRESS *) NULL, 32458082Seric &e->e_sendqueue, e); 32511414Seric } 32611414Seric 32757208Seric /* save the message-id for logging */ 32858737Seric if (!bitset(EF_QUEUERUN, e->e_flags) && h->h_value != NULL && 32911299Seric strcmp(h->h_field, "message-id") == 0) 33011290Seric { 33157208Seric msgid = h->h_value; 33211290Seric } 33357359Seric 33457359Seric /* see if this is a return-receipt header */ 33557359Seric if (bitset(H_RECEIPTTO, h->h_flags)) 33657359Seric e->e_receiptto = h->h_value; 33757359Seric 33857359Seric /* see if this is an errors-to header */ 33957359Seric if (bitset(H_ERRORSTO, h->h_flags)) 34058082Seric (void) sendtolist(h->h_value, (ADDRESS *) NULL, 34158082Seric &e->e_errorqueue, e); 3429382Seric } 3439382Seric if (tTd(32, 1)) 3447783Seric printf("----------------------------\n"); 3457783Seric 34658145Seric /* if we are just verifying (that is, sendmail -t -bv), drop out now */ 34758145Seric if (OpMode == MD_VERIFY) 34858145Seric return; 34958145Seric 3509382Seric /* store hop count */ 3519382Seric if (hopcnt > e->e_hopcount) 3529382Seric e->e_hopcount = hopcnt; 3539382Seric 3547783Seric /* message priority */ 35555012Seric p = hvalue("precedence", e); 3569382Seric if (p != NULL) 3579382Seric e->e_class = priencode(p); 35858737Seric if (!bitset(EF_QUEUERUN, e->e_flags)) 35925013Seric e->e_msgpriority = e->e_msgsize 36024981Seric - e->e_class * WkClassFact 36124981Seric + e->e_nrcpts * WkRecipFact; 3627783Seric 3637783Seric /* full name of from person */ 36455012Seric p = hvalue("full-name", e); 3657783Seric if (p != NULL) 3669382Seric define('x', p, e); 3677783Seric 3687783Seric /* date message originated */ 36955012Seric p = hvalue("posted-date", e); 3707783Seric if (p == NULL) 37155012Seric p = hvalue("date", e); 3727783Seric if (p != NULL) 3739382Seric define('a', p, e); 37411290Seric 37511290Seric /* 37611290Seric ** Log collection information. 37711290Seric */ 37811290Seric 37911290Seric # ifdef LOG 38058737Seric if (!bitset(EF_QUEUERUN, e->e_flags) && LogLevel > 4) 38111290Seric { 38257359Seric char *name; 38357232Seric char hbuf[MAXNAME]; 38457359Seric char sbuf[MAXLINE]; 38536230Skarels 38658667Seric if (bitset(EF_RESPONSE, e->e_flags)) 38758667Seric name = "[RESPONSE]"; 38858667Seric else if (RealHostName[0] == '[') 38936230Skarels name = RealHostName; 39036230Skarels else 39157359Seric { 39258755Seric extern char *anynet_ntoa(); 39357359Seric 39457359Seric name = hbuf; 395*58798Seric (void) sprintf(hbuf, "%.80s", RealHostName); 396*58798Seric if (RealHostAddr.sa.sa_family != 0) 397*58798Seric { 398*58798Seric p = &hbuf[strlen(hbuf)]; 399*58798Seric (void) sprintf(p, " (%s)", 400*58798Seric anynet_ntoa(&RealHostAddr)); 401*58798Seric } 40257359Seric } 40357359Seric 40457359Seric /* some versions of syslog only take 5 printf args */ 40557589Seric sprintf(sbuf, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s", 40658558Seric e->e_from.q_paddr, e->e_msgsize, e->e_class, 40757589Seric e->e_msgpriority, e->e_nrcpts, msgid); 40858686Seric syslog(LOG_INFO, "%s: %s, relay=%s", 40957359Seric e->e_id, sbuf, name); 41011290Seric } 41156795Seric # endif /* LOG */ 4127783Seric } 4137783Seric /* 4147783Seric ** PRIENCODE -- encode external priority names into internal values. 4157783Seric ** 4167783Seric ** Parameters: 4177783Seric ** p -- priority in ascii. 4187783Seric ** 4197783Seric ** Returns: 4207783Seric ** priority as a numeric level. 4217783Seric ** 4227783Seric ** Side Effects: 4237783Seric ** none. 4247783Seric */ 4257783Seric 4267783Seric priencode(p) 4277783Seric char *p; 4287783Seric { 4298253Seric register int i; 4307783Seric 4318253Seric for (i = 0; i < NumPriorities; i++) 4327783Seric { 43333725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 4348253Seric return (Priorities[i].pri_val); 4357783Seric } 4368253Seric 4378253Seric /* unknown priority */ 4388253Seric return (0); 4397783Seric } 4407783Seric /* 4417890Seric ** CRACKADDR -- parse an address and turn it into a macro 4427783Seric ** 4437783Seric ** This doesn't actually parse the address -- it just extracts 4447783Seric ** it and replaces it with "$g". The parse is totally ad hoc 4457783Seric ** and isn't even guaranteed to leave something syntactically 4467783Seric ** identical to what it started with. However, it does leave 4477783Seric ** something semantically identical. 4487783Seric ** 44951379Seric ** This algorithm has been cleaned up to handle a wider range 45051379Seric ** of cases -- notably quoted and backslash escaped strings. 45151379Seric ** This modification makes it substantially better at preserving 45251379Seric ** the original syntax. 4537783Seric ** 4547783Seric ** Parameters: 4557890Seric ** addr -- the address to be cracked. 4567783Seric ** 4577783Seric ** Returns: 4587783Seric ** a pointer to the new version. 4597783Seric ** 4607783Seric ** Side Effects: 4617890Seric ** none. 4627783Seric ** 4637783Seric ** Warning: 4647783Seric ** The return value is saved in local storage and should 4657783Seric ** be copied if it is to be reused. 4667783Seric */ 4677783Seric 4687783Seric char * 4697890Seric crackaddr(addr) 4707890Seric register char *addr; 4717783Seric { 4727783Seric register char *p; 47351379Seric register char c; 47451379Seric int cmtlev; 47556764Seric int realcmtlev; 47656764Seric int anglelev, realanglelev; 47751379Seric int copylev; 47851379Seric bool qmode; 47956764Seric bool realqmode; 48056764Seric bool skipping; 48151379Seric bool putgmac = FALSE; 48256735Seric bool quoteit = FALSE; 48351379Seric register char *bp; 48456764Seric char *buflim; 4857783Seric static char buf[MAXNAME]; 4867783Seric 4877783Seric if (tTd(33, 1)) 4887890Seric printf("crackaddr(%s)\n", addr); 4897783Seric 4908082Seric /* strip leading spaces */ 49158050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 4928082Seric addr++; 4938082Seric 4947783Seric /* 49551379Seric ** Start by assuming we have no angle brackets. This will be 49651379Seric ** adjusted later if we find them. 4977783Seric */ 4987783Seric 49951379Seric bp = buf; 50056764Seric buflim = &buf[sizeof buf - 5]; 50151379Seric p = addr; 50256764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 50356764Seric qmode = realqmode = FALSE; 50451379Seric 50551379Seric while ((c = *p++) != '\0') 5067783Seric { 50756764Seric /* 50856764Seric ** If the buffer is overful, go into a special "skipping" 50956764Seric ** mode that tries to keep legal syntax but doesn't actually 51056764Seric ** output things. 51156764Seric */ 5127783Seric 51356764Seric skipping = bp >= buflim; 51456735Seric 51556764Seric if (copylev > 0 && !skipping) 51656764Seric *bp++ = c; 51756735Seric 51851379Seric /* check for backslash escapes */ 51951379Seric if (c == '\\') 5207783Seric { 52151379Seric if ((c = *p++) == '\0') 5227783Seric { 52351379Seric /* too far */ 52451379Seric p--; 52551379Seric goto putg; 5267783Seric } 52756764Seric if (copylev > 0 && !skipping) 52851379Seric *bp++ = c; 52951379Seric goto putg; 5307783Seric } 5317783Seric 53251379Seric /* check for quoted strings */ 53351379Seric if (c == '"') 5347783Seric { 53551379Seric qmode = !qmode; 53656764Seric if (copylev > 0 && !skipping) 53756764Seric realqmode = !realqmode; 53851379Seric continue; 5397783Seric } 54051379Seric if (qmode) 54151379Seric goto putg; 5427783Seric 54351379Seric /* check for comments */ 54451379Seric if (c == '(') 5457783Seric { 54651379Seric cmtlev++; 54756764Seric 54856764Seric /* allow space for closing paren */ 54956764Seric if (!skipping) 55056764Seric { 55156764Seric buflim--; 55256764Seric realcmtlev++; 55356764Seric if (copylev++ <= 0) 55456764Seric { 55556764Seric *bp++ = ' '; 55656764Seric *bp++ = c; 55756764Seric } 55856764Seric } 55951379Seric } 56051379Seric if (cmtlev > 0) 56151379Seric { 56251379Seric if (c == ')') 5637783Seric { 56451379Seric cmtlev--; 56551379Seric copylev--; 56656764Seric if (!skipping) 56756764Seric { 56856764Seric realcmtlev--; 56956764Seric buflim++; 57056764Seric } 5717783Seric } 5727783Seric continue; 5737783Seric } 57456764Seric else if (c == ')') 57556764Seric { 57656764Seric /* syntax error: unmatched ) */ 57756764Seric if (!skipping) 57856764Seric bp--; 57956764Seric } 5807783Seric 58156764Seric 58256764Seric /* check for characters that may have to be quoted */ 58357175Seric if (strchr(".'@,;:\\()", c) != NULL) 58456764Seric { 58556764Seric /* 58656764Seric ** If these occur as the phrase part of a <> 58756764Seric ** construct, but are not inside of () or already 58856764Seric ** quoted, they will have to be quoted. Note that 58956764Seric ** now (but don't actually do the quoting). 59056764Seric */ 59156764Seric 59256764Seric if (cmtlev <= 0 && !qmode) 59356764Seric quoteit = TRUE; 59456764Seric } 59556764Seric 59651379Seric /* check for angle brackets */ 59751379Seric if (c == '<') 59851379Seric { 59956735Seric register char *q; 60056735Seric 60151379Seric /* oops -- have to change our mind */ 60256764Seric anglelev++; 60356764Seric if (!skipping) 60456764Seric realanglelev++; 60556764Seric 60656735Seric bp = buf; 60756735Seric if (quoteit) 60856735Seric { 60956735Seric *bp++ = '"'; 61056735Seric 61156735Seric /* back up over the '<' and any spaces */ 61256735Seric --p; 61358050Seric while (isascii(*--p) && isspace(*p)) 61456735Seric continue; 61556735Seric p++; 61656735Seric } 61756735Seric for (q = addr; q < p; ) 61856735Seric { 61956735Seric c = *q++; 62056764Seric if (bp < buflim) 62156764Seric { 62256764Seric if (quoteit && c == '"') 62356764Seric *bp++ = '\\'; 62456764Seric *bp++ = c; 62556764Seric } 62656735Seric } 62756735Seric if (quoteit) 62856735Seric { 62956735Seric *bp++ = '"'; 63056764Seric while ((c = *p++) != '<') 63156764Seric { 63256764Seric if (bp < buflim) 63356764Seric *bp++ = c; 63456764Seric } 63556764Seric *bp++ = c; 63656735Seric } 63751379Seric copylev = 0; 63856735Seric putgmac = quoteit = FALSE; 63951379Seric continue; 64051379Seric } 6417783Seric 64251379Seric if (c == '>') 6437783Seric { 64456764Seric if (anglelev > 0) 64556764Seric { 64656764Seric anglelev--; 64756764Seric if (!skipping) 64856764Seric { 64956764Seric realanglelev--; 65056764Seric buflim++; 65156764Seric } 65256764Seric } 65356764Seric else if (!skipping) 65456764Seric { 65556764Seric /* syntax error: unmatched > */ 65656764Seric if (copylev > 0) 65756764Seric bp--; 65856764Seric continue; 65956764Seric } 66051379Seric if (copylev++ <= 0) 66151379Seric *bp++ = c; 66251379Seric continue; 6637783Seric } 66451379Seric 66551379Seric /* must be a real address character */ 66651379Seric putg: 66751379Seric if (copylev <= 0 && !putgmac) 66851379Seric { 66958050Seric *bp++ = MACROEXPAND; 67051379Seric *bp++ = 'g'; 67151379Seric putgmac = TRUE; 67251379Seric } 6737783Seric } 6747783Seric 67556764Seric /* repair any syntactic damage */ 67656764Seric if (realqmode) 67756764Seric *bp++ = '"'; 67856764Seric while (realcmtlev-- > 0) 67956764Seric *bp++ = ')'; 68056764Seric while (realanglelev-- > 0) 68156764Seric *bp++ = '>'; 68251379Seric *bp++ = '\0'; 6837783Seric 6847783Seric if (tTd(33, 1)) 6857944Seric printf("crackaddr=>`%s'\n", buf); 6867783Seric 6877783Seric return (buf); 6887783Seric } 6899382Seric /* 6909382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 6919382Seric ** 6929382Seric ** Parameters: 6939382Seric ** fp -- file to put it on. 6949382Seric ** m -- mailer to use. 6959382Seric ** e -- envelope to use. 6969382Seric ** 6979382Seric ** Returns: 6989382Seric ** none. 6999382Seric ** 7009382Seric ** Side Effects: 7019382Seric ** none. 7029382Seric */ 7039382Seric 70457589Seric /* 70557589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 70657589Seric */ 70757589Seric #ifndef MAX 70857589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 70957589Seric #endif 71057589Seric 71110176Seric putheader(fp, m, e) 7129382Seric register FILE *fp; 7139382Seric register MAILER *m; 7149382Seric register ENVELOPE *e; 7159382Seric { 71657135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 7179382Seric register HDR *h; 71857135Seric char obuf[MAXLINE]; 7199382Seric 7209382Seric for (h = e->e_header; h != NULL; h = h->h_link) 7219382Seric { 7229382Seric register char *p; 72310689Seric extern bool bitintersect(); 7249382Seric 7259382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 72610689Seric !bitintersect(h->h_mflags, m->m_flags)) 7279382Seric continue; 7289382Seric 72911414Seric /* handle Resent-... headers specially */ 73011414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 73111414Seric continue; 73211414Seric 7339382Seric p = h->h_value; 7349382Seric if (bitset(H_DEFAULT, h->h_flags)) 7359382Seric { 7369382Seric /* macro expand value if generated internally */ 7379382Seric expand(p, buf, &buf[sizeof buf], e); 7389382Seric p = buf; 7399382Seric if (p == NULL || *p == '\0') 7409382Seric continue; 7419382Seric } 7429382Seric 7439382Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 7449382Seric { 7459382Seric /* address field */ 7469382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 7479382Seric 7489382Seric if (bitset(H_FROM, h->h_flags)) 7499382Seric oldstyle = FALSE; 75055012Seric commaize(h, p, fp, oldstyle, m, e); 7519382Seric } 7529382Seric else 7539382Seric { 7549382Seric /* vanilla header line */ 75512159Seric register char *nlp; 75657642Seric extern char *capitalize(); 75712159Seric 75812159Seric (void) sprintf(obuf, "%s: ", capitalize(h->h_field)); 75956795Seric while ((nlp = strchr(p, '\n')) != NULL) 76012159Seric { 76112159Seric *nlp = '\0'; 76212159Seric (void) strcat(obuf, p); 76312159Seric *nlp = '\n'; 76412159Seric putline(obuf, fp, m); 76512159Seric p = ++nlp; 76612161Seric obuf[0] = '\0'; 76712159Seric } 76812159Seric (void) strcat(obuf, p); 76910176Seric putline(obuf, fp, m); 7709382Seric } 7719382Seric } 7729382Seric } 7739382Seric /* 7749382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 7759382Seric ** 7769382Seric ** Parameters: 7779382Seric ** h -- the header field to output. 7789382Seric ** p -- the value to put in it. 7799382Seric ** fp -- file to put it to. 7809382Seric ** oldstyle -- TRUE if this is an old style header. 7819382Seric ** m -- a pointer to the mailer descriptor. If NULL, 7829382Seric ** don't transform the name at all. 78355012Seric ** e -- the envelope containing the message. 7849382Seric ** 7859382Seric ** Returns: 7869382Seric ** none. 7879382Seric ** 7889382Seric ** Side Effects: 7899382Seric ** outputs "p" to file "fp". 7909382Seric */ 7919382Seric 79255012Seric commaize(h, p, fp, oldstyle, m, e) 7939382Seric register HDR *h; 7949382Seric register char *p; 7959382Seric FILE *fp; 7969382Seric bool oldstyle; 7979382Seric register MAILER *m; 79855012Seric register ENVELOPE *e; 7999382Seric { 8009382Seric register char *obp; 8019382Seric int opos; 8029382Seric bool firstone = TRUE; 80311157Seric char obuf[MAXLINE + 3]; 80457642Seric extern char *capitalize(); 8059382Seric 8069382Seric /* 8079382Seric ** Output the address list translated by the 8089382Seric ** mailer and with commas. 8099382Seric */ 8109382Seric 8119382Seric if (tTd(14, 2)) 8129382Seric printf("commaize(%s: %s)\n", h->h_field, p); 8139382Seric 8149382Seric obp = obuf; 8159382Seric (void) sprintf(obp, "%s: ", capitalize(h->h_field)); 8169382Seric opos = strlen(h->h_field) + 2; 8179382Seric obp += opos; 8189382Seric 8199382Seric /* 8209382Seric ** Run through the list of values. 8219382Seric */ 8229382Seric 8239382Seric while (*p != '\0') 8249382Seric { 8259382Seric register char *name; 82654983Seric register int c; 8279382Seric char savechar; 8289382Seric extern char *remotename(); 8299382Seric 8309382Seric /* 8319382Seric ** Find the end of the name. New style names 8329382Seric ** end with a comma, old style names end with 8339382Seric ** a space character. However, spaces do not 8349382Seric ** necessarily delimit an old-style name -- at 8359382Seric ** signs mean keep going. 8369382Seric */ 8379382Seric 8389382Seric /* find end of name */ 83958050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 8409382Seric p++; 8419382Seric name = p; 8429382Seric for (;;) 8439382Seric { 84458333Seric auto char *oldp; 84516909Seric char pvpbuf[PSBUFSIZE]; 8469382Seric extern char **prescan(); 8479382Seric 84858333Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp); 84958333Seric p = oldp; 8509382Seric 8519382Seric /* look to see if we have an at sign */ 85258050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 8539382Seric p++; 8549382Seric 85558170Seric if (*p != '@') 8569382Seric { 8579382Seric p = oldp; 8589382Seric break; 8599382Seric } 8609382Seric p += *p == '@' ? 1 : 2; 86158050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 8629382Seric p++; 8639382Seric } 8649382Seric /* at the end of one complete name */ 8659382Seric 8669382Seric /* strip off trailing white space */ 86758050Seric while (p >= name && 86858050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 8699382Seric p--; 8709382Seric if (++p == name) 8719382Seric continue; 8729382Seric savechar = *p; 8739382Seric *p = '\0'; 8749382Seric 8759382Seric /* translate the name to be relative */ 87658020Seric name = remotename(name, m, bitset(H_FROM, h->h_flags), 87758509Seric TRUE, FALSE, TRUE, e); 8789382Seric if (*name == '\0') 8799382Seric { 8809382Seric *p = savechar; 8819382Seric continue; 8829382Seric } 8839382Seric 8849382Seric /* output the name with nice formatting */ 88554983Seric opos += strlen(name); 8869382Seric if (!firstone) 8879382Seric opos += 2; 8889382Seric if (opos > 78 && !firstone) 8899382Seric { 89010178Seric (void) strcpy(obp, ",\n"); 89110176Seric putline(obuf, fp, m); 8929382Seric obp = obuf; 8939382Seric (void) sprintf(obp, " "); 89410161Seric opos = strlen(obp); 89510161Seric obp += opos; 89654983Seric opos += strlen(name); 8979382Seric } 8989382Seric else if (!firstone) 8999382Seric { 9009382Seric (void) sprintf(obp, ", "); 9019382Seric obp += 2; 9029382Seric } 9039382Seric 9049382Seric /* strip off quote bits as we output */ 90554983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 9069382Seric { 90754983Seric if (bitnset(M_7BITS, m->m_flags)) 90854983Seric c &= 0177; 90954983Seric *obp++ = c; 9109382Seric } 9119382Seric firstone = FALSE; 9129382Seric *p = savechar; 9139382Seric } 9149382Seric (void) strcpy(obp, "\n"); 91510176Seric putline(obuf, fp, m); 9169382Seric } 9179382Seric /* 91858170Seric ** COPYHEADER -- copy header list 9199382Seric ** 92058170Seric ** This routine is the equivalent of newstr for header lists 92158170Seric ** 9229382Seric ** Parameters: 92358170Seric ** header -- list of header structures to copy. 9249382Seric ** 9259382Seric ** Returns: 92658170Seric ** a copy of 'header'. 9279382Seric ** 9289382Seric ** Side Effects: 9299382Seric ** none. 9309382Seric */ 9319382Seric 93258170Seric HDR * 93358170Seric copyheader(header) 93458170Seric register HDR *header; 9359382Seric { 93658170Seric register HDR *newhdr; 93758170Seric HDR *ret; 93858170Seric register HDR **tail = &ret; 9399382Seric 94058170Seric while (header != NULL) 94158170Seric { 94258170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 94358170Seric STRUCTCOPY(*header, *newhdr); 94458170Seric *tail = newhdr; 94558170Seric tail = &newhdr->h_link; 94658170Seric header = header->h_link; 94758170Seric } 94858170Seric *tail = NULL; 94958170Seric 95058170Seric return ret; 9519382Seric } 952