122697Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 333728Sbostic * Copyright (c) 1988 Regents of the University of California. 433728Sbostic * All rights reserved. 533728Sbostic * 633728Sbostic * Redistribution and use in source and binary forms are permitted 734920Sbostic * provided that the above copyright notice and this paragraph are 834920Sbostic * duplicated in all such forms and that any documentation, 934920Sbostic * advertising materials, and other materials related to such 1034920Sbostic * distribution and use acknowledge that the software was developed 1134920Sbostic * by the University of California, Berkeley. The name of the 1234920Sbostic * University may not be used to endorse or promote products derived 1334920Sbostic * from this software without specific prior written permission. 1434920Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1534920Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1634920Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1733728Sbostic */ 1822697Sdist 1922697Sdist #ifndef lint 20*36233Skarels static char sccsid[] = "@(#)collect.c 5.6 (Berkeley) 11/17/88"; 2133728Sbostic #endif /* not lint */ 2222697Sdist 231439Seric # include <errno.h> 243309Seric # include "sendmail.h" 251392Seric 261392Seric /* 272969Seric ** COLLECT -- read & parse message header & make temp file. 281392Seric ** 291392Seric ** Creates a temporary file name and copies the standard 309371Seric ** input to that file. Leading UNIX-style "From" lines are 319371Seric ** stripped off (after important information is extracted). 321392Seric ** 331392Seric ** Parameters: 344710Seric ** sayok -- if set, give an ARPANET style message 354710Seric ** to say we are ready to collect input. 361392Seric ** 371392Seric ** Returns: 384162Seric ** none. 391392Seric ** 401392Seric ** Side Effects: 411392Seric ** Temp file is created and filled. 424162Seric ** The from person may be set. 431392Seric */ 441392Seric 454710Seric collect(sayok) 464710Seric bool sayok; 471392Seric { 481392Seric register FILE *tf; 497852Seric char buf[MAXFIELD+2]; 501392Seric register char *p; 512900Seric extern char *hvalue(); 521392Seric 531392Seric /* 541392Seric ** Create the temp file name and create the file. 551392Seric */ 561392Seric 577809Seric CurEnv->e_df = newstr(queuename(CurEnv, 'd')); 587809Seric if ((tf = dfopen(CurEnv->e_df, "w")) == NULL) 591392Seric { 607809Seric syserr("Cannot create %s", CurEnv->e_df); 615366Seric NoReturn = TRUE; 625366Seric finis(); 631392Seric } 649047Seric (void) chmod(CurEnv->e_df, FileMode); 651392Seric 664316Seric /* 674322Seric ** Tell ARPANET to go ahead. 684322Seric */ 694322Seric 704710Seric if (sayok) 714710Seric message("354", "Enter mail, end with \".\" on a line by itself"); 724322Seric 734322Seric /* 744316Seric ** Try to read a UNIX-style From line 754316Seric */ 764316Seric 7715532Seric (void) sfgets(buf, sizeof buf, InChannel); 784557Seric fixcrlf(buf, FALSE); 794321Seric # ifndef NOTUNIX 804322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 812900Seric { 822900Seric eatfrom(buf); 8313932Seric (void) sfgets(buf, sizeof buf, InChannel); 844557Seric fixcrlf(buf, FALSE); 852900Seric } 864321Seric # 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 9515532Seric do 961392Seric { 9715532Seric int c; 984316Seric extern bool isheader(); 994316Seric 10019036Seric /* drop out on error */ 10119036Seric if (ferror(InChannel)) 10219036Seric break; 10319036Seric 1047681Seric /* if the line is too long, throw the rest away */ 1057681Seric if (index(buf, '\n') == NULL) 1067681Seric { 10715532Seric while ((c = getc(InChannel)) != '\n' && c != EOF) 1087681Seric continue; 1097681Seric /* give an error? */ 1107681Seric } 1117681Seric 1127852Seric fixcrlf(buf, TRUE); 1134557Seric 1142900Seric /* see if the header is over */ 1152900Seric if (!isheader(buf)) 1162900Seric break; 1172900Seric 1182900Seric /* get the rest of this field */ 1195975Seric while ((c = getc(InChannel)) == ' ' || c == '\t') 1201392Seric { 1212900Seric p = &buf[strlen(buf)]; 1227852Seric *p++ = '\n'; 1232900Seric *p++ = c; 12413932Seric if (sfgets(p, MAXFIELD - (p - buf), InChannel) == NULL) 1252900Seric break; 1267852Seric fixcrlf(p, TRUE); 1271392Seric } 12819036Seric if (!feof(InChannel) && !ferror(InChannel)) 1295975Seric (void) ungetc(c, InChannel); 1301392Seric 1316901Seric CurEnv->e_msgsize += strlen(buf); 1321392Seric 1332900Seric /* 1342900Seric ** Snarf header away. 1352900Seric */ 1362900Seric 1373391Seric if (bitset(H_EOH, chompheader(buf, FALSE))) 1383058Seric break; 13915532Seric } while (sfgets(buf, MAXFIELD, InChannel) != NULL); 1401392Seric 1412900Seric # ifdef DEBUG 1427673Seric if (tTd(30, 1)) 1432900Seric printf("EOH\n"); 1442900Seric # endif DEBUG 1452900Seric 1462900Seric /* throw away a blank line */ 1477852Seric if (buf[0] == '\0') 14813932Seric (void) sfgets(buf, MAXFIELD, InChannel); 1492900Seric 1502900Seric /* 1512900Seric ** Collect the body of the message. 1522900Seric */ 1532900Seric 15415532Seric do 1552900Seric { 1564551Seric register char *bp = buf; 1574156Seric 1587852Seric fixcrlf(buf, TRUE); 1594557Seric 1602900Seric /* check for end-of-message */ 1612900Seric if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 1622900Seric break; 1632900Seric 1644551Seric /* check for transparent dot */ 1659371Seric if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.') 1664551Seric bp++; 1674551Seric 1684156Seric /* 1694156Seric ** Figure message length, output the line to the temp 1704156Seric ** file, and insert a newline if missing. 1714156Seric */ 1724156Seric 1739371Seric CurEnv->e_msgsize += strlen(bp) + 1; 1744551Seric fputs(bp, tf); 1757852Seric fputs("\n", tf); 1761392Seric if (ferror(tf)) 17711544Seric tferror(tf); 17815532Seric } while (sfgets(buf, MAXFIELD, InChannel) != NULL); 17911544Seric if (fflush(tf) != 0) 18011544Seric tferror(tf); 1814083Seric (void) fclose(tf); 1822900Seric 18311145Seric /* An EOF when running SMTP is an error */ 18419036Seric if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP) 18516136Seric { 186*36233Skarels # ifdef LOG 187*36233Skarels if (RealHostName != NULL && LogLevel > 0) 18836230Skarels syslog(LOG_NOTICE, 18936230Skarels "collect: unexpected close on connection from %s: %m\n", 19036230Skarels CurEnv->e_from.q_paddr, RealHostName); 191*36233Skarels # endif 19236230Skarels usrerr("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr); 19311145Seric 19416136Seric /* don't return an error indication */ 19516136Seric CurEnv->e_to = NULL; 19616136Seric CurEnv->e_flags &= ~EF_FATALERRS; 19716136Seric 19816136Seric /* and don't try to deliver the partial message either */ 19916136Seric finis(); 20016136Seric } 20116136Seric 2022900Seric /* 2032900Seric ** Find out some information from the headers. 2043386Seric ** Examples are who is the from person & the date. 2052900Seric */ 2062900Seric 2079371Seric eatheader(CurEnv); 2087673Seric 2097782Seric /* 2107782Seric ** Add an Apparently-To: line if we have no recipient lines. 2117782Seric */ 2124622Seric 2137367Seric if (hvalue("to") == NULL && hvalue("cc") == NULL && 2147367Seric hvalue("bcc") == NULL && hvalue("apparently-to") == NULL) 2157367Seric { 2167367Seric register ADDRESS *q; 2177367Seric 2187367Seric /* create an Apparently-To: field */ 2197367Seric /* that or reject the message.... */ 2207367Seric for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 2217367Seric { 2227389Seric if (q->q_alias != NULL) 2237389Seric continue; 2247367Seric # ifdef DEBUG 2257673Seric if (tTd(30, 3)) 2267367Seric printf("Adding Apparently-To: %s\n", q->q_paddr); 2277367Seric # endif DEBUG 2287367Seric addheader("apparently-to", q->q_paddr, CurEnv); 2297367Seric } 2307367Seric } 2317367Seric 2329539Seric if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL) 2336986Seric syserr("Cannot reopen %s", CurEnv->e_df); 2341392Seric } 2351392Seric /* 23611544Seric ** TFERROR -- signal error on writing the temporary file. 23711544Seric ** 23811544Seric ** Parameters: 23911544Seric ** tf -- the file pointer for the temporary file. 24011544Seric ** 24111544Seric ** Returns: 24211544Seric ** none. 24311544Seric ** 24411544Seric ** Side Effects: 24511544Seric ** Gives an error message. 24611544Seric ** Arranges for following output to go elsewhere. 24711544Seric */ 24811544Seric 24911544Seric tferror(tf) 25011544Seric FILE *tf; 25111544Seric { 25211544Seric if (errno == ENOSPC) 25311544Seric { 25411544Seric (void) freopen(CurEnv->e_df, "w", tf); 25511544Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 25611544Seric usrerr("452 Out of disk space for temp file"); 25711544Seric } 25811544Seric else 25911544Seric syserr("collect: Cannot write %s", CurEnv->e_df); 26011544Seric (void) freopen("/dev/null", "w", tf); 26111544Seric } 26211544Seric /* 2632900Seric ** EATFROM -- chew up a UNIX style from line and process 2642900Seric ** 2652900Seric ** This does indeed make some assumptions about the format 2662900Seric ** of UNIX messages. 2672900Seric ** 2682900Seric ** Parameters: 2692900Seric ** fm -- the from line. 2702900Seric ** 2712900Seric ** Returns: 2722900Seric ** none. 2732900Seric ** 2742900Seric ** Side Effects: 2752900Seric ** extracts what information it can from the header, 2763386Seric ** such as the date. 2772900Seric */ 2782900Seric 2794321Seric # ifndef NOTUNIX 2804321Seric 2814203Seric char *DowList[] = 2824203Seric { 2834203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 2844203Seric }; 2854203Seric 2862900Seric char *MonthList[] = 2872900Seric { 2882900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 2892900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 2902900Seric NULL 2912900Seric }; 2922900Seric 2932900Seric eatfrom(fm) 2942900Seric char *fm; 2952900Seric { 2962900Seric register char *p; 2972900Seric register char **dt; 2982900Seric 2994203Seric # ifdef DEBUG 3007673Seric if (tTd(30, 2)) 3014203Seric printf("eatfrom(%s)\n", fm); 3024203Seric # endif DEBUG 3034203Seric 3042900Seric /* find the date part */ 3052900Seric p = fm; 3062900Seric while (*p != '\0') 3072900Seric { 3082900Seric /* skip a word */ 3092900Seric while (*p != '\0' && *p != ' ') 31016896Seric p++; 3112900Seric while (*p == ' ') 31216896Seric p++; 3132900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 3142900Seric continue; 3152900Seric 3162900Seric /* we have a possible date */ 3174203Seric for (dt = DowList; *dt != NULL; dt++) 3182900Seric if (strncmp(*dt, p, 3) == 0) 3192900Seric break; 3204203Seric if (*dt == NULL) 3214203Seric continue; 3222900Seric 3234203Seric for (dt = MonthList; *dt != NULL; dt++) 3244203Seric if (strncmp(*dt, &p[4], 3) == 0) 3254203Seric break; 3262900Seric if (*dt != NULL) 3272900Seric break; 3282900Seric } 3292900Seric 3302900Seric if (*p != NULL) 3312900Seric { 3323386Seric char *q; 3335366Seric extern char *arpadate(); 3343386Seric 3352900Seric /* we have found a date */ 3363386Seric q = xalloc(25); 33723103Seric (void) strncpy(q, p, 25); 3383386Seric q[24] = '\0'; 3399371Seric define('d', q, CurEnv); 3405366Seric q = arpadate(q); 3419371Seric define('a', newstr(q), CurEnv); 3422900Seric } 3432900Seric } 3444321Seric 3454321Seric # endif NOTUNIX 346