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*65731Seric static char sccsid[] = "@(#)headers.c 8.22 (Berkeley) 01/13/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; 14064351Seric extern char *crackaddr(); 14164351Seric extern char *udbsender(); 14264351Seric 14364653Seric /* 14464653Seric ** Try doing USERDB rewriting even on fully commented 14564653Seric ** names; this saves the "comment" information (such 14664653Seric ** as full name) and rewrites the electronic part. 14764653Seric */ 14864653Seric 14964351Seric fancy = crackaddr(fvalue); 15064351Seric if (parseaddr(fvalue, &a, RF_COPYNONE, '\0', NULL, e) != NULL && 15164351Seric a.q_mailer == LocalMailer && 15264351Seric (p = udbsender(a.q_user)) != NULL) 15364351Seric { 15464351Seric char *oldg = macvalue('g', e); 15564351Seric 15664351Seric define('g', p, e); 15764351Seric expand(fancy, buf, &buf[sizeof buf], e); 15864351Seric define('g', oldg, e); 15964351Seric fvalue = buf; 16064351Seric } 16164351Seric } 16264351Seric #endif 16364351Seric #endif 16411414Seric } 16511414Seric 16614784Seric /* delete default value for this header */ 16755012Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 16814784Seric { 16959579Seric if (strcasecmp(fname, h->h_field) == 0 && 17014784Seric bitset(H_DEFAULT, h->h_flags) && 17114784Seric !bitset(H_FORCE, h->h_flags)) 17214784Seric h->h_value = NULL; 17314784Seric } 17414784Seric 17513012Seric /* create a new node */ 17613012Seric h = (HDR *) xalloc(sizeof *h); 17713012Seric h->h_field = newstr(fname); 17864134Seric h->h_value = newstr(fvalue); 17913012Seric h->h_link = NULL; 18023117Seric bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts); 18113012Seric *hp = h; 1828066Seric h->h_flags = hi->hi_flags; 1834091Seric if (def) 1844091Seric h->h_flags |= H_DEFAULT; 1859059Seric if (cond) 1869059Seric h->h_flags |= H_CHECK; 1874091Seric 1885937Seric /* hack to see if this is a new format message */ 1898095Seric if (!def && bitset(H_RCPT|H_FROM, h->h_flags) && 19056795Seric (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL || 19156795Seric strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL)) 1928089Seric { 19355012Seric e->e_flags &= ~EF_OLDSTYLE; 1948089Seric } 1955937Seric 1964091Seric return (h->h_flags); 1974091Seric } 1984091Seric /* 1996980Seric ** ADDHEADER -- add a header entry to the end of the queue. 2006980Seric ** 2016980Seric ** This bypasses the special checking of chompheader. 2026980Seric ** 2036980Seric ** Parameters: 20459579Seric ** field -- the name of the header field. 20558789Seric ** value -- the value of the field. 2066980Seric ** e -- the envelope to add them to. 2076980Seric ** 2086980Seric ** Returns: 2096980Seric ** none. 2106980Seric ** 2116980Seric ** Side Effects: 2126980Seric ** adds the field on the list of headers for this envelope. 2136980Seric */ 2146980Seric 2156980Seric addheader(field, value, e) 2166980Seric char *field; 2176980Seric char *value; 2186980Seric ENVELOPE *e; 2196980Seric { 2206980Seric register HDR *h; 2216980Seric register struct hdrinfo *hi; 2226980Seric HDR **hp; 2236980Seric 2246980Seric /* find info struct */ 2256980Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 2266980Seric { 22759579Seric if (strcasecmp(field, hi->hi_field) == 0) 2286980Seric break; 2296980Seric } 2306980Seric 2316980Seric /* find current place in list -- keep back pointer? */ 2326980Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 2336980Seric { 23459579Seric if (strcasecmp(field, h->h_field) == 0) 2356980Seric break; 2366980Seric } 2376980Seric 2386980Seric /* allocate space for new header */ 2396980Seric h = (HDR *) xalloc(sizeof *h); 2406980Seric h->h_field = field; 2416980Seric h->h_value = newstr(value); 2427368Seric h->h_link = *hp; 2436980Seric h->h_flags = hi->hi_flags | H_DEFAULT; 24410689Seric clrbitmap(h->h_mflags); 2456980Seric *hp = h; 2466980Seric } 2476980Seric /* 2484091Seric ** HVALUE -- return value of a header. 2494091Seric ** 2504091Seric ** Only "real" fields (i.e., ones that have not been supplied 2514091Seric ** as a default) are used. 2524091Seric ** 2534091Seric ** Parameters: 2544091Seric ** field -- the field name. 25555012Seric ** e -- the envelope containing the header. 2564091Seric ** 2574091Seric ** Returns: 2584091Seric ** pointer to the value part. 2594091Seric ** NULL if not found. 2604091Seric ** 2614091Seric ** Side Effects: 2629382Seric ** none. 2634091Seric */ 2644091Seric 2654091Seric char * 26655012Seric hvalue(field, e) 2674091Seric char *field; 26855012Seric register ENVELOPE *e; 2694091Seric { 2704091Seric register HDR *h; 2714091Seric 27255012Seric for (h = e->e_header; h != NULL; h = h->h_link) 2734091Seric { 27459579Seric if (!bitset(H_DEFAULT, h->h_flags) && 27559579Seric strcasecmp(h->h_field, field) == 0) 2764091Seric return (h->h_value); 2774091Seric } 2784091Seric return (NULL); 2794091Seric } 2804091Seric /* 2814091Seric ** ISHEADER -- predicate telling if argument is a header. 2824091Seric ** 2834319Seric ** A line is a header if it has a single word followed by 2844319Seric ** optional white space followed by a colon. 2854319Seric ** 2864091Seric ** Parameters: 2874091Seric ** s -- string to check for possible headerness. 2884091Seric ** 2894091Seric ** Returns: 2904091Seric ** TRUE if s is a header. 2914091Seric ** FALSE otherwise. 2924091Seric ** 2934091Seric ** Side Effects: 2944091Seric ** none. 2954091Seric */ 2964091Seric 2974091Seric bool 2984091Seric isheader(s) 2994091Seric register char *s; 3004091Seric { 3019382Seric while (*s > ' ' && *s != ':' && *s != '\0') 3024091Seric s++; 3039382Seric 3049382Seric /* following technically violates RFC822 */ 30558050Seric while (isascii(*s) && isspace(*s)) 3064091Seric s++; 3079382Seric 3084091Seric return (*s == ':'); 3094091Seric } 3105919Seric /* 3117783Seric ** EATHEADER -- run through the stored header and extract info. 3127783Seric ** 3137783Seric ** Parameters: 3149382Seric ** e -- the envelope to process. 31558929Seric ** full -- if set, do full processing (e.g., compute 31658929Seric ** message priority). 3177783Seric ** 3187783Seric ** Returns: 3197783Seric ** none. 3207783Seric ** 3217783Seric ** Side Effects: 3227783Seric ** Sets a bunch of global variables from information 3239382Seric ** in the collected header. 3249382Seric ** Aborts the message if the hop count is exceeded. 3257783Seric */ 3267783Seric 32758929Seric eatheader(e, full) 3289382Seric register ENVELOPE *e; 32958929Seric bool full; 3307783Seric { 3317783Seric register HDR *h; 3327783Seric register char *p; 3339382Seric int hopcnt = 0; 33457208Seric char *msgid; 33558688Seric char buf[MAXLINE]; 3367783Seric 33758688Seric /* 33858688Seric ** Set up macros for possible expansion in headers. 33958688Seric */ 34058688Seric 34158704Seric define('f', e->e_sender, e); 34258704Seric define('g', e->e_sender, e); 34364148Seric if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0') 34464148Seric define('u', e->e_origrcpt, e); 34564148Seric else 34664148Seric define('u', NULL, e); 34758688Seric 34865052Seric /* full name of from person */ 34965052Seric p = hvalue("full-name", e); 35065052Seric if (p != NULL) 35165052Seric define('x', p, e); 35265052Seric 3539382Seric if (tTd(32, 1)) 3549382Seric printf("----- collected header -----\n"); 35557208Seric msgid = "<none>"; 3569382Seric for (h = e->e_header; h != NULL; h = h->h_link) 3577783Seric { 35864156Seric if (h->h_value == NULL) 35964156Seric { 36064156Seric if (tTd(32, 1)) 36164156Seric printf("%s: <NULL>\n", h->h_field); 36264156Seric continue; 36364156Seric } 36464156Seric 36558688Seric /* do early binding */ 36664156Seric if (bitset(H_DEFAULT, h->h_flags)) 36758688Seric { 36858688Seric expand(h->h_value, buf, &buf[sizeof buf], e); 36958688Seric if (buf[0] != '\0') 37058688Seric { 37158688Seric h->h_value = newstr(buf); 37258688Seric h->h_flags &= ~H_DEFAULT; 37358688Seric } 37458688Seric } 37558688Seric 3769382Seric if (tTd(32, 1)) 37765152Seric { 37865152Seric printf("%s: ", h->h_field); 37965152Seric xputs(h->h_value); 38065152Seric printf("\n"); 38165152Seric } 38257359Seric 38311414Seric /* count the number of times it has been processed */ 3849382Seric if (bitset(H_TRACE, h->h_flags)) 3859382Seric hopcnt++; 38611414Seric 38711414Seric /* send to this person if we so desire */ 38811414Seric if (GrabTo && bitset(H_RCPT, h->h_flags) && 38911414Seric !bitset(H_DEFAULT, h->h_flags) && 39055012Seric (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags))) 39111414Seric { 39263839Seric int saveflags = e->e_flags; 39363839Seric 39464283Seric (void) sendtolist(h->h_value, NULLADDR, 39558082Seric &e->e_sendqueue, e); 39663839Seric 39763839Seric /* delete fatal errors generated by this address */ 39864148Seric if (!GrabTo && !bitset(EF_FATALERRS, saveflags)) 39963839Seric e->e_flags &= ~EF_FATALERRS; 40011414Seric } 40111414Seric 40257208Seric /* save the message-id for logging */ 40364156Seric if (full && strcasecmp(h->h_field, "message-id") == 0) 40411290Seric { 40557208Seric msgid = h->h_value; 40659859Seric while (isascii(*msgid) && isspace(*msgid)) 40759859Seric msgid++; 40811290Seric } 40957359Seric 41057359Seric /* see if this is a return-receipt header */ 41157359Seric if (bitset(H_RECEIPTTO, h->h_flags)) 41257359Seric e->e_receiptto = h->h_value; 41357359Seric 41457359Seric /* see if this is an errors-to header */ 41561104Seric if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags)) 41664283Seric (void) sendtolist(h->h_value, NULLADDR, 41758082Seric &e->e_errorqueue, e); 4189382Seric } 4199382Seric if (tTd(32, 1)) 4207783Seric printf("----------------------------\n"); 4217783Seric 42258145Seric /* if we are just verifying (that is, sendmail -t -bv), drop out now */ 42358145Seric if (OpMode == MD_VERIFY) 42458145Seric return; 42558145Seric 4269382Seric /* store hop count */ 4279382Seric if (hopcnt > e->e_hopcount) 4289382Seric e->e_hopcount = hopcnt; 4299382Seric 4307783Seric /* message priority */ 43155012Seric p = hvalue("precedence", e); 4329382Seric if (p != NULL) 4339382Seric e->e_class = priencode(p); 43458929Seric if (full) 43525013Seric e->e_msgpriority = e->e_msgsize 43624981Seric - e->e_class * WkClassFact 43724981Seric + e->e_nrcpts * WkRecipFact; 4387783Seric 4397783Seric /* date message originated */ 44055012Seric p = hvalue("posted-date", e); 4417783Seric if (p == NULL) 44255012Seric p = hvalue("date", e); 4437783Seric if (p != NULL) 4449382Seric define('a', p, e); 44511290Seric 44611290Seric /* 44711290Seric ** Log collection information. 44811290Seric */ 44911290Seric 45011290Seric # ifdef LOG 45158929Seric if (full && LogLevel > 4) 45265089Seric logsender(e, msgid); 45365089Seric # endif /* LOG */ 45465089Seric e->e_flags &= ~EF_LOGSENDER; 45565089Seric } 45665089Seric /* 45765089Seric ** LOGSENDER -- log sender information 45865089Seric ** 45965089Seric ** Parameters: 46065089Seric ** e -- the envelope to log 46165089Seric ** msgid -- the message id 46265089Seric ** 46365089Seric ** Returns: 46465089Seric ** none 46565089Seric */ 46665089Seric 46765089Seric logsender(e, msgid) 46865089Seric register ENVELOPE *e; 46965089Seric char *msgid; 47065089Seric { 47165089Seric char *name; 47265089Seric register char *sbp; 47365089Seric register char *p; 47465089Seric char hbuf[MAXNAME]; 47565089Seric char sbuf[MAXLINE]; 47665089Seric 47765089Seric if (bitset(EF_RESPONSE, e->e_flags)) 47865089Seric name = "[RESPONSE]"; 47965089Seric else if ((name = macvalue('_', e)) != NULL) 48065089Seric ; 48165089Seric else if (RealHostName[0] == '[') 48265089Seric name = RealHostName; 48365089Seric else 48411290Seric { 48565089Seric name = hbuf; 48665089Seric (void) sprintf(hbuf, "%.80s", RealHostName); 48765089Seric if (RealHostAddr.sa.sa_family != 0) 48857359Seric { 48965089Seric p = &hbuf[strlen(hbuf)]; 49065089Seric (void) sprintf(p, " (%s)", 49165089Seric anynet_ntoa(&RealHostAddr)); 49257359Seric } 49365089Seric } 49457359Seric 49565089Seric /* some versions of syslog only take 5 printf args */ 49665059Seric # if (SYSLOG_BUFSIZE) >= 256 49765089Seric sbp = sbuf; 49865089Seric sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d", 49965089Seric e->e_from.q_paddr, e->e_msgsize, e->e_class, 50065089Seric e->e_msgpriority, e->e_nrcpts); 50165089Seric sbp += strlen(sbp); 50265089Seric if (msgid != NULL) 50365089Seric { 50465089Seric sprintf(sbp, ", msgid=%.100s", msgid); 50560575Seric sbp += strlen(sbp); 50665089Seric } 50765089Seric if (e->e_bodytype != NULL) 50865089Seric { 50965089Seric (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype); 51065089Seric sbp += strlen(sbp); 51165089Seric } 51265089Seric p = macvalue('r', e); 51365089Seric if (p != NULL) 51465089Seric (void) sprintf(sbp, ", proto=%.20s", p); 51565089Seric syslog(LOG_INFO, "%s: %s, relay=%s", 51665089Seric e->e_id, sbuf, name); 51765059Seric 51865059Seric # else /* short syslog buffer */ 51965059Seric 52065089Seric syslog(LOG_INFO, "%s: from=%s", 52165089Seric e->e_id, shortenstring(e->e_from.q_paddr, 83)); 52265089Seric syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d", 52365089Seric e->e_id, e->e_msgsize, e->e_class, 52465089Seric e->e_msgpriority, e->e_nrcpts); 52565089Seric if (msgid != NULL) 52665059Seric syslog(LOG_INFO, "%s: msgid=%s", e->e_id, msgid); 52765650Seric sbp = sbuf; 52865650Seric sprintf(sbp, "%s:", e->e_id); 52965650Seric sbp += strlen(sbp); 53065089Seric if (e->e_bodytype != NULL) 53165650Seric { 53265650Seric sprintf(sbp, " bodytype=%s,", e->e_bodytype); 53365650Seric sbp += strlen(sbp); 53465650Seric } 53565089Seric p = macvalue('r', e); 53665089Seric if (p != NULL) 53765650Seric { 538*65731Seric sprintf(sbp, " proto=%s,", p); 53965650Seric sbp += strlen(sbp); 54065650Seric } 54165650Seric syslog(LOG_INFO, "%s relay=%s", sbuf, name); 54265059Seric # endif 5437783Seric } 5447783Seric /* 5457783Seric ** PRIENCODE -- encode external priority names into internal values. 5467783Seric ** 5477783Seric ** Parameters: 5487783Seric ** p -- priority in ascii. 5497783Seric ** 5507783Seric ** Returns: 5517783Seric ** priority as a numeric level. 5527783Seric ** 5537783Seric ** Side Effects: 5547783Seric ** none. 5557783Seric */ 5567783Seric 5577783Seric priencode(p) 5587783Seric char *p; 5597783Seric { 5608253Seric register int i; 5617783Seric 5628253Seric for (i = 0; i < NumPriorities; i++) 5637783Seric { 56433725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 5658253Seric return (Priorities[i].pri_val); 5667783Seric } 5678253Seric 5688253Seric /* unknown priority */ 5698253Seric return (0); 5707783Seric } 5717783Seric /* 5727890Seric ** CRACKADDR -- parse an address and turn it into a macro 5737783Seric ** 5747783Seric ** This doesn't actually parse the address -- it just extracts 5757783Seric ** it and replaces it with "$g". The parse is totally ad hoc 5767783Seric ** and isn't even guaranteed to leave something syntactically 5777783Seric ** identical to what it started with. However, it does leave 5787783Seric ** something semantically identical. 5797783Seric ** 58051379Seric ** This algorithm has been cleaned up to handle a wider range 58151379Seric ** of cases -- notably quoted and backslash escaped strings. 58251379Seric ** This modification makes it substantially better at preserving 58351379Seric ** the original syntax. 5847783Seric ** 5857783Seric ** Parameters: 5867890Seric ** addr -- the address to be cracked. 5877783Seric ** 5887783Seric ** Returns: 5897783Seric ** a pointer to the new version. 5907783Seric ** 5917783Seric ** Side Effects: 5927890Seric ** none. 5937783Seric ** 5947783Seric ** Warning: 5957783Seric ** The return value is saved in local storage and should 5967783Seric ** be copied if it is to be reused. 5977783Seric */ 5987783Seric 5997783Seric char * 6007890Seric crackaddr(addr) 6017890Seric register char *addr; 6027783Seric { 6037783Seric register char *p; 60451379Seric register char c; 60551379Seric int cmtlev; 60656764Seric int realcmtlev; 60756764Seric int anglelev, realanglelev; 60851379Seric int copylev; 60951379Seric bool qmode; 61056764Seric bool realqmode; 61156764Seric bool skipping; 61251379Seric bool putgmac = FALSE; 61356735Seric bool quoteit = FALSE; 61464148Seric bool gotangle = FALSE; 61551379Seric register char *bp; 61656764Seric char *buflim; 6177783Seric static char buf[MAXNAME]; 6187783Seric 6197783Seric if (tTd(33, 1)) 6207890Seric printf("crackaddr(%s)\n", addr); 6217783Seric 6228082Seric /* strip leading spaces */ 62358050Seric while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 6248082Seric addr++; 6258082Seric 6267783Seric /* 62751379Seric ** Start by assuming we have no angle brackets. This will be 62851379Seric ** adjusted later if we find them. 6297783Seric */ 6307783Seric 63151379Seric bp = buf; 63256764Seric buflim = &buf[sizeof buf - 5]; 63351379Seric p = addr; 63456764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 63556764Seric qmode = realqmode = FALSE; 63651379Seric 63751379Seric while ((c = *p++) != '\0') 6387783Seric { 63956764Seric /* 64056764Seric ** If the buffer is overful, go into a special "skipping" 64156764Seric ** mode that tries to keep legal syntax but doesn't actually 64256764Seric ** output things. 64356764Seric */ 6447783Seric 64556764Seric skipping = bp >= buflim; 64656735Seric 64756764Seric if (copylev > 0 && !skipping) 64856764Seric *bp++ = c; 64956735Seric 65051379Seric /* check for backslash escapes */ 65151379Seric if (c == '\\') 6527783Seric { 65358890Seric /* arrange to quote the address */ 65458890Seric if (cmtlev <= 0 && !qmode) 65558890Seric quoteit = TRUE; 65658890Seric 65751379Seric if ((c = *p++) == '\0') 6587783Seric { 65951379Seric /* too far */ 66051379Seric p--; 66151379Seric goto putg; 6627783Seric } 66356764Seric if (copylev > 0 && !skipping) 66451379Seric *bp++ = c; 66551379Seric goto putg; 6667783Seric } 6677783Seric 66851379Seric /* check for quoted strings */ 66964967Seric if (c == '"' && cmtlev <= 0) 6707783Seric { 67151379Seric qmode = !qmode; 67256764Seric if (copylev > 0 && !skipping) 67356764Seric realqmode = !realqmode; 67451379Seric continue; 6757783Seric } 67651379Seric if (qmode) 67751379Seric goto putg; 6787783Seric 67951379Seric /* check for comments */ 68051379Seric if (c == '(') 6817783Seric { 68251379Seric cmtlev++; 68356764Seric 68456764Seric /* allow space for closing paren */ 68556764Seric if (!skipping) 68656764Seric { 68756764Seric buflim--; 68856764Seric realcmtlev++; 68956764Seric if (copylev++ <= 0) 69056764Seric { 69156764Seric *bp++ = ' '; 69256764Seric *bp++ = c; 69356764Seric } 69456764Seric } 69551379Seric } 69651379Seric if (cmtlev > 0) 69751379Seric { 69851379Seric if (c == ')') 6997783Seric { 70051379Seric cmtlev--; 70151379Seric copylev--; 70256764Seric if (!skipping) 70356764Seric { 70456764Seric realcmtlev--; 70556764Seric buflim++; 70656764Seric } 7077783Seric } 7087783Seric continue; 7097783Seric } 71056764Seric else if (c == ')') 71156764Seric { 71256764Seric /* syntax error: unmatched ) */ 71365544Seric if (copylev > 0 && !skipping) 71456764Seric bp--; 71556764Seric } 7167783Seric 71756764Seric /* check for characters that may have to be quoted */ 71864148Seric if (strchr(".'@,;:\\()[]", c) != NULL) 71956764Seric { 72056764Seric /* 72156764Seric ** If these occur as the phrase part of a <> 72256764Seric ** construct, but are not inside of () or already 72356764Seric ** quoted, they will have to be quoted. Note that 72456764Seric ** now (but don't actually do the quoting). 72556764Seric */ 72656764Seric 72756764Seric if (cmtlev <= 0 && !qmode) 72856764Seric quoteit = TRUE; 72956764Seric } 73056764Seric 73151379Seric /* check for angle brackets */ 73251379Seric if (c == '<') 73351379Seric { 73456735Seric register char *q; 73556735Seric 73664148Seric /* assume first of two angles is bogus */ 73764148Seric if (gotangle) 73864148Seric quoteit = TRUE; 73964148Seric gotangle = TRUE; 74064148Seric 74151379Seric /* oops -- have to change our mind */ 74264752Seric anglelev = 1; 74356764Seric if (!skipping) 74464752Seric realanglelev = 1; 74556764Seric 74656735Seric bp = buf; 74756735Seric if (quoteit) 74856735Seric { 74956735Seric *bp++ = '"'; 75056735Seric 75156735Seric /* back up over the '<' and any spaces */ 75256735Seric --p; 75358050Seric while (isascii(*--p) && isspace(*p)) 75456735Seric continue; 75556735Seric p++; 75656735Seric } 75756735Seric for (q = addr; q < p; ) 75856735Seric { 75956735Seric c = *q++; 76056764Seric if (bp < buflim) 76156764Seric { 76256764Seric if (quoteit && c == '"') 76356764Seric *bp++ = '\\'; 76456764Seric *bp++ = c; 76556764Seric } 76656735Seric } 76756735Seric if (quoteit) 76856735Seric { 76964148Seric if (bp == &buf[1]) 77064148Seric bp--; 77164148Seric else 77264148Seric *bp++ = '"'; 77356764Seric while ((c = *p++) != '<') 77456764Seric { 77556764Seric if (bp < buflim) 77656764Seric *bp++ = c; 77756764Seric } 77856764Seric *bp++ = c; 77956735Seric } 78051379Seric copylev = 0; 78156735Seric putgmac = quoteit = FALSE; 78251379Seric continue; 78351379Seric } 7847783Seric 78551379Seric if (c == '>') 7867783Seric { 78756764Seric if (anglelev > 0) 78856764Seric { 78956764Seric anglelev--; 79056764Seric if (!skipping) 79156764Seric { 79256764Seric realanglelev--; 79356764Seric buflim++; 79456764Seric } 79556764Seric } 79656764Seric else if (!skipping) 79756764Seric { 79856764Seric /* syntax error: unmatched > */ 79956764Seric if (copylev > 0) 80056764Seric bp--; 80164752Seric quoteit = TRUE; 80256764Seric continue; 80356764Seric } 80451379Seric if (copylev++ <= 0) 80551379Seric *bp++ = c; 80651379Seric continue; 8077783Seric } 80851379Seric 80951379Seric /* must be a real address character */ 81051379Seric putg: 81151379Seric if (copylev <= 0 && !putgmac) 81251379Seric { 81358050Seric *bp++ = MACROEXPAND; 81451379Seric *bp++ = 'g'; 81551379Seric putgmac = TRUE; 81651379Seric } 8177783Seric } 8187783Seric 81956764Seric /* repair any syntactic damage */ 82056764Seric if (realqmode) 82156764Seric *bp++ = '"'; 82256764Seric while (realcmtlev-- > 0) 82356764Seric *bp++ = ')'; 82456764Seric while (realanglelev-- > 0) 82556764Seric *bp++ = '>'; 82651379Seric *bp++ = '\0'; 8277783Seric 8287783Seric if (tTd(33, 1)) 8297944Seric printf("crackaddr=>`%s'\n", buf); 8307783Seric 8317783Seric return (buf); 8327783Seric } 8339382Seric /* 8349382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 8359382Seric ** 8369382Seric ** Parameters: 8379382Seric ** fp -- file to put it on. 8389382Seric ** m -- mailer to use. 8399382Seric ** e -- envelope to use. 8409382Seric ** 8419382Seric ** Returns: 8429382Seric ** none. 8439382Seric ** 8449382Seric ** Side Effects: 8459382Seric ** none. 8469382Seric */ 8479382Seric 84857589Seric /* 84957589Seric * Macro for fast max (not available in e.g. DG/UX, 386/ix). 85057589Seric */ 85157589Seric #ifndef MAX 85257589Seric # define MAX(a,b) (((a)>(b))?(a):(b)) 85357589Seric #endif 85457589Seric 85510176Seric putheader(fp, m, e) 8569382Seric register FILE *fp; 8579382Seric register MAILER *m; 8589382Seric register ENVELOPE *e; 8599382Seric { 86057135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 8619382Seric register HDR *h; 86257135Seric char obuf[MAXLINE]; 8639382Seric 86459882Seric if (tTd(34, 1)) 86559882Seric printf("--- putheader, mailer = %s ---\n", m->m_name); 86659882Seric 8679382Seric for (h = e->e_header; h != NULL; h = h->h_link) 8689382Seric { 8699382Seric register char *p; 87010689Seric extern bool bitintersect(); 8719382Seric 87259882Seric if (tTd(34, 11)) 87359882Seric { 87459882Seric printf(" %s: ", h->h_field); 87559882Seric xputs(h->h_value); 87659882Seric } 87759882Seric 8789382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 87910689Seric !bitintersect(h->h_mflags, m->m_flags)) 88059882Seric { 88159882Seric if (tTd(34, 11)) 88259882Seric printf(" (skipped)\n"); 8839382Seric continue; 88459882Seric } 8859382Seric 88611414Seric /* handle Resent-... headers specially */ 88711414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 88859882Seric { 88959882Seric if (tTd(34, 11)) 89059882Seric printf(" (skipped (resent))\n"); 89111414Seric continue; 89259882Seric } 89311414Seric 89465152Seric /* macro expand value if generated internally */ 8959382Seric p = h->h_value; 8969382Seric if (bitset(H_DEFAULT, h->h_flags)) 8979382Seric { 8989382Seric expand(p, buf, &buf[sizeof buf], e); 8999382Seric p = buf; 9009382Seric if (p == NULL || *p == '\0') 90165152Seric { 90265152Seric if (tTd(34, 11)) 90365152Seric printf(" (skipped -- null value)\n"); 9049382Seric continue; 90565152Seric } 9069382Seric } 9079382Seric 90865152Seric if (tTd(34, 11)) 90965152Seric printf("\n"); 91065152Seric 9119382Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 9129382Seric { 9139382Seric /* address field */ 9149382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 9159382Seric 9169382Seric if (bitset(H_FROM, h->h_flags)) 9179382Seric oldstyle = FALSE; 91855012Seric commaize(h, p, fp, oldstyle, m, e); 9199382Seric } 9209382Seric else 9219382Seric { 9229382Seric /* vanilla header line */ 92312159Seric register char *nlp; 92412159Seric 92559579Seric (void) sprintf(obuf, "%s: ", h->h_field); 92656795Seric while ((nlp = strchr(p, '\n')) != NULL) 92712159Seric { 92812159Seric *nlp = '\0'; 92912159Seric (void) strcat(obuf, p); 93012159Seric *nlp = '\n'; 93112159Seric putline(obuf, fp, m); 93212159Seric p = ++nlp; 93312161Seric obuf[0] = '\0'; 93412159Seric } 93512159Seric (void) strcat(obuf, p); 93610176Seric putline(obuf, fp, m); 9379382Seric } 9389382Seric } 9399382Seric } 9409382Seric /* 9419382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 9429382Seric ** 9439382Seric ** Parameters: 9449382Seric ** h -- the header field to output. 9459382Seric ** p -- the value to put in it. 9469382Seric ** fp -- file to put it to. 9479382Seric ** oldstyle -- TRUE if this is an old style header. 9489382Seric ** m -- a pointer to the mailer descriptor. If NULL, 9499382Seric ** don't transform the name at all. 95055012Seric ** e -- the envelope containing the message. 9519382Seric ** 9529382Seric ** Returns: 9539382Seric ** none. 9549382Seric ** 9559382Seric ** Side Effects: 9569382Seric ** outputs "p" to file "fp". 9579382Seric */ 9589382Seric 95955012Seric commaize(h, p, fp, oldstyle, m, e) 9609382Seric register HDR *h; 9619382Seric register char *p; 9629382Seric FILE *fp; 9639382Seric bool oldstyle; 9649382Seric register MAILER *m; 96555012Seric register ENVELOPE *e; 9669382Seric { 9679382Seric register char *obp; 9689382Seric int opos; 9699382Seric bool firstone = TRUE; 97011157Seric char obuf[MAXLINE + 3]; 9719382Seric 9729382Seric /* 9739382Seric ** Output the address list translated by the 9749382Seric ** mailer and with commas. 9759382Seric */ 9769382Seric 9779382Seric if (tTd(14, 2)) 9789382Seric printf("commaize(%s: %s)\n", h->h_field, p); 9799382Seric 9809382Seric obp = obuf; 98159579Seric (void) sprintf(obp, "%s: ", h->h_field); 9829382Seric opos = strlen(h->h_field) + 2; 9839382Seric obp += opos; 9849382Seric 9859382Seric /* 9869382Seric ** Run through the list of values. 9879382Seric */ 9889382Seric 9899382Seric while (*p != '\0') 9909382Seric { 9919382Seric register char *name; 99254983Seric register int c; 9939382Seric char savechar; 99459163Seric int flags; 99559163Seric auto int stat; 9969382Seric 9979382Seric /* 9989382Seric ** Find the end of the name. New style names 9999382Seric ** end with a comma, old style names end with 10009382Seric ** a space character. However, spaces do not 10019382Seric ** necessarily delimit an old-style name -- at 10029382Seric ** signs mean keep going. 10039382Seric */ 10049382Seric 10059382Seric /* find end of name */ 100658050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 10079382Seric p++; 10089382Seric name = p; 10099382Seric for (;;) 10109382Seric { 101158333Seric auto char *oldp; 101216909Seric char pvpbuf[PSBUFSIZE]; 10139382Seric 101465066Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, 101565066Seric sizeof pvpbuf, &oldp); 101658333Seric p = oldp; 10179382Seric 10189382Seric /* look to see if we have an at sign */ 101958050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 10209382Seric p++; 10219382Seric 102258170Seric if (*p != '@') 10239382Seric { 10249382Seric p = oldp; 10259382Seric break; 10269382Seric } 10279382Seric p += *p == '@' ? 1 : 2; 102858050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 10299382Seric p++; 10309382Seric } 10319382Seric /* at the end of one complete name */ 10329382Seric 10339382Seric /* strip off trailing white space */ 103458050Seric while (p >= name && 103558050Seric ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 10369382Seric p--; 10379382Seric if (++p == name) 10389382Seric continue; 10399382Seric savechar = *p; 10409382Seric *p = '\0'; 10419382Seric 10429382Seric /* translate the name to be relative */ 104359163Seric flags = RF_HEADERADDR|RF_ADDDOMAIN; 104459163Seric if (bitset(H_FROM, h->h_flags)) 104559163Seric flags |= RF_SENDERADDR; 104659163Seric stat = EX_OK; 104759163Seric name = remotename(name, m, flags, &stat, e); 10489382Seric if (*name == '\0') 10499382Seric { 10509382Seric *p = savechar; 10519382Seric continue; 10529382Seric } 10539382Seric 10549382Seric /* output the name with nice formatting */ 105554983Seric opos += strlen(name); 10569382Seric if (!firstone) 10579382Seric opos += 2; 10589382Seric if (opos > 78 && !firstone) 10599382Seric { 106010178Seric (void) strcpy(obp, ",\n"); 106110176Seric putline(obuf, fp, m); 10629382Seric obp = obuf; 10639382Seric (void) sprintf(obp, " "); 106410161Seric opos = strlen(obp); 106510161Seric obp += opos; 106654983Seric opos += strlen(name); 10679382Seric } 10689382Seric else if (!firstone) 10699382Seric { 10709382Seric (void) sprintf(obp, ", "); 10719382Seric obp += 2; 10729382Seric } 10739382Seric 107454983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 107554983Seric *obp++ = c; 10769382Seric firstone = FALSE; 10779382Seric *p = savechar; 10789382Seric } 10799382Seric (void) strcpy(obp, "\n"); 108010176Seric putline(obuf, fp, m); 10819382Seric } 10829382Seric /* 108358170Seric ** COPYHEADER -- copy header list 10849382Seric ** 108558170Seric ** This routine is the equivalent of newstr for header lists 108658170Seric ** 10879382Seric ** Parameters: 108858170Seric ** header -- list of header structures to copy. 10899382Seric ** 10909382Seric ** Returns: 109158170Seric ** a copy of 'header'. 10929382Seric ** 10939382Seric ** Side Effects: 10949382Seric ** none. 10959382Seric */ 10969382Seric 109758170Seric HDR * 109858170Seric copyheader(header) 109958170Seric register HDR *header; 11009382Seric { 110158170Seric register HDR *newhdr; 110258170Seric HDR *ret; 110358170Seric register HDR **tail = &ret; 11049382Seric 110558170Seric while (header != NULL) 110658170Seric { 110758170Seric newhdr = (HDR *) xalloc(sizeof(HDR)); 110858170Seric STRUCTCOPY(*header, *newhdr); 110958170Seric *tail = newhdr; 111058170Seric tail = &newhdr->h_link; 111158170Seric header = header->h_link; 111258170Seric } 111358170Seric *tail = NULL; 111458170Seric 111558170Seric return ret; 11169382Seric } 1117