11439Seric # include <errno.h> 23309Seric # include "sendmail.h" 31392Seric 4*7782Seric SCCSID(@(#)collect.c 3.47 08/17/82); 51392Seric 61392Seric /* 72969Seric ** COLLECT -- read & parse message header & make temp file. 81392Seric ** 91392Seric ** Creates a temporary file name and copies the standard 101392Seric ** input to that file. While it is doing it, it looks for 111392Seric ** "From:" and "Sender:" fields to use as the from-person 121392Seric ** (but only if the -a flag is specified). It prefers to 131392Seric ** to use the "Sender:" field. 141392Seric ** 151392Seric ** MIT seems to like to produce "Sent-By:" fields instead 161392Seric ** of "Sender:" fields. We used to catch this, but it turns 171392Seric ** out that the "Sent-By:" field doesn't always correspond 181392Seric ** to someone real ("___057", for instance), as required by 191392Seric ** the protocol. So we limp by..... 201392Seric ** 211392Seric ** Parameters: 224710Seric ** sayok -- if set, give an ARPANET style message 234710Seric ** to say we are ready to collect input. 241392Seric ** 251392Seric ** Returns: 264162Seric ** none. 271392Seric ** 281392Seric ** Side Effects: 291392Seric ** Temp file is created and filled. 304162Seric ** The from person may be set. 311392Seric */ 321392Seric 334710Seric collect(sayok) 344710Seric bool sayok; 351392Seric { 361392Seric register FILE *tf; 371392Seric char buf[MAXFIELD+1]; 381392Seric register char *p; 392900Seric extern char *hvalue(); 404083Seric extern char *mktemp(); 41*7782Seric static char tempfname[60]; 425192Seric extern char *macvalue(); 431392Seric 441392Seric /* 451392Seric ** Create the temp file name and create the file. 461392Seric */ 471392Seric 487004Seric (void) strcpy(tempfname, QueueDir); 497004Seric (void) strcat(tempfname, "/dfXXXXXX"); 504622Seric (void) mktemp(tempfname); 516888Seric if ((tf = dfopen(tempfname, "w")) == NULL) 521392Seric { 534622Seric syserr("Cannot create %s", tempfname); 545366Seric NoReturn = TRUE; 555366Seric finis(); 561392Seric } 577004Seric (void) chmod(tempfname, 0600); 586986Seric CurEnv->e_df = tempfname; 591392Seric 604316Seric /* 615185Seric ** Create the Mail-From line if we want to. 625185Seric */ 635185Seric 645366Seric if (Smtp && macvalue('s') != NULL) 655185Seric { 665185Seric char xbuf[50]; 675185Seric 685192Seric (void) sprintf(xbuf, "Mail-From: %s$s received by $i at $b", 695185Seric macvalue('r') == NULL ? "" : "$r host "); 706975Seric expand(xbuf, buf, &buf[sizeof buf - 1], CurEnv); 715192Seric (void) chompheader(buf, FALSE); 725185Seric } 735185Seric 745185Seric /* 754322Seric ** Tell ARPANET to go ahead. 764322Seric */ 774322Seric 784710Seric if (sayok) 794710Seric message("354", "Enter mail, end with \".\" on a line by itself"); 804322Seric 814322Seric /* 824316Seric ** Try to read a UNIX-style From line 834316Seric */ 844316Seric 855975Seric if (fgets(buf, sizeof buf, InChannel) == NULL) 864162Seric return; 874557Seric fixcrlf(buf, FALSE); 884321Seric # ifndef NOTUNIX 894322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 902900Seric { 912900Seric eatfrom(buf); 925975Seric (void) fgets(buf, sizeof buf, InChannel); 934557Seric fixcrlf(buf, FALSE); 942900Seric } 954321Seric # endif NOTUNIX 962900Seric 971392Seric /* 985975Seric ** Copy InChannel to temp file & do message editing. 991392Seric ** To keep certain mailers from getting confused, 1001392Seric ** and to keep the output clean, lines that look 1011392Seric ** like UNIX "From" lines are deleted in the header, 1021392Seric ** and prepended with ">" in the body. 1031392Seric */ 1041392Seric 1055975Seric for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL) 1061392Seric { 1074316Seric register char c; 1084316Seric extern bool isheader(); 1094316Seric 1107681Seric /* if the line is too long, throw the rest away */ 1117681Seric if (index(buf, '\n') == NULL) 1127681Seric { 1137681Seric while ((c = getc(InChannel)) != '\n') 1147681Seric continue; 1157681Seric /* give an error? */ 1167681Seric } 1177681Seric 1184557Seric fixcrlf(buf, FALSE); 1194557Seric 1202900Seric /* see if the header is over */ 1212900Seric if (!isheader(buf)) 1222900Seric break; 1232900Seric 1242900Seric /* get the rest of this field */ 1255975Seric while ((c = getc(InChannel)) == ' ' || c == '\t') 1261392Seric { 1272900Seric p = &buf[strlen(buf)]; 1282900Seric *p++ = c; 1295975Seric if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL) 1302900Seric break; 1314557Seric fixcrlf(p, FALSE); 1321392Seric } 1335975Seric if (!feof(InChannel)) 1345975Seric (void) ungetc(c, InChannel); 1351392Seric 1366901Seric CurEnv->e_msgsize += strlen(buf); 1371392Seric 1382900Seric /* 1392900Seric ** Snarf header away. 1402900Seric */ 1412900Seric 1423391Seric if (bitset(H_EOH, chompheader(buf, FALSE))) 1433058Seric break; 1442900Seric } 1451392Seric 1462900Seric # ifdef DEBUG 1477673Seric if (tTd(30, 1)) 1482900Seric printf("EOH\n"); 1492900Seric # endif DEBUG 1502900Seric 1512900Seric /* throw away a blank line */ 1522900Seric if (buf[0] == '\n') 1534557Seric { 1545975Seric (void) fgets(buf, sizeof buf, InChannel); 1554557Seric fixcrlf(buf, FALSE); 1564557Seric } 1572900Seric 1582900Seric /* 1592900Seric ** Collect the body of the message. 1602900Seric */ 1612900Seric 1625975Seric for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL) 1632900Seric { 1644156Seric register int i; 1654551Seric register char *bp = buf; 1664156Seric 1674557Seric fixcrlf(buf, FALSE); 1684557Seric 1692900Seric /* check for end-of-message */ 1702900Seric if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 1712900Seric break; 1722900Seric 1734551Seric /* check for transparent dot */ 1744551Seric if (Smtp && *bp == '.') 1754551Seric bp++; 1764551Seric 1774321Seric # ifndef NOTUNIX 1782900Seric /* Hide UNIX-like From lines */ 1794551Seric if (strncmp(bp, "From ", 5) == 0) 1801392Seric { 1812900Seric fputs(">", tf); 1826901Seric CurEnv->e_msgsize++; 1831392Seric } 1844321Seric # endif NOTUNIX 1854156Seric 1864156Seric /* 1874156Seric ** Figure message length, output the line to the temp 1884156Seric ** file, and insert a newline if missing. 1894156Seric */ 1904156Seric 1914551Seric i = strlen(bp); 1926901Seric CurEnv->e_msgsize += i; 1934551Seric fputs(bp, tf); 1944551Seric if (bp[i - 1] != '\n') 1954156Seric fputs("\n", tf); 1961392Seric if (ferror(tf)) 1971392Seric { 1981439Seric if (errno == ENOSPC) 1991439Seric { 2006986Seric (void) freopen(CurEnv->e_df, "w", tf); 2011439Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 2024557Seric usrerr("452 Out of disk space for temp file"); 2031439Seric } 2041439Seric else 2056986Seric syserr("collect: Cannot write %s", CurEnv->e_df); 2064083Seric (void) freopen("/dev/null", "w", tf); 2071392Seric } 2081392Seric } 2094083Seric (void) fclose(tf); 2102900Seric 2112900Seric /* 2122900Seric ** Find out some information from the headers. 2133386Seric ** Examples are who is the from person & the date. 2142900Seric */ 2152900Seric 216*7782Seric eatheader(); 2177673Seric 218*7782Seric /* 219*7782Seric ** Add an Apparently-To: line if we have no recipient lines. 220*7782Seric */ 2214622Seric 2227367Seric if (hvalue("to") == NULL && hvalue("cc") == NULL && 2237367Seric hvalue("bcc") == NULL && hvalue("apparently-to") == NULL) 2247367Seric { 2257367Seric register ADDRESS *q; 2267367Seric 2277367Seric /* create an Apparently-To: field */ 2287367Seric /* that or reject the message.... */ 2297367Seric for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 2307367Seric { 2317389Seric if (q->q_alias != NULL) 2327389Seric continue; 2337367Seric # ifdef DEBUG 2347673Seric if (tTd(30, 3)) 2357367Seric printf("Adding Apparently-To: %s\n", q->q_paddr); 2367367Seric # endif DEBUG 2377367Seric addheader("apparently-to", q->q_paddr, CurEnv); 2387367Seric } 2397367Seric } 2407367Seric 2417364Seric /* check for hop count overflow */ 2427364Seric if (HopCount > MAXHOP) 2437364Seric syserr("Too many hops (%d max); probably forwarding loop", MAXHOP); 2447364Seric 2456986Seric if ((TempFile = fopen(CurEnv->e_df, "r")) == NULL) 2466986Seric syserr("Cannot reopen %s", CurEnv->e_df); 2472900Seric 2487673Seric /* 2497673Seric ** Log collection information. 2507673Seric */ 2517673Seric 2527673Seric # ifdef LOG 2537673Seric if (LogLevel > 1) 2547673Seric syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n", MsgId, 2557673Seric CurEnv->e_from.q_paddr, CurEnv->e_msgsize, 2567758Seric CurEnv->e_class); 2577673Seric # endif LOG 2584162Seric return; 2591392Seric } 2601392Seric /* 2612900Seric ** EATFROM -- chew up a UNIX style from line and process 2622900Seric ** 2632900Seric ** This does indeed make some assumptions about the format 2642900Seric ** of UNIX messages. 2652900Seric ** 2662900Seric ** Parameters: 2672900Seric ** fm -- the from line. 2682900Seric ** 2692900Seric ** Returns: 2702900Seric ** none. 2712900Seric ** 2722900Seric ** Side Effects: 2732900Seric ** extracts what information it can from the header, 2743386Seric ** such as the date. 2752900Seric */ 2762900Seric 2774321Seric # ifndef NOTUNIX 2784321Seric 2794203Seric char *DowList[] = 2804203Seric { 2814203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 2824203Seric }; 2834203Seric 2842900Seric char *MonthList[] = 2852900Seric { 2862900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 2872900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 2882900Seric NULL 2892900Seric }; 2902900Seric 2912900Seric eatfrom(fm) 2922900Seric char *fm; 2932900Seric { 2942900Seric register char *p; 2952900Seric register char **dt; 2962900Seric 2974203Seric # ifdef DEBUG 2987673Seric if (tTd(30, 2)) 2994203Seric printf("eatfrom(%s)\n", fm); 3004203Seric # endif DEBUG 3014203Seric 3022900Seric /* find the date part */ 3032900Seric p = fm; 3042900Seric while (*p != '\0') 3052900Seric { 3062900Seric /* skip a word */ 3072900Seric while (*p != '\0' && *p != ' ') 3082900Seric *p++; 3092900Seric while (*p == ' ') 3102900Seric *p++; 3112900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 3122900Seric continue; 3132900Seric 3142900Seric /* we have a possible date */ 3154203Seric for (dt = DowList; *dt != NULL; dt++) 3162900Seric if (strncmp(*dt, p, 3) == 0) 3172900Seric break; 3184203Seric if (*dt == NULL) 3194203Seric continue; 3202900Seric 3214203Seric for (dt = MonthList; *dt != NULL; dt++) 3224203Seric if (strncmp(*dt, &p[4], 3) == 0) 3234203Seric break; 3242900Seric if (*dt != NULL) 3252900Seric break; 3262900Seric } 3272900Seric 3282900Seric if (*p != NULL) 3292900Seric { 3303386Seric char *q; 3315366Seric extern char *arpadate(); 3323386Seric 3332900Seric /* we have found a date */ 3343386Seric q = xalloc(25); 3353386Seric strncpy(q, p, 25); 3363386Seric q[24] = '\0'; 3373386Seric define('d', q); 3385366Seric q = arpadate(q); 3395366Seric define('a', newstr(q)); 3402900Seric } 3412900Seric } 3424321Seric 3434321Seric # endif NOTUNIX 344