1 # include <pwd.h>
2 # include "sendmail.h"
3 # include <sys/stat.h>
4 
5 SCCSID(@(#)recipient.c	3.42		09/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 			continue;
69 		p = DelimChar;
70 		a->q_next = al;
71 		a->q_alias = ctladdr;
72 
73 		/* see if this should be marked as a primary address */
74 		if (ctladdr == NULL ||
75 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
76 			a->q_flags |= QPRIMARY;
77 
78 		/* put on send queue or suppress self-reference */
79 		if (ctladdr != NULL && sameaddr(ctladdr, a, FALSE))
80 			selfref = TRUE;
81 		else
82 			al = a;
83 		firstone = FALSE;
84 	}
85 
86 	/* if this alias doesn't include itself, delete ctladdr */
87 	if (!selfref && ctladdr != NULL)
88 		ctladdr->q_flags |= QDONTSEND;
89 
90 	/* arrange to send to everyone on the local send list */
91 	while (al != NULL)
92 	{
93 		register ADDRESS *a = al;
94 
95 		al = a->q_next;
96 		recipient(a, sendq);
97 
98 		/* arrange to inherit full name */
99 		if (a->q_fullname == NULL && ctladdr != NULL)
100 			a->q_fullname = ctladdr->q_fullname;
101 	}
102 
103 	CurEnv->e_to = NULL;
104 }
105 /*
106 **  RECIPIENT -- Designate a message recipient
107 **
108 **	Saves the named person for future mailing.
109 **
110 **	Parameters:
111 **		a -- the (preparsed) address header for the recipient.
112 **		sendq -- a pointer to the head of a queue to put the
113 **			recipient in.  Duplicate supression is done
114 **			in this queue.
115 **
116 **	Returns:
117 **		none.
118 **
119 **	Side Effects:
120 **		none.
121 */
122 
123 recipient(a, sendq)
124 	register ADDRESS *a;
125 	register ADDRESS **sendq;
126 {
127 	register ADDRESS *q;
128 	ADDRESS **pq;
129 	register struct mailer *m;
130 	extern ADDRESS *getctladdr();
131 	extern bool safefile();
132 
133 	CurEnv->e_to = a->q_paddr;
134 	m = a->q_mailer;
135 	errno = 0;
136 # ifdef DEBUG
137 	if (tTd(26, 1))
138 	{
139 		printf("\nrecipient: ");
140 		printaddr(a, FALSE);
141 	}
142 # endif DEBUG
143 
144 	/* break aliasing loops */
145 	if (AliasLevel > MAXRCRSN)
146 	{
147 		usrerr("aliasing/forwarding loop broken");
148 		return;
149 	}
150 
151 	/*
152 	**  Finish setting up address structure.
153 	*/
154 
155 	a->q_timeout = TimeOut;
156 
157 	/* do sickly crude mapping for program mailing, etc. */
158 	if (a->q_mailer == LocalMailer)
159 	{
160 		if (a->q_user[0] == '|')
161 		{
162 			a->q_mailer = m = ProgMailer;
163 			a->q_user++;
164 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
165 			{
166 				usrerr("Cannot mail directly to programs");
167 				a->q_flags |= QDONTSEND;
168 			}
169 		}
170 	}
171 
172 	/*
173 	**  Look up this person in the recipient list.
174 	**	If they are there already, return, otherwise continue.
175 	**	If the list is empty, just add it.  Notice the cute
176 	**	hack to make from addresses suppress things correctly:
177 	**	the QDONTSEND bit will be set in the send list.
178 	**	[Please note: the emphasis is on "hack."]
179 	*/
180 
181 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
182 	{
183 		if (!ForceMail && sameaddr(q, a, FALSE))
184 		{
185 # ifdef DEBUG
186 			if (tTd(26, 1))
187 			{
188 				printf("%s in sendq: ", a->q_paddr);
189 				printaddr(q, FALSE);
190 			}
191 # endif DEBUG
192 			if (!bitset(QDONTSEND, a->q_flags))
193 				message(Arpa_Info, "duplicate suppressed");
194 			if (!bitset(QPRIMARY, q->q_flags))
195 				q->q_flags |= a->q_flags;
196 			return;
197 		}
198 	}
199 
200 	/* add address on list */
201 	*pq = a;
202 	a->q_next = NULL;
203 	if (DontSend)
204 		a->q_flags |= QDONTSEND;
205 
206 	/*
207 	**  Alias the name and handle :include: specs.
208 	*/
209 
210 	if (a->q_mailer == LocalMailer)
211 	{
212 		if (strncmp(a->q_user, ":include:", 9) == 0)
213 		{
214 			a->q_flags |= QDONTSEND;
215 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
216 				usrerr("Cannot mail directly to :include:s");
217 			else
218 			{
219 				message(Arpa_Info, "including file %s", &a->q_user[9]);
220 				include(&a->q_user[9], " sending", a, sendq);
221 			}
222 		}
223 		else
224 			alias(a, sendq);
225 	}
226 
227 	/*
228 	**  If the user is local and still being sent, verify that
229 	**  the address is good.  If it is, try to forward.
230 	**  If the address is already good, we have a forwarding
231 	**  loop.  This can be broken by just sending directly to
232 	**  the user (which is probably correct anyway).
233 	*/
234 
235 	if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == LocalMailer)
236 	{
237 		char buf[MAXNAME];
238 		register char *p;
239 		struct stat stb;
240 		extern bool writable();
241 		bool quoted = FALSE;
242 
243 		(void) strcpy(buf, a->q_user);
244 		for (p = buf; *p != '\0' && !quoted; p++)
245 		{
246 			if (!isascii(*p) && (*p & 0377) != (SPACESUB & 0377))
247 				quoted = TRUE;
248 		}
249 		stripquotes(buf, TRUE);
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