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