122697Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 362522Sbostic * Copyright (c) 1988, 1993 462522Sbostic * The Regents of the University of California. All rights reserved. 533728Sbostic * 642824Sbostic * %sccs.include.redist.c% 733728Sbostic */ 822697Sdist 922697Sdist #ifndef lint 10*64762Seric static char sccsid[] = "@(#)collect.c 8.6 (Berkeley) 10/27/93"; 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. 2858929Seric ** requeueflag -- this message will be requeued later, so 2958929Seric ** don't do final processing on it. 3058929Seric ** e -- the current envelope. 311392Seric ** 321392Seric ** Returns: 334162Seric ** none. 341392Seric ** 351392Seric ** Side Effects: 361392Seric ** Temp file is created and filled. 374162Seric ** The from person may be set. 381392Seric */ 391392Seric 4058929Seric collect(smtpmode, requeueflag, e) 4152105Seric bool smtpmode; 4258929Seric bool requeueflag; 4355012Seric register ENVELOPE *e; 441392Seric { 451392Seric register FILE *tf; 4652105Seric bool ignrdot = smtpmode ? FALSE : IgnrDot; 4757135Seric char buf[MAXLINE], buf2[MAXLINE]; 4840965Sbostic register char *workbuf, *freebuf; 4964718Seric bool inputerr = FALSE; 502900Seric extern char *hvalue(); 5140965Sbostic extern bool isheader(), flusheol(); 521392Seric 531392Seric /* 541392Seric ** Create the temp file name and create the file. 551392Seric */ 561392Seric 5764086Seric e->e_df = queuename(e, 'd'); 5864086Seric e->e_df = newstr(e->e_df); 5959745Seric if ((tf = dfopen(e->e_df, O_WRONLY|O_CREAT, FileMode)) == NULL) 601392Seric { 6155012Seric syserr("Cannot create %s", e->e_df); 625366Seric NoReturn = TRUE; 635366Seric finis(); 641392Seric } 651392Seric 664316Seric /* 674322Seric ** Tell ARPANET to go ahead. 684322Seric */ 694322Seric 7052105Seric if (smtpmode) 7158151Seric message("354 Enter mail, end with \".\" on a line by itself"); 724322Seric 734322Seric /* 744316Seric ** Try to read a UNIX-style From line 754316Seric */ 764316Seric 7761093Seric if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock, 7861093Seric "initial message read") == NULL) 7940965Sbostic goto readerr; 804557Seric fixcrlf(buf, FALSE); 814321Seric # ifndef NOTUNIX 824322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 832900Seric { 8440965Sbostic if (!flusheol(buf, InChannel)) 8540965Sbostic goto readerr; 8655012Seric eatfrom(buf, e); 8761093Seric if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock, 8861093Seric "message header read") == NULL) 8940965Sbostic goto readerr; 904557Seric fixcrlf(buf, FALSE); 912900Seric } 9256795Seric # endif /* NOTUNIX */ 932900Seric 941392Seric /* 955975Seric ** Copy InChannel to temp file & do message editing. 961392Seric ** To keep certain mailers from getting confused, 971392Seric ** and to keep the output clean, lines that look 9813932Seric ** like UNIX "From" lines are deleted in the header. 991392Seric */ 1001392Seric 10140965Sbostic workbuf = buf; /* `workbuf' contains a header field */ 10240965Sbostic freebuf = buf2; /* `freebuf' can be used for read-ahead */ 10340965Sbostic for (;;) 1041392Seric { 10557135Seric char *curbuf; 10657135Seric int curbuffree; 10757135Seric register int curbuflen; 10857135Seric char *p; 10957135Seric 11040965Sbostic /* first, see if the header is over */ 11140965Sbostic if (!isheader(workbuf)) 11240965Sbostic { 11340965Sbostic fixcrlf(workbuf, TRUE); 11419036Seric break; 11540965Sbostic } 11619036Seric 1177681Seric /* if the line is too long, throw the rest away */ 11840965Sbostic if (!flusheol(workbuf, InChannel)) 11940965Sbostic goto readerr; 1207681Seric 12140965Sbostic /* it's okay to toss '\n' now (flusheol() needed it) */ 12240965Sbostic fixcrlf(workbuf, TRUE); 1234557Seric 12457135Seric curbuf = workbuf; 12557135Seric curbuflen = strlen(curbuf); 12657135Seric curbuffree = MAXLINE - curbuflen; 12757135Seric p = curbuf + curbuflen; 1282900Seric 1292900Seric /* get the rest of this field */ 13040965Sbostic for (;;) 1311392Seric { 13257135Seric int clen; 13357135Seric 13461093Seric if (sfgets(freebuf, MAXLINE, InChannel, 13561093Seric TimeOuts.to_datablock, 13661093Seric "message header read") == NULL) 13740965Sbostic goto readerr; 13840965Sbostic 13940965Sbostic /* is this a continuation line? */ 14040965Sbostic if (*freebuf != ' ' && *freebuf != '\t') 1412900Seric break; 14240965Sbostic 14340965Sbostic if (!flusheol(freebuf, InChannel)) 14440965Sbostic goto readerr; 14540965Sbostic 14657135Seric fixcrlf(freebuf, TRUE); 14757135Seric clen = strlen(freebuf) + 1; 14857135Seric 14957135Seric /* if insufficient room, dynamically allocate buffer */ 15057135Seric if (clen >= curbuffree) 15140965Sbostic { 15257135Seric /* reallocate buffer */ 15357135Seric int nbuflen = ((p - curbuf) + clen) * 2; 15457135Seric char *nbuf = xalloc(nbuflen); 15540965Sbostic 15657135Seric p = nbuf + curbuflen; 15757135Seric curbuffree = nbuflen - curbuflen; 15857135Seric bcopy(curbuf, nbuf, curbuflen); 15957135Seric if (curbuf != buf && curbuf != buf2) 16057135Seric free(curbuf); 16157135Seric curbuf = nbuf; 16240965Sbostic } 16357135Seric *p++ = '\n'; 16457135Seric bcopy(freebuf, p, clen - 1); 16557135Seric p += clen - 1; 16657135Seric curbuffree -= clen; 16757135Seric curbuflen += clen; 1681392Seric } 16957135Seric *p++ = '\0'; 1701392Seric 17157135Seric e->e_msgsize += curbuflen; 1721392Seric 1732900Seric /* 17440965Sbostic ** The working buffer now becomes the free buffer, since 17540965Sbostic ** the free buffer contains a new header field. 17640965Sbostic ** 17740965Sbostic ** This is premature, since we still havent called 17840965Sbostic ** chompheader() to process the field we just created 17940965Sbostic ** (so the call to chompheader() will use `freebuf'). 18040965Sbostic ** This convolution is necessary so that if we break out 18140965Sbostic ** of the loop due to H_EOH, `workbuf' will always be 18240965Sbostic ** the next unprocessed buffer. 18340965Sbostic */ 18440965Sbostic 18540965Sbostic { 18640965Sbostic register char *tmp = workbuf; 18740965Sbostic workbuf = freebuf; 18840965Sbostic freebuf = tmp; 18940965Sbostic } 19040965Sbostic 19140965Sbostic /* 1922900Seric ** Snarf header away. 1932900Seric */ 1942900Seric 19557135Seric if (bitset(H_EOH, chompheader(curbuf, FALSE, e))) 1963058Seric break; 19757135Seric 19857135Seric /* 19957135Seric ** If the buffer was dynamically allocated, free it. 20057135Seric */ 20157135Seric 20257135Seric if (curbuf != buf && curbuf != buf2) 20357135Seric free(curbuf); 20440965Sbostic } 2051392Seric 2067673Seric if (tTd(30, 1)) 2072900Seric printf("EOH\n"); 2082900Seric 20940965Sbostic if (*workbuf == '\0') 21040965Sbostic { 21140965Sbostic /* throw away a blank line */ 21261093Seric if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock, 21361093Seric "message separator read") == NULL) 21440965Sbostic goto readerr; 21540965Sbostic } 21640965Sbostic else if (workbuf == buf2) /* guarantee `buf' contains data */ 21740965Sbostic (void) strcpy(buf, buf2); 2182900Seric 2192900Seric /* 2202900Seric ** Collect the body of the message. 2212900Seric */ 2222900Seric 22364718Seric for (;;) 2242900Seric { 2254551Seric register char *bp = buf; 2264156Seric 2277852Seric fixcrlf(buf, TRUE); 2284557Seric 2292900Seric /* check for end-of-message */ 23052105Seric if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 2312900Seric break; 2322900Seric 2334551Seric /* check for transparent dot */ 23452105Seric if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.') 2354551Seric bp++; 2364551Seric 2374156Seric /* 2384156Seric ** Figure message length, output the line to the temp 2394156Seric ** file, and insert a newline if missing. 2404156Seric */ 2414156Seric 24255012Seric e->e_msgsize += strlen(bp) + 1; 2434551Seric fputs(bp, tf); 2447852Seric fputs("\n", tf); 2451392Seric if (ferror(tf)) 24655012Seric tferror(tf, e); 24764718Seric if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock, 24864718Seric "message body read") == NULL) 24964718Seric goto readerr; 25064718Seric } 25140965Sbostic 25264718Seric if (feof(InChannel) || ferror(InChannel)) 25364718Seric { 25440965Sbostic readerr: 25564718Seric inputerr = TRUE; 25664718Seric } 25764718Seric 25811544Seric if (fflush(tf) != 0) 25955012Seric tferror(tf, e); 260*64762Seric if (fsync(fileno(tf)) < 0 || fclose(tf) < 0) 261*64762Seric { 262*64762Seric syserr("cannot sync message data to disk (%s)", e->e_df); 263*64762Seric finis(); 264*64762Seric } 2652900Seric 26611145Seric /* An EOF when running SMTP is an error */ 26764718Seric if (inputerr && OpMode == MD_SMTP) 26816136Seric { 26958308Seric char *host; 27064718Seric char *problem; 27158082Seric 27258308Seric host = RealHostName; 27358308Seric if (host == NULL) 27458308Seric host = "localhost"; 27558308Seric 27664718Seric if (feof(InChannel)) 27764718Seric problem = "unexpected close"; 27864718Seric else if (ferror(InChannel)) 27964718Seric problem = "I/O error"; 28064718Seric else 28164718Seric problem = "read timeout"; 28236233Skarels # ifdef LOG 28358308Seric if (LogLevel > 0 && feof(InChannel)) 28436230Skarels syslog(LOG_NOTICE, 28564718Seric "collect: %s on connection from %s, sender=%s: %m\n", 28664718Seric problem, host, e->e_from.q_paddr); 28736233Skarels # endif 28858082Seric (feof(InChannel) ? usrerr : syserr) 28964718Seric ("451 collect: %s on connection from %s, from=%s", 29064718Seric problem, host, e->e_from.q_paddr); 29111145Seric 29216136Seric /* don't return an error indication */ 29355012Seric e->e_to = NULL; 29455012Seric e->e_flags &= ~EF_FATALERRS; 29564124Seric e->e_flags |= EF_CLRQUEUE; 29616136Seric 29716136Seric /* and don't try to deliver the partial message either */ 29864718Seric if (InChild) 29964718Seric ExitStat = EX_QUIT; 30016136Seric finis(); 30116136Seric } 30216136Seric 3032900Seric /* 3042900Seric ** Find out some information from the headers. 3053386Seric ** Examples are who is the from person & the date. 3062900Seric */ 3072900Seric 30858929Seric eatheader(e, !requeueflag); 3097673Seric 31064068Seric /* collect statistics */ 31164068Seric if (OpMode != MD_VERIFY) 31264068Seric markstats(e, (ADDRESS *) NULL); 31364068Seric 3147782Seric /* 3157782Seric ** Add an Apparently-To: line if we have no recipient lines. 3167782Seric */ 3174622Seric 31855012Seric if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL && 31955012Seric hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL) 3207367Seric { 3217367Seric register ADDRESS *q; 3227367Seric 3237367Seric /* create an Apparently-To: field */ 3247367Seric /* that or reject the message.... */ 32555012Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 3267367Seric { 3277389Seric if (q->q_alias != NULL) 3287389Seric continue; 3297673Seric if (tTd(30, 3)) 3307367Seric printf("Adding Apparently-To: %s\n", q->q_paddr); 33159579Seric addheader("Apparently-To", q->q_paddr, e); 3327367Seric } 3337367Seric } 3347367Seric 33559320Seric /* check for message too large */ 33659320Seric if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize) 33759320Seric { 33859320Seric usrerr("552 Message exceeds maximum fixed size (%ld)", 33959320Seric MaxMessageSize); 34059320Seric } 34159320Seric 34255012Seric if ((e->e_dfp = fopen(e->e_df, "r")) == NULL) 34358690Seric { 34458690Seric /* we haven't acked receipt yet, so just chuck this */ 34555012Seric syserr("Cannot reopen %s", e->e_df); 34658690Seric finis(); 34758690Seric } 3481392Seric } 3491392Seric /* 35040965Sbostic ** FLUSHEOL -- if not at EOL, throw away rest of input line. 35140965Sbostic ** 35240965Sbostic ** Parameters: 35340965Sbostic ** buf -- last line read in (checked for '\n'), 35440965Sbostic ** fp -- file to be read from. 35540965Sbostic ** 35640965Sbostic ** Returns: 35740965Sbostic ** FALSE on error from sfgets(), TRUE otherwise. 35840965Sbostic ** 35940965Sbostic ** Side Effects: 36040965Sbostic ** none. 36140965Sbostic */ 36240965Sbostic 36340965Sbostic bool 36440965Sbostic flusheol(buf, fp) 36540965Sbostic char *buf; 36640965Sbostic FILE *fp; 36740965Sbostic { 36840965Sbostic register char *p = buf; 36957134Seric bool printmsg = TRUE; 37057134Seric char junkbuf[MAXLINE]; 37140965Sbostic 37257134Seric while (strchr(p, '\n') == NULL) 37357134Seric { 37457134Seric if (printmsg) 37558151Seric usrerr("553 header line too long"); 37657134Seric printmsg = FALSE; 37761093Seric if (sfgets(junkbuf, MAXLINE, fp, TimeOuts.to_datablock, 37861093Seric "long line flush") == NULL) 37957134Seric return (FALSE); 38040965Sbostic p = junkbuf; 38140965Sbostic } 38240965Sbostic 38357134Seric return (TRUE); 38440965Sbostic } 38540965Sbostic /* 38611544Seric ** TFERROR -- signal error on writing the temporary file. 38711544Seric ** 38811544Seric ** Parameters: 38911544Seric ** tf -- the file pointer for the temporary file. 39011544Seric ** 39111544Seric ** Returns: 39211544Seric ** none. 39311544Seric ** 39411544Seric ** Side Effects: 39511544Seric ** Gives an error message. 39611544Seric ** Arranges for following output to go elsewhere. 39711544Seric */ 39811544Seric 39955012Seric tferror(tf, e) 40011544Seric FILE *tf; 40155012Seric register ENVELOPE *e; 40211544Seric { 40311544Seric if (errno == ENOSPC) 40411544Seric { 40555012Seric (void) freopen(e->e_df, "w", tf); 40611544Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 40711544Seric usrerr("452 Out of disk space for temp file"); 40811544Seric } 40911544Seric else 41055012Seric syserr("collect: Cannot write %s", e->e_df); 41111544Seric (void) freopen("/dev/null", "w", tf); 41211544Seric } 41311544Seric /* 4142900Seric ** EATFROM -- chew up a UNIX style from line and process 4152900Seric ** 4162900Seric ** This does indeed make some assumptions about the format 4172900Seric ** of UNIX messages. 4182900Seric ** 4192900Seric ** Parameters: 4202900Seric ** fm -- the from line. 4212900Seric ** 4222900Seric ** Returns: 4232900Seric ** none. 4242900Seric ** 4252900Seric ** Side Effects: 4262900Seric ** extracts what information it can from the header, 4273386Seric ** such as the date. 4282900Seric */ 4292900Seric 4304321Seric # ifndef NOTUNIX 4314321Seric 4324203Seric char *DowList[] = 4334203Seric { 4344203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 4354203Seric }; 4364203Seric 4372900Seric char *MonthList[] = 4382900Seric { 4392900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 4402900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 4412900Seric NULL 4422900Seric }; 4432900Seric 44455012Seric eatfrom(fm, e) 4452900Seric char *fm; 44655012Seric register ENVELOPE *e; 4472900Seric { 4482900Seric register char *p; 4492900Seric register char **dt; 4502900Seric 4517673Seric if (tTd(30, 2)) 4524203Seric printf("eatfrom(%s)\n", fm); 4534203Seric 4542900Seric /* find the date part */ 4552900Seric p = fm; 4562900Seric while (*p != '\0') 4572900Seric { 4582900Seric /* skip a word */ 4592900Seric while (*p != '\0' && *p != ' ') 46016896Seric p++; 4612900Seric while (*p == ' ') 46216896Seric p++; 46358050Seric if (!(isascii(*p) && isupper(*p)) || 46458050Seric p[3] != ' ' || p[13] != ':' || p[16] != ':') 4652900Seric continue; 4662900Seric 4672900Seric /* we have a possible date */ 4684203Seric for (dt = DowList; *dt != NULL; dt++) 4692900Seric if (strncmp(*dt, p, 3) == 0) 4702900Seric break; 4714203Seric if (*dt == NULL) 4724203Seric continue; 4732900Seric 4744203Seric for (dt = MonthList; *dt != NULL; dt++) 4754203Seric if (strncmp(*dt, &p[4], 3) == 0) 4764203Seric break; 4772900Seric if (*dt != NULL) 4782900Seric break; 4792900Seric } 4802900Seric 48160502Seric if (*p != '\0') 4822900Seric { 4833386Seric char *q; 4845366Seric extern char *arpadate(); 4853386Seric 4862900Seric /* we have found a date */ 4873386Seric q = xalloc(25); 48823103Seric (void) strncpy(q, p, 25); 4893386Seric q[24] = '\0'; 4905366Seric q = arpadate(q); 49155012Seric define('a', newstr(q), e); 4922900Seric } 4932900Seric } 4944321Seric 49556795Seric # endif /* NOTUNIX */ 496