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[] = "@(#)recipient.c	5.3 (Berkeley) 06/08/85";
13 #endif not lint
14 
15 # include <pwd.h>
16 # include "sendmail.h"
17 # include <sys/stat.h>
18 
19 /*
20 **  SENDTOLIST -- Designate a send list.
21 **
22 **	The parameter is a comma-separated list of people to send to.
23 **	This routine arranges to send to all of them.
24 **
25 **	Parameters:
26 **		list -- the send list.
27 **		ctladdr -- the address template for the person to
28 **			send to -- effective uid/gid are important.
29 **			This is typically the alias that caused this
30 **			expansion.
31 **		sendq -- a pointer to the head of a queue to put
32 **			these people into.
33 **
34 **	Returns:
35 **		none
36 **
37 **	Side Effects:
38 **		none.
39 */
40 
41 # define MAXRCRSN	10
42 
43 sendtolist(list, ctladdr, sendq)
44 	char *list;
45 	ADDRESS *ctladdr;
46 	ADDRESS **sendq;
47 {
48 	register char *p;
49 	register ADDRESS *al;	/* list of addresses to send to */
50 	bool firstone;		/* set on first address sent */
51 	bool selfref;		/* set if this list includes ctladdr */
52 	char delimiter;		/* the address delimiter */
53 
54 # ifdef DEBUG
55 	if (tTd(25, 1))
56 	{
57 		printf("sendto: %s\n   ctladdr=", list);
58 		printaddr(ctladdr, FALSE);
59 	}
60 # endif DEBUG
61 
62 	/* heuristic to determine old versus new style addresses */
63 	if (ctladdr == NULL &&
64 	    (index(list, ',') != NULL || index(list, ';') != NULL ||
65 	     index(list, '<') != NULL || index(list, '(') != NULL))
66 		CurEnv->e_flags &= ~EF_OLDSTYLE;
67 	delimiter = ' ';
68 	if (!bitset(EF_OLDSTYLE, CurEnv->e_flags) || ctladdr != NULL)
69 		delimiter = ',';
70 
71 	firstone = TRUE;
72 	selfref = FALSE;
73 	al = NULL;
74 
75 	for (p = list; *p != '\0'; )
76 	{
77 		register ADDRESS *a;
78 		extern char *DelimChar;		/* defined in prescan */
79 
80 		/* parse the address */
81 		while (isspace(*p) || *p == ',')
82 			p++;
83 		a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter);
84 		p = DelimChar;
85 		if (a == NULL)
86 			continue;
87 		a->q_next = al;
88 		a->q_alias = ctladdr;
89 
90 		/* see if this should be marked as a primary address */
91 		if (ctladdr == NULL ||
92 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
93 			a->q_flags |= QPRIMARY;
94 
95 		/* put on send queue or suppress self-reference */
96 		if (ctladdr != NULL && sameaddr(ctladdr, a))
97 			selfref = TRUE;
98 		else
99 			al = a;
100 		firstone = FALSE;
101 	}
102 
103 	/* if this alias doesn't include itself, delete ctladdr */
104 	if (!selfref && ctladdr != NULL)
105 		ctladdr->q_flags |= QDONTSEND;
106 
107 	/* arrange to send to everyone on the local send list */
108 	while (al != NULL)
109 	{
110 		register ADDRESS *a = al;
111 		extern ADDRESS *recipient();
112 
113 		al = a->q_next;
114 		a = recipient(a, sendq);
115 
116 		/* arrange to inherit full name */
117 		if (a->q_fullname == NULL && ctladdr != NULL)
118 			a->q_fullname = ctladdr->q_fullname;
119 	}
120 
121 	CurEnv->e_to = NULL;
122 }
123 /*
124 **  RECIPIENT -- Designate a message recipient
125 **
126 **	Saves the named person for future mailing.
127 **
128 **	Parameters:
129 **		a -- the (preparsed) address header for the recipient.
130 **		sendq -- a pointer to the head of a queue to put the
131 **			recipient in.  Duplicate supression is done
132 **			in this queue.
133 **
134 **	Returns:
135 **		The actual address in the queue.  This will be "a" if
136 **		the address is not a duplicate, else the original address.
137 **
138 **	Side Effects:
139 **		none.
140 */
141 
142 ADDRESS *
143 recipient(a, sendq)
144 	register ADDRESS *a;
145 	register ADDRESS **sendq;
146 {
147 	register ADDRESS *q;
148 	ADDRESS **pq;
149 	register struct mailer *m;
150 	register char *p;
151 	bool quoted = FALSE;		/* set if the addr has a quote bit */
152 	char buf[MAXNAME];		/* unquoted image of the user name */
153 	extern ADDRESS *getctladdr();
154 	extern bool safefile();
155 
156 	CurEnv->e_to = a->q_paddr;
157 	m = a->q_mailer;
158 	errno = 0;
159 # ifdef DEBUG
160 	if (tTd(26, 1))
161 	{
162 		printf("\nrecipient: ");
163 		printaddr(a, FALSE);
164 	}
165 # endif DEBUG
166 
167 	/* break aliasing loops */
168 	if (AliasLevel > MAXRCRSN)
169 	{
170 		usrerr("aliasing/forwarding loop broken");
171 		return (a);
172 	}
173 
174 	/*
175 	**  Finish setting up address structure.
176 	*/
177 
178 	/* set the queue timeout */
179 	a->q_timeout = TimeOut;
180 
181 	/* map user & host to lower case if requested on non-aliases */
182 	if (a->q_alias == NULL)
183 		loweraddr(a);
184 
185 	/* get unquoted user for file, program or user.name check */
186 	(void) strcpy(buf, a->q_user);
187 	for (p = buf; *p != '\0' && !quoted; p++)
188 	{
189 		if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377))
190 			quoted = TRUE;
191 	}
192 	stripquotes(buf, TRUE);
193 
194 	/* do sickly crude mapping for program mailing, etc. */
195 	if (m == LocalMailer && buf[0] == '|')
196 	{
197 		a->q_mailer = m = ProgMailer;
198 		a->q_user++;
199 		if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
200 		{
201 			usrerr("Cannot mail directly to programs");
202 			a->q_flags |= QDONTSEND;
203 		}
204 	}
205 
206 	/*
207 	**  Look up this person in the recipient list.
208 	**	If they are there already, return, otherwise continue.
209 	**	If the list is empty, just add it.  Notice the cute
210 	**	hack to make from addresses suppress things correctly:
211 	**	the QDONTSEND bit will be set in the send list.
212 	**	[Please note: the emphasis is on "hack."]
213 	*/
214 
215 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
216 	{
217 		if (!ForceMail && sameaddr(q, a))
218 		{
219 # ifdef DEBUG
220 			if (tTd(26, 1))
221 			{
222 				printf("%s in sendq: ", a->q_paddr);
223 				printaddr(q, FALSE);
224 			}
225 # endif DEBUG
226 			if (!bitset(QDONTSEND, a->q_flags))
227 				message(Arpa_Info, "duplicate suppressed");
228 			if (!bitset(QPRIMARY, q->q_flags))
229 				q->q_flags |= a->q_flags;
230 			return (q);
231 		}
232 	}
233 
234 	/* add address on list */
235 	*pq = a;
236 	a->q_next = NULL;
237 
238 	/*
239 	**  Alias the name and handle :include: specs.
240 	*/
241 
242 	if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags))
243 	{
244 		if (strncmp(a->q_user, ":include:", 9) == 0)
245 		{
246 			a->q_flags |= QDONTSEND;
247 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
248 				usrerr("Cannot mail directly to :include:s");
249 			else
250 			{
251 				message(Arpa_Info, "including file %s", &a->q_user[9]);
252 				include(&a->q_user[9], " sending", a, sendq);
253 			}
254 		}
255 		else
256 			alias(a, sendq);
257 	}
258 
259 	/*
260 	**  If the user is local and still being sent, verify that
261 	**  the address is good.  If it is, try to forward.
262 	**  If the address is already good, we have a forwarding
263 	**  loop.  This can be broken by just sending directly to
264 	**  the user (which is probably correct anyway).
265 	*/
266 
267 	if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer)
268 	{
269 		struct stat stb;
270 		extern bool writable();
271 
272 		/* see if this is to a file */
273 		if (buf[0] == '/')
274 		{
275 			p = rindex(buf, '/');
276 			/* check if writable or creatable */
277 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
278 			{
279 				usrerr("Cannot mail directly to files");
280 				a->q_flags |= QDONTSEND;
281 			}
282 			else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
283 			    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
284 			{
285 				a->q_flags |= QBADADDR;
286 				giveresponse(EX_CANTCREAT, m, CurEnv);
287 			}
288 		}
289 		else
290 		{
291 			register struct passwd *pw;
292 			extern struct passwd *finduser();
293 
294 			/* warning -- finduser may trash buf */
295 			pw = finduser(buf);
296 			if (pw == NULL)
297 			{
298 				a->q_flags |= QBADADDR;
299 				giveresponse(EX_NOUSER, m, CurEnv);
300 			}
301 			else
302 			{
303 				char nbuf[MAXNAME];
304 
305 				if (strcmp(a->q_user, pw->pw_name) != 0)
306 				{
307 					a->q_user = newstr(pw->pw_name);
308 					(void) strcpy(buf, pw->pw_name);
309 				}
310 				a->q_home = newstr(pw->pw_dir);
311 				a->q_uid = pw->pw_uid;
312 				a->q_gid = pw->pw_gid;
313 				a->q_flags |= QGOODUID;
314 				buildfname(pw->pw_gecos, pw->pw_name, nbuf);
315 				if (nbuf[0] != '\0')
316 					a->q_fullname = newstr(nbuf);
317 				if (!quoted)
318 					forward(a, sendq);
319 			}
320 		}
321 	}
322 	return (a);
323 }
324 /*
325 **  FINDUSER -- find the password entry for a user.
326 **
327 **	This looks a lot like getpwnam, except that it may want to
328 **	do some fancier pattern matching in /etc/passwd.
329 **
330 **	This routine contains most of the time of many sendmail runs.
331 **	It deserves to be optimized.
332 **
333 **	Parameters:
334 **		name -- the name to match against.
335 **
336 **	Returns:
337 **		A pointer to a pw struct.
338 **		NULL if name is unknown or ambiguous.
339 **
340 **	Side Effects:
341 **		may modify name.
342 */
343 
344 struct passwd *
345 finduser(name)
346 	char *name;
347 {
348 	register struct passwd *pw;
349 	register char *p;
350 	extern struct passwd *getpwent();
351 	extern struct passwd *getpwnam();
352 
353 	/*
354 	**  Make name canonical.
355 	*/
356 
357 	for (p = name; *p != '\0'; p++)
358 	{
359 		if (*p == (SpaceSub & 0177) || *p == '_')
360 			*p = ' ';
361 	}
362 
363 	/* look up this login name */
364 	if ((pw = getpwnam(name)) != NULL)
365 		return (pw);
366 
367 	/* search for a matching full name instead */
368 	(void) setpwent();
369 	while ((pw = getpwent()) != NULL)
370 	{
371 		char buf[MAXNAME];
372 		extern bool sameword();
373 
374 		if (strcmp(pw->pw_name, name) == 0)
375 			return (pw);
376 		buildfname(pw->pw_gecos, pw->pw_name, buf);
377 		if (index(buf, ' ') != NULL && sameword(buf, name))
378 		{
379 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
380 			return (pw);
381 		}
382 	}
383 	return (NULL);
384 }
385 /*
386 **  WRITABLE -- predicate returning if the file is writable.
387 **
388 **	This routine must duplicate the algorithm in sys/fio.c.
389 **	Unfortunately, we cannot use the access call since we
390 **	won't necessarily be the real uid when we try to
391 **	actually open the file.
392 **
393 **	Notice that ANY file with ANY execute bit is automatically
394 **	not writable.  This is also enforced by mailfile.
395 **
396 **	Parameters:
397 **		s -- pointer to a stat struct for the file.
398 **
399 **	Returns:
400 **		TRUE -- if we will be able to write this file.
401 **		FALSE -- if we cannot write this file.
402 **
403 **	Side Effects:
404 **		none.
405 */
406 
407 bool
408 writable(s)
409 	register struct stat *s;
410 {
411 	int euid, egid;
412 	int bits;
413 
414 	if (bitset(0111, s->st_mode))
415 		return (FALSE);
416 	euid = getruid();
417 	egid = getrgid();
418 	if (geteuid() == 0)
419 	{
420 		if (bitset(S_ISUID, s->st_mode))
421 			euid = s->st_uid;
422 		if (bitset(S_ISGID, s->st_mode))
423 			egid = s->st_gid;
424 	}
425 
426 	if (euid == 0)
427 		return (TRUE);
428 	bits = S_IWRITE;
429 	if (euid != s->st_uid)
430 	{
431 		bits >>= 3;
432 		if (egid != s->st_gid)
433 			bits >>= 3;
434 	}
435 	return ((s->st_mode & bits) != 0);
436 }
437 /*
438 **  INCLUDE -- handle :include: specification.
439 **
440 **	Parameters:
441 **		fname -- filename to include.
442 **		msg -- message to print in verbose mode.
443 **		ctladdr -- address template to use to fill in these
444 **			addresses -- effective user/group id are
445 **			the important things.
446 **		sendq -- a pointer to the head of the send queue
447 **			to put these addresses in.
448 **
449 **	Returns:
450 **		none.
451 **
452 **	Side Effects:
453 **		reads the :include: file and sends to everyone
454 **		listed in that file.
455 */
456 
457 include(fname, msg, ctladdr, sendq)
458 	char *fname;
459 	char *msg;
460 	ADDRESS *ctladdr;
461 	ADDRESS **sendq;
462 {
463 	char buf[MAXLINE];
464 	register FILE *fp;
465 	char *oldto = CurEnv->e_to;
466 	char *oldfilename = FileName;
467 	int oldlinenumber = LineNumber;
468 
469 	fp = fopen(fname, "r");
470 	if (fp == NULL)
471 	{
472 		usrerr("Cannot open %s", fname);
473 		return;
474 	}
475 	if (getctladdr(ctladdr) == NULL)
476 	{
477 		struct stat st;
478 
479 		if (fstat(fileno(fp), &st) < 0)
480 			syserr("Cannot fstat %s!", fname);
481 		ctladdr->q_uid = st.st_uid;
482 		ctladdr->q_gid = st.st_gid;
483 		ctladdr->q_flags |= QGOODUID;
484 	}
485 
486 	/* read the file -- each line is a comma-separated list. */
487 	FileName = fname;
488 	LineNumber = 0;
489 	while (fgets(buf, sizeof buf, fp) != NULL)
490 	{
491 		register char *p = index(buf, '\n');
492 
493 		if (p != NULL)
494 			*p = '\0';
495 		if (buf[0] == '\0')
496 			continue;
497 		CurEnv->e_to = oldto;
498 		message(Arpa_Info, "%s to %s", msg, buf);
499 		AliasLevel++;
500 		sendtolist(buf, ctladdr, sendq);
501 		AliasLevel--;
502 	}
503 
504 	(void) fclose(fp);
505 	FileName = oldfilename;
506 	LineNumber = oldlinenumber;
507 }
508 /*
509 **  SENDTOARGV -- send to an argument vector.
510 **
511 **	Parameters:
512 **		argv -- argument vector to send to.
513 **
514 **	Returns:
515 **		none.
516 **
517 **	Side Effects:
518 **		puts all addresses on the argument vector onto the
519 **			send queue.
520 */
521 
522 sendtoargv(argv)
523 	register char **argv;
524 {
525 	register char *p;
526 	extern bool sameword();
527 
528 	while ((p = *argv++) != NULL)
529 	{
530 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
531 		{
532 			char nbuf[MAXNAME];
533 
534 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
535 				usrerr("address overflow");
536 			else
537 			{
538 				(void) strcpy(nbuf, p);
539 				(void) strcat(nbuf, "@");
540 				(void) strcat(nbuf, argv[1]);
541 				p = newstr(nbuf);
542 				argv += 2;
543 			}
544 		}
545 		sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
546 	}
547 }
548 /*
549 **  GETCTLADDR -- get controlling address from an address header.
550 **
551 **	If none, get one corresponding to the effective userid.
552 **
553 **	Parameters:
554 **		a -- the address to find the controller of.
555 **
556 **	Returns:
557 **		the controlling address.
558 **
559 **	Side Effects:
560 **		none.
561 */
562 
563 ADDRESS *
564 getctladdr(a)
565 	register ADDRESS *a;
566 {
567 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
568 		a = a->q_alias;
569 	return (a);
570 }
571