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