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