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.26 (Berkeley) 03/19/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 openxscript(e)
349 	register ENVELOPE *e;
350 {
351 	register char *p;
352 	int fd;
353 
354 	if (e->e_xfp != NULL)
355 		return;
356 	p = queuename(e, 'x');
357 	fd = open(p, O_WRONLY|O_CREAT, 0644);
358 	if (fd < 0)
359 		syserr("Can't create %s", p);
360 	else
361 		e->e_xfp = fdopen(fd, "w");
362 }
363 /*
364 **  CLOSEXSCRIPT -- close the transcript file.
365 **
366 **	Parameters:
367 **		e -- the envelope containing the transcript to close.
368 **
369 **	Returns:
370 **		none.
371 **
372 **	Side Effects:
373 **		none.
374 */
375 
376 closexscript(e)
377 	register ENVELOPE *e;
378 {
379 	if (e->e_xfp == NULL)
380 		return;
381 	(void) xfclose(e->e_xfp, "closexscript", e->e_id);
382 	e->e_xfp = NULL;
383 }
384 /*
385 **  SETSENDER -- set the person who this message is from
386 **
387 **	Under certain circumstances allow the user to say who
388 **	s/he is (using -f or -r).  These are:
389 **	1.  The user's uid is zero (root).
390 **	2.  The user's login name is in an approved list (typically
391 **	    from a network server).
392 **	3.  The address the user is trying to claim has a
393 **	    "!" character in it (since #2 doesn't do it for
394 **	    us if we are dialing out for UUCP).
395 **	A better check to replace #3 would be if the
396 **	effective uid is "UUCP" -- this would require me
397 **	to rewrite getpwent to "grab" uucp as it went by,
398 **	make getname more nasty, do another passwd file
399 **	scan, or compile the UID of "UUCP" into the code,
400 **	all of which are reprehensible.
401 **
402 **	Assuming all of these fail, we figure out something
403 **	ourselves.
404 **
405 **	Parameters:
406 **		from -- the person we would like to believe this message
407 **			is from, as specified on the command line.
408 **		e -- the envelope in which we would like the sender set.
409 **		delimptr -- if non-NULL, set to the location of the
410 **			trailing delimiter.
411 **		internal -- set if this address is coming from an internal
412 **			source such as an owner alias.
413 **
414 **	Returns:
415 **		none.
416 **
417 **	Side Effects:
418 **		sets sendmail's notion of who the from person is.
419 */
420 
421 setsender(from, e, delimptr, internal)
422 	char *from;
423 	register ENVELOPE *e;
424 	char **delimptr;
425 	bool internal;
426 {
427 	register char **pvp;
428 	char *realname = NULL;
429 	register struct passwd *pw;
430 	char delimchar;
431 	char buf[MAXNAME];
432 	char pvpbuf[PSBUFSIZE];
433 	extern struct passwd *getpwnam();
434 	extern char *macvalue();
435 	extern char **prescan();
436 	extern char *FullName;
437 
438 	if (tTd(45, 1))
439 		printf("setsender(%s)\n", from == NULL ? "" : from);
440 
441 	/*
442 	**  Figure out the real user executing us.
443 	**	Username can return errno != 0 on non-errors.
444 	*/
445 
446 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP)
447 		realname = from;
448 	if (realname == NULL || realname[0] == '\0')
449 	{
450 		extern char *username();
451 
452 		realname = username();
453 	}
454 
455 /*
456 	SuprErrs = TRUE;
457 */
458 	delimchar = internal ? '\0' : ' ';
459 	if (from == NULL ||
460 	    parseaddr(from, &e->e_from, 1, delimchar, delimptr, e) == NULL)
461 	{
462 		/* log garbage addresses for traceback */
463 # ifdef LOG
464 		if (from != NULL && LogLevel > 2)
465 		{
466 			char *host = RealHostName;
467 
468 			if (host == NULL)
469 				host = MyHostName;
470 			syslog(LOG_NOTICE,
471 				"from=%s unparseable, received from %s@%s",
472 				from, realname, host);
473 		}
474 # endif /* LOG */
475 		if (from != NULL)
476 			SuprErrs = TRUE;
477 		if (from == realname ||
478 		    parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL)
479 		{
480 			SuprErrs = TRUE;
481 			if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL)
482 				syserr("553 setsender: can't even parse postmaster!");
483 		}
484 	}
485 	else
486 		FromFlag = TRUE;
487 	e->e_from.q_flags |= QDONTSEND;
488 	if (tTd(45, 5))
489 	{
490 		printf("setsender: QDONTSEND ");
491 		printaddr(&e->e_from, FALSE);
492 	}
493 	SuprErrs = FALSE;
494 
495 	pvp = NULL;
496 	if (e->e_from.q_mailer == LocalMailer)
497 	{
498 # ifdef USERDB
499 		register char *p;
500 		extern char *udbsender();
501 # endif
502 
503 		if (!internal)
504 		{
505 			/* if the user has given fullname already, don't redefine */
506 			if (FullName == NULL)
507 				FullName = macvalue('x', e);
508 			if (FullName != NULL && FullName[0] == '\0')
509 				FullName = NULL;
510 
511 # ifdef USERDB
512 			p = udbsender(from);
513 
514 			if (p != NULL)
515 			{
516 				/*
517 				**  We have an alternate address for the sender
518 				*/
519 
520 				pvp = prescan(p, '\0', pvpbuf, NULL);
521 			}
522 # endif /* USERDB */
523 		}
524 
525 		if ((pw = getpwnam(e->e_from.q_user)) != NULL)
526 		{
527 			/*
528 			**  Process passwd file entry.
529 			*/
530 
531 
532 			/* extract home directory */
533 			e->e_from.q_home = newstr(pw->pw_dir);
534 			define('z', e->e_from.q_home, e);
535 
536 			/* extract user and group id */
537 			e->e_from.q_uid = pw->pw_uid;
538 			e->e_from.q_gid = pw->pw_gid;
539 
540 			/* extract full name from passwd file */
541 			if (FullName == NULL && pw->pw_gecos != NULL &&
542 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
543 			    !internal)
544 			{
545 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
546 				if (buf[0] != '\0')
547 					FullName = newstr(buf);
548 			}
549 		}
550 		if (FullName != NULL && !internal)
551 			define('x', FullName, e);
552 	}
553 	else if (!internal)
554 	{
555 		if (e->e_from.q_home == NULL)
556 			e->e_from.q_home = getenv("HOME");
557 		e->e_from.q_uid = getuid();
558 		e->e_from.q_gid = getgid();
559 	}
560 
561 	/*
562 	**  Rewrite the from person to dispose of possible implicit
563 	**	links in the net.
564 	*/
565 
566 	if (pvp == NULL)
567 		pvp = prescan(from, '\0', pvpbuf, NULL);
568 	if (pvp == NULL)
569 	{
570 		/* don't need to give error -- prescan did that already */
571 # ifdef LOG
572 		if (LogLevel > 2)
573 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
574 # endif
575 		finis();
576 	}
577 	rewrite(pvp, 3);
578 	rewrite(pvp, 1);
579 	rewrite(pvp, 4);
580 	cataddr(pvp, buf, sizeof buf, '\0');
581 	e->e_sender = newstr(buf);
582 	define('f', e->e_sender, e);
583 
584 	/* save the domain spec if this mailer wants it */
585 	if (!internal && e->e_from.q_mailer != NULL &&
586 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
587 	{
588 		extern char **copyplist();
589 
590 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
591 			pvp++;
592 		if (*pvp != NULL)
593 			e->e_fromdomain = copyplist(pvp, TRUE);
594 	}
595 }
596