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