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*68558Seric static char sccsid[] = "@(#)headers.c 8.51 (Berkeley) 03/21/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; 4768528Seric char buf[MAXNAME + 1]; 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 110*68558Seric /* if this is an Errors-To: header keep track of it now */ 111*68558Seric if (UseErrorsTo && !def && bitset(H_ERRORSTO, hi->hi_flags)) 112*68558Seric (void) sendtolist(fvalue, NULLADDR, &e->e_errorqueue, 0, e); 113*68558Seric 1144091Seric /* if this means "end of header" quit now */ 1154091Seric if (bitset(H_EOH, hi->hi_flags)) 1164091Seric return (hi->hi_flags); 1174091Seric 11864653Seric /* 11964653Seric ** Drop explicit From: if same as what we would generate. 12064653Seric ** This is to make MH (which doesn't always give a full name) 12164653Seric ** insert the full name information in all circumstances. 12264653Seric */ 12364653Seric 12414784Seric p = "resent-from"; 12555012Seric if (!bitset(EF_RESENT, e->e_flags)) 12614784Seric p += 7; 12759579Seric if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0) 12811414Seric { 12963753Seric if (tTd(31, 2)) 13063753Seric { 13163753Seric printf("comparing header from (%s) against default (%s or %s)\n", 13263753Seric fvalue, e->e_from.q_paddr, e->e_from.q_user); 13363753Seric } 13455012Seric if (e->e_from.q_paddr != NULL && 13563753Seric (strcmp(fvalue, e->e_from.q_paddr) == 0 || 13663753Seric strcmp(fvalue, e->e_from.q_user) == 0)) 13711414Seric return (hi->hi_flags); 13864351Seric #ifdef MAYBENEXTRELEASE /* XXX UNTESTED XXX UNTESTED XXX UNTESTED XXX */ 13964351Seric #ifdef USERDB 14064351Seric else 14164351Seric { 14264351Seric auto ADDRESS a; 14364351Seric char *fancy; 14466123Seric bool oldSuprErrs = SuprErrs; 14564351Seric extern char *crackaddr(); 14664351Seric extern char *udbsender(); 14764351Seric 14864653Seric /* 14964653Seric ** Try doing USERDB rewriting even on fully commented 15064653Seric ** names; this saves the "comment" information (such 15164653Seric ** as full name) and rewrites the electronic part. 15266106Seric ** 15366106Seric ** XXX This code doesn't belong here -- parsing should 15466106Seric ** XXX not be done during collect() phase because 15566106Seric ** XXX error messages can confuse the SMTP phase. 15666123Seric ** XXX Setting SuprErrs is a crude hack around this 15766106Seric ** XXX problem. 15864653Seric */ 15964653Seric 16066106Seric if (OpMode == MD_SMTP || OpMode == MD_ARPAFTP) 16166123Seric SuprErrs = TRUE; 16264351Seric fancy = crackaddr(fvalue); 16364351Seric if (parseaddr(fvalue, &a, RF_COPYNONE, '\0', NULL, e) != NULL && 16467472Seric bitnset(M_CHECKUDB, a.q_mailer->m_flags) && 16564351Seric (p = udbsender(a.q_user)) != NULL) 16664351Seric { 16764351Seric char *oldg = macvalue('g', e); 16864351Seric 16964351Seric define('g', p, e); 17068529Seric expand(fancy, buf, sizeof buf, e); 17164351Seric define('g', oldg, e); 17264351Seric fvalue = buf; 17364351Seric } 17466123Seric SuprErrs = oldSuprErrs; 17564351Seric } 17664351Seric #endif 17764351Seric #endif 17811414Seric } 17911414Seric 18014784Seric /* delete default value for this header */ 18155012Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 18214784Seric { 18359579Seric if (strcasecmp(fname, h->h_field) == 0 && 18414784Seric bitset(H_DEFAULT, h->h_flags) && 18514784Seric !bitset(H_FORCE, h->h_flags)) 18614784Seric h->h_value = NULL; 18714784Seric } 18814784Seric 18913012Seric /* create a new node */ 19013012Seric h = (HDR *) xalloc(sizeof *h); 19113012Seric h->h_field = newstr(fname); 19264134Seric h->h_value = newstr(fvalue); 19313012Seric h->h_link = NULL; 19423117Seric bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts); 19513012Seric *hp = h; 1968066Seric h->h_flags = hi->hi_flags; 1974091Seric if (def) 1984091Seric h->h_flags |= H_DEFAULT; 1999059Seric if (cond) 2009059Seric h->h_flags |= H_CHECK; 2014091Seric 2025937Seric /* hack to see if this is a new format message */ 2038095Seric if (!def && bitset(H_RCPT|H_FROM, h->h_flags) && 20456795Seric (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL || 20556795Seric strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL)) 2068089Seric { 20755012Seric e->e_flags &= ~EF_OLDSTYLE; 2088089Seric } 2095937Seric 2104091Seric return (h->h_flags); 2114091Seric } 2124091Seric /* 2136980Seric ** ADDHEADER -- add a header entry to the end of the queue. 2146980Seric ** 2156980Seric ** This bypasses the special checking of chompheader. 2166980Seric ** 2176980Seric ** Parameters: 21859579Seric ** field -- the name of the header field. 21958789Seric ** value -- the value of the field. 22067546Seric ** hp -- an indirect pointer to the header structure list. 2216980Seric ** 2226980Seric ** Returns: 2236980Seric ** none. 2246980Seric ** 2256980Seric ** Side Effects: 2266980Seric ** adds the field on the list of headers for this envelope. 2276980Seric */ 2286980Seric 22967546Seric addheader(field, value, hdrlist) 2306980Seric char *field; 2316980Seric char *value; 23267546Seric HDR **hdrlist; 2336980Seric { 2346980Seric register HDR *h; 2356980Seric register struct hdrinfo *hi; 2366980Seric HDR **hp; 2376980Seric 2386980Seric /* find info struct */ 2396980Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 2406980Seric { 24159579Seric if (strcasecmp(field, hi->hi_field) == 0) 2426980Seric break; 2436980Seric } 2446980Seric 2456980Seric /* find current place in list -- keep back pointer? */ 24667546Seric for (hp = hdrlist; (h = *hp) != NULL; hp = &h->h_link) 2476980Seric { 24859579Seric if (strcasecmp(field, h->h_field) == 0) 2496980Seric break; 2506980Seric } 2516980Seric 2526980Seric /* allocate space for new header */ 2536980Seric h = (HDR *) xalloc(sizeof *h); 2546980Seric h->h_field = field; 2556980Seric h->h_value = newstr(value); 2567368Seric h->h_link = *hp; 2576980Seric h->h_flags = hi->hi_flags | H_DEFAULT; 25810689Seric clrbitmap(h->h_mflags); 2596980Seric *hp = h; 2606980Seric } 2616980Seric /* 2624091Seric ** HVALUE -- return value of a header. 2634091Seric ** 2644091Seric ** Only "real" fields (i.e., ones that have not been supplied 2654091Seric ** as a default) are used. 2664091Seric ** 2674091Seric ** Parameters: 2684091Seric ** field -- the field name. 26967546Seric ** header -- the header list. 2704091Seric ** 2714091Seric ** Returns: 2724091Seric ** pointer to the value part. 2734091Seric ** NULL if not found. 2744091Seric ** 2754091Seric ** Side Effects: 2769382Seric ** none. 2774091Seric */ 2784091Seric 2794091Seric char * 28067546Seric hvalue(field, header) 2814091Seric char *field; 28267546Seric HDR *header; 2834091Seric { 2844091Seric register HDR *h; 2854091Seric 28667546Seric for (h = header; h != NULL; h = h->h_link) 2874091Seric { 28859579Seric if (!bitset(H_DEFAULT, h->h_flags) && 28959579Seric strcasecmp(h->h_field, field) == 0) 2904091Seric return (h->h_value); 2914091Seric } 2924091Seric return (NULL); 2934091Seric } 2944091Seric /* 2954091Seric ** ISHEADER -- predicate telling if argument is a header. 2964091Seric ** 2974319Seric ** A line is a header if it has a single word followed by 2984319Seric ** optional white space followed by a colon. 2994319Seric ** 3004091Seric ** Parameters: 3014091Seric ** s -- string to check for possible headerness. 3024091Seric ** 3034091Seric ** Returns: 3044091Seric ** TRUE if s is a header. 3054091Seric ** FALSE otherwise. 3064091Seric ** 3074091Seric ** Side Effects: 3084091Seric ** none. 3094091Seric */ 3104091Seric 3114091Seric bool 3124091Seric isheader(s) 3134091Seric register char *s; 3144091Seric { 3159382Seric while (*s > ' ' && *s != ':' && *s != '\0') 3164091Seric s++; 3179382Seric 3189382Seric /* following technically violates RFC822 */ 31958050Seric while (isascii(*s) && isspace(*s)) 3204091Seric s++; 3219382Seric 3224091Seric return (*s == ':'); 3234091Seric } 3245919Seric /* 3257783Seric ** EATHEADER -- run through the stored header and extract info. 3267783Seric ** 3277783Seric ** Parameters: 3289382Seric ** e -- the envelope to process. 32958929Seric ** full -- if set, do full processing (e.g., compute 33058929Seric ** message priority). 3317783Seric ** 3327783Seric ** Returns: 3337783Seric ** none. 3347783Seric ** 3357783Seric ** Side Effects: 3367783Seric ** Sets a bunch of global variables from information 3379382Seric ** in the collected header. 3389382Seric ** Aborts the message if the hop count is exceeded. 3397783Seric */ 3407783Seric 34158929Seric eatheader(e, full) 3429382Seric register ENVELOPE *e; 34358929Seric bool full; 3447783Seric { 3457783Seric register HDR *h; 3467783Seric register char *p; 3479382Seric int hopcnt = 0; 34857208Seric char *msgid; 34958688Seric char buf[MAXLINE]; 3507783Seric 35158688Seric /* 35258688Seric ** Set up macros for possible expansion in headers. 35358688Seric */ 35458688Seric 35558704Seric define('f', e->e_sender, e); 35658704Seric define('g', e->e_sender, e); 35764148Seric if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0') 35864148Seric define('u', e->e_origrcpt, e); 35964148Seric else 36064148Seric define('u', NULL, e); 36158688Seric 36265052Seric /* full name of from person */ 36367546Seric p = hvalue("full-name", e->e_header); 36465052Seric if (p != NULL) 36565052Seric define('x', p, e); 36665052Seric 3679382Seric if (tTd(32, 1)) 3689382Seric printf("----- collected header -----\n"); 36957208Seric msgid = "<none>"; 3709382Seric for (h = e->e_header; h != NULL; h = h->h_link) 3717783Seric { 37264156Seric if (h->h_value == NULL) 37364156Seric { 37464156Seric if (tTd(32, 1)) 37564156Seric printf("%s: <NULL>\n", h->h_field); 37664156Seric continue; 37764156Seric } 37864156Seric 37958688Seric /* do early binding */ 38064156Seric if (bitset(H_DEFAULT, h->h_flags)) 38158688Seric { 38268529Seric expand(h->h_value, buf, sizeof buf, e); 38358688Seric if (buf[0] != '\0') 38458688Seric { 38558688Seric h->h_value = newstr(buf); 38658688Seric h->h_flags &= ~H_DEFAULT; 38758688Seric } 38858688Seric } 38958688Seric 3909382Seric if (tTd(32, 1)) 39165152Seric { 39265152Seric printf("%s: ", h->h_field); 39365152Seric xputs(h->h_value); 39465152Seric printf("\n"); 39565152Seric } 39657359Seric 39711414Seric /* count the number of times it has been processed */ 3989382Seric if (bitset(H_TRACE, h->h_flags)) 3999382Seric hopcnt++; 40011414Seric 40111414Seric /* send to this person if we so desire */ 40211414Seric if (GrabTo && bitset(H_RCPT, h->h_flags) && 40311414Seric !bitset(H_DEFAULT, h->h_flags) && 40455012Seric (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags))) 40511414Seric { 40663839Seric int saveflags = e->e_flags; 40763839Seric 40864283Seric (void) sendtolist(h->h_value, NULLADDR, 40967982Seric &e->e_sendqueue, 0, e); 41063839Seric 41163839Seric /* delete fatal errors generated by this address */ 41264148Seric if (!GrabTo && !bitset(EF_FATALERRS, saveflags)) 41363839Seric e->e_flags &= ~EF_FATALERRS; 41411414Seric } 41511414Seric 41657208Seric /* save the message-id for logging */ 41764156Seric if (full && strcasecmp(h->h_field, "message-id") == 0) 41811290Seric { 41957208Seric msgid = h->h_value; 42059859Seric while (isascii(*msgid) && isspace(*msgid)) 42159859Seric msgid++; 42211290Seric } 42357359Seric 42457359Seric /* see if this is a return-receipt header */ 42557359Seric if (bitset(H_RECEIPTTO, h->h_flags)) 42657359Seric e->e_receiptto = h->h_value; 4279382Seric } 4289382Seric if (tTd(32, 1)) 4297783Seric printf("----------------------------\n"); 4307783Seric 43158145Seric /* if we are just verifying (that is, sendmail -t -bv), drop out now */ 43258145Seric if (OpMode == MD_VERIFY) 43358145Seric return; 43458145Seric 4359382Seric /* store hop count */ 4369382Seric if (hopcnt > e->e_hopcount) 4379382Seric e->e_hopcount = hopcnt; 4389382Seric 4397783Seric /* message priority */ 44067546Seric p = hvalue("precedence", e->e_header); 4419382Seric if (p != NULL) 4429382Seric e->e_class = priencode(p); 44358929Seric if (full) 44467730Seric { 44525013Seric e->e_msgpriority = e->e_msgsize 44624981Seric - e->e_class * WkClassFact 44724981Seric + e->e_nrcpts * WkRecipFact; 44867730Seric if (e->e_class < 0) 44967730Seric e->e_timeoutclass = TOC_NONURGENT; 45067730Seric else if (e->e_class > 0) 45167730Seric e->e_timeoutclass = TOC_URGENT; 45267730Seric } 4537783Seric 45467730Seric /* message timeout priority */ 45567730Seric p = hvalue("priority", e->e_header); 45667730Seric if (full && p != NULL) 45767730Seric { 45867730Seric /* (this should be in the configuration file) */ 45967730Seric if (strcasecmp(p, "urgent")) 46067730Seric e->e_timeoutclass = TOC_URGENT; 46167730Seric else if (strcasecmp(p, "normal")) 46267730Seric e->e_timeoutclass = TOC_NORMAL; 46367730Seric else if (strcasecmp(p, "non-urgent")) 46467730Seric e->e_timeoutclass = TOC_NONURGENT; 46567730Seric } 46667730Seric 4677783Seric /* date message originated */ 46867546Seric p = hvalue("posted-date", e->e_header); 4697783Seric if (p == NULL) 47067546Seric p = hvalue("date", e->e_header); 4717783Seric if (p != NULL) 4729382Seric define('a', p, e); 47311290Seric 47411290Seric /* 47565983Seric ** From person in antiquated ARPANET mode 47665983Seric ** required by UK Grey Book e-mail gateways (sigh) 47765983Seric */ 47865983Seric 47965983Seric if (OpMode == MD_ARPAFTP) 48065983Seric { 48165983Seric register struct hdrinfo *hi; 48265983Seric 48365983Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 48465983Seric { 48565983Seric if (bitset(H_FROM, hi->hi_flags) && 48665983Seric (!bitset(H_RESENT, hi->hi_flags) || 48765983Seric bitset(EF_RESENT, e->e_flags)) && 48867546Seric (p = hvalue(hi->hi_field, e->e_header)) != NULL) 48965983Seric break; 49065983Seric } 49165983Seric if (hi->hi_field != NULL) 49265983Seric { 49365983Seric if (tTd(32, 2)) 49465983Seric printf("eatheader: setsender(*%s == %s)\n", 49565983Seric hi->hi_field, p); 49665983Seric setsender(p, e, NULL, TRUE); 49765983Seric } 49865983Seric } 49965983Seric 50065983Seric /* 50111290Seric ** Log collection information. 50211290Seric */ 50311290Seric 50411290Seric # ifdef LOG 50568036Seric if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4) 50665089Seric logsender(e, msgid); 50765089Seric # endif /* LOG */ 50865089Seric e->e_flags &= ~EF_LOGSENDER; 50965089Seric } 51065089Seric /* 51165089Seric ** LOGSENDER -- log sender information 51265089Seric ** 51365089Seric ** Parameters: 51465089Seric ** e -- the envelope to log 51565089Seric ** msgid -- the message id 51665089Seric ** 51765089Seric ** Returns: 51865089Seric ** none 51965089Seric */ 52065089Seric 52165089Seric logsender(e, msgid) 52265089Seric register ENVELOPE *e; 52365089Seric char *msgid; 52465089Seric { 52566748Seric # ifdef LOG 52665089Seric char *name; 52765089Seric register char *sbp; 52865089Seric register char *p; 52967901Seric int l; 53068528Seric char hbuf[MAXNAME + 1]; 53168528Seric char sbuf[MAXLINE + 1]; 53268528Seric char mbuf[MAXNAME + 1]; 53365089Seric 53467901Seric /* don't allow newlines in the message-id */ 53567901Seric if (msgid != NULL) 53667901Seric { 53767901Seric l = strlen(msgid); 53867901Seric if (l > sizeof mbuf - 1) 53967901Seric l = sizeof mbuf - 1; 54067901Seric bcopy(msgid, mbuf, l); 54167901Seric mbuf[l] = '\0'; 54267901Seric p = mbuf; 54367901Seric while ((p = strchr(p, '\n')) != NULL) 54467901Seric *p++ = ' '; 54567901Seric } 54667901Seric 54765089Seric if (bitset(EF_RESPONSE, e->e_flags)) 54865089Seric name = "[RESPONSE]"; 54965089Seric else if ((name = macvalue('_', e)) != NULL) 55065089Seric ; 55166003Seric else if (RealHostName == NULL) 55266003Seric name = "localhost"; 55365089Seric else if (RealHostName[0] == '[') 55465089Seric name = RealHostName; 55565089Seric else 55611290Seric { 55765089Seric name = hbuf; 55865089Seric (void) sprintf(hbuf, "%.80s", RealHostName); 55965089Seric if (RealHostAddr.sa.sa_family != 0) 56057359Seric { 56165089Seric p = &hbuf[strlen(hbuf)]; 56265089Seric (void) sprintf(p, " (%s)", 56365089Seric anynet_ntoa(&RealHostAddr)); 56457359Seric } 56565089Seric } 56657359Seric 56765089Seric /* some versions of syslog only take 5 printf args */ 56865059Seric # if (SYSLOG_BUFSIZE) >= 256 56965089Seric sbp = sbuf; 57065089Seric sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d", 57167788Seric e->e_from.q_paddr == NULL ? "<NONE>" : e->e_from.q_paddr, 57267788Seric e->e_msgsize, e->e_class, e->e_msgpriority, e->e_nrcpts); 57365089Seric sbp += strlen(sbp); 57465089Seric if (msgid != NULL) 57565089Seric { 57667901Seric sprintf(sbp, ", msgid=%.100s", mbuf); 57760575Seric sbp += strlen(sbp); 57865089Seric } 57965089Seric if (e->e_bodytype != NULL) 58065089Seric { 58165089Seric (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype); 58265089Seric sbp += strlen(sbp); 58365089Seric } 58465089Seric p = macvalue('r', e); 58565089Seric if (p != NULL) 58665089Seric (void) sprintf(sbp, ", proto=%.20s", p); 58765089Seric syslog(LOG_INFO, "%s: %s, relay=%s", 58865089Seric e->e_id, sbuf, name); 58965059Seric 59065059Seric # else /* short syslog buffer */ 59165059Seric 59265089Seric syslog(LOG_INFO, "%s: from=%s", 59367788Seric e->e_id, e->e_from.q_paddr == NULL ? "<NONE>" : 59467788Seric shortenstring(e->e_from.q_paddr, 83)); 59565089Seric syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d", 59665089Seric e->e_id, e->e_msgsize, e->e_class, 59765089Seric e->e_msgpriority, e->e_nrcpts); 59865089Seric if (msgid != NULL) 59967901Seric syslog(LOG_INFO, "%s: msgid=%s", e->e_id, mbuf); 60065650Seric sbp = sbuf; 60165650Seric sprintf(sbp, "%s:", e->e_id); 60265650Seric sbp += strlen(sbp); 60365089Seric if (e->e_bodytype != NULL) 60465650Seric { 60565650Seric sprintf(sbp, " bodytype=%s,", e->e_bodytype); 60665650Seric sbp += strlen(sbp); 60765650Seric } 60865089Seric p = macvalue('r', e); 60965089Seric if (p != NULL) 61065650Seric { 61165731Seric sprintf(sbp, " proto=%s,", p); 61265650Seric sbp += strlen(sbp); 61365650Seric } 61465650Seric syslog(LOG_INFO, "%s relay=%s", sbuf, name); 61565059Seric # endif 61666748Seric # endif 6177783Seric } 6187783Seric /* 6197783Seric ** PRIENCODE -- encode external priority names into internal values. 6207783Seric ** 6217783Seric ** Parameters: 6227783Seric ** p -- priority in ascii. 6237783Seric ** 6247783Seric ** Returns: 6257783Seric ** priority as a numeric level. 6267783Seric ** 6277783Seric ** Side Effects: 6287783Seric ** none. 6297783Seric */ 6307783Seric 6317783Seric priencode(p) 6327783Seric char *p; 6337783Seric { 6348253Seric register int i; 6357783Seric 6368253Seric for (i = 0; i < NumPriorities; i++) 6377783Seric { 63833725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 6398253Seric return (Priorities[i].pri_val); 6407783Seric } 6418253Seric 6428253Seric /* unknown priority */ 6438253Seric return (0); 6447783Seric } 6457783Seric /* 6467890Seric ** CRACKADDR -- parse an address and turn it into a macro 6477783Seric ** 6487783Seric ** This doesn't actually parse the address -- it just extracts 6497783Seric ** it and replaces it with "$g". The parse is totally ad hoc 6507783Seric ** and isn't even guaranteed to leave something syntactically 6517783Seric ** identical to what it started with. However, it does leave 6527783Seric ** something semantically identical. 6537783Seric ** 65451379Seric ** This algorithm has been cleaned up to handle a wider range 65551379Seric ** of cases -- notably quoted and backslash escaped strings. 65651379Seric ** This modification makes it substantially better at preserving 65751379Seric ** the original syntax. 6587783Seric ** 6597783Seric ** Parameters: 6607890Seric ** addr -- the address to be cracked. 6617783Seric ** 6627783Seric ** Returns: 6637783Seric ** a pointer to the new version. 6647783Seric ** 6657783Seric ** Side Effects: 6667890Seric ** none. 6677783Seric ** 6687783Seric ** Warning: 6697783Seric ** The return value is saved in local storage and should 6707783Seric ** be copied if it is to be reused. 6717783Seric */ 6727783Seric 6737783Seric char * 6747890Seric crackaddr(addr) 6757890Seric register char *addr; 6767783Seric { 6777783Seric register char *p; 67851379Seric register char c; 67951379Seric int cmtlev; 68056764Seric int realcmtlev; 68156764Seric int anglelev, realanglelev; 68251379Seric int copylev; 68351379Seric bool qmode; 68456764Seric bool realqmode; 68556764Seric bool skipping; 68651379Seric bool putgmac = FALSE; 68756735Seric bool quoteit = FALSE; 68864148Seric bool gotangle = FALSE; 68951379Seric register char *bp; 69056764Seric char *buflim; 69168528Seric static char buf[MAXNAME + 1]; 6927783Seric 6937783Seric if (tTd(33, 1)) 6947890Seric printf("crackaddr(%s)\n", addr); 6957783Seric 6968082Seric /* strip leading spaces */ 69758050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 6988082Seric addr++; 6998082Seric 7007783Seric /* 70151379Seric ** Start by assuming we have no angle brackets. This will be 70251379Seric ** adjusted later if we find them. 7037783Seric */ 7047783Seric 70551379Seric bp = buf; 70656764Seric buflim = &buf[sizeof buf - 5]; 70751379Seric p = addr; 70856764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 70956764Seric qmode = realqmode = FALSE; 71051379Seric 71151379Seric while ((c = *p++) != '\0') 7127783Seric { 71356764Seric /* 71456764Seric ** If the buffer is overful, go into a special "skipping" 71556764Seric ** mode that tries to keep legal syntax but doesn't actually 71656764Seric ** output things. 71756764Seric */ 7187783Seric 71956764Seric skipping = bp >= buflim; 72056735Seric 72156764Seric if (copylev > 0 && !skipping) 72256764Seric *bp++ = c; 72356735Seric 72451379Seric /* check for backslash escapes */ 72551379Seric if (c == '\\') 7267783Seric { 72758890Seric /* arrange to quote the address */ 72858890Seric if (cmtlev <= 0 && !qmode) 72958890Seric quoteit = TRUE; 73058890Seric 73151379Seric if ((c = *p++) == '\0') 7327783Seric { 73351379Seric /* too far */ 73451379Seric p--; 73551379Seric goto putg; 7367783Seric } 73756764Seric if (copylev > 0 && !skipping) 73851379Seric *bp++ = c; 73951379Seric goto putg; 7407783Seric } 7417783Seric 74251379Seric /* check for quoted strings */ 74364967Seric if (c == '"' && cmtlev <= 0) 7447783Seric { 74551379Seric qmode = !qmode; 74656764Seric if (copylev > 0 && !skipping) 74756764Seric realqmode = !realqmode; 74851379Seric continue; 7497783Seric } 75051379Seric if (qmode) 75151379Seric goto putg; 7527783Seric 75351379Seric /* check for comments */ 75451379Seric if (c == '(') 7557783Seric { 75651379Seric cmtlev++; 75756764Seric 75856764Seric /* allow space for closing paren */ 75956764Seric if (!skipping) 76056764Seric { 76156764Seric buflim--; 76256764Seric realcmtlev++; 76356764Seric if (copylev++ <= 0) 76456764Seric { 76556764Seric *bp++ = ' '; 76656764Seric *bp++ = c; 76756764Seric } 76856764Seric } 76951379Seric } 77051379Seric if (cmtlev > 0) 77151379Seric { 77251379Seric if (c == ')') 7737783Seric { 77451379Seric cmtlev--; 77551379Seric copylev--; 77656764Seric if (!skipping) 77756764Seric { 77856764Seric realcmtlev--; 77956764Seric buflim++; 78056764Seric } 7817783Seric } 7827783Seric continue; 7837783Seric } 78456764Seric else if (c == ')') 78556764Seric { 78656764Seric /* syntax error: unmatched ) */ 78765544Seric if (copylev > 0 && !skipping) 78856764Seric bp--; 78956764Seric } 7907783Seric 79156764Seric /* check for characters that may have to be quoted */ 79264148Seric if (strchr(".'@,;:\\()[]", c) != NULL) 79356764Seric { 79456764Seric /* 79556764Seric ** If these occur as the phrase part of a <> 79656764Seric ** construct, but are not inside of () or already 79756764Seric ** quoted, they will have to be quoted. Note that 79856764Seric ** now (but don't actually do the quoting). 79956764Seric */ 80056764Seric 80156764Seric if (cmtlev <= 0 && !qmode) 80256764Seric quoteit = TRUE; 80356764Seric } 80456764Seric 80551379Seric /* check for angle brackets */ 80651379Seric if (c == '<') 80751379Seric { 80856735Seric register char *q; 80956735Seric 81064148Seric /* assume first of two angles is bogus */ 81164148Seric if (gotangle) 81264148Seric quoteit = TRUE; 81364148Seric gotangle = TRUE; 81464148Seric 81551379Seric /* oops -- have to change our mind */ 81664752Seric anglelev = 1; 81756764Seric if (!skipping) 81864752Seric realanglelev = 1; 81956764Seric 82056735Seric bp = buf; 82156735Seric if (quoteit) 82256735Seric { 82356735Seric *bp++ = '"'; 82456735Seric 82556735Seric /* back up over the '<' and any spaces */ 82656735Seric --p; 82758050Seric while (isascii(*--p) && isspace(*p)) 82856735Seric continue; 82956735Seric p++; 83056735Seric } 83156735Seric for (q = addr; q < p; ) 83256735Seric { 83356735Seric c = *q++; 83456764Seric if (bp < buflim) 83556764Seric { 83656764Seric if (quoteit && c == '"') 83756764Seric *bp++ = '\\'; 83856764Seric *bp++ = c; 83956764Seric } 84056735Seric } 84156735Seric if (quoteit) 84256735Seric { 84364148Seric if (bp == &buf[1]) 84464148Seric bp--; 84564148Seric else 84664148Seric *bp++ = '"'; 84756764Seric while ((c = *p++) != '<') 84856764Seric { 84956764Seric if (bp < buflim) 85056764Seric *bp++ = c; 85156764Seric } 85256764Seric *bp++ = c; 85356735Seric } 85451379Seric copylev = 0; 85556735Seric putgmac = quoteit = FALSE; 85651379Seric continue; 85751379Seric } 8587783Seric 85951379Seric if (c == '>') 8607783Seric { 86156764Seric if (anglelev > 0) 86256764Seric { 86356764Seric anglelev--; 86456764Seric if (!skipping) 86556764Seric { 86656764Seric realanglelev--; 86756764Seric buflim++; 86856764Seric } 86956764Seric } 87056764Seric else if (!skipping) 87156764Seric { 87256764Seric /* syntax error: unmatched > */ 87356764Seric if (copylev > 0) 87456764Seric bp--; 87564752Seric quoteit = TRUE; 87656764Seric continue; 87756764Seric } 87851379Seric if (copylev++ <= 0) 87951379Seric *bp++ = c; 88051379Seric continue; 8817783Seric } 88251379Seric 88351379Seric /* must be a real address character */ 88451379Seric putg: 88551379Seric if (copylev <= 0 && !putgmac) 88651379Seric { 88758050Seric *bp++ = MACROEXPAND; 88851379Seric *bp++ = 'g'; 88951379Seric putgmac = TRUE; 89051379Seric } 8917783Seric } 8927783Seric 89356764Seric /* repair any syntactic damage */ 89456764Seric if (realqmode) 89556764Seric *bp++ = '"'; 89656764Seric while (realcmtlev-- > 0) 89756764Seric *bp++ = ')'; 89856764Seric while (realanglelev-- > 0) 89956764Seric *bp++ = '>'; 90051379Seric *bp++ = '\0'; 9017783Seric 9027783Seric if (tTd(33, 1)) 9037944Seric printf("crackaddr=>`%s'\n", buf); 9047783Seric 9057783Seric return (buf); 9067783Seric } 9079382Seric /* 9089382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 9099382Seric ** 9109382Seric ** Parameters: 91165870Seric ** mci -- the connection information. 91267546Seric ** h -- the header to put. 9139382Seric ** e -- envelope to use. 9149382Seric ** 9159382Seric ** Returns: 9169382Seric ** none. 9179382Seric ** 9189382Seric ** Side Effects: 9199382Seric ** none. 9209382Seric */ 9219382Seric 92257589Seric /* 92357589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 92457589Seric */ 92557589Seric #ifndef MAX 92657589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 92757589Seric #endif 92857589Seric 92968228Seric putheader(mci, h, e) 93065870Seric register MCI *mci; 93167546Seric register HDR *h; 9329382Seric register ENVELOPE *e; 9339382Seric { 93457135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 93557135Seric char obuf[MAXLINE]; 9369382Seric 93759882Seric if (tTd(34, 1)) 93865870Seric printf("--- putheader, mailer = %s ---\n", 93965870Seric mci->mci_mailer->m_name); 94059882Seric 94167546Seric mci->mci_flags |= MCIF_INHEADER; 94267546Seric for (; h != NULL; h = h->h_link) 9439382Seric { 9449382Seric register char *p; 94510689Seric extern bool bitintersect(); 9469382Seric 94759882Seric if (tTd(34, 11)) 94859882Seric { 94959882Seric printf(" %s: ", h->h_field); 95059882Seric xputs(h->h_value); 95159882Seric } 95259882Seric 9539382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 95465870Seric !bitintersect(h->h_mflags, mci->mci_mailer->m_flags)) 95559882Seric { 95659882Seric if (tTd(34, 11)) 95759882Seric printf(" (skipped)\n"); 9589382Seric continue; 95959882Seric } 9609382Seric 96111414Seric /* handle Resent-... headers specially */ 96211414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 96359882Seric { 96459882Seric if (tTd(34, 11)) 96559882Seric printf(" (skipped (resent))\n"); 96611414Seric continue; 96759882Seric } 96811414Seric 96966784Seric /* suppress return receipts if requested */ 97066784Seric if (bitset(H_RECEIPTTO, h->h_flags) && 97166784Seric bitset(EF_NORECEIPT, e->e_flags)) 97266784Seric { 97366784Seric if (tTd(34, 11)) 97466784Seric printf(" (skipped (receipt))\n"); 97566784Seric continue; 97666784Seric } 97766784Seric 97867694Seric /* suppress Content-Transfer-Encoding: if we are MIMEing */ 97967694Seric if (bitset(H_CTE, h->h_flags) && 98068228Seric bitset(MCIF_CVT8TO7, mci->mci_flags)) 98167694Seric { 98267694Seric if (tTd(34, 11)) 98367694Seric printf(" (skipped (content-transfer-encoding))\n"); 98467694Seric continue; 98567694Seric } 98667694Seric 98765152Seric /* macro expand value if generated internally */ 9889382Seric p = h->h_value; 9899382Seric if (bitset(H_DEFAULT, h->h_flags)) 9909382Seric { 99168529Seric expand(p, buf, sizeof buf, e); 9929382Seric p = buf; 9939382Seric if (p == NULL || *p == '\0') 99465152Seric { 99565152Seric if (tTd(34, 11)) 99665152Seric printf(" (skipped -- null value)\n"); 9979382Seric continue; 99865152Seric } 9999382Seric } 10009382Seric 100165152Seric if (tTd(34, 11)) 100265152Seric printf("\n"); 100365152Seric 100468449Seric if (bitset(H_STRIPVAL, h->h_flags)) 10059382Seric { 100668449Seric /* empty field */ 100768449Seric (void) sprintf(obuf, "%s:", h->h_field); 100868449Seric putline(obuf, mci); 100968449Seric } 101068449Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 101168449Seric { 10129382Seric /* address field */ 10139382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 10149382Seric 10159382Seric if (bitset(H_FROM, h->h_flags)) 10169382Seric oldstyle = FALSE; 101765870Seric commaize(h, p, oldstyle, mci, e); 10189382Seric } 10199382Seric else 10209382Seric { 10219382Seric /* vanilla header line */ 102212159Seric register char *nlp; 102312159Seric 102459579Seric (void) sprintf(obuf, "%s: ", h->h_field); 102556795Seric while ((nlp = strchr(p, '\n')) != NULL) 102612159Seric { 102712159Seric *nlp = '\0'; 102812159Seric (void) strcat(obuf, p); 102912159Seric *nlp = '\n'; 103065870Seric putline(obuf, mci); 103112159Seric p = ++nlp; 103212161Seric obuf[0] = '\0'; 103312159Seric } 103412159Seric (void) strcat(obuf, p); 103565870Seric putline(obuf, mci); 10369382Seric } 10379382Seric } 103867887Seric 103967887Seric /* 104067887Seric ** If we are converting this to a MIME message, add the 104167889Seric ** MIME headers. 104267887Seric */ 104367887Seric 104467887Seric if (bitset(MM_MIME8BIT, MimeMode) && 104567887Seric bitset(EF_HAS8BIT, e->e_flags) && 104667887Seric !bitnset(M_8BITS, mci->mci_mailer->m_flags) && 104767889Seric !bitset(MCIF_CVT8TO7, mci->mci_flags)) 104867887Seric { 104967889Seric if (hvalue("MIME-Version", e->e_header) == NULL) 105067889Seric putline("MIME-Version: 1.0", mci); 105167889Seric if (hvalue("Content-Type", e->e_header) == NULL) 105267889Seric { 105367889Seric sprintf(obuf, "Content-Type: text/plain; charset=%s", 105467896Seric defcharset(e)); 105567889Seric putline(obuf, mci); 105667889Seric } 105767889Seric if (hvalue("Content-Transfer-Encoding", e->e_header) == NULL) 105867889Seric putline("Content-Transfer-Encoding: 8bit", mci); 105967887Seric } 10609382Seric } 10619382Seric /* 10629382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 10639382Seric ** 10649382Seric ** Parameters: 10659382Seric ** h -- the header field to output. 10669382Seric ** p -- the value to put in it. 10679382Seric ** oldstyle -- TRUE if this is an old style header. 106865870Seric ** mci -- the connection information. 106955012Seric ** e -- the envelope containing the message. 10709382Seric ** 10719382Seric ** Returns: 10729382Seric ** none. 10739382Seric ** 10749382Seric ** Side Effects: 10759382Seric ** outputs "p" to file "fp". 10769382Seric */ 10779382Seric 107865870Seric void 107965870Seric commaize(h, p, oldstyle, mci, e) 10809382Seric register HDR *h; 10819382Seric register char *p; 10829382Seric bool oldstyle; 108365870Seric register MCI *mci; 108455012Seric register ENVELOPE *e; 10859382Seric { 10869382Seric register char *obp; 10879382Seric int opos; 108866004Seric int omax; 10899382Seric bool firstone = TRUE; 109011157Seric char obuf[MAXLINE + 3]; 10919382Seric 10929382Seric /* 10939382Seric ** Output the address list translated by the 10949382Seric ** mailer and with commas. 10959382Seric */ 10969382Seric 10979382Seric if (tTd(14, 2)) 10989382Seric printf("commaize(%s: %s)\n", h->h_field, p); 10999382Seric 11009382Seric obp = obuf; 110159579Seric (void) sprintf(obp, "%s: ", h->h_field); 11029382Seric opos = strlen(h->h_field) + 2; 11039382Seric obp += opos; 110466254Seric omax = mci->mci_mailer->m_linelimit - 2; 110566254Seric if (omax < 0 || omax > 78) 110666254Seric omax = 78; 11079382Seric 11089382Seric /* 11099382Seric ** Run through the list of values. 11109382Seric */ 11119382Seric 11129382Seric while (*p != '\0') 11139382Seric { 11149382Seric register char *name; 111554983Seric register int c; 11169382Seric char savechar; 111759163Seric int flags; 111859163Seric auto int stat; 11199382Seric 11209382Seric /* 11219382Seric ** Find the end of the name. New style names 11229382Seric ** end with a comma, old style names end with 11239382Seric ** a space character. However, spaces do not 11249382Seric ** necessarily delimit an old-style name -- at 11259382Seric ** signs mean keep going. 11269382Seric */ 11279382Seric 11289382Seric /* find end of name */ 112958050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 11309382Seric p++; 11319382Seric name = p; 11329382Seric for (;;) 11339382Seric { 113458333Seric auto char *oldp; 113516909Seric char pvpbuf[PSBUFSIZE]; 11369382Seric 113765066Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, 113865066Seric sizeof pvpbuf, &oldp); 113958333Seric p = oldp; 11409382Seric 11419382Seric /* look to see if we have an at sign */ 114258050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 11439382Seric p++; 11449382Seric 114558170Seric if (*p != '@') 11469382Seric { 11479382Seric p = oldp; 11489382Seric break; 11499382Seric } 11509382Seric p += *p == '@' ? 1 : 2; 115158050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 11529382Seric p++; 11539382Seric } 11549382Seric /* at the end of one complete name */ 11559382Seric 11569382Seric /* strip off trailing white space */ 115758050Seric while (p >= name && 115858050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 11599382Seric p--; 11609382Seric if (++p == name) 11619382Seric continue; 11629382Seric savechar = *p; 11639382Seric *p = '\0'; 11649382Seric 11659382Seric /* translate the name to be relative */ 116659163Seric flags = RF_HEADERADDR|RF_ADDDOMAIN; 116759163Seric if (bitset(H_FROM, h->h_flags)) 116859163Seric flags |= RF_SENDERADDR; 116968522Seric #ifdef USERDB 117068522Seric else if (e->e_from.q_mailer != NULL && 117168522Seric bitnset(M_UDBRECIPIENT, e->e_from.q_mailer->m_flags)) 117268522Seric { 117368522Seric extern char *udbsender(); 117468522Seric 117568522Seric name = udbsender(name); 117668522Seric } 117768522Seric #endif 117859163Seric stat = EX_OK; 117965870Seric name = remotename(name, mci->mci_mailer, flags, &stat, e); 11809382Seric if (*name == '\0') 11819382Seric { 11829382Seric *p = savechar; 11839382Seric continue; 11849382Seric } 11859382Seric 11869382Seric /* output the name with nice formatting */ 118754983Seric opos += strlen(name); 11889382Seric if (!firstone) 11899382Seric opos += 2; 119066004Seric if (opos > omax && !firstone) 11919382Seric { 119210178Seric (void) strcpy(obp, ",\n"); 119365870Seric putline(obuf, mci); 11949382Seric obp = obuf; 119566255Seric (void) strcpy(obp, " "); 119610161Seric opos = strlen(obp); 119710161Seric obp += opos; 119854983Seric opos += strlen(name); 11999382Seric } 12009382Seric else if (!firstone) 12019382Seric { 120266255Seric (void) strcpy(obp, ", "); 12039382Seric obp += 2; 12049382Seric } 12059382Seric 120654983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 120754983Seric *obp++ = c; 12089382Seric firstone = FALSE; 12099382Seric *p = savechar; 12109382Seric } 12119382Seric (void) strcpy(obp, "\n"); 121265870Seric putline(obuf, mci); 12139382Seric } 12149382Seric /* 121558170Seric ** COPYHEADER -- copy header list 12169382Seric ** 121758170Seric ** This routine is the equivalent of newstr for header lists 121858170Seric ** 12199382Seric ** Parameters: 122058170Seric ** header -- list of header structures to copy. 12219382Seric ** 12229382Seric ** Returns: 122358170Seric ** a copy of 'header'. 12249382Seric ** 12259382Seric ** Side Effects: 12269382Seric ** none. 12279382Seric */ 12289382Seric 122958170Seric HDR * 123058170Seric copyheader(header) 123158170Seric register HDR *header; 12329382Seric { 123358170Seric register HDR *newhdr; 123458170Seric HDR *ret; 123558170Seric register HDR **tail = &ret; 12369382Seric 123758170Seric while (header != NULL) 123858170Seric { 123958170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 124058170Seric STRUCTCOPY(*header, *newhdr); 124158170Seric *tail = newhdr; 124258170Seric tail = &newhdr->h_link; 124358170Seric header = header->h_link; 124458170Seric } 124558170Seric *tail = NULL; 124658170Seric 124758170Seric return ret; 12489382Seric } 1249