11439Seric # include <errno.h> 23309Seric # include "sendmail.h" 31392Seric 4*9047Seric SCCSID(@(#)collect.c 3.53 11/03/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; 377852Seric char buf[MAXFIELD+2]; 381392Seric register char *p; 392900Seric extern char *hvalue(); 401392Seric 411392Seric /* 421392Seric ** Create the temp file name and create the file. 431392Seric */ 441392Seric 457809Seric CurEnv->e_df = newstr(queuename(CurEnv, 'd')); 467809Seric if ((tf = dfopen(CurEnv->e_df, "w")) == NULL) 471392Seric { 487809Seric syserr("Cannot create %s", CurEnv->e_df); 495366Seric NoReturn = TRUE; 505366Seric finis(); 511392Seric } 52*9047Seric (void) chmod(CurEnv->e_df, FileMode); 531392Seric 544316Seric /* 554322Seric ** Tell ARPANET to go ahead. 564322Seric */ 574322Seric 584710Seric if (sayok) 594710Seric message("354", "Enter mail, end with \".\" on a line by itself"); 604322Seric 614322Seric /* 624316Seric ** Try to read a UNIX-style From line 634316Seric */ 644316Seric 655975Seric if (fgets(buf, sizeof buf, InChannel) == NULL) 664162Seric return; 674557Seric fixcrlf(buf, FALSE); 684321Seric # ifndef NOTUNIX 694322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 702900Seric { 712900Seric eatfrom(buf); 725975Seric (void) fgets(buf, sizeof buf, InChannel); 734557Seric fixcrlf(buf, FALSE); 742900Seric } 754321Seric # endif NOTUNIX 762900Seric 771392Seric /* 785975Seric ** Copy InChannel to temp file & do message editing. 791392Seric ** To keep certain mailers from getting confused, 801392Seric ** and to keep the output clean, lines that look 811392Seric ** like UNIX "From" lines are deleted in the header, 821392Seric ** and prepended with ">" in the body. 831392Seric */ 841392Seric 857852Seric for (; !feof(InChannel); !feof(InChannel) && fgets(buf, MAXFIELD, InChannel) != NULL) 861392Seric { 874316Seric register char c; 884316Seric extern bool isheader(); 894316Seric 907681Seric /* if the line is too long, throw the rest away */ 917681Seric if (index(buf, '\n') == NULL) 927681Seric { 937681Seric while ((c = getc(InChannel)) != '\n') 947681Seric continue; 957681Seric /* give an error? */ 967681Seric } 977681Seric 987852Seric fixcrlf(buf, TRUE); 994557Seric 1002900Seric /* see if the header is over */ 1012900Seric if (!isheader(buf)) 1022900Seric break; 1032900Seric 1042900Seric /* get the rest of this field */ 1055975Seric while ((c = getc(InChannel)) == ' ' || c == '\t') 1061392Seric { 1072900Seric p = &buf[strlen(buf)]; 1087852Seric *p++ = '\n'; 1092900Seric *p++ = c; 1107852Seric if (fgets(p, MAXFIELD - (p - buf), InChannel) == NULL) 1112900Seric break; 1127852Seric fixcrlf(p, TRUE); 1131392Seric } 1145975Seric if (!feof(InChannel)) 1155975Seric (void) ungetc(c, InChannel); 1161392Seric 1176901Seric CurEnv->e_msgsize += strlen(buf); 1181392Seric 1192900Seric /* 1202900Seric ** Snarf header away. 1212900Seric */ 1222900Seric 1233391Seric if (bitset(H_EOH, chompheader(buf, FALSE))) 1243058Seric break; 1252900Seric } 1261392Seric 1272900Seric # ifdef DEBUG 1287673Seric if (tTd(30, 1)) 1292900Seric printf("EOH\n"); 1302900Seric # endif DEBUG 1312900Seric 1322900Seric /* throw away a blank line */ 1337852Seric if (buf[0] == '\0') 1344557Seric { 1357852Seric (void) fgets(buf, MAXFIELD, InChannel); 1367852Seric fixcrlf(buf, TRUE); 1374557Seric } 1382900Seric 1392900Seric /* 1402900Seric ** Collect the body of the message. 1412900Seric */ 1422900Seric 1435975Seric for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL) 1442900Seric { 1454156Seric register int i; 1464551Seric register char *bp = buf; 1474156Seric 1487852Seric fixcrlf(buf, TRUE); 1494557Seric 1502900Seric /* check for end-of-message */ 1512900Seric if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 1522900Seric break; 1532900Seric 1544551Seric /* check for transparent dot */ 1554551Seric if (Smtp && *bp == '.') 1564551Seric bp++; 1574551Seric 1584321Seric # ifndef NOTUNIX 1592900Seric /* Hide UNIX-like From lines */ 1604551Seric if (strncmp(bp, "From ", 5) == 0) 1611392Seric { 1622900Seric fputs(">", tf); 1636901Seric CurEnv->e_msgsize++; 1641392Seric } 1654321Seric # endif NOTUNIX 1664156Seric 1674156Seric /* 1684156Seric ** Figure message length, output the line to the temp 1694156Seric ** file, and insert a newline if missing. 1704156Seric */ 1714156Seric 1724551Seric i = strlen(bp); 1737852Seric CurEnv->e_msgsize += i + 1; 1744551Seric fputs(bp, tf); 1757852Seric fputs("\n", tf); 1761392Seric if (ferror(tf)) 1771392Seric { 1781439Seric if (errno == ENOSPC) 1791439Seric { 1806986Seric (void) freopen(CurEnv->e_df, "w", tf); 1811439Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 1824557Seric usrerr("452 Out of disk space for temp file"); 1831439Seric } 1841439Seric else 1856986Seric syserr("collect: Cannot write %s", CurEnv->e_df); 1864083Seric (void) freopen("/dev/null", "w", tf); 1871392Seric } 1881392Seric } 1894083Seric (void) fclose(tf); 1902900Seric 1912900Seric /* 1922900Seric ** Find out some information from the headers. 1933386Seric ** Examples are who is the from person & the date. 1942900Seric */ 1952900Seric 1967782Seric eatheader(); 1977673Seric 1987782Seric /* 1997782Seric ** Add an Apparently-To: line if we have no recipient lines. 2007782Seric */ 2014622Seric 2027367Seric if (hvalue("to") == NULL && hvalue("cc") == NULL && 2037367Seric hvalue("bcc") == NULL && hvalue("apparently-to") == NULL) 2047367Seric { 2057367Seric register ADDRESS *q; 2067367Seric 2077367Seric /* create an Apparently-To: field */ 2087367Seric /* that or reject the message.... */ 2097367Seric for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 2107367Seric { 2117389Seric if (q->q_alias != NULL) 2127389Seric continue; 2137367Seric # ifdef DEBUG 2147673Seric if (tTd(30, 3)) 2157367Seric printf("Adding Apparently-To: %s\n", q->q_paddr); 2167367Seric # endif DEBUG 2177367Seric addheader("apparently-to", q->q_paddr, CurEnv); 2187367Seric } 2197367Seric } 2207367Seric 2217364Seric /* check for hop count overflow */ 2227364Seric if (HopCount > MAXHOP) 2237364Seric syserr("Too many hops (%d max); probably forwarding loop", MAXHOP); 2247364Seric 2256986Seric if ((TempFile = fopen(CurEnv->e_df, "r")) == NULL) 2266986Seric syserr("Cannot reopen %s", CurEnv->e_df); 2272900Seric 2287673Seric /* 2297673Seric ** Log collection information. 2307673Seric */ 2317673Seric 2327673Seric # ifdef LOG 2337673Seric if (LogLevel > 1) 2347809Seric syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n", 2357809Seric CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize, 2367758Seric CurEnv->e_class); 2377673Seric # endif LOG 2384162Seric return; 2391392Seric } 2401392Seric /* 2412900Seric ** EATFROM -- chew up a UNIX style from line and process 2422900Seric ** 2432900Seric ** This does indeed make some assumptions about the format 2442900Seric ** of UNIX messages. 2452900Seric ** 2462900Seric ** Parameters: 2472900Seric ** fm -- the from line. 2482900Seric ** 2492900Seric ** Returns: 2502900Seric ** none. 2512900Seric ** 2522900Seric ** Side Effects: 2532900Seric ** extracts what information it can from the header, 2543386Seric ** such as the date. 2552900Seric */ 2562900Seric 2574321Seric # ifndef NOTUNIX 2584321Seric 2594203Seric char *DowList[] = 2604203Seric { 2614203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 2624203Seric }; 2634203Seric 2642900Seric char *MonthList[] = 2652900Seric { 2662900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 2672900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 2682900Seric NULL 2692900Seric }; 2702900Seric 2712900Seric eatfrom(fm) 2722900Seric char *fm; 2732900Seric { 2742900Seric register char *p; 2752900Seric register char **dt; 2762900Seric 2774203Seric # ifdef DEBUG 2787673Seric if (tTd(30, 2)) 2794203Seric printf("eatfrom(%s)\n", fm); 2804203Seric # endif DEBUG 2814203Seric 2822900Seric /* find the date part */ 2832900Seric p = fm; 2842900Seric while (*p != '\0') 2852900Seric { 2862900Seric /* skip a word */ 2872900Seric while (*p != '\0' && *p != ' ') 2882900Seric *p++; 2892900Seric while (*p == ' ') 2902900Seric *p++; 2912900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 2922900Seric continue; 2932900Seric 2942900Seric /* we have a possible date */ 2954203Seric for (dt = DowList; *dt != NULL; dt++) 2962900Seric if (strncmp(*dt, p, 3) == 0) 2972900Seric break; 2984203Seric if (*dt == NULL) 2994203Seric continue; 3002900Seric 3014203Seric for (dt = MonthList; *dt != NULL; dt++) 3024203Seric if (strncmp(*dt, &p[4], 3) == 0) 3034203Seric break; 3042900Seric if (*dt != NULL) 3052900Seric break; 3062900Seric } 3072900Seric 3082900Seric if (*p != NULL) 3092900Seric { 3103386Seric char *q; 3115366Seric extern char *arpadate(); 3123386Seric 3132900Seric /* we have found a date */ 3143386Seric q = xalloc(25); 3153386Seric strncpy(q, p, 25); 3163386Seric q[24] = '\0'; 3173386Seric define('d', q); 3185366Seric q = arpadate(q); 3195366Seric define('a', newstr(q)); 3202900Seric } 3212900Seric } 3224321Seric 3234321Seric # endif NOTUNIX 324