11439Seric # include <errno.h> 23309Seric # include "sendmail.h" 31392Seric 4*7681Seric SCCSID(@(#)collect.c 3.45 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(); 447673Seric register HDR *h; 457673Seric 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 113*7681Seric /* if the line is too long, throw the rest away */ 114*7681Seric if (index(buf, '\n') == NULL) 115*7681Seric { 116*7681Seric while ((c = getc(InChannel)) != '\n') 117*7681Seric continue; 118*7681Seric /* give an error? */ 119*7681Seric } 120*7681Seric 1214557Seric fixcrlf(buf, FALSE); 1224557Seric 1232900Seric /* see if the header is over */ 1242900Seric if (!isheader(buf)) 1252900Seric break; 1262900Seric 1272900Seric /* get the rest of this field */ 1285975Seric while ((c = getc(InChannel)) == ' ' || c == '\t') 1291392Seric { 1302900Seric p = &buf[strlen(buf)]; 1312900Seric *p++ = c; 1325975Seric if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL) 1332900Seric break; 1344557Seric fixcrlf(p, FALSE); 1351392Seric } 1365975Seric if (!feof(InChannel)) 1375975Seric (void) ungetc(c, InChannel); 1381392Seric 1396901Seric CurEnv->e_msgsize += strlen(buf); 1401392Seric 1412900Seric /* 1422900Seric ** Snarf header away. 1432900Seric */ 1442900Seric 1453391Seric if (bitset(H_EOH, chompheader(buf, FALSE))) 1463058Seric break; 1472900Seric } 1481392Seric 1492900Seric # ifdef DEBUG 1507673Seric if (tTd(30, 1)) 1512900Seric printf("EOH\n"); 1522900Seric # endif DEBUG 1532900Seric 1542900Seric /* throw away a blank line */ 1552900Seric if (buf[0] == '\n') 1564557Seric { 1575975Seric (void) fgets(buf, sizeof buf, InChannel); 1584557Seric fixcrlf(buf, FALSE); 1594557Seric } 1602900Seric 1612900Seric /* 1622900Seric ** Collect the body of the message. 1632900Seric */ 1642900Seric 1655975Seric for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL) 1662900Seric { 1674156Seric register int i; 1684551Seric register char *bp = buf; 1694156Seric 1704557Seric fixcrlf(buf, FALSE); 1714557Seric 1722900Seric /* check for end-of-message */ 1732900Seric if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 1742900Seric break; 1752900Seric 1764551Seric /* check for transparent dot */ 1774551Seric if (Smtp && *bp == '.') 1784551Seric bp++; 1794551Seric 1804321Seric # ifndef NOTUNIX 1812900Seric /* Hide UNIX-like From lines */ 1824551Seric if (strncmp(bp, "From ", 5) == 0) 1831392Seric { 1842900Seric fputs(">", tf); 1856901Seric CurEnv->e_msgsize++; 1861392Seric } 1874321Seric # endif NOTUNIX 1884156Seric 1894156Seric /* 1904156Seric ** Figure message length, output the line to the temp 1914156Seric ** file, and insert a newline if missing. 1924156Seric */ 1934156Seric 1944551Seric i = strlen(bp); 1956901Seric CurEnv->e_msgsize += i; 1964551Seric fputs(bp, tf); 1974551Seric if (bp[i - 1] != '\n') 1984156Seric fputs("\n", tf); 1991392Seric if (ferror(tf)) 2001392Seric { 2011439Seric if (errno == ENOSPC) 2021439Seric { 2036986Seric (void) freopen(CurEnv->e_df, "w", tf); 2041439Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 2054557Seric usrerr("452 Out of disk space for temp file"); 2061439Seric } 2071439Seric else 2086986Seric syserr("collect: Cannot write %s", CurEnv->e_df); 2094083Seric (void) freopen("/dev/null", "w", tf); 2101392Seric } 2111392Seric } 2124083Seric (void) fclose(tf); 2132900Seric 2142900Seric /* 2152900Seric ** Find out some information from the headers. 2163386Seric ** Examples are who is the from person & the date. 2172900Seric */ 2182900Seric 2197673Seric /* message id */ 2207673Seric h = hrvalue("message-id"); 2217673Seric if (h == NULL) 2227673Seric syserr("No Message-Id spec"); 2237673Seric else if (bitset(H_DEFAULT, h->h_flags)) 2247673Seric { 2257673Seric (void) expand(h->h_value, buf, &buf[sizeof buf - 1], CurEnv); 2267673Seric MsgId = newstr(buf); 2277673Seric } 2287673Seric else 2297673Seric MsgId = h->h_value; 2307673Seric # ifdef DEBUG 2317673Seric if (tTd(30, 1)) 2327673Seric printf("Message-Id: %s\n", MsgId); 2337673Seric # endif DEBUG 2347673Seric 2355982Seric /* message priority */ 2365033Seric if (!QueueRun) 2375033Seric { 2385033Seric /* adjust total priority by message priority */ 2396901Seric CurEnv->e_msgpriority = CurEnv->e_msgsize; 2405033Seric p = hvalue("priority"); 2415033Seric if (p != NULL) 2426909Seric CurEnv->e_class = priencode(p); 2436909Seric else 2446909Seric CurEnv->e_class = PRI_NORMAL; 2456909Seric CurEnv->e_msgpriority -= CurEnv->e_class * WKPRIFACT; 2465033Seric } 2474622Seric 2485982Seric /* special handling */ 2495982Seric p = hvalue("special-handling"); 2505982Seric if (p != NULL) 2515982Seric spechandling(p); 2525982Seric 2532900Seric /* from person */ 2542900Seric xfrom = hvalue("sender"); 2552900Seric if (xfrom == NULL) 2566901Seric xfrom = CurEnv->e_origfrom; 2574710Seric if (ArpaMode) 2584316Seric setfrom(xfrom, (char *) NULL); 2592900Seric 2603390Seric /* full name of from person */ 2613390Seric p = hvalue("full-name"); 2623390Seric if (p != NULL) 2633390Seric define('x', p); 2644210Seric else 2654210Seric { 2665917Seric extern char *getxpart(); 2673390Seric 2684210Seric /* 2694210Seric ** Try to extract the full name from a general From: 2704210Seric ** field. We take anything which is a comment as a 2714210Seric ** first choice. Failing in that, we see if there is 2724210Seric ** a "machine readable" name (in <angle brackets>); if 2734210Seric ** so we take anything preceeding that clause. 2744210Seric ** 2754210Seric ** If we blow it here it's not all that serious. 2764210Seric */ 2774210Seric 2784210Seric p = hvalue("original-from"); 2794371Seric if (p == NULL) 2806901Seric p = CurEnv->e_origfrom; 2815917Seric p = getxpart(p); 2825917Seric if (p != NULL) 2835917Seric define('x', newstr(p)); 2844210Seric } 2854210Seric 2862900Seric /* date message originated */ 2874149Seric p = hvalue("posted-date"); 2884149Seric if (p == NULL) 2894149Seric p = hvalue("date"); 2902900Seric if (p != NULL) 2912900Seric { 2923386Seric define('a', p); 2933386Seric /* we don't have a good way to do canonical conversion .... 2943386Seric define('d', newstr(arpatounix(p))); 2953386Seric .... so we will ignore the problem for the time being */ 2962900Seric } 2972900Seric 2987367Seric if (hvalue("to") == NULL && hvalue("cc") == NULL && 2997367Seric hvalue("bcc") == NULL && hvalue("apparently-to") == NULL) 3007367Seric { 3017367Seric register ADDRESS *q; 3027367Seric 3037367Seric /* create an Apparently-To: field */ 3047367Seric /* that or reject the message.... */ 3057367Seric for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 3067367Seric { 3077389Seric if (q->q_alias != NULL) 3087389Seric continue; 3097367Seric # ifdef DEBUG 3107673Seric if (tTd(30, 3)) 3117367Seric printf("Adding Apparently-To: %s\n", q->q_paddr); 3127367Seric # endif DEBUG 3137367Seric addheader("apparently-to", q->q_paddr, CurEnv); 3147367Seric } 3157367Seric } 3167367Seric 3177364Seric /* check for hop count overflow */ 3187364Seric if (HopCount > MAXHOP) 3197364Seric syserr("Too many hops (%d max); probably forwarding loop", MAXHOP); 3207364Seric 3216986Seric if ((TempFile = fopen(CurEnv->e_df, "r")) == NULL) 3226986Seric syserr("Cannot reopen %s", CurEnv->e_df); 3232900Seric 3242900Seric # ifdef DEBUG 3257673Seric if (tTd(30, 2)) 3262900Seric { 3274316Seric HDR *h; 3284316Seric extern char *capitalize(); 3294316Seric 3302900Seric printf("----- collected header -----\n"); 3316901Seric for (h = CurEnv->e_header; h != NULL; h = h->h_link) 3322900Seric printf("%s: %s\n", capitalize(h->h_field), h->h_value); 3332900Seric printf("----------------------------\n"); 3342900Seric } 3352900Seric # endif DEBUG 3367673Seric 3377673Seric /* 3387673Seric ** Log collection information. 3397673Seric */ 3407673Seric 3417673Seric # ifdef LOG 3427673Seric if (LogLevel > 1) 3437673Seric syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n", MsgId, 3447673Seric CurEnv->e_from.q_paddr, CurEnv->e_msgsize, 3457673Seric CurEnv->e_msgpriority); 3467673Seric # endif LOG 3474162Seric return; 3481392Seric } 3491392Seric /* 3502900Seric ** EATFROM -- chew up a UNIX style from line and process 3512900Seric ** 3522900Seric ** This does indeed make some assumptions about the format 3532900Seric ** of UNIX messages. 3542900Seric ** 3552900Seric ** Parameters: 3562900Seric ** fm -- the from line. 3572900Seric ** 3582900Seric ** Returns: 3592900Seric ** none. 3602900Seric ** 3612900Seric ** Side Effects: 3622900Seric ** extracts what information it can from the header, 3633386Seric ** such as the date. 3642900Seric */ 3652900Seric 3664321Seric # ifndef NOTUNIX 3674321Seric 3684203Seric char *DowList[] = 3694203Seric { 3704203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 3714203Seric }; 3724203Seric 3732900Seric char *MonthList[] = 3742900Seric { 3752900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 3762900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 3772900Seric NULL 3782900Seric }; 3792900Seric 3802900Seric eatfrom(fm) 3812900Seric char *fm; 3822900Seric { 3832900Seric register char *p; 3842900Seric register char **dt; 3852900Seric 3864203Seric # ifdef DEBUG 3877673Seric if (tTd(30, 2)) 3884203Seric printf("eatfrom(%s)\n", fm); 3894203Seric # endif DEBUG 3904203Seric 3912900Seric /* find the date part */ 3922900Seric p = fm; 3932900Seric while (*p != '\0') 3942900Seric { 3952900Seric /* skip a word */ 3962900Seric while (*p != '\0' && *p != ' ') 3972900Seric *p++; 3982900Seric while (*p == ' ') 3992900Seric *p++; 4002900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 4012900Seric continue; 4022900Seric 4032900Seric /* we have a possible date */ 4044203Seric for (dt = DowList; *dt != NULL; dt++) 4052900Seric if (strncmp(*dt, p, 3) == 0) 4062900Seric break; 4074203Seric if (*dt == NULL) 4084203Seric continue; 4092900Seric 4104203Seric for (dt = MonthList; *dt != NULL; dt++) 4114203Seric if (strncmp(*dt, &p[4], 3) == 0) 4124203Seric break; 4132900Seric if (*dt != NULL) 4142900Seric break; 4152900Seric } 4162900Seric 4172900Seric if (*p != NULL) 4182900Seric { 4193386Seric char *q; 4205366Seric extern char *arpadate(); 4213386Seric 4222900Seric /* we have found a date */ 4233386Seric q = xalloc(25); 4243386Seric strncpy(q, p, 25); 4253386Seric q[24] = '\0'; 4263386Seric define('d', q); 4275366Seric q = arpadate(q); 4285366Seric define('a', newstr(q)); 4292900Seric } 4302900Seric } 4314321Seric 4324321Seric # endif NOTUNIX 4334622Seric /* 4344622Seric ** PRIENCODE -- encode external priority names into internal values. 4354622Seric ** 4364622Seric ** Parameters: 4374622Seric ** p -- priority in ascii. 4384622Seric ** 4394622Seric ** Returns: 4404622Seric ** priority as a numeric level. 4414622Seric ** 4424622Seric ** Side Effects: 4434622Seric ** none. 4444622Seric */ 4454622Seric 4464622Seric struct prio 4474622Seric { 4484622Seric char *pri_name; /* external name of priority */ 4494622Seric int pri_val; /* internal value for same */ 4504622Seric }; 4514622Seric 4524622Seric static struct prio Prio[] = 4534622Seric { 4544633Seric "alert", PRI_ALERT, 4554633Seric "quick", PRI_QUICK, 4564633Seric "first-class", PRI_FIRSTCL, 4574622Seric "normal", PRI_NORMAL, 4584622Seric "second-class", PRI_SECONDCL, 4594622Seric "third-class", PRI_THIRDCL, 4606909Seric "junk", PRI_JUNK, 4614622Seric NULL, PRI_NORMAL, 4624622Seric }; 4634622Seric 4644622Seric priencode(p) 4654622Seric char *p; 4664622Seric { 4674622Seric register struct prio *pl; 4684633Seric extern bool sameword(); 4694622Seric 4704622Seric for (pl = Prio; pl->pri_name != NULL; pl++) 4714622Seric { 4724633Seric if (sameword(p, pl->pri_name)) 4734622Seric break; 4744622Seric } 4754622Seric return (pl->pri_val); 4764622Seric } 4775982Seric /* 4785982Seric ** SPECHANDLE -- do special handling 4795982Seric ** 4805982Seric ** Parameters: 4815982Seric ** p -- pointer to list of special handling words. 4825982Seric ** 4835982Seric ** Returns: 4845982Seric ** none. 4855982Seric ** 4865982Seric ** Side Effects: 4875982Seric ** Sets flags as indicated by p. 4885982Seric */ 4895982Seric 4905982Seric struct handling 4915982Seric { 4925982Seric char *han_name; /* word to get this magic */ 4935982Seric int han_what; /* what to do, see below */ 4945982Seric }; 4955982Seric 4965982Seric /* modes for han_what */ 4975982Seric # define HAN_NONE 0 /* nothing special */ 4985982Seric # define HAN_RRECEIPT 1 /* give return receipt */ 4995982Seric 5005982Seric struct handling Handling[] = 5015982Seric { 5025982Seric "return-receipt-requested", HAN_RRECEIPT, 5035982Seric NULL, HAN_NONE 5045982Seric }; 5055982Seric 5065982Seric spechandling(p) 5075982Seric register char *p; 5085982Seric { 5095982Seric register char *w; 5105982Seric register struct handling *h; 5115982Seric extern bool sameword(); 5125982Seric 5135982Seric while (*p != '\0') 5145982Seric { 5155982Seric /* collect a word to compare to */ 5165982Seric while (*p != '\0' && (*p == ',' || isspace(*p))) 5175982Seric p++; 5185982Seric if (*p == '\0') 5195982Seric break; 5205982Seric w = p; 5215982Seric while (*p != '\0' && *p != ',' && !isspace(*p)) 5225982Seric p++; 5235982Seric if (*p != '\0') 5245982Seric *p++ = '\0'; 5255982Seric 5265982Seric /* scan the special handling table */ 5275982Seric for (h = Handling; h->han_name != NULL; h++) 5285982Seric if (sameword(h->han_name, w)) 5295982Seric break; 5305982Seric 5315982Seric /* see if we can do anything interesting */ 5325982Seric switch (h->han_what) 5335982Seric { 5345982Seric case HAN_NONE: /* nothing to be done */ 5355982Seric break; 5365982Seric 5375982Seric case HAN_RRECEIPT: /* give return receipt */ 5386901Seric CurEnv->e_retreceipt = TRUE; 5395982Seric # ifdef DEBUG 5407673Seric if (tTd(30, 3)) 5415982Seric printf(">>> Return receipt requested\n"); 5425982Seric # endif DEBUG 5435982Seric break; 5445982Seric 5455982Seric default: 5465982Seric syserr("spechandling: handling %d (%s)", h->han_what, w); 5475982Seric } 5485982Seric } 5495982Seric } 550