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*58690Seric static char sccsid[] = "@(#)collect.c 6.10 (Berkeley) 03/17/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. 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; 4357135Seric 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) 6658151Seric message("354 Enter mail, end with \".\" on a line by itself"); 674322Seric 684322Seric /* 694316Seric ** Try to read a UNIX-style From line 704316Seric */ 714316Seric 7258112Seric if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) == 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); 8158112Seric if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) == 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 { 9857135Seric char *curbuf; 9957135Seric int curbuffree; 10057135Seric register int curbuflen; 10157135Seric char *p; 10257135Seric 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 11757135Seric curbuf = workbuf; 11857135Seric curbuflen = strlen(curbuf); 11957135Seric curbuffree = MAXLINE - curbuflen; 12057135Seric p = curbuf + curbuflen; 1212900Seric 1222900Seric /* get the rest of this field */ 12340965Sbostic for (;;) 1241392Seric { 12557135Seric int clen; 12657135Seric 12758112Seric if (sfgets(freebuf, MAXLINE, InChannel, TimeOuts.to_datablock) == 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 13757135Seric fixcrlf(freebuf, TRUE); 13857135Seric clen = strlen(freebuf) + 1; 13957135Seric 14057135Seric /* if insufficient room, dynamically allocate buffer */ 14157135Seric if (clen >= curbuffree) 14240965Sbostic { 14357135Seric /* reallocate buffer */ 14457135Seric int nbuflen = ((p - curbuf) + clen) * 2; 14557135Seric char *nbuf = xalloc(nbuflen); 14640965Sbostic 14757135Seric p = nbuf + curbuflen; 14857135Seric curbuffree = nbuflen - curbuflen; 14957135Seric bcopy(curbuf, nbuf, curbuflen); 15057135Seric if (curbuf != buf && curbuf != buf2) 15157135Seric free(curbuf); 15257135Seric curbuf = nbuf; 15340965Sbostic } 15457135Seric *p++ = '\n'; 15557135Seric bcopy(freebuf, p, clen - 1); 15657135Seric p += clen - 1; 15757135Seric curbuffree -= clen; 15857135Seric curbuflen += clen; 1591392Seric } 16057135Seric *p++ = '\0'; 1611392Seric 16257135Seric 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 18657135Seric if (bitset(H_EOH, chompheader(curbuf, FALSE, e))) 1873058Seric break; 18857135Seric 18957135Seric /* 19057135Seric ** If the buffer was dynamically allocated, free it. 19157135Seric */ 19257135Seric 19357135Seric if (curbuf != buf && curbuf != buf2) 19457135Seric 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 */ 20358112Seric if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) == 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); 23758112Seric } while (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) != 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 { 24758308Seric char *host; 24840965Sbostic int usrerr(), syserr(); 24958082Seric 25058308Seric host = RealHostName; 25158308Seric if (host == NULL) 25258308Seric host = "localhost"; 25358308Seric 25436233Skarels # ifdef LOG 25558308Seric if (LogLevel > 0 && feof(InChannel)) 25636230Skarels syslog(LOG_NOTICE, 25758308Seric "collect: unexpected close on connection from %s, sender=%s: %m\n", 25858308Seric host, e->e_from.q_paddr); 25936233Skarels # endif 26058082Seric (feof(InChannel) ? usrerr : syserr) 26158308Seric ("451 collect: unexpected close on connection from %s, from=%s", 26258308Seric host, e->e_from.q_paddr); 26311145Seric 26416136Seric /* don't return an error indication */ 26555012Seric e->e_to = NULL; 26655012Seric e->e_flags &= ~EF_FATALERRS; 26716136Seric 26816136Seric /* and don't try to deliver the partial message either */ 26916136Seric finis(); 27016136Seric } 27116136Seric 2722900Seric /* 2732900Seric ** Find out some information from the headers. 2743386Seric ** Examples are who is the from person & the date. 2752900Seric */ 2762900Seric 27757642Seric eatheader(e, QueueRun); 2787673Seric 2797782Seric /* 2807782Seric ** Add an Apparently-To: line if we have no recipient lines. 2817782Seric */ 2824622Seric 28355012Seric if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL && 28455012Seric hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL) 2857367Seric { 2867367Seric register ADDRESS *q; 2877367Seric 2887367Seric /* create an Apparently-To: field */ 2897367Seric /* that or reject the message.... */ 29055012Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2917367Seric { 2927389Seric if (q->q_alias != NULL) 2937389Seric continue; 2947673Seric if (tTd(30, 3)) 2957367Seric printf("Adding Apparently-To: %s\n", q->q_paddr); 29655012Seric addheader("apparently-to", q->q_paddr, e); 2977367Seric } 2987367Seric } 2997367Seric 30055012Seric if ((e->e_dfp = fopen(e->e_df, "r")) == NULL) 301*58690Seric { 302*58690Seric /* we haven't acked receipt yet, so just chuck this */ 30355012Seric syserr("Cannot reopen %s", e->e_df); 304*58690Seric finis(); 305*58690Seric } 3061392Seric } 3071392Seric /* 30840965Sbostic ** FLUSHEOL -- if not at EOL, throw away rest of input line. 30940965Sbostic ** 31040965Sbostic ** Parameters: 31140965Sbostic ** buf -- last line read in (checked for '\n'), 31240965Sbostic ** fp -- file to be read from. 31340965Sbostic ** 31440965Sbostic ** Returns: 31540965Sbostic ** FALSE on error from sfgets(), TRUE otherwise. 31640965Sbostic ** 31740965Sbostic ** Side Effects: 31840965Sbostic ** none. 31940965Sbostic */ 32040965Sbostic 32140965Sbostic bool 32240965Sbostic flusheol(buf, fp) 32340965Sbostic char *buf; 32440965Sbostic FILE *fp; 32540965Sbostic { 32640965Sbostic register char *p = buf; 32757134Seric bool printmsg = TRUE; 32857134Seric char junkbuf[MAXLINE]; 32957134Seric extern char *sfgets(); 33040965Sbostic 33157134Seric while (strchr(p, '\n') == NULL) 33257134Seric { 33357134Seric if (printmsg) 33458151Seric usrerr("553 header line too long"); 33557134Seric printmsg = FALSE; 33658112Seric if (sfgets(junkbuf, MAXLINE, fp, TimeOuts.to_datablock) == NULL) 33757134Seric return (FALSE); 33840965Sbostic p = junkbuf; 33940965Sbostic } 34040965Sbostic 34157134Seric return (TRUE); 34240965Sbostic } 34340965Sbostic /* 34411544Seric ** TFERROR -- signal error on writing the temporary file. 34511544Seric ** 34611544Seric ** Parameters: 34711544Seric ** tf -- the file pointer for the temporary file. 34811544Seric ** 34911544Seric ** Returns: 35011544Seric ** none. 35111544Seric ** 35211544Seric ** Side Effects: 35311544Seric ** Gives an error message. 35411544Seric ** Arranges for following output to go elsewhere. 35511544Seric */ 35611544Seric 35755012Seric tferror(tf, e) 35811544Seric FILE *tf; 35955012Seric register ENVELOPE *e; 36011544Seric { 36111544Seric if (errno == ENOSPC) 36211544Seric { 36355012Seric (void) freopen(e->e_df, "w", tf); 36411544Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 36511544Seric usrerr("452 Out of disk space for temp file"); 36611544Seric } 36711544Seric else 36855012Seric syserr("collect: Cannot write %s", e->e_df); 36911544Seric (void) freopen("/dev/null", "w", tf); 37011544Seric } 37111544Seric /* 3722900Seric ** EATFROM -- chew up a UNIX style from line and process 3732900Seric ** 3742900Seric ** This does indeed make some assumptions about the format 3752900Seric ** of UNIX messages. 3762900Seric ** 3772900Seric ** Parameters: 3782900Seric ** fm -- the from line. 3792900Seric ** 3802900Seric ** Returns: 3812900Seric ** none. 3822900Seric ** 3832900Seric ** Side Effects: 3842900Seric ** extracts what information it can from the header, 3853386Seric ** such as the date. 3862900Seric */ 3872900Seric 3884321Seric # ifndef NOTUNIX 3894321Seric 3904203Seric char *DowList[] = 3914203Seric { 3924203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 3934203Seric }; 3944203Seric 3952900Seric char *MonthList[] = 3962900Seric { 3972900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 3982900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 3992900Seric NULL 4002900Seric }; 4012900Seric 40255012Seric eatfrom(fm, e) 4032900Seric char *fm; 40455012Seric register ENVELOPE *e; 4052900Seric { 4062900Seric register char *p; 4072900Seric register char **dt; 4082900Seric 4097673Seric if (tTd(30, 2)) 4104203Seric printf("eatfrom(%s)\n", fm); 4114203Seric 4122900Seric /* find the date part */ 4132900Seric p = fm; 4142900Seric while (*p != '\0') 4152900Seric { 4162900Seric /* skip a word */ 4172900Seric while (*p != '\0' && *p != ' ') 41816896Seric p++; 4192900Seric while (*p == ' ') 42016896Seric p++; 42158050Seric if (!(isascii(*p) && isupper(*p)) || 42258050Seric p[3] != ' ' || p[13] != ':' || p[16] != ':') 4232900Seric continue; 4242900Seric 4252900Seric /* we have a possible date */ 4264203Seric for (dt = DowList; *dt != NULL; dt++) 4272900Seric if (strncmp(*dt, p, 3) == 0) 4282900Seric break; 4294203Seric if (*dt == NULL) 4304203Seric continue; 4312900Seric 4324203Seric for (dt = MonthList; *dt != NULL; dt++) 4334203Seric if (strncmp(*dt, &p[4], 3) == 0) 4344203Seric break; 4352900Seric if (*dt != NULL) 4362900Seric break; 4372900Seric } 4382900Seric 4392900Seric if (*p != NULL) 4402900Seric { 4413386Seric char *q; 4425366Seric extern char *arpadate(); 4433386Seric 4442900Seric /* we have found a date */ 4453386Seric q = xalloc(25); 44623103Seric (void) strncpy(q, p, 25); 4473386Seric q[24] = '\0'; 4485366Seric q = arpadate(q); 44955012Seric define('a', newstr(q), e); 4502900Seric } 4512900Seric } 4524321Seric 45356795Seric # endif /* NOTUNIX */ 454