122704Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333729Sbostic  * Copyright (c) 1988 Regents of the University of California.
433729Sbostic  * All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822704Sdist 
922704Sdist #ifndef lint
10*58332Seric static char sccsid[] = "@(#)envelope.c	6.17 (Berkeley) 03/01/93";
1133729Sbostic #endif /* not lint */
1222704Sdist 
13*58332Seric #include "sendmail.h"
1436928Sbostic #include <sys/time.h>
1536928Sbostic #include <sys/stat.h>
169536Seric #include <pwd.h>
1757737Seric #include <fcntl.h>
189536Seric 
199536Seric /*
209536Seric **  NEWENVELOPE -- allocate a new envelope
219536Seric **
229536Seric **	Supports inheritance.
239536Seric **
249536Seric **	Parameters:
259536Seric **		e -- the new envelope to fill in.
2658179Seric **		parent -- the envelope to be the parent of e.
279536Seric **
289536Seric **	Returns:
299536Seric **		e.
309536Seric **
319536Seric **	Side Effects:
329536Seric **		none.
339536Seric */
349536Seric 
359536Seric ENVELOPE *
3658179Seric newenvelope(e, parent)
379536Seric 	register ENVELOPE *e;
3858179Seric 	register ENVELOPE *parent;
399536Seric {
409536Seric 	extern putheader(), putbody();
4125611Seric 	extern ENVELOPE BlankEnvelope;
429536Seric 
4358179Seric 	if (e == parent && e->e_parent != NULL)
449536Seric 		parent = e->e_parent;
4525611Seric 	clearenvelope(e, TRUE);
4624944Seric 	if (e == CurEnv)
4724944Seric 		bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from);
4824944Seric 	else
4924944Seric 		bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
509536Seric 	e->e_parent = parent;
519536Seric 	e->e_ctime = curtime();
5256215Seric 	if (parent != NULL)
5356215Seric 		e->e_msgpriority = parent->e_msgsize;
549536Seric 	e->e_puthdr = putheader;
559536Seric 	e->e_putbody = putbody;
569536Seric 	if (CurEnv->e_xfp != NULL)
579536Seric 		(void) fflush(CurEnv->e_xfp);
589536Seric 
599536Seric 	return (e);
609536Seric }
619536Seric /*
629536Seric **  DROPENVELOPE -- deallocate an envelope.
639536Seric **
649536Seric **	Parameters:
659536Seric **		e -- the envelope to deallocate.
669536Seric **
679536Seric **	Returns:
689536Seric **		none.
699536Seric **
709536Seric **	Side Effects:
719536Seric **		housekeeping necessary to dispose of an envelope.
729536Seric **		Unlocks this queue file.
739536Seric */
749536Seric 
759536Seric dropenvelope(e)
769536Seric 	register ENVELOPE *e;
779536Seric {
789536Seric 	bool queueit = FALSE;
799536Seric 	register ADDRESS *q;
8057943Seric 	char *id = e->e_id;
819536Seric 
829536Seric 	if (tTd(50, 1))
839536Seric 	{
849536Seric 		printf("dropenvelope %x id=", e);
859536Seric 		xputs(e->e_id);
869536Seric 		printf(" flags=%o\n", e->e_flags);
879536Seric 	}
8857943Seric 
8957943Seric 	if (id == NULL)
9057943Seric 		id = "(none)";
9157943Seric 
929536Seric #ifdef LOG
9358020Seric 	if (LogLevel > 84)
949536Seric 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d",
9557943Seric 				  id, e->e_flags, getpid());
9656795Seric #endif /* LOG */
979536Seric 
989536Seric 	/* we must have an id to remove disk files */
999536Seric 	if (e->e_id == NULL)
1009536Seric 		return;
1019536Seric 
1029536Seric 	/*
1039536Seric 	**  Extract state information from dregs of send list.
1049536Seric 	*/
1059536Seric 
1069536Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1079536Seric 	{
1089536Seric 		if (bitset(QQUEUEUP, q->q_flags))
1099536Seric 			queueit = TRUE;
1109536Seric 	}
1119536Seric 
1129536Seric 	/*
1139536Seric 	**  Send back return receipts as requested.
1149536Seric 	*/
1159536Seric 
1169536Seric 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags))
1179536Seric 	{
11810844Seric 		auto ADDRESS *rlist = NULL;
1199536Seric 
12058082Seric 		(void) sendtolist(e->e_receiptto, (ADDRESS *) NULL, &rlist, e);
12155012Seric 		(void) returntosender("Return receipt", rlist, FALSE, e);
1229536Seric 	}
1239536Seric 
1249536Seric 	/*
1259536Seric 	**  Arrange to send error messages if there are fatal errors.
1269536Seric 	*/
1279536Seric 
12810754Seric 	if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET)
1299536Seric 		savemail(e);
1309536Seric 
1319536Seric 	/*
1329536Seric 	**  Instantiate or deinstantiate the queue.
1339536Seric 	*/
1349536Seric 
1359536Seric 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
1369536Seric 	    bitset(EF_CLRQUEUE, e->e_flags))
1379536Seric 	{
13823497Seric 		if (e->e_df != NULL)
13923497Seric 			xunlink(e->e_df);
1409536Seric 		xunlink(queuename(e, 'q'));
1419536Seric 	}
1429536Seric 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
14310754Seric 	{
14410754Seric #ifdef QUEUE
14551920Seric 		queueup(e, FALSE, FALSE);
14656795Seric #else /* QUEUE */
14758151Seric 		syserr("554 dropenvelope: queueup");
14856795Seric #endif /* QUEUE */
14910754Seric 	}
1509536Seric 
1519536Seric 	/* now unlock the job */
15210196Seric 	closexscript(e);
1539536Seric 	unlockqueue(e);
1549536Seric 
1559536Seric 	/* make sure that this envelope is marked unused */
1569536Seric 	e->e_id = e->e_df = NULL;
15724944Seric 	if (e->e_dfp != NULL)
15824944Seric 		(void) fclose(e->e_dfp);
15910196Seric 	e->e_dfp = NULL;
16057589Seric 
16157589Seric #ifdef LOG
16258020Seric 	if (LogLevel > 74)
16357943Seric 		syslog(LOG_INFO, "%s: done", id);
16457589Seric #endif /* LOG */
1659536Seric }
1669536Seric /*
1679536Seric **  CLEARENVELOPE -- clear an envelope without unlocking
1689536Seric **
1699536Seric **	This is normally used by a child process to get a clean
1709536Seric **	envelope without disturbing the parent.
1719536Seric **
1729536Seric **	Parameters:
1739536Seric **		e -- the envelope to clear.
17425611Seric **		fullclear - if set, the current envelope is total
17525611Seric **			garbage and should be ignored; otherwise,
17625611Seric **			release any resources it may indicate.
1779536Seric **
1789536Seric **	Returns:
1799536Seric **		none.
1809536Seric **
1819536Seric **	Side Effects:
1829536Seric **		Closes files associated with the envelope.
1839536Seric **		Marks the envelope as unallocated.
1849536Seric */
1859536Seric 
18625611Seric clearenvelope(e, fullclear)
1879536Seric 	register ENVELOPE *e;
18825611Seric 	bool fullclear;
1899536Seric {
19025514Seric 	register HDR *bh;
19125514Seric 	register HDR **nhp;
19225514Seric 	extern ENVELOPE BlankEnvelope;
19325514Seric 
19425611Seric 	if (!fullclear)
19525611Seric 	{
19625611Seric 		/* clear out any file information */
19725611Seric 		if (e->e_xfp != NULL)
19825611Seric 			(void) fclose(e->e_xfp);
19925611Seric 		if (e->e_dfp != NULL)
20025611Seric 			(void) fclose(e->e_dfp);
20125611Seric 	}
2029536Seric 
20324961Seric 	/* now clear out the data */
20424965Seric 	STRUCTCOPY(BlankEnvelope, *e);
20525514Seric 	bh = BlankEnvelope.e_header;
20625514Seric 	nhp = &e->e_header;
20725514Seric 	while (bh != NULL)
20825514Seric 	{
20925514Seric 		*nhp = (HDR *) xalloc(sizeof *bh);
21025514Seric 		bcopy((char *) bh, (char *) *nhp, sizeof *bh);
21125514Seric 		bh = bh->h_link;
21225514Seric 		nhp = &(*nhp)->h_link;
21325514Seric 	}
2149536Seric }
2159536Seric /*
2169536Seric **  INITSYS -- initialize instantiation of system
2179536Seric **
2189536Seric **	In Daemon mode, this is done in the child.
2199536Seric **
2209536Seric **	Parameters:
2219536Seric **		none.
2229536Seric **
2239536Seric **	Returns:
2249536Seric **		none.
2259536Seric **
2269536Seric **	Side Effects:
2279536Seric **		Initializes the system macros, some global variables,
2289536Seric **		etc.  In particular, the current time in various
2299536Seric **		forms is set.
2309536Seric */
2319536Seric 
23255012Seric initsys(e)
23355012Seric 	register ENVELOPE *e;
2349536Seric {
2359536Seric 	static char cbuf[5];			/* holds hop count */
2369536Seric 	static char pbuf[10];			/* holds pid */
23722963Smiriam #ifdef TTYNAME
2389536Seric 	static char ybuf[10];			/* holds tty id */
2399536Seric 	register char *p;
24056795Seric #endif /* TTYNAME */
2419536Seric 	extern char *ttyname();
2429536Seric 	extern char *macvalue();
2439536Seric 	extern char Version[];
2449536Seric 
2459536Seric 	/*
2469536Seric 	**  Give this envelope a reality.
2479536Seric 	**	I.e., an id, a transcript, and a creation time.
2489536Seric 	*/
2499536Seric 
25055012Seric 	openxscript(e);
25155012Seric 	e->e_ctime = curtime();
2529536Seric 
2539536Seric 	/*
2549536Seric 	**  Set OutChannel to something useful if stdout isn't it.
2559536Seric 	**	This arranges that any extra stuff the mailer produces
2569536Seric 	**	gets sent back to the user on error (because it is
2579536Seric 	**	tucked away in the transcript).
2589536Seric 	*/
2599536Seric 
26058069Seric 	if (OpMode == MD_DAEMON && QueueRun && e->e_xfp != NULL)
26155012Seric 		OutChannel = e->e_xfp;
2629536Seric 
2639536Seric 	/*
2649536Seric 	**  Set up some basic system macros.
2659536Seric 	*/
2669536Seric 
2679536Seric 	/* process id */
2689536Seric 	(void) sprintf(pbuf, "%d", getpid());
26955012Seric 	define('p', pbuf, e);
2709536Seric 
2719536Seric 	/* hop count */
27255012Seric 	(void) sprintf(cbuf, "%d", e->e_hopcount);
27355012Seric 	define('c', cbuf, e);
2749536Seric 
2759536Seric 	/* time as integer, unix time, arpa time */
27655012Seric 	settime(e);
2779536Seric 
27817472Seric #ifdef TTYNAME
2799536Seric 	/* tty name */
28055012Seric 	if (macvalue('y', e) == NULL)
2819536Seric 	{
2829536Seric 		p = ttyname(2);
2839536Seric 		if (p != NULL)
2849536Seric 		{
28556795Seric 			if (strrchr(p, '/') != NULL)
28656795Seric 				p = strrchr(p, '/') + 1;
2879536Seric 			(void) strcpy(ybuf, p);
28855012Seric 			define('y', ybuf, e);
2899536Seric 		}
2909536Seric 	}
29156795Seric #endif /* TTYNAME */
2929536Seric }
2939536Seric /*
29411932Seric **  SETTIME -- set the current time.
29511932Seric **
29611932Seric **	Parameters:
29711932Seric **		none.
29811932Seric **
29911932Seric **	Returns:
30011932Seric **		none.
30111932Seric **
30211932Seric **	Side Effects:
30311932Seric **		Sets the various time macros -- $a, $b, $d, $t.
30411932Seric */
30511932Seric 
30655012Seric settime(e)
30755012Seric 	register ENVELOPE *e;
30811932Seric {
30911932Seric 	register char *p;
31011932Seric 	auto time_t now;
31111932Seric 	static char tbuf[20];			/* holds "current" time */
31211932Seric 	static char dbuf[30];			/* holds ctime(tbuf) */
31311932Seric 	register struct tm *tm;
31411932Seric 	extern char *arpadate();
31511932Seric 	extern struct tm *gmtime();
31611932Seric 	extern char *macvalue();
31711932Seric 
31811932Seric 	now = curtime();
31911932Seric 	tm = gmtime(&now);
32057014Seric 	(void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
32157014Seric 			tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
32255012Seric 	define('t', tbuf, e);
32311932Seric 	(void) strcpy(dbuf, ctime(&now));
32458131Seric 	p = strchr(dbuf, '\n');
32558131Seric 	if (p != NULL)
32658131Seric 		*p = '\0';
32758131Seric 	define('d', dbuf, e);
32811932Seric 	p = newstr(arpadate(dbuf));
32955012Seric 	if (macvalue('a', e) == NULL)
33055012Seric 		define('a', p, e);
33155012Seric 	define('b', p, e);
33211932Seric }
33311932Seric /*
3349536Seric **  OPENXSCRIPT -- Open transcript file
3359536Seric **
3369536Seric **	Creates a transcript file for possible eventual mailing or
3379536Seric **	sending back.
3389536Seric **
3399536Seric **	Parameters:
3409536Seric **		e -- the envelope to create the transcript in/for.
3419536Seric **
3429536Seric **	Returns:
3439536Seric **		none
3449536Seric **
3459536Seric **	Side Effects:
3469536Seric **		Creates the transcript file.
3479536Seric */
3489536Seric 
3499536Seric openxscript(e)
3509536Seric 	register ENVELOPE *e;
3519536Seric {
3529536Seric 	register char *p;
35340933Srick 	int fd;
3549536Seric 
3559536Seric 	if (e->e_xfp != NULL)
3569536Seric 		return;
3579536Seric 	p = queuename(e, 'x');
35840933Srick 	fd = open(p, O_WRONLY|O_CREAT, 0644);
35940933Srick 	if (fd < 0)
3609536Seric 		syserr("Can't create %s", p);
3619536Seric 	else
36240933Srick 		e->e_xfp = fdopen(fd, "w");
3639536Seric }
3649536Seric /*
36510196Seric **  CLOSEXSCRIPT -- close the transcript file.
36610196Seric **
36710196Seric **	Parameters:
36810196Seric **		e -- the envelope containing the transcript to close.
36910196Seric **
37010196Seric **	Returns:
37110196Seric **		none.
37210196Seric **
37310196Seric **	Side Effects:
37410196Seric **		none.
37510196Seric */
37610196Seric 
37710196Seric closexscript(e)
37810196Seric 	register ENVELOPE *e;
37910196Seric {
38010196Seric 	if (e->e_xfp == NULL)
38110196Seric 		return;
38210196Seric 	(void) fclose(e->e_xfp);
38310196Seric 	e->e_xfp = NULL;
38410196Seric }
38510196Seric /*
3869536Seric **  SETSENDER -- set the person who this message is from
3879536Seric **
3889536Seric **	Under certain circumstances allow the user to say who
3899536Seric **	s/he is (using -f or -r).  These are:
3909536Seric **	1.  The user's uid is zero (root).
3919536Seric **	2.  The user's login name is in an approved list (typically
3929536Seric **	    from a network server).
3939536Seric **	3.  The address the user is trying to claim has a
3949536Seric **	    "!" character in it (since #2 doesn't do it for
3959536Seric **	    us if we are dialing out for UUCP).
3969536Seric **	A better check to replace #3 would be if the
3979536Seric **	effective uid is "UUCP" -- this would require me
3989536Seric **	to rewrite getpwent to "grab" uucp as it went by,
3999536Seric **	make getname more nasty, do another passwd file
4009536Seric **	scan, or compile the UID of "UUCP" into the code,
4019536Seric **	all of which are reprehensible.
4029536Seric **
4039536Seric **	Assuming all of these fail, we figure out something
4049536Seric **	ourselves.
4059536Seric **
4069536Seric **	Parameters:
4079536Seric **		from -- the person we would like to believe this message
4089536Seric **			is from, as specified on the command line.
40953182Seric **		e -- the envelope in which we would like the sender set.
4109536Seric **
4119536Seric **	Returns:
4129536Seric **		none.
4139536Seric **
4149536Seric **	Side Effects:
4159536Seric **		sets sendmail's notion of who the from person is.
4169536Seric */
4179536Seric 
41853182Seric setsender(from, e)
4199536Seric 	char *from;
42053182Seric 	register ENVELOPE *e;
4219536Seric {
4229536Seric 	register char **pvp;
4239536Seric 	char *realname = NULL;
42418665Seric 	register struct passwd *pw;
4259536Seric 	char buf[MAXNAME];
42616913Seric 	char pvpbuf[PSBUFSIZE];
42718665Seric 	extern struct passwd *getpwnam();
4289536Seric 	extern char *macvalue();
4299536Seric 	extern char **prescan();
4309536Seric 	extern char *FullName;
4319536Seric 
4329536Seric 	if (tTd(45, 1))
43314786Seric 		printf("setsender(%s)\n", from == NULL ? "" : from);
4349536Seric 
4359536Seric 	/*
4369536Seric 	**  Figure out the real user executing us.
4379536Seric 	**	Username can return errno != 0 on non-errors.
4389536Seric 	*/
4399536Seric 
44052220Seric 	if (QueueRun || OpMode == MD_SMTP)
4419536Seric 		realname = from;
4429536Seric 	if (realname == NULL || realname[0] == '\0')
4439536Seric 	{
4449536Seric 		extern char *username();
4459536Seric 
4469536Seric 		realname = username();
4479536Seric 	}
4489536Seric 
44957589Seric /*
4509536Seric 	SuprErrs = TRUE;
45157589Seric */
45258323Seric 	if (from == NULL || parseaddr(from, &e->e_from, 1, ' ', e) == NULL)
4539536Seric 	{
45421750Seric 		/* log garbage addresses for traceback */
45555173Seric # ifdef LOG
45658020Seric 		if (from != NULL && LogLevel > 2)
45721750Seric 		{
45855173Seric 			char *host = RealHostName;
45955173Seric 
46055173Seric 			if (host == NULL)
46155173Seric 				host = MyHostName;
46255173Seric 			syslog(LOG_NOTICE,
46355173Seric 				"from=%s unparseable, received from %s@%s",
46455173Seric 				from, realname, host);
46555173Seric 		}
46656795Seric # endif /* LOG */
46757589Seric 		if (from != NULL)
46857589Seric 			SuprErrs = TRUE;
46957589Seric 		if (from == realname ||
47058323Seric 		    parseaddr(from = newstr(realname), &e->e_from, 1, ' ', e) == NULL)
47124944Seric 		{
47257589Seric 			SuprErrs = TRUE;
47358323Seric 			if (parseaddr("postmaster", &e->e_from, 1, ' ', e) == NULL)
47458151Seric 				syserr("553 setsender: can't even parse postmaster!");
47524944Seric 		}
4769536Seric 	}
4779536Seric 	else
4789536Seric 		FromFlag = TRUE;
47953182Seric 	e->e_from.q_flags |= QDONTSEND;
48057731Seric 	if (tTd(45, 5))
48157731Seric 	{
48257731Seric 		printf("setsender: QDONTSEND ");
48357731Seric 		printaddr(&e->e_from, FALSE);
48457731Seric 	}
48553182Seric 	loweraddr(&e->e_from);
4869536Seric 	SuprErrs = FALSE;
4879536Seric 
48853736Seric 	pvp = NULL;
48953736Seric 	if (e->e_from.q_mailer == LocalMailer)
4909536Seric 	{
49153736Seric # ifdef USERDB
49253736Seric 		register char *p;
49353736Seric 		extern char *udbsender();
49453736Seric # endif
49517472Seric 
4969536Seric 		/* if the user has given fullname already, don't redefine */
4979536Seric 		if (FullName == NULL)
49853182Seric 			FullName = macvalue('x', e);
49911932Seric 		if (FullName != NULL && FullName[0] == '\0')
5009536Seric 			FullName = NULL;
5019536Seric 
50253736Seric # ifdef USERDB
50353736Seric 		p = udbsender(from);
50453736Seric 
50553736Seric 		if (p != NULL)
5069536Seric 		{
50753736Seric 			/*
50853736Seric 			**  We have an alternate address for the sender
50953736Seric 			*/
51053736Seric 
51153736Seric 			pvp = prescan(p, '\0', pvpbuf);
5129536Seric 		}
51353736Seric # endif /* USERDB */
51453736Seric 
51553736Seric 		if ((pw = getpwnam(e->e_from.q_user)) != NULL)
51653736Seric 		{
51753736Seric 			/*
51853736Seric 			**  Process passwd file entry.
51953736Seric 			*/
52053736Seric 
52153736Seric 
52253736Seric 			/* extract home directory */
52353736Seric 			e->e_from.q_home = newstr(pw->pw_dir);
52453736Seric 			define('z', e->e_from.q_home, e);
52553736Seric 
52653736Seric 			/* extract user and group id */
52753736Seric 			e->e_from.q_uid = pw->pw_uid;
52853736Seric 			e->e_from.q_gid = pw->pw_gid;
52953736Seric 
53053736Seric 			/* extract full name from passwd file */
53153736Seric 			if (FullName == NULL && pw->pw_gecos != NULL &&
53253736Seric 			    strcmp(pw->pw_name, e->e_from.q_user) == 0)
53353736Seric 			{
53453736Seric 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
53553736Seric 				if (buf[0] != '\0')
53653736Seric 					FullName = newstr(buf);
53753736Seric 			}
53853736Seric 		}
5399536Seric 		if (FullName != NULL)
54053182Seric 			define('x', FullName, e);
5419536Seric 	}
54211625Seric 	else
54311625Seric 	{
54453182Seric 		if (e->e_from.q_home == NULL)
54553182Seric 			e->e_from.q_home = getenv("HOME");
54653182Seric 		e->e_from.q_uid = getuid();
54753182Seric 		e->e_from.q_gid = getgid();
54811625Seric 	}
54911625Seric 
5509536Seric 	/*
5519536Seric 	**  Rewrite the from person to dispose of possible implicit
5529536Seric 	**	links in the net.
5539536Seric 	*/
5549536Seric 
5559536Seric 	if (pvp == NULL)
55653736Seric 		pvp = prescan(from, '\0', pvpbuf);
55753736Seric 	if (pvp == NULL)
5589536Seric 	{
55936233Skarels # ifdef LOG
56058020Seric 		if (LogLevel > 2)
56136233Skarels 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
56236233Skarels # endif
56358151Seric 		usrerr("553 cannot prescan from (%s)", from);
5649536Seric 		finis();
5659536Seric 	}
5669536Seric 	rewrite(pvp, 3);
5679536Seric 	rewrite(pvp, 1);
56825032Seric 	rewrite(pvp, 4);
56958082Seric 	cataddr(pvp, buf, sizeof buf, '\0');
57053182Seric 	e->e_sender = e->e_returnpath = newstr(buf);
5719536Seric 
57253182Seric 	define('f', e->e_sender, e);
57351951Seric 
5749536Seric 	/* save the domain spec if this mailer wants it */
57553182Seric 	if (e->e_from.q_mailer != NULL &&
57653182Seric 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
5779536Seric 	{
5789536Seric 		extern char **copyplist();
5799536Seric 
5809536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
5819536Seric 			pvp++;
5829536Seric 		if (*pvp != NULL)
58353182Seric 			e->e_fromdomain = copyplist(pvp, TRUE);
5849536Seric 	}
5859536Seric }
586