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.56 (Berkeley) 03/27/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 		extern void printenvflags();
86 
87 		printf("dropenvelope %x: id=", e);
88 		xputs(e->e_id);
89 		printf(", flags=");
90 		printenvflags(e);
91 		if (tTd(50, 10))
92 		{
93 			printf("sendq=");
94 			printaddr(e->e_sendqueue, TRUE);
95 		}
96 	}
97 
98 	/* we must have an id to remove disk files */
99 	if (id == NULL)
100 		return;
101 
102 #ifdef LOG
103 	if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
104 		logsender(e, NULL);
105 	if (LogLevel > 84)
106 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d",
107 				  id, e->e_flags, getpid());
108 #endif /* LOG */
109 	e->e_flags &= ~EF_LOGSENDER;
110 
111 	/* post statistics */
112 	poststats(StatFile);
113 
114 	/*
115 	**  Extract state information from dregs of send list.
116 	*/
117 
118 	e->e_flags &= ~EF_QUEUERUN;
119 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
120 	{
121 		if (bitset(QQUEUEUP, q->q_flags))
122 			queueit = TRUE;
123 
124 		/* see if a notification is needed */
125 		if (bitset(QBADADDR, q->q_flags) &&
126 		    bitset(QPINGONFAILURE, q->q_flags))
127 		{
128 			failure_return = TRUE;
129 			if (q->q_owner == NULL && !emptyaddr(&e->e_from))
130 				(void) sendtolist(e->e_from.q_paddr, NULL,
131 						  &e->e_errorqueue, 0, e);
132 		}
133 		else if (bitset(QPINGONSUCCESS, q->q_flags) &&
134 			 ((bitset(QSENT, q->q_flags) &&
135 			   bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) ||
136 			  bitset(QRELAYED|QEXPLODED, q->q_flags)))
137 		{
138 			success_return = TRUE;
139 		}
140 	}
141 
142 	if (e->e_class < 0)
143 		e->e_flags |= EF_NO_BODY_RETN;
144 
145 	/*
146 	**  See if the message timed out.
147 	*/
148 
149 	if (!queueit)
150 		/* nothing to do */ ;
151 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
152 	{
153 		(void) sprintf(buf, "Cannot send message for %s",
154 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
155 		if (e->e_message != NULL)
156 			free(e->e_message);
157 		e->e_message = newstr(buf);
158 		message(buf);
159 		e->e_flags |= EF_CLRQUEUE;
160 		failure_return = TRUE;
161 		fprintf(e->e_xfp, "Message could not be delivered for %s\n",
162 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
163 		fprintf(e->e_xfp, "Message will be deleted from queue\n");
164 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
165 		{
166 			if (bitset(QQUEUEUP, q->q_flags))
167 				q->q_flags |= QBADADDR;
168 		}
169 	}
170 	else if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 &&
171 	    curtime() > e->e_ctime + TimeOuts.to_q_warning[e->e_timeoutclass])
172 	{
173 		bool delay_return = FALSE;
174 
175 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
176 		{
177 			if (bitset(QQUEUEUP, q->q_flags) &&
178 			    bitset(QPINGONDELAY, q->q_flags))
179 			{
180 				q->q_flags |= QREPORT;
181 				delay_return = TRUE;
182 			}
183 		}
184 		if (delay_return &&
185 		    !bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
186 		    e->e_class >= 0 &&
187 		    strcmp(e->e_from.q_paddr, "<>") != 0 &&
188 		    strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
189 		    (strlen(e->e_from.q_paddr) <= 8 ||
190 		     strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8], "-request") != 0))
191 		{
192 			(void) sprintf(buf,
193 				"Warning: cannot send message for %s",
194 				pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE));
195 			if (e->e_message != NULL)
196 				free(e->e_message);
197 			e->e_message = newstr(buf);
198 			message(buf);
199 			e->e_flags |= EF_WARNING;
200 			failure_return = TRUE;
201 		}
202 		fprintf(e->e_xfp,
203 			"Warning: message still undelivered after %s\n",
204 			pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE));
205 		fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
206 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
207 	}
208 
209 	if (tTd(50, 2))
210 		printf("failure_return=%d success_return=%d queueit=%d\n",
211 			failure_return, success_return, queueit);
212 
213 	/*
214 	**  If we had some fatal error, but no addresses are marked as
215 	**  bad, mark them _all_ as bad.
216 	*/
217 
218 	if (bitset(EF_FATALERRS, e->e_flags) && !failure_return)
219 	{
220 		failure_return = TRUE;
221 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
222 		{
223 			if (!bitset(QDONTSEND, q->q_flags))
224 				q->q_flags |= QBADADDR;
225 		}
226 	}
227 
228 	/*
229 	**  Send back return receipts as requested.
230 	*/
231 
232 /*
233 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)
234 	    && !bitset(PRIV_NORECEIPTS, PrivacyFlags))
235 */
236 	if (e->e_receiptto == NULL)
237 		e->e_receiptto = e->e_from.q_paddr;
238 	if (success_return && !failure_return &&
239 	    !bitset(PRIV_NORECEIPTS, PrivacyFlags) &&
240 	    strcmp(e->e_receiptto, "<>") != 0)
241 	{
242 		auto ADDRESS *rlist = NULL;
243 
244 		e->e_flags |= EF_SENDRECEIPT;
245 		(void) sendtolist(e->e_receiptto, NULLADDR, &rlist, 0, e);
246 		(void) returntosender("Return receipt", rlist, FALSE, e);
247 	}
248 	e->e_flags &= ~EF_SENDRECEIPT;
249 
250 	/*
251 	**  Arrange to send error messages if there are fatal errors.
252 	*/
253 
254 	if (failure_return && e->e_errormode != EM_QUIET)
255 		savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags));
256 
257 	/*
258 	**  Arrange to send warning messages to postmaster as requested.
259 	*/
260 
261 	if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL &&
262 	    !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0)
263 	{
264 		auto ADDRESS *rlist = NULL;
265 
266 		(void) sendtolist(PostMasterCopy, NULLADDR, &rlist, 0, e);
267 		(void) returntosender(e->e_message, rlist, FALSE, e);
268 	}
269 
270 	/*
271 	**  Instantiate or deinstantiate the queue.
272 	*/
273 
274 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
275 	    bitset(EF_CLRQUEUE, e->e_flags))
276 	{
277 		if (tTd(50, 1))
278 		{
279 			extern void printenvflags();
280 
281 			printf("\n===== Dropping [dq]f%s... queueit=%d, e_flags=",
282 				e->e_id, queueit);
283 			printenvflags(e);
284 		}
285 		xunlink(queuename(e, 'd'));
286 		xunlink(queuename(e, 'q'));
287 
288 #ifdef LOG
289 		if (LogLevel > 10)
290 			syslog(LOG_INFO, "%s: done", id);
291 #endif
292 	}
293 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
294 	{
295 #ifdef QUEUE
296 		queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE);
297 #else /* QUEUE */
298 		syserr("554 dropenvelope: queueup");
299 #endif /* QUEUE */
300 	}
301 
302 	/* now unlock the job */
303 	closexscript(e);
304 	unlockqueue(e);
305 
306 	/* make sure that this envelope is marked unused */
307 	if (e->e_dfp != NULL)
308 		(void) xfclose(e->e_dfp, "dropenvelope df", e->e_id);
309 	e->e_dfp = NULL;
310 	e->e_id = NULL;
311 	e->e_flags &= ~EF_HAS_DF;
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_id);
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 /*
809 **  PRINTENVFLAGS -- print envelope flags for debugging
810 **
811 **	Parameters:
812 **		e -- the envelope with the flags to be printed.
813 **
814 **	Returns:
815 **		none.
816 */
817 
818 struct eflags
819 {
820 	char	*ef_name;
821 	u_long	ef_bit;
822 };
823 
824 struct eflags	EnvelopeFlags[] =
825 {
826 	"OLDSTYLE",	EF_OLDSTYLE,
827 	"INQUEUE",	EF_INQUEUE,
828 	"NO_BODY_RETN",	EF_NO_BODY_RETN,
829 	"CLRQUEUE",	EF_CLRQUEUE,
830 	"SENDRECEIPT",	EF_SENDRECEIPT,
831 	"FATALERRS",	EF_FATALERRS,
832 	"KEEPQUEUE",	EF_KEEPQUEUE,
833 	"RESPONSE",	EF_RESPONSE,
834 	"RESENT",	EF_RESENT,
835 	"VRFYONLY",	EF_VRFYONLY,
836 	"WARNING",	EF_WARNING,
837 	"QUEUERUN",	EF_QUEUERUN,
838 	"GLOBALERRS",	EF_GLOBALERRS,
839 	"PM_NOTIFY",	EF_PM_NOTIFY,
840 	"METOO",	EF_METOO,
841 	"LOGSENDER",	EF_LOGSENDER,
842 	"NORECEIPT",	EF_NORECEIPT,
843 	"HAS8BIT",	EF_HAS8BIT,
844 	"NL_NOT_EOL",	EF_NL_NOT_EOL,
845 	"CRLF_NOT_EOL",	EF_CRLF_NOT_EOL,
846 	"RET_PARAM",	EF_RET_PARAM,
847 	"HAS_DF",	EF_HAS_DF,
848 	NULL
849 };
850 
851 void
852 printenvflags(e)
853 	register ENVELOPE *e;
854 {
855 	register struct eflags *ef;
856 	bool first = TRUE;
857 
858 	printf("%lx", e->e_flags);
859 	for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++)
860 	{
861 		if (!bitset(ef->ef_bit, e->e_flags))
862 			continue;
863 		if (first)
864 			printf("<%s", ef->ef_name);
865 		else
866 			printf(",%s", ef->ef_name);
867 		first = FALSE;
868 	}
869 	if (!first)
870 		printf(">\n");
871 }
872