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*40965Sbostic static char sccsid[] = "@(#)collect.c 5.8 (Berkeley) 04/18/90"; 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; 49*40965Sbostic char buf[MAXFIELD], buf2[MAXFIELD]; 50*40965Sbostic register char *workbuf, *freebuf; 51*40965Sbostic register int workbuflen; 522900Seric extern char *hvalue(); 53*40965Sbostic extern bool isheader(), flusheol(); 541392Seric 551392Seric /* 561392Seric ** Create the temp file name and create the file. 571392Seric */ 581392Seric 597809Seric CurEnv->e_df = newstr(queuename(CurEnv, 'd')); 607809Seric if ((tf = dfopen(CurEnv->e_df, "w")) == NULL) 611392Seric { 627809Seric syserr("Cannot create %s", CurEnv->e_df); 635366Seric NoReturn = TRUE; 645366Seric finis(); 651392Seric } 669047Seric (void) chmod(CurEnv->e_df, FileMode); 671392Seric 684316Seric /* 694322Seric ** Tell ARPANET to go ahead. 704322Seric */ 714322Seric 724710Seric if (sayok) 734710Seric message("354", "Enter mail, end with \".\" on a line by itself"); 744322Seric 754322Seric /* 764316Seric ** Try to read a UNIX-style From line 774316Seric */ 784316Seric 79*40965Sbostic if (sfgets(buf, MAXFIELD, InChannel) == NULL) 80*40965Sbostic goto readerr; 814557Seric fixcrlf(buf, FALSE); 824321Seric # ifndef NOTUNIX 834322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 842900Seric { 85*40965Sbostic if (!flusheol(buf, InChannel)) 86*40965Sbostic goto readerr; 872900Seric eatfrom(buf); 88*40965Sbostic if (sfgets(buf, MAXFIELD, InChannel) == NULL) 89*40965Sbostic goto readerr; 904557Seric fixcrlf(buf, FALSE); 912900Seric } 924321Seric # 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 101*40965Sbostic workbuf = buf; /* `workbuf' contains a header field */ 102*40965Sbostic freebuf = buf2; /* `freebuf' can be used for read-ahead */ 103*40965Sbostic for (;;) 1041392Seric { 105*40965Sbostic /* first, see if the header is over */ 106*40965Sbostic if (!isheader(workbuf)) 107*40965Sbostic { 108*40965Sbostic fixcrlf(workbuf, TRUE); 10919036Seric break; 110*40965Sbostic } 11119036Seric 1127681Seric /* if the line is too long, throw the rest away */ 113*40965Sbostic if (!flusheol(workbuf, InChannel)) 114*40965Sbostic goto readerr; 1157681Seric 116*40965Sbostic /* it's okay to toss '\n' now (flusheol() needed it) */ 117*40965Sbostic fixcrlf(workbuf, TRUE); 1184557Seric 119*40965Sbostic workbuflen = strlen(workbuf); 1202900Seric 1212900Seric /* get the rest of this field */ 122*40965Sbostic for (;;) 1231392Seric { 124*40965Sbostic if (sfgets(freebuf, MAXFIELD, InChannel) == NULL) 125*40965Sbostic goto readerr; 126*40965Sbostic 127*40965Sbostic /* is this a continuation line? */ 128*40965Sbostic if (*freebuf != ' ' && *freebuf != '\t') 1292900Seric break; 130*40965Sbostic 131*40965Sbostic if (!flusheol(freebuf, InChannel)) 132*40965Sbostic goto readerr; 133*40965Sbostic 134*40965Sbostic /* yes; append line to `workbuf' if there's room */ 135*40965Sbostic if (workbuflen < MAXFIELD-3) 136*40965Sbostic { 137*40965Sbostic register char *p = workbuf + workbuflen; 138*40965Sbostic register char *q = freebuf; 139*40965Sbostic 140*40965Sbostic /* we have room for more of this field */ 141*40965Sbostic fixcrlf(freebuf, TRUE); 142*40965Sbostic *p++ = '\n'; workbuflen++; 143*40965Sbostic while(*q != '\0' && workbuflen < MAXFIELD-1) 144*40965Sbostic { 145*40965Sbostic *p++ = *q++; 146*40965Sbostic workbuflen++; 147*40965Sbostic } 148*40965Sbostic *p = '\0'; 149*40965Sbostic } 1501392Seric } 1511392Seric 152*40965Sbostic CurEnv->e_msgsize += workbuflen; 1531392Seric 1542900Seric /* 155*40965Sbostic ** The working buffer now becomes the free buffer, since 156*40965Sbostic ** the free buffer contains a new header field. 157*40965Sbostic ** 158*40965Sbostic ** This is premature, since we still havent called 159*40965Sbostic ** chompheader() to process the field we just created 160*40965Sbostic ** (so the call to chompheader() will use `freebuf'). 161*40965Sbostic ** This convolution is necessary so that if we break out 162*40965Sbostic ** of the loop due to H_EOH, `workbuf' will always be 163*40965Sbostic ** the next unprocessed buffer. 164*40965Sbostic */ 165*40965Sbostic 166*40965Sbostic { 167*40965Sbostic register char *tmp = workbuf; 168*40965Sbostic workbuf = freebuf; 169*40965Sbostic freebuf = tmp; 170*40965Sbostic } 171*40965Sbostic 172*40965Sbostic /* 1732900Seric ** Snarf header away. 1742900Seric */ 1752900Seric 176*40965Sbostic if (bitset(H_EOH, chompheader(freebuf, FALSE))) 1773058Seric break; 178*40965Sbostic } 1791392Seric 1807673Seric if (tTd(30, 1)) 1812900Seric printf("EOH\n"); 1822900Seric 183*40965Sbostic if (*workbuf == '\0') 184*40965Sbostic { 185*40965Sbostic /* throw away a blank line */ 186*40965Sbostic if (sfgets(buf, MAXFIELD, InChannel) == NULL) 187*40965Sbostic goto readerr; 188*40965Sbostic } 189*40965Sbostic else if (workbuf == buf2) /* guarantee `buf' contains data */ 190*40965Sbostic (void) strcpy(buf, buf2); 1912900Seric 1922900Seric /* 1932900Seric ** Collect the body of the message. 1942900Seric */ 1952900Seric 19615532Seric do 1972900Seric { 1984551Seric register char *bp = buf; 1994156Seric 2007852Seric fixcrlf(buf, TRUE); 2014557Seric 2022900Seric /* check for end-of-message */ 2032900Seric if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 2042900Seric break; 2052900Seric 2064551Seric /* check for transparent dot */ 2079371Seric if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.') 2084551Seric bp++; 2094551Seric 2104156Seric /* 2114156Seric ** Figure message length, output the line to the temp 2124156Seric ** file, and insert a newline if missing. 2134156Seric */ 2144156Seric 2159371Seric CurEnv->e_msgsize += strlen(bp) + 1; 2164551Seric fputs(bp, tf); 2177852Seric fputs("\n", tf); 2181392Seric if (ferror(tf)) 21911544Seric tferror(tf); 22015532Seric } while (sfgets(buf, MAXFIELD, InChannel) != NULL); 221*40965Sbostic 222*40965Sbostic readerr: 22311544Seric if (fflush(tf) != 0) 22411544Seric tferror(tf); 2254083Seric (void) fclose(tf); 2262900Seric 22711145Seric /* An EOF when running SMTP is an error */ 22819036Seric if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP) 22916136Seric { 230*40965Sbostic int usrerr(), syserr(); 23136233Skarels # ifdef LOG 23236233Skarels if (RealHostName != NULL && LogLevel > 0) 23336230Skarels syslog(LOG_NOTICE, 23436230Skarels "collect: unexpected close on connection from %s: %m\n", 23536230Skarels CurEnv->e_from.q_paddr, RealHostName); 23636233Skarels # endif 237*40965Sbostic (feof(InChannel) ? usrerr: syserr) 238*40965Sbostic ("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr); 23911145Seric 24016136Seric /* don't return an error indication */ 24116136Seric CurEnv->e_to = NULL; 24216136Seric CurEnv->e_flags &= ~EF_FATALERRS; 24316136Seric 24416136Seric /* and don't try to deliver the partial message either */ 24516136Seric finis(); 24616136Seric } 24716136Seric 2482900Seric /* 2492900Seric ** Find out some information from the headers. 2503386Seric ** Examples are who is the from person & the date. 2512900Seric */ 2522900Seric 2539371Seric eatheader(CurEnv); 2547673Seric 2557782Seric /* 2567782Seric ** Add an Apparently-To: line if we have no recipient lines. 2577782Seric */ 2584622Seric 2597367Seric if (hvalue("to") == NULL && hvalue("cc") == NULL && 2607367Seric hvalue("bcc") == NULL && hvalue("apparently-to") == NULL) 2617367Seric { 2627367Seric register ADDRESS *q; 2637367Seric 2647367Seric /* create an Apparently-To: field */ 2657367Seric /* that or reject the message.... */ 2667367Seric for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 2677367Seric { 2687389Seric if (q->q_alias != NULL) 2697389Seric continue; 2707673Seric if (tTd(30, 3)) 2717367Seric printf("Adding Apparently-To: %s\n", q->q_paddr); 2727367Seric addheader("apparently-to", q->q_paddr, CurEnv); 2737367Seric } 2747367Seric } 2757367Seric 2769539Seric if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL) 2776986Seric syserr("Cannot reopen %s", CurEnv->e_df); 2781392Seric } 2791392Seric /* 280*40965Sbostic ** FLUSHEOL -- if not at EOL, throw away rest of input line. 281*40965Sbostic ** 282*40965Sbostic ** Parameters: 283*40965Sbostic ** buf -- last line read in (checked for '\n'), 284*40965Sbostic ** fp -- file to be read from. 285*40965Sbostic ** 286*40965Sbostic ** Returns: 287*40965Sbostic ** FALSE on error from sfgets(), TRUE otherwise. 288*40965Sbostic ** 289*40965Sbostic ** Side Effects: 290*40965Sbostic ** none. 291*40965Sbostic */ 292*40965Sbostic 293*40965Sbostic bool 294*40965Sbostic flusheol(buf, fp) 295*40965Sbostic char *buf; 296*40965Sbostic FILE *fp; 297*40965Sbostic { 298*40965Sbostic char junkbuf[MAXLINE], *sfgets(); 299*40965Sbostic register char *p = buf; 300*40965Sbostic 301*40965Sbostic while (index(p, '\n') == NULL) { 302*40965Sbostic if (sfgets(junkbuf,MAXLINE,fp) == NULL) 303*40965Sbostic return(FALSE); 304*40965Sbostic p = junkbuf; 305*40965Sbostic } 306*40965Sbostic 307*40965Sbostic return(TRUE); 308*40965Sbostic } 309*40965Sbostic /* 31011544Seric ** TFERROR -- signal error on writing the temporary file. 31111544Seric ** 31211544Seric ** Parameters: 31311544Seric ** tf -- the file pointer for the temporary file. 31411544Seric ** 31511544Seric ** Returns: 31611544Seric ** none. 31711544Seric ** 31811544Seric ** Side Effects: 31911544Seric ** Gives an error message. 32011544Seric ** Arranges for following output to go elsewhere. 32111544Seric */ 32211544Seric 32311544Seric tferror(tf) 32411544Seric FILE *tf; 32511544Seric { 32611544Seric if (errno == ENOSPC) 32711544Seric { 32811544Seric (void) freopen(CurEnv->e_df, "w", tf); 32911544Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 33011544Seric usrerr("452 Out of disk space for temp file"); 33111544Seric } 33211544Seric else 33311544Seric syserr("collect: Cannot write %s", CurEnv->e_df); 33411544Seric (void) freopen("/dev/null", "w", tf); 33511544Seric } 33611544Seric /* 3372900Seric ** EATFROM -- chew up a UNIX style from line and process 3382900Seric ** 3392900Seric ** This does indeed make some assumptions about the format 3402900Seric ** of UNIX messages. 3412900Seric ** 3422900Seric ** Parameters: 3432900Seric ** fm -- the from line. 3442900Seric ** 3452900Seric ** Returns: 3462900Seric ** none. 3472900Seric ** 3482900Seric ** Side Effects: 3492900Seric ** extracts what information it can from the header, 3503386Seric ** such as the date. 3512900Seric */ 3522900Seric 3534321Seric # ifndef NOTUNIX 3544321Seric 3554203Seric char *DowList[] = 3564203Seric { 3574203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 3584203Seric }; 3594203Seric 3602900Seric char *MonthList[] = 3612900Seric { 3622900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 3632900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 3642900Seric NULL 3652900Seric }; 3662900Seric 3672900Seric eatfrom(fm) 3682900Seric char *fm; 3692900Seric { 3702900Seric register char *p; 3712900Seric register char **dt; 3722900Seric 3737673Seric if (tTd(30, 2)) 3744203Seric printf("eatfrom(%s)\n", fm); 3754203Seric 3762900Seric /* find the date part */ 3772900Seric p = fm; 3782900Seric while (*p != '\0') 3792900Seric { 3802900Seric /* skip a word */ 3812900Seric while (*p != '\0' && *p != ' ') 38216896Seric p++; 3832900Seric while (*p == ' ') 38416896Seric p++; 3852900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 3862900Seric continue; 3872900Seric 3882900Seric /* we have a possible date */ 3894203Seric for (dt = DowList; *dt != NULL; dt++) 3902900Seric if (strncmp(*dt, p, 3) == 0) 3912900Seric break; 3924203Seric if (*dt == NULL) 3934203Seric continue; 3942900Seric 3954203Seric for (dt = MonthList; *dt != NULL; dt++) 3964203Seric if (strncmp(*dt, &p[4], 3) == 0) 3974203Seric break; 3982900Seric if (*dt != NULL) 3992900Seric break; 4002900Seric } 4012900Seric 4022900Seric if (*p != NULL) 4032900Seric { 4043386Seric char *q; 4055366Seric extern char *arpadate(); 4063386Seric 4072900Seric /* we have found a date */ 4083386Seric q = xalloc(25); 40923103Seric (void) strncpy(q, p, 25); 4103386Seric q[24] = '\0'; 4119371Seric define('d', q, CurEnv); 4125366Seric q = arpadate(q); 4139371Seric define('a', newstr(q), CurEnv); 4142900Seric } 4152900Seric } 4164321Seric 4174321Seric # endif NOTUNIX 418