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*64718Seric static char sccsid[] = "@(#)collect.c 8.5 (Berkeley) 10/15/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; 49*64718Seric 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 223*64718Seric 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); 247*64718Seric if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock, 248*64718Seric "message body read") == NULL) 249*64718Seric goto readerr; 250*64718Seric } 25140965Sbostic 252*64718Seric if (feof(InChannel) || ferror(InChannel)) 253*64718Seric { 25440965Sbostic readerr: 255*64718Seric inputerr = TRUE; 256*64718Seric } 257*64718Seric 25811544Seric if (fflush(tf) != 0) 25955012Seric tferror(tf, e); 26060603Seric (void) fsync(fileno(tf)); 2614083Seric (void) fclose(tf); 2622900Seric 26311145Seric /* An EOF when running SMTP is an error */ 264*64718Seric if (inputerr && OpMode == MD_SMTP) 26516136Seric { 26658308Seric char *host; 267*64718Seric char *problem; 26858082Seric 26958308Seric host = RealHostName; 27058308Seric if (host == NULL) 27158308Seric host = "localhost"; 27258308Seric 273*64718Seric if (feof(InChannel)) 274*64718Seric problem = "unexpected close"; 275*64718Seric else if (ferror(InChannel)) 276*64718Seric problem = "I/O error"; 277*64718Seric else 278*64718Seric problem = "read timeout"; 27936233Skarels # ifdef LOG 28058308Seric if (LogLevel > 0 && feof(InChannel)) 28136230Skarels syslog(LOG_NOTICE, 282*64718Seric "collect: %s on connection from %s, sender=%s: %m\n", 283*64718Seric problem, host, e->e_from.q_paddr); 28436233Skarels # endif 28558082Seric (feof(InChannel) ? usrerr : syserr) 286*64718Seric ("451 collect: %s on connection from %s, from=%s", 287*64718Seric problem, host, e->e_from.q_paddr); 28811145Seric 28916136Seric /* don't return an error indication */ 29055012Seric e->e_to = NULL; 29155012Seric e->e_flags &= ~EF_FATALERRS; 29264124Seric e->e_flags |= EF_CLRQUEUE; 29316136Seric 29416136Seric /* and don't try to deliver the partial message either */ 295*64718Seric if (InChild) 296*64718Seric ExitStat = EX_QUIT; 29716136Seric finis(); 29816136Seric } 29916136Seric 3002900Seric /* 3012900Seric ** Find out some information from the headers. 3023386Seric ** Examples are who is the from person & the date. 3032900Seric */ 3042900Seric 30558929Seric eatheader(e, !requeueflag); 3067673Seric 30764068Seric /* collect statistics */ 30864068Seric if (OpMode != MD_VERIFY) 30964068Seric markstats(e, (ADDRESS *) NULL); 31064068Seric 3117782Seric /* 3127782Seric ** Add an Apparently-To: line if we have no recipient lines. 3137782Seric */ 3144622Seric 31555012Seric if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL && 31655012Seric hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL) 3177367Seric { 3187367Seric register ADDRESS *q; 3197367Seric 3207367Seric /* create an Apparently-To: field */ 3217367Seric /* that or reject the message.... */ 32255012Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 3237367Seric { 3247389Seric if (q->q_alias != NULL) 3257389Seric continue; 3267673Seric if (tTd(30, 3)) 3277367Seric printf("Adding Apparently-To: %s\n", q->q_paddr); 32859579Seric addheader("Apparently-To", q->q_paddr, e); 3297367Seric } 3307367Seric } 3317367Seric 33259320Seric /* check for message too large */ 33359320Seric if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize) 33459320Seric { 33559320Seric usrerr("552 Message exceeds maximum fixed size (%ld)", 33659320Seric MaxMessageSize); 33759320Seric } 33859320Seric 33955012Seric if ((e->e_dfp = fopen(e->e_df, "r")) == NULL) 34058690Seric { 34158690Seric /* we haven't acked receipt yet, so just chuck this */ 34255012Seric syserr("Cannot reopen %s", e->e_df); 34358690Seric finis(); 34458690Seric } 3451392Seric } 3461392Seric /* 34740965Sbostic ** FLUSHEOL -- if not at EOL, throw away rest of input line. 34840965Sbostic ** 34940965Sbostic ** Parameters: 35040965Sbostic ** buf -- last line read in (checked for '\n'), 35140965Sbostic ** fp -- file to be read from. 35240965Sbostic ** 35340965Sbostic ** Returns: 35440965Sbostic ** FALSE on error from sfgets(), TRUE otherwise. 35540965Sbostic ** 35640965Sbostic ** Side Effects: 35740965Sbostic ** none. 35840965Sbostic */ 35940965Sbostic 36040965Sbostic bool 36140965Sbostic flusheol(buf, fp) 36240965Sbostic char *buf; 36340965Sbostic FILE *fp; 36440965Sbostic { 36540965Sbostic register char *p = buf; 36657134Seric bool printmsg = TRUE; 36757134Seric char junkbuf[MAXLINE]; 36840965Sbostic 36957134Seric while (strchr(p, '\n') == NULL) 37057134Seric { 37157134Seric if (printmsg) 37258151Seric usrerr("553 header line too long"); 37357134Seric printmsg = FALSE; 37461093Seric if (sfgets(junkbuf, MAXLINE, fp, TimeOuts.to_datablock, 37561093Seric "long line flush") == NULL) 37657134Seric return (FALSE); 37740965Sbostic p = junkbuf; 37840965Sbostic } 37940965Sbostic 38057134Seric return (TRUE); 38140965Sbostic } 38240965Sbostic /* 38311544Seric ** TFERROR -- signal error on writing the temporary file. 38411544Seric ** 38511544Seric ** Parameters: 38611544Seric ** tf -- the file pointer for the temporary file. 38711544Seric ** 38811544Seric ** Returns: 38911544Seric ** none. 39011544Seric ** 39111544Seric ** Side Effects: 39211544Seric ** Gives an error message. 39311544Seric ** Arranges for following output to go elsewhere. 39411544Seric */ 39511544Seric 39655012Seric tferror(tf, e) 39711544Seric FILE *tf; 39855012Seric register ENVELOPE *e; 39911544Seric { 40011544Seric if (errno == ENOSPC) 40111544Seric { 40255012Seric (void) freopen(e->e_df, "w", tf); 40311544Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 40411544Seric usrerr("452 Out of disk space for temp file"); 40511544Seric } 40611544Seric else 40755012Seric syserr("collect: Cannot write %s", e->e_df); 40811544Seric (void) freopen("/dev/null", "w", tf); 40911544Seric } 41011544Seric /* 4112900Seric ** EATFROM -- chew up a UNIX style from line and process 4122900Seric ** 4132900Seric ** This does indeed make some assumptions about the format 4142900Seric ** of UNIX messages. 4152900Seric ** 4162900Seric ** Parameters: 4172900Seric ** fm -- the from line. 4182900Seric ** 4192900Seric ** Returns: 4202900Seric ** none. 4212900Seric ** 4222900Seric ** Side Effects: 4232900Seric ** extracts what information it can from the header, 4243386Seric ** such as the date. 4252900Seric */ 4262900Seric 4274321Seric # ifndef NOTUNIX 4284321Seric 4294203Seric char *DowList[] = 4304203Seric { 4314203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 4324203Seric }; 4334203Seric 4342900Seric char *MonthList[] = 4352900Seric { 4362900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 4372900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 4382900Seric NULL 4392900Seric }; 4402900Seric 44155012Seric eatfrom(fm, e) 4422900Seric char *fm; 44355012Seric register ENVELOPE *e; 4442900Seric { 4452900Seric register char *p; 4462900Seric register char **dt; 4472900Seric 4487673Seric if (tTd(30, 2)) 4494203Seric printf("eatfrom(%s)\n", fm); 4504203Seric 4512900Seric /* find the date part */ 4522900Seric p = fm; 4532900Seric while (*p != '\0') 4542900Seric { 4552900Seric /* skip a word */ 4562900Seric while (*p != '\0' && *p != ' ') 45716896Seric p++; 4582900Seric while (*p == ' ') 45916896Seric p++; 46058050Seric if (!(isascii(*p) && isupper(*p)) || 46158050Seric p[3] != ' ' || p[13] != ':' || p[16] != ':') 4622900Seric continue; 4632900Seric 4642900Seric /* we have a possible date */ 4654203Seric for (dt = DowList; *dt != NULL; dt++) 4662900Seric if (strncmp(*dt, p, 3) == 0) 4672900Seric break; 4684203Seric if (*dt == NULL) 4694203Seric continue; 4702900Seric 4714203Seric for (dt = MonthList; *dt != NULL; dt++) 4724203Seric if (strncmp(*dt, &p[4], 3) == 0) 4734203Seric break; 4742900Seric if (*dt != NULL) 4752900Seric break; 4762900Seric } 4772900Seric 47860502Seric if (*p != '\0') 4792900Seric { 4803386Seric char *q; 4815366Seric extern char *arpadate(); 4823386Seric 4832900Seric /* we have found a date */ 4843386Seric q = xalloc(25); 48523103Seric (void) strncpy(q, p, 25); 4863386Seric q[24] = '\0'; 4875366Seric q = arpadate(q); 48855012Seric define('a', newstr(q), e); 4892900Seric } 4902900Seric } 4914321Seric 49256795Seric # endif /* NOTUNIX */ 493