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*64156Seric static char sccsid[] = "@(#)headers.c 8.9 (Berkeley) 08/08/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; 7364149Seric while (isascii(*p) && isgraph(*p) && *p != ':') 7464149Seric p++; 7564149Seric fvalue = p; 7664149Seric while (isascii(*p) && isspace(*p)) 7764149Seric p++; 7864150Seric if (*p++ != ':' || fname == fvalue) 7910118Seric { 8058151Seric syserr("553 header syntax error, line \"%s\"", line); 8110118Seric return (0); 8210118Seric } 8364149Seric *fvalue = '\0'; 8464149Seric fvalue = p; 854091Seric 864091Seric /* strip field value on front */ 874091Seric if (*fvalue == ' ') 884091Seric fvalue++; 894091Seric 904091Seric /* see if it is a known type */ 914091Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 924091Seric { 9359579Seric if (strcasecmp(hi->hi_field, fname) == 0) 944091Seric break; 954091Seric } 964091Seric 9711414Seric /* see if this is a resent message */ 9811930Seric if (!def && bitset(H_RESENT, hi->hi_flags)) 9955012Seric e->e_flags |= EF_RESENT; 10011414Seric 1014091Seric /* if this means "end of header" quit now */ 1024091Seric if (bitset(H_EOH, hi->hi_flags)) 1034091Seric return (hi->hi_flags); 1044091Seric 10511414Seric /* drop explicit From: if same as what we would generate -- for MH */ 10614784Seric p = "resent-from"; 10755012Seric if (!bitset(EF_RESENT, e->e_flags)) 10814784Seric p += 7; 10959579Seric if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0) 11011414Seric { 11163753Seric if (tTd(31, 2)) 11263753Seric { 11363753Seric printf("comparing header from (%s) against default (%s or %s)\n", 11463753Seric fvalue, e->e_from.q_paddr, e->e_from.q_user); 11563753Seric } 11655012Seric if (e->e_from.q_paddr != NULL && 11763753Seric (strcmp(fvalue, e->e_from.q_paddr) == 0 || 11863753Seric strcmp(fvalue, e->e_from.q_user) == 0)) 11911414Seric return (hi->hi_flags); 12011414Seric } 12111414Seric 12214784Seric /* delete default value for this header */ 12355012Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 12414784Seric { 12559579Seric if (strcasecmp(fname, h->h_field) == 0 && 12614784Seric bitset(H_DEFAULT, h->h_flags) && 12714784Seric !bitset(H_FORCE, h->h_flags)) 12814784Seric h->h_value = NULL; 12914784Seric } 13014784Seric 13113012Seric /* create a new node */ 13213012Seric h = (HDR *) xalloc(sizeof *h); 13313012Seric h->h_field = newstr(fname); 13464134Seric h->h_value = newstr(fvalue); 13513012Seric h->h_link = NULL; 13623117Seric bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts); 13713012Seric *hp = h; 1388066Seric h->h_flags = hi->hi_flags; 1394091Seric if (def) 1404091Seric h->h_flags |= H_DEFAULT; 1419059Seric if (cond) 1429059Seric h->h_flags |= H_CHECK; 1434091Seric 1445937Seric /* hack to see if this is a new format message */ 1458095Seric if (!def && bitset(H_RCPT|H_FROM, h->h_flags) && 14656795Seric (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL || 14756795Seric strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL)) 1488089Seric { 14955012Seric e->e_flags &= ~EF_OLDSTYLE; 1508089Seric } 1515937Seric 1524091Seric return (h->h_flags); 1534091Seric } 1544091Seric /* 1556980Seric ** ADDHEADER -- add a header entry to the end of the queue. 1566980Seric ** 1576980Seric ** This bypasses the special checking of chompheader. 1586980Seric ** 1596980Seric ** Parameters: 16059579Seric ** field -- the name of the header field. 16158789Seric ** value -- the value of the field. 1626980Seric ** e -- the envelope to add them to. 1636980Seric ** 1646980Seric ** Returns: 1656980Seric ** none. 1666980Seric ** 1676980Seric ** Side Effects: 1686980Seric ** adds the field on the list of headers for this envelope. 1696980Seric */ 1706980Seric 1716980Seric addheader(field, value, e) 1726980Seric char *field; 1736980Seric char *value; 1746980Seric ENVELOPE *e; 1756980Seric { 1766980Seric register HDR *h; 1776980Seric register struct hdrinfo *hi; 1786980Seric HDR **hp; 1796980Seric 1806980Seric /* find info struct */ 1816980Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 1826980Seric { 18359579Seric if (strcasecmp(field, hi->hi_field) == 0) 1846980Seric break; 1856980Seric } 1866980Seric 1876980Seric /* find current place in list -- keep back pointer? */ 1886980Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 1896980Seric { 19059579Seric if (strcasecmp(field, h->h_field) == 0) 1916980Seric break; 1926980Seric } 1936980Seric 1946980Seric /* allocate space for new header */ 1956980Seric h = (HDR *) xalloc(sizeof *h); 1966980Seric h->h_field = field; 1976980Seric h->h_value = newstr(value); 1987368Seric h->h_link = *hp; 1996980Seric h->h_flags = hi->hi_flags | H_DEFAULT; 20010689Seric clrbitmap(h->h_mflags); 2016980Seric *hp = h; 2026980Seric } 2036980Seric /* 2044091Seric ** HVALUE -- return value of a header. 2054091Seric ** 2064091Seric ** Only "real" fields (i.e., ones that have not been supplied 2074091Seric ** as a default) are used. 2084091Seric ** 2094091Seric ** Parameters: 2104091Seric ** field -- the field name. 21155012Seric ** e -- the envelope containing the header. 2124091Seric ** 2134091Seric ** Returns: 2144091Seric ** pointer to the value part. 2154091Seric ** NULL if not found. 2164091Seric ** 2174091Seric ** Side Effects: 2189382Seric ** none. 2194091Seric */ 2204091Seric 2214091Seric char * 22255012Seric hvalue(field, e) 2234091Seric char *field; 22455012Seric register ENVELOPE *e; 2254091Seric { 2264091Seric register HDR *h; 2274091Seric 22855012Seric for (h = e->e_header; h != NULL; h = h->h_link) 2294091Seric { 23059579Seric if (!bitset(H_DEFAULT, h->h_flags) && 23159579Seric strcasecmp(h->h_field, field) == 0) 2324091Seric return (h->h_value); 2334091Seric } 2344091Seric return (NULL); 2354091Seric } 2364091Seric /* 2374091Seric ** ISHEADER -- predicate telling if argument is a header. 2384091Seric ** 2394319Seric ** A line is a header if it has a single word followed by 2404319Seric ** optional white space followed by a colon. 2414319Seric ** 2424091Seric ** Parameters: 2434091Seric ** s -- string to check for possible headerness. 2444091Seric ** 2454091Seric ** Returns: 2464091Seric ** TRUE if s is a header. 2474091Seric ** FALSE otherwise. 2484091Seric ** 2494091Seric ** Side Effects: 2504091Seric ** none. 2514091Seric */ 2524091Seric 2534091Seric bool 2544091Seric isheader(s) 2554091Seric register char *s; 2564091Seric { 2579382Seric while (*s > ' ' && *s != ':' && *s != '\0') 2584091Seric s++; 2599382Seric 2609382Seric /* following technically violates RFC822 */ 26158050Seric while (isascii(*s) && isspace(*s)) 2624091Seric s++; 2639382Seric 2644091Seric return (*s == ':'); 2654091Seric } 2665919Seric /* 2677783Seric ** EATHEADER -- run through the stored header and extract info. 2687783Seric ** 2697783Seric ** Parameters: 2709382Seric ** e -- the envelope to process. 27158929Seric ** full -- if set, do full processing (e.g., compute 27258929Seric ** message priority). 2737783Seric ** 2747783Seric ** Returns: 2757783Seric ** none. 2767783Seric ** 2777783Seric ** Side Effects: 2787783Seric ** Sets a bunch of global variables from information 2799382Seric ** in the collected header. 2809382Seric ** Aborts the message if the hop count is exceeded. 2817783Seric */ 2827783Seric 28358929Seric eatheader(e, full) 2849382Seric register ENVELOPE *e; 28558929Seric bool full; 2867783Seric { 2877783Seric register HDR *h; 2887783Seric register char *p; 2899382Seric int hopcnt = 0; 29057208Seric char *msgid; 29158688Seric char buf[MAXLINE]; 2927783Seric 29358688Seric /* 29458688Seric ** Set up macros for possible expansion in headers. 29558688Seric */ 29658688Seric 29758704Seric define('f', e->e_sender, e); 29858704Seric define('g', e->e_sender, e); 29964148Seric if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0') 30064148Seric define('u', e->e_origrcpt, e); 30164148Seric else 30264148Seric define('u', NULL, e); 30358688Seric 3049382Seric if (tTd(32, 1)) 3059382Seric printf("----- collected header -----\n"); 30657208Seric msgid = "<none>"; 3079382Seric for (h = e->e_header; h != NULL; h = h->h_link) 3087783Seric { 309*64156Seric if (h->h_value == NULL) 310*64156Seric { 311*64156Seric if (tTd(32, 1)) 312*64156Seric printf("%s: <NULL>\n", h->h_field); 313*64156Seric continue; 314*64156Seric } 315*64156Seric 31658688Seric /* do early binding */ 317*64156Seric if (bitset(H_DEFAULT, h->h_flags)) 31858688Seric { 31958688Seric expand(h->h_value, buf, &buf[sizeof buf], e); 32058688Seric if (buf[0] != '\0') 32158688Seric { 32258688Seric h->h_value = newstr(buf); 32358688Seric h->h_flags &= ~H_DEFAULT; 32458688Seric } 32558688Seric } 32658688Seric 3279382Seric if (tTd(32, 1)) 32859579Seric printf("%s: %s\n", h->h_field, h->h_value); 32957359Seric 33011414Seric /* count the number of times it has been processed */ 3319382Seric if (bitset(H_TRACE, h->h_flags)) 3329382Seric hopcnt++; 33311414Seric 33411414Seric /* send to this person if we so desire */ 33511414Seric if (GrabTo && bitset(H_RCPT, h->h_flags) && 33611414Seric !bitset(H_DEFAULT, h->h_flags) && 33755012Seric (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags))) 33811414Seric { 33963839Seric int saveflags = e->e_flags; 34063839Seric 34158082Seric (void) sendtolist(h->h_value, (ADDRESS *) NULL, 34258082Seric &e->e_sendqueue, e); 34363839Seric 34463839Seric /* delete fatal errors generated by this address */ 34564148Seric if (!GrabTo && !bitset(EF_FATALERRS, saveflags)) 34663839Seric e->e_flags &= ~EF_FATALERRS; 34711414Seric } 34811414Seric 34957208Seric /* save the message-id for logging */ 350*64156Seric if (full && strcasecmp(h->h_field, "message-id") == 0) 35111290Seric { 35257208Seric msgid = h->h_value; 35359859Seric while (isascii(*msgid) && isspace(*msgid)) 35459859Seric msgid++; 35511290Seric } 35657359Seric 35757359Seric /* see if this is a return-receipt header */ 35857359Seric if (bitset(H_RECEIPTTO, h->h_flags)) 35957359Seric e->e_receiptto = h->h_value; 36057359Seric 36157359Seric /* see if this is an errors-to header */ 36261104Seric if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags)) 36358082Seric (void) sendtolist(h->h_value, (ADDRESS *) NULL, 36458082Seric &e->e_errorqueue, e); 3659382Seric } 3669382Seric if (tTd(32, 1)) 3677783Seric printf("----------------------------\n"); 3687783Seric 36958145Seric /* if we are just verifying (that is, sendmail -t -bv), drop out now */ 37058145Seric if (OpMode == MD_VERIFY) 37158145Seric return; 37258145Seric 3739382Seric /* store hop count */ 3749382Seric if (hopcnt > e->e_hopcount) 3759382Seric e->e_hopcount = hopcnt; 3769382Seric 3777783Seric /* message priority */ 37855012Seric p = hvalue("precedence", e); 3799382Seric if (p != NULL) 3809382Seric e->e_class = priencode(p); 38158929Seric if (full) 38225013Seric e->e_msgpriority = e->e_msgsize 38324981Seric - e->e_class * WkClassFact 38424981Seric + e->e_nrcpts * WkRecipFact; 3857783Seric 3867783Seric /* full name of from person */ 38755012Seric p = hvalue("full-name", e); 3887783Seric if (p != NULL) 3899382Seric define('x', p, e); 3907783Seric 3917783Seric /* date message originated */ 39255012Seric p = hvalue("posted-date", e); 3937783Seric if (p == NULL) 39455012Seric p = hvalue("date", e); 3957783Seric if (p != NULL) 3969382Seric define('a', p, e); 39711290Seric 39811290Seric /* 39911290Seric ** Log collection information. 40011290Seric */ 40111290Seric 40211290Seric # ifdef LOG 40358929Seric if (full && LogLevel > 4) 40411290Seric { 40557359Seric char *name; 40660575Seric register char *sbp; 40757232Seric char hbuf[MAXNAME]; 40857359Seric char sbuf[MAXLINE]; 40936230Skarels 41058667Seric if (bitset(EF_RESPONSE, e->e_flags)) 41158667Seric name = "[RESPONSE]"; 41258951Seric else if ((name = macvalue('_', e)) != NULL) 41358951Seric ; 41458667Seric else if (RealHostName[0] == '[') 41536230Skarels name = RealHostName; 41636230Skarels else 41757359Seric { 41857359Seric name = hbuf; 41958798Seric (void) sprintf(hbuf, "%.80s", RealHostName); 42058798Seric if (RealHostAddr.sa.sa_family != 0) 42158798Seric { 42258798Seric p = &hbuf[strlen(hbuf)]; 42358798Seric (void) sprintf(p, " (%s)", 42458798Seric anynet_ntoa(&RealHostAddr)); 42558798Seric } 42657359Seric } 42757359Seric 42857359Seric /* some versions of syslog only take 5 printf args */ 42960575Seric sbp = sbuf; 43060575Seric sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s", 43158558Seric e->e_from.q_paddr, e->e_msgsize, e->e_class, 43257589Seric e->e_msgpriority, e->e_nrcpts, msgid); 43360575Seric sbp += strlen(sbp); 43460575Seric if (e->e_bodytype != NULL) 43560575Seric { 43660575Seric (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype); 43760575Seric sbp += strlen(sbp); 43860575Seric } 43960575Seric p = macvalue('r', e); 44060575Seric if (p != NULL) 44160575Seric (void) sprintf(sbp, ", proto=%.20s", p); 44258686Seric syslog(LOG_INFO, "%s: %s, relay=%s", 44357359Seric e->e_id, sbuf, name); 44411290Seric } 44556795Seric # endif /* LOG */ 4467783Seric } 4477783Seric /* 4487783Seric ** PRIENCODE -- encode external priority names into internal values. 4497783Seric ** 4507783Seric ** Parameters: 4517783Seric ** p -- priority in ascii. 4527783Seric ** 4537783Seric ** Returns: 4547783Seric ** priority as a numeric level. 4557783Seric ** 4567783Seric ** Side Effects: 4577783Seric ** none. 4587783Seric */ 4597783Seric 4607783Seric priencode(p) 4617783Seric char *p; 4627783Seric { 4638253Seric register int i; 4647783Seric 4658253Seric for (i = 0; i < NumPriorities; i++) 4667783Seric { 46733725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 4688253Seric return (Priorities[i].pri_val); 4697783Seric } 4708253Seric 4718253Seric /* unknown priority */ 4728253Seric return (0); 4737783Seric } 4747783Seric /* 4757890Seric ** CRACKADDR -- parse an address and turn it into a macro 4767783Seric ** 4777783Seric ** This doesn't actually parse the address -- it just extracts 4787783Seric ** it and replaces it with "$g". The parse is totally ad hoc 4797783Seric ** and isn't even guaranteed to leave something syntactically 4807783Seric ** identical to what it started with. However, it does leave 4817783Seric ** something semantically identical. 4827783Seric ** 48351379Seric ** This algorithm has been cleaned up to handle a wider range 48451379Seric ** of cases -- notably quoted and backslash escaped strings. 48551379Seric ** This modification makes it substantially better at preserving 48651379Seric ** the original syntax. 4877783Seric ** 4887783Seric ** Parameters: 4897890Seric ** addr -- the address to be cracked. 4907783Seric ** 4917783Seric ** Returns: 4927783Seric ** a pointer to the new version. 4937783Seric ** 4947783Seric ** Side Effects: 4957890Seric ** none. 4967783Seric ** 4977783Seric ** Warning: 4987783Seric ** The return value is saved in local storage and should 4997783Seric ** be copied if it is to be reused. 5007783Seric */ 5017783Seric 5027783Seric char * 5037890Seric crackaddr(addr) 5047890Seric register char *addr; 5057783Seric { 5067783Seric register char *p; 50751379Seric register char c; 50851379Seric int cmtlev; 50956764Seric int realcmtlev; 51056764Seric int anglelev, realanglelev; 51151379Seric int copylev; 51251379Seric bool qmode; 51356764Seric bool realqmode; 51456764Seric bool skipping; 51551379Seric bool putgmac = FALSE; 51656735Seric bool quoteit = FALSE; 51764148Seric bool gotangle = FALSE; 51851379Seric register char *bp; 51956764Seric char *buflim; 5207783Seric static char buf[MAXNAME]; 5217783Seric 5227783Seric if (tTd(33, 1)) 5237890Seric printf("crackaddr(%s)\n", addr); 5247783Seric 5258082Seric /* strip leading spaces */ 52658050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 5278082Seric addr++; 5288082Seric 5297783Seric /* 53051379Seric ** Start by assuming we have no angle brackets. This will be 53151379Seric ** adjusted later if we find them. 5327783Seric */ 5337783Seric 53451379Seric bp = buf; 53556764Seric buflim = &buf[sizeof buf - 5]; 53651379Seric p = addr; 53756764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 53856764Seric qmode = realqmode = FALSE; 53951379Seric 54051379Seric while ((c = *p++) != '\0') 5417783Seric { 54256764Seric /* 54356764Seric ** If the buffer is overful, go into a special "skipping" 54456764Seric ** mode that tries to keep legal syntax but doesn't actually 54556764Seric ** output things. 54656764Seric */ 5477783Seric 54856764Seric skipping = bp >= buflim; 54956735Seric 55056764Seric if (copylev > 0 && !skipping) 55156764Seric *bp++ = c; 55256735Seric 55351379Seric /* check for backslash escapes */ 55451379Seric if (c == '\\') 5557783Seric { 55658890Seric /* arrange to quote the address */ 55758890Seric if (cmtlev <= 0 && !qmode) 55858890Seric quoteit = TRUE; 55958890Seric 56051379Seric if ((c = *p++) == '\0') 5617783Seric { 56251379Seric /* too far */ 56351379Seric p--; 56451379Seric goto putg; 5657783Seric } 56656764Seric if (copylev > 0 && !skipping) 56751379Seric *bp++ = c; 56851379Seric goto putg; 5697783Seric } 5707783Seric 57151379Seric /* check for quoted strings */ 57251379Seric if (c == '"') 5737783Seric { 57451379Seric qmode = !qmode; 57556764Seric if (copylev > 0 && !skipping) 57656764Seric realqmode = !realqmode; 57751379Seric continue; 5787783Seric } 57951379Seric if (qmode) 58051379Seric goto putg; 5817783Seric 58251379Seric /* check for comments */ 58351379Seric if (c == '(') 5847783Seric { 58551379Seric cmtlev++; 58656764Seric 58756764Seric /* allow space for closing paren */ 58856764Seric if (!skipping) 58956764Seric { 59056764Seric buflim--; 59156764Seric realcmtlev++; 59256764Seric if (copylev++ <= 0) 59356764Seric { 59456764Seric *bp++ = ' '; 59556764Seric *bp++ = c; 59656764Seric } 59756764Seric } 59851379Seric } 59951379Seric if (cmtlev > 0) 60051379Seric { 60151379Seric if (c == ')') 6027783Seric { 60351379Seric cmtlev--; 60451379Seric copylev--; 60556764Seric if (!skipping) 60656764Seric { 60756764Seric realcmtlev--; 60856764Seric buflim++; 60956764Seric } 6107783Seric } 6117783Seric continue; 6127783Seric } 61356764Seric else if (c == ')') 61456764Seric { 61556764Seric /* syntax error: unmatched ) */ 61656764Seric if (!skipping) 61756764Seric bp--; 61856764Seric } 6197783Seric 62056764Seric 62156764Seric /* check for characters that may have to be quoted */ 62264148Seric if (strchr(".'@,;:\\()[]", c) != NULL) 62356764Seric { 62456764Seric /* 62556764Seric ** If these occur as the phrase part of a <> 62656764Seric ** construct, but are not inside of () or already 62756764Seric ** quoted, they will have to be quoted. Note that 62856764Seric ** now (but don't actually do the quoting). 62956764Seric */ 63056764Seric 63156764Seric if (cmtlev <= 0 && !qmode) 63256764Seric quoteit = TRUE; 63356764Seric } 63456764Seric 63551379Seric /* check for angle brackets */ 63651379Seric if (c == '<') 63751379Seric { 63856735Seric register char *q; 63956735Seric 64064148Seric /* assume first of two angles is bogus */ 64164148Seric if (gotangle) 64264148Seric quoteit = TRUE; 64364148Seric gotangle = TRUE; 64464148Seric 64551379Seric /* oops -- have to change our mind */ 64656764Seric anglelev++; 64756764Seric if (!skipping) 64856764Seric realanglelev++; 64956764Seric 65056735Seric bp = buf; 65156735Seric if (quoteit) 65256735Seric { 65356735Seric *bp++ = '"'; 65456735Seric 65556735Seric /* back up over the '<' and any spaces */ 65656735Seric --p; 65758050Seric while (isascii(*--p) && isspace(*p)) 65856735Seric continue; 65956735Seric p++; 66056735Seric } 66156735Seric for (q = addr; q < p; ) 66256735Seric { 66356735Seric c = *q++; 66456764Seric if (bp < buflim) 66556764Seric { 66656764Seric if (quoteit && c == '"') 66756764Seric *bp++ = '\\'; 66856764Seric *bp++ = c; 66956764Seric } 67056735Seric } 67156735Seric if (quoteit) 67256735Seric { 67364148Seric if (bp == &buf[1]) 67464148Seric bp--; 67564148Seric else 67664148Seric *bp++ = '"'; 67756764Seric while ((c = *p++) != '<') 67856764Seric { 67956764Seric if (bp < buflim) 68056764Seric *bp++ = c; 68156764Seric } 68256764Seric *bp++ = c; 68356735Seric } 68451379Seric copylev = 0; 68556735Seric putgmac = quoteit = FALSE; 68651379Seric continue; 68751379Seric } 6887783Seric 68951379Seric if (c == '>') 6907783Seric { 69156764Seric if (anglelev > 0) 69256764Seric { 69356764Seric anglelev--; 69456764Seric if (!skipping) 69556764Seric { 69656764Seric realanglelev--; 69756764Seric buflim++; 69856764Seric } 69956764Seric } 70056764Seric else if (!skipping) 70156764Seric { 70256764Seric /* syntax error: unmatched > */ 70356764Seric if (copylev > 0) 70456764Seric bp--; 70556764Seric continue; 70656764Seric } 70751379Seric if (copylev++ <= 0) 70851379Seric *bp++ = c; 70951379Seric continue; 7107783Seric } 71151379Seric 71251379Seric /* must be a real address character */ 71351379Seric putg: 71451379Seric if (copylev <= 0 && !putgmac) 71551379Seric { 71658050Seric *bp++ = MACROEXPAND; 71751379Seric *bp++ = 'g'; 71851379Seric putgmac = TRUE; 71951379Seric } 7207783Seric } 7217783Seric 72256764Seric /* repair any syntactic damage */ 72356764Seric if (realqmode) 72456764Seric *bp++ = '"'; 72556764Seric while (realcmtlev-- > 0) 72656764Seric *bp++ = ')'; 72756764Seric while (realanglelev-- > 0) 72856764Seric *bp++ = '>'; 72951379Seric *bp++ = '\0'; 7307783Seric 7317783Seric if (tTd(33, 1)) 7327944Seric printf("crackaddr=>`%s'\n", buf); 7337783Seric 7347783Seric return (buf); 7357783Seric } 7369382Seric /* 7379382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 7389382Seric ** 7399382Seric ** Parameters: 7409382Seric ** fp -- file to put it on. 7419382Seric ** m -- mailer to use. 7429382Seric ** e -- envelope to use. 7439382Seric ** 7449382Seric ** Returns: 7459382Seric ** none. 7469382Seric ** 7479382Seric ** Side Effects: 7489382Seric ** none. 7499382Seric */ 7509382Seric 75157589Seric /* 75257589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 75357589Seric */ 75457589Seric #ifndef MAX 75557589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 75657589Seric #endif 75757589Seric 75810176Seric putheader(fp, m, e) 7599382Seric register FILE *fp; 7609382Seric register MAILER *m; 7619382Seric register ENVELOPE *e; 7629382Seric { 76357135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 7649382Seric register HDR *h; 76557135Seric char obuf[MAXLINE]; 7669382Seric 76759882Seric if (tTd(34, 1)) 76859882Seric printf("--- putheader, mailer = %s ---\n", m->m_name); 76959882Seric 7709382Seric for (h = e->e_header; h != NULL; h = h->h_link) 7719382Seric { 7729382Seric register char *p; 77310689Seric extern bool bitintersect(); 7749382Seric 77559882Seric if (tTd(34, 11)) 77659882Seric { 77759882Seric printf(" %s: ", h->h_field); 77859882Seric xputs(h->h_value); 77959882Seric } 78059882Seric 7819382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 78210689Seric !bitintersect(h->h_mflags, m->m_flags)) 78359882Seric { 78459882Seric if (tTd(34, 11)) 78559882Seric printf(" (skipped)\n"); 7869382Seric continue; 78759882Seric } 7889382Seric 78911414Seric /* handle Resent-... headers specially */ 79011414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 79159882Seric { 79259882Seric if (tTd(34, 11)) 79359882Seric printf(" (skipped (resent))\n"); 79411414Seric continue; 79559882Seric } 79659882Seric if (tTd(34, 11)) 79759882Seric printf("\n"); 79811414Seric 7999382Seric p = h->h_value; 8009382Seric if (bitset(H_DEFAULT, h->h_flags)) 8019382Seric { 8029382Seric /* macro expand value if generated internally */ 8039382Seric expand(p, buf, &buf[sizeof buf], e); 8049382Seric p = buf; 8059382Seric if (p == NULL || *p == '\0') 8069382Seric continue; 8079382Seric } 8089382Seric 8099382Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 8109382Seric { 8119382Seric /* address field */ 8129382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 8139382Seric 8149382Seric if (bitset(H_FROM, h->h_flags)) 8159382Seric oldstyle = FALSE; 81655012Seric commaize(h, p, fp, oldstyle, m, e); 8179382Seric } 8189382Seric else 8199382Seric { 8209382Seric /* vanilla header line */ 82112159Seric register char *nlp; 82212159Seric 82359579Seric (void) sprintf(obuf, "%s: ", h->h_field); 82456795Seric while ((nlp = strchr(p, '\n')) != NULL) 82512159Seric { 82612159Seric *nlp = '\0'; 82712159Seric (void) strcat(obuf, p); 82812159Seric *nlp = '\n'; 82912159Seric putline(obuf, fp, m); 83012159Seric p = ++nlp; 83112161Seric obuf[0] = '\0'; 83212159Seric } 83312159Seric (void) strcat(obuf, p); 83410176Seric putline(obuf, fp, m); 8359382Seric } 8369382Seric } 8379382Seric } 8389382Seric /* 8399382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 8409382Seric ** 8419382Seric ** Parameters: 8429382Seric ** h -- the header field to output. 8439382Seric ** p -- the value to put in it. 8449382Seric ** fp -- file to put it to. 8459382Seric ** oldstyle -- TRUE if this is an old style header. 8469382Seric ** m -- a pointer to the mailer descriptor. If NULL, 8479382Seric ** don't transform the name at all. 84855012Seric ** e -- the envelope containing the message. 8499382Seric ** 8509382Seric ** Returns: 8519382Seric ** none. 8529382Seric ** 8539382Seric ** Side Effects: 8549382Seric ** outputs "p" to file "fp". 8559382Seric */ 8569382Seric 85755012Seric commaize(h, p, fp, oldstyle, m, e) 8589382Seric register HDR *h; 8599382Seric register char *p; 8609382Seric FILE *fp; 8619382Seric bool oldstyle; 8629382Seric register MAILER *m; 86355012Seric register ENVELOPE *e; 8649382Seric { 8659382Seric register char *obp; 8669382Seric int opos; 8679382Seric bool firstone = TRUE; 86811157Seric char obuf[MAXLINE + 3]; 8699382Seric 8709382Seric /* 8719382Seric ** Output the address list translated by the 8729382Seric ** mailer and with commas. 8739382Seric */ 8749382Seric 8759382Seric if (tTd(14, 2)) 8769382Seric printf("commaize(%s: %s)\n", h->h_field, p); 8779382Seric 8789382Seric obp = obuf; 87959579Seric (void) sprintf(obp, "%s: ", h->h_field); 8809382Seric opos = strlen(h->h_field) + 2; 8819382Seric obp += opos; 8829382Seric 8839382Seric /* 8849382Seric ** Run through the list of values. 8859382Seric */ 8869382Seric 8879382Seric while (*p != '\0') 8889382Seric { 8899382Seric register char *name; 89054983Seric register int c; 8919382Seric char savechar; 89259163Seric int flags; 89359163Seric auto int stat; 8949382Seric 8959382Seric /* 8969382Seric ** Find the end of the name. New style names 8979382Seric ** end with a comma, old style names end with 8989382Seric ** a space character. However, spaces do not 8999382Seric ** necessarily delimit an old-style name -- at 9009382Seric ** signs mean keep going. 9019382Seric */ 9029382Seric 9039382Seric /* find end of name */ 90458050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 9059382Seric p++; 9069382Seric name = p; 9079382Seric for (;;) 9089382Seric { 90958333Seric auto char *oldp; 91016909Seric char pvpbuf[PSBUFSIZE]; 9119382Seric 91258333Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp); 91358333Seric p = oldp; 9149382Seric 9159382Seric /* look to see if we have an at sign */ 91658050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 9179382Seric p++; 9189382Seric 91958170Seric if (*p != '@') 9209382Seric { 9219382Seric p = oldp; 9229382Seric break; 9239382Seric } 9249382Seric p += *p == '@' ? 1 : 2; 92558050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 9269382Seric p++; 9279382Seric } 9289382Seric /* at the end of one complete name */ 9299382Seric 9309382Seric /* strip off trailing white space */ 93158050Seric while (p >= name && 93258050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 9339382Seric p--; 9349382Seric if (++p == name) 9359382Seric continue; 9369382Seric savechar = *p; 9379382Seric *p = '\0'; 9389382Seric 9399382Seric /* translate the name to be relative */ 94059163Seric flags = RF_HEADERADDR|RF_ADDDOMAIN; 94159163Seric if (bitset(H_FROM, h->h_flags)) 94259163Seric flags |= RF_SENDERADDR; 94359163Seric stat = EX_OK; 94459163Seric name = remotename(name, m, flags, &stat, e); 9459382Seric if (*name == '\0') 9469382Seric { 9479382Seric *p = savechar; 9489382Seric continue; 9499382Seric } 9509382Seric 9519382Seric /* output the name with nice formatting */ 95254983Seric opos += strlen(name); 9539382Seric if (!firstone) 9549382Seric opos += 2; 9559382Seric if (opos > 78 && !firstone) 9569382Seric { 95710178Seric (void) strcpy(obp, ",\n"); 95810176Seric putline(obuf, fp, m); 9599382Seric obp = obuf; 9609382Seric (void) sprintf(obp, " "); 96110161Seric opos = strlen(obp); 96210161Seric obp += opos; 96354983Seric opos += strlen(name); 9649382Seric } 9659382Seric else if (!firstone) 9669382Seric { 9679382Seric (void) sprintf(obp, ", "); 9689382Seric obp += 2; 9699382Seric } 9709382Seric 97154983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 97254983Seric *obp++ = c; 9739382Seric firstone = FALSE; 9749382Seric *p = savechar; 9759382Seric } 9769382Seric (void) strcpy(obp, "\n"); 97710176Seric putline(obuf, fp, m); 9789382Seric } 9799382Seric /* 98058170Seric ** COPYHEADER -- copy header list 9819382Seric ** 98258170Seric ** This routine is the equivalent of newstr for header lists 98358170Seric ** 9849382Seric ** Parameters: 98558170Seric ** header -- list of header structures to copy. 9869382Seric ** 9879382Seric ** Returns: 98858170Seric ** a copy of 'header'. 9899382Seric ** 9909382Seric ** Side Effects: 9919382Seric ** none. 9929382Seric */ 9939382Seric 99458170Seric HDR * 99558170Seric copyheader(header) 99658170Seric register HDR *header; 9979382Seric { 99858170Seric register HDR *newhdr; 99958170Seric HDR *ret; 100058170Seric register HDR **tail = &ret; 10019382Seric 100258170Seric while (header != NULL) 100358170Seric { 100458170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 100558170Seric STRUCTCOPY(*header, *newhdr); 100658170Seric *tail = newhdr; 100758170Seric tail = &newhdr->h_link; 100858170Seric header = header->h_link; 100958170Seric } 101058170Seric *tail = NULL; 101158170Seric 101258170Seric return ret; 10139382Seric } 1014