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