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