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*67730Seric static char sccsid[] = "@(#)headers.c 8.36 (Berkeley) 08/21/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) 445*67730Seric { 44625013Seric e->e_msgpriority = e->e_msgsize 44724981Seric - e->e_class * WkClassFact 44824981Seric + e->e_nrcpts * WkRecipFact; 449*67730Seric if (e->e_class < 0) 450*67730Seric e->e_timeoutclass = TOC_NONURGENT; 451*67730Seric else if (e->e_class > 0) 452*67730Seric e->e_timeoutclass = TOC_URGENT; 453*67730Seric } 4547783Seric 455*67730Seric /* message timeout priority */ 456*67730Seric p = hvalue("priority", e->e_header); 457*67730Seric if (full && p != NULL) 458*67730Seric { 459*67730Seric /* (this should be in the configuration file) */ 460*67730Seric if (strcasecmp(p, "urgent")) 461*67730Seric e->e_timeoutclass = TOC_URGENT; 462*67730Seric else if (strcasecmp(p, "normal")) 463*67730Seric e->e_timeoutclass = TOC_NORMAL; 464*67730Seric else if (strcasecmp(p, "non-urgent")) 465*67730Seric e->e_timeoutclass = TOC_NONURGENT; 466*67730Seric } 467*67730Seric 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", 55765089Seric e->e_from.q_paddr, e->e_msgsize, e->e_class, 55865089Seric 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", 57965089Seric e->e_id, shortenstring(e->e_from.q_paddr, 83)); 58065089Seric syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d", 58165089Seric e->e_id, e->e_msgsize, e->e_class, 58265089Seric e->e_msgpriority, e->e_nrcpts); 58365089Seric if (msgid != NULL) 58465059Seric syslog(LOG_INFO, "%s: msgid=%s", e->e_id, msgid); 58565650Seric sbp = sbuf; 58665650Seric sprintf(sbp, "%s:", e->e_id); 58765650Seric sbp += strlen(sbp); 58865089Seric if (e->e_bodytype != NULL) 58965650Seric { 59065650Seric sprintf(sbp, " bodytype=%s,", e->e_bodytype); 59165650Seric sbp += strlen(sbp); 59265650Seric } 59365089Seric p = macvalue('r', e); 59465089Seric if (p != NULL) 59565650Seric { 59665731Seric sprintf(sbp, " proto=%s,", p); 59765650Seric sbp += strlen(sbp); 59865650Seric } 59965650Seric syslog(LOG_INFO, "%s relay=%s", sbuf, name); 60065059Seric # endif 60166748Seric # endif 6027783Seric } 6037783Seric /* 6047783Seric ** PRIENCODE -- encode external priority names into internal values. 6057783Seric ** 6067783Seric ** Parameters: 6077783Seric ** p -- priority in ascii. 6087783Seric ** 6097783Seric ** Returns: 6107783Seric ** priority as a numeric level. 6117783Seric ** 6127783Seric ** Side Effects: 6137783Seric ** none. 6147783Seric */ 6157783Seric 6167783Seric priencode(p) 6177783Seric char *p; 6187783Seric { 6198253Seric register int i; 6207783Seric 6218253Seric for (i = 0; i < NumPriorities; i++) 6227783Seric { 62333725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 6248253Seric return (Priorities[i].pri_val); 6257783Seric } 6268253Seric 6278253Seric /* unknown priority */ 6288253Seric return (0); 6297783Seric } 6307783Seric /* 6317890Seric ** CRACKADDR -- parse an address and turn it into a macro 6327783Seric ** 6337783Seric ** This doesn't actually parse the address -- it just extracts 6347783Seric ** it and replaces it with "$g". The parse is totally ad hoc 6357783Seric ** and isn't even guaranteed to leave something syntactically 6367783Seric ** identical to what it started with. However, it does leave 6377783Seric ** something semantically identical. 6387783Seric ** 63951379Seric ** This algorithm has been cleaned up to handle a wider range 64051379Seric ** of cases -- notably quoted and backslash escaped strings. 64151379Seric ** This modification makes it substantially better at preserving 64251379Seric ** the original syntax. 6437783Seric ** 6447783Seric ** Parameters: 6457890Seric ** addr -- the address to be cracked. 6467783Seric ** 6477783Seric ** Returns: 6487783Seric ** a pointer to the new version. 6497783Seric ** 6507783Seric ** Side Effects: 6517890Seric ** none. 6527783Seric ** 6537783Seric ** Warning: 6547783Seric ** The return value is saved in local storage and should 6557783Seric ** be copied if it is to be reused. 6567783Seric */ 6577783Seric 6587783Seric char * 6597890Seric crackaddr(addr) 6607890Seric register char *addr; 6617783Seric { 6627783Seric register char *p; 66351379Seric register char c; 66451379Seric int cmtlev; 66556764Seric int realcmtlev; 66656764Seric int anglelev, realanglelev; 66751379Seric int copylev; 66851379Seric bool qmode; 66956764Seric bool realqmode; 67056764Seric bool skipping; 67151379Seric bool putgmac = FALSE; 67256735Seric bool quoteit = FALSE; 67364148Seric bool gotangle = FALSE; 67451379Seric register char *bp; 67556764Seric char *buflim; 6767783Seric static char buf[MAXNAME]; 6777783Seric 6787783Seric if (tTd(33, 1)) 6797890Seric printf("crackaddr(%s)\n", addr); 6807783Seric 6818082Seric /* strip leading spaces */ 68258050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 6838082Seric addr++; 6848082Seric 6857783Seric /* 68651379Seric ** Start by assuming we have no angle brackets. This will be 68751379Seric ** adjusted later if we find them. 6887783Seric */ 6897783Seric 69051379Seric bp = buf; 69156764Seric buflim = &buf[sizeof buf - 5]; 69251379Seric p = addr; 69356764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 69456764Seric qmode = realqmode = FALSE; 69551379Seric 69651379Seric while ((c = *p++) != '\0') 6977783Seric { 69856764Seric /* 69956764Seric ** If the buffer is overful, go into a special "skipping" 70056764Seric ** mode that tries to keep legal syntax but doesn't actually 70156764Seric ** output things. 70256764Seric */ 7037783Seric 70456764Seric skipping = bp >= buflim; 70556735Seric 70656764Seric if (copylev > 0 && !skipping) 70756764Seric *bp++ = c; 70856735Seric 70951379Seric /* check for backslash escapes */ 71051379Seric if (c == '\\') 7117783Seric { 71258890Seric /* arrange to quote the address */ 71358890Seric if (cmtlev <= 0 && !qmode) 71458890Seric quoteit = TRUE; 71558890Seric 71651379Seric if ((c = *p++) == '\0') 7177783Seric { 71851379Seric /* too far */ 71951379Seric p--; 72051379Seric goto putg; 7217783Seric } 72256764Seric if (copylev > 0 && !skipping) 72351379Seric *bp++ = c; 72451379Seric goto putg; 7257783Seric } 7267783Seric 72751379Seric /* check for quoted strings */ 72864967Seric if (c == '"' && cmtlev <= 0) 7297783Seric { 73051379Seric qmode = !qmode; 73156764Seric if (copylev > 0 && !skipping) 73256764Seric realqmode = !realqmode; 73351379Seric continue; 7347783Seric } 73551379Seric if (qmode) 73651379Seric goto putg; 7377783Seric 73851379Seric /* check for comments */ 73951379Seric if (c == '(') 7407783Seric { 74151379Seric cmtlev++; 74256764Seric 74356764Seric /* allow space for closing paren */ 74456764Seric if (!skipping) 74556764Seric { 74656764Seric buflim--; 74756764Seric realcmtlev++; 74856764Seric if (copylev++ <= 0) 74956764Seric { 75056764Seric *bp++ = ' '; 75156764Seric *bp++ = c; 75256764Seric } 75356764Seric } 75451379Seric } 75551379Seric if (cmtlev > 0) 75651379Seric { 75751379Seric if (c == ')') 7587783Seric { 75951379Seric cmtlev--; 76051379Seric copylev--; 76156764Seric if (!skipping) 76256764Seric { 76356764Seric realcmtlev--; 76456764Seric buflim++; 76556764Seric } 7667783Seric } 7677783Seric continue; 7687783Seric } 76956764Seric else if (c == ')') 77056764Seric { 77156764Seric /* syntax error: unmatched ) */ 77265544Seric if (copylev > 0 && !skipping) 77356764Seric bp--; 77456764Seric } 7757783Seric 77656764Seric /* check for characters that may have to be quoted */ 77764148Seric if (strchr(".'@,;:\\()[]", c) != NULL) 77856764Seric { 77956764Seric /* 78056764Seric ** If these occur as the phrase part of a <> 78156764Seric ** construct, but are not inside of () or already 78256764Seric ** quoted, they will have to be quoted. Note that 78356764Seric ** now (but don't actually do the quoting). 78456764Seric */ 78556764Seric 78656764Seric if (cmtlev <= 0 && !qmode) 78756764Seric quoteit = TRUE; 78856764Seric } 78956764Seric 79051379Seric /* check for angle brackets */ 79151379Seric if (c == '<') 79251379Seric { 79356735Seric register char *q; 79456735Seric 79564148Seric /* assume first of two angles is bogus */ 79664148Seric if (gotangle) 79764148Seric quoteit = TRUE; 79864148Seric gotangle = TRUE; 79964148Seric 80051379Seric /* oops -- have to change our mind */ 80164752Seric anglelev = 1; 80256764Seric if (!skipping) 80364752Seric realanglelev = 1; 80456764Seric 80556735Seric bp = buf; 80656735Seric if (quoteit) 80756735Seric { 80856735Seric *bp++ = '"'; 80956735Seric 81056735Seric /* back up over the '<' and any spaces */ 81156735Seric --p; 81258050Seric while (isascii(*--p) && isspace(*p)) 81356735Seric continue; 81456735Seric p++; 81556735Seric } 81656735Seric for (q = addr; q < p; ) 81756735Seric { 81856735Seric c = *q++; 81956764Seric if (bp < buflim) 82056764Seric { 82156764Seric if (quoteit && c == '"') 82256764Seric *bp++ = '\\'; 82356764Seric *bp++ = c; 82456764Seric } 82556735Seric } 82656735Seric if (quoteit) 82756735Seric { 82864148Seric if (bp == &buf[1]) 82964148Seric bp--; 83064148Seric else 83164148Seric *bp++ = '"'; 83256764Seric while ((c = *p++) != '<') 83356764Seric { 83456764Seric if (bp < buflim) 83556764Seric *bp++ = c; 83656764Seric } 83756764Seric *bp++ = c; 83856735Seric } 83951379Seric copylev = 0; 84056735Seric putgmac = quoteit = FALSE; 84151379Seric continue; 84251379Seric } 8437783Seric 84451379Seric if (c == '>') 8457783Seric { 84656764Seric if (anglelev > 0) 84756764Seric { 84856764Seric anglelev--; 84956764Seric if (!skipping) 85056764Seric { 85156764Seric realanglelev--; 85256764Seric buflim++; 85356764Seric } 85456764Seric } 85556764Seric else if (!skipping) 85656764Seric { 85756764Seric /* syntax error: unmatched > */ 85856764Seric if (copylev > 0) 85956764Seric bp--; 86064752Seric quoteit = TRUE; 86156764Seric continue; 86256764Seric } 86351379Seric if (copylev++ <= 0) 86451379Seric *bp++ = c; 86551379Seric continue; 8667783Seric } 86751379Seric 86851379Seric /* must be a real address character */ 86951379Seric putg: 87051379Seric if (copylev <= 0 && !putgmac) 87151379Seric { 87258050Seric *bp++ = MACROEXPAND; 87351379Seric *bp++ = 'g'; 87451379Seric putgmac = TRUE; 87551379Seric } 8767783Seric } 8777783Seric 87856764Seric /* repair any syntactic damage */ 87956764Seric if (realqmode) 88056764Seric *bp++ = '"'; 88156764Seric while (realcmtlev-- > 0) 88256764Seric *bp++ = ')'; 88356764Seric while (realanglelev-- > 0) 88456764Seric *bp++ = '>'; 88551379Seric *bp++ = '\0'; 8867783Seric 8877783Seric if (tTd(33, 1)) 8887944Seric printf("crackaddr=>`%s'\n", buf); 8897783Seric 8907783Seric return (buf); 8917783Seric } 8929382Seric /* 8939382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 8949382Seric ** 8959382Seric ** Parameters: 89665870Seric ** mci -- the connection information. 89767546Seric ** h -- the header to put. 8989382Seric ** e -- envelope to use. 8999382Seric ** 9009382Seric ** Returns: 9019382Seric ** none. 9029382Seric ** 9039382Seric ** Side Effects: 9049382Seric ** none. 9059382Seric */ 9069382Seric 90757589Seric /* 90857589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 90957589Seric */ 91057589Seric #ifndef MAX 91157589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 91257589Seric #endif 91357589Seric 91467546Seric putheader(mci, h, e) 91565870Seric register MCI *mci; 91667546Seric register HDR *h; 9179382Seric register ENVELOPE *e; 9189382Seric { 91957135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 92057135Seric char obuf[MAXLINE]; 9219382Seric 92259882Seric if (tTd(34, 1)) 92365870Seric printf("--- putheader, mailer = %s ---\n", 92465870Seric mci->mci_mailer->m_name); 92559882Seric 92667546Seric mci->mci_flags |= MCIF_INHEADER; 92767546Seric for (; h != NULL; h = h->h_link) 9289382Seric { 9299382Seric register char *p; 93010689Seric extern bool bitintersect(); 9319382Seric 93259882Seric if (tTd(34, 11)) 93359882Seric { 93459882Seric printf(" %s: ", h->h_field); 93559882Seric xputs(h->h_value); 93659882Seric } 93759882Seric 9389382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 93965870Seric !bitintersect(h->h_mflags, mci->mci_mailer->m_flags)) 94059882Seric { 94159882Seric if (tTd(34, 11)) 94259882Seric printf(" (skipped)\n"); 9439382Seric continue; 94459882Seric } 9459382Seric 94611414Seric /* handle Resent-... headers specially */ 94711414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 94859882Seric { 94959882Seric if (tTd(34, 11)) 95059882Seric printf(" (skipped (resent))\n"); 95111414Seric continue; 95259882Seric } 95311414Seric 95466784Seric /* suppress return receipts if requested */ 95566784Seric if (bitset(H_RECEIPTTO, h->h_flags) && 95666784Seric bitset(EF_NORECEIPT, e->e_flags)) 95766784Seric { 95866784Seric if (tTd(34, 11)) 95966784Seric printf(" (skipped (receipt))\n"); 96066784Seric continue; 96166784Seric } 96266784Seric 96367694Seric /* suppress Content-Transfer-Encoding: if we are MIMEing */ 96467694Seric if (bitset(H_CTE, h->h_flags) && 96567694Seric bitset(MCIF_CVT8TO7, mci->mci_flags)) 96667694Seric { 96767694Seric if (tTd(34, 11)) 96867694Seric printf(" (skipped (content-transfer-encoding))\n"); 96967694Seric continue; 97067694Seric } 97167694Seric 97265152Seric /* macro expand value if generated internally */ 9739382Seric p = h->h_value; 9749382Seric if (bitset(H_DEFAULT, h->h_flags)) 9759382Seric { 9769382Seric expand(p, buf, &buf[sizeof buf], e); 9779382Seric p = buf; 9789382Seric if (p == NULL || *p == '\0') 97965152Seric { 98065152Seric if (tTd(34, 11)) 98165152Seric printf(" (skipped -- null value)\n"); 9829382Seric continue; 98365152Seric } 9849382Seric } 9859382Seric 98665152Seric if (tTd(34, 11)) 98765152Seric printf("\n"); 98865152Seric 9899382Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 9909382Seric { 9919382Seric /* address field */ 9929382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 9939382Seric 9949382Seric if (bitset(H_FROM, h->h_flags)) 9959382Seric oldstyle = FALSE; 99665870Seric commaize(h, p, oldstyle, mci, e); 9979382Seric } 9989382Seric else 9999382Seric { 10009382Seric /* vanilla header line */ 100112159Seric register char *nlp; 100212159Seric 100359579Seric (void) sprintf(obuf, "%s: ", h->h_field); 100456795Seric while ((nlp = strchr(p, '\n')) != NULL) 100512159Seric { 100612159Seric *nlp = '\0'; 100712159Seric (void) strcat(obuf, p); 100812159Seric *nlp = '\n'; 100965870Seric putline(obuf, mci); 101012159Seric p = ++nlp; 101112161Seric obuf[0] = '\0'; 101212159Seric } 101312159Seric (void) strcat(obuf, p); 101465870Seric putline(obuf, mci); 10159382Seric } 10169382Seric } 10179382Seric } 10189382Seric /* 10199382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 10209382Seric ** 10219382Seric ** Parameters: 10229382Seric ** h -- the header field to output. 10239382Seric ** p -- the value to put in it. 10249382Seric ** oldstyle -- TRUE if this is an old style header. 102565870Seric ** mci -- the connection information. 102655012Seric ** e -- the envelope containing the message. 10279382Seric ** 10289382Seric ** Returns: 10299382Seric ** none. 10309382Seric ** 10319382Seric ** Side Effects: 10329382Seric ** outputs "p" to file "fp". 10339382Seric */ 10349382Seric 103565870Seric void 103665870Seric commaize(h, p, oldstyle, mci, e) 10379382Seric register HDR *h; 10389382Seric register char *p; 10399382Seric bool oldstyle; 104065870Seric register MCI *mci; 104155012Seric register ENVELOPE *e; 10429382Seric { 10439382Seric register char *obp; 10449382Seric int opos; 104566004Seric int omax; 10469382Seric bool firstone = TRUE; 104711157Seric char obuf[MAXLINE + 3]; 10489382Seric 10499382Seric /* 10509382Seric ** Output the address list translated by the 10519382Seric ** mailer and with commas. 10529382Seric */ 10539382Seric 10549382Seric if (tTd(14, 2)) 10559382Seric printf("commaize(%s: %s)\n", h->h_field, p); 10569382Seric 10579382Seric obp = obuf; 105859579Seric (void) sprintf(obp, "%s: ", h->h_field); 10599382Seric opos = strlen(h->h_field) + 2; 10609382Seric obp += opos; 106166254Seric omax = mci->mci_mailer->m_linelimit - 2; 106266254Seric if (omax < 0 || omax > 78) 106366254Seric omax = 78; 10649382Seric 10659382Seric /* 10669382Seric ** Run through the list of values. 10679382Seric */ 10689382Seric 10699382Seric while (*p != '\0') 10709382Seric { 10719382Seric register char *name; 107254983Seric register int c; 10739382Seric char savechar; 107459163Seric int flags; 107559163Seric auto int stat; 10769382Seric 10779382Seric /* 10789382Seric ** Find the end of the name. New style names 10799382Seric ** end with a comma, old style names end with 10809382Seric ** a space character. However, spaces do not 10819382Seric ** necessarily delimit an old-style name -- at 10829382Seric ** signs mean keep going. 10839382Seric */ 10849382Seric 10859382Seric /* find end of name */ 108658050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 10879382Seric p++; 10889382Seric name = p; 10899382Seric for (;;) 10909382Seric { 109158333Seric auto char *oldp; 109216909Seric char pvpbuf[PSBUFSIZE]; 10939382Seric 109465066Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, 109565066Seric sizeof pvpbuf, &oldp); 109658333Seric p = oldp; 10979382Seric 10989382Seric /* look to see if we have an at sign */ 109958050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 11009382Seric p++; 11019382Seric 110258170Seric if (*p != '@') 11039382Seric { 11049382Seric p = oldp; 11059382Seric break; 11069382Seric } 11079382Seric p += *p == '@' ? 1 : 2; 110858050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 11099382Seric p++; 11109382Seric } 11119382Seric /* at the end of one complete name */ 11129382Seric 11139382Seric /* strip off trailing white space */ 111458050Seric while (p >= name && 111558050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 11169382Seric p--; 11179382Seric if (++p == name) 11189382Seric continue; 11199382Seric savechar = *p; 11209382Seric *p = '\0'; 11219382Seric 11229382Seric /* translate the name to be relative */ 112359163Seric flags = RF_HEADERADDR|RF_ADDDOMAIN; 112459163Seric if (bitset(H_FROM, h->h_flags)) 112559163Seric flags |= RF_SENDERADDR; 112659163Seric stat = EX_OK; 112765870Seric name = remotename(name, mci->mci_mailer, flags, &stat, e); 11289382Seric if (*name == '\0') 11299382Seric { 11309382Seric *p = savechar; 11319382Seric continue; 11329382Seric } 11339382Seric 11349382Seric /* output the name with nice formatting */ 113554983Seric opos += strlen(name); 11369382Seric if (!firstone) 11379382Seric opos += 2; 113866004Seric if (opos > omax && !firstone) 11399382Seric { 114010178Seric (void) strcpy(obp, ",\n"); 114165870Seric putline(obuf, mci); 11429382Seric obp = obuf; 114366255Seric (void) strcpy(obp, " "); 114410161Seric opos = strlen(obp); 114510161Seric obp += opos; 114654983Seric opos += strlen(name); 11479382Seric } 11489382Seric else if (!firstone) 11499382Seric { 115066255Seric (void) strcpy(obp, ", "); 11519382Seric obp += 2; 11529382Seric } 11539382Seric 115454983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 115554983Seric *obp++ = c; 11569382Seric firstone = FALSE; 11579382Seric *p = savechar; 11589382Seric } 11599382Seric (void) strcpy(obp, "\n"); 116065870Seric putline(obuf, mci); 11619382Seric } 11629382Seric /* 116358170Seric ** COPYHEADER -- copy header list 11649382Seric ** 116558170Seric ** This routine is the equivalent of newstr for header lists 116658170Seric ** 11679382Seric ** Parameters: 116858170Seric ** header -- list of header structures to copy. 11699382Seric ** 11709382Seric ** Returns: 117158170Seric ** a copy of 'header'. 11729382Seric ** 11739382Seric ** Side Effects: 11749382Seric ** none. 11759382Seric */ 11769382Seric 117758170Seric HDR * 117858170Seric copyheader(header) 117958170Seric register HDR *header; 11809382Seric { 118158170Seric register HDR *newhdr; 118258170Seric HDR *ret; 118358170Seric register HDR **tail = &ret; 11849382Seric 118558170Seric while (header != NULL) 118658170Seric { 118758170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 118858170Seric STRUCTCOPY(*header, *newhdr); 118958170Seric *tail = newhdr; 119058170Seric tail = &newhdr->h_link; 119158170Seric header = header->h_link; 119258170Seric } 119358170Seric *tail = NULL; 119458170Seric 119558170Seric return ret; 11969382Seric } 1197