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*65152Seric static char sccsid[] = "@(#)headers.c 8.19 (Berkeley) 12/15/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)) 377*65152Seric { 378*65152Seric printf("%s: ", h->h_field); 379*65152Seric xputs(h->h_value); 380*65152Seric printf("\n"); 381*65152Seric } 38257359Seric 38311414Seric /* count the number of times it has been processed */ 3849382Seric if (bitset(H_TRACE, h->h_flags)) 3859382Seric hopcnt++; 38611414Seric 38711414Seric /* send to this person if we so desire */ 38811414Seric if (GrabTo && bitset(H_RCPT, h->h_flags) && 38911414Seric !bitset(H_DEFAULT, h->h_flags) && 39055012Seric (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags))) 39111414Seric { 39263839Seric int saveflags = e->e_flags; 39363839Seric 39464283Seric (void) sendtolist(h->h_value, NULLADDR, 39558082Seric &e->e_sendqueue, e); 39663839Seric 39763839Seric /* delete fatal errors generated by this address */ 39864148Seric if (!GrabTo && !bitset(EF_FATALERRS, saveflags)) 39963839Seric e->e_flags &= ~EF_FATALERRS; 40011414Seric } 40111414Seric 40257208Seric /* save the message-id for logging */ 40364156Seric if (full && strcasecmp(h->h_field, "message-id") == 0) 40411290Seric { 40557208Seric msgid = h->h_value; 40659859Seric while (isascii(*msgid) && isspace(*msgid)) 40759859Seric msgid++; 40811290Seric } 40957359Seric 41057359Seric /* see if this is a return-receipt header */ 41157359Seric if (bitset(H_RECEIPTTO, h->h_flags)) 41257359Seric e->e_receiptto = h->h_value; 41357359Seric 41457359Seric /* see if this is an errors-to header */ 41561104Seric if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags)) 41664283Seric (void) sendtolist(h->h_value, NULLADDR, 41758082Seric &e->e_errorqueue, e); 4189382Seric } 4199382Seric if (tTd(32, 1)) 4207783Seric printf("----------------------------\n"); 4217783Seric 42258145Seric /* if we are just verifying (that is, sendmail -t -bv), drop out now */ 42358145Seric if (OpMode == MD_VERIFY) 42458145Seric return; 42558145Seric 4269382Seric /* store hop count */ 4279382Seric if (hopcnt > e->e_hopcount) 4289382Seric e->e_hopcount = hopcnt; 4299382Seric 4307783Seric /* message priority */ 43155012Seric p = hvalue("precedence", e); 4329382Seric if (p != NULL) 4339382Seric e->e_class = priencode(p); 43458929Seric if (full) 43525013Seric e->e_msgpriority = e->e_msgsize 43624981Seric - e->e_class * WkClassFact 43724981Seric + e->e_nrcpts * WkRecipFact; 4387783Seric 4397783Seric /* date message originated */ 44055012Seric p = hvalue("posted-date", e); 4417783Seric if (p == NULL) 44255012Seric p = hvalue("date", e); 4437783Seric if (p != NULL) 4449382Seric define('a', p, e); 44511290Seric 44611290Seric /* 44711290Seric ** Log collection information. 44811290Seric */ 44911290Seric 45011290Seric # ifdef LOG 45158929Seric if (full && LogLevel > 4) 45265089Seric logsender(e, msgid); 45365089Seric # endif /* LOG */ 45465089Seric e->e_flags &= ~EF_LOGSENDER; 45565089Seric } 45665089Seric /* 45765089Seric ** LOGSENDER -- log sender information 45865089Seric ** 45965089Seric ** Parameters: 46065089Seric ** e -- the envelope to log 46165089Seric ** msgid -- the message id 46265089Seric ** 46365089Seric ** Returns: 46465089Seric ** none 46565089Seric */ 46665089Seric 46765089Seric logsender(e, msgid) 46865089Seric register ENVELOPE *e; 46965089Seric char *msgid; 47065089Seric { 47165089Seric char *name; 47265089Seric register char *sbp; 47365089Seric register char *p; 47465089Seric char hbuf[MAXNAME]; 47565089Seric char sbuf[MAXLINE]; 47665089Seric 47765089Seric if (bitset(EF_RESPONSE, e->e_flags)) 47865089Seric name = "[RESPONSE]"; 47965089Seric else if ((name = macvalue('_', e)) != NULL) 48065089Seric ; 48165089Seric else if (RealHostName[0] == '[') 48265089Seric name = RealHostName; 48365089Seric else 48411290Seric { 48565089Seric name = hbuf; 48665089Seric (void) sprintf(hbuf, "%.80s", RealHostName); 48765089Seric if (RealHostAddr.sa.sa_family != 0) 48857359Seric { 48965089Seric p = &hbuf[strlen(hbuf)]; 49065089Seric (void) sprintf(p, " (%s)", 49165089Seric anynet_ntoa(&RealHostAddr)); 49257359Seric } 49365089Seric } 49457359Seric 49565089Seric /* some versions of syslog only take 5 printf args */ 49665059Seric # if (SYSLOG_BUFSIZE) >= 256 49765089Seric sbp = sbuf; 49865089Seric sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d", 49965089Seric e->e_from.q_paddr, e->e_msgsize, e->e_class, 50065089Seric e->e_msgpriority, e->e_nrcpts); 50165089Seric sbp += strlen(sbp); 50265089Seric if (msgid != NULL) 50365089Seric { 50465089Seric sprintf(sbp, ", msgid=%.100s", msgid); 50560575Seric sbp += strlen(sbp); 50665089Seric } 50765089Seric if (e->e_bodytype != NULL) 50865089Seric { 50965089Seric (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype); 51065089Seric sbp += strlen(sbp); 51165089Seric } 51265089Seric p = macvalue('r', e); 51365089Seric if (p != NULL) 51465089Seric (void) sprintf(sbp, ", proto=%.20s", p); 51565089Seric syslog(LOG_INFO, "%s: %s, relay=%s", 51665089Seric e->e_id, sbuf, name); 51765059Seric 51865059Seric # else /* short syslog buffer */ 51965059Seric 52065089Seric syslog(LOG_INFO, "%s: from=%s", 52165089Seric e->e_id, shortenstring(e->e_from.q_paddr, 83)); 52265089Seric syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d", 52365089Seric e->e_id, e->e_msgsize, e->e_class, 52465089Seric e->e_msgpriority, e->e_nrcpts); 52565089Seric if (msgid != NULL) 52665059Seric syslog(LOG_INFO, "%s: msgid=%s", e->e_id, msgid); 52765089Seric if (e->e_bodytype != NULL) 52865089Seric syslog(LOG_INFO, "%s: bodytype=%s", e->e_id, e->e_bodytype); 52965089Seric p = macvalue('r', e); 53065089Seric if (p != NULL) 53165089Seric syslog(LOG_INFO, "%s: proto=%s", e->e_id, p); 53265089Seric syslog(LOG_INFO, "%s: relay=%s", e->e_id, name); 53365059Seric # endif 5347783Seric } 5357783Seric /* 5367783Seric ** PRIENCODE -- encode external priority names into internal values. 5377783Seric ** 5387783Seric ** Parameters: 5397783Seric ** p -- priority in ascii. 5407783Seric ** 5417783Seric ** Returns: 5427783Seric ** priority as a numeric level. 5437783Seric ** 5447783Seric ** Side Effects: 5457783Seric ** none. 5467783Seric */ 5477783Seric 5487783Seric priencode(p) 5497783Seric char *p; 5507783Seric { 5518253Seric register int i; 5527783Seric 5538253Seric for (i = 0; i < NumPriorities; i++) 5547783Seric { 55533725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 5568253Seric return (Priorities[i].pri_val); 5577783Seric } 5588253Seric 5598253Seric /* unknown priority */ 5608253Seric return (0); 5617783Seric } 5627783Seric /* 5637890Seric ** CRACKADDR -- parse an address and turn it into a macro 5647783Seric ** 5657783Seric ** This doesn't actually parse the address -- it just extracts 5667783Seric ** it and replaces it with "$g". The parse is totally ad hoc 5677783Seric ** and isn't even guaranteed to leave something syntactically 5687783Seric ** identical to what it started with. However, it does leave 5697783Seric ** something semantically identical. 5707783Seric ** 57151379Seric ** This algorithm has been cleaned up to handle a wider range 57251379Seric ** of cases -- notably quoted and backslash escaped strings. 57351379Seric ** This modification makes it substantially better at preserving 57451379Seric ** the original syntax. 5757783Seric ** 5767783Seric ** Parameters: 5777890Seric ** addr -- the address to be cracked. 5787783Seric ** 5797783Seric ** Returns: 5807783Seric ** a pointer to the new version. 5817783Seric ** 5827783Seric ** Side Effects: 5837890Seric ** none. 5847783Seric ** 5857783Seric ** Warning: 5867783Seric ** The return value is saved in local storage and should 5877783Seric ** be copied if it is to be reused. 5887783Seric */ 5897783Seric 5907783Seric char * 5917890Seric crackaddr(addr) 5927890Seric register char *addr; 5937783Seric { 5947783Seric register char *p; 59551379Seric register char c; 59651379Seric int cmtlev; 59756764Seric int realcmtlev; 59856764Seric int anglelev, realanglelev; 59951379Seric int copylev; 60051379Seric bool qmode; 60156764Seric bool realqmode; 60256764Seric bool skipping; 60351379Seric bool putgmac = FALSE; 60456735Seric bool quoteit = FALSE; 60564148Seric bool gotangle = FALSE; 60651379Seric register char *bp; 60756764Seric char *buflim; 6087783Seric static char buf[MAXNAME]; 6097783Seric 6107783Seric if (tTd(33, 1)) 6117890Seric printf("crackaddr(%s)\n", addr); 6127783Seric 6138082Seric /* strip leading spaces */ 61458050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 6158082Seric addr++; 6168082Seric 6177783Seric /* 61851379Seric ** Start by assuming we have no angle brackets. This will be 61951379Seric ** adjusted later if we find them. 6207783Seric */ 6217783Seric 62251379Seric bp = buf; 62356764Seric buflim = &buf[sizeof buf - 5]; 62451379Seric p = addr; 62556764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 62656764Seric qmode = realqmode = FALSE; 62751379Seric 62851379Seric while ((c = *p++) != '\0') 6297783Seric { 63056764Seric /* 63156764Seric ** If the buffer is overful, go into a special "skipping" 63256764Seric ** mode that tries to keep legal syntax but doesn't actually 63356764Seric ** output things. 63456764Seric */ 6357783Seric 63656764Seric skipping = bp >= buflim; 63756735Seric 63856764Seric if (copylev > 0 && !skipping) 63956764Seric *bp++ = c; 64056735Seric 64151379Seric /* check for backslash escapes */ 64251379Seric if (c == '\\') 6437783Seric { 64458890Seric /* arrange to quote the address */ 64558890Seric if (cmtlev <= 0 && !qmode) 64658890Seric quoteit = TRUE; 64758890Seric 64851379Seric if ((c = *p++) == '\0') 6497783Seric { 65051379Seric /* too far */ 65151379Seric p--; 65251379Seric goto putg; 6537783Seric } 65456764Seric if (copylev > 0 && !skipping) 65551379Seric *bp++ = c; 65651379Seric goto putg; 6577783Seric } 6587783Seric 65951379Seric /* check for quoted strings */ 66064967Seric if (c == '"' && cmtlev <= 0) 6617783Seric { 66251379Seric qmode = !qmode; 66356764Seric if (copylev > 0 && !skipping) 66456764Seric realqmode = !realqmode; 66551379Seric continue; 6667783Seric } 66751379Seric if (qmode) 66851379Seric goto putg; 6697783Seric 67051379Seric /* check for comments */ 67151379Seric if (c == '(') 6727783Seric { 67351379Seric cmtlev++; 67456764Seric 67556764Seric /* allow space for closing paren */ 67656764Seric if (!skipping) 67756764Seric { 67856764Seric buflim--; 67956764Seric realcmtlev++; 68056764Seric if (copylev++ <= 0) 68156764Seric { 68256764Seric *bp++ = ' '; 68356764Seric *bp++ = c; 68456764Seric } 68556764Seric } 68651379Seric } 68751379Seric if (cmtlev > 0) 68851379Seric { 68951379Seric if (c == ')') 6907783Seric { 69151379Seric cmtlev--; 69251379Seric copylev--; 69356764Seric if (!skipping) 69456764Seric { 69556764Seric realcmtlev--; 69656764Seric buflim++; 69756764Seric } 6987783Seric } 6997783Seric continue; 7007783Seric } 70156764Seric else if (c == ')') 70256764Seric { 70356764Seric /* syntax error: unmatched ) */ 70456764Seric if (!skipping) 70556764Seric bp--; 70656764Seric } 7077783Seric 70856764Seric /* check for characters that may have to be quoted */ 70964148Seric if (strchr(".'@,;:\\()[]", c) != NULL) 71056764Seric { 71156764Seric /* 71256764Seric ** If these occur as the phrase part of a <> 71356764Seric ** construct, but are not inside of () or already 71456764Seric ** quoted, they will have to be quoted. Note that 71556764Seric ** now (but don't actually do the quoting). 71656764Seric */ 71756764Seric 71856764Seric if (cmtlev <= 0 && !qmode) 71956764Seric quoteit = TRUE; 72056764Seric } 72156764Seric 72251379Seric /* check for angle brackets */ 72351379Seric if (c == '<') 72451379Seric { 72556735Seric register char *q; 72656735Seric 72764148Seric /* assume first of two angles is bogus */ 72864148Seric if (gotangle) 72964148Seric quoteit = TRUE; 73064148Seric gotangle = TRUE; 73164148Seric 73251379Seric /* oops -- have to change our mind */ 73364752Seric anglelev = 1; 73456764Seric if (!skipping) 73564752Seric realanglelev = 1; 73656764Seric 73756735Seric bp = buf; 73856735Seric if (quoteit) 73956735Seric { 74056735Seric *bp++ = '"'; 74156735Seric 74256735Seric /* back up over the '<' and any spaces */ 74356735Seric --p; 74458050Seric while (isascii(*--p) && isspace(*p)) 74556735Seric continue; 74656735Seric p++; 74756735Seric } 74856735Seric for (q = addr; q < p; ) 74956735Seric { 75056735Seric c = *q++; 75156764Seric if (bp < buflim) 75256764Seric { 75356764Seric if (quoteit && c == '"') 75456764Seric *bp++ = '\\'; 75556764Seric *bp++ = c; 75656764Seric } 75756735Seric } 75856735Seric if (quoteit) 75956735Seric { 76064148Seric if (bp == &buf[1]) 76164148Seric bp--; 76264148Seric else 76364148Seric *bp++ = '"'; 76456764Seric while ((c = *p++) != '<') 76556764Seric { 76656764Seric if (bp < buflim) 76756764Seric *bp++ = c; 76856764Seric } 76956764Seric *bp++ = c; 77056735Seric } 77151379Seric copylev = 0; 77256735Seric putgmac = quoteit = FALSE; 77351379Seric continue; 77451379Seric } 7757783Seric 77651379Seric if (c == '>') 7777783Seric { 77856764Seric if (anglelev > 0) 77956764Seric { 78056764Seric anglelev--; 78156764Seric if (!skipping) 78256764Seric { 78356764Seric realanglelev--; 78456764Seric buflim++; 78556764Seric } 78656764Seric } 78756764Seric else if (!skipping) 78856764Seric { 78956764Seric /* syntax error: unmatched > */ 79056764Seric if (copylev > 0) 79156764Seric bp--; 79264752Seric quoteit = TRUE; 79356764Seric continue; 79456764Seric } 79551379Seric if (copylev++ <= 0) 79651379Seric *bp++ = c; 79751379Seric continue; 7987783Seric } 79951379Seric 80051379Seric /* must be a real address character */ 80151379Seric putg: 80251379Seric if (copylev <= 0 && !putgmac) 80351379Seric { 80458050Seric *bp++ = MACROEXPAND; 80551379Seric *bp++ = 'g'; 80651379Seric putgmac = TRUE; 80751379Seric } 8087783Seric } 8097783Seric 81056764Seric /* repair any syntactic damage */ 81156764Seric if (realqmode) 81256764Seric *bp++ = '"'; 81356764Seric while (realcmtlev-- > 0) 81456764Seric *bp++ = ')'; 81556764Seric while (realanglelev-- > 0) 81656764Seric *bp++ = '>'; 81751379Seric *bp++ = '\0'; 8187783Seric 8197783Seric if (tTd(33, 1)) 8207944Seric printf("crackaddr=>`%s'\n", buf); 8217783Seric 8227783Seric return (buf); 8237783Seric } 8249382Seric /* 8259382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 8269382Seric ** 8279382Seric ** Parameters: 8289382Seric ** fp -- file to put it on. 8299382Seric ** m -- mailer to use. 8309382Seric ** e -- envelope to use. 8319382Seric ** 8329382Seric ** Returns: 8339382Seric ** none. 8349382Seric ** 8359382Seric ** Side Effects: 8369382Seric ** none. 8379382Seric */ 8389382Seric 83957589Seric /* 84057589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 84157589Seric */ 84257589Seric #ifndef MAX 84357589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 84457589Seric #endif 84557589Seric 84610176Seric putheader(fp, m, e) 8479382Seric register FILE *fp; 8489382Seric register MAILER *m; 8499382Seric register ENVELOPE *e; 8509382Seric { 85157135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 8529382Seric register HDR *h; 85357135Seric char obuf[MAXLINE]; 8549382Seric 85559882Seric if (tTd(34, 1)) 85659882Seric printf("--- putheader, mailer = %s ---\n", m->m_name); 85759882Seric 8589382Seric for (h = e->e_header; h != NULL; h = h->h_link) 8599382Seric { 8609382Seric register char *p; 86110689Seric extern bool bitintersect(); 8629382Seric 86359882Seric if (tTd(34, 11)) 86459882Seric { 86559882Seric printf(" %s: ", h->h_field); 86659882Seric xputs(h->h_value); 86759882Seric } 86859882Seric 8699382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 87010689Seric !bitintersect(h->h_mflags, m->m_flags)) 87159882Seric { 87259882Seric if (tTd(34, 11)) 87359882Seric printf(" (skipped)\n"); 8749382Seric continue; 87559882Seric } 8769382Seric 87711414Seric /* handle Resent-... headers specially */ 87811414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 87959882Seric { 88059882Seric if (tTd(34, 11)) 88159882Seric printf(" (skipped (resent))\n"); 88211414Seric continue; 88359882Seric } 88411414Seric 885*65152Seric /* macro expand value if generated internally */ 8869382Seric p = h->h_value; 8879382Seric if (bitset(H_DEFAULT, h->h_flags)) 8889382Seric { 8899382Seric expand(p, buf, &buf[sizeof buf], e); 8909382Seric p = buf; 8919382Seric if (p == NULL || *p == '\0') 892*65152Seric { 893*65152Seric if (tTd(34, 11)) 894*65152Seric printf(" (skipped -- null value)\n"); 8959382Seric continue; 896*65152Seric } 8979382Seric } 8989382Seric 899*65152Seric if (tTd(34, 11)) 900*65152Seric printf("\n"); 901*65152Seric 9029382Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 9039382Seric { 9049382Seric /* address field */ 9059382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 9069382Seric 9079382Seric if (bitset(H_FROM, h->h_flags)) 9089382Seric oldstyle = FALSE; 90955012Seric commaize(h, p, fp, oldstyle, m, e); 9109382Seric } 9119382Seric else 9129382Seric { 9139382Seric /* vanilla header line */ 91412159Seric register char *nlp; 91512159Seric 91659579Seric (void) sprintf(obuf, "%s: ", h->h_field); 91756795Seric while ((nlp = strchr(p, '\n')) != NULL) 91812159Seric { 91912159Seric *nlp = '\0'; 92012159Seric (void) strcat(obuf, p); 92112159Seric *nlp = '\n'; 92212159Seric putline(obuf, fp, m); 92312159Seric p = ++nlp; 92412161Seric obuf[0] = '\0'; 92512159Seric } 92612159Seric (void) strcat(obuf, p); 92710176Seric putline(obuf, fp, m); 9289382Seric } 9299382Seric } 9309382Seric } 9319382Seric /* 9329382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 9339382Seric ** 9349382Seric ** Parameters: 9359382Seric ** h -- the header field to output. 9369382Seric ** p -- the value to put in it. 9379382Seric ** fp -- file to put it to. 9389382Seric ** oldstyle -- TRUE if this is an old style header. 9399382Seric ** m -- a pointer to the mailer descriptor. If NULL, 9409382Seric ** don't transform the name at all. 94155012Seric ** e -- the envelope containing the message. 9429382Seric ** 9439382Seric ** Returns: 9449382Seric ** none. 9459382Seric ** 9469382Seric ** Side Effects: 9479382Seric ** outputs "p" to file "fp". 9489382Seric */ 9499382Seric 95055012Seric commaize(h, p, fp, oldstyle, m, e) 9519382Seric register HDR *h; 9529382Seric register char *p; 9539382Seric FILE *fp; 9549382Seric bool oldstyle; 9559382Seric register MAILER *m; 95655012Seric register ENVELOPE *e; 9579382Seric { 9589382Seric register char *obp; 9599382Seric int opos; 9609382Seric bool firstone = TRUE; 96111157Seric char obuf[MAXLINE + 3]; 9629382Seric 9639382Seric /* 9649382Seric ** Output the address list translated by the 9659382Seric ** mailer and with commas. 9669382Seric */ 9679382Seric 9689382Seric if (tTd(14, 2)) 9699382Seric printf("commaize(%s: %s)\n", h->h_field, p); 9709382Seric 9719382Seric obp = obuf; 97259579Seric (void) sprintf(obp, "%s: ", h->h_field); 9739382Seric opos = strlen(h->h_field) + 2; 9749382Seric obp += opos; 9759382Seric 9769382Seric /* 9779382Seric ** Run through the list of values. 9789382Seric */ 9799382Seric 9809382Seric while (*p != '\0') 9819382Seric { 9829382Seric register char *name; 98354983Seric register int c; 9849382Seric char savechar; 98559163Seric int flags; 98659163Seric auto int stat; 9879382Seric 9889382Seric /* 9899382Seric ** Find the end of the name. New style names 9909382Seric ** end with a comma, old style names end with 9919382Seric ** a space character. However, spaces do not 9929382Seric ** necessarily delimit an old-style name -- at 9939382Seric ** signs mean keep going. 9949382Seric */ 9959382Seric 9969382Seric /* find end of name */ 99758050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 9989382Seric p++; 9999382Seric name = p; 10009382Seric for (;;) 10019382Seric { 100258333Seric auto char *oldp; 100316909Seric char pvpbuf[PSBUFSIZE]; 10049382Seric 100565066Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, 100665066Seric sizeof pvpbuf, &oldp); 100758333Seric p = oldp; 10089382Seric 10099382Seric /* look to see if we have an at sign */ 101058050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 10119382Seric p++; 10129382Seric 101358170Seric if (*p != '@') 10149382Seric { 10159382Seric p = oldp; 10169382Seric break; 10179382Seric } 10189382Seric p += *p == '@' ? 1 : 2; 101958050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 10209382Seric p++; 10219382Seric } 10229382Seric /* at the end of one complete name */ 10239382Seric 10249382Seric /* strip off trailing white space */ 102558050Seric while (p >= name && 102658050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 10279382Seric p--; 10289382Seric if (++p == name) 10299382Seric continue; 10309382Seric savechar = *p; 10319382Seric *p = '\0'; 10329382Seric 10339382Seric /* translate the name to be relative */ 103459163Seric flags = RF_HEADERADDR|RF_ADDDOMAIN; 103559163Seric if (bitset(H_FROM, h->h_flags)) 103659163Seric flags |= RF_SENDERADDR; 103759163Seric stat = EX_OK; 103859163Seric name = remotename(name, m, flags, &stat, e); 10399382Seric if (*name == '\0') 10409382Seric { 10419382Seric *p = savechar; 10429382Seric continue; 10439382Seric } 10449382Seric 10459382Seric /* output the name with nice formatting */ 104654983Seric opos += strlen(name); 10479382Seric if (!firstone) 10489382Seric opos += 2; 10499382Seric if (opos > 78 && !firstone) 10509382Seric { 105110178Seric (void) strcpy(obp, ",\n"); 105210176Seric putline(obuf, fp, m); 10539382Seric obp = obuf; 10549382Seric (void) sprintf(obp, " "); 105510161Seric opos = strlen(obp); 105610161Seric obp += opos; 105754983Seric opos += strlen(name); 10589382Seric } 10599382Seric else if (!firstone) 10609382Seric { 10619382Seric (void) sprintf(obp, ", "); 10629382Seric obp += 2; 10639382Seric } 10649382Seric 106554983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 106654983Seric *obp++ = c; 10679382Seric firstone = FALSE; 10689382Seric *p = savechar; 10699382Seric } 10709382Seric (void) strcpy(obp, "\n"); 107110176Seric putline(obuf, fp, m); 10729382Seric } 10739382Seric /* 107458170Seric ** COPYHEADER -- copy header list 10759382Seric ** 107658170Seric ** This routine is the equivalent of newstr for header lists 107758170Seric ** 10789382Seric ** Parameters: 107958170Seric ** header -- list of header structures to copy. 10809382Seric ** 10819382Seric ** Returns: 108258170Seric ** a copy of 'header'. 10839382Seric ** 10849382Seric ** Side Effects: 10859382Seric ** none. 10869382Seric */ 10879382Seric 108858170Seric HDR * 108958170Seric copyheader(header) 109058170Seric register HDR *header; 10919382Seric { 109258170Seric register HDR *newhdr; 109358170Seric HDR *ret; 109458170Seric register HDR **tail = &ret; 10959382Seric 109658170Seric while (header != NULL) 109758170Seric { 109858170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 109958170Seric STRUCTCOPY(*header, *newhdr); 110058170Seric *tail = newhdr; 110158170Seric tail = &newhdr->h_link; 110258170Seric header = header->h_link; 110358170Seric } 110458170Seric *tail = NULL; 110558170Seric 110658170Seric return ret; 11079382Seric } 1108