11392Seric # include <stdio.h>
21392Seric # include <ctype.h>
31439Seric # include <errno.h>
43309Seric # include "sendmail.h"
51392Seric 
6*3391Seric static char	SccsId[] = "@(#)collect.c	3.10	03/28/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;
491439Seric 	extern int errno;
502900Seric 	extern bool isheader();
512900Seric 	extern char *newstr();
522900Seric 	extern char *xalloc();
532900Seric 	extern char *index(), *rindex();
542900Seric 	char *xfrom;
552900Seric 	extern char *hvalue();
562988Seric 	extern char *strcpy(), *strcat(), *mktemp();
573386Seric 	HDR *h;
581392Seric 
591392Seric 	/*
601392Seric 	**  Create the temp file name and create the file.
611392Seric 	*/
621392Seric 
631392Seric 	mktemp(InFileName);
641392Seric 	close(creat(InFileName, 0600));
651392Seric 	if ((tf = fopen(InFileName, "w")) == NULL)
661392Seric 	{
671392Seric 		syserr("Cannot create %s", InFileName);
681392Seric 		return (NULL);
691392Seric 	}
701392Seric 
712900Seric 	/* try to read a UNIX-style From line */
722900Seric 	if (fgets(buf, sizeof buf, stdin) == NULL)
732900Seric 		return (NULL);
742900Seric 	if (strncmp(buf, "From ", 5) == 0)
752900Seric 	{
762900Seric 		eatfrom(buf);
772900Seric 		fgets(buf, sizeof buf, stdin);
782900Seric 	}
792900Seric 
801392Seric 	/*
811392Seric 	**  Copy stdin to temp file & do message editting.
821392Seric 	**	To keep certain mailers from getting confused,
831392Seric 	**	and to keep the output clean, lines that look
841392Seric 	**	like UNIX "From" lines are deleted in the header,
851392Seric 	**	and prepended with ">" in the body.
861392Seric 	*/
871392Seric 
882900Seric 	for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin))
891392Seric 	{
902900Seric 		/* see if the header is over */
912900Seric 		if (!isheader(buf))
922900Seric 			break;
932900Seric 
942900Seric 		/* get the rest of this field */
952900Seric 		while ((c = getc(stdin)) == ' ' || c == '\t')
961392Seric 		{
972900Seric 			p = &buf[strlen(buf)];
982900Seric 			*p++ = c;
992900Seric 			if (fgets(p, sizeof buf - (p - buf), stdin) == NULL)
1002900Seric 				break;
1011392Seric 		}
1022900Seric 		if (c != EOF)
1032900Seric 			ungetc(c, stdin);
1041392Seric 
1052900Seric 		MsgSize += strlen(buf);
1061392Seric 
1072900Seric 		/*
1082900Seric 		**  Snarf header away.
1092900Seric 		*/
1102900Seric 
111*3391Seric 		if (bitset(H_EOH, chompheader(buf, FALSE)))
1123058Seric 			break;
1132900Seric 	}
1141392Seric 
1152900Seric # ifdef DEBUG
1162900Seric 	if (Debug)
1172900Seric 		printf("EOH\n");
1182900Seric # endif DEBUG
1192900Seric 
1202900Seric 	/* throw away a blank line */
1212900Seric 	if (buf[0] == '\n')
1222900Seric 		fgets(buf, sizeof buf, stdin);
1232900Seric 
1242900Seric 	/*
1252900Seric 	**  Collect the body of the message.
1262900Seric 	*/
1272900Seric 
1282900Seric 	for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL)
1292900Seric 	{
1302900Seric 		/* check for end-of-message */
1312900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1322900Seric 			break;
1332900Seric 
1342900Seric 		/* Hide UNIX-like From lines */
1352900Seric 		if (strncmp(buf, "From ", 5) == 0)
1361392Seric 		{
1372900Seric 			fputs(">", tf);
1382900Seric 			MsgSize++;
1391392Seric 		}
1401624Seric 		MsgSize += strlen(buf);
1411392Seric 		fputs(buf, tf);
1421392Seric 		if (ferror(tf))
1431392Seric 		{
1441439Seric 			if (errno == ENOSPC)
1451439Seric 			{
1461439Seric 				freopen(InFileName, "w", tf);
1471439Seric 				fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
1481439Seric 				syserr("Out of disk space for temp file");
1491439Seric 			}
1501439Seric 			else
1511439Seric 				syserr("Cannot write %s", InFileName);
1521439Seric 			freopen("/dev/null", "w", tf);
1531392Seric 		}
1541392Seric 	}
1551392Seric 	fclose(tf);
1562900Seric 
1572900Seric 	/*
1582900Seric 	**  Find out some information from the headers.
1593386Seric 	**	Examples are who is the from person & the date.
1602900Seric 	*/
1612900Seric 
1622900Seric 	/* from person */
1632900Seric 	xfrom = hvalue("sender");
1642900Seric 	if (xfrom == NULL)
1652900Seric 		xfrom = hvalue("from");
1662900Seric 
1673390Seric 	/* full name of from person */
1683390Seric 	p = hvalue("full-name");
1693390Seric 	if (p != NULL)
1703390Seric 		define('x', p);
1713390Seric 
1722900Seric 	/* date message originated */
1732900Seric 	p = hvalue("date");
1742900Seric 	if (p != NULL)
1752900Seric 	{
1763386Seric 		define('a', p);
1773386Seric 		/* we don't have a good way to do canonical conversion ....
1783386Seric 		define('d', newstr(arpatounix(p)));
1793386Seric 		.... so we will ignore the problem for the time being */
1802900Seric 	}
1812900Seric 
1821392Seric 	if (freopen(InFileName, "r", stdin) == NULL)
1831392Seric 		syserr("Cannot reopen %s", InFileName);
1842900Seric 
1852900Seric # ifdef DEBUG
1862900Seric 	if (Debug)
1872900Seric 	{
1882900Seric 		printf("----- collected header -----\n");
1892900Seric 		for (h = Header; h != NULL; h = h->h_link)
1902900Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
1912900Seric 		printf("----------------------------\n");
1922900Seric 	}
1932900Seric # endif DEBUG
1942900Seric 	return (ArpaFmt ? xfrom : NULL);
1951392Seric }
1961392Seric /*
1972900Seric **  EATFROM -- chew up a UNIX style from line and process
1982900Seric **
1992900Seric **	This does indeed make some assumptions about the format
2002900Seric **	of UNIX messages.
2012900Seric **
2022900Seric **	Parameters:
2032900Seric **		fm -- the from line.
2042900Seric **
2052900Seric **	Returns:
2062900Seric **		none.
2072900Seric **
2082900Seric **	Side Effects:
2092900Seric **		extracts what information it can from the header,
2103386Seric **		such as the date.
2112900Seric */
2122900Seric 
2132900Seric char	*MonthList[] =
2142900Seric {
2152900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2162900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2172900Seric 	NULL
2182900Seric };
2192900Seric 
2202900Seric eatfrom(fm)
2212900Seric 	char *fm;
2222900Seric {
2232900Seric 	register char *p;
2242900Seric 	register char **dt;
2252900Seric 
2262900Seric 	/* find the date part */
2272900Seric 	p = fm;
2282900Seric 	while (*p != '\0')
2292900Seric 	{
2302900Seric 		/* skip a word */
2312900Seric 		while (*p != '\0' && *p != ' ')
2322900Seric 			*p++;
2332900Seric 		while (*p == ' ')
2342900Seric 			*p++;
2352900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
2362900Seric 			continue;
2372900Seric 
2382900Seric 		/* we have a possible date */
2392900Seric 		for (dt = MonthList; *dt != NULL; dt++)
2402900Seric 			if (strncmp(*dt, p, 3) == 0)
2412900Seric 				break;
2422900Seric 
2432900Seric 		if (*dt != NULL)
2442900Seric 			break;
2452900Seric 	}
2462900Seric 
2472900Seric 	if (*p != NULL)
2482900Seric 	{
2493386Seric 		char *q;
2503386Seric 
2512900Seric 		/* we have found a date */
2523386Seric 		q = xalloc(25);
2533386Seric 		strncpy(q, p, 25);
2543386Seric 		q[24] = '\0';
2553386Seric 		define('d', q);
2562900Seric 	}
2572900Seric }
2582900Seric /*
2592900Seric **  HVALUE -- return value of a header.
2602900Seric **
2613386Seric **	Only "real" fields (i.e., ones that have not been supplied
2623386Seric **	as a default) are used.
2633386Seric **
2642900Seric **	Parameters:
2652900Seric **		field -- the field name.
2662900Seric **
2672900Seric **	Returns:
2682900Seric **		pointer to the value part.
2692900Seric **		NULL if not found.
2702900Seric **
2712900Seric **	Side Effects:
2722900Seric **		sets the H_USED bit in the header if found.
2732900Seric */
2742900Seric 
2752900Seric char *
2762900Seric hvalue(field)
2772900Seric 	char *field;
2782900Seric {
2792900Seric 	register HDR *h;
2802900Seric 
2812900Seric 	for (h = Header; h != NULL; h = h->h_link)
2822900Seric 	{
2833386Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
2842900Seric 		{
2852900Seric 			h->h_flags |= H_USED;
2862900Seric 			return (h->h_value);
2872900Seric 		}
2882900Seric 	}
2892900Seric 	return (NULL);
2902900Seric }
2912900Seric /*
2922900Seric **  ISHEADER -- predicate telling if argument is a header.
2932900Seric **
2942900Seric **	Parameters:
2952900Seric **		s -- string to check for possible headerness.
2962900Seric **
2972900Seric **	Returns:
2982900Seric **		TRUE if s is a header.
2992900Seric **		FALSE otherwise.
3002900Seric **
3012900Seric **	Side Effects:
3022900Seric **		none.
3032900Seric */
3042900Seric 
3052900Seric bool
3062900Seric isheader(s)
3072900Seric 	register char *s;
3082900Seric {
3092900Seric 	if (!isalnum(*s))
3102900Seric 		return (FALSE);
3112900Seric 	while (!isspace(*s) && *s != ':')
3122900Seric 		s++;
3132900Seric 	while (isspace(*s))
3142900Seric 		s++;
3152900Seric 	return (*s == ':');
3162900Seric }
3173386Seric /*
3183386Seric **  CHOMPHEADER -- process and save a header line.
3193386Seric **
3203386Seric **	Called by collect and by readcf to deal with header lines.
3213386Seric **
3223386Seric **	Parameters:
3233386Seric **		line -- header as a text line.
324*3391Seric **		def -- if set, this is a default value.
3253386Seric **
3263386Seric **	Returns:
3273386Seric **		flags for this header.
3283386Seric **
3293386Seric **	Side Effects:
3303386Seric **		The header is saved on the header list.
3313386Seric */
3323386Seric 
333*3391Seric chompheader(line, def)
3343386Seric 	char *line;
335*3391Seric 	bool def;
3363386Seric {
3373386Seric 	register char *p;
3383386Seric 	extern int errno;
3393386Seric 	register HDR *h;
3403386Seric 	HDR **hp;
3413386Seric 	extern bool isheader();
3423386Seric 	extern char *newstr();
3433386Seric 	extern char *xalloc();
3443386Seric 	char *fname;
3453386Seric 	char *fvalue;
3463386Seric 	extern char *index(), *rindex();
3473386Seric 	struct hdrinfo *hi;
3483386Seric 	extern char *strcpy(), *strcat(), *mktemp();
3493386Seric 
3503386Seric 	/* strip off trailing newline */
3513386Seric 	p = rindex(line, '\n');
3523386Seric 	if (p != NULL)
3533386Seric 		*p = '\0';
3543386Seric 
3553386Seric 	/* find canonical name */
3563386Seric 	fname = line;
3573386Seric 	p = index(line, ':');
3583386Seric 	fvalue = &p[1];
3593386Seric 	while (isspace(*--p))
3603386Seric 		continue;
3613386Seric 	*++p = '\0';
3623386Seric 	makelower(fname);
3633386Seric 
3643386Seric 	/* strip field value on front */
3653386Seric 	if (*fvalue == ' ')
3663386Seric 		fvalue++;
3673386Seric 
3683386Seric 	/* search header list for this header */
3693386Seric 	for (hp = &Header, h = Header; h != NULL; hp = &h->h_link, h = h->h_link)
3703386Seric 	{
3713386Seric 		if (strcmp(fname, h->h_field) == 0 && bitset(H_DEFAULT, h->h_flags))
3723386Seric 			break;
3733386Seric 	}
3743386Seric 
3753386Seric 	/* see if it is a known type */
3763386Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
3773386Seric 	{
3783386Seric 		if (strcmp(hi->hi_field, fname) == 0)
3793386Seric 			break;
3803386Seric 	}
3813386Seric 
3823386Seric 	/* if this means "end of header" quit now */
3833386Seric 	if (bitset(H_EOH, hi->hi_flags))
3843386Seric 		return (hi->hi_flags);
3853386Seric 
3863386Seric 	/* create/fill in a new node */
3873386Seric 	if (h == NULL)
3883386Seric 	{
3893386Seric 		/* create a new node */
3903386Seric 		*hp = h = (HDR *) xalloc(sizeof *h);
3913386Seric 		h->h_field = newstr(fname);
3923386Seric 		h->h_value = NULL;
3933386Seric 		h->h_link = NULL;
394*3391Seric 		h->h_flags = hi->hi_flags;
3953386Seric 		h->h_mflags = hi->hi_mflags;
3963386Seric 	}
397*3391Seric 	if (def)
398*3391Seric 		h->h_flags |= H_DEFAULT;
3993386Seric 	else
400*3391Seric 		h->h_flags &= ~H_CHECK;
4013386Seric 	if (h->h_value != NULL)
4023386Seric 		free(h->h_value);
4033386Seric 	h->h_value = newstr(fvalue);
4043386Seric 
4053386Seric 	return (h->h_flags);
4063386Seric }
407