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