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*68522Seric static char sccsid[] = "@(#)headers.c 8.48 (Berkeley) 03/12/95"; 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, 40567982Seric &e->e_sendqueue, 0, 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, 42767982Seric &e->e_errorqueue, 0, 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 50668036Seric if (bitset(EF_LOGSENDER, e->e_flags) && 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; 53067901Seric int l; 53165089Seric char hbuf[MAXNAME]; 53265089Seric char sbuf[MAXLINE]; 53367901Seric char mbuf[MAXNAME]; 53465089Seric 53567901Seric /* don't allow newlines in the message-id */ 53667901Seric if (msgid != NULL) 53767901Seric { 53867901Seric l = strlen(msgid); 53967901Seric if (l > sizeof mbuf - 1) 54067901Seric l = sizeof mbuf - 1; 54167901Seric bcopy(msgid, mbuf, l); 54267901Seric mbuf[l] = '\0'; 54367901Seric p = mbuf; 54467901Seric while ((p = strchr(p, '\n')) != NULL) 54567901Seric *p++ = ' '; 54667901Seric } 54767901Seric 54865089Seric if (bitset(EF_RESPONSE, e->e_flags)) 54965089Seric name = "[RESPONSE]"; 55065089Seric else if ((name = macvalue('_', e)) != NULL) 55165089Seric ; 55266003Seric else if (RealHostName == NULL) 55366003Seric name = "localhost"; 55465089Seric else if (RealHostName[0] == '[') 55565089Seric name = RealHostName; 55665089Seric else 55711290Seric { 55865089Seric name = hbuf; 55965089Seric (void) sprintf(hbuf, "%.80s", RealHostName); 56065089Seric if (RealHostAddr.sa.sa_family != 0) 56157359Seric { 56265089Seric p = &hbuf[strlen(hbuf)]; 56365089Seric (void) sprintf(p, " (%s)", 56465089Seric anynet_ntoa(&RealHostAddr)); 56557359Seric } 56665089Seric } 56757359Seric 56865089Seric /* some versions of syslog only take 5 printf args */ 56965059Seric # if (SYSLOG_BUFSIZE) >= 256 57065089Seric sbp = sbuf; 57165089Seric sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d", 57267788Seric e->e_from.q_paddr == NULL ? "<NONE>" : e->e_from.q_paddr, 57367788Seric e->e_msgsize, e->e_class, e->e_msgpriority, e->e_nrcpts); 57465089Seric sbp += strlen(sbp); 57565089Seric if (msgid != NULL) 57665089Seric { 57767901Seric sprintf(sbp, ", msgid=%.100s", mbuf); 57860575Seric sbp += strlen(sbp); 57965089Seric } 58065089Seric if (e->e_bodytype != NULL) 58165089Seric { 58265089Seric (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype); 58365089Seric sbp += strlen(sbp); 58465089Seric } 58565089Seric p = macvalue('r', e); 58665089Seric if (p != NULL) 58765089Seric (void) sprintf(sbp, ", proto=%.20s", p); 58865089Seric syslog(LOG_INFO, "%s: %s, relay=%s", 58965089Seric e->e_id, sbuf, name); 59065059Seric 59165059Seric # else /* short syslog buffer */ 59265059Seric 59365089Seric syslog(LOG_INFO, "%s: from=%s", 59467788Seric e->e_id, e->e_from.q_paddr == NULL ? "<NONE>" : 59567788Seric shortenstring(e->e_from.q_paddr, 83)); 59665089Seric syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d", 59765089Seric e->e_id, e->e_msgsize, e->e_class, 59865089Seric e->e_msgpriority, e->e_nrcpts); 59965089Seric if (msgid != NULL) 60067901Seric syslog(LOG_INFO, "%s: msgid=%s", e->e_id, mbuf); 60165650Seric sbp = sbuf; 60265650Seric sprintf(sbp, "%s:", e->e_id); 60365650Seric sbp += strlen(sbp); 60465089Seric if (e->e_bodytype != NULL) 60565650Seric { 60665650Seric sprintf(sbp, " bodytype=%s,", e->e_bodytype); 60765650Seric sbp += strlen(sbp); 60865650Seric } 60965089Seric p = macvalue('r', e); 61065089Seric if (p != NULL) 61165650Seric { 61265731Seric sprintf(sbp, " proto=%s,", p); 61365650Seric sbp += strlen(sbp); 61465650Seric } 61565650Seric syslog(LOG_INFO, "%s relay=%s", sbuf, name); 61665059Seric # endif 61766748Seric # endif 6187783Seric } 6197783Seric /* 6207783Seric ** PRIENCODE -- encode external priority names into internal values. 6217783Seric ** 6227783Seric ** Parameters: 6237783Seric ** p -- priority in ascii. 6247783Seric ** 6257783Seric ** Returns: 6267783Seric ** priority as a numeric level. 6277783Seric ** 6287783Seric ** Side Effects: 6297783Seric ** none. 6307783Seric */ 6317783Seric 6327783Seric priencode(p) 6337783Seric char *p; 6347783Seric { 6358253Seric register int i; 6367783Seric 6378253Seric for (i = 0; i < NumPriorities; i++) 6387783Seric { 63933725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 6408253Seric return (Priorities[i].pri_val); 6417783Seric } 6428253Seric 6438253Seric /* unknown priority */ 6448253Seric return (0); 6457783Seric } 6467783Seric /* 6477890Seric ** CRACKADDR -- parse an address and turn it into a macro 6487783Seric ** 6497783Seric ** This doesn't actually parse the address -- it just extracts 6507783Seric ** it and replaces it with "$g". The parse is totally ad hoc 6517783Seric ** and isn't even guaranteed to leave something syntactically 6527783Seric ** identical to what it started with. However, it does leave 6537783Seric ** something semantically identical. 6547783Seric ** 65551379Seric ** This algorithm has been cleaned up to handle a wider range 65651379Seric ** of cases -- notably quoted and backslash escaped strings. 65751379Seric ** This modification makes it substantially better at preserving 65851379Seric ** the original syntax. 6597783Seric ** 6607783Seric ** Parameters: 6617890Seric ** addr -- the address to be cracked. 6627783Seric ** 6637783Seric ** Returns: 6647783Seric ** a pointer to the new version. 6657783Seric ** 6667783Seric ** Side Effects: 6677890Seric ** none. 6687783Seric ** 6697783Seric ** Warning: 6707783Seric ** The return value is saved in local storage and should 6717783Seric ** be copied if it is to be reused. 6727783Seric */ 6737783Seric 6747783Seric char * 6757890Seric crackaddr(addr) 6767890Seric register char *addr; 6777783Seric { 6787783Seric register char *p; 67951379Seric register char c; 68051379Seric int cmtlev; 68156764Seric int realcmtlev; 68256764Seric int anglelev, realanglelev; 68351379Seric int copylev; 68451379Seric bool qmode; 68556764Seric bool realqmode; 68656764Seric bool skipping; 68751379Seric bool putgmac = FALSE; 68856735Seric bool quoteit = FALSE; 68964148Seric bool gotangle = FALSE; 69051379Seric register char *bp; 69156764Seric char *buflim; 6927783Seric static char buf[MAXNAME]; 6937783Seric 6947783Seric if (tTd(33, 1)) 6957890Seric printf("crackaddr(%s)\n", addr); 6967783Seric 6978082Seric /* strip leading spaces */ 69858050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 6998082Seric addr++; 7008082Seric 7017783Seric /* 70251379Seric ** Start by assuming we have no angle brackets. This will be 70351379Seric ** adjusted later if we find them. 7047783Seric */ 7057783Seric 70651379Seric bp = buf; 70756764Seric buflim = &buf[sizeof buf - 5]; 70851379Seric p = addr; 70956764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 71056764Seric qmode = realqmode = FALSE; 71151379Seric 71251379Seric while ((c = *p++) != '\0') 7137783Seric { 71456764Seric /* 71556764Seric ** If the buffer is overful, go into a special "skipping" 71656764Seric ** mode that tries to keep legal syntax but doesn't actually 71756764Seric ** output things. 71856764Seric */ 7197783Seric 72056764Seric skipping = bp >= buflim; 72156735Seric 72256764Seric if (copylev > 0 && !skipping) 72356764Seric *bp++ = c; 72456735Seric 72551379Seric /* check for backslash escapes */ 72651379Seric if (c == '\\') 7277783Seric { 72858890Seric /* arrange to quote the address */ 72958890Seric if (cmtlev <= 0 && !qmode) 73058890Seric quoteit = TRUE; 73158890Seric 73251379Seric if ((c = *p++) == '\0') 7337783Seric { 73451379Seric /* too far */ 73551379Seric p--; 73651379Seric goto putg; 7377783Seric } 73856764Seric if (copylev > 0 && !skipping) 73951379Seric *bp++ = c; 74051379Seric goto putg; 7417783Seric } 7427783Seric 74351379Seric /* check for quoted strings */ 74464967Seric if (c == '"' && cmtlev <= 0) 7457783Seric { 74651379Seric qmode = !qmode; 74756764Seric if (copylev > 0 && !skipping) 74856764Seric realqmode = !realqmode; 74951379Seric continue; 7507783Seric } 75151379Seric if (qmode) 75251379Seric goto putg; 7537783Seric 75451379Seric /* check for comments */ 75551379Seric if (c == '(') 7567783Seric { 75751379Seric cmtlev++; 75856764Seric 75956764Seric /* allow space for closing paren */ 76056764Seric if (!skipping) 76156764Seric { 76256764Seric buflim--; 76356764Seric realcmtlev++; 76456764Seric if (copylev++ <= 0) 76556764Seric { 76656764Seric *bp++ = ' '; 76756764Seric *bp++ = c; 76856764Seric } 76956764Seric } 77051379Seric } 77151379Seric if (cmtlev > 0) 77251379Seric { 77351379Seric if (c == ')') 7747783Seric { 77551379Seric cmtlev--; 77651379Seric copylev--; 77756764Seric if (!skipping) 77856764Seric { 77956764Seric realcmtlev--; 78056764Seric buflim++; 78156764Seric } 7827783Seric } 7837783Seric continue; 7847783Seric } 78556764Seric else if (c == ')') 78656764Seric { 78756764Seric /* syntax error: unmatched ) */ 78865544Seric if (copylev > 0 && !skipping) 78956764Seric bp--; 79056764Seric } 7917783Seric 79256764Seric /* check for characters that may have to be quoted */ 79364148Seric if (strchr(".'@,;:\\()[]", c) != NULL) 79456764Seric { 79556764Seric /* 79656764Seric ** If these occur as the phrase part of a <> 79756764Seric ** construct, but are not inside of () or already 79856764Seric ** quoted, they will have to be quoted. Note that 79956764Seric ** now (but don't actually do the quoting). 80056764Seric */ 80156764Seric 80256764Seric if (cmtlev <= 0 && !qmode) 80356764Seric quoteit = TRUE; 80456764Seric } 80556764Seric 80651379Seric /* check for angle brackets */ 80751379Seric if (c == '<') 80851379Seric { 80956735Seric register char *q; 81056735Seric 81164148Seric /* assume first of two angles is bogus */ 81264148Seric if (gotangle) 81364148Seric quoteit = TRUE; 81464148Seric gotangle = TRUE; 81564148Seric 81651379Seric /* oops -- have to change our mind */ 81764752Seric anglelev = 1; 81856764Seric if (!skipping) 81964752Seric realanglelev = 1; 82056764Seric 82156735Seric bp = buf; 82256735Seric if (quoteit) 82356735Seric { 82456735Seric *bp++ = '"'; 82556735Seric 82656735Seric /* back up over the '<' and any spaces */ 82756735Seric --p; 82858050Seric while (isascii(*--p) && isspace(*p)) 82956735Seric continue; 83056735Seric p++; 83156735Seric } 83256735Seric for (q = addr; q < p; ) 83356735Seric { 83456735Seric c = *q++; 83556764Seric if (bp < buflim) 83656764Seric { 83756764Seric if (quoteit && c == '"') 83856764Seric *bp++ = '\\'; 83956764Seric *bp++ = c; 84056764Seric } 84156735Seric } 84256735Seric if (quoteit) 84356735Seric { 84464148Seric if (bp == &buf[1]) 84564148Seric bp--; 84664148Seric else 84764148Seric *bp++ = '"'; 84856764Seric while ((c = *p++) != '<') 84956764Seric { 85056764Seric if (bp < buflim) 85156764Seric *bp++ = c; 85256764Seric } 85356764Seric *bp++ = c; 85456735Seric } 85551379Seric copylev = 0; 85656735Seric putgmac = quoteit = FALSE; 85751379Seric continue; 85851379Seric } 8597783Seric 86051379Seric if (c == '>') 8617783Seric { 86256764Seric if (anglelev > 0) 86356764Seric { 86456764Seric anglelev--; 86556764Seric if (!skipping) 86656764Seric { 86756764Seric realanglelev--; 86856764Seric buflim++; 86956764Seric } 87056764Seric } 87156764Seric else if (!skipping) 87256764Seric { 87356764Seric /* syntax error: unmatched > */ 87456764Seric if (copylev > 0) 87556764Seric bp--; 87664752Seric quoteit = TRUE; 87756764Seric continue; 87856764Seric } 87951379Seric if (copylev++ <= 0) 88051379Seric *bp++ = c; 88151379Seric continue; 8827783Seric } 88351379Seric 88451379Seric /* must be a real address character */ 88551379Seric putg: 88651379Seric if (copylev <= 0 && !putgmac) 88751379Seric { 88858050Seric *bp++ = MACROEXPAND; 88951379Seric *bp++ = 'g'; 89051379Seric putgmac = TRUE; 89151379Seric } 8927783Seric } 8937783Seric 89456764Seric /* repair any syntactic damage */ 89556764Seric if (realqmode) 89656764Seric *bp++ = '"'; 89756764Seric while (realcmtlev-- > 0) 89856764Seric *bp++ = ')'; 89956764Seric while (realanglelev-- > 0) 90056764Seric *bp++ = '>'; 90151379Seric *bp++ = '\0'; 9027783Seric 9037783Seric if (tTd(33, 1)) 9047944Seric printf("crackaddr=>`%s'\n", buf); 9057783Seric 9067783Seric return (buf); 9077783Seric } 9089382Seric /* 9099382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 9109382Seric ** 9119382Seric ** Parameters: 91265870Seric ** mci -- the connection information. 91367546Seric ** h -- the header to put. 9149382Seric ** e -- envelope to use. 9159382Seric ** 9169382Seric ** Returns: 9179382Seric ** none. 9189382Seric ** 9199382Seric ** Side Effects: 9209382Seric ** none. 9219382Seric */ 9229382Seric 92357589Seric /* 92457589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 92557589Seric */ 92657589Seric #ifndef MAX 92757589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 92857589Seric #endif 92957589Seric 93068228Seric putheader(mci, h, e) 93165870Seric register MCI *mci; 93267546Seric register HDR *h; 9339382Seric register ENVELOPE *e; 9349382Seric { 93557135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 93657135Seric char obuf[MAXLINE]; 9379382Seric 93859882Seric if (tTd(34, 1)) 93965870Seric printf("--- putheader, mailer = %s ---\n", 94065870Seric mci->mci_mailer->m_name); 94159882Seric 94267546Seric mci->mci_flags |= MCIF_INHEADER; 94367546Seric for (; h != NULL; h = h->h_link) 9449382Seric { 9459382Seric register char *p; 94610689Seric extern bool bitintersect(); 9479382Seric 94859882Seric if (tTd(34, 11)) 94959882Seric { 95059882Seric printf(" %s: ", h->h_field); 95159882Seric xputs(h->h_value); 95259882Seric } 95359882Seric 9549382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 95565870Seric !bitintersect(h->h_mflags, mci->mci_mailer->m_flags)) 95659882Seric { 95759882Seric if (tTd(34, 11)) 95859882Seric printf(" (skipped)\n"); 9599382Seric continue; 96059882Seric } 9619382Seric 96211414Seric /* handle Resent-... headers specially */ 96311414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 96459882Seric { 96559882Seric if (tTd(34, 11)) 96659882Seric printf(" (skipped (resent))\n"); 96711414Seric continue; 96859882Seric } 96911414Seric 97066784Seric /* suppress return receipts if requested */ 97166784Seric if (bitset(H_RECEIPTTO, h->h_flags) && 97266784Seric bitset(EF_NORECEIPT, e->e_flags)) 97366784Seric { 97466784Seric if (tTd(34, 11)) 97566784Seric printf(" (skipped (receipt))\n"); 97666784Seric continue; 97766784Seric } 97866784Seric 97967694Seric /* suppress Content-Transfer-Encoding: if we are MIMEing */ 98067694Seric if (bitset(H_CTE, h->h_flags) && 98168228Seric bitset(MCIF_CVT8TO7, mci->mci_flags)) 98267694Seric { 98367694Seric if (tTd(34, 11)) 98467694Seric printf(" (skipped (content-transfer-encoding))\n"); 98567694Seric continue; 98667694Seric } 98767694Seric 98865152Seric /* macro expand value if generated internally */ 9899382Seric p = h->h_value; 9909382Seric if (bitset(H_DEFAULT, h->h_flags)) 9919382Seric { 9929382Seric expand(p, buf, &buf[sizeof buf], e); 9939382Seric p = buf; 9949382Seric if (p == NULL || *p == '\0') 99565152Seric { 99665152Seric if (tTd(34, 11)) 99765152Seric printf(" (skipped -- null value)\n"); 9989382Seric continue; 99965152Seric } 10009382Seric } 10019382Seric 100265152Seric if (tTd(34, 11)) 100365152Seric printf("\n"); 100465152Seric 100568449Seric if (bitset(H_STRIPVAL, h->h_flags)) 10069382Seric { 100768449Seric /* empty field */ 100868449Seric (void) sprintf(obuf, "%s:", h->h_field); 100968449Seric putline(obuf, mci); 101068449Seric } 101168449Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 101268449Seric { 10139382Seric /* address field */ 10149382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 10159382Seric 10169382Seric if (bitset(H_FROM, h->h_flags)) 10179382Seric oldstyle = FALSE; 101865870Seric commaize(h, p, oldstyle, mci, e); 10199382Seric } 10209382Seric else 10219382Seric { 10229382Seric /* vanilla header line */ 102312159Seric register char *nlp; 102412159Seric 102559579Seric (void) sprintf(obuf, "%s: ", h->h_field); 102656795Seric while ((nlp = strchr(p, '\n')) != NULL) 102712159Seric { 102812159Seric *nlp = '\0'; 102912159Seric (void) strcat(obuf, p); 103012159Seric *nlp = '\n'; 103165870Seric putline(obuf, mci); 103212159Seric p = ++nlp; 103312161Seric obuf[0] = '\0'; 103412159Seric } 103512159Seric (void) strcat(obuf, p); 103665870Seric putline(obuf, mci); 10379382Seric } 10389382Seric } 103967887Seric 104067887Seric /* 104167887Seric ** If we are converting this to a MIME message, add the 104267889Seric ** MIME headers. 104367887Seric */ 104467887Seric 104567887Seric if (bitset(MM_MIME8BIT, MimeMode) && 104667887Seric bitset(EF_HAS8BIT, e->e_flags) && 104767887Seric !bitnset(M_8BITS, mci->mci_mailer->m_flags) && 104867889Seric !bitset(MCIF_CVT8TO7, mci->mci_flags)) 104967887Seric { 105067889Seric if (hvalue("MIME-Version", e->e_header) == NULL) 105167889Seric putline("MIME-Version: 1.0", mci); 105267889Seric if (hvalue("Content-Type", e->e_header) == NULL) 105367889Seric { 105467889Seric sprintf(obuf, "Content-Type: text/plain; charset=%s", 105567896Seric defcharset(e)); 105667889Seric putline(obuf, mci); 105767889Seric } 105867889Seric if (hvalue("Content-Transfer-Encoding", e->e_header) == NULL) 105967889Seric putline("Content-Transfer-Encoding: 8bit", mci); 106067887Seric } 10619382Seric } 10629382Seric /* 10639382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 10649382Seric ** 10659382Seric ** Parameters: 10669382Seric ** h -- the header field to output. 10679382Seric ** p -- the value to put in it. 10689382Seric ** oldstyle -- TRUE if this is an old style header. 106965870Seric ** mci -- the connection information. 107055012Seric ** e -- the envelope containing the message. 10719382Seric ** 10729382Seric ** Returns: 10739382Seric ** none. 10749382Seric ** 10759382Seric ** Side Effects: 10769382Seric ** outputs "p" to file "fp". 10779382Seric */ 10789382Seric 107965870Seric void 108065870Seric commaize(h, p, oldstyle, mci, e) 10819382Seric register HDR *h; 10829382Seric register char *p; 10839382Seric bool oldstyle; 108465870Seric register MCI *mci; 108555012Seric register ENVELOPE *e; 10869382Seric { 10879382Seric register char *obp; 10889382Seric int opos; 108966004Seric int omax; 10909382Seric bool firstone = TRUE; 109111157Seric char obuf[MAXLINE + 3]; 10929382Seric 10939382Seric /* 10949382Seric ** Output the address list translated by the 10959382Seric ** mailer and with commas. 10969382Seric */ 10979382Seric 10989382Seric if (tTd(14, 2)) 10999382Seric printf("commaize(%s: %s)\n", h->h_field, p); 11009382Seric 11019382Seric obp = obuf; 110259579Seric (void) sprintf(obp, "%s: ", h->h_field); 11039382Seric opos = strlen(h->h_field) + 2; 11049382Seric obp += opos; 110566254Seric omax = mci->mci_mailer->m_linelimit - 2; 110666254Seric if (omax < 0 || omax > 78) 110766254Seric omax = 78; 11089382Seric 11099382Seric /* 11109382Seric ** Run through the list of values. 11119382Seric */ 11129382Seric 11139382Seric while (*p != '\0') 11149382Seric { 11159382Seric register char *name; 111654983Seric register int c; 11179382Seric char savechar; 111859163Seric int flags; 111959163Seric auto int stat; 11209382Seric 11219382Seric /* 11229382Seric ** Find the end of the name. New style names 11239382Seric ** end with a comma, old style names end with 11249382Seric ** a space character. However, spaces do not 11259382Seric ** necessarily delimit an old-style name -- at 11269382Seric ** signs mean keep going. 11279382Seric */ 11289382Seric 11299382Seric /* find end of name */ 113058050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 11319382Seric p++; 11329382Seric name = p; 11339382Seric for (;;) 11349382Seric { 113558333Seric auto char *oldp; 113616909Seric char pvpbuf[PSBUFSIZE]; 11379382Seric 113865066Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, 113965066Seric sizeof pvpbuf, &oldp); 114058333Seric p = oldp; 11419382Seric 11429382Seric /* look to see if we have an at sign */ 114358050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 11449382Seric p++; 11459382Seric 114658170Seric if (*p != '@') 11479382Seric { 11489382Seric p = oldp; 11499382Seric break; 11509382Seric } 11519382Seric p += *p == '@' ? 1 : 2; 115258050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 11539382Seric p++; 11549382Seric } 11559382Seric /* at the end of one complete name */ 11569382Seric 11579382Seric /* strip off trailing white space */ 115858050Seric while (p >= name && 115958050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 11609382Seric p--; 11619382Seric if (++p == name) 11629382Seric continue; 11639382Seric savechar = *p; 11649382Seric *p = '\0'; 11659382Seric 11669382Seric /* translate the name to be relative */ 116759163Seric flags = RF_HEADERADDR|RF_ADDDOMAIN; 116859163Seric if (bitset(H_FROM, h->h_flags)) 116959163Seric flags |= RF_SENDERADDR; 1170*68522Seric #ifdef USERDB 1171*68522Seric else if (e->e_from.q_mailer != NULL && 1172*68522Seric bitnset(M_UDBRECIPIENT, e->e_from.q_mailer->m_flags)) 1173*68522Seric { 1174*68522Seric extern char *udbsender(); 1175*68522Seric 1176*68522Seric name = udbsender(name); 1177*68522Seric } 1178*68522Seric #endif 117959163Seric stat = EX_OK; 118065870Seric name = remotename(name, mci->mci_mailer, flags, &stat, e); 11819382Seric if (*name == '\0') 11829382Seric { 11839382Seric *p = savechar; 11849382Seric continue; 11859382Seric } 11869382Seric 11879382Seric /* output the name with nice formatting */ 118854983Seric opos += strlen(name); 11899382Seric if (!firstone) 11909382Seric opos += 2; 119166004Seric if (opos > omax && !firstone) 11929382Seric { 119310178Seric (void) strcpy(obp, ",\n"); 119465870Seric putline(obuf, mci); 11959382Seric obp = obuf; 119666255Seric (void) strcpy(obp, " "); 119710161Seric opos = strlen(obp); 119810161Seric obp += opos; 119954983Seric opos += strlen(name); 12009382Seric } 12019382Seric else if (!firstone) 12029382Seric { 120366255Seric (void) strcpy(obp, ", "); 12049382Seric obp += 2; 12059382Seric } 12069382Seric 120754983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 120854983Seric *obp++ = c; 12099382Seric firstone = FALSE; 12109382Seric *p = savechar; 12119382Seric } 12129382Seric (void) strcpy(obp, "\n"); 121365870Seric putline(obuf, mci); 12149382Seric } 12159382Seric /* 121658170Seric ** COPYHEADER -- copy header list 12179382Seric ** 121858170Seric ** This routine is the equivalent of newstr for header lists 121958170Seric ** 12209382Seric ** Parameters: 122158170Seric ** header -- list of header structures to copy. 12229382Seric ** 12239382Seric ** Returns: 122458170Seric ** a copy of 'header'. 12259382Seric ** 12269382Seric ** Side Effects: 12279382Seric ** none. 12289382Seric */ 12299382Seric 123058170Seric HDR * 123158170Seric copyheader(header) 123258170Seric register HDR *header; 12339382Seric { 123458170Seric register HDR *newhdr; 123558170Seric HDR *ret; 123658170Seric register HDR **tail = &ret; 12379382Seric 123858170Seric while (header != NULL) 123958170Seric { 124058170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 124158170Seric STRUCTCOPY(*header, *newhdr); 124258170Seric *tail = newhdr; 124358170Seric tail = &newhdr->h_link; 124458170Seric header = header->h_link; 124558170Seric } 124658170Seric *tail = NULL; 124758170Seric 124858170Seric return ret; 12499382Seric } 1250