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.66 (Berkeley) 05/28/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 	if (e == parent && e->e_parent != NULL)
37 		parent = e->e_parent;
38 	clearenvelope(e, TRUE);
39 	if (e == CurEnv)
40 		bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from);
41 	else
42 		bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
43 	e->e_parent = parent;
44 	e->e_ctime = curtime();
45 	if (parent != NULL)
46 		e->e_msgpriority = parent->e_msgsize;
47 	e->e_puthdr = putheader;
48 	e->e_putbody = putbody;
49 	if (CurEnv->e_xfp != NULL)
50 		(void) fflush(CurEnv->e_xfp);
51 
52 	return (e);
53 }
54 /*
55 **  DROPENVELOPE -- deallocate an envelope.
56 **
57 **	Parameters:
58 **		e -- the envelope to deallocate.
59 **
60 **	Returns:
61 **		none.
62 **
63 **	Side Effects:
64 **		housekeeping necessary to dispose of an envelope.
65 **		Unlocks this queue file.
66 */
67 
68 void
69 dropenvelope(e)
70 	register ENVELOPE *e;
71 {
72 	bool queueit = FALSE;
73 	bool failure_return = FALSE;
74 	bool success_return = FALSE;
75 	register ADDRESS *q;
76 	char *id = e->e_id;
77 	char buf[MAXLINE];
78 
79 	if (tTd(50, 1))
80 	{
81 		extern void printenvflags();
82 
83 		printf("dropenvelope %x: id=", e);
84 		xputs(e->e_id);
85 		printf(", flags=");
86 		printenvflags(e);
87 		if (tTd(50, 10))
88 		{
89 			printf("sendq=");
90 			printaddr(e->e_sendqueue, TRUE);
91 		}
92 	}
93 
94 	/* we must have an id to remove disk files */
95 	if (id == NULL)
96 		return;
97 
98 #ifdef LOG
99 	if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
100 		logsender(e, NULL);
101 	if (LogLevel > 84)
102 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d",
103 				  id, e->e_flags, getpid());
104 #endif /* LOG */
105 	e->e_flags &= ~EF_LOGSENDER;
106 
107 	/* post statistics */
108 	poststats(StatFile);
109 
110 	/*
111 	**  Extract state information from dregs of send list.
112 	*/
113 
114 	e->e_flags &= ~EF_QUEUERUN;
115 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
116 	{
117 		if (bitset(QQUEUEUP, q->q_flags))
118 			queueit = TRUE;
119 
120 		/* see if a notification is needed */
121 		if (bitset(QBADADDR, q->q_flags) &&
122 		    bitset(QPINGONFAILURE, q->q_flags))
123 		{
124 			failure_return = TRUE;
125 			if (q->q_owner == NULL && !emptyaddr(&e->e_from))
126 				(void) sendtolist(e->e_from.q_paddr, NULL,
127 						  &e->e_errorqueue, 0, e);
128 		}
129 		else if (bitset(QPINGONSUCCESS, q->q_flags) &&
130 			 ((bitset(QSENT, q->q_flags) &&
131 			   bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) ||
132 			  bitset(QRELAYED|QEXPANDED|QDELIVERED, q->q_flags)))
133 		{
134 			success_return = TRUE;
135 		}
136 	}
137 
138 	if (e->e_class < 0)
139 		e->e_flags |= EF_NO_BODY_RETN;
140 
141 	/*
142 	**  See if the message timed out.
143 	*/
144 
145 	if (!queueit)
146 		/* nothing to do */ ;
147 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
148 	{
149 		(void) sprintf(buf, "Cannot send message for %s",
150 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
151 		if (e->e_message != NULL)
152 			free(e->e_message);
153 		e->e_message = newstr(buf);
154 		message(buf);
155 		e->e_flags |= EF_CLRQUEUE;
156 		failure_return = TRUE;
157 		fprintf(e->e_xfp, "Message could not be delivered for %s\n",
158 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
159 		fprintf(e->e_xfp, "Message will be deleted from queue\n");
160 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
161 		{
162 			if (bitset(QQUEUEUP, q->q_flags))
163 			{
164 				q->q_flags |= QBADADDR;
165 				q->q_status = "4.4.7";
166 			}
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 |= QDELAYED;
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 ((failure_return || bitset(EF_PM_NOTIFY, e->e_flags)) &&
261 	    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 
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 				e->e_status = "5.1.7";
667 				usrerr("553 Invalid sender address");
668 			}
669 			SuprErrs = TRUE;
670 		}
671 		if (from == realname ||
672 		    parseaddr(from = newstr(realname), &e->e_from,
673 			      RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL)
674 		{
675 			char nbuf[100];
676 
677 			SuprErrs = TRUE;
678 			expand("\201n", nbuf, sizeof nbuf, e);
679 			if (parseaddr(from = newstr(nbuf), &e->e_from,
680 				      RF_COPYALL, ' ', NULL, e) == NULL &&
681 			    parseaddr(from = "postmaster", &e->e_from,
682 			    	      RF_COPYALL, ' ', NULL, e) == NULL)
683 				syserr("553 setsender: can't even parse postmaster!");
684 		}
685 	}
686 	else
687 		FromFlag = TRUE;
688 	e->e_from.q_flags |= QDONTSEND;
689 	if (tTd(45, 5))
690 	{
691 		printf("setsender: QDONTSEND ");
692 		printaddr(&e->e_from, FALSE);
693 	}
694 	SuprErrs = FALSE;
695 
696 # if USERDB
697 	if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
698 	{
699 		register char *p;
700 		extern char *udbsender();
701 
702 		p = udbsender(e->e_from.q_user);
703 		if (p != NULL)
704 			from = p;
705 	}
706 # endif /* USERDB */
707 
708 	if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
709 	{
710 		if (!internal)
711 		{
712 			/* if the user already given fullname don't redefine */
713 			if (FullName == NULL)
714 				FullName = macvalue('x', e);
715 			if (FullName != NULL && FullName[0] == '\0')
716 				FullName = NULL;
717 		}
718 
719 		if ((pw = sm_getpwnam(e->e_from.q_user)) != NULL)
720 		{
721 			/*
722 			**  Process passwd file entry.
723 			*/
724 
725 			/* extract home directory */
726 			if (strcmp(pw->pw_dir, "/") == 0)
727 				e->e_from.q_home = newstr("");
728 			else
729 				e->e_from.q_home = newstr(pw->pw_dir);
730 			define('z', e->e_from.q_home, e);
731 
732 			/* extract user and group id */
733 			e->e_from.q_uid = pw->pw_uid;
734 			e->e_from.q_gid = pw->pw_gid;
735 			e->e_from.q_flags |= QGOODUID;
736 
737 			/* extract full name from passwd file */
738 			if (FullName == NULL && pw->pw_gecos != NULL &&
739 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
740 			    !internal)
741 			{
742 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
743 				if (buf[0] != '\0')
744 					FullName = newstr(buf);
745 			}
746 		}
747 		if (FullName != NULL && !internal)
748 			define('x', FullName, e);
749 	}
750 	else if (!internal && OpMode != MD_DAEMON)
751 	{
752 		if (e->e_from.q_home == NULL)
753 		{
754 			e->e_from.q_home = getenv("HOME");
755 			if (e->e_from.q_home != NULL &&
756 			    strcmp(e->e_from.q_home, "/") == 0)
757 				e->e_from.q_home++;
758 		}
759 		e->e_from.q_uid = RealUid;
760 		e->e_from.q_gid = RealGid;
761 		e->e_from.q_flags |= QGOODUID;
762 	}
763 
764 	/*
765 	**  Rewrite the from person to dispose of possible implicit
766 	**	links in the net.
767 	*/
768 
769 	pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL, NULL);
770 	if (pvp == NULL)
771 	{
772 		/* don't need to give error -- prescan did that already */
773 # ifdef LOG
774 		if (LogLevel > 2)
775 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
776 # endif
777 		finis();
778 	}
779 /*
780 	(void) rewrite(pvp, 3, 0, e);
781 	(void) rewrite(pvp, 1, 0, e);
782 	(void) rewrite(pvp, 4, 0, e);
783 */
784 	bp = buf + 1;
785 	cataddr(pvp, NULL, bp, sizeof buf - 2, '\0');
786 	if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
787 	{
788 		/* heuristic: route-addr: add angle brackets */
789 		strcat(bp, ">");
790 		*--bp = '<';
791 	}
792 	e->e_sender = newstr(bp);
793 	define('f', e->e_sender, e);
794 
795 	/* save the domain spec if this mailer wants it */
796 	if (e->e_from.q_mailer != NULL &&
797 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
798 	{
799 		char **lastat;
800 		extern char **copyplist();
801 
802 		/* get rid of any pesky angle brackets */
803 		(void) rewrite(pvp, 3, 0, e);
804 		(void) rewrite(pvp, 1, 0, e);
805 		(void) rewrite(pvp, 4, 0, e);
806 
807 		/* strip off to the last "@" sign */
808 		for (lastat = NULL; *pvp != NULL; pvp++)
809 			if (strcmp(*pvp, "@") == 0)
810 				lastat = pvp;
811 		if (lastat != NULL)
812 		{
813 			e->e_fromdomain = copyplist(lastat, TRUE);
814 			if (tTd(45, 3))
815 			{
816 				printf("Saving from domain: ");
817 				printav(e->e_fromdomain);
818 			}
819 		}
820 	}
821 }
822 /*
823 **  PRINTENVFLAGS -- print envelope flags for debugging
824 **
825 **	Parameters:
826 **		e -- the envelope with the flags to be printed.
827 **
828 **	Returns:
829 **		none.
830 */
831 
832 struct eflags
833 {
834 	char	*ef_name;
835 	u_long	ef_bit;
836 };
837 
838 struct eflags	EnvelopeFlags[] =
839 {
840 	"OLDSTYLE",	EF_OLDSTYLE,
841 	"INQUEUE",	EF_INQUEUE,
842 	"NO_BODY_RETN",	EF_NO_BODY_RETN,
843 	"CLRQUEUE",	EF_CLRQUEUE,
844 	"SENDRECEIPT",	EF_SENDRECEIPT,
845 	"FATALERRS",	EF_FATALERRS,
846 	"KEEPQUEUE",	EF_KEEPQUEUE,
847 	"RESPONSE",	EF_RESPONSE,
848 	"RESENT",	EF_RESENT,
849 	"VRFYONLY",	EF_VRFYONLY,
850 	"WARNING",	EF_WARNING,
851 	"QUEUERUN",	EF_QUEUERUN,
852 	"GLOBALERRS",	EF_GLOBALERRS,
853 	"PM_NOTIFY",	EF_PM_NOTIFY,
854 	"METOO",	EF_METOO,
855 	"LOGSENDER",	EF_LOGSENDER,
856 	"NORECEIPT",	EF_NORECEIPT,
857 	"HAS8BIT",	EF_HAS8BIT,
858 	"NL_NOT_EOL",	EF_NL_NOT_EOL,
859 	"CRLF_NOT_EOL",	EF_CRLF_NOT_EOL,
860 	"RET_PARAM",	EF_RET_PARAM,
861 	"HAS_DF",	EF_HAS_DF,
862 	NULL
863 };
864 
865 void
866 printenvflags(e)
867 	register ENVELOPE *e;
868 {
869 	register struct eflags *ef;
870 	bool first = TRUE;
871 
872 	printf("%lx", e->e_flags);
873 	for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++)
874 	{
875 		if (!bitset(ef->ef_bit, e->e_flags))
876 			continue;
877 		if (first)
878 			printf("<%s", ef->ef_name);
879 		else
880 			printf(",%s", ef->ef_name);
881 		first = FALSE;
882 	}
883 	if (!first)
884 		printf(">\n");
885 }
886