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.62 (Berkeley) 04/21/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 ((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 	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 char *FullName;
613 
614 	if (tTd(45, 1))
615 		printf("setsender(%s)\n", from == NULL ? "" : from);
616 
617 	/*
618 	**  Figure out the real user executing us.
619 	**	Username can return errno != 0 on non-errors.
620 	*/
621 
622 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
623 	    OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
624 		realname = from;
625 	if (realname == NULL || realname[0] == '\0')
626 		realname = username();
627 
628 	if (ConfigLevel < 2)
629 		SuprErrs = TRUE;
630 
631 	delimchar = internal ? '\0' : ' ';
632 	e->e_from.q_flags = QBADADDR;
633 	if (from == NULL ||
634 	    parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
635 		      delimchar, delimptr, e) == NULL ||
636 	    bitset(QBADADDR, e->e_from.q_flags) ||
637 	    e->e_from.q_mailer == ProgMailer ||
638 	    e->e_from.q_mailer == FileMailer ||
639 	    e->e_from.q_mailer == InclMailer)
640 	{
641 		/* log garbage addresses for traceback */
642 # ifdef LOG
643 		if (from != NULL && LogLevel > 2)
644 		{
645 			char *p;
646 			char ebuf[MAXNAME * 2 + 2];
647 
648 			p = macvalue('_', e);
649 			if (p == NULL)
650 			{
651 				char *host = RealHostName;
652 				if (host == NULL)
653 					host = MyHostName;
654 				(void) sprintf(ebuf, "%s@%s", realname, host);
655 				p = ebuf;
656 			}
657 			syslog(LOG_NOTICE,
658 				"setsender: %s: invalid or unparseable, received from %s",
659 				shortenstring(from, 83), p);
660 		}
661 # endif /* LOG */
662 		if (from != NULL)
663 		{
664 			if (!bitset(QBADADDR, e->e_from.q_flags))
665 			{
666 				/* it was a bogus mailer in the from addr */
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 # ifdef 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