11439Seric # include <errno.h> 23309Seric # include "sendmail.h" 31392Seric 4*7673Seric SCCSID(@(#)collect.c 3.44 08/08/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 char *xfrom; 402900Seric extern char *hvalue(); 414083Seric extern char *mktemp(); 424622Seric static char tempfname[40]; 435192Seric extern char *macvalue(); 44*7673Seric register HDR *h; 45*7673Seric extern HDR *hrvalue(); 461392Seric 471392Seric /* 481392Seric ** Create the temp file name and create the file. 491392Seric */ 501392Seric 517004Seric (void) strcpy(tempfname, QueueDir); 527004Seric (void) strcat(tempfname, "/dfXXXXXX"); 534622Seric (void) mktemp(tempfname); 546888Seric if ((tf = dfopen(tempfname, "w")) == NULL) 551392Seric { 564622Seric syserr("Cannot create %s", tempfname); 575366Seric NoReturn = TRUE; 585366Seric finis(); 591392Seric } 607004Seric (void) chmod(tempfname, 0600); 616986Seric CurEnv->e_df = tempfname; 621392Seric 634316Seric /* 645185Seric ** Create the Mail-From line if we want to. 655185Seric */ 665185Seric 675366Seric if (Smtp && macvalue('s') != NULL) 685185Seric { 695185Seric char xbuf[50]; 705185Seric 715192Seric (void) sprintf(xbuf, "Mail-From: %s$s received by $i at $b", 725185Seric macvalue('r') == NULL ? "" : "$r host "); 736975Seric expand(xbuf, buf, &buf[sizeof buf - 1], CurEnv); 745192Seric (void) chompheader(buf, FALSE); 755185Seric } 765185Seric 775185Seric /* 784322Seric ** Tell ARPANET to go ahead. 794322Seric */ 804322Seric 814710Seric if (sayok) 824710Seric message("354", "Enter mail, end with \".\" on a line by itself"); 834322Seric 844322Seric /* 854316Seric ** Try to read a UNIX-style From line 864316Seric */ 874316Seric 885975Seric if (fgets(buf, sizeof buf, InChannel) == NULL) 894162Seric return; 904557Seric fixcrlf(buf, FALSE); 914321Seric # ifndef NOTUNIX 924322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 932900Seric { 942900Seric eatfrom(buf); 955975Seric (void) fgets(buf, sizeof buf, InChannel); 964557Seric fixcrlf(buf, FALSE); 972900Seric } 984321Seric # endif NOTUNIX 992900Seric 1001392Seric /* 1015975Seric ** Copy InChannel to temp file & do message editing. 1021392Seric ** To keep certain mailers from getting confused, 1031392Seric ** and to keep the output clean, lines that look 1041392Seric ** like UNIX "From" lines are deleted in the header, 1051392Seric ** and prepended with ">" in the body. 1061392Seric */ 1071392Seric 1085975Seric for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL) 1091392Seric { 1104316Seric register char c; 1114316Seric extern bool isheader(); 1124316Seric 1134557Seric fixcrlf(buf, FALSE); 1144557Seric 1152900Seric /* see if the header is over */ 1162900Seric if (!isheader(buf)) 1172900Seric break; 1182900Seric 1192900Seric /* get the rest of this field */ 1205975Seric while ((c = getc(InChannel)) == ' ' || c == '\t') 1211392Seric { 1222900Seric p = &buf[strlen(buf)]; 1232900Seric *p++ = c; 1245975Seric if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL) 1252900Seric break; 1264557Seric fixcrlf(p, FALSE); 1271392Seric } 1285975Seric if (!feof(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; 1392900Seric } 1401392Seric 1412900Seric # ifdef DEBUG 142*7673Seric if (tTd(30, 1)) 1432900Seric printf("EOH\n"); 1442900Seric # endif DEBUG 1452900Seric 1462900Seric /* throw away a blank line */ 1472900Seric if (buf[0] == '\n') 1484557Seric { 1495975Seric (void) fgets(buf, sizeof buf, InChannel); 1504557Seric fixcrlf(buf, FALSE); 1514557Seric } 1522900Seric 1532900Seric /* 1542900Seric ** Collect the body of the message. 1552900Seric */ 1562900Seric 1575975Seric for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL) 1582900Seric { 1594156Seric register int i; 1604551Seric register char *bp = buf; 1614156Seric 1624557Seric fixcrlf(buf, FALSE); 1634557Seric 1642900Seric /* check for end-of-message */ 1652900Seric if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 1662900Seric break; 1672900Seric 1684551Seric /* check for transparent dot */ 1694551Seric if (Smtp && *bp == '.') 1704551Seric bp++; 1714551Seric 1724321Seric # ifndef NOTUNIX 1732900Seric /* Hide UNIX-like From lines */ 1744551Seric if (strncmp(bp, "From ", 5) == 0) 1751392Seric { 1762900Seric fputs(">", tf); 1776901Seric CurEnv->e_msgsize++; 1781392Seric } 1794321Seric # endif NOTUNIX 1804156Seric 1814156Seric /* 1824156Seric ** Figure message length, output the line to the temp 1834156Seric ** file, and insert a newline if missing. 1844156Seric */ 1854156Seric 1864551Seric i = strlen(bp); 1876901Seric CurEnv->e_msgsize += i; 1884551Seric fputs(bp, tf); 1894551Seric if (bp[i - 1] != '\n') 1904156Seric fputs("\n", tf); 1911392Seric if (ferror(tf)) 1921392Seric { 1931439Seric if (errno == ENOSPC) 1941439Seric { 1956986Seric (void) freopen(CurEnv->e_df, "w", tf); 1961439Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 1974557Seric usrerr("452 Out of disk space for temp file"); 1981439Seric } 1991439Seric else 2006986Seric syserr("collect: Cannot write %s", CurEnv->e_df); 2014083Seric (void) freopen("/dev/null", "w", tf); 2021392Seric } 2031392Seric } 2044083Seric (void) fclose(tf); 2052900Seric 2062900Seric /* 2072900Seric ** Find out some information from the headers. 2083386Seric ** Examples are who is the from person & the date. 2092900Seric */ 2102900Seric 211*7673Seric /* message id */ 212*7673Seric h = hrvalue("message-id"); 213*7673Seric if (h == NULL) 214*7673Seric syserr("No Message-Id spec"); 215*7673Seric else if (bitset(H_DEFAULT, h->h_flags)) 216*7673Seric { 217*7673Seric (void) expand(h->h_value, buf, &buf[sizeof buf - 1], CurEnv); 218*7673Seric MsgId = newstr(buf); 219*7673Seric } 220*7673Seric else 221*7673Seric MsgId = h->h_value; 222*7673Seric # ifdef DEBUG 223*7673Seric if (tTd(30, 1)) 224*7673Seric printf("Message-Id: %s\n", MsgId); 225*7673Seric # endif DEBUG 226*7673Seric 2275982Seric /* message priority */ 2285033Seric if (!QueueRun) 2295033Seric { 2305033Seric /* adjust total priority by message priority */ 2316901Seric CurEnv->e_msgpriority = CurEnv->e_msgsize; 2325033Seric p = hvalue("priority"); 2335033Seric if (p != NULL) 2346909Seric CurEnv->e_class = priencode(p); 2356909Seric else 2366909Seric CurEnv->e_class = PRI_NORMAL; 2376909Seric CurEnv->e_msgpriority -= CurEnv->e_class * WKPRIFACT; 2385033Seric } 2394622Seric 2405982Seric /* special handling */ 2415982Seric p = hvalue("special-handling"); 2425982Seric if (p != NULL) 2435982Seric spechandling(p); 2445982Seric 2452900Seric /* from person */ 2462900Seric xfrom = hvalue("sender"); 2472900Seric if (xfrom == NULL) 2486901Seric xfrom = CurEnv->e_origfrom; 2494710Seric if (ArpaMode) 2504316Seric setfrom(xfrom, (char *) NULL); 2512900Seric 2523390Seric /* full name of from person */ 2533390Seric p = hvalue("full-name"); 2543390Seric if (p != NULL) 2553390Seric define('x', p); 2564210Seric else 2574210Seric { 2585917Seric extern char *getxpart(); 2593390Seric 2604210Seric /* 2614210Seric ** Try to extract the full name from a general From: 2624210Seric ** field. We take anything which is a comment as a 2634210Seric ** first choice. Failing in that, we see if there is 2644210Seric ** a "machine readable" name (in <angle brackets>); if 2654210Seric ** so we take anything preceeding that clause. 2664210Seric ** 2674210Seric ** If we blow it here it's not all that serious. 2684210Seric */ 2694210Seric 2704210Seric p = hvalue("original-from"); 2714371Seric if (p == NULL) 2726901Seric p = CurEnv->e_origfrom; 2735917Seric p = getxpart(p); 2745917Seric if (p != NULL) 2755917Seric define('x', newstr(p)); 2764210Seric } 2774210Seric 2782900Seric /* date message originated */ 2794149Seric p = hvalue("posted-date"); 2804149Seric if (p == NULL) 2814149Seric p = hvalue("date"); 2822900Seric if (p != NULL) 2832900Seric { 2843386Seric define('a', p); 2853386Seric /* we don't have a good way to do canonical conversion .... 2863386Seric define('d', newstr(arpatounix(p))); 2873386Seric .... so we will ignore the problem for the time being */ 2882900Seric } 2892900Seric 2907367Seric if (hvalue("to") == NULL && hvalue("cc") == NULL && 2917367Seric hvalue("bcc") == NULL && hvalue("apparently-to") == NULL) 2927367Seric { 2937367Seric register ADDRESS *q; 2947367Seric 2957367Seric /* create an Apparently-To: field */ 2967367Seric /* that or reject the message.... */ 2977367Seric for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 2987367Seric { 2997389Seric if (q->q_alias != NULL) 3007389Seric continue; 3017367Seric # ifdef DEBUG 302*7673Seric if (tTd(30, 3)) 3037367Seric printf("Adding Apparently-To: %s\n", q->q_paddr); 3047367Seric # endif DEBUG 3057367Seric addheader("apparently-to", q->q_paddr, CurEnv); 3067367Seric } 3077367Seric } 3087367Seric 3097364Seric /* check for hop count overflow */ 3107364Seric if (HopCount > MAXHOP) 3117364Seric syserr("Too many hops (%d max); probably forwarding loop", MAXHOP); 3127364Seric 3136986Seric if ((TempFile = fopen(CurEnv->e_df, "r")) == NULL) 3146986Seric syserr("Cannot reopen %s", CurEnv->e_df); 3152900Seric 3162900Seric # ifdef DEBUG 317*7673Seric if (tTd(30, 2)) 3182900Seric { 3194316Seric HDR *h; 3204316Seric extern char *capitalize(); 3214316Seric 3222900Seric printf("----- collected header -----\n"); 3236901Seric for (h = CurEnv->e_header; h != NULL; h = h->h_link) 3242900Seric printf("%s: %s\n", capitalize(h->h_field), h->h_value); 3252900Seric printf("----------------------------\n"); 3262900Seric } 3272900Seric # endif DEBUG 328*7673Seric 329*7673Seric /* 330*7673Seric ** Log collection information. 331*7673Seric */ 332*7673Seric 333*7673Seric # ifdef LOG 334*7673Seric if (LogLevel > 1) 335*7673Seric syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n", MsgId, 336*7673Seric CurEnv->e_from.q_paddr, CurEnv->e_msgsize, 337*7673Seric CurEnv->e_msgpriority); 338*7673Seric # endif LOG 3394162Seric return; 3401392Seric } 3411392Seric /* 3422900Seric ** EATFROM -- chew up a UNIX style from line and process 3432900Seric ** 3442900Seric ** This does indeed make some assumptions about the format 3452900Seric ** of UNIX messages. 3462900Seric ** 3472900Seric ** Parameters: 3482900Seric ** fm -- the from line. 3492900Seric ** 3502900Seric ** Returns: 3512900Seric ** none. 3522900Seric ** 3532900Seric ** Side Effects: 3542900Seric ** extracts what information it can from the header, 3553386Seric ** such as the date. 3562900Seric */ 3572900Seric 3584321Seric # ifndef NOTUNIX 3594321Seric 3604203Seric char *DowList[] = 3614203Seric { 3624203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 3634203Seric }; 3644203Seric 3652900Seric char *MonthList[] = 3662900Seric { 3672900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 3682900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 3692900Seric NULL 3702900Seric }; 3712900Seric 3722900Seric eatfrom(fm) 3732900Seric char *fm; 3742900Seric { 3752900Seric register char *p; 3762900Seric register char **dt; 3772900Seric 3784203Seric # ifdef DEBUG 379*7673Seric if (tTd(30, 2)) 3804203Seric printf("eatfrom(%s)\n", fm); 3814203Seric # endif DEBUG 3824203Seric 3832900Seric /* find the date part */ 3842900Seric p = fm; 3852900Seric while (*p != '\0') 3862900Seric { 3872900Seric /* skip a word */ 3882900Seric while (*p != '\0' && *p != ' ') 3892900Seric *p++; 3902900Seric while (*p == ' ') 3912900Seric *p++; 3922900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 3932900Seric continue; 3942900Seric 3952900Seric /* we have a possible date */ 3964203Seric for (dt = DowList; *dt != NULL; dt++) 3972900Seric if (strncmp(*dt, p, 3) == 0) 3982900Seric break; 3994203Seric if (*dt == NULL) 4004203Seric continue; 4012900Seric 4024203Seric for (dt = MonthList; *dt != NULL; dt++) 4034203Seric if (strncmp(*dt, &p[4], 3) == 0) 4044203Seric break; 4052900Seric if (*dt != NULL) 4062900Seric break; 4072900Seric } 4082900Seric 4092900Seric if (*p != NULL) 4102900Seric { 4113386Seric char *q; 4125366Seric extern char *arpadate(); 4133386Seric 4142900Seric /* we have found a date */ 4153386Seric q = xalloc(25); 4163386Seric strncpy(q, p, 25); 4173386Seric q[24] = '\0'; 4183386Seric define('d', q); 4195366Seric q = arpadate(q); 4205366Seric define('a', newstr(q)); 4212900Seric } 4222900Seric } 4234321Seric 4244321Seric # endif NOTUNIX 4254622Seric /* 4264622Seric ** PRIENCODE -- encode external priority names into internal values. 4274622Seric ** 4284622Seric ** Parameters: 4294622Seric ** p -- priority in ascii. 4304622Seric ** 4314622Seric ** Returns: 4324622Seric ** priority as a numeric level. 4334622Seric ** 4344622Seric ** Side Effects: 4354622Seric ** none. 4364622Seric */ 4374622Seric 4384622Seric struct prio 4394622Seric { 4404622Seric char *pri_name; /* external name of priority */ 4414622Seric int pri_val; /* internal value for same */ 4424622Seric }; 4434622Seric 4444622Seric static struct prio Prio[] = 4454622Seric { 4464633Seric "alert", PRI_ALERT, 4474633Seric "quick", PRI_QUICK, 4484633Seric "first-class", PRI_FIRSTCL, 4494622Seric "normal", PRI_NORMAL, 4504622Seric "second-class", PRI_SECONDCL, 4514622Seric "third-class", PRI_THIRDCL, 4526909Seric "junk", PRI_JUNK, 4534622Seric NULL, PRI_NORMAL, 4544622Seric }; 4554622Seric 4564622Seric priencode(p) 4574622Seric char *p; 4584622Seric { 4594622Seric register struct prio *pl; 4604633Seric extern bool sameword(); 4614622Seric 4624622Seric for (pl = Prio; pl->pri_name != NULL; pl++) 4634622Seric { 4644633Seric if (sameword(p, pl->pri_name)) 4654622Seric break; 4664622Seric } 4674622Seric return (pl->pri_val); 4684622Seric } 4695982Seric /* 4705982Seric ** SPECHANDLE -- do special handling 4715982Seric ** 4725982Seric ** Parameters: 4735982Seric ** p -- pointer to list of special handling words. 4745982Seric ** 4755982Seric ** Returns: 4765982Seric ** none. 4775982Seric ** 4785982Seric ** Side Effects: 4795982Seric ** Sets flags as indicated by p. 4805982Seric */ 4815982Seric 4825982Seric struct handling 4835982Seric { 4845982Seric char *han_name; /* word to get this magic */ 4855982Seric int han_what; /* what to do, see below */ 4865982Seric }; 4875982Seric 4885982Seric /* modes for han_what */ 4895982Seric # define HAN_NONE 0 /* nothing special */ 4905982Seric # define HAN_RRECEIPT 1 /* give return receipt */ 4915982Seric 4925982Seric struct handling Handling[] = 4935982Seric { 4945982Seric "return-receipt-requested", HAN_RRECEIPT, 4955982Seric NULL, HAN_NONE 4965982Seric }; 4975982Seric 4985982Seric spechandling(p) 4995982Seric register char *p; 5005982Seric { 5015982Seric register char *w; 5025982Seric register struct handling *h; 5035982Seric extern bool sameword(); 5045982Seric 5055982Seric while (*p != '\0') 5065982Seric { 5075982Seric /* collect a word to compare to */ 5085982Seric while (*p != '\0' && (*p == ',' || isspace(*p))) 5095982Seric p++; 5105982Seric if (*p == '\0') 5115982Seric break; 5125982Seric w = p; 5135982Seric while (*p != '\0' && *p != ',' && !isspace(*p)) 5145982Seric p++; 5155982Seric if (*p != '\0') 5165982Seric *p++ = '\0'; 5175982Seric 5185982Seric /* scan the special handling table */ 5195982Seric for (h = Handling; h->han_name != NULL; h++) 5205982Seric if (sameword(h->han_name, w)) 5215982Seric break; 5225982Seric 5235982Seric /* see if we can do anything interesting */ 5245982Seric switch (h->han_what) 5255982Seric { 5265982Seric case HAN_NONE: /* nothing to be done */ 5275982Seric break; 5285982Seric 5295982Seric case HAN_RRECEIPT: /* give return receipt */ 5306901Seric CurEnv->e_retreceipt = TRUE; 5315982Seric # ifdef DEBUG 532*7673Seric if (tTd(30, 3)) 5335982Seric printf(">>> Return receipt requested\n"); 5345982Seric # endif DEBUG 5355982Seric break; 5365982Seric 5375982Seric default: 5385982Seric syserr("spechandling: handling %d (%s)", h->han_what, w); 5395982Seric } 5405982Seric } 5415982Seric } 542