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