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*67472Seric static char sccsid[] = "@(#)headers.c 8.33 (Berkeley) 07/03/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 10364283Seric printf("header match, hi_flags=%o\n", hi->hi_flags); 10464283Seric } 10564283Seric 10611414Seric /* see if this is a resent message */ 10711930Seric if (!def && bitset(H_RESENT, hi->hi_flags)) 10855012Seric e->e_flags |= EF_RESENT; 10911414Seric 1104091Seric /* if this means "end of header" quit now */ 1114091Seric if (bitset(H_EOH, hi->hi_flags)) 1124091Seric return (hi->hi_flags); 1134091Seric 11464653Seric /* 11564653Seric ** Drop explicit From: if same as what we would generate. 11664653Seric ** This is to make MH (which doesn't always give a full name) 11764653Seric ** insert the full name information in all circumstances. 11864653Seric */ 11964653Seric 12014784Seric p = "resent-from"; 12155012Seric if (!bitset(EF_RESENT, e->e_flags)) 12214784Seric p += 7; 12359579Seric if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0) 12411414Seric { 12563753Seric if (tTd(31, 2)) 12663753Seric { 12763753Seric printf("comparing header from (%s) against default (%s or %s)\n", 12863753Seric fvalue, e->e_from.q_paddr, e->e_from.q_user); 12963753Seric } 13055012Seric if (e->e_from.q_paddr != NULL && 13163753Seric (strcmp(fvalue, e->e_from.q_paddr) == 0 || 13263753Seric strcmp(fvalue, e->e_from.q_user) == 0)) 13311414Seric return (hi->hi_flags); 13464351Seric #ifdef MAYBENEXTRELEASE /* XXX UNTESTED XXX UNTESTED XXX UNTESTED XXX */ 13564351Seric #ifdef USERDB 13664351Seric else 13764351Seric { 13864351Seric auto ADDRESS a; 13964351Seric char *fancy; 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 && 160*67472Seric 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. 2166980Seric ** e -- the envelope to add them to. 2176980Seric ** 2186980Seric ** Returns: 2196980Seric ** none. 2206980Seric ** 2216980Seric ** Side Effects: 2226980Seric ** adds the field on the list of headers for this envelope. 2236980Seric */ 2246980Seric 2256980Seric addheader(field, value, e) 2266980Seric char *field; 2276980Seric char *value; 2286980Seric ENVELOPE *e; 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? */ 2426980Seric for (hp = &e->e_header; (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. 26555012Seric ** e -- the envelope containing the header. 2664091Seric ** 2674091Seric ** Returns: 2684091Seric ** pointer to the value part. 2694091Seric ** NULL if not found. 2704091Seric ** 2714091Seric ** Side Effects: 2729382Seric ** none. 2734091Seric */ 2744091Seric 2754091Seric char * 27655012Seric hvalue(field, e) 2774091Seric char *field; 27855012Seric register ENVELOPE *e; 2794091Seric { 2804091Seric register HDR *h; 2814091Seric 28255012Seric for (h = e->e_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 */ 35965052Seric p = hvalue("full-name", e); 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 */ 44155012Seric p = hvalue("precedence", e); 4429382Seric if (p != NULL) 4439382Seric e->e_class = priencode(p); 44458929Seric if (full) 44525013Seric e->e_msgpriority = e->e_msgsize 44624981Seric - e->e_class * WkClassFact 44724981Seric + e->e_nrcpts * WkRecipFact; 4487783Seric 4497783Seric /* date message originated */ 45055012Seric p = hvalue("posted-date", e); 4517783Seric if (p == NULL) 45255012Seric p = hvalue("date", e); 4537783Seric if (p != NULL) 4549382Seric define('a', p, e); 45511290Seric 45611290Seric /* 45765983Seric ** From person in antiquated ARPANET mode 45865983Seric ** required by UK Grey Book e-mail gateways (sigh) 45965983Seric */ 46065983Seric 46165983Seric if (OpMode == MD_ARPAFTP) 46265983Seric { 46365983Seric register struct hdrinfo *hi; 46465983Seric 46565983Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 46665983Seric { 46765983Seric if (bitset(H_FROM, hi->hi_flags) && 46865983Seric (!bitset(H_RESENT, hi->hi_flags) || 46965983Seric bitset(EF_RESENT, e->e_flags)) && 47065983Seric (p = hvalue(hi->hi_field, e)) != NULL) 47165983Seric break; 47265983Seric } 47365983Seric if (hi->hi_field != NULL) 47465983Seric { 47565983Seric if (tTd(32, 2)) 47665983Seric printf("eatheader: setsender(*%s == %s)\n", 47765983Seric hi->hi_field, p); 47865983Seric setsender(p, e, NULL, TRUE); 47965983Seric } 48065983Seric } 48165983Seric 48265983Seric /* 48311290Seric ** Log collection information. 48411290Seric */ 48511290Seric 48611290Seric # ifdef LOG 48758929Seric if (full && LogLevel > 4) 48865089Seric logsender(e, msgid); 48965089Seric # endif /* LOG */ 49065089Seric e->e_flags &= ~EF_LOGSENDER; 49165089Seric } 49265089Seric /* 49365089Seric ** LOGSENDER -- log sender information 49465089Seric ** 49565089Seric ** Parameters: 49665089Seric ** e -- the envelope to log 49765089Seric ** msgid -- the message id 49865089Seric ** 49965089Seric ** Returns: 50065089Seric ** none 50165089Seric */ 50265089Seric 50365089Seric logsender(e, msgid) 50465089Seric register ENVELOPE *e; 50565089Seric char *msgid; 50665089Seric { 50766748Seric # ifdef LOG 50865089Seric char *name; 50965089Seric register char *sbp; 51065089Seric register char *p; 51165089Seric char hbuf[MAXNAME]; 51265089Seric char sbuf[MAXLINE]; 51365089Seric 51465089Seric if (bitset(EF_RESPONSE, e->e_flags)) 51565089Seric name = "[RESPONSE]"; 51665089Seric else if ((name = macvalue('_', e)) != NULL) 51765089Seric ; 51866003Seric else if (RealHostName == NULL) 51966003Seric name = "localhost"; 52065089Seric else if (RealHostName[0] == '[') 52165089Seric name = RealHostName; 52265089Seric else 52311290Seric { 52465089Seric name = hbuf; 52565089Seric (void) sprintf(hbuf, "%.80s", RealHostName); 52665089Seric if (RealHostAddr.sa.sa_family != 0) 52757359Seric { 52865089Seric p = &hbuf[strlen(hbuf)]; 52965089Seric (void) sprintf(p, " (%s)", 53065089Seric anynet_ntoa(&RealHostAddr)); 53157359Seric } 53265089Seric } 53357359Seric 53465089Seric /* some versions of syslog only take 5 printf args */ 53565059Seric # if (SYSLOG_BUFSIZE) >= 256 53665089Seric sbp = sbuf; 53765089Seric sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d", 53865089Seric e->e_from.q_paddr, e->e_msgsize, e->e_class, 53965089Seric e->e_msgpriority, e->e_nrcpts); 54065089Seric sbp += strlen(sbp); 54165089Seric if (msgid != NULL) 54265089Seric { 54365089Seric sprintf(sbp, ", msgid=%.100s", msgid); 54460575Seric sbp += strlen(sbp); 54565089Seric } 54665089Seric if (e->e_bodytype != NULL) 54765089Seric { 54865089Seric (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype); 54965089Seric sbp += strlen(sbp); 55065089Seric } 55165089Seric p = macvalue('r', e); 55265089Seric if (p != NULL) 55365089Seric (void) sprintf(sbp, ", proto=%.20s", p); 55465089Seric syslog(LOG_INFO, "%s: %s, relay=%s", 55565089Seric e->e_id, sbuf, name); 55665059Seric 55765059Seric # else /* short syslog buffer */ 55865059Seric 55965089Seric syslog(LOG_INFO, "%s: from=%s", 56065089Seric e->e_id, shortenstring(e->e_from.q_paddr, 83)); 56165089Seric syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d", 56265089Seric e->e_id, e->e_msgsize, e->e_class, 56365089Seric e->e_msgpriority, e->e_nrcpts); 56465089Seric if (msgid != NULL) 56565059Seric syslog(LOG_INFO, "%s: msgid=%s", e->e_id, msgid); 56665650Seric sbp = sbuf; 56765650Seric sprintf(sbp, "%s:", e->e_id); 56865650Seric sbp += strlen(sbp); 56965089Seric if (e->e_bodytype != NULL) 57065650Seric { 57165650Seric sprintf(sbp, " bodytype=%s,", e->e_bodytype); 57265650Seric sbp += strlen(sbp); 57365650Seric } 57465089Seric p = macvalue('r', e); 57565089Seric if (p != NULL) 57665650Seric { 57765731Seric sprintf(sbp, " proto=%s,", p); 57865650Seric sbp += strlen(sbp); 57965650Seric } 58065650Seric syslog(LOG_INFO, "%s relay=%s", sbuf, name); 58165059Seric # endif 58266748Seric # endif 5837783Seric } 5847783Seric /* 5857783Seric ** PRIENCODE -- encode external priority names into internal values. 5867783Seric ** 5877783Seric ** Parameters: 5887783Seric ** p -- priority in ascii. 5897783Seric ** 5907783Seric ** Returns: 5917783Seric ** priority as a numeric level. 5927783Seric ** 5937783Seric ** Side Effects: 5947783Seric ** none. 5957783Seric */ 5967783Seric 5977783Seric priencode(p) 5987783Seric char *p; 5997783Seric { 6008253Seric register int i; 6017783Seric 6028253Seric for (i = 0; i < NumPriorities; i++) 6037783Seric { 60433725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 6058253Seric return (Priorities[i].pri_val); 6067783Seric } 6078253Seric 6088253Seric /* unknown priority */ 6098253Seric return (0); 6107783Seric } 6117783Seric /* 6127890Seric ** CRACKADDR -- parse an address and turn it into a macro 6137783Seric ** 6147783Seric ** This doesn't actually parse the address -- it just extracts 6157783Seric ** it and replaces it with "$g". The parse is totally ad hoc 6167783Seric ** and isn't even guaranteed to leave something syntactically 6177783Seric ** identical to what it started with. However, it does leave 6187783Seric ** something semantically identical. 6197783Seric ** 62051379Seric ** This algorithm has been cleaned up to handle a wider range 62151379Seric ** of cases -- notably quoted and backslash escaped strings. 62251379Seric ** This modification makes it substantially better at preserving 62351379Seric ** the original syntax. 6247783Seric ** 6257783Seric ** Parameters: 6267890Seric ** addr -- the address to be cracked. 6277783Seric ** 6287783Seric ** Returns: 6297783Seric ** a pointer to the new version. 6307783Seric ** 6317783Seric ** Side Effects: 6327890Seric ** none. 6337783Seric ** 6347783Seric ** Warning: 6357783Seric ** The return value is saved in local storage and should 6367783Seric ** be copied if it is to be reused. 6377783Seric */ 6387783Seric 6397783Seric char * 6407890Seric crackaddr(addr) 6417890Seric register char *addr; 6427783Seric { 6437783Seric register char *p; 64451379Seric register char c; 64551379Seric int cmtlev; 64656764Seric int realcmtlev; 64756764Seric int anglelev, realanglelev; 64851379Seric int copylev; 64951379Seric bool qmode; 65056764Seric bool realqmode; 65156764Seric bool skipping; 65251379Seric bool putgmac = FALSE; 65356735Seric bool quoteit = FALSE; 65464148Seric bool gotangle = FALSE; 65551379Seric register char *bp; 65656764Seric char *buflim; 6577783Seric static char buf[MAXNAME]; 6587783Seric 6597783Seric if (tTd(33, 1)) 6607890Seric printf("crackaddr(%s)\n", addr); 6617783Seric 6628082Seric /* strip leading spaces */ 66358050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 6648082Seric addr++; 6658082Seric 6667783Seric /* 66751379Seric ** Start by assuming we have no angle brackets. This will be 66851379Seric ** adjusted later if we find them. 6697783Seric */ 6707783Seric 67151379Seric bp = buf; 67256764Seric buflim = &buf[sizeof buf - 5]; 67351379Seric p = addr; 67456764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 67556764Seric qmode = realqmode = FALSE; 67651379Seric 67751379Seric while ((c = *p++) != '\0') 6787783Seric { 67956764Seric /* 68056764Seric ** If the buffer is overful, go into a special "skipping" 68156764Seric ** mode that tries to keep legal syntax but doesn't actually 68256764Seric ** output things. 68356764Seric */ 6847783Seric 68556764Seric skipping = bp >= buflim; 68656735Seric 68756764Seric if (copylev > 0 && !skipping) 68856764Seric *bp++ = c; 68956735Seric 69051379Seric /* check for backslash escapes */ 69151379Seric if (c == '\\') 6927783Seric { 69358890Seric /* arrange to quote the address */ 69458890Seric if (cmtlev <= 0 && !qmode) 69558890Seric quoteit = TRUE; 69658890Seric 69751379Seric if ((c = *p++) == '\0') 6987783Seric { 69951379Seric /* too far */ 70051379Seric p--; 70151379Seric goto putg; 7027783Seric } 70356764Seric if (copylev > 0 && !skipping) 70451379Seric *bp++ = c; 70551379Seric goto putg; 7067783Seric } 7077783Seric 70851379Seric /* check for quoted strings */ 70964967Seric if (c == '"' && cmtlev <= 0) 7107783Seric { 71151379Seric qmode = !qmode; 71256764Seric if (copylev > 0 && !skipping) 71356764Seric realqmode = !realqmode; 71451379Seric continue; 7157783Seric } 71651379Seric if (qmode) 71751379Seric goto putg; 7187783Seric 71951379Seric /* check for comments */ 72051379Seric if (c == '(') 7217783Seric { 72251379Seric cmtlev++; 72356764Seric 72456764Seric /* allow space for closing paren */ 72556764Seric if (!skipping) 72656764Seric { 72756764Seric buflim--; 72856764Seric realcmtlev++; 72956764Seric if (copylev++ <= 0) 73056764Seric { 73156764Seric *bp++ = ' '; 73256764Seric *bp++ = c; 73356764Seric } 73456764Seric } 73551379Seric } 73651379Seric if (cmtlev > 0) 73751379Seric { 73851379Seric if (c == ')') 7397783Seric { 74051379Seric cmtlev--; 74151379Seric copylev--; 74256764Seric if (!skipping) 74356764Seric { 74456764Seric realcmtlev--; 74556764Seric buflim++; 74656764Seric } 7477783Seric } 7487783Seric continue; 7497783Seric } 75056764Seric else if (c == ')') 75156764Seric { 75256764Seric /* syntax error: unmatched ) */ 75365544Seric if (copylev > 0 && !skipping) 75456764Seric bp--; 75556764Seric } 7567783Seric 75756764Seric /* check for characters that may have to be quoted */ 75864148Seric if (strchr(".'@,;:\\()[]", c) != NULL) 75956764Seric { 76056764Seric /* 76156764Seric ** If these occur as the phrase part of a <> 76256764Seric ** construct, but are not inside of () or already 76356764Seric ** quoted, they will have to be quoted. Note that 76456764Seric ** now (but don't actually do the quoting). 76556764Seric */ 76656764Seric 76756764Seric if (cmtlev <= 0 && !qmode) 76856764Seric quoteit = TRUE; 76956764Seric } 77056764Seric 77151379Seric /* check for angle brackets */ 77251379Seric if (c == '<') 77351379Seric { 77456735Seric register char *q; 77556735Seric 77664148Seric /* assume first of two angles is bogus */ 77764148Seric if (gotangle) 77864148Seric quoteit = TRUE; 77964148Seric gotangle = TRUE; 78064148Seric 78151379Seric /* oops -- have to change our mind */ 78264752Seric anglelev = 1; 78356764Seric if (!skipping) 78464752Seric realanglelev = 1; 78556764Seric 78656735Seric bp = buf; 78756735Seric if (quoteit) 78856735Seric { 78956735Seric *bp++ = '"'; 79056735Seric 79156735Seric /* back up over the '<' and any spaces */ 79256735Seric --p; 79358050Seric while (isascii(*--p) && isspace(*p)) 79456735Seric continue; 79556735Seric p++; 79656735Seric } 79756735Seric for (q = addr; q < p; ) 79856735Seric { 79956735Seric c = *q++; 80056764Seric if (bp < buflim) 80156764Seric { 80256764Seric if (quoteit && c == '"') 80356764Seric *bp++ = '\\'; 80456764Seric *bp++ = c; 80556764Seric } 80656735Seric } 80756735Seric if (quoteit) 80856735Seric { 80964148Seric if (bp == &buf[1]) 81064148Seric bp--; 81164148Seric else 81264148Seric *bp++ = '"'; 81356764Seric while ((c = *p++) != '<') 81456764Seric { 81556764Seric if (bp < buflim) 81656764Seric *bp++ = c; 81756764Seric } 81856764Seric *bp++ = c; 81956735Seric } 82051379Seric copylev = 0; 82156735Seric putgmac = quoteit = FALSE; 82251379Seric continue; 82351379Seric } 8247783Seric 82551379Seric if (c == '>') 8267783Seric { 82756764Seric if (anglelev > 0) 82856764Seric { 82956764Seric anglelev--; 83056764Seric if (!skipping) 83156764Seric { 83256764Seric realanglelev--; 83356764Seric buflim++; 83456764Seric } 83556764Seric } 83656764Seric else if (!skipping) 83756764Seric { 83856764Seric /* syntax error: unmatched > */ 83956764Seric if (copylev > 0) 84056764Seric bp--; 84164752Seric quoteit = TRUE; 84256764Seric continue; 84356764Seric } 84451379Seric if (copylev++ <= 0) 84551379Seric *bp++ = c; 84651379Seric continue; 8477783Seric } 84851379Seric 84951379Seric /* must be a real address character */ 85051379Seric putg: 85151379Seric if (copylev <= 0 && !putgmac) 85251379Seric { 85358050Seric *bp++ = MACROEXPAND; 85451379Seric *bp++ = 'g'; 85551379Seric putgmac = TRUE; 85651379Seric } 8577783Seric } 8587783Seric 85956764Seric /* repair any syntactic damage */ 86056764Seric if (realqmode) 86156764Seric *bp++ = '"'; 86256764Seric while (realcmtlev-- > 0) 86356764Seric *bp++ = ')'; 86456764Seric while (realanglelev-- > 0) 86556764Seric *bp++ = '>'; 86651379Seric *bp++ = '\0'; 8677783Seric 8687783Seric if (tTd(33, 1)) 8697944Seric printf("crackaddr=>`%s'\n", buf); 8707783Seric 8717783Seric return (buf); 8727783Seric } 8739382Seric /* 8749382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 8759382Seric ** 8769382Seric ** Parameters: 87765870Seric ** mci -- the connection information. 8789382Seric ** e -- envelope to use. 8799382Seric ** 8809382Seric ** Returns: 8819382Seric ** none. 8829382Seric ** 8839382Seric ** Side Effects: 8849382Seric ** none. 8859382Seric */ 8869382Seric 88757589Seric /* 88857589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 88957589Seric */ 89057589Seric #ifndef MAX 89157589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 89257589Seric #endif 89357589Seric 89465870Seric putheader(mci, e) 89565870Seric register MCI *mci; 8969382Seric register ENVELOPE *e; 8979382Seric { 89857135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 8999382Seric register HDR *h; 90057135Seric char obuf[MAXLINE]; 9019382Seric 90259882Seric if (tTd(34, 1)) 90365870Seric printf("--- putheader, mailer = %s ---\n", 90465870Seric mci->mci_mailer->m_name); 90559882Seric 9069382Seric for (h = e->e_header; h != NULL; h = h->h_link) 9079382Seric { 9089382Seric register char *p; 90910689Seric extern bool bitintersect(); 9109382Seric 91159882Seric if (tTd(34, 11)) 91259882Seric { 91359882Seric printf(" %s: ", h->h_field); 91459882Seric xputs(h->h_value); 91559882Seric } 91659882Seric 9179382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 91865870Seric !bitintersect(h->h_mflags, mci->mci_mailer->m_flags)) 91959882Seric { 92059882Seric if (tTd(34, 11)) 92159882Seric printf(" (skipped)\n"); 9229382Seric continue; 92359882Seric } 9249382Seric 92511414Seric /* handle Resent-... headers specially */ 92611414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 92759882Seric { 92859882Seric if (tTd(34, 11)) 92959882Seric printf(" (skipped (resent))\n"); 93011414Seric continue; 93159882Seric } 93211414Seric 93366784Seric /* suppress return receipts if requested */ 93466784Seric if (bitset(H_RECEIPTTO, h->h_flags) && 93566784Seric bitset(EF_NORECEIPT, e->e_flags)) 93666784Seric { 93766784Seric if (tTd(34, 11)) 93866784Seric printf(" (skipped (receipt))\n"); 93966784Seric continue; 94066784Seric } 94166784Seric 94265152Seric /* macro expand value if generated internally */ 9439382Seric p = h->h_value; 9449382Seric if (bitset(H_DEFAULT, h->h_flags)) 9459382Seric { 9469382Seric expand(p, buf, &buf[sizeof buf], e); 9479382Seric p = buf; 9489382Seric if (p == NULL || *p == '\0') 94965152Seric { 95065152Seric if (tTd(34, 11)) 95165152Seric printf(" (skipped -- null value)\n"); 9529382Seric continue; 95365152Seric } 9549382Seric } 9559382Seric 95665152Seric if (tTd(34, 11)) 95765152Seric printf("\n"); 95865152Seric 9599382Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 9609382Seric { 9619382Seric /* address field */ 9629382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 9639382Seric 9649382Seric if (bitset(H_FROM, h->h_flags)) 9659382Seric oldstyle = FALSE; 96665870Seric commaize(h, p, oldstyle, mci, e); 9679382Seric } 9689382Seric else 9699382Seric { 9709382Seric /* vanilla header line */ 97112159Seric register char *nlp; 97212159Seric 97359579Seric (void) sprintf(obuf, "%s: ", h->h_field); 97456795Seric while ((nlp = strchr(p, '\n')) != NULL) 97512159Seric { 97612159Seric *nlp = '\0'; 97712159Seric (void) strcat(obuf, p); 97812159Seric *nlp = '\n'; 97965870Seric putline(obuf, mci); 98012159Seric p = ++nlp; 98112161Seric obuf[0] = '\0'; 98212159Seric } 98312159Seric (void) strcat(obuf, p); 98465870Seric putline(obuf, mci); 9859382Seric } 9869382Seric } 9879382Seric } 9889382Seric /* 9899382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 9909382Seric ** 9919382Seric ** Parameters: 9929382Seric ** h -- the header field to output. 9939382Seric ** p -- the value to put in it. 9949382Seric ** oldstyle -- TRUE if this is an old style header. 99565870Seric ** mci -- the connection information. 99655012Seric ** e -- the envelope containing the message. 9979382Seric ** 9989382Seric ** Returns: 9999382Seric ** none. 10009382Seric ** 10019382Seric ** Side Effects: 10029382Seric ** outputs "p" to file "fp". 10039382Seric */ 10049382Seric 100565870Seric void 100665870Seric commaize(h, p, oldstyle, mci, e) 10079382Seric register HDR *h; 10089382Seric register char *p; 10099382Seric bool oldstyle; 101065870Seric register MCI *mci; 101155012Seric register ENVELOPE *e; 10129382Seric { 10139382Seric register char *obp; 10149382Seric int opos; 101566004Seric int omax; 10169382Seric bool firstone = TRUE; 101711157Seric char obuf[MAXLINE + 3]; 10189382Seric 10199382Seric /* 10209382Seric ** Output the address list translated by the 10219382Seric ** mailer and with commas. 10229382Seric */ 10239382Seric 10249382Seric if (tTd(14, 2)) 10259382Seric printf("commaize(%s: %s)\n", h->h_field, p); 10269382Seric 10279382Seric obp = obuf; 102859579Seric (void) sprintf(obp, "%s: ", h->h_field); 10299382Seric opos = strlen(h->h_field) + 2; 10309382Seric obp += opos; 103166254Seric omax = mci->mci_mailer->m_linelimit - 2; 103266254Seric if (omax < 0 || omax > 78) 103366254Seric omax = 78; 10349382Seric 10359382Seric /* 10369382Seric ** Run through the list of values. 10379382Seric */ 10389382Seric 10399382Seric while (*p != '\0') 10409382Seric { 10419382Seric register char *name; 104254983Seric register int c; 10439382Seric char savechar; 104459163Seric int flags; 104559163Seric auto int stat; 10469382Seric 10479382Seric /* 10489382Seric ** Find the end of the name. New style names 10499382Seric ** end with a comma, old style names end with 10509382Seric ** a space character. However, spaces do not 10519382Seric ** necessarily delimit an old-style name -- at 10529382Seric ** signs mean keep going. 10539382Seric */ 10549382Seric 10559382Seric /* find end of name */ 105658050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 10579382Seric p++; 10589382Seric name = p; 10599382Seric for (;;) 10609382Seric { 106158333Seric auto char *oldp; 106216909Seric char pvpbuf[PSBUFSIZE]; 10639382Seric 106465066Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, 106565066Seric sizeof pvpbuf, &oldp); 106658333Seric p = oldp; 10679382Seric 10689382Seric /* look to see if we have an at sign */ 106958050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 10709382Seric p++; 10719382Seric 107258170Seric if (*p != '@') 10739382Seric { 10749382Seric p = oldp; 10759382Seric break; 10769382Seric } 10779382Seric p += *p == '@' ? 1 : 2; 107858050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 10799382Seric p++; 10809382Seric } 10819382Seric /* at the end of one complete name */ 10829382Seric 10839382Seric /* strip off trailing white space */ 108458050Seric while (p >= name && 108558050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 10869382Seric p--; 10879382Seric if (++p == name) 10889382Seric continue; 10899382Seric savechar = *p; 10909382Seric *p = '\0'; 10919382Seric 10929382Seric /* translate the name to be relative */ 109359163Seric flags = RF_HEADERADDR|RF_ADDDOMAIN; 109459163Seric if (bitset(H_FROM, h->h_flags)) 109559163Seric flags |= RF_SENDERADDR; 109659163Seric stat = EX_OK; 109765870Seric name = remotename(name, mci->mci_mailer, flags, &stat, e); 10989382Seric if (*name == '\0') 10999382Seric { 11009382Seric *p = savechar; 11019382Seric continue; 11029382Seric } 11039382Seric 11049382Seric /* output the name with nice formatting */ 110554983Seric opos += strlen(name); 11069382Seric if (!firstone) 11079382Seric opos += 2; 110866004Seric if (opos > omax && !firstone) 11099382Seric { 111010178Seric (void) strcpy(obp, ",\n"); 111165870Seric putline(obuf, mci); 11129382Seric obp = obuf; 111366255Seric (void) strcpy(obp, " "); 111410161Seric opos = strlen(obp); 111510161Seric obp += opos; 111654983Seric opos += strlen(name); 11179382Seric } 11189382Seric else if (!firstone) 11199382Seric { 112066255Seric (void) strcpy(obp, ", "); 11219382Seric obp += 2; 11229382Seric } 11239382Seric 112454983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 112554983Seric *obp++ = c; 11269382Seric firstone = FALSE; 11279382Seric *p = savechar; 11289382Seric } 11299382Seric (void) strcpy(obp, "\n"); 113065870Seric putline(obuf, mci); 11319382Seric } 11329382Seric /* 113358170Seric ** COPYHEADER -- copy header list 11349382Seric ** 113558170Seric ** This routine is the equivalent of newstr for header lists 113658170Seric ** 11379382Seric ** Parameters: 113858170Seric ** header -- list of header structures to copy. 11399382Seric ** 11409382Seric ** Returns: 114158170Seric ** a copy of 'header'. 11429382Seric ** 11439382Seric ** Side Effects: 11449382Seric ** none. 11459382Seric */ 11469382Seric 114758170Seric HDR * 114858170Seric copyheader(header) 114958170Seric register HDR *header; 11509382Seric { 115158170Seric register HDR *newhdr; 115258170Seric HDR *ret; 115358170Seric register HDR **tail = &ret; 11549382Seric 115558170Seric while (header != NULL) 115658170Seric { 115758170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 115858170Seric STRUCTCOPY(*header, *newhdr); 115958170Seric *tail = newhdr; 116058170Seric tail = &newhdr->h_link; 116158170Seric header = header->h_link; 116258170Seric } 116358170Seric *tail = NULL; 116458170Seric 116558170Seric return ret; 11669382Seric } 1167