1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)envelope.c	8.53 (Berkeley) 03/21/95";
11 #endif /* not lint */
12 
13 #include "sendmail.h"
14 #include <pwd.h>
15 
16 /*
17 **  NEWENVELOPE -- allocate a new envelope
18 **
19 **	Supports inheritance.
20 **
21 **	Parameters:
22 **		e -- the new envelope to fill in.
23 **		parent -- the envelope to be the parent of e.
24 **
25 **	Returns:
26 **		e.
27 **
28 **	Side Effects:
29 **		none.
30 */
31 
32 ENVELOPE *
33 newenvelope(e, parent)
34 	register ENVELOPE *e;
35 	register ENVELOPE *parent;
36 {
37 	extern putheader(), putbody();
38 	extern ENVELOPE BlankEnvelope;
39 
40 	if (e == parent && e->e_parent != NULL)
41 		parent = e->e_parent;
42 	clearenvelope(e, TRUE);
43 	if (e == CurEnv)
44 		bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from);
45 	else
46 		bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
47 	e->e_parent = parent;
48 	e->e_ctime = curtime();
49 	if (parent != NULL)
50 		e->e_msgpriority = parent->e_msgsize;
51 	e->e_puthdr = putheader;
52 	e->e_putbody = putbody;
53 	if (CurEnv->e_xfp != NULL)
54 		(void) fflush(CurEnv->e_xfp);
55 
56 	return (e);
57 }
58 /*
59 **  DROPENVELOPE -- deallocate an envelope.
60 **
61 **	Parameters:
62 **		e -- the envelope to deallocate.
63 **
64 **	Returns:
65 **		none.
66 **
67 **	Side Effects:
68 **		housekeeping necessary to dispose of an envelope.
69 **		Unlocks this queue file.
70 */
71 
72 void
73 dropenvelope(e)
74 	register ENVELOPE *e;
75 {
76 	bool queueit = FALSE;
77 	bool failure_return = FALSE;
78 	bool success_return = FALSE;
79 	register ADDRESS *q;
80 	char *id = e->e_id;
81 	char buf[MAXLINE];
82 
83 	if (tTd(50, 1))
84 	{
85 		printf("dropenvelope %x: id=", e);
86 		xputs(e->e_id);
87 		printf(", flags=0x%x\n", e->e_flags);
88 		if (tTd(50, 10))
89 		{
90 			printf("sendq=");
91 			printaddr(e->e_sendqueue, TRUE);
92 		}
93 	}
94 
95 	/* we must have an id to remove disk files */
96 	if (id == NULL)
97 		return;
98 
99 #ifdef LOG
100 	if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
101 		logsender(e, NULL);
102 	if (LogLevel > 84)
103 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d",
104 				  id, e->e_flags, getpid());
105 #endif /* LOG */
106 	e->e_flags &= ~EF_LOGSENDER;
107 
108 	/* post statistics */
109 	poststats(StatFile);
110 
111 	/*
112 	**  Extract state information from dregs of send list.
113 	*/
114 
115 	e->e_flags &= ~EF_QUEUERUN;
116 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
117 	{
118 		if (bitset(QQUEUEUP, q->q_flags))
119 			queueit = TRUE;
120 
121 		/* see if a notification is needed */
122 		if (e->e_df != NULL &&
123 		    bitset(QBADADDR, q->q_flags) &&
124 		    bitset(QPINGONFAILURE, q->q_flags))
125 		{
126 			failure_return = TRUE;
127 			if (q->q_owner == NULL && !emptyaddr(&e->e_from))
128 				(void) sendtolist(e->e_from.q_paddr, NULL,
129 						  &e->e_errorqueue, 0, e);
130 		}
131 		else if (bitset(QSENT, q->q_flags) &&
132 		    bitnset(M_LOCALMAILER, q->q_mailer->m_flags) &&
133 		    bitset(QPINGONSUCCESS, q->q_flags))
134 		{
135 			success_return = TRUE;
136 		}
137 		else if (bitset(QRELAYED, q->q_flags))
138 		{
139 			success_return = TRUE;
140 		}
141 		else
142 		{
143 			continue;
144 		}
145 	}
146 
147 	if (e->e_class < 0)
148 		e->e_flags |= EF_NO_BODY_RETN;
149 
150 	/*
151 	**  See if the message timed out.
152 	*/
153 
154 	if (!queueit)
155 		/* nothing to do */ ;
156 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
157 	{
158 		(void) sprintf(buf, "Cannot send message for %s",
159 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
160 		if (e->e_message != NULL)
161 			free(e->e_message);
162 		e->e_message = newstr(buf);
163 		message(buf);
164 		e->e_flags |= EF_CLRQUEUE;
165 		failure_return = TRUE;
166 		fprintf(e->e_xfp, "Message could not be delivered for %s\n",
167 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
168 		fprintf(e->e_xfp, "Message will be deleted from queue\n");
169 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
170 		{
171 			if (bitset(QQUEUEUP, q->q_flags))
172 				q->q_flags |= QBADADDR;
173 		}
174 	}
175 	else if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 &&
176 	    curtime() > e->e_ctime + TimeOuts.to_q_warning[e->e_timeoutclass])
177 	{
178 		bool delay_return = FALSE;
179 
180 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
181 		{
182 			if (bitset(QQUEUEUP, q->q_flags) &&
183 			    bitset(QPINGONDELAY, q->q_flags))
184 			{
185 				q->q_flags |= QREPORT;
186 				delay_return = TRUE;
187 			}
188 		}
189 		if (delay_return &&
190 		    !bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
191 		    e->e_class >= 0 &&
192 		    strcmp(e->e_from.q_paddr, "<>") != 0 &&
193 		    strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
194 		    (strlen(e->e_from.q_paddr) <= 8 ||
195 		     strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8], "-request") != 0))
196 		{
197 			(void) sprintf(buf,
198 				"Warning: cannot send message for %s",
199 				pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE));
200 			if (e->e_message != NULL)
201 				free(e->e_message);
202 			e->e_message = newstr(buf);
203 			message(buf);
204 			e->e_flags |= EF_WARNING;
205 			failure_return = TRUE;
206 		}
207 		fprintf(e->e_xfp,
208 			"Warning: message still undelivered after %s\n",
209 			pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE));
210 		fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
211 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
212 	}
213 
214 	if (tTd(50, 2))
215 		printf("failure_return=%d success_return=%d queueit=%d\n",
216 			failure_return, success_return, queueit);
217 
218 	/*
219 	**  If we had some fatal error, but no addresses are marked as
220 	**  bad, mark them _all_ as bad.
221 	*/
222 
223 	if (bitset(EF_FATALERRS, e->e_flags) && !failure_return)
224 	{
225 		failure_return = TRUE;
226 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
227 		{
228 			if (!bitset(QDONTSEND, q->q_flags))
229 				q->q_flags |= QBADADDR;
230 		}
231 	}
232 
233 	/*
234 	**  Send back return receipts as requested.
235 	*/
236 
237 /*
238 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)
239 	    && !bitset(PRIV_NORECEIPTS, PrivacyFlags))
240 */
241 	if (e->e_receiptto == NULL)
242 		e->e_receiptto = e->e_from.q_paddr;
243 	if (success_return && !failure_return &&
244 	    !bitset(PRIV_NORECEIPTS, PrivacyFlags) &&
245 	    strcmp(e->e_receiptto, "<>") != 0)
246 	{
247 		auto ADDRESS *rlist = NULL;
248 
249 		e->e_flags |= EF_SENDRECEIPT;
250 		(void) sendtolist(e->e_receiptto, NULLADDR, &rlist, 0, e);
251 		(void) returntosender("Return receipt", rlist, FALSE, e);
252 	}
253 	e->e_flags &= ~EF_SENDRECEIPT;
254 
255 	/*
256 	**  Arrange to send error messages if there are fatal errors.
257 	*/
258 
259 	if (failure_return && e->e_errormode != EM_QUIET)
260 		savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags));
261 
262 	/*
263 	**  Arrange to send warning messages to postmaster as requested.
264 	*/
265 
266 	if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL &&
267 	    !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0)
268 	{
269 		auto ADDRESS *rlist = NULL;
270 
271 		(void) sendtolist(PostMasterCopy, NULLADDR, &rlist, 0, e);
272 		(void) returntosender(e->e_message, rlist, FALSE, e);
273 	}
274 
275 	/*
276 	**  Instantiate or deinstantiate the queue.
277 	*/
278 
279 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
280 	    bitset(EF_CLRQUEUE, e->e_flags))
281 	{
282 		if (tTd(50, 1))
283 			printf("\n===== Dropping [dq]f%s (queueit=%d, e_flags=%x) =====\n\n",
284 				e->e_id, queueit, e->e_flags);
285 		if (e->e_df != NULL)
286 			xunlink(e->e_df);
287 		xunlink(queuename(e, 'q'));
288 
289 #ifdef LOG
290 		if (LogLevel > 10)
291 			syslog(LOG_INFO, "%s: done", id);
292 #endif
293 	}
294 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
295 	{
296 #ifdef QUEUE
297 		queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE);
298 #else /* QUEUE */
299 		syserr("554 dropenvelope: queueup");
300 #endif /* QUEUE */
301 	}
302 
303 	/* now unlock the job */
304 	closexscript(e);
305 	unlockqueue(e);
306 
307 	/* make sure that this envelope is marked unused */
308 	if (e->e_dfp != NULL)
309 		(void) xfclose(e->e_dfp, "dropenvelope", e->e_df);
310 	e->e_dfp = NULL;
311 	e->e_id = e->e_df = NULL;
312 }
313 /*
314 **  CLEARENVELOPE -- clear an envelope without unlocking
315 **
316 **	This is normally used by a child process to get a clean
317 **	envelope without disturbing the parent.
318 **
319 **	Parameters:
320 **		e -- the envelope to clear.
321 **		fullclear - if set, the current envelope is total
322 **			garbage and should be ignored; otherwise,
323 **			release any resources it may indicate.
324 **
325 **	Returns:
326 **		none.
327 **
328 **	Side Effects:
329 **		Closes files associated with the envelope.
330 **		Marks the envelope as unallocated.
331 */
332 
333 void
334 clearenvelope(e, fullclear)
335 	register ENVELOPE *e;
336 	bool fullclear;
337 {
338 	register HDR *bh;
339 	register HDR **nhp;
340 	extern ENVELOPE BlankEnvelope;
341 
342 	if (!fullclear)
343 	{
344 		/* clear out any file information */
345 		if (e->e_xfp != NULL)
346 			(void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id);
347 		if (e->e_dfp != NULL)
348 			(void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df);
349 		e->e_xfp = e->e_dfp = NULL;
350 	}
351 
352 	/* now clear out the data */
353 	STRUCTCOPY(BlankEnvelope, *e);
354 	if (Verbose)
355 		e->e_sendmode = SM_DELIVER;
356 	bh = BlankEnvelope.e_header;
357 	nhp = &e->e_header;
358 	while (bh != NULL)
359 	{
360 		*nhp = (HDR *) xalloc(sizeof *bh);
361 		bcopy((char *) bh, (char *) *nhp, sizeof *bh);
362 		bh = bh->h_link;
363 		nhp = &(*nhp)->h_link;
364 	}
365 }
366 /*
367 **  INITSYS -- initialize instantiation of system
368 **
369 **	In Daemon mode, this is done in the child.
370 **
371 **	Parameters:
372 **		none.
373 **
374 **	Returns:
375 **		none.
376 **
377 **	Side Effects:
378 **		Initializes the system macros, some global variables,
379 **		etc.  In particular, the current time in various
380 **		forms is set.
381 */
382 
383 void
384 initsys(e)
385 	register ENVELOPE *e;
386 {
387 	char cbuf[5];				/* holds hop count */
388 	char pbuf[10];				/* holds pid */
389 #ifdef TTYNAME
390 	static char ybuf[60];			/* holds tty id */
391 	register char *p;
392 #endif /* TTYNAME */
393 	extern char *ttyname();
394 	extern void settime();
395 	extern char Version[];
396 
397 	/*
398 	**  Give this envelope a reality.
399 	**	I.e., an id, a transcript, and a creation time.
400 	*/
401 
402 	openxscript(e);
403 	e->e_ctime = curtime();
404 
405 	/*
406 	**  Set OutChannel to something useful if stdout isn't it.
407 	**	This arranges that any extra stuff the mailer produces
408 	**	gets sent back to the user on error (because it is
409 	**	tucked away in the transcript).
410 	*/
411 
412 	if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
413 	    e->e_xfp != NULL)
414 		OutChannel = e->e_xfp;
415 
416 	/*
417 	**  Set up some basic system macros.
418 	*/
419 
420 	/* process id */
421 	(void) sprintf(pbuf, "%d", getpid());
422 	define('p', newstr(pbuf), e);
423 
424 	/* hop count */
425 	(void) sprintf(cbuf, "%d", e->e_hopcount);
426 	define('c', newstr(cbuf), e);
427 
428 	/* time as integer, unix time, arpa time */
429 	settime(e);
430 
431 #ifdef TTYNAME
432 	/* tty name */
433 	if (macvalue('y', e) == NULL)
434 	{
435 		p = ttyname(2);
436 		if (p != NULL)
437 		{
438 			if (strrchr(p, '/') != NULL)
439 				p = strrchr(p, '/') + 1;
440 			(void) strcpy(ybuf, p);
441 			define('y', ybuf, e);
442 		}
443 	}
444 #endif /* TTYNAME */
445 }
446 /*
447 **  SETTIME -- set the current time.
448 **
449 **	Parameters:
450 **		none.
451 **
452 **	Returns:
453 **		none.
454 **
455 **	Side Effects:
456 **		Sets the various time macros -- $a, $b, $d, $t.
457 */
458 
459 void
460 settime(e)
461 	register ENVELOPE *e;
462 {
463 	register char *p;
464 	auto time_t now;
465 	char tbuf[20];				/* holds "current" time */
466 	char dbuf[30];				/* holds ctime(tbuf) */
467 	register struct tm *tm;
468 	extern char *arpadate();
469 	extern struct tm *gmtime();
470 
471 	now = curtime();
472 	tm = gmtime(&now);
473 	(void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
474 			tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
475 	define('t', newstr(tbuf), e);
476 	(void) strcpy(dbuf, ctime(&now));
477 	p = strchr(dbuf, '\n');
478 	if (p != NULL)
479 		*p = '\0';
480 	define('d', newstr(dbuf), e);
481 	p = arpadate(dbuf);
482 	p = newstr(p);
483 	if (macvalue('a', e) == NULL)
484 		define('a', p, e);
485 	define('b', p, e);
486 }
487 /*
488 **  OPENXSCRIPT -- Open transcript file
489 **
490 **	Creates a transcript file for possible eventual mailing or
491 **	sending back.
492 **
493 **	Parameters:
494 **		e -- the envelope to create the transcript in/for.
495 **
496 **	Returns:
497 **		none
498 **
499 **	Side Effects:
500 **		Creates the transcript file.
501 */
502 
503 #ifndef O_APPEND
504 #define O_APPEND	0
505 #endif
506 
507 void
508 openxscript(e)
509 	register ENVELOPE *e;
510 {
511 	register char *p;
512 	int fd;
513 
514 	if (e->e_xfp != NULL)
515 		return;
516 	p = queuename(e, 'x');
517 	fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
518 	if (fd < 0)
519 	{
520 		syserr("Can't create transcript file %s", p);
521 		fd = open("/dev/null", O_WRONLY, 0644);
522 		if (fd < 0)
523 			syserr("!Can't open /dev/null");
524 	}
525 	e->e_xfp = fdopen(fd, "a");
526 	if (e->e_xfp == NULL)
527 		syserr("!Can't create transcript stream %s", p);
528 #ifdef HASSETVBUF
529 	setvbuf(e->e_xfp, NULL, _IOLBF, 0);
530 #else
531 	setlinebuf(e->e_xfp);
532 #endif
533 	if (tTd(46, 9))
534 	{
535 		printf("openxscript(%s):\n  ", p);
536 		dumpfd(fileno(e->e_xfp), TRUE, FALSE);
537 	}
538 }
539 /*
540 **  CLOSEXSCRIPT -- close the transcript file.
541 **
542 **	Parameters:
543 **		e -- the envelope containing the transcript to close.
544 **
545 **	Returns:
546 **		none.
547 **
548 **	Side Effects:
549 **		none.
550 */
551 
552 void
553 closexscript(e)
554 	register ENVELOPE *e;
555 {
556 	if (e->e_xfp == NULL)
557 		return;
558 	(void) xfclose(e->e_xfp, "closexscript", e->e_id);
559 	e->e_xfp = NULL;
560 }
561 /*
562 **  SETSENDER -- set the person who this message is from
563 **
564 **	Under certain circumstances allow the user to say who
565 **	s/he is (using -f or -r).  These are:
566 **	1.  The user's uid is zero (root).
567 **	2.  The user's login name is in an approved list (typically
568 **	    from a network server).
569 **	3.  The address the user is trying to claim has a
570 **	    "!" character in it (since #2 doesn't do it for
571 **	    us if we are dialing out for UUCP).
572 **	A better check to replace #3 would be if the
573 **	effective uid is "UUCP" -- this would require me
574 **	to rewrite getpwent to "grab" uucp as it went by,
575 **	make getname more nasty, do another passwd file
576 **	scan, or compile the UID of "UUCP" into the code,
577 **	all of which are reprehensible.
578 **
579 **	Assuming all of these fail, we figure out something
580 **	ourselves.
581 **
582 **	Parameters:
583 **		from -- the person we would like to believe this message
584 **			is from, as specified on the command line.
585 **		e -- the envelope in which we would like the sender set.
586 **		delimptr -- if non-NULL, set to the location of the
587 **			trailing delimiter.
588 **		internal -- set if this address is coming from an internal
589 **			source such as an owner alias.
590 **
591 **	Returns:
592 **		none.
593 **
594 **	Side Effects:
595 **		sets sendmail's notion of who the from person is.
596 */
597 
598 void
599 setsender(from, e, delimptr, internal)
600 	char *from;
601 	register ENVELOPE *e;
602 	char **delimptr;
603 	bool internal;
604 {
605 	register char **pvp;
606 	char *realname = NULL;
607 	register struct passwd *pw;
608 	char delimchar;
609 	char *bp;
610 	char buf[MAXNAME + 2];
611 	char pvpbuf[PSBUFSIZE];
612 	extern struct passwd *getpwnam();
613 	extern char *FullName;
614 
615 	if (tTd(45, 1))
616 		printf("setsender(%s)\n", from == NULL ? "" : from);
617 
618 	/*
619 	**  Figure out the real user executing us.
620 	**	Username can return errno != 0 on non-errors.
621 	*/
622 
623 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
624 	    OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
625 		realname = from;
626 	if (realname == NULL || realname[0] == '\0')
627 		realname = username();
628 
629 	if (ConfigLevel < 2)
630 		SuprErrs = TRUE;
631 
632 	delimchar = internal ? '\0' : ' ';
633 	e->e_from.q_flags = QBADADDR;
634 	if (from == NULL ||
635 	    parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
636 		      delimchar, delimptr, e) == NULL ||
637 	    bitset(QBADADDR, e->e_from.q_flags) ||
638 	    e->e_from.q_mailer == ProgMailer ||
639 	    e->e_from.q_mailer == FileMailer ||
640 	    e->e_from.q_mailer == InclMailer)
641 	{
642 		/* log garbage addresses for traceback */
643 # ifdef LOG
644 		if (from != NULL && LogLevel > 2)
645 		{
646 			char *p;
647 			char ebuf[MAXNAME * 2 + 2];
648 
649 			p = macvalue('_', e);
650 			if (p == NULL)
651 			{
652 				char *host = RealHostName;
653 				if (host == NULL)
654 					host = MyHostName;
655 				(void) sprintf(ebuf, "%s@%s", realname, host);
656 				p = ebuf;
657 			}
658 			syslog(LOG_NOTICE,
659 				"setsender: %s: invalid or unparseable, received from %s",
660 				shortenstring(from, 83), p);
661 		}
662 # endif /* LOG */
663 		if (from != NULL)
664 		{
665 			if (!bitset(QBADADDR, e->e_from.q_flags))
666 			{
667 				/* it was a bogus mailer in the from addr */
668 				usrerr("553 Invalid sender address");
669 			}
670 			SuprErrs = TRUE;
671 		}
672 		if (from == realname ||
673 		    parseaddr(from = newstr(realname), &e->e_from,
674 			      RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL)
675 		{
676 			char nbuf[100];
677 
678 			SuprErrs = TRUE;
679 			expand("\201n", nbuf, sizeof nbuf, e);
680 			if (parseaddr(from = newstr(nbuf), &e->e_from,
681 				      RF_COPYALL, ' ', NULL, e) == NULL &&
682 			    parseaddr(from = "postmaster", &e->e_from,
683 			    	      RF_COPYALL, ' ', NULL, e) == NULL)
684 				syserr("553 setsender: can't even parse postmaster!");
685 		}
686 	}
687 	else
688 		FromFlag = TRUE;
689 	e->e_from.q_flags |= QDONTSEND;
690 	if (tTd(45, 5))
691 	{
692 		printf("setsender: QDONTSEND ");
693 		printaddr(&e->e_from, FALSE);
694 	}
695 	SuprErrs = FALSE;
696 
697 # ifdef USERDB
698 	if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
699 	{
700 		register char *p;
701 		extern char *udbsender();
702 
703 		p = udbsender(e->e_from.q_user);
704 		if (p != NULL)
705 			from = p;
706 	}
707 # endif /* USERDB */
708 
709 	if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
710 	{
711 		if (!internal)
712 		{
713 			/* if the user already given fullname don't redefine */
714 			if (FullName == NULL)
715 				FullName = macvalue('x', e);
716 			if (FullName != NULL && FullName[0] == '\0')
717 				FullName = NULL;
718 		}
719 
720 		if ((pw = getpwnam(e->e_from.q_user)) != NULL)
721 		{
722 			/*
723 			**  Process passwd file entry.
724 			*/
725 
726 			/* extract home directory */
727 			if (strcmp(pw->pw_dir, "/") == 0)
728 				e->e_from.q_home = newstr("");
729 			else
730 				e->e_from.q_home = newstr(pw->pw_dir);
731 			define('z', e->e_from.q_home, e);
732 
733 			/* extract user and group id */
734 			e->e_from.q_uid = pw->pw_uid;
735 			e->e_from.q_gid = pw->pw_gid;
736 			e->e_from.q_flags |= QGOODUID;
737 
738 			/* extract full name from passwd file */
739 			if (FullName == NULL && pw->pw_gecos != NULL &&
740 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
741 			    !internal)
742 			{
743 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
744 				if (buf[0] != '\0')
745 					FullName = newstr(buf);
746 			}
747 		}
748 		if (FullName != NULL && !internal)
749 			define('x', FullName, e);
750 	}
751 	else if (!internal && OpMode != MD_DAEMON)
752 	{
753 		if (e->e_from.q_home == NULL)
754 		{
755 			e->e_from.q_home = getenv("HOME");
756 			if (e->e_from.q_home != NULL &&
757 			    strcmp(e->e_from.q_home, "/") == 0)
758 				e->e_from.q_home++;
759 		}
760 		e->e_from.q_uid = RealUid;
761 		e->e_from.q_gid = RealGid;
762 		e->e_from.q_flags |= QGOODUID;
763 	}
764 
765 	/*
766 	**  Rewrite the from person to dispose of possible implicit
767 	**	links in the net.
768 	*/
769 
770 	pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL);
771 	if (pvp == NULL)
772 	{
773 		/* don't need to give error -- prescan did that already */
774 # ifdef LOG
775 		if (LogLevel > 2)
776 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
777 # endif
778 		finis();
779 	}
780 /*
781 	(void) rewrite(pvp, 3, 0, e);
782 	(void) rewrite(pvp, 1, 0, e);
783 	(void) rewrite(pvp, 4, 0, e);
784 */
785 	bp = buf + 1;
786 	cataddr(pvp, NULL, bp, sizeof buf - 2, '\0');
787 	if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
788 	{
789 		/* heuristic: route-addr: add angle brackets */
790 		strcat(bp, ">");
791 		*--bp = '<';
792 	}
793 	e->e_sender = newstr(bp);
794 	define('f', e->e_sender, e);
795 
796 	/* save the domain spec if this mailer wants it */
797 	if (e->e_from.q_mailer != NULL &&
798 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
799 	{
800 		extern char **copyplist();
801 
802 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
803 			pvp++;
804 		if (*pvp != NULL)
805 			e->e_fromdomain = copyplist(pvp, TRUE);
806 	}
807 }
808