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*67889Seric static char sccsid[] = "@(#)headers.c 8.39 (Berkeley) 11/06/94"; 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 10367694Seric printf("header match, hi_flags=%x\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; 14066123Seric bool oldSuprErrs = SuprErrs; 14164351Seric extern char *crackaddr(); 14264351Seric extern char *udbsender(); 14364351Seric 14464653Seric /* 14564653Seric ** Try doing USERDB rewriting even on fully commented 14664653Seric ** names; this saves the "comment" information (such 14764653Seric ** as full name) and rewrites the electronic part. 14866106Seric ** 14966106Seric ** XXX This code doesn't belong here -- parsing should 15066106Seric ** XXX not be done during collect() phase because 15166106Seric ** XXX error messages can confuse the SMTP phase. 15266123Seric ** XXX Setting SuprErrs is a crude hack around this 15366106Seric ** XXX problem. 15464653Seric */ 15564653Seric 15666106Seric if (OpMode == MD_SMTP || OpMode == MD_ARPAFTP) 15766123Seric SuprErrs = TRUE; 15864351Seric fancy = crackaddr(fvalue); 15964351Seric if (parseaddr(fvalue, &a, RF_COPYNONE, '\0', NULL, e) != NULL && 16067472Seric bitnset(M_CHECKUDB, a.q_mailer->m_flags) && 16164351Seric (p = udbsender(a.q_user)) != NULL) 16264351Seric { 16364351Seric char *oldg = macvalue('g', e); 16464351Seric 16564351Seric define('g', p, e); 16664351Seric expand(fancy, buf, &buf[sizeof buf], e); 16764351Seric define('g', oldg, e); 16864351Seric fvalue = buf; 16964351Seric } 17066123Seric SuprErrs = oldSuprErrs; 17164351Seric } 17264351Seric #endif 17364351Seric #endif 17411414Seric } 17511414Seric 17614784Seric /* delete default value for this header */ 17755012Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 17814784Seric { 17959579Seric if (strcasecmp(fname, h->h_field) == 0 && 18014784Seric bitset(H_DEFAULT, h->h_flags) && 18114784Seric !bitset(H_FORCE, h->h_flags)) 18214784Seric h->h_value = NULL; 18314784Seric } 18414784Seric 18513012Seric /* create a new node */ 18613012Seric h = (HDR *) xalloc(sizeof *h); 18713012Seric h->h_field = newstr(fname); 18864134Seric h->h_value = newstr(fvalue); 18913012Seric h->h_link = NULL; 19023117Seric bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts); 19113012Seric *hp = h; 1928066Seric h->h_flags = hi->hi_flags; 1934091Seric if (def) 1944091Seric h->h_flags |= H_DEFAULT; 1959059Seric if (cond) 1969059Seric h->h_flags |= H_CHECK; 1974091Seric 1985937Seric /* hack to see if this is a new format message */ 1998095Seric if (!def && bitset(H_RCPT|H_FROM, h->h_flags) && 20056795Seric (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL || 20156795Seric strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL)) 2028089Seric { 20355012Seric e->e_flags &= ~EF_OLDSTYLE; 2048089Seric } 2055937Seric 2064091Seric return (h->h_flags); 2074091Seric } 2084091Seric /* 2096980Seric ** ADDHEADER -- add a header entry to the end of the queue. 2106980Seric ** 2116980Seric ** This bypasses the special checking of chompheader. 2126980Seric ** 2136980Seric ** Parameters: 21459579Seric ** field -- the name of the header field. 21558789Seric ** value -- the value of the field. 21667546Seric ** hp -- an indirect pointer to the header structure list. 2176980Seric ** 2186980Seric ** Returns: 2196980Seric ** none. 2206980Seric ** 2216980Seric ** Side Effects: 2226980Seric ** adds the field on the list of headers for this envelope. 2236980Seric */ 2246980Seric 22567546Seric addheader(field, value, hdrlist) 2266980Seric char *field; 2276980Seric char *value; 22867546Seric HDR **hdrlist; 2296980Seric { 2306980Seric register HDR *h; 2316980Seric register struct hdrinfo *hi; 2326980Seric HDR **hp; 2336980Seric 2346980Seric /* find info struct */ 2356980Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 2366980Seric { 23759579Seric if (strcasecmp(field, hi->hi_field) == 0) 2386980Seric break; 2396980Seric } 2406980Seric 2416980Seric /* find current place in list -- keep back pointer? */ 24267546Seric for (hp = hdrlist; (h = *hp) != NULL; hp = &h->h_link) 2436980Seric { 24459579Seric if (strcasecmp(field, h->h_field) == 0) 2456980Seric break; 2466980Seric } 2476980Seric 2486980Seric /* allocate space for new header */ 2496980Seric h = (HDR *) xalloc(sizeof *h); 2506980Seric h->h_field = field; 2516980Seric h->h_value = newstr(value); 2527368Seric h->h_link = *hp; 2536980Seric h->h_flags = hi->hi_flags | H_DEFAULT; 25410689Seric clrbitmap(h->h_mflags); 2556980Seric *hp = h; 2566980Seric } 2576980Seric /* 2584091Seric ** HVALUE -- return value of a header. 2594091Seric ** 2604091Seric ** Only "real" fields (i.e., ones that have not been supplied 2614091Seric ** as a default) are used. 2624091Seric ** 2634091Seric ** Parameters: 2644091Seric ** field -- the field name. 26567546Seric ** header -- the header list. 2664091Seric ** 2674091Seric ** Returns: 2684091Seric ** pointer to the value part. 2694091Seric ** NULL if not found. 2704091Seric ** 2714091Seric ** Side Effects: 2729382Seric ** none. 2734091Seric */ 2744091Seric 2754091Seric char * 27667546Seric hvalue(field, header) 2774091Seric char *field; 27867546Seric HDR *header; 2794091Seric { 2804091Seric register HDR *h; 2814091Seric 28267546Seric for (h = header; h != NULL; h = h->h_link) 2834091Seric { 28459579Seric if (!bitset(H_DEFAULT, h->h_flags) && 28559579Seric strcasecmp(h->h_field, field) == 0) 2864091Seric return (h->h_value); 2874091Seric } 2884091Seric return (NULL); 2894091Seric } 2904091Seric /* 2914091Seric ** ISHEADER -- predicate telling if argument is a header. 2924091Seric ** 2934319Seric ** A line is a header if it has a single word followed by 2944319Seric ** optional white space followed by a colon. 2954319Seric ** 2964091Seric ** Parameters: 2974091Seric ** s -- string to check for possible headerness. 2984091Seric ** 2994091Seric ** Returns: 3004091Seric ** TRUE if s is a header. 3014091Seric ** FALSE otherwise. 3024091Seric ** 3034091Seric ** Side Effects: 3044091Seric ** none. 3054091Seric */ 3064091Seric 3074091Seric bool 3084091Seric isheader(s) 3094091Seric register char *s; 3104091Seric { 3119382Seric while (*s > ' ' && *s != ':' && *s != '\0') 3124091Seric s++; 3139382Seric 3149382Seric /* following technically violates RFC822 */ 31558050Seric while (isascii(*s) && isspace(*s)) 3164091Seric s++; 3179382Seric 3184091Seric return (*s == ':'); 3194091Seric } 3205919Seric /* 3217783Seric ** EATHEADER -- run through the stored header and extract info. 3227783Seric ** 3237783Seric ** Parameters: 3249382Seric ** e -- the envelope to process. 32558929Seric ** full -- if set, do full processing (e.g., compute 32658929Seric ** message priority). 3277783Seric ** 3287783Seric ** Returns: 3297783Seric ** none. 3307783Seric ** 3317783Seric ** Side Effects: 3327783Seric ** Sets a bunch of global variables from information 3339382Seric ** in the collected header. 3349382Seric ** Aborts the message if the hop count is exceeded. 3357783Seric */ 3367783Seric 33758929Seric eatheader(e, full) 3389382Seric register ENVELOPE *e; 33958929Seric bool full; 3407783Seric { 3417783Seric register HDR *h; 3427783Seric register char *p; 3439382Seric int hopcnt = 0; 34457208Seric char *msgid; 34558688Seric char buf[MAXLINE]; 3467783Seric 34758688Seric /* 34858688Seric ** Set up macros for possible expansion in headers. 34958688Seric */ 35058688Seric 35158704Seric define('f', e->e_sender, e); 35258704Seric define('g', e->e_sender, e); 35364148Seric if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0') 35464148Seric define('u', e->e_origrcpt, e); 35564148Seric else 35664148Seric define('u', NULL, e); 35758688Seric 35865052Seric /* full name of from person */ 35967546Seric p = hvalue("full-name", e->e_header); 36065052Seric if (p != NULL) 36165052Seric define('x', p, e); 36265052Seric 3639382Seric if (tTd(32, 1)) 3649382Seric printf("----- collected header -----\n"); 36557208Seric msgid = "<none>"; 3669382Seric for (h = e->e_header; h != NULL; h = h->h_link) 3677783Seric { 36864156Seric if (h->h_value == NULL) 36964156Seric { 37064156Seric if (tTd(32, 1)) 37164156Seric printf("%s: <NULL>\n", h->h_field); 37264156Seric continue; 37364156Seric } 37464156Seric 37558688Seric /* do early binding */ 37664156Seric if (bitset(H_DEFAULT, h->h_flags)) 37758688Seric { 37858688Seric expand(h->h_value, buf, &buf[sizeof buf], e); 37958688Seric if (buf[0] != '\0') 38058688Seric { 38158688Seric h->h_value = newstr(buf); 38258688Seric h->h_flags &= ~H_DEFAULT; 38358688Seric } 38458688Seric } 38558688Seric 3869382Seric if (tTd(32, 1)) 38765152Seric { 38865152Seric printf("%s: ", h->h_field); 38965152Seric xputs(h->h_value); 39065152Seric printf("\n"); 39165152Seric } 39257359Seric 39311414Seric /* count the number of times it has been processed */ 3949382Seric if (bitset(H_TRACE, h->h_flags)) 3959382Seric hopcnt++; 39611414Seric 39711414Seric /* send to this person if we so desire */ 39811414Seric if (GrabTo && bitset(H_RCPT, h->h_flags) && 39911414Seric !bitset(H_DEFAULT, h->h_flags) && 40055012Seric (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags))) 40111414Seric { 40263839Seric int saveflags = e->e_flags; 40363839Seric 40464283Seric (void) sendtolist(h->h_value, NULLADDR, 40558082Seric &e->e_sendqueue, e); 40663839Seric 40763839Seric /* delete fatal errors generated by this address */ 40864148Seric if (!GrabTo && !bitset(EF_FATALERRS, saveflags)) 40963839Seric e->e_flags &= ~EF_FATALERRS; 41011414Seric } 41111414Seric 41257208Seric /* save the message-id for logging */ 41364156Seric if (full && strcasecmp(h->h_field, "message-id") == 0) 41411290Seric { 41557208Seric msgid = h->h_value; 41659859Seric while (isascii(*msgid) && isspace(*msgid)) 41759859Seric msgid++; 41811290Seric } 41957359Seric 42057359Seric /* see if this is a return-receipt header */ 42157359Seric if (bitset(H_RECEIPTTO, h->h_flags)) 42257359Seric e->e_receiptto = h->h_value; 42357359Seric 42457359Seric /* see if this is an errors-to header */ 42561104Seric if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags)) 42664283Seric (void) sendtolist(h->h_value, NULLADDR, 42758082Seric &e->e_errorqueue, e); 4289382Seric } 4299382Seric if (tTd(32, 1)) 4307783Seric printf("----------------------------\n"); 4317783Seric 43258145Seric /* if we are just verifying (that is, sendmail -t -bv), drop out now */ 43358145Seric if (OpMode == MD_VERIFY) 43458145Seric return; 43558145Seric 4369382Seric /* store hop count */ 4379382Seric if (hopcnt > e->e_hopcount) 4389382Seric e->e_hopcount = hopcnt; 4399382Seric 4407783Seric /* message priority */ 44167546Seric p = hvalue("precedence", e->e_header); 4429382Seric if (p != NULL) 4439382Seric e->e_class = priencode(p); 44458929Seric if (full) 44567730Seric { 44625013Seric e->e_msgpriority = e->e_msgsize 44724981Seric - e->e_class * WkClassFact 44824981Seric + e->e_nrcpts * WkRecipFact; 44967730Seric if (e->e_class < 0) 45067730Seric e->e_timeoutclass = TOC_NONURGENT; 45167730Seric else if (e->e_class > 0) 45267730Seric e->e_timeoutclass = TOC_URGENT; 45367730Seric } 4547783Seric 45567730Seric /* message timeout priority */ 45667730Seric p = hvalue("priority", e->e_header); 45767730Seric if (full && p != NULL) 45867730Seric { 45967730Seric /* (this should be in the configuration file) */ 46067730Seric if (strcasecmp(p, "urgent")) 46167730Seric e->e_timeoutclass = TOC_URGENT; 46267730Seric else if (strcasecmp(p, "normal")) 46367730Seric e->e_timeoutclass = TOC_NORMAL; 46467730Seric else if (strcasecmp(p, "non-urgent")) 46567730Seric e->e_timeoutclass = TOC_NONURGENT; 46667730Seric } 46767730Seric 4687783Seric /* date message originated */ 46967546Seric p = hvalue("posted-date", e->e_header); 4707783Seric if (p == NULL) 47167546Seric p = hvalue("date", e->e_header); 4727783Seric if (p != NULL) 4739382Seric define('a', p, e); 47411290Seric 47511290Seric /* 47665983Seric ** From person in antiquated ARPANET mode 47765983Seric ** required by UK Grey Book e-mail gateways (sigh) 47865983Seric */ 47965983Seric 48065983Seric if (OpMode == MD_ARPAFTP) 48165983Seric { 48265983Seric register struct hdrinfo *hi; 48365983Seric 48465983Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 48565983Seric { 48665983Seric if (bitset(H_FROM, hi->hi_flags) && 48765983Seric (!bitset(H_RESENT, hi->hi_flags) || 48865983Seric bitset(EF_RESENT, e->e_flags)) && 48967546Seric (p = hvalue(hi->hi_field, e->e_header)) != NULL) 49065983Seric break; 49165983Seric } 49265983Seric if (hi->hi_field != NULL) 49365983Seric { 49465983Seric if (tTd(32, 2)) 49565983Seric printf("eatheader: setsender(*%s == %s)\n", 49665983Seric hi->hi_field, p); 49765983Seric setsender(p, e, NULL, TRUE); 49865983Seric } 49965983Seric } 50065983Seric 50165983Seric /* 50211290Seric ** Log collection information. 50311290Seric */ 50411290Seric 50511290Seric # ifdef LOG 50658929Seric if (full && LogLevel > 4) 50765089Seric logsender(e, msgid); 50865089Seric # endif /* LOG */ 50965089Seric e->e_flags &= ~EF_LOGSENDER; 51065089Seric } 51165089Seric /* 51265089Seric ** LOGSENDER -- log sender information 51365089Seric ** 51465089Seric ** Parameters: 51565089Seric ** e -- the envelope to log 51665089Seric ** msgid -- the message id 51765089Seric ** 51865089Seric ** Returns: 51965089Seric ** none 52065089Seric */ 52165089Seric 52265089Seric logsender(e, msgid) 52365089Seric register ENVELOPE *e; 52465089Seric char *msgid; 52565089Seric { 52666748Seric # ifdef LOG 52765089Seric char *name; 52865089Seric register char *sbp; 52965089Seric register char *p; 53065089Seric char hbuf[MAXNAME]; 53165089Seric char sbuf[MAXLINE]; 53265089Seric 53365089Seric if (bitset(EF_RESPONSE, e->e_flags)) 53465089Seric name = "[RESPONSE]"; 53565089Seric else if ((name = macvalue('_', e)) != NULL) 53665089Seric ; 53766003Seric else if (RealHostName == NULL) 53866003Seric name = "localhost"; 53965089Seric else if (RealHostName[0] == '[') 54065089Seric name = RealHostName; 54165089Seric else 54211290Seric { 54365089Seric name = hbuf; 54465089Seric (void) sprintf(hbuf, "%.80s", RealHostName); 54565089Seric if (RealHostAddr.sa.sa_family != 0) 54657359Seric { 54765089Seric p = &hbuf[strlen(hbuf)]; 54865089Seric (void) sprintf(p, " (%s)", 54965089Seric anynet_ntoa(&RealHostAddr)); 55057359Seric } 55165089Seric } 55257359Seric 55365089Seric /* some versions of syslog only take 5 printf args */ 55465059Seric # if (SYSLOG_BUFSIZE) >= 256 55565089Seric sbp = sbuf; 55665089Seric sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d", 55767788Seric e->e_from.q_paddr == NULL ? "<NONE>" : e->e_from.q_paddr, 55867788Seric e->e_msgsize, e->e_class, e->e_msgpriority, e->e_nrcpts); 55965089Seric sbp += strlen(sbp); 56065089Seric if (msgid != NULL) 56165089Seric { 56265089Seric sprintf(sbp, ", msgid=%.100s", msgid); 56360575Seric sbp += strlen(sbp); 56465089Seric } 56565089Seric if (e->e_bodytype != NULL) 56665089Seric { 56765089Seric (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype); 56865089Seric sbp += strlen(sbp); 56965089Seric } 57065089Seric p = macvalue('r', e); 57165089Seric if (p != NULL) 57265089Seric (void) sprintf(sbp, ", proto=%.20s", p); 57365089Seric syslog(LOG_INFO, "%s: %s, relay=%s", 57465089Seric e->e_id, sbuf, name); 57565059Seric 57665059Seric # else /* short syslog buffer */ 57765059Seric 57865089Seric syslog(LOG_INFO, "%s: from=%s", 57967788Seric e->e_id, e->e_from.q_paddr == NULL ? "<NONE>" : 58067788Seric shortenstring(e->e_from.q_paddr, 83)); 58165089Seric syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d", 58265089Seric e->e_id, e->e_msgsize, e->e_class, 58365089Seric e->e_msgpriority, e->e_nrcpts); 58465089Seric if (msgid != NULL) 58565059Seric syslog(LOG_INFO, "%s: msgid=%s", e->e_id, msgid); 58665650Seric sbp = sbuf; 58765650Seric sprintf(sbp, "%s:", e->e_id); 58865650Seric sbp += strlen(sbp); 58965089Seric if (e->e_bodytype != NULL) 59065650Seric { 59165650Seric sprintf(sbp, " bodytype=%s,", e->e_bodytype); 59265650Seric sbp += strlen(sbp); 59365650Seric } 59465089Seric p = macvalue('r', e); 59565089Seric if (p != NULL) 59665650Seric { 59765731Seric sprintf(sbp, " proto=%s,", p); 59865650Seric sbp += strlen(sbp); 59965650Seric } 60065650Seric syslog(LOG_INFO, "%s relay=%s", sbuf, name); 60165059Seric # endif 60266748Seric # endif 6037783Seric } 6047783Seric /* 6057783Seric ** PRIENCODE -- encode external priority names into internal values. 6067783Seric ** 6077783Seric ** Parameters: 6087783Seric ** p -- priority in ascii. 6097783Seric ** 6107783Seric ** Returns: 6117783Seric ** priority as a numeric level. 6127783Seric ** 6137783Seric ** Side Effects: 6147783Seric ** none. 6157783Seric */ 6167783Seric 6177783Seric priencode(p) 6187783Seric char *p; 6197783Seric { 6208253Seric register int i; 6217783Seric 6228253Seric for (i = 0; i < NumPriorities; i++) 6237783Seric { 62433725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 6258253Seric return (Priorities[i].pri_val); 6267783Seric } 6278253Seric 6288253Seric /* unknown priority */ 6298253Seric return (0); 6307783Seric } 6317783Seric /* 6327890Seric ** CRACKADDR -- parse an address and turn it into a macro 6337783Seric ** 6347783Seric ** This doesn't actually parse the address -- it just extracts 6357783Seric ** it and replaces it with "$g". The parse is totally ad hoc 6367783Seric ** and isn't even guaranteed to leave something syntactically 6377783Seric ** identical to what it started with. However, it does leave 6387783Seric ** something semantically identical. 6397783Seric ** 64051379Seric ** This algorithm has been cleaned up to handle a wider range 64151379Seric ** of cases -- notably quoted and backslash escaped strings. 64251379Seric ** This modification makes it substantially better at preserving 64351379Seric ** the original syntax. 6447783Seric ** 6457783Seric ** Parameters: 6467890Seric ** addr -- the address to be cracked. 6477783Seric ** 6487783Seric ** Returns: 6497783Seric ** a pointer to the new version. 6507783Seric ** 6517783Seric ** Side Effects: 6527890Seric ** none. 6537783Seric ** 6547783Seric ** Warning: 6557783Seric ** The return value is saved in local storage and should 6567783Seric ** be copied if it is to be reused. 6577783Seric */ 6587783Seric 6597783Seric char * 6607890Seric crackaddr(addr) 6617890Seric register char *addr; 6627783Seric { 6637783Seric register char *p; 66451379Seric register char c; 66551379Seric int cmtlev; 66656764Seric int realcmtlev; 66756764Seric int anglelev, realanglelev; 66851379Seric int copylev; 66951379Seric bool qmode; 67056764Seric bool realqmode; 67156764Seric bool skipping; 67251379Seric bool putgmac = FALSE; 67356735Seric bool quoteit = FALSE; 67464148Seric bool gotangle = FALSE; 67551379Seric register char *bp; 67656764Seric char *buflim; 6777783Seric static char buf[MAXNAME]; 6787783Seric 6797783Seric if (tTd(33, 1)) 6807890Seric printf("crackaddr(%s)\n", addr); 6817783Seric 6828082Seric /* strip leading spaces */ 68358050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 6848082Seric addr++; 6858082Seric 6867783Seric /* 68751379Seric ** Start by assuming we have no angle brackets. This will be 68851379Seric ** adjusted later if we find them. 6897783Seric */ 6907783Seric 69151379Seric bp = buf; 69256764Seric buflim = &buf[sizeof buf - 5]; 69351379Seric p = addr; 69456764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 69556764Seric qmode = realqmode = FALSE; 69651379Seric 69751379Seric while ((c = *p++) != '\0') 6987783Seric { 69956764Seric /* 70056764Seric ** If the buffer is overful, go into a special "skipping" 70156764Seric ** mode that tries to keep legal syntax but doesn't actually 70256764Seric ** output things. 70356764Seric */ 7047783Seric 70556764Seric skipping = bp >= buflim; 70656735Seric 70756764Seric if (copylev > 0 && !skipping) 70856764Seric *bp++ = c; 70956735Seric 71051379Seric /* check for backslash escapes */ 71151379Seric if (c == '\\') 7127783Seric { 71358890Seric /* arrange to quote the address */ 71458890Seric if (cmtlev <= 0 && !qmode) 71558890Seric quoteit = TRUE; 71658890Seric 71751379Seric if ((c = *p++) == '\0') 7187783Seric { 71951379Seric /* too far */ 72051379Seric p--; 72151379Seric goto putg; 7227783Seric } 72356764Seric if (copylev > 0 && !skipping) 72451379Seric *bp++ = c; 72551379Seric goto putg; 7267783Seric } 7277783Seric 72851379Seric /* check for quoted strings */ 72964967Seric if (c == '"' && cmtlev <= 0) 7307783Seric { 73151379Seric qmode = !qmode; 73256764Seric if (copylev > 0 && !skipping) 73356764Seric realqmode = !realqmode; 73451379Seric continue; 7357783Seric } 73651379Seric if (qmode) 73751379Seric goto putg; 7387783Seric 73951379Seric /* check for comments */ 74051379Seric if (c == '(') 7417783Seric { 74251379Seric cmtlev++; 74356764Seric 74456764Seric /* allow space for closing paren */ 74556764Seric if (!skipping) 74656764Seric { 74756764Seric buflim--; 74856764Seric realcmtlev++; 74956764Seric if (copylev++ <= 0) 75056764Seric { 75156764Seric *bp++ = ' '; 75256764Seric *bp++ = c; 75356764Seric } 75456764Seric } 75551379Seric } 75651379Seric if (cmtlev > 0) 75751379Seric { 75851379Seric if (c == ')') 7597783Seric { 76051379Seric cmtlev--; 76151379Seric copylev--; 76256764Seric if (!skipping) 76356764Seric { 76456764Seric realcmtlev--; 76556764Seric buflim++; 76656764Seric } 7677783Seric } 7687783Seric continue; 7697783Seric } 77056764Seric else if (c == ')') 77156764Seric { 77256764Seric /* syntax error: unmatched ) */ 77365544Seric if (copylev > 0 && !skipping) 77456764Seric bp--; 77556764Seric } 7767783Seric 77756764Seric /* check for characters that may have to be quoted */ 77864148Seric if (strchr(".'@,;:\\()[]", c) != NULL) 77956764Seric { 78056764Seric /* 78156764Seric ** If these occur as the phrase part of a <> 78256764Seric ** construct, but are not inside of () or already 78356764Seric ** quoted, they will have to be quoted. Note that 78456764Seric ** now (but don't actually do the quoting). 78556764Seric */ 78656764Seric 78756764Seric if (cmtlev <= 0 && !qmode) 78856764Seric quoteit = TRUE; 78956764Seric } 79056764Seric 79151379Seric /* check for angle brackets */ 79251379Seric if (c == '<') 79351379Seric { 79456735Seric register char *q; 79556735Seric 79664148Seric /* assume first of two angles is bogus */ 79764148Seric if (gotangle) 79864148Seric quoteit = TRUE; 79964148Seric gotangle = TRUE; 80064148Seric 80151379Seric /* oops -- have to change our mind */ 80264752Seric anglelev = 1; 80356764Seric if (!skipping) 80464752Seric realanglelev = 1; 80556764Seric 80656735Seric bp = buf; 80756735Seric if (quoteit) 80856735Seric { 80956735Seric *bp++ = '"'; 81056735Seric 81156735Seric /* back up over the '<' and any spaces */ 81256735Seric --p; 81358050Seric while (isascii(*--p) && isspace(*p)) 81456735Seric continue; 81556735Seric p++; 81656735Seric } 81756735Seric for (q = addr; q < p; ) 81856735Seric { 81956735Seric c = *q++; 82056764Seric if (bp < buflim) 82156764Seric { 82256764Seric if (quoteit && c == '"') 82356764Seric *bp++ = '\\'; 82456764Seric *bp++ = c; 82556764Seric } 82656735Seric } 82756735Seric if (quoteit) 82856735Seric { 82964148Seric if (bp == &buf[1]) 83064148Seric bp--; 83164148Seric else 83264148Seric *bp++ = '"'; 83356764Seric while ((c = *p++) != '<') 83456764Seric { 83556764Seric if (bp < buflim) 83656764Seric *bp++ = c; 83756764Seric } 83856764Seric *bp++ = c; 83956735Seric } 84051379Seric copylev = 0; 84156735Seric putgmac = quoteit = FALSE; 84251379Seric continue; 84351379Seric } 8447783Seric 84551379Seric if (c == '>') 8467783Seric { 84756764Seric if (anglelev > 0) 84856764Seric { 84956764Seric anglelev--; 85056764Seric if (!skipping) 85156764Seric { 85256764Seric realanglelev--; 85356764Seric buflim++; 85456764Seric } 85556764Seric } 85656764Seric else if (!skipping) 85756764Seric { 85856764Seric /* syntax error: unmatched > */ 85956764Seric if (copylev > 0) 86056764Seric bp--; 86164752Seric quoteit = TRUE; 86256764Seric continue; 86356764Seric } 86451379Seric if (copylev++ <= 0) 86551379Seric *bp++ = c; 86651379Seric continue; 8677783Seric } 86851379Seric 86951379Seric /* must be a real address character */ 87051379Seric putg: 87151379Seric if (copylev <= 0 && !putgmac) 87251379Seric { 87358050Seric *bp++ = MACROEXPAND; 87451379Seric *bp++ = 'g'; 87551379Seric putgmac = TRUE; 87651379Seric } 8777783Seric } 8787783Seric 87956764Seric /* repair any syntactic damage */ 88056764Seric if (realqmode) 88156764Seric *bp++ = '"'; 88256764Seric while (realcmtlev-- > 0) 88356764Seric *bp++ = ')'; 88456764Seric while (realanglelev-- > 0) 88556764Seric *bp++ = '>'; 88651379Seric *bp++ = '\0'; 8877783Seric 8887783Seric if (tTd(33, 1)) 8897944Seric printf("crackaddr=>`%s'\n", buf); 8907783Seric 8917783Seric return (buf); 8927783Seric } 8939382Seric /* 8949382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 8959382Seric ** 8969382Seric ** Parameters: 89765870Seric ** mci -- the connection information. 89867546Seric ** h -- the header to put. 8999382Seric ** e -- envelope to use. 9009382Seric ** 9019382Seric ** Returns: 9029382Seric ** none. 9039382Seric ** 9049382Seric ** Side Effects: 9059382Seric ** none. 9069382Seric */ 9079382Seric 90857589Seric /* 90957589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 91057589Seric */ 91157589Seric #ifndef MAX 91257589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 91357589Seric #endif 91457589Seric 91567546Seric putheader(mci, h, e) 91665870Seric register MCI *mci; 91767546Seric register HDR *h; 9189382Seric register ENVELOPE *e; 9199382Seric { 92057135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 92157135Seric char obuf[MAXLINE]; 9229382Seric 92359882Seric if (tTd(34, 1)) 92465870Seric printf("--- putheader, mailer = %s ---\n", 92565870Seric mci->mci_mailer->m_name); 92659882Seric 92767546Seric mci->mci_flags |= MCIF_INHEADER; 92867546Seric for (; h != NULL; h = h->h_link) 9299382Seric { 9309382Seric register char *p; 93110689Seric extern bool bitintersect(); 9329382Seric 93359882Seric if (tTd(34, 11)) 93459882Seric { 93559882Seric printf(" %s: ", h->h_field); 93659882Seric xputs(h->h_value); 93759882Seric } 93859882Seric 9399382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 94065870Seric !bitintersect(h->h_mflags, mci->mci_mailer->m_flags)) 94159882Seric { 94259882Seric if (tTd(34, 11)) 94359882Seric printf(" (skipped)\n"); 9449382Seric continue; 94559882Seric } 9469382Seric 94711414Seric /* handle Resent-... headers specially */ 94811414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 94959882Seric { 95059882Seric if (tTd(34, 11)) 95159882Seric printf(" (skipped (resent))\n"); 95211414Seric continue; 95359882Seric } 95411414Seric 95566784Seric /* suppress return receipts if requested */ 95666784Seric if (bitset(H_RECEIPTTO, h->h_flags) && 95766784Seric bitset(EF_NORECEIPT, e->e_flags)) 95866784Seric { 95966784Seric if (tTd(34, 11)) 96066784Seric printf(" (skipped (receipt))\n"); 96166784Seric continue; 96266784Seric } 96366784Seric 96467694Seric /* suppress Content-Transfer-Encoding: if we are MIMEing */ 96567694Seric if (bitset(H_CTE, h->h_flags) && 96667694Seric bitset(MCIF_CVT8TO7, mci->mci_flags)) 96767694Seric { 96867694Seric if (tTd(34, 11)) 96967694Seric printf(" (skipped (content-transfer-encoding))\n"); 97067694Seric continue; 97167694Seric } 97267694Seric 97365152Seric /* macro expand value if generated internally */ 9749382Seric p = h->h_value; 9759382Seric if (bitset(H_DEFAULT, h->h_flags)) 9769382Seric { 9779382Seric expand(p, buf, &buf[sizeof buf], e); 9789382Seric p = buf; 9799382Seric if (p == NULL || *p == '\0') 98065152Seric { 98165152Seric if (tTd(34, 11)) 98265152Seric printf(" (skipped -- null value)\n"); 9839382Seric continue; 98465152Seric } 9859382Seric } 9869382Seric 98765152Seric if (tTd(34, 11)) 98865152Seric printf("\n"); 98965152Seric 9909382Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 9919382Seric { 9929382Seric /* address field */ 9939382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 9949382Seric 9959382Seric if (bitset(H_FROM, h->h_flags)) 9969382Seric oldstyle = FALSE; 99765870Seric commaize(h, p, oldstyle, mci, e); 9989382Seric } 9999382Seric else 10009382Seric { 10019382Seric /* vanilla header line */ 100212159Seric register char *nlp; 100312159Seric 100459579Seric (void) sprintf(obuf, "%s: ", h->h_field); 100556795Seric while ((nlp = strchr(p, '\n')) != NULL) 100612159Seric { 100712159Seric *nlp = '\0'; 100812159Seric (void) strcat(obuf, p); 100912159Seric *nlp = '\n'; 101065870Seric putline(obuf, mci); 101112159Seric p = ++nlp; 101212161Seric obuf[0] = '\0'; 101312159Seric } 101412159Seric (void) strcat(obuf, p); 101565870Seric putline(obuf, mci); 10169382Seric } 10179382Seric } 101867887Seric 101967887Seric /* 102067887Seric ** If we are converting this to a MIME message, add the 1021*67889Seric ** MIME headers. 102267887Seric */ 102367887Seric 102467887Seric if (bitset(MM_MIME8BIT, MimeMode) && 102567887Seric bitset(EF_HAS8BIT, e->e_flags) && 102667887Seric !bitnset(M_8BITS, mci->mci_mailer->m_flags) && 1027*67889Seric !bitset(MCIF_CVT8TO7, mci->mci_flags)) 102867887Seric { 1029*67889Seric if (hvalue("MIME-Version", e->e_header) == NULL) 1030*67889Seric putline("MIME-Version: 1.0", mci); 1031*67889Seric if (hvalue("Content-Type", e->e_header) == NULL) 1032*67889Seric { 1033*67889Seric sprintf(obuf, "Content-Type: text/plain; charset=%s", 1034*67889Seric DefaultCharSet != NULL ? DefaultCharSet 1035*67889Seric : "unknown-8bit"); 1036*67889Seric putline(obuf, mci); 1037*67889Seric } 1038*67889Seric if (hvalue("Content-Transfer-Encoding", e->e_header) == NULL) 1039*67889Seric putline("Content-Transfer-Encoding: 8bit", mci); 104067887Seric } 10419382Seric } 10429382Seric /* 10439382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 10449382Seric ** 10459382Seric ** Parameters: 10469382Seric ** h -- the header field to output. 10479382Seric ** p -- the value to put in it. 10489382Seric ** oldstyle -- TRUE if this is an old style header. 104965870Seric ** mci -- the connection information. 105055012Seric ** e -- the envelope containing the message. 10519382Seric ** 10529382Seric ** Returns: 10539382Seric ** none. 10549382Seric ** 10559382Seric ** Side Effects: 10569382Seric ** outputs "p" to file "fp". 10579382Seric */ 10589382Seric 105965870Seric void 106065870Seric commaize(h, p, oldstyle, mci, e) 10619382Seric register HDR *h; 10629382Seric register char *p; 10639382Seric bool oldstyle; 106465870Seric register MCI *mci; 106555012Seric register ENVELOPE *e; 10669382Seric { 10679382Seric register char *obp; 10689382Seric int opos; 106966004Seric int omax; 10709382Seric bool firstone = TRUE; 107111157Seric char obuf[MAXLINE + 3]; 10729382Seric 10739382Seric /* 10749382Seric ** Output the address list translated by the 10759382Seric ** mailer and with commas. 10769382Seric */ 10779382Seric 10789382Seric if (tTd(14, 2)) 10799382Seric printf("commaize(%s: %s)\n", h->h_field, p); 10809382Seric 10819382Seric obp = obuf; 108259579Seric (void) sprintf(obp, "%s: ", h->h_field); 10839382Seric opos = strlen(h->h_field) + 2; 10849382Seric obp += opos; 108566254Seric omax = mci->mci_mailer->m_linelimit - 2; 108666254Seric if (omax < 0 || omax > 78) 108766254Seric omax = 78; 10889382Seric 10899382Seric /* 10909382Seric ** Run through the list of values. 10919382Seric */ 10929382Seric 10939382Seric while (*p != '\0') 10949382Seric { 10959382Seric register char *name; 109654983Seric register int c; 10979382Seric char savechar; 109859163Seric int flags; 109959163Seric auto int stat; 11009382Seric 11019382Seric /* 11029382Seric ** Find the end of the name. New style names 11039382Seric ** end with a comma, old style names end with 11049382Seric ** a space character. However, spaces do not 11059382Seric ** necessarily delimit an old-style name -- at 11069382Seric ** signs mean keep going. 11079382Seric */ 11089382Seric 11099382Seric /* find end of name */ 111058050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 11119382Seric p++; 11129382Seric name = p; 11139382Seric for (;;) 11149382Seric { 111558333Seric auto char *oldp; 111616909Seric char pvpbuf[PSBUFSIZE]; 11179382Seric 111865066Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, 111965066Seric sizeof pvpbuf, &oldp); 112058333Seric p = oldp; 11219382Seric 11229382Seric /* look to see if we have an at sign */ 112358050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 11249382Seric p++; 11259382Seric 112658170Seric if (*p != '@') 11279382Seric { 11289382Seric p = oldp; 11299382Seric break; 11309382Seric } 11319382Seric p += *p == '@' ? 1 : 2; 113258050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 11339382Seric p++; 11349382Seric } 11359382Seric /* at the end of one complete name */ 11369382Seric 11379382Seric /* strip off trailing white space */ 113858050Seric while (p >= name && 113958050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 11409382Seric p--; 11419382Seric if (++p == name) 11429382Seric continue; 11439382Seric savechar = *p; 11449382Seric *p = '\0'; 11459382Seric 11469382Seric /* translate the name to be relative */ 114759163Seric flags = RF_HEADERADDR|RF_ADDDOMAIN; 114859163Seric if (bitset(H_FROM, h->h_flags)) 114959163Seric flags |= RF_SENDERADDR; 115059163Seric stat = EX_OK; 115165870Seric name = remotename(name, mci->mci_mailer, flags, &stat, e); 11529382Seric if (*name == '\0') 11539382Seric { 11549382Seric *p = savechar; 11559382Seric continue; 11569382Seric } 11579382Seric 11589382Seric /* output the name with nice formatting */ 115954983Seric opos += strlen(name); 11609382Seric if (!firstone) 11619382Seric opos += 2; 116266004Seric if (opos > omax && !firstone) 11639382Seric { 116410178Seric (void) strcpy(obp, ",\n"); 116565870Seric putline(obuf, mci); 11669382Seric obp = obuf; 116766255Seric (void) strcpy(obp, " "); 116810161Seric opos = strlen(obp); 116910161Seric obp += opos; 117054983Seric opos += strlen(name); 11719382Seric } 11729382Seric else if (!firstone) 11739382Seric { 117466255Seric (void) strcpy(obp, ", "); 11759382Seric obp += 2; 11769382Seric } 11779382Seric 117854983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 117954983Seric *obp++ = c; 11809382Seric firstone = FALSE; 11819382Seric *p = savechar; 11829382Seric } 11839382Seric (void) strcpy(obp, "\n"); 118465870Seric putline(obuf, mci); 11859382Seric } 11869382Seric /* 118758170Seric ** COPYHEADER -- copy header list 11889382Seric ** 118958170Seric ** This routine is the equivalent of newstr for header lists 119058170Seric ** 11919382Seric ** Parameters: 119258170Seric ** header -- list of header structures to copy. 11939382Seric ** 11949382Seric ** Returns: 119558170Seric ** a copy of 'header'. 11969382Seric ** 11979382Seric ** Side Effects: 11989382Seric ** none. 11999382Seric */ 12009382Seric 120158170Seric HDR * 120258170Seric copyheader(header) 120358170Seric register HDR *header; 12049382Seric { 120558170Seric register HDR *newhdr; 120658170Seric HDR *ret; 120758170Seric register HDR **tail = &ret; 12089382Seric 120958170Seric while (header != NULL) 121058170Seric { 121158170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 121258170Seric STRUCTCOPY(*header, *newhdr); 121358170Seric *tail = newhdr; 121458170Seric tail = &newhdr->h_link; 121558170Seric header = header->h_link; 121658170Seric } 121758170Seric *tail = NULL; 121858170Seric 121958170Seric return ret; 12209382Seric } 1221