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.1 (Berkeley) 06/07/93";
11 #endif /* not lint */
12 
13 #include "sendmail.h"
14 #include <sys/time.h>
15 #include <pwd.h>
16 
17 /*
18 **  NEWENVELOPE -- allocate a new envelope
19 **
20 **	Supports inheritance.
21 **
22 **	Parameters:
23 **		e -- the new envelope to fill in.
24 **		parent -- the envelope to be the parent of e.
25 **
26 **	Returns:
27 **		e.
28 **
29 **	Side Effects:
30 **		none.
31 */
32 
33 ENVELOPE *
34 newenvelope(e, parent)
35 	register ENVELOPE *e;
36 	register ENVELOPE *parent;
37 {
38 	extern putheader(), putbody();
39 	extern ENVELOPE BlankEnvelope;
40 
41 	if (e == parent && e->e_parent != NULL)
42 		parent = e->e_parent;
43 	clearenvelope(e, TRUE);
44 	if (e == CurEnv)
45 		bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from);
46 	else
47 		bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
48 	e->e_parent = parent;
49 	e->e_ctime = curtime();
50 	if (parent != NULL)
51 		e->e_msgpriority = parent->e_msgsize;
52 	e->e_puthdr = putheader;
53 	e->e_putbody = putbody;
54 	if (CurEnv->e_xfp != NULL)
55 		(void) fflush(CurEnv->e_xfp);
56 
57 	return (e);
58 }
59 /*
60 **  DROPENVELOPE -- deallocate an envelope.
61 **
62 **	Parameters:
63 **		e -- the envelope to deallocate.
64 **
65 **	Returns:
66 **		none.
67 **
68 **	Side Effects:
69 **		housekeeping necessary to dispose of an envelope.
70 **		Unlocks this queue file.
71 */
72 
73 void
74 dropenvelope(e)
75 	register ENVELOPE *e;
76 {
77 	bool queueit = FALSE;
78 	register ADDRESS *q;
79 	char *id = e->e_id;
80 
81 	if (tTd(50, 1))
82 	{
83 		printf("dropenvelope %x: id=", e);
84 		xputs(e->e_id);
85 		printf(", flags=%o\n", e->e_flags);
86 	}
87 
88 	/* we must have an id to remove disk files */
89 	if (id == NULL)
90 		return;
91 
92 #ifdef LOG
93 	if (LogLevel > 84)
94 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d",
95 				  id, e->e_flags, getpid());
96 #endif /* LOG */
97 
98 	/*
99 	**  Extract state information from dregs of send list.
100 	*/
101 
102 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
103 	{
104 		if (bitset(QQUEUEUP, q->q_flags))
105 			queueit = TRUE;
106 	}
107 
108 	/*
109 	**  Send back return receipts as requested.
110 	*/
111 
112 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags))
113 	{
114 		auto ADDRESS *rlist = NULL;
115 
116 		(void) sendtolist(e->e_receiptto, (ADDRESS *) NULL, &rlist, e);
117 		(void) returntosender("Return receipt", rlist, FALSE, e);
118 	}
119 
120 	/*
121 	**  Arrange to send error messages if there are fatal errors.
122 	*/
123 
124 	if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) &&
125 	    e->e_errormode != EM_QUIET)
126 		savemail(e);
127 
128 	/*
129 	**  Instantiate or deinstantiate the queue.
130 	*/
131 
132 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
133 	    bitset(EF_CLRQUEUE, e->e_flags))
134 	{
135 		if (e->e_df != NULL)
136 			xunlink(e->e_df);
137 		xunlink(queuename(e, 'q'));
138 	}
139 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
140 	{
141 #ifdef QUEUE
142 		queueup(e, FALSE, FALSE);
143 #else /* QUEUE */
144 		syserr("554 dropenvelope: queueup");
145 #endif /* QUEUE */
146 	}
147 
148 	/* now unlock the job */
149 	closexscript(e);
150 	unlockqueue(e);
151 
152 	/* make sure that this envelope is marked unused */
153 	if (e->e_dfp != NULL)
154 		(void) xfclose(e->e_dfp, "dropenvelope", e->e_df);
155 	e->e_dfp = NULL;
156 	e->e_id = e->e_df = NULL;
157 
158 #ifdef LOG
159 	if (LogLevel > 74)
160 		syslog(LOG_INFO, "%s: done", id);
161 #endif /* LOG */
162 }
163 /*
164 **  CLEARENVELOPE -- clear an envelope without unlocking
165 **
166 **	This is normally used by a child process to get a clean
167 **	envelope without disturbing the parent.
168 **
169 **	Parameters:
170 **		e -- the envelope to clear.
171 **		fullclear - if set, the current envelope is total
172 **			garbage and should be ignored; otherwise,
173 **			release any resources it may indicate.
174 **
175 **	Returns:
176 **		none.
177 **
178 **	Side Effects:
179 **		Closes files associated with the envelope.
180 **		Marks the envelope as unallocated.
181 */
182 
183 void
184 clearenvelope(e, fullclear)
185 	register ENVELOPE *e;
186 	bool fullclear;
187 {
188 	register HDR *bh;
189 	register HDR **nhp;
190 	extern ENVELOPE BlankEnvelope;
191 
192 	if (!fullclear)
193 	{
194 		/* clear out any file information */
195 		if (e->e_xfp != NULL)
196 			(void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id);
197 		if (e->e_dfp != NULL)
198 			(void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df);
199 		e->e_xfp = e->e_dfp = NULL;
200 	}
201 
202 	/* now clear out the data */
203 	STRUCTCOPY(BlankEnvelope, *e);
204 	if (Verbose)
205 		e->e_sendmode = SM_DELIVER;
206 	bh = BlankEnvelope.e_header;
207 	nhp = &e->e_header;
208 	while (bh != NULL)
209 	{
210 		*nhp = (HDR *) xalloc(sizeof *bh);
211 		bcopy((char *) bh, (char *) *nhp, sizeof *bh);
212 		bh = bh->h_link;
213 		nhp = &(*nhp)->h_link;
214 	}
215 }
216 /*
217 **  INITSYS -- initialize instantiation of system
218 **
219 **	In Daemon mode, this is done in the child.
220 **
221 **	Parameters:
222 **		none.
223 **
224 **	Returns:
225 **		none.
226 **
227 **	Side Effects:
228 **		Initializes the system macros, some global variables,
229 **		etc.  In particular, the current time in various
230 **		forms is set.
231 */
232 
233 void
234 initsys(e)
235 	register ENVELOPE *e;
236 {
237 	static char cbuf[5];			/* holds hop count */
238 	static char pbuf[10];			/* holds pid */
239 #ifdef TTYNAME
240 	static char ybuf[60];			/* holds tty id */
241 	register char *p;
242 #endif /* TTYNAME */
243 	extern char *ttyname();
244 	extern void settime();
245 	extern char Version[];
246 
247 	/*
248 	**  Give this envelope a reality.
249 	**	I.e., an id, a transcript, and a creation time.
250 	*/
251 
252 	openxscript(e);
253 	e->e_ctime = curtime();
254 
255 	/*
256 	**  Set OutChannel to something useful if stdout isn't it.
257 	**	This arranges that any extra stuff the mailer produces
258 	**	gets sent back to the user on error (because it is
259 	**	tucked away in the transcript).
260 	*/
261 
262 	if (OpMode == MD_DAEMON && !bitset(EF_QUEUERUN, e->e_flags) &&
263 	    e->e_xfp != NULL)
264 		OutChannel = e->e_xfp;
265 
266 	/*
267 	**  Set up some basic system macros.
268 	*/
269 
270 	/* process id */
271 	(void) sprintf(pbuf, "%d", getpid());
272 	define('p', pbuf, e);
273 
274 	/* hop count */
275 	(void) sprintf(cbuf, "%d", e->e_hopcount);
276 	define('c', cbuf, e);
277 
278 	/* time as integer, unix time, arpa time */
279 	settime(e);
280 
281 #ifdef TTYNAME
282 	/* tty name */
283 	if (macvalue('y', e) == NULL)
284 	{
285 		p = ttyname(2);
286 		if (p != NULL)
287 		{
288 			if (strrchr(p, '/') != NULL)
289 				p = strrchr(p, '/') + 1;
290 			(void) strcpy(ybuf, p);
291 			define('y', ybuf, e);
292 		}
293 	}
294 #endif /* TTYNAME */
295 }
296 /*
297 **  SETTIME -- set the current time.
298 **
299 **	Parameters:
300 **		none.
301 **
302 **	Returns:
303 **		none.
304 **
305 **	Side Effects:
306 **		Sets the various time macros -- $a, $b, $d, $t.
307 */
308 
309 void
310 settime(e)
311 	register ENVELOPE *e;
312 {
313 	register char *p;
314 	auto time_t now;
315 	static char tbuf[20];			/* holds "current" time */
316 	static char dbuf[30];			/* holds ctime(tbuf) */
317 	register struct tm *tm;
318 	extern char *arpadate();
319 	extern struct tm *gmtime();
320 
321 	now = curtime();
322 	tm = gmtime(&now);
323 	(void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
324 			tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
325 	define('t', tbuf, e);
326 	(void) strcpy(dbuf, ctime(&now));
327 	p = strchr(dbuf, '\n');
328 	if (p != NULL)
329 		*p = '\0';
330 	define('d', dbuf, e);
331 	p = newstr(arpadate(dbuf));
332 	if (macvalue('a', e) == NULL)
333 		define('a', p, e);
334 	define('b', p, e);
335 }
336 /*
337 **  OPENXSCRIPT -- Open transcript file
338 **
339 **	Creates a transcript file for possible eventual mailing or
340 **	sending back.
341 **
342 **	Parameters:
343 **		e -- the envelope to create the transcript in/for.
344 **
345 **	Returns:
346 **		none
347 **
348 **	Side Effects:
349 **		Creates the transcript file.
350 */
351 
352 #ifndef O_APPEND
353 #define O_APPEND	0
354 #endif
355 
356 void
357 openxscript(e)
358 	register ENVELOPE *e;
359 {
360 	register char *p;
361 	int fd;
362 
363 	if (e->e_xfp != NULL)
364 		return;
365 	p = queuename(e, 'x');
366 	fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
367 	if (fd < 0)
368 		syserr("Can't create %s", p);
369 	else
370 		e->e_xfp = fdopen(fd, "w");
371 }
372 /*
373 **  CLOSEXSCRIPT -- close the transcript file.
374 **
375 **	Parameters:
376 **		e -- the envelope containing the transcript to close.
377 **
378 **	Returns:
379 **		none.
380 **
381 **	Side Effects:
382 **		none.
383 */
384 
385 void
386 closexscript(e)
387 	register ENVELOPE *e;
388 {
389 	if (e->e_xfp == NULL)
390 		return;
391 	(void) xfclose(e->e_xfp, "closexscript", e->e_id);
392 	e->e_xfp = NULL;
393 }
394 /*
395 **  SETSENDER -- set the person who this message is from
396 **
397 **	Under certain circumstances allow the user to say who
398 **	s/he is (using -f or -r).  These are:
399 **	1.  The user's uid is zero (root).
400 **	2.  The user's login name is in an approved list (typically
401 **	    from a network server).
402 **	3.  The address the user is trying to claim has a
403 **	    "!" character in it (since #2 doesn't do it for
404 **	    us if we are dialing out for UUCP).
405 **	A better check to replace #3 would be if the
406 **	effective uid is "UUCP" -- this would require me
407 **	to rewrite getpwent to "grab" uucp as it went by,
408 **	make getname more nasty, do another passwd file
409 **	scan, or compile the UID of "UUCP" into the code,
410 **	all of which are reprehensible.
411 **
412 **	Assuming all of these fail, we figure out something
413 **	ourselves.
414 **
415 **	Parameters:
416 **		from -- the person we would like to believe this message
417 **			is from, as specified on the command line.
418 **		e -- the envelope in which we would like the sender set.
419 **		delimptr -- if non-NULL, set to the location of the
420 **			trailing delimiter.
421 **		internal -- set if this address is coming from an internal
422 **			source such as an owner alias.
423 **
424 **	Returns:
425 **		none.
426 **
427 **	Side Effects:
428 **		sets sendmail's notion of who the from person is.
429 */
430 
431 void
432 setsender(from, e, delimptr, internal)
433 	char *from;
434 	register ENVELOPE *e;
435 	char **delimptr;
436 	bool internal;
437 {
438 	register char **pvp;
439 	char *realname = NULL;
440 	register struct passwd *pw;
441 	char delimchar;
442 	char buf[MAXNAME];
443 	char pvpbuf[PSBUFSIZE];
444 	extern struct passwd *getpwnam();
445 	extern char *FullName;
446 
447 	if (tTd(45, 1))
448 		printf("setsender(%s)\n", from == NULL ? "" : from);
449 
450 	/*
451 	**  Figure out the real user executing us.
452 	**	Username can return errno != 0 on non-errors.
453 	*/
454 
455 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP)
456 		realname = from;
457 	if (realname == NULL || realname[0] == '\0')
458 		realname = username();
459 
460 	if (ConfigLevel < 2)
461 		SuprErrs = TRUE;
462 
463 	delimchar = internal ? '\0' : ' ';
464 	if (from == NULL ||
465 	    parseaddr(from, &e->e_from, 1, delimchar, delimptr, e) == NULL)
466 	{
467 		/* log garbage addresses for traceback */
468 # ifdef LOG
469 		if (from != NULL && LogLevel > 2)
470 		{
471 			char *p;
472 			char ebuf[MAXNAME * 2 + 2];
473 
474 			p = macvalue('_', e);
475 			if (p == NULL)
476 			{
477 				char *host = RealHostName;
478 				if (host == NULL)
479 					host = MyHostName;
480 				(void) sprintf(ebuf, "%s@%s", realname, host);
481 				p = ebuf;
482 			}
483 			syslog(LOG_NOTICE,
484 				"from=%s unparseable, received from %s",
485 				from, p);
486 		}
487 # endif /* LOG */
488 		if (from != NULL)
489 			SuprErrs = TRUE;
490 		if (from == realname ||
491 		    parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL)
492 		{
493 			SuprErrs = TRUE;
494 			if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL)
495 				syserr("553 setsender: can't even parse postmaster!");
496 		}
497 	}
498 	else
499 		FromFlag = TRUE;
500 	e->e_from.q_flags |= QDONTSEND;
501 	if (tTd(45, 5))
502 	{
503 		printf("setsender: QDONTSEND ");
504 		printaddr(&e->e_from, FALSE);
505 	}
506 	SuprErrs = FALSE;
507 
508 	pvp = NULL;
509 	if (e->e_from.q_mailer == LocalMailer)
510 	{
511 # ifdef USERDB
512 		register char *p;
513 		extern char *udbsender();
514 # endif
515 
516 		if (!internal)
517 		{
518 			/* if the user has given fullname already, don't redefine */
519 			if (FullName == NULL)
520 				FullName = macvalue('x', e);
521 			if (FullName != NULL && FullName[0] == '\0')
522 				FullName = NULL;
523 
524 # ifdef USERDB
525 			p = udbsender(from);
526 
527 			if (p != NULL)
528 			{
529 				/*
530 				**  We have an alternate address for the sender
531 				*/
532 
533 				pvp = prescan(p, '\0', pvpbuf, NULL);
534 			}
535 # endif /* USERDB */
536 		}
537 
538 		if ((pw = getpwnam(e->e_from.q_user)) != NULL)
539 		{
540 			/*
541 			**  Process passwd file entry.
542 			*/
543 
544 
545 			/* extract home directory */
546 			e->e_from.q_home = newstr(pw->pw_dir);
547 			define('z', e->e_from.q_home, e);
548 
549 			/* extract user and group id */
550 			e->e_from.q_uid = pw->pw_uid;
551 			e->e_from.q_gid = pw->pw_gid;
552 
553 			/* extract full name from passwd file */
554 			if (FullName == NULL && pw->pw_gecos != NULL &&
555 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
556 			    !internal)
557 			{
558 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
559 				if (buf[0] != '\0')
560 					FullName = newstr(buf);
561 			}
562 		}
563 		if (FullName != NULL && !internal)
564 			define('x', FullName, e);
565 	}
566 	else if (!internal)
567 	{
568 		if (e->e_from.q_home == NULL)
569 			e->e_from.q_home = getenv("HOME");
570 		e->e_from.q_uid = getuid();
571 		e->e_from.q_gid = getgid();
572 	}
573 
574 	/*
575 	**  Rewrite the from person to dispose of possible implicit
576 	**	links in the net.
577 	*/
578 
579 	if (pvp == NULL)
580 		pvp = prescan(from, '\0', pvpbuf, NULL);
581 	if (pvp == NULL)
582 	{
583 		/* don't need to give error -- prescan did that already */
584 # ifdef LOG
585 		if (LogLevel > 2)
586 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
587 # endif
588 		finis();
589 	}
590 	(void) rewrite(pvp, 3, e);
591 	(void) rewrite(pvp, 1, e);
592 	(void) rewrite(pvp, 4, e);
593 	cataddr(pvp, NULL, buf, sizeof buf, '\0');
594 	e->e_sender = newstr(buf);
595 	define('f', e->e_sender, e);
596 
597 	/* save the domain spec if this mailer wants it */
598 	if (!internal && e->e_from.q_mailer != NULL &&
599 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
600 	{
601 		extern char **copyplist();
602 
603 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
604 			pvp++;
605 		if (*pvp != NULL)
606 			e->e_fromdomain = copyplist(pvp, TRUE);
607 	}
608 }
609