122697Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 333728Sbostic * Copyright (c) 1988 Regents of the University of California. 433728Sbostic * All rights reserved. 533728Sbostic * 642824Sbostic * %sccs.include.redist.c% 733728Sbostic */ 822697Sdist 922697Sdist #ifndef lint 10*56795Seric static char sccsid[] = "@(#)collect.c 5.14 (Berkeley) 11/14/92"; 1133728Sbostic #endif /* not lint */ 1222697Sdist 131439Seric # include <errno.h> 143309Seric # include "sendmail.h" 151392Seric 161392Seric /* 172969Seric ** COLLECT -- read & parse message header & make temp file. 181392Seric ** 191392Seric ** Creates a temporary file name and copies the standard 209371Seric ** input to that file. Leading UNIX-style "From" lines are 219371Seric ** stripped off (after important information is extracted). 221392Seric ** 231392Seric ** Parameters: 2452106Seric ** smtpmode -- if set, we are running SMTP: give an RFC821 2552105Seric ** style message to say we are ready to collect 2652105Seric ** input, and never ignore a single dot to mean 2752105Seric ** end of message. 281392Seric ** 291392Seric ** Returns: 304162Seric ** none. 311392Seric ** 321392Seric ** Side Effects: 331392Seric ** Temp file is created and filled. 344162Seric ** The from person may be set. 351392Seric */ 361392Seric 3755012Seric collect(smtpmode, e) 3852105Seric bool smtpmode; 3955012Seric register ENVELOPE *e; 401392Seric { 411392Seric register FILE *tf; 4252105Seric bool ignrdot = smtpmode ? FALSE : IgnrDot; 4340965Sbostic char buf[MAXFIELD], buf2[MAXFIELD]; 4440965Sbostic register char *workbuf, *freebuf; 4540965Sbostic register int workbuflen; 462900Seric extern char *hvalue(); 4740965Sbostic extern bool isheader(), flusheol(); 481392Seric 491392Seric /* 501392Seric ** Create the temp file name and create the file. 511392Seric */ 521392Seric 5355012Seric e->e_df = newstr(queuename(e, 'd')); 5455012Seric if ((tf = dfopen(e->e_df, "w")) == NULL) 551392Seric { 5655012Seric syserr("Cannot create %s", e->e_df); 575366Seric NoReturn = TRUE; 585366Seric finis(); 591392Seric } 6055012Seric (void) chmod(e->e_df, FileMode); 611392Seric 624316Seric /* 634322Seric ** Tell ARPANET to go ahead. 644322Seric */ 654322Seric 6652105Seric if (smtpmode) 674710Seric message("354", "Enter mail, end with \".\" on a line by itself"); 684322Seric 694322Seric /* 704316Seric ** Try to read a UNIX-style From line 714316Seric */ 724316Seric 7340965Sbostic if (sfgets(buf, MAXFIELD, InChannel) == NULL) 7440965Sbostic goto readerr; 754557Seric fixcrlf(buf, FALSE); 764321Seric # ifndef NOTUNIX 774322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 782900Seric { 7940965Sbostic if (!flusheol(buf, InChannel)) 8040965Sbostic goto readerr; 8155012Seric eatfrom(buf, e); 8240965Sbostic if (sfgets(buf, MAXFIELD, InChannel) == NULL) 8340965Sbostic goto readerr; 844557Seric fixcrlf(buf, FALSE); 852900Seric } 86*56795Seric # endif /* NOTUNIX */ 872900Seric 881392Seric /* 895975Seric ** Copy InChannel to temp file & do message editing. 901392Seric ** To keep certain mailers from getting confused, 911392Seric ** and to keep the output clean, lines that look 9213932Seric ** like UNIX "From" lines are deleted in the header. 931392Seric */ 941392Seric 9540965Sbostic workbuf = buf; /* `workbuf' contains a header field */ 9640965Sbostic freebuf = buf2; /* `freebuf' can be used for read-ahead */ 9740965Sbostic for (;;) 981392Seric { 9940965Sbostic /* first, see if the header is over */ 10040965Sbostic if (!isheader(workbuf)) 10140965Sbostic { 10240965Sbostic fixcrlf(workbuf, TRUE); 10319036Seric break; 10440965Sbostic } 10519036Seric 1067681Seric /* if the line is too long, throw the rest away */ 10740965Sbostic if (!flusheol(workbuf, InChannel)) 10840965Sbostic goto readerr; 1097681Seric 11040965Sbostic /* it's okay to toss '\n' now (flusheol() needed it) */ 11140965Sbostic fixcrlf(workbuf, TRUE); 1124557Seric 11340965Sbostic workbuflen = strlen(workbuf); 1142900Seric 1152900Seric /* get the rest of this field */ 11640965Sbostic for (;;) 1171392Seric { 11840965Sbostic if (sfgets(freebuf, MAXFIELD, InChannel) == NULL) 11940965Sbostic goto readerr; 12040965Sbostic 12140965Sbostic /* is this a continuation line? */ 12240965Sbostic if (*freebuf != ' ' && *freebuf != '\t') 1232900Seric break; 12440965Sbostic 12540965Sbostic if (!flusheol(freebuf, InChannel)) 12640965Sbostic goto readerr; 12740965Sbostic 12840965Sbostic /* yes; append line to `workbuf' if there's room */ 12940965Sbostic if (workbuflen < MAXFIELD-3) 13040965Sbostic { 13140965Sbostic register char *p = workbuf + workbuflen; 13240965Sbostic register char *q = freebuf; 13340965Sbostic 13440965Sbostic /* we have room for more of this field */ 13540965Sbostic fixcrlf(freebuf, TRUE); 13656678Seric *p++ = '\n'; 13756678Seric workbuflen++; 13840965Sbostic while(*q != '\0' && workbuflen < MAXFIELD-1) 13940965Sbostic { 14040965Sbostic *p++ = *q++; 14140965Sbostic workbuflen++; 14240965Sbostic } 14340965Sbostic *p = '\0'; 14440965Sbostic } 1451392Seric } 1461392Seric 14755012Seric e->e_msgsize += workbuflen; 1481392Seric 1492900Seric /* 15040965Sbostic ** The working buffer now becomes the free buffer, since 15140965Sbostic ** the free buffer contains a new header field. 15240965Sbostic ** 15340965Sbostic ** This is premature, since we still havent called 15440965Sbostic ** chompheader() to process the field we just created 15540965Sbostic ** (so the call to chompheader() will use `freebuf'). 15640965Sbostic ** This convolution is necessary so that if we break out 15740965Sbostic ** of the loop due to H_EOH, `workbuf' will always be 15840965Sbostic ** the next unprocessed buffer. 15940965Sbostic */ 16040965Sbostic 16140965Sbostic { 16240965Sbostic register char *tmp = workbuf; 16340965Sbostic workbuf = freebuf; 16440965Sbostic freebuf = tmp; 16540965Sbostic } 16640965Sbostic 16740965Sbostic /* 1682900Seric ** Snarf header away. 1692900Seric */ 1702900Seric 17155012Seric if (bitset(H_EOH, chompheader(freebuf, FALSE, e))) 1723058Seric break; 17340965Sbostic } 1741392Seric 1757673Seric if (tTd(30, 1)) 1762900Seric printf("EOH\n"); 1772900Seric 17840965Sbostic if (*workbuf == '\0') 17940965Sbostic { 18040965Sbostic /* throw away a blank line */ 18140965Sbostic if (sfgets(buf, MAXFIELD, InChannel) == NULL) 18240965Sbostic goto readerr; 18340965Sbostic } 18440965Sbostic else if (workbuf == buf2) /* guarantee `buf' contains data */ 18540965Sbostic (void) strcpy(buf, buf2); 1862900Seric 1872900Seric /* 1882900Seric ** Collect the body of the message. 1892900Seric */ 1902900Seric 19115532Seric do 1922900Seric { 1934551Seric register char *bp = buf; 1944156Seric 1957852Seric fixcrlf(buf, TRUE); 1964557Seric 1972900Seric /* check for end-of-message */ 19852105Seric if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 1992900Seric break; 2002900Seric 2014551Seric /* check for transparent dot */ 20252105Seric if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.') 2034551Seric bp++; 2044551Seric 2054156Seric /* 2064156Seric ** Figure message length, output the line to the temp 2074156Seric ** file, and insert a newline if missing. 2084156Seric */ 2094156Seric 21055012Seric e->e_msgsize += strlen(bp) + 1; 2114551Seric fputs(bp, tf); 2127852Seric fputs("\n", tf); 2131392Seric if (ferror(tf)) 21455012Seric tferror(tf, e); 21515532Seric } while (sfgets(buf, MAXFIELD, InChannel) != NULL); 21640965Sbostic 21740965Sbostic readerr: 21811544Seric if (fflush(tf) != 0) 21955012Seric tferror(tf, e); 2204083Seric (void) fclose(tf); 2212900Seric 22211145Seric /* An EOF when running SMTP is an error */ 22319036Seric if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP) 22416136Seric { 22540965Sbostic int usrerr(), syserr(); 22636233Skarels # ifdef LOG 22736233Skarels if (RealHostName != NULL && LogLevel > 0) 22836230Skarels syslog(LOG_NOTICE, 22936230Skarels "collect: unexpected close on connection from %s: %m\n", 23055012Seric e->e_from.q_paddr, RealHostName); 23136233Skarels # endif 23240965Sbostic (feof(InChannel) ? usrerr: syserr) 23355012Seric ("collect: unexpected close, from=%s", e->e_from.q_paddr); 23411145Seric 23516136Seric /* don't return an error indication */ 23655012Seric e->e_to = NULL; 23755012Seric e->e_flags &= ~EF_FATALERRS; 23816136Seric 23916136Seric /* and don't try to deliver the partial message either */ 24016136Seric finis(); 24116136Seric } 24216136Seric 2432900Seric /* 2442900Seric ** Find out some information from the headers. 2453386Seric ** Examples are who is the from person & the date. 2462900Seric */ 2472900Seric 24855012Seric eatheader(e); 2497673Seric 2507782Seric /* 2517782Seric ** Add an Apparently-To: line if we have no recipient lines. 2527782Seric */ 2534622Seric 25455012Seric if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL && 25555012Seric hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL) 2567367Seric { 2577367Seric register ADDRESS *q; 2587367Seric 2597367Seric /* create an Apparently-To: field */ 2607367Seric /* that or reject the message.... */ 26155012Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2627367Seric { 2637389Seric if (q->q_alias != NULL) 2647389Seric continue; 2657673Seric if (tTd(30, 3)) 2667367Seric printf("Adding Apparently-To: %s\n", q->q_paddr); 26755012Seric addheader("apparently-to", q->q_paddr, e); 2687367Seric } 2697367Seric } 2707367Seric 27155012Seric if ((e->e_dfp = fopen(e->e_df, "r")) == NULL) 27255012Seric syserr("Cannot reopen %s", e->e_df); 2731392Seric } 2741392Seric /* 27540965Sbostic ** FLUSHEOL -- if not at EOL, throw away rest of input line. 27640965Sbostic ** 27740965Sbostic ** Parameters: 27840965Sbostic ** buf -- last line read in (checked for '\n'), 27940965Sbostic ** fp -- file to be read from. 28040965Sbostic ** 28140965Sbostic ** Returns: 28240965Sbostic ** FALSE on error from sfgets(), TRUE otherwise. 28340965Sbostic ** 28440965Sbostic ** Side Effects: 28540965Sbostic ** none. 28640965Sbostic */ 28740965Sbostic 28840965Sbostic bool 28940965Sbostic flusheol(buf, fp) 29040965Sbostic char *buf; 29140965Sbostic FILE *fp; 29240965Sbostic { 29340965Sbostic char junkbuf[MAXLINE], *sfgets(); 29440965Sbostic register char *p = buf; 29540965Sbostic 296*56795Seric while (strchr(p, '\n') == NULL) { 29740965Sbostic if (sfgets(junkbuf,MAXLINE,fp) == NULL) 29840965Sbostic return(FALSE); 29940965Sbostic p = junkbuf; 30040965Sbostic } 30140965Sbostic 30240965Sbostic return(TRUE); 30340965Sbostic } 30440965Sbostic /* 30511544Seric ** TFERROR -- signal error on writing the temporary file. 30611544Seric ** 30711544Seric ** Parameters: 30811544Seric ** tf -- the file pointer for the temporary file. 30911544Seric ** 31011544Seric ** Returns: 31111544Seric ** none. 31211544Seric ** 31311544Seric ** Side Effects: 31411544Seric ** Gives an error message. 31511544Seric ** Arranges for following output to go elsewhere. 31611544Seric */ 31711544Seric 31855012Seric tferror(tf, e) 31911544Seric FILE *tf; 32055012Seric register ENVELOPE *e; 32111544Seric { 32211544Seric if (errno == ENOSPC) 32311544Seric { 32455012Seric (void) freopen(e->e_df, "w", tf); 32511544Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 32611544Seric usrerr("452 Out of disk space for temp file"); 32711544Seric } 32811544Seric else 32955012Seric syserr("collect: Cannot write %s", e->e_df); 33011544Seric (void) freopen("/dev/null", "w", tf); 33111544Seric } 33211544Seric /* 3332900Seric ** EATFROM -- chew up a UNIX style from line and process 3342900Seric ** 3352900Seric ** This does indeed make some assumptions about the format 3362900Seric ** of UNIX messages. 3372900Seric ** 3382900Seric ** Parameters: 3392900Seric ** fm -- the from line. 3402900Seric ** 3412900Seric ** Returns: 3422900Seric ** none. 3432900Seric ** 3442900Seric ** Side Effects: 3452900Seric ** extracts what information it can from the header, 3463386Seric ** such as the date. 3472900Seric */ 3482900Seric 3494321Seric # ifndef NOTUNIX 3504321Seric 3514203Seric char *DowList[] = 3524203Seric { 3534203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 3544203Seric }; 3554203Seric 3562900Seric char *MonthList[] = 3572900Seric { 3582900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 3592900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 3602900Seric NULL 3612900Seric }; 3622900Seric 36355012Seric eatfrom(fm, e) 3642900Seric char *fm; 36555012Seric register ENVELOPE *e; 3662900Seric { 3672900Seric register char *p; 3682900Seric register char **dt; 3692900Seric 3707673Seric if (tTd(30, 2)) 3714203Seric printf("eatfrom(%s)\n", fm); 3724203Seric 3732900Seric /* find the date part */ 3742900Seric p = fm; 3752900Seric while (*p != '\0') 3762900Seric { 3772900Seric /* skip a word */ 3782900Seric while (*p != '\0' && *p != ' ') 37916896Seric p++; 3802900Seric while (*p == ' ') 38116896Seric p++; 3822900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 3832900Seric continue; 3842900Seric 3852900Seric /* we have a possible date */ 3864203Seric for (dt = DowList; *dt != NULL; dt++) 3872900Seric if (strncmp(*dt, p, 3) == 0) 3882900Seric break; 3894203Seric if (*dt == NULL) 3904203Seric continue; 3912900Seric 3924203Seric for (dt = MonthList; *dt != NULL; dt++) 3934203Seric if (strncmp(*dt, &p[4], 3) == 0) 3944203Seric break; 3952900Seric if (*dt != NULL) 3962900Seric break; 3972900Seric } 3982900Seric 3992900Seric if (*p != NULL) 4002900Seric { 4013386Seric char *q; 4025366Seric extern char *arpadate(); 4033386Seric 4042900Seric /* we have found a date */ 4053386Seric q = xalloc(25); 40623103Seric (void) strncpy(q, p, 25); 4073386Seric q[24] = '\0'; 40855012Seric define('d', q, e); 4095366Seric q = arpadate(q); 41055012Seric define('a', newstr(q), e); 4112900Seric } 4122900Seric } 4134321Seric 414*56795Seric # endif /* NOTUNIX */ 415