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*65089Seric static char sccsid[] = "@(#)headers.c 8.18 (Berkeley) 12/11/93"; 1133729Sbostic #endif /* not lint */ 1222706Sdist 134091Seric # include <errno.h> 144091Seric # include "sendmail.h" 154091Seric 164091Seric /* 174091Seric ** CHOMPHEADER -- process and save a header line. 184091Seric ** 194091Seric ** Called by collect and by readcf to deal with header lines. 204091Seric ** 214091Seric ** Parameters: 224091Seric ** line -- header as a text line. 234091Seric ** def -- if set, this is a default value. 2455012Seric ** e -- the envelope including this header. 254091Seric ** 264091Seric ** Returns: 274091Seric ** flags for this header. 284091Seric ** 294091Seric ** Side Effects: 304091Seric ** The header is saved on the header list. 314319Seric ** Contents of 'line' are destroyed. 324091Seric */ 334091Seric 3455012Seric chompheader(line, def, e) 354091Seric char *line; 364091Seric bool def; 3755012Seric register ENVELOPE *e; 384091Seric { 394091Seric register char *p; 404091Seric register HDR *h; 414091Seric HDR **hp; 424091Seric char *fname; 434091Seric char *fvalue; 444091Seric struct hdrinfo *hi; 459059Seric bool cond = FALSE; 4610689Seric BITMAP mopts; 4764351Seric char buf[MAXNAME]; 484091Seric 497677Seric if (tTd(31, 6)) 507677Seric printf("chompheader: %s\n", line); 517677Seric 524627Seric /* strip off options */ 5310689Seric clrbitmap(mopts); 544627Seric p = line; 5557405Seric if (*p == '?') 564627Seric { 574627Seric /* have some */ 5856795Seric register char *q = strchr(p + 1, *p); 594627Seric 604627Seric if (q != NULL) 614627Seric { 624627Seric *q++ = '\0'; 6310689Seric while (*++p != '\0') 6410689Seric setbitn(*p, mopts); 654627Seric p = q; 664627Seric } 674627Seric else 6858151Seric usrerr("553 header syntax error, line \"%s\"", line); 699059Seric cond = TRUE; 704627Seric } 714627Seric 724091Seric /* find canonical name */ 734627Seric fname = p; 7464149Seric while (isascii(*p) && isgraph(*p) && *p != ':') 7564149Seric p++; 7664149Seric fvalue = p; 7764149Seric while (isascii(*p) && isspace(*p)) 7864149Seric p++; 7964150Seric if (*p++ != ':' || fname == fvalue) 8010118Seric { 8158151Seric syserr("553 header syntax error, line \"%s\"", line); 8210118Seric return (0); 8310118Seric } 8464149Seric *fvalue = '\0'; 8564149Seric fvalue = p; 864091Seric 874091Seric /* strip field value on front */ 884091Seric if (*fvalue == ' ') 894091Seric fvalue++; 904091Seric 914091Seric /* see if it is a known type */ 924091Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 934091Seric { 9459579Seric if (strcasecmp(hi->hi_field, fname) == 0) 954091Seric break; 964091Seric } 974091Seric 9864283Seric if (tTd(31, 9)) 9964283Seric { 10064283Seric if (hi->hi_field == NULL) 10164283Seric printf("no header match\n"); 10264283Seric else 10364283Seric printf("header match, hi_flags=%o\n", hi->hi_flags); 10464283Seric } 10564283Seric 10611414Seric /* see if this is a resent message */ 10711930Seric if (!def && bitset(H_RESENT, hi->hi_flags)) 10855012Seric e->e_flags |= EF_RESENT; 10911414Seric 1104091Seric /* if this means "end of header" quit now */ 1114091Seric if (bitset(H_EOH, hi->hi_flags)) 1124091Seric return (hi->hi_flags); 1134091Seric 11464653Seric /* 11564653Seric ** Drop explicit From: if same as what we would generate. 11664653Seric ** This is to make MH (which doesn't always give a full name) 11764653Seric ** insert the full name information in all circumstances. 11864653Seric */ 11964653Seric 12014784Seric p = "resent-from"; 12155012Seric if (!bitset(EF_RESENT, e->e_flags)) 12214784Seric p += 7; 12359579Seric if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0) 12411414Seric { 12563753Seric if (tTd(31, 2)) 12663753Seric { 12763753Seric printf("comparing header from (%s) against default (%s or %s)\n", 12863753Seric fvalue, e->e_from.q_paddr, e->e_from.q_user); 12963753Seric } 13055012Seric if (e->e_from.q_paddr != NULL && 13163753Seric (strcmp(fvalue, e->e_from.q_paddr) == 0 || 13263753Seric strcmp(fvalue, e->e_from.q_user) == 0)) 13311414Seric return (hi->hi_flags); 13464351Seric #ifdef MAYBENEXTRELEASE /* XXX UNTESTED XXX UNTESTED XXX UNTESTED XXX */ 13564351Seric #ifdef USERDB 13664351Seric else 13764351Seric { 13864351Seric auto ADDRESS a; 13964351Seric char *fancy; 14064351Seric extern char *crackaddr(); 14164351Seric extern char *udbsender(); 14264351Seric 14364653Seric /* 14464653Seric ** Try doing USERDB rewriting even on fully commented 14564653Seric ** names; this saves the "comment" information (such 14664653Seric ** as full name) and rewrites the electronic part. 14764653Seric */ 14864653Seric 14964351Seric fancy = crackaddr(fvalue); 15064351Seric if (parseaddr(fvalue, &a, RF_COPYNONE, '\0', NULL, e) != NULL && 15164351Seric a.q_mailer == LocalMailer && 15264351Seric (p = udbsender(a.q_user)) != NULL) 15364351Seric { 15464351Seric char *oldg = macvalue('g', e); 15564351Seric 15664351Seric define('g', p, e); 15764351Seric expand(fancy, buf, &buf[sizeof buf], e); 15864351Seric define('g', oldg, e); 15964351Seric fvalue = buf; 16064351Seric } 16164351Seric } 16264351Seric #endif 16364351Seric #endif 16411414Seric } 16511414Seric 16614784Seric /* delete default value for this header */ 16755012Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 16814784Seric { 16959579Seric if (strcasecmp(fname, h->h_field) == 0 && 17014784Seric bitset(H_DEFAULT, h->h_flags) && 17114784Seric !bitset(H_FORCE, h->h_flags)) 17214784Seric h->h_value = NULL; 17314784Seric } 17414784Seric 17513012Seric /* create a new node */ 17613012Seric h = (HDR *) xalloc(sizeof *h); 17713012Seric h->h_field = newstr(fname); 17864134Seric h->h_value = newstr(fvalue); 17913012Seric h->h_link = NULL; 18023117Seric bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts); 18113012Seric *hp = h; 1828066Seric h->h_flags = hi->hi_flags; 1834091Seric if (def) 1844091Seric h->h_flags |= H_DEFAULT; 1859059Seric if (cond) 1869059Seric h->h_flags |= H_CHECK; 1874091Seric 1885937Seric /* hack to see if this is a new format message */ 1898095Seric if (!def && bitset(H_RCPT|H_FROM, h->h_flags) && 19056795Seric (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL || 19156795Seric strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL)) 1928089Seric { 19355012Seric e->e_flags &= ~EF_OLDSTYLE; 1948089Seric } 1955937Seric 1964091Seric return (h->h_flags); 1974091Seric } 1984091Seric /* 1996980Seric ** ADDHEADER -- add a header entry to the end of the queue. 2006980Seric ** 2016980Seric ** This bypasses the special checking of chompheader. 2026980Seric ** 2036980Seric ** Parameters: 20459579Seric ** field -- the name of the header field. 20558789Seric ** value -- the value of the field. 2066980Seric ** e -- the envelope to add them to. 2076980Seric ** 2086980Seric ** Returns: 2096980Seric ** none. 2106980Seric ** 2116980Seric ** Side Effects: 2126980Seric ** adds the field on the list of headers for this envelope. 2136980Seric */ 2146980Seric 2156980Seric addheader(field, value, e) 2166980Seric char *field; 2176980Seric char *value; 2186980Seric ENVELOPE *e; 2196980Seric { 2206980Seric register HDR *h; 2216980Seric register struct hdrinfo *hi; 2226980Seric HDR **hp; 2236980Seric 2246980Seric /* find info struct */ 2256980Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 2266980Seric { 22759579Seric if (strcasecmp(field, hi->hi_field) == 0) 2286980Seric break; 2296980Seric } 2306980Seric 2316980Seric /* find current place in list -- keep back pointer? */ 2326980Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 2336980Seric { 23459579Seric if (strcasecmp(field, h->h_field) == 0) 2356980Seric break; 2366980Seric } 2376980Seric 2386980Seric /* allocate space for new header */ 2396980Seric h = (HDR *) xalloc(sizeof *h); 2406980Seric h->h_field = field; 2416980Seric h->h_value = newstr(value); 2427368Seric h->h_link = *hp; 2436980Seric h->h_flags = hi->hi_flags | H_DEFAULT; 24410689Seric clrbitmap(h->h_mflags); 2456980Seric *hp = h; 2466980Seric } 2476980Seric /* 2484091Seric ** HVALUE -- return value of a header. 2494091Seric ** 2504091Seric ** Only "real" fields (i.e., ones that have not been supplied 2514091Seric ** as a default) are used. 2524091Seric ** 2534091Seric ** Parameters: 2544091Seric ** field -- the field name. 25555012Seric ** e -- the envelope containing the header. 2564091Seric ** 2574091Seric ** Returns: 2584091Seric ** pointer to the value part. 2594091Seric ** NULL if not found. 2604091Seric ** 2614091Seric ** Side Effects: 2629382Seric ** none. 2634091Seric */ 2644091Seric 2654091Seric char * 26655012Seric hvalue(field, e) 2674091Seric char *field; 26855012Seric register ENVELOPE *e; 2694091Seric { 2704091Seric register HDR *h; 2714091Seric 27255012Seric for (h = e->e_header; h != NULL; h = h->h_link) 2734091Seric { 27459579Seric if (!bitset(H_DEFAULT, h->h_flags) && 27559579Seric strcasecmp(h->h_field, field) == 0) 2764091Seric return (h->h_value); 2774091Seric } 2784091Seric return (NULL); 2794091Seric } 2804091Seric /* 2814091Seric ** ISHEADER -- predicate telling if argument is a header. 2824091Seric ** 2834319Seric ** A line is a header if it has a single word followed by 2844319Seric ** optional white space followed by a colon. 2854319Seric ** 2864091Seric ** Parameters: 2874091Seric ** s -- string to check for possible headerness. 2884091Seric ** 2894091Seric ** Returns: 2904091Seric ** TRUE if s is a header. 2914091Seric ** FALSE otherwise. 2924091Seric ** 2934091Seric ** Side Effects: 2944091Seric ** none. 2954091Seric */ 2964091Seric 2974091Seric bool 2984091Seric isheader(s) 2994091Seric register char *s; 3004091Seric { 3019382Seric while (*s > ' ' && *s != ':' && *s != '\0') 3024091Seric s++; 3039382Seric 3049382Seric /* following technically violates RFC822 */ 30558050Seric while (isascii(*s) && isspace(*s)) 3064091Seric s++; 3079382Seric 3084091Seric return (*s == ':'); 3094091Seric } 3105919Seric /* 3117783Seric ** EATHEADER -- run through the stored header and extract info. 3127783Seric ** 3137783Seric ** Parameters: 3149382Seric ** e -- the envelope to process. 31558929Seric ** full -- if set, do full processing (e.g., compute 31658929Seric ** message priority). 3177783Seric ** 3187783Seric ** Returns: 3197783Seric ** none. 3207783Seric ** 3217783Seric ** Side Effects: 3227783Seric ** Sets a bunch of global variables from information 3239382Seric ** in the collected header. 3249382Seric ** Aborts the message if the hop count is exceeded. 3257783Seric */ 3267783Seric 32758929Seric eatheader(e, full) 3289382Seric register ENVELOPE *e; 32958929Seric bool full; 3307783Seric { 3317783Seric register HDR *h; 3327783Seric register char *p; 3339382Seric int hopcnt = 0; 33457208Seric char *msgid; 33558688Seric char buf[MAXLINE]; 3367783Seric 33758688Seric /* 33858688Seric ** Set up macros for possible expansion in headers. 33958688Seric */ 34058688Seric 34158704Seric define('f', e->e_sender, e); 34258704Seric define('g', e->e_sender, e); 34364148Seric if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0') 34464148Seric define('u', e->e_origrcpt, e); 34564148Seric else 34664148Seric define('u', NULL, e); 34758688Seric 34865052Seric /* full name of from person */ 34965052Seric p = hvalue("full-name", e); 35065052Seric if (p != NULL) 35165052Seric define('x', p, e); 35265052Seric 3539382Seric if (tTd(32, 1)) 3549382Seric printf("----- collected header -----\n"); 35557208Seric msgid = "<none>"; 3569382Seric for (h = e->e_header; h != NULL; h = h->h_link) 3577783Seric { 35864156Seric if (h->h_value == NULL) 35964156Seric { 36064156Seric if (tTd(32, 1)) 36164156Seric printf("%s: <NULL>\n", h->h_field); 36264156Seric continue; 36364156Seric } 36464156Seric 36558688Seric /* do early binding */ 36664156Seric if (bitset(H_DEFAULT, h->h_flags)) 36758688Seric { 36858688Seric expand(h->h_value, buf, &buf[sizeof buf], e); 36958688Seric if (buf[0] != '\0') 37058688Seric { 37158688Seric h->h_value = newstr(buf); 37258688Seric h->h_flags &= ~H_DEFAULT; 37358688Seric } 37458688Seric } 37558688Seric 3769382Seric if (tTd(32, 1)) 37759579Seric printf("%s: %s\n", h->h_field, h->h_value); 37857359Seric 37911414Seric /* count the number of times it has been processed */ 3809382Seric if (bitset(H_TRACE, h->h_flags)) 3819382Seric hopcnt++; 38211414Seric 38311414Seric /* send to this person if we so desire */ 38411414Seric if (GrabTo && bitset(H_RCPT, h->h_flags) && 38511414Seric !bitset(H_DEFAULT, h->h_flags) && 38655012Seric (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags))) 38711414Seric { 38863839Seric int saveflags = e->e_flags; 38963839Seric 39064283Seric (void) sendtolist(h->h_value, NULLADDR, 39158082Seric &e->e_sendqueue, e); 39263839Seric 39363839Seric /* delete fatal errors generated by this address */ 39464148Seric if (!GrabTo && !bitset(EF_FATALERRS, saveflags)) 39563839Seric e->e_flags &= ~EF_FATALERRS; 39611414Seric } 39711414Seric 39857208Seric /* save the message-id for logging */ 39964156Seric if (full && strcasecmp(h->h_field, "message-id") == 0) 40011290Seric { 40157208Seric msgid = h->h_value; 40259859Seric while (isascii(*msgid) && isspace(*msgid)) 40359859Seric msgid++; 40411290Seric } 40557359Seric 40657359Seric /* see if this is a return-receipt header */ 40757359Seric if (bitset(H_RECEIPTTO, h->h_flags)) 40857359Seric e->e_receiptto = h->h_value; 40957359Seric 41057359Seric /* see if this is an errors-to header */ 41161104Seric if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags)) 41264283Seric (void) sendtolist(h->h_value, NULLADDR, 41358082Seric &e->e_errorqueue, e); 4149382Seric } 4159382Seric if (tTd(32, 1)) 4167783Seric printf("----------------------------\n"); 4177783Seric 41858145Seric /* if we are just verifying (that is, sendmail -t -bv), drop out now */ 41958145Seric if (OpMode == MD_VERIFY) 42058145Seric return; 42158145Seric 4229382Seric /* store hop count */ 4239382Seric if (hopcnt > e->e_hopcount) 4249382Seric e->e_hopcount = hopcnt; 4259382Seric 4267783Seric /* message priority */ 42755012Seric p = hvalue("precedence", e); 4289382Seric if (p != NULL) 4299382Seric e->e_class = priencode(p); 43058929Seric if (full) 43125013Seric e->e_msgpriority = e->e_msgsize 43224981Seric - e->e_class * WkClassFact 43324981Seric + e->e_nrcpts * WkRecipFact; 4347783Seric 4357783Seric /* date message originated */ 43655012Seric p = hvalue("posted-date", e); 4377783Seric if (p == NULL) 43855012Seric p = hvalue("date", e); 4397783Seric if (p != NULL) 4409382Seric define('a', p, e); 44111290Seric 44211290Seric /* 44311290Seric ** Log collection information. 44411290Seric */ 44511290Seric 44611290Seric # ifdef LOG 44758929Seric if (full && LogLevel > 4) 448*65089Seric logsender(e, msgid); 449*65089Seric # endif /* LOG */ 450*65089Seric e->e_flags &= ~EF_LOGSENDER; 451*65089Seric } 452*65089Seric /* 453*65089Seric ** LOGSENDER -- log sender information 454*65089Seric ** 455*65089Seric ** Parameters: 456*65089Seric ** e -- the envelope to log 457*65089Seric ** msgid -- the message id 458*65089Seric ** 459*65089Seric ** Returns: 460*65089Seric ** none 461*65089Seric */ 462*65089Seric 463*65089Seric logsender(e, msgid) 464*65089Seric register ENVELOPE *e; 465*65089Seric char *msgid; 466*65089Seric { 467*65089Seric char *name; 468*65089Seric register char *sbp; 469*65089Seric register char *p; 470*65089Seric char hbuf[MAXNAME]; 471*65089Seric char sbuf[MAXLINE]; 472*65089Seric 473*65089Seric if (bitset(EF_RESPONSE, e->e_flags)) 474*65089Seric name = "[RESPONSE]"; 475*65089Seric else if ((name = macvalue('_', e)) != NULL) 476*65089Seric ; 477*65089Seric else if (RealHostName[0] == '[') 478*65089Seric name = RealHostName; 479*65089Seric else 48011290Seric { 481*65089Seric name = hbuf; 482*65089Seric (void) sprintf(hbuf, "%.80s", RealHostName); 483*65089Seric if (RealHostAddr.sa.sa_family != 0) 48457359Seric { 485*65089Seric p = &hbuf[strlen(hbuf)]; 486*65089Seric (void) sprintf(p, " (%s)", 487*65089Seric anynet_ntoa(&RealHostAddr)); 48857359Seric } 489*65089Seric } 49057359Seric 491*65089Seric /* some versions of syslog only take 5 printf args */ 49265059Seric # if (SYSLOG_BUFSIZE) >= 256 493*65089Seric sbp = sbuf; 494*65089Seric sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d", 495*65089Seric e->e_from.q_paddr, e->e_msgsize, e->e_class, 496*65089Seric e->e_msgpriority, e->e_nrcpts); 497*65089Seric sbp += strlen(sbp); 498*65089Seric if (msgid != NULL) 499*65089Seric { 500*65089Seric sprintf(sbp, ", msgid=%.100s", msgid); 50160575Seric sbp += strlen(sbp); 502*65089Seric } 503*65089Seric if (e->e_bodytype != NULL) 504*65089Seric { 505*65089Seric (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype); 506*65089Seric sbp += strlen(sbp); 507*65089Seric } 508*65089Seric p = macvalue('r', e); 509*65089Seric if (p != NULL) 510*65089Seric (void) sprintf(sbp, ", proto=%.20s", p); 511*65089Seric syslog(LOG_INFO, "%s: %s, relay=%s", 512*65089Seric e->e_id, sbuf, name); 51365059Seric 51465059Seric # else /* short syslog buffer */ 51565059Seric 516*65089Seric syslog(LOG_INFO, "%s: from=%s", 517*65089Seric e->e_id, shortenstring(e->e_from.q_paddr, 83)); 518*65089Seric syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d", 519*65089Seric e->e_id, e->e_msgsize, e->e_class, 520*65089Seric e->e_msgpriority, e->e_nrcpts); 521*65089Seric if (msgid != NULL) 52265059Seric syslog(LOG_INFO, "%s: msgid=%s", e->e_id, msgid); 523*65089Seric if (e->e_bodytype != NULL) 524*65089Seric syslog(LOG_INFO, "%s: bodytype=%s", e->e_id, e->e_bodytype); 525*65089Seric p = macvalue('r', e); 526*65089Seric if (p != NULL) 527*65089Seric syslog(LOG_INFO, "%s: proto=%s", e->e_id, p); 528*65089Seric syslog(LOG_INFO, "%s: relay=%s", e->e_id, name); 52965059Seric # endif 5307783Seric } 5317783Seric /* 5327783Seric ** PRIENCODE -- encode external priority names into internal values. 5337783Seric ** 5347783Seric ** Parameters: 5357783Seric ** p -- priority in ascii. 5367783Seric ** 5377783Seric ** Returns: 5387783Seric ** priority as a numeric level. 5397783Seric ** 5407783Seric ** Side Effects: 5417783Seric ** none. 5427783Seric */ 5437783Seric 5447783Seric priencode(p) 5457783Seric char *p; 5467783Seric { 5478253Seric register int i; 5487783Seric 5498253Seric for (i = 0; i < NumPriorities; i++) 5507783Seric { 55133725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 5528253Seric return (Priorities[i].pri_val); 5537783Seric } 5548253Seric 5558253Seric /* unknown priority */ 5568253Seric return (0); 5577783Seric } 5587783Seric /* 5597890Seric ** CRACKADDR -- parse an address and turn it into a macro 5607783Seric ** 5617783Seric ** This doesn't actually parse the address -- it just extracts 5627783Seric ** it and replaces it with "$g". The parse is totally ad hoc 5637783Seric ** and isn't even guaranteed to leave something syntactically 5647783Seric ** identical to what it started with. However, it does leave 5657783Seric ** something semantically identical. 5667783Seric ** 56751379Seric ** This algorithm has been cleaned up to handle a wider range 56851379Seric ** of cases -- notably quoted and backslash escaped strings. 56951379Seric ** This modification makes it substantially better at preserving 57051379Seric ** the original syntax. 5717783Seric ** 5727783Seric ** Parameters: 5737890Seric ** addr -- the address to be cracked. 5747783Seric ** 5757783Seric ** Returns: 5767783Seric ** a pointer to the new version. 5777783Seric ** 5787783Seric ** Side Effects: 5797890Seric ** none. 5807783Seric ** 5817783Seric ** Warning: 5827783Seric ** The return value is saved in local storage and should 5837783Seric ** be copied if it is to be reused. 5847783Seric */ 5857783Seric 5867783Seric char * 5877890Seric crackaddr(addr) 5887890Seric register char *addr; 5897783Seric { 5907783Seric register char *p; 59151379Seric register char c; 59251379Seric int cmtlev; 59356764Seric int realcmtlev; 59456764Seric int anglelev, realanglelev; 59551379Seric int copylev; 59651379Seric bool qmode; 59756764Seric bool realqmode; 59856764Seric bool skipping; 59951379Seric bool putgmac = FALSE; 60056735Seric bool quoteit = FALSE; 60164148Seric bool gotangle = FALSE; 60251379Seric register char *bp; 60356764Seric char *buflim; 6047783Seric static char buf[MAXNAME]; 6057783Seric 6067783Seric if (tTd(33, 1)) 6077890Seric printf("crackaddr(%s)\n", addr); 6087783Seric 6098082Seric /* strip leading spaces */ 61058050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 6118082Seric addr++; 6128082Seric 6137783Seric /* 61451379Seric ** Start by assuming we have no angle brackets. This will be 61551379Seric ** adjusted later if we find them. 6167783Seric */ 6177783Seric 61851379Seric bp = buf; 61956764Seric buflim = &buf[sizeof buf - 5]; 62051379Seric p = addr; 62156764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 62256764Seric qmode = realqmode = FALSE; 62351379Seric 62451379Seric while ((c = *p++) != '\0') 6257783Seric { 62656764Seric /* 62756764Seric ** If the buffer is overful, go into a special "skipping" 62856764Seric ** mode that tries to keep legal syntax but doesn't actually 62956764Seric ** output things. 63056764Seric */ 6317783Seric 63256764Seric skipping = bp >= buflim; 63356735Seric 63456764Seric if (copylev > 0 && !skipping) 63556764Seric *bp++ = c; 63656735Seric 63751379Seric /* check for backslash escapes */ 63851379Seric if (c == '\\') 6397783Seric { 64058890Seric /* arrange to quote the address */ 64158890Seric if (cmtlev <= 0 && !qmode) 64258890Seric quoteit = TRUE; 64358890Seric 64451379Seric if ((c = *p++) == '\0') 6457783Seric { 64651379Seric /* too far */ 64751379Seric p--; 64851379Seric goto putg; 6497783Seric } 65056764Seric if (copylev > 0 && !skipping) 65151379Seric *bp++ = c; 65251379Seric goto putg; 6537783Seric } 6547783Seric 65551379Seric /* check for quoted strings */ 65664967Seric if (c == '"' && cmtlev <= 0) 6577783Seric { 65851379Seric qmode = !qmode; 65956764Seric if (copylev > 0 && !skipping) 66056764Seric realqmode = !realqmode; 66151379Seric continue; 6627783Seric } 66351379Seric if (qmode) 66451379Seric goto putg; 6657783Seric 66651379Seric /* check for comments */ 66751379Seric if (c == '(') 6687783Seric { 66951379Seric cmtlev++; 67056764Seric 67156764Seric /* allow space for closing paren */ 67256764Seric if (!skipping) 67356764Seric { 67456764Seric buflim--; 67556764Seric realcmtlev++; 67656764Seric if (copylev++ <= 0) 67756764Seric { 67856764Seric *bp++ = ' '; 67956764Seric *bp++ = c; 68056764Seric } 68156764Seric } 68251379Seric } 68351379Seric if (cmtlev > 0) 68451379Seric { 68551379Seric if (c == ')') 6867783Seric { 68751379Seric cmtlev--; 68851379Seric copylev--; 68956764Seric if (!skipping) 69056764Seric { 69156764Seric realcmtlev--; 69256764Seric buflim++; 69356764Seric } 6947783Seric } 6957783Seric continue; 6967783Seric } 69756764Seric else if (c == ')') 69856764Seric { 69956764Seric /* syntax error: unmatched ) */ 70056764Seric if (!skipping) 70156764Seric bp--; 70256764Seric } 7037783Seric 70456764Seric /* check for characters that may have to be quoted */ 70564148Seric if (strchr(".'@,;:\\()[]", c) != NULL) 70656764Seric { 70756764Seric /* 70856764Seric ** If these occur as the phrase part of a <> 70956764Seric ** construct, but are not inside of () or already 71056764Seric ** quoted, they will have to be quoted. Note that 71156764Seric ** now (but don't actually do the quoting). 71256764Seric */ 71356764Seric 71456764Seric if (cmtlev <= 0 && !qmode) 71556764Seric quoteit = TRUE; 71656764Seric } 71756764Seric 71851379Seric /* check for angle brackets */ 71951379Seric if (c == '<') 72051379Seric { 72156735Seric register char *q; 72256735Seric 72364148Seric /* assume first of two angles is bogus */ 72464148Seric if (gotangle) 72564148Seric quoteit = TRUE; 72664148Seric gotangle = TRUE; 72764148Seric 72851379Seric /* oops -- have to change our mind */ 72964752Seric anglelev = 1; 73056764Seric if (!skipping) 73164752Seric realanglelev = 1; 73256764Seric 73356735Seric bp = buf; 73456735Seric if (quoteit) 73556735Seric { 73656735Seric *bp++ = '"'; 73756735Seric 73856735Seric /* back up over the '<' and any spaces */ 73956735Seric --p; 74058050Seric while (isascii(*--p) && isspace(*p)) 74156735Seric continue; 74256735Seric p++; 74356735Seric } 74456735Seric for (q = addr; q < p; ) 74556735Seric { 74656735Seric c = *q++; 74756764Seric if (bp < buflim) 74856764Seric { 74956764Seric if (quoteit && c == '"') 75056764Seric *bp++ = '\\'; 75156764Seric *bp++ = c; 75256764Seric } 75356735Seric } 75456735Seric if (quoteit) 75556735Seric { 75664148Seric if (bp == &buf[1]) 75764148Seric bp--; 75864148Seric else 75964148Seric *bp++ = '"'; 76056764Seric while ((c = *p++) != '<') 76156764Seric { 76256764Seric if (bp < buflim) 76356764Seric *bp++ = c; 76456764Seric } 76556764Seric *bp++ = c; 76656735Seric } 76751379Seric copylev = 0; 76856735Seric putgmac = quoteit = FALSE; 76951379Seric continue; 77051379Seric } 7717783Seric 77251379Seric if (c == '>') 7737783Seric { 77456764Seric if (anglelev > 0) 77556764Seric { 77656764Seric anglelev--; 77756764Seric if (!skipping) 77856764Seric { 77956764Seric realanglelev--; 78056764Seric buflim++; 78156764Seric } 78256764Seric } 78356764Seric else if (!skipping) 78456764Seric { 78556764Seric /* syntax error: unmatched > */ 78656764Seric if (copylev > 0) 78756764Seric bp--; 78864752Seric quoteit = TRUE; 78956764Seric continue; 79056764Seric } 79151379Seric if (copylev++ <= 0) 79251379Seric *bp++ = c; 79351379Seric continue; 7947783Seric } 79551379Seric 79651379Seric /* must be a real address character */ 79751379Seric putg: 79851379Seric if (copylev <= 0 && !putgmac) 79951379Seric { 80058050Seric *bp++ = MACROEXPAND; 80151379Seric *bp++ = 'g'; 80251379Seric putgmac = TRUE; 80351379Seric } 8047783Seric } 8057783Seric 80656764Seric /* repair any syntactic damage */ 80756764Seric if (realqmode) 80856764Seric *bp++ = '"'; 80956764Seric while (realcmtlev-- > 0) 81056764Seric *bp++ = ')'; 81156764Seric while (realanglelev-- > 0) 81256764Seric *bp++ = '>'; 81351379Seric *bp++ = '\0'; 8147783Seric 8157783Seric if (tTd(33, 1)) 8167944Seric printf("crackaddr=>`%s'\n", buf); 8177783Seric 8187783Seric return (buf); 8197783Seric } 8209382Seric /* 8219382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 8229382Seric ** 8239382Seric ** Parameters: 8249382Seric ** fp -- file to put it on. 8259382Seric ** m -- mailer to use. 8269382Seric ** e -- envelope to use. 8279382Seric ** 8289382Seric ** Returns: 8299382Seric ** none. 8309382Seric ** 8319382Seric ** Side Effects: 8329382Seric ** none. 8339382Seric */ 8349382Seric 83557589Seric /* 83657589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 83757589Seric */ 83857589Seric #ifndef MAX 83957589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 84057589Seric #endif 84157589Seric 84210176Seric putheader(fp, m, e) 8439382Seric register FILE *fp; 8449382Seric register MAILER *m; 8459382Seric register ENVELOPE *e; 8469382Seric { 84757135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 8489382Seric register HDR *h; 84957135Seric char obuf[MAXLINE]; 8509382Seric 85159882Seric if (tTd(34, 1)) 85259882Seric printf("--- putheader, mailer = %s ---\n", m->m_name); 85359882Seric 8549382Seric for (h = e->e_header; h != NULL; h = h->h_link) 8559382Seric { 8569382Seric register char *p; 85710689Seric extern bool bitintersect(); 8589382Seric 85959882Seric if (tTd(34, 11)) 86059882Seric { 86159882Seric printf(" %s: ", h->h_field); 86259882Seric xputs(h->h_value); 86359882Seric } 86459882Seric 8659382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 86610689Seric !bitintersect(h->h_mflags, m->m_flags)) 86759882Seric { 86859882Seric if (tTd(34, 11)) 86959882Seric printf(" (skipped)\n"); 8709382Seric continue; 87159882Seric } 8729382Seric 87311414Seric /* handle Resent-... headers specially */ 87411414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 87559882Seric { 87659882Seric if (tTd(34, 11)) 87759882Seric printf(" (skipped (resent))\n"); 87811414Seric continue; 87959882Seric } 88059882Seric if (tTd(34, 11)) 88159882Seric printf("\n"); 88211414Seric 8839382Seric p = h->h_value; 8849382Seric if (bitset(H_DEFAULT, h->h_flags)) 8859382Seric { 8869382Seric /* macro expand value if generated internally */ 8879382Seric expand(p, buf, &buf[sizeof buf], e); 8889382Seric p = buf; 8899382Seric if (p == NULL || *p == '\0') 8909382Seric continue; 8919382Seric } 8929382Seric 8939382Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 8949382Seric { 8959382Seric /* address field */ 8969382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 8979382Seric 8989382Seric if (bitset(H_FROM, h->h_flags)) 8999382Seric oldstyle = FALSE; 90055012Seric commaize(h, p, fp, oldstyle, m, e); 9019382Seric } 9029382Seric else 9039382Seric { 9049382Seric /* vanilla header line */ 90512159Seric register char *nlp; 90612159Seric 90759579Seric (void) sprintf(obuf, "%s: ", h->h_field); 90856795Seric while ((nlp = strchr(p, '\n')) != NULL) 90912159Seric { 91012159Seric *nlp = '\0'; 91112159Seric (void) strcat(obuf, p); 91212159Seric *nlp = '\n'; 91312159Seric putline(obuf, fp, m); 91412159Seric p = ++nlp; 91512161Seric obuf[0] = '\0'; 91612159Seric } 91712159Seric (void) strcat(obuf, p); 91810176Seric putline(obuf, fp, m); 9199382Seric } 9209382Seric } 9219382Seric } 9229382Seric /* 9239382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 9249382Seric ** 9259382Seric ** Parameters: 9269382Seric ** h -- the header field to output. 9279382Seric ** p -- the value to put in it. 9289382Seric ** fp -- file to put it to. 9299382Seric ** oldstyle -- TRUE if this is an old style header. 9309382Seric ** m -- a pointer to the mailer descriptor. If NULL, 9319382Seric ** don't transform the name at all. 93255012Seric ** e -- the envelope containing the message. 9339382Seric ** 9349382Seric ** Returns: 9359382Seric ** none. 9369382Seric ** 9379382Seric ** Side Effects: 9389382Seric ** outputs "p" to file "fp". 9399382Seric */ 9409382Seric 94155012Seric commaize(h, p, fp, oldstyle, m, e) 9429382Seric register HDR *h; 9439382Seric register char *p; 9449382Seric FILE *fp; 9459382Seric bool oldstyle; 9469382Seric register MAILER *m; 94755012Seric register ENVELOPE *e; 9489382Seric { 9499382Seric register char *obp; 9509382Seric int opos; 9519382Seric bool firstone = TRUE; 95211157Seric char obuf[MAXLINE + 3]; 9539382Seric 9549382Seric /* 9559382Seric ** Output the address list translated by the 9569382Seric ** mailer and with commas. 9579382Seric */ 9589382Seric 9599382Seric if (tTd(14, 2)) 9609382Seric printf("commaize(%s: %s)\n", h->h_field, p); 9619382Seric 9629382Seric obp = obuf; 96359579Seric (void) sprintf(obp, "%s: ", h->h_field); 9649382Seric opos = strlen(h->h_field) + 2; 9659382Seric obp += opos; 9669382Seric 9679382Seric /* 9689382Seric ** Run through the list of values. 9699382Seric */ 9709382Seric 9719382Seric while (*p != '\0') 9729382Seric { 9739382Seric register char *name; 97454983Seric register int c; 9759382Seric char savechar; 97659163Seric int flags; 97759163Seric auto int stat; 9789382Seric 9799382Seric /* 9809382Seric ** Find the end of the name. New style names 9819382Seric ** end with a comma, old style names end with 9829382Seric ** a space character. However, spaces do not 9839382Seric ** necessarily delimit an old-style name -- at 9849382Seric ** signs mean keep going. 9859382Seric */ 9869382Seric 9879382Seric /* find end of name */ 98858050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 9899382Seric p++; 9909382Seric name = p; 9919382Seric for (;;) 9929382Seric { 99358333Seric auto char *oldp; 99416909Seric char pvpbuf[PSBUFSIZE]; 9959382Seric 99665066Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, 99765066Seric sizeof pvpbuf, &oldp); 99858333Seric p = oldp; 9999382Seric 10009382Seric /* look to see if we have an at sign */ 100158050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 10029382Seric p++; 10039382Seric 100458170Seric if (*p != '@') 10059382Seric { 10069382Seric p = oldp; 10079382Seric break; 10089382Seric } 10099382Seric p += *p == '@' ? 1 : 2; 101058050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 10119382Seric p++; 10129382Seric } 10139382Seric /* at the end of one complete name */ 10149382Seric 10159382Seric /* strip off trailing white space */ 101658050Seric while (p >= name && 101758050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 10189382Seric p--; 10199382Seric if (++p == name) 10209382Seric continue; 10219382Seric savechar = *p; 10229382Seric *p = '\0'; 10239382Seric 10249382Seric /* translate the name to be relative */ 102559163Seric flags = RF_HEADERADDR|RF_ADDDOMAIN; 102659163Seric if (bitset(H_FROM, h->h_flags)) 102759163Seric flags |= RF_SENDERADDR; 102859163Seric stat = EX_OK; 102959163Seric name = remotename(name, m, flags, &stat, e); 10309382Seric if (*name == '\0') 10319382Seric { 10329382Seric *p = savechar; 10339382Seric continue; 10349382Seric } 10359382Seric 10369382Seric /* output the name with nice formatting */ 103754983Seric opos += strlen(name); 10389382Seric if (!firstone) 10399382Seric opos += 2; 10409382Seric if (opos > 78 && !firstone) 10419382Seric { 104210178Seric (void) strcpy(obp, ",\n"); 104310176Seric putline(obuf, fp, m); 10449382Seric obp = obuf; 10459382Seric (void) sprintf(obp, " "); 104610161Seric opos = strlen(obp); 104710161Seric obp += opos; 104854983Seric opos += strlen(name); 10499382Seric } 10509382Seric else if (!firstone) 10519382Seric { 10529382Seric (void) sprintf(obp, ", "); 10539382Seric obp += 2; 10549382Seric } 10559382Seric 105654983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 105754983Seric *obp++ = c; 10589382Seric firstone = FALSE; 10599382Seric *p = savechar; 10609382Seric } 10619382Seric (void) strcpy(obp, "\n"); 106210176Seric putline(obuf, fp, m); 10639382Seric } 10649382Seric /* 106558170Seric ** COPYHEADER -- copy header list 10669382Seric ** 106758170Seric ** This routine is the equivalent of newstr for header lists 106858170Seric ** 10699382Seric ** Parameters: 107058170Seric ** header -- list of header structures to copy. 10719382Seric ** 10729382Seric ** Returns: 107358170Seric ** a copy of 'header'. 10749382Seric ** 10759382Seric ** Side Effects: 10769382Seric ** none. 10779382Seric */ 10789382Seric 107958170Seric HDR * 108058170Seric copyheader(header) 108158170Seric register HDR *header; 10829382Seric { 108358170Seric register HDR *newhdr; 108458170Seric HDR *ret; 108558170Seric register HDR **tail = &ret; 10869382Seric 108758170Seric while (header != NULL) 108858170Seric { 108958170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 109058170Seric STRUCTCOPY(*header, *newhdr); 109158170Seric *tail = newhdr; 109258170Seric tail = &newhdr->h_link; 109358170Seric header = header->h_link; 109458170Seric } 109558170Seric *tail = NULL; 109658170Seric 109758170Seric return ret; 10989382Seric } 1099