11392Seric # include <stdio.h> 21392Seric # include <ctype.h> 31439Seric # include <errno.h> 43309Seric # include "sendmail.h" 51392Seric 6*4083Seric static char SccsId[] = "@(#)collect.c 3.11 08/09/81"; 71392Seric 81392Seric /* 92969Seric ** COLLECT -- read & parse message header & make temp file. 101392Seric ** 111392Seric ** Creates a temporary file name and copies the standard 121392Seric ** input to that file. While it is doing it, it looks for 131392Seric ** "From:" and "Sender:" fields to use as the from-person 141392Seric ** (but only if the -a flag is specified). It prefers to 151392Seric ** to use the "Sender:" field. 161392Seric ** 171392Seric ** MIT seems to like to produce "Sent-By:" fields instead 181392Seric ** of "Sender:" fields. We used to catch this, but it turns 191392Seric ** out that the "Sent-By:" field doesn't always correspond 201392Seric ** to someone real ("___057", for instance), as required by 211392Seric ** the protocol. So we limp by..... 221392Seric ** 231392Seric ** Parameters: 241875Seric ** none 251392Seric ** 261392Seric ** Returns: 271875Seric ** Name of temp file. 281392Seric ** 291392Seric ** Side Effects: 301392Seric ** Temp file is created and filled. 311392Seric ** 321392Seric ** Called By: 331392Seric ** main 341392Seric ** 351392Seric ** Notes: 361392Seric ** This is broken off from main largely so that the 371392Seric ** temp buffer can be deallocated. 381392Seric */ 391392Seric 401624Seric long MsgSize; /* size of message in bytes */ 411397Seric 421392Seric char * 432969Seric collect() 441392Seric { 451392Seric register FILE *tf; 461392Seric char buf[MAXFIELD+1]; 471392Seric register char *p; 481392Seric char c; 492900Seric extern bool isheader(); 502900Seric char *xfrom; 512900Seric extern char *hvalue(); 52*4083Seric extern char *mktemp(); 53*4083Seric extern char *capitalize(); 54*4083Seric # ifdef DEBUG 553386Seric HDR *h; 56*4083Seric # endif 571392Seric 581392Seric /* 591392Seric ** Create the temp file name and create the file. 601392Seric */ 611392Seric 62*4083Seric (void) mktemp(InFileName); 63*4083Seric (void) close(creat(InFileName, 0600)); 641392Seric if ((tf = fopen(InFileName, "w")) == NULL) 651392Seric { 661392Seric syserr("Cannot create %s", InFileName); 671392Seric return (NULL); 681392Seric } 691392Seric 702900Seric /* try to read a UNIX-style From line */ 712900Seric if (fgets(buf, sizeof buf, stdin) == NULL) 722900Seric return (NULL); 732900Seric if (strncmp(buf, "From ", 5) == 0) 742900Seric { 752900Seric eatfrom(buf); 76*4083Seric (void) fgets(buf, sizeof buf, stdin); 772900Seric } 782900Seric 791392Seric /* 801392Seric ** Copy stdin to temp file & do message editting. 811392Seric ** To keep certain mailers from getting confused, 821392Seric ** and to keep the output clean, lines that look 831392Seric ** like UNIX "From" lines are deleted in the header, 841392Seric ** and prepended with ">" in the body. 851392Seric */ 861392Seric 872900Seric for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin)) 881392Seric { 892900Seric /* see if the header is over */ 902900Seric if (!isheader(buf)) 912900Seric break; 922900Seric 932900Seric /* get the rest of this field */ 942900Seric while ((c = getc(stdin)) == ' ' || c == '\t') 951392Seric { 962900Seric p = &buf[strlen(buf)]; 972900Seric *p++ = c; 982900Seric if (fgets(p, sizeof buf - (p - buf), stdin) == NULL) 992900Seric break; 1001392Seric } 101*4083Seric if (!feof(stdin)) 102*4083Seric (void) ungetc(c, stdin); 1031392Seric 1042900Seric MsgSize += strlen(buf); 1051392Seric 1062900Seric /* 1072900Seric ** Snarf header away. 1082900Seric */ 1092900Seric 1103391Seric if (bitset(H_EOH, chompheader(buf, FALSE))) 1113058Seric break; 1122900Seric } 1131392Seric 1142900Seric # ifdef DEBUG 1152900Seric if (Debug) 1162900Seric printf("EOH\n"); 1172900Seric # endif DEBUG 1182900Seric 1192900Seric /* throw away a blank line */ 1202900Seric if (buf[0] == '\n') 121*4083Seric (void) fgets(buf, sizeof buf, stdin); 1222900Seric 1232900Seric /* 1242900Seric ** Collect the body of the message. 1252900Seric */ 1262900Seric 1272900Seric for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL) 1282900Seric { 1292900Seric /* check for end-of-message */ 1302900Seric if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 1312900Seric break; 1322900Seric 1332900Seric /* Hide UNIX-like From lines */ 1342900Seric if (strncmp(buf, "From ", 5) == 0) 1351392Seric { 1362900Seric fputs(">", tf); 1372900Seric MsgSize++; 1381392Seric } 1391624Seric MsgSize += strlen(buf); 1401392Seric fputs(buf, tf); 1411392Seric if (ferror(tf)) 1421392Seric { 1431439Seric if (errno == ENOSPC) 1441439Seric { 145*4083Seric (void) freopen(InFileName, "w", tf); 1461439Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 1471439Seric syserr("Out of disk space for temp file"); 1481439Seric } 1491439Seric else 1501439Seric syserr("Cannot write %s", InFileName); 151*4083Seric (void) freopen("/dev/null", "w", tf); 1521392Seric } 1531392Seric } 154*4083Seric (void) fclose(tf); 1552900Seric 1562900Seric /* 1572900Seric ** Find out some information from the headers. 1583386Seric ** Examples are who is the from person & the date. 1592900Seric */ 1602900Seric 1612900Seric /* from person */ 1622900Seric xfrom = hvalue("sender"); 1632900Seric if (xfrom == NULL) 1642900Seric xfrom = hvalue("from"); 1652900Seric 1663390Seric /* full name of from person */ 1673390Seric p = hvalue("full-name"); 1683390Seric if (p != NULL) 1693390Seric define('x', p); 1703390Seric 1712900Seric /* date message originated */ 1722900Seric p = hvalue("date"); 1732900Seric if (p != NULL) 1742900Seric { 1753386Seric define('a', p); 1763386Seric /* we don't have a good way to do canonical conversion .... 1773386Seric define('d', newstr(arpatounix(p))); 1783386Seric .... so we will ignore the problem for the time being */ 1792900Seric } 1802900Seric 1811392Seric if (freopen(InFileName, "r", stdin) == NULL) 1821392Seric syserr("Cannot reopen %s", InFileName); 1832900Seric 1842900Seric # ifdef DEBUG 1852900Seric if (Debug) 1862900Seric { 1872900Seric printf("----- collected header -----\n"); 1882900Seric for (h = Header; h != NULL; h = h->h_link) 1892900Seric printf("%s: %s\n", capitalize(h->h_field), h->h_value); 1902900Seric printf("----------------------------\n"); 1912900Seric } 1922900Seric # endif DEBUG 1932900Seric return (ArpaFmt ? xfrom : NULL); 1941392Seric } 1951392Seric /* 1962900Seric ** EATFROM -- chew up a UNIX style from line and process 1972900Seric ** 1982900Seric ** This does indeed make some assumptions about the format 1992900Seric ** of UNIX messages. 2002900Seric ** 2012900Seric ** Parameters: 2022900Seric ** fm -- the from line. 2032900Seric ** 2042900Seric ** Returns: 2052900Seric ** none. 2062900Seric ** 2072900Seric ** Side Effects: 2082900Seric ** extracts what information it can from the header, 2093386Seric ** such as the date. 2102900Seric */ 2112900Seric 2122900Seric char *MonthList[] = 2132900Seric { 2142900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 2152900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 2162900Seric NULL 2172900Seric }; 2182900Seric 2192900Seric eatfrom(fm) 2202900Seric char *fm; 2212900Seric { 2222900Seric register char *p; 2232900Seric register char **dt; 2242900Seric 2252900Seric /* find the date part */ 2262900Seric p = fm; 2272900Seric while (*p != '\0') 2282900Seric { 2292900Seric /* skip a word */ 2302900Seric while (*p != '\0' && *p != ' ') 2312900Seric *p++; 2322900Seric while (*p == ' ') 2332900Seric *p++; 2342900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 2352900Seric continue; 2362900Seric 2372900Seric /* we have a possible date */ 2382900Seric for (dt = MonthList; *dt != NULL; dt++) 2392900Seric if (strncmp(*dt, p, 3) == 0) 2402900Seric break; 2412900Seric 2422900Seric if (*dt != NULL) 2432900Seric break; 2442900Seric } 2452900Seric 2462900Seric if (*p != NULL) 2472900Seric { 2483386Seric char *q; 2493386Seric 2502900Seric /* we have found a date */ 2513386Seric q = xalloc(25); 2523386Seric strncpy(q, p, 25); 2533386Seric q[24] = '\0'; 2543386Seric define('d', q); 2552900Seric } 2562900Seric } 2572900Seric /* 2582900Seric ** HVALUE -- return value of a header. 2592900Seric ** 2603386Seric ** Only "real" fields (i.e., ones that have not been supplied 2613386Seric ** as a default) are used. 2623386Seric ** 2632900Seric ** Parameters: 2642900Seric ** field -- the field name. 2652900Seric ** 2662900Seric ** Returns: 2672900Seric ** pointer to the value part. 2682900Seric ** NULL if not found. 2692900Seric ** 2702900Seric ** Side Effects: 2712900Seric ** sets the H_USED bit in the header if found. 2722900Seric */ 2732900Seric 2742900Seric char * 2752900Seric hvalue(field) 2762900Seric char *field; 2772900Seric { 2782900Seric register HDR *h; 2792900Seric 2802900Seric for (h = Header; h != NULL; h = h->h_link) 2812900Seric { 2823386Seric if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0) 2832900Seric { 2842900Seric h->h_flags |= H_USED; 2852900Seric return (h->h_value); 2862900Seric } 2872900Seric } 2882900Seric return (NULL); 2892900Seric } 2902900Seric /* 2912900Seric ** ISHEADER -- predicate telling if argument is a header. 2922900Seric ** 2932900Seric ** Parameters: 2942900Seric ** s -- string to check for possible headerness. 2952900Seric ** 2962900Seric ** Returns: 2972900Seric ** TRUE if s is a header. 2982900Seric ** FALSE otherwise. 2992900Seric ** 3002900Seric ** Side Effects: 3012900Seric ** none. 3022900Seric */ 3032900Seric 3042900Seric bool 3052900Seric isheader(s) 3062900Seric register char *s; 3072900Seric { 3082900Seric if (!isalnum(*s)) 3092900Seric return (FALSE); 3102900Seric while (!isspace(*s) && *s != ':') 3112900Seric s++; 3122900Seric while (isspace(*s)) 3132900Seric s++; 3142900Seric return (*s == ':'); 3152900Seric } 3163386Seric /* 3173386Seric ** CHOMPHEADER -- process and save a header line. 3183386Seric ** 3193386Seric ** Called by collect and by readcf to deal with header lines. 3203386Seric ** 3213386Seric ** Parameters: 3223386Seric ** line -- header as a text line. 3233391Seric ** def -- if set, this is a default value. 3243386Seric ** 3253386Seric ** Returns: 3263386Seric ** flags for this header. 3273386Seric ** 3283386Seric ** Side Effects: 3293386Seric ** The header is saved on the header list. 3303386Seric */ 3313386Seric 3323391Seric chompheader(line, def) 3333386Seric char *line; 3343391Seric bool def; 3353386Seric { 3363386Seric register char *p; 3373386Seric register HDR *h; 3383386Seric HDR **hp; 3393386Seric extern bool isheader(); 3403386Seric char *fname; 3413386Seric char *fvalue; 3423386Seric struct hdrinfo *hi; 3433386Seric 3443386Seric /* strip off trailing newline */ 3453386Seric p = rindex(line, '\n'); 3463386Seric if (p != NULL) 3473386Seric *p = '\0'; 3483386Seric 3493386Seric /* find canonical name */ 3503386Seric fname = line; 3513386Seric p = index(line, ':'); 3523386Seric fvalue = &p[1]; 3533386Seric while (isspace(*--p)) 3543386Seric continue; 3553386Seric *++p = '\0'; 3563386Seric makelower(fname); 3573386Seric 3583386Seric /* strip field value on front */ 3593386Seric if (*fvalue == ' ') 3603386Seric fvalue++; 3613386Seric 3623386Seric /* search header list for this header */ 3633386Seric for (hp = &Header, h = Header; h != NULL; hp = &h->h_link, h = h->h_link) 3643386Seric { 3653386Seric if (strcmp(fname, h->h_field) == 0 && bitset(H_DEFAULT, h->h_flags)) 3663386Seric break; 3673386Seric } 3683386Seric 3693386Seric /* see if it is a known type */ 3703386Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 3713386Seric { 3723386Seric if (strcmp(hi->hi_field, fname) == 0) 3733386Seric break; 3743386Seric } 3753386Seric 3763386Seric /* if this means "end of header" quit now */ 3773386Seric if (bitset(H_EOH, hi->hi_flags)) 3783386Seric return (hi->hi_flags); 3793386Seric 3803386Seric /* create/fill in a new node */ 3813386Seric if (h == NULL) 3823386Seric { 3833386Seric /* create a new node */ 3843386Seric *hp = h = (HDR *) xalloc(sizeof *h); 3853386Seric h->h_field = newstr(fname); 3863386Seric h->h_value = NULL; 3873386Seric h->h_link = NULL; 3883391Seric h->h_flags = hi->hi_flags; 3893386Seric h->h_mflags = hi->hi_mflags; 3903386Seric } 3913391Seric if (def) 3923391Seric h->h_flags |= H_DEFAULT; 3933386Seric else 3943391Seric h->h_flags &= ~H_CHECK; 3953386Seric if (h->h_value != NULL) 3963386Seric free(h->h_value); 3973386Seric h->h_value = newstr(fvalue); 3983386Seric 3993386Seric return (h->h_flags); 4003386Seric } 401