1 #include <pwd.h>
2 #include <time.h>
3 #include "sendmail.h"
4 #include <sys/stat.h>
5 
6 SCCSID(@(#)envelope.c	3.3		12/13/82);
7 
8 /*
9 **  NEWENVELOPE -- allocate a new envelope
10 **
11 **	Supports inheritance.
12 **
13 **	Parameters:
14 **		e -- the new envelope to fill in.
15 **
16 **	Returns:
17 **		e.
18 **
19 **	Side Effects:
20 **		none.
21 */
22 
23 ENVELOPE *
24 newenvelope(e)
25 	register ENVELOPE *e;
26 {
27 	register HDR *bh;
28 	register HDR **nhp;
29 	register ENVELOPE *parent;
30 	extern putheader(), putbody();
31 	extern ENVELOPE BlankEnvelope;
32 
33 	parent = CurEnv;
34 	if (e == CurEnv)
35 		parent = e->e_parent;
36 	clear((char *) e, sizeof *e);
37 	bmove((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
38 	e->e_parent = parent;
39 	e->e_ctime = curtime();
40 	e->e_puthdr = putheader;
41 	e->e_putbody = putbody;
42 	bh = BlankEnvelope.e_header;
43 	nhp = &e->e_header;
44 	while (bh != NULL)
45 	{
46 		*nhp = (HDR *) xalloc(sizeof *bh);
47 		bmove((char *) bh, (char *) *nhp, sizeof *bh);
48 		bh = bh->h_link;
49 		nhp = &(*nhp)->h_link;
50 	}
51 	if (CurEnv->e_xfp != NULL)
52 		(void) fflush(CurEnv->e_xfp);
53 
54 	return (e);
55 }
56 /*
57 **  DROPENVELOPE -- deallocate an envelope.
58 **
59 **	Parameters:
60 **		e -- the envelope to deallocate.
61 **
62 **	Returns:
63 **		none.
64 **
65 **	Side Effects:
66 **		housekeeping necessary to dispose of an envelope.
67 **		Unlocks this queue file.
68 */
69 
70 dropenvelope(e)
71 	register ENVELOPE *e;
72 {
73 	bool queueit = FALSE;
74 	register ADDRESS *q;
75 
76 #ifdef DEBUG
77 	if (tTd(50, 1))
78 	{
79 		printf("dropenvelope %x id=", e);
80 		xputs(e->e_id);
81 		printf(" flags=%o\n", e->e_flags);
82 	}
83 #endif DEBUG
84 #ifdef LOG
85 	if (LogLevel > 10)
86 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d",
87 				  e->e_id == NULL ? "(none)" : e->e_id,
88 				  e->e_flags, getpid());
89 #endif LOG
90 
91 	/* we must have an id to remove disk files */
92 	if (e->e_id == NULL)
93 		return;
94 
95 	/*
96 	**  Extract state information from dregs of send list.
97 	*/
98 
99 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
100 	{
101 		if (bitset(QQUEUEUP, q->q_flags))
102 			queueit = TRUE;
103 	}
104 
105 	/*
106 	**  Send back return receipts as requested.
107 	*/
108 
109 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags))
110 	{
111 		auto ADDRESS *rlist;
112 
113 		sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist);
114 		(void) returntosender("Return receipt", rlist, FALSE);
115 	}
116 
117 	/*
118 	**  See if this message has timed out
119 	*/
120 
121 	if (bitset(EF_TIMEOUT, e->e_flags) && queueit)
122 		timeout(e);
123 
124 	/*
125 	**  Arrange to send error messages if there are fatal errors.
126 	*/
127 
128 	if (bitset(EF_FATALERRS, e->e_flags))
129 		savemail(e);
130 
131 	/*
132 	**  Instantiate or deinstantiate the queue.
133 	*/
134 
135 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
136 	    bitset(EF_CLRQUEUE, e->e_flags))
137 	{
138 		if (e->e_dfp != NULL)
139 			(void) fclose(e->e_dfp);
140 		if (e->e_df != NULL)
141 			xunlink(e->e_df);
142 		xunlink(queuename(e, 'q'));
143 	}
144 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
145 		queueup(e, FALSE, FALSE);
146 
147 	/* now unlock the job */
148 	if (e->e_xfp != NULL)
149 		(void) fclose(e->e_xfp);
150 	unlockqueue(e);
151 
152 	/* make sure that this envelope is marked unused */
153 	e->e_id = e->e_df = NULL;
154 	e->e_dfp = e->e_xfp = NULL;
155 }
156 /*
157 **  CLEARENVELOPE -- clear an envelope without unlocking
158 **
159 **	This is normally used by a child process to get a clean
160 **	envelope without disturbing the parent.
161 **
162 **	Parameters:
163 **		e -- the envelope to clear.
164 **
165 **	Returns:
166 **		none.
167 **
168 **	Side Effects:
169 **		Closes files associated with the envelope.
170 **		Marks the envelope as unallocated.
171 */
172 
173 clearenvelope(e)
174 	register ENVELOPE *e;
175 {
176 	/* clear out any file information */
177 	if (e->e_xfp != NULL)
178 		(void) fclose(e->e_xfp);
179 	if (e->e_dfp != NULL)
180 		(void) fclose(e->e_dfp);
181 	e->e_xfp = e->e_dfp = NULL;
182 
183 	/* now expunge names of objects */
184 	e->e_df = e->e_id = NULL;
185 
186 	/* and the flags which are now meaningless */
187 	e->e_flags = 0;
188 }
189 /*
190 **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
191 **
192 **	Parameters:
193 **		e -- the envelope to unlock.
194 **
195 **	Returns:
196 **		none
197 **
198 **	Side Effects:
199 **		unlocks the queue for `e'.
200 */
201 
202 unlockqueue(e)
203 	ENVELOPE *e;
204 {
205 	/* remove the transcript */
206 #ifdef DEBUG
207 	if (!tTd(51, 4))
208 #endif DEBUG
209 		xunlink(queuename(e, 'x'));
210 
211 	/* last but not least, remove the lock */
212 	xunlink(queuename(e, 'l'));
213 }
214 /*
215 **  INITSYS -- initialize instantiation of system
216 **
217 **	In Daemon mode, this is done in the child.
218 **
219 **	Parameters:
220 **		none.
221 **
222 **	Returns:
223 **		none.
224 **
225 **	Side Effects:
226 **		Initializes the system macros, some global variables,
227 **		etc.  In particular, the current time in various
228 **		forms is set.
229 */
230 
231 initsys()
232 {
233 	auto time_t now;
234 	static char cbuf[5];			/* holds hop count */
235 	static char dbuf[30];			/* holds ctime(tbuf) */
236 	static char pbuf[10];			/* holds pid */
237 	static char tbuf[20];			/* holds "current" time */
238 	static char ybuf[10];			/* holds tty id */
239 	register char *p;
240 	register struct tm *tm;
241 	extern char *ttyname();
242 	extern char *arpadate();
243 	extern struct tm *gmtime();
244 	extern char *macvalue();
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(CurEnv);
253 	CurEnv->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 && QueueRun)
263 		OutChannel = CurEnv->e_xfp;
264 
265 	/*
266 	**  Set up some basic system macros.
267 	*/
268 
269 	/* process id */
270 	(void) sprintf(pbuf, "%d", getpid());
271 	define('p', pbuf, CurEnv);
272 
273 	/* hop count */
274 	(void) sprintf(cbuf, "%d", CurEnv->e_hopcount);
275 	define('c', cbuf, CurEnv);
276 
277 	/* time as integer, unix time, arpa time */
278 	now = curtime();
279 	tm = gmtime(&now);
280 	(void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon,
281 			tm->tm_mday, tm->tm_hour, tm->tm_min);
282 	define('t', tbuf, CurEnv);
283 	(void) strcpy(dbuf, ctime(&now));
284 	*index(dbuf, '\n') = '\0';
285 	if (macvalue('d', CurEnv) == NULL)
286 		define('d', dbuf, CurEnv);
287 	p = newstr(arpadate(dbuf));
288 	if (macvalue('a', CurEnv) == NULL)
289 		define('a', p, CurEnv);
290 	define('b', p, CurEnv);
291 
292 	/* version */
293 	define('v', Version, CurEnv);
294 
295 	/* tty name */
296 	if (macvalue('y', CurEnv) == NULL)
297 	{
298 		p = ttyname(2);
299 		if (p != NULL)
300 		{
301 			if (rindex(p, '/') != NULL)
302 				p = rindex(p, '/') + 1;
303 			(void) strcpy(ybuf, p);
304 			define('y', ybuf, CurEnv);
305 		}
306 	}
307 }
308 /*
309 **  QUEUENAME -- build a file name in the queue directory for this envelope.
310 **
311 **	Assigns an id code if one does not already exist.
312 **	This code is very careful to avoid trashing existing files
313 **	under any circumstances.
314 **		We first create an nf file that is only used when
315 **		assigning an id.  This file is always empty, so that
316 **		we can never accidently truncate an lf file.
317 **
318 **	Parameters:
319 **		e -- envelope to build it in/from.
320 **		type -- the file type, used as the first character
321 **			of the file name.
322 **
323 **	Returns:
324 **		a pointer to the new file name (in a static buffer).
325 **
326 **	Side Effects:
327 **		Will create the lf and qf files if no id code is
328 **		already assigned.  This will cause the envelope
329 **		to be modified.
330 */
331 
332 char *
333 queuename(e, type)
334 	register ENVELOPE *e;
335 	char type;
336 {
337 	static char buf[MAXNAME];
338 	static int pid = -1;
339 	char c1 = 'A';
340 	char c2 = 'A';
341 
342 	if (e->e_id == NULL)
343 	{
344 		char qf[20];
345 		char lf[20];
346 		char nf[20];
347 
348 		/* find a unique id */
349 		if (pid != getpid())
350 		{
351 			/* new process -- start back at "AA" */
352 			pid = getpid();
353 			c1 = 'A';
354 			c2 = 'A' - 1;
355 		}
356 		(void) sprintf(qf, "qfAA%05d", pid);
357 		strcpy(lf, qf);
358 		lf[0] = 'l';
359 		strcpy(nf, qf);
360 		nf[0] = 'n';
361 
362 		while (c1 < '~' || c2 < 'Z')
363 		{
364 			int i;
365 
366 			if (c2 >= 'Z')
367 			{
368 				c1++;
369 				c2 = 'A' - 1;
370 			}
371 			qf[2] = lf[2] = nf[2] = c1;
372 			qf[3] = lf[3] = nf[3] = ++c2;
373 # ifdef DEBUG
374 			if (tTd(7, 20))
375 				printf("queuename: trying \"%s\"\n", nf);
376 # endif DEBUG
377 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
378 				continue;
379 			errno = 0;
380 			i = creat(nf, FileMode);
381 			if (i < 0)
382 			{
383 				(void) unlink(nf);	/* kernel bug */
384 				continue;
385 			}
386 			(void) close(i);
387 			i = link(nf, lf);
388 			(void) unlink(nf);
389 			if (i < 0)
390 				continue;
391 			if (link(lf, qf) >= 0)
392 				break;
393 			(void) unlink(lf);
394 		}
395 		if (c1 >= '~' && c2 >= 'Z')
396 		{
397 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
398 				lf, QueueDir);
399 			exit(EX_OSERR);
400 		}
401 		e->e_id = newstr(&qf[2]);
402 		define('i', e->e_id, e);
403 # ifdef DEBUG
404 		if (tTd(7, 1))
405 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
406 # endif DEBUG
407 	}
408 
409 	if (type == '\0')
410 		return (NULL);
411 	(void) sprintf(buf, "%cf%s", type, e->e_id);
412 # ifdef DEBUG
413 	if (tTd(7, 2))
414 		printf("queuename: %s\n", buf);
415 # endif DEBUG
416 	return (buf);
417 }
418 /*
419 **  OPENXSCRIPT -- Open transcript file
420 **
421 **	Creates a transcript file for possible eventual mailing or
422 **	sending back.
423 **
424 **	Parameters:
425 **		e -- the envelope to create the transcript in/for.
426 **
427 **	Returns:
428 **		none
429 **
430 **	Side Effects:
431 **		Creates the transcript file.
432 */
433 
434 openxscript(e)
435 	register ENVELOPE *e;
436 {
437 	register char *p;
438 
439 	if (e->e_xfp != NULL)
440 		return;
441 	p = queuename(e, 'x');
442 	e->e_xfp = fopen(p, "w");
443 	if (e->e_xfp == NULL)
444 		syserr("Can't create %s", p);
445 	else
446 		(void) chmod(p, 0644);
447 }
448 /*
449 **  SETSENDER -- set the person who this message is from
450 **
451 **	Under certain circumstances allow the user to say who
452 **	s/he is (using -f or -r).  These are:
453 **	1.  The user's uid is zero (root).
454 **	2.  The user's login name is in an approved list (typically
455 **	    from a network server).
456 **	3.  The address the user is trying to claim has a
457 **	    "!" character in it (since #2 doesn't do it for
458 **	    us if we are dialing out for UUCP).
459 **	A better check to replace #3 would be if the
460 **	effective uid is "UUCP" -- this would require me
461 **	to rewrite getpwent to "grab" uucp as it went by,
462 **	make getname more nasty, do another passwd file
463 **	scan, or compile the UID of "UUCP" into the code,
464 **	all of which are reprehensible.
465 **
466 **	Assuming all of these fail, we figure out something
467 **	ourselves.
468 **
469 **	Parameters:
470 **		from -- the person we would like to believe this message
471 **			is from, as specified on the command line.
472 **
473 **	Returns:
474 **		none.
475 **
476 **	Side Effects:
477 **		sets sendmail's notion of who the from person is.
478 */
479 
480 setsender(from)
481 	char *from;
482 {
483 	register char **pvp;
484 	register struct passwd *pw = NULL;
485 	char *realname = NULL;
486 	char buf[MAXNAME];
487 	extern char *macvalue();
488 	extern char **prescan();
489 	extern bool safefile();
490 	extern char *FullName;
491 
492 # ifdef DEBUG
493 	if (tTd(45, 1))
494 		printf("setsender(%s)\n", from);
495 # endif DEBUG
496 
497 	/*
498 	**  Figure out the real user executing us.
499 	**	Username can return errno != 0 on non-errors.
500 	*/
501 
502 	if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP)
503 		realname = from;
504 	if (realname == NULL || realname[0] == '\0')
505 	{
506 		extern char *username();
507 
508 		realname = username();
509 		errno = 0;
510 	}
511 	if (realname == NULL || realname[0] == '\0')
512 	{
513 		extern struct passwd *getpwuid();
514 
515 		pw = getpwuid(getruid());
516 		if (pw != NULL)
517 			realname = pw->pw_name;
518 	}
519 	if (realname == NULL || realname[0] == '\0')
520 	{
521 		syserr("Who are you?");
522 		realname = "root";
523 	}
524 
525 	/*
526 	**  Determine if this real person is allowed to alias themselves.
527 	*/
528 
529 	if (from != NULL)
530 	{
531 		extern bool trusteduser();
532 
533 		if (!trusteduser(realname) &&
534 # ifdef DEBUG
535 		    (!tTd(1, 9) || getuid() != geteuid()) &&
536 # endif DEBUG
537 		    index(from, '!') == NULL && getuid() != 0)
538 		{
539 			/* network sends -r regardless (why why why?) */
540 			/* syserr("%s, you cannot use the -f flag", realname); */
541 			from = NULL;
542 		}
543 	}
544 
545 	SuprErrs = TRUE;
546 	if (from == NULL || parse(from, &CurEnv->e_from, 1) == NULL)
547 	{
548 		from = newstr(realname);
549 		(void) parse(from, &CurEnv->e_from, 1);
550 	}
551 	else
552 		FromFlag = TRUE;
553 	CurEnv->e_from.q_flags |= QDONTSEND;
554 	SuprErrs = FALSE;
555 
556 	if (pw == NULL && CurEnv->e_from.q_mailer == LocalMailer)
557 	{
558 		extern struct passwd *getpwnam();
559 
560 		pw = getpwnam(CurEnv->e_from.q_user);
561 	}
562 
563 	/*
564 	**  Process passwd file entry.
565 	*/
566 
567 	if (pw != NULL)
568 	{
569 		/* extract home directory */
570 		CurEnv->e_from.q_home = newstr(pw->pw_dir);
571 
572 		/* run user's .mailcf file */
573 		define('z', CurEnv->e_from.q_home, CurEnv);
574 		expand("$z/.mailcf", buf, &buf[sizeof buf - 1], CurEnv);
575 		if (safefile(buf, getruid(), S_IREAD))
576 			readcf(buf, FALSE);
577 
578 		/* if the user has given fullname already, don't redefine */
579 		if (FullName == NULL)
580 			FullName = macvalue('x', CurEnv);
581 		if (FullName[0] == '\0')
582 			FullName = NULL;
583 
584 		/* extract full name from passwd file */
585 		if (FullName == NULL && pw->pw_gecos != NULL &&
586 		    strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0)
587 		{
588 			buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf);
589 			if (buf[0] != '\0')
590 				FullName = newstr(buf);
591 		}
592 		if (FullName != NULL)
593 			define('x', FullName, CurEnv);
594 	}
595 
596 #ifndef V6
597 	if (CurEnv->e_from.q_home == NULL)
598 		CurEnv->e_from.q_home = getenv("HOME");
599 #endif V6
600 	CurEnv->e_from.q_uid = getuid();
601 	CurEnv->e_from.q_gid = getgid();
602 	if (CurEnv->e_from.q_uid != 0)
603 	{
604 		DefUid = CurEnv->e_from.q_uid;
605 		DefGid = CurEnv->e_from.q_gid;
606 	}
607 
608 	/*
609 	**  Rewrite the from person to dispose of possible implicit
610 	**	links in the net.
611 	*/
612 
613 	pvp = prescan(from, '\0');
614 	if (pvp == NULL)
615 	{
616 		syserr("cannot prescan from (%s)", from);
617 		finis();
618 	}
619 	rewrite(pvp, 3);
620 	rewrite(pvp, 1);
621 	cataddr(pvp, buf, sizeof buf);
622 	define('f', newstr(buf), CurEnv);
623 
624 	/* save the domain spec if this mailer wants it */
625 	if (bitset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags))
626 	{
627 		extern char **copyplist();
628 
629 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
630 			pvp++;
631 		if (*pvp != NULL)
632 			CurEnv->e_fromdomain = copyplist(pvp, TRUE);
633 	}
634 }
635 /*
636 **  TRUSTEDUSER -- tell us if this user is to be trusted.
637 **
638 **	Parameters:
639 **		user -- the user to be checked.
640 **
641 **	Returns:
642 **		TRUE if the user is in an approved list.
643 **		FALSE otherwise.
644 **
645 **	Side Effects:
646 **		none.
647 */
648 
649 bool
650 trusteduser(user)
651 	char *user;
652 {
653 	register char **ulist;
654 	extern char *TrustedUsers[];
655 
656 	for (ulist = TrustedUsers; *ulist != NULL; ulist++)
657 		if (strcmp(*ulist, user) == 0)
658 			return (TRUE);
659 	return (FALSE);
660 }
661