122704Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
362525Sbostic  * Copyright (c) 1988, 1993
462525Sbostic  *	The Regents of the University of California.  All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822704Sdist 
922704Sdist #ifndef lint
10*67691Seric static char sccsid[] = "@(#)envelope.c	8.39 (Berkeley) 08/16/94";
1133729Sbostic #endif /* not lint */
1222704Sdist 
1358332Seric #include "sendmail.h"
149536Seric #include <pwd.h>
159536Seric 
169536Seric /*
179536Seric **  NEWENVELOPE -- allocate a new envelope
189536Seric **
199536Seric **	Supports inheritance.
209536Seric **
219536Seric **	Parameters:
229536Seric **		e -- the new envelope to fill in.
2358179Seric **		parent -- the envelope to be the parent of e.
249536Seric **
259536Seric **	Returns:
269536Seric **		e.
279536Seric **
289536Seric **	Side Effects:
299536Seric **		none.
309536Seric */
319536Seric 
329536Seric ENVELOPE *
3358179Seric newenvelope(e, parent)
349536Seric 	register ENVELOPE *e;
3558179Seric 	register ENVELOPE *parent;
369536Seric {
379536Seric 	extern putheader(), putbody();
3825611Seric 	extern ENVELOPE BlankEnvelope;
399536Seric 
4058179Seric 	if (e == parent && e->e_parent != NULL)
419536Seric 		parent = e->e_parent;
4225611Seric 	clearenvelope(e, TRUE);
4324944Seric 	if (e == CurEnv)
4424944Seric 		bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from);
4524944Seric 	else
4624944Seric 		bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
479536Seric 	e->e_parent = parent;
489536Seric 	e->e_ctime = curtime();
4956215Seric 	if (parent != NULL)
5056215Seric 		e->e_msgpriority = parent->e_msgsize;
519536Seric 	e->e_puthdr = putheader;
529536Seric 	e->e_putbody = putbody;
539536Seric 	if (CurEnv->e_xfp != NULL)
549536Seric 		(void) fflush(CurEnv->e_xfp);
559536Seric 
569536Seric 	return (e);
579536Seric }
589536Seric /*
599536Seric **  DROPENVELOPE -- deallocate an envelope.
609536Seric **
619536Seric **	Parameters:
629536Seric **		e -- the envelope to deallocate.
639536Seric **
649536Seric **	Returns:
659536Seric **		none.
669536Seric **
679536Seric **	Side Effects:
689536Seric **		housekeeping necessary to dispose of an envelope.
699536Seric **		Unlocks this queue file.
709536Seric */
719536Seric 
7260494Seric void
739536Seric dropenvelope(e)
749536Seric 	register ENVELOPE *e;
759536Seric {
769536Seric 	bool queueit = FALSE;
7763839Seric 	bool saveit = bitset(EF_FATALERRS, e->e_flags);
789536Seric 	register ADDRESS *q;
7957943Seric 	char *id = e->e_id;
8063753Seric 	char buf[MAXLINE];
819536Seric 
829536Seric 	if (tTd(50, 1))
839536Seric 	{
8458680Seric 		printf("dropenvelope %x: id=", e);
859536Seric 		xputs(e->e_id);
8665089Seric 		printf(", flags=0x%x\n", e->e_flags);
8763753Seric 		if (tTd(50, 10))
8863753Seric 		{
8963753Seric 			printf("sendq=");
9063753Seric 			printaddr(e->e_sendqueue, TRUE);
9163753Seric 		}
929536Seric 	}
9357943Seric 
9458680Seric 	/* we must have an id to remove disk files */
9557943Seric 	if (id == NULL)
9658680Seric 		return;
9757943Seric 
989536Seric #ifdef LOG
9965089Seric 	if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
10065089Seric 		logsender(e, NULL);
10158020Seric 	if (LogLevel > 84)
10265089Seric 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d",
10357943Seric 				  id, e->e_flags, getpid());
10456795Seric #endif /* LOG */
10565089Seric 	e->e_flags &= ~EF_LOGSENDER;
1069536Seric 
10763753Seric 	/* post statistics */
10863753Seric 	poststats(StatFile);
10963753Seric 
1109536Seric 	/*
1119536Seric 	**  Extract state information from dregs of send list.
1129536Seric 	*/
1139536Seric 
11464743Seric 	e->e_flags &= ~EF_QUEUERUN;
1159536Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1169536Seric 	{
1179536Seric 		if (bitset(QQUEUEUP, q->q_flags))
1189536Seric 			queueit = TRUE;
11963839Seric 		if (!bitset(QDONTSEND, q->q_flags) &&
12063839Seric 		    bitset(QBADADDR, q->q_flags))
12163839Seric 		{
12263839Seric 			if (q->q_owner == NULL &&
12363839Seric 			    strcmp(e->e_from.q_paddr, "<>") != 0)
12463839Seric 				(void) sendtolist(e->e_from.q_paddr, NULL,
12563839Seric 						  &e->e_errorqueue, e);
12663839Seric 		}
1279536Seric 	}
1289536Seric 
1299536Seric 	/*
13063753Seric 	**  See if the message timed out.
13163753Seric 	*/
13263753Seric 
13363753Seric 	if (!queueit)
13463753Seric 		/* nothing to do */ ;
13563753Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
13663753Seric 	{
13763839Seric 		(void) sprintf(buf, "Cannot send message for %s",
13863839Seric 			pintvl(TimeOuts.to_q_return, FALSE));
13963839Seric 		if (e->e_message != NULL)
14063839Seric 			free(e->e_message);
14163839Seric 		e->e_message = newstr(buf);
14263839Seric 		message(buf);
14363839Seric 		e->e_flags |= EF_CLRQUEUE;
14463839Seric 		saveit = TRUE;
14563787Seric 		fprintf(e->e_xfp, "Message could not be delivered for %s\n",
14663787Seric 			pintvl(TimeOuts.to_q_return, FALSE));
14763787Seric 		fprintf(e->e_xfp, "Message will be deleted from queue\n");
14863787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14963787Seric 		{
15063787Seric 			if (bitset(QQUEUEUP, q->q_flags))
15163787Seric 				q->q_flags |= QBADADDR;
15263787Seric 		}
15363753Seric 	}
15463753Seric 	else if (TimeOuts.to_q_warning > 0 &&
15563753Seric 	    curtime() > e->e_ctime + TimeOuts.to_q_warning)
15663753Seric 	{
15763753Seric 		if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
15863753Seric 		    e->e_class >= 0 &&
159*67691Seric 		    strcmp(e->e_from.q_paddr, "<>") != 0 &&
160*67691Seric 		    strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
161*67691Seric 		    (strlen(e->e_from.q_paddr) <= 8 ||
162*67691Seric 		     strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8], "-request") != 0))
16363753Seric 		{
16463753Seric 			(void) sprintf(buf,
16567261Seric 				"Warning: cannot send message for %s",
16663753Seric 				pintvl(TimeOuts.to_q_warning, FALSE));
16763753Seric 			if (e->e_message != NULL)
16863753Seric 				free(e->e_message);
16963753Seric 			e->e_message = newstr(buf);
17063753Seric 			message(buf);
17163839Seric 			e->e_flags |= EF_WARNING;
17263839Seric 			saveit = TRUE;
17363753Seric 		}
17463753Seric 		fprintf(e->e_xfp,
17563753Seric 			"Warning: message still undelivered after %s\n",
17663753Seric 			pintvl(TimeOuts.to_q_warning, FALSE));
17763753Seric 		fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
17863753Seric 			pintvl(TimeOuts.to_q_return, FALSE));
17963787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
18063787Seric 		{
18163787Seric 			if (bitset(QQUEUEUP, q->q_flags))
18263787Seric 				q->q_flags |= QREPORT;
18363787Seric 		}
18463753Seric 	}
18563753Seric 
18663753Seric 	/*
1879536Seric 	**  Send back return receipts as requested.
1889536Seric 	*/
1899536Seric 
19066783Seric 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)
19166783Seric 	    && !bitset(PRIV_NORECEIPTS, PrivacyFlags))
1929536Seric 	{
19310844Seric 		auto ADDRESS *rlist = NULL;
1949536Seric 
19564284Seric 		(void) sendtolist(e->e_receiptto, NULLADDR, &rlist, e);
19655012Seric 		(void) returntosender("Return receipt", rlist, FALSE, e);
1979536Seric 	}
19867682Seric 	e->e_flags &= ~EF_SENDRECEIPT;
1999536Seric 
2009536Seric 	/*
2019536Seric 	**  Arrange to send error messages if there are fatal errors.
2029536Seric 	*/
2039536Seric 
20463839Seric 	if (saveit && e->e_errormode != EM_QUIET)
2059536Seric 		savemail(e);
2069536Seric 
2079536Seric 	/*
20863849Seric 	**  Arrange to send warning messages to postmaster as requested.
20963849Seric 	*/
21063849Seric 
21163849Seric 	if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL &&
21264363Seric 	    !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0)
21363849Seric 	{
21463849Seric 		auto ADDRESS *rlist = NULL;
21563849Seric 
21664284Seric 		(void) sendtolist(PostMasterCopy, NULLADDR, &rlist, e);
21763849Seric 		(void) returntosender(e->e_message, rlist, FALSE, e);
21863849Seric 	}
21963849Seric 
22063849Seric 	/*
2219536Seric 	**  Instantiate or deinstantiate the queue.
2229536Seric 	*/
2239536Seric 
2249536Seric 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
2259536Seric 	    bitset(EF_CLRQUEUE, e->e_flags))
2269536Seric 	{
22764307Seric 		if (tTd(50, 1))
22864307Seric 			printf("\n===== Dropping [dq]f%s =====\n\n", e->e_id);
22923497Seric 		if (e->e_df != NULL)
23023497Seric 			xunlink(e->e_df);
2319536Seric 		xunlink(queuename(e, 'q'));
23263839Seric 
23363839Seric #ifdef LOG
23463839Seric 		if (LogLevel > 10)
23563839Seric 			syslog(LOG_INFO, "%s: done", id);
23663839Seric #endif
2379536Seric 	}
2389536Seric 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
23910754Seric 	{
24010754Seric #ifdef QUEUE
24164307Seric 		queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE);
24256795Seric #else /* QUEUE */
24358151Seric 		syserr("554 dropenvelope: queueup");
24456795Seric #endif /* QUEUE */
24510754Seric 	}
2469536Seric 
2479536Seric 	/* now unlock the job */
24810196Seric 	closexscript(e);
2499536Seric 	unlockqueue(e);
2509536Seric 
2519536Seric 	/* make sure that this envelope is marked unused */
25224944Seric 	if (e->e_dfp != NULL)
25358680Seric 		(void) xfclose(e->e_dfp, "dropenvelope", e->e_df);
25410196Seric 	e->e_dfp = NULL;
25558680Seric 	e->e_id = e->e_df = NULL;
2569536Seric }
2579536Seric /*
2589536Seric **  CLEARENVELOPE -- clear an envelope without unlocking
2599536Seric **
2609536Seric **	This is normally used by a child process to get a clean
2619536Seric **	envelope without disturbing the parent.
2629536Seric **
2639536Seric **	Parameters:
2649536Seric **		e -- the envelope to clear.
26525611Seric **		fullclear - if set, the current envelope is total
26625611Seric **			garbage and should be ignored; otherwise,
26725611Seric **			release any resources it may indicate.
2689536Seric **
2699536Seric **	Returns:
2709536Seric **		none.
2719536Seric **
2729536Seric **	Side Effects:
2739536Seric **		Closes files associated with the envelope.
2749536Seric **		Marks the envelope as unallocated.
2759536Seric */
2769536Seric 
27760494Seric void
27825611Seric clearenvelope(e, fullclear)
2799536Seric 	register ENVELOPE *e;
28025611Seric 	bool fullclear;
2819536Seric {
28225514Seric 	register HDR *bh;
28325514Seric 	register HDR **nhp;
28425514Seric 	extern ENVELOPE BlankEnvelope;
28525514Seric 
28625611Seric 	if (!fullclear)
28725611Seric 	{
28825611Seric 		/* clear out any file information */
28925611Seric 		if (e->e_xfp != NULL)
29058680Seric 			(void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id);
29125611Seric 		if (e->e_dfp != NULL)
29258680Seric 			(void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df);
29358680Seric 		e->e_xfp = e->e_dfp = NULL;
29425611Seric 	}
2959536Seric 
29624961Seric 	/* now clear out the data */
29724965Seric 	STRUCTCOPY(BlankEnvelope, *e);
29859698Seric 	if (Verbose)
29959698Seric 		e->e_sendmode = SM_DELIVER;
30025514Seric 	bh = BlankEnvelope.e_header;
30125514Seric 	nhp = &e->e_header;
30225514Seric 	while (bh != NULL)
30325514Seric 	{
30425514Seric 		*nhp = (HDR *) xalloc(sizeof *bh);
30525514Seric 		bcopy((char *) bh, (char *) *nhp, sizeof *bh);
30625514Seric 		bh = bh->h_link;
30725514Seric 		nhp = &(*nhp)->h_link;
30825514Seric 	}
3099536Seric }
3109536Seric /*
3119536Seric **  INITSYS -- initialize instantiation of system
3129536Seric **
3139536Seric **	In Daemon mode, this is done in the child.
3149536Seric **
3159536Seric **	Parameters:
3169536Seric **		none.
3179536Seric **
3189536Seric **	Returns:
3199536Seric **		none.
3209536Seric **
3219536Seric **	Side Effects:
3229536Seric **		Initializes the system macros, some global variables,
3239536Seric **		etc.  In particular, the current time in various
3249536Seric **		forms is set.
3259536Seric */
3269536Seric 
32760494Seric void
32855012Seric initsys(e)
32955012Seric 	register ENVELOPE *e;
3309536Seric {
33164768Seric 	char cbuf[5];				/* holds hop count */
33264768Seric 	char pbuf[10];				/* holds pid */
33322963Smiriam #ifdef TTYNAME
33459304Seric 	static char ybuf[60];			/* holds tty id */
3359536Seric 	register char *p;
33656795Seric #endif /* TTYNAME */
3379536Seric 	extern char *ttyname();
33860494Seric 	extern void settime();
3399536Seric 	extern char Version[];
3409536Seric 
3419536Seric 	/*
3429536Seric 	**  Give this envelope a reality.
3439536Seric 	**	I.e., an id, a transcript, and a creation time.
3449536Seric 	*/
3459536Seric 
34655012Seric 	openxscript(e);
34755012Seric 	e->e_ctime = curtime();
3489536Seric 
3499536Seric 	/*
3509536Seric 	**  Set OutChannel to something useful if stdout isn't it.
3519536Seric 	**	This arranges that any extra stuff the mailer produces
3529536Seric 	**	gets sent back to the user on error (because it is
3539536Seric 	**	tucked away in the transcript).
3549536Seric 	*/
3559536Seric 
35664760Seric 	if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
35758737Seric 	    e->e_xfp != NULL)
35855012Seric 		OutChannel = e->e_xfp;
3599536Seric 
3609536Seric 	/*
3619536Seric 	**  Set up some basic system macros.
3629536Seric 	*/
3639536Seric 
3649536Seric 	/* process id */
3659536Seric 	(void) sprintf(pbuf, "%d", getpid());
36664768Seric 	define('p', newstr(pbuf), e);
3679536Seric 
3689536Seric 	/* hop count */
36955012Seric 	(void) sprintf(cbuf, "%d", e->e_hopcount);
37064768Seric 	define('c', newstr(cbuf), e);
3719536Seric 
3729536Seric 	/* time as integer, unix time, arpa time */
37355012Seric 	settime(e);
3749536Seric 
37517472Seric #ifdef TTYNAME
3769536Seric 	/* tty name */
37755012Seric 	if (macvalue('y', e) == NULL)
3789536Seric 	{
3799536Seric 		p = ttyname(2);
3809536Seric 		if (p != NULL)
3819536Seric 		{
38256795Seric 			if (strrchr(p, '/') != NULL)
38356795Seric 				p = strrchr(p, '/') + 1;
3849536Seric 			(void) strcpy(ybuf, p);
38555012Seric 			define('y', ybuf, e);
3869536Seric 		}
3879536Seric 	}
38856795Seric #endif /* TTYNAME */
3899536Seric }
3909536Seric /*
39111932Seric **  SETTIME -- set the current time.
39211932Seric **
39311932Seric **	Parameters:
39411932Seric **		none.
39511932Seric **
39611932Seric **	Returns:
39711932Seric **		none.
39811932Seric **
39911932Seric **	Side Effects:
40011932Seric **		Sets the various time macros -- $a, $b, $d, $t.
40111932Seric */
40211932Seric 
40360494Seric void
40455012Seric settime(e)
40555012Seric 	register ENVELOPE *e;
40611932Seric {
40711932Seric 	register char *p;
40811932Seric 	auto time_t now;
40964768Seric 	char tbuf[20];				/* holds "current" time */
41064768Seric 	char dbuf[30];				/* holds ctime(tbuf) */
41111932Seric 	register struct tm *tm;
41211932Seric 	extern char *arpadate();
41311932Seric 	extern struct tm *gmtime();
41411932Seric 
41511932Seric 	now = curtime();
41611932Seric 	tm = gmtime(&now);
41757014Seric 	(void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
41857014Seric 			tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
41964768Seric 	define('t', newstr(tbuf), e);
42011932Seric 	(void) strcpy(dbuf, ctime(&now));
42158131Seric 	p = strchr(dbuf, '\n');
42258131Seric 	if (p != NULL)
42358131Seric 		*p = '\0';
42464768Seric 	define('d', newstr(dbuf), e);
42564086Seric 	p = arpadate(dbuf);
42664086Seric 	p = newstr(p);
42755012Seric 	if (macvalue('a', e) == NULL)
42855012Seric 		define('a', p, e);
42955012Seric 	define('b', p, e);
43011932Seric }
43111932Seric /*
4329536Seric **  OPENXSCRIPT -- Open transcript file
4339536Seric **
4349536Seric **	Creates a transcript file for possible eventual mailing or
4359536Seric **	sending back.
4369536Seric **
4379536Seric **	Parameters:
4389536Seric **		e -- the envelope to create the transcript in/for.
4399536Seric **
4409536Seric **	Returns:
4419536Seric **		none
4429536Seric **
4439536Seric **	Side Effects:
4449536Seric **		Creates the transcript file.
4459536Seric */
4469536Seric 
44758803Seric #ifndef O_APPEND
44858803Seric #define O_APPEND	0
44958803Seric #endif
45058803Seric 
45160494Seric void
4529536Seric openxscript(e)
4539536Seric 	register ENVELOPE *e;
4549536Seric {
4559536Seric 	register char *p;
45640933Srick 	int fd;
4579536Seric 
4589536Seric 	if (e->e_xfp != NULL)
4599536Seric 		return;
4609536Seric 	p = queuename(e, 'x');
46158803Seric 	fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
46240933Srick 	if (fd < 0)
46363753Seric 	{
46463753Seric 		syserr("Can't create transcript file %s", p);
46563753Seric 		fd = open("/dev/null", O_WRONLY, 0644);
46663753Seric 		if (fd < 0)
46763753Seric 			syserr("!Can't open /dev/null");
46863753Seric 	}
46963753Seric 	e->e_xfp = fdopen(fd, "w");
47064724Seric 	if (e->e_xfp == NULL)
47164724Seric 	{
47264724Seric 		syserr("!Can't create transcript stream %s", p);
47364724Seric 	}
47464743Seric 	if (tTd(46, 9))
47564743Seric 	{
47664743Seric 		printf("openxscript(%s):\n  ", p);
47764743Seric 		dumpfd(fileno(e->e_xfp), TRUE, FALSE);
47864743Seric 	}
4799536Seric }
4809536Seric /*
48110196Seric **  CLOSEXSCRIPT -- close the transcript file.
48210196Seric **
48310196Seric **	Parameters:
48410196Seric **		e -- the envelope containing the transcript to close.
48510196Seric **
48610196Seric **	Returns:
48710196Seric **		none.
48810196Seric **
48910196Seric **	Side Effects:
49010196Seric **		none.
49110196Seric */
49210196Seric 
49360494Seric void
49410196Seric closexscript(e)
49510196Seric 	register ENVELOPE *e;
49610196Seric {
49710196Seric 	if (e->e_xfp == NULL)
49810196Seric 		return;
49958680Seric 	(void) xfclose(e->e_xfp, "closexscript", e->e_id);
50010196Seric 	e->e_xfp = NULL;
50110196Seric }
50210196Seric /*
5039536Seric **  SETSENDER -- set the person who this message is from
5049536Seric **
5059536Seric **	Under certain circumstances allow the user to say who
5069536Seric **	s/he is (using -f or -r).  These are:
5079536Seric **	1.  The user's uid is zero (root).
5089536Seric **	2.  The user's login name is in an approved list (typically
5099536Seric **	    from a network server).
5109536Seric **	3.  The address the user is trying to claim has a
5119536Seric **	    "!" character in it (since #2 doesn't do it for
5129536Seric **	    us if we are dialing out for UUCP).
5139536Seric **	A better check to replace #3 would be if the
5149536Seric **	effective uid is "UUCP" -- this would require me
5159536Seric **	to rewrite getpwent to "grab" uucp as it went by,
5169536Seric **	make getname more nasty, do another passwd file
5179536Seric **	scan, or compile the UID of "UUCP" into the code,
5189536Seric **	all of which are reprehensible.
5199536Seric **
5209536Seric **	Assuming all of these fail, we figure out something
5219536Seric **	ourselves.
5229536Seric **
5239536Seric **	Parameters:
5249536Seric **		from -- the person we would like to believe this message
5259536Seric **			is from, as specified on the command line.
52653182Seric **		e -- the envelope in which we would like the sender set.
52758333Seric **		delimptr -- if non-NULL, set to the location of the
52858333Seric **			trailing delimiter.
52958704Seric **		internal -- set if this address is coming from an internal
53058704Seric **			source such as an owner alias.
5319536Seric **
5329536Seric **	Returns:
53358704Seric **		none.
5349536Seric **
5359536Seric **	Side Effects:
5369536Seric **		sets sendmail's notion of who the from person is.
5379536Seric */
5389536Seric 
53960494Seric void
54058704Seric setsender(from, e, delimptr, internal)
5419536Seric 	char *from;
54253182Seric 	register ENVELOPE *e;
54358333Seric 	char **delimptr;
54458704Seric 	bool internal;
5459536Seric {
5469536Seric 	register char **pvp;
5479536Seric 	char *realname = NULL;
54818665Seric 	register struct passwd *pw;
54958727Seric 	char delimchar;
55064147Seric 	char *bp;
55164147Seric 	char buf[MAXNAME + 2];
55216913Seric 	char pvpbuf[PSBUFSIZE];
55318665Seric 	extern struct passwd *getpwnam();
5549536Seric 	extern char *FullName;
5559536Seric 
5569536Seric 	if (tTd(45, 1))
55714786Seric 		printf("setsender(%s)\n", from == NULL ? "" : from);
5589536Seric 
5599536Seric 	/*
5609536Seric 	**  Figure out the real user executing us.
5619536Seric 	**	Username can return errno != 0 on non-errors.
5629536Seric 	*/
5639536Seric 
56465580Seric 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
56565983Seric 	    OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
5669536Seric 		realname = from;
5679536Seric 	if (realname == NULL || realname[0] == '\0')
5689536Seric 		realname = username();
5699536Seric 
57059027Seric 	if (ConfigLevel < 2)
57159027Seric 		SuprErrs = TRUE;
57259027Seric 
57358727Seric 	delimchar = internal ? '\0' : ' ';
57464793Seric 	e->e_from.q_flags = QBADADDR;
57558333Seric 	if (from == NULL ||
57664284Seric 	    parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
57764793Seric 		      delimchar, delimptr, e) == NULL ||
57864793Seric 	    bitset(QBADADDR, e->e_from.q_flags) ||
57964793Seric 	    e->e_from.q_mailer == ProgMailer ||
58064793Seric 	    e->e_from.q_mailer == FileMailer ||
58164793Seric 	    e->e_from.q_mailer == InclMailer)
5829536Seric 	{
58321750Seric 		/* log garbage addresses for traceback */
58455173Seric # ifdef LOG
58558020Seric 		if (from != NULL && LogLevel > 2)
58621750Seric 		{
58758951Seric 			char *p;
58858951Seric 			char ebuf[MAXNAME * 2 + 2];
58955173Seric 
59058951Seric 			p = macvalue('_', e);
59158951Seric 			if (p == NULL)
59258951Seric 			{
59358951Seric 				char *host = RealHostName;
59458951Seric 				if (host == NULL)
59558951Seric 					host = MyHostName;
59658951Seric 				(void) sprintf(ebuf, "%s@%s", realname, host);
59758951Seric 				p = ebuf;
59858951Seric 			}
59955173Seric 			syslog(LOG_NOTICE,
60064793Seric 				"setsender: %s: invalid or unparseable, received from %s",
60165015Seric 				shortenstring(from, 83), p);
60255173Seric 		}
60356795Seric # endif /* LOG */
60457589Seric 		if (from != NULL)
60564793Seric 		{
60664793Seric 			if (!bitset(QBADADDR, e->e_from.q_flags))
60764793Seric 			{
60864793Seric 				/* it was a bogus mailer in the from addr */
60964793Seric 				usrerr("553 Invalid sender address");
61064793Seric 			}
61157589Seric 			SuprErrs = TRUE;
61264793Seric 		}
61357589Seric 		if (from == realname ||
61464284Seric 		    parseaddr(from = newstr(realname), &e->e_from,
61564284Seric 			      RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL)
61624944Seric 		{
61764793Seric 			char nbuf[100];
61864793Seric 
61957589Seric 			SuprErrs = TRUE;
62064808Seric 			expand("\201n", nbuf, &nbuf[sizeof nbuf], e);
62164793Seric 			if (parseaddr(from = newstr(nbuf), &e->e_from,
62264793Seric 				      RF_COPYALL, ' ', NULL, e) == NULL &&
62364793Seric 			    parseaddr(from = "postmaster", &e->e_from,
62464793Seric 			    	      RF_COPYALL, ' ', NULL, e) == NULL)
62558151Seric 				syserr("553 setsender: can't even parse postmaster!");
62624944Seric 		}
6279536Seric 	}
6289536Seric 	else
6299536Seric 		FromFlag = TRUE;
63053182Seric 	e->e_from.q_flags |= QDONTSEND;
63157731Seric 	if (tTd(45, 5))
63257731Seric 	{
63357731Seric 		printf("setsender: QDONTSEND ");
63457731Seric 		printaddr(&e->e_from, FALSE);
63557731Seric 	}
6369536Seric 	SuprErrs = FALSE;
6379536Seric 
63853736Seric 	pvp = NULL;
63967472Seric 	if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
6409536Seric 	{
64153736Seric # ifdef USERDB
64253736Seric 		register char *p;
64353736Seric 		extern char *udbsender();
64453736Seric # endif
64517472Seric 
64658704Seric 		if (!internal)
64758704Seric 		{
64858704Seric 			/* if the user has given fullname already, don't redefine */
64958704Seric 			if (FullName == NULL)
65058704Seric 				FullName = macvalue('x', e);
65158704Seric 			if (FullName != NULL && FullName[0] == '\0')
65258704Seric 				FullName = NULL;
6539536Seric 
65453736Seric # ifdef USERDB
65564284Seric 			p = udbsender(e->e_from.q_user);
65653736Seric 
65758704Seric 			if (p != NULL)
65858704Seric 			{
65958704Seric 				/*
66058704Seric 				**  We have an alternate address for the sender
66158704Seric 				*/
66253736Seric 
66365066Seric 				pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL);
66458704Seric 			}
66558704Seric # endif /* USERDB */
6669536Seric 		}
66753736Seric 
66853736Seric 		if ((pw = getpwnam(e->e_from.q_user)) != NULL)
66953736Seric 		{
67053736Seric 			/*
67153736Seric 			**  Process passwd file entry.
67253736Seric 			*/
67353736Seric 
67453736Seric 			/* extract home directory */
67565822Seric 			if (strcmp(pw->pw_dir, "/") == 0)
67665822Seric 				e->e_from.q_home = newstr("");
67765822Seric 			else
67865822Seric 				e->e_from.q_home = newstr(pw->pw_dir);
67953736Seric 			define('z', e->e_from.q_home, e);
68053736Seric 
68153736Seric 			/* extract user and group id */
68253736Seric 			e->e_from.q_uid = pw->pw_uid;
68353736Seric 			e->e_from.q_gid = pw->pw_gid;
68465023Seric 			e->e_from.q_flags |= QGOODUID;
68553736Seric 
68653736Seric 			/* extract full name from passwd file */
68753736Seric 			if (FullName == NULL && pw->pw_gecos != NULL &&
68858704Seric 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
68958704Seric 			    !internal)
69053736Seric 			{
69165015Seric 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
69253736Seric 				if (buf[0] != '\0')
69353736Seric 					FullName = newstr(buf);
69453736Seric 			}
69553736Seric 		}
69658704Seric 		if (FullName != NULL && !internal)
69753182Seric 			define('x', FullName, e);
6989536Seric 	}
69965580Seric 	else if (!internal && OpMode != MD_DAEMON)
70011625Seric 	{
70153182Seric 		if (e->e_from.q_home == NULL)
70265822Seric 		{
70353182Seric 			e->e_from.q_home = getenv("HOME");
70466049Seric 			if (e->e_from.q_home != NULL &&
70566049Seric 			    strcmp(e->e_from.q_home, "/") == 0)
70665822Seric 				e->e_from.q_home++;
70765822Seric 		}
70863787Seric 		e->e_from.q_uid = RealUid;
70963787Seric 		e->e_from.q_gid = RealGid;
71065023Seric 		e->e_from.q_flags |= QGOODUID;
71111625Seric 	}
71211625Seric 
7139536Seric 	/*
7149536Seric 	**  Rewrite the from person to dispose of possible implicit
7159536Seric 	**	links in the net.
7169536Seric 	*/
7179536Seric 
7189536Seric 	if (pvp == NULL)
71965066Seric 		pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL);
72053736Seric 	if (pvp == NULL)
7219536Seric 	{
72258403Seric 		/* don't need to give error -- prescan did that already */
72336233Skarels # ifdef LOG
72458020Seric 		if (LogLevel > 2)
72536233Skarels 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
72636233Skarels # endif
7279536Seric 		finis();
7289536Seric 	}
72965071Seric 	(void) rewrite(pvp, 3, 0, e);
73065071Seric 	(void) rewrite(pvp, 1, 0, e);
73165071Seric 	(void) rewrite(pvp, 4, 0, e);
73264147Seric 	bp = buf + 1;
73364147Seric 	cataddr(pvp, NULL, bp, sizeof buf - 2, '\0');
73467619Seric 	if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
73564147Seric 	{
73664147Seric 		/* heuristic: route-addr: add angle brackets */
73764147Seric 		strcat(bp, ">");
73864147Seric 		*--bp = '<';
73964147Seric 	}
74064147Seric 	e->e_sender = newstr(bp);
74158704Seric 	define('f', e->e_sender, e);
7429536Seric 
7439536Seric 	/* save the domain spec if this mailer wants it */
74465584Seric 	if (e->e_from.q_mailer != NULL &&
74553182Seric 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
7469536Seric 	{
7479536Seric 		extern char **copyplist();
7489536Seric 
7499536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
7509536Seric 			pvp++;
7519536Seric 		if (*pvp != NULL)
75253182Seric 			e->e_fromdomain = copyplist(pvp, TRUE);
7539536Seric 	}
7549536Seric }
755