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*57134Seric static char sccsid[] = "@(#)collect.c 5.15 (Berkeley) 12/15/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; 45*57134Seric 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 } 8656795Seric # 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 113*57134Seric 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 128*57134Seric /* yes; append line to `workbuf' if there's room */ 129*57134Seric if (workbuflen < MAXFIELD-3) 13040965Sbostic { 131*57134Seric register char *p = workbuf + workbuflen; 132*57134Seric register char *q = freebuf; 13340965Sbostic 134*57134Seric /* we have room for more of this field */ 135*57134Seric fixcrlf(freebuf, TRUE); 136*57134Seric *p++ = '\n'; 137*57134Seric workbuflen++; 138*57134Seric while(*q != '\0' && workbuflen < MAXFIELD-1) 139*57134Seric { 140*57134Seric *p++ = *q++; 141*57134Seric workbuflen++; 142*57134Seric } 143*57134Seric *p = '\0'; 14440965Sbostic } 1451392Seric } 1461392Seric 147*57134Seric 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 171*57134Seric 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 register char *p = buf; 294*57134Seric bool printmsg = TRUE; 295*57134Seric char junkbuf[MAXLINE]; 296*57134Seric extern char *sfgets(); 29740965Sbostic 298*57134Seric while (strchr(p, '\n') == NULL) 299*57134Seric { 300*57134Seric if (printmsg) 301*57134Seric usrerr("header line too long"); 302*57134Seric printmsg = FALSE; 303*57134Seric if (sfgets(junkbuf, MAXLINE, fp) == NULL) 304*57134Seric return (FALSE); 30540965Sbostic p = junkbuf; 30640965Sbostic } 30740965Sbostic 308*57134Seric return (TRUE); 30940965Sbostic } 31040965Sbostic /* 31111544Seric ** TFERROR -- signal error on writing the temporary file. 31211544Seric ** 31311544Seric ** Parameters: 31411544Seric ** tf -- the file pointer for the temporary file. 31511544Seric ** 31611544Seric ** Returns: 31711544Seric ** none. 31811544Seric ** 31911544Seric ** Side Effects: 32011544Seric ** Gives an error message. 32111544Seric ** Arranges for following output to go elsewhere. 32211544Seric */ 32311544Seric 32455012Seric tferror(tf, e) 32511544Seric FILE *tf; 32655012Seric register ENVELOPE *e; 32711544Seric { 32811544Seric if (errno == ENOSPC) 32911544Seric { 33055012Seric (void) freopen(e->e_df, "w", tf); 33111544Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 33211544Seric usrerr("452 Out of disk space for temp file"); 33311544Seric } 33411544Seric else 33555012Seric syserr("collect: Cannot write %s", e->e_df); 33611544Seric (void) freopen("/dev/null", "w", tf); 33711544Seric } 33811544Seric /* 3392900Seric ** EATFROM -- chew up a UNIX style from line and process 3402900Seric ** 3412900Seric ** This does indeed make some assumptions about the format 3422900Seric ** of UNIX messages. 3432900Seric ** 3442900Seric ** Parameters: 3452900Seric ** fm -- the from line. 3462900Seric ** 3472900Seric ** Returns: 3482900Seric ** none. 3492900Seric ** 3502900Seric ** Side Effects: 3512900Seric ** extracts what information it can from the header, 3523386Seric ** such as the date. 3532900Seric */ 3542900Seric 3554321Seric # ifndef NOTUNIX 3564321Seric 3574203Seric char *DowList[] = 3584203Seric { 3594203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 3604203Seric }; 3614203Seric 3622900Seric char *MonthList[] = 3632900Seric { 3642900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 3652900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 3662900Seric NULL 3672900Seric }; 3682900Seric 36955012Seric eatfrom(fm, e) 3702900Seric char *fm; 37155012Seric register ENVELOPE *e; 3722900Seric { 3732900Seric register char *p; 3742900Seric register char **dt; 3752900Seric 3767673Seric if (tTd(30, 2)) 3774203Seric printf("eatfrom(%s)\n", fm); 3784203Seric 3792900Seric /* find the date part */ 3802900Seric p = fm; 3812900Seric while (*p != '\0') 3822900Seric { 3832900Seric /* skip a word */ 3842900Seric while (*p != '\0' && *p != ' ') 38516896Seric p++; 3862900Seric while (*p == ' ') 38716896Seric p++; 3882900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 3892900Seric continue; 3902900Seric 3912900Seric /* we have a possible date */ 3924203Seric for (dt = DowList; *dt != NULL; dt++) 3932900Seric if (strncmp(*dt, p, 3) == 0) 3942900Seric break; 3954203Seric if (*dt == NULL) 3964203Seric continue; 3972900Seric 3984203Seric for (dt = MonthList; *dt != NULL; dt++) 3994203Seric if (strncmp(*dt, &p[4], 3) == 0) 4004203Seric break; 4012900Seric if (*dt != NULL) 4022900Seric break; 4032900Seric } 4042900Seric 4052900Seric if (*p != NULL) 4062900Seric { 4073386Seric char *q; 4085366Seric extern char *arpadate(); 4093386Seric 4102900Seric /* we have found a date */ 4113386Seric q = xalloc(25); 41223103Seric (void) strncpy(q, p, 25); 4133386Seric q[24] = '\0'; 41455012Seric define('d', q, e); 4155366Seric q = arpadate(q); 41655012Seric define('a', newstr(q), e); 4172900Seric } 4182900Seric } 4194321Seric 42056795Seric # endif /* NOTUNIX */ 421