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