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*57135Seric static char sccsid[] = "@(#)collect.c 5.16 (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; 43*57135Seric char buf[MAXLINE], buf2[MAXLINE]; 4440965Sbostic register char *workbuf, *freebuf; 452900Seric extern char *hvalue(); 4640965Sbostic extern bool isheader(), flusheol(); 471392Seric 481392Seric /* 491392Seric ** Create the temp file name and create the file. 501392Seric */ 511392Seric 5255012Seric e->e_df = newstr(queuename(e, 'd')); 5355012Seric if ((tf = dfopen(e->e_df, "w")) == NULL) 541392Seric { 5555012Seric syserr("Cannot create %s", e->e_df); 565366Seric NoReturn = TRUE; 575366Seric finis(); 581392Seric } 5955012Seric (void) chmod(e->e_df, FileMode); 601392Seric 614316Seric /* 624322Seric ** Tell ARPANET to go ahead. 634322Seric */ 644322Seric 6552105Seric if (smtpmode) 664710Seric message("354", "Enter mail, end with \".\" on a line by itself"); 674322Seric 684322Seric /* 694316Seric ** Try to read a UNIX-style From line 704316Seric */ 714316Seric 72*57135Seric if (sfgets(buf, MAXLINE, InChannel) == NULL) 7340965Sbostic goto readerr; 744557Seric fixcrlf(buf, FALSE); 754321Seric # ifndef NOTUNIX 764322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 772900Seric { 7840965Sbostic if (!flusheol(buf, InChannel)) 7940965Sbostic goto readerr; 8055012Seric eatfrom(buf, e); 81*57135Seric if (sfgets(buf, MAXLINE, InChannel) == NULL) 8240965Sbostic goto readerr; 834557Seric fixcrlf(buf, FALSE); 842900Seric } 8556795Seric # endif /* NOTUNIX */ 862900Seric 871392Seric /* 885975Seric ** Copy InChannel to temp file & do message editing. 891392Seric ** To keep certain mailers from getting confused, 901392Seric ** and to keep the output clean, lines that look 9113932Seric ** like UNIX "From" lines are deleted in the header. 921392Seric */ 931392Seric 9440965Sbostic workbuf = buf; /* `workbuf' contains a header field */ 9540965Sbostic freebuf = buf2; /* `freebuf' can be used for read-ahead */ 9640965Sbostic for (;;) 971392Seric { 98*57135Seric char *curbuf; 99*57135Seric int curbuffree; 100*57135Seric register int curbuflen; 101*57135Seric char *p; 102*57135Seric 10340965Sbostic /* first, see if the header is over */ 10440965Sbostic if (!isheader(workbuf)) 10540965Sbostic { 10640965Sbostic fixcrlf(workbuf, TRUE); 10719036Seric break; 10840965Sbostic } 10919036Seric 1107681Seric /* if the line is too long, throw the rest away */ 11140965Sbostic if (!flusheol(workbuf, InChannel)) 11240965Sbostic goto readerr; 1137681Seric 11440965Sbostic /* it's okay to toss '\n' now (flusheol() needed it) */ 11540965Sbostic fixcrlf(workbuf, TRUE); 1164557Seric 117*57135Seric curbuf = workbuf; 118*57135Seric curbuflen = strlen(curbuf); 119*57135Seric curbuffree = MAXLINE - curbuflen; 120*57135Seric p = curbuf + curbuflen; 1212900Seric 1222900Seric /* get the rest of this field */ 12340965Sbostic for (;;) 1241392Seric { 125*57135Seric int clen; 126*57135Seric 127*57135Seric if (sfgets(freebuf, MAXLINE, InChannel) == NULL) 12840965Sbostic goto readerr; 12940965Sbostic 13040965Sbostic /* is this a continuation line? */ 13140965Sbostic if (*freebuf != ' ' && *freebuf != '\t') 1322900Seric break; 13340965Sbostic 13440965Sbostic if (!flusheol(freebuf, InChannel)) 13540965Sbostic goto readerr; 13640965Sbostic 137*57135Seric fixcrlf(freebuf, TRUE); 138*57135Seric clen = strlen(freebuf) + 1; 139*57135Seric 140*57135Seric /* if insufficient room, dynamically allocate buffer */ 141*57135Seric if (clen >= curbuffree) 14240965Sbostic { 143*57135Seric /* reallocate buffer */ 144*57135Seric int nbuflen = ((p - curbuf) + clen) * 2; 145*57135Seric char *nbuf = xalloc(nbuflen); 14640965Sbostic 147*57135Seric p = nbuf + curbuflen; 148*57135Seric curbuffree = nbuflen - curbuflen; 149*57135Seric bcopy(curbuf, nbuf, curbuflen); 150*57135Seric if (curbuf != buf && curbuf != buf2) 151*57135Seric free(curbuf); 152*57135Seric curbuf = nbuf; 15340965Sbostic } 154*57135Seric *p++ = '\n'; 155*57135Seric bcopy(freebuf, p, clen - 1); 156*57135Seric p += clen - 1; 157*57135Seric curbuffree -= clen; 158*57135Seric curbuflen += clen; 1591392Seric } 160*57135Seric *p++ = '\0'; 1611392Seric 162*57135Seric e->e_msgsize += curbuflen; 1631392Seric 1642900Seric /* 16540965Sbostic ** The working buffer now becomes the free buffer, since 16640965Sbostic ** the free buffer contains a new header field. 16740965Sbostic ** 16840965Sbostic ** This is premature, since we still havent called 16940965Sbostic ** chompheader() to process the field we just created 17040965Sbostic ** (so the call to chompheader() will use `freebuf'). 17140965Sbostic ** This convolution is necessary so that if we break out 17240965Sbostic ** of the loop due to H_EOH, `workbuf' will always be 17340965Sbostic ** the next unprocessed buffer. 17440965Sbostic */ 17540965Sbostic 17640965Sbostic { 17740965Sbostic register char *tmp = workbuf; 17840965Sbostic workbuf = freebuf; 17940965Sbostic freebuf = tmp; 18040965Sbostic } 18140965Sbostic 18240965Sbostic /* 1832900Seric ** Snarf header away. 1842900Seric */ 1852900Seric 186*57135Seric if (bitset(H_EOH, chompheader(curbuf, FALSE, e))) 1873058Seric break; 188*57135Seric 189*57135Seric /* 190*57135Seric ** If the buffer was dynamically allocated, free it. 191*57135Seric */ 192*57135Seric 193*57135Seric if (curbuf != buf && curbuf != buf2) 194*57135Seric free(curbuf); 19540965Sbostic } 1961392Seric 1977673Seric if (tTd(30, 1)) 1982900Seric printf("EOH\n"); 1992900Seric 20040965Sbostic if (*workbuf == '\0') 20140965Sbostic { 20240965Sbostic /* throw away a blank line */ 203*57135Seric if (sfgets(buf, MAXLINE, InChannel) == NULL) 20440965Sbostic goto readerr; 20540965Sbostic } 20640965Sbostic else if (workbuf == buf2) /* guarantee `buf' contains data */ 20740965Sbostic (void) strcpy(buf, buf2); 2082900Seric 2092900Seric /* 2102900Seric ** Collect the body of the message. 2112900Seric */ 2122900Seric 21315532Seric do 2142900Seric { 2154551Seric register char *bp = buf; 2164156Seric 2177852Seric fixcrlf(buf, TRUE); 2184557Seric 2192900Seric /* check for end-of-message */ 22052105Seric if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 2212900Seric break; 2222900Seric 2234551Seric /* check for transparent dot */ 22452105Seric if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.') 2254551Seric bp++; 2264551Seric 2274156Seric /* 2284156Seric ** Figure message length, output the line to the temp 2294156Seric ** file, and insert a newline if missing. 2304156Seric */ 2314156Seric 23255012Seric e->e_msgsize += strlen(bp) + 1; 2334551Seric fputs(bp, tf); 2347852Seric fputs("\n", tf); 2351392Seric if (ferror(tf)) 23655012Seric tferror(tf, e); 237*57135Seric } while (sfgets(buf, MAXLINE, InChannel) != NULL); 23840965Sbostic 23940965Sbostic readerr: 24011544Seric if (fflush(tf) != 0) 24155012Seric tferror(tf, e); 2424083Seric (void) fclose(tf); 2432900Seric 24411145Seric /* An EOF when running SMTP is an error */ 24519036Seric if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP) 24616136Seric { 24740965Sbostic int usrerr(), syserr(); 24836233Skarels # ifdef LOG 24936233Skarels if (RealHostName != NULL && LogLevel > 0) 25036230Skarels syslog(LOG_NOTICE, 25136230Skarels "collect: unexpected close on connection from %s: %m\n", 25255012Seric e->e_from.q_paddr, RealHostName); 25336233Skarels # endif 25440965Sbostic (feof(InChannel) ? usrerr: syserr) 25555012Seric ("collect: unexpected close, from=%s", e->e_from.q_paddr); 25611145Seric 25716136Seric /* don't return an error indication */ 25855012Seric e->e_to = NULL; 25955012Seric e->e_flags &= ~EF_FATALERRS; 26016136Seric 26116136Seric /* and don't try to deliver the partial message either */ 26216136Seric finis(); 26316136Seric } 26416136Seric 2652900Seric /* 2662900Seric ** Find out some information from the headers. 2673386Seric ** Examples are who is the from person & the date. 2682900Seric */ 2692900Seric 27055012Seric eatheader(e); 2717673Seric 2727782Seric /* 2737782Seric ** Add an Apparently-To: line if we have no recipient lines. 2747782Seric */ 2754622Seric 27655012Seric if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL && 27755012Seric hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL) 2787367Seric { 2797367Seric register ADDRESS *q; 2807367Seric 2817367Seric /* create an Apparently-To: field */ 2827367Seric /* that or reject the message.... */ 28355012Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2847367Seric { 2857389Seric if (q->q_alias != NULL) 2867389Seric continue; 2877673Seric if (tTd(30, 3)) 2887367Seric printf("Adding Apparently-To: %s\n", q->q_paddr); 28955012Seric addheader("apparently-to", q->q_paddr, e); 2907367Seric } 2917367Seric } 2927367Seric 29355012Seric if ((e->e_dfp = fopen(e->e_df, "r")) == NULL) 29455012Seric syserr("Cannot reopen %s", e->e_df); 2951392Seric } 2961392Seric /* 29740965Sbostic ** FLUSHEOL -- if not at EOL, throw away rest of input line. 29840965Sbostic ** 29940965Sbostic ** Parameters: 30040965Sbostic ** buf -- last line read in (checked for '\n'), 30140965Sbostic ** fp -- file to be read from. 30240965Sbostic ** 30340965Sbostic ** Returns: 30440965Sbostic ** FALSE on error from sfgets(), TRUE otherwise. 30540965Sbostic ** 30640965Sbostic ** Side Effects: 30740965Sbostic ** none. 30840965Sbostic */ 30940965Sbostic 31040965Sbostic bool 31140965Sbostic flusheol(buf, fp) 31240965Sbostic char *buf; 31340965Sbostic FILE *fp; 31440965Sbostic { 31540965Sbostic register char *p = buf; 31657134Seric bool printmsg = TRUE; 31757134Seric char junkbuf[MAXLINE]; 31857134Seric extern char *sfgets(); 31940965Sbostic 32057134Seric while (strchr(p, '\n') == NULL) 32157134Seric { 32257134Seric if (printmsg) 32357134Seric usrerr("header line too long"); 32457134Seric printmsg = FALSE; 32557134Seric if (sfgets(junkbuf, MAXLINE, fp) == NULL) 32657134Seric return (FALSE); 32740965Sbostic p = junkbuf; 32840965Sbostic } 32940965Sbostic 33057134Seric return (TRUE); 33140965Sbostic } 33240965Sbostic /* 33311544Seric ** TFERROR -- signal error on writing the temporary file. 33411544Seric ** 33511544Seric ** Parameters: 33611544Seric ** tf -- the file pointer for the temporary file. 33711544Seric ** 33811544Seric ** Returns: 33911544Seric ** none. 34011544Seric ** 34111544Seric ** Side Effects: 34211544Seric ** Gives an error message. 34311544Seric ** Arranges for following output to go elsewhere. 34411544Seric */ 34511544Seric 34655012Seric tferror(tf, e) 34711544Seric FILE *tf; 34855012Seric register ENVELOPE *e; 34911544Seric { 35011544Seric if (errno == ENOSPC) 35111544Seric { 35255012Seric (void) freopen(e->e_df, "w", tf); 35311544Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 35411544Seric usrerr("452 Out of disk space for temp file"); 35511544Seric } 35611544Seric else 35755012Seric syserr("collect: Cannot write %s", e->e_df); 35811544Seric (void) freopen("/dev/null", "w", tf); 35911544Seric } 36011544Seric /* 3612900Seric ** EATFROM -- chew up a UNIX style from line and process 3622900Seric ** 3632900Seric ** This does indeed make some assumptions about the format 3642900Seric ** of UNIX messages. 3652900Seric ** 3662900Seric ** Parameters: 3672900Seric ** fm -- the from line. 3682900Seric ** 3692900Seric ** Returns: 3702900Seric ** none. 3712900Seric ** 3722900Seric ** Side Effects: 3732900Seric ** extracts what information it can from the header, 3743386Seric ** such as the date. 3752900Seric */ 3762900Seric 3774321Seric # ifndef NOTUNIX 3784321Seric 3794203Seric char *DowList[] = 3804203Seric { 3814203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 3824203Seric }; 3834203Seric 3842900Seric char *MonthList[] = 3852900Seric { 3862900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 3872900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 3882900Seric NULL 3892900Seric }; 3902900Seric 39155012Seric eatfrom(fm, e) 3922900Seric char *fm; 39355012Seric register ENVELOPE *e; 3942900Seric { 3952900Seric register char *p; 3962900Seric register char **dt; 3972900Seric 3987673Seric if (tTd(30, 2)) 3994203Seric printf("eatfrom(%s)\n", fm); 4004203Seric 4012900Seric /* find the date part */ 4022900Seric p = fm; 4032900Seric while (*p != '\0') 4042900Seric { 4052900Seric /* skip a word */ 4062900Seric while (*p != '\0' && *p != ' ') 40716896Seric p++; 4082900Seric while (*p == ' ') 40916896Seric p++; 4102900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 4112900Seric continue; 4122900Seric 4132900Seric /* we have a possible date */ 4144203Seric for (dt = DowList; *dt != NULL; dt++) 4152900Seric if (strncmp(*dt, p, 3) == 0) 4162900Seric break; 4174203Seric if (*dt == NULL) 4184203Seric continue; 4192900Seric 4204203Seric for (dt = MonthList; *dt != NULL; dt++) 4214203Seric if (strncmp(*dt, &p[4], 3) == 0) 4224203Seric break; 4232900Seric if (*dt != NULL) 4242900Seric break; 4252900Seric } 4262900Seric 4272900Seric if (*p != NULL) 4282900Seric { 4293386Seric char *q; 4305366Seric extern char *arpadate(); 4313386Seric 4322900Seric /* we have found a date */ 4333386Seric q = xalloc(25); 43423103Seric (void) strncpy(q, p, 25); 4353386Seric q[24] = '\0'; 43655012Seric define('d', q, e); 4375366Seric q = arpadate(q); 43855012Seric define('a', newstr(q), e); 4392900Seric } 4402900Seric } 4414321Seric 44256795Seric # endif /* NOTUNIX */ 443